msxml4/tests: Copy namespaces as attributes tests.
[wine.git] / dlls / mshtml / nsembed.c
blob1b31e4f71596ad8860cf11a3882645b5071e2ca3
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 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 = malloc(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 = malloc(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 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 = malloc((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 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, "dom.ipc.plugins.enabled", FALSE);
544 set_bool_pref(pref, "layout.css.grid.enabled", TRUE);
545 set_int_pref(pref, "layout.spellcheckDefault", 0);
547 nsIPrefBranch_Release(pref);
550 static BOOL init_xpcom(const PRUnichar *gre_path)
552 nsIComponentRegistrar *registrar = NULL;
553 nsIFile *gre_dir;
554 WCHAR *ptr;
555 nsresult nsres;
557 nsres = create_nsfile(gre_path, &gre_dir);
558 if(NS_FAILED(nsres)) {
559 FreeLibrary(xul_handle);
560 return FALSE;
563 nsres = NS_InitXPCOM2(&pServMgr, gre_dir, (nsIDirectoryServiceProvider*)&nsDirectoryServiceProvider2);
564 nsIFile_Release(gre_dir);
565 if(NS_FAILED(nsres)) {
566 ERR("NS_InitXPCOM2 failed: %08lx\n", nsres);
567 FreeLibrary(xul_handle);
568 return FALSE;
571 lstrcpyW(gecko_path, gre_path);
572 for(ptr = gecko_path; *ptr; ptr++) {
573 if(*ptr == '\\')
574 *ptr = '/';
576 gecko_path_len = ptr-gecko_path;
578 nsres = nsIServiceManager_QueryInterface(pServMgr, &IID_nsIComponentManager, (void**)&pCompMgr);
579 if(NS_FAILED(nsres))
580 ERR("Could not get nsIComponentManager: %08lx\n", nsres);
582 init_nsio(pCompMgr);
583 init_mutation(pCompMgr);
584 set_preferences();
586 nsres = nsIServiceManager_GetServiceByContractID(pServMgr, NS_CATEGORYMANAGER_CONTRACTID,
587 &IID_nsICategoryManager, (void**)&cat_mgr);
588 if(NS_FAILED(nsres))
589 ERR("Could not get category manager service: %08lx\n", nsres);
591 nsres = NS_GetComponentRegistrar(&registrar);
592 if(NS_SUCCEEDED(nsres)) {
593 register_nsservice(registrar, pServMgr);
594 nsIComponentRegistrar_Release(registrar);
595 }else {
596 ERR("NS_GetComponentRegistrar failed: %08lx\n", nsres);
599 init_dispex_cc();
600 init_window_cc();
602 return TRUE;
605 static BOOL load_xul(WCHAR *gecko_path)
607 size_t len;
609 set_environment(gecko_path);
611 TRACE("(%s)\n", debugstr_w(gecko_path));
613 len = wcslen(gecko_path);
614 wcscpy(gecko_path + len, L"\\xul.dll");
615 xul_handle = LoadLibraryExW(gecko_path, 0, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
616 gecko_path[len] = 0;
617 if(!xul_handle) {
618 WARN("Could not load XUL: %ld\n", GetLastError());
619 return FALSE;
622 #define NS_DLSYM(func) \
623 func = (void *)GetProcAddress(xul_handle, #func); \
624 if(!func) \
625 ERR("Could not GetProcAddress(" #func ") failed\n")
627 NS_DLSYM(NS_InitXPCOM2);
628 NS_DLSYM(NS_ShutdownXPCOM);
629 NS_DLSYM(NS_GetComponentRegistrar);
630 NS_DLSYM(NS_StringContainerInit2);
631 NS_DLSYM(NS_CStringContainerInit2);
632 NS_DLSYM(NS_StringContainerFinish);
633 NS_DLSYM(NS_CStringContainerFinish);
634 NS_DLSYM(NS_StringSetData);
635 NS_DLSYM(NS_CStringSetData);
636 NS_DLSYM(NS_NewLocalFile);
637 NS_DLSYM(NS_StringGetData);
638 NS_DLSYM(NS_CStringGetData);
639 NS_DLSYM(NS_StringGetIsVoid);
640 NS_DLSYM(NS_Alloc);
641 NS_DLSYM(NS_Free);
642 NS_DLSYM(ccref_incr);
643 NS_DLSYM(ccref_decr);
644 NS_DLSYM(ccref_init);
645 NS_DLSYM(ccp_init);
646 NS_DLSYM(describe_cc_node);
647 NS_DLSYM(note_cc_edge);
649 #undef NS_DLSYM
651 return init_xpcom(gecko_path);
654 static WCHAR *check_version(const WCHAR *path)
656 WCHAR *file_name;
657 char version[128];
658 DWORD read=0;
659 size_t len;
660 HANDLE hfile;
662 if(!wcsncmp(path, L"\\??\\", 4))
663 path += 4;
664 if(path[1] != ':') {
665 TRACE("Skipping %s\n", debugstr_w(path));
666 return FALSE; /* Gecko needs to be accessible via dos path */
669 len = wcslen(path);
670 file_name = malloc((len + 12) * sizeof(WCHAR));
671 if(!file_name)
672 return NULL;
674 PathCanonicalizeW(file_name, path);
675 len = lstrlenW(file_name);
676 wcscpy(file_name + len, L"\\VERSION");
678 hfile = CreateFileW(file_name, GENERIC_READ, FILE_SHARE_READ, NULL,
679 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
680 file_name[len] = 0;
681 if(hfile == INVALID_HANDLE_VALUE) {
682 TRACE("%s not found\n", debugstr_w(file_name));
683 free(file_name);
684 return NULL;
687 ReadFile(hfile, version, sizeof(version), &read, NULL);
688 version[read] = 0;
689 CloseHandle(hfile);
691 TRACE("%s: %s\n", debugstr_w(file_name), debugstr_a(version));
693 if(strcmp(version, GECKO_VERSION_STRING)) {
694 ERR("Unexpected version %s, expected \"%s\"\n", debugstr_a(version),
695 GECKO_VERSION_STRING);
696 free(file_name);
697 return NULL;
700 return file_name;
703 static WCHAR *find_wine_gecko_reg(void)
705 WCHAR buffer[MAX_PATH];
706 DWORD res, type, size;
707 HKEY hkey;
709 /* @@ Wine registry key: HKLM\Software\Wine\MSHTML\<version> */
710 res = RegOpenKeyW(HKEY_LOCAL_MACHINE, L"Software\\Wine\\MSHTML\\" GECKO_VERSION, &hkey);
711 if(res != ERROR_SUCCESS)
712 return NULL;
714 size = sizeof(buffer);
715 res = RegQueryValueExW(hkey, L"GeckoPath", NULL, &type, (LPBYTE)buffer, &size);
716 RegCloseKey(hkey);
717 if(res != ERROR_SUCCESS || type != REG_SZ)
718 return FALSE;
720 return check_version(buffer);
723 static WCHAR *strdupWW(const WCHAR *str1, const WCHAR *str2)
725 size_t len1 = lstrlenW(str1);
726 size_t len2 = lstrlenW(str2);
727 WCHAR *ret = malloc((len1 + len2 + 1) * sizeof(WCHAR));
728 if(!ret) return NULL;
729 memcpy(ret, str1, len1 * sizeof(WCHAR));
730 memcpy(ret + len1, str2, len2 * sizeof(WCHAR));
731 ret[len1 + len2] = 0;
732 return ret;
735 static WCHAR *find_wine_gecko_datadir(void)
737 const WCHAR *data_dir;
738 WCHAR *path = NULL, *ret;
740 if((data_dir = _wgetenv(L"WINEDATADIR")))
741 path = strdupWW(data_dir, L"\\gecko\\" GECKO_DIR_NAME);
742 else if((data_dir = _wgetenv(L"WINEBUILDDIR")))
743 path = strdupWW(data_dir, L"\\..\\gecko\\" GECKO_DIR_NAME);
744 if(!path)
745 return NULL;
747 ret = check_version(path);
748 free(path);
749 return ret;
752 static WCHAR *find_wine_gecko_unix(const char *unix_path)
754 static WCHAR * (CDECL *p_wine_get_dos_file_name)(const char*);
755 WCHAR *dos_dir, *ret;
757 if(!p_wine_get_dos_file_name) {
758 p_wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleA("kernel32"), "wine_get_dos_file_name");
759 if(!p_wine_get_dos_file_name)
760 return FALSE;
763 dos_dir = p_wine_get_dos_file_name(unix_path);
764 if(!dos_dir)
765 return FALSE;
767 ret = check_version(dos_dir);
769 HeapFree(GetProcessHeap(), 0, dos_dir);
770 return ret;
773 static CRITICAL_SECTION cs_load_gecko;
774 static CRITICAL_SECTION_DEBUG cs_load_gecko_dbg =
776 0, 0, &cs_load_gecko,
777 { &cs_load_gecko_dbg.ProcessLocksList, &cs_load_gecko_dbg.ProcessLocksList },
778 0, 0, { (DWORD_PTR)(__FILE__ ": load_gecko") }
780 static CRITICAL_SECTION cs_load_gecko = { &cs_load_gecko_dbg, -1, 0, 0, 0, 0 };
782 BOOL load_gecko(void)
784 BOOL ret = FALSE;
786 static DWORD loading_thread;
788 TRACE("()\n");
790 if(!GECKO_ARCH_STRING[0]) {
791 FIXME("Wine Gecko is not supported on this architecture.\n");
792 return FALSE;
795 /* load_gecko may be called recursively */
796 if(loading_thread == GetCurrentThreadId())
797 return pCompMgr != NULL;
799 EnterCriticalSection(&cs_load_gecko);
801 if(!loading_thread) {
802 WCHAR *gecko_path;
804 loading_thread = GetCurrentThreadId();
806 if(!(gecko_path = find_wine_gecko_reg())
807 && !(gecko_path = find_wine_gecko_datadir())
808 && !(gecko_path = find_wine_gecko_unix(INSTALL_DATADIR "/wine/gecko/" GECKO_DIR_NAME))
809 && (!strcmp(INSTALL_DATADIR, "/usr/share") ||
810 !(gecko_path = find_wine_gecko_unix("/usr/share/wine/gecko/" GECKO_DIR_NAME)))
811 && !(gecko_path = find_wine_gecko_unix("/opt/wine/gecko/" GECKO_DIR_NAME))
812 && install_wine_gecko())
813 gecko_path = find_wine_gecko_reg();
815 if(gecko_path) {
816 ret = load_xul(gecko_path);
817 free(gecko_path);
818 }else {
819 MESSAGE("Could not find Wine Gecko. HTML rendering will be disabled.\n");
821 }else {
822 FIXME("Gecko can only be used from one thread.\n");
823 ret = FALSE;
826 LeaveCriticalSection(&cs_load_gecko);
828 return ret;
831 void *nsalloc(size_t size)
833 return NS_Alloc(size);
836 void nsfree(void *mem)
838 NS_Free(mem);
841 BOOL nsACString_Init(nsACString *str, const char *data)
843 return NS_SUCCEEDED(NS_CStringContainerInit2(str, data, PR_UINT32_MAX, 0));
847 * Initializes nsACString with data owned by caller.
848 * Caller must ensure that data is valid during lifetime of string object.
850 void nsACString_InitDepend(nsACString *str, const char *data)
852 NS_CStringContainerInit2(str, data, PR_UINT32_MAX, NS_CSTRING_CONTAINER_INIT_DEPEND);
855 void nsACString_SetData(nsACString *str, const char *data)
857 NS_CStringSetData(str, data, PR_UINT32_MAX);
860 UINT32 nsACString_GetData(const nsACString *str, const char **data)
862 return NS_CStringGetData(str, data, NULL);
865 void nsACString_Finish(nsACString *str)
867 NS_CStringContainerFinish(str);
870 BOOL nsAString_Init(nsAString *str, const PRUnichar *data)
872 return NS_SUCCEEDED(NS_StringContainerInit2(str, data, PR_UINT32_MAX, 0));
876 * Initializes nsAString with data owned by caller.
877 * Caller must ensure that data is valid during lifetime of string object.
879 void nsAString_InitDepend(nsAString *str, const PRUnichar *data)
881 NS_StringContainerInit2(str, data, PR_UINT32_MAX, NS_STRING_CONTAINER_INIT_DEPEND);
884 UINT32 nsAString_GetData(const nsAString *str, const PRUnichar **data)
886 return NS_StringGetData(str, data, NULL);
889 void nsAString_SetData(nsAString *str, const PRUnichar *data)
891 NS_StringSetData(str, data, PR_UINT32_MAX);
894 void nsAString_Finish(nsAString *str)
896 NS_StringContainerFinish(str);
899 HRESULT map_nsresult(nsresult nsres)
901 switch(nsres) {
902 case NS_OK:
903 return S_OK;
904 case NS_ERROR_OUT_OF_MEMORY:
905 return E_OUTOFMEMORY;
906 case NS_ERROR_NOT_IMPLEMENTED:
907 return E_NOTIMPL;
908 case NS_NOINTERFACE:
909 return E_NOINTERFACE;
910 case NS_ERROR_INVALID_POINTER:
911 return E_POINTER;
912 case NS_ERROR_INVALID_ARG:
913 return E_INVALIDARG;
914 case NS_ERROR_UNEXPECTED:
915 return E_UNEXPECTED;
916 case NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR:
917 return 0x80700007; /* according to tests */
918 case NS_ERROR_DOM_SYNTAX_ERR:
919 return E_INVALIDARG; /* FIXME: Throw SyntaxError for IE9+ modes */
920 case NS_BINDING_ABORTED:
921 return E_ABORT;
923 return E_FAIL;
926 HRESULT return_nsstr(nsresult nsres, nsAString *nsstr, BSTR *p)
928 const PRUnichar *str;
929 HRESULT hres = S_OK;
931 if(NS_FAILED(nsres)) {
932 WARN("failed: %08lx\n", nsres);
933 nsAString_Finish(nsstr);
934 return map_nsresult(nsres);
937 nsAString_GetData(nsstr, &str);
938 TRACE("ret %s\n", debugstr_w(str));
939 if(*str) {
940 *p = SysAllocString(str);
941 if(!*p)
942 hres = E_OUTOFMEMORY;
943 }else {
944 *p = NULL;
947 nsAString_Finish(nsstr);
948 return hres;
951 HRESULT return_nsstr_variant(nsresult nsres, nsAString *nsstr, unsigned flags, VARIANT *p)
953 HRESULT hres = S_OK;
955 if(NS_FAILED(nsres)) {
956 ERR("failed: %08lx\n", nsres);
957 nsAString_Finish(nsstr);
958 return map_nsresult(nsres);
961 if(NS_StringGetIsVoid(nsstr)) {
962 V_VT(p) = VT_NULL;
963 }else {
964 const WCHAR *str;
965 size_t len;
966 nsAString_GetData(nsstr, &str);
967 len = wcslen(str);
968 if(flags & NSSTR_IMPLICIT_PX) {
969 const WCHAR *iter;
970 if(len > 2 && !wcscmp(str + len - 2, L"px"))
971 len -= 2;
972 for(iter = str; iter < str + len && is_digit(*iter); iter++);
973 if(*iter == '.') {
974 const WCHAR *dot = iter++;
975 while(iter < str + len && is_digit(*iter)) iter++;
976 if(iter == str + len && dot) len = dot - str;
979 if(flags & NSSTR_COLOR) {
980 hres = nscolor_to_str(str, &V_BSTR(p));
981 }else if(*str) {
982 V_BSTR(p) = SysAllocStringLen(str, len);
983 if(!V_BSTR(p))
984 hres = E_OUTOFMEMORY;
985 }else {
986 V_BSTR(p) = NULL;
988 if(SUCCEEDED(hres))
989 V_VT(p) = VT_BSTR;
992 nsAString_Finish(nsstr);
993 if(FAILED(hres))
994 return hres;
995 TRACE("ret %s\n", debugstr_variant(p));
996 return S_OK;
1000 * Converts VARIANT to string and stores the result in nsAString. To avoid useless
1001 * allocations, the function uses an existing string if available, so caller must
1002 * ensure that passed VARIANT is unchanged as long as its string representation is used
1004 HRESULT variant_to_nsstr(VARIANT *v, BOOL hex_int, nsAString *nsstr)
1006 WCHAR buf[32];
1008 switch(V_VT(v)) {
1009 case VT_EMPTY:
1010 case VT_NULL:
1011 nsAString_InitDepend(nsstr, NULL);
1012 return S_OK;
1014 case VT_BSTR:
1015 nsAString_InitDepend(nsstr, V_BSTR(v));
1016 break;
1018 case VT_BSTR|VT_BYREF:
1019 nsAString_InitDepend(nsstr, *V_BSTRREF(v));
1020 break;
1022 case VT_I4:
1023 wsprintfW(buf, hex_int ? L"#%06x" : L"%d", V_I4(v));
1024 nsAString_Init(nsstr, buf);
1025 break;
1027 case VT_R8:
1028 case VT_DISPATCH: {
1029 VARIANT strv;
1030 HRESULT hres;
1032 V_VT(&strv) = VT_EMPTY;
1033 hres = VariantChangeTypeEx(&strv, v, MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT),
1034 0, VT_BSTR);
1035 if(FAILED(hres))
1036 return hres;
1038 nsAString_Init(nsstr, V_BSTR(&strv));
1039 SysFreeString(V_BSTR(&strv));
1040 break;
1043 default:
1044 FIXME("not implemented for %s\n", debugstr_variant(v));
1045 return E_NOTIMPL;
1048 return S_OK;
1051 nsICommandParams *create_nscommand_params(void)
1053 nsICommandParams *ret = NULL;
1054 nsresult nsres;
1056 if(!pCompMgr)
1057 return NULL;
1059 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
1060 NS_COMMANDPARAMS_CONTRACTID, NULL, &IID_nsICommandParams,
1061 (void**)&ret);
1062 if(NS_FAILED(nsres))
1063 ERR("Could not get nsICommandParams\n");
1065 return ret;
1068 nsIWritableVariant *create_nsvariant(void)
1070 nsIWritableVariant *ret = NULL;
1071 nsresult nsres;
1073 if(!pCompMgr)
1074 return NULL;
1076 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
1077 NS_VARIANT_CONTRACTID, NULL, &IID_nsIWritableVariant, (void**)&ret);
1078 if(NS_FAILED(nsres))
1079 ERR("Could not get nsIVariant\n");
1081 return ret;
1084 char *get_nscategory_entry(const char *category, const char *entry)
1086 char *ret = NULL;
1087 nsresult nsres;
1089 nsres = nsICategoryManager_GetCategoryEntry(cat_mgr, category, entry, &ret);
1090 return NS_SUCCEEDED(nsres) ? ret : NULL;
1093 nsresult get_nsinterface(nsISupports *iface, REFIID riid, void **ppv)
1095 nsIInterfaceRequestor *iface_req;
1096 nsresult nsres;
1098 nsres = nsISupports_QueryInterface(iface, &IID_nsIInterfaceRequestor, (void**)&iface_req);
1099 if(NS_FAILED(nsres))
1100 return nsres;
1102 nsres = nsIInterfaceRequestor_GetInterface(iface_req, riid, ppv);
1103 nsIInterfaceRequestor_Release(iface_req);
1105 return nsres;
1108 static HRESULT nsnode_to_nsstring_rec(nsIContentSerializer *serializer, nsIDOMNode *nsnode, nsAString *str)
1110 nsIDOMNodeList *node_list = NULL;
1111 cpp_bool has_children = FALSE;
1112 nsIContent *nscontent;
1113 UINT16 type;
1114 nsresult nsres;
1116 nsIDOMNode_HasChildNodes(nsnode, &has_children);
1118 nsres = nsIDOMNode_GetNodeType(nsnode, &type);
1119 if(NS_FAILED(nsres)) {
1120 ERR("GetType failed: %08lx\n", nsres);
1121 return E_FAIL;
1124 if(type != DOCUMENT_NODE) {
1125 nsres = nsIDOMNode_QueryInterface(nsnode, &IID_nsIContent, (void**)&nscontent);
1126 if(NS_FAILED(nsres)) {
1127 ERR("Could not get nsIContent interface: %08lx\n", nsres);
1128 return E_FAIL;
1132 switch(type) {
1133 case ELEMENT_NODE:
1134 nsIContentSerializer_AppendElementStart(serializer, nscontent, nscontent, str);
1135 break;
1136 case TEXT_NODE:
1137 nsIContentSerializer_AppendText(serializer, nscontent, 0, -1, str);
1138 break;
1139 case COMMENT_NODE:
1140 nsIContentSerializer_AppendComment(serializer, nscontent, 0, -1, str);
1141 break;
1142 case DOCUMENT_NODE: {
1143 nsIDocument *nsdoc;
1144 nsIDOMNode_QueryInterface(nsnode, &IID_nsIDocument, (void**)&nsdoc);
1145 nsIContentSerializer_AppendDocumentStart(serializer, nsdoc, str);
1146 nsIDocument_Release(nsdoc);
1147 break;
1149 case DOCUMENT_TYPE_NODE:
1150 nsIContentSerializer_AppendDoctype(serializer, nscontent, str);
1151 break;
1152 case DOCUMENT_FRAGMENT_NODE:
1153 break;
1154 default:
1155 FIXME("Unhandled type %u\n", type);
1158 if(has_children) {
1159 UINT32 child_cnt, i;
1160 nsIDOMNode *child_node;
1162 nsIDOMNode_GetChildNodes(nsnode, &node_list);
1163 nsIDOMNodeList_GetLength(node_list, &child_cnt);
1165 for(i=0; i<child_cnt; i++) {
1166 nsres = nsIDOMNodeList_Item(node_list, i, &child_node);
1167 if(NS_SUCCEEDED(nsres)) {
1168 nsnode_to_nsstring_rec(serializer, child_node, str);
1169 nsIDOMNode_Release(child_node);
1170 }else {
1171 ERR("Item failed: %08lx\n", nsres);
1175 nsIDOMNodeList_Release(node_list);
1178 if(type == ELEMENT_NODE)
1179 nsIContentSerializer_AppendElementEnd(serializer, nscontent, str);
1181 if(type != DOCUMENT_NODE)
1182 nsIContent_Release(nscontent);
1183 return S_OK;
1186 HRESULT nsnode_to_nsstring(nsIDOMNode *nsnode, nsAString *str)
1188 nsIContentSerializer *serializer;
1189 nsresult nsres;
1190 HRESULT hres;
1192 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
1193 NS_HTMLSERIALIZER_CONTRACTID, NULL, &IID_nsIContentSerializer,
1194 (void**)&serializer);
1195 if(NS_FAILED(nsres)) {
1196 ERR("Could not get nsIContentSerializer: %08lx\n", nsres);
1197 return E_FAIL;
1200 nsres = nsIContentSerializer_Init(serializer, 0, 100, NULL, FALSE, FALSE /* FIXME */);
1201 if(NS_FAILED(nsres))
1202 ERR("Init failed: %08lx\n", nsres);
1204 hres = nsnode_to_nsstring_rec(serializer, nsnode, str);
1205 if(SUCCEEDED(hres)) {
1206 nsres = nsIContentSerializer_Flush(serializer, str);
1207 if(NS_FAILED(nsres))
1208 ERR("Flush failed: %08lx\n", nsres);
1211 nsIContentSerializer_Release(serializer);
1212 return hres;
1215 void setup_editor_controller(GeckoBrowser *This)
1217 nsIEditingSession *editing_session = NULL;
1218 nsIControllerContext *ctrlctx;
1219 nsresult nsres;
1221 unlink_ref(&This->editor);
1222 unlink_ref(&This->editor_controller);
1224 nsres = get_nsinterface((nsISupports*)This->webbrowser, &IID_nsIEditingSession,
1225 (void**)&editing_session);
1226 if(NS_FAILED(nsres)) {
1227 ERR("Could not get nsIEditingSession: %08lx\n", nsres);
1228 return;
1231 nsres = nsIEditingSession_GetEditorForWindow(editing_session,
1232 This->doc->window->window_proxy, &This->editor);
1233 nsIEditingSession_Release(editing_session);
1234 if(NS_FAILED(nsres)) {
1235 ERR("Could not get editor: %08lx\n", nsres);
1236 return;
1239 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
1240 NS_EDITORCONTROLLER_CONTRACTID, NULL, &IID_nsIControllerContext, (void**)&ctrlctx);
1241 if(NS_SUCCEEDED(nsres)) {
1242 nsres = nsIControllerContext_SetCommandContext(ctrlctx, (nsISupports *)This->editor);
1243 if(NS_FAILED(nsres))
1244 ERR("SetCommandContext failed: %08lx\n", nsres);
1245 nsres = nsIControllerContext_QueryInterface(ctrlctx, &IID_nsIController,
1246 (void**)&This->editor_controller);
1247 nsIControllerContext_Release(ctrlctx);
1248 if(NS_FAILED(nsres))
1249 ERR("Could not get nsIController interface: %08lx\n", nsres);
1250 }else {
1251 ERR("Could not create edit controller: %08lx\n", nsres);
1255 void close_gecko(void)
1257 TRACE("()\n");
1259 release_nsio();
1260 init_mutation(NULL);
1262 if(profile_directory) {
1263 nsIFile_Release(profile_directory);
1264 profile_directory = NULL;
1267 if(plugin_directory) {
1268 nsIFile_Release(plugin_directory);
1269 plugin_directory = NULL;
1272 if(pCompMgr)
1273 nsIComponentManager_Release(pCompMgr);
1275 if(pServMgr)
1276 nsIServiceManager_Release(pServMgr);
1278 if(cat_mgr)
1279 nsICategoryManager_Release(cat_mgr);
1281 /* Gecko doesn't really support being unloaded */
1282 /* if (hXPCOM) FreeLibrary(hXPCOM); */
1284 DeleteCriticalSection(&cs_load_gecko);
1287 BOOL is_gecko_path(const char *path)
1289 WCHAR *buf, *ptr;
1290 BOOL ret;
1292 buf = strdupUtoW(path);
1293 if(!buf || lstrlenW(buf) < gecko_path_len)
1294 return FALSE;
1296 for(ptr = buf; *ptr; ptr++) {
1297 if(*ptr == '\\')
1298 *ptr = '/';
1301 UrlUnescapeW(buf, NULL, NULL, URL_UNESCAPE_INPLACE | URL_UNESCAPE_AS_UTF8);
1302 buf[gecko_path_len] = 0;
1304 ret = !wcsicmp(buf, gecko_path);
1305 free(buf);
1306 return ret;
1309 void set_viewer_zoom(GeckoBrowser *browser, float factor)
1311 nsIContentViewer *content_viewer;
1312 nsIDocShell *doc_shell;
1313 nsresult nsres;
1315 TRACE("Setting to %f\n", factor);
1317 nsres = get_nsinterface((nsISupports*)browser->navigation, &IID_nsIDocShell, (void**)&doc_shell);
1318 assert(nsres == NS_OK);
1320 nsres = nsIDocShell_GetContentViewer(doc_shell, &content_viewer);
1321 assert(nsres == NS_OK && content_viewer);
1322 nsIDocShell_Release(doc_shell);
1324 nsres = nsIContentViewer_SetFullZoom(content_viewer, factor);
1325 if(NS_FAILED(nsres))
1326 ERR("SetFullZoom failed: %08lx\n", nsres);
1328 nsIContentViewer_Release(content_viewer);
1331 float get_viewer_zoom(GeckoBrowser *browser)
1333 nsIContentViewer *content_viewer;
1334 nsIDocShell *doc_shell;
1335 nsresult nsres;
1336 float factor;
1338 nsres = get_nsinterface((nsISupports*)browser->navigation, &IID_nsIDocShell, (void**)&doc_shell);
1339 assert(nsres == NS_OK);
1341 nsres = nsIDocShell_GetContentViewer(doc_shell, &content_viewer);
1342 assert(nsres == NS_OK && content_viewer);
1343 nsIDocShell_Release(doc_shell);
1345 nsres = nsIContentViewer_GetFullZoom(content_viewer, &factor);
1346 if(NS_FAILED(nsres))
1347 ERR("GetFullZoom failed: %08lx\n", nsres);
1348 TRACE("Got %f\n", factor);
1350 nsIContentViewer_Release(content_viewer);
1351 return factor;
1354 struct nsWeakReference {
1355 nsIWeakReference nsIWeakReference_iface;
1357 LONG ref;
1359 GeckoBrowser *browser;
1362 static inline nsWeakReference *impl_from_nsIWeakReference(nsIWeakReference *iface)
1364 return CONTAINING_RECORD(iface, nsWeakReference, nsIWeakReference_iface);
1367 static nsresult NSAPI nsWeakReference_QueryInterface(nsIWeakReference *iface,
1368 nsIIDRef riid, void **result)
1370 nsWeakReference *This = impl_from_nsIWeakReference(iface);
1372 if(IsEqualGUID(&IID_nsISupports, riid)) {
1373 TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
1374 *result = &This->nsIWeakReference_iface;
1375 }else if(IsEqualGUID(&IID_nsIWeakReference, riid)) {
1376 TRACE("(%p)->(IID_nsIWeakReference %p)\n", This, result);
1377 *result = &This->nsIWeakReference_iface;
1378 }else {
1379 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
1380 *result = NULL;
1381 return NS_NOINTERFACE;
1384 nsISupports_AddRef((nsISupports*)*result);
1385 return NS_OK;
1388 static nsrefcnt NSAPI nsWeakReference_AddRef(nsIWeakReference *iface)
1390 nsWeakReference *This = impl_from_nsIWeakReference(iface);
1391 LONG ref = InterlockedIncrement(&This->ref);
1393 TRACE("(%p) ref=%ld\n", This, ref);
1395 return ref;
1398 static nsrefcnt NSAPI nsWeakReference_Release(nsIWeakReference *iface)
1400 nsWeakReference *This = impl_from_nsIWeakReference(iface);
1401 LONG ref = InterlockedDecrement(&This->ref);
1403 TRACE("(%p) ref=%ld\n", This, ref);
1405 if(!ref) {
1406 assert(!This->browser);
1407 free(This);
1410 return ref;
1413 static nsresult NSAPI nsWeakReference_QueryReferent(nsIWeakReference *iface,
1414 const nsIID *riid, void **result)
1416 nsWeakReference *This = impl_from_nsIWeakReference(iface);
1418 if(!This->browser)
1419 return NS_ERROR_NULL_POINTER;
1421 return nsIWebBrowserChrome_QueryInterface(&This->browser->nsIWebBrowserChrome_iface, riid, result);
1424 static const nsIWeakReferenceVtbl nsWeakReferenceVtbl = {
1425 nsWeakReference_QueryInterface,
1426 nsWeakReference_AddRef,
1427 nsWeakReference_Release,
1428 nsWeakReference_QueryReferent
1431 /**********************************************************
1432 * nsIWebBrowserChrome interface
1435 static inline GeckoBrowser *impl_from_nsIWebBrowserChrome(nsIWebBrowserChrome *iface)
1437 return CONTAINING_RECORD(iface, GeckoBrowser, nsIWebBrowserChrome_iface);
1440 static nsresult NSAPI nsWebBrowserChrome_QueryInterface(nsIWebBrowserChrome *iface,
1441 nsIIDRef riid, void **result)
1443 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1445 *result = NULL;
1446 if(IsEqualGUID(&IID_nsISupports, riid)) {
1447 TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
1448 *result = &This->nsIWebBrowserChrome_iface;
1449 }else if(IsEqualGUID(&IID_nsIWebBrowserChrome, riid)) {
1450 TRACE("(%p)->(IID_nsIWebBrowserChrome, %p)\n", This, result);
1451 *result = &This->nsIWebBrowserChrome_iface;
1452 }else if(IsEqualGUID(&IID_nsIContextMenuListener, riid)) {
1453 TRACE("(%p)->(IID_nsIContextMenuListener, %p)\n", This, result);
1454 *result = &This->nsIContextMenuListener_iface;
1455 }else if(IsEqualGUID(&IID_nsIURIContentListener, riid)) {
1456 TRACE("(%p)->(IID_nsIURIContentListener %p)\n", This, result);
1457 *result = &This->nsIURIContentListener_iface;
1458 }else if(IsEqualGUID(&IID_nsIEmbeddingSiteWindow, riid)) {
1459 TRACE("(%p)->(IID_nsIEmbeddingSiteWindow %p)\n", This, result);
1460 *result = &This->nsIEmbeddingSiteWindow_iface;
1461 }else if(IsEqualGUID(&IID_nsITooltipListener, riid)) {
1462 TRACE("(%p)->(IID_nsITooltipListener %p)\n", This, result);
1463 *result = &This->nsITooltipListener_iface;
1464 }else if(IsEqualGUID(&IID_nsIInterfaceRequestor, riid)) {
1465 TRACE("(%p)->(IID_nsIInterfaceRequestor %p)\n", This, result);
1466 *result = &This->nsIInterfaceRequestor_iface;
1467 }else if(IsEqualGUID(&IID_nsISupportsWeakReference, riid)) {
1468 TRACE("(%p)->(IID_nsISupportsWeakReference %p)\n", This, result);
1469 *result = &This->nsISupportsWeakReference_iface;
1472 if(*result) {
1473 nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1474 return NS_OK;
1477 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
1478 return NS_NOINTERFACE;
1481 static nsrefcnt NSAPI nsWebBrowserChrome_AddRef(nsIWebBrowserChrome *iface)
1483 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1484 LONG ref = InterlockedIncrement(&This->ref);
1486 TRACE("(%p) ref=%ld\n", This, ref);
1488 return ref;
1491 static nsrefcnt NSAPI nsWebBrowserChrome_Release(nsIWebBrowserChrome *iface)
1493 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1494 LONG ref = InterlockedDecrement(&This->ref);
1496 TRACE("(%p) ref=%ld\n", This, ref);
1498 if(!ref) {
1499 if(This->doc)
1500 detach_gecko_browser(This);
1501 if(This->weak_reference) {
1502 This->weak_reference->browser = NULL;
1503 nsIWeakReference_Release(&This->weak_reference->nsIWeakReference_iface);
1505 free(This);
1508 return ref;
1511 static nsresult NSAPI nsWebBrowserChrome_SetStatus(nsIWebBrowserChrome *iface,
1512 UINT32 statusType, const PRUnichar *status)
1514 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1515 TRACE("(%p)->(%d %s)\n", This, statusType, debugstr_w(status));
1516 return NS_OK;
1519 static nsresult NSAPI nsWebBrowserChrome_GetWebBrowser(nsIWebBrowserChrome *iface,
1520 nsIWebBrowser **aWebBrowser)
1522 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1524 TRACE("(%p)->(%p)\n", This, aWebBrowser);
1526 if(!aWebBrowser)
1527 return NS_ERROR_INVALID_ARG;
1529 if(This->webbrowser)
1530 nsIWebBrowser_AddRef(This->webbrowser);
1531 *aWebBrowser = This->webbrowser;
1532 return S_OK;
1535 static nsresult NSAPI nsWebBrowserChrome_SetWebBrowser(nsIWebBrowserChrome *iface,
1536 nsIWebBrowser *aWebBrowser)
1538 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1540 TRACE("(%p)->(%p)\n", This, aWebBrowser);
1542 if(aWebBrowser != This->webbrowser)
1543 ERR("Wrong nsWebBrowser!\n");
1545 return NS_OK;
1548 static nsresult NSAPI nsWebBrowserChrome_GetChromeFlags(nsIWebBrowserChrome *iface,
1549 UINT32 *aChromeFlags)
1551 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1552 WARN("(%p)->(%p)\n", This, aChromeFlags);
1553 return NS_ERROR_NOT_IMPLEMENTED;
1556 static nsresult NSAPI nsWebBrowserChrome_SetChromeFlags(nsIWebBrowserChrome *iface,
1557 UINT32 aChromeFlags)
1559 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1560 WARN("(%p)->(%08x)\n", This, aChromeFlags);
1561 return NS_ERROR_NOT_IMPLEMENTED;
1564 static nsresult NSAPI nsWebBrowserChrome_DestroyBrowserWindow(nsIWebBrowserChrome *iface)
1566 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1567 TRACE("(%p)\n", This);
1568 return NS_ERROR_NOT_IMPLEMENTED;
1571 static nsresult NSAPI nsWebBrowserChrome_SizeBrowserTo(nsIWebBrowserChrome *iface,
1572 LONG aCX, LONG aCY)
1574 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1575 WARN("(%p)->(%ld %ld)\n", This, aCX, aCY);
1576 return NS_ERROR_NOT_IMPLEMENTED;
1579 static nsresult NSAPI nsWebBrowserChrome_ShowAsModal(nsIWebBrowserChrome *iface)
1581 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1582 WARN("(%p)\n", This);
1583 return NS_ERROR_NOT_IMPLEMENTED;
1586 static nsresult NSAPI nsWebBrowserChrome_IsWindowModal(nsIWebBrowserChrome *iface, cpp_bool *_retval)
1588 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1589 WARN("(%p)->(%p)\n", This, _retval);
1590 return NS_ERROR_NOT_IMPLEMENTED;
1593 static nsresult NSAPI nsWebBrowserChrome_ExitModalEventLoop(nsIWebBrowserChrome *iface,
1594 nsresult aStatus)
1596 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1597 WARN("(%p)->(%08lx)\n", This, aStatus);
1598 return NS_ERROR_NOT_IMPLEMENTED;
1601 static const nsIWebBrowserChromeVtbl nsWebBrowserChromeVtbl = {
1602 nsWebBrowserChrome_QueryInterface,
1603 nsWebBrowserChrome_AddRef,
1604 nsWebBrowserChrome_Release,
1605 nsWebBrowserChrome_SetStatus,
1606 nsWebBrowserChrome_GetWebBrowser,
1607 nsWebBrowserChrome_SetWebBrowser,
1608 nsWebBrowserChrome_GetChromeFlags,
1609 nsWebBrowserChrome_SetChromeFlags,
1610 nsWebBrowserChrome_DestroyBrowserWindow,
1611 nsWebBrowserChrome_SizeBrowserTo,
1612 nsWebBrowserChrome_ShowAsModal,
1613 nsWebBrowserChrome_IsWindowModal,
1614 nsWebBrowserChrome_ExitModalEventLoop
1617 /**********************************************************
1618 * nsIContextMenuListener interface
1621 static inline GeckoBrowser *impl_from_nsIContextMenuListener(nsIContextMenuListener *iface)
1623 return CONTAINING_RECORD(iface, GeckoBrowser, nsIContextMenuListener_iface);
1626 static nsresult NSAPI nsContextMenuListener_QueryInterface(nsIContextMenuListener *iface,
1627 nsIIDRef riid, void **result)
1629 GeckoBrowser *This = impl_from_nsIContextMenuListener(iface);
1630 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1633 static nsrefcnt NSAPI nsContextMenuListener_AddRef(nsIContextMenuListener *iface)
1635 GeckoBrowser *This = impl_from_nsIContextMenuListener(iface);
1636 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1639 static nsrefcnt NSAPI nsContextMenuListener_Release(nsIContextMenuListener *iface)
1641 GeckoBrowser *This = impl_from_nsIContextMenuListener(iface);
1642 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
1645 static nsresult NSAPI nsContextMenuListener_OnShowContextMenu(nsIContextMenuListener *iface,
1646 UINT32 aContextFlags, nsIDOMEvent *aEvent, nsIDOMNode *aNode)
1648 GeckoBrowser *This = impl_from_nsIContextMenuListener(iface);
1649 nsIDOMMouseEvent *mouse_event;
1650 HTMLDOMNode *node;
1651 DOMEvent *event;
1652 POINT pt;
1653 DWORD dwID = CONTEXT_MENU_DEFAULT;
1654 nsresult nsres;
1655 HRESULT hres;
1657 TRACE("(%p)->(%08x %p %p)\n", This, aContextFlags, aEvent, aNode);
1659 if (!aNode)
1660 return NS_ERROR_FAILURE;
1662 hres = get_node(aNode, TRUE, &node);
1663 if(FAILED(hres))
1664 return NS_ERROR_FAILURE;
1666 hres = create_event_from_nsevent(aEvent, dispex_compat_mode(&node->event_target.dispex), &event);
1667 if(SUCCEEDED(hres)) {
1668 dispatch_event(&node->event_target, event);
1669 IDOMEvent_Release(&event->IDOMEvent_iface);
1672 nsres = nsIDOMEvent_QueryInterface(aEvent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
1673 assert(NS_SUCCEEDED(nsres));
1675 nsIDOMMouseEvent_GetScreenX(mouse_event, &pt.x);
1676 nsIDOMMouseEvent_GetScreenY(mouse_event, &pt.y);
1677 nsIDOMMouseEvent_Release(mouse_event);
1679 switch(aContextFlags) {
1680 case CONTEXT_NONE:
1681 case CONTEXT_DOCUMENT:
1682 case CONTEXT_TEXT: {
1683 nsISelection *selection;
1685 if(!This->doc->doc_node->html_document) {
1686 FIXME("Not implemented for XML document\n");
1687 break;
1690 nsres = nsIDOMHTMLDocument_GetSelection(This->doc->doc_node->html_document, &selection);
1691 if(NS_SUCCEEDED(nsres) && selection) {
1692 cpp_bool is_collapsed;
1694 /* FIXME: Check if the click was inside selection. */
1695 nsres = nsISelection_GetIsCollapsed(selection, &is_collapsed);
1696 nsISelection_Release(selection);
1697 if(NS_SUCCEEDED(nsres) && !is_collapsed)
1698 dwID = CONTEXT_MENU_TEXTSELECT;
1700 break;
1702 case CONTEXT_IMAGE:
1703 case CONTEXT_IMAGE|CONTEXT_LINK:
1704 dwID = CONTEXT_MENU_IMAGE;
1705 break;
1706 case CONTEXT_LINK:
1707 dwID = CONTEXT_MENU_ANCHOR;
1708 break;
1709 case CONTEXT_INPUT:
1710 dwID = CONTEXT_MENU_CONTROL;
1711 break;
1712 default:
1713 FIXME("aContextFlags=%08x\n", aContextFlags);
1716 show_context_menu(This->doc, dwID, &pt, (IDispatch*)&node->IHTMLDOMNode_iface);
1717 node_release(node);
1718 return NS_OK;
1721 static const nsIContextMenuListenerVtbl nsContextMenuListenerVtbl = {
1722 nsContextMenuListener_QueryInterface,
1723 nsContextMenuListener_AddRef,
1724 nsContextMenuListener_Release,
1725 nsContextMenuListener_OnShowContextMenu
1728 /**********************************************************
1729 * nsIURIContentListener interface
1732 static inline GeckoBrowser *impl_from_nsIURIContentListener(nsIURIContentListener *iface)
1734 return CONTAINING_RECORD(iface, GeckoBrowser, nsIURIContentListener_iface);
1737 static nsresult NSAPI nsURIContentListener_QueryInterface(nsIURIContentListener *iface,
1738 nsIIDRef riid, void **result)
1740 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1741 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1744 static nsrefcnt NSAPI nsURIContentListener_AddRef(nsIURIContentListener *iface)
1746 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1747 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1750 static nsrefcnt NSAPI nsURIContentListener_Release(nsIURIContentListener *iface)
1752 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1753 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
1756 static nsresult NSAPI nsURIContentListener_OnStartURIOpen(nsIURIContentListener *iface,
1757 nsIURI *aURI, cpp_bool *_retval)
1759 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1760 nsACString spec_str;
1761 const char *spec;
1763 nsACString_Init(&spec_str, NULL);
1764 nsIURI_GetSpec(aURI, &spec_str);
1765 nsACString_GetData(&spec_str, &spec);
1767 TRACE("(%p)->(%p(%s) %p)\n", This, aURI, debugstr_a(spec), _retval);
1769 nsACString_Finish(&spec_str);
1771 return This->content_listener
1772 ? nsIURIContentListener_OnStartURIOpen(This->content_listener, aURI, _retval)
1773 : NS_OK;
1776 static nsresult NSAPI nsURIContentListener_DoContent(nsIURIContentListener *iface,
1777 const nsACString *aContentType, cpp_bool aIsContentPreferred, nsIRequest *aRequest,
1778 nsIStreamListener **aContentHandler, cpp_bool *_retval)
1780 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1782 TRACE("(%p)->(%p %x %p %p %p)\n", This, aContentType, aIsContentPreferred,
1783 aRequest, aContentHandler, _retval);
1785 return This->content_listener
1786 ? nsIURIContentListener_DoContent(This->content_listener, aContentType,
1787 aIsContentPreferred, aRequest, aContentHandler, _retval)
1788 : NS_ERROR_NOT_IMPLEMENTED;
1791 static nsresult NSAPI nsURIContentListener_IsPreferred(nsIURIContentListener *iface,
1792 const char *aContentType, char **aDesiredContentType, cpp_bool *_retval)
1794 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1796 TRACE("(%p)->(%s %p %p)\n", This, debugstr_a(aContentType), aDesiredContentType, _retval);
1798 /* FIXME: Should we do something here? */
1799 *_retval = TRUE;
1801 return This->content_listener
1802 ? nsIURIContentListener_IsPreferred(This->content_listener, aContentType,
1803 aDesiredContentType, _retval)
1804 : NS_OK;
1807 static nsresult NSAPI nsURIContentListener_CanHandleContent(nsIURIContentListener *iface,
1808 const char *aContentType, cpp_bool aIsContentPreferred, char **aDesiredContentType,
1809 cpp_bool *_retval)
1811 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1813 TRACE("(%p)->(%s %x %p %p)\n", This, debugstr_a(aContentType), aIsContentPreferred,
1814 aDesiredContentType, _retval);
1816 return This->content_listener
1817 ? nsIURIContentListener_CanHandleContent(This->content_listener, aContentType,
1818 aIsContentPreferred, aDesiredContentType, _retval)
1819 : NS_ERROR_NOT_IMPLEMENTED;
1822 static nsresult NSAPI nsURIContentListener_GetLoadCookie(nsIURIContentListener *iface,
1823 nsISupports **aLoadCookie)
1825 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1827 WARN("(%p)->(%p)\n", This, aLoadCookie);
1829 return This->content_listener
1830 ? nsIURIContentListener_GetLoadCookie(This->content_listener, aLoadCookie)
1831 : NS_ERROR_NOT_IMPLEMENTED;
1834 static nsresult NSAPI nsURIContentListener_SetLoadCookie(nsIURIContentListener *iface,
1835 nsISupports *aLoadCookie)
1837 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1839 WARN("(%p)->(%p)\n", This, aLoadCookie);
1841 return This->content_listener
1842 ? nsIURIContentListener_SetLoadCookie(This->content_listener, aLoadCookie)
1843 : NS_ERROR_NOT_IMPLEMENTED;
1846 static nsresult NSAPI nsURIContentListener_GetParentContentListener(nsIURIContentListener *iface,
1847 nsIURIContentListener **aParentContentListener)
1849 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1851 TRACE("(%p)->(%p)\n", This, aParentContentListener);
1853 if(This->content_listener)
1854 nsIURIContentListener_AddRef(This->content_listener);
1856 *aParentContentListener = This->content_listener;
1857 return NS_OK;
1860 static nsresult NSAPI nsURIContentListener_SetParentContentListener(nsIURIContentListener *iface,
1861 nsIURIContentListener *aParentContentListener)
1863 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1865 TRACE("(%p)->(%p)\n", This, aParentContentListener);
1867 if(aParentContentListener == &This->nsIURIContentListener_iface)
1868 return NS_OK;
1870 if(This->content_listener)
1871 nsIURIContentListener_Release(This->content_listener);
1873 This->content_listener = aParentContentListener;
1874 if(This->content_listener)
1875 nsIURIContentListener_AddRef(This->content_listener);
1877 return NS_OK;
1880 static const nsIURIContentListenerVtbl nsURIContentListenerVtbl = {
1881 nsURIContentListener_QueryInterface,
1882 nsURIContentListener_AddRef,
1883 nsURIContentListener_Release,
1884 nsURIContentListener_OnStartURIOpen,
1885 nsURIContentListener_DoContent,
1886 nsURIContentListener_IsPreferred,
1887 nsURIContentListener_CanHandleContent,
1888 nsURIContentListener_GetLoadCookie,
1889 nsURIContentListener_SetLoadCookie,
1890 nsURIContentListener_GetParentContentListener,
1891 nsURIContentListener_SetParentContentListener
1894 /**********************************************************
1895 * nsIEmbeddinSiteWindow interface
1898 static inline GeckoBrowser *impl_from_nsIEmbeddingSiteWindow(nsIEmbeddingSiteWindow *iface)
1900 return CONTAINING_RECORD(iface, GeckoBrowser, nsIEmbeddingSiteWindow_iface);
1903 static nsresult NSAPI nsEmbeddingSiteWindow_QueryInterface(nsIEmbeddingSiteWindow *iface,
1904 nsIIDRef riid, void **result)
1906 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1907 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1910 static nsrefcnt NSAPI nsEmbeddingSiteWindow_AddRef(nsIEmbeddingSiteWindow *iface)
1912 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1913 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1916 static nsrefcnt NSAPI nsEmbeddingSiteWindow_Release(nsIEmbeddingSiteWindow *iface)
1918 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1919 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
1922 static nsresult NSAPI nsEmbeddingSiteWindow_SetDimensions(nsIEmbeddingSiteWindow *iface,
1923 UINT32 flags, LONG x, LONG y, LONG cx, LONG cy)
1925 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1926 WARN("(%p)->(%08x %ld %ld %ld %ld)\n", This, flags, x, y, cx, cy);
1927 return NS_ERROR_NOT_IMPLEMENTED;
1930 static nsresult NSAPI nsEmbeddingSiteWindow_GetDimensions(nsIEmbeddingSiteWindow *iface,
1931 UINT32 flags, LONG *x, LONG *y, LONG *cx, LONG *cy)
1933 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1934 RECT r;
1936 TRACE("(%p)->(%x %p %p %p %p)\n", This, flags, x, y, cx, cy);
1938 if(!GetWindowRect(This->hwnd, &r)) {
1939 ERR("GetWindowRect failed\n");
1940 return NS_ERROR_FAILURE;
1943 if(x)
1944 *x = r.left;
1945 if(y)
1946 *y = r.top;
1947 if(cx)
1948 *cx = r.right-r.left;
1949 if(cy)
1950 *cy = r.bottom-r.top;
1951 return NS_OK;
1954 static nsresult NSAPI nsEmbeddingSiteWindow_SetFocus(nsIEmbeddingSiteWindow *iface)
1956 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1958 TRACE("(%p)\n", This);
1960 return nsIBaseWindow_SetFocus(This->window);
1963 static nsresult NSAPI nsEmbeddingSiteWindow_GetVisibility(nsIEmbeddingSiteWindow *iface,
1964 cpp_bool *aVisibility)
1966 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1968 TRACE("(%p)->(%p)\n", This, aVisibility);
1970 *aVisibility = This->doc && This->doc->hwnd && IsWindowVisible(This->doc->hwnd);
1971 return NS_OK;
1974 static nsresult NSAPI nsEmbeddingSiteWindow_SetVisibility(nsIEmbeddingSiteWindow *iface,
1975 cpp_bool aVisibility)
1977 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1979 TRACE("(%p)->(%x)\n", This, aVisibility);
1981 return NS_OK;
1984 static nsresult NSAPI nsEmbeddingSiteWindow_GetTitle(nsIEmbeddingSiteWindow *iface,
1985 PRUnichar **aTitle)
1987 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1988 WARN("(%p)->(%p)\n", This, aTitle);
1989 return NS_ERROR_NOT_IMPLEMENTED;
1992 static nsresult NSAPI nsEmbeddingSiteWindow_SetTitle(nsIEmbeddingSiteWindow *iface,
1993 const PRUnichar *aTitle)
1995 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1996 WARN("(%p)->(%s)\n", This, debugstr_w(aTitle));
1997 return NS_ERROR_NOT_IMPLEMENTED;
2000 static nsresult NSAPI nsEmbeddingSiteWindow_GetSiteWindow(nsIEmbeddingSiteWindow *iface,
2001 void **aSiteWindow)
2003 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
2005 TRACE("(%p)->(%p)\n", This, aSiteWindow);
2007 *aSiteWindow = This->hwnd;
2008 return NS_OK;
2011 static const nsIEmbeddingSiteWindowVtbl nsEmbeddingSiteWindowVtbl = {
2012 nsEmbeddingSiteWindow_QueryInterface,
2013 nsEmbeddingSiteWindow_AddRef,
2014 nsEmbeddingSiteWindow_Release,
2015 nsEmbeddingSiteWindow_SetDimensions,
2016 nsEmbeddingSiteWindow_GetDimensions,
2017 nsEmbeddingSiteWindow_SetFocus,
2018 nsEmbeddingSiteWindow_GetVisibility,
2019 nsEmbeddingSiteWindow_SetVisibility,
2020 nsEmbeddingSiteWindow_GetTitle,
2021 nsEmbeddingSiteWindow_SetTitle,
2022 nsEmbeddingSiteWindow_GetSiteWindow
2025 static inline GeckoBrowser *impl_from_nsITooltipListener(nsITooltipListener *iface)
2027 return CONTAINING_RECORD(iface, GeckoBrowser, nsITooltipListener_iface);
2030 static nsresult NSAPI nsTooltipListener_QueryInterface(nsITooltipListener *iface, nsIIDRef riid,
2031 void **result)
2033 GeckoBrowser *This = impl_from_nsITooltipListener(iface);
2034 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
2037 static nsrefcnt NSAPI nsTooltipListener_AddRef(nsITooltipListener *iface)
2039 GeckoBrowser *This = impl_from_nsITooltipListener(iface);
2040 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
2043 static nsrefcnt NSAPI nsTooltipListener_Release(nsITooltipListener *iface)
2045 GeckoBrowser *This = impl_from_nsITooltipListener(iface);
2046 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
2049 static nsresult NSAPI nsTooltipListener_OnShowTooltip(nsITooltipListener *iface,
2050 LONG aXCoord, LONG aYCoord, const PRUnichar *aTipText)
2052 GeckoBrowser *This = impl_from_nsITooltipListener(iface);
2054 if (This->doc)
2055 show_tooltip(This->doc, aXCoord, aYCoord, aTipText);
2057 return NS_OK;
2060 static nsresult NSAPI nsTooltipListener_OnHideTooltip(nsITooltipListener *iface)
2062 GeckoBrowser *This = impl_from_nsITooltipListener(iface);
2064 if (This->doc)
2065 hide_tooltip(This->doc);
2067 return NS_OK;
2070 static const nsITooltipListenerVtbl nsTooltipListenerVtbl = {
2071 nsTooltipListener_QueryInterface,
2072 nsTooltipListener_AddRef,
2073 nsTooltipListener_Release,
2074 nsTooltipListener_OnShowTooltip,
2075 nsTooltipListener_OnHideTooltip
2078 static inline GeckoBrowser *impl_from_nsIInterfaceRequestor(nsIInterfaceRequestor *iface)
2080 return CONTAINING_RECORD(iface, GeckoBrowser, nsIInterfaceRequestor_iface);
2083 static nsresult NSAPI nsInterfaceRequestor_QueryInterface(nsIInterfaceRequestor *iface,
2084 nsIIDRef riid, void **result)
2086 GeckoBrowser *This = impl_from_nsIInterfaceRequestor(iface);
2087 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
2090 static nsrefcnt NSAPI nsInterfaceRequestor_AddRef(nsIInterfaceRequestor *iface)
2092 GeckoBrowser *This = impl_from_nsIInterfaceRequestor(iface);
2093 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
2096 static nsrefcnt NSAPI nsInterfaceRequestor_Release(nsIInterfaceRequestor *iface)
2098 GeckoBrowser *This = impl_from_nsIInterfaceRequestor(iface);
2099 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
2102 static nsresult NSAPI nsInterfaceRequestor_GetInterface(nsIInterfaceRequestor *iface,
2103 nsIIDRef riid, void **result)
2105 GeckoBrowser *This = impl_from_nsIInterfaceRequestor(iface);
2107 if(IsEqualGUID(&IID_mozIDOMWindowProxy, riid)) {
2108 TRACE("(%p)->(IID_nsIDOMWindow %p)\n", This, result);
2109 return nsIWebBrowser_GetContentDOMWindow(This->webbrowser, (mozIDOMWindowProxy**)result);
2112 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
2115 static const nsIInterfaceRequestorVtbl nsInterfaceRequestorVtbl = {
2116 nsInterfaceRequestor_QueryInterface,
2117 nsInterfaceRequestor_AddRef,
2118 nsInterfaceRequestor_Release,
2119 nsInterfaceRequestor_GetInterface
2122 static inline GeckoBrowser *impl_from_nsISupportsWeakReference(nsISupportsWeakReference *iface)
2124 return CONTAINING_RECORD(iface, GeckoBrowser, nsISupportsWeakReference_iface);
2127 static nsresult NSAPI nsSupportsWeakReference_QueryInterface(nsISupportsWeakReference *iface,
2128 nsIIDRef riid, void **result)
2130 GeckoBrowser *This = impl_from_nsISupportsWeakReference(iface);
2131 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
2134 static nsrefcnt NSAPI nsSupportsWeakReference_AddRef(nsISupportsWeakReference *iface)
2136 GeckoBrowser *This = impl_from_nsISupportsWeakReference(iface);
2137 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
2140 static nsrefcnt NSAPI nsSupportsWeakReference_Release(nsISupportsWeakReference *iface)
2142 GeckoBrowser *This = impl_from_nsISupportsWeakReference(iface);
2143 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
2146 static nsresult NSAPI nsSupportsWeakReference_GetWeakReference(nsISupportsWeakReference *iface,
2147 nsIWeakReference **_retval)
2149 GeckoBrowser *This = impl_from_nsISupportsWeakReference(iface);
2151 TRACE("(%p)->(%p)\n", This, _retval);
2153 if(!This->weak_reference) {
2154 This->weak_reference = malloc(sizeof(nsWeakReference));
2155 if(!This->weak_reference)
2156 return NS_ERROR_OUT_OF_MEMORY;
2158 This->weak_reference->nsIWeakReference_iface.lpVtbl = &nsWeakReferenceVtbl;
2159 This->weak_reference->ref = 1;
2160 This->weak_reference->browser = This;
2163 *_retval = &This->weak_reference->nsIWeakReference_iface;
2164 nsIWeakReference_AddRef(*_retval);
2165 return NS_OK;
2168 static const nsISupportsWeakReferenceVtbl nsSupportsWeakReferenceVtbl = {
2169 nsSupportsWeakReference_QueryInterface,
2170 nsSupportsWeakReference_AddRef,
2171 nsSupportsWeakReference_Release,
2172 nsSupportsWeakReference_GetWeakReference
2175 static HRESULT init_browser(GeckoBrowser *browser)
2177 mozIDOMWindowProxy *mozwindow;
2178 nsIWebBrowserSetup *wbsetup;
2179 nsIScrollable *scrollable;
2180 nsresult nsres;
2181 HRESULT hres;
2183 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_WEBBROWSER_CONTRACTID,
2184 NULL, &IID_nsIWebBrowser, (void**)&browser->webbrowser);
2185 if(NS_FAILED(nsres)) {
2186 ERR("Creating WebBrowser failed: %08lx\n", nsres);
2187 return E_FAIL;
2190 nsres = nsIWebBrowser_SetContainerWindow(browser->webbrowser, &browser->nsIWebBrowserChrome_iface);
2191 if(NS_FAILED(nsres)) {
2192 ERR("SetContainerWindow failed: %08lx\n", nsres);
2193 return E_FAIL;
2196 nsres = nsIWebBrowser_QueryInterface(browser->webbrowser, &IID_nsIBaseWindow,
2197 (void**)&browser->window);
2198 if(NS_FAILED(nsres)) {
2199 ERR("Could not get nsIBaseWindow interface: %08lx\n", nsres);
2200 return E_FAIL;
2203 nsres = nsIWebBrowser_QueryInterface(browser->webbrowser, &IID_nsIWebBrowserSetup,
2204 (void**)&wbsetup);
2205 if(NS_SUCCEEDED(nsres)) {
2206 nsres = nsIWebBrowserSetup_SetProperty(wbsetup, SETUP_IS_CHROME_WRAPPER, FALSE);
2207 nsIWebBrowserSetup_Release(wbsetup);
2208 if(NS_FAILED(nsres)) {
2209 ERR("SetProperty(SETUP_IS_CHROME_WRAPPER) failed: %08lx\n", nsres);
2210 return E_FAIL;
2212 }else {
2213 ERR("Could not get nsIWebBrowserSetup interface\n");
2214 return E_FAIL;
2217 nsres = nsIWebBrowser_QueryInterface(browser->webbrowser, &IID_nsIWebNavigation,
2218 (void**)&browser->navigation);
2219 if(NS_FAILED(nsres)) {
2220 ERR("Could not get nsIWebNavigation interface: %08lx\n", nsres);
2221 return E_FAIL;
2224 nsres = nsIWebBrowser_QueryInterface(browser->webbrowser, &IID_nsIWebBrowserFocus,
2225 (void**)&browser->focus);
2226 if(NS_FAILED(nsres)) {
2227 ERR("Could not get nsIWebBrowserFocus interface: %08lx\n", nsres);
2228 return E_FAIL;
2231 if(!browser_class) {
2232 register_browser_class();
2233 if(!browser_class)
2234 return E_FAIL;
2237 browser->hwnd = CreateWindowExW(0, L"NsContainer", NULL,
2238 WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 0, 0, 100, 100,
2239 GetDesktopWindow(), NULL, hInst, browser);
2240 if(!browser->hwnd) {
2241 WARN("Could not create window\n");
2242 return E_FAIL;
2245 nsres = nsIBaseWindow_InitWindow(browser->window, browser->hwnd, NULL, 0, 0, 100, 100);
2246 if(NS_SUCCEEDED(nsres)) {
2247 nsres = nsIBaseWindow_Create(browser->window);
2248 if(NS_FAILED(nsres)) {
2249 WARN("Creating window failed: %08lx\n", nsres);
2250 return E_FAIL;
2253 nsIBaseWindow_SetVisibility(browser->window, FALSE);
2254 nsIBaseWindow_SetEnabled(browser->window, FALSE);
2255 }else {
2256 ERR("InitWindow failed: %08lx\n", nsres);
2257 return E_FAIL;
2260 nsres = nsIWebBrowser_SetParentURIContentListener(browser->webbrowser,
2261 &browser->nsIURIContentListener_iface);
2262 if(NS_FAILED(nsres))
2263 ERR("SetParentURIContentListener failed: %08lx\n", nsres);
2265 nsres = nsIWebBrowser_QueryInterface(browser->webbrowser, &IID_nsIScrollable, (void**)&scrollable);
2266 if(NS_SUCCEEDED(nsres)) {
2267 nsres = nsIScrollable_SetDefaultScrollbarPreferences(scrollable,
2268 ScrollOrientation_Y, Scrollbar_Auto);
2269 if(NS_FAILED(nsres))
2270 ERR("Could not set default Y scrollbar prefs: %08lx\n", nsres);
2272 nsres = nsIScrollable_SetDefaultScrollbarPreferences(scrollable,
2273 ScrollOrientation_X, Scrollbar_Auto);
2274 if(NS_FAILED(nsres))
2275 ERR("Could not set default X scrollbar prefs: %08lx\n", nsres);
2277 nsIScrollable_Release(scrollable);
2278 }else {
2279 ERR("Could not get nsIScrollable: %08lx\n", nsres);
2282 nsres = nsIWebBrowser_GetContentDOMWindow(browser->webbrowser, &mozwindow);
2283 if(NS_FAILED(nsres)) {
2284 ERR("GetContentDOMWindow failed: %08lx\n", nsres);
2285 return E_FAIL;
2288 hres = create_outer_window(browser, mozwindow, NULL, &browser->content_window);
2289 mozIDOMWindowProxy_Release(mozwindow);
2290 return hres;
2293 HRESULT create_gecko_browser(HTMLDocumentObj *doc, GeckoBrowser **_ret)
2295 GeckoBrowser *ret;
2296 HRESULT hres;
2298 if(!load_gecko())
2299 return CLASS_E_CLASSNOTAVAILABLE;
2301 ret = calloc(1, sizeof(GeckoBrowser));
2302 if(!ret)
2303 return E_OUTOFMEMORY;
2305 ret->nsIWebBrowserChrome_iface.lpVtbl = &nsWebBrowserChromeVtbl;
2306 ret->nsIContextMenuListener_iface.lpVtbl = &nsContextMenuListenerVtbl;
2307 ret->nsIURIContentListener_iface.lpVtbl = &nsURIContentListenerVtbl;
2308 ret->nsIEmbeddingSiteWindow_iface.lpVtbl = &nsEmbeddingSiteWindowVtbl;
2309 ret->nsITooltipListener_iface.lpVtbl = &nsTooltipListenerVtbl;
2310 ret->nsIInterfaceRequestor_iface.lpVtbl = &nsInterfaceRequestorVtbl;
2311 ret->nsISupportsWeakReference_iface.lpVtbl = &nsSupportsWeakReferenceVtbl;
2313 ret->doc = doc;
2314 ret->ref = 1;
2315 ret->script_mode = SCRIPTMODE_ACTIVESCRIPT;
2316 ret->usermode = UNKNOWN_USERMODE;
2317 list_init(&ret->document_nodes);
2318 list_init(&ret->outer_windows);
2320 hres = init_browser(ret);
2321 if(SUCCEEDED(hres))
2322 *_ret = ret;
2323 else
2324 nsIWebBrowserChrome_Release(&ret->nsIWebBrowserChrome_iface);
2325 return hres;
2328 void detach_gecko_browser(GeckoBrowser *This)
2330 nsIDOMWindowUtils *window_utils = NULL;
2332 TRACE("(%p)\n", This);
2334 This->doc = NULL;
2336 if(This->content_window) {
2337 get_nsinterface((nsISupports*)This->content_window->nswindow, &IID_nsIDOMWindowUtils, (void**)&window_utils);
2339 IHTMLWindow2_Release(&This->content_window->base.IHTMLWindow2_iface);
2340 This->content_window = NULL;
2343 while(!list_empty(&This->document_nodes)) {
2344 HTMLDocumentNode *doc = LIST_ENTRY(list_head(&This->document_nodes), HTMLDocumentNode, browser_entry);
2345 detach_document_node(doc);
2348 while(!list_empty(&This->outer_windows)) {
2349 HTMLOuterWindow *window = LIST_ENTRY(list_head(&This->outer_windows), HTMLOuterWindow, browser_entry);
2350 list_remove(&window->browser_entry);
2351 window->browser = NULL;
2354 ShowWindow(This->hwnd, SW_HIDE);
2355 SetParent(This->hwnd, NULL);
2357 nsIBaseWindow_SetVisibility(This->window, FALSE);
2358 nsIBaseWindow_Destroy(This->window);
2360 nsIWebBrowser_SetContainerWindow(This->webbrowser, NULL);
2362 nsIWebBrowser_Release(This->webbrowser);
2363 This->webbrowser = NULL;
2365 nsIWebNavigation_Release(This->navigation);
2366 This->navigation = NULL;
2368 nsIBaseWindow_Release(This->window);
2369 This->window = NULL;
2371 nsIWebBrowserFocus_Release(This->focus);
2372 This->focus = NULL;
2374 if(This->editor_controller) {
2375 nsIController_Release(This->editor_controller);
2376 This->editor_controller = NULL;
2379 if(This->editor) {
2380 nsIEditor_Release(This->editor);
2381 This->editor = NULL;
2384 if(This->content_listener) {
2385 nsIURIContentListener_Release(This->content_listener);
2386 This->content_listener = NULL;
2389 if(This->hwnd) {
2390 DestroyWindow(This->hwnd);
2391 This->hwnd = NULL;
2394 nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
2396 /* Force cycle collection */
2397 if(window_utils) {
2398 nsIDOMWindowUtils_CycleCollect(window_utils, NULL, 0);
2399 nsIDOMWindowUtils_Release(window_utils);
2404 * FIXME: nsIScriptObjectPrincipal uses thiscall calling convention, so we need this hack on i386.
2405 * This will be removed after the next Gecko update, that will change calling convention on Gecko side.
2407 #ifdef __i386__
2408 __ASM_GLOBAL_FUNC(call_thiscall_func,
2409 "popl %eax\n\t"
2410 "popl %edx\n\t"
2411 "popl %ecx\n\t"
2412 "pushl %eax\n\t"
2413 "jmp *%edx\n\t")
2414 #define nsIScriptObjectPrincipal_GetPrincipal(this) ((void* (WINAPI*)(void*,void*))&call_thiscall_func)((this)->lpVtbl->GetPrincipal,this)
2415 #endif
2417 nsIXMLHttpRequest *create_nsxhr(nsIDOMWindow *nswindow)
2419 nsIScriptObjectPrincipal *sop;
2420 mozIDOMWindow *inner_window;
2421 nsIPrincipal *nspri;
2422 nsIGlobalObject *nsglo;
2423 nsIXMLHttpRequest *nsxhr;
2424 nsresult nsres;
2426 nsres = nsIDOMWindow_GetInnerWindow(nswindow, &inner_window);
2427 if(NS_FAILED(nsres)) {
2428 ERR("Could not get inner window: %08lx\n", nsres);
2429 return NULL;
2432 nsres = mozIDOMWindow_QueryInterface(inner_window, &IID_nsIGlobalObject, (void **)&nsglo);
2433 mozIDOMWindow_Release(inner_window);
2434 assert(nsres == NS_OK);
2436 nsres = nsIGlobalObject_QueryInterface(nsglo, &IID_nsIScriptObjectPrincipal, (void**)&sop);
2437 assert(nsres == NS_OK);
2439 /* The returned principal is *not* AddRef'd */
2440 nspri = nsIScriptObjectPrincipal_GetPrincipal(sop);
2441 nsIScriptObjectPrincipal_Release(sop);
2443 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
2444 NS_XMLHTTPREQUEST_CONTRACTID, NULL, &IID_nsIXMLHttpRequest,
2445 (void**)&nsxhr);
2446 if(NS_SUCCEEDED(nsres)) {
2447 nsres = nsIXMLHttpRequest_Init(nsxhr, nspri, NULL, nsglo, NULL, NULL);
2448 if(NS_FAILED(nsres))
2449 nsIXMLHttpRequest_Release(nsxhr);
2451 nsIGlobalObject_Release(nsglo);
2452 if(NS_FAILED(nsres)) {
2453 ERR("nsIXMLHttpRequest_Init failed: %08lx\n", nsres);
2454 return NULL;
2457 return nsxhr;