[PATCH v5 5/5] comsvcs: Implement ISharedPropertyGroup_CreateProperty().

Jactry Zeng jzeng at codeweavers.com
Fri Oct 30 01:33:47 CDT 2020


Signed-off-by: Jactry Zeng <jzeng at codeweavers.com>

---
v5: Unlock &group->cs as soon as create_shared_property() finish.
v4:
- Storage properties in a list instead of an array.
v3:
- Fix some leaks.
- Add more tests.
- Check SysAllocString() result in CreateProperty().
v2: No changes.
---
 dlls/comsvcs/comsvcs_private.h |   2 +
 dlls/comsvcs/property.c        | 215 ++++++++++++++++++++++++++++++++-
 dlls/comsvcs/tests/property.c  |  81 +++++++++++++
 3 files changed, 296 insertions(+), 2 deletions(-)

diff --git a/dlls/comsvcs/comsvcs_private.h b/dlls/comsvcs/comsvcs_private.h
index eee6e9938c2..3eeb9d751ac 100644
--- a/dlls/comsvcs/comsvcs_private.h
+++ b/dlls/comsvcs/comsvcs_private.h
@@ -38,6 +38,7 @@ enum tid_t
     NULL_tid,
     ISharedPropertyGroupManager_tid,
     ISharedPropertyGroup_tid,
+    ISharedProperty_tid,
     LAST_tid
 };
 
@@ -46,6 +47,7 @@ static REFIID tid_ids[] =
     &IID_NULL,
     &IID_ISharedPropertyGroupManager,
     &IID_ISharedPropertyGroup,
+    &IID_ISharedProperty,
 };
 
 HRESULT get_typeinfo(enum tid_t tid, ITypeInfo **typeinfo);
diff --git a/dlls/comsvcs/property.c b/dlls/comsvcs/property.c
index 4db19491c15..76e6c13f58c 100644
--- a/dlls/comsvcs/property.c
+++ b/dlls/comsvcs/property.c
@@ -21,6 +21,14 @@
 
 WINE_DEFAULT_DEBUG_CHANNEL(comsvcs);
 
+struct shared_property
+{
+    ISharedProperty ISharedProperty_iface;
+    LONG refcount;
+    BSTR name;
+    struct list entry;
+};
+
 struct group_manager
 {
     ISharedPropertyGroupManager ISharedPropertyGroupManager_iface;
@@ -37,6 +45,8 @@ struct property_group
     LONG isolation, release;
     struct list entry;
     BSTR name;
+    struct list properties;
+    CRITICAL_SECTION cs;
 };
 
 static struct group_manager *group_manager = NULL;
@@ -51,6 +61,140 @@ static inline struct property_group *impl_from_ISharedPropertyGroup(ISharedPrope
     return CONTAINING_RECORD(iface, struct property_group, ISharedPropertyGroup_iface);
 }
 
