d3dcompiler_43: Use BOOL type where appropriate.
[wine.git] / dlls / mshtml / script.c
blob8a745543bc5a63753b28a324c6876a0096a237e9
1 /*
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
19 #include "config.h"
21 #include <stdarg.h>
22 #include <assert.h>
24 #define COBJMACROS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "ole2.h"
30 #include "activscp.h"
31 #include "activdbg.h"
33 #include "wine/debug.h"
35 #include "mshtml_private.h"
36 #include "htmlscript.h"
37 #include "pluginhost.h"
38 #include "htmlevent.h"
39 #include "binding.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
43 #ifdef _WIN64
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
54 #else
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
65 #endif
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};
72 struct ScriptHost {
73 IActiveScriptSite IActiveScriptSite_iface;
74 IActiveScriptSiteInterruptPoll IActiveScriptSiteInterruptPoll_iface;
75 IActiveScriptSiteWindow IActiveScriptSiteWindow_iface;
76 IActiveScriptSiteUIControl IActiveScriptSiteUIControl_iface;
77 IActiveScriptSiteDebug IActiveScriptSiteDebug_iface;
78 IServiceProvider IServiceProvider_iface;
80 LONG ref;
82 IActiveScript *script;
83 IActiveScriptParse *parse;
84 IActiveScriptParseProcedure2 *parse_proc;
86 SCRIPTSTATE script_state;
88 HTMLInnerWindow *window;
90 GUID guid;
91 struct list entry;
94 static void set_script_prop(ScriptHost *script_host, DWORD property, VARIANT *val)
96 IActiveScriptProperty *script_prop;
97 HRESULT hres;
99 hres = IActiveScript_QueryInterface(script_host->script, &IID_IActiveScriptProperty,
100 (void**)&script_prop);
101 if(FAILED(hres)) {
102 WARN("Could not get IActiveScriptProperty iface: %08x\n", hres);
103 return;
106 hres = IActiveScriptProperty_SetProperty(script_prop, property, NULL, val);
107 IActiveScriptProperty_Release(script_prop);
108 if(FAILED(hres))
109 WARN("SetProperty(%x) failed: %08x\n", property, hres);
112 static BOOL init_script_engine(ScriptHost *script_host)
114 IObjectSafety *safety;
115 SCRIPTSTATE state;
116 DWORD supported_opts=0, enabled_opts=0;
117 VARIANT var;
118 HRESULT hres;
120 hres = IActiveScript_QueryInterface(script_host->script, &IID_IActiveScriptParse, (void**)&script_host->parse);
121 if(FAILED(hres)) {
122 WARN("Could not get IActiveScriptHost: %08x\n", hres);
123 return FALSE;
126 hres = IActiveScript_QueryInterface(script_host->script, &IID_IObjectSafety, (void**)&safety);
127 if(FAILED(hres)) {
128 FIXME("Could not get IObjectSafety: %08x\n", hres);
129 return FALSE;
132 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported_opts, &enabled_opts);
133 if(FAILED(hres)) {
134 FIXME("GetInterfaceSafetyOptions failed: %08x\n", hres);
135 }else if(!(supported_opts & INTERFACE_USES_DISPEX)) {
136 FIXME("INTERFACE_USES_DISPEX is not supported\n");
137 }else {
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);
141 if(FAILED(hres))
142 FIXME("SetInterfaceSafetyOptions failed: %08x\n", hres);
145 IObjectSafety_Release(safety);
146 if(FAILED(hres))
147 return FALSE;
149 V_VT(&var) = VT_I4;
150 V_I4(&var) = 1;
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);
158 if(FAILED(hres)) {
159 WARN("InitNew failed: %08x\n", hres);
160 return FALSE;
163 hres = IActiveScript_SetScriptSite(script_host->script, &script_host->IActiveScriptSite_iface);
164 if(FAILED(hres)) {
165 WARN("SetScriptSite failed: %08x\n", hres);
166 IActiveScript_Close(script_host->script);
167 return FALSE;
170 hres = IActiveScript_GetScriptState(script_host->script, &state);
171 if(FAILED(hres))
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);
177 if(FAILED(hres)) {
178 WARN("Starting script failed: %08x\n", hres);
179 return FALSE;
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);
188 }else {
189 WARN("AddNamedItem failed: %08x\n", hres);
192 hres = IActiveScript_QueryInterface(script_host->script, &IID_IActiveScriptParseProcedure2,
193 (void**)&script_host->parse_proc);
194 if(FAILED(hres)) {
195 /* FIXME: QI for IActiveScriptParseProcedure */
196 WARN("Could not get IActiveScriptParseProcedure iface: %08x\n", hres);
199 return TRUE;
202 static void release_script_engine(ScriptHost *This)
204 if(!This->script)
205 return;
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);
216 default:
217 if(This->parse_proc) {
218 IActiveScriptParseProcedure2_Release(This->parse_proc);
219 This->parse_proc = NULL;
222 if(This->parse) {
223 IActiveScriptParse_Release(This->parse);
224 This->parse = NULL;
228 IActiveScript_Release(This->script);
229 This->script = NULL;
230 This->script_state = SCRIPTSTATE_UNINITIALIZED;
233 void connect_scripts(HTMLInnerWindow *window)
235 ScriptHost *iter;
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);
252 *ppv = NULL;
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;
278 }else {
279 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
280 return E_NOINTERFACE;
283 IUnknown_AddRef((IUnknown*)*ppv);
284 return S_OK;
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);
294 return 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);
304 if(!ref) {
305 release_script_engine(This);
306 if(This->window)
307 list_remove(&This->entry);
308 heap_free(This);
311 return ref;
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();
321 return S_OK;
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);
333 return E_NOTIMPL;
336 *ppiunkItem = NULL;
338 if(strcmpW(pstrName, windowW))
339 return DISP_E_MEMBERNOTFOUND;
341 if(!This->window)
342 return E_FAIL;
344 /* FIXME: Return proxy object */
345 *ppiunkItem = (IUnknown*)&This->window->base.IHTMLWindow2_iface;
346 IUnknown_AddRef(*ppiunkItem);
348 return S_OK;
351 static HRESULT WINAPI ActiveScriptSite_GetDocVersionString(IActiveScriptSite *iface, BSTR *pbstrVersion)
353 ScriptHost *This = impl_from_IActiveScriptSite(iface);
354 FIXME("(%p)->(%p)\n", This, pbstrVersion);
355 return E_NOTIMPL;
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);
363 return E_NOTIMPL;
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;
373 return S_OK;
376 static HRESULT WINAPI ActiveScriptSite_OnScriptError(IActiveScriptSite *iface, IActiveScriptError *pscripterror)
378 ScriptHost *This = impl_from_IActiveScriptSite(iface);
379 FIXME("(%p)->(%p)\n", This, pscripterror);
380 return E_NOTIMPL;
383 static HRESULT WINAPI ActiveScriptSite_OnEnterScript(IActiveScriptSite *iface)
385 ScriptHost *This = impl_from_IActiveScriptSite(iface);
387 TRACE("(%p)->()\n", This);
389 return S_OK;
392 static HRESULT WINAPI ActiveScriptSite_OnLeaveScript(IActiveScriptSite *iface)
394 ScriptHost *This = impl_from_IActiveScriptSite(iface);
396 TRACE("(%p)->()\n", This);
398 return S_OK;
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);
445 return S_OK;
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)
486 return E_UNEXPECTED;
488 *phwnd = This->window->base.outer_window->doc_obj->hwnd;
489 return S_OK;
492 static HRESULT WINAPI ActiveScriptSiteWindow_EnableModeless(IActiveScriptSiteWindow *iface, BOOL fEnable)
494 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
495 FIXME("(%p)->(%x)\n", This, fEnable);
496 return S_OK;
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;
538 return S_OK;
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,
577 uNumChars, ppsc);
578 return E_NOTIMPL;
581 static HRESULT WINAPI ActiveScriptSiteDebug_GetApplication(IActiveScriptSiteDebug *iface, IDebugApplication **ppda)
583 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
584 FIXME("(%p)->(%p)\n", This, ppda);
585 return E_NOTIMPL;
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);
593 return E_NOTIMPL;
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);
601 return E_NOTIMPL;
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,
649 riid, ppv);
652 if(IsEqualGUID(&SID_SContainerDispatch, guidService)) {
653 TRACE("(%p)->(SID_SContainerDispatch)\n", This);
655 if(!This->window || !This->window->doc)
656 return E_NOINTERFACE;
658 return IHTMLDocument2_QueryInterface(&This->window->doc->basedoc.IHTMLDocument2_iface, riid, ppv);
661 FIXME("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
662 return E_NOINTERFACE;
665 static const IServiceProviderVtbl ASServiceProviderVtbl = {
666 ASServiceProvider_QueryInterface,
667 ASServiceProvider_AddRef,
668 ASServiceProvider_Release,
669 ASServiceProvider_QueryService
672 static ScriptHost *create_script_host(HTMLInnerWindow *window, const GUID *guid)
674 ScriptHost *ret;
675 HRESULT hres;
677 ret = heap_alloc_zero(sizeof(*ret));
678 if(!ret)
679 return NULL;
681 ret->IActiveScriptSite_iface.lpVtbl = &ActiveScriptSiteVtbl;
682 ret->IActiveScriptSiteInterruptPoll_iface.lpVtbl = &ActiveScriptSiteInterruptPollVtbl;
683 ret->IActiveScriptSiteWindow_iface.lpVtbl = &ActiveScriptSiteWindowVtbl;
684 ret->IActiveScriptSiteUIControl_iface.lpVtbl = &ActiveScriptSiteUIControlVtbl;
685 ret->IActiveScriptSiteDebug_iface.lpVtbl = &ActiveScriptSiteDebugVtbl;
686 ret->IServiceProvider_iface.lpVtbl = &ASServiceProviderVtbl;
687 ret->ref = 1;
688 ret->window = window;
689 ret->script_state = SCRIPTSTATE_UNINITIALIZED;
691 ret->guid = *guid;
692 list_add_tail(&window->script_hosts, &ret->entry);
694 hres = CoCreateInstance(&ret->guid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
695 &IID_IActiveScript, (void**)&ret->script);
696 if(FAILED(hres))
697 WARN("Could not load script engine: %08x\n", hres);
698 else if(!init_script_engine(ret))
699 release_script_engine(ret);
701 return ret;
704 static void parse_text(ScriptHost *script_host, LPCWSTR text)
706 EXCEPINFO excepinfo;
707 VARIANT var;
708 HRESULT hres;
710 TRACE("%s\n", debugstr_w(text));
712 VariantInit(&var);
713 memset(&excepinfo, 0, sizeof(excepinfo));
714 TRACE(">>>\n");
715 hres = IActiveScriptParse_ParseScriptText(script_host->parse, text, windowW, NULL, script_endW,
716 0, 0, SCRIPTTEXT_ISVISIBLE|SCRIPTTEXT_HOSTMANAGESSOURCE,
717 &var, &excepinfo);
718 if(SUCCEEDED(hres))
719 TRACE("<<<\n");
720 else
721 WARN("<<< %08x\n", hres);
725 static void parse_extern_script(ScriptHost *script_host, LPCWSTR src)
727 IMoniker *mon;
728 WCHAR *text;
729 HRESULT hres;
731 static const WCHAR wine_schemaW[] = {'w','i','n','e',':'};
733 if(strlenW(src) > sizeof(wine_schemaW)/sizeof(WCHAR) && !memcmp(src, wine_schemaW, sizeof(wine_schemaW)))
734 src += sizeof(wine_schemaW)/sizeof(WCHAR);
736 hres = CreateURLMoniker(NULL, src, &mon);
737 if(FAILED(hres))
738 return;
740 hres = bind_mon_to_wstr(script_host->window, mon, &text);
741 IMoniker_Release(mon);
742 if(FAILED(hres))
743 return;
745 parse_text(script_host, text);
747 heap_free(text);
750 static void parse_inline_script(ScriptHost *script_host, HTMLScriptElement *script_elem)
752 const PRUnichar *text;
753 nsAString text_str;
754 nsresult nsres;
756 nsAString_Init(&text_str, NULL);
757 nsres = nsIDOMHTMLScriptElement_GetText(script_elem->nsscript, &text_str);
758 nsAString_GetData(&text_str, &text);
760 if(NS_FAILED(nsres)) {
761 ERR("GetText failed: %08x\n", nsres);
762 }else if(*text) {
763 parse_text(script_host, text);
766 nsAString_Finish(&text_str);
769 static void parse_script_elem(ScriptHost *script_host, HTMLScriptElement *script_elem)
771 nsAString src_str, event_str;
772 const PRUnichar *src;
773 nsresult nsres;
775 nsAString_Init(&event_str, NULL);
776 nsres = nsIDOMHTMLScriptElement_GetEvent(script_elem->nsscript, &event_str);
777 if(NS_SUCCEEDED(nsres)) {
778 const PRUnichar *event;
780 nsAString_GetData(&event_str, &event);
781 if(*event) {
782 TRACE("deferring event %s script evaluation\n", debugstr_w(event));
783 nsAString_Finish(&event_str);
784 return;
786 }else {
787 ERR("GetAttribute(event) failed: %08x\n", nsres);
789 nsAString_Finish(&event_str);
791 nsAString_Init(&src_str, NULL);
792 nsres = nsIDOMHTMLScriptElement_GetSrc(script_elem->nsscript, &src_str);
793 nsAString_GetData(&src_str, &src);
795 if(NS_FAILED(nsres)) {
796 ERR("GetSrc failed: %08x\n", nsres);
797 }else if(*src) {
798 script_elem->parsed = TRUE;
799 parse_extern_script(script_host, src);
800 }else {
801 parse_inline_script(script_host, script_elem);
804 nsAString_Finish(&src_str);
807 static GUID get_default_script_guid(HTMLInnerWindow *window)
809 /* If not specified, we should use very first script host that was created for the page (or JScript if none) */
810 return list_empty(&window->script_hosts)
811 ? CLSID_JScript
812 : LIST_ENTRY(list_head(&window->script_hosts), ScriptHost, entry)->guid;
815 static BOOL get_guid_from_type(LPCWSTR type, GUID *guid)
817 const WCHAR text_javascriptW[] =
818 {'t','e','x','t','/','j','a','v','a','s','c','r','i','p','t',0};
819 const WCHAR text_vbscriptW[] =
820 {'t','e','x','t','/','v','b','s','c','r','i','p','t',0};
822 /* FIXME: Handle more types */
823 if(!strcmpiW(type, text_javascriptW)) {
824 *guid = CLSID_JScript;
825 }else if(!strcmpiW(type, text_vbscriptW)) {
826 *guid = CLSID_VBScript;
827 }else {
828 FIXME("Unknown type %s\n", debugstr_w(type));
829 return FALSE;
832 return TRUE;
835 static BOOL get_guid_from_language(LPCWSTR type, GUID *guid)
837 HRESULT hres;
839 hres = CLSIDFromProgID(type, guid);
840 if(FAILED(hres))
841 return FALSE;
843 /* FIXME: Check CATID_ActiveScriptParse */
845 return TRUE;
848 static BOOL get_script_guid(HTMLInnerWindow *window, nsIDOMHTMLScriptElement *nsscript, GUID *guid)
850 nsAString attr_str, val_str;
851 BOOL ret = FALSE;
852 nsresult nsres;
854 static const PRUnichar languageW[] = {'l','a','n','g','u','a','g','e',0};
856 nsAString_Init(&val_str, NULL);
858 nsres = nsIDOMHTMLScriptElement_GetType(nsscript, &val_str);
859 if(NS_SUCCEEDED(nsres)) {
860 const PRUnichar *type;
862 nsAString_GetData(&val_str, &type);
863 if(*type) {
864 ret = get_guid_from_type(type, guid);
865 nsAString_Finish(&val_str);
866 return ret;
868 }else {
869 ERR("GetType failed: %08x\n", nsres);
872 nsAString_InitDepend(&attr_str, languageW);
873 nsres = nsIDOMHTMLScriptElement_GetAttribute(nsscript, &attr_str, &val_str);
874 nsAString_Finish(&attr_str);
875 if(NS_SUCCEEDED(nsres)) {
876 const PRUnichar *language;
878 nsAString_GetData(&val_str, &language);
880 if(*language) {
881 ret = get_guid_from_language(language, guid);
882 }else {
883 *guid = get_default_script_guid(window);
884 ret = TRUE;
886 }else {
887 ERR("GetAttribute(language) failed: %08x\n", nsres);
890 nsAString_Finish(&val_str);
892 return ret;
895 static ScriptHost *get_script_host(HTMLInnerWindow *window, const GUID *guid)
897 ScriptHost *iter;
899 LIST_FOR_EACH_ENTRY(iter, &window->script_hosts, ScriptHost, entry) {
900 if(IsEqualGUID(guid, &iter->guid))
901 return iter;
904 return create_script_host(window, guid);
907 static ScriptHost *get_elem_script_host(HTMLInnerWindow *window, HTMLScriptElement *script_elem)
909 GUID guid;
911 if(!get_script_guid(window, script_elem->nsscript, &guid)) {
912 WARN("Could not find script GUID\n");
913 return NULL;
916 if(IsEqualGUID(&CLSID_JScript, &guid)
917 && (!window->base.outer_window || window->base.outer_window->scriptmode != SCRIPTMODE_ACTIVESCRIPT)) {
918 TRACE("Ignoring JScript\n");
919 return NULL;
922 return get_script_host(window, &guid);
925 void doc_insert_script(HTMLInnerWindow *window, HTMLScriptElement *script_elem)
927 ScriptHost *script_host;
929 script_host = get_elem_script_host(window, script_elem);
930 if(!script_host)
931 return;
933 if(script_host->parse)
934 parse_script_elem(script_host, script_elem);
937 IDispatch *script_parse_event(HTMLInnerWindow *window, LPCWSTR text)
939 ScriptHost *script_host;
940 GUID guid;
941 const WCHAR *ptr;
942 IDispatch *disp;
943 HRESULT hres;
945 static const WCHAR delimiterW[] = {'\"',0};
947 TRACE("%s\n", debugstr_w(text));
949 for(ptr = text; isalnumW(*ptr); ptr++);
950 if(*ptr == ':') {
951 LPWSTR language;
952 BOOL b;
954 language = heap_alloc((ptr-text+1)*sizeof(WCHAR));
955 if(!language)
956 return NULL;
958 memcpy(language, text, (ptr-text)*sizeof(WCHAR));
959 language[ptr-text] = 0;
961 b = get_guid_from_language(language, &guid);
963 heap_free(language);
965 if(!b) {
966 WARN("Could not find language\n");
967 return NULL;
970 ptr++;
971 }else {
972 ptr = text;
973 guid = get_default_script_guid(window);
976 if(IsEqualGUID(&CLSID_JScript, &guid)
977 && (!window->base.outer_window || window->base.outer_window->scriptmode != SCRIPTMODE_ACTIVESCRIPT)) {
978 TRACE("Ignoring JScript\n");
979 return NULL;
982 script_host = get_script_host(window, &guid);
983 if(!script_host || !script_host->parse_proc)
984 return NULL;
986 hres = IActiveScriptParseProcedure2_ParseProcedureText(script_host->parse_proc, ptr, NULL, emptyW,
987 NULL, NULL, delimiterW, 0 /* FIXME */, 0,
988 SCRIPTPROC_HOSTMANAGESSOURCE|SCRIPTPROC_IMPLICIT_THIS|SCRIPTPROC_IMPLICIT_PARENTS, &disp);
989 if(FAILED(hres)) {
990 WARN("ParseProcedureText failed: %08x\n", hres);
991 return NULL;
994 TRACE("ret %p\n", disp);
995 return disp;
998 HRESULT exec_script(HTMLInnerWindow *window, const WCHAR *code, const WCHAR *lang, VARIANT *ret)
1000 ScriptHost *script_host;
1001 EXCEPINFO ei;
1002 GUID guid;
1003 HRESULT hres;
1005 static const WCHAR delimW[] = {'"',0};
1007 if(!get_guid_from_language(lang, &guid)) {
1008 WARN("Could not find script GUID\n");
1009 return CO_E_CLASSSTRING;
1012 script_host = get_script_host(window, &guid);
1013 if(!script_host) {
1014 FIXME("No script host\n");
1015 return E_FAIL;
1018 if(!script_host->parse) {
1019 FIXME("script_host->parse == NULL\n");
1020 return E_FAIL;
1023 memset(&ei, 0, sizeof(ei));
1024 TRACE(">>>\n");
1025 hres = IActiveScriptParse_ParseScriptText(script_host->parse, code, NULL, NULL, delimW, 0, 0, SCRIPTTEXT_ISVISIBLE, ret, &ei);
1026 if(SUCCEEDED(hres))
1027 TRACE("<<<\n");
1028 else
1029 WARN("<<< %08x\n", hres);
1031 return hres;
1034 IDispatch *get_script_disp(ScriptHost *script_host)
1036 IDispatch *disp;
1037 HRESULT hres;
1039 if(!script_host->script)
1040 return NULL;
1042 hres = IActiveScript_GetScriptDispatch(script_host->script, windowW, &disp);
1043 if(FAILED(hres))
1044 return NULL;
1046 return disp;
1049 static event_target_t **find_event_target(HTMLDocumentNode *doc, HTMLScriptElement *script_elem, HTMLDOMNode **ret_target_node)
1051 HTMLDOMNode *target_node = NULL;
1052 event_target_t **target = NULL;
1053 const PRUnichar *target_id;
1054 nsAString target_id_str;
1055 nsresult nsres;
1056 HRESULT hres;
1058 nsAString_Init(&target_id_str, NULL);
1059 nsres = nsIDOMHTMLScriptElement_GetHtmlFor(script_elem->nsscript, &target_id_str);
1060 if(NS_FAILED(nsres)) {
1061 ERR("GetScriptFor failed: %08x\n", nsres);
1062 nsAString_Finish(&target_id_str);
1063 return NULL;
1066 nsAString_GetData(&target_id_str, &target_id);
1067 if(!*target_id) {
1068 FIXME("Empty for attribute\n");
1069 }else if(!strcmpW(target_id, documentW)) {
1070 target = &doc->node.event_target;
1071 target_node = &doc->node;
1072 IHTMLDOMNode_AddRef(&target_node->IHTMLDOMNode_iface);
1073 }else if(!strcmpW(target_id, windowW)) {
1074 target = &doc->body_event_target;
1075 }else {
1076 HTMLElement *target_elem;
1078 hres = get_doc_elem_by_id(doc, target_id, &target_elem);
1079 if(SUCCEEDED(hres) && target_elem) {
1080 target_node = &target_elem->node;
1081 target = &target_elem->node.event_target;
1084 nsAString_Finish(&target_id_str);
1086 *ret_target_node = target_node;
1087 return target;
1090 static BOOL parse_event_str(WCHAR *event, const WCHAR **args)
1092 WCHAR *ptr;
1094 TRACE("%s\n", debugstr_w(event));
1096 for(ptr = event; isalnumW(*ptr); ptr++);
1097 if(!*ptr) {
1098 *args = NULL;
1099 return TRUE;
1102 if(*ptr != '(')
1103 return FALSE;
1105 *ptr++ = 0;
1106 *args = ptr;
1107 while(isalnumW(*ptr) || isspaceW(*ptr) || *ptr == ',')
1108 ptr++;
1110 if(*ptr != ')')
1111 return FALSE;
1113 *ptr++ = 0;
1114 return !*ptr;
1117 static IDispatch *parse_event_elem(HTMLDocumentNode *doc, HTMLScriptElement *script_elem, WCHAR **ret_event)
1119 ScriptHost *script_host;
1120 WCHAR *event = NULL;
1121 const WCHAR *args;
1122 nsAString nsstr;
1123 IDispatch *disp;
1124 nsresult nsres;
1125 HRESULT hres;
1127 if(script_elem->parsed)
1128 return NULL;
1130 script_host = get_elem_script_host(doc->window, script_elem);
1131 if(!script_host || !script_host->parse_proc)
1132 return NULL;
1134 nsAString_Init(&nsstr, NULL);
1135 nsres = nsIDOMHTMLScriptElement_GetEvent(script_elem->nsscript, &nsstr);
1136 if(NS_SUCCEEDED(nsres)) {
1137 const PRUnichar *event_val;
1139 nsAString_GetData(&nsstr, &event_val);
1140 event = heap_strdupW(event_val);
1142 nsAString_Finish(&nsstr);
1143 if(!event)
1144 return NULL;
1146 if(!parse_event_str(event, &args)) {
1147 WARN("parsing %s failed\n", debugstr_w(event));
1148 heap_free(event);
1149 return NULL;
1152 nsAString_Init(&nsstr, NULL);
1153 nsres = nsIDOMHTMLScriptElement_GetText(script_elem->nsscript, &nsstr);
1154 if(NS_SUCCEEDED(nsres)) {
1155 const PRUnichar *text;
1157 nsAString_GetData(&nsstr, &text);
1158 hres = IActiveScriptParseProcedure2_ParseProcedureText(script_host->parse_proc, text, args,
1159 emptyW, NULL, NULL, script_endW, 0, 0,
1160 SCRIPTPROC_HOSTMANAGESSOURCE|SCRIPTPROC_IMPLICIT_THIS|SCRIPTPROC_IMPLICIT_PARENTS, &disp);
1161 if(FAILED(hres))
1162 disp = NULL;
1163 }else {
1164 ERR("GetText failed: %08x\n", nsres);
1165 disp = NULL;
1167 nsAString_Finish(&nsstr);
1168 if(!disp) {
1169 heap_free(event);
1170 return NULL;
1173 *ret_event = event;
1174 return disp;
1177 void bind_event_scripts(HTMLDocumentNode *doc)
1179 HTMLPluginContainer *plugin_container;
1180 nsIDOMHTMLScriptElement *nsscript;
1181 HTMLScriptElement *script_elem;
1182 event_target_t **event_target;
1183 nsIDOMNodeList *node_list;
1184 HTMLDOMNode *target_node;
1185 nsIDOMNode *script_node;
1186 nsAString selector_str;
1187 IDispatch *event_disp;
1188 UINT32 length, i;
1189 WCHAR *event;
1190 nsresult nsres;
1191 HRESULT hres;
1193 static const PRUnichar selectorW[] = {'s','c','r','i','p','t','[','e','v','e','n','t',']',0};
1195 TRACE("%p\n", doc);
1197 if(!doc->nsdoc)
1198 return;
1200 nsAString_InitDepend(&selector_str, selectorW);
1201 nsres = nsIDOMNodeSelector_QuerySelectorAll(doc->nsnode_selector, &selector_str, &node_list);
1202 nsAString_Finish(&selector_str);
1203 if(NS_FAILED(nsres)) {
1204 ERR("QuerySelectorAll failed: %08x\n", nsres);
1205 return;
1208 if(!node_list)
1209 return;
1211 nsres = nsIDOMNodeList_GetLength(node_list, &length);
1212 assert(nsres == NS_OK);
1214 for(i=0; i < length; i++) {
1215 nsres = nsIDOMNodeList_Item(node_list, i, &script_node);
1216 if(NS_FAILED(nsres) || !script_node) {
1217 ERR("Item(%d) failed: %08x\n", i, nsres);
1218 continue;
1221 nsres = nsIDOMNode_QueryInterface(script_node, &IID_nsIDOMHTMLScriptElement, (void**)&nsscript);
1222 assert(nsres == NS_OK);
1223 nsIDOMNode_Release(script_node);
1225 hres = script_elem_from_nsscript(doc, nsscript, &script_elem);
1226 if(FAILED(hres))
1227 continue;
1229 event_disp = parse_event_elem(doc, script_elem, &event);
1230 if(event_disp) {
1231 event_target = find_event_target(doc, script_elem, &target_node);
1232 if(event_target) {
1233 if(target_node)
1234 hres = IHTMLDOMNode_QueryInterface(&target_node->IHTMLDOMNode_iface, &IID_HTMLPluginContainer,
1235 (void**)&plugin_container);
1236 else
1237 hres = E_NOINTERFACE;
1239 if(SUCCEEDED(hres))
1240 bind_activex_event(doc, plugin_container, event, event_disp);
1241 else
1242 bind_node_event(doc, event_target, target_node, event, event_disp);
1244 if(target_node)
1245 IHTMLDOMNode_Release(&target_node->IHTMLDOMNode_iface);
1248 heap_free(event);
1249 IDispatch_Release(event_disp);
1252 IHTMLScriptElement_Release(&script_elem->IHTMLScriptElement_iface);
1255 nsIDOMNodeList_Release(node_list);
1258 BOOL find_global_prop(HTMLInnerWindow *window, BSTR name, DWORD flags, ScriptHost **ret_host, DISPID *ret_id)
1260 IDispatchEx *dispex;
1261 IDispatch *disp;
1262 ScriptHost *iter;
1263 HRESULT hres;
1265 LIST_FOR_EACH_ENTRY(iter, &window->script_hosts, ScriptHost, entry) {
1266 disp = get_script_disp(iter);
1267 if(!disp)
1268 continue;
1270 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1271 if(SUCCEEDED(hres)) {
1272 hres = IDispatchEx_GetDispID(dispex, name, flags & (~fdexNameEnsure), ret_id);
1273 IDispatchEx_Release(dispex);
1274 }else {
1275 FIXME("No IDispatchEx\n");
1276 hres = E_NOTIMPL;
1279 IDispatch_Release(disp);
1280 if(SUCCEEDED(hres)) {
1281 *ret_host = iter;
1282 return TRUE;
1286 return FALSE;
1289 static BOOL is_jscript_available(void)
1291 static BOOL available, checked;
1293 if(!checked) {
1294 IUnknown *unk;
1295 HRESULT hres = CoGetClassObject(&CLSID_JScript, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void**)&unk);
1297 if(SUCCEEDED(hres)) {
1298 available = TRUE;
1299 IUnknown_Release(unk);
1300 }else {
1301 available = FALSE;
1303 checked = TRUE;
1306 return available;
1309 void set_script_mode(HTMLOuterWindow *window, SCRIPTMODE mode)
1311 nsIWebBrowserSetup *setup;
1312 nsresult nsres;
1314 if(mode == SCRIPTMODE_ACTIVESCRIPT && !is_jscript_available()) {
1315 TRACE("jscript.dll not available\n");
1316 window->scriptmode = SCRIPTMODE_GECKO;
1317 return;
1320 window->scriptmode = mode;
1322 if(!window->doc_obj->nscontainer || !window->doc_obj->nscontainer->webbrowser)
1323 return;
1325 nsres = nsIWebBrowser_QueryInterface(window->doc_obj->nscontainer->webbrowser,
1326 &IID_nsIWebBrowserSetup, (void**)&setup);
1327 if(NS_SUCCEEDED(nsres)) {
1328 nsres = nsIWebBrowserSetup_SetProperty(setup, SETUP_ALLOW_JAVASCRIPT,
1329 window->scriptmode == SCRIPTMODE_GECKO);
1331 if(NS_SUCCEEDED(nsres))
1332 nsres = nsIWebBrowserSetup_SetProperty(setup, SETUP_DISABLE_NOSCRIPT, TRUE);
1334 nsIWebBrowserSetup_Release(setup);
1337 if(NS_FAILED(nsres))
1338 ERR("JavaScript setup failed: %08x\n", nsres);
1341 void release_script_hosts(HTMLInnerWindow *window)
1343 script_queue_entry_t *queue_iter;
1344 ScriptHost *iter;
1346 while(!list_empty(&window->script_queue)) {
1347 queue_iter = LIST_ENTRY(list_head(&window->script_queue), script_queue_entry_t, entry);
1349 list_remove(&queue_iter->entry);
1350 IHTMLScriptElement_Release(&queue_iter->script->IHTMLScriptElement_iface);
1351 heap_free(queue_iter);
1354 while(!list_empty(&window->script_hosts)) {
1355 iter = LIST_ENTRY(list_head(&window->script_hosts), ScriptHost, entry);
1357 release_script_engine(iter);
1358 list_remove(&iter->entry);
1359 iter->window = NULL;
1360 IActiveScriptSite_Release(&iter->IActiveScriptSite_iface);