mshtml: Use wineaddon.cpl to install Gecko.
[wine] / dlls / mshtml / nsembed.c
1 /*
2  * Copyright 2005-2007 Jacek Caban for CodeWeavers
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 "shlobj.h"
31
32 #include "wine/debug.h"
33 #include "wine/unicode.h"
34
35 #include "mshtml_private.h"
36 #include "htmlevent.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
39 WINE_DECLARE_DEBUG_CHANNEL(gecko);
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_MEMORY_CONTRACTID "@mozilla.org/xpcom/memory-service;1"
44 #define NS_COMMANDPARAMS_CONTRACTID "@mozilla.org/embedcomp/command-params;1"
45 #define NS_HTMLSERIALIZER_CONTRACTID "@mozilla.org/layout/contentserializer;1?mimetype=text/html"
46 #define NS_EDITORCONTROLLER_CONTRACTID "@mozilla.org/editor/editorcontroller;1"
47 #define NS_PREFERENCES_CONTRACTID "@mozilla.org/preferences;1"
48
49 #define APPSTARTUP_TOPIC "app-startup"
50
51 #define PR_UINT32_MAX 0xffffffff
52
53 #define NS_STRING_CONTAINER_INIT_DEPEND  0x0002
54 #define NS_CSTRING_CONTAINER_INIT_DEPEND 0x0002
55
56 static nsresult (CDECL *NS_InitXPCOM2)(nsIServiceManager**,void*,void*);
57 static nsresult (CDECL *NS_ShutdownXPCOM)(nsIServiceManager*);
58 static nsresult (CDECL *NS_GetComponentRegistrar)(nsIComponentRegistrar**);
59 static nsresult (CDECL *NS_StringContainerInit2)(nsStringContainer*,const PRUnichar*,PRUint32,PRUint32);
60 static nsresult (CDECL *NS_CStringContainerInit2)(nsCStringContainer*,const char*,PRUint32,PRUint32);
61 static nsresult (CDECL *NS_StringContainerFinish)(nsStringContainer*);
62 static nsresult (CDECL *NS_CStringContainerFinish)(nsCStringContainer*);
63 static nsresult (CDECL *NS_StringSetData)(nsAString*,const PRUnichar*,PRUint32);
64 static nsresult (CDECL *NS_CStringSetData)(nsACString*,const char*,PRUint32);
65 static nsresult (CDECL *NS_NewLocalFile)(const nsAString*,PRBool,nsIFile**);
66 static PRUint32 (CDECL *NS_StringGetData)(const nsAString*,const PRUnichar **,PRBool*);
67 static PRUint32 (CDECL *NS_CStringGetData)(const nsACString*,const char**,PRBool*);
68
69 static HINSTANCE hXPCOM = NULL;
70
71 static nsIServiceManager *pServMgr = NULL;
72 static nsIComponentManager *pCompMgr = NULL;
73 static nsIMemory *nsmem = NULL;
74 static nsIFile *profile_directory;
75
76 static const WCHAR wszNsContainer[] = {'N','s','C','o','n','t','a','i','n','e','r',0};
77
78 static ATOM nscontainer_class;
79 static WCHAR gecko_path[MAX_PATH];
80 static unsigned gecko_path_len;
81
82 static nsresult NSAPI nsDirectoryServiceProvider_QueryInterface(nsIDirectoryServiceProvider *iface,
83         nsIIDRef riid, void **result)
84 {
85     if(IsEqualGUID(&IID_nsISupports, riid)) {
86         TRACE("(IID_nsISupports %p)\n", result);
87         *result = iface;
88     }else if(IsEqualGUID(&IID_nsIDirectoryServiceProvider, riid)) {
89         TRACE("(IID_nsIDirectoryServiceProvider %p)\n", result);
90         *result = iface;
91     }else {
92         WARN("(%s %p)\n", debugstr_guid(riid), result);
93         *result = NULL;
94         return NS_NOINTERFACE;
95     }
96
97     nsISupports_AddRef((nsISupports*)*result);
98     return NS_OK;
99 }
100
101 static nsrefcnt NSAPI nsDirectoryServiceProvider_AddRef(nsIDirectoryServiceProvider *iface)
102 {
103     return 2;
104 }
105
106 static nsrefcnt NSAPI nsDirectoryServiceProvider_Release(nsIDirectoryServiceProvider *iface)
107 {
108     return 1;
109 }
110
111 static nsresult create_profile_directory(void)
112 {
113     static const WCHAR wine_geckoW[] = {'\\','w','i','n','e','_','g','e','c','k','o',0};
114
115     WCHAR path[MAX_PATH + sizeof(wine_geckoW)/sizeof(WCHAR)];
116     nsAString str;
117     PRBool exists;
118     nsresult nsres;
119     HRESULT hres;
120
121     hres = SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_DEFAULT, path);
122     if(FAILED(hres)) {
123         ERR("SHGetFolderPath failed: %08x\n", hres);
124         return NS_ERROR_FAILURE;
125     }
126
127     strcatW(path, wine_geckoW);
128     nsAString_InitDepend(&str, path);
129     nsres = NS_NewLocalFile(&str, FALSE, &profile_directory);
130     nsAString_Finish(&str);
131     if(NS_FAILED(nsres)) {
132         ERR("NS_NewLocalFile failed: %08x\n", nsres);
133         return nsres;
134     }
135
136     nsres = nsIFile_Exists(profile_directory, &exists);
137     if(NS_FAILED(nsres)) {
138         ERR("Exists failed: %08x\n", nsres);
139         return nsres;
140     }
141
142     if(!exists) {
143         nsres = nsIFile_Create(profile_directory, 1, 0700);
144         if(NS_FAILED(nsres))
145             ERR("Create failed: %08x\n", nsres);
146     }
147
148     return nsres;
149 }
150
151 static nsresult NSAPI nsDirectoryServiceProvider_GetFile(nsIDirectoryServiceProvider *iface,
152         const char *prop, PRBool *persistent, nsIFile **_retval)
153 {
154     TRACE("(%s %p %p)\n", debugstr_a(prop), persistent, _retval);
155
156     if(!strcmp(prop, "ProfD")) {
157         if(!profile_directory) {
158             nsresult nsres;
159
160             nsres = create_profile_directory();
161             if(NS_FAILED(nsres))
162                 return nsres;
163         }
164
165         return nsIFile_Clone(profile_directory, _retval);
166     }
167
168     return NS_ERROR_FAILURE;
169 }
170
171 #undef NSWEAKREF_THIS
172
173 static const nsIDirectoryServiceProviderVtbl nsDirectoryServiceProviderVtbl = {
174     nsDirectoryServiceProvider_QueryInterface,
175     nsDirectoryServiceProvider_AddRef,
176     nsDirectoryServiceProvider_Release,
177     nsDirectoryServiceProvider_GetFile
178 };
179
180 static nsIDirectoryServiceProvider nsDirectoryServiceProvider =
181     { &nsDirectoryServiceProviderVtbl };
182
183 static LRESULT WINAPI nsembed_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
184 {
185     NSContainer *This;
186     nsresult nsres;
187
188     static const WCHAR wszTHIS[] = {'T','H','I','S',0};
189
190     if(msg == WM_CREATE) {
191         This = *(NSContainer**)lParam;
192         SetPropW(hwnd, wszTHIS, This);
193     }else {
194         This = GetPropW(hwnd, wszTHIS);
195     }
196
197     switch(msg) {
198     case WM_SIZE:
199         TRACE("(%p)->(WM_SIZE)\n", This);
200
201         nsres = nsIBaseWindow_SetSize(This->window,
202                 LOWORD(lParam), HIWORD(lParam), TRUE);
203         if(NS_FAILED(nsres))
204             WARN("SetSize failed: %08x\n", nsres);
205         break;
206
207     case WM_PARENTNOTIFY:
208         TRACE("WM_PARENTNOTIFY %x\n", (unsigned)wParam);
209
210         switch(wParam) {
211         case WM_LBUTTONDOWN:
212         case WM_RBUTTONDOWN:
213             nsIWebBrowserFocus_Activate(This->focus);
214         }
215     }
216
217     return DefWindowProcW(hwnd, msg, wParam, lParam);
218 }
219
220
221 static void register_nscontainer_class(void)
222 {
223     static WNDCLASSEXW wndclass = {
224         sizeof(WNDCLASSEXW),
225         CS_DBLCLKS,
226         nsembed_proc,
227         0, 0, NULL, NULL, NULL, NULL, NULL,
228         wszNsContainer,
229         NULL,
230     };
231     wndclass.hInstance = hInst;
232     nscontainer_class = RegisterClassExW(&wndclass);
233 }
234
235 static BOOL install_wine_gecko(BOOL silent)
236 {
237     PROCESS_INFORMATION pi;
238     STARTUPINFOW si;
239     WCHAR app[MAX_PATH];
240     WCHAR *args;
241     LONG len;
242     BOOL ret;
243
244     static const WCHAR controlW[] = {'\\','c','o','n','t','r','o','l','.','e','x','e',0};
245     static const WCHAR argsW[] =
246         {' ','a','p','p','w','i','z','.','c','p','l',' ','i','n','s','t','a','l','l','_','g','e','c','k','o',0};
247
248     len = GetSystemDirectoryW(app, MAX_PATH-sizeof(controlW)/sizeof(WCHAR));
249     memcpy(app+len, controlW, sizeof(controlW));
250
251     args = heap_alloc(len*sizeof(WCHAR) + sizeof(controlW) + sizeof(argsW));
252     if(!args)
253         return FALSE;
254
255     memcpy(args, app, len*sizeof(WCHAR) + sizeof(controlW));
256     memcpy(args + len + sizeof(controlW)/sizeof(WCHAR)-1, argsW, sizeof(argsW));
257
258     TRACE("starting %s\n", debugstr_w(args));
259
260     memset(&si, 0, sizeof(si));
261     si.cb = sizeof(si);
262     ret = CreateProcessW(app, args, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
263     heap_free(args);
264     if (ret) {
265         CloseHandle(pi.hThread);
266         WaitForSingleObject(pi.hProcess, INFINITE);
267         CloseHandle(pi.hProcess);
268     }
269
270     return ret;
271 }
272
273 static void set_environment(LPCWSTR gre_path)
274 {
275     WCHAR path_env[MAX_PATH], buf[20];
276     int len, debug_level = 0;
277
278     static const WCHAR pathW[] = {'P','A','T','H',0};
279     static const WCHAR warnW[] = {'w','a','r','n',0};
280     static const WCHAR xpcom_debug_breakW[] =
281         {'X','P','C','O','M','_','D','E','B','U','G','_','B','R','E','A','K',0};
282     static const WCHAR nspr_log_modulesW[] =
283         {'N','S','P','R','_','L','O','G','_','M','O','D','U','L','E','S',0};
284     static const WCHAR debug_formatW[] = {'a','l','l',':','%','d',0};
285     static const WCHAR moz_plugin_pathW[] = {'M','O','Z','_','P','L','U','G','I','N','_','P','A','T','H',0};
286     static const WCHAR gecko_pluginW[] = {'\\','g','e','c','k','o','\\','p','l','u','g','i','n',0};
287
288     /* We have to modify PATH as XPCOM loads other DLLs from this directory. */
289     GetEnvironmentVariableW(pathW, path_env, sizeof(path_env)/sizeof(WCHAR));
290     len = strlenW(path_env);
291     path_env[len++] = ';';
292     strcpyW(path_env+len, gre_path);
293     SetEnvironmentVariableW(pathW, path_env);
294
295     SetEnvironmentVariableW(xpcom_debug_breakW, warnW);
296
297     if(TRACE_ON(gecko))
298         debug_level = 5;
299     else if(WARN_ON(gecko))
300         debug_level = 3;
301     else if(ERR_ON(gecko))
302         debug_level = 2;
303
304     sprintfW(buf, debug_formatW, debug_level);
305     SetEnvironmentVariableW(nspr_log_modulesW, buf);
306
307     len = GetSystemDirectoryW(path_env, (sizeof(path_env)-sizeof(gecko_pluginW))/sizeof(WCHAR)+1);
308     if(len) {
309         strcpyW(path_env+len, gecko_pluginW);
310         SetEnvironmentVariableW(moz_plugin_pathW, path_env);
311     }
312 }
313
314 static BOOL load_xpcom(const PRUnichar *gre_path)
315 {
316     static const WCHAR strXPCOM[] = {'x','p','c','o','m','.','d','l','l',0};
317
318     TRACE("(%s)\n", debugstr_w(gre_path));
319
320     set_environment(gre_path);
321
322     hXPCOM = LoadLibraryW(strXPCOM);
323     if(!hXPCOM) {
324         WARN("Could not load XPCOM: %d\n", GetLastError());
325         return FALSE;
326     }
327
328 #define NS_DLSYM(func) \
329     func = (void *)GetProcAddress(hXPCOM, #func); \
330     if(!func) \
331         ERR("Could not GetProcAddress(" #func ") failed\n")
332
333     NS_DLSYM(NS_InitXPCOM2);
334     NS_DLSYM(NS_ShutdownXPCOM);
335     NS_DLSYM(NS_GetComponentRegistrar);
336     NS_DLSYM(NS_StringContainerInit2);
337     NS_DLSYM(NS_CStringContainerInit2);
338     NS_DLSYM(NS_StringContainerFinish);
339     NS_DLSYM(NS_CStringContainerFinish);
340     NS_DLSYM(NS_StringSetData);
341     NS_DLSYM(NS_CStringSetData);
342     NS_DLSYM(NS_NewLocalFile);
343     NS_DLSYM(NS_StringGetData);
344     NS_DLSYM(NS_CStringGetData);
345
346 #undef NS_DLSYM
347
348     return TRUE;
349 }
350
351 static BOOL check_version(LPCWSTR gre_path, const char *version_string)
352 {
353     WCHAR file_name[MAX_PATH];
354     char version[128];
355     DWORD read=0;
356     HANDLE hfile;
357
358     static const WCHAR wszVersion[] = {'\\','V','E','R','S','I','O','N',0};
359
360     strcpyW(file_name, gre_path);
361     strcatW(file_name, wszVersion);
362
363     hfile = CreateFileW(file_name, GENERIC_READ, FILE_SHARE_READ, NULL,
364                         OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
365     if(hfile == INVALID_HANDLE_VALUE) {
366         ERR("Could not open VERSION file\n");
367         return FALSE;
368     }
369
370     ReadFile(hfile, version, sizeof(version), &read, NULL);
371     version[read] = 0;
372     CloseHandle(hfile);
373
374     TRACE("%s\n", debugstr_a(version));
375
376     if(strcmp(version, version_string)) {
377         ERR("Unexpected version %s, expected %s\n", debugstr_a(version),
378             debugstr_a(version_string));
379         return FALSE;
380     }
381
382     return TRUE;
383 }
384
385 static BOOL load_wine_gecko_v(PRUnichar *gre_path, HKEY mshtml_key,
386         const char *version, const char *version_string)
387 {
388     DWORD res, type, size = MAX_PATH;
389     HKEY hkey = mshtml_key;
390
391     static const WCHAR wszGeckoPath[] =
392         {'G','e','c','k','o','P','a','t','h',0};
393
394     if(version) {
395         /* @@ Wine registry key: HKCU\Software\Wine\MSHTML\<version> */
396         res = RegOpenKeyA(mshtml_key, version, &hkey);
397         if(res != ERROR_SUCCESS)
398             return FALSE;
399     }
400
401     res = RegQueryValueExW(hkey, wszGeckoPath, NULL, &type, (LPBYTE)gre_path, &size);
402     if(hkey != mshtml_key)
403         RegCloseKey(hkey);
404     if(res != ERROR_SUCCESS || type != REG_SZ)
405         return FALSE;
406
407     if(!check_version(gre_path, version_string))
408         return FALSE;
409
410     return load_xpcom(gre_path);
411 }
412
413 static BOOL load_wine_gecko(PRUnichar *gre_path)
414 {
415     HKEY hkey;
416     DWORD res;
417     BOOL ret;
418
419     static const WCHAR wszMshtmlKey[] = {
420         'S','o','f','t','w','a','r','e','\\','W','i','n','e',
421         '\\','M','S','H','T','M','L',0};
422
423     /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
424     res = RegOpenKeyW(HKEY_CURRENT_USER, wszMshtmlKey, &hkey);
425     if(res != ERROR_SUCCESS)
426         return FALSE;
427
428     ret = load_wine_gecko_v(gre_path, hkey, GECKO_VERSION, GECKO_VERSION_STRING);
429
430     RegCloseKey(hkey);
431     return ret;
432 }
433
434 static void set_bool_pref(nsIPrefBranch *pref, const char *pref_name, BOOL val)
435 {
436     nsresult nsres;
437
438     nsres = nsIPrefBranch_SetBoolPref(pref, pref_name, val);
439     if(NS_FAILED(nsres))
440         ERR("Could not set pref %s\n", debugstr_a(pref_name));
441 }
442
443 static void set_int_pref(nsIPrefBranch *pref, const char *pref_name, int val)
444 {
445     nsresult nsres;
446
447     nsres = nsIPrefBranch_SetIntPref(pref, pref_name, val);
448     if(NS_FAILED(nsres))
449         ERR("Could not set pref %s\n", debugstr_a(pref_name));
450 }
451
452 static void set_string_pref(nsIPrefBranch *pref, const char *pref_name, const char *val)
453 {
454     nsresult nsres;
455
456     nsres = nsIPrefBranch_SetCharPref(pref, pref_name, val);
457     if(NS_FAILED(nsres))
458         ERR("Could not set pref %s\n", debugstr_a(pref_name));
459 }
460
461 static void set_lang(nsIPrefBranch *pref)
462 {
463     char langs[100];
464     DWORD res, size, type;
465     HKEY hkey;
466
467     static const WCHAR international_keyW[] =
468         {'S','o','f','t','w','a','r','e',
469          '\\','M','i','c','r','o','s','o','f','t',
470          '\\','I','n','t','e','r','n','e','t',' ','E','x','p','l','o','r','e','r',
471          '\\','I','n','t','e','r','n','a','t','i','o','n','a','l',0};
472
473     res = RegOpenKeyW(HKEY_CURRENT_USER, international_keyW, &hkey);
474     if(res != ERROR_SUCCESS)
475         return;
476
477     size = sizeof(langs);
478     res = RegQueryValueExA(hkey, "AcceptLanguage", 0, &type, (LPBYTE)langs, &size);
479     RegCloseKey(hkey);
480     if(res != ERROR_SUCCESS || type != REG_SZ)
481         return;
482
483     TRACE("Setting lang %s\n", debugstr_a(langs));
484
485     set_string_pref(pref, "intl.accept_languages", langs);
486 }
487
488 static void set_proxy(nsIPrefBranch *pref)
489 {
490     char proxy[512];
491     char * proxy_port;
492     int proxy_port_num;
493     DWORD enabled = 0, res, size, type;
494     HKEY hkey;
495
496     static const WCHAR proxy_keyW[] =
497         {'S','o','f','t','w','a','r','e',
498          '\\','M','i','c','r','o','s','o','f','t',
499          '\\','W','i','n','d','o','w','s',
500          '\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n',
501          '\\','I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s',0};
502
503     res = RegOpenKeyW(HKEY_CURRENT_USER, proxy_keyW, &hkey);
504     if(res != ERROR_SUCCESS)
505         return;
506
507     size = sizeof(enabled);
508     res = RegQueryValueExA(hkey, "ProxyEnable", 0, &type, (LPBYTE)&enabled, &size);
509     if(res != ERROR_SUCCESS || type != REG_DWORD || enabled == 0)
510     {
511         RegCloseKey(hkey);
512         return;
513     }
514
515     size = sizeof(proxy);
516     res = RegQueryValueExA(hkey, "ProxyServer", 0, &type, (LPBYTE)proxy, &size);
517     RegCloseKey(hkey);
518     if(res != ERROR_SUCCESS || type != REG_SZ)
519         return;
520
521     proxy_port = strchr(proxy, ':');
522     if (!proxy_port)
523         return;
524
525     *proxy_port = 0;
526     proxy_port_num = atoi(proxy_port + 1);
527     TRACE("Setting proxy to %s, port %d\n", debugstr_a(proxy), proxy_port_num);
528
529     set_string_pref(pref, "network.proxy.http", proxy);
530     set_string_pref(pref, "network.proxy.ssl", proxy);
531
532     set_int_pref(pref, "network.proxy.type", 1);
533     set_int_pref(pref, "network.proxy.http_port", proxy_port_num);
534     set_int_pref(pref, "network.proxy.ssl_port", proxy_port_num);
535 }
536
537 static void set_preferences(void)
538 {
539     nsIPrefBranch *pref;
540     nsresult nsres;
541
542     nsres = nsIServiceManager_GetServiceByContractID(pServMgr, NS_PREFERENCES_CONTRACTID,
543             &IID_nsIPrefBranch, (void**)&pref);
544     if(NS_FAILED(nsres)) {
545         ERR("Could not get preference service: %08x\n", nsres);
546         return;
547     }
548
549     set_lang(pref);
550     set_proxy(pref);
551     set_bool_pref(pref, "security.warn_entering_secure", FALSE);
552     set_bool_pref(pref, "security.warn_submit_insecure", FALSE);
553     set_int_pref(pref, "layout.spellcheckDefault", 0);
554
555     nsIPrefBranch_Release(pref);
556 }
557
558 static BOOL init_xpcom(const PRUnichar *gre_path)
559 {
560     nsresult nsres;
561     nsIObserver *pStartNotif;
562     nsIComponentRegistrar *registrar = NULL;
563     nsAString path;
564     nsIFile *gre_dir;
565     WCHAR *ptr;
566
567     nsAString_InitDepend(&path, gre_path);
568     nsres = NS_NewLocalFile(&path, FALSE, &gre_dir);
569     nsAString_Finish(&path);
570     if(NS_FAILED(nsres)) {
571         ERR("NS_NewLocalFile failed: %08x\n", nsres);
572         FreeLibrary(hXPCOM);
573         return FALSE;
574     }
575
576     nsres = NS_InitXPCOM2(&pServMgr, gre_dir, &nsDirectoryServiceProvider);
577     if(NS_FAILED(nsres)) {
578         ERR("NS_InitXPCOM2 failed: %08x\n", nsres);
579         FreeLibrary(hXPCOM);
580         return FALSE;
581     }
582
583     strcpyW(gecko_path, gre_path);
584     for(ptr = gecko_path; *ptr; ptr++) {
585         if(*ptr == '\\')
586             *ptr = '/';
587     }
588     gecko_path_len = ptr-gecko_path;
589
590     nsres = nsIServiceManager_QueryInterface(pServMgr, &IID_nsIComponentManager, (void**)&pCompMgr);
591     if(NS_FAILED(nsres))
592         ERR("Could not get nsIComponentManager: %08x\n", nsres);
593
594     nsres = NS_GetComponentRegistrar(&registrar);
595     if(NS_SUCCEEDED(nsres))
596         init_nsio(pCompMgr, registrar);
597     else
598         ERR("NS_GetComponentRegistrar failed: %08x\n", nsres);
599
600     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_APPSTARTUPNOTIFIER_CONTRACTID,
601             NULL, &IID_nsIObserver, (void**)&pStartNotif);
602     if(NS_SUCCEEDED(nsres)) {
603         nsres = nsIObserver_Observe(pStartNotif, NULL, APPSTARTUP_TOPIC, NULL);
604         if(NS_FAILED(nsres))
605             ERR("Observe failed: %08x\n", nsres);
606
607         nsIObserver_Release(pStartNotif);
608     }else {
609         ERR("could not get appstartup-notifier: %08x\n", nsres);
610     }
611
612     set_preferences();
613
614     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_MEMORY_CONTRACTID,
615             NULL, &IID_nsIMemory, (void**)&nsmem);
616     if(NS_FAILED(nsres))
617         ERR("Could not get nsIMemory: %08x\n", nsres);
618
619     if(registrar) {
620         register_nsservice(registrar, pServMgr);
621         nsIComponentRegistrar_Release(registrar);
622     }
623
624     return TRUE;
625 }
626
627 static CRITICAL_SECTION cs_load_gecko;
628 static CRITICAL_SECTION_DEBUG cs_load_gecko_dbg =
629 {
630     0, 0, &cs_load_gecko,
631     { &cs_load_gecko_dbg.ProcessLocksList, &cs_load_gecko_dbg.ProcessLocksList },
632       0, 0, { (DWORD_PTR)(__FILE__ ": load_gecko") }
633 };
634 static CRITICAL_SECTION cs_load_gecko = { &cs_load_gecko_dbg, -1, 0, 0, 0, 0 };
635
636 BOOL load_gecko(BOOL silent)
637 {
638     PRUnichar gre_path[MAX_PATH];
639     BOOL ret = FALSE;
640
641     static DWORD loading_thread;
642
643     TRACE("()\n");
644
645     /* load_gecko may be called recursively */
646     if(loading_thread == GetCurrentThreadId())
647         return pCompMgr != NULL;
648
649     EnterCriticalSection(&cs_load_gecko);
650
651     if(!loading_thread) {
652         loading_thread = GetCurrentThreadId();
653
654         if(load_wine_gecko(gre_path)
655            || (install_wine_gecko(silent) && load_wine_gecko(gre_path)))
656             ret = init_xpcom(gre_path);
657         else
658            MESSAGE("Could not load wine-gecko. HTML rendering will be disabled.\n");
659     }else {
660         ret = pCompMgr != NULL;
661     }
662
663     LeaveCriticalSection(&cs_load_gecko);
664
665     return ret;
666 }
667
668 void *nsalloc(size_t size)
669 {
670     return nsIMemory_Alloc(nsmem, size);
671 }
672
673 void nsfree(void *mem)
674 {
675     nsIMemory_Free(nsmem, mem);
676 }
677
678 static BOOL nsACString_Init(nsACString *str, const char *data)
679 {
680     return NS_SUCCEEDED(NS_CStringContainerInit2(str, data, PR_UINT32_MAX, 0));
681 }
682
683 /*
684  * Initializes nsACString with data owned by caller.
685  * Caller must ensure that data is valid during lifetime of string object.
686  */
687 void nsACString_InitDepend(nsACString *str, const char *data)
688 {
689     NS_CStringContainerInit2(str, data, PR_UINT32_MAX, NS_CSTRING_CONTAINER_INIT_DEPEND);
690 }
691
692 void nsACString_SetData(nsACString *str, const char *data)
693 {
694     NS_CStringSetData(str, data, PR_UINT32_MAX);
695 }
696
697 PRUint32 nsACString_GetData(const nsACString *str, const char **data)
698 {
699     return NS_CStringGetData(str, data, NULL);
700 }
701
702 void nsACString_Finish(nsACString *str)
703 {
704     NS_CStringContainerFinish(str);
705 }
706
707 BOOL nsAString_Init(nsAString *str, const PRUnichar *data)
708 {
709     return NS_SUCCEEDED(NS_StringContainerInit2(str, data, PR_UINT32_MAX, 0));
710 }
711
712 /*
713  * Initializes nsAString with data owned by caller.
714  * Caller must ensure that data is valid during lifetime of string object.
715  */
716 void nsAString_InitDepend(nsAString *str, const PRUnichar *data)
717 {
718     NS_StringContainerInit2(str, data, PR_UINT32_MAX, NS_STRING_CONTAINER_INIT_DEPEND);
719 }
720
721 void nsAString_SetData(nsAString *str, const PRUnichar *data)
722 {
723     NS_StringSetData(str, data, PR_UINT32_MAX);
724 }
725
726 PRUint32 nsAString_GetData(const nsAString *str, const PRUnichar **data)
727 {
728     return NS_StringGetData(str, data, NULL);
729 }
730
731 void nsAString_Finish(nsAString *str)
732 {
733     NS_StringContainerFinish(str);
734 }
735
736 nsICommandParams *create_nscommand_params(void)
737 {
738     nsICommandParams *ret = NULL;
739     nsresult nsres;
740
741     if(!pCompMgr)
742         return NULL;
743
744     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
745             NS_COMMANDPARAMS_CONTRACTID, NULL, &IID_nsICommandParams,
746             (void**)&ret);
747     if(NS_FAILED(nsres))
748         ERR("Could not get nsICommandParams\n");
749
750     return ret;
751 }
752
753 nsresult get_nsinterface(nsISupports *iface, REFIID riid, void **ppv)
754 {
755     nsIInterfaceRequestor *iface_req;
756     nsresult nsres;
757
758     nsres = nsISupports_QueryInterface(iface, &IID_nsIInterfaceRequestor, (void**)&iface_req);
759     if(NS_FAILED(nsres))
760         return nsres;
761
762     nsres = nsIInterfaceRequestor_GetInterface(iface_req, riid, ppv);
763     nsIInterfaceRequestor_Release(iface_req);
764
765     return nsres;
766 }
767
768 static HRESULT nsnode_to_nsstring_rec(nsIContentSerializer *serializer, nsIDOMNode *nsnode, nsAString *str)
769 {
770     nsIDOMNodeList *node_list = NULL;
771     PRBool has_children = FALSE;
772     nsIContent *nscontent;
773     PRUint16 type;
774     nsresult nsres;
775
776     nsIDOMNode_HasChildNodes(nsnode, &has_children);
777
778     nsres = nsIDOMNode_GetNodeType(nsnode, &type);
779     if(NS_FAILED(nsres)) {
780         ERR("GetType failed: %08x\n", nsres);
781         return E_FAIL;
782     }
783
784     nsres = nsIDOMNode_QueryInterface(nsnode, &IID_nsIContent, (void**)&nscontent);
785     if(NS_FAILED(nsres)) {
786         ERR("Could not get nsIDontent interface: %08x\n", nsres);
787         return E_FAIL;
788     }
789
790     switch(type) {
791     case ELEMENT_NODE:
792         nsIContentSerializer_AppendElementStart(serializer, nscontent, nscontent, str);
793         break;
794     case TEXT_NODE:
795         nsIContentSerializer_AppendText(serializer, nscontent, 0, -1, str);
796         break;
797     case COMMENT_NODE:
798         nsres = nsIContentSerializer_AppendComment(serializer, nscontent, 0, -1, str);
799         break;
800     case DOCUMENT_NODE: {
801         nsIDocument *nsdoc;
802         nsIDOMNode_QueryInterface(nsnode, &IID_nsIDocument, (void**)&nsdoc);
803         nsIContentSerializer_AppendDocumentStart(serializer, nsdoc, str);
804         nsIDocument_Release(nsdoc);
805         break;
806     }
807     case DOCUMENT_TYPE_NODE:
808         WARN("Ignoring DOCUMENT_TYPE_NODE\n");
809         break;
810     case DOCUMENT_FRAGMENT_NODE:
811         break;
812     default:
813         FIXME("Unhandled type %u\n", type);
814     }
815
816     if(has_children) {
817         PRUint32 child_cnt, i;
818         nsIDOMNode *child_node;
819
820         nsIDOMNode_GetChildNodes(nsnode, &node_list);
821         nsIDOMNodeList_GetLength(node_list, &child_cnt);
822
823         for(i=0; i<child_cnt; i++) {
824             nsres = nsIDOMNodeList_Item(node_list, i, &child_node);
825             if(NS_SUCCEEDED(nsres)) {
826                 nsnode_to_nsstring_rec(serializer, child_node, str);
827                 nsIDOMNode_Release(child_node);
828             }else {
829                 ERR("Item failed: %08x\n", nsres);
830             }
831         }
832
833         nsIDOMNodeList_Release(node_list);
834     }
835
836     if(type == ELEMENT_NODE)
837         nsIContentSerializer_AppendElementEnd(serializer, nscontent, str);
838
839     nsIContent_Release(nscontent);
840     return S_OK;
841 }
842
843 HRESULT nsnode_to_nsstring(nsIDOMNode *nsnode, nsAString *str)
844 {
845     nsIContentSerializer *serializer;
846     nsresult nsres;
847     HRESULT hres;
848
849     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
850             NS_HTMLSERIALIZER_CONTRACTID, NULL, &IID_nsIContentSerializer,
851             (void**)&serializer);
852     if(NS_FAILED(nsres)) {
853         ERR("Could not get nsIContentSerializer: %08x\n", nsres);
854         return E_FAIL;
855     }
856
857     nsres = nsIContentSerializer_Init(serializer, 0, 100, NULL, FALSE, FALSE /* FIXME */);
858     if(NS_FAILED(nsres))
859         ERR("Init failed: %08x\n", nsres);
860
861     hres = nsnode_to_nsstring_rec(serializer, nsnode, str);
862     if(SUCCEEDED(hres)) {
863         nsres = nsIContentSerializer_Flush(serializer, str);
864         if(NS_FAILED(nsres))
865             ERR("Flush failed: %08x\n", nsres);
866     }
867
868     nsIContentSerializer_Release(serializer);
869     return hres;
870 }
871
872 void get_editor_controller(NSContainer *This)
873 {
874     nsIEditingSession *editing_session = NULL;
875     nsIControllerContext *ctrlctx;
876     nsresult nsres;
877
878     if(This->editor) {
879         nsIEditor_Release(This->editor);
880         This->editor = NULL;
881     }
882
883     if(This->editor_controller) {
884         nsIController_Release(This->editor_controller);
885         This->editor_controller = NULL;
886     }
887
888     nsres = get_nsinterface((nsISupports*)This->webbrowser, &IID_nsIEditingSession,
889             (void**)&editing_session);
890     if(NS_FAILED(nsres)) {
891         ERR("Could not get nsIEditingSession: %08x\n", nsres);
892         return;
893     }
894
895     nsres = nsIEditingSession_GetEditorForWindow(editing_session,
896             This->doc->basedoc.window->nswindow, &This->editor);
897     nsIEditingSession_Release(editing_session);
898     if(NS_FAILED(nsres)) {
899         ERR("Could not get editor: %08x\n", nsres);
900         return;
901     }
902
903     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
904             NS_EDITORCONTROLLER_CONTRACTID, NULL, &IID_nsIControllerContext, (void**)&ctrlctx);
905     if(NS_SUCCEEDED(nsres)) {
906         nsres = nsIControllerContext_SetCommandContext(ctrlctx, (nsISupports *)This->editor);
907         if(NS_FAILED(nsres))
908             ERR("SetCommandContext failed: %08x\n", nsres);
909         nsres = nsIControllerContext_QueryInterface(ctrlctx, &IID_nsIController,
910                 (void**)&This->editor_controller);
911         nsIControllerContext_Release(ctrlctx);
912         if(NS_FAILED(nsres))
913             ERR("Could not get nsIController interface: %08x\n", nsres);
914     }else {
915         ERR("Could not create edit controller: %08x\n", nsres);
916     }
917 }
918
919 void close_gecko(void)
920 {
921     TRACE("()\n");
922
923     release_nsio();
924
925     if(profile_directory) {
926         nsIFile_Release(profile_directory);
927         profile_directory = NULL;
928     }
929
930     if(pCompMgr)
931         nsIComponentManager_Release(pCompMgr);
932
933     if(pServMgr)
934         nsIServiceManager_Release(pServMgr);
935
936     if(nsmem)
937         nsIMemory_Release(nsmem);
938
939     /* Gecko doesn't really support being unloaded */
940     /* if (hXPCOM) FreeLibrary(hXPCOM); */
941 }
942
943 BOOL is_gecko_path(const char *path)
944 {
945     WCHAR *buf, *ptr;
946     BOOL ret;
947
948     buf = heap_strdupAtoW(path);
949     if(strlenW(buf) < gecko_path_len)
950         return FALSE;
951
952     buf[gecko_path_len] = 0;
953     for(ptr = buf; *ptr; ptr++) {
954         if(*ptr == '\\')
955             *ptr = '/';
956     }
957
958     ret = !strcmpiW(buf, gecko_path);
959     heap_free(buf);
960     return ret;
961 }
962
963 /**********************************************************
964  *      nsIWebBrowserChrome interface
965  */
966
967 #define NSWBCHROME_THIS(iface) DEFINE_THIS(NSContainer, WebBrowserChrome, iface)
968
969 static nsresult NSAPI nsWebBrowserChrome_QueryInterface(nsIWebBrowserChrome *iface,
970         nsIIDRef riid, void **result)
971 {
972     NSContainer *This = NSWBCHROME_THIS(iface);
973
974     *result = NULL;
975     if(IsEqualGUID(&IID_nsISupports, riid)) {
976         TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
977         *result = NSWBCHROME(This);
978     }else if(IsEqualGUID(&IID_nsIWebBrowserChrome, riid)) {
979         TRACE("(%p)->(IID_nsIWebBrowserChrome, %p)\n", This, result);
980         *result = NSWBCHROME(This);
981     }else if(IsEqualGUID(&IID_nsIContextMenuListener, riid)) {
982         TRACE("(%p)->(IID_nsIContextMenuListener, %p)\n", This, result);
983         *result = NSCML(This);
984     }else if(IsEqualGUID(&IID_nsIURIContentListener, riid)) {
985         TRACE("(%p)->(IID_nsIURIContentListener %p)\n", This, result);
986         *result = NSURICL(This);
987     }else if(IsEqualGUID(&IID_nsIEmbeddingSiteWindow, riid)) {
988         TRACE("(%p)->(IID_nsIEmbeddingSiteWindow %p)\n", This, result);
989         *result = NSEMBWNDS(This);
990     }else if(IsEqualGUID(&IID_nsITooltipListener, riid)) {
991         TRACE("(%p)->(IID_nsITooltipListener %p)\n", This, result);
992         *result = NSTOOLTIP(This);
993     }else if(IsEqualGUID(&IID_nsIInterfaceRequestor, riid)) {
994         TRACE("(%p)->(IID_nsIInterfaceRequestor %p)\n", This, result);
995         *result = NSIFACEREQ(This);
996     }else if(IsEqualGUID(&IID_nsIWeakReference, riid)) {
997         TRACE("(%p)->(IID_nsIWeakReference %p)\n", This, result);
998         *result = NSWEAKREF(This);
999     }else if(IsEqualGUID(&IID_nsISupportsWeakReference, riid)) {
1000         TRACE("(%p)->(IID_nsISupportsWeakReference %p)\n", This, result);
1001         *result = NSSUPWEAKREF(This);
1002     }
1003
1004     if(*result) {
1005         nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1006         return NS_OK;
1007     }
1008
1009     TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
1010     return NS_NOINTERFACE;
1011 }
1012
1013 static nsrefcnt NSAPI nsWebBrowserChrome_AddRef(nsIWebBrowserChrome *iface)
1014 {
1015     NSContainer *This = NSWBCHROME_THIS(iface);
1016     LONG ref = InterlockedIncrement(&This->ref);
1017
1018     TRACE("(%p) ref=%d\n", This, ref);
1019
1020     return ref;
1021 }
1022
1023 static nsrefcnt NSAPI nsWebBrowserChrome_Release(nsIWebBrowserChrome *iface)
1024 {
1025     NSContainer *This = NSWBCHROME_THIS(iface);
1026     LONG ref = InterlockedDecrement(&This->ref);
1027
1028     TRACE("(%p) ref=%d\n", This, ref);
1029
1030     if(!ref) {
1031         if(This->parent)
1032             nsIWebBrowserChrome_Release(NSWBCHROME(This->parent));
1033         heap_free(This);
1034     }
1035
1036     return ref;
1037 }
1038
1039 static nsresult NSAPI nsWebBrowserChrome_SetStatus(nsIWebBrowserChrome *iface,
1040         PRUint32 statusType, const PRUnichar *status)
1041 {
1042     NSContainer *This = NSWBCHROME_THIS(iface);
1043     TRACE("(%p)->(%d %s)\n", This, statusType, debugstr_w(status));
1044     return NS_OK;
1045 }
1046
1047 static nsresult NSAPI nsWebBrowserChrome_GetWebBrowser(nsIWebBrowserChrome *iface,
1048         nsIWebBrowser **aWebBrowser)
1049 {
1050     NSContainer *This = NSWBCHROME_THIS(iface);
1051
1052     TRACE("(%p)->(%p)\n", This, aWebBrowser);
1053
1054     if(!aWebBrowser)
1055         return NS_ERROR_INVALID_ARG;
1056
1057     if(This->webbrowser)
1058         nsIWebBrowser_AddRef(This->webbrowser);
1059     *aWebBrowser = This->webbrowser;
1060     return S_OK;
1061 }
1062
1063 static nsresult NSAPI nsWebBrowserChrome_SetWebBrowser(nsIWebBrowserChrome *iface,
1064         nsIWebBrowser *aWebBrowser)
1065 {
1066     NSContainer *This = NSWBCHROME_THIS(iface);
1067
1068     TRACE("(%p)->(%p)\n", This, aWebBrowser);
1069
1070     if(aWebBrowser != This->webbrowser)
1071         ERR("Wrong nsWebBrowser!\n");
1072
1073     return NS_OK;
1074 }
1075
1076 static nsresult NSAPI nsWebBrowserChrome_GetChromeFlags(nsIWebBrowserChrome *iface,
1077         PRUint32 *aChromeFlags)
1078 {
1079     NSContainer *This = NSWBCHROME_THIS(iface);
1080     WARN("(%p)->(%p)\n", This, aChromeFlags);
1081     return NS_ERROR_NOT_IMPLEMENTED;
1082 }
1083
1084 static nsresult NSAPI nsWebBrowserChrome_SetChromeFlags(nsIWebBrowserChrome *iface,
1085         PRUint32 aChromeFlags)
1086 {
1087     NSContainer *This = NSWBCHROME_THIS(iface);
1088     WARN("(%p)->(%08x)\n", This, aChromeFlags);
1089     return NS_ERROR_NOT_IMPLEMENTED;
1090 }
1091
1092 static nsresult NSAPI nsWebBrowserChrome_DestroyBrowserWindow(nsIWebBrowserChrome *iface)
1093 {
1094     NSContainer *This = NSWBCHROME_THIS(iface);
1095     TRACE("(%p)\n", This);
1096     return NS_ERROR_NOT_IMPLEMENTED;
1097 }
1098
1099 static nsresult NSAPI nsWebBrowserChrome_SizeBrowserTo(nsIWebBrowserChrome *iface,
1100         PRInt32 aCX, PRInt32 aCY)
1101 {
1102     NSContainer *This = NSWBCHROME_THIS(iface);
1103     WARN("(%p)->(%d %d)\n", This, aCX, aCY);
1104     return NS_ERROR_NOT_IMPLEMENTED;
1105 }
1106
1107 static nsresult NSAPI nsWebBrowserChrome_ShowAsModal(nsIWebBrowserChrome *iface)
1108 {
1109     NSContainer *This = NSWBCHROME_THIS(iface);
1110     WARN("(%p)\n", This);
1111     return NS_ERROR_NOT_IMPLEMENTED;
1112 }
1113
1114 static nsresult NSAPI nsWebBrowserChrome_IsWindowModal(nsIWebBrowserChrome *iface, PRBool *_retval)
1115 {
1116     NSContainer *This = NSWBCHROME_THIS(iface);
1117     WARN("(%p)->(%p)\n", This, _retval);
1118     return NS_ERROR_NOT_IMPLEMENTED;
1119 }
1120
1121 static nsresult NSAPI nsWebBrowserChrome_ExitModalEventLoop(nsIWebBrowserChrome *iface,
1122         nsresult aStatus)
1123 {
1124     NSContainer *This = NSWBCHROME_THIS(iface);
1125     WARN("(%p)->(%08x)\n", This, aStatus);
1126     return NS_ERROR_NOT_IMPLEMENTED;
1127 }
1128
1129 #undef NSWBCHROME_THIS
1130
1131 static const nsIWebBrowserChromeVtbl nsWebBrowserChromeVtbl = {
1132     nsWebBrowserChrome_QueryInterface,
1133     nsWebBrowserChrome_AddRef,
1134     nsWebBrowserChrome_Release,
1135     nsWebBrowserChrome_SetStatus,
1136     nsWebBrowserChrome_GetWebBrowser,
1137     nsWebBrowserChrome_SetWebBrowser,
1138     nsWebBrowserChrome_GetChromeFlags,
1139     nsWebBrowserChrome_SetChromeFlags,
1140     nsWebBrowserChrome_DestroyBrowserWindow,
1141     nsWebBrowserChrome_SizeBrowserTo,
1142     nsWebBrowserChrome_ShowAsModal,
1143     nsWebBrowserChrome_IsWindowModal,
1144     nsWebBrowserChrome_ExitModalEventLoop
1145 };
1146
1147 /**********************************************************
1148  *      nsIContextMenuListener interface
1149  */
1150
1151 #define NSCML_THIS(iface) DEFINE_THIS(NSContainer, ContextMenuListener, iface)
1152
1153 static nsresult NSAPI nsContextMenuListener_QueryInterface(nsIContextMenuListener *iface,
1154         nsIIDRef riid, void **result)
1155 {
1156     NSContainer *This = NSCML_THIS(iface);
1157     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1158 }
1159
1160 static nsrefcnt NSAPI nsContextMenuListener_AddRef(nsIContextMenuListener *iface)
1161 {
1162     NSContainer *This = NSCML_THIS(iface);
1163     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1164 }
1165
1166 static nsrefcnt NSAPI nsContextMenuListener_Release(nsIContextMenuListener *iface)
1167 {
1168     NSContainer *This = NSCML_THIS(iface);
1169     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1170 }
1171
1172 static nsresult NSAPI nsContextMenuListener_OnShowContextMenu(nsIContextMenuListener *iface,
1173         PRUint32 aContextFlags, nsIDOMEvent *aEvent, nsIDOMNode *aNode)
1174 {
1175     NSContainer *This = NSCML_THIS(iface);
1176     nsIDOMMouseEvent *event;
1177     HTMLDOMNode *node;
1178     POINT pt;
1179     DWORD dwID = CONTEXT_MENU_DEFAULT;
1180     nsresult nsres;
1181     HRESULT hres;
1182
1183     TRACE("(%p)->(%08x %p %p)\n", This, aContextFlags, aEvent, aNode);
1184
1185     fire_event(This->doc->basedoc.doc_node /* FIXME */, EVENTID_CONTEXTMENU, TRUE, aNode, aEvent);
1186
1187     nsres = nsIDOMEvent_QueryInterface(aEvent, &IID_nsIDOMMouseEvent, (void**)&event);
1188     if(NS_FAILED(nsres)) {
1189         ERR("Could not get nsIDOMMouseEvent interface: %08x\n", nsres);
1190         return nsres;
1191     }
1192
1193     nsIDOMMouseEvent_GetScreenX(event, &pt.x);
1194     nsIDOMMouseEvent_GetScreenY(event, &pt.y);
1195     nsIDOMMouseEvent_Release(event);
1196
1197     switch(aContextFlags) {
1198     case CONTEXT_NONE:
1199     case CONTEXT_DOCUMENT:
1200     case CONTEXT_TEXT:
1201         dwID = CONTEXT_MENU_DEFAULT;
1202         break;
1203     case CONTEXT_IMAGE:
1204     case CONTEXT_IMAGE|CONTEXT_LINK:
1205         dwID = CONTEXT_MENU_IMAGE;
1206         break;
1207     case CONTEXT_LINK:
1208         dwID = CONTEXT_MENU_ANCHOR;
1209         break;
1210     case CONTEXT_INPUT:
1211         dwID = CONTEXT_MENU_CONTROL;
1212         break;
1213     default:
1214         FIXME("aContextFlags=%08x\n", aContextFlags);
1215     };
1216
1217     hres = get_node(This->doc->basedoc.doc_node, aNode, TRUE, &node);
1218     if(FAILED(hres))
1219         return NS_ERROR_FAILURE;
1220
1221     show_context_menu(This->doc, dwID, &pt, (IDispatch*)HTMLDOMNODE(node));
1222     return NS_OK;
1223 }
1224
1225 #undef NSCML_THIS
1226
1227 static const nsIContextMenuListenerVtbl nsContextMenuListenerVtbl = {
1228     nsContextMenuListener_QueryInterface,
1229     nsContextMenuListener_AddRef,
1230     nsContextMenuListener_Release,
1231     nsContextMenuListener_OnShowContextMenu
1232 };
1233
1234 /**********************************************************
1235  *      nsIURIContentListener interface
1236  */
1237
1238 #define NSURICL_THIS(iface) DEFINE_THIS(NSContainer, URIContentListener, iface)
1239
1240 static nsresult NSAPI nsURIContentListener_QueryInterface(nsIURIContentListener *iface,
1241         nsIIDRef riid, void **result)
1242 {
1243     NSContainer *This = NSURICL_THIS(iface);
1244     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1245 }
1246
1247 static nsrefcnt NSAPI nsURIContentListener_AddRef(nsIURIContentListener *iface)
1248 {
1249     NSContainer *This = NSURICL_THIS(iface);
1250     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1251 }
1252
1253 static nsrefcnt NSAPI nsURIContentListener_Release(nsIURIContentListener *iface)
1254 {
1255     NSContainer *This = NSURICL_THIS(iface);
1256     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1257 }
1258
1259 static nsresult NSAPI nsURIContentListener_OnStartURIOpen(nsIURIContentListener *iface,
1260                                                           nsIURI *aURI, PRBool *_retval)
1261 {
1262     NSContainer *This = NSURICL_THIS(iface);
1263     nsACString spec_str;
1264     const char *spec;
1265     nsresult nsres;
1266
1267     nsACString_Init(&spec_str, NULL);
1268     nsIURI_GetSpec(aURI, &spec_str);
1269     nsACString_GetData(&spec_str, &spec);
1270
1271     TRACE("(%p)->(%p(%s) %p)\n", This, aURI, debugstr_a(spec), _retval);
1272
1273     nsACString_Finish(&spec_str);
1274
1275     nsres = on_start_uri_open(This, aURI, _retval);
1276     if(NS_FAILED(nsres))
1277         return nsres;
1278
1279     return !*_retval && This->content_listener
1280         ? nsIURIContentListener_OnStartURIOpen(This->content_listener, aURI, _retval)
1281         : NS_OK;
1282 }
1283
1284 static nsresult NSAPI nsURIContentListener_DoContent(nsIURIContentListener *iface,
1285         const char *aContentType, PRBool aIsContentPreferred, nsIRequest *aRequest,
1286         nsIStreamListener **aContentHandler, PRBool *_retval)
1287 {
1288     NSContainer *This = NSURICL_THIS(iface);
1289
1290     TRACE("(%p)->(%s %x %p %p %p)\n", This, debugstr_a(aContentType), aIsContentPreferred,
1291             aRequest, aContentHandler, _retval);
1292
1293     return This->content_listener
1294         ? nsIURIContentListener_DoContent(This->content_listener, aContentType,
1295                   aIsContentPreferred, aRequest, aContentHandler, _retval)
1296         : NS_ERROR_NOT_IMPLEMENTED;
1297 }
1298
1299 static nsresult NSAPI nsURIContentListener_IsPreferred(nsIURIContentListener *iface,
1300         const char *aContentType, char **aDesiredContentType, PRBool *_retval)
1301 {
1302     NSContainer *This = NSURICL_THIS(iface);
1303
1304     TRACE("(%p)->(%s %p %p)\n", This, debugstr_a(aContentType), aDesiredContentType, _retval);
1305
1306     /* FIXME: Should we do something here? */
1307     *_retval = TRUE; 
1308
1309     return This->content_listener
1310         ? nsIURIContentListener_IsPreferred(This->content_listener, aContentType,
1311                   aDesiredContentType, _retval)
1312         : NS_OK;
1313 }
1314
1315 static nsresult NSAPI nsURIContentListener_CanHandleContent(nsIURIContentListener *iface,
1316         const char *aContentType, PRBool aIsContentPreferred, char **aDesiredContentType,
1317         PRBool *_retval)
1318 {
1319     NSContainer *This = NSURICL_THIS(iface);
1320
1321     TRACE("(%p)->(%s %x %p %p)\n", This, debugstr_a(aContentType), aIsContentPreferred,
1322             aDesiredContentType, _retval);
1323
1324     return This->content_listener
1325         ? nsIURIContentListener_CanHandleContent(This->content_listener, aContentType,
1326                 aIsContentPreferred, aDesiredContentType, _retval)
1327         : NS_ERROR_NOT_IMPLEMENTED;
1328 }
1329
1330 static nsresult NSAPI nsURIContentListener_GetLoadCookie(nsIURIContentListener *iface,
1331         nsISupports **aLoadCookie)
1332 {
1333     NSContainer *This = NSURICL_THIS(iface);
1334
1335     WARN("(%p)->(%p)\n", This, aLoadCookie);
1336
1337     return This->content_listener
1338         ? nsIURIContentListener_GetLoadCookie(This->content_listener, aLoadCookie)
1339         : NS_ERROR_NOT_IMPLEMENTED;
1340 }
1341
1342 static nsresult NSAPI nsURIContentListener_SetLoadCookie(nsIURIContentListener *iface,
1343         nsISupports *aLoadCookie)
1344 {
1345     NSContainer *This = NSURICL_THIS(iface);
1346
1347     WARN("(%p)->(%p)\n", This, aLoadCookie);
1348
1349     return This->content_listener
1350         ? nsIURIContentListener_SetLoadCookie(This->content_listener, aLoadCookie)
1351         : NS_ERROR_NOT_IMPLEMENTED;
1352 }
1353
1354 static nsresult NSAPI nsURIContentListener_GetParentContentListener(nsIURIContentListener *iface,
1355         nsIURIContentListener **aParentContentListener)
1356 {
1357     NSContainer *This = NSURICL_THIS(iface);
1358
1359     TRACE("(%p)->(%p)\n", This, aParentContentListener);
1360
1361     if(This->content_listener)
1362         nsIURIContentListener_AddRef(This->content_listener);
1363
1364     *aParentContentListener = This->content_listener;
1365     return NS_OK;
1366 }
1367
1368 static nsresult NSAPI nsURIContentListener_SetParentContentListener(nsIURIContentListener *iface,
1369         nsIURIContentListener *aParentContentListener)
1370 {
1371     NSContainer *This = NSURICL_THIS(iface);
1372
1373     TRACE("(%p)->(%p)\n", This, aParentContentListener);
1374
1375     if(aParentContentListener == NSURICL(This))
1376         return NS_OK;
1377
1378     if(This->content_listener)
1379         nsIURIContentListener_Release(This->content_listener);
1380
1381     This->content_listener = aParentContentListener;
1382     if(This->content_listener)
1383         nsIURIContentListener_AddRef(This->content_listener);
1384
1385     return NS_OK;
1386 }
1387
1388 #undef NSURICL_THIS
1389
1390 static const nsIURIContentListenerVtbl nsURIContentListenerVtbl = {
1391     nsURIContentListener_QueryInterface,
1392     nsURIContentListener_AddRef,
1393     nsURIContentListener_Release,
1394     nsURIContentListener_OnStartURIOpen,
1395     nsURIContentListener_DoContent,
1396     nsURIContentListener_IsPreferred,
1397     nsURIContentListener_CanHandleContent,
1398     nsURIContentListener_GetLoadCookie,
1399     nsURIContentListener_SetLoadCookie,
1400     nsURIContentListener_GetParentContentListener,
1401     nsURIContentListener_SetParentContentListener
1402 };
1403
1404 /**********************************************************
1405  *      nsIEmbeddinSiteWindow interface
1406  */
1407
1408 #define NSEMBWNDS_THIS(iface) DEFINE_THIS(NSContainer, EmbeddingSiteWindow, iface)
1409
1410 static nsresult NSAPI nsEmbeddingSiteWindow_QueryInterface(nsIEmbeddingSiteWindow *iface,
1411         nsIIDRef riid, void **result)
1412 {
1413     NSContainer *This = NSEMBWNDS_THIS(iface);
1414     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1415 }
1416
1417 static nsrefcnt NSAPI nsEmbeddingSiteWindow_AddRef(nsIEmbeddingSiteWindow *iface)
1418 {
1419     NSContainer *This = NSEMBWNDS_THIS(iface);
1420     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1421 }
1422
1423 static nsrefcnt NSAPI nsEmbeddingSiteWindow_Release(nsIEmbeddingSiteWindow *iface)
1424 {
1425     NSContainer *This = NSEMBWNDS_THIS(iface);
1426     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1427 }
1428
1429 static nsresult NSAPI nsEmbeddingSiteWindow_SetDimensions(nsIEmbeddingSiteWindow *iface,
1430         PRUint32 flags, PRInt32 x, PRInt32 y, PRInt32 cx, PRInt32 cy)
1431 {
1432     NSContainer *This = NSEMBWNDS_THIS(iface);
1433     WARN("(%p)->(%08x %d %d %d %d)\n", This, flags, x, y, cx, cy);
1434     return NS_ERROR_NOT_IMPLEMENTED;
1435 }
1436
1437 static nsresult NSAPI nsEmbeddingSiteWindow_GetDimensions(nsIEmbeddingSiteWindow *iface,
1438         PRUint32 flags, PRInt32 *x, PRInt32 *y, PRInt32 *cx, PRInt32 *cy)
1439 {
1440     NSContainer *This = NSEMBWNDS_THIS(iface);
1441     WARN("(%p)->(%08x %p %p %p %p)\n", This, flags, x, y, cx, cy);
1442     return NS_ERROR_NOT_IMPLEMENTED;
1443 }
1444
1445 static nsresult NSAPI nsEmbeddingSiteWindow_SetFocus(nsIEmbeddingSiteWindow *iface)
1446 {
1447     NSContainer *This = NSEMBWNDS_THIS(iface);
1448
1449     TRACE("(%p)\n", This);
1450
1451     return nsIBaseWindow_SetFocus(This->window);
1452 }
1453
1454 static nsresult NSAPI nsEmbeddingSiteWindow_GetVisibility(nsIEmbeddingSiteWindow *iface,
1455         PRBool *aVisibility)
1456 {
1457     NSContainer *This = NSEMBWNDS_THIS(iface);
1458
1459     TRACE("(%p)->(%p)\n", This, aVisibility);
1460
1461     *aVisibility = This->doc && This->doc->hwnd && IsWindowVisible(This->doc->hwnd);
1462     return NS_OK;
1463 }
1464
1465 static nsresult NSAPI nsEmbeddingSiteWindow_SetVisibility(nsIEmbeddingSiteWindow *iface,
1466         PRBool aVisibility)
1467 {
1468     NSContainer *This = NSEMBWNDS_THIS(iface);
1469
1470     TRACE("(%p)->(%x)\n", This, aVisibility);
1471
1472     return NS_OK;
1473 }
1474
1475 static nsresult NSAPI nsEmbeddingSiteWindow_GetTitle(nsIEmbeddingSiteWindow *iface,
1476         PRUnichar **aTitle)
1477 {
1478     NSContainer *This = NSEMBWNDS_THIS(iface);
1479     WARN("(%p)->(%p)\n", This, aTitle);
1480     return NS_ERROR_NOT_IMPLEMENTED;
1481 }
1482
1483 static nsresult NSAPI nsEmbeddingSiteWindow_SetTitle(nsIEmbeddingSiteWindow *iface,
1484         const PRUnichar *aTitle)
1485 {
1486     NSContainer *This = NSEMBWNDS_THIS(iface);
1487     WARN("(%p)->(%s)\n", This, debugstr_w(aTitle));
1488     return NS_ERROR_NOT_IMPLEMENTED;
1489 }
1490
1491 static nsresult NSAPI nsEmbeddingSiteWindow_GetSiteWindow(nsIEmbeddingSiteWindow *iface,
1492         void **aSiteWindow)
1493 {
1494     NSContainer *This = NSEMBWNDS_THIS(iface);
1495
1496     TRACE("(%p)->(%p)\n", This, aSiteWindow);
1497
1498     *aSiteWindow = This->hwnd;
1499     return NS_OK;
1500 }
1501
1502 static const nsIEmbeddingSiteWindowVtbl nsEmbeddingSiteWindowVtbl = {
1503     nsEmbeddingSiteWindow_QueryInterface,
1504     nsEmbeddingSiteWindow_AddRef,
1505     nsEmbeddingSiteWindow_Release,
1506     nsEmbeddingSiteWindow_SetDimensions,
1507     nsEmbeddingSiteWindow_GetDimensions,
1508     nsEmbeddingSiteWindow_SetFocus,
1509     nsEmbeddingSiteWindow_GetVisibility,
1510     nsEmbeddingSiteWindow_SetVisibility,
1511     nsEmbeddingSiteWindow_GetTitle,
1512     nsEmbeddingSiteWindow_SetTitle,
1513     nsEmbeddingSiteWindow_GetSiteWindow
1514 };
1515
1516 #define NSTOOLTIP_THIS(iface) DEFINE_THIS(NSContainer, TooltipListener, iface)
1517
1518 static nsresult NSAPI nsTooltipListener_QueryInterface(nsITooltipListener *iface, nsIIDRef riid,
1519         void **result)
1520 {
1521     NSContainer *This = NSTOOLTIP_THIS(iface);
1522     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1523 }
1524
1525 static nsrefcnt NSAPI nsTooltipListener_AddRef(nsITooltipListener *iface)
1526 {
1527     NSContainer *This = NSTOOLTIP_THIS(iface);
1528     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1529 }
1530
1531 static nsrefcnt NSAPI nsTooltipListener_Release(nsITooltipListener *iface)
1532 {
1533     NSContainer *This = NSTOOLTIP_THIS(iface);
1534     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1535 }
1536
1537 static nsresult NSAPI nsTooltipListener_OnShowTooltip(nsITooltipListener *iface,
1538         PRInt32 aXCoord, PRInt32 aYCoord, const PRUnichar *aTipText)
1539 {
1540     NSContainer *This = NSTOOLTIP_THIS(iface);
1541
1542     if (This->doc)
1543         show_tooltip(This->doc, aXCoord, aYCoord, aTipText);
1544
1545     return NS_OK;
1546 }
1547
1548 static nsresult NSAPI nsTooltipListener_OnHideTooltip(nsITooltipListener *iface)
1549 {
1550     NSContainer *This = NSTOOLTIP_THIS(iface);
1551
1552     if (This->doc)
1553         hide_tooltip(This->doc);
1554
1555     return NS_OK;
1556 }
1557
1558 #undef NSTOOLTIM_THIS
1559
1560 static const nsITooltipListenerVtbl nsTooltipListenerVtbl = {
1561     nsTooltipListener_QueryInterface,
1562     nsTooltipListener_AddRef,
1563     nsTooltipListener_Release,
1564     nsTooltipListener_OnShowTooltip,
1565     nsTooltipListener_OnHideTooltip
1566 };
1567
1568 #define NSIFACEREQ_THIS(iface) DEFINE_THIS(NSContainer, InterfaceRequestor, iface)
1569
1570 static nsresult NSAPI nsInterfaceRequestor_QueryInterface(nsIInterfaceRequestor *iface,
1571         nsIIDRef riid, void **result)
1572 {
1573     NSContainer *This = NSIFACEREQ_THIS(iface);
1574     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1575 }
1576
1577 static nsrefcnt NSAPI nsInterfaceRequestor_AddRef(nsIInterfaceRequestor *iface)
1578 {
1579     NSContainer *This = NSIFACEREQ_THIS(iface);
1580     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1581 }
1582
1583 static nsrefcnt NSAPI nsInterfaceRequestor_Release(nsIInterfaceRequestor *iface)
1584 {
1585     NSContainer *This = NSIFACEREQ_THIS(iface);
1586     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1587 }
1588
1589 static nsresult NSAPI nsInterfaceRequestor_GetInterface(nsIInterfaceRequestor *iface,
1590         nsIIDRef riid, void **result)
1591 {
1592     NSContainer *This = NSIFACEREQ_THIS(iface);
1593
1594     if(IsEqualGUID(&IID_nsIDOMWindow, riid)) {
1595         TRACE("(%p)->(IID_nsIDOMWindow %p)\n", This, result);
1596         return nsIWebBrowser_GetContentDOMWindow(This->webbrowser, (nsIDOMWindow**)result);
1597     }
1598
1599     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1600 }
1601
1602 #undef NSIFACEREQ_THIS
1603
1604 static const nsIInterfaceRequestorVtbl nsInterfaceRequestorVtbl = {
1605     nsInterfaceRequestor_QueryInterface,
1606     nsInterfaceRequestor_AddRef,
1607     nsInterfaceRequestor_Release,
1608     nsInterfaceRequestor_GetInterface
1609 };
1610
1611 #define NSWEAKREF_THIS(iface) DEFINE_THIS(NSContainer, WeakReference, iface)
1612
1613 static nsresult NSAPI nsWeakReference_QueryInterface(nsIWeakReference *iface,
1614         nsIIDRef riid, void **result)
1615 {
1616     NSContainer *This = NSWEAKREF_THIS(iface);
1617     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1618 }
1619
1620 static nsrefcnt NSAPI nsWeakReference_AddRef(nsIWeakReference *iface)
1621 {
1622     NSContainer *This = NSWEAKREF_THIS(iface);
1623     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1624 }
1625
1626 static nsrefcnt NSAPI nsWeakReference_Release(nsIWeakReference *iface)
1627 {
1628     NSContainer *This = NSWEAKREF_THIS(iface);
1629     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1630 }
1631
1632 static nsresult NSAPI nsWeakReference_QueryReferent(nsIWeakReference *iface,
1633         const nsIID *riid, void **result)
1634 {
1635     NSContainer *This = NSWEAKREF_THIS(iface);
1636     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1637 }
1638
1639 #undef NSWEAKREF_THIS
1640
1641 static const nsIWeakReferenceVtbl nsWeakReferenceVtbl = {
1642     nsWeakReference_QueryInterface,
1643     nsWeakReference_AddRef,
1644     nsWeakReference_Release,
1645     nsWeakReference_QueryReferent
1646 };
1647
1648 #define NSSUPWEAKREF_THIS(iface) DEFINE_THIS(NSContainer, SupportsWeakReference, iface)
1649
1650 static nsresult NSAPI nsSupportsWeakReference_QueryInterface(nsISupportsWeakReference *iface,
1651         nsIIDRef riid, void **result)
1652 {
1653     NSContainer *This = NSSUPWEAKREF_THIS(iface);
1654     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1655 }
1656
1657 static nsrefcnt NSAPI nsSupportsWeakReference_AddRef(nsISupportsWeakReference *iface)
1658 {
1659     NSContainer *This = NSSUPWEAKREF_THIS(iface);
1660     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1661 }
1662
1663 static nsrefcnt NSAPI nsSupportsWeakReference_Release(nsISupportsWeakReference *iface)
1664 {
1665     NSContainer *This = NSSUPWEAKREF_THIS(iface);
1666     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1667 }
1668
1669 static nsresult NSAPI nsSupportsWeakReference_GetWeakReference(nsISupportsWeakReference *iface,
1670         nsIWeakReference **_retval)
1671 {
1672     NSContainer *This = NSSUPWEAKREF_THIS(iface);
1673
1674     TRACE("(%p)->(%p)\n", This, _retval);
1675
1676     nsIWeakReference_AddRef(NSWEAKREF(This));
1677     *_retval = NSWEAKREF(This);
1678     return NS_OK;
1679 }
1680
1681 #undef NSWEAKREF_THIS
1682
1683 static const nsISupportsWeakReferenceVtbl nsSupportsWeakReferenceVtbl = {
1684     nsSupportsWeakReference_QueryInterface,
1685     nsSupportsWeakReference_AddRef,
1686     nsSupportsWeakReference_Release,
1687     nsSupportsWeakReference_GetWeakReference
1688 };
1689
1690
1691 NSContainer *NSContainer_Create(HTMLDocumentObj *doc, NSContainer *parent)
1692 {
1693     nsIWebBrowserSetup *wbsetup;
1694     nsIScrollable *scrollable;
1695     NSContainer *ret;
1696     nsresult nsres;
1697
1698     if(!load_gecko(TRUE))
1699         return NULL;
1700
1701     ret = heap_alloc_zero(sizeof(NSContainer));
1702
1703     ret->lpWebBrowserChromeVtbl      = &nsWebBrowserChromeVtbl;
1704     ret->lpContextMenuListenerVtbl   = &nsContextMenuListenerVtbl;
1705     ret->lpURIContentListenerVtbl    = &nsURIContentListenerVtbl;
1706     ret->lpEmbeddingSiteWindowVtbl   = &nsEmbeddingSiteWindowVtbl;
1707     ret->lpTooltipListenerVtbl       = &nsTooltipListenerVtbl;
1708     ret->lpInterfaceRequestorVtbl    = &nsInterfaceRequestorVtbl;
1709     ret->lpWeakReferenceVtbl         = &nsWeakReferenceVtbl;
1710     ret->lpSupportsWeakReferenceVtbl = &nsSupportsWeakReferenceVtbl;
1711
1712     ret->doc = doc;
1713     ret->ref = 1;
1714
1715     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_WEBBROWSER_CONTRACTID,
1716             NULL, &IID_nsIWebBrowser, (void**)&ret->webbrowser);
1717     if(NS_FAILED(nsres)) {
1718         ERR("Creating WebBrowser failed: %08x\n", nsres);
1719         heap_free(ret);
1720         return NULL;
1721     }
1722
1723     if(parent)
1724         nsIWebBrowserChrome_AddRef(NSWBCHROME(parent));
1725     ret->parent = parent;
1726
1727     nsres = nsIWebBrowser_SetContainerWindow(ret->webbrowser, NSWBCHROME(ret));
1728     if(NS_FAILED(nsres))
1729         ERR("SetContainerWindow failed: %08x\n", nsres);
1730
1731     nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIBaseWindow,
1732             (void**)&ret->window);
1733     if(NS_FAILED(nsres))
1734         ERR("Could not get nsIBaseWindow interface: %08x\n", nsres);
1735
1736     nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIWebBrowserSetup,
1737                                          (void**)&wbsetup);
1738     if(NS_SUCCEEDED(nsres)) {
1739         nsres = nsIWebBrowserSetup_SetProperty(wbsetup, SETUP_IS_CHROME_WRAPPER, FALSE);
1740         nsIWebBrowserSetup_Release(wbsetup);
1741         if(NS_FAILED(nsres))
1742             ERR("SetProperty(SETUP_IS_CHROME_WRAPPER) failed: %08x\n", nsres);
1743     }else {
1744         ERR("Could not get nsIWebBrowserSetup interface\n");
1745     }
1746
1747     nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIWebNavigation,
1748             (void**)&ret->navigation);
1749     if(NS_FAILED(nsres))
1750         ERR("Could not get nsIWebNavigation interface: %08x\n", nsres);
1751
1752     nsres = nsIWebBrowserFocus_QueryInterface(ret->webbrowser, &IID_nsIWebBrowserFocus,
1753             (void**)&ret->focus);
1754     if(NS_FAILED(nsres))
1755         ERR("Could not get nsIWebBrowserFocus interface: %08x\n", nsres);
1756
1757     if(!nscontainer_class)
1758         register_nscontainer_class();
1759
1760     ret->hwnd = CreateWindowExW(0, wszNsContainer, NULL,
1761             WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 0, 0, 100, 100,
1762             GetDesktopWindow(), NULL, hInst, ret);
1763
1764     nsres = nsIBaseWindow_InitWindow(ret->window, ret->hwnd, NULL, 0, 0, 100, 100);
1765     if(NS_SUCCEEDED(nsres)) {
1766         nsres = nsIBaseWindow_Create(ret->window);
1767         if(NS_FAILED(nsres))
1768             WARN("Creating window failed: %08x\n", nsres);
1769
1770         nsIBaseWindow_SetVisibility(ret->window, FALSE);
1771         nsIBaseWindow_SetEnabled(ret->window, FALSE);
1772     }else {
1773         ERR("InitWindow failed: %08x\n", nsres);
1774     }
1775
1776     nsres = nsIWebBrowser_SetParentURIContentListener(ret->webbrowser, NSURICL(ret));
1777     if(NS_FAILED(nsres))
1778         ERR("SetParentURIContentListener failed: %08x\n", nsres);
1779
1780     nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIScrollable, (void**)&scrollable);
1781     if(NS_SUCCEEDED(nsres)) {
1782         nsres = nsIScrollable_SetDefaultScrollbarPreferences(scrollable,
1783                 ScrollOrientation_Y, Scrollbar_Always);
1784         if(NS_FAILED(nsres))
1785             ERR("Could not set default Y scrollbar prefs: %08x\n", nsres);
1786
1787         nsres = nsIScrollable_SetDefaultScrollbarPreferences(scrollable,
1788                 ScrollOrientation_X, Scrollbar_Auto);
1789         if(NS_FAILED(nsres))
1790             ERR("Could not set default X scrollbar prefs: %08x\n", nsres);
1791
1792         nsIScrollable_Release(scrollable);
1793     }else {
1794         ERR("Could not get nsIScrollable: %08x\n", nsres);
1795     }
1796
1797     return ret;
1798 }
1799
1800 void NSContainer_Release(NSContainer *This)
1801 {
1802     TRACE("(%p)\n", This);
1803
1804     This->doc = NULL;
1805
1806     ShowWindow(This->hwnd, SW_HIDE);
1807     SetParent(This->hwnd, NULL);
1808
1809     nsIBaseWindow_SetVisibility(This->window, FALSE);
1810     nsIBaseWindow_Destroy(This->window);
1811
1812     nsIWebBrowser_SetContainerWindow(This->webbrowser, NULL);
1813
1814     nsIWebBrowser_Release(This->webbrowser);
1815     This->webbrowser = NULL;
1816
1817     nsIWebNavigation_Release(This->navigation);
1818     This->navigation = NULL;
1819
1820     nsIBaseWindow_Release(This->window);
1821     This->window = NULL;
1822
1823     nsIWebBrowserFocus_Release(This->focus);
1824     This->focus = NULL;
1825
1826     if(This->editor_controller) {
1827         nsIController_Release(This->editor_controller);
1828         This->editor_controller = NULL;
1829     }
1830
1831     if(This->editor) {
1832         nsIEditor_Release(This->editor);
1833         This->editor = NULL;
1834     }
1835
1836     if(This->content_listener) {
1837         nsIURIContentListener_Release(This->content_listener);
1838         This->content_listener = NULL;
1839     }
1840
1841     if(This->hwnd) {
1842         DestroyWindow(This->hwnd);
1843         This->hwnd = NULL;
1844     }
1845
1846     nsIWebBrowserChrome_Release(NSWBCHROME(This));
1847 }