rsaenh: Cast-qual warning fix.
[wine] / dlls / 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 "config.h"
28
29 #include <assert.h>
30 #include <stdlib.h>
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <string.h>
34
35 #define COBJMACROS
36 #define NONAMELESSUNION
37 #define NONAMELESSSTRUCT
38
39 #include "windef.h"
40 #include "winbase.h"
41 #include "winuser.h"
42 #include "objbase.h"
43 #include "ole2.h"
44 #include "winerror.h"
45 #include "winreg.h"
46 #include "winternl.h"
47
48 #include "compobj_private.h" 
49
50 #include "wine/debug.h"
51
52 WINE_DEFAULT_DEBUG_CHANNEL(ole);
53
54 /****************************************************************************
55  * StdGlobalInterfaceTable definition
56  *
57  * This class implements IGlobalInterfaceTable and is a process-wide singleton
58  * used for marshalling interfaces between threading apartments using cookies.
59  */
60
61 /* Each entry in the linked list of GIT entries */
62 typedef struct StdGITEntry
63 {
64   DWORD cookie;
65   IID iid;         /* IID of the interface */
66   IStream* stream; /* Holds the marshalled interface */
67
68   struct StdGITEntry* next;
69   struct StdGITEntry* prev;  
70 } StdGITEntry;
71
72 /* Class data */
73 typedef struct StdGlobalInterfaceTableImpl
74 {
75   const IGlobalInterfaceTableVtbl *lpVtbl;
76
77   ULONG ref;
78   struct StdGITEntry* firstEntry;
79   struct StdGITEntry* lastEntry;
80   ULONG nextCookie;
81   
82 } StdGlobalInterfaceTableImpl;
83
84 void* StdGlobalInterfaceTableInstance;
85
86 static CRITICAL_SECTION git_section;
87 static CRITICAL_SECTION_DEBUG critsect_debug =
88 {
89     0, 0, &git_section,
90     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
91       0, 0, { (DWORD_PTR)(__FILE__ ": global interface table") }
92 };
93 static CRITICAL_SECTION git_section = { &critsect_debug, -1, 0, 0, 0, 0 };
94
95
96 /** This destroys it again. It should revoke all the held interfaces first **/
97 static void StdGlobalInterfaceTable_Destroy(void* self)
98 {
99   TRACE("(%p)\n", self);
100   FIXME("Revoke held interfaces here\n");
101   
102   HeapFree(GetProcessHeap(), 0, self);
103   StdGlobalInterfaceTableInstance = NULL;
104 }
105
106 /***
107  * A helper function to traverse the list and find the entry that matches the cookie.
108  * Returns NULL if not found
109  */
110 static StdGITEntry*
111 StdGlobalInterfaceTable_FindEntry(IGlobalInterfaceTable* iface, DWORD cookie)
112 {
113   StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
114   StdGITEntry* e;
115
116   TRACE("iface=%p, cookie=0x%x\n", iface, (UINT)cookie);
117
118   EnterCriticalSection(&git_section);
119   e = self->firstEntry;
120   while (e != NULL) {
121     if (e->cookie == cookie) {
122       LeaveCriticalSection(&git_section);
123       return e;
124     }
125     e = e->next;
126   }
127   LeaveCriticalSection(&git_section);
128   
129   TRACE("Entry not found\n");
130   return NULL;
131 }
132
133 /***
134  * Here's the boring boilerplate stuff for IUnknown
135  */
136
137 static HRESULT WINAPI
138 StdGlobalInterfaceTable_QueryInterface(IGlobalInterfaceTable* iface,
139                REFIID riid, void** ppvObject)
140 {
141   /* Make sure silly coders can't crash us */
142   if (ppvObject == 0) return E_INVALIDARG;
143
144   *ppvObject = 0; /* assume we don't have the interface */
145
146   /* Do we implement that interface? */
147   if (IsEqualIID(&IID_IUnknown, riid) ||
148       IsEqualIID(&IID_IGlobalInterfaceTable, riid))
149     *ppvObject = iface;
150   else
151     return E_NOINTERFACE;
152
153   /* Now inc the refcount */
154   IGlobalInterfaceTable_AddRef(iface);
155   return S_OK;
156 }
157
158 static ULONG WINAPI
159 StdGlobalInterfaceTable_AddRef(IGlobalInterfaceTable* iface)
160 {
161   StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
162
163   /* InterlockedIncrement(&self->ref); */
164   return self->ref;
165 }
166
167 static ULONG WINAPI
168 StdGlobalInterfaceTable_Release(IGlobalInterfaceTable* iface)
169 {
170   StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
171
172   /* InterlockedDecrement(&self->ref); */
173   if (self->ref == 0) {
174     /* Hey ho, it's time to go, so long again 'till next weeks show! */
175     StdGlobalInterfaceTable_Destroy(self);
176     return 0;
177   }
178
179   return self->ref;
180 }
181
182 /***
183  * Now implement the actual IGlobalInterfaceTable interface
184  */
185
186 static HRESULT WINAPI
187 StdGlobalInterfaceTable_RegisterInterfaceInGlobal(
188                IGlobalInterfaceTable* iface, IUnknown* pUnk,
189                REFIID riid, DWORD* pdwCookie)
190 {
191   StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
192   IStream* stream = NULL;
193   HRESULT hres;
194   StdGITEntry* entry;
195   LARGE_INTEGER zero;
196
197   TRACE("iface=%p, pUnk=%p, riid=%s, pdwCookie=0x%p\n", iface, pUnk, debugstr_guid(riid), pdwCookie);
198
199   if (pUnk == NULL) return E_INVALIDARG;
200   
201   /* marshal the interface */
202   TRACE("About to marshal the interface\n");
203
204   hres = CreateStreamOnHGlobal(0, TRUE, &stream);
205   if (hres) return hres;
206   hres = CoMarshalInterface(stream, riid, pUnk, MSHCTX_INPROC, NULL, MSHLFLAGS_TABLESTRONG);
207   if (hres)
208   {
209     IStream_Release(stream);
210     return hres;
211   }
212
213   zero.QuadPart = 0;
214   IStream_Seek(stream, zero, SEEK_SET, NULL);
215
216   entry = HeapAlloc(GetProcessHeap(), 0, sizeof(StdGITEntry));
217   if (entry == NULL) return E_OUTOFMEMORY;
218
219   EnterCriticalSection(&git_section);
220   
221   entry->iid = *riid;
222   entry->stream = stream;
223   entry->cookie = self->nextCookie;
224   self->nextCookie++; /* inc the cookie count */
225
226   /* insert the new entry at the end of the list */
227   entry->next = NULL;
228   entry->prev = self->lastEntry;
229   if (entry->prev) entry->prev->next = entry;
230   else self->firstEntry = entry;
231   self->lastEntry = entry;
232
233   /* and return the cookie */
234   *pdwCookie = entry->cookie;
235   
236   LeaveCriticalSection(&git_section);
237   
238   TRACE("Cookie is 0x%x\n", entry->cookie);
239   return S_OK;
240 }
241
242 static HRESULT WINAPI
243 StdGlobalInterfaceTable_RevokeInterfaceFromGlobal(
244                IGlobalInterfaceTable* iface, DWORD dwCookie)
245 {
246   StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
247   StdGITEntry* entry;
248   HRESULT hr;
249
250   TRACE("iface=%p, dwCookie=0x%x\n", iface, (UINT)dwCookie);
251   
252   entry = StdGlobalInterfaceTable_FindEntry(iface, dwCookie);
253   if (entry == NULL) {
254     TRACE("Entry not found\n");
255     return E_INVALIDARG; /* not found */
256   }
257   
258   /* Free the stream */
259   hr = CoReleaseMarshalData(entry->stream);
260   if (hr != S_OK)
261   {
262     WARN("Failed to release marshal data, hr = 0x%08x\n", hr);
263     return hr;
264   }
265   IStream_Release(entry->stream);
266                     
267   /* chop entry out of the list, and free the memory */
268   EnterCriticalSection(&git_section);
269   if (entry->prev) entry->prev->next = entry->next;
270   else self->firstEntry = entry->next;
271   if (entry->next) entry->next->prev = entry->prev;
272   else self->lastEntry = entry->prev;
273   LeaveCriticalSection(&git_section);
274
275   HeapFree(GetProcessHeap(), 0, entry);
276   return S_OK;
277 }
278
279 static HRESULT WINAPI
280 StdGlobalInterfaceTable_GetInterfaceFromGlobal(
281                IGlobalInterfaceTable* iface, DWORD dwCookie,
282                REFIID riid, void **ppv)
283 {
284   StdGITEntry* entry;
285   HRESULT hres;
286   LARGE_INTEGER move;
287   LPUNKNOWN lpUnk;
288   
289   TRACE("dwCookie=0x%x, riid=%s, ppv=%p\n", dwCookie, debugstr_guid(riid), ppv);
290   
291   entry = StdGlobalInterfaceTable_FindEntry(iface, dwCookie);
292   if (entry == NULL) return E_INVALIDARG;
293
294   if (!IsEqualIID(&entry->iid, riid)) {
295     WARN("entry->iid (%s) != riid\n", debugstr_guid(&entry->iid));
296     return E_INVALIDARG;
297   }
298   TRACE("entry=%p\n", entry);
299   
300   /* unmarshal the interface */
301   hres = CoUnmarshalInterface(entry->stream, riid, ppv);
302   
303   /* rewind stream, in case it's used again */
304   move.u.LowPart = 0;
305   move.u.HighPart = 0;
306   IStream_Seek(entry->stream, move, STREAM_SEEK_SET, NULL);
307
308   if (hres) {
309     WARN("Failed to unmarshal stream\n");
310     return hres;
311   }
312
313   /* addref it */
314   lpUnk = *ppv;
315   IUnknown_AddRef(lpUnk);
316   TRACE("ppv=%p\n", *ppv);
317   return S_OK;
318 }
319
320 /* Classfactory definition - despite what MSDN says, some programs need this */
321
322 static HRESULT WINAPI
323 GITCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid, LPVOID *ppv)
324 {
325   *ppv = NULL;
326   if (IsEqualIID(riid,&IID_IUnknown) ||
327       IsEqualIID(riid,&IID_IGlobalInterfaceTable))
328   {
329     *ppv = (LPVOID)iface;
330     return S_OK;
331   }
332   return E_NOINTERFACE;
333 }
334
335 static ULONG WINAPI GITCF_AddRef(LPCLASSFACTORY iface)
336 {
337   return 2;
338 }
339
340 static ULONG WINAPI GITCF_Release(LPCLASSFACTORY iface)
341 {
342   return 1;
343 }
344
345 static HRESULT WINAPI
346 GITCF_CreateInstance(LPCLASSFACTORY iface, LPUNKNOWN pUnk,
347                      REFIID riid, LPVOID *ppv)
348 {
349   if (IsEqualIID(riid,&IID_IGlobalInterfaceTable)) {
350     if (StdGlobalInterfaceTableInstance == NULL) 
351       StdGlobalInterfaceTableInstance = StdGlobalInterfaceTable_Construct();
352     return IGlobalInterfaceTable_QueryInterface( (IGlobalInterfaceTable*) StdGlobalInterfaceTableInstance, riid, ppv);
353   }
354
355   FIXME("(%s), not supported.\n",debugstr_guid(riid));
356   return E_NOINTERFACE;
357 }
358
359 static HRESULT WINAPI GITCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
360 {
361     FIXME("(%d), stub!\n",fLock);
362     return S_OK;
363 }
364
365 static const IClassFactoryVtbl GITClassFactoryVtbl = {
366     GITCF_QueryInterface,
367     GITCF_AddRef,
368     GITCF_Release,
369     GITCF_CreateInstance,
370     GITCF_LockServer
371 };
372
373 static const IClassFactoryVtbl *PGITClassFactoryVtbl = &GITClassFactoryVtbl;
374
375 HRESULT StdGlobalInterfaceTable_GetFactory(LPVOID *ppv)
376 {
377   *ppv = &PGITClassFactoryVtbl;
378   TRACE("Returning GIT classfactory\n");
379   return S_OK;
380 }
381
382 /* Virtual function table */
383 static const IGlobalInterfaceTableVtbl StdGlobalInterfaceTableImpl_Vtbl =
384 {
385   StdGlobalInterfaceTable_QueryInterface,
386   StdGlobalInterfaceTable_AddRef,
387   StdGlobalInterfaceTable_Release,
388   StdGlobalInterfaceTable_RegisterInterfaceInGlobal,
389   StdGlobalInterfaceTable_RevokeInterfaceFromGlobal,
390   StdGlobalInterfaceTable_GetInterfaceFromGlobal
391 };
392
393 /** This function constructs the GIT. It should only be called once **/
394 void* StdGlobalInterfaceTable_Construct(void)
395 {
396   StdGlobalInterfaceTableImpl* newGIT;
397
398   newGIT = HeapAlloc(GetProcessHeap(), 0, sizeof(StdGlobalInterfaceTableImpl));
399   if (newGIT == 0) return newGIT;
400
401   newGIT->lpVtbl = &StdGlobalInterfaceTableImpl_Vtbl;
402   newGIT->ref = 1;      /* Initialise the reference count */
403   newGIT->firstEntry = NULL; /* we start with an empty table   */
404   newGIT->lastEntry  = NULL;
405   newGIT->nextCookie = 0xf100; /* that's where windows starts, so that's where we start */
406   TRACE("Created the GIT at %p\n", newGIT);
407
408   return (void*)newGIT;
409 }