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}};
53 IActiveScriptSite IActiveScriptSite_iface
;
54 IActiveScriptSiteInterruptPoll IActiveScriptSiteInterruptPoll_iface
;
55 IActiveScriptSiteWindow IActiveScriptSiteWindow_iface
;
56 IActiveScriptSiteDebug IActiveScriptSiteDebug_iface
;
57 IServiceProvider IServiceProvider_iface
;
61 IActiveScript
*script
;
62 IActiveScriptParse
*parse
;
63 IActiveScriptParseProcedure
*parse_proc
;
65 SCRIPTSTATE script_state
;
73 static void set_script_prop(ScriptHost
*script_host
, DWORD property
, VARIANT
*val
)
75 IActiveScriptProperty
*script_prop
;
78 hres
= IActiveScript_QueryInterface(script_host
->script
, &IID_IActiveScriptProperty
,
79 (void**)&script_prop
);
81 WARN("Could not get IActiveScriptProperty iface: %08x\n", hres
);
85 hres
= IActiveScriptProperty_SetProperty(script_prop
, property
, NULL
, val
);
86 IActiveScriptProperty_Release(script_prop
);
88 WARN("SetProperty(%x) failed: %08x\n", property
, hres
);
91 static BOOL
init_script_engine(ScriptHost
*script_host
)
93 IObjectSafety
*safety
;
95 DWORD supported_opts
=0, enabled_opts
=0;
99 hres
= IActiveScript_QueryInterface(script_host
->script
, &IID_IActiveScriptParse
, (void**)&script_host
->parse
);
101 WARN("Could not get IActiveScriptHost: %08x\n", hres
);
105 hres
= IActiveScript_QueryInterface(script_host
->script
, &IID_IObjectSafety
, (void**)&safety
);
107 FIXME("Could not get IObjectSafety: %08x\n", hres
);
111 hres
= IObjectSafety_GetInterfaceSafetyOptions(safety
, &IID_IActiveScriptParse
, &supported_opts
, &enabled_opts
);
113 FIXME("GetInterfaceSafetyOptions failed: %08x\n", hres
);
114 }else if(!(supported_opts
& INTERFACE_USES_DISPEX
)) {
115 FIXME("INTERFACE_USES_DISPEX is not supported\n");
117 hres
= IObjectSafety_SetInterfaceSafetyOptions(safety
, &IID_IActiveScriptParse
,
118 INTERFACESAFE_FOR_UNTRUSTED_DATA
|INTERFACE_USES_DISPEX
|INTERFACE_USES_SECURITY_MANAGER
,
119 INTERFACESAFE_FOR_UNTRUSTED_DATA
|INTERFACE_USES_DISPEX
|INTERFACE_USES_SECURITY_MANAGER
);
121 FIXME("SetInterfaceSafetyOptions failed: %08x\n", hres
);
124 IObjectSafety_Release(safety
);
130 set_script_prop(script_host
, SCRIPTPROP_INVOKEVERSIONING
, &var
);
132 V_VT(&var
) = VT_BOOL
;
133 V_BOOL(&var
) = VARIANT_TRUE
;
134 set_script_prop(script_host
, SCRIPTPROP_HACK_TRIDENTEVENTSINK
, &var
);
136 hres
= IActiveScriptParse64_InitNew(script_host
->parse
);
138 WARN("InitNew failed: %08x\n", hres
);
142 hres
= IActiveScript_SetScriptSite(script_host
->script
, &script_host
->IActiveScriptSite_iface
);
144 WARN("SetScriptSite failed: %08x\n", hres
);
145 IActiveScript_Close(script_host
->script
);
149 hres
= IActiveScript_GetScriptState(script_host
->script
, &state
);
151 WARN("GetScriptState failed: %08x\n", hres
);
152 else if(state
!= SCRIPTSTATE_INITIALIZED
)
153 FIXME("state = %x\n", state
);
155 hres
= IActiveScript_SetScriptState(script_host
->script
, SCRIPTSTATE_STARTED
);
157 WARN("Starting script failed: %08x\n", hres
);
161 hres
= IActiveScript_AddNamedItem(script_host
->script
, windowW
,
162 SCRIPTITEM_ISVISIBLE
|SCRIPTITEM_ISSOURCE
|SCRIPTITEM_GLOBALMEMBERS
);
163 if(SUCCEEDED(hres
)) {
164 V_VT(&var
) = VT_BOOL
;
165 V_BOOL(&var
) = VARIANT_TRUE
;
166 set_script_prop(script_host
, SCRIPTPROP_ABBREVIATE_GLOBALNAME_RESOLUTION
, &var
);
168 WARN("AddNamedItem failed: %08x\n", hres
);
171 hres
= IActiveScript_QueryInterface(script_host
->script
, &IID_IActiveScriptParseProcedure2
,
172 (void**)&script_host
->parse_proc
);
174 /* FIXME: QI for IActiveScriptParseProcedure */
175 WARN("Could not get IActiveScriptParseProcedure iface: %08x\n", hres
);
181 static void release_script_engine(ScriptHost
*This
)
186 switch(This
->script_state
) {
187 case SCRIPTSTATE_CONNECTED
:
188 IActiveScript_SetScriptState(This
->script
, SCRIPTSTATE_DISCONNECTED
);
190 case SCRIPTSTATE_STARTED
:
191 case SCRIPTSTATE_DISCONNECTED
:
192 case SCRIPTSTATE_INITIALIZED
:
193 IActiveScript_Close(This
->script
);
196 if(This
->parse_proc
) {
197 IUnknown_Release(This
->parse_proc
);
198 This
->parse_proc
= NULL
;
202 IUnknown_Release(This
->parse
);
207 IActiveScript_Release(This
->script
);
209 This
->script_state
= SCRIPTSTATE_UNINITIALIZED
;
212 void connect_scripts(HTMLWindow
*window
)
216 LIST_FOR_EACH_ENTRY(iter
, &window
->script_hosts
, ScriptHost
, entry
) {
217 if(iter
->script_state
== SCRIPTSTATE_STARTED
)
218 IActiveScript_SetScriptState(iter
->script
, SCRIPTSTATE_CONNECTED
);
222 static inline ScriptHost
*impl_from_IActiveScriptSite(IActiveScriptSite
*iface
)
224 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSite_iface
);
227 static HRESULT WINAPI
ActiveScriptSite_QueryInterface(IActiveScriptSite
*iface
, REFIID riid
, void **ppv
)
229 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
233 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
234 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
235 *ppv
= &This
->IActiveScriptSite_iface
;
236 }else if(IsEqualGUID(&IID_IActiveScriptSite
, riid
)) {
237 TRACE("(%p)->(IID_IActiveScriptSite %p)\n", This
, ppv
);
238 *ppv
= &This
->IActiveScriptSite_iface
;
239 }else if(IsEqualGUID(&IID_IActiveScriptSiteInterruptPoll
, riid
)) {
240 TRACE("(%p)->(IID_IActiveScriptSiteInterruprtPoll %p)\n", This
, ppv
);
241 *ppv
= &This
->IActiveScriptSiteInterruptPoll_iface
;
242 }else if(IsEqualGUID(&IID_IActiveScriptSiteWindow
, riid
)) {
243 TRACE("(%p)->(IID_IActiveScriptSiteWindow %p)\n", This
, ppv
);
244 *ppv
= &This
->IActiveScriptSiteWindow_iface
;
245 }else if(IsEqualGUID(&IID_IActiveScriptSiteDebug
, riid
)) {
246 TRACE("(%p)->(IID_IActiveScriptSiteDebug %p)\n", This
, ppv
);
247 *ppv
= &This
->IActiveScriptSiteDebug_iface
;
248 }else if(IsEqualGUID(&IID_IServiceProvider
, riid
)) {
249 TRACE("(%p)->(IID_IServiceProvider %p)\n", This
, ppv
);
250 *ppv
= &This
->IServiceProvider_iface
;
251 }else if(IsEqualGUID(&IID_ICanHandleException
, riid
)) {
252 TRACE("(%p)->(IID_ICanHandleException not supported %p)\n", This
, ppv
);
253 return E_NOINTERFACE
;
255 FIXME("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
256 return E_NOINTERFACE
;
259 IUnknown_AddRef((IUnknown
*)*ppv
);
263 static ULONG WINAPI
ActiveScriptSite_AddRef(IActiveScriptSite
*iface
)
265 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
266 LONG ref
= InterlockedIncrement(&This
->ref
);
268 TRACE("(%p) ref=%d\n", This
, ref
);
273 static ULONG WINAPI
ActiveScriptSite_Release(IActiveScriptSite
*iface
)
275 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
276 LONG ref
= InterlockedDecrement(&This
->ref
);
278 TRACE("(%p) ref=%d\n", This
, ref
);
281 release_script_engine(This
);
283 list_remove(&This
->entry
);
290 static HRESULT WINAPI
ActiveScriptSite_GetLCID(IActiveScriptSite
*iface
, LCID
*plcid
)
292 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
294 TRACE("(%p)->(%p)\n", This
, plcid
);
296 *plcid
= GetUserDefaultLCID();
300 static HRESULT WINAPI
ActiveScriptSite_GetItemInfo(IActiveScriptSite
*iface
, LPCOLESTR pstrName
,
301 DWORD dwReturnMask
, IUnknown
**ppiunkItem
, ITypeInfo
**ppti
)
303 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
305 TRACE("(%p)->(%s %x %p %p)\n", This
, debugstr_w(pstrName
), dwReturnMask
, ppiunkItem
, ppti
);
307 if(dwReturnMask
!= SCRIPTINFO_IUNKNOWN
) {
308 FIXME("Unsupported mask %x\n", dwReturnMask
);
314 if(strcmpW(pstrName
, windowW
))
315 return DISP_E_MEMBERNOTFOUND
;
320 /* FIXME: Return proxy object */
321 *ppiunkItem
= (IUnknown
*)&This
->window
->IHTMLWindow2_iface
;
322 IUnknown_AddRef(*ppiunkItem
);
327 static HRESULT WINAPI
ActiveScriptSite_GetDocVersionString(IActiveScriptSite
*iface
, BSTR
*pbstrVersion
)
329 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
330 FIXME("(%p)->(%p)\n", This
, pbstrVersion
);
334 static HRESULT WINAPI
ActiveScriptSite_OnScriptTerminate(IActiveScriptSite
*iface
,
335 const VARIANT
*pvarResult
, const EXCEPINFO
*pexcepinfo
)
337 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
338 FIXME("(%p)->(%p %p)\n", This
, pvarResult
, pexcepinfo
);
342 static HRESULT WINAPI
ActiveScriptSite_OnStateChange(IActiveScriptSite
*iface
, SCRIPTSTATE ssScriptState
)
344 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
346 TRACE("(%p)->(%x)\n", This
, ssScriptState
);
348 This
->script_state
= ssScriptState
;
352 static HRESULT WINAPI
ActiveScriptSite_OnScriptError(IActiveScriptSite
*iface
, IActiveScriptError
*pscripterror
)
354 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
355 FIXME("(%p)->(%p)\n", This
, pscripterror
);
359 static HRESULT WINAPI
ActiveScriptSite_OnEnterScript(IActiveScriptSite
*iface
)
361 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
363 TRACE("(%p)->()\n", This
);
368 static HRESULT WINAPI
ActiveScriptSite_OnLeaveScript(IActiveScriptSite
*iface
)
370 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
372 TRACE("(%p)->()\n", This
);
377 static const IActiveScriptSiteVtbl ActiveScriptSiteVtbl
= {
378 ActiveScriptSite_QueryInterface
,
379 ActiveScriptSite_AddRef
,
380 ActiveScriptSite_Release
,
381 ActiveScriptSite_GetLCID
,
382 ActiveScriptSite_GetItemInfo
,
383 ActiveScriptSite_GetDocVersionString
,
384 ActiveScriptSite_OnScriptTerminate
,
385 ActiveScriptSite_OnStateChange
,
386 ActiveScriptSite_OnScriptError
,
387 ActiveScriptSite_OnEnterScript
,
388 ActiveScriptSite_OnLeaveScript
391 static inline ScriptHost
*impl_from_IActiveScriptSiteInterruptPoll(IActiveScriptSiteInterruptPoll
*iface
)
393 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSiteInterruptPoll_iface
);
396 static HRESULT WINAPI
ActiveScriptSiteInterruptPoll_QueryInterface(IActiveScriptSiteInterruptPoll
*iface
,
397 REFIID riid
, void **ppv
)
399 ScriptHost
*This
= impl_from_IActiveScriptSiteInterruptPoll(iface
);
400 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
403 static ULONG WINAPI
ActiveScriptSiteInterruptPoll_AddRef(IActiveScriptSiteInterruptPoll
*iface
)
405 ScriptHost
*This
= impl_from_IActiveScriptSiteInterruptPoll(iface
);
406 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
409 static ULONG WINAPI
ActiveScriptSiteInterruptPoll_Release(IActiveScriptSiteInterruptPoll
*iface
)
411 ScriptHost
*This
= impl_from_IActiveScriptSiteInterruptPoll(iface
);
412 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
415 static HRESULT WINAPI
ActiveScriptSiteInterruptPoll_QueryContinue(IActiveScriptSiteInterruptPoll
*iface
)
417 ScriptHost
*This
= impl_from_IActiveScriptSiteInterruptPoll(iface
);
419 TRACE("(%p)\n", This
);
424 static const IActiveScriptSiteInterruptPollVtbl ActiveScriptSiteInterruptPollVtbl
= {
425 ActiveScriptSiteInterruptPoll_QueryInterface
,
426 ActiveScriptSiteInterruptPoll_AddRef
,
427 ActiveScriptSiteInterruptPoll_Release
,
428 ActiveScriptSiteInterruptPoll_QueryContinue
431 static inline ScriptHost
*impl_from_IActiveScriptSiteWindow(IActiveScriptSiteWindow
*iface
)
433 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSiteWindow_iface
);
436 static HRESULT WINAPI
ActiveScriptSiteWindow_QueryInterface(IActiveScriptSiteWindow
*iface
,
437 REFIID riid
, void **ppv
)
439 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
440 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
443 static ULONG WINAPI
ActiveScriptSiteWindow_AddRef(IActiveScriptSiteWindow
*iface
)
445 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
446 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
449 static ULONG WINAPI
ActiveScriptSiteWindow_Release(IActiveScriptSiteWindow
*iface
)
451 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
452 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
455 static HRESULT WINAPI
ActiveScriptSiteWindow_GetWindow(IActiveScriptSiteWindow
*iface
, HWND
*phwnd
)
457 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
458 FIXME("(%p)->(%p)\n", This
, phwnd
);
462 static HRESULT WINAPI
ActiveScriptSiteWindow_EnableModeless(IActiveScriptSiteWindow
*iface
, BOOL fEnable
)
464 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
465 FIXME("(%p)->(%x)\n", This
, fEnable
);
469 static const IActiveScriptSiteWindowVtbl ActiveScriptSiteWindowVtbl
= {
470 ActiveScriptSiteWindow_QueryInterface
,
471 ActiveScriptSiteWindow_AddRef
,
472 ActiveScriptSiteWindow_Release
,
473 ActiveScriptSiteWindow_GetWindow
,
474 ActiveScriptSiteWindow_EnableModeless
477 static inline ScriptHost
*impl_from_IActiveScriptSiteDebug(IActiveScriptSiteDebug
*iface
)
479 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSiteDebug_iface
);
482 static HRESULT WINAPI
ActiveScriptSiteDebug_QueryInterface(IActiveScriptSiteDebug
*iface
,
483 REFIID riid
, void **ppv
)
485 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
486 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
489 static ULONG WINAPI
ActiveScriptSiteDebug_AddRef(IActiveScriptSiteDebug
*iface
)
491 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
492 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
495 static ULONG WINAPI
ActiveScriptSiteDebug_Release(IActiveScriptSiteDebug
*iface
)
497 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
498 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
501 static HRESULT WINAPI
ActiveScriptSiteDebug_GetDocumentContextFromPosition(IActiveScriptSiteDebug
*iface
,
502 CTXARG_T dwSourceContext
, ULONG uCharacterOffset
, ULONG uNumChars
, IDebugDocumentContext
**ppsc
)
504 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(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
= impl_from_IActiveScriptSiteDebug(iface
);
513 FIXME("(%p)->(%p)\n", This
, ppda
);
517 static HRESULT WINAPI
ActiveScriptSiteDebug_GetRootApplicationNode(IActiveScriptSiteDebug
*iface
,
518 IDebugApplicationNode
**ppdanRoot
)
520 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(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
= impl_from_IActiveScriptSiteDebug(iface
);
529 FIXME("(%p)->(%p %p %p)\n", This
, pErrorDebug
, pfEnterDebugger
, pfCallOnScriptErrorWhenContinuing
);
533 static const IActiveScriptSiteDebugVtbl ActiveScriptSiteDebugVtbl
= {
534 ActiveScriptSiteDebug_QueryInterface
,
535 ActiveScriptSiteDebug_AddRef
,
536 ActiveScriptSiteDebug_Release
,
537 ActiveScriptSiteDebug_GetDocumentContextFromPosition
,
538 ActiveScriptSiteDebug_GetApplication
,
539 ActiveScriptSiteDebug_GetRootApplicationNode
,
540 ActiveScriptSiteDebug_OnScriptErrorDebug
543 static inline ScriptHost
*impl_from_IServiceProvider(IServiceProvider
*iface
)
545 return CONTAINING_RECORD(iface
, ScriptHost
, IServiceProvider_iface
);
548 static HRESULT WINAPI
ASServiceProvider_QueryInterface(IServiceProvider
*iface
, REFIID riid
, void **ppv
)
550 ScriptHost
*This
= impl_from_IServiceProvider(iface
);
551 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
554 static ULONG WINAPI
ASServiceProvider_AddRef(IServiceProvider
*iface
)
556 ScriptHost
*This
= impl_from_IServiceProvider(iface
);
557 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
560 static ULONG WINAPI
ASServiceProvider_Release(IServiceProvider
*iface
)
562 ScriptHost
*This
= impl_from_IServiceProvider(iface
);
563 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
566 static HRESULT WINAPI
ASServiceProvider_QueryService(IServiceProvider
*iface
, REFGUID guidService
,
567 REFIID riid
, void **ppv
)
569 ScriptHost
*This
= impl_from_IServiceProvider(iface
);
571 if(IsEqualGUID(&SID_SInternetHostSecurityManager
, guidService
)) {
572 TRACE("(%p)->(SID_SInternetHostSecurityManager)\n", This
);
574 if(!This
->window
|| !This
->window
->doc
)
575 return E_NOINTERFACE
;
577 return IInternetHostSecurityManager_QueryInterface(&This
->window
->doc
->IInternetHostSecurityManager_iface
,
581 FIXME("(%p)->(%s %s %p)\n", This
, debugstr_guid(guidService
), debugstr_guid(riid
), ppv
);
582 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
->IActiveScriptSite_iface
.lpVtbl
= &ActiveScriptSiteVtbl
;
599 ret
->IActiveScriptSiteInterruptPoll_iface
.lpVtbl
= &ActiveScriptSiteInterruptPollVtbl
;
600 ret
->IActiveScriptSiteWindow_iface
.lpVtbl
= &ActiveScriptSiteWindowVtbl
;
601 ret
->IActiveScriptSiteDebug_iface
.lpVtbl
= &ActiveScriptSiteDebugVtbl
;
602 ret
->IServiceProvider_iface
.lpVtbl
= &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 LIST_FOR_EACH_ENTRY(iter
, &window
->script_hosts
, ScriptHost
, entry
) {
798 if(IsEqualGUID(guid
, &iter
->guid
))
802 return create_script_host(window
, guid
);
805 void doc_insert_script(HTMLWindow
*window
, nsIDOMHTMLScriptElement
*nsscript
)
807 ScriptHost
*script_host
;
810 if(!get_script_guid(nsscript
, &guid
)) {
811 WARN("Could not find script GUID\n");
815 if(IsEqualGUID(&CLSID_JScript
, &guid
) && window
->scriptmode
!= SCRIPTMODE_ACTIVESCRIPT
) {
816 TRACE("Ignoring JScript\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 if(IsEqualGUID(&CLSID_JScript
, &guid
) && window
->scriptmode
!= SCRIPTMODE_ACTIVESCRIPT
) {
862 TRACE("Ignoring JScript\n");
866 script_host
= get_script_host(window
, &guid
);
867 if(!script_host
|| !script_host
->parse_proc
)
870 hres
= IActiveScriptParseProcedure64_ParseProcedureText(script_host
->parse_proc
, ptr
, NULL
, emptyW
,
871 NULL
, NULL
, delimiterW
, 0 /* FIXME */, 0,
872 SCRIPTPROC_HOSTMANAGESSOURCE
|SCRIPTPROC_IMPLICIT_THIS
|SCRIPTPROC_IMPLICIT_PARENTS
, &disp
);
874 WARN("ParseProcedureText failed: %08x\n", hres
);
878 TRACE("ret %p\n", disp
);
882 HRESULT
exec_script(HTMLWindow
*window
, const WCHAR
*code
, const WCHAR
*lang
, VARIANT
*ret
)
884 ScriptHost
*script_host
;
889 static const WCHAR delimW
[] = {'"',0};
891 if(!get_guid_from_language(lang
, &guid
)) {
892 WARN("Could not find script GUID\n");
893 return CO_E_CLASSSTRING
;
896 script_host
= get_script_host(window
, &guid
);
898 FIXME("No script host\n");
902 if(!script_host
->parse
) {
903 FIXME("script_host->parse == NULL\n");
907 memset(&ei
, 0, sizeof(ei
));
909 hres
= IActiveScriptParse64_ParseScriptText(script_host
->parse
, code
, NULL
, NULL
, delimW
, 0, 0, SCRIPTTEXT_ISVISIBLE
, ret
, &ei
);
913 WARN("<<< %08x\n", hres
);
918 IDispatch
*get_script_disp(ScriptHost
*script_host
)
923 if(!script_host
->script
)
926 hres
= IActiveScript_GetScriptDispatch(script_host
->script
, windowW
, &disp
);
933 BOOL
find_global_prop(HTMLWindow
*window
, BSTR name
, DWORD flags
, ScriptHost
**ret_host
, DISPID
*ret_id
)
940 LIST_FOR_EACH_ENTRY(iter
, &window
->script_hosts
, ScriptHost
, entry
) {
941 disp
= get_script_disp(iter
);
945 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
946 if(SUCCEEDED(hres
)) {
947 hres
= IDispatchEx_GetDispID(dispex
, name
, flags
& (~fdexNameEnsure
), ret_id
);
948 IDispatchEx_Release(dispex
);
950 FIXME("No IDispatchEx\n");
954 IDispatch_Release(disp
);
955 if(SUCCEEDED(hres
)) {
964 static BOOL
is_jscript_available(void)
966 static BOOL available
, checked
;
970 HRESULT hres
= CoGetClassObject(&CLSID_JScript
, CLSCTX_INPROC_SERVER
, NULL
, &IID_IUnknown
, (void**)&unk
);
972 if(SUCCEEDED(hres
)) {
974 IUnknown_Release(unk
);
984 void set_script_mode(HTMLWindow
*window
, SCRIPTMODE mode
)
986 nsIWebBrowserSetup
*setup
;
989 if(mode
== SCRIPTMODE_ACTIVESCRIPT
&& !is_jscript_available()) {
990 TRACE("jscript.dll not available\n");
991 window
->scriptmode
= SCRIPTMODE_GECKO
;
995 window
->scriptmode
= mode
;
997 if(!window
->doc_obj
->nscontainer
|| !window
->doc_obj
->nscontainer
->webbrowser
)
1000 nsres
= nsIWebBrowser_QueryInterface(window
->doc_obj
->nscontainer
->webbrowser
,
1001 &IID_nsIWebBrowserSetup
, (void**)&setup
);
1002 if(NS_SUCCEEDED(nsres
)) {
1003 nsres
= nsIWebBrowserSetup_SetProperty(setup
, SETUP_ALLOW_JAVASCRIPT
,
1004 window
->scriptmode
== SCRIPTMODE_GECKO
);
1005 nsIWebBrowserSetup_Release(setup
);
1008 if(NS_FAILED(nsres
))
1009 ERR("JavaScript setup failed: %08x\n", nsres
);
1012 void release_script_hosts(HTMLWindow
*window
)
1016 while(!list_empty(&window
->script_hosts
)) {
1017 iter
= LIST_ENTRY(list_head(&window
->script_hosts
), ScriptHost
, entry
);
1019 release_script_engine(iter
);
1020 list_remove(&iter
->entry
);
1021 iter
->window
= NULL
;
1022 IActiveScript_Release(&iter
->IActiveScriptSite_iface
);