fltlib: Add a stub dll.
[wine] / dlls / jscript / activex.c
1 /*
2  * Copyright 2009 Jacek Caban for CodeWeavers
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include "config.h"
20 #include "wine/port.h"
21
22 #include "jscript.h"
23 #include "objsafe.h"
24
25 #include "wine/debug.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
28
29 /* Defined as extern in urlmon.idl, but not exported by uuid.lib */
30 const GUID GUID_CUSTOM_CONFIRMOBJECTSAFETY =
31     {0x10200490,0xfa38,0x11d0,{0xac,0x0e,0x00,0xa0,0xc9,0xf,0xff,0xc0}};
32
33 static IInternetHostSecurityManager *get_sec_mgr(script_ctx_t *ctx)
34 {
35     IInternetHostSecurityManager *secmgr;
36     IServiceProvider *sp;
37     HRESULT hres;
38
39     if(!ctx->site)
40         return NULL;
41
42     if(ctx->secmgr)
43         return ctx->secmgr;
44
45     hres = IActiveScriptSite_QueryInterface(ctx->site, &IID_IServiceProvider, (void**)&sp);
46     if(FAILED(hres))
47         return NULL;
48
49     hres = IServiceProvider_QueryService(sp, &SID_SInternetHostSecurityManager, &IID_IInternetHostSecurityManager,
50             (void**)&secmgr);
51     IServiceProvider_Release(sp);
52     if(FAILED(hres))
53         return NULL;
54
55     return ctx->secmgr = secmgr;
56 }
57
58 static IUnknown *create_activex_object(script_ctx_t *ctx, const WCHAR *progid)
59 {
60     IInternetHostSecurityManager *secmgr;
61     struct CONFIRMSAFETY cs;
62     DWORD policy_size;
63     BYTE *bpolicy;
64     IUnknown *obj;
65     DWORD policy;
66     GUID guid;
67     HRESULT hres;
68
69     hres = CLSIDFromProgID(progid, &guid);
70     if(FAILED(hres))
71         return NULL;
72
73     TRACE("GUID %s\n", debugstr_guid(&guid));
74
75     secmgr = get_sec_mgr(ctx);
76     if(!secmgr)
77         return NULL;
78
79     policy = 0;
80     hres = IInternetHostSecurityManager_ProcessUrlAction(secmgr, URLACTION_ACTIVEX_RUN, (BYTE*)&policy, sizeof(policy),
81             (BYTE*)&guid, sizeof(GUID), 0, 0);
82     if(FAILED(hres) || policy != URLPOLICY_ALLOW)
83         return NULL;
84
85     /* FIXME: Use IClassFactoryEx */
86
87     hres = CoCreateInstance(&guid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER, &IID_IUnknown, (void**)&obj);
88     if(FAILED(hres))
89         return NULL;
90
91     cs.clsid = guid;
92     cs.pUnk = obj;
93     cs.dwFlags = 0;
94     hres = IInternetHostSecurityManager_QueryCustomPolicy(secmgr, &GUID_CUSTOM_CONFIRMOBJECTSAFETY, &bpolicy, &policy_size,
95             (BYTE*)&cs, sizeof(cs), 0);
96     if(SUCCEEDED(hres)) {
97         policy = policy_size >= sizeof(DWORD) ? *(DWORD*)bpolicy : URLPOLICY_DISALLOW;
98         CoTaskMemFree(bpolicy);
99     }
100
101     if(FAILED(hres) || policy != URLPOLICY_ALLOW) {
102         IUnknown_Release(obj);
103         return NULL;
104     }
105
106     return obj;
107 }
108
109 static HRESULT ActiveXObject_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
110         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
111 {
112     IDispatch *disp;
113     IUnknown *obj;
114     BSTR progid;
115     HRESULT hres;
116
117     TRACE("\n");
118
119     if(flags != DISPATCH_CONSTRUCT) {
120         FIXME("unsupported flags %x\n", flags);
121         return E_NOTIMPL;
122     }
123
124     if(ctx->safeopt != (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER)) {
125         FIXME("Unsupported safeopt %x\n", ctx->safeopt);
126         return E_NOTIMPL;
127     }
128
129     if(arg_cnt(dp) != 1) {
130         FIXME("unsuported arg_cnt %d\n", arg_cnt(dp));
131         return E_NOTIMPL;
132     }
133
134     hres = to_string(ctx, get_arg(dp,0), ei, &progid);
135     if(FAILED(hres))
136         return hres;
137
138     obj = create_activex_object(ctx, progid);
139     SysFreeString(progid);
140     if(!obj)
141         return throw_generic_error(ctx, ei, IDS_CREATE_OBJ_ERROR, NULL);
142
143     hres = IUnknown_QueryInterface(obj, &IID_IDispatch, (void**)&disp);
144     IUnknown_Release(obj);
145     if(FAILED(hres)) {
146         FIXME("Object does not support IDispatch\n");
147         return E_NOTIMPL;
148     }
149
150     V_VT(retv) = VT_DISPATCH;
151     V_DISPATCH(retv) = disp;
152     return S_OK;
153 }
154
155 HRESULT create_activex_constr(script_ctx_t *ctx, DispatchEx **ret)
156 {
157     DispatchEx *prototype;
158     HRESULT hres;
159
160     static const WCHAR ActiveXObjectW[] = {'A','c','t','i','v','e','X','O','b','j','e','c','t',0};
161
162     hres = create_object(ctx, NULL, &prototype);
163     if(FAILED(hres))
164         return hres;
165
166     hres = create_builtin_function(ctx, ActiveXObject_value, ActiveXObjectW, NULL, PROPF_CONSTR, prototype, ret);
167
168     jsdisp_release(prototype);
169     return hres;
170 }