janitorial: Remove remaining NULL checks before free() (found by Smatch).
[wine] / dlls / mshtml / nsembed.c
1 /*
2  * Copyright 2005-2006 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
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
33
34 #include "mshtml_private.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
37
38 #define NS_APPSTARTUPNOTIFIER_CONTRACTID "@mozilla.org/embedcomp/appstartup-notifier;1"
39 #define NS_WEBBROWSER_CONTRACTID "@mozilla.org/embedding/browser/nsWebBrowser;1"
40 #define NS_PROFILE_CONTRACTID "@mozilla.org/profile/manager;1"
41 #define NS_MEMORY_CONTRACTID "@mozilla.org/xpcom/memory-service;1"
42 #define NS_STRINGSTREAM_CONTRACTID "@mozilla.org/io/string-input-stream;1"
43 #define NS_COMMANDPARAMS_CONTRACTID "@mozilla.org/embedcomp/command-params;1"
44 #define NS_HTMLSERIALIZER_CONTRACTID "@mozilla.org/layout/contentserializer;1?mimetype=text/html"
45
46 #define APPSTARTUP_TOPIC "app-startup"
47
48 #define PR_UINT32_MAX 0xffffffff
49
50 struct nsCStringContainer {
51     void *v;
52     void *d1;
53     PRUint32 d2;
54     void *d3;
55 };
56
57 static nsresult (*NS_InitXPCOM2)(nsIServiceManager**,void*,void*);
58 static nsresult (*NS_ShutdownXPCOM)(nsIServiceManager*);
59 static nsresult (*NS_GetComponentRegistrar)(nsIComponentRegistrar**);
60 static nsresult (*NS_StringContainerInit)(nsStringContainer*);
61 static nsresult (*NS_CStringContainerInit)(nsCStringContainer*);
62 static nsresult (*NS_StringContainerFinish)(nsStringContainer*);
63 static nsresult (*NS_CStringContainerFinish)(nsCStringContainer*);
64 static nsresult (*NS_StringSetData)(nsAString*,const PRUnichar*,PRUint32);
65 static nsresult (*NS_CStringSetData)(nsACString*,const char*,PRUint32);
66 static nsresult (*NS_NewLocalFile)(const nsAString*,PRBool,nsIFile**);
67 static PRUint32 (*NS_StringGetData)(const nsAString*,const PRUnichar **,PRBool*);
68 static PRUint32 (*NS_CStringGetData)(const nsACString*,const char**,PRBool*);
69
70 static HINSTANCE hXPCOM = NULL;
71
72 static nsIServiceManager *pServMgr = NULL;
73 static nsIComponentManager *pCompMgr = NULL;
74 static nsIMemory *nsmem = NULL;
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
80 static LRESULT WINAPI nsembed_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
81 {
82     NSContainer *This;
83     nsresult nsres;
84
85     static const WCHAR wszTHIS[] = {'T','H','I','S',0};
86
87     if(msg == WM_CREATE) {
88         This = *(NSContainer**)lParam;
89         SetPropW(hwnd, wszTHIS, This);
90     }else {
91         This = (NSContainer*)GetPropW(hwnd, wszTHIS);
92     }
93
94     switch(msg) {
95         case WM_SIZE:
96             TRACE("(%p)->(WM_SIZE)\n", This);
97
98             nsres = nsIBaseWindow_SetSize(This->window,
99                     LOWORD(lParam), HIWORD(lParam), TRUE);
100             if(NS_FAILED(nsres))
101                 WARN("SetSize failed: %08x\n", nsres);
102     }
103
104     return DefWindowProcW(hwnd, msg, wParam, lParam);
105 }
106
107
108 static void register_nscontainer_class(void)
109 {
110     static WNDCLASSEXW wndclass = {
111         sizeof(WNDCLASSEXW),
112         CS_DBLCLKS,
113         nsembed_proc,
114         0, 0, NULL, NULL, NULL, NULL, NULL,
115         wszNsContainer,
116         NULL,
117     };
118     wndclass.hInstance = hInst;
119     nscontainer_class = RegisterClassExW(&wndclass);
120 }
121
122 static BOOL load_xpcom(PRUnichar *gre_path)
123 {
124     WCHAR path_env[MAX_PATH];
125     int len;
126
127     static const WCHAR wszPATH[] = {'P','A','T','H',0};
128     static const WCHAR strXPCOM[] = {'x','p','c','o','m','.','d','l','l',0};
129
130     TRACE("(%s)\n", debugstr_w(gre_path));
131
132     /* We have to modify PATH as XPCOM loads other DLLs from this directory. */
133     GetEnvironmentVariableW(wszPATH, path_env, sizeof(path_env)/sizeof(WCHAR));
134     len = strlenW(path_env);
135     path_env[len++] = ';';
136     strcpyW(path_env+len, gre_path);
137     SetEnvironmentVariableW(wszPATH, path_env);
138
139     hXPCOM = LoadLibraryW(strXPCOM);
140     if(!hXPCOM) {
141         WARN("Could not load XPCOM: %d\n", GetLastError());
142         return FALSE;
143     }
144
145 #define NS_DLSYM(func) \
146     func = (typeof(func))GetProcAddress(hXPCOM, #func); \
147     if(!func) \
148         ERR("Could not GetProcAddress(" #func ") failed\n")
149
150     NS_DLSYM(NS_InitXPCOM2);
151     NS_DLSYM(NS_ShutdownXPCOM);
152     NS_DLSYM(NS_GetComponentRegistrar);
153     NS_DLSYM(NS_StringContainerInit);
154     NS_DLSYM(NS_CStringContainerInit);
155     NS_DLSYM(NS_StringContainerFinish);
156     NS_DLSYM(NS_CStringContainerFinish);
157     NS_DLSYM(NS_StringSetData);
158     NS_DLSYM(NS_CStringSetData);
159     NS_DLSYM(NS_NewLocalFile);
160     NS_DLSYM(NS_StringGetData);
161     NS_DLSYM(NS_CStringGetData);
162
163 #undef NS_DLSYM
164
165     return TRUE;
166 }
167
168 static BOOL load_mozilla(PRUnichar *gre_path)
169 {
170     DWORD res, type, i, size = MAX_PATH;
171     HKEY mozilla_key, hkey;
172     WCHAR key_name[100];
173     BOOL ret = FALSE;
174
175     static const WCHAR wszGreKey[] =
176         {'S','o','f','t','w','a','r','e','\\',
177             'm','o','z','i','l','l','a','.','o','r','g','\\',
178                 'G','R','E',0};
179
180     static const WCHAR wszGreHome[] = {'G','r','e','H','o','m','e',0};
181
182     res = RegOpenKeyW(HKEY_LOCAL_MACHINE, wszGreKey, &mozilla_key);
183     if(res != ERROR_SUCCESS) {
184         TRACE("Could not open key %s\n", debugstr_w(wszGreKey));
185         return FALSE;
186     }
187
188     for(i=0; !ret && RegEnumKeyW(mozilla_key, i, key_name, sizeof(key_name)/sizeof(WCHAR)) == ERROR_SUCCESS; i++) {
189         RegOpenKeyW(mozilla_key, key_name, &hkey);
190         res = RegQueryValueExW(hkey, wszGreHome, NULL, &type, (LPBYTE)gre_path, &size);
191         if(res == ERROR_SUCCESS)
192             ret = TRUE;
193         RegCloseKey(hkey);
194     }
195
196     RegCloseKey(mozilla_key);
197     return ret ? load_xpcom(gre_path) : FALSE;
198 }
199
200 static BOOL load_mozctl(PRUnichar *gre_path)
201 {
202     HKEY hkey;
203     DWORD res, type, size = MAX_PATH;
204
205     static const WCHAR wszMozCtlKey[] =
206         {'S','o','f','t','w','a','r','e','\\','M','o','z','i','l','l','a',0};
207     static const WCHAR wszBinDirectoryPath[] =
208         {'B','i','n','D','i','r','e','c','t','o','r','y','P','a','t','h',0};
209     static const WCHAR wszMozCtlClsidKey[] =
210         {'C','L','S','I','D','\\',
211          '{','1','3','3','9','B','5','4','C','-','3','4','5','3','-','1','1','D','2',
212          '-','9','3','B','9','-','0','0','0','0','0','0','0','0','0','0','0','0','}','\\',
213          'I','n','p','r','o','c','S','e','r','v','e','r','3','2',0};
214
215     res = RegOpenKeyW(HKEY_LOCAL_MACHINE, wszMozCtlKey, &hkey);
216     if(res == ERROR_SUCCESS) {
217         res = RegQueryValueExW(hkey, wszBinDirectoryPath, NULL, &type, (LPBYTE)gre_path, &size);
218         if(res == ERROR_SUCCESS)
219             return load_xpcom(gre_path);
220         else
221             ERR("Could not get value %s\n", debugstr_w(wszBinDirectoryPath));
222     }
223
224     res = RegOpenKeyW(HKEY_CLASSES_ROOT, wszMozCtlClsidKey, &hkey);
225     if(res == ERROR_SUCCESS) {
226         res = RegQueryValueExW(hkey, NULL, NULL, &type, (LPBYTE)gre_path, &size);
227         if(res == ERROR_SUCCESS) {
228             WCHAR *ptr;
229             if((ptr = strrchrW(gre_path, '\\')))
230                 ptr[1] = 0;
231             load_xpcom(gre_path);
232         }else {
233             ERR("Could not get value of %s\n", debugstr_w(wszMozCtlClsidKey));
234         }
235     }
236
237     TRACE("Could not find Mozilla ActiveX Control\n");
238
239     return FALSE;
240 }
241
242 static BOOL load_wine_gecko(PRUnichar *gre_path)
243 {
244     HKEY hkey;
245     DWORD res, type, size = MAX_PATH;
246
247     static const WCHAR wszMshtmlKey[] = {
248         'S','o','f','t','w','a','r','e','\\','W','i','n','e',
249         '\\','M','S','H','T','M','L',0};
250     static const WCHAR wszGeckoPath[] =
251         {'G','e','c','k','o','P','a','t','h',0};
252
253     /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
254     res = RegOpenKeyW(HKEY_CURRENT_USER, wszMshtmlKey, &hkey);
255     if(res != ERROR_SUCCESS)
256         return FALSE;
257
258     res = RegQueryValueExW(hkey, wszGeckoPath, NULL, &type, (LPBYTE)gre_path, &size);
259     if(res != ERROR_SUCCESS || type != REG_SZ)
260         return FALSE;
261
262     return load_xpcom(gre_path);
263 }
264
265 static void set_profile(void)
266 {
267     nsIProfile *profile;
268     PRBool exists = FALSE;
269     nsresult nsres;
270
271     static const WCHAR wszMSHTML[] = {'M','S','H','T','M','L',0};
272
273     nsres = nsIServiceManager_GetServiceByContactID(pServMgr, NS_PROFILE_CONTRACTID,
274                                          &IID_nsIProfile, (void**)&profile);
275     if(NS_FAILED(nsres)) {
276         ERR("Could not get profile service: %08x\n", nsres);
277         return;
278     }
279
280     nsres = nsIProfile_ProfileExists(profile, wszMSHTML, &exists);
281     if(!exists) {
282         nsres = nsIProfile_CreateNewProfile(profile, wszMSHTML, NULL, NULL, FALSE);
283         if(NS_FAILED(nsres))
284             ERR("CreateNewProfile failed: %08x\n", nsres);
285     }
286
287     nsres = nsIProfile_SetCurrentProfile(profile, wszMSHTML);
288     if(NS_FAILED(nsres))
289         ERR("SetCurrentProfile failed: %08x\n", nsres);
290
291     nsIProfile_Release(profile);
292 }
293
294 static BOOL load_gecko(void)
295 {
296     nsresult nsres;
297     nsIObserver *pStartNotif;
298     nsIComponentRegistrar *registrar = NULL;
299     nsAString path;
300     nsIFile *gre_dir;
301     PRUnichar gre_path[MAX_PATH];
302
303     static BOOL tried_load = FALSE;
304
305     TRACE("()\n");
306
307     if(tried_load)
308         return pCompMgr != NULL;
309     tried_load = TRUE;
310
311     if(!load_wine_gecko(gre_path) && !load_mozctl(gre_path) && !load_mozilla(gre_path)) {
312         install_wine_gecko();
313         if(!load_wine_gecko(gre_path)) {
314             MESSAGE("Could not load Mozilla. HTML rendering will be disabled.\n");
315             return FALSE;
316         }
317     }
318
319     nsAString_Init(&path, gre_path);
320     nsres = NS_NewLocalFile(&path, FALSE, &gre_dir);
321     nsAString_Finish(&path);
322     if(NS_FAILED(nsres)) {
323         ERR("NS_NewLocalFile failed: %08x\n", nsres);
324         FreeLibrary(hXPCOM);
325         return FALSE;
326     }
327
328     nsres = NS_InitXPCOM2(&pServMgr, gre_dir, NULL);
329     if(NS_FAILED(nsres)) {
330         ERR("NS_InitXPCOM2 failed: %08x\n", nsres);
331         FreeLibrary(hXPCOM);
332         return FALSE;
333     }
334
335     nsres = nsIServiceManager_QueryInterface(pServMgr, &IID_nsIComponentManager, (void**)&pCompMgr);
336     if(NS_FAILED(nsres))
337         ERR("Could not get nsIComponentManager: %08x\n", nsres);
338
339     nsres = NS_GetComponentRegistrar(&registrar);
340     if(NS_SUCCEEDED(nsres)) {
341         nsres = nsIComponentRegistrar_AutoRegister(registrar, NULL);
342         if(NS_FAILED(nsres))
343             ERR("AutoRegister(NULL) failed: %08x\n", nsres);
344
345         nsres = nsIComponentRegistrar_AutoRegister(registrar, gre_dir);
346         if(NS_FAILED(nsres))
347             ERR("AutoRegister(gre_dir) failed: %08x\n", nsres);
348
349         init_nsio(pCompMgr, registrar);
350     }else {
351         ERR("NS_GetComponentRegistrar failed: %08x\n", nsres);
352     }
353
354     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_APPSTARTUPNOTIFIER_CONTRACTID,
355             NULL, &IID_nsIObserver, (void**)&pStartNotif);
356     if(NS_SUCCEEDED(nsres)) {
357         nsres = nsIObserver_Observe(pStartNotif, NULL, APPSTARTUP_TOPIC, NULL);
358         if(NS_FAILED(nsres))
359             ERR("Observe failed: %08x\n", nsres);
360
361         nsIObserver_Release(pStartNotif);
362     }else {
363         ERR("could not get appstartup-notifier: %08x\n", nsres);
364     }
365
366     set_profile();
367
368     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_MEMORY_CONTRACTID,
369             NULL, &IID_nsIMemory, (void**)&nsmem);
370     if(NS_FAILED(nsres))
371         ERR("Could not get nsIMemory: %08x\n", nsres);
372
373     if(registrar) {
374         register_nsservice(registrar, pServMgr);
375         nsIComponentRegistrar_Release(registrar);
376     }
377
378     return TRUE;
379 }
380
381 void *nsalloc(size_t size)
382 {
383     return nsIMemory_Alloc(nsmem, size);
384 }
385
386 void nsfree(void *mem)
387 {
388     nsIMemory_Free(nsmem, mem);
389 }
390
391 void nsACString_Init(nsACString *str, const char *data)
392 {
393     NS_CStringContainerInit(str);
394     if(data)
395         NS_CStringSetData(str, data, PR_UINT32_MAX);
396 }
397
398 PRUint32 nsACString_GetData(const nsACString *str, const char **data, PRBool *termited)
399 {
400     return NS_CStringGetData(str, data, termited);
401 }
402
403 void nsACString_Finish(nsACString *str)
404 {
405     NS_CStringContainerFinish(str);
406 }
407
408 void nsAString_Init(nsAString *str, const PRUnichar *data)
409 {
410     NS_StringContainerInit(str);
411     if(data)
412         NS_StringSetData(str, data, PR_UINT32_MAX);
413 }
414
415 PRUint32 nsAString_GetData(const nsAString *str, const PRUnichar **data, PRBool *termited)
416 {
417     return NS_StringGetData(str, data, termited);
418 }
419
420 void nsAString_Finish(nsAString *str)
421 {
422     NS_StringContainerFinish(str);
423 }
424
425 nsIInputStream *create_nsstream(const char *data, PRInt32 data_len)
426 {
427     nsIStringInputStream *ret;
428     nsresult nsres;
429
430     if(!pCompMgr)
431         return NULL;
432
433     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
434             NS_STRINGSTREAM_CONTRACTID, NULL, &IID_nsIStringInputStream,
435             (void**)&ret);
436     if(NS_FAILED(nsres)) {
437         ERR("Could not get nsIStringInputStream\n");
438         return NULL;
439     }
440
441     nsres = nsIStringInputStream_SetData(ret, data, data_len);
442     if(NS_FAILED(nsres)) {
443         ERR("AdoptData failed: %08x\n", nsres);
444         nsIStringInputStream_Release(ret);
445         return NULL;
446     }
447
448     return (nsIInputStream*)ret;
449 }
450
451 nsICommandParams *create_nscommand_params(void)
452 {
453     nsICommandParams *ret = NULL;
454     nsresult nsres;
455
456     if(!pCompMgr)
457         return NULL;
458
459     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
460             NS_COMMANDPARAMS_CONTRACTID, NULL, &IID_nsICommandParams,
461             (void**)&ret);
462     if(NS_FAILED(nsres))
463         ERR("Could not get nsICommandParams\n");
464
465     return ret;
466 }
467
468 static void nsnode_to_nsstring_rec(nsIContentSerializer *serializer, nsIDOMNode *nsnode, nsAString *str)
469 {
470     nsIDOMNodeList *node_list = NULL;
471     PRBool has_children = FALSE;
472     PRUint16 type;
473     nsresult nsres;
474
475     nsIDOMNode_HasChildNodes(nsnode, &has_children);
476
477     nsres = nsIDOMNode_GetNodeType(nsnode, &type);
478     if(NS_FAILED(nsres)) {
479         ERR("GetType failed: %08x\n", nsres);
480         return;
481     }
482
483     switch(type) {
484     case ELEMENT_NODE: {
485         nsIDOMElement *nselem;
486         nsIDOMNode_QueryInterface(nsnode, &IID_nsIDOMElement, (void**)&nselem);
487         nsIContentSerializer_AppendElementStart(serializer, nselem, has_children, str);
488         nsIDOMElement_Release(nselem);
489         break;
490     }
491     case TEXT_NODE: {
492         nsIDOMText *nstext;
493         nsIDOMNode_QueryInterface(nsnode, &IID_nsIDOMText, (void**)&nstext);
494         nsIContentSerializer_AppendText(serializer, nstext, 0, -1, str);
495         nsIDOMText_Release(nstext);
496         break;
497     }
498     case DOCUMENT_NODE: {
499         nsIDOMDocument *nsdoc;
500         nsIDOMNode_QueryInterface(nsnode, &IID_nsIDOMDocument, (void**)&nsdoc);
501         nsIContentSerializer_AppendDocumentStart(serializer, nsdoc, str);
502         nsIDOMDocument_Release(nsdoc);
503     }
504     default:
505         FIXME("Unhandled type %u\n", type);
506     }
507
508     if(has_children) {
509         PRUint32 child_cnt, i;
510         nsIDOMNode *child_node;
511
512         nsIDOMNode_GetChildNodes(nsnode, &node_list);
513         nsIDOMNodeList_GetLength(node_list, &child_cnt);
514
515         for(i=0; i<child_cnt; i++) {
516             nsres = nsIDOMNodeList_Item(node_list, i, &child_node);
517             if(NS_SUCCEEDED(nsres)) {
518                 nsnode_to_nsstring_rec(serializer, child_node, str);
519                 nsIDOMNode_Release(child_node);
520             }else {
521                 ERR("Item failed: %08x\n", nsres);
522             }
523         }
524
525         nsIDOMNodeList_Release(node_list);
526     }
527
528     if(type == ELEMENT_NODE) {
529         nsIDOMElement *nselem;
530         nsIDOMNode_QueryInterface(nsnode, &IID_nsIDOMElement, (void**)&nselem);
531         nsIContentSerializer_AppendElementEnd(serializer, nselem, str);
532         nsIDOMElement_Release(nselem);
533     }
534 }
535
536 void nsnode_to_nsstring(nsIDOMNode *nsdoc, nsAString *str)
537 {
538     nsIContentSerializer *serializer;
539     nsIDOMNode *nsnode;
540     nsresult nsres;
541
542     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
543             NS_HTMLSERIALIZER_CONTRACTID, NULL, &IID_nsIContentSerializer,
544             (void**)&serializer);
545     if(NS_FAILED(nsres)) {
546         ERR("Could not get nsIContentSerializer: %08x\n", nsres);
547         return;
548     }
549
550     nsres = nsIContentSerializer_Init(serializer, 0, 100, NULL, FALSE);
551     if(NS_FAILED(nsres))
552         ERR("Init failed: %08x\n", nsres);
553
554     nsIDOMDocument_QueryInterface(nsdoc, &IID_nsIDOMNode, (void**)&nsnode);
555     nsnode_to_nsstring_rec(serializer, nsnode, str);
556     nsIDOMNode_Release(nsnode);
557
558     nsres = nsIContentSerializer_Flush(serializer, str);
559     if(NS_FAILED(nsres))
560         ERR("Flush failed: %08x\n", nsres);
561
562     nsIContentSerializer_Release(serializer);
563 }
564
565 void close_gecko()
566 {
567     TRACE("()\n");
568
569     if(pCompMgr)
570         nsIComponentManager_Release(pCompMgr);
571
572     if(pServMgr)
573         nsIServiceManager_Release(pServMgr);
574
575     if(nsmem)
576         nsIMemory_Release(nsmem);
577
578     if(hXPCOM)
579         FreeLibrary(hXPCOM);
580 }
581
582 /**********************************************************
583  *      nsIWebBrowserChrome interface
584  */
585
586 #define NSWBCHROME_THIS(iface) DEFINE_THIS(NSContainer, WebBrowserChrome, iface)
587
588 static nsresult NSAPI nsWebBrowserChrome_QueryInterface(nsIWebBrowserChrome *iface,
589         nsIIDRef riid, nsQIResult result)
590 {
591     NSContainer *This = NSWBCHROME_THIS(iface);
592
593     *result = NULL;
594     if(IsEqualGUID(&IID_nsISupports, riid)) {
595         TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
596         *result = NSWBCHROME(This);
597     }else if(IsEqualGUID(&IID_nsIWebBrowserChrome, riid)) {
598         TRACE("(%p)->(IID_nsIWebBrowserChrome, %p)\n", This, result);
599         *result = NSWBCHROME(This);
600     }else if(IsEqualGUID(&IID_nsIContextMenuListener, riid)) {
601         TRACE("(%p)->(IID_nsIContextMenuListener, %p)\n", This, result);
602         *result = NSCML(This);
603     }else if(IsEqualGUID(&IID_nsIURIContentListener, riid)) {
604         TRACE("(%p)->(IID_nsIURIContentListener %p)\n", This, result);
605         *result = NSURICL(This);
606     }else if(IsEqualGUID(&IID_nsIEmbeddingSiteWindow, riid)) {
607         TRACE("(%p)->(IID_nsIEmbeddingSiteWindow %p)\n", This, result);
608         *result = NSEMBWNDS(This);
609     }else if(IsEqualGUID(&IID_nsITooltipListener, riid)) {
610         TRACE("(%p)->(IID_nsITooltipListener %p)\n", This, result);
611         *result = NSTOOLTIP(This);
612     }else if(IsEqualGUID(&IID_nsIInterfaceRequestor, riid)) {
613         TRACE("(%p)->(IID_nsIInterfaceRequestor %p)\n", This, result);
614         *result = NSIFACEREQ(This);
615     }else if(IsEqualGUID(&IID_nsIWeakReference, riid)) {
616         TRACE("(%p)->(IID_nsIWeakReference %p)\n", This, result);
617         *result = NSWEAKREF(This);
618     }else if(IsEqualGUID(&IID_nsISupportsWeakReference, riid)) {
619         TRACE("(%p)->(IID_nsISupportsWeakReference %p)\n", This, result);
620         *result = NSSUPWEAKREF(This);
621     }
622
623     if(*result) {
624         nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
625         return NS_OK;
626     }
627
628     TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
629     return NS_NOINTERFACE;
630 }
631
632 static nsrefcnt NSAPI nsWebBrowserChrome_AddRef(nsIWebBrowserChrome *iface)
633 {
634     NSContainer *This = NSWBCHROME_THIS(iface);
635     LONG ref = InterlockedIncrement(&This->ref);
636
637     TRACE("(%p) ref=%d\n", This, ref);
638
639     return ref;
640 }
641
642 static nsrefcnt NSAPI nsWebBrowserChrome_Release(nsIWebBrowserChrome *iface)
643 {
644     NSContainer *This = NSWBCHROME_THIS(iface);
645     LONG ref = InterlockedDecrement(&This->ref);
646
647     TRACE("(%p) ref=%d\n", This, ref);
648
649     if(!ref) {
650         if(This->parent)
651             nsIWebBrowserChrome_Release(NSWBCHROME(This->parent));
652         mshtml_free(This);
653     }
654
655     return ref;
656 }
657
658 static nsresult NSAPI nsWebBrowserChrome_SetStatus(nsIWebBrowserChrome *iface,
659         PRUint32 statusType, const PRUnichar *status)
660 {
661     NSContainer *This = NSWBCHROME_THIS(iface);
662     TRACE("(%p)->(%d %s)\n", This, statusType, debugstr_w(status));
663     return NS_ERROR_NOT_IMPLEMENTED;
664 }
665
666 static nsresult NSAPI nsWebBrowserChrome_GetWebBrowser(nsIWebBrowserChrome *iface,
667         nsIWebBrowser **aWebBrowser)
668 {
669     NSContainer *This = NSWBCHROME_THIS(iface);
670
671     TRACE("(%p)->(%p)\n", This, aWebBrowser);
672
673     if(!aWebBrowser)
674         return NS_ERROR_INVALID_ARG;
675
676     if(This->webbrowser)
677         nsIWebBrowser_AddRef(This->webbrowser);
678     *aWebBrowser = This->webbrowser;
679     return S_OK;
680 }
681
682 static nsresult NSAPI nsWebBrowserChrome_SetWebBrowser(nsIWebBrowserChrome *iface,
683         nsIWebBrowser *aWebBrowser)
684 {
685     NSContainer *This = NSWBCHROME_THIS(iface);
686
687     TRACE("(%p)->(%p)\n", This, aWebBrowser);
688
689     if(aWebBrowser != This->webbrowser)
690         ERR("Wrong nsWebBrowser!\n");
691
692     return NS_OK;
693 }
694
695 static nsresult NSAPI nsWebBrowserChrome_GetChromeFlags(nsIWebBrowserChrome *iface,
696         PRUint32 *aChromeFlags)
697 {
698     NSContainer *This = NSWBCHROME_THIS(iface);
699     WARN("(%p)->(%p)\n", This, aChromeFlags);
700     return NS_ERROR_NOT_IMPLEMENTED;
701 }
702
703 static nsresult NSAPI nsWebBrowserChrome_SetChromeFlags(nsIWebBrowserChrome *iface,
704         PRUint32 aChromeFlags)
705 {
706     NSContainer *This = NSWBCHROME_THIS(iface);
707     WARN("(%p)->(%08x)\n", This, aChromeFlags);
708     return NS_ERROR_NOT_IMPLEMENTED;
709 }
710
711 static nsresult NSAPI nsWebBrowserChrome_DestroyBrowserWindow(nsIWebBrowserChrome *iface)
712 {
713     NSContainer *This = NSWBCHROME_THIS(iface);
714     TRACE("(%p)\n", This);
715     return NS_ERROR_NOT_IMPLEMENTED;
716 }
717
718 static nsresult NSAPI nsWebBrowserChrome_SizeBrowserTo(nsIWebBrowserChrome *iface,
719         PRInt32 aCX, PRInt32 aCY)
720 {
721     NSContainer *This = NSWBCHROME_THIS(iface);
722     WARN("(%p)->(%d %d)\n", This, aCX, aCY);
723     return NS_ERROR_NOT_IMPLEMENTED;
724 }
725
726 static nsresult NSAPI nsWebBrowserChrome_ShowAsModal(nsIWebBrowserChrome *iface)
727 {
728     NSContainer *This = NSWBCHROME_THIS(iface);
729     WARN("(%p)\n", This);
730     return NS_ERROR_NOT_IMPLEMENTED;
731 }
732
733 static nsresult NSAPI nsWebBrowserChrome_IsWindowModal(nsIWebBrowserChrome *iface, PRBool *_retval)
734 {
735     NSContainer *This = NSWBCHROME_THIS(iface);
736     WARN("(%p)->(%p)\n", This, _retval);
737     return NS_ERROR_NOT_IMPLEMENTED;
738 }
739
740 static nsresult NSAPI nsWebBrowserChrome_ExitModalEventLoop(nsIWebBrowserChrome *iface,
741         nsresult aStatus)
742 {
743     NSContainer *This = NSWBCHROME_THIS(iface);
744     WARN("(%p)->(%08x)\n", This, aStatus);
745     return NS_ERROR_NOT_IMPLEMENTED;
746 }
747
748 #undef NSWBCHROME_THIS
749
750 static const nsIWebBrowserChromeVtbl nsWebBrowserChromeVtbl = {
751     nsWebBrowserChrome_QueryInterface,
752     nsWebBrowserChrome_AddRef,
753     nsWebBrowserChrome_Release,
754     nsWebBrowserChrome_SetStatus,
755     nsWebBrowserChrome_GetWebBrowser,
756     nsWebBrowserChrome_SetWebBrowser,
757     nsWebBrowserChrome_GetChromeFlags,
758     nsWebBrowserChrome_SetChromeFlags,
759     nsWebBrowserChrome_DestroyBrowserWindow,
760     nsWebBrowserChrome_SizeBrowserTo,
761     nsWebBrowserChrome_ShowAsModal,
762     nsWebBrowserChrome_IsWindowModal,
763     nsWebBrowserChrome_ExitModalEventLoop
764 };
765
766 /**********************************************************
767  *      nsIContextMenuListener interface
768  */
769
770 #define NSCML_THIS(iface) DEFINE_THIS(NSContainer, ContextMenuListener, iface)
771
772 static nsresult NSAPI nsContextMenuListener_QueryInterface(nsIContextMenuListener *iface,
773         nsIIDRef riid, nsQIResult result)
774 {
775     NSContainer *This = NSCML_THIS(iface);
776     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
777 }
778
779 static nsrefcnt NSAPI nsContextMenuListener_AddRef(nsIContextMenuListener *iface)
780 {
781     NSContainer *This = NSCML_THIS(iface);
782     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
783 }
784
785 static nsrefcnt NSAPI nsContextMenuListener_Release(nsIContextMenuListener *iface)
786 {
787     NSContainer *This = NSCML_THIS(iface);
788     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
789 }
790
791 static nsresult NSAPI nsContextMenuListener_OnShowContextMenu(nsIContextMenuListener *iface,
792         PRUint32 aContextFlags, nsIDOMEvent *aEvent, nsIDOMNode *aNode)
793 {
794     NSContainer *This = NSCML_THIS(iface);
795     nsIDOMMouseEvent *event;
796     POINT pt;
797     DWORD dwID = CONTEXT_MENU_DEFAULT;
798     nsresult nsres;
799
800     TRACE("(%p)->(%08x %p %p)\n", This, aContextFlags, aEvent, aNode);
801
802     nsres = nsIDOMEvent_QueryInterface(aEvent, &IID_nsIDOMMouseEvent, (void**)&event);
803     if(NS_FAILED(nsres)) {
804         ERR("Could not get nsIDOMMouseEvent interface: %08x\n", nsres);
805         return nsres;
806     }
807
808     nsIDOMMouseEvent_GetScreenX(event, &pt.x);
809     nsIDOMMouseEvent_GetScreenY(event, &pt.y);
810     nsIDOMMouseEvent_Release(event);
811
812     switch(aContextFlags) {
813     case CONTEXT_NONE:
814     case CONTEXT_DOCUMENT:
815     case CONTEXT_TEXT:
816         dwID = CONTEXT_MENU_DEFAULT;
817         break;
818     case CONTEXT_IMAGE:
819     case CONTEXT_IMAGE|CONTEXT_LINK:
820         dwID = CONTEXT_MENU_IMAGE;
821         break;
822     case CONTEXT_LINK:
823         dwID = CONTEXT_MENU_ANCHOR;
824         break;
825     case CONTEXT_INPUT:
826         dwID = CONTEXT_MENU_CONTROL;
827         break;
828     default:
829         FIXME("aContextFlags=%08x\n", aContextFlags);
830     };
831
832     HTMLDocument_ShowContextMenu(This->doc, dwID, &pt);
833
834     return NS_OK;
835 }
836
837 #undef NSCML_THIS
838
839 static const nsIContextMenuListenerVtbl nsContextMenuListenerVtbl = {
840     nsContextMenuListener_QueryInterface,
841     nsContextMenuListener_AddRef,
842     nsContextMenuListener_Release,
843     nsContextMenuListener_OnShowContextMenu
844 };
845
846 /**********************************************************
847  *      nsIURIContentListener interface
848  */
849
850 #define NSURICL_THIS(iface) DEFINE_THIS(NSContainer, URIContentListener, iface)
851
852 static nsresult NSAPI nsURIContentListener_QueryInterface(nsIURIContentListener *iface,
853         nsIIDRef riid, nsQIResult result)
854 {
855     NSContainer *This = NSURICL_THIS(iface);
856     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
857 }
858
859 static nsrefcnt NSAPI nsURIContentListener_AddRef(nsIURIContentListener *iface)
860 {
861     NSContainer *This = NSURICL_THIS(iface);
862     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
863 }
864
865 static nsrefcnt NSAPI nsURIContentListener_Release(nsIURIContentListener *iface)
866 {
867     NSContainer *This = NSURICL_THIS(iface);
868     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
869 }
870
871 static nsresult NSAPI nsURIContentListener_OnStartURIOpen(nsIURIContentListener *iface,
872                                                           nsIURI *aURI, PRBool *_retval)
873 {
874     NSContainer *This = NSURICL_THIS(iface);
875     nsIWineURI *wine_uri;
876     nsACString spec_str;
877     const char *spec;
878     nsresult nsres;
879
880     nsACString_Init(&spec_str, NULL);
881     nsIURI_GetSpec(aURI, &spec_str);
882     nsACString_GetData(&spec_str, &spec, NULL);
883
884     TRACE("(%p)->(%p(%s) %p)\n", This, aURI, debugstr_a(spec), _retval);
885
886     nsACString_Finish(&spec_str);
887
888     nsres = nsIURI_QueryInterface(aURI, &IID_nsIWineURI, (void**)&wine_uri);
889     if(NS_FAILED(nsres)) {
890         WARN("Could not get nsIWineURI interface: %08x\n", nsres);
891         return NS_ERROR_NOT_IMPLEMENTED;
892     }
893
894     nsIWineURI_SetNSContainer(wine_uri, This);
895
896     if(This->bscallback && This->bscallback->mon) {
897         LPWSTR url = NULL;
898         HRESULT hres;
899
900         hres = IMoniker_GetDisplayName(This->bscallback->mon, NULL, 0, &url);
901         if(SUCCEEDED(hres)) {
902             IMoniker *mon = NULL;
903
904             hres = CreateURLMoniker(NULL, url, &mon);
905             if(SUCCEEDED(hres)) {
906                 nsIWineURI_SetMoniker(wine_uri, mon);
907                 IMoniker_Release(mon);
908             }else {
909                 WARN("CreateURLMoniker failed: %08x\n", hres);
910             }
911         }else {
912             WARN("GetDisplayName failed: %08x\n", hres);
913         }
914     }
915
916     nsIWineURI_Release(wine_uri);
917
918     *_retval = FALSE;
919     return This->content_listener
920         ? nsIURIContentListener_OnStartURIOpen(This->content_listener, aURI, _retval)
921         : NS_OK;
922 }
923
924 static nsresult NSAPI nsURIContentListener_DoContent(nsIURIContentListener *iface,
925         const char *aContentType, PRBool aIsContentPreferred, nsIRequest *aRequest,
926         nsIStreamListener **aContentHandler, PRBool *_retval)
927 {
928     NSContainer *This = NSURICL_THIS(iface);
929
930     TRACE("(%p)->(%s %x %p %p %p)\n", This, debugstr_a(aContentType), aIsContentPreferred,
931             aRequest, aContentHandler, _retval);
932
933     return This->content_listener
934         ? nsIURIContentListener_DoContent(This->content_listener, aContentType,
935                   aIsContentPreferred, aRequest, aContentHandler, _retval)
936         : NS_ERROR_NOT_IMPLEMENTED;
937 }
938
939 static nsresult NSAPI nsURIContentListener_IsPreferred(nsIURIContentListener *iface,
940         const char *aContentType, char **aDesiredContentType, PRBool *_retval)
941 {
942     NSContainer *This = NSURICL_THIS(iface);
943
944     TRACE("(%p)->(%s %p %p)\n", This, debugstr_a(aContentType), aDesiredContentType, _retval);
945
946     /* FIXME: Should we do something here? */
947     *_retval = TRUE; 
948
949     return This->content_listener
950         ? nsIURIContentListener_IsPreferred(This->content_listener, aContentType,
951                   aDesiredContentType, _retval)
952         : NS_OK;
953 }
954
955 static nsresult NSAPI nsURIContentListener_CanHandleContent(nsIURIContentListener *iface,
956         const char *aContentType, PRBool aIsContentPreferred, char **aDesiredContentType,
957         PRBool *_retval)
958 {
959     NSContainer *This = NSURICL_THIS(iface);
960
961     TRACE("(%p)->(%s %x %p %p)\n", This, debugstr_a(aContentType), aIsContentPreferred,
962             aDesiredContentType, _retval);
963
964     return This->content_listener
965         ? nsIURIContentListener_CanHandleContent(This->content_listener, aContentType,
966                 aIsContentPreferred, aDesiredContentType, _retval)
967         : NS_ERROR_NOT_IMPLEMENTED;
968 }
969
970 static nsresult NSAPI nsURIContentListener_GetLoadCookie(nsIURIContentListener *iface,
971         nsISupports **aLoadCookie)
972 {
973     NSContainer *This = NSURICL_THIS(iface);
974
975     WARN("(%p)->(%p)\n", This, aLoadCookie);
976
977     return This->content_listener
978         ? nsIURIContentListener_GetLoadCookie(This->content_listener, aLoadCookie)
979         : NS_ERROR_NOT_IMPLEMENTED;
980 }
981
982 static nsresult NSAPI nsURIContentListener_SetLoadCookie(nsIURIContentListener *iface,
983         nsISupports *aLoadCookie)
984 {
985     NSContainer *This = NSURICL_THIS(iface);
986
987     WARN("(%p)->(%p)\n", This, aLoadCookie);
988
989     return This->content_listener
990         ? nsIURIContentListener_SetLoadCookie(This->content_listener, aLoadCookie)
991         : NS_ERROR_NOT_IMPLEMENTED;
992 }
993
994 static nsresult NSAPI nsURIContentListener_GetParentContentListener(nsIURIContentListener *iface,
995         nsIURIContentListener **aParentContentListener)
996 {
997     NSContainer *This = NSURICL_THIS(iface);
998
999     TRACE("(%p)->(%p)\n", This, aParentContentListener);
1000
1001     if(This->content_listener)
1002         nsIURIContentListener_AddRef(This->content_listener);
1003
1004     *aParentContentListener = This->content_listener;
1005     return NS_OK;
1006 }
1007
1008 static nsresult NSAPI nsURIContentListener_SetParentContentListener(nsIURIContentListener *iface,
1009         nsIURIContentListener *aParentContentListener)
1010 {
1011     NSContainer *This = NSURICL_THIS(iface);
1012
1013     TRACE("(%p)->(%p)\n", This, aParentContentListener);
1014
1015     if(aParentContentListener == NSURICL(This))
1016         return NS_OK;
1017
1018     if(This->content_listener)
1019         nsIURIContentListener_Release(This->content_listener);
1020
1021     This->content_listener = aParentContentListener;
1022     if(This->content_listener)
1023         nsIURIContentListener_AddRef(This->content_listener);
1024
1025     return NS_OK;
1026 }
1027
1028 #undef NSURICL_THIS
1029
1030 static const nsIURIContentListenerVtbl nsURIContentListenerVtbl = {
1031     nsURIContentListener_QueryInterface,
1032     nsURIContentListener_AddRef,
1033     nsURIContentListener_Release,
1034     nsURIContentListener_OnStartURIOpen,
1035     nsURIContentListener_DoContent,
1036     nsURIContentListener_IsPreferred,
1037     nsURIContentListener_CanHandleContent,
1038     nsURIContentListener_GetLoadCookie,
1039     nsURIContentListener_SetLoadCookie,
1040     nsURIContentListener_GetParentContentListener,
1041     nsURIContentListener_SetParentContentListener
1042 };
1043
1044 /**********************************************************
1045  *      nsIEmbeddinSiteWindow interface
1046  */
1047
1048 #define NSEMBWNDS_THIS(iface) DEFINE_THIS(NSContainer, EmbeddingSiteWindow, iface)
1049
1050 static nsresult NSAPI nsEmbeddingSiteWindow_QueryInterface(nsIEmbeddingSiteWindow *iface,
1051         nsIIDRef riid, nsQIResult result)
1052 {
1053     NSContainer *This = NSEMBWNDS_THIS(iface);
1054     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1055 }
1056
1057 static nsrefcnt NSAPI nsEmbeddingSiteWindow_AddRef(nsIEmbeddingSiteWindow *iface)
1058 {
1059     NSContainer *This = NSEMBWNDS_THIS(iface);
1060     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1061 }
1062
1063 static nsrefcnt NSAPI nsEmbeddingSiteWindow_Release(nsIEmbeddingSiteWindow *iface)
1064 {
1065     NSContainer *This = NSEMBWNDS_THIS(iface);
1066     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1067 }
1068
1069 static nsresult NSAPI nsEmbeddingSiteWindow_SetDimensions(nsIEmbeddingSiteWindow *iface,
1070         PRUint32 flags, PRInt32 x, PRInt32 y, PRInt32 cx, PRInt32 cy)
1071 {
1072     NSContainer *This = NSEMBWNDS_THIS(iface);
1073     WARN("(%p)->(%08x %d %d %d %d)\n", This, flags, x, y, cx, cy);
1074     return NS_ERROR_NOT_IMPLEMENTED;
1075 }
1076
1077 static nsresult NSAPI nsEmbeddingSiteWindow_GetDimensions(nsIEmbeddingSiteWindow *iface,
1078         PRUint32 flags, PRInt32 *x, PRInt32 *y, PRInt32 *cx, PRInt32 *cy)
1079 {
1080     NSContainer *This = NSEMBWNDS_THIS(iface);
1081     WARN("(%p)->(%08x %p %p %p %p)\n", This, flags, x, y, cx, cy);
1082     return NS_ERROR_NOT_IMPLEMENTED;
1083 }
1084
1085 static nsresult NSAPI nsEmbeddingSiteWindow_SetFocus(nsIEmbeddingSiteWindow *iface)
1086 {
1087     NSContainer *This = NSEMBWNDS_THIS(iface);
1088     WARN("(%p)\n", This);
1089     return NS_ERROR_NOT_IMPLEMENTED;
1090 }
1091
1092 static nsresult NSAPI nsEmbeddingSiteWindow_GetVisibility(nsIEmbeddingSiteWindow *iface,
1093         PRBool *aVisibility)
1094 {
1095     NSContainer *This = NSEMBWNDS_THIS(iface);
1096     WARN("(%p)->(%p)\n", This, aVisibility);
1097     return NS_ERROR_NOT_IMPLEMENTED;
1098 }
1099
1100 static nsresult NSAPI nsEmbeddingSiteWindow_SetVisibility(nsIEmbeddingSiteWindow *iface,
1101         PRBool aVisibility)
1102 {
1103     NSContainer *This = NSEMBWNDS_THIS(iface);
1104     WARN("(%p)->(%x)\n", This, aVisibility);
1105     return NS_ERROR_NOT_IMPLEMENTED;
1106 }
1107
1108 static nsresult NSAPI nsEmbeddingSiteWindow_GetTitle(nsIEmbeddingSiteWindow *iface,
1109         PRUnichar **aTitle)
1110 {
1111     NSContainer *This = NSEMBWNDS_THIS(iface);
1112     WARN("(%p)->(%p)\n", This, aTitle);
1113     return NS_ERROR_NOT_IMPLEMENTED;
1114 }
1115
1116 static nsresult NSAPI nsEmbeddingSiteWindow_SetTitle(nsIEmbeddingSiteWindow *iface,
1117         const PRUnichar *aTitle)
1118 {
1119     NSContainer *This = NSEMBWNDS_THIS(iface);
1120     WARN("(%p)->(%s)\n", This, debugstr_w(aTitle));
1121     return NS_ERROR_NOT_IMPLEMENTED;
1122 }
1123
1124 static nsresult NSAPI nsEmbeddingSiteWindow_GetSiteWindow(nsIEmbeddingSiteWindow *iface,
1125         void **aSiteWindow)
1126 {
1127     NSContainer *This = NSEMBWNDS_THIS(iface);
1128
1129     TRACE("(%p)->(%p)\n", This, aSiteWindow);
1130
1131     *aSiteWindow = This->hwnd;
1132     return NS_OK;
1133 }
1134
1135 static const nsIEmbeddingSiteWindowVtbl nsEmbeddingSiteWindowVtbl = {
1136     nsEmbeddingSiteWindow_QueryInterface,
1137     nsEmbeddingSiteWindow_AddRef,
1138     nsEmbeddingSiteWindow_Release,
1139     nsEmbeddingSiteWindow_SetDimensions,
1140     nsEmbeddingSiteWindow_GetDimensions,
1141     nsEmbeddingSiteWindow_SetFocus,
1142     nsEmbeddingSiteWindow_GetVisibility,
1143     nsEmbeddingSiteWindow_SetVisibility,
1144     nsEmbeddingSiteWindow_GetTitle,
1145     nsEmbeddingSiteWindow_SetTitle,
1146     nsEmbeddingSiteWindow_GetSiteWindow
1147 };
1148
1149 #define NSTOOLTIP_THIS(iface) DEFINE_THIS(NSContainer, TooltipListener, iface)
1150
1151 static nsresult NSAPI nsTooltipListener_QueryInterface(nsITooltipListener *iface, nsIIDRef riid,
1152                                                        nsQIResult result)
1153 {
1154     NSContainer *This = NSTOOLTIP_THIS(iface);
1155     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1156 }
1157
1158 static nsrefcnt NSAPI nsTooltipListener_AddRef(nsITooltipListener *iface)
1159 {
1160     NSContainer *This = NSTOOLTIP_THIS(iface);
1161     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1162 }
1163
1164 static nsrefcnt NSAPI nsTooltipListener_Release(nsITooltipListener *iface)
1165 {
1166     NSContainer *This = NSTOOLTIP_THIS(iface);
1167     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1168 }
1169
1170 static nsresult NSAPI nsTooltipListener_OnShowTooltip(nsITooltipListener *iface,
1171         PRInt32 aXCoord, PRInt32 aYCoord, const PRUnichar *aTipText)
1172 {
1173     NSContainer *This = NSTOOLTIP_THIS(iface);
1174
1175     show_tooltip(This->doc, aXCoord, aYCoord, aTipText);
1176
1177     return NS_OK;
1178 }
1179
1180 static nsresult NSAPI nsTooltipListener_OnHideTooltip(nsITooltipListener *iface)
1181 {
1182     NSContainer *This = NSTOOLTIP_THIS(iface);
1183
1184     hide_tooltip(This->doc);
1185
1186     return NS_OK;
1187 }
1188
1189 #undef NSTOOLTIM_THIS
1190
1191 static const nsITooltipListenerVtbl nsTooltipListenerVtbl = {
1192     nsTooltipListener_QueryInterface,
1193     nsTooltipListener_AddRef,
1194     nsTooltipListener_Release,
1195     nsTooltipListener_OnShowTooltip,
1196     nsTooltipListener_OnHideTooltip
1197 };
1198
1199 #define NSIFACEREQ_THIS(iface) DEFINE_THIS(NSContainer, InterfaceRequestor, iface)
1200
1201 static nsresult NSAPI nsInterfaceRequestor_QueryInterface(nsIInterfaceRequestor *iface,
1202                                                           nsIIDRef riid, nsQIResult result)
1203 {
1204     NSContainer *This = NSIFACEREQ_THIS(iface);
1205     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1206 }
1207
1208 static nsrefcnt NSAPI nsInterfaceRequestor_AddRef(nsIInterfaceRequestor *iface)
1209 {
1210     NSContainer *This = NSIFACEREQ_THIS(iface);
1211     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1212 }
1213
1214 static nsrefcnt NSAPI nsInterfaceRequestor_Release(nsIInterfaceRequestor *iface)
1215 {
1216     NSContainer *This = NSIFACEREQ_THIS(iface);
1217     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1218 }
1219
1220 static nsresult NSAPI nsInterfaceRequestor_GetInterface(nsIInterfaceRequestor *iface,
1221                                                         nsIIDRef riid, nsQIResult result)
1222 {
1223     NSContainer *This = NSIFACEREQ_THIS(iface);
1224
1225     if(IsEqualGUID(&IID_nsIDOMWindow, riid)) {
1226         TRACE("(%p)->(IID_nsIDOMWindow %p)\n", This, result);
1227         return nsIWebBrowser_GetContentDOMWindow(This->webbrowser, (nsIDOMWindow**)result);
1228     }
1229
1230     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1231 }
1232
1233 #undef NSIFACEREQ_THIS
1234
1235 static const nsIInterfaceRequestorVtbl nsInterfaceRequestorVtbl = {
1236     nsInterfaceRequestor_QueryInterface,
1237     nsInterfaceRequestor_AddRef,
1238     nsInterfaceRequestor_Release,
1239     nsInterfaceRequestor_GetInterface
1240 };
1241
1242 #define NSWEAKREF_THIS(iface) DEFINE_THIS(NSContainer, WeakReference, iface)
1243
1244 static nsresult NSAPI nsWeakReference_QueryInterface(nsIWeakReference *iface,
1245         nsIIDRef riid, nsQIResult result)
1246 {
1247     NSContainer *This = NSWEAKREF_THIS(iface);
1248     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1249 }
1250
1251 static nsrefcnt NSAPI nsWeakReference_AddRef(nsIWeakReference *iface)
1252 {
1253     NSContainer *This = NSWEAKREF_THIS(iface);
1254     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1255 }
1256
1257 static nsrefcnt NSAPI nsWeakReference_Release(nsIWeakReference *iface)
1258 {
1259     NSContainer *This = NSWEAKREF_THIS(iface);
1260     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1261 }
1262
1263 static nsresult NSAPI nsWeakReference_QueryReferent(nsIWeakReference *iface,
1264         const nsIID *riid, void **result)
1265 {
1266     NSContainer *This = NSWEAKREF_THIS(iface);
1267     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1268 }
1269
1270 #undef NSWEAKREF_THIS
1271
1272 static const nsIWeakReferenceVtbl nsWeakReferenceVtbl = {
1273     nsWeakReference_QueryInterface,
1274     nsWeakReference_AddRef,
1275     nsWeakReference_Release,
1276     nsWeakReference_QueryReferent
1277 };
1278
1279 #define NSSUPWEAKREF_THIS(iface) DEFINE_THIS(NSContainer, SupportsWeakReference, iface)
1280
1281 static nsresult NSAPI nsSupportsWeakReference_QueryInterface(nsISupportsWeakReference *iface,
1282         nsIIDRef riid, nsQIResult result)
1283 {
1284     NSContainer *This = NSSUPWEAKREF_THIS(iface);
1285     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1286 }
1287
1288 static nsrefcnt NSAPI nsSupportsWeakReference_AddRef(nsISupportsWeakReference *iface)
1289 {
1290     NSContainer *This = NSSUPWEAKREF_THIS(iface);
1291     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1292 }
1293
1294 static nsrefcnt NSAPI nsSupportsWeakReference_Release(nsISupportsWeakReference *iface)
1295 {
1296     NSContainer *This = NSSUPWEAKREF_THIS(iface);
1297     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1298 }
1299
1300 static nsresult NSAPI nsSupportsWeakReference_GetWeakReference(nsISupportsWeakReference *iface,
1301         nsIWeakReference **_retval)
1302 {
1303     NSContainer *This = NSSUPWEAKREF_THIS(iface);
1304
1305     TRACE("(%p)->(%p)\n", This, _retval);
1306
1307     nsIWeakReference_AddRef(NSWEAKREF(This));
1308     *_retval = NSWEAKREF(This);
1309     return NS_OK;
1310 }
1311
1312 #undef NSWEAKREF_THIS
1313
1314 const nsISupportsWeakReferenceVtbl nsSupportsWeakReferenceVtbl = {
1315     nsSupportsWeakReference_QueryInterface,
1316     nsSupportsWeakReference_AddRef,
1317     nsSupportsWeakReference_Release,
1318     nsSupportsWeakReference_GetWeakReference
1319 };
1320
1321
1322 NSContainer *NSContainer_Create(HTMLDocument *doc, NSContainer *parent)
1323 {
1324     nsIWebBrowserSetup *wbsetup;
1325     NSContainer *ret;
1326     nsresult nsres;
1327
1328     if(!load_gecko())
1329         return NULL;
1330
1331     ret = mshtml_alloc(sizeof(NSContainer));
1332
1333     ret->lpWebBrowserChromeVtbl      = &nsWebBrowserChromeVtbl;
1334     ret->lpContextMenuListenerVtbl   = &nsContextMenuListenerVtbl;
1335     ret->lpURIContentListenerVtbl    = &nsURIContentListenerVtbl;
1336     ret->lpEmbeddingSiteWindowVtbl   = &nsEmbeddingSiteWindowVtbl;
1337     ret->lpTooltipListenerVtbl       = &nsTooltipListenerVtbl;
1338     ret->lpInterfaceRequestorVtbl    = &nsInterfaceRequestorVtbl;
1339     ret->lpWeakReferenceVtbl         = &nsWeakReferenceVtbl;
1340     ret->lpSupportsWeakReferenceVtbl = &nsSupportsWeakReferenceVtbl;
1341
1342
1343     ret->doc = doc;
1344     ret->ref = 1;
1345     ret->bscallback = NULL;
1346     ret->content_listener = NULL;
1347
1348     if(parent)
1349         nsIWebBrowserChrome_AddRef(NSWBCHROME(parent));
1350     ret->parent = parent;
1351
1352     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_WEBBROWSER_CONTRACTID,
1353             NULL, &IID_nsIWebBrowser, (void**)&ret->webbrowser);
1354     if(NS_FAILED(nsres))
1355         ERR("Creating WebBrowser failed: %08x\n", nsres);
1356
1357     nsres = nsIWebBrowser_SetContainerWindow(ret->webbrowser, NSWBCHROME(ret));
1358     if(NS_FAILED(nsres))
1359         ERR("SetContainerWindow failed: %08x\n", nsres);
1360
1361     nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIBaseWindow,
1362             (void**)&ret->window);
1363     if(NS_FAILED(nsres))
1364         ERR("Could not get nsIBaseWindow interface: %08x\n", nsres);
1365
1366     nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIWebBrowserSetup,
1367                                          (void**)&wbsetup);
1368     if(NS_SUCCEEDED(nsres)) {
1369         nsres = nsIWebBrowserSetup_SetProperty(wbsetup, SETUP_IS_CHROME_WRAPPER, TRUE);
1370         nsIWebBrowserSetup_Release(wbsetup);
1371         if(NS_FAILED(nsres))
1372             ERR("SetProperty(SETUP_IS_CHROME_WRAPPER) failed: %08x\n", nsres);
1373     }else {
1374         ERR("Could not get nsIWebBrowserSetup interface\n");
1375     }
1376
1377     nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIWebNavigation,
1378             (void**)&ret->navigation);
1379     if(NS_FAILED(nsres))
1380         ERR("Could not get nsIWebNavigation interface: %08x\n", nsres);
1381
1382     nsres = nsIWebBrowserFocus_QueryInterface(ret->webbrowser, &IID_nsIWebBrowserFocus,
1383             (void**)&ret->focus);
1384     if(NS_FAILED(nsres))
1385         ERR("Could not get nsIWebBrowserFocus interface: %08x\n", nsres);
1386
1387     if(!nscontainer_class)
1388         register_nscontainer_class();
1389
1390     ret->hwnd = CreateWindowExW(0, wszNsContainer, NULL,
1391             WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 0, 0, 100, 100,
1392             GetDesktopWindow(), NULL, hInst, ret);
1393
1394     nsres = nsIBaseWindow_InitWindow(ret->window, ret->hwnd, NULL, 0, 0, 100, 100);
1395     if(NS_SUCCEEDED(nsres)) {
1396         nsres = nsIBaseWindow_Create(ret->window);
1397         if(NS_FAILED(nsres))
1398             WARN("Creating window failed: %08x\n", nsres);
1399
1400         nsIBaseWindow_SetVisibility(ret->window, FALSE);
1401         nsIBaseWindow_SetEnabled(ret->window, FALSE);
1402     }else {
1403         ERR("InitWindow failed: %08x\n", nsres);
1404     }
1405
1406     nsres = nsIWebBrowser_SetParentURIContentListener(ret->webbrowser, NSURICL(ret));
1407     if(NS_FAILED(nsres))
1408         ERR("SetParentURIContentListener failed: %08x\n", nsres);
1409
1410     return ret;
1411 }
1412
1413 void NSContainer_Release(NSContainer *This)
1414 {
1415     TRACE("(%p)\n", This);
1416
1417     ShowWindow(This->hwnd, SW_HIDE);
1418     SetParent(This->hwnd, NULL);
1419
1420     nsIBaseWindow_SetVisibility(This->window, FALSE);
1421     nsIBaseWindow_Destroy(This->window);
1422
1423     nsIWebBrowser_SetContainerWindow(This->webbrowser, NULL);
1424
1425     nsIWebBrowser_Release(This->webbrowser);
1426     This->webbrowser = NULL;
1427
1428     nsIWebNavigation_Release(This->navigation);
1429     This->navigation = NULL;
1430
1431     nsIBaseWindow_Release(This->window);
1432     This->window = NULL;
1433
1434     nsIWebBrowserFocus_Release(This->focus);
1435     This->focus = NULL;
1436
1437     if(This->content_listener) {
1438         nsIURIContentListener_Release(This->content_listener);
1439         This->content_listener = NULL;
1440     }
1441
1442     if(This->hwnd) {
1443         DestroyWindow(This->hwnd);
1444         This->hwnd = NULL;
1445     }
1446
1447     nsIWebBrowserChrome_Release(NSWBCHROME(This));
1448 }