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"
37 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
41 #define CTXARG_T DWORDLONG
42 #define IActiveScriptSiteDebugVtbl IActiveScriptSiteDebug64Vtbl
44 #define IActiveScriptParse_Release IActiveScriptParse64_Release
45 #define IActiveScriptParse_InitNew IActiveScriptParse64_InitNew
46 #define IActiveScriptParse_ParseScriptText IActiveScriptParse64_ParseScriptText
47 #define IActiveScriptParseProcedure_Release IActiveScriptParseProcedure64_Release
48 #define IActiveScriptParseProcedure_ParseProcedureText IActiveScriptParseProcedure64_ParseProcedureText
52 #define CTXARG_T DWORD
53 #define IActiveScriptSiteDebugVtbl IActiveScriptSiteDebug32Vtbl
55 #define IActiveScriptParse_Release IActiveScriptParse32_Release
56 #define IActiveScriptParse_InitNew IActiveScriptParse32_InitNew
57 #define IActiveScriptParse_ParseScriptText IActiveScriptParse32_ParseScriptText
58 #define IActiveScriptParseProcedure_Release IActiveScriptParseProcedure32_Release
59 #define IActiveScriptParseProcedure_ParseProcedureText IActiveScriptParseProcedure32_ParseProcedureText
63 static const WCHAR windowW[] = {'w','i','n','d','o','w',0};
64 static const WCHAR emptyW[] = {0};
67 IActiveScriptSite IActiveScriptSite_iface;
68 IActiveScriptSiteInterruptPoll IActiveScriptSiteInterruptPoll_iface;
69 IActiveScriptSiteWindow IActiveScriptSiteWindow_iface;
70 IActiveScriptSiteDebug IActiveScriptSiteDebug_iface;
71 IServiceProvider IServiceProvider_iface;
75 IActiveScript *script;
76 IActiveScriptParse *parse;
77 IActiveScriptParseProcedure *parse_proc;
79 SCRIPTSTATE script_state;
81 HTMLInnerWindow *window;
87 static void set_script_prop(ScriptHost *script_host, DWORD property, VARIANT *val)
89 IActiveScriptProperty *script_prop;
92 hres = IActiveScript_QueryInterface(script_host->script, &IID_IActiveScriptProperty,
93 (void**)&script_prop);
95 WARN("Could not get IActiveScriptProperty iface: %08x\n", hres);
99 hres = IActiveScriptProperty_SetProperty(script_prop, property, NULL, val);
100 IActiveScriptProperty_Release(script_prop);
102 WARN("SetProperty(%x) failed: %08x\n", property, hres);
105 static BOOL init_script_engine(ScriptHost *script_host)
107 IObjectSafety *safety;
109 DWORD supported_opts=0, enabled_opts=0;
113 hres = IActiveScript_QueryInterface(script_host->script, &IID_IActiveScriptParse, (void**)&script_host->parse);
115 WARN("Could not get IActiveScriptHost: %08x\n", hres);
119 hres = IActiveScript_QueryInterface(script_host->script, &IID_IObjectSafety, (void**)&safety);
121 FIXME("Could not get IObjectSafety: %08x\n", hres);
125 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported_opts, &enabled_opts);
127 FIXME("GetInterfaceSafetyOptions failed: %08x\n", hres);
128 }else if(!(supported_opts & INTERFACE_USES_DISPEX)) {
129 FIXME("INTERFACE_USES_DISPEX is not supported\n");
131 hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse,
132 INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER,
133 INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER);
135 FIXME("SetInterfaceSafetyOptions failed: %08x\n", hres);
138 IObjectSafety_Release(safety);
144 set_script_prop(script_host, SCRIPTPROP_INVOKEVERSIONING, &var);
146 V_VT(&var) = VT_BOOL;
147 V_BOOL(&var) = VARIANT_TRUE;
148 set_script_prop(script_host, SCRIPTPROP_HACK_TRIDENTEVENTSINK, &var);
150 hres = IActiveScriptParse_InitNew(script_host->parse);
152 WARN("InitNew failed: %08x\n", hres);
156 hres = IActiveScript_SetScriptSite(script_host->script, &script_host->IActiveScriptSite_iface);
158 WARN("SetScriptSite failed: %08x\n", hres);
159 IActiveScript_Close(script_host->script);
163 hres = IActiveScript_GetScriptState(script_host->script, &state);
165 WARN("GetScriptState failed: %08x\n", hres);
166 else if(state != SCRIPTSTATE_INITIALIZED)
167 FIXME("state = %x\n", state);
169 hres = IActiveScript_SetScriptState(script_host->script, SCRIPTSTATE_STARTED);
171 WARN("Starting script failed: %08x\n", hres);
175 hres = IActiveScript_AddNamedItem(script_host->script, windowW,
176 SCRIPTITEM_ISVISIBLE|SCRIPTITEM_ISSOURCE|SCRIPTITEM_GLOBALMEMBERS);
177 if(SUCCEEDED(hres)) {
178 V_VT(&var) = VT_BOOL;
179 V_BOOL(&var) = VARIANT_TRUE;
180 set_script_prop(script_host, SCRIPTPROP_ABBREVIATE_GLOBALNAME_RESOLUTION, &var);
182 WARN("AddNamedItem failed: %08x\n", hres);
185 hres = IActiveScript_QueryInterface(script_host->script, &IID_IActiveScriptParseProcedure2,
186 (void**)&script_host->parse_proc);
188 /* FIXME: QI for IActiveScriptParseProcedure */
189 WARN("Could not get IActiveScriptParseProcedure iface: %08x\n", hres);
195 static void release_script_engine(ScriptHost *This)
200 switch(This->script_state) {
201 case SCRIPTSTATE_CONNECTED:
202 IActiveScript_SetScriptState(This->script, SCRIPTSTATE_DISCONNECTED);
204 case SCRIPTSTATE_STARTED:
205 case SCRIPTSTATE_DISCONNECTED:
206 case SCRIPTSTATE_INITIALIZED:
207 IActiveScript_Close(This->script);
210 if(This->parse_proc) {
211 IActiveScriptParseProcedure_Release(This->parse_proc);
212 This->parse_proc = NULL;
216 IActiveScriptParse_Release(This->parse);
221 IActiveScript_Release(This->script);
223 This->script_state = SCRIPTSTATE_UNINITIALIZED;
226 void connect_scripts(HTMLInnerWindow *window)
230 LIST_FOR_EACH_ENTRY(iter, &window->script_hosts, ScriptHost, entry) {
231 if(iter->script_state == SCRIPTSTATE_STARTED)
232 IActiveScript_SetScriptState(iter->script, SCRIPTSTATE_CONNECTED);
236 static inline ScriptHost *impl_from_IActiveScriptSite(IActiveScriptSite *iface)
238 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSite_iface);
241 static HRESULT WINAPI ActiveScriptSite_QueryInterface(IActiveScriptSite *iface, REFIID riid, void **ppv)
243 ScriptHost *This = impl_from_IActiveScriptSite(iface);
247 if(IsEqualGUID(&IID_IUnknown, riid)) {
248 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
249 *ppv = &This->IActiveScriptSite_iface;
250 }else if(IsEqualGUID(&IID_IActiveScriptSite, riid)) {
251 TRACE("(%p)->(IID_IActiveScriptSite %p)\n", This, ppv);
252 *ppv = &This->IActiveScriptSite_iface;
253 }else if(IsEqualGUID(&IID_IActiveScriptSiteInterruptPoll, riid)) {
254 TRACE("(%p)->(IID_IActiveScriptSiteInterruprtPoll %p)\n", This, ppv);
255 *ppv = &This->IActiveScriptSiteInterruptPoll_iface;
256 }else if(IsEqualGUID(&IID_IActiveScriptSiteWindow, riid)) {
257 TRACE("(%p)->(IID_IActiveScriptSiteWindow %p)\n", This, ppv);
258 *ppv = &This->IActiveScriptSiteWindow_iface;
259 }else if(IsEqualGUID(&IID_IActiveScriptSiteDebug, riid)) {
260 TRACE("(%p)->(IID_IActiveScriptSiteDebug %p)\n", This, ppv);
261 *ppv = &This->IActiveScriptSiteDebug_iface;
262 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
263 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
264 *ppv = &This->IServiceProvider_iface;
265 }else if(IsEqualGUID(&IID_ICanHandleException, riid)) {
266 TRACE("(%p)->(IID_ICanHandleException not supported %p)\n", This, ppv);
267 return E_NOINTERFACE;
269 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
270 return E_NOINTERFACE;
273 IUnknown_AddRef((IUnknown*)*ppv);
277 static ULONG WINAPI ActiveScriptSite_AddRef(IActiveScriptSite *iface)
279 ScriptHost *This = impl_from_IActiveScriptSite(iface);
280 LONG ref = InterlockedIncrement(&This->ref);
282 TRACE("(%p) ref=%d\n", This, ref);
287 static ULONG WINAPI ActiveScriptSite_Release(IActiveScriptSite *iface)
289 ScriptHost *This = impl_from_IActiveScriptSite(iface);
290 LONG ref = InterlockedDecrement(&This->ref);
292 TRACE("(%p) ref=%d\n", This, ref);
295 release_script_engine(This);
297 list_remove(&This->entry);
304 static HRESULT WINAPI ActiveScriptSite_GetLCID(IActiveScriptSite *iface, LCID *plcid)
306 ScriptHost *This = impl_from_IActiveScriptSite(iface);
308 TRACE("(%p)->(%p)\n", This, plcid);
310 *plcid = GetUserDefaultLCID();
314 static HRESULT WINAPI ActiveScriptSite_GetItemInfo(IActiveScriptSite *iface, LPCOLESTR pstrName,
315 DWORD dwReturnMask, IUnknown **ppiunkItem, ITypeInfo **ppti)
317 ScriptHost *This = impl_from_IActiveScriptSite(iface);
319 TRACE("(%p)->(%s %x %p %p)\n", This, debugstr_w(pstrName), dwReturnMask, ppiunkItem, ppti);
321 if(dwReturnMask != SCRIPTINFO_IUNKNOWN) {
322 FIXME("Unsupported mask %x\n", dwReturnMask);
328 if(strcmpW(pstrName, windowW))
329 return DISP_E_MEMBERNOTFOUND;
334 /* FIXME: Return proxy object */
335 *ppiunkItem = (IUnknown*)&This->window->base.IHTMLWindow2_iface;
336 IUnknown_AddRef(*ppiunkItem);
341 static HRESULT WINAPI ActiveScriptSite_GetDocVersionString(IActiveScriptSite *iface, BSTR *pbstrVersion)
343 ScriptHost *This = impl_from_IActiveScriptSite(iface);
344 FIXME("(%p)->(%p)\n", This, pbstrVersion);
348 static HRESULT WINAPI ActiveScriptSite_OnScriptTerminate(IActiveScriptSite *iface,
349 const VARIANT *pvarResult, const EXCEPINFO *pexcepinfo)
351 ScriptHost *This = impl_from_IActiveScriptSite(iface);
352 FIXME("(%p)->(%p %p)\n", This, pvarResult, pexcepinfo);
356 static HRESULT WINAPI ActiveScriptSite_OnStateChange(IActiveScriptSite *iface, SCRIPTSTATE ssScriptState)
358 ScriptHost *This = impl_from_IActiveScriptSite(iface);
360 TRACE("(%p)->(%x)\n", This, ssScriptState);
362 This->script_state = ssScriptState;
366 static HRESULT WINAPI ActiveScriptSite_OnScriptError(IActiveScriptSite *iface, IActiveScriptError *pscripterror)
368 ScriptHost *This = impl_from_IActiveScriptSite(iface);
369 FIXME("(%p)->(%p)\n", This, pscripterror);
373 static HRESULT WINAPI ActiveScriptSite_OnEnterScript(IActiveScriptSite *iface)
375 ScriptHost *This = impl_from_IActiveScriptSite(iface);
377 TRACE("(%p)->()\n", This);
382 static HRESULT WINAPI ActiveScriptSite_OnLeaveScript(IActiveScriptSite *iface)
384 ScriptHost *This = impl_from_IActiveScriptSite(iface);
386 TRACE("(%p)->()\n", This);
391 static const IActiveScriptSiteVtbl ActiveScriptSiteVtbl = {
392 ActiveScriptSite_QueryInterface,
393 ActiveScriptSite_AddRef,
394 ActiveScriptSite_Release,
395 ActiveScriptSite_GetLCID,
396 ActiveScriptSite_GetItemInfo,
397 ActiveScriptSite_GetDocVersionString,
398 ActiveScriptSite_OnScriptTerminate,
399 ActiveScriptSite_OnStateChange,
400 ActiveScriptSite_OnScriptError,
401 ActiveScriptSite_OnEnterScript,
402 ActiveScriptSite_OnLeaveScript
405 static inline ScriptHost *impl_from_IActiveScriptSiteInterruptPoll(IActiveScriptSiteInterruptPoll *iface)
407 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSiteInterruptPoll_iface);
410 static HRESULT WINAPI ActiveScriptSiteInterruptPoll_QueryInterface(IActiveScriptSiteInterruptPoll *iface,
411 REFIID riid, void **ppv)
413 ScriptHost *This = impl_from_IActiveScriptSiteInterruptPoll(iface);
414 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
417 static ULONG WINAPI ActiveScriptSiteInterruptPoll_AddRef(IActiveScriptSiteInterruptPoll *iface)
419 ScriptHost *This = impl_from_IActiveScriptSiteInterruptPoll(iface);
420 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
423 static ULONG WINAPI ActiveScriptSiteInterruptPoll_Release(IActiveScriptSiteInterruptPoll *iface)
425 ScriptHost *This = impl_from_IActiveScriptSiteInterruptPoll(iface);
426 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
429 static HRESULT WINAPI ActiveScriptSiteInterruptPoll_QueryContinue(IActiveScriptSiteInterruptPoll *iface)
431 ScriptHost *This = impl_from_IActiveScriptSiteInterruptPoll(iface);
433 TRACE("(%p)\n", This);
438 static const IActiveScriptSiteInterruptPollVtbl ActiveScriptSiteInterruptPollVtbl = {
439 ActiveScriptSiteInterruptPoll_QueryInterface,
440 ActiveScriptSiteInterruptPoll_AddRef,
441 ActiveScriptSiteInterruptPoll_Release,
442 ActiveScriptSiteInterruptPoll_QueryContinue
445 static inline ScriptHost *impl_from_IActiveScriptSiteWindow(IActiveScriptSiteWindow *iface)
447 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSiteWindow_iface);
450 static HRESULT WINAPI ActiveScriptSiteWindow_QueryInterface(IActiveScriptSiteWindow *iface,
451 REFIID riid, void **ppv)
453 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
454 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
457 static ULONG WINAPI ActiveScriptSiteWindow_AddRef(IActiveScriptSiteWindow *iface)
459 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
460 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
463 static ULONG WINAPI ActiveScriptSiteWindow_Release(IActiveScriptSiteWindow *iface)
465 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
466 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
469 static HRESULT WINAPI ActiveScriptSiteWindow_GetWindow(IActiveScriptSiteWindow *iface, HWND *phwnd)
471 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
472 FIXME("(%p)->(%p)\n", This, phwnd);
476 static HRESULT WINAPI ActiveScriptSiteWindow_EnableModeless(IActiveScriptSiteWindow *iface, BOOL fEnable)
478 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
479 FIXME("(%p)->(%x)\n", This, fEnable);
483 static const IActiveScriptSiteWindowVtbl ActiveScriptSiteWindowVtbl = {
484 ActiveScriptSiteWindow_QueryInterface,
485 ActiveScriptSiteWindow_AddRef,
486 ActiveScriptSiteWindow_Release,
487 ActiveScriptSiteWindow_GetWindow,
488 ActiveScriptSiteWindow_EnableModeless
491 static inline ScriptHost *impl_from_IActiveScriptSiteDebug(IActiveScriptSiteDebug *iface)
493 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSiteDebug_iface);
496 static HRESULT WINAPI ActiveScriptSiteDebug_QueryInterface(IActiveScriptSiteDebug *iface,
497 REFIID riid, void **ppv)
499 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
500 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
503 static ULONG WINAPI ActiveScriptSiteDebug_AddRef(IActiveScriptSiteDebug *iface)
505 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
506 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
509 static ULONG WINAPI ActiveScriptSiteDebug_Release(IActiveScriptSiteDebug *iface)
511 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
512 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
515 static HRESULT WINAPI ActiveScriptSiteDebug_GetDocumentContextFromPosition(IActiveScriptSiteDebug *iface,
516 CTXARG_T dwSourceContext, ULONG uCharacterOffset, ULONG uNumChars, IDebugDocumentContext **ppsc)
518 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
519 FIXME("(%p)->(%s %u %u %p)\n", This, wine_dbgstr_longlong(dwSourceContext), uCharacterOffset,
524 static HRESULT WINAPI ActiveScriptSiteDebug_GetApplication(IActiveScriptSiteDebug *iface, IDebugApplication **ppda)
526 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
527 FIXME("(%p)->(%p)\n", This, ppda);
531 static HRESULT WINAPI ActiveScriptSiteDebug_GetRootApplicationNode(IActiveScriptSiteDebug *iface,
532 IDebugApplicationNode **ppdanRoot)
534 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
535 FIXME("(%p)->(%p)\n", This, ppdanRoot);
539 static HRESULT WINAPI ActiveScriptSiteDebug_OnScriptErrorDebug(IActiveScriptSiteDebug *iface,
540 IActiveScriptErrorDebug *pErrorDebug, BOOL *pfEnterDebugger, BOOL *pfCallOnScriptErrorWhenContinuing)
542 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
543 FIXME("(%p)->(%p %p %p)\n", This, pErrorDebug, pfEnterDebugger, pfCallOnScriptErrorWhenContinuing);
547 static const IActiveScriptSiteDebugVtbl ActiveScriptSiteDebugVtbl = {
548 ActiveScriptSiteDebug_QueryInterface,
549 ActiveScriptSiteDebug_AddRef,
550 ActiveScriptSiteDebug_Release,
551 ActiveScriptSiteDebug_GetDocumentContextFromPosition,
552 ActiveScriptSiteDebug_GetApplication,
553 ActiveScriptSiteDebug_GetRootApplicationNode,
554 ActiveScriptSiteDebug_OnScriptErrorDebug
557 static inline ScriptHost *impl_from_IServiceProvider(IServiceProvider *iface)
559 return CONTAINING_RECORD(iface, ScriptHost, IServiceProvider_iface);
562 static HRESULT WINAPI ASServiceProvider_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
564 ScriptHost *This = impl_from_IServiceProvider(iface);
565 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
568 static ULONG WINAPI ASServiceProvider_AddRef(IServiceProvider *iface)
570 ScriptHost *This = impl_from_IServiceProvider(iface);
571 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
574 static ULONG WINAPI ASServiceProvider_Release(IServiceProvider *iface)
576 ScriptHost *This = impl_from_IServiceProvider(iface);
577 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
580 static HRESULT WINAPI ASServiceProvider_QueryService(IServiceProvider *iface, REFGUID guidService,
581 REFIID riid, void **ppv)
583 ScriptHost *This = impl_from_IServiceProvider(iface);
585 if(IsEqualGUID(&SID_SInternetHostSecurityManager, guidService)) {
586 TRACE("(%p)->(SID_SInternetHostSecurityManager)\n", This);
588 if(!This->window || !This->window->doc)
589 return E_NOINTERFACE;
591 return IInternetHostSecurityManager_QueryInterface(&This->window->doc->IInternetHostSecurityManager_iface,
595 FIXME("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
596 return E_NOINTERFACE;
599 static const IServiceProviderVtbl ASServiceProviderVtbl = {
600 ASServiceProvider_QueryInterface,
601 ASServiceProvider_AddRef,
602 ASServiceProvider_Release,
603 ASServiceProvider_QueryService
606 static ScriptHost *create_script_host(HTMLInnerWindow *window, const GUID *guid)
611 ret = heap_alloc_zero(sizeof(*ret));
612 ret->IActiveScriptSite_iface.lpVtbl = &ActiveScriptSiteVtbl;
613 ret->IActiveScriptSiteInterruptPoll_iface.lpVtbl = &ActiveScriptSiteInterruptPollVtbl;
614 ret->IActiveScriptSiteWindow_iface.lpVtbl = &ActiveScriptSiteWindowVtbl;
615 ret->IActiveScriptSiteDebug_iface.lpVtbl = &ActiveScriptSiteDebugVtbl;
616 ret->IServiceProvider_iface.lpVtbl = &ASServiceProviderVtbl;
618 ret->window = window;
619 ret->script_state = SCRIPTSTATE_UNINITIALIZED;
622 list_add_tail(&window->script_hosts, &ret->entry);
624 hres = CoCreateInstance(&ret->guid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
625 &IID_IActiveScript, (void**)&ret->script);
627 WARN("Could not load script engine: %08x\n", hres);
628 else if(!init_script_engine(ret))
629 release_script_engine(ret);
634 static void parse_text(ScriptHost *script_host, LPCWSTR text)
640 static const WCHAR script_endW[] = {'<','/','S','C','R','I','P','T','>',0};
642 TRACE("%s\n", debugstr_w(text));
644 script_host->window->current_script_guid = script_host->guid;
647 memset(&excepinfo, 0, sizeof(excepinfo));
649 hres = IActiveScriptParse_ParseScriptText(script_host->parse, text, windowW, NULL, script_endW,
650 0, 0, SCRIPTTEXT_ISVISIBLE|SCRIPTTEXT_HOSTMANAGESSOURCE,
655 WARN("<<< %08x\n", hres);
659 static void parse_extern_script(ScriptHost *script_host, LPCWSTR src)
665 static const WCHAR wine_schemaW[] = {'w','i','n','e',':'};
667 if(strlenW(src) > sizeof(wine_schemaW)/sizeof(WCHAR) && !memcmp(src, wine_schemaW, sizeof(wine_schemaW)))
668 src += sizeof(wine_schemaW)/sizeof(WCHAR);
670 hres = CreateURLMoniker(NULL, src, &mon);
674 hres = bind_mon_to_wstr(script_host->window, mon, &text);
675 IMoniker_Release(mon);
679 parse_text(script_host, text);
684 static void parse_inline_script(ScriptHost *script_host, nsIDOMHTMLScriptElement *nsscript)
686 const PRUnichar *text;
690 nsAString_Init(&text_str, NULL);
692 nsres = nsIDOMHTMLScriptElement_GetText(nsscript, &text_str);
694 if(NS_SUCCEEDED(nsres)) {
695 nsAString_GetData(&text_str, &text);
696 parse_text(script_host, text);
698 ERR("GetText failed: %08x\n", nsres);
701 nsAString_Finish(&text_str);
704 static void parse_script_elem(ScriptHost *script_host, nsIDOMHTMLScriptElement *nsscript)
706 const PRUnichar *src;
710 nsAString_Init(&src_str, NULL);
712 nsres = nsIDOMHTMLScriptElement_GetSrc(nsscript, &src_str);
713 nsAString_GetData(&src_str, &src);
716 ERR("GetSrc failed: %08x\n", nsres);
718 parse_extern_script(script_host, src);
720 parse_inline_script(script_host, nsscript);
722 nsAString_Finish(&src_str);
725 static BOOL get_guid_from_type(LPCWSTR type, GUID *guid)
727 const WCHAR text_javascriptW[] =
728 {'t','e','x','t','/','j','a','v','a','s','c','r','i','p','t',0};
729 const WCHAR text_vbscriptW[] =
730 {'t','e','x','t','/','v','b','s','c','r','i','p','t',0};
732 /* FIXME: Handle more types */
733 if(!strcmpiW(type, text_javascriptW)) {
734 *guid = CLSID_JScript;
735 }else if(!strcmpiW(type, text_vbscriptW)) {
736 *guid = CLSID_VBScript;
738 FIXME("Unknown type %s\n", debugstr_w(type));
745 static BOOL get_guid_from_language(LPCWSTR type, GUID *guid)
749 hres = CLSIDFromProgID(type, guid);
753 /* FIXME: Check CATID_ActiveScriptParse */
758 static BOOL get_script_guid(HTMLInnerWindow *window, nsIDOMHTMLScriptElement *nsscript, GUID *guid)
760 nsAString attr_str, val_str;
764 static const PRUnichar languageW[] = {'l','a','n','g','u','a','g','e',0};
766 nsAString_Init(&val_str, NULL);
768 nsres = nsIDOMHTMLScriptElement_GetType(nsscript, &val_str);
769 if(NS_SUCCEEDED(nsres)) {
770 const PRUnichar *type;
772 nsAString_GetData(&val_str, &type);
774 ret = get_guid_from_type(type, guid);
775 nsAString_Finish(&val_str);
779 ERR("GetType failed: %08x\n", nsres);
782 nsAString_InitDepend(&attr_str, languageW);
783 nsres = nsIDOMHTMLScriptElement_GetAttribute(nsscript, &attr_str, &val_str);
784 nsAString_Finish(&attr_str);
785 if(NS_SUCCEEDED(nsres)) {
786 const PRUnichar *language;
788 nsAString_GetData(&val_str, &language);
791 ret = get_guid_from_language(language, guid);
793 *guid = window->current_script_guid;
797 ERR("GetAttribute(language) failed: %08x\n", nsres);
800 nsAString_Finish(&val_str);
805 static ScriptHost *get_script_host(HTMLInnerWindow *window, const GUID *guid)
809 LIST_FOR_EACH_ENTRY(iter, &window->script_hosts, ScriptHost, entry) {
810 if(IsEqualGUID(guid, &iter->guid))
814 return create_script_host(window, guid);
817 void doc_insert_script(HTMLInnerWindow *window, nsIDOMHTMLScriptElement *nsscript)
819 ScriptHost *script_host;
822 if(!get_script_guid(window, nsscript, &guid)) {
823 WARN("Could not find script GUID\n");
827 if(IsEqualGUID(&CLSID_JScript, &guid)
828 && (!window->base.outer_window || window->base.outer_window->scriptmode != SCRIPTMODE_ACTIVESCRIPT)) {
829 TRACE("Ignoring JScript\n");
833 script_host = get_script_host(window, &guid);
837 if(script_host->parse)
838 parse_script_elem(script_host, nsscript);
841 IDispatch *script_parse_event(HTMLInnerWindow *window, LPCWSTR text)
843 ScriptHost *script_host;
849 static const WCHAR delimiterW[] = {'\"',0};
851 TRACE("%s\n", debugstr_w(text));
853 for(ptr = text; isalnumW(*ptr); ptr++);
858 language = heap_alloc((ptr-text+1)*sizeof(WCHAR));
859 memcpy(language, text, (ptr-text)*sizeof(WCHAR));
860 language[ptr-text] = 0;
862 b = get_guid_from_language(language, &guid);
867 WARN("Could not find language\n");
874 guid = window->current_script_guid;
877 if(IsEqualGUID(&CLSID_JScript, &guid)
878 && (!window->base.outer_window || window->base.outer_window->scriptmode != SCRIPTMODE_ACTIVESCRIPT)) {
879 TRACE("Ignoring JScript\n");
883 window->current_script_guid = guid;
885 script_host = get_script_host(window, &guid);
886 if(!script_host || !script_host->parse_proc)
889 hres = IActiveScriptParseProcedure_ParseProcedureText(script_host->parse_proc, ptr, NULL, emptyW,
890 NULL, NULL, delimiterW, 0 /* FIXME */, 0,
891 SCRIPTPROC_HOSTMANAGESSOURCE|SCRIPTPROC_IMPLICIT_THIS|SCRIPTPROC_IMPLICIT_PARENTS, &disp);
893 WARN("ParseProcedureText failed: %08x\n", hres);
897 TRACE("ret %p\n", disp);
901 HRESULT exec_script(HTMLInnerWindow *window, const WCHAR *code, const WCHAR *lang, VARIANT *ret)
903 ScriptHost *script_host;
908 static const WCHAR delimW[] = {'"',0};
910 if(!get_guid_from_language(lang, &guid)) {
911 WARN("Could not find script GUID\n");
912 return CO_E_CLASSSTRING;
915 script_host = get_script_host(window, &guid);
917 FIXME("No script host\n");
921 if(!script_host->parse) {
922 FIXME("script_host->parse == NULL\n");
926 window->current_script_guid = guid;
928 memset(&ei, 0, sizeof(ei));
930 hres = IActiveScriptParse_ParseScriptText(script_host->parse, code, NULL, NULL, delimW, 0, 0, SCRIPTTEXT_ISVISIBLE, ret, &ei);
934 WARN("<<< %08x\n", hres);
939 IDispatch *get_script_disp(ScriptHost *script_host)
944 if(!script_host->script)
947 hres = IActiveScript_GetScriptDispatch(script_host->script, windowW, &disp);
954 BOOL find_global_prop(HTMLInnerWindow *window, BSTR name, DWORD flags, ScriptHost **ret_host, DISPID *ret_id)
961 LIST_FOR_EACH_ENTRY(iter, &window->script_hosts, ScriptHost, entry) {
962 disp = get_script_disp(iter);
966 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
967 if(SUCCEEDED(hres)) {
968 hres = IDispatchEx_GetDispID(dispex, name, flags & (~fdexNameEnsure), ret_id);
969 IDispatchEx_Release(dispex);
971 FIXME("No IDispatchEx\n");
975 IDispatch_Release(disp);
976 if(SUCCEEDED(hres)) {
985 static BOOL is_jscript_available(void)
987 static BOOL available, checked;
991 HRESULT hres = CoGetClassObject(&CLSID_JScript, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void**)&unk);
993 if(SUCCEEDED(hres)) {
995 IUnknown_Release(unk);
1005 void set_script_mode(HTMLOuterWindow *window, SCRIPTMODE mode)
1007 nsIWebBrowserSetup *setup;
1010 if(mode == SCRIPTMODE_ACTIVESCRIPT && !is_jscript_available()) {
1011 TRACE("jscript.dll not available\n");
1012 window->scriptmode = SCRIPTMODE_GECKO;
1016 window->scriptmode = mode;
1018 if(!window->doc_obj->nscontainer || !window->doc_obj->nscontainer->webbrowser)
1021 nsres = nsIWebBrowser_QueryInterface(window->doc_obj->nscontainer->webbrowser,
1022 &IID_nsIWebBrowserSetup, (void**)&setup);
1023 if(NS_SUCCEEDED(nsres)) {
1024 nsres = nsIWebBrowserSetup_SetProperty(setup, SETUP_ALLOW_JAVASCRIPT,
1025 window->scriptmode == SCRIPTMODE_GECKO);
1026 nsIWebBrowserSetup_Release(setup);
1029 if(NS_FAILED(nsres))
1030 ERR("JavaScript setup failed: %08x\n", nsres);
1033 void release_script_hosts(HTMLInnerWindow *window)
1037 while(!list_empty(&window->script_hosts)) {
1038 iter = LIST_ENTRY(list_head(&window->script_hosts), ScriptHost, entry);
1040 release_script_engine(iter);
1041 list_remove(&iter->entry);
1042 iter->window = NULL;
1043 IActiveScriptSite_Release(&iter->IActiveScriptSite_iface);