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 const IActiveScriptSiteVtbl
*lpIActiveScriptSiteVtbl
;
55 const IActiveScriptSiteInterruptPollVtbl
*lpIActiveScriptSiteInterruptPollVtbl
;
56 const IActiveScriptSiteWindowVtbl
*lpIActiveScriptSiteWindowVtbl
;
57 const IActiveScriptSiteDebugVtbl
*lpIActiveScriptSiteDebugVtbl
;
58 const IServiceProviderVtbl
*lpServiceProviderVtbl
;
62 IActiveScript
*script
;
63 IActiveScriptParse
*parse
;
64 IActiveScriptParseProcedure
*parse_proc
;
66 SCRIPTSTATE script_state
;
74 #define ACTSCPSITE(x) ((IActiveScriptSite*) &(x)->lpIActiveScriptSiteVtbl)
75 #define ACTSCPPOLL(x) (&(x)->lpIActiveScriptSiteInterruptPollVtbl)
76 #define ACTSCPWIN(x) (&(x)->lpIActiveScriptSiteWindowVtbl)
77 #define ACTSCPDBG(x) (&(x)->lpIActiveScriptSiteDebugVtbl)
79 static void set_script_prop(ScriptHost
*script_host
, DWORD property
, VARIANT
*val
)
81 IActiveScriptProperty
*script_prop
;
84 hres
= IActiveScript_QueryInterface(script_host
->script
, &IID_IActiveScriptProperty
,
85 (void**)&script_prop
);
87 WARN("Could not get IActiveScriptProperty iface: %08x\n", hres
);
91 hres
= IActiveScriptProperty_SetProperty(script_prop
, property
, NULL
, val
);
92 IActiveScriptProperty_Release(script_prop
);
94 WARN("SetProperty(%x) failed: %08x\n", property
, hres
);
97 static BOOL
init_script_engine(ScriptHost
*script_host
)
99 IObjectSafety
*safety
;
101 DWORD supported_opts
=0, enabled_opts
=0;
105 hres
= IActiveScript_QueryInterface(script_host
->script
, &IID_IActiveScriptParse
, (void**)&script_host
->parse
);
107 WARN("Could not get IActiveScriptHost: %08x\n", hres
);
111 hres
= IActiveScript_QueryInterface(script_host
->script
, &IID_IObjectSafety
, (void**)&safety
);
113 FIXME("Could not get IObjectSafety: %08x\n", hres
);
117 hres
= IObjectSafety_GetInterfaceSafetyOptions(safety
, &IID_IActiveScriptParse
, &supported_opts
, &enabled_opts
);
119 FIXME("GetInterfaceSafetyOptions failed: %08x\n", hres
);
120 }else if(!(supported_opts
& INTERFACE_USES_DISPEX
)) {
121 FIXME("INTERFACE_USES_DISPEX is not supported\n");
123 hres
= IObjectSafety_SetInterfaceSafetyOptions(safety
, &IID_IActiveScriptParse
,
124 INTERFACESAFE_FOR_UNTRUSTED_DATA
|INTERFACE_USES_DISPEX
|INTERFACE_USES_SECURITY_MANAGER
,
125 INTERFACESAFE_FOR_UNTRUSTED_DATA
|INTERFACE_USES_DISPEX
|INTERFACE_USES_SECURITY_MANAGER
);
127 FIXME("SetInterfaceSafetyOptions failed: %08x\n", hres
);
130 IObjectSafety_Release(safety
);
136 set_script_prop(script_host
, SCRIPTPROP_INVOKEVERSIONING
, &var
);
138 V_VT(&var
) = VT_BOOL
;
139 V_BOOL(&var
) = VARIANT_TRUE
;
140 set_script_prop(script_host
, SCRIPTPROP_HACK_TRIDENTEVENTSINK
, &var
);
142 hres
= IActiveScriptParse64_InitNew(script_host
->parse
);
144 WARN("InitNew failed: %08x\n", hres
);
148 hres
= IActiveScript_SetScriptSite(script_host
->script
, ACTSCPSITE(script_host
));
150 WARN("SetScriptSite failed: %08x\n", hres
);
151 IActiveScript_Close(script_host
->script
);
155 hres
= IActiveScript_GetScriptState(script_host
->script
, &state
);
157 WARN("GetScriptState failed: %08x\n", hres
);
158 else if(state
!= SCRIPTSTATE_INITIALIZED
)
159 FIXME("state = %x\n", state
);
161 hres
= IActiveScript_SetScriptState(script_host
->script
, SCRIPTSTATE_STARTED
);
163 WARN("Starting script failed: %08x\n", hres
);
167 hres
= IActiveScript_AddNamedItem(script_host
->script
, windowW
,
168 SCRIPTITEM_ISVISIBLE
|SCRIPTITEM_ISSOURCE
|SCRIPTITEM_GLOBALMEMBERS
);
169 if(SUCCEEDED(hres
)) {
170 V_VT(&var
) = VT_BOOL
;
171 V_BOOL(&var
) = VARIANT_TRUE
;
172 set_script_prop(script_host
, SCRIPTPROP_ABBREVIATE_GLOBALNAME_RESOLUTION
, &var
);
174 WARN("AddNamedItem failed: %08x\n", hres
);
177 hres
= IActiveScript_QueryInterface(script_host
->script
, &IID_IActiveScriptParseProcedure2
,
178 (void**)&script_host
->parse_proc
);
180 /* FIXME: QI for IActiveScriptParseProcedure */
181 WARN("Could not get IActiveScriptParseProcedure iface: %08x\n", hres
);
187 static void release_script_engine(ScriptHost
*This
)
192 switch(This
->script_state
) {
193 case SCRIPTSTATE_CONNECTED
:
194 IActiveScript_SetScriptState(This
->script
, SCRIPTSTATE_DISCONNECTED
);
196 case SCRIPTSTATE_STARTED
:
197 case SCRIPTSTATE_DISCONNECTED
:
198 case SCRIPTSTATE_INITIALIZED
:
199 IActiveScript_Close(This
->script
);
202 if(This
->parse_proc
) {
203 IUnknown_Release(This
->parse_proc
);
204 This
->parse_proc
= NULL
;
208 IUnknown_Release(This
->parse
);
213 IActiveScript_Release(This
->script
);
215 This
->script_state
= SCRIPTSTATE_UNINITIALIZED
;
218 void connect_scripts(HTMLWindow
*window
)
222 LIST_FOR_EACH_ENTRY(iter
, &window
->script_hosts
, ScriptHost
, entry
) {
223 if(iter
->script_state
== SCRIPTSTATE_STARTED
)
224 IActiveScript_SetScriptState(iter
->script
, SCRIPTSTATE_CONNECTED
);
228 #define ACTSCPSITE_THIS(iface) DEFINE_THIS(ScriptHost, IActiveScriptSite, iface)
230 static HRESULT WINAPI
ActiveScriptSite_QueryInterface(IActiveScriptSite
*iface
, REFIID riid
, void **ppv
)
232 ScriptHost
*This
= ACTSCPSITE_THIS(iface
);
236 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
237 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
238 *ppv
= ACTSCPSITE(This
);
239 }else if(IsEqualGUID(&IID_IActiveScriptSite
, riid
)) {
240 TRACE("(%p)->(IID_IActiveScriptSite %p)\n", This
, ppv
);
241 *ppv
= ACTSCPSITE(This
);
242 }else if(IsEqualGUID(&IID_IActiveScriptSiteInterruptPoll
, riid
)) {
243 TRACE("(%p)->(IID_IActiveScriptSiteInterruprtPoll %p)\n", This
, ppv
);
244 *ppv
= ACTSCPPOLL(This
);
245 }else if(IsEqualGUID(&IID_IActiveScriptSiteWindow
, riid
)) {
246 TRACE("(%p)->(IID_IActiveScriptSiteWindow %p)\n", This
, ppv
);
247 *ppv
= ACTSCPWIN(This
);
248 }else if(IsEqualGUID(&IID_IActiveScriptSiteDebug
, riid
)) {
249 TRACE("(%p)->(IID_IActiveScriptSiteDebug %p)\n", This
, ppv
);
250 *ppv
= ACTSCPDBG(This
);
251 }else if(IsEqualGUID(&IID_IServiceProvider
, riid
)) {
252 TRACE("(%p)->(IID_IServiceProvider %p)\n", This
, ppv
);
253 *ppv
= SERVPROV(This
);
254 }else if(IsEqualGUID(&IID_ICanHandleException
, riid
)) {
255 TRACE("(%p)->(IID_ICanHandleException not supported %p)\n", This
, ppv
);
256 return E_NOINTERFACE
;
258 FIXME("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
259 return E_NOINTERFACE
;
262 IUnknown_AddRef((IUnknown
*)*ppv
);
266 static ULONG WINAPI
ActiveScriptSite_AddRef(IActiveScriptSite
*iface
)
268 ScriptHost
*This
= ACTSCPSITE_THIS(iface
);
269 LONG ref
= InterlockedIncrement(&This
->ref
);
271 TRACE("(%p) ref=%d\n", This
, ref
);
276 static ULONG WINAPI
ActiveScriptSite_Release(IActiveScriptSite
*iface
)
278 ScriptHost
*This
= ACTSCPSITE_THIS(iface
);
279 LONG ref
= InterlockedDecrement(&This
->ref
);
281 TRACE("(%p) ref=%d\n", This
, ref
);
284 release_script_engine(This
);
286 list_remove(&This
->entry
);
293 static HRESULT WINAPI
ActiveScriptSite_GetLCID(IActiveScriptSite
*iface
, LCID
*plcid
)
295 ScriptHost
*This
= ACTSCPSITE_THIS(iface
);
297 TRACE("(%p)->(%p)\n", This
, plcid
);
299 *plcid
= GetUserDefaultLCID();
303 static HRESULT WINAPI
ActiveScriptSite_GetItemInfo(IActiveScriptSite
*iface
, LPCOLESTR pstrName
,
304 DWORD dwReturnMask
, IUnknown
**ppiunkItem
, ITypeInfo
**ppti
)
306 ScriptHost
*This
= ACTSCPSITE_THIS(iface
);
308 TRACE("(%p)->(%s %x %p %p)\n", This
, debugstr_w(pstrName
), dwReturnMask
, ppiunkItem
, ppti
);
310 if(dwReturnMask
!= SCRIPTINFO_IUNKNOWN
) {
311 FIXME("Unsupported mask %x\n", dwReturnMask
);
317 if(strcmpW(pstrName
, windowW
))
318 return DISP_E_MEMBERNOTFOUND
;
323 /* FIXME: Return proxy object */
324 *ppiunkItem
= (IUnknown
*)HTMLWINDOW2(This
->window
);
325 IUnknown_AddRef(*ppiunkItem
);
330 static HRESULT WINAPI
ActiveScriptSite_GetDocVersionString(IActiveScriptSite
*iface
, BSTR
*pbstrVersion
)
332 ScriptHost
*This
= ACTSCPSITE_THIS(iface
);
333 FIXME("(%p)->(%p)\n", This
, pbstrVersion
);
337 static HRESULT WINAPI
ActiveScriptSite_OnScriptTerminate(IActiveScriptSite
*iface
,
338 const VARIANT
*pvarResult
, const EXCEPINFO
*pexcepinfo
)
340 ScriptHost
*This
= ACTSCPSITE_THIS(iface
);
341 FIXME("(%p)->(%p %p)\n", This
, pvarResult
, pexcepinfo
);
345 static HRESULT WINAPI
ActiveScriptSite_OnStateChange(IActiveScriptSite
*iface
, SCRIPTSTATE ssScriptState
)
347 ScriptHost
*This
= ACTSCPSITE_THIS(iface
);
349 TRACE("(%p)->(%x)\n", This
, ssScriptState
);
351 This
->script_state
= ssScriptState
;
355 static HRESULT WINAPI
ActiveScriptSite_OnScriptError(IActiveScriptSite
*iface
, IActiveScriptError
*pscripterror
)
357 ScriptHost
*This
= ACTSCPSITE_THIS(iface
);
358 FIXME("(%p)->(%p)\n", This
, pscripterror
);
362 static HRESULT WINAPI
ActiveScriptSite_OnEnterScript(IActiveScriptSite
*iface
)
364 ScriptHost
*This
= ACTSCPSITE_THIS(iface
);
366 TRACE("(%p)->()\n", This
);
371 static HRESULT WINAPI
ActiveScriptSite_OnLeaveScript(IActiveScriptSite
*iface
)
373 ScriptHost
*This
= ACTSCPSITE_THIS(iface
);
375 TRACE("(%p)->()\n", This
);
380 #undef ACTSCPSITE_THIS
382 static const IActiveScriptSiteVtbl ActiveScriptSiteVtbl
= {
383 ActiveScriptSite_QueryInterface
,
384 ActiveScriptSite_AddRef
,
385 ActiveScriptSite_Release
,
386 ActiveScriptSite_GetLCID
,
387 ActiveScriptSite_GetItemInfo
,
388 ActiveScriptSite_GetDocVersionString
,
389 ActiveScriptSite_OnScriptTerminate
,
390 ActiveScriptSite_OnStateChange
,
391 ActiveScriptSite_OnScriptError
,
392 ActiveScriptSite_OnEnterScript
,
393 ActiveScriptSite_OnLeaveScript
396 #define ACTSCPPOLL_THIS(iface) DEFINE_THIS(ScriptHost, IActiveScriptSiteInterruptPoll, iface)
398 static HRESULT WINAPI
ActiveScriptSiteInterruptPoll_QueryInterface(IActiveScriptSiteInterruptPoll
*iface
,
399 REFIID riid
, void **ppv
)
401 ScriptHost
*This
= ACTSCPPOLL_THIS(iface
);
402 return IActiveScriptSite_QueryInterface(ACTSCPSITE(This
), riid
, ppv
);
405 static ULONG WINAPI
ActiveScriptSiteInterruptPoll_AddRef(IActiveScriptSiteInterruptPoll
*iface
)
407 ScriptHost
*This
= ACTSCPPOLL_THIS(iface
);
408 return IActiveScriptSite_AddRef(ACTSCPSITE(This
));
411 static ULONG WINAPI
ActiveScriptSiteInterruptPoll_Release(IActiveScriptSiteInterruptPoll
*iface
)
413 ScriptHost
*This
= ACTSCPPOLL_THIS(iface
);
414 return IActiveScriptSite_Release(ACTSCPSITE(This
));
417 static HRESULT WINAPI
ActiveScriptSiteInterruptPoll_QueryContinue(IActiveScriptSiteInterruptPoll
*iface
)
419 ScriptHost
*This
= ACTSCPPOLL_THIS(iface
);
421 TRACE("(%p)\n", This
);
426 #undef ACTSCPPOLL_THIS
428 static const IActiveScriptSiteInterruptPollVtbl ActiveScriptSiteInterruptPollVtbl
= {
429 ActiveScriptSiteInterruptPoll_QueryInterface
,
430 ActiveScriptSiteInterruptPoll_AddRef
,
431 ActiveScriptSiteInterruptPoll_Release
,
432 ActiveScriptSiteInterruptPoll_QueryContinue
435 #define ACTSCPWIN_THIS(iface) DEFINE_THIS(ScriptHost, IActiveScriptSiteWindow, iface)
437 static HRESULT WINAPI
ActiveScriptSiteWindow_QueryInterface(IActiveScriptSiteWindow
*iface
,
438 REFIID riid
, void **ppv
)
440 ScriptHost
*This
= ACTSCPWIN_THIS(iface
);
441 return IActiveScriptSite_QueryInterface(ACTSCPSITE(This
), riid
, ppv
);
444 static ULONG WINAPI
ActiveScriptSiteWindow_AddRef(IActiveScriptSiteWindow
*iface
)
446 ScriptHost
*This
= ACTSCPWIN_THIS(iface
);
447 return IActiveScriptSite_AddRef(ACTSCPSITE(This
));
450 static ULONG WINAPI
ActiveScriptSiteWindow_Release(IActiveScriptSiteWindow
*iface
)
452 ScriptHost
*This
= ACTSCPWIN_THIS(iface
);
453 return IActiveScriptSite_Release(ACTSCPSITE(This
));
456 static HRESULT WINAPI
ActiveScriptSiteWindow_GetWindow(IActiveScriptSiteWindow
*iface
, HWND
*phwnd
)
458 ScriptHost
*This
= ACTSCPWIN_THIS(iface
);
459 FIXME("(%p)->(%p)\n", This
, phwnd
);
463 static HRESULT WINAPI
ActiveScriptSiteWindow_EnableModeless(IActiveScriptSiteWindow
*iface
, BOOL fEnable
)
465 ScriptHost
*This
= ACTSCPWIN_THIS(iface
);
466 FIXME("(%p)->(%x)\n", This
, fEnable
);
470 #undef ACTSCPWIN_THIS
472 static const IActiveScriptSiteWindowVtbl ActiveScriptSiteWindowVtbl
= {
473 ActiveScriptSiteWindow_QueryInterface
,
474 ActiveScriptSiteWindow_AddRef
,
475 ActiveScriptSiteWindow_Release
,
476 ActiveScriptSiteWindow_GetWindow
,
477 ActiveScriptSiteWindow_EnableModeless
480 #define ACTSCPDBG_THIS(iface) DEFINE_THIS(ScriptHost, IActiveScriptSiteDebug, iface)
482 static HRESULT WINAPI
ActiveScriptSiteDebug_QueryInterface(IActiveScriptSiteDebug
*iface
,
483 REFIID riid
, void **ppv
)
485 ScriptHost
*This
= ACTSCPDBG_THIS(iface
);
486 return IActiveScriptSite_QueryInterface(ACTSCPSITE(This
), riid
, ppv
);
489 static ULONG WINAPI
ActiveScriptSiteDebug_AddRef(IActiveScriptSiteDebug
*iface
)
491 ScriptHost
*This
= ACTSCPDBG_THIS(iface
);
492 return IActiveScriptSite_AddRef(ACTSCPSITE(This
));
495 static ULONG WINAPI
ActiveScriptSiteDebug_Release(IActiveScriptSiteDebug
*iface
)
497 ScriptHost
*This
= ACTSCPDBG_THIS(iface
);
498 return IActiveScriptSite_Release(ACTSCPSITE(This
));
501 static HRESULT WINAPI
ActiveScriptSiteDebug_GetDocumentContextFromPosition(IActiveScriptSiteDebug
*iface
,
502 CTXARG_T dwSourceContext
, ULONG uCharacterOffset
, ULONG uNumChars
, IDebugDocumentContext
**ppsc
)
504 ScriptHost
*This
= ACTSCPDBG_THIS(iface
);
505 FIXME("(%p)->(%s %u %u %p)\n", This
, wine_dbgstr_longlong(dwSourceContext
), uCharacterOffset
,
510 static HRESULT WINAPI
ActiveScriptSiteDebug_GetApplication(IActiveScriptSiteDebug
*iface
, IDebugApplication
**ppda
)
512 ScriptHost
*This
= ACTSCPDBG_THIS(iface
);
513 FIXME("(%p)->(%p)\n", This
, ppda
);
517 static HRESULT WINAPI
ActiveScriptSiteDebug_GetRootApplicationNode(IActiveScriptSiteDebug
*iface
,
518 IDebugApplicationNode
**ppdanRoot
)
520 ScriptHost
*This
= ACTSCPDBG_THIS(iface
);
521 FIXME("(%p)->(%p)\n", This
, ppdanRoot
);
525 static HRESULT WINAPI
ActiveScriptSiteDebug_OnScriptErrorDebug(IActiveScriptSiteDebug
*iface
,
526 IActiveScriptErrorDebug
*pErrorDebug
, BOOL
*pfEnterDebugger
, BOOL
*pfCallOnScriptErrorWhenContinuing
)
528 ScriptHost
*This
= ACTSCPDBG_THIS(iface
);
529 FIXME("(%p)->(%p %p %p)\n", This
, pErrorDebug
, pfEnterDebugger
, pfCallOnScriptErrorWhenContinuing
);
533 #undef ACTSCPDBG_THIS
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 #define SERVPROV_THIS(iface) DEFINE_THIS(ScriptHost, ServiceProvider, iface)
547 static HRESULT WINAPI
ASServiceProvider_QueryInterface(IServiceProvider
*iface
, REFIID riid
, void **ppv
)
549 ScriptHost
*This
= SERVPROV_THIS(iface
);
550 return IActiveScriptSite_QueryInterface(ACTSCPSITE(This
), riid
, ppv
);
553 static ULONG WINAPI
ASServiceProvider_AddRef(IServiceProvider
*iface
)
555 ScriptHost
*This
= SERVPROV_THIS(iface
);
556 return IActiveScriptSite_AddRef(ACTSCPSITE(This
));
559 static ULONG WINAPI
ASServiceProvider_Release(IServiceProvider
*iface
)
561 ScriptHost
*This
= SERVPROV_THIS(iface
);
562 return IActiveScriptSite_Release(ACTSCPSITE(This
));
565 static HRESULT WINAPI
ASServiceProvider_QueryService(IServiceProvider
*iface
, REFGUID guidService
,
566 REFIID riid
, void **ppv
)
568 ScriptHost
*This
= SERVPROV_THIS(iface
);
570 if(IsEqualGUID(&SID_SInternetHostSecurityManager
, guidService
)) {
571 TRACE("(%p)->(SID_SInternetHostSecurityManager)\n", This
);
573 if(!This
->window
|| !This
->window
->doc
)
574 return E_NOINTERFACE
;
576 return IInternetHostSecurityManager_QueryInterface(HOSTSECMGR(This
->window
->doc
), riid
, ppv
);
579 FIXME("(%p)->(%s %s %p)\n", This
, debugstr_guid(guidService
), debugstr_guid(riid
), ppv
);
580 return E_NOINTERFACE
;
585 static const IServiceProviderVtbl ASServiceProviderVtbl
= {
586 ASServiceProvider_QueryInterface
,
587 ASServiceProvider_AddRef
,
588 ASServiceProvider_Release
,
589 ASServiceProvider_QueryService
592 static ScriptHost
*create_script_host(HTMLWindow
*window
, const GUID
*guid
)
597 ret
= heap_alloc_zero(sizeof(*ret
));
598 ret
->lpIActiveScriptSiteVtbl
= &ActiveScriptSiteVtbl
;
599 ret
->lpIActiveScriptSiteInterruptPollVtbl
= &ActiveScriptSiteInterruptPollVtbl
;
600 ret
->lpIActiveScriptSiteWindowVtbl
= &ActiveScriptSiteWindowVtbl
;
601 ret
->lpIActiveScriptSiteDebugVtbl
= &ActiveScriptSiteDebugVtbl
;
602 ret
->lpServiceProviderVtbl
= &ASServiceProviderVtbl
;
604 ret
->window
= window
;
605 ret
->script_state
= SCRIPTSTATE_UNINITIALIZED
;
608 list_add_tail(&window
->script_hosts
, &ret
->entry
);
610 hres
= CoCreateInstance(&ret
->guid
, NULL
, CLSCTX_INPROC_SERVER
|CLSCTX_INPROC_HANDLER
,
611 &IID_IActiveScript
, (void**)&ret
->script
);
613 WARN("Could not load script engine: %08x\n", hres
);
614 else if(!init_script_engine(ret
))
615 release_script_engine(ret
);
620 static void parse_text(ScriptHost
*script_host
, LPCWSTR text
)
626 static const WCHAR script_endW
[] = {'<','/','S','C','R','I','P','T','>',0};
628 TRACE("%s\n", debugstr_w(text
));
631 memset(&excepinfo
, 0, sizeof(excepinfo
));
633 hres
= IActiveScriptParse64_ParseScriptText(script_host
->parse
, text
, windowW
, NULL
, script_endW
,
634 0, 0, SCRIPTTEXT_ISVISIBLE
|SCRIPTTEXT_HOSTMANAGESSOURCE
,
639 WARN("<<< %08x\n", hres
);
643 static void parse_extern_script(ScriptHost
*script_host
, LPCWSTR src
)
651 static const WCHAR wine_schemaW
[] = {'w','i','n','e',':'};
653 if(strlenW(src
) > sizeof(wine_schemaW
)/sizeof(WCHAR
) && !memcmp(src
, wine_schemaW
, sizeof(wine_schemaW
)))
654 src
+= sizeof(wine_schemaW
)/sizeof(WCHAR
);
656 hres
= CreateURLMoniker(NULL
, src
, &mon
);
660 hres
= bind_mon_to_buffer(script_host
->window
->doc
, mon
, (void**)&buf
, &size
);
661 IMoniker_Release(mon
);
665 len
= MultiByteToWideChar(CP_ACP
, 0, buf
, size
, NULL
, 0);
666 text
= heap_alloc((len
+1)*sizeof(WCHAR
));
667 MultiByteToWideChar(CP_ACP
, 0, buf
, size
, text
, len
);
671 parse_text(script_host
, text
);
676 static void parse_inline_script(ScriptHost
*script_host
, nsIDOMHTMLScriptElement
*nsscript
)
678 const PRUnichar
*text
;
682 nsAString_Init(&text_str
, NULL
);
684 nsres
= nsIDOMHTMLScriptElement_GetText(nsscript
, &text_str
);
686 if(NS_SUCCEEDED(nsres
)) {
687 nsAString_GetData(&text_str
, &text
);
688 parse_text(script_host
, text
);
690 ERR("GetText failed: %08x\n", nsres
);
693 nsAString_Finish(&text_str
);
696 static void parse_script_elem(ScriptHost
*script_host
, nsIDOMHTMLScriptElement
*nsscript
)
698 const PRUnichar
*src
;
702 nsAString_Init(&src_str
, NULL
);
704 nsres
= nsIDOMHTMLScriptElement_GetSrc(nsscript
, &src_str
);
705 nsAString_GetData(&src_str
, &src
);
708 ERR("GetSrc failed: %08x\n", nsres
);
710 parse_extern_script(script_host
, src
);
712 parse_inline_script(script_host
, nsscript
);
714 nsAString_Finish(&src_str
);
717 static BOOL
get_guid_from_type(LPCWSTR type
, GUID
*guid
)
719 const WCHAR text_javascriptW
[] =
720 {'t','e','x','t','/','j','a','v','a','s','c','r','i','p','t',0};
722 /* FIXME: Handle more types */
723 if(!strcmpiW(type
, text_javascriptW
)) {
724 *guid
= CLSID_JScript
;
726 FIXME("Unknown type %s\n", debugstr_w(type
));
733 static BOOL
get_guid_from_language(LPCWSTR type
, GUID
*guid
)
737 hres
= CLSIDFromProgID(type
, guid
);
741 /* FIXME: Check CATID_ActiveScriptParse */
746 static BOOL
get_script_guid(nsIDOMHTMLScriptElement
*nsscript
, GUID
*guid
)
748 nsAString attr_str
, val_str
;
752 static const PRUnichar languageW
[] = {'l','a','n','g','u','a','g','e',0};
754 nsAString_Init(&val_str
, NULL
);
756 nsres
= nsIDOMHTMLScriptElement_GetType(nsscript
, &val_str
);
757 if(NS_SUCCEEDED(nsres
)) {
758 const PRUnichar
*type
;
760 nsAString_GetData(&val_str
, &type
);
762 ret
= get_guid_from_type(type
, guid
);
763 nsAString_Finish(&val_str
);
767 ERR("GetType failed: %08x\n", nsres
);
770 nsAString_InitDepend(&attr_str
, languageW
);
771 nsres
= nsIDOMHTMLScriptElement_GetAttribute(nsscript
, &attr_str
, &val_str
);
772 nsAString_Finish(&attr_str
);
773 if(NS_SUCCEEDED(nsres
)) {
774 const PRUnichar
*language
;
776 nsAString_GetData(&val_str
, &language
);
779 ret
= get_guid_from_language(language
, guid
);
781 *guid
= CLSID_JScript
;
785 ERR("GetAttribute(language) failed: %08x\n", nsres
);
788 nsAString_Finish(&val_str
);
793 static ScriptHost
*get_script_host(HTMLWindow
*window
, const GUID
*guid
)
797 if(IsEqualGUID(&CLSID_JScript
, guid
) && window
->scriptmode
!= SCRIPTMODE_ACTIVESCRIPT
) {
798 TRACE("Ignoring JScript\n");
802 LIST_FOR_EACH_ENTRY(iter
, &window
->script_hosts
, ScriptHost
, entry
) {
803 if(IsEqualGUID(guid
, &iter
->guid
))
807 return create_script_host(window
, guid
);
810 void doc_insert_script(HTMLWindow
*window
, nsIDOMHTMLScriptElement
*nsscript
)
812 ScriptHost
*script_host
;
815 if(!get_script_guid(nsscript
, &guid
)) {
816 WARN("Could not find script GUID\n");
820 script_host
= get_script_host(window
, &guid
);
824 if(script_host
->parse
)
825 parse_script_elem(script_host
, nsscript
);
828 IDispatch
*script_parse_event(HTMLWindow
*window
, LPCWSTR text
)
830 ScriptHost
*script_host
;
831 GUID guid
= CLSID_JScript
;
836 static const WCHAR delimiterW
[] = {'\"',0};
838 for(ptr
= text
; isalnumW(*ptr
); ptr
++);
843 language
= heap_alloc((ptr
-text
+1)*sizeof(WCHAR
));
844 memcpy(language
, text
, (ptr
-text
)*sizeof(WCHAR
));
845 language
[ptr
-text
] = 0;
847 b
= get_guid_from_language(language
, &guid
);
852 WARN("Could not find language\n");
861 script_host
= get_script_host(window
, &guid
);
862 if(!script_host
|| !script_host
->parse_proc
)
865 hres
= IActiveScriptParseProcedure64_ParseProcedureText(script_host
->parse_proc
, ptr
, NULL
, emptyW
,
866 NULL
, NULL
, delimiterW
, 0 /* FIXME */, 0,
867 SCRIPTPROC_HOSTMANAGESSOURCE
|SCRIPTPROC_IMPLICIT_THIS
|SCRIPTPROC_IMPLICIT_PARENTS
, &disp
);
869 WARN("ParseProcedureText failed: %08x\n", hres
);
873 TRACE("ret %p\n", disp
);
877 IDispatch
*get_script_disp(ScriptHost
*script_host
)
882 if(!script_host
->script
)
885 hres
= IActiveScript_GetScriptDispatch(script_host
->script
, windowW
, &disp
);
892 BOOL
find_global_prop(HTMLWindow
*window
, BSTR name
, DWORD flags
, ScriptHost
**ret_host
, DISPID
*ret_id
)
899 LIST_FOR_EACH_ENTRY(iter
, &window
->script_hosts
, ScriptHost
, entry
) {
900 disp
= get_script_disp(iter
);
904 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
905 if(SUCCEEDED(hres
)) {
906 hres
= IDispatchEx_GetDispID(dispex
, name
, flags
& (~fdexNameEnsure
), ret_id
);
907 IDispatchEx_Release(dispex
);
909 FIXME("No IDispatchEx\n");
913 IDispatch_Release(disp
);
914 if(SUCCEEDED(hres
)) {
923 static BOOL
is_jscript_available(void)
925 static BOOL available
, checked
;
929 HRESULT hres
= CoGetClassObject(&CLSID_JScript
, CLSCTX_INPROC_SERVER
, NULL
, &IID_IUnknown
, (void**)&unk
);
931 if(SUCCEEDED(hres
)) {
933 IUnknown_Release(unk
);
943 void set_script_mode(HTMLWindow
*window
, SCRIPTMODE mode
)
945 nsIWebBrowserSetup
*setup
;
948 if(mode
== SCRIPTMODE_ACTIVESCRIPT
&& !is_jscript_available()) {
949 TRACE("jscript.dll not available\n");
950 window
->scriptmode
= SCRIPTMODE_GECKO
;
954 window
->scriptmode
= mode
;
956 if(!window
->doc_obj
->nscontainer
|| !window
->doc_obj
->nscontainer
->webbrowser
)
959 nsres
= nsIWebBrowser_QueryInterface(window
->doc_obj
->nscontainer
->webbrowser
,
960 &IID_nsIWebBrowserSetup
, (void**)&setup
);
961 if(NS_SUCCEEDED(nsres
)) {
962 nsres
= nsIWebBrowserSetup_SetProperty(setup
, SETUP_ALLOW_JAVASCRIPT
,
963 window
->scriptmode
== SCRIPTMODE_GECKO
);
964 nsIWebBrowserSetup_Release(setup
);
968 ERR("JavaScript setup failed: %08x\n", nsres
);
971 void release_script_hosts(HTMLWindow
*window
)
975 while(!list_empty(&window
->script_hosts
)) {
976 iter
= LIST_ENTRY(list_head(&window
->script_hosts
), ScriptHost
, entry
);
978 release_script_engine(iter
);
979 list_remove(&iter
->entry
);
981 IActiveScript_Release(ACTSCPSITE(iter
));