wined3d: Pass a wined3d_device_context to wined3d_cs_emit_blt_sub_resource().
[wine/zf.git] / dlls / mshtml / script.c
blobec739c4777fdfe2d2a4c8e6fde5088ee169f397a
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>
20 #include <assert.h>
22 #define COBJMACROS
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winuser.h"
27 #include "ole2.h"
28 #include "activscp.h"
29 #include "activdbg.h"
30 #include "shlwapi.h"
32 #include "wine/debug.h"
34 #include "mshtml_private.h"
35 #include "htmlscript.h"
36 #include "pluginhost.h"
37 #include "htmlevent.h"
38 #include "binding.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
42 #ifdef _WIN64
44 #define CTXARG_T DWORDLONG
45 #define IActiveScriptSiteDebugVtbl IActiveScriptSiteDebug64Vtbl
47 #define IActiveScriptParse_Release IActiveScriptParse64_Release
48 #define IActiveScriptParse_InitNew IActiveScriptParse64_InitNew
49 #define IActiveScriptParse_ParseScriptText IActiveScriptParse64_ParseScriptText
50 #define IActiveScriptParseProcedure2_Release IActiveScriptParseProcedure2_64_Release
51 #define IActiveScriptParseProcedure2_ParseProcedureText IActiveScriptParseProcedure2_64_ParseProcedureText
53 #else
55 #define CTXARG_T DWORD
56 #define IActiveScriptSiteDebugVtbl IActiveScriptSiteDebug32Vtbl
58 #define IActiveScriptParse_Release IActiveScriptParse32_Release
59 #define IActiveScriptParse_InitNew IActiveScriptParse32_InitNew
60 #define IActiveScriptParse_ParseScriptText IActiveScriptParse32_ParseScriptText
61 #define IActiveScriptParseProcedure2_Release IActiveScriptParseProcedure2_32_Release
62 #define IActiveScriptParseProcedure2_ParseProcedureText IActiveScriptParseProcedure2_32_ParseProcedureText
64 #endif
66 /* See jscript.h in jscript.dll. */
67 #define SCRIPTLANGUAGEVERSION_HTML 0x400
68 #define SCRIPTLANGUAGEVERSION_ES5 0x102
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(ScriptHost *script_host, DWORD property, VARIANT *val)
96 IActiveScriptProperty *script_prop;
97 HRESULT hres;
99 hres = IActiveScript_QueryInterface(script_host->script, &IID_IActiveScriptProperty,
100 (void**)&script_prop);
101 if(FAILED(hres)) {
102 WARN("Could not get IActiveScriptProperty iface: %08x\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(%x) failed: %08x\n", property, hres);
110 return FALSE;
113 return TRUE;
116 static BOOL init_script_engine(ScriptHost *script_host)
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_host->script, &IID_IActiveScriptParse, (void**)&script_host->parse);
126 if(FAILED(hres)) {
127 WARN("Could not get IActiveScriptParse: %08x\n", hres);
128 return FALSE;
131 hres = IActiveScript_QueryInterface(script_host->script, &IID_IObjectSafety, (void**)&safety);
132 if(FAILED(hres)) {
133 FIXME("Could not get IObjectSafety: %08x\n", hres);
134 return FALSE;
137 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported_opts, &enabled_opts);
138 if(FAILED(hres)) {
139 FIXME("GetInterfaceSafetyOptions failed: %08x\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: %08x\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_IE9)
158 script_mode = SCRIPTLANGUAGEVERSION_ES5;
159 script_mode |= SCRIPTLANGUAGEVERSION_HTML;
161 V_VT(&var) = VT_I4;
162 V_I4(&var) = script_mode;
163 if(!set_script_prop(script_host, SCRIPTPROP_INVOKEVERSIONING, &var) && (script_mode & SCRIPTLANGUAGEVERSION_HTML)) {
164 /* If this failed, we're most likely using native jscript. */
165 WARN("Failed to set script mode to HTML version.\n");
166 V_I4(&var) = compat_mode < COMPAT_MODE_IE8 ? SCRIPTLANGUAGEVERSION_5_7 : SCRIPTLANGUAGEVERSION_5_8;
167 set_script_prop(script_host, SCRIPTPROP_INVOKEVERSIONING, &var);
170 V_VT(&var) = VT_BOOL;
171 V_BOOL(&var) = VARIANT_TRUE;
172 set_script_prop(script_host, SCRIPTPROP_HACK_TRIDENTEVENTSINK, &var);
174 hres = IActiveScriptParse_InitNew(script_host->parse);
175 if(FAILED(hres)) {
176 WARN("InitNew failed: %08x\n", hres);
177 return FALSE;
180 hres = IActiveScript_SetScriptSite(script_host->script, &script_host->IActiveScriptSite_iface);
181 if(FAILED(hres)) {
182 WARN("SetScriptSite failed: %08x\n", hres);
183 IActiveScript_Close(script_host->script);
184 return FALSE;
187 hres = IActiveScript_GetScriptState(script_host->script, &state);
188 if(FAILED(hres))
189 WARN("GetScriptState failed: %08x\n", hres);
190 else if(state != SCRIPTSTATE_INITIALIZED)
191 FIXME("state = %x\n", state);
193 hres = IActiveScript_SetScriptState(script_host->script, SCRIPTSTATE_STARTED);
194 if(FAILED(hres)) {
195 WARN("Starting script failed: %08x\n", hres);
196 return FALSE;
199 hres = IActiveScript_AddNamedItem(script_host->script, L"window",
200 SCRIPTITEM_ISVISIBLE|SCRIPTITEM_ISSOURCE|SCRIPTITEM_GLOBALMEMBERS);
201 if(SUCCEEDED(hres)) {
202 V_VT(&var) = VT_BOOL;
203 V_BOOL(&var) = VARIANT_TRUE;
204 set_script_prop(script_host, SCRIPTPROP_ABBREVIATE_GLOBALNAME_RESOLUTION, &var);
205 }else {
206 WARN("AddNamedItem failed: %08x\n", hres);
209 hres = IActiveScript_QueryInterface(script_host->script, &IID_IActiveScriptParseProcedure2,
210 (void**)&script_host->parse_proc);
211 if(FAILED(hres)) {
212 /* FIXME: QI for IActiveScriptParseProcedure */
213 WARN("Could not get IActiveScriptParseProcedure iface: %08x\n", hres);
216 return TRUE;
219 static void release_script_engine(ScriptHost *This)
221 if(!This->script)
222 return;
224 switch(This->script_state) {
225 case SCRIPTSTATE_CONNECTED:
226 IActiveScript_SetScriptState(This->script, SCRIPTSTATE_DISCONNECTED);
228 case SCRIPTSTATE_STARTED:
229 case SCRIPTSTATE_DISCONNECTED:
230 case SCRIPTSTATE_INITIALIZED:
231 IActiveScript_Close(This->script);
233 default:
234 if(This->parse_proc) {
235 IActiveScriptParseProcedure2_Release(This->parse_proc);
236 This->parse_proc = NULL;
239 if(This->parse) {
240 IActiveScriptParse_Release(This->parse);
241 This->parse = NULL;
245 IActiveScript_Release(This->script);
246 This->script = NULL;
247 This->script_state = SCRIPTSTATE_UNINITIALIZED;
250 void connect_scripts(HTMLInnerWindow *window)
252 ScriptHost *iter;
254 LIST_FOR_EACH_ENTRY(iter, &window->script_hosts, ScriptHost, entry) {
255 if(iter->script_state == SCRIPTSTATE_STARTED)
256 IActiveScript_SetScriptState(iter->script, SCRIPTSTATE_CONNECTED);
260 static inline ScriptHost *impl_from_IActiveScriptSite(IActiveScriptSite *iface)
262 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSite_iface);
265 static HRESULT WINAPI ActiveScriptSite_QueryInterface(IActiveScriptSite *iface, REFIID riid, void **ppv)
267 ScriptHost *This = impl_from_IActiveScriptSite(iface);
269 *ppv = NULL;
271 if(IsEqualGUID(&IID_IUnknown, riid)) {
272 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
273 *ppv = &This->IActiveScriptSite_iface;
274 }else if(IsEqualGUID(&IID_IActiveScriptSite, riid)) {
275 TRACE("(%p)->(IID_IActiveScriptSite %p)\n", This, ppv);
276 *ppv = &This->IActiveScriptSite_iface;
277 }else if(IsEqualGUID(&IID_IActiveScriptSiteInterruptPoll, riid)) {
278 TRACE("(%p)->(IID_IActiveScriptSiteInterruprtPoll %p)\n", This, ppv);
279 *ppv = &This->IActiveScriptSiteInterruptPoll_iface;
280 }else if(IsEqualGUID(&IID_IActiveScriptSiteWindow, riid)) {
281 TRACE("(%p)->(IID_IActiveScriptSiteWindow %p)\n", This, ppv);
282 *ppv = &This->IActiveScriptSiteWindow_iface;
283 }else if(IsEqualGUID(&IID_IActiveScriptSiteUIControl, riid)) {
284 TRACE("(%p)->(IID_IActiveScriptSiteUIControl %p)\n", This, ppv);
285 *ppv = &This->IActiveScriptSiteUIControl_iface;
286 }else if(IsEqualGUID(&IID_IActiveScriptSiteDebug, riid)) {
287 TRACE("(%p)->(IID_IActiveScriptSiteDebug %p)\n", This, ppv);
288 *ppv = &This->IActiveScriptSiteDebug_iface;
289 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
290 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
291 *ppv = &This->IServiceProvider_iface;
292 }else if(IsEqualGUID(&IID_ICanHandleException, riid)) {
293 TRACE("(%p)->(IID_ICanHandleException not supported %p)\n", This, ppv);
294 return E_NOINTERFACE;
295 }else {
296 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
297 return E_NOINTERFACE;
300 IUnknown_AddRef((IUnknown*)*ppv);
301 return S_OK;
304 static ULONG WINAPI ActiveScriptSite_AddRef(IActiveScriptSite *iface)
306 ScriptHost *This = impl_from_IActiveScriptSite(iface);
307 LONG ref = InterlockedIncrement(&This->ref);
309 TRACE("(%p) ref=%d\n", This, ref);
311 return ref;
314 static ULONG WINAPI ActiveScriptSite_Release(IActiveScriptSite *iface)
316 ScriptHost *This = impl_from_IActiveScriptSite(iface);
317 LONG ref = InterlockedDecrement(&This->ref);
319 TRACE("(%p) ref=%d\n", This, ref);
321 if(!ref) {
322 release_script_engine(This);
323 if(This->window)
324 list_remove(&This->entry);
325 heap_free(This);
328 return ref;
331 static HRESULT WINAPI ActiveScriptSite_GetLCID(IActiveScriptSite *iface, LCID *plcid)
333 ScriptHost *This = impl_from_IActiveScriptSite(iface);
335 TRACE("(%p)->(%p)\n", This, plcid);
337 *plcid = GetUserDefaultLCID();
338 return S_OK;
341 static HRESULT WINAPI ActiveScriptSite_GetItemInfo(IActiveScriptSite *iface, LPCOLESTR pstrName,
342 DWORD dwReturnMask, IUnknown **ppiunkItem, ITypeInfo **ppti)
344 ScriptHost *This = impl_from_IActiveScriptSite(iface);
346 TRACE("(%p)->(%s %x %p %p)\n", This, debugstr_w(pstrName), dwReturnMask, ppiunkItem, ppti);
348 if(dwReturnMask != SCRIPTINFO_IUNKNOWN) {
349 FIXME("Unsupported mask %x\n", dwReturnMask);
350 return E_NOTIMPL;
353 *ppiunkItem = NULL;
355 if(wcscmp(pstrName, L"window"))
356 return DISP_E_MEMBERNOTFOUND;
358 if(!This->window)
359 return E_FAIL;
361 /* FIXME: Return proxy object */
362 *ppiunkItem = (IUnknown*)&This->window->base.IHTMLWindow2_iface;
363 IUnknown_AddRef(*ppiunkItem);
365 return S_OK;
368 static HRESULT WINAPI ActiveScriptSite_GetDocVersionString(IActiveScriptSite *iface, BSTR *pbstrVersion)
370 ScriptHost *This = impl_from_IActiveScriptSite(iface);
371 FIXME("(%p)->(%p)\n", This, pbstrVersion);
372 return E_NOTIMPL;
375 static HRESULT WINAPI ActiveScriptSite_OnScriptTerminate(IActiveScriptSite *iface,
376 const VARIANT *pvarResult, const EXCEPINFO *pexcepinfo)
378 ScriptHost *This = impl_from_IActiveScriptSite(iface);
379 FIXME("(%p)->(%p %p)\n", This, pvarResult, pexcepinfo);
380 return E_NOTIMPL;
383 static HRESULT WINAPI ActiveScriptSite_OnStateChange(IActiveScriptSite *iface, SCRIPTSTATE ssScriptState)
385 ScriptHost *This = impl_from_IActiveScriptSite(iface);
387 TRACE("(%p)->(%x)\n", This, ssScriptState);
389 This->script_state = ssScriptState;
390 return S_OK;
393 static HRESULT WINAPI ActiveScriptSite_OnScriptError(IActiveScriptSite *iface, IActiveScriptError *pscripterror)
395 ScriptHost *This = impl_from_IActiveScriptSite(iface);
396 FIXME("(%p)->(%p)\n", This, pscripterror);
397 return E_NOTIMPL;
400 static HRESULT WINAPI ActiveScriptSite_OnEnterScript(IActiveScriptSite *iface)
402 ScriptHost *This = impl_from_IActiveScriptSite(iface);
404 TRACE("(%p)->()\n", This);
406 return S_OK;
409 static HRESULT WINAPI ActiveScriptSite_OnLeaveScript(IActiveScriptSite *iface)
411 ScriptHost *This = impl_from_IActiveScriptSite(iface);
413 TRACE("(%p)->()\n", This);
415 return S_OK;
418 static const IActiveScriptSiteVtbl ActiveScriptSiteVtbl = {
419 ActiveScriptSite_QueryInterface,
420 ActiveScriptSite_AddRef,
421 ActiveScriptSite_Release,
422 ActiveScriptSite_GetLCID,
423 ActiveScriptSite_GetItemInfo,
424 ActiveScriptSite_GetDocVersionString,
425 ActiveScriptSite_OnScriptTerminate,
426 ActiveScriptSite_OnStateChange,
427 ActiveScriptSite_OnScriptError,
428 ActiveScriptSite_OnEnterScript,
429 ActiveScriptSite_OnLeaveScript
432 static inline ScriptHost *impl_from_IActiveScriptSiteInterruptPoll(IActiveScriptSiteInterruptPoll *iface)
434 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSiteInterruptPoll_iface);
437 static HRESULT WINAPI ActiveScriptSiteInterruptPoll_QueryInterface(IActiveScriptSiteInterruptPoll *iface,
438 REFIID riid, void **ppv)
440 ScriptHost *This = impl_from_IActiveScriptSiteInterruptPoll(iface);
441 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
444 static ULONG WINAPI ActiveScriptSiteInterruptPoll_AddRef(IActiveScriptSiteInterruptPoll *iface)
446 ScriptHost *This = impl_from_IActiveScriptSiteInterruptPoll(iface);
447 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
450 static ULONG WINAPI ActiveScriptSiteInterruptPoll_Release(IActiveScriptSiteInterruptPoll *iface)
452 ScriptHost *This = impl_from_IActiveScriptSiteInterruptPoll(iface);
453 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
456 static HRESULT WINAPI ActiveScriptSiteInterruptPoll_QueryContinue(IActiveScriptSiteInterruptPoll *iface)
458 ScriptHost *This = impl_from_IActiveScriptSiteInterruptPoll(iface);
460 TRACE("(%p)\n", This);
462 return S_OK;
465 static const IActiveScriptSiteInterruptPollVtbl ActiveScriptSiteInterruptPollVtbl = {
466 ActiveScriptSiteInterruptPoll_QueryInterface,
467 ActiveScriptSiteInterruptPoll_AddRef,
468 ActiveScriptSiteInterruptPoll_Release,
469 ActiveScriptSiteInterruptPoll_QueryContinue
472 static inline ScriptHost *impl_from_IActiveScriptSiteWindow(IActiveScriptSiteWindow *iface)
474 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSiteWindow_iface);
477 static HRESULT WINAPI ActiveScriptSiteWindow_QueryInterface(IActiveScriptSiteWindow *iface,
478 REFIID riid, void **ppv)
480 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
481 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
484 static ULONG WINAPI ActiveScriptSiteWindow_AddRef(IActiveScriptSiteWindow *iface)
486 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
487 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
490 static ULONG WINAPI ActiveScriptSiteWindow_Release(IActiveScriptSiteWindow *iface)
492 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
493 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
496 static HRESULT WINAPI ActiveScriptSiteWindow_GetWindow(IActiveScriptSiteWindow *iface, HWND *phwnd)
498 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
500 TRACE("(%p)->(%p)\n", This, phwnd);
502 if(!This->window || !This->window->base.outer_window)
503 return E_UNEXPECTED;
505 *phwnd = This->window->base.outer_window->browser->doc->hwnd;
506 return S_OK;
509 static HRESULT WINAPI ActiveScriptSiteWindow_EnableModeless(IActiveScriptSiteWindow *iface, BOOL fEnable)
511 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
512 FIXME("(%p)->(%x)\n", This, fEnable);
513 return S_OK;
516 static const IActiveScriptSiteWindowVtbl ActiveScriptSiteWindowVtbl = {
517 ActiveScriptSiteWindow_QueryInterface,
518 ActiveScriptSiteWindow_AddRef,
519 ActiveScriptSiteWindow_Release,
520 ActiveScriptSiteWindow_GetWindow,
521 ActiveScriptSiteWindow_EnableModeless
524 static inline ScriptHost *impl_from_IActiveScriptSiteUIControl(IActiveScriptSiteUIControl *iface)
526 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSiteUIControl_iface);
529 static HRESULT WINAPI ActiveScriptSiteUIControl_QueryInterface(IActiveScriptSiteUIControl *iface, REFIID riid, void **ppv)
531 ScriptHost *This = impl_from_IActiveScriptSiteUIControl(iface);
532 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
535 static ULONG WINAPI ActiveScriptSiteUIControl_AddRef(IActiveScriptSiteUIControl *iface)
537 ScriptHost *This = impl_from_IActiveScriptSiteUIControl(iface);
538 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
541 static ULONG WINAPI ActiveScriptSiteUIControl_Release(IActiveScriptSiteUIControl *iface)
543 ScriptHost *This = impl_from_IActiveScriptSiteUIControl(iface);
544 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
547 static HRESULT WINAPI ActiveScriptSiteUIControl_GetUIBehavior(IActiveScriptSiteUIControl *iface, SCRIPTUICITEM UicItem,
548 SCRIPTUICHANDLING *pUicHandling)
550 ScriptHost *This = impl_from_IActiveScriptSiteUIControl(iface);
552 WARN("(%p)->(%d %p) semi-stub\n", This, UicItem, pUicHandling);
554 *pUicHandling = SCRIPTUICHANDLING_ALLOW;
555 return S_OK;
558 static const IActiveScriptSiteUIControlVtbl ActiveScriptSiteUIControlVtbl = {
559 ActiveScriptSiteUIControl_QueryInterface,
560 ActiveScriptSiteUIControl_AddRef,
561 ActiveScriptSiteUIControl_Release,
562 ActiveScriptSiteUIControl_GetUIBehavior
565 static inline ScriptHost *impl_from_IActiveScriptSiteDebug(IActiveScriptSiteDebug *iface)
567 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSiteDebug_iface);
570 static HRESULT WINAPI ActiveScriptSiteDebug_QueryInterface(IActiveScriptSiteDebug *iface,
571 REFIID riid, void **ppv)
573 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
574 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
577 static ULONG WINAPI ActiveScriptSiteDebug_AddRef(IActiveScriptSiteDebug *iface)
579 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
580 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
583 static ULONG WINAPI ActiveScriptSiteDebug_Release(IActiveScriptSiteDebug *iface)
585 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
586 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
589 static HRESULT WINAPI ActiveScriptSiteDebug_GetDocumentContextFromPosition(IActiveScriptSiteDebug *iface,
590 CTXARG_T dwSourceContext, ULONG uCharacterOffset, ULONG uNumChars, IDebugDocumentContext **ppsc)
592 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
593 FIXME("(%p)->(%s %u %u %p)\n", This, wine_dbgstr_longlong(dwSourceContext), uCharacterOffset,
594 uNumChars, ppsc);
595 return E_NOTIMPL;
598 static HRESULT WINAPI ActiveScriptSiteDebug_GetApplication(IActiveScriptSiteDebug *iface, IDebugApplication **ppda)
600 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
601 FIXME("(%p)->(%p)\n", This, ppda);
602 return E_NOTIMPL;
605 static HRESULT WINAPI ActiveScriptSiteDebug_GetRootApplicationNode(IActiveScriptSiteDebug *iface,
606 IDebugApplicationNode **ppdanRoot)
608 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
609 FIXME("(%p)->(%p)\n", This, ppdanRoot);
610 return E_NOTIMPL;
613 static HRESULT WINAPI ActiveScriptSiteDebug_OnScriptErrorDebug(IActiveScriptSiteDebug *iface,
614 IActiveScriptErrorDebug *pErrorDebug, BOOL *pfEnterDebugger, BOOL *pfCallOnScriptErrorWhenContinuing)
616 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
617 FIXME("(%p)->(%p %p %p)\n", This, pErrorDebug, pfEnterDebugger, pfCallOnScriptErrorWhenContinuing);
618 return E_NOTIMPL;
621 static const IActiveScriptSiteDebugVtbl ActiveScriptSiteDebugVtbl = {
622 ActiveScriptSiteDebug_QueryInterface,
623 ActiveScriptSiteDebug_AddRef,
624 ActiveScriptSiteDebug_Release,
625 ActiveScriptSiteDebug_GetDocumentContextFromPosition,
626 ActiveScriptSiteDebug_GetApplication,
627 ActiveScriptSiteDebug_GetRootApplicationNode,
628 ActiveScriptSiteDebug_OnScriptErrorDebug
631 static inline ScriptHost *impl_from_IServiceProvider(IServiceProvider *iface)
633 return CONTAINING_RECORD(iface, ScriptHost, IServiceProvider_iface);
636 static HRESULT WINAPI ASServiceProvider_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
638 ScriptHost *This = impl_from_IServiceProvider(iface);
639 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
642 static ULONG WINAPI ASServiceProvider_AddRef(IServiceProvider *iface)
644 ScriptHost *This = impl_from_IServiceProvider(iface);
645 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
648 static ULONG WINAPI ASServiceProvider_Release(IServiceProvider *iface)
650 ScriptHost *This = impl_from_IServiceProvider(iface);
651 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
654 static HRESULT WINAPI ASServiceProvider_QueryService(IServiceProvider *iface, REFGUID guidService,
655 REFIID riid, void **ppv)
657 ScriptHost *This = impl_from_IServiceProvider(iface);
659 if(IsEqualGUID(&SID_SInternetHostSecurityManager, guidService)) {
660 TRACE("(%p)->(SID_SInternetHostSecurityManager)\n", This);
662 if(!This->window || !This->window->doc)
663 return E_NOINTERFACE;
665 return IInternetHostSecurityManager_QueryInterface(&This->window->doc->IInternetHostSecurityManager_iface,
666 riid, ppv);
669 if(IsEqualGUID(&SID_SContainerDispatch, guidService)) {
670 TRACE("(%p)->(SID_SContainerDispatch)\n", This);
672 if(!This->window || !This->window->doc)
673 return E_NOINTERFACE;
675 return IHTMLDocument2_QueryInterface(&This->window->doc->basedoc.IHTMLDocument2_iface, riid, ppv);
678 FIXME("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
679 return E_NOINTERFACE;
682 static const IServiceProviderVtbl ASServiceProviderVtbl = {
683 ASServiceProvider_QueryInterface,
684 ASServiceProvider_AddRef,
685 ASServiceProvider_Release,
686 ASServiceProvider_QueryService
689 static ScriptHost *create_script_host(HTMLInnerWindow *window, const GUID *guid)
691 ScriptHost *ret;
692 HRESULT hres;
694 ret = heap_alloc_zero(sizeof(*ret));
695 if(!ret)
696 return NULL;
698 ret->IActiveScriptSite_iface.lpVtbl = &ActiveScriptSiteVtbl;
699 ret->IActiveScriptSiteInterruptPoll_iface.lpVtbl = &ActiveScriptSiteInterruptPollVtbl;
700 ret->IActiveScriptSiteWindow_iface.lpVtbl = &ActiveScriptSiteWindowVtbl;
701 ret->IActiveScriptSiteUIControl_iface.lpVtbl = &ActiveScriptSiteUIControlVtbl;
702 ret->IActiveScriptSiteDebug_iface.lpVtbl = &ActiveScriptSiteDebugVtbl;
703 ret->IServiceProvider_iface.lpVtbl = &ASServiceProviderVtbl;
704 ret->ref = 1;
705 ret->window = window;
706 ret->script_state = SCRIPTSTATE_UNINITIALIZED;
708 ret->guid = *guid;
709 list_add_tail(&window->script_hosts, &ret->entry);
711 hres = CoCreateInstance(&ret->guid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
712 &IID_IActiveScript, (void**)&ret->script);
713 if(FAILED(hres))
714 WARN("Could not load script engine: %08x\n", hres);
715 else if(!init_script_engine(ret))
716 release_script_engine(ret);
718 return ret;
721 static void dispatch_script_readystatechange_event(HTMLScriptElement *script)
723 DOMEvent *event;
724 HRESULT hres;
726 if(script->readystate != READYSTATE_LOADED ||
727 dispex_compat_mode(&script->element.node.event_target.dispex) < COMPAT_MODE_IE10) {
728 hres = create_document_event(script->element.node.doc, EVENTID_READYSTATECHANGE, &event);
729 if(SUCCEEDED(hres)) {
730 dispatch_event(&script->element.node.event_target, event);
731 IDOMEvent_Release(&event->IDOMEvent_iface);
735 if(script->readystate == READYSTATE_LOADED) {
736 hres = create_document_event(script->element.node.doc, EVENTID_LOAD, &event);
737 if(SUCCEEDED(hres)) {
738 dispatch_event(&script->element.node.event_target, event);
739 IDOMEvent_Release(&event->IDOMEvent_iface);
744 typedef struct {
745 task_t header;
746 HTMLScriptElement *elem;
747 } fire_readystatechange_task_t;
749 static void fire_readystatechange_proc(task_t *_task)
751 fire_readystatechange_task_t *task = (fire_readystatechange_task_t*)_task;
753 if(!task->elem->pending_readystatechange_event)
754 return;
756 task->elem->pending_readystatechange_event = FALSE;
757 dispatch_script_readystatechange_event(task->elem);
760 static void fire_readystatechange_task_destr(task_t *_task)
762 fire_readystatechange_task_t *task = (fire_readystatechange_task_t*)_task;
764 IHTMLScriptElement_Release(&task->elem->IHTMLScriptElement_iface);
767 static void set_script_elem_readystate(HTMLScriptElement *script_elem, READYSTATE readystate)
769 script_elem->readystate = readystate;
771 if(readystate != READYSTATE_LOADED &&
772 dispex_compat_mode(&script_elem->element.node.event_target.dispex) >= COMPAT_MODE_IE11)
773 return;
775 if(readystate != READYSTATE_INTERACTIVE) {
776 if(!script_elem->element.node.doc->window->parser_callback_cnt) {
777 fire_readystatechange_task_t *task;
778 HRESULT hres;
780 if(script_elem->pending_readystatechange_event)
781 return;
783 task = heap_alloc(sizeof(*task));
784 if(!task)
785 return;
787 IHTMLScriptElement_AddRef(&script_elem->IHTMLScriptElement_iface);
788 task->elem = script_elem;
790 hres = push_task(&task->header, fire_readystatechange_proc, fire_readystatechange_task_destr,
791 script_elem->element.node.doc->window->task_magic);
792 if(SUCCEEDED(hres))
793 script_elem->pending_readystatechange_event = TRUE;
794 }else {
795 script_elem->pending_readystatechange_event = FALSE;
796 dispatch_script_readystatechange_event(script_elem);
801 static void parse_elem_text(ScriptHost *script_host, HTMLScriptElement *script_elem, LPCWSTR text)
803 EXCEPINFO excepinfo;
804 VARIANT var;
805 HRESULT hres;
807 TRACE("%s\n", debugstr_w(text));
809 VariantInit(&var);
810 memset(&excepinfo, 0, sizeof(excepinfo));
811 TRACE(">>>\n");
812 hres = IActiveScriptParse_ParseScriptText(script_host->parse, text, L"window", NULL, L"</SCRIPT>",
813 0, 0, SCRIPTTEXT_ISVISIBLE|SCRIPTTEXT_HOSTMANAGESSOURCE,
814 &var, &excepinfo);
815 if(SUCCEEDED(hres))
816 TRACE("<<<\n");
817 else
818 WARN("<<< %08x\n", hres);
821 typedef struct {
822 BSCallback bsc;
824 HTMLScriptElement *script_elem;
825 nsILoadGroup *load_group;
826 nsIRequest *request;
827 DWORD scheme;
829 DWORD size;
830 char *buf;
831 } ScriptBSC;
833 static HRESULT get_binding_text(ScriptBSC *bsc, WCHAR **ret)
835 UINT cp = CP_UTF8;
836 WCHAR *text;
838 if(!bsc->bsc.read) {
839 text = heap_alloc(sizeof(WCHAR));
840 if(!text)
841 return E_OUTOFMEMORY;
842 *text = 0;
843 *ret = text;
844 return S_OK;
847 switch(bsc->bsc.bom) {
848 case BOM_UTF16:
849 if(bsc->bsc.read % sizeof(WCHAR)) {
850 FIXME("The buffer is not a valid utf16 string\n");
851 return E_FAIL;
854 text = heap_alloc(bsc->bsc.read+sizeof(WCHAR));
855 if(!text)
856 return E_OUTOFMEMORY;
858 memcpy(text, bsc->buf, bsc->bsc.read);
859 text[bsc->bsc.read/sizeof(WCHAR)] = 0;
860 break;
862 default:
863 /* FIXME: Try to use charset from HTTP headers first */
864 cp = get_document_charset(bsc->script_elem->element.node.doc);
865 /* fall through */
866 case BOM_UTF8: {
867 DWORD len;
869 len = MultiByteToWideChar(cp, 0, bsc->buf, bsc->bsc.read, NULL, 0);
870 text = heap_alloc((len+1)*sizeof(WCHAR));
871 if(!text)
872 return E_OUTOFMEMORY;
874 MultiByteToWideChar(cp, 0, bsc->buf, bsc->bsc.read, text, len);
875 text[len] = 0;
879 *ret = text;
880 return S_OK;
883 static void script_file_available(ScriptBSC *bsc)
885 HTMLScriptElement *script_elem = bsc->script_elem;
886 HTMLInnerWindow *window = bsc->bsc.window;
887 ScriptHost *script_host;
888 nsIDOMNode *parent;
889 nsresult nsres;
890 HRESULT hres;
892 assert(window != NULL);
894 hres = get_binding_text(bsc, &script_elem->src_text);
895 if(FAILED(hres))
896 return;
898 script_host = get_elem_script_host(window, script_elem);
899 if(!script_host)
900 return;
902 if(window->parser_callback_cnt) {
903 script_queue_entry_t *queue;
905 TRACE("Adding to queue\n");
907 queue = heap_alloc(sizeof(*queue));
908 if(!queue)
909 return;
911 IHTMLScriptElement_AddRef(&script_elem->IHTMLScriptElement_iface);
912 queue->script = script_elem;
914 list_add_tail(&window->script_queue, &queue->entry);
915 return;
918 nsres = nsIDOMElement_GetParentNode(script_elem->element.dom_element, &parent);
919 if(NS_FAILED(nsres) || !parent) {
920 TRACE("No parent, not executing\n");
921 script_elem->parse_on_bind = TRUE;
922 return;
925 nsIDOMNode_Release(parent);
927 script_host = get_elem_script_host(window, script_elem);
928 if(!script_host)
929 return;
931 script_elem->parsed = TRUE;
932 if(script_host->parse)
933 parse_elem_text(script_host, script_elem, script_elem->src_text);
936 static inline ScriptBSC *impl_from_BSCallback(BSCallback *iface)
938 return CONTAINING_RECORD(iface, ScriptBSC, bsc);
941 static void ScriptBSC_destroy(BSCallback *bsc)
943 ScriptBSC *This = impl_from_BSCallback(bsc);
945 if(This->script_elem) {
946 IHTMLScriptElement_Release(&This->script_elem->IHTMLScriptElement_iface);
947 This->script_elem = NULL;
950 if(This->request) {
951 ERR("Unfinished request\n");
952 nsIRequest_Release(This->request);
954 if(This->load_group)
955 nsILoadGroup_Release(This->load_group);
957 heap_free(This->buf);
958 heap_free(This);
961 static HRESULT ScriptBSC_init_bindinfo(BSCallback *bsc)
963 return S_OK;
966 static HRESULT ScriptBSC_start_binding(BSCallback *bsc)
968 ScriptBSC *This = impl_from_BSCallback(bsc);
969 nsresult nsres;
971 This->script_elem->binding = &This->bsc;
973 if(This->load_group) {
974 nsres = create_onload_blocker_request(&This->request);
975 if(NS_SUCCEEDED(nsres)) {
976 nsres = nsILoadGroup_AddRequest(This->load_group, This->request, NULL);
977 if(NS_FAILED(nsres))
978 ERR("AddRequest failed: %08x\n", nsres);
982 /* FIXME: We should find a better to decide if 'loading' state is supposed to be used by the protocol. */
983 if(This->scheme == URL_SCHEME_HTTPS || This->scheme == URL_SCHEME_HTTP)
984 set_script_elem_readystate(This->script_elem, READYSTATE_LOADING);
986 return S_OK;
989 static HRESULT ScriptBSC_stop_binding(BSCallback *bsc, HRESULT result)
991 ScriptBSC *This = impl_from_BSCallback(bsc);
992 nsresult nsres;
994 if(SUCCEEDED(result) && !This->script_elem)
995 result = E_UNEXPECTED;
997 assert(FAILED(result) || This->script_elem->binding == &This->bsc);
998 This->script_elem->binding = NULL;
1000 if(This->script_elem->readystate == READYSTATE_LOADING)
1001 set_script_elem_readystate(This->script_elem, READYSTATE_LOADED);
1003 if(SUCCEEDED(result)) {
1004 script_file_available(This);
1005 }else {
1006 FIXME("binding failed %08x\n", result);
1007 heap_free(This->buf);
1008 This->buf = NULL;
1009 This->size = 0;
1012 if(This->request) {
1013 nsres = nsILoadGroup_RemoveRequest(This->load_group, This->request, NULL, NS_OK);
1014 if(NS_FAILED(nsres))
1015 ERR("RemoveRequest failed: %08x\n", nsres);
1016 nsIRequest_Release(This->request);
1017 This->request = NULL;
1020 IHTMLScriptElement_Release(&This->script_elem->IHTMLScriptElement_iface);
1021 This->script_elem = NULL;
1022 return S_OK;
1025 static HRESULT ScriptBSC_read_data(BSCallback *bsc, IStream *stream)
1027 ScriptBSC *This = impl_from_BSCallback(bsc);
1028 DWORD read;
1029 HRESULT hres;
1031 if(!This->buf) {
1032 This->buf = heap_alloc(128);
1033 if(!This->buf)
1034 return E_OUTOFMEMORY;
1035 This->size = 128;
1038 do {
1039 if(This->bsc.read >= This->size) {
1040 void *new_buf;
1041 new_buf = heap_realloc(This->buf, This->size << 1);
1042 if(!new_buf)
1043 return E_OUTOFMEMORY;
1044 This->size <<= 1;
1045 This->buf = new_buf;
1048 hres = read_stream(&This->bsc, stream, This->buf+This->bsc.read, This->size-This->bsc.read, &read);
1049 }while(hres == S_OK);
1051 return S_OK;
1054 static HRESULT ScriptBSC_on_progress(BSCallback *bsc, ULONG status_code, LPCWSTR status_text)
1056 return S_OK;
1059 static HRESULT ScriptBSC_on_response(BSCallback *bsc, DWORD response_code,
1060 LPCWSTR response_headers)
1062 return S_OK;
1065 static HRESULT ScriptBSC_beginning_transaction(BSCallback *bsc, WCHAR **additional_headers)
1067 return S_FALSE;
1070 static const BSCallbackVtbl ScriptBSCVtbl = {
1071 ScriptBSC_destroy,
1072 ScriptBSC_init_bindinfo,
1073 ScriptBSC_start_binding,
1074 ScriptBSC_stop_binding,
1075 ScriptBSC_read_data,
1076 ScriptBSC_on_progress,
1077 ScriptBSC_on_response,
1078 ScriptBSC_beginning_transaction
1082 HRESULT load_script(HTMLScriptElement *script_elem, const WCHAR *src, BOOL async)
1084 HTMLInnerWindow *window;
1085 ScriptBSC *bsc;
1086 IMoniker *mon;
1087 IUri *uri;
1088 HRESULT hres;
1090 static const WCHAR wine_schemaW[] = {'w','i','n','e',':'};
1092 if(lstrlenW(src) > ARRAY_SIZE(wine_schemaW) && !memcmp(src, wine_schemaW, sizeof(wine_schemaW)))
1093 src += ARRAY_SIZE(wine_schemaW);
1095 TRACE("(%p %s %x)\n", script_elem, debugstr_w(src), async);
1097 if(!script_elem->element.node.doc || !(window = script_elem->element.node.doc->window)) {
1098 ERR("no window\n");
1099 return E_UNEXPECTED;
1102 hres = create_uri(src, 0, &uri);
1103 if(FAILED(hres))
1104 return hres;
1106 hres = CreateURLMonikerEx2(NULL, uri, &mon, URL_MK_UNIFORM);
1107 if(FAILED(hres)) {
1108 IUri_Release(uri);
1109 return hres;
1112 bsc = heap_alloc_zero(sizeof(*bsc));
1113 if(!bsc) {
1114 IMoniker_Release(mon);
1115 IUri_Release(uri);
1116 return E_OUTOFMEMORY;
1119 init_bscallback(&bsc->bsc, &ScriptBSCVtbl, mon, async ? BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA : 0);
1120 IMoniker_Release(mon);
1122 hres = IUri_GetScheme(uri, &bsc->scheme);
1123 IUri_Release(uri);
1124 if(FAILED(hres))
1125 bsc->scheme = URL_SCHEME_UNKNOWN;
1127 IHTMLScriptElement_AddRef(&script_elem->IHTMLScriptElement_iface);
1128 bsc->script_elem = script_elem;
1130 if(window->bscallback && window->bscallback->nschannel &&
1131 window->bscallback->nschannel->load_group) {
1132 cpp_bool contains;
1133 nsresult nsres;
1135 nsres = nsIDOMNode_Contains(script_elem->element.node.doc->node.nsnode,
1136 script_elem->element.node.nsnode, &contains);
1137 if(NS_SUCCEEDED(nsres) && contains) {
1138 TRACE("script %p will block load event\n", script_elem);
1139 bsc->load_group = window->bscallback->nschannel->load_group;
1140 nsILoadGroup_AddRef(bsc->load_group);
1144 hres = start_binding(window, &bsc->bsc, NULL);
1146 IBindStatusCallback_Release(&bsc->bsc.IBindStatusCallback_iface);
1147 return hres;
1150 static void parse_inline_script(ScriptHost *script_host, HTMLScriptElement *script_elem)
1152 const PRUnichar *text;
1153 nsAString text_str;
1154 nsresult nsres;
1156 nsAString_Init(&text_str, NULL);
1157 nsres = nsIDOMHTMLScriptElement_GetText(script_elem->nsscript, &text_str);
1158 nsAString_GetData(&text_str, &text);
1160 set_script_elem_readystate(script_elem, READYSTATE_INTERACTIVE);
1162 if(NS_FAILED(nsres)) {
1163 ERR("GetText failed: %08x\n", nsres);
1164 }else if(*text) {
1165 parse_elem_text(script_host, script_elem, text);
1168 nsAString_Finish(&text_str);
1171 static BOOL parse_script_elem(ScriptHost *script_host, HTMLScriptElement *script_elem)
1173 nsAString src_str, event_str;
1174 BOOL is_complete = FALSE;
1175 const PRUnichar *src;
1176 nsresult nsres;
1178 nsAString_Init(&event_str, NULL);
1179 nsres = nsIDOMHTMLScriptElement_GetEvent(script_elem->nsscript, &event_str);
1180 if(NS_SUCCEEDED(nsres)) {
1181 const PRUnichar *event;
1183 nsAString_GetData(&event_str, &event);
1184 if(*event) {
1185 TRACE("deferring event %s script evaluation\n", debugstr_w(event));
1186 nsAString_Finish(&event_str);
1187 return FALSE;
1189 }else {
1190 ERR("GetEvent failed: %08x\n", nsres);
1192 nsAString_Finish(&event_str);
1194 nsAString_Init(&src_str, NULL);
1195 nsres = nsIDOMHTMLScriptElement_GetSrc(script_elem->nsscript, &src_str);
1196 nsAString_GetData(&src_str, &src);
1198 if(NS_FAILED(nsres)) {
1199 ERR("GetSrc failed: %08x\n", nsres);
1200 }else if(*src) {
1201 load_script(script_elem, src, FALSE);
1202 is_complete = script_elem->parsed;
1203 }else {
1204 parse_inline_script(script_host, script_elem);
1205 is_complete = TRUE;
1208 nsAString_Finish(&src_str);
1210 return is_complete;
1213 static GUID get_default_script_guid(HTMLInnerWindow *window)
1215 /* If not specified, we should use very first script host that was created for the page (or JScript if none) */
1216 return list_empty(&window->script_hosts)
1217 ? CLSID_JScript
1218 : LIST_ENTRY(list_head(&window->script_hosts), ScriptHost, entry)->guid;
1221 static BOOL get_guid_from_type(LPCWSTR type, GUID *guid)
1223 /* FIXME: Handle more types */
1224 if(!wcsicmp(type, L"text/javascript") || !wcsicmp(type, L"text/jscript")) {
1225 *guid = CLSID_JScript;
1226 }else if(!wcsicmp(type, L"text/vbscript")) {
1227 *guid = CLSID_VBScript;
1228 }else {
1229 FIXME("Unknown type %s\n", debugstr_w(type));
1230 return FALSE;
1233 return TRUE;
1236 static BOOL get_guid_from_language(LPCWSTR type, GUID *guid)
1238 HRESULT hres;
1240 hres = CLSIDFromProgID(type, guid);
1241 if(FAILED(hres))
1242 return FALSE;
1244 /* FIXME: Check CATID_ActiveScriptParse */
1246 return TRUE;
1249 static BOOL get_script_guid(HTMLInnerWindow *window, nsIDOMHTMLScriptElement *nsscript, GUID *guid)
1251 nsIDOMElement *nselem;
1252 const PRUnichar *language;
1253 nsAString val_str;
1254 BOOL ret = FALSE;
1255 nsresult nsres;
1257 nsAString_Init(&val_str, NULL);
1259 nsres = nsIDOMHTMLScriptElement_GetType(nsscript, &val_str);
1260 if(NS_SUCCEEDED(nsres)) {
1261 const PRUnichar *type;
1263 nsAString_GetData(&val_str, &type);
1264 if(*type) {
1265 ret = get_guid_from_type(type, guid);
1266 nsAString_Finish(&val_str);
1267 return ret;
1269 }else {
1270 ERR("GetType failed: %08x\n", nsres);
1273 nsres = nsIDOMHTMLScriptElement_QueryInterface(nsscript, &IID_nsIDOMElement, (void**)&nselem);
1274 assert(nsres == NS_OK);
1276 nsres = get_elem_attr_value(nselem, L"language", &val_str, &language);
1277 nsIDOMElement_Release(nselem);
1278 if(NS_SUCCEEDED(nsres)) {
1279 if(*language) {
1280 ret = get_guid_from_language(language, guid);
1281 }else {
1282 *guid = get_default_script_guid(window);
1283 ret = TRUE;
1285 nsAString_Finish(&val_str);
1288 return ret;
1291 static ScriptHost *get_script_host(HTMLInnerWindow *window, const GUID *guid)
1293 ScriptHost *iter;
1295 LIST_FOR_EACH_ENTRY(iter, &window->script_hosts, ScriptHost, entry) {
1296 if(IsEqualGUID(guid, &iter->guid))
1297 return iter;
1300 return create_script_host(window, guid);
1303 static ScriptHost *get_elem_script_host(HTMLInnerWindow *window, HTMLScriptElement *script_elem)
1305 GUID guid;
1307 if(!get_script_guid(window, script_elem->nsscript, &guid)) {
1308 WARN("Could not find script GUID\n");
1309 return NULL;
1312 if(IsEqualGUID(&CLSID_JScript, &guid) && (!window->doc->browser || window->doc->browser->script_mode != SCRIPTMODE_ACTIVESCRIPT)) {
1313 TRACE("Ignoring JScript\n");
1314 return NULL;
1317 return get_script_host(window, &guid);
1320 void doc_insert_script(HTMLInnerWindow *window, HTMLScriptElement *script_elem, BOOL from_parser)
1322 ScriptHost *script_host;
1323 BOOL is_complete = FALSE;
1325 script_host = get_elem_script_host(window, script_elem);
1326 if(!script_host)
1327 return;
1329 if(script_host->parse) {
1330 if(script_elem->src_text) {
1331 if(from_parser)
1332 set_script_elem_readystate(script_elem, READYSTATE_INTERACTIVE);
1333 script_elem->parsed = TRUE;
1334 parse_elem_text(script_host, script_elem, script_elem->src_text);
1335 is_complete = TRUE;
1336 }else if(!script_elem->binding) {
1337 is_complete = parse_script_elem(script_host, script_elem);
1341 if(is_complete)
1342 set_script_elem_readystate(script_elem, READYSTATE_COMPLETE);
1345 IDispatch *script_parse_event(HTMLInnerWindow *window, LPCWSTR text)
1347 ScriptHost *script_host;
1348 GUID guid;
1349 const WCHAR *ptr;
1350 IDispatch *disp;
1351 HRESULT hres;
1353 TRACE("%s\n", debugstr_w(text));
1355 for(ptr = text; iswalnum(*ptr); ptr++);
1356 if(*ptr == ':') {
1357 LPWSTR language;
1358 BOOL b;
1360 language = heap_alloc((ptr-text+1)*sizeof(WCHAR));
1361 if(!language)
1362 return NULL;
1364 memcpy(language, text, (ptr-text)*sizeof(WCHAR));
1365 language[ptr-text] = 0;
1367 b = get_guid_from_language(language, &guid);
1369 heap_free(language);
1371 if(!b) {
1372 WARN("Could not find language\n");
1373 return NULL;
1376 ptr++;
1377 }else {
1378 ptr = text;
1379 guid = get_default_script_guid(window);
1382 if(IsEqualGUID(&CLSID_JScript, &guid)
1383 && (!window->doc->browser || window->doc->browser->script_mode != SCRIPTMODE_ACTIVESCRIPT)) {
1384 TRACE("Ignoring JScript\n");
1385 return NULL;
1388 script_host = get_script_host(window, &guid);
1389 if(!script_host || !script_host->parse_proc)
1390 return NULL;
1392 hres = IActiveScriptParseProcedure2_ParseProcedureText(script_host->parse_proc, ptr, NULL, L"",
1393 NULL, NULL, L"\"", 0 /* FIXME */, 0,
1394 SCRIPTPROC_HOSTMANAGESSOURCE|SCRIPTPROC_IMPLICIT_THIS|SCRIPTPROC_IMPLICIT_PARENTS, &disp);
1395 if(FAILED(hres)) {
1396 WARN("ParseProcedureText failed: %08x\n", hres);
1397 return NULL;
1400 TRACE("ret %p\n", disp);
1401 return disp;
1404 HRESULT exec_script(HTMLInnerWindow *window, const WCHAR *code, const WCHAR *lang, VARIANT *ret)
1406 ScriptHost *script_host;
1407 EXCEPINFO ei;
1408 GUID guid;
1409 HRESULT hres;
1411 if(!get_guid_from_language(lang, &guid)) {
1412 WARN("Could not find script GUID\n");
1413 return CO_E_CLASSSTRING;
1416 script_host = get_script_host(window, &guid);
1417 if(!script_host) {
1418 FIXME("No script host\n");
1419 return E_FAIL;
1422 if(!script_host->parse) {
1423 FIXME("script_host->parse == NULL\n");
1424 return E_FAIL;
1427 memset(&ei, 0, sizeof(ei));
1428 TRACE(">>>\n");
1429 hres = IActiveScriptParse_ParseScriptText(script_host->parse, code, NULL, NULL, L"\"", 0, 0, SCRIPTTEXT_ISVISIBLE, ret, &ei);
1430 if(SUCCEEDED(hres))
1431 TRACE("<<<\n");
1432 else
1433 WARN("<<< %08x\n", hres);
1435 return hres;
1438 IDispatch *get_script_disp(ScriptHost *script_host)
1440 IDispatch *disp;
1441 HRESULT hres;
1443 if(!script_host->script)
1444 return NULL;
1446 hres = IActiveScript_GetScriptDispatch(script_host->script, L"window", &disp);
1447 if(FAILED(hres))
1448 return NULL;
1450 return disp;
1453 static EventTarget *find_event_target(HTMLDocumentNode *doc, HTMLScriptElement *script_elem)
1455 EventTarget *event_target = NULL;
1456 const PRUnichar *target_id;
1457 nsAString target_id_str;
1458 nsresult nsres;
1459 HRESULT hres;
1461 nsAString_Init(&target_id_str, NULL);
1462 nsres = nsIDOMHTMLScriptElement_GetHtmlFor(script_elem->nsscript, &target_id_str);
1463 if(NS_FAILED(nsres)) {
1464 ERR("GetScriptFor failed: %08x\n", nsres);
1465 nsAString_Finish(&target_id_str);
1466 return NULL;
1469 nsAString_GetData(&target_id_str, &target_id);
1470 if(!*target_id) {
1471 FIXME("Empty for attribute\n");
1472 }else if(!wcscmp(target_id, L"document")) {
1473 event_target = &doc->node.event_target;
1474 htmldoc_addref(&doc->basedoc);
1475 }else if(!wcscmp(target_id, L"window")) {
1476 if(doc->window) {
1477 event_target = &doc->window->event_target;
1478 IDispatchEx_AddRef(&event_target->dispex.IDispatchEx_iface);
1480 }else {
1481 HTMLElement *target_elem;
1483 hres = get_doc_elem_by_id(doc, target_id, &target_elem);
1484 if(SUCCEEDED(hres) && target_elem) {
1485 event_target = &target_elem->node.event_target;
1489 nsAString_Finish(&target_id_str);
1490 return event_target;
1493 static BOOL parse_event_str(WCHAR *event, const WCHAR **args)
1495 WCHAR *ptr;
1497 TRACE("%s\n", debugstr_w(event));
1499 for(ptr = event; iswalnum(*ptr); ptr++);
1500 if(!*ptr) {
1501 *args = NULL;
1502 return TRUE;
1505 if(*ptr != '(')
1506 return FALSE;
1508 *ptr++ = 0;
1509 *args = ptr;
1510 while(iswalnum(*ptr) || iswspace(*ptr) || *ptr == ',')
1511 ptr++;
1513 if(*ptr != ')')
1514 return FALSE;
1516 *ptr++ = 0;
1517 return !*ptr;
1520 static IDispatch *parse_event_elem(HTMLDocumentNode *doc, HTMLScriptElement *script_elem, WCHAR **ret_event)
1522 ScriptHost *script_host;
1523 WCHAR *event = NULL;
1524 const WCHAR *args;
1525 nsAString nsstr;
1526 IDispatch *disp;
1527 nsresult nsres;
1528 HRESULT hres;
1530 if(script_elem->parsed)
1531 return NULL;
1533 script_host = get_elem_script_host(doc->window, script_elem);
1534 if(!script_host || !script_host->parse_proc)
1535 return NULL;
1537 nsAString_Init(&nsstr, NULL);
1538 nsres = nsIDOMHTMLScriptElement_GetEvent(script_elem->nsscript, &nsstr);
1539 if(NS_SUCCEEDED(nsres)) {
1540 const PRUnichar *event_val;
1542 nsAString_GetData(&nsstr, &event_val);
1543 event = heap_strdupW(event_val);
1545 nsAString_Finish(&nsstr);
1546 if(!event)
1547 return NULL;
1549 if(!parse_event_str(event, &args)) {
1550 WARN("parsing %s failed\n", debugstr_w(event));
1551 heap_free(event);
1552 return NULL;
1555 nsAString_Init(&nsstr, NULL);
1556 nsres = nsIDOMHTMLScriptElement_GetText(script_elem->nsscript, &nsstr);
1557 if(NS_SUCCEEDED(nsres)) {
1558 const PRUnichar *text;
1560 nsAString_GetData(&nsstr, &text);
1561 hres = IActiveScriptParseProcedure2_ParseProcedureText(script_host->parse_proc, text, args,
1562 L"", NULL, NULL, L"</SCRIPT>", 0, 0,
1563 SCRIPTPROC_HOSTMANAGESSOURCE|SCRIPTPROC_IMPLICIT_THIS|SCRIPTPROC_IMPLICIT_PARENTS, &disp);
1564 if(FAILED(hres))
1565 disp = NULL;
1566 }else {
1567 ERR("GetText failed: %08x\n", nsres);
1568 disp = NULL;
1570 nsAString_Finish(&nsstr);
1571 if(!disp) {
1572 heap_free(event);
1573 return NULL;
1576 *ret_event = event;
1577 return disp;
1580 void bind_event_scripts(HTMLDocumentNode *doc)
1582 HTMLPluginContainer *plugin_container;
1583 nsIDOMHTMLScriptElement *nsscript;
1584 HTMLScriptElement *script_elem;
1585 EventTarget *event_target;
1586 nsIDOMNodeList *node_list;
1587 nsIDOMNode *script_node;
1588 nsAString selector_str;
1589 IDispatch *event_disp;
1590 UINT32 length, i;
1591 WCHAR *event;
1592 nsresult nsres;
1593 HRESULT hres;
1595 TRACE("%p\n", doc);
1597 if(!doc->nsdoc)
1598 return;
1600 nsAString_InitDepend(&selector_str, L"script[event]");
1601 nsres = nsIDOMHTMLDocument_QuerySelectorAll(doc->nsdoc, &selector_str, &node_list);
1602 nsAString_Finish(&selector_str);
1603 if(NS_FAILED(nsres)) {
1604 ERR("QuerySelectorAll failed: %08x\n", nsres);
1605 return;
1608 if(!node_list)
1609 return;
1611 nsres = nsIDOMNodeList_GetLength(node_list, &length);
1612 assert(nsres == NS_OK);
1614 for(i=0; i < length; i++) {
1615 nsres = nsIDOMNodeList_Item(node_list, i, &script_node);
1616 if(NS_FAILED(nsres) || !script_node) {
1617 ERR("Item(%d) failed: %08x\n", i, nsres);
1618 continue;
1621 nsres = nsIDOMNode_QueryInterface(script_node, &IID_nsIDOMHTMLScriptElement, (void**)&nsscript);
1622 assert(nsres == NS_OK);
1623 nsIDOMNode_Release(script_node);
1625 hres = script_elem_from_nsscript(nsscript, &script_elem);
1626 if(FAILED(hres))
1627 continue;
1629 event_disp = parse_event_elem(doc, script_elem, &event);
1630 if(event_disp) {
1631 event_target = find_event_target(doc, script_elem);
1632 if(event_target) {
1633 hres = IDispatchEx_QueryInterface(&event_target->dispex.IDispatchEx_iface, &IID_HTMLPluginContainer,
1634 (void**)&plugin_container);
1635 if(SUCCEEDED(hres))
1636 bind_activex_event(doc, plugin_container, event, event_disp);
1637 else
1638 bind_target_event(doc, event_target, event, event_disp);
1640 IDispatchEx_Release(&event_target->dispex.IDispatchEx_iface);
1641 if(plugin_container)
1642 node_release(&plugin_container->element.node);
1645 heap_free(event);
1646 IDispatch_Release(event_disp);
1649 IHTMLScriptElement_Release(&script_elem->IHTMLScriptElement_iface);
1652 nsIDOMNodeList_Release(node_list);
1655 BOOL find_global_prop(HTMLInnerWindow *window, BSTR name, DWORD flags, ScriptHost **ret_host, DISPID *ret_id)
1657 IDispatchEx *dispex;
1658 IDispatch *disp;
1659 ScriptHost *iter;
1660 HRESULT hres;
1662 LIST_FOR_EACH_ENTRY(iter, &window->script_hosts, ScriptHost, entry) {
1663 disp = get_script_disp(iter);
1664 if(!disp)
1665 continue;
1667 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1668 if(SUCCEEDED(hres)) {
1669 hres = IDispatchEx_GetDispID(dispex, name, flags & (~fdexNameEnsure), ret_id);
1670 IDispatchEx_Release(dispex);
1671 }else {
1672 FIXME("No IDispatchEx\n");
1673 hres = E_NOTIMPL;
1676 IDispatch_Release(disp);
1677 if(SUCCEEDED(hres)) {
1678 *ret_host = iter;
1679 return TRUE;
1683 return FALSE;
1686 static BOOL is_jscript_available(void)
1688 static BOOL available, checked;
1690 if(!checked) {
1691 IUnknown *unk;
1692 HRESULT hres = CoGetClassObject(&CLSID_JScript, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void**)&unk);
1694 if(SUCCEEDED(hres)) {
1695 available = TRUE;
1696 IUnknown_Release(unk);
1697 }else {
1698 available = FALSE;
1700 checked = TRUE;
1703 return available;
1706 static BOOL use_gecko_script(IUri *uri)
1708 BSTR display_uri;
1709 DWORD zone;
1710 HRESULT hres;
1712 hres = IUri_GetDisplayUri(uri, &display_uri);
1713 if(FAILED(hres))
1714 return FALSE;
1716 hres = IInternetSecurityManager_MapUrlToZone(get_security_manager(), display_uri, &zone, 0);
1717 SysFreeString(display_uri);
1718 if(FAILED(hres)) {
1719 WARN("Could not map %s to zone: %08x\n", debugstr_w(display_uri), hres);
1720 return TRUE;
1723 TRACE("zone %d\n", zone);
1724 return zone == URLZONE_UNTRUSTED;
1727 void update_browser_script_mode(GeckoBrowser *browser, IUri *uri)
1729 nsIWebBrowserSetup *setup;
1730 nsresult nsres;
1732 if(!is_jscript_available()) {
1733 TRACE("jscript.dll not available\n");
1734 browser->script_mode = SCRIPTMODE_GECKO;
1735 return;
1738 browser->script_mode = use_gecko_script(uri) ? SCRIPTMODE_GECKO : SCRIPTMODE_ACTIVESCRIPT;
1740 nsres = nsIWebBrowser_QueryInterface(browser->webbrowser, &IID_nsIWebBrowserSetup, (void**)&setup);
1741 if(NS_SUCCEEDED(nsres)) {
1742 nsres = nsIWebBrowserSetup_SetProperty(setup, SETUP_ALLOW_JAVASCRIPT,
1743 browser->script_mode == SCRIPTMODE_GECKO);
1745 if(NS_SUCCEEDED(nsres))
1746 nsres = nsIWebBrowserSetup_SetProperty(setup, SETUP_DISABLE_NOSCRIPT, TRUE);
1748 nsIWebBrowserSetup_Release(setup);
1751 if(NS_FAILED(nsres))
1752 ERR("JavaScript setup failed: %08x\n", nsres);
1755 void release_script_hosts(HTMLInnerWindow *window)
1757 script_queue_entry_t *queue_iter;
1758 ScriptHost *iter;
1760 while(!list_empty(&window->script_queue)) {
1761 queue_iter = LIST_ENTRY(list_head(&window->script_queue), script_queue_entry_t, entry);
1763 list_remove(&queue_iter->entry);
1764 IHTMLScriptElement_Release(&queue_iter->script->IHTMLScriptElement_iface);
1765 heap_free(queue_iter);
1768 while(!list_empty(&window->script_hosts)) {
1769 iter = LIST_ENTRY(list_head(&window->script_hosts), ScriptHost, entry);
1771 release_script_engine(iter);
1772 list_remove(&iter->entry);
1773 iter->window = NULL;
1774 IActiveScriptSite_Release(&iter->IActiveScriptSite_iface);