Fix protocol test on winxp.
[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
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
33
34 #include "mshtml_private.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
37
38 DEFINE_GUID(CLSID_StartupNotifier, 0x1f59b001,0x02c9,0x11d5,0xae,0x76,0xcc,0x92,0xf7,0xdb,0x9e,0x03);
39 DEFINE_GUID(CLSID_nsWebBrowser, 0xf1eac761,0x87e9,0x11d3,0xaf,0x80,0x00,0xa0,0x24,0xff,0xc0,0x8c);
40
41 #define NS_APPSTARTUPNOTIFIER_CONTRACTID "@mozilla.org/embedcomp/appstartup-notifier;1"
42 #define NS_WEBBROWSER_CONTRACTID "@mozilla.org/embedding/browser/nsWebBrowser;1"
43 #define NS_IOSERVICE_CONTRACTID "@mozilla.org/network/io-service;1"
44
45 #define APPSTARTUP_TOPIC "app-startup"
46
47 #define PR_UINT32_MAX 0xffffffff
48
49 typedef struct nsACString {
50     void *d1;
51     PRUint32 d2;
52     void *d3;
53 } nsString;
54
55 static nsresult (*NS_InitXPCOM2)(nsIServiceManager**,void*,void*);
56 static nsresult (*NS_ShutdownXPCOM)(nsIServiceManager*);
57 static nsresult (*NS_StringContainerInit)(nsString*);
58 static nsresult (*NS_CStringContainerInit)(nsACString*);
59 static nsresult (*NS_StringContainerFinish)(nsString*);
60 static nsresult (*NS_CStringContainerFinish)(nsACString*);
61 static nsresult (*NS_StringSetData)(nsString*,const PRUnichar*,PRUint32);
62 static nsresult (*NS_CStringSetData)(nsString*,const char*,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 static nsIIOService *pIOService = NULL;
70
71 static const WCHAR wszNsContainer[] = {'N','s','C','o','n','t','a','i','n','e','r',0};
72
73 static ATOM nscontainer_class;
74
75 static LRESULT WINAPI nsembed_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
76 {
77     HTMLDocument *This;
78     nsresult nsres;
79
80     static const WCHAR wszTHIS[] = {'T','H','I','S',0};
81
82     if(msg == WM_CREATE) {
83         This = *(HTMLDocument**)lParam;
84         SetPropW(hwnd, wszTHIS, This);
85     }else {
86         This = (HTMLDocument*)GetPropW(hwnd, wszTHIS);
87     }
88
89     switch(msg) {
90         case WM_SIZE:
91             TRACE("(%p)->(WM_SIZE)\n", This);
92
93             nsres = nsIBaseWindow_SetSize(This->nscontainer->window,
94                     LOWORD(lParam), HIWORD(lParam), TRUE);
95             if(NS_FAILED(nsres))
96                 WARN("SetSize failed: %08lx\n", nsres);
97     }
98
99     return DefWindowProcW(hwnd, msg, wParam, lParam);
100 }
101
102
103 static void register_nscontainer_class(void)
104 {
105     static WNDCLASSEXW wndclass = {
106         sizeof(WNDCLASSEXW),
107         CS_DBLCLKS,
108         nsembed_proc,
109         0, 0, NULL, NULL, NULL, NULL, NULL,
110         wszNsContainer,
111         NULL,
112     };
113     wndclass.hInstance = hInst;
114     nscontainer_class = RegisterClassExW(&wndclass);
115 }
116
117 static BOOL get_mozilla_path(PRUnichar *gre_path)
118 {
119     DWORD res, type, i, size = MAX_PATH;
120     HKEY mozilla_key, hkey;
121     WCHAR key_name[100];
122     BOOL ret = FALSE;
123
124     static const WCHAR wszGreKey[] =
125         {'S','o','f','t','w','a','r','e','\\',
126             'm','o','z','i','l','l','a','.','o','r','g','\\',
127                 'G','R','E',0};
128
129     static const WCHAR wszGreHome[] = {'G','r','e','H','o','m','e',0};
130
131     res = RegOpenKeyW(HKEY_LOCAL_MACHINE, wszGreKey, &mozilla_key);
132     if(res != ERROR_SUCCESS) {
133         TRACE("Could not open key %s\n", debugstr_w(wszGreKey));
134         return FALSE;
135     }
136
137     for(i=0; !ret && RegEnumKeyW(mozilla_key, i, key_name, sizeof(key_name)/sizeof(WCHAR)) == ERROR_SUCCESS; i++) {
138         RegOpenKeyW(mozilla_key, key_name, &hkey);
139         res = RegQueryValueExW(hkey, wszGreHome, NULL, &type, (LPBYTE)gre_path, &size);
140         if(res == ERROR_SUCCESS)
141             ret = TRUE;
142         RegCloseKey(hkey);
143     }
144
145     RegCloseKey(mozilla_key);
146     return ret;
147 }
148
149 static BOOL get_mozctl_path(PRUnichar *gre_path)
150 {
151     HKEY hkey;
152     DWORD res, type, size = MAX_PATH;
153
154     static const WCHAR wszMozCtlKey[] =
155         {'S','o','f','t','w','a','r','e','\\','M','o','z','i','l','l','a',0};
156     static const WCHAR wszBinDirectoryPath[] =
157         {'B','i','n','D','i','r','e','c','t','o','r','y','P','a','t','h',0};
158
159     res = RegOpenKeyW(HKEY_LOCAL_MACHINE, wszMozCtlKey, &hkey);
160     if(res != ERROR_SUCCESS) {
161         TRACE("Could not open key %s\n", debugstr_w(wszMozCtlKey));
162         return FALSE;
163     }
164
165     res = RegQueryValueExW(hkey, wszBinDirectoryPath, NULL, &type, (LPBYTE)gre_path, &size);
166     if(res != ERROR_SUCCESS) {
167         ERR("Could not get value %s\n", debugstr_w(wszBinDirectoryPath));
168         return FALSE;
169     }
170
171     return TRUE;
172 }
173
174 static BOOL load_gecko()
175 {
176     nsresult nsres;
177     nsIObserver *pStartNotif;
178     nsString path;
179     nsIFile *gre_dir;
180     PRUnichar gre_path[MAX_PATH];
181
182     static BOOL tried_load = FALSE;
183     static const WCHAR strXPCOM[] = {'x','p','c','o','m','.','d','l','l',0};
184
185     TRACE("()\n");
186
187     if(tried_load)
188         return pCompMgr != NULL;
189     tried_load = TRUE;
190
191     if(!get_mozctl_path(gre_path) && !get_mozilla_path(gre_path)) {
192         MESSAGE("Could not load Mozilla. HTML rendering will be disabled.\n");
193         return FALSE;
194     }
195     
196     TRACE("found path %s\n", debugstr_w(gre_path));
197
198     hXPCOM = LoadLibraryW(strXPCOM);
199     if(!hXPCOM) {
200         /* We have to modify PATH as XPCOM loads other DLLs from this directory. */
201         WCHAR path_env[MAX_PATH];
202         static WCHAR wszPATH[] = {'P','A','T','H',0};
203         int len;
204
205         GetEnvironmentVariableW(wszPATH, path_env, sizeof(path_env)/sizeof(WCHAR));
206         len = strlenW(path_env);
207         path_env[len++] = ';';
208         strcpyW(path_env+len, gre_path);
209         SetEnvironmentVariableW(wszPATH, path_env);
210
211         hXPCOM = LoadLibraryW(strXPCOM);
212         if(!hXPCOM) {
213             ERR("Could not load XPCOM: %ld\n", GetLastError());
214             return FALSE;
215         }
216     }
217
218 #define NS_DLSYM(func) \
219     func = (typeof(func))GetProcAddress(hXPCOM, #func); \
220     if(!func) \
221         ERR("Could not GetProcAddress(" #func ") failed\n")
222
223     NS_DLSYM(NS_InitXPCOM2);
224     NS_DLSYM(NS_ShutdownXPCOM);
225     NS_DLSYM(NS_StringContainerInit);
226     NS_DLSYM(NS_CStringContainerInit);
227     NS_DLSYM(NS_StringContainerFinish);
228     NS_DLSYM(NS_CStringContainerFinish);
229     NS_DLSYM(NS_StringSetData);
230     NS_DLSYM(NS_CStringSetData);
231     NS_DLSYM(NS_NewLocalFile);
232
233 #undef NS_DLSYM
234
235     NS_StringContainerInit(&path);
236     NS_StringSetData(&path, gre_path, PR_UINT32_MAX);
237     nsres = NS_NewLocalFile(&path, FALSE, &gre_dir);
238     NS_StringContainerFinish(&path);
239     if(NS_FAILED(nsres)) {
240         ERR("NS_NewLocalFile failed: %08lx\n", nsres);
241         FreeLibrary(hXPCOM);
242         return FALSE;
243     }
244
245     nsres = NS_InitXPCOM2(&pServMgr, gre_dir, NULL);
246     if(NS_FAILED(nsres)) {
247         ERR("NS_InitXPCOM2 failed: %08lx\n", nsres);
248         FreeLibrary(hXPCOM);
249         return FALSE;
250     }
251
252     nsres = nsIServiceManager_QueryInterface(pServMgr, &IID_nsIComponentManager, (void**)&pCompMgr);
253     if(NS_FAILED(nsres))
254         ERR("Could not get nsIComponentManager: %08lx\n", nsres);
255
256     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_APPSTARTUPNOTIFIER_CONTRACTID,
257             NULL, &IID_nsIObserver, (void**)&pStartNotif);
258     if(NS_SUCCEEDED(nsres)) {
259         nsres = nsIObserver_Observe(pStartNotif, NULL, APPSTARTUP_TOPIC, NULL);
260         if(NS_FAILED(nsres))
261             ERR("Observe failed: %08lx\n", nsres);
262
263         nsIObserver_Release(pStartNotif);
264     }else {
265         ERR("could not get appstartup-notifier: %08lx\n", nsres);
266     }
267
268     return TRUE;
269 }
270
271 nsACString *nsACString_Create(void)
272 {
273     nsACString *ret;
274     ret = HeapAlloc(GetProcessHeap(), 0, sizeof(nsACString));
275     NS_CStringContainerInit(ret);
276     return ret;
277 }
278
279 void nsACString_SetData(nsACString *str, const char *data)
280 {
281     NS_CStringSetData(str, data, PR_UINT32_MAX);
282 }
283
284 void nsACString_Destroy(nsACString *str)
285 {
286     NS_CStringContainerFinish(str);
287     HeapFree(GetProcessHeap(), 0, str);
288 }
289
290 void close_gecko()
291 {
292     TRACE("()\n");
293
294     if(pCompMgr)
295         nsIComponentManager_Release(pCompMgr);
296
297     if(pServMgr)
298         nsIServiceManager_Release(pServMgr);
299
300     if(hXPCOM)
301         FreeLibrary(hXPCOM);
302 }
303
304 nsIURI *get_nsIURI(LPCWSTR url)
305 {
306     nsIURI *ret;
307     nsACString *acstr;
308     nsresult nsres;
309     char *urla;
310     int len;
311
312     if(!pIOService) {
313         nsres = nsIServiceManager_GetServiceByContactID(pServMgr, NS_IOSERVICE_CONTRACTID,
314                 &IID_nsIIOService, (void**)&pIOService);
315         if(NS_FAILED(nsres))
316             ERR("Failed to create nsIOService: %08lx\n", nsres);
317     }
318
319     len = WideCharToMultiByte(CP_ACP, 0, url, -1, NULL, -1, NULL, NULL);
320     urla = HeapAlloc(GetProcessHeap(), 0, len);
321     WideCharToMultiByte(CP_ACP, 0, url, -1, urla, -1, NULL, NULL);
322
323     acstr = nsACString_Create();
324     nsACString_SetData(acstr, urla);
325
326     nsres = nsIIOService_NewURI(pIOService, acstr, NULL, NULL, &ret);
327     if(NS_FAILED(nsres))
328         FIXME("NewURI failed: %08lx\n", nsres);
329
330     nsACString_Destroy(acstr);
331     HeapFree(GetProcessHeap(), 0, urla);
332
333     return ret;
334 }
335
336 void HTMLDocument_NSContainer_Init(HTMLDocument *This)
337 {
338     nsIWebBrowserSetup *wbsetup;
339     nsresult nsres;
340
341     This->nscontainer = NULL;
342
343     if(!load_gecko())
344         return;
345
346     This->nscontainer = HeapAlloc(GetProcessHeap(), 0, sizeof(NSContainer));
347
348     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_WEBBROWSER_CONTRACTID,
349             NULL, &IID_nsIWebBrowser, (void**)&This->nscontainer->webbrowser);
350     if(NS_FAILED(nsres))
351         ERR("Creating WebBrowser failed: %08lx\n", nsres);
352
353     nsres = nsIWebBrowser_QueryInterface(This->nscontainer->webbrowser, &IID_nsIBaseWindow,
354             (void**)&This->nscontainer->window);
355     if(NS_FAILED(nsres))
356         ERR("Could not get nsIBaseWindow interface: %08lx\n", nsres);
357
358     nsres = nsIWebBrowser_QueryInterface(This->nscontainer->webbrowser,
359             &IID_nsIWebBrowserSetup, (void**)&wbsetup);
360     if(NS_SUCCEEDED(nsres)) {
361         nsres = nsIWebBrowserSetup_SetProperty(wbsetup, SETUP_IS_CHROME_WRAPPER, TRUE);
362         if(NS_FAILED(nsres))
363             ERR("SetProperty failed: %08lx\n", nsres);
364         nsIWebBrowserSetup_Release(wbsetup);
365     }else {
366         ERR("Could not get nsIWebBrowserSetup interface\n");
367     }
368
369     nsres = nsIWebBrowser_QueryInterface(This->nscontainer->webbrowser, &IID_nsIWebNavigation,
370             (void**)&This->nscontainer->navigation);
371     if(NS_FAILED(nsres))
372         ERR("Could not get nsIWebNavigation interface: %08lx\n", nsres);
373
374     nsres = nsIWebBrowserStream_QueryInterface(This->nscontainer->webbrowser, &IID_nsIWebBrowserStream,
375             (void**)&This->nscontainer->stream);
376     if(NS_FAILED(nsres))
377         ERR("Could not get nsIWebBrowserStream interface: %08lx\n", nsres);
378
379     if(!nscontainer_class)
380         register_nscontainer_class();
381
382     This->nscontainer->hwnd = CreateWindowExW(0, wszNsContainer, NULL,
383             WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 0, 0, 100, 100,
384             GetDesktopWindow(), NULL, hInst, This);
385
386     nsres = nsIBaseWindow_InitWindow(This->nscontainer->window, This->nscontainer->hwnd, NULL,
387             0, 0, 100, 100);
388     if(NS_SUCCEEDED(nsres)) {
389         nsres = nsIBaseWindow_Create(This->nscontainer->window);
390         if(NS_FAILED(nsres))
391             WARN("Creating window failed: %08lx\n", nsres);
392
393         nsIBaseWindow_SetVisibility(This->nscontainer->window, FALSE);
394         nsIBaseWindow_SetEnabled(This->nscontainer->window, FALSE);
395     }else {
396         ERR("InitWindow failed: %08lx\n", nsres);
397     }
398 }
399
400 void HTMLDocument_NSContainer_Destroy(HTMLDocument *This)
401 {
402     TRACE("(%p)\n", This);
403
404     nsIWebBrowser_Release(This->nscontainer->webbrowser);
405     nsIWebNavigation_Release(This->nscontainer->navigation);
406     nsIBaseWindow_Release(This->nscontainer->window);
407
408     if(This->nscontainer->stream)
409         nsIWebBrowserStream_Release(This->nscontainer->stream);
410
411     HeapFree(GetProcessHeap(), 0, This->nscontainer);
412 }