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