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
28 #include "wine/debug.h"
30 #include "mshtml_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
40 struct dispex_data_t {
43 func_info_t **name_table;
48 static ITypeLib *typelib;
49 static ITypeInfo *typeinfos[LAST_tid];
50 static struct list dispex_data_list = LIST_INIT(dispex_data_list);
52 static REFIID tid_ids[] = {
54 &DIID_DispHTMLDocument,
55 &DIID_DispHTMLDOMTextNode,
56 &DIID_DispHTMLElementCollection,
57 &DIID_DispHTMLUnknownElement,
58 &DIID_DispHTMLWindow2,
65 &IID_IHTMLDOMTextNode,
68 &IID_IHTMLElementCollection,
74 HRESULT get_typeinfo(tid_t tid, ITypeInfo **typeinfo)
81 hres = LoadRegTypeLib(&LIBID_MSHTML, 4, 0, LOCALE_SYSTEM_DEFAULT, &tl);
83 ERR("LoadRegTypeLib failed: %08x\n", hres);
87 if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
94 hres = ITypeLib_GetTypeInfoOfGuid(typelib, tid_ids[tid], &typeinfo);
96 ERR("GetTypeInfoOfGuid(%s) failed: %08x\n", debugstr_guid(tid_ids[tid]), hres);
100 if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), typeinfo, NULL))
101 ITypeInfo_Release(typeinfo);
104 *typeinfo = typeinfos[tid];
108 void release_typelib(void)
113 while(!list_empty(&dispex_data_list)) {
114 iter = LIST_ENTRY(list_head(&dispex_data_list), dispex_data_t, entry);
115 list_remove(&iter->entry);
117 for(i=0; i < iter->func_cnt; i++)
118 SysFreeString(iter->funcs[i].name);
120 heap_free(iter->funcs);
121 heap_free(iter->name_table);
128 for(i=0; i < sizeof(typeinfos)/sizeof(*typeinfos); i++)
130 ITypeInfo_Release(typeinfos[i]);
132 ITypeLib_Release(typelib);
135 static void add_func_info(dispex_data_t *data, DWORD *size, tid_t tid, DISPID id, ITypeInfo *dti)
139 if(data->func_cnt && data->funcs[data->func_cnt-1].id == id)
142 if(data->func_cnt == *size)
143 data->funcs = heap_realloc(data->funcs, (*size <<= 1)*sizeof(func_info_t));
145 hres = ITypeInfo_GetDocumentation(dti, id, &data->funcs[data->func_cnt].name, NULL, NULL, NULL);
149 data->funcs[data->func_cnt].id = id;
150 data->funcs[data->func_cnt].tid = tid;
155 static int dispid_cmp(const void *p1, const void *p2)
157 return ((func_info_t*)p1)->id - ((func_info_t*)p2)->id;
160 static int func_name_cmp(const void *p1, const void *p2)
162 return strcmpiW((*(func_info_t**)p1)->name, (*(func_info_t**)p2)->name);
165 static dispex_data_t *preprocess_dispex_data(DispatchEx *This)
167 const tid_t *tid = This->data->iface_tids;
174 TRACE("(%p)\n", This);
176 hres = get_typeinfo(This->data->disp_tid, &dti);
178 ERR("Could not get disp type info: %08x\n", hres);
182 data = heap_alloc(sizeof(dispex_data_t));
184 data->funcs = heap_alloc(size*sizeof(func_info_t));
185 list_add_tail(&dispex_data_list, &data->entry);
188 hres = get_typeinfo(*tid, &ti);
194 hres = ITypeInfo_GetFuncDesc(ti, i++, &funcdesc);
198 add_func_info(data, &size, *tid, funcdesc->memid, dti);
199 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
205 if(!data->func_cnt) {
206 heap_free(data->funcs);
208 }else if(data->func_cnt != size) {
209 data->funcs = heap_realloc(data->funcs, data->func_cnt * sizeof(func_info_t));
212 qsort(data->funcs, data->func_cnt, sizeof(func_info_t), dispid_cmp);
215 data->name_table = heap_alloc(data->func_cnt * sizeof(func_info_t*));
216 for(i=0; i < data->func_cnt; i++)
217 data->name_table[i] = data->funcs+i;
218 qsort(data->name_table, data->func_cnt, sizeof(func_info_t*), func_name_cmp);
220 data->name_table = NULL;
226 static CRITICAL_SECTION cs_dispex_static_data;
227 static CRITICAL_SECTION_DEBUG cs_dispex_static_data_dbg =
229 0, 0, &cs_dispex_static_data,
230 { &cs_dispex_static_data_dbg.ProcessLocksList, &cs_dispex_static_data_dbg.ProcessLocksList },
231 0, 0, { (DWORD_PTR)(__FILE__ ": dispex_static_data") }
233 static CRITICAL_SECTION cs_dispex_static_data = { &cs_dispex_static_data_dbg, -1, 0, 0, 0, 0 };
236 static dispex_data_t *get_dispex_data(DispatchEx *This)
239 return This->data->data;
241 EnterCriticalSection(&cs_dispex_static_data);
243 if(!This->data->data)
244 This->data->data = preprocess_dispex_data(This);
246 LeaveCriticalSection(&cs_dispex_static_data);
248 return This->data->data;
251 void call_disp_func(HTMLDocument *doc, IDispatch *disp)
253 DISPID named_arg = DISPID_THIS;
255 DISPPARAMS params = {&arg, &named_arg, 1, 1};
261 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
263 FIXME("Could not get IDispatchEx interface: %08x\n", hres);
267 V_VT(&arg) = VT_DISPATCH;
268 V_DISPATCH(&arg) = (IDispatch*)HTMLWINDOW2(doc->window);
270 memset(&ei, 0, sizeof(ei));
272 hres = IDispatchEx_InvokeEx(dispex, 0, GetUserDefaultLCID(), DISPATCH_METHOD, ¶ms, &res, &ei, NULL);
273 IDispatchEx_Release(dispex);
275 TRACE("%p returned %08x\n", disp, hres);
280 #define DISPATCHEX_THIS(iface) DEFINE_THIS(DispatchEx, IDispatchEx, iface)
282 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
284 DispatchEx *This = DISPATCHEX_THIS(iface);
286 return IUnknown_QueryInterface(This->outer, riid, ppv);
289 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
291 DispatchEx *This = DISPATCHEX_THIS(iface);
293 return IUnknown_AddRef(This->outer);
296 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
298 DispatchEx *This = DISPATCHEX_THIS(iface);
300 return IUnknown_Release(This->outer);
303 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
305 DispatchEx *This = DISPATCHEX_THIS(iface);
307 TRACE("(%p)->(%p)\n", This, pctinfo);
313 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
314 LCID lcid, ITypeInfo **ppTInfo)
316 DispatchEx *This = DISPATCHEX_THIS(iface);
319 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
321 hres = get_typeinfo(This->data->disp_tid, ppTInfo);
325 ITypeInfo_AddRef(*ppTInfo);
329 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
330 LPOLESTR *rgszNames, UINT cNames,
331 LCID lcid, DISPID *rgDispId)
333 DispatchEx *This = DISPATCHEX_THIS(iface);
337 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
340 for(i=0; i < cNames; i++) {
341 hres = IDispatchEx_GetDispID(DISPATCHEX(This), rgszNames[i], 0, rgDispId+i);
349 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
350 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
351 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
353 DispatchEx *This = DISPATCHEX_THIS(iface);
355 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
356 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
358 return IDispatchEx_InvokeEx(DISPATCHEX(This), dispIdMember, lcid, wFlags,
359 pDispParams, pVarResult, pExcepInfo, NULL);
362 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
364 DispatchEx *This = DISPATCHEX_THIS(iface);
368 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
370 if(grfdex & (~fdexNameCaseSensitive))
371 FIXME("Unsupported grfdex %x\n", grfdex);
373 data = get_dispex_data(This);
378 max = data->func_cnt-1;
383 c = strcmpiW(data->name_table[n]->name, bstrName);
385 if((grfdex & fdexNameCaseSensitive) && strcmpW(data->name_table[n]->name, bstrName))
388 *pid = data->name_table[n]->id;
398 TRACE("not found %s\n", debugstr_w(bstrName));
399 return DISP_E_UNKNOWNNAME;
402 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
403 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
405 DispatchEx *This = DISPATCHEX_THIS(iface);
413 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
415 if(wFlags == DISPATCH_CONSTRUCT) {
416 FIXME("DISPATCH_CONSTRUCT not implemented\n");
420 data = get_dispex_data(This);
425 max = data->func_cnt-1;
430 if(data->funcs[n].id == id)
433 if(data->funcs[n].id < id)
440 WARN("invalid id %x\n", id);
441 return DISP_E_UNKNOWNNAME;
444 hres = get_typeinfo(data->funcs[n].tid, &ti);
446 ERR("Could not get type info: %08x\n", hres);
450 hres = IUnknown_QueryInterface(This->outer, tid_ids[data->funcs[n].tid], (void**)&unk);
452 ERR("Could not get iface: %08x\n", hres);
456 hres = ITypeInfo_Invoke(ti, unk, id, wFlags, pdp, pvarRes, pei, &argerr);
458 IUnknown_Release(unk);
462 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
464 DispatchEx *This = DISPATCHEX_THIS(iface);
465 FIXME("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
469 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
471 DispatchEx *This = DISPATCHEX_THIS(iface);
472 FIXME("(%p)->(%x)\n", This, id);
476 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
478 DispatchEx *This = DISPATCHEX_THIS(iface);
479 FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
483 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
485 DispatchEx *This = DISPATCHEX_THIS(iface);
486 FIXME("(%p)->(%x %p)\n", This, id, pbstrName);
490 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
492 DispatchEx *This = DISPATCHEX_THIS(iface);
493 FIXME("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
497 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
499 DispatchEx *This = DISPATCHEX_THIS(iface);
500 FIXME("(%p)->(%p)\n", This, ppunk);
504 #undef DISPATCHEX_THIS
506 static IDispatchExVtbl DispatchExVtbl = {
507 DispatchEx_QueryInterface,
510 DispatchEx_GetTypeInfoCount,
511 DispatchEx_GetTypeInfo,
512 DispatchEx_GetIDsOfNames,
514 DispatchEx_GetDispID,
516 DispatchEx_DeleteMemberByName,
517 DispatchEx_DeleteMemberByDispID,
518 DispatchEx_GetMemberProperties,
519 DispatchEx_GetMemberName,
520 DispatchEx_GetNextDispID,
521 DispatchEx_GetNameSpaceParent
524 void init_dispex(DispatchEx *dispex, IUnknown *outer, dispex_static_data_t *data)
526 dispex->lpIDispatchExVtbl = &DispatchExVtbl;
527 dispex->outer = outer;