jscript: Added Date.setYear implementation.
[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 HRESULT return_nsstr(nsresult nsres, nsAString *nsstr, BSTR *p)
737 {
738     const PRUnichar *str;
739
740     if(NS_FAILED(nsres)) {
741         ERR("failed: %08x\n", nsres);
742         nsAString_Finish(nsstr);
743         return E_FAIL;
744     }
745
746     nsAString_GetData(nsstr, &str);
747     TRACE("ret %s\n", debugstr_w(str));
748     if(*str) {
749         *p = SysAllocString(str);
750         if(!*p)
751             return E_OUTOFMEMORY;
752     }else {
753         *p = NULL;
754     }
755
756     nsAString_Finish(nsstr);
757     return S_OK;
758 }
759
760 nsICommandParams *create_nscommand_params(void)
761 {
762     nsICommandParams *ret = NULL;
763     nsresult nsres;
764
765     if(!pCompMgr)
766         return NULL;
767
768     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
769             NS_COMMANDPARAMS_CONTRACTID, NULL, &IID_nsICommandParams,
770             (void**)&ret);
771     if(NS_FAILED(nsres))
772         ERR("Could not get nsICommandParams\n");
773
774     return ret;
775 }
776
777 nsresult get_nsinterface(nsISupports *iface, REFIID riid, void **ppv)
778 {
779     nsIInterfaceRequestor *iface_req;
780     nsresult nsres;
781
782     nsres = nsISupports_QueryInterface(iface, &IID_nsIInterfaceRequestor, (void**)&iface_req);
783     if(NS_FAILED(nsres))
784         return nsres;
785
786     nsres = nsIInterfaceRequestor_GetInterface(iface_req, riid, ppv);
787     nsIInterfaceRequestor_Release(iface_req);
788
789     return nsres;
790 }
791
792 static HRESULT nsnode_to_nsstring_rec(nsIContentSerializer *serializer, nsIDOMNode *nsnode, nsAString *str)
793 {
794     nsIDOMNodeList *node_list = NULL;
795     PRBool has_children = FALSE;
796     nsIContent *nscontent;
797     PRUint16 type;
798     nsresult nsres;
799
800     nsIDOMNode_HasChildNodes(nsnode, &has_children);
801
802     nsres = nsIDOMNode_GetNodeType(nsnode, &type);
803     if(NS_FAILED(nsres)) {
804         ERR("GetType failed: %08x\n", nsres);
805         return E_FAIL;
806     }
807
808     nsres = nsIDOMNode_QueryInterface(nsnode, &IID_nsIContent, (void**)&nscontent);
809     if(NS_FAILED(nsres)) {
810         ERR("Could not get nsIDontent interface: %08x\n", nsres);
811         return E_FAIL;
812     }
813
814     switch(type) {
815     case ELEMENT_NODE:
816         nsIContentSerializer_AppendElementStart(serializer, nscontent, nscontent, str);
817         break;
818     case TEXT_NODE:
819         nsIContentSerializer_AppendText(serializer, nscontent, 0, -1, str);
820         break;
821     case COMMENT_NODE:
822         nsres = nsIContentSerializer_AppendComment(serializer, nscontent, 0, -1, str);
823         break;
824     case DOCUMENT_NODE: {
825         nsIDocument *nsdoc;
826         nsIDOMNode_QueryInterface(nsnode, &IID_nsIDocument, (void**)&nsdoc);
827         nsIContentSerializer_AppendDocumentStart(serializer, nsdoc, str);
828         nsIDocument_Release(nsdoc);
829         break;
830     }
831     case DOCUMENT_TYPE_NODE:
832         WARN("Ignoring DOCUMENT_TYPE_NODE\n");
833         break;
834     case DOCUMENT_FRAGMENT_NODE:
835         break;
836     default:
837         FIXME("Unhandled type %u\n", type);
838     }
839
840     if(has_children) {
841         PRUint32 child_cnt, i;
842         nsIDOMNode *child_node;
843
844         nsIDOMNode_GetChildNodes(nsnode, &node_list);
845         nsIDOMNodeList_GetLength(node_list, &child_cnt);
846
847         for(i=0; i<child_cnt; i++) {
848             nsres = nsIDOMNodeList_Item(node_list, i, &child_node);
849             if(NS_SUCCEEDED(nsres)) {
850                 nsnode_to_nsstring_rec(serializer, child_node, str);
851                 nsIDOMNode_Release(child_node);
852             }else {
853                 ERR("Item failed: %08x\n", nsres);
854             }
855         }
856
857         nsIDOMNodeList_Release(node_list);
858     }
859
860     if(type == ELEMENT_NODE)
861         nsIContentSerializer_AppendElementEnd(serializer, nscontent, str);
862
863     nsIContent_Release(nscontent);
864     return S_OK;
865 }
866
867 HRESULT nsnode_to_nsstring(nsIDOMNode *nsnode, nsAString *str)
868 {
869     nsIContentSerializer *serializer;
870     nsresult nsres;
871     HRESULT hres;
872
873     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
874             NS_HTMLSERIALIZER_CONTRACTID, NULL, &IID_nsIContentSerializer,
875             (void**)&serializer);
876     if(NS_FAILED(nsres)) {
877         ERR("Could not get nsIContentSerializer: %08x\n", nsres);
878         return E_FAIL;
879     }
880
881     nsres = nsIContentSerializer_Init(serializer, 0, 100, NULL, FALSE, FALSE /* FIXME */);
882     if(NS_FAILED(nsres))
883         ERR("Init failed: %08x\n", nsres);
884
885     hres = nsnode_to_nsstring_rec(serializer, nsnode, str);
886     if(SUCCEEDED(hres)) {
887         nsres = nsIContentSerializer_Flush(serializer, str);
888         if(NS_FAILED(nsres))
889             ERR("Flush failed: %08x\n", nsres);
890     }
891
892     nsIContentSerializer_Release(serializer);
893     return hres;
894 }
895
896 void get_editor_controller(NSContainer *This)
897 {
898     nsIEditingSession *editing_session = NULL;
899     nsIControllerContext *ctrlctx;
900     nsresult nsres;
901
902     if(This->editor) {
903         nsIEditor_Release(This->editor);
904         This->editor = NULL;
905     }
906
907     if(This->editor_controller) {
908         nsIController_Release(This->editor_controller);
909         This->editor_controller = NULL;
910     }
911
912     nsres = get_nsinterface((nsISupports*)This->webbrowser, &IID_nsIEditingSession,
913             (void**)&editing_session);
914     if(NS_FAILED(nsres)) {
915         ERR("Could not get nsIEditingSession: %08x\n", nsres);
916         return;
917     }
918
919     nsres = nsIEditingSession_GetEditorForWindow(editing_session,
920             This->doc->basedoc.window->nswindow, &This->editor);
921     nsIEditingSession_Release(editing_session);
922     if(NS_FAILED(nsres)) {
923         ERR("Could not get editor: %08x\n", nsres);
924         return;
925     }
926
927     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
928             NS_EDITORCONTROLLER_CONTRACTID, NULL, &IID_nsIControllerContext, (void**)&ctrlctx);
929     if(NS_SUCCEEDED(nsres)) {
930         nsres = nsIControllerContext_SetCommandContext(ctrlctx, (nsISupports *)This->editor);
931         if(NS_FAILED(nsres))
932             ERR("SetCommandContext failed: %08x\n", nsres);
933         nsres = nsIControllerContext_QueryInterface(ctrlctx, &IID_nsIController,
934                 (void**)&This->editor_controller);
935         nsIControllerContext_Release(ctrlctx);
936         if(NS_FAILED(nsres))
937             ERR("Could not get nsIController interface: %08x\n", nsres);
938     }else {
939         ERR("Could not create edit controller: %08x\n", nsres);
940     }
941 }
942
943 void close_gecko(void)
944 {
945     TRACE("()\n");
946
947     release_nsio();
948
949     if(profile_directory) {
950         nsIFile_Release(profile_directory);
951         profile_directory = NULL;
952     }
953
954     if(pCompMgr)
955         nsIComponentManager_Release(pCompMgr);
956
957     if(pServMgr)
958         nsIServiceManager_Release(pServMgr);
959
960     if(nsmem)
961         nsIMemory_Release(nsmem);
962
963     /* Gecko doesn't really support being unloaded */
964     /* if (hXPCOM) FreeLibrary(hXPCOM); */
965 }
966
967 BOOL is_gecko_path(const char *path)
968 {
969     WCHAR *buf, *ptr;
970     BOOL ret;
971
972     buf = heap_strdupAtoW(path);
973     if(strlenW(buf) < gecko_path_len)
974         return FALSE;
975
976     buf[gecko_path_len] = 0;
977     for(ptr = buf; *ptr; ptr++) {
978         if(*ptr == '\\')
979             *ptr = '/';
980     }
981
982     ret = !strcmpiW(buf, gecko_path);
983     heap_free(buf);
984     return ret;
985 }
986
987 /**********************************************************
988  *      nsIWebBrowserChrome interface
989  */
990
991 #define NSWBCHROME_THIS(iface) DEFINE_THIS(NSContainer, WebBrowserChrome, iface)
992
993 static nsresult NSAPI nsWebBrowserChrome_QueryInterface(nsIWebBrowserChrome *iface,
994         nsIIDRef riid, void **result)
995 {
996     NSContainer *This = NSWBCHROME_THIS(iface);
997
998     *result = NULL;
999     if(IsEqualGUID(&IID_nsISupports, riid)) {
1000         TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
1001         *result = NSWBCHROME(This);
1002     }else if(IsEqualGUID(&IID_nsIWebBrowserChrome, riid)) {
1003         TRACE("(%p)->(IID_nsIWebBrowserChrome, %p)\n", This, result);
1004         *result = NSWBCHROME(This);
1005     }else if(IsEqualGUID(&IID_nsIContextMenuListener, riid)) {
1006         TRACE("(%p)->(IID_nsIContextMenuListener, %p)\n", This, result);
1007         *result = NSCML(This);
1008     }else if(IsEqualGUID(&IID_nsIURIContentListener, riid)) {
1009         TRACE("(%p)->(IID_nsIURIContentListener %p)\n", This, result);
1010         *result = NSURICL(This);
1011     }else if(IsEqualGUID(&IID_nsIEmbeddingSiteWindow, riid)) {
1012         TRACE("(%p)->(IID_nsIEmbeddingSiteWindow %p)\n", This, result);
1013         *result = NSEMBWNDS(This);
1014     }else if(IsEqualGUID(&IID_nsITooltipListener, riid)) {
1015         TRACE("(%p)->(IID_nsITooltipListener %p)\n", This, result);
1016         *result = NSTOOLTIP(This);
1017     }else if(IsEqualGUID(&IID_nsIInterfaceRequestor, riid)) {
1018         TRACE("(%p)->(IID_nsIInterfaceRequestor %p)\n", This, result);
1019         *result = NSIFACEREQ(This);
1020     }else if(IsEqualGUID(&IID_nsIWeakReference, riid)) {
1021         TRACE("(%p)->(IID_nsIWeakReference %p)\n", This, result);
1022         *result = NSWEAKREF(This);
1023     }else if(IsEqualGUID(&IID_nsISupportsWeakReference, riid)) {
1024         TRACE("(%p)->(IID_nsISupportsWeakReference %p)\n", This, result);
1025         *result = NSSUPWEAKREF(This);
1026     }
1027
1028     if(*result) {
1029         nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1030         return NS_OK;
1031     }
1032
1033     TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
1034     return NS_NOINTERFACE;
1035 }
1036
1037 static nsrefcnt NSAPI nsWebBrowserChrome_AddRef(nsIWebBrowserChrome *iface)
1038 {
1039     NSContainer *This = NSWBCHROME_THIS(iface);
1040     LONG ref = InterlockedIncrement(&This->ref);
1041
1042     TRACE("(%p) ref=%d\n", This, ref);
1043
1044     return ref;
1045 }
1046
1047 static nsrefcnt NSAPI nsWebBrowserChrome_Release(nsIWebBrowserChrome *iface)
1048 {
1049     NSContainer *This = NSWBCHROME_THIS(iface);
1050     LONG ref = InterlockedDecrement(&This->ref);
1051
1052     TRACE("(%p) ref=%d\n", This, ref);
1053
1054     if(!ref) {
1055         if(This->parent)
1056             nsIWebBrowserChrome_Release(NSWBCHROME(This->parent));
1057         heap_free(This);
1058     }
1059
1060     return ref;
1061 }
1062
1063 static nsresult NSAPI nsWebBrowserChrome_SetStatus(nsIWebBrowserChrome *iface,
1064         PRUint32 statusType, const PRUnichar *status)
1065 {
1066     NSContainer *This = NSWBCHROME_THIS(iface);
1067     TRACE("(%p)->(%d %s)\n", This, statusType, debugstr_w(status));
1068     return NS_OK;
1069 }
1070
1071 static nsresult NSAPI nsWebBrowserChrome_GetWebBrowser(nsIWebBrowserChrome *iface,
1072         nsIWebBrowser **aWebBrowser)
1073 {
1074     NSContainer *This = NSWBCHROME_THIS(iface);
1075
1076     TRACE("(%p)->(%p)\n", This, aWebBrowser);
1077
1078     if(!aWebBrowser)
1079         return NS_ERROR_INVALID_ARG;
1080
1081     if(This->webbrowser)
1082         nsIWebBrowser_AddRef(This->webbrowser);
1083     *aWebBrowser = This->webbrowser;
1084     return S_OK;
1085 }
1086
1087 static nsresult NSAPI nsWebBrowserChrome_SetWebBrowser(nsIWebBrowserChrome *iface,
1088         nsIWebBrowser *aWebBrowser)
1089 {
1090     NSContainer *This = NSWBCHROME_THIS(iface);
1091
1092     TRACE("(%p)->(%p)\n", This, aWebBrowser);
1093
1094     if(aWebBrowser != This->webbrowser)
1095         ERR("Wrong nsWebBrowser!\n");
1096
1097     return NS_OK;
1098 }
1099
1100 static nsresult NSAPI nsWebBrowserChrome_GetChromeFlags(nsIWebBrowserChrome *iface,
1101         PRUint32 *aChromeFlags)
1102 {
1103     NSContainer *This = NSWBCHROME_THIS(iface);
1104     WARN("(%p)->(%p)\n", This, aChromeFlags);
1105     return NS_ERROR_NOT_IMPLEMENTED;
1106 }
1107
1108 static nsresult NSAPI nsWebBrowserChrome_SetChromeFlags(nsIWebBrowserChrome *iface,
1109         PRUint32 aChromeFlags)
1110 {
1111     NSContainer *This = NSWBCHROME_THIS(iface);
1112     WARN("(%p)->(%08x)\n", This, aChromeFlags);
1113     return NS_ERROR_NOT_IMPLEMENTED;
1114 }
1115
1116 static nsresult NSAPI nsWebBrowserChrome_DestroyBrowserWindow(nsIWebBrowserChrome *iface)
1117 {
1118     NSContainer *This = NSWBCHROME_THIS(iface);
1119     TRACE("(%p)\n", This);
1120     return NS_ERROR_NOT_IMPLEMENTED;
1121 }
1122
1123 static nsresult NSAPI nsWebBrowserChrome_SizeBrowserTo(nsIWebBrowserChrome *iface,
1124         PRInt32 aCX, PRInt32 aCY)
1125 {
1126     NSContainer *This = NSWBCHROME_THIS(iface);
1127     WARN("(%p)->(%d %d)\n", This, aCX, aCY);
1128     return NS_ERROR_NOT_IMPLEMENTED;
1129 }
1130
1131 static nsresult NSAPI nsWebBrowserChrome_ShowAsModal(nsIWebBrowserChrome *iface)
1132 {
1133     NSContainer *This = NSWBCHROME_THIS(iface);
1134     WARN("(%p)\n", This);
1135     return NS_ERROR_NOT_IMPLEMENTED;
1136 }
1137
1138 static nsresult NSAPI nsWebBrowserChrome_IsWindowModal(nsIWebBrowserChrome *iface, PRBool *_retval)
1139 {
1140     NSContainer *This = NSWBCHROME_THIS(iface);
1141     WARN("(%p)->(%p)\n", This, _retval);
1142     return NS_ERROR_NOT_IMPLEMENTED;
1143 }
1144
1145 static nsresult NSAPI nsWebBrowserChrome_ExitModalEventLoop(nsIWebBrowserChrome *iface,
1146         nsresult aStatus)
1147 {
1148     NSContainer *This = NSWBCHROME_THIS(iface);
1149     WARN("(%p)->(%08x)\n", This, aStatus);
1150     return NS_ERROR_NOT_IMPLEMENTED;
1151 }
1152
1153 #undef NSWBCHROME_THIS
1154
1155 static const nsIWebBrowserChromeVtbl nsWebBrowserChromeVtbl = {
1156     nsWebBrowserChrome_QueryInterface,
1157     nsWebBrowserChrome_AddRef,
1158     nsWebBrowserChrome_Release,
1159     nsWebBrowserChrome_SetStatus,
1160     nsWebBrowserChrome_GetWebBrowser,
1161     nsWebBrowserChrome_SetWebBrowser,
1162     nsWebBrowserChrome_GetChromeFlags,
1163     nsWebBrowserChrome_SetChromeFlags,
1164     nsWebBrowserChrome_DestroyBrowserWindow,
1165     nsWebBrowserChrome_SizeBrowserTo,
1166     nsWebBrowserChrome_ShowAsModal,
1167     nsWebBrowserChrome_IsWindowModal,
1168     nsWebBrowserChrome_ExitModalEventLoop
1169 };
1170
1171 /**********************************************************
1172  *      nsIContextMenuListener interface
1173  */
1174
1175 #define NSCML_THIS(iface) DEFINE_THIS(NSContainer, ContextMenuListener, iface)
1176
1177 static nsresult NSAPI nsContextMenuListener_QueryInterface(nsIContextMenuListener *iface,
1178         nsIIDRef riid, void **result)
1179 {
1180     NSContainer *This = NSCML_THIS(iface);
1181     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1182 }
1183
1184 static nsrefcnt NSAPI nsContextMenuListener_AddRef(nsIContextMenuListener *iface)
1185 {
1186     NSContainer *This = NSCML_THIS(iface);
1187     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1188 }
1189
1190 static nsrefcnt NSAPI nsContextMenuListener_Release(nsIContextMenuListener *iface)
1191 {
1192     NSContainer *This = NSCML_THIS(iface);
1193     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1194 }
1195
1196 static nsresult NSAPI nsContextMenuListener_OnShowContextMenu(nsIContextMenuListener *iface,
1197         PRUint32 aContextFlags, nsIDOMEvent *aEvent, nsIDOMNode *aNode)
1198 {
1199     NSContainer *This = NSCML_THIS(iface);
1200     nsIDOMMouseEvent *event;
1201     HTMLDOMNode *node;
1202     POINT pt;
1203     DWORD dwID = CONTEXT_MENU_DEFAULT;
1204     nsresult nsres;
1205     HRESULT hres;
1206
1207     TRACE("(%p)->(%08x %p %p)\n", This, aContextFlags, aEvent, aNode);
1208
1209     fire_event(This->doc->basedoc.doc_node /* FIXME */, EVENTID_CONTEXTMENU, TRUE, aNode, aEvent);
1210
1211     nsres = nsIDOMEvent_QueryInterface(aEvent, &IID_nsIDOMMouseEvent, (void**)&event);
1212     if(NS_FAILED(nsres)) {
1213         ERR("Could not get nsIDOMMouseEvent interface: %08x\n", nsres);
1214         return nsres;
1215     }
1216
1217     nsIDOMMouseEvent_GetScreenX(event, &pt.x);
1218     nsIDOMMouseEvent_GetScreenY(event, &pt.y);
1219     nsIDOMMouseEvent_Release(event);
1220
1221     switch(aContextFlags) {
1222     case CONTEXT_NONE:
1223     case CONTEXT_DOCUMENT:
1224     case CONTEXT_TEXT:
1225         dwID = CONTEXT_MENU_DEFAULT;
1226         break;
1227     case CONTEXT_IMAGE:
1228     case CONTEXT_IMAGE|CONTEXT_LINK:
1229         dwID = CONTEXT_MENU_IMAGE;
1230         break;
1231     case CONTEXT_LINK:
1232         dwID = CONTEXT_MENU_ANCHOR;
1233         break;
1234     case CONTEXT_INPUT:
1235         dwID = CONTEXT_MENU_CONTROL;
1236         break;
1237     default:
1238         FIXME("aContextFlags=%08x\n", aContextFlags);
1239     };
1240
1241     hres = get_node(This->doc->basedoc.doc_node, aNode, TRUE, &node);
1242     if(FAILED(hres))
1243         return NS_ERROR_FAILURE;
1244
1245     show_context_menu(This->doc, dwID, &pt, (IDispatch*)HTMLDOMNODE(node));
1246     return NS_OK;
1247 }
1248
1249 #undef NSCML_THIS
1250
1251 static const nsIContextMenuListenerVtbl nsContextMenuListenerVtbl = {
1252     nsContextMenuListener_QueryInterface,
1253     nsContextMenuListener_AddRef,
1254     nsContextMenuListener_Release,
1255     nsContextMenuListener_OnShowContextMenu
1256 };
1257
1258 /**********************************************************
1259  *      nsIURIContentListener interface
1260  */
1261
1262 #define NSURICL_THIS(iface) DEFINE_THIS(NSContainer, URIContentListener, iface)
1263
1264 static nsresult NSAPI nsURIContentListener_QueryInterface(nsIURIContentListener *iface,
1265         nsIIDRef riid, void **result)
1266 {
1267     NSContainer *This = NSURICL_THIS(iface);
1268     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1269 }
1270
1271 static nsrefcnt NSAPI nsURIContentListener_AddRef(nsIURIContentListener *iface)
1272 {
1273     NSContainer *This = NSURICL_THIS(iface);
1274     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1275 }
1276
1277 static nsrefcnt NSAPI nsURIContentListener_Release(nsIURIContentListener *iface)
1278 {
1279     NSContainer *This = NSURICL_THIS(iface);
1280     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1281 }
1282
1283 static nsresult NSAPI nsURIContentListener_OnStartURIOpen(nsIURIContentListener *iface,
1284                                                           nsIURI *aURI, PRBool *_retval)
1285 {
1286     NSContainer *This = NSURICL_THIS(iface);
1287     nsACString spec_str;
1288     const char *spec;
1289     nsresult nsres;
1290
1291     nsACString_Init(&spec_str, NULL);
1292     nsIURI_GetSpec(aURI, &spec_str);
1293     nsACString_GetData(&spec_str, &spec);
1294
1295     TRACE("(%p)->(%p(%s) %p)\n", This, aURI, debugstr_a(spec), _retval);
1296
1297     nsACString_Finish(&spec_str);
1298
1299     nsres = on_start_uri_open(This, aURI, _retval);
1300     if(NS_FAILED(nsres))
1301         return nsres;
1302
1303     return !*_retval && This->content_listener
1304         ? nsIURIContentListener_OnStartURIOpen(This->content_listener, aURI, _retval)
1305         : NS_OK;
1306 }
1307
1308 static nsresult NSAPI nsURIContentListener_DoContent(nsIURIContentListener *iface,
1309         const char *aContentType, PRBool aIsContentPreferred, nsIRequest *aRequest,
1310         nsIStreamListener **aContentHandler, PRBool *_retval)
1311 {
1312     NSContainer *This = NSURICL_THIS(iface);
1313
1314     TRACE("(%p)->(%s %x %p %p %p)\n", This, debugstr_a(aContentType), aIsContentPreferred,
1315             aRequest, aContentHandler, _retval);
1316
1317     return This->content_listener
1318         ? nsIURIContentListener_DoContent(This->content_listener, aContentType,
1319                   aIsContentPreferred, aRequest, aContentHandler, _retval)
1320         : NS_ERROR_NOT_IMPLEMENTED;
1321 }
1322
1323 static nsresult NSAPI nsURIContentListener_IsPreferred(nsIURIContentListener *iface,
1324         const char *aContentType, char **aDesiredContentType, PRBool *_retval)
1325 {
1326     NSContainer *This = NSURICL_THIS(iface);
1327
1328     TRACE("(%p)->(%s %p %p)\n", This, debugstr_a(aContentType), aDesiredContentType, _retval);
1329
1330     /* FIXME: Should we do something here? */
1331     *_retval = TRUE; 
1332
1333     return This->content_listener
1334         ? nsIURIContentListener_IsPreferred(This->content_listener, aContentType,
1335                   aDesiredContentType, _retval)
1336         : NS_OK;
1337 }
1338
1339 static nsresult NSAPI nsURIContentListener_CanHandleContent(nsIURIContentListener *iface,
1340         const char *aContentType, PRBool aIsContentPreferred, char **aDesiredContentType,
1341         PRBool *_retval)
1342 {
1343     NSContainer *This = NSURICL_THIS(iface);
1344
1345     TRACE("(%p)->(%s %x %p %p)\n", This, debugstr_a(aContentType), aIsContentPreferred,
1346             aDesiredContentType, _retval);
1347
1348     return This->content_listener
1349         ? nsIURIContentListener_CanHandleContent(This->content_listener, aContentType,
1350                 aIsContentPreferred, aDesiredContentType, _retval)
1351         : NS_ERROR_NOT_IMPLEMENTED;
1352 }
1353
1354 static nsresult NSAPI nsURIContentListener_GetLoadCookie(nsIURIContentListener *iface,
1355         nsISupports **aLoadCookie)
1356 {
1357     NSContainer *This = NSURICL_THIS(iface);
1358
1359     WARN("(%p)->(%p)\n", This, aLoadCookie);
1360
1361     return This->content_listener
1362         ? nsIURIContentListener_GetLoadCookie(This->content_listener, aLoadCookie)
1363         : NS_ERROR_NOT_IMPLEMENTED;
1364 }
1365
1366 static nsresult NSAPI nsURIContentListener_SetLoadCookie(nsIURIContentListener *iface,
1367         nsISupports *aLoadCookie)
1368 {
1369     NSContainer *This = NSURICL_THIS(iface);
1370
1371     WARN("(%p)->(%p)\n", This, aLoadCookie);
1372
1373     return This->content_listener
1374         ? nsIURIContentListener_SetLoadCookie(This->content_listener, aLoadCookie)
1375         : NS_ERROR_NOT_IMPLEMENTED;
1376 }
1377
1378 static nsresult NSAPI nsURIContentListener_GetParentContentListener(nsIURIContentListener *iface,
1379         nsIURIContentListener **aParentContentListener)
1380 {
1381     NSContainer *This = NSURICL_THIS(iface);
1382
1383     TRACE("(%p)->(%p)\n", This, aParentContentListener);
1384
1385     if(This->content_listener)
1386         nsIURIContentListener_AddRef(This->content_listener);
1387
1388     *aParentContentListener = This->content_listener;
1389     return NS_OK;
1390 }
1391
1392 static nsresult NSAPI nsURIContentListener_SetParentContentListener(nsIURIContentListener *iface,
1393         nsIURIContentListener *aParentContentListener)
1394 {
1395     NSContainer *This = NSURICL_THIS(iface);
1396
1397     TRACE("(%p)->(%p)\n", This, aParentContentListener);
1398
1399     if(aParentContentListener == NSURICL(This))
1400         return NS_OK;
1401
1402     if(This->content_listener)
1403         nsIURIContentListener_Release(This->content_listener);
1404
1405     This->content_listener = aParentContentListener;
1406     if(This->content_listener)
1407         nsIURIContentListener_AddRef(This->content_listener);
1408
1409     return NS_OK;
1410 }
1411
1412 #undef NSURICL_THIS
1413
1414 static const nsIURIContentListenerVtbl nsURIContentListenerVtbl = {
1415     nsURIContentListener_QueryInterface,
1416     nsURIContentListener_AddRef,
1417     nsURIContentListener_Release,
1418     nsURIContentListener_OnStartURIOpen,
1419     nsURIContentListener_DoContent,
1420     nsURIContentListener_IsPreferred,
1421     nsURIContentListener_CanHandleContent,
1422     nsURIContentListener_GetLoadCookie,
1423     nsURIContentListener_SetLoadCookie,
1424     nsURIContentListener_GetParentContentListener,
1425     nsURIContentListener_SetParentContentListener
1426 };
1427
1428 /**********************************************************
1429  *      nsIEmbeddinSiteWindow interface
1430  */
1431
1432 #define NSEMBWNDS_THIS(iface) DEFINE_THIS(NSContainer, EmbeddingSiteWindow, iface)
1433
1434 static nsresult NSAPI nsEmbeddingSiteWindow_QueryInterface(nsIEmbeddingSiteWindow *iface,
1435         nsIIDRef riid, void **result)
1436 {
1437     NSContainer *This = NSEMBWNDS_THIS(iface);
1438     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1439 }
1440
1441 static nsrefcnt NSAPI nsEmbeddingSiteWindow_AddRef(nsIEmbeddingSiteWindow *iface)
1442 {
1443     NSContainer *This = NSEMBWNDS_THIS(iface);
1444     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1445 }
1446
1447 static nsrefcnt NSAPI nsEmbeddingSiteWindow_Release(nsIEmbeddingSiteWindow *iface)
1448 {
1449     NSContainer *This = NSEMBWNDS_THIS(iface);
1450     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1451 }
1452
1453 static nsresult NSAPI nsEmbeddingSiteWindow_SetDimensions(nsIEmbeddingSiteWindow *iface,
1454         PRUint32 flags, PRInt32 x, PRInt32 y, PRInt32 cx, PRInt32 cy)
1455 {
1456     NSContainer *This = NSEMBWNDS_THIS(iface);
1457     WARN("(%p)->(%08x %d %d %d %d)\n", This, flags, x, y, cx, cy);
1458     return NS_ERROR_NOT_IMPLEMENTED;
1459 }
1460
1461 static nsresult NSAPI nsEmbeddingSiteWindow_GetDimensions(nsIEmbeddingSiteWindow *iface,
1462         PRUint32 flags, PRInt32 *x, PRInt32 *y, PRInt32 *cx, PRInt32 *cy)
1463 {
1464     NSContainer *This = NSEMBWNDS_THIS(iface);
1465     WARN("(%p)->(%08x %p %p %p %p)\n", This, flags, x, y, cx, cy);
1466     return NS_ERROR_NOT_IMPLEMENTED;
1467 }
1468
1469 static nsresult NSAPI nsEmbeddingSiteWindow_SetFocus(nsIEmbeddingSiteWindow *iface)
1470 {
1471     NSContainer *This = NSEMBWNDS_THIS(iface);
1472
1473     TRACE("(%p)\n", This);
1474
1475     return nsIBaseWindow_SetFocus(This->window);
1476 }
1477
1478 static nsresult NSAPI nsEmbeddingSiteWindow_GetVisibility(nsIEmbeddingSiteWindow *iface,
1479         PRBool *aVisibility)
1480 {
1481     NSContainer *This = NSEMBWNDS_THIS(iface);
1482
1483     TRACE("(%p)->(%p)\n", This, aVisibility);
1484
1485     *aVisibility = This->doc && This->doc->hwnd && IsWindowVisible(This->doc->hwnd);
1486     return NS_OK;
1487 }
1488
1489 static nsresult NSAPI nsEmbeddingSiteWindow_SetVisibility(nsIEmbeddingSiteWindow *iface,
1490         PRBool aVisibility)
1491 {
1492     NSContainer *This = NSEMBWNDS_THIS(iface);
1493
1494     TRACE("(%p)->(%x)\n", This, aVisibility);
1495
1496     return NS_OK;
1497 }
1498
1499 static nsresult NSAPI nsEmbeddingSiteWindow_GetTitle(nsIEmbeddingSiteWindow *iface,
1500         PRUnichar **aTitle)
1501 {
1502     NSContainer *This = NSEMBWNDS_THIS(iface);
1503     WARN("(%p)->(%p)\n", This, aTitle);
1504     return NS_ERROR_NOT_IMPLEMENTED;
1505 }
1506
1507 static nsresult NSAPI nsEmbeddingSiteWindow_SetTitle(nsIEmbeddingSiteWindow *iface,
1508         const PRUnichar *aTitle)
1509 {
1510     NSContainer *This = NSEMBWNDS_THIS(iface);
1511     WARN("(%p)->(%s)\n", This, debugstr_w(aTitle));
1512     return NS_ERROR_NOT_IMPLEMENTED;
1513 }
1514
1515 static nsresult NSAPI nsEmbeddingSiteWindow_GetSiteWindow(nsIEmbeddingSiteWindow *iface,
1516         void **aSiteWindow)
1517 {
1518     NSContainer *This = NSEMBWNDS_THIS(iface);
1519
1520     TRACE("(%p)->(%p)\n", This, aSiteWindow);
1521
1522     *aSiteWindow = This->hwnd;
1523     return NS_OK;
1524 }
1525
1526 static const nsIEmbeddingSiteWindowVtbl nsEmbeddingSiteWindowVtbl = {
1527     nsEmbeddingSiteWindow_QueryInterface,
1528     nsEmbeddingSiteWindow_AddRef,
1529     nsEmbeddingSiteWindow_Release,
1530     nsEmbeddingSiteWindow_SetDimensions,
1531     nsEmbeddingSiteWindow_GetDimensions,
1532     nsEmbeddingSiteWindow_SetFocus,
1533     nsEmbeddingSiteWindow_GetVisibility,
1534     nsEmbeddingSiteWindow_SetVisibility,
1535     nsEmbeddingSiteWindow_GetTitle,
1536     nsEmbeddingSiteWindow_SetTitle,
1537     nsEmbeddingSiteWindow_GetSiteWindow
1538 };
1539
1540 #define NSTOOLTIP_THIS(iface) DEFINE_THIS(NSContainer, TooltipListener, iface)
1541
1542 static nsresult NSAPI nsTooltipListener_QueryInterface(nsITooltipListener *iface, nsIIDRef riid,
1543         void **result)
1544 {
1545     NSContainer *This = NSTOOLTIP_THIS(iface);
1546     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1547 }
1548
1549 static nsrefcnt NSAPI nsTooltipListener_AddRef(nsITooltipListener *iface)
1550 {
1551     NSContainer *This = NSTOOLTIP_THIS(iface);
1552     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1553 }
1554
1555 static nsrefcnt NSAPI nsTooltipListener_Release(nsITooltipListener *iface)
1556 {
1557     NSContainer *This = NSTOOLTIP_THIS(iface);
1558     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1559 }
1560
1561 static nsresult NSAPI nsTooltipListener_OnShowTooltip(nsITooltipListener *iface,
1562         PRInt32 aXCoord, PRInt32 aYCoord, const PRUnichar *aTipText)
1563 {
1564     NSContainer *This = NSTOOLTIP_THIS(iface);
1565
1566     if (This->doc)
1567         show_tooltip(This->doc, aXCoord, aYCoord, aTipText);
1568
1569     return NS_OK;
1570 }
1571
1572 static nsresult NSAPI nsTooltipListener_OnHideTooltip(nsITooltipListener *iface)
1573 {
1574     NSContainer *This = NSTOOLTIP_THIS(iface);
1575
1576     if (This->doc)
1577         hide_tooltip(This->doc);
1578
1579     return NS_OK;
1580 }
1581
1582 #undef NSTOOLTIM_THIS
1583
1584 static const nsITooltipListenerVtbl nsTooltipListenerVtbl = {
1585     nsTooltipListener_QueryInterface,
1586     nsTooltipListener_AddRef,
1587     nsTooltipListener_Release,
1588     nsTooltipListener_OnShowTooltip,
1589     nsTooltipListener_OnHideTooltip
1590 };
1591
1592 #define NSIFACEREQ_THIS(iface) DEFINE_THIS(NSContainer, InterfaceRequestor, iface)
1593
1594 static nsresult NSAPI nsInterfaceRequestor_QueryInterface(nsIInterfaceRequestor *iface,
1595         nsIIDRef riid, void **result)
1596 {
1597     NSContainer *This = NSIFACEREQ_THIS(iface);
1598     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1599 }
1600
1601 static nsrefcnt NSAPI nsInterfaceRequestor_AddRef(nsIInterfaceRequestor *iface)
1602 {
1603     NSContainer *This = NSIFACEREQ_THIS(iface);
1604     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1605 }
1606
1607 static nsrefcnt NSAPI nsInterfaceRequestor_Release(nsIInterfaceRequestor *iface)
1608 {
1609     NSContainer *This = NSIFACEREQ_THIS(iface);
1610     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1611 }
1612
1613 static nsresult NSAPI nsInterfaceRequestor_GetInterface(nsIInterfaceRequestor *iface,
1614         nsIIDRef riid, void **result)
1615 {
1616     NSContainer *This = NSIFACEREQ_THIS(iface);
1617
1618     if(IsEqualGUID(&IID_nsIDOMWindow, riid)) {
1619         TRACE("(%p)->(IID_nsIDOMWindow %p)\n", This, result);
1620         return nsIWebBrowser_GetContentDOMWindow(This->webbrowser, (nsIDOMWindow**)result);
1621     }
1622
1623     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1624 }
1625
1626 #undef NSIFACEREQ_THIS
1627
1628 static const nsIInterfaceRequestorVtbl nsInterfaceRequestorVtbl = {
1629     nsInterfaceRequestor_QueryInterface,
1630     nsInterfaceRequestor_AddRef,
1631     nsInterfaceRequestor_Release,
1632     nsInterfaceRequestor_GetInterface
1633 };
1634
1635 #define NSWEAKREF_THIS(iface) DEFINE_THIS(NSContainer, WeakReference, iface)
1636
1637 static nsresult NSAPI nsWeakReference_QueryInterface(nsIWeakReference *iface,
1638         nsIIDRef riid, void **result)
1639 {
1640     NSContainer *This = NSWEAKREF_THIS(iface);
1641     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1642 }
1643
1644 static nsrefcnt NSAPI nsWeakReference_AddRef(nsIWeakReference *iface)
1645 {
1646     NSContainer *This = NSWEAKREF_THIS(iface);
1647     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1648 }
1649
1650 static nsrefcnt NSAPI nsWeakReference_Release(nsIWeakReference *iface)
1651 {
1652     NSContainer *This = NSWEAKREF_THIS(iface);
1653     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1654 }
1655
1656 static nsresult NSAPI nsWeakReference_QueryReferent(nsIWeakReference *iface,
1657         const nsIID *riid, void **result)
1658 {
1659     NSContainer *This = NSWEAKREF_THIS(iface);
1660     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1661 }
1662
1663 #undef NSWEAKREF_THIS
1664
1665 static const nsIWeakReferenceVtbl nsWeakReferenceVtbl = {
1666     nsWeakReference_QueryInterface,
1667     nsWeakReference_AddRef,
1668     nsWeakReference_Release,
1669     nsWeakReference_QueryReferent
1670 };
1671
1672 #define NSSUPWEAKREF_THIS(iface) DEFINE_THIS(NSContainer, SupportsWeakReference, iface)
1673
1674 static nsresult NSAPI nsSupportsWeakReference_QueryInterface(nsISupportsWeakReference *iface,
1675         nsIIDRef riid, void **result)
1676 {
1677     NSContainer *This = NSSUPWEAKREF_THIS(iface);
1678     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1679 }
1680
1681 static nsrefcnt NSAPI nsSupportsWeakReference_AddRef(nsISupportsWeakReference *iface)
1682 {
1683     NSContainer *This = NSSUPWEAKREF_THIS(iface);
1684     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1685 }
1686
1687 static nsrefcnt NSAPI nsSupportsWeakReference_Release(nsISupportsWeakReference *iface)
1688 {
1689     NSContainer *This = NSSUPWEAKREF_THIS(iface);
1690     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1691 }
1692
1693 static nsresult NSAPI nsSupportsWeakReference_GetWeakReference(nsISupportsWeakReference *iface,
1694         nsIWeakReference **_retval)
1695 {
1696     NSContainer *This = NSSUPWEAKREF_THIS(iface);
1697
1698     TRACE("(%p)->(%p)\n", This, _retval);
1699
1700     nsIWeakReference_AddRef(NSWEAKREF(This));
1701     *_retval = NSWEAKREF(This);
1702     return NS_OK;
1703 }
1704
1705 #undef NSWEAKREF_THIS
1706
1707 static const nsISupportsWeakReferenceVtbl nsSupportsWeakReferenceVtbl = {
1708     nsSupportsWeakReference_QueryInterface,
1709     nsSupportsWeakReference_AddRef,
1710     nsSupportsWeakReference_Release,
1711     nsSupportsWeakReference_GetWeakReference
1712 };
1713
1714
1715 NSContainer *NSContainer_Create(HTMLDocumentObj *doc, NSContainer *parent)
1716 {
1717     nsIWebBrowserSetup *wbsetup;
1718     nsIScrollable *scrollable;
1719     NSContainer *ret;
1720     nsresult nsres;
1721
1722     if(!load_gecko(TRUE))
1723         return NULL;
1724
1725     ret = heap_alloc_zero(sizeof(NSContainer));
1726
1727     ret->lpWebBrowserChromeVtbl      = &nsWebBrowserChromeVtbl;
1728     ret->lpContextMenuListenerVtbl   = &nsContextMenuListenerVtbl;
1729     ret->lpURIContentListenerVtbl    = &nsURIContentListenerVtbl;
1730     ret->lpEmbeddingSiteWindowVtbl   = &nsEmbeddingSiteWindowVtbl;
1731     ret->lpTooltipListenerVtbl       = &nsTooltipListenerVtbl;
1732     ret->lpInterfaceRequestorVtbl    = &nsInterfaceRequestorVtbl;
1733     ret->lpWeakReferenceVtbl         = &nsWeakReferenceVtbl;
1734     ret->lpSupportsWeakReferenceVtbl = &nsSupportsWeakReferenceVtbl;
1735
1736     ret->doc = doc;
1737     ret->ref = 1;
1738
1739     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_WEBBROWSER_CONTRACTID,
1740             NULL, &IID_nsIWebBrowser, (void**)&ret->webbrowser);
1741     if(NS_FAILED(nsres)) {
1742         ERR("Creating WebBrowser failed: %08x\n", nsres);
1743         heap_free(ret);
1744         return NULL;
1745     }
1746
1747     if(parent)
1748         nsIWebBrowserChrome_AddRef(NSWBCHROME(parent));
1749     ret->parent = parent;
1750
1751     nsres = nsIWebBrowser_SetContainerWindow(ret->webbrowser, NSWBCHROME(ret));
1752     if(NS_FAILED(nsres))
1753         ERR("SetContainerWindow failed: %08x\n", nsres);
1754
1755     nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIBaseWindow,
1756             (void**)&ret->window);
1757     if(NS_FAILED(nsres))
1758         ERR("Could not get nsIBaseWindow interface: %08x\n", nsres);
1759
1760     nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIWebBrowserSetup,
1761                                          (void**)&wbsetup);
1762     if(NS_SUCCEEDED(nsres)) {
1763         nsres = nsIWebBrowserSetup_SetProperty(wbsetup, SETUP_IS_CHROME_WRAPPER, FALSE);
1764         nsIWebBrowserSetup_Release(wbsetup);
1765         if(NS_FAILED(nsres))
1766             ERR("SetProperty(SETUP_IS_CHROME_WRAPPER) failed: %08x\n", nsres);
1767     }else {
1768         ERR("Could not get nsIWebBrowserSetup interface\n");
1769     }
1770
1771     nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIWebNavigation,
1772             (void**)&ret->navigation);
1773     if(NS_FAILED(nsres))
1774         ERR("Could not get nsIWebNavigation interface: %08x\n", nsres);
1775
1776     nsres = nsIWebBrowserFocus_QueryInterface(ret->webbrowser, &IID_nsIWebBrowserFocus,
1777             (void**)&ret->focus);
1778     if(NS_FAILED(nsres))
1779         ERR("Could not get nsIWebBrowserFocus interface: %08x\n", nsres);
1780
1781     if(!nscontainer_class)
1782         register_nscontainer_class();
1783
1784     ret->hwnd = CreateWindowExW(0, wszNsContainer, NULL,
1785             WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 0, 0, 100, 100,
1786             GetDesktopWindow(), NULL, hInst, ret);
1787
1788     nsres = nsIBaseWindow_InitWindow(ret->window, ret->hwnd, NULL, 0, 0, 100, 100);
1789     if(NS_SUCCEEDED(nsres)) {
1790         nsres = nsIBaseWindow_Create(ret->window);
1791         if(NS_FAILED(nsres))
1792             WARN("Creating window failed: %08x\n", nsres);
1793
1794         nsIBaseWindow_SetVisibility(ret->window, FALSE);
1795         nsIBaseWindow_SetEnabled(ret->window, FALSE);
1796     }else {
1797         ERR("InitWindow failed: %08x\n", nsres);
1798     }
1799
1800     nsres = nsIWebBrowser_SetParentURIContentListener(ret->webbrowser, NSURICL(ret));
1801     if(NS_FAILED(nsres))
1802         ERR("SetParentURIContentListener failed: %08x\n", nsres);
1803
1804     nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIScrollable, (void**)&scrollable);
1805     if(NS_SUCCEEDED(nsres)) {
1806         nsres = nsIScrollable_SetDefaultScrollbarPreferences(scrollable,
1807                 ScrollOrientation_Y, Scrollbar_Always);
1808         if(NS_FAILED(nsres))
1809             ERR("Could not set default Y scrollbar prefs: %08x\n", nsres);
1810
1811         nsres = nsIScrollable_SetDefaultScrollbarPreferences(scrollable,
1812                 ScrollOrientation_X, Scrollbar_Auto);
1813         if(NS_FAILED(nsres))
1814             ERR("Could not set default X scrollbar prefs: %08x\n", nsres);
1815
1816         nsIScrollable_Release(scrollable);
1817     }else {
1818         ERR("Could not get nsIScrollable: %08x\n", nsres);
1819     }
1820
1821     return ret;
1822 }
1823
1824 void NSContainer_Release(NSContainer *This)
1825 {
1826     TRACE("(%p)\n", This);
1827
1828     This->doc = NULL;
1829
1830     ShowWindow(This->hwnd, SW_HIDE);
1831     SetParent(This->hwnd, NULL);
1832
1833     nsIBaseWindow_SetVisibility(This->window, FALSE);
1834     nsIBaseWindow_Destroy(This->window);
1835
1836     nsIWebBrowser_SetContainerWindow(This->webbrowser, NULL);
1837
1838     nsIWebBrowser_Release(This->webbrowser);
1839     This->webbrowser = NULL;
1840
1841     nsIWebNavigation_Release(This->navigation);
1842     This->navigation = NULL;
1843
1844     nsIBaseWindow_Release(This->window);
1845     This->window = NULL;
1846
1847     nsIWebBrowserFocus_Release(This->focus);
1848     This->focus = NULL;
1849
1850     if(This->editor_controller) {
1851         nsIController_Release(This->editor_controller);
1852         This->editor_controller = NULL;
1853     }
1854
1855     if(This->editor) {
1856         nsIEditor_Release(This->editor);
1857         This->editor = NULL;
1858     }
1859
1860     if(This->content_listener) {
1861         nsIURIContentListener_Release(This->content_listener);
1862         This->content_listener = NULL;
1863     }
1864
1865     if(This->hwnd) {
1866         DestroyWindow(This->hwnd);
1867         This->hwnd = NULL;
1868     }
1869
1870     nsIWebBrowserChrome_Release(NSWBCHROME(This));
1871 }