user32: Hook drawing menu buttons.
[wine.git] / dlls / mshtml / nsembed.c
blob67d40ca00857a4d40a50fcd66ff95cff94e8c71f
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: %08lx\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=%ld\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=%ld\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: %08lx\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: %08lx\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: %08lx\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: %08lx\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: %08lx\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_bool_pref(pref, "layout.css.grid.enabled", TRUE);
544 set_int_pref(pref, "layout.spellcheckDefault", 0);
546 nsIPrefBranch_Release(pref);
549 static BOOL init_xpcom(const PRUnichar *gre_path)
551 nsIComponentRegistrar *registrar = NULL;
552 nsIFile *gre_dir;
553 WCHAR *ptr;
554 nsresult nsres;
556 nsres = create_nsfile(gre_path, &gre_dir);
557 if(NS_FAILED(nsres)) {
558 FreeLibrary(xul_handle);
559 return FALSE;
562 nsres = NS_InitXPCOM2(&pServMgr, gre_dir, (nsIDirectoryServiceProvider*)&nsDirectoryServiceProvider2);
563 if(NS_FAILED(nsres)) {
564 ERR("NS_InitXPCOM2 failed: %08lx\n", nsres);
565 FreeLibrary(xul_handle);
566 return FALSE;
569 lstrcpyW(gecko_path, gre_path);
570 for(ptr = gecko_path; *ptr; ptr++) {
571 if(*ptr == '\\')
572 *ptr = '/';
574 gecko_path_len = ptr-gecko_path;
576 nsres = nsIServiceManager_QueryInterface(pServMgr, &IID_nsIComponentManager, (void**)&pCompMgr);
577 if(NS_FAILED(nsres))
578 ERR("Could not get nsIComponentManager: %08lx\n", nsres);
580 init_nsio(pCompMgr);
581 init_mutation(pCompMgr);
582 set_preferences();
584 nsres = nsIServiceManager_GetServiceByContractID(pServMgr, NS_CATEGORYMANAGER_CONTRACTID,
585 &IID_nsICategoryManager, (void**)&cat_mgr);
586 if(NS_FAILED(nsres))
587 ERR("Could not get category manager service: %08lx\n", nsres);
589 nsres = NS_GetComponentRegistrar(&registrar);
590 if(NS_SUCCEEDED(nsres)) {
591 register_nsservice(registrar, pServMgr);
592 nsIComponentRegistrar_Release(registrar);
593 }else {
594 ERR("NS_GetComponentRegistrar failed: %08lx\n", nsres);
597 init_node_cc();
599 return TRUE;
602 static BOOL load_xul(WCHAR *gecko_path)
604 size_t len;
606 set_environment(gecko_path);
608 TRACE("(%s)\n", debugstr_w(gecko_path));
610 len = wcslen(gecko_path);
611 wcscpy(gecko_path + len, L"\\xul.dll");
612 xul_handle = LoadLibraryExW(gecko_path, 0, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
613 gecko_path[len] = 0;
614 if(!xul_handle) {
615 WARN("Could not load XUL: %ld\n", GetLastError());
616 return FALSE;
619 #define NS_DLSYM(func) \
620 func = (void *)GetProcAddress(xul_handle, #func); \
621 if(!func) \
622 ERR("Could not GetProcAddress(" #func ") failed\n")
624 NS_DLSYM(NS_InitXPCOM2);
625 NS_DLSYM(NS_ShutdownXPCOM);
626 NS_DLSYM(NS_GetComponentRegistrar);
627 NS_DLSYM(NS_StringContainerInit2);
628 NS_DLSYM(NS_CStringContainerInit2);
629 NS_DLSYM(NS_StringContainerFinish);
630 NS_DLSYM(NS_CStringContainerFinish);
631 NS_DLSYM(NS_StringSetData);
632 NS_DLSYM(NS_CStringSetData);
633 NS_DLSYM(NS_NewLocalFile);
634 NS_DLSYM(NS_StringGetData);
635 NS_DLSYM(NS_CStringGetData);
636 NS_DLSYM(NS_StringGetIsVoid);
637 NS_DLSYM(NS_Alloc);
638 NS_DLSYM(NS_Free);
639 NS_DLSYM(ccref_incr);
640 NS_DLSYM(ccref_decr);
641 NS_DLSYM(ccref_init);
642 NS_DLSYM(ccp_init);
643 NS_DLSYM(describe_cc_node);
644 NS_DLSYM(note_cc_edge);
646 #undef NS_DLSYM
648 return init_xpcom(gecko_path);
651 static WCHAR *check_version(const WCHAR *path)
653 WCHAR *file_name;
654 char version[128];
655 DWORD read=0;
656 size_t len;
657 HANDLE hfile;
659 if(!wcsncmp(path, L"\\??\\", 4))
660 path += 4;
661 if(path[1] != ':') {
662 TRACE("Skipping %s\n", debugstr_w(path));
663 return FALSE; /* Gecko needs to be accessible via dos path */
666 len = wcslen(path);
667 file_name = heap_alloc((len + 12) * sizeof(WCHAR));
668 if(!file_name)
669 return NULL;
671 PathCanonicalizeW(file_name, path);
672 len = lstrlenW(file_name);
673 wcscpy(file_name + len, L"\\VERSION");
675 hfile = CreateFileW(file_name, GENERIC_READ, FILE_SHARE_READ, NULL,
676 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
677 file_name[len] = 0;
678 if(hfile == INVALID_HANDLE_VALUE) {
679 TRACE("%s not found\n", debugstr_w(file_name));
680 heap_free(file_name);
681 return NULL;
684 ReadFile(hfile, version, sizeof(version), &read, NULL);
685 version[read] = 0;
686 CloseHandle(hfile);
688 TRACE("%s: %s\n", debugstr_w(file_name), debugstr_a(version));
690 if(strcmp(version, GECKO_VERSION_STRING)) {
691 ERR("Unexpected version %s, expected \"%s\"\n", debugstr_a(version),
692 GECKO_VERSION_STRING);
693 heap_free(file_name);
694 return NULL;
697 return file_name;
700 static WCHAR *find_wine_gecko_reg(void)
702 WCHAR buffer[MAX_PATH];
703 DWORD res, type, size;
704 HKEY hkey;
706 /* @@ Wine registry key: HKLM\Software\Wine\MSHTML\<version> */
707 res = RegOpenKeyW(HKEY_LOCAL_MACHINE, L"Software\\Wine\\MSHTML\\" GECKO_VERSION, &hkey);
708 if(res != ERROR_SUCCESS)
709 return NULL;
711 size = sizeof(buffer);
712 res = RegQueryValueExW(hkey, L"GeckoPath", NULL, &type, (LPBYTE)buffer, &size);
713 RegCloseKey(hkey);
714 if(res != ERROR_SUCCESS || type != REG_SZ)
715 return FALSE;
717 return check_version(buffer);
720 static WCHAR *heap_strcat(const WCHAR *str1, const WCHAR *str2)
722 size_t len1 = lstrlenW(str1);
723 size_t len2 = lstrlenW(str2);
724 WCHAR *ret = heap_alloc((len1 + len2 + 1) * sizeof(WCHAR));
725 if(!ret) return NULL;
726 memcpy(ret, str1, len1 * sizeof(WCHAR));
727 memcpy(ret + len1, str2, len2 * sizeof(WCHAR));
728 ret[len1 + len2] = 0;
729 return ret;
732 static WCHAR *find_wine_gecko_datadir(void)
734 const WCHAR *data_dir;
735 WCHAR *path = NULL, *ret;
737 if((data_dir = _wgetenv(L"WINEDATADIR")))
738 path = heap_strcat(data_dir, L"\\gecko\\" GECKO_DIR_NAME);
739 else if((data_dir = _wgetenv(L"WINEBUILDDIR")))
740 path = heap_strcat(data_dir, L"\\..\\gecko\\" GECKO_DIR_NAME);
741 if(!path)
742 return NULL;
744 ret = check_version(path);
745 heap_free(path);
746 return ret;
749 static WCHAR *find_wine_gecko_unix(const char *unix_path)
751 static WCHAR * (CDECL *p_wine_get_dos_file_name)(const char*);
752 WCHAR *dos_dir, *ret;
754 if(!p_wine_get_dos_file_name) {
755 p_wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleA("kernel32"), "wine_get_dos_file_name");
756 if(!p_wine_get_dos_file_name)
757 return FALSE;
760 dos_dir = p_wine_get_dos_file_name(unix_path);
761 if(!dos_dir)
762 return FALSE;
764 ret = check_version(dos_dir);
766 heap_free(dos_dir);
767 return ret;
770 static CRITICAL_SECTION cs_load_gecko;
771 static CRITICAL_SECTION_DEBUG cs_load_gecko_dbg =
773 0, 0, &cs_load_gecko,
774 { &cs_load_gecko_dbg.ProcessLocksList, &cs_load_gecko_dbg.ProcessLocksList },
775 0, 0, { (DWORD_PTR)(__FILE__ ": load_gecko") }
777 static CRITICAL_SECTION cs_load_gecko = { &cs_load_gecko_dbg, -1, 0, 0, 0, 0 };
779 BOOL load_gecko(void)
781 BOOL ret = FALSE;
783 static DWORD loading_thread;
785 TRACE("()\n");
787 if(!GECKO_ARCH_STRING[0]) {
788 FIXME("Wine Gecko is not supported on this architecture.\n");
789 return FALSE;
792 /* load_gecko may be called recursively */
793 if(loading_thread == GetCurrentThreadId())
794 return pCompMgr != NULL;
796 EnterCriticalSection(&cs_load_gecko);
798 if(!loading_thread) {
799 WCHAR *gecko_path;
801 loading_thread = GetCurrentThreadId();
803 if(!(gecko_path = find_wine_gecko_reg())
804 && !(gecko_path = find_wine_gecko_datadir())
805 && !(gecko_path = find_wine_gecko_unix(INSTALL_DATADIR "/wine/gecko/" GECKO_DIR_NAME))
806 && (!strcmp(INSTALL_DATADIR, "/usr/share") ||
807 !(gecko_path = find_wine_gecko_unix("/usr/share/wine/gecko/" GECKO_DIR_NAME)))
808 && !(gecko_path = find_wine_gecko_unix("/opt/wine/gecko/" GECKO_DIR_NAME))
809 && install_wine_gecko())
810 gecko_path = find_wine_gecko_reg();
812 if(gecko_path) {
813 ret = load_xul(gecko_path);
814 heap_free(gecko_path);
815 }else {
816 MESSAGE("Could not find Wine Gecko. HTML rendering will be disabled.\n");
818 }else {
819 ret = pCompMgr != NULL;
822 LeaveCriticalSection(&cs_load_gecko);
824 return ret;
827 void *nsalloc(size_t size)
829 return NS_Alloc(size);
832 void nsfree(void *mem)
834 NS_Free(mem);
837 BOOL nsACString_Init(nsACString *str, const char *data)
839 return NS_SUCCEEDED(NS_CStringContainerInit2(str, data, PR_UINT32_MAX, 0));
843 * Initializes nsACString with data owned by caller.
844 * Caller must ensure that data is valid during lifetime of string object.
846 void nsACString_InitDepend(nsACString *str, const char *data)
848 NS_CStringContainerInit2(str, data, PR_UINT32_MAX, NS_CSTRING_CONTAINER_INIT_DEPEND);
851 void nsACString_SetData(nsACString *str, const char *data)
853 NS_CStringSetData(str, data, PR_UINT32_MAX);
856 UINT32 nsACString_GetData(const nsACString *str, const char **data)
858 return NS_CStringGetData(str, data, NULL);
861 void nsACString_Finish(nsACString *str)
863 NS_CStringContainerFinish(str);
866 BOOL nsAString_Init(nsAString *str, const PRUnichar *data)
868 return NS_SUCCEEDED(NS_StringContainerInit2(str, data, PR_UINT32_MAX, 0));
872 * Initializes nsAString with data owned by caller.
873 * Caller must ensure that data is valid during lifetime of string object.
875 void nsAString_InitDepend(nsAString *str, const PRUnichar *data)
877 NS_StringContainerInit2(str, data, PR_UINT32_MAX, NS_STRING_CONTAINER_INIT_DEPEND);
880 UINT32 nsAString_GetData(const nsAString *str, const PRUnichar **data)
882 return NS_StringGetData(str, data, NULL);
885 void nsAString_SetData(nsAString *str, const PRUnichar *data)
887 NS_StringSetData(str, data, PR_UINT32_MAX);
890 void nsAString_Finish(nsAString *str)
892 NS_StringContainerFinish(str);
895 HRESULT map_nsresult(nsresult nsres)
897 switch(nsres) {
898 case NS_OK:
899 return S_OK;
900 case NS_ERROR_OUT_OF_MEMORY:
901 return E_OUTOFMEMORY;
902 case NS_ERROR_NOT_IMPLEMENTED:
903 return E_NOTIMPL;
904 case NS_NOINTERFACE:
905 return E_NOINTERFACE;
906 case NS_ERROR_INVALID_POINTER:
907 return E_POINTER;
908 case NS_ERROR_INVALID_ARG:
909 return E_INVALIDARG;
910 case NS_ERROR_UNEXPECTED:
911 return E_UNEXPECTED;
912 case NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR:
913 return 0x80700007; /* according to tests */
915 return E_FAIL;
918 HRESULT return_nsstr(nsresult nsres, nsAString *nsstr, BSTR *p)
920 const PRUnichar *str;
921 HRESULT hres = S_OK;
923 if(NS_FAILED(nsres)) {
924 WARN("failed: %08lx\n", nsres);
925 nsAString_Finish(nsstr);
926 return map_nsresult(nsres);
929 nsAString_GetData(nsstr, &str);
930 TRACE("ret %s\n", debugstr_w(str));
931 if(*str) {
932 *p = SysAllocString(str);
933 if(!*p)
934 hres = E_OUTOFMEMORY;
935 }else {
936 *p = NULL;
939 nsAString_Finish(nsstr);
940 return hres;
943 HRESULT return_nsstr_variant(nsresult nsres, nsAString *nsstr, unsigned flags, VARIANT *p)
945 HRESULT hres = S_OK;
947 if(NS_FAILED(nsres)) {
948 ERR("failed: %08lx\n", nsres);
949 nsAString_Finish(nsstr);
950 return map_nsresult(nsres);
953 if(NS_StringGetIsVoid(nsstr)) {
954 V_VT(p) = VT_NULL;
955 }else {
956 const WCHAR *str;
957 size_t len;
958 nsAString_GetData(nsstr, &str);
959 len = wcslen(str);
960 if(flags & NSSTR_IMPLICIT_PX) {
961 const WCHAR *iter;
962 if(len > 2 && !wcscmp(str + len - 2, L"px"))
963 len -= 2;
964 for(iter = str; iter < str + len && is_digit(*iter); iter++);
965 if(*iter == '.') {
966 const WCHAR *dot = iter++;
967 while(iter < str + len && is_digit(*iter)) iter++;
968 if(iter == str + len && dot) len = dot - str;
971 if(flags & NSSTR_COLOR) {
972 hres = nscolor_to_str(str, &V_BSTR(p));
973 }else if(*str) {
974 V_BSTR(p) = SysAllocStringLen(str, len);
975 if(!V_BSTR(p))
976 hres = E_OUTOFMEMORY;
977 }else {
978 V_BSTR(p) = NULL;
980 if(SUCCEEDED(hres))
981 V_VT(p) = VT_BSTR;
984 nsAString_Finish(nsstr);
985 if(FAILED(hres))
986 return hres;
987 TRACE("ret %s\n", debugstr_variant(p));
988 return S_OK;
992 * Converts VARIANT to string and stores the result in nsAString. To avoid useless
993 * allocations, the function uses an existing string if available, so caller must
994 * ensure that passed VARIANT is unchanged as long as its string representation is used
996 HRESULT variant_to_nsstr(VARIANT *v, BOOL hex_int, nsAString *nsstr)
998 WCHAR buf[32];
1000 switch(V_VT(v)) {
1001 case VT_EMPTY:
1002 case VT_NULL:
1003 nsAString_InitDepend(nsstr, NULL);
1004 return S_OK;
1006 case VT_BSTR:
1007 nsAString_InitDepend(nsstr, V_BSTR(v));
1008 break;
1010 case VT_BSTR|VT_BYREF:
1011 nsAString_InitDepend(nsstr, *V_BSTRREF(v));
1012 break;
1014 case VT_I4:
1015 wsprintfW(buf, hex_int ? L"#%06x" : L"%d", V_I4(v));
1016 nsAString_Init(nsstr, buf);
1017 break;
1019 case VT_R8:
1020 case VT_DISPATCH: {
1021 VARIANT strv;
1022 HRESULT hres;
1024 V_VT(&strv) = VT_EMPTY;
1025 hres = VariantChangeTypeEx(&strv, v, MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT),
1026 0, VT_BSTR);
1027 if(FAILED(hres))
1028 return hres;
1030 nsAString_Init(nsstr, V_BSTR(&strv));
1031 SysFreeString(V_BSTR(&strv));
1032 break;
1035 default:
1036 FIXME("not implemented for %s\n", debugstr_variant(v));
1037 return E_NOTIMPL;
1040 return S_OK;
1043 nsICommandParams *create_nscommand_params(void)
1045 nsICommandParams *ret = NULL;
1046 nsresult nsres;
1048 if(!pCompMgr)
1049 return NULL;
1051 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
1052 NS_COMMANDPARAMS_CONTRACTID, NULL, &IID_nsICommandParams,
1053 (void**)&ret);
1054 if(NS_FAILED(nsres))
1055 ERR("Could not get nsICommandParams\n");
1057 return ret;
1060 nsIWritableVariant *create_nsvariant(void)
1062 nsIWritableVariant *ret = NULL;
1063 nsresult nsres;
1065 if(!pCompMgr)
1066 return NULL;
1068 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
1069 NS_VARIANT_CONTRACTID, NULL, &IID_nsIWritableVariant, (void**)&ret);
1070 if(NS_FAILED(nsres))
1071 ERR("Could not get nsIVariant\n");
1073 return ret;
1076 char *get_nscategory_entry(const char *category, const char *entry)
1078 char *ret = NULL;
1079 nsresult nsres;
1081 nsres = nsICategoryManager_GetCategoryEntry(cat_mgr, category, entry, &ret);
1082 return NS_SUCCEEDED(nsres) ? ret : NULL;
1085 nsresult get_nsinterface(nsISupports *iface, REFIID riid, void **ppv)
1087 nsIInterfaceRequestor *iface_req;
1088 nsresult nsres;
1090 nsres = nsISupports_QueryInterface(iface, &IID_nsIInterfaceRequestor, (void**)&iface_req);
1091 if(NS_FAILED(nsres))
1092 return nsres;
1094 nsres = nsIInterfaceRequestor_GetInterface(iface_req, riid, ppv);
1095 nsIInterfaceRequestor_Release(iface_req);
1097 return nsres;
1100 static HRESULT nsnode_to_nsstring_rec(nsIContentSerializer *serializer, nsIDOMNode *nsnode, nsAString *str)
1102 nsIDOMNodeList *node_list = NULL;
1103 cpp_bool has_children = FALSE;
1104 nsIContent *nscontent;
1105 UINT16 type;
1106 nsresult nsres;
1108 nsIDOMNode_HasChildNodes(nsnode, &has_children);
1110 nsres = nsIDOMNode_GetNodeType(nsnode, &type);
1111 if(NS_FAILED(nsres)) {
1112 ERR("GetType failed: %08lx\n", nsres);
1113 return E_FAIL;
1116 if(type != DOCUMENT_NODE) {
1117 nsres = nsIDOMNode_QueryInterface(nsnode, &IID_nsIContent, (void**)&nscontent);
1118 if(NS_FAILED(nsres)) {
1119 ERR("Could not get nsIContent interface: %08lx\n", nsres);
1120 return E_FAIL;
1124 switch(type) {
1125 case ELEMENT_NODE:
1126 nsIContentSerializer_AppendElementStart(serializer, nscontent, nscontent, str);
1127 break;
1128 case TEXT_NODE:
1129 nsIContentSerializer_AppendText(serializer, nscontent, 0, -1, str);
1130 break;
1131 case COMMENT_NODE:
1132 nsIContentSerializer_AppendComment(serializer, nscontent, 0, -1, str);
1133 break;
1134 case DOCUMENT_NODE: {
1135 nsIDocument *nsdoc;
1136 nsIDOMNode_QueryInterface(nsnode, &IID_nsIDocument, (void**)&nsdoc);
1137 nsIContentSerializer_AppendDocumentStart(serializer, nsdoc, str);
1138 nsIDocument_Release(nsdoc);
1139 break;
1141 case DOCUMENT_TYPE_NODE:
1142 nsIContentSerializer_AppendDoctype(serializer, nscontent, str);
1143 break;
1144 case DOCUMENT_FRAGMENT_NODE:
1145 break;
1146 default:
1147 FIXME("Unhandled type %u\n", type);
1150 if(has_children) {
1151 UINT32 child_cnt, i;
1152 nsIDOMNode *child_node;
1154 nsIDOMNode_GetChildNodes(nsnode, &node_list);
1155 nsIDOMNodeList_GetLength(node_list, &child_cnt);
1157 for(i=0; i<child_cnt; i++) {
1158 nsres = nsIDOMNodeList_Item(node_list, i, &child_node);
1159 if(NS_SUCCEEDED(nsres)) {
1160 nsnode_to_nsstring_rec(serializer, child_node, str);
1161 nsIDOMNode_Release(child_node);
1162 }else {
1163 ERR("Item failed: %08lx\n", nsres);
1167 nsIDOMNodeList_Release(node_list);
1170 if(type == ELEMENT_NODE)
1171 nsIContentSerializer_AppendElementEnd(serializer, nscontent, str);
1173 if(type != DOCUMENT_NODE)
1174 nsIContent_Release(nscontent);
1175 return S_OK;
1178 HRESULT nsnode_to_nsstring(nsIDOMNode *nsnode, nsAString *str)
1180 nsIContentSerializer *serializer;
1181 nsresult nsres;
1182 HRESULT hres;
1184 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
1185 NS_HTMLSERIALIZER_CONTRACTID, NULL, &IID_nsIContentSerializer,
1186 (void**)&serializer);
1187 if(NS_FAILED(nsres)) {
1188 ERR("Could not get nsIContentSerializer: %08lx\n", nsres);
1189 return E_FAIL;
1192 nsres = nsIContentSerializer_Init(serializer, 0, 100, NULL, FALSE, FALSE /* FIXME */);
1193 if(NS_FAILED(nsres))
1194 ERR("Init failed: %08lx\n", nsres);
1196 hres = nsnode_to_nsstring_rec(serializer, nsnode, str);
1197 if(SUCCEEDED(hres)) {
1198 nsres = nsIContentSerializer_Flush(serializer, str);
1199 if(NS_FAILED(nsres))
1200 ERR("Flush failed: %08lx\n", nsres);
1203 nsIContentSerializer_Release(serializer);
1204 return hres;
1207 void setup_editor_controller(GeckoBrowser *This)
1209 nsIEditingSession *editing_session = NULL;
1210 nsIControllerContext *ctrlctx;
1211 nsresult nsres;
1213 if(This->editor) {
1214 nsIEditor_Release(This->editor);
1215 This->editor = NULL;
1218 if(This->editor_controller) {
1219 nsIController_Release(This->editor_controller);
1220 This->editor_controller = NULL;
1223 nsres = get_nsinterface((nsISupports*)This->webbrowser, &IID_nsIEditingSession,
1224 (void**)&editing_session);
1225 if(NS_FAILED(nsres)) {
1226 ERR("Could not get nsIEditingSession: %08lx\n", nsres);
1227 return;
1230 nsres = nsIEditingSession_GetEditorForWindow(editing_session,
1231 This->doc->window->window_proxy, &This->editor);
1232 nsIEditingSession_Release(editing_session);
1233 if(NS_FAILED(nsres)) {
1234 ERR("Could not get editor: %08lx\n", nsres);
1235 return;
1238 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
1239 NS_EDITORCONTROLLER_CONTRACTID, NULL, &IID_nsIControllerContext, (void**)&ctrlctx);
1240 if(NS_SUCCEEDED(nsres)) {
1241 nsres = nsIControllerContext_SetCommandContext(ctrlctx, (nsISupports *)This->editor);
1242 if(NS_FAILED(nsres))
1243 ERR("SetCommandContext failed: %08lx\n", nsres);
1244 nsres = nsIControllerContext_QueryInterface(ctrlctx, &IID_nsIController,
1245 (void**)&This->editor_controller);
1246 nsIControllerContext_Release(ctrlctx);
1247 if(NS_FAILED(nsres))
1248 ERR("Could not get nsIController interface: %08lx\n", nsres);
1249 }else {
1250 ERR("Could not create edit controller: %08lx\n", nsres);
1254 void close_gecko(void)
1256 TRACE("()\n");
1258 release_nsio();
1259 init_mutation(NULL);
1261 if(profile_directory) {
1262 nsIFile_Release(profile_directory);
1263 profile_directory = NULL;
1266 if(plugin_directory) {
1267 nsIFile_Release(plugin_directory);
1268 plugin_directory = NULL;
1271 if(pCompMgr)
1272 nsIComponentManager_Release(pCompMgr);
1274 if(pServMgr)
1275 nsIServiceManager_Release(pServMgr);
1277 if(cat_mgr)
1278 nsICategoryManager_Release(cat_mgr);
1280 /* Gecko doesn't really support being unloaded */
1281 /* if (hXPCOM) FreeLibrary(hXPCOM); */
1283 DeleteCriticalSection(&cs_load_gecko);
1286 BOOL is_gecko_path(const char *path)
1288 WCHAR *buf, *ptr;
1289 BOOL ret;
1291 buf = heap_strdupUtoW(path);
1292 if(!buf || lstrlenW(buf) < gecko_path_len)
1293 return FALSE;
1295 for(ptr = buf; *ptr; ptr++) {
1296 if(*ptr == '\\')
1297 *ptr = '/';
1300 UrlUnescapeW(buf, NULL, NULL, URL_UNESCAPE_INPLACE);
1301 buf[gecko_path_len] = 0;
1303 ret = !wcsicmp(buf, gecko_path);
1304 heap_free(buf);
1305 return ret;
1308 void set_viewer_zoom(GeckoBrowser *browser, float factor)
1310 nsIContentViewer *content_viewer;
1311 nsIDocShell *doc_shell;
1312 nsresult nsres;
1314 TRACE("Setting to %f\n", factor);
1316 nsres = get_nsinterface((nsISupports*)browser->navigation, &IID_nsIDocShell, (void**)&doc_shell);
1317 assert(nsres == NS_OK);
1319 nsres = nsIDocShell_GetContentViewer(doc_shell, &content_viewer);
1320 assert(nsres == NS_OK && content_viewer);
1321 nsIDocShell_Release(doc_shell);
1323 nsres = nsIContentViewer_SetFullZoom(content_viewer, factor);
1324 if(NS_FAILED(nsres))
1325 ERR("SetFullZoom failed: %08lx\n", nsres);
1327 nsIContentViewer_Release(content_viewer);
1330 float get_viewer_zoom(GeckoBrowser *browser)
1332 nsIContentViewer *content_viewer;
1333 nsIDocShell *doc_shell;
1334 nsresult nsres;
1335 float factor;
1337 nsres = get_nsinterface((nsISupports*)browser->navigation, &IID_nsIDocShell, (void**)&doc_shell);
1338 assert(nsres == NS_OK);
1340 nsres = nsIDocShell_GetContentViewer(doc_shell, &content_viewer);
1341 assert(nsres == NS_OK && content_viewer);
1342 nsIDocShell_Release(doc_shell);
1344 nsres = nsIContentViewer_GetFullZoom(content_viewer, &factor);
1345 if(NS_FAILED(nsres))
1346 ERR("GetFullZoom failed: %08lx\n", nsres);
1347 TRACE("Got %f\n", factor);
1349 nsIContentViewer_Release(content_viewer);
1350 return factor;
1353 struct nsWeakReference {
1354 nsIWeakReference nsIWeakReference_iface;
1356 LONG ref;
1358 GeckoBrowser *browser;
1361 static inline nsWeakReference *impl_from_nsIWeakReference(nsIWeakReference *iface)
1363 return CONTAINING_RECORD(iface, nsWeakReference, nsIWeakReference_iface);
1366 static nsresult NSAPI nsWeakReference_QueryInterface(nsIWeakReference *iface,
1367 nsIIDRef riid, void **result)
1369 nsWeakReference *This = impl_from_nsIWeakReference(iface);
1371 if(IsEqualGUID(&IID_nsISupports, riid)) {
1372 TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
1373 *result = &This->nsIWeakReference_iface;
1374 }else if(IsEqualGUID(&IID_nsIWeakReference, riid)) {
1375 TRACE("(%p)->(IID_nsIWeakReference %p)\n", This, result);
1376 *result = &This->nsIWeakReference_iface;
1377 }else {
1378 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
1379 *result = NULL;
1380 return NS_NOINTERFACE;
1383 nsISupports_AddRef((nsISupports*)*result);
1384 return NS_OK;
1387 static nsrefcnt NSAPI nsWeakReference_AddRef(nsIWeakReference *iface)
1389 nsWeakReference *This = impl_from_nsIWeakReference(iface);
1390 LONG ref = InterlockedIncrement(&This->ref);
1392 TRACE("(%p) ref=%ld\n", This, ref);
1394 return ref;
1397 static nsrefcnt NSAPI nsWeakReference_Release(nsIWeakReference *iface)
1399 nsWeakReference *This = impl_from_nsIWeakReference(iface);
1400 LONG ref = InterlockedDecrement(&This->ref);
1402 TRACE("(%p) ref=%ld\n", This, ref);
1404 if(!ref) {
1405 assert(!This->browser);
1406 heap_free(This);
1409 return ref;
1412 static nsresult NSAPI nsWeakReference_QueryReferent(nsIWeakReference *iface,
1413 const nsIID *riid, void **result)
1415 nsWeakReference *This = impl_from_nsIWeakReference(iface);
1417 if(!This->browser)
1418 return NS_ERROR_NULL_POINTER;
1420 return nsIWebBrowserChrome_QueryInterface(&This->browser->nsIWebBrowserChrome_iface, riid, result);
1423 static const nsIWeakReferenceVtbl nsWeakReferenceVtbl = {
1424 nsWeakReference_QueryInterface,
1425 nsWeakReference_AddRef,
1426 nsWeakReference_Release,
1427 nsWeakReference_QueryReferent
1430 /**********************************************************
1431 * nsIWebBrowserChrome interface
1434 static inline GeckoBrowser *impl_from_nsIWebBrowserChrome(nsIWebBrowserChrome *iface)
1436 return CONTAINING_RECORD(iface, GeckoBrowser, nsIWebBrowserChrome_iface);
1439 static nsresult NSAPI nsWebBrowserChrome_QueryInterface(nsIWebBrowserChrome *iface,
1440 nsIIDRef riid, void **result)
1442 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1444 *result = NULL;
1445 if(IsEqualGUID(&IID_nsISupports, riid)) {
1446 TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
1447 *result = &This->nsIWebBrowserChrome_iface;
1448 }else if(IsEqualGUID(&IID_nsIWebBrowserChrome, riid)) {
1449 TRACE("(%p)->(IID_nsIWebBrowserChrome, %p)\n", This, result);
1450 *result = &This->nsIWebBrowserChrome_iface;
1451 }else if(IsEqualGUID(&IID_nsIContextMenuListener, riid)) {
1452 TRACE("(%p)->(IID_nsIContextMenuListener, %p)\n", This, result);
1453 *result = &This->nsIContextMenuListener_iface;
1454 }else if(IsEqualGUID(&IID_nsIURIContentListener, riid)) {
1455 TRACE("(%p)->(IID_nsIURIContentListener %p)\n", This, result);
1456 *result = &This->nsIURIContentListener_iface;
1457 }else if(IsEqualGUID(&IID_nsIEmbeddingSiteWindow, riid)) {
1458 TRACE("(%p)->(IID_nsIEmbeddingSiteWindow %p)\n", This, result);
1459 *result = &This->nsIEmbeddingSiteWindow_iface;
1460 }else if(IsEqualGUID(&IID_nsITooltipListener, riid)) {
1461 TRACE("(%p)->(IID_nsITooltipListener %p)\n", This, result);
1462 *result = &This->nsITooltipListener_iface;
1463 }else if(IsEqualGUID(&IID_nsIInterfaceRequestor, riid)) {
1464 TRACE("(%p)->(IID_nsIInterfaceRequestor %p)\n", This, result);
1465 *result = &This->nsIInterfaceRequestor_iface;
1466 }else if(IsEqualGUID(&IID_nsISupportsWeakReference, riid)) {
1467 TRACE("(%p)->(IID_nsISupportsWeakReference %p)\n", This, result);
1468 *result = &This->nsISupportsWeakReference_iface;
1471 if(*result) {
1472 nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1473 return NS_OK;
1476 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
1477 return NS_NOINTERFACE;
1480 static nsrefcnt NSAPI nsWebBrowserChrome_AddRef(nsIWebBrowserChrome *iface)
1482 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1483 LONG ref = InterlockedIncrement(&This->ref);
1485 TRACE("(%p) ref=%ld\n", This, ref);
1487 return ref;
1490 static nsrefcnt NSAPI nsWebBrowserChrome_Release(nsIWebBrowserChrome *iface)
1492 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1493 LONG ref = InterlockedDecrement(&This->ref);
1495 TRACE("(%p) ref=%ld\n", This, ref);
1497 if(!ref) {
1498 if(This->doc)
1499 detach_gecko_browser(This);
1500 if(This->weak_reference) {
1501 This->weak_reference->browser = NULL;
1502 nsIWeakReference_Release(&This->weak_reference->nsIWeakReference_iface);
1504 heap_free(This);
1507 return ref;
1510 static nsresult NSAPI nsWebBrowserChrome_SetStatus(nsIWebBrowserChrome *iface,
1511 UINT32 statusType, const PRUnichar *status)
1513 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1514 TRACE("(%p)->(%d %s)\n", This, statusType, debugstr_w(status));
1515 return NS_OK;
1518 static nsresult NSAPI nsWebBrowserChrome_GetWebBrowser(nsIWebBrowserChrome *iface,
1519 nsIWebBrowser **aWebBrowser)
1521 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1523 TRACE("(%p)->(%p)\n", This, aWebBrowser);
1525 if(!aWebBrowser)
1526 return NS_ERROR_INVALID_ARG;
1528 if(This->webbrowser)
1529 nsIWebBrowser_AddRef(This->webbrowser);
1530 *aWebBrowser = This->webbrowser;
1531 return S_OK;
1534 static nsresult NSAPI nsWebBrowserChrome_SetWebBrowser(nsIWebBrowserChrome *iface,
1535 nsIWebBrowser *aWebBrowser)
1537 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1539 TRACE("(%p)->(%p)\n", This, aWebBrowser);
1541 if(aWebBrowser != This->webbrowser)
1542 ERR("Wrong nsWebBrowser!\n");
1544 return NS_OK;
1547 static nsresult NSAPI nsWebBrowserChrome_GetChromeFlags(nsIWebBrowserChrome *iface,
1548 UINT32 *aChromeFlags)
1550 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1551 WARN("(%p)->(%p)\n", This, aChromeFlags);
1552 return NS_ERROR_NOT_IMPLEMENTED;
1555 static nsresult NSAPI nsWebBrowserChrome_SetChromeFlags(nsIWebBrowserChrome *iface,
1556 UINT32 aChromeFlags)
1558 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1559 WARN("(%p)->(%08x)\n", This, aChromeFlags);
1560 return NS_ERROR_NOT_IMPLEMENTED;
1563 static nsresult NSAPI nsWebBrowserChrome_DestroyBrowserWindow(nsIWebBrowserChrome *iface)
1565 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1566 TRACE("(%p)\n", This);
1567 return NS_ERROR_NOT_IMPLEMENTED;
1570 static nsresult NSAPI nsWebBrowserChrome_SizeBrowserTo(nsIWebBrowserChrome *iface,
1571 LONG aCX, LONG aCY)
1573 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1574 WARN("(%p)->(%ld %ld)\n", This, aCX, aCY);
1575 return NS_ERROR_NOT_IMPLEMENTED;
1578 static nsresult NSAPI nsWebBrowserChrome_ShowAsModal(nsIWebBrowserChrome *iface)
1580 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1581 WARN("(%p)\n", This);
1582 return NS_ERROR_NOT_IMPLEMENTED;
1585 static nsresult NSAPI nsWebBrowserChrome_IsWindowModal(nsIWebBrowserChrome *iface, cpp_bool *_retval)
1587 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1588 WARN("(%p)->(%p)\n", This, _retval);
1589 return NS_ERROR_NOT_IMPLEMENTED;
1592 static nsresult NSAPI nsWebBrowserChrome_ExitModalEventLoop(nsIWebBrowserChrome *iface,
1593 nsresult aStatus)
1595 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1596 WARN("(%p)->(%08lx)\n", This, aStatus);
1597 return NS_ERROR_NOT_IMPLEMENTED;
1600 static const nsIWebBrowserChromeVtbl nsWebBrowserChromeVtbl = {
1601 nsWebBrowserChrome_QueryInterface,
1602 nsWebBrowserChrome_AddRef,
1603 nsWebBrowserChrome_Release,
1604 nsWebBrowserChrome_SetStatus,
1605 nsWebBrowserChrome_GetWebBrowser,
1606 nsWebBrowserChrome_SetWebBrowser,
1607 nsWebBrowserChrome_GetChromeFlags,
1608 nsWebBrowserChrome_SetChromeFlags,
1609 nsWebBrowserChrome_DestroyBrowserWindow,
1610 nsWebBrowserChrome_SizeBrowserTo,
1611 nsWebBrowserChrome_ShowAsModal,
1612 nsWebBrowserChrome_IsWindowModal,
1613 nsWebBrowserChrome_ExitModalEventLoop
1616 /**********************************************************
1617 * nsIContextMenuListener interface
1620 static inline GeckoBrowser *impl_from_nsIContextMenuListener(nsIContextMenuListener *iface)
1622 return CONTAINING_RECORD(iface, GeckoBrowser, nsIContextMenuListener_iface);
1625 static nsresult NSAPI nsContextMenuListener_QueryInterface(nsIContextMenuListener *iface,
1626 nsIIDRef riid, void **result)
1628 GeckoBrowser *This = impl_from_nsIContextMenuListener(iface);
1629 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1632 static nsrefcnt NSAPI nsContextMenuListener_AddRef(nsIContextMenuListener *iface)
1634 GeckoBrowser *This = impl_from_nsIContextMenuListener(iface);
1635 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1638 static nsrefcnt NSAPI nsContextMenuListener_Release(nsIContextMenuListener *iface)
1640 GeckoBrowser *This = impl_from_nsIContextMenuListener(iface);
1641 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
1644 static nsresult NSAPI nsContextMenuListener_OnShowContextMenu(nsIContextMenuListener *iface,
1645 UINT32 aContextFlags, nsIDOMEvent *aEvent, nsIDOMNode *aNode)
1647 GeckoBrowser *This = impl_from_nsIContextMenuListener(iface);
1648 nsIDOMMouseEvent *mouse_event;
1649 HTMLDOMNode *node;
1650 DOMEvent *event;
1651 POINT pt;
1652 DWORD dwID = CONTEXT_MENU_DEFAULT;
1653 nsresult nsres;
1654 HRESULT hres;
1656 TRACE("(%p)->(%08x %p %p)\n", This, aContextFlags, aEvent, aNode);
1658 if (!aNode)
1659 return NS_ERROR_FAILURE;
1661 hres = get_node(aNode, TRUE, &node);
1662 if(FAILED(hres))
1663 return NS_ERROR_FAILURE;
1665 hres = create_event_from_nsevent(aEvent, dispex_compat_mode(&node->event_target.dispex), &event);
1666 if(SUCCEEDED(hres)) {
1667 dispatch_event(&node->event_target, event);
1668 IDOMEvent_Release(&event->IDOMEvent_iface);
1671 nsres = nsIDOMEvent_QueryInterface(aEvent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
1672 assert(NS_SUCCEEDED(nsres));
1674 nsIDOMMouseEvent_GetScreenX(mouse_event, &pt.x);
1675 nsIDOMMouseEvent_GetScreenY(mouse_event, &pt.y);
1676 nsIDOMMouseEvent_Release(mouse_event);
1678 switch(aContextFlags) {
1679 case CONTEXT_NONE:
1680 case CONTEXT_DOCUMENT:
1681 case CONTEXT_TEXT: {
1682 nsISelection *selection;
1684 nsres = nsIDOMHTMLDocument_GetSelection(This->doc->doc_node->nsdoc, &selection);
1685 if(NS_SUCCEEDED(nsres) && selection) {
1686 cpp_bool is_collapsed;
1688 /* FIXME: Check if the click was inside selection. */
1689 nsres = nsISelection_GetIsCollapsed(selection, &is_collapsed);
1690 nsISelection_Release(selection);
1691 if(NS_SUCCEEDED(nsres) && !is_collapsed)
1692 dwID = CONTEXT_MENU_TEXTSELECT;
1694 break;
1696 case CONTEXT_IMAGE:
1697 case CONTEXT_IMAGE|CONTEXT_LINK:
1698 dwID = CONTEXT_MENU_IMAGE;
1699 break;
1700 case CONTEXT_LINK:
1701 dwID = CONTEXT_MENU_ANCHOR;
1702 break;
1703 case CONTEXT_INPUT:
1704 dwID = CONTEXT_MENU_CONTROL;
1705 break;
1706 default:
1707 FIXME("aContextFlags=%08x\n", aContextFlags);
1710 show_context_menu(This->doc, dwID, &pt, (IDispatch*)&node->IHTMLDOMNode_iface);
1711 node_release(node);
1712 return NS_OK;
1715 static const nsIContextMenuListenerVtbl nsContextMenuListenerVtbl = {
1716 nsContextMenuListener_QueryInterface,
1717 nsContextMenuListener_AddRef,
1718 nsContextMenuListener_Release,
1719 nsContextMenuListener_OnShowContextMenu
1722 /**********************************************************
1723 * nsIURIContentListener interface
1726 static inline GeckoBrowser *impl_from_nsIURIContentListener(nsIURIContentListener *iface)
1728 return CONTAINING_RECORD(iface, GeckoBrowser, nsIURIContentListener_iface);
1731 static nsresult NSAPI nsURIContentListener_QueryInterface(nsIURIContentListener *iface,
1732 nsIIDRef riid, void **result)
1734 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1735 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1738 static nsrefcnt NSAPI nsURIContentListener_AddRef(nsIURIContentListener *iface)
1740 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1741 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1744 static nsrefcnt NSAPI nsURIContentListener_Release(nsIURIContentListener *iface)
1746 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1747 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
1750 static nsresult NSAPI nsURIContentListener_OnStartURIOpen(nsIURIContentListener *iface,
1751 nsIURI *aURI, cpp_bool *_retval)
1753 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1754 nsACString spec_str;
1755 const char *spec;
1757 nsACString_Init(&spec_str, NULL);
1758 nsIURI_GetSpec(aURI, &spec_str);
1759 nsACString_GetData(&spec_str, &spec);
1761 TRACE("(%p)->(%p(%s) %p)\n", This, aURI, debugstr_a(spec), _retval);
1763 nsACString_Finish(&spec_str);
1765 return This->content_listener
1766 ? nsIURIContentListener_OnStartURIOpen(This->content_listener, aURI, _retval)
1767 : NS_OK;
1770 static nsresult NSAPI nsURIContentListener_DoContent(nsIURIContentListener *iface,
1771 const nsACString *aContentType, cpp_bool aIsContentPreferred, nsIRequest *aRequest,
1772 nsIStreamListener **aContentHandler, cpp_bool *_retval)
1774 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1776 TRACE("(%p)->(%p %x %p %p %p)\n", This, aContentType, aIsContentPreferred,
1777 aRequest, aContentHandler, _retval);
1779 return This->content_listener
1780 ? nsIURIContentListener_DoContent(This->content_listener, aContentType,
1781 aIsContentPreferred, aRequest, aContentHandler, _retval)
1782 : NS_ERROR_NOT_IMPLEMENTED;
1785 static nsresult NSAPI nsURIContentListener_IsPreferred(nsIURIContentListener *iface,
1786 const char *aContentType, char **aDesiredContentType, cpp_bool *_retval)
1788 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1790 TRACE("(%p)->(%s %p %p)\n", This, debugstr_a(aContentType), aDesiredContentType, _retval);
1792 /* FIXME: Should we do something here? */
1793 *_retval = TRUE;
1795 return This->content_listener
1796 ? nsIURIContentListener_IsPreferred(This->content_listener, aContentType,
1797 aDesiredContentType, _retval)
1798 : NS_OK;
1801 static nsresult NSAPI nsURIContentListener_CanHandleContent(nsIURIContentListener *iface,
1802 const char *aContentType, cpp_bool aIsContentPreferred, char **aDesiredContentType,
1803 cpp_bool *_retval)
1805 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1807 TRACE("(%p)->(%s %x %p %p)\n", This, debugstr_a(aContentType), aIsContentPreferred,
1808 aDesiredContentType, _retval);
1810 return This->content_listener
1811 ? nsIURIContentListener_CanHandleContent(This->content_listener, aContentType,
1812 aIsContentPreferred, aDesiredContentType, _retval)
1813 : NS_ERROR_NOT_IMPLEMENTED;
1816 static nsresult NSAPI nsURIContentListener_GetLoadCookie(nsIURIContentListener *iface,
1817 nsISupports **aLoadCookie)
1819 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1821 WARN("(%p)->(%p)\n", This, aLoadCookie);
1823 return This->content_listener
1824 ? nsIURIContentListener_GetLoadCookie(This->content_listener, aLoadCookie)
1825 : NS_ERROR_NOT_IMPLEMENTED;
1828 static nsresult NSAPI nsURIContentListener_SetLoadCookie(nsIURIContentListener *iface,
1829 nsISupports *aLoadCookie)
1831 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1833 WARN("(%p)->(%p)\n", This, aLoadCookie);
1835 return This->content_listener
1836 ? nsIURIContentListener_SetLoadCookie(This->content_listener, aLoadCookie)
1837 : NS_ERROR_NOT_IMPLEMENTED;
1840 static nsresult NSAPI nsURIContentListener_GetParentContentListener(nsIURIContentListener *iface,
1841 nsIURIContentListener **aParentContentListener)
1843 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1845 TRACE("(%p)->(%p)\n", This, aParentContentListener);
1847 if(This->content_listener)
1848 nsIURIContentListener_AddRef(This->content_listener);
1850 *aParentContentListener = This->content_listener;
1851 return NS_OK;
1854 static nsresult NSAPI nsURIContentListener_SetParentContentListener(nsIURIContentListener *iface,
1855 nsIURIContentListener *aParentContentListener)
1857 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1859 TRACE("(%p)->(%p)\n", This, aParentContentListener);
1861 if(aParentContentListener == &This->nsIURIContentListener_iface)
1862 return NS_OK;
1864 if(This->content_listener)
1865 nsIURIContentListener_Release(This->content_listener);
1867 This->content_listener = aParentContentListener;
1868 if(This->content_listener)
1869 nsIURIContentListener_AddRef(This->content_listener);
1871 return NS_OK;
1874 static const nsIURIContentListenerVtbl nsURIContentListenerVtbl = {
1875 nsURIContentListener_QueryInterface,
1876 nsURIContentListener_AddRef,
1877 nsURIContentListener_Release,
1878 nsURIContentListener_OnStartURIOpen,
1879 nsURIContentListener_DoContent,
1880 nsURIContentListener_IsPreferred,
1881 nsURIContentListener_CanHandleContent,
1882 nsURIContentListener_GetLoadCookie,
1883 nsURIContentListener_SetLoadCookie,
1884 nsURIContentListener_GetParentContentListener,
1885 nsURIContentListener_SetParentContentListener
1888 /**********************************************************
1889 * nsIEmbeddinSiteWindow interface
1892 static inline GeckoBrowser *impl_from_nsIEmbeddingSiteWindow(nsIEmbeddingSiteWindow *iface)
1894 return CONTAINING_RECORD(iface, GeckoBrowser, nsIEmbeddingSiteWindow_iface);
1897 static nsresult NSAPI nsEmbeddingSiteWindow_QueryInterface(nsIEmbeddingSiteWindow *iface,
1898 nsIIDRef riid, void **result)
1900 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1901 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1904 static nsrefcnt NSAPI nsEmbeddingSiteWindow_AddRef(nsIEmbeddingSiteWindow *iface)
1906 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1907 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1910 static nsrefcnt NSAPI nsEmbeddingSiteWindow_Release(nsIEmbeddingSiteWindow *iface)
1912 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1913 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
1916 static nsresult NSAPI nsEmbeddingSiteWindow_SetDimensions(nsIEmbeddingSiteWindow *iface,
1917 UINT32 flags, LONG x, LONG y, LONG cx, LONG cy)
1919 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1920 WARN("(%p)->(%08x %ld %ld %ld %ld)\n", This, flags, x, y, cx, cy);
1921 return NS_ERROR_NOT_IMPLEMENTED;
1924 static nsresult NSAPI nsEmbeddingSiteWindow_GetDimensions(nsIEmbeddingSiteWindow *iface,
1925 UINT32 flags, LONG *x, LONG *y, LONG *cx, LONG *cy)
1927 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1928 RECT r;
1930 TRACE("(%p)->(%x %p %p %p %p)\n", This, flags, x, y, cx, cy);
1932 if(!GetWindowRect(This->hwnd, &r)) {
1933 ERR("GetWindowRect failed\n");
1934 return NS_ERROR_FAILURE;
1937 if(x)
1938 *x = r.left;
1939 if(y)
1940 *y = r.top;
1941 if(cx)
1942 *cx = r.right-r.left;
1943 if(cy)
1944 *cy = r.bottom-r.top;
1945 return NS_OK;
1948 static nsresult NSAPI nsEmbeddingSiteWindow_SetFocus(nsIEmbeddingSiteWindow *iface)
1950 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1952 TRACE("(%p)\n", This);
1954 return nsIBaseWindow_SetFocus(This->window);
1957 static nsresult NSAPI nsEmbeddingSiteWindow_GetVisibility(nsIEmbeddingSiteWindow *iface,
1958 cpp_bool *aVisibility)
1960 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1962 TRACE("(%p)->(%p)\n", This, aVisibility);
1964 *aVisibility = This->doc && This->doc->hwnd && IsWindowVisible(This->doc->hwnd);
1965 return NS_OK;
1968 static nsresult NSAPI nsEmbeddingSiteWindow_SetVisibility(nsIEmbeddingSiteWindow *iface,
1969 cpp_bool aVisibility)
1971 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1973 TRACE("(%p)->(%x)\n", This, aVisibility);
1975 return NS_OK;
1978 static nsresult NSAPI nsEmbeddingSiteWindow_GetTitle(nsIEmbeddingSiteWindow *iface,
1979 PRUnichar **aTitle)
1981 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1982 WARN("(%p)->(%p)\n", This, aTitle);
1983 return NS_ERROR_NOT_IMPLEMENTED;
1986 static nsresult NSAPI nsEmbeddingSiteWindow_SetTitle(nsIEmbeddingSiteWindow *iface,
1987 const PRUnichar *aTitle)
1989 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1990 WARN("(%p)->(%s)\n", This, debugstr_w(aTitle));
1991 return NS_ERROR_NOT_IMPLEMENTED;
1994 static nsresult NSAPI nsEmbeddingSiteWindow_GetSiteWindow(nsIEmbeddingSiteWindow *iface,
1995 void **aSiteWindow)
1997 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1999 TRACE("(%p)->(%p)\n", This, aSiteWindow);
2001 *aSiteWindow = This->hwnd;
2002 return NS_OK;
2005 static const nsIEmbeddingSiteWindowVtbl nsEmbeddingSiteWindowVtbl = {
2006 nsEmbeddingSiteWindow_QueryInterface,
2007 nsEmbeddingSiteWindow_AddRef,
2008 nsEmbeddingSiteWindow_Release,
2009 nsEmbeddingSiteWindow_SetDimensions,
2010 nsEmbeddingSiteWindow_GetDimensions,
2011 nsEmbeddingSiteWindow_SetFocus,
2012 nsEmbeddingSiteWindow_GetVisibility,
2013 nsEmbeddingSiteWindow_SetVisibility,
2014 nsEmbeddingSiteWindow_GetTitle,
2015 nsEmbeddingSiteWindow_SetTitle,
2016 nsEmbeddingSiteWindow_GetSiteWindow
2019 static inline GeckoBrowser *impl_from_nsITooltipListener(nsITooltipListener *iface)
2021 return CONTAINING_RECORD(iface, GeckoBrowser, nsITooltipListener_iface);
2024 static nsresult NSAPI nsTooltipListener_QueryInterface(nsITooltipListener *iface, nsIIDRef riid,
2025 void **result)
2027 GeckoBrowser *This = impl_from_nsITooltipListener(iface);
2028 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
2031 static nsrefcnt NSAPI nsTooltipListener_AddRef(nsITooltipListener *iface)
2033 GeckoBrowser *This = impl_from_nsITooltipListener(iface);
2034 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
2037 static nsrefcnt NSAPI nsTooltipListener_Release(nsITooltipListener *iface)
2039 GeckoBrowser *This = impl_from_nsITooltipListener(iface);
2040 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
2043 static nsresult NSAPI nsTooltipListener_OnShowTooltip(nsITooltipListener *iface,
2044 LONG aXCoord, LONG aYCoord, const PRUnichar *aTipText)
2046 GeckoBrowser *This = impl_from_nsITooltipListener(iface);
2048 if (This->doc)
2049 show_tooltip(This->doc, aXCoord, aYCoord, aTipText);
2051 return NS_OK;
2054 static nsresult NSAPI nsTooltipListener_OnHideTooltip(nsITooltipListener *iface)
2056 GeckoBrowser *This = impl_from_nsITooltipListener(iface);
2058 if (This->doc)
2059 hide_tooltip(This->doc);
2061 return NS_OK;
2064 static const nsITooltipListenerVtbl nsTooltipListenerVtbl = {
2065 nsTooltipListener_QueryInterface,
2066 nsTooltipListener_AddRef,
2067 nsTooltipListener_Release,
2068 nsTooltipListener_OnShowTooltip,
2069 nsTooltipListener_OnHideTooltip
2072 static inline GeckoBrowser *impl_from_nsIInterfaceRequestor(nsIInterfaceRequestor *iface)
2074 return CONTAINING_RECORD(iface, GeckoBrowser, nsIInterfaceRequestor_iface);
2077 static nsresult NSAPI nsInterfaceRequestor_QueryInterface(nsIInterfaceRequestor *iface,
2078 nsIIDRef riid, void **result)
2080 GeckoBrowser *This = impl_from_nsIInterfaceRequestor(iface);
2081 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
2084 static nsrefcnt NSAPI nsInterfaceRequestor_AddRef(nsIInterfaceRequestor *iface)
2086 GeckoBrowser *This = impl_from_nsIInterfaceRequestor(iface);
2087 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
2090 static nsrefcnt NSAPI nsInterfaceRequestor_Release(nsIInterfaceRequestor *iface)
2092 GeckoBrowser *This = impl_from_nsIInterfaceRequestor(iface);
2093 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
2096 static nsresult NSAPI nsInterfaceRequestor_GetInterface(nsIInterfaceRequestor *iface,
2097 nsIIDRef riid, void **result)
2099 GeckoBrowser *This = impl_from_nsIInterfaceRequestor(iface);
2101 if(IsEqualGUID(&IID_mozIDOMWindowProxy, riid)) {
2102 TRACE("(%p)->(IID_nsIDOMWindow %p)\n", This, result);
2103 return nsIWebBrowser_GetContentDOMWindow(This->webbrowser, (mozIDOMWindowProxy**)result);
2106 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
2109 static const nsIInterfaceRequestorVtbl nsInterfaceRequestorVtbl = {
2110 nsInterfaceRequestor_QueryInterface,
2111 nsInterfaceRequestor_AddRef,
2112 nsInterfaceRequestor_Release,
2113 nsInterfaceRequestor_GetInterface
2116 static inline GeckoBrowser *impl_from_nsISupportsWeakReference(nsISupportsWeakReference *iface)
2118 return CONTAINING_RECORD(iface, GeckoBrowser, nsISupportsWeakReference_iface);
2121 static nsresult NSAPI nsSupportsWeakReference_QueryInterface(nsISupportsWeakReference *iface,
2122 nsIIDRef riid, void **result)
2124 GeckoBrowser *This = impl_from_nsISupportsWeakReference(iface);
2125 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
2128 static nsrefcnt NSAPI nsSupportsWeakReference_AddRef(nsISupportsWeakReference *iface)
2130 GeckoBrowser *This = impl_from_nsISupportsWeakReference(iface);
2131 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
2134 static nsrefcnt NSAPI nsSupportsWeakReference_Release(nsISupportsWeakReference *iface)
2136 GeckoBrowser *This = impl_from_nsISupportsWeakReference(iface);
2137 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
2140 static nsresult NSAPI nsSupportsWeakReference_GetWeakReference(nsISupportsWeakReference *iface,
2141 nsIWeakReference **_retval)
2143 GeckoBrowser *This = impl_from_nsISupportsWeakReference(iface);
2145 TRACE("(%p)->(%p)\n", This, _retval);
2147 if(!This->weak_reference) {
2148 This->weak_reference = heap_alloc(sizeof(nsWeakReference));
2149 if(!This->weak_reference)
2150 return NS_ERROR_OUT_OF_MEMORY;
2152 This->weak_reference->nsIWeakReference_iface.lpVtbl = &nsWeakReferenceVtbl;
2153 This->weak_reference->ref = 1;
2154 This->weak_reference->browser = This;
2157 *_retval = &This->weak_reference->nsIWeakReference_iface;
2158 nsIWeakReference_AddRef(*_retval);
2159 return NS_OK;
2162 static const nsISupportsWeakReferenceVtbl nsSupportsWeakReferenceVtbl = {
2163 nsSupportsWeakReference_QueryInterface,
2164 nsSupportsWeakReference_AddRef,
2165 nsSupportsWeakReference_Release,
2166 nsSupportsWeakReference_GetWeakReference
2169 static HRESULT init_browser(GeckoBrowser *browser)
2171 mozIDOMWindowProxy *mozwindow;
2172 nsIWebBrowserSetup *wbsetup;
2173 nsIScrollable *scrollable;
2174 nsresult nsres;
2175 HRESULT hres;
2177 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_WEBBROWSER_CONTRACTID,
2178 NULL, &IID_nsIWebBrowser, (void**)&browser->webbrowser);
2179 if(NS_FAILED(nsres)) {
2180 ERR("Creating WebBrowser failed: %08lx\n", nsres);
2181 return E_FAIL;
2184 nsres = nsIWebBrowser_SetContainerWindow(browser->webbrowser, &browser->nsIWebBrowserChrome_iface);
2185 if(NS_FAILED(nsres)) {
2186 ERR("SetContainerWindow failed: %08lx\n", nsres);
2187 return E_FAIL;
2190 nsres = nsIWebBrowser_QueryInterface(browser->webbrowser, &IID_nsIBaseWindow,
2191 (void**)&browser->window);
2192 if(NS_FAILED(nsres)) {
2193 ERR("Could not get nsIBaseWindow interface: %08lx\n", nsres);
2194 return E_FAIL;
2197 nsres = nsIWebBrowser_QueryInterface(browser->webbrowser, &IID_nsIWebBrowserSetup,
2198 (void**)&wbsetup);
2199 if(NS_SUCCEEDED(nsres)) {
2200 nsres = nsIWebBrowserSetup_SetProperty(wbsetup, SETUP_IS_CHROME_WRAPPER, FALSE);
2201 nsIWebBrowserSetup_Release(wbsetup);
2202 if(NS_FAILED(nsres)) {
2203 ERR("SetProperty(SETUP_IS_CHROME_WRAPPER) failed: %08lx\n", nsres);
2204 return E_FAIL;
2206 }else {
2207 ERR("Could not get nsIWebBrowserSetup interface\n");
2208 return E_FAIL;
2211 nsres = nsIWebBrowser_QueryInterface(browser->webbrowser, &IID_nsIWebNavigation,
2212 (void**)&browser->navigation);
2213 if(NS_FAILED(nsres)) {
2214 ERR("Could not get nsIWebNavigation interface: %08lx\n", nsres);
2215 return E_FAIL;
2218 nsres = nsIWebBrowser_QueryInterface(browser->webbrowser, &IID_nsIWebBrowserFocus,
2219 (void**)&browser->focus);
2220 if(NS_FAILED(nsres)) {
2221 ERR("Could not get nsIWebBrowserFocus interface: %08lx\n", nsres);
2222 return E_FAIL;
2225 if(!browser_class) {
2226 register_browser_class();
2227 if(!browser_class)
2228 return E_FAIL;
2231 browser->hwnd = CreateWindowExW(0, L"NsContainer", NULL,
2232 WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 0, 0, 100, 100,
2233 GetDesktopWindow(), NULL, hInst, browser);
2234 if(!browser->hwnd) {
2235 WARN("Could not create window\n");
2236 return E_FAIL;
2239 nsres = nsIBaseWindow_InitWindow(browser->window, browser->hwnd, NULL, 0, 0, 100, 100);
2240 if(NS_SUCCEEDED(nsres)) {
2241 nsres = nsIBaseWindow_Create(browser->window);
2242 if(NS_FAILED(nsres)) {
2243 WARN("Creating window failed: %08lx\n", nsres);
2244 return E_FAIL;
2247 nsIBaseWindow_SetVisibility(browser->window, FALSE);
2248 nsIBaseWindow_SetEnabled(browser->window, FALSE);
2249 }else {
2250 ERR("InitWindow failed: %08lx\n", nsres);
2251 return E_FAIL;
2254 nsres = nsIWebBrowser_SetParentURIContentListener(browser->webbrowser,
2255 &browser->nsIURIContentListener_iface);
2256 if(NS_FAILED(nsres))
2257 ERR("SetParentURIContentListener failed: %08lx\n", nsres);
2259 nsres = nsIWebBrowser_QueryInterface(browser->webbrowser, &IID_nsIScrollable, (void**)&scrollable);
2260 if(NS_SUCCEEDED(nsres)) {
2261 nsres = nsIScrollable_SetDefaultScrollbarPreferences(scrollable,
2262 ScrollOrientation_Y, Scrollbar_Always);
2263 if(NS_FAILED(nsres))
2264 ERR("Could not set default Y scrollbar prefs: %08lx\n", nsres);
2266 nsres = nsIScrollable_SetDefaultScrollbarPreferences(scrollable,
2267 ScrollOrientation_X, Scrollbar_Auto);
2268 if(NS_FAILED(nsres))
2269 ERR("Could not set default X scrollbar prefs: %08lx\n", nsres);
2271 nsIScrollable_Release(scrollable);
2272 }else {
2273 ERR("Could not get nsIScrollable: %08lx\n", nsres);
2276 nsres = nsIWebBrowser_GetContentDOMWindow(browser->webbrowser, &mozwindow);
2277 if(NS_FAILED(nsres)) {
2278 ERR("GetContentDOMWindow failed: %08lx\n", nsres);
2279 return E_FAIL;
2282 hres = create_outer_window(browser, mozwindow, NULL, &browser->content_window);
2283 mozIDOMWindowProxy_Release(mozwindow);
2284 return hres;
2287 HRESULT create_gecko_browser(HTMLDocumentObj *doc, GeckoBrowser **_ret)
2289 GeckoBrowser *ret;
2290 HRESULT hres;
2292 if(!load_gecko())
2293 return CLASS_E_CLASSNOTAVAILABLE;
2295 ret = heap_alloc_zero(sizeof(GeckoBrowser));
2296 if(!ret)
2297 return E_OUTOFMEMORY;
2299 ret->nsIWebBrowserChrome_iface.lpVtbl = &nsWebBrowserChromeVtbl;
2300 ret->nsIContextMenuListener_iface.lpVtbl = &nsContextMenuListenerVtbl;
2301 ret->nsIURIContentListener_iface.lpVtbl = &nsURIContentListenerVtbl;
2302 ret->nsIEmbeddingSiteWindow_iface.lpVtbl = &nsEmbeddingSiteWindowVtbl;
2303 ret->nsITooltipListener_iface.lpVtbl = &nsTooltipListenerVtbl;
2304 ret->nsIInterfaceRequestor_iface.lpVtbl = &nsInterfaceRequestorVtbl;
2305 ret->nsISupportsWeakReference_iface.lpVtbl = &nsSupportsWeakReferenceVtbl;
2307 ret->doc = doc;
2308 ret->ref = 1;
2309 ret->script_mode = SCRIPTMODE_ACTIVESCRIPT;
2310 ret->usermode = UNKNOWN_USERMODE;
2311 list_init(&ret->document_nodes);
2312 list_init(&ret->outer_windows);
2314 hres = init_browser(ret);
2315 if(SUCCEEDED(hres))
2316 *_ret = ret;
2317 else
2318 nsIWebBrowserChrome_Release(&ret->nsIWebBrowserChrome_iface);
2319 return hres;
2322 void detach_gecko_browser(GeckoBrowser *This)
2324 nsIDOMWindowUtils *window_utils = NULL;
2326 TRACE("(%p)\n", This);
2328 This->doc = NULL;
2330 if(This->content_window) {
2331 get_nsinterface((nsISupports*)This->content_window->nswindow, &IID_nsIDOMWindowUtils, (void**)&window_utils);
2333 IHTMLWindow2_Release(&This->content_window->base.IHTMLWindow2_iface);
2334 This->content_window = NULL;
2337 while(!list_empty(&This->document_nodes)) {
2338 HTMLDocumentNode *doc = LIST_ENTRY(list_head(&This->document_nodes), HTMLDocumentNode, browser_entry);
2339 detach_document_node(doc);
2342 while(!list_empty(&This->outer_windows)) {
2343 HTMLOuterWindow *window = LIST_ENTRY(list_head(&This->outer_windows), HTMLOuterWindow, browser_entry);
2344 list_remove(&window->browser_entry);
2345 window->browser = NULL;
2348 ShowWindow(This->hwnd, SW_HIDE);
2349 SetParent(This->hwnd, NULL);
2351 nsIBaseWindow_SetVisibility(This->window, FALSE);
2352 nsIBaseWindow_Destroy(This->window);
2354 nsIWebBrowser_SetContainerWindow(This->webbrowser, NULL);
2356 nsIWebBrowser_Release(This->webbrowser);
2357 This->webbrowser = NULL;
2359 nsIWebNavigation_Release(This->navigation);
2360 This->navigation = NULL;
2362 nsIBaseWindow_Release(This->window);
2363 This->window = NULL;
2365 nsIWebBrowserFocus_Release(This->focus);
2366 This->focus = NULL;
2368 if(This->editor_controller) {
2369 nsIController_Release(This->editor_controller);
2370 This->editor_controller = NULL;
2373 if(This->editor) {
2374 nsIEditor_Release(This->editor);
2375 This->editor = NULL;
2378 if(This->content_listener) {
2379 nsIURIContentListener_Release(This->content_listener);
2380 This->content_listener = NULL;
2383 if(This->hwnd) {
2384 DestroyWindow(This->hwnd);
2385 This->hwnd = NULL;
2388 nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
2390 /* Force cycle collection */
2391 if(window_utils) {
2392 nsIDOMWindowUtils_CycleCollect(window_utils, NULL, 0);
2393 nsIDOMWindowUtils_Release(window_utils);
2398 * FIXME: nsIScriptObjectPrincipal uses thiscall calling convention, so we need this hack on i386.
2399 * This will be removed after the next Gecko update, that will change calling convention on Gecko side.
2401 #ifdef __i386__
2402 __ASM_GLOBAL_FUNC(call_thiscall_func,
2403 "popl %eax\n\t"
2404 "popl %edx\n\t"
2405 "popl %ecx\n\t"
2406 "pushl %eax\n\t"
2407 "jmp *%edx\n\t")
2408 #define nsIScriptObjectPrincipal_GetPrincipal(this) ((void* (WINAPI*)(void*,void*))&call_thiscall_func)((this)->lpVtbl->GetPrincipal,this)
2409 #endif
2411 nsIXMLHttpRequest *create_nsxhr(nsIDOMWindow *nswindow)
2413 nsIScriptObjectPrincipal *sop;
2414 mozIDOMWindow *inner_window;
2415 nsIPrincipal *nspri;
2416 nsIGlobalObject *nsglo;
2417 nsIXMLHttpRequest *nsxhr;
2418 nsresult nsres;
2420 nsres = nsIDOMWindow_GetInnerWindow(nswindow, &inner_window);
2421 if(NS_FAILED(nsres)) {
2422 ERR("Could not get inner window: %08lx\n", nsres);
2423 return NULL;
2426 nsres = mozIDOMWindow_QueryInterface(inner_window, &IID_nsIGlobalObject, (void **)&nsglo);
2427 mozIDOMWindow_Release(inner_window);
2428 assert(nsres == NS_OK);
2430 nsres = nsIGlobalObject_QueryInterface(nsglo, &IID_nsIScriptObjectPrincipal, (void**)&sop);
2431 assert(nsres == NS_OK);
2433 nspri = nsIScriptObjectPrincipal_GetPrincipal(sop);
2434 nsIScriptObjectPrincipal_Release(sop);
2436 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
2437 NS_XMLHTTPREQUEST_CONTRACTID, NULL, &IID_nsIXMLHttpRequest,
2438 (void**)&nsxhr);
2439 if(NS_SUCCEEDED(nsres)) {
2440 nsres = nsIXMLHttpRequest_Init(nsxhr, nspri, NULL, nsglo, NULL, NULL);
2441 if(NS_FAILED(nsres))
2442 nsIXMLHttpRequest_Release(nsxhr);
2444 nsISupports_Release(nspri);
2445 nsIGlobalObject_Release(nsglo);
2446 if(NS_FAILED(nsres)) {
2447 ERR("nsIXMLHttpRequest_Init failed: %08lx\n", nsres);
2448 return NULL;
2451 return nsxhr;