git.c 10.7 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * Implementation of the StdGlobalInterfaceTable object
 *
 * The GlobalInterfaceTable (GIT) object is used to marshal interfaces between
 * threading apartments (contexts). When you want to pass an interface but not
 * as a parameter, it wouldn't get marshalled automatically, so you can use this
 * object to insert the interface into a table, and you get back a cookie.
 * Then when it's retrieved, it'll be unmarshalled into the right apartment.
 *
Mike Hearn's avatar
Mike Hearn committed
10
 * Copyright 2003 Mike Hearn <mike@theoretic.com>
11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 30 31 32
#define COBJMACROS
#define NONAMELESSUNION
#define NONAMELESSSTRUCT

33
#include "windef.h"
34
#include "winbase.h"
35
#include "winuser.h"
36 37 38 39 40 41
#include "objbase.h"
#include "ole2.h"
#include "winerror.h"

#include "compobj_private.h" 

42
#include "wine/list.h"
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(ole);

/****************************************************************************
 * StdGlobalInterfaceTable definition
 *
 * This class implements IGlobalInterfaceTable and is a process-wide singleton
 * used for marshalling interfaces between threading apartments using cookies.
 */

/* Each entry in the linked list of GIT entries */
typedef struct StdGITEntry
{
  DWORD cookie;
58
  IID iid;         /* IID of the interface */
59 60
  IStream* stream; /* Holds the marshalled interface */

61
  struct list entry;
62 63 64 65 66
} StdGITEntry;

/* Class data */
typedef struct StdGlobalInterfaceTableImpl
{
67
  IGlobalInterfaceTable IGlobalInterfaceTable_iface;
68 69

  ULONG ref;
70
  struct list list;
71 72 73 74
  ULONG nextCookie;
  
} StdGlobalInterfaceTableImpl;

75 76
void* StdGlobalInterfaceTableInstance;

