mshtml: Store current IHTMLEventObj in DOMEvent and create it in fire_event_obj if...
[wine.git] / dlls / mshtml / nsembed.c
blobbb4f122a27b6617feff55410dc3c83778e77ca4d
1 /*
2 * Copyright 2005-2007 Jacek Caban for CodeWeavers
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.
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.
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
19 #include "config.h"
21 #include <stdarg.h>
22 #include <assert.h>
24 #define COBJMACROS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "winreg.h"
30 #include "ole2.h"
31 #include "shlobj.h"
32 #include "shlwapi.h"
34 #include "wine/debug.h"
36 #include "mshtml_private.h"
37 #include "htmlevent.h"
38 #include "binding.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
41 WINE_DECLARE_DEBUG_CHANNEL(gecko);
43 #define NS_APPSTARTUPNOTIFIER_CONTRACTID "@mozilla.org/embedcomp/appstartup-notifier;1"
44 #define NS_WEBBROWSER_CONTRACTID "@mozilla.org/embedding/browser/nsWebBrowser;1"
45 #define NS_COMMANDPARAMS_CONTRACTID "@mozilla.org/embedcomp/command-params;1"
46 #define NS_HTMLSERIALIZER_CONTRACTID "@mozilla.org/layout/contentserializer;1?mimetype=text/html"
47 #define NS_EDITORCONTROLLER_CONTRACTID "@mozilla.org/editor/editorcontroller;1"
48 #define NS_PREFERENCES_CONTRACTID "@mozilla.org/preferences;1"
49 #define NS_VARIANT_CONTRACTID "@mozilla.org/variant;1"
50 #define NS_CATEGORYMANAGER_CONTRACTID "@mozilla.org/categorymanager;1"
51 #define NS_XMLHTTPREQUEST_CONTRACTID "@mozilla.org/xmlextras/xmlhttprequest;1"
53 #define PR_UINT32_MAX 0xffffffff
55 #define NS_STRING_CONTAINER_INIT_DEPEND 0x0002
56 #define NS_CSTRING_CONTAINER_INIT_DEPEND 0x0002
58 typedef UINT32 PRUint32;
60 static nsresult (CDECL *NS_InitXPCOM2)(nsIServiceManager**,void*,void*);
61 static nsresult (CDECL *NS_ShutdownXPCOM)(nsIServiceManager*);
62 static nsresult (CDECL *NS_GetComponentRegistrar)(nsIComponentRegistrar**);
63 static nsresult (CDECL *NS_StringContainerInit2)(nsStringContainer*,const PRUnichar*,PRUint32,PRUint32);
64 static nsresult (CDECL *NS_CStringContainerInit2)(nsCStringContainer*,const char*,PRUint32,PRUint32);
65 static nsresult (CDECL *NS_StringContainerFinish)(nsStringContainer*);
66 static nsresult (CDECL *NS_CStringContainerFinish)(nsCStringContainer*);
67 static nsresult (CDECL *NS_StringSetData)(nsAString*,const PRUnichar*,PRUint32);
68 static nsresult (CDECL *NS_CStringSetData)(nsACString*,const char*,PRUint32);
69 static nsresult (CDECL *NS_NewLocalFile)(const nsAString*,cpp_bool,nsIFile**);
70 static PRUint32 (CDECL *NS_StringGetData)(const nsAString*,const PRUnichar **,cpp_bool*);
71 static PRUint32 (CDECL *NS_CStringGetData)(const nsACString*,const char**,cpp_bool*);
72 static cpp_bool (CDECL *NS_StringGetIsVoid)(const nsAString*);
73 static void* (CDECL *NS_Alloc)(SIZE_T);
74 static void (CDECL *NS_Free)(void*);
76 static HINSTANCE xul_handle = NULL;
78 static nsIServiceManager *pServMgr = NULL;
79 static nsIComponentManager *pCompMgr = NULL;
80 static nsICategoryManager *cat_mgr;
81 static nsIFile *profile_directory, *plugin_directory;
83 static const WCHAR wszNsContainer[] = {'N','s','C','o','n','t','a','i','n','e','r',0};
85 static ATOM nscontainer_class;
86 static WCHAR gecko_path[MAX_PATH];
87 static unsigned gecko_path_len;
89 nsresult create_nsfile(const PRUnichar *path, nsIFile **ret)
91 nsAString str;
92 nsresult nsres;
94 nsAString_InitDepend(&str, path);
95 nsres = NS_NewLocalFile(&str, FALSE, ret);
96 nsAString_Finish(&str);
98 if(NS_FAILED(nsres))
99 WARN("NS_NewLocalFile failed: %08x\n", nsres);
100 return nsres;
103 typedef struct {
104 nsISimpleEnumerator nsISimpleEnumerator_iface;
105 LONG ref;
106 nsISupports *value;
107 } nsSingletonEnumerator;
109 static inline nsSingletonEnumerator *impl_from_nsISimpleEnumerator(nsISimpleEnumerator *iface)
111 return CONTAINING_RECORD(iface, nsSingletonEnumerator, nsISimpleEnumerator_iface);
114 static nsresult NSAPI nsSingletonEnumerator_QueryInterface(nsISimpleEnumerator *iface, nsIIDRef riid, void **ppv)
116 nsSingletonEnumerator *This = impl_from_nsISimpleEnumerator(iface);
118 if(IsEqualGUID(&IID_nsISupports, riid)) {
119 TRACE("(%p)->(IID_nsISupports %p)\n", This, ppv);
120 *ppv = &This->nsISimpleEnumerator_iface;
121 }else if(IsEqualGUID(&IID_nsISimpleEnumerator, riid)) {
122 TRACE("(%p)->(IID_nsISimpleEnumerator %p)\n", This, ppv);
123 *ppv = &This->nsISimpleEnumerator_iface;
124 }else {
125 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
126 *ppv = NULL;
127 return NS_NOINTERFACE;
130 nsISupports_AddRef((nsISupports*)*ppv);
131 return NS_OK;
134 static nsrefcnt NSAPI nsSingletonEnumerator_AddRef(nsISimpleEnumerator *iface)
136 nsSingletonEnumerator *This = impl_from_nsISimpleEnumerator(iface);
137 nsrefcnt ref = InterlockedIncrement(&This->ref);
139 TRACE("(%p) ref=%d\n", This, ref);
141 return ref;
144 static nsrefcnt NSAPI nsSingletonEnumerator_Release(nsISimpleEnumerator *iface)
146 nsSingletonEnumerator *This = impl_from_nsISimpleEnumerator(iface);
147 nsrefcnt ref = InterlockedDecrement(&This->ref);
149 TRACE("(%p) ref=%d\n", This, ref);
151 if(!ref) {
152 if(This->value)
153 nsISupports_Release(This->value);
154 heap_free(This);
157 return ref;
160 static nsresult NSAPI nsSingletonEnumerator_HasMoreElements(nsISimpleEnumerator *iface, cpp_bool *_retval)
162 nsSingletonEnumerator *This = impl_from_nsISimpleEnumerator(iface);
164 TRACE("(%p)->()\n", This);
166 *_retval = This->value != NULL;
167 return NS_OK;
170 static nsresult NSAPI nsSingletonEnumerator_GetNext(nsISimpleEnumerator *iface, nsISupports **_retval)
172 nsSingletonEnumerator *This = impl_from_nsISimpleEnumerator(iface);
174 TRACE("(%p)->()\n", This);
176 if(!This->value)
177 return NS_ERROR_UNEXPECTED;
179 *_retval = This->value;
180 This->value = NULL;
181 return NS_OK;
184 static const nsISimpleEnumeratorVtbl nsSingletonEnumeratorVtbl = {
185 nsSingletonEnumerator_QueryInterface,
186 nsSingletonEnumerator_AddRef,
187 nsSingletonEnumerator_Release,
188 nsSingletonEnumerator_HasMoreElements,
189 nsSingletonEnumerator_GetNext
192 static nsISimpleEnumerator *create_singleton_enumerator(nsISupports *value)
194 nsSingletonEnumerator *ret;
196 ret = heap_alloc(sizeof(*ret));
197 if(!ret)
198 return NULL;
200 ret->nsISimpleEnumerator_iface.lpVtbl = &nsSingletonEnumeratorVtbl;
201 ret->ref = 1;
203 if(value)
204 nsISupports_AddRef(value);
205 ret->value = value;
206 return &ret->nsISimpleEnumerator_iface;
209 static nsresult NSAPI nsDirectoryServiceProvider2_QueryInterface(nsIDirectoryServiceProvider2 *iface,
210 nsIIDRef riid, void **result)
212 if(IsEqualGUID(&IID_nsISupports, riid)) {
213 TRACE("(IID_nsISupports %p)\n", result);
214 *result = iface;
215 }else if(IsEqualGUID(&IID_nsIDirectoryServiceProvider, riid)) {
216 TRACE("(IID_nsIDirectoryServiceProvider %p)\n", result);
217 *result = iface;
218 }else if(IsEqualGUID(&IID_nsIDirectoryServiceProvider2, riid)) {
219 TRACE("(IID_nsIDirectoryServiceProvider2 %p)\n", result);
220 *result = iface;
221 }else {
222 WARN("(%s %p)\n", debugstr_guid(riid), result);
223 *result = NULL;
224 return NS_NOINTERFACE;
227 nsISupports_AddRef((nsISupports*)*result);
228 return NS_OK;
231 static nsrefcnt NSAPI nsDirectoryServiceProvider2_AddRef(nsIDirectoryServiceProvider2 *iface)
233 return 2;
236 static nsrefcnt NSAPI nsDirectoryServiceProvider2_Release(nsIDirectoryServiceProvider2 *iface)
238 return 1;
241 static nsresult create_profile_directory(void)
243 static const WCHAR wine_geckoW[] = {'\\','w','i','n','e','_','g','e','c','k','o',0};
245 WCHAR path[MAX_PATH + sizeof(wine_geckoW)/sizeof(WCHAR)];
246 cpp_bool exists;
247 nsresult nsres;
248 HRESULT hres;
250 hres = SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, path);
251 if(FAILED(hres)) {
252 ERR("SHGetFolderPath failed: %08x\n", hres);
253 return NS_ERROR_FAILURE;
256 strcatW(path, wine_geckoW);
257 nsres = create_nsfile(path, &profile_directory);
258 if(NS_FAILED(nsres))
259 return nsres;
261 nsres = nsIFile_Exists(profile_directory, &exists);
262 if(NS_FAILED(nsres)) {
263 ERR("Exists failed: %08x\n", nsres);
264 return nsres;
267 if(!exists) {
268 nsres = nsIFile_Create(profile_directory, 1, 0700);
269 if(NS_FAILED(nsres))
270 ERR("Create failed: %08x\n", nsres);
273 return nsres;
276 static nsresult NSAPI nsDirectoryServiceProvider2_GetFile(nsIDirectoryServiceProvider2 *iface,
277 const char *prop, cpp_bool *persistent, nsIFile **_retval)
279 TRACE("(%s %p %p)\n", debugstr_a(prop), persistent, _retval);
281 if(!strcmp(prop, "ProfD")) {
282 if(!profile_directory) {
283 nsresult nsres;
285 nsres = create_profile_directory();
286 if(NS_FAILED(nsres))
287 return nsres;
290 assert(profile_directory != NULL);
291 return nsIFile_Clone(profile_directory, _retval);
294 *_retval = NULL;
295 return NS_ERROR_FAILURE;
298 static nsresult NSAPI nsDirectoryServiceProvider2_GetFiles(nsIDirectoryServiceProvider2 *iface,
299 const char *prop, nsISimpleEnumerator **_retval)
301 TRACE("(%s %p)\n", debugstr_a(prop), _retval);
303 if(!strcmp(prop, "APluginsDL")) {
304 WCHAR plugin_path[MAX_PATH];
305 nsIFile *file;
306 int len;
307 nsresult nsres;
309 if(!plugin_directory) {
310 static const WCHAR gecko_pluginW[] = {'\\','g','e','c','k','o','\\','p','l','u','g','i','n',0};
312 len = GetSystemDirectoryW(plugin_path, (sizeof(plugin_path)-sizeof(gecko_pluginW))/sizeof(WCHAR)+1);
313 if(!len)
314 return NS_ERROR_UNEXPECTED;
316 strcpyW(plugin_path+len, gecko_pluginW);
317 nsres = create_nsfile(plugin_path, &plugin_directory);
318 if(NS_FAILED(nsres)) {
319 *_retval = NULL;
320 return nsres;
324 nsres = nsIFile_Clone(plugin_directory, &file);
325 if(NS_FAILED(nsres))
326 return nsres;
328 *_retval = create_singleton_enumerator((nsISupports*)file);
329 nsIFile_Release(file);
330 if(!*_retval)
331 return NS_ERROR_OUT_OF_MEMORY;
333 return NS_OK;
336 *_retval = NULL;
337 return NS_ERROR_FAILURE;
340 static const nsIDirectoryServiceProvider2Vtbl nsDirectoryServiceProvider2Vtbl = {
341 nsDirectoryServiceProvider2_QueryInterface,
342 nsDirectoryServiceProvider2_AddRef,
343 nsDirectoryServiceProvider2_Release,
344 nsDirectoryServiceProvider2_GetFile,
345 nsDirectoryServiceProvider2_GetFiles
348 static nsIDirectoryServiceProvider2 nsDirectoryServiceProvider2 =
349 { &nsDirectoryServiceProvider2Vtbl };
351 static LRESULT WINAPI nsembed_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
353 NSContainer *This;
354 nsresult nsres;
356 static const WCHAR wszTHIS[] = {'T','H','I','S',0};
358 if(msg == WM_CREATE) {
359 This = *(NSContainer**)lParam;
360 SetPropW(hwnd, wszTHIS, This);
361 }else {
362 This = GetPropW(hwnd, wszTHIS);
365 switch(msg) {
366 case WM_SIZE:
367 TRACE("(%p)->(WM_SIZE)\n", This);
369 nsres = nsIBaseWindow_SetSize(This->window,
370 LOWORD(lParam), HIWORD(lParam), TRUE);
371 if(NS_FAILED(nsres))
372 WARN("SetSize failed: %08x\n", nsres);
373 break;
375 case WM_PARENTNOTIFY:
376 TRACE("WM_PARENTNOTIFY %x\n", (unsigned)wParam);
378 switch(wParam) {
379 case WM_LBUTTONDOWN:
380 case WM_RBUTTONDOWN:
381 nsIWebBrowserFocus_Activate(This->focus);
385 return DefWindowProcW(hwnd, msg, wParam, lParam);
389 static void register_nscontainer_class(void)
391 static WNDCLASSEXW wndclass = {
392 sizeof(WNDCLASSEXW),
393 CS_DBLCLKS,
394 nsembed_proc,
395 0, 0, NULL, NULL, NULL, NULL, NULL,
396 wszNsContainer,
397 NULL,
399 wndclass.hInstance = hInst;
400 nscontainer_class = RegisterClassExW(&wndclass);
403 static BOOL install_wine_gecko(void)
405 PROCESS_INFORMATION pi;
406 STARTUPINFOW si;
407 WCHAR app[MAX_PATH];
408 WCHAR *args;
409 LONG len;
410 BOOL ret;
412 static const WCHAR controlW[] = {'\\','c','o','n','t','r','o','l','.','e','x','e',0};
413 static const WCHAR argsW[] =
414 {' ','a','p','p','w','i','z','.','c','p','l',' ','i','n','s','t','a','l','l','_','g','e','c','k','o',0};
416 len = GetSystemDirectoryW(app, MAX_PATH-sizeof(controlW)/sizeof(WCHAR));
417 memcpy(app+len, controlW, sizeof(controlW));
419 args = heap_alloc(len*sizeof(WCHAR) + sizeof(controlW) + sizeof(argsW));
420 if(!args)
421 return FALSE;
423 memcpy(args, app, len*sizeof(WCHAR) + sizeof(controlW));
424 memcpy(args + len + sizeof(controlW)/sizeof(WCHAR)-1, argsW, sizeof(argsW));
426 TRACE("starting %s\n", debugstr_w(args));
428 memset(&si, 0, sizeof(si));
429 si.cb = sizeof(si);
430 ret = CreateProcessW(app, args, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
431 heap_free(args);
432 if (ret) {
433 CloseHandle(pi.hThread);
434 WaitForSingleObject(pi.hProcess, INFINITE);
435 CloseHandle(pi.hProcess);
438 return ret;
441 static void set_environment(LPCWSTR gre_path)
443 size_t len, gre_path_len;
444 int debug_level = 0;
445 WCHAR *path, buf[20];
446 const WCHAR *ptr;
448 static const WCHAR pathW[] = {'P','A','T','H',0};
449 static const WCHAR warnW[] = {'w','a','r','n',0};
450 static const WCHAR xpcom_debug_breakW[] =
451 {'X','P','C','O','M','_','D','E','B','U','G','_','B','R','E','A','K',0};
452 static const WCHAR nspr_log_modulesW[] =
453 {'N','S','P','R','_','L','O','G','_','M','O','D','U','L','E','S',0};
454 static const WCHAR debug_formatW[] = {'a','l','l',':','%','d',0};
456 SetEnvironmentVariableW(xpcom_debug_breakW, warnW);
458 if(TRACE_ON(gecko))
459 debug_level = 5;
460 else if(WARN_ON(gecko))
461 debug_level = 2;
462 else if(ERR_ON(gecko))
463 debug_level = 1;
465 sprintfW(buf, debug_formatW, debug_level);
466 SetEnvironmentVariableW(nspr_log_modulesW, buf);
468 len = GetEnvironmentVariableW(pathW, NULL, 0);
469 gre_path_len = strlenW(gre_path);
470 path = heap_alloc((len+gre_path_len+1)*sizeof(WCHAR));
471 if(!path)
472 return;
473 GetEnvironmentVariableW(pathW, path, len);
475 /* We have to modify PATH as xul.dll loads other DLLs from this directory. */
476 if(!(ptr = strstrW(path, gre_path))
477 || (ptr > path && *(ptr-1) != ';')
478 || (ptr[gre_path_len] && ptr[gre_path_len] != ';')) {
479 if(len)
480 path[len-1] = ';';
481 strcpyW(path+len, gre_path);
482 SetEnvironmentVariableW(pathW, path);
484 heap_free(path);
487 static BOOL load_xul(const PRUnichar *gre_path)
489 static const WCHAR xul_dllW[] = {'\\','x','u','l','.','d','l','l',0};
490 WCHAR file_name[MAX_PATH];
492 strcpyW(file_name, gre_path);
493 strcatW(file_name, xul_dllW);
495 TRACE("(%s)\n", debugstr_w(file_name));
497 set_environment(gre_path);
499 xul_handle = LoadLibraryExW(file_name, 0, LOAD_WITH_ALTERED_SEARCH_PATH);
500 if(!xul_handle) {
501 WARN("Could not load XUL: %d\n", GetLastError());
502 return FALSE;
505 #define NS_DLSYM(func) \
506 func = (void *)GetProcAddress(xul_handle, #func); \
507 if(!func) \
508 ERR("Could not GetProcAddress(" #func ") failed\n")
510 NS_DLSYM(NS_InitXPCOM2);
511 NS_DLSYM(NS_ShutdownXPCOM);
512 NS_DLSYM(NS_GetComponentRegistrar);
513 NS_DLSYM(NS_StringContainerInit2);
514 NS_DLSYM(NS_CStringContainerInit2);
515 NS_DLSYM(NS_StringContainerFinish);
516 NS_DLSYM(NS_CStringContainerFinish);
517 NS_DLSYM(NS_StringSetData);
518 NS_DLSYM(NS_CStringSetData);
519 NS_DLSYM(NS_NewLocalFile);
520 NS_DLSYM(NS_StringGetData);
521 NS_DLSYM(NS_CStringGetData);
522 NS_DLSYM(NS_StringGetIsVoid);
523 NS_DLSYM(NS_Alloc);
524 NS_DLSYM(NS_Free);
525 NS_DLSYM(ccref_incr);
526 NS_DLSYM(ccref_decr);
527 NS_DLSYM(ccref_init);
528 NS_DLSYM(ccp_init);
529 NS_DLSYM(describe_cc_node);
530 NS_DLSYM(note_cc_edge);
532 #undef NS_DLSYM
534 return TRUE;
537 static BOOL check_version(LPCWSTR gre_path, const char *version_string)
539 WCHAR file_name[MAX_PATH];
540 char version[128];
541 DWORD read=0;
542 HANDLE hfile;
544 static const WCHAR wszVersion[] = {'\\','V','E','R','S','I','O','N',0};
546 strcpyW(file_name, gre_path);
547 strcatW(file_name, wszVersion);
549 hfile = CreateFileW(file_name, GENERIC_READ, FILE_SHARE_READ, NULL,
550 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
551 if(hfile == INVALID_HANDLE_VALUE) {
552 ERR("Could not open VERSION file\n");
553 return FALSE;
556 ReadFile(hfile, version, sizeof(version), &read, NULL);
557 version[read] = 0;
558 CloseHandle(hfile);
560 TRACE("%s\n", debugstr_a(version));
562 if(strcmp(version, version_string)) {
563 ERR("Unexpected version %s, expected %s\n", debugstr_a(version),
564 debugstr_a(version_string));
565 return FALSE;
568 return TRUE;
571 static BOOL load_wine_gecko_v(PRUnichar *gre_path, HKEY mshtml_key,
572 const char *version, const char *version_string)
574 DWORD res, type, size = MAX_PATH;
575 HKEY hkey = mshtml_key;
577 static const WCHAR wszGeckoPath[] =
578 {'G','e','c','k','o','P','a','t','h',0};
580 if(version) {
581 /* @@ Wine registry key: HKLM\Software\Wine\MSHTML\<version> */
582 res = RegOpenKeyA(mshtml_key, version, &hkey);
583 if(res != ERROR_SUCCESS)
584 return FALSE;
587 res = RegQueryValueExW(hkey, wszGeckoPath, NULL, &type, (LPBYTE)gre_path, &size);
588 if(hkey != mshtml_key)
589 RegCloseKey(hkey);
590 if(res != ERROR_SUCCESS || type != REG_SZ)
591 return FALSE;
593 if(!check_version(gre_path, version_string))
594 return FALSE;
596 return load_xul(gre_path);
599 static BOOL load_wine_gecko(PRUnichar *gre_path)
601 HKEY hkey;
602 DWORD res;
603 BOOL ret;
605 static const WCHAR wszMshtmlKey[] = {
606 'S','o','f','t','w','a','r','e','\\','W','i','n','e',
607 '\\','M','S','H','T','M','L',0};
609 /* @@ Wine registry key: HKLM\Software\Wine\MSHTML */
610 res = RegOpenKeyW(HKEY_LOCAL_MACHINE, wszMshtmlKey, &hkey);
611 if(res != ERROR_SUCCESS)
612 return FALSE;
614 ret = load_wine_gecko_v(gre_path, hkey, GECKO_VERSION, GECKO_VERSION_STRING);
616 RegCloseKey(hkey);
617 return ret;
620 static void set_bool_pref(nsIPrefBranch *pref, const char *pref_name, BOOL val)
622 nsresult nsres;
624 nsres = nsIPrefBranch_SetBoolPref(pref, pref_name, val);
625 if(NS_FAILED(nsres))
626 ERR("Could not set pref %s\n", debugstr_a(pref_name));
629 static void set_int_pref(nsIPrefBranch *pref, const char *pref_name, int val)
631 nsresult nsres;
633 nsres = nsIPrefBranch_SetIntPref(pref, pref_name, val);
634 if(NS_FAILED(nsres))
635 ERR("Could not set pref %s\n", debugstr_a(pref_name));
638 static void set_string_pref(nsIPrefBranch *pref, const char *pref_name, const char *val)
640 nsresult nsres;
642 nsres = nsIPrefBranch_SetCharPref(pref, pref_name, val);
643 if(NS_FAILED(nsres))
644 ERR("Could not set pref %s\n", debugstr_a(pref_name));
647 static void set_lang(nsIPrefBranch *pref)
649 char langs[100];
650 DWORD res, size, type;
651 HKEY hkey;
653 static const WCHAR international_keyW[] =
654 {'S','o','f','t','w','a','r','e',
655 '\\','M','i','c','r','o','s','o','f','t',
656 '\\','I','n','t','e','r','n','e','t',' ','E','x','p','l','o','r','e','r',
657 '\\','I','n','t','e','r','n','a','t','i','o','n','a','l',0};
659 res = RegOpenKeyW(HKEY_CURRENT_USER, international_keyW, &hkey);
660 if(res != ERROR_SUCCESS)
661 return;
663 size = sizeof(langs);
664 res = RegQueryValueExA(hkey, "AcceptLanguage", 0, &type, (LPBYTE)langs, &size);
665 RegCloseKey(hkey);
666 if(res != ERROR_SUCCESS || type != REG_SZ)
667 return;
669 TRACE("Setting lang %s\n", debugstr_a(langs));
671 set_string_pref(pref, "intl.accept_languages", langs);
674 static void set_preferences(void)
676 nsIPrefBranch *pref;
677 nsresult nsres;
679 nsres = nsIServiceManager_GetServiceByContractID(pServMgr, NS_PREFERENCES_CONTRACTID,
680 &IID_nsIPrefBranch, (void**)&pref);
681 if(NS_FAILED(nsres)) {
682 ERR("Could not get preference service: %08x\n", nsres);
683 return;
686 set_lang(pref);
687 set_bool_pref(pref, "security.warn_entering_secure", FALSE);
688 set_bool_pref(pref, "security.warn_submit_insecure", FALSE);
689 set_int_pref(pref, "layout.spellcheckDefault", 0);
691 nsIPrefBranch_Release(pref);
694 static BOOL init_xpcom(const PRUnichar *gre_path)
696 nsIComponentRegistrar *registrar = NULL;
697 nsIFile *gre_dir;
698 WCHAR *ptr;
699 nsresult nsres;
701 nsres = create_nsfile(gre_path, &gre_dir);
702 if(NS_FAILED(nsres)) {
703 FreeLibrary(xul_handle);
704 return FALSE;
707 nsres = NS_InitXPCOM2(&pServMgr, gre_dir, (nsIDirectoryServiceProvider*)&nsDirectoryServiceProvider2);
708 if(NS_FAILED(nsres)) {
709 ERR("NS_InitXPCOM2 failed: %08x\n", nsres);
710 FreeLibrary(xul_handle);
711 return FALSE;
714 strcpyW(gecko_path, gre_path);
715 for(ptr = gecko_path; *ptr; ptr++) {
716 if(*ptr == '\\')
717 *ptr = '/';
719 gecko_path_len = ptr-gecko_path;
721 nsres = nsIServiceManager_QueryInterface(pServMgr, &IID_nsIComponentManager, (void**)&pCompMgr);
722 if(NS_FAILED(nsres))
723 ERR("Could not get nsIComponentManager: %08x\n", nsres);
725 init_nsio(pCompMgr);
726 init_mutation(pCompMgr);
727 set_preferences();
729 nsres = nsIServiceManager_GetServiceByContractID(pServMgr, NS_CATEGORYMANAGER_CONTRACTID,
730 &IID_nsICategoryManager, (void**)&cat_mgr);
731 if(NS_FAILED(nsres))
732 ERR("Could not get category manager service: %08x\n", nsres);
734 nsres = NS_GetComponentRegistrar(&registrar);
735 if(NS_SUCCEEDED(nsres)) {
736 register_nsservice(registrar, pServMgr);
737 nsIComponentRegistrar_Release(registrar);
738 }else {
739 ERR("NS_GetComponentRegistrar failed: %08x\n", nsres);
742 init_node_cc();
744 return TRUE;
747 static CRITICAL_SECTION cs_load_gecko;
748 static CRITICAL_SECTION_DEBUG cs_load_gecko_dbg =
750 0, 0, &cs_load_gecko,
751 { &cs_load_gecko_dbg.ProcessLocksList, &cs_load_gecko_dbg.ProcessLocksList },
752 0, 0, { (DWORD_PTR)(__FILE__ ": load_gecko") }
754 static CRITICAL_SECTION cs_load_gecko = { &cs_load_gecko_dbg, -1, 0, 0, 0, 0 };
756 BOOL load_gecko(void)
758 PRUnichar gre_path[MAX_PATH];
759 BOOL ret = FALSE;
761 static DWORD loading_thread;
763 TRACE("()\n");
765 /* load_gecko may be called recursively */
766 if(loading_thread == GetCurrentThreadId())
767 return pCompMgr != NULL;
769 EnterCriticalSection(&cs_load_gecko);
771 if(!loading_thread) {
772 loading_thread = GetCurrentThreadId();
774 if(load_wine_gecko(gre_path)
775 || (install_wine_gecko() && load_wine_gecko(gre_path)))
776 ret = init_xpcom(gre_path);
777 else
778 MESSAGE("Could not load wine-gecko. HTML rendering will be disabled.\n");
779 }else {
780 ret = pCompMgr != NULL;
783 LeaveCriticalSection(&cs_load_gecko);
785 return ret;
788 void *nsalloc(size_t size)
790 return NS_Alloc(size);
793 void nsfree(void *mem)
795 NS_Free(mem);
798 BOOL nsACString_Init(nsACString *str, const char *data)
800 return NS_SUCCEEDED(NS_CStringContainerInit2(str, data, PR_UINT32_MAX, 0));
804 * Initializes nsACString with data owned by caller.
805 * Caller must ensure that data is valid during lifetime of string object.
807 void nsACString_InitDepend(nsACString *str, const char *data)
809 NS_CStringContainerInit2(str, data, PR_UINT32_MAX, NS_CSTRING_CONTAINER_INIT_DEPEND);
812 void nsACString_SetData(nsACString *str, const char *data)
814 NS_CStringSetData(str, data, PR_UINT32_MAX);
817 UINT32 nsACString_GetData(const nsACString *str, const char **data)
819 return NS_CStringGetData(str, data, NULL);
822 void nsACString_Finish(nsACString *str)
824 NS_CStringContainerFinish(str);
827 BOOL nsAString_Init(nsAString *str, const PRUnichar *data)
829 return NS_SUCCEEDED(NS_StringContainerInit2(str, data, PR_UINT32_MAX, 0));
833 * Initializes nsAString with data owned by caller.
834 * Caller must ensure that data is valid during lifetime of string object.
836 void nsAString_InitDepend(nsAString *str, const PRUnichar *data)
838 NS_StringContainerInit2(str, data, PR_UINT32_MAX, NS_STRING_CONTAINER_INIT_DEPEND);
841 UINT32 nsAString_GetData(const nsAString *str, const PRUnichar **data)
843 return NS_StringGetData(str, data, NULL);
846 void nsAString_Finish(nsAString *str)
848 NS_StringContainerFinish(str);
851 HRESULT return_nsstr(nsresult nsres, nsAString *nsstr, BSTR *p)
853 const PRUnichar *str;
855 if(NS_FAILED(nsres)) {
856 ERR("failed: %08x\n", nsres);
857 nsAString_Finish(nsstr);
858 return E_FAIL;
861 nsAString_GetData(nsstr, &str);
862 TRACE("ret %s\n", debugstr_w(str));
863 if(*str) {
864 *p = SysAllocString(str);
865 if(!*p)
866 return E_OUTOFMEMORY;
867 }else {
868 *p = NULL;
871 nsAString_Finish(nsstr);
872 return S_OK;
875 HRESULT return_nsstr_variant(nsresult nsres, nsAString *nsstr, VARIANT *p)
877 const PRUnichar *str;
879 if(NS_FAILED(nsres)) {
880 ERR("failed: %08x\n", nsres);
881 nsAString_Finish(nsstr);
882 return E_FAIL;
885 if(NS_StringGetIsVoid(nsstr)) {
886 TRACE("ret null\n");
887 V_VT(p) = VT_NULL;
888 }else {
889 nsAString_GetData(nsstr, &str);
890 TRACE("ret %s\n", debugstr_w(str));
891 if(*str) {
892 V_BSTR(p) = SysAllocString(str);
893 if(!V_BSTR(p)) {
894 nsAString_Finish(nsstr);
895 return E_OUTOFMEMORY;
897 }else {
898 V_BSTR(p) = NULL;
900 V_VT(p) = VT_BSTR;
903 nsAString_Finish(nsstr);
904 return S_OK;
907 nsICommandParams *create_nscommand_params(void)
909 nsICommandParams *ret = NULL;
910 nsresult nsres;
912 if(!pCompMgr)
913 return NULL;
915 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
916 NS_COMMANDPARAMS_CONTRACTID, NULL, &IID_nsICommandParams,
917 (void**)&ret);
918 if(NS_FAILED(nsres))
919 ERR("Could not get nsICommandParams\n");
921 return ret;
924 nsIWritableVariant *create_nsvariant(void)
926 nsIWritableVariant *ret = NULL;
927 nsresult nsres;
929 if(!pCompMgr)
930 return NULL;
932 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
933 NS_VARIANT_CONTRACTID, NULL, &IID_nsIWritableVariant, (void**)&ret);
934 if(NS_FAILED(nsres))
935 ERR("Could not get nsIVariant\n");
937 return ret;
940 char *get_nscategory_entry(const char *category, const char *entry)
942 char *ret = NULL;
943 nsresult nsres;
945 nsres = nsICategoryManager_GetCategoryEntry(cat_mgr, category, entry, &ret);
946 return NS_SUCCEEDED(nsres) ? ret : NULL;
949 nsresult get_nsinterface(nsISupports *iface, REFIID riid, void **ppv)
951 nsIInterfaceRequestor *iface_req;
952 nsresult nsres;
954 nsres = nsISupports_QueryInterface(iface, &IID_nsIInterfaceRequestor, (void**)&iface_req);
955 if(NS_FAILED(nsres))
956 return nsres;
958 nsres = nsIInterfaceRequestor_GetInterface(iface_req, riid, ppv);
959 nsIInterfaceRequestor_Release(iface_req);
961 return nsres;
964 static HRESULT nsnode_to_nsstring_rec(nsIContentSerializer *serializer, nsIDOMNode *nsnode, nsAString *str)
966 nsIDOMNodeList *node_list = NULL;
967 cpp_bool has_children = FALSE;
968 nsIContent *nscontent;
969 UINT16 type;
970 nsresult nsres;
972 nsIDOMNode_HasChildNodes(nsnode, &has_children);
974 nsres = nsIDOMNode_GetNodeType(nsnode, &type);
975 if(NS_FAILED(nsres)) {
976 ERR("GetType failed: %08x\n", nsres);
977 return E_FAIL;
980 if(type != DOCUMENT_NODE) {
981 nsres = nsIDOMNode_QueryInterface(nsnode, &IID_nsIContent, (void**)&nscontent);
982 if(NS_FAILED(nsres)) {
983 ERR("Could not get nsIContent interface: %08x\n", nsres);
984 return E_FAIL;
988 switch(type) {
989 case ELEMENT_NODE:
990 nsIContentSerializer_AppendElementStart(serializer, nscontent, nscontent, str);
991 break;
992 case TEXT_NODE:
993 nsIContentSerializer_AppendText(serializer, nscontent, 0, -1, str);
994 break;
995 case COMMENT_NODE:
996 nsIContentSerializer_AppendComment(serializer, nscontent, 0, -1, str);
997 break;
998 case DOCUMENT_NODE: {
999 nsIDocument *nsdoc;
1000 nsIDOMNode_QueryInterface(nsnode, &IID_nsIDocument, (void**)&nsdoc);
1001 nsIContentSerializer_AppendDocumentStart(serializer, nsdoc, str);
1002 nsIDocument_Release(nsdoc);
1003 break;
1005 case DOCUMENT_TYPE_NODE:
1006 nsIContentSerializer_AppendDoctype(serializer, nscontent, str);
1007 break;
1008 case DOCUMENT_FRAGMENT_NODE:
1009 break;
1010 default:
1011 FIXME("Unhandled type %u\n", type);
1014 if(has_children) {
1015 UINT32 child_cnt, i;
1016 nsIDOMNode *child_node;
1018 nsIDOMNode_GetChildNodes(nsnode, &node_list);
1019 nsIDOMNodeList_GetLength(node_list, &child_cnt);
1021 for(i=0; i<child_cnt; i++) {
1022 nsres = nsIDOMNodeList_Item(node_list, i, &child_node);
1023 if(NS_SUCCEEDED(nsres)) {
1024 nsnode_to_nsstring_rec(serializer, child_node, str);
1025 nsIDOMNode_Release(child_node);
1026 }else {
1027 ERR("Item failed: %08x\n", nsres);
1031 nsIDOMNodeList_Release(node_list);
1034 if(type == ELEMENT_NODE)
1035 nsIContentSerializer_AppendElementEnd(serializer, nscontent, str);
1037 if(type != DOCUMENT_NODE)
1038 nsIContent_Release(nscontent);
1039 return S_OK;
1042 HRESULT nsnode_to_nsstring(nsIDOMNode *nsnode, nsAString *str)
1044 nsIContentSerializer *serializer;
1045 nsresult nsres;
1046 HRESULT hres;
1048 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
1049 NS_HTMLSERIALIZER_CONTRACTID, NULL, &IID_nsIContentSerializer,
1050 (void**)&serializer);
1051 if(NS_FAILED(nsres)) {
1052 ERR("Could not get nsIContentSerializer: %08x\n", nsres);
1053 return E_FAIL;
1056 nsres = nsIContentSerializer_Init(serializer, 0, 100, NULL, FALSE, FALSE /* FIXME */);
1057 if(NS_FAILED(nsres))
1058 ERR("Init failed: %08x\n", nsres);
1060 hres = nsnode_to_nsstring_rec(serializer, nsnode, str);
1061 if(SUCCEEDED(hres)) {
1062 nsres = nsIContentSerializer_Flush(serializer, str);
1063 if(NS_FAILED(nsres))
1064 ERR("Flush failed: %08x\n", nsres);
1067 nsIContentSerializer_Release(serializer);
1068 return hres;
1071 void get_editor_controller(NSContainer *This)
1073 nsIEditingSession *editing_session = NULL;
1074 nsIControllerContext *ctrlctx;
1075 nsresult nsres;
1077 if(This->editor) {
1078 nsIEditor_Release(This->editor);
1079 This->editor = NULL;
1082 if(This->editor_controller) {
1083 nsIController_Release(This->editor_controller);
1084 This->editor_controller = NULL;
1087 nsres = get_nsinterface((nsISupports*)This->webbrowser, &IID_nsIEditingSession,
1088 (void**)&editing_session);
1089 if(NS_FAILED(nsres)) {
1090 ERR("Could not get nsIEditingSession: %08x\n", nsres);
1091 return;
1094 nsres = nsIEditingSession_GetEditorForWindow(editing_session,
1095 This->doc->basedoc.window->window_proxy, &This->editor);
1096 nsIEditingSession_Release(editing_session);
1097 if(NS_FAILED(nsres)) {
1098 ERR("Could not get editor: %08x\n", nsres);
1099 return;
1102 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
1103 NS_EDITORCONTROLLER_CONTRACTID, NULL, &IID_nsIControllerContext, (void**)&ctrlctx);
1104 if(NS_SUCCEEDED(nsres)) {
1105 nsres = nsIControllerContext_SetCommandContext(ctrlctx, (nsISupports *)This->editor);
1106 if(NS_FAILED(nsres))
1107 ERR("SetCommandContext failed: %08x\n", nsres);
1108 nsres = nsIControllerContext_QueryInterface(ctrlctx, &IID_nsIController,
1109 (void**)&This->editor_controller);
1110 nsIControllerContext_Release(ctrlctx);
1111 if(NS_FAILED(nsres))
1112 ERR("Could not get nsIController interface: %08x\n", nsres);
1113 }else {
1114 ERR("Could not create edit controller: %08x\n", nsres);
1118 void close_gecko(void)
1120 TRACE("()\n");
1122 release_nsio();
1123 init_mutation(NULL);
1125 if(profile_directory) {
1126 nsIFile_Release(profile_directory);
1127 profile_directory = NULL;
1130 if(plugin_directory) {
1131 nsIFile_Release(plugin_directory);
1132 plugin_directory = NULL;
1135 if(pCompMgr)
1136 nsIComponentManager_Release(pCompMgr);
1138 if(pServMgr)
1139 nsIServiceManager_Release(pServMgr);
1141 if(cat_mgr)
1142 nsICategoryManager_Release(cat_mgr);
1144 /* Gecko doesn't really support being unloaded */
1145 /* if (hXPCOM) FreeLibrary(hXPCOM); */
1147 DeleteCriticalSection(&cs_load_gecko);
1150 BOOL is_gecko_path(const char *path)
1152 WCHAR *buf, *ptr;
1153 BOOL ret;
1155 buf = heap_strdupUtoW(path);
1156 if(!buf || strlenW(buf) < gecko_path_len)
1157 return FALSE;
1159 for(ptr = buf; *ptr; ptr++) {
1160 if(*ptr == '\\')
1161 *ptr = '/';
1164 UrlUnescapeW(buf, NULL, NULL, URL_UNESCAPE_INPLACE);
1165 buf[gecko_path_len] = 0;
1167 ret = !strcmpiW(buf, gecko_path);
1168 heap_free(buf);
1169 return ret;
1172 void set_viewer_zoom(NSContainer *nscontainer, float factor)
1174 nsIContentViewer *content_viewer;
1175 nsIDocShell *doc_shell;
1176 nsresult nsres;
1178 TRACE("Setting to %f\n", factor);
1180 nsres = get_nsinterface((nsISupports*)nscontainer->navigation, &IID_nsIDocShell, (void**)&doc_shell);
1181 assert(nsres == NS_OK);
1183 nsres = nsIDocShell_GetContentViewer(doc_shell, &content_viewer);
1184 assert(nsres == NS_OK && content_viewer);
1185 nsIDocShell_Release(doc_shell);
1187 nsres = nsIContentViewer_SetFullZoom(content_viewer, factor);
1188 if(NS_FAILED(nsres))
1189 ERR("SetFullZoom failed: %08x\n", nsres);
1191 nsIContentViewer_Release(content_viewer);
1194 struct nsWeakReference {
1195 nsIWeakReference nsIWeakReference_iface;
1197 LONG ref;
1199 NSContainer *nscontainer;
1202 static inline nsWeakReference *impl_from_nsIWeakReference(nsIWeakReference *iface)
1204 return CONTAINING_RECORD(iface, nsWeakReference, nsIWeakReference_iface);
1207 static nsresult NSAPI nsWeakReference_QueryInterface(nsIWeakReference *iface,
1208 nsIIDRef riid, void **result)
1210 nsWeakReference *This = impl_from_nsIWeakReference(iface);
1212 if(IsEqualGUID(&IID_nsISupports, riid)) {
1213 TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
1214 *result = &This->nsIWeakReference_iface;
1215 }else if(IsEqualGUID(&IID_nsIWeakReference, riid)) {
1216 TRACE("(%p)->(IID_nsIWeakReference %p)\n", This, result);
1217 *result = &This->nsIWeakReference_iface;
1218 }else {
1219 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
1220 *result = NULL;
1221 return NS_NOINTERFACE;
1224 nsISupports_AddRef((nsISupports*)*result);
1225 return NS_OK;
1228 static nsrefcnt NSAPI nsWeakReference_AddRef(nsIWeakReference *iface)
1230 nsWeakReference *This = impl_from_nsIWeakReference(iface);
1231 LONG ref = InterlockedIncrement(&This->ref);
1233 TRACE("(%p) ref=%d\n", This, ref);
1235 return ref;
1238 static nsrefcnt NSAPI nsWeakReference_Release(nsIWeakReference *iface)
1240 nsWeakReference *This = impl_from_nsIWeakReference(iface);
1241 LONG ref = InterlockedDecrement(&This->ref);
1243 TRACE("(%p) ref=%d\n", This, ref);
1245 if(!ref) {
1246 assert(!This->nscontainer);
1247 heap_free(This);
1250 return ref;
1253 static nsresult NSAPI nsWeakReference_QueryReferent(nsIWeakReference *iface,
1254 const nsIID *riid, void **result)
1256 nsWeakReference *This = impl_from_nsIWeakReference(iface);
1258 if(!This->nscontainer)
1259 return NS_ERROR_NULL_POINTER;
1261 return nsIWebBrowserChrome_QueryInterface(&This->nscontainer->nsIWebBrowserChrome_iface, riid, result);
1264 static const nsIWeakReferenceVtbl nsWeakReferenceVtbl = {
1265 nsWeakReference_QueryInterface,
1266 nsWeakReference_AddRef,
1267 nsWeakReference_Release,
1268 nsWeakReference_QueryReferent
1271 /**********************************************************
1272 * nsIWebBrowserChrome interface
1275 static inline NSContainer *impl_from_nsIWebBrowserChrome(nsIWebBrowserChrome *iface)
1277 return CONTAINING_RECORD(iface, NSContainer, nsIWebBrowserChrome_iface);
1280 static nsresult NSAPI nsWebBrowserChrome_QueryInterface(nsIWebBrowserChrome *iface,
1281 nsIIDRef riid, void **result)
1283 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1285 *result = NULL;
1286 if(IsEqualGUID(&IID_nsISupports, riid)) {
1287 TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
1288 *result = &This->nsIWebBrowserChrome_iface;
1289 }else if(IsEqualGUID(&IID_nsIWebBrowserChrome, riid)) {
1290 TRACE("(%p)->(IID_nsIWebBrowserChrome, %p)\n", This, result);
1291 *result = &This->nsIWebBrowserChrome_iface;
1292 }else if(IsEqualGUID(&IID_nsIContextMenuListener, riid)) {
1293 TRACE("(%p)->(IID_nsIContextMenuListener, %p)\n", This, result);
1294 *result = &This->nsIContextMenuListener_iface;
1295 }else if(IsEqualGUID(&IID_nsIURIContentListener, riid)) {
1296 TRACE("(%p)->(IID_nsIURIContentListener %p)\n", This, result);
1297 *result = &This->nsIURIContentListener_iface;
1298 }else if(IsEqualGUID(&IID_nsIEmbeddingSiteWindow, riid)) {
1299 TRACE("(%p)->(IID_nsIEmbeddingSiteWindow %p)\n", This, result);
1300 *result = &This->nsIEmbeddingSiteWindow_iface;
1301 }else if(IsEqualGUID(&IID_nsITooltipListener, riid)) {
1302 TRACE("(%p)->(IID_nsITooltipListener %p)\n", This, result);
1303 *result = &This->nsITooltipListener_iface;
1304 }else if(IsEqualGUID(&IID_nsIInterfaceRequestor, riid)) {
1305 TRACE("(%p)->(IID_nsIInterfaceRequestor %p)\n", This, result);
1306 *result = &This->nsIInterfaceRequestor_iface;
1307 }else if(IsEqualGUID(&IID_nsISupportsWeakReference, riid)) {
1308 TRACE("(%p)->(IID_nsISupportsWeakReference %p)\n", This, result);
1309 *result = &This->nsISupportsWeakReference_iface;
1312 if(*result) {
1313 nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1314 return NS_OK;
1317 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
1318 return NS_NOINTERFACE;
1321 static nsrefcnt NSAPI nsWebBrowserChrome_AddRef(nsIWebBrowserChrome *iface)
1323 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1324 LONG ref = InterlockedIncrement(&This->ref);
1326 TRACE("(%p) ref=%d\n", This, ref);
1328 return ref;
1331 static nsrefcnt NSAPI nsWebBrowserChrome_Release(nsIWebBrowserChrome *iface)
1333 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1334 LONG ref = InterlockedDecrement(&This->ref);
1336 TRACE("(%p) ref=%d\n", This, ref);
1338 if(!ref) {
1339 if(This->parent)
1340 nsIWebBrowserChrome_Release(&This->parent->nsIWebBrowserChrome_iface);
1341 if(This->weak_reference) {
1342 This->weak_reference->nscontainer = NULL;
1343 nsIWeakReference_Release(&This->weak_reference->nsIWeakReference_iface);
1345 heap_free(This);
1348 return ref;
1351 static nsresult NSAPI nsWebBrowserChrome_SetStatus(nsIWebBrowserChrome *iface,
1352 UINT32 statusType, const PRUnichar *status)
1354 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1355 TRACE("(%p)->(%d %s)\n", This, statusType, debugstr_w(status));
1356 return NS_OK;
1359 static nsresult NSAPI nsWebBrowserChrome_GetWebBrowser(nsIWebBrowserChrome *iface,
1360 nsIWebBrowser **aWebBrowser)
1362 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1364 TRACE("(%p)->(%p)\n", This, aWebBrowser);
1366 if(!aWebBrowser)
1367 return NS_ERROR_INVALID_ARG;
1369 if(This->webbrowser)
1370 nsIWebBrowser_AddRef(This->webbrowser);
1371 *aWebBrowser = This->webbrowser;
1372 return S_OK;
1375 static nsresult NSAPI nsWebBrowserChrome_SetWebBrowser(nsIWebBrowserChrome *iface,
1376 nsIWebBrowser *aWebBrowser)
1378 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1380 TRACE("(%p)->(%p)\n", This, aWebBrowser);
1382 if(aWebBrowser != This->webbrowser)
1383 ERR("Wrong nsWebBrowser!\n");
1385 return NS_OK;
1388 static nsresult NSAPI nsWebBrowserChrome_GetChromeFlags(nsIWebBrowserChrome *iface,
1389 UINT32 *aChromeFlags)
1391 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1392 WARN("(%p)->(%p)\n", This, aChromeFlags);
1393 return NS_ERROR_NOT_IMPLEMENTED;
1396 static nsresult NSAPI nsWebBrowserChrome_SetChromeFlags(nsIWebBrowserChrome *iface,
1397 UINT32 aChromeFlags)
1399 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1400 WARN("(%p)->(%08x)\n", This, aChromeFlags);
1401 return NS_ERROR_NOT_IMPLEMENTED;
1404 static nsresult NSAPI nsWebBrowserChrome_DestroyBrowserWindow(nsIWebBrowserChrome *iface)
1406 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1407 TRACE("(%p)\n", This);
1408 return NS_ERROR_NOT_IMPLEMENTED;
1411 static nsresult NSAPI nsWebBrowserChrome_SizeBrowserTo(nsIWebBrowserChrome *iface,
1412 LONG aCX, LONG aCY)
1414 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1415 WARN("(%p)->(%d %d)\n", This, aCX, aCY);
1416 return NS_ERROR_NOT_IMPLEMENTED;
1419 static nsresult NSAPI nsWebBrowserChrome_ShowAsModal(nsIWebBrowserChrome *iface)
1421 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1422 WARN("(%p)\n", This);
1423 return NS_ERROR_NOT_IMPLEMENTED;
1426 static nsresult NSAPI nsWebBrowserChrome_IsWindowModal(nsIWebBrowserChrome *iface, cpp_bool *_retval)
1428 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1429 WARN("(%p)->(%p)\n", This, _retval);
1430 return NS_ERROR_NOT_IMPLEMENTED;
1433 static nsresult NSAPI nsWebBrowserChrome_ExitModalEventLoop(nsIWebBrowserChrome *iface,
1434 nsresult aStatus)
1436 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1437 WARN("(%p)->(%08x)\n", This, aStatus);
1438 return NS_ERROR_NOT_IMPLEMENTED;
1441 static const nsIWebBrowserChromeVtbl nsWebBrowserChromeVtbl = {
1442 nsWebBrowserChrome_QueryInterface,
1443 nsWebBrowserChrome_AddRef,
1444 nsWebBrowserChrome_Release,
1445 nsWebBrowserChrome_SetStatus,
1446 nsWebBrowserChrome_GetWebBrowser,
1447 nsWebBrowserChrome_SetWebBrowser,
1448 nsWebBrowserChrome_GetChromeFlags,
1449 nsWebBrowserChrome_SetChromeFlags,
1450 nsWebBrowserChrome_DestroyBrowserWindow,
1451 nsWebBrowserChrome_SizeBrowserTo,
1452 nsWebBrowserChrome_ShowAsModal,
1453 nsWebBrowserChrome_IsWindowModal,
1454 nsWebBrowserChrome_ExitModalEventLoop
1457 /**********************************************************
1458 * nsIContextMenuListener interface
1461 static inline NSContainer *impl_from_nsIContextMenuListener(nsIContextMenuListener *iface)
1463 return CONTAINING_RECORD(iface, NSContainer, nsIContextMenuListener_iface);
1466 static nsresult NSAPI nsContextMenuListener_QueryInterface(nsIContextMenuListener *iface,
1467 nsIIDRef riid, void **result)
1469 NSContainer *This = impl_from_nsIContextMenuListener(iface);
1470 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1473 static nsrefcnt NSAPI nsContextMenuListener_AddRef(nsIContextMenuListener *iface)
1475 NSContainer *This = impl_from_nsIContextMenuListener(iface);
1476 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1479 static nsrefcnt NSAPI nsContextMenuListener_Release(nsIContextMenuListener *iface)
1481 NSContainer *This = impl_from_nsIContextMenuListener(iface);
1482 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
1485 static nsresult NSAPI nsContextMenuListener_OnShowContextMenu(nsIContextMenuListener *iface,
1486 UINT32 aContextFlags, nsIDOMEvent *aEvent, nsIDOMNode *aNode)
1488 NSContainer *This = impl_from_nsIContextMenuListener(iface);
1489 nsIDOMMouseEvent *event;
1490 HTMLDOMNode *node;
1491 POINT pt;
1492 DWORD dwID = CONTEXT_MENU_DEFAULT;
1493 nsresult nsres;
1494 HRESULT hres;
1496 TRACE("(%p)->(%08x %p %p)\n", This, aContextFlags, aEvent, aNode);
1498 hres = get_node(This->doc->basedoc.doc_node, aNode, TRUE, &node);
1499 if(FAILED(hres))
1500 return NS_ERROR_FAILURE;
1502 fire_event(This->doc->basedoc.doc_node /* FIXME */, EVENTID_CONTEXTMENU, TRUE, &node->event_target, aEvent);
1504 nsres = nsIDOMEvent_QueryInterface(aEvent, &IID_nsIDOMMouseEvent, (void**)&event);
1505 assert(NS_SUCCEEDED(nsres));
1507 nsIDOMMouseEvent_GetScreenX(event, &pt.x);
1508 nsIDOMMouseEvent_GetScreenY(event, &pt.y);
1509 nsIDOMMouseEvent_Release(event);
1511 switch(aContextFlags) {
1512 case CONTEXT_NONE:
1513 case CONTEXT_DOCUMENT:
1514 case CONTEXT_TEXT: {
1515 nsISelection *selection;
1517 nsres = nsIDOMHTMLDocument_GetSelection(This->doc->basedoc.doc_node->nsdoc, &selection);
1518 if(NS_SUCCEEDED(nsres) && selection) {
1519 cpp_bool is_collapsed;
1521 /* FIXME: Check if the click was inside selection. */
1522 nsres = nsISelection_GetIsCollapsed(selection, &is_collapsed);
1523 nsISelection_Release(selection);
1524 if(NS_SUCCEEDED(nsres) && !is_collapsed)
1525 dwID = CONTEXT_MENU_TEXTSELECT;
1527 break;
1529 case CONTEXT_IMAGE:
1530 case CONTEXT_IMAGE|CONTEXT_LINK:
1531 dwID = CONTEXT_MENU_IMAGE;
1532 break;
1533 case CONTEXT_LINK:
1534 dwID = CONTEXT_MENU_ANCHOR;
1535 break;
1536 case CONTEXT_INPUT:
1537 dwID = CONTEXT_MENU_CONTROL;
1538 break;
1539 default:
1540 FIXME("aContextFlags=%08x\n", aContextFlags);
1543 show_context_menu(This->doc, dwID, &pt, (IDispatch*)&node->IHTMLDOMNode_iface);
1544 node_release(node);
1545 return NS_OK;
1548 static const nsIContextMenuListenerVtbl nsContextMenuListenerVtbl = {
1549 nsContextMenuListener_QueryInterface,
1550 nsContextMenuListener_AddRef,
1551 nsContextMenuListener_Release,
1552 nsContextMenuListener_OnShowContextMenu
1555 /**********************************************************
1556 * nsIURIContentListener interface
1559 static inline NSContainer *impl_from_nsIURIContentListener(nsIURIContentListener *iface)
1561 return CONTAINING_RECORD(iface, NSContainer, nsIURIContentListener_iface);
1564 static nsresult NSAPI nsURIContentListener_QueryInterface(nsIURIContentListener *iface,
1565 nsIIDRef riid, void **result)
1567 NSContainer *This = impl_from_nsIURIContentListener(iface);
1568 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1571 static nsrefcnt NSAPI nsURIContentListener_AddRef(nsIURIContentListener *iface)
1573 NSContainer *This = impl_from_nsIURIContentListener(iface);
1574 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1577 static nsrefcnt NSAPI nsURIContentListener_Release(nsIURIContentListener *iface)
1579 NSContainer *This = impl_from_nsIURIContentListener(iface);
1580 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
1583 static nsresult NSAPI nsURIContentListener_OnStartURIOpen(nsIURIContentListener *iface,
1584 nsIURI *aURI, cpp_bool *_retval)
1586 NSContainer *This = impl_from_nsIURIContentListener(iface);
1587 nsACString spec_str;
1588 const char *spec;
1589 nsresult nsres;
1591 nsACString_Init(&spec_str, NULL);
1592 nsIURI_GetSpec(aURI, &spec_str);
1593 nsACString_GetData(&spec_str, &spec);
1595 TRACE("(%p)->(%p(%s) %p)\n", This, aURI, debugstr_a(spec), _retval);
1597 nsACString_Finish(&spec_str);
1599 nsres = on_start_uri_open(This, aURI, _retval);
1600 if(NS_FAILED(nsres))
1601 return nsres;
1603 return !*_retval && This->content_listener
1604 ? nsIURIContentListener_OnStartURIOpen(This->content_listener, aURI, _retval)
1605 : NS_OK;
1608 static nsresult NSAPI nsURIContentListener_DoContent(nsIURIContentListener *iface,
1609 const nsACString *aContentType, cpp_bool aIsContentPreferred, nsIRequest *aRequest,
1610 nsIStreamListener **aContentHandler, cpp_bool *_retval)
1612 NSContainer *This = impl_from_nsIURIContentListener(iface);
1614 TRACE("(%p)->(%p %x %p %p %p)\n", This, aContentType, aIsContentPreferred,
1615 aRequest, aContentHandler, _retval);
1617 return This->content_listener
1618 ? nsIURIContentListener_DoContent(This->content_listener, aContentType,
1619 aIsContentPreferred, aRequest, aContentHandler, _retval)
1620 : NS_ERROR_NOT_IMPLEMENTED;
1623 static nsresult NSAPI nsURIContentListener_IsPreferred(nsIURIContentListener *iface,
1624 const char *aContentType, char **aDesiredContentType, cpp_bool *_retval)
1626 NSContainer *This = impl_from_nsIURIContentListener(iface);
1628 TRACE("(%p)->(%s %p %p)\n", This, debugstr_a(aContentType), aDesiredContentType, _retval);
1630 /* FIXME: Should we do something here? */
1631 *_retval = TRUE;
1633 return This->content_listener
1634 ? nsIURIContentListener_IsPreferred(This->content_listener, aContentType,
1635 aDesiredContentType, _retval)
1636 : NS_OK;
1639 static nsresult NSAPI nsURIContentListener_CanHandleContent(nsIURIContentListener *iface,
1640 const char *aContentType, cpp_bool aIsContentPreferred, char **aDesiredContentType,
1641 cpp_bool *_retval)
1643 NSContainer *This = impl_from_nsIURIContentListener(iface);
1645 TRACE("(%p)->(%s %x %p %p)\n", This, debugstr_a(aContentType), aIsContentPreferred,
1646 aDesiredContentType, _retval);
1648 return This->content_listener
1649 ? nsIURIContentListener_CanHandleContent(This->content_listener, aContentType,
1650 aIsContentPreferred, aDesiredContentType, _retval)
1651 : NS_ERROR_NOT_IMPLEMENTED;
1654 static nsresult NSAPI nsURIContentListener_GetLoadCookie(nsIURIContentListener *iface,
1655 nsISupports **aLoadCookie)
1657 NSContainer *This = impl_from_nsIURIContentListener(iface);
1659 WARN("(%p)->(%p)\n", This, aLoadCookie);
1661 return This->content_listener
1662 ? nsIURIContentListener_GetLoadCookie(This->content_listener, aLoadCookie)
1663 : NS_ERROR_NOT_IMPLEMENTED;
1666 static nsresult NSAPI nsURIContentListener_SetLoadCookie(nsIURIContentListener *iface,
1667 nsISupports *aLoadCookie)
1669 NSContainer *This = impl_from_nsIURIContentListener(iface);
1671 WARN("(%p)->(%p)\n", This, aLoadCookie);
1673 return This->content_listener
1674 ? nsIURIContentListener_SetLoadCookie(This->content_listener, aLoadCookie)
1675 : NS_ERROR_NOT_IMPLEMENTED;
1678 static nsresult NSAPI nsURIContentListener_GetParentContentListener(nsIURIContentListener *iface,
1679 nsIURIContentListener **aParentContentListener)
1681 NSContainer *This = impl_from_nsIURIContentListener(iface);
1683 TRACE("(%p)->(%p)\n", This, aParentContentListener);
1685 if(This->content_listener)
1686 nsIURIContentListener_AddRef(This->content_listener);
1688 *aParentContentListener = This->content_listener;
1689 return NS_OK;
1692 static nsresult NSAPI nsURIContentListener_SetParentContentListener(nsIURIContentListener *iface,
1693 nsIURIContentListener *aParentContentListener)
1695 NSContainer *This = impl_from_nsIURIContentListener(iface);
1697 TRACE("(%p)->(%p)\n", This, aParentContentListener);
1699 if(aParentContentListener == &This->nsIURIContentListener_iface)
1700 return NS_OK;
1702 if(This->content_listener)
1703 nsIURIContentListener_Release(This->content_listener);
1705 This->content_listener = aParentContentListener;
1706 if(This->content_listener)
1707 nsIURIContentListener_AddRef(This->content_listener);
1709 return NS_OK;
1712 static const nsIURIContentListenerVtbl nsURIContentListenerVtbl = {
1713 nsURIContentListener_QueryInterface,
1714 nsURIContentListener_AddRef,
1715 nsURIContentListener_Release,
1716 nsURIContentListener_OnStartURIOpen,
1717 nsURIContentListener_DoContent,
1718 nsURIContentListener_IsPreferred,
1719 nsURIContentListener_CanHandleContent,
1720 nsURIContentListener_GetLoadCookie,
1721 nsURIContentListener_SetLoadCookie,
1722 nsURIContentListener_GetParentContentListener,
1723 nsURIContentListener_SetParentContentListener
1726 /**********************************************************
1727 * nsIEmbeddinSiteWindow interface
1730 static inline NSContainer *impl_from_nsIEmbeddingSiteWindow(nsIEmbeddingSiteWindow *iface)
1732 return CONTAINING_RECORD(iface, NSContainer, nsIEmbeddingSiteWindow_iface);
1735 static nsresult NSAPI nsEmbeddingSiteWindow_QueryInterface(nsIEmbeddingSiteWindow *iface,
1736 nsIIDRef riid, void **result)
1738 NSContainer *This = impl_from_nsIEmbeddingSiteWindow(iface);
1739 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1742 static nsrefcnt NSAPI nsEmbeddingSiteWindow_AddRef(nsIEmbeddingSiteWindow *iface)
1744 NSContainer *This = impl_from_nsIEmbeddingSiteWindow(iface);
1745 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1748 static nsrefcnt NSAPI nsEmbeddingSiteWindow_Release(nsIEmbeddingSiteWindow *iface)
1750 NSContainer *This = impl_from_nsIEmbeddingSiteWindow(iface);
1751 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
1754 static nsresult NSAPI nsEmbeddingSiteWindow_SetDimensions(nsIEmbeddingSiteWindow *iface,
1755 UINT32 flags, LONG x, LONG y, LONG cx, LONG cy)
1757 NSContainer *This = impl_from_nsIEmbeddingSiteWindow(iface);
1758 WARN("(%p)->(%08x %d %d %d %d)\n", This, flags, x, y, cx, cy);
1759 return NS_ERROR_NOT_IMPLEMENTED;
1762 static nsresult NSAPI nsEmbeddingSiteWindow_GetDimensions(nsIEmbeddingSiteWindow *iface,
1763 UINT32 flags, LONG *x, LONG *y, LONG *cx, LONG *cy)
1765 NSContainer *This = impl_from_nsIEmbeddingSiteWindow(iface);
1766 RECT r;
1768 TRACE("(%p)->(%x %p %p %p %p)\n", This, flags, x, y, cx, cy);
1770 if(!GetWindowRect(This->hwnd, &r)) {
1771 ERR("GetWindowRect failed\n");
1772 return NS_ERROR_FAILURE;
1775 if(x)
1776 *x = r.left;
1777 if(y)
1778 *y = r.top;
1779 if(cx)
1780 *cx = r.right-r.left;
1781 if(cy)
1782 *cy = r.bottom-r.top;
1783 return NS_OK;
1786 static nsresult NSAPI nsEmbeddingSiteWindow_SetFocus(nsIEmbeddingSiteWindow *iface)
1788 NSContainer *This = impl_from_nsIEmbeddingSiteWindow(iface);
1790 TRACE("(%p)\n", This);
1792 return nsIBaseWindow_SetFocus(This->window);
1795 static nsresult NSAPI nsEmbeddingSiteWindow_GetVisibility(nsIEmbeddingSiteWindow *iface,
1796 cpp_bool *aVisibility)
1798 NSContainer *This = impl_from_nsIEmbeddingSiteWindow(iface);
1800 TRACE("(%p)->(%p)\n", This, aVisibility);
1802 *aVisibility = This->doc && This->doc->hwnd && IsWindowVisible(This->doc->hwnd);
1803 return NS_OK;
1806 static nsresult NSAPI nsEmbeddingSiteWindow_SetVisibility(nsIEmbeddingSiteWindow *iface,
1807 cpp_bool aVisibility)
1809 NSContainer *This = impl_from_nsIEmbeddingSiteWindow(iface);
1811 TRACE("(%p)->(%x)\n", This, aVisibility);
1813 return NS_OK;
1816 static nsresult NSAPI nsEmbeddingSiteWindow_GetTitle(nsIEmbeddingSiteWindow *iface,
1817 PRUnichar **aTitle)
1819 NSContainer *This = impl_from_nsIEmbeddingSiteWindow(iface);
1820 WARN("(%p)->(%p)\n", This, aTitle);
1821 return NS_ERROR_NOT_IMPLEMENTED;
1824 static nsresult NSAPI nsEmbeddingSiteWindow_SetTitle(nsIEmbeddingSiteWindow *iface,
1825 const PRUnichar *aTitle)
1827 NSContainer *This = impl_from_nsIEmbeddingSiteWindow(iface);
1828 WARN("(%p)->(%s)\n", This, debugstr_w(aTitle));
1829 return NS_ERROR_NOT_IMPLEMENTED;
1832 static nsresult NSAPI nsEmbeddingSiteWindow_GetSiteWindow(nsIEmbeddingSiteWindow *iface,
1833 void **aSiteWindow)
1835 NSContainer *This = impl_from_nsIEmbeddingSiteWindow(iface);
1837 TRACE("(%p)->(%p)\n", This, aSiteWindow);
1839 *aSiteWindow = This->hwnd;
1840 return NS_OK;
1843 static const nsIEmbeddingSiteWindowVtbl nsEmbeddingSiteWindowVtbl = {
1844 nsEmbeddingSiteWindow_QueryInterface,
1845 nsEmbeddingSiteWindow_AddRef,
1846 nsEmbeddingSiteWindow_Release,
1847 nsEmbeddingSiteWindow_SetDimensions,
1848 nsEmbeddingSiteWindow_GetDimensions,
1849 nsEmbeddingSiteWindow_SetFocus,
1850 nsEmbeddingSiteWindow_GetVisibility,
1851 nsEmbeddingSiteWindow_SetVisibility,
1852 nsEmbeddingSiteWindow_GetTitle,
1853 nsEmbeddingSiteWindow_SetTitle,
1854 nsEmbeddingSiteWindow_GetSiteWindow
1857 static inline NSContainer *impl_from_nsITooltipListener(nsITooltipListener *iface)
1859 return CONTAINING_RECORD(iface, NSContainer, nsITooltipListener_iface);
1862 static nsresult NSAPI nsTooltipListener_QueryInterface(nsITooltipListener *iface, nsIIDRef riid,
1863 void **result)
1865 NSContainer *This = impl_from_nsITooltipListener(iface);
1866 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1869 static nsrefcnt NSAPI nsTooltipListener_AddRef(nsITooltipListener *iface)
1871 NSContainer *This = impl_from_nsITooltipListener(iface);
1872 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1875 static nsrefcnt NSAPI nsTooltipListener_Release(nsITooltipListener *iface)
1877 NSContainer *This = impl_from_nsITooltipListener(iface);
1878 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
1881 static nsresult NSAPI nsTooltipListener_OnShowTooltip(nsITooltipListener *iface,
1882 LONG aXCoord, LONG aYCoord, const PRUnichar *aTipText)
1884 NSContainer *This = impl_from_nsITooltipListener(iface);
1886 if (This->doc)
1887 show_tooltip(This->doc, aXCoord, aYCoord, aTipText);
1889 return NS_OK;
1892 static nsresult NSAPI nsTooltipListener_OnHideTooltip(nsITooltipListener *iface)
1894 NSContainer *This = impl_from_nsITooltipListener(iface);
1896 if (This->doc)
1897 hide_tooltip(This->doc);
1899 return NS_OK;
1902 static const nsITooltipListenerVtbl nsTooltipListenerVtbl = {
1903 nsTooltipListener_QueryInterface,
1904 nsTooltipListener_AddRef,
1905 nsTooltipListener_Release,
1906 nsTooltipListener_OnShowTooltip,
1907 nsTooltipListener_OnHideTooltip
1910 static inline NSContainer *impl_from_nsIInterfaceRequestor(nsIInterfaceRequestor *iface)
1912 return CONTAINING_RECORD(iface, NSContainer, nsIInterfaceRequestor_iface);
1915 static nsresult NSAPI nsInterfaceRequestor_QueryInterface(nsIInterfaceRequestor *iface,
1916 nsIIDRef riid, void **result)
1918 NSContainer *This = impl_from_nsIInterfaceRequestor(iface);
1919 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1922 static nsrefcnt NSAPI nsInterfaceRequestor_AddRef(nsIInterfaceRequestor *iface)
1924 NSContainer *This = impl_from_nsIInterfaceRequestor(iface);
1925 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1928 static nsrefcnt NSAPI nsInterfaceRequestor_Release(nsIInterfaceRequestor *iface)
1930 NSContainer *This = impl_from_nsIInterfaceRequestor(iface);
1931 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
1934 static nsresult NSAPI nsInterfaceRequestor_GetInterface(nsIInterfaceRequestor *iface,
1935 nsIIDRef riid, void **result)
1937 NSContainer *This = impl_from_nsIInterfaceRequestor(iface);
1939 if(IsEqualGUID(&IID_mozIDOMWindowProxy, riid)) {
1940 TRACE("(%p)->(IID_nsIDOMWindow %p)\n", This, result);
1941 return nsIWebBrowser_GetContentDOMWindow(This->webbrowser, (mozIDOMWindowProxy**)result);
1944 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1947 static const nsIInterfaceRequestorVtbl nsInterfaceRequestorVtbl = {
1948 nsInterfaceRequestor_QueryInterface,
1949 nsInterfaceRequestor_AddRef,
1950 nsInterfaceRequestor_Release,
1951 nsInterfaceRequestor_GetInterface
1954 static inline NSContainer *impl_from_nsISupportsWeakReference(nsISupportsWeakReference *iface)
1956 return CONTAINING_RECORD(iface, NSContainer, nsISupportsWeakReference_iface);
1959 static nsresult NSAPI nsSupportsWeakReference_QueryInterface(nsISupportsWeakReference *iface,
1960 nsIIDRef riid, void **result)
1962 NSContainer *This = impl_from_nsISupportsWeakReference(iface);
1963 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1966 static nsrefcnt NSAPI nsSupportsWeakReference_AddRef(nsISupportsWeakReference *iface)
1968 NSContainer *This = impl_from_nsISupportsWeakReference(iface);
1969 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1972 static nsrefcnt NSAPI nsSupportsWeakReference_Release(nsISupportsWeakReference *iface)
1974 NSContainer *This = impl_from_nsISupportsWeakReference(iface);
1975 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
1978 static nsresult NSAPI nsSupportsWeakReference_GetWeakReference(nsISupportsWeakReference *iface,
1979 nsIWeakReference **_retval)
1981 NSContainer *This = impl_from_nsISupportsWeakReference(iface);
1983 TRACE("(%p)->(%p)\n", This, _retval);
1985 if(!This->weak_reference) {
1986 This->weak_reference = heap_alloc(sizeof(nsWeakReference));
1987 if(!This->weak_reference)
1988 return NS_ERROR_OUT_OF_MEMORY;
1990 This->weak_reference->nsIWeakReference_iface.lpVtbl = &nsWeakReferenceVtbl;
1991 This->weak_reference->ref = 1;
1992 This->weak_reference->nscontainer = This;
1995 *_retval = &This->weak_reference->nsIWeakReference_iface;
1996 nsIWeakReference_AddRef(*_retval);
1997 return NS_OK;
2000 static const nsISupportsWeakReferenceVtbl nsSupportsWeakReferenceVtbl = {
2001 nsSupportsWeakReference_QueryInterface,
2002 nsSupportsWeakReference_AddRef,
2003 nsSupportsWeakReference_Release,
2004 nsSupportsWeakReference_GetWeakReference
2007 static HRESULT init_nscontainer(NSContainer *nscontainer)
2009 nsIWebBrowserSetup *wbsetup;
2010 nsIScrollable *scrollable;
2011 nsresult nsres;
2013 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_WEBBROWSER_CONTRACTID,
2014 NULL, &IID_nsIWebBrowser, (void**)&nscontainer->webbrowser);
2015 if(NS_FAILED(nsres)) {
2016 ERR("Creating WebBrowser failed: %08x\n", nsres);
2017 return E_FAIL;
2020 nsres = nsIWebBrowser_SetContainerWindow(nscontainer->webbrowser, &nscontainer->nsIWebBrowserChrome_iface);
2021 if(NS_FAILED(nsres)) {
2022 ERR("SetContainerWindow failed: %08x\n", nsres);
2023 return E_FAIL;
2026 nsres = nsIWebBrowser_QueryInterface(nscontainer->webbrowser, &IID_nsIBaseWindow,
2027 (void**)&nscontainer->window);
2028 if(NS_FAILED(nsres)) {
2029 ERR("Could not get nsIBaseWindow interface: %08x\n", nsres);
2030 return E_FAIL;
2033 nsres = nsIWebBrowser_QueryInterface(nscontainer->webbrowser, &IID_nsIWebBrowserSetup,
2034 (void**)&wbsetup);
2035 if(NS_SUCCEEDED(nsres)) {
2036 nsres = nsIWebBrowserSetup_SetProperty(wbsetup, SETUP_IS_CHROME_WRAPPER, FALSE);
2037 nsIWebBrowserSetup_Release(wbsetup);
2038 if(NS_FAILED(nsres)) {
2039 ERR("SetProperty(SETUP_IS_CHROME_WRAPPER) failed: %08x\n", nsres);
2040 return E_FAIL;
2042 }else {
2043 ERR("Could not get nsIWebBrowserSetup interface\n");
2044 return E_FAIL;
2047 nsres = nsIWebBrowser_QueryInterface(nscontainer->webbrowser, &IID_nsIWebNavigation,
2048 (void**)&nscontainer->navigation);
2049 if(NS_FAILED(nsres)) {
2050 ERR("Could not get nsIWebNavigation interface: %08x\n", nsres);
2051 return E_FAIL;
2054 nsres = nsIWebBrowser_QueryInterface(nscontainer->webbrowser, &IID_nsIWebBrowserFocus,
2055 (void**)&nscontainer->focus);
2056 if(NS_FAILED(nsres)) {
2057 ERR("Could not get nsIWebBrowserFocus interface: %08x\n", nsres);
2058 return E_FAIL;
2061 if(!nscontainer_class) {
2062 register_nscontainer_class();
2063 if(!nscontainer_class)
2064 return E_FAIL;
2067 nscontainer->hwnd = CreateWindowExW(0, wszNsContainer, NULL,
2068 WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 0, 0, 100, 100,
2069 GetDesktopWindow(), NULL, hInst, nscontainer);
2070 if(!nscontainer->hwnd) {
2071 WARN("Could not create window\n");
2072 return E_FAIL;
2075 nsres = nsIBaseWindow_InitWindow(nscontainer->window, nscontainer->hwnd, NULL, 0, 0, 100, 100);
2076 if(NS_SUCCEEDED(nsres)) {
2077 nsres = nsIBaseWindow_Create(nscontainer->window);
2078 if(NS_FAILED(nsres)) {
2079 WARN("Creating window failed: %08x\n", nsres);
2080 return E_FAIL;
2083 nsIBaseWindow_SetVisibility(nscontainer->window, FALSE);
2084 nsIBaseWindow_SetEnabled(nscontainer->window, FALSE);
2085 }else {
2086 ERR("InitWindow failed: %08x\n", nsres);
2087 return E_FAIL;
2090 nsres = nsIWebBrowser_SetParentURIContentListener(nscontainer->webbrowser,
2091 &nscontainer->nsIURIContentListener_iface);
2092 if(NS_FAILED(nsres))
2093 ERR("SetParentURIContentListener failed: %08x\n", nsres);
2095 nsres = nsIWebBrowser_QueryInterface(nscontainer->webbrowser, &IID_nsIScrollable, (void**)&scrollable);
2096 if(NS_SUCCEEDED(nsres)) {
2097 nsres = nsIScrollable_SetDefaultScrollbarPreferences(scrollable,
2098 ScrollOrientation_Y, Scrollbar_Always);
2099 if(NS_FAILED(nsres))
2100 ERR("Could not set default Y scrollbar prefs: %08x\n", nsres);
2102 nsres = nsIScrollable_SetDefaultScrollbarPreferences(scrollable,
2103 ScrollOrientation_X, Scrollbar_Auto);
2104 if(NS_FAILED(nsres))
2105 ERR("Could not set default X scrollbar prefs: %08x\n", nsres);
2107 nsIScrollable_Release(scrollable);
2108 }else {
2109 ERR("Could not get nsIScrollable: %08x\n", nsres);
2112 return S_OK;
2115 HRESULT create_nscontainer(HTMLDocumentObj *doc, NSContainer **_ret)
2117 NSContainer *ret;
2118 HRESULT hres;
2120 if(!load_gecko())
2121 return CLASS_E_CLASSNOTAVAILABLE;
2123 ret = heap_alloc_zero(sizeof(NSContainer));
2124 if(!ret)
2125 return E_OUTOFMEMORY;
2127 ret->nsIWebBrowserChrome_iface.lpVtbl = &nsWebBrowserChromeVtbl;
2128 ret->nsIContextMenuListener_iface.lpVtbl = &nsContextMenuListenerVtbl;
2129 ret->nsIURIContentListener_iface.lpVtbl = &nsURIContentListenerVtbl;
2130 ret->nsIEmbeddingSiteWindow_iface.lpVtbl = &nsEmbeddingSiteWindowVtbl;
2131 ret->nsITooltipListener_iface.lpVtbl = &nsTooltipListenerVtbl;
2132 ret->nsIInterfaceRequestor_iface.lpVtbl = &nsInterfaceRequestorVtbl;
2133 ret->nsISupportsWeakReference_iface.lpVtbl = &nsSupportsWeakReferenceVtbl;
2135 ret->doc = doc;
2136 ret->ref = 1;
2138 hres = init_nscontainer(ret);
2139 if(SUCCEEDED(hres))
2140 *_ret = ret;
2141 else
2142 nsIWebBrowserChrome_Release(&ret->nsIWebBrowserChrome_iface);
2143 return hres;
2146 void NSContainer_Release(NSContainer *This)
2148 TRACE("(%p)\n", This);
2150 This->doc = NULL;
2152 ShowWindow(This->hwnd, SW_HIDE);
2153 SetParent(This->hwnd, NULL);
2155 nsIBaseWindow_SetVisibility(This->window, FALSE);
2156 nsIBaseWindow_Destroy(This->window);
2158 nsIWebBrowser_SetContainerWindow(This->webbrowser, NULL);
2160 nsIWebBrowser_Release(This->webbrowser);
2161 This->webbrowser = NULL;
2163 nsIWebNavigation_Release(This->navigation);
2164 This->navigation = NULL;
2166 nsIBaseWindow_Release(This->window);
2167 This->window = NULL;
2169 nsIWebBrowserFocus_Release(This->focus);
2170 This->focus = NULL;
2172 if(This->editor_controller) {
2173 nsIController_Release(This->editor_controller);
2174 This->editor_controller = NULL;
2177 if(This->editor) {
2178 nsIEditor_Release(This->editor);
2179 This->editor = NULL;
2182 if(This->content_listener) {
2183 nsIURIContentListener_Release(This->content_listener);
2184 This->content_listener = NULL;
2187 if(This->hwnd) {
2188 DestroyWindow(This->hwnd);
2189 This->hwnd = NULL;
2192 nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
2196 * FIXME: nsIScriptObjectPrincipal uses thiscall calling convention, so we need this hack on i386.
2197 * This will be removed after the next Gecko update, that will change calling convention on Gecko side.
2199 #ifdef __i386__
2200 extern void *call_thiscall_func;
2201 __ASM_GLOBAL_FUNC(call_thiscall_func,
2202 "popl %eax\n\t"
2203 "popl %edx\n\t"
2204 "popl %ecx\n\t"
2205 "pushl %eax\n\t"
2206 "jmp *%edx\n\t")
2207 #define nsIScriptObjectPrincipal_GetPrincipal(this) ((void* (WINAPI*)(void*,void*))&call_thiscall_func)((this)->lpVtbl->GetPrincipal,this)
2208 #endif
2210 nsIXMLHttpRequest *create_nsxhr(nsIDOMWindow *nswindow)
2212 nsIScriptObjectPrincipal *sop;
2213 mozIDOMWindow *inner_window;
2214 nsIPrincipal *nspri;
2215 nsIGlobalObject *nsglo;
2216 nsIXMLHttpRequest *nsxhr;
2217 nsresult nsres;
2219 nsres = nsIDOMWindow_GetInnerWindow(nswindow, &inner_window);
2220 if(NS_FAILED(nsres)) {
2221 ERR("Could not get inner window: %08x\n", nsres);
2222 return NULL;
2225 nsres = mozIDOMWindow_QueryInterface(inner_window, &IID_nsIGlobalObject, (void **)&nsglo);
2226 mozIDOMWindow_Release(inner_window);
2227 assert(nsres == NS_OK);
2229 nsres = nsIGlobalObject_QueryInterface(nsglo, &IID_nsIScriptObjectPrincipal, (void**)&sop);
2230 assert(nsres == NS_OK);
2232 nspri = nsIScriptObjectPrincipal_GetPrincipal(sop);
2233 nsIScriptObjectPrincipal_Release(sop);
2235 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
2236 NS_XMLHTTPREQUEST_CONTRACTID, NULL, &IID_nsIXMLHttpRequest,
2237 (void**)&nsxhr);
2238 if(NS_SUCCEEDED(nsres)) {
2239 nsres = nsIXMLHttpRequest_Init(nsxhr, nspri, NULL, nsglo, NULL, NULL);
2240 if(NS_FAILED(nsres))
2241 nsIXMLHttpRequest_Release(nsxhr);
2243 nsISupports_Release(nspri);
2244 nsIGlobalObject_Release(nsglo);
2245 if(NS_FAILED(nsres)) {
2246 ERR("nsIXMLHttpRequest_Init failed: %08x\n", nsres);
2247 return NULL;
2250 return nsxhr;