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_DispDOMChildrenCollection
,
55 &DIID_DispHTMLCommentElement
,
56 &DIID_DispHTMLDocument
,
57 &DIID_DispHTMLDOMTextNode
,
58 &DIID_DispHTMLElementCollection
,
60 &DIID_DispHTMLInputElement
,
61 &DIID_DispHTMLOptionElement
,
63 &DIID_DispHTMLUnknownElement
,
64 &DIID_DispHTMLWindow2
,
65 &IID_IHTMLCommentElement
,
70 &IID_IHTMLDOMChildrenCollection
,
73 &IID_IHTMLDOMTextNode
,
76 &IID_IHTMLElementCollection
,
78 &IID_IHTMLInputElement
,
79 &IID_IHTMLOptionElement
,
86 HRESULT
get_typeinfo(tid_t tid
, ITypeInfo
**typeinfo
)
93 hres
= LoadRegTypeLib(&LIBID_MSHTML
, 4, 0, LOCALE_SYSTEM_DEFAULT
, &tl
);
95 ERR("LoadRegTypeLib failed: %08x\n", hres
);
99 if(InterlockedCompareExchangePointer((void**)&typelib
, tl
, NULL
))
100 ITypeLib_Release(tl
);
103 if(!typeinfos
[tid
]) {
106 hres
= ITypeLib_GetTypeInfoOfGuid(typelib
, tid_ids
[tid
], &typeinfo
);
108 ERR("GetTypeInfoOfGuid(%s) failed: %08x\n", debugstr_guid(tid_ids
[tid
]), hres
);
112 if(InterlockedCompareExchangePointer((void**)(typeinfos
+tid
), typeinfo
, NULL
))
113 ITypeInfo_Release(typeinfo
);
116 *typeinfo
= typeinfos
[tid
];
120 void release_typelib(void)
125 while(!list_empty(&dispex_data_list
)) {
126 iter
= LIST_ENTRY(list_head(&dispex_data_list
), dispex_data_t
, entry
);
127 list_remove(&iter
->entry
);
129 for(i
=0; i
< iter
->func_cnt
; i
++)
130 SysFreeString(iter
->funcs
[i
].name
);
132 heap_free(iter
->funcs
);
133 heap_free(iter
->name_table
);
140 for(i
=0; i
< sizeof(typeinfos
)/sizeof(*typeinfos
); i
++)
142 ITypeInfo_Release(typeinfos
[i
]);
144 ITypeLib_Release(typelib
);
147 static void add_func_info(dispex_data_t
*data
, DWORD
*size
, tid_t tid
, DISPID id
, ITypeInfo
*dti
)
151 if(data
->func_cnt
&& data
->funcs
[data
->func_cnt
-1].id
== id
)
154 if(data
->func_cnt
== *size
)
155 data
->funcs
= heap_realloc(data
->funcs
, (*size
<<= 1)*sizeof(func_info_t
));
157 hres
= ITypeInfo_GetDocumentation(dti
, id
, &data
->funcs
[data
->func_cnt
].name
, NULL
, NULL
, NULL
);
161 data
->funcs
[data
->func_cnt
].id
= id
;
162 data
->funcs
[data
->func_cnt
].tid
= tid
;
167 static int dispid_cmp(const void *p1
, const void *p2
)
169 return ((func_info_t
*)p1
)->id
- ((func_info_t
*)p2
)->id
;
172 static int func_name_cmp(const void *p1
, const void *p2
)
174 return strcmpiW((*(func_info_t
**)p1
)->name
, (*(func_info_t
**)p2
)->name
);
177 static dispex_data_t
*preprocess_dispex_data(DispatchEx
*This
)
179 const tid_t
*tid
= This
->data
->iface_tids
;
186 TRACE("(%p)\n", This
);
188 hres
= get_typeinfo(This
->data
->disp_tid
, &dti
);
190 ERR("Could not get disp type info: %08x\n", hres
);
194 data
= heap_alloc(sizeof(dispex_data_t
));
196 data
->funcs
= heap_alloc(size
*sizeof(func_info_t
));
197 list_add_tail(&dispex_data_list
, &data
->entry
);
200 hres
= get_typeinfo(*tid
, &ti
);
206 hres
= ITypeInfo_GetFuncDesc(ti
, i
++, &funcdesc
);
210 add_func_info(data
, &size
, *tid
, funcdesc
->memid
, dti
);
211 ITypeInfo_ReleaseFuncDesc(ti
, funcdesc
);
217 if(!data
->func_cnt
) {
218 heap_free(data
->funcs
);
220 }else if(data
->func_cnt
!= size
) {
221 data
->funcs
= heap_realloc(data
->funcs
, data
->func_cnt
* sizeof(func_info_t
));
224 qsort(data
->funcs
, data
->func_cnt
, sizeof(func_info_t
), dispid_cmp
);
227 data
->name_table
= heap_alloc(data
->func_cnt
* sizeof(func_info_t
*));
228 for(i
=0; i
< data
->func_cnt
; i
++)
229 data
->name_table
[i
] = data
->funcs
+i
;
230 qsort(data
->name_table
, data
->func_cnt
, sizeof(func_info_t
*), func_name_cmp
);
232 data
->name_table
= NULL
;
238 static CRITICAL_SECTION cs_dispex_static_data
;
239 static CRITICAL_SECTION_DEBUG cs_dispex_static_data_dbg
=
241 0, 0, &cs_dispex_static_data
,
242 { &cs_dispex_static_data_dbg
.ProcessLocksList
, &cs_dispex_static_data_dbg
.ProcessLocksList
},
243 0, 0, { (DWORD_PTR
)(__FILE__
": dispex_static_data") }
245 static CRITICAL_SECTION cs_dispex_static_data
= { &cs_dispex_static_data_dbg
, -1, 0, 0, 0, 0 };
248 static dispex_data_t
*get_dispex_data(DispatchEx
*This
)
251 return This
->data
->data
;
253 EnterCriticalSection(&cs_dispex_static_data
);
255 if(!This
->data
->data
)
256 This
->data
->data
= preprocess_dispex_data(This
);
258 LeaveCriticalSection(&cs_dispex_static_data
);
260 return This
->data
->data
;
263 void call_disp_func(HTMLDocument
*doc
, IDispatch
*disp
)
265 DISPID named_arg
= DISPID_THIS
;
267 DISPPARAMS params
= {&arg
, &named_arg
, 1, 1};
273 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
275 FIXME("Could not get IDispatchEx interface: %08x\n", hres
);
279 V_VT(&arg
) = VT_DISPATCH
;
280 V_DISPATCH(&arg
) = (IDispatch
*)HTMLWINDOW2(doc
->window
);
282 memset(&ei
, 0, sizeof(ei
));
284 hres
= IDispatchEx_InvokeEx(dispex
, 0, GetUserDefaultLCID(), DISPATCH_METHOD
, ¶ms
, &res
, &ei
, NULL
);
285 IDispatchEx_Release(dispex
);
287 TRACE("%p returned %08x\n", disp
, hres
);
292 static inline BOOL
is_custom_dispid(DISPID id
)
294 return MSHTML_DISPID_CUSTOM_MIN
<= id
&& id
<= MSHTML_DISPID_CUSTOM_MAX
;
297 #define DISPATCHEX_THIS(iface) DEFINE_THIS(DispatchEx, IDispatchEx, iface)
299 static HRESULT WINAPI
DispatchEx_QueryInterface(IDispatchEx
*iface
, REFIID riid
, void **ppv
)
301 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
303 return IUnknown_QueryInterface(This
->outer
, riid
, ppv
);
306 static ULONG WINAPI
DispatchEx_AddRef(IDispatchEx
*iface
)
308 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
310 return IUnknown_AddRef(This
->outer
);
313 static ULONG WINAPI
DispatchEx_Release(IDispatchEx
*iface
)
315 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
317 return IUnknown_Release(This
->outer
);
320 static HRESULT WINAPI
DispatchEx_GetTypeInfoCount(IDispatchEx
*iface
, UINT
*pctinfo
)
322 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
324 TRACE("(%p)->(%p)\n", This
, pctinfo
);
330 static HRESULT WINAPI
DispatchEx_GetTypeInfo(IDispatchEx
*iface
, UINT iTInfo
,
331 LCID lcid
, ITypeInfo
**ppTInfo
)
333 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
336 TRACE("(%p)->(%u %u %p)\n", This
, iTInfo
, lcid
, ppTInfo
);
338 hres
= get_typeinfo(This
->data
->disp_tid
, ppTInfo
);
342 ITypeInfo_AddRef(*ppTInfo
);
346 static HRESULT WINAPI
DispatchEx_GetIDsOfNames(IDispatchEx
*iface
, REFIID riid
,
347 LPOLESTR
*rgszNames
, UINT cNames
,
348 LCID lcid
, DISPID
*rgDispId
)
350 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
354 TRACE("(%p)->(%s %p %u %u %p)\n", This
, debugstr_guid(riid
), rgszNames
, cNames
,
357 for(i
=0; i
< cNames
; i
++) {
358 hres
= IDispatchEx_GetDispID(DISPATCHEX(This
), rgszNames
[i
], 0, rgDispId
+i
);
366 static HRESULT WINAPI
DispatchEx_Invoke(IDispatchEx
*iface
, DISPID dispIdMember
,
367 REFIID riid
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
,
368 VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
370 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
372 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This
, dispIdMember
, debugstr_guid(riid
),
373 lcid
, wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
375 return IDispatchEx_InvokeEx(DISPATCHEX(This
), dispIdMember
, lcid
, wFlags
,
376 pDispParams
, pVarResult
, pExcepInfo
, NULL
);
379 static HRESULT WINAPI
DispatchEx_GetDispID(IDispatchEx
*iface
, BSTR bstrName
, DWORD grfdex
, DISPID
*pid
)
381 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
385 TRACE("(%p)->(%s %x %p)\n", This
, debugstr_w(bstrName
), grfdex
, pid
);
387 if(grfdex
& (~fdexNameCaseSensitive
))
388 FIXME("Unsupported grfdex %x\n", grfdex
);
390 data
= get_dispex_data(This
);
395 max
= data
->func_cnt
-1;
400 c
= strcmpiW(data
->name_table
[n
]->name
, bstrName
);
402 if((grfdex
& fdexNameCaseSensitive
) && strcmpW(data
->name_table
[n
]->name
, bstrName
))
405 *pid
= data
->name_table
[n
]->id
;
415 if(This
->data
->vtbl
&& This
->data
->vtbl
->get_dispid
) {
418 hres
= This
->data
->vtbl
->get_dispid(This
->outer
, bstrName
, grfdex
, pid
);
419 if(hres
!= DISP_E_UNKNOWNNAME
)
423 TRACE("not found %s\n", debugstr_w(bstrName
));
424 return DISP_E_UNKNOWNNAME
;
427 static HRESULT WINAPI
DispatchEx_InvokeEx(IDispatchEx
*iface
, DISPID id
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pdp
,
428 VARIANT
*pvarRes
, EXCEPINFO
*pei
, IServiceProvider
*pspCaller
)
430 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
438 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This
, id
, lcid
, wFlags
, pdp
, pvarRes
, pei
, pspCaller
);
440 if(is_custom_dispid(id
) && This
->data
->vtbl
&& This
->data
->vtbl
->invoke
)
441 return This
->data
->vtbl
->invoke(This
->outer
, id
, lcid
, wFlags
, pdp
, pvarRes
, pei
, pspCaller
);
443 if(wFlags
== DISPATCH_CONSTRUCT
) {
444 FIXME("DISPATCH_CONSTRUCT not implemented\n");
448 data
= get_dispex_data(This
);
453 max
= data
->func_cnt
-1;
458 if(data
->funcs
[n
].id
== id
)
461 if(data
->funcs
[n
].id
< id
)
468 WARN("invalid id %x\n", id
);
469 return DISP_E_UNKNOWNNAME
;
472 hres
= get_typeinfo(data
->funcs
[n
].tid
, &ti
);
474 ERR("Could not get type info: %08x\n", hres
);
478 hres
= IUnknown_QueryInterface(This
->outer
, tid_ids
[data
->funcs
[n
].tid
], (void**)&unk
);
480 ERR("Could not get iface: %08x\n", hres
);
484 hres
= ITypeInfo_Invoke(ti
, unk
, id
, wFlags
, pdp
, pvarRes
, pei
, &argerr
);
486 IUnknown_Release(unk
);
490 static HRESULT WINAPI
DispatchEx_DeleteMemberByName(IDispatchEx
*iface
, BSTR bstrName
, DWORD grfdex
)
492 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
493 FIXME("(%p)->(%s %x)\n", This
, debugstr_w(bstrName
), grfdex
);
497 static HRESULT WINAPI
DispatchEx_DeleteMemberByDispID(IDispatchEx
*iface
, DISPID id
)
499 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
500 FIXME("(%p)->(%x)\n", This
, id
);
504 static HRESULT WINAPI
DispatchEx_GetMemberProperties(IDispatchEx
*iface
, DISPID id
, DWORD grfdexFetch
, DWORD
*pgrfdex
)
506 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
507 FIXME("(%p)->(%x %x %p)\n", This
, id
, grfdexFetch
, pgrfdex
);
511 static HRESULT WINAPI
DispatchEx_GetMemberName(IDispatchEx
*iface
, DISPID id
, BSTR
*pbstrName
)
513 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
514 FIXME("(%p)->(%x %p)\n", This
, id
, pbstrName
);
518 static HRESULT WINAPI
DispatchEx_GetNextDispID(IDispatchEx
*iface
, DWORD grfdex
, DISPID id
, DISPID
*pid
)
520 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
521 FIXME("(%p)->(%x %x %p)\n", This
, grfdex
, id
, pid
);
525 static HRESULT WINAPI
DispatchEx_GetNameSpaceParent(IDispatchEx
*iface
, IUnknown
**ppunk
)
527 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
528 FIXME("(%p)->(%p)\n", This
, ppunk
);
532 #undef DISPATCHEX_THIS
534 static IDispatchExVtbl DispatchExVtbl
= {
535 DispatchEx_QueryInterface
,
538 DispatchEx_GetTypeInfoCount
,
539 DispatchEx_GetTypeInfo
,
540 DispatchEx_GetIDsOfNames
,
542 DispatchEx_GetDispID
,
544 DispatchEx_DeleteMemberByName
,
545 DispatchEx_DeleteMemberByDispID
,
546 DispatchEx_GetMemberProperties
,
547 DispatchEx_GetMemberName
,
548 DispatchEx_GetNextDispID
,
549 DispatchEx_GetNameSpaceParent
552 void init_dispex(DispatchEx
*dispex
, IUnknown
*outer
, dispex_static_data_t
*data
)
554 dispex
->lpIDispatchExVtbl
= &DispatchExVtbl
;
555 dispex
->outer
= outer
;