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