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