Fix comment about hack.
[wine] / dlls / mshtml / nsembed.c
1 /*
2  * Copyright 2005 Jacek Caban
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #include "config.h"
20
21 #include <stdarg.h>
22
23 #define COBJMACROS
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "winreg.h"
29 #include "ole2.h"
30 #include "docobj.h"
31
32 #include "mshtml.h"
33 #include "mshtmhst.h"
34
35 #include "wine/debug.h"
36 #include "wine/unicode.h"
37
38 #include "mshtml_private.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
41
42 DEFINE_GUID(CLSID_StartupNotifier, 0x1f59b001,0x02c9,0x11d5,0xae,0x76,0xcc,0x92,0xf7,0xdb,0x9e,0x03);
43 DEFINE_GUID(CLSID_nsWebBrowser, 0xf1eac761,0x87e9,0x11d3,0xaf,0x80,0x00,0xa0,0x24,0xff,0xc0,0x8c);
44
45 #define NS_APPSTARTUPNOTIFIER_CONTRACTID "@mozilla.org/embedcomp/appstartup-notifier;1"
46 #define NS_WEBBROWSER_CONTRACTID "@mozilla.org/embedding/browser/nsWebBrowser;1"
47
48 #define APPSTARTUP_TOPIC "app-startup"
49
50 #define PR_UINT32_MAX 0xffffffff
51
52 typedef struct {
53     void *d1;
54     PRUint32 d2;
55     void *d3;
56 } nsString;
57
58 static nsresult (*NS_InitXPCOM2)(nsIServiceManager**,void*,void*);
59 static nsresult (*NS_ShutdownXPCOM)(nsIServiceManager*);
60 static nsresult (*NS_StringContainerInit)(nsString*);
61 static nsresult (*NS_StringContainerFinish)(nsString*);
62 static nsresult (*NS_StringSetData)(nsString*,const PRUnichar*,PRUint32);
63 static nsresult (*NS_NewLocalFile)(const nsString*,PRBool,nsIFile**);
64
65 static HINSTANCE hXPCOM = NULL;
66
67 static nsIServiceManager *pServMgr = NULL;
68 static nsIComponentManager *pCompMgr = NULL;
69
70 static BOOL get_mozilla_path(PRUnichar *gre_path)
71 {
72     DWORD res, type, i, size = MAX_PATH;
73     HKEY mozilla_key, hkey;
74     WCHAR key_name[100];
75     BOOL ret = FALSE;
76
77     static const WCHAR wszGreKey[] =
78         {'S','o','f','t','w','a','r','e','\\',
79             'm','o','z','i','l','l','a','.','o','r','g','\\',
80                 'G','R','E',0};
81
82     static const WCHAR wszGreHome[] = {'G','r','e','H','o','m','e',0};
83
84     res = RegOpenKeyW(HKEY_LOCAL_MACHINE, wszGreKey, &mozilla_key);
85     if(res != ERROR_SUCCESS) {
86         TRACE("Could not open key %s\n", debugstr_w(wszGreKey));
87         return FALSE;
88     }
89
90     for(i=0; !ret && RegEnumKeyW(mozilla_key, i, key_name, sizeof(key_name)/sizeof(WCHAR)) == ERROR_SUCCESS; i++) {
91         RegOpenKeyW(mozilla_key, key_name, &hkey);
92         res = RegQueryValueExW(hkey, wszGreHome, NULL, &type, (LPBYTE)gre_path, &size);
93         if(res == ERROR_SUCCESS)
94             ret = TRUE;
95         RegCloseKey(hkey);
96     }
97
98     RegCloseKey(mozilla_key);
99     return ret;
100 }
101
102 static BOOL get_mozctl_path(PRUnichar *gre_path)
103 {
104     HKEY hkey;
105     DWORD res, type, size = MAX_PATH;
106
107     static const WCHAR wszMozCtlKey[] =
108         {'S','o','f','t','w','a','r','e','\\','M','o','z','i','l','l','a',0};
109     static const WCHAR wszBinDirectoryPath[] =
110         {'B','i','n','D','i','r','e','c','t','o','r','y','P','a','t','h',0};
111
112     res = RegOpenKeyW(HKEY_LOCAL_MACHINE, wszMozCtlKey, &hkey);
113     if(res != ERROR_SUCCESS) {
114         TRACE("Could not open key %s\n", debugstr_w(wszMozCtlKey));
115         return FALSE;
116     }
117
118     res = RegQueryValueExW(hkey, wszBinDirectoryPath, NULL, &type, (LPBYTE)gre_path, &size);
119     if(res != ERROR_SUCCESS) {
120         ERR("Could not get value %s\n", debugstr_w(wszBinDirectoryPath));
121         return FALSE;
122     }
123
124     return TRUE;
125 }
126
127
128
129 static BOOL load_gecko()
130 {
131     nsresult nsres;
132     nsIObserver *pStartNotif;
133     nsString path;
134     nsIFile *gre_dir;
135     PRUnichar gre_path[MAX_PATH];
136
137     static BOOL tried_load = FALSE;
138     static const WCHAR strXPCOM[] = {'x','p','c','o','m','.','d','l','l',0};
139
140     TRACE("()\n");
141
142     if(tried_load)
143         return pCompMgr != NULL;
144     tried_load = TRUE;
145
146     if(!(get_mozctl_path(gre_path) || get_mozilla_path(gre_path))) {
147         MESSAGE("Could not load Mozilla. HTML rendering will be disabled.\n");
148         return FALSE;
149     }
150     
151     TRACE("found path %s\n", debugstr_w(gre_path));
152
153     hXPCOM = LoadLibraryW(strXPCOM);
154     if(!hXPCOM) {
155         /* We have to modify PATH as XPCOM loads other DLLs from this directory. */
156         WCHAR path_env[MAX_PATH];
157         static WCHAR wszPATH[] = {'P','A','T','H',0};
158         int len;
159
160         TRACE("here\n");
161
162         GetEnvironmentVariableW(wszPATH, path_env, sizeof(path_env)/sizeof(WCHAR));
163         len = strlenW(path_env);
164         path_env[len++] = ';';
165         strcpyW(path_env+len, gre_path);
166         SetEnvironmentVariableW(wszPATH, path_env);
167
168         hXPCOM = LoadLibraryW(strXPCOM);
169         if(!hXPCOM) {
170             ERR("Could not load XPCOM: %ld\n", GetLastError());
171             return FALSE;
172         }
173     }
174
175 #define NS_DLSYM(func) \
176     func = (typeof(func))GetProcAddress(hXPCOM, #func); \
177     if(!func) \
178         ERR("Could not GetProcAddress(" #func ") failed\n");
179
180     NS_DLSYM(NS_InitXPCOM2)
181     NS_DLSYM(NS_ShutdownXPCOM)
182     NS_DLSYM(NS_StringContainerInit)
183     NS_DLSYM(NS_StringContainerFinish)
184     NS_DLSYM(NS_StringSetData)
185     NS_DLSYM(NS_NewLocalFile)
186
187 #undef NS_DLSYM
188
189     NS_StringContainerInit(&path);
190     NS_StringSetData(&path, gre_path, PR_UINT32_MAX);
191     nsres = NS_NewLocalFile(&path, FALSE, &gre_dir);
192     NS_StringContainerFinish(&path);
193     if(NS_FAILED(nsres)) {
194         ERR("NS_NewLocalFile failed: %08lx\n", nsres);
195         FreeLibrary(hXPCOM);
196         return FALSE;
197     }
198
199     nsres = NS_InitXPCOM2(&pServMgr, gre_dir, NULL);
200     if(NS_FAILED(nsres)) {
201         ERR("NS_InitXPCOM2 failed: %08lx\n", nsres);
202         FreeLibrary(hXPCOM);
203         return FALSE;
204     }
205
206     nsres = nsIServiceManager_QueryInterface(pServMgr, &IID_nsIComponentManager, (void**)&pCompMgr);
207     if(NS_FAILED(nsres))
208         ERR("Could not get nsIComponentManager: %08lx\n", nsres);
209
210     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_APPSTARTUPNOTIFIER_CONTRACTID,
211             NULL, &IID_nsIObserver, (void**)&pStartNotif);
212     if(NS_SUCCEEDED(nsres)) {
213         nsres = nsIObserver_Observe(pStartNotif, NULL, APPSTARTUP_TOPIC, NULL);
214         if(NS_FAILED(nsres))
215             ERR("Observe failed: %08lx\n", nsres);
216
217         nsIObserver_Release(pStartNotif);
218     }else {
219         ERR("could not get appstartup-notifier: %08lx\n", nsres);
220     }
221
222     return TRUE;
223 }
224
225 void close_gecko()
226 {
227     TRACE("()\n");
228
229     if(pCompMgr)
230         nsIComponentManager_Release(pCompMgr);
231
232     if(pServMgr)
233         nsIServiceManager_Release(pServMgr);
234
235     if(hXPCOM)
236         FreeLibrary(hXPCOM);
237 }
238
239 void HTMLDocument_NSContainer_Init(HTMLDocument *This)
240 {
241     NSContainer *ret;
242     nsIWebBrowserSetup *wbsetup;
243     nsresult nsres;
244
245     This->nscontainer = NULL;
246
247     if(!load_gecko())
248         return;
249
250     ret = HeapAlloc(GetProcessHeap(), 0, sizeof(NSContainer));
251     ret->url = NULL;
252
253     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
254             NS_WEBBROWSER_CONTRACTID, NULL, &IID_nsIWebBrowser, (void**)&ret->webbrowser);
255     if(NS_FAILED(nsres))
256         ERR("Creating WebBrowser failed: %08lx\n", nsres);
257
258     nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIBaseWindow,
259             (void**)&ret->window);
260     if(NS_FAILED(nsres))
261         ERR("Could not get nsIBaseWindow interface: %08lx\n", nsres);
262
263     nsres = nsIWebBrowser_QueryInterface(ret->webbrowser,
264             &IID_nsIWebBrowserSetup, (void**)&wbsetup);
265     if(NS_SUCCEEDED(nsres)) {
266         nsres = nsIWebBrowserSetup_SetProperty(wbsetup, SETUP_IS_CHROME_WRAPPER, TRUE);
267         if(NS_FAILED(nsres))
268             ERR("SetProperty failed: %08lx\n", nsres);
269         nsIWebBrowserSetup_Release(wbsetup);
270     }else {
271         ERR("Could not get nsIWebBrowserSetup interface\n");
272     }
273
274     nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIWebNavigation,
275             (void**)&ret->navigation);
276     if(NS_FAILED(nsres))
277         ERR("Could not get nsIWebNavigation interface: %08lx\n", nsres);
278
279     This->nscontainer = ret;
280 }
281
282 void HTMLDocument_NSContainer_Destroy(HTMLDocument *This)
283 {
284     TRACE("(%p)\n", This);
285
286     nsIWebBrowser_Release(This->nscontainer->webbrowser);
287     nsIWebNavigation_Release(This->nscontainer->navigation);
288     nsIBaseWindow_Release(This->nscontainer->window);
289
290     HeapFree(GetProcessHeap(), 0, This->nscontainer);
291 }