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
[] = {
69 &DIID_DispDOMChildrenCollection
,
71 &DIID_DispHTMLCommentElement
,
72 &DIID_DispHTMLCurrentStyle
,
73 &DIID_DispHTMLDocument
,
74 &DIID_DispHTMLDOMTextNode
,
75 &DIID_DispHTMLElementCollection
,
76 &DIID_DispHTMLGenericElement
,
79 &DIID_DispHTMLInputElement
,
80 &DIID_DispHTMLLocation
,
81 &DIID_DispHTMLNavigator
,
82 &DIID_DispHTMLOptionElement
,
83 &DIID_DispHTMLSelectElement
,
86 &DIID_DispHTMLTableRow
,
87 &DIID_DispHTMLUnknownElement
,
88 &DIID_DispHTMLWindow2
,
89 &IID_IHTMLBodyElement
,
90 &IID_IHTMLBodyElement2
,
91 &IID_IHTMLCommentElement
,
92 &IID_IHTMLCurrentStyle
,
93 &IID_IHTMLCurrentStyle2
,
94 &IID_IHTMLCurrentStyle3
,
95 &IID_IHTMLCurrentStyle4
,
100 &IID_IHTMLDOMChildrenCollection
,
103 &IID_IHTMLDOMTextNode
,
108 &IID_IHTMLElementCollection
,
110 &IID_IHTMLFrameBase2
,
111 &IID_IHTMLGenericElement
,
112 &IID_IHTMLImgElement
,
113 &IID_IHTMLInputElement
,
115 &IID_IHTMLOptionElement
,
116 &IID_IHTMLSelectElement
,
123 &IID_IHTMLTextContainer
,
124 &IID_IHTMLUniqueName
,
130 static HRESULT
get_typeinfo(tid_t tid
, ITypeInfo
**typeinfo
)
137 hres
= LoadRegTypeLib(&LIBID_MSHTML
, 4, 0, LOCALE_SYSTEM_DEFAULT
, &tl
);
139 ERR("LoadRegTypeLib failed: %08x\n", hres
);
143 if(InterlockedCompareExchangePointer((void**)&typelib
, tl
, NULL
))
144 ITypeLib_Release(tl
);
147 if(!typeinfos
[tid
]) {
150 hres
= ITypeLib_GetTypeInfoOfGuid(typelib
, tid_ids
[tid
], &typeinfo
);
152 ERR("GetTypeInfoOfGuid(%s) failed: %08x\n", debugstr_guid(tid_ids
[tid
]), hres
);
156 if(InterlockedCompareExchangePointer((void**)(typeinfos
+tid
), typeinfo
, NULL
))
157 ITypeInfo_Release(typeinfo
);
160 *typeinfo
= typeinfos
[tid
];
164 void release_typelib(void)
169 while(!list_empty(&dispex_data_list
)) {
170 iter
= LIST_ENTRY(list_head(&dispex_data_list
), dispex_data_t
, entry
);
171 list_remove(&iter
->entry
);
173 for(i
=0; i
< iter
->func_cnt
; i
++)
174 SysFreeString(iter
->funcs
[i
].name
);
176 heap_free(iter
->funcs
);
177 heap_free(iter
->name_table
);
184 for(i
=0; i
< sizeof(typeinfos
)/sizeof(*typeinfos
); i
++)
186 ITypeInfo_Release(typeinfos
[i
]);
188 ITypeLib_Release(typelib
);
191 static void add_func_info(dispex_data_t
*data
, DWORD
*size
, tid_t tid
, DISPID id
, ITypeInfo
*dti
)
195 if(data
->func_cnt
&& data
->funcs
[data
->func_cnt
-1].id
== id
)
198 if(data
->func_cnt
== *size
)
199 data
->funcs
= heap_realloc(data
->funcs
, (*size
<<= 1)*sizeof(func_info_t
));
201 hres
= ITypeInfo_GetDocumentation(dti
, id
, &data
->funcs
[data
->func_cnt
].name
, NULL
, NULL
, NULL
);
205 data
->funcs
[data
->func_cnt
].id
= id
;
206 data
->funcs
[data
->func_cnt
].tid
= tid
;
211 static int dispid_cmp(const void *p1
, const void *p2
)
213 return ((func_info_t
*)p1
)->id
- ((func_info_t
*)p2
)->id
;
216 static int func_name_cmp(const void *p1
, const void *p2
)
218 return strcmpiW((*(func_info_t
**)p1
)->name
, (*(func_info_t
**)p2
)->name
);
221 static dispex_data_t
*preprocess_dispex_data(DispatchEx
*This
)
223 const tid_t
*tid
= This
->data
->iface_tids
;
230 TRACE("(%p)\n", This
);
232 hres
= get_typeinfo(This
->data
->disp_tid
, &dti
);
234 ERR("Could not get disp type info: %08x\n", hres
);
238 data
= heap_alloc(sizeof(dispex_data_t
));
240 data
->funcs
= heap_alloc(size
*sizeof(func_info_t
));
241 list_add_tail(&dispex_data_list
, &data
->entry
);
244 hres
= get_typeinfo(*tid
, &ti
);
250 hres
= ITypeInfo_GetFuncDesc(ti
, i
++, &funcdesc
);
254 add_func_info(data
, &size
, *tid
, funcdesc
->memid
, dti
);
255 ITypeInfo_ReleaseFuncDesc(ti
, funcdesc
);
261 if(!data
->func_cnt
) {
262 heap_free(data
->funcs
);
264 }else if(data
->func_cnt
!= size
) {
265 data
->funcs
= heap_realloc(data
->funcs
, data
->func_cnt
* sizeof(func_info_t
));
268 qsort(data
->funcs
, data
->func_cnt
, sizeof(func_info_t
), dispid_cmp
);
271 data
->name_table
= heap_alloc(data
->func_cnt
* sizeof(func_info_t
*));
272 for(i
=0; i
< data
->func_cnt
; i
++)
273 data
->name_table
[i
] = data
->funcs
+i
;
274 qsort(data
->name_table
, data
->func_cnt
, sizeof(func_info_t
*), func_name_cmp
);
276 data
->name_table
= NULL
;
282 static CRITICAL_SECTION cs_dispex_static_data
;
283 static CRITICAL_SECTION_DEBUG cs_dispex_static_data_dbg
=
285 0, 0, &cs_dispex_static_data
,
286 { &cs_dispex_static_data_dbg
.ProcessLocksList
, &cs_dispex_static_data_dbg
.ProcessLocksList
},
287 0, 0, { (DWORD_PTR
)(__FILE__
": dispex_static_data") }
289 static CRITICAL_SECTION cs_dispex_static_data
= { &cs_dispex_static_data_dbg
, -1, 0, 0, 0, 0 };
292 static dispex_data_t
*get_dispex_data(DispatchEx
*This
)
295 return This
->data
->data
;
297 EnterCriticalSection(&cs_dispex_static_data
);
299 if(!This
->data
->data
)
300 This
->data
->data
= preprocess_dispex_data(This
);
302 LeaveCriticalSection(&cs_dispex_static_data
);
304 return This
->data
->data
;
307 void call_disp_func(HTMLDocument
*doc
, IDispatch
*disp
, IDispatch
*this_obj
)
309 DISPID named_arg
= DISPID_THIS
;
311 DISPPARAMS params
= {&arg
, &named_arg
, 1, 1};
317 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
319 FIXME("Could not get IDispatchEx interface: %08x\n", hres
);
323 V_VT(&arg
) = VT_DISPATCH
;
324 V_DISPATCH(&arg
) = this_obj
;
326 memset(&ei
, 0, sizeof(ei
));
328 hres
= IDispatchEx_InvokeEx(dispex
, 0, GetUserDefaultLCID(), DISPATCH_METHOD
, ¶ms
, &res
, &ei
, NULL
);
329 IDispatchEx_Release(dispex
);
331 TRACE("%p returned %08x\n", disp
, hres
);
336 static inline BOOL
is_custom_dispid(DISPID id
)
338 return MSHTML_DISPID_CUSTOM_MIN
<= id
&& id
<= MSHTML_DISPID_CUSTOM_MAX
;
341 static inline BOOL
is_dynamic_dispid(DISPID id
)
343 return DISPID_DYNPROP_0
<= id
&& id
<= DISPID_DYNPROP_MAX
;
346 static HRESULT
get_dynamic_prop(DispatchEx
*This
, const WCHAR
*name
, BOOL alloc
, dynamic_prop_t
**ret
)
348 dispex_dynamic_data_t
*data
= This
->dynamic_data
;
353 for(i
=0; i
< data
->prop_cnt
; i
++) {
354 if(!strcmpW(data
->props
[i
].name
, name
)) {
355 *ret
= data
->props
+i
;
362 TRACE("creating dynamic prop %s\n", debugstr_w(name
));
365 data
= This
->dynamic_data
= heap_alloc_zero(sizeof(dispex_dynamic_data_t
));
367 return E_OUTOFMEMORY
;
370 if(!data
->buf_size
) {
371 data
->props
= heap_alloc(sizeof(dynamic_prop_t
)*4);
373 return E_OUTOFMEMORY
;
375 }else if(data
->buf_size
== data
->prop_cnt
) {
376 dynamic_prop_t
*new_props
;
378 new_props
= heap_realloc(data
->props
, sizeof(dynamic_prop_t
)*(data
->buf_size
<<1));
380 return E_OUTOFMEMORY
;
382 data
->props
= new_props
;
383 data
->buf_size
<<= 1;
386 data
->props
[data
->prop_cnt
].name
= heap_strdupW(name
);
387 VariantInit(&data
->props
[data
->prop_cnt
].var
);
388 *ret
= data
->props
+ data
->prop_cnt
++;
393 TRACE("not found %s\n", debugstr_w(name
));
394 return DISP_E_UNKNOWNNAME
;
397 HRESULT
dispex_get_dprop_ref(DispatchEx
*This
, const WCHAR
*name
, BOOL alloc
, VARIANT
**ret
)
399 dynamic_prop_t
*prop
;
402 hres
= get_dynamic_prop(This
, name
, alloc
, &prop
);
410 #define DISPATCHEX_THIS(iface) DEFINE_THIS(DispatchEx, IDispatchEx, iface)
412 static HRESULT WINAPI
DispatchEx_QueryInterface(IDispatchEx
*iface
, REFIID riid
, void **ppv
)
414 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
416 return IUnknown_QueryInterface(This
->outer
, riid
, ppv
);
419 static ULONG WINAPI
DispatchEx_AddRef(IDispatchEx
*iface
)
421 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
423 return IUnknown_AddRef(This
->outer
);
426 static ULONG WINAPI
DispatchEx_Release(IDispatchEx
*iface
)
428 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
430 return IUnknown_Release(This
->outer
);
433 static HRESULT WINAPI
DispatchEx_GetTypeInfoCount(IDispatchEx
*iface
, UINT
*pctinfo
)
435 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
437 TRACE("(%p)->(%p)\n", This
, pctinfo
);
443 static HRESULT WINAPI
DispatchEx_GetTypeInfo(IDispatchEx
*iface
, UINT iTInfo
,
444 LCID lcid
, ITypeInfo
**ppTInfo
)
446 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
449 TRACE("(%p)->(%u %u %p)\n", This
, iTInfo
, lcid
, ppTInfo
);
451 hres
= get_typeinfo(This
->data
->disp_tid
, ppTInfo
);
455 ITypeInfo_AddRef(*ppTInfo
);
459 static HRESULT WINAPI
DispatchEx_GetIDsOfNames(IDispatchEx
*iface
, REFIID riid
,
460 LPOLESTR
*rgszNames
, UINT cNames
,
461 LCID lcid
, DISPID
*rgDispId
)
463 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
467 TRACE("(%p)->(%s %p %u %u %p)\n", This
, debugstr_guid(riid
), rgszNames
, cNames
,
470 for(i
=0; i
< cNames
; i
++) {
471 hres
= IDispatchEx_GetDispID(DISPATCHEX(This
), rgszNames
[i
], 0, rgDispId
+i
);
479 static HRESULT WINAPI
DispatchEx_Invoke(IDispatchEx
*iface
, DISPID dispIdMember
,
480 REFIID riid
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
,
481 VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
483 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
485 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This
, dispIdMember
, debugstr_guid(riid
),
486 lcid
, wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
488 return IDispatchEx_InvokeEx(DISPATCHEX(This
), dispIdMember
, lcid
, wFlags
,
489 pDispParams
, pVarResult
, pExcepInfo
, NULL
);
492 static HRESULT WINAPI
DispatchEx_GetDispID(IDispatchEx
*iface
, BSTR bstrName
, DWORD grfdex
, DISPID
*pid
)
494 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
495 dynamic_prop_t
*dprop
;
500 TRACE("(%p)->(%s %x %p)\n", This
, debugstr_w(bstrName
), grfdex
, pid
);
502 if(grfdex
& ~(fdexNameCaseSensitive
|fdexNameEnsure
|fdexNameImplicit
))
503 FIXME("Unsupported grfdex %x\n", grfdex
);
505 data
= get_dispex_data(This
);
510 max
= data
->func_cnt
-1;
515 c
= strcmpiW(data
->name_table
[n
]->name
, bstrName
);
517 if((grfdex
& fdexNameCaseSensitive
) && strcmpW(data
->name_table
[n
]->name
, bstrName
))
520 *pid
= data
->name_table
[n
]->id
;
530 if(This
->data
->vtbl
&& This
->data
->vtbl
->get_dispid
) {
533 hres
= This
->data
->vtbl
->get_dispid(This
->outer
, bstrName
, grfdex
, pid
);
534 if(hres
!= DISP_E_UNKNOWNNAME
)
538 hres
= get_dynamic_prop(This
, bstrName
, grfdex
&fdexNameEnsure
, &dprop
);
542 *pid
= DISPID_DYNPROP_0
+ (dprop
- This
->dynamic_data
->props
);
546 static HRESULT WINAPI
DispatchEx_InvokeEx(IDispatchEx
*iface
, DISPID id
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pdp
,
547 VARIANT
*pvarRes
, EXCEPINFO
*pei
, IServiceProvider
*pspCaller
)
549 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
557 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This
, id
, lcid
, wFlags
, pdp
, pvarRes
, pei
, pspCaller
);
559 if(is_custom_dispid(id
) && This
->data
->vtbl
&& This
->data
->vtbl
->invoke
)
560 return This
->data
->vtbl
->invoke(This
->outer
, id
, lcid
, wFlags
, pdp
, pvarRes
, pei
, pspCaller
);
562 if(wFlags
== DISPATCH_CONSTRUCT
) {
563 FIXME("DISPATCH_CONSTRUCT not implemented\n");
567 if(is_dynamic_dispid(id
)) {
568 DWORD idx
= id
- DISPID_DYNPROP_0
;
571 if(!This
->dynamic_data
|| This
->dynamic_data
->prop_cnt
<= idx
)
572 return DISP_E_UNKNOWNNAME
;
574 var
= &This
->dynamic_data
->props
[idx
].var
;
578 DISPID named_arg
= DISPID_THIS
;
579 DISPPARAMS dp
= {NULL
, &named_arg
, 0, 1};
582 if(V_VT(var
) != VT_DISPATCH
) {
583 FIXME("invoke vt %d\n", V_VT(var
));
587 if(pdp
->cNamedArgs
) {
588 FIXME("named args not supported\n");
592 dp
.rgvarg
= heap_alloc((pdp
->cArgs
+1)*sizeof(VARIANTARG
));
594 return E_OUTOFMEMORY
;
596 dp
.cArgs
= pdp
->cArgs
+1;
597 memcpy(dp
.rgvarg
+1, pdp
->rgvarg
, pdp
->cArgs
*sizeof(VARIANTARG
));
599 V_VT(dp
.rgvarg
) = VT_DISPATCH
;
600 V_DISPATCH(dp
.rgvarg
) = (IDispatch
*)DISPATCHEX(This
);
602 hres
= IDispatch_QueryInterface(V_DISPATCH(var
), &IID_IDispatchEx
, (void**)&dispex
);
603 TRACE("%s call\n", debugstr_w(This
->dynamic_data
->props
[idx
].name
));
604 if(SUCCEEDED(hres
)) {
605 hres
= IDispatchEx_InvokeEx(dispex
, DISPID_VALUE
, lcid
, wFlags
, &dp
, pvarRes
, pei
, pspCaller
);
606 IDispatchEx_Release(dispex
);
609 hres
= IDispatch_Invoke(V_DISPATCH(var
), DISPID_VALUE
, &IID_NULL
, lcid
, wFlags
, pdp
, pvarRes
, pei
, &err
);
611 TRACE("%s ret %08x\n", debugstr_w(This
->dynamic_data
->props
[idx
].name
), hres
);
613 heap_free(dp
.rgvarg
);
616 case INVOKE_PROPERTYGET
:
617 return VariantCopy(pvarRes
, var
);
618 case INVOKE_PROPERTYPUT
:
620 return VariantCopy(var
, pdp
->rgvarg
);
622 FIXME("unhandled wFlags %x\n", wFlags
);
627 data
= get_dispex_data(This
);
632 max
= data
->func_cnt
-1;
637 if(data
->funcs
[n
].id
== id
)
640 if(data
->funcs
[n
].id
< id
)
647 WARN("invalid id %x\n", id
);
648 return DISP_E_UNKNOWNNAME
;
651 hres
= get_typeinfo(data
->funcs
[n
].tid
, &ti
);
653 ERR("Could not get type info: %08x\n", hres
);
657 hres
= IUnknown_QueryInterface(This
->outer
, tid_ids
[data
->funcs
[n
].tid
], (void**)&unk
);
659 ERR("Could not get iface %s: %08x\n", debugstr_guid(tid_ids
[data
->funcs
[n
].tid
]), hres
);
663 hres
= ITypeInfo_Invoke(ti
, unk
, id
, wFlags
, pdp
, pvarRes
, pei
, &argerr
);
665 IUnknown_Release(unk
);
669 static HRESULT WINAPI
DispatchEx_DeleteMemberByName(IDispatchEx
*iface
, BSTR bstrName
, DWORD grfdex
)
671 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
672 FIXME("(%p)->(%s %x)\n", This
, debugstr_w(bstrName
), grfdex
);
676 static HRESULT WINAPI
DispatchEx_DeleteMemberByDispID(IDispatchEx
*iface
, DISPID id
)
678 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
679 FIXME("(%p)->(%x)\n", This
, id
);
683 static HRESULT WINAPI
DispatchEx_GetMemberProperties(IDispatchEx
*iface
, DISPID id
, DWORD grfdexFetch
, DWORD
*pgrfdex
)
685 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
686 FIXME("(%p)->(%x %x %p)\n", This
, id
, grfdexFetch
, pgrfdex
);
690 static HRESULT WINAPI
DispatchEx_GetMemberName(IDispatchEx
*iface
, DISPID id
, BSTR
*pbstrName
)
692 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
693 FIXME("(%p)->(%x %p)\n", This
, id
, pbstrName
);
697 static HRESULT WINAPI
DispatchEx_GetNextDispID(IDispatchEx
*iface
, DWORD grfdex
, DISPID id
, DISPID
*pid
)
699 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
700 FIXME("(%p)->(%x %x %p)\n", This
, grfdex
, id
, pid
);
704 static HRESULT WINAPI
DispatchEx_GetNameSpaceParent(IDispatchEx
*iface
, IUnknown
**ppunk
)
706 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
707 FIXME("(%p)->(%p)\n", This
, ppunk
);
711 #undef DISPATCHEX_THIS
713 static IDispatchExVtbl DispatchExVtbl
= {
714 DispatchEx_QueryInterface
,
717 DispatchEx_GetTypeInfoCount
,
718 DispatchEx_GetTypeInfo
,
719 DispatchEx_GetIDsOfNames
,
721 DispatchEx_GetDispID
,
723 DispatchEx_DeleteMemberByName
,
724 DispatchEx_DeleteMemberByDispID
,
725 DispatchEx_GetMemberProperties
,
726 DispatchEx_GetMemberName
,
727 DispatchEx_GetNextDispID
,
728 DispatchEx_GetNameSpaceParent
731 BOOL
dispex_query_interface(DispatchEx
*This
, REFIID riid
, void **ppv
)
733 static const IID IID_UndocumentedScriptIface
=
734 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa0}};
735 static const IID IID_IDispatchJS
=
736 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa6}};
738 if(IsEqualGUID(&IID_IDispatch
, riid
)) {
739 TRACE("(%p)->(IID_IDispatch %p)\n", This
, ppv
);
740 *ppv
= DISPATCHEX(This
);
741 }else if(IsEqualGUID(&IID_IDispatchEx
, riid
)) {
742 TRACE("(%p)->(IID_IDispatchEx %p)\n", This
, ppv
);
743 *ppv
= DISPATCHEX(This
);
744 }else if(IsEqualGUID(&IID_IDispatchJS
, riid
)) {
745 TRACE("(%p)->(IID_IDispatchJS %p) returning NULL\n", This
, ppv
);
747 }else if(IsEqualGUID(&IID_UndocumentedScriptIface
, riid
)) {
748 TRACE("(%p)->(IID_UndocumentedScriptIface %p) returning NULL\n", This
, ppv
);
755 IUnknown_AddRef((IUnknown
*)*ppv
);
759 void init_dispex(DispatchEx
*dispex
, IUnknown
*outer
, dispex_static_data_t
*data
)
761 dispex
->lpIDispatchExVtbl
= &DispatchExVtbl
;
762 dispex
->outer
= outer
;