gdi32: Simplify the code a little by using a separate variable for the return value.
[wine/multimedia.git] / dlls / mshtml / script.c
blob9b1a81ee8c1073cab5031ba06c4e546d1e7b30cd
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}};
52 struct ScriptHost {
53 IActiveScriptSite IActiveScriptSite_iface;
54 IActiveScriptSiteInterruptPoll IActiveScriptSiteInterruptPoll_iface;
55 IActiveScriptSiteWindow IActiveScriptSiteWindow_iface;
56 IActiveScriptSiteDebug IActiveScriptSiteDebug_iface;
57 IServiceProvider IServiceProvider_iface;
59 LONG ref;
61 IActiveScript *script;
62 IActiveScriptParse *parse;
63 IActiveScriptParseProcedure *parse_proc;
65 SCRIPTSTATE script_state;
67 HTMLWindow *window;
69 GUID guid;
70 struct list entry;
73 static void set_script_prop(ScriptHost *script_host, DWORD property, VARIANT *val)
75 IActiveScriptProperty *script_prop;
76 HRESULT hres;
78 hres = IActiveScript_QueryInterface(script_host->script, &IID_IActiveScriptProperty,
79 (void**)&script_prop);
80 if(FAILED(hres)) {
81 WARN("Could not get IActiveScriptProperty iface: %08x\n", hres);
82 return;
85 hres = IActiveScriptProperty_SetProperty(script_prop, property, NULL, val);
86 IActiveScriptProperty_Release(script_prop);
87 if(FAILED(hres))
88 WARN("SetProperty(%x) failed: %08x\n", property, hres);
91 static BOOL init_script_engine(ScriptHost *script_host)
93 IObjectSafety *safety;
94 SCRIPTSTATE state;
95 DWORD supported_opts=0, enabled_opts=0;
96 VARIANT var;
97 HRESULT hres;
99 hres = IActiveScript_QueryInterface(script_host->script, &IID_IActiveScriptParse, (void**)&script_host->parse);
100 if(FAILED(hres)) {
101 WARN("Could not get IActiveScriptHost: %08x\n", hres);
102 return FALSE;
105 hres = IActiveScript_QueryInterface(script_host->script, &IID_IObjectSafety, (void**)&safety);
106 if(FAILED(hres)) {
107 FIXME("Could not get IObjectSafety: %08x\n", hres);
108 return FALSE;
111 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported_opts, &enabled_opts);
112 if(FAILED(hres)) {
113 FIXME("GetInterfaceSafetyOptions failed: %08x\n", hres);
114 }else if(!(supported_opts & INTERFACE_USES_DISPEX)) {
115 FIXME("INTERFACE_USES_DISPEX is not supported\n");
116 }else {
117 hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse,
118 INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER,
119 INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER);
120 if(FAILED(hres))
121 FIXME("SetInterfaceSafetyOptions failed: %08x\n", hres);
124 IObjectSafety_Release(safety);
125 if(FAILED(hres))
126 return FALSE;
128 V_VT(&var) = VT_I4;
129 V_I4(&var) = 1;
130 set_script_prop(script_host, SCRIPTPROP_INVOKEVERSIONING, &var);
132 V_VT(&var) = VT_BOOL;
133 V_BOOL(&var) = VARIANT_TRUE;
134 set_script_prop(script_host, SCRIPTPROP_HACK_TRIDENTEVENTSINK, &var);
136 hres = IActiveScriptParse64_InitNew(script_host->parse);
137 if(FAILED(hres)) {
138 WARN("InitNew failed: %08x\n", hres);
139 return FALSE;
142 hres = IActiveScript_SetScriptSite(script_host->script, &script_host->IActiveScriptSite_iface);
143 if(FAILED(hres)) {
144 WARN("SetScriptSite failed: %08x\n", hres);
145 IActiveScript_Close(script_host->script);
146 return FALSE;
149 hres = IActiveScript_GetScriptState(script_host->script, &state);
150 if(FAILED(hres))
151 WARN("GetScriptState failed: %08x\n", hres);
152 else if(state != SCRIPTSTATE_INITIALIZED)
153 FIXME("state = %x\n", state);
155 hres = IActiveScript_SetScriptState(script_host->script, SCRIPTSTATE_STARTED);
156 if(FAILED(hres)) {
157 WARN("Starting script failed: %08x\n", hres);
158 return FALSE;
161 hres = IActiveScript_AddNamedItem(script_host->script, windowW,
162 SCRIPTITEM_ISVISIBLE|SCRIPTITEM_ISSOURCE|SCRIPTITEM_GLOBALMEMBERS);
163 if(SUCCEEDED(hres)) {
164 V_VT(&var) = VT_BOOL;
165 V_BOOL(&var) = VARIANT_TRUE;
166 set_script_prop(script_host, SCRIPTPROP_ABBREVIATE_GLOBALNAME_RESOLUTION, &var);
167 }else {
168 WARN("AddNamedItem failed: %08x\n", hres);
171 hres = IActiveScript_QueryInterface(script_host->script, &IID_IActiveScriptParseProcedure2,
172 (void**)&script_host->parse_proc);
173 if(FAILED(hres)) {
174 /* FIXME: QI for IActiveScriptParseProcedure */
175 WARN("Could not get IActiveScriptParseProcedure iface: %08x\n", hres);
178 return TRUE;
181 static void release_script_engine(ScriptHost *This)
183 if(!This->script)
184 return;
186 switch(This->script_state) {
187 case SCRIPTSTATE_CONNECTED:
188 IActiveScript_SetScriptState(This->script, SCRIPTSTATE_DISCONNECTED);
190 case SCRIPTSTATE_STARTED:
191 case SCRIPTSTATE_DISCONNECTED:
192 case SCRIPTSTATE_INITIALIZED:
193 IActiveScript_Close(This->script);
195 default:
196 if(This->parse_proc) {
197 IUnknown_Release(This->parse_proc);
198 This->parse_proc = NULL;
201 if(This->parse) {
202 IUnknown_Release(This->parse);
203 This->parse = NULL;
207 IActiveScript_Release(This->script);
208 This->script = NULL;
209 This->script_state = SCRIPTSTATE_UNINITIALIZED;
212 void connect_scripts(HTMLWindow *window)
214 ScriptHost *iter;
216 LIST_FOR_EACH_ENTRY(iter, &window->script_hosts, ScriptHost, entry) {
217 if(iter->script_state == SCRIPTSTATE_STARTED)
218 IActiveScript_SetScriptState(iter->script, SCRIPTSTATE_CONNECTED);
222 static inline ScriptHost *impl_from_IActiveScriptSite(IActiveScriptSite *iface)
224 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSite_iface);
227 static HRESULT WINAPI ActiveScriptSite_QueryInterface(IActiveScriptSite *iface, REFIID riid, void **ppv)
229 ScriptHost *This = impl_from_IActiveScriptSite(iface);
231 *ppv = NULL;
233 if(IsEqualGUID(&IID_IUnknown, riid)) {
234 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
235 *ppv = &This->IActiveScriptSite_iface;
236 }else if(IsEqualGUID(&IID_IActiveScriptSite, riid)) {
237 TRACE("(%p)->(IID_IActiveScriptSite %p)\n", This, ppv);
238 *ppv = &This->IActiveScriptSite_iface;
239 }else if(IsEqualGUID(&IID_IActiveScriptSiteInterruptPoll, riid)) {
240 TRACE("(%p)->(IID_IActiveScriptSiteInterruprtPoll %p)\n", This, ppv);
241 *ppv = &This->IActiveScriptSiteInterruptPoll_iface;
242 }else if(IsEqualGUID(&IID_IActiveScriptSiteWindow, riid)) {
243 TRACE("(%p)->(IID_IActiveScriptSiteWindow %p)\n", This, ppv);
244 *ppv = &This->IActiveScriptSiteWindow_iface;
245 }else if(IsEqualGUID(&IID_IActiveScriptSiteDebug, riid)) {
246 TRACE("(%p)->(IID_IActiveScriptSiteDebug %p)\n", This, ppv);
247 *ppv = &This->IActiveScriptSiteDebug_iface;
248 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
249 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
250 *ppv = &This->IServiceProvider_iface;
251 }else if(IsEqualGUID(&IID_ICanHandleException, riid)) {
252 TRACE("(%p)->(IID_ICanHandleException not supported %p)\n", This, ppv);
253 return E_NOINTERFACE;
254 }else {
255 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
256 return E_NOINTERFACE;
259 IUnknown_AddRef((IUnknown*)*ppv);
260 return S_OK;
263 static ULONG WINAPI ActiveScriptSite_AddRef(IActiveScriptSite *iface)
265 ScriptHost *This = impl_from_IActiveScriptSite(iface);
266 LONG ref = InterlockedIncrement(&This->ref);
268 TRACE("(%p) ref=%d\n", This, ref);
270 return ref;
273 static ULONG WINAPI ActiveScriptSite_Release(IActiveScriptSite *iface)
275 ScriptHost *This = impl_from_IActiveScriptSite(iface);
276 LONG ref = InterlockedDecrement(&This->ref);
278 TRACE("(%p) ref=%d\n", This, ref);
280 if(!ref) {
281 release_script_engine(This);
282 if(This->window)
283 list_remove(&This->entry);
284 heap_free(This);
287 return ref;
290 static HRESULT WINAPI ActiveScriptSite_GetLCID(IActiveScriptSite *iface, LCID *plcid)
292 ScriptHost *This = impl_from_IActiveScriptSite(iface);
294 TRACE("(%p)->(%p)\n", This, plcid);
296 *plcid = GetUserDefaultLCID();
297 return S_OK;
300 static HRESULT WINAPI ActiveScriptSite_GetItemInfo(IActiveScriptSite *iface, LPCOLESTR pstrName,
301 DWORD dwReturnMask, IUnknown **ppiunkItem, ITypeInfo **ppti)
303 ScriptHost *This = impl_from_IActiveScriptSite(iface);
305 TRACE("(%p)->(%s %x %p %p)\n", This, debugstr_w(pstrName), dwReturnMask, ppiunkItem, ppti);
307 if(dwReturnMask != SCRIPTINFO_IUNKNOWN) {
308 FIXME("Unsupported mask %x\n", dwReturnMask);
309 return E_NOTIMPL;
312 *ppiunkItem = NULL;
314 if(strcmpW(pstrName, windowW))
315 return DISP_E_MEMBERNOTFOUND;
317 if(!This->window)
318 return E_FAIL;
320 /* FIXME: Return proxy object */
321 *ppiunkItem = (IUnknown*)&This->window->IHTMLWindow2_iface;
322 IUnknown_AddRef(*ppiunkItem);
324 return S_OK;
327 static HRESULT WINAPI ActiveScriptSite_GetDocVersionString(IActiveScriptSite *iface, BSTR *pbstrVersion)
329 ScriptHost *This = impl_from_IActiveScriptSite(iface);
330 FIXME("(%p)->(%p)\n", This, pbstrVersion);
331 return E_NOTIMPL;
334 static HRESULT WINAPI ActiveScriptSite_OnScriptTerminate(IActiveScriptSite *iface,
335 const VARIANT *pvarResult, const EXCEPINFO *pexcepinfo)
337 ScriptHost *This = impl_from_IActiveScriptSite(iface);
338 FIXME("(%p)->(%p %p)\n", This, pvarResult, pexcepinfo);
339 return E_NOTIMPL;
342 static HRESULT WINAPI ActiveScriptSite_OnStateChange(IActiveScriptSite *iface, SCRIPTSTATE ssScriptState)
344 ScriptHost *This = impl_from_IActiveScriptSite(iface);
346 TRACE("(%p)->(%x)\n", This, ssScriptState);
348 This->script_state = ssScriptState;
349 return S_OK;
352 static HRESULT WINAPI ActiveScriptSite_OnScriptError(IActiveScriptSite *iface, IActiveScriptError *pscripterror)
354 ScriptHost *This = impl_from_IActiveScriptSite(iface);
355 FIXME("(%p)->(%p)\n", This, pscripterror);
356 return E_NOTIMPL;
359 static HRESULT WINAPI ActiveScriptSite_OnEnterScript(IActiveScriptSite *iface)
361 ScriptHost *This = impl_from_IActiveScriptSite(iface);
363 TRACE("(%p)->()\n", This);
365 return S_OK;
368 static HRESULT WINAPI ActiveScriptSite_OnLeaveScript(IActiveScriptSite *iface)
370 ScriptHost *This = impl_from_IActiveScriptSite(iface);
372 TRACE("(%p)->()\n", This);
374 return S_OK;
377 static const IActiveScriptSiteVtbl ActiveScriptSiteVtbl = {
378 ActiveScriptSite_QueryInterface,
379 ActiveScriptSite_AddRef,
380 ActiveScriptSite_Release,
381 ActiveScriptSite_GetLCID,
382 ActiveScriptSite_GetItemInfo,
383 ActiveScriptSite_GetDocVersionString,
384 ActiveScriptSite_OnScriptTerminate,
385 ActiveScriptSite_OnStateChange,
386 ActiveScriptSite_OnScriptError,
387 ActiveScriptSite_OnEnterScript,
388 ActiveScriptSite_OnLeaveScript
391 static inline ScriptHost *impl_from_IActiveScriptSiteInterruptPoll(IActiveScriptSiteInterruptPoll *iface)
393 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSiteInterruptPoll_iface);
396 static HRESULT WINAPI ActiveScriptSiteInterruptPoll_QueryInterface(IActiveScriptSiteInterruptPoll *iface,
397 REFIID riid, void **ppv)
399 ScriptHost *This = impl_from_IActiveScriptSiteInterruptPoll(iface);
400 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
403 static ULONG WINAPI ActiveScriptSiteInterruptPoll_AddRef(IActiveScriptSiteInterruptPoll *iface)
405 ScriptHost *This = impl_from_IActiveScriptSiteInterruptPoll(iface);
406 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
409 static ULONG WINAPI ActiveScriptSiteInterruptPoll_Release(IActiveScriptSiteInterruptPoll *iface)
411 ScriptHost *This = impl_from_IActiveScriptSiteInterruptPoll(iface);
412 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
415 static HRESULT WINAPI ActiveScriptSiteInterruptPoll_QueryContinue(IActiveScriptSiteInterruptPoll *iface)
417 ScriptHost *This = impl_from_IActiveScriptSiteInterruptPoll(iface);
419 TRACE("(%p)\n", This);
421 return S_OK;
424 static const IActiveScriptSiteInterruptPollVtbl ActiveScriptSiteInterruptPollVtbl = {
425 ActiveScriptSiteInterruptPoll_QueryInterface,
426 ActiveScriptSiteInterruptPoll_AddRef,
427 ActiveScriptSiteInterruptPoll_Release,
428 ActiveScriptSiteInterruptPoll_QueryContinue
431 static inline ScriptHost *impl_from_IActiveScriptSiteWindow(IActiveScriptSiteWindow *iface)
433 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSiteWindow_iface);
436 static HRESULT WINAPI ActiveScriptSiteWindow_QueryInterface(IActiveScriptSiteWindow *iface,
437 REFIID riid, void **ppv)
439 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
440 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
443 static ULONG WINAPI ActiveScriptSiteWindow_AddRef(IActiveScriptSiteWindow *iface)
445 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
446 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
449 static ULONG WINAPI ActiveScriptSiteWindow_Release(IActiveScriptSiteWindow *iface)
451 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
452 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
455 static HRESULT WINAPI ActiveScriptSiteWindow_GetWindow(IActiveScriptSiteWindow *iface, HWND *phwnd)
457 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
458 FIXME("(%p)->(%p)\n", This, phwnd);
459 return E_NOTIMPL;
462 static HRESULT WINAPI ActiveScriptSiteWindow_EnableModeless(IActiveScriptSiteWindow *iface, BOOL fEnable)
464 ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
465 FIXME("(%p)->(%x)\n", This, fEnable);
466 return E_NOTIMPL;
469 static const IActiveScriptSiteWindowVtbl ActiveScriptSiteWindowVtbl = {
470 ActiveScriptSiteWindow_QueryInterface,
471 ActiveScriptSiteWindow_AddRef,
472 ActiveScriptSiteWindow_Release,
473 ActiveScriptSiteWindow_GetWindow,
474 ActiveScriptSiteWindow_EnableModeless
477 static inline ScriptHost *impl_from_IActiveScriptSiteDebug(IActiveScriptSiteDebug *iface)
479 return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSiteDebug_iface);
482 static HRESULT WINAPI ActiveScriptSiteDebug_QueryInterface(IActiveScriptSiteDebug *iface,
483 REFIID riid, void **ppv)
485 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
486 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
489 static ULONG WINAPI ActiveScriptSiteDebug_AddRef(IActiveScriptSiteDebug *iface)
491 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
492 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
495 static ULONG WINAPI ActiveScriptSiteDebug_Release(IActiveScriptSiteDebug *iface)
497 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
498 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
501 static HRESULT WINAPI ActiveScriptSiteDebug_GetDocumentContextFromPosition(IActiveScriptSiteDebug *iface,
502 CTXARG_T dwSourceContext, ULONG uCharacterOffset, ULONG uNumChars, IDebugDocumentContext **ppsc)
504 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
505 FIXME("(%p)->(%s %u %u %p)\n", This, wine_dbgstr_longlong(dwSourceContext), uCharacterOffset,
506 uNumChars, ppsc);
507 return E_NOTIMPL;
510 static HRESULT WINAPI ActiveScriptSiteDebug_GetApplication(IActiveScriptSiteDebug *iface, IDebugApplication **ppda)
512 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
513 FIXME("(%p)->(%p)\n", This, ppda);
514 return E_NOTIMPL;
517 static HRESULT WINAPI ActiveScriptSiteDebug_GetRootApplicationNode(IActiveScriptSiteDebug *iface,
518 IDebugApplicationNode **ppdanRoot)
520 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
521 FIXME("(%p)->(%p)\n", This, ppdanRoot);
522 return E_NOTIMPL;
525 static HRESULT WINAPI ActiveScriptSiteDebug_OnScriptErrorDebug(IActiveScriptSiteDebug *iface,
526 IActiveScriptErrorDebug *pErrorDebug, BOOL *pfEnterDebugger, BOOL *pfCallOnScriptErrorWhenContinuing)
528 ScriptHost *This = impl_from_IActiveScriptSiteDebug(iface);
529 FIXME("(%p)->(%p %p %p)\n", This, pErrorDebug, pfEnterDebugger, pfCallOnScriptErrorWhenContinuing);
530 return E_NOTIMPL;
533 static const IActiveScriptSiteDebugVtbl ActiveScriptSiteDebugVtbl = {
534 ActiveScriptSiteDebug_QueryInterface,
535 ActiveScriptSiteDebug_AddRef,
536 ActiveScriptSiteDebug_Release,
537 ActiveScriptSiteDebug_GetDocumentContextFromPosition,
538 ActiveScriptSiteDebug_GetApplication,
539 ActiveScriptSiteDebug_GetRootApplicationNode,
540 ActiveScriptSiteDebug_OnScriptErrorDebug
543 static inline ScriptHost *impl_from_IServiceProvider(IServiceProvider *iface)
545 return CONTAINING_RECORD(iface, ScriptHost, IServiceProvider_iface);
548 static HRESULT WINAPI ASServiceProvider_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
550 ScriptHost *This = impl_from_IServiceProvider(iface);
551 return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, ppv);
554 static ULONG WINAPI ASServiceProvider_AddRef(IServiceProvider *iface)
556 ScriptHost *This = impl_from_IServiceProvider(iface);
557 return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
560 static ULONG WINAPI ASServiceProvider_Release(IServiceProvider *iface)
562 ScriptHost *This = impl_from_IServiceProvider(iface);
563 return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
566 static HRESULT WINAPI ASServiceProvider_QueryService(IServiceProvider *iface, REFGUID guidService,
567 REFIID riid, void **ppv)
569 ScriptHost *This = impl_from_IServiceProvider(iface);
571 if(IsEqualGUID(&SID_SInternetHostSecurityManager, guidService)) {
572 TRACE("(%p)->(SID_SInternetHostSecurityManager)\n", This);
574 if(!This->window || !This->window->doc)
575 return E_NOINTERFACE;
577 return IInternetHostSecurityManager_QueryInterface(&This->window->doc->IInternetHostSecurityManager_iface,
578 riid, ppv);
581 FIXME("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
582 return E_NOINTERFACE;
585 static const IServiceProviderVtbl ASServiceProviderVtbl = {
586 ASServiceProvider_QueryInterface,
587 ASServiceProvider_AddRef,
588 ASServiceProvider_Release,
589 ASServiceProvider_QueryService
592 static ScriptHost *create_script_host(HTMLWindow *window, const GUID *guid)
594 ScriptHost *ret;
595 HRESULT hres;
597 ret = heap_alloc_zero(sizeof(*ret));
598 ret->IActiveScriptSite_iface.lpVtbl = &ActiveScriptSiteVtbl;
599 ret->IActiveScriptSiteInterruptPoll_iface.lpVtbl = &ActiveScriptSiteInterruptPollVtbl;
600 ret->IActiveScriptSiteWindow_iface.lpVtbl = &ActiveScriptSiteWindowVtbl;
601 ret->IActiveScriptSiteDebug_iface.lpVtbl = &ActiveScriptSiteDebugVtbl;
602 ret->IServiceProvider_iface.lpVtbl = &ASServiceProviderVtbl;
603 ret->ref = 1;
604 ret->window = window;
605 ret->script_state = SCRIPTSTATE_UNINITIALIZED;
607 ret->guid = *guid;
608 list_add_tail(&window->script_hosts, &ret->entry);
610 hres = CoCreateInstance(&ret->guid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
611 &IID_IActiveScript, (void**)&ret->script);
612 if(FAILED(hres))
613 WARN("Could not load script engine: %08x\n", hres);
614 else if(!init_script_engine(ret))
615 release_script_engine(ret);
617 return ret;
620 static void parse_text(ScriptHost *script_host, LPCWSTR text)
622 EXCEPINFO excepinfo;
623 VARIANT var;
624 HRESULT hres;
626 static const WCHAR script_endW[] = {'<','/','S','C','R','I','P','T','>',0};
628 TRACE("%s\n", debugstr_w(text));
630 VariantInit(&var);
631 memset(&excepinfo, 0, sizeof(excepinfo));
632 TRACE(">>>\n");
633 hres = IActiveScriptParse64_ParseScriptText(script_host->parse, text, windowW, NULL, script_endW,
634 0, 0, SCRIPTTEXT_ISVISIBLE|SCRIPTTEXT_HOSTMANAGESSOURCE,
635 &var, &excepinfo);
636 if(SUCCEEDED(hres))
637 TRACE("<<<\n");
638 else
639 WARN("<<< %08x\n", hres);
643 static void parse_extern_script(ScriptHost *script_host, LPCWSTR src)
645 IMoniker *mon;
646 char *buf;
647 WCHAR *text;
648 DWORD len, size=0;
649 HRESULT hres;
651 static const WCHAR wine_schemaW[] = {'w','i','n','e',':'};
653 if(strlenW(src) > sizeof(wine_schemaW)/sizeof(WCHAR) && !memcmp(src, wine_schemaW, sizeof(wine_schemaW)))
654 src += sizeof(wine_schemaW)/sizeof(WCHAR);
656 hres = CreateURLMoniker(NULL, src, &mon);
657 if(FAILED(hres))
658 return;
660 hres = bind_mon_to_buffer(script_host->window->doc, mon, (void**)&buf, &size);
661 IMoniker_Release(mon);
662 if(FAILED(hres))
663 return;
665 len = MultiByteToWideChar(CP_ACP, 0, buf, size, NULL, 0);
666 text = heap_alloc((len+1)*sizeof(WCHAR));
667 MultiByteToWideChar(CP_ACP, 0, buf, size, text, len);
668 heap_free(buf);
669 text[len] = 0;
671 parse_text(script_host, text);
673 heap_free(text);
676 static void parse_inline_script(ScriptHost *script_host, nsIDOMHTMLScriptElement *nsscript)
678 const PRUnichar *text;
679 nsAString text_str;
680 nsresult nsres;
682 nsAString_Init(&text_str, NULL);
684 nsres = nsIDOMHTMLScriptElement_GetText(nsscript, &text_str);
686 if(NS_SUCCEEDED(nsres)) {
687 nsAString_GetData(&text_str, &text);
688 parse_text(script_host, text);
689 }else {
690 ERR("GetText failed: %08x\n", nsres);
693 nsAString_Finish(&text_str);
696 static void parse_script_elem(ScriptHost *script_host, nsIDOMHTMLScriptElement *nsscript)
698 const PRUnichar *src;
699 nsAString src_str;
700 nsresult nsres;
702 nsAString_Init(&src_str, NULL);
704 nsres = nsIDOMHTMLScriptElement_GetSrc(nsscript, &src_str);
705 nsAString_GetData(&src_str, &src);
707 if(NS_FAILED(nsres))
708 ERR("GetSrc failed: %08x\n", nsres);
709 else if(*src)
710 parse_extern_script(script_host, src);
711 else
712 parse_inline_script(script_host, nsscript);
714 nsAString_Finish(&src_str);
717 static BOOL get_guid_from_type(LPCWSTR type, GUID *guid)
719 const WCHAR text_javascriptW[] =
720 {'t','e','x','t','/','j','a','v','a','s','c','r','i','p','t',0};
722 /* FIXME: Handle more types */
723 if(!strcmpiW(type, text_javascriptW)) {
724 *guid = CLSID_JScript;
725 }else {
726 FIXME("Unknown type %s\n", debugstr_w(type));
727 return FALSE;
730 return TRUE;
733 static BOOL get_guid_from_language(LPCWSTR type, GUID *guid)
735 HRESULT hres;
737 hres = CLSIDFromProgID(type, guid);
738 if(FAILED(hres))
739 return FALSE;
741 /* FIXME: Check CATID_ActiveScriptParse */
743 return TRUE;
746 static BOOL get_script_guid(nsIDOMHTMLScriptElement *nsscript, GUID *guid)
748 nsAString attr_str, val_str;
749 BOOL ret = FALSE;
750 nsresult nsres;
752 static const PRUnichar languageW[] = {'l','a','n','g','u','a','g','e',0};
754 nsAString_Init(&val_str, NULL);
756 nsres = nsIDOMHTMLScriptElement_GetType(nsscript, &val_str);
757 if(NS_SUCCEEDED(nsres)) {
758 const PRUnichar *type;
760 nsAString_GetData(&val_str, &type);
761 if(*type) {
762 ret = get_guid_from_type(type, guid);
763 nsAString_Finish(&val_str);
764 return ret;
766 }else {
767 ERR("GetType failed: %08x\n", nsres);
770 nsAString_InitDepend(&attr_str, languageW);
771 nsres = nsIDOMHTMLScriptElement_GetAttribute(nsscript, &attr_str, &val_str);
772 nsAString_Finish(&attr_str);
773 if(NS_SUCCEEDED(nsres)) {
774 const PRUnichar *language;
776 nsAString_GetData(&val_str, &language);
778 if(*language) {
779 ret = get_guid_from_language(language, guid);
780 }else {
781 *guid = CLSID_JScript;
782 ret = TRUE;
784 }else {
785 ERR("GetAttribute(language) failed: %08x\n", nsres);
788 nsAString_Finish(&val_str);
790 return ret;
793 static ScriptHost *get_script_host(HTMLWindow *window, const GUID *guid)
795 ScriptHost *iter;
797 LIST_FOR_EACH_ENTRY(iter, &window->script_hosts, ScriptHost, entry) {
798 if(IsEqualGUID(guid, &iter->guid))
799 return iter;
802 return create_script_host(window, guid);
805 void doc_insert_script(HTMLWindow *window, nsIDOMHTMLScriptElement *nsscript)
807 ScriptHost *script_host;
808 GUID guid;
810 if(!get_script_guid(nsscript, &guid)) {
811 WARN("Could not find script GUID\n");
812 return;
815 if(IsEqualGUID(&CLSID_JScript, &guid) && window->scriptmode != SCRIPTMODE_ACTIVESCRIPT) {
816 TRACE("Ignoring JScript\n");
817 return;
820 script_host = get_script_host(window, &guid);
821 if(!script_host)
822 return;
824 if(script_host->parse)
825 parse_script_elem(script_host, nsscript);
828 IDispatch *script_parse_event(HTMLWindow *window, LPCWSTR text)
830 ScriptHost *script_host;
831 GUID guid = CLSID_JScript;
832 const WCHAR *ptr;
833 IDispatch *disp;
834 HRESULT hres;
836 static const WCHAR delimiterW[] = {'\"',0};
838 for(ptr = text; isalnumW(*ptr); ptr++);
839 if(*ptr == ':') {
840 LPWSTR language;
841 BOOL b;
843 language = heap_alloc((ptr-text+1)*sizeof(WCHAR));
844 memcpy(language, text, (ptr-text)*sizeof(WCHAR));
845 language[ptr-text] = 0;
847 b = get_guid_from_language(language, &guid);
849 heap_free(language);
851 if(!b) {
852 WARN("Could not find language\n");
853 return NULL;
856 ptr++;
857 }else {
858 ptr = text;
861 if(IsEqualGUID(&CLSID_JScript, &guid) && window->scriptmode != SCRIPTMODE_ACTIVESCRIPT) {
862 TRACE("Ignoring JScript\n");
863 return NULL;
866 script_host = get_script_host(window, &guid);
867 if(!script_host || !script_host->parse_proc)
868 return NULL;
870 hres = IActiveScriptParseProcedure64_ParseProcedureText(script_host->parse_proc, ptr, NULL, emptyW,
871 NULL, NULL, delimiterW, 0 /* FIXME */, 0,
872 SCRIPTPROC_HOSTMANAGESSOURCE|SCRIPTPROC_IMPLICIT_THIS|SCRIPTPROC_IMPLICIT_PARENTS, &disp);
873 if(FAILED(hres)) {
874 WARN("ParseProcedureText failed: %08x\n", hres);
875 return NULL;
878 TRACE("ret %p\n", disp);
879 return disp;
882 HRESULT exec_script(HTMLWindow *window, const WCHAR *code, const WCHAR *lang, VARIANT *ret)
884 ScriptHost *script_host;
885 EXCEPINFO ei;
886 GUID guid;
887 HRESULT hres;
889 static const WCHAR delimW[] = {'"',0};
891 if(!get_guid_from_language(lang, &guid)) {
892 WARN("Could not find script GUID\n");
893 return CO_E_CLASSSTRING;
896 script_host = get_script_host(window, &guid);
897 if(!script_host) {
898 FIXME("No script host\n");
899 return E_FAIL;
902 if(!script_host->parse) {
903 FIXME("script_host->parse == NULL\n");
904 return E_FAIL;
907 memset(&ei, 0, sizeof(ei));
908 TRACE(">>>\n");
909 hres = IActiveScriptParse64_ParseScriptText(script_host->parse, code, NULL, NULL, delimW, 0, 0, SCRIPTTEXT_ISVISIBLE, ret, &ei);
910 if(SUCCEEDED(hres))
911 TRACE("<<<\n");
912 else
913 WARN("<<< %08x\n", hres);
915 return hres;
918 IDispatch *get_script_disp(ScriptHost *script_host)
920 IDispatch *disp;
921 HRESULT hres;
923 if(!script_host->script)
924 return NULL;
926 hres = IActiveScript_GetScriptDispatch(script_host->script, windowW, &disp);
927 if(FAILED(hres))
928 return NULL;
930 return disp;
933 BOOL find_global_prop(HTMLWindow *window, BSTR name, DWORD flags, ScriptHost **ret_host, DISPID *ret_id)
935 IDispatchEx *dispex;
936 IDispatch *disp;
937 ScriptHost *iter;
938 HRESULT hres;
940 LIST_FOR_EACH_ENTRY(iter, &window->script_hosts, ScriptHost, entry) {
941 disp = get_script_disp(iter);
942 if(!disp)
943 continue;
945 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
946 if(SUCCEEDED(hres)) {
947 hres = IDispatchEx_GetDispID(dispex, name, flags & (~fdexNameEnsure), ret_id);
948 IDispatchEx_Release(dispex);
949 }else {
950 FIXME("No IDispatchEx\n");
951 hres = E_NOTIMPL;
954 IDispatch_Release(disp);
955 if(SUCCEEDED(hres)) {
956 *ret_host = iter;
957 return TRUE;
961 return FALSE;
964 static BOOL is_jscript_available(void)
966 static BOOL available, checked;
968 if(!checked) {
969 IUnknown *unk;
970 HRESULT hres = CoGetClassObject(&CLSID_JScript, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void**)&unk);
972 if(SUCCEEDED(hres)) {
973 available = TRUE;
974 IUnknown_Release(unk);
975 }else {
976 available = FALSE;
978 checked = TRUE;
981 return available;
984 void set_script_mode(HTMLWindow *window, SCRIPTMODE mode)
986 nsIWebBrowserSetup *setup;
987 nsresult nsres;
989 if(mode == SCRIPTMODE_ACTIVESCRIPT && !is_jscript_available()) {
990 TRACE("jscript.dll not available\n");
991 window->scriptmode = SCRIPTMODE_GECKO;
992 return;
995 window->scriptmode = mode;
997 if(!window->doc_obj->nscontainer || !window->doc_obj->nscontainer->webbrowser)
998 return;
1000 nsres = nsIWebBrowser_QueryInterface(window->doc_obj->nscontainer->webbrowser,
1001 &IID_nsIWebBrowserSetup, (void**)&setup);
1002 if(NS_SUCCEEDED(nsres)) {
1003 nsres = nsIWebBrowserSetup_SetProperty(setup, SETUP_ALLOW_JAVASCRIPT,
1004 window->scriptmode == SCRIPTMODE_GECKO);
1005 nsIWebBrowserSetup_Release(setup);
1008 if(NS_FAILED(nsres))
1009 ERR("JavaScript setup failed: %08x\n", nsres);
1012 void release_script_hosts(HTMLWindow *window)
1014 ScriptHost *iter;
1016 while(!list_empty(&window->script_hosts)) {
1017 iter = LIST_ENTRY(list_head(&window->script_hosts), ScriptHost, entry);
1019 release_script_engine(iter);
1020 list_remove(&iter->entry);
1021 iter->window = NULL;
1022 IActiveScript_Release(&iter->IActiveScriptSite_iface);