maintainers: Update winemaker section.
[wine.git] / dlls / mshtml / script.c
blob2a47f8a02357851b238396dad5069334d88785c7
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 "config.h"
21 #include <stdarg.h>
22 #include <assert.h>
24 #define COBJMACROS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "ole2.h"
30 #include "activscp.h"
31 #include "activdbg.h"
32 #include "shlwapi.h"
34 #include "wine/debug.h"
36 #include "mshtml_private.h"
37 #include "htmlscript.h"
38 #include "pluginhost.h"
39 #include "htmlevent.h"
40 #include "binding.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
44 #ifdef _WIN64
46 #define CTXARG_T DWORDLONG
47 #define IActiveScriptSiteDebugVtbl IActiveScriptSiteDebug64Vtbl
49 #define IActiveScriptParse_Release IActiveScriptParse64_Release
50 #define IActiveScriptParse_InitNew IActiveScriptParse64_InitNew
51 #define IActiveScriptParse_ParseScriptText IActiveScriptParse64_ParseScriptText
52 #define IActiveScriptParseProcedure2_Release IActiveScriptParseProcedure2_64_Release
53 #define IActiveScriptParseProcedure2_ParseProcedureText IActiveScriptParseProcedure2_64_ParseProcedureText
55 #else
57 #define CTXARG_T DWORD
58 #define IActiveScriptSiteDebugVtbl IActiveScriptSiteDebug32Vtbl
60 #define IActiveScriptParse_Release IActiveScriptParse32_Release
61 #define IActiveScriptParse_InitNew IActiveScriptParse32_InitNew
62 #define IActiveScriptParse_ParseScriptText IActiveScriptParse32_ParseScriptText
63 #define IActiveScriptParseProcedure2_Release IActiveScriptParseProcedure2_32_Release
64 #define IActiveScriptParseProcedure2_ParseProcedureText IActiveScriptParseProcedure2_32_ParseProcedureText
66 #endif
68 static const WCHAR documentW[] = {'d','o','c','u','m','e','n','t',0};
69 static const WCHAR windowW[] = {'w','i','n','d','o','w',0};
70 static const WCHAR script_endW[] = {'<','/','S','C','R','I','P','T','>',0};
71 static const WCHAR emptyW[] = {0};
73 struct ScriptHost {
74 IActiveScriptSite IActiveScriptSite_iface;
75 IActiveScriptSiteInterruptPoll IActiveScriptSiteInterruptPoll_iface;
76 IActiveScriptSiteWindow IActiveScriptSiteWindow_iface;
77 IActiveScriptSiteUIControl IActiveScriptSiteUIControl_iface;
78 IActiveScriptSiteDebug IActiveScriptSiteDebug_iface;
79 IServiceProvider IServiceProvider_iface;
81 LONG ref;
83 IActiveScript *script;
84 IActiveScriptParse *parse;
85 IActiveScriptParseProcedure2 *parse_proc;
87 SCRIPTSTATE script_state;
89 HTMLInnerWindow *window;
91 GUID guid;
92 struct list entry;
95 static void set_script_prop(ScriptHost *script_host, DWORD property, VARIANT *val)
97 IActiveScriptProperty *script_prop;
98 HRESULT hres;
100 hres = IActiveScript_QueryInterface(script_host->script, &IID_IActiveScriptProperty,
101 (void**)&script_prop);
102 if(FAILED(hres)) {
103 WARN("Could not get IActiveScriptProperty iface: %08x\n", hres);
104 return;
107 hres = IActiveScriptProperty_SetProperty(script_prop, property, NULL, val);
108 IActiveScriptProperty_Release(script_prop);
109 if(FAILED(hres))
110 WARN("SetProperty(%x) failed: %08x\n", property, hres);
113 static BOOL init_script_engine(ScriptHost *script_host)
115 IObjectSafety *safety;
116 SCRIPTSTATE state;
117 DWORD supported_opts=0, enabled_opts=0;
118 VARIANT var;
119 HRESULT hres;
121 hres = IActiveScript_QueryInterface(script_host->script, &IID_IActiveScriptParse, (void**)&script_host->parse);
122 if(FAILED(hres)) {
123 WARN("Could not get IActiveScriptHost: %08x\n", hres);
124 return FALSE;
127 hres = IActiveScript_QueryInterface(script_host->script, &IID_IObjectSafety, (void**)&safety);
128 if(FAILED(hres)) {
129 FIXME("Could not get IObjectSafety: %08x\n", hres);
130 return FALSE;
133 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported_opts, &enabled_opts);
134 if(FAILED(hres)) {
135 FIXME("GetInterfaceSafetyOptions failed: %08x\n", hres);
136 }else if(!(supported_opts & INTERFACE_USES_DISPEX)) {
137 FIXME("INTERFACE_USES_DISPEX is not supported\n");
138 }else {
139 hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse,
140 INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER,
141 INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER);
142 if(FAILED(hres))
143 FIXME("SetInterfaceSafetyOptions failed: %08x\n", hres);
146 IObjectSafety_Release(safety);
147 if(FAILED(hres))
148 return FALSE;
150 V_VT(&var) = VT_I4;
151 V_I4(&var) = 1;
152 set_script_prop(script_host, SCRIPTPROP_INVOKEVERSIONING, &var);
154 V_VT(&var) = VT_BOOL;
155 V_BOOL(&var) = VARIANT_TRUE;
156 set_script_prop(script_host, SCRIPTPROP_HACK_TRIDENTEVENTSINK, &var);
158 hres = IActiveScriptParse_InitNew(script_host->parse);
159 if(FAILED(hres)) {
160 WARN("InitNew failed: %08x\n", hres);
161 return FALSE;
164 hres = IActiveScript_SetScriptSite(script_host->script, &script_host->IActiveScriptSite_iface);
165 if(FAILED(hres)) {
166 WARN("SetScriptSite failed: %08x\n", hres);
167 IActiveScript_Close(script_host->script);
168 return FALSE;
171 hres = IActiveScript_GetScriptState(script_host->script, &state);
172 if(FAILED(hres))
173 WARN("GetScriptState failed: %08x\n", hres);
174 else if(state != SCRIPTSTATE_INITIALIZED)
175 FIXME("state = %x\n", state);
177 hres = IActiveScript_SetScriptState(script_host->script, SCRIPTSTATE_STARTED);
178 if(FAILED(hres)) {
179 WARN("Starting script failed: %08x\n", hres);
180 return FALSE;
183 hres = IActiveScript_AddNamedItem(script_host->script, windowW,
184 SCRIPTITEM_ISVISIBLE|SCRIPTITEM_ISSOURCE|SCRIPTITEM_GLOBALMEMBERS);
185 if(SUCCEEDED(hres)) {
186 V_VT(&var) = VT_BOOL;
187 V_BOOL(&var) = VARIANT_TRUE;
188 set_script_prop(script_host, SCRIPTPROP_ABBREVIATE_GLOBALNAME_RESOLUTION, &var);
189 }else {
190 WARN("AddNamedItem failed: %08x\n", hres);
193 hres = IActiveScript_QueryInterface(script_host->script, &IID_IActiveScriptParseProcedure2,
194 (void**)&script_host->parse_proc);
195 if(FAILED(hres)) {
196 /* FIXME: QI for IActiveScriptParseProcedure */
197 WARN("Could not get IActiveScriptParseProcedure iface: %08x\n", hres);
200 return TRUE;
203 static void release_script_engine(ScriptHost *This)
205 if(!This->script)
206 return;
208 switch(This->script_state) {
209 case SCRIPTSTATE_CONNECTED:
210 IActiveScript_SetScriptState(This->script, SCRIPTSTATE_DISCONNECTED);
212 case SCRIPTSTATE_STARTED:
213 case SCRIPTSTATE_DISCONNECTED:
214 case SCRIPTSTATE_INITIALIZED:
215 IActiveScript_Close(This->script);
217 default:
218 if(This->parse_proc) {
219 IActiveScriptParseProcedure2_Release(This->parse_proc);
220 This->parse_proc = NULL;
223 if(This->parse) {
224 IActiveScriptParse_Release(This->parse);
225 This->parse = NULL;
229 IActiveScript_Release(This->script);
230 This->script = NULL;
231 This->script_state = SCRIPTSTATE_UNINITIALIZED;
234 void connect_scripts(HTMLInnerWindow *window)
236 ScriptHost *iter;
238 LIST_FOR_EACH_ENTRY(iter, &window->script_hosts, ScriptHost, entry) {
239 if(iter->script_state == SCRIPTSTATE_STARTED)
240 IActiveScript_SetScriptState(iter->script, SCRIPTSTATE_CONNECTED);
244 static inline ScriptHost *impl_from_IActiveScriptSite(IActiveScriptSite *iface)
246 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSite_iface);
249 static HRESULT WINAPI ActiveScriptSite_QueryInterface(IActiveScriptSite *iface, REFIID riid, void **ppv)
251 ScriptHost *This = impl_from_IActiveScriptSite(iface);
253 *ppv = NULL;
255 if(IsEqualGUID(&IID_IUnknown, riid)) {
256 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
257 *ppv = &This->IActiveScriptSite_iface;
258 }else if(IsEqualGUID(&IID_IActiveScriptSite, riid)) {
259 TRACE("(%p)->(IID_IActiveScriptSite %p)\n", This, ppv);
260 *ppv = &This->IActiveScriptSite_iface;
261 }else if(IsEqualGUID(&IID_IActiveScriptSiteInterruptPoll, riid)) {
262 TRACE("(%p)->(IID_IActiveScriptSiteInterruprtPoll %p)\n", This, ppv);
263 *ppv = &This->IActiveScriptSiteInterruptPoll_iface;
264 }else if(IsEqualGUID(&IID_IActiveScriptSiteWindow, riid)) {
265 TRACE("(%p)->(IID_IActiveScriptSiteWindow %p)\n", This, ppv);
266 *ppv = &This->IActiveScriptSiteWindow_iface;
267 }else if(IsEqualGUID(&IID_IActiveScriptSiteUIControl, riid)) {
268 TRACE("(%p)->(IID_IActiveScriptSiteUIControl %p)\n", This, ppv);
269 *ppv = &This->IActiveScriptSiteUIControl_iface;
270 }else if(IsEqualGUID(&IID_IActiveScriptSiteDebug, riid)) {
271 TRACE("(%p)->(IID_IActiveScriptSiteDebug %p)\n", This, ppv);
272 *ppv = &This->IActiveScriptSiteDebug_iface;
273 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
274 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
275 *ppv = &This->IServiceProvider_iface;
276 }else if(IsEqualGUID(&IID_ICanHandleException, riid)) {
277 TRACE("(%p)->(IID_ICanHandleException not supported %p)\n", This, ppv);
278 return E_NOINTERFACE;
279 }else {
280 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
281 return E_NOINTERFACE;
284 IUnknown_AddRef((IUnknown*)*ppv);
285 return S_OK;
288 static ULONG WINAPI ActiveScriptSite_AddRef(IActiveScriptSite *iface)
290 ScriptHost *This = impl_from_IActiveScriptSite(iface);
291 LONG ref = InterlockedIncrement(&This->ref);
293 TRACE("(%p) ref=%d\n", This, ref);
295 return ref;
298 static ULONG WINAPI ActiveScriptSite_Release(IActiveScriptSite *iface)
300 ScriptHost *This = impl_from_IActiveScriptSite(iface);
301 LONG ref = InterlockedDecrement(&This->ref);
303 TRACE("(%p) ref=%d\n", This, ref);
305 if(!ref) {
306 release_script_engine(This);
307 if(This->window)
308 list_remove(&This->entry);
309 heap_free(This);
312 return ref;
315 static HRESULT WINAPI ActiveScriptSite_GetLCID(IActiveScriptSite *iface, LCID *plcid)
317 ScriptHost *This = impl_from_IActiveScriptSite(iface);
319 TRACE("(%p)->(%p)\n", This, plcid);
321 *plcid = GetUserDefaultLCID();
322 return S_OK;
325 static HRESULT WINAPI ActiveScriptSite_GetItemInfo(IActiveScriptSite *iface, LPCOLESTR pstrName,
326 DWORD dwReturnMask, IUnknown **ppiunkItem, ITypeInfo **ppti)
328 ScriptHost *This = impl_from_IActiveScriptSite(iface);
330 TRACE("(%p)->(%s %x %p %p)\n", This, debugstr_w(pstrName), dwReturnMask, ppiunkItem, ppti);
332 if(dwReturnMask != SCRIPTINFO_IUNKNOWN) {
333 FIXME("Unsupported mask %x\n", dwReturnMask);
334 return E_NOTIMPL;
337 *ppiunkItem = NULL;
339 if(strcmpW(pstrName, windowW))
340 return DISP_E_MEMBERNOTFOUND;
342 if(!This->window)
343 return E_FAIL;
345 /* FIXME: Return proxy object */
346 *ppiunkItem = (IUnknown*)&This->window->base.IHTMLWindow2_iface;
347 IUnknown_AddRef(*ppiunkItem);
349 return S_OK;
352 static HRESULT WINAPI ActiveScriptSite_GetDocVersionString(IActiveScriptSite *iface, BSTR *pbstrVersion)
354 ScriptHost *This = impl_from_IActiveScriptSite(iface);
355 FIXME("(%p)->(%p)\n", This, pbstrVersion);
356 return E_NOTIMPL;
359 static HRESULT WINAPI ActiveScriptSite_OnScriptTerminate(IActiveScriptSite *iface,
360 const VARIANT *pvarResult, const EXCEPINFO *pexcepinfo)
362 ScriptHost *This = impl_from_IActiveScriptSite(iface);
363 FIXME("(%p)->(%p %p)\n", This, pvarResult, pexcepinfo);
364 return E_NOTIMPL;
367 static HRESULT WINAPI ActiveScriptSite_OnStateChange(IActiveScriptSite *iface, SCRIPTSTATE ssScriptState)
369 ScriptHost *This = impl_from_IActiveScriptSite(iface);
371 TRACE("(%p)->(%x)\n", This, ssScriptState);
373 This->script_state = ssScriptState;
374 return S_OK;
377 static HRESULT WINAPI ActiveScriptSite_OnScriptError(IActiveScriptSite *iface, IActiveScriptError *pscripterror)
379 ScriptHost *This = impl_from_IActiveScriptSite(iface);
380 FIXME("(%p)->(%p)\n", This, pscripterror);
381 return E_NOTIMPL;
384 static HRESULT WINAPI ActiveScriptSite_OnEnterScript(IActiveScriptSite *iface)
386 ScriptHost *This = impl_from_IActiveScriptSite(iface);
388 TRACE("(%p)->()\n", This);
390 return S_OK;
393 static HRESULT WINAPI ActiveScriptSite_OnLeaveScript(IActiveScriptSite *iface)
395 ScriptHost *This = impl_from_IActiveScriptSite(iface);
397 TRACE("(%p)->()\n", This);
399 return S_OK;
402 static const IActiveScriptSiteVtbl ActiveScriptSiteVtbl = {
403 ActiveScriptSite_QueryInterface,
404 ActiveScriptSite_AddRef,
405 ActiveScriptSite_Release,
406 ActiveScriptSite_GetLCID,
407 ActiveScriptSite_GetItemInfo,
408 ActiveScriptSite_GetDocVersionString,
409 ActiveScriptSite_OnScriptTerminate,
410 ActiveScriptSite_OnStateChange,
411 ActiveScriptSite_OnScriptError,
412 ActiveScriptSite_OnEnterScript,
413 ActiveScriptSite_OnLeaveScript
416 static inline ScriptHost *impl_from_IActiveScriptSiteInterruptPoll(IActiveScriptSiteInterruptPoll *iface)
418 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSiteInterruptPoll_iface);
421 static HRESULT WINAPI ActiveScriptSiteInterruptPoll_QueryInterface(IActiveScriptSiteInterruptPoll *iface,
422 REFIID riid, void **ppv)
424 ScriptHost *This = impl_from_IActiveScriptSiteInterruptPoll(iface);
425 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
428 static ULONG WINAPI ActiveScriptSiteInterruptPoll_AddRef(IActiveScriptSiteInterruptPoll *iface)
430 ScriptHost *This = impl_from_IActiveScriptSiteInterruptPoll(iface);
431 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
434 static ULONG WINAPI ActiveScriptSiteInterruptPoll_Release(IActiveScriptSiteInterruptPoll *iface)
436 ScriptHost *This = impl_from_IActiveScriptSiteInterruptPoll(iface);
437 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
440 static HRESULT WINAPI ActiveScriptSiteInterruptPoll_QueryContinue(IActiveScriptSiteInterruptPoll *iface)
442 ScriptHost *This = impl_from_IActiveScriptSiteInterruptPoll(iface);
444 TRACE("(%p)\n", This);
446 return S_OK;
449 static const IActiveScriptSiteInterruptPollVtbl ActiveScriptSiteInterruptPollVtbl = {
450 ActiveScriptSiteInterruptPoll_QueryInterface,
451 ActiveScriptSiteInterruptPoll_AddRef,
452 ActiveScriptSiteInterruptPoll_Release,
453 ActiveScriptSiteInterruptPoll_QueryContinue
456 static inline ScriptHost *impl_from_IActiveScriptSiteWindow(IActiveScriptSiteWindow *iface)
458 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSiteWindow_iface);
461 static HRESULT WINAPI ActiveScriptSiteWindow_QueryInterface(IActiveScriptSiteWindow *iface,
462 REFIID riid, void **ppv)
464 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
465 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
468 static ULONG WINAPI ActiveScriptSiteWindow_AddRef(IActiveScriptSiteWindow *iface)
470 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
471 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
474 static ULONG WINAPI ActiveScriptSiteWindow_Release(IActiveScriptSiteWindow *iface)
476 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
477 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
480 static HRESULT WINAPI ActiveScriptSiteWindow_GetWindow(IActiveScriptSiteWindow *iface, HWND *phwnd)
482 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
484 TRACE("(%p)->(%p)\n", This, phwnd);
486 if(!This->window || !This->window->base.outer_window || !This->window->base.outer_window->doc_obj)
487 return E_UNEXPECTED;
489 *phwnd = This->window->base.outer_window->doc_obj->hwnd;
490 return S_OK;
493 static HRESULT WINAPI ActiveScriptSiteWindow_EnableModeless(IActiveScriptSiteWindow *iface, BOOL fEnable)
495 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
496 FIXME("(%p)->(%x)\n", This, fEnable);
497 return S_OK;
500 static const IActiveScriptSiteWindowVtbl ActiveScriptSiteWindowVtbl = {
501 ActiveScriptSiteWindow_QueryInterface,
502 ActiveScriptSiteWindow_AddRef,
503 ActiveScriptSiteWindow_Release,
504 ActiveScriptSiteWindow_GetWindow,
505 ActiveScriptSiteWindow_EnableModeless
508 static inline ScriptHost *impl_from_IActiveScriptSiteUIControl(IActiveScriptSiteUIControl *iface)
510 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSiteUIControl_iface);
513 static HRESULT WINAPI ActiveScriptSiteUIControl_QueryInterface(IActiveScriptSiteUIControl *iface, REFIID riid, void **ppv)
515 ScriptHost *This = impl_from_IActiveScriptSiteUIControl(iface);
516 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
519 static ULONG WINAPI ActiveScriptSiteUIControl_AddRef(IActiveScriptSiteUIControl *iface)
521 ScriptHost *This = impl_from_IActiveScriptSiteUIControl(iface);
522 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
525 static ULONG WINAPI ActiveScriptSiteUIControl_Release(IActiveScriptSiteUIControl *iface)
527 ScriptHost *This = impl_from_IActiveScriptSiteUIControl(iface);
528 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
531 static HRESULT WINAPI ActiveScriptSiteUIControl_GetUIBehavior(IActiveScriptSiteUIControl *iface, SCRIPTUICITEM UicItem,
532 SCRIPTUICHANDLING *pUicHandling)
534 ScriptHost *This = impl_from_IActiveScriptSiteUIControl(iface);
536 WARN("(%p)->(%d %p) semi-stub\n", This, UicItem, pUicHandling);
538 *pUicHandling = SCRIPTUICHANDLING_ALLOW;
539 return S_OK;
542 static const IActiveScriptSiteUIControlVtbl ActiveScriptSiteUIControlVtbl = {
543 ActiveScriptSiteUIControl_QueryInterface,
544 ActiveScriptSiteUIControl_AddRef,
545 ActiveScriptSiteUIControl_Release,
546 ActiveScriptSiteUIControl_GetUIBehavior
549 static inline ScriptHost *impl_from_IActiveScriptSiteDebug(IActiveScriptSiteDebug *iface)
551 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSiteDebug_iface);
554 static HRESULT WINAPI ActiveScriptSiteDebug_QueryInterface(IActiveScriptSiteDebug *iface,
555 REFIID riid, void **ppv)
557 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
558 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
561 static ULONG WINAPI ActiveScriptSiteDebug_AddRef(IActiveScriptSiteDebug *iface)
563 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
564 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
567 static ULONG WINAPI ActiveScriptSiteDebug_Release(IActiveScriptSiteDebug *iface)
569 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
570 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
573 static HRESULT WINAPI ActiveScriptSiteDebug_GetDocumentContextFromPosition(IActiveScriptSiteDebug *iface,
574 CTXARG_T dwSourceContext, ULONG uCharacterOffset, ULONG uNumChars, IDebugDocumentContext **ppsc)
576 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
577 FIXME("(%p)->(%s %u %u %p)\n", This, wine_dbgstr_longlong(dwSourceContext), uCharacterOffset,
578 uNumChars, ppsc);
579 return E_NOTIMPL;
582 static HRESULT WINAPI ActiveScriptSiteDebug_GetApplication(IActiveScriptSiteDebug *iface, IDebugApplication **ppda)
584 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
585 FIXME("(%p)->(%p)\n", This, ppda);
586 return E_NOTIMPL;
589 static HRESULT WINAPI ActiveScriptSiteDebug_GetRootApplicationNode(IActiveScriptSiteDebug *iface,
590 IDebugApplicationNode **ppdanRoot)
592 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
593 FIXME("(%p)->(%p)\n", This, ppdanRoot);
594 return E_NOTIMPL;
597 static HRESULT WINAPI ActiveScriptSiteDebug_OnScriptErrorDebug(IActiveScriptSiteDebug *iface,
598 IActiveScriptErrorDebug *pErrorDebug, BOOL *pfEnterDebugger, BOOL *pfCallOnScriptErrorWhenContinuing)
600 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
601 FIXME("(%p)->(%p %p %p)\n", This, pErrorDebug, pfEnterDebugger, pfCallOnScriptErrorWhenContinuing);
602 return E_NOTIMPL;
605 static const IActiveScriptSiteDebugVtbl ActiveScriptSiteDebugVtbl = {
606 ActiveScriptSiteDebug_QueryInterface,
607 ActiveScriptSiteDebug_AddRef,
608 ActiveScriptSiteDebug_Release,
609 ActiveScriptSiteDebug_GetDocumentContextFromPosition,
610 ActiveScriptSiteDebug_GetApplication,
611 ActiveScriptSiteDebug_GetRootApplicationNode,
612 ActiveScriptSiteDebug_OnScriptErrorDebug
615 static inline ScriptHost *impl_from_IServiceProvider(IServiceProvider *iface)
617 return CONTAINING_RECORD(iface, ScriptHost, IServiceProvider_iface);
620 static HRESULT WINAPI ASServiceProvider_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
622 ScriptHost *This = impl_from_IServiceProvider(iface);
623 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
626 static ULONG WINAPI ASServiceProvider_AddRef(IServiceProvider *iface)
628 ScriptHost *This = impl_from_IServiceProvider(iface);
629 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
632 static ULONG WINAPI ASServiceProvider_Release(IServiceProvider *iface)
634 ScriptHost *This = impl_from_IServiceProvider(iface);
635 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
638 static HRESULT WINAPI ASServiceProvider_QueryService(IServiceProvider *iface, REFGUID guidService,
639 REFIID riid, void **ppv)
641 ScriptHost *This = impl_from_IServiceProvider(iface);
643 if(IsEqualGUID(&SID_SInternetHostSecurityManager, guidService)) {
644 TRACE("(%p)->(SID_SInternetHostSecurityManager)\n", This);
646 if(!This->window || !This->window->doc)
647 return E_NOINTERFACE;
649 return IInternetHostSecurityManager_QueryInterface(&This->window->doc->IInternetHostSecurityManager_iface,
650 riid, ppv);
653 if(IsEqualGUID(&SID_SContainerDispatch, guidService)) {
654 TRACE("(%p)->(SID_SContainerDispatch)\n", This);
656 if(!This->window || !This->window->doc)
657 return E_NOINTERFACE;
659 return IHTMLDocument2_QueryInterface(&This->window->doc->basedoc.IHTMLDocument2_iface, riid, ppv);
662 FIXME("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
663 return E_NOINTERFACE;
666 static const IServiceProviderVtbl ASServiceProviderVtbl = {
667 ASServiceProvider_QueryInterface,
668 ASServiceProvider_AddRef,
669 ASServiceProvider_Release,
670 ASServiceProvider_QueryService
673 static ScriptHost *create_script_host(HTMLInnerWindow *window, const GUID *guid)
675 ScriptHost *ret;
676 HRESULT hres;
678 ret = heap_alloc_zero(sizeof(*ret));
679 if(!ret)
680 return NULL;
682 ret->IActiveScriptSite_iface.lpVtbl = &ActiveScriptSiteVtbl;
683 ret->IActiveScriptSiteInterruptPoll_iface.lpVtbl = &ActiveScriptSiteInterruptPollVtbl;
684 ret->IActiveScriptSiteWindow_iface.lpVtbl = &ActiveScriptSiteWindowVtbl;
685 ret->IActiveScriptSiteUIControl_iface.lpVtbl = &ActiveScriptSiteUIControlVtbl;
686 ret->IActiveScriptSiteDebug_iface.lpVtbl = &ActiveScriptSiteDebugVtbl;
687 ret->IServiceProvider_iface.lpVtbl = &ASServiceProviderVtbl;
688 ret->ref = 1;
689 ret->window = window;
690 ret->script_state = SCRIPTSTATE_UNINITIALIZED;
692 ret->guid = *guid;
693 list_add_tail(&window->script_hosts, &ret->entry);
695 hres = CoCreateInstance(&ret->guid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
696 &IID_IActiveScript, (void**)&ret->script);
697 if(FAILED(hres))
698 WARN("Could not load script engine: %08x\n", hres);
699 else if(!init_script_engine(ret))
700 release_script_engine(ret);
702 return ret;
705 typedef struct {
706 task_t header;
707 HTMLScriptElement *elem;
708 } fire_readystatechange_task_t;
710 static void fire_readystatechange_proc(task_t *_task)
712 fire_readystatechange_task_t *task = (fire_readystatechange_task_t*)_task;
714 if(!task->elem->pending_readystatechange_event)
715 return;
717 task->elem->pending_readystatechange_event = FALSE;
718 fire_event(task->elem->element.node.doc, EVENTID_READYSTATECHANGE, FALSE, task->elem->element.node.nsnode, NULL, NULL);
721 static void fire_readystatechange_task_destr(task_t *_task)
723 fire_readystatechange_task_t *task = (fire_readystatechange_task_t*)_task;
725 IHTMLScriptElement_Release(&task->elem->IHTMLScriptElement_iface);
728 static void set_script_elem_readystate(HTMLScriptElement *script_elem, READYSTATE readystate)
730 script_elem->readystate = readystate;
732 if(readystate != READYSTATE_INTERACTIVE) {
733 if(!script_elem->element.node.doc->window->parser_callback_cnt) {
734 fire_readystatechange_task_t *task;
735 HRESULT hres;
737 if(script_elem->pending_readystatechange_event)
738 return;
740 task = heap_alloc(sizeof(*task));
741 if(!task)
742 return;
744 IHTMLScriptElement_AddRef(&script_elem->IHTMLScriptElement_iface);
745 task->elem = script_elem;
747 hres = push_task(&task->header, fire_readystatechange_proc, fire_readystatechange_task_destr,
748 script_elem->element.node.doc->window->task_magic);
749 if(SUCCEEDED(hres))
750 script_elem->pending_readystatechange_event = TRUE;
751 }else {
752 script_elem->pending_readystatechange_event = FALSE;
753 fire_event(script_elem->element.node.doc, EVENTID_READYSTATECHANGE, FALSE,
754 script_elem->element.node.nsnode, NULL, NULL);
759 static void parse_elem_text(ScriptHost *script_host, HTMLScriptElement *script_elem, LPCWSTR text)
761 EXCEPINFO excepinfo;
762 VARIANT var;
763 HRESULT hres;
765 TRACE("%s\n", debugstr_w(text));
767 set_script_elem_readystate(script_elem, READYSTATE_INTERACTIVE);
769 VariantInit(&var);
770 memset(&excepinfo, 0, sizeof(excepinfo));
771 TRACE(">>>\n");
772 hres = IActiveScriptParse_ParseScriptText(script_host->parse, text, windowW, NULL, script_endW,
773 0, 0, SCRIPTTEXT_ISVISIBLE|SCRIPTTEXT_HOSTMANAGESSOURCE,
774 &var, &excepinfo);
775 if(SUCCEEDED(hres))
776 TRACE("<<<\n");
777 else
778 WARN("<<< %08x\n", hres);
782 typedef struct {
783 BSCallback bsc;
785 HTMLScriptElement *script_elem;
786 DWORD scheme;
788 DWORD size;
789 char *buf;
790 HRESULT hres;
791 } ScriptBSC;
793 static inline ScriptBSC *impl_from_BSCallback(BSCallback *iface)
795 return CONTAINING_RECORD(iface, ScriptBSC, bsc);
798 static void ScriptBSC_destroy(BSCallback *bsc)
800 ScriptBSC *This = impl_from_BSCallback(bsc);
802 if(This->script_elem) {
803 IHTMLScriptElement_Release(&This->script_elem->IHTMLScriptElement_iface);
804 This->script_elem = NULL;
807 heap_free(This->buf);
808 heap_free(This);
811 static HRESULT ScriptBSC_init_bindinfo(BSCallback *bsc)
813 return S_OK;
816 static HRESULT ScriptBSC_start_binding(BSCallback *bsc)
818 ScriptBSC *This = impl_from_BSCallback(bsc);
820 /* FIXME: We should find a better to decide if 'loading' state is supposed to be used by the protocol. */
821 if(This->scheme == URL_SCHEME_HTTPS || This->scheme == URL_SCHEME_HTTP)
822 set_script_elem_readystate(This->script_elem, READYSTATE_LOADING);
824 return S_OK;
827 static HRESULT ScriptBSC_stop_binding(BSCallback *bsc, HRESULT result)
829 ScriptBSC *This = impl_from_BSCallback(bsc);
831 This->hres = result;
833 if(SUCCEEDED(result)) {
834 if(This->script_elem->readystate == READYSTATE_LOADING)
835 set_script_elem_readystate(This->script_elem, READYSTATE_LOADED);
836 }else {
837 FIXME("binding failed %08x\n", result);
838 heap_free(This->buf);
839 This->buf = NULL;
840 This->size = 0;
843 IHTMLScriptElement_Release(&This->script_elem->IHTMLScriptElement_iface);
844 This->script_elem = NULL;
845 return S_OK;
848 static HRESULT ScriptBSC_read_data(BSCallback *bsc, IStream *stream)
850 ScriptBSC *This = impl_from_BSCallback(bsc);
851 DWORD readed;
852 HRESULT hres;
854 if(!This->buf) {
855 This->buf = heap_alloc(128);
856 if(!This->buf)
857 return E_OUTOFMEMORY;
858 This->size = 128;
861 do {
862 if(This->bsc.readed >= This->size) {
863 void *new_buf;
864 new_buf = heap_realloc(This->buf, This->size << 1);
865 if(!new_buf)
866 return E_OUTOFMEMORY;
867 This->size <<= 1;
868 This->buf = new_buf;
871 hres = read_stream(&This->bsc, stream, This->buf+This->bsc.readed, This->size-This->bsc.readed, &readed);
872 }while(hres == S_OK);
874 return S_OK;
877 static HRESULT ScriptBSC_on_progress(BSCallback *bsc, ULONG status_code, LPCWSTR status_text)
879 return S_OK;
882 static HRESULT ScriptBSC_on_response(BSCallback *bsc, DWORD response_code,
883 LPCWSTR response_headers)
885 return S_OK;
888 static HRESULT ScriptBSC_beginning_transaction(BSCallback *bsc, WCHAR **additional_headers)
890 return S_FALSE;
893 static const BSCallbackVtbl ScriptBSCVtbl = {
894 ScriptBSC_destroy,
895 ScriptBSC_init_bindinfo,
896 ScriptBSC_start_binding,
897 ScriptBSC_stop_binding,
898 ScriptBSC_read_data,
899 ScriptBSC_on_progress,
900 ScriptBSC_on_response,
901 ScriptBSC_beginning_transaction
905 static HRESULT bind_script_to_text(HTMLInnerWindow *window, IUri *uri, HTMLScriptElement *script_elem, WCHAR **ret)
907 UINT cp = CP_UTF8;
908 ScriptBSC *bsc;
909 IMoniker *mon;
910 WCHAR *text;
911 HRESULT hres;
913 hres = CreateURLMonikerEx2(NULL, uri, &mon, URL_MK_UNIFORM);
914 if(FAILED(hres))
915 return hres;
917 bsc = heap_alloc_zero(sizeof(*bsc));
918 if(!bsc) {
919 IMoniker_Release(mon);
920 return E_OUTOFMEMORY;
923 init_bscallback(&bsc->bsc, &ScriptBSCVtbl, mon, 0);
924 IMoniker_Release(mon);
925 bsc->hres = E_FAIL;
927 hres = IUri_GetScheme(uri, &bsc->scheme);
928 if(FAILED(hres))
929 bsc->scheme = URL_SCHEME_UNKNOWN;
931 IHTMLScriptElement_AddRef(&script_elem->IHTMLScriptElement_iface);
932 bsc->script_elem = script_elem;
934 hres = start_binding(window, &bsc->bsc, NULL);
935 if(SUCCEEDED(hres))
936 hres = bsc->hres;
937 if(FAILED(hres)) {
938 IBindStatusCallback_Release(&bsc->bsc.IBindStatusCallback_iface);
939 return hres;
942 if(!bsc->bsc.readed) {
943 *ret = NULL;
944 return S_OK;
947 switch(bsc->bsc.bom) {
948 case BOM_UTF16:
949 if(bsc->bsc.readed % sizeof(WCHAR)) {
950 FIXME("The buffer is not a valid utf16 string\n");
951 hres = E_FAIL;
952 break;
955 text = heap_alloc(bsc->bsc.readed+sizeof(WCHAR));
956 if(!text) {
957 hres = E_OUTOFMEMORY;
958 break;
961 memcpy(text, bsc->buf, bsc->bsc.readed);
962 text[bsc->bsc.readed/sizeof(WCHAR)] = 0;
963 break;
965 default:
966 /* FIXME: Try to use charset from HTTP headers first */
967 cp = get_document_charset(window->doc);
968 /* fall through */
969 case BOM_UTF8: {
970 DWORD len;
972 len = MultiByteToWideChar(cp, 0, bsc->buf, bsc->bsc.readed, NULL, 0);
973 text = heap_alloc((len+1)*sizeof(WCHAR));
974 if(!text) {
975 hres = E_OUTOFMEMORY;
976 break;
979 MultiByteToWideChar(cp, 0, bsc->buf, bsc->bsc.readed, text, len);
980 text[len] = 0;
984 IBindStatusCallback_Release(&bsc->bsc.IBindStatusCallback_iface);
985 if(FAILED(hres))
986 return hres;
988 *ret = text;
989 return S_OK;
992 static void parse_extern_script(ScriptHost *script_host, HTMLScriptElement *script_elem, LPCWSTR src)
994 WCHAR *text;
995 IUri *uri;
996 HRESULT hres;
998 static const WCHAR wine_schemaW[] = {'w','i','n','e',':'};
1000 if(strlenW(src) > sizeof(wine_schemaW)/sizeof(WCHAR) && !memcmp(src, wine_schemaW, sizeof(wine_schemaW)))
1001 src += sizeof(wine_schemaW)/sizeof(WCHAR);
1003 hres = create_uri(src, 0, &uri);
1004 if(FAILED(hres))
1005 return;
1007 hres = bind_script_to_text(script_host->window, uri, script_elem, &text);
1008 IUri_Release(uri);
1009 if(FAILED(hres) || !text)
1010 return;
1012 parse_elem_text(script_host, script_elem, text);
1014 heap_free(text);
1017 static void parse_inline_script(ScriptHost *script_host, HTMLScriptElement *script_elem)
1019 const PRUnichar *text;
1020 nsAString text_str;
1021 nsresult nsres;
1023 nsAString_Init(&text_str, NULL);
1024 nsres = nsIDOMHTMLScriptElement_GetText(script_elem->nsscript, &text_str);
1025 nsAString_GetData(&text_str, &text);
1027 if(NS_FAILED(nsres)) {
1028 ERR("GetText failed: %08x\n", nsres);
1029 }else if(*text) {
1030 parse_elem_text(script_host, script_elem, text);
1033 nsAString_Finish(&text_str);
1036 static void parse_script_elem(ScriptHost *script_host, HTMLScriptElement *script_elem)
1038 nsAString src_str, event_str;
1039 const PRUnichar *src;
1040 nsresult nsres;
1042 nsAString_Init(&event_str, NULL);
1043 nsres = nsIDOMHTMLScriptElement_GetEvent(script_elem->nsscript, &event_str);
1044 if(NS_SUCCEEDED(nsres)) {
1045 const PRUnichar *event;
1047 nsAString_GetData(&event_str, &event);
1048 if(*event) {
1049 TRACE("deferring event %s script evaluation\n", debugstr_w(event));
1050 nsAString_Finish(&event_str);
1051 return;
1053 }else {
1054 ERR("GetEvent failed: %08x\n", nsres);
1056 nsAString_Finish(&event_str);
1058 nsAString_Init(&src_str, NULL);
1059 nsres = nsIDOMHTMLScriptElement_GetSrc(script_elem->nsscript, &src_str);
1060 nsAString_GetData(&src_str, &src);
1062 if(NS_FAILED(nsres)) {
1063 ERR("GetSrc failed: %08x\n", nsres);
1064 }else if(*src) {
1065 script_elem->parsed = TRUE;
1066 parse_extern_script(script_host, script_elem, src);
1067 }else {
1068 parse_inline_script(script_host, script_elem);
1071 nsAString_Finish(&src_str);
1073 set_script_elem_readystate(script_elem, READYSTATE_COMPLETE);
1076 static GUID get_default_script_guid(HTMLInnerWindow *window)
1078 /* If not specified, we should use very first script host that was created for the page (or JScript if none) */
1079 return list_empty(&window->script_hosts)
1080 ? CLSID_JScript
1081 : LIST_ENTRY(list_head(&window->script_hosts), ScriptHost, entry)->guid;
1084 static BOOL get_guid_from_type(LPCWSTR type, GUID *guid)
1086 const WCHAR text_javascriptW[] =
1087 {'t','e','x','t','/','j','a','v','a','s','c','r','i','p','t',0};
1088 const WCHAR text_jscriptW[] =
1089 {'t','e','x','t','/','j','s','c','r','i','p','t',0};
1090 const WCHAR text_vbscriptW[] =
1091 {'t','e','x','t','/','v','b','s','c','r','i','p','t',0};
1093 /* FIXME: Handle more types */
1094 if(!strcmpiW(type, text_javascriptW) || !strcmpiW(type, text_jscriptW)) {
1095 *guid = CLSID_JScript;
1096 }else if(!strcmpiW(type, text_vbscriptW)) {
1097 *guid = CLSID_VBScript;
1098 }else {
1099 FIXME("Unknown type %s\n", debugstr_w(type));
1100 return FALSE;
1103 return TRUE;
1106 static BOOL get_guid_from_language(LPCWSTR type, GUID *guid)
1108 HRESULT hres;
1110 hres = CLSIDFromProgID(type, guid);
1111 if(FAILED(hres))
1112 return FALSE;
1114 /* FIXME: Check CATID_ActiveScriptParse */
1116 return TRUE;
1119 static BOOL get_script_guid(HTMLInnerWindow *window, nsIDOMHTMLScriptElement *nsscript, GUID *guid)
1121 nsIDOMHTMLElement *nselem;
1122 const PRUnichar *language;
1123 nsAString val_str;
1124 BOOL ret = FALSE;
1125 nsresult nsres;
1127 static const PRUnichar languageW[] = {'l','a','n','g','u','a','g','e',0};
1129 nsAString_Init(&val_str, NULL);
1131 nsres = nsIDOMHTMLScriptElement_GetType(nsscript, &val_str);
1132 if(NS_SUCCEEDED(nsres)) {
1133 const PRUnichar *type;
1135 nsAString_GetData(&val_str, &type);
1136 if(*type) {
1137 ret = get_guid_from_type(type, guid);
1138 nsAString_Finish(&val_str);
1139 return ret;
1141 }else {
1142 ERR("GetType failed: %08x\n", nsres);
1145 nsres = nsIDOMHTMLScriptElement_QueryInterface(nsscript, &IID_nsIDOMHTMLElement, (void**)&nselem);
1146 assert(nsres == NS_OK);
1148 nsres = get_elem_attr_value(nselem, languageW, &val_str, &language);
1149 nsIDOMHTMLElement_Release(nselem);
1150 if(NS_SUCCEEDED(nsres)) {
1151 if(*language) {
1152 ret = get_guid_from_language(language, guid);
1153 }else {
1154 *guid = get_default_script_guid(window);
1155 ret = TRUE;
1157 nsAString_Finish(&val_str);
1160 return ret;
1163 static ScriptHost *get_script_host(HTMLInnerWindow *window, const GUID *guid)
1165 ScriptHost *iter;
1167 LIST_FOR_EACH_ENTRY(iter, &window->script_hosts, ScriptHost, entry) {
1168 if(IsEqualGUID(guid, &iter->guid))
1169 return iter;
1172 return create_script_host(window, guid);
1175 static ScriptHost *get_elem_script_host(HTMLInnerWindow *window, HTMLScriptElement *script_elem)
1177 GUID guid;
1179 if(!get_script_guid(window, script_elem->nsscript, &guid)) {
1180 WARN("Could not find script GUID\n");
1181 return NULL;
1184 if(IsEqualGUID(&CLSID_JScript, &guid)
1185 && (!window->base.outer_window || window->base.outer_window->scriptmode != SCRIPTMODE_ACTIVESCRIPT)) {
1186 TRACE("Ignoring JScript\n");
1187 return NULL;
1190 return get_script_host(window, &guid);
1193 void doc_insert_script(HTMLInnerWindow *window, HTMLScriptElement *script_elem)
1195 ScriptHost *script_host;
1197 script_host = get_elem_script_host(window, script_elem);
1198 if(!script_host)
1199 return;
1201 if(script_host->parse)
1202 parse_script_elem(script_host, script_elem);
1205 IDispatch *script_parse_event(HTMLInnerWindow *window, LPCWSTR text)
1207 ScriptHost *script_host;
1208 GUID guid;
1209 const WCHAR *ptr;
1210 IDispatch *disp;
1211 HRESULT hres;
1213 static const WCHAR delimiterW[] = {'\"',0};
1215 TRACE("%s\n", debugstr_w(text));
1217 for(ptr = text; isalnumW(*ptr); ptr++);
1218 if(*ptr == ':') {
1219 LPWSTR language;
1220 BOOL b;
1222 language = heap_alloc((ptr-text+1)*sizeof(WCHAR));
1223 if(!language)
1224 return NULL;
1226 memcpy(language, text, (ptr-text)*sizeof(WCHAR));
1227 language[ptr-text] = 0;
1229 b = get_guid_from_language(language, &guid);
1231 heap_free(language);
1233 if(!b) {
1234 WARN("Could not find language\n");
1235 return NULL;
1238 ptr++;
1239 }else {
1240 ptr = text;
1241 guid = get_default_script_guid(window);
1244 if(IsEqualGUID(&CLSID_JScript, &guid)
1245 && (!window->base.outer_window || window->base.outer_window->scriptmode != SCRIPTMODE_ACTIVESCRIPT)) {
1246 TRACE("Ignoring JScript\n");
1247 return NULL;
1250 script_host = get_script_host(window, &guid);
1251 if(!script_host || !script_host->parse_proc)
1252 return NULL;
1254 hres = IActiveScriptParseProcedure2_ParseProcedureText(script_host->parse_proc, ptr, NULL, emptyW,
1255 NULL, NULL, delimiterW, 0 /* FIXME */, 0,
1256 SCRIPTPROC_HOSTMANAGESSOURCE|SCRIPTPROC_IMPLICIT_THIS|SCRIPTPROC_IMPLICIT_PARENTS, &disp);
1257 if(FAILED(hres)) {
1258 WARN("ParseProcedureText failed: %08x\n", hres);
1259 return NULL;
1262 TRACE("ret %p\n", disp);
1263 return disp;
1266 HRESULT exec_script(HTMLInnerWindow *window, const WCHAR *code, const WCHAR *lang, VARIANT *ret)
1268 ScriptHost *script_host;
1269 EXCEPINFO ei;
1270 GUID guid;
1271 HRESULT hres;
1273 static const WCHAR delimW[] = {'"',0};
1275 if(!get_guid_from_language(lang, &guid)) {
1276 WARN("Could not find script GUID\n");
1277 return CO_E_CLASSSTRING;
1280 script_host = get_script_host(window, &guid);
1281 if(!script_host) {
1282 FIXME("No script host\n");
1283 return E_FAIL;
1286 if(!script_host->parse) {
1287 FIXME("script_host->parse == NULL\n");
1288 return E_FAIL;
1291 memset(&ei, 0, sizeof(ei));
1292 TRACE(">>>\n");
1293 hres = IActiveScriptParse_ParseScriptText(script_host->parse, code, NULL, NULL, delimW, 0, 0, SCRIPTTEXT_ISVISIBLE, ret, &ei);
1294 if(SUCCEEDED(hres))
1295 TRACE("<<<\n");
1296 else
1297 WARN("<<< %08x\n", hres);
1299 return hres;
1302 IDispatch *get_script_disp(ScriptHost *script_host)
1304 IDispatch *disp;
1305 HRESULT hres;
1307 if(!script_host->script)
1308 return NULL;
1310 hres = IActiveScript_GetScriptDispatch(script_host->script, windowW, &disp);
1311 if(FAILED(hres))
1312 return NULL;
1314 return disp;
1317 static EventTarget *find_event_target(HTMLDocumentNode *doc, HTMLScriptElement *script_elem)
1319 EventTarget *event_target = NULL;
1320 const PRUnichar *target_id;
1321 nsAString target_id_str;
1322 nsresult nsres;
1323 HRESULT hres;
1325 nsAString_Init(&target_id_str, NULL);
1326 nsres = nsIDOMHTMLScriptElement_GetHtmlFor(script_elem->nsscript, &target_id_str);
1327 if(NS_FAILED(nsres)) {
1328 ERR("GetScriptFor failed: %08x\n", nsres);
1329 nsAString_Finish(&target_id_str);
1330 return NULL;
1333 nsAString_GetData(&target_id_str, &target_id);
1334 if(!*target_id) {
1335 FIXME("Empty for attribute\n");
1336 }else if(!strcmpW(target_id, documentW)) {
1337 event_target = &doc->node.event_target;
1338 htmldoc_addref(&doc->basedoc);
1339 }else if(!strcmpW(target_id, windowW)) {
1340 if(doc->window) {
1341 event_target = &doc->window->event_target;
1342 IDispatchEx_AddRef(&event_target->dispex.IDispatchEx_iface);
1344 }else {
1345 HTMLElement *target_elem;
1347 hres = get_doc_elem_by_id(doc, target_id, &target_elem);
1348 if(SUCCEEDED(hres) && target_elem) {
1349 event_target = &target_elem->node.event_target;
1353 nsAString_Finish(&target_id_str);
1354 return event_target;
1357 static BOOL parse_event_str(WCHAR *event, const WCHAR **args)
1359 WCHAR *ptr;
1361 TRACE("%s\n", debugstr_w(event));
1363 for(ptr = event; isalnumW(*ptr); ptr++);
1364 if(!*ptr) {
1365 *args = NULL;
1366 return TRUE;
1369 if(*ptr != '(')
1370 return FALSE;
1372 *ptr++ = 0;
1373 *args = ptr;
1374 while(isalnumW(*ptr) || isspaceW(*ptr) || *ptr == ',')
1375 ptr++;
1377 if(*ptr != ')')
1378 return FALSE;
1380 *ptr++ = 0;
1381 return !*ptr;
1384 static IDispatch *parse_event_elem(HTMLDocumentNode *doc, HTMLScriptElement *script_elem, WCHAR **ret_event)
1386 ScriptHost *script_host;
1387 WCHAR *event = NULL;
1388 const WCHAR *args;
1389 nsAString nsstr;
1390 IDispatch *disp;
1391 nsresult nsres;
1392 HRESULT hres;
1394 if(script_elem->parsed)
1395 return NULL;
1397 script_host = get_elem_script_host(doc->window, script_elem);
1398 if(!script_host || !script_host->parse_proc)
1399 return NULL;
1401 nsAString_Init(&nsstr, NULL);
1402 nsres = nsIDOMHTMLScriptElement_GetEvent(script_elem->nsscript, &nsstr);
1403 if(NS_SUCCEEDED(nsres)) {
1404 const PRUnichar *event_val;
1406 nsAString_GetData(&nsstr, &event_val);
1407 event = heap_strdupW(event_val);
1409 nsAString_Finish(&nsstr);
1410 if(!event)
1411 return NULL;
1413 if(!parse_event_str(event, &args)) {
1414 WARN("parsing %s failed\n", debugstr_w(event));
1415 heap_free(event);
1416 return NULL;
1419 nsAString_Init(&nsstr, NULL);
1420 nsres = nsIDOMHTMLScriptElement_GetText(script_elem->nsscript, &nsstr);
1421 if(NS_SUCCEEDED(nsres)) {
1422 const PRUnichar *text;
1424 nsAString_GetData(&nsstr, &text);
1425 hres = IActiveScriptParseProcedure2_ParseProcedureText(script_host->parse_proc, text, args,
1426 emptyW, NULL, NULL, script_endW, 0, 0,
1427 SCRIPTPROC_HOSTMANAGESSOURCE|SCRIPTPROC_IMPLICIT_THIS|SCRIPTPROC_IMPLICIT_PARENTS, &disp);
1428 if(FAILED(hres))
1429 disp = NULL;
1430 }else {
1431 ERR("GetText failed: %08x\n", nsres);
1432 disp = NULL;
1434 nsAString_Finish(&nsstr);
1435 if(!disp) {
1436 heap_free(event);
1437 return NULL;
1440 *ret_event = event;
1441 return disp;
1444 void bind_event_scripts(HTMLDocumentNode *doc)
1446 HTMLPluginContainer *plugin_container;
1447 nsIDOMHTMLScriptElement *nsscript;
1448 HTMLScriptElement *script_elem;
1449 EventTarget *event_target;
1450 nsIDOMNodeList *node_list;
1451 nsIDOMNode *script_node;
1452 nsAString selector_str;
1453 IDispatch *event_disp;
1454 UINT32 length, i;
1455 WCHAR *event;
1456 nsresult nsres;
1457 HRESULT hres;
1459 static const PRUnichar selectorW[] = {'s','c','r','i','p','t','[','e','v','e','n','t',']',0};
1461 TRACE("%p\n", doc);
1463 if(!doc->nsdoc)
1464 return;
1466 nsAString_InitDepend(&selector_str, selectorW);
1467 nsres = nsIDOMHTMLDocument_QuerySelectorAll(doc->nsdoc, &selector_str, &node_list);
1468 nsAString_Finish(&selector_str);
1469 if(NS_FAILED(nsres)) {
1470 ERR("QuerySelectorAll failed: %08x\n", nsres);
1471 return;
1474 if(!node_list)
1475 return;
1477 nsres = nsIDOMNodeList_GetLength(node_list, &length);
1478 assert(nsres == NS_OK);
1480 for(i=0; i < length; i++) {
1481 nsres = nsIDOMNodeList_Item(node_list, i, &script_node);
1482 if(NS_FAILED(nsres) || !script_node) {
1483 ERR("Item(%d) failed: %08x\n", i, nsres);
1484 continue;
1487 nsres = nsIDOMNode_QueryInterface(script_node, &IID_nsIDOMHTMLScriptElement, (void**)&nsscript);
1488 assert(nsres == NS_OK);
1489 nsIDOMNode_Release(script_node);
1491 hres = script_elem_from_nsscript(doc, nsscript, &script_elem);
1492 if(FAILED(hres))
1493 continue;
1495 event_disp = parse_event_elem(doc, script_elem, &event);
1496 if(event_disp) {
1497 event_target = find_event_target(doc, script_elem);
1498 if(event_target) {
1499 hres = IDispatchEx_QueryInterface(&event_target->dispex.IDispatchEx_iface, &IID_HTMLPluginContainer,
1500 (void**)&plugin_container);
1501 if(SUCCEEDED(hres))
1502 bind_activex_event(doc, plugin_container, event, event_disp);
1503 else
1504 bind_target_event(doc, event_target, event, event_disp);
1506 IDispatchEx_Release(&event_target->dispex.IDispatchEx_iface);
1507 if(plugin_container)
1508 node_release(&plugin_container->element.node);
1511 heap_free(event);
1512 IDispatch_Release(event_disp);
1515 IHTMLScriptElement_Release(&script_elem->IHTMLScriptElement_iface);
1518 nsIDOMNodeList_Release(node_list);
1521 BOOL find_global_prop(HTMLInnerWindow *window, BSTR name, DWORD flags, ScriptHost **ret_host, DISPID *ret_id)
1523 IDispatchEx *dispex;
1524 IDispatch *disp;
1525 ScriptHost *iter;
1526 HRESULT hres;
1528 LIST_FOR_EACH_ENTRY(iter, &window->script_hosts, ScriptHost, entry) {
1529 disp = get_script_disp(iter);
1530 if(!disp)
1531 continue;
1533 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1534 if(SUCCEEDED(hres)) {
1535 hres = IDispatchEx_GetDispID(dispex, name, flags & (~fdexNameEnsure), ret_id);
1536 IDispatchEx_Release(dispex);
1537 }else {
1538 FIXME("No IDispatchEx\n");
1539 hres = E_NOTIMPL;
1542 IDispatch_Release(disp);
1543 if(SUCCEEDED(hres)) {
1544 *ret_host = iter;
1545 return TRUE;
1549 return FALSE;
1552 static BOOL is_jscript_available(void)
1554 static BOOL available, checked;
1556 if(!checked) {
1557 IUnknown *unk;
1558 HRESULT hres = CoGetClassObject(&CLSID_JScript, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void**)&unk);
1560 if(SUCCEEDED(hres)) {
1561 available = TRUE;
1562 IUnknown_Release(unk);
1563 }else {
1564 available = FALSE;
1566 checked = TRUE;
1569 return available;
1572 void set_script_mode(HTMLOuterWindow *window, SCRIPTMODE mode)
1574 nsIWebBrowserSetup *setup;
1575 nsresult nsres;
1577 if(mode == SCRIPTMODE_ACTIVESCRIPT && !is_jscript_available()) {
1578 TRACE("jscript.dll not available\n");
1579 window->scriptmode = SCRIPTMODE_GECKO;
1580 return;
1583 window->scriptmode = mode;
1585 if(!window->doc_obj->nscontainer || !window->doc_obj->nscontainer->webbrowser)
1586 return;
1588 nsres = nsIWebBrowser_QueryInterface(window->doc_obj->nscontainer->webbrowser,
1589 &IID_nsIWebBrowserSetup, (void**)&setup);
1590 if(NS_SUCCEEDED(nsres)) {
1591 nsres = nsIWebBrowserSetup_SetProperty(setup, SETUP_ALLOW_JAVASCRIPT,
1592 window->scriptmode == SCRIPTMODE_GECKO);
1594 if(NS_SUCCEEDED(nsres))
1595 nsres = nsIWebBrowserSetup_SetProperty(setup, SETUP_DISABLE_NOSCRIPT, TRUE);
1597 nsIWebBrowserSetup_Release(setup);
1600 if(NS_FAILED(nsres))
1601 ERR("JavaScript setup failed: %08x\n", nsres);
1604 void release_script_hosts(HTMLInnerWindow *window)
1606 script_queue_entry_t *queue_iter;
1607 ScriptHost *iter;
1609 while(!list_empty(&window->script_queue)) {
1610 queue_iter = LIST_ENTRY(list_head(&window->script_queue), script_queue_entry_t, entry);
1612 list_remove(&queue_iter->entry);
1613 IHTMLScriptElement_Release(&queue_iter->script->IHTMLScriptElement_iface);
1614 heap_free(queue_iter);
1617 while(!list_empty(&window->script_hosts)) {
1618 iter = LIST_ENTRY(list_head(&window->script_hosts), ScriptHost, entry);
1620 release_script_engine(iter);
1621 list_remove(&iter->entry);
1622 iter->window = NULL;
1623 IActiveScriptSite_Release(&iter->IActiveScriptSite_iface);