Add AppDefaults app selection to control panel
[wine/gsoc-2012-control.git] / dlls / vbscript / vbscript.c
blobaf037bd8de6f1f63ab36241e01c6253ca7bd66d1
1 /*
2 * Copyright 2011 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
20 #include <assert.h>
22 #include "vbscript.h"
23 #include "objsafe.h"
25 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
29 #ifdef _WIN64
31 #define CTXARG_T DWORDLONG
32 #define IActiveScriptParseVtbl IActiveScriptParse64Vtbl
34 #else
36 #define CTXARG_T DWORD
37 #define IActiveScriptParseVtbl IActiveScriptParse32Vtbl
39 #endif
41 struct VBScript {
42 IActiveScript IActiveScript_iface;
43 IActiveScriptParse IActiveScriptParse_iface;
44 IObjectSafety IObjectSafety_iface;
46 LONG ref;
48 DWORD safeopt;
49 SCRIPTSTATE state;
50 IActiveScriptSite *site;
51 script_ctx_t *ctx;
52 LONG thread_id;
53 LCID lcid;
56 static void change_state(VBScript *This, SCRIPTSTATE state)
58 if(This->state == state)
59 return;
61 This->state = state;
62 if(This->site)
63 IActiveScriptSite_OnStateChange(This->site, state);
66 static inline BOOL is_started(VBScript *This)
68 return This->state == SCRIPTSTATE_STARTED
69 || This->state == SCRIPTSTATE_CONNECTED
70 || This->state == SCRIPTSTATE_DISCONNECTED;
73 static HRESULT exec_global_code(script_ctx_t *ctx, vbscode_t *code)
75 HRESULT hres;
77 code->global_executed = TRUE;
79 IActiveScriptSite_OnEnterScript(ctx->site);
80 hres = exec_script(ctx, &code->global_code, NULL, NULL, NULL);
81 IActiveScriptSite_OnLeaveScript(ctx->site);
83 return hres;
86 static void exec_queued_code(script_ctx_t *ctx)
88 vbscode_t *iter;
90 LIST_FOR_EACH_ENTRY(iter, &ctx->code_list, vbscode_t, entry) {
91 if(!iter->global_executed)
92 exec_global_code(ctx, iter);
96 static HRESULT set_ctx_site(VBScript *This)
98 HRESULT hres;
100 This->ctx->lcid = This->lcid;
102 hres = init_global(This->ctx);
103 if(FAILED(hres))
104 return hres;
106 IActiveScriptSite_AddRef(This->site);
107 This->ctx->site = This->site;
109 change_state(This, SCRIPTSTATE_INITIALIZED);
110 return S_OK;
113 static void release_script(script_ctx_t *ctx)
115 collect_objects(ctx);
117 release_dynamic_vars(ctx->global_vars);
118 ctx->global_vars = NULL;
120 while(!list_empty(&ctx->named_items)) {
121 named_item_t *iter = LIST_ENTRY(list_head(&ctx->named_items), named_item_t, entry);
123 list_remove(&iter->entry);
124 if(iter->disp)
125 IDispatch_Release(iter->disp);
126 heap_free(iter->name);
127 heap_free(iter);
130 if(ctx->host_global) {
131 IDispatch_Release(ctx->host_global);
132 ctx->host_global = NULL;
135 if(ctx->secmgr) {
136 IInternetHostSecurityManager_Release(ctx->secmgr);
137 ctx->secmgr = NULL;
140 if(ctx->site) {
141 IActiveScriptSite_Release(ctx->site);
142 ctx->site = NULL;
145 if(ctx->err_obj) {
146 IDispatchEx_Release(&ctx->err_obj->IDispatchEx_iface);
147 ctx->err_obj = NULL;
150 if(ctx->global_obj) {
151 IDispatchEx_Release(&ctx->global_obj->IDispatchEx_iface);
152 ctx->global_obj = NULL;
155 if(ctx->script_obj) {
156 IDispatchEx_Release(&ctx->script_obj->IDispatchEx_iface);
157 ctx->script_obj = NULL;
160 vbsheap_free(&ctx->heap);
161 vbsheap_init(&ctx->heap);
164 static void destroy_script(script_ctx_t *ctx)
166 while(!list_empty(&ctx->code_list))
167 release_vbscode(LIST_ENTRY(list_head(&ctx->code_list), vbscode_t, entry));
169 release_script(ctx);
170 heap_free(ctx);
173 static void decrease_state(VBScript *This, SCRIPTSTATE state)
175 switch(This->state) {
176 case SCRIPTSTATE_CONNECTED:
177 change_state(This, SCRIPTSTATE_DISCONNECTED);
178 if(state == SCRIPTSTATE_DISCONNECTED)
179 return;
180 /* FALLTHROUGH */
181 case SCRIPTSTATE_STARTED:
182 case SCRIPTSTATE_DISCONNECTED:
183 if(This->state == SCRIPTSTATE_DISCONNECTED)
184 change_state(This, SCRIPTSTATE_INITIALIZED);
185 if(state == SCRIPTSTATE_INITIALIZED)
186 break;
187 /* FALLTHROUGH */
188 case SCRIPTSTATE_INITIALIZED:
189 case SCRIPTSTATE_UNINITIALIZED:
190 change_state(This, state);
192 if(This->site) {
193 IActiveScriptSite_Release(This->site);
194 This->site = NULL;
197 if(This->ctx)
198 release_script(This->ctx);
200 This->thread_id = 0;
201 break;
202 case SCRIPTSTATE_CLOSED:
203 break;
204 default:
205 assert(0);
209 static inline VBScript *impl_from_IActiveScript(IActiveScript *iface)
211 return CONTAINING_RECORD(iface, VBScript, IActiveScript_iface);
214 static HRESULT WINAPI VBScript_QueryInterface(IActiveScript *iface, REFIID riid, void **ppv)
216 VBScript *This = impl_from_IActiveScript(iface);
218 if(IsEqualGUID(riid, &IID_IUnknown)) {
219 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
220 *ppv = &This->IActiveScript_iface;
221 }else if(IsEqualGUID(riid, &IID_IActiveScript)) {
222 TRACE("(%p)->(IID_IActiveScript %p)\n", This, ppv);
223 *ppv = &This->IActiveScript_iface;
224 }else if(IsEqualGUID(riid, &IID_IActiveScriptParse)) {
225 TRACE("(%p)->(IID_IActiveScriptParse %p)\n", This, ppv);
226 *ppv = &This->IActiveScriptParse_iface;
227 }else if(IsEqualGUID(riid, &IID_IObjectSafety)) {
228 TRACE("(%p)->(IID_IObjectSafety %p)\n", This, ppv);
229 *ppv = &This->IObjectSafety_iface;
230 }else {
231 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
232 *ppv = NULL;
233 return E_NOINTERFACE;
236 IUnknown_AddRef((IUnknown*)*ppv);
237 return S_OK;
240 static ULONG WINAPI VBScript_AddRef(IActiveScript *iface)
242 VBScript *This = impl_from_IActiveScript(iface);
243 LONG ref = InterlockedIncrement(&This->ref);
245 TRACE("(%p) ref=%d\n", This, ref);
247 return ref;
250 static ULONG WINAPI VBScript_Release(IActiveScript *iface)
252 VBScript *This = impl_from_IActiveScript(iface);
253 LONG ref = InterlockedDecrement(&This->ref);
255 TRACE("(%p) ref=%d\n", iface, ref);
257 if(!ref) {
258 if(This->ctx) {
259 decrease_state(This, SCRIPTSTATE_CLOSED);
260 destroy_script(This->ctx);
261 This->ctx = NULL;
263 if(This->site)
264 IActiveScriptSite_Release(This->site);
265 heap_free(This);
268 return ref;
271 static HRESULT WINAPI VBScript_SetScriptSite(IActiveScript *iface, IActiveScriptSite *pass)
273 VBScript *This = impl_from_IActiveScript(iface);
274 LCID lcid;
275 HRESULT hres;
277 TRACE("(%p)->(%p)\n", This, pass);
279 if(!pass)
280 return E_POINTER;
282 if(This->site)
283 return E_UNEXPECTED;
285 if(InterlockedCompareExchange(&This->thread_id, GetCurrentThreadId(), 0))
286 return E_UNEXPECTED;
288 This->site = pass;
289 IActiveScriptSite_AddRef(This->site);
291 hres = IActiveScriptSite_GetLCID(This->site, &lcid);
292 if(hres == S_OK)
293 This->lcid = lcid;
295 return This->ctx ? set_ctx_site(This) : S_OK;
298 static HRESULT WINAPI VBScript_GetScriptSite(IActiveScript *iface, REFIID riid,
299 void **ppvObject)
301 VBScript *This = impl_from_IActiveScript(iface);
302 FIXME("(%p)->()\n", This);
303 return E_NOTIMPL;
306 static HRESULT WINAPI VBScript_SetScriptState(IActiveScript *iface, SCRIPTSTATE ss)
308 VBScript *This = impl_from_IActiveScript(iface);
310 TRACE("(%p)->(%d)\n", This, ss);
312 if(This->thread_id && GetCurrentThreadId() != This->thread_id)
313 return E_UNEXPECTED;
315 if(ss == SCRIPTSTATE_UNINITIALIZED) {
316 if(This->state == SCRIPTSTATE_CLOSED)
317 return E_UNEXPECTED;
319 decrease_state(This, SCRIPTSTATE_UNINITIALIZED);
320 return S_OK;
323 if(!This->ctx)
324 return E_UNEXPECTED;
326 switch(ss) {
327 case SCRIPTSTATE_STARTED:
328 case SCRIPTSTATE_CONNECTED: /* FIXME */
329 if(This->state == SCRIPTSTATE_CLOSED)
330 return E_UNEXPECTED;
332 exec_queued_code(This->ctx);
333 break;
334 case SCRIPTSTATE_INITIALIZED:
335 FIXME("unimplemented SCRIPTSTATE_INITIALIZED\n");
336 return S_OK;
337 default:
338 FIXME("unimplemented state %d\n", ss);
339 return E_NOTIMPL;
342 change_state(This, ss);
343 return S_OK;
346 static HRESULT WINAPI VBScript_GetScriptState(IActiveScript *iface, SCRIPTSTATE *pssState)
348 VBScript *This = impl_from_IActiveScript(iface);
350 TRACE("(%p)->(%p)\n", This, pssState);
352 if(!pssState)
353 return E_POINTER;
355 if(This->thread_id && This->thread_id != GetCurrentThreadId())
356 return E_UNEXPECTED;
358 *pssState = This->state;
359 return S_OK;
362 static HRESULT WINAPI VBScript_Close(IActiveScript *iface)
364 VBScript *This = impl_from_IActiveScript(iface);
366 TRACE("(%p)->()\n", This);
368 if(This->thread_id && This->thread_id != GetCurrentThreadId())
369 return E_UNEXPECTED;
371 decrease_state(This, SCRIPTSTATE_CLOSED);
372 return S_OK;
375 static HRESULT WINAPI VBScript_AddNamedItem(IActiveScript *iface, LPCOLESTR pstrName, DWORD dwFlags)
377 VBScript *This = impl_from_IActiveScript(iface);
378 named_item_t *item;
379 IDispatch *disp = NULL;
380 HRESULT hres;
382 TRACE("(%p)->(%s %x)\n", This, debugstr_w(pstrName), dwFlags);
384 if(This->thread_id != GetCurrentThreadId() || !This->ctx || This->state == SCRIPTSTATE_CLOSED)
385 return E_UNEXPECTED;
387 if(dwFlags & SCRIPTITEM_GLOBALMEMBERS) {
388 IUnknown *unk;
390 hres = IActiveScriptSite_GetItemInfo(This->site, pstrName, SCRIPTINFO_IUNKNOWN, &unk, NULL);
391 if(FAILED(hres)) {
392 WARN("GetItemInfo failed: %08x\n", hres);
393 return hres;
396 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&disp);
397 IUnknown_Release(unk);
398 if(FAILED(hres)) {
399 WARN("object does not implement IDispatch\n");
400 return hres;
403 if(This->ctx->host_global)
404 IDispatch_Release(This->ctx->host_global);
405 IDispatch_AddRef(disp);
406 This->ctx->host_global = disp;
409 item = heap_alloc(sizeof(*item));
410 if(!item) {
411 if(disp)
412 IDispatch_Release(disp);
413 return E_OUTOFMEMORY;
416 item->disp = disp;
417 item->flags = dwFlags;
418 item->name = heap_strdupW(pstrName);
419 if(!item->name) {
420 if(disp)
421 IDispatch_Release(disp);
422 heap_free(item);
423 return E_OUTOFMEMORY;
426 list_add_tail(&This->ctx->named_items, &item->entry);
427 return S_OK;
430 static HRESULT WINAPI VBScript_AddTypeLib(IActiveScript *iface, REFGUID rguidTypeLib,
431 DWORD dwMajor, DWORD dwMinor, DWORD dwFlags)
433 VBScript *This = impl_from_IActiveScript(iface);
434 FIXME("(%p)->()\n", This);
435 return E_NOTIMPL;
438 static HRESULT WINAPI VBScript_GetScriptDispatch(IActiveScript *iface, LPCOLESTR pstrItemName, IDispatch **ppdisp)
440 VBScript *This = impl_from_IActiveScript(iface);
442 TRACE("(%p)->(%p)\n", This, ppdisp);
444 if(!ppdisp)
445 return E_POINTER;
447 if(This->thread_id != GetCurrentThreadId() || !This->ctx || !This->ctx->script_obj) {
448 *ppdisp = NULL;
449 return E_UNEXPECTED;
452 *ppdisp = (IDispatch*)&This->ctx->script_obj->IDispatchEx_iface;
453 IDispatch_AddRef(*ppdisp);
454 return S_OK;
457 static HRESULT WINAPI VBScript_GetCurrentScriptThreadID(IActiveScript *iface,
458 SCRIPTTHREADID *pstridThread)
460 VBScript *This = impl_from_IActiveScript(iface);
461 FIXME("(%p)->()\n", This);
462 return E_NOTIMPL;
465 static HRESULT WINAPI VBScript_GetScriptThreadID(IActiveScript *iface,
466 DWORD dwWin32ThreadId, SCRIPTTHREADID *pstidThread)
468 VBScript *This = impl_from_IActiveScript(iface);
469 FIXME("(%p)->()\n", This);
470 return E_NOTIMPL;
473 static HRESULT WINAPI VBScript_GetScriptThreadState(IActiveScript *iface,
474 SCRIPTTHREADID stidThread, SCRIPTTHREADSTATE *pstsState)
476 VBScript *This = impl_from_IActiveScript(iface);
477 FIXME("(%p)->()\n", This);
478 return E_NOTIMPL;
481 static HRESULT WINAPI VBScript_InterruptScriptThread(IActiveScript *iface,
482 SCRIPTTHREADID stidThread, const EXCEPINFO *pexcepinfo, DWORD dwFlags)
484 VBScript *This = impl_from_IActiveScript(iface);
485 FIXME("(%p)->()\n", This);
486 return E_NOTIMPL;
489 static HRESULT WINAPI VBScript_Clone(IActiveScript *iface, IActiveScript **ppscript)
491 VBScript *This = impl_from_IActiveScript(iface);
492 FIXME("(%p)->()\n", This);
493 return E_NOTIMPL;
496 static const IActiveScriptVtbl VBScriptVtbl = {
497 VBScript_QueryInterface,
498 VBScript_AddRef,
499 VBScript_Release,
500 VBScript_SetScriptSite,
501 VBScript_GetScriptSite,
502 VBScript_SetScriptState,
503 VBScript_GetScriptState,
504 VBScript_Close,
505 VBScript_AddNamedItem,
506 VBScript_AddTypeLib,
507 VBScript_GetScriptDispatch,
508 VBScript_GetCurrentScriptThreadID,
509 VBScript_GetScriptThreadID,
510 VBScript_GetScriptThreadState,
511 VBScript_InterruptScriptThread,
512 VBScript_Clone
515 static inline VBScript *impl_from_IActiveScriptParse(IActiveScriptParse *iface)
517 return CONTAINING_RECORD(iface, VBScript, IActiveScriptParse_iface);
520 static HRESULT WINAPI VBScriptParse_QueryInterface(IActiveScriptParse *iface, REFIID riid, void **ppv)
522 VBScript *This = impl_from_IActiveScriptParse(iface);
523 return IActiveScript_QueryInterface(&This->IActiveScript_iface, riid, ppv);
526 static ULONG WINAPI VBScriptParse_AddRef(IActiveScriptParse *iface)
528 VBScript *This = impl_from_IActiveScriptParse(iface);
529 return IActiveScript_AddRef(&This->IActiveScript_iface);
532 static ULONG WINAPI VBScriptParse_Release(IActiveScriptParse *iface)
534 VBScript *This = impl_from_IActiveScriptParse(iface);
535 return IActiveScript_Release(&This->IActiveScript_iface);
538 static HRESULT WINAPI VBScriptParse_InitNew(IActiveScriptParse *iface)
540 VBScript *This = impl_from_IActiveScriptParse(iface);
541 script_ctx_t *ctx, *old_ctx;
543 TRACE("(%p)\n", This);
545 if(This->ctx)
546 return E_UNEXPECTED;
548 ctx = heap_alloc_zero(sizeof(script_ctx_t));
549 if(!ctx)
550 return E_OUTOFMEMORY;
552 ctx->safeopt = This->safeopt;
553 vbsheap_init(&ctx->heap);
554 list_init(&ctx->objects);
555 list_init(&ctx->code_list);
556 list_init(&ctx->named_items);
558 old_ctx = InterlockedCompareExchangePointer((void**)&This->ctx, ctx, NULL);
559 if(old_ctx) {
560 destroy_script(ctx);
561 return E_UNEXPECTED;
564 return This->site ? set_ctx_site(This) : S_OK;
567 static HRESULT WINAPI VBScriptParse_AddScriptlet(IActiveScriptParse *iface,
568 LPCOLESTR pstrDefaultName, LPCOLESTR pstrCode, LPCOLESTR pstrItemName,
569 LPCOLESTR pstrSubItemName, LPCOLESTR pstrEventName, LPCOLESTR pstrDelimiter,
570 CTXARG_T dwSourceContextCookie, ULONG ulStartingLineNumber, DWORD dwFlags,
571 BSTR *pbstrName, EXCEPINFO *pexcepinfo)
573 VBScript *This = impl_from_IActiveScriptParse(iface);
574 FIXME("(%p)->(%s %s %s %s %s %s %s %u %x %p %p)\n", This, debugstr_w(pstrDefaultName),
575 debugstr_w(pstrCode), debugstr_w(pstrItemName), debugstr_w(pstrSubItemName),
576 debugstr_w(pstrEventName), debugstr_w(pstrDelimiter), wine_dbgstr_longlong(dwSourceContextCookie),
577 ulStartingLineNumber, dwFlags, pbstrName, pexcepinfo);
578 return E_NOTIMPL;
581 static HRESULT WINAPI VBScriptParse_ParseScriptText(IActiveScriptParse *iface,
582 LPCOLESTR pstrCode, LPCOLESTR pstrItemName, IUnknown *punkContext,
583 LPCOLESTR pstrDelimiter, CTXARG_T dwSourceContextCookie, ULONG ulStartingLine,
584 DWORD dwFlags, VARIANT *pvarResult, EXCEPINFO *pexcepinfo)
586 VBScript *This = impl_from_IActiveScriptParse(iface);
587 vbscode_t *code;
588 HRESULT hres;
590 TRACE("(%p)->(%s %s %p %s %s %u %x %p %p)\n", This, debugstr_w(pstrCode),
591 debugstr_w(pstrItemName), punkContext, debugstr_w(pstrDelimiter),
592 wine_dbgstr_longlong(dwSourceContextCookie), ulStartingLine, dwFlags, pvarResult, pexcepinfo);
594 if(This->thread_id != GetCurrentThreadId() || This->state == SCRIPTSTATE_CLOSED)
595 return E_UNEXPECTED;
597 hres = compile_script(This->ctx, pstrCode, &code);
598 if(FAILED(hres))
599 return hres;
601 return is_started(This) ? exec_global_code(This->ctx, code) : S_OK;
604 static const IActiveScriptParseVtbl VBScriptParseVtbl = {
605 VBScriptParse_QueryInterface,
606 VBScriptParse_AddRef,
607 VBScriptParse_Release,
608 VBScriptParse_InitNew,
609 VBScriptParse_AddScriptlet,
610 VBScriptParse_ParseScriptText
613 static inline VBScript *impl_from_IObjectSafety(IObjectSafety *iface)
615 return CONTAINING_RECORD(iface, VBScript, IObjectSafety_iface);
618 static HRESULT WINAPI VBScriptSafety_QueryInterface(IObjectSafety *iface, REFIID riid, void **ppv)
620 VBScript *This = impl_from_IObjectSafety(iface);
621 return IActiveScript_QueryInterface(&This->IActiveScript_iface, riid, ppv);
624 static ULONG WINAPI VBScriptSafety_AddRef(IObjectSafety *iface)
626 VBScript *This = impl_from_IObjectSafety(iface);
627 return IActiveScript_AddRef(&This->IActiveScript_iface);
630 static ULONG WINAPI VBScriptSafety_Release(IObjectSafety *iface)
632 VBScript *This = impl_from_IObjectSafety(iface);
633 return IActiveScript_Release(&This->IActiveScript_iface);
636 #define SUPPORTED_OPTIONS (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER)
638 static HRESULT WINAPI VBScriptSafety_GetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
639 DWORD *pdwSupportedOptions, DWORD *pdwEnabledOptions)
641 VBScript *This = impl_from_IObjectSafety(iface);
643 TRACE("(%p)->(%s %p %p)\n", This, debugstr_guid(riid), pdwSupportedOptions, pdwEnabledOptions);
645 if(!pdwSupportedOptions || !pdwEnabledOptions)
646 return E_POINTER;
648 *pdwSupportedOptions = SUPPORTED_OPTIONS;
649 *pdwEnabledOptions = This->safeopt;
650 return S_OK;
653 static HRESULT WINAPI VBScriptSafety_SetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
654 DWORD dwOptionSetMask, DWORD dwEnabledOptions)
656 VBScript *This = impl_from_IObjectSafety(iface);
658 TRACE("(%p)->(%s %x %x)\n", This, debugstr_guid(riid), dwOptionSetMask, dwEnabledOptions);
660 if(dwOptionSetMask & ~SUPPORTED_OPTIONS)
661 return E_FAIL;
663 This->safeopt = (dwEnabledOptions & dwOptionSetMask) | (This->safeopt & ~dwOptionSetMask) | INTERFACE_USES_DISPEX;
664 return S_OK;
667 static const IObjectSafetyVtbl VBScriptSafetyVtbl = {
668 VBScriptSafety_QueryInterface,
669 VBScriptSafety_AddRef,
670 VBScriptSafety_Release,
671 VBScriptSafety_GetInterfaceSafetyOptions,
672 VBScriptSafety_SetInterfaceSafetyOptions
675 HRESULT WINAPI VBScriptFactory_CreateInstance(IClassFactory *iface, IUnknown *pUnkOuter, REFIID riid, void **ppv)
677 VBScript *ret;
678 HRESULT hres;
680 TRACE("(%p %s %p)\n", pUnkOuter, debugstr_guid(riid), ppv);
682 ret = heap_alloc_zero(sizeof(*ret));
683 if(!ret)
684 return E_OUTOFMEMORY;
686 ret->IActiveScript_iface.lpVtbl = &VBScriptVtbl;
687 ret->IActiveScriptParse_iface.lpVtbl = &VBScriptParseVtbl;
688 ret->IObjectSafety_iface.lpVtbl = &VBScriptSafetyVtbl;
690 ret->ref = 1;
691 ret->state = SCRIPTSTATE_UNINITIALIZED;
692 ret->safeopt = INTERFACE_USES_DISPEX;
694 hres = IActiveScript_QueryInterface(&ret->IActiveScript_iface, riid, ppv);
695 IActiveScript_Release(&ret->IActiveScript_iface);
696 return hres;
699 typedef struct {
700 IServiceProvider IServiceProvider_iface;
702 LONG ref;
704 IServiceProvider *sp;
705 } AXSite;
707 static inline AXSite *impl_from_IServiceProvider(IServiceProvider *iface)
709 return CONTAINING_RECORD(iface, AXSite, IServiceProvider_iface);
712 static HRESULT WINAPI AXSite_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
714 AXSite *This = impl_from_IServiceProvider(iface);
716 if(IsEqualGUID(&IID_IUnknown, riid)) {
717 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
718 *ppv = &This->IServiceProvider_iface;
719 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
720 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
721 *ppv = &This->IServiceProvider_iface;
722 }else {
723 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
724 *ppv = NULL;
725 return E_NOINTERFACE;
728 IUnknown_AddRef((IUnknown*)*ppv);
729 return S_OK;
732 static ULONG WINAPI AXSite_AddRef(IServiceProvider *iface)
734 AXSite *This = impl_from_IServiceProvider(iface);
735 LONG ref = InterlockedIncrement(&This->ref);
737 TRACE("(%p) ref=%d\n", This, ref);
739 return ref;
742 static ULONG WINAPI AXSite_Release(IServiceProvider *iface)
744 AXSite *This = impl_from_IServiceProvider(iface);
745 LONG ref = InterlockedDecrement(&This->ref);
747 TRACE("(%p) ref=%d\n", This, ref);
749 if(!ref)
750 heap_free(This);
752 return ref;
755 static HRESULT WINAPI AXSite_QueryService(IServiceProvider *iface,
756 REFGUID guidService, REFIID riid, void **ppv)
758 AXSite *This = impl_from_IServiceProvider(iface);
760 TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
762 return IServiceProvider_QueryService(This->sp, guidService, riid, ppv);
765 static IServiceProviderVtbl AXSiteVtbl = {
766 AXSite_QueryInterface,
767 AXSite_AddRef,
768 AXSite_Release,
769 AXSite_QueryService
772 IUnknown *create_ax_site(script_ctx_t *ctx)
774 IServiceProvider *sp;
775 AXSite *ret;
776 HRESULT hres;
778 hres = IActiveScriptSite_QueryInterface(ctx->site, &IID_IServiceProvider, (void**)&sp);
779 if(FAILED(hres)) {
780 ERR("Could not get IServiceProvider iface: %08x\n", hres);
781 return NULL;
784 ret = heap_alloc(sizeof(*ret));
785 if(!ret) {
786 IServiceProvider_Release(sp);
787 return NULL;
790 ret->IServiceProvider_iface.lpVtbl = &AXSiteVtbl;
791 ret->ref = 1;
792 ret->sp = sp;
794 return (IUnknown*)&ret->IServiceProvider_iface;