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