d3d9/tests: Don't fail d3d9ex if the window manager restores focus too soon.
[wine.git] / dlls / mshtml / script.c
blobad44a1ace41a0d120f6e25b08b4a2e5055fa937f
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 unlink_ref(&This->parse_proc);
261 unlink_ref(&This->parse);
264 IActiveScript_Release(This->script);
265 This->script = NULL;
266 This->script_state = SCRIPTSTATE_UNINITIALIZED;
269 void connect_scripts(HTMLInnerWindow *window)
271 ScriptHost *iter;
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);
288 *ppv = NULL;
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;
314 }else {
315 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
316 return E_NOINTERFACE;
319 IUnknown_AddRef((IUnknown*)*ppv);
320 return S_OK;
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);
330 return 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);
340 if(!ref) {
341 release_script_engine(This);
342 if(This->window)
343 list_remove(&This->entry);
344 free(This);
347 return ref;
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();
357 return S_OK;
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);
369 return E_NOTIMPL;
372 *ppiunkItem = NULL;
374 if(wcscmp(pstrName, L"window"))
375 return DISP_E_MEMBERNOTFOUND;
377 if(!This->window)
378 return E_FAIL;
380 /* FIXME: Return proxy object */
381 *ppiunkItem = (IUnknown*)&This->window->base.IHTMLWindow2_iface;
382 IUnknown_AddRef(*ppiunkItem);
384 return S_OK;
387 static HRESULT WINAPI ActiveScriptSite_GetDocVersionString(IActiveScriptSite *iface, BSTR *pbstrVersion)
389 ScriptHost *This = impl_from_IActiveScriptSite(iface);
390 FIXME("(%p)->(%p)\n", This, pbstrVersion);
391 return E_NOTIMPL;
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);
399 return E_NOTIMPL;
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;
409 return S_OK;
412 static HRESULT WINAPI ActiveScriptSite_OnScriptError(IActiveScriptSite *iface, IActiveScriptError *pscripterror)
414 ScriptHost *This = impl_from_IActiveScriptSite(iface);
415 FIXME("(%p)->(%p)\n", This, pscripterror);
416 return E_NOTIMPL;
419 static HRESULT WINAPI ActiveScriptSite_OnEnterScript(IActiveScriptSite *iface)
421 ScriptHost *This = impl_from_IActiveScriptSite(iface);
423 TRACE("(%p)->()\n", This);
425 return S_OK;
428 static HRESULT WINAPI ActiveScriptSite_OnLeaveScript(IActiveScriptSite *iface)
430 ScriptHost *This = impl_from_IActiveScriptSite(iface);
432 TRACE("(%p)->()\n", This);
434 return S_OK;
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);
481 return S_OK;
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)
522 return E_UNEXPECTED;
524 *phwnd = This->window->base.outer_window->browser->doc->hwnd;
525 return S_OK;
528 static HRESULT WINAPI ActiveScriptSiteWindow_EnableModeless(IActiveScriptSiteWindow *iface, BOOL fEnable)
530 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
531 FIXME("(%p)->(%x)\n", This, fEnable);
532 return S_OK;
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;
574 return S_OK;
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,
613 uNumChars, ppsc);
614 return E_NOTIMPL;
617 static HRESULT WINAPI ActiveScriptSiteDebug_GetApplication(IActiveScriptSiteDebug *iface, IDebugApplication **ppda)
619 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
620 FIXME("(%p)->(%p)\n", This, ppda);
621 return E_NOTIMPL;
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);
629 return E_NOTIMPL;
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);
637 return E_NOTIMPL;
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,
685 riid, ppv);
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;
711 ScriptHost *ret;
712 HRESULT hres;
714 ret = calloc(1, sizeof(*ret));
715 if(!ret)
716 return NULL;
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;
724 ret->ref = 1;
725 ret->window = window;
726 ret->script_state = SCRIPTSTATE_UNINITIALIZED;
728 ret->guid = *guid;
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);
733 if(FAILED(hres))
734 WARN("Could not load script engine: %08lx\n", hres);
735 else {
736 BOOL succeeded = init_script_engine(ret, script);
737 ret->script = script;
738 if(!succeeded)
739 release_script_engine(ret);
742 return ret;
745 static void dispatch_script_readystatechange_event(HTMLScriptElement *script)
747 DOMEvent *event;
748 HRESULT hres;
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);
768 typedef struct {
769 event_task_t header;
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)
778 return;
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)
797 return;
799 if(readystate != READYSTATE_INTERACTIVE) {
800 if(!script_elem->element.node.doc->window->parser_callback_cnt) {
801 fire_readystatechange_task_t *task;
802 HRESULT hres;
804 if(script_elem->pending_readystatechange_event)
805 return;
807 task = malloc(sizeof(*task));
808 if(!task)
809 return;
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);
816 if(SUCCEEDED(hres))
817 script_elem->pending_readystatechange_event = TRUE;
818 }else {
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)
827 EXCEPINFO excepinfo;
828 VARIANT var;
829 HRESULT hres;
831 TRACE("%s\n", debugstr_w(text));
833 VariantInit(&var);
834 memset(&excepinfo, 0, sizeof(excepinfo));
835 TRACE(">>>\n");
836 hres = IActiveScriptParse_ParseScriptText(script_host->parse, text, L"window", NULL, L"</SCRIPT>",
837 0, 0, SCRIPTTEXT_ISVISIBLE|SCRIPTTEXT_HOSTMANAGESSOURCE,
838 &var, &excepinfo);
839 if(SUCCEEDED(hres))
840 TRACE("<<<\n");
841 else
842 WARN("<<< %08lx\n", hres);
845 typedef struct {
846 BSCallback bsc;
848 HTMLScriptElement *script_elem;
849 nsILoadGroup *load_group;
850 nsIRequest *request;
851 DWORD scheme;
853 DWORD size;
854 char *buf;
855 } ScriptBSC;
857 static HRESULT get_binding_text(ScriptBSC *bsc, WCHAR **ret)
859 UINT cp = CP_UTF8;
860 WCHAR *text;
862 if(!bsc->bsc.read) {
863 text = malloc(sizeof(WCHAR));
864 if(!text)
865 return E_OUTOFMEMORY;
866 *text = 0;
867 *ret = text;
868 return S_OK;
871 switch(bsc->bsc.bom) {
872 case BOM_UTF16:
873 if(bsc->bsc.read % sizeof(WCHAR)) {
874 FIXME("The buffer is not a valid utf16 string\n");
875 return E_FAIL;
878 text = malloc(bsc->bsc.read + sizeof(WCHAR));
879 if(!text)
880 return E_OUTOFMEMORY;
882 memcpy(text, bsc->buf, bsc->bsc.read);
883 text[bsc->bsc.read/sizeof(WCHAR)] = 0;
884 break;
886 default:
887 /* FIXME: Try to use charset from HTTP headers first */
888 cp = get_document_charset(bsc->script_elem->element.node.doc);
889 /* fall through */
890 case BOM_UTF8: {
891 DWORD len;
893 len = MultiByteToWideChar(cp, 0, bsc->buf, bsc->bsc.read, NULL, 0);
894 text = malloc((len + 1) * sizeof(WCHAR));
895 if(!text)
896 return E_OUTOFMEMORY;
898 MultiByteToWideChar(cp, 0, bsc->buf, bsc->bsc.read, text, len);
899 text[len] = 0;
903 *ret = text;
904 return S_OK;
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;
912 nsIDOMNode *parent;
913 nsresult nsres;
914 HRESULT hres;
916 assert(window != NULL);
918 hres = get_binding_text(bsc, &script_elem->src_text);
919 if(FAILED(hres))
920 return;
922 script_host = get_elem_script_host(window, script_elem);
923 if(!script_host)
924 return;
926 if(window->parser_callback_cnt) {
927 script_queue_entry_t *queue;
929 TRACE("Adding to queue\n");
931 queue = malloc(sizeof(*queue));
932 if(!queue)
933 return;
935 IHTMLScriptElement_AddRef(&script_elem->IHTMLScriptElement_iface);
936 queue->script = script_elem;
938 list_add_tail(&window->script_queue, &queue->entry);
939 return;
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;
946 return;
949 nsIDOMNode_Release(parent);
951 script_host = get_elem_script_host(window, script_elem);
952 if(!script_host)
953 return;
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;
974 if(This->request) {
975 ERR("Unfinished request\n");
976 nsIRequest_Release(This->request);
978 if(This->load_group)
979 nsILoadGroup_Release(This->load_group);
981 free(This->buf);
982 free(This);
985 static HRESULT ScriptBSC_init_bindinfo(BSCallback *bsc)
987 return S_OK;
990 static HRESULT ScriptBSC_start_binding(BSCallback *bsc)
992 ScriptBSC *This = impl_from_BSCallback(bsc);
993 nsresult nsres;
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);
1010 return S_OK;
1013 static HRESULT ScriptBSC_stop_binding(BSCallback *bsc, HRESULT result)
1015 ScriptBSC *This = impl_from_BSCallback(bsc);
1016 nsresult nsres;
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);
1029 }else {
1030 FIXME("binding failed %08lx\n", result);
1031 free(This->buf);
1032 This->buf = NULL;
1033 This->size = 0;
1036 if(This->request) {
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;
1046 return S_OK;
1049 static HRESULT ScriptBSC_read_data(BSCallback *bsc, IStream *stream)
1051 ScriptBSC *This = impl_from_BSCallback(bsc);
1052 DWORD read;
1053 HRESULT hres;
1055 if(!This->buf) {
1056 This->buf = malloc(128);
1057 if(!This->buf)
1058 return E_OUTOFMEMORY;
1059 This->size = 128;
1062 do {
1063 if(This->bsc.read >= This->size) {
1064 void *new_buf;
1065 new_buf = realloc(This->buf, This->size << 1);
1066 if(!new_buf)
1067 return E_OUTOFMEMORY;
1068 This->size <<= 1;
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);
1075 return S_OK;
1078 static HRESULT ScriptBSC_on_progress(BSCallback *bsc, ULONG progress, ULONG total, ULONG status_code, LPCWSTR status_text)
1080 return S_OK;
1083 static HRESULT ScriptBSC_on_response(BSCallback *bsc, DWORD response_code,
1084 LPCWSTR response_headers)
1086 return S_OK;
1089 static HRESULT ScriptBSC_beginning_transaction(BSCallback *bsc, WCHAR **additional_headers)
1091 return S_FALSE;
1094 static const BSCallbackVtbl ScriptBSCVtbl = {
1095 ScriptBSC_destroy,
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;
1109 ScriptBSC *bsc;
1110 IMoniker *mon;
1111 IUri *uri;
1112 HRESULT hres;
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)) {
1122 ERR("no window\n");
1123 return E_UNEXPECTED;
1126 hres = create_uri(src, 0, &uri);
1127 if(FAILED(hres))
1128 return hres;
1130 hres = CreateURLMonikerEx2(NULL, uri, &mon, URL_MK_UNIFORM);
1131 if(FAILED(hres)) {
1132 IUri_Release(uri);
1133 return hres;
1136 bsc = calloc(1, sizeof(*bsc));
1137 if(!bsc) {
1138 IMoniker_Release(mon);
1139 IUri_Release(uri);
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);
1147 IUri_Release(uri);
1148 if(hres != S_OK)
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) {
1156 cpp_bool contains;
1157 nsresult nsres;
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);
1171 return hres;
1174 static void parse_inline_script(ScriptHost *script_host, HTMLScriptElement *script_elem)
1176 const PRUnichar *text;
1177 nsAString text_str;
1178 nsresult nsres;
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);
1188 }else if(*text) {
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;
1200 nsresult nsres;
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);
1208 if(*event) {
1209 TRACE("deferring event %s script evaluation\n", debugstr_w(event));
1210 nsAString_Finish(&event_str);
1211 return FALSE;
1213 }else {
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);
1224 }else if(*src) {
1225 load_script(script_elem, src, FALSE);
1226 is_complete = script_elem->parsed;
1227 }else {
1228 parse_inline_script(script_host, script_elem);
1229 is_complete = TRUE;
1232 nsAString_Finish(&src_str);
1234 return is_complete;
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)
1241 ? CLSID_JScript
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;
1252 }else {
1253 FIXME("Unknown type %s\n", debugstr_w(type));
1254 return FALSE;
1257 return TRUE;
1260 static BOOL get_guid_from_language(LPCWSTR type, GUID *guid)
1262 HRESULT hres;
1264 hres = CLSIDFromProgID(type, guid);
1265 if(FAILED(hres))
1266 return FALSE;
1268 /* FIXME: Check CATID_ActiveScriptParse */
1270 return TRUE;
1273 static BOOL get_script_guid(HTMLInnerWindow *window, nsIDOMHTMLScriptElement *nsscript, GUID *guid)
1275 nsIDOMElement *nselem;
1276 const PRUnichar *language;
1277 nsAString val_str;
1278 BOOL ret = FALSE;
1279 nsresult nsres;
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);
1288 if(*type) {
1289 ret = get_guid_from_type(type, guid);
1290 nsAString_Finish(&val_str);
1291 return ret;
1293 }else {
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)) {
1303 if(*language) {
1304 ret = get_guid_from_language(language, guid);
1305 }else {
1306 *guid = get_default_script_guid(window);
1307 ret = TRUE;
1309 nsAString_Finish(&val_str);
1312 return ret;
1315 static ScriptHost *get_script_host(HTMLInnerWindow *window, const GUID *guid)
1317 ScriptHost *iter;
1319 LIST_FOR_EACH_ENTRY(iter, &window->script_hosts, ScriptHost, entry) {
1320 if(IsEqualGUID(guid, &iter->guid))
1321 return iter;
1324 return create_script_host(window, guid);
1327 static ScriptHost *get_elem_script_host(HTMLInnerWindow *window, HTMLScriptElement *script_elem)
1329 GUID guid;
1331 if(!get_script_guid(window, script_elem->nsscript, &guid)) {
1332 WARN("Could not find script GUID\n");
1333 return NULL;
1336 if(IsEqualGUID(&CLSID_JScript, &guid) && (!window->doc->browser || window->doc->browser->script_mode != SCRIPTMODE_ACTIVESCRIPT)) {
1337 TRACE("Ignoring JScript\n");
1338 return NULL;
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);
1350 if(!script_host)
1351 return;
1353 if(script_host->parse) {
1354 if(script_elem->src_text) {
1355 if(from_parser)
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);
1359 is_complete = TRUE;
1360 }else if(!script_elem->binding) {
1361 is_complete = parse_script_elem(script_host, script_elem);
1365 if(is_complete)
1366 set_script_elem_readystate(script_elem, READYSTATE_COMPLETE);
1369 IDispatch *script_parse_event(HTMLInnerWindow *window, LPCWSTR text)
1371 ScriptHost *script_host;
1372 GUID guid;
1373 const WCHAR *ptr;
1374 IDispatch *disp;
1375 HRESULT hres;
1377 TRACE("%s\n", debugstr_w(text));
1379 for(ptr = text; iswalnum(*ptr); ptr++);
1380 if(*ptr == ':') {
1381 LPWSTR language;
1382 BOOL b;
1384 language = malloc((ptr - text + 1) * sizeof(WCHAR));
1385 if(!language)
1386 return NULL;
1388 memcpy(language, text, (ptr-text)*sizeof(WCHAR));
1389 language[ptr-text] = 0;
1391 b = get_guid_from_language(language, &guid);
1393 free(language);
1395 if(!b) {
1396 WARN("Could not find language\n");
1397 return NULL;
1400 ptr++;
1401 }else {
1402 ptr = text;
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");
1409 return NULL;
1412 script_host = get_script_host(window, &guid);
1413 if(!script_host || !script_host->parse_proc)
1414 return NULL;
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);
1419 if(FAILED(hres)) {
1420 WARN("ParseProcedureText failed: %08lx\n", hres);
1421 return NULL;
1424 TRACE("ret %p\n", disp);
1425 return disp;
1428 HRESULT exec_script(HTMLInnerWindow *window, const WCHAR *code, const WCHAR *lang, VARIANT *ret)
1430 ScriptHost *script_host;
1431 EXCEPINFO ei;
1432 GUID guid;
1433 HRESULT hres;
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);
1441 if(!script_host) {
1442 FIXME("No script host\n");
1443 return E_FAIL;
1446 if(!script_host->parse) {
1447 FIXME("script_host->parse == NULL\n");
1448 return E_FAIL;
1451 memset(&ei, 0, sizeof(ei));
1452 TRACE(">>>\n");
1453 hres = IActiveScriptParse_ParseScriptText(script_host->parse, code, NULL, NULL, L"\"", 0, 0, SCRIPTTEXT_ISVISIBLE, ret, &ei);
1454 if(SUCCEEDED(hres))
1455 TRACE("<<<\n");
1456 else
1457 WARN("<<< %08lx\n", hres);
1459 return hres;
1462 IDispatch *get_script_disp(ScriptHost *script_host)
1464 IDispatch *disp;
1465 HRESULT hres;
1467 if(!script_host->script)
1468 return NULL;
1470 hres = IActiveScript_GetScriptDispatch(script_host->script, L"window", &disp);
1471 if(FAILED(hres))
1472 return NULL;
1474 return 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;
1482 nsresult nsres;
1483 HRESULT hres;
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);
1490 return NULL;
1493 nsAString_GetData(&target_id_str, &target_id);
1494 if(!*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")) {
1500 if(doc->window) {
1501 event_target = &doc->window->event_target;
1502 IDispatchEx_AddRef(&event_target->dispex.IDispatchEx_iface);
1504 }else {
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)
1519 WCHAR *ptr;
1521 TRACE("%s\n", debugstr_w(event));
1523 for(ptr = event; iswalnum(*ptr); ptr++);
1524 if(!*ptr) {
1525 *args = NULL;
1526 return TRUE;
1529 if(*ptr != '(')
1530 return FALSE;
1532 *ptr++ = 0;
1533 *args = ptr;
1534 while(iswalnum(*ptr) || iswspace(*ptr) || *ptr == ',')
1535 ptr++;
1537 if(*ptr != ')')
1538 return FALSE;
1540 *ptr++ = 0;
1541 return !*ptr;
1544 static IDispatch *parse_event_elem(HTMLDocumentNode *doc, HTMLScriptElement *script_elem, WCHAR **ret_event)
1546 ScriptHost *script_host;
1547 WCHAR *event = NULL;
1548 const WCHAR *args;
1549 nsAString nsstr;
1550 IDispatch *disp;
1551 nsresult nsres;
1552 HRESULT hres;
1554 if(script_elem->parsed)
1555 return NULL;
1557 script_host = get_elem_script_host(doc->window, script_elem);
1558 if(!script_host || !script_host->parse_proc)
1559 return NULL;
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);
1570 if(!event)
1571 return NULL;
1573 if(!parse_event_str(event, &args)) {
1574 WARN("parsing %s failed\n", debugstr_w(event));
1575 free(event);
1576 return NULL;
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);
1588 if(FAILED(hres))
1589 disp = NULL;
1590 }else {
1591 ERR("GetText failed: %08lx\n", nsres);
1592 disp = NULL;
1594 nsAString_Finish(&nsstr);
1595 if(!disp) {
1596 free(event);
1597 return NULL;
1600 *ret_event = event;
1601 return disp;
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;
1614 UINT32 length, i;
1615 WCHAR *event;
1616 nsresult nsres;
1617 HRESULT hres;
1619 TRACE("%p\n", doc);
1621 if(!doc->dom_document)
1622 return;
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);
1629 return;
1632 if(!node_list)
1633 return;
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);
1642 continue;
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);
1651 if(FAILED(hres))
1652 continue;
1654 event_disp = parse_event_elem(doc, script_elem, &event);
1655 if(event_disp) {
1656 event_target = find_event_target(doc, script_elem);
1657 if(event_target) {
1658 hres = IDispatchEx_QueryInterface(&event_target->dispex.IDispatchEx_iface, &IID_HTMLPluginContainer,
1659 (void**)&plugin_container);
1660 if(SUCCEEDED(hres))
1661 bind_activex_event(doc, plugin_container, event, event_disp);
1662 else
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);
1670 free(event);
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;
1683 IDispatch *disp;
1684 ScriptHost *iter;
1685 HRESULT hres;
1687 LIST_FOR_EACH_ENTRY(iter, &window->script_hosts, ScriptHost, entry) {
1688 disp = get_script_disp(iter);
1689 if(!disp)
1690 continue;
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);
1696 }else {
1697 FIXME("No IDispatchEx\n");
1698 hres = E_NOTIMPL;
1701 IDispatch_Release(disp);
1702 if(SUCCEEDED(hres)) {
1703 *ret_host = iter;
1704 return TRUE;
1708 return FALSE;
1711 static BOOL is_jscript_available(void)
1713 static BOOL available, checked;
1715 if(!checked) {
1716 IUnknown *unk;
1717 HRESULT hres = CoGetClassObject(&CLSID_JScript, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void**)&unk);
1719 if(SUCCEEDED(hres)) {
1720 available = TRUE;
1721 IUnknown_Release(unk);
1722 }else {
1723 available = FALSE;
1725 checked = TRUE;
1728 return available;
1731 static BOOL use_gecko_script(IUri *uri)
1733 BSTR display_uri;
1734 DWORD zone;
1735 HRESULT hres;
1737 hres = IUri_GetDisplayUri(uri, &display_uri);
1738 if(FAILED(hres))
1739 return FALSE;
1741 hres = IInternetSecurityManager_MapUrlToZone(get_security_manager(), display_uri, &zone, 0);
1742 if(FAILED(hres)) {
1743 WARN("Could not map %s to zone: %08lx\n", debugstr_w(display_uri), hres);
1744 SysFreeString(display_uri);
1745 return TRUE;
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;
1756 nsresult nsres;
1758 if(!is_jscript_available()) {
1759 TRACE("jscript.dll not available\n");
1760 browser->script_mode = SCRIPTMODE_GECKO;
1761 return;
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;
1784 ScriptHost *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);
1791 free(queue_iter);
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);