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,
119 HRESULT get_typeinfo(enum tid_t tid, ITypeInfo **typeinfo)
126 hres = LoadRegTypeLib(&LIBID_MSXML2, 3, 0, LOCALE_SYSTEM_DEFAULT, &tl);
128 ERR("LoadRegTypeLib failed: %08x\n", hres);
132 if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
133 ITypeLib_Release(tl);
136 if(!typeinfos[tid]) {
139 hres = ITypeLib_GetTypeInfoOfGuid(typelib, tid_ids[tid], &typeinfo);
141 ERR("GetTypeInfoOfGuid failed: %08x\n", hres);
145 if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), typeinfo, NULL))
146 ITypeInfo_Release(typeinfo);
149 *typeinfo = typeinfos[tid];
151 ITypeInfo_AddRef(typeinfos[tid]);
155 void release_typelib(void)
160 while(!list_empty(&dispex_data_list)) {
161 iter = LIST_ENTRY(list_head(&dispex_data_list), dispex_data_t, entry);
162 list_remove(&iter->entry);
164 for(i=0; i < iter->func_cnt; i++)
165 SysFreeString(iter->funcs[i].name);
167 heap_free(iter->funcs);
168 heap_free(iter->name_table);
175 for(i=0; i < sizeof(typeinfos)/sizeof(*typeinfos); i++)
177 ITypeInfo_Release(typeinfos[i]);
179 ITypeLib_Release(typelib);
182 static void add_func_info(dispex_data_t *data, DWORD *size, tid_t tid, DISPID id, ITypeInfo *dti)
186 if(data->func_cnt && data->funcs[data->func_cnt-1].id == id)
189 if(data->func_cnt == *size)
190 data->funcs = heap_realloc(data->funcs, (*size <<= 1)*sizeof(func_info_t));
192 hres = ITypeInfo_GetDocumentation(dti, id, &data->funcs[data->func_cnt].name, NULL, NULL, NULL);
196 data->funcs[data->func_cnt].id = id;
197 data->funcs[data->func_cnt].tid = tid;
202 static int dispid_cmp(const void *p1, const void *p2)
204 return ((const func_info_t*)p1)->id - ((const func_info_t*)p2)->id;
207 static int func_name_cmp(const void *p1, const void *p2)
209 return strcmpiW((*(func_info_t* const*)p1)->name, (*(func_info_t* const*)p2)->name);
212 static dispex_data_t *preprocess_dispex_data(DispatchEx *This)
214 const tid_t *tid = This->data->iface_tids;
221 TRACE("(%p)\n", This);
223 hres = get_typeinfo(This->data->disp_tid, &dti);
225 ERR("Could not get disp type info: %08x\n", hres);
229 data = heap_alloc(sizeof(dispex_data_t));
231 data->funcs = heap_alloc(size*sizeof(func_info_t));
232 list_add_tail(&dispex_data_list, &data->entry);
235 hres = get_typeinfo(*tid, &ti);
241 hres = ITypeInfo_GetFuncDesc(ti, i++, &funcdesc);
245 add_func_info(data, &size, *tid, funcdesc->memid, dti);
246 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
249 ITypeInfo_Release(ti);
253 if(!data->func_cnt) {
254 heap_free(data->funcs);
256 }else if(data->func_cnt != size) {
257 data->funcs = heap_realloc(data->funcs, data->func_cnt * sizeof(func_info_t));
260 qsort(data->funcs, data->func_cnt, sizeof(func_info_t), dispid_cmp);
263 data->name_table = heap_alloc(data->func_cnt * sizeof(func_info_t*));
264 for(i=0; i < data->func_cnt; i++)
265 data->name_table[i] = data->funcs+i;
266 qsort(data->name_table, data->func_cnt, sizeof(func_info_t*), func_name_cmp);
268 data->name_table = NULL;
271 ITypeInfo_Release(dti);
275 static CRITICAL_SECTION cs_dispex_static_data;
276 static CRITICAL_SECTION_DEBUG cs_dispex_static_data_dbg =
278 0, 0, &cs_dispex_static_data,
279 { &cs_dispex_static_data_dbg.ProcessLocksList, &cs_dispex_static_data_dbg.ProcessLocksList },
280 0, 0, { (DWORD_PTR)(__FILE__ ": dispex_static_data") }
282 static CRITICAL_SECTION cs_dispex_static_data = { &cs_dispex_static_data_dbg, -1, 0, 0, 0, 0 };
285 static dispex_data_t *get_dispex_data(DispatchEx *This)
288 return This->data->data;
290 EnterCriticalSection(&cs_dispex_static_data);
292 if(!This->data->data)
293 This->data->data = preprocess_dispex_data(This);
295 LeaveCriticalSection(&cs_dispex_static_data);
297 return This->data->data;
300 static inline BOOL is_custom_dispid(DISPID id)
302 return MSXML_DISPID_CUSTOM_MIN <= id && id <= MSXML_DISPID_CUSTOM_MAX;
305 static inline BOOL is_dynamic_dispid(DISPID id)
307 return DISPID_DYNPROP_0 <= id && id <= DISPID_DYNPROP_MAX;
310 static inline DispatchEx *impl_from_IDispatchEx(IDispatchEx *iface)
312 return CONTAINING_RECORD(iface, DispatchEx, IDispatchEx_iface);
315 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
317 DispatchEx *This = impl_from_IDispatchEx(iface);
319 return IUnknown_QueryInterface(This->outer, riid, ppv);
322 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
324 DispatchEx *This = impl_from_IDispatchEx(iface);
326 return IUnknown_AddRef(This->outer);
329 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
331 DispatchEx *This = impl_from_IDispatchEx(iface);
333 return IUnknown_Release(This->outer);
336 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
338 DispatchEx *This = impl_from_IDispatchEx(iface);
340 TRACE("(%p)->(%p)\n", This, pctinfo);
346 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
347 LCID lcid, ITypeInfo **ppTInfo)
349 DispatchEx *This = impl_from_IDispatchEx(iface);
351 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
353 return get_typeinfo(This->data->disp_tid, ppTInfo);
356 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
357 LPOLESTR *rgszNames, UINT cNames,
358 LCID lcid, DISPID *rgDispId)
360 DispatchEx *This = impl_from_IDispatchEx(iface);
364 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
367 for(i=0; i < cNames; i++) {
368 hres = IDispatchEx_GetDispID(&This->IDispatchEx_iface, rgszNames[i], 0, rgDispId+i);
376 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
377 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
378 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
380 DispatchEx *This = impl_from_IDispatchEx(iface);
382 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
383 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
385 return IDispatchEx_InvokeEx(&This->IDispatchEx_iface, dispIdMember, lcid, wFlags,
386 pDispParams, pVarResult, pExcepInfo, NULL);
389 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
391 DispatchEx *This = impl_from_IDispatchEx(iface);
395 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
397 if(grfdex & ~(fdexNameCaseSensitive|fdexNameEnsure|fdexNameImplicit))
398 FIXME("Unsupported grfdex %x\n", grfdex);
400 data = get_dispex_data(This);
405 max = data->func_cnt-1;
410 c = strcmpiW(data->name_table[n]->name, bstrName);
412 if((grfdex & fdexNameCaseSensitive) && strcmpW(data->name_table[n]->name, bstrName))
415 *pid = data->name_table[n]->id;
425 if(This->dynamic_data) {
428 for(i=0; i < This->dynamic_data->prop_cnt; i++) {
429 if(!strcmpW(This->dynamic_data->props[i].name, bstrName)) {
430 *pid = DISPID_DYNPROP_0 + i;
436 if(This->data->vtbl && This->data->vtbl->get_dispid) {
439 hres = This->data->vtbl->get_dispid(This->outer, bstrName, grfdex, pid);
440 if(hres != DISP_E_UNKNOWNNAME)
444 if(grfdex & fdexNameEnsure) {
445 dispex_dynamic_data_t *dynamic_data;
447 TRACE("creating dynamic prop %s\n", debugstr_w(bstrName));
449 if(This->dynamic_data) {
450 dynamic_data = This->dynamic_data;
452 dynamic_data = This->dynamic_data = heap_alloc_zero(sizeof(dispex_dynamic_data_t));
454 return E_OUTOFMEMORY;
457 if(!dynamic_data->buf_size) {
458 dynamic_data->props = heap_alloc(sizeof(dynamic_prop_t)*4);
459 if(!dynamic_data->props)
460 return E_OUTOFMEMORY;
461 dynamic_data->buf_size = 4;
462 }else if(dynamic_data->buf_size == dynamic_data->prop_cnt) {
463 dynamic_prop_t *new_props;
465 new_props = heap_realloc(dynamic_data->props, sizeof(dynamic_prop_t)*(dynamic_data->buf_size<<1));
467 return E_OUTOFMEMORY;
469 dynamic_data->props = new_props;
470 dynamic_data->buf_size <<= 1;
473 dynamic_data->props[dynamic_data->prop_cnt].name = heap_strdupW(bstrName);
474 VariantInit(&dynamic_data->props[dynamic_data->prop_cnt].var);
475 *pid = DISPID_DYNPROP_0 + dynamic_data->prop_cnt++;
480 TRACE("not found %s\n", debugstr_w(bstrName));
481 return DISP_E_UNKNOWNNAME;
484 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
485 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
487 DispatchEx *This = impl_from_IDispatchEx(iface);
495 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
497 if(is_custom_dispid(id) && This->data->vtbl && This->data->vtbl->invoke)
498 return This->data->vtbl->invoke(This->outer, id, lcid, wFlags, pdp, pvarRes, pei);
500 if(wFlags == DISPATCH_CONSTRUCT) {
501 FIXME("DISPATCH_CONSTRUCT not implemented\n");
505 if(is_dynamic_dispid(id)) {
506 DWORD idx = id - DISPID_DYNPROP_0;
509 if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
510 return DISP_E_UNKNOWNNAME;
512 var = &This->dynamic_data->props[idx].var;
515 case INVOKE_PROPERTYGET:
516 return VariantCopy(pvarRes, var);
517 case INVOKE_PROPERTYPUT:
519 return VariantCopy(var, pdp->rgvarg);
521 FIXME("unhandled wFlags %x\n", wFlags);
526 data = get_dispex_data(This);
531 max = data->func_cnt-1;
536 if(data->funcs[n].id == id)
539 if(data->funcs[n].id < id)
546 WARN("invalid id %x\n", id);
547 return DISP_E_UNKNOWNNAME;
550 hres = get_typeinfo(data->funcs[n].tid, &ti);
552 ERR("Could not get type info: %08x\n", hres);
556 hres = IUnknown_QueryInterface(This->outer, tid_ids[data->funcs[n].tid], (void**)&unk);
558 ERR("Could not get iface: %08x\n", hres);
562 hres = ITypeInfo_Invoke(ti, unk, id, wFlags, pdp, pvarRes, pei, &argerr);
564 ITypeInfo_Release(ti);
565 IUnknown_Release(unk);
569 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
571 DispatchEx *This = impl_from_IDispatchEx(iface);
572 TRACE("Not implemented in native msxml3 (%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
576 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
578 DispatchEx *This = impl_from_IDispatchEx(iface);
579 TRACE("Not implemented in native msxml3 (%p)->(%x)\n", This, id);
583 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
585 DispatchEx *This = impl_from_IDispatchEx(iface);
586 TRACE("Not implemented in native msxml3 (%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
590 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
592 DispatchEx *This = impl_from_IDispatchEx(iface);
593 TRACE("Not implemented in native msxml3 (%p)->(%x %p)\n", This, id, pbstrName);
597 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
599 DispatchEx *This = impl_from_IDispatchEx(iface);
600 TRACE(" Not implemented in native msxml3 (%p)->(%x %x %p)\n", This, grfdex, id, pid);
604 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
606 DispatchEx *This = impl_from_IDispatchEx(iface);
607 TRACE("Not implemented in native msxml3 (%p)->(%p)\n", This, ppunk);
611 static IDispatchExVtbl DispatchExVtbl = {
612 DispatchEx_QueryInterface,
615 DispatchEx_GetTypeInfoCount,
616 DispatchEx_GetTypeInfo,
617 DispatchEx_GetIDsOfNames,
619 DispatchEx_GetDispID,
621 DispatchEx_DeleteMemberByName,
622 DispatchEx_DeleteMemberByDispID,
623 DispatchEx_GetMemberProperties,
624 DispatchEx_GetMemberName,
625 DispatchEx_GetNextDispID,
626 DispatchEx_GetNameSpaceParent
629 BOOL dispex_query_interface(DispatchEx *This, REFIID riid, void **ppv)
631 static const IID IID_UndocumentedScriptIface =
632 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa0}};
634 if(IsEqualGUID(&IID_IDispatch, riid)) {
635 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
636 *ppv = &This->IDispatchEx_iface;
637 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
638 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
639 *ppv = &This->IDispatchEx_iface;
640 }else if(IsEqualGUID(&IID_UndocumentedScriptIface, riid)) {
641 TRACE("(%p)->(IID_UndocumentedScriptIface %p) returning NULL\n", This, ppv);
643 }else if (IsEqualGUID(&IID_IObjectIdentity, riid)) {
644 TRACE("IID_IObjectIdentity not supported returning NULL\n");
651 IUnknown_AddRef((IUnknown*)*ppv);
655 void init_dispex(DispatchEx *dispex, IUnknown *outer, dispex_static_data_t *data)
657 dispex->IDispatchEx_iface.lpVtbl = &DispatchExVtbl;
658 dispex->outer = outer;