ntoskrnl.exe: Send PnP and Power IRPs to plug and play devices.
[wine.git] / dlls / mshtml / script.c
blobf10a702d7cc9e23cf091d898cd81c3fc28dd5eb2
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 ScriptHost *get_elem_script_host(HTMLInnerWindow*,HTMLScriptElement*);
97 static void set_script_prop(ScriptHost *script_host, DWORD property, VARIANT *val)
99 IActiveScriptProperty *script_prop;
100 HRESULT hres;
102 hres = IActiveScript_QueryInterface(script_host->script, &IID_IActiveScriptProperty,
103 (void**)&script_prop);
104 if(FAILED(hres)) {
105 WARN("Could not get IActiveScriptProperty iface: %08x\n", hres);
106 return;
109 hres = IActiveScriptProperty_SetProperty(script_prop, property, NULL, val);
110 IActiveScriptProperty_Release(script_prop);
111 if(FAILED(hres))
112 WARN("SetProperty(%x) failed: %08x\n", property, hres);
115 static BOOL is_quirks_mode(HTMLDocumentNode *doc)
117 const WCHAR *compat_mode;
118 nsAString nsstr;
119 nsresult nsres;
120 BOOL ret = FALSE;
122 static const WCHAR BackCompatW[] = {'B','a','c','k','C','o','m','p','a','t',0};
124 nsAString_Init(&nsstr, NULL);
125 nsres = nsIDOMHTMLDocument_GetCompatMode(doc->nsdoc, &nsstr);
126 if(NS_SUCCEEDED(nsres)) {
127 nsAString_GetData(&nsstr, &compat_mode);
128 if(!strcmpW(compat_mode, BackCompatW))
129 ret = TRUE;
131 nsAString_Finish(&nsstr);
132 return ret;
135 static BOOL init_script_engine(ScriptHost *script_host)
137 IObjectSafety *safety;
138 SCRIPTSTATE state;
139 DWORD supported_opts=0, enabled_opts=0;
140 VARIANT var;
141 HRESULT hres;
143 hres = IActiveScript_QueryInterface(script_host->script, &IID_IActiveScriptParse, (void**)&script_host->parse);
144 if(FAILED(hres)) {
145 WARN("Could not get IActiveScriptParse: %08x\n", hres);
146 return FALSE;
149 hres = IActiveScript_QueryInterface(script_host->script, &IID_IObjectSafety, (void**)&safety);
150 if(FAILED(hres)) {
151 FIXME("Could not get IObjectSafety: %08x\n", hres);
152 return FALSE;
155 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported_opts, &enabled_opts);
156 if(FAILED(hres)) {
157 FIXME("GetInterfaceSafetyOptions failed: %08x\n", hres);
158 }else if(!(supported_opts & INTERFACE_USES_DISPEX)) {
159 FIXME("INTERFACE_USES_DISPEX is not supported\n");
160 }else {
161 hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse,
162 INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER,
163 INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER);
164 if(FAILED(hres))
165 FIXME("SetInterfaceSafetyOptions failed: %08x\n", hres);
168 IObjectSafety_Release(safety);
169 if(FAILED(hres))
170 return FALSE;
172 V_VT(&var) = VT_I4;
173 V_I4(&var) = is_quirks_mode(script_host->window->doc) ? 1 : 2;
174 set_script_prop(script_host, SCRIPTPROP_INVOKEVERSIONING, &var);
176 V_VT(&var) = VT_BOOL;
177 V_BOOL(&var) = VARIANT_TRUE;
178 set_script_prop(script_host, SCRIPTPROP_HACK_TRIDENTEVENTSINK, &var);
180 hres = IActiveScriptParse_InitNew(script_host->parse);
181 if(FAILED(hres)) {
182 WARN("InitNew failed: %08x\n", hres);
183 return FALSE;
186 hres = IActiveScript_SetScriptSite(script_host->script, &script_host->IActiveScriptSite_iface);
187 if(FAILED(hres)) {
188 WARN("SetScriptSite failed: %08x\n", hres);
189 IActiveScript_Close(script_host->script);
190 return FALSE;
193 hres = IActiveScript_GetScriptState(script_host->script, &state);
194 if(FAILED(hres))
195 WARN("GetScriptState failed: %08x\n", hres);
196 else if(state != SCRIPTSTATE_INITIALIZED)
197 FIXME("state = %x\n", state);
199 hres = IActiveScript_SetScriptState(script_host->script, SCRIPTSTATE_STARTED);
200 if(FAILED(hres)) {
201 WARN("Starting script failed: %08x\n", hres);
202 return FALSE;
205 hres = IActiveScript_AddNamedItem(script_host->script, windowW,
206 SCRIPTITEM_ISVISIBLE|SCRIPTITEM_ISSOURCE|SCRIPTITEM_GLOBALMEMBERS);
207 if(SUCCEEDED(hres)) {
208 V_VT(&var) = VT_BOOL;
209 V_BOOL(&var) = VARIANT_TRUE;
210 set_script_prop(script_host, SCRIPTPROP_ABBREVIATE_GLOBALNAME_RESOLUTION, &var);
211 }else {
212 WARN("AddNamedItem failed: %08x\n", hres);
215 hres = IActiveScript_QueryInterface(script_host->script, &IID_IActiveScriptParseProcedure2,
216 (void**)&script_host->parse_proc);
217 if(FAILED(hres)) {
218 /* FIXME: QI for IActiveScriptParseProcedure */
219 WARN("Could not get IActiveScriptParseProcedure iface: %08x\n", hres);
222 return TRUE;
225 static void release_script_engine(ScriptHost *This)
227 if(!This->script)
228 return;
230 switch(This->script_state) {
231 case SCRIPTSTATE_CONNECTED:
232 IActiveScript_SetScriptState(This->script, SCRIPTSTATE_DISCONNECTED);
234 case SCRIPTSTATE_STARTED:
235 case SCRIPTSTATE_DISCONNECTED:
236 case SCRIPTSTATE_INITIALIZED:
237 IActiveScript_Close(This->script);
239 default:
240 if(This->parse_proc) {
241 IActiveScriptParseProcedure2_Release(This->parse_proc);
242 This->parse_proc = NULL;
245 if(This->parse) {
246 IActiveScriptParse_Release(This->parse);
247 This->parse = NULL;
251 IActiveScript_Release(This->script);
252 This->script = NULL;
253 This->script_state = SCRIPTSTATE_UNINITIALIZED;
256 void connect_scripts(HTMLInnerWindow *window)
258 ScriptHost *iter;
260 LIST_FOR_EACH_ENTRY(iter, &window->script_hosts, ScriptHost, entry) {
261 if(iter->script_state == SCRIPTSTATE_STARTED)
262 IActiveScript_SetScriptState(iter->script, SCRIPTSTATE_CONNECTED);
266 static inline ScriptHost *impl_from_IActiveScriptSite(IActiveScriptSite *iface)
268 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSite_iface);
271 static HRESULT WINAPI ActiveScriptSite_QueryInterface(IActiveScriptSite *iface, REFIID riid, void **ppv)
273 ScriptHost *This = impl_from_IActiveScriptSite(iface);
275 *ppv = NULL;
277 if(IsEqualGUID(&IID_IUnknown, riid)) {
278 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
279 *ppv = &This->IActiveScriptSite_iface;
280 }else if(IsEqualGUID(&IID_IActiveScriptSite, riid)) {
281 TRACE("(%p)->(IID_IActiveScriptSite %p)\n", This, ppv);
282 *ppv = &This->IActiveScriptSite_iface;
283 }else if(IsEqualGUID(&IID_IActiveScriptSiteInterruptPoll, riid)) {
284 TRACE("(%p)->(IID_IActiveScriptSiteInterruprtPoll %p)\n", This, ppv);
285 *ppv = &This->IActiveScriptSiteInterruptPoll_iface;
286 }else if(IsEqualGUID(&IID_IActiveScriptSiteWindow, riid)) {
287 TRACE("(%p)->(IID_IActiveScriptSiteWindow %p)\n", This, ppv);
288 *ppv = &This->IActiveScriptSiteWindow_iface;
289 }else if(IsEqualGUID(&IID_IActiveScriptSiteUIControl, riid)) {
290 TRACE("(%p)->(IID_IActiveScriptSiteUIControl %p)\n", This, ppv);
291 *ppv = &This->IActiveScriptSiteUIControl_iface;
292 }else if(IsEqualGUID(&IID_IActiveScriptSiteDebug, riid)) {
293 TRACE("(%p)->(IID_IActiveScriptSiteDebug %p)\n", This, ppv);
294 *ppv = &This->IActiveScriptSiteDebug_iface;
295 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
296 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
297 *ppv = &This->IServiceProvider_iface;
298 }else if(IsEqualGUID(&IID_ICanHandleException, riid)) {
299 TRACE("(%p)->(IID_ICanHandleException not supported %p)\n", This, ppv);
300 return E_NOINTERFACE;
301 }else {
302 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
303 return E_NOINTERFACE;
306 IUnknown_AddRef((IUnknown*)*ppv);
307 return S_OK;
310 static ULONG WINAPI ActiveScriptSite_AddRef(IActiveScriptSite *iface)
312 ScriptHost *This = impl_from_IActiveScriptSite(iface);
313 LONG ref = InterlockedIncrement(&This->ref);
315 TRACE("(%p) ref=%d\n", This, ref);
317 return ref;
320 static ULONG WINAPI ActiveScriptSite_Release(IActiveScriptSite *iface)
322 ScriptHost *This = impl_from_IActiveScriptSite(iface);
323 LONG ref = InterlockedDecrement(&This->ref);
325 TRACE("(%p) ref=%d\n", This, ref);
327 if(!ref) {
328 release_script_engine(This);
329 if(This->window)
330 list_remove(&This->entry);
331 heap_free(This);
334 return ref;
337 static HRESULT WINAPI ActiveScriptSite_GetLCID(IActiveScriptSite *iface, LCID *plcid)
339 ScriptHost *This = impl_from_IActiveScriptSite(iface);
341 TRACE("(%p)->(%p)\n", This, plcid);
343 *plcid = GetUserDefaultLCID();
344 return S_OK;
347 static HRESULT WINAPI ActiveScriptSite_GetItemInfo(IActiveScriptSite *iface, LPCOLESTR pstrName,
348 DWORD dwReturnMask, IUnknown **ppiunkItem, ITypeInfo **ppti)
350 ScriptHost *This = impl_from_IActiveScriptSite(iface);
352 TRACE("(%p)->(%s %x %p %p)\n", This, debugstr_w(pstrName), dwReturnMask, ppiunkItem, ppti);
354 if(dwReturnMask != SCRIPTINFO_IUNKNOWN) {
355 FIXME("Unsupported mask %x\n", dwReturnMask);
356 return E_NOTIMPL;
359 *ppiunkItem = NULL;
361 if(strcmpW(pstrName, windowW))
362 return DISP_E_MEMBERNOTFOUND;
364 if(!This->window)
365 return E_FAIL;
367 /* FIXME: Return proxy object */
368 *ppiunkItem = (IUnknown*)&This->window->base.IHTMLWindow2_iface;
369 IUnknown_AddRef(*ppiunkItem);
371 return S_OK;
374 static HRESULT WINAPI ActiveScriptSite_GetDocVersionString(IActiveScriptSite *iface, BSTR *pbstrVersion)
376 ScriptHost *This = impl_from_IActiveScriptSite(iface);
377 FIXME("(%p)->(%p)\n", This, pbstrVersion);
378 return E_NOTIMPL;
381 static HRESULT WINAPI ActiveScriptSite_OnScriptTerminate(IActiveScriptSite *iface,
382 const VARIANT *pvarResult, const EXCEPINFO *pexcepinfo)
384 ScriptHost *This = impl_from_IActiveScriptSite(iface);
385 FIXME("(%p)->(%p %p)\n", This, pvarResult, pexcepinfo);
386 return E_NOTIMPL;
389 static HRESULT WINAPI ActiveScriptSite_OnStateChange(IActiveScriptSite *iface, SCRIPTSTATE ssScriptState)
391 ScriptHost *This = impl_from_IActiveScriptSite(iface);
393 TRACE("(%p)->(%x)\n", This, ssScriptState);
395 This->script_state = ssScriptState;
396 return S_OK;
399 static HRESULT WINAPI ActiveScriptSite_OnScriptError(IActiveScriptSite *iface, IActiveScriptError *pscripterror)
401 ScriptHost *This = impl_from_IActiveScriptSite(iface);
402 FIXME("(%p)->(%p)\n", This, pscripterror);
403 return E_NOTIMPL;
406 static HRESULT WINAPI ActiveScriptSite_OnEnterScript(IActiveScriptSite *iface)
408 ScriptHost *This = impl_from_IActiveScriptSite(iface);
410 TRACE("(%p)->()\n", This);
412 return S_OK;
415 static HRESULT WINAPI ActiveScriptSite_OnLeaveScript(IActiveScriptSite *iface)
417 ScriptHost *This = impl_from_IActiveScriptSite(iface);
419 TRACE("(%p)->()\n", This);
421 return S_OK;
424 static const IActiveScriptSiteVtbl ActiveScriptSiteVtbl = {
425 ActiveScriptSite_QueryInterface,
426 ActiveScriptSite_AddRef,
427 ActiveScriptSite_Release,
428 ActiveScriptSite_GetLCID,
429 ActiveScriptSite_GetItemInfo,
430 ActiveScriptSite_GetDocVersionString,
431 ActiveScriptSite_OnScriptTerminate,
432 ActiveScriptSite_OnStateChange,
433 ActiveScriptSite_OnScriptError,
434 ActiveScriptSite_OnEnterScript,
435 ActiveScriptSite_OnLeaveScript
438 static inline ScriptHost *impl_from_IActiveScriptSiteInterruptPoll(IActiveScriptSiteInterruptPoll *iface)
440 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSiteInterruptPoll_iface);
443 static HRESULT WINAPI ActiveScriptSiteInterruptPoll_QueryInterface(IActiveScriptSiteInterruptPoll *iface,
444 REFIID riid, void **ppv)
446 ScriptHost *This = impl_from_IActiveScriptSiteInterruptPoll(iface);
447 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
450 static ULONG WINAPI ActiveScriptSiteInterruptPoll_AddRef(IActiveScriptSiteInterruptPoll *iface)
452 ScriptHost *This = impl_from_IActiveScriptSiteInterruptPoll(iface);
453 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
456 static ULONG WINAPI ActiveScriptSiteInterruptPoll_Release(IActiveScriptSiteInterruptPoll *iface)
458 ScriptHost *This = impl_from_IActiveScriptSiteInterruptPoll(iface);
459 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
462 static HRESULT WINAPI ActiveScriptSiteInterruptPoll_QueryContinue(IActiveScriptSiteInterruptPoll *iface)
464 ScriptHost *This = impl_from_IActiveScriptSiteInterruptPoll(iface);
466 TRACE("(%p)\n", This);
468 return S_OK;
471 static const IActiveScriptSiteInterruptPollVtbl ActiveScriptSiteInterruptPollVtbl = {
472 ActiveScriptSiteInterruptPoll_QueryInterface,
473 ActiveScriptSiteInterruptPoll_AddRef,
474 ActiveScriptSiteInterruptPoll_Release,
475 ActiveScriptSiteInterruptPoll_QueryContinue
478 static inline ScriptHost *impl_from_IActiveScriptSiteWindow(IActiveScriptSiteWindow *iface)
480 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSiteWindow_iface);
483 static HRESULT WINAPI ActiveScriptSiteWindow_QueryInterface(IActiveScriptSiteWindow *iface,
484 REFIID riid, void **ppv)
486 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
487 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
490 static ULONG WINAPI ActiveScriptSiteWindow_AddRef(IActiveScriptSiteWindow *iface)
492 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
493 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
496 static ULONG WINAPI ActiveScriptSiteWindow_Release(IActiveScriptSiteWindow *iface)
498 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
499 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
502 static HRESULT WINAPI ActiveScriptSiteWindow_GetWindow(IActiveScriptSiteWindow *iface, HWND *phwnd)
504 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
506 TRACE("(%p)->(%p)\n", This, phwnd);
508 if(!This->window || !This->window->base.outer_window || !This->window->base.outer_window->doc_obj)
509 return E_UNEXPECTED;
511 *phwnd = This->window->base.outer_window->doc_obj->hwnd;
512 return S_OK;
515 static HRESULT WINAPI ActiveScriptSiteWindow_EnableModeless(IActiveScriptSiteWindow *iface, BOOL fEnable)
517 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
518 FIXME("(%p)->(%x)\n", This, fEnable);
519 return S_OK;
522 static const IActiveScriptSiteWindowVtbl ActiveScriptSiteWindowVtbl = {
523 ActiveScriptSiteWindow_QueryInterface,
524 ActiveScriptSiteWindow_AddRef,
525 ActiveScriptSiteWindow_Release,
526 ActiveScriptSiteWindow_GetWindow,
527 ActiveScriptSiteWindow_EnableModeless
530 static inline ScriptHost *impl_from_IActiveScriptSiteUIControl(IActiveScriptSiteUIControl *iface)
532 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSiteUIControl_iface);
535 static HRESULT WINAPI ActiveScriptSiteUIControl_QueryInterface(IActiveScriptSiteUIControl *iface, REFIID riid, void **ppv)
537 ScriptHost *This = impl_from_IActiveScriptSiteUIControl(iface);
538 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
541 static ULONG WINAPI ActiveScriptSiteUIControl_AddRef(IActiveScriptSiteUIControl *iface)
543 ScriptHost *This = impl_from_IActiveScriptSiteUIControl(iface);
544 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
547 static ULONG WINAPI ActiveScriptSiteUIControl_Release(IActiveScriptSiteUIControl *iface)
549 ScriptHost *This = impl_from_IActiveScriptSiteUIControl(iface);
550 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
553 static HRESULT WINAPI ActiveScriptSiteUIControl_GetUIBehavior(IActiveScriptSiteUIControl *iface, SCRIPTUICITEM UicItem,
554 SCRIPTUICHANDLING *pUicHandling)
556 ScriptHost *This = impl_from_IActiveScriptSiteUIControl(iface);
558 WARN("(%p)->(%d %p) semi-stub\n", This, UicItem, pUicHandling);
560 *pUicHandling = SCRIPTUICHANDLING_ALLOW;
561 return S_OK;
564 static const IActiveScriptSiteUIControlVtbl ActiveScriptSiteUIControlVtbl = {
565 ActiveScriptSiteUIControl_QueryInterface,
566 ActiveScriptSiteUIControl_AddRef,
567 ActiveScriptSiteUIControl_Release,
568 ActiveScriptSiteUIControl_GetUIBehavior
571 static inline ScriptHost *impl_from_IActiveScriptSiteDebug(IActiveScriptSiteDebug *iface)
573 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSiteDebug_iface);
576 static HRESULT WINAPI ActiveScriptSiteDebug_QueryInterface(IActiveScriptSiteDebug *iface,
577 REFIID riid, void **ppv)
579 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
580 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
583 static ULONG WINAPI ActiveScriptSiteDebug_AddRef(IActiveScriptSiteDebug *iface)
585 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
586 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
589 static ULONG WINAPI ActiveScriptSiteDebug_Release(IActiveScriptSiteDebug *iface)
591 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
592 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
595 static HRESULT WINAPI ActiveScriptSiteDebug_GetDocumentContextFromPosition(IActiveScriptSiteDebug *iface,
596 CTXARG_T dwSourceContext, ULONG uCharacterOffset, ULONG uNumChars, IDebugDocumentContext **ppsc)
598 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
599 FIXME("(%p)->(%s %u %u %p)\n", This, wine_dbgstr_longlong(dwSourceContext), uCharacterOffset,
600 uNumChars, ppsc);
601 return E_NOTIMPL;
604 static HRESULT WINAPI ActiveScriptSiteDebug_GetApplication(IActiveScriptSiteDebug *iface, IDebugApplication **ppda)
606 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
607 FIXME("(%p)->(%p)\n", This, ppda);
608 return E_NOTIMPL;
611 static HRESULT WINAPI ActiveScriptSiteDebug_GetRootApplicationNode(IActiveScriptSiteDebug *iface,
612 IDebugApplicationNode **ppdanRoot)
614 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
615 FIXME("(%p)->(%p)\n", This, ppdanRoot);
616 return E_NOTIMPL;
619 static HRESULT WINAPI ActiveScriptSiteDebug_OnScriptErrorDebug(IActiveScriptSiteDebug *iface,
620 IActiveScriptErrorDebug *pErrorDebug, BOOL *pfEnterDebugger, BOOL *pfCallOnScriptErrorWhenContinuing)
622 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
623 FIXME("(%p)->(%p %p %p)\n", This, pErrorDebug, pfEnterDebugger, pfCallOnScriptErrorWhenContinuing);
624 return E_NOTIMPL;
627 static const IActiveScriptSiteDebugVtbl ActiveScriptSiteDebugVtbl = {
628 ActiveScriptSiteDebug_QueryInterface,
629 ActiveScriptSiteDebug_AddRef,
630 ActiveScriptSiteDebug_Release,
631 ActiveScriptSiteDebug_GetDocumentContextFromPosition,
632 ActiveScriptSiteDebug_GetApplication,
633 ActiveScriptSiteDebug_GetRootApplicationNode,
634 ActiveScriptSiteDebug_OnScriptErrorDebug
637 static inline ScriptHost *impl_from_IServiceProvider(IServiceProvider *iface)
639 return CONTAINING_RECORD(iface, ScriptHost, IServiceProvider_iface);
642 static HRESULT WINAPI ASServiceProvider_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
644 ScriptHost *This = impl_from_IServiceProvider(iface);
645 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
648 static ULONG WINAPI ASServiceProvider_AddRef(IServiceProvider *iface)
650 ScriptHost *This = impl_from_IServiceProvider(iface);
651 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
654 static ULONG WINAPI ASServiceProvider_Release(IServiceProvider *iface)
656 ScriptHost *This = impl_from_IServiceProvider(iface);
657 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
660 static HRESULT WINAPI ASServiceProvider_QueryService(IServiceProvider *iface, REFGUID guidService,
661 REFIID riid, void **ppv)
663 ScriptHost *This = impl_from_IServiceProvider(iface);
665 if(IsEqualGUID(&SID_SInternetHostSecurityManager, guidService)) {
666 TRACE("(%p)->(SID_SInternetHostSecurityManager)\n", This);
668 if(!This->window || !This->window->doc)
669 return E_NOINTERFACE;
671 return IInternetHostSecurityManager_QueryInterface(&This->window->doc->IInternetHostSecurityManager_iface,
672 riid, ppv);
675 if(IsEqualGUID(&SID_SContainerDispatch, guidService)) {
676 TRACE("(%p)->(SID_SContainerDispatch)\n", This);
678 if(!This->window || !This->window->doc)
679 return E_NOINTERFACE;
681 return IHTMLDocument2_QueryInterface(&This->window->doc->basedoc.IHTMLDocument2_iface, riid, ppv);
684 FIXME("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
685 return E_NOINTERFACE;
688 static const IServiceProviderVtbl ASServiceProviderVtbl = {
689 ASServiceProvider_QueryInterface,
690 ASServiceProvider_AddRef,
691 ASServiceProvider_Release,
692 ASServiceProvider_QueryService
695 static ScriptHost *create_script_host(HTMLInnerWindow *window, const GUID *guid)
697 ScriptHost *ret;
698 HRESULT hres;
700 ret = heap_alloc_zero(sizeof(*ret));
701 if(!ret)
702 return NULL;
704 ret->IActiveScriptSite_iface.lpVtbl = &ActiveScriptSiteVtbl;
705 ret->IActiveScriptSiteInterruptPoll_iface.lpVtbl = &ActiveScriptSiteInterruptPollVtbl;
706 ret->IActiveScriptSiteWindow_iface.lpVtbl = &ActiveScriptSiteWindowVtbl;
707 ret->IActiveScriptSiteUIControl_iface.lpVtbl = &ActiveScriptSiteUIControlVtbl;
708 ret->IActiveScriptSiteDebug_iface.lpVtbl = &ActiveScriptSiteDebugVtbl;
709 ret->IServiceProvider_iface.lpVtbl = &ASServiceProviderVtbl;
710 ret->ref = 1;
711 ret->window = window;
712 ret->script_state = SCRIPTSTATE_UNINITIALIZED;
714 ret->guid = *guid;
715 list_add_tail(&window->script_hosts, &ret->entry);
717 hres = CoCreateInstance(&ret->guid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
718 &IID_IActiveScript, (void**)&ret->script);
719 if(FAILED(hres))
720 WARN("Could not load script engine: %08x\n", hres);
721 else if(!init_script_engine(ret))
722 release_script_engine(ret);
724 return ret;
727 typedef struct {
728 task_t header;
729 HTMLScriptElement *elem;
730 } fire_readystatechange_task_t;
732 static void fire_readystatechange_proc(task_t *_task)
734 fire_readystatechange_task_t *task = (fire_readystatechange_task_t*)_task;
736 if(!task->elem->pending_readystatechange_event)
737 return;
739 task->elem->pending_readystatechange_event = FALSE;
740 fire_event(task->elem->element.node.doc, EVENTID_READYSTATECHANGE, FALSE, &task->elem->element.node, NULL, NULL);
743 static void fire_readystatechange_task_destr(task_t *_task)
745 fire_readystatechange_task_t *task = (fire_readystatechange_task_t*)_task;
747 IHTMLScriptElement_Release(&task->elem->IHTMLScriptElement_iface);
750 static void set_script_elem_readystate(HTMLScriptElement *script_elem, READYSTATE readystate)
752 script_elem->readystate = readystate;
754 if(readystate != READYSTATE_INTERACTIVE) {
755 if(!script_elem->element.node.doc->window->parser_callback_cnt) {
756 fire_readystatechange_task_t *task;
757 HRESULT hres;
759 if(script_elem->pending_readystatechange_event)
760 return;
762 task = heap_alloc(sizeof(*task));
763 if(!task)
764 return;
766 IHTMLScriptElement_AddRef(&script_elem->IHTMLScriptElement_iface);
767 task->elem = script_elem;
769 hres = push_task(&task->header, fire_readystatechange_proc, fire_readystatechange_task_destr,
770 script_elem->element.node.doc->window->task_magic);
771 if(SUCCEEDED(hres))
772 script_elem->pending_readystatechange_event = TRUE;
773 }else {
774 script_elem->pending_readystatechange_event = FALSE;
775 fire_event(script_elem->element.node.doc, EVENTID_READYSTATECHANGE, FALSE,
776 &script_elem->element.node, NULL, NULL);
781 static void parse_elem_text(ScriptHost *script_host, HTMLScriptElement *script_elem, LPCWSTR text)
783 EXCEPINFO excepinfo;
784 VARIANT var;
785 HRESULT hres;
787 TRACE("%s\n", debugstr_w(text));
789 VariantInit(&var);
790 memset(&excepinfo, 0, sizeof(excepinfo));
791 TRACE(">>>\n");
792 hres = IActiveScriptParse_ParseScriptText(script_host->parse, text, windowW, NULL, script_endW,
793 0, 0, SCRIPTTEXT_ISVISIBLE|SCRIPTTEXT_HOSTMANAGESSOURCE,
794 &var, &excepinfo);
795 if(SUCCEEDED(hres))
796 TRACE("<<<\n");
797 else
798 WARN("<<< %08x\n", hres);
801 typedef struct {
802 BSCallback bsc;
804 HTMLScriptElement *script_elem;
805 DWORD scheme;
807 DWORD size;
808 char *buf;
809 } ScriptBSC;
811 static HRESULT get_binding_text(ScriptBSC *bsc, WCHAR **ret)
813 UINT cp = CP_UTF8;
814 WCHAR *text;
816 if(!bsc->bsc.readed) {
817 text = heap_alloc(sizeof(WCHAR));
818 if(!text)
819 return E_OUTOFMEMORY;
820 *text = 0;
821 *ret = text;
822 return S_OK;
825 switch(bsc->bsc.bom) {
826 case BOM_UTF16:
827 if(bsc->bsc.readed % sizeof(WCHAR)) {
828 FIXME("The buffer is not a valid utf16 string\n");
829 return E_FAIL;
832 text = heap_alloc(bsc->bsc.readed+sizeof(WCHAR));
833 if(!text)
834 return E_OUTOFMEMORY;
836 memcpy(text, bsc->buf, bsc->bsc.readed);
837 text[bsc->bsc.readed/sizeof(WCHAR)] = 0;
838 break;
840 default:
841 /* FIXME: Try to use charset from HTTP headers first */
842 cp = get_document_charset(bsc->script_elem->element.node.doc);
843 /* fall through */
844 case BOM_UTF8: {
845 DWORD len;
847 len = MultiByteToWideChar(cp, 0, bsc->buf, bsc->bsc.readed, NULL, 0);
848 text = heap_alloc((len+1)*sizeof(WCHAR));
849 if(!text)
850 return E_OUTOFMEMORY;
852 MultiByteToWideChar(cp, 0, bsc->buf, bsc->bsc.readed, text, len);
853 text[len] = 0;
857 *ret = text;
858 return S_OK;
861 static void script_file_available(ScriptBSC *bsc)
863 HTMLScriptElement *script_elem = bsc->script_elem;
864 HTMLInnerWindow *window = bsc->bsc.window;
865 ScriptHost *script_host;
866 nsIDOMNode *parent;
867 nsresult nsres;
868 HRESULT hres;
870 assert(window != NULL);
872 hres = get_binding_text(bsc, &script_elem->src_text);
873 if(FAILED(hres))
874 return;
876 script_host = get_elem_script_host(window, script_elem);
877 if(!script_host)
878 return;
880 if(window->parser_callback_cnt) {
881 script_queue_entry_t *queue;
883 TRACE("Adding to queue\n");
885 queue = heap_alloc(sizeof(*queue));
886 if(!queue)
887 return;
889 IHTMLScriptElement_AddRef(&script_elem->IHTMLScriptElement_iface);
890 queue->script = script_elem;
892 list_add_tail(&window->script_queue, &queue->entry);
893 return;
896 nsres = nsIDOMHTMLElement_GetParentNode(script_elem->element.nselem, &parent);
897 if(NS_FAILED(nsres) || !parent) {
898 TRACE("No parent, not executing\n");
899 script_elem->parse_on_bind = TRUE;
900 return;
903 nsIDOMNode_Release(parent);
905 script_host = get_elem_script_host(window, script_elem);
906 if(!script_host)
907 return;
909 script_elem->parsed = TRUE;
910 if(script_host->parse)
911 parse_elem_text(script_host, script_elem, script_elem->src_text);
914 static inline ScriptBSC *impl_from_BSCallback(BSCallback *iface)
916 return CONTAINING_RECORD(iface, ScriptBSC, bsc);
919 static void ScriptBSC_destroy(BSCallback *bsc)
921 ScriptBSC *This = impl_from_BSCallback(bsc);
923 if(This->script_elem) {
924 IHTMLScriptElement_Release(&This->script_elem->IHTMLScriptElement_iface);
925 This->script_elem = NULL;
928 heap_free(This->buf);
929 heap_free(This);
932 static HRESULT ScriptBSC_init_bindinfo(BSCallback *bsc)
934 return S_OK;
937 static HRESULT ScriptBSC_start_binding(BSCallback *bsc)
939 ScriptBSC *This = impl_from_BSCallback(bsc);
941 This->script_elem->binding = &This->bsc;
943 /* FIXME: We should find a better to decide if 'loading' state is supposed to be used by the protocol. */
944 if(This->scheme == URL_SCHEME_HTTPS || This->scheme == URL_SCHEME_HTTP)
945 set_script_elem_readystate(This->script_elem, READYSTATE_LOADING);
947 return S_OK;
950 static HRESULT ScriptBSC_stop_binding(BSCallback *bsc, HRESULT result)
952 ScriptBSC *This = impl_from_BSCallback(bsc);
954 if(SUCCEEDED(result) && !This->script_elem)
955 result = E_UNEXPECTED;
957 assert(FAILED(result) || This->script_elem->binding == &This->bsc);
958 This->script_elem->binding = NULL;
960 if(This->script_elem->readystate == READYSTATE_LOADING)
961 set_script_elem_readystate(This->script_elem, READYSTATE_LOADED);
963 if(SUCCEEDED(result)) {
964 script_file_available(This);
965 }else {
966 FIXME("binding failed %08x\n", result);
967 heap_free(This->buf);
968 This->buf = NULL;
969 This->size = 0;
972 IHTMLScriptElement_Release(&This->script_elem->IHTMLScriptElement_iface);
973 This->script_elem = NULL;
974 return S_OK;
977 static HRESULT ScriptBSC_read_data(BSCallback *bsc, IStream *stream)
979 ScriptBSC *This = impl_from_BSCallback(bsc);
980 DWORD readed;
981 HRESULT hres;
983 if(!This->buf) {
984 This->buf = heap_alloc(128);
985 if(!This->buf)
986 return E_OUTOFMEMORY;
987 This->size = 128;
990 do {
991 if(This->bsc.readed >= This->size) {
992 void *new_buf;
993 new_buf = heap_realloc(This->buf, This->size << 1);
994 if(!new_buf)
995 return E_OUTOFMEMORY;
996 This->size <<= 1;
997 This->buf = new_buf;
1000 hres = read_stream(&This->bsc, stream, This->buf+This->bsc.readed, This->size-This->bsc.readed, &readed);
1001 }while(hres == S_OK);
1003 return S_OK;
1006 static HRESULT ScriptBSC_on_progress(BSCallback *bsc, ULONG status_code, LPCWSTR status_text)
1008 return S_OK;
1011 static HRESULT ScriptBSC_on_response(BSCallback *bsc, DWORD response_code,
1012 LPCWSTR response_headers)
1014 return S_OK;
1017 static HRESULT ScriptBSC_beginning_transaction(BSCallback *bsc, WCHAR **additional_headers)
1019 return S_FALSE;
1022 static const BSCallbackVtbl ScriptBSCVtbl = {
1023 ScriptBSC_destroy,
1024 ScriptBSC_init_bindinfo,
1025 ScriptBSC_start_binding,
1026 ScriptBSC_stop_binding,
1027 ScriptBSC_read_data,
1028 ScriptBSC_on_progress,
1029 ScriptBSC_on_response,
1030 ScriptBSC_beginning_transaction
1034 HRESULT load_script(HTMLScriptElement *script_elem, const WCHAR *src, BOOL async)
1036 HTMLInnerWindow *window;
1037 ScriptBSC *bsc;
1038 IMoniker *mon;
1039 IUri *uri;
1040 HRESULT hres;
1042 static const WCHAR wine_schemaW[] = {'w','i','n','e',':'};
1044 if(strlenW(src) > sizeof(wine_schemaW)/sizeof(WCHAR) && !memcmp(src, wine_schemaW, sizeof(wine_schemaW)))
1045 src += sizeof(wine_schemaW)/sizeof(WCHAR);
1047 TRACE("(%p %s %x)\n", script_elem, debugstr_w(src), async);
1049 if(!script_elem->element.node.doc || !(window = script_elem->element.node.doc->window)) {
1050 ERR("no window\n");
1051 return E_UNEXPECTED;
1054 hres = create_uri(src, 0, &uri);
1055 if(FAILED(hres))
1056 return hres;
1058 hres = CreateURLMonikerEx2(NULL, uri, &mon, URL_MK_UNIFORM);
1059 if(FAILED(hres))
1060 return hres;
1062 bsc = heap_alloc_zero(sizeof(*bsc));
1063 if(!bsc) {
1064 IMoniker_Release(mon);
1065 return E_OUTOFMEMORY;
1068 init_bscallback(&bsc->bsc, &ScriptBSCVtbl, mon, async ? BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA : 0);
1069 IMoniker_Release(mon);
1071 hres = IUri_GetScheme(uri, &bsc->scheme);
1072 if(FAILED(hres))
1073 bsc->scheme = URL_SCHEME_UNKNOWN;
1075 IHTMLScriptElement_AddRef(&script_elem->IHTMLScriptElement_iface);
1076 bsc->script_elem = script_elem;
1078 hres = start_binding(window, &bsc->bsc, NULL);
1080 IBindStatusCallback_Release(&bsc->bsc.IBindStatusCallback_iface);
1081 return hres;
1084 static void parse_inline_script(ScriptHost *script_host, HTMLScriptElement *script_elem)
1086 const PRUnichar *text;
1087 nsAString text_str;
1088 nsresult nsres;
1090 nsAString_Init(&text_str, NULL);
1091 nsres = nsIDOMHTMLScriptElement_GetText(script_elem->nsscript, &text_str);
1092 nsAString_GetData(&text_str, &text);
1094 set_script_elem_readystate(script_elem, READYSTATE_INTERACTIVE);
1096 if(NS_FAILED(nsres)) {
1097 ERR("GetText failed: %08x\n", nsres);
1098 }else if(*text) {
1099 parse_elem_text(script_host, script_elem, text);
1102 nsAString_Finish(&text_str);
1105 static BOOL parse_script_elem(ScriptHost *script_host, HTMLScriptElement *script_elem)
1107 nsAString src_str, event_str;
1108 BOOL is_complete = FALSE;
1109 const PRUnichar *src;
1110 nsresult nsres;
1112 nsAString_Init(&event_str, NULL);
1113 nsres = nsIDOMHTMLScriptElement_GetEvent(script_elem->nsscript, &event_str);
1114 if(NS_SUCCEEDED(nsres)) {
1115 const PRUnichar *event;
1117 nsAString_GetData(&event_str, &event);
1118 if(*event) {
1119 TRACE("deferring event %s script evaluation\n", debugstr_w(event));
1120 nsAString_Finish(&event_str);
1121 return FALSE;
1123 }else {
1124 ERR("GetEvent failed: %08x\n", nsres);
1126 nsAString_Finish(&event_str);
1128 nsAString_Init(&src_str, NULL);
1129 nsres = nsIDOMHTMLScriptElement_GetSrc(script_elem->nsscript, &src_str);
1130 nsAString_GetData(&src_str, &src);
1132 if(NS_FAILED(nsres)) {
1133 ERR("GetSrc failed: %08x\n", nsres);
1134 }else if(*src) {
1135 load_script(script_elem, src, FALSE);
1136 is_complete = script_elem->parsed;
1137 }else {
1138 parse_inline_script(script_host, script_elem);
1139 is_complete = TRUE;
1142 nsAString_Finish(&src_str);
1144 return is_complete;
1147 static GUID get_default_script_guid(HTMLInnerWindow *window)
1149 /* If not specified, we should use very first script host that was created for the page (or JScript if none) */
1150 return list_empty(&window->script_hosts)
1151 ? CLSID_JScript
1152 : LIST_ENTRY(list_head(&window->script_hosts), ScriptHost, entry)->guid;
1155 static BOOL get_guid_from_type(LPCWSTR type, GUID *guid)
1157 const WCHAR text_javascriptW[] =
1158 {'t','e','x','t','/','j','a','v','a','s','c','r','i','p','t',0};
1159 const WCHAR text_jscriptW[] =
1160 {'t','e','x','t','/','j','s','c','r','i','p','t',0};
1161 const WCHAR text_vbscriptW[] =
1162 {'t','e','x','t','/','v','b','s','c','r','i','p','t',0};
1164 /* FIXME: Handle more types */
1165 if(!strcmpiW(type, text_javascriptW) || !strcmpiW(type, text_jscriptW)) {
1166 *guid = CLSID_JScript;
1167 }else if(!strcmpiW(type, text_vbscriptW)) {
1168 *guid = CLSID_VBScript;
1169 }else {
1170 FIXME("Unknown type %s\n", debugstr_w(type));
1171 return FALSE;
1174 return TRUE;
1177 static BOOL get_guid_from_language(LPCWSTR type, GUID *guid)
1179 HRESULT hres;
1181 hres = CLSIDFromProgID(type, guid);
1182 if(FAILED(hres))
1183 return FALSE;
1185 /* FIXME: Check CATID_ActiveScriptParse */
1187 return TRUE;
1190 static BOOL get_script_guid(HTMLInnerWindow *window, nsIDOMHTMLScriptElement *nsscript, GUID *guid)
1192 nsIDOMHTMLElement *nselem;
1193 const PRUnichar *language;
1194 nsAString val_str;
1195 BOOL ret = FALSE;
1196 nsresult nsres;
1198 static const PRUnichar languageW[] = {'l','a','n','g','u','a','g','e',0};
1200 nsAString_Init(&val_str, NULL);
1202 nsres = nsIDOMHTMLScriptElement_GetType(nsscript, &val_str);
1203 if(NS_SUCCEEDED(nsres)) {
1204 const PRUnichar *type;
1206 nsAString_GetData(&val_str, &type);
1207 if(*type) {
1208 ret = get_guid_from_type(type, guid);
1209 nsAString_Finish(&val_str);
1210 return ret;
1212 }else {
1213 ERR("GetType failed: %08x\n", nsres);
1216 nsres = nsIDOMHTMLScriptElement_QueryInterface(nsscript, &IID_nsIDOMHTMLElement, (void**)&nselem);
1217 assert(nsres == NS_OK);
1219 nsres = get_elem_attr_value(nselem, languageW, &val_str, &language);
1220 nsIDOMHTMLElement_Release(nselem);
1221 if(NS_SUCCEEDED(nsres)) {
1222 if(*language) {
1223 ret = get_guid_from_language(language, guid);
1224 }else {
1225 *guid = get_default_script_guid(window);
1226 ret = TRUE;
1228 nsAString_Finish(&val_str);
1231 return ret;
1234 static ScriptHost *get_script_host(HTMLInnerWindow *window, const GUID *guid)
1236 ScriptHost *iter;
1238 LIST_FOR_EACH_ENTRY(iter, &window->script_hosts, ScriptHost, entry) {
1239 if(IsEqualGUID(guid, &iter->guid))
1240 return iter;
1243 return create_script_host(window, guid);
1246 static ScriptHost *get_elem_script_host(HTMLInnerWindow *window, HTMLScriptElement *script_elem)
1248 GUID guid;
1250 if(!get_script_guid(window, script_elem->nsscript, &guid)) {
1251 WARN("Could not find script GUID\n");
1252 return NULL;
1255 if(IsEqualGUID(&CLSID_JScript, &guid)
1256 && (!window->base.outer_window || window->base.outer_window->scriptmode != SCRIPTMODE_ACTIVESCRIPT)) {
1257 TRACE("Ignoring JScript\n");
1258 return NULL;
1261 return get_script_host(window, &guid);
1264 void doc_insert_script(HTMLInnerWindow *window, HTMLScriptElement *script_elem, BOOL from_parser)
1266 ScriptHost *script_host;
1267 BOOL is_complete = FALSE;
1269 script_host = get_elem_script_host(window, script_elem);
1270 if(!script_host)
1271 return;
1273 if(script_host->parse) {
1274 if(script_elem->src_text) {
1275 if(from_parser)
1276 set_script_elem_readystate(script_elem, READYSTATE_INTERACTIVE);
1277 script_elem->parsed = TRUE;
1278 parse_elem_text(script_host, script_elem, script_elem->src_text);
1279 is_complete = TRUE;
1280 }else if(!script_elem->binding) {
1281 is_complete = parse_script_elem(script_host, script_elem);
1285 if(is_complete)
1286 set_script_elem_readystate(script_elem, READYSTATE_COMPLETE);
1289 IDispatch *script_parse_event(HTMLInnerWindow *window, LPCWSTR text)
1291 ScriptHost *script_host;
1292 GUID guid;
1293 const WCHAR *ptr;
1294 IDispatch *disp;
1295 HRESULT hres;
1297 static const WCHAR delimiterW[] = {'\"',0};
1299 TRACE("%s\n", debugstr_w(text));
1301 for(ptr = text; isalnumW(*ptr); ptr++);
1302 if(*ptr == ':') {
1303 LPWSTR language;
1304 BOOL b;
1306 language = heap_alloc((ptr-text+1)*sizeof(WCHAR));
1307 if(!language)
1308 return NULL;
1310 memcpy(language, text, (ptr-text)*sizeof(WCHAR));
1311 language[ptr-text] = 0;
1313 b = get_guid_from_language(language, &guid);
1315 heap_free(language);
1317 if(!b) {
1318 WARN("Could not find language\n");
1319 return NULL;
1322 ptr++;
1323 }else {
1324 ptr = text;
1325 guid = get_default_script_guid(window);
1328 if(IsEqualGUID(&CLSID_JScript, &guid)
1329 && (!window->base.outer_window || window->base.outer_window->scriptmode != SCRIPTMODE_ACTIVESCRIPT)) {
1330 TRACE("Ignoring JScript\n");
1331 return NULL;
1334 script_host = get_script_host(window, &guid);
1335 if(!script_host || !script_host->parse_proc)
1336 return NULL;
1338 hres = IActiveScriptParseProcedure2_ParseProcedureText(script_host->parse_proc, ptr, NULL, emptyW,
1339 NULL, NULL, delimiterW, 0 /* FIXME */, 0,
1340 SCRIPTPROC_HOSTMANAGESSOURCE|SCRIPTPROC_IMPLICIT_THIS|SCRIPTPROC_IMPLICIT_PARENTS, &disp);
1341 if(FAILED(hres)) {
1342 WARN("ParseProcedureText failed: %08x\n", hres);
1343 return NULL;
1346 TRACE("ret %p\n", disp);
1347 return disp;
1350 HRESULT exec_script(HTMLInnerWindow *window, const WCHAR *code, const WCHAR *lang, VARIANT *ret)
1352 ScriptHost *script_host;
1353 EXCEPINFO ei;
1354 GUID guid;
1355 HRESULT hres;
1357 static const WCHAR delimW[] = {'"',0};
1359 if(!get_guid_from_language(lang, &guid)) {
1360 WARN("Could not find script GUID\n");
1361 return CO_E_CLASSSTRING;
1364 script_host = get_script_host(window, &guid);
1365 if(!script_host) {
1366 FIXME("No script host\n");
1367 return E_FAIL;
1370 if(!script_host->parse) {
1371 FIXME("script_host->parse == NULL\n");
1372 return E_FAIL;
1375 memset(&ei, 0, sizeof(ei));
1376 TRACE(">>>\n");
1377 hres = IActiveScriptParse_ParseScriptText(script_host->parse, code, NULL, NULL, delimW, 0, 0, SCRIPTTEXT_ISVISIBLE, ret, &ei);
1378 if(SUCCEEDED(hres))
1379 TRACE("<<<\n");
1380 else
1381 WARN("<<< %08x\n", hres);
1383 return hres;
1386 IDispatch *get_script_disp(ScriptHost *script_host)
1388 IDispatch *disp;
1389 HRESULT hres;
1391 if(!script_host->script)
1392 return NULL;
1394 hres = IActiveScript_GetScriptDispatch(script_host->script, windowW, &disp);
1395 if(FAILED(hres))
1396 return NULL;
1398 return disp;
1401 static EventTarget *find_event_target(HTMLDocumentNode *doc, HTMLScriptElement *script_elem)
1403 EventTarget *event_target = NULL;
1404 const PRUnichar *target_id;
1405 nsAString target_id_str;
1406 nsresult nsres;
1407 HRESULT hres;
1409 nsAString_Init(&target_id_str, NULL);
1410 nsres = nsIDOMHTMLScriptElement_GetHtmlFor(script_elem->nsscript, &target_id_str);
1411 if(NS_FAILED(nsres)) {
1412 ERR("GetScriptFor failed: %08x\n", nsres);
1413 nsAString_Finish(&target_id_str);
1414 return NULL;
1417 nsAString_GetData(&target_id_str, &target_id);
1418 if(!*target_id) {
1419 FIXME("Empty for attribute\n");
1420 }else if(!strcmpW(target_id, documentW)) {
1421 event_target = &doc->node.event_target;
1422 htmldoc_addref(&doc->basedoc);
1423 }else if(!strcmpW(target_id, windowW)) {
1424 if(doc->window) {
1425 event_target = &doc->window->event_target;
1426 IDispatchEx_AddRef(&event_target->dispex.IDispatchEx_iface);
1428 }else {
1429 HTMLElement *target_elem;
1431 hres = get_doc_elem_by_id(doc, target_id, &target_elem);
1432 if(SUCCEEDED(hres) && target_elem) {
1433 event_target = &target_elem->node.event_target;
1437 nsAString_Finish(&target_id_str);
1438 return event_target;
1441 static BOOL parse_event_str(WCHAR *event, const WCHAR **args)
1443 WCHAR *ptr;
1445 TRACE("%s\n", debugstr_w(event));
1447 for(ptr = event; isalnumW(*ptr); ptr++);
1448 if(!*ptr) {
1449 *args = NULL;
1450 return TRUE;
1453 if(*ptr != '(')
1454 return FALSE;
1456 *ptr++ = 0;
1457 *args = ptr;
1458 while(isalnumW(*ptr) || isspaceW(*ptr) || *ptr == ',')
1459 ptr++;
1461 if(*ptr != ')')
1462 return FALSE;
1464 *ptr++ = 0;
1465 return !*ptr;
1468 static IDispatch *parse_event_elem(HTMLDocumentNode *doc, HTMLScriptElement *script_elem, WCHAR **ret_event)
1470 ScriptHost *script_host;
1471 WCHAR *event = NULL;
1472 const WCHAR *args;
1473 nsAString nsstr;
1474 IDispatch *disp;
1475 nsresult nsres;
1476 HRESULT hres;
1478 if(script_elem->parsed)
1479 return NULL;
1481 script_host = get_elem_script_host(doc->window, script_elem);
1482 if(!script_host || !script_host->parse_proc)
1483 return NULL;
1485 nsAString_Init(&nsstr, NULL);
1486 nsres = nsIDOMHTMLScriptElement_GetEvent(script_elem->nsscript, &nsstr);
1487 if(NS_SUCCEEDED(nsres)) {
1488 const PRUnichar *event_val;
1490 nsAString_GetData(&nsstr, &event_val);
1491 event = heap_strdupW(event_val);
1493 nsAString_Finish(&nsstr);
1494 if(!event)
1495 return NULL;
1497 if(!parse_event_str(event, &args)) {
1498 WARN("parsing %s failed\n", debugstr_w(event));
1499 heap_free(event);
1500 return NULL;
1503 nsAString_Init(&nsstr, NULL);
1504 nsres = nsIDOMHTMLScriptElement_GetText(script_elem->nsscript, &nsstr);
1505 if(NS_SUCCEEDED(nsres)) {
1506 const PRUnichar *text;
1508 nsAString_GetData(&nsstr, &text);
1509 hres = IActiveScriptParseProcedure2_ParseProcedureText(script_host->parse_proc, text, args,
1510 emptyW, NULL, NULL, script_endW, 0, 0,
1511 SCRIPTPROC_HOSTMANAGESSOURCE|SCRIPTPROC_IMPLICIT_THIS|SCRIPTPROC_IMPLICIT_PARENTS, &disp);
1512 if(FAILED(hres))
1513 disp = NULL;
1514 }else {
1515 ERR("GetText failed: %08x\n", nsres);
1516 disp = NULL;
1518 nsAString_Finish(&nsstr);
1519 if(!disp) {
1520 heap_free(event);
1521 return NULL;
1524 *ret_event = event;
1525 return disp;
1528 void bind_event_scripts(HTMLDocumentNode *doc)
1530 HTMLPluginContainer *plugin_container;
1531 nsIDOMHTMLScriptElement *nsscript;
1532 HTMLScriptElement *script_elem;
1533 EventTarget *event_target;
1534 nsIDOMNodeList *node_list;
1535 nsIDOMNode *script_node;
1536 nsAString selector_str;
1537 IDispatch *event_disp;
1538 UINT32 length, i;
1539 WCHAR *event;
1540 nsresult nsres;
1541 HRESULT hres;
1543 static const PRUnichar selectorW[] = {'s','c','r','i','p','t','[','e','v','e','n','t',']',0};
1545 TRACE("%p\n", doc);
1547 if(!doc->nsdoc)
1548 return;
1550 nsAString_InitDepend(&selector_str, selectorW);
1551 nsres = nsIDOMHTMLDocument_QuerySelectorAll(doc->nsdoc, &selector_str, &node_list);
1552 nsAString_Finish(&selector_str);
1553 if(NS_FAILED(nsres)) {
1554 ERR("QuerySelectorAll failed: %08x\n", nsres);
1555 return;
1558 if(!node_list)
1559 return;
1561 nsres = nsIDOMNodeList_GetLength(node_list, &length);
1562 assert(nsres == NS_OK);
1564 for(i=0; i < length; i++) {
1565 nsres = nsIDOMNodeList_Item(node_list, i, &script_node);
1566 if(NS_FAILED(nsres) || !script_node) {
1567 ERR("Item(%d) failed: %08x\n", i, nsres);
1568 continue;
1571 nsres = nsIDOMNode_QueryInterface(script_node, &IID_nsIDOMHTMLScriptElement, (void**)&nsscript);
1572 assert(nsres == NS_OK);
1573 nsIDOMNode_Release(script_node);
1575 hres = script_elem_from_nsscript(doc, nsscript, &script_elem);
1576 if(FAILED(hres))
1577 continue;
1579 event_disp = parse_event_elem(doc, script_elem, &event);
1580 if(event_disp) {
1581 event_target = find_event_target(doc, script_elem);
1582 if(event_target) {
1583 hres = IDispatchEx_QueryInterface(&event_target->dispex.IDispatchEx_iface, &IID_HTMLPluginContainer,
1584 (void**)&plugin_container);
1585 if(SUCCEEDED(hres))
1586 bind_activex_event(doc, plugin_container, event, event_disp);
1587 else
1588 bind_target_event(doc, event_target, event, event_disp);
1590 IDispatchEx_Release(&event_target->dispex.IDispatchEx_iface);
1591 if(plugin_container)
1592 node_release(&plugin_container->element.node);
1595 heap_free(event);
1596 IDispatch_Release(event_disp);
1599 IHTMLScriptElement_Release(&script_elem->IHTMLScriptElement_iface);
1602 nsIDOMNodeList_Release(node_list);
1605 BOOL find_global_prop(HTMLInnerWindow *window, BSTR name, DWORD flags, ScriptHost **ret_host, DISPID *ret_id)
1607 IDispatchEx *dispex;
1608 IDispatch *disp;
1609 ScriptHost *iter;
1610 HRESULT hres;
1612 LIST_FOR_EACH_ENTRY(iter, &window->script_hosts, ScriptHost, entry) {
1613 disp = get_script_disp(iter);
1614 if(!disp)
1615 continue;
1617 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1618 if(SUCCEEDED(hres)) {
1619 hres = IDispatchEx_GetDispID(dispex, name, flags & (~fdexNameEnsure), ret_id);
1620 IDispatchEx_Release(dispex);
1621 }else {
1622 FIXME("No IDispatchEx\n");
1623 hres = E_NOTIMPL;
1626 IDispatch_Release(disp);
1627 if(SUCCEEDED(hres)) {
1628 *ret_host = iter;
1629 return TRUE;
1633 return FALSE;
1636 static BOOL is_jscript_available(void)
1638 static BOOL available, checked;
1640 if(!checked) {
1641 IUnknown *unk;
1642 HRESULT hres = CoGetClassObject(&CLSID_JScript, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void**)&unk);
1644 if(SUCCEEDED(hres)) {
1645 available = TRUE;
1646 IUnknown_Release(unk);
1647 }else {
1648 available = FALSE;
1650 checked = TRUE;
1653 return available;
1656 void set_script_mode(HTMLOuterWindow *window, SCRIPTMODE mode)
1658 nsIWebBrowserSetup *setup;
1659 nsresult nsres;
1661 if(mode == SCRIPTMODE_ACTIVESCRIPT && !is_jscript_available()) {
1662 TRACE("jscript.dll not available\n");
1663 window->scriptmode = SCRIPTMODE_GECKO;
1664 return;
1667 window->scriptmode = mode;
1669 if(!window->doc_obj->nscontainer || !window->doc_obj->nscontainer->webbrowser)
1670 return;
1672 nsres = nsIWebBrowser_QueryInterface(window->doc_obj->nscontainer->webbrowser,
1673 &IID_nsIWebBrowserSetup, (void**)&setup);
1674 if(NS_SUCCEEDED(nsres)) {
1675 nsres = nsIWebBrowserSetup_SetProperty(setup, SETUP_ALLOW_JAVASCRIPT,
1676 window->scriptmode == SCRIPTMODE_GECKO);
1678 if(NS_SUCCEEDED(nsres))
1679 nsres = nsIWebBrowserSetup_SetProperty(setup, SETUP_DISABLE_NOSCRIPT, TRUE);
1681 nsIWebBrowserSetup_Release(setup);
1684 if(NS_FAILED(nsres))
1685 ERR("JavaScript setup failed: %08x\n", nsres);
1688 void release_script_hosts(HTMLInnerWindow *window)
1690 script_queue_entry_t *queue_iter;
1691 ScriptHost *iter;
1693 while(!list_empty(&window->script_queue)) {
1694 queue_iter = LIST_ENTRY(list_head(&window->script_queue), script_queue_entry_t, entry);
1696 list_remove(&queue_iter->entry);
1697 IHTMLScriptElement_Release(&queue_iter->script->IHTMLScriptElement_iface);
1698 heap_free(queue_iter);
1701 while(!list_empty(&window->script_hosts)) {
1702 iter = LIST_ENTRY(list_head(&window->script_hosts), ScriptHost, entry);
1704 release_script_engine(iter);
1705 list_remove(&iter->entry);
1706 iter->window = NULL;
1707 IActiveScriptSite_Release(&iter->IActiveScriptSite_iface);