user32: Remove _wassert workaround.
[wine.git] / dlls / mshtml / nsembed.c
blob32ae792625c8ebf31c6368c6cc12672bdc91a821
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 <stdarg.h>
21 #define COBJMACROS
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "winreg.h"
27 #include "ole2.h"
28 #include "shlobj.h"
29 #include "shlwapi.h"
31 #include "wine/asm.h"
32 #include "wine/debug.h"
34 #include "mshtml_private.h"
35 #include "htmlevent.h"
36 #include "binding.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
39 WINE_DECLARE_DEBUG_CHANNEL(gecko);
41 #define NS_APPSTARTUPNOTIFIER_CONTRACTID "@mozilla.org/embedcomp/appstartup-notifier;1"
42 #define NS_WEBBROWSER_CONTRACTID "@mozilla.org/embedding/browser/nsWebBrowser;1"
43 #define NS_COMMANDPARAMS_CONTRACTID "@mozilla.org/embedcomp/command-params;1"
44 #define NS_HTMLSERIALIZER_CONTRACTID "@mozilla.org/layout/contentserializer;1?mimetype=text/html"
45 #define NS_EDITORCONTROLLER_CONTRACTID "@mozilla.org/editor/editorcontroller;1"
46 #define NS_PREFERENCES_CONTRACTID "@mozilla.org/preferences;1"
47 #define NS_VARIANT_CONTRACTID "@mozilla.org/variant;1"
48 #define NS_CATEGORYMANAGER_CONTRACTID "@mozilla.org/categorymanager;1"
49 #define NS_XMLHTTPREQUEST_CONTRACTID "@mozilla.org/xmlextras/xmlhttprequest;1"
51 #define PR_UINT32_MAX 0xffffffff
53 #define NS_STRING_CONTAINER_INIT_DEPEND 0x0002
54 #define NS_CSTRING_CONTAINER_INIT_DEPEND 0x0002
56 #ifdef __i386__
57 #define GECKO_ARCH_STRING "x86"
58 #elif defined(__x86_64__)
59 #define GECKO_ARCH_STRING "x86_64"
60 #else
61 #define GECKO_ARCH_STRING ""
62 #endif
64 #define GECKO_DIR_NAME "wine-gecko-" GECKO_VERSION "-" GECKO_ARCH_STRING
66 typedef UINT32 PRUint32;
68 static nsresult (CDECL *NS_InitXPCOM2)(nsIServiceManager**,void*,void*);
69 static nsresult (CDECL *NS_ShutdownXPCOM)(nsIServiceManager*);
70 static nsresult (CDECL *NS_GetComponentRegistrar)(nsIComponentRegistrar**);
71 static nsresult (CDECL *NS_StringContainerInit2)(nsStringContainer*,const PRUnichar*,PRUint32,PRUint32);
72 static nsresult (CDECL *NS_CStringContainerInit2)(nsCStringContainer*,const char*,PRUint32,PRUint32);
73 static nsresult (CDECL *NS_StringContainerFinish)(nsStringContainer*);
74 static nsresult (CDECL *NS_CStringContainerFinish)(nsCStringContainer*);
75 static nsresult (CDECL *NS_StringSetData)(nsAString*,const PRUnichar*,PRUint32);
76 static nsresult (CDECL *NS_CStringSetData)(nsACString*,const char*,PRUint32);
77 static nsresult (CDECL *NS_NewLocalFile)(const nsAString*,cpp_bool,nsIFile**);
78 static PRUint32 (CDECL *NS_StringGetData)(const nsAString*,const PRUnichar **,cpp_bool*);
79 static PRUint32 (CDECL *NS_CStringGetData)(const nsACString*,const char**,cpp_bool*);
80 static cpp_bool (CDECL *NS_StringGetIsVoid)(const nsAString*);
81 static void* (CDECL *NS_Alloc)(SIZE_T);
82 static void (CDECL *NS_Free)(void*);
84 static HINSTANCE xul_handle = NULL;
86 static nsIServiceManager *pServMgr = NULL;
87 static nsIComponentManager *pCompMgr = NULL;
88 static nsICategoryManager *cat_mgr;
89 static nsIFile *profile_directory, *plugin_directory;
91 static ATOM browser_class;
92 static WCHAR gecko_path[MAX_PATH];
93 static unsigned gecko_path_len;
95 nsresult create_nsfile(const PRUnichar *path, nsIFile **ret)
97 nsAString str;
98 nsresult nsres;
100 nsAString_InitDepend(&str, path);
101 nsres = NS_NewLocalFile(&str, FALSE, ret);
102 nsAString_Finish(&str);
104 if(NS_FAILED(nsres))
105 WARN("NS_NewLocalFile failed: %08x\n", nsres);
106 return nsres;
109 typedef struct {
110 nsISimpleEnumerator nsISimpleEnumerator_iface;
111 LONG ref;
112 nsISupports *value;
113 } nsSingletonEnumerator;
115 static inline nsSingletonEnumerator *impl_from_nsISimpleEnumerator(nsISimpleEnumerator *iface)
117 return CONTAINING_RECORD(iface, nsSingletonEnumerator, nsISimpleEnumerator_iface);
120 static nsresult NSAPI nsSingletonEnumerator_QueryInterface(nsISimpleEnumerator *iface, nsIIDRef riid, void **ppv)
122 nsSingletonEnumerator *This = impl_from_nsISimpleEnumerator(iface);
124 if(IsEqualGUID(&IID_nsISupports, riid)) {
125 TRACE("(%p)->(IID_nsISupports %p)\n", This, ppv);
126 *ppv = &This->nsISimpleEnumerator_iface;
127 }else if(IsEqualGUID(&IID_nsISimpleEnumerator, riid)) {
128 TRACE("(%p)->(IID_nsISimpleEnumerator %p)\n", This, ppv);
129 *ppv = &This->nsISimpleEnumerator_iface;
130 }else {
131 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
132 *ppv = NULL;
133 return NS_NOINTERFACE;
136 nsISupports_AddRef((nsISupports*)*ppv);
137 return NS_OK;
140 static nsrefcnt NSAPI nsSingletonEnumerator_AddRef(nsISimpleEnumerator *iface)
142 nsSingletonEnumerator *This = impl_from_nsISimpleEnumerator(iface);
143 nsrefcnt ref = InterlockedIncrement(&This->ref);
145 TRACE("(%p) ref=%d\n", This, ref);
147 return ref;
150 static nsrefcnt NSAPI nsSingletonEnumerator_Release(nsISimpleEnumerator *iface)
152 nsSingletonEnumerator *This = impl_from_nsISimpleEnumerator(iface);
153 nsrefcnt ref = InterlockedDecrement(&This->ref);
155 TRACE("(%p) ref=%d\n", This, ref);
157 if(!ref) {
158 if(This->value)
159 nsISupports_Release(This->value);
160 heap_free(This);
163 return ref;
166 static nsresult NSAPI nsSingletonEnumerator_HasMoreElements(nsISimpleEnumerator *iface, cpp_bool *_retval)
168 nsSingletonEnumerator *This = impl_from_nsISimpleEnumerator(iface);
170 TRACE("(%p)->()\n", This);
172 *_retval = This->value != NULL;
173 return NS_OK;
176 static nsresult NSAPI nsSingletonEnumerator_GetNext(nsISimpleEnumerator *iface, nsISupports **_retval)
178 nsSingletonEnumerator *This = impl_from_nsISimpleEnumerator(iface);
180 TRACE("(%p)->()\n", This);
182 if(!This->value)
183 return NS_ERROR_UNEXPECTED;
185 *_retval = This->value;
186 This->value = NULL;
187 return NS_OK;
190 static const nsISimpleEnumeratorVtbl nsSingletonEnumeratorVtbl = {
191 nsSingletonEnumerator_QueryInterface,
192 nsSingletonEnumerator_AddRef,
193 nsSingletonEnumerator_Release,
194 nsSingletonEnumerator_HasMoreElements,
195 nsSingletonEnumerator_GetNext
198 static nsISimpleEnumerator *create_singleton_enumerator(nsISupports *value)
200 nsSingletonEnumerator *ret;
202 ret = heap_alloc(sizeof(*ret));
203 if(!ret)
204 return NULL;
206 ret->nsISimpleEnumerator_iface.lpVtbl = &nsSingletonEnumeratorVtbl;
207 ret->ref = 1;
209 if(value)
210 nsISupports_AddRef(value);
211 ret->value = value;
212 return &ret->nsISimpleEnumerator_iface;
215 static nsresult NSAPI nsDirectoryServiceProvider2_QueryInterface(nsIDirectoryServiceProvider2 *iface,
216 nsIIDRef riid, void **result)
218 if(IsEqualGUID(&IID_nsISupports, riid)) {
219 TRACE("(IID_nsISupports %p)\n", result);
220 *result = iface;
221 }else if(IsEqualGUID(&IID_nsIDirectoryServiceProvider, riid)) {
222 TRACE("(IID_nsIDirectoryServiceProvider %p)\n", result);
223 *result = iface;
224 }else if(IsEqualGUID(&IID_nsIDirectoryServiceProvider2, riid)) {
225 TRACE("(IID_nsIDirectoryServiceProvider2 %p)\n", result);
226 *result = iface;
227 }else {
228 WARN("(%s %p)\n", debugstr_guid(riid), result);
229 *result = NULL;
230 return NS_NOINTERFACE;
233 nsISupports_AddRef((nsISupports*)*result);
234 return NS_OK;
237 static nsrefcnt NSAPI nsDirectoryServiceProvider2_AddRef(nsIDirectoryServiceProvider2 *iface)
239 return 2;
242 static nsrefcnt NSAPI nsDirectoryServiceProvider2_Release(nsIDirectoryServiceProvider2 *iface)
244 return 1;
247 static nsresult create_profile_directory(void)
249 WCHAR path[MAX_PATH + ARRAY_SIZE(L"\\wine_gecko")];
250 cpp_bool exists;
251 nsresult nsres;
252 HRESULT hres;
254 hres = SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, path);
255 if(FAILED(hres)) {
256 ERR("SHGetFolderPath failed: %08x\n", hres);
257 return NS_ERROR_FAILURE;
260 lstrcatW(path, L"\\wine_gecko");
261 nsres = create_nsfile(path, &profile_directory);
262 if(NS_FAILED(nsres))
263 return nsres;
265 nsres = nsIFile_Exists(profile_directory, &exists);
266 if(NS_FAILED(nsres)) {
267 ERR("Exists failed: %08x\n", nsres);
268 return nsres;
271 if(!exists) {
272 nsres = nsIFile_Create(profile_directory, 1, 0700);
273 if(NS_FAILED(nsres))
274 ERR("Create failed: %08x\n", nsres);
277 return nsres;
280 static nsresult NSAPI nsDirectoryServiceProvider2_GetFile(nsIDirectoryServiceProvider2 *iface,
281 const char *prop, cpp_bool *persistent, nsIFile **_retval)
283 TRACE("(%s %p %p)\n", debugstr_a(prop), persistent, _retval);
285 if(!strcmp(prop, "ProfD")) {
286 if(!profile_directory) {
287 nsresult nsres;
289 nsres = create_profile_directory();
290 if(NS_FAILED(nsres))
291 return nsres;
294 assert(profile_directory != NULL);
295 return nsIFile_Clone(profile_directory, _retval);
298 *_retval = NULL;
299 return NS_ERROR_FAILURE;
302 static nsresult NSAPI nsDirectoryServiceProvider2_GetFiles(nsIDirectoryServiceProvider2 *iface,
303 const char *prop, nsISimpleEnumerator **_retval)
305 TRACE("(%s %p)\n", debugstr_a(prop), _retval);
307 if(!strcmp(prop, "APluginsDL")) {
308 WCHAR plugin_path[MAX_PATH];
309 nsIFile *file;
310 int len;
311 nsresult nsres;
313 if(!plugin_directory) {
314 len = GetSystemDirectoryW(plugin_path, ARRAY_SIZE(plugin_path)-ARRAY_SIZE(L"\\gecko\\plugin")+1);
315 if(!len)
316 return NS_ERROR_UNEXPECTED;
318 lstrcpyW(plugin_path+len, L"\\gecko\\plugin");
319 nsres = create_nsfile(plugin_path, &plugin_directory);
320 if(NS_FAILED(nsres)) {
321 *_retval = NULL;
322 return nsres;
326 nsres = nsIFile_Clone(plugin_directory, &file);
327 if(NS_FAILED(nsres))
328 return nsres;
330 *_retval = create_singleton_enumerator((nsISupports*)file);
331 nsIFile_Release(file);
332 if(!*_retval)
333 return NS_ERROR_OUT_OF_MEMORY;
335 return NS_OK;
338 *_retval = NULL;
339 return NS_ERROR_FAILURE;
342 static const nsIDirectoryServiceProvider2Vtbl nsDirectoryServiceProvider2Vtbl = {
343 nsDirectoryServiceProvider2_QueryInterface,
344 nsDirectoryServiceProvider2_AddRef,
345 nsDirectoryServiceProvider2_Release,
346 nsDirectoryServiceProvider2_GetFile,
347 nsDirectoryServiceProvider2_GetFiles
350 static nsIDirectoryServiceProvider2 nsDirectoryServiceProvider2 =
351 { &nsDirectoryServiceProvider2Vtbl };
353 static LRESULT WINAPI nsembed_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
355 GeckoBrowser *This;
356 nsresult nsres;
358 if(msg == WM_CREATE) {
359 This = *(GeckoBrowser**)lParam;
360 SetPropW(hwnd, L"THIS", This);
361 }else {
362 This = GetPropW(hwnd, L"THIS");
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_browser_class(void)
391 static WNDCLASSEXW wndclass = {
392 sizeof(WNDCLASSEXW),
393 CS_DBLCLKS,
394 nsembed_proc,
395 0, 0, NULL, NULL, NULL, NULL, NULL,
396 L"NsContainer",
397 NULL,
399 wndclass.hInstance = hInst;
400 browser_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[] = L"\\control.exe";
413 static const WCHAR argsW[] = L" appwiz.cpl install_gecko";
415 len = GetSystemDirectoryW(app, MAX_PATH-ARRAY_SIZE(controlW));
416 memcpy(app+len, controlW, sizeof(controlW));
418 args = heap_alloc(len*sizeof(WCHAR) + sizeof(controlW) + sizeof(argsW));
419 if(!args)
420 return FALSE;
422 memcpy(args, app, len*sizeof(WCHAR) + sizeof(controlW));
423 memcpy(args + len + ARRAY_SIZE(controlW)-1, argsW, sizeof(argsW));
425 TRACE("starting %s\n", debugstr_w(args));
427 memset(&si, 0, sizeof(si));
428 si.cb = sizeof(si);
429 ret = CreateProcessW(app, args, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
430 heap_free(args);
431 if (ret) {
432 CloseHandle(pi.hThread);
433 WaitForSingleObject(pi.hProcess, INFINITE);
434 CloseHandle(pi.hProcess);
435 }else {
436 WARN("installation failed\n");
439 return ret;
442 static void set_environment(LPCWSTR gre_path)
444 size_t len, gre_path_len;
445 int debug_level = 0;
446 WCHAR *path, buf[20];
447 const WCHAR *ptr;
449 SetEnvironmentVariableW(L"XPCOM_DEBUG_BREAK", L"warn");
451 if(TRACE_ON(gecko))
452 debug_level = 5;
453 else if(WARN_ON(gecko))
454 debug_level = 2;
455 else if(ERR_ON(gecko))
456 debug_level = 1;
458 swprintf(buf, ARRAY_SIZE(buf), L"all:%d", debug_level);
459 SetEnvironmentVariableW(L"NSPR_LOG_MODULES", buf);
461 len = GetEnvironmentVariableW(L"PATH", NULL, 0);
462 gre_path_len = lstrlenW(gre_path);
463 path = heap_alloc((len+gre_path_len+1)*sizeof(WCHAR));
464 if(!path)
465 return;
466 GetEnvironmentVariableW(L"PATH", path, len);
468 /* We have to modify PATH as xul.dll loads other DLLs from this directory. */
469 if(!(ptr = wcsstr(path, gre_path))
470 || (ptr > path && *(ptr-1) != ';')
471 || (ptr[gre_path_len] && ptr[gre_path_len] != ';')) {
472 if(len)
473 path[len-1] = ';';
474 lstrcpyW(path+len, gre_path);
475 SetEnvironmentVariableW(L"PATH", path);
477 heap_free(path);
480 static void set_bool_pref(nsIPrefBranch *pref, const char *pref_name, BOOL val)
482 nsresult nsres;
484 nsres = nsIPrefBranch_SetBoolPref(pref, pref_name, val);
485 if(NS_FAILED(nsres))
486 ERR("Could not set pref %s\n", debugstr_a(pref_name));
489 static void set_int_pref(nsIPrefBranch *pref, const char *pref_name, int val)
491 nsresult nsres;
493 nsres = nsIPrefBranch_SetIntPref(pref, pref_name, val);
494 if(NS_FAILED(nsres))
495 ERR("Could not set pref %s\n", debugstr_a(pref_name));
498 static void set_string_pref(nsIPrefBranch *pref, const char *pref_name, const char *val)
500 nsresult nsres;
502 nsres = nsIPrefBranch_SetCharPref(pref, pref_name, val);
503 if(NS_FAILED(nsres))
504 ERR("Could not set pref %s\n", debugstr_a(pref_name));
507 static void set_lang(nsIPrefBranch *pref)
509 char langs[100];
510 DWORD res, size, type;
511 HKEY hkey;
513 res = RegOpenKeyW(HKEY_CURRENT_USER, L"Software\\Microsoft\\Internet Explorer\\International", &hkey);
514 if(res != ERROR_SUCCESS)
515 return;
517 size = sizeof(langs);
518 res = RegQueryValueExA(hkey, "AcceptLanguage", 0, &type, (LPBYTE)langs, &size);
519 RegCloseKey(hkey);
520 if(res != ERROR_SUCCESS || type != REG_SZ)
521 return;
523 TRACE("Setting lang %s\n", debugstr_a(langs));
525 set_string_pref(pref, "intl.accept_languages", langs);
528 static void set_preferences(void)
530 nsIPrefBranch *pref;
531 nsresult nsres;
533 nsres = nsIServiceManager_GetServiceByContractID(pServMgr, NS_PREFERENCES_CONTRACTID,
534 &IID_nsIPrefBranch, (void**)&pref);
535 if(NS_FAILED(nsres)) {
536 ERR("Could not get preference service: %08x\n", nsres);
537 return;
540 set_lang(pref);
541 set_bool_pref(pref, "security.warn_entering_secure", FALSE);
542 set_bool_pref(pref, "security.warn_submit_insecure", FALSE);
543 set_int_pref(pref, "layout.spellcheckDefault", 0);
545 nsIPrefBranch_Release(pref);
548 static BOOL init_xpcom(const PRUnichar *gre_path)
550 nsIComponentRegistrar *registrar = NULL;
551 nsIFile *gre_dir;
552 WCHAR *ptr;
553 nsresult nsres;
555 nsres = create_nsfile(gre_path, &gre_dir);
556 if(NS_FAILED(nsres)) {
557 FreeLibrary(xul_handle);
558 return FALSE;
561 nsres = NS_InitXPCOM2(&pServMgr, gre_dir, (nsIDirectoryServiceProvider*)&nsDirectoryServiceProvider2);
562 if(NS_FAILED(nsres)) {
563 ERR("NS_InitXPCOM2 failed: %08x\n", nsres);
564 FreeLibrary(xul_handle);
565 return FALSE;
568 lstrcpyW(gecko_path, gre_path);
569 for(ptr = gecko_path; *ptr; ptr++) {
570 if(*ptr == '\\')
571 *ptr = '/';
573 gecko_path_len = ptr-gecko_path;
575 nsres = nsIServiceManager_QueryInterface(pServMgr, &IID_nsIComponentManager, (void**)&pCompMgr);
576 if(NS_FAILED(nsres))
577 ERR("Could not get nsIComponentManager: %08x\n", nsres);
579 init_nsio(pCompMgr);
580 init_mutation(pCompMgr);
581 set_preferences();
583 nsres = nsIServiceManager_GetServiceByContractID(pServMgr, NS_CATEGORYMANAGER_CONTRACTID,
584 &IID_nsICategoryManager, (void**)&cat_mgr);
585 if(NS_FAILED(nsres))
586 ERR("Could not get category manager service: %08x\n", nsres);
588 nsres = NS_GetComponentRegistrar(&registrar);
589 if(NS_SUCCEEDED(nsres)) {
590 register_nsservice(registrar, pServMgr);
591 nsIComponentRegistrar_Release(registrar);
592 }else {
593 ERR("NS_GetComponentRegistrar failed: %08x\n", nsres);
596 init_node_cc();
598 return TRUE;
601 static BOOL load_xul(WCHAR *gecko_path)
603 size_t len;
605 set_environment(gecko_path);
607 TRACE("(%s)\n", debugstr_w(gecko_path));
609 len = wcslen(gecko_path);
610 wcscpy(gecko_path + len, L"\\xul.dll");
611 xul_handle = LoadLibraryExW(gecko_path, 0, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
612 gecko_path[len] = 0;
613 if(!xul_handle) {
614 WARN("Could not load XUL: %d\n", GetLastError());
615 return FALSE;
618 #define NS_DLSYM(func) \
619 func = (void *)GetProcAddress(xul_handle, #func); \
620 if(!func) \
621 ERR("Could not GetProcAddress(" #func ") failed\n")
623 NS_DLSYM(NS_InitXPCOM2);
624 NS_DLSYM(NS_ShutdownXPCOM);
625 NS_DLSYM(NS_GetComponentRegistrar);
626 NS_DLSYM(NS_StringContainerInit2);
627 NS_DLSYM(NS_CStringContainerInit2);
628 NS_DLSYM(NS_StringContainerFinish);
629 NS_DLSYM(NS_CStringContainerFinish);
630 NS_DLSYM(NS_StringSetData);
631 NS_DLSYM(NS_CStringSetData);
632 NS_DLSYM(NS_NewLocalFile);
633 NS_DLSYM(NS_StringGetData);
634 NS_DLSYM(NS_CStringGetData);
635 NS_DLSYM(NS_StringGetIsVoid);
636 NS_DLSYM(NS_Alloc);
637 NS_DLSYM(NS_Free);
638 NS_DLSYM(ccref_incr);
639 NS_DLSYM(ccref_decr);
640 NS_DLSYM(ccref_init);
641 NS_DLSYM(ccp_init);
642 NS_DLSYM(describe_cc_node);
643 NS_DLSYM(note_cc_edge);
645 #undef NS_DLSYM
647 return init_xpcom(gecko_path);
650 static WCHAR *check_version(const WCHAR *path)
652 WCHAR *file_name;
653 char version[128];
654 DWORD read=0;
655 size_t len;
656 HANDLE hfile;
658 if(!wcsncmp(path, L"\\??\\", 4))
659 path += 4;
660 if(path[1] != ':') {
661 TRACE("Skipping %s\n", debugstr_w(path));
662 return FALSE; /* Gecko needs to be accessible via dos path */
665 len = wcslen(path);
666 file_name = heap_alloc((len + 12) * sizeof(WCHAR));
667 if(!file_name)
668 return NULL;
670 PathCanonicalizeW(file_name, path);
671 len = lstrlenW(file_name);
672 wcscpy(file_name + len, L"\\VERSION");
674 hfile = CreateFileW(file_name, GENERIC_READ, FILE_SHARE_READ, NULL,
675 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
676 file_name[len] = 0;
677 if(hfile == INVALID_HANDLE_VALUE) {
678 TRACE("%s not found\n", debugstr_w(file_name));
679 heap_free(file_name);
680 return NULL;
683 ReadFile(hfile, version, sizeof(version), &read, NULL);
684 version[read] = 0;
685 CloseHandle(hfile);
687 TRACE("%s: %s\n", debugstr_w(file_name), debugstr_a(version));
689 if(strcmp(version, GECKO_VERSION_STRING)) {
690 ERR("Unexpected version %s, expected \"%s\"\n", debugstr_a(version),
691 GECKO_VERSION_STRING);
692 heap_free(file_name);
693 return NULL;
696 return file_name;
699 static WCHAR *find_wine_gecko_reg(void)
701 WCHAR buffer[MAX_PATH];
702 DWORD res, type, size;
703 HKEY hkey;
705 /* @@ Wine registry key: HKLM\Software\Wine\MSHTML\<version> */
706 res = RegOpenKeyW(HKEY_LOCAL_MACHINE, L"Software\\Wine\\MSHTML\\" GECKO_VERSION, &hkey);
707 if(res != ERROR_SUCCESS)
708 return NULL;
710 size = sizeof(buffer);
711 res = RegQueryValueExW(hkey, L"GeckoPath", NULL, &type, (LPBYTE)buffer, &size);
712 RegCloseKey(hkey);
713 if(res != ERROR_SUCCESS || type != REG_SZ)
714 return FALSE;
716 return check_version(buffer);
719 static WCHAR *heap_strcat(const WCHAR *str1, const WCHAR *str2)
721 size_t len1 = lstrlenW(str1);
722 size_t len2 = lstrlenW(str2);
723 WCHAR *ret = heap_alloc((len1 + len2 + 1) * sizeof(WCHAR));
724 if(!ret) return NULL;
725 memcpy(ret, str1, len1 * sizeof(WCHAR));
726 memcpy(ret + len1, str2, len2 * sizeof(WCHAR));
727 ret[len1 + len2] = 0;
728 return ret;
731 static WCHAR *find_wine_gecko_datadir(void)
733 const WCHAR *data_dir;
734 WCHAR *path = NULL, *ret;
736 if((data_dir = _wgetenv(L"WINEDATADIR")))
737 path = heap_strcat(data_dir, L"\\gecko\\" GECKO_DIR_NAME);
738 else if((data_dir = _wgetenv(L"WINEBUILDDIR")))
739 path = heap_strcat(data_dir, L"\\..\\gecko\\" GECKO_DIR_NAME);
740 if(!path)
741 return NULL;
743 ret = check_version(path);
744 heap_free(path);
745 return ret;
748 static WCHAR *find_wine_gecko_unix(const char *unix_path)
750 static WCHAR * (CDECL *p_wine_get_dos_file_name)(const char*);
751 WCHAR *dos_dir, *ret;
753 if(!p_wine_get_dos_file_name) {
754 p_wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleA("kernel32"), "wine_get_dos_file_name");
755 if(!p_wine_get_dos_file_name)
756 return FALSE;
759 dos_dir = p_wine_get_dos_file_name(unix_path);
760 if(!dos_dir)
761 return FALSE;
763 ret = check_version(dos_dir);
765 heap_free(dos_dir);
766 return ret;
769 static CRITICAL_SECTION cs_load_gecko;
770 static CRITICAL_SECTION_DEBUG cs_load_gecko_dbg =
772 0, 0, &cs_load_gecko,
773 { &cs_load_gecko_dbg.ProcessLocksList, &cs_load_gecko_dbg.ProcessLocksList },
774 0, 0, { (DWORD_PTR)(__FILE__ ": load_gecko") }
776 static CRITICAL_SECTION cs_load_gecko = { &cs_load_gecko_dbg, -1, 0, 0, 0, 0 };
778 BOOL load_gecko(void)
780 BOOL ret = FALSE;
782 static DWORD loading_thread;
784 TRACE("()\n");
786 if(!GECKO_ARCH_STRING[0]) {
787 FIXME("Wine Gecko is not supported on this architecture.\n");
788 return FALSE;
791 /* load_gecko may be called recursively */
792 if(loading_thread == GetCurrentThreadId())
793 return pCompMgr != NULL;
795 EnterCriticalSection(&cs_load_gecko);
797 if(!loading_thread) {
798 WCHAR *gecko_path;
800 loading_thread = GetCurrentThreadId();
802 if(!(gecko_path = find_wine_gecko_reg())
803 && !(gecko_path = find_wine_gecko_datadir())
804 && !(gecko_path = find_wine_gecko_unix(INSTALL_DATADIR "/wine/gecko/" GECKO_DIR_NAME))
805 && (!strcmp(INSTALL_DATADIR, "/usr/share") ||
806 !(gecko_path = find_wine_gecko_unix("/usr/share/wine/gecko/" GECKO_DIR_NAME)))
807 && !(gecko_path = find_wine_gecko_unix("/opt/wine/gecko/" GECKO_DIR_NAME))
808 && install_wine_gecko())
809 gecko_path = find_wine_gecko_reg();
811 if(gecko_path) {
812 ret = load_xul(gecko_path);
813 heap_free(gecko_path);
814 }else {
815 MESSAGE("Could not find Wine Gecko. HTML rendering will be disabled.\n");
817 }else {
818 ret = pCompMgr != NULL;
821 LeaveCriticalSection(&cs_load_gecko);
823 return ret;
826 void *nsalloc(size_t size)
828 return NS_Alloc(size);
831 void nsfree(void *mem)
833 NS_Free(mem);
836 BOOL nsACString_Init(nsACString *str, const char *data)
838 return NS_SUCCEEDED(NS_CStringContainerInit2(str, data, PR_UINT32_MAX, 0));
842 * Initializes nsACString with data owned by caller.
843 * Caller must ensure that data is valid during lifetime of string object.
845 void nsACString_InitDepend(nsACString *str, const char *data)
847 NS_CStringContainerInit2(str, data, PR_UINT32_MAX, NS_CSTRING_CONTAINER_INIT_DEPEND);
850 void nsACString_SetData(nsACString *str, const char *data)
852 NS_CStringSetData(str, data, PR_UINT32_MAX);
855 UINT32 nsACString_GetData(const nsACString *str, const char **data)
857 return NS_CStringGetData(str, data, NULL);
860 void nsACString_Finish(nsACString *str)
862 NS_CStringContainerFinish(str);
865 BOOL nsAString_Init(nsAString *str, const PRUnichar *data)
867 return NS_SUCCEEDED(NS_StringContainerInit2(str, data, PR_UINT32_MAX, 0));
871 * Initializes nsAString with data owned by caller.
872 * Caller must ensure that data is valid during lifetime of string object.
874 void nsAString_InitDepend(nsAString *str, const PRUnichar *data)
876 NS_StringContainerInit2(str, data, PR_UINT32_MAX, NS_STRING_CONTAINER_INIT_DEPEND);
879 UINT32 nsAString_GetData(const nsAString *str, const PRUnichar **data)
881 return NS_StringGetData(str, data, NULL);
884 void nsAString_SetData(nsAString *str, const PRUnichar *data)
886 NS_StringSetData(str, data, PR_UINT32_MAX);
889 void nsAString_Finish(nsAString *str)
891 NS_StringContainerFinish(str);
894 HRESULT map_nsresult(nsresult nsres)
896 switch(nsres) {
897 case NS_OK:
898 return S_OK;
899 case NS_ERROR_OUT_OF_MEMORY:
900 return E_OUTOFMEMORY;
901 case NS_ERROR_NOT_IMPLEMENTED:
902 return E_NOTIMPL;
903 case NS_NOINTERFACE:
904 return E_NOINTERFACE;
905 case NS_ERROR_INVALID_POINTER:
906 return E_POINTER;
907 case NS_ERROR_INVALID_ARG:
908 return E_INVALIDARG;
909 case NS_ERROR_UNEXPECTED:
910 return E_UNEXPECTED;
911 case NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR:
912 return 0x80700007; /* according to tests */
914 return E_FAIL;
917 HRESULT return_nsstr(nsresult nsres, nsAString *nsstr, BSTR *p)
919 const PRUnichar *str;
920 HRESULT hres = S_OK;
922 if(NS_FAILED(nsres)) {
923 WARN("failed: %08x\n", nsres);
924 nsAString_Finish(nsstr);
925 return map_nsresult(nsres);
928 nsAString_GetData(nsstr, &str);
929 TRACE("ret %s\n", debugstr_w(str));
930 if(*str) {
931 *p = SysAllocString(str);
932 if(!*p)
933 hres = E_OUTOFMEMORY;
934 }else {
935 *p = NULL;
938 nsAString_Finish(nsstr);
939 return hres;
942 HRESULT return_nsstr_variant(nsresult nsres, nsAString *nsstr, unsigned flags, VARIANT *p)
944 HRESULT hres = S_OK;
946 if(NS_FAILED(nsres)) {
947 ERR("failed: %08x\n", nsres);
948 nsAString_Finish(nsstr);
949 return map_nsresult(nsres);
952 if(NS_StringGetIsVoid(nsstr)) {
953 V_VT(p) = VT_NULL;
954 }else {
955 const WCHAR *str;
956 size_t len;
957 nsAString_GetData(nsstr, &str);
958 len = wcslen(str);
959 if(flags & NSSTR_IMPLICIT_PX) {
960 const WCHAR *iter;
961 if(len > 2 && !wcscmp(str + len - 2, L"px"))
962 len -= 2;
963 for(iter = str; iter < str + len && is_digit(*iter); iter++);
964 if(*iter == '.') {
965 const WCHAR *dot = iter++;
966 while(iter < str + len && is_digit(*iter)) iter++;
967 if(iter == str + len && dot) len = dot - str;
970 if(flags & NSSTR_COLOR) {
971 hres = nscolor_to_str(str, &V_BSTR(p));
972 }else if(*str) {
973 V_BSTR(p) = SysAllocStringLen(str, len);
974 if(!V_BSTR(p))
975 hres = E_OUTOFMEMORY;
976 }else {
977 V_BSTR(p) = NULL;
979 if(SUCCEEDED(hres))
980 V_VT(p) = VT_BSTR;
983 nsAString_Finish(nsstr);
984 if(FAILED(hres))
985 return hres;
986 TRACE("ret %s\n", debugstr_variant(p));
987 return S_OK;
991 * Converts VARIANT to string and stores the result in nsAString. To avoid useless
992 * allocations, the function uses an existing string if available, so caller must
993 * ensure that passed VARIANT is unchanged as long as its string representation is used
995 HRESULT variant_to_nsstr(VARIANT *v, BOOL hex_int, nsAString *nsstr)
997 WCHAR buf[32];
999 switch(V_VT(v)) {
1000 case VT_NULL:
1001 nsAString_InitDepend(nsstr, NULL);
1002 return S_OK;
1004 case VT_BSTR:
1005 nsAString_InitDepend(nsstr, V_BSTR(v));
1006 break;
1008 case VT_BSTR|VT_BYREF:
1009 nsAString_InitDepend(nsstr, *V_BSTRREF(v));
1010 break;
1012 case VT_I4:
1013 wsprintfW(buf, hex_int ? L"#%06x" : L"%d", V_I4(v));
1014 nsAString_Init(nsstr, buf);
1015 break;
1017 case VT_R8:
1018 case VT_DISPATCH: {
1019 VARIANT strv;
1020 HRESULT hres;
1022 V_VT(&strv) = VT_EMPTY;
1023 hres = VariantChangeTypeEx(&strv, v, MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT),
1024 0, VT_BSTR);
1025 if(FAILED(hres))
1026 return hres;
1028 nsAString_Init(nsstr, V_BSTR(&strv));
1029 SysFreeString(V_BSTR(&strv));
1030 break;
1033 default:
1034 FIXME("not implemented for %s\n", debugstr_variant(v));
1035 return E_NOTIMPL;
1038 return S_OK;
1041 nsICommandParams *create_nscommand_params(void)
1043 nsICommandParams *ret = NULL;
1044 nsresult nsres;
1046 if(!pCompMgr)
1047 return NULL;
1049 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
1050 NS_COMMANDPARAMS_CONTRACTID, NULL, &IID_nsICommandParams,
1051 (void**)&ret);
1052 if(NS_FAILED(nsres))
1053 ERR("Could not get nsICommandParams\n");
1055 return ret;
1058 nsIWritableVariant *create_nsvariant(void)
1060 nsIWritableVariant *ret = NULL;
1061 nsresult nsres;
1063 if(!pCompMgr)
1064 return NULL;
1066 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
1067 NS_VARIANT_CONTRACTID, NULL, &IID_nsIWritableVariant, (void**)&ret);
1068 if(NS_FAILED(nsres))
1069 ERR("Could not get nsIVariant\n");
1071 return ret;
1074 char *get_nscategory_entry(const char *category, const char *entry)
1076 char *ret = NULL;
1077 nsresult nsres;
1079 nsres = nsICategoryManager_GetCategoryEntry(cat_mgr, category, entry, &ret);
1080 return NS_SUCCEEDED(nsres) ? ret : NULL;
1083 nsresult get_nsinterface(nsISupports *iface, REFIID riid, void **ppv)
1085 nsIInterfaceRequestor *iface_req;
1086 nsresult nsres;
1088 nsres = nsISupports_QueryInterface(iface, &IID_nsIInterfaceRequestor, (void**)&iface_req);
1089 if(NS_FAILED(nsres))
1090 return nsres;
1092 nsres = nsIInterfaceRequestor_GetInterface(iface_req, riid, ppv);
1093 nsIInterfaceRequestor_Release(iface_req);
1095 return nsres;
1098 static HRESULT nsnode_to_nsstring_rec(nsIContentSerializer *serializer, nsIDOMNode *nsnode, nsAString *str)
1100 nsIDOMNodeList *node_list = NULL;
1101 cpp_bool has_children = FALSE;
1102 nsIContent *nscontent;
1103 UINT16 type;
1104 nsresult nsres;
1106 nsIDOMNode_HasChildNodes(nsnode, &has_children);
1108 nsres = nsIDOMNode_GetNodeType(nsnode, &type);
1109 if(NS_FAILED(nsres)) {
1110 ERR("GetType failed: %08x\n", nsres);
1111 return E_FAIL;
1114 if(type != DOCUMENT_NODE) {
1115 nsres = nsIDOMNode_QueryInterface(nsnode, &IID_nsIContent, (void**)&nscontent);
1116 if(NS_FAILED(nsres)) {
1117 ERR("Could not get nsIContent interface: %08x\n", nsres);
1118 return E_FAIL;
1122 switch(type) {
1123 case ELEMENT_NODE:
1124 nsIContentSerializer_AppendElementStart(serializer, nscontent, nscontent, str);
1125 break;
1126 case TEXT_NODE:
1127 nsIContentSerializer_AppendText(serializer, nscontent, 0, -1, str);
1128 break;
1129 case COMMENT_NODE:
1130 nsIContentSerializer_AppendComment(serializer, nscontent, 0, -1, str);
1131 break;
1132 case DOCUMENT_NODE: {
1133 nsIDocument *nsdoc;
1134 nsIDOMNode_QueryInterface(nsnode, &IID_nsIDocument, (void**)&nsdoc);
1135 nsIContentSerializer_AppendDocumentStart(serializer, nsdoc, str);
1136 nsIDocument_Release(nsdoc);
1137 break;
1139 case DOCUMENT_TYPE_NODE:
1140 nsIContentSerializer_AppendDoctype(serializer, nscontent, str);
1141 break;
1142 case DOCUMENT_FRAGMENT_NODE:
1143 break;
1144 default:
1145 FIXME("Unhandled type %u\n", type);
1148 if(has_children) {
1149 UINT32 child_cnt, i;
1150 nsIDOMNode *child_node;
1152 nsIDOMNode_GetChildNodes(nsnode, &node_list);
1153 nsIDOMNodeList_GetLength(node_list, &child_cnt);
1155 for(i=0; i<child_cnt; i++) {
1156 nsres = nsIDOMNodeList_Item(node_list, i, &child_node);
1157 if(NS_SUCCEEDED(nsres)) {
1158 nsnode_to_nsstring_rec(serializer, child_node, str);
1159 nsIDOMNode_Release(child_node);
1160 }else {
1161 ERR("Item failed: %08x\n", nsres);
1165 nsIDOMNodeList_Release(node_list);
1168 if(type == ELEMENT_NODE)
1169 nsIContentSerializer_AppendElementEnd(serializer, nscontent, str);
1171 if(type != DOCUMENT_NODE)
1172 nsIContent_Release(nscontent);
1173 return S_OK;
1176 HRESULT nsnode_to_nsstring(nsIDOMNode *nsnode, nsAString *str)
1178 nsIContentSerializer *serializer;
1179 nsresult nsres;
1180 HRESULT hres;
1182 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
1183 NS_HTMLSERIALIZER_CONTRACTID, NULL, &IID_nsIContentSerializer,
1184 (void**)&serializer);
1185 if(NS_FAILED(nsres)) {
1186 ERR("Could not get nsIContentSerializer: %08x\n", nsres);
1187 return E_FAIL;
1190 nsres = nsIContentSerializer_Init(serializer, 0, 100, NULL, FALSE, FALSE /* FIXME */);
1191 if(NS_FAILED(nsres))
1192 ERR("Init failed: %08x\n", nsres);
1194 hres = nsnode_to_nsstring_rec(serializer, nsnode, str);
1195 if(SUCCEEDED(hres)) {
1196 nsres = nsIContentSerializer_Flush(serializer, str);
1197 if(NS_FAILED(nsres))
1198 ERR("Flush failed: %08x\n", nsres);
1201 nsIContentSerializer_Release(serializer);
1202 return hres;
1205 void setup_editor_controller(GeckoBrowser *This)
1207 nsIEditingSession *editing_session = NULL;
1208 nsIControllerContext *ctrlctx;
1209 nsresult nsres;
1211 if(This->editor) {
1212 nsIEditor_Release(This->editor);
1213 This->editor = NULL;
1216 if(This->editor_controller) {
1217 nsIController_Release(This->editor_controller);
1218 This->editor_controller = NULL;
1221 nsres = get_nsinterface((nsISupports*)This->webbrowser, &IID_nsIEditingSession,
1222 (void**)&editing_session);
1223 if(NS_FAILED(nsres)) {
1224 ERR("Could not get nsIEditingSession: %08x\n", nsres);
1225 return;
1228 nsres = nsIEditingSession_GetEditorForWindow(editing_session,
1229 This->doc->basedoc.window->window_proxy, &This->editor);
1230 nsIEditingSession_Release(editing_session);
1231 if(NS_FAILED(nsres)) {
1232 ERR("Could not get editor: %08x\n", nsres);
1233 return;
1236 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
1237 NS_EDITORCONTROLLER_CONTRACTID, NULL, &IID_nsIControllerContext, (void**)&ctrlctx);
1238 if(NS_SUCCEEDED(nsres)) {
1239 nsres = nsIControllerContext_SetCommandContext(ctrlctx, (nsISupports *)This->editor);
1240 if(NS_FAILED(nsres))
1241 ERR("SetCommandContext failed: %08x\n", nsres);
1242 nsres = nsIControllerContext_QueryInterface(ctrlctx, &IID_nsIController,
1243 (void**)&This->editor_controller);
1244 nsIControllerContext_Release(ctrlctx);
1245 if(NS_FAILED(nsres))
1246 ERR("Could not get nsIController interface: %08x\n", nsres);
1247 }else {
1248 ERR("Could not create edit controller: %08x\n", nsres);
1252 void close_gecko(void)
1254 TRACE("()\n");
1256 release_nsio();
1257 init_mutation(NULL);
1259 if(profile_directory) {
1260 nsIFile_Release(profile_directory);
1261 profile_directory = NULL;
1264 if(plugin_directory) {
1265 nsIFile_Release(plugin_directory);
1266 plugin_directory = NULL;
1269 if(pCompMgr)
1270 nsIComponentManager_Release(pCompMgr);
1272 if(pServMgr)
1273 nsIServiceManager_Release(pServMgr);
1275 if(cat_mgr)
1276 nsICategoryManager_Release(cat_mgr);
1278 /* Gecko doesn't really support being unloaded */
1279 /* if (hXPCOM) FreeLibrary(hXPCOM); */
1281 DeleteCriticalSection(&cs_load_gecko);
1284 BOOL is_gecko_path(const char *path)
1286 WCHAR *buf, *ptr;
1287 BOOL ret;
1289 buf = heap_strdupUtoW(path);
1290 if(!buf || lstrlenW(buf) < gecko_path_len)
1291 return FALSE;
1293 for(ptr = buf; *ptr; ptr++) {
1294 if(*ptr == '\\')
1295 *ptr = '/';
1298 UrlUnescapeW(buf, NULL, NULL, URL_UNESCAPE_INPLACE);
1299 buf[gecko_path_len] = 0;
1301 ret = !wcsicmp(buf, gecko_path);
1302 heap_free(buf);
1303 return ret;
1306 void set_viewer_zoom(GeckoBrowser *browser, float factor)
1308 nsIContentViewer *content_viewer;
1309 nsIDocShell *doc_shell;
1310 nsresult nsres;
1312 TRACE("Setting to %f\n", factor);
1314 nsres = get_nsinterface((nsISupports*)browser->navigation, &IID_nsIDocShell, (void**)&doc_shell);
1315 assert(nsres == NS_OK);
1317 nsres = nsIDocShell_GetContentViewer(doc_shell, &content_viewer);
1318 assert(nsres == NS_OK && content_viewer);
1319 nsIDocShell_Release(doc_shell);
1321 nsres = nsIContentViewer_SetFullZoom(content_viewer, factor);
1322 if(NS_FAILED(nsres))
1323 ERR("SetFullZoom failed: %08x\n", nsres);
1325 nsIContentViewer_Release(content_viewer);
1328 float get_viewer_zoom(GeckoBrowser *browser)
1330 nsIContentViewer *content_viewer;
1331 nsIDocShell *doc_shell;
1332 nsresult nsres;
1333 float factor;
1335 nsres = get_nsinterface((nsISupports*)browser->navigation, &IID_nsIDocShell, (void**)&doc_shell);
1336 assert(nsres == NS_OK);
1338 nsres = nsIDocShell_GetContentViewer(doc_shell, &content_viewer);
1339 assert(nsres == NS_OK && content_viewer);
1340 nsIDocShell_Release(doc_shell);
1342 nsres = nsIContentViewer_GetFullZoom(content_viewer, &factor);
1343 if(NS_FAILED(nsres))
1344 ERR("GetFullZoom failed: %08x\n", nsres);
1345 TRACE("Got %f\n", factor);
1347 nsIContentViewer_Release(content_viewer);
1348 return factor;
1351 struct nsWeakReference {
1352 nsIWeakReference nsIWeakReference_iface;
1354 LONG ref;
1356 GeckoBrowser *browser;
1359 static inline nsWeakReference *impl_from_nsIWeakReference(nsIWeakReference *iface)
1361 return CONTAINING_RECORD(iface, nsWeakReference, nsIWeakReference_iface);
1364 static nsresult NSAPI nsWeakReference_QueryInterface(nsIWeakReference *iface,
1365 nsIIDRef riid, void **result)
1367 nsWeakReference *This = impl_from_nsIWeakReference(iface);
1369 if(IsEqualGUID(&IID_nsISupports, riid)) {
1370 TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
1371 *result = &This->nsIWeakReference_iface;
1372 }else if(IsEqualGUID(&IID_nsIWeakReference, riid)) {
1373 TRACE("(%p)->(IID_nsIWeakReference %p)\n", This, result);
1374 *result = &This->nsIWeakReference_iface;
1375 }else {
1376 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
1377 *result = NULL;
1378 return NS_NOINTERFACE;
1381 nsISupports_AddRef((nsISupports*)*result);
1382 return NS_OK;
1385 static nsrefcnt NSAPI nsWeakReference_AddRef(nsIWeakReference *iface)
1387 nsWeakReference *This = impl_from_nsIWeakReference(iface);
1388 LONG ref = InterlockedIncrement(&This->ref);
1390 TRACE("(%p) ref=%d\n", This, ref);
1392 return ref;
1395 static nsrefcnt NSAPI nsWeakReference_Release(nsIWeakReference *iface)
1397 nsWeakReference *This = impl_from_nsIWeakReference(iface);
1398 LONG ref = InterlockedDecrement(&This->ref);
1400 TRACE("(%p) ref=%d\n", This, ref);
1402 if(!ref) {
1403 assert(!This->browser);
1404 heap_free(This);
1407 return ref;
1410 static nsresult NSAPI nsWeakReference_QueryReferent(nsIWeakReference *iface,
1411 const nsIID *riid, void **result)
1413 nsWeakReference *This = impl_from_nsIWeakReference(iface);
1415 if(!This->browser)
1416 return NS_ERROR_NULL_POINTER;
1418 return nsIWebBrowserChrome_QueryInterface(&This->browser->nsIWebBrowserChrome_iface, riid, result);
1421 static const nsIWeakReferenceVtbl nsWeakReferenceVtbl = {
1422 nsWeakReference_QueryInterface,
1423 nsWeakReference_AddRef,
1424 nsWeakReference_Release,
1425 nsWeakReference_QueryReferent
1428 /**********************************************************
1429 * nsIWebBrowserChrome interface
1432 static inline GeckoBrowser *impl_from_nsIWebBrowserChrome(nsIWebBrowserChrome *iface)
1434 return CONTAINING_RECORD(iface, GeckoBrowser, nsIWebBrowserChrome_iface);
1437 static nsresult NSAPI nsWebBrowserChrome_QueryInterface(nsIWebBrowserChrome *iface,
1438 nsIIDRef riid, void **result)
1440 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1442 *result = NULL;
1443 if(IsEqualGUID(&IID_nsISupports, riid)) {
1444 TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
1445 *result = &This->nsIWebBrowserChrome_iface;
1446 }else if(IsEqualGUID(&IID_nsIWebBrowserChrome, riid)) {
1447 TRACE("(%p)->(IID_nsIWebBrowserChrome, %p)\n", This, result);
1448 *result = &This->nsIWebBrowserChrome_iface;
1449 }else if(IsEqualGUID(&IID_nsIContextMenuListener, riid)) {
1450 TRACE("(%p)->(IID_nsIContextMenuListener, %p)\n", This, result);
1451 *result = &This->nsIContextMenuListener_iface;
1452 }else if(IsEqualGUID(&IID_nsIURIContentListener, riid)) {
1453 TRACE("(%p)->(IID_nsIURIContentListener %p)\n", This, result);
1454 *result = &This->nsIURIContentListener_iface;
1455 }else if(IsEqualGUID(&IID_nsIEmbeddingSiteWindow, riid)) {
1456 TRACE("(%p)->(IID_nsIEmbeddingSiteWindow %p)\n", This, result);
1457 *result = &This->nsIEmbeddingSiteWindow_iface;
1458 }else if(IsEqualGUID(&IID_nsITooltipListener, riid)) {
1459 TRACE("(%p)->(IID_nsITooltipListener %p)\n", This, result);
1460 *result = &This->nsITooltipListener_iface;
1461 }else if(IsEqualGUID(&IID_nsIInterfaceRequestor, riid)) {
1462 TRACE("(%p)->(IID_nsIInterfaceRequestor %p)\n", This, result);
1463 *result = &This->nsIInterfaceRequestor_iface;
1464 }else if(IsEqualGUID(&IID_nsISupportsWeakReference, riid)) {
1465 TRACE("(%p)->(IID_nsISupportsWeakReference %p)\n", This, result);
1466 *result = &This->nsISupportsWeakReference_iface;
1469 if(*result) {
1470 nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1471 return NS_OK;
1474 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
1475 return NS_NOINTERFACE;
1478 static nsrefcnt NSAPI nsWebBrowserChrome_AddRef(nsIWebBrowserChrome *iface)
1480 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1481 LONG ref = InterlockedIncrement(&This->ref);
1483 TRACE("(%p) ref=%d\n", This, ref);
1485 return ref;
1488 static nsrefcnt NSAPI nsWebBrowserChrome_Release(nsIWebBrowserChrome *iface)
1490 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1491 LONG ref = InterlockedDecrement(&This->ref);
1493 TRACE("(%p) ref=%d\n", This, ref);
1495 if(!ref) {
1496 if(This->doc)
1497 detach_gecko_browser(This);
1498 if(This->weak_reference) {
1499 This->weak_reference->browser = NULL;
1500 nsIWeakReference_Release(&This->weak_reference->nsIWeakReference_iface);
1502 heap_free(This);
1505 return ref;
1508 static nsresult NSAPI nsWebBrowserChrome_SetStatus(nsIWebBrowserChrome *iface,
1509 UINT32 statusType, const PRUnichar *status)
1511 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1512 TRACE("(%p)->(%d %s)\n", This, statusType, debugstr_w(status));
1513 return NS_OK;
1516 static nsresult NSAPI nsWebBrowserChrome_GetWebBrowser(nsIWebBrowserChrome *iface,
1517 nsIWebBrowser **aWebBrowser)
1519 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1521 TRACE("(%p)->(%p)\n", This, aWebBrowser);
1523 if(!aWebBrowser)
1524 return NS_ERROR_INVALID_ARG;
1526 if(This->webbrowser)
1527 nsIWebBrowser_AddRef(This->webbrowser);
1528 *aWebBrowser = This->webbrowser;
1529 return S_OK;
1532 static nsresult NSAPI nsWebBrowserChrome_SetWebBrowser(nsIWebBrowserChrome *iface,
1533 nsIWebBrowser *aWebBrowser)
1535 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1537 TRACE("(%p)->(%p)\n", This, aWebBrowser);
1539 if(aWebBrowser != This->webbrowser)
1540 ERR("Wrong nsWebBrowser!\n");
1542 return NS_OK;
1545 static nsresult NSAPI nsWebBrowserChrome_GetChromeFlags(nsIWebBrowserChrome *iface,
1546 UINT32 *aChromeFlags)
1548 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1549 WARN("(%p)->(%p)\n", This, aChromeFlags);
1550 return NS_ERROR_NOT_IMPLEMENTED;
1553 static nsresult NSAPI nsWebBrowserChrome_SetChromeFlags(nsIWebBrowserChrome *iface,
1554 UINT32 aChromeFlags)
1556 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1557 WARN("(%p)->(%08x)\n", This, aChromeFlags);
1558 return NS_ERROR_NOT_IMPLEMENTED;
1561 static nsresult NSAPI nsWebBrowserChrome_DestroyBrowserWindow(nsIWebBrowserChrome *iface)
1563 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1564 TRACE("(%p)\n", This);
1565 return NS_ERROR_NOT_IMPLEMENTED;
1568 static nsresult NSAPI nsWebBrowserChrome_SizeBrowserTo(nsIWebBrowserChrome *iface,
1569 LONG aCX, LONG aCY)
1571 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1572 WARN("(%p)->(%d %d)\n", This, aCX, aCY);
1573 return NS_ERROR_NOT_IMPLEMENTED;
1576 static nsresult NSAPI nsWebBrowserChrome_ShowAsModal(nsIWebBrowserChrome *iface)
1578 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1579 WARN("(%p)\n", This);
1580 return NS_ERROR_NOT_IMPLEMENTED;
1583 static nsresult NSAPI nsWebBrowserChrome_IsWindowModal(nsIWebBrowserChrome *iface, cpp_bool *_retval)
1585 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1586 WARN("(%p)->(%p)\n", This, _retval);
1587 return NS_ERROR_NOT_IMPLEMENTED;
1590 static nsresult NSAPI nsWebBrowserChrome_ExitModalEventLoop(nsIWebBrowserChrome *iface,
1591 nsresult aStatus)
1593 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1594 WARN("(%p)->(%08x)\n", This, aStatus);
1595 return NS_ERROR_NOT_IMPLEMENTED;
1598 static const nsIWebBrowserChromeVtbl nsWebBrowserChromeVtbl = {
1599 nsWebBrowserChrome_QueryInterface,
1600 nsWebBrowserChrome_AddRef,
1601 nsWebBrowserChrome_Release,
1602 nsWebBrowserChrome_SetStatus,
1603 nsWebBrowserChrome_GetWebBrowser,
1604 nsWebBrowserChrome_SetWebBrowser,
1605 nsWebBrowserChrome_GetChromeFlags,
1606 nsWebBrowserChrome_SetChromeFlags,
1607 nsWebBrowserChrome_DestroyBrowserWindow,
1608 nsWebBrowserChrome_SizeBrowserTo,
1609 nsWebBrowserChrome_ShowAsModal,
1610 nsWebBrowserChrome_IsWindowModal,
1611 nsWebBrowserChrome_ExitModalEventLoop
1614 /**********************************************************
1615 * nsIContextMenuListener interface
1618 static inline GeckoBrowser *impl_from_nsIContextMenuListener(nsIContextMenuListener *iface)
1620 return CONTAINING_RECORD(iface, GeckoBrowser, nsIContextMenuListener_iface);
1623 static nsresult NSAPI nsContextMenuListener_QueryInterface(nsIContextMenuListener *iface,
1624 nsIIDRef riid, void **result)
1626 GeckoBrowser *This = impl_from_nsIContextMenuListener(iface);
1627 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1630 static nsrefcnt NSAPI nsContextMenuListener_AddRef(nsIContextMenuListener *iface)
1632 GeckoBrowser *This = impl_from_nsIContextMenuListener(iface);
1633 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1636 static nsrefcnt NSAPI nsContextMenuListener_Release(nsIContextMenuListener *iface)
1638 GeckoBrowser *This = impl_from_nsIContextMenuListener(iface);
1639 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
1642 static nsresult NSAPI nsContextMenuListener_OnShowContextMenu(nsIContextMenuListener *iface,
1643 UINT32 aContextFlags, nsIDOMEvent *aEvent, nsIDOMNode *aNode)
1645 GeckoBrowser *This = impl_from_nsIContextMenuListener(iface);
1646 nsIDOMMouseEvent *mouse_event;
1647 HTMLDOMNode *node;
1648 DOMEvent *event;
1649 POINT pt;
1650 DWORD dwID = CONTEXT_MENU_DEFAULT;
1651 nsresult nsres;
1652 HRESULT hres;
1654 TRACE("(%p)->(%08x %p %p)\n", This, aContextFlags, aEvent, aNode);
1656 if (!aNode)
1657 return NS_ERROR_FAILURE;
1659 hres = get_node(aNode, TRUE, &node);
1660 if(FAILED(hres))
1661 return NS_ERROR_FAILURE;
1663 hres = create_event_from_nsevent(aEvent, dispex_compat_mode(&node->event_target.dispex), &event);
1664 if(SUCCEEDED(hres)) {
1665 dispatch_event(&node->event_target, event);
1666 IDOMEvent_Release(&event->IDOMEvent_iface);
1669 nsres = nsIDOMEvent_QueryInterface(aEvent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
1670 assert(NS_SUCCEEDED(nsres));
1672 nsIDOMMouseEvent_GetScreenX(mouse_event, &pt.x);
1673 nsIDOMMouseEvent_GetScreenY(mouse_event, &pt.y);
1674 nsIDOMMouseEvent_Release(mouse_event);
1676 switch(aContextFlags) {
1677 case CONTEXT_NONE:
1678 case CONTEXT_DOCUMENT:
1679 case CONTEXT_TEXT: {
1680 nsISelection *selection;
1682 nsres = nsIDOMHTMLDocument_GetSelection(This->doc->basedoc.doc_node->nsdoc, &selection);
1683 if(NS_SUCCEEDED(nsres) && selection) {
1684 cpp_bool is_collapsed;
1686 /* FIXME: Check if the click was inside selection. */
1687 nsres = nsISelection_GetIsCollapsed(selection, &is_collapsed);
1688 nsISelection_Release(selection);
1689 if(NS_SUCCEEDED(nsres) && !is_collapsed)
1690 dwID = CONTEXT_MENU_TEXTSELECT;
1692 break;
1694 case CONTEXT_IMAGE:
1695 case CONTEXT_IMAGE|CONTEXT_LINK:
1696 dwID = CONTEXT_MENU_IMAGE;
1697 break;
1698 case CONTEXT_LINK:
1699 dwID = CONTEXT_MENU_ANCHOR;
1700 break;
1701 case CONTEXT_INPUT:
1702 dwID = CONTEXT_MENU_CONTROL;
1703 break;
1704 default:
1705 FIXME("aContextFlags=%08x\n", aContextFlags);
1708 show_context_menu(This->doc, dwID, &pt, (IDispatch*)&node->IHTMLDOMNode_iface);
1709 node_release(node);
1710 return NS_OK;
1713 static const nsIContextMenuListenerVtbl nsContextMenuListenerVtbl = {
1714 nsContextMenuListener_QueryInterface,
1715 nsContextMenuListener_AddRef,
1716 nsContextMenuListener_Release,
1717 nsContextMenuListener_OnShowContextMenu
1720 /**********************************************************
1721 * nsIURIContentListener interface
1724 static inline GeckoBrowser *impl_from_nsIURIContentListener(nsIURIContentListener *iface)
1726 return CONTAINING_RECORD(iface, GeckoBrowser, nsIURIContentListener_iface);
1729 static nsresult NSAPI nsURIContentListener_QueryInterface(nsIURIContentListener *iface,
1730 nsIIDRef riid, void **result)
1732 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1733 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1736 static nsrefcnt NSAPI nsURIContentListener_AddRef(nsIURIContentListener *iface)
1738 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1739 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1742 static nsrefcnt NSAPI nsURIContentListener_Release(nsIURIContentListener *iface)
1744 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1745 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
1748 static nsresult NSAPI nsURIContentListener_OnStartURIOpen(nsIURIContentListener *iface,
1749 nsIURI *aURI, cpp_bool *_retval)
1751 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1752 nsACString spec_str;
1753 const char *spec;
1755 nsACString_Init(&spec_str, NULL);
1756 nsIURI_GetSpec(aURI, &spec_str);
1757 nsACString_GetData(&spec_str, &spec);
1759 TRACE("(%p)->(%p(%s) %p)\n", This, aURI, debugstr_a(spec), _retval);
1761 nsACString_Finish(&spec_str);
1763 return This->content_listener
1764 ? nsIURIContentListener_OnStartURIOpen(This->content_listener, aURI, _retval)
1765 : NS_OK;
1768 static nsresult NSAPI nsURIContentListener_DoContent(nsIURIContentListener *iface,
1769 const nsACString *aContentType, cpp_bool aIsContentPreferred, nsIRequest *aRequest,
1770 nsIStreamListener **aContentHandler, cpp_bool *_retval)
1772 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1774 TRACE("(%p)->(%p %x %p %p %p)\n", This, aContentType, aIsContentPreferred,
1775 aRequest, aContentHandler, _retval);
1777 return This->content_listener
1778 ? nsIURIContentListener_DoContent(This->content_listener, aContentType,
1779 aIsContentPreferred, aRequest, aContentHandler, _retval)
1780 : NS_ERROR_NOT_IMPLEMENTED;
1783 static nsresult NSAPI nsURIContentListener_IsPreferred(nsIURIContentListener *iface,
1784 const char *aContentType, char **aDesiredContentType, cpp_bool *_retval)
1786 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1788 TRACE("(%p)->(%s %p %p)\n", This, debugstr_a(aContentType), aDesiredContentType, _retval);
1790 /* FIXME: Should we do something here? */
1791 *_retval = TRUE;
1793 return This->content_listener
1794 ? nsIURIContentListener_IsPreferred(This->content_listener, aContentType,
1795 aDesiredContentType, _retval)
1796 : NS_OK;
1799 static nsresult NSAPI nsURIContentListener_CanHandleContent(nsIURIContentListener *iface,
1800 const char *aContentType, cpp_bool aIsContentPreferred, char **aDesiredContentType,
1801 cpp_bool *_retval)
1803 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1805 TRACE("(%p)->(%s %x %p %p)\n", This, debugstr_a(aContentType), aIsContentPreferred,
1806 aDesiredContentType, _retval);
1808 return This->content_listener
1809 ? nsIURIContentListener_CanHandleContent(This->content_listener, aContentType,
1810 aIsContentPreferred, aDesiredContentType, _retval)
1811 : NS_ERROR_NOT_IMPLEMENTED;
1814 static nsresult NSAPI nsURIContentListener_GetLoadCookie(nsIURIContentListener *iface,
1815 nsISupports **aLoadCookie)
1817 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1819 WARN("(%p)->(%p)\n", This, aLoadCookie);
1821 return This->content_listener
1822 ? nsIURIContentListener_GetLoadCookie(This->content_listener, aLoadCookie)
1823 : NS_ERROR_NOT_IMPLEMENTED;
1826 static nsresult NSAPI nsURIContentListener_SetLoadCookie(nsIURIContentListener *iface,
1827 nsISupports *aLoadCookie)
1829 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1831 WARN("(%p)->(%p)\n", This, aLoadCookie);
1833 return This->content_listener
1834 ? nsIURIContentListener_SetLoadCookie(This->content_listener, aLoadCookie)
1835 : NS_ERROR_NOT_IMPLEMENTED;
1838 static nsresult NSAPI nsURIContentListener_GetParentContentListener(nsIURIContentListener *iface,
1839 nsIURIContentListener **aParentContentListener)
1841 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1843 TRACE("(%p)->(%p)\n", This, aParentContentListener);
1845 if(This->content_listener)
1846 nsIURIContentListener_AddRef(This->content_listener);
1848 *aParentContentListener = This->content_listener;
1849 return NS_OK;
1852 static nsresult NSAPI nsURIContentListener_SetParentContentListener(nsIURIContentListener *iface,
1853 nsIURIContentListener *aParentContentListener)
1855 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1857 TRACE("(%p)->(%p)\n", This, aParentContentListener);
1859 if(aParentContentListener == &This->nsIURIContentListener_iface)
1860 return NS_OK;
1862 if(This->content_listener)
1863 nsIURIContentListener_Release(This->content_listener);
1865 This->content_listener = aParentContentListener;
1866 if(This->content_listener)
1867 nsIURIContentListener_AddRef(This->content_listener);
1869 return NS_OK;
1872 static const nsIURIContentListenerVtbl nsURIContentListenerVtbl = {
1873 nsURIContentListener_QueryInterface,
1874 nsURIContentListener_AddRef,
1875 nsURIContentListener_Release,
1876 nsURIContentListener_OnStartURIOpen,
1877 nsURIContentListener_DoContent,
1878 nsURIContentListener_IsPreferred,
1879 nsURIContentListener_CanHandleContent,
1880 nsURIContentListener_GetLoadCookie,
1881 nsURIContentListener_SetLoadCookie,
1882 nsURIContentListener_GetParentContentListener,
1883 nsURIContentListener_SetParentContentListener
1886 /**********************************************************
1887 * nsIEmbeddinSiteWindow interface
1890 static inline GeckoBrowser *impl_from_nsIEmbeddingSiteWindow(nsIEmbeddingSiteWindow *iface)
1892 return CONTAINING_RECORD(iface, GeckoBrowser, nsIEmbeddingSiteWindow_iface);
1895 static nsresult NSAPI nsEmbeddingSiteWindow_QueryInterface(nsIEmbeddingSiteWindow *iface,
1896 nsIIDRef riid, void **result)
1898 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1899 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1902 static nsrefcnt NSAPI nsEmbeddingSiteWindow_AddRef(nsIEmbeddingSiteWindow *iface)
1904 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1905 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1908 static nsrefcnt NSAPI nsEmbeddingSiteWindow_Release(nsIEmbeddingSiteWindow *iface)
1910 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1911 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
1914 static nsresult NSAPI nsEmbeddingSiteWindow_SetDimensions(nsIEmbeddingSiteWindow *iface,
1915 UINT32 flags, LONG x, LONG y, LONG cx, LONG cy)
1917 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1918 WARN("(%p)->(%08x %d %d %d %d)\n", This, flags, x, y, cx, cy);
1919 return NS_ERROR_NOT_IMPLEMENTED;
1922 static nsresult NSAPI nsEmbeddingSiteWindow_GetDimensions(nsIEmbeddingSiteWindow *iface,
1923 UINT32 flags, LONG *x, LONG *y, LONG *cx, LONG *cy)
1925 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1926 RECT r;
1928 TRACE("(%p)->(%x %p %p %p %p)\n", This, flags, x, y, cx, cy);
1930 if(!GetWindowRect(This->hwnd, &r)) {
1931 ERR("GetWindowRect failed\n");
1932 return NS_ERROR_FAILURE;
1935 if(x)
1936 *x = r.left;
1937 if(y)
1938 *y = r.top;
1939 if(cx)
1940 *cx = r.right-r.left;
1941 if(cy)
1942 *cy = r.bottom-r.top;
1943 return NS_OK;
1946 static nsresult NSAPI nsEmbeddingSiteWindow_SetFocus(nsIEmbeddingSiteWindow *iface)
1948 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1950 TRACE("(%p)\n", This);
1952 return nsIBaseWindow_SetFocus(This->window);
1955 static nsresult NSAPI nsEmbeddingSiteWindow_GetVisibility(nsIEmbeddingSiteWindow *iface,
1956 cpp_bool *aVisibility)
1958 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1960 TRACE("(%p)->(%p)\n", This, aVisibility);
1962 *aVisibility = This->doc && This->doc->hwnd && IsWindowVisible(This->doc->hwnd);
1963 return NS_OK;
1966 static nsresult NSAPI nsEmbeddingSiteWindow_SetVisibility(nsIEmbeddingSiteWindow *iface,
1967 cpp_bool aVisibility)
1969 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1971 TRACE("(%p)->(%x)\n", This, aVisibility);
1973 return NS_OK;
1976 static nsresult NSAPI nsEmbeddingSiteWindow_GetTitle(nsIEmbeddingSiteWindow *iface,
1977 PRUnichar **aTitle)
1979 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1980 WARN("(%p)->(%p)\n", This, aTitle);
1981 return NS_ERROR_NOT_IMPLEMENTED;
1984 static nsresult NSAPI nsEmbeddingSiteWindow_SetTitle(nsIEmbeddingSiteWindow *iface,
1985 const PRUnichar *aTitle)
1987 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1988 WARN("(%p)->(%s)\n", This, debugstr_w(aTitle));
1989 return NS_ERROR_NOT_IMPLEMENTED;
1992 static nsresult NSAPI nsEmbeddingSiteWindow_GetSiteWindow(nsIEmbeddingSiteWindow *iface,
1993 void **aSiteWindow)
1995 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1997 TRACE("(%p)->(%p)\n", This, aSiteWindow);
1999 *aSiteWindow = This->hwnd;
2000 return NS_OK;
2003 static const nsIEmbeddingSiteWindowVtbl nsEmbeddingSiteWindowVtbl = {
2004 nsEmbeddingSiteWindow_QueryInterface,
2005 nsEmbeddingSiteWindow_AddRef,
2006 nsEmbeddingSiteWindow_Release,
2007 nsEmbeddingSiteWindow_SetDimensions,
2008 nsEmbeddingSiteWindow_GetDimensions,
2009 nsEmbeddingSiteWindow_SetFocus,
2010 nsEmbeddingSiteWindow_GetVisibility,
2011 nsEmbeddingSiteWindow_SetVisibility,
2012 nsEmbeddingSiteWindow_GetTitle,
2013 nsEmbeddingSiteWindow_SetTitle,
2014 nsEmbeddingSiteWindow_GetSiteWindow
2017 static inline GeckoBrowser *impl_from_nsITooltipListener(nsITooltipListener *iface)
2019 return CONTAINING_RECORD(iface, GeckoBrowser, nsITooltipListener_iface);
2022 static nsresult NSAPI nsTooltipListener_QueryInterface(nsITooltipListener *iface, nsIIDRef riid,
2023 void **result)
2025 GeckoBrowser *This = impl_from_nsITooltipListener(iface);
2026 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
2029 static nsrefcnt NSAPI nsTooltipListener_AddRef(nsITooltipListener *iface)
2031 GeckoBrowser *This = impl_from_nsITooltipListener(iface);
2032 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
2035 static nsrefcnt NSAPI nsTooltipListener_Release(nsITooltipListener *iface)
2037 GeckoBrowser *This = impl_from_nsITooltipListener(iface);
2038 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
2041 static nsresult NSAPI nsTooltipListener_OnShowTooltip(nsITooltipListener *iface,
2042 LONG aXCoord, LONG aYCoord, const PRUnichar *aTipText)
2044 GeckoBrowser *This = impl_from_nsITooltipListener(iface);
2046 if (This->doc)
2047 show_tooltip(This->doc, aXCoord, aYCoord, aTipText);
2049 return NS_OK;
2052 static nsresult NSAPI nsTooltipListener_OnHideTooltip(nsITooltipListener *iface)
2054 GeckoBrowser *This = impl_from_nsITooltipListener(iface);
2056 if (This->doc)
2057 hide_tooltip(This->doc);
2059 return NS_OK;
2062 static const nsITooltipListenerVtbl nsTooltipListenerVtbl = {
2063 nsTooltipListener_QueryInterface,
2064 nsTooltipListener_AddRef,
2065 nsTooltipListener_Release,
2066 nsTooltipListener_OnShowTooltip,
2067 nsTooltipListener_OnHideTooltip
2070 static inline GeckoBrowser *impl_from_nsIInterfaceRequestor(nsIInterfaceRequestor *iface)
2072 return CONTAINING_RECORD(iface, GeckoBrowser, nsIInterfaceRequestor_iface);
2075 static nsresult NSAPI nsInterfaceRequestor_QueryInterface(nsIInterfaceRequestor *iface,
2076 nsIIDRef riid, void **result)
2078 GeckoBrowser *This = impl_from_nsIInterfaceRequestor(iface);
2079 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
2082 static nsrefcnt NSAPI nsInterfaceRequestor_AddRef(nsIInterfaceRequestor *iface)
2084 GeckoBrowser *This = impl_from_nsIInterfaceRequestor(iface);
2085 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
2088 static nsrefcnt NSAPI nsInterfaceRequestor_Release(nsIInterfaceRequestor *iface)
2090 GeckoBrowser *This = impl_from_nsIInterfaceRequestor(iface);
2091 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
2094 static nsresult NSAPI nsInterfaceRequestor_GetInterface(nsIInterfaceRequestor *iface,
2095 nsIIDRef riid, void **result)
2097 GeckoBrowser *This = impl_from_nsIInterfaceRequestor(iface);
2099 if(IsEqualGUID(&IID_mozIDOMWindowProxy, riid)) {
2100 TRACE("(%p)->(IID_nsIDOMWindow %p)\n", This, result);
2101 return nsIWebBrowser_GetContentDOMWindow(This->webbrowser, (mozIDOMWindowProxy**)result);
2104 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
2107 static const nsIInterfaceRequestorVtbl nsInterfaceRequestorVtbl = {
2108 nsInterfaceRequestor_QueryInterface,
2109 nsInterfaceRequestor_AddRef,
2110 nsInterfaceRequestor_Release,
2111 nsInterfaceRequestor_GetInterface
2114 static inline GeckoBrowser *impl_from_nsISupportsWeakReference(nsISupportsWeakReference *iface)
2116 return CONTAINING_RECORD(iface, GeckoBrowser, nsISupportsWeakReference_iface);
2119 static nsresult NSAPI nsSupportsWeakReference_QueryInterface(nsISupportsWeakReference *iface,
2120 nsIIDRef riid, void **result)
2122 GeckoBrowser *This = impl_from_nsISupportsWeakReference(iface);
2123 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
2126 static nsrefcnt NSAPI nsSupportsWeakReference_AddRef(nsISupportsWeakReference *iface)
2128 GeckoBrowser *This = impl_from_nsISupportsWeakReference(iface);
2129 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
2132 static nsrefcnt NSAPI nsSupportsWeakReference_Release(nsISupportsWeakReference *iface)
2134 GeckoBrowser *This = impl_from_nsISupportsWeakReference(iface);
2135 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
2138 static nsresult NSAPI nsSupportsWeakReference_GetWeakReference(nsISupportsWeakReference *iface,
2139 nsIWeakReference **_retval)
2141 GeckoBrowser *This = impl_from_nsISupportsWeakReference(iface);
2143 TRACE("(%p)->(%p)\n", This, _retval);
2145 if(!This->weak_reference) {
2146 This->weak_reference = heap_alloc(sizeof(nsWeakReference));
2147 if(!This->weak_reference)
2148 return NS_ERROR_OUT_OF_MEMORY;
2150 This->weak_reference->nsIWeakReference_iface.lpVtbl = &nsWeakReferenceVtbl;
2151 This->weak_reference->ref = 1;
2152 This->weak_reference->browser = This;
2155 *_retval = &This->weak_reference->nsIWeakReference_iface;
2156 nsIWeakReference_AddRef(*_retval);
2157 return NS_OK;
2160 static const nsISupportsWeakReferenceVtbl nsSupportsWeakReferenceVtbl = {
2161 nsSupportsWeakReference_QueryInterface,
2162 nsSupportsWeakReference_AddRef,
2163 nsSupportsWeakReference_Release,
2164 nsSupportsWeakReference_GetWeakReference
2167 static HRESULT init_browser(GeckoBrowser *browser)
2169 mozIDOMWindowProxy *mozwindow;
2170 nsIWebBrowserSetup *wbsetup;
2171 nsIScrollable *scrollable;
2172 nsresult nsres;
2173 HRESULT hres;
2175 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_WEBBROWSER_CONTRACTID,
2176 NULL, &IID_nsIWebBrowser, (void**)&browser->webbrowser);
2177 if(NS_FAILED(nsres)) {
2178 ERR("Creating WebBrowser failed: %08x\n", nsres);
2179 return E_FAIL;
2182 nsres = nsIWebBrowser_SetContainerWindow(browser->webbrowser, &browser->nsIWebBrowserChrome_iface);
2183 if(NS_FAILED(nsres)) {
2184 ERR("SetContainerWindow failed: %08x\n", nsres);
2185 return E_FAIL;
2188 nsres = nsIWebBrowser_QueryInterface(browser->webbrowser, &IID_nsIBaseWindow,
2189 (void**)&browser->window);
2190 if(NS_FAILED(nsres)) {
2191 ERR("Could not get nsIBaseWindow interface: %08x\n", nsres);
2192 return E_FAIL;
2195 nsres = nsIWebBrowser_QueryInterface(browser->webbrowser, &IID_nsIWebBrowserSetup,
2196 (void**)&wbsetup);
2197 if(NS_SUCCEEDED(nsres)) {
2198 nsres = nsIWebBrowserSetup_SetProperty(wbsetup, SETUP_IS_CHROME_WRAPPER, FALSE);
2199 nsIWebBrowserSetup_Release(wbsetup);
2200 if(NS_FAILED(nsres)) {
2201 ERR("SetProperty(SETUP_IS_CHROME_WRAPPER) failed: %08x\n", nsres);
2202 return E_FAIL;
2204 }else {
2205 ERR("Could not get nsIWebBrowserSetup interface\n");
2206 return E_FAIL;
2209 nsres = nsIWebBrowser_QueryInterface(browser->webbrowser, &IID_nsIWebNavigation,
2210 (void**)&browser->navigation);
2211 if(NS_FAILED(nsres)) {
2212 ERR("Could not get nsIWebNavigation interface: %08x\n", nsres);
2213 return E_FAIL;
2216 nsres = nsIWebBrowser_QueryInterface(browser->webbrowser, &IID_nsIWebBrowserFocus,
2217 (void**)&browser->focus);
2218 if(NS_FAILED(nsres)) {
2219 ERR("Could not get nsIWebBrowserFocus interface: %08x\n", nsres);
2220 return E_FAIL;
2223 if(!browser_class) {
2224 register_browser_class();
2225 if(!browser_class)
2226 return E_FAIL;
2229 browser->hwnd = CreateWindowExW(0, L"NsContainer", NULL,
2230 WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 0, 0, 100, 100,
2231 GetDesktopWindow(), NULL, hInst, browser);
2232 if(!browser->hwnd) {
2233 WARN("Could not create window\n");
2234 return E_FAIL;
2237 nsres = nsIBaseWindow_InitWindow(browser->window, browser->hwnd, NULL, 0, 0, 100, 100);
2238 if(NS_SUCCEEDED(nsres)) {
2239 nsres = nsIBaseWindow_Create(browser->window);
2240 if(NS_FAILED(nsres)) {
2241 WARN("Creating window failed: %08x\n", nsres);
2242 return E_FAIL;
2245 nsIBaseWindow_SetVisibility(browser->window, FALSE);
2246 nsIBaseWindow_SetEnabled(browser->window, FALSE);
2247 }else {
2248 ERR("InitWindow failed: %08x\n", nsres);
2249 return E_FAIL;
2252 nsres = nsIWebBrowser_SetParentURIContentListener(browser->webbrowser,
2253 &browser->nsIURIContentListener_iface);
2254 if(NS_FAILED(nsres))
2255 ERR("SetParentURIContentListener failed: %08x\n", nsres);
2257 nsres = nsIWebBrowser_QueryInterface(browser->webbrowser, &IID_nsIScrollable, (void**)&scrollable);
2258 if(NS_SUCCEEDED(nsres)) {
2259 nsres = nsIScrollable_SetDefaultScrollbarPreferences(scrollable,
2260 ScrollOrientation_Y, Scrollbar_Always);
2261 if(NS_FAILED(nsres))
2262 ERR("Could not set default Y scrollbar prefs: %08x\n", nsres);
2264 nsres = nsIScrollable_SetDefaultScrollbarPreferences(scrollable,
2265 ScrollOrientation_X, Scrollbar_Auto);
2266 if(NS_FAILED(nsres))
2267 ERR("Could not set default X scrollbar prefs: %08x\n", nsres);
2269 nsIScrollable_Release(scrollable);
2270 }else {
2271 ERR("Could not get nsIScrollable: %08x\n", nsres);
2274 nsres = nsIWebBrowser_GetContentDOMWindow(browser->webbrowser, &mozwindow);
2275 if(NS_FAILED(nsres)) {
2276 ERR("GetContentDOMWindow failed: %08x\n", nsres);
2277 return E_FAIL;
2280 hres = create_outer_window(browser, mozwindow, NULL, &browser->content_window);
2281 mozIDOMWindowProxy_Release(mozwindow);
2282 return hres;
2285 HRESULT create_gecko_browser(HTMLDocumentObj *doc, GeckoBrowser **_ret)
2287 GeckoBrowser *ret;
2288 HRESULT hres;
2290 if(!load_gecko())
2291 return CLASS_E_CLASSNOTAVAILABLE;
2293 ret = heap_alloc_zero(sizeof(GeckoBrowser));
2294 if(!ret)
2295 return E_OUTOFMEMORY;
2297 ret->nsIWebBrowserChrome_iface.lpVtbl = &nsWebBrowserChromeVtbl;
2298 ret->nsIContextMenuListener_iface.lpVtbl = &nsContextMenuListenerVtbl;
2299 ret->nsIURIContentListener_iface.lpVtbl = &nsURIContentListenerVtbl;
2300 ret->nsIEmbeddingSiteWindow_iface.lpVtbl = &nsEmbeddingSiteWindowVtbl;
2301 ret->nsITooltipListener_iface.lpVtbl = &nsTooltipListenerVtbl;
2302 ret->nsIInterfaceRequestor_iface.lpVtbl = &nsInterfaceRequestorVtbl;
2303 ret->nsISupportsWeakReference_iface.lpVtbl = &nsSupportsWeakReferenceVtbl;
2305 ret->doc = doc;
2306 ret->ref = 1;
2307 ret->script_mode = SCRIPTMODE_ACTIVESCRIPT;
2308 ret->usermode = UNKNOWN_USERMODE;
2309 list_init(&ret->document_nodes);
2310 list_init(&ret->outer_windows);
2312 hres = init_browser(ret);
2313 if(SUCCEEDED(hres))
2314 *_ret = ret;
2315 else
2316 nsIWebBrowserChrome_Release(&ret->nsIWebBrowserChrome_iface);
2317 return hres;
2320 void detach_gecko_browser(GeckoBrowser *This)
2322 nsIDOMWindowUtils *window_utils = NULL;
2324 TRACE("(%p)\n", This);
2326 This->doc = NULL;
2328 if(This->content_window) {
2329 get_nsinterface((nsISupports*)This->content_window->nswindow, &IID_nsIDOMWindowUtils, (void**)&window_utils);
2331 IHTMLWindow2_Release(&This->content_window->base.IHTMLWindow2_iface);
2332 This->content_window = NULL;
2335 while(!list_empty(&This->document_nodes)) {
2336 HTMLDocumentNode *doc = LIST_ENTRY(list_head(&This->document_nodes), HTMLDocumentNode, browser_entry);
2337 detach_document_node(doc);
2340 while(!list_empty(&This->outer_windows)) {
2341 HTMLOuterWindow *window = LIST_ENTRY(list_head(&This->outer_windows), HTMLOuterWindow, browser_entry);
2342 list_remove(&window->browser_entry);
2343 window->browser = NULL;
2346 ShowWindow(This->hwnd, SW_HIDE);
2347 SetParent(This->hwnd, NULL);
2349 nsIBaseWindow_SetVisibility(This->window, FALSE);
2350 nsIBaseWindow_Destroy(This->window);
2352 nsIWebBrowser_SetContainerWindow(This->webbrowser, NULL);
2354 nsIWebBrowser_Release(This->webbrowser);
2355 This->webbrowser = NULL;
2357 nsIWebNavigation_Release(This->navigation);
2358 This->navigation = NULL;
2360 nsIBaseWindow_Release(This->window);
2361 This->window = NULL;
2363 nsIWebBrowserFocus_Release(This->focus);
2364 This->focus = NULL;
2366 if(This->editor_controller) {
2367 nsIController_Release(This->editor_controller);
2368 This->editor_controller = NULL;
2371 if(This->editor) {
2372 nsIEditor_Release(This->editor);
2373 This->editor = NULL;
2376 if(This->content_listener) {
2377 nsIURIContentListener_Release(This->content_listener);
2378 This->content_listener = NULL;
2381 if(This->hwnd) {
2382 DestroyWindow(This->hwnd);
2383 This->hwnd = NULL;
2386 nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
2388 /* Force cycle collection */
2389 if(window_utils) {
2390 nsIDOMWindowUtils_CycleCollect(window_utils, NULL, 0);
2391 nsIDOMWindowUtils_Release(window_utils);
2396 * FIXME: nsIScriptObjectPrincipal uses thiscall calling convention, so we need this hack on i386.
2397 * This will be removed after the next Gecko update, that will change calling convention on Gecko side.
2399 #ifdef __i386__
2400 __ASM_GLOBAL_FUNC(call_thiscall_func,
2401 "popl %eax\n\t"
2402 "popl %edx\n\t"
2403 "popl %ecx\n\t"
2404 "pushl %eax\n\t"
2405 "jmp *%edx\n\t")
2406 #define nsIScriptObjectPrincipal_GetPrincipal(this) ((void* (WINAPI*)(void*,void*))&call_thiscall_func)((this)->lpVtbl->GetPrincipal,this)
2407 #endif
2409 nsIXMLHttpRequest *create_nsxhr(nsIDOMWindow *nswindow)
2411 nsIScriptObjectPrincipal *sop;
2412 mozIDOMWindow *inner_window;
2413 nsIPrincipal *nspri;
2414 nsIGlobalObject *nsglo;
2415 nsIXMLHttpRequest *nsxhr;
2416 nsresult nsres;
2418 nsres = nsIDOMWindow_GetInnerWindow(nswindow, &inner_window);
2419 if(NS_FAILED(nsres)) {
2420 ERR("Could not get inner window: %08x\n", nsres);
2421 return NULL;
2424 nsres = mozIDOMWindow_QueryInterface(inner_window, &IID_nsIGlobalObject, (void **)&nsglo);
2425 mozIDOMWindow_Release(inner_window);
2426 assert(nsres == NS_OK);
2428 nsres = nsIGlobalObject_QueryInterface(nsglo, &IID_nsIScriptObjectPrincipal, (void**)&sop);
2429 assert(nsres == NS_OK);
2431 nspri = nsIScriptObjectPrincipal_GetPrincipal(sop);
2432 nsIScriptObjectPrincipal_Release(sop);
2434 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
2435 NS_XMLHTTPREQUEST_CONTRACTID, NULL, &IID_nsIXMLHttpRequest,
2436 (void**)&nsxhr);
2437 if(NS_SUCCEEDED(nsres)) {
2438 nsres = nsIXMLHttpRequest_Init(nsxhr, nspri, NULL, nsglo, NULL, NULL);
2439 if(NS_FAILED(nsres))
2440 nsIXMLHttpRequest_Release(nsxhr);
2442 nsISupports_Release(nspri);
2443 nsIGlobalObject_Release(nsglo);
2444 if(NS_FAILED(nsres)) {
2445 ERR("nsIXMLHttpRequest_Init failed: %08x\n", nsres);
2446 return NULL;
2449 return nsxhr;