ntdll/tests: Wait for async I/O to complete when the result is STATUS_PENDING.
[wine.git] / dlls / msxml3 / dispex.c
blobfa9fbb85293d578af7c7e516e4b9d2c341534f84
1 /*
2 * Copyright 2008 Jacek Caban for CodeWeavers
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.
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.
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
19 #define COBJMACROS
21 #include "config.h"
23 #include <stdarg.h>
24 #ifdef HAVE_LIBXML2
25 # include <libxml/parser.h>
26 # include <libxml/xmlerror.h>
27 #endif
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winuser.h"
32 #include "winnls.h"
33 #include "ole2.h"
34 #include "msxml6.h"
35 #include "wininet.h"
36 #include "urlmon.h"
37 #include "winreg.h"
38 #include "shlwapi.h"
40 #include "wine/debug.h"
41 #include "wine/list.h"
42 #include "wine/unicode.h"
44 #include "msxml_private.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
48 typedef struct {
49 DISPID id;
50 BSTR name;
51 enum tid_t tid;
52 } func_info_t;
54 struct dispex_data_t {
55 DWORD func_cnt;
56 func_info_t *funcs;
57 func_info_t **name_table;
59 struct list entry;
62 typedef struct {
63 VARIANT var;
64 LPWSTR name;
65 } dynamic_prop_t;
67 struct dispex_dynamic_data_t {
68 DWORD buf_size;
69 DWORD prop_cnt;
70 dynamic_prop_t *props;
73 #define DISPID_DYNPROP_0 0x50000000
74 #define DISPID_DYNPROP_MAX 0x5fffffff
76 static struct list dispex_data_list = LIST_INIT(dispex_data_list);
77 static ITypeLib *typelib;
78 static ITypeInfo *typeinfos[LAST_tid];
80 static REFIID tid_ids[] = {
81 &IID_IXMLDOMAttribute,
82 &IID_IXMLDOMCDATASection,
83 &IID_IXMLDOMComment,
84 &IID_IXMLDOMDocument,
85 &IID_IXMLDOMDocument2,
86 &IID_IXMLDOMDocumentFragment,
87 &IID_IXMLDOMDocumentType,
88 &IID_IXMLDOMElement,
89 &IID_IXMLDOMEntityReference,
90 &IID_IXMLDOMImplementation,
91 &IID_IXMLDOMNamedNodeMap,
92 &IID_IXMLDOMNode,
93 &IID_IXMLDOMNodeList,
94 &IID_IXMLDOMParseError,
95 &IID_IXMLDOMProcessingInstruction,
96 &IID_IXMLDOMSchemaCollection,
97 &IID_IXMLDOMSelection,
98 &IID_IXMLDOMText,
99 &IID_IXMLElement,
100 &IID_IXMLDOMDocument,
101 &IID_IXMLHTTPRequest,
102 &IID_IXSLProcessor,
103 &IID_IXSLTemplate,
104 &IID_IVBSAXAttributes,
105 &IID_IVBSAXContentHandler,
106 &IID_IVBSAXDeclHandler,
107 &IID_IVBSAXDTDHandler,
108 &IID_IVBSAXEntityResolver,
109 &IID_IVBSAXErrorHandler,
110 &IID_IVBSAXLexicalHandler,
111 &IID_IVBSAXLocator,
112 &IID_IVBSAXXMLFilter,
113 &IID_IVBSAXXMLReader,
114 &IID_IMXAttributes,
115 &IID_IMXReaderControl,
116 &IID_IMXWriter,
117 &IID_IVBMXNamespaceManager
120 HRESULT get_typeinfo(enum tid_t tid, ITypeInfo **typeinfo)
122 HRESULT hres;
124 if(!typelib) {
125 ITypeLib *tl;
127 hres = LoadRegTypeLib(&LIBID_MSXML2, 3, 0, LOCALE_SYSTEM_DEFAULT, &tl);
128 if(FAILED(hres)) {
129 ERR("LoadRegTypeLib failed: %08x\n", hres);
130 return hres;
133 if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
134 ITypeLib_Release(tl);
137 if(!typeinfos[tid]) {
138 ITypeInfo *ti;
140 hres = ITypeLib_GetTypeInfoOfGuid(typelib, tid_ids[tid], &ti);
141 if(FAILED(hres)) {
142 ERR("GetTypeInfoOfGuid failed: %08x\n", hres);
143 return hres;
146 if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), ti, NULL))
147 ITypeInfo_Release(ti);
150 *typeinfo = typeinfos[tid];
152 ITypeInfo_AddRef(typeinfos[tid]);
153 return S_OK;
156 void release_typelib(void)
158 dispex_data_t *iter;
159 unsigned i;
161 while(!list_empty(&dispex_data_list)) {
162 iter = LIST_ENTRY(list_head(&dispex_data_list), dispex_data_t, entry);
163 list_remove(&iter->entry);
165 for(i=0; i < iter->func_cnt; i++)
166 SysFreeString(iter->funcs[i].name);
168 heap_free(iter->funcs);
169 heap_free(iter->name_table);
170 heap_free(iter);
173 if(!typelib)
174 return;
176 for(i=0; i < sizeof(typeinfos)/sizeof(*typeinfos); i++)
177 if(typeinfos[i])
178 ITypeInfo_Release(typeinfos[i]);
180 ITypeLib_Release(typelib);
183 static void add_func_info(dispex_data_t *data, DWORD *size, tid_t tid, DISPID id, ITypeInfo *dti)
185 HRESULT hres;
187 if(data->func_cnt && data->funcs[data->func_cnt-1].id == id)
188 return;
190 if(data->func_cnt == *size)
191 data->funcs = heap_realloc(data->funcs, (*size <<= 1)*sizeof(func_info_t));
193 hres = ITypeInfo_GetDocumentation(dti, id, &data->funcs[data->func_cnt].name, NULL, NULL, NULL);
194 if(FAILED(hres))
195 return;
197 data->funcs[data->func_cnt].id = id;
198 data->funcs[data->func_cnt].tid = tid;
200 data->func_cnt++;
203 static int dispid_cmp(const void *p1, const void *p2)
205 return ((const func_info_t*)p1)->id - ((const func_info_t*)p2)->id;
208 static int func_name_cmp(const void *p1, const void *p2)
210 return strcmpiW((*(func_info_t* const*)p1)->name, (*(func_info_t* const*)p2)->name);
213 static dispex_data_t *preprocess_dispex_data(DispatchEx *This)
215 const tid_t *tid = This->data->iface_tids;
216 FUNCDESC *funcdesc;
217 dispex_data_t *data;
218 DWORD size = 16, i;
219 ITypeInfo *ti, *dti;
220 HRESULT hres;
222 TRACE("(%p)\n", This);
224 hres = get_typeinfo(This->data->disp_tid, &dti);
225 if(FAILED(hres)) {
226 ERR("Could not get disp type info: %08x\n", hres);
227 return NULL;
230 data = heap_alloc(sizeof(dispex_data_t));
231 data->func_cnt = 0;
232 data->funcs = heap_alloc(size*sizeof(func_info_t));
233 list_add_tail(&dispex_data_list, &data->entry);
235 while(*tid) {
236 hres = get_typeinfo(*tid, &ti);
237 if(FAILED(hres))
238 break;
240 i=0;
241 while(1) {
242 hres = ITypeInfo_GetFuncDesc(ti, i++, &funcdesc);
243 if(FAILED(hres))
244 break;
246 add_func_info(data, &size, *tid, funcdesc->memid, dti);
247 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
250 ITypeInfo_Release(ti);
251 tid++;
254 if(!data->func_cnt) {
255 heap_free(data->funcs);
256 data->funcs = NULL;
257 }else if(data->func_cnt != size) {
258 data->funcs = heap_realloc(data->funcs, data->func_cnt * sizeof(func_info_t));
261 qsort(data->funcs, data->func_cnt, sizeof(func_info_t), dispid_cmp);
263 if(data->funcs) {
264 data->name_table = heap_alloc(data->func_cnt * sizeof(func_info_t*));
265 for(i=0; i < data->func_cnt; i++)
266 data->name_table[i] = data->funcs+i;
267 qsort(data->name_table, data->func_cnt, sizeof(func_info_t*), func_name_cmp);
268 }else {
269 data->name_table = NULL;
272 ITypeInfo_Release(dti);
273 return data;
276 static CRITICAL_SECTION cs_dispex_static_data;
277 static CRITICAL_SECTION_DEBUG cs_dispex_static_data_dbg =
279 0, 0, &cs_dispex_static_data,
280 { &cs_dispex_static_data_dbg.ProcessLocksList, &cs_dispex_static_data_dbg.ProcessLocksList },
281 0, 0, { (DWORD_PTR)(__FILE__ ": dispex_static_data") }
283 static CRITICAL_SECTION cs_dispex_static_data = { &cs_dispex_static_data_dbg, -1, 0, 0, 0, 0 };
286 static dispex_data_t *get_dispex_data(DispatchEx *This)
288 if(This->data->data)
289 return This->data->data;
291 EnterCriticalSection(&cs_dispex_static_data);
293 if(!This->data->data)
294 This->data->data = preprocess_dispex_data(This);
296 LeaveCriticalSection(&cs_dispex_static_data);
298 return This->data->data;
301 static inline BOOL is_custom_dispid(DISPID id)
303 return MSXML_DISPID_CUSTOM_MIN <= id && id <= MSXML_DISPID_CUSTOM_MAX;
306 static inline BOOL is_dynamic_dispid(DISPID id)
308 return DISPID_DYNPROP_0 <= id && id <= DISPID_DYNPROP_MAX;
311 static inline DispatchEx *impl_from_IDispatchEx(IDispatchEx *iface)
313 return CONTAINING_RECORD(iface, DispatchEx, IDispatchEx_iface);
316 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
318 DispatchEx *This = impl_from_IDispatchEx(iface);
320 return IUnknown_QueryInterface(This->outer, riid, ppv);
323 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
325 DispatchEx *This = impl_from_IDispatchEx(iface);
327 return IUnknown_AddRef(This->outer);
330 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
332 DispatchEx *This = impl_from_IDispatchEx(iface);
334 return IUnknown_Release(This->outer);
337 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
339 DispatchEx *This = impl_from_IDispatchEx(iface);
341 TRACE("(%p)->(%p)\n", This, pctinfo);
343 *pctinfo = 1;
344 return S_OK;
347 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
348 LCID lcid, ITypeInfo **ppTInfo)
350 DispatchEx *This = impl_from_IDispatchEx(iface);
352 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
354 return get_typeinfo(This->data->disp_tid, ppTInfo);
357 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
358 LPOLESTR *rgszNames, UINT cNames,
359 LCID lcid, DISPID *rgDispId)
361 DispatchEx *This = impl_from_IDispatchEx(iface);
362 UINT i;
363 HRESULT hres;
365 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
366 lcid, rgDispId);
368 for(i=0; i < cNames; i++) {
369 hres = IDispatchEx_GetDispID(&This->IDispatchEx_iface, rgszNames[i], 0, rgDispId+i);
370 if(FAILED(hres))
371 return hres;
374 return S_OK;
377 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
378 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
379 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
381 DispatchEx *This = impl_from_IDispatchEx(iface);
383 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
384 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
386 return IDispatchEx_InvokeEx(&This->IDispatchEx_iface, dispIdMember, lcid, wFlags,
387 pDispParams, pVarResult, pExcepInfo, NULL);
390 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
392 DispatchEx *This = impl_from_IDispatchEx(iface);
393 dispex_data_t *data;
394 int min, max, n, c;
396 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
398 if(grfdex & ~(fdexNameCaseSensitive|fdexNameEnsure|fdexNameImplicit))
399 FIXME("Unsupported grfdex %x\n", grfdex);
401 data = get_dispex_data(This);
402 if(!data)
403 return E_FAIL;
405 min = 0;
406 max = data->func_cnt-1;
408 while(min <= max) {
409 n = (min+max)/2;
411 c = strcmpiW(data->name_table[n]->name, bstrName);
412 if(!c) {
413 if((grfdex & fdexNameCaseSensitive) && strcmpW(data->name_table[n]->name, bstrName))
414 break;
416 *pid = data->name_table[n]->id;
417 return S_OK;
420 if(c > 0)
421 max = n-1;
422 else
423 min = n+1;
426 if(This->dynamic_data) {
427 unsigned i;
429 for(i=0; i < This->dynamic_data->prop_cnt; i++) {
430 if(!strcmpW(This->dynamic_data->props[i].name, bstrName)) {
431 *pid = DISPID_DYNPROP_0 + i;
432 return S_OK;
437 if(This->data->vtbl && This->data->vtbl->get_dispid) {
438 HRESULT hres;
440 hres = This->data->vtbl->get_dispid(This->outer, bstrName, grfdex, pid);
441 if(hres != DISP_E_UNKNOWNNAME)
442 return hres;
445 if(grfdex & fdexNameEnsure) {
446 dispex_dynamic_data_t *dynamic_data;
448 TRACE("creating dynamic prop %s\n", debugstr_w(bstrName));
450 if(This->dynamic_data) {
451 dynamic_data = This->dynamic_data;
452 }else {
453 dynamic_data = This->dynamic_data = heap_alloc_zero(sizeof(dispex_dynamic_data_t));
454 if(!dynamic_data)
455 return E_OUTOFMEMORY;
458 if(!dynamic_data->buf_size) {
459 dynamic_data->props = heap_alloc(sizeof(dynamic_prop_t)*4);
460 if(!dynamic_data->props)
461 return E_OUTOFMEMORY;
462 dynamic_data->buf_size = 4;
463 }else if(dynamic_data->buf_size == dynamic_data->prop_cnt) {
464 dynamic_prop_t *new_props;
466 new_props = heap_realloc(dynamic_data->props, sizeof(dynamic_prop_t)*(dynamic_data->buf_size<<1));
467 if(!new_props)
468 return E_OUTOFMEMORY;
470 dynamic_data->props = new_props;
471 dynamic_data->buf_size <<= 1;
474 dynamic_data->props[dynamic_data->prop_cnt].name = heap_strdupW(bstrName);
475 VariantInit(&dynamic_data->props[dynamic_data->prop_cnt].var);
476 *pid = DISPID_DYNPROP_0 + dynamic_data->prop_cnt++;
478 return S_OK;
481 TRACE("not found %s\n", debugstr_w(bstrName));
482 return DISP_E_UNKNOWNNAME;
485 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
486 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
488 DispatchEx *This = impl_from_IDispatchEx(iface);
489 IUnknown *unk;
490 ITypeInfo *ti;
491 dispex_data_t *data;
492 UINT argerr=0;
493 int min, max, n;
494 HRESULT hres;
496 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
498 if(is_custom_dispid(id) && This->data->vtbl && This->data->vtbl->invoke)
499 return This->data->vtbl->invoke(This->outer, id, lcid, wFlags, pdp, pvarRes, pei);
501 if(wFlags == DISPATCH_CONSTRUCT) {
502 FIXME("DISPATCH_CONSTRUCT not implemented\n");
503 return E_NOTIMPL;
506 if(is_dynamic_dispid(id)) {
507 DWORD idx = id - DISPID_DYNPROP_0;
508 VARIANT *var;
510 if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
511 return DISP_E_UNKNOWNNAME;
513 var = &This->dynamic_data->props[idx].var;
515 switch(wFlags) {
516 case INVOKE_PROPERTYGET:
517 return VariantCopy(pvarRes, var);
518 case INVOKE_PROPERTYPUT:
519 VariantClear(var);
520 return VariantCopy(var, pdp->rgvarg);
521 default:
522 FIXME("unhandled wFlags %x\n", wFlags);
523 return E_NOTIMPL;
527 data = get_dispex_data(This);
528 if(!data)
529 return E_FAIL;
531 min = 0;
532 max = data->func_cnt-1;
534 while(min <= max) {
535 n = (min+max)/2;
537 if(data->funcs[n].id == id)
538 break;
540 if(data->funcs[n].id < id)
541 min = n+1;
542 else
543 max = n-1;
546 if(min > max) {
547 WARN("invalid id %x\n", id);
548 return DISP_E_UNKNOWNNAME;
551 hres = get_typeinfo(data->funcs[n].tid, &ti);
552 if(FAILED(hres)) {
553 ERR("Could not get type info: %08x\n", hres);
554 return hres;
557 hres = IUnknown_QueryInterface(This->outer, tid_ids[data->funcs[n].tid], (void**)&unk);
558 if(FAILED(hres)) {
559 ERR("Could not get iface: %08x\n", hres);
560 return E_FAIL;
563 hres = ITypeInfo_Invoke(ti, unk, id, wFlags, pdp, pvarRes, pei, &argerr);
565 ITypeInfo_Release(ti);
566 IUnknown_Release(unk);
567 return hres;
570 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
572 DispatchEx *This = impl_from_IDispatchEx(iface);
573 TRACE("Not implemented in native msxml3 (%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
574 return E_NOTIMPL;
577 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
579 DispatchEx *This = impl_from_IDispatchEx(iface);
580 TRACE("Not implemented in native msxml3 (%p)->(%x)\n", This, id);
581 return E_NOTIMPL;
584 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
586 DispatchEx *This = impl_from_IDispatchEx(iface);
587 TRACE("Not implemented in native msxml3 (%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
588 return E_NOTIMPL;
591 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
593 DispatchEx *This = impl_from_IDispatchEx(iface);
594 TRACE("Not implemented in native msxml3 (%p)->(%x %p)\n", This, id, pbstrName);
595 return E_NOTIMPL;
598 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
600 DispatchEx *This = impl_from_IDispatchEx(iface);
601 TRACE(" Not implemented in native msxml3 (%p)->(%x %x %p)\n", This, grfdex, id, pid);
602 return E_NOTIMPL;
605 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
607 DispatchEx *This = impl_from_IDispatchEx(iface);
608 TRACE("Not implemented in native msxml3 (%p)->(%p)\n", This, ppunk);
609 return E_NOTIMPL;
612 static IDispatchExVtbl DispatchExVtbl = {
613 DispatchEx_QueryInterface,
614 DispatchEx_AddRef,
615 DispatchEx_Release,
616 DispatchEx_GetTypeInfoCount,
617 DispatchEx_GetTypeInfo,
618 DispatchEx_GetIDsOfNames,
619 DispatchEx_Invoke,
620 DispatchEx_GetDispID,
621 DispatchEx_InvokeEx,
622 DispatchEx_DeleteMemberByName,
623 DispatchEx_DeleteMemberByDispID,
624 DispatchEx_GetMemberProperties,
625 DispatchEx_GetMemberName,
626 DispatchEx_GetNextDispID,
627 DispatchEx_GetNameSpaceParent
630 BOOL dispex_query_interface(DispatchEx *This, REFIID riid, void **ppv)
632 static const IID IID_UndocumentedScriptIface =
633 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa0}};
635 if(IsEqualGUID(&IID_IDispatch, riid)) {
636 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
637 *ppv = &This->IDispatchEx_iface;
638 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
639 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
640 *ppv = &This->IDispatchEx_iface;
641 }else if(IsEqualGUID(&IID_UndocumentedScriptIface, riid)) {
642 TRACE("(%p)->(IID_UndocumentedScriptIface %p) returning NULL\n", This, ppv);
643 *ppv = NULL;
644 }else if (IsEqualGUID(&IID_IObjectIdentity, riid)) {
645 TRACE("IID_IObjectIdentity not supported returning NULL\n");
646 *ppv = NULL;
647 }else {
648 return FALSE;
651 if(*ppv)
652 IUnknown_AddRef((IUnknown*)*ppv);
653 return TRUE;
656 void init_dispex(DispatchEx *dispex, IUnknown *outer, dispex_static_data_t *data)
658 dispex->IDispatchEx_iface.lpVtbl = &DispatchExVtbl;
659 dispex->outer = outer;
660 dispex->data = data;