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);
39 static const WCHAR windowW[] = {'w','i','n','d','o','w',0};
40 static const WCHAR emptyW[] = {0};
42 static const CLSID CLSID_JScript =
43 {0xf414c260,0x6ac0,0x11cf,{0xb6,0xd1,0x00,0xaa,0x00,0xbb,0xbb,0x58}};
46 const IActiveScriptSiteVtbl *lpIActiveScriptSiteVtbl;
47 const IActiveScriptSiteInterruptPollVtbl *lpIActiveScriptSiteInterruptPollVtbl;
48 const IActiveScriptSiteWindowVtbl *lpIActiveScriptSiteWindowVtbl;
49 const IActiveScriptSiteDebug32Vtbl *lpIActiveScriptSiteDebug32Vtbl;
50 const IServiceProviderVtbl *lpServiceProviderVtbl;
54 IActiveScript *script;
55 IActiveScriptParse *parse;
56 IActiveScriptParseProcedure *parse_proc;
58 SCRIPTSTATE script_state;
66 #define ACTSCPSITE(x) ((IActiveScriptSite*) &(x)->lpIActiveScriptSiteVtbl)
67 #define ACTSCPPOLL(x) (&(x)->lpIActiveScriptSiteInterruptPollVtbl)
68 #define ACTSCPWIN(x) (&(x)->lpIActiveScriptSiteWindowVtbl)
69 #define ACTSCPDBG32(x) (&(x)->lpIActiveScriptSiteDebug32Vtbl)
71 static void set_script_prop(ScriptHost *script_host, DWORD property, VARIANT *val)
73 IActiveScriptProperty *script_prop;
76 hres = IActiveScript_QueryInterface(script_host->script, &IID_IActiveScriptProperty,
77 (void**)&script_prop);
79 WARN("Could not get IActiveScriptProperty iface: %08x\n", hres);
83 hres = IActiveScriptProperty_SetProperty(script_prop, property, NULL, val);
84 IActiveScriptProperty_Release(script_prop);
86 WARN("SetProperty(%x) failed: %08x\n", property, hres);
89 static BOOL init_script_engine(ScriptHost *script_host)
91 IObjectSafety *safety;
93 DWORD supported_opts=0, enabled_opts=0;
97 hres = IActiveScript_QueryInterface(script_host->script, &IID_IActiveScriptParse, (void**)&script_host->parse);
99 WARN("Could not get IActiveScriptHost: %08x\n", hres);
103 hres = IActiveScript_QueryInterface(script_host->script, &IID_IObjectSafety, (void**)&safety);
105 FIXME("Could not get IObjectSafety: %08x\n", hres);
109 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported_opts, &enabled_opts);
111 FIXME("GetInterfaceSafetyOptions failed: %08x\n", hres);
112 }else if(!(supported_opts & INTERFACE_USES_DISPEX)) {
113 FIXME("INTERFACE_USES_DISPEX is not supported\n");
115 hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse,
116 INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER,
117 INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER);
119 FIXME("SetInterfaceSafetyOptions failed: %08x\n", hres);
122 IObjectSafety_Release(safety);
128 set_script_prop(script_host, SCRIPTPROP_INVOKEVERSIONING, &var);
130 V_VT(&var) = VT_BOOL;
131 V_BOOL(&var) = VARIANT_TRUE;
132 set_script_prop(script_host, SCRIPTPROP_HACK_TRIDENTEVENTSINK, &var);
134 hres = IActiveScriptParse64_InitNew(script_host->parse);
136 WARN("InitNew failed: %08x\n", hres);
140 hres = IActiveScript_SetScriptSite(script_host->script, ACTSCPSITE(script_host));
142 WARN("SetScriptSite failed: %08x\n", hres);
143 IActiveScript_Close(script_host->script);
147 hres = IActiveScript_GetScriptState(script_host->script, &state);
149 WARN("GetScriptState failed: %08x\n", hres);
150 else if(state != SCRIPTSTATE_INITIALIZED)
151 FIXME("state = %x\n", state);
153 hres = IActiveScript_SetScriptState(script_host->script, SCRIPTSTATE_STARTED);
155 WARN("Starting script failed: %08x\n", hres);
159 hres = IActiveScript_AddNamedItem(script_host->script, windowW,
160 SCRIPTITEM_ISVISIBLE|SCRIPTITEM_ISSOURCE|SCRIPTITEM_GLOBALMEMBERS);
161 if(SUCCEEDED(hres)) {
162 V_VT(&var) = VT_BOOL;
163 V_BOOL(&var) = VARIANT_TRUE;
164 set_script_prop(script_host, SCRIPTPROP_ABBREVIATE_GLOBALNAME_RESOLUTION, &var);
166 WARN("AddNamedItem failed: %08x\n", hres);
169 hres = IActiveScript_QueryInterface(script_host->script, &IID_IActiveScriptParseProcedure2,
170 (void**)&script_host->parse_proc);
172 /* FIXME: QI for IActiveScriptParseProcedure */
173 WARN("Could not get IActiveScriptParseProcedure iface: %08x\n", hres);
179 static void release_script_engine(ScriptHost *This)
184 switch(This->script_state) {
185 case SCRIPTSTATE_CONNECTED:
186 IActiveScript_SetScriptState(This->script, SCRIPTSTATE_DISCONNECTED);
188 case SCRIPTSTATE_STARTED:
189 case SCRIPTSTATE_DISCONNECTED:
190 case SCRIPTSTATE_INITIALIZED:
191 IActiveScript_Close(This->script);
194 if(This->parse_proc) {
195 IUnknown_Release(This->parse_proc);
196 This->parse_proc = NULL;
200 IUnknown_Release(This->parse);
205 IActiveScript_Release(This->script);
207 This->script_state = SCRIPTSTATE_UNINITIALIZED;
210 void connect_scripts(HTMLWindow *window)
214 LIST_FOR_EACH_ENTRY(iter, &window->script_hosts, ScriptHost, entry) {
215 if(iter->script_state == SCRIPTSTATE_STARTED)
216 IActiveScript_SetScriptState(iter->script, SCRIPTSTATE_CONNECTED);
220 #define ACTSCPSITE_THIS(iface) DEFINE_THIS(ScriptHost, IActiveScriptSite, iface)
222 static HRESULT WINAPI ActiveScriptSite_QueryInterface(IActiveScriptSite *iface, REFIID riid, void **ppv)
224 ScriptHost *This = ACTSCPSITE_THIS(iface);
228 if(IsEqualGUID(&IID_IUnknown, riid)) {
229 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
230 *ppv = ACTSCPSITE(This);
231 }else if(IsEqualGUID(&IID_IActiveScriptSite, riid)) {
232 TRACE("(%p)->(IID_IActiveScriptSite %p)\n", This, ppv);
233 *ppv = ACTSCPSITE(This);
234 }else if(IsEqualGUID(&IID_IActiveScriptSiteInterruptPoll, riid)) {
235 TRACE("(%p)->(IID_IActiveScriptSiteInterruprtPoll %p)\n", This, ppv);
236 *ppv = ACTSCPPOLL(This);
237 }else if(IsEqualGUID(&IID_IActiveScriptSiteWindow, riid)) {
238 TRACE("(%p)->(IID_IActiveScriptSiteWindow %p)\n", This, ppv);
239 *ppv = ACTSCPWIN(This);
240 }else if(IsEqualGUID(&IID_IActiveScriptSiteDebug32, riid)) {
241 TRACE("(%p)->(IID_IActiveScriptSiteDebug32 %p)\n", This, ppv);
242 *ppv = ACTSCPDBG32(This);
243 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
244 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
245 *ppv = SERVPROV(This);
246 }else if(IsEqualGUID(&IID_ICanHandleException, riid)) {
247 TRACE("(%p)->(IID_ICanHandleException not supported %p)\n", This, ppv);
248 return E_NOINTERFACE;
250 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
251 return E_NOINTERFACE;
254 IUnknown_AddRef((IUnknown*)*ppv);
258 static ULONG WINAPI ActiveScriptSite_AddRef(IActiveScriptSite *iface)
260 ScriptHost *This = ACTSCPSITE_THIS(iface);
261 LONG ref = InterlockedIncrement(&This->ref);
263 TRACE("(%p) ref=%d\n", This, ref);
268 static ULONG WINAPI ActiveScriptSite_Release(IActiveScriptSite *iface)
270 ScriptHost *This = ACTSCPSITE_THIS(iface);
271 LONG ref = InterlockedDecrement(&This->ref);
273 TRACE("(%p) ref=%d\n", This, ref);
276 release_script_engine(This);
278 list_remove(&This->entry);
285 static HRESULT WINAPI ActiveScriptSite_GetLCID(IActiveScriptSite *iface, LCID *plcid)
287 ScriptHost *This = ACTSCPSITE_THIS(iface);
289 TRACE("(%p)->(%p)\n", This, plcid);
291 *plcid = GetUserDefaultLCID();
295 static HRESULT WINAPI ActiveScriptSite_GetItemInfo(IActiveScriptSite *iface, LPCOLESTR pstrName,
296 DWORD dwReturnMask, IUnknown **ppiunkItem, ITypeInfo **ppti)
298 ScriptHost *This = ACTSCPSITE_THIS(iface);
300 TRACE("(%p)->(%s %x %p %p)\n", This, debugstr_w(pstrName), dwReturnMask, ppiunkItem, ppti);
302 if(dwReturnMask != SCRIPTINFO_IUNKNOWN) {
303 FIXME("Unsupported mask %x\n", dwReturnMask);
309 if(strcmpW(pstrName, windowW))
310 return DISP_E_MEMBERNOTFOUND;
315 /* FIXME: Return proxy object */
316 *ppiunkItem = (IUnknown*)HTMLWINDOW2(This->window);
317 IUnknown_AddRef(*ppiunkItem);
322 static HRESULT WINAPI ActiveScriptSite_GetDocVersionString(IActiveScriptSite *iface, BSTR *pbstrVersion)
324 ScriptHost *This = ACTSCPSITE_THIS(iface);
325 FIXME("(%p)->(%p)\n", This, pbstrVersion);
329 static HRESULT WINAPI ActiveScriptSite_OnScriptTerminate(IActiveScriptSite *iface,
330 const VARIANT *pvarResult, const EXCEPINFO *pexcepinfo)
332 ScriptHost *This = ACTSCPSITE_THIS(iface);
333 FIXME("(%p)->(%p %p)\n", This, pvarResult, pexcepinfo);
337 static HRESULT WINAPI ActiveScriptSite_OnStateChange(IActiveScriptSite *iface, SCRIPTSTATE ssScriptState)
339 ScriptHost *This = ACTSCPSITE_THIS(iface);
341 TRACE("(%p)->(%x)\n", This, ssScriptState);
343 This->script_state = ssScriptState;
347 static HRESULT WINAPI ActiveScriptSite_OnScriptError(IActiveScriptSite *iface, IActiveScriptError *pscripterror)
349 ScriptHost *This = ACTSCPSITE_THIS(iface);
350 FIXME("(%p)->(%p)\n", This, pscripterror);
354 static HRESULT WINAPI ActiveScriptSite_OnEnterScript(IActiveScriptSite *iface)
356 ScriptHost *This = ACTSCPSITE_THIS(iface);
358 TRACE("(%p)->()\n", This);
363 static HRESULT WINAPI ActiveScriptSite_OnLeaveScript(IActiveScriptSite *iface)
365 ScriptHost *This = ACTSCPSITE_THIS(iface);
367 TRACE("(%p)->()\n", This);
372 #undef ACTSCPSITE_THIS
374 static const IActiveScriptSiteVtbl ActiveScriptSiteVtbl = {
375 ActiveScriptSite_QueryInterface,
376 ActiveScriptSite_AddRef,
377 ActiveScriptSite_Release,
378 ActiveScriptSite_GetLCID,
379 ActiveScriptSite_GetItemInfo,
380 ActiveScriptSite_GetDocVersionString,
381 ActiveScriptSite_OnScriptTerminate,
382 ActiveScriptSite_OnStateChange,
383 ActiveScriptSite_OnScriptError,
384 ActiveScriptSite_OnEnterScript,
385 ActiveScriptSite_OnLeaveScript
388 #define ACTSCPPOLL_THIS(iface) DEFINE_THIS(ScriptHost, IActiveScriptSiteInterruptPoll, iface)
390 static HRESULT WINAPI ActiveScriptSiteInterruptPoll_QueryInterface(IActiveScriptSiteInterruptPoll *iface,
391 REFIID riid, void **ppv)
393 ScriptHost *This = ACTSCPPOLL_THIS(iface);
394 return IActiveScriptSite_QueryInterface(ACTSCPSITE(This), riid, ppv);
397 static ULONG WINAPI ActiveScriptSiteInterruptPoll_AddRef(IActiveScriptSiteInterruptPoll *iface)
399 ScriptHost *This = ACTSCPPOLL_THIS(iface);
400 return IActiveScriptSite_AddRef(ACTSCPSITE(This));
403 static ULONG WINAPI ActiveScriptSiteInterruptPoll_Release(IActiveScriptSiteInterruptPoll *iface)
405 ScriptHost *This = ACTSCPPOLL_THIS(iface);
406 return IActiveScriptSite_Release(ACTSCPSITE(This));
409 static HRESULT WINAPI ActiveScriptSiteInterruptPoll_QueryContinue(IActiveScriptSiteInterruptPoll *iface)
411 ScriptHost *This = ACTSCPPOLL_THIS(iface);
413 TRACE("(%p)\n", This);
418 #undef ACTSCPPOLL_THIS
420 static const IActiveScriptSiteInterruptPollVtbl ActiveScriptSiteInterruptPollVtbl = {
421 ActiveScriptSiteInterruptPoll_QueryInterface,
422 ActiveScriptSiteInterruptPoll_AddRef,
423 ActiveScriptSiteInterruptPoll_Release,
424 ActiveScriptSiteInterruptPoll_QueryContinue
427 #define ACTSCPWIN_THIS(iface) DEFINE_THIS(ScriptHost, IActiveScriptSiteWindow, iface)
429 static HRESULT WINAPI ActiveScriptSiteWindow_QueryInterface(IActiveScriptSiteWindow *iface,
430 REFIID riid, void **ppv)
432 ScriptHost *This = ACTSCPWIN_THIS(iface);
433 return IActiveScriptSite_QueryInterface(ACTSCPSITE(This), riid, ppv);
436 static ULONG WINAPI ActiveScriptSiteWindow_AddRef(IActiveScriptSiteWindow *iface)
438 ScriptHost *This = ACTSCPWIN_THIS(iface);
439 return IActiveScriptSite_AddRef(ACTSCPSITE(This));
442 static ULONG WINAPI ActiveScriptSiteWindow_Release(IActiveScriptSiteWindow *iface)
444 ScriptHost *This = ACTSCPWIN_THIS(iface);
445 return IActiveScriptSite_Release(ACTSCPSITE(This));
448 static HRESULT WINAPI ActiveScriptSiteWindow_GetWindow(IActiveScriptSiteWindow *iface, HWND *phwnd)
450 ScriptHost *This = ACTSCPWIN_THIS(iface);
451 FIXME("(%p)->(%p)\n", This, phwnd);
455 static HRESULT WINAPI ActiveScriptSiteWindow_EnableModeless(IActiveScriptSiteWindow *iface, BOOL fEnable)
457 ScriptHost *This = ACTSCPWIN_THIS(iface);
458 FIXME("(%p)->(%x)\n", This, fEnable);
462 #undef ACTSCPWIN_THIS
464 static const IActiveScriptSiteWindowVtbl ActiveScriptSiteWindowVtbl = {
465 ActiveScriptSiteWindow_QueryInterface,
466 ActiveScriptSiteWindow_AddRef,
467 ActiveScriptSiteWindow_Release,
468 ActiveScriptSiteWindow_GetWindow,
469 ActiveScriptSiteWindow_EnableModeless
472 #define ACTSCPDBG32_THIS(iface) DEFINE_THIS(ScriptHost, IActiveScriptSiteDebug32, iface)
474 static HRESULT WINAPI ActiveScriptSiteDebug32_QueryInterface(IActiveScriptSiteDebug32 *iface,
475 REFIID riid, void **ppv)
477 ScriptHost *This = ACTSCPDBG32_THIS(iface);
478 return IActiveScriptSite_QueryInterface(ACTSCPSITE(This), riid, ppv);
481 static ULONG WINAPI ActiveScriptSiteDebug32_AddRef(IActiveScriptSiteDebug32 *iface)
483 ScriptHost *This = ACTSCPDBG32_THIS(iface);
484 return IActiveScriptSite_AddRef(ACTSCPSITE(This));
487 static ULONG WINAPI ActiveScriptSiteDebug32_Release(IActiveScriptSiteDebug32 *iface)
489 ScriptHost *This = ACTSCPDBG32_THIS(iface);
490 return IActiveScriptSite_Release(ACTSCPSITE(This));
493 static HRESULT WINAPI ActiveScriptSiteDebug32_GetDocumentContextFromPosition(IActiveScriptSiteDebug32 *iface,
494 DWORD dwSourceContext, ULONG uCharacterOffset, ULONG uNumChars, IDebugDocumentContext **ppsc)
496 ScriptHost *This = ACTSCPDBG32_THIS(iface);
497 FIXME("(%p)->(%x %u %u %p)\n", This, dwSourceContext, uCharacterOffset, uNumChars, ppsc);
501 static HRESULT WINAPI ActiveScriptSiteDebug32_GetApplication(IActiveScriptSiteDebug32 *iface, IDebugApplication32 **ppda)
503 ScriptHost *This = ACTSCPDBG32_THIS(iface);
504 FIXME("(%p)->(%p)\n", This, ppda);
508 static HRESULT WINAPI ActiveScriptSiteDebug32_GetRootApplicationNode(IActiveScriptSiteDebug32 *iface,
509 IDebugApplicationNode **ppdanRoot)
511 ScriptHost *This = ACTSCPDBG32_THIS(iface);
512 FIXME("(%p)->(%p)\n", This, ppdanRoot);
516 static HRESULT WINAPI ActiveScriptSiteDebug32_OnScriptErrorDebug(IActiveScriptSiteDebug32 *iface,
517 IActiveScriptErrorDebug *pErrorDebug, BOOL *pfEnterDebugger, BOOL *pfCallOnScriptErrorWhenContinuing)
519 ScriptHost *This = ACTSCPDBG32_THIS(iface);
520 FIXME("(%p)->(%p %p %p)\n", This, pErrorDebug, pfEnterDebugger, pfCallOnScriptErrorWhenContinuing);
524 #undef ACTSCPDBG32_THIS
526 static const IActiveScriptSiteDebug32Vtbl ActiveScriptSiteDebug32Vtbl = {
527 ActiveScriptSiteDebug32_QueryInterface,
528 ActiveScriptSiteDebug32_AddRef,
529 ActiveScriptSiteDebug32_Release,
530 ActiveScriptSiteDebug32_GetDocumentContextFromPosition,
531 ActiveScriptSiteDebug32_GetApplication,
532 ActiveScriptSiteDebug32_GetRootApplicationNode,
533 ActiveScriptSiteDebug32_OnScriptErrorDebug
536 #define SERVPROV_THIS(iface) DEFINE_THIS(ScriptHost, ServiceProvider, iface)
538 static HRESULT WINAPI ASServiceProvider_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
540 ScriptHost *This = SERVPROV_THIS(iface);
541 return IActiveScriptSite_QueryInterface(ACTSCPSITE(This), riid, ppv);
544 static ULONG WINAPI ASServiceProvider_AddRef(IServiceProvider *iface)
546 ScriptHost *This = SERVPROV_THIS(iface);
547 return IActiveScriptSite_AddRef(ACTSCPSITE(This));
550 static ULONG WINAPI ASServiceProvider_Release(IServiceProvider *iface)
552 ScriptHost *This = SERVPROV_THIS(iface);
553 return IActiveScriptSite_Release(ACTSCPSITE(This));
556 static HRESULT WINAPI ASServiceProvider_QueryService(IServiceProvider *iface, REFGUID guidService,
557 REFIID riid, void **ppv)
559 ScriptHost *This = SERVPROV_THIS(iface);
561 if(IsEqualGUID(&SID_SInternetHostSecurityManager, guidService)) {
562 TRACE("(%p)->(SID_SInternetHostSecurityManager)\n", This);
564 if(!This->window || !This->window->doc)
565 return E_NOINTERFACE;
567 return IInternetHostSecurityManager_QueryInterface(HOSTSECMGR(This->window->doc), riid, ppv);
570 FIXME("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
571 return E_NOINTERFACE;
576 static const IServiceProviderVtbl ASServiceProviderVtbl = {
577 ASServiceProvider_QueryInterface,
578 ASServiceProvider_AddRef,
579 ASServiceProvider_Release,
580 ASServiceProvider_QueryService
583 static ScriptHost *create_script_host(HTMLWindow *window, const GUID *guid)
588 ret = heap_alloc_zero(sizeof(*ret));
589 ret->lpIActiveScriptSiteVtbl = &ActiveScriptSiteVtbl;
590 ret->lpIActiveScriptSiteInterruptPollVtbl = &ActiveScriptSiteInterruptPollVtbl;
591 ret->lpIActiveScriptSiteWindowVtbl = &ActiveScriptSiteWindowVtbl;
592 ret->lpIActiveScriptSiteDebug32Vtbl = &ActiveScriptSiteDebug32Vtbl;
593 ret->lpServiceProviderVtbl = &ASServiceProviderVtbl;
595 ret->window = window;
596 ret->script_state = SCRIPTSTATE_UNINITIALIZED;
599 list_add_tail(&window->script_hosts, &ret->entry);
601 hres = CoCreateInstance(&ret->guid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
602 &IID_IActiveScript, (void**)&ret->script);
604 WARN("Could not load script engine: %08x\n", hres);
605 else if(!init_script_engine(ret))
606 release_script_engine(ret);
611 static void parse_text(ScriptHost *script_host, LPCWSTR text)
617 static const WCHAR script_endW[] = {'<','/','S','C','R','I','P','T','>',0};
619 TRACE("%s\n", debugstr_w(text));
622 memset(&excepinfo, 0, sizeof(excepinfo));
624 hres = IActiveScriptParse64_ParseScriptText(script_host->parse, text, windowW, NULL, script_endW,
625 0, 0, SCRIPTTEXT_ISVISIBLE|SCRIPTTEXT_HOSTMANAGESSOURCE,
630 WARN("<<< %08x\n", hres);
634 static void parse_extern_script(ScriptHost *script_host, LPCWSTR src)
642 static const WCHAR wine_schemaW[] = {'w','i','n','e',':'};
644 if(strlenW(src) > sizeof(wine_schemaW)/sizeof(WCHAR) && !memcmp(src, wine_schemaW, sizeof(wine_schemaW)))
645 src += sizeof(wine_schemaW)/sizeof(WCHAR);
647 hres = CreateURLMoniker(NULL, src, &mon);
651 hres = bind_mon_to_buffer(script_host->window->doc, mon, (void**)&buf, &size);
652 IMoniker_Release(mon);
656 len = MultiByteToWideChar(CP_ACP, 0, buf, size, NULL, 0);
657 text = heap_alloc((len+1)*sizeof(WCHAR));
658 MultiByteToWideChar(CP_ACP, 0, buf, size, text, len);
662 parse_text(script_host, text);
667 static void parse_inline_script(ScriptHost *script_host, nsIDOMHTMLScriptElement *nsscript)
669 const PRUnichar *text;
673 nsAString_Init(&text_str, NULL);
675 nsres = nsIDOMHTMLScriptElement_GetText(nsscript, &text_str);
677 if(NS_SUCCEEDED(nsres)) {
678 nsAString_GetData(&text_str, &text);
679 parse_text(script_host, text);
681 ERR("GetText failed: %08x\n", nsres);
684 nsAString_Finish(&text_str);
687 static void parse_script_elem(ScriptHost *script_host, nsIDOMHTMLScriptElement *nsscript)
689 const PRUnichar *src;
693 nsAString_Init(&src_str, NULL);
695 nsres = nsIDOMHTMLScriptElement_GetSrc(nsscript, &src_str);
696 nsAString_GetData(&src_str, &src);
699 ERR("GetSrc failed: %08x\n", nsres);
701 parse_extern_script(script_host, src);
703 parse_inline_script(script_host, nsscript);
705 nsAString_Finish(&src_str);
708 static BOOL get_guid_from_type(LPCWSTR type, GUID *guid)
710 const WCHAR text_javascriptW[] =
711 {'t','e','x','t','/','j','a','v','a','s','c','r','i','p','t',0};
713 /* FIXME: Handle more types */
714 if(!strcmpiW(type, text_javascriptW)) {
715 *guid = CLSID_JScript;
717 FIXME("Unknown type %s\n", debugstr_w(type));
724 static BOOL get_guid_from_language(LPCWSTR type, GUID *guid)
728 hres = CLSIDFromProgID(type, guid);
732 /* FIXME: Check CATID_ActiveScriptParse */
737 static BOOL get_script_guid(nsIDOMHTMLScriptElement *nsscript, GUID *guid)
739 nsAString attr_str, val_str;
743 static const PRUnichar languageW[] = {'l','a','n','g','u','a','g','e',0};
745 nsAString_Init(&val_str, NULL);
747 nsres = nsIDOMHTMLScriptElement_GetType(nsscript, &val_str);
748 if(NS_SUCCEEDED(nsres)) {
749 const PRUnichar *type;
751 nsAString_GetData(&val_str, &type);
753 ret = get_guid_from_type(type, guid);
754 nsAString_Finish(&val_str);
758 ERR("GetType failed: %08x\n", nsres);
761 nsAString_Init(&attr_str, languageW);
763 nsres = nsIDOMHTMLScriptElement_GetAttribute(nsscript, &attr_str, &val_str);
764 if(NS_SUCCEEDED(nsres)) {
765 const PRUnichar *language;
767 nsAString_GetData(&val_str, &language);
770 ret = get_guid_from_language(language, guid);
772 *guid = CLSID_JScript;
776 ERR("GetAttribute(language) failed: %08x\n", nsres);
779 nsAString_Finish(&attr_str);
780 nsAString_Finish(&val_str);
785 static ScriptHost *get_script_host(HTMLWindow *window, const GUID *guid)
789 if(IsEqualGUID(&CLSID_JScript, guid) && window->scriptmode != SCRIPTMODE_ACTIVESCRIPT) {
790 TRACE("Ignoring JScript\n");
794 LIST_FOR_EACH_ENTRY(iter, &window->script_hosts, ScriptHost, entry) {
795 if(IsEqualGUID(guid, &iter->guid))
799 return create_script_host(window, guid);
802 void doc_insert_script(HTMLWindow *window, nsIDOMHTMLScriptElement *nsscript)
804 ScriptHost *script_host;
807 if(!get_script_guid(nsscript, &guid)) {
808 WARN("Could not find script GUID\n");
812 script_host = get_script_host(window, &guid);
816 if(script_host->parse)
817 parse_script_elem(script_host, nsscript);
820 IDispatch *script_parse_event(HTMLWindow *window, LPCWSTR text)
822 ScriptHost *script_host;
823 GUID guid = CLSID_JScript;
828 static const WCHAR delimiterW[] = {'\"',0};
830 for(ptr = text; isalnumW(*ptr); ptr++);
835 language = heap_alloc((ptr-text+1)*sizeof(WCHAR));
836 memcpy(language, text, (ptr-text)*sizeof(WCHAR));
837 language[ptr-text] = 0;
839 b = get_guid_from_language(language, &guid);
844 WARN("Could not find language\n");
853 script_host = get_script_host(window, &guid);
854 if(!script_host || !script_host->parse_proc)
857 hres = IActiveScriptParseProcedure64_ParseProcedureText(script_host->parse_proc, ptr, NULL, emptyW,
858 NULL, NULL, delimiterW, 0 /* FIXME */, 0,
859 SCRIPTPROC_HOSTMANAGESSOURCE|SCRIPTPROC_IMPLICIT_THIS|SCRIPTPROC_IMPLICIT_PARENTS, &disp);
861 WARN("ParseProcedureText failed: %08x\n", hres);
865 TRACE("ret %p\n", disp);
869 IDispatch *get_script_disp(ScriptHost *script_host)
874 if(!script_host->script)
877 hres = IActiveScript_GetScriptDispatch(script_host->script, windowW, &disp);
884 BOOL find_global_prop(HTMLWindow *window, BSTR name, DWORD flags, ScriptHost **ret_host, DISPID *ret_id)
891 LIST_FOR_EACH_ENTRY(iter, &window->script_hosts, ScriptHost, entry) {
892 disp = get_script_disp(iter);
896 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
897 if(SUCCEEDED(hres)) {
898 hres = IDispatchEx_GetDispID(dispex, name, flags & (~fdexNameEnsure), ret_id);
899 IDispatchEx_Release(dispex);
901 FIXME("No IDispatchEx\n");
905 IDispatch_Release(disp);
906 if(SUCCEEDED(hres)) {
915 static BOOL is_jscript_available(void)
917 static BOOL available, checked;
921 HRESULT hres = CoGetClassObject(&CLSID_JScript, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void**)&unk);
923 if(SUCCEEDED(hres)) {
925 IUnknown_Release(unk);
935 void set_script_mode(HTMLWindow *window, SCRIPTMODE mode)
937 nsIWebBrowserSetup *setup;
940 if(mode == SCRIPTMODE_ACTIVESCRIPT && !is_jscript_available()) {
941 TRACE("jscript.dll not available\n");
942 window->scriptmode = SCRIPTMODE_GECKO;
946 window->scriptmode = mode;
948 if(!window->doc_obj->nscontainer || !window->doc_obj->nscontainer->webbrowser)
951 nsres = nsIWebBrowser_QueryInterface(window->doc_obj->nscontainer->webbrowser,
952 &IID_nsIWebBrowserSetup, (void**)&setup);
953 if(NS_SUCCEEDED(nsres)) {
954 nsres = nsIWebBrowserSetup_SetProperty(setup, SETUP_ALLOW_JAVASCRIPT,
955 window->scriptmode == SCRIPTMODE_GECKO);
956 nsIWebBrowserSetup_Release(setup);
960 ERR("JavaScript setup failed: %08x\n", nsres);
963 void release_script_hosts(HTMLWindow *window)
967 while(!list_empty(&window->script_hosts)) {
968 iter = LIST_ENTRY(list_head(&window->script_hosts), ScriptHost, entry);
970 release_script_engine(iter);
971 list_remove(&iter->entry);
973 IActiveScript_Release(ACTSCPSITE(iter));