msxml3: Correct test.
[wine] / dlls / msxml3 / dispex.c
1 /*
2  * Copyright 2008 Jacek Caban for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #define COBJMACROS
20
21 #include "config.h"
22
23 #include <stdarg.h>
24 #include <assert.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "winnls.h"
29 #include "ole2.h"
30 #include "msxml2.h"
31 #include "wininet.h"
32 #include "urlmon.h"
33 #include "winreg.h"
34 #include "shlwapi.h"
35
36 #include "wine/debug.h"
37 #include "wine/list.h"
38 #include "wine/unicode.h"
39
40 #include "msxml_private.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
43
44 #define DISPATCHEX(x)  ((IDispatchEx*)  &(x)->lpIDispatchExVtbl)
45
46 typedef struct {
47     DISPID id;
48     BSTR name;
49     enum tid_t tid;
50 } func_info_t;
51
52 struct dispex_data_t {
53     DWORD func_cnt;
54     func_info_t *funcs;
55     func_info_t **name_table;
56
57     struct list entry;
58 };
59
60 typedef struct {
61     VARIANT var;
62     LPWSTR name;
63 } dynamic_prop_t;
64
65 struct dispex_dynamic_data_t {
66     DWORD buf_size;
67     DWORD prop_cnt;
68     dynamic_prop_t *props;
69 };
70
71 #define DISPID_DYNPROP_0    0x50000000
72 #define DISPID_DYNPROP_MAX  0x5fffffff
73
74 static struct list dispex_data_list = LIST_INIT(dispex_data_list);
75 static ITypeLib *typelib;
76 static ITypeInfo *typeinfos[LAST_tid];
77
78 static REFIID tid_ids[] = {
79     &IID_IXMLDOMAttribute,
80     &IID_IXMLDOMCDATASection,
81     &IID_IXMLDOMComment,
82     &IID_IXMLDOMDocument,
83     &IID_IXMLDOMDocument2,
84     &IID_IXMLDOMDocumentFragment,
85     &IID_IXMLDOMElement,
86     &IID_IXMLDOMEntityReference,
87     &IID_IXMLDOMImplementation,
88     &IID_IXMLDOMNamedNodeMap,
89     &IID_IXMLDOMNode,
90     &IID_IXMLDOMNodeList,
91     &IID_IXMLDOMParseError,
92     &IID_IXMLDOMProcessingInstruction,
93     &IID_IXMLDOMSchemaCollection,
94     &IID_IXMLDOMText,
95     &IID_IXMLElement,
96     &IID_IXMLDOMDocument,
97     &IID_IVBSAXAttributes,
98     &IID_IVBSAXContentHandler,
99     &IID_IVBSAXDeclHandler,
100     &IID_IVBSAXDTDHandler,
101     &IID_IVBSAXEntityResolver,
102     &IID_IVBSAXErrorHandler,
103     &IID_IVBSAXLexicalHandler,
104     &IID_IVBSAXLocator,
105     &IID_IVBSAXXMLFilter,
106     &IID_IVBSAXXMLReader,
107     &IID_IMXAttributes,
108     &IID_IMXReaderControl,
109     &IID_IMXWriter,
110 };
111
112 HRESULT get_typeinfo(enum tid_t tid, ITypeInfo **typeinfo)
113 {
114     HRESULT hres;
115
116     if(!typelib) {
117         ITypeLib *tl;
118
119         hres = LoadRegTypeLib(&LIBID_MSXML2, 3, 0, LOCALE_SYSTEM_DEFAULT, &tl);
120         if(FAILED(hres)) {
121             ERR("LoadRegTypeLib failed: %08x\n", hres);
122             return hres;
123         }
124
125         if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
126             ITypeLib_Release(tl);
127     }
128
129     if(!typeinfos[tid]) {
130         ITypeInfo *typeinfo;
131
132         hres = ITypeLib_GetTypeInfoOfGuid(typelib, tid_ids[tid], &typeinfo);
133         if(FAILED(hres)) {
134             ERR("GetTypeInfoOfGuid failed: %08x\n", hres);
135             return hres;
136         }
137
138         if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), typeinfo, NULL))
139             ITypeInfo_Release(typeinfo);
140     }
141
142     *typeinfo = typeinfos[tid];
143
144     ITypeInfo_AddRef(typeinfos[tid]);
145     return S_OK;
146 }
147
148 void release_typelib(void)
149 {
150     dispex_data_t *iter;
151     unsigned i;
152
153     while(!list_empty(&dispex_data_list)) {
154         iter = LIST_ENTRY(list_head(&dispex_data_list), dispex_data_t, entry);
155         list_remove(&iter->entry);
156
157         for(i=0; i < iter->func_cnt; i++)
158             SysFreeString(iter->funcs[i].name);
159
160         heap_free(iter->funcs);
161         heap_free(iter->name_table);
162         heap_free(iter);
163     }
164
165     if(!typelib)
166         return;
167
168     for(i=0; i < sizeof(typeinfos)/sizeof(*typeinfos); i++)
169         if(typeinfos[i])
170             ITypeInfo_Release(typeinfos[i]);
171
172     ITypeLib_Release(typelib);
173 }
174
175 static void add_func_info(dispex_data_t *data, DWORD *size, tid_t tid, DISPID id, ITypeInfo *dti)
176 {
177     HRESULT hres;
178
179     if(data->func_cnt && data->funcs[data->func_cnt-1].id == id)
180         return;
181
182     if(data->func_cnt == *size)
183         data->funcs = heap_realloc(data->funcs, (*size <<= 1)*sizeof(func_info_t));
184
185     hres = ITypeInfo_GetDocumentation(dti, id, &data->funcs[data->func_cnt].name, NULL, NULL, NULL);
186     if(FAILED(hres))
187         return;
188
189     data->funcs[data->func_cnt].id = id;
190     data->funcs[data->func_cnt].tid = tid;
191
192     data->func_cnt++;
193 }
194
195 static int dispid_cmp(const void *p1, const void *p2)
196 {
197     return ((func_info_t*)p1)->id - ((func_info_t*)p2)->id;
198 }
199
200 static int func_name_cmp(const void *p1, const void *p2)
201 {
202     return strcmpiW((*(func_info_t**)p1)->name, (*(func_info_t**)p2)->name);
203 }
204
205 static dispex_data_t *preprocess_dispex_data(DispatchEx *This)
206 {
207     const tid_t *tid = This->data->iface_tids;
208     FUNCDESC *funcdesc;
209     dispex_data_t *data;
210     DWORD size = 16, i;
211     ITypeInfo *ti, *dti;
212     HRESULT hres;
213
214     TRACE("(%p)\n", This);
215
216     hres = get_typeinfo(This->data->disp_tid, &dti);
217     if(FAILED(hres)) {
218         ERR("Could not get disp type info: %08x\n", hres);
219         return NULL;
220     }
221
222     data = heap_alloc(sizeof(dispex_data_t));
223     data->func_cnt = 0;
224     data->funcs = heap_alloc(size*sizeof(func_info_t));
225     list_add_tail(&dispex_data_list, &data->entry);
226
227     while(*tid) {
228         hres = get_typeinfo(*tid, &ti);
229         if(FAILED(hres))
230             break;
231
232         i=0;
233         while(1) {
234             hres = ITypeInfo_GetFuncDesc(ti, i++, &funcdesc);
235             if(FAILED(hres))
236                 break;
237
238             add_func_info(data, &size, *tid, funcdesc->memid, dti);
239             ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
240         }
241
242         ITypeInfo_Release(ti);
243         tid++;
244     }
245
246     if(!data->func_cnt) {
247         heap_free(data->funcs);
248         data->funcs = NULL;
249     }else if(data->func_cnt != size) {
250         data->funcs = heap_realloc(data->funcs, data->func_cnt * sizeof(func_info_t));
251     }
252
253     qsort(data->funcs, data->func_cnt, sizeof(func_info_t), dispid_cmp);
254
255     if(data->funcs) {
256         data->name_table = heap_alloc(data->func_cnt * sizeof(func_info_t*));
257         for(i=0; i < data->func_cnt; i++)
258             data->name_table[i] = data->funcs+i;
259         qsort(data->name_table, data->func_cnt, sizeof(func_info_t*), func_name_cmp);
260     }else {
261         data->name_table = NULL;
262     }
263
264     ITypeInfo_Release(dti);
265     return data;
266 }
267
268 static CRITICAL_SECTION cs_dispex_static_data;
269 static CRITICAL_SECTION_DEBUG cs_dispex_static_data_dbg =
270 {
271     0, 0, &cs_dispex_static_data,
272     { &cs_dispex_static_data_dbg.ProcessLocksList, &cs_dispex_static_data_dbg.ProcessLocksList },
273       0, 0, { (DWORD_PTR)(__FILE__ ": dispex_static_data") }
274 };
275 static CRITICAL_SECTION cs_dispex_static_data = { &cs_dispex_static_data_dbg, -1, 0, 0, 0, 0 };
276
277
278 static dispex_data_t *get_dispex_data(DispatchEx *This)
279 {
280     if(This->data->data)
281         return This->data->data;
282
283     EnterCriticalSection(&cs_dispex_static_data);
284
285     if(!This->data->data)
286         This->data->data = preprocess_dispex_data(This);
287
288     LeaveCriticalSection(&cs_dispex_static_data);
289
290     return This->data->data;
291 }
292
293 static inline BOOL is_custom_dispid(DISPID id)
294 {
295     return MSXML_DISPID_CUSTOM_MIN <= id && id <= MSXML_DISPID_CUSTOM_MAX;
296 }
297
298 static inline BOOL is_dynamic_dispid(DISPID id)
299 {
300     return DISPID_DYNPROP_0 <= id && id <= DISPID_DYNPROP_MAX;
301 }
302
303 static inline DispatchEx *impl_from_IDispatchEx(IDispatchEx *iface)
304 {
305     return (DispatchEx*)((char*)iface - FIELD_OFFSET(DispatchEx, lpIDispatchExVtbl));
306 }
307
308 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
309 {
310     DispatchEx *This = impl_from_IDispatchEx(iface);
311
312     return IUnknown_QueryInterface(This->outer, riid, ppv);
313 }
314
315 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
316 {
317     DispatchEx *This = impl_from_IDispatchEx(iface);
318
319     return IUnknown_AddRef(This->outer);
320 }
321
322 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
323 {
324     DispatchEx *This = impl_from_IDispatchEx(iface);
325
326     return IUnknown_Release(This->outer);
327 }
328
329 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
330 {
331     DispatchEx *This = impl_from_IDispatchEx(iface);
332
333     TRACE("(%p)->(%p)\n", This, pctinfo);
334
335     *pctinfo = 1;
336     return S_OK;
337 }
338
339 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
340                                               LCID lcid, ITypeInfo **ppTInfo)
341 {
342     DispatchEx *This = impl_from_IDispatchEx(iface);
343
344     TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
345
346     return get_typeinfo(This->data->disp_tid, ppTInfo);
347 }
348
349 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
350                                                 LPOLESTR *rgszNames, UINT cNames,
351                                                 LCID lcid, DISPID *rgDispId)
352 {
353     DispatchEx *This = impl_from_IDispatchEx(iface);
354     UINT i;
355     HRESULT hres;
356
357     TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
358           lcid, rgDispId);
359
360     for(i=0; i < cNames; i++) {
361         hres = IDispatchEx_GetDispID(DISPATCHEX(This), rgszNames[i], 0, rgDispId+i);
362         if(FAILED(hres))
363             return hres;
364     }
365
366     return S_OK;
367 }
368
369 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
370                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
371                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
372 {
373     DispatchEx *This = impl_from_IDispatchEx(iface);
374
375     TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
376           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
377
378     return IDispatchEx_InvokeEx(DISPATCHEX(This), dispIdMember, lcid, wFlags,
379                                 pDispParams, pVarResult, pExcepInfo, NULL);
380 }
381
382 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
383 {
384     DispatchEx *This = impl_from_IDispatchEx(iface);
385     dispex_data_t *data;
386     int min, max, n, c;
387
388     TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
389
390     if(grfdex & ~(fdexNameCaseSensitive|fdexNameEnsure|fdexNameImplicit))
391         FIXME("Unsupported grfdex %x\n", grfdex);
392
393     data = get_dispex_data(This);
394     if(!data)
395         return E_FAIL;
396
397     min = 0;
398     max = data->func_cnt-1;
399
400     while(min <= max) {
401         n = (min+max)/2;
402
403         c = strcmpiW(data->name_table[n]->name, bstrName);
404         if(!c) {
405             if((grfdex & fdexNameCaseSensitive) && strcmpW(data->name_table[n]->name, bstrName))
406                 break;
407
408             *pid = data->name_table[n]->id;
409             return S_OK;
410         }
411
412         if(c > 0)
413             max = n-1;
414         else
415             min = n+1;
416     }
417
418     if(This->dynamic_data) {
419         unsigned i;
420
421         for(i=0; i < This->dynamic_data->prop_cnt; i++) {
422             if(!strcmpW(This->dynamic_data->props[i].name, bstrName)) {
423                 *pid = DISPID_DYNPROP_0 + i;
424                 return S_OK;
425             }
426         }
427     }
428
429     if(This->data->vtbl && This->data->vtbl->get_dispid) {
430         HRESULT hres;
431
432         hres = This->data->vtbl->get_dispid(This->outer, bstrName, grfdex, pid);
433         if(hres != DISP_E_UNKNOWNNAME)
434             return hres;
435     }
436
437     if(grfdex & fdexNameEnsure) {
438         TRACE("creating dynamic prop %s\n", debugstr_w(bstrName));
439
440         if(!This->dynamic_data) {
441             This->dynamic_data = heap_alloc_zero(sizeof(dispex_dynamic_data_t));
442             This->dynamic_data->props = heap_alloc(This->dynamic_data->buf_size = 4);
443         }else if(This->dynamic_data->buf_size == This->dynamic_data->prop_cnt) {
444             This->dynamic_data->props = heap_realloc(This->dynamic_data->props, This->dynamic_data->buf_size<<=1);
445         }
446
447         This->dynamic_data->props[This->dynamic_data->prop_cnt].name = heap_strdupW(bstrName);
448         VariantInit(&This->dynamic_data->props[This->dynamic_data->prop_cnt].var);
449         *pid = DISPID_DYNPROP_0 + This->dynamic_data->prop_cnt++;
450
451         return S_OK;
452     }
453
454     TRACE("not found %s\n", debugstr_w(bstrName));
455     return DISP_E_UNKNOWNNAME;
456 }
457
458 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
459         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
460 {
461     DispatchEx *This = impl_from_IDispatchEx(iface);
462     IUnknown *unk;
463     ITypeInfo *ti;
464     dispex_data_t *data;
465     UINT argerr=0;
466     int min, max, n;
467     HRESULT hres;
468
469     TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
470
471     if(is_custom_dispid(id) && This->data->vtbl && This->data->vtbl->invoke)
472         return This->data->vtbl->invoke(This->outer, id, lcid, wFlags, pdp, pvarRes, pei);
473
474     if(wFlags == DISPATCH_CONSTRUCT) {
475         FIXME("DISPATCH_CONSTRUCT not implemented\n");
476         return E_NOTIMPL;
477     }
478
479     if(is_dynamic_dispid(id)) {
480         DWORD idx = id - DISPID_DYNPROP_0;
481         VARIANT *var;
482
483         if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
484             return DISP_E_UNKNOWNNAME;
485
486         var = &This->dynamic_data->props[idx].var;
487
488         switch(wFlags) {
489         case INVOKE_PROPERTYGET:
490             return VariantCopy(pvarRes, var);
491         case INVOKE_PROPERTYPUT:
492             VariantClear(var);
493             return VariantCopy(var, pdp->rgvarg);
494         default:
495             FIXME("unhandled wFlags %x\n", wFlags);
496             return E_NOTIMPL;
497         }
498     }
499
500     data = get_dispex_data(This);
501     if(!data)
502         return E_FAIL;
503
504     min = 0;
505     max = data->func_cnt-1;
506
507     while(min <= max) {
508         n = (min+max)/2;
509
510         if(data->funcs[n].id == id)
511             break;
512
513         if(data->funcs[n].id < id)
514             min = n+1;
515         else
516             max = n-1;
517     }
518
519     if(min > max) {
520         WARN("invalid id %x\n", id);
521         return DISP_E_UNKNOWNNAME;
522     }
523
524     hres = get_typeinfo(data->funcs[n].tid, &ti);
525     if(FAILED(hres)) {
526         ERR("Could not get type info: %08x\n", hres);
527         return hres;
528     }
529
530     hres = IUnknown_QueryInterface(This->outer, tid_ids[data->funcs[n].tid], (void**)&unk);
531     if(FAILED(hres)) {
532         ERR("Could not get iface: %08x\n", hres);
533         return E_FAIL;
534     }
535
536     hres = ITypeInfo_Invoke(ti, unk, id, wFlags, pdp, pvarRes, pei, &argerr);
537
538     ITypeInfo_Release(ti);
539     IUnknown_Release(unk);
540     return hres;
541 }
542
543 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
544 {
545     DispatchEx *This = impl_from_IDispatchEx(iface);
546     TRACE("Not implemented in native msxml3 (%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
547     return E_NOTIMPL;
548 }
549
550 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
551 {
552     DispatchEx *This = impl_from_IDispatchEx(iface);
553     TRACE("Not implemented in native msxml3 (%p)->(%x)\n", This, id);
554     return E_NOTIMPL;
555 }
556
557 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
558 {
559     DispatchEx *This = impl_from_IDispatchEx(iface);
560     TRACE("Not implemented in native msxml3 (%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
561     return E_NOTIMPL;
562 }
563
564 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
565 {
566     DispatchEx *This = impl_from_IDispatchEx(iface);
567     TRACE("Not implemented in native msxml3 (%p)->(%x %p)\n", This, id, pbstrName);
568     return E_NOTIMPL;
569 }
570
571 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
572 {
573     DispatchEx *This = impl_from_IDispatchEx(iface);
574     TRACE(" Not implemented in native msxml3 (%p)->(%x %x %p)\n", This, grfdex, id, pid);
575     return E_NOTIMPL;
576 }
577
578 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
579 {
580     DispatchEx *This = impl_from_IDispatchEx(iface);
581     TRACE("Not implemented in native msxml3 (%p)->(%p)\n", This, ppunk);
582     return E_NOTIMPL;
583 }
584
585 static IDispatchExVtbl DispatchExVtbl = {
586     DispatchEx_QueryInterface,
587     DispatchEx_AddRef,
588     DispatchEx_Release,
589     DispatchEx_GetTypeInfoCount,
590     DispatchEx_GetTypeInfo,
591     DispatchEx_GetIDsOfNames,
592     DispatchEx_Invoke,
593     DispatchEx_GetDispID,
594     DispatchEx_InvokeEx,
595     DispatchEx_DeleteMemberByName,
596     DispatchEx_DeleteMemberByDispID,
597     DispatchEx_GetMemberProperties,
598     DispatchEx_GetMemberName,
599     DispatchEx_GetNextDispID,
600     DispatchEx_GetNameSpaceParent
601 };
602
603 BOOL dispex_query_interface(DispatchEx *This, REFIID riid, void **ppv)
604 {
605     static const IID IID_UndocumentedScriptIface =
606         {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa0}};
607
608     if(IsEqualGUID(&IID_IDispatch, riid)) {
609         TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
610         *ppv = DISPATCHEX(This);
611     }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
612         TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
613         *ppv = DISPATCHEX(This);
614     }else if(IsEqualGUID(&IID_UndocumentedScriptIface, riid)) {
615         TRACE("(%p)->(IID_UndocumentedScriptIface %p) returning NULL\n", This, ppv);
616         *ppv = NULL;
617     }else {
618         return FALSE;
619     }
620
621     if(*ppv)
622         IUnknown_AddRef((IUnknown*)*ppv);
623     return TRUE;
624 }
625
626 void init_dispex(DispatchEx *dispex, IUnknown *outer, dispex_static_data_t *data)
627 {
628     dispex->lpIDispatchExVtbl = &DispatchExVtbl;
629     dispex->outer = outer;
630     dispex->data = data;
631 }