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
);
38 static const WCHAR windowW
[] = {'w','i','n','d','o','w',0};
40 static const CLSID CLSID_JScript
=
41 {0xf414c260,0x6ac0,0x11cf,{0xb6,0xd1,0x00,0xaa,0x00,0xbb,0xbb,0x58}};
44 const IActiveScriptSiteVtbl
*lpIActiveScriptSiteVtbl
;
45 const IActiveScriptSiteInterruptPollVtbl
*lpIActiveScriptSiteInterruptPollVtbl
;
46 const IActiveScriptSiteWindowVtbl
*lpIActiveScriptSiteWindowVtbl
;
50 IActiveScript
*script
;
51 IActiveScriptParse
*parse
;
53 SCRIPTSTATE script_state
;
61 #define ACTSCPSITE(x) ((IActiveScriptSite*) &(x)->lpIActiveScriptSiteVtbl)
62 #define ACTSCPPOLL(x) ((IActiveScriptSiteInterruptPoll*) &(x)->lpIActiveScriptSiteInterruptPollVtbl)
63 #define ACTSCPWIN(x) ((IActiveScriptSiteWindow*) &(x)->lpIActiveScriptSiteWindowVtbl)
65 static BOOL
init_script_engine(ScriptHost
*script_host
)
67 IActiveScriptProperty
*property
;
68 IObjectSafety
*safety
;
70 DWORD supported_opts
=0, enabled_opts
=0;
73 hres
= IActiveScript_QueryInterface(script_host
->script
, &IID_IActiveScriptParse
, (void**)&script_host
->parse
);
75 WARN("Could not get IActiveScriptHost: %08x\n", hres
);
79 hres
= IActiveScript_QueryInterface(script_host
->script
, &IID_IObjectSafety
, (void**)&safety
);
81 FIXME("Could not get IObjectSafety: %08x\n", hres
);
85 hres
= IObjectSafety_GetInterfaceSafetyOptions(safety
, &IID_IActiveScriptParse
, &supported_opts
, &enabled_opts
);
87 FIXME("GetInterfaceSafetyOptions failed: %08x\n", hres
);
88 }else if(!(supported_opts
& INTERFACE_USES_DISPEX
)) {
89 FIXME("INTERFACE_USES_DISPEX is not supported\n");
91 hres
= IObjectSafety_SetInterfaceSafetyOptions(safety
, &IID_IActiveScriptParse
,
92 INTERFACESAFE_FOR_UNTRUSTED_DATA
|INTERFACE_USES_DISPEX
|INTERFACE_USES_SECURITY_MANAGER
,
93 INTERFACESAFE_FOR_UNTRUSTED_DATA
|INTERFACE_USES_DISPEX
|INTERFACE_USES_SECURITY_MANAGER
);
95 FIXME("SetInterfaceSafetyOptions failed: %08x\n", hres
);
98 IObjectSafety_Release(safety
);
102 hres
= IActiveScript_QueryInterface(script_host
->script
, &IID_IActiveScriptProperty
, (void**)&property
);
103 if(SUCCEEDED(hres
)) {
106 V_VT(&var
) = VT_BOOL
;
107 V_BOOL(&var
) = VARIANT_TRUE
;
108 hres
= IActiveScriptProperty_SetProperty(property
, SCRIPTPROP_HACK_TRIDENTEVENTSINK
, NULL
, &var
);
110 WARN("SetProperty failed: %08x\n", hres
);
112 IActiveScriptProperty_Release(property
);
114 WARN("Could not get IActiveScriptProperty: %08x\n", hres
);
117 hres
= IActiveScriptParse_InitNew(script_host
->parse
);
119 WARN("InitNew failed: %08x\n", hres
);
123 hres
= IActiveScript_SetScriptSite(script_host
->script
, ACTSCPSITE(script_host
));
125 WARN("SetScriptSite failed: %08x\n", hres
);
126 IActiveScript_Close(script_host
->script
);
130 hres
= IActiveScript_GetScriptState(script_host
->script
, &state
);
132 WARN("GetScriptState failed: %08x\n", hres
);
133 else if(state
!= SCRIPTSTATE_INITIALIZED
)
134 FIXME("state = %x\n", state
);
136 hres
= IActiveScript_SetScriptState(script_host
->script
, SCRIPTSTATE_STARTED
);
138 WARN("Starting script failed: %08x\n", hres
);
142 hres
= IActiveScript_AddNamedItem(script_host
->script
, windowW
,
143 SCRIPTITEM_ISVISIBLE
|SCRIPTITEM_ISSOURCE
|SCRIPTITEM_GLOBALMEMBERS
);
145 WARN("AddNamedItem failed: %08x\n", hres
);
147 /* FIXME: QI for IActiveScriptParseProcedure2 and IActiveScriptParseProcedure */
152 static void release_script_engine(ScriptHost
*This
)
157 switch(This
->script_state
) {
158 case SCRIPTSTATE_CONNECTED
:
159 IActiveScript_SetScriptState(This
->script
, SCRIPTSTATE_DISCONNECTED
);
161 case SCRIPTSTATE_STARTED
:
162 case SCRIPTSTATE_DISCONNECTED
:
163 case SCRIPTSTATE_INITIALIZED
:
164 IActiveScript_Close(This
->script
);
168 IActiveScriptParse_Release(This
->parse
);
173 IActiveScript_Release(This
->script
);
175 This
->script_state
= SCRIPTSTATE_UNINITIALIZED
;
178 void connect_scripts(HTMLDocument
*doc
)
182 LIST_FOR_EACH_ENTRY(iter
, &doc
->script_hosts
, ScriptHost
, entry
) {
183 if(iter
->script_state
== SCRIPTSTATE_STARTED
)
184 IActiveScript_SetScriptState(iter
->script
, SCRIPTSTATE_CONNECTED
);
188 #define ACTSCPSITE_THIS(iface) DEFINE_THIS(ScriptHost, IActiveScriptSite, iface)
190 static HRESULT WINAPI
ActiveScriptSite_QueryInterface(IActiveScriptSite
*iface
, REFIID riid
, void **ppv
)
192 ScriptHost
*This
= ACTSCPSITE_THIS(iface
);
196 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
197 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
198 *ppv
= ACTSCPSITE(This
);
199 }else if(IsEqualGUID(&IID_IActiveScriptSite
, riid
)) {
200 TRACE("(%p)->(IID_IActiveScriptSite %p)\n", This
, ppv
);
201 *ppv
= ACTSCPSITE(This
);
202 }else if(IsEqualGUID(&IID_IActiveScriptSiteInterruptPoll
, riid
)) {
203 TRACE("(%p)->(IID_IActiveScriptSiteInterruprtPoll %p)\n", This
, ppv
);
204 *ppv
= ACTSCPPOLL(This
);
205 }else if(IsEqualGUID(&IID_IActiveScriptSiteWindow
, riid
)) {
206 TRACE("(%p)->(IID_IActiveScriptSiteWindow %p)\n", This
, ppv
);
207 *ppv
= ACTSCPWIN(This
);
209 FIXME("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
210 return E_NOINTERFACE
;
213 IUnknown_AddRef((IUnknown
*)*ppv
);
217 static ULONG WINAPI
ActiveScriptSite_AddRef(IActiveScriptSite
*iface
)
219 ScriptHost
*This
= ACTSCPSITE_THIS(iface
);
220 LONG ref
= InterlockedIncrement(&This
->ref
);
222 TRACE("(%p) ref=%d\n", This
, ref
);
227 static ULONG WINAPI
ActiveScriptSite_Release(IActiveScriptSite
*iface
)
229 ScriptHost
*This
= ACTSCPSITE_THIS(iface
);
230 LONG ref
= InterlockedDecrement(&This
->ref
);
232 TRACE("(%p) ref=%d\n", This
, ref
);
235 release_script_engine(This
);
237 list_remove(&This
->entry
);
244 static HRESULT WINAPI
ActiveScriptSite_GetLCID(IActiveScriptSite
*iface
, LCID
*plcid
)
246 ScriptHost
*This
= ACTSCPSITE_THIS(iface
);
248 TRACE("(%p)->(%p)\n", This
, plcid
);
250 *plcid
= GetUserDefaultLCID();
254 static HRESULT WINAPI
ActiveScriptSite_GetItemInfo(IActiveScriptSite
*iface
, LPCOLESTR pstrName
,
255 DWORD dwReturnMask
, IUnknown
**ppiunkItem
, ITypeInfo
**ppti
)
257 ScriptHost
*This
= ACTSCPSITE_THIS(iface
);
259 TRACE("(%p)->(%s %x %p %p)\n", This
, debugstr_w(pstrName
), dwReturnMask
, ppiunkItem
, ppti
);
261 if(dwReturnMask
!= SCRIPTINFO_IUNKNOWN
) {
262 FIXME("Unsupported mask %x\n", dwReturnMask
);
268 if(strcmpW(pstrName
, windowW
))
269 return DISP_E_MEMBERNOTFOUND
;
274 /* FIXME: Return proxy object */
275 *ppiunkItem
= (IUnknown
*)HTMLWINDOW2(This
->doc
->window
);
276 IUnknown_AddRef(*ppiunkItem
);
281 static HRESULT WINAPI
ActiveScriptSite_GetDocVersionString(IActiveScriptSite
*iface
, BSTR
*pbstrVersion
)
283 ScriptHost
*This
= ACTSCPSITE_THIS(iface
);
284 FIXME("(%p)->(%p)\n", This
, pbstrVersion
);
288 static HRESULT WINAPI
ActiveScriptSite_OnScriptTerminate(IActiveScriptSite
*iface
,
289 const VARIANT
*pvarResult
, const EXCEPINFO
*pexcepinfo
)
291 ScriptHost
*This
= ACTSCPSITE_THIS(iface
);
292 FIXME("(%p)->(%p %p)\n", This
, pvarResult
, pexcepinfo
);
296 static HRESULT WINAPI
ActiveScriptSite_OnStateChange(IActiveScriptSite
*iface
, SCRIPTSTATE ssScriptState
)
298 ScriptHost
*This
= ACTSCPSITE_THIS(iface
);
300 TRACE("(%p)->(%x)\n", This
, ssScriptState
);
302 This
->script_state
= ssScriptState
;
306 static HRESULT WINAPI
ActiveScriptSite_OnScriptError(IActiveScriptSite
*iface
, IActiveScriptError
*pscripterror
)
308 ScriptHost
*This
= ACTSCPSITE_THIS(iface
);
309 FIXME("(%p)->(%p)\n", This
, pscripterror
);
313 static HRESULT WINAPI
ActiveScriptSite_OnEnterScript(IActiveScriptSite
*iface
)
315 ScriptHost
*This
= ACTSCPSITE_THIS(iface
);
317 TRACE("(%p)->()\n", This
);
322 static HRESULT WINAPI
ActiveScriptSite_OnLeaveScript(IActiveScriptSite
*iface
)
324 ScriptHost
*This
= ACTSCPSITE_THIS(iface
);
326 TRACE("(%p)->()\n", This
);
331 #undef ACTSCPSITE_THIS
333 static const IActiveScriptSiteVtbl ActiveScriptSiteVtbl
= {
334 ActiveScriptSite_QueryInterface
,
335 ActiveScriptSite_AddRef
,
336 ActiveScriptSite_Release
,
337 ActiveScriptSite_GetLCID
,
338 ActiveScriptSite_GetItemInfo
,
339 ActiveScriptSite_GetDocVersionString
,
340 ActiveScriptSite_OnScriptTerminate
,
341 ActiveScriptSite_OnStateChange
,
342 ActiveScriptSite_OnScriptError
,
343 ActiveScriptSite_OnEnterScript
,
344 ActiveScriptSite_OnLeaveScript
347 #define ACTSCPPOLL_THIS(iface) DEFINE_THIS(ScriptHost, IActiveScriptSiteInterruptPoll, iface)
349 static HRESULT WINAPI
ActiveScriptSiteInterruptPoll_QueryInterface(IActiveScriptSiteInterruptPoll
*iface
,
350 REFIID riid
, void **ppv
)
352 ScriptHost
*This
= ACTSCPPOLL_THIS(iface
);
353 return IActiveScriptSite_QueryInterface(ACTSCPSITE(This
), riid
, ppv
);
356 static ULONG WINAPI
ActiveScriptSiteInterruptPoll_AddRef(IActiveScriptSiteInterruptPoll
*iface
)
358 ScriptHost
*This
= ACTSCPPOLL_THIS(iface
);
359 return IActiveScriptSite_AddRef(ACTSCPSITE(This
));
362 static ULONG WINAPI
ActiveScriptSiteInterruptPoll_Release(IActiveScriptSiteInterruptPoll
*iface
)
364 ScriptHost
*This
= ACTSCPPOLL_THIS(iface
);
365 return IActiveScriptSite_Release(ACTSCPSITE(This
));
368 static HRESULT WINAPI
ActiveScriptSiteInterruptPoll_QueryContinue(IActiveScriptSiteInterruptPoll
*iface
)
370 ScriptHost
*This
= ACTSCPPOLL_THIS(iface
);
371 FIXME("(%p)\n", This
);
375 #undef ACTSCPPOLL_THIS
377 static const IActiveScriptSiteInterruptPollVtbl ActiveScriptSiteInterruptPollVtbl
= {
378 ActiveScriptSiteInterruptPoll_QueryInterface
,
379 ActiveScriptSiteInterruptPoll_AddRef
,
380 ActiveScriptSiteInterruptPoll_Release
,
381 ActiveScriptSiteInterruptPoll_QueryContinue
384 #define ACTSCPWIN_THIS(iface) DEFINE_THIS(ScriptHost, IActiveScriptSiteWindow, iface)
386 static HRESULT WINAPI
ActiveScriptSiteWindow_QueryInterface(IActiveScriptSiteWindow
*iface
,
387 REFIID riid
, void **ppv
)
389 ScriptHost
*This
= ACTSCPWIN_THIS(iface
);
390 return IActiveScriptSite_QueryInterface(ACTSCPSITE(This
), riid
, ppv
);
393 static ULONG WINAPI
ActiveScriptSiteWindow_AddRef(IActiveScriptSiteWindow
*iface
)
395 ScriptHost
*This
= ACTSCPWIN_THIS(iface
);
396 return IActiveScriptSite_AddRef(ACTSCPSITE(This
));
399 static ULONG WINAPI
ActiveScriptSiteWindow_Release(IActiveScriptSiteWindow
*iface
)
401 ScriptHost
*This
= ACTSCPWIN_THIS(iface
);
402 return IActiveScriptSite_Release(ACTSCPSITE(This
));
405 static HRESULT WINAPI
ActiveScriptSiteWindow_GetWindow(IActiveScriptSiteWindow
*iface
, HWND
*phwnd
)
407 ScriptHost
*This
= ACTSCPWIN_THIS(iface
);
408 FIXME("(%p)->(%p)\n", This
, phwnd
);
412 static HRESULT WINAPI
ActiveScriptSiteWindow_EnableModeless(IActiveScriptSiteWindow
*iface
, BOOL fEnable
)
414 ScriptHost
*This
= ACTSCPWIN_THIS(iface
);
415 FIXME("(%p)->(%x)\n", This
, fEnable
);
419 #undef ACTSCPWIN_THIS
421 static const IActiveScriptSiteWindowVtbl ActiveScriptSiteWindowVtbl
= {
422 ActiveScriptSiteWindow_QueryInterface
,
423 ActiveScriptSiteWindow_AddRef
,
424 ActiveScriptSiteWindow_Release
,
425 ActiveScriptSiteWindow_GetWindow
,
426 ActiveScriptSiteWindow_EnableModeless
429 static ScriptHost
*create_script_host(HTMLDocument
*doc
, GUID
*guid
)
434 ret
= heap_alloc_zero(sizeof(*ret
));
435 ret
->lpIActiveScriptSiteVtbl
= &ActiveScriptSiteVtbl
;
436 ret
->lpIActiveScriptSiteInterruptPollVtbl
= &ActiveScriptSiteInterruptPollVtbl
;
437 ret
->lpIActiveScriptSiteWindowVtbl
= &ActiveScriptSiteWindowVtbl
;
440 ret
->script_state
= SCRIPTSTATE_UNINITIALIZED
;
443 list_add_tail(&doc
->script_hosts
, &ret
->entry
);
445 hres
= CoCreateInstance(&ret
->guid
, NULL
, CLSCTX_INPROC_SERVER
|CLSCTX_INPROC_HANDLER
,
446 &IID_IActiveScript
, (void**)&ret
->script
);
448 WARN("Could not load script engine: %08x\n", hres
);
449 else if(!init_script_engine(ret
))
450 release_script_engine(ret
);
455 static void parse_text(ScriptHost
*script_host
, LPCWSTR text
)
461 static const WCHAR script_endW
[] = {'<','/','S','C','R','I','P','T','>',0};
463 TRACE("%s\n", debugstr_w(text
));
466 memset(&excepinfo
, 0, sizeof(excepinfo
));
467 hres
= IActiveScriptParse_ParseScriptText(script_host
->parse
, text
, windowW
, NULL
, script_endW
,
468 0, 0, SCRIPTTEXT_ISVISIBLE
|SCRIPTTEXT_HOSTMANAGESSOURCE
,
471 WARN("ParseScriptText failed: %08x\n", hres
);
475 static void parse_extern_script(ScriptHost
*script_host
, LPCWSTR src
)
482 static const WCHAR wine_schemaW
[] = {'w','i','n','e',':'};
484 if(strlenW(src
) > sizeof(wine_schemaW
)/sizeof(WCHAR
) && !memcmp(src
, wine_schemaW
, sizeof(wine_schemaW
)))
485 src
+= sizeof(wine_schemaW
)/sizeof(WCHAR
);
487 hres
= CreateURLMoniker(NULL
, src
, &mon
);
491 hres
= bind_mon_to_buffer(script_host
->doc
, mon
, (void**)&buf
);
492 IMoniker_Release(mon
);
496 text
= heap_strdupAtoW(buf
);
499 parse_text(script_host
, text
);
504 static void parse_inline_script(ScriptHost
*script_host
, nsIDOMHTMLScriptElement
*nsscript
)
506 const PRUnichar
*text
;
510 nsAString_Init(&text_str
, NULL
);
512 nsres
= nsIDOMHTMLScriptElement_GetText(nsscript
, &text_str
);
514 if(NS_SUCCEEDED(nsres
)) {
515 nsAString_GetData(&text_str
, &text
);
516 parse_text(script_host
, text
);
518 ERR("GetText failed: %08x\n", nsres
);
521 nsAString_Finish(&text_str
);
524 static void parse_script_elem(ScriptHost
*script_host
, nsIDOMHTMLScriptElement
*nsscript
)
526 const PRUnichar
*src
;
530 nsAString_Init(&src_str
, NULL
);
532 nsres
= nsIDOMHTMLScriptElement_GetSrc(nsscript
, &src_str
);
533 nsAString_GetData(&src_str
, &src
);
536 ERR("GetSrc failed: %08x\n", nsres
);
538 parse_extern_script(script_host
, src
);
540 parse_inline_script(script_host
, nsscript
);
542 nsAString_Finish(&src_str
);
545 static BOOL
get_guid_from_type(LPCWSTR type
, GUID
*guid
)
547 const WCHAR text_javascriptW
[] =
548 {'t','e','x','t','/','j','a','v','a','s','c','r','i','p','t',0};
550 /* FIXME: Handle more types */
551 if(!strcmpW(type
, text_javascriptW
)) {
552 *guid
= CLSID_JScript
;
554 FIXME("Unknown type %s\n", debugstr_w(type
));
561 static BOOL
get_guid_from_language(LPCWSTR type
, GUID
*guid
)
565 hres
= CLSIDFromProgID(type
, guid
);
569 /* FIXME: Check CATID_ActiveScriptParse */
574 static BOOL
get_script_guid(nsIDOMHTMLScriptElement
*nsscript
, GUID
*guid
)
576 nsAString attr_str
, val_str
;
580 static const PRUnichar languageW
[] = {'l','a','n','g','u','a','g','e',0};
582 nsAString_Init(&val_str
, NULL
);
584 nsres
= nsIDOMHTMLScriptElement_GetType(nsscript
, &val_str
);
585 if(NS_SUCCEEDED(nsres
)) {
586 const PRUnichar
*type
;
588 nsAString_GetData(&val_str
, &type
);
590 ret
= get_guid_from_type(type
, guid
);
591 nsAString_Finish(&val_str
);
595 ERR("GetType failed: %08x\n", nsres
);
598 nsAString_Init(&attr_str
, languageW
);
600 nsres
= nsIDOMHTMLScriptElement_GetAttribute(nsscript
, &attr_str
, &val_str
);
601 if(NS_SUCCEEDED(nsres
)) {
602 const PRUnichar
*language
;
604 nsAString_GetData(&val_str
, &language
);
607 ret
= get_guid_from_language(language
, guid
);
609 *guid
= CLSID_JScript
;
613 ERR("GetAttribute(language) failed: %08x\n", nsres
);
616 nsAString_Finish(&attr_str
);
617 nsAString_Finish(&val_str
);
622 static ScriptHost
*get_script_host(HTMLDocument
*doc
, nsIDOMHTMLScriptElement
*nsscript
)
627 if(!get_script_guid(nsscript
, &guid
)) {
628 WARN("Could not find script GUID\n");
632 if(IsEqualGUID(&CLSID_JScript
, &guid
)) {
633 FIXME("Ignoring JScript\n");
637 LIST_FOR_EACH_ENTRY(iter
, &doc
->script_hosts
, ScriptHost
, entry
) {
638 if(IsEqualGUID(&guid
, &iter
->guid
))
642 return create_script_host(doc
, &guid
);
645 void doc_insert_script(HTMLDocument
*doc
, nsIDOMHTMLScriptElement
*nsscript
)
647 ScriptHost
*script_host
;
649 script_host
= get_script_host(doc
, nsscript
);
653 if(script_host
->parse
)
654 parse_script_elem(script_host
, nsscript
);
657 void release_script_hosts(HTMLDocument
*doc
)
661 while(!list_empty(&doc
->script_hosts
)) {
662 iter
= LIST_ENTRY(list_head(&doc
->script_hosts
), ScriptHost
, entry
);
664 release_script_engine(iter
);
665 list_remove(&iter
->entry
);
667 IActiveScript_Release(ACTSCPSITE(iter
));