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
);
62 InitializeCriticalSection(&sm
->lock
);
63 IUnknown_AddRef(object
);
67 /* start off with 2 references because the stub is in the apartment
68 * and the caller will also hold a reference */
71 /* yes, that's right, this starts at zero. that's zero EXTERNAL
72 * refs, ie nobody has unmarshalled anything yet. we can't have
73 * negative refs because the stub manager cannot be explicitly
74 * killed, it has to die by somebody unmarshalling then releasing
75 * the marshalled ifptr.
79 if (mshlflags
& MSHLFLAGS_TABLESTRONG
)
80 sm
->state
= STUBSTATE_TABLE_STRONG
;
81 else if (mshlflags
& MSHLFLAGS_TABLEWEAK
)
82 sm
->state
= STUBSTATE_TABLE_WEAK_UNMARSHALED
;
84 sm
->state
= STUBSTATE_NORMAL_MARSHALED
;
86 EnterCriticalSection(&apt
->cs
);
87 sm
->oid
= apt
->oidc
++;
88 list_add_head(&apt
->stubmgrs
, &sm
->entry
);
89 LeaveCriticalSection(&apt
->cs
);
91 TRACE("Created new stub manager (oid=%s) at %p for object with IUnknown %p\n", wine_dbgstr_longlong(sm
->oid
), sm
, object
);
96 /* m->apt->cs must be held on entry to this function */
97 static void stub_manager_delete(struct stub_manager
*m
)
101 TRACE("destroying %p (oid=%s)\n", m
, wine_dbgstr_longlong(m
->oid
));
103 list_remove(&m
->entry
);
105 /* release every ifstub */
106 while ((cursor
= list_head(&m
->ifstubs
)))
108 struct ifstub
*ifstub
= LIST_ENTRY(cursor
, struct ifstub
, entry
);
109 stub_manager_delete_ifstub(m
, ifstub
);
112 IUnknown_Release(m
->object
);
114 DeleteCriticalSection(&m
->lock
);
116 HeapFree(GetProcessHeap(), 0, m
);
119 /* gets the stub manager associated with an object - caller must have
120 * a reference to the apartment while a reference to the stub manager is held.
121 * it must also call release on the stub manager when it is no longer needed */
122 struct stub_manager
*get_stub_manager_from_object(APARTMENT
*apt
, void *object
)
124 struct stub_manager
*result
= NULL
;
127 EnterCriticalSection(&apt
->cs
);
128 LIST_FOR_EACH( cursor
, &apt
->stubmgrs
)
130 struct stub_manager
*m
= LIST_ENTRY( cursor
, struct stub_manager
, entry
);
132 if (m
->object
== object
)
135 stub_manager_int_addref(result
);
139 LeaveCriticalSection(&apt
->cs
);
142 TRACE("found %p for object %p\n", result
, object
);
144 TRACE("not found for object %p\n", object
);
149 /* removes the apartment reference to an object, destroying it when no other
150 * threads have a reference to it */
151 void apartment_disconnect_object(APARTMENT
*apt
, void *object
)
154 struct stub_manager
*stubmgr
;
156 EnterCriticalSection(&apt
->cs
);
157 LIST_FOR_EACH_ENTRY( stubmgr
, &apt
->stubmgrs
, struct stub_manager
, entry
)
159 if (stubmgr
->object
== object
)
162 stub_manager_int_release(stubmgr
);
166 LeaveCriticalSection(&apt
->cs
);
169 TRACE("disconnect object %p\n", object
);
171 WARN("couldn't find object %p\n", object
);
174 /* gets the stub manager associated with an object id - caller must have
175 * a reference to the apartment while a reference to the stub manager is held.
176 * it must also call release on the stub manager when it is no longer needed */
177 struct stub_manager
*get_stub_manager(APARTMENT
*apt
, OID oid
)
179 struct stub_manager
*result
= NULL
;
182 EnterCriticalSection(&apt
->cs
);
183 LIST_FOR_EACH( cursor
, &apt
->stubmgrs
)
185 struct stub_manager
*m
= LIST_ENTRY( cursor
, struct stub_manager
, entry
);
190 stub_manager_int_addref(result
);
194 LeaveCriticalSection(&apt
->cs
);
197 TRACE("found %p for oid %s\n", result
, wine_dbgstr_longlong(oid
));
199 TRACE("not found for oid %s\n", wine_dbgstr_longlong(oid
));
204 /* increments the internal refcount */
205 ULONG
stub_manager_int_addref(struct stub_manager
*This
)
209 EnterCriticalSection(&This
->apt
->cs
);
211 LeaveCriticalSection(&This
->apt
->cs
);
213 TRACE("before %ld\n", refs
- 1);
218 /* decrements the internal refcount */
219 ULONG
stub_manager_int_release(struct stub_manager
*This
)
222 APARTMENT
*apt
= This
->apt
;
224 EnterCriticalSection(&apt
->cs
);
227 TRACE("after %ld\n", refs
);
230 stub_manager_delete(This
);
231 LeaveCriticalSection(&apt
->cs
);
236 /* add some external references (ie from a client that unmarshaled an ifptr) */
237 ULONG
stub_manager_ext_addref(struct stub_manager
*m
, ULONG refs
)
241 EnterCriticalSection(&m
->lock
);
243 /* make sure we don't overflow extrefs */
244 refs
= min(refs
, (ULONG_MAX
-1 - m
->extrefs
));
245 rc
= (m
->extrefs
+= refs
);
247 LeaveCriticalSection(&m
->lock
);
249 TRACE("added %lu refs to %p (oid %s), rc is now %lu\n", refs
, m
, wine_dbgstr_longlong(m
->oid
), rc
);
254 /* remove some external references */
255 ULONG
stub_manager_ext_release(struct stub_manager
*m
, ULONG refs
)
259 EnterCriticalSection(&m
->lock
);
261 /* make sure we don't underflow extrefs */
262 refs
= min(refs
, m
->extrefs
);
263 rc
= (m
->extrefs
-= refs
);
265 LeaveCriticalSection(&m
->lock
);
267 TRACE("removed %lu refs from %p (oid %s), rc is now %lu\n", refs
, m
, wine_dbgstr_longlong(m
->oid
), rc
);
270 stub_manager_int_release(m
);
275 static struct ifstub
*stub_manager_ipid_to_ifstub(struct stub_manager
*m
, const IPID
*ipid
)
278 struct ifstub
*result
= NULL
;
280 EnterCriticalSection(&m
->lock
);
281 LIST_FOR_EACH( cursor
, &m
->ifstubs
)
283 struct ifstub
*ifstub
= LIST_ENTRY( cursor
, struct ifstub
, entry
);
285 if (IsEqualGUID(ipid
, &ifstub
->ipid
))
291 LeaveCriticalSection(&m
->lock
);
296 /* gets the stub manager associated with an ipid - caller must have
297 * a reference to the apartment while a reference to the stub manager is held.
298 * it must also call release on the stub manager when it is no longer needed */
299 static struct stub_manager
*get_stub_manager_from_ipid(APARTMENT
*apt
, const IPID
*ipid
)
301 struct stub_manager
*result
= NULL
;
304 EnterCriticalSection(&apt
->cs
);
305 LIST_FOR_EACH( cursor
, &apt
->stubmgrs
)
307 struct stub_manager
*m
= LIST_ENTRY( cursor
, struct stub_manager
, entry
);
309 if (stub_manager_ipid_to_ifstub(m
, ipid
))
312 stub_manager_int_addref(result
);
316 LeaveCriticalSection(&apt
->cs
);
319 TRACE("found %p for ipid %s\n", result
, debugstr_guid(ipid
));
321 ERR("not found for ipid %s\n", debugstr_guid(ipid
));
326 HRESULT
ipid_to_stub_manager(const IPID
*ipid
, APARTMENT
**stub_apt
, struct stub_manager
**stubmgr_ret
)
328 /* FIXME: hack for IRemUnknown */
329 if (ipid
->Data2
== 0xffff)
330 *stub_apt
= COM_ApartmentFromOXID(*(OXID
*)ipid
->Data4
, TRUE
);
332 *stub_apt
= COM_ApartmentFromTID(ipid
->Data2
);
335 ERR("Couldn't find apartment corresponding to TID 0x%04x\n", ipid
->Data2
);
336 return RPC_E_INVALID_OBJECT
;
338 *stubmgr_ret
= get_stub_manager_from_ipid(*stub_apt
, ipid
);
341 COM_ApartmentRelease(*stub_apt
);
343 return RPC_E_INVALID_OBJECT
;
348 /* gets the apartment and IRpcStubBuffer from an object. the caller must
349 * release the references to both objects */
350 IRpcStubBuffer
*ipid_to_apt_and_stubbuffer(const IPID
*ipid
, APARTMENT
**stub_apt
)
352 IRpcStubBuffer
*ret
= NULL
;
353 struct stub_manager
*stubmgr
;
354 struct ifstub
*ifstub
;
359 hr
= ipid_to_stub_manager(ipid
, stub_apt
, &stubmgr
);
360 if (hr
!= S_OK
) return NULL
;
362 ifstub
= stub_manager_ipid_to_ifstub(stubmgr
, ipid
);
364 ret
= ifstub
->stubbuffer
;
366 if (ret
) IRpcStubBuffer_AddRef(ret
);
368 stub_manager_int_release(stubmgr
);
373 /* generates an ipid in the following format (similar to native version):
374 * Data1 = apartment-local ipid counter
375 * Data2 = apartment creator thread ID
377 * Data4 = random value
379 static inline HRESULT
generate_ipid(struct stub_manager
*m
, IPID
*ipid
)
382 hr
= UuidCreate(ipid
);
385 ERR("couldn't create IPID for stub manager %p\n", m
);
390 ipid
->Data1
= InterlockedIncrement(&m
->apt
->ipidc
);
391 ipid
->Data2
= (USHORT
)m
->apt
->tid
;
392 ipid
->Data3
= (USHORT
)GetCurrentProcessId();
396 /* registers a new interface stub COM object with the stub manager and returns registration record */
397 struct ifstub
*stub_manager_new_ifstub(struct stub_manager
*m
, IRpcStubBuffer
*sb
, IUnknown
*iptr
, REFIID iid
)
401 TRACE("oid=%s, stubbuffer=%p, iptr=%p, iid=%s\n",
402 wine_dbgstr_longlong(m
->oid
), sb
, iptr
, debugstr_guid(iid
));
404 stub
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(struct ifstub
));
405 if (!stub
) return NULL
;
407 stub
->stubbuffer
= sb
;
410 /* no need to ref this, same object as sb */
415 /* FIXME: hack for IRemUnknown because we don't notify SCM of our IPID
416 * yet, so we need to use a well-known one */
417 if (IsEqualIID(iid
, &IID_IRemUnknown
))
419 stub
->ipid
.Data1
= 0xffffffff;
420 stub
->ipid
.Data2
= 0xffff;
421 stub
->ipid
.Data3
= 0xffff;
422 assert(sizeof(stub
->ipid
.Data4
) == sizeof(m
->apt
->oxid
));
423 memcpy(&stub
->ipid
.Data4
, &m
->apt
->oxid
, sizeof(OXID
));
426 generate_ipid(m
, &stub
->ipid
);
428 EnterCriticalSection(&m
->lock
);
429 list_add_head(&m
->ifstubs
, &stub
->entry
);
430 LeaveCriticalSection(&m
->lock
);
432 TRACE("ifstub %p created with ipid %s\n", stub
, debugstr_guid(&stub
->ipid
));
437 static void stub_manager_delete_ifstub(struct stub_manager
*m
, struct ifstub
*ifstub
)
439 TRACE("m=%p, m->oid=%s, ipid=%s\n", m
, wine_dbgstr_longlong(m
->oid
), debugstr_guid(&ifstub
->ipid
));
441 list_remove(&ifstub
->entry
);
443 RPC_UnregisterInterface(&ifstub
->iid
);
445 IUnknown_Release(ifstub
->stubbuffer
);
446 IUnknown_Release(ifstub
->iface
);
448 HeapFree(GetProcessHeap(), 0, ifstub
);
451 /* returns TRUE if it is possible to unmarshal, FALSE otherwise. */
452 BOOL
stub_manager_notify_unmarshal(struct stub_manager
*m
)
456 EnterCriticalSection(&m
->lock
);
460 case STUBSTATE_TABLE_STRONG
:
461 case STUBSTATE_TABLE_WEAK_MARSHALED
:
465 case STUBSTATE_TABLE_WEAK_UNMARSHALED
:
466 m
->state
= STUBSTATE_TABLE_WEAK_MARSHALED
;
469 case STUBSTATE_NORMAL_MARSHALED
:
470 m
->state
= STUBSTATE_NORMAL_UNMARSHALED
;
474 WARN("object OID %s already unmarshaled\n",
475 wine_dbgstr_longlong(m
->oid
));
480 LeaveCriticalSection(&m
->lock
);
485 void stub_manager_release_marshal_data(struct stub_manager
*m
, ULONG refs
)
487 EnterCriticalSection(&m
->lock
);
491 case STUBSTATE_NORMAL_MARSHALED
:
492 case STUBSTATE_NORMAL_UNMARSHALED
: /* FIXME: check this */
493 /* nothing to change */
495 case STUBSTATE_TABLE_WEAK_UNMARSHALED
:
496 case STUBSTATE_TABLE_STRONG
:
499 case STUBSTATE_TABLE_WEAK_MARSHALED
:
500 refs
= 0; /* like native */
504 stub_manager_ext_release(m
, refs
);
506 LeaveCriticalSection(&m
->lock
);
509 /* is an ifstub table marshaled? */
510 BOOL
stub_manager_is_table_marshaled(struct stub_manager
*m
)
514 EnterCriticalSection(&m
->lock
);
515 ret
= ((m
->state
== STUBSTATE_TABLE_STRONG
) ||
516 (m
->state
== STUBSTATE_TABLE_WEAK_MARSHALED
) ||
517 (m
->state
== STUBSTATE_TABLE_WEAK_UNMARSHALED
));
518 LeaveCriticalSection(&m
->lock
);
524 /*****************************************************************************
526 * IRemUnknown implementation
529 * Note: this object is not related to the lifetime of a stub_manager, but it
530 * interacts with stub managers.
533 const IID IID_IRemUnknown
= { 0x00000131, 0, 0, {0xc0, 0, 0, 0, 0, 0, 0, 0x46} };
535 typedef struct rem_unknown
537 const IRemUnknownVtbl
*lpVtbl
;
541 static const IRemUnknownVtbl RemUnknown_Vtbl
;
544 /* construct an IRemUnknown object with one outstanding reference */
545 static HRESULT
RemUnknown_Construct(IRemUnknown
**ppRemUnknown
)
547 RemUnknown
*This
= HeapAlloc(GetProcessHeap(), 0, sizeof(*This
));
549 if (!This
) return E_OUTOFMEMORY
;
551 This
->lpVtbl
= &RemUnknown_Vtbl
;
554 *ppRemUnknown
= (IRemUnknown
*)This
;
558 static HRESULT WINAPI
RemUnknown_QueryInterface(IRemUnknown
*iface
, REFIID riid
, void **ppv
)
560 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
562 if (IsEqualIID(riid
, &IID_IUnknown
) ||
563 IsEqualIID(riid
, &IID_IRemUnknown
))
565 *ppv
= (LPVOID
)iface
;
566 IRemUnknown_AddRef(iface
);
570 FIXME("No interface for iid %s\n", debugstr_guid(riid
));
573 return E_NOINTERFACE
;
576 static ULONG WINAPI
RemUnknown_AddRef(IRemUnknown
*iface
)
579 RemUnknown
*This
= (RemUnknown
*)iface
;
581 refs
= InterlockedIncrement(&This
->refs
);
583 TRACE("%p before: %ld\n", iface
, refs
-1);
587 static ULONG WINAPI
RemUnknown_Release(IRemUnknown
*iface
)
590 RemUnknown
*This
= (RemUnknown
*)iface
;
592 refs
= InterlockedDecrement(&This
->refs
);
594 HeapFree(GetProcessHeap(), 0, This
);
596 TRACE("%p after: %ld\n", iface
, refs
);
600 static HRESULT WINAPI
RemUnknown_RemQueryInterface(IRemUnknown
*iface
,
601 REFIPID ripid
, ULONG cRefs
, USHORT cIids
, IID
*iids
/* [size_is(cIids)] */,
602 REMQIRESULT
**ppQIResults
/* [size_is(,cIids)] */)
606 USHORT successful_qis
= 0;
608 struct stub_manager
*stubmgr
;
610 TRACE("(%p)->(%s, %ld, %d, %p, %p)\n", iface
, debugstr_guid(ripid
), cRefs
, cIids
, iids
, ppQIResults
);
612 hr
= ipid_to_stub_manager(ripid
, &apt
, &stubmgr
);
613 if (hr
!= S_OK
) return hr
;
615 *ppQIResults
= CoTaskMemAlloc(sizeof(REMQIRESULT
) * cIids
);
617 for (i
= 0; i
< cIids
; i
++)
619 HRESULT hrobj
= register_ifstub(apt
, &(*ppQIResults
)[i
].std
, &iids
[i
],
620 stubmgr
->object
, MSHLFLAGS_NORMAL
);
623 (*ppQIResults
)[i
].hResult
= hrobj
;
626 stub_manager_int_release(stubmgr
);
627 COM_ApartmentRelease(apt
);
629 if (successful_qis
== cIids
)
630 return S_OK
; /* we got all requested interfaces */
631 else if (successful_qis
== 0)
632 return E_NOINTERFACE
; /* we didn't get any interfaces */
634 return S_FALSE
; /* we got some interfaces */
637 static HRESULT WINAPI
RemUnknown_RemAddRef(IRemUnknown
*iface
,
638 USHORT cInterfaceRefs
,
639 REMINTERFACEREF
* InterfaceRefs
/* [size_is(cInterfaceRefs)] */,
640 HRESULT
*pResults
/* [size_is(cInterfaceRefs)] */)
645 TRACE("(%p)->(%d, %p, %p)\n", iface
, cInterfaceRefs
, InterfaceRefs
, pResults
);
647 for (i
= 0; i
< cInterfaceRefs
; i
++)
650 struct stub_manager
*stubmgr
;
652 pResults
[i
] = ipid_to_stub_manager(&InterfaceRefs
[i
].ipid
, &apt
, &stubmgr
);
653 if (pResults
[i
] != S_OK
)
659 stub_manager_ext_addref(stubmgr
, InterfaceRefs
[i
].cPublicRefs
);
660 if (InterfaceRefs
[i
].cPrivateRefs
)
661 FIXME("Adding %ld refs securely not implemented\n", InterfaceRefs
[i
].cPrivateRefs
);
663 stub_manager_int_release(stubmgr
);
664 COM_ApartmentRelease(apt
);
670 static HRESULT WINAPI
RemUnknown_RemRelease(IRemUnknown
*iface
,
671 USHORT cInterfaceRefs
,
672 REMINTERFACEREF
* InterfaceRefs
/* [size_is(cInterfaceRefs)] */)
677 TRACE("(%p)->(%d, %p)\n", iface
, cInterfaceRefs
, InterfaceRefs
);
679 for (i
= 0; i
< cInterfaceRefs
; i
++)
682 struct stub_manager
*stubmgr
;
684 hr
= ipid_to_stub_manager(&InterfaceRefs
[i
].ipid
, &apt
, &stubmgr
);
688 /* FIXME: we should undo any changes already made in this function */
692 stub_manager_ext_release(stubmgr
, InterfaceRefs
[i
].cPublicRefs
);
693 if (InterfaceRefs
[i
].cPrivateRefs
)
694 FIXME("Releasing %ld refs securely not implemented\n", InterfaceRefs
[i
].cPrivateRefs
);
696 stub_manager_int_release(stubmgr
);
697 COM_ApartmentRelease(apt
);
703 static const IRemUnknownVtbl RemUnknown_Vtbl
=
705 RemUnknown_QueryInterface
,
708 RemUnknown_RemQueryInterface
,
709 RemUnknown_RemAddRef
,
710 RemUnknown_RemRelease
713 /* starts the IRemUnknown listener for the current apartment */
714 HRESULT
start_apartment_remote_unknown()
716 IRemUnknown
*pRemUnknown
;
718 APARTMENT
*apt
= COM_CurrentApt();
720 EnterCriticalSection(&apt
->cs
);
721 if (!apt
->remunk_exported
)
723 /* create the IRemUnknown object */
724 hr
= RemUnknown_Construct(&pRemUnknown
);
727 STDOBJREF stdobjref
; /* dummy - not used */
728 /* register it with the stub manager */
729 hr
= register_ifstub(apt
, &stdobjref
, &IID_IRemUnknown
, (IUnknown
*)pRemUnknown
, MSHLFLAGS_NORMAL
);
730 /* release our reference to the object as the stub manager will manage the life cycle for us */
731 IRemUnknown_Release(pRemUnknown
);
733 apt
->remunk_exported
= TRUE
;
736 LeaveCriticalSection(&apt
->cs
);