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
33 #include "wine/debug.h"
35 #include "mshtml_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
40 #define CTXARG_T DWORDLONG
41 #define IActiveScriptSiteDebugVtbl IActiveScriptSiteDebug64Vtbl
43 #define CTXARG_T DWORD
44 #define IActiveScriptSiteDebugVtbl IActiveScriptSiteDebug32Vtbl
47 static const WCHAR windowW[] = {'w','i','n','d','o','w',0};
48 static const WCHAR emptyW[] = {0};
50 static const CLSID CLSID_JScript =
51 {0xf414c260,0x6ac0,0x11cf,{0xb6,0xd1,0x00,0xaa,0x00,0xbb,0xbb,0x58}};
54 IActiveScriptSite IActiveScriptSite_iface;
55 IActiveScriptSiteInterruptPoll IActiveScriptSiteInterruptPoll_iface;
56 IActiveScriptSiteWindow IActiveScriptSiteWindow_iface;
57 IActiveScriptSiteDebug IActiveScriptSiteDebug_iface;
58 IServiceProvider IServiceProvider_iface;
62 IActiveScript *script;
63 IActiveScriptParse *parse;
64 IActiveScriptParseProcedure *parse_proc;
66 SCRIPTSTATE script_state;
74 static void set_script_prop(ScriptHost *script_host, DWORD property, VARIANT *val)
76 IActiveScriptProperty *script_prop;
79 hres = IActiveScript_QueryInterface(script_host->script, &IID_IActiveScriptProperty,
80 (void**)&script_prop);
82 WARN("Could not get IActiveScriptProperty iface: %08x\n", hres);
86 hres = IActiveScriptProperty_SetProperty(script_prop, property, NULL, val);
87 IActiveScriptProperty_Release(script_prop);
89 WARN("SetProperty(%x) failed: %08x\n", property, hres);
92 static BOOL init_script_engine(ScriptHost *script_host)
94 IObjectSafety *safety;
96 DWORD supported_opts=0, enabled_opts=0;
100 hres = IActiveScript_QueryInterface(script_host->script, &IID_IActiveScriptParse, (void**)&script_host->parse);
102 WARN("Could not get IActiveScriptHost: %08x\n", hres);
106 hres = IActiveScript_QueryInterface(script_host->script, &IID_IObjectSafety, (void**)&safety);
108 FIXME("Could not get IObjectSafety: %08x\n", hres);
112 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported_opts, &enabled_opts);
114 FIXME("GetInterfaceSafetyOptions failed: %08x\n", hres);
115 }else if(!(supported_opts & INTERFACE_USES_DISPEX)) {
116 FIXME("INTERFACE_USES_DISPEX is not supported\n");
118 hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse,
119 INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER,
120 INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER);
122 FIXME("SetInterfaceSafetyOptions failed: %08x\n", hres);
125 IObjectSafety_Release(safety);
131 set_script_prop(script_host, SCRIPTPROP_INVOKEVERSIONING, &var);
133 V_VT(&var) = VT_BOOL;
134 V_BOOL(&var) = VARIANT_TRUE;
135 set_script_prop(script_host, SCRIPTPROP_HACK_TRIDENTEVENTSINK, &var);
137 hres = IActiveScriptParse64_InitNew(script_host->parse);
139 WARN("InitNew failed: %08x\n", hres);
143 hres = IActiveScript_SetScriptSite(script_host->script, &script_host->IActiveScriptSite_iface);
145 WARN("SetScriptSite failed: %08x\n", hres);
146 IActiveScript_Close(script_host->script);
150 hres = IActiveScript_GetScriptState(script_host->script, &state);
152 WARN("GetScriptState failed: %08x\n", hres);
153 else if(state != SCRIPTSTATE_INITIALIZED)
154 FIXME("state = %x\n", state);
156 hres = IActiveScript_SetScriptState(script_host->script, SCRIPTSTATE_STARTED);
158 WARN("Starting script failed: %08x\n", hres);
162 hres = IActiveScript_AddNamedItem(script_host->script, windowW,
163 SCRIPTITEM_ISVISIBLE|SCRIPTITEM_ISSOURCE|SCRIPTITEM_GLOBALMEMBERS);
164 if(SUCCEEDED(hres)) {
165 V_VT(&var) = VT_BOOL;
166 V_BOOL(&var) = VARIANT_TRUE;
167 set_script_prop(script_host, SCRIPTPROP_ABBREVIATE_GLOBALNAME_RESOLUTION, &var);
169 WARN("AddNamedItem failed: %08x\n", hres);
172 hres = IActiveScript_QueryInterface(script_host->script, &IID_IActiveScriptParseProcedure2,
173 (void**)&script_host->parse_proc);
175 /* FIXME: QI for IActiveScriptParseProcedure */
176 WARN("Could not get IActiveScriptParseProcedure iface: %08x\n", hres);
182 static void release_script_engine(ScriptHost *This)
187 switch(This->script_state) {
188 case SCRIPTSTATE_CONNECTED:
189 IActiveScript_SetScriptState(This->script, SCRIPTSTATE_DISCONNECTED);
191 case SCRIPTSTATE_STARTED:
192 case SCRIPTSTATE_DISCONNECTED:
193 case SCRIPTSTATE_INITIALIZED:
194 IActiveScript_Close(This->script);
197 if(This->parse_proc) {
198 IUnknown_Release(This->parse_proc);
199 This->parse_proc = NULL;
203 IUnknown_Release(This->parse);
208 IActiveScript_Release(This->script);
210 This->script_state = SCRIPTSTATE_UNINITIALIZED;
213 void connect_scripts(HTMLWindow *window)
217 LIST_FOR_EACH_ENTRY(iter, &window->script_hosts, ScriptHost, entry) {
218 if(iter->script_state == SCRIPTSTATE_STARTED)
219 IActiveScript_SetScriptState(iter->script, SCRIPTSTATE_CONNECTED);
223 static inline ScriptHost *impl_from_IActiveScriptSite(IActiveScriptSite *iface)
225 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSite_iface);
228 static HRESULT WINAPI ActiveScriptSite_QueryInterface(IActiveScriptSite *iface, REFIID riid, void **ppv)
230 ScriptHost *This = impl_from_IActiveScriptSite(iface);
234 if(IsEqualGUID(&IID_IUnknown, riid)) {
235 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
236 *ppv = &This->IActiveScriptSite_iface;
237 }else if(IsEqualGUID(&IID_IActiveScriptSite, riid)) {
238 TRACE("(%p)->(IID_IActiveScriptSite %p)\n", This, ppv);
239 *ppv = &This->IActiveScriptSite_iface;
240 }else if(IsEqualGUID(&IID_IActiveScriptSiteInterruptPoll, riid)) {
241 TRACE("(%p)->(IID_IActiveScriptSiteInterruprtPoll %p)\n", This, ppv);
242 *ppv = &This->IActiveScriptSiteInterruptPoll_iface;
243 }else if(IsEqualGUID(&IID_IActiveScriptSiteWindow, riid)) {
244 TRACE("(%p)->(IID_IActiveScriptSiteWindow %p)\n", This, ppv);
245 *ppv = &This->IActiveScriptSiteWindow_iface;
246 }else if(IsEqualGUID(&IID_IActiveScriptSiteDebug, riid)) {
247 TRACE("(%p)->(IID_IActiveScriptSiteDebug %p)\n", This, ppv);
248 *ppv = &This->IActiveScriptSiteDebug_iface;
249 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
250 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
251 *ppv = &This->IServiceProvider_iface;
252 }else if(IsEqualGUID(&IID_ICanHandleException, riid)) {
253 TRACE("(%p)->(IID_ICanHandleException not supported %p)\n", This, ppv);
254 return E_NOINTERFACE;
256 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
257 return E_NOINTERFACE;
260 IUnknown_AddRef((IUnknown*)*ppv);
264 static ULONG WINAPI ActiveScriptSite_AddRef(IActiveScriptSite *iface)
266 ScriptHost *This = impl_from_IActiveScriptSite(iface);
267 LONG ref = InterlockedIncrement(&This->ref);
269 TRACE("(%p) ref=%d\n", This, ref);
274 static ULONG WINAPI ActiveScriptSite_Release(IActiveScriptSite *iface)
276 ScriptHost *This = impl_from_IActiveScriptSite(iface);
277 LONG ref = InterlockedDecrement(&This->ref);
279 TRACE("(%p) ref=%d\n", This, ref);
282 release_script_engine(This);
284 list_remove(&This->entry);
291 static HRESULT WINAPI ActiveScriptSite_GetLCID(IActiveScriptSite *iface, LCID *plcid)
293 ScriptHost *This = impl_from_IActiveScriptSite(iface);
295 TRACE("(%p)->(%p)\n", This, plcid);
297 *plcid = GetUserDefaultLCID();
301 static HRESULT WINAPI ActiveScriptSite_GetItemInfo(IActiveScriptSite *iface, LPCOLESTR pstrName,
302 DWORD dwReturnMask, IUnknown **ppiunkItem, ITypeInfo **ppti)
304 ScriptHost *This = impl_from_IActiveScriptSite(iface);
306 TRACE("(%p)->(%s %x %p %p)\n", This, debugstr_w(pstrName), dwReturnMask, ppiunkItem, ppti);
308 if(dwReturnMask != SCRIPTINFO_IUNKNOWN) {
309 FIXME("Unsupported mask %x\n", dwReturnMask);
315 if(strcmpW(pstrName, windowW))
316 return DISP_E_MEMBERNOTFOUND;
321 /* FIXME: Return proxy object */
322 *ppiunkItem = (IUnknown*)&This->window->IHTMLWindow2_iface;
323 IUnknown_AddRef(*ppiunkItem);
328 static HRESULT WINAPI ActiveScriptSite_GetDocVersionString(IActiveScriptSite *iface, BSTR *pbstrVersion)
330 ScriptHost *This = impl_from_IActiveScriptSite(iface);
331 FIXME("(%p)->(%p)\n", This, pbstrVersion);
335 static HRESULT WINAPI ActiveScriptSite_OnScriptTerminate(IActiveScriptSite *iface,
336 const VARIANT *pvarResult, const EXCEPINFO *pexcepinfo)
338 ScriptHost *This = impl_from_IActiveScriptSite(iface);
339 FIXME("(%p)->(%p %p)\n", This, pvarResult, pexcepinfo);
343 static HRESULT WINAPI ActiveScriptSite_OnStateChange(IActiveScriptSite *iface, SCRIPTSTATE ssScriptState)
345 ScriptHost *This = impl_from_IActiveScriptSite(iface);
347 TRACE("(%p)->(%x)\n", This, ssScriptState);
349 This->script_state = ssScriptState;
353 static HRESULT WINAPI ActiveScriptSite_OnScriptError(IActiveScriptSite *iface, IActiveScriptError *pscripterror)
355 ScriptHost *This = impl_from_IActiveScriptSite(iface);
356 FIXME("(%p)->(%p)\n", This, pscripterror);
360 static HRESULT WINAPI ActiveScriptSite_OnEnterScript(IActiveScriptSite *iface)
362 ScriptHost *This = impl_from_IActiveScriptSite(iface);
364 TRACE("(%p)->()\n", This);
369 static HRESULT WINAPI ActiveScriptSite_OnLeaveScript(IActiveScriptSite *iface)
371 ScriptHost *This = impl_from_IActiveScriptSite(iface);
373 TRACE("(%p)->()\n", This);
378 static const IActiveScriptSiteVtbl ActiveScriptSiteVtbl = {
379 ActiveScriptSite_QueryInterface,
380 ActiveScriptSite_AddRef,
381 ActiveScriptSite_Release,
382 ActiveScriptSite_GetLCID,
383 ActiveScriptSite_GetItemInfo,
384 ActiveScriptSite_GetDocVersionString,
385 ActiveScriptSite_OnScriptTerminate,
386 ActiveScriptSite_OnStateChange,
387 ActiveScriptSite_OnScriptError,
388 ActiveScriptSite_OnEnterScript,
389 ActiveScriptSite_OnLeaveScript
392 static inline ScriptHost *impl_from_IActiveScriptSiteInterruptPoll(IActiveScriptSiteInterruptPoll *iface)
394 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSiteInterruptPoll_iface);
397 static HRESULT WINAPI ActiveScriptSiteInterruptPoll_QueryInterface(IActiveScriptSiteInterruptPoll *iface,
398 REFIID riid, void **ppv)
400 ScriptHost *This = impl_from_IActiveScriptSiteInterruptPoll(iface);
401 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
404 static ULONG WINAPI ActiveScriptSiteInterruptPoll_AddRef(IActiveScriptSiteInterruptPoll *iface)
406 ScriptHost *This = impl_from_IActiveScriptSiteInterruptPoll(iface);
407 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
410 static ULONG WINAPI ActiveScriptSiteInterruptPoll_Release(IActiveScriptSiteInterruptPoll *iface)
412 ScriptHost *This = impl_from_IActiveScriptSiteInterruptPoll(iface);
413 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
416 static HRESULT WINAPI ActiveScriptSiteInterruptPoll_QueryContinue(IActiveScriptSiteInterruptPoll *iface)
418 ScriptHost *This = impl_from_IActiveScriptSiteInterruptPoll(iface);
420 TRACE("(%p)\n", This);
425 static const IActiveScriptSiteInterruptPollVtbl ActiveScriptSiteInterruptPollVtbl = {
426 ActiveScriptSiteInterruptPoll_QueryInterface,
427 ActiveScriptSiteInterruptPoll_AddRef,
428 ActiveScriptSiteInterruptPoll_Release,
429 ActiveScriptSiteInterruptPoll_QueryContinue
432 static inline ScriptHost *impl_from_IActiveScriptSiteWindow(IActiveScriptSiteWindow *iface)
434 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSiteWindow_iface);
437 static HRESULT WINAPI ActiveScriptSiteWindow_QueryInterface(IActiveScriptSiteWindow *iface,
438 REFIID riid, void **ppv)
440 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
441 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
444 static ULONG WINAPI ActiveScriptSiteWindow_AddRef(IActiveScriptSiteWindow *iface)
446 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
447 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
450 static ULONG WINAPI ActiveScriptSiteWindow_Release(IActiveScriptSiteWindow *iface)
452 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
453 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
456 static HRESULT WINAPI ActiveScriptSiteWindow_GetWindow(IActiveScriptSiteWindow *iface, HWND *phwnd)
458 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
459 FIXME("(%p)->(%p)\n", This, phwnd);
463 static HRESULT WINAPI ActiveScriptSiteWindow_EnableModeless(IActiveScriptSiteWindow *iface, BOOL fEnable)
465 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
466 FIXME("(%p)->(%x)\n", This, fEnable);
470 static const IActiveScriptSiteWindowVtbl ActiveScriptSiteWindowVtbl = {
471 ActiveScriptSiteWindow_QueryInterface,
472 ActiveScriptSiteWindow_AddRef,
473 ActiveScriptSiteWindow_Release,
474 ActiveScriptSiteWindow_GetWindow,
475 ActiveScriptSiteWindow_EnableModeless
478 static inline ScriptHost *impl_from_IActiveScriptSiteDebug(IActiveScriptSiteDebug *iface)
480 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSiteDebug_iface);
483 static HRESULT WINAPI ActiveScriptSiteDebug_QueryInterface(IActiveScriptSiteDebug *iface,
484 REFIID riid, void **ppv)
486 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
487 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
490 static ULONG WINAPI ActiveScriptSiteDebug_AddRef(IActiveScriptSiteDebug *iface)
492 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
493 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
496 static ULONG WINAPI ActiveScriptSiteDebug_Release(IActiveScriptSiteDebug *iface)
498 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
499 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
502 static HRESULT WINAPI ActiveScriptSiteDebug_GetDocumentContextFromPosition(IActiveScriptSiteDebug *iface,
503 CTXARG_T dwSourceContext, ULONG uCharacterOffset, ULONG uNumChars, IDebugDocumentContext **ppsc)
505 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
506 FIXME("(%p)->(%s %u %u %p)\n", This, wine_dbgstr_longlong(dwSourceContext), uCharacterOffset,
511 static HRESULT WINAPI ActiveScriptSiteDebug_GetApplication(IActiveScriptSiteDebug *iface, IDebugApplication **ppda)
513 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
514 FIXME("(%p)->(%p)\n", This, ppda);
518 static HRESULT WINAPI ActiveScriptSiteDebug_GetRootApplicationNode(IActiveScriptSiteDebug *iface,
519 IDebugApplicationNode **ppdanRoot)
521 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
522 FIXME("(%p)->(%p)\n", This, ppdanRoot);
526 static HRESULT WINAPI ActiveScriptSiteDebug_OnScriptErrorDebug(IActiveScriptSiteDebug *iface,
527 IActiveScriptErrorDebug *pErrorDebug, BOOL *pfEnterDebugger, BOOL *pfCallOnScriptErrorWhenContinuing)
529 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
530 FIXME("(%p)->(%p %p %p)\n", This, pErrorDebug, pfEnterDebugger, pfCallOnScriptErrorWhenContinuing);
534 static const IActiveScriptSiteDebugVtbl ActiveScriptSiteDebugVtbl = {
535 ActiveScriptSiteDebug_QueryInterface,
536 ActiveScriptSiteDebug_AddRef,
537 ActiveScriptSiteDebug_Release,
538 ActiveScriptSiteDebug_GetDocumentContextFromPosition,
539 ActiveScriptSiteDebug_GetApplication,
540 ActiveScriptSiteDebug_GetRootApplicationNode,
541 ActiveScriptSiteDebug_OnScriptErrorDebug
544 static inline ScriptHost *impl_from_IServiceProvider(IServiceProvider *iface)
546 return CONTAINING_RECORD(iface, ScriptHost, IServiceProvider_iface);
549 static HRESULT WINAPI ASServiceProvider_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
551 ScriptHost *This = impl_from_IServiceProvider(iface);
552 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
555 static ULONG WINAPI ASServiceProvider_AddRef(IServiceProvider *iface)
557 ScriptHost *This = impl_from_IServiceProvider(iface);
558 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
561 static ULONG WINAPI ASServiceProvider_Release(IServiceProvider *iface)
563 ScriptHost *This = impl_from_IServiceProvider(iface);
564 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
567 static HRESULT WINAPI ASServiceProvider_QueryService(IServiceProvider *iface, REFGUID guidService,
568 REFIID riid, void **ppv)
570 ScriptHost *This = impl_from_IServiceProvider(iface);
572 if(IsEqualGUID(&SID_SInternetHostSecurityManager, guidService)) {
573 TRACE("(%p)->(SID_SInternetHostSecurityManager)\n", This);
575 if(!This->window || !This->window->doc)
576 return E_NOINTERFACE;
578 return IInternetHostSecurityManager_QueryInterface(&This->window->doc->IInternetHostSecurityManager_iface,
582 FIXME("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
583 return E_NOINTERFACE;
586 static const IServiceProviderVtbl ASServiceProviderVtbl = {
587 ASServiceProvider_QueryInterface,
588 ASServiceProvider_AddRef,
589 ASServiceProvider_Release,
590 ASServiceProvider_QueryService
593 static ScriptHost *create_script_host(HTMLWindow *window, const GUID *guid)
598 ret = heap_alloc_zero(sizeof(*ret));
599 ret->IActiveScriptSite_iface.lpVtbl = &ActiveScriptSiteVtbl;
600 ret->IActiveScriptSiteInterruptPoll_iface.lpVtbl = &ActiveScriptSiteInterruptPollVtbl;
601 ret->IActiveScriptSiteWindow_iface.lpVtbl = &ActiveScriptSiteWindowVtbl;
602 ret->IActiveScriptSiteDebug_iface.lpVtbl = &ActiveScriptSiteDebugVtbl;
603 ret->IServiceProvider_iface.lpVtbl = &ASServiceProviderVtbl;
605 ret->window = window;
606 ret->script_state = SCRIPTSTATE_UNINITIALIZED;
609 list_add_tail(&window->script_hosts, &ret->entry);
611 hres = CoCreateInstance(&ret->guid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
612 &IID_IActiveScript, (void**)&ret->script);
614 WARN("Could not load script engine: %08x\n", hres);
615 else if(!init_script_engine(ret))
616 release_script_engine(ret);
621 static void parse_text(ScriptHost *script_host, LPCWSTR text)
627 static const WCHAR script_endW[] = {'<','/','S','C','R','I','P','T','>',0};
629 TRACE("%s\n", debugstr_w(text));
632 memset(&excepinfo, 0, sizeof(excepinfo));
634 hres = IActiveScriptParse64_ParseScriptText(script_host->parse, text, windowW, NULL, script_endW,
635 0, 0, SCRIPTTEXT_ISVISIBLE|SCRIPTTEXT_HOSTMANAGESSOURCE,
640 WARN("<<< %08x\n", hres);
644 static void parse_extern_script(ScriptHost *script_host, LPCWSTR src)
652 static const WCHAR wine_schemaW[] = {'w','i','n','e',':'};
654 if(strlenW(src) > sizeof(wine_schemaW)/sizeof(WCHAR) && !memcmp(src, wine_schemaW, sizeof(wine_schemaW)))
655 src += sizeof(wine_schemaW)/sizeof(WCHAR);
657 hres = CreateURLMoniker(NULL, src, &mon);
661 hres = bind_mon_to_buffer(script_host->window->doc, mon, (void**)&buf, &size);
662 IMoniker_Release(mon);
666 len = MultiByteToWideChar(CP_ACP, 0, buf, size, NULL, 0);
667 text = heap_alloc((len+1)*sizeof(WCHAR));
668 MultiByteToWideChar(CP_ACP, 0, buf, size, text, len);
672 parse_text(script_host, text);
677 static void parse_inline_script(ScriptHost *script_host, nsIDOMHTMLScriptElement *nsscript)
679 const PRUnichar *text;
683 nsAString_Init(&text_str, NULL);
685 nsres = nsIDOMHTMLScriptElement_GetText(nsscript, &text_str);
687 if(NS_SUCCEEDED(nsres)) {
688 nsAString_GetData(&text_str, &text);
689 parse_text(script_host, text);
691 ERR("GetText failed: %08x\n", nsres);
694 nsAString_Finish(&text_str);
697 static void parse_script_elem(ScriptHost *script_host, nsIDOMHTMLScriptElement *nsscript)
699 const PRUnichar *src;
703 nsAString_Init(&src_str, NULL);
705 nsres = nsIDOMHTMLScriptElement_GetSrc(nsscript, &src_str);
706 nsAString_GetData(&src_str, &src);
709 ERR("GetSrc failed: %08x\n", nsres);
711 parse_extern_script(script_host, src);
713 parse_inline_script(script_host, nsscript);
715 nsAString_Finish(&src_str);
718 static BOOL get_guid_from_type(LPCWSTR type, GUID *guid)
720 const WCHAR text_javascriptW[] =
721 {'t','e','x','t','/','j','a','v','a','s','c','r','i','p','t',0};
723 /* FIXME: Handle more types */
724 if(!strcmpiW(type, text_javascriptW)) {
725 *guid = CLSID_JScript;
727 FIXME("Unknown type %s\n", debugstr_w(type));
734 static BOOL get_guid_from_language(LPCWSTR type, GUID *guid)
738 hres = CLSIDFromProgID(type, guid);
742 /* FIXME: Check CATID_ActiveScriptParse */
747 static BOOL get_script_guid(nsIDOMHTMLScriptElement *nsscript, GUID *guid)
749 nsAString attr_str, val_str;
753 static const PRUnichar languageW[] = {'l','a','n','g','u','a','g','e',0};
755 nsAString_Init(&val_str, NULL);
757 nsres = nsIDOMHTMLScriptElement_GetType(nsscript, &val_str);
758 if(NS_SUCCEEDED(nsres)) {
759 const PRUnichar *type;
761 nsAString_GetData(&val_str, &type);
763 ret = get_guid_from_type(type, guid);
764 nsAString_Finish(&val_str);
768 ERR("GetType failed: %08x\n", nsres);
771 nsAString_InitDepend(&attr_str, languageW);
772 nsres = nsIDOMHTMLScriptElement_GetAttribute(nsscript, &attr_str, &val_str);
773 nsAString_Finish(&attr_str);
774 if(NS_SUCCEEDED(nsres)) {
775 const PRUnichar *language;
777 nsAString_GetData(&val_str, &language);
780 ret = get_guid_from_language(language, guid);
782 *guid = CLSID_JScript;
786 ERR("GetAttribute(language) failed: %08x\n", nsres);
789 nsAString_Finish(&val_str);
794 static ScriptHost *get_script_host(HTMLWindow *window, const GUID *guid)
798 if(IsEqualGUID(&CLSID_JScript, guid) && window->scriptmode != SCRIPTMODE_ACTIVESCRIPT) {
799 TRACE("Ignoring JScript\n");
803 LIST_FOR_EACH_ENTRY(iter, &window->script_hosts, ScriptHost, entry) {
804 if(IsEqualGUID(guid, &iter->guid))
808 return create_script_host(window, guid);
811 void doc_insert_script(HTMLWindow *window, nsIDOMHTMLScriptElement *nsscript)
813 ScriptHost *script_host;
816 if(!get_script_guid(nsscript, &guid)) {
817 WARN("Could not find script GUID\n");
821 script_host = get_script_host(window, &guid);
825 if(script_host->parse)
826 parse_script_elem(script_host, nsscript);
829 IDispatch *script_parse_event(HTMLWindow *window, LPCWSTR text)
831 ScriptHost *script_host;
832 GUID guid = CLSID_JScript;
837 static const WCHAR delimiterW[] = {'\"',0};
839 for(ptr = text; isalnumW(*ptr); ptr++);
844 language = heap_alloc((ptr-text+1)*sizeof(WCHAR));
845 memcpy(language, text, (ptr-text)*sizeof(WCHAR));
846 language[ptr-text] = 0;
848 b = get_guid_from_language(language, &guid);
853 WARN("Could not find language\n");
862 script_host = get_script_host(window, &guid);
863 if(!script_host || !script_host->parse_proc)
866 hres = IActiveScriptParseProcedure64_ParseProcedureText(script_host->parse_proc, ptr, NULL, emptyW,
867 NULL, NULL, delimiterW, 0 /* FIXME */, 0,
868 SCRIPTPROC_HOSTMANAGESSOURCE|SCRIPTPROC_IMPLICIT_THIS|SCRIPTPROC_IMPLICIT_PARENTS, &disp);
870 WARN("ParseProcedureText failed: %08x\n", hres);
874 TRACE("ret %p\n", disp);
878 HRESULT exec_script(HTMLWindow *window, const WCHAR *code, const WCHAR *lang, VARIANT *ret)
880 ScriptHost *script_host;
885 static const WCHAR delimW[] = {'"',0};
887 if(!get_guid_from_language(lang, &guid)) {
888 WARN("Could not find script GUID\n");
889 return CO_E_CLASSSTRING;
892 script_host = get_script_host(window, &guid);
894 FIXME("No script host\n");
898 if(!script_host->parse) {
899 FIXME("script_host->parse == NULL\n");
903 memset(&ei, 0, sizeof(ei));
905 hres = IActiveScriptParse64_ParseScriptText(script_host->parse, code, NULL, NULL, delimW, 0, 0, SCRIPTTEXT_ISVISIBLE, ret, &ei);
909 WARN("<<< %08x\n", hres);
914 IDispatch *get_script_disp(ScriptHost *script_host)
919 if(!script_host->script)
922 hres = IActiveScript_GetScriptDispatch(script_host->script, windowW, &disp);
929 BOOL find_global_prop(HTMLWindow *window, BSTR name, DWORD flags, ScriptHost **ret_host, DISPID *ret_id)
936 LIST_FOR_EACH_ENTRY(iter, &window->script_hosts, ScriptHost, entry) {
937 disp = get_script_disp(iter);
941 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
942 if(SUCCEEDED(hres)) {
943 hres = IDispatchEx_GetDispID(dispex, name, flags & (~fdexNameEnsure), ret_id);
944 IDispatchEx_Release(dispex);
946 FIXME("No IDispatchEx\n");
950 IDispatch_Release(disp);
951 if(SUCCEEDED(hres)) {
960 static BOOL is_jscript_available(void)
962 static BOOL available, checked;
966 HRESULT hres = CoGetClassObject(&CLSID_JScript, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void**)&unk);
968 if(SUCCEEDED(hres)) {
970 IUnknown_Release(unk);
980 void set_script_mode(HTMLWindow *window, SCRIPTMODE mode)
982 nsIWebBrowserSetup *setup;
985 if(mode == SCRIPTMODE_ACTIVESCRIPT && !is_jscript_available()) {
986 TRACE("jscript.dll not available\n");
987 window->scriptmode = SCRIPTMODE_GECKO;
991 window->scriptmode = mode;
993 if(!window->doc_obj->nscontainer || !window->doc_obj->nscontainer->webbrowser)
996 nsres = nsIWebBrowser_QueryInterface(window->doc_obj->nscontainer->webbrowser,
997 &IID_nsIWebBrowserSetup, (void**)&setup);
998 if(NS_SUCCEEDED(nsres)) {
999 nsres = nsIWebBrowserSetup_SetProperty(setup, SETUP_ALLOW_JAVASCRIPT,
1000 window->scriptmode == SCRIPTMODE_GECKO);
1001 nsIWebBrowserSetup_Release(setup);
1004 if(NS_FAILED(nsres))
1005 ERR("JavaScript setup failed: %08x\n", nsres);
1008 void release_script_hosts(HTMLWindow *window)
1012 while(!list_empty(&window->script_hosts)) {
1013 iter = LIST_ENTRY(list_head(&window->script_hosts), ScriptHost, entry);
1015 release_script_engine(iter);
1016 list_remove(&iter->entry);
1017 iter->window = NULL;
1018 IActiveScript_Release(&iter->IActiveScriptSite_iface);