2 * A stub manager is an object that controls interface stubs. It is
3 * identified by an OID (object identifier) and acts as the network
4 * identity of the object. There can be many stub managers in a
5 * process or apartment.
7 * Copyright 2002 Marcus Meissner
8 * Copyright 2004 Mike Hearn for CodeWeavers
9 * Copyright 2004 Robert Shearman (for CodeWeavers)
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
40 #include "wine/debug.h"
41 #include "compobj_private.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(ole);
45 static void stub_manager_delete_ifstub(struct stub_manager *m, struct ifstub *ifstub);
46 static struct ifstub *stub_manager_ipid_to_ifstub(struct stub_manager *m, const IPID *ipid);
48 /* creates a new stub manager and adds it into the apartment. caller must
49 * release stub manager when it is no longer required. the apartment and
50 * external refs together take one implicit ref */
51 struct stub_manager *new_stub_manager(APARTMENT *apt, IUnknown *object)
53 struct stub_manager *sm;
57 sm = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct stub_manager));
60 list_init(&sm->ifstubs);
61 InitializeCriticalSection(&sm->lock);
62 IUnknown_AddRef(object);
66 /* start off with 2 references because the stub is in the apartment
67 * and the caller will also hold a reference */
70 /* yes, that's right, this starts at zero. that's zero EXTERNAL
71 * refs, ie nobody has unmarshalled anything yet. we can't have
72 * negative refs because the stub manager cannot be explicitly
73 * killed, it has to die by somebody unmarshalling then releasing
74 * the marshalled ifptr.
78 EnterCriticalSection(&apt->cs);
79 sm->oid = apt->oidc++;
80 list_add_head(&apt->stubmgrs, &sm->entry);
81 LeaveCriticalSection(&apt->cs);
83 TRACE("Created new stub manager (oid=%s) at %p for object with IUnknown %p\n", wine_dbgstr_longlong(sm->oid), sm, object);
88 /* m->apt->cs must be held on entry to this function */
89 static void stub_manager_delete(struct stub_manager *m)
93 TRACE("destroying %p (oid=%s)\n", m, wine_dbgstr_longlong(m->oid));
95 list_remove(&m->entry);
97 /* release every ifstub */
98 while ((cursor = list_head(&m->ifstubs)))
100 struct ifstub *ifstub = LIST_ENTRY(cursor, struct ifstub, entry);
101 stub_manager_delete_ifstub(m, ifstub);
104 IUnknown_Release(m->object);
106 DeleteCriticalSection(&m->lock);
108 HeapFree(GetProcessHeap(), 0, m);
111 /* gets the stub manager associated with an object - caller must have
112 * a reference to the apartment while a reference to the stub manager is held.
113 * it must also call release on the stub manager when it is no longer needed */
114 struct stub_manager *get_stub_manager_from_object(APARTMENT *apt, void *object)
116 struct stub_manager *result = NULL;
119 EnterCriticalSection(&apt->cs);
120 LIST_FOR_EACH( cursor, &apt->stubmgrs )
122 struct stub_manager *m = LIST_ENTRY( cursor, struct stub_manager, entry );
124 if (m->object == object)
127 stub_manager_int_addref(result);
131 LeaveCriticalSection(&apt->cs);
134 TRACE("found %p for object %p\n", result, object);
136 TRACE("not found for object %p\n", object);
141 /* gets the stub manager associated with an object id - caller must have
142 * a reference to the apartment while a reference to the stub manager is held.
143 * it must also call release on the stub manager when it is no longer needed */
144 struct stub_manager *get_stub_manager(APARTMENT *apt, OID oid)
146 struct stub_manager *result = NULL;
149 EnterCriticalSection(&apt->cs);
150 LIST_FOR_EACH( cursor, &apt->stubmgrs )
152 struct stub_manager *m = LIST_ENTRY( cursor, struct stub_manager, entry );
157 stub_manager_int_addref(result);
161 LeaveCriticalSection(&apt->cs);
164 TRACE("found %p for oid %s\n", result, wine_dbgstr_longlong(oid));
166 TRACE("not found for oid %s\n", wine_dbgstr_longlong(oid));
171 /* increments the internal refcount */
172 ULONG stub_manager_int_addref(struct stub_manager *This)
176 EnterCriticalSection(&This->apt->cs);
178 LeaveCriticalSection(&This->apt->cs);
180 TRACE("before %ld\n", refs - 1);
185 /* decrements the internal refcount */
186 ULONG stub_manager_int_release(struct stub_manager *This)
189 APARTMENT *apt = This->apt;
191 EnterCriticalSection(&apt->cs);
194 TRACE("after %ld\n", refs);
197 stub_manager_delete(This);
198 LeaveCriticalSection(&apt->cs);
203 /* add some external references (ie from a client that unmarshaled an ifptr) */
204 ULONG stub_manager_ext_addref(struct stub_manager *m, ULONG refs)
206 ULONG rc = InterlockedExchangeAdd(&m->extrefs, refs) + refs;
208 TRACE("added %lu refs to %p (oid %s), rc is now %lu\n", refs, m, wine_dbgstr_longlong(m->oid), rc);
213 /* remove some external references */
214 ULONG stub_manager_ext_release(struct stub_manager *m, ULONG refs)
216 ULONG rc = InterlockedExchangeAdd(&m->extrefs, -refs) - refs;
218 TRACE("removed %lu refs from %p (oid %s), rc is now %lu\n", refs, m, wine_dbgstr_longlong(m->oid), rc);
221 stub_manager_int_release(m);
226 static struct ifstub *stub_manager_ipid_to_ifstub(struct stub_manager *m, const IPID *ipid)
229 struct ifstub *result = NULL;
231 EnterCriticalSection(&m->lock);
232 LIST_FOR_EACH( cursor, &m->ifstubs )
234 struct ifstub *ifstub = LIST_ENTRY( cursor, struct ifstub, entry );
236 if (IsEqualGUID(ipid, &ifstub->ipid))
242 LeaveCriticalSection(&m->lock);
247 /* gets the stub manager associated with an ipid - caller must have
248 * a reference to the apartment while a reference to the stub manager is held.
249 * it must also call release on the stub manager when it is no longer needed */
250 static struct stub_manager *get_stub_manager_from_ipid(APARTMENT *apt, const IPID *ipid)
252 struct stub_manager *result = NULL;
255 EnterCriticalSection(&apt->cs);
256 LIST_FOR_EACH( cursor, &apt->stubmgrs )
258 struct stub_manager *m = LIST_ENTRY( cursor, struct stub_manager, entry );
260 if (stub_manager_ipid_to_ifstub(m, ipid))
263 stub_manager_int_addref(result);
267 LeaveCriticalSection(&apt->cs);
270 TRACE("found %p for ipid %s\n", result, debugstr_guid(ipid));
272 ERR("not found for ipid %s\n", debugstr_guid(ipid));
277 HRESULT ipid_to_stub_manager(const IPID *ipid, APARTMENT **stub_apt, struct stub_manager **stubmgr_ret)
279 /* FIXME: hack for IRemUnknown */
280 if (ipid->Data2 == 0xffff)
281 *stub_apt = COM_ApartmentFromOXID(*(OXID *)ipid->Data4, TRUE);
283 *stub_apt = COM_ApartmentFromTID(ipid->Data2);
286 ERR("Couldn't find apartment corresponding to TID 0x%04x\n", ipid->Data2);
287 return RPC_E_INVALID_OBJECT;
289 *stubmgr_ret = get_stub_manager_from_ipid(*stub_apt, ipid);
292 COM_ApartmentRelease(*stub_apt);
294 return RPC_E_INVALID_OBJECT;
299 IRpcStubBuffer *ipid_to_stubbuffer(const IPID *ipid)
301 IRpcStubBuffer *ret = NULL;
303 struct stub_manager *stubmgr;
304 struct ifstub *ifstub;
307 hr = ipid_to_stub_manager(ipid, &apt, &stubmgr);
308 if (hr != S_OK) return NULL;
310 ifstub = stub_manager_ipid_to_ifstub(stubmgr, ipid);
312 ret = ifstub->stubbuffer;
314 stub_manager_int_release(stubmgr);
316 COM_ApartmentRelease(apt);
321 /* generates an ipid in the following format (similar to native version):
322 * Data1 = apartment-local ipid counter
323 * Data2 = apartment creator thread ID
325 * Data4 = random value
327 static inline HRESULT generate_ipid(struct stub_manager *m, IPID *ipid)
330 hr = UuidCreate(ipid);
333 ERR("couldn't create IPID for stub manager %p\n", m);
338 ipid->Data1 = InterlockedIncrement(&m->apt->ipidc);
339 ipid->Data2 = (USHORT)m->apt->tid;
340 ipid->Data3 = (USHORT)GetCurrentProcessId();
344 /* registers a new interface stub COM object with the stub manager and returns registration record */
345 struct ifstub *stub_manager_new_ifstub(struct stub_manager *m, IRpcStubBuffer *sb, IUnknown *iptr, REFIID iid, BOOL tablemarshal)
349 TRACE("oid=%s, stubbuffer=%p, iptr=%p, iid=%s, tablemarshal=%s\n",
350 wine_dbgstr_longlong(m->oid), sb, iptr, debugstr_guid(iid), tablemarshal ? "TRUE" : "FALSE");
352 stub = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct ifstub));
353 if (!stub) return NULL;
355 stub->stubbuffer = sb;
358 /* no need to ref this, same object as sb */
362 stub->state = IFSTUB_STATE_TABLE_MARSHALED;
364 stub->state = IFSTUB_STATE_NORMAL_MARSHALED;
368 /* FIXME: hack for IRemUnknown because we don't notify SCM of our IPID
369 * yet, so we need to use a well-known one */
370 if (IsEqualIID(iid, &IID_IRemUnknown))
372 stub->ipid.Data1 = 0xffffffff;
373 stub->ipid.Data2 = 0xffff;
374 stub->ipid.Data3 = 0xffff;
375 assert(sizeof(stub->ipid.Data4) == sizeof(m->apt->oxid));
376 memcpy(&stub->ipid.Data4, &m->apt->oxid, sizeof(OXID));
379 generate_ipid(m, &stub->ipid);
381 EnterCriticalSection(&m->lock);
382 list_add_head(&m->ifstubs, &stub->entry);
383 LeaveCriticalSection(&m->lock);
385 TRACE("ifstub %p created with ipid %s\n", stub, debugstr_guid(&stub->ipid));
390 static void stub_manager_delete_ifstub(struct stub_manager *m, struct ifstub *ifstub)
392 TRACE("m=%p, m->oid=%s, ipid=%s\n", m, wine_dbgstr_longlong(m->oid), debugstr_guid(&ifstub->ipid));
394 list_remove(&ifstub->entry);
396 IUnknown_Release(ifstub->stubbuffer);
397 IUnknown_Release(ifstub->iface);
399 HeapFree(GetProcessHeap(), 0, ifstub);
402 /* returns TRUE if it is possible to unmarshal, FALSE otherwise. */
403 BOOL stub_manager_notify_unmarshal(struct stub_manager *m, const IPID *ipid)
405 struct ifstub *ifstub;
408 ifstub = stub_manager_ipid_to_ifstub(m, ipid);
411 WARN("Can't find ifstub for OID %s, IPID %s\n",
412 wine_dbgstr_longlong(m->oid), wine_dbgstr_guid(ipid));
416 EnterCriticalSection(&m->lock);
418 switch (ifstub->state)
420 case IFSTUB_STATE_TABLE_MARSHALED:
423 case IFSTUB_STATE_NORMAL_MARSHALED:
424 ifstub->state = IFSTUB_STATE_NORMAL_UNMARSHALED;
428 WARN("object OID %s, IPID %s already unmarshaled\n",
429 wine_dbgstr_longlong(m->oid), wine_dbgstr_guid(ipid));
434 LeaveCriticalSection(&m->lock);
439 /* is an ifstub table marshaled? */
440 BOOL stub_manager_is_table_marshaled(struct stub_manager *m, const IPID *ipid)
442 struct ifstub *ifstub;
445 ifstub = stub_manager_ipid_to_ifstub(m, ipid);
448 WARN("Can't find ifstub for OID %s, IPID %s\n",
449 wine_dbgstr_longlong(m->oid), wine_dbgstr_guid(ipid));
453 EnterCriticalSection(&m->lock);
454 ret = (ifstub->state == IFSTUB_STATE_TABLE_MARSHALED);
455 LeaveCriticalSection(&m->lock);
461 /*****************************************************************************
463 * IRemUnknown implementation
466 * Note: this object is not related to the lifetime of a stub_manager, but it
467 * interacts with stub managers.
470 const IID IID_IRemUnknown = { 0x00000131, 0, 0, {0xc0, 0, 0, 0, 0, 0, 0, 0x46} };
472 typedef struct rem_unknown
474 const IRemUnknownVtbl *lpVtbl;
478 static const IRemUnknownVtbl RemUnknown_Vtbl;
481 /* construct an IRemUnknown object with one outstanding reference */
482 static HRESULT RemUnknown_Construct(IRemUnknown **ppRemUnknown)
484 RemUnknown *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
486 if (!This) return E_OUTOFMEMORY;
488 This->lpVtbl = &RemUnknown_Vtbl;
491 *ppRemUnknown = (IRemUnknown *)This;
495 static HRESULT WINAPI RemUnknown_QueryInterface(IRemUnknown *iface, REFIID riid, void **ppv)
497 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
499 if (IsEqualIID(riid, &IID_IUnknown) ||
500 IsEqualIID(riid, &IID_IRemUnknown))
502 *ppv = (LPVOID)iface;
503 IRemUnknown_AddRef(iface);
507 FIXME("No interface for iid %s\n", debugstr_guid(riid));
510 return E_NOINTERFACE;
513 static ULONG WINAPI RemUnknown_AddRef(IRemUnknown *iface)
516 RemUnknown *This = (RemUnknown *)iface;
518 refs = InterlockedIncrement(&This->refs);
520 TRACE("%p before: %ld\n", iface, refs-1);
524 static ULONG WINAPI RemUnknown_Release(IRemUnknown *iface)
527 RemUnknown *This = (RemUnknown *)iface;
529 refs = InterlockedDecrement(&This->refs);
531 HeapFree(GetProcessHeap(), 0, This);
533 TRACE("%p after: %ld\n", iface, refs);
537 static HRESULT WINAPI RemUnknown_RemQueryInterface(IRemUnknown *iface,
538 REFIPID ripid, ULONG cRefs, USHORT cIids, IID *iids /* [size_is(cIids)] */,
539 REMQIRESULT **ppQIResults /* [size_is(,cIids)] */)
543 USHORT successful_qis = 0;
545 struct stub_manager *stubmgr;
547 TRACE("(%p)->(%s, %ld, %d, %p, %p)\n", iface, debugstr_guid(ripid), cRefs, cIids, iids, ppQIResults);
549 hr = ipid_to_stub_manager(ripid, &apt, &stubmgr);
550 if (hr != S_OK) return hr;
552 *ppQIResults = CoTaskMemAlloc(sizeof(REMQIRESULT) * cIids);
554 for (i = 0; i < cIids; i++)
556 HRESULT hrobj = register_ifstub(apt, &(*ppQIResults)[i].std, &iids[i],
557 stubmgr->object, MSHLFLAGS_NORMAL);
560 (*ppQIResults)[i].hResult = hrobj;
563 stub_manager_int_release(stubmgr);
564 COM_ApartmentRelease(apt);
566 if (successful_qis == cIids)
567 return S_OK; /* we got all requested interfaces */
568 else if (successful_qis == 0)
569 return E_NOINTERFACE; /* we didn't get any interfaces */
571 return S_FALSE; /* we got some interfaces */
574 static HRESULT WINAPI RemUnknown_RemAddRef(IRemUnknown *iface,
575 USHORT cInterfaceRefs,
576 REMINTERFACEREF* InterfaceRefs /* [size_is(cInterfaceRefs)] */,
577 HRESULT *pResults /* [size_is(cInterfaceRefs)] */)
582 TRACE("(%p)->(%d, %p, %p)\n", iface, cInterfaceRefs, InterfaceRefs, pResults);
584 for (i = 0; i < cInterfaceRefs; i++)
587 struct stub_manager *stubmgr;
589 pResults[i] = ipid_to_stub_manager(&InterfaceRefs[i].ipid, &apt, &stubmgr);
590 if (pResults[i] != S_OK)
596 stub_manager_ext_addref(stubmgr, InterfaceRefs[i].cPublicRefs);
597 if (InterfaceRefs[i].cPrivateRefs)
598 FIXME("Adding %ld refs securely not implemented\n", InterfaceRefs[i].cPrivateRefs);
600 stub_manager_int_release(stubmgr);
601 COM_ApartmentRelease(apt);
607 static HRESULT WINAPI RemUnknown_RemRelease(IRemUnknown *iface,
608 USHORT cInterfaceRefs,
609 REMINTERFACEREF* InterfaceRefs /* [size_is(cInterfaceRefs)] */)
614 TRACE("(%p)->(%d, %p)\n", iface, cInterfaceRefs, InterfaceRefs);
616 for (i = 0; i < cInterfaceRefs; i++)
619 struct stub_manager *stubmgr;
621 hr = ipid_to_stub_manager(&InterfaceRefs[i].ipid, &apt, &stubmgr);
625 /* FIXME: we should undo any changes already made in this function */
629 stub_manager_ext_release(stubmgr, InterfaceRefs[i].cPublicRefs);
630 if (InterfaceRefs[i].cPrivateRefs)
631 FIXME("Releasing %ld refs securely not implemented\n", InterfaceRefs[i].cPrivateRefs);
633 stub_manager_int_release(stubmgr);
634 COM_ApartmentRelease(apt);
640 static const IRemUnknownVtbl RemUnknown_Vtbl =
642 RemUnknown_QueryInterface,
645 RemUnknown_RemQueryInterface,
646 RemUnknown_RemAddRef,
647 RemUnknown_RemRelease
650 /* starts the IRemUnknown listener for the current apartment */
651 HRESULT start_apartment_remote_unknown()
653 IRemUnknown *pRemUnknown;
655 APARTMENT *apt = COM_CurrentApt();
657 EnterCriticalSection(&apt->cs);
658 if (!apt->remunk_exported)
660 /* create the IRemUnknown object */
661 hr = RemUnknown_Construct(&pRemUnknown);
664 STDOBJREF stdobjref; /* dummy - not used */
665 /* register it with the stub manager */
666 hr = register_ifstub(COM_CurrentApt(), &stdobjref, &IID_IRemUnknown, (IUnknown *)pRemUnknown, MSHLFLAGS_NORMAL);
667 /* release our reference to the object as the stub manager will manage the life cycle for us */
668 IRemUnknown_Release(pRemUnknown);
670 apt->remunk_exported = TRUE;
673 LeaveCriticalSection(&apt->cs);