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
;
53 struct dispex_dynamic_data_t
{
56 dynamic_prop_t
*props
;
59 #define DISPID_DYNPROP_0 0x50000000
60 #define DISPID_DYNPROP_MAX 0x5fffffff
62 static ITypeLib
*typelib
;
63 static ITypeInfo
*typeinfos
[LAST_tid
];
64 static struct list dispex_data_list
= LIST_INIT(dispex_data_list
);
66 static REFIID tid_ids
[] = {
68 &DIID_DispDOMChildrenCollection
,
69 &DIID_DispHTMLCommentElement
,
70 &DIID_DispHTMLDocument
,
71 &DIID_DispHTMLDOMTextNode
,
72 &DIID_DispHTMLElementCollection
,
73 &DIID_DispHTMLGenericElement
,
75 &DIID_DispHTMLInputElement
,
76 &DIID_DispHTMLOptionElement
,
77 &DIID_DispHTMLSelectElement
,
79 &DIID_DispHTMLUnknownElement
,
80 &DIID_DispHTMLWindow2
,
81 &IID_IHTMLCommentElement
,
86 &IID_IHTMLDOMChildrenCollection
,
89 &IID_IHTMLDOMTextNode
,
92 &IID_IHTMLElementCollection
,
93 &IID_IHTMLGenericElement
,
95 &IID_IHTMLInputElement
,
96 &IID_IHTMLOptionElement
,
97 &IID_IHTMLSelectElement
,
104 HRESULT
get_typeinfo(tid_t tid
, ITypeInfo
**typeinfo
)
111 hres
= LoadRegTypeLib(&LIBID_MSHTML
, 4, 0, LOCALE_SYSTEM_DEFAULT
, &tl
);
113 ERR("LoadRegTypeLib failed: %08x\n", hres
);
117 if(InterlockedCompareExchangePointer((void**)&typelib
, tl
, NULL
))
118 ITypeLib_Release(tl
);
121 if(!typeinfos
[tid
]) {
124 hres
= ITypeLib_GetTypeInfoOfGuid(typelib
, tid_ids
[tid
], &typeinfo
);
126 ERR("GetTypeInfoOfGuid(%s) failed: %08x\n", debugstr_guid(tid_ids
[tid
]), hres
);
130 if(InterlockedCompareExchangePointer((void**)(typeinfos
+tid
), typeinfo
, NULL
))
131 ITypeInfo_Release(typeinfo
);
134 *typeinfo
= typeinfos
[tid
];
138 void release_typelib(void)
143 while(!list_empty(&dispex_data_list
)) {
144 iter
= LIST_ENTRY(list_head(&dispex_data_list
), dispex_data_t
, entry
);
145 list_remove(&iter
->entry
);
147 for(i
=0; i
< iter
->func_cnt
; i
++)
148 SysFreeString(iter
->funcs
[i
].name
);
150 heap_free(iter
->funcs
);
151 heap_free(iter
->name_table
);
158 for(i
=0; i
< sizeof(typeinfos
)/sizeof(*typeinfos
); i
++)
160 ITypeInfo_Release(typeinfos
[i
]);
162 ITypeLib_Release(typelib
);
165 static void add_func_info(dispex_data_t
*data
, DWORD
*size
, tid_t tid
, DISPID id
, ITypeInfo
*dti
)
169 if(data
->func_cnt
&& data
->funcs
[data
->func_cnt
-1].id
== id
)
172 if(data
->func_cnt
== *size
)
173 data
->funcs
= heap_realloc(data
->funcs
, (*size
<<= 1)*sizeof(func_info_t
));
175 hres
= ITypeInfo_GetDocumentation(dti
, id
, &data
->funcs
[data
->func_cnt
].name
, NULL
, NULL
, NULL
);
179 data
->funcs
[data
->func_cnt
].id
= id
;
180 data
->funcs
[data
->func_cnt
].tid
= tid
;
185 static int dispid_cmp(const void *p1
, const void *p2
)
187 return ((func_info_t
*)p1
)->id
- ((func_info_t
*)p2
)->id
;
190 static int func_name_cmp(const void *p1
, const void *p2
)
192 return strcmpiW((*(func_info_t
**)p1
)->name
, (*(func_info_t
**)p2
)->name
);
195 static dispex_data_t
*preprocess_dispex_data(DispatchEx
*This
)
197 const tid_t
*tid
= This
->data
->iface_tids
;
204 TRACE("(%p)\n", This
);
206 hres
= get_typeinfo(This
->data
->disp_tid
, &dti
);
208 ERR("Could not get disp type info: %08x\n", hres
);
212 data
= heap_alloc(sizeof(dispex_data_t
));
214 data
->funcs
= heap_alloc(size
*sizeof(func_info_t
));
215 list_add_tail(&dispex_data_list
, &data
->entry
);
218 hres
= get_typeinfo(*tid
, &ti
);
224 hres
= ITypeInfo_GetFuncDesc(ti
, i
++, &funcdesc
);
228 add_func_info(data
, &size
, *tid
, funcdesc
->memid
, dti
);
229 ITypeInfo_ReleaseFuncDesc(ti
, funcdesc
);
235 if(!data
->func_cnt
) {
236 heap_free(data
->funcs
);
238 }else if(data
->func_cnt
!= size
) {
239 data
->funcs
= heap_realloc(data
->funcs
, data
->func_cnt
* sizeof(func_info_t
));
242 qsort(data
->funcs
, data
->func_cnt
, sizeof(func_info_t
), dispid_cmp
);
245 data
->name_table
= heap_alloc(data
->func_cnt
* sizeof(func_info_t
*));
246 for(i
=0; i
< data
->func_cnt
; i
++)
247 data
->name_table
[i
] = data
->funcs
+i
;
248 qsort(data
->name_table
, data
->func_cnt
, sizeof(func_info_t
*), func_name_cmp
);
250 data
->name_table
= NULL
;
256 static CRITICAL_SECTION cs_dispex_static_data
;
257 static CRITICAL_SECTION_DEBUG cs_dispex_static_data_dbg
=
259 0, 0, &cs_dispex_static_data
,
260 { &cs_dispex_static_data_dbg
.ProcessLocksList
, &cs_dispex_static_data_dbg
.ProcessLocksList
},
261 0, 0, { (DWORD_PTR
)(__FILE__
": dispex_static_data") }
263 static CRITICAL_SECTION cs_dispex_static_data
= { &cs_dispex_static_data_dbg
, -1, 0, 0, 0, 0 };
266 static dispex_data_t
*get_dispex_data(DispatchEx
*This
)
269 return This
->data
->data
;
271 EnterCriticalSection(&cs_dispex_static_data
);
273 if(!This
->data
->data
)
274 This
->data
->data
= preprocess_dispex_data(This
);
276 LeaveCriticalSection(&cs_dispex_static_data
);
278 return This
->data
->data
;
281 void call_disp_func(HTMLDocument
*doc
, IDispatch
*disp
)
283 DISPID named_arg
= DISPID_THIS
;
285 DISPPARAMS params
= {&arg
, &named_arg
, 1, 1};
291 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
293 FIXME("Could not get IDispatchEx interface: %08x\n", hres
);
297 V_VT(&arg
) = VT_DISPATCH
;
298 V_DISPATCH(&arg
) = (IDispatch
*)HTMLWINDOW2(doc
->window
);
300 memset(&ei
, 0, sizeof(ei
));
302 hres
= IDispatchEx_InvokeEx(dispex
, 0, GetUserDefaultLCID(), DISPATCH_METHOD
, ¶ms
, &res
, &ei
, NULL
);
303 IDispatchEx_Release(dispex
);
305 TRACE("%p returned %08x\n", disp
, hres
);
310 static inline BOOL
is_custom_dispid(DISPID id
)
312 return MSHTML_DISPID_CUSTOM_MIN
<= id
&& id
<= MSHTML_DISPID_CUSTOM_MAX
;
315 static inline BOOL
is_dynamic_dispid(DISPID id
)
317 return DISPID_DYNPROP_0
<= id
&& id
<= DISPID_DYNPROP_MAX
;
320 #define DISPATCHEX_THIS(iface) DEFINE_THIS(DispatchEx, IDispatchEx, iface)
322 static HRESULT WINAPI
DispatchEx_QueryInterface(IDispatchEx
*iface
, REFIID riid
, void **ppv
)
324 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
326 return IUnknown_QueryInterface(This
->outer
, riid
, ppv
);
329 static ULONG WINAPI
DispatchEx_AddRef(IDispatchEx
*iface
)
331 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
333 return IUnknown_AddRef(This
->outer
);
336 static ULONG WINAPI
DispatchEx_Release(IDispatchEx
*iface
)
338 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
340 return IUnknown_Release(This
->outer
);
343 static HRESULT WINAPI
DispatchEx_GetTypeInfoCount(IDispatchEx
*iface
, UINT
*pctinfo
)
345 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
347 TRACE("(%p)->(%p)\n", This
, pctinfo
);
353 static HRESULT WINAPI
DispatchEx_GetTypeInfo(IDispatchEx
*iface
, UINT iTInfo
,
354 LCID lcid
, ITypeInfo
**ppTInfo
)
356 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
359 TRACE("(%p)->(%u %u %p)\n", This
, iTInfo
, lcid
, ppTInfo
);
361 hres
= get_typeinfo(This
->data
->disp_tid
, ppTInfo
);
365 ITypeInfo_AddRef(*ppTInfo
);
369 static HRESULT WINAPI
DispatchEx_GetIDsOfNames(IDispatchEx
*iface
, REFIID riid
,
370 LPOLESTR
*rgszNames
, UINT cNames
,
371 LCID lcid
, DISPID
*rgDispId
)
373 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
377 TRACE("(%p)->(%s %p %u %u %p)\n", This
, debugstr_guid(riid
), rgszNames
, cNames
,
380 for(i
=0; i
< cNames
; i
++) {
381 hres
= IDispatchEx_GetDispID(DISPATCHEX(This
), rgszNames
[i
], 0, rgDispId
+i
);
389 static HRESULT WINAPI
DispatchEx_Invoke(IDispatchEx
*iface
, DISPID dispIdMember
,
390 REFIID riid
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
,
391 VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
393 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
395 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This
, dispIdMember
, debugstr_guid(riid
),
396 lcid
, wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
398 return IDispatchEx_InvokeEx(DISPATCHEX(This
), dispIdMember
, lcid
, wFlags
,
399 pDispParams
, pVarResult
, pExcepInfo
, NULL
);
402 static HRESULT WINAPI
DispatchEx_GetDispID(IDispatchEx
*iface
, BSTR bstrName
, DWORD grfdex
, DISPID
*pid
)
404 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
408 TRACE("(%p)->(%s %x %p)\n", This
, debugstr_w(bstrName
), grfdex
, pid
);
410 if(grfdex
& ~(fdexNameCaseSensitive
|fdexNameEnsure
|fdexNameImplicit
))
411 FIXME("Unsupported grfdex %x\n", grfdex
);
413 data
= get_dispex_data(This
);
418 max
= data
->func_cnt
-1;
423 c
= strcmpiW(data
->name_table
[n
]->name
, bstrName
);
425 if((grfdex
& fdexNameCaseSensitive
) && strcmpW(data
->name_table
[n
]->name
, bstrName
))
428 *pid
= data
->name_table
[n
]->id
;
438 if(This
->dynamic_data
) {
441 for(i
=0; i
< This
->dynamic_data
->prop_cnt
; i
++) {
442 if(!strcmpW(This
->dynamic_data
->props
[i
].name
, bstrName
)) {
443 *pid
= DISPID_DYNPROP_0
+ i
;
449 if(This
->data
->vtbl
&& This
->data
->vtbl
->get_dispid
) {
452 hres
= This
->data
->vtbl
->get_dispid(This
->outer
, bstrName
, grfdex
, pid
);
453 if(hres
!= DISP_E_UNKNOWNNAME
)
457 if(grfdex
& fdexNameEnsure
) {
458 TRACE("creating dynamic prop %s\n", debugstr_w(bstrName
));
460 if(!This
->dynamic_data
) {
461 This
->dynamic_data
= heap_alloc_zero(sizeof(dispex_dynamic_data_t
));
462 This
->dynamic_data
->props
= heap_alloc(This
->dynamic_data
->buf_size
= 4);
463 }else if(This
->dynamic_data
->buf_size
== This
->dynamic_data
->prop_cnt
) {
464 This
->dynamic_data
->props
= heap_realloc(This
->dynamic_data
->props
, This
->dynamic_data
->buf_size
<<=1);
467 This
->dynamic_data
->props
[This
->dynamic_data
->prop_cnt
].name
= heap_strdupW(bstrName
);
468 VariantInit(&This
->dynamic_data
->props
[This
->dynamic_data
->prop_cnt
].var
);
469 *pid
= DISPID_DYNPROP_0
+ This
->dynamic_data
->prop_cnt
++;
474 TRACE("not found %s\n", debugstr_w(bstrName
));
475 return DISP_E_UNKNOWNNAME
;
478 static HRESULT WINAPI
DispatchEx_InvokeEx(IDispatchEx
*iface
, DISPID id
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pdp
,
479 VARIANT
*pvarRes
, EXCEPINFO
*pei
, IServiceProvider
*pspCaller
)
481 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
489 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This
, id
, lcid
, wFlags
, pdp
, pvarRes
, pei
, pspCaller
);
491 if(is_custom_dispid(id
) && This
->data
->vtbl
&& This
->data
->vtbl
->invoke
)
492 return This
->data
->vtbl
->invoke(This
->outer
, id
, lcid
, wFlags
, pdp
, pvarRes
, pei
, pspCaller
);
494 if(wFlags
== DISPATCH_CONSTRUCT
) {
495 FIXME("DISPATCH_CONSTRUCT not implemented\n");
499 if(is_dynamic_dispid(id
)) {
500 DWORD idx
= id
- DISPID_DYNPROP_0
;
503 if(!This
->dynamic_data
|| This
->dynamic_data
->prop_cnt
<= idx
)
504 return DISP_E_UNKNOWNNAME
;
506 var
= &This
->dynamic_data
->props
[idx
].var
;
509 case INVOKE_PROPERTYGET
:
510 return VariantCopy(pvarRes
, var
);
511 case INVOKE_PROPERTYPUT
:
513 return VariantCopy(var
, pdp
->rgvarg
);
515 FIXME("unhandled wFlags %x\n", wFlags
);
520 data
= get_dispex_data(This
);
525 max
= data
->func_cnt
-1;
530 if(data
->funcs
[n
].id
== id
)
533 if(data
->funcs
[n
].id
< id
)
540 WARN("invalid id %x\n", id
);
541 return DISP_E_UNKNOWNNAME
;
544 hres
= get_typeinfo(data
->funcs
[n
].tid
, &ti
);
546 ERR("Could not get type info: %08x\n", hres
);
550 hres
= IUnknown_QueryInterface(This
->outer
, tid_ids
[data
->funcs
[n
].tid
], (void**)&unk
);
552 ERR("Could not get iface: %08x\n", hres
);
556 hres
= ITypeInfo_Invoke(ti
, unk
, id
, wFlags
, pdp
, pvarRes
, pei
, &argerr
);
558 IUnknown_Release(unk
);
562 static HRESULT WINAPI
DispatchEx_DeleteMemberByName(IDispatchEx
*iface
, BSTR bstrName
, DWORD grfdex
)
564 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
565 FIXME("(%p)->(%s %x)\n", This
, debugstr_w(bstrName
), grfdex
);
569 static HRESULT WINAPI
DispatchEx_DeleteMemberByDispID(IDispatchEx
*iface
, DISPID id
)
571 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
572 FIXME("(%p)->(%x)\n", This
, id
);
576 static HRESULT WINAPI
DispatchEx_GetMemberProperties(IDispatchEx
*iface
, DISPID id
, DWORD grfdexFetch
, DWORD
*pgrfdex
)
578 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
579 FIXME("(%p)->(%x %x %p)\n", This
, id
, grfdexFetch
, pgrfdex
);
583 static HRESULT WINAPI
DispatchEx_GetMemberName(IDispatchEx
*iface
, DISPID id
, BSTR
*pbstrName
)
585 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
586 FIXME("(%p)->(%x %p)\n", This
, id
, pbstrName
);
590 static HRESULT WINAPI
DispatchEx_GetNextDispID(IDispatchEx
*iface
, DWORD grfdex
, DISPID id
, DISPID
*pid
)
592 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
593 FIXME("(%p)->(%x %x %p)\n", This
, grfdex
, id
, pid
);
597 static HRESULT WINAPI
DispatchEx_GetNameSpaceParent(IDispatchEx
*iface
, IUnknown
**ppunk
)
599 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
600 FIXME("(%p)->(%p)\n", This
, ppunk
);
604 #undef DISPATCHEX_THIS
606 static IDispatchExVtbl DispatchExVtbl
= {
607 DispatchEx_QueryInterface
,
610 DispatchEx_GetTypeInfoCount
,
611 DispatchEx_GetTypeInfo
,
612 DispatchEx_GetIDsOfNames
,
614 DispatchEx_GetDispID
,
616 DispatchEx_DeleteMemberByName
,
617 DispatchEx_DeleteMemberByDispID
,
618 DispatchEx_GetMemberProperties
,
619 DispatchEx_GetMemberName
,
620 DispatchEx_GetNextDispID
,
621 DispatchEx_GetNameSpaceParent
624 BOOL
dispex_query_interface(DispatchEx
*This
, REFIID riid
, void **ppv
)
626 static const IID IID_UndocumentedScriptIface
=
627 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa0}};
629 if(IsEqualGUID(&IID_IDispatch
, riid
)) {
630 TRACE("(%p)->(IID_IDispatch %p)\n", This
, ppv
);
631 *ppv
= DISPATCHEX(This
);
632 }else if(IsEqualGUID(&IID_IDispatchEx
, riid
)) {
633 TRACE("(%p)->(IID_IDispatchEx %p)\n", This
, ppv
);
634 *ppv
= DISPATCHEX(This
);
635 }else if(IsEqualGUID(&IID_UndocumentedScriptIface
, riid
)) {
636 TRACE("(%p)->(IID_UndocumentedScriptIface %p) returning NULL\n", This
, ppv
);
643 IUnknown_AddRef((IUnknown
*)*ppv
);
647 void init_dispex(DispatchEx
*dispex
, IUnknown
*outer
, dispex_static_data_t
*data
)
649 dispex
->lpIDispatchExVtbl
= &DispatchExVtbl
;
650 dispex
->outer
= outer
;