shell32/tests: Avoid SHDeleteKeyA() because shlwapi.dll is missing on Windows 95.
[wine] / dlls / 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 "wine/debug.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(shdocvw);
32
33 /**********************************************************************
34  * Implement the WebBrowser class factory
35  *
36  * (Based on implementation in ddraw/main.c)
37  */
38
39 #define FACTORY(x) ((IClassFactory*) &(x)->lpClassFactoryVtbl)
40
41 typedef struct
42 {
43     /* IUnknown fields */
44     const IClassFactoryVtbl *lpClassFactoryVtbl;
45     HRESULT (*cf)(LPUNKNOWN, REFIID, LPVOID *);
46     LONG ref;
47 } IClassFactoryImpl;
48
49
50 /**********************************************************************
51  * WBCF_QueryInterface (IUnknown)
52  */
53 static HRESULT WINAPI WBCF_QueryInterface(LPCLASSFACTORY iface,
54                                           REFIID riid, LPVOID *ppobj)
55 {
56     TRACE("(%s %p)\n", debugstr_guid(riid), ppobj);
57
58     if (!ppobj)
59         return E_POINTER;
60
61     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IClassFactory, riid)) {
62         *ppobj = iface;
63         IClassFactory_AddRef(iface);
64         return S_OK;
65     }
66
67     WARN("Not supported interface %s\n", debugstr_guid(riid));
68
69     *ppobj = NULL;
70     return E_NOINTERFACE;
71 }
72
73 /************************************************************************
74  * WBCF_AddRef (IUnknown)
75  */
76 static ULONG WINAPI WBCF_AddRef(LPCLASSFACTORY iface)
77 {
78     SHDOCVW_LockModule();
79
80     return 2; /* non-heap based object */
81 }
82
83 /************************************************************************
84  * WBCF_Release (IUnknown)
85  */
86 static ULONG WINAPI WBCF_Release(LPCLASSFACTORY iface)
87 {
88     SHDOCVW_UnlockModule();
89
90     return 1; /* non-heap based object */
91 }
92
93 /************************************************************************
94  * WBCF_CreateInstance (IClassFactory)
95  */
96 static HRESULT WINAPI WBCF_CreateInstance(LPCLASSFACTORY iface, LPUNKNOWN pOuter,
97                                           REFIID riid, LPVOID *ppobj)
98 {
99     IClassFactoryImpl *This = (IClassFactoryImpl *) iface;
100     return This->cf(pOuter, riid, ppobj);
101 }
102
103 /************************************************************************
104  * WBCF_LockServer (IClassFactory)
105  */
106 static HRESULT WINAPI WBCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
107 {
108     TRACE("(%d)\n", dolock);
109
110     if (dolock)
111         SHDOCVW_LockModule();
112     else
113         SHDOCVW_UnlockModule();
114     
115     return S_OK;
116 }
117
118 static const IClassFactoryVtbl WBCF_Vtbl =
119 {
120     WBCF_QueryInterface,
121     WBCF_AddRef,
122     WBCF_Release,
123     WBCF_CreateInstance,
124     WBCF_LockServer
125 };
126
127 /*************************************************************************
128  *              DllGetClassObject (SHDOCVW.@)
129  */
130 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, void **ppv)
131 {
132     static IClassFactoryImpl WB1ClassFactory = {&WBCF_Vtbl, WebBrowserV1_Create};
133     static IClassFactoryImpl WB2ClassFactory = {&WBCF_Vtbl, WebBrowserV2_Create};
134     static IClassFactoryImpl CUHClassFactory = {&WBCF_Vtbl, CUrlHistory_Create};
135
136     TRACE("\n");
137
138     if(IsEqualGUID(&CLSID_WebBrowser, rclsid))
139         return IClassFactory_QueryInterface(FACTORY(&WB2ClassFactory), riid, ppv);
140
141     if(IsEqualGUID(&CLSID_WebBrowser_V1, rclsid))
142         return IClassFactory_QueryInterface(FACTORY(&WB1ClassFactory), riid, ppv);
143
144     if(IsEqualGUID(&CLSID_CUrlHistory, rclsid))
145         return IClassFactory_QueryInterface(FACTORY(&CUHClassFactory), riid, ppv);
146
147     /* As a last resort, figure if the CLSID belongs to a 'Shell Instance Object' */
148     return SHDOCVW_GetShellInstanceObjectClassObject(rclsid, riid, ppv);
149 }
150
151 HRESULT register_class_object(BOOL do_reg)
152 {
153     HRESULT hres;
154
155     static DWORD cookie;
156     static IClassFactoryImpl IEClassFactory = {&WBCF_Vtbl, InternetExplorer_Create};
157
158     if(do_reg) {
159         hres = CoRegisterClassObject(&CLSID_InternetExplorer, (IUnknown*)FACTORY(&IEClassFactory),
160                                      CLSCTX_SERVER, REGCLS_MULTIPLEUSE|REGCLS_SUSPENDED, &cookie);
161         if (FAILED(hres)) {
162             ERR("failed to register object %08x\n", hres);
163             return hres;
164         }
165
166         hres = CoResumeClassObjects();
167         if(SUCCEEDED(hres))
168             return hres;
169
170         ERR("failed to resume object %08x\n", hres);
171     }
172
173     return CoRevokeClassObject(cookie);
174 }
175
176 static HRESULT reg_install(LPCSTR section, STRTABLEA *strtable)
177 {
178     typeof(RegInstallA) *pRegInstall;
179     HMODULE hadvpack;
180     HRESULT hres;
181
182     static const WCHAR advpackW[] = {'a','d','v','p','a','c','k','.','d','l','l',0};
183
184     hadvpack = LoadLibraryW(advpackW);
185     pRegInstall = (typeof(RegInstallA)*)GetProcAddress(hadvpack, "RegInstall");
186
187     hres = pRegInstall(shdocvw_hinstance, section, strtable);
188
189     FreeLibrary(hadvpack);
190     return hres;
191 }
192
193 static const GUID CLSID_MicrosoftBrowserArchitecture =
194     {0xa5e46e3a, 0x8849, 0x11d1, {0x9d, 0x8c, 0x00, 0xc0, 0x4f, 0xc9, 0x9d, 0x61}};
195 static const GUID CLSID_MruLongList =
196     {0x53bd6b4e, 0x3780, 0x4693, {0xaf, 0xc3, 0x71, 0x61, 0xc2, 0xf3, 0xee, 0x9c}};
197
198 #define INF_SET_CLSID(clsid)                  \
199     do                                        \
200     {                                         \
201         static CHAR name[] = "CLSID_" #clsid; \
202                                               \
203         pse[i].pszName = name;                \
204         clsids[i++] = &CLSID_ ## clsid;       \
205     } while (0)
206
207 static HRESULT register_server(BOOL doregister)
208 {
209     STRTABLEA strtable;
210     STRENTRYA pse[14];
211     static CLSID const *clsids[14];
212     int i = 0;
213     HRESULT hres;
214
215     INF_SET_CLSID(CUrlHistory);
216     INF_SET_CLSID(Internet);
217     INF_SET_CLSID(InternetExplorer);
218     INF_SET_CLSID(InternetShortcut);
219     INF_SET_CLSID(MicrosoftBrowserArchitecture);
220     INF_SET_CLSID(MruLongList);
221     INF_SET_CLSID(SearchAssistantOC);
222     INF_SET_CLSID(ShellNameSpace);
223     INF_SET_CLSID(ShellSearchAssistantOC);
224     INF_SET_CLSID(ShellShellNameSpace);
225     INF_SET_CLSID(ShellUIHelper);
226     INF_SET_CLSID(ShellWindows);
227     INF_SET_CLSID(WebBrowser);
228     INF_SET_CLSID(WebBrowser_V1);
229
230     for(i = 0; i < sizeof(pse)/sizeof(pse[0]); i++) {
231         pse[i].pszValue = HeapAlloc(GetProcessHeap(), 0, 39);
232         sprintf(pse[i].pszValue, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
233                 clsids[i]->Data1, clsids[i]->Data2, clsids[i]->Data3, clsids[i]->Data4[0],
234                 clsids[i]->Data4[1], clsids[i]->Data4[2], clsids[i]->Data4[3], clsids[i]->Data4[4],
235                 clsids[i]->Data4[5], clsids[i]->Data4[6], clsids[i]->Data4[7]);
236     }
237
238     strtable.cEntries = sizeof(pse)/sizeof(pse[0]);
239     strtable.pse = pse;
240
241     hres = reg_install(doregister ? "RegisterDll" : "UnregisterDll", &strtable);
242
243     for(i=0; i < sizeof(pse)/sizeof(pse[0]); i++)
244         HeapFree(GetProcessHeap(), 0, pse[i].pszValue);
245
246     return hres;
247 }
248
249 #undef INF_SET_CLSID
250
251 /***********************************************************************
252  *          DllRegisterServer (shdocvw.@)
253  */
254 HRESULT WINAPI DllRegisterServer(void)
255 {
256     ITypeLib *typelib;
257     HRESULT hres;
258
259     static const WCHAR shdocvwW[] = {'s','h','d','o','c','v','w','.','d','l','l',0};
260
261     hres = register_server(TRUE);
262     if(FAILED(hres))
263         return hres;
264
265     hres = LoadTypeLibEx(shdocvwW, REGKIND_REGISTER, &typelib);
266     if(FAILED(hres)) {
267         ERR("Could not load typelib: %08x\n", hres);
268         return hres;
269     }
270
271     ITypeLib_Release(typelib);
272
273     return hres;
274 }
275
276 /***********************************************************************
277  *          DllUnregisterServer (shdocvw.@)
278  */
279 HRESULT WINAPI DllUnregisterServer(void)
280 {
281     HRESULT hres;
282
283     hres = register_server(FALSE);
284     if(FAILED(hres))
285         return hres;
286
287     return UnRegisterTypeLib(&LIBID_SHDocVw, 1, 1, LOCALE_SYSTEM_DEFAULT, SYS_WIN32);
288 }
289
290 DWORD register_iexplore(BOOL doregister)
291 {
292     HRESULT hres = reg_install(doregister ? "RegisterIE" : "UnregisterIE", NULL);
293     return !SUCCEEDED(hres);
294 }