bcrypt: Require macOS version 10.7 or newer for encryption support.
[wine.git] / dlls / mshtml / script.c
blob025db3bdab114bdd2d3215fca2b5a3b12033cb67
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 static void dispatch_script_readystatechange_event(HTMLScriptElement *script)
729 DOMEvent *event;
730 HRESULT hres;
732 hres = create_document_event(script->element.node.doc, EVENTID_READYSTATECHANGE, &event);
733 if(FAILED(hres))
734 return;
736 dispatch_event(&script->element.node.event_target, event);
737 IDOMEvent_Release(&event->IDOMEvent_iface);
740 typedef struct {
741 task_t header;
742 HTMLScriptElement *elem;
743 } fire_readystatechange_task_t;
745 static void fire_readystatechange_proc(task_t *_task)
747 fire_readystatechange_task_t *task = (fire_readystatechange_task_t*)_task;
749 if(!task->elem->pending_readystatechange_event)
750 return;
752 task->elem->pending_readystatechange_event = FALSE;
753 dispatch_script_readystatechange_event(task->elem);
756 static void fire_readystatechange_task_destr(task_t *_task)
758 fire_readystatechange_task_t *task = (fire_readystatechange_task_t*)_task;
760 IHTMLScriptElement_Release(&task->elem->IHTMLScriptElement_iface);
763 static void set_script_elem_readystate(HTMLScriptElement *script_elem, READYSTATE readystate)
765 script_elem->readystate = readystate;
767 if(readystate != READYSTATE_INTERACTIVE) {
768 if(!script_elem->element.node.doc->window->parser_callback_cnt) {
769 fire_readystatechange_task_t *task;
770 HRESULT hres;
772 if(script_elem->pending_readystatechange_event)
773 return;
775 task = heap_alloc(sizeof(*task));
776 if(!task)
777 return;
779 IHTMLScriptElement_AddRef(&script_elem->IHTMLScriptElement_iface);
780 task->elem = script_elem;
782 hres = push_task(&task->header, fire_readystatechange_proc, fire_readystatechange_task_destr,
783 script_elem->element.node.doc->window->task_magic);
784 if(SUCCEEDED(hres))
785 script_elem->pending_readystatechange_event = TRUE;
786 }else {
787 script_elem->pending_readystatechange_event = FALSE;
788 dispatch_script_readystatechange_event(script_elem);
793 static void parse_elem_text(ScriptHost *script_host, HTMLScriptElement *script_elem, LPCWSTR text)
795 EXCEPINFO excepinfo;
796 VARIANT var;
797 HRESULT hres;
799 TRACE("%s\n", debugstr_w(text));
801 VariantInit(&var);
802 memset(&excepinfo, 0, sizeof(excepinfo));
803 TRACE(">>>\n");
804 hres = IActiveScriptParse_ParseScriptText(script_host->parse, text, windowW, NULL, script_endW,
805 0, 0, SCRIPTTEXT_ISVISIBLE|SCRIPTTEXT_HOSTMANAGESSOURCE,
806 &var, &excepinfo);
807 if(SUCCEEDED(hres))
808 TRACE("<<<\n");
809 else
810 WARN("<<< %08x\n", hres);
813 typedef struct {
814 BSCallback bsc;
816 HTMLScriptElement *script_elem;
817 DWORD scheme;
819 DWORD size;
820 char *buf;
821 } ScriptBSC;
823 static HRESULT get_binding_text(ScriptBSC *bsc, WCHAR **ret)
825 UINT cp = CP_UTF8;
826 WCHAR *text;
828 if(!bsc->bsc.readed) {
829 text = heap_alloc(sizeof(WCHAR));
830 if(!text)
831 return E_OUTOFMEMORY;
832 *text = 0;
833 *ret = text;
834 return S_OK;
837 switch(bsc->bsc.bom) {
838 case BOM_UTF16:
839 if(bsc->bsc.readed % sizeof(WCHAR)) {
840 FIXME("The buffer is not a valid utf16 string\n");
841 return E_FAIL;
844 text = heap_alloc(bsc->bsc.readed+sizeof(WCHAR));
845 if(!text)
846 return E_OUTOFMEMORY;
848 memcpy(text, bsc->buf, bsc->bsc.readed);
849 text[bsc->bsc.readed/sizeof(WCHAR)] = 0;
850 break;
852 default:
853 /* FIXME: Try to use charset from HTTP headers first */
854 cp = get_document_charset(bsc->script_elem->element.node.doc);
855 /* fall through */
856 case BOM_UTF8: {
857 DWORD len;
859 len = MultiByteToWideChar(cp, 0, bsc->buf, bsc->bsc.readed, NULL, 0);
860 text = heap_alloc((len+1)*sizeof(WCHAR));
861 if(!text)
862 return E_OUTOFMEMORY;
864 MultiByteToWideChar(cp, 0, bsc->buf, bsc->bsc.readed, text, len);
865 text[len] = 0;
869 *ret = text;
870 return S_OK;
873 static void script_file_available(ScriptBSC *bsc)
875 HTMLScriptElement *script_elem = bsc->script_elem;
876 HTMLInnerWindow *window = bsc->bsc.window;
877 ScriptHost *script_host;
878 nsIDOMNode *parent;
879 nsresult nsres;
880 HRESULT hres;
882 assert(window != NULL);
884 hres = get_binding_text(bsc, &script_elem->src_text);
885 if(FAILED(hres))
886 return;
888 script_host = get_elem_script_host(window, script_elem);
889 if(!script_host)
890 return;
892 if(window->parser_callback_cnt) {
893 script_queue_entry_t *queue;
895 TRACE("Adding to queue\n");
897 queue = heap_alloc(sizeof(*queue));
898 if(!queue)
899 return;
901 IHTMLScriptElement_AddRef(&script_elem->IHTMLScriptElement_iface);
902 queue->script = script_elem;
904 list_add_tail(&window->script_queue, &queue->entry);
905 return;
908 nsres = nsIDOMHTMLElement_GetParentNode(script_elem->element.nselem, &parent);
909 if(NS_FAILED(nsres) || !parent) {
910 TRACE("No parent, not executing\n");
911 script_elem->parse_on_bind = TRUE;
912 return;
915 nsIDOMNode_Release(parent);
917 script_host = get_elem_script_host(window, script_elem);
918 if(!script_host)
919 return;
921 script_elem->parsed = TRUE;
922 if(script_host->parse)
923 parse_elem_text(script_host, script_elem, script_elem->src_text);
926 static inline ScriptBSC *impl_from_BSCallback(BSCallback *iface)
928 return CONTAINING_RECORD(iface, ScriptBSC, bsc);
931 static void ScriptBSC_destroy(BSCallback *bsc)
933 ScriptBSC *This = impl_from_BSCallback(bsc);
935 if(This->script_elem) {
936 IHTMLScriptElement_Release(&This->script_elem->IHTMLScriptElement_iface);
937 This->script_elem = NULL;
940 heap_free(This->buf);
941 heap_free(This);
944 static HRESULT ScriptBSC_init_bindinfo(BSCallback *bsc)
946 return S_OK;
949 static HRESULT ScriptBSC_start_binding(BSCallback *bsc)
951 ScriptBSC *This = impl_from_BSCallback(bsc);
953 This->script_elem->binding = &This->bsc;
955 /* FIXME: We should find a better to decide if 'loading' state is supposed to be used by the protocol. */
956 if(This->scheme == URL_SCHEME_HTTPS || This->scheme == URL_SCHEME_HTTP)
957 set_script_elem_readystate(This->script_elem, READYSTATE_LOADING);
959 return S_OK;
962 static HRESULT ScriptBSC_stop_binding(BSCallback *bsc, HRESULT result)
964 ScriptBSC *This = impl_from_BSCallback(bsc);
966 if(SUCCEEDED(result) && !This->script_elem)
967 result = E_UNEXPECTED;
969 assert(FAILED(result) || This->script_elem->binding == &This->bsc);
970 This->script_elem->binding = NULL;
972 if(This->script_elem->readystate == READYSTATE_LOADING)
973 set_script_elem_readystate(This->script_elem, READYSTATE_LOADED);
975 if(SUCCEEDED(result)) {
976 script_file_available(This);
977 }else {
978 FIXME("binding failed %08x\n", result);
979 heap_free(This->buf);
980 This->buf = NULL;
981 This->size = 0;
984 IHTMLScriptElement_Release(&This->script_elem->IHTMLScriptElement_iface);
985 This->script_elem = NULL;
986 return S_OK;
989 static HRESULT ScriptBSC_read_data(BSCallback *bsc, IStream *stream)
991 ScriptBSC *This = impl_from_BSCallback(bsc);
992 DWORD readed;
993 HRESULT hres;
995 if(!This->buf) {
996 This->buf = heap_alloc(128);
997 if(!This->buf)
998 return E_OUTOFMEMORY;
999 This->size = 128;
1002 do {
1003 if(This->bsc.readed >= This->size) {
1004 void *new_buf;
1005 new_buf = heap_realloc(This->buf, This->size << 1);
1006 if(!new_buf)
1007 return E_OUTOFMEMORY;
1008 This->size <<= 1;
1009 This->buf = new_buf;
1012 hres = read_stream(&This->bsc, stream, This->buf+This->bsc.readed, This->size-This->bsc.readed, &readed);
1013 }while(hres == S_OK);
1015 return S_OK;
1018 static HRESULT ScriptBSC_on_progress(BSCallback *bsc, ULONG status_code, LPCWSTR status_text)
1020 return S_OK;
1023 static HRESULT ScriptBSC_on_response(BSCallback *bsc, DWORD response_code,
1024 LPCWSTR response_headers)
1026 return S_OK;
1029 static HRESULT ScriptBSC_beginning_transaction(BSCallback *bsc, WCHAR **additional_headers)
1031 return S_FALSE;
1034 static const BSCallbackVtbl ScriptBSCVtbl = {
1035 ScriptBSC_destroy,
1036 ScriptBSC_init_bindinfo,
1037 ScriptBSC_start_binding,
1038 ScriptBSC_stop_binding,
1039 ScriptBSC_read_data,
1040 ScriptBSC_on_progress,
1041 ScriptBSC_on_response,
1042 ScriptBSC_beginning_transaction
1046 HRESULT load_script(HTMLScriptElement *script_elem, const WCHAR *src, BOOL async)
1048 HTMLInnerWindow *window;
1049 ScriptBSC *bsc;
1050 IMoniker *mon;
1051 IUri *uri;
1052 HRESULT hres;
1054 static const WCHAR wine_schemaW[] = {'w','i','n','e',':'};
1056 if(strlenW(src) > sizeof(wine_schemaW)/sizeof(WCHAR) && !memcmp(src, wine_schemaW, sizeof(wine_schemaW)))
1057 src += sizeof(wine_schemaW)/sizeof(WCHAR);
1059 TRACE("(%p %s %x)\n", script_elem, debugstr_w(src), async);
1061 if(!script_elem->element.node.doc || !(window = script_elem->element.node.doc->window)) {
1062 ERR("no window\n");
1063 return E_UNEXPECTED;
1066 hres = create_uri(src, 0, &uri);
1067 if(FAILED(hres))
1068 return hres;
1070 hres = CreateURLMonikerEx2(NULL, uri, &mon, URL_MK_UNIFORM);
1071 if(FAILED(hres)) {
1072 IUri_Release(uri);
1073 return hres;
1076 bsc = heap_alloc_zero(sizeof(*bsc));
1077 if(!bsc) {
1078 IMoniker_Release(mon);
1079 IUri_Release(uri);
1080 return E_OUTOFMEMORY;
1083 init_bscallback(&bsc->bsc, &ScriptBSCVtbl, mon, async ? BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA : 0);
1084 IMoniker_Release(mon);
1086 hres = IUri_GetScheme(uri, &bsc->scheme);
1087 IUri_Release(uri);
1088 if(FAILED(hres))
1089 bsc->scheme = URL_SCHEME_UNKNOWN;
1091 IHTMLScriptElement_AddRef(&script_elem->IHTMLScriptElement_iface);
1092 bsc->script_elem = script_elem;
1094 hres = start_binding(window, &bsc->bsc, NULL);
1096 IBindStatusCallback_Release(&bsc->bsc.IBindStatusCallback_iface);
1097 return hres;
1100 static void parse_inline_script(ScriptHost *script_host, HTMLScriptElement *script_elem)
1102 const PRUnichar *text;
1103 nsAString text_str;
1104 nsresult nsres;
1106 nsAString_Init(&text_str, NULL);
1107 nsres = nsIDOMHTMLScriptElement_GetText(script_elem->nsscript, &text_str);
1108 nsAString_GetData(&text_str, &text);
1110 set_script_elem_readystate(script_elem, READYSTATE_INTERACTIVE);
1112 if(NS_FAILED(nsres)) {
1113 ERR("GetText failed: %08x\n", nsres);
1114 }else if(*text) {
1115 parse_elem_text(script_host, script_elem, text);
1118 nsAString_Finish(&text_str);
1121 static BOOL parse_script_elem(ScriptHost *script_host, HTMLScriptElement *script_elem)
1123 nsAString src_str, event_str;
1124 BOOL is_complete = FALSE;
1125 const PRUnichar *src;
1126 nsresult nsres;
1128 nsAString_Init(&event_str, NULL);
1129 nsres = nsIDOMHTMLScriptElement_GetEvent(script_elem->nsscript, &event_str);
1130 if(NS_SUCCEEDED(nsres)) {
1131 const PRUnichar *event;
1133 nsAString_GetData(&event_str, &event);
1134 if(*event) {
1135 TRACE("deferring event %s script evaluation\n", debugstr_w(event));
1136 nsAString_Finish(&event_str);
1137 return FALSE;
1139 }else {
1140 ERR("GetEvent failed: %08x\n", nsres);
1142 nsAString_Finish(&event_str);
1144 nsAString_Init(&src_str, NULL);
1145 nsres = nsIDOMHTMLScriptElement_GetSrc(script_elem->nsscript, &src_str);
1146 nsAString_GetData(&src_str, &src);
1148 if(NS_FAILED(nsres)) {
1149 ERR("GetSrc failed: %08x\n", nsres);
1150 }else if(*src) {
1151 load_script(script_elem, src, FALSE);
1152 is_complete = script_elem->parsed;
1153 }else {
1154 parse_inline_script(script_host, script_elem);
1155 is_complete = TRUE;
1158 nsAString_Finish(&src_str);
1160 return is_complete;
1163 static GUID get_default_script_guid(HTMLInnerWindow *window)
1165 /* If not specified, we should use very first script host that was created for the page (or JScript if none) */
1166 return list_empty(&window->script_hosts)
1167 ? CLSID_JScript
1168 : LIST_ENTRY(list_head(&window->script_hosts), ScriptHost, entry)->guid;
1171 static BOOL get_guid_from_type(LPCWSTR type, GUID *guid)
1173 const WCHAR text_javascriptW[] =
1174 {'t','e','x','t','/','j','a','v','a','s','c','r','i','p','t',0};
1175 const WCHAR text_jscriptW[] =
1176 {'t','e','x','t','/','j','s','c','r','i','p','t',0};
1177 const WCHAR text_vbscriptW[] =
1178 {'t','e','x','t','/','v','b','s','c','r','i','p','t',0};
1180 /* FIXME: Handle more types */
1181 if(!strcmpiW(type, text_javascriptW) || !strcmpiW(type, text_jscriptW)) {
1182 *guid = CLSID_JScript;
1183 }else if(!strcmpiW(type, text_vbscriptW)) {
1184 *guid = CLSID_VBScript;
1185 }else {
1186 FIXME("Unknown type %s\n", debugstr_w(type));
1187 return FALSE;
1190 return TRUE;
1193 static BOOL get_guid_from_language(LPCWSTR type, GUID *guid)
1195 HRESULT hres;
1197 hres = CLSIDFromProgID(type, guid);
1198 if(FAILED(hres))
1199 return FALSE;
1201 /* FIXME: Check CATID_ActiveScriptParse */
1203 return TRUE;
1206 static BOOL get_script_guid(HTMLInnerWindow *window, nsIDOMHTMLScriptElement *nsscript, GUID *guid)
1208 nsIDOMHTMLElement *nselem;
1209 const PRUnichar *language;
1210 nsAString val_str;
1211 BOOL ret = FALSE;
1212 nsresult nsres;
1214 static const PRUnichar languageW[] = {'l','a','n','g','u','a','g','e',0};
1216 nsAString_Init(&val_str, NULL);
1218 nsres = nsIDOMHTMLScriptElement_GetType(nsscript, &val_str);
1219 if(NS_SUCCEEDED(nsres)) {
1220 const PRUnichar *type;
1222 nsAString_GetData(&val_str, &type);
1223 if(*type) {
1224 ret = get_guid_from_type(type, guid);
1225 nsAString_Finish(&val_str);
1226 return ret;
1228 }else {
1229 ERR("GetType failed: %08x\n", nsres);
1232 nsres = nsIDOMHTMLScriptElement_QueryInterface(nsscript, &IID_nsIDOMHTMLElement, (void**)&nselem);
1233 assert(nsres == NS_OK);
1235 nsres = get_elem_attr_value(nselem, languageW, &val_str, &language);
1236 nsIDOMHTMLElement_Release(nselem);
1237 if(NS_SUCCEEDED(nsres)) {
1238 if(*language) {
1239 ret = get_guid_from_language(language, guid);
1240 }else {
1241 *guid = get_default_script_guid(window);
1242 ret = TRUE;
1244 nsAString_Finish(&val_str);
1247 return ret;
1250 static ScriptHost *get_script_host(HTMLInnerWindow *window, const GUID *guid)
1252 ScriptHost *iter;
1254 LIST_FOR_EACH_ENTRY(iter, &window->script_hosts, ScriptHost, entry) {
1255 if(IsEqualGUID(guid, &iter->guid))
1256 return iter;
1259 return create_script_host(window, guid);
1262 static ScriptHost *get_elem_script_host(HTMLInnerWindow *window, HTMLScriptElement *script_elem)
1264 GUID guid;
1266 if(!get_script_guid(window, script_elem->nsscript, &guid)) {
1267 WARN("Could not find script GUID\n");
1268 return NULL;
1271 if(IsEqualGUID(&CLSID_JScript, &guid)
1272 && (!window->base.outer_window || window->base.outer_window->scriptmode != SCRIPTMODE_ACTIVESCRIPT)) {
1273 TRACE("Ignoring JScript\n");
1274 return NULL;
1277 return get_script_host(window, &guid);
1280 void doc_insert_script(HTMLInnerWindow *window, HTMLScriptElement *script_elem, BOOL from_parser)
1282 ScriptHost *script_host;
1283 BOOL is_complete = FALSE;
1285 script_host = get_elem_script_host(window, script_elem);
1286 if(!script_host)
1287 return;
1289 if(script_host->parse) {
1290 if(script_elem->src_text) {
1291 if(from_parser)
1292 set_script_elem_readystate(script_elem, READYSTATE_INTERACTIVE);
1293 script_elem->parsed = TRUE;
1294 parse_elem_text(script_host, script_elem, script_elem->src_text);
1295 is_complete = TRUE;
1296 }else if(!script_elem->binding) {
1297 is_complete = parse_script_elem(script_host, script_elem);
1301 if(is_complete)
1302 set_script_elem_readystate(script_elem, READYSTATE_COMPLETE);
1305 IDispatch *script_parse_event(HTMLInnerWindow *window, LPCWSTR text)
1307 ScriptHost *script_host;
1308 GUID guid;
1309 const WCHAR *ptr;
1310 IDispatch *disp;
1311 HRESULT hres;
1313 static const WCHAR delimiterW[] = {'\"',0};
1315 TRACE("%s\n", debugstr_w(text));
1317 for(ptr = text; isalnumW(*ptr); ptr++);
1318 if(*ptr == ':') {
1319 LPWSTR language;
1320 BOOL b;
1322 language = heap_alloc((ptr-text+1)*sizeof(WCHAR));
1323 if(!language)
1324 return NULL;
1326 memcpy(language, text, (ptr-text)*sizeof(WCHAR));
1327 language[ptr-text] = 0;
1329 b = get_guid_from_language(language, &guid);
1331 heap_free(language);
1333 if(!b) {
1334 WARN("Could not find language\n");
1335 return NULL;
1338 ptr++;
1339 }else {
1340 ptr = text;
1341 guid = get_default_script_guid(window);
1344 if(IsEqualGUID(&CLSID_JScript, &guid)
1345 && (!window->base.outer_window || window->base.outer_window->scriptmode != SCRIPTMODE_ACTIVESCRIPT)) {
1346 TRACE("Ignoring JScript\n");
1347 return NULL;
1350 script_host = get_script_host(window, &guid);
1351 if(!script_host || !script_host->parse_proc)
1352 return NULL;
1354 hres = IActiveScriptParseProcedure2_ParseProcedureText(script_host->parse_proc, ptr, NULL, emptyW,
1355 NULL, NULL, delimiterW, 0 /* FIXME */, 0,
1356 SCRIPTPROC_HOSTMANAGESSOURCE|SCRIPTPROC_IMPLICIT_THIS|SCRIPTPROC_IMPLICIT_PARENTS, &disp);
1357 if(FAILED(hres)) {
1358 WARN("ParseProcedureText failed: %08x\n", hres);
1359 return NULL;
1362 TRACE("ret %p\n", disp);
1363 return disp;
1366 HRESULT exec_script(HTMLInnerWindow *window, const WCHAR *code, const WCHAR *lang, VARIANT *ret)
1368 ScriptHost *script_host;
1369 EXCEPINFO ei;
1370 GUID guid;
1371 HRESULT hres;
1373 static const WCHAR delimW[] = {'"',0};
1375 if(!get_guid_from_language(lang, &guid)) {
1376 WARN("Could not find script GUID\n");
1377 return CO_E_CLASSSTRING;
1380 script_host = get_script_host(window, &guid);
1381 if(!script_host) {
1382 FIXME("No script host\n");
1383 return E_FAIL;
1386 if(!script_host->parse) {
1387 FIXME("script_host->parse == NULL\n");
1388 return E_FAIL;
1391 memset(&ei, 0, sizeof(ei));
1392 TRACE(">>>\n");
1393 hres = IActiveScriptParse_ParseScriptText(script_host->parse, code, NULL, NULL, delimW, 0, 0, SCRIPTTEXT_ISVISIBLE, ret, &ei);
1394 if(SUCCEEDED(hres))
1395 TRACE("<<<\n");
1396 else
1397 WARN("<<< %08x\n", hres);
1399 return hres;
1402 IDispatch *get_script_disp(ScriptHost *script_host)
1404 IDispatch *disp;
1405 HRESULT hres;
1407 if(!script_host->script)
1408 return NULL;
1410 hres = IActiveScript_GetScriptDispatch(script_host->script, windowW, &disp);
1411 if(FAILED(hres))
1412 return NULL;
1414 return disp;
1417 static EventTarget *find_event_target(HTMLDocumentNode *doc, HTMLScriptElement *script_elem)
1419 EventTarget *event_target = NULL;
1420 const PRUnichar *target_id;
1421 nsAString target_id_str;
1422 nsresult nsres;
1423 HRESULT hres;
1425 nsAString_Init(&target_id_str, NULL);
1426 nsres = nsIDOMHTMLScriptElement_GetHtmlFor(script_elem->nsscript, &target_id_str);
1427 if(NS_FAILED(nsres)) {
1428 ERR("GetScriptFor failed: %08x\n", nsres);
1429 nsAString_Finish(&target_id_str);
1430 return NULL;
1433 nsAString_GetData(&target_id_str, &target_id);
1434 if(!*target_id) {
1435 FIXME("Empty for attribute\n");
1436 }else if(!strcmpW(target_id, documentW)) {
1437 event_target = &doc->node.event_target;
1438 htmldoc_addref(&doc->basedoc);
1439 }else if(!strcmpW(target_id, windowW)) {
1440 if(doc->window) {
1441 event_target = &doc->window->event_target;
1442 IDispatchEx_AddRef(&event_target->dispex.IDispatchEx_iface);
1444 }else {
1445 HTMLElement *target_elem;
1447 hres = get_doc_elem_by_id(doc, target_id, &target_elem);
1448 if(SUCCEEDED(hres) && target_elem) {
1449 event_target = &target_elem->node.event_target;
1453 nsAString_Finish(&target_id_str);
1454 return event_target;
1457 static BOOL parse_event_str(WCHAR *event, const WCHAR **args)
1459 WCHAR *ptr;
1461 TRACE("%s\n", debugstr_w(event));
1463 for(ptr = event; isalnumW(*ptr); ptr++);
1464 if(!*ptr) {
1465 *args = NULL;
1466 return TRUE;
1469 if(*ptr != '(')
1470 return FALSE;
1472 *ptr++ = 0;
1473 *args = ptr;
1474 while(isalnumW(*ptr) || isspaceW(*ptr) || *ptr == ',')
1475 ptr++;
1477 if(*ptr != ')')
1478 return FALSE;
1480 *ptr++ = 0;
1481 return !*ptr;
1484 static IDispatch *parse_event_elem(HTMLDocumentNode *doc, HTMLScriptElement *script_elem, WCHAR **ret_event)
1486 ScriptHost *script_host;
1487 WCHAR *event = NULL;
1488 const WCHAR *args;
1489 nsAString nsstr;
1490 IDispatch *disp;
1491 nsresult nsres;
1492 HRESULT hres;
1494 if(script_elem->parsed)
1495 return NULL;
1497 script_host = get_elem_script_host(doc->window, script_elem);
1498 if(!script_host || !script_host->parse_proc)
1499 return NULL;
1501 nsAString_Init(&nsstr, NULL);
1502 nsres = nsIDOMHTMLScriptElement_GetEvent(script_elem->nsscript, &nsstr);
1503 if(NS_SUCCEEDED(nsres)) {
1504 const PRUnichar *event_val;
1506 nsAString_GetData(&nsstr, &event_val);
1507 event = heap_strdupW(event_val);
1509 nsAString_Finish(&nsstr);
1510 if(!event)
1511 return NULL;
1513 if(!parse_event_str(event, &args)) {
1514 WARN("parsing %s failed\n", debugstr_w(event));
1515 heap_free(event);
1516 return NULL;
1519 nsAString_Init(&nsstr, NULL);
1520 nsres = nsIDOMHTMLScriptElement_GetText(script_elem->nsscript, &nsstr);
1521 if(NS_SUCCEEDED(nsres)) {
1522 const PRUnichar *text;
1524 nsAString_GetData(&nsstr, &text);
1525 hres = IActiveScriptParseProcedure2_ParseProcedureText(script_host->parse_proc, text, args,
1526 emptyW, NULL, NULL, script_endW, 0, 0,
1527 SCRIPTPROC_HOSTMANAGESSOURCE|SCRIPTPROC_IMPLICIT_THIS|SCRIPTPROC_IMPLICIT_PARENTS, &disp);
1528 if(FAILED(hres))
1529 disp = NULL;
1530 }else {
1531 ERR("GetText failed: %08x\n", nsres);
1532 disp = NULL;
1534 nsAString_Finish(&nsstr);
1535 if(!disp) {
1536 heap_free(event);
1537 return NULL;
1540 *ret_event = event;
1541 return disp;
1544 void bind_event_scripts(HTMLDocumentNode *doc)
1546 HTMLPluginContainer *plugin_container;
1547 nsIDOMHTMLScriptElement *nsscript;
1548 HTMLScriptElement *script_elem;
1549 EventTarget *event_target;
1550 nsIDOMNodeList *node_list;
1551 nsIDOMNode *script_node;
1552 nsAString selector_str;
1553 IDispatch *event_disp;
1554 UINT32 length, i;
1555 WCHAR *event;
1556 nsresult nsres;
1557 HRESULT hres;
1559 static const PRUnichar selectorW[] = {'s','c','r','i','p','t','[','e','v','e','n','t',']',0};
1561 TRACE("%p\n", doc);
1563 if(!doc->nsdoc)
1564 return;
1566 nsAString_InitDepend(&selector_str, selectorW);
1567 nsres = nsIDOMHTMLDocument_QuerySelectorAll(doc->nsdoc, &selector_str, &node_list);
1568 nsAString_Finish(&selector_str);
1569 if(NS_FAILED(nsres)) {
1570 ERR("QuerySelectorAll failed: %08x\n", nsres);
1571 return;
1574 if(!node_list)
1575 return;
1577 nsres = nsIDOMNodeList_GetLength(node_list, &length);
1578 assert(nsres == NS_OK);
1580 for(i=0; i < length; i++) {
1581 nsres = nsIDOMNodeList_Item(node_list, i, &script_node);
1582 if(NS_FAILED(nsres) || !script_node) {
1583 ERR("Item(%d) failed: %08x\n", i, nsres);
1584 continue;
1587 nsres = nsIDOMNode_QueryInterface(script_node, &IID_nsIDOMHTMLScriptElement, (void**)&nsscript);
1588 assert(nsres == NS_OK);
1589 nsIDOMNode_Release(script_node);
1591 hres = script_elem_from_nsscript(doc, nsscript, &script_elem);
1592 if(FAILED(hres))
1593 continue;
1595 event_disp = parse_event_elem(doc, script_elem, &event);
1596 if(event_disp) {
1597 event_target = find_event_target(doc, script_elem);
1598 if(event_target) {
1599 hres = IDispatchEx_QueryInterface(&event_target->dispex.IDispatchEx_iface, &IID_HTMLPluginContainer,
1600 (void**)&plugin_container);
1601 if(SUCCEEDED(hres))
1602 bind_activex_event(doc, plugin_container, event, event_disp);
1603 else
1604 bind_target_event(doc, event_target, event, event_disp);
1606 IDispatchEx_Release(&event_target->dispex.IDispatchEx_iface);
1607 if(plugin_container)
1608 node_release(&plugin_container->element.node);
1611 heap_free(event);
1612 IDispatch_Release(event_disp);
1615 IHTMLScriptElement_Release(&script_elem->IHTMLScriptElement_iface);
1618 nsIDOMNodeList_Release(node_list);
1621 BOOL find_global_prop(HTMLInnerWindow *window, BSTR name, DWORD flags, ScriptHost **ret_host, DISPID *ret_id)
1623 IDispatchEx *dispex;
1624 IDispatch *disp;
1625 ScriptHost *iter;
1626 HRESULT hres;
1628 LIST_FOR_EACH_ENTRY(iter, &window->script_hosts, ScriptHost, entry) {
1629 disp = get_script_disp(iter);
1630 if(!disp)
1631 continue;
1633 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1634 if(SUCCEEDED(hres)) {
1635 hres = IDispatchEx_GetDispID(dispex, name, flags & (~fdexNameEnsure), ret_id);
1636 IDispatchEx_Release(dispex);
1637 }else {
1638 FIXME("No IDispatchEx\n");
1639 hres = E_NOTIMPL;
1642 IDispatch_Release(disp);
1643 if(SUCCEEDED(hres)) {
1644 *ret_host = iter;
1645 return TRUE;
1649 return FALSE;
1652 static BOOL is_jscript_available(void)
1654 static BOOL available, checked;
1656 if(!checked) {
1657 IUnknown *unk;
1658 HRESULT hres = CoGetClassObject(&CLSID_JScript, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void**)&unk);
1660 if(SUCCEEDED(hres)) {
1661 available = TRUE;
1662 IUnknown_Release(unk);
1663 }else {
1664 available = FALSE;
1666 checked = TRUE;
1669 return available;
1672 void set_script_mode(HTMLOuterWindow *window, SCRIPTMODE mode)
1674 nsIWebBrowserSetup *setup;
1675 nsresult nsres;
1677 if(mode == SCRIPTMODE_ACTIVESCRIPT && !is_jscript_available()) {
1678 TRACE("jscript.dll not available\n");
1679 window->scriptmode = SCRIPTMODE_GECKO;
1680 return;
1683 window->scriptmode = mode;
1685 if(!window->doc_obj->nscontainer || !window->doc_obj->nscontainer->webbrowser)
1686 return;
1688 nsres = nsIWebBrowser_QueryInterface(window->doc_obj->nscontainer->webbrowser,
1689 &IID_nsIWebBrowserSetup, (void**)&setup);
1690 if(NS_SUCCEEDED(nsres)) {
1691 nsres = nsIWebBrowserSetup_SetProperty(setup, SETUP_ALLOW_JAVASCRIPT,
1692 window->scriptmode == SCRIPTMODE_GECKO);
1694 if(NS_SUCCEEDED(nsres))
1695 nsres = nsIWebBrowserSetup_SetProperty(setup, SETUP_DISABLE_NOSCRIPT, TRUE);
1697 nsIWebBrowserSetup_Release(setup);
1700 if(NS_FAILED(nsres))
1701 ERR("JavaScript setup failed: %08x\n", nsres);
1704 void release_script_hosts(HTMLInnerWindow *window)
1706 script_queue_entry_t *queue_iter;
1707 ScriptHost *iter;
1709 while(!list_empty(&window->script_queue)) {
1710 queue_iter = LIST_ENTRY(list_head(&window->script_queue), script_queue_entry_t, entry);
1712 list_remove(&queue_iter->entry);
1713 IHTMLScriptElement_Release(&queue_iter->script->IHTMLScriptElement_iface);
1714 heap_free(queue_iter);
1717 while(!list_empty(&window->script_hosts)) {
1718 iter = LIST_ENTRY(list_head(&window->script_hosts), ScriptHost, entry);
1720 release_script_engine(iter);
1721 list_remove(&iter->entry);
1722 iter->window = NULL;
1723 IActiveScriptSite_Release(&iter->IActiveScriptSite_iface);