winsock: Implement getnameinfo.
[wine] / dlls / mshtml / nsembed.c
1 /*
2  * Copyright 2005 Jacek Caban
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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_STRINGSTREAM_CONTRACTID "@mozilla.org/io/string-input-stream;1"
42
43 #define APPSTARTUP_TOPIC "app-startup"
44
45 #define PR_UINT32_MAX 0xffffffff
46
47 struct nsStringContainer {
48     void *v;
49     void *d1;
50     PRUint32 d2;
51     void *d3;
52 };
53
54 struct nsCStringContainer {
55     void *v;
56     void *d1;
57     PRUint32 d2;
58     void *d3;
59 };
60
61 static nsresult (*NS_InitXPCOM2)(nsIServiceManager**,void*,void*);
62 static nsresult (*NS_ShutdownXPCOM)(nsIServiceManager*);
63 static nsresult (*NS_GetComponentRegistrar)(nsIComponentRegistrar**);
64 static nsresult (*NS_StringContainerInit)(nsStringContainer*);
65 static nsresult (*NS_CStringContainerInit)(nsCStringContainer*);
66 static nsresult (*NS_StringContainerFinish)(nsStringContainer*);
67 static nsresult (*NS_CStringContainerFinish)(nsCStringContainer*);
68 static nsresult (*NS_StringSetData)(nsAString*,const PRUnichar*,PRUint32);
69 static nsresult (*NS_CStringSetData)(nsACString*,const char*,PRUint32);
70 static nsresult (*NS_NewLocalFile)(const nsAString*,PRBool,nsIFile**);
71 static PRUint32 (*NS_CStringGetData)(const nsACString*,const char**,PRBool*);
72
73 static HINSTANCE hXPCOM = NULL;
74
75 static nsIServiceManager *pServMgr = NULL;
76 static nsIComponentManager *pCompMgr = NULL;
77
78 static const WCHAR wszNsContainer[] = {'N','s','C','o','n','t','a','i','n','e','r',0};
79
80 static ATOM nscontainer_class;
81
82 static LRESULT WINAPI nsembed_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
83 {
84     HTMLDocument *This;
85     nsresult nsres;
86
87     static const WCHAR wszTHIS[] = {'T','H','I','S',0};
88
89     if(msg == WM_CREATE) {
90         This = *(HTMLDocument**)lParam;
91         SetPropW(hwnd, wszTHIS, This);
92     }else {
93         This = (HTMLDocument*)GetPropW(hwnd, wszTHIS);
94     }
95
96     switch(msg) {
97         case WM_SIZE:
98             TRACE("(%p)->(WM_SIZE)\n", This);
99
100             nsres = nsIBaseWindow_SetSize(This->nscontainer->window,
101                     LOWORD(lParam), HIWORD(lParam), TRUE);
102             if(NS_FAILED(nsres))
103                 WARN("SetSize failed: %08lx\n", nsres);
104     }
105
106     return DefWindowProcW(hwnd, msg, wParam, lParam);
107 }
108
109
110 static void register_nscontainer_class(void)
111 {
112     static WNDCLASSEXW wndclass = {
113         sizeof(WNDCLASSEXW),
114         CS_DBLCLKS,
115         nsembed_proc,
116         0, 0, NULL, NULL, NULL, NULL, NULL,
117         wszNsContainer,
118         NULL,
119     };
120     wndclass.hInstance = hInst;
121     nscontainer_class = RegisterClassExW(&wndclass);
122 }
123
124 static BOOL get_mozilla_path(PRUnichar *gre_path)
125 {
126     DWORD res, type, i, size = MAX_PATH;
127     HKEY mozilla_key, hkey;
128     WCHAR key_name[100];
129     BOOL ret = FALSE;
130
131     static const WCHAR wszGreKey[] =
132         {'S','o','f','t','w','a','r','e','\\',
133             'm','o','z','i','l','l','a','.','o','r','g','\\',
134                 'G','R','E',0};
135
136     static const WCHAR wszGreHome[] = {'G','r','e','H','o','m','e',0};
137
138     res = RegOpenKeyW(HKEY_LOCAL_MACHINE, wszGreKey, &mozilla_key);
139     if(res != ERROR_SUCCESS) {
140         TRACE("Could not open key %s\n", debugstr_w(wszGreKey));
141         return FALSE;
142     }
143
144     for(i=0; !ret && RegEnumKeyW(mozilla_key, i, key_name, sizeof(key_name)/sizeof(WCHAR)) == ERROR_SUCCESS; i++) {
145         RegOpenKeyW(mozilla_key, key_name, &hkey);
146         res = RegQueryValueExW(hkey, wszGreHome, NULL, &type, (LPBYTE)gre_path, &size);
147         if(res == ERROR_SUCCESS)
148             ret = TRUE;
149         RegCloseKey(hkey);
150     }
151
152     RegCloseKey(mozilla_key);
153     return ret;
154 }
155
156 static BOOL get_mozctl_path(PRUnichar *gre_path)
157 {
158     HKEY hkey;
159     DWORD res, type, size = MAX_PATH;
160
161     static const WCHAR wszMozCtlKey[] =
162         {'S','o','f','t','w','a','r','e','\\','M','o','z','i','l','l','a',0};
163     static const WCHAR wszBinDirectoryPath[] =
164         {'B','i','n','D','i','r','e','c','t','o','r','y','P','a','t','h',0};
165     static const WCHAR wszMozCtlClsidKey[] =
166         {'C','L','S','I','D','\\',
167          '{','1','3','3','9','B','5','4','C','-','3','4','5','3','-','1','1','D','2',
168          '-','9','3','B','9','-','0','0','0','0','0','0','0','0','0','0','0','0','}','\\',
169          'I','n','p','r','o','c','S','e','r','v','e','r','3','2',0};
170
171     res = RegOpenKeyW(HKEY_LOCAL_MACHINE, wszMozCtlKey, &hkey);
172     if(res == ERROR_SUCCESS) {
173         res = RegQueryValueExW(hkey, wszBinDirectoryPath, NULL, &type, (LPBYTE)gre_path, &size);
174         if(res == ERROR_SUCCESS)
175             return TRUE;
176         else
177             ERR("Could not get value %s\n", debugstr_w(wszBinDirectoryPath));
178     }
179
180     res = RegOpenKeyW(HKEY_CLASSES_ROOT, wszMozCtlClsidKey, &hkey);
181     if(res == ERROR_SUCCESS) {
182         res = RegQueryValueExW(hkey, NULL, NULL, &type, (LPBYTE)gre_path, &size);
183         if(res == ERROR_SUCCESS) {
184             WCHAR *ptr;
185             if((ptr = strrchrW(gre_path, '\\')))
186                 ptr[1] = 0;
187             return TRUE;
188         }else {
189             ERR("Could not get value of %s\n", debugstr_w(wszMozCtlClsidKey));
190         }
191     }
192
193     TRACE("Could not find Mozilla ActiveX Control\n");
194
195     return FALSE;
196 }
197
198 static BOOL get_wine_gecko_path(PRUnichar *gre_path)
199 {
200     HKEY hkey;
201     DWORD res, type, size = MAX_PATH;
202
203     static const WCHAR wszMshtmlKey[] = {
204         'S','o','f','t','w','a','r','e','\\','W','i','n','e',
205         '\\','M','S','H','T','M','L',0};
206     static const WCHAR wszGeckoPath[] =
207         {'G','e','c','k','o','P','a','t','h',0};
208
209     /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
210     res = RegOpenKeyW(HKEY_CURRENT_USER, wszMshtmlKey, &hkey);
211     if(res != ERROR_SUCCESS)
212         return FALSE;
213
214     res = RegQueryValueExW(hkey, wszGeckoPath, NULL, &type, (LPBYTE)gre_path, &size);
215     if(res != ERROR_SUCCESS || type != REG_SZ)
216         return FALSE;
217
218     return TRUE;
219 }
220
221 static void set_profile(void)
222 {
223     nsIProfile *profile;
224     PRBool exists = FALSE;
225     nsresult nsres;
226
227     static const WCHAR wszMSHTML[] = {'M','S','H','T','M','L',0};
228
229     nsres = nsIServiceManager_GetServiceByContactID(pServMgr, NS_PROFILE_CONTRACTID,
230                                          &IID_nsIProfile, (void**)&profile);
231     if(NS_FAILED(nsres)) {
232         ERR("Could not get profile service: %08lx\n", nsres);
233         return;
234     }
235
236     nsres = nsIProfile_ProfileExists(profile, wszMSHTML, &exists);
237     if(!exists) {
238         nsres = nsIProfile_CreateNewProfile(profile, wszMSHTML, NULL, NULL, FALSE);
239         if(NS_FAILED(nsres))
240             ERR("CreateNewProfile failed: %08lx\n", nsres);
241     }
242
243     nsres = nsIProfile_SetCurrentProfile(profile, wszMSHTML);
244     if(NS_FAILED(nsres))
245         ERR("SetCurrentProfile failed: %08lx\n", nsres);
246
247     nsIProfile_Release(profile);
248 }
249
250 static BOOL load_gecko(void)
251 {
252     nsresult nsres;
253     nsIObserver *pStartNotif;
254     nsIComponentRegistrar *registrar = NULL;
255     nsAString path;
256     nsIFile *gre_dir;
257     PRUnichar gre_path[MAX_PATH];
258     WCHAR path_env[MAX_PATH];
259     int len;
260
261     static BOOL tried_load = FALSE;
262     static const WCHAR wszPATH[] = {'P','A','T','H',0};
263     static const WCHAR strXPCOM[] = {'x','p','c','o','m','.','d','l','l',0};
264
265     TRACE("()\n");
266
267     if(tried_load)
268         return pCompMgr != NULL;
269     tried_load = TRUE;
270
271     if(!get_wine_gecko_path(gre_path) && !get_mozctl_path(gre_path)
272        && !get_mozilla_path(gre_path)) {
273         MESSAGE("Could not load Mozilla. HTML rendering will be disabled.\n");
274         return FALSE;
275     }
276
277     TRACE("found path %s\n", debugstr_w(gre_path));
278
279     /* We have to modify PATH as XPCOM loads other DLLs from this directory. */
280     GetEnvironmentVariableW(wszPATH, path_env, sizeof(path_env)/sizeof(WCHAR));
281     len = strlenW(path_env);
282     path_env[len++] = ';';
283     strcpyW(path_env+len, gre_path);
284     SetEnvironmentVariableW(wszPATH, path_env);
285
286     hXPCOM = LoadLibraryW(strXPCOM);
287     if(!hXPCOM) {
288         ERR("Could not load XPCOM: %ld\n", GetLastError());
289         return FALSE;
290     }
291
292 #define NS_DLSYM(func) \
293     func = (typeof(func))GetProcAddress(hXPCOM, #func); \
294     if(!func) \
295         ERR("Could not GetProcAddress(" #func ") failed\n")
296
297     NS_DLSYM(NS_InitXPCOM2);
298     NS_DLSYM(NS_ShutdownXPCOM);
299     NS_DLSYM(NS_GetComponentRegistrar);
300     NS_DLSYM(NS_StringContainerInit);
301     NS_DLSYM(NS_CStringContainerInit);
302     NS_DLSYM(NS_StringContainerFinish);
303     NS_DLSYM(NS_CStringContainerFinish);
304     NS_DLSYM(NS_StringSetData);
305     NS_DLSYM(NS_CStringSetData);
306     NS_DLSYM(NS_NewLocalFile);
307     NS_DLSYM(NS_CStringGetData);
308
309 #undef NS_DLSYM
310
311     NS_StringContainerInit(&path);
312     NS_StringSetData(&path, gre_path, PR_UINT32_MAX);
313     nsres = NS_NewLocalFile(&path, FALSE, &gre_dir);
314     NS_StringContainerFinish(&path);
315     if(NS_FAILED(nsres)) {
316         ERR("NS_NewLocalFile failed: %08lx\n", nsres);
317         FreeLibrary(hXPCOM);
318         return FALSE;
319     }
320
321     nsres = NS_InitXPCOM2(&pServMgr, gre_dir, NULL);
322     if(NS_FAILED(nsres)) {
323         ERR("NS_InitXPCOM2 failed: %08lx\n", nsres);
324         FreeLibrary(hXPCOM);
325         return FALSE;
326     }
327
328     nsres = nsIServiceManager_QueryInterface(pServMgr, &IID_nsIComponentManager, (void**)&pCompMgr);
329     if(NS_FAILED(nsres))
330         ERR("Could not get nsIComponentManager: %08lx\n", nsres);
331
332     nsres = NS_GetComponentRegistrar(&registrar);
333     if(NS_SUCCEEDED(nsres)) {
334         nsres = nsIComponentRegistrar_AutoRegister(registrar, NULL);
335         if(NS_FAILED(nsres))
336             ERR("AutoRegister(NULL) failed: %08lx\n", nsres);
337
338         nsres = nsIComponentRegistrar_AutoRegister(registrar, gre_dir);
339         if(NS_FAILED(nsres))
340             ERR("AutoRegister(gre_dir) failed: %08lx\n", nsres);
341     }else {
342         ERR("NS_GetComponentRegistrar failed: %08lx\n", nsres);
343     }
344
345     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_APPSTARTUPNOTIFIER_CONTRACTID,
346             NULL, &IID_nsIObserver, (void**)&pStartNotif);
347     if(NS_SUCCEEDED(nsres)) {
348         nsres = nsIObserver_Observe(pStartNotif, NULL, APPSTARTUP_TOPIC, NULL);
349         if(NS_FAILED(nsres))
350             ERR("Observe failed: %08lx\n", nsres);
351
352         nsIObserver_Release(pStartNotif);
353     }else {
354         ERR("could not get appstartup-notifier: %08lx\n", nsres);
355     }
356
357     set_profile();
358
359     if(registrar) {
360         register_nsservice(registrar);
361         init_nsio(pCompMgr, registrar);
362         nsIComponentRegistrar_Release(registrar);
363     }
364
365     return TRUE;
366 }
367
368 nsACString *nsACString_Create(void)
369 {
370     nsACString *ret;
371     ret = HeapAlloc(GetProcessHeap(), 0, sizeof(nsACString));
372     NS_CStringContainerInit(ret);
373     return ret;
374 }
375
376 void nsACString_SetData(nsACString *str, const char *data)
377 {
378     NS_CStringSetData(str, data, PR_UINT32_MAX);
379 }
380
381 PRUint32 nsACString_GetData(const nsACString *str, const char **data, PRBool *termited)
382 {
383     return NS_CStringGetData(str, data, termited);
384 }
385
386 void nsACString_Destroy(nsACString *str)
387 {
388     NS_CStringContainerFinish(str);
389     HeapFree(GetProcessHeap(), 0, str);
390 }
391
392 nsIInputStream *create_nsstream(const char *data, PRInt32 data_len)
393 {
394     nsIStringInputStream *ret;
395     nsresult nsres;
396
397     if(!pCompMgr)
398         return NULL;
399
400     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
401             NS_STRINGSTREAM_CONTRACTID, NULL, &IID_nsIStringInputStream,
402             (void**)&ret);
403     if(NS_FAILED(nsres)) {
404         ERR("Could not get nsIStringInputStream\n");
405         return NULL;
406     }
407
408     nsres = nsIStringInputStream_SetData(ret, data, data_len);
409     if(NS_FAILED(nsres)) {
410         ERR("AdoptData failed: %08lx\n", nsres);
411         nsIStringInputStream_Release(ret);
412         return NULL;
413     }
414
415     return (nsIInputStream*)ret;
416 }
417
418 void close_gecko()
419 {
420     TRACE("()\n");
421
422     if(pCompMgr)
423         nsIComponentManager_Release(pCompMgr);
424
425     if(pServMgr)
426         nsIServiceManager_Release(pServMgr);
427
428     if(hXPCOM)
429         FreeLibrary(hXPCOM);
430 }
431
432 /**********************************************************
433  *      nsIWebBrowserChrome interface
434  */
435
436 #define NSWBCHROME_THIS(iface) DEFINE_THIS(NSContainer, WebBrowserChrome, iface)
437
438 static nsresult NSAPI nsWebBrowserChrome_QueryInterface(nsIWebBrowserChrome *iface,
439         nsIIDRef riid, nsQIResult result)
440 {
441     NSContainer *This = NSWBCHROME_THIS(iface);
442
443     *result = NULL;
444     if(IsEqualGUID(&IID_nsISupports, riid)) {
445         TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
446         *result = NSWBCHROME(This);
447     }else if(IsEqualGUID(&IID_nsIWebBrowserChrome, riid)) {
448         TRACE("(%p)->(IID_nsIWebBrowserChrome, %p)\n", This, result);
449         *result = NSWBCHROME(This);
450     }else if(IsEqualGUID(&IID_nsIContextMenuListener, riid)) {
451         TRACE("(%p)->(IID_nsIContextMenuListener, %p)\n", This, result);
452         *result = NSCML(This);
453     }else if(IsEqualGUID(&IID_nsIURIContentListener, riid)) {
454         TRACE("(%p)->(IID_nsIURIContentListener %p)\n", This, result);
455         *result = NSURICL(This);
456     }else if(IsEqualGUID(&IID_nsIEmbeddingSiteWindow, riid)) {
457         TRACE("(%p)->(IID_nsIEmbeddingSiteWindow %p)\n", This, result);
458         *result = NSEMBWNDS(This);
459     }
460
461     if(*result) {
462         nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
463         return NS_OK;
464     }
465
466     TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
467     return NS_NOINTERFACE;
468 }
469
470 static nsrefcnt NSAPI nsWebBrowserChrome_AddRef(nsIWebBrowserChrome *iface)
471 {
472     NSContainer *This = NSWBCHROME_THIS(iface);
473     LONG ref = InterlockedIncrement(&This->ref);
474
475     TRACE("(%p) ref=%ld\n", This, ref);
476
477     return ref;
478 }
479
480 static nsrefcnt NSAPI nsWebBrowserChrome_Release(nsIWebBrowserChrome *iface)
481 {
482     NSContainer *This = NSWBCHROME_THIS(iface);
483     LONG ref = InterlockedDecrement(&This->ref);
484
485     TRACE("(%p) ref=%ld\n", This, ref);
486
487     if(!ref)
488         HeapFree(GetProcessHeap(), 0, This);
489
490     return ref;
491 }
492
493 static nsresult NSAPI nsWebBrowserChrome_SetStatus(nsIWebBrowserChrome *iface,
494         PRUint32 statusType, const PRUnichar *status)
495 {
496     NSContainer *This = NSWBCHROME_THIS(iface);
497     TRACE("(%p)->(%ld %s)\n", This, statusType, debugstr_w(status));
498     return NS_ERROR_NOT_IMPLEMENTED;
499 }
500
501 static nsresult NSAPI nsWebBrowserChrome_GetWebBrowser(nsIWebBrowserChrome *iface,
502         nsIWebBrowser **aWebBrowser)
503 {
504     NSContainer *This = NSWBCHROME_THIS(iface);
505
506     TRACE("(%p)->(%p)\n", This, aWebBrowser);
507
508     if(!aWebBrowser)
509         return NS_ERROR_INVALID_ARG;
510
511     if(This->webbrowser)
512         nsIWebBrowser_AddRef(This->webbrowser);
513     *aWebBrowser = This->webbrowser;
514     return S_OK;
515 }
516
517 static nsresult NSAPI nsWebBrowserChrome_SetWebBrowser(nsIWebBrowserChrome *iface,
518         nsIWebBrowser *aWebBrowser)
519 {
520     NSContainer *This = NSWBCHROME_THIS(iface);
521
522     TRACE("(%p)->(%p)\n", This, aWebBrowser);
523
524     if(aWebBrowser != This->webbrowser)
525         ERR("Wrong nsWebBrowser!\n");
526
527     return NS_OK;
528 }
529
530 static nsresult NSAPI nsWebBrowserChrome_GetChromeFlags(nsIWebBrowserChrome *iface,
531         PRUint32 *aChromeFlags)
532 {
533     NSContainer *This = NSWBCHROME_THIS(iface);
534     WARN("(%p)->(%p)\n", This, aChromeFlags);
535     return NS_ERROR_NOT_IMPLEMENTED;
536 }
537
538 static nsresult NSAPI nsWebBrowserChrome_SetChromeFlags(nsIWebBrowserChrome *iface,
539         PRUint32 aChromeFlags)
540 {
541     NSContainer *This = NSWBCHROME_THIS(iface);
542     WARN("(%p)->(%08lx)\n", This, aChromeFlags);
543     return NS_ERROR_NOT_IMPLEMENTED;
544 }
545
546 static nsresult NSAPI nsWebBrowserChrome_DestroyBrowserWindow(nsIWebBrowserChrome *iface)
547 {
548     NSContainer *This = NSWBCHROME_THIS(iface);
549     TRACE("(%p)\n", This);
550     return NS_ERROR_NOT_IMPLEMENTED;
551 }
552
553 static nsresult NSAPI nsWebBrowserChrome_SizeBrowserTo(nsIWebBrowserChrome *iface,
554         PRInt32 aCX, PRInt32 aCY)
555 {
556     NSContainer *This = NSWBCHROME_THIS(iface);
557     WARN("(%p)->(%ld %ld)\n", This, aCX, aCY);
558     return NS_ERROR_NOT_IMPLEMENTED;
559 }
560
561 static nsresult NSAPI nsWebBrowserChrome_ShowAsModal(nsIWebBrowserChrome *iface)
562 {
563     NSContainer *This = NSWBCHROME_THIS(iface);
564     WARN("(%p)\n", This);
565     return NS_ERROR_NOT_IMPLEMENTED;
566 }
567
568 static nsresult NSAPI nsWebBrowserChrome_IsWindowModal(nsIWebBrowserChrome *iface, PRBool *_retval)
569 {
570     NSContainer *This = NSWBCHROME_THIS(iface);
571     WARN("(%p)->(%p)\n", This, _retval);
572     return NS_ERROR_NOT_IMPLEMENTED;
573 }
574
575 static nsresult NSAPI nsWebBrowserChrome_ExitModalEventLoop(nsIWebBrowserChrome *iface,
576         nsresult aStatus)
577 {
578     NSContainer *This = NSWBCHROME_THIS(iface);
579     WARN("(%p)->(%08lx)\n", This, aStatus);
580     return NS_ERROR_NOT_IMPLEMENTED;
581 }
582
583 #undef NSWBCHROME_THIS
584
585 static const nsIWebBrowserChromeVtbl nsWebBrowserChromeVtbl = {
586     nsWebBrowserChrome_QueryInterface,
587     nsWebBrowserChrome_AddRef,
588     nsWebBrowserChrome_Release,
589     nsWebBrowserChrome_SetStatus,
590     nsWebBrowserChrome_GetWebBrowser,
591     nsWebBrowserChrome_SetWebBrowser,
592     nsWebBrowserChrome_GetChromeFlags,
593     nsWebBrowserChrome_SetChromeFlags,
594     nsWebBrowserChrome_DestroyBrowserWindow,
595     nsWebBrowserChrome_SizeBrowserTo,
596     nsWebBrowserChrome_ShowAsModal,
597     nsWebBrowserChrome_IsWindowModal,
598     nsWebBrowserChrome_ExitModalEventLoop
599 };
600
601 /**********************************************************
602  *      nsIContextMenuListener interface
603  */
604
605 #define NSCML_THIS(iface) DEFINE_THIS(NSContainer, ContextMenuListener, iface)
606
607 static nsresult NSAPI nsContextMenuListener_QueryInterface(nsIContextMenuListener *iface,
608         nsIIDRef riid, nsQIResult result)
609 {
610     NSContainer *This = NSCML_THIS(iface);
611     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
612 }
613
614 static nsrefcnt NSAPI nsContextMenuListener_AddRef(nsIContextMenuListener *iface)
615 {
616     NSContainer *This = NSCML_THIS(iface);
617     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
618 }
619
620 static nsrefcnt NSAPI nsContextMenuListener_Release(nsIContextMenuListener *iface)
621 {
622     NSContainer *This = NSCML_THIS(iface);
623     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
624 }
625
626 static nsresult NSAPI nsContextMenuListener_OnShowContextMenu(nsIContextMenuListener *iface,
627         PRUint32 aContextFlags, nsIDOMEvent *aEvent, nsIDOMNode *aNode)
628 {
629     NSContainer *This = NSCML_THIS(iface);
630     nsIDOMMouseEvent *event;
631     POINT pt;
632     DWORD dwID = CONTEXT_MENU_DEFAULT;
633     nsresult nsres;
634
635     TRACE("(%p)->(%08lx %p %p)\n", This, aContextFlags, aEvent, aNode);
636
637     nsres = nsIDOMEvent_QueryInterface(aEvent, &IID_nsIDOMMouseEvent, (void**)&event);
638     if(NS_FAILED(nsres)) {
639         ERR("Could not get nsIDOMMouseEvent interface: %08lx\n", nsres);
640         return nsres;
641     }
642
643     nsIDOMMouseEvent_GetScreenX(event, &pt.x);
644     nsIDOMMouseEvent_GetScreenY(event, &pt.y);
645     nsIDOMMouseEvent_Release(event);
646
647     switch(aContextFlags) {
648     case CONTEXT_NONE:
649     case CONTEXT_DOCUMENT:
650     case CONTEXT_TEXT:
651         dwID = CONTEXT_MENU_DEFAULT;
652         break;
653     case CONTEXT_IMAGE:
654     case CONTEXT_IMAGE|CONTEXT_LINK:
655         dwID = CONTEXT_MENU_IMAGE;
656         break;
657     case CONTEXT_LINK:
658         dwID = CONTEXT_MENU_ANCHOR;
659         break;
660     case CONTEXT_INPUT:
661         dwID = CONTEXT_MENU_CONTROL;
662         break;
663     default:
664         FIXME("aContextFlags=%08lx\n", aContextFlags);
665     };
666
667     HTMLDocument_ShowContextMenu(This->doc, dwID, &pt);
668
669     return NS_OK;
670 }
671
672 #undef NSCML_THIS
673
674 static const nsIContextMenuListenerVtbl nsContextMenuListenerVtbl = {
675     nsContextMenuListener_QueryInterface,
676     nsContextMenuListener_AddRef,
677     nsContextMenuListener_Release,
678     nsContextMenuListener_OnShowContextMenu
679 };
680
681 /**********************************************************
682  *      nsIURIContentListener interface
683  */
684
685 #define NSURICL_THIS(iface) DEFINE_THIS(NSContainer, URIContentListener, iface)
686
687 static nsresult NSAPI nsURIContentListener_QueryInterface(nsIURIContentListener *iface,
688         nsIIDRef riid, nsQIResult result)
689 {
690     NSContainer *This = NSURICL_THIS(iface);
691     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
692 }
693
694 static nsrefcnt NSAPI nsURIContentListener_AddRef(nsIURIContentListener *iface)
695 {
696     NSContainer *This = NSURICL_THIS(iface);
697     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
698 }
699
700 static nsrefcnt NSAPI nsURIContentListener_Release(nsIURIContentListener *iface)
701 {
702     NSContainer *This = NSURICL_THIS(iface);
703     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
704 }
705
706 static nsresult NSAPI nsURIContentListener_OnStartURIOpen(nsIURIContentListener *iface,
707                                                           nsIURI *aURI, PRBool *_retval)
708 {
709     NSContainer *This = NSURICL_THIS(iface);
710     nsIWineURI *wine_uri;
711     nsACString *spec_str = nsACString_Create();
712     const char *spec;
713     nsresult nsres;
714
715     nsIURI_GetSpec(aURI, spec_str);
716     nsACString_GetData(spec_str, &spec, NULL);
717
718     TRACE("(%p)->(%p(%s) %p)\n", This, aURI, debugstr_a(spec), _retval);
719
720     nsACString_Destroy(spec_str);
721
722     nsres = nsIURI_QueryInterface(aURI, &IID_nsIWineURI, (void**)&wine_uri);
723     if(NS_SUCCEEDED(nsres)) {
724         nsIWineURI_SetNSContainer(wine_uri, This);
725         nsIWineURI_Release(wine_uri);
726     }else {
727         WARN("Could not get nsIWineURI interface: %08lx\n", nsres);
728     }
729
730     return NS_ERROR_NOT_IMPLEMENTED;
731 }
732
733 static nsresult NSAPI nsURIContentListener_DoContent(nsIURIContentListener *iface,
734         const char *aContentType, PRBool aIsContentPreferred, nsIRequest *aRequest,
735         nsIStreamListener **aContentHandler, PRBool *_retval)
736 {
737     NSContainer *This = NSURICL_THIS(iface);
738     TRACE("(%p)->(%s %x %p %p %p)\n", This, debugstr_a(aContentType), aIsContentPreferred,
739             aRequest, aContentHandler, _retval);
740     return NS_ERROR_NOT_IMPLEMENTED;
741 }
742
743 static nsresult NSAPI nsURIContentListener_IsPreferred(nsIURIContentListener *iface,
744         const char *aContentType, char **aDesiredContentType, PRBool *_retval)
745 {
746     NSContainer *This = NSURICL_THIS(iface);
747
748     TRACE("(%p)->(%s %p %p)\n", This, debugstr_a(aContentType), aDesiredContentType, _retval);
749
750     /* FIXME: Should we do something here? */
751     *_retval = TRUE; 
752     return NS_OK;
753 }
754
755 static nsresult NSAPI nsURIContentListener_CanHandleContent(nsIURIContentListener *iface,
756         const char *aContentType, PRBool aIsContentPreferred, char **aDesiredContentType,
757         PRBool *_retval)
758 {
759     NSContainer *This = NSURICL_THIS(iface);
760     TRACE("(%p)->(%s %x %p %p)\n", This, debugstr_a(aContentType), aIsContentPreferred,
761             aDesiredContentType, _retval);
762     return NS_ERROR_NOT_IMPLEMENTED;
763 }
764
765 static nsresult NSAPI nsURIContentListener_GetLoadCookie(nsIURIContentListener *iface,
766         nsISupports **aLoadCookie)
767 {
768     NSContainer *This = NSURICL_THIS(iface);
769     WARN("(%p)->(%p)\n", This, aLoadCookie);
770     return NS_ERROR_NOT_IMPLEMENTED;
771 }
772
773 static nsresult NSAPI nsURIContentListener_SetLoadCookie(nsIURIContentListener *iface,
774         nsISupports *aLoadCookie)
775 {
776     NSContainer *This = NSURICL_THIS(iface);
777     WARN("(%p)->(%p)\n", This, aLoadCookie);
778     return NS_ERROR_NOT_IMPLEMENTED;
779 }
780
781 static nsresult NSAPI nsURIContentListener_GetParentContentListener(nsIURIContentListener *iface,
782         nsIURIContentListener **aParentContentListener)
783 {
784     NSContainer *This = NSURICL_THIS(iface);
785     WARN("(%p)->(%p)\n", This, aParentContentListener);
786     return NS_ERROR_NOT_IMPLEMENTED;
787 }
788
789 static nsresult NSAPI nsURIContentListener_SetParentContentListener(nsIURIContentListener *iface,
790         nsIURIContentListener *aParentContentListener)
791 {
792     NSContainer *This = NSURICL_THIS(iface);
793     WARN("(%p)->(%p)\n", This, aParentContentListener);
794     return NS_ERROR_NOT_IMPLEMENTED;
795 }
796
797 #undef NSURICL_THIS
798
799 static const nsIURIContentListenerVtbl nsURIContentListenerVtbl = {
800     nsURIContentListener_QueryInterface,
801     nsURIContentListener_AddRef,
802     nsURIContentListener_Release,
803     nsURIContentListener_OnStartURIOpen,
804     nsURIContentListener_DoContent,
805     nsURIContentListener_IsPreferred,
806     nsURIContentListener_CanHandleContent,
807     nsURIContentListener_GetLoadCookie,
808     nsURIContentListener_SetLoadCookie,
809     nsURIContentListener_GetParentContentListener,
810     nsURIContentListener_SetParentContentListener
811 };
812
813 /**********************************************************
814  *      nsIEmbeddinSiteWindow interface
815  */
816
817 #define NSEMBWNDS_THIS(iface) DEFINE_THIS(NSContainer, EmbeddingSiteWindow, iface)
818
819 static nsresult NSAPI nsEmbeddingSiteWindow_QueryInterface(nsIEmbeddingSiteWindow *iface,
820         nsIIDRef riid, nsQIResult result)
821 {
822     NSContainer *This = NSEMBWNDS_THIS(iface);
823     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
824 }
825
826 static nsrefcnt NSAPI nsEmbeddingSiteWindow_AddRef(nsIEmbeddingSiteWindow *iface)
827 {
828     NSContainer *This = NSEMBWNDS_THIS(iface);
829     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
830 }
831
832 static nsrefcnt NSAPI nsEmbeddingSiteWindow_Release(nsIEmbeddingSiteWindow *iface)
833 {
834     NSContainer *This = NSEMBWNDS_THIS(iface);
835     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
836 }
837
838 static nsresult NSAPI nsEmbeddingSiteWindow_SetDimensions(nsIEmbeddingSiteWindow *iface,
839         PRUint32 flags, PRInt32 x, PRInt32 y, PRInt32 cx, PRInt32 cy)
840 {
841     NSContainer *This = NSEMBWNDS_THIS(iface);
842     WARN("(%p)->(%08lx %ld %ld %ld %ld)\n", This, flags, x, y, cx, cy);
843     return NS_ERROR_NOT_IMPLEMENTED;
844 }
845
846 static nsresult NSAPI nsEmbeddingSiteWindow_GetDimensions(nsIEmbeddingSiteWindow *iface,
847         PRUint32 flags, PRInt32 *x, PRInt32 *y, PRInt32 *cx, PRInt32 *cy)
848 {
849     NSContainer *This = NSEMBWNDS_THIS(iface);
850     WARN("(%p)->(%08lx %p %p %p %p)\n", This, flags, x, y, cx, cy);
851     return NS_ERROR_NOT_IMPLEMENTED;
852 }
853
854 static nsresult NSAPI nsEmbeddingSiteWindow_SetFocus(nsIEmbeddingSiteWindow *iface)
855 {
856     NSContainer *This = NSEMBWNDS_THIS(iface);
857     WARN("(%p)\n", This);
858     return NS_ERROR_NOT_IMPLEMENTED;
859 }
860
861 static nsresult NSAPI nsEmbeddingSiteWindow_GetVisibility(nsIEmbeddingSiteWindow *iface,
862         PRBool *aVisibility)
863 {
864     NSContainer *This = NSEMBWNDS_THIS(iface);
865     WARN("(%p)->(%p)\n", This, aVisibility);
866     return NS_ERROR_NOT_IMPLEMENTED;
867 }
868
869 static nsresult NSAPI nsEmbeddingSiteWindow_SetVisibility(nsIEmbeddingSiteWindow *iface,
870         PRBool aVisibility)
871 {
872     NSContainer *This = NSEMBWNDS_THIS(iface);
873     WARN("(%p)->(%x)\n", This, aVisibility);
874     return NS_ERROR_NOT_IMPLEMENTED;
875 }
876
877 static nsresult NSAPI nsEmbeddingSiteWindow_GetTitle(nsIEmbeddingSiteWindow *iface,
878         PRUnichar **aTitle)
879 {
880     NSContainer *This = NSEMBWNDS_THIS(iface);
881     WARN("(%p)->(%p)\n", This, aTitle);
882     return NS_ERROR_NOT_IMPLEMENTED;
883 }
884
885 static nsresult NSAPI nsEmbeddingSiteWindow_SetTitle(nsIEmbeddingSiteWindow *iface,
886         const PRUnichar *aTitle)
887 {
888     NSContainer *This = NSEMBWNDS_THIS(iface);
889     WARN("(%p)->(%s)\n", This, debugstr_w(aTitle));
890     return NS_ERROR_NOT_IMPLEMENTED;
891 }
892
893 static nsresult NSAPI nsEmbeddingSiteWindow_GetSiteWindow(nsIEmbeddingSiteWindow *iface,
894         void **aSiteWindow)
895 {
896     NSContainer *This = NSEMBWNDS_THIS(iface);
897
898     TRACE("(%p)->(%p)\n", This, aSiteWindow);
899
900     *aSiteWindow = This->hwnd;
901     return NS_OK;
902 }
903
904 static const nsIEmbeddingSiteWindowVtbl nsEmbeddingSiteWindowVtbl = {
905     nsEmbeddingSiteWindow_QueryInterface,
906     nsEmbeddingSiteWindow_AddRef,
907     nsEmbeddingSiteWindow_Release,
908     nsEmbeddingSiteWindow_SetDimensions,
909     nsEmbeddingSiteWindow_GetDimensions,
910     nsEmbeddingSiteWindow_SetFocus,
911     nsEmbeddingSiteWindow_GetVisibility,
912     nsEmbeddingSiteWindow_SetVisibility,
913     nsEmbeddingSiteWindow_GetTitle,
914     nsEmbeddingSiteWindow_SetTitle,
915     nsEmbeddingSiteWindow_GetSiteWindow
916 };
917
918 void NSContainer_Create(HTMLDocument *doc)
919 {
920     nsIWebBrowserSetup *wbsetup;
921     NSContainer *ret;
922     nsresult nsres;
923
924     if(!load_gecko())
925         return;
926
927     ret = HeapAlloc(GetProcessHeap(), 0, sizeof(NSContainer));
928
929     ret->lpWebBrowserChromeVtbl    = &nsWebBrowserChromeVtbl;
930     ret->lpContextMenuListenerVtbl = &nsContextMenuListenerVtbl;
931     ret->lpURIContentListenerVtbl  = &nsURIContentListenerVtbl;
932     ret->lpEmbeddingSiteWindowVtbl = &nsEmbeddingSiteWindowVtbl;
933
934     ret->doc = doc;
935     ret->ref = 1;
936     ret->load_call = FALSE;
937
938     doc->nscontainer = ret;
939
940     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_WEBBROWSER_CONTRACTID,
941             NULL, &IID_nsIWebBrowser, (void**)&ret->webbrowser);
942     if(NS_FAILED(nsres))
943         ERR("Creating WebBrowser failed: %08lx\n", nsres);
944
945     nsres = nsIWebBrowser_SetContainerWindow(ret->webbrowser, NSWBCHROME(ret));
946     if(NS_FAILED(nsres))
947         ERR("SetContainerWindow failed: %08lx\n", nsres);
948
949     nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIBaseWindow,
950             (void**)&ret->window);
951     if(NS_FAILED(nsres))
952         ERR("Could not get nsIBaseWindow interface: %08lx\n", nsres);
953
954     nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIWebBrowserSetup,
955                                          (void**)&wbsetup);
956     if(NS_SUCCEEDED(nsres)) {
957         nsres = nsIWebBrowserSetup_SetProperty(wbsetup, SETUP_IS_CHROME_WRAPPER, TRUE);
958         nsIWebBrowserSetup_Release(wbsetup);
959         if(NS_FAILED(nsres))
960             ERR("SetProperty(SETUP_IS_CHROME_WRAPPER) failed: %08lx\n", nsres);
961     }else {
962         ERR("Could not get nsIWebBrowserSetup interface\n");
963     }
964
965     nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIWebNavigation,
966             (void**)&ret->navigation);
967     if(NS_FAILED(nsres))
968         ERR("Could not get nsIWebNavigation interface: %08lx\n", nsres);
969
970     nsres = nsIWebBrowserFocus_QueryInterface(ret->webbrowser, &IID_nsIWebBrowserFocus,
971             (void**)&ret->focus);
972     if(NS_FAILED(nsres))
973         ERR("Could not get nsIWebBrowserFocus interface: %08lx\n", nsres);
974
975 #if 0
976     nsres = nsIWebBrowserStream_QueryInterface(ret->webbrowser, &IID_nsIWebBrowserStream,
977             (void**)&ret->stream);
978     if(NS_FAILED(nsres))
979         ERR("Could not get nsIWebBrowserStream interface: %08lx\n", nsres);
980 #else
981     ret->stream = NULL;
982 #endif
983
984     if(!nscontainer_class)
985         register_nscontainer_class();
986
987     ret->hwnd = CreateWindowExW(0, wszNsContainer, NULL,
988             WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 0, 0, 100, 100,
989             GetDesktopWindow(), NULL, hInst, doc);
990
991     nsres = nsIBaseWindow_InitWindow(ret->window, ret->hwnd, NULL, 0, 0, 100, 100);
992     if(NS_SUCCEEDED(nsres)) {
993         nsres = nsIBaseWindow_Create(ret->window);
994         if(NS_FAILED(nsres))
995             WARN("Creating window failed: %08lx\n", nsres);
996
997         nsIBaseWindow_SetVisibility(ret->window, FALSE);
998         nsIBaseWindow_SetEnabled(ret->window, FALSE);
999     }else {
1000         ERR("InitWindow failed: %08lx\n", nsres);
1001     }
1002
1003     nsres = nsIWebBrowser_SetParentURIContentListener(ret->webbrowser, NSURICL(ret));
1004     if(NS_FAILED(nsres))
1005         ERR("SetParentURIContentListener failed: %08lx\n", nsres);
1006 }
1007
1008 void NSContainer_Release(NSContainer *This)
1009 {
1010     nsIBaseWindow *base_window;
1011     nsresult nsres;
1012
1013     TRACE("(%p)\n", This);
1014
1015     ShowWindow(This->hwnd, SW_HIDE);
1016     SetParent(This->hwnd, NULL);
1017
1018     nsres = nsIWebBrowser_QueryInterface(This->webbrowser, &IID_nsIBaseWindow,
1019                                          (void**)&base_window);
1020     if(NS_SUCCEEDED(nsres)) {
1021         nsIBaseWindow_SetVisibility(base_window, FALSE);
1022         nsIBaseWindow_Destroy(base_window);
1023         nsIBaseWindow_Release(base_window);
1024     }else {
1025         ERR("Could not get nsIBaseWindow interface: %08lx\n", nsres);
1026     }
1027
1028     nsIWebBrowser_SetContainerWindow(This->webbrowser, NULL);
1029
1030     nsIWebBrowser_Release(This->webbrowser);
1031     This->webbrowser = NULL;
1032
1033     nsIWebNavigation_Release(This->navigation);
1034     This->navigation = NULL;
1035
1036     nsIBaseWindow_Release(This->window);
1037     This->window = NULL;
1038
1039     if(This->stream) {
1040         nsIWebBrowserStream_Release(This->stream);
1041         This->stream = NULL;
1042     }
1043
1044     if(This->hwnd) {
1045         DestroyWindow(This->hwnd);
1046         This->hwnd = NULL;
1047     }
1048
1049     nsIWebBrowserChrome_Release(NSWBCHROME(This));
1050 }