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
25 # include <libxml/parser.h>
26 # include <libxml/xmlerror.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);
54 struct dispex_data_t {
57 func_info_t **name_table;
67 struct dispex_dynamic_data_t {
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,
85 &IID_IXMLDOMDocument2,
86 &IID_IXMLDOMDocumentFragment,
87 &IID_IXMLDOMDocumentType,
89 &IID_IXMLDOMEntityReference,
90 &IID_IXMLDOMImplementation,
91 &IID_IXMLDOMNamedNodeMap,
94 &IID_IXMLDOMParseError,
95 &IID_IXMLDOMProcessingInstruction,
96 &IID_IXMLDOMSchemaCollection,
97 &IID_IXMLDOMSelection,
100 &IID_IXMLDOMDocument,
101 &IID_IXMLHTTPRequest,
104 &IID_IVBSAXAttributes,
105 &IID_IVBSAXContentHandler,
106 &IID_IVBSAXDeclHandler,
107 &IID_IVBSAXDTDHandler,
108 &IID_IVBSAXEntityResolver,
109 &IID_IVBSAXErrorHandler,
110 &IID_IVBSAXLexicalHandler,
112 &IID_IVBSAXXMLFilter,
113 &IID_IVBSAXXMLReader,
115 &IID_IMXReaderControl,
117 &IID_IVBMXNamespaceManager
120 HRESULT get_typeinfo(enum tid_t tid, ITypeInfo **typeinfo)
127 hres = LoadRegTypeLib(&LIBID_MSXML2, 3, 0, LOCALE_SYSTEM_DEFAULT, &tl);
129 ERR("LoadRegTypeLib failed: %08x\n", hres);
133 if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
134 ITypeLib_Release(tl);
137 if(!typeinfos[tid]) {
140 hres = ITypeLib_GetTypeInfoOfGuid(typelib, tid_ids[tid], &ti);
142 ERR("GetTypeInfoOfGuid failed: %08x\n", hres);
146 if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), ti, NULL))
147 ITypeInfo_Release(ti);
150 *typeinfo = typeinfos[tid];
152 ITypeInfo_AddRef(typeinfos[tid]);
156 void release_typelib(void)
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);
176 for(i=0; i < sizeof(typeinfos)/sizeof(*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)
187 if(data->func_cnt && data->funcs[data->func_cnt-1].id == id)
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);
197 data->funcs[data->func_cnt].id = id;
198 data->funcs[data->func_cnt].tid = tid;
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;
222 TRACE("(%p)\n", This);
224 hres = get_typeinfo(This->data->disp_tid, &dti);
226 ERR("Could not get disp type info: %08x\n", hres);
230 data = heap_alloc(sizeof(dispex_data_t));
232 data->funcs = heap_alloc(size*sizeof(func_info_t));
233 list_add_tail(&dispex_data_list, &data->entry);
236 hres = get_typeinfo(*tid, &ti);
242 hres = ITypeInfo_GetFuncDesc(ti, i++, &funcdesc);
246 add_func_info(data, &size, *tid, funcdesc->memid, dti);
247 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
250 ITypeInfo_Release(ti);
254 if(!data->func_cnt) {
255 heap_free(data->funcs);
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);
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);
269 data->name_table = NULL;
272 ITypeInfo_Release(dti);
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)
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);
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);
365 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
368 for(i=0; i < cNames; i++) {
369 hres = IDispatchEx_GetDispID(&This->IDispatchEx_iface, rgszNames[i], 0, rgDispId+i);
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);
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);
406 max = data->func_cnt-1;
411 c = strcmpiW(data->name_table[n]->name, bstrName);
413 if((grfdex & fdexNameCaseSensitive) && strcmpW(data->name_table[n]->name, bstrName))
416 *pid = data->name_table[n]->id;
426 if(This->dynamic_data) {
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;
437 if(This->data->vtbl && This->data->vtbl->get_dispid) {
440 hres = This->data->vtbl->get_dispid(This->outer, bstrName, grfdex, pid);
441 if(hres != DISP_E_UNKNOWNNAME)
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;
453 dynamic_data = This->dynamic_data = heap_alloc_zero(sizeof(dispex_dynamic_data_t));
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));
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++;
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);
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");
506 if(is_dynamic_dispid(id)) {
507 DWORD idx = id - DISPID_DYNPROP_0;
510 if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
511 return DISP_E_UNKNOWNNAME;
513 var = &This->dynamic_data->props[idx].var;
516 case INVOKE_PROPERTYGET:
517 return VariantCopy(pvarRes, var);
518 case INVOKE_PROPERTYPUT:
520 return VariantCopy(var, pdp->rgvarg);
522 FIXME("unhandled wFlags %x\n", wFlags);
527 data = get_dispex_data(This);
532 max = data->func_cnt-1;
537 if(data->funcs[n].id == id)
540 if(data->funcs[n].id < id)
547 WARN("invalid id %x\n", id);
548 return DISP_E_UNKNOWNNAME;
551 hres = get_typeinfo(data->funcs[n].tid, &ti);
553 ERR("Could not get type info: %08x\n", hres);
557 hres = IUnknown_QueryInterface(This->outer, tid_ids[data->funcs[n].tid], (void**)&unk);
559 ERR("Could not get iface: %08x\n", hres);
563 hres = ITypeInfo_Invoke(ti, unk, id, wFlags, pdp, pvarRes, pei, &argerr);
565 ITypeInfo_Release(ti);
566 IUnknown_Release(unk);
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);
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);
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);
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);
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);
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);
612 static IDispatchExVtbl DispatchExVtbl = {
613 DispatchEx_QueryInterface,
616 DispatchEx_GetTypeInfoCount,
617 DispatchEx_GetTypeInfo,
618 DispatchEx_GetIDsOfNames,
620 DispatchEx_GetDispID,
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);
644 }else if (IsEqualGUID(&IID_IObjectIdentity, riid)) {
645 TRACE("IID_IObjectIdentity not supported returning NULL\n");
652 IUnknown_AddRef((IUnknown*)*ppv);
656 void init_dispex(DispatchEx *dispex, IUnknown *outer, dispex_static_data_t *data)
658 dispex->IDispatchEx_iface.lpVtbl = &DispatchExVtbl;
659 dispex->outer = outer;