[CMAKE]
[reactos.git] / dll / win32 / ole32 / git.c
1 /*
2 * Implementation of the StdGlobalInterfaceTable object
3 *
4 * The GlobalInterfaceTable (GIT) object is used to marshal interfaces between
5 * threading apartments (contexts). When you want to pass an interface but not
6 * as a parameter, it wouldn't get marshalled automatically, so you can use this
7 * object to insert the interface into a table, and you get back a cookie.
8 * Then when it's retrieved, it'll be unmarshalled into the right apartment.
9 *
10 * Copyright 2003 Mike Hearn <mike@theoretic.com>
11 *
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
16 *
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 */
26
27 #include <stdarg.h>
28
29 #define COBJMACROS
30 #define NONAMELESSUNION
31 #define NONAMELESSSTRUCT
32
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winuser.h"
36 #include "objbase.h"
37 #include "ole2.h"
38 #include "winerror.h"
39
40 #include "compobj_private.h"
41
42 #include "wine/list.h"
43 #include "wine/debug.h"
44
45 WINE_DEFAULT_DEBUG_CHANNEL(ole);
46
47 /****************************************************************************
48 * StdGlobalInterfaceTable definition
49 *
50 * This class implements IGlobalInterfaceTable and is a process-wide singleton
51 * used for marshalling interfaces between threading apartments using cookies.
52 */
53
54 /* Each entry in the linked list of GIT entries */
55 typedef struct StdGITEntry
56 {
57 DWORD cookie;
58 IID iid; /* IID of the interface */
59 IStream* stream; /* Holds the marshalled interface */
60
61 struct list entry;
62 } StdGITEntry;
63
64 /* Class data */
65 typedef struct StdGlobalInterfaceTableImpl
66 {
67 const IGlobalInterfaceTableVtbl *lpVtbl;
68
69 ULONG ref;
70 struct list list;
71 ULONG nextCookie;
72
73 } StdGlobalInterfaceTableImpl;
74
75 void* StdGlobalInterfaceTableInstance;
76
77 static CRITICAL_SECTION git_section;
78 static CRITICAL_SECTION_DEBUG critsect_debug =
79 {
80 0, 0, &git_section,
81 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
82 0, 0, { (DWORD_PTR)(__FILE__ ": global interface table") }
83 };
84 static CRITICAL_SECTION git_section = { &critsect_debug, -1, 0, 0, 0, 0 };
85
86
87 /** This destroys it again. It should revoke all the held interfaces first **/
88 static void StdGlobalInterfaceTable_Destroy(void* self)
89 {
90 TRACE("(%p)\n", self);
91 FIXME("Revoke held interfaces here\n");
92
93 HeapFree(GetProcessHeap(), 0, self);
94 StdGlobalInterfaceTableInstance = NULL;
95 }
96
97 /***
98 * A helper function to traverse the list and find the entry that matches the cookie.
99 * Returns NULL if not found. Must be called inside git_section critical section.
100 */
101 static StdGITEntry*
102 StdGlobalInterfaceTable_FindEntry(IGlobalInterfaceTable* iface, DWORD cookie)
103 {
104 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
105 StdGITEntry* e;
106
107 TRACE("iface=%p, cookie=0x%x\n", iface, cookie);
108
109 LIST_FOR_EACH_ENTRY(e, &self->list, StdGITEntry, entry) {
110 if (e->cookie == cookie)
111 return e;
112 }
113
114 TRACE("Entry not found\n");
115 return NULL;
116 }
117
118 /***
119 * Here's the boring boilerplate stuff for IUnknown
120 */
121
122 static HRESULT WINAPI
123 StdGlobalInterfaceTable_QueryInterface(IGlobalInterfaceTable* iface,
124 REFIID riid, void** ppvObject)
125 {
126 /* Make sure silly coders can't crash us */
127 if (ppvObject == 0) return E_INVALIDARG;
128
129 *ppvObject = 0; /* assume we don't have the interface */
130
131 /* Do we implement that interface? */
132 if (IsEqualIID(&IID_IUnknown, riid) ||
133 IsEqualIID(&IID_IGlobalInterfaceTable, riid))
134 *ppvObject = iface;
135 else
136 return E_NOINTERFACE;
137
138 /* Now inc the refcount */
139 IGlobalInterfaceTable_AddRef(iface);
140 return S_OK;
141 }
142
143 static ULONG WINAPI
144 StdGlobalInterfaceTable_AddRef(IGlobalInterfaceTable* iface)
145 {
146 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
147
148 /* InterlockedIncrement(&self->ref); */
149 return self->ref;
150 }
151
152 static ULONG WINAPI
153 StdGlobalInterfaceTable_Release(IGlobalInterfaceTable* iface)
154 {
155 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
156
157 /* InterlockedDecrement(&self->ref); */
158 if (self->ref == 0) {
159 /* Hey ho, it's time to go, so long again 'till next weeks show! */
160 StdGlobalInterfaceTable_Destroy(self);
161 return 0;
162 }
163
164 return self->ref;
165 }
166
167 /***
168 * Now implement the actual IGlobalInterfaceTable interface
169 */
170
171 static HRESULT WINAPI
172 StdGlobalInterfaceTable_RegisterInterfaceInGlobal(
173 IGlobalInterfaceTable* iface, IUnknown* pUnk,
174 REFIID riid, DWORD* pdwCookie)
175 {
176 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
177 IStream* stream = NULL;
178 HRESULT hres;
179 StdGITEntry* entry;
180 LARGE_INTEGER zero;
181
182 TRACE("iface=%p, pUnk=%p, riid=%s, pdwCookie=0x%p\n", iface, pUnk, debugstr_guid(riid), pdwCookie);
183
184 if (pUnk == NULL) return E_INVALIDARG;
185
186 /* marshal the interface */
187 TRACE("About to marshal the interface\n");
188
189 hres = CreateStreamOnHGlobal(0, TRUE, &stream);
190 if (hres != S_OK) return hres;
191 hres = CoMarshalInterface(stream, riid, pUnk, MSHCTX_INPROC, NULL, MSHLFLAGS_TABLESTRONG);
192 if (hres != S_OK)
193 {
194 IStream_Release(stream);
195 return hres;
196 }
197
198 zero.QuadPart = 0;
199 IStream_Seek(stream, zero, STREAM_SEEK_SET, NULL);
200
201 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(StdGITEntry));
202 if (entry == NULL) return E_OUTOFMEMORY;
203
204 EnterCriticalSection(&git_section);
205
206 entry->iid = *riid;
207 entry->stream = stream;
208 entry->cookie = self->nextCookie;
209 self->nextCookie++; /* inc the cookie count */
210
211 /* insert the new entry at the end of the list */
212 list_add_tail(&self->list, &entry->entry);
213
214 /* and return the cookie */
215 *pdwCookie = entry->cookie;
216
217 LeaveCriticalSection(&git_section);
218
219 TRACE("Cookie is 0x%x\n", entry->cookie);
220 return S_OK;
221 }
222
223 static HRESULT WINAPI
224 StdGlobalInterfaceTable_RevokeInterfaceFromGlobal(
225 IGlobalInterfaceTable* iface, DWORD dwCookie)
226 {
227 StdGITEntry* entry;
228 HRESULT hr;
229
230 TRACE("iface=%p, dwCookie=0x%x\n", iface, dwCookie);
231
232 EnterCriticalSection(&git_section);
233
234 entry = StdGlobalInterfaceTable_FindEntry(iface, dwCookie);
235 if (entry == NULL) {
236 TRACE("Entry not found\n");
237 LeaveCriticalSection(&git_section);
238 return E_INVALIDARG; /* not found */
239 }
240
241 list_remove(&entry->entry);
242
243 LeaveCriticalSection(&git_section);
244
245 /* Free the stream */
246 hr = CoReleaseMarshalData(entry->stream);
247 if (hr != S_OK)
248 {
249 WARN("Failed to release marshal data, hr = 0x%08x\n", hr);
250 return hr;
251 }
252 IStream_Release(entry->stream);
253
254 HeapFree(GetProcessHeap(), 0, entry);
255 return S_OK;
256 }
257
258 static HRESULT WINAPI
259 StdGlobalInterfaceTable_GetInterfaceFromGlobal(
260 IGlobalInterfaceTable* iface, DWORD dwCookie,
261 REFIID riid, void **ppv)
262 {
263 StdGITEntry* entry;
264 HRESULT hres;
265 IStream *stream;
266
267 TRACE("dwCookie=0x%x, riid=%s, ppv=%p\n", dwCookie, debugstr_guid(riid), ppv);
268
269 EnterCriticalSection(&git_section);
270
271 entry = StdGlobalInterfaceTable_FindEntry(iface, dwCookie);
272 if (entry == NULL) {
273 WARN("Entry for cookie 0x%x not found\n", dwCookie);
274 LeaveCriticalSection(&git_section);
275 return E_INVALIDARG;
276 }
277
278 TRACE("entry=%p\n", entry);
279
280 hres = IStream_Clone(entry->stream, &stream);
281
282 LeaveCriticalSection(&git_section);
283
284 if (hres != S_OK) {
285 WARN("Failed to clone stream with error 0x%08x\n", hres);
286 return hres;
287 }
288
289 /* unmarshal the interface */
290 hres = CoUnmarshalInterface(stream, riid, ppv);
291 IStream_Release(stream);
292
293 if (hres) {
294 WARN("Failed to unmarshal stream\n");
295 return hres;
296 }
297
298 TRACE("ppv=%p\n", *ppv);
299 return S_OK;
300 }
301
302 /* Classfactory definition - despite what MSDN says, some programs need this */
303
304 static HRESULT WINAPI
305 GITCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid, LPVOID *ppv)
306 {
307 *ppv = NULL;
308 if (IsEqualIID(riid,&IID_IUnknown) ||
309 IsEqualIID(riid,&IID_IGlobalInterfaceTable))
310 {
311 *ppv = iface;
312 return S_OK;
313 }
314 return E_NOINTERFACE;
315 }
316
317 static ULONG WINAPI GITCF_AddRef(LPCLASSFACTORY iface)
318 {
319 return 2;
320 }
321
322 static ULONG WINAPI GITCF_Release(LPCLASSFACTORY iface)
323 {
324 return 1;
325 }
326
327 static HRESULT WINAPI
328 GITCF_CreateInstance(LPCLASSFACTORY iface, LPUNKNOWN pUnk,
329 REFIID riid, LPVOID *ppv)
330 {
331 if (IsEqualIID(riid,&IID_IGlobalInterfaceTable)) {
332 if (StdGlobalInterfaceTableInstance == NULL)
333 StdGlobalInterfaceTableInstance = StdGlobalInterfaceTable_Construct();
334 return IGlobalInterfaceTable_QueryInterface( (IGlobalInterfaceTable*) StdGlobalInterfaceTableInstance, riid, ppv);
335 }
336
337 FIXME("(%s), not supported.\n",debugstr_guid(riid));
338 return E_NOINTERFACE;
339 }
340
341 static HRESULT WINAPI GITCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
342 {
343 FIXME("(%d), stub!\n",fLock);
344 return S_OK;
345 }
346
347 static const IClassFactoryVtbl GITClassFactoryVtbl = {
348 GITCF_QueryInterface,
349 GITCF_AddRef,
350 GITCF_Release,
351 GITCF_CreateInstance,
352 GITCF_LockServer
353 };
354
355 static const IClassFactoryVtbl *PGITClassFactoryVtbl = &GITClassFactoryVtbl;
356
357 HRESULT StdGlobalInterfaceTable_GetFactory(LPVOID *ppv)
358 {
359 *ppv = &PGITClassFactoryVtbl;
360 TRACE("Returning GIT classfactory\n");
361 return S_OK;
362 }
363
364 /* Virtual function table */
365 static const IGlobalInterfaceTableVtbl StdGlobalInterfaceTableImpl_Vtbl =
366 {
367 StdGlobalInterfaceTable_QueryInterface,
368 StdGlobalInterfaceTable_AddRef,
369 StdGlobalInterfaceTable_Release,
370 StdGlobalInterfaceTable_RegisterInterfaceInGlobal,
371 StdGlobalInterfaceTable_RevokeInterfaceFromGlobal,
372 StdGlobalInterfaceTable_GetInterfaceFromGlobal
373 };
374
375 /** This function constructs the GIT. It should only be called once **/
376 void* StdGlobalInterfaceTable_Construct(void)
377 {
378 StdGlobalInterfaceTableImpl* newGIT;
379
380 newGIT = HeapAlloc(GetProcessHeap(), 0, sizeof(StdGlobalInterfaceTableImpl));
381 if (newGIT == 0) return newGIT;
382
383 newGIT->lpVtbl = &StdGlobalInterfaceTableImpl_Vtbl;
384 newGIT->ref = 1; /* Initialise the reference count */
385 list_init(&newGIT->list);
386 newGIT->nextCookie = 0xf100; /* that's where windows starts, so that's where we start */
387 TRACE("Created the GIT at %p\n", newGIT);
388
389 return (void*)newGIT;
390 }