--- /dev/null
+/*\r
+ * A stub manager is an object that controls interface stubs. It is\r
+ * identified by an OID (object identifier) and acts as the network\r
+ * identity of the object. There can be many stub managers in a\r
+ * process or apartment.\r
+ *\r
+ * Copyright 2002 Marcus Meissner\r
+ * Copyright 2004 Mike Hearn for CodeWeavers\r
+ * Copyright 2004 Robert Shearman (for CodeWeavers)\r
+ *\r
+ * This library is free software; you can redistribute it and/or\r
+ * modify it under the terms of the GNU Lesser General Public\r
+ * License as published by the Free Software Foundation; either\r
+ * version 2.1 of the License, or (at your option) any later version.\r
+ *\r
+ * This library is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
+ * Lesser General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU Lesser General Public\r
+ * License along with this library; if not, write to the Free Software\r
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\r
+ */\r
+\r
+#define COBJMACROS\r
+#define NONAMELESSUNION\r
+#define NONAMELESSSTRUCT\r
+\r
+#include <assert.h>\r
+#include <stdarg.h>\r
+\r
+#include "windef.h"\r
+#include "winbase.h"\r
+#include "winuser.h"\r
+#include "objbase.h"\r
+#include "ole2.h"\r
+#include "ole2ver.h"\r
+#include "rpc.h"\r
+#include "wine/debug.h"\r
+#include "compobj_private.h"\r
+\r
+WINE_DEFAULT_DEBUG_CHANNEL(ole);\r
+\r
+static void stub_manager_delete_ifstub(struct stub_manager *m, struct ifstub *ifstub);\r
+static struct ifstub *stub_manager_ipid_to_ifstub(struct stub_manager *m, const IPID *ipid);\r
+\r
+/* creates a new stub manager and adds it into the apartment. caller must\r
+ * release stub manager when it is no longer required. the apartment and\r
+ * external refs together take one implicit ref */\r
+struct stub_manager *new_stub_manager(APARTMENT *apt, IUnknown *object)\r
+{\r
+ struct stub_manager *sm;\r
+\r
+ assert( apt );\r
+ \r
+ sm = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct stub_manager));\r
+ if (!sm) return NULL;\r
+\r
+ list_init(&sm->ifstubs);\r
+ InitializeCriticalSection(&sm->lock);\r
+ IUnknown_AddRef(object);\r
+ sm->object = object;\r
+ sm->apt = apt;\r
+\r
+ /* start off with 2 references because the stub is in the apartment\r
+ * and the caller will also hold a reference */\r
+ sm->refs = 2;\r
+\r
+ /* yes, that's right, this starts at zero. that's zero EXTERNAL\r
+ * refs, ie nobody has unmarshalled anything yet. we can't have\r
+ * negative refs because the stub manager cannot be explicitly\r
+ * killed, it has to die by somebody unmarshalling then releasing\r
+ * the marshalled ifptr.\r
+ */\r
+ sm->extrefs = 0;\r
+ \r
+ EnterCriticalSection(&apt->cs);\r
+ sm->oid = apt->oidc++;\r
+ list_add_head(&apt->stubmgrs, &sm->entry);\r
+ LeaveCriticalSection(&apt->cs);\r
+\r
+ TRACE("Created new stub manager (oid=%s) at %p for object with IUnknown %p\n", wine_dbgstr_longlong(sm->oid), sm, object);\r
+ \r
+ return sm;\r
+}\r
+\r
+/* m->apt->cs must be held on entry to this function */\r
+static void stub_manager_delete(struct stub_manager *m)\r
+{\r
+ struct list *cursor;\r
+\r
+ TRACE("destroying %p (oid=%s)\n", m, wine_dbgstr_longlong(m->oid));\r
+\r
+ list_remove(&m->entry);\r
+\r
+ while ((cursor = list_head(&m->ifstubs)))\r
+ {\r
+ struct ifstub *ifstub = LIST_ENTRY(cursor, struct ifstub, entry);\r
+ stub_manager_delete_ifstub(m, ifstub);\r
+ }\r
+\r
+ IUnknown_Release(m->object);\r
+\r
+ DeleteCriticalSection(&m->lock);\r
+\r
+ HeapFree(GetProcessHeap(), 0, m);\r
+}\r
+\r
+/* gets the stub manager associated with an object - caller must call\r
+ * release on the stub manager when it is no longer needed */\r
+struct stub_manager *get_stub_manager_from_object(OXID oxid, void *object)\r
+{\r
+ struct stub_manager *result = NULL;\r
+ struct list *cursor;\r
+ APARTMENT *apt;\r
+\r
+ if (!(apt = COM_ApartmentFromOXID(oxid, TRUE)))\r
+ {\r
+ WARN("Could not map OXID %s to apartment object\n", wine_dbgstr_longlong(oxid));\r
+ return NULL;\r
+ }\r
+\r
+ EnterCriticalSection(&apt->cs);\r
+ LIST_FOR_EACH( cursor, &apt->stubmgrs )\r
+ {\r
+ struct stub_manager *m = LIST_ENTRY( cursor, struct stub_manager, entry );\r
+\r
+ if (m->object == object)\r
+ {\r
+ result = m;\r
+ stub_manager_int_addref(result);\r
+ break;\r
+ }\r
+ }\r
+ LeaveCriticalSection(&apt->cs);\r
+\r
+ COM_ApartmentRelease(apt);\r
+ \r
+ TRACE("found %p from object %p\n", result, object);\r
+\r
+ return result; \r
+}\r
+\r
+/* increments the internal refcount */\r
+ULONG stub_manager_int_addref(struct stub_manager *This)\r
+{\r
+ ULONG refs;\r
+\r
+ EnterCriticalSection(&This->apt->cs);\r
+ refs = ++This->refs;\r
+ LeaveCriticalSection(&This->apt->cs);\r
+\r
+ TRACE("before %ld\n", refs - 1);\r
+\r
+ return refs;\r
+}\r
+\r
+/* decrements the internal refcount */\r
+ULONG stub_manager_int_release(struct stub_manager *This)\r
+{\r
+ ULONG refs;\r
+ APARTMENT *apt = This->apt;\r
+\r
+ EnterCriticalSection(&apt->cs);\r
+ refs = --This->refs;\r
+\r
+ TRACE("after %ld\n", refs);\r
+\r
+ if (!refs)\r
+ stub_manager_delete(This);\r
+ LeaveCriticalSection(&apt->cs);\r
+\r
+ return refs;\r
+}\r
+\r
+/* gets the stub manager associated with an object id - caller must call\r
+ * release on the stub manager when it is no longer needed */\r
+struct stub_manager *get_stub_manager(OXID oxid, OID oid)\r
+{\r
+ struct stub_manager *result = NULL;\r
+ struct list *cursor;\r
+ APARTMENT *apt;\r
+\r
+ if (!(apt = COM_ApartmentFromOXID(oxid, TRUE)))\r
+ {\r
+ WARN("Could not map OXID %s to apartment object\n", wine_dbgstr_longlong(oxid));\r
+ return NULL;\r
+ }\r
+ \r
+ EnterCriticalSection(&apt->cs);\r
+ LIST_FOR_EACH( cursor, &apt->stubmgrs )\r
+ {\r
+ struct stub_manager *m = LIST_ENTRY( cursor, struct stub_manager, entry );\r
+\r
+ if (m->oid == oid)\r
+ {\r
+ result = m;\r
+ stub_manager_int_addref(result);\r
+ break;\r
+ }\r
+ }\r
+ LeaveCriticalSection(&apt->cs);\r
+\r
+ COM_ApartmentRelease(apt);\r
+\r
+ TRACE("found %p from oid %s\n", result, wine_dbgstr_longlong(oid));\r
+\r
+ return result;\r
+}\r
+\r
+/* add some external references (ie from a client that unmarshaled an ifptr) */\r
+ULONG stub_manager_ext_addref(struct stub_manager *m, ULONG refs)\r
+{\r
+ ULONG rc = InterlockedExchangeAdd(&m->extrefs, refs) + refs;\r
+\r
+ TRACE("added %lu refs to %p (oid %s), rc is now %lu\n", refs, m, wine_dbgstr_longlong(m->oid), rc);\r
+\r
+ return rc;\r
+}\r
+\r
+/* remove some external references */\r
+ULONG stub_manager_ext_release(struct stub_manager *m, ULONG refs)\r
+{\r
+ ULONG rc = InterlockedExchangeAdd(&m->extrefs, -refs) - refs;\r
+ \r
+ TRACE("removed %lu refs from %p (oid %s), rc is now %lu\n", refs, m, wine_dbgstr_longlong(m->oid), rc);\r
+\r
+ if (rc == 0)\r
+ stub_manager_int_release(m);\r
+\r
+ return rc;\r
+}\r
+\r
+static struct ifstub *stub_manager_ipid_to_ifstub(struct stub_manager *m, const IPID *ipid)\r
+{\r
+ struct list *cursor;\r
+ struct ifstub *result = NULL;\r
+ \r
+ EnterCriticalSection(&m->lock);\r
+ LIST_FOR_EACH( cursor, &m->ifstubs )\r
+ {\r
+ struct ifstub *ifstub = LIST_ENTRY( cursor, struct ifstub, entry );\r
+\r
+ if (IsEqualGUID(ipid, &ifstub->ipid))\r
+ {\r
+ result = ifstub;\r
+ break;\r
+ }\r
+ }\r
+ LeaveCriticalSection(&m->lock);\r
+\r
+ return result;\r
+}\r
+\r
+IRpcStubBuffer *stub_manager_ipid_to_stubbuffer(struct stub_manager *m, const IPID *ipid)\r
+{\r
+ struct ifstub *ifstub = stub_manager_ipid_to_ifstub(m, ipid);\r
+ \r
+ return ifstub ? ifstub->stubbuffer : NULL;\r
+}\r
+\r
+/* registers a new interface stub COM object with the stub manager and returns registration record */\r
+struct ifstub *stub_manager_new_ifstub(struct stub_manager *m, IRpcStubBuffer *sb, IUnknown *iptr, REFIID iid, BOOL tablemarshal)\r
+{\r
+ struct ifstub *stub;\r
+\r
+ TRACE("oid=%s, stubbuffer=%p, iptr=%p, iid=%s, tablemarshal=%s\n",\r
+ wine_dbgstr_longlong(m->oid), sb, iptr, debugstr_guid(iid), tablemarshal ? "TRUE" : "FALSE");\r
+\r
+ stub = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct ifstub));\r
+ if (!stub) return NULL;\r
+\r
+ stub->stubbuffer = sb;\r
+ IUnknown_AddRef(sb);\r
+\r
+ /* no need to ref this, same object as sb */\r
+ stub->iface = iptr;\r
+ stub->table = tablemarshal;\r
+ stub->iid = *iid;\r
+ stub->ipid = *iid; /* FIXME: should be globally unique */\r
+\r
+ EnterCriticalSection(&m->lock);\r
+ list_add_head(&m->ifstubs, &stub->entry);\r
+ LeaveCriticalSection(&m->lock);\r
+\r
+ return stub;\r
+}\r
+\r
+static void stub_manager_delete_ifstub(struct stub_manager *m, struct ifstub *ifstub)\r
+{\r
+ TRACE("m=%p, m->oid=%s, ipid=%s\n", m, wine_dbgstr_longlong(m->oid), debugstr_guid(&ifstub->ipid));\r
+ \r
+ list_remove(&ifstub->entry);\r
+ \r
+ IUnknown_Release(ifstub->stubbuffer);\r
+ IUnknown_Release(ifstub->iface);\r
+\r
+ HeapFree(GetProcessHeap(), 0, ifstub);\r
+}\r