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
,
70 &DIID_DispHTMLCommentElement
,
71 &DIID_DispHTMLCurrentStyle
,
72 &DIID_DispHTMLDocument
,
73 &DIID_DispHTMLDOMTextNode
,
74 &DIID_DispHTMLElementCollection
,
75 &DIID_DispHTMLGenericElement
,
78 &DIID_DispHTMLInputElement
,
79 &DIID_DispHTMLOptionElement
,
80 &DIID_DispHTMLSelectElement
,
83 &DIID_DispHTMLTableRow
,
84 &DIID_DispHTMLUnknownElement
,
85 &DIID_DispHTMLWindow2
,
86 &IID_IHTMLBodyElement
,
87 &IID_IHTMLBodyElement2
,
88 &IID_IHTMLCommentElement
,
89 &IID_IHTMLCurrentStyle
,
94 &IID_IHTMLDOMChildrenCollection
,
97 &IID_IHTMLDOMTextNode
,
102 &IID_IHTMLElementCollection
,
103 &IID_IHTMLFrameBase2
,
104 &IID_IHTMLGenericElement
,
105 &IID_IHTMLImgElement
,
106 &IID_IHTMLInputElement
,
107 &IID_IHTMLOptionElement
,
108 &IID_IHTMLSelectElement
,
113 &IID_IHTMLTextContainer
,
114 &IID_IHTMLUniqueName
,
120 HRESULT
get_typeinfo(tid_t tid
, ITypeInfo
**typeinfo
)
127 hres
= LoadRegTypeLib(&LIBID_MSHTML
, 4, 0, LOCALE_SYSTEM_DEFAULT
, &tl
);
129 ERR("LoadRegTypeLib failed: %08x\n", hres
);
133 if(InterlockedCompareExchangePointer((void**)&typelib
, tl
, NULL
))
134 ITypeLib_Release(tl
);
137 if(!typeinfos
[tid
]) {
140 hres
= ITypeLib_GetTypeInfoOfGuid(typelib
, tid_ids
[tid
], &typeinfo
);
142 ERR("GetTypeInfoOfGuid(%s) failed: %08x\n", debugstr_guid(tid_ids
[tid
]), hres
);
146 if(InterlockedCompareExchangePointer((void**)(typeinfos
+tid
), typeinfo
, NULL
))
147 ITypeInfo_Release(typeinfo
);
150 *typeinfo
= typeinfos
[tid
];
154 void release_typelib(void)
159 while(!list_empty(&dispex_data_list
)) {
160 iter
= LIST_ENTRY(list_head(&dispex_data_list
), dispex_data_t
, entry
);
161 list_remove(&iter
->entry
);
163 for(i
=0; i
< iter
->func_cnt
; i
++)
164 SysFreeString(iter
->funcs
[i
].name
);
166 heap_free(iter
->funcs
);
167 heap_free(iter
->name_table
);
174 for(i
=0; i
< sizeof(typeinfos
)/sizeof(*typeinfos
); i
++)
176 ITypeInfo_Release(typeinfos
[i
]);
178 ITypeLib_Release(typelib
);
181 static void add_func_info(dispex_data_t
*data
, DWORD
*size
, tid_t tid
, DISPID id
, ITypeInfo
*dti
)
185 if(data
->func_cnt
&& data
->funcs
[data
->func_cnt
-1].id
== id
)
188 if(data
->func_cnt
== *size
)
189 data
->funcs
= heap_realloc(data
->funcs
, (*size
<<= 1)*sizeof(func_info_t
));
191 hres
= ITypeInfo_GetDocumentation(dti
, id
, &data
->funcs
[data
->func_cnt
].name
, NULL
, NULL
, NULL
);
195 data
->funcs
[data
->func_cnt
].id
= id
;
196 data
->funcs
[data
->func_cnt
].tid
= tid
;
201 static int dispid_cmp(const void *p1
, const void *p2
)
203 return ((func_info_t
*)p1
)->id
- ((func_info_t
*)p2
)->id
;
206 static int func_name_cmp(const void *p1
, const void *p2
)
208 return strcmpiW((*(func_info_t
**)p1
)->name
, (*(func_info_t
**)p2
)->name
);
211 static dispex_data_t
*preprocess_dispex_data(DispatchEx
*This
)
213 const tid_t
*tid
= This
->data
->iface_tids
;
220 TRACE("(%p)\n", This
);
222 hres
= get_typeinfo(This
->data
->disp_tid
, &dti
);
224 ERR("Could not get disp type info: %08x\n", hres
);
228 data
= heap_alloc(sizeof(dispex_data_t
));
230 data
->funcs
= heap_alloc(size
*sizeof(func_info_t
));
231 list_add_tail(&dispex_data_list
, &data
->entry
);
234 hres
= get_typeinfo(*tid
, &ti
);
240 hres
= ITypeInfo_GetFuncDesc(ti
, i
++, &funcdesc
);
244 add_func_info(data
, &size
, *tid
, funcdesc
->memid
, dti
);
245 ITypeInfo_ReleaseFuncDesc(ti
, funcdesc
);
251 if(!data
->func_cnt
) {
252 heap_free(data
->funcs
);
254 }else if(data
->func_cnt
!= size
) {
255 data
->funcs
= heap_realloc(data
->funcs
, data
->func_cnt
* sizeof(func_info_t
));
258 qsort(data
->funcs
, data
->func_cnt
, sizeof(func_info_t
), dispid_cmp
);
261 data
->name_table
= heap_alloc(data
->func_cnt
* sizeof(func_info_t
*));
262 for(i
=0; i
< data
->func_cnt
; i
++)
263 data
->name_table
[i
] = data
->funcs
+i
;
264 qsort(data
->name_table
, data
->func_cnt
, sizeof(func_info_t
*), func_name_cmp
);
266 data
->name_table
= NULL
;
272 static CRITICAL_SECTION cs_dispex_static_data
;
273 static CRITICAL_SECTION_DEBUG cs_dispex_static_data_dbg
=
275 0, 0, &cs_dispex_static_data
,
276 { &cs_dispex_static_data_dbg
.ProcessLocksList
, &cs_dispex_static_data_dbg
.ProcessLocksList
},
277 0, 0, { (DWORD_PTR
)(__FILE__
": dispex_static_data") }
279 static CRITICAL_SECTION cs_dispex_static_data
= { &cs_dispex_static_data_dbg
, -1, 0, 0, 0, 0 };
282 static dispex_data_t
*get_dispex_data(DispatchEx
*This
)
285 return This
->data
->data
;
287 EnterCriticalSection(&cs_dispex_static_data
);
289 if(!This
->data
->data
)
290 This
->data
->data
= preprocess_dispex_data(This
);
292 LeaveCriticalSection(&cs_dispex_static_data
);
294 return This
->data
->data
;
297 void call_disp_func(HTMLDocument
*doc
, IDispatch
*disp
)
299 DISPID named_arg
= DISPID_THIS
;
301 DISPPARAMS params
= {&arg
, &named_arg
, 1, 1};
307 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
309 FIXME("Could not get IDispatchEx interface: %08x\n", hres
);
313 V_VT(&arg
) = VT_DISPATCH
;
314 V_DISPATCH(&arg
) = (IDispatch
*)HTMLWINDOW2(doc
->window
);
316 memset(&ei
, 0, sizeof(ei
));
318 hres
= IDispatchEx_InvokeEx(dispex
, 0, GetUserDefaultLCID(), DISPATCH_METHOD
, ¶ms
, &res
, &ei
, NULL
);
319 IDispatchEx_Release(dispex
);
321 TRACE("%p returned %08x\n", disp
, hres
);
326 static inline BOOL
is_custom_dispid(DISPID id
)
328 return MSHTML_DISPID_CUSTOM_MIN
<= id
&& id
<= MSHTML_DISPID_CUSTOM_MAX
;
331 static inline BOOL
is_dynamic_dispid(DISPID id
)
333 return DISPID_DYNPROP_0
<= id
&& id
<= DISPID_DYNPROP_MAX
;
336 #define DISPATCHEX_THIS(iface) DEFINE_THIS(DispatchEx, IDispatchEx, iface)
338 static HRESULT WINAPI
DispatchEx_QueryInterface(IDispatchEx
*iface
, REFIID riid
, void **ppv
)
340 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
342 return IUnknown_QueryInterface(This
->outer
, riid
, ppv
);
345 static ULONG WINAPI
DispatchEx_AddRef(IDispatchEx
*iface
)
347 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
349 return IUnknown_AddRef(This
->outer
);
352 static ULONG WINAPI
DispatchEx_Release(IDispatchEx
*iface
)
354 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
356 return IUnknown_Release(This
->outer
);
359 static HRESULT WINAPI
DispatchEx_GetTypeInfoCount(IDispatchEx
*iface
, UINT
*pctinfo
)
361 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
363 TRACE("(%p)->(%p)\n", This
, pctinfo
);
369 static HRESULT WINAPI
DispatchEx_GetTypeInfo(IDispatchEx
*iface
, UINT iTInfo
,
370 LCID lcid
, ITypeInfo
**ppTInfo
)
372 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
375 TRACE("(%p)->(%u %u %p)\n", This
, iTInfo
, lcid
, ppTInfo
);
377 hres
= get_typeinfo(This
->data
->disp_tid
, ppTInfo
);
381 ITypeInfo_AddRef(*ppTInfo
);
385 static HRESULT WINAPI
DispatchEx_GetIDsOfNames(IDispatchEx
*iface
, REFIID riid
,
386 LPOLESTR
*rgszNames
, UINT cNames
,
387 LCID lcid
, DISPID
*rgDispId
)
389 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
393 TRACE("(%p)->(%s %p %u %u %p)\n", This
, debugstr_guid(riid
), rgszNames
, cNames
,
396 for(i
=0; i
< cNames
; i
++) {
397 hres
= IDispatchEx_GetDispID(DISPATCHEX(This
), rgszNames
[i
], 0, rgDispId
+i
);
405 static HRESULT WINAPI
DispatchEx_Invoke(IDispatchEx
*iface
, DISPID dispIdMember
,
406 REFIID riid
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
,
407 VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
409 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
411 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This
, dispIdMember
, debugstr_guid(riid
),
412 lcid
, wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
414 return IDispatchEx_InvokeEx(DISPATCHEX(This
), dispIdMember
, lcid
, wFlags
,
415 pDispParams
, pVarResult
, pExcepInfo
, NULL
);
418 static HRESULT WINAPI
DispatchEx_GetDispID(IDispatchEx
*iface
, BSTR bstrName
, DWORD grfdex
, DISPID
*pid
)
420 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
424 TRACE("(%p)->(%s %x %p)\n", This
, debugstr_w(bstrName
), grfdex
, pid
);
426 if(grfdex
& ~(fdexNameCaseSensitive
|fdexNameEnsure
|fdexNameImplicit
))
427 FIXME("Unsupported grfdex %x\n", grfdex
);
429 data
= get_dispex_data(This
);
434 max
= data
->func_cnt
-1;
439 c
= strcmpiW(data
->name_table
[n
]->name
, bstrName
);
441 if((grfdex
& fdexNameCaseSensitive
) && strcmpW(data
->name_table
[n
]->name
, bstrName
))
444 *pid
= data
->name_table
[n
]->id
;
454 if(This
->dynamic_data
) {
457 for(i
=0; i
< This
->dynamic_data
->prop_cnt
; i
++) {
458 if(!strcmpW(This
->dynamic_data
->props
[i
].name
, bstrName
)) {
459 *pid
= DISPID_DYNPROP_0
+ i
;
465 if(This
->data
->vtbl
&& This
->data
->vtbl
->get_dispid
) {
468 hres
= This
->data
->vtbl
->get_dispid(This
->outer
, bstrName
, grfdex
, pid
);
469 if(hres
!= DISP_E_UNKNOWNNAME
)
473 if(grfdex
& fdexNameEnsure
) {
474 dispex_dynamic_data_t
*dynamic_data
;
476 TRACE("creating dynamic prop %s\n", debugstr_w(bstrName
));
478 if(This
->dynamic_data
) {
479 dynamic_data
= This
->dynamic_data
;
481 dynamic_data
= This
->dynamic_data
= heap_alloc_zero(sizeof(dispex_dynamic_data_t
));
483 return E_OUTOFMEMORY
;
486 if(!dynamic_data
->buf_size
) {
487 dynamic_data
->props
= heap_alloc(sizeof(dynamic_prop_t
)*4);
488 if(!dynamic_data
->props
)
489 return E_OUTOFMEMORY
;
490 dynamic_data
->buf_size
= 4;
491 }else if(dynamic_data
->buf_size
== dynamic_data
->prop_cnt
) {
492 dynamic_prop_t
*new_props
;
494 new_props
= heap_realloc(dynamic_data
->props
, sizeof(dynamic_prop_t
)*(dynamic_data
->buf_size
<<1));
496 return E_OUTOFMEMORY
;
498 dynamic_data
->props
= new_props
;
499 dynamic_data
->buf_size
<<= 1;
502 dynamic_data
->props
[dynamic_data
->prop_cnt
].name
= heap_strdupW(bstrName
);
503 VariantInit(&dynamic_data
->props
[dynamic_data
->prop_cnt
].var
);
504 *pid
= DISPID_DYNPROP_0
+ dynamic_data
->prop_cnt
++;
509 TRACE("not found %s\n", debugstr_w(bstrName
));
510 return DISP_E_UNKNOWNNAME
;
513 static HRESULT WINAPI
DispatchEx_InvokeEx(IDispatchEx
*iface
, DISPID id
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pdp
,
514 VARIANT
*pvarRes
, EXCEPINFO
*pei
, IServiceProvider
*pspCaller
)
516 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
524 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This
, id
, lcid
, wFlags
, pdp
, pvarRes
, pei
, pspCaller
);
526 if(is_custom_dispid(id
) && This
->data
->vtbl
&& This
->data
->vtbl
->invoke
)
527 return This
->data
->vtbl
->invoke(This
->outer
, id
, lcid
, wFlags
, pdp
, pvarRes
, pei
, pspCaller
);
529 if(wFlags
== DISPATCH_CONSTRUCT
) {
530 FIXME("DISPATCH_CONSTRUCT not implemented\n");
534 if(is_dynamic_dispid(id
)) {
535 DWORD idx
= id
- DISPID_DYNPROP_0
;
538 if(!This
->dynamic_data
|| This
->dynamic_data
->prop_cnt
<= idx
)
539 return DISP_E_UNKNOWNNAME
;
541 var
= &This
->dynamic_data
->props
[idx
].var
;
545 DISPID named_arg
= DISPID_THIS
;
546 DISPPARAMS dp
= {NULL
, &named_arg
, 0, 1};
549 if(V_VT(var
) != VT_DISPATCH
) {
550 FIXME("invoke vt %d\n", V_VT(var
));
554 if(pdp
->cNamedArgs
) {
555 FIXME("named args not supported\n");
559 dp
.rgvarg
= heap_alloc((pdp
->cArgs
+1)*sizeof(VARIANTARG
));
561 return E_OUTOFMEMORY
;
563 dp
.cArgs
= pdp
->cArgs
+1;
564 memcpy(dp
.rgvarg
+1, pdp
->rgvarg
, pdp
->cArgs
*sizeof(VARIANTARG
));
566 V_VT(dp
.rgvarg
) = VT_DISPATCH
;
567 V_DISPATCH(dp
.rgvarg
) = (IDispatch
*)DISPATCHEX(This
);
569 hres
= IDispatch_QueryInterface(V_DISPATCH(var
), &IID_IDispatchEx
, (void**)&dispex
);
570 TRACE("%s call\n", debugstr_w(This
->dynamic_data
->props
[idx
].name
));
571 if(SUCCEEDED(hres
)) {
572 hres
= IDispatchEx_InvokeEx(dispex
, DISPID_VALUE
, lcid
, wFlags
, &dp
, pvarRes
, pei
, pspCaller
);
573 IDispatchEx_Release(dispex
);
576 hres
= IDispatch_Invoke(V_DISPATCH(var
), DISPID_VALUE
, &IID_NULL
, lcid
, wFlags
, pdp
, pvarRes
, pei
, &err
);
578 TRACE("%s ret %08x\n", debugstr_w(This
->dynamic_data
->props
[idx
].name
), hres
);
580 heap_free(dp
.rgvarg
);
583 case INVOKE_PROPERTYGET
:
584 return VariantCopy(pvarRes
, var
);
585 case INVOKE_PROPERTYPUT
:
587 return VariantCopy(var
, pdp
->rgvarg
);
589 FIXME("unhandled wFlags %x\n", wFlags
);
594 data
= get_dispex_data(This
);
599 max
= data
->func_cnt
-1;
604 if(data
->funcs
[n
].id
== id
)
607 if(data
->funcs
[n
].id
< id
)
614 WARN("invalid id %x\n", id
);
615 return DISP_E_UNKNOWNNAME
;
618 hres
= get_typeinfo(data
->funcs
[n
].tid
, &ti
);
620 ERR("Could not get type info: %08x\n", hres
);
624 hres
= IUnknown_QueryInterface(This
->outer
, tid_ids
[data
->funcs
[n
].tid
], (void**)&unk
);
626 ERR("Could not get iface %s: %08x\n", debugstr_guid(tid_ids
[data
->funcs
[n
].tid
]), hres
);
630 hres
= ITypeInfo_Invoke(ti
, unk
, id
, wFlags
, pdp
, pvarRes
, pei
, &argerr
);
632 IUnknown_Release(unk
);
636 static HRESULT WINAPI
DispatchEx_DeleteMemberByName(IDispatchEx
*iface
, BSTR bstrName
, DWORD grfdex
)
638 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
639 FIXME("(%p)->(%s %x)\n", This
, debugstr_w(bstrName
), grfdex
);
643 static HRESULT WINAPI
DispatchEx_DeleteMemberByDispID(IDispatchEx
*iface
, DISPID id
)
645 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
646 FIXME("(%p)->(%x)\n", This
, id
);
650 static HRESULT WINAPI
DispatchEx_GetMemberProperties(IDispatchEx
*iface
, DISPID id
, DWORD grfdexFetch
, DWORD
*pgrfdex
)
652 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
653 FIXME("(%p)->(%x %x %p)\n", This
, id
, grfdexFetch
, pgrfdex
);
657 static HRESULT WINAPI
DispatchEx_GetMemberName(IDispatchEx
*iface
, DISPID id
, BSTR
*pbstrName
)
659 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
660 FIXME("(%p)->(%x %p)\n", This
, id
, pbstrName
);
664 static HRESULT WINAPI
DispatchEx_GetNextDispID(IDispatchEx
*iface
, DWORD grfdex
, DISPID id
, DISPID
*pid
)
666 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
667 FIXME("(%p)->(%x %x %p)\n", This
, grfdex
, id
, pid
);
671 static HRESULT WINAPI
DispatchEx_GetNameSpaceParent(IDispatchEx
*iface
, IUnknown
**ppunk
)
673 DispatchEx
*This
= DISPATCHEX_THIS(iface
);
674 FIXME("(%p)->(%p)\n", This
, ppunk
);
678 #undef DISPATCHEX_THIS
680 static IDispatchExVtbl DispatchExVtbl
= {
681 DispatchEx_QueryInterface
,
684 DispatchEx_GetTypeInfoCount
,
685 DispatchEx_GetTypeInfo
,
686 DispatchEx_GetIDsOfNames
,
688 DispatchEx_GetDispID
,
690 DispatchEx_DeleteMemberByName
,
691 DispatchEx_DeleteMemberByDispID
,
692 DispatchEx_GetMemberProperties
,
693 DispatchEx_GetMemberName
,
694 DispatchEx_GetNextDispID
,
695 DispatchEx_GetNameSpaceParent
698 BOOL
dispex_query_interface(DispatchEx
*This
, REFIID riid
, void **ppv
)
700 static const IID IID_UndocumentedScriptIface
=
701 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa0}};
702 static const IID IID_IDispatchJS
=
703 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa6}};
705 if(IsEqualGUID(&IID_IDispatch
, riid
)) {
706 TRACE("(%p)->(IID_IDispatch %p)\n", This
, ppv
);
707 *ppv
= DISPATCHEX(This
);
708 }else if(IsEqualGUID(&IID_IDispatchEx
, riid
)) {
709 TRACE("(%p)->(IID_IDispatchEx %p)\n", This
, ppv
);
710 *ppv
= DISPATCHEX(This
);
711 }else if(IsEqualGUID(&IID_IDispatchJS
, riid
)) {
712 TRACE("(%p)->(IID_IDispatchJS %p) returning NULL\n", This
, ppv
);
714 }else if(IsEqualGUID(&IID_UndocumentedScriptIface
, riid
)) {
715 TRACE("(%p)->(IID_UndocumentedScriptIface %p) returning NULL\n", This
, ppv
);
722 IUnknown_AddRef((IUnknown
*)*ppv
);
726 void init_dispex(DispatchEx
*dispex
, IUnknown
*outer
, dispex_static_data_t
*data
)
728 dispex
->lpIDispatchExVtbl
= &DispatchExVtbl
;
729 dispex
->outer
= outer
;