shdocvw: Add Japanese translation.
[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 "rpcproxy.h"
28 #include "isguids.h"
29
30 #include "winver.h"
31
32 #include "wine/debug.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(shdocvw);
35
36 /**********************************************************************
37  * Implement the WebBrowser class factory
38  */
39
40 typedef struct
41 {
42     /* IUnknown fields */
43     IClassFactory IClassFactory_iface;
44     HRESULT (*cf)(LPUNKNOWN, REFIID, LPVOID *);
45     LONG ref;
46 } IClassFactoryImpl;
47
48 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
49 {
50     return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
51 }
52
53
54 /**********************************************************************
55  * WBCF_QueryInterface (IUnknown)
56  */
57 static HRESULT WINAPI WBCF_QueryInterface(LPCLASSFACTORY iface,
58                                           REFIID riid, LPVOID *ppobj)
59 {
60     TRACE("(%s %p)\n", debugstr_guid(riid), ppobj);
61
62     if (!ppobj)
63         return E_POINTER;
64
65     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IClassFactory, riid)) {
66         *ppobj = iface;
67         IClassFactory_AddRef(iface);
68         return S_OK;
69     }
70
71     WARN("Not supported interface %s\n", debugstr_guid(riid));
72
73     *ppobj = NULL;
74     return E_NOINTERFACE;
75 }
76
77 /************************************************************************
78  * WBCF_AddRef (IUnknown)
79  */
80 static ULONG WINAPI WBCF_AddRef(LPCLASSFACTORY iface)
81 {
82     SHDOCVW_LockModule();
83
84     return 2; /* non-heap based object */
85 }
86
87 /************************************************************************
88  * WBCF_Release (IUnknown)
89  */
90 static ULONG WINAPI WBCF_Release(LPCLASSFACTORY iface)
91 {
92     SHDOCVW_UnlockModule();
93
94     return 1; /* non-heap based object */
95 }
96
97 /************************************************************************
98  * WBCF_CreateInstance (IClassFactory)
99  */
100 static HRESULT WINAPI WBCF_CreateInstance(LPCLASSFACTORY iface, LPUNKNOWN pOuter,
101                                           REFIID riid, LPVOID *ppobj)
102 {
103     IClassFactoryImpl *This = (IClassFactoryImpl *) iface;
104     return This->cf(pOuter, riid, ppobj);
105 }
106
107 /************************************************************************
108  * WBCF_LockServer (IClassFactory)
109  */
110 static HRESULT WINAPI WBCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
111 {
112     TRACE("(%d)\n", dolock);
113
114     if (dolock)
115         SHDOCVW_LockModule();
116     else
117         SHDOCVW_UnlockModule();
118     
119     return S_OK;
120 }
121
122 static const IClassFactoryVtbl WBCF_Vtbl =
123 {
124     WBCF_QueryInterface,
125     WBCF_AddRef,
126     WBCF_Release,
127     WBCF_CreateInstance,
128     WBCF_LockServer
129 };
130
131 /*************************************************************************
132  *              DllGetClassObject (SHDOCVW.@)
133  */
134 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, void **ppv)
135 {
136     static IClassFactoryImpl WB1ClassFactory = {{&WBCF_Vtbl}, WebBrowserV1_Create};
137     static IClassFactoryImpl WB2ClassFactory = {{&WBCF_Vtbl}, WebBrowserV2_Create};
138     static IClassFactoryImpl CUHClassFactory = {{&WBCF_Vtbl}, CUrlHistory_Create};
139     static IClassFactoryImpl ISCClassFactory = {{&WBCF_Vtbl}, InternetShortcut_Create};
140     static IClassFactoryImpl TBLClassFactory = {{&WBCF_Vtbl}, TaskbarList_Create};
141
142     TRACE("\n");
143
144     if(IsEqualGUID(&CLSID_WebBrowser, rclsid))
145         return IClassFactory_QueryInterface(&WB2ClassFactory.IClassFactory_iface, riid, ppv);
146
147     if(IsEqualGUID(&CLSID_WebBrowser_V1, rclsid))
148         return IClassFactory_QueryInterface(&WB1ClassFactory.IClassFactory_iface, riid, ppv);
149
150     if(IsEqualGUID(&CLSID_CUrlHistory, rclsid))
151         return IClassFactory_QueryInterface(&CUHClassFactory.IClassFactory_iface, riid, ppv);
152
153     if(IsEqualGUID(&CLSID_InternetShortcut, rclsid))
154         return IClassFactory_QueryInterface(&ISCClassFactory.IClassFactory_iface, riid, ppv);
155
156     if(IsEqualGUID(&CLSID_TaskbarList, rclsid))
157         return IClassFactory_QueryInterface(&TBLClassFactory.IClassFactory_iface, riid, ppv);
158
159     /* As a last resort, figure if the CLSID belongs to a 'Shell Instance Object' */
160     return SHDOCVW_GetShellInstanceObjectClassObject(rclsid, riid, ppv);
161 }
162
163 HRESULT register_class_object(BOOL do_reg)
164 {
165     HRESULT hres;
166
167     static DWORD cookie;
168     static IClassFactoryImpl IEClassFactory = {{&WBCF_Vtbl}, InternetExplorer_Create};
169
170     if(do_reg) {
171         hres = CoRegisterClassObject(&CLSID_InternetExplorer,
172                                      (IUnknown*)&IEClassFactory.IClassFactory_iface, CLSCTX_SERVER,
173                                      REGCLS_MULTIPLEUSE|REGCLS_SUSPENDED, &cookie);
174         if (FAILED(hres)) {
175             ERR("failed to register object %08x\n", hres);
176             return hres;
177         }
178
179         hres = CoResumeClassObjects();
180         if(SUCCEEDED(hres))
181             return hres;
182
183         ERR("failed to resume object %08x\n", hres);
184     }
185
186     return CoRevokeClassObject(cookie);
187 }
188
189 static HRESULT reg_install(LPCSTR section, STRTABLEA *strtable)
190 {
191     HRESULT (WINAPI *pRegInstall)(HMODULE hm, LPCSTR pszSection, const STRTABLEA* pstTable);
192     HMODULE hadvpack;
193     HRESULT hres;
194
195     static const WCHAR advpackW[] = {'a','d','v','p','a','c','k','.','d','l','l',0};
196
197     hadvpack = LoadLibraryW(advpackW);
198     pRegInstall = (void *)GetProcAddress(hadvpack, "RegInstall");
199
200     hres = pRegInstall(shdocvw_hinstance, section, strtable);
201
202     FreeLibrary(hadvpack);
203     return hres;
204 }
205
206 #define INF_SET_CLSID(clsid)                  \
207     do                                        \
208     {                                         \
209         static CHAR name[] = "CLSID_" #clsid; \
210                                               \
211         pse[i].pszName = name;                \
212         clsids[i++] = &CLSID_ ## clsid;       \
213     } while (0)
214
215 static HRESULT register_server(BOOL doregister)
216 {
217     STRTABLEA strtable;
218     STRENTRYA pse[3];
219     static CLSID const *clsids[3];
220     unsigned int i = 0;
221     HRESULT hres;
222
223     INF_SET_CLSID(Internet);
224     INF_SET_CLSID(InternetExplorer);
225     INF_SET_CLSID(InternetShortcut);
226
227     for(i = 0; i < sizeof(pse)/sizeof(pse[0]); i++) {
228         pse[i].pszValue = HeapAlloc(GetProcessHeap(), 0, 39);
229         sprintf(pse[i].pszValue, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
230                 clsids[i]->Data1, clsids[i]->Data2, clsids[i]->Data3, clsids[i]->Data4[0],
231                 clsids[i]->Data4[1], clsids[i]->Data4[2], clsids[i]->Data4[3], clsids[i]->Data4[4],
232                 clsids[i]->Data4[5], clsids[i]->Data4[6], clsids[i]->Data4[7]);
233     }
234
235     strtable.cEntries = sizeof(pse)/sizeof(pse[0]);
236     strtable.pse = pse;
237
238     hres = reg_install(doregister ? "RegisterDll" : "UnregisterDll", &strtable);
239
240     for(i=0; i < sizeof(pse)/sizeof(pse[0]); i++)
241         HeapFree(GetProcessHeap(), 0, pse[i].pszValue);
242
243     return hres;
244 }
245
246 #undef INF_SET_CLSID
247
248 /***********************************************************************
249  *          DllRegisterServer (shdocvw.@)
250  */
251 HRESULT WINAPI DllRegisterServer(void)
252 {
253     HRESULT hres;
254
255     hres = __wine_register_resources( shdocvw_hinstance, NULL );
256     if(FAILED(hres))
257         return hres;
258
259     return register_server(TRUE);
260 }
261
262 /***********************************************************************
263  *          DllUnregisterServer (shdocvw.@)
264  */
265 HRESULT WINAPI DllUnregisterServer(void)
266 {
267     HRESULT hres;
268
269     hres = register_server(FALSE);
270     if(FAILED(hres))
271         return hres;
272
273     return __wine_unregister_resources( shdocvw_hinstance, NULL );
274 }
275
276 static BOOL check_native_ie(void)
277 {
278     static const WCHAR cszPath[] = {'b','r','o','w','s','e','u','i','.','d','l','l',0};
279     DWORD handle,size;
280     BOOL ret = TRUE;
281
282     size = GetFileVersionInfoSizeW(cszPath,&handle);
283     if (size)
284     {
285         LPVOID buf;
286         LPWSTR lpFileDescription;
287         UINT dwBytes;
288         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};
289         static const WCHAR cszWine[] = {'W','i','n','e',0};
290
291         buf = HeapAlloc(GetProcessHeap(),0,size);
292         GetFileVersionInfoW(cszPath,0,size,buf);
293
294         if (VerQueryValueW(buf, cszFD, (LPVOID*)&lpFileDescription, &dwBytes) &&
295             strstrW(lpFileDescription,cszWine))
296                 ret = FALSE;
297
298         HeapFree(GetProcessHeap(), 0, buf);
299     }
300
301     return ret;
302 }
303
304 DWORD register_iexplore(BOOL doregister)
305 {
306     HRESULT hres;
307     if (check_native_ie())
308     {
309         TRACE("Native IE detected, not doing registration\n");
310         return S_OK;
311     }
312     hres = reg_install(doregister ? "RegisterIE" : "UnregisterIE", NULL);
313     return FAILED(hres);
314 }