mshtml: Fix classList toggle() when return value pointer is NULL.
[wine.git] / dlls / mshtml / script.c
blob0cd79b4540539a17da1a41aa1df86ce83fe80f90
1 /*
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
19 #include <stdarg.h>
21 #define COBJMACROS
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "ole2.h"
27 #include "activscp.h"
28 #include "activdbg.h"
29 #include "shlwapi.h"
31 #include "wine/debug.h"
33 #include "mshtml_private.h"
34 #include "htmlscript.h"
35 #include "pluginhost.h"
36 #include "htmlevent.h"
37 #include "binding.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
41 #ifdef _WIN64
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
52 #else
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
63 #endif
65 /* See jscript.h in jscript.dll. */
66 #define SCRIPTLANGUAGEVERSION_HTML 0x400
67 #define SCRIPTLANGUAGEVERSION_ES5 0x102
68 #define SCRIPTLANGUAGEVERSION_ES6 0x103
70 struct ScriptHost {
71 IActiveScriptSite IActiveScriptSite_iface;
72 IActiveScriptSiteInterruptPoll IActiveScriptSiteInterruptPoll_iface;
73 IActiveScriptSiteWindow IActiveScriptSiteWindow_iface;
74 IActiveScriptSiteUIControl IActiveScriptSiteUIControl_iface;
75 IActiveScriptSiteDebug IActiveScriptSiteDebug_iface;
76 IServiceProvider IServiceProvider_iface;
78 LONG ref;
80 IActiveScript *script;
81 IActiveScriptParse *parse;
82 IActiveScriptParseProcedure2 *parse_proc;
84 SCRIPTSTATE script_state;
86 HTMLInnerWindow *window;
88 GUID guid;
89 struct list entry;
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;
97 HRESULT hres;
99 hres = IActiveScript_QueryInterface(script, &IID_IActiveScriptProperty,
100 (void**)&script_prop);
101 if(FAILED(hres)) {
102 WARN("Could not get IActiveScriptProperty iface: %08lx\n", hres);
103 return FALSE;
106 hres = IActiveScriptProperty_SetProperty(script_prop, property, NULL, val);
107 IActiveScriptProperty_Release(script_prop);
108 if(FAILED(hres)) {
109 WARN("SetProperty(%lx) failed: %08lx\n", property, hres);
110 return FALSE;
113 return TRUE;
116 static BOOL init_script_engine(ScriptHost *script_host, IActiveScript *script)
118 compat_mode_t compat_mode;
119 IObjectSafety *safety;
120 SCRIPTSTATE state;
121 DWORD supported_opts=0, enabled_opts=0, script_mode;
122 VARIANT var;
123 HRESULT hres;
125 hres = IActiveScript_QueryInterface(script, &IID_IActiveScriptParse, (void**)&script_host->parse);
126 if(FAILED(hres)) {
127 WARN("Could not get IActiveScriptParse: %08lx\n", hres);
128 return FALSE;
131 hres = IActiveScript_QueryInterface(script, &IID_IObjectSafety, (void**)&safety);
132 if(FAILED(hres)) {
133 FIXME("Could not get IObjectSafety: %08lx\n", hres);
134 return FALSE;
137 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported_opts, &enabled_opts);
138 if(FAILED(hres)) {
139 FIXME("GetInterfaceSafetyOptions failed: %08lx\n", hres);
140 }else if(!(supported_opts & INTERFACE_USES_DISPEX)) {
141 FIXME("INTERFACE_USES_DISPEX is not supported\n");
142 }else {
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);
146 if(FAILED(hres))
147 FIXME("SetInterfaceSafetyOptions failed: %08lx\n", hres);
150 IObjectSafety_Release(safety);
151 if(FAILED(hres))
152 return FALSE;
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;
163 V_VT(&var) = VT_I4;
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);
177 if(FAILED(hres)) {
178 WARN("InitNew failed: %08lx\n", hres);
179 return FALSE;
182 hres = IActiveScript_SetScriptSite(script, &script_host->IActiveScriptSite_iface);
183 if(FAILED(hres)) {
184 WARN("SetScriptSite failed: %08lx\n", hres);
185 IActiveScript_Close(script);
186 return FALSE;
189 hres = IActiveScript_GetScriptState(script, &state);
190 if(FAILED(hres))
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);
196 if(FAILED(hres)) {
197 WARN("Starting script failed: %08lx\n", hres);
198 return FALSE;
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;
212 break;
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 */
218 if(!V_BOOL(&var)) {
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;
225 break;
228 if(is_second_init)
229 set_script_prop(first_host->script, SCRIPTPROP_ABBREVIATE_GLOBALNAME_RESOLUTION, &var);
231 }else {
232 WARN("AddNamedItem failed: %08lx\n", hres);
235 hres = IActiveScript_QueryInterface(script, &IID_IActiveScriptParseProcedure2,
236 (void**)&script_host->parse_proc);
237 if(FAILED(hres)) {
238 /* FIXME: QI for IActiveScriptParseProcedure */
239 WARN("Could not get IActiveScriptParseProcedure iface: %08lx\n", hres);
242 return TRUE;
245 static void release_script_engine(ScriptHost *This)
247 if(!This->script)
248 return;
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);
259 default:
260 if(This->parse_proc) {
261 IActiveScriptParseProcedure2_Release(This->parse_proc);
262 This->parse_proc = NULL;
265 if(This->parse) {
266 IActiveScriptParse_Release(This->parse);
267 This->parse = NULL;
271 IActiveScript_Release(This->script);
272 This->script = NULL;
273 This->script_state = SCRIPTSTATE_UNINITIALIZED;
276 void connect_scripts(HTMLInnerWindow *window)
278 ScriptHost *iter;
280 LIST_FOR_EACH_ENTRY(iter, &window->script_hosts, ScriptHost, entry) {
281 if(iter->script_state == SCRIPTSTATE_STARTED)
282 IActiveScript_SetScriptState(iter->script, SCRIPTSTATE_CONNECTED);
286 static inline ScriptHost *impl_from_IActiveScriptSite(IActiveScriptSite *iface)
288 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSite_iface);
291 static HRESULT WINAPI ActiveScriptSite_QueryInterface(IActiveScriptSite *iface, REFIID riid, void **ppv)
293 ScriptHost *This = impl_from_IActiveScriptSite(iface);
295 *ppv = NULL;
297 if(IsEqualGUID(&IID_IUnknown, riid)) {
298 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
299 *ppv = &This->IActiveScriptSite_iface;
300 }else if(IsEqualGUID(&IID_IActiveScriptSite, riid)) {
301 TRACE("(%p)->(IID_IActiveScriptSite %p)\n", This, ppv);
302 *ppv = &This->IActiveScriptSite_iface;
303 }else if(IsEqualGUID(&IID_IActiveScriptSiteInterruptPoll, riid)) {
304 TRACE("(%p)->(IID_IActiveScriptSiteInterruprtPoll %p)\n", This, ppv);
305 *ppv = &This->IActiveScriptSiteInterruptPoll_iface;
306 }else if(IsEqualGUID(&IID_IActiveScriptSiteWindow, riid)) {
307 TRACE("(%p)->(IID_IActiveScriptSiteWindow %p)\n", This, ppv);
308 *ppv = &This->IActiveScriptSiteWindow_iface;
309 }else if(IsEqualGUID(&IID_IActiveScriptSiteUIControl, riid)) {
310 TRACE("(%p)->(IID_IActiveScriptSiteUIControl %p)\n", This, ppv);
311 *ppv = &This->IActiveScriptSiteUIControl_iface;
312 }else if(IsEqualGUID(&IID_IActiveScriptSiteDebug, riid)) {
313 TRACE("(%p)->(IID_IActiveScriptSiteDebug %p)\n", This, ppv);
314 *ppv = &This->IActiveScriptSiteDebug_iface;
315 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
316 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
317 *ppv = &This->IServiceProvider_iface;
318 }else if(IsEqualGUID(&IID_ICanHandleException, riid)) {
319 TRACE("(%p)->(IID_ICanHandleException not supported %p)\n", This, ppv);
320 return E_NOINTERFACE;
321 }else {
322 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
323 return E_NOINTERFACE;
326 IUnknown_AddRef((IUnknown*)*ppv);
327 return S_OK;
330 static ULONG WINAPI ActiveScriptSite_AddRef(IActiveScriptSite *iface)
332 ScriptHost *This = impl_from_IActiveScriptSite(iface);
333 LONG ref = InterlockedIncrement(&This->ref);
335 TRACE("(%p) ref=%ld\n", This, ref);
337 return ref;
340 static ULONG WINAPI ActiveScriptSite_Release(IActiveScriptSite *iface)
342 ScriptHost *This = impl_from_IActiveScriptSite(iface);
343 LONG ref = InterlockedDecrement(&This->ref);
345 TRACE("(%p) ref=%ld\n", This, ref);
347 if(!ref) {
348 release_script_engine(This);
349 if(This->window)
350 list_remove(&This->entry);
351 free(This);
354 return ref;
357 static HRESULT WINAPI ActiveScriptSite_GetLCID(IActiveScriptSite *iface, LCID *plcid)
359 ScriptHost *This = impl_from_IActiveScriptSite(iface);
361 TRACE("(%p)->(%p)\n", This, plcid);
363 *plcid = GetUserDefaultLCID();
364 return S_OK;
367 static HRESULT WINAPI ActiveScriptSite_GetItemInfo(IActiveScriptSite *iface, LPCOLESTR pstrName,
368 DWORD dwReturnMask, IUnknown **ppiunkItem, ITypeInfo **ppti)
370 ScriptHost *This = impl_from_IActiveScriptSite(iface);
372 TRACE("(%p)->(%s %lx %p %p)\n", This, debugstr_w(pstrName), dwReturnMask, ppiunkItem, ppti);
374 if(dwReturnMask != SCRIPTINFO_IUNKNOWN) {
375 FIXME("Unsupported mask %lx\n", dwReturnMask);
376 return E_NOTIMPL;
379 *ppiunkItem = NULL;
381 if(wcscmp(pstrName, L"window"))
382 return DISP_E_MEMBERNOTFOUND;
384 if(!This->window)
385 return E_FAIL;
387 /* FIXME: Return proxy object */
388 *ppiunkItem = (IUnknown*)&This->window->base.IHTMLWindow2_iface;
389 IUnknown_AddRef(*ppiunkItem);
391 return S_OK;
394 static HRESULT WINAPI ActiveScriptSite_GetDocVersionString(IActiveScriptSite *iface, BSTR *pbstrVersion)
396 ScriptHost *This = impl_from_IActiveScriptSite(iface);
397 FIXME("(%p)->(%p)\n", This, pbstrVersion);
398 return E_NOTIMPL;
401 static HRESULT WINAPI ActiveScriptSite_OnScriptTerminate(IActiveScriptSite *iface,
402 const VARIANT *pvarResult, const EXCEPINFO *pexcepinfo)
404 ScriptHost *This = impl_from_IActiveScriptSite(iface);
405 FIXME("(%p)->(%p %p)\n", This, pvarResult, pexcepinfo);
406 return E_NOTIMPL;
409 static HRESULT WINAPI ActiveScriptSite_OnStateChange(IActiveScriptSite *iface, SCRIPTSTATE ssScriptState)
411 ScriptHost *This = impl_from_IActiveScriptSite(iface);
413 TRACE("(%p)->(%x)\n", This, ssScriptState);
415 This->script_state = ssScriptState;
416 return S_OK;
419 static HRESULT WINAPI ActiveScriptSite_OnScriptError(IActiveScriptSite *iface, IActiveScriptError *pscripterror)
421 ScriptHost *This = impl_from_IActiveScriptSite(iface);
422 FIXME("(%p)->(%p)\n", This, pscripterror);
423 return E_NOTIMPL;
426 static HRESULT WINAPI ActiveScriptSite_OnEnterScript(IActiveScriptSite *iface)
428 ScriptHost *This = impl_from_IActiveScriptSite(iface);
430 TRACE("(%p)->()\n", This);
432 return S_OK;
435 static HRESULT WINAPI ActiveScriptSite_OnLeaveScript(IActiveScriptSite *iface)
437 ScriptHost *This = impl_from_IActiveScriptSite(iface);
439 TRACE("(%p)->()\n", This);
441 return S_OK;
444 static const IActiveScriptSiteVtbl ActiveScriptSiteVtbl = {
445 ActiveScriptSite_QueryInterface,
446 ActiveScriptSite_AddRef,
447 ActiveScriptSite_Release,
448 ActiveScriptSite_GetLCID,
449 ActiveScriptSite_GetItemInfo,
450 ActiveScriptSite_GetDocVersionString,
451 ActiveScriptSite_OnScriptTerminate,
452 ActiveScriptSite_OnStateChange,
453 ActiveScriptSite_OnScriptError,
454 ActiveScriptSite_OnEnterScript,
455 ActiveScriptSite_OnLeaveScript
458 static inline ScriptHost *impl_from_IActiveScriptSiteInterruptPoll(IActiveScriptSiteInterruptPoll *iface)
460 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSiteInterruptPoll_iface);
463 static HRESULT WINAPI ActiveScriptSiteInterruptPoll_QueryInterface(IActiveScriptSiteInterruptPoll *iface,
464 REFIID riid, void **ppv)
466 ScriptHost *This = impl_from_IActiveScriptSiteInterruptPoll(iface);
467 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
470 static ULONG WINAPI ActiveScriptSiteInterruptPoll_AddRef(IActiveScriptSiteInterruptPoll *iface)
472 ScriptHost *This = impl_from_IActiveScriptSiteInterruptPoll(iface);
473 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
476 static ULONG WINAPI ActiveScriptSiteInterruptPoll_Release(IActiveScriptSiteInterruptPoll *iface)
478 ScriptHost *This = impl_from_IActiveScriptSiteInterruptPoll(iface);
479 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
482 static HRESULT WINAPI ActiveScriptSiteInterruptPoll_QueryContinue(IActiveScriptSiteInterruptPoll *iface)
484 ScriptHost *This = impl_from_IActiveScriptSiteInterruptPoll(iface);
486 TRACE("(%p)\n", This);
488 return S_OK;
491 static const IActiveScriptSiteInterruptPollVtbl ActiveScriptSiteInterruptPollVtbl = {
492 ActiveScriptSiteInterruptPoll_QueryInterface,
493 ActiveScriptSiteInterruptPoll_AddRef,
494 ActiveScriptSiteInterruptPoll_Release,
495 ActiveScriptSiteInterruptPoll_QueryContinue
498 static inline ScriptHost *impl_from_IActiveScriptSiteWindow(IActiveScriptSiteWindow *iface)
500 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSiteWindow_iface);
503 static HRESULT WINAPI ActiveScriptSiteWindow_QueryInterface(IActiveScriptSiteWindow *iface,
504 REFIID riid, void **ppv)
506 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
507 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
510 static ULONG WINAPI ActiveScriptSiteWindow_AddRef(IActiveScriptSiteWindow *iface)
512 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
513 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
516 static ULONG WINAPI ActiveScriptSiteWindow_Release(IActiveScriptSiteWindow *iface)
518 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
519 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
522 static HRESULT WINAPI ActiveScriptSiteWindow_GetWindow(IActiveScriptSiteWindow *iface, HWND *phwnd)
524 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
526 TRACE("(%p)->(%p)\n", This, phwnd);
528 if(!This->window || !This->window->base.outer_window)
529 return E_UNEXPECTED;
531 *phwnd = This->window->base.outer_window->browser->doc->hwnd;
532 return S_OK;
535 static HRESULT WINAPI ActiveScriptSiteWindow_EnableModeless(IActiveScriptSiteWindow *iface, BOOL fEnable)
537 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
538 FIXME("(%p)->(%x)\n", This, fEnable);
539 return S_OK;
542 static const IActiveScriptSiteWindowVtbl ActiveScriptSiteWindowVtbl = {
543 ActiveScriptSiteWindow_QueryInterface,
544 ActiveScriptSiteWindow_AddRef,
545 ActiveScriptSiteWindow_Release,
546 ActiveScriptSiteWindow_GetWindow,
547 ActiveScriptSiteWindow_EnableModeless
550 static inline ScriptHost *impl_from_IActiveScriptSiteUIControl(IActiveScriptSiteUIControl *iface)
552 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSiteUIControl_iface);
555 static HRESULT WINAPI ActiveScriptSiteUIControl_QueryInterface(IActiveScriptSiteUIControl *iface, REFIID riid, void **ppv)
557 ScriptHost *This = impl_from_IActiveScriptSiteUIControl(iface);
558 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
561 static ULONG WINAPI ActiveScriptSiteUIControl_AddRef(IActiveScriptSiteUIControl *iface)
563 ScriptHost *This = impl_from_IActiveScriptSiteUIControl(iface);
564 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
567 static ULONG WINAPI ActiveScriptSiteUIControl_Release(IActiveScriptSiteUIControl *iface)
569 ScriptHost *This = impl_from_IActiveScriptSiteUIControl(iface);
570 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
573 static HRESULT WINAPI ActiveScriptSiteUIControl_GetUIBehavior(IActiveScriptSiteUIControl *iface, SCRIPTUICITEM UicItem,
574 SCRIPTUICHANDLING *pUicHandling)
576 ScriptHost *This = impl_from_IActiveScriptSiteUIControl(iface);
578 WARN("(%p)->(%d %p) semi-stub\n", This, UicItem, pUicHandling);
580 *pUicHandling = SCRIPTUICHANDLING_ALLOW;
581 return S_OK;
584 static const IActiveScriptSiteUIControlVtbl ActiveScriptSiteUIControlVtbl = {
585 ActiveScriptSiteUIControl_QueryInterface,
586 ActiveScriptSiteUIControl_AddRef,
587 ActiveScriptSiteUIControl_Release,
588 ActiveScriptSiteUIControl_GetUIBehavior
591 static inline ScriptHost *impl_from_IActiveScriptSiteDebug(IActiveScriptSiteDebug *iface)
593 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSiteDebug_iface);
596 static HRESULT WINAPI ActiveScriptSiteDebug_QueryInterface(IActiveScriptSiteDebug *iface,
597 REFIID riid, void **ppv)
599 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
600 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
603 static ULONG WINAPI ActiveScriptSiteDebug_AddRef(IActiveScriptSiteDebug *iface)
605 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
606 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
609 static ULONG WINAPI ActiveScriptSiteDebug_Release(IActiveScriptSiteDebug *iface)
611 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
612 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
615 static HRESULT WINAPI ActiveScriptSiteDebug_GetDocumentContextFromPosition(IActiveScriptSiteDebug *iface,
616 CTXARG_T dwSourceContext, ULONG uCharacterOffset, ULONG uNumChars, IDebugDocumentContext **ppsc)
618 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
619 FIXME("(%p)->(%s %lu %lu %p)\n", This, wine_dbgstr_longlong(dwSourceContext), uCharacterOffset,
620 uNumChars, ppsc);
621 return E_NOTIMPL;
624 static HRESULT WINAPI ActiveScriptSiteDebug_GetApplication(IActiveScriptSiteDebug *iface, IDebugApplication **ppda)
626 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
627 FIXME("(%p)->(%p)\n", This, ppda);
628 return E_NOTIMPL;
631 static HRESULT WINAPI ActiveScriptSiteDebug_GetRootApplicationNode(IActiveScriptSiteDebug *iface,
632 IDebugApplicationNode **ppdanRoot)
634 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
635 FIXME("(%p)->(%p)\n", This, ppdanRoot);
636 return E_NOTIMPL;
639 static HRESULT WINAPI ActiveScriptSiteDebug_OnScriptErrorDebug(IActiveScriptSiteDebug *iface,
640 IActiveScriptErrorDebug *pErrorDebug, BOOL *pfEnterDebugger, BOOL *pfCallOnScriptErrorWhenContinuing)
642 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
643 FIXME("(%p)->(%p %p %p)\n", This, pErrorDebug, pfEnterDebugger, pfCallOnScriptErrorWhenContinuing);
644 return E_NOTIMPL;
647 static const IActiveScriptSiteDebugVtbl ActiveScriptSiteDebugVtbl = {
648 ActiveScriptSiteDebug_QueryInterface,
649 ActiveScriptSiteDebug_AddRef,
650 ActiveScriptSiteDebug_Release,
651 ActiveScriptSiteDebug_GetDocumentContextFromPosition,
652 ActiveScriptSiteDebug_GetApplication,
653 ActiveScriptSiteDebug_GetRootApplicationNode,
654 ActiveScriptSiteDebug_OnScriptErrorDebug
657 static inline ScriptHost *impl_from_IServiceProvider(IServiceProvider *iface)
659 return CONTAINING_RECORD(iface, ScriptHost, IServiceProvider_iface);
662 static HRESULT WINAPI ASServiceProvider_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
664 ScriptHost *This = impl_from_IServiceProvider(iface);
665 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
668 static ULONG WINAPI ASServiceProvider_AddRef(IServiceProvider *iface)
670 ScriptHost *This = impl_from_IServiceProvider(iface);
671 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
674 static ULONG WINAPI ASServiceProvider_Release(IServiceProvider *iface)
676 ScriptHost *This = impl_from_IServiceProvider(iface);
677 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
680 static HRESULT WINAPI ASServiceProvider_QueryService(IServiceProvider *iface, REFGUID guidService,
681 REFIID riid, void **ppv)
683 ScriptHost *This = impl_from_IServiceProvider(iface);
685 if(IsEqualGUID(&SID_SInternetHostSecurityManager, guidService)) {
686 TRACE("(%p)->(SID_SInternetHostSecurityManager)\n", This);
688 if(!This->window || !This->window->doc)
689 return E_NOINTERFACE;
691 return IInternetHostSecurityManager_QueryInterface(&This->window->doc->IInternetHostSecurityManager_iface,
692 riid, ppv);
695 if(IsEqualGUID(&SID_SContainerDispatch, guidService)) {
696 TRACE("(%p)->(SID_SContainerDispatch)\n", This);
698 if(!This->window || !This->window->doc)
699 return E_NOINTERFACE;
701 return IHTMLDocument2_QueryInterface(&This->window->doc->IHTMLDocument2_iface, riid, ppv);
704 FIXME("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
705 return E_NOINTERFACE;
708 static const IServiceProviderVtbl ASServiceProviderVtbl = {
709 ASServiceProvider_QueryInterface,
710 ASServiceProvider_AddRef,
711 ASServiceProvider_Release,
712 ASServiceProvider_QueryService
715 static ScriptHost *create_script_host(HTMLInnerWindow *window, const GUID *guid)
717 IActiveScript *script;
718 ScriptHost *ret;
719 HRESULT hres;
721 ret = calloc(1, sizeof(*ret));
722 if(!ret)
723 return NULL;
725 ret->IActiveScriptSite_iface.lpVtbl = &ActiveScriptSiteVtbl;
726 ret->IActiveScriptSiteInterruptPoll_iface.lpVtbl = &ActiveScriptSiteInterruptPollVtbl;
727 ret->IActiveScriptSiteWindow_iface.lpVtbl = &ActiveScriptSiteWindowVtbl;
728 ret->IActiveScriptSiteUIControl_iface.lpVtbl = &ActiveScriptSiteUIControlVtbl;
729 ret->IActiveScriptSiteDebug_iface.lpVtbl = &ActiveScriptSiteDebugVtbl;
730 ret->IServiceProvider_iface.lpVtbl = &ASServiceProviderVtbl;
731 ret->ref = 1;
732 ret->window = window;
733 ret->script_state = SCRIPTSTATE_UNINITIALIZED;
735 ret->guid = *guid;
736 list_add_tail(&window->script_hosts, &ret->entry);
738 hres = CoCreateInstance(&ret->guid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
739 &IID_IActiveScript, (void**)&script);
740 if(FAILED(hres))
741 WARN("Could not load script engine: %08lx\n", hres);
742 else {
743 BOOL succeeded = init_script_engine(ret, script);
744 ret->script = script;
745 if(!succeeded)
746 release_script_engine(ret);
749 return ret;
752 static void dispatch_script_readystatechange_event(HTMLScriptElement *script)
754 DOMEvent *event;
755 HRESULT hres;
757 if(script->readystate != READYSTATE_LOADED ||
758 dispex_compat_mode(&script->element.node.event_target.dispex) < COMPAT_MODE_IE10) {
759 hres = create_document_event(script->element.node.doc, EVENTID_READYSTATECHANGE, &event);
760 if(SUCCEEDED(hres)) {
761 dispatch_event(&script->element.node.event_target, event);
762 IDOMEvent_Release(&event->IDOMEvent_iface);
766 if(script->readystate == READYSTATE_LOADED) {
767 hres = create_document_event(script->element.node.doc, EVENTID_LOAD, &event);
768 if(SUCCEEDED(hres)) {
769 dispatch_event(&script->element.node.event_target, event);
770 IDOMEvent_Release(&event->IDOMEvent_iface);
775 typedef struct {
776 event_task_t header;
777 HTMLScriptElement *elem;
778 } fire_readystatechange_task_t;
780 static void fire_readystatechange_proc(event_task_t *_task)
782 fire_readystatechange_task_t *task = (fire_readystatechange_task_t*)_task;
784 if(!task->elem->pending_readystatechange_event)
785 return;
787 task->elem->pending_readystatechange_event = FALSE;
788 dispatch_script_readystatechange_event(task->elem);
791 static void fire_readystatechange_task_destr(event_task_t *_task)
793 fire_readystatechange_task_t *task = (fire_readystatechange_task_t*)_task;
795 IHTMLScriptElement_Release(&task->elem->IHTMLScriptElement_iface);
798 static void set_script_elem_readystate(HTMLScriptElement *script_elem, READYSTATE readystate)
800 script_elem->readystate = readystate;
802 if(readystate != READYSTATE_LOADED &&
803 dispex_compat_mode(&script_elem->element.node.event_target.dispex) >= COMPAT_MODE_IE11)
804 return;
806 if(readystate != READYSTATE_INTERACTIVE) {
807 if(!script_elem->element.node.doc->window->parser_callback_cnt) {
808 fire_readystatechange_task_t *task;
809 HRESULT hres;
811 if(script_elem->pending_readystatechange_event)
812 return;
814 task = malloc(sizeof(*task));
815 if(!task)
816 return;
818 IHTMLScriptElement_AddRef(&script_elem->IHTMLScriptElement_iface);
819 task->elem = script_elem;
821 hres = push_event_task(&task->header, script_elem->element.node.doc->window, fire_readystatechange_proc,
822 fire_readystatechange_task_destr, script_elem->element.node.doc->window->task_magic);
823 if(SUCCEEDED(hres))
824 script_elem->pending_readystatechange_event = TRUE;
825 }else {
826 script_elem->pending_readystatechange_event = FALSE;
827 dispatch_script_readystatechange_event(script_elem);
832 static void parse_elem_text(ScriptHost *script_host, HTMLScriptElement *script_elem, LPCWSTR text)
834 EXCEPINFO excepinfo;
835 VARIANT var;
836 HRESULT hres;
838 TRACE("%s\n", debugstr_w(text));
840 VariantInit(&var);
841 memset(&excepinfo, 0, sizeof(excepinfo));
842 TRACE(">>>\n");
843 hres = IActiveScriptParse_ParseScriptText(script_host->parse, text, L"window", NULL, L"</SCRIPT>",
844 0, 0, SCRIPTTEXT_ISVISIBLE|SCRIPTTEXT_HOSTMANAGESSOURCE,
845 &var, &excepinfo);
846 if(SUCCEEDED(hres))
847 TRACE("<<<\n");
848 else
849 WARN("<<< %08lx\n", hres);
852 typedef struct {
853 BSCallback bsc;
855 HTMLScriptElement *script_elem;
856 nsILoadGroup *load_group;
857 nsIRequest *request;
858 DWORD scheme;
860 DWORD size;
861 char *buf;
862 } ScriptBSC;
864 static HRESULT get_binding_text(ScriptBSC *bsc, WCHAR **ret)
866 UINT cp = CP_UTF8;
867 WCHAR *text;
869 if(!bsc->bsc.read) {
870 text = malloc(sizeof(WCHAR));
871 if(!text)
872 return E_OUTOFMEMORY;
873 *text = 0;
874 *ret = text;
875 return S_OK;
878 switch(bsc->bsc.bom) {
879 case BOM_UTF16:
880 if(bsc->bsc.read % sizeof(WCHAR)) {
881 FIXME("The buffer is not a valid utf16 string\n");
882 return E_FAIL;
885 text = malloc(bsc->bsc.read + sizeof(WCHAR));
886 if(!text)
887 return E_OUTOFMEMORY;
889 memcpy(text, bsc->buf, bsc->bsc.read);
890 text[bsc->bsc.read/sizeof(WCHAR)] = 0;
891 break;
893 default:
894 /* FIXME: Try to use charset from HTTP headers first */
895 cp = get_document_charset(bsc->script_elem->element.node.doc);
896 /* fall through */
897 case BOM_UTF8: {
898 DWORD len;
900 len = MultiByteToWideChar(cp, 0, bsc->buf, bsc->bsc.read, NULL, 0);
901 text = malloc((len + 1) * sizeof(WCHAR));
902 if(!text)
903 return E_OUTOFMEMORY;
905 MultiByteToWideChar(cp, 0, bsc->buf, bsc->bsc.read, text, len);
906 text[len] = 0;
910 *ret = text;
911 return S_OK;
914 static void script_file_available(ScriptBSC *bsc)
916 HTMLScriptElement *script_elem = bsc->script_elem;
917 HTMLInnerWindow *window = bsc->bsc.window;
918 ScriptHost *script_host;
919 nsIDOMNode *parent;
920 nsresult nsres;
921 HRESULT hres;
923 assert(window != NULL);
925 hres = get_binding_text(bsc, &script_elem->src_text);
926 if(FAILED(hres))
927 return;
929 script_host = get_elem_script_host(window, script_elem);
930 if(!script_host)
931 return;
933 if(window->parser_callback_cnt) {
934 script_queue_entry_t *queue;
936 TRACE("Adding to queue\n");
938 queue = malloc(sizeof(*queue));
939 if(!queue)
940 return;
942 IHTMLScriptElement_AddRef(&script_elem->IHTMLScriptElement_iface);
943 queue->script = script_elem;
945 list_add_tail(&window->script_queue, &queue->entry);
946 return;
949 nsres = nsIDOMElement_GetParentNode(script_elem->element.dom_element, &parent);
950 if(NS_FAILED(nsres) || !parent) {
951 TRACE("No parent, not executing\n");
952 script_elem->parse_on_bind = TRUE;
953 return;
956 nsIDOMNode_Release(parent);
958 script_host = get_elem_script_host(window, script_elem);
959 if(!script_host)
960 return;
962 script_elem->parsed = TRUE;
963 if(script_host->parse)
964 parse_elem_text(script_host, script_elem, script_elem->src_text);
967 static inline ScriptBSC *impl_from_BSCallback(BSCallback *iface)
969 return CONTAINING_RECORD(iface, ScriptBSC, bsc);
972 static void ScriptBSC_destroy(BSCallback *bsc)
974 ScriptBSC *This = impl_from_BSCallback(bsc);
976 if(This->script_elem) {
977 IHTMLScriptElement_Release(&This->script_elem->IHTMLScriptElement_iface);
978 This->script_elem = NULL;
981 if(This->request) {
982 ERR("Unfinished request\n");
983 nsIRequest_Release(This->request);
985 if(This->load_group)
986 nsILoadGroup_Release(This->load_group);
988 free(This->buf);
989 free(This);
992 static HRESULT ScriptBSC_init_bindinfo(BSCallback *bsc)
994 return S_OK;
997 static HRESULT ScriptBSC_start_binding(BSCallback *bsc)
999 ScriptBSC *This = impl_from_BSCallback(bsc);
1000 nsresult nsres;
1002 This->script_elem->binding = &This->bsc;
1004 if(This->load_group) {
1005 nsres = create_onload_blocker_request(&This->request);
1006 if(NS_SUCCEEDED(nsres)) {
1007 nsres = nsILoadGroup_AddRequest(This->load_group, This->request, NULL);
1008 if(NS_FAILED(nsres))
1009 ERR("AddRequest failed: %08lx\n", nsres);
1013 /* FIXME: We should find a better to decide if 'loading' state is supposed to be used by the protocol. */
1014 if(This->scheme == URL_SCHEME_HTTPS || This->scheme == URL_SCHEME_HTTP)
1015 set_script_elem_readystate(This->script_elem, READYSTATE_LOADING);
1017 return S_OK;
1020 static HRESULT ScriptBSC_stop_binding(BSCallback *bsc, HRESULT result)
1022 ScriptBSC *This = impl_from_BSCallback(bsc);
1023 nsresult nsres;
1025 if(SUCCEEDED(result) && !This->script_elem)
1026 result = E_UNEXPECTED;
1028 assert(FAILED(result) || This->script_elem->binding == &This->bsc);
1029 This->script_elem->binding = NULL;
1031 if(This->script_elem->readystate == READYSTATE_LOADING)
1032 set_script_elem_readystate(This->script_elem, READYSTATE_LOADED);
1034 if(SUCCEEDED(result)) {
1035 script_file_available(This);
1036 }else {
1037 FIXME("binding failed %08lx\n", result);
1038 free(This->buf);
1039 This->buf = NULL;
1040 This->size = 0;
1043 if(This->request) {
1044 nsres = nsILoadGroup_RemoveRequest(This->load_group, This->request, NULL, NS_OK);
1045 if(NS_FAILED(nsres))
1046 ERR("RemoveRequest failed: %08lx\n", nsres);
1047 nsIRequest_Release(This->request);
1048 This->request = NULL;
1051 IHTMLScriptElement_Release(&This->script_elem->IHTMLScriptElement_iface);
1052 This->script_elem = NULL;
1053 return S_OK;
1056 static HRESULT ScriptBSC_read_data(BSCallback *bsc, IStream *stream)
1058 ScriptBSC *This = impl_from_BSCallback(bsc);
1059 DWORD read;
1060 HRESULT hres;
1062 if(!This->buf) {
1063 This->buf = malloc(128);
1064 if(!This->buf)
1065 return E_OUTOFMEMORY;
1066 This->size = 128;
1069 do {
1070 if(This->bsc.read >= This->size) {
1071 void *new_buf;
1072 new_buf = realloc(This->buf, This->size << 1);
1073 if(!new_buf)
1074 return E_OUTOFMEMORY;
1075 This->size <<= 1;
1076 This->buf = new_buf;
1079 hres = read_stream(&This->bsc, stream, This->buf+This->bsc.read, This->size-This->bsc.read, &read);
1080 }while(hres == S_OK);
1082 return S_OK;
1085 static HRESULT ScriptBSC_on_progress(BSCallback *bsc, ULONG progress, ULONG total, ULONG status_code, LPCWSTR status_text)
1087 return S_OK;
1090 static HRESULT ScriptBSC_on_response(BSCallback *bsc, DWORD response_code,
1091 LPCWSTR response_headers)
1093 return S_OK;
1096 static HRESULT ScriptBSC_beginning_transaction(BSCallback *bsc, WCHAR **additional_headers)
1098 return S_FALSE;
1101 static const BSCallbackVtbl ScriptBSCVtbl = {
1102 ScriptBSC_destroy,
1103 ScriptBSC_init_bindinfo,
1104 ScriptBSC_start_binding,
1105 ScriptBSC_stop_binding,
1106 ScriptBSC_read_data,
1107 ScriptBSC_on_progress,
1108 ScriptBSC_on_response,
1109 ScriptBSC_beginning_transaction
1113 HRESULT load_script(HTMLScriptElement *script_elem, const WCHAR *src, BOOL async)
1115 HTMLInnerWindow *window;
1116 ScriptBSC *bsc;
1117 IMoniker *mon;
1118 IUri *uri;
1119 HRESULT hres;
1121 static const WCHAR wine_schemaW[] = {'w','i','n','e',':'};
1123 if(lstrlenW(src) > ARRAY_SIZE(wine_schemaW) && !memcmp(src, wine_schemaW, sizeof(wine_schemaW)))
1124 src += ARRAY_SIZE(wine_schemaW);
1126 TRACE("(%p %s %x)\n", script_elem, debugstr_w(src), async);
1128 if(!script_elem->element.node.doc || !(window = script_elem->element.node.doc->window)) {
1129 ERR("no window\n");
1130 return E_UNEXPECTED;
1133 hres = create_uri(src, 0, &uri);
1134 if(FAILED(hres))
1135 return hres;
1137 hres = CreateURLMonikerEx2(NULL, uri, &mon, URL_MK_UNIFORM);
1138 if(FAILED(hres)) {
1139 IUri_Release(uri);
1140 return hres;
1143 bsc = calloc(1, sizeof(*bsc));
1144 if(!bsc) {
1145 IMoniker_Release(mon);
1146 IUri_Release(uri);
1147 return E_OUTOFMEMORY;
1150 init_bscallback(&bsc->bsc, &ScriptBSCVtbl, mon, async ? BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA : 0);
1151 IMoniker_Release(mon);
1153 hres = IUri_GetScheme(uri, &bsc->scheme);
1154 IUri_Release(uri);
1155 if(hres != S_OK)
1156 bsc->scheme = URL_SCHEME_UNKNOWN;
1158 IHTMLScriptElement_AddRef(&script_elem->IHTMLScriptElement_iface);
1159 bsc->script_elem = script_elem;
1161 if(window->bscallback && window->bscallback->nschannel &&
1162 window->bscallback->nschannel->load_group) {
1163 cpp_bool contains;
1164 nsresult nsres;
1166 nsres = nsIDOMNode_Contains(script_elem->element.node.doc->node.nsnode,
1167 script_elem->element.node.nsnode, &contains);
1168 if(NS_SUCCEEDED(nsres) && contains) {
1169 TRACE("script %p will block load event\n", script_elem);
1170 bsc->load_group = window->bscallback->nschannel->load_group;
1171 nsILoadGroup_AddRef(bsc->load_group);
1175 hres = start_binding(window, &bsc->bsc, NULL);
1177 IBindStatusCallback_Release(&bsc->bsc.IBindStatusCallback_iface);
1178 return hres;
1181 static void parse_inline_script(ScriptHost *script_host, HTMLScriptElement *script_elem)
1183 const PRUnichar *text;
1184 nsAString text_str;
1185 nsresult nsres;
1187 nsAString_Init(&text_str, NULL);
1188 nsres = nsIDOMHTMLScriptElement_GetText(script_elem->nsscript, &text_str);
1189 nsAString_GetData(&text_str, &text);
1191 set_script_elem_readystate(script_elem, READYSTATE_INTERACTIVE);
1193 if(NS_FAILED(nsres)) {
1194 ERR("GetText failed: %08lx\n", nsres);
1195 }else if(*text) {
1196 parse_elem_text(script_host, script_elem, text);
1199 nsAString_Finish(&text_str);
1202 static BOOL parse_script_elem(ScriptHost *script_host, HTMLScriptElement *script_elem)
1204 nsAString src_str, event_str;
1205 BOOL is_complete = FALSE;
1206 const PRUnichar *src;
1207 nsresult nsres;
1209 nsAString_Init(&event_str, NULL);
1210 nsres = nsIDOMHTMLScriptElement_GetEvent(script_elem->nsscript, &event_str);
1211 if(NS_SUCCEEDED(nsres)) {
1212 const PRUnichar *event;
1214 nsAString_GetData(&event_str, &event);
1215 if(*event) {
1216 TRACE("deferring event %s script evaluation\n", debugstr_w(event));
1217 nsAString_Finish(&event_str);
1218 return FALSE;
1220 }else {
1221 ERR("GetEvent failed: %08lx\n", nsres);
1223 nsAString_Finish(&event_str);
1225 nsAString_Init(&src_str, NULL);
1226 nsres = nsIDOMHTMLScriptElement_GetSrc(script_elem->nsscript, &src_str);
1227 nsAString_GetData(&src_str, &src);
1229 if(NS_FAILED(nsres)) {
1230 ERR("GetSrc failed: %08lx\n", nsres);
1231 }else if(*src) {
1232 load_script(script_elem, src, FALSE);
1233 is_complete = script_elem->parsed;
1234 }else {
1235 parse_inline_script(script_host, script_elem);
1236 is_complete = TRUE;
1239 nsAString_Finish(&src_str);
1241 return is_complete;
1244 static GUID get_default_script_guid(HTMLInnerWindow *window)
1246 /* If not specified, we should use very first script host that was created for the page (or JScript if none) */
1247 return list_empty(&window->script_hosts)
1248 ? CLSID_JScript
1249 : LIST_ENTRY(list_head(&window->script_hosts), ScriptHost, entry)->guid;
1252 static BOOL get_guid_from_type(LPCWSTR type, GUID *guid)
1254 /* FIXME: Handle more types */
1255 if(!wcsicmp(type, L"text/javascript") || !wcsicmp(type, L"text/jscript")) {
1256 *guid = CLSID_JScript;
1257 }else if(!wcsicmp(type, L"text/vbscript")) {
1258 *guid = CLSID_VBScript;
1259 }else {
1260 FIXME("Unknown type %s\n", debugstr_w(type));
1261 return FALSE;
1264 return TRUE;
1267 static BOOL get_guid_from_language(LPCWSTR type, GUID *guid)
1269 HRESULT hres;
1271 hres = CLSIDFromProgID(type, guid);
1272 if(FAILED(hres))
1273 return FALSE;
1275 /* FIXME: Check CATID_ActiveScriptParse */
1277 return TRUE;
1280 static BOOL get_script_guid(HTMLInnerWindow *window, nsIDOMHTMLScriptElement *nsscript, GUID *guid)
1282 nsIDOMElement *nselem;
1283 const PRUnichar *language;
1284 nsAString val_str;
1285 BOOL ret = FALSE;
1286 nsresult nsres;
1288 nsAString_Init(&val_str, NULL);
1290 nsres = nsIDOMHTMLScriptElement_GetType(nsscript, &val_str);
1291 if(NS_SUCCEEDED(nsres)) {
1292 const PRUnichar *type;
1294 nsAString_GetData(&val_str, &type);
1295 if(*type) {
1296 ret = get_guid_from_type(type, guid);
1297 nsAString_Finish(&val_str);
1298 return ret;
1300 }else {
1301 ERR("GetType failed: %08lx\n", nsres);
1304 nsres = nsIDOMHTMLScriptElement_QueryInterface(nsscript, &IID_nsIDOMElement, (void**)&nselem);
1305 assert(nsres == NS_OK);
1307 nsres = get_elem_attr_value(nselem, L"language", &val_str, &language);
1308 nsIDOMElement_Release(nselem);
1309 if(NS_SUCCEEDED(nsres)) {
1310 if(*language) {
1311 ret = get_guid_from_language(language, guid);
1312 }else {
1313 *guid = get_default_script_guid(window);
1314 ret = TRUE;
1316 nsAString_Finish(&val_str);
1319 return ret;
1322 static ScriptHost *get_script_host(HTMLInnerWindow *window, const GUID *guid)
1324 ScriptHost *iter;
1326 LIST_FOR_EACH_ENTRY(iter, &window->script_hosts, ScriptHost, entry) {
1327 if(IsEqualGUID(guid, &iter->guid))
1328 return iter;
1331 return create_script_host(window, guid);
1334 static ScriptHost *get_elem_script_host(HTMLInnerWindow *window, HTMLScriptElement *script_elem)
1336 GUID guid;
1338 if(!get_script_guid(window, script_elem->nsscript, &guid)) {
1339 WARN("Could not find script GUID\n");
1340 return NULL;
1343 if(IsEqualGUID(&CLSID_JScript, &guid) && (!window->doc->browser || window->doc->browser->script_mode != SCRIPTMODE_ACTIVESCRIPT)) {
1344 TRACE("Ignoring JScript\n");
1345 return NULL;
1348 return get_script_host(window, &guid);
1351 void doc_insert_script(HTMLInnerWindow *window, HTMLScriptElement *script_elem, BOOL from_parser)
1353 ScriptHost *script_host;
1354 BOOL is_complete = FALSE;
1356 script_host = get_elem_script_host(window, script_elem);
1357 if(!script_host)
1358 return;
1360 if(script_host->parse) {
1361 if(script_elem->src_text) {
1362 if(from_parser)
1363 set_script_elem_readystate(script_elem, READYSTATE_INTERACTIVE);
1364 script_elem->parsed = TRUE;
1365 parse_elem_text(script_host, script_elem, script_elem->src_text);
1366 is_complete = TRUE;
1367 }else if(!script_elem->binding) {
1368 is_complete = parse_script_elem(script_host, script_elem);
1372 if(is_complete)
1373 set_script_elem_readystate(script_elem, READYSTATE_COMPLETE);
1376 IDispatch *script_parse_event(HTMLInnerWindow *window, LPCWSTR text)
1378 ScriptHost *script_host;
1379 GUID guid;
1380 const WCHAR *ptr;
1381 IDispatch *disp;
1382 HRESULT hres;
1384 TRACE("%s\n", debugstr_w(text));
1386 for(ptr = text; iswalnum(*ptr); ptr++);
1387 if(*ptr == ':') {
1388 LPWSTR language;
1389 BOOL b;
1391 language = malloc((ptr - text + 1) * sizeof(WCHAR));
1392 if(!language)
1393 return NULL;
1395 memcpy(language, text, (ptr-text)*sizeof(WCHAR));
1396 language[ptr-text] = 0;
1398 b = get_guid_from_language(language, &guid);
1400 free(language);
1402 if(!b) {
1403 WARN("Could not find language\n");
1404 return NULL;
1407 ptr++;
1408 }else {
1409 ptr = text;
1410 guid = get_default_script_guid(window);
1413 if(IsEqualGUID(&CLSID_JScript, &guid)
1414 && (!window->doc->browser || window->doc->browser->script_mode != SCRIPTMODE_ACTIVESCRIPT)) {
1415 TRACE("Ignoring JScript\n");
1416 return NULL;
1419 script_host = get_script_host(window, &guid);
1420 if(!script_host || !script_host->parse_proc)
1421 return NULL;
1423 hres = IActiveScriptParseProcedure2_ParseProcedureText(script_host->parse_proc, ptr, NULL, L"",
1424 NULL, NULL, L"\"", 0 /* FIXME */, 0,
1425 SCRIPTPROC_HOSTMANAGESSOURCE|SCRIPTPROC_IMPLICIT_THIS|SCRIPTPROC_IMPLICIT_PARENTS, &disp);
1426 if(FAILED(hres)) {
1427 WARN("ParseProcedureText failed: %08lx\n", hres);
1428 return NULL;
1431 TRACE("ret %p\n", disp);
1432 return disp;
1435 HRESULT exec_script(HTMLInnerWindow *window, const WCHAR *code, const WCHAR *lang, VARIANT *ret)
1437 ScriptHost *script_host;
1438 EXCEPINFO ei;
1439 GUID guid;
1440 HRESULT hres;
1442 if(!get_guid_from_language(lang, &guid)) {
1443 WARN("Could not find script GUID\n");
1444 return CO_E_CLASSSTRING;
1447 script_host = get_script_host(window, &guid);
1448 if(!script_host) {
1449 FIXME("No script host\n");
1450 return E_FAIL;
1453 if(!script_host->parse) {
1454 FIXME("script_host->parse == NULL\n");
1455 return E_FAIL;
1458 memset(&ei, 0, sizeof(ei));
1459 TRACE(">>>\n");
1460 hres = IActiveScriptParse_ParseScriptText(script_host->parse, code, NULL, NULL, L"\"", 0, 0, SCRIPTTEXT_ISVISIBLE, ret, &ei);
1461 if(SUCCEEDED(hres))
1462 TRACE("<<<\n");
1463 else
1464 WARN("<<< %08lx\n", hres);
1466 return hres;
1469 IDispatch *get_script_disp(ScriptHost *script_host)
1471 IDispatch *disp;
1472 HRESULT hres;
1474 if(!script_host->script)
1475 return NULL;
1477 hres = IActiveScript_GetScriptDispatch(script_host->script, L"window", &disp);
1478 if(FAILED(hres))
1479 return NULL;
1481 return disp;
1484 static EventTarget *find_event_target(HTMLDocumentNode *doc, HTMLScriptElement *script_elem)
1486 EventTarget *event_target = NULL;
1487 const PRUnichar *target_id;
1488 nsAString target_id_str;
1489 nsresult nsres;
1490 HRESULT hres;
1492 nsAString_Init(&target_id_str, NULL);
1493 nsres = nsIDOMHTMLScriptElement_GetHtmlFor(script_elem->nsscript, &target_id_str);
1494 if(NS_FAILED(nsres)) {
1495 ERR("GetScriptFor failed: %08lx\n", nsres);
1496 nsAString_Finish(&target_id_str);
1497 return NULL;
1500 nsAString_GetData(&target_id_str, &target_id);
1501 if(!*target_id) {
1502 FIXME("Empty for attribute\n");
1503 }else if(!wcscmp(target_id, L"document")) {
1504 event_target = &doc->node.event_target;
1505 IHTMLDOMNode_AddRef(&doc->node.IHTMLDOMNode_iface);
1506 }else if(!wcscmp(target_id, L"window")) {
1507 if(doc->window) {
1508 event_target = &doc->window->event_target;
1509 IDispatchEx_AddRef(&event_target->dispex.IDispatchEx_iface);
1511 }else {
1512 HTMLElement *target_elem;
1514 hres = get_doc_elem_by_id(doc, target_id, &target_elem);
1515 if(SUCCEEDED(hres) && target_elem) {
1516 event_target = &target_elem->node.event_target;
1520 nsAString_Finish(&target_id_str);
1521 return event_target;
1524 static BOOL parse_event_str(WCHAR *event, const WCHAR **args)
1526 WCHAR *ptr;
1528 TRACE("%s\n", debugstr_w(event));
1530 for(ptr = event; iswalnum(*ptr); ptr++);
1531 if(!*ptr) {
1532 *args = NULL;
1533 return TRUE;
1536 if(*ptr != '(')
1537 return FALSE;
1539 *ptr++ = 0;
1540 *args = ptr;
1541 while(iswalnum(*ptr) || iswspace(*ptr) || *ptr == ',')
1542 ptr++;
1544 if(*ptr != ')')
1545 return FALSE;
1547 *ptr++ = 0;
1548 return !*ptr;
1551 static IDispatch *parse_event_elem(HTMLDocumentNode *doc, HTMLScriptElement *script_elem, WCHAR **ret_event)
1553 ScriptHost *script_host;
1554 WCHAR *event = NULL;
1555 const WCHAR *args;
1556 nsAString nsstr;
1557 IDispatch *disp;
1558 nsresult nsres;
1559 HRESULT hres;
1561 if(script_elem->parsed)
1562 return NULL;
1564 script_host = get_elem_script_host(doc->window, script_elem);
1565 if(!script_host || !script_host->parse_proc)
1566 return NULL;
1568 nsAString_Init(&nsstr, NULL);
1569 nsres = nsIDOMHTMLScriptElement_GetEvent(script_elem->nsscript, &nsstr);
1570 if(NS_SUCCEEDED(nsres)) {
1571 const PRUnichar *event_val;
1573 nsAString_GetData(&nsstr, &event_val);
1574 event = wcsdup(event_val);
1576 nsAString_Finish(&nsstr);
1577 if(!event)
1578 return NULL;
1580 if(!parse_event_str(event, &args)) {
1581 WARN("parsing %s failed\n", debugstr_w(event));
1582 free(event);
1583 return NULL;
1586 nsAString_Init(&nsstr, NULL);
1587 nsres = nsIDOMHTMLScriptElement_GetText(script_elem->nsscript, &nsstr);
1588 if(NS_SUCCEEDED(nsres)) {
1589 const PRUnichar *text;
1591 nsAString_GetData(&nsstr, &text);
1592 hres = IActiveScriptParseProcedure2_ParseProcedureText(script_host->parse_proc, text, args,
1593 L"", NULL, NULL, L"</SCRIPT>", 0, 0,
1594 SCRIPTPROC_HOSTMANAGESSOURCE|SCRIPTPROC_IMPLICIT_THIS|SCRIPTPROC_IMPLICIT_PARENTS, &disp);
1595 if(FAILED(hres))
1596 disp = NULL;
1597 }else {
1598 ERR("GetText failed: %08lx\n", nsres);
1599 disp = NULL;
1601 nsAString_Finish(&nsstr);
1602 if(!disp) {
1603 free(event);
1604 return NULL;
1607 *ret_event = event;
1608 return disp;
1611 void bind_event_scripts(HTMLDocumentNode *doc)
1613 HTMLPluginContainer *plugin_container;
1614 nsIDOMHTMLScriptElement *nsscript;
1615 HTMLScriptElement *script_elem;
1616 EventTarget *event_target;
1617 nsIDOMNodeList *node_list;
1618 nsIDOMNode *script_node;
1619 nsAString selector_str;
1620 IDispatch *event_disp;
1621 UINT32 length, i;
1622 WCHAR *event;
1623 nsresult nsres;
1624 HRESULT hres;
1626 TRACE("%p\n", doc);
1628 if(!doc->dom_document)
1629 return;
1631 nsAString_InitDepend(&selector_str, L"script[event]");
1632 nsres = nsIDOMDocument_QuerySelectorAll(doc->dom_document, &selector_str, &node_list);
1633 nsAString_Finish(&selector_str);
1634 if(NS_FAILED(nsres)) {
1635 ERR("QuerySelectorAll failed: %08lx\n", nsres);
1636 return;
1639 if(!node_list)
1640 return;
1642 nsres = nsIDOMNodeList_GetLength(node_list, &length);
1643 assert(nsres == NS_OK);
1645 for(i=0; i < length; i++) {
1646 nsres = nsIDOMNodeList_Item(node_list, i, &script_node);
1647 if(NS_FAILED(nsres) || !script_node) {
1648 ERR("Item(%d) failed: %08lx\n", i, nsres);
1649 continue;
1652 nsres = nsIDOMNode_QueryInterface(script_node, &IID_nsIDOMHTMLScriptElement, (void**)&nsscript);
1653 assert(nsres == NS_OK);
1654 nsIDOMNode_Release(script_node);
1656 hres = script_elem_from_nsscript(nsscript, &script_elem);
1657 nsIDOMHTMLScriptElement_Release(nsscript);
1658 if(FAILED(hres))
1659 continue;
1661 event_disp = parse_event_elem(doc, script_elem, &event);
1662 if(event_disp) {
1663 event_target = find_event_target(doc, script_elem);
1664 if(event_target) {
1665 hres = IDispatchEx_QueryInterface(&event_target->dispex.IDispatchEx_iface, &IID_HTMLPluginContainer,
1666 (void**)&plugin_container);
1667 if(SUCCEEDED(hres))
1668 bind_activex_event(doc, plugin_container, event, event_disp);
1669 else
1670 bind_target_event(doc, event_target, event, event_disp);
1672 IDispatchEx_Release(&event_target->dispex.IDispatchEx_iface);
1673 if(plugin_container)
1674 node_release(&plugin_container->element.node);
1677 free(event);
1678 IDispatch_Release(event_disp);
1681 IHTMLScriptElement_Release(&script_elem->IHTMLScriptElement_iface);
1684 nsIDOMNodeList_Release(node_list);
1687 BOOL find_global_prop(HTMLInnerWindow *window, BSTR name, DWORD flags, ScriptHost **ret_host, DISPID *ret_id)
1689 IDispatchEx *dispex;
1690 IDispatch *disp;
1691 ScriptHost *iter;
1692 HRESULT hres;
1694 LIST_FOR_EACH_ENTRY(iter, &window->script_hosts, ScriptHost, entry) {
1695 disp = get_script_disp(iter);
1696 if(!disp)
1697 continue;
1699 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1700 if(SUCCEEDED(hres)) {
1701 hres = IDispatchEx_GetDispID(dispex, name, flags & (~fdexNameEnsure), ret_id);
1702 IDispatchEx_Release(dispex);
1703 }else {
1704 FIXME("No IDispatchEx\n");
1705 hres = E_NOTIMPL;
1708 IDispatch_Release(disp);
1709 if(SUCCEEDED(hres)) {
1710 *ret_host = iter;
1711 return TRUE;
1715 return FALSE;
1718 static BOOL is_jscript_available(void)
1720 static BOOL available, checked;
1722 if(!checked) {
1723 IUnknown *unk;
1724 HRESULT hres = CoGetClassObject(&CLSID_JScript, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void**)&unk);
1726 if(SUCCEEDED(hres)) {
1727 available = TRUE;
1728 IUnknown_Release(unk);
1729 }else {
1730 available = FALSE;
1732 checked = TRUE;
1735 return available;
1738 static BOOL use_gecko_script(IUri *uri)
1740 BSTR display_uri;
1741 DWORD zone;
1742 HRESULT hres;
1744 hres = IUri_GetDisplayUri(uri, &display_uri);
1745 if(FAILED(hres))
1746 return FALSE;
1748 hres = IInternetSecurityManager_MapUrlToZone(get_security_manager(), display_uri, &zone, 0);
1749 if(FAILED(hres)) {
1750 WARN("Could not map %s to zone: %08lx\n", debugstr_w(display_uri), hres);
1751 SysFreeString(display_uri);
1752 return TRUE;
1755 SysFreeString(display_uri);
1756 TRACE("zone %ld\n", zone);
1757 return zone == URLZONE_UNTRUSTED;
1760 void update_browser_script_mode(GeckoBrowser *browser, IUri *uri)
1762 nsIWebBrowserSetup *setup;
1763 nsresult nsres;
1765 if(!is_jscript_available()) {
1766 TRACE("jscript.dll not available\n");
1767 browser->script_mode = SCRIPTMODE_GECKO;
1768 return;
1771 browser->script_mode = use_gecko_script(uri) ? SCRIPTMODE_GECKO : SCRIPTMODE_ACTIVESCRIPT;
1773 nsres = nsIWebBrowser_QueryInterface(browser->webbrowser, &IID_nsIWebBrowserSetup, (void**)&setup);
1774 if(NS_SUCCEEDED(nsres)) {
1775 nsres = nsIWebBrowserSetup_SetProperty(setup, SETUP_ALLOW_JAVASCRIPT,
1776 browser->script_mode == SCRIPTMODE_GECKO);
1778 if(NS_SUCCEEDED(nsres))
1779 nsres = nsIWebBrowserSetup_SetProperty(setup, SETUP_DISABLE_NOSCRIPT, TRUE);
1781 nsIWebBrowserSetup_Release(setup);
1784 if(NS_FAILED(nsres))
1785 ERR("JavaScript setup failed: %08lx\n", nsres);
1788 void release_script_hosts(HTMLInnerWindow *window)
1790 script_queue_entry_t *queue_iter;
1791 ScriptHost *iter;
1793 while(!list_empty(&window->script_queue)) {
1794 queue_iter = LIST_ENTRY(list_head(&window->script_queue), script_queue_entry_t, entry);
1796 list_remove(&queue_iter->entry);
1797 IHTMLScriptElement_Release(&queue_iter->script->IHTMLScriptElement_iface);
1798 free(queue_iter);
1801 while(!list_empty(&window->script_hosts)) {
1802 iter = LIST_ENTRY(list_head(&window->script_hosts), ScriptHost, entry);
1804 release_script_engine(iter);
1805 list_remove(&iter->entry);
1806 iter->window = NULL;
1807 IActiveScriptSite_Release(&iter->IActiveScriptSite_iface);