quartz: Use proper alloc/free functions for COM objects.
[wine] / dlls / ole32 / bindctx.c
1 /*
2  *                            BindCtx implementation
3  *
4  *  Copyright 1999  Noomen Hamza
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdarg.h>
22 #include <string.h>
23
24 #define COBJMACROS
25
26 #include "winerror.h"
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winnls.h"
30 #include "objbase.h"
31
32 #include "wine/debug.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(ole);
35
36 /* represent the first size table and it's increment block size */
37 #define  BLOCK_TAB_SIZE 10
38 #define  MAX_TAB_SIZE   0xFFFFFFFF
39
40 /* data structure of the BindCtx table elements */
41 typedef struct BindCtxObject{
42
43     IUnknown*   pObj; /* point on a bound object */
44
45     LPOLESTR  pkeyObj; /* key associated to this bound object */
46
47     BYTE regType; /* registration type: 1 if RegisterObjectParam and 0 if RegisterObjectBound */
48
49 } BindCtxObject;
50
51 /* BindCtx data strucrture */
52 typedef struct BindCtxImpl{
53
54     const IBindCtxVtbl *lpVtbl; /* VTable relative to the IBindCtx interface.*/
55
56     LONG ref; /* reference counter for this object */
57
58     BindCtxObject* bindCtxTable; /* this is a table in which all bounded objects are stored*/
59     DWORD          bindCtxTableLastIndex;  /* first free index in the table */
60     DWORD          bindCtxTableSize;   /* size table */
61
62     BIND_OPTS2 bindOption2; /* a structure which contains the bind options*/
63
64 } BindCtxImpl;
65
66 /* IBindCtx prototype functions : */
67 static HRESULT WINAPI BindCtxImpl_ReleaseBoundObjects(IBindCtx*);
68 static HRESULT BindCtxImpl_GetObjectIndex(BindCtxImpl*, IUnknown*, LPOLESTR, DWORD *);
69
70 /*******************************************************************************
71  *        BindCtx_QueryInterface
72  *******************************************************************************/
73 static HRESULT WINAPI
74 BindCtxImpl_QueryInterface(IBindCtx* iface,REFIID riid,void** ppvObject)
75 {
76     BindCtxImpl *This = (BindCtxImpl *)iface;
77
78     TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
79
80     /* Perform a sanity check on the parameters.*/
81     if ( (This==0) || (ppvObject==0) )
82         return E_INVALIDARG;
83
84     /* Initialize the return parameter.*/
85     *ppvObject = 0;
86
87     /* Compare the riid with the interface IDs implemented by this object.*/
88     if (IsEqualIID(&IID_IUnknown, riid) ||
89         IsEqualIID(&IID_IBindCtx, riid))
90     {
91         *ppvObject = (IBindCtx*)This;
92         IBindCtx_AddRef(iface);
93         return S_OK;
94     }
95
96     return E_NOINTERFACE;
97 }
98
99 /******************************************************************************
100  *       BindCtx_AddRef
101  ******************************************************************************/
102 static ULONG WINAPI BindCtxImpl_AddRef(IBindCtx* iface)
103 {
104     BindCtxImpl *This = (BindCtxImpl *)iface;
105
106     TRACE("(%p)\n",This);
107
108     return InterlockedIncrement(&This->ref);
109 }
110
111 /******************************************************************************
112  *        BindCtx_Destroy    (local function)
113  *******************************************************************************/
114 static HRESULT BindCtxImpl_Destroy(BindCtxImpl* This)
115 {
116     TRACE("(%p)\n",This);
117
118     /* free the table space memory */
119     HeapFree(GetProcessHeap(),0,This->bindCtxTable);
120
121     /* free the bindctx structure */
122     HeapFree(GetProcessHeap(),0,This);
123
124     return S_OK;
125 }
126
127 /******************************************************************************
128  *        BindCtx_Release
129  ******************************************************************************/
130 static ULONG WINAPI BindCtxImpl_Release(IBindCtx* iface)
131 {
132     BindCtxImpl *This = (BindCtxImpl *)iface;
133     ULONG ref;
134
135     TRACE("(%p)\n",This);
136
137     ref = InterlockedDecrement(&This->ref);
138     if (ref == 0)
139     {
140         /* release all registered objects */
141         BindCtxImpl_ReleaseBoundObjects((IBindCtx*)This);
142
143         BindCtxImpl_Destroy(This);
144     }
145     return ref;
146 }
147
148
149 /******************************************************************************
150  *        BindCtx_RegisterObjectBound
151  ******************************************************************************/
152 static HRESULT WINAPI
153 BindCtxImpl_RegisterObjectBound(IBindCtx* iface,IUnknown* punk)
154 {
155     BindCtxImpl *This = (BindCtxImpl *)iface;
156     DWORD lastIndex=This->bindCtxTableLastIndex;
157
158     TRACE("(%p,%p)\n",This,punk);
159
160     if (punk==NULL)
161         return E_POINTER;
162
163     IUnknown_AddRef(punk);
164
165     /* put the object in the first free element in the table */
166     This->bindCtxTable[lastIndex].pObj = punk;
167     This->bindCtxTable[lastIndex].pkeyObj = NULL;
168     This->bindCtxTable[lastIndex].regType = 0;
169     lastIndex= ++This->bindCtxTableLastIndex;
170
171     if (lastIndex == This->bindCtxTableSize){ /* the table is full so it must be resized */
172
173         if (This->bindCtxTableSize > (MAX_TAB_SIZE-BLOCK_TAB_SIZE)){
174             FIXME("This->bindCtxTableSize: %d is out of data limite\n", This->bindCtxTableSize);
175             return E_FAIL;
176         }
177
178         This->bindCtxTableSize+=BLOCK_TAB_SIZE; /* new table size */
179
180         This->bindCtxTable = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,This->bindCtxTable,
181                                          This->bindCtxTableSize * sizeof(BindCtxObject));
182         if (!This->bindCtxTable)
183             return E_OUTOFMEMORY;
184     }
185     return S_OK;
186 }
187
188 /******************************************************************************
189  *        BindCtx_RevokeObjectBound
190  ******************************************************************************/
191 static HRESULT WINAPI
192 BindCtxImpl_RevokeObjectBound(IBindCtx* iface, IUnknown* punk)
193 {
194     DWORD index,j;
195
196     BindCtxImpl *This = (BindCtxImpl *)iface;
197
198     TRACE("(%p,%p)\n",This,punk);
199
200     /* check if the object was registered or not */
201     if (BindCtxImpl_GetObjectIndex(This,punk,NULL,&index)==S_FALSE)
202         return MK_E_NOTBOUND;
203
204     if(This->bindCtxTable[index].pObj)
205         IUnknown_Release(This->bindCtxTable[index].pObj);
206     HeapFree(GetProcessHeap(),0,This->bindCtxTable[index].pkeyObj);
207     
208     /* left-shift all elements in the right side of the current revoked object */
209     for(j=index; j<This->bindCtxTableLastIndex-1; j++)
210         This->bindCtxTable[j]= This->bindCtxTable[j+1];
211
212     This->bindCtxTableLastIndex--;
213
214     return S_OK;
215 }
216
217 /******************************************************************************
218  *        BindCtx_ReleaseBoundObjects
219  ******************************************************************************/
220 static HRESULT WINAPI
221 BindCtxImpl_ReleaseBoundObjects(IBindCtx* iface)
222 {
223     DWORD i;
224
225     BindCtxImpl *This = (BindCtxImpl *)iface;
226
227     TRACE("(%p)\n",This);
228
229     for(i=0;i<This->bindCtxTableLastIndex;i++)
230     {
231         if(This->bindCtxTable[i].pObj)
232             IUnknown_Release(This->bindCtxTable[i].pObj);
233         HeapFree(GetProcessHeap(),0,This->bindCtxTable[i].pkeyObj);
234     }
235     
236     This->bindCtxTableLastIndex = 0;
237
238     return S_OK;
239 }
240
241 /******************************************************************************
242  *        BindCtx_SetBindOptions
243  ******************************************************************************/
244 static HRESULT WINAPI
245 BindCtxImpl_SetBindOptions(IBindCtx* iface,BIND_OPTS *pbindopts)
246 {
247     BindCtxImpl *This = (BindCtxImpl *)iface;
248
249     TRACE("(%p,%p)\n",This,pbindopts);
250
251     if (pbindopts==NULL)
252         return E_POINTER;
253
254     if (pbindopts->cbStruct > sizeof(BIND_OPTS2))
255     {
256         WARN("invalid size\n");
257         return E_INVALIDARG; /* FIXME : not verified */
258     }
259     memcpy(&This->bindOption2, pbindopts, pbindopts->cbStruct);
260     return S_OK;
261 }
262
263 /******************************************************************************
264  *        BindCtx_GetBindOptions
265  ******************************************************************************/
266 static HRESULT WINAPI
267 BindCtxImpl_GetBindOptions(IBindCtx* iface,BIND_OPTS *pbindopts)
268 {
269     BindCtxImpl *This = (BindCtxImpl *)iface;
270
271     TRACE("(%p,%p)\n",This,pbindopts);
272
273     if (pbindopts==NULL)
274         return E_POINTER;
275
276     if (pbindopts->cbStruct > sizeof(BIND_OPTS2))
277         pbindopts->cbStruct = sizeof(BIND_OPTS2);
278
279     memcpy(pbindopts, &This->bindOption2, pbindopts->cbStruct);
280     return S_OK;
281 }
282
283 /******************************************************************************
284  *        BindCtx_GetRunningObjectTable
285  ******************************************************************************/
286 static HRESULT WINAPI
287 BindCtxImpl_GetRunningObjectTable(IBindCtx* iface,IRunningObjectTable** pprot)
288 {
289     HRESULT res;
290
291     BindCtxImpl *This = (BindCtxImpl *)iface;
292
293     TRACE("(%p,%p)\n",This,pprot);
294
295     if (pprot==NULL)
296         return E_POINTER;
297
298     res=GetRunningObjectTable(0, pprot);
299
300     return res;
301 }
302
303 /******************************************************************************
304  *        BindCtx_RegisterObjectParam
305  ******************************************************************************/
306 static HRESULT WINAPI
307 BindCtxImpl_RegisterObjectParam(IBindCtx* iface,LPOLESTR pszkey, IUnknown* punk)
308 {
309     DWORD index=0;
310     BindCtxImpl *This = (BindCtxImpl *)iface;
311
312     TRACE("(%p,%s,%p)\n",This,debugstr_w(pszkey),punk);
313
314     if (punk==NULL)
315         return E_INVALIDARG;
316
317     if (pszkey!=NULL && BindCtxImpl_GetObjectIndex(This,NULL,pszkey,&index)==S_OK)
318     {
319         TRACE("Overwriting existing key\n");
320         if(This->bindCtxTable[index].pObj!=NULL)
321             IUnknown_Release(This->bindCtxTable[index].pObj);
322         This->bindCtxTable[index].pObj=punk;
323         IUnknown_AddRef(punk);
324         return S_OK;
325     }
326     This->bindCtxTable[This->bindCtxTableLastIndex].pObj = punk;
327     This->bindCtxTable[This->bindCtxTableLastIndex].regType = 1;
328
329     if (pszkey==NULL)
330
331         This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj=NULL;
332
333     else
334     {
335
336         This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj=
337             HeapAlloc(GetProcessHeap(),0,(sizeof(WCHAR)*(1+lstrlenW(pszkey))));
338
339         if (This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj==NULL)
340             return E_OUTOFMEMORY;
341         lstrcpyW(This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj,pszkey);
342     }
343
344     This->bindCtxTableLastIndex++;
345
346     if (This->bindCtxTableLastIndex == This->bindCtxTableSize)
347     {
348         /* table is full ! must be resized */
349
350         This->bindCtxTableSize+=BLOCK_TAB_SIZE; /* new table size */
351         if (This->bindCtxTableSize > (MAX_TAB_SIZE-BLOCK_TAB_SIZE))
352         {
353             FIXME("This->bindCtxTableSize: %d is out of data limite\n", This->bindCtxTableSize);
354             return E_FAIL;
355         }
356         This->bindCtxTable = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,This->bindCtxTable,
357                                          This->bindCtxTableSize * sizeof(BindCtxObject));
358         if (!This->bindCtxTable)
359             return E_OUTOFMEMORY;
360     }
361     IUnknown_AddRef(punk);
362     return S_OK;
363 }
364
365 /******************************************************************************
366  *        BindCtx_GetObjectParam
367  ******************************************************************************/
368 static HRESULT WINAPI
369 BindCtxImpl_GetObjectParam(IBindCtx* iface,LPOLESTR pszkey, IUnknown** punk)
370 {
371     DWORD index;
372     BindCtxImpl *This = (BindCtxImpl *)iface;
373
374     TRACE("(%p,%s,%p)\n",This,debugstr_w(pszkey),punk);
375
376     if (punk==NULL)
377         return E_POINTER;
378
379     *punk=0;
380
381     if (BindCtxImpl_GetObjectIndex(This,NULL,pszkey,&index)==S_FALSE)
382         return E_FAIL;
383
384     IUnknown_AddRef(This->bindCtxTable[index].pObj);
385
386     *punk = This->bindCtxTable[index].pObj;
387
388     return S_OK;
389 }
390
391 /******************************************************************************
392  *        BindCtx_RevokeObjectParam
393  ******************************************************************************/
394 static HRESULT WINAPI
395 BindCtxImpl_RevokeObjectParam(IBindCtx* iface,LPOLESTR ppenum)
396 {
397     DWORD index,j;
398
399     BindCtxImpl *This = (BindCtxImpl *)iface;
400
401     TRACE("(%p,%s)\n",This,debugstr_w(ppenum));
402
403     if (BindCtxImpl_GetObjectIndex(This,NULL,ppenum,&index)==S_FALSE)
404         return E_FAIL;
405
406     /* release the object if it's found */
407     if(This->bindCtxTable[index].pObj)
408         IUnknown_Release(This->bindCtxTable[index].pObj);
409     HeapFree(GetProcessHeap(),0,This->bindCtxTable[index].pkeyObj);
410     
411     /* remove the object from the table with a left-shifting of all objects in the right side */
412     for(j=index; j<This->bindCtxTableLastIndex-1; j++)
413         This->bindCtxTable[j]= This->bindCtxTable[j+1];
414
415     This->bindCtxTableLastIndex--;
416
417     return S_OK;
418 }
419
420 /******************************************************************************
421  *        BindCtx_EnumObjectParam
422  ******************************************************************************/
423 static HRESULT WINAPI
424 BindCtxImpl_EnumObjectParam(IBindCtx* iface,IEnumString** pszkey)
425 {
426     TRACE("(%p,%p)\n",iface,pszkey);
427
428     *pszkey = NULL;
429
430     /* not implemented in native either */
431     return E_NOTIMPL;
432 }
433
434 /********************************************************************************
435  *        GetObjectIndex (local function)
436  ********************************************************************************/
437 static HRESULT BindCtxImpl_GetObjectIndex(BindCtxImpl* This,
438                                           IUnknown* punk,
439                                           LPOLESTR pszkey,
440                                           DWORD *index)
441 {
442
443     DWORD i;
444     BYTE found=0;
445
446     TRACE("(%p,%p,%p,%p)\n",This,punk,pszkey,index);
447
448     if (punk==NULL)
449         /* search object identified by a register key */
450         for(i=0; ( (i<This->bindCtxTableLastIndex) && (!found));i++)
451         {
452             if(This->bindCtxTable[i].regType==1){
453
454                 if ( ( (This->bindCtxTable[i].pkeyObj==NULL) && (pszkey==NULL) ) ||
455                      ( (This->bindCtxTable[i].pkeyObj!=NULL) &&
456                        (pszkey!=NULL) &&
457                        (lstrcmpW(This->bindCtxTable[i].pkeyObj,pszkey)==0)
458                      )
459                    )
460
461                     found=1;
462             }
463         }
464     else
465         /* search object identified by a moniker*/
466         for(i=0; ( (i<This->bindCtxTableLastIndex) && (!found));i++)
467             if(This->bindCtxTable[i].pObj==punk)
468                 found=1;
469
470     if (index != NULL)
471         *index=i-1;
472
473     if (found)
474         return S_OK;
475     TRACE("key not found\n");
476     return S_FALSE;
477 }
478
479 /* Virtual function table for the BindCtx class. */
480 static const IBindCtxVtbl VT_BindCtxImpl =
481 {
482     BindCtxImpl_QueryInterface,
483     BindCtxImpl_AddRef,
484     BindCtxImpl_Release,
485     BindCtxImpl_RegisterObjectBound,
486     BindCtxImpl_RevokeObjectBound,
487     BindCtxImpl_ReleaseBoundObjects,
488     BindCtxImpl_SetBindOptions,
489     BindCtxImpl_GetBindOptions,
490     BindCtxImpl_GetRunningObjectTable,
491     BindCtxImpl_RegisterObjectParam,
492     BindCtxImpl_GetObjectParam,
493     BindCtxImpl_EnumObjectParam,
494     BindCtxImpl_RevokeObjectParam
495 };
496
497 /******************************************************************************
498  *         BindCtx_Construct (local function)
499  *******************************************************************************/
500 static HRESULT BindCtxImpl_Construct(BindCtxImpl* This)
501 {
502     TRACE("(%p)\n",This);
503
504     /* Initialize the virtual function table.*/
505     This->lpVtbl       = &VT_BindCtxImpl;
506     This->ref          = 0;
507
508     /* Initialize the BIND_OPTS2 structure */
509     This->bindOption2.cbStruct  = sizeof(BIND_OPTS2);
510     This->bindOption2.grfFlags = 0;
511     This->bindOption2.grfMode = STGM_READWRITE;
512     This->bindOption2.dwTickCountDeadline = 0;
513
514     This->bindOption2.dwTrackFlags = 0;
515     This->bindOption2.dwClassContext = CLSCTX_SERVER;
516     This->bindOption2.locale = GetThreadLocale();
517     This->bindOption2.pServerInfo = 0;
518
519     /* Initialize the bindctx table */
520     This->bindCtxTableSize=BLOCK_TAB_SIZE;
521     This->bindCtxTableLastIndex=0;
522     This->bindCtxTable = HeapAlloc(GetProcessHeap(), 0,
523                                 This->bindCtxTableSize*sizeof(BindCtxObject));
524
525     if (This->bindCtxTable==NULL)
526         return E_OUTOFMEMORY;
527
528     return S_OK;
529 }
530
531 /******************************************************************************
532  *        CreateBindCtx (OLE32.@)
533  ******************************************************************************/
534 HRESULT WINAPI CreateBindCtx(DWORD reserved, LPBC * ppbc)
535 {
536     BindCtxImpl* newBindCtx = 0;
537     HRESULT hr;
538     IID riid=IID_IBindCtx;
539
540     TRACE("(%d,%p)\n",reserved,ppbc);
541
542     *ppbc = NULL;
543
544     if (reserved != 0)
545     {
546         ERR("reserved should be 0, not 0x%x\n", reserved);
547         return E_INVALIDARG;
548     }
549
550     newBindCtx = HeapAlloc(GetProcessHeap(), 0, sizeof(BindCtxImpl));
551     if (newBindCtx == 0)
552         return E_OUTOFMEMORY;
553
554     hr = BindCtxImpl_Construct(newBindCtx);
555     if (FAILED(hr))
556     {
557         HeapFree(GetProcessHeap(),0,newBindCtx);
558         return hr;
559     }
560
561     hr = BindCtxImpl_QueryInterface((IBindCtx*)newBindCtx,&riid,(void**)ppbc);
562
563     return hr;
564 }
565
566 HRESULT WINAPI BindMoniker(LPMONIKER pmk, DWORD grfOpt, REFIID riid, LPVOID * ppvResult)
567 {
568     HRESULT res;
569     IBindCtx * pbc;
570
571     TRACE("(%p, %x, %s, %p)\n", pmk, grfOpt, debugstr_guid(riid), ppvResult);
572
573     res = CreateBindCtx(grfOpt, &pbc);
574     if (SUCCEEDED(res))
575         res = IMoniker_BindToObject(pmk, pbc, NULL, riid, ppvResult);
576     return res;
577 }