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
33 #include "wine/debug.h"
35 #include "mshtml_private.h"
36 #include "htmlscript.h"
37 #include "pluginhost.h"
38 #include "htmlevent.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(mshtml
);
45 #define CTXARG_T DWORDLONG
46 #define IActiveScriptSiteDebugVtbl IActiveScriptSiteDebug64Vtbl
48 #define IActiveScriptParse_Release IActiveScriptParse64_Release
49 #define IActiveScriptParse_InitNew IActiveScriptParse64_InitNew
50 #define IActiveScriptParse_ParseScriptText IActiveScriptParse64_ParseScriptText
51 #define IActiveScriptParseProcedure2_Release IActiveScriptParseProcedure2_64_Release
52 #define IActiveScriptParseProcedure2_ParseProcedureText IActiveScriptParseProcedure2_64_ParseProcedureText
56 #define CTXARG_T DWORD
57 #define IActiveScriptSiteDebugVtbl IActiveScriptSiteDebug32Vtbl
59 #define IActiveScriptParse_Release IActiveScriptParse32_Release
60 #define IActiveScriptParse_InitNew IActiveScriptParse32_InitNew
61 #define IActiveScriptParse_ParseScriptText IActiveScriptParse32_ParseScriptText
62 #define IActiveScriptParseProcedure2_Release IActiveScriptParseProcedure2_32_Release
63 #define IActiveScriptParseProcedure2_ParseProcedureText IActiveScriptParseProcedure2_32_ParseProcedureText
67 static const WCHAR documentW
[] = {'d','o','c','u','m','e','n','t',0};
68 static const WCHAR windowW
[] = {'w','i','n','d','o','w',0};
69 static const WCHAR script_endW
[] = {'<','/','S','C','R','I','P','T','>',0};
70 static const WCHAR emptyW
[] = {0};
73 IActiveScriptSite IActiveScriptSite_iface
;
74 IActiveScriptSiteInterruptPoll IActiveScriptSiteInterruptPoll_iface
;
75 IActiveScriptSiteWindow IActiveScriptSiteWindow_iface
;
76 IActiveScriptSiteUIControl IActiveScriptSiteUIControl_iface
;
77 IActiveScriptSiteDebug IActiveScriptSiteDebug_iface
;
78 IServiceProvider IServiceProvider_iface
;
82 IActiveScript
*script
;
83 IActiveScriptParse
*parse
;
84 IActiveScriptParseProcedure2
*parse_proc
;
86 SCRIPTSTATE script_state
;
88 HTMLInnerWindow
*window
;
94 static void set_script_prop(ScriptHost
*script_host
, DWORD property
, VARIANT
*val
)
96 IActiveScriptProperty
*script_prop
;
99 hres
= IActiveScript_QueryInterface(script_host
->script
, &IID_IActiveScriptProperty
,
100 (void**)&script_prop
);
102 WARN("Could not get IActiveScriptProperty iface: %08x\n", hres
);
106 hres
= IActiveScriptProperty_SetProperty(script_prop
, property
, NULL
, val
);
107 IActiveScriptProperty_Release(script_prop
);
109 WARN("SetProperty(%x) failed: %08x\n", property
, hres
);
112 static BOOL
init_script_engine(ScriptHost
*script_host
)
114 IObjectSafety
*safety
;
116 DWORD supported_opts
=0, enabled_opts
=0;
120 hres
= IActiveScript_QueryInterface(script_host
->script
, &IID_IActiveScriptParse
, (void**)&script_host
->parse
);
122 WARN("Could not get IActiveScriptHost: %08x\n", hres
);
126 hres
= IActiveScript_QueryInterface(script_host
->script
, &IID_IObjectSafety
, (void**)&safety
);
128 FIXME("Could not get IObjectSafety: %08x\n", hres
);
132 hres
= IObjectSafety_GetInterfaceSafetyOptions(safety
, &IID_IActiveScriptParse
, &supported_opts
, &enabled_opts
);
134 FIXME("GetInterfaceSafetyOptions failed: %08x\n", hres
);
135 }else if(!(supported_opts
& INTERFACE_USES_DISPEX
)) {
136 FIXME("INTERFACE_USES_DISPEX is not supported\n");
138 hres
= IObjectSafety_SetInterfaceSafetyOptions(safety
, &IID_IActiveScriptParse
,
139 INTERFACESAFE_FOR_UNTRUSTED_DATA
|INTERFACE_USES_DISPEX
|INTERFACE_USES_SECURITY_MANAGER
,
140 INTERFACESAFE_FOR_UNTRUSTED_DATA
|INTERFACE_USES_DISPEX
|INTERFACE_USES_SECURITY_MANAGER
);
142 FIXME("SetInterfaceSafetyOptions failed: %08x\n", hres
);
145 IObjectSafety_Release(safety
);
151 set_script_prop(script_host
, SCRIPTPROP_INVOKEVERSIONING
, &var
);
153 V_VT(&var
) = VT_BOOL
;
154 V_BOOL(&var
) = VARIANT_TRUE
;
155 set_script_prop(script_host
, SCRIPTPROP_HACK_TRIDENTEVENTSINK
, &var
);
157 hres
= IActiveScriptParse_InitNew(script_host
->parse
);
159 WARN("InitNew failed: %08x\n", hres
);
163 hres
= IActiveScript_SetScriptSite(script_host
->script
, &script_host
->IActiveScriptSite_iface
);
165 WARN("SetScriptSite failed: %08x\n", hres
);
166 IActiveScript_Close(script_host
->script
);
170 hres
= IActiveScript_GetScriptState(script_host
->script
, &state
);
172 WARN("GetScriptState failed: %08x\n", hres
);
173 else if(state
!= SCRIPTSTATE_INITIALIZED
)
174 FIXME("state = %x\n", state
);
176 hres
= IActiveScript_SetScriptState(script_host
->script
, SCRIPTSTATE_STARTED
);
178 WARN("Starting script failed: %08x\n", hres
);
182 hres
= IActiveScript_AddNamedItem(script_host
->script
, windowW
,
183 SCRIPTITEM_ISVISIBLE
|SCRIPTITEM_ISSOURCE
|SCRIPTITEM_GLOBALMEMBERS
);
184 if(SUCCEEDED(hres
)) {
185 V_VT(&var
) = VT_BOOL
;
186 V_BOOL(&var
) = VARIANT_TRUE
;
187 set_script_prop(script_host
, SCRIPTPROP_ABBREVIATE_GLOBALNAME_RESOLUTION
, &var
);
189 WARN("AddNamedItem failed: %08x\n", hres
);
192 hres
= IActiveScript_QueryInterface(script_host
->script
, &IID_IActiveScriptParseProcedure2
,
193 (void**)&script_host
->parse_proc
);
195 /* FIXME: QI for IActiveScriptParseProcedure */
196 WARN("Could not get IActiveScriptParseProcedure iface: %08x\n", hres
);
202 static void release_script_engine(ScriptHost
*This
)
207 switch(This
->script_state
) {
208 case SCRIPTSTATE_CONNECTED
:
209 IActiveScript_SetScriptState(This
->script
, SCRIPTSTATE_DISCONNECTED
);
211 case SCRIPTSTATE_STARTED
:
212 case SCRIPTSTATE_DISCONNECTED
:
213 case SCRIPTSTATE_INITIALIZED
:
214 IActiveScript_Close(This
->script
);
217 if(This
->parse_proc
) {
218 IActiveScriptParseProcedure2_Release(This
->parse_proc
);
219 This
->parse_proc
= NULL
;
223 IActiveScriptParse_Release(This
->parse
);
228 IActiveScript_Release(This
->script
);
230 This
->script_state
= SCRIPTSTATE_UNINITIALIZED
;
233 void connect_scripts(HTMLInnerWindow
*window
)
237 LIST_FOR_EACH_ENTRY(iter
, &window
->script_hosts
, ScriptHost
, entry
) {
238 if(iter
->script_state
== SCRIPTSTATE_STARTED
)
239 IActiveScript_SetScriptState(iter
->script
, SCRIPTSTATE_CONNECTED
);
243 static inline ScriptHost
*impl_from_IActiveScriptSite(IActiveScriptSite
*iface
)
245 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSite_iface
);
248 static HRESULT WINAPI
ActiveScriptSite_QueryInterface(IActiveScriptSite
*iface
, REFIID riid
, void **ppv
)
250 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
254 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
255 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
256 *ppv
= &This
->IActiveScriptSite_iface
;
257 }else if(IsEqualGUID(&IID_IActiveScriptSite
, riid
)) {
258 TRACE("(%p)->(IID_IActiveScriptSite %p)\n", This
, ppv
);
259 *ppv
= &This
->IActiveScriptSite_iface
;
260 }else if(IsEqualGUID(&IID_IActiveScriptSiteInterruptPoll
, riid
)) {
261 TRACE("(%p)->(IID_IActiveScriptSiteInterruprtPoll %p)\n", This
, ppv
);
262 *ppv
= &This
->IActiveScriptSiteInterruptPoll_iface
;
263 }else if(IsEqualGUID(&IID_IActiveScriptSiteWindow
, riid
)) {
264 TRACE("(%p)->(IID_IActiveScriptSiteWindow %p)\n", This
, ppv
);
265 *ppv
= &This
->IActiveScriptSiteWindow_iface
;
266 }else if(IsEqualGUID(&IID_IActiveScriptSiteUIControl
, riid
)) {
267 TRACE("(%p)->(IID_IActiveScriptSiteUIControl %p)\n", This
, ppv
);
268 *ppv
= &This
->IActiveScriptSiteUIControl_iface
;
269 }else if(IsEqualGUID(&IID_IActiveScriptSiteDebug
, riid
)) {
270 TRACE("(%p)->(IID_IActiveScriptSiteDebug %p)\n", This
, ppv
);
271 *ppv
= &This
->IActiveScriptSiteDebug_iface
;
272 }else if(IsEqualGUID(&IID_IServiceProvider
, riid
)) {
273 TRACE("(%p)->(IID_IServiceProvider %p)\n", This
, ppv
);
274 *ppv
= &This
->IServiceProvider_iface
;
275 }else if(IsEqualGUID(&IID_ICanHandleException
, riid
)) {
276 TRACE("(%p)->(IID_ICanHandleException not supported %p)\n", This
, ppv
);
277 return E_NOINTERFACE
;
279 FIXME("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
280 return E_NOINTERFACE
;
283 IUnknown_AddRef((IUnknown
*)*ppv
);
287 static ULONG WINAPI
ActiveScriptSite_AddRef(IActiveScriptSite
*iface
)
289 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
290 LONG ref
= InterlockedIncrement(&This
->ref
);
292 TRACE("(%p) ref=%d\n", This
, ref
);
297 static ULONG WINAPI
ActiveScriptSite_Release(IActiveScriptSite
*iface
)
299 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
300 LONG ref
= InterlockedDecrement(&This
->ref
);
302 TRACE("(%p) ref=%d\n", This
, ref
);
305 release_script_engine(This
);
307 list_remove(&This
->entry
);
314 static HRESULT WINAPI
ActiveScriptSite_GetLCID(IActiveScriptSite
*iface
, LCID
*plcid
)
316 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
318 TRACE("(%p)->(%p)\n", This
, plcid
);
320 *plcid
= GetUserDefaultLCID();
324 static HRESULT WINAPI
ActiveScriptSite_GetItemInfo(IActiveScriptSite
*iface
, LPCOLESTR pstrName
,
325 DWORD dwReturnMask
, IUnknown
**ppiunkItem
, ITypeInfo
**ppti
)
327 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
329 TRACE("(%p)->(%s %x %p %p)\n", This
, debugstr_w(pstrName
), dwReturnMask
, ppiunkItem
, ppti
);
331 if(dwReturnMask
!= SCRIPTINFO_IUNKNOWN
) {
332 FIXME("Unsupported mask %x\n", dwReturnMask
);
338 if(strcmpW(pstrName
, windowW
))
339 return DISP_E_MEMBERNOTFOUND
;
344 /* FIXME: Return proxy object */
345 *ppiunkItem
= (IUnknown
*)&This
->window
->base
.IHTMLWindow2_iface
;
346 IUnknown_AddRef(*ppiunkItem
);
351 static HRESULT WINAPI
ActiveScriptSite_GetDocVersionString(IActiveScriptSite
*iface
, BSTR
*pbstrVersion
)
353 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
354 FIXME("(%p)->(%p)\n", This
, pbstrVersion
);
358 static HRESULT WINAPI
ActiveScriptSite_OnScriptTerminate(IActiveScriptSite
*iface
,
359 const VARIANT
*pvarResult
, const EXCEPINFO
*pexcepinfo
)
361 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
362 FIXME("(%p)->(%p %p)\n", This
, pvarResult
, pexcepinfo
);
366 static HRESULT WINAPI
ActiveScriptSite_OnStateChange(IActiveScriptSite
*iface
, SCRIPTSTATE ssScriptState
)
368 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
370 TRACE("(%p)->(%x)\n", This
, ssScriptState
);
372 This
->script_state
= ssScriptState
;
376 static HRESULT WINAPI
ActiveScriptSite_OnScriptError(IActiveScriptSite
*iface
, IActiveScriptError
*pscripterror
)
378 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
379 FIXME("(%p)->(%p)\n", This
, pscripterror
);
383 static HRESULT WINAPI
ActiveScriptSite_OnEnterScript(IActiveScriptSite
*iface
)
385 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
387 TRACE("(%p)->()\n", This
);
392 static HRESULT WINAPI
ActiveScriptSite_OnLeaveScript(IActiveScriptSite
*iface
)
394 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
396 TRACE("(%p)->()\n", This
);
401 static const IActiveScriptSiteVtbl ActiveScriptSiteVtbl
= {
402 ActiveScriptSite_QueryInterface
,
403 ActiveScriptSite_AddRef
,
404 ActiveScriptSite_Release
,
405 ActiveScriptSite_GetLCID
,
406 ActiveScriptSite_GetItemInfo
,
407 ActiveScriptSite_GetDocVersionString
,
408 ActiveScriptSite_OnScriptTerminate
,
409 ActiveScriptSite_OnStateChange
,
410 ActiveScriptSite_OnScriptError
,
411 ActiveScriptSite_OnEnterScript
,
412 ActiveScriptSite_OnLeaveScript
415 static inline ScriptHost
*impl_from_IActiveScriptSiteInterruptPoll(IActiveScriptSiteInterruptPoll
*iface
)
417 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSiteInterruptPoll_iface
);
420 static HRESULT WINAPI
ActiveScriptSiteInterruptPoll_QueryInterface(IActiveScriptSiteInterruptPoll
*iface
,
421 REFIID riid
, void **ppv
)
423 ScriptHost
*This
= impl_from_IActiveScriptSiteInterruptPoll(iface
);
424 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
427 static ULONG WINAPI
ActiveScriptSiteInterruptPoll_AddRef(IActiveScriptSiteInterruptPoll
*iface
)
429 ScriptHost
*This
= impl_from_IActiveScriptSiteInterruptPoll(iface
);
430 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
433 static ULONG WINAPI
ActiveScriptSiteInterruptPoll_Release(IActiveScriptSiteInterruptPoll
*iface
)
435 ScriptHost
*This
= impl_from_IActiveScriptSiteInterruptPoll(iface
);
436 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
439 static HRESULT WINAPI
ActiveScriptSiteInterruptPoll_QueryContinue(IActiveScriptSiteInterruptPoll
*iface
)
441 ScriptHost
*This
= impl_from_IActiveScriptSiteInterruptPoll(iface
);
443 TRACE("(%p)\n", This
);
448 static const IActiveScriptSiteInterruptPollVtbl ActiveScriptSiteInterruptPollVtbl
= {
449 ActiveScriptSiteInterruptPoll_QueryInterface
,
450 ActiveScriptSiteInterruptPoll_AddRef
,
451 ActiveScriptSiteInterruptPoll_Release
,
452 ActiveScriptSiteInterruptPoll_QueryContinue
455 static inline ScriptHost
*impl_from_IActiveScriptSiteWindow(IActiveScriptSiteWindow
*iface
)
457 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSiteWindow_iface
);
460 static HRESULT WINAPI
ActiveScriptSiteWindow_QueryInterface(IActiveScriptSiteWindow
*iface
,
461 REFIID riid
, void **ppv
)
463 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
464 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
467 static ULONG WINAPI
ActiveScriptSiteWindow_AddRef(IActiveScriptSiteWindow
*iface
)
469 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
470 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
473 static ULONG WINAPI
ActiveScriptSiteWindow_Release(IActiveScriptSiteWindow
*iface
)
475 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
476 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
479 static HRESULT WINAPI
ActiveScriptSiteWindow_GetWindow(IActiveScriptSiteWindow
*iface
, HWND
*phwnd
)
481 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
483 TRACE("(%p)->(%p)\n", This
, phwnd
);
485 if(!This
->window
|| !This
->window
->base
.outer_window
|| !This
->window
->base
.outer_window
->doc_obj
)
488 *phwnd
= This
->window
->base
.outer_window
->doc_obj
->hwnd
;
492 static HRESULT WINAPI
ActiveScriptSiteWindow_EnableModeless(IActiveScriptSiteWindow
*iface
, BOOL fEnable
)
494 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
495 FIXME("(%p)->(%x)\n", This
, fEnable
);
499 static const IActiveScriptSiteWindowVtbl ActiveScriptSiteWindowVtbl
= {
500 ActiveScriptSiteWindow_QueryInterface
,
501 ActiveScriptSiteWindow_AddRef
,
502 ActiveScriptSiteWindow_Release
,
503 ActiveScriptSiteWindow_GetWindow
,
504 ActiveScriptSiteWindow_EnableModeless
507 static inline ScriptHost
*impl_from_IActiveScriptSiteUIControl(IActiveScriptSiteUIControl
*iface
)
509 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSiteUIControl_iface
);
512 static HRESULT WINAPI
ActiveScriptSiteUIControl_QueryInterface(IActiveScriptSiteUIControl
*iface
, REFIID riid
, void **ppv
)
514 ScriptHost
*This
= impl_from_IActiveScriptSiteUIControl(iface
);
515 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
518 static ULONG WINAPI
ActiveScriptSiteUIControl_AddRef(IActiveScriptSiteUIControl
*iface
)
520 ScriptHost
*This
= impl_from_IActiveScriptSiteUIControl(iface
);
521 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
524 static ULONG WINAPI
ActiveScriptSiteUIControl_Release(IActiveScriptSiteUIControl
*iface
)
526 ScriptHost
*This
= impl_from_IActiveScriptSiteUIControl(iface
);
527 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
530 static HRESULT WINAPI
ActiveScriptSiteUIControl_GetUIBehavior(IActiveScriptSiteUIControl
*iface
, SCRIPTUICITEM UicItem
,
531 SCRIPTUICHANDLING
*pUicHandling
)
533 ScriptHost
*This
= impl_from_IActiveScriptSiteUIControl(iface
);
535 WARN("(%p)->(%d %p) semi-stub\n", This
, UicItem
, pUicHandling
);
537 *pUicHandling
= SCRIPTUICHANDLING_ALLOW
;
541 static const IActiveScriptSiteUIControlVtbl ActiveScriptSiteUIControlVtbl
= {
542 ActiveScriptSiteUIControl_QueryInterface
,
543 ActiveScriptSiteUIControl_AddRef
,
544 ActiveScriptSiteUIControl_Release
,
545 ActiveScriptSiteUIControl_GetUIBehavior
548 static inline ScriptHost
*impl_from_IActiveScriptSiteDebug(IActiveScriptSiteDebug
*iface
)
550 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSiteDebug_iface
);
553 static HRESULT WINAPI
ActiveScriptSiteDebug_QueryInterface(IActiveScriptSiteDebug
*iface
,
554 REFIID riid
, void **ppv
)
556 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
557 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
560 static ULONG WINAPI
ActiveScriptSiteDebug_AddRef(IActiveScriptSiteDebug
*iface
)
562 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
563 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
566 static ULONG WINAPI
ActiveScriptSiteDebug_Release(IActiveScriptSiteDebug
*iface
)
568 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
569 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
572 static HRESULT WINAPI
ActiveScriptSiteDebug_GetDocumentContextFromPosition(IActiveScriptSiteDebug
*iface
,
573 CTXARG_T dwSourceContext
, ULONG uCharacterOffset
, ULONG uNumChars
, IDebugDocumentContext
**ppsc
)
575 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
576 FIXME("(%p)->(%s %u %u %p)\n", This
, wine_dbgstr_longlong(dwSourceContext
), uCharacterOffset
,
581 static HRESULT WINAPI
ActiveScriptSiteDebug_GetApplication(IActiveScriptSiteDebug
*iface
, IDebugApplication
**ppda
)
583 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
584 FIXME("(%p)->(%p)\n", This
, ppda
);
588 static HRESULT WINAPI
ActiveScriptSiteDebug_GetRootApplicationNode(IActiveScriptSiteDebug
*iface
,
589 IDebugApplicationNode
**ppdanRoot
)
591 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
592 FIXME("(%p)->(%p)\n", This
, ppdanRoot
);
596 static HRESULT WINAPI
ActiveScriptSiteDebug_OnScriptErrorDebug(IActiveScriptSiteDebug
*iface
,
597 IActiveScriptErrorDebug
*pErrorDebug
, BOOL
*pfEnterDebugger
, BOOL
*pfCallOnScriptErrorWhenContinuing
)
599 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
600 FIXME("(%p)->(%p %p %p)\n", This
, pErrorDebug
, pfEnterDebugger
, pfCallOnScriptErrorWhenContinuing
);
604 static const IActiveScriptSiteDebugVtbl ActiveScriptSiteDebugVtbl
= {
605 ActiveScriptSiteDebug_QueryInterface
,
606 ActiveScriptSiteDebug_AddRef
,
607 ActiveScriptSiteDebug_Release
,
608 ActiveScriptSiteDebug_GetDocumentContextFromPosition
,
609 ActiveScriptSiteDebug_GetApplication
,
610 ActiveScriptSiteDebug_GetRootApplicationNode
,
611 ActiveScriptSiteDebug_OnScriptErrorDebug
614 static inline ScriptHost
*impl_from_IServiceProvider(IServiceProvider
*iface
)
616 return CONTAINING_RECORD(iface
, ScriptHost
, IServiceProvider_iface
);
619 static HRESULT WINAPI
ASServiceProvider_QueryInterface(IServiceProvider
*iface
, REFIID riid
, void **ppv
)
621 ScriptHost
*This
= impl_from_IServiceProvider(iface
);
622 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
625 static ULONG WINAPI
ASServiceProvider_AddRef(IServiceProvider
*iface
)
627 ScriptHost
*This
= impl_from_IServiceProvider(iface
);
628 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
631 static ULONG WINAPI
ASServiceProvider_Release(IServiceProvider
*iface
)
633 ScriptHost
*This
= impl_from_IServiceProvider(iface
);
634 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
637 static HRESULT WINAPI
ASServiceProvider_QueryService(IServiceProvider
*iface
, REFGUID guidService
,
638 REFIID riid
, void **ppv
)
640 ScriptHost
*This
= impl_from_IServiceProvider(iface
);
642 if(IsEqualGUID(&SID_SInternetHostSecurityManager
, guidService
)) {
643 TRACE("(%p)->(SID_SInternetHostSecurityManager)\n", This
);
645 if(!This
->window
|| !This
->window
->doc
)
646 return E_NOINTERFACE
;
648 return IInternetHostSecurityManager_QueryInterface(&This
->window
->doc
->IInternetHostSecurityManager_iface
,
652 if(IsEqualGUID(&SID_SContainerDispatch
, guidService
)) {
653 TRACE("(%p)->(SID_SContainerDispatch)\n", This
);
655 if(!This
->window
|| !This
->window
->doc
)
656 return E_NOINTERFACE
;
658 return IHTMLDocument2_QueryInterface(&This
->window
->doc
->basedoc
.IHTMLDocument2_iface
, riid
, ppv
);
661 FIXME("(%p)->(%s %s %p)\n", This
, debugstr_guid(guidService
), debugstr_guid(riid
), ppv
);
662 return E_NOINTERFACE
;
665 static const IServiceProviderVtbl ASServiceProviderVtbl
= {
666 ASServiceProvider_QueryInterface
,
667 ASServiceProvider_AddRef
,
668 ASServiceProvider_Release
,
669 ASServiceProvider_QueryService
672 static ScriptHost
*create_script_host(HTMLInnerWindow
*window
, const GUID
*guid
)
677 ret
= heap_alloc_zero(sizeof(*ret
));
681 ret
->IActiveScriptSite_iface
.lpVtbl
= &ActiveScriptSiteVtbl
;
682 ret
->IActiveScriptSiteInterruptPoll_iface
.lpVtbl
= &ActiveScriptSiteInterruptPollVtbl
;
683 ret
->IActiveScriptSiteWindow_iface
.lpVtbl
= &ActiveScriptSiteWindowVtbl
;
684 ret
->IActiveScriptSiteUIControl_iface
.lpVtbl
= &ActiveScriptSiteUIControlVtbl
;
685 ret
->IActiveScriptSiteDebug_iface
.lpVtbl
= &ActiveScriptSiteDebugVtbl
;
686 ret
->IServiceProvider_iface
.lpVtbl
= &ASServiceProviderVtbl
;
688 ret
->window
= window
;
689 ret
->script_state
= SCRIPTSTATE_UNINITIALIZED
;
692 list_add_tail(&window
->script_hosts
, &ret
->entry
);
694 hres
= CoCreateInstance(&ret
->guid
, NULL
, CLSCTX_INPROC_SERVER
|CLSCTX_INPROC_HANDLER
,
695 &IID_IActiveScript
, (void**)&ret
->script
);
697 WARN("Could not load script engine: %08x\n", hres
);
698 else if(!init_script_engine(ret
))
699 release_script_engine(ret
);
704 static void parse_text(ScriptHost
*script_host
, LPCWSTR text
)
710 TRACE("%s\n", debugstr_w(text
));
713 memset(&excepinfo
, 0, sizeof(excepinfo
));
715 hres
= IActiveScriptParse_ParseScriptText(script_host
->parse
, text
, windowW
, NULL
, script_endW
,
716 0, 0, SCRIPTTEXT_ISVISIBLE
|SCRIPTTEXT_HOSTMANAGESSOURCE
,
721 WARN("<<< %08x\n", hres
);
725 static void parse_extern_script(ScriptHost
*script_host
, LPCWSTR src
)
731 static const WCHAR wine_schemaW
[] = {'w','i','n','e',':'};
733 if(strlenW(src
) > sizeof(wine_schemaW
)/sizeof(WCHAR
) && !memcmp(src
, wine_schemaW
, sizeof(wine_schemaW
)))
734 src
+= sizeof(wine_schemaW
)/sizeof(WCHAR
);
736 hres
= CreateURLMoniker(NULL
, src
, &mon
);
740 hres
= bind_mon_to_wstr(script_host
->window
, mon
, &text
);
741 IMoniker_Release(mon
);
745 parse_text(script_host
, text
);
750 static void parse_inline_script(ScriptHost
*script_host
, HTMLScriptElement
*script_elem
)
752 const PRUnichar
*text
;
756 nsAString_Init(&text_str
, NULL
);
757 nsres
= nsIDOMHTMLScriptElement_GetText(script_elem
->nsscript
, &text_str
);
758 nsAString_GetData(&text_str
, &text
);
760 if(NS_FAILED(nsres
)) {
761 ERR("GetText failed: %08x\n", nsres
);
763 parse_text(script_host
, text
);
766 nsAString_Finish(&text_str
);
769 static void parse_script_elem(ScriptHost
*script_host
, HTMLScriptElement
*script_elem
)
771 nsAString src_str
, event_str
;
772 const PRUnichar
*src
;
775 nsAString_Init(&event_str
, NULL
);
776 nsres
= nsIDOMHTMLScriptElement_GetEvent(script_elem
->nsscript
, &event_str
);
777 if(NS_SUCCEEDED(nsres
)) {
778 const PRUnichar
*event
;
780 nsAString_GetData(&event_str
, &event
);
782 TRACE("deferring event %s script evaluation\n", debugstr_w(event
));
783 nsAString_Finish(&event_str
);
787 ERR("GetAttribute(event) failed: %08x\n", nsres
);
789 nsAString_Finish(&event_str
);
791 nsAString_Init(&src_str
, NULL
);
792 nsres
= nsIDOMHTMLScriptElement_GetSrc(script_elem
->nsscript
, &src_str
);
793 nsAString_GetData(&src_str
, &src
);
795 if(NS_FAILED(nsres
)) {
796 ERR("GetSrc failed: %08x\n", nsres
);
798 script_elem
->parsed
= TRUE
;
799 parse_extern_script(script_host
, src
);
801 parse_inline_script(script_host
, script_elem
);
804 nsAString_Finish(&src_str
);
807 static GUID
get_default_script_guid(HTMLInnerWindow
*window
)
809 /* If not specified, we should use very first script host that was created for the page (or JScript if none) */
810 return list_empty(&window
->script_hosts
)
812 : LIST_ENTRY(list_head(&window
->script_hosts
), ScriptHost
, entry
)->guid
;
815 static BOOL
get_guid_from_type(LPCWSTR type
, GUID
*guid
)
817 const WCHAR text_javascriptW
[] =
818 {'t','e','x','t','/','j','a','v','a','s','c','r','i','p','t',0};
819 const WCHAR text_vbscriptW
[] =
820 {'t','e','x','t','/','v','b','s','c','r','i','p','t',0};
822 /* FIXME: Handle more types */
823 if(!strcmpiW(type
, text_javascriptW
)) {
824 *guid
= CLSID_JScript
;
825 }else if(!strcmpiW(type
, text_vbscriptW
)) {
826 *guid
= CLSID_VBScript
;
828 FIXME("Unknown type %s\n", debugstr_w(type
));
835 static BOOL
get_guid_from_language(LPCWSTR type
, GUID
*guid
)
839 hres
= CLSIDFromProgID(type
, guid
);
843 /* FIXME: Check CATID_ActiveScriptParse */
848 static BOOL
get_script_guid(HTMLInnerWindow
*window
, nsIDOMHTMLScriptElement
*nsscript
, GUID
*guid
)
850 nsAString attr_str
, val_str
;
854 static const PRUnichar languageW
[] = {'l','a','n','g','u','a','g','e',0};
856 nsAString_Init(&val_str
, NULL
);
858 nsres
= nsIDOMHTMLScriptElement_GetType(nsscript
, &val_str
);
859 if(NS_SUCCEEDED(nsres
)) {
860 const PRUnichar
*type
;
862 nsAString_GetData(&val_str
, &type
);
864 ret
= get_guid_from_type(type
, guid
);
865 nsAString_Finish(&val_str
);
869 ERR("GetType failed: %08x\n", nsres
);
872 nsAString_InitDepend(&attr_str
, languageW
);
873 nsres
= nsIDOMHTMLScriptElement_GetAttribute(nsscript
, &attr_str
, &val_str
);
874 nsAString_Finish(&attr_str
);
875 if(NS_SUCCEEDED(nsres
)) {
876 const PRUnichar
*language
;
878 nsAString_GetData(&val_str
, &language
);
881 ret
= get_guid_from_language(language
, guid
);
883 *guid
= get_default_script_guid(window
);
887 ERR("GetAttribute(language) failed: %08x\n", nsres
);
890 nsAString_Finish(&val_str
);
895 static ScriptHost
*get_script_host(HTMLInnerWindow
*window
, const GUID
*guid
)
899 LIST_FOR_EACH_ENTRY(iter
, &window
->script_hosts
, ScriptHost
, entry
) {
900 if(IsEqualGUID(guid
, &iter
->guid
))
904 return create_script_host(window
, guid
);
907 static ScriptHost
*get_elem_script_host(HTMLInnerWindow
*window
, HTMLScriptElement
*script_elem
)
911 if(!get_script_guid(window
, script_elem
->nsscript
, &guid
)) {
912 WARN("Could not find script GUID\n");
916 if(IsEqualGUID(&CLSID_JScript
, &guid
)
917 && (!window
->base
.outer_window
|| window
->base
.outer_window
->scriptmode
!= SCRIPTMODE_ACTIVESCRIPT
)) {
918 TRACE("Ignoring JScript\n");
922 return get_script_host(window
, &guid
);
925 void doc_insert_script(HTMLInnerWindow
*window
, HTMLScriptElement
*script_elem
)
927 ScriptHost
*script_host
;
929 script_host
= get_elem_script_host(window
, script_elem
);
933 if(script_host
->parse
)
934 parse_script_elem(script_host
, script_elem
);
937 IDispatch
*script_parse_event(HTMLInnerWindow
*window
, LPCWSTR text
)
939 ScriptHost
*script_host
;
945 static const WCHAR delimiterW
[] = {'\"',0};
947 TRACE("%s\n", debugstr_w(text
));
949 for(ptr
= text
; isalnumW(*ptr
); ptr
++);
954 language
= heap_alloc((ptr
-text
+1)*sizeof(WCHAR
));
958 memcpy(language
, text
, (ptr
-text
)*sizeof(WCHAR
));
959 language
[ptr
-text
] = 0;
961 b
= get_guid_from_language(language
, &guid
);
966 WARN("Could not find language\n");
973 guid
= get_default_script_guid(window
);
976 if(IsEqualGUID(&CLSID_JScript
, &guid
)
977 && (!window
->base
.outer_window
|| window
->base
.outer_window
->scriptmode
!= SCRIPTMODE_ACTIVESCRIPT
)) {
978 TRACE("Ignoring JScript\n");
982 script_host
= get_script_host(window
, &guid
);
983 if(!script_host
|| !script_host
->parse_proc
)
986 hres
= IActiveScriptParseProcedure2_ParseProcedureText(script_host
->parse_proc
, ptr
, NULL
, emptyW
,
987 NULL
, NULL
, delimiterW
, 0 /* FIXME */, 0,
988 SCRIPTPROC_HOSTMANAGESSOURCE
|SCRIPTPROC_IMPLICIT_THIS
|SCRIPTPROC_IMPLICIT_PARENTS
, &disp
);
990 WARN("ParseProcedureText failed: %08x\n", hres
);
994 TRACE("ret %p\n", disp
);
998 HRESULT
exec_script(HTMLInnerWindow
*window
, const WCHAR
*code
, const WCHAR
*lang
, VARIANT
*ret
)
1000 ScriptHost
*script_host
;
1005 static const WCHAR delimW
[] = {'"',0};
1007 if(!get_guid_from_language(lang
, &guid
)) {
1008 WARN("Could not find script GUID\n");
1009 return CO_E_CLASSSTRING
;
1012 script_host
= get_script_host(window
, &guid
);
1014 FIXME("No script host\n");
1018 if(!script_host
->parse
) {
1019 FIXME("script_host->parse == NULL\n");
1023 memset(&ei
, 0, sizeof(ei
));
1025 hres
= IActiveScriptParse_ParseScriptText(script_host
->parse
, code
, NULL
, NULL
, delimW
, 0, 0, SCRIPTTEXT_ISVISIBLE
, ret
, &ei
);
1029 WARN("<<< %08x\n", hres
);
1034 IDispatch
*get_script_disp(ScriptHost
*script_host
)
1039 if(!script_host
->script
)
1042 hres
= IActiveScript_GetScriptDispatch(script_host
->script
, windowW
, &disp
);
1049 static event_target_t
**find_event_target(HTMLDocumentNode
*doc
, HTMLScriptElement
*script_elem
, HTMLDOMNode
**ret_target_node
)
1051 HTMLDOMNode
*target_node
= NULL
;
1052 event_target_t
**target
= NULL
;
1053 const PRUnichar
*target_id
;
1054 nsAString target_id_str
;
1058 nsAString_Init(&target_id_str
, NULL
);
1059 nsres
= nsIDOMHTMLScriptElement_GetHtmlFor(script_elem
->nsscript
, &target_id_str
);
1060 if(NS_FAILED(nsres
)) {
1061 ERR("GetScriptFor failed: %08x\n", nsres
);
1062 nsAString_Finish(&target_id_str
);
1066 nsAString_GetData(&target_id_str
, &target_id
);
1068 FIXME("Empty for attribute\n");
1069 }else if(!strcmpW(target_id
, documentW
)) {
1070 target
= &doc
->node
.event_target
;
1071 target_node
= &doc
->node
;
1072 IHTMLDOMNode_AddRef(&target_node
->IHTMLDOMNode_iface
);
1073 }else if(!strcmpW(target_id
, windowW
)) {
1074 target
= &doc
->body_event_target
;
1076 HTMLElement
*target_elem
;
1078 hres
= get_doc_elem_by_id(doc
, target_id
, &target_elem
);
1079 if(SUCCEEDED(hres
) && target_elem
) {
1080 target_node
= &target_elem
->node
;
1081 target
= &target_elem
->node
.event_target
;
1084 nsAString_Finish(&target_id_str
);
1086 *ret_target_node
= target_node
;
1090 static BOOL
parse_event_str(WCHAR
*event
, const WCHAR
**args
)
1094 TRACE("%s\n", debugstr_w(event
));
1096 for(ptr
= event
; isalnumW(*ptr
); ptr
++);
1107 while(isalnumW(*ptr
) || isspaceW(*ptr
) || *ptr
== ',')
1117 static IDispatch
*parse_event_elem(HTMLDocumentNode
*doc
, HTMLScriptElement
*script_elem
, WCHAR
**ret_event
)
1119 ScriptHost
*script_host
;
1120 WCHAR
*event
= NULL
;
1127 if(script_elem
->parsed
)
1130 script_host
= get_elem_script_host(doc
->window
, script_elem
);
1131 if(!script_host
|| !script_host
->parse_proc
)
1134 nsAString_Init(&nsstr
, NULL
);
1135 nsres
= nsIDOMHTMLScriptElement_GetEvent(script_elem
->nsscript
, &nsstr
);
1136 if(NS_SUCCEEDED(nsres
)) {
1137 const PRUnichar
*event_val
;
1139 nsAString_GetData(&nsstr
, &event_val
);
1140 event
= heap_strdupW(event_val
);
1142 nsAString_Finish(&nsstr
);
1146 if(!parse_event_str(event
, &args
)) {
1147 WARN("parsing %s failed\n", debugstr_w(event
));
1152 nsAString_Init(&nsstr
, NULL
);
1153 nsres
= nsIDOMHTMLScriptElement_GetText(script_elem
->nsscript
, &nsstr
);
1154 if(NS_SUCCEEDED(nsres
)) {
1155 const PRUnichar
*text
;
1157 nsAString_GetData(&nsstr
, &text
);
1158 hres
= IActiveScriptParseProcedure2_ParseProcedureText(script_host
->parse_proc
, text
, args
,
1159 emptyW
, NULL
, NULL
, script_endW
, 0, 0,
1160 SCRIPTPROC_HOSTMANAGESSOURCE
|SCRIPTPROC_IMPLICIT_THIS
|SCRIPTPROC_IMPLICIT_PARENTS
, &disp
);
1164 ERR("GetText failed: %08x\n", nsres
);
1167 nsAString_Finish(&nsstr
);
1177 void bind_event_scripts(HTMLDocumentNode
*doc
)
1179 HTMLPluginContainer
*plugin_container
;
1180 nsIDOMHTMLScriptElement
*nsscript
;
1181 HTMLScriptElement
*script_elem
;
1182 event_target_t
**event_target
;
1183 nsIDOMNodeList
*node_list
;
1184 HTMLDOMNode
*target_node
;
1185 nsIDOMNode
*script_node
;
1186 nsAString selector_str
;
1187 IDispatch
*event_disp
;
1193 static const PRUnichar selectorW
[] = {'s','c','r','i','p','t','[','e','v','e','n','t',']',0};
1200 nsAString_InitDepend(&selector_str
, selectorW
);
1201 nsres
= nsIDOMNodeSelector_QuerySelectorAll(doc
->nsnode_selector
, &selector_str
, &node_list
);
1202 nsAString_Finish(&selector_str
);
1203 if(NS_FAILED(nsres
)) {
1204 ERR("QuerySelectorAll failed: %08x\n", nsres
);
1211 nsres
= nsIDOMNodeList_GetLength(node_list
, &length
);
1212 assert(nsres
== NS_OK
);
1214 for(i
=0; i
< length
; i
++) {
1215 nsres
= nsIDOMNodeList_Item(node_list
, i
, &script_node
);
1216 if(NS_FAILED(nsres
) || !script_node
) {
1217 ERR("Item(%d) failed: %08x\n", i
, nsres
);
1221 nsres
= nsIDOMNode_QueryInterface(script_node
, &IID_nsIDOMHTMLScriptElement
, (void**)&nsscript
);
1222 assert(nsres
== NS_OK
);
1223 nsIDOMNode_Release(script_node
);
1225 hres
= script_elem_from_nsscript(doc
, nsscript
, &script_elem
);
1229 event_disp
= parse_event_elem(doc
, script_elem
, &event
);
1231 event_target
= find_event_target(doc
, script_elem
, &target_node
);
1234 hres
= IHTMLDOMNode_QueryInterface(&target_node
->IHTMLDOMNode_iface
, &IID_HTMLPluginContainer
,
1235 (void**)&plugin_container
);
1237 hres
= E_NOINTERFACE
;
1240 bind_activex_event(doc
, plugin_container
, event
, event_disp
);
1242 bind_node_event(doc
, event_target
, target_node
, event
, event_disp
);
1245 IHTMLDOMNode_Release(&target_node
->IHTMLDOMNode_iface
);
1249 IDispatch_Release(event_disp
);
1252 IHTMLScriptElement_Release(&script_elem
->IHTMLScriptElement_iface
);
1255 nsIDOMNodeList_Release(node_list
);
1258 BOOL
find_global_prop(HTMLInnerWindow
*window
, BSTR name
, DWORD flags
, ScriptHost
**ret_host
, DISPID
*ret_id
)
1260 IDispatchEx
*dispex
;
1265 LIST_FOR_EACH_ENTRY(iter
, &window
->script_hosts
, ScriptHost
, entry
) {
1266 disp
= get_script_disp(iter
);
1270 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
1271 if(SUCCEEDED(hres
)) {
1272 hres
= IDispatchEx_GetDispID(dispex
, name
, flags
& (~fdexNameEnsure
), ret_id
);
1273 IDispatchEx_Release(dispex
);
1275 FIXME("No IDispatchEx\n");
1279 IDispatch_Release(disp
);
1280 if(SUCCEEDED(hres
)) {
1289 static BOOL
is_jscript_available(void)
1291 static BOOL available
, checked
;
1295 HRESULT hres
= CoGetClassObject(&CLSID_JScript
, CLSCTX_INPROC_SERVER
, NULL
, &IID_IUnknown
, (void**)&unk
);
1297 if(SUCCEEDED(hres
)) {
1299 IUnknown_Release(unk
);
1309 void set_script_mode(HTMLOuterWindow
*window
, SCRIPTMODE mode
)
1311 nsIWebBrowserSetup
*setup
;
1314 if(mode
== SCRIPTMODE_ACTIVESCRIPT
&& !is_jscript_available()) {
1315 TRACE("jscript.dll not available\n");
1316 window
->scriptmode
= SCRIPTMODE_GECKO
;
1320 window
->scriptmode
= mode
;
1322 if(!window
->doc_obj
->nscontainer
|| !window
->doc_obj
->nscontainer
->webbrowser
)
1325 nsres
= nsIWebBrowser_QueryInterface(window
->doc_obj
->nscontainer
->webbrowser
,
1326 &IID_nsIWebBrowserSetup
, (void**)&setup
);
1327 if(NS_SUCCEEDED(nsres
)) {
1328 nsres
= nsIWebBrowserSetup_SetProperty(setup
, SETUP_ALLOW_JAVASCRIPT
,
1329 window
->scriptmode
== SCRIPTMODE_GECKO
);
1331 if(NS_SUCCEEDED(nsres
))
1332 nsres
= nsIWebBrowserSetup_SetProperty(setup
, SETUP_DISABLE_NOSCRIPT
, TRUE
);
1334 nsIWebBrowserSetup_Release(setup
);
1337 if(NS_FAILED(nsres
))
1338 ERR("JavaScript setup failed: %08x\n", nsres
);
1341 void release_script_hosts(HTMLInnerWindow
*window
)
1343 script_queue_entry_t
*queue_iter
;
1346 while(!list_empty(&window
->script_queue
)) {
1347 queue_iter
= LIST_ENTRY(list_head(&window
->script_queue
), script_queue_entry_t
, entry
);
1349 list_remove(&queue_iter
->entry
);
1350 IHTMLScriptElement_Release(&queue_iter
->script
->IHTMLScriptElement_iface
);
1351 heap_free(queue_iter
);
1354 while(!list_empty(&window
->script_hosts
)) {
1355 iter
= LIST_ENTRY(list_head(&window
->script_hosts
), ScriptHost
, entry
);
1357 release_script_engine(iter
);
1358 list_remove(&iter
->entry
);
1359 iter
->window
= NULL
;
1360 IActiveScriptSite_Release(&iter
->IActiveScriptSite_iface
);