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