From: Conor McCarthy <cmccarthy@codeweavers.com> --- dlls/windows.storage/Makefile.in | 2 + dlls/windows.storage/async.c | 531 +++++++++++++++++++++++++ dlls/windows.storage/async_private.idl | 46 +++ dlls/windows.storage/private.h | 7 + dlls/windows.storage/streams.c | 77 +++- dlls/windows.storage/tests/storage.c | 3 - 6 files changed, 661 insertions(+), 5 deletions(-) create mode 100644 dlls/windows.storage/async.c create mode 100644 dlls/windows.storage/async_private.idl diff --git a/dlls/windows.storage/Makefile.in b/dlls/windows.storage/Makefile.in index bb446a2be99..adc8597dd11 100644 --- a/dlls/windows.storage/Makefile.in +++ b/dlls/windows.storage/Makefile.in @@ -2,6 +2,8 @@ MODULE = windows.storage.dll IMPORTS = shell32 combase SOURCES = \ + async.c \ + async_private.idl \ classes.idl \ main.c \ streams.c diff --git a/dlls/windows.storage/async.c b/dlls/windows.storage/async.c new file mode 100644 index 00000000000..090ae84fac9 --- /dev/null +++ b/dlls/windows.storage/async.c @@ -0,0 +1,531 @@ +/* WinRT IAsync* implementation + * + * Copyright 2022 Bernhard Kölbl for CodeWeavers + * Copyright 2022 Rémi Bernon for CodeWeavers + * Copyright 2026 Conor McCarthy for CodeWeavers + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#define WIDL_using_Wine_Internal +#include "private.h" +#include "initguid.h" + +#include "wine/debug.h" + +WINE_DEFAULT_DEBUG_CHANNEL(storage); + +#define Closed 4 +#define HANDLER_NOT_SET ((void *)~(ULONG_PTR)0) + +struct async_info +{ + IAsyncInfoImpl IAsyncInfoImpl_iface; + IAsyncInfo IAsyncInfo_iface; + IInspectable *IInspectable_outer; + LONG ref; + + async_operation_callback callback; + TP_WORK *async_run_work; + IUnknown *invoker; + IUnknown *param; + + CRITICAL_SECTION cs; + IAsyncOperationCompletedHandlerImpl *handler; + PROPVARIANT result; + AsyncStatus status; + HRESULT hr; +}; + +static inline struct async_info *impl_from_IAsyncInfoImpl( IAsyncInfoImpl *iface ) +{ + return CONTAINING_RECORD( iface, struct async_info, IAsyncInfoImpl_iface ); +} + +static HRESULT WINAPI async_impl_QueryInterface( IAsyncInfoImpl *iface, REFIID iid, void **out ) +{ + struct async_info *impl = impl_from_IAsyncInfoImpl( iface ); + + TRACE( "iface %p, iid %s, out %p.\n", iface, debugstr_guid( iid ), out ); + + if (IsEqualGUID( iid, &IID_IUnknown ) || + IsEqualGUID( iid, &IID_IInspectable ) || + IsEqualGUID( iid, &IID_IAgileObject ) || + IsEqualGUID( iid, &IID_IAsyncInfoImpl )) + { + IInspectable_AddRef( (*out = &impl->IAsyncInfoImpl_iface) ); + return S_OK; + } + + if (IsEqualGUID( iid, &IID_IAsyncInfo )) + { + IInspectable_AddRef( (*out = &impl->IAsyncInfo_iface) ); + return S_OK; + } + + FIXME( "%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid( iid ) ); + *out = NULL; + return E_NOINTERFACE; +} + +static ULONG WINAPI async_impl_AddRef( IAsyncInfoImpl *iface ) +{ + struct async_info *impl = impl_from_IAsyncInfoImpl( iface ); + ULONG ref = InterlockedIncrement( &impl->ref ); + TRACE( "iface %p, ref %lu.\n", iface, ref ); + return ref; +} + +static ULONG WINAPI async_impl_Release( IAsyncInfoImpl *iface ) +{ + struct async_info *impl = impl_from_IAsyncInfoImpl( iface ); + ULONG ref = InterlockedDecrement( &impl->ref ); + TRACE( "iface %p, ref %lu.\n", iface, ref ); + + if (!ref) + { + if (impl->handler && impl->handler != HANDLER_NOT_SET) IAsyncOperationCompletedHandlerImpl_Release( impl->handler ); + IAsyncInfo_Close( &impl->IAsyncInfo_iface ); + if (impl->param) IUnknown_Release( impl->param ); + if (impl->invoker) IUnknown_Release( impl->invoker ); + PropVariantClear( &impl->result ); + impl->cs.DebugInfo->Spare[0] = 0; + DeleteCriticalSection( &impl->cs ); + free( impl ); + } + + return ref; +} + +static HRESULT WINAPI async_impl_put_Completed( IAsyncInfoImpl *iface, IAsyncOperationCompletedHandlerImpl *handler ) +{ + struct async_info *impl = impl_from_IAsyncInfoImpl( iface ); + HRESULT hr = S_OK; + + TRACE( "iface %p, handler %p.\n", iface, handler ); + + EnterCriticalSection( &impl->cs ); + if (impl->status == Closed) hr = E_ILLEGAL_METHOD_CALL; + else if (impl->handler != HANDLER_NOT_SET) hr = E_ILLEGAL_DELEGATE_ASSIGNMENT; + else if ((impl->handler = handler)) + { + IAsyncOperationCompletedHandlerImpl_AddRef( impl->handler ); + + if (impl->status > Started) + { + IInspectable *operation = impl->IInspectable_outer; + AsyncStatus status = impl->status; + impl->handler = NULL; /* Prevent concurrent invoke. */ + LeaveCriticalSection( &impl->cs ); + + IAsyncOperationCompletedHandlerImpl_Invoke( handler, operation, status ); + IAsyncOperationCompletedHandlerImpl_Release( handler ); + + return S_OK; + } + } + LeaveCriticalSection( &impl->cs ); + + return hr; +} + +static HRESULT WINAPI async_impl_get_Completed( IAsyncInfoImpl *iface, IAsyncOperationCompletedHandlerImpl **handler ) +{ + struct async_info *impl = impl_from_IAsyncInfoImpl( iface ); + HRESULT hr = S_OK; + + TRACE( "iface %p, handler %p.\n", iface, handler ); + + EnterCriticalSection( &impl->cs ); + if (impl->status == Closed) hr = E_ILLEGAL_METHOD_CALL; + if (impl->handler == NULL || impl->handler == HANDLER_NOT_SET) *handler = NULL; + else IAsyncOperationCompletedHandlerImpl_AddRef( (*handler = impl->handler) ); + LeaveCriticalSection( &impl->cs ); + + return hr; +} + +static HRESULT WINAPI async_impl_get_Result( IAsyncInfoImpl *iface, PROPVARIANT *result ) +{ + struct async_info *impl = impl_from_IAsyncInfoImpl( iface ); + HRESULT hr = E_ILLEGAL_METHOD_CALL; + + TRACE( "iface %p, result %p.\n", iface, result ); + + EnterCriticalSection( &impl->cs ); + if (impl->status == Completed || impl->status == Error) + { + PropVariantCopy( result, &impl->result ); + hr = impl->hr; + } + LeaveCriticalSection( &impl->cs ); + + return hr; +} + +static BOOL async_info_complete( struct async_info *impl, BOOL called_async ) +{ + IInspectable *operation = impl->IInspectable_outer; + PROPVARIANT result = {0}; + HRESULT hr; + + hr = impl->callback( impl->invoker, impl->param, &result, called_async ); + if (!called_async && hr == STATUS_PENDING) return FALSE; + + EnterCriticalSection( &impl->cs ); + if (impl->status != Closed) impl->status = FAILED(hr) ? Error : Completed; + PropVariantCopy( &impl->result, &result ); + impl->hr = hr; + + if (impl->handler != NULL && impl->handler != HANDLER_NOT_SET) + { + IAsyncOperationCompletedHandlerImpl *handler = impl->handler; + AsyncStatus status = impl->status; + impl->handler = NULL; /* Prevent concurrent invoke. */ + LeaveCriticalSection( &impl->cs ); + + IAsyncOperationCompletedHandlerImpl_Invoke( handler, operation, status ); + IAsyncOperationCompletedHandlerImpl_Release( handler ); + } + else LeaveCriticalSection( &impl->cs ); + + /* release refcount acquired in Start */ + IInspectable_Release( operation ); + + PropVariantClear( &result ); + return TRUE; +} + +static HRESULT WINAPI async_impl_Start( IAsyncInfoImpl *iface ) +{ + struct async_info *impl = impl_from_IAsyncInfoImpl( iface ); + + TRACE( "iface %p.\n", iface ); + + /* keep the async alive in the callback */ + IInspectable_AddRef( impl->IInspectable_outer ); + if (!async_info_complete( impl, FALSE )) SubmitThreadpoolWork( impl->async_run_work ); + + return S_OK; +} + +static const struct IAsyncInfoImplVtbl async_impl_vtbl = +{ + /* IUnknown methods */ + async_impl_QueryInterface, + async_impl_AddRef, + async_impl_Release, + /* IAsyncInfoImpl */ + async_impl_put_Completed, + async_impl_get_Completed, + async_impl_get_Result, + async_impl_Start, +}; + +DEFINE_IINSPECTABLE_OUTER( async_info, IAsyncInfo, struct async_info, IInspectable_outer ) + +static HRESULT WINAPI async_info_get_Id( IAsyncInfo *iface, UINT32 *id ) +{ + struct async_info *impl = impl_from_IAsyncInfo( iface ); + HRESULT hr = S_OK; + + TRACE( "iface %p, id %p.\n", iface, id ); + + EnterCriticalSection( &impl->cs ); + if (impl->status == Closed) hr = E_ILLEGAL_METHOD_CALL; + *id = 1; + LeaveCriticalSection( &impl->cs ); + + return hr; +} + +static HRESULT WINAPI async_info_get_Status( IAsyncInfo *iface, AsyncStatus *status ) +{ + struct async_info *impl = impl_from_IAsyncInfo( iface ); + HRESULT hr = S_OK; + + TRACE( "iface %p, status %p.\n", iface, status ); + + EnterCriticalSection( &impl->cs ); + if (impl->status == Closed) hr = E_ILLEGAL_METHOD_CALL; + *status = impl->status; + LeaveCriticalSection( &impl->cs ); + + return hr; +} + +static HRESULT WINAPI async_info_get_ErrorCode( IAsyncInfo *iface, HRESULT *error_code ) +{ + struct async_info *impl = impl_from_IAsyncInfo( iface ); + HRESULT hr = S_OK; + + TRACE( "iface %p, error_code %p.\n", iface, error_code ); + + EnterCriticalSection( &impl->cs ); + if (impl->status == Closed) *error_code = hr = E_ILLEGAL_METHOD_CALL; + else *error_code = impl->hr; + LeaveCriticalSection( &impl->cs ); + + return hr; +} + +static HRESULT WINAPI async_info_Cancel( IAsyncInfo *iface ) +{ + struct async_info *impl = impl_from_IAsyncInfo( iface ); + HRESULT hr = S_OK; + + TRACE( "iface %p.\n", iface ); + + EnterCriticalSection( &impl->cs ); + if (impl->status == Closed) hr = E_ILLEGAL_METHOD_CALL; + else if (impl->status == Started) impl->status = Canceled; + LeaveCriticalSection( &impl->cs ); + + return hr; +} + +static HRESULT WINAPI async_info_Close( IAsyncInfo *iface ) +{ + struct async_info *impl = impl_from_IAsyncInfo( iface ); + HRESULT hr = S_OK; + + TRACE( "iface %p.\n", iface ); + + EnterCriticalSection( &impl->cs ); + if (impl->status == Started) + hr = E_ILLEGAL_STATE_CHANGE; + else if (impl->status != Closed) + { + CloseThreadpoolWork( impl->async_run_work ); + impl->async_run_work = NULL; + impl->status = Closed; + } + LeaveCriticalSection( &impl->cs ); + + return hr; +} + +static const struct IAsyncInfoVtbl async_info_vtbl = +{ + /* IUnknown methods */ + async_info_QueryInterface, + async_info_AddRef, + async_info_Release, + /* IInspectable methods */ + async_info_GetIids, + async_info_GetRuntimeClassName, + async_info_GetTrustLevel, + /* IAsyncInfo */ + async_info_get_Id, + async_info_get_Status, + async_info_get_ErrorCode, + async_info_Cancel, + async_info_Close, +}; + +static void CALLBACK async_info_callback( TP_CALLBACK_INSTANCE *instance, void *iface, TP_WORK *work ) +{ + struct async_info *impl = impl_from_IAsyncInfoImpl( iface ); + + async_info_complete( impl, TRUE ); +} + +static HRESULT async_info_create( IUnknown *invoker, IUnknown *param, async_operation_callback callback, + IInspectable *outer, IAsyncInfoImpl **out ) +{ + struct async_info *impl; + HRESULT hr; + + if (!(impl = calloc( 1, sizeof(struct async_info) ))) return E_OUTOFMEMORY; + impl->IAsyncInfoImpl_iface.lpVtbl = &async_impl_vtbl; + impl->IAsyncInfo_iface.lpVtbl = &async_info_vtbl; + impl->IInspectable_outer = outer; + impl->ref = 1; + + impl->callback = callback; + impl->handler = HANDLER_NOT_SET; + impl->status = Started; + if (!(impl->async_run_work = CreateThreadpoolWork( async_info_callback, &impl->IAsyncInfoImpl_iface, NULL ))) + { + hr = HRESULT_FROM_WIN32( GetLastError() ); + free( impl ); + return hr; + } + + if ((impl->invoker = invoker)) IUnknown_AddRef( impl->invoker ); + if ((impl->param = param)) IUnknown_AddRef( impl->param ); + + InitializeCriticalSectionEx( &impl->cs, 0, RTL_CRITICAL_SECTION_FLAG_FORCE_DEBUG_INFO ); + impl->cs.DebugInfo->Spare[0] = (DWORD_PTR)( __FILE__ ": async_info.cs" ); + + *out = &impl->IAsyncInfoImpl_iface; + return S_OK; +} + +#define DEFINE_IASYNCOPERATIONWITHPROGRESS( iface_type, progress_type, completed_type, cs_type_str, impl_type, \ + result_type, propvar_type, propvar_name, creation_fn ) \ +static inline struct impl_type *impl_from_##iface_type( iface_type *iface ) \ +{ \ + return CONTAINING_RECORD( iface, struct impl_type, iface_type##_iface ); \ +} \ +static HRESULT WINAPI impl_type##_QueryInterface( iface_type *iface, REFIID iid, void **out ) \ +{ \ + struct impl_type *impl = impl_from_##iface_type( iface ); \ + \ + TRACE( "iface %p, iid %s, out %p.\n", iface, debugstr_guid( iid ), out ); \ + \ + if (IsEqualGUID( iid, &IID_IUnknown ) || \ + IsEqualGUID( iid, &IID_IInspectable ) || \ + IsEqualGUID( iid, &IID_IAgileObject ) || \ + IsEqualGUID( iid, &IID_##iface_type )) \ + { \ + IInspectable_AddRef( (*out = &impl->iface_type##_iface) ); \ + return S_OK; \ + } \ + \ + return IAsyncInfoImpl_QueryInterface( impl->IAsyncInfoImpl_inner, iid, out ); \ +} \ +static ULONG WINAPI impl_type##_AddRef( iface_type *iface ) \ +{ \ + struct impl_type *impl = impl_from_##iface_type( iface ); \ + ULONG ref = InterlockedIncrement( &impl->ref ); \ + TRACE( "iface %p, ref %lu.\n", iface, ref ); \ + return ref; \ +} \ +static ULONG WINAPI impl_type##_Release( iface_type *iface ) \ +{ \ + struct impl_type *impl = impl_from_##iface_type( iface ); \ + ULONG ref = InterlockedDecrement( &impl->ref ); \ + TRACE( "iface %p, ref %lu.\n", iface, ref ); \ + \ + if (!ref) \ + { \ + /* guard against re-entry if inner releases an outer iface */ \ + InterlockedIncrement( &impl->ref ); \ + IAsyncInfoImpl_Release( impl->IAsyncInfoImpl_inner ); \ + free( impl ); \ + } \ + \ + return ref; \ +} \ +static HRESULT WINAPI impl_type##_GetIids( iface_type *iface, ULONG *iid_count, IID **iids ) \ +{ \ + FIXME( "iface %p, iid_count %p, iids %p stub!\n", iface, iid_count, iids ); \ + return E_NOTIMPL; \ +} \ +static HRESULT WINAPI impl_type##_GetRuntimeClassName( iface_type *iface, HSTRING *class_name ) \ +{ \ + return WindowsCreateString( L"Windows.Foundation.IAsyncOperationWithProgress`2<"cs_type_str">", \ + ARRAY_SIZE(L"Windows.Foundation.IAsyncOperationWithProgress`2<"cs_type_str">"), \ + class_name ); \ +} \ +static HRESULT WINAPI impl_type##_GetTrustLevel( iface_type *iface, TrustLevel *trust_level ) \ +{ \ + FIXME( "iface %p, trust_level %p stub!\n", iface, trust_level ); \ + return E_NOTIMPL; \ +} \ +static HRESULT WINAPI impl_type##_put_Progress( iface_type *iface, progress_type *handler ) \ +{ \ + FIXME( "iface %p, handler %p stub!\n", iface, handler ); \ + return E_NOTIMPL; \ +} \ +static HRESULT WINAPI impl_type##_get_Progress( iface_type *iface, progress_type **handler ) \ +{ \ + FIXME( "iface %p, handler %p stub!\n", iface, handler ); \ + return E_NOTIMPL; \ +} \ +static HRESULT WINAPI impl_type##_put_Completed( iface_type *iface, completed_type *typed_handler ) \ +{ \ + IAsyncOperationCompletedHandlerImpl *handler = (IAsyncOperationCompletedHandlerImpl *)typed_handler; \ + struct impl_type *impl = impl_from_##iface_type( iface ); \ + TRACE( "iface %p, handler %p.\n", iface, handler ); \ + return IAsyncInfoImpl_put_Completed( impl->IAsyncInfoImpl_inner, handler ); \ +} \ +static HRESULT WINAPI impl_type##_get_Completed( iface_type *iface, completed_type **typed_handler ) \ +{ \ + IAsyncOperationCompletedHandlerImpl **handler = (IAsyncOperationCompletedHandlerImpl **)typed_handler; \ + struct impl_type *impl = impl_from_##iface_type( iface ); \ + TRACE( "iface %p, handler %p.\n", iface, handler ); \ + return IAsyncInfoImpl_get_Completed( impl->IAsyncInfoImpl_inner, handler ); \ +} \ +static HRESULT WINAPI impl_type##_GetResults( iface_type *iface, result_type *results ) \ +{ \ + struct impl_type *impl = impl_from_##iface_type( iface ); \ + PROPVARIANT result = {.vt = propvar_type}; \ + HRESULT hr; \ + \ + TRACE( "iface %p, results %p.\n", iface, results ); \ + \ + hr = IAsyncInfoImpl_get_Result( impl->IAsyncInfoImpl_inner, &result ); \ + \ + *results = (result_type)result.propvar_name; \ + if (propvar_type == VT_UNKNOWN && result.punkVal) IUnknown_AddRef( result.punkVal ); \ + PropVariantClear( &result ); \ + return hr; \ +} \ +static const struct iface_type##Vtbl impl_type##_vtbl = \ +{ \ + /* IUnknown methods */ \ + impl_type##_QueryInterface, \ + impl_type##_AddRef, \ + impl_type##_Release, \ + /* IInspectable methods */ \ + impl_type##_GetIids, \ + impl_type##_GetRuntimeClassName, \ + impl_type##_GetTrustLevel, \ + /* IAsyncOperationWithProgress<result_type,prog_type> */ \ + impl_type##_put_Progress, \ + impl_type##_get_Progress, \ + impl_type##_put_Completed, \ + impl_type##_get_Completed, \ + impl_type##_GetResults, \ +}; \ +HRESULT creation_fn( IUnknown *invoker, IUnknown *param, \ + async_operation_callback callback, iface_type **out ) \ +{ \ + struct impl_type *impl; \ + HRESULT hr; \ + \ + *out = NULL; \ + if (!(impl = calloc( 1, sizeof(*impl) ))) return E_OUTOFMEMORY; \ + impl->iface_type##_iface.lpVtbl = &impl_type##_vtbl; \ + impl->ref = 1; \ + \ + if (FAILED(hr = async_info_create( invoker, param, callback, \ + (IInspectable *)&impl->iface_type##_iface, &impl->IAsyncInfoImpl_inner )) \ + || FAILED(hr = IAsyncInfoImpl_Start( impl->IAsyncInfoImpl_inner ))) \ + { \ + TRACE("hr %#lx\n", hr ); \ + if (impl->IAsyncInfoImpl_inner) IAsyncInfoImpl_Release( impl->IAsyncInfoImpl_inner ); \ + free( impl ); \ + return hr; \ + } \ + \ + *out = &impl->iface_type##_iface; \ + TRACE( "created IAsyncOperationWithProgress %p\n", *out ); \ + return S_OK; \ +} \ + +struct async_uint32_uint32 +{ + IAsyncOperationWithProgress_UINT32_UINT32 IAsyncOperationWithProgress_UINT32_UINT32_iface; + IAsyncInfoImpl *IAsyncInfoImpl_inner; + LONG ref; +}; + +DEFINE_IASYNCOPERATIONWITHPROGRESS( IAsyncOperationWithProgress_UINT32_UINT32, \ + IAsyncOperationProgressHandler_UINT32_UINT32, \ + IAsyncOperationWithProgressCompletedHandler_UINT32_UINT32, \ + "UInt32,UInt32", async_uint32_uint32, UINT32, VT_UI4, ulVal, async_operation_uint32_uint32_create ) diff --git a/dlls/windows.storage/async_private.idl b/dlls/windows.storage/async_private.idl new file mode 100644 index 00000000000..40ea63ca4c1 --- /dev/null +++ b/dlls/windows.storage/async_private.idl @@ -0,0 +1,46 @@ +/* + * Copyright 2022 Rémi Bernon for CodeWeavers + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#pragma makedep header + +#ifdef __WIDL__ +#pragma winrt ns_prefix +#endif + +import "propidl.idl"; +import "inspectable.idl"; +import "asyncinfo.idl"; +import "windowscontracts.idl"; + +namespace Wine.Internal { + /* type-pruning version of AsyncOperationCompletedHandlerImpl<T> */ + delegate HRESULT AsyncOperationCompletedHandlerImpl([in] IInspectable *async, [in] AsyncStatus status); + + [ + uuid(d81ab70d-82e0-481c-983d-401225d98a2c) + ] + interface IAsyncInfoImpl : IUnknown + { + [propput] HRESULT Completed([in] AsyncOperationCompletedHandlerImpl *handler); + [propget] HRESULT Completed([out, retval] AsyncOperationCompletedHandlerImpl **handler); + [propget] HRESULT Result([out, retval] PROPVARIANT *result); + HRESULT Start(); + } + + typedef HRESULT (*async_operation_callback)( IUnknown *invoker, IUnknown *param, PROPVARIANT *result, BOOL called_async ); +} diff --git a/dlls/windows.storage/private.h b/dlls/windows.storage/private.h index 6387288a399..fdf7ad48769 100644 --- a/dlls/windows.storage/private.h +++ b/dlls/windows.storage/private.h @@ -38,6 +38,11 @@ #define WIDL_using_Windows_Storage_Streams #include "windows.storage.h" #include "windows.storage.streams.h" +#include "robuffer.h" +#include "async_private.h" + +HRESULT async_operation_uint32_uint32_create( IUnknown *invoker, IUnknown *param, async_operation_callback callback, + IAsyncOperationWithProgress_UINT32_UINT32 **out ); extern IActivationFactory *random_access_stream_reference_factory; extern IActivationFactory *memory_stream_activation_factory; @@ -79,5 +84,7 @@ extern IActivationFactory *memory_stream_activation_factory; } #define DEFINE_IINSPECTABLE( pfx, iface_type, impl_type, base_iface ) \ DEFINE_IINSPECTABLE_( pfx, iface_type, impl_type, impl_from_##iface_type, iface_type##_iface, &impl->base_iface ) +#define DEFINE_IINSPECTABLE_OUTER( pfx, iface_type, impl_type, outer_iface ) \ + DEFINE_IINSPECTABLE_( pfx, iface_type, impl_type, impl_from_##iface_type, iface_type##_iface, impl->outer_iface ) #endif diff --git a/dlls/windows.storage/streams.c b/dlls/windows.storage/streams.c index 7edb9eedf04..292b3d25748 100644 --- a/dlls/windows.storage/streams.c +++ b/dlls/windows.storage/streams.c @@ -18,6 +18,7 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA */ +#include <assert.h> #include "private.h" WINE_DEFAULT_DEBUG_CHANNEL(storage); @@ -178,9 +179,35 @@ struct memory_stream IClosable IClosable_iface; LONG ref; + BYTE *buffer; + size_t capacity; + size_t size; + size_t pos; BOOL closed; }; +static HRESULT memory_stream_require_capacity( struct memory_stream *impl, size_t capacity ) +{ + BYTE *new_buffer; + + if (capacity <= impl->capacity) + return S_OK; + + capacity = max( capacity, impl->capacity + impl->capacity / 2u ); + capacity = max( capacity, 0x1000 ); + new_buffer = realloc( impl->buffer, capacity ); + + if (!new_buffer) + return HRESULT_FROM_WIN32( ERROR_DISK_FULL ); + + /* Zero memory for security and to silence address sanitisers */ + memset( &new_buffer[impl->capacity], 0, capacity - impl->capacity ); + impl->capacity = capacity; + impl->buffer = new_buffer; + + return S_OK; +} + static inline struct memory_stream *impl_from_IRandomAccessStream( IRandomAccessStream *iface ) { return CONTAINING_RECORD( iface, struct memory_stream, IRandomAccessStream_iface ); @@ -233,7 +260,12 @@ static ULONG WINAPI memory_stream_random_access_Release( IRandomAccessStream *if { struct memory_stream *impl = impl_from_IRandomAccessStream( iface ); ULONG ref = InterlockedDecrement( &impl->ref ); + TRACE( "iface %p, ref %lu.\n", iface, ref ); + + if (!ref) + free( impl->buffer ); + return ref; } @@ -510,19 +542,60 @@ static HRESULT WINAPI memory_stream_output_GetTrustLevel( IOutputStream *iface, return E_NOTIMPL; } +static HRESULT memory_stream_output_async( IUnknown *invoker, IUnknown *param, PROPVARIANT *result, BOOL called_async ) +{ + struct memory_stream *impl = impl_from_IOutputStream( (IOutputStream *)invoker ); + IBuffer *buffer = (IBuffer *)param; + IBufferByteAccess *access; + size_t capacity; + UINT32 length; + HRESULT hr; + BYTE *data; + + assert( !called_async ); + + IBuffer_get_Length( buffer, &length ); + + if (!length) + return S_OK; + + capacity = impl->pos + length; + if (capacity < impl->pos || capacity < length) + return HRESULT_FROM_WIN32( ERROR_DISK_FULL ); + + if (FAILED(hr = memory_stream_require_capacity( impl, capacity ))) + return hr; + + IBuffer_QueryInterface( buffer, &IID_IBufferByteAccess, (void **)&access ); + IBufferByteAccess_Buffer( access, &data ); + IBufferByteAccess_Release( access ); + + memcpy( &impl->buffer[impl->pos], data, length ); + impl->pos += length; + impl->size = max( impl->size, impl->pos ); + + result->vt = VT_UI4; + result->ulVal = length; + + return S_OK; +} + static HRESULT WINAPI memory_stream_output_WriteAsync( IOutputStream *iface, IBuffer *buffer, IAsyncOperationWithProgress_UINT32_UINT32 **operation ) { struct memory_stream *impl = impl_from_IOutputStream( iface ); - FIXME( "iface %p, buffer %p, operation %p stub!\n", iface, buffer, operation ); + TRACE( "iface %p, buffer %p, operation %p.\n", iface, buffer, operation ); *operation = NULL; + if (!buffer) + return E_POINTER; + if (impl->closed) return RO_E_CLOSED; - return E_NOTIMPL; + return async_operation_uint32_uint32_create( (IUnknown *)iface, (IUnknown *)buffer, memory_stream_output_async, operation ); } static HRESULT WINAPI memory_stream_output_FlushAsync( IOutputStream *iface, IAsyncOperation_boolean **operation ) diff --git a/dlls/windows.storage/tests/storage.c b/dlls/windows.storage/tests/storage.c index 71aa00adb19..bade06c1ae6 100644 --- a/dlls/windows.storage/tests/storage.c +++ b/dlls/windows.storage/tests/storage.c @@ -243,9 +243,7 @@ static void output_stream_write_( unsigned int line, IOutputStream *output_strea hr = IBuffer_put_Length( buffer, count ); ok_(__FILE__, line)( hr == S_OK, "got hr %#lx.\n", hr ); hr = IOutputStream_WriteAsync( output_stream, buffer, &operation ); - todo_wine ok( hr == S_OK, "got hr %#lx.\n", hr ); - if (FAILED(hr)) return; res = await_IAsyncOperationWithProgress_UINT32_UINT32( operation, 1000 ); ok( res == 0, "await_IAsyncOperationWithProgress_UINT32_UINT32 returned %#x\n", res ); check_async_info( operation, Completed, S_OK ); @@ -415,7 +413,6 @@ static void test_InMemoryRandomAccessStream(void) ok( value64 == 8, "got pos %I64u.\n", value64 ); hr = IOutputStream_WriteAsync( output_stream, NULL, &write_op ); - todo_wine ok( hr == E_POINTER, "got hr %#lx.\n", hr ); /* Crashes on Windows if the op pointer is null * hr = IOutputStream_WriteAsync( output_stream, NULL, NULL ); */ -- GitLab https://gitlab.winehq.org/wine/wine/-/merge_requests/11297