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
41 #include "wine/debug.h"
42 #include "compobj_private.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(ole);
46 static void stub_manager_delete_ifstub(struct stub_manager *m, struct ifstub *ifstub);
47 static struct ifstub *stub_manager_ipid_to_ifstub(struct stub_manager *m, const IPID *ipid);
49 /* creates a new stub manager and adds it into the apartment. caller must
50 * release stub manager when it is no longer required. the apartment and
51 * external refs together take one implicit ref */
52 struct stub_manager *new_stub_manager(APARTMENT *apt, IUnknown *object, MSHLFLAGS mshlflags)
54 struct stub_manager *sm;
58 sm = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct stub_manager));
61 list_init(&sm->ifstubs);
63 InitializeCriticalSection(&sm->lock);
64 DEBUG_SET_CRITSEC_NAME(&sm->lock, "stub_manager");
66 IUnknown_AddRef(object);
70 /* start off with 2 references because the stub is in the apartment
71 * and the caller will also hold a reference */
74 /* yes, that's right, this starts at zero. that's zero EXTERNAL
75 * refs, ie nobody has unmarshalled anything yet. we can't have
76 * negative refs because the stub manager cannot be explicitly
77 * killed, it has to die by somebody unmarshalling then releasing
78 * the marshalled ifptr.
82 if (mshlflags & MSHLFLAGS_TABLESTRONG)
83 sm->state = STUBSTATE_TABLE_STRONG;
84 else if (mshlflags & MSHLFLAGS_TABLEWEAK)
85 sm->state = STUBSTATE_TABLE_WEAK_UNMARSHALED;
87 sm->state = STUBSTATE_NORMAL_MARSHALED;
89 EnterCriticalSection(&apt->cs);
90 sm->oid = apt->oidc++;
91 list_add_head(&apt->stubmgrs, &sm->entry);
92 LeaveCriticalSection(&apt->cs);
94 TRACE("Created new stub manager (oid=%s) at %p for object with IUnknown %p\n", wine_dbgstr_longlong(sm->oid), sm, object);
99 /* caller must remove stub manager from apartment prior to calling this function */
100 static void stub_manager_delete(struct stub_manager *m)
104 TRACE("destroying %p (oid=%s)\n", m, wine_dbgstr_longlong(m->oid));
106 /* release every ifstub */
107 while ((cursor = list_head(&m->ifstubs)))
109 struct ifstub *ifstub = LIST_ENTRY(cursor, struct ifstub, entry);
110 stub_manager_delete_ifstub(m, ifstub);
113 IUnknown_Release(m->object);
115 DEBUG_CLEAR_CRITSEC_NAME(&m->lock);
116 DeleteCriticalSection(&m->lock);
118 HeapFree(GetProcessHeap(), 0, m);
121 /* gets the stub manager associated with an object - caller must have
122 * a reference to the apartment while a reference to the stub manager is held.
123 * it must also call release on the stub manager when it is no longer needed */
124 struct stub_manager *get_stub_manager_from_object(APARTMENT *apt, void *object)
126 struct stub_manager *result = NULL;
129 EnterCriticalSection(&apt->cs);
130 LIST_FOR_EACH( cursor, &apt->stubmgrs )
132 struct stub_manager *m = LIST_ENTRY( cursor, struct stub_manager, entry );
134 if (m->object == object)
137 stub_manager_int_addref(result);
141 LeaveCriticalSection(&apt->cs);
144 TRACE("found %p for object %p\n", result, object);
146 TRACE("not found for object %p\n", object);
151 /* removes the apartment reference to an object, destroying it when no other
152 * threads have a reference to it */
153 void apartment_disconnectobject(struct apartment *apt, void *object)
156 struct stub_manager *stubmgr;
158 EnterCriticalSection(&apt->cs);
159 LIST_FOR_EACH_ENTRY( stubmgr, &apt->stubmgrs, struct stub_manager, entry )
161 if (stubmgr->object == object)
164 stub_manager_int_release(stubmgr);
168 LeaveCriticalSection(&apt->cs);
171 TRACE("disconnect object %p\n", object);
173 WARN("couldn't find object %p\n", object);
176 /* gets the stub manager associated with an object id - caller must have
177 * a reference to the apartment while a reference to the stub manager is held.
178 * it must also call release on the stub manager when it is no longer needed */
179 struct stub_manager *get_stub_manager(APARTMENT *apt, OID oid)
181 struct stub_manager *result = NULL;
184 EnterCriticalSection(&apt->cs);
185 LIST_FOR_EACH( cursor, &apt->stubmgrs )
187 struct stub_manager *m = LIST_ENTRY( cursor, struct stub_manager, entry );
192 stub_manager_int_addref(result);
196 LeaveCriticalSection(&apt->cs);
199 TRACE("found %p for oid %s\n", result, wine_dbgstr_longlong(oid));
201 TRACE("not found for oid %s\n", wine_dbgstr_longlong(oid));
206 /* increments the internal refcount */
207 ULONG stub_manager_int_addref(struct stub_manager *This)
211 EnterCriticalSection(&This->apt->cs);
213 LeaveCriticalSection(&This->apt->cs);
215 TRACE("before %ld\n", refs - 1);
220 /* decrements the internal refcount */
221 ULONG stub_manager_int_release(struct stub_manager *This)
224 APARTMENT *apt = This->apt;
226 EnterCriticalSection(&apt->cs);
229 TRACE("after %ld\n", refs);
231 /* remove from apartment so no other thread can access it... */
233 list_remove(&This->entry);
235 LeaveCriticalSection(&apt->cs);
237 /* ... so now we can delete it without being inside the apartment critsec */
239 stub_manager_delete(This);
244 /* add some external references (ie from a client that unmarshaled an ifptr) */
245 ULONG stub_manager_ext_addref(struct stub_manager *m, ULONG refs)
249 EnterCriticalSection(&m->lock);
251 /* make sure we don't overflow extrefs */
252 refs = min(refs, (ULONG_MAX-1 - m->extrefs));
253 rc = (m->extrefs += refs);
255 LeaveCriticalSection(&m->lock);
257 TRACE("added %lu refs to %p (oid %s), rc is now %lu\n", refs, m, wine_dbgstr_longlong(m->oid), rc);
262 /* remove some external references */
263 ULONG stub_manager_ext_release(struct stub_manager *m, ULONG refs)
267 EnterCriticalSection(&m->lock);
269 /* make sure we don't underflow extrefs */
270 refs = min(refs, m->extrefs);
271 rc = (m->extrefs -= refs);
273 LeaveCriticalSection(&m->lock);
275 TRACE("removed %lu refs from %p (oid %s), rc is now %lu\n", refs, m, wine_dbgstr_longlong(m->oid), rc);
278 stub_manager_int_release(m);
283 static struct ifstub *stub_manager_ipid_to_ifstub(struct stub_manager *m, const IPID *ipid)
286 struct ifstub *result = NULL;
288 EnterCriticalSection(&m->lock);
289 LIST_FOR_EACH( cursor, &m->ifstubs )
291 struct ifstub *ifstub = LIST_ENTRY( cursor, struct ifstub, entry );
293 if (IsEqualGUID(ipid, &ifstub->ipid))
299 LeaveCriticalSection(&m->lock);
304 /* gets the stub manager associated with an ipid - caller must have
305 * a reference to the apartment while a reference to the stub manager is held.
306 * it must also call release on the stub manager when it is no longer needed */
307 static struct stub_manager *get_stub_manager_from_ipid(APARTMENT *apt, const IPID *ipid)
309 struct stub_manager *result = NULL;
312 EnterCriticalSection(&apt->cs);
313 LIST_FOR_EACH( cursor, &apt->stubmgrs )
315 struct stub_manager *m = LIST_ENTRY( cursor, struct stub_manager, entry );
317 if (stub_manager_ipid_to_ifstub(m, ipid))
320 stub_manager_int_addref(result);
324 LeaveCriticalSection(&apt->cs);
327 TRACE("found %p for ipid %s\n", result, debugstr_guid(ipid));
329 ERR("not found for ipid %s\n", debugstr_guid(ipid));
334 HRESULT ipid_to_stub_manager(const IPID *ipid, APARTMENT **stub_apt, struct stub_manager **stubmgr_ret)
336 /* FIXME: hack for IRemUnknown */
337 if (ipid->Data2 == 0xffff)
338 *stub_apt = apartment_findfromoxid(*(OXID *)ipid->Data4, TRUE);
340 *stub_apt = apartment_findfromtid(ipid->Data2);
343 TRACE("Couldn't find apartment corresponding to TID 0x%04x\n", ipid->Data2);
344 return RPC_E_INVALID_OBJECT;
346 *stubmgr_ret = get_stub_manager_from_ipid(*stub_apt, ipid);
349 apartment_release(*stub_apt);
351 return RPC_E_INVALID_OBJECT;
356 /* gets the apartment and IRpcStubBuffer from an object. the caller must
357 * release the references to both objects */
358 IRpcStubBuffer *ipid_to_apt_and_stubbuffer(const IPID *ipid, APARTMENT **stub_apt)
360 IRpcStubBuffer *ret = NULL;
361 struct stub_manager *stubmgr;
362 struct ifstub *ifstub;
367 hr = ipid_to_stub_manager(ipid, stub_apt, &stubmgr);
368 if (hr != S_OK) return NULL;
370 ifstub = stub_manager_ipid_to_ifstub(stubmgr, ipid);
372 ret = ifstub->stubbuffer;
374 if (ret) IRpcStubBuffer_AddRef(ret);
376 stub_manager_int_release(stubmgr);
381 /* generates an ipid in the following format (similar to native version):
382 * Data1 = apartment-local ipid counter
383 * Data2 = apartment creator thread ID
385 * Data4 = random value
387 static inline HRESULT generate_ipid(struct stub_manager *m, IPID *ipid)
390 hr = UuidCreate(ipid);
393 ERR("couldn't create IPID for stub manager %p\n", m);
398 ipid->Data1 = InterlockedIncrement(&m->apt->ipidc);
399 ipid->Data2 = (USHORT)m->apt->tid;
400 ipid->Data3 = (USHORT)GetCurrentProcessId();
404 /* registers a new interface stub COM object with the stub manager and returns registration record */
405 struct ifstub *stub_manager_new_ifstub(struct stub_manager *m, IRpcStubBuffer *sb, IUnknown *iptr, REFIID iid)
409 TRACE("oid=%s, stubbuffer=%p, iptr=%p, iid=%s\n",
410 wine_dbgstr_longlong(m->oid), sb, iptr, debugstr_guid(iid));
412 stub = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct ifstub));
413 if (!stub) return NULL;
415 stub->stubbuffer = sb;
416 if (sb) IRpcStubBuffer_AddRef(sb);
418 IUnknown_AddRef(iptr);
423 /* FIXME: hack for IRemUnknown because we don't notify SCM of our IPID
424 * yet, so we need to use a well-known one */
425 if (IsEqualIID(iid, &IID_IRemUnknown))
427 stub->ipid.Data1 = 0xffffffff;
428 stub->ipid.Data2 = 0xffff;
429 stub->ipid.Data3 = 0xffff;
430 assert(sizeof(stub->ipid.Data4) == sizeof(m->apt->oxid));
431 memcpy(&stub->ipid.Data4, &m->apt->oxid, sizeof(OXID));
434 generate_ipid(m, &stub->ipid);
436 EnterCriticalSection(&m->lock);
437 list_add_head(&m->ifstubs, &stub->entry);
438 LeaveCriticalSection(&m->lock);
440 TRACE("ifstub %p created with ipid %s\n", stub, debugstr_guid(&stub->ipid));
445 static void stub_manager_delete_ifstub(struct stub_manager *m, struct ifstub *ifstub)
447 TRACE("m=%p, m->oid=%s, ipid=%s\n", m, wine_dbgstr_longlong(m->oid), debugstr_guid(&ifstub->ipid));
449 list_remove(&ifstub->entry);
451 RPC_UnregisterInterface(&ifstub->iid);
453 if (ifstub->stubbuffer) IUnknown_Release(ifstub->stubbuffer);
454 IUnknown_Release(ifstub->iface);
456 HeapFree(GetProcessHeap(), 0, ifstub);
459 /* returns TRUE if it is possible to unmarshal, FALSE otherwise. */
460 BOOL stub_manager_notify_unmarshal(struct stub_manager *m)
464 EnterCriticalSection(&m->lock);
468 case STUBSTATE_TABLE_STRONG:
469 case STUBSTATE_TABLE_WEAK_MARSHALED:
473 case STUBSTATE_TABLE_WEAK_UNMARSHALED:
474 m->state = STUBSTATE_TABLE_WEAK_MARSHALED;
477 case STUBSTATE_NORMAL_MARSHALED:
478 m->state = STUBSTATE_NORMAL_UNMARSHALED;
482 WARN("object OID %s already unmarshaled\n",
483 wine_dbgstr_longlong(m->oid));
484 ret = TRUE; /* FIXME: the state management should be per-ifstub, so
485 * it is disabled at the moment so that InstallShield
490 LeaveCriticalSection(&m->lock);
495 void stub_manager_release_marshal_data(struct stub_manager *m, ULONG refs)
497 EnterCriticalSection(&m->lock);
501 case STUBSTATE_NORMAL_MARSHALED:
502 case STUBSTATE_NORMAL_UNMARSHALED: /* FIXME: check this */
503 /* nothing to change */
505 case STUBSTATE_TABLE_WEAK_UNMARSHALED:
506 case STUBSTATE_TABLE_STRONG:
509 case STUBSTATE_TABLE_WEAK_MARSHALED:
510 refs = 0; /* like native */
514 LeaveCriticalSection(&m->lock);
516 stub_manager_ext_release(m, refs);
519 /* is an ifstub table marshaled? */
520 BOOL stub_manager_is_table_marshaled(struct stub_manager *m)
524 EnterCriticalSection(&m->lock);
525 ret = ((m->state == STUBSTATE_TABLE_STRONG) ||
526 (m->state == STUBSTATE_TABLE_WEAK_MARSHALED) ||
527 (m->state == STUBSTATE_TABLE_WEAK_UNMARSHALED));
528 LeaveCriticalSection(&m->lock);
534 /*****************************************************************************
536 * IRemUnknown implementation
539 * Note: this object is not related to the lifetime of a stub_manager, but it
540 * interacts with stub managers.
543 const IID IID_IRemUnknown = { 0x00000131, 0, 0, {0xc0, 0, 0, 0, 0, 0, 0, 0x46} };
545 typedef struct rem_unknown
547 const IRemUnknownVtbl *lpVtbl;
551 static const IRemUnknownVtbl RemUnknown_Vtbl;
554 /* construct an IRemUnknown object with one outstanding reference */
555 static HRESULT RemUnknown_Construct(IRemUnknown **ppRemUnknown)
557 RemUnknown *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
559 if (!This) return E_OUTOFMEMORY;
561 This->lpVtbl = &RemUnknown_Vtbl;
564 *ppRemUnknown = (IRemUnknown *)This;
568 static HRESULT WINAPI RemUnknown_QueryInterface(IRemUnknown *iface, REFIID riid, void **ppv)
570 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
572 if (IsEqualIID(riid, &IID_IUnknown) ||
573 IsEqualIID(riid, &IID_IRemUnknown))
575 *ppv = (LPVOID)iface;
576 IRemUnknown_AddRef(iface);
580 FIXME("No interface for iid %s\n", debugstr_guid(riid));
583 return E_NOINTERFACE;
586 static ULONG WINAPI RemUnknown_AddRef(IRemUnknown *iface)
589 RemUnknown *This = (RemUnknown *)iface;
591 refs = InterlockedIncrement(&This->refs);
593 TRACE("%p before: %ld\n", iface, refs-1);
597 static ULONG WINAPI RemUnknown_Release(IRemUnknown *iface)
600 RemUnknown *This = (RemUnknown *)iface;
602 refs = InterlockedDecrement(&This->refs);
604 HeapFree(GetProcessHeap(), 0, This);
606 TRACE("%p after: %ld\n", iface, refs);
610 static HRESULT WINAPI RemUnknown_RemQueryInterface(IRemUnknown *iface,
611 REFIPID ripid, ULONG cRefs, USHORT cIids, IID *iids /* [size_is(cIids)] */,
612 REMQIRESULT **ppQIResults /* [size_is(,cIids)] */)
616 USHORT successful_qis = 0;
618 struct stub_manager *stubmgr;
620 TRACE("(%p)->(%s, %ld, %d, %p, %p)\n", iface, debugstr_guid(ripid), cRefs, cIids, iids, ppQIResults);
622 hr = ipid_to_stub_manager(ripid, &apt, &stubmgr);
623 if (hr != S_OK) return hr;
625 *ppQIResults = CoTaskMemAlloc(sizeof(REMQIRESULT) * cIids);
627 for (i = 0; i < cIids; i++)
629 HRESULT hrobj = marshal_object(apt, &(*ppQIResults)[i].std, &iids[i],
630 stubmgr->object, MSHLFLAGS_NORMAL);
633 (*ppQIResults)[i].hResult = hrobj;
636 stub_manager_int_release(stubmgr);
637 apartment_release(apt);
639 if (successful_qis == cIids)
640 return S_OK; /* we got all requested interfaces */
641 else if (successful_qis == 0)
642 return E_NOINTERFACE; /* we didn't get any interfaces */
644 return S_FALSE; /* we got some interfaces */
647 static HRESULT WINAPI RemUnknown_RemAddRef(IRemUnknown *iface,
648 USHORT cInterfaceRefs,
649 REMINTERFACEREF* InterfaceRefs /* [size_is(cInterfaceRefs)] */,
650 HRESULT *pResults /* [size_is(cInterfaceRefs)] */)
655 TRACE("(%p)->(%d, %p, %p)\n", iface, cInterfaceRefs, InterfaceRefs, pResults);
657 for (i = 0; i < cInterfaceRefs; i++)
660 struct stub_manager *stubmgr;
662 pResults[i] = ipid_to_stub_manager(&InterfaceRefs[i].ipid, &apt, &stubmgr);
663 if (pResults[i] != S_OK)
669 stub_manager_ext_addref(stubmgr, InterfaceRefs[i].cPublicRefs);
670 if (InterfaceRefs[i].cPrivateRefs)
671 FIXME("Adding %ld refs securely not implemented\n", InterfaceRefs[i].cPrivateRefs);
673 stub_manager_int_release(stubmgr);
674 apartment_release(apt);
680 static HRESULT WINAPI RemUnknown_RemRelease(IRemUnknown *iface,
681 USHORT cInterfaceRefs,
682 REMINTERFACEREF* InterfaceRefs /* [size_is(cInterfaceRefs)] */)
687 TRACE("(%p)->(%d, %p)\n", iface, cInterfaceRefs, InterfaceRefs);
689 for (i = 0; i < cInterfaceRefs; i++)
692 struct stub_manager *stubmgr;
694 hr = ipid_to_stub_manager(&InterfaceRefs[i].ipid, &apt, &stubmgr);
698 /* FIXME: we should undo any changes already made in this function */
702 stub_manager_ext_release(stubmgr, InterfaceRefs[i].cPublicRefs);
703 if (InterfaceRefs[i].cPrivateRefs)
704 FIXME("Releasing %ld refs securely not implemented\n", InterfaceRefs[i].cPrivateRefs);
706 stub_manager_int_release(stubmgr);
707 apartment_release(apt);
713 static const IRemUnknownVtbl RemUnknown_Vtbl =
715 RemUnknown_QueryInterface,
718 RemUnknown_RemQueryInterface,
719 RemUnknown_RemAddRef,
720 RemUnknown_RemRelease
723 /* starts the IRemUnknown listener for the current apartment */
724 HRESULT start_apartment_remote_unknown()
726 IRemUnknown *pRemUnknown;
728 APARTMENT *apt = COM_CurrentApt();
730 EnterCriticalSection(&apt->cs);
731 if (!apt->remunk_exported)
733 /* create the IRemUnknown object */
734 hr = RemUnknown_Construct(&pRemUnknown);
737 STDOBJREF stdobjref; /* dummy - not used */
738 /* register it with the stub manager */
739 hr = marshal_object(apt, &stdobjref, &IID_IRemUnknown, (IUnknown *)pRemUnknown, MSHLFLAGS_NORMAL);
740 /* release our reference to the object as the stub manager will manage the life cycle for us */
741 IRemUnknown_Release(pRemUnknown);
743 apt->remunk_exported = TRUE;
746 LeaveCriticalSection(&apt->cs);