Sync with trunk r58687.
[reactos.git] / dll / win32 / shdocvw / factory.c
1 /*
2 * Implementation of class factory for IE Web Browser
3 *
4 * Copyright 2001 John R. Sheets (for CodeWeavers)
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 //#include <string.h>
22 #include <stdio.h>
23
24 #include "shdocvw.h"
25 //#include "winreg.h"
26 #include <advpub.h>
27 //#include "isguids.h"
28
29 //#include "winver.h"
30
31 #include <wine/debug.h>
32
33 WINE_DEFAULT_DEBUG_CHANNEL(shdocvw);
34
35 /**********************************************************************
36 * Implement the WebBrowser class factory
37 *
38 * (Based on implementation in ddraw/main.c)
39 */
40
41 #define FACTORY(x) ((IClassFactory*) &(x)->lpClassFactoryVtbl)
42
43 typedef struct
44 {
45 /* IUnknown fields */
46 const IClassFactoryVtbl *lpClassFactoryVtbl;
47 HRESULT (*cf)(LPUNKNOWN, REFIID, LPVOID *);
48 LONG ref;
49 } IClassFactoryImpl;
50
51
52 /**********************************************************************
53 * WBCF_QueryInterface (IUnknown)
54 */
55 static HRESULT WINAPI WBCF_QueryInterface(LPCLASSFACTORY iface,
56 REFIID riid, LPVOID *ppobj)
57 {
58 TRACE("(%s %p)\n", debugstr_guid(riid), ppobj);
59
60 if (!ppobj)
61 return E_POINTER;
62
63 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IClassFactory, riid)) {
64 *ppobj = iface;
65 IClassFactory_AddRef(iface);
66 return S_OK;
67 }
68
69 WARN("Not supported interface %s\n", debugstr_guid(riid));
70
71 *ppobj = NULL;
72 return E_NOINTERFACE;
73 }
74
75 /************************************************************************
76 * WBCF_AddRef (IUnknown)
77 */
78 static ULONG WINAPI WBCF_AddRef(LPCLASSFACTORY iface)
79 {
80 SHDOCVW_LockModule();
81
82 return 2; /* non-heap based object */
83 }
84
85 /************************************************************************
86 * WBCF_Release (IUnknown)
87 */
88 static ULONG WINAPI WBCF_Release(LPCLASSFACTORY iface)
89 {
90 SHDOCVW_UnlockModule();
91
92 return 1; /* non-heap based object */
93 }
94
95 /************************************************************************
96 * WBCF_CreateInstance (IClassFactory)
97 */
98 static HRESULT WINAPI WBCF_CreateInstance(LPCLASSFACTORY iface, LPUNKNOWN pOuter,
99 REFIID riid, LPVOID *ppobj)
100 {
101 IClassFactoryImpl *This = (IClassFactoryImpl *) iface;
102 return This->cf(pOuter, riid, ppobj);
103 }
104
105 /************************************************************************
106 * WBCF_LockServer (IClassFactory)
107 */
108 static HRESULT WINAPI WBCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
109 {
110 TRACE("(%d)\n", dolock);
111
112 if (dolock)
113 SHDOCVW_LockModule();
114 else
115 SHDOCVW_UnlockModule();
116
117 return S_OK;
118 }
119
120 static const IClassFactoryVtbl WBCF_Vtbl =
121 {
122 WBCF_QueryInterface,
123 WBCF_AddRef,
124 WBCF_Release,
125 WBCF_CreateInstance,
126 WBCF_LockServer
127 };
128
129 /*************************************************************************
130 * DllGetClassObject (SHDOCVW.@)
131 */
132 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, void **ppv)
133 {
134 static IClassFactoryImpl WB1ClassFactory = {&WBCF_Vtbl, WebBrowserV1_Create};
135 static IClassFactoryImpl WB2ClassFactory = {&WBCF_Vtbl, WebBrowserV2_Create};
136 static IClassFactoryImpl CUHClassFactory = {&WBCF_Vtbl, CUrlHistory_Create};
137 static IClassFactoryImpl ISCClassFactory = {&WBCF_Vtbl, InternetShortcut_Create};
138 static IClassFactoryImpl TBLClassFactory = {&WBCF_Vtbl, TaskbarList_Create};
139
140 TRACE("\n");
141
142 if(IsEqualGUID(&CLSID_WebBrowser, rclsid))
143 return IClassFactory_QueryInterface(FACTORY(&WB2ClassFactory), riid, ppv);
144
145 if(IsEqualGUID(&CLSID_WebBrowser_V1, rclsid))
146 return IClassFactory_QueryInterface(FACTORY(&WB1ClassFactory), riid, ppv);
147
148 if(IsEqualGUID(&CLSID_CUrlHistory, rclsid))
149 return IClassFactory_QueryInterface(FACTORY(&CUHClassFactory), riid, ppv);
150
151 if(IsEqualGUID(&CLSID_InternetShortcut, rclsid))
152 return IClassFactory_QueryInterface(FACTORY(&ISCClassFactory), riid, ppv);
153
154 if(IsEqualGUID(&CLSID_TaskbarList, rclsid))
155 return IClassFactory_QueryInterface(FACTORY(&TBLClassFactory), riid, ppv);
156
157 /* As a last resort, figure if the CLSID belongs to a 'Shell Instance Object' */
158 return SHDOCVW_GetShellInstanceObjectClassObject(rclsid, riid, ppv);
159 }
160
161 HRESULT register_class_object(BOOL do_reg)
162 {
163 HRESULT hres;
164
165 static DWORD cookie;
166 static IClassFactoryImpl IEClassFactory = {&WBCF_Vtbl, InternetExplorer_Create};
167
168 if(do_reg) {
169 hres = CoRegisterClassObject(&CLSID_InternetExplorer, (IUnknown*)FACTORY(&IEClassFactory),
170 CLSCTX_SERVER, REGCLS_MULTIPLEUSE|REGCLS_SUSPENDED, &cookie);
171 if (FAILED(hres)) {
172 ERR("failed to register object %08x\n", hres);
173 return hres;
174 }
175
176 hres = CoResumeClassObjects();
177 if(SUCCEEDED(hres))
178 return hres;
179
180 ERR("failed to resume object %08x\n", hres);
181 }
182
183 return CoRevokeClassObject(cookie);
184 }
185
186 static HRESULT reg_install(LPCSTR section, STRTABLEA *strtable)
187 {
188 HRESULT (WINAPI *pRegInstall)(HMODULE hm, LPCSTR pszSection, const STRTABLEA* pstTable);
189 HMODULE hadvpack;
190 HRESULT hres;
191
192 static const WCHAR advpackW[] = {'a','d','v','p','a','c','k','.','d','l','l',0};
193
194 hadvpack = LoadLibraryW(advpackW);
195 pRegInstall = (void *)GetProcAddress(hadvpack, "RegInstall");
196
197 hres = pRegInstall(shdocvw_hinstance, section, strtable);
198
199 FreeLibrary(hadvpack);
200 return hres;
201 }
202
203 static const GUID CLSID_MicrosoftBrowserArchitecture =
204 {0xa5e46e3a, 0x8849, 0x11d1, {0x9d, 0x8c, 0x00, 0xc0, 0x4f, 0xc9, 0x9d, 0x61}};
205 static const GUID CLSID_MruLongList =
206 {0x53bd6b4e, 0x3780, 0x4693, {0xaf, 0xc3, 0x71, 0x61, 0xc2, 0xf3, 0xee, 0x9c}};
207
208 #define INF_SET_CLSID(clsid) \
209 do \
210 { \
211 static CHAR name[] = "CLSID_" #clsid; \
212 \
213 pse[i].pszName = name; \
214 clsids[i++] = &CLSID_ ## clsid; \
215 } while (0)
216
217 static HRESULT register_server(BOOL doregister)
218 {
219 STRTABLEA strtable;
220 STRENTRYA pse[15];
221 static CLSID const *clsids[15];
222 unsigned int i = 0;
223 HRESULT hres;
224
225 INF_SET_CLSID(CUrlHistory);
226 INF_SET_CLSID(Internet);
227 INF_SET_CLSID(InternetExplorer);
228 INF_SET_CLSID(InternetShortcut);
229 INF_SET_CLSID(MicrosoftBrowserArchitecture);
230 INF_SET_CLSID(MruLongList);
231 INF_SET_CLSID(SearchAssistantOC);
232 INF_SET_CLSID(ShellNameSpace);
233 INF_SET_CLSID(ShellSearchAssistantOC);
234 INF_SET_CLSID(ShellShellNameSpace);
235 INF_SET_CLSID(ShellUIHelper);
236 INF_SET_CLSID(ShellWindows);
237 INF_SET_CLSID(TaskbarList);
238 INF_SET_CLSID(WebBrowser);
239 INF_SET_CLSID(WebBrowser_V1);
240
241 for(i = 0; i < sizeof(pse)/sizeof(pse[0]); i++) {
242 pse[i].pszValue = HeapAlloc(GetProcessHeap(), 0, 39);
243 sprintf(pse[i].pszValue, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
244 clsids[i]->Data1, clsids[i]->Data2, clsids[i]->Data3, clsids[i]->Data4[0],
245 clsids[i]->Data4[1], clsids[i]->Data4[2], clsids[i]->Data4[3], clsids[i]->Data4[4],
246 clsids[i]->Data4[5], clsids[i]->Data4[6], clsids[i]->Data4[7]);
247 }
248
249 strtable.cEntries = sizeof(pse)/sizeof(pse[0]);
250 strtable.pse = pse;
251
252 hres = reg_install(doregister ? "RegisterDll" : "UnregisterDll", &strtable);
253
254 for(i=0; i < sizeof(pse)/sizeof(pse[0]); i++)
255 HeapFree(GetProcessHeap(), 0, pse[i].pszValue);
256
257 return hres;
258 }
259
260 #undef INF_SET_CLSID
261
262 /***********************************************************************
263 * DllRegisterServer (shdocvw.@)
264 */
265 HRESULT WINAPI DllRegisterServer(void)
266 {
267 ITypeLib *typelib;
268 HRESULT hres;
269
270 static const WCHAR shdocvwW[] = {'s','h','d','o','c','v','w','.','d','l','l',0};
271
272 hres = register_server(TRUE);
273 if(FAILED(hres))
274 return hres;
275
276 hres = LoadTypeLibEx(shdocvwW, REGKIND_REGISTER, &typelib);
277 if(FAILED(hres)) {
278 ERR("Could not load typelib: %08x\n", hres);
279 return hres;
280 }
281
282 ITypeLib_Release(typelib);
283
284 return hres;
285 }
286
287 /***********************************************************************
288 * DllUnregisterServer (shdocvw.@)
289 */
290 HRESULT WINAPI DllUnregisterServer(void)
291 {
292 HRESULT hres;
293
294 hres = register_server(FALSE);
295 if(FAILED(hres))
296 return hres;
297
298 return UnRegisterTypeLib(&LIBID_SHDocVw, 1, 1, LOCALE_SYSTEM_DEFAULT, SYS_WIN32);
299 }
300
301 static BOOL check_native_ie(void)
302 {
303 static const WCHAR cszPath[] = {'b','r','o','w','s','e','u','i','.','d','l','l',0};
304 DWORD handle,size;
305 BOOL ret = TRUE;
306
307 size = GetFileVersionInfoSizeW(cszPath,&handle);
308 if (size)
309 {
310 LPVOID buf;
311 LPWSTR lpFileDescription;
312 UINT dwBytes;
313 static const WCHAR cszFD[] = {'\\','S','t','r','i','n','g','F','i','l','e','I','n','f','o','\\','0','4','0','9','0','4','e','4','\\','F','i','l','e','D','e','s','c','r','i','p','t','i','o','n',0};
314 static const WCHAR cszWine[] = {'W','i','n','e',0};
315
316 buf = HeapAlloc(GetProcessHeap(),0,size);
317 GetFileVersionInfoW(cszPath,0,size,buf);
318
319 if (VerQueryValueW(buf, cszFD, (LPVOID*)&lpFileDescription, &dwBytes) &&
320 strstrW(lpFileDescription,cszWine))
321 ret = FALSE;
322
323 HeapFree(GetProcessHeap(), 0, buf);
324 }
325
326 return ret;
327 }
328
329 DWORD register_iexplore(BOOL doregister)
330 {
331 HRESULT hres;
332 if (check_native_ie())
333 {
334 TRACE("Native IE detected, not doing registration\n");
335 return S_OK;
336 }
337 hres = reg_install(doregister ? "RegisterIE" : "UnregisterIE", NULL);
338 return FAILED(hres);
339 }