+static inline struct shared_property *impl_from_ISharedProperty(ISharedProperty *iface)
+{
+    return CONTAINING_RECORD(iface, struct shared_property, ISharedProperty_iface);
+}
+
+static HRESULT WINAPI shared_property_QueryInterface(ISharedProperty *iface, REFIID riid, void **out)
+{
+    struct shared_property *property = impl_from_ISharedProperty(iface);
+
+    TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
+
+    if (IsEqualGUID(riid, &IID_IUnknown)
+            || IsEqualGUID(riid, &IID_IDispatch)
+            || IsEqualGUID(riid, &IID_ISharedProperty))
+    {
+        *out = &property->ISharedProperty_iface;
+        IUnknown_AddRef((IUnknown *)*out);
+        return S_OK;
+    }
+
+    WARN("%s not implemented.\n", debugstr_guid(riid));
+    *out = NULL;
+    return E_NOINTERFACE;
+}
+
+static ULONG WINAPI shared_property_AddRef(ISharedProperty *iface)
+{
+    struct shared_property *property = impl_from_ISharedProperty(iface);
+    ULONG refcount = InterlockedIncrement(&property->refcount);
+
+    TRACE("%p increasing refcount to %u.\n", iface, refcount);
+
+    return refcount;
+}
+
+static ULONG WINAPI shared_property_Release(ISharedProperty *iface)
+{
+    struct shared_property *property = impl_from_ISharedProperty(iface);
+    ULONG refcount = InterlockedDecrement(&property->refcount);
+
+    TRACE("%p decreasing refcount to %u.\n", iface, refcount);
+
+    if (!refcount)
+    {
+        SysFreeString(property->name);
+        list_remove(&property->entry);
+    }
+
+    return refcount;
+}
+
+static HRESULT WINAPI shared_property_GetTypeInfoCount(ISharedProperty *iface, UINT *info)
+{
+    TRACE("iface %p, info %p.\n", iface, info);
+
+    if (!info)
+        return E_INVALIDARG;
+    *info = 1;
+    return S_OK;
+}
+
+static HRESULT WINAPI shared_property_GetTypeInfo(ISharedProperty *iface, UINT index, LCID lcid, ITypeInfo **info)
+{
+    TRACE("iface %p, index %u, lcid %u, info %p.\n", iface, index, lcid, info);
+
+    if (index)
+        return DISP_E_BADINDEX;
+
+    return get_typeinfo(ISharedProperty_tid, info);
+}
+
+static HRESULT WINAPI shared_property_GetIDsOfNames(ISharedProperty *iface, REFIID riid, LPOLESTR *names, UINT count,
+        LCID lcid, DISPID *dispid)
+{
+    ITypeInfo *typeinfo;
+    HRESULT hr;
+
+    TRACE("iface %p, riid %s, names %p, count %u, lcid %u, dispid %p.\n",
+            iface, debugstr_guid(riid), names, count, lcid, dispid);
+
+    hr = get_typeinfo(ISharedProperty_tid, &typeinfo);
+    if (SUCCEEDED(hr))
+    {
+        hr = ITypeInfo_GetIDsOfNames(typeinfo, names, count, dispid);
+        ITypeInfo_Release(typeinfo);
+    }
+
+    return hr;
+}
+
+static HRESULT WINAPI shared_property_Invoke(ISharedProperty *iface, DISPID member, REFIID riid, LCID lcid,
+        WORD flags, DISPPARAMS *params, VARIANT *result, EXCEPINFO *except, UINT *argerr)
+{
+    ITypeInfo *typeinfo;
+    HRESULT hr;
+
+    TRACE("iface %p, member %u, riid %s, lcid %u, flags %x, params %p, result %p, except %p, argerr %p.\n",
+            iface, member, debugstr_guid(riid), lcid, flags, params, result, except, argerr);
+
+    hr = get_typeinfo(ISharedProperty_tid, &typeinfo);
+    if (SUCCEEDED(hr))
+    {
+        hr = ITypeInfo_Invoke(typeinfo, iface, member, flags, params, result, except, argerr);
+        ITypeInfo_Release(typeinfo);
+    }
+
+    return hr;
+}
+
+static HRESULT WINAPI shared_property_get_Value(ISharedProperty *iface, VARIANT *value)
+{
+    FIXME("iface %p, value %p: stub.\n", iface, value);
+    return E_NOTIMPL;
+}
+
+static HRESULT WINAPI shared_property_put_Value(ISharedProperty *iface, VARIANT value)
+{
+    FIXME("iface %p, value %s: stub.\n", iface, debugstr_variant(&value));
+    return E_NOTIMPL;
+}
+
+static const ISharedPropertyVtbl shared_property_vtbl =
+{
+    shared_property_QueryInterface,
+    shared_property_AddRef,
+    shared_property_Release,
+    shared_property_GetTypeInfoCount,
+    shared_property_GetTypeInfo,
+    shared_property_GetIDsOfNames,
+    shared_property_Invoke,
+    shared_property_get_Value,
+    shared_property_put_Value
+};
+
 static HRESULT WINAPI property_group_QueryInterface(ISharedPropertyGroup *iface, REFIID riid, void **out)
 {
     struct property_group *group = impl_from_ISharedPropertyGroup(iface);
@@ -91,7 +235,14 @@ static ULONG WINAPI property_group_Release(ISharedPropertyGroup *iface)
     if (!refcount)
     {
         struct group_manager *manager = group->parent;
+        struct shared_property *property;
 
+        EnterCriticalSection(&group->cs);
+        LIST_FOR_EACH_ENTRY(property, &group->properties, struct shared_property, entry)
+        {
+            SysFreeString(property->name);
+        }
+        LeaveCriticalSection(&group->cs);
         SysFreeString(group->name);
 
         EnterCriticalSection(&manager->cs);
@@ -99,6 +250,7 @@ static ULONG WINAPI property_group_Release(ISharedPropertyGroup *iface)
         LeaveCriticalSection(&manager->cs);
 
         ISharedPropertyGroupManager_Release(&manager->ISharedPropertyGroupManager_iface);
+        DeleteCriticalSection(&group->cs);
         heap_free(group);
     }
 
@@ -177,11 +329,68 @@ static HRESULT WINAPI property_group_get_PropertyByPosition(ISharedPropertyGroup
     return E_NOTIMPL;
 }
 
+static struct shared_property *find_shared_property(struct property_group *group, BSTR name)
+{
+    struct shared_property *property;
+
+    LIST_FOR_EACH_ENTRY(property, &group->properties, struct shared_property, entry)
+    {
+        if (!lstrcmpW(property->name, name))
+            return property;
+    }
+    return NULL;
+}
+
+static struct shared_property *create_shared_property(struct property_group *group, BSTR name, VARIANT_BOOL *exists)
+{
+    struct shared_property *property = NULL;
+    BSTR property_name;
+
+    property = find_shared_property(group, name);
+    if (property)
+    {
+        *exists = TRUE;
+        ISharedProperty_AddRef(&property->ISharedProperty_iface);
+    }
+    else
+    {
+        property_name = SysAllocString(name);
+        if (property_name)
+        {
+            property = heap_alloc(sizeof(*property));
+            if (property)
+            {
+                property->refcount = 1;
+                property->name = property_name;
+                property->ISharedProperty_iface.lpVtbl = &shared_property_vtbl;
+                *exists = FALSE;
+                list_add_tail(&group->properties, &property->entry);
+            }
+            else
+                SysFreeString(property_name);
+        }
+    }
+    return property;
+}
+
 static HRESULT WINAPI property_group_CreateProperty(ISharedPropertyGroup *iface, BSTR name, VARIANT_BOOL *exists,
         ISharedProperty **property)
 {
-    FIXME("iface %p, name %s, exists %p, property %p: stub.\n", iface, debugstr_w(name), exists, property);
-    return E_NOTIMPL;
+    struct property_group *group = impl_from_ISharedPropertyGroup(iface);
+    struct shared_property *shared_property;
+
+    TRACE("iface %p, name %s, exists %p, property %p.\n", iface, debugstr_w(name), exists, property);
+
+    EnterCriticalSection(&group->cs);
+    shared_property = create_shared_property(group, name, exists);
+    LeaveCriticalSection(&group->cs);
+
+    if (!shared_property)
+        return E_OUTOFMEMORY;
+
+    *property = &shared_property->ISharedProperty_iface;
+
+    return S_OK;
 }
 
 static HRESULT WINAPI property_group_get_Property(ISharedPropertyGroup *iface, BSTR name, ISharedProperty **property)
@@ -339,12 +548,14 @@ struct property_group *create_property_group(struct group_manager* manager, BSTR
         property_group->isolation = *isolation;
         property_group->release = *release;
         property_group->refcount = 1;
+        list_init(&property_group->properties);
         property_group->name = SysAllocString(name);
         if (!property_group->name)
         {
             heap_free(property_group);
             return NULL;
         }
+        InitializeCriticalSection(&property_group->cs);
         list_add_tail(&manager->property_groups, &property_group->entry);
 
         *exists = FALSE;
diff --git a/dlls/comsvcs/tests/property.c b/dlls/comsvcs/tests/property.c
index 7321bbcfe63..1f62656e7ac 100644
--- a/dlls/comsvcs/tests/property.c
+++ b/dlls/comsvcs/tests/property.c
@@ -320,12 +320,93 @@ static void test_property_group(void)
     ISharedPropertyGroupManager_Release(manager);
 }
 
+static void test_shared_property(void)
+{
+    ISharedProperty *property, *property1;
+    ISharedPropertyGroupManager *manager;
+    ULONG refcount, expected_refcount;
+    ISharedPropertyGroup *group;
+    BSTR name, property_name;
+    LONG isolation, release;
+    VARIANT_BOOL exists;
+    IDispatch *dispatch;
+    HRESULT hr;
+    static const struct test_name_id test_name_ids[] =
+    {
+        {L"Value", DISPID_VALUE},
+    };
+
+    hr = CoCreateInstance(&CLSID_SharedPropertyGroupManager, NULL, CLSCTX_INPROC_SERVER,
+            &IID_ISharedPropertyGroupManager, (void **)&manager);
+    ok(hr == S_OK, "Got hr %#x.\n", hr);
+
+    name = SysAllocString(L"testgroupname");
+    isolation = 0;
+    release = 0;
+    exists = FALSE;
+    hr = ISharedPropertyGroupManager_CreatePropertyGroup(manager, name, &isolation, &release, &exists, &group);
+    ok(hr == S_OK, "Got hr %#x.\n", hr);
+    SysFreeString(name);
+
+    property_name = SysAllocString(L"testpropertyname");
+    exists = TRUE;
+    expected_refcount = get_refcount(group);
+    hr = ISharedPropertyGroup_CreateProperty(group, property_name, &exists, &property);
+    ok(hr == S_OK, "Got hr %#x.\n", hr);
+    ok(!exists, "Got unexpected value: %d.\n", exists);
+    refcount = get_refcount(group);
+    ok(refcount == expected_refcount, "Got refcount: %u, expected %u.\n", refcount, expected_refcount);
+
+    hr = ISharedPropertyGroup_get_PropertyByPosition(group, 0, &property1);
+    todo_wine ok(hr == E_INVALIDARG, "Got hr %#x.\n", hr);
+    hr = ISharedPropertyGroup_get_PropertyByPosition(group, 1, &property1);
+    todo_wine ok(hr == E_INVALIDARG, "Got hr %#x.\n", hr);
+
+    exists = FALSE;
+    expected_refcount = get_refcount(property) + 1;
+    hr = ISharedPropertyGroup_CreateProperty(group, property_name, &exists, &property1);
+    ok(hr == S_OK, "Got hr %#x.\n", hr);
+    ok(!!exists, "Got unexpected value: %d.\n", exists);
+    refcount = get_refcount(property);
+    ok(refcount == expected_refcount, "Got refcount: %u, expected %u.\n", refcount, expected_refcount);
+    expected_refcount--;
+    ISharedProperty_Release(property1);
+    refcount = get_refcount(property);
+    ok(refcount == expected_refcount, "Got refcount: %u, expected %u.\n", refcount, expected_refcount);
+
+    expected_refcount = get_refcount(property) + 1;
+    property1 = NULL;
+    hr = ISharedPropertyGroup_get_Property(group, property_name, &property1);
+    todo_wine ok(hr == S_OK, "Got hr %#x.\n", hr);
+    todo_wine ok(property1 == property, "Got wrong pointer %p.\n", property);
+    refcount = get_refcount(property);
+    todo_wine ok(refcount == expected_refcount, "Got refcount: %u, expected %u.\n", refcount, expected_refcount);
+    if (property1)
+        ISharedProperty_Release(property1);
+    SysFreeString(property_name);
+
+    property_name = SysAllocString(L"Testpropertyname");
+    hr = ISharedPropertyGroup_get_Property(group, property_name, &property1);
+    todo_wine ok(hr == E_INVALIDARG, "Got hr %#x.\n", hr);
+    SysFreeString(property_name);
+
+    hr = ISharedProperty_QueryInterface(property, &IID_IDispatch, (void **)&dispatch);
+    ok(hr == S_OK, "Got hr %#x.\n", hr);
+    TEST_TYPEINFO(dispatch, test_name_ids, ARRAY_SIZE(test_name_ids), &IID_ISharedProperty);
+    IDispatch_Release(dispatch);
+
+    ISharedProperty_Release(property);
+    ISharedPropertyGroup_Release(group);
+    ISharedPropertyGroupManager_Release(manager);
+}
+
 START_TEST(property)
 {
     CoInitialize(NULL);
 
     test_interfaces();
     test_property_group();
+    test_shared_property();
 
     CoUninitialize();
 }
-- 
2.28.0




More information about the wine-devel mailing list