shell32: Add printers CLSID to test, clean up a bit.
[wine/testsucceed.git] / dlls / mshtml / script.c
blob1d21da9a07e03d244242a2d01a3a48d0c4f2635a
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"
31 #include "objsafe.h"
33 #include "wine/debug.h"
35 #include "mshtml_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
39 static const WCHAR windowW[] = {'w','i','n','d','o','w',0};
40 static const WCHAR emptyW[] = {0};
42 static const CLSID CLSID_JScript =
43 {0xf414c260,0x6ac0,0x11cf,{0xb6,0xd1,0x00,0xaa,0x00,0xbb,0xbb,0x58}};
45 struct ScriptHost {
46 const IActiveScriptSiteVtbl *lpIActiveScriptSiteVtbl;
47 const IActiveScriptSiteInterruptPollVtbl *lpIActiveScriptSiteInterruptPollVtbl;
48 const IActiveScriptSiteWindowVtbl *lpIActiveScriptSiteWindowVtbl;
49 const IActiveScriptSiteDebug32Vtbl *lpIActiveScriptSiteDebug32Vtbl;
50 const IServiceProviderVtbl *lpServiceProviderVtbl;
52 LONG ref;
54 IActiveScript *script;
55 IActiveScriptParse *parse;
56 IActiveScriptParseProcedure *parse_proc;
58 SCRIPTSTATE script_state;
60 HTMLWindow *window;
62 GUID guid;
63 struct list entry;
66 #define ACTSCPSITE(x) ((IActiveScriptSite*) &(x)->lpIActiveScriptSiteVtbl)
67 #define ACTSCPPOLL(x) (&(x)->lpIActiveScriptSiteInterruptPollVtbl)
68 #define ACTSCPWIN(x) (&(x)->lpIActiveScriptSiteWindowVtbl)
69 #define ACTSCPDBG32(x) (&(x)->lpIActiveScriptSiteDebug32Vtbl)
71 static void set_script_prop(ScriptHost *script_host, DWORD property, VARIANT *val)
73 IActiveScriptProperty *script_prop;
74 HRESULT hres;
76 hres = IActiveScript_QueryInterface(script_host->script, &IID_IActiveScriptProperty,
77 (void**)&script_prop);
78 if(FAILED(hres)) {
79 WARN("Could not get IActiveScriptProperty iface: %08x\n", hres);
80 return;
83 hres = IActiveScriptProperty_SetProperty(script_prop, property, NULL, val);
84 IActiveScriptProperty_Release(script_prop);
85 if(FAILED(hres))
86 WARN("SetProperty(%x) failed: %08x\n", property, hres);
89 static BOOL init_script_engine(ScriptHost *script_host)
91 IObjectSafety *safety;
92 SCRIPTSTATE state;
93 DWORD supported_opts=0, enabled_opts=0;
94 VARIANT var;
95 HRESULT hres;
97 hres = IActiveScript_QueryInterface(script_host->script, &IID_IActiveScriptParse, (void**)&script_host->parse);
98 if(FAILED(hres)) {
99 WARN("Could not get IActiveScriptHost: %08x\n", hres);
100 return FALSE;
103 hres = IActiveScript_QueryInterface(script_host->script, &IID_IObjectSafety, (void**)&safety);
104 if(FAILED(hres)) {
105 FIXME("Could not get IObjectSafety: %08x\n", hres);
106 return FALSE;
109 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported_opts, &enabled_opts);
110 if(FAILED(hres)) {
111 FIXME("GetInterfaceSafetyOptions failed: %08x\n", hres);
112 }else if(!(supported_opts & INTERFACE_USES_DISPEX)) {
113 FIXME("INTERFACE_USES_DISPEX is not supported\n");
114 }else {
115 hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse,
116 INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER,
117 INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER);
118 if(FAILED(hres))
119 FIXME("SetInterfaceSafetyOptions failed: %08x\n", hres);
122 IObjectSafety_Release(safety);
123 if(FAILED(hres))
124 return FALSE;
126 V_VT(&var) = VT_I4;
127 V_I4(&var) = 1;
128 set_script_prop(script_host, SCRIPTPROP_INVOKEVERSIONING, &var);
130 V_VT(&var) = VT_BOOL;
131 V_BOOL(&var) = VARIANT_TRUE;
132 set_script_prop(script_host, SCRIPTPROP_HACK_TRIDENTEVENTSINK, &var);
134 hres = IActiveScriptParse64_InitNew(script_host->parse);
135 if(FAILED(hres)) {
136 WARN("InitNew failed: %08x\n", hres);
137 return FALSE;
140 hres = IActiveScript_SetScriptSite(script_host->script, ACTSCPSITE(script_host));
141 if(FAILED(hres)) {
142 WARN("SetScriptSite failed: %08x\n", hres);
143 IActiveScript_Close(script_host->script);
144 return FALSE;
147 hres = IActiveScript_GetScriptState(script_host->script, &state);
148 if(FAILED(hres))
149 WARN("GetScriptState failed: %08x\n", hres);
150 else if(state != SCRIPTSTATE_INITIALIZED)
151 FIXME("state = %x\n", state);
153 hres = IActiveScript_SetScriptState(script_host->script, SCRIPTSTATE_STARTED);
154 if(FAILED(hres)) {
155 WARN("Starting script failed: %08x\n", hres);
156 return FALSE;
159 hres = IActiveScript_AddNamedItem(script_host->script, windowW,
160 SCRIPTITEM_ISVISIBLE|SCRIPTITEM_ISSOURCE|SCRIPTITEM_GLOBALMEMBERS);
161 if(SUCCEEDED(hres)) {
162 V_VT(&var) = VT_BOOL;
163 V_BOOL(&var) = VARIANT_TRUE;
164 set_script_prop(script_host, SCRIPTPROP_ABBREVIATE_GLOBALNAME_RESOLUTION, &var);
165 }else {
166 WARN("AddNamedItem failed: %08x\n", hres);
169 hres = IActiveScript_QueryInterface(script_host->script, &IID_IActiveScriptParseProcedure2,
170 (void**)&script_host->parse_proc);
171 if(FAILED(hres)) {
172 /* FIXME: QI for IActiveScriptParseProcedure */
173 WARN("Could not get IActiveScriptParseProcedure iface: %08x\n", hres);
176 return TRUE;
179 static void release_script_engine(ScriptHost *This)
181 if(!This->script)
182 return;
184 switch(This->script_state) {
185 case SCRIPTSTATE_CONNECTED:
186 IActiveScript_SetScriptState(This->script, SCRIPTSTATE_DISCONNECTED);
188 case SCRIPTSTATE_STARTED:
189 case SCRIPTSTATE_DISCONNECTED:
190 case SCRIPTSTATE_INITIALIZED:
191 IActiveScript_Close(This->script);
193 default:
194 if(This->parse_proc) {
195 IUnknown_Release(This->parse_proc);
196 This->parse_proc = NULL;
199 if(This->parse) {
200 IUnknown_Release(This->parse);
201 This->parse = NULL;
205 IActiveScript_Release(This->script);
206 This->script = NULL;
207 This->script_state = SCRIPTSTATE_UNINITIALIZED;
210 void connect_scripts(HTMLWindow *window)
212 ScriptHost *iter;
214 LIST_FOR_EACH_ENTRY(iter, &window->script_hosts, ScriptHost, entry) {
215 if(iter->script_state == SCRIPTSTATE_STARTED)
216 IActiveScript_SetScriptState(iter->script, SCRIPTSTATE_CONNECTED);
220 #define ACTSCPSITE_THIS(iface) DEFINE_THIS(ScriptHost, IActiveScriptSite, iface)
222 static HRESULT WINAPI ActiveScriptSite_QueryInterface(IActiveScriptSite *iface, REFIID riid, void **ppv)
224 ScriptHost *This = ACTSCPSITE_THIS(iface);
226 *ppv = NULL;
228 if(IsEqualGUID(&IID_IUnknown, riid)) {
229 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
230 *ppv = ACTSCPSITE(This);
231 }else if(IsEqualGUID(&IID_IActiveScriptSite, riid)) {
232 TRACE("(%p)->(IID_IActiveScriptSite %p)\n", This, ppv);
233 *ppv = ACTSCPSITE(This);
234 }else if(IsEqualGUID(&IID_IActiveScriptSiteInterruptPoll, riid)) {
235 TRACE("(%p)->(IID_IActiveScriptSiteInterruprtPoll %p)\n", This, ppv);
236 *ppv = ACTSCPPOLL(This);
237 }else if(IsEqualGUID(&IID_IActiveScriptSiteWindow, riid)) {
238 TRACE("(%p)->(IID_IActiveScriptSiteWindow %p)\n", This, ppv);
239 *ppv = ACTSCPWIN(This);
240 }else if(IsEqualGUID(&IID_IActiveScriptSiteDebug32, riid)) {
241 TRACE("(%p)->(IID_IActiveScriptSiteDebug32 %p)\n", This, ppv);
242 *ppv = ACTSCPDBG32(This);
243 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
244 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
245 *ppv = SERVPROV(This);
246 }else if(IsEqualGUID(&IID_ICanHandleException, riid)) {
247 TRACE("(%p)->(IID_ICanHandleException not supported %p)\n", This, ppv);
248 return E_NOINTERFACE;
249 }else {
250 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
251 return E_NOINTERFACE;
254 IUnknown_AddRef((IUnknown*)*ppv);
255 return S_OK;
258 static ULONG WINAPI ActiveScriptSite_AddRef(IActiveScriptSite *iface)
260 ScriptHost *This = ACTSCPSITE_THIS(iface);
261 LONG ref = InterlockedIncrement(&This->ref);
263 TRACE("(%p) ref=%d\n", This, ref);
265 return ref;
268 static ULONG WINAPI ActiveScriptSite_Release(IActiveScriptSite *iface)
270 ScriptHost *This = ACTSCPSITE_THIS(iface);
271 LONG ref = InterlockedDecrement(&This->ref);
273 TRACE("(%p) ref=%d\n", This, ref);
275 if(!ref) {
276 release_script_engine(This);
277 if(This->window)
278 list_remove(&This->entry);
279 heap_free(This);
282 return ref;
285 static HRESULT WINAPI ActiveScriptSite_GetLCID(IActiveScriptSite *iface, LCID *plcid)
287 ScriptHost *This = ACTSCPSITE_THIS(iface);
289 TRACE("(%p)->(%p)\n", This, plcid);
291 *plcid = GetUserDefaultLCID();
292 return S_OK;
295 static HRESULT WINAPI ActiveScriptSite_GetItemInfo(IActiveScriptSite *iface, LPCOLESTR pstrName,
296 DWORD dwReturnMask, IUnknown **ppiunkItem, ITypeInfo **ppti)
298 ScriptHost *This = ACTSCPSITE_THIS(iface);
300 TRACE("(%p)->(%s %x %p %p)\n", This, debugstr_w(pstrName), dwReturnMask, ppiunkItem, ppti);
302 if(dwReturnMask != SCRIPTINFO_IUNKNOWN) {
303 FIXME("Unsupported mask %x\n", dwReturnMask);
304 return E_NOTIMPL;
307 *ppiunkItem = NULL;
309 if(strcmpW(pstrName, windowW))
310 return DISP_E_MEMBERNOTFOUND;
312 if(!This->window)
313 return E_FAIL;
315 /* FIXME: Return proxy object */
316 *ppiunkItem = (IUnknown*)HTMLWINDOW2(This->window);
317 IUnknown_AddRef(*ppiunkItem);
319 return S_OK;
322 static HRESULT WINAPI ActiveScriptSite_GetDocVersionString(IActiveScriptSite *iface, BSTR *pbstrVersion)
324 ScriptHost *This = ACTSCPSITE_THIS(iface);
325 FIXME("(%p)->(%p)\n", This, pbstrVersion);
326 return E_NOTIMPL;
329 static HRESULT WINAPI ActiveScriptSite_OnScriptTerminate(IActiveScriptSite *iface,
330 const VARIANT *pvarResult, const EXCEPINFO *pexcepinfo)
332 ScriptHost *This = ACTSCPSITE_THIS(iface);
333 FIXME("(%p)->(%p %p)\n", This, pvarResult, pexcepinfo);
334 return E_NOTIMPL;
337 static HRESULT WINAPI ActiveScriptSite_OnStateChange(IActiveScriptSite *iface, SCRIPTSTATE ssScriptState)
339 ScriptHost *This = ACTSCPSITE_THIS(iface);
341 TRACE("(%p)->(%x)\n", This, ssScriptState);
343 This->script_state = ssScriptState;
344 return S_OK;
347 static HRESULT WINAPI ActiveScriptSite_OnScriptError(IActiveScriptSite *iface, IActiveScriptError *pscripterror)
349 ScriptHost *This = ACTSCPSITE_THIS(iface);
350 FIXME("(%p)->(%p)\n", This, pscripterror);
351 return E_NOTIMPL;
354 static HRESULT WINAPI ActiveScriptSite_OnEnterScript(IActiveScriptSite *iface)
356 ScriptHost *This = ACTSCPSITE_THIS(iface);
358 TRACE("(%p)->()\n", This);
360 return S_OK;
363 static HRESULT WINAPI ActiveScriptSite_OnLeaveScript(IActiveScriptSite *iface)
365 ScriptHost *This = ACTSCPSITE_THIS(iface);
367 TRACE("(%p)->()\n", This);
369 return S_OK;
372 #undef ACTSCPSITE_THIS
374 static const IActiveScriptSiteVtbl ActiveScriptSiteVtbl = {
375 ActiveScriptSite_QueryInterface,
376 ActiveScriptSite_AddRef,
377 ActiveScriptSite_Release,
378 ActiveScriptSite_GetLCID,
379 ActiveScriptSite_GetItemInfo,
380 ActiveScriptSite_GetDocVersionString,
381 ActiveScriptSite_OnScriptTerminate,
382 ActiveScriptSite_OnStateChange,
383 ActiveScriptSite_OnScriptError,
384 ActiveScriptSite_OnEnterScript,
385 ActiveScriptSite_OnLeaveScript
388 #define ACTSCPPOLL_THIS(iface) DEFINE_THIS(ScriptHost, IActiveScriptSiteInterruptPoll, iface)
390 static HRESULT WINAPI ActiveScriptSiteInterruptPoll_QueryInterface(IActiveScriptSiteInterruptPoll *iface,
391 REFIID riid, void **ppv)
393 ScriptHost *This = ACTSCPPOLL_THIS(iface);
394 return IActiveScriptSite_QueryInterface(ACTSCPSITE(This), riid, ppv);
397 static ULONG WINAPI ActiveScriptSiteInterruptPoll_AddRef(IActiveScriptSiteInterruptPoll *iface)
399 ScriptHost *This = ACTSCPPOLL_THIS(iface);
400 return IActiveScriptSite_AddRef(ACTSCPSITE(This));
403 static ULONG WINAPI ActiveScriptSiteInterruptPoll_Release(IActiveScriptSiteInterruptPoll *iface)
405 ScriptHost *This = ACTSCPPOLL_THIS(iface);
406 return IActiveScriptSite_Release(ACTSCPSITE(This));
409 static HRESULT WINAPI ActiveScriptSiteInterruptPoll_QueryContinue(IActiveScriptSiteInterruptPoll *iface)
411 ScriptHost *This = ACTSCPPOLL_THIS(iface);
413 TRACE("(%p)\n", This);
415 return S_OK;
418 #undef ACTSCPPOLL_THIS
420 static const IActiveScriptSiteInterruptPollVtbl ActiveScriptSiteInterruptPollVtbl = {
421 ActiveScriptSiteInterruptPoll_QueryInterface,
422 ActiveScriptSiteInterruptPoll_AddRef,
423 ActiveScriptSiteInterruptPoll_Release,
424 ActiveScriptSiteInterruptPoll_QueryContinue
427 #define ACTSCPWIN_THIS(iface) DEFINE_THIS(ScriptHost, IActiveScriptSiteWindow, iface)
429 static HRESULT WINAPI ActiveScriptSiteWindow_QueryInterface(IActiveScriptSiteWindow *iface,
430 REFIID riid, void **ppv)
432 ScriptHost *This = ACTSCPWIN_THIS(iface);
433 return IActiveScriptSite_QueryInterface(ACTSCPSITE(This), riid, ppv);
436 static ULONG WINAPI ActiveScriptSiteWindow_AddRef(IActiveScriptSiteWindow *iface)
438 ScriptHost *This = ACTSCPWIN_THIS(iface);
439 return IActiveScriptSite_AddRef(ACTSCPSITE(This));
442 static ULONG WINAPI ActiveScriptSiteWindow_Release(IActiveScriptSiteWindow *iface)
444 ScriptHost *This = ACTSCPWIN_THIS(iface);
445 return IActiveScriptSite_Release(ACTSCPSITE(This));
448 static HRESULT WINAPI ActiveScriptSiteWindow_GetWindow(IActiveScriptSiteWindow *iface, HWND *phwnd)
450 ScriptHost *This = ACTSCPWIN_THIS(iface);
451 FIXME("(%p)->(%p)\n", This, phwnd);
452 return E_NOTIMPL;
455 static HRESULT WINAPI ActiveScriptSiteWindow_EnableModeless(IActiveScriptSiteWindow *iface, BOOL fEnable)
457 ScriptHost *This = ACTSCPWIN_THIS(iface);
458 FIXME("(%p)->(%x)\n", This, fEnable);
459 return E_NOTIMPL;
462 #undef ACTSCPWIN_THIS
464 static const IActiveScriptSiteWindowVtbl ActiveScriptSiteWindowVtbl = {
465 ActiveScriptSiteWindow_QueryInterface,
466 ActiveScriptSiteWindow_AddRef,
467 ActiveScriptSiteWindow_Release,
468 ActiveScriptSiteWindow_GetWindow,
469 ActiveScriptSiteWindow_EnableModeless
472 #define ACTSCPDBG32_THIS(iface) DEFINE_THIS(ScriptHost, IActiveScriptSiteDebug32, iface)
474 static HRESULT WINAPI ActiveScriptSiteDebug32_QueryInterface(IActiveScriptSiteDebug32 *iface,
475 REFIID riid, void **ppv)
477 ScriptHost *This = ACTSCPDBG32_THIS(iface);
478 return IActiveScriptSite_QueryInterface(ACTSCPSITE(This), riid, ppv);
481 static ULONG WINAPI ActiveScriptSiteDebug32_AddRef(IActiveScriptSiteDebug32 *iface)
483 ScriptHost *This = ACTSCPDBG32_THIS(iface);
484 return IActiveScriptSite_AddRef(ACTSCPSITE(This));
487 static ULONG WINAPI ActiveScriptSiteDebug32_Release(IActiveScriptSiteDebug32 *iface)
489 ScriptHost *This = ACTSCPDBG32_THIS(iface);
490 return IActiveScriptSite_Release(ACTSCPSITE(This));
493 static HRESULT WINAPI ActiveScriptSiteDebug32_GetDocumentContextFromPosition(IActiveScriptSiteDebug32 *iface,
494 DWORD dwSourceContext, ULONG uCharacterOffset, ULONG uNumChars, IDebugDocumentContext **ppsc)
496 ScriptHost *This = ACTSCPDBG32_THIS(iface);
497 FIXME("(%p)->(%x %u %u %p)\n", This, dwSourceContext, uCharacterOffset, uNumChars, ppsc);
498 return E_NOTIMPL;
501 static HRESULT WINAPI ActiveScriptSiteDebug32_GetApplication(IActiveScriptSiteDebug32 *iface, IDebugApplication32 **ppda)
503 ScriptHost *This = ACTSCPDBG32_THIS(iface);
504 FIXME("(%p)->(%p)\n", This, ppda);
505 return E_NOTIMPL;
508 static HRESULT WINAPI ActiveScriptSiteDebug32_GetRootApplicationNode(IActiveScriptSiteDebug32 *iface,
509 IDebugApplicationNode **ppdanRoot)
511 ScriptHost *This = ACTSCPDBG32_THIS(iface);
512 FIXME("(%p)->(%p)\n", This, ppdanRoot);
513 return E_NOTIMPL;
516 static HRESULT WINAPI ActiveScriptSiteDebug32_OnScriptErrorDebug(IActiveScriptSiteDebug32 *iface,
517 IActiveScriptErrorDebug *pErrorDebug, BOOL *pfEnterDebugger, BOOL *pfCallOnScriptErrorWhenContinuing)
519 ScriptHost *This = ACTSCPDBG32_THIS(iface);
520 FIXME("(%p)->(%p %p %p)\n", This, pErrorDebug, pfEnterDebugger, pfCallOnScriptErrorWhenContinuing);
521 return E_NOTIMPL;
524 #undef ACTSCPDBG32_THIS
526 static const IActiveScriptSiteDebug32Vtbl ActiveScriptSiteDebug32Vtbl = {
527 ActiveScriptSiteDebug32_QueryInterface,
528 ActiveScriptSiteDebug32_AddRef,
529 ActiveScriptSiteDebug32_Release,
530 ActiveScriptSiteDebug32_GetDocumentContextFromPosition,
531 ActiveScriptSiteDebug32_GetApplication,
532 ActiveScriptSiteDebug32_GetRootApplicationNode,
533 ActiveScriptSiteDebug32_OnScriptErrorDebug
536 #define SERVPROV_THIS(iface) DEFINE_THIS(ScriptHost, ServiceProvider, iface)
538 static HRESULT WINAPI ASServiceProvider_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
540 ScriptHost *This = SERVPROV_THIS(iface);
541 return IActiveScriptSite_QueryInterface(ACTSCPSITE(This), riid, ppv);
544 static ULONG WINAPI ASServiceProvider_AddRef(IServiceProvider *iface)
546 ScriptHost *This = SERVPROV_THIS(iface);
547 return IActiveScriptSite_AddRef(ACTSCPSITE(This));
550 static ULONG WINAPI ASServiceProvider_Release(IServiceProvider *iface)
552 ScriptHost *This = SERVPROV_THIS(iface);
553 return IActiveScriptSite_Release(ACTSCPSITE(This));
556 static HRESULT WINAPI ASServiceProvider_QueryService(IServiceProvider *iface, REFGUID guidService,
557 REFIID riid, void **ppv)
559 ScriptHost *This = SERVPROV_THIS(iface);
561 if(IsEqualGUID(&SID_SInternetHostSecurityManager, guidService)) {
562 TRACE("(%p)->(SID_SInternetHostSecurityManager)\n", This);
564 if(!This->window || !This->window->doc)
565 return E_NOINTERFACE;
567 return IInternetHostSecurityManager_QueryInterface(HOSTSECMGR(This->window->doc), riid, ppv);
570 FIXME("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
571 return E_NOINTERFACE;
574 #undef SERVPROV_THIS
576 static const IServiceProviderVtbl ASServiceProviderVtbl = {
577 ASServiceProvider_QueryInterface,
578 ASServiceProvider_AddRef,
579 ASServiceProvider_Release,
580 ASServiceProvider_QueryService
583 static ScriptHost *create_script_host(HTMLWindow *window, const GUID *guid)
585 ScriptHost *ret;
586 HRESULT hres;
588 ret = heap_alloc_zero(sizeof(*ret));
589 ret->lpIActiveScriptSiteVtbl = &ActiveScriptSiteVtbl;
590 ret->lpIActiveScriptSiteInterruptPollVtbl = &ActiveScriptSiteInterruptPollVtbl;
591 ret->lpIActiveScriptSiteWindowVtbl = &ActiveScriptSiteWindowVtbl;
592 ret->lpIActiveScriptSiteDebug32Vtbl = &ActiveScriptSiteDebug32Vtbl;
593 ret->lpServiceProviderVtbl = &ASServiceProviderVtbl;
594 ret->ref = 1;
595 ret->window = window;
596 ret->script_state = SCRIPTSTATE_UNINITIALIZED;
598 ret->guid = *guid;
599 list_add_tail(&window->script_hosts, &ret->entry);
601 hres = CoCreateInstance(&ret->guid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
602 &IID_IActiveScript, (void**)&ret->script);
603 if(FAILED(hres))
604 WARN("Could not load script engine: %08x\n", hres);
605 else if(!init_script_engine(ret))
606 release_script_engine(ret);
608 return ret;
611 static void parse_text(ScriptHost *script_host, LPCWSTR text)
613 EXCEPINFO excepinfo;
614 VARIANT var;
615 HRESULT hres;
617 static const WCHAR script_endW[] = {'<','/','S','C','R','I','P','T','>',0};
619 TRACE("%s\n", debugstr_w(text));
621 VariantInit(&var);
622 memset(&excepinfo, 0, sizeof(excepinfo));
623 TRACE(">>>\n");
624 hres = IActiveScriptParse64_ParseScriptText(script_host->parse, text, windowW, NULL, script_endW,
625 0, 0, SCRIPTTEXT_ISVISIBLE|SCRIPTTEXT_HOSTMANAGESSOURCE,
626 &var, &excepinfo);
627 if(SUCCEEDED(hres))
628 TRACE("<<<\n");
629 else
630 WARN("<<< %08x\n", hres);
634 static void parse_extern_script(ScriptHost *script_host, LPCWSTR src)
636 IMoniker *mon;
637 char *buf;
638 WCHAR *text;
639 DWORD len, size=0;
640 HRESULT hres;
642 static const WCHAR wine_schemaW[] = {'w','i','n','e',':'};
644 if(strlenW(src) > sizeof(wine_schemaW)/sizeof(WCHAR) && !memcmp(src, wine_schemaW, sizeof(wine_schemaW)))
645 src += sizeof(wine_schemaW)/sizeof(WCHAR);
647 hres = CreateURLMoniker(NULL, src, &mon);
648 if(FAILED(hres))
649 return;
651 hres = bind_mon_to_buffer(script_host->window->doc, mon, (void**)&buf, &size);
652 IMoniker_Release(mon);
653 if(FAILED(hres))
654 return;
656 len = MultiByteToWideChar(CP_ACP, 0, buf, size, NULL, 0);
657 text = heap_alloc((len+1)*sizeof(WCHAR));
658 MultiByteToWideChar(CP_ACP, 0, buf, size, text, len);
659 heap_free(buf);
660 text[len] = 0;
662 parse_text(script_host, text);
664 heap_free(text);
667 static void parse_inline_script(ScriptHost *script_host, nsIDOMHTMLScriptElement *nsscript)
669 const PRUnichar *text;
670 nsAString text_str;
671 nsresult nsres;
673 nsAString_Init(&text_str, NULL);
675 nsres = nsIDOMHTMLScriptElement_GetText(nsscript, &text_str);
677 if(NS_SUCCEEDED(nsres)) {
678 nsAString_GetData(&text_str, &text);
679 parse_text(script_host, text);
680 }else {
681 ERR("GetText failed: %08x\n", nsres);
684 nsAString_Finish(&text_str);
687 static void parse_script_elem(ScriptHost *script_host, nsIDOMHTMLScriptElement *nsscript)
689 const PRUnichar *src;
690 nsAString src_str;
691 nsresult nsres;
693 nsAString_Init(&src_str, NULL);
695 nsres = nsIDOMHTMLScriptElement_GetSrc(nsscript, &src_str);
696 nsAString_GetData(&src_str, &src);
698 if(NS_FAILED(nsres))
699 ERR("GetSrc failed: %08x\n", nsres);
700 else if(*src)
701 parse_extern_script(script_host, src);
702 else
703 parse_inline_script(script_host, nsscript);
705 nsAString_Finish(&src_str);
708 static BOOL get_guid_from_type(LPCWSTR type, GUID *guid)
710 const WCHAR text_javascriptW[] =
711 {'t','e','x','t','/','j','a','v','a','s','c','r','i','p','t',0};
713 /* FIXME: Handle more types */
714 if(!strcmpiW(type, text_javascriptW)) {
715 *guid = CLSID_JScript;
716 }else {
717 FIXME("Unknown type %s\n", debugstr_w(type));
718 return FALSE;
721 return TRUE;
724 static BOOL get_guid_from_language(LPCWSTR type, GUID *guid)
726 HRESULT hres;
728 hres = CLSIDFromProgID(type, guid);
729 if(FAILED(hres))
730 return FALSE;
732 /* FIXME: Check CATID_ActiveScriptParse */
734 return TRUE;
737 static BOOL get_script_guid(nsIDOMHTMLScriptElement *nsscript, GUID *guid)
739 nsAString attr_str, val_str;
740 BOOL ret = FALSE;
741 nsresult nsres;
743 static const PRUnichar languageW[] = {'l','a','n','g','u','a','g','e',0};
745 nsAString_Init(&val_str, NULL);
747 nsres = nsIDOMHTMLScriptElement_GetType(nsscript, &val_str);
748 if(NS_SUCCEEDED(nsres)) {
749 const PRUnichar *type;
751 nsAString_GetData(&val_str, &type);
752 if(*type) {
753 ret = get_guid_from_type(type, guid);
754 nsAString_Finish(&val_str);
755 return ret;
757 }else {
758 ERR("GetType failed: %08x\n", nsres);
761 nsAString_InitDepend(&attr_str, languageW);
762 nsres = nsIDOMHTMLScriptElement_GetAttribute(nsscript, &attr_str, &val_str);
763 nsAString_Finish(&attr_str);
764 if(NS_SUCCEEDED(nsres)) {
765 const PRUnichar *language;
767 nsAString_GetData(&val_str, &language);
769 if(*language) {
770 ret = get_guid_from_language(language, guid);
771 }else {
772 *guid = CLSID_JScript;
773 ret = TRUE;
775 }else {
776 ERR("GetAttribute(language) failed: %08x\n", nsres);
779 nsAString_Finish(&val_str);
781 return ret;
784 static ScriptHost *get_script_host(HTMLWindow *window, const GUID *guid)
786 ScriptHost *iter;
788 if(IsEqualGUID(&CLSID_JScript, guid) && window->scriptmode != SCRIPTMODE_ACTIVESCRIPT) {
789 TRACE("Ignoring JScript\n");
790 return NULL;
793 LIST_FOR_EACH_ENTRY(iter, &window->script_hosts, ScriptHost, entry) {
794 if(IsEqualGUID(guid, &iter->guid))
795 return iter;
798 return create_script_host(window, guid);
801 void doc_insert_script(HTMLWindow *window, nsIDOMHTMLScriptElement *nsscript)
803 ScriptHost *script_host;
804 GUID guid;
806 if(!get_script_guid(nsscript, &guid)) {
807 WARN("Could not find script GUID\n");
808 return;
811 script_host = get_script_host(window, &guid);
812 if(!script_host)
813 return;
815 if(script_host->parse)
816 parse_script_elem(script_host, nsscript);
819 IDispatch *script_parse_event(HTMLWindow *window, LPCWSTR text)
821 ScriptHost *script_host;
822 GUID guid = CLSID_JScript;
823 const WCHAR *ptr;
824 IDispatch *disp;
825 HRESULT hres;
827 static const WCHAR delimiterW[] = {'\"',0};
829 for(ptr = text; isalnumW(*ptr); ptr++);
830 if(*ptr == ':') {
831 LPWSTR language;
832 BOOL b;
834 language = heap_alloc((ptr-text+1)*sizeof(WCHAR));
835 memcpy(language, text, (ptr-text)*sizeof(WCHAR));
836 language[ptr-text] = 0;
838 b = get_guid_from_language(language, &guid);
840 heap_free(language);
842 if(!b) {
843 WARN("Could not find language\n");
844 return NULL;
847 ptr++;
848 }else {
849 ptr = text;
852 script_host = get_script_host(window, &guid);
853 if(!script_host || !script_host->parse_proc)
854 return NULL;
856 hres = IActiveScriptParseProcedure64_ParseProcedureText(script_host->parse_proc, ptr, NULL, emptyW,
857 NULL, NULL, delimiterW, 0 /* FIXME */, 0,
858 SCRIPTPROC_HOSTMANAGESSOURCE|SCRIPTPROC_IMPLICIT_THIS|SCRIPTPROC_IMPLICIT_PARENTS, &disp);
859 if(FAILED(hres)) {
860 WARN("ParseProcedureText failed: %08x\n", hres);
861 return NULL;
864 TRACE("ret %p\n", disp);
865 return disp;
868 IDispatch *get_script_disp(ScriptHost *script_host)
870 IDispatch *disp;
871 HRESULT hres;
873 if(!script_host->script)
874 return NULL;
876 hres = IActiveScript_GetScriptDispatch(script_host->script, windowW, &disp);
877 if(FAILED(hres))
878 return NULL;
880 return disp;
883 BOOL find_global_prop(HTMLWindow *window, BSTR name, DWORD flags, ScriptHost **ret_host, DISPID *ret_id)
885 IDispatchEx *dispex;
886 IDispatch *disp;
887 ScriptHost *iter;
888 HRESULT hres;
890 LIST_FOR_EACH_ENTRY(iter, &window->script_hosts, ScriptHost, entry) {
891 disp = get_script_disp(iter);
892 if(!disp)
893 continue;
895 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
896 if(SUCCEEDED(hres)) {
897 hres = IDispatchEx_GetDispID(dispex, name, flags & (~fdexNameEnsure), ret_id);
898 IDispatchEx_Release(dispex);
899 }else {
900 FIXME("No IDispatchEx\n");
901 hres = E_NOTIMPL;
904 IDispatch_Release(disp);
905 if(SUCCEEDED(hres)) {
906 *ret_host = iter;
907 return TRUE;
911 return FALSE;
914 static BOOL is_jscript_available(void)
916 static BOOL available, checked;
918 if(!checked) {
919 IUnknown *unk;
920 HRESULT hres = CoGetClassObject(&CLSID_JScript, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void**)&unk);
922 if(SUCCEEDED(hres)) {
923 available = TRUE;
924 IUnknown_Release(unk);
925 }else {
926 available = FALSE;
928 checked = TRUE;
931 return available;
934 void set_script_mode(HTMLWindow *window, SCRIPTMODE mode)
936 nsIWebBrowserSetup *setup;
937 nsresult nsres;
939 if(mode == SCRIPTMODE_ACTIVESCRIPT && !is_jscript_available()) {
940 TRACE("jscript.dll not available\n");
941 window->scriptmode = SCRIPTMODE_GECKO;
942 return;
945 window->scriptmode = mode;
947 if(!window->doc_obj->nscontainer || !window->doc_obj->nscontainer->webbrowser)
948 return;
950 nsres = nsIWebBrowser_QueryInterface(window->doc_obj->nscontainer->webbrowser,
951 &IID_nsIWebBrowserSetup, (void**)&setup);
952 if(NS_SUCCEEDED(nsres)) {
953 nsres = nsIWebBrowserSetup_SetProperty(setup, SETUP_ALLOW_JAVASCRIPT,
954 window->scriptmode == SCRIPTMODE_GECKO);
955 nsIWebBrowserSetup_Release(setup);
958 if(NS_FAILED(nsres))
959 ERR("JavaScript setup failed: %08x\n", nsres);
962 void release_script_hosts(HTMLWindow *window)
964 ScriptHost *iter;
966 while(!list_empty(&window->script_hosts)) {
967 iter = LIST_ENTRY(list_head(&window->script_hosts), ScriptHost, entry);
969 release_script_engine(iter);
970 list_remove(&iter->entry);
971 iter->window = NULL;
972 IActiveScript_Release(ACTSCPSITE(iter));