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