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"
36 #include "htmlscript.h"
37 #include "pluginhost.h"
38 #include "htmlevent.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(mshtml
);
45 #define CTXARG_T DWORDLONG
46 #define IActiveScriptSiteDebugVtbl IActiveScriptSiteDebug64Vtbl
48 #define IActiveScriptParse_Release IActiveScriptParse64_Release
49 #define IActiveScriptParse_InitNew IActiveScriptParse64_InitNew
50 #define IActiveScriptParse_ParseScriptText IActiveScriptParse64_ParseScriptText
51 #define IActiveScriptParseProcedure2_Release IActiveScriptParseProcedure2_64_Release
52 #define IActiveScriptParseProcedure2_ParseProcedureText IActiveScriptParseProcedure2_64_ParseProcedureText
56 #define CTXARG_T DWORD
57 #define IActiveScriptSiteDebugVtbl IActiveScriptSiteDebug32Vtbl
59 #define IActiveScriptParse_Release IActiveScriptParse32_Release
60 #define IActiveScriptParse_InitNew IActiveScriptParse32_InitNew
61 #define IActiveScriptParse_ParseScriptText IActiveScriptParse32_ParseScriptText
62 #define IActiveScriptParseProcedure2_Release IActiveScriptParseProcedure2_32_Release
63 #define IActiveScriptParseProcedure2_ParseProcedureText IActiveScriptParseProcedure2_32_ParseProcedureText
67 static const WCHAR documentW
[] = {'d','o','c','u','m','e','n','t',0};
68 static const WCHAR windowW
[] = {'w','i','n','d','o','w',0};
69 static const WCHAR script_endW
[] = {'<','/','S','C','R','I','P','T','>',0};
70 static const WCHAR emptyW
[] = {0};
73 IActiveScriptSite IActiveScriptSite_iface
;
74 IActiveScriptSiteInterruptPoll IActiveScriptSiteInterruptPoll_iface
;
75 IActiveScriptSiteWindow IActiveScriptSiteWindow_iface
;
76 IActiveScriptSiteUIControl IActiveScriptSiteUIControl_iface
;
77 IActiveScriptSiteDebug IActiveScriptSiteDebug_iface
;
78 IServiceProvider IServiceProvider_iface
;
82 IActiveScript
*script
;
83 IActiveScriptParse
*parse
;
84 IActiveScriptParseProcedure2
*parse_proc
;
86 SCRIPTSTATE script_state
;
88 HTMLInnerWindow
*window
;
94 static void set_script_prop(ScriptHost
*script_host
, DWORD property
, VARIANT
*val
)
96 IActiveScriptProperty
*script_prop
;
99 hres
= IActiveScript_QueryInterface(script_host
->script
, &IID_IActiveScriptProperty
,
100 (void**)&script_prop
);
102 WARN("Could not get IActiveScriptProperty iface: %08x\n", hres
);
106 hres
= IActiveScriptProperty_SetProperty(script_prop
, property
, NULL
, val
);
107 IActiveScriptProperty_Release(script_prop
);
109 WARN("SetProperty(%x) failed: %08x\n", property
, hres
);
112 static BOOL
init_script_engine(ScriptHost
*script_host
)
114 IObjectSafety
*safety
;
116 DWORD supported_opts
=0, enabled_opts
=0;
120 hres
= IActiveScript_QueryInterface(script_host
->script
, &IID_IActiveScriptParse
, (void**)&script_host
->parse
);
122 WARN("Could not get IActiveScriptHost: %08x\n", hres
);
126 hres
= IActiveScript_QueryInterface(script_host
->script
, &IID_IObjectSafety
, (void**)&safety
);
128 FIXME("Could not get IObjectSafety: %08x\n", hres
);
132 hres
= IObjectSafety_GetInterfaceSafetyOptions(safety
, &IID_IActiveScriptParse
, &supported_opts
, &enabled_opts
);
134 FIXME("GetInterfaceSafetyOptions failed: %08x\n", hres
);
135 }else if(!(supported_opts
& INTERFACE_USES_DISPEX
)) {
136 FIXME("INTERFACE_USES_DISPEX is not supported\n");
138 hres
= IObjectSafety_SetInterfaceSafetyOptions(safety
, &IID_IActiveScriptParse
,
139 INTERFACESAFE_FOR_UNTRUSTED_DATA
|INTERFACE_USES_DISPEX
|INTERFACE_USES_SECURITY_MANAGER
,
140 INTERFACESAFE_FOR_UNTRUSTED_DATA
|INTERFACE_USES_DISPEX
|INTERFACE_USES_SECURITY_MANAGER
);
142 FIXME("SetInterfaceSafetyOptions failed: %08x\n", hres
);
145 IObjectSafety_Release(safety
);
151 set_script_prop(script_host
, SCRIPTPROP_INVOKEVERSIONING
, &var
);
153 V_VT(&var
) = VT_BOOL
;
154 V_BOOL(&var
) = VARIANT_TRUE
;
155 set_script_prop(script_host
, SCRIPTPROP_HACK_TRIDENTEVENTSINK
, &var
);
157 hres
= IActiveScriptParse_InitNew(script_host
->parse
);
159 WARN("InitNew failed: %08x\n", hres
);
163 hres
= IActiveScript_SetScriptSite(script_host
->script
, &script_host
->IActiveScriptSite_iface
);
165 WARN("SetScriptSite failed: %08x\n", hres
);
166 IActiveScript_Close(script_host
->script
);
170 hres
= IActiveScript_GetScriptState(script_host
->script
, &state
);
172 WARN("GetScriptState failed: %08x\n", hres
);
173 else if(state
!= SCRIPTSTATE_INITIALIZED
)
174 FIXME("state = %x\n", state
);
176 hres
= IActiveScript_SetScriptState(script_host
->script
, SCRIPTSTATE_STARTED
);
178 WARN("Starting script failed: %08x\n", hres
);
182 hres
= IActiveScript_AddNamedItem(script_host
->script
, windowW
,
183 SCRIPTITEM_ISVISIBLE
|SCRIPTITEM_ISSOURCE
|SCRIPTITEM_GLOBALMEMBERS
);
184 if(SUCCEEDED(hres
)) {
185 V_VT(&var
) = VT_BOOL
;
186 V_BOOL(&var
) = VARIANT_TRUE
;
187 set_script_prop(script_host
, SCRIPTPROP_ABBREVIATE_GLOBALNAME_RESOLUTION
, &var
);
189 WARN("AddNamedItem failed: %08x\n", hres
);
192 hres
= IActiveScript_QueryInterface(script_host
->script
, &IID_IActiveScriptParseProcedure2
,
193 (void**)&script_host
->parse_proc
);
195 /* FIXME: QI for IActiveScriptParseProcedure */
196 WARN("Could not get IActiveScriptParseProcedure iface: %08x\n", hres
);
202 static void release_script_engine(ScriptHost
*This
)
207 switch(This
->script_state
) {
208 case SCRIPTSTATE_CONNECTED
:
209 IActiveScript_SetScriptState(This
->script
, SCRIPTSTATE_DISCONNECTED
);
211 case SCRIPTSTATE_STARTED
:
212 case SCRIPTSTATE_DISCONNECTED
:
213 case SCRIPTSTATE_INITIALIZED
:
214 IActiveScript_Close(This
->script
);
217 if(This
->parse_proc
) {
218 IActiveScriptParseProcedure2_Release(This
->parse_proc
);
219 This
->parse_proc
= NULL
;
223 IActiveScriptParse_Release(This
->parse
);
228 IActiveScript_Release(This
->script
);
230 This
->script_state
= SCRIPTSTATE_UNINITIALIZED
;
233 void connect_scripts(HTMLInnerWindow
*window
)
237 LIST_FOR_EACH_ENTRY(iter
, &window
->script_hosts
, ScriptHost
, entry
) {
238 if(iter
->script_state
== SCRIPTSTATE_STARTED
)
239 IActiveScript_SetScriptState(iter
->script
, SCRIPTSTATE_CONNECTED
);
243 static inline ScriptHost
*impl_from_IActiveScriptSite(IActiveScriptSite
*iface
)
245 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSite_iface
);
248 static HRESULT WINAPI
ActiveScriptSite_QueryInterface(IActiveScriptSite
*iface
, REFIID riid
, void **ppv
)
250 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
254 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
255 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
256 *ppv
= &This
->IActiveScriptSite_iface
;
257 }else if(IsEqualGUID(&IID_IActiveScriptSite
, riid
)) {
258 TRACE("(%p)->(IID_IActiveScriptSite %p)\n", This
, ppv
);
259 *ppv
= &This
->IActiveScriptSite_iface
;
260 }else if(IsEqualGUID(&IID_IActiveScriptSiteInterruptPoll
, riid
)) {
261 TRACE("(%p)->(IID_IActiveScriptSiteInterruprtPoll %p)\n", This
, ppv
);
262 *ppv
= &This
->IActiveScriptSiteInterruptPoll_iface
;
263 }else if(IsEqualGUID(&IID_IActiveScriptSiteWindow
, riid
)) {
264 TRACE("(%p)->(IID_IActiveScriptSiteWindow %p)\n", This
, ppv
);
265 *ppv
= &This
->IActiveScriptSiteWindow_iface
;
266 }else if(IsEqualGUID(&IID_IActiveScriptSiteUIControl
, riid
)) {
267 TRACE("(%p)->(IID_IActiveScriptSiteUIControl %p)\n", This
, ppv
);
268 *ppv
= &This
->IActiveScriptSiteUIControl_iface
;
269 }else if(IsEqualGUID(&IID_IActiveScriptSiteDebug
, riid
)) {
270 TRACE("(%p)->(IID_IActiveScriptSiteDebug %p)\n", This
, ppv
);
271 *ppv
= &This
->IActiveScriptSiteDebug_iface
;
272 }else if(IsEqualGUID(&IID_IServiceProvider
, riid
)) {
273 TRACE("(%p)->(IID_IServiceProvider %p)\n", This
, ppv
);
274 *ppv
= &This
->IServiceProvider_iface
;
275 }else if(IsEqualGUID(&IID_ICanHandleException
, riid
)) {
276 TRACE("(%p)->(IID_ICanHandleException not supported %p)\n", This
, ppv
);
277 return E_NOINTERFACE
;
279 FIXME("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
280 return E_NOINTERFACE
;
283 IUnknown_AddRef((IUnknown
*)*ppv
);
287 static ULONG WINAPI
ActiveScriptSite_AddRef(IActiveScriptSite
*iface
)
289 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
290 LONG ref
= InterlockedIncrement(&This
->ref
);
292 TRACE("(%p) ref=%d\n", This
, ref
);
297 static ULONG WINAPI
ActiveScriptSite_Release(IActiveScriptSite
*iface
)
299 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
300 LONG ref
= InterlockedDecrement(&This
->ref
);
302 TRACE("(%p) ref=%d\n", This
, ref
);
305 release_script_engine(This
);
307 list_remove(&This
->entry
);
314 static HRESULT WINAPI
ActiveScriptSite_GetLCID(IActiveScriptSite
*iface
, LCID
*plcid
)
316 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
318 TRACE("(%p)->(%p)\n", This
, plcid
);
320 *plcid
= GetUserDefaultLCID();
324 static HRESULT WINAPI
ActiveScriptSite_GetItemInfo(IActiveScriptSite
*iface
, LPCOLESTR pstrName
,
325 DWORD dwReturnMask
, IUnknown
**ppiunkItem
, ITypeInfo
**ppti
)
327 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
329 TRACE("(%p)->(%s %x %p %p)\n", This
, debugstr_w(pstrName
), dwReturnMask
, ppiunkItem
, ppti
);
331 if(dwReturnMask
!= SCRIPTINFO_IUNKNOWN
) {
332 FIXME("Unsupported mask %x\n", dwReturnMask
);
338 if(strcmpW(pstrName
, windowW
))
339 return DISP_E_MEMBERNOTFOUND
;
344 /* FIXME: Return proxy object */
345 *ppiunkItem
= (IUnknown
*)&This
->window
->base
.IHTMLWindow2_iface
;
346 IUnknown_AddRef(*ppiunkItem
);
351 static HRESULT WINAPI
ActiveScriptSite_GetDocVersionString(IActiveScriptSite
*iface
, BSTR
*pbstrVersion
)
353 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
354 FIXME("(%p)->(%p)\n", This
, pbstrVersion
);
358 static HRESULT WINAPI
ActiveScriptSite_OnScriptTerminate(IActiveScriptSite
*iface
,
359 const VARIANT
*pvarResult
, const EXCEPINFO
*pexcepinfo
)
361 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
362 FIXME("(%p)->(%p %p)\n", This
, pvarResult
, pexcepinfo
);
366 static HRESULT WINAPI
ActiveScriptSite_OnStateChange(IActiveScriptSite
*iface
, SCRIPTSTATE ssScriptState
)
368 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
370 TRACE("(%p)->(%x)\n", This
, ssScriptState
);
372 This
->script_state
= ssScriptState
;
376 static HRESULT WINAPI
ActiveScriptSite_OnScriptError(IActiveScriptSite
*iface
, IActiveScriptError
*pscripterror
)
378 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
379 FIXME("(%p)->(%p)\n", This
, pscripterror
);
383 static HRESULT WINAPI
ActiveScriptSite_OnEnterScript(IActiveScriptSite
*iface
)
385 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
387 TRACE("(%p)->()\n", This
);
392 static HRESULT WINAPI
ActiveScriptSite_OnLeaveScript(IActiveScriptSite
*iface
)
394 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
396 TRACE("(%p)->()\n", This
);
401 static const IActiveScriptSiteVtbl ActiveScriptSiteVtbl
= {
402 ActiveScriptSite_QueryInterface
,
403 ActiveScriptSite_AddRef
,
404 ActiveScriptSite_Release
,
405 ActiveScriptSite_GetLCID
,
406 ActiveScriptSite_GetItemInfo
,
407 ActiveScriptSite_GetDocVersionString
,
408 ActiveScriptSite_OnScriptTerminate
,
409 ActiveScriptSite_OnStateChange
,
410 ActiveScriptSite_OnScriptError
,
411 ActiveScriptSite_OnEnterScript
,
412 ActiveScriptSite_OnLeaveScript
415 static inline ScriptHost
*impl_from_IActiveScriptSiteInterruptPoll(IActiveScriptSiteInterruptPoll
*iface
)
417 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSiteInterruptPoll_iface
);
420 static HRESULT WINAPI
ActiveScriptSiteInterruptPoll_QueryInterface(IActiveScriptSiteInterruptPoll
*iface
,
421 REFIID riid
, void **ppv
)
423 ScriptHost
*This
= impl_from_IActiveScriptSiteInterruptPoll(iface
);
424 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
427 static ULONG WINAPI
ActiveScriptSiteInterruptPoll_AddRef(IActiveScriptSiteInterruptPoll
*iface
)
429 ScriptHost
*This
= impl_from_IActiveScriptSiteInterruptPoll(iface
);
430 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
433 static ULONG WINAPI
ActiveScriptSiteInterruptPoll_Release(IActiveScriptSiteInterruptPoll
*iface
)
435 ScriptHost
*This
= impl_from_IActiveScriptSiteInterruptPoll(iface
);
436 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
439 static HRESULT WINAPI
ActiveScriptSiteInterruptPoll_QueryContinue(IActiveScriptSiteInterruptPoll
*iface
)
441 ScriptHost
*This
= impl_from_IActiveScriptSiteInterruptPoll(iface
);
443 TRACE("(%p)\n", This
);
448 static const IActiveScriptSiteInterruptPollVtbl ActiveScriptSiteInterruptPollVtbl
= {
449 ActiveScriptSiteInterruptPoll_QueryInterface
,
450 ActiveScriptSiteInterruptPoll_AddRef
,
451 ActiveScriptSiteInterruptPoll_Release
,
452 ActiveScriptSiteInterruptPoll_QueryContinue
455 static inline ScriptHost
*impl_from_IActiveScriptSiteWindow(IActiveScriptSiteWindow
*iface
)
457 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSiteWindow_iface
);
460 static HRESULT WINAPI
ActiveScriptSiteWindow_QueryInterface(IActiveScriptSiteWindow
*iface
,
461 REFIID riid
, void **ppv
)
463 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
464 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
467 static ULONG WINAPI
ActiveScriptSiteWindow_AddRef(IActiveScriptSiteWindow
*iface
)
469 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
470 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
473 static ULONG WINAPI
ActiveScriptSiteWindow_Release(IActiveScriptSiteWindow
*iface
)
475 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
476 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
479 static HRESULT WINAPI
ActiveScriptSiteWindow_GetWindow(IActiveScriptSiteWindow
*iface
, HWND
*phwnd
)
481 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
483 TRACE("(%p)->(%p)\n", This
, phwnd
);
485 if(!This
->window
|| !This
->window
->base
.outer_window
|| !This
->window
->base
.outer_window
->doc_obj
)
488 *phwnd
= This
->window
->base
.outer_window
->doc_obj
->hwnd
;
492 static HRESULT WINAPI
ActiveScriptSiteWindow_EnableModeless(IActiveScriptSiteWindow
*iface
, BOOL fEnable
)
494 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
495 FIXME("(%p)->(%x)\n", This
, fEnable
);
499 static const IActiveScriptSiteWindowVtbl ActiveScriptSiteWindowVtbl
= {
500 ActiveScriptSiteWindow_QueryInterface
,
501 ActiveScriptSiteWindow_AddRef
,
502 ActiveScriptSiteWindow_Release
,
503 ActiveScriptSiteWindow_GetWindow
,
504 ActiveScriptSiteWindow_EnableModeless
507 static inline ScriptHost
*impl_from_IActiveScriptSiteUIControl(IActiveScriptSiteUIControl
*iface
)
509 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSiteUIControl_iface
);
512 static HRESULT WINAPI
ActiveScriptSiteUIControl_QueryInterface(IActiveScriptSiteUIControl
*iface
, REFIID riid
, void **ppv
)
514 ScriptHost
*This
= impl_from_IActiveScriptSiteUIControl(iface
);
515 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
518 static ULONG WINAPI
ActiveScriptSiteUIControl_AddRef(IActiveScriptSiteUIControl
*iface
)
520 ScriptHost
*This
= impl_from_IActiveScriptSiteUIControl(iface
);
521 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
524 static ULONG WINAPI
ActiveScriptSiteUIControl_Release(IActiveScriptSiteUIControl
*iface
)
526 ScriptHost
*This
= impl_from_IActiveScriptSiteUIControl(iface
);
527 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
530 static HRESULT WINAPI
ActiveScriptSiteUIControl_GetUIBehavior(IActiveScriptSiteUIControl
*iface
, SCRIPTUICITEM UicItem
,
531 SCRIPTUICHANDLING
*pUicHandling
)
533 ScriptHost
*This
= impl_from_IActiveScriptSiteUIControl(iface
);
535 WARN("(%p)->(%d %p) semi-stub\n", This
, UicItem
, pUicHandling
);
537 *pUicHandling
= SCRIPTUICHANDLING_ALLOW
;
541 static const IActiveScriptSiteUIControlVtbl ActiveScriptSiteUIControlVtbl
= {
542 ActiveScriptSiteUIControl_QueryInterface
,
543 ActiveScriptSiteUIControl_AddRef
,
544 ActiveScriptSiteUIControl_Release
,
545 ActiveScriptSiteUIControl_GetUIBehavior
548 static inline ScriptHost
*impl_from_IActiveScriptSiteDebug(IActiveScriptSiteDebug
*iface
)
550 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSiteDebug_iface
);
553 static HRESULT WINAPI
ActiveScriptSiteDebug_QueryInterface(IActiveScriptSiteDebug
*iface
,
554 REFIID riid
, void **ppv
)
556 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
557 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
560 static ULONG WINAPI
ActiveScriptSiteDebug_AddRef(IActiveScriptSiteDebug
*iface
)
562 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
563 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
566 static ULONG WINAPI
ActiveScriptSiteDebug_Release(IActiveScriptSiteDebug
*iface
)
568 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
569 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
572 static HRESULT WINAPI
ActiveScriptSiteDebug_GetDocumentContextFromPosition(IActiveScriptSiteDebug
*iface
,
573 CTXARG_T dwSourceContext
, ULONG uCharacterOffset
, ULONG uNumChars
, IDebugDocumentContext
**ppsc
)
575 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
576 FIXME("(%p)->(%s %u %u %p)\n", This
, wine_dbgstr_longlong(dwSourceContext
), uCharacterOffset
,
581 static HRESULT WINAPI
ActiveScriptSiteDebug_GetApplication(IActiveScriptSiteDebug
*iface
, IDebugApplication
**ppda
)
583 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
584 FIXME("(%p)->(%p)\n", This
, ppda
);
588 static HRESULT WINAPI
ActiveScriptSiteDebug_GetRootApplicationNode(IActiveScriptSiteDebug
*iface
,
589 IDebugApplicationNode
**ppdanRoot
)
591 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
592 FIXME("(%p)->(%p)\n", This
, ppdanRoot
);
596 static HRESULT WINAPI
ActiveScriptSiteDebug_OnScriptErrorDebug(IActiveScriptSiteDebug
*iface
,
597 IActiveScriptErrorDebug
*pErrorDebug
, BOOL
*pfEnterDebugger
, BOOL
*pfCallOnScriptErrorWhenContinuing
)
599 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
600 FIXME("(%p)->(%p %p %p)\n", This
, pErrorDebug
, pfEnterDebugger
, pfCallOnScriptErrorWhenContinuing
);
604 static const IActiveScriptSiteDebugVtbl ActiveScriptSiteDebugVtbl
= {
605 ActiveScriptSiteDebug_QueryInterface
,
606 ActiveScriptSiteDebug_AddRef
,
607 ActiveScriptSiteDebug_Release
,
608 ActiveScriptSiteDebug_GetDocumentContextFromPosition
,
609 ActiveScriptSiteDebug_GetApplication
,
610 ActiveScriptSiteDebug_GetRootApplicationNode
,
611 ActiveScriptSiteDebug_OnScriptErrorDebug
614 static inline ScriptHost
*impl_from_IServiceProvider(IServiceProvider
*iface
)
616 return CONTAINING_RECORD(iface
, ScriptHost
, IServiceProvider_iface
);
619 static HRESULT WINAPI
ASServiceProvider_QueryInterface(IServiceProvider
*iface
, REFIID riid
, void **ppv
)
621 ScriptHost
*This
= impl_from_IServiceProvider(iface
);
622 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
625 static ULONG WINAPI
ASServiceProvider_AddRef(IServiceProvider
*iface
)
627 ScriptHost
*This
= impl_from_IServiceProvider(iface
);
628 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
631 static ULONG WINAPI
ASServiceProvider_Release(IServiceProvider
*iface
)
633 ScriptHost
*This
= impl_from_IServiceProvider(iface
);
634 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
637 static HRESULT WINAPI
ASServiceProvider_QueryService(IServiceProvider
*iface
, REFGUID guidService
,
638 REFIID riid
, void **ppv
)
640 ScriptHost
*This
= impl_from_IServiceProvider(iface
);
642 if(IsEqualGUID(&SID_SInternetHostSecurityManager
, guidService
)) {
643 TRACE("(%p)->(SID_SInternetHostSecurityManager)\n", This
);
645 if(!This
->window
|| !This
->window
->doc
)
646 return E_NOINTERFACE
;
648 return IInternetHostSecurityManager_QueryInterface(&This
->window
->doc
->IInternetHostSecurityManager_iface
,
652 FIXME("(%p)->(%s %s %p)\n", This
, debugstr_guid(guidService
), debugstr_guid(riid
), ppv
);
653 return E_NOINTERFACE
;
656 static const IServiceProviderVtbl ASServiceProviderVtbl
= {
657 ASServiceProvider_QueryInterface
,
658 ASServiceProvider_AddRef
,
659 ASServiceProvider_Release
,
660 ASServiceProvider_QueryService
663 static ScriptHost
*create_script_host(HTMLInnerWindow
*window
, const GUID
*guid
)
668 ret
= heap_alloc_zero(sizeof(*ret
));
672 ret
->IActiveScriptSite_iface
.lpVtbl
= &ActiveScriptSiteVtbl
;
673 ret
->IActiveScriptSiteInterruptPoll_iface
.lpVtbl
= &ActiveScriptSiteInterruptPollVtbl
;
674 ret
->IActiveScriptSiteWindow_iface
.lpVtbl
= &ActiveScriptSiteWindowVtbl
;
675 ret
->IActiveScriptSiteUIControl_iface
.lpVtbl
= &ActiveScriptSiteUIControlVtbl
;
676 ret
->IActiveScriptSiteDebug_iface
.lpVtbl
= &ActiveScriptSiteDebugVtbl
;
677 ret
->IServiceProvider_iface
.lpVtbl
= &ASServiceProviderVtbl
;
679 ret
->window
= window
;
680 ret
->script_state
= SCRIPTSTATE_UNINITIALIZED
;
683 list_add_tail(&window
->script_hosts
, &ret
->entry
);
685 hres
= CoCreateInstance(&ret
->guid
, NULL
, CLSCTX_INPROC_SERVER
|CLSCTX_INPROC_HANDLER
,
686 &IID_IActiveScript
, (void**)&ret
->script
);
688 WARN("Could not load script engine: %08x\n", hres
);
689 else if(!init_script_engine(ret
))
690 release_script_engine(ret
);
695 static void parse_text(ScriptHost
*script_host
, LPCWSTR text
)
701 TRACE("%s\n", debugstr_w(text
));
704 memset(&excepinfo
, 0, sizeof(excepinfo
));
706 hres
= IActiveScriptParse_ParseScriptText(script_host
->parse
, text
, windowW
, NULL
, script_endW
,
707 0, 0, SCRIPTTEXT_ISVISIBLE
|SCRIPTTEXT_HOSTMANAGESSOURCE
,
712 WARN("<<< %08x\n", hres
);
716 static void parse_extern_script(ScriptHost
*script_host
, LPCWSTR src
)
722 static const WCHAR wine_schemaW
[] = {'w','i','n','e',':'};
724 if(strlenW(src
) > sizeof(wine_schemaW
)/sizeof(WCHAR
) && !memcmp(src
, wine_schemaW
, sizeof(wine_schemaW
)))
725 src
+= sizeof(wine_schemaW
)/sizeof(WCHAR
);
727 hres
= CreateURLMoniker(NULL
, src
, &mon
);
731 hres
= bind_mon_to_wstr(script_host
->window
, mon
, &text
);
732 IMoniker_Release(mon
);
736 parse_text(script_host
, text
);
741 static void parse_inline_script(ScriptHost
*script_host
, HTMLScriptElement
*script_elem
)
743 const PRUnichar
*text
;
747 nsAString_Init(&text_str
, NULL
);
748 nsres
= nsIDOMHTMLScriptElement_GetText(script_elem
->nsscript
, &text_str
);
749 nsAString_GetData(&text_str
, &text
);
751 if(NS_FAILED(nsres
)) {
752 ERR("GetText failed: %08x\n", nsres
);
754 script_elem
->parsed
= TRUE
;
755 parse_text(script_host
, text
);
758 nsAString_Finish(&text_str
);
761 static void parse_script_elem(ScriptHost
*script_host
, HTMLScriptElement
*script_elem
)
763 nsAString src_str
, event_str
;
764 const PRUnichar
*src
;
767 nsAString_Init(&event_str
, NULL
);
768 nsres
= nsIDOMHTMLScriptElement_GetEvent(script_elem
->nsscript
, &event_str
);
769 if(NS_SUCCEEDED(nsres
)) {
770 const PRUnichar
*event
;
772 nsAString_GetData(&event_str
, &event
);
774 TRACE("deferring event %s script evaluation\n", debugstr_w(event
));
775 nsAString_Finish(&event_str
);
779 ERR("GetAttribute(event) failed: %08x\n", nsres
);
781 nsAString_Finish(&event_str
);
783 nsAString_Init(&src_str
, NULL
);
784 nsres
= nsIDOMHTMLScriptElement_GetSrc(script_elem
->nsscript
, &src_str
);
785 nsAString_GetData(&src_str
, &src
);
787 if(NS_FAILED(nsres
)) {
788 ERR("GetSrc failed: %08x\n", nsres
);
790 script_elem
->parsed
= TRUE
;
791 parse_extern_script(script_host
, src
);
793 parse_inline_script(script_host
, script_elem
);
796 nsAString_Finish(&src_str
);
799 static GUID
get_default_script_guid(HTMLInnerWindow
*window
)
801 /* If not specified, we should use very first script host that was created for the page (or JScript if none) */
802 return list_empty(&window
->script_hosts
)
804 : LIST_ENTRY(list_head(&window
->script_hosts
), ScriptHost
, entry
)->guid
;
807 static BOOL
get_guid_from_type(LPCWSTR type
, GUID
*guid
)
809 const WCHAR text_javascriptW
[] =
810 {'t','e','x','t','/','j','a','v','a','s','c','r','i','p','t',0};
811 const WCHAR text_vbscriptW
[] =
812 {'t','e','x','t','/','v','b','s','c','r','i','p','t',0};
814 /* FIXME: Handle more types */
815 if(!strcmpiW(type
, text_javascriptW
)) {
816 *guid
= CLSID_JScript
;
817 }else if(!strcmpiW(type
, text_vbscriptW
)) {
818 *guid
= CLSID_VBScript
;
820 FIXME("Unknown type %s\n", debugstr_w(type
));
827 static BOOL
get_guid_from_language(LPCWSTR type
, GUID
*guid
)
831 hres
= CLSIDFromProgID(type
, guid
);
835 /* FIXME: Check CATID_ActiveScriptParse */
840 static BOOL
get_script_guid(HTMLInnerWindow
*window
, nsIDOMHTMLScriptElement
*nsscript
, GUID
*guid
)
842 nsAString attr_str
, val_str
;
846 static const PRUnichar languageW
[] = {'l','a','n','g','u','a','g','e',0};
848 nsAString_Init(&val_str
, NULL
);
850 nsres
= nsIDOMHTMLScriptElement_GetType(nsscript
, &val_str
);
851 if(NS_SUCCEEDED(nsres
)) {
852 const PRUnichar
*type
;
854 nsAString_GetData(&val_str
, &type
);
856 ret
= get_guid_from_type(type
, guid
);
857 nsAString_Finish(&val_str
);
861 ERR("GetType failed: %08x\n", nsres
);
864 nsAString_InitDepend(&attr_str
, languageW
);
865 nsres
= nsIDOMHTMLScriptElement_GetAttribute(nsscript
, &attr_str
, &val_str
);
866 nsAString_Finish(&attr_str
);
867 if(NS_SUCCEEDED(nsres
)) {
868 const PRUnichar
*language
;
870 nsAString_GetData(&val_str
, &language
);
873 ret
= get_guid_from_language(language
, guid
);
875 *guid
= get_default_script_guid(window
);
879 ERR("GetAttribute(language) failed: %08x\n", nsres
);
882 nsAString_Finish(&val_str
);
887 static ScriptHost
*get_script_host(HTMLInnerWindow
*window
, const GUID
*guid
)
891 LIST_FOR_EACH_ENTRY(iter
, &window
->script_hosts
, ScriptHost
, entry
) {
892 if(IsEqualGUID(guid
, &iter
->guid
))
896 return create_script_host(window
, guid
);
899 static ScriptHost
*get_elem_script_host(HTMLInnerWindow
*window
, HTMLScriptElement
*script_elem
)
903 if(!get_script_guid(window
, script_elem
->nsscript
, &guid
)) {
904 WARN("Could not find script GUID\n");
908 if(IsEqualGUID(&CLSID_JScript
, &guid
)
909 && (!window
->base
.outer_window
|| window
->base
.outer_window
->scriptmode
!= SCRIPTMODE_ACTIVESCRIPT
)) {
910 TRACE("Ignoring JScript\n");
914 return get_script_host(window
, &guid
);
917 void doc_insert_script(HTMLInnerWindow
*window
, HTMLScriptElement
*script_elem
)
919 ScriptHost
*script_host
;
921 if(script_elem
->parsed
)
924 script_host
= get_elem_script_host(window
, script_elem
);
928 if(script_host
->parse
)
929 parse_script_elem(script_host
, script_elem
);
932 IDispatch
*script_parse_event(HTMLInnerWindow
*window
, LPCWSTR text
)
934 ScriptHost
*script_host
;
940 static const WCHAR delimiterW
[] = {'\"',0};
942 TRACE("%s\n", debugstr_w(text
));
944 for(ptr
= text
; isalnumW(*ptr
); ptr
++);
949 language
= heap_alloc((ptr
-text
+1)*sizeof(WCHAR
));
953 memcpy(language
, text
, (ptr
-text
)*sizeof(WCHAR
));
954 language
[ptr
-text
] = 0;
956 b
= get_guid_from_language(language
, &guid
);
961 WARN("Could not find language\n");
968 guid
= get_default_script_guid(window
);
971 if(IsEqualGUID(&CLSID_JScript
, &guid
)
972 && (!window
->base
.outer_window
|| window
->base
.outer_window
->scriptmode
!= SCRIPTMODE_ACTIVESCRIPT
)) {
973 TRACE("Ignoring JScript\n");
977 script_host
= get_script_host(window
, &guid
);
978 if(!script_host
|| !script_host
->parse_proc
)
981 hres
= IActiveScriptParseProcedure2_ParseProcedureText(script_host
->parse_proc
, ptr
, NULL
, emptyW
,
982 NULL
, NULL
, delimiterW
, 0 /* FIXME */, 0,
983 SCRIPTPROC_HOSTMANAGESSOURCE
|SCRIPTPROC_IMPLICIT_THIS
|SCRIPTPROC_IMPLICIT_PARENTS
, &disp
);
985 WARN("ParseProcedureText failed: %08x\n", hres
);
989 TRACE("ret %p\n", disp
);
993 HRESULT
exec_script(HTMLInnerWindow
*window
, const WCHAR
*code
, const WCHAR
*lang
, VARIANT
*ret
)
995 ScriptHost
*script_host
;
1000 static const WCHAR delimW
[] = {'"',0};
1002 if(!get_guid_from_language(lang
, &guid
)) {
1003 WARN("Could not find script GUID\n");
1004 return CO_E_CLASSSTRING
;
1007 script_host
= get_script_host(window
, &guid
);
1009 FIXME("No script host\n");
1013 if(!script_host
->parse
) {
1014 FIXME("script_host->parse == NULL\n");
1018 memset(&ei
, 0, sizeof(ei
));
1020 hres
= IActiveScriptParse_ParseScriptText(script_host
->parse
, code
, NULL
, NULL
, delimW
, 0, 0, SCRIPTTEXT_ISVISIBLE
, ret
, &ei
);
1024 WARN("<<< %08x\n", hres
);
1029 IDispatch
*get_script_disp(ScriptHost
*script_host
)
1034 if(!script_host
->script
)
1037 hres
= IActiveScript_GetScriptDispatch(script_host
->script
, windowW
, &disp
);
1044 static HTMLElement
*find_event_target(HTMLDocumentNode
*doc
, HTMLScriptElement
*script_elem
)
1046 const PRUnichar
*target_id
;
1047 nsAString target_id_str
;
1052 nsAString_Init(&target_id_str
, NULL
);
1053 nsres
= nsIDOMHTMLScriptElement_GetHtmlFor(script_elem
->nsscript
, &target_id_str
);
1054 if(NS_FAILED(nsres
)) {
1055 ERR("GetScriptFor failed: %08x\n", nsres
);
1056 nsAString_Finish(&target_id_str
);
1060 nsAString_GetData(&target_id_str
, &target_id
);
1061 if(!*target_id
|| !strcmpW(target_id
, documentW
) || !strcmpW(target_id
, windowW
)) {
1062 FIXME("for %s not supported\n", debugstr_w(target_id
));
1065 hres
= get_doc_elem_by_id(doc
, target_id
, &elem
);
1069 nsAString_Finish(&target_id_str
);
1074 static BOOL
parse_event_str(WCHAR
*event
, const WCHAR
**args
)
1078 TRACE("%s\n", debugstr_w(event
));
1080 for(ptr
= event
; isalnumW(*ptr
); ptr
++);
1091 while(isalnumW(*ptr
) || isspaceW(*ptr
) || *ptr
== ',')
1101 static IDispatch
*parse_event_elem(HTMLDocumentNode
*doc
, HTMLScriptElement
*script_elem
, WCHAR
**ret_event
)
1103 ScriptHost
*script_host
;
1104 WCHAR
*event
= NULL
;
1111 if(script_elem
->parsed
)
1114 script_host
= get_elem_script_host(doc
->window
, script_elem
);
1115 if(!script_host
|| !script_host
->parse_proc
)
1118 nsAString_Init(&nsstr
, NULL
);
1119 nsres
= nsIDOMHTMLScriptElement_GetEvent(script_elem
->nsscript
, &nsstr
);
1120 if(NS_SUCCEEDED(nsres
)) {
1121 const PRUnichar
*event_val
;
1123 nsAString_GetData(&nsstr
, &event_val
);
1124 event
= heap_strdupW(event_val
);
1126 nsAString_Finish(&nsstr
);
1130 if(!parse_event_str(event
, &args
)) {
1131 WARN("parsing %s failed\n", debugstr_w(event
));
1136 nsAString_Init(&nsstr
, NULL
);
1137 nsres
= nsIDOMHTMLScriptElement_GetText(script_elem
->nsscript
, &nsstr
);
1138 if(NS_SUCCEEDED(nsres
)) {
1139 const PRUnichar
*text
;
1141 nsAString_GetData(&nsstr
, &text
);
1142 hres
= IActiveScriptParseProcedure2_ParseProcedureText(script_host
->parse_proc
, text
, args
,
1143 emptyW
, NULL
, NULL
, script_endW
, 0, 0,
1144 SCRIPTPROC_HOSTMANAGESSOURCE
|SCRIPTPROC_IMPLICIT_THIS
|SCRIPTPROC_IMPLICIT_PARENTS
, &disp
);
1148 ERR("GetText failed: %08x\n", nsres
);
1151 nsAString_Finish(&nsstr
);
1161 void bind_event_scripts(HTMLDocumentNode
*doc
)
1163 HTMLPluginContainer
*plugin_container
;
1164 nsIDOMHTMLScriptElement
*nsscript
;
1165 HTMLScriptElement
*script_elem
;
1166 HTMLElement
*event_target
;
1167 nsIDOMNodeList
*node_list
;
1168 nsIDOMNode
*script_node
;
1169 nsAString selector_str
;
1170 IDispatch
*event_disp
;
1176 static const PRUnichar selectorW
[] = {'s','c','r','i','p','t','[','e','v','e','n','t',']',0};
1183 nsAString_InitDepend(&selector_str
, selectorW
);
1184 nsres
= nsIDOMNodeSelector_QuerySelectorAll(doc
->nsnode_selector
, &selector_str
, &node_list
);
1185 nsAString_Finish(&selector_str
);
1186 if(NS_FAILED(nsres
)) {
1187 ERR("QuerySelectorAll failed: %08x\n", nsres
);
1194 nsres
= nsIDOMNodeList_GetLength(node_list
, &length
);
1195 assert(nsres
== NS_OK
);
1197 for(i
=0; i
< length
; i
++) {
1198 nsres
= nsIDOMNodeList_Item(node_list
, i
, &script_node
);
1199 if(NS_FAILED(nsres
) || !script_node
) {
1200 ERR("Item(%d) failed: %08x\n", i
, nsres
);
1204 nsres
= nsIDOMNode_QueryInterface(script_node
, &IID_nsIDOMHTMLScriptElement
, (void**)&nsscript
);
1205 assert(nsres
== NS_OK
);
1206 nsIDOMNode_Release(script_node
);
1208 hres
= script_elem_from_nsscript(doc
, nsscript
, &script_elem
);
1212 event_disp
= parse_event_elem(doc
, script_elem
, &event
);
1214 event_target
= find_event_target(doc
, script_elem
);
1216 hres
= IHTMLElement_QueryInterface(&event_target
->IHTMLElement_iface
, &IID_HTMLPluginContainer
,
1217 (void**)&plugin_container
);
1220 bind_activex_event(doc
, plugin_container
, event
, event_disp
);
1222 bind_elem_event(doc
, event_target
, event
, event_disp
);
1224 IHTMLElement_Release(&event_target
->IHTMLElement_iface
);
1228 IDispatch_Release(event_disp
);
1231 IHTMLScriptElement_Release(&script_elem
->IHTMLScriptElement_iface
);
1234 nsIDOMNodeList_Release(node_list
);
1237 BOOL
find_global_prop(HTMLInnerWindow
*window
, BSTR name
, DWORD flags
, ScriptHost
**ret_host
, DISPID
*ret_id
)
1239 IDispatchEx
*dispex
;
1244 LIST_FOR_EACH_ENTRY(iter
, &window
->script_hosts
, ScriptHost
, entry
) {
1245 disp
= get_script_disp(iter
);
1249 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
1250 if(SUCCEEDED(hres
)) {
1251 hres
= IDispatchEx_GetDispID(dispex
, name
, flags
& (~fdexNameEnsure
), ret_id
);
1252 IDispatchEx_Release(dispex
);
1254 FIXME("No IDispatchEx\n");
1258 IDispatch_Release(disp
);
1259 if(SUCCEEDED(hres
)) {
1268 static BOOL
is_jscript_available(void)
1270 static BOOL available
, checked
;
1274 HRESULT hres
= CoGetClassObject(&CLSID_JScript
, CLSCTX_INPROC_SERVER
, NULL
, &IID_IUnknown
, (void**)&unk
);
1276 if(SUCCEEDED(hres
)) {
1278 IUnknown_Release(unk
);
1288 void set_script_mode(HTMLOuterWindow
*window
, SCRIPTMODE mode
)
1290 nsIWebBrowserSetup
*setup
;
1293 if(mode
== SCRIPTMODE_ACTIVESCRIPT
&& !is_jscript_available()) {
1294 TRACE("jscript.dll not available\n");
1295 window
->scriptmode
= SCRIPTMODE_GECKO
;
1299 window
->scriptmode
= mode
;
1301 if(!window
->doc_obj
->nscontainer
|| !window
->doc_obj
->nscontainer
->webbrowser
)
1304 nsres
= nsIWebBrowser_QueryInterface(window
->doc_obj
->nscontainer
->webbrowser
,
1305 &IID_nsIWebBrowserSetup
, (void**)&setup
);
1306 if(NS_SUCCEEDED(nsres
)) {
1307 nsres
= nsIWebBrowserSetup_SetProperty(setup
, SETUP_ALLOW_JAVASCRIPT
,
1308 window
->scriptmode
== SCRIPTMODE_GECKO
);
1310 if(NS_SUCCEEDED(nsres
))
1311 nsres
= nsIWebBrowserSetup_SetProperty(setup
, SETUP_DISABLE_NOSCRIPT
, TRUE
);
1313 nsIWebBrowserSetup_Release(setup
);
1316 if(NS_FAILED(nsres
))
1317 ERR("JavaScript setup failed: %08x\n", nsres
);
1320 void release_script_hosts(HTMLInnerWindow
*window
)
1322 script_queue_entry_t
*queue_iter
;
1325 while(!list_empty(&window
->script_queue
)) {
1326 queue_iter
= LIST_ENTRY(list_head(&window
->script_queue
), script_queue_entry_t
, entry
);
1328 list_remove(&queue_iter
->entry
);
1329 IHTMLScriptElement_Release(&queue_iter
->script
->IHTMLScriptElement_iface
);
1330 heap_free(queue_iter
);
1333 while(!list_empty(&window
->script_hosts
)) {
1334 iter
= LIST_ENTRY(list_head(&window
->script_hosts
), ScriptHost
, entry
);
1336 release_script_engine(iter
);
1337 list_remove(&iter
->entry
);
1338 iter
->window
= NULL
;
1339 IActiveScriptSite_Release(&iter
->IActiveScriptSite_iface
);