jscript: Assorted spelling fixes.
[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 #include "mshtmhst.h"
25
26 #include "wine/debug.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
29
30 /* Defined as extern in urlmon.idl, but not exported by uuid.lib */
31 const GUID GUID_CUSTOM_CONFIRMOBJECTSAFETY =
32     {0x10200490,0xfa38,0x11d0,{0xac,0x0e,0x00,0xa0,0xc9,0xf,0xff,0xc0}};
33
34 static IInternetHostSecurityManager *get_sec_mgr(script_ctx_t *ctx)
35 {
36     IInternetHostSecurityManager *secmgr;
37     IServiceProvider *sp;
38     HRESULT hres;
39
40     if(!ctx->site)
41         return NULL;
42
43     if(ctx->secmgr)
44         return ctx->secmgr;
45
46     hres = IActiveScriptSite_QueryInterface(ctx->site, &IID_IServiceProvider, (void**)&sp);
47     if(FAILED(hres))
48         return NULL;
49
50     hres = IServiceProvider_QueryService(sp, &SID_SInternetHostSecurityManager, &IID_IInternetHostSecurityManager,
51             (void**)&secmgr);
52     IServiceProvider_Release(sp);
53     if(FAILED(hres))
54         return NULL;
55
56     return ctx->secmgr = secmgr;
57 }
58
59 static IUnknown *create_activex_object(script_ctx_t *ctx, const WCHAR *progid)
60 {
61     IInternetHostSecurityManager *secmgr = NULL;
62     IObjectWithSite *obj_site;
63     struct CONFIRMSAFETY cs;
64     IClassFactoryEx *cfex;
65     IClassFactory *cf;
66     DWORD policy_size;
67     BYTE *bpolicy;
68     IUnknown *obj;
69     DWORD policy;
70     GUID guid;
71     HRESULT hres;
72
73     hres = CLSIDFromProgID(progid, &guid);
74     if(FAILED(hres))
75         return NULL;
76
77     TRACE("GUID %s\n", debugstr_guid(&guid));
78
79     if(ctx->safeopt & INTERFACE_USES_SECURITY_MANAGER) {
80         secmgr = get_sec_mgr(ctx);
81         if(!secmgr)
82             return NULL;
83
84         policy = 0;
85         hres = IInternetHostSecurityManager_ProcessUrlAction(secmgr, URLACTION_ACTIVEX_RUN,
86                 (BYTE*)&policy, sizeof(policy), (BYTE*)&guid, sizeof(GUID), 0, 0);
87         if(FAILED(hres) || policy != URLPOLICY_ALLOW)
88             return NULL;
89     }
90
91     hres = CoGetClassObject(&guid, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, NULL, &IID_IClassFactory, (void**)&cf);
92     if(FAILED(hres))
93         return NULL;
94
95     hres = IClassFactory_QueryInterface(cf, &IID_IClassFactoryEx, (void**)&cfex);
96     if(SUCCEEDED(hres)) {
97         FIXME("Use IClassFactoryEx\n");
98         IClassFactoryEx_Release(cfex);
99     }
100
101     hres = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (void**)&obj);
102     if(FAILED(hres))
103         return NULL;
104
105     if(secmgr) {
106         cs.clsid = guid;
107         cs.pUnk = obj;
108         cs.dwFlags = 0;
109         hres = IInternetHostSecurityManager_QueryCustomPolicy(secmgr, &GUID_CUSTOM_CONFIRMOBJECTSAFETY,
110                 &bpolicy, &policy_size, (BYTE*)&cs, sizeof(cs), 0);
111         if(SUCCEEDED(hres)) {
112             policy = policy_size >= sizeof(DWORD) ? *(DWORD*)bpolicy : URLPOLICY_DISALLOW;
113             CoTaskMemFree(bpolicy);
114         }
115
116         if(FAILED(hres) || policy != URLPOLICY_ALLOW) {
117             IUnknown_Release(obj);
118             return NULL;
119         }
120     }
121
122     hres = IUnknown_QueryInterface(obj, &IID_IObjectWithSite, (void**)&obj_site);
123     if(SUCCEEDED(hres)) {
124         IUnknown *ax_site;
125
126         ax_site = create_ax_site(ctx);
127         if(ax_site) {
128             hres = IObjectWithSite_SetSite(obj_site, ax_site);
129             IUnknown_Release(ax_site);
130         }
131         IObjectWithSite_Release(obj_site);
132         if(!ax_site || FAILED(hres)) {
133             IUnknown_Release(obj);
134             return NULL;
135         }
136     }
137
138     return obj;
139 }
140
141 static HRESULT ActiveXObject_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
142         VARIANT *retv, jsexcept_t *ei)
143 {
144     IDispatch *disp;
145     IUnknown *obj;
146     BSTR progid;
147     HRESULT hres;
148
149     TRACE("\n");
150
151     if(flags != DISPATCH_CONSTRUCT) {
152         FIXME("unsupported flags %x\n", flags);
153         return E_NOTIMPL;
154     }
155
156     if(ctx->safeopt != (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER)
157         && ctx->safeopt != INTERFACE_USES_DISPEX) {
158         FIXME("Unsupported safeopt %x\n", ctx->safeopt);
159         return E_NOTIMPL;
160     }
161
162     if(arg_cnt(dp) != 1) {
163         FIXME("unsupported arg_cnt %d\n", arg_cnt(dp));
164         return E_NOTIMPL;
165     }
166
167     hres = to_string(ctx, get_arg(dp,0), ei, &progid);
168     if(FAILED(hres))
169         return hres;
170
171     obj = create_activex_object(ctx, progid);
172     SysFreeString(progid);
173     if(!obj)
174         return throw_generic_error(ctx, ei, JS_E_CANNOT_CREATE_OBJ, NULL);
175
176     hres = IUnknown_QueryInterface(obj, &IID_IDispatch, (void**)&disp);
177     IUnknown_Release(obj);
178     if(FAILED(hres)) {
179         FIXME("Object does not support IDispatch\n");
180         return E_NOTIMPL;
181     }
182
183     V_VT(retv) = VT_DISPATCH;
184     V_DISPATCH(retv) = disp;
185     return S_OK;
186 }
187
188 HRESULT create_activex_constr(script_ctx_t *ctx, jsdisp_t **ret)
189 {
190     jsdisp_t *prototype;
191     HRESULT hres;
192
193     static const WCHAR ActiveXObjectW[] = {'A','c','t','i','v','e','X','O','b','j','e','c','t',0};
194
195     hres = create_object(ctx, NULL, &prototype);
196     if(FAILED(hres))
197         return hres;
198
199     hres = create_builtin_function(ctx, ActiveXObject_value, ActiveXObjectW, NULL,
200             PROPF_CONSTR|1, prototype, ret);
201
202     jsdisp_release(prototype);
203     return hres;
204 }