77 78 79 80 81
static CRITICAL_SECTION git_section;
static CRITICAL_SECTION_DEBUG critsect_debug =
{
    0, 0, &git_section,
    { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
82
      0, 0, { (DWORD_PTR)(__FILE__ ": global interface table") }
83 84 85 86
};
static CRITICAL_SECTION git_section = { &critsect_debug, -1, 0, 0, 0, 0 };


87 88 89 90 91
static inline StdGlobalInterfaceTableImpl *impl_from_IGlobalInterfaceTable(IGlobalInterfaceTable *iface)
{
  return CONTAINING_RECORD(iface, StdGlobalInterfaceTableImpl, IGlobalInterfaceTable_iface);
}

92
/** This destroys it again. It should revoke all the held interfaces first **/
93
static void StdGlobalInterfaceTable_Destroy(void* This)
94
{
95
  TRACE("(%p)\n", This);
96 97
  FIXME("Revoke held interfaces here\n");
  
98
  HeapFree(GetProcessHeap(), 0, This);
99
  StdGlobalInterfaceTableInstance = NULL;
100 101 102 103
}

/***
 * A helper function to traverse the list and find the entry that matches the cookie.
104
 * Returns NULL if not found. Must be called inside git_section critical section.
105
 */
106 107
static StdGITEntry* StdGlobalInterfaceTable_FindEntry(StdGlobalInterfaceTableImpl* This,
                DWORD cookie)
108
{
109 110
  StdGITEntry* e;

111
  TRACE("This=%p, cookie=0x%x\n", This, cookie);
112

113
  LIST_FOR_EACH_ENTRY(e, &This->list, StdGITEntry, entry) {
114
    if (e->cookie == cookie)
115
      return e;
116
  }
117

118
  TRACE("Entry not found\n");
119 120 121 122 123 124 125
  return NULL;
}

/***
 * Here's the boring boilerplate stuff for IUnknown
 */

126 127 128 129
static HRESULT WINAPI
StdGlobalInterfaceTable_QueryInterface(IGlobalInterfaceTable* iface,
               REFIID riid, void** ppvObject)
{
130 131 132 133 134 135
  /* Make sure silly coders can't crash us */
  if (ppvObject == 0) return E_INVALIDARG;

  *ppvObject = 0; /* assume we don't have the interface */

  /* Do we implement that interface? */
136 137 138 139 140
  if (IsEqualIID(&IID_IUnknown, riid) ||
      IsEqualIID(&IID_IGlobalInterfaceTable, riid))
    *ppvObject = iface;
  else
    return E_NOINTERFACE;
141 142

  /* Now inc the refcount */
143
  IGlobalInterfaceTable_AddRef(iface);
144 145 146
  return S_OK;
}

147 148 149
static ULONG WINAPI
StdGlobalInterfaceTable_AddRef(IGlobalInterfaceTable* iface)
{
150
  StdGlobalInterfaceTableImpl* const This = impl_from_IGlobalInterfaceTable(iface);
151

152 153
  /* InterlockedIncrement(&This->ref); */
  return This->ref;
154 155
}

156 157 158
static ULONG WINAPI
StdGlobalInterfaceTable_Release(IGlobalInterfaceTable* iface)
{
159
  StdGlobalInterfaceTableImpl* const This = impl_from_IGlobalInterfaceTable(iface);
160

161 162
  /* InterlockedDecrement(&This->ref); */
  if (This->ref == 0) {
163
    /* Hey ho, it's time to go, so long again 'till next weeks show! */
164
    StdGlobalInterfaceTable_Destroy(This);
165 166 167
    return 0;
  }

168
  return This->ref;
169 170 171 172 173 174
}

/***
 * Now implement the actual IGlobalInterfaceTable interface
 */

175 176 177 178 179
static HRESULT WINAPI
StdGlobalInterfaceTable_RegisterInterfaceInGlobal(
               IGlobalInterfaceTable* iface, IUnknown* pUnk,
               REFIID riid, DWORD* pdwCookie)
{
180
  StdGlobalInterfaceTableImpl* const This = impl_from_IGlobalInterfaceTable(iface);
181 182 183
  IStream* stream = NULL;
  HRESULT hres;
  StdGITEntry* entry;
184
  LARGE_INTEGER zero;
185

186
  TRACE("iface=%p, pUnk=%p, riid=%s, pdwCookie=0x%p\n", iface, pUnk, debugstr_guid(riid), pdwCookie);
187 188 189 190

  if (pUnk == NULL) return E_INVALIDARG;
  
  /* marshal the interface */
191
  TRACE("About to marshal the interface\n");
192 193

  hres = CreateStreamOnHGlobal(0, TRUE, &stream);
194
  if (hres != S_OK) return hres;
195
  hres = CoMarshalInterface(stream, riid, pUnk, MSHCTX_INPROC, NULL, MSHLFLAGS_TABLESTRONG);
196
  if (hres != S_OK)
197 198 199 200 201
  {
    IStream_Release(stream);
    return hres;
  }

202
  zero.QuadPart = 0;
203
  IStream_Seek(stream, zero, STREAM_SEEK_SET, NULL);
204

205 206
  entry = HeapAlloc(GetProcessHeap(), 0, sizeof(StdGITEntry));
  if (entry == NULL) return E_OUTOFMEMORY;
207 208

  EnterCriticalSection(&git_section);
209 210 211
  
  entry->iid = *riid;
  entry->stream = stream;
212 213
  entry->cookie = This->nextCookie;
  This->nextCookie++; /* inc the cookie count */
214 215

  /* insert the new entry at the end of the list */
216
  list_add_tail(&This->list, &entry->entry);
217 218 219

  /* and return the cookie */
  *pdwCookie = entry->cookie;
220 221 222
  
  LeaveCriticalSection(&git_section);
  
223
  TRACE("Cookie is 0x%x\n", entry->cookie);
224 225 226
  return S_OK;
}

227 228 229 230
static HRESULT WINAPI
StdGlobalInterfaceTable_RevokeInterfaceFromGlobal(
               IGlobalInterfaceTable* iface, DWORD dwCookie)
{
231
  StdGlobalInterfaceTableImpl* This = impl_from_IGlobalInterfaceTable(iface);
232
  StdGITEntry* entry;
233
  HRESULT hr;
234

235
  TRACE("iface=%p, dwCookie=0x%x\n", iface, dwCookie);
236 237 238

  EnterCriticalSection(&git_section);

239
  entry = StdGlobalInterfaceTable_FindEntry(This, dwCookie);
240 241
  if (entry == NULL) {
    TRACE("Entry not found\n");
242
    LeaveCriticalSection(&git_section);
243 244
    return E_INVALIDARG; /* not found */
  }
245 246 247 248

  list_remove(&entry->entry);

  LeaveCriticalSection(&git_section);
249 250
  
  /* Free the stream */
251 252 253
  hr = CoReleaseMarshalData(entry->stream);
  if (hr != S_OK)
  {
254
    WARN("Failed to release marshal data, hr = 0x%08x\n", hr);
255 256
    return hr;
  }
257 258
  IStream_Release(entry->stream);
		    
259 260 261 262
  HeapFree(GetProcessHeap(), 0, entry);
  return S_OK;
}

263 264 265 266 267
static HRESULT WINAPI
StdGlobalInterfaceTable_GetInterfaceFromGlobal(
               IGlobalInterfaceTable* iface, DWORD dwCookie,
               REFIID riid, void **ppv)
{
268
  StdGlobalInterfaceTableImpl* This = impl_from_IGlobalInterfaceTable(iface);
269 270
  StdGITEntry* entry;
  HRESULT hres;
271 272
  IStream *stream;

273
  TRACE("dwCookie=0x%x, riid=%s, ppv=%p\n", dwCookie, debugstr_guid(riid), ppv);
274 275 276

  EnterCriticalSection(&git_section);

277
  entry = StdGlobalInterfaceTable_FindEntry(This, dwCookie);
278
  if (entry == NULL) {
279
    WARN("Entry for cookie 0x%x not found\n", dwCookie);
280 281 282
    LeaveCriticalSection(&git_section);
    return E_INVALIDARG;
  }
283

284
  TRACE("entry=%p\n", entry);
285 286 287 288 289

  hres = IStream_Clone(entry->stream, &stream);

  LeaveCriticalSection(&git_section);

290
  if (hres != S_OK) {
291 292 293 294
    WARN("Failed to clone stream with error 0x%08x\n", hres);
    return hres;
  }

295
  /* unmarshal the interface */
296 297
  hres = CoUnmarshalInterface(stream, riid, ppv);
  IStream_Release(stream);
298

299 300 301 302 303
  if (hres) {
    WARN("Failed to unmarshal stream\n");
    return hres;
  }

304
  TRACE("ppv=%p\n", *ppv);
305 306
  return S_OK;
}
307 308 309

/* Classfactory definition - despite what MSDN says, some programs need this */

310 311 312
static HRESULT WINAPI
GITCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid, LPVOID *ppv)
{
313
  *ppv = NULL;
314 315 316
  if (IsEqualIID(riid,&IID_IUnknown) ||
      IsEqualIID(riid,&IID_IGlobalInterfaceTable))
  {
317
    *ppv = iface;
318 319 320 321 322
    return S_OK;
  }
  return E_NOINTERFACE;
}

