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