ddraw: Use unsafe_impl_from_IDirect3DViewport for application provided interfaces.
[wine/multimedia.git] / dlls / mshtml / script.c
blob25ae0b676f14b2de7ebfff5f0f3f560cbf72f9a2
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>
23 #define COBJMACROS
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "ole2.h"
29 #include "activscp.h"
30 #include "activdbg.h"
32 #include "wine/debug.h"
34 #include "mshtml_private.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
38 #ifdef _WIN64
39 #define CTXARG_T DWORDLONG
40 #define IActiveScriptSiteDebugVtbl IActiveScriptSiteDebug64Vtbl
41 #else
42 #define CTXARG_T DWORD
43 #define IActiveScriptSiteDebugVtbl IActiveScriptSiteDebug32Vtbl
44 #endif
46 static const WCHAR windowW[] = {'w','i','n','d','o','w',0};
47 static const WCHAR emptyW[] = {0};
49 static const CLSID CLSID_JScript =
50 {0xf414c260,0x6ac0,0x11cf,{0xb6,0xd1,0x00,0xaa,0x00,0xbb,0xbb,0x58}};
51 static const CLSID CLSID_VBScript =
52 {0xb54f3741,0x5b07,0x11cf,{0xa4,0xb0,0x00,0xaa,0x00,0x4a,0x55,0xe8}};
54 struct ScriptHost {
55 IActiveScriptSite IActiveScriptSite_iface;
56 IActiveScriptSiteInterruptPoll IActiveScriptSiteInterruptPoll_iface;
57 IActiveScriptSiteWindow IActiveScriptSiteWindow_iface;
58 IActiveScriptSiteDebug IActiveScriptSiteDebug_iface;
59 IServiceProvider IServiceProvider_iface;
61 LONG ref;
63 IActiveScript *script;
64 IActiveScriptParse *parse;
65 IActiveScriptParseProcedure *parse_proc;
67 SCRIPTSTATE script_state;
69 HTMLWindow *window;
71 GUID guid;
72 struct list entry;
75 static void set_script_prop(ScriptHost *script_host, DWORD property, VARIANT *val)
77 IActiveScriptProperty *script_prop;
78 HRESULT hres;
80 hres = IActiveScript_QueryInterface(script_host->script, &IID_IActiveScriptProperty,
81 (void**)&script_prop);
82 if(FAILED(hres)) {
83 WARN("Could not get IActiveScriptProperty iface: %08x\n", hres);
84 return;
87 hres = IActiveScriptProperty_SetProperty(script_prop, property, NULL, val);
88 IActiveScriptProperty_Release(script_prop);
89 if(FAILED(hres))
90 WARN("SetProperty(%x) failed: %08x\n", property, hres);
93 static BOOL init_script_engine(ScriptHost *script_host)
95 IObjectSafety *safety;
96 SCRIPTSTATE state;
97 DWORD supported_opts=0, enabled_opts=0;
98 VARIANT var;
99 HRESULT hres;
101 hres = IActiveScript_QueryInterface(script_host->script, &IID_IActiveScriptParse, (void**)&script_host->parse);
102 if(FAILED(hres)) {
103 WARN("Could not get IActiveScriptHost: %08x\n", hres);
104 return FALSE;
107 hres = IActiveScript_QueryInterface(script_host->script, &IID_IObjectSafety, (void**)&safety);
108 if(FAILED(hres)) {
109 FIXME("Could not get IObjectSafety: %08x\n", hres);
110 return FALSE;
113 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported_opts, &enabled_opts);
114 if(FAILED(hres)) {
115 FIXME("GetInterfaceSafetyOptions failed: %08x\n", hres);
116 }else if(!(supported_opts & INTERFACE_USES_DISPEX)) {
117 FIXME("INTERFACE_USES_DISPEX is not supported\n");
118 }else {
119 hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse,
120 INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER,
121 INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER);
122 if(FAILED(hres))
123 FIXME("SetInterfaceSafetyOptions failed: %08x\n", hres);
126 IObjectSafety_Release(safety);
127 if(FAILED(hres))
128 return FALSE;
130 V_VT(&var) = VT_I4;
131 V_I4(&var) = 1;
132 set_script_prop(script_host, SCRIPTPROP_INVOKEVERSIONING, &var);
134 V_VT(&var) = VT_BOOL;
135 V_BOOL(&var) = VARIANT_TRUE;
136 set_script_prop(script_host, SCRIPTPROP_HACK_TRIDENTEVENTSINK, &var);
138 hres = IActiveScriptParse64_InitNew(script_host->parse);
139 if(FAILED(hres)) {
140 WARN("InitNew failed: %08x\n", hres);
141 return FALSE;
144 hres = IActiveScript_SetScriptSite(script_host->script, &script_host->IActiveScriptSite_iface);
145 if(FAILED(hres)) {
146 WARN("SetScriptSite failed: %08x\n", hres);
147 IActiveScript_Close(script_host->script);
148 return FALSE;
151 hres = IActiveScript_GetScriptState(script_host->script, &state);
152 if(FAILED(hres))
153 WARN("GetScriptState failed: %08x\n", hres);
154 else if(state != SCRIPTSTATE_INITIALIZED)
155 FIXME("state = %x\n", state);
157 hres = IActiveScript_SetScriptState(script_host->script, SCRIPTSTATE_STARTED);
158 if(FAILED(hres)) {
159 WARN("Starting script failed: %08x\n", hres);
160 return FALSE;
163 hres = IActiveScript_AddNamedItem(script_host->script, windowW,
164 SCRIPTITEM_ISVISIBLE|SCRIPTITEM_ISSOURCE|SCRIPTITEM_GLOBALMEMBERS);
165 if(SUCCEEDED(hres)) {
166 V_VT(&var) = VT_BOOL;
167 V_BOOL(&var) = VARIANT_TRUE;
168 set_script_prop(script_host, SCRIPTPROP_ABBREVIATE_GLOBALNAME_RESOLUTION, &var);
169 }else {
170 WARN("AddNamedItem failed: %08x\n", hres);
173 hres = IActiveScript_QueryInterface(script_host->script, &IID_IActiveScriptParseProcedure2,
174 (void**)&script_host->parse_proc);
175 if(FAILED(hres)) {
176 /* FIXME: QI for IActiveScriptParseProcedure */
177 WARN("Could not get IActiveScriptParseProcedure iface: %08x\n", hres);
180 return TRUE;
183 static void release_script_engine(ScriptHost *This)
185 if(!This->script)
186 return;
188 switch(This->script_state) {
189 case SCRIPTSTATE_CONNECTED:
190 IActiveScript_SetScriptState(This->script, SCRIPTSTATE_DISCONNECTED);
192 case SCRIPTSTATE_STARTED:
193 case SCRIPTSTATE_DISCONNECTED:
194 case SCRIPTSTATE_INITIALIZED:
195 IActiveScript_Close(This->script);
197 default:
198 if(This->parse_proc) {
199 IUnknown_Release(This->parse_proc);
200 This->parse_proc = NULL;
203 if(This->parse) {
204 IUnknown_Release(This->parse);
205 This->parse = NULL;
209 IActiveScript_Release(This->script);
210 This->script = NULL;
211 This->script_state = SCRIPTSTATE_UNINITIALIZED;
214 void connect_scripts(HTMLWindow *window)
216 ScriptHost *iter;
218 LIST_FOR_EACH_ENTRY(iter, &window->script_hosts, ScriptHost, entry) {
219 if(iter->script_state == SCRIPTSTATE_STARTED)
220 IActiveScript_SetScriptState(iter->script, SCRIPTSTATE_CONNECTED);
224 static inline ScriptHost *impl_from_IActiveScriptSite(IActiveScriptSite *iface)
226 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSite_iface);
229 static HRESULT WINAPI ActiveScriptSite_QueryInterface(IActiveScriptSite *iface, REFIID riid, void **ppv)
231 ScriptHost *This = impl_from_IActiveScriptSite(iface);
233 *ppv = NULL;
235 if(IsEqualGUID(&IID_IUnknown, riid)) {
236 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
237 *ppv = &This->IActiveScriptSite_iface;
238 }else if(IsEqualGUID(&IID_IActiveScriptSite, riid)) {
239 TRACE("(%p)->(IID_IActiveScriptSite %p)\n", This, ppv);
240 *ppv = &This->IActiveScriptSite_iface;
241 }else if(IsEqualGUID(&IID_IActiveScriptSiteInterruptPoll, riid)) {
242 TRACE("(%p)->(IID_IActiveScriptSiteInterruprtPoll %p)\n", This, ppv);
243 *ppv = &This->IActiveScriptSiteInterruptPoll_iface;
244 }else if(IsEqualGUID(&IID_IActiveScriptSiteWindow, riid)) {
245 TRACE("(%p)->(IID_IActiveScriptSiteWindow %p)\n", This, ppv);
246 *ppv = &This->IActiveScriptSiteWindow_iface;
247 }else if(IsEqualGUID(&IID_IActiveScriptSiteDebug, riid)) {
248 TRACE("(%p)->(IID_IActiveScriptSiteDebug %p)\n", This, ppv);
249 *ppv = &This->IActiveScriptSiteDebug_iface;
250 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
251 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
252 *ppv = &This->IServiceProvider_iface;
253 }else if(IsEqualGUID(&IID_ICanHandleException, riid)) {
254 TRACE("(%p)->(IID_ICanHandleException not supported %p)\n", This, ppv);
255 return E_NOINTERFACE;
256 }else {
257 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
258 return E_NOINTERFACE;
261 IUnknown_AddRef((IUnknown*)*ppv);
262 return S_OK;
265 static ULONG WINAPI ActiveScriptSite_AddRef(IActiveScriptSite *iface)
267 ScriptHost *This = impl_from_IActiveScriptSite(iface);
268 LONG ref = InterlockedIncrement(&This->ref);
270 TRACE("(%p) ref=%d\n", This, ref);
272 return ref;
275 static ULONG WINAPI ActiveScriptSite_Release(IActiveScriptSite *iface)
277 ScriptHost *This = impl_from_IActiveScriptSite(iface);
278 LONG ref = InterlockedDecrement(&This->ref);
280 TRACE("(%p) ref=%d\n", This, ref);
282 if(!ref) {
283 release_script_engine(This);
284 if(This->window)
285 list_remove(&This->entry);
286 heap_free(This);
289 return ref;
292 static HRESULT WINAPI ActiveScriptSite_GetLCID(IActiveScriptSite *iface, LCID *plcid)
294 ScriptHost *This = impl_from_IActiveScriptSite(iface);
296 TRACE("(%p)->(%p)\n", This, plcid);
298 *plcid = GetUserDefaultLCID();
299 return S_OK;
302 static HRESULT WINAPI ActiveScriptSite_GetItemInfo(IActiveScriptSite *iface, LPCOLESTR pstrName,
303 DWORD dwReturnMask, IUnknown **ppiunkItem, ITypeInfo **ppti)
305 ScriptHost *This = impl_from_IActiveScriptSite(iface);
307 TRACE("(%p)->(%s %x %p %p)\n", This, debugstr_w(pstrName), dwReturnMask, ppiunkItem, ppti);
309 if(dwReturnMask != SCRIPTINFO_IUNKNOWN) {
310 FIXME("Unsupported mask %x\n", dwReturnMask);
311 return E_NOTIMPL;
314 *ppiunkItem = NULL;
316 if(strcmpW(pstrName, windowW))
317 return DISP_E_MEMBERNOTFOUND;
319 if(!This->window)
320 return E_FAIL;
322 /* FIXME: Return proxy object */
323 *ppiunkItem = (IUnknown*)&This->window->IHTMLWindow2_iface;
324 IUnknown_AddRef(*ppiunkItem);
326 return S_OK;
329 static HRESULT WINAPI ActiveScriptSite_GetDocVersionString(IActiveScriptSite *iface, BSTR *pbstrVersion)
331 ScriptHost *This = impl_from_IActiveScriptSite(iface);
332 FIXME("(%p)->(%p)\n", This, pbstrVersion);
333 return E_NOTIMPL;
336 static HRESULT WINAPI ActiveScriptSite_OnScriptTerminate(IActiveScriptSite *iface,
337 const VARIANT *pvarResult, const EXCEPINFO *pexcepinfo)
339 ScriptHost *This = impl_from_IActiveScriptSite(iface);
340 FIXME("(%p)->(%p %p)\n", This, pvarResult, pexcepinfo);
341 return E_NOTIMPL;
344 static HRESULT WINAPI ActiveScriptSite_OnStateChange(IActiveScriptSite *iface, SCRIPTSTATE ssScriptState)
346 ScriptHost *This = impl_from_IActiveScriptSite(iface);
348 TRACE("(%p)->(%x)\n", This, ssScriptState);
350 This->script_state = ssScriptState;
351 return S_OK;
354 static HRESULT WINAPI ActiveScriptSite_OnScriptError(IActiveScriptSite *iface, IActiveScriptError *pscripterror)
356 ScriptHost *This = impl_from_IActiveScriptSite(iface);
357 FIXME("(%p)->(%p)\n", This, pscripterror);
358 return E_NOTIMPL;
361 static HRESULT WINAPI ActiveScriptSite_OnEnterScript(IActiveScriptSite *iface)
363 ScriptHost *This = impl_from_IActiveScriptSite(iface);
365 TRACE("(%p)->()\n", This);
367 return S_OK;
370 static HRESULT WINAPI ActiveScriptSite_OnLeaveScript(IActiveScriptSite *iface)
372 ScriptHost *This = impl_from_IActiveScriptSite(iface);
374 TRACE("(%p)->()\n", This);
376 return S_OK;
379 static const IActiveScriptSiteVtbl ActiveScriptSiteVtbl = {
380 ActiveScriptSite_QueryInterface,
381 ActiveScriptSite_AddRef,
382 ActiveScriptSite_Release,
383 ActiveScriptSite_GetLCID,
384 ActiveScriptSite_GetItemInfo,
385 ActiveScriptSite_GetDocVersionString,
386 ActiveScriptSite_OnScriptTerminate,
387 ActiveScriptSite_OnStateChange,
388 ActiveScriptSite_OnScriptError,
389 ActiveScriptSite_OnEnterScript,
390 ActiveScriptSite_OnLeaveScript
393 static inline ScriptHost *impl_from_IActiveScriptSiteInterruptPoll(IActiveScriptSiteInterruptPoll *iface)
395 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSiteInterruptPoll_iface);
398 static HRESULT WINAPI ActiveScriptSiteInterruptPoll_QueryInterface(IActiveScriptSiteInterruptPoll *iface,
399 REFIID riid, void **ppv)
401 ScriptHost *This = impl_from_IActiveScriptSiteInterruptPoll(iface);
402 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
405 static ULONG WINAPI ActiveScriptSiteInterruptPoll_AddRef(IActiveScriptSiteInterruptPoll *iface)
407 ScriptHost *This = impl_from_IActiveScriptSiteInterruptPoll(iface);
408 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
411 static ULONG WINAPI ActiveScriptSiteInterruptPoll_Release(IActiveScriptSiteInterruptPoll *iface)
413 ScriptHost *This = impl_from_IActiveScriptSiteInterruptPoll(iface);
414 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
417 static HRESULT WINAPI ActiveScriptSiteInterruptPoll_QueryContinue(IActiveScriptSiteInterruptPoll *iface)
419 ScriptHost *This = impl_from_IActiveScriptSiteInterruptPoll(iface);
421 TRACE("(%p)\n", This);
423 return S_OK;
426 static const IActiveScriptSiteInterruptPollVtbl ActiveScriptSiteInterruptPollVtbl = {
427 ActiveScriptSiteInterruptPoll_QueryInterface,
428 ActiveScriptSiteInterruptPoll_AddRef,
429 ActiveScriptSiteInterruptPoll_Release,
430 ActiveScriptSiteInterruptPoll_QueryContinue
433 static inline ScriptHost *impl_from_IActiveScriptSiteWindow(IActiveScriptSiteWindow *iface)
435 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSiteWindow_iface);
438 static HRESULT WINAPI ActiveScriptSiteWindow_QueryInterface(IActiveScriptSiteWindow *iface,
439 REFIID riid, void **ppv)
441 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
442 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
445 static ULONG WINAPI ActiveScriptSiteWindow_AddRef(IActiveScriptSiteWindow *iface)
447 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
448 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
451 static ULONG WINAPI ActiveScriptSiteWindow_Release(IActiveScriptSiteWindow *iface)
453 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
454 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
457 static HRESULT WINAPI ActiveScriptSiteWindow_GetWindow(IActiveScriptSiteWindow *iface, HWND *phwnd)
459 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
460 FIXME("(%p)->(%p)\n", This, phwnd);
461 return E_NOTIMPL;
464 static HRESULT WINAPI ActiveScriptSiteWindow_EnableModeless(IActiveScriptSiteWindow *iface, BOOL fEnable)
466 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
467 FIXME("(%p)->(%x)\n", This, fEnable);
468 return E_NOTIMPL;
471 static const IActiveScriptSiteWindowVtbl ActiveScriptSiteWindowVtbl = {
472 ActiveScriptSiteWindow_QueryInterface,
473 ActiveScriptSiteWindow_AddRef,
474 ActiveScriptSiteWindow_Release,
475 ActiveScriptSiteWindow_GetWindow,
476 ActiveScriptSiteWindow_EnableModeless
479 static inline ScriptHost *impl_from_IActiveScriptSiteDebug(IActiveScriptSiteDebug *iface)
481 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSiteDebug_iface);
484 static HRESULT WINAPI ActiveScriptSiteDebug_QueryInterface(IActiveScriptSiteDebug *iface,
485 REFIID riid, void **ppv)
487 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
488 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
491 static ULONG WINAPI ActiveScriptSiteDebug_AddRef(IActiveScriptSiteDebug *iface)
493 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
494 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
497 static ULONG WINAPI ActiveScriptSiteDebug_Release(IActiveScriptSiteDebug *iface)
499 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
500 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
503 static HRESULT WINAPI ActiveScriptSiteDebug_GetDocumentContextFromPosition(IActiveScriptSiteDebug *iface,
504 CTXARG_T dwSourceContext, ULONG uCharacterOffset, ULONG uNumChars, IDebugDocumentContext **ppsc)
506 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
507 FIXME("(%p)->(%s %u %u %p)\n", This, wine_dbgstr_longlong(dwSourceContext), uCharacterOffset,
508 uNumChars, ppsc);
509 return E_NOTIMPL;
512 static HRESULT WINAPI ActiveScriptSiteDebug_GetApplication(IActiveScriptSiteDebug *iface, IDebugApplication **ppda)
514 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
515 FIXME("(%p)->(%p)\n", This, ppda);
516 return E_NOTIMPL;
519 static HRESULT WINAPI ActiveScriptSiteDebug_GetRootApplicationNode(IActiveScriptSiteDebug *iface,
520 IDebugApplicationNode **ppdanRoot)
522 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
523 FIXME("(%p)->(%p)\n", This, ppdanRoot);
524 return E_NOTIMPL;
527 static HRESULT WINAPI ActiveScriptSiteDebug_OnScriptErrorDebug(IActiveScriptSiteDebug *iface,
528 IActiveScriptErrorDebug *pErrorDebug, BOOL *pfEnterDebugger, BOOL *pfCallOnScriptErrorWhenContinuing)
530 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
531 FIXME("(%p)->(%p %p %p)\n", This, pErrorDebug, pfEnterDebugger, pfCallOnScriptErrorWhenContinuing);
532 return E_NOTIMPL;
535 static const IActiveScriptSiteDebugVtbl ActiveScriptSiteDebugVtbl = {
536 ActiveScriptSiteDebug_QueryInterface,
537 ActiveScriptSiteDebug_AddRef,
538 ActiveScriptSiteDebug_Release,
539 ActiveScriptSiteDebug_GetDocumentContextFromPosition,
540 ActiveScriptSiteDebug_GetApplication,
541 ActiveScriptSiteDebug_GetRootApplicationNode,
542 ActiveScriptSiteDebug_OnScriptErrorDebug
545 static inline ScriptHost *impl_from_IServiceProvider(IServiceProvider *iface)
547 return CONTAINING_RECORD(iface, ScriptHost, IServiceProvider_iface);
550 static HRESULT WINAPI ASServiceProvider_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
552 ScriptHost *This = impl_from_IServiceProvider(iface);
553 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
556 static ULONG WINAPI ASServiceProvider_AddRef(IServiceProvider *iface)
558 ScriptHost *This = impl_from_IServiceProvider(iface);
559 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
562 static ULONG WINAPI ASServiceProvider_Release(IServiceProvider *iface)
564 ScriptHost *This = impl_from_IServiceProvider(iface);
565 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
568 static HRESULT WINAPI ASServiceProvider_QueryService(IServiceProvider *iface, REFGUID guidService,
569 REFIID riid, void **ppv)
571 ScriptHost *This = impl_from_IServiceProvider(iface);
573 if(IsEqualGUID(&SID_SInternetHostSecurityManager, guidService)) {
574 TRACE("(%p)->(SID_SInternetHostSecurityManager)\n", This);
576 if(!This->window || !This->window->doc)
577 return E_NOINTERFACE;
579 return IInternetHostSecurityManager_QueryInterface(&This->window->doc->IInternetHostSecurityManager_iface,
580 riid, ppv);
583 FIXME("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
584 return E_NOINTERFACE;
587 static const IServiceProviderVtbl ASServiceProviderVtbl = {
588 ASServiceProvider_QueryInterface,
589 ASServiceProvider_AddRef,
590 ASServiceProvider_Release,
591 ASServiceProvider_QueryService
594 static ScriptHost *create_script_host(HTMLWindow *window, const GUID *guid)
596 ScriptHost *ret;
597 HRESULT hres;
599 ret = heap_alloc_zero(sizeof(*ret));
600 ret->IActiveScriptSite_iface.lpVtbl = &ActiveScriptSiteVtbl;
601 ret->IActiveScriptSiteInterruptPoll_iface.lpVtbl = &ActiveScriptSiteInterruptPollVtbl;
602 ret->IActiveScriptSiteWindow_iface.lpVtbl = &ActiveScriptSiteWindowVtbl;
603 ret->IActiveScriptSiteDebug_iface.lpVtbl = &ActiveScriptSiteDebugVtbl;
604 ret->IServiceProvider_iface.lpVtbl = &ASServiceProviderVtbl;
605 ret->ref = 1;
606 ret->window = window;
607 ret->script_state = SCRIPTSTATE_UNINITIALIZED;
609 ret->guid = *guid;
610 list_add_tail(&window->script_hosts, &ret->entry);
612 hres = CoCreateInstance(&ret->guid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
613 &IID_IActiveScript, (void**)&ret->script);
614 if(FAILED(hres))
615 WARN("Could not load script engine: %08x\n", hres);
616 else if(!init_script_engine(ret))
617 release_script_engine(ret);
619 return ret;
622 static void parse_text(ScriptHost *script_host, LPCWSTR text)
624 EXCEPINFO excepinfo;
625 VARIANT var;
626 HRESULT hres;
628 static const WCHAR script_endW[] = {'<','/','S','C','R','I','P','T','>',0};
630 TRACE("%s\n", debugstr_w(text));
632 VariantInit(&var);
633 memset(&excepinfo, 0, sizeof(excepinfo));
634 TRACE(">>>\n");
635 hres = IActiveScriptParse64_ParseScriptText(script_host->parse, text, windowW, NULL, script_endW,
636 0, 0, SCRIPTTEXT_ISVISIBLE|SCRIPTTEXT_HOSTMANAGESSOURCE,
637 &var, &excepinfo);
638 if(SUCCEEDED(hres))
639 TRACE("<<<\n");
640 else
641 WARN("<<< %08x\n", hres);
645 static void parse_extern_script(ScriptHost *script_host, LPCWSTR src)
647 IMoniker *mon;
648 char *buf;
649 WCHAR *text;
650 DWORD len, size=0;
651 HRESULT hres;
653 static const WCHAR wine_schemaW[] = {'w','i','n','e',':'};
655 if(strlenW(src) > sizeof(wine_schemaW)/sizeof(WCHAR) && !memcmp(src, wine_schemaW, sizeof(wine_schemaW)))
656 src += sizeof(wine_schemaW)/sizeof(WCHAR);
658 hres = CreateURLMoniker(NULL, src, &mon);
659 if(FAILED(hres))
660 return;
662 hres = bind_mon_to_buffer(script_host->window->doc, mon, (void**)&buf, &size);
663 IMoniker_Release(mon);
664 if(FAILED(hres))
665 return;
667 len = MultiByteToWideChar(CP_ACP, 0, buf, size, NULL, 0);
668 text = heap_alloc((len+1)*sizeof(WCHAR));
669 MultiByteToWideChar(CP_ACP, 0, buf, size, text, len);
670 heap_free(buf);
671 text[len] = 0;
673 parse_text(script_host, text);
675 heap_free(text);
678 static void parse_inline_script(ScriptHost *script_host, nsIDOMHTMLScriptElement *nsscript)
680 const PRUnichar *text;
681 nsAString text_str;
682 nsresult nsres;
684 nsAString_Init(&text_str, NULL);
686 nsres = nsIDOMHTMLScriptElement_GetText(nsscript, &text_str);
688 if(NS_SUCCEEDED(nsres)) {
689 nsAString_GetData(&text_str, &text);
690 parse_text(script_host, text);
691 }else {
692 ERR("GetText failed: %08x\n", nsres);
695 nsAString_Finish(&text_str);
698 static void parse_script_elem(ScriptHost *script_host, nsIDOMHTMLScriptElement *nsscript)
700 const PRUnichar *src;
701 nsAString src_str;
702 nsresult nsres;
704 nsAString_Init(&src_str, NULL);
706 nsres = nsIDOMHTMLScriptElement_GetSrc(nsscript, &src_str);
707 nsAString_GetData(&src_str, &src);
709 if(NS_FAILED(nsres))
710 ERR("GetSrc failed: %08x\n", nsres);
711 else if(*src)
712 parse_extern_script(script_host, src);
713 else
714 parse_inline_script(script_host, nsscript);
716 nsAString_Finish(&src_str);
719 static BOOL get_guid_from_type(LPCWSTR type, GUID *guid)
721 const WCHAR text_javascriptW[] =
722 {'t','e','x','t','/','j','a','v','a','s','c','r','i','p','t',0};
723 const WCHAR text_vbscriptW[] =
724 {'t','e','x','t','/','v','b','s','c','r','i','p','t',0};
726 /* FIXME: Handle more types */
727 if(!strcmpiW(type, text_javascriptW)) {
728 *guid = CLSID_JScript;
729 }else if(!strcmpiW(type, text_vbscriptW)) {
730 *guid = CLSID_VBScript;
731 }else {
732 FIXME("Unknown type %s\n", debugstr_w(type));
733 return FALSE;
736 return TRUE;
739 static BOOL get_guid_from_language(LPCWSTR type, GUID *guid)
741 HRESULT hres;
743 hres = CLSIDFromProgID(type, guid);
744 if(FAILED(hres))
745 return FALSE;
747 /* FIXME: Check CATID_ActiveScriptParse */
749 return TRUE;
752 static BOOL get_script_guid(nsIDOMHTMLScriptElement *nsscript, GUID *guid)
754 nsAString attr_str, val_str;
755 BOOL ret = FALSE;
756 nsresult nsres;
758 static const PRUnichar languageW[] = {'l','a','n','g','u','a','g','e',0};
760 nsAString_Init(&val_str, NULL);
762 nsres = nsIDOMHTMLScriptElement_GetType(nsscript, &val_str);
763 if(NS_SUCCEEDED(nsres)) {
764 const PRUnichar *type;
766 nsAString_GetData(&val_str, &type);
767 if(*type) {
768 ret = get_guid_from_type(type, guid);
769 nsAString_Finish(&val_str);
770 return ret;
772 }else {
773 ERR("GetType failed: %08x\n", nsres);
776 nsAString_InitDepend(&attr_str, languageW);
777 nsres = nsIDOMHTMLScriptElement_GetAttribute(nsscript, &attr_str, &val_str);
778 nsAString_Finish(&attr_str);
779 if(NS_SUCCEEDED(nsres)) {
780 const PRUnichar *language;
782 nsAString_GetData(&val_str, &language);
784 if(*language) {
785 ret = get_guid_from_language(language, guid);
786 }else {
787 *guid = CLSID_JScript;
788 ret = TRUE;
790 }else {
791 ERR("GetAttribute(language) failed: %08x\n", nsres);
794 nsAString_Finish(&val_str);
796 return ret;
799 static ScriptHost *get_script_host(HTMLWindow *window, const GUID *guid)
801 ScriptHost *iter;
803 LIST_FOR_EACH_ENTRY(iter, &window->script_hosts, ScriptHost, entry) {
804 if(IsEqualGUID(guid, &iter->guid))
805 return iter;
808 return create_script_host(window, guid);
811 void doc_insert_script(HTMLWindow *window, nsIDOMHTMLScriptElement *nsscript)
813 ScriptHost *script_host;
814 GUID guid;
816 if(!get_script_guid(nsscript, &guid)) {
817 WARN("Could not find script GUID\n");
818 return;
821 if(IsEqualGUID(&CLSID_JScript, &guid) && window->scriptmode != SCRIPTMODE_ACTIVESCRIPT) {
822 TRACE("Ignoring JScript\n");
823 return;
826 script_host = get_script_host(window, &guid);
827 if(!script_host)
828 return;
830 if(script_host->parse)
831 parse_script_elem(script_host, nsscript);
834 IDispatch *script_parse_event(HTMLWindow *window, LPCWSTR text)
836 ScriptHost *script_host;
837 GUID guid = CLSID_JScript;
838 const WCHAR *ptr;
839 IDispatch *disp;
840 HRESULT hres;
842 static const WCHAR delimiterW[] = {'\"',0};
844 for(ptr = text; isalnumW(*ptr); ptr++);
845 if(*ptr == ':') {
846 LPWSTR language;
847 BOOL b;
849 language = heap_alloc((ptr-text+1)*sizeof(WCHAR));
850 memcpy(language, text, (ptr-text)*sizeof(WCHAR));
851 language[ptr-text] = 0;
853 b = get_guid_from_language(language, &guid);
855 heap_free(language);
857 if(!b) {
858 WARN("Could not find language\n");
859 return NULL;
862 ptr++;
863 }else {
864 ptr = text;
867 if(IsEqualGUID(&CLSID_JScript, &guid) && window->scriptmode != SCRIPTMODE_ACTIVESCRIPT) {
868 TRACE("Ignoring JScript\n");
869 return NULL;
872 script_host = get_script_host(window, &guid);
873 if(!script_host || !script_host->parse_proc)
874 return NULL;
876 hres = IActiveScriptParseProcedure64_ParseProcedureText(script_host->parse_proc, ptr, NULL, emptyW,
877 NULL, NULL, delimiterW, 0 /* FIXME */, 0,
878 SCRIPTPROC_HOSTMANAGESSOURCE|SCRIPTPROC_IMPLICIT_THIS|SCRIPTPROC_IMPLICIT_PARENTS, &disp);
879 if(FAILED(hres)) {
880 WARN("ParseProcedureText failed: %08x\n", hres);
881 return NULL;
884 TRACE("ret %p\n", disp);
885 return disp;
888 HRESULT exec_script(HTMLWindow *window, const WCHAR *code, const WCHAR *lang, VARIANT *ret)
890 ScriptHost *script_host;
891 EXCEPINFO ei;
892 GUID guid;
893 HRESULT hres;
895 static const WCHAR delimW[] = {'"',0};
897 if(!get_guid_from_language(lang, &guid)) {
898 WARN("Could not find script GUID\n");
899 return CO_E_CLASSSTRING;
902 script_host = get_script_host(window, &guid);
903 if(!script_host) {
904 FIXME("No script host\n");
905 return E_FAIL;
908 if(!script_host->parse) {
909 FIXME("script_host->parse == NULL\n");
910 return E_FAIL;
913 memset(&ei, 0, sizeof(ei));
914 TRACE(">>>\n");
915 hres = IActiveScriptParse64_ParseScriptText(script_host->parse, code, NULL, NULL, delimW, 0, 0, SCRIPTTEXT_ISVISIBLE, ret, &ei);
916 if(SUCCEEDED(hres))
917 TRACE("<<<\n");
918 else
919 WARN("<<< %08x\n", hres);
921 return hres;
924 IDispatch *get_script_disp(ScriptHost *script_host)
926 IDispatch *disp;
927 HRESULT hres;
929 if(!script_host->script)
930 return NULL;
932 hres = IActiveScript_GetScriptDispatch(script_host->script, windowW, &disp);
933 if(FAILED(hres))
934 return NULL;
936 return disp;
939 BOOL find_global_prop(HTMLWindow *window, BSTR name, DWORD flags, ScriptHost **ret_host, DISPID *ret_id)
941 IDispatchEx *dispex;
942 IDispatch *disp;
943 ScriptHost *iter;
944 HRESULT hres;
946 LIST_FOR_EACH_ENTRY(iter, &window->script_hosts, ScriptHost, entry) {
947 disp = get_script_disp(iter);
948 if(!disp)
949 continue;
951 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
952 if(SUCCEEDED(hres)) {
953 hres = IDispatchEx_GetDispID(dispex, name, flags & (~fdexNameEnsure), ret_id);
954 IDispatchEx_Release(dispex);
955 }else {
956 FIXME("No IDispatchEx\n");
957 hres = E_NOTIMPL;
960 IDispatch_Release(disp);
961 if(SUCCEEDED(hres)) {
962 *ret_host = iter;
963 return TRUE;
967 return FALSE;
970 static BOOL is_jscript_available(void)
972 static BOOL available, checked;
974 if(!checked) {
975 IUnknown *unk;
976 HRESULT hres = CoGetClassObject(&CLSID_JScript, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void**)&unk);
978 if(SUCCEEDED(hres)) {
979 available = TRUE;
980 IUnknown_Release(unk);
981 }else {
982 available = FALSE;
984 checked = TRUE;
987 return available;
990 void set_script_mode(HTMLWindow *window, SCRIPTMODE mode)
992 nsIWebBrowserSetup *setup;
993 nsresult nsres;
995 if(mode == SCRIPTMODE_ACTIVESCRIPT && !is_jscript_available()) {
996 TRACE("jscript.dll not available\n");
997 window->scriptmode = SCRIPTMODE_GECKO;
998 return;
1001 window->scriptmode = mode;
1003 if(!window->doc_obj->nscontainer || !window->doc_obj->nscontainer->webbrowser)
1004 return;
1006 nsres = nsIWebBrowser_QueryInterface(window->doc_obj->nscontainer->webbrowser,
1007 &IID_nsIWebBrowserSetup, (void**)&setup);
1008 if(NS_SUCCEEDED(nsres)) {
1009 nsres = nsIWebBrowserSetup_SetProperty(setup, SETUP_ALLOW_JAVASCRIPT,
1010 window->scriptmode == SCRIPTMODE_GECKO);
1011 nsIWebBrowserSetup_Release(setup);
1014 if(NS_FAILED(nsres))
1015 ERR("JavaScript setup failed: %08x\n", nsres);
1018 void release_script_hosts(HTMLWindow *window)
1020 ScriptHost *iter;
1022 while(!list_empty(&window->script_hosts)) {
1023 iter = LIST_ENTRY(list_head(&window->script_hosts), ScriptHost, entry);
1025 release_script_engine(iter);
1026 list_remove(&iter->entry);
1027 iter->window = NULL;
1028 IActiveScript_Release(&iter->IActiveScriptSite_iface);