directmanipulation: Return S_OK form viewport_SetViewportOptions stub.
[wine/zf.git] / dlls / mshtml / script.c
blob6f06d3b0919675c4786f13900d906c1b8c710cee
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 static const WCHAR documentW[] = {'d','o','c','u','m','e','n','t',0};
71 static const WCHAR windowW[] = {'w','i','n','d','o','w',0};
72 static const WCHAR script_endW[] = {'<','/','S','C','R','I','P','T','>',0};
73 static const WCHAR emptyW[] = {0};
75 struct ScriptHost {
76 IActiveScriptSite IActiveScriptSite_iface;
77 IActiveScriptSiteInterruptPoll IActiveScriptSiteInterruptPoll_iface;
78 IActiveScriptSiteWindow IActiveScriptSiteWindow_iface;
79 IActiveScriptSiteUIControl IActiveScriptSiteUIControl_iface;
80 IActiveScriptSiteDebug IActiveScriptSiteDebug_iface;
81 IServiceProvider IServiceProvider_iface;
83 LONG ref;
85 IActiveScript *script;
86 IActiveScriptParse *parse;
87 IActiveScriptParseProcedure2 *parse_proc;
89 SCRIPTSTATE script_state;
91 HTMLInnerWindow *window;
93 GUID guid;
94 struct list entry;
97 static ScriptHost *get_elem_script_host(HTMLInnerWindow*,HTMLScriptElement*);
99 static BOOL set_script_prop(ScriptHost *script_host, DWORD property, VARIANT *val)
101 IActiveScriptProperty *script_prop;
102 HRESULT hres;
104 hres = IActiveScript_QueryInterface(script_host->script, &IID_IActiveScriptProperty,
105 (void**)&script_prop);
106 if(FAILED(hres)) {
107 WARN("Could not get IActiveScriptProperty iface: %08x\n", hres);
108 return FALSE;
111 hres = IActiveScriptProperty_SetProperty(script_prop, property, NULL, val);
112 IActiveScriptProperty_Release(script_prop);
113 if(FAILED(hres)) {
114 WARN("SetProperty(%x) failed: %08x\n", property, hres);
115 return FALSE;
118 return TRUE;
121 static BOOL init_script_engine(ScriptHost *script_host)
123 compat_mode_t compat_mode;
124 IObjectSafety *safety;
125 SCRIPTSTATE state;
126 DWORD supported_opts=0, enabled_opts=0, script_mode;
127 VARIANT var;
128 HRESULT hres;
130 hres = IActiveScript_QueryInterface(script_host->script, &IID_IActiveScriptParse, (void**)&script_host->parse);
131 if(FAILED(hres)) {
132 WARN("Could not get IActiveScriptParse: %08x\n", hres);
133 return FALSE;
136 hres = IActiveScript_QueryInterface(script_host->script, &IID_IObjectSafety, (void**)&safety);
137 if(FAILED(hres)) {
138 FIXME("Could not get IObjectSafety: %08x\n", hres);
139 return FALSE;
142 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported_opts, &enabled_opts);
143 if(FAILED(hres)) {
144 FIXME("GetInterfaceSafetyOptions failed: %08x\n", hres);
145 }else if(!(supported_opts & INTERFACE_USES_DISPEX)) {
146 FIXME("INTERFACE_USES_DISPEX is not supported\n");
147 }else {
148 hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse,
149 INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER,
150 INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER);
151 if(FAILED(hres))
152 FIXME("SetInterfaceSafetyOptions failed: %08x\n", hres);
155 IObjectSafety_Release(safety);
156 if(FAILED(hres))
157 return FALSE;
159 compat_mode = lock_document_mode(script_host->window->doc);
160 script_mode = compat_mode < COMPAT_MODE_IE8 ? SCRIPTLANGUAGEVERSION_5_7 : SCRIPTLANGUAGEVERSION_5_8;
161 if(IsEqualGUID(&script_host->guid, &CLSID_JScript)) {
162 if(compat_mode >= COMPAT_MODE_IE9)
163 script_mode = SCRIPTLANGUAGEVERSION_ES5;
164 script_mode |= SCRIPTLANGUAGEVERSION_HTML;
166 V_VT(&var) = VT_I4;
167 V_I4(&var) = script_mode;
168 if(!set_script_prop(script_host, SCRIPTPROP_INVOKEVERSIONING, &var) && (script_mode & SCRIPTLANGUAGEVERSION_HTML)) {
169 /* If this failed, we're most likely using native jscript. */
170 WARN("Failed to set script mode to HTML version.\n");
171 V_I4(&var) = compat_mode < COMPAT_MODE_IE8 ? SCRIPTLANGUAGEVERSION_5_7 : SCRIPTLANGUAGEVERSION_5_8;
172 set_script_prop(script_host, SCRIPTPROP_INVOKEVERSIONING, &var);
175 V_VT(&var) = VT_BOOL;
176 V_BOOL(&var) = VARIANT_TRUE;
177 set_script_prop(script_host, SCRIPTPROP_HACK_TRIDENTEVENTSINK, &var);
179 hres = IActiveScriptParse_InitNew(script_host->parse);
180 if(FAILED(hres)) {
181 WARN("InitNew failed: %08x\n", hres);
182 return FALSE;
185 hres = IActiveScript_SetScriptSite(script_host->script, &script_host->IActiveScriptSite_iface);
186 if(FAILED(hres)) {
187 WARN("SetScriptSite failed: %08x\n", hres);
188 IActiveScript_Close(script_host->script);
189 return FALSE;
192 hres = IActiveScript_GetScriptState(script_host->script, &state);
193 if(FAILED(hres))
194 WARN("GetScriptState failed: %08x\n", hres);
195 else if(state != SCRIPTSTATE_INITIALIZED)
196 FIXME("state = %x\n", state);
198 hres = IActiveScript_SetScriptState(script_host->script, SCRIPTSTATE_STARTED);
199 if(FAILED(hres)) {
200 WARN("Starting script failed: %08x\n", hres);
201 return FALSE;
204 hres = IActiveScript_AddNamedItem(script_host->script, windowW,
205 SCRIPTITEM_ISVISIBLE|SCRIPTITEM_ISSOURCE|SCRIPTITEM_GLOBALMEMBERS);
206 if(SUCCEEDED(hres)) {
207 V_VT(&var) = VT_BOOL;
208 V_BOOL(&var) = VARIANT_TRUE;
209 set_script_prop(script_host, SCRIPTPROP_ABBREVIATE_GLOBALNAME_RESOLUTION, &var);
210 }else {
211 WARN("AddNamedItem failed: %08x\n", hres);
214 hres = IActiveScript_QueryInterface(script_host->script, &IID_IActiveScriptParseProcedure2,
215 (void**)&script_host->parse_proc);
216 if(FAILED(hres)) {
217 /* FIXME: QI for IActiveScriptParseProcedure */
218 WARN("Could not get IActiveScriptParseProcedure iface: %08x\n", hres);
221 return TRUE;
224 static void release_script_engine(ScriptHost *This)
226 if(!This->script)
227 return;
229 switch(This->script_state) {
230 case SCRIPTSTATE_CONNECTED:
231 IActiveScript_SetScriptState(This->script, SCRIPTSTATE_DISCONNECTED);
233 case SCRIPTSTATE_STARTED:
234 case SCRIPTSTATE_DISCONNECTED:
235 case SCRIPTSTATE_INITIALIZED:
236 IActiveScript_Close(This->script);
238 default:
239 if(This->parse_proc) {
240 IActiveScriptParseProcedure2_Release(This->parse_proc);
241 This->parse_proc = NULL;
244 if(This->parse) {
245 IActiveScriptParse_Release(This->parse);
246 This->parse = NULL;
250 IActiveScript_Release(This->script);
251 This->script = NULL;
252 This->script_state = SCRIPTSTATE_UNINITIALIZED;
255 void connect_scripts(HTMLInnerWindow *window)
257 ScriptHost *iter;
259 LIST_FOR_EACH_ENTRY(iter, &window->script_hosts, ScriptHost, entry) {
260 if(iter->script_state == SCRIPTSTATE_STARTED)
261 IActiveScript_SetScriptState(iter->script, SCRIPTSTATE_CONNECTED);
265 static inline ScriptHost *impl_from_IActiveScriptSite(IActiveScriptSite *iface)
267 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSite_iface);
270 static HRESULT WINAPI ActiveScriptSite_QueryInterface(IActiveScriptSite *iface, REFIID riid, void **ppv)
272 ScriptHost *This = impl_from_IActiveScriptSite(iface);
274 *ppv = NULL;
276 if(IsEqualGUID(&IID_IUnknown, riid)) {
277 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
278 *ppv = &This->IActiveScriptSite_iface;
279 }else if(IsEqualGUID(&IID_IActiveScriptSite, riid)) {
280 TRACE("(%p)->(IID_IActiveScriptSite %p)\n", This, ppv);
281 *ppv = &This->IActiveScriptSite_iface;
282 }else if(IsEqualGUID(&IID_IActiveScriptSiteInterruptPoll, riid)) {
283 TRACE("(%p)->(IID_IActiveScriptSiteInterruprtPoll %p)\n", This, ppv);
284 *ppv = &This->IActiveScriptSiteInterruptPoll_iface;
285 }else if(IsEqualGUID(&IID_IActiveScriptSiteWindow, riid)) {
286 TRACE("(%p)->(IID_IActiveScriptSiteWindow %p)\n", This, ppv);
287 *ppv = &This->IActiveScriptSiteWindow_iface;
288 }else if(IsEqualGUID(&IID_IActiveScriptSiteUIControl, riid)) {
289 TRACE("(%p)->(IID_IActiveScriptSiteUIControl %p)\n", This, ppv);
290 *ppv = &This->IActiveScriptSiteUIControl_iface;
291 }else if(IsEqualGUID(&IID_IActiveScriptSiteDebug, riid)) {
292 TRACE("(%p)->(IID_IActiveScriptSiteDebug %p)\n", This, ppv);
293 *ppv = &This->IActiveScriptSiteDebug_iface;
294 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
295 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
296 *ppv = &This->IServiceProvider_iface;
297 }else if(IsEqualGUID(&IID_ICanHandleException, riid)) {
298 TRACE("(%p)->(IID_ICanHandleException not supported %p)\n", This, ppv);
299 return E_NOINTERFACE;
300 }else {
301 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
302 return E_NOINTERFACE;
305 IUnknown_AddRef((IUnknown*)*ppv);
306 return S_OK;
309 static ULONG WINAPI ActiveScriptSite_AddRef(IActiveScriptSite *iface)
311 ScriptHost *This = impl_from_IActiveScriptSite(iface);
312 LONG ref = InterlockedIncrement(&This->ref);
314 TRACE("(%p) ref=%d\n", This, ref);
316 return ref;
319 static ULONG WINAPI ActiveScriptSite_Release(IActiveScriptSite *iface)
321 ScriptHost *This = impl_from_IActiveScriptSite(iface);
322 LONG ref = InterlockedDecrement(&This->ref);
324 TRACE("(%p) ref=%d\n", This, ref);
326 if(!ref) {
327 release_script_engine(This);
328 if(This->window)
329 list_remove(&This->entry);
330 heap_free(This);
333 return ref;
336 static HRESULT WINAPI ActiveScriptSite_GetLCID(IActiveScriptSite *iface, LCID *plcid)
338 ScriptHost *This = impl_from_IActiveScriptSite(iface);
340 TRACE("(%p)->(%p)\n", This, plcid);
342 *plcid = GetUserDefaultLCID();
343 return S_OK;
346 static HRESULT WINAPI ActiveScriptSite_GetItemInfo(IActiveScriptSite *iface, LPCOLESTR pstrName,
347 DWORD dwReturnMask, IUnknown **ppiunkItem, ITypeInfo **ppti)
349 ScriptHost *This = impl_from_IActiveScriptSite(iface);
351 TRACE("(%p)->(%s %x %p %p)\n", This, debugstr_w(pstrName), dwReturnMask, ppiunkItem, ppti);
353 if(dwReturnMask != SCRIPTINFO_IUNKNOWN) {
354 FIXME("Unsupported mask %x\n", dwReturnMask);
355 return E_NOTIMPL;
358 *ppiunkItem = NULL;
360 if(wcscmp(pstrName, windowW))
361 return DISP_E_MEMBERNOTFOUND;
363 if(!This->window)
364 return E_FAIL;
366 /* FIXME: Return proxy object */
367 *ppiunkItem = (IUnknown*)&This->window->base.IHTMLWindow2_iface;
368 IUnknown_AddRef(*ppiunkItem);
370 return S_OK;
373 static HRESULT WINAPI ActiveScriptSite_GetDocVersionString(IActiveScriptSite *iface, BSTR *pbstrVersion)
375 ScriptHost *This = impl_from_IActiveScriptSite(iface);
376 FIXME("(%p)->(%p)\n", This, pbstrVersion);
377 return E_NOTIMPL;
380 static HRESULT WINAPI ActiveScriptSite_OnScriptTerminate(IActiveScriptSite *iface,
381 const VARIANT *pvarResult, const EXCEPINFO *pexcepinfo)
383 ScriptHost *This = impl_from_IActiveScriptSite(iface);
384 FIXME("(%p)->(%p %p)\n", This, pvarResult, pexcepinfo);
385 return E_NOTIMPL;
388 static HRESULT WINAPI ActiveScriptSite_OnStateChange(IActiveScriptSite *iface, SCRIPTSTATE ssScriptState)
390 ScriptHost *This = impl_from_IActiveScriptSite(iface);
392 TRACE("(%p)->(%x)\n", This, ssScriptState);
394 This->script_state = ssScriptState;
395 return S_OK;
398 static HRESULT WINAPI ActiveScriptSite_OnScriptError(IActiveScriptSite *iface, IActiveScriptError *pscripterror)
400 ScriptHost *This = impl_from_IActiveScriptSite(iface);
401 FIXME("(%p)->(%p)\n", This, pscripterror);
402 return E_NOTIMPL;
405 static HRESULT WINAPI ActiveScriptSite_OnEnterScript(IActiveScriptSite *iface)
407 ScriptHost *This = impl_from_IActiveScriptSite(iface);
409 TRACE("(%p)->()\n", This);
411 return S_OK;
414 static HRESULT WINAPI ActiveScriptSite_OnLeaveScript(IActiveScriptSite *iface)
416 ScriptHost *This = impl_from_IActiveScriptSite(iface);
418 TRACE("(%p)->()\n", This);
420 return S_OK;
423 static const IActiveScriptSiteVtbl ActiveScriptSiteVtbl = {
424 ActiveScriptSite_QueryInterface,
425 ActiveScriptSite_AddRef,
426 ActiveScriptSite_Release,
427 ActiveScriptSite_GetLCID,
428 ActiveScriptSite_GetItemInfo,
429 ActiveScriptSite_GetDocVersionString,
430 ActiveScriptSite_OnScriptTerminate,
431 ActiveScriptSite_OnStateChange,
432 ActiveScriptSite_OnScriptError,
433 ActiveScriptSite_OnEnterScript,
434 ActiveScriptSite_OnLeaveScript
437 static inline ScriptHost *impl_from_IActiveScriptSiteInterruptPoll(IActiveScriptSiteInterruptPoll *iface)
439 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSiteInterruptPoll_iface);
442 static HRESULT WINAPI ActiveScriptSiteInterruptPoll_QueryInterface(IActiveScriptSiteInterruptPoll *iface,
443 REFIID riid, void **ppv)
445 ScriptHost *This = impl_from_IActiveScriptSiteInterruptPoll(iface);
446 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
449 static ULONG WINAPI ActiveScriptSiteInterruptPoll_AddRef(IActiveScriptSiteInterruptPoll *iface)
451 ScriptHost *This = impl_from_IActiveScriptSiteInterruptPoll(iface);
452 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
455 static ULONG WINAPI ActiveScriptSiteInterruptPoll_Release(IActiveScriptSiteInterruptPoll *iface)
457 ScriptHost *This = impl_from_IActiveScriptSiteInterruptPoll(iface);
458 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
461 static HRESULT WINAPI ActiveScriptSiteInterruptPoll_QueryContinue(IActiveScriptSiteInterruptPoll *iface)
463 ScriptHost *This = impl_from_IActiveScriptSiteInterruptPoll(iface);
465 TRACE("(%p)\n", This);
467 return S_OK;
470 static const IActiveScriptSiteInterruptPollVtbl ActiveScriptSiteInterruptPollVtbl = {
471 ActiveScriptSiteInterruptPoll_QueryInterface,
472 ActiveScriptSiteInterruptPoll_AddRef,
473 ActiveScriptSiteInterruptPoll_Release,
474 ActiveScriptSiteInterruptPoll_QueryContinue
477 static inline ScriptHost *impl_from_IActiveScriptSiteWindow(IActiveScriptSiteWindow *iface)
479 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSiteWindow_iface);
482 static HRESULT WINAPI ActiveScriptSiteWindow_QueryInterface(IActiveScriptSiteWindow *iface,
483 REFIID riid, void **ppv)
485 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
486 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
489 static ULONG WINAPI ActiveScriptSiteWindow_AddRef(IActiveScriptSiteWindow *iface)
491 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
492 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
495 static ULONG WINAPI ActiveScriptSiteWindow_Release(IActiveScriptSiteWindow *iface)
497 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
498 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
501 static HRESULT WINAPI ActiveScriptSiteWindow_GetWindow(IActiveScriptSiteWindow *iface, HWND *phwnd)
503 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
505 TRACE("(%p)->(%p)\n", This, phwnd);
507 if(!This->window || !This->window->base.outer_window)
508 return E_UNEXPECTED;
510 *phwnd = This->window->base.outer_window->browser->doc->hwnd;
511 return S_OK;
514 static HRESULT WINAPI ActiveScriptSiteWindow_EnableModeless(IActiveScriptSiteWindow *iface, BOOL fEnable)
516 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
517 FIXME("(%p)->(%x)\n", This, fEnable);
518 return S_OK;
521 static const IActiveScriptSiteWindowVtbl ActiveScriptSiteWindowVtbl = {
522 ActiveScriptSiteWindow_QueryInterface,
523 ActiveScriptSiteWindow_AddRef,
524 ActiveScriptSiteWindow_Release,
525 ActiveScriptSiteWindow_GetWindow,
526 ActiveScriptSiteWindow_EnableModeless
529 static inline ScriptHost *impl_from_IActiveScriptSiteUIControl(IActiveScriptSiteUIControl *iface)
531 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSiteUIControl_iface);
534 static HRESULT WINAPI ActiveScriptSiteUIControl_QueryInterface(IActiveScriptSiteUIControl *iface, REFIID riid, void **ppv)
536 ScriptHost *This = impl_from_IActiveScriptSiteUIControl(iface);
537 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
540 static ULONG WINAPI ActiveScriptSiteUIControl_AddRef(IActiveScriptSiteUIControl *iface)
542 ScriptHost *This = impl_from_IActiveScriptSiteUIControl(iface);
543 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
546 static ULONG WINAPI ActiveScriptSiteUIControl_Release(IActiveScriptSiteUIControl *iface)
548 ScriptHost *This = impl_from_IActiveScriptSiteUIControl(iface);
549 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
552 static HRESULT WINAPI ActiveScriptSiteUIControl_GetUIBehavior(IActiveScriptSiteUIControl *iface, SCRIPTUICITEM UicItem,
553 SCRIPTUICHANDLING *pUicHandling)
555 ScriptHost *This = impl_from_IActiveScriptSiteUIControl(iface);
557 WARN("(%p)->(%d %p) semi-stub\n", This, UicItem, pUicHandling);
559 *pUicHandling = SCRIPTUICHANDLING_ALLOW;
560 return S_OK;
563 static const IActiveScriptSiteUIControlVtbl ActiveScriptSiteUIControlVtbl = {
564 ActiveScriptSiteUIControl_QueryInterface,
565 ActiveScriptSiteUIControl_AddRef,
566 ActiveScriptSiteUIControl_Release,
567 ActiveScriptSiteUIControl_GetUIBehavior
570 static inline ScriptHost *impl_from_IActiveScriptSiteDebug(IActiveScriptSiteDebug *iface)
572 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSiteDebug_iface);
575 static HRESULT WINAPI ActiveScriptSiteDebug_QueryInterface(IActiveScriptSiteDebug *iface,
576 REFIID riid, void **ppv)
578 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
579 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
582 static ULONG WINAPI ActiveScriptSiteDebug_AddRef(IActiveScriptSiteDebug *iface)
584 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
585 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
588 static ULONG WINAPI ActiveScriptSiteDebug_Release(IActiveScriptSiteDebug *iface)
590 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
591 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
594 static HRESULT WINAPI ActiveScriptSiteDebug_GetDocumentContextFromPosition(IActiveScriptSiteDebug *iface,
595 CTXARG_T dwSourceContext, ULONG uCharacterOffset, ULONG uNumChars, IDebugDocumentContext **ppsc)
597 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
598 FIXME("(%p)->(%s %u %u %p)\n", This, wine_dbgstr_longlong(dwSourceContext), uCharacterOffset,
599 uNumChars, ppsc);
600 return E_NOTIMPL;
603 static HRESULT WINAPI ActiveScriptSiteDebug_GetApplication(IActiveScriptSiteDebug *iface, IDebugApplication **ppda)
605 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
606 FIXME("(%p)->(%p)\n", This, ppda);
607 return E_NOTIMPL;
610 static HRESULT WINAPI ActiveScriptSiteDebug_GetRootApplicationNode(IActiveScriptSiteDebug *iface,
611 IDebugApplicationNode **ppdanRoot)
613 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
614 FIXME("(%p)->(%p)\n", This, ppdanRoot);
615 return E_NOTIMPL;
618 static HRESULT WINAPI ActiveScriptSiteDebug_OnScriptErrorDebug(IActiveScriptSiteDebug *iface,
619 IActiveScriptErrorDebug *pErrorDebug, BOOL *pfEnterDebugger, BOOL *pfCallOnScriptErrorWhenContinuing)
621 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
622 FIXME("(%p)->(%p %p %p)\n", This, pErrorDebug, pfEnterDebugger, pfCallOnScriptErrorWhenContinuing);
623 return E_NOTIMPL;
626 static const IActiveScriptSiteDebugVtbl ActiveScriptSiteDebugVtbl = {
627 ActiveScriptSiteDebug_QueryInterface,
628 ActiveScriptSiteDebug_AddRef,
629 ActiveScriptSiteDebug_Release,
630 ActiveScriptSiteDebug_GetDocumentContextFromPosition,
631 ActiveScriptSiteDebug_GetApplication,
632 ActiveScriptSiteDebug_GetRootApplicationNode,
633 ActiveScriptSiteDebug_OnScriptErrorDebug
636 static inline ScriptHost *impl_from_IServiceProvider(IServiceProvider *iface)
638 return CONTAINING_RECORD(iface, ScriptHost, IServiceProvider_iface);
641 static HRESULT WINAPI ASServiceProvider_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
643 ScriptHost *This = impl_from_IServiceProvider(iface);
644 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
647 static ULONG WINAPI ASServiceProvider_AddRef(IServiceProvider *iface)
649 ScriptHost *This = impl_from_IServiceProvider(iface);
650 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
653 static ULONG WINAPI ASServiceProvider_Release(IServiceProvider *iface)
655 ScriptHost *This = impl_from_IServiceProvider(iface);
656 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
659 static HRESULT WINAPI ASServiceProvider_QueryService(IServiceProvider *iface, REFGUID guidService,
660 REFIID riid, void **ppv)
662 ScriptHost *This = impl_from_IServiceProvider(iface);
664 if(IsEqualGUID(&SID_SInternetHostSecurityManager, guidService)) {
665 TRACE("(%p)->(SID_SInternetHostSecurityManager)\n", This);
667 if(!This->window || !This->window->doc)
668 return E_NOINTERFACE;
670 return IInternetHostSecurityManager_QueryInterface(&This->window->doc->IInternetHostSecurityManager_iface,
671 riid, ppv);
674 if(IsEqualGUID(&SID_SContainerDispatch, guidService)) {
675 TRACE("(%p)->(SID_SContainerDispatch)\n", This);
677 if(!This->window || !This->window->doc)
678 return E_NOINTERFACE;
680 return IHTMLDocument2_QueryInterface(&This->window->doc->basedoc.IHTMLDocument2_iface, riid, ppv);
683 FIXME("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
684 return E_NOINTERFACE;
687 static const IServiceProviderVtbl ASServiceProviderVtbl = {
688 ASServiceProvider_QueryInterface,
689 ASServiceProvider_AddRef,
690 ASServiceProvider_Release,
691 ASServiceProvider_QueryService
694 static ScriptHost *create_script_host(HTMLInnerWindow *window, const GUID *guid)
696 ScriptHost *ret;
697 HRESULT hres;
699 ret = heap_alloc_zero(sizeof(*ret));
700 if(!ret)
701 return NULL;
703 ret->IActiveScriptSite_iface.lpVtbl = &ActiveScriptSiteVtbl;
704 ret->IActiveScriptSiteInterruptPoll_iface.lpVtbl = &ActiveScriptSiteInterruptPollVtbl;
705 ret->IActiveScriptSiteWindow_iface.lpVtbl = &ActiveScriptSiteWindowVtbl;
706 ret->IActiveScriptSiteUIControl_iface.lpVtbl = &ActiveScriptSiteUIControlVtbl;
707 ret->IActiveScriptSiteDebug_iface.lpVtbl = &ActiveScriptSiteDebugVtbl;
708 ret->IServiceProvider_iface.lpVtbl = &ASServiceProviderVtbl;
709 ret->ref = 1;
710 ret->window = window;
711 ret->script_state = SCRIPTSTATE_UNINITIALIZED;
713 ret->guid = *guid;
714 list_add_tail(&window->script_hosts, &ret->entry);
716 hres = CoCreateInstance(&ret->guid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
717 &IID_IActiveScript, (void**)&ret->script);
718 if(FAILED(hres))
719 WARN("Could not load script engine: %08x\n", hres);
720 else if(!init_script_engine(ret))
721 release_script_engine(ret);
723 return ret;
726 static void dispatch_script_readystatechange_event(HTMLScriptElement *script)
728 DOMEvent *event;
729 HRESULT hres;
731 hres = create_document_event(script->element.node.doc, EVENTID_READYSTATECHANGE, &event);
732 if(FAILED(hres))
733 return;
735 dispatch_event(&script->element.node.event_target, event);
736 IDOMEvent_Release(&event->IDOMEvent_iface);
739 typedef struct {
740 task_t header;
741 HTMLScriptElement *elem;
742 } fire_readystatechange_task_t;
744 static void fire_readystatechange_proc(task_t *_task)
746 fire_readystatechange_task_t *task = (fire_readystatechange_task_t*)_task;
748 if(!task->elem->pending_readystatechange_event)
749 return;
751 task->elem->pending_readystatechange_event = FALSE;
752 dispatch_script_readystatechange_event(task->elem);
755 static void fire_readystatechange_task_destr(task_t *_task)
757 fire_readystatechange_task_t *task = (fire_readystatechange_task_t*)_task;
759 IHTMLScriptElement_Release(&task->elem->IHTMLScriptElement_iface);
762 static void set_script_elem_readystate(HTMLScriptElement *script_elem, READYSTATE readystate)
764 script_elem->readystate = readystate;
766 if(readystate != READYSTATE_INTERACTIVE) {
767 if(!script_elem->element.node.doc->window->parser_callback_cnt) {
768 fire_readystatechange_task_t *task;
769 HRESULT hres;
771 if(script_elem->pending_readystatechange_event)
772 return;
774 task = heap_alloc(sizeof(*task));
775 if(!task)
776 return;
778 IHTMLScriptElement_AddRef(&script_elem->IHTMLScriptElement_iface);
779 task->elem = script_elem;
781 hres = push_task(&task->header, fire_readystatechange_proc, fire_readystatechange_task_destr,
782 script_elem->element.node.doc->window->task_magic);
783 if(SUCCEEDED(hres))
784 script_elem->pending_readystatechange_event = TRUE;
785 }else {
786 script_elem->pending_readystatechange_event = FALSE;
787 dispatch_script_readystatechange_event(script_elem);
792 static void parse_elem_text(ScriptHost *script_host, HTMLScriptElement *script_elem, LPCWSTR text)
794 EXCEPINFO excepinfo;
795 VARIANT var;
796 HRESULT hres;
798 TRACE("%s\n", debugstr_w(text));
800 VariantInit(&var);
801 memset(&excepinfo, 0, sizeof(excepinfo));
802 TRACE(">>>\n");
803 hres = IActiveScriptParse_ParseScriptText(script_host->parse, text, windowW, NULL, script_endW,
804 0, 0, SCRIPTTEXT_ISVISIBLE|SCRIPTTEXT_HOSTMANAGESSOURCE,
805 &var, &excepinfo);
806 if(SUCCEEDED(hres))
807 TRACE("<<<\n");
808 else
809 WARN("<<< %08x\n", hres);
812 typedef struct {
813 BSCallback bsc;
815 HTMLScriptElement *script_elem;
816 DWORD scheme;
818 DWORD size;
819 char *buf;
820 } ScriptBSC;
822 static HRESULT get_binding_text(ScriptBSC *bsc, WCHAR **ret)
824 UINT cp = CP_UTF8;
825 WCHAR *text;
827 if(!bsc->bsc.read) {
828 text = heap_alloc(sizeof(WCHAR));
829 if(!text)
830 return E_OUTOFMEMORY;
831 *text = 0;
832 *ret = text;
833 return S_OK;
836 switch(bsc->bsc.bom) {
837 case BOM_UTF16:
838 if(bsc->bsc.read % sizeof(WCHAR)) {
839 FIXME("The buffer is not a valid utf16 string\n");
840 return E_FAIL;
843 text = heap_alloc(bsc->bsc.read+sizeof(WCHAR));
844 if(!text)
845 return E_OUTOFMEMORY;
847 memcpy(text, bsc->buf, bsc->bsc.read);
848 text[bsc->bsc.read/sizeof(WCHAR)] = 0;
849 break;
851 default:
852 /* FIXME: Try to use charset from HTTP headers first */
853 cp = get_document_charset(bsc->script_elem->element.node.doc);
854 /* fall through */
855 case BOM_UTF8: {
856 DWORD len;
858 len = MultiByteToWideChar(cp, 0, bsc->buf, bsc->bsc.read, NULL, 0);
859 text = heap_alloc((len+1)*sizeof(WCHAR));
860 if(!text)
861 return E_OUTOFMEMORY;
863 MultiByteToWideChar(cp, 0, bsc->buf, bsc->bsc.read, text, len);
864 text[len] = 0;
868 *ret = text;
869 return S_OK;
872 static void script_file_available(ScriptBSC *bsc)
874 HTMLScriptElement *script_elem = bsc->script_elem;
875 HTMLInnerWindow *window = bsc->bsc.window;
876 ScriptHost *script_host;
877 nsIDOMNode *parent;
878 nsresult nsres;
879 HRESULT hres;
881 assert(window != NULL);
883 hres = get_binding_text(bsc, &script_elem->src_text);
884 if(FAILED(hres))
885 return;
887 script_host = get_elem_script_host(window, script_elem);
888 if(!script_host)
889 return;
891 if(window->parser_callback_cnt) {
892 script_queue_entry_t *queue;
894 TRACE("Adding to queue\n");
896 queue = heap_alloc(sizeof(*queue));
897 if(!queue)
898 return;
900 IHTMLScriptElement_AddRef(&script_elem->IHTMLScriptElement_iface);
901 queue->script = script_elem;
903 list_add_tail(&window->script_queue, &queue->entry);
904 return;
907 nsres = nsIDOMElement_GetParentNode(script_elem->element.dom_element, &parent);
908 if(NS_FAILED(nsres) || !parent) {
909 TRACE("No parent, not executing\n");
910 script_elem->parse_on_bind = TRUE;
911 return;
914 nsIDOMNode_Release(parent);
916 script_host = get_elem_script_host(window, script_elem);
917 if(!script_host)
918 return;
920 script_elem->parsed = TRUE;
921 if(script_host->parse)
922 parse_elem_text(script_host, script_elem, script_elem->src_text);
925 static inline ScriptBSC *impl_from_BSCallback(BSCallback *iface)
927 return CONTAINING_RECORD(iface, ScriptBSC, bsc);
930 static void ScriptBSC_destroy(BSCallback *bsc)
932 ScriptBSC *This = impl_from_BSCallback(bsc);
934 if(This->script_elem) {
935 IHTMLScriptElement_Release(&This->script_elem->IHTMLScriptElement_iface);
936 This->script_elem = NULL;
939 heap_free(This->buf);
940 heap_free(This);
943 static HRESULT ScriptBSC_init_bindinfo(BSCallback *bsc)
945 return S_OK;
948 static HRESULT ScriptBSC_start_binding(BSCallback *bsc)
950 ScriptBSC *This = impl_from_BSCallback(bsc);
952 This->script_elem->binding = &This->bsc;
954 /* FIXME: We should find a better to decide if 'loading' state is supposed to be used by the protocol. */
955 if(This->scheme == URL_SCHEME_HTTPS || This->scheme == URL_SCHEME_HTTP)
956 set_script_elem_readystate(This->script_elem, READYSTATE_LOADING);
958 return S_OK;
961 static HRESULT ScriptBSC_stop_binding(BSCallback *bsc, HRESULT result)
963 ScriptBSC *This = impl_from_BSCallback(bsc);
965 if(SUCCEEDED(result) && !This->script_elem)
966 result = E_UNEXPECTED;
968 assert(FAILED(result) || This->script_elem->binding == &This->bsc);
969 This->script_elem->binding = NULL;
971 if(This->script_elem->readystate == READYSTATE_LOADING)
972 set_script_elem_readystate(This->script_elem, READYSTATE_LOADED);
974 if(SUCCEEDED(result)) {
975 script_file_available(This);
976 }else {
977 FIXME("binding failed %08x\n", result);
978 heap_free(This->buf);
979 This->buf = NULL;
980 This->size = 0;
983 IHTMLScriptElement_Release(&This->script_elem->IHTMLScriptElement_iface);
984 This->script_elem = NULL;
985 return S_OK;
988 static HRESULT ScriptBSC_read_data(BSCallback *bsc, IStream *stream)
990 ScriptBSC *This = impl_from_BSCallback(bsc);
991 DWORD read;
992 HRESULT hres;
994 if(!This->buf) {
995 This->buf = heap_alloc(128);
996 if(!This->buf)
997 return E_OUTOFMEMORY;
998 This->size = 128;
1001 do {
1002 if(This->bsc.read >= This->size) {
1003 void *new_buf;
1004 new_buf = heap_realloc(This->buf, This->size << 1);
1005 if(!new_buf)
1006 return E_OUTOFMEMORY;
1007 This->size <<= 1;
1008 This->buf = new_buf;
1011 hres = read_stream(&This->bsc, stream, This->buf+This->bsc.read, This->size-This->bsc.read, &read);
1012 }while(hres == S_OK);
1014 return S_OK;
1017 static HRESULT ScriptBSC_on_progress(BSCallback *bsc, ULONG status_code, LPCWSTR status_text)
1019 return S_OK;
1022 static HRESULT ScriptBSC_on_response(BSCallback *bsc, DWORD response_code,
1023 LPCWSTR response_headers)
1025 return S_OK;
1028 static HRESULT ScriptBSC_beginning_transaction(BSCallback *bsc, WCHAR **additional_headers)
1030 return S_FALSE;
1033 static const BSCallbackVtbl ScriptBSCVtbl = {
1034 ScriptBSC_destroy,
1035 ScriptBSC_init_bindinfo,
1036 ScriptBSC_start_binding,
1037 ScriptBSC_stop_binding,
1038 ScriptBSC_read_data,
1039 ScriptBSC_on_progress,
1040 ScriptBSC_on_response,
1041 ScriptBSC_beginning_transaction
1045 HRESULT load_script(HTMLScriptElement *script_elem, const WCHAR *src, BOOL async)
1047 HTMLInnerWindow *window;
1048 ScriptBSC *bsc;
1049 IMoniker *mon;
1050 IUri *uri;
1051 HRESULT hres;
1053 static const WCHAR wine_schemaW[] = {'w','i','n','e',':'};
1055 if(lstrlenW(src) > ARRAY_SIZE(wine_schemaW) && !memcmp(src, wine_schemaW, sizeof(wine_schemaW)))
1056 src += ARRAY_SIZE(wine_schemaW);
1058 TRACE("(%p %s %x)\n", script_elem, debugstr_w(src), async);
1060 if(!script_elem->element.node.doc || !(window = script_elem->element.node.doc->window)) {
1061 ERR("no window\n");
1062 return E_UNEXPECTED;
1065 hres = create_uri(src, 0, &uri);
1066 if(FAILED(hres))
1067 return hres;
1069 hres = CreateURLMonikerEx2(NULL, uri, &mon, URL_MK_UNIFORM);
1070 if(FAILED(hres)) {
1071 IUri_Release(uri);
1072 return hres;
1075 bsc = heap_alloc_zero(sizeof(*bsc));
1076 if(!bsc) {
1077 IMoniker_Release(mon);
1078 IUri_Release(uri);
1079 return E_OUTOFMEMORY;
1082 init_bscallback(&bsc->bsc, &ScriptBSCVtbl, mon, async ? BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA : 0);
1083 IMoniker_Release(mon);
1085 hres = IUri_GetScheme(uri, &bsc->scheme);
1086 IUri_Release(uri);
1087 if(FAILED(hres))
1088 bsc->scheme = URL_SCHEME_UNKNOWN;
1090 IHTMLScriptElement_AddRef(&script_elem->IHTMLScriptElement_iface);
1091 bsc->script_elem = script_elem;
1093 hres = start_binding(window, &bsc->bsc, NULL);
1095 IBindStatusCallback_Release(&bsc->bsc.IBindStatusCallback_iface);
1096 return hres;
1099 static void parse_inline_script(ScriptHost *script_host, HTMLScriptElement *script_elem)
1101 const PRUnichar *text;
1102 nsAString text_str;
1103 nsresult nsres;
1105 nsAString_Init(&text_str, NULL);
1106 nsres = nsIDOMHTMLScriptElement_GetText(script_elem->nsscript, &text_str);
1107 nsAString_GetData(&text_str, &text);
1109 set_script_elem_readystate(script_elem, READYSTATE_INTERACTIVE);
1111 if(NS_FAILED(nsres)) {
1112 ERR("GetText failed: %08x\n", nsres);
1113 }else if(*text) {
1114 parse_elem_text(script_host, script_elem, text);
1117 nsAString_Finish(&text_str);
1120 static BOOL parse_script_elem(ScriptHost *script_host, HTMLScriptElement *script_elem)
1122 nsAString src_str, event_str;
1123 BOOL is_complete = FALSE;
1124 const PRUnichar *src;
1125 nsresult nsres;
1127 nsAString_Init(&event_str, NULL);
1128 nsres = nsIDOMHTMLScriptElement_GetEvent(script_elem->nsscript, &event_str);
1129 if(NS_SUCCEEDED(nsres)) {
1130 const PRUnichar *event;
1132 nsAString_GetData(&event_str, &event);
1133 if(*event) {
1134 TRACE("deferring event %s script evaluation\n", debugstr_w(event));
1135 nsAString_Finish(&event_str);
1136 return FALSE;
1138 }else {
1139 ERR("GetEvent failed: %08x\n", nsres);
1141 nsAString_Finish(&event_str);
1143 nsAString_Init(&src_str, NULL);
1144 nsres = nsIDOMHTMLScriptElement_GetSrc(script_elem->nsscript, &src_str);
1145 nsAString_GetData(&src_str, &src);
1147 if(NS_FAILED(nsres)) {
1148 ERR("GetSrc failed: %08x\n", nsres);
1149 }else if(*src) {
1150 load_script(script_elem, src, FALSE);
1151 is_complete = script_elem->parsed;
1152 }else {
1153 parse_inline_script(script_host, script_elem);
1154 is_complete = TRUE;
1157 nsAString_Finish(&src_str);
1159 return is_complete;
1162 static GUID get_default_script_guid(HTMLInnerWindow *window)
1164 /* If not specified, we should use very first script host that was created for the page (or JScript if none) */
1165 return list_empty(&window->script_hosts)
1166 ? CLSID_JScript
1167 : LIST_ENTRY(list_head(&window->script_hosts), ScriptHost, entry)->guid;
1170 static BOOL get_guid_from_type(LPCWSTR type, GUID *guid)
1172 static const WCHAR text_javascriptW[] =
1173 {'t','e','x','t','/','j','a','v','a','s','c','r','i','p','t',0};
1174 static const WCHAR text_jscriptW[] =
1175 {'t','e','x','t','/','j','s','c','r','i','p','t',0};
1176 static const WCHAR text_vbscriptW[] =
1177 {'t','e','x','t','/','v','b','s','c','r','i','p','t',0};
1179 /* FIXME: Handle more types */
1180 if(!wcsicmp(type, text_javascriptW) || !wcsicmp(type, text_jscriptW)) {
1181 *guid = CLSID_JScript;
1182 }else if(!wcsicmp(type, text_vbscriptW)) {
1183 *guid = CLSID_VBScript;
1184 }else {
1185 FIXME("Unknown type %s\n", debugstr_w(type));
1186 return FALSE;
1189 return TRUE;
1192 static BOOL get_guid_from_language(LPCWSTR type, GUID *guid)
1194 HRESULT hres;
1196 hres = CLSIDFromProgID(type, guid);
1197 if(FAILED(hres))
1198 return FALSE;
1200 /* FIXME: Check CATID_ActiveScriptParse */
1202 return TRUE;
1205 static BOOL get_script_guid(HTMLInnerWindow *window, nsIDOMHTMLScriptElement *nsscript, GUID *guid)
1207 nsIDOMElement *nselem;
1208 const PRUnichar *language;
1209 nsAString val_str;
1210 BOOL ret = FALSE;
1211 nsresult nsres;
1213 static const PRUnichar languageW[] = {'l','a','n','g','u','a','g','e',0};
1215 nsAString_Init(&val_str, NULL);
1217 nsres = nsIDOMHTMLScriptElement_GetType(nsscript, &val_str);
1218 if(NS_SUCCEEDED(nsres)) {
1219 const PRUnichar *type;
1221 nsAString_GetData(&val_str, &type);
1222 if(*type) {
1223 ret = get_guid_from_type(type, guid);
1224 nsAString_Finish(&val_str);
1225 return ret;
1227 }else {
1228 ERR("GetType failed: %08x\n", nsres);
1231 nsres = nsIDOMHTMLScriptElement_QueryInterface(nsscript, &IID_nsIDOMElement, (void**)&nselem);
1232 assert(nsres == NS_OK);
1234 nsres = get_elem_attr_value(nselem, languageW, &val_str, &language);
1235 nsIDOMElement_Release(nselem);
1236 if(NS_SUCCEEDED(nsres)) {
1237 if(*language) {
1238 ret = get_guid_from_language(language, guid);
1239 }else {
1240 *guid = get_default_script_guid(window);
1241 ret = TRUE;
1243 nsAString_Finish(&val_str);
1246 return ret;
1249 static ScriptHost *get_script_host(HTMLInnerWindow *window, const GUID *guid)
1251 ScriptHost *iter;
1253 LIST_FOR_EACH_ENTRY(iter, &window->script_hosts, ScriptHost, entry) {
1254 if(IsEqualGUID(guid, &iter->guid))
1255 return iter;
1258 return create_script_host(window, guid);
1261 static ScriptHost *get_elem_script_host(HTMLInnerWindow *window, HTMLScriptElement *script_elem)
1263 GUID guid;
1265 if(!get_script_guid(window, script_elem->nsscript, &guid)) {
1266 WARN("Could not find script GUID\n");
1267 return NULL;
1270 if(IsEqualGUID(&CLSID_JScript, &guid) && (!window->doc->browser || window->doc->browser->script_mode != SCRIPTMODE_ACTIVESCRIPT)) {
1271 TRACE("Ignoring JScript\n");
1272 return NULL;
1275 return get_script_host(window, &guid);
1278 void doc_insert_script(HTMLInnerWindow *window, HTMLScriptElement *script_elem, BOOL from_parser)
1280 ScriptHost *script_host;
1281 BOOL is_complete = FALSE;
1283 script_host = get_elem_script_host(window, script_elem);
1284 if(!script_host)
1285 return;
1287 if(script_host->parse) {
1288 if(script_elem->src_text) {
1289 if(from_parser)
1290 set_script_elem_readystate(script_elem, READYSTATE_INTERACTIVE);
1291 script_elem->parsed = TRUE;
1292 parse_elem_text(script_host, script_elem, script_elem->src_text);
1293 is_complete = TRUE;
1294 }else if(!script_elem->binding) {
1295 is_complete = parse_script_elem(script_host, script_elem);
1299 if(is_complete)
1300 set_script_elem_readystate(script_elem, READYSTATE_COMPLETE);
1303 IDispatch *script_parse_event(HTMLInnerWindow *window, LPCWSTR text)
1305 ScriptHost *script_host;
1306 GUID guid;
1307 const WCHAR *ptr;
1308 IDispatch *disp;
1309 HRESULT hres;
1311 static const WCHAR delimiterW[] = {'\"',0};
1313 TRACE("%s\n", debugstr_w(text));
1315 for(ptr = text; iswalnum(*ptr); ptr++);
1316 if(*ptr == ':') {
1317 LPWSTR language;
1318 BOOL b;
1320 language = heap_alloc((ptr-text+1)*sizeof(WCHAR));
1321 if(!language)
1322 return NULL;
1324 memcpy(language, text, (ptr-text)*sizeof(WCHAR));
1325 language[ptr-text] = 0;
1327 b = get_guid_from_language(language, &guid);
1329 heap_free(language);
1331 if(!b) {
1332 WARN("Could not find language\n");
1333 return NULL;
1336 ptr++;
1337 }else {
1338 ptr = text;
1339 guid = get_default_script_guid(window);
1342 if(IsEqualGUID(&CLSID_JScript, &guid)
1343 && (!window->doc->browser || window->doc->browser->script_mode != SCRIPTMODE_ACTIVESCRIPT)) {
1344 TRACE("Ignoring JScript\n");
1345 return NULL;
1348 script_host = get_script_host(window, &guid);
1349 if(!script_host || !script_host->parse_proc)
1350 return NULL;
1352 hres = IActiveScriptParseProcedure2_ParseProcedureText(script_host->parse_proc, ptr, NULL, emptyW,
1353 NULL, NULL, delimiterW, 0 /* FIXME */, 0,
1354 SCRIPTPROC_HOSTMANAGESSOURCE|SCRIPTPROC_IMPLICIT_THIS|SCRIPTPROC_IMPLICIT_PARENTS, &disp);
1355 if(FAILED(hres)) {
1356 WARN("ParseProcedureText failed: %08x\n", hres);
1357 return NULL;
1360 TRACE("ret %p\n", disp);
1361 return disp;
1364 HRESULT exec_script(HTMLInnerWindow *window, const WCHAR *code, const WCHAR *lang, VARIANT *ret)
1366 ScriptHost *script_host;
1367 EXCEPINFO ei;
1368 GUID guid;
1369 HRESULT hres;
1371 static const WCHAR delimW[] = {'"',0};
1373 if(!get_guid_from_language(lang, &guid)) {
1374 WARN("Could not find script GUID\n");
1375 return CO_E_CLASSSTRING;
1378 script_host = get_script_host(window, &guid);
1379 if(!script_host) {
1380 FIXME("No script host\n");
1381 return E_FAIL;
1384 if(!script_host->parse) {
1385 FIXME("script_host->parse == NULL\n");
1386 return E_FAIL;
1389 memset(&ei, 0, sizeof(ei));
1390 TRACE(">>>\n");
1391 hres = IActiveScriptParse_ParseScriptText(script_host->parse, code, NULL, NULL, delimW, 0, 0, SCRIPTTEXT_ISVISIBLE, ret, &ei);
1392 if(SUCCEEDED(hres))
1393 TRACE("<<<\n");
1394 else
1395 WARN("<<< %08x\n", hres);
1397 return hres;
1400 IDispatch *get_script_disp(ScriptHost *script_host)
1402 IDispatch *disp;
1403 HRESULT hres;
1405 if(!script_host->script)
1406 return NULL;
1408 hres = IActiveScript_GetScriptDispatch(script_host->script, windowW, &disp);
1409 if(FAILED(hres))
1410 return NULL;
1412 return disp;
1415 static EventTarget *find_event_target(HTMLDocumentNode *doc, HTMLScriptElement *script_elem)
1417 EventTarget *event_target = NULL;
1418 const PRUnichar *target_id;
1419 nsAString target_id_str;
1420 nsresult nsres;
1421 HRESULT hres;
1423 nsAString_Init(&target_id_str, NULL);
1424 nsres = nsIDOMHTMLScriptElement_GetHtmlFor(script_elem->nsscript, &target_id_str);
1425 if(NS_FAILED(nsres)) {
1426 ERR("GetScriptFor failed: %08x\n", nsres);
1427 nsAString_Finish(&target_id_str);
1428 return NULL;
1431 nsAString_GetData(&target_id_str, &target_id);
1432 if(!*target_id) {
1433 FIXME("Empty for attribute\n");
1434 }else if(!wcscmp(target_id, documentW)) {
1435 event_target = &doc->node.event_target;
1436 htmldoc_addref(&doc->basedoc);
1437 }else if(!wcscmp(target_id, windowW)) {
1438 if(doc->window) {
1439 event_target = &doc->window->event_target;
1440 IDispatchEx_AddRef(&event_target->dispex.IDispatchEx_iface);
1442 }else {
1443 HTMLElement *target_elem;
1445 hres = get_doc_elem_by_id(doc, target_id, &target_elem);
1446 if(SUCCEEDED(hres) && target_elem) {
1447 event_target = &target_elem->node.event_target;
1451 nsAString_Finish(&target_id_str);
1452 return event_target;
1455 static BOOL parse_event_str(WCHAR *event, const WCHAR **args)
1457 WCHAR *ptr;
1459 TRACE("%s\n", debugstr_w(event));
1461 for(ptr = event; iswalnum(*ptr); ptr++);
1462 if(!*ptr) {
1463 *args = NULL;
1464 return TRUE;
1467 if(*ptr != '(')
1468 return FALSE;
1470 *ptr++ = 0;
1471 *args = ptr;
1472 while(iswalnum(*ptr) || iswspace(*ptr) || *ptr == ',')
1473 ptr++;
1475 if(*ptr != ')')
1476 return FALSE;
1478 *ptr++ = 0;
1479 return !*ptr;
1482 static IDispatch *parse_event_elem(HTMLDocumentNode *doc, HTMLScriptElement *script_elem, WCHAR **ret_event)
1484 ScriptHost *script_host;
1485 WCHAR *event = NULL;
1486 const WCHAR *args;
1487 nsAString nsstr;
1488 IDispatch *disp;
1489 nsresult nsres;
1490 HRESULT hres;
1492 if(script_elem->parsed)
1493 return NULL;
1495 script_host = get_elem_script_host(doc->window, script_elem);
1496 if(!script_host || !script_host->parse_proc)
1497 return NULL;
1499 nsAString_Init(&nsstr, NULL);
1500 nsres = nsIDOMHTMLScriptElement_GetEvent(script_elem->nsscript, &nsstr);
1501 if(NS_SUCCEEDED(nsres)) {
1502 const PRUnichar *event_val;
1504 nsAString_GetData(&nsstr, &event_val);
1505 event = heap_strdupW(event_val);
1507 nsAString_Finish(&nsstr);
1508 if(!event)
1509 return NULL;
1511 if(!parse_event_str(event, &args)) {
1512 WARN("parsing %s failed\n", debugstr_w(event));
1513 heap_free(event);
1514 return NULL;
1517 nsAString_Init(&nsstr, NULL);
1518 nsres = nsIDOMHTMLScriptElement_GetText(script_elem->nsscript, &nsstr);
1519 if(NS_SUCCEEDED(nsres)) {
1520 const PRUnichar *text;
1522 nsAString_GetData(&nsstr, &text);
1523 hres = IActiveScriptParseProcedure2_ParseProcedureText(script_host->parse_proc, text, args,
1524 emptyW, NULL, NULL, script_endW, 0, 0,
1525 SCRIPTPROC_HOSTMANAGESSOURCE|SCRIPTPROC_IMPLICIT_THIS|SCRIPTPROC_IMPLICIT_PARENTS, &disp);
1526 if(FAILED(hres))
1527 disp = NULL;
1528 }else {
1529 ERR("GetText failed: %08x\n", nsres);
1530 disp = NULL;
1532 nsAString_Finish(&nsstr);
1533 if(!disp) {
1534 heap_free(event);
1535 return NULL;
1538 *ret_event = event;
1539 return disp;
1542 void bind_event_scripts(HTMLDocumentNode *doc)
1544 HTMLPluginContainer *plugin_container;
1545 nsIDOMHTMLScriptElement *nsscript;
1546 HTMLScriptElement *script_elem;
1547 EventTarget *event_target;
1548 nsIDOMNodeList *node_list;
1549 nsIDOMNode *script_node;
1550 nsAString selector_str;
1551 IDispatch *event_disp;
1552 UINT32 length, i;
1553 WCHAR *event;
1554 nsresult nsres;
1555 HRESULT hres;
1557 static const PRUnichar selectorW[] = {'s','c','r','i','p','t','[','e','v','e','n','t',']',0};
1559 TRACE("%p\n", doc);
1561 if(!doc->nsdoc)
1562 return;
1564 nsAString_InitDepend(&selector_str, selectorW);
1565 nsres = nsIDOMHTMLDocument_QuerySelectorAll(doc->nsdoc, &selector_str, &node_list);
1566 nsAString_Finish(&selector_str);
1567 if(NS_FAILED(nsres)) {
1568 ERR("QuerySelectorAll failed: %08x\n", nsres);
1569 return;
1572 if(!node_list)
1573 return;
1575 nsres = nsIDOMNodeList_GetLength(node_list, &length);
1576 assert(nsres == NS_OK);
1578 for(i=0; i < length; i++) {
1579 nsres = nsIDOMNodeList_Item(node_list, i, &script_node);
1580 if(NS_FAILED(nsres) || !script_node) {
1581 ERR("Item(%d) failed: %08x\n", i, nsres);
1582 continue;
1585 nsres = nsIDOMNode_QueryInterface(script_node, &IID_nsIDOMHTMLScriptElement, (void**)&nsscript);
1586 assert(nsres == NS_OK);
1587 nsIDOMNode_Release(script_node);
1589 hres = script_elem_from_nsscript(nsscript, &script_elem);
1590 if(FAILED(hres))
1591 continue;
1593 event_disp = parse_event_elem(doc, script_elem, &event);
1594 if(event_disp) {
1595 event_target = find_event_target(doc, script_elem);
1596 if(event_target) {
1597 hres = IDispatchEx_QueryInterface(&event_target->dispex.IDispatchEx_iface, &IID_HTMLPluginContainer,
1598 (void**)&plugin_container);
1599 if(SUCCEEDED(hres))
1600 bind_activex_event(doc, plugin_container, event, event_disp);
1601 else
1602 bind_target_event(doc, event_target, event, event_disp);
1604 IDispatchEx_Release(&event_target->dispex.IDispatchEx_iface);
1605 if(plugin_container)
1606 node_release(&plugin_container->element.node);
1609 heap_free(event);
1610 IDispatch_Release(event_disp);
1613 IHTMLScriptElement_Release(&script_elem->IHTMLScriptElement_iface);
1616 nsIDOMNodeList_Release(node_list);
1619 BOOL find_global_prop(HTMLInnerWindow *window, BSTR name, DWORD flags, ScriptHost **ret_host, DISPID *ret_id)
1621 IDispatchEx *dispex;
1622 IDispatch *disp;
1623 ScriptHost *iter;
1624 HRESULT hres;
1626 LIST_FOR_EACH_ENTRY(iter, &window->script_hosts, ScriptHost, entry) {
1627 disp = get_script_disp(iter);
1628 if(!disp)
1629 continue;
1631 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1632 if(SUCCEEDED(hres)) {
1633 hres = IDispatchEx_GetDispID(dispex, name, flags & (~fdexNameEnsure), ret_id);
1634 IDispatchEx_Release(dispex);
1635 }else {
1636 FIXME("No IDispatchEx\n");
1637 hres = E_NOTIMPL;
1640 IDispatch_Release(disp);
1641 if(SUCCEEDED(hres)) {
1642 *ret_host = iter;
1643 return TRUE;
1647 return FALSE;
1650 static BOOL is_jscript_available(void)
1652 static BOOL available, checked;
1654 if(!checked) {
1655 IUnknown *unk;
1656 HRESULT hres = CoGetClassObject(&CLSID_JScript, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void**)&unk);
1658 if(SUCCEEDED(hres)) {
1659 available = TRUE;
1660 IUnknown_Release(unk);
1661 }else {
1662 available = FALSE;
1664 checked = TRUE;
1667 return available;
1670 static BOOL use_gecko_script(IUri *uri)
1672 BSTR display_uri;
1673 DWORD zone;
1674 HRESULT hres;
1676 hres = IUri_GetDisplayUri(uri, &display_uri);
1677 if(FAILED(hres))
1678 return FALSE;
1680 hres = IInternetSecurityManager_MapUrlToZone(get_security_manager(), display_uri, &zone, 0);
1681 SysFreeString(display_uri);
1682 if(FAILED(hres)) {
1683 WARN("Could not map %s to zone: %08x\n", debugstr_w(display_uri), hres);
1684 return TRUE;
1687 TRACE("zone %d\n", zone);
1688 return zone == URLZONE_UNTRUSTED;
1691 void update_browser_script_mode(GeckoBrowser *browser, IUri *uri)
1693 nsIWebBrowserSetup *setup;
1694 nsresult nsres;
1696 if(!is_jscript_available()) {
1697 TRACE("jscript.dll not available\n");
1698 browser->script_mode = SCRIPTMODE_GECKO;
1699 return;
1702 browser->script_mode = use_gecko_script(uri) ? SCRIPTMODE_GECKO : SCRIPTMODE_ACTIVESCRIPT;
1704 nsres = nsIWebBrowser_QueryInterface(browser->webbrowser, &IID_nsIWebBrowserSetup, (void**)&setup);
1705 if(NS_SUCCEEDED(nsres)) {
1706 nsres = nsIWebBrowserSetup_SetProperty(setup, SETUP_ALLOW_JAVASCRIPT,
1707 browser->script_mode == SCRIPTMODE_GECKO);
1709 if(NS_SUCCEEDED(nsres))
1710 nsres = nsIWebBrowserSetup_SetProperty(setup, SETUP_DISABLE_NOSCRIPT, TRUE);
1712 nsIWebBrowserSetup_Release(setup);
1715 if(NS_FAILED(nsres))
1716 ERR("JavaScript setup failed: %08x\n", nsres);
1719 void release_script_hosts(HTMLInnerWindow *window)
1721 script_queue_entry_t *queue_iter;
1722 ScriptHost *iter;
1724 while(!list_empty(&window->script_queue)) {
1725 queue_iter = LIST_ENTRY(list_head(&window->script_queue), script_queue_entry_t, entry);
1727 list_remove(&queue_iter->entry);
1728 IHTMLScriptElement_Release(&queue_iter->script->IHTMLScriptElement_iface);
1729 heap_free(queue_iter);
1732 while(!list_empty(&window->script_hosts)) {
1733 iter = LIST_ENTRY(list_head(&window->script_hosts), ScriptHost, entry);
1735 release_script_engine(iter);
1736 list_remove(&iter->entry);
1737 iter->window = NULL;
1738 IActiveScriptSite_Release(&iter->IActiveScriptSite_iface);