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
32 #include "wine/debug.h"
34 #include "mshtml_private.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
39 #define CTXARG_T DWORDLONG
40 #define IActiveScriptSiteDebugVtbl IActiveScriptSiteDebug64Vtbl
42 #define CTXARG_T DWORD
43 #define IActiveScriptSiteDebugVtbl IActiveScriptSiteDebug32Vtbl
46 static const WCHAR windowW[] = {'w','i','n','d','o','w',0};
47 static const WCHAR emptyW[] = {0};
49 static const CLSID CLSID_JScript =
50 {0xf414c260,0x6ac0,0x11cf,{0xb6,0xd1,0x00,0xaa,0x00,0xbb,0xbb,0x58}};
51 static const CLSID CLSID_VBScript =
52 {0xb54f3741,0x5b07,0x11cf,{0xa4,0xb0,0x00,0xaa,0x00,0x4a,0x55,0xe8}};
55 IActiveScriptSite IActiveScriptSite_iface;
56 IActiveScriptSiteInterruptPoll IActiveScriptSiteInterruptPoll_iface;
57 IActiveScriptSiteWindow IActiveScriptSiteWindow_iface;
58 IActiveScriptSiteDebug IActiveScriptSiteDebug_iface;
59 IServiceProvider IServiceProvider_iface;
63 IActiveScript *script;
64 IActiveScriptParse *parse;
65 IActiveScriptParseProcedure *parse_proc;
67 SCRIPTSTATE script_state;
69 HTMLOuterWindow *window;
75 static void set_script_prop(ScriptHost *script_host, DWORD property, VARIANT *val)
77 IActiveScriptProperty *script_prop;
80 hres = IActiveScript_QueryInterface(script_host->script, &IID_IActiveScriptProperty,
81 (void**)&script_prop);
83 WARN("Could not get IActiveScriptProperty iface: %08x\n", hres);
87 hres = IActiveScriptProperty_SetProperty(script_prop, property, NULL, val);
88 IActiveScriptProperty_Release(script_prop);
90 WARN("SetProperty(%x) failed: %08x\n", property, hres);
93 static BOOL init_script_engine(ScriptHost *script_host)
95 IObjectSafety *safety;
97 DWORD supported_opts=0, enabled_opts=0;
101 hres = IActiveScript_QueryInterface(script_host->script, &IID_IActiveScriptParse, (void**)&script_host->parse);
103 WARN("Could not get IActiveScriptHost: %08x\n", hres);
107 hres = IActiveScript_QueryInterface(script_host->script, &IID_IObjectSafety, (void**)&safety);
109 FIXME("Could not get IObjectSafety: %08x\n", hres);
113 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported_opts, &enabled_opts);
115 FIXME("GetInterfaceSafetyOptions failed: %08x\n", hres);
116 }else if(!(supported_opts & INTERFACE_USES_DISPEX)) {
117 FIXME("INTERFACE_USES_DISPEX is not supported\n");
119 hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse,
120 INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER,
121 INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER);
123 FIXME("SetInterfaceSafetyOptions failed: %08x\n", hres);
126 IObjectSafety_Release(safety);
132 set_script_prop(script_host, SCRIPTPROP_INVOKEVERSIONING, &var);
134 V_VT(&var) = VT_BOOL;
135 V_BOOL(&var) = VARIANT_TRUE;
136 set_script_prop(script_host, SCRIPTPROP_HACK_TRIDENTEVENTSINK, &var);
138 hres = IActiveScriptParse64_InitNew(script_host->parse);
140 WARN("InitNew failed: %08x\n", hres);
144 hres = IActiveScript_SetScriptSite(script_host->script, &script_host->IActiveScriptSite_iface);
146 WARN("SetScriptSite failed: %08x\n", hres);
147 IActiveScript_Close(script_host->script);
151 hres = IActiveScript_GetScriptState(script_host->script, &state);
153 WARN("GetScriptState failed: %08x\n", hres);
154 else if(state != SCRIPTSTATE_INITIALIZED)
155 FIXME("state = %x\n", state);
157 hres = IActiveScript_SetScriptState(script_host->script, SCRIPTSTATE_STARTED);
159 WARN("Starting script failed: %08x\n", hres);
163 hres = IActiveScript_AddNamedItem(script_host->script, windowW,
164 SCRIPTITEM_ISVISIBLE|SCRIPTITEM_ISSOURCE|SCRIPTITEM_GLOBALMEMBERS);
165 if(SUCCEEDED(hres)) {
166 V_VT(&var) = VT_BOOL;
167 V_BOOL(&var) = VARIANT_TRUE;
168 set_script_prop(script_host, SCRIPTPROP_ABBREVIATE_GLOBALNAME_RESOLUTION, &var);
170 WARN("AddNamedItem failed: %08x\n", hres);
173 hres = IActiveScript_QueryInterface(script_host->script, &IID_IActiveScriptParseProcedure2,
174 (void**)&script_host->parse_proc);
176 /* FIXME: QI for IActiveScriptParseProcedure */
177 WARN("Could not get IActiveScriptParseProcedure iface: %08x\n", hres);
183 static void release_script_engine(ScriptHost *This)
188 switch(This->script_state) {
189 case SCRIPTSTATE_CONNECTED:
190 IActiveScript_SetScriptState(This->script, SCRIPTSTATE_DISCONNECTED);
192 case SCRIPTSTATE_STARTED:
193 case SCRIPTSTATE_DISCONNECTED:
194 case SCRIPTSTATE_INITIALIZED:
195 IActiveScript_Close(This->script);
198 if(This->parse_proc) {
199 IUnknown_Release(This->parse_proc);
200 This->parse_proc = NULL;
204 IUnknown_Release(This->parse);
209 IActiveScript_Release(This->script);
211 This->script_state = SCRIPTSTATE_UNINITIALIZED;
214 void connect_scripts(HTMLOuterWindow *window)
218 LIST_FOR_EACH_ENTRY(iter, &window->script_hosts, ScriptHost, entry) {
219 if(iter->script_state == SCRIPTSTATE_STARTED)
220 IActiveScript_SetScriptState(iter->script, SCRIPTSTATE_CONNECTED);
224 static inline ScriptHost *impl_from_IActiveScriptSite(IActiveScriptSite *iface)
226 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSite_iface);
229 static HRESULT WINAPI ActiveScriptSite_QueryInterface(IActiveScriptSite *iface, REFIID riid, void **ppv)
231 ScriptHost *This = impl_from_IActiveScriptSite(iface);
235 if(IsEqualGUID(&IID_IUnknown, riid)) {
236 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
237 *ppv = &This->IActiveScriptSite_iface;
238 }else if(IsEqualGUID(&IID_IActiveScriptSite, riid)) {
239 TRACE("(%p)->(IID_IActiveScriptSite %p)\n", This, ppv);
240 *ppv = &This->IActiveScriptSite_iface;
241 }else if(IsEqualGUID(&IID_IActiveScriptSiteInterruptPoll, riid)) {
242 TRACE("(%p)->(IID_IActiveScriptSiteInterruprtPoll %p)\n", This, ppv);
243 *ppv = &This->IActiveScriptSiteInterruptPoll_iface;
244 }else if(IsEqualGUID(&IID_IActiveScriptSiteWindow, riid)) {
245 TRACE("(%p)->(IID_IActiveScriptSiteWindow %p)\n", This, ppv);
246 *ppv = &This->IActiveScriptSiteWindow_iface;
247 }else if(IsEqualGUID(&IID_IActiveScriptSiteDebug, riid)) {
248 TRACE("(%p)->(IID_IActiveScriptSiteDebug %p)\n", This, ppv);
249 *ppv = &This->IActiveScriptSiteDebug_iface;
250 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
251 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
252 *ppv = &This->IServiceProvider_iface;
253 }else if(IsEqualGUID(&IID_ICanHandleException, riid)) {
254 TRACE("(%p)->(IID_ICanHandleException not supported %p)\n", This, ppv);
255 return E_NOINTERFACE;
257 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
258 return E_NOINTERFACE;
261 IUnknown_AddRef((IUnknown*)*ppv);
265 static ULONG WINAPI ActiveScriptSite_AddRef(IActiveScriptSite *iface)
267 ScriptHost *This = impl_from_IActiveScriptSite(iface);
268 LONG ref = InterlockedIncrement(&This->ref);
270 TRACE("(%p) ref=%d\n", This, ref);
275 static ULONG WINAPI ActiveScriptSite_Release(IActiveScriptSite *iface)
277 ScriptHost *This = impl_from_IActiveScriptSite(iface);
278 LONG ref = InterlockedDecrement(&This->ref);
280 TRACE("(%p) ref=%d\n", This, ref);
283 release_script_engine(This);
285 list_remove(&This->entry);
292 static HRESULT WINAPI ActiveScriptSite_GetLCID(IActiveScriptSite *iface, LCID *plcid)
294 ScriptHost *This = impl_from_IActiveScriptSite(iface);
296 TRACE("(%p)->(%p)\n", This, plcid);
298 *plcid = GetUserDefaultLCID();
302 static HRESULT WINAPI ActiveScriptSite_GetItemInfo(IActiveScriptSite *iface, LPCOLESTR pstrName,
303 DWORD dwReturnMask, IUnknown **ppiunkItem, ITypeInfo **ppti)
305 ScriptHost *This = impl_from_IActiveScriptSite(iface);
307 TRACE("(%p)->(%s %x %p %p)\n", This, debugstr_w(pstrName), dwReturnMask, ppiunkItem, ppti);
309 if(dwReturnMask != SCRIPTINFO_IUNKNOWN) {
310 FIXME("Unsupported mask %x\n", dwReturnMask);
316 if(strcmpW(pstrName, windowW))
317 return DISP_E_MEMBERNOTFOUND;
322 /* FIXME: Return proxy object */
323 *ppiunkItem = (IUnknown*)&This->window->base.IHTMLWindow2_iface;
324 IUnknown_AddRef(*ppiunkItem);
329 static HRESULT WINAPI ActiveScriptSite_GetDocVersionString(IActiveScriptSite *iface, BSTR *pbstrVersion)
331 ScriptHost *This = impl_from_IActiveScriptSite(iface);
332 FIXME("(%p)->(%p)\n", This, pbstrVersion);
336 static HRESULT WINAPI ActiveScriptSite_OnScriptTerminate(IActiveScriptSite *iface,
337 const VARIANT *pvarResult, const EXCEPINFO *pexcepinfo)
339 ScriptHost *This = impl_from_IActiveScriptSite(iface);
340 FIXME("(%p)->(%p %p)\n", This, pvarResult, pexcepinfo);
344 static HRESULT WINAPI ActiveScriptSite_OnStateChange(IActiveScriptSite *iface, SCRIPTSTATE ssScriptState)
346 ScriptHost *This = impl_from_IActiveScriptSite(iface);
348 TRACE("(%p)->(%x)\n", This, ssScriptState);
350 This->script_state = ssScriptState;
354 static HRESULT WINAPI ActiveScriptSite_OnScriptError(IActiveScriptSite *iface, IActiveScriptError *pscripterror)
356 ScriptHost *This = impl_from_IActiveScriptSite(iface);
357 FIXME("(%p)->(%p)\n", This, pscripterror);
361 static HRESULT WINAPI ActiveScriptSite_OnEnterScript(IActiveScriptSite *iface)
363 ScriptHost *This = impl_from_IActiveScriptSite(iface);
365 TRACE("(%p)->()\n", This);
370 static HRESULT WINAPI ActiveScriptSite_OnLeaveScript(IActiveScriptSite *iface)
372 ScriptHost *This = impl_from_IActiveScriptSite(iface);
374 TRACE("(%p)->()\n", This);
379 static const IActiveScriptSiteVtbl ActiveScriptSiteVtbl = {
380 ActiveScriptSite_QueryInterface,
381 ActiveScriptSite_AddRef,
382 ActiveScriptSite_Release,
383 ActiveScriptSite_GetLCID,
384 ActiveScriptSite_GetItemInfo,
385 ActiveScriptSite_GetDocVersionString,
386 ActiveScriptSite_OnScriptTerminate,
387 ActiveScriptSite_OnStateChange,
388 ActiveScriptSite_OnScriptError,
389 ActiveScriptSite_OnEnterScript,
390 ActiveScriptSite_OnLeaveScript
393 static inline ScriptHost *impl_from_IActiveScriptSiteInterruptPoll(IActiveScriptSiteInterruptPoll *iface)
395 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSiteInterruptPoll_iface);
398 static HRESULT WINAPI ActiveScriptSiteInterruptPoll_QueryInterface(IActiveScriptSiteInterruptPoll *iface,
399 REFIID riid, void **ppv)
401 ScriptHost *This = impl_from_IActiveScriptSiteInterruptPoll(iface);
402 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
405 static ULONG WINAPI ActiveScriptSiteInterruptPoll_AddRef(IActiveScriptSiteInterruptPoll *iface)
407 ScriptHost *This = impl_from_IActiveScriptSiteInterruptPoll(iface);
408 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
411 static ULONG WINAPI ActiveScriptSiteInterruptPoll_Release(IActiveScriptSiteInterruptPoll *iface)
413 ScriptHost *This = impl_from_IActiveScriptSiteInterruptPoll(iface);
414 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
417 static HRESULT WINAPI ActiveScriptSiteInterruptPoll_QueryContinue(IActiveScriptSiteInterruptPoll *iface)
419 ScriptHost *This = impl_from_IActiveScriptSiteInterruptPoll(iface);
421 TRACE("(%p)\n", This);
426 static const IActiveScriptSiteInterruptPollVtbl ActiveScriptSiteInterruptPollVtbl = {
427 ActiveScriptSiteInterruptPoll_QueryInterface,
428 ActiveScriptSiteInterruptPoll_AddRef,
429 ActiveScriptSiteInterruptPoll_Release,
430 ActiveScriptSiteInterruptPoll_QueryContinue
433 static inline ScriptHost *impl_from_IActiveScriptSiteWindow(IActiveScriptSiteWindow *iface)
435 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSiteWindow_iface);
438 static HRESULT WINAPI ActiveScriptSiteWindow_QueryInterface(IActiveScriptSiteWindow *iface,
439 REFIID riid, void **ppv)
441 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
442 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
445 static ULONG WINAPI ActiveScriptSiteWindow_AddRef(IActiveScriptSiteWindow *iface)
447 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
448 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
451 static ULONG WINAPI ActiveScriptSiteWindow_Release(IActiveScriptSiteWindow *iface)
453 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
454 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
457 static HRESULT WINAPI ActiveScriptSiteWindow_GetWindow(IActiveScriptSiteWindow *iface, HWND *phwnd)
459 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
460 FIXME("(%p)->(%p)\n", This, phwnd);
464 static HRESULT WINAPI ActiveScriptSiteWindow_EnableModeless(IActiveScriptSiteWindow *iface, BOOL fEnable)
466 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
467 FIXME("(%p)->(%x)\n", This, fEnable);
471 static const IActiveScriptSiteWindowVtbl ActiveScriptSiteWindowVtbl = {
472 ActiveScriptSiteWindow_QueryInterface,
473 ActiveScriptSiteWindow_AddRef,
474 ActiveScriptSiteWindow_Release,
475 ActiveScriptSiteWindow_GetWindow,
476 ActiveScriptSiteWindow_EnableModeless
479 static inline ScriptHost *impl_from_IActiveScriptSiteDebug(IActiveScriptSiteDebug *iface)
481 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSiteDebug_iface);
484 static HRESULT WINAPI ActiveScriptSiteDebug_QueryInterface(IActiveScriptSiteDebug *iface,
485 REFIID riid, void **ppv)
487 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
488 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
491 static ULONG WINAPI ActiveScriptSiteDebug_AddRef(IActiveScriptSiteDebug *iface)
493 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
494 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
497 static ULONG WINAPI ActiveScriptSiteDebug_Release(IActiveScriptSiteDebug *iface)
499 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
500 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
503 static HRESULT WINAPI ActiveScriptSiteDebug_GetDocumentContextFromPosition(IActiveScriptSiteDebug *iface,
504 CTXARG_T dwSourceContext, ULONG uCharacterOffset, ULONG uNumChars, IDebugDocumentContext **ppsc)
506 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
507 FIXME("(%p)->(%s %u %u %p)\n", This, wine_dbgstr_longlong(dwSourceContext), uCharacterOffset,
512 static HRESULT WINAPI ActiveScriptSiteDebug_GetApplication(IActiveScriptSiteDebug *iface, IDebugApplication **ppda)
514 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
515 FIXME("(%p)->(%p)\n", This, ppda);
519 static HRESULT WINAPI ActiveScriptSiteDebug_GetRootApplicationNode(IActiveScriptSiteDebug *iface,
520 IDebugApplicationNode **ppdanRoot)
522 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
523 FIXME("(%p)->(%p)\n", This, ppdanRoot);
527 static HRESULT WINAPI ActiveScriptSiteDebug_OnScriptErrorDebug(IActiveScriptSiteDebug *iface,
528 IActiveScriptErrorDebug *pErrorDebug, BOOL *pfEnterDebugger, BOOL *pfCallOnScriptErrorWhenContinuing)
530 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
531 FIXME("(%p)->(%p %p %p)\n", This, pErrorDebug, pfEnterDebugger, pfCallOnScriptErrorWhenContinuing);
535 static const IActiveScriptSiteDebugVtbl ActiveScriptSiteDebugVtbl = {
536 ActiveScriptSiteDebug_QueryInterface,
537 ActiveScriptSiteDebug_AddRef,
538 ActiveScriptSiteDebug_Release,
539 ActiveScriptSiteDebug_GetDocumentContextFromPosition,
540 ActiveScriptSiteDebug_GetApplication,
541 ActiveScriptSiteDebug_GetRootApplicationNode,
542 ActiveScriptSiteDebug_OnScriptErrorDebug
545 static inline ScriptHost *impl_from_IServiceProvider(IServiceProvider *iface)
547 return CONTAINING_RECORD(iface, ScriptHost, IServiceProvider_iface);
550 static HRESULT WINAPI ASServiceProvider_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
552 ScriptHost *This = impl_from_IServiceProvider(iface);
553 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
556 static ULONG WINAPI ASServiceProvider_AddRef(IServiceProvider *iface)
558 ScriptHost *This = impl_from_IServiceProvider(iface);
559 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
562 static ULONG WINAPI ASServiceProvider_Release(IServiceProvider *iface)
564 ScriptHost *This = impl_from_IServiceProvider(iface);
565 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
568 static HRESULT WINAPI ASServiceProvider_QueryService(IServiceProvider *iface, REFGUID guidService,
569 REFIID riid, void **ppv)
571 ScriptHost *This = impl_from_IServiceProvider(iface);
573 if(IsEqualGUID(&SID_SInternetHostSecurityManager, guidService)) {
574 TRACE("(%p)->(SID_SInternetHostSecurityManager)\n", This);
576 if(!This->window || !This->window->base.inner_window->doc)
577 return E_NOINTERFACE;
579 return IInternetHostSecurityManager_QueryInterface(&This->window->base.inner_window->doc->IInternetHostSecurityManager_iface,
583 FIXME("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
584 return E_NOINTERFACE;
587 static const IServiceProviderVtbl ASServiceProviderVtbl = {
588 ASServiceProvider_QueryInterface,
589 ASServiceProvider_AddRef,
590 ASServiceProvider_Release,
591 ASServiceProvider_QueryService
594 static ScriptHost *create_script_host(HTMLOuterWindow *window, const GUID *guid)
599 ret = heap_alloc_zero(sizeof(*ret));
600 ret->IActiveScriptSite_iface.lpVtbl = &ActiveScriptSiteVtbl;
601 ret->IActiveScriptSiteInterruptPoll_iface.lpVtbl = &ActiveScriptSiteInterruptPollVtbl;
602 ret->IActiveScriptSiteWindow_iface.lpVtbl = &ActiveScriptSiteWindowVtbl;
603 ret->IActiveScriptSiteDebug_iface.lpVtbl = &ActiveScriptSiteDebugVtbl;
604 ret->IServiceProvider_iface.lpVtbl = &ASServiceProviderVtbl;
606 ret->window = window;
607 ret->script_state = SCRIPTSTATE_UNINITIALIZED;
610 list_add_tail(&window->script_hosts, &ret->entry);
612 hres = CoCreateInstance(&ret->guid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
613 &IID_IActiveScript, (void**)&ret->script);
615 WARN("Could not load script engine: %08x\n", hres);
616 else if(!init_script_engine(ret))
617 release_script_engine(ret);
622 static void parse_text(ScriptHost *script_host, LPCWSTR text)
628 static const WCHAR script_endW[] = {'<','/','S','C','R','I','P','T','>',0};
630 TRACE("%s\n", debugstr_w(text));
633 memset(&excepinfo, 0, sizeof(excepinfo));
635 hres = IActiveScriptParse64_ParseScriptText(script_host->parse, text, windowW, NULL, script_endW,
636 0, 0, SCRIPTTEXT_ISVISIBLE|SCRIPTTEXT_HOSTMANAGESSOURCE,
641 WARN("<<< %08x\n", hres);
645 static void parse_extern_script(ScriptHost *script_host, LPCWSTR src)
653 static const WCHAR wine_schemaW[] = {'w','i','n','e',':'};
655 if(strlenW(src) > sizeof(wine_schemaW)/sizeof(WCHAR) && !memcmp(src, wine_schemaW, sizeof(wine_schemaW)))
656 src += sizeof(wine_schemaW)/sizeof(WCHAR);
658 hres = CreateURLMoniker(NULL, src, &mon);
662 hres = bind_mon_to_buffer(script_host->window->base.inner_window->doc, mon, (void**)&buf, &size);
663 IMoniker_Release(mon);
667 len = MultiByteToWideChar(CP_ACP, 0, buf, size, NULL, 0);
668 text = heap_alloc((len+1)*sizeof(WCHAR));
669 MultiByteToWideChar(CP_ACP, 0, buf, size, text, len);
673 parse_text(script_host, text);
678 static void parse_inline_script(ScriptHost *script_host, nsIDOMHTMLScriptElement *nsscript)
680 const PRUnichar *text;
684 nsAString_Init(&text_str, NULL);
686 nsres = nsIDOMHTMLScriptElement_GetText(nsscript, &text_str);
688 if(NS_SUCCEEDED(nsres)) {
689 nsAString_GetData(&text_str, &text);
690 parse_text(script_host, text);
692 ERR("GetText failed: %08x\n", nsres);
695 nsAString_Finish(&text_str);
698 static void parse_script_elem(ScriptHost *script_host, nsIDOMHTMLScriptElement *nsscript)
700 const PRUnichar *src;
704 nsAString_Init(&src_str, NULL);
706 nsres = nsIDOMHTMLScriptElement_GetSrc(nsscript, &src_str);
707 nsAString_GetData(&src_str, &src);
710 ERR("GetSrc failed: %08x\n", nsres);
712 parse_extern_script(script_host, src);
714 parse_inline_script(script_host, nsscript);
716 nsAString_Finish(&src_str);
719 static BOOL get_guid_from_type(LPCWSTR type, GUID *guid)
721 const WCHAR text_javascriptW[] =
722 {'t','e','x','t','/','j','a','v','a','s','c','r','i','p','t',0};
723 const WCHAR text_vbscriptW[] =
724 {'t','e','x','t','/','v','b','s','c','r','i','p','t',0};
726 /* FIXME: Handle more types */
727 if(!strcmpiW(type, text_javascriptW)) {
728 *guid = CLSID_JScript;
729 }else if(!strcmpiW(type, text_vbscriptW)) {
730 *guid = CLSID_VBScript;
732 FIXME("Unknown type %s\n", debugstr_w(type));
739 static BOOL get_guid_from_language(LPCWSTR type, GUID *guid)
743 hres = CLSIDFromProgID(type, guid);
747 /* FIXME: Check CATID_ActiveScriptParse */
752 static BOOL get_script_guid(nsIDOMHTMLScriptElement *nsscript, GUID *guid)
754 nsAString attr_str, val_str;
758 static const PRUnichar languageW[] = {'l','a','n','g','u','a','g','e',0};
760 nsAString_Init(&val_str, NULL);
762 nsres = nsIDOMHTMLScriptElement_GetType(nsscript, &val_str);
763 if(NS_SUCCEEDED(nsres)) {
764 const PRUnichar *type;
766 nsAString_GetData(&val_str, &type);
768 ret = get_guid_from_type(type, guid);
769 nsAString_Finish(&val_str);
773 ERR("GetType failed: %08x\n", nsres);
776 nsAString_InitDepend(&attr_str, languageW);
777 nsres = nsIDOMHTMLScriptElement_GetAttribute(nsscript, &attr_str, &val_str);
778 nsAString_Finish(&attr_str);
779 if(NS_SUCCEEDED(nsres)) {
780 const PRUnichar *language;
782 nsAString_GetData(&val_str, &language);
785 ret = get_guid_from_language(language, guid);
787 *guid = CLSID_JScript;
791 ERR("GetAttribute(language) failed: %08x\n", nsres);
794 nsAString_Finish(&val_str);
799 static ScriptHost *get_script_host(HTMLOuterWindow *window, const GUID *guid)
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(HTMLOuterWindow *window, nsIDOMHTMLScriptElement *nsscript)
813 ScriptHost *script_host;
816 if(!get_script_guid(nsscript, &guid)) {
817 WARN("Could not find script GUID\n");
821 if(IsEqualGUID(&CLSID_JScript, &guid) && window->scriptmode != SCRIPTMODE_ACTIVESCRIPT) {
822 TRACE("Ignoring JScript\n");
826 script_host = get_script_host(window, &guid);
830 if(script_host->parse)
831 parse_script_elem(script_host, nsscript);
834 IDispatch *script_parse_event(HTMLOuterWindow *window, LPCWSTR text)
836 ScriptHost *script_host;
837 GUID guid = CLSID_JScript;
842 static const WCHAR delimiterW[] = {'\"',0};
844 for(ptr = text; isalnumW(*ptr); ptr++);
849 language = heap_alloc((ptr-text+1)*sizeof(WCHAR));
850 memcpy(language, text, (ptr-text)*sizeof(WCHAR));
851 language[ptr-text] = 0;
853 b = get_guid_from_language(language, &guid);
858 WARN("Could not find language\n");
867 if(IsEqualGUID(&CLSID_JScript, &guid) && window->scriptmode != SCRIPTMODE_ACTIVESCRIPT) {
868 TRACE("Ignoring JScript\n");
872 script_host = get_script_host(window, &guid);
873 if(!script_host || !script_host->parse_proc)
876 hres = IActiveScriptParseProcedure64_ParseProcedureText(script_host->parse_proc, ptr, NULL, emptyW,
877 NULL, NULL, delimiterW, 0 /* FIXME */, 0,
878 SCRIPTPROC_HOSTMANAGESSOURCE|SCRIPTPROC_IMPLICIT_THIS|SCRIPTPROC_IMPLICIT_PARENTS, &disp);
880 WARN("ParseProcedureText failed: %08x\n", hres);
884 TRACE("ret %p\n", disp);
888 HRESULT exec_script(HTMLOuterWindow *window, const WCHAR *code, const WCHAR *lang, VARIANT *ret)
890 ScriptHost *script_host;
895 static const WCHAR delimW[] = {'"',0};
897 if(!get_guid_from_language(lang, &guid)) {
898 WARN("Could not find script GUID\n");
899 return CO_E_CLASSSTRING;
902 script_host = get_script_host(window, &guid);
904 FIXME("No script host\n");
908 if(!script_host->parse) {
909 FIXME("script_host->parse == NULL\n");
913 memset(&ei, 0, sizeof(ei));
915 hres = IActiveScriptParse64_ParseScriptText(script_host->parse, code, NULL, NULL, delimW, 0, 0, SCRIPTTEXT_ISVISIBLE, ret, &ei);
919 WARN("<<< %08x\n", hres);
924 IDispatch *get_script_disp(ScriptHost *script_host)
929 if(!script_host->script)
932 hres = IActiveScript_GetScriptDispatch(script_host->script, windowW, &disp);
939 BOOL find_global_prop(HTMLOuterWindow *window, BSTR name, DWORD flags, ScriptHost **ret_host, DISPID *ret_id)
946 LIST_FOR_EACH_ENTRY(iter, &window->script_hosts, ScriptHost, entry) {
947 disp = get_script_disp(iter);
951 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
952 if(SUCCEEDED(hres)) {
953 hres = IDispatchEx_GetDispID(dispex, name, flags & (~fdexNameEnsure), ret_id);
954 IDispatchEx_Release(dispex);
956 FIXME("No IDispatchEx\n");
960 IDispatch_Release(disp);
961 if(SUCCEEDED(hres)) {
970 static BOOL is_jscript_available(void)
972 static BOOL available, checked;
976 HRESULT hres = CoGetClassObject(&CLSID_JScript, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void**)&unk);
978 if(SUCCEEDED(hres)) {
980 IUnknown_Release(unk);
990 void set_script_mode(HTMLOuterWindow *window, SCRIPTMODE mode)
992 nsIWebBrowserSetup *setup;
995 if(mode == SCRIPTMODE_ACTIVESCRIPT && !is_jscript_available()) {
996 TRACE("jscript.dll not available\n");
997 window->scriptmode = SCRIPTMODE_GECKO;
1001 window->scriptmode = mode;
1003 if(!window->doc_obj->nscontainer || !window->doc_obj->nscontainer->webbrowser)
1006 nsres = nsIWebBrowser_QueryInterface(window->doc_obj->nscontainer->webbrowser,
1007 &IID_nsIWebBrowserSetup, (void**)&setup);
1008 if(NS_SUCCEEDED(nsres)) {
1009 nsres = nsIWebBrowserSetup_SetProperty(setup, SETUP_ALLOW_JAVASCRIPT,
1010 window->scriptmode == SCRIPTMODE_GECKO);
1011 nsIWebBrowserSetup_Release(setup);
1014 if(NS_FAILED(nsres))
1015 ERR("JavaScript setup failed: %08x\n", nsres);
1018 void release_script_hosts(HTMLOuterWindow *window)
1022 while(!list_empty(&window->script_hosts)) {
1023 iter = LIST_ENTRY(list_head(&window->script_hosts), ScriptHost, entry);
1025 release_script_engine(iter);
1026 list_remove(&iter->entry);
1027 iter->window = NULL;
1028 IActiveScript_Release(&iter->IActiveScriptSite_iface);