323 324 325 326 327 328 329 330 331 332 333 334 335 336
static ULONG WINAPI GITCF_AddRef(LPCLASSFACTORY iface)
{
  return 2;
}

static ULONG WINAPI GITCF_Release(LPCLASSFACTORY iface)
{
  return 1;
}

static HRESULT WINAPI
GITCF_CreateInstance(LPCLASSFACTORY iface, LPUNKNOWN pUnk,
                     REFIID riid, LPVOID *ppv)
{
337 338 339 340 341 342 343 344 345 346
  if (IsEqualIID(riid,&IID_IGlobalInterfaceTable)) {
    if (StdGlobalInterfaceTableInstance == NULL) 
      StdGlobalInterfaceTableInstance = StdGlobalInterfaceTable_Construct();
    return IGlobalInterfaceTable_QueryInterface( (IGlobalInterfaceTable*) StdGlobalInterfaceTableInstance, riid, ppv);
  }

  FIXME("(%s), not supported.\n",debugstr_guid(riid));
  return E_NOINTERFACE;
}

347 348
static HRESULT WINAPI GITCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
{
349 350 351 352
    FIXME("(%d), stub!\n",fLock);
    return S_OK;
}

353
static const IClassFactoryVtbl GITClassFactoryVtbl = {
354 355 356 357 358 359
    GITCF_QueryInterface,
    GITCF_AddRef,
    GITCF_Release,
    GITCF_CreateInstance,
    GITCF_LockServer
};
360

361
static const IClassFactoryVtbl *PGITClassFactoryVtbl = &GITClassFactoryVtbl;
362

363 364
HRESULT StdGlobalInterfaceTable_GetFactory(LPVOID *ppv)
{
365 366 367 368
  *ppv = &PGITClassFactoryVtbl;
  TRACE("Returning GIT classfactory\n");
  return S_OK;
}
369 370

/* Virtual function table */
371
static const IGlobalInterfaceTableVtbl StdGlobalInterfaceTableImpl_Vtbl =
372 373 374 375 376 377 378 379 380 381
{
  StdGlobalInterfaceTable_QueryInterface,
  StdGlobalInterfaceTable_AddRef,
  StdGlobalInterfaceTable_Release,
  StdGlobalInterfaceTable_RegisterInterfaceInGlobal,
  StdGlobalInterfaceTable_RevokeInterfaceFromGlobal,
  StdGlobalInterfaceTable_GetInterfaceFromGlobal
};

/** This function constructs the GIT. It should only be called once **/
382
void* StdGlobalInterfaceTable_Construct(void)
383 384 385 386 387 388
{
  StdGlobalInterfaceTableImpl* newGIT;

  newGIT = HeapAlloc(GetProcessHeap(), 0, sizeof(StdGlobalInterfaceTableImpl));
  if (newGIT == 0) return newGIT;

389
  newGIT->IGlobalInterfaceTable_iface.lpVtbl = &StdGlobalInterfaceTableImpl_Vtbl;
390
  newGIT->ref = 1;      /* Initialise the reference count */
391
  list_init(&newGIT->list);
392 393 394 395 396
  newGIT->nextCookie = 0xf100; /* that's where windows starts, so that's where we start */
  TRACE("Created the GIT at %p\n", newGIT);

  return (void*)newGIT;
}