Added a missing prototype.
[wine/wine-kai.git] / dlls / ole32 / stubmanager.c
blob171babbf4802d9934bebc58cc2d3298729a1644e
1 /*
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
26 #define COBJMACROS
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
30 #include <assert.h>
31 #include <stdarg.h>
32 #include <limits.h>
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winuser.h"
37 #include "objbase.h"
38 #include "ole2.h"
39 #include "ole2ver.h"
40 #include "rpc.h"
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;
56 assert( apt );
58 sm = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct stub_manager));
59 if (!sm) return NULL;
61 list_init(&sm->ifstubs);
62 InitializeCriticalSection(&sm->lock);
63 IUnknown_AddRef(object);
64 sm->object = object;
65 sm->apt = apt;
67 /* start off with 2 references because the stub is in the apartment
68 * and the caller will also hold a reference */
69 sm->refs = 2;
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.
77 sm->extrefs = 0;
79 if (mshlflags & MSHLFLAGS_TABLESTRONG)
80 sm->state = STUBSTATE_TABLE_STRONG;
81 else if (mshlflags & MSHLFLAGS_TABLEWEAK)
82 sm->state = STUBSTATE_TABLE_WEAK_UNMARSHALED;
83 else
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);
93 return sm;
96 /* m->apt->cs must be held on entry to this function */
97 static void stub_manager_delete(struct stub_manager *m)
99 struct list *cursor;
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;
125 struct list *cursor;
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)
134 result = m;
135 stub_manager_int_addref(result);
136 break;
139 LeaveCriticalSection(&apt->cs);
141 if (result)
142 TRACE("found %p for object %p\n", result, object);
143 else
144 TRACE("not found for object %p\n", object);
146 return result;
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)
153 int found = FALSE;
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)
161 found = TRUE;
162 stub_manager_int_release(stubmgr);
163 break;
166 LeaveCriticalSection(&apt->cs);
168 if (found)
169 TRACE("disconnect object %p\n", object);
170 else
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;
180 struct list *cursor;
182 EnterCriticalSection(&apt->cs);
183 LIST_FOR_EACH( cursor, &apt->stubmgrs )
185 struct stub_manager *m = LIST_ENTRY( cursor, struct stub_manager, entry );
187 if (m->oid == oid)
189 result = m;
190 stub_manager_int_addref(result);
191 break;
194 LeaveCriticalSection(&apt->cs);
196 if (result)
197 TRACE("found %p for oid %s\n", result, wine_dbgstr_longlong(oid));
198 else
199 TRACE("not found for oid %s\n", wine_dbgstr_longlong(oid));
201 return result;
204 /* increments the internal refcount */
205 ULONG stub_manager_int_addref(struct stub_manager *This)
207 ULONG refs;
209 EnterCriticalSection(&This->apt->cs);
210 refs = ++This->refs;
211 LeaveCriticalSection(&This->apt->cs);
213 TRACE("before %ld\n", refs - 1);
215 return refs;
218 /* decrements the internal refcount */
219 ULONG stub_manager_int_release(struct stub_manager *This)
221 ULONG refs;
222 APARTMENT *apt = This->apt;
224 EnterCriticalSection(&apt->cs);
225 refs = --This->refs;
227 TRACE("after %ld\n", refs);
229 if (!refs)
230 stub_manager_delete(This);
231 LeaveCriticalSection(&apt->cs);
233 return refs;
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)
239 ULONG rc;
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);
251 return rc;
254 /* remove some external references */
255 ULONG stub_manager_ext_release(struct stub_manager *m, ULONG refs)
257 ULONG rc;
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);
269 if (rc == 0)
270 stub_manager_int_release(m);
272 return rc;
275 static struct ifstub *stub_manager_ipid_to_ifstub(struct stub_manager *m, const IPID *ipid)
277 struct list *cursor;
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))
287 result = ifstub;
288 break;
291 LeaveCriticalSection(&m->lock);
293 return result;
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;
302 struct list *cursor;
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))
311 result = m;
312 stub_manager_int_addref(result);
313 break;
316 LeaveCriticalSection(&apt->cs);
318 if (result)
319 TRACE("found %p for ipid %s\n", result, debugstr_guid(ipid));
320 else
321 ERR("not found for ipid %s\n", debugstr_guid(ipid));
323 return result;
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);
331 else
332 *stub_apt = COM_ApartmentFromTID(ipid->Data2);
333 if (!*stub_apt)
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);
339 if (!*stubmgr_ret)
341 COM_ApartmentRelease(*stub_apt);
342 *stub_apt = NULL;
343 return RPC_E_INVALID_OBJECT;
345 return S_OK;
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;
355 HRESULT hr;
357 *stub_apt = NULL;
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);
363 if (ifstub)
364 ret = ifstub->stubbuffer;
366 if (ret) IRpcStubBuffer_AddRef(ret);
368 stub_manager_int_release(stubmgr);
370 return ret;
373 /* generates an ipid in the following format (similar to native version):
374 * Data1 = apartment-local ipid counter
375 * Data2 = apartment creator thread ID
376 * Data3 = process ID
377 * Data4 = random value
379 static inline HRESULT generate_ipid(struct stub_manager *m, IPID *ipid)
381 HRESULT hr;
382 hr = UuidCreate(ipid);
383 if (FAILED(hr))
385 ERR("couldn't create IPID for stub manager %p\n", m);
386 UuidCreateNil(ipid);
387 return hr;
390 ipid->Data1 = InterlockedIncrement(&m->apt->ipidc);
391 ipid->Data2 = (USHORT)m->apt->tid;
392 ipid->Data3 = (USHORT)GetCurrentProcessId();
393 return S_OK;
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)
399 struct ifstub *stub;
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;
408 IUnknown_AddRef(sb);
410 /* no need to ref this, same object as sb */
411 stub->iface = iptr;
413 stub->iid = *iid;
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));
425 else
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));
434 return stub;
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)
454 BOOL ret;
456 EnterCriticalSection(&m->lock);
458 switch (m->state)
460 case STUBSTATE_TABLE_STRONG:
461 case STUBSTATE_TABLE_WEAK_MARSHALED:
462 /* no transition */
463 ret = TRUE;
464 break;
465 case STUBSTATE_TABLE_WEAK_UNMARSHALED:
466 m->state = STUBSTATE_TABLE_WEAK_MARSHALED;
467 ret = TRUE;
468 break;
469 case STUBSTATE_NORMAL_MARSHALED:
470 m->state = STUBSTATE_NORMAL_UNMARSHALED;
471 ret = TRUE;
472 break;
473 default:
474 WARN("object OID %s already unmarshaled\n",
475 wine_dbgstr_longlong(m->oid));
476 ret = FALSE;
477 break;
480 LeaveCriticalSection(&m->lock);
482 return ret;
485 void stub_manager_release_marshal_data(struct stub_manager *m, ULONG refs)
487 EnterCriticalSection(&m->lock);
489 switch (m->state)
491 case STUBSTATE_NORMAL_MARSHALED:
492 case STUBSTATE_NORMAL_UNMARSHALED: /* FIXME: check this */
493 /* nothing to change */
494 break;
495 case STUBSTATE_TABLE_WEAK_UNMARSHALED:
496 case STUBSTATE_TABLE_STRONG:
497 refs = 1;
498 break;
499 case STUBSTATE_TABLE_WEAK_MARSHALED:
500 refs = 0; /* like native */
501 break;
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)
512 BOOL ret;
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);
520 return ret;
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;
538 ULONG refs;
539 } RemUnknown;
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;
552 This->refs = 1;
554 *ppRemUnknown = (IRemUnknown *)This;
555 return S_OK;
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);
567 return S_OK;
570 FIXME("No interface for iid %s\n", debugstr_guid(riid));
572 *ppv = NULL;
573 return E_NOINTERFACE;
576 static ULONG WINAPI RemUnknown_AddRef(IRemUnknown *iface)
578 ULONG refs;
579 RemUnknown *This = (RemUnknown *)iface;
581 refs = InterlockedIncrement(&This->refs);
583 TRACE("%p before: %ld\n", iface, refs-1);
584 return refs;
587 static ULONG WINAPI RemUnknown_Release(IRemUnknown *iface)
589 ULONG refs;
590 RemUnknown *This = (RemUnknown *)iface;
592 refs = InterlockedDecrement(&This->refs);
593 if (!refs)
594 HeapFree(GetProcessHeap(), 0, This);
596 TRACE("%p after: %ld\n", iface, refs);
597 return 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)] */)
604 HRESULT hr;
605 USHORT i;
606 USHORT successful_qis = 0;
607 APARTMENT *apt;
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);
621 if (hrobj == S_OK)
622 successful_qis++;
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 */
633 else
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)] */)
642 HRESULT hr = S_OK;
643 USHORT i;
645 TRACE("(%p)->(%d, %p, %p)\n", iface, cInterfaceRefs, InterfaceRefs, pResults);
647 for (i = 0; i < cInterfaceRefs; i++)
649 APARTMENT *apt;
650 struct stub_manager *stubmgr;
652 pResults[i] = ipid_to_stub_manager(&InterfaceRefs[i].ipid, &apt, &stubmgr);
653 if (pResults[i] != S_OK)
655 hr = S_FALSE;
656 continue;
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);
667 return hr;
670 static HRESULT WINAPI RemUnknown_RemRelease(IRemUnknown *iface,
671 USHORT cInterfaceRefs,
672 REMINTERFACEREF* InterfaceRefs /* [size_is(cInterfaceRefs)] */)
674 HRESULT hr = S_OK;
675 USHORT i;
677 TRACE("(%p)->(%d, %p)\n", iface, cInterfaceRefs, InterfaceRefs);
679 for (i = 0; i < cInterfaceRefs; i++)
681 APARTMENT *apt;
682 struct stub_manager *stubmgr;
684 hr = ipid_to_stub_manager(&InterfaceRefs[i].ipid, &apt, &stubmgr);
685 if (hr != S_OK)
687 hr = E_INVALIDARG;
688 /* FIXME: we should undo any changes already made in this function */
689 break;
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);
700 return hr;
703 static const IRemUnknownVtbl RemUnknown_Vtbl =
705 RemUnknown_QueryInterface,
706 RemUnknown_AddRef,
707 RemUnknown_Release,
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;
717 HRESULT hr = S_OK;
718 APARTMENT *apt = COM_CurrentApt();
720 EnterCriticalSection(&apt->cs);
721 if (!apt->remunk_exported)
723 /* create the IRemUnknown object */
724 hr = RemUnknown_Construct(&pRemUnknown);
725 if (hr == S_OK)
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);
732 if (hr == S_OK)
733 apt->remunk_exported = TRUE;
736 LeaveCriticalSection(&apt->cs);
737 return hr;