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
31 #include "wine/debug.h"
33 #include "mshtml_private.h"
34 #include "htmlscript.h"
35 #include "pluginhost.h"
36 #include "htmlevent.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(mshtml
);
43 #define CTXARG_T DWORDLONG
44 #define IActiveScriptSiteDebugVtbl IActiveScriptSiteDebug64Vtbl
46 #define IActiveScriptParse_Release IActiveScriptParse64_Release
47 #define IActiveScriptParse_InitNew IActiveScriptParse64_InitNew
48 #define IActiveScriptParse_ParseScriptText IActiveScriptParse64_ParseScriptText
49 #define IActiveScriptParseProcedure2_Release IActiveScriptParseProcedure2_64_Release
50 #define IActiveScriptParseProcedure2_ParseProcedureText IActiveScriptParseProcedure2_64_ParseProcedureText
54 #define CTXARG_T DWORD
55 #define IActiveScriptSiteDebugVtbl IActiveScriptSiteDebug32Vtbl
57 #define IActiveScriptParse_Release IActiveScriptParse32_Release
58 #define IActiveScriptParse_InitNew IActiveScriptParse32_InitNew
59 #define IActiveScriptParse_ParseScriptText IActiveScriptParse32_ParseScriptText
60 #define IActiveScriptParseProcedure2_Release IActiveScriptParseProcedure2_32_Release
61 #define IActiveScriptParseProcedure2_ParseProcedureText IActiveScriptParseProcedure2_32_ParseProcedureText
65 /* See jscript.h in jscript.dll. */
66 #define SCRIPTLANGUAGEVERSION_HTML 0x400
67 #define SCRIPTLANGUAGEVERSION_ES5 0x102
68 #define SCRIPTLANGUAGEVERSION_ES6 0x103
71 IActiveScriptSite IActiveScriptSite_iface
;
72 IActiveScriptSiteInterruptPoll IActiveScriptSiteInterruptPoll_iface
;
73 IActiveScriptSiteWindow IActiveScriptSiteWindow_iface
;
74 IActiveScriptSiteUIControl IActiveScriptSiteUIControl_iface
;
75 IActiveScriptSiteDebug IActiveScriptSiteDebug_iface
;
76 IServiceProvider IServiceProvider_iface
;
80 IActiveScript
*script
;
81 IActiveScriptParse
*parse
;
82 IActiveScriptParseProcedure2
*parse_proc
;
84 SCRIPTSTATE script_state
;
86 HTMLInnerWindow
*window
;
92 static ScriptHost
*get_elem_script_host(HTMLInnerWindow
*,HTMLScriptElement
*);
94 static BOOL
set_script_prop(IActiveScript
*script
, DWORD property
, VARIANT
*val
)
96 IActiveScriptProperty
*script_prop
;
99 hres
= IActiveScript_QueryInterface(script
, &IID_IActiveScriptProperty
,
100 (void**)&script_prop
);
102 WARN("Could not get IActiveScriptProperty iface: %08lx\n", hres
);
106 hres
= IActiveScriptProperty_SetProperty(script_prop
, property
, NULL
, val
);
107 IActiveScriptProperty_Release(script_prop
);
109 WARN("SetProperty(%lx) failed: %08lx\n", property
, hres
);
116 static BOOL
init_script_engine(ScriptHost
*script_host
, IActiveScript
*script
)
118 compat_mode_t compat_mode
;
119 IObjectSafety
*safety
;
121 DWORD supported_opts
=0, enabled_opts
=0, script_mode
;
125 hres
= IActiveScript_QueryInterface(script
, &IID_IActiveScriptParse
, (void**)&script_host
->parse
);
127 WARN("Could not get IActiveScriptParse: %08lx\n", hres
);
131 hres
= IActiveScript_QueryInterface(script
, &IID_IObjectSafety
, (void**)&safety
);
133 FIXME("Could not get IObjectSafety: %08lx\n", hres
);
137 hres
= IObjectSafety_GetInterfaceSafetyOptions(safety
, &IID_IActiveScriptParse
, &supported_opts
, &enabled_opts
);
139 FIXME("GetInterfaceSafetyOptions failed: %08lx\n", hres
);
140 }else if(!(supported_opts
& INTERFACE_USES_DISPEX
)) {
141 FIXME("INTERFACE_USES_DISPEX is not supported\n");
143 hres
= IObjectSafety_SetInterfaceSafetyOptions(safety
, &IID_IActiveScriptParse
,
144 INTERFACESAFE_FOR_UNTRUSTED_DATA
|INTERFACE_USES_DISPEX
|INTERFACE_USES_SECURITY_MANAGER
,
145 INTERFACESAFE_FOR_UNTRUSTED_DATA
|INTERFACE_USES_DISPEX
|INTERFACE_USES_SECURITY_MANAGER
);
147 FIXME("SetInterfaceSafetyOptions failed: %08lx\n", hres
);
150 IObjectSafety_Release(safety
);
154 compat_mode
= lock_document_mode(script_host
->window
->doc
);
155 script_mode
= compat_mode
< COMPAT_MODE_IE8
? SCRIPTLANGUAGEVERSION_5_7
: SCRIPTLANGUAGEVERSION_5_8
;
156 if(IsEqualGUID(&script_host
->guid
, &CLSID_JScript
)) {
157 if(compat_mode
>= COMPAT_MODE_IE11
)
158 script_mode
= SCRIPTLANGUAGEVERSION_ES6
;
159 else if(compat_mode
>= COMPAT_MODE_IE9
)
160 script_mode
= SCRIPTLANGUAGEVERSION_ES5
;
161 script_mode
|= SCRIPTLANGUAGEVERSION_HTML
;
164 V_I4(&var
) = script_mode
;
165 if(!set_script_prop(script
, SCRIPTPROP_INVOKEVERSIONING
, &var
) && (script_mode
& SCRIPTLANGUAGEVERSION_HTML
)) {
166 /* If this failed, we're most likely using native jscript. */
167 WARN("Failed to set script mode to HTML version.\n");
168 V_I4(&var
) = compat_mode
< COMPAT_MODE_IE8
? SCRIPTLANGUAGEVERSION_5_7
: SCRIPTLANGUAGEVERSION_5_8
;
169 set_script_prop(script
, SCRIPTPROP_INVOKEVERSIONING
, &var
);
172 V_VT(&var
) = VT_BOOL
;
173 V_BOOL(&var
) = VARIANT_TRUE
;
174 set_script_prop(script
, SCRIPTPROP_HACK_TRIDENTEVENTSINK
, &var
);
176 hres
= IActiveScriptParse_InitNew(script_host
->parse
);
178 WARN("InitNew failed: %08lx\n", hres
);
182 hres
= IActiveScript_SetScriptSite(script
, &script_host
->IActiveScriptSite_iface
);
184 WARN("SetScriptSite failed: %08lx\n", hres
);
185 IActiveScript_Close(script
);
189 hres
= IActiveScript_GetScriptState(script
, &state
);
191 WARN("GetScriptState failed: %08lx\n", hres
);
192 else if(state
!= SCRIPTSTATE_INITIALIZED
)
193 FIXME("state = %x\n", state
);
195 hres
= IActiveScript_SetScriptState(script
, SCRIPTSTATE_STARTED
);
197 WARN("Starting script failed: %08lx\n", hres
);
201 hres
= IActiveScript_AddNamedItem(script
, L
"window",
202 SCRIPTITEM_ISVISIBLE
|SCRIPTITEM_ISSOURCE
|SCRIPTITEM_GLOBALMEMBERS
);
203 if(SUCCEEDED(hres
)) {
204 ScriptHost
*first_host
;
206 V_VT(&var
) = VT_BOOL
;
207 V_BOOL(&var
) = VARIANT_TRUE
;
209 LIST_FOR_EACH_ENTRY(first_host
, &script_host
->window
->script_hosts
, ScriptHost
, entry
) {
210 if(first_host
->script
) {
211 V_BOOL(&var
) = VARIANT_FALSE
;
215 set_script_prop(script
, SCRIPTPROP_ABBREVIATE_GLOBALNAME_RESOLUTION
, &var
);
217 /* if this was second engine initialized, also set it to first engine, since it used to be TRUE */
219 struct list
*iter
= &first_host
->entry
;
220 BOOL is_second_init
= TRUE
;
222 while((iter
= list_next(&script_host
->window
->script_hosts
, iter
))) {
223 if(LIST_ENTRY(iter
, ScriptHost
, entry
)->script
) {
224 is_second_init
= FALSE
;
229 set_script_prop(first_host
->script
, SCRIPTPROP_ABBREVIATE_GLOBALNAME_RESOLUTION
, &var
);
232 WARN("AddNamedItem failed: %08lx\n", hres
);
235 hres
= IActiveScript_QueryInterface(script
, &IID_IActiveScriptParseProcedure2
,
236 (void**)&script_host
->parse_proc
);
238 /* FIXME: QI for IActiveScriptParseProcedure */
239 WARN("Could not get IActiveScriptParseProcedure iface: %08lx\n", hres
);
245 static void release_script_engine(ScriptHost
*This
)
250 switch(This
->script_state
) {
251 case SCRIPTSTATE_CONNECTED
:
252 IActiveScript_SetScriptState(This
->script
, SCRIPTSTATE_DISCONNECTED
);
254 case SCRIPTSTATE_STARTED
:
255 case SCRIPTSTATE_DISCONNECTED
:
256 case SCRIPTSTATE_INITIALIZED
:
257 IActiveScript_Close(This
->script
);
260 unlink_ref(&This
->parse_proc
);
261 unlink_ref(&This
->parse
);
264 IActiveScript_Release(This
->script
);
266 This
->script_state
= SCRIPTSTATE_UNINITIALIZED
;
269 void connect_scripts(HTMLInnerWindow
*window
)
273 LIST_FOR_EACH_ENTRY(iter
, &window
->script_hosts
, ScriptHost
, entry
) {
274 if(iter
->script_state
== SCRIPTSTATE_STARTED
)
275 IActiveScript_SetScriptState(iter
->script
, SCRIPTSTATE_CONNECTED
);
279 static inline ScriptHost
*impl_from_IActiveScriptSite(IActiveScriptSite
*iface
)
281 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSite_iface
);
284 static HRESULT WINAPI
ActiveScriptSite_QueryInterface(IActiveScriptSite
*iface
, REFIID riid
, void **ppv
)
286 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
290 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
291 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
292 *ppv
= &This
->IActiveScriptSite_iface
;
293 }else if(IsEqualGUID(&IID_IActiveScriptSite
, riid
)) {
294 TRACE("(%p)->(IID_IActiveScriptSite %p)\n", This
, ppv
);
295 *ppv
= &This
->IActiveScriptSite_iface
;
296 }else if(IsEqualGUID(&IID_IActiveScriptSiteInterruptPoll
, riid
)) {
297 TRACE("(%p)->(IID_IActiveScriptSiteInterruprtPoll %p)\n", This
, ppv
);
298 *ppv
= &This
->IActiveScriptSiteInterruptPoll_iface
;
299 }else if(IsEqualGUID(&IID_IActiveScriptSiteWindow
, riid
)) {
300 TRACE("(%p)->(IID_IActiveScriptSiteWindow %p)\n", This
, ppv
);
301 *ppv
= &This
->IActiveScriptSiteWindow_iface
;
302 }else if(IsEqualGUID(&IID_IActiveScriptSiteUIControl
, riid
)) {
303 TRACE("(%p)->(IID_IActiveScriptSiteUIControl %p)\n", This
, ppv
);
304 *ppv
= &This
->IActiveScriptSiteUIControl_iface
;
305 }else if(IsEqualGUID(&IID_IActiveScriptSiteDebug
, riid
)) {
306 TRACE("(%p)->(IID_IActiveScriptSiteDebug %p)\n", This
, ppv
);
307 *ppv
= &This
->IActiveScriptSiteDebug_iface
;
308 }else if(IsEqualGUID(&IID_IServiceProvider
, riid
)) {
309 TRACE("(%p)->(IID_IServiceProvider %p)\n", This
, ppv
);
310 *ppv
= &This
->IServiceProvider_iface
;
311 }else if(IsEqualGUID(&IID_ICanHandleException
, riid
)) {
312 TRACE("(%p)->(IID_ICanHandleException not supported %p)\n", This
, ppv
);
313 return E_NOINTERFACE
;
315 FIXME("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
316 return E_NOINTERFACE
;
319 IUnknown_AddRef((IUnknown
*)*ppv
);
323 static ULONG WINAPI
ActiveScriptSite_AddRef(IActiveScriptSite
*iface
)
325 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
326 LONG ref
= InterlockedIncrement(&This
->ref
);
328 TRACE("(%p) ref=%ld\n", This
, ref
);
333 static ULONG WINAPI
ActiveScriptSite_Release(IActiveScriptSite
*iface
)
335 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
336 LONG ref
= InterlockedDecrement(&This
->ref
);
338 TRACE("(%p) ref=%ld\n", This
, ref
);
341 release_script_engine(This
);
343 list_remove(&This
->entry
);
350 static HRESULT WINAPI
ActiveScriptSite_GetLCID(IActiveScriptSite
*iface
, LCID
*plcid
)
352 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
354 TRACE("(%p)->(%p)\n", This
, plcid
);
356 *plcid
= GetUserDefaultLCID();
360 static HRESULT WINAPI
ActiveScriptSite_GetItemInfo(IActiveScriptSite
*iface
, LPCOLESTR pstrName
,
361 DWORD dwReturnMask
, IUnknown
**ppiunkItem
, ITypeInfo
**ppti
)
363 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
365 TRACE("(%p)->(%s %lx %p %p)\n", This
, debugstr_w(pstrName
), dwReturnMask
, ppiunkItem
, ppti
);
367 if(dwReturnMask
!= SCRIPTINFO_IUNKNOWN
) {
368 FIXME("Unsupported mask %lx\n", dwReturnMask
);
374 if(wcscmp(pstrName
, L
"window"))
375 return DISP_E_MEMBERNOTFOUND
;
380 /* FIXME: Return proxy object */
381 *ppiunkItem
= (IUnknown
*)&This
->window
->base
.IHTMLWindow2_iface
;
382 IUnknown_AddRef(*ppiunkItem
);
387 static HRESULT WINAPI
ActiveScriptSite_GetDocVersionString(IActiveScriptSite
*iface
, BSTR
*pbstrVersion
)
389 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
390 FIXME("(%p)->(%p)\n", This
, pbstrVersion
);
394 static HRESULT WINAPI
ActiveScriptSite_OnScriptTerminate(IActiveScriptSite
*iface
,
395 const VARIANT
*pvarResult
, const EXCEPINFO
*pexcepinfo
)
397 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
398 FIXME("(%p)->(%p %p)\n", This
, pvarResult
, pexcepinfo
);
402 static HRESULT WINAPI
ActiveScriptSite_OnStateChange(IActiveScriptSite
*iface
, SCRIPTSTATE ssScriptState
)
404 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
406 TRACE("(%p)->(%x)\n", This
, ssScriptState
);
408 This
->script_state
= ssScriptState
;
412 static HRESULT WINAPI
ActiveScriptSite_OnScriptError(IActiveScriptSite
*iface
, IActiveScriptError
*pscripterror
)
414 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
415 FIXME("(%p)->(%p)\n", This
, pscripterror
);
419 static HRESULT WINAPI
ActiveScriptSite_OnEnterScript(IActiveScriptSite
*iface
)
421 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
423 TRACE("(%p)->()\n", This
);
428 static HRESULT WINAPI
ActiveScriptSite_OnLeaveScript(IActiveScriptSite
*iface
)
430 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
432 TRACE("(%p)->()\n", This
);
437 static const IActiveScriptSiteVtbl ActiveScriptSiteVtbl
= {
438 ActiveScriptSite_QueryInterface
,
439 ActiveScriptSite_AddRef
,
440 ActiveScriptSite_Release
,
441 ActiveScriptSite_GetLCID
,
442 ActiveScriptSite_GetItemInfo
,
443 ActiveScriptSite_GetDocVersionString
,
444 ActiveScriptSite_OnScriptTerminate
,
445 ActiveScriptSite_OnStateChange
,
446 ActiveScriptSite_OnScriptError
,
447 ActiveScriptSite_OnEnterScript
,
448 ActiveScriptSite_OnLeaveScript
451 static inline ScriptHost
*impl_from_IActiveScriptSiteInterruptPoll(IActiveScriptSiteInterruptPoll
*iface
)
453 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSiteInterruptPoll_iface
);
456 static HRESULT WINAPI
ActiveScriptSiteInterruptPoll_QueryInterface(IActiveScriptSiteInterruptPoll
*iface
,
457 REFIID riid
, void **ppv
)
459 ScriptHost
*This
= impl_from_IActiveScriptSiteInterruptPoll(iface
);
460 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
463 static ULONG WINAPI
ActiveScriptSiteInterruptPoll_AddRef(IActiveScriptSiteInterruptPoll
*iface
)
465 ScriptHost
*This
= impl_from_IActiveScriptSiteInterruptPoll(iface
);
466 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
469 static ULONG WINAPI
ActiveScriptSiteInterruptPoll_Release(IActiveScriptSiteInterruptPoll
*iface
)
471 ScriptHost
*This
= impl_from_IActiveScriptSiteInterruptPoll(iface
);
472 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
475 static HRESULT WINAPI
ActiveScriptSiteInterruptPoll_QueryContinue(IActiveScriptSiteInterruptPoll
*iface
)
477 ScriptHost
*This
= impl_from_IActiveScriptSiteInterruptPoll(iface
);
479 TRACE("(%p)\n", This
);
484 static const IActiveScriptSiteInterruptPollVtbl ActiveScriptSiteInterruptPollVtbl
= {
485 ActiveScriptSiteInterruptPoll_QueryInterface
,
486 ActiveScriptSiteInterruptPoll_AddRef
,
487 ActiveScriptSiteInterruptPoll_Release
,
488 ActiveScriptSiteInterruptPoll_QueryContinue
491 static inline ScriptHost
*impl_from_IActiveScriptSiteWindow(IActiveScriptSiteWindow
*iface
)
493 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSiteWindow_iface
);
496 static HRESULT WINAPI
ActiveScriptSiteWindow_QueryInterface(IActiveScriptSiteWindow
*iface
,
497 REFIID riid
, void **ppv
)
499 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
500 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
503 static ULONG WINAPI
ActiveScriptSiteWindow_AddRef(IActiveScriptSiteWindow
*iface
)
505 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
506 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
509 static ULONG WINAPI
ActiveScriptSiteWindow_Release(IActiveScriptSiteWindow
*iface
)
511 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
512 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
515 static HRESULT WINAPI
ActiveScriptSiteWindow_GetWindow(IActiveScriptSiteWindow
*iface
, HWND
*phwnd
)
517 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
519 TRACE("(%p)->(%p)\n", This
, phwnd
);
521 if(!This
->window
|| !This
->window
->base
.outer_window
)
524 *phwnd
= This
->window
->base
.outer_window
->browser
->doc
->hwnd
;
528 static HRESULT WINAPI
ActiveScriptSiteWindow_EnableModeless(IActiveScriptSiteWindow
*iface
, BOOL fEnable
)
530 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
531 FIXME("(%p)->(%x)\n", This
, fEnable
);
535 static const IActiveScriptSiteWindowVtbl ActiveScriptSiteWindowVtbl
= {
536 ActiveScriptSiteWindow_QueryInterface
,
537 ActiveScriptSiteWindow_AddRef
,
538 ActiveScriptSiteWindow_Release
,
539 ActiveScriptSiteWindow_GetWindow
,
540 ActiveScriptSiteWindow_EnableModeless
543 static inline ScriptHost
*impl_from_IActiveScriptSiteUIControl(IActiveScriptSiteUIControl
*iface
)
545 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSiteUIControl_iface
);
548 static HRESULT WINAPI
ActiveScriptSiteUIControl_QueryInterface(IActiveScriptSiteUIControl
*iface
, REFIID riid
, void **ppv
)
550 ScriptHost
*This
= impl_from_IActiveScriptSiteUIControl(iface
);
551 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
554 static ULONG WINAPI
ActiveScriptSiteUIControl_AddRef(IActiveScriptSiteUIControl
*iface
)
556 ScriptHost
*This
= impl_from_IActiveScriptSiteUIControl(iface
);
557 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
560 static ULONG WINAPI
ActiveScriptSiteUIControl_Release(IActiveScriptSiteUIControl
*iface
)
562 ScriptHost
*This
= impl_from_IActiveScriptSiteUIControl(iface
);
563 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
566 static HRESULT WINAPI
ActiveScriptSiteUIControl_GetUIBehavior(IActiveScriptSiteUIControl
*iface
, SCRIPTUICITEM UicItem
,
567 SCRIPTUICHANDLING
*pUicHandling
)
569 ScriptHost
*This
= impl_from_IActiveScriptSiteUIControl(iface
);
571 WARN("(%p)->(%d %p) semi-stub\n", This
, UicItem
, pUicHandling
);
573 *pUicHandling
= SCRIPTUICHANDLING_ALLOW
;
577 static const IActiveScriptSiteUIControlVtbl ActiveScriptSiteUIControlVtbl
= {
578 ActiveScriptSiteUIControl_QueryInterface
,
579 ActiveScriptSiteUIControl_AddRef
,
580 ActiveScriptSiteUIControl_Release
,
581 ActiveScriptSiteUIControl_GetUIBehavior
584 static inline ScriptHost
*impl_from_IActiveScriptSiteDebug(IActiveScriptSiteDebug
*iface
)
586 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSiteDebug_iface
);
589 static HRESULT WINAPI
ActiveScriptSiteDebug_QueryInterface(IActiveScriptSiteDebug
*iface
,
590 REFIID riid
, void **ppv
)
592 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
593 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
596 static ULONG WINAPI
ActiveScriptSiteDebug_AddRef(IActiveScriptSiteDebug
*iface
)
598 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
599 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
602 static ULONG WINAPI
ActiveScriptSiteDebug_Release(IActiveScriptSiteDebug
*iface
)
604 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
605 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
608 static HRESULT WINAPI
ActiveScriptSiteDebug_GetDocumentContextFromPosition(IActiveScriptSiteDebug
*iface
,
609 CTXARG_T dwSourceContext
, ULONG uCharacterOffset
, ULONG uNumChars
, IDebugDocumentContext
**ppsc
)
611 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
612 FIXME("(%p)->(%s %lu %lu %p)\n", This
, wine_dbgstr_longlong(dwSourceContext
), uCharacterOffset
,
617 static HRESULT WINAPI
ActiveScriptSiteDebug_GetApplication(IActiveScriptSiteDebug
*iface
, IDebugApplication
**ppda
)
619 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
620 FIXME("(%p)->(%p)\n", This
, ppda
);
624 static HRESULT WINAPI
ActiveScriptSiteDebug_GetRootApplicationNode(IActiveScriptSiteDebug
*iface
,
625 IDebugApplicationNode
**ppdanRoot
)
627 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
628 FIXME("(%p)->(%p)\n", This
, ppdanRoot
);
632 static HRESULT WINAPI
ActiveScriptSiteDebug_OnScriptErrorDebug(IActiveScriptSiteDebug
*iface
,
633 IActiveScriptErrorDebug
*pErrorDebug
, BOOL
*pfEnterDebugger
, BOOL
*pfCallOnScriptErrorWhenContinuing
)
635 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
636 FIXME("(%p)->(%p %p %p)\n", This
, pErrorDebug
, pfEnterDebugger
, pfCallOnScriptErrorWhenContinuing
);
640 static const IActiveScriptSiteDebugVtbl ActiveScriptSiteDebugVtbl
= {
641 ActiveScriptSiteDebug_QueryInterface
,
642 ActiveScriptSiteDebug_AddRef
,
643 ActiveScriptSiteDebug_Release
,
644 ActiveScriptSiteDebug_GetDocumentContextFromPosition
,
645 ActiveScriptSiteDebug_GetApplication
,
646 ActiveScriptSiteDebug_GetRootApplicationNode
,
647 ActiveScriptSiteDebug_OnScriptErrorDebug
650 static inline ScriptHost
*impl_from_IServiceProvider(IServiceProvider
*iface
)
652 return CONTAINING_RECORD(iface
, ScriptHost
, IServiceProvider_iface
);
655 static HRESULT WINAPI
ASServiceProvider_QueryInterface(IServiceProvider
*iface
, REFIID riid
, void **ppv
)
657 ScriptHost
*This
= impl_from_IServiceProvider(iface
);
658 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
661 static ULONG WINAPI
ASServiceProvider_AddRef(IServiceProvider
*iface
)
663 ScriptHost
*This
= impl_from_IServiceProvider(iface
);
664 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
667 static ULONG WINAPI
ASServiceProvider_Release(IServiceProvider
*iface
)
669 ScriptHost
*This
= impl_from_IServiceProvider(iface
);
670 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
673 static HRESULT WINAPI
ASServiceProvider_QueryService(IServiceProvider
*iface
, REFGUID guidService
,
674 REFIID riid
, void **ppv
)
676 ScriptHost
*This
= impl_from_IServiceProvider(iface
);
678 if(IsEqualGUID(&SID_SInternetHostSecurityManager
, guidService
)) {
679 TRACE("(%p)->(SID_SInternetHostSecurityManager)\n", This
);
681 if(!This
->window
|| !This
->window
->doc
)
682 return E_NOINTERFACE
;
684 return IInternetHostSecurityManager_QueryInterface(&This
->window
->doc
->IInternetHostSecurityManager_iface
,
688 if(IsEqualGUID(&SID_SContainerDispatch
, guidService
)) {
689 TRACE("(%p)->(SID_SContainerDispatch)\n", This
);
691 if(!This
->window
|| !This
->window
->doc
)
692 return E_NOINTERFACE
;
694 return IHTMLDocument2_QueryInterface(&This
->window
->doc
->IHTMLDocument2_iface
, riid
, ppv
);
697 FIXME("(%p)->(%s %s %p)\n", This
, debugstr_guid(guidService
), debugstr_guid(riid
), ppv
);
698 return E_NOINTERFACE
;
701 static const IServiceProviderVtbl ASServiceProviderVtbl
= {
702 ASServiceProvider_QueryInterface
,
703 ASServiceProvider_AddRef
,
704 ASServiceProvider_Release
,
705 ASServiceProvider_QueryService
708 static ScriptHost
*create_script_host(HTMLInnerWindow
*window
, const GUID
*guid
)
710 IActiveScript
*script
;
714 ret
= calloc(1, sizeof(*ret
));
718 ret
->IActiveScriptSite_iface
.lpVtbl
= &ActiveScriptSiteVtbl
;
719 ret
->IActiveScriptSiteInterruptPoll_iface
.lpVtbl
= &ActiveScriptSiteInterruptPollVtbl
;
720 ret
->IActiveScriptSiteWindow_iface
.lpVtbl
= &ActiveScriptSiteWindowVtbl
;
721 ret
->IActiveScriptSiteUIControl_iface
.lpVtbl
= &ActiveScriptSiteUIControlVtbl
;
722 ret
->IActiveScriptSiteDebug_iface
.lpVtbl
= &ActiveScriptSiteDebugVtbl
;
723 ret
->IServiceProvider_iface
.lpVtbl
= &ASServiceProviderVtbl
;
725 ret
->window
= window
;
726 ret
->script_state
= SCRIPTSTATE_UNINITIALIZED
;
729 list_add_tail(&window
->script_hosts
, &ret
->entry
);
731 hres
= CoCreateInstance(&ret
->guid
, NULL
, CLSCTX_INPROC_SERVER
|CLSCTX_INPROC_HANDLER
,
732 &IID_IActiveScript
, (void**)&script
);
734 WARN("Could not load script engine: %08lx\n", hres
);
736 BOOL succeeded
= init_script_engine(ret
, script
);
737 ret
->script
= script
;
739 release_script_engine(ret
);
745 static void dispatch_script_readystatechange_event(HTMLScriptElement
*script
)
750 if(script
->readystate
!= READYSTATE_LOADED
||
751 dispex_compat_mode(&script
->element
.node
.event_target
.dispex
) < COMPAT_MODE_IE10
) {
752 hres
= create_document_event(script
->element
.node
.doc
, EVENTID_READYSTATECHANGE
, &event
);
753 if(SUCCEEDED(hres
)) {
754 dispatch_event(&script
->element
.node
.event_target
, event
);
755 IDOMEvent_Release(&event
->IDOMEvent_iface
);
759 if(script
->readystate
== READYSTATE_LOADED
) {
760 hres
= create_document_event(script
->element
.node
.doc
, EVENTID_LOAD
, &event
);
761 if(SUCCEEDED(hres
)) {
762 dispatch_event(&script
->element
.node
.event_target
, event
);
763 IDOMEvent_Release(&event
->IDOMEvent_iface
);
770 HTMLScriptElement
*elem
;
771 } fire_readystatechange_task_t
;
773 static void fire_readystatechange_proc(event_task_t
*_task
)
775 fire_readystatechange_task_t
*task
= (fire_readystatechange_task_t
*)_task
;
777 if(!task
->elem
->pending_readystatechange_event
)
780 task
->elem
->pending_readystatechange_event
= FALSE
;
781 dispatch_script_readystatechange_event(task
->elem
);
784 static void fire_readystatechange_task_destr(event_task_t
*_task
)
786 fire_readystatechange_task_t
*task
= (fire_readystatechange_task_t
*)_task
;
788 IHTMLScriptElement_Release(&task
->elem
->IHTMLScriptElement_iface
);
791 static void set_script_elem_readystate(HTMLScriptElement
*script_elem
, READYSTATE readystate
)
793 script_elem
->readystate
= readystate
;
795 if(readystate
!= READYSTATE_LOADED
&&
796 dispex_compat_mode(&script_elem
->element
.node
.event_target
.dispex
) >= COMPAT_MODE_IE11
)
799 if(readystate
!= READYSTATE_INTERACTIVE
) {
800 if(!script_elem
->element
.node
.doc
->window
->parser_callback_cnt
) {
801 fire_readystatechange_task_t
*task
;
804 if(script_elem
->pending_readystatechange_event
)
807 task
= malloc(sizeof(*task
));
811 IHTMLScriptElement_AddRef(&script_elem
->IHTMLScriptElement_iface
);
812 task
->elem
= script_elem
;
814 hres
= push_event_task(&task
->header
, script_elem
->element
.node
.doc
->window
, fire_readystatechange_proc
,
815 fire_readystatechange_task_destr
, script_elem
->element
.node
.doc
->window
->task_magic
);
817 script_elem
->pending_readystatechange_event
= TRUE
;
819 script_elem
->pending_readystatechange_event
= FALSE
;
820 dispatch_script_readystatechange_event(script_elem
);
825 static void parse_elem_text(ScriptHost
*script_host
, HTMLScriptElement
*script_elem
, LPCWSTR text
)
831 TRACE("%s\n", debugstr_w(text
));
834 memset(&excepinfo
, 0, sizeof(excepinfo
));
836 hres
= IActiveScriptParse_ParseScriptText(script_host
->parse
, text
, L
"window", NULL
, L
"</SCRIPT>",
837 0, 0, SCRIPTTEXT_ISVISIBLE
|SCRIPTTEXT_HOSTMANAGESSOURCE
,
842 WARN("<<< %08lx\n", hres
);
848 HTMLScriptElement
*script_elem
;
849 nsILoadGroup
*load_group
;
857 static HRESULT
get_binding_text(ScriptBSC
*bsc
, WCHAR
**ret
)
863 text
= malloc(sizeof(WCHAR
));
865 return E_OUTOFMEMORY
;
871 switch(bsc
->bsc
.bom
) {
873 if(bsc
->bsc
.read
% sizeof(WCHAR
)) {
874 FIXME("The buffer is not a valid utf16 string\n");
878 text
= malloc(bsc
->bsc
.read
+ sizeof(WCHAR
));
880 return E_OUTOFMEMORY
;
882 memcpy(text
, bsc
->buf
, bsc
->bsc
.read
);
883 text
[bsc
->bsc
.read
/sizeof(WCHAR
)] = 0;
887 /* FIXME: Try to use charset from HTTP headers first */
888 cp
= get_document_charset(bsc
->script_elem
->element
.node
.doc
);
893 len
= MultiByteToWideChar(cp
, 0, bsc
->buf
, bsc
->bsc
.read
, NULL
, 0);
894 text
= malloc((len
+ 1) * sizeof(WCHAR
));
896 return E_OUTOFMEMORY
;
898 MultiByteToWideChar(cp
, 0, bsc
->buf
, bsc
->bsc
.read
, text
, len
);
907 static void script_file_available(ScriptBSC
*bsc
)
909 HTMLScriptElement
*script_elem
= bsc
->script_elem
;
910 HTMLInnerWindow
*window
= bsc
->bsc
.window
;
911 ScriptHost
*script_host
;
916 assert(window
!= NULL
);
918 hres
= get_binding_text(bsc
, &script_elem
->src_text
);
922 script_host
= get_elem_script_host(window
, script_elem
);
926 if(window
->parser_callback_cnt
) {
927 script_queue_entry_t
*queue
;
929 TRACE("Adding to queue\n");
931 queue
= malloc(sizeof(*queue
));
935 IHTMLScriptElement_AddRef(&script_elem
->IHTMLScriptElement_iface
);
936 queue
->script
= script_elem
;
938 list_add_tail(&window
->script_queue
, &queue
->entry
);
942 nsres
= nsIDOMElement_GetParentNode(script_elem
->element
.dom_element
, &parent
);
943 if(NS_FAILED(nsres
) || !parent
) {
944 TRACE("No parent, not executing\n");
945 script_elem
->parse_on_bind
= TRUE
;
949 nsIDOMNode_Release(parent
);
951 script_host
= get_elem_script_host(window
, script_elem
);
955 script_elem
->parsed
= TRUE
;
956 if(script_host
->parse
)
957 parse_elem_text(script_host
, script_elem
, script_elem
->src_text
);
960 static inline ScriptBSC
*impl_from_BSCallback(BSCallback
*iface
)
962 return CONTAINING_RECORD(iface
, ScriptBSC
, bsc
);
965 static void ScriptBSC_destroy(BSCallback
*bsc
)
967 ScriptBSC
*This
= impl_from_BSCallback(bsc
);
969 if(This
->script_elem
) {
970 IHTMLScriptElement_Release(&This
->script_elem
->IHTMLScriptElement_iface
);
971 This
->script_elem
= NULL
;
975 ERR("Unfinished request\n");
976 nsIRequest_Release(This
->request
);
979 nsILoadGroup_Release(This
->load_group
);
985 static HRESULT
ScriptBSC_init_bindinfo(BSCallback
*bsc
)
990 static HRESULT
ScriptBSC_start_binding(BSCallback
*bsc
)
992 ScriptBSC
*This
= impl_from_BSCallback(bsc
);
995 This
->script_elem
->binding
= &This
->bsc
;
997 if(This
->load_group
) {
998 nsres
= create_onload_blocker_request(&This
->request
);
999 if(NS_SUCCEEDED(nsres
)) {
1000 nsres
= nsILoadGroup_AddRequest(This
->load_group
, This
->request
, NULL
);
1001 if(NS_FAILED(nsres
))
1002 ERR("AddRequest failed: %08lx\n", nsres
);
1006 /* FIXME: We should find a better to decide if 'loading' state is supposed to be used by the protocol. */
1007 if(This
->scheme
== URL_SCHEME_HTTPS
|| This
->scheme
== URL_SCHEME_HTTP
)
1008 set_script_elem_readystate(This
->script_elem
, READYSTATE_LOADING
);
1013 static HRESULT
ScriptBSC_stop_binding(BSCallback
*bsc
, HRESULT result
)
1015 ScriptBSC
*This
= impl_from_BSCallback(bsc
);
1018 if(SUCCEEDED(result
) && !This
->script_elem
)
1019 result
= E_UNEXPECTED
;
1021 assert(FAILED(result
) || This
->script_elem
->binding
== &This
->bsc
);
1022 This
->script_elem
->binding
= NULL
;
1024 if(This
->script_elem
->readystate
== READYSTATE_LOADING
)
1025 set_script_elem_readystate(This
->script_elem
, READYSTATE_LOADED
);
1027 if(SUCCEEDED(result
)) {
1028 script_file_available(This
);
1030 FIXME("binding failed %08lx\n", result
);
1037 nsres
= nsILoadGroup_RemoveRequest(This
->load_group
, This
->request
, NULL
, NS_OK
);
1038 if(NS_FAILED(nsres
))
1039 ERR("RemoveRequest failed: %08lx\n", nsres
);
1040 nsIRequest_Release(This
->request
);
1041 This
->request
= NULL
;
1044 IHTMLScriptElement_Release(&This
->script_elem
->IHTMLScriptElement_iface
);
1045 This
->script_elem
= NULL
;
1049 static HRESULT
ScriptBSC_read_data(BSCallback
*bsc
, IStream
*stream
)
1051 ScriptBSC
*This
= impl_from_BSCallback(bsc
);
1056 This
->buf
= malloc(128);
1058 return E_OUTOFMEMORY
;
1063 if(This
->bsc
.read
>= This
->size
) {
1065 new_buf
= realloc(This
->buf
, This
->size
<< 1);
1067 return E_OUTOFMEMORY
;
1069 This
->buf
= new_buf
;
1072 hres
= read_stream(&This
->bsc
, stream
, This
->buf
+This
->bsc
.read
, This
->size
-This
->bsc
.read
, &read
);
1073 }while(hres
== S_OK
);
1078 static HRESULT
ScriptBSC_on_progress(BSCallback
*bsc
, ULONG progress
, ULONG total
, ULONG status_code
, LPCWSTR status_text
)
1083 static HRESULT
ScriptBSC_on_response(BSCallback
*bsc
, DWORD response_code
,
1084 LPCWSTR response_headers
)
1089 static HRESULT
ScriptBSC_beginning_transaction(BSCallback
*bsc
, WCHAR
**additional_headers
)
1094 static const BSCallbackVtbl ScriptBSCVtbl
= {
1096 ScriptBSC_init_bindinfo
,
1097 ScriptBSC_start_binding
,
1098 ScriptBSC_stop_binding
,
1099 ScriptBSC_read_data
,
1100 ScriptBSC_on_progress
,
1101 ScriptBSC_on_response
,
1102 ScriptBSC_beginning_transaction
1106 HRESULT
load_script(HTMLScriptElement
*script_elem
, const WCHAR
*src
, BOOL async
)
1108 HTMLInnerWindow
*window
;
1114 static const WCHAR wine_schemaW
[] = {'w','i','n','e',':'};
1116 if(lstrlenW(src
) > ARRAY_SIZE(wine_schemaW
) && !memcmp(src
, wine_schemaW
, sizeof(wine_schemaW
)))
1117 src
+= ARRAY_SIZE(wine_schemaW
);
1119 TRACE("(%p %s %x)\n", script_elem
, debugstr_w(src
), async
);
1121 if(!script_elem
->element
.node
.doc
|| !(window
= script_elem
->element
.node
.doc
->window
)) {
1123 return E_UNEXPECTED
;
1126 hres
= create_uri(src
, 0, &uri
);
1130 hres
= CreateURLMonikerEx2(NULL
, uri
, &mon
, URL_MK_UNIFORM
);
1136 bsc
= calloc(1, sizeof(*bsc
));
1138 IMoniker_Release(mon
);
1140 return E_OUTOFMEMORY
;
1143 init_bscallback(&bsc
->bsc
, &ScriptBSCVtbl
, mon
, async
? BINDF_ASYNCHRONOUS
| BINDF_ASYNCSTORAGE
| BINDF_PULLDATA
: 0);
1144 IMoniker_Release(mon
);
1146 hres
= IUri_GetScheme(uri
, &bsc
->scheme
);
1149 bsc
->scheme
= URL_SCHEME_UNKNOWN
;
1151 IHTMLScriptElement_AddRef(&script_elem
->IHTMLScriptElement_iface
);
1152 bsc
->script_elem
= script_elem
;
1154 if(window
->bscallback
&& window
->bscallback
->nschannel
&&
1155 window
->bscallback
->nschannel
->load_group
) {
1159 nsres
= nsIDOMNode_Contains(script_elem
->element
.node
.doc
->node
.nsnode
,
1160 script_elem
->element
.node
.nsnode
, &contains
);
1161 if(NS_SUCCEEDED(nsres
) && contains
) {
1162 TRACE("script %p will block load event\n", script_elem
);
1163 bsc
->load_group
= window
->bscallback
->nschannel
->load_group
;
1164 nsILoadGroup_AddRef(bsc
->load_group
);
1168 hres
= start_binding(window
, &bsc
->bsc
, NULL
);
1170 IBindStatusCallback_Release(&bsc
->bsc
.IBindStatusCallback_iface
);
1174 static void parse_inline_script(ScriptHost
*script_host
, HTMLScriptElement
*script_elem
)
1176 const PRUnichar
*text
;
1180 nsAString_Init(&text_str
, NULL
);
1181 nsres
= nsIDOMHTMLScriptElement_GetText(script_elem
->nsscript
, &text_str
);
1182 nsAString_GetData(&text_str
, &text
);
1184 set_script_elem_readystate(script_elem
, READYSTATE_INTERACTIVE
);
1186 if(NS_FAILED(nsres
)) {
1187 ERR("GetText failed: %08lx\n", nsres
);
1189 parse_elem_text(script_host
, script_elem
, text
);
1192 nsAString_Finish(&text_str
);
1195 static BOOL
parse_script_elem(ScriptHost
*script_host
, HTMLScriptElement
*script_elem
)
1197 nsAString src_str
, event_str
;
1198 BOOL is_complete
= FALSE
;
1199 const PRUnichar
*src
;
1202 nsAString_Init(&event_str
, NULL
);
1203 nsres
= nsIDOMHTMLScriptElement_GetEvent(script_elem
->nsscript
, &event_str
);
1204 if(NS_SUCCEEDED(nsres
)) {
1205 const PRUnichar
*event
;
1207 nsAString_GetData(&event_str
, &event
);
1209 TRACE("deferring event %s script evaluation\n", debugstr_w(event
));
1210 nsAString_Finish(&event_str
);
1214 ERR("GetEvent failed: %08lx\n", nsres
);
1216 nsAString_Finish(&event_str
);
1218 nsAString_Init(&src_str
, NULL
);
1219 nsres
= nsIDOMHTMLScriptElement_GetSrc(script_elem
->nsscript
, &src_str
);
1220 nsAString_GetData(&src_str
, &src
);
1222 if(NS_FAILED(nsres
)) {
1223 ERR("GetSrc failed: %08lx\n", nsres
);
1225 load_script(script_elem
, src
, FALSE
);
1226 is_complete
= script_elem
->parsed
;
1228 parse_inline_script(script_host
, script_elem
);
1232 nsAString_Finish(&src_str
);
1237 static GUID
get_default_script_guid(HTMLInnerWindow
*window
)
1239 /* If not specified, we should use very first script host that was created for the page (or JScript if none) */
1240 return list_empty(&window
->script_hosts
)
1242 : LIST_ENTRY(list_head(&window
->script_hosts
), ScriptHost
, entry
)->guid
;
1245 static BOOL
get_guid_from_type(LPCWSTR type
, GUID
*guid
)
1247 /* FIXME: Handle more types */
1248 if(!wcsicmp(type
, L
"text/javascript") || !wcsicmp(type
, L
"text/jscript")) {
1249 *guid
= CLSID_JScript
;
1250 }else if(!wcsicmp(type
, L
"text/vbscript")) {
1251 *guid
= CLSID_VBScript
;
1253 FIXME("Unknown type %s\n", debugstr_w(type
));
1260 static BOOL
get_guid_from_language(LPCWSTR type
, GUID
*guid
)
1264 hres
= CLSIDFromProgID(type
, guid
);
1268 /* FIXME: Check CATID_ActiveScriptParse */
1273 static BOOL
get_script_guid(HTMLInnerWindow
*window
, nsIDOMHTMLScriptElement
*nsscript
, GUID
*guid
)
1275 nsIDOMElement
*nselem
;
1276 const PRUnichar
*language
;
1281 nsAString_Init(&val_str
, NULL
);
1283 nsres
= nsIDOMHTMLScriptElement_GetType(nsscript
, &val_str
);
1284 if(NS_SUCCEEDED(nsres
)) {
1285 const PRUnichar
*type
;
1287 nsAString_GetData(&val_str
, &type
);
1289 ret
= get_guid_from_type(type
, guid
);
1290 nsAString_Finish(&val_str
);
1294 ERR("GetType failed: %08lx\n", nsres
);
1297 nsres
= nsIDOMHTMLScriptElement_QueryInterface(nsscript
, &IID_nsIDOMElement
, (void**)&nselem
);
1298 assert(nsres
== NS_OK
);
1300 nsres
= get_elem_attr_value(nselem
, L
"language", &val_str
, &language
);
1301 nsIDOMElement_Release(nselem
);
1302 if(NS_SUCCEEDED(nsres
)) {
1304 ret
= get_guid_from_language(language
, guid
);
1306 *guid
= get_default_script_guid(window
);
1309 nsAString_Finish(&val_str
);
1315 static ScriptHost
*get_script_host(HTMLInnerWindow
*window
, const GUID
*guid
)
1319 LIST_FOR_EACH_ENTRY(iter
, &window
->script_hosts
, ScriptHost
, entry
) {
1320 if(IsEqualGUID(guid
, &iter
->guid
))
1324 return create_script_host(window
, guid
);
1327 static ScriptHost
*get_elem_script_host(HTMLInnerWindow
*window
, HTMLScriptElement
*script_elem
)
1331 if(!get_script_guid(window
, script_elem
->nsscript
, &guid
)) {
1332 WARN("Could not find script GUID\n");
1336 if(IsEqualGUID(&CLSID_JScript
, &guid
) && (!window
->doc
->browser
|| window
->doc
->browser
->script_mode
!= SCRIPTMODE_ACTIVESCRIPT
)) {
1337 TRACE("Ignoring JScript\n");
1341 return get_script_host(window
, &guid
);
1344 void doc_insert_script(HTMLInnerWindow
*window
, HTMLScriptElement
*script_elem
, BOOL from_parser
)
1346 ScriptHost
*script_host
;
1347 BOOL is_complete
= FALSE
;
1349 script_host
= get_elem_script_host(window
, script_elem
);
1353 if(script_host
->parse
) {
1354 if(script_elem
->src_text
) {
1356 set_script_elem_readystate(script_elem
, READYSTATE_INTERACTIVE
);
1357 script_elem
->parsed
= TRUE
;
1358 parse_elem_text(script_host
, script_elem
, script_elem
->src_text
);
1360 }else if(!script_elem
->binding
) {
1361 is_complete
= parse_script_elem(script_host
, script_elem
);
1366 set_script_elem_readystate(script_elem
, READYSTATE_COMPLETE
);
1369 IDispatch
*script_parse_event(HTMLInnerWindow
*window
, LPCWSTR text
)
1371 ScriptHost
*script_host
;
1377 TRACE("%s\n", debugstr_w(text
));
1379 for(ptr
= text
; iswalnum(*ptr
); ptr
++);
1384 language
= malloc((ptr
- text
+ 1) * sizeof(WCHAR
));
1388 memcpy(language
, text
, (ptr
-text
)*sizeof(WCHAR
));
1389 language
[ptr
-text
] = 0;
1391 b
= get_guid_from_language(language
, &guid
);
1396 WARN("Could not find language\n");
1403 guid
= get_default_script_guid(window
);
1406 if(IsEqualGUID(&CLSID_JScript
, &guid
)
1407 && (!window
->doc
->browser
|| window
->doc
->browser
->script_mode
!= SCRIPTMODE_ACTIVESCRIPT
)) {
1408 TRACE("Ignoring JScript\n");
1412 script_host
= get_script_host(window
, &guid
);
1413 if(!script_host
|| !script_host
->parse_proc
)
1416 hres
= IActiveScriptParseProcedure2_ParseProcedureText(script_host
->parse_proc
, ptr
, NULL
, L
"",
1417 NULL
, NULL
, L
"\"", 0 /* FIXME */, 0,
1418 SCRIPTPROC_HOSTMANAGESSOURCE
|SCRIPTPROC_IMPLICIT_THIS
|SCRIPTPROC_IMPLICIT_PARENTS
, &disp
);
1420 WARN("ParseProcedureText failed: %08lx\n", hres
);
1424 TRACE("ret %p\n", disp
);
1428 HRESULT
exec_script(HTMLInnerWindow
*window
, const WCHAR
*code
, const WCHAR
*lang
, VARIANT
*ret
)
1430 ScriptHost
*script_host
;
1435 if(!get_guid_from_language(lang
, &guid
)) {
1436 WARN("Could not find script GUID\n");
1437 return CO_E_CLASSSTRING
;
1440 script_host
= get_script_host(window
, &guid
);
1442 FIXME("No script host\n");
1446 if(!script_host
->parse
) {
1447 FIXME("script_host->parse == NULL\n");
1451 memset(&ei
, 0, sizeof(ei
));
1453 hres
= IActiveScriptParse_ParseScriptText(script_host
->parse
, code
, NULL
, NULL
, L
"\"", 0, 0, SCRIPTTEXT_ISVISIBLE
, ret
, &ei
);
1457 WARN("<<< %08lx\n", hres
);
1462 IDispatch
*get_script_disp(ScriptHost
*script_host
)
1467 if(!script_host
->script
)
1470 hres
= IActiveScript_GetScriptDispatch(script_host
->script
, L
"window", &disp
);
1477 static EventTarget
*find_event_target(HTMLDocumentNode
*doc
, HTMLScriptElement
*script_elem
)
1479 EventTarget
*event_target
= NULL
;
1480 const PRUnichar
*target_id
;
1481 nsAString target_id_str
;
1485 nsAString_Init(&target_id_str
, NULL
);
1486 nsres
= nsIDOMHTMLScriptElement_GetHtmlFor(script_elem
->nsscript
, &target_id_str
);
1487 if(NS_FAILED(nsres
)) {
1488 ERR("GetScriptFor failed: %08lx\n", nsres
);
1489 nsAString_Finish(&target_id_str
);
1493 nsAString_GetData(&target_id_str
, &target_id
);
1495 FIXME("Empty for attribute\n");
1496 }else if(!wcscmp(target_id
, L
"document")) {
1497 event_target
= &doc
->node
.event_target
;
1498 IHTMLDOMNode_AddRef(&doc
->node
.IHTMLDOMNode_iface
);
1499 }else if(!wcscmp(target_id
, L
"window")) {
1501 event_target
= &doc
->window
->event_target
;
1502 IDispatchEx_AddRef(&event_target
->dispex
.IDispatchEx_iface
);
1505 HTMLElement
*target_elem
;
1507 hres
= get_doc_elem_by_id(doc
, target_id
, &target_elem
);
1508 if(SUCCEEDED(hres
) && target_elem
) {
1509 event_target
= &target_elem
->node
.event_target
;
1513 nsAString_Finish(&target_id_str
);
1514 return event_target
;
1517 static BOOL
parse_event_str(WCHAR
*event
, const WCHAR
**args
)
1521 TRACE("%s\n", debugstr_w(event
));
1523 for(ptr
= event
; iswalnum(*ptr
); ptr
++);
1534 while(iswalnum(*ptr
) || iswspace(*ptr
) || *ptr
== ',')
1544 static IDispatch
*parse_event_elem(HTMLDocumentNode
*doc
, HTMLScriptElement
*script_elem
, WCHAR
**ret_event
)
1546 ScriptHost
*script_host
;
1547 WCHAR
*event
= NULL
;
1554 if(script_elem
->parsed
)
1557 script_host
= get_elem_script_host(doc
->window
, script_elem
);
1558 if(!script_host
|| !script_host
->parse_proc
)
1561 nsAString_Init(&nsstr
, NULL
);
1562 nsres
= nsIDOMHTMLScriptElement_GetEvent(script_elem
->nsscript
, &nsstr
);
1563 if(NS_SUCCEEDED(nsres
)) {
1564 const PRUnichar
*event_val
;
1566 nsAString_GetData(&nsstr
, &event_val
);
1567 event
= wcsdup(event_val
);
1569 nsAString_Finish(&nsstr
);
1573 if(!parse_event_str(event
, &args
)) {
1574 WARN("parsing %s failed\n", debugstr_w(event
));
1579 nsAString_Init(&nsstr
, NULL
);
1580 nsres
= nsIDOMHTMLScriptElement_GetText(script_elem
->nsscript
, &nsstr
);
1581 if(NS_SUCCEEDED(nsres
)) {
1582 const PRUnichar
*text
;
1584 nsAString_GetData(&nsstr
, &text
);
1585 hres
= IActiveScriptParseProcedure2_ParseProcedureText(script_host
->parse_proc
, text
, args
,
1586 L
"", NULL
, NULL
, L
"</SCRIPT>", 0, 0,
1587 SCRIPTPROC_HOSTMANAGESSOURCE
|SCRIPTPROC_IMPLICIT_THIS
|SCRIPTPROC_IMPLICIT_PARENTS
, &disp
);
1591 ERR("GetText failed: %08lx\n", nsres
);
1594 nsAString_Finish(&nsstr
);
1604 void bind_event_scripts(HTMLDocumentNode
*doc
)
1606 HTMLPluginContainer
*plugin_container
;
1607 nsIDOMHTMLScriptElement
*nsscript
;
1608 HTMLScriptElement
*script_elem
;
1609 EventTarget
*event_target
;
1610 nsIDOMNodeList
*node_list
;
1611 nsIDOMNode
*script_node
;
1612 nsAString selector_str
;
1613 IDispatch
*event_disp
;
1621 if(!doc
->dom_document
)
1624 nsAString_InitDepend(&selector_str
, L
"script[event]");
1625 nsres
= nsIDOMDocument_QuerySelectorAll(doc
->dom_document
, &selector_str
, &node_list
);
1626 nsAString_Finish(&selector_str
);
1627 if(NS_FAILED(nsres
)) {
1628 ERR("QuerySelectorAll failed: %08lx\n", nsres
);
1635 nsres
= nsIDOMNodeList_GetLength(node_list
, &length
);
1636 assert(nsres
== NS_OK
);
1638 for(i
=0; i
< length
; i
++) {
1639 nsres
= nsIDOMNodeList_Item(node_list
, i
, &script_node
);
1640 if(NS_FAILED(nsres
) || !script_node
) {
1641 ERR("Item(%d) failed: %08lx\n", i
, nsres
);
1645 nsres
= nsIDOMNode_QueryInterface(script_node
, &IID_nsIDOMHTMLScriptElement
, (void**)&nsscript
);
1646 assert(nsres
== NS_OK
);
1647 nsIDOMNode_Release(script_node
);
1649 hres
= script_elem_from_nsscript(nsscript
, &script_elem
);
1650 nsIDOMHTMLScriptElement_Release(nsscript
);
1654 event_disp
= parse_event_elem(doc
, script_elem
, &event
);
1656 event_target
= find_event_target(doc
, script_elem
);
1658 hres
= IDispatchEx_QueryInterface(&event_target
->dispex
.IDispatchEx_iface
, &IID_HTMLPluginContainer
,
1659 (void**)&plugin_container
);
1661 bind_activex_event(doc
, plugin_container
, event
, event_disp
);
1663 bind_target_event(doc
, event_target
, event
, event_disp
);
1665 IDispatchEx_Release(&event_target
->dispex
.IDispatchEx_iface
);
1666 if(plugin_container
)
1667 node_release(&plugin_container
->element
.node
);
1671 IDispatch_Release(event_disp
);
1674 IHTMLScriptElement_Release(&script_elem
->IHTMLScriptElement_iface
);
1677 nsIDOMNodeList_Release(node_list
);
1680 BOOL
find_global_prop(HTMLInnerWindow
*window
, BSTR name
, DWORD flags
, ScriptHost
**ret_host
, DISPID
*ret_id
)
1682 IDispatchEx
*dispex
;
1687 LIST_FOR_EACH_ENTRY(iter
, &window
->script_hosts
, ScriptHost
, entry
) {
1688 disp
= get_script_disp(iter
);
1692 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
1693 if(SUCCEEDED(hres
)) {
1694 hres
= IDispatchEx_GetDispID(dispex
, name
, flags
& (~fdexNameEnsure
), ret_id
);
1695 IDispatchEx_Release(dispex
);
1697 FIXME("No IDispatchEx\n");
1701 IDispatch_Release(disp
);
1702 if(SUCCEEDED(hres
)) {
1711 static BOOL
is_jscript_available(void)
1713 static BOOL available
, checked
;
1717 HRESULT hres
= CoGetClassObject(&CLSID_JScript
, CLSCTX_INPROC_SERVER
, NULL
, &IID_IUnknown
, (void**)&unk
);
1719 if(SUCCEEDED(hres
)) {
1721 IUnknown_Release(unk
);
1731 static BOOL
use_gecko_script(IUri
*uri
)
1737 hres
= IUri_GetDisplayUri(uri
, &display_uri
);
1741 hres
= IInternetSecurityManager_MapUrlToZone(get_security_manager(), display_uri
, &zone
, 0);
1743 WARN("Could not map %s to zone: %08lx\n", debugstr_w(display_uri
), hres
);
1744 SysFreeString(display_uri
);
1748 SysFreeString(display_uri
);
1749 TRACE("zone %ld\n", zone
);
1750 return zone
== URLZONE_UNTRUSTED
;
1753 void update_browser_script_mode(GeckoBrowser
*browser
, IUri
*uri
)
1755 nsIWebBrowserSetup
*setup
;
1758 if(!is_jscript_available()) {
1759 TRACE("jscript.dll not available\n");
1760 browser
->script_mode
= SCRIPTMODE_GECKO
;
1764 browser
->script_mode
= use_gecko_script(uri
) ? SCRIPTMODE_GECKO
: SCRIPTMODE_ACTIVESCRIPT
;
1766 nsres
= nsIWebBrowser_QueryInterface(browser
->webbrowser
, &IID_nsIWebBrowserSetup
, (void**)&setup
);
1767 if(NS_SUCCEEDED(nsres
)) {
1768 nsres
= nsIWebBrowserSetup_SetProperty(setup
, SETUP_ALLOW_JAVASCRIPT
,
1769 browser
->script_mode
== SCRIPTMODE_GECKO
);
1771 if(NS_SUCCEEDED(nsres
))
1772 nsres
= nsIWebBrowserSetup_SetProperty(setup
, SETUP_DISABLE_NOSCRIPT
, TRUE
);
1774 nsIWebBrowserSetup_Release(setup
);
1777 if(NS_FAILED(nsres
))
1778 ERR("JavaScript setup failed: %08lx\n", nsres
);
1781 void release_script_hosts(HTMLInnerWindow
*window
)
1783 script_queue_entry_t
*queue_iter
;
1786 while(!list_empty(&window
->script_queue
)) {
1787 queue_iter
= LIST_ENTRY(list_head(&window
->script_queue
), script_queue_entry_t
, entry
);
1789 list_remove(&queue_iter
->entry
);
1790 IHTMLScriptElement_Release(&queue_iter
->script
->IHTMLScriptElement_iface
);
1794 while(!list_empty(&window
->script_hosts
)) {
1795 iter
= LIST_ENTRY(list_head(&window
->script_hosts
), ScriptHost
, entry
);
1797 release_script_engine(iter
);
1798 list_remove(&iter
->entry
);
1799 iter
->window
= NULL
;
1800 IActiveScriptSite_Release(&iter
->IActiveScriptSite_iface
);