msvcrt: Use fpclass constants from public header.
[wine/zf.git] / dlls / mshtml / nsembed.c
blob750f5cfd5e04d5b381e6f1da186b4bf737d4305e
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>
20 #include <assert.h>
22 #define COBJMACROS
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winuser.h"
27 #include "winreg.h"
28 #include "ole2.h"
29 #include "shlobj.h"
30 #include "shlwapi.h"
32 #include "wine/asm.h"
33 #include "wine/debug.h"
35 #include "mshtml_private.h"
36 #include "htmlevent.h"
37 #include "binding.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
40 WINE_DECLARE_DEBUG_CHANNEL(gecko);
42 #define NS_APPSTARTUPNOTIFIER_CONTRACTID "@mozilla.org/embedcomp/appstartup-notifier;1"
43 #define NS_WEBBROWSER_CONTRACTID "@mozilla.org/embedding/browser/nsWebBrowser;1"
44 #define NS_COMMANDPARAMS_CONTRACTID "@mozilla.org/embedcomp/command-params;1"
45 #define NS_HTMLSERIALIZER_CONTRACTID "@mozilla.org/layout/contentserializer;1?mimetype=text/html"
46 #define NS_EDITORCONTROLLER_CONTRACTID "@mozilla.org/editor/editorcontroller;1"
47 #define NS_PREFERENCES_CONTRACTID "@mozilla.org/preferences;1"
48 #define NS_VARIANT_CONTRACTID "@mozilla.org/variant;1"
49 #define NS_CATEGORYMANAGER_CONTRACTID "@mozilla.org/categorymanager;1"
50 #define NS_XMLHTTPREQUEST_CONTRACTID "@mozilla.org/xmlextras/xmlhttprequest;1"
52 #define PR_UINT32_MAX 0xffffffff
54 #define NS_STRING_CONTAINER_INIT_DEPEND 0x0002
55 #define NS_CSTRING_CONTAINER_INIT_DEPEND 0x0002
57 #ifdef __i386__
58 #define GECKO_ARCH_STRING "x86"
59 #elif defined(__x86_64__)
60 #define GECKO_ARCH_STRING "x86_64"
61 #else
62 #define GECKO_ARCH_STRING ""
63 #endif
65 #define GECKO_DIR_NAME "wine-gecko-" GECKO_VERSION "-" GECKO_ARCH_STRING
67 typedef UINT32 PRUint32;
69 static nsresult (CDECL *NS_InitXPCOM2)(nsIServiceManager**,void*,void*);
70 static nsresult (CDECL *NS_ShutdownXPCOM)(nsIServiceManager*);
71 static nsresult (CDECL *NS_GetComponentRegistrar)(nsIComponentRegistrar**);
72 static nsresult (CDECL *NS_StringContainerInit2)(nsStringContainer*,const PRUnichar*,PRUint32,PRUint32);
73 static nsresult (CDECL *NS_CStringContainerInit2)(nsCStringContainer*,const char*,PRUint32,PRUint32);
74 static nsresult (CDECL *NS_StringContainerFinish)(nsStringContainer*);
75 static nsresult (CDECL *NS_CStringContainerFinish)(nsCStringContainer*);
76 static nsresult (CDECL *NS_StringSetData)(nsAString*,const PRUnichar*,PRUint32);
77 static nsresult (CDECL *NS_CStringSetData)(nsACString*,const char*,PRUint32);
78 static nsresult (CDECL *NS_NewLocalFile)(const nsAString*,cpp_bool,nsIFile**);
79 static PRUint32 (CDECL *NS_StringGetData)(const nsAString*,const PRUnichar **,cpp_bool*);
80 static PRUint32 (CDECL *NS_CStringGetData)(const nsACString*,const char**,cpp_bool*);
81 static cpp_bool (CDECL *NS_StringGetIsVoid)(const nsAString*);
82 static void* (CDECL *NS_Alloc)(SIZE_T);
83 static void (CDECL *NS_Free)(void*);
85 static HINSTANCE xul_handle = NULL;
87 static nsIServiceManager *pServMgr = NULL;
88 static nsIComponentManager *pCompMgr = NULL;
89 static nsICategoryManager *cat_mgr;
90 static nsIFile *profile_directory, *plugin_directory;
92 static ATOM browser_class;
93 static WCHAR gecko_path[MAX_PATH];
94 static unsigned gecko_path_len;
96 nsresult create_nsfile(const PRUnichar *path, nsIFile **ret)
98 nsAString str;
99 nsresult nsres;
101 nsAString_InitDepend(&str, path);
102 nsres = NS_NewLocalFile(&str, FALSE, ret);
103 nsAString_Finish(&str);
105 if(NS_FAILED(nsres))
106 WARN("NS_NewLocalFile failed: %08x\n", nsres);
107 return nsres;
110 typedef struct {
111 nsISimpleEnumerator nsISimpleEnumerator_iface;
112 LONG ref;
113 nsISupports *value;
114 } nsSingletonEnumerator;
116 static inline nsSingletonEnumerator *impl_from_nsISimpleEnumerator(nsISimpleEnumerator *iface)
118 return CONTAINING_RECORD(iface, nsSingletonEnumerator, nsISimpleEnumerator_iface);
121 static nsresult NSAPI nsSingletonEnumerator_QueryInterface(nsISimpleEnumerator *iface, nsIIDRef riid, void **ppv)
123 nsSingletonEnumerator *This = impl_from_nsISimpleEnumerator(iface);
125 if(IsEqualGUID(&IID_nsISupports, riid)) {
126 TRACE("(%p)->(IID_nsISupports %p)\n", This, ppv);
127 *ppv = &This->nsISimpleEnumerator_iface;
128 }else if(IsEqualGUID(&IID_nsISimpleEnumerator, riid)) {
129 TRACE("(%p)->(IID_nsISimpleEnumerator %p)\n", This, ppv);
130 *ppv = &This->nsISimpleEnumerator_iface;
131 }else {
132 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
133 *ppv = NULL;
134 return NS_NOINTERFACE;
137 nsISupports_AddRef((nsISupports*)*ppv);
138 return NS_OK;
141 static nsrefcnt NSAPI nsSingletonEnumerator_AddRef(nsISimpleEnumerator *iface)
143 nsSingletonEnumerator *This = impl_from_nsISimpleEnumerator(iface);
144 nsrefcnt ref = InterlockedIncrement(&This->ref);
146 TRACE("(%p) ref=%d\n", This, ref);
148 return ref;
151 static nsrefcnt NSAPI nsSingletonEnumerator_Release(nsISimpleEnumerator *iface)
153 nsSingletonEnumerator *This = impl_from_nsISimpleEnumerator(iface);
154 nsrefcnt ref = InterlockedDecrement(&This->ref);
156 TRACE("(%p) ref=%d\n", This, ref);
158 if(!ref) {
159 if(This->value)
160 nsISupports_Release(This->value);
161 heap_free(This);
164 return ref;
167 static nsresult NSAPI nsSingletonEnumerator_HasMoreElements(nsISimpleEnumerator *iface, cpp_bool *_retval)
169 nsSingletonEnumerator *This = impl_from_nsISimpleEnumerator(iface);
171 TRACE("(%p)->()\n", This);
173 *_retval = This->value != NULL;
174 return NS_OK;
177 static nsresult NSAPI nsSingletonEnumerator_GetNext(nsISimpleEnumerator *iface, nsISupports **_retval)
179 nsSingletonEnumerator *This = impl_from_nsISimpleEnumerator(iface);
181 TRACE("(%p)->()\n", This);
183 if(!This->value)
184 return NS_ERROR_UNEXPECTED;
186 *_retval = This->value;
187 This->value = NULL;
188 return NS_OK;
191 static const nsISimpleEnumeratorVtbl nsSingletonEnumeratorVtbl = {
192 nsSingletonEnumerator_QueryInterface,
193 nsSingletonEnumerator_AddRef,
194 nsSingletonEnumerator_Release,
195 nsSingletonEnumerator_HasMoreElements,
196 nsSingletonEnumerator_GetNext
199 static nsISimpleEnumerator *create_singleton_enumerator(nsISupports *value)
201 nsSingletonEnumerator *ret;
203 ret = heap_alloc(sizeof(*ret));
204 if(!ret)
205 return NULL;
207 ret->nsISimpleEnumerator_iface.lpVtbl = &nsSingletonEnumeratorVtbl;
208 ret->ref = 1;
210 if(value)
211 nsISupports_AddRef(value);
212 ret->value = value;
213 return &ret->nsISimpleEnumerator_iface;
216 static nsresult NSAPI nsDirectoryServiceProvider2_QueryInterface(nsIDirectoryServiceProvider2 *iface,
217 nsIIDRef riid, void **result)
219 if(IsEqualGUID(&IID_nsISupports, riid)) {
220 TRACE("(IID_nsISupports %p)\n", result);
221 *result = iface;
222 }else if(IsEqualGUID(&IID_nsIDirectoryServiceProvider, riid)) {
223 TRACE("(IID_nsIDirectoryServiceProvider %p)\n", result);
224 *result = iface;
225 }else if(IsEqualGUID(&IID_nsIDirectoryServiceProvider2, riid)) {
226 TRACE("(IID_nsIDirectoryServiceProvider2 %p)\n", result);
227 *result = iface;
228 }else {
229 WARN("(%s %p)\n", debugstr_guid(riid), result);
230 *result = NULL;
231 return NS_NOINTERFACE;
234 nsISupports_AddRef((nsISupports*)*result);
235 return NS_OK;
238 static nsrefcnt NSAPI nsDirectoryServiceProvider2_AddRef(nsIDirectoryServiceProvider2 *iface)
240 return 2;
243 static nsrefcnt NSAPI nsDirectoryServiceProvider2_Release(nsIDirectoryServiceProvider2 *iface)
245 return 1;
248 static nsresult create_profile_directory(void)
250 WCHAR path[MAX_PATH + ARRAY_SIZE(L"\\wine_gecko")];
251 cpp_bool exists;
252 nsresult nsres;
253 HRESULT hres;
255 hres = SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, path);
256 if(FAILED(hres)) {
257 ERR("SHGetFolderPath failed: %08x\n", hres);
258 return NS_ERROR_FAILURE;
261 lstrcatW(path, L"\\wine_gecko");
262 nsres = create_nsfile(path, &profile_directory);
263 if(NS_FAILED(nsres))
264 return nsres;
266 nsres = nsIFile_Exists(profile_directory, &exists);
267 if(NS_FAILED(nsres)) {
268 ERR("Exists failed: %08x\n", nsres);
269 return nsres;
272 if(!exists) {
273 nsres = nsIFile_Create(profile_directory, 1, 0700);
274 if(NS_FAILED(nsres))
275 ERR("Create failed: %08x\n", nsres);
278 return nsres;
281 static nsresult NSAPI nsDirectoryServiceProvider2_GetFile(nsIDirectoryServiceProvider2 *iface,
282 const char *prop, cpp_bool *persistent, nsIFile **_retval)
284 TRACE("(%s %p %p)\n", debugstr_a(prop), persistent, _retval);
286 if(!strcmp(prop, "ProfD")) {
287 if(!profile_directory) {
288 nsresult nsres;
290 nsres = create_profile_directory();
291 if(NS_FAILED(nsres))
292 return nsres;
295 assert(profile_directory != NULL);
296 return nsIFile_Clone(profile_directory, _retval);
299 *_retval = NULL;
300 return NS_ERROR_FAILURE;
303 static nsresult NSAPI nsDirectoryServiceProvider2_GetFiles(nsIDirectoryServiceProvider2 *iface,
304 const char *prop, nsISimpleEnumerator **_retval)
306 TRACE("(%s %p)\n", debugstr_a(prop), _retval);
308 if(!strcmp(prop, "APluginsDL")) {
309 WCHAR plugin_path[MAX_PATH];
310 nsIFile *file;
311 int len;
312 nsresult nsres;
314 if(!plugin_directory) {
315 len = GetSystemDirectoryW(plugin_path, ARRAY_SIZE(plugin_path)-ARRAY_SIZE(L"\\gecko\\plugin")+1);
316 if(!len)
317 return NS_ERROR_UNEXPECTED;
319 lstrcpyW(plugin_path+len, L"\\gecko\\plugin");
320 nsres = create_nsfile(plugin_path, &plugin_directory);
321 if(NS_FAILED(nsres)) {
322 *_retval = NULL;
323 return nsres;
327 nsres = nsIFile_Clone(plugin_directory, &file);
328 if(NS_FAILED(nsres))
329 return nsres;
331 *_retval = create_singleton_enumerator((nsISupports*)file);
332 nsIFile_Release(file);
333 if(!*_retval)
334 return NS_ERROR_OUT_OF_MEMORY;
336 return NS_OK;
339 *_retval = NULL;
340 return NS_ERROR_FAILURE;
343 static const nsIDirectoryServiceProvider2Vtbl nsDirectoryServiceProvider2Vtbl = {
344 nsDirectoryServiceProvider2_QueryInterface,
345 nsDirectoryServiceProvider2_AddRef,
346 nsDirectoryServiceProvider2_Release,
347 nsDirectoryServiceProvider2_GetFile,
348 nsDirectoryServiceProvider2_GetFiles
351 static nsIDirectoryServiceProvider2 nsDirectoryServiceProvider2 =
352 { &nsDirectoryServiceProvider2Vtbl };
354 static LRESULT WINAPI nsembed_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
356 GeckoBrowser *This;
357 nsresult nsres;
359 if(msg == WM_CREATE) {
360 This = *(GeckoBrowser**)lParam;
361 SetPropW(hwnd, L"THIS", This);
362 }else {
363 This = GetPropW(hwnd, L"THIS");
366 switch(msg) {
367 case WM_SIZE:
368 TRACE("(%p)->(WM_SIZE)\n", This);
370 nsres = nsIBaseWindow_SetSize(This->window,
371 LOWORD(lParam), HIWORD(lParam), TRUE);
372 if(NS_FAILED(nsres))
373 WARN("SetSize failed: %08x\n", nsres);
374 break;
376 case WM_PARENTNOTIFY:
377 TRACE("WM_PARENTNOTIFY %x\n", (unsigned)wParam);
379 switch(wParam) {
380 case WM_LBUTTONDOWN:
381 case WM_RBUTTONDOWN:
382 nsIWebBrowserFocus_Activate(This->focus);
386 return DefWindowProcW(hwnd, msg, wParam, lParam);
390 static void register_browser_class(void)
392 static WNDCLASSEXW wndclass = {
393 sizeof(WNDCLASSEXW),
394 CS_DBLCLKS,
395 nsembed_proc,
396 0, 0, NULL, NULL, NULL, NULL, NULL,
397 L"NsContainer",
398 NULL,
400 wndclass.hInstance = hInst;
401 browser_class = RegisterClassExW(&wndclass);
404 static BOOL install_wine_gecko(void)
406 PROCESS_INFORMATION pi;
407 STARTUPINFOW si;
408 WCHAR app[MAX_PATH];
409 WCHAR *args;
410 LONG len;
411 BOOL ret;
413 static const WCHAR controlW[] = L"\\control.exe";
414 static const WCHAR argsW[] = L" appwiz.cpl install_gecko";
416 len = GetSystemDirectoryW(app, MAX_PATH-ARRAY_SIZE(controlW));
417 memcpy(app+len, controlW, sizeof(controlW));
419 args = heap_alloc(len*sizeof(WCHAR) + sizeof(controlW) + sizeof(argsW));
420 if(!args)
421 return FALSE;
423 memcpy(args, app, len*sizeof(WCHAR) + sizeof(controlW));
424 memcpy(args + len + ARRAY_SIZE(controlW)-1, argsW, sizeof(argsW));
426 TRACE("starting %s\n", debugstr_w(args));
428 memset(&si, 0, sizeof(si));
429 si.cb = sizeof(si);
430 ret = CreateProcessW(app, args, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
431 heap_free(args);
432 if (ret) {
433 CloseHandle(pi.hThread);
434 WaitForSingleObject(pi.hProcess, INFINITE);
435 CloseHandle(pi.hProcess);
436 }else {
437 WARN("installation failed\n");
440 return ret;
443 static void set_environment(LPCWSTR gre_path)
445 size_t len, gre_path_len;
446 int debug_level = 0;
447 WCHAR *path, buf[20];
448 const WCHAR *ptr;
450 SetEnvironmentVariableW(L"XPCOM_DEBUG_BREAK", L"warn");
452 if(TRACE_ON(gecko))
453 debug_level = 5;
454 else if(WARN_ON(gecko))
455 debug_level = 2;
456 else if(ERR_ON(gecko))
457 debug_level = 1;
459 swprintf(buf, ARRAY_SIZE(buf), L"all:%d", debug_level);
460 SetEnvironmentVariableW(L"NSPR_LOG_MODULES", buf);
462 len = GetEnvironmentVariableW(L"PATH", NULL, 0);
463 gre_path_len = lstrlenW(gre_path);
464 path = heap_alloc((len+gre_path_len+1)*sizeof(WCHAR));
465 if(!path)
466 return;
467 GetEnvironmentVariableW(L"PATH", path, len);
469 /* We have to modify PATH as xul.dll loads other DLLs from this directory. */
470 if(!(ptr = wcsstr(path, gre_path))
471 || (ptr > path && *(ptr-1) != ';')
472 || (ptr[gre_path_len] && ptr[gre_path_len] != ';')) {
473 if(len)
474 path[len-1] = ';';
475 lstrcpyW(path+len, gre_path);
476 SetEnvironmentVariableW(L"PATH", path);
478 heap_free(path);
481 static void set_bool_pref(nsIPrefBranch *pref, const char *pref_name, BOOL val)
483 nsresult nsres;
485 nsres = nsIPrefBranch_SetBoolPref(pref, pref_name, val);
486 if(NS_FAILED(nsres))
487 ERR("Could not set pref %s\n", debugstr_a(pref_name));
490 static void set_int_pref(nsIPrefBranch *pref, const char *pref_name, int val)
492 nsresult nsres;
494 nsres = nsIPrefBranch_SetIntPref(pref, pref_name, val);
495 if(NS_FAILED(nsres))
496 ERR("Could not set pref %s\n", debugstr_a(pref_name));
499 static void set_string_pref(nsIPrefBranch *pref, const char *pref_name, const char *val)
501 nsresult nsres;
503 nsres = nsIPrefBranch_SetCharPref(pref, pref_name, val);
504 if(NS_FAILED(nsres))
505 ERR("Could not set pref %s\n", debugstr_a(pref_name));
508 static void set_lang(nsIPrefBranch *pref)
510 char langs[100];
511 DWORD res, size, type;
512 HKEY hkey;
514 res = RegOpenKeyW(HKEY_CURRENT_USER, L"Software\\Microsoft\\Internet Explorer\\International", &hkey);
515 if(res != ERROR_SUCCESS)
516 return;
518 size = sizeof(langs);
519 res = RegQueryValueExA(hkey, "AcceptLanguage", 0, &type, (LPBYTE)langs, &size);
520 RegCloseKey(hkey);
521 if(res != ERROR_SUCCESS || type != REG_SZ)
522 return;
524 TRACE("Setting lang %s\n", debugstr_a(langs));
526 set_string_pref(pref, "intl.accept_languages", langs);
529 static void set_preferences(void)
531 nsIPrefBranch *pref;
532 nsresult nsres;
534 nsres = nsIServiceManager_GetServiceByContractID(pServMgr, NS_PREFERENCES_CONTRACTID,
535 &IID_nsIPrefBranch, (void**)&pref);
536 if(NS_FAILED(nsres)) {
537 ERR("Could not get preference service: %08x\n", nsres);
538 return;
541 set_lang(pref);
542 set_bool_pref(pref, "security.warn_entering_secure", FALSE);
543 set_bool_pref(pref, "security.warn_submit_insecure", FALSE);
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: %08x\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: %08x\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: %08x\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: %08x\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);
613 gecko_path[len] = 0;
614 if(!xul_handle) {
615 WARN("Could not load XUL: %d\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 = ARRAY_SIZE(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;
922 if(NS_FAILED(nsres)) {
923 WARN("failed: %08x\n", nsres);
924 nsAString_Finish(nsstr);
925 return map_nsresult(nsres);
928 nsAString_GetData(nsstr, &str);
929 TRACE("ret %s\n", debugstr_w(str));
930 if(*str) {
931 *p = SysAllocString(str);
932 if(!*p)
933 return E_OUTOFMEMORY;
934 }else {
935 *p = NULL;
938 nsAString_Finish(nsstr);
939 return S_OK;
942 HRESULT return_nsstr_variant(nsresult nsres, nsAString *nsstr, unsigned flags, VARIANT *p)
944 HRESULT hres = S_OK;
946 if(NS_FAILED(nsres)) {
947 ERR("failed: %08x\n", nsres);
948 nsAString_Finish(nsstr);
949 return map_nsresult(nsres);
952 if(NS_StringGetIsVoid(nsstr)) {
953 V_VT(p) = VT_NULL;
954 }else {
955 const WCHAR *str;
956 size_t len;
957 nsAString_GetData(nsstr, &str);
958 len = wcslen(str);
959 if(flags & NSSTR_IMPLICIT_PX) {
960 const WCHAR *iter;
961 if(len > 2 && !wcscmp(str + len - 2, L"px"))
962 len -= 2;
963 for(iter = str; iter < str + len && is_digit(*iter); iter++);
964 if(*iter == '.') {
965 const WCHAR *dot = iter++;
966 while(iter < str + len && is_digit(*iter)) iter++;
967 if(iter == str + len && dot) len = dot - str;
970 if(flags & NSSTR_COLOR) {
971 hres = nscolor_to_str(str, &V_BSTR(p));
972 }else if(*str) {
973 V_BSTR(p) = SysAllocStringLen(str, len);
974 if(!V_BSTR(p))
975 hres = E_OUTOFMEMORY;
976 }else {
977 V_BSTR(p) = NULL;
979 if(SUCCEEDED(hres))
980 V_VT(p) = VT_BSTR;
983 nsAString_Finish(nsstr);
984 if(FAILED(hres))
985 return hres;
986 TRACE("ret %s\n", debugstr_variant(p));
987 return S_OK;
991 * Converts VARIANT to string and stores the result in nsAString. To avoid useless
992 * allocations, the function uses an existing string if available, so caller must
993 * ensure that passed VARIANT is unchanged as long as its string representation is used
995 HRESULT variant_to_nsstr(VARIANT *v, BOOL hex_int, nsAString *nsstr)
997 WCHAR buf[32];
999 switch(V_VT(v)) {
1000 case VT_NULL:
1001 nsAString_InitDepend(nsstr, NULL);
1002 return S_OK;
1004 case VT_BSTR:
1005 nsAString_InitDepend(nsstr, V_BSTR(v));
1006 break;
1008 case VT_BSTR|VT_BYREF:
1009 nsAString_InitDepend(nsstr, *V_BSTRREF(v));
1010 break;
1012 case VT_I4:
1013 wsprintfW(buf, hex_int ? L"#%06x" : L"%d", V_I4(v));
1014 nsAString_Init(nsstr, buf);
1015 break;
1017 case VT_R8: {
1018 VARIANT strv;
1019 HRESULT hres;
1021 V_VT(&strv) = VT_EMPTY;
1022 hres = VariantChangeTypeEx(&strv, v, MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT),
1023 0, VT_BSTR);
1024 if(FAILED(hres))
1025 return hres;
1027 nsAString_Init(nsstr, V_BSTR(&strv));
1028 SysFreeString(V_BSTR(&strv));
1029 break;
1032 default:
1033 FIXME("not implemented for %s\n", debugstr_variant(v));
1034 return E_NOTIMPL;
1037 return S_OK;
1040 nsICommandParams *create_nscommand_params(void)
1042 nsICommandParams *ret = NULL;
1043 nsresult nsres;
1045 if(!pCompMgr)
1046 return NULL;
1048 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
1049 NS_COMMANDPARAMS_CONTRACTID, NULL, &IID_nsICommandParams,
1050 (void**)&ret);
1051 if(NS_FAILED(nsres))
1052 ERR("Could not get nsICommandParams\n");
1054 return ret;
1057 nsIWritableVariant *create_nsvariant(void)
1059 nsIWritableVariant *ret = NULL;
1060 nsresult nsres;
1062 if(!pCompMgr)
1063 return NULL;
1065 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
1066 NS_VARIANT_CONTRACTID, NULL, &IID_nsIWritableVariant, (void**)&ret);
1067 if(NS_FAILED(nsres))
1068 ERR("Could not get nsIVariant\n");
1070 return ret;
1073 char *get_nscategory_entry(const char *category, const char *entry)
1075 char *ret = NULL;
1076 nsresult nsres;
1078 nsres = nsICategoryManager_GetCategoryEntry(cat_mgr, category, entry, &ret);
1079 return NS_SUCCEEDED(nsres) ? ret : NULL;
1082 nsresult get_nsinterface(nsISupports *iface, REFIID riid, void **ppv)
1084 nsIInterfaceRequestor *iface_req;
1085 nsresult nsres;
1087 nsres = nsISupports_QueryInterface(iface, &IID_nsIInterfaceRequestor, (void**)&iface_req);
1088 if(NS_FAILED(nsres))
1089 return nsres;
1091 nsres = nsIInterfaceRequestor_GetInterface(iface_req, riid, ppv);
1092 nsIInterfaceRequestor_Release(iface_req);
1094 return nsres;
1097 static HRESULT nsnode_to_nsstring_rec(nsIContentSerializer *serializer, nsIDOMNode *nsnode, nsAString *str)
1099 nsIDOMNodeList *node_list = NULL;
1100 cpp_bool has_children = FALSE;
1101 nsIContent *nscontent;
1102 UINT16 type;
1103 nsresult nsres;
1105 nsIDOMNode_HasChildNodes(nsnode, &has_children);
1107 nsres = nsIDOMNode_GetNodeType(nsnode, &type);
1108 if(NS_FAILED(nsres)) {
1109 ERR("GetType failed: %08x\n", nsres);
1110 return E_FAIL;
1113 if(type != DOCUMENT_NODE) {
1114 nsres = nsIDOMNode_QueryInterface(nsnode, &IID_nsIContent, (void**)&nscontent);
1115 if(NS_FAILED(nsres)) {
1116 ERR("Could not get nsIContent interface: %08x\n", nsres);
1117 return E_FAIL;
1121 switch(type) {
1122 case ELEMENT_NODE:
1123 nsIContentSerializer_AppendElementStart(serializer, nscontent, nscontent, str);
1124 break;
1125 case TEXT_NODE:
1126 nsIContentSerializer_AppendText(serializer, nscontent, 0, -1, str);
1127 break;
1128 case COMMENT_NODE:
1129 nsIContentSerializer_AppendComment(serializer, nscontent, 0, -1, str);
1130 break;
1131 case DOCUMENT_NODE: {
1132 nsIDocument *nsdoc;
1133 nsIDOMNode_QueryInterface(nsnode, &IID_nsIDocument, (void**)&nsdoc);
1134 nsIContentSerializer_AppendDocumentStart(serializer, nsdoc, str);
1135 nsIDocument_Release(nsdoc);
1136 break;
1138 case DOCUMENT_TYPE_NODE:
1139 nsIContentSerializer_AppendDoctype(serializer, nscontent, str);
1140 break;
1141 case DOCUMENT_FRAGMENT_NODE:
1142 break;
1143 default:
1144 FIXME("Unhandled type %u\n", type);
1147 if(has_children) {
1148 UINT32 child_cnt, i;
1149 nsIDOMNode *child_node;
1151 nsIDOMNode_GetChildNodes(nsnode, &node_list);
1152 nsIDOMNodeList_GetLength(node_list, &child_cnt);
1154 for(i=0; i<child_cnt; i++) {
1155 nsres = nsIDOMNodeList_Item(node_list, i, &child_node);
1156 if(NS_SUCCEEDED(nsres)) {
1157 nsnode_to_nsstring_rec(serializer, child_node, str);
1158 nsIDOMNode_Release(child_node);
1159 }else {
1160 ERR("Item failed: %08x\n", nsres);
1164 nsIDOMNodeList_Release(node_list);
1167 if(type == ELEMENT_NODE)
1168 nsIContentSerializer_AppendElementEnd(serializer, nscontent, str);
1170 if(type != DOCUMENT_NODE)
1171 nsIContent_Release(nscontent);
1172 return S_OK;
1175 HRESULT nsnode_to_nsstring(nsIDOMNode *nsnode, nsAString *str)
1177 nsIContentSerializer *serializer;
1178 nsresult nsres;
1179 HRESULT hres;
1181 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
1182 NS_HTMLSERIALIZER_CONTRACTID, NULL, &IID_nsIContentSerializer,
1183 (void**)&serializer);
1184 if(NS_FAILED(nsres)) {
1185 ERR("Could not get nsIContentSerializer: %08x\n", nsres);
1186 return E_FAIL;
1189 nsres = nsIContentSerializer_Init(serializer, 0, 100, NULL, FALSE, FALSE /* FIXME */);
1190 if(NS_FAILED(nsres))
1191 ERR("Init failed: %08x\n", nsres);
1193 hres = nsnode_to_nsstring_rec(serializer, nsnode, str);
1194 if(SUCCEEDED(hres)) {
1195 nsres = nsIContentSerializer_Flush(serializer, str);
1196 if(NS_FAILED(nsres))
1197 ERR("Flush failed: %08x\n", nsres);
1200 nsIContentSerializer_Release(serializer);
1201 return hres;
1204 void setup_editor_controller(GeckoBrowser *This)
1206 nsIEditingSession *editing_session = NULL;
1207 nsIControllerContext *ctrlctx;
1208 nsresult nsres;
1210 if(This->editor) {
1211 nsIEditor_Release(This->editor);
1212 This->editor = NULL;
1215 if(This->editor_controller) {
1216 nsIController_Release(This->editor_controller);
1217 This->editor_controller = NULL;
1220 nsres = get_nsinterface((nsISupports*)This->webbrowser, &IID_nsIEditingSession,
1221 (void**)&editing_session);
1222 if(NS_FAILED(nsres)) {
1223 ERR("Could not get nsIEditingSession: %08x\n", nsres);
1224 return;
1227 nsres = nsIEditingSession_GetEditorForWindow(editing_session,
1228 This->doc->basedoc.window->window_proxy, &This->editor);
1229 nsIEditingSession_Release(editing_session);
1230 if(NS_FAILED(nsres)) {
1231 ERR("Could not get editor: %08x\n", nsres);
1232 return;
1235 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
1236 NS_EDITORCONTROLLER_CONTRACTID, NULL, &IID_nsIControllerContext, (void**)&ctrlctx);
1237 if(NS_SUCCEEDED(nsres)) {
1238 nsres = nsIControllerContext_SetCommandContext(ctrlctx, (nsISupports *)This->editor);
1239 if(NS_FAILED(nsres))
1240 ERR("SetCommandContext failed: %08x\n", nsres);
1241 nsres = nsIControllerContext_QueryInterface(ctrlctx, &IID_nsIController,
1242 (void**)&This->editor_controller);
1243 nsIControllerContext_Release(ctrlctx);
1244 if(NS_FAILED(nsres))
1245 ERR("Could not get nsIController interface: %08x\n", nsres);
1246 }else {
1247 ERR("Could not create edit controller: %08x\n", nsres);
1251 void close_gecko(void)
1253 TRACE("()\n");
1255 release_nsio();
1256 init_mutation(NULL);
1258 if(profile_directory) {
1259 nsIFile_Release(profile_directory);
1260 profile_directory = NULL;
1263 if(plugin_directory) {
1264 nsIFile_Release(plugin_directory);
1265 plugin_directory = NULL;
1268 if(pCompMgr)
1269 nsIComponentManager_Release(pCompMgr);
1271 if(pServMgr)
1272 nsIServiceManager_Release(pServMgr);
1274 if(cat_mgr)
1275 nsICategoryManager_Release(cat_mgr);
1277 /* Gecko doesn't really support being unloaded */
1278 /* if (hXPCOM) FreeLibrary(hXPCOM); */
1280 DeleteCriticalSection(&cs_load_gecko);
1283 BOOL is_gecko_path(const char *path)
1285 WCHAR *buf, *ptr;
1286 BOOL ret;
1288 buf = heap_strdupUtoW(path);
1289 if(!buf || lstrlenW(buf) < gecko_path_len)
1290 return FALSE;
1292 for(ptr = buf; *ptr; ptr++) {
1293 if(*ptr == '\\')
1294 *ptr = '/';
1297 UrlUnescapeW(buf, NULL, NULL, URL_UNESCAPE_INPLACE);
1298 buf[gecko_path_len] = 0;
1300 ret = !wcsicmp(buf, gecko_path);
1301 heap_free(buf);
1302 return ret;
1305 void set_viewer_zoom(GeckoBrowser *browser, float factor)
1307 nsIContentViewer *content_viewer;
1308 nsIDocShell *doc_shell;
1309 nsresult nsres;
1311 TRACE("Setting to %f\n", factor);
1313 nsres = get_nsinterface((nsISupports*)browser->navigation, &IID_nsIDocShell, (void**)&doc_shell);
1314 assert(nsres == NS_OK);
1316 nsres = nsIDocShell_GetContentViewer(doc_shell, &content_viewer);
1317 assert(nsres == NS_OK && content_viewer);
1318 nsIDocShell_Release(doc_shell);
1320 nsres = nsIContentViewer_SetFullZoom(content_viewer, factor);
1321 if(NS_FAILED(nsres))
1322 ERR("SetFullZoom failed: %08x\n", nsres);
1324 nsIContentViewer_Release(content_viewer);
1327 float get_viewer_zoom(GeckoBrowser *browser)
1329 nsIContentViewer *content_viewer;
1330 nsIDocShell *doc_shell;
1331 nsresult nsres;
1332 float factor;
1334 nsres = get_nsinterface((nsISupports*)browser->navigation, &IID_nsIDocShell, (void**)&doc_shell);
1335 assert(nsres == NS_OK);
1337 nsres = nsIDocShell_GetContentViewer(doc_shell, &content_viewer);
1338 assert(nsres == NS_OK && content_viewer);
1339 nsIDocShell_Release(doc_shell);
1341 nsres = nsIContentViewer_GetFullZoom(content_viewer, &factor);
1342 if(NS_FAILED(nsres))
1343 ERR("GetFullZoom failed: %08x\n", nsres);
1344 TRACE("Got %f\n", factor);
1346 nsIContentViewer_Release(content_viewer);
1347 return factor;
1350 struct nsWeakReference {
1351 nsIWeakReference nsIWeakReference_iface;
1353 LONG ref;
1355 GeckoBrowser *browser;
1358 static inline nsWeakReference *impl_from_nsIWeakReference(nsIWeakReference *iface)
1360 return CONTAINING_RECORD(iface, nsWeakReference, nsIWeakReference_iface);
1363 static nsresult NSAPI nsWeakReference_QueryInterface(nsIWeakReference *iface,
1364 nsIIDRef riid, void **result)
1366 nsWeakReference *This = impl_from_nsIWeakReference(iface);
1368 if(IsEqualGUID(&IID_nsISupports, riid)) {
1369 TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
1370 *result = &This->nsIWeakReference_iface;
1371 }else if(IsEqualGUID(&IID_nsIWeakReference, riid)) {
1372 TRACE("(%p)->(IID_nsIWeakReference %p)\n", This, result);
1373 *result = &This->nsIWeakReference_iface;
1374 }else {
1375 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
1376 *result = NULL;
1377 return NS_NOINTERFACE;
1380 nsISupports_AddRef((nsISupports*)*result);
1381 return NS_OK;
1384 static nsrefcnt NSAPI nsWeakReference_AddRef(nsIWeakReference *iface)
1386 nsWeakReference *This = impl_from_nsIWeakReference(iface);
1387 LONG ref = InterlockedIncrement(&This->ref);
1389 TRACE("(%p) ref=%d\n", This, ref);
1391 return ref;
1394 static nsrefcnt NSAPI nsWeakReference_Release(nsIWeakReference *iface)
1396 nsWeakReference *This = impl_from_nsIWeakReference(iface);
1397 LONG ref = InterlockedDecrement(&This->ref);
1399 TRACE("(%p) ref=%d\n", This, ref);
1401 if(!ref) {
1402 assert(!This->browser);
1403 heap_free(This);
1406 return ref;
1409 static nsresult NSAPI nsWeakReference_QueryReferent(nsIWeakReference *iface,
1410 const nsIID *riid, void **result)
1412 nsWeakReference *This = impl_from_nsIWeakReference(iface);
1414 if(!This->browser)
1415 return NS_ERROR_NULL_POINTER;
1417 return nsIWebBrowserChrome_QueryInterface(&This->browser->nsIWebBrowserChrome_iface, riid, result);
1420 static const nsIWeakReferenceVtbl nsWeakReferenceVtbl = {
1421 nsWeakReference_QueryInterface,
1422 nsWeakReference_AddRef,
1423 nsWeakReference_Release,
1424 nsWeakReference_QueryReferent
1427 /**********************************************************
1428 * nsIWebBrowserChrome interface
1431 static inline GeckoBrowser *impl_from_nsIWebBrowserChrome(nsIWebBrowserChrome *iface)
1433 return CONTAINING_RECORD(iface, GeckoBrowser, nsIWebBrowserChrome_iface);
1436 static nsresult NSAPI nsWebBrowserChrome_QueryInterface(nsIWebBrowserChrome *iface,
1437 nsIIDRef riid, void **result)
1439 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1441 *result = NULL;
1442 if(IsEqualGUID(&IID_nsISupports, riid)) {
1443 TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
1444 *result = &This->nsIWebBrowserChrome_iface;
1445 }else if(IsEqualGUID(&IID_nsIWebBrowserChrome, riid)) {
1446 TRACE("(%p)->(IID_nsIWebBrowserChrome, %p)\n", This, result);
1447 *result = &This->nsIWebBrowserChrome_iface;
1448 }else if(IsEqualGUID(&IID_nsIContextMenuListener, riid)) {
1449 TRACE("(%p)->(IID_nsIContextMenuListener, %p)\n", This, result);
1450 *result = &This->nsIContextMenuListener_iface;
1451 }else if(IsEqualGUID(&IID_nsIURIContentListener, riid)) {
1452 TRACE("(%p)->(IID_nsIURIContentListener %p)\n", This, result);
1453 *result = &This->nsIURIContentListener_iface;
1454 }else if(IsEqualGUID(&IID_nsIEmbeddingSiteWindow, riid)) {
1455 TRACE("(%p)->(IID_nsIEmbeddingSiteWindow %p)\n", This, result);
1456 *result = &This->nsIEmbeddingSiteWindow_iface;
1457 }else if(IsEqualGUID(&IID_nsITooltipListener, riid)) {
1458 TRACE("(%p)->(IID_nsITooltipListener %p)\n", This, result);
1459 *result = &This->nsITooltipListener_iface;
1460 }else if(IsEqualGUID(&IID_nsIInterfaceRequestor, riid)) {
1461 TRACE("(%p)->(IID_nsIInterfaceRequestor %p)\n", This, result);
1462 *result = &This->nsIInterfaceRequestor_iface;
1463 }else if(IsEqualGUID(&IID_nsISupportsWeakReference, riid)) {
1464 TRACE("(%p)->(IID_nsISupportsWeakReference %p)\n", This, result);
1465 *result = &This->nsISupportsWeakReference_iface;
1468 if(*result) {
1469 nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1470 return NS_OK;
1473 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
1474 return NS_NOINTERFACE;
1477 static nsrefcnt NSAPI nsWebBrowserChrome_AddRef(nsIWebBrowserChrome *iface)
1479 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1480 LONG ref = InterlockedIncrement(&This->ref);
1482 TRACE("(%p) ref=%d\n", This, ref);
1484 return ref;
1487 static nsrefcnt NSAPI nsWebBrowserChrome_Release(nsIWebBrowserChrome *iface)
1489 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1490 LONG ref = InterlockedDecrement(&This->ref);
1492 TRACE("(%p) ref=%d\n", This, ref);
1494 if(!ref) {
1495 if(This->doc)
1496 detach_gecko_browser(This);
1497 if(This->weak_reference) {
1498 This->weak_reference->browser = NULL;
1499 nsIWeakReference_Release(&This->weak_reference->nsIWeakReference_iface);
1501 heap_free(This);
1504 return ref;
1507 static nsresult NSAPI nsWebBrowserChrome_SetStatus(nsIWebBrowserChrome *iface,
1508 UINT32 statusType, const PRUnichar *status)
1510 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1511 TRACE("(%p)->(%d %s)\n", This, statusType, debugstr_w(status));
1512 return NS_OK;
1515 static nsresult NSAPI nsWebBrowserChrome_GetWebBrowser(nsIWebBrowserChrome *iface,
1516 nsIWebBrowser **aWebBrowser)
1518 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1520 TRACE("(%p)->(%p)\n", This, aWebBrowser);
1522 if(!aWebBrowser)
1523 return NS_ERROR_INVALID_ARG;
1525 if(This->webbrowser)
1526 nsIWebBrowser_AddRef(This->webbrowser);
1527 *aWebBrowser = This->webbrowser;
1528 return S_OK;
1531 static nsresult NSAPI nsWebBrowserChrome_SetWebBrowser(nsIWebBrowserChrome *iface,
1532 nsIWebBrowser *aWebBrowser)
1534 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1536 TRACE("(%p)->(%p)\n", This, aWebBrowser);
1538 if(aWebBrowser != This->webbrowser)
1539 ERR("Wrong nsWebBrowser!\n");
1541 return NS_OK;
1544 static nsresult NSAPI nsWebBrowserChrome_GetChromeFlags(nsIWebBrowserChrome *iface,
1545 UINT32 *aChromeFlags)
1547 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1548 WARN("(%p)->(%p)\n", This, aChromeFlags);
1549 return NS_ERROR_NOT_IMPLEMENTED;
1552 static nsresult NSAPI nsWebBrowserChrome_SetChromeFlags(nsIWebBrowserChrome *iface,
1553 UINT32 aChromeFlags)
1555 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1556 WARN("(%p)->(%08x)\n", This, aChromeFlags);
1557 return NS_ERROR_NOT_IMPLEMENTED;
1560 static nsresult NSAPI nsWebBrowserChrome_DestroyBrowserWindow(nsIWebBrowserChrome *iface)
1562 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1563 TRACE("(%p)\n", This);
1564 return NS_ERROR_NOT_IMPLEMENTED;
1567 static nsresult NSAPI nsWebBrowserChrome_SizeBrowserTo(nsIWebBrowserChrome *iface,
1568 LONG aCX, LONG aCY)
1570 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1571 WARN("(%p)->(%d %d)\n", This, aCX, aCY);
1572 return NS_ERROR_NOT_IMPLEMENTED;
1575 static nsresult NSAPI nsWebBrowserChrome_ShowAsModal(nsIWebBrowserChrome *iface)
1577 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1578 WARN("(%p)\n", This);
1579 return NS_ERROR_NOT_IMPLEMENTED;
1582 static nsresult NSAPI nsWebBrowserChrome_IsWindowModal(nsIWebBrowserChrome *iface, cpp_bool *_retval)
1584 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1585 WARN("(%p)->(%p)\n", This, _retval);
1586 return NS_ERROR_NOT_IMPLEMENTED;
1589 static nsresult NSAPI nsWebBrowserChrome_ExitModalEventLoop(nsIWebBrowserChrome *iface,
1590 nsresult aStatus)
1592 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1593 WARN("(%p)->(%08x)\n", This, aStatus);
1594 return NS_ERROR_NOT_IMPLEMENTED;
1597 static const nsIWebBrowserChromeVtbl nsWebBrowserChromeVtbl = {
1598 nsWebBrowserChrome_QueryInterface,
1599 nsWebBrowserChrome_AddRef,
1600 nsWebBrowserChrome_Release,
1601 nsWebBrowserChrome_SetStatus,
1602 nsWebBrowserChrome_GetWebBrowser,
1603 nsWebBrowserChrome_SetWebBrowser,
1604 nsWebBrowserChrome_GetChromeFlags,
1605 nsWebBrowserChrome_SetChromeFlags,
1606 nsWebBrowserChrome_DestroyBrowserWindow,
1607 nsWebBrowserChrome_SizeBrowserTo,
1608 nsWebBrowserChrome_ShowAsModal,
1609 nsWebBrowserChrome_IsWindowModal,
1610 nsWebBrowserChrome_ExitModalEventLoop
1613 /**********************************************************
1614 * nsIContextMenuListener interface
1617 static inline GeckoBrowser *impl_from_nsIContextMenuListener(nsIContextMenuListener *iface)
1619 return CONTAINING_RECORD(iface, GeckoBrowser, nsIContextMenuListener_iface);
1622 static nsresult NSAPI nsContextMenuListener_QueryInterface(nsIContextMenuListener *iface,
1623 nsIIDRef riid, void **result)
1625 GeckoBrowser *This = impl_from_nsIContextMenuListener(iface);
1626 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1629 static nsrefcnt NSAPI nsContextMenuListener_AddRef(nsIContextMenuListener *iface)
1631 GeckoBrowser *This = impl_from_nsIContextMenuListener(iface);
1632 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1635 static nsrefcnt NSAPI nsContextMenuListener_Release(nsIContextMenuListener *iface)
1637 GeckoBrowser *This = impl_from_nsIContextMenuListener(iface);
1638 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
1641 static nsresult NSAPI nsContextMenuListener_OnShowContextMenu(nsIContextMenuListener *iface,
1642 UINT32 aContextFlags, nsIDOMEvent *aEvent, nsIDOMNode *aNode)
1644 GeckoBrowser *This = impl_from_nsIContextMenuListener(iface);
1645 nsIDOMMouseEvent *mouse_event;
1646 HTMLDOMNode *node;
1647 DOMEvent *event;
1648 POINT pt;
1649 DWORD dwID = CONTEXT_MENU_DEFAULT;
1650 nsresult nsres;
1651 HRESULT hres;
1653 TRACE("(%p)->(%08x %p %p)\n", This, aContextFlags, aEvent, aNode);
1655 if (!aNode)
1656 return NS_ERROR_FAILURE;
1658 hres = get_node(aNode, TRUE, &node);
1659 if(FAILED(hres))
1660 return NS_ERROR_FAILURE;
1662 hres = create_event_from_nsevent(aEvent, &event);
1663 if(SUCCEEDED(hres)) {
1664 dispatch_event(&node->event_target, event);
1665 IDOMEvent_Release(&event->IDOMEvent_iface);
1668 nsres = nsIDOMEvent_QueryInterface(aEvent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
1669 assert(NS_SUCCEEDED(nsres));
1671 nsIDOMMouseEvent_GetScreenX(mouse_event, &pt.x);
1672 nsIDOMMouseEvent_GetScreenY(mouse_event, &pt.y);
1673 nsIDOMMouseEvent_Release(mouse_event);
1675 switch(aContextFlags) {
1676 case CONTEXT_NONE:
1677 case CONTEXT_DOCUMENT:
1678 case CONTEXT_TEXT: {
1679 nsISelection *selection;
1681 nsres = nsIDOMHTMLDocument_GetSelection(This->doc->basedoc.doc_node->nsdoc, &selection);
1682 if(NS_SUCCEEDED(nsres) && selection) {
1683 cpp_bool is_collapsed;
1685 /* FIXME: Check if the click was inside selection. */
1686 nsres = nsISelection_GetIsCollapsed(selection, &is_collapsed);
1687 nsISelection_Release(selection);
1688 if(NS_SUCCEEDED(nsres) && !is_collapsed)
1689 dwID = CONTEXT_MENU_TEXTSELECT;
1691 break;
1693 case CONTEXT_IMAGE:
1694 case CONTEXT_IMAGE|CONTEXT_LINK:
1695 dwID = CONTEXT_MENU_IMAGE;
1696 break;
1697 case CONTEXT_LINK:
1698 dwID = CONTEXT_MENU_ANCHOR;
1699 break;
1700 case CONTEXT_INPUT:
1701 dwID = CONTEXT_MENU_CONTROL;
1702 break;
1703 default:
1704 FIXME("aContextFlags=%08x\n", aContextFlags);
1707 show_context_menu(This->doc, dwID, &pt, (IDispatch*)&node->IHTMLDOMNode_iface);
1708 node_release(node);
1709 return NS_OK;
1712 static const nsIContextMenuListenerVtbl nsContextMenuListenerVtbl = {
1713 nsContextMenuListener_QueryInterface,
1714 nsContextMenuListener_AddRef,
1715 nsContextMenuListener_Release,
1716 nsContextMenuListener_OnShowContextMenu
1719 /**********************************************************
1720 * nsIURIContentListener interface
1723 static inline GeckoBrowser *impl_from_nsIURIContentListener(nsIURIContentListener *iface)
1725 return CONTAINING_RECORD(iface, GeckoBrowser, nsIURIContentListener_iface);
1728 static nsresult NSAPI nsURIContentListener_QueryInterface(nsIURIContentListener *iface,
1729 nsIIDRef riid, void **result)
1731 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1732 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1735 static nsrefcnt NSAPI nsURIContentListener_AddRef(nsIURIContentListener *iface)
1737 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1738 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1741 static nsrefcnt NSAPI nsURIContentListener_Release(nsIURIContentListener *iface)
1743 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1744 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
1747 static nsresult NSAPI nsURIContentListener_OnStartURIOpen(nsIURIContentListener *iface,
1748 nsIURI *aURI, cpp_bool *_retval)
1750 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1751 nsACString spec_str;
1752 const char *spec;
1754 nsACString_Init(&spec_str, NULL);
1755 nsIURI_GetSpec(aURI, &spec_str);
1756 nsACString_GetData(&spec_str, &spec);
1758 TRACE("(%p)->(%p(%s) %p)\n", This, aURI, debugstr_a(spec), _retval);
1760 nsACString_Finish(&spec_str);
1762 return This->content_listener
1763 ? nsIURIContentListener_OnStartURIOpen(This->content_listener, aURI, _retval)
1764 : NS_OK;
1767 static nsresult NSAPI nsURIContentListener_DoContent(nsIURIContentListener *iface,
1768 const nsACString *aContentType, cpp_bool aIsContentPreferred, nsIRequest *aRequest,
1769 nsIStreamListener **aContentHandler, cpp_bool *_retval)
1771 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1773 TRACE("(%p)->(%p %x %p %p %p)\n", This, aContentType, aIsContentPreferred,
1774 aRequest, aContentHandler, _retval);
1776 return This->content_listener
1777 ? nsIURIContentListener_DoContent(This->content_listener, aContentType,
1778 aIsContentPreferred, aRequest, aContentHandler, _retval)
1779 : NS_ERROR_NOT_IMPLEMENTED;
1782 static nsresult NSAPI nsURIContentListener_IsPreferred(nsIURIContentListener *iface,
1783 const char *aContentType, char **aDesiredContentType, cpp_bool *_retval)
1785 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1787 TRACE("(%p)->(%s %p %p)\n", This, debugstr_a(aContentType), aDesiredContentType, _retval);
1789 /* FIXME: Should we do something here? */
1790 *_retval = TRUE;
1792 return This->content_listener
1793 ? nsIURIContentListener_IsPreferred(This->content_listener, aContentType,
1794 aDesiredContentType, _retval)
1795 : NS_OK;
1798 static nsresult NSAPI nsURIContentListener_CanHandleContent(nsIURIContentListener *iface,
1799 const char *aContentType, cpp_bool aIsContentPreferred, char **aDesiredContentType,
1800 cpp_bool *_retval)
1802 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1804 TRACE("(%p)->(%s %x %p %p)\n", This, debugstr_a(aContentType), aIsContentPreferred,
1805 aDesiredContentType, _retval);
1807 return This->content_listener
1808 ? nsIURIContentListener_CanHandleContent(This->content_listener, aContentType,
1809 aIsContentPreferred, aDesiredContentType, _retval)
1810 : NS_ERROR_NOT_IMPLEMENTED;
1813 static nsresult NSAPI nsURIContentListener_GetLoadCookie(nsIURIContentListener *iface,
1814 nsISupports **aLoadCookie)
1816 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1818 WARN("(%p)->(%p)\n", This, aLoadCookie);
1820 return This->content_listener
1821 ? nsIURIContentListener_GetLoadCookie(This->content_listener, aLoadCookie)
1822 : NS_ERROR_NOT_IMPLEMENTED;
1825 static nsresult NSAPI nsURIContentListener_SetLoadCookie(nsIURIContentListener *iface,
1826 nsISupports *aLoadCookie)
1828 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1830 WARN("(%p)->(%p)\n", This, aLoadCookie);
1832 return This->content_listener
1833 ? nsIURIContentListener_SetLoadCookie(This->content_listener, aLoadCookie)
1834 : NS_ERROR_NOT_IMPLEMENTED;
1837 static nsresult NSAPI nsURIContentListener_GetParentContentListener(nsIURIContentListener *iface,
1838 nsIURIContentListener **aParentContentListener)
1840 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1842 TRACE("(%p)->(%p)\n", This, aParentContentListener);
1844 if(This->content_listener)
1845 nsIURIContentListener_AddRef(This->content_listener);
1847 *aParentContentListener = This->content_listener;
1848 return NS_OK;
1851 static nsresult NSAPI nsURIContentListener_SetParentContentListener(nsIURIContentListener *iface,
1852 nsIURIContentListener *aParentContentListener)
1854 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1856 TRACE("(%p)->(%p)\n", This, aParentContentListener);
1858 if(aParentContentListener == &This->nsIURIContentListener_iface)
1859 return NS_OK;
1861 if(This->content_listener)
1862 nsIURIContentListener_Release(This->content_listener);
1864 This->content_listener = aParentContentListener;
1865 if(This->content_listener)
1866 nsIURIContentListener_AddRef(This->content_listener);
1868 return NS_OK;
1871 static const nsIURIContentListenerVtbl nsURIContentListenerVtbl = {
1872 nsURIContentListener_QueryInterface,
1873 nsURIContentListener_AddRef,
1874 nsURIContentListener_Release,
1875 nsURIContentListener_OnStartURIOpen,
1876 nsURIContentListener_DoContent,
1877 nsURIContentListener_IsPreferred,
1878 nsURIContentListener_CanHandleContent,
1879 nsURIContentListener_GetLoadCookie,
1880 nsURIContentListener_SetLoadCookie,
1881 nsURIContentListener_GetParentContentListener,
1882 nsURIContentListener_SetParentContentListener
1885 /**********************************************************
1886 * nsIEmbeddinSiteWindow interface
1889 static inline GeckoBrowser *impl_from_nsIEmbeddingSiteWindow(nsIEmbeddingSiteWindow *iface)
1891 return CONTAINING_RECORD(iface, GeckoBrowser, nsIEmbeddingSiteWindow_iface);
1894 static nsresult NSAPI nsEmbeddingSiteWindow_QueryInterface(nsIEmbeddingSiteWindow *iface,
1895 nsIIDRef riid, void **result)
1897 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1898 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1901 static nsrefcnt NSAPI nsEmbeddingSiteWindow_AddRef(nsIEmbeddingSiteWindow *iface)
1903 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1904 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1907 static nsrefcnt NSAPI nsEmbeddingSiteWindow_Release(nsIEmbeddingSiteWindow *iface)
1909 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1910 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
1913 static nsresult NSAPI nsEmbeddingSiteWindow_SetDimensions(nsIEmbeddingSiteWindow *iface,
1914 UINT32 flags, LONG x, LONG y, LONG cx, LONG cy)
1916 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1917 WARN("(%p)->(%08x %d %d %d %d)\n", This, flags, x, y, cx, cy);
1918 return NS_ERROR_NOT_IMPLEMENTED;
1921 static nsresult NSAPI nsEmbeddingSiteWindow_GetDimensions(nsIEmbeddingSiteWindow *iface,
1922 UINT32 flags, LONG *x, LONG *y, LONG *cx, LONG *cy)
1924 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1925 RECT r;
1927 TRACE("(%p)->(%x %p %p %p %p)\n", This, flags, x, y, cx, cy);
1929 if(!GetWindowRect(This->hwnd, &r)) {
1930 ERR("GetWindowRect failed\n");
1931 return NS_ERROR_FAILURE;
1934 if(x)
1935 *x = r.left;
1936 if(y)
1937 *y = r.top;
1938 if(cx)
1939 *cx = r.right-r.left;
1940 if(cy)
1941 *cy = r.bottom-r.top;
1942 return NS_OK;
1945 static nsresult NSAPI nsEmbeddingSiteWindow_SetFocus(nsIEmbeddingSiteWindow *iface)
1947 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1949 TRACE("(%p)\n", This);
1951 return nsIBaseWindow_SetFocus(This->window);
1954 static nsresult NSAPI nsEmbeddingSiteWindow_GetVisibility(nsIEmbeddingSiteWindow *iface,
1955 cpp_bool *aVisibility)
1957 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1959 TRACE("(%p)->(%p)\n", This, aVisibility);
1961 *aVisibility = This->doc && This->doc->hwnd && IsWindowVisible(This->doc->hwnd);
1962 return NS_OK;
1965 static nsresult NSAPI nsEmbeddingSiteWindow_SetVisibility(nsIEmbeddingSiteWindow *iface,
1966 cpp_bool aVisibility)
1968 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1970 TRACE("(%p)->(%x)\n", This, aVisibility);
1972 return NS_OK;
1975 static nsresult NSAPI nsEmbeddingSiteWindow_GetTitle(nsIEmbeddingSiteWindow *iface,
1976 PRUnichar **aTitle)
1978 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1979 WARN("(%p)->(%p)\n", This, aTitle);
1980 return NS_ERROR_NOT_IMPLEMENTED;
1983 static nsresult NSAPI nsEmbeddingSiteWindow_SetTitle(nsIEmbeddingSiteWindow *iface,
1984 const PRUnichar *aTitle)
1986 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1987 WARN("(%p)->(%s)\n", This, debugstr_w(aTitle));
1988 return NS_ERROR_NOT_IMPLEMENTED;
1991 static nsresult NSAPI nsEmbeddingSiteWindow_GetSiteWindow(nsIEmbeddingSiteWindow *iface,
1992 void **aSiteWindow)
1994 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1996 TRACE("(%p)->(%p)\n", This, aSiteWindow);
1998 *aSiteWindow = This->hwnd;
1999 return NS_OK;
2002 static const nsIEmbeddingSiteWindowVtbl nsEmbeddingSiteWindowVtbl = {
2003 nsEmbeddingSiteWindow_QueryInterface,
2004 nsEmbeddingSiteWindow_AddRef,
2005 nsEmbeddingSiteWindow_Release,
2006 nsEmbeddingSiteWindow_SetDimensions,
2007 nsEmbeddingSiteWindow_GetDimensions,
2008 nsEmbeddingSiteWindow_SetFocus,
2009 nsEmbeddingSiteWindow_GetVisibility,
2010 nsEmbeddingSiteWindow_SetVisibility,
2011 nsEmbeddingSiteWindow_GetTitle,
2012 nsEmbeddingSiteWindow_SetTitle,
2013 nsEmbeddingSiteWindow_GetSiteWindow
2016 static inline GeckoBrowser *impl_from_nsITooltipListener(nsITooltipListener *iface)
2018 return CONTAINING_RECORD(iface, GeckoBrowser, nsITooltipListener_iface);
2021 static nsresult NSAPI nsTooltipListener_QueryInterface(nsITooltipListener *iface, nsIIDRef riid,
2022 void **result)
2024 GeckoBrowser *This = impl_from_nsITooltipListener(iface);
2025 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
2028 static nsrefcnt NSAPI nsTooltipListener_AddRef(nsITooltipListener *iface)
2030 GeckoBrowser *This = impl_from_nsITooltipListener(iface);
2031 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
2034 static nsrefcnt NSAPI nsTooltipListener_Release(nsITooltipListener *iface)
2036 GeckoBrowser *This = impl_from_nsITooltipListener(iface);
2037 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
2040 static nsresult NSAPI nsTooltipListener_OnShowTooltip(nsITooltipListener *iface,
2041 LONG aXCoord, LONG aYCoord, const PRUnichar *aTipText)
2043 GeckoBrowser *This = impl_from_nsITooltipListener(iface);
2045 if (This->doc)
2046 show_tooltip(This->doc, aXCoord, aYCoord, aTipText);
2048 return NS_OK;
2051 static nsresult NSAPI nsTooltipListener_OnHideTooltip(nsITooltipListener *iface)
2053 GeckoBrowser *This = impl_from_nsITooltipListener(iface);
2055 if (This->doc)
2056 hide_tooltip(This->doc);
2058 return NS_OK;
2061 static const nsITooltipListenerVtbl nsTooltipListenerVtbl = {
2062 nsTooltipListener_QueryInterface,
2063 nsTooltipListener_AddRef,
2064 nsTooltipListener_Release,
2065 nsTooltipListener_OnShowTooltip,
2066 nsTooltipListener_OnHideTooltip
2069 static inline GeckoBrowser *impl_from_nsIInterfaceRequestor(nsIInterfaceRequestor *iface)
2071 return CONTAINING_RECORD(iface, GeckoBrowser, nsIInterfaceRequestor_iface);
2074 static nsresult NSAPI nsInterfaceRequestor_QueryInterface(nsIInterfaceRequestor *iface,
2075 nsIIDRef riid, void **result)
2077 GeckoBrowser *This = impl_from_nsIInterfaceRequestor(iface);
2078 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
2081 static nsrefcnt NSAPI nsInterfaceRequestor_AddRef(nsIInterfaceRequestor *iface)
2083 GeckoBrowser *This = impl_from_nsIInterfaceRequestor(iface);
2084 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
2087 static nsrefcnt NSAPI nsInterfaceRequestor_Release(nsIInterfaceRequestor *iface)
2089 GeckoBrowser *This = impl_from_nsIInterfaceRequestor(iface);
2090 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
2093 static nsresult NSAPI nsInterfaceRequestor_GetInterface(nsIInterfaceRequestor *iface,
2094 nsIIDRef riid, void **result)
2096 GeckoBrowser *This = impl_from_nsIInterfaceRequestor(iface);
2098 if(IsEqualGUID(&IID_mozIDOMWindowProxy, riid)) {
2099 TRACE("(%p)->(IID_nsIDOMWindow %p)\n", This, result);
2100 return nsIWebBrowser_GetContentDOMWindow(This->webbrowser, (mozIDOMWindowProxy**)result);
2103 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
2106 static const nsIInterfaceRequestorVtbl nsInterfaceRequestorVtbl = {
2107 nsInterfaceRequestor_QueryInterface,
2108 nsInterfaceRequestor_AddRef,
2109 nsInterfaceRequestor_Release,
2110 nsInterfaceRequestor_GetInterface
2113 static inline GeckoBrowser *impl_from_nsISupportsWeakReference(nsISupportsWeakReference *iface)
2115 return CONTAINING_RECORD(iface, GeckoBrowser, nsISupportsWeakReference_iface);
2118 static nsresult NSAPI nsSupportsWeakReference_QueryInterface(nsISupportsWeakReference *iface,
2119 nsIIDRef riid, void **result)
2121 GeckoBrowser *This = impl_from_nsISupportsWeakReference(iface);
2122 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
2125 static nsrefcnt NSAPI nsSupportsWeakReference_AddRef(nsISupportsWeakReference *iface)
2127 GeckoBrowser *This = impl_from_nsISupportsWeakReference(iface);
2128 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
2131 static nsrefcnt NSAPI nsSupportsWeakReference_Release(nsISupportsWeakReference *iface)
2133 GeckoBrowser *This = impl_from_nsISupportsWeakReference(iface);
2134 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
2137 static nsresult NSAPI nsSupportsWeakReference_GetWeakReference(nsISupportsWeakReference *iface,
2138 nsIWeakReference **_retval)
2140 GeckoBrowser *This = impl_from_nsISupportsWeakReference(iface);
2142 TRACE("(%p)->(%p)\n", This, _retval);
2144 if(!This->weak_reference) {
2145 This->weak_reference = heap_alloc(sizeof(nsWeakReference));
2146 if(!This->weak_reference)
2147 return NS_ERROR_OUT_OF_MEMORY;
2149 This->weak_reference->nsIWeakReference_iface.lpVtbl = &nsWeakReferenceVtbl;
2150 This->weak_reference->ref = 1;
2151 This->weak_reference->browser = This;
2154 *_retval = &This->weak_reference->nsIWeakReference_iface;
2155 nsIWeakReference_AddRef(*_retval);
2156 return NS_OK;
2159 static const nsISupportsWeakReferenceVtbl nsSupportsWeakReferenceVtbl = {
2160 nsSupportsWeakReference_QueryInterface,
2161 nsSupportsWeakReference_AddRef,
2162 nsSupportsWeakReference_Release,
2163 nsSupportsWeakReference_GetWeakReference
2166 static HRESULT init_browser(GeckoBrowser *browser)
2168 mozIDOMWindowProxy *mozwindow;
2169 nsIWebBrowserSetup *wbsetup;
2170 nsIScrollable *scrollable;
2171 nsresult nsres;
2172 HRESULT hres;
2174 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_WEBBROWSER_CONTRACTID,
2175 NULL, &IID_nsIWebBrowser, (void**)&browser->webbrowser);
2176 if(NS_FAILED(nsres)) {
2177 ERR("Creating WebBrowser failed: %08x\n", nsres);
2178 return E_FAIL;
2181 nsres = nsIWebBrowser_SetContainerWindow(browser->webbrowser, &browser->nsIWebBrowserChrome_iface);
2182 if(NS_FAILED(nsres)) {
2183 ERR("SetContainerWindow failed: %08x\n", nsres);
2184 return E_FAIL;
2187 nsres = nsIWebBrowser_QueryInterface(browser->webbrowser, &IID_nsIBaseWindow,
2188 (void**)&browser->window);
2189 if(NS_FAILED(nsres)) {
2190 ERR("Could not get nsIBaseWindow interface: %08x\n", nsres);
2191 return E_FAIL;
2194 nsres = nsIWebBrowser_QueryInterface(browser->webbrowser, &IID_nsIWebBrowserSetup,
2195 (void**)&wbsetup);
2196 if(NS_SUCCEEDED(nsres)) {
2197 nsres = nsIWebBrowserSetup_SetProperty(wbsetup, SETUP_IS_CHROME_WRAPPER, FALSE);
2198 nsIWebBrowserSetup_Release(wbsetup);
2199 if(NS_FAILED(nsres)) {
2200 ERR("SetProperty(SETUP_IS_CHROME_WRAPPER) failed: %08x\n", nsres);
2201 return E_FAIL;
2203 }else {
2204 ERR("Could not get nsIWebBrowserSetup interface\n");
2205 return E_FAIL;
2208 nsres = nsIWebBrowser_QueryInterface(browser->webbrowser, &IID_nsIWebNavigation,
2209 (void**)&browser->navigation);
2210 if(NS_FAILED(nsres)) {
2211 ERR("Could not get nsIWebNavigation interface: %08x\n", nsres);
2212 return E_FAIL;
2215 nsres = nsIWebBrowser_QueryInterface(browser->webbrowser, &IID_nsIWebBrowserFocus,
2216 (void**)&browser->focus);
2217 if(NS_FAILED(nsres)) {
2218 ERR("Could not get nsIWebBrowserFocus interface: %08x\n", nsres);
2219 return E_FAIL;
2222 if(!browser_class) {
2223 register_browser_class();
2224 if(!browser_class)
2225 return E_FAIL;
2228 browser->hwnd = CreateWindowExW(0, L"NsContainer", NULL,
2229 WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 0, 0, 100, 100,
2230 GetDesktopWindow(), NULL, hInst, browser);
2231 if(!browser->hwnd) {
2232 WARN("Could not create window\n");
2233 return E_FAIL;
2236 nsres = nsIBaseWindow_InitWindow(browser->window, browser->hwnd, NULL, 0, 0, 100, 100);
2237 if(NS_SUCCEEDED(nsres)) {
2238 nsres = nsIBaseWindow_Create(browser->window);
2239 if(NS_FAILED(nsres)) {
2240 WARN("Creating window failed: %08x\n", nsres);
2241 return E_FAIL;
2244 nsIBaseWindow_SetVisibility(browser->window, FALSE);
2245 nsIBaseWindow_SetEnabled(browser->window, FALSE);
2246 }else {
2247 ERR("InitWindow failed: %08x\n", nsres);
2248 return E_FAIL;
2251 nsres = nsIWebBrowser_SetParentURIContentListener(browser->webbrowser,
2252 &browser->nsIURIContentListener_iface);
2253 if(NS_FAILED(nsres))
2254 ERR("SetParentURIContentListener failed: %08x\n", nsres);
2256 nsres = nsIWebBrowser_QueryInterface(browser->webbrowser, &IID_nsIScrollable, (void**)&scrollable);
2257 if(NS_SUCCEEDED(nsres)) {
2258 nsres = nsIScrollable_SetDefaultScrollbarPreferences(scrollable,
2259 ScrollOrientation_Y, Scrollbar_Always);
2260 if(NS_FAILED(nsres))
2261 ERR("Could not set default Y scrollbar prefs: %08x\n", nsres);
2263 nsres = nsIScrollable_SetDefaultScrollbarPreferences(scrollable,
2264 ScrollOrientation_X, Scrollbar_Auto);
2265 if(NS_FAILED(nsres))
2266 ERR("Could not set default X scrollbar prefs: %08x\n", nsres);
2268 nsIScrollable_Release(scrollable);
2269 }else {
2270 ERR("Could not get nsIScrollable: %08x\n", nsres);
2273 nsres = nsIWebBrowser_GetContentDOMWindow(browser->webbrowser, &mozwindow);
2274 if(NS_FAILED(nsres)) {
2275 ERR("GetContentDOMWindow failed: %08x\n", nsres);
2276 return E_FAIL;
2279 hres = create_outer_window(browser, mozwindow, NULL, &browser->content_window);
2280 mozIDOMWindowProxy_Release(mozwindow);
2281 return hres;
2284 HRESULT create_gecko_browser(HTMLDocumentObj *doc, GeckoBrowser **_ret)
2286 GeckoBrowser *ret;
2287 HRESULT hres;
2289 if(!load_gecko())
2290 return CLASS_E_CLASSNOTAVAILABLE;
2292 ret = heap_alloc_zero(sizeof(GeckoBrowser));
2293 if(!ret)
2294 return E_OUTOFMEMORY;
2296 ret->nsIWebBrowserChrome_iface.lpVtbl = &nsWebBrowserChromeVtbl;
2297 ret->nsIContextMenuListener_iface.lpVtbl = &nsContextMenuListenerVtbl;
2298 ret->nsIURIContentListener_iface.lpVtbl = &nsURIContentListenerVtbl;
2299 ret->nsIEmbeddingSiteWindow_iface.lpVtbl = &nsEmbeddingSiteWindowVtbl;
2300 ret->nsITooltipListener_iface.lpVtbl = &nsTooltipListenerVtbl;
2301 ret->nsIInterfaceRequestor_iface.lpVtbl = &nsInterfaceRequestorVtbl;
2302 ret->nsISupportsWeakReference_iface.lpVtbl = &nsSupportsWeakReferenceVtbl;
2304 ret->doc = doc;
2305 ret->ref = 1;
2306 ret->script_mode = SCRIPTMODE_ACTIVESCRIPT;
2307 ret->usermode = UNKNOWN_USERMODE;
2308 list_init(&ret->document_nodes);
2309 list_init(&ret->outer_windows);
2311 hres = init_browser(ret);
2312 if(SUCCEEDED(hres))
2313 *_ret = ret;
2314 else
2315 nsIWebBrowserChrome_Release(&ret->nsIWebBrowserChrome_iface);
2316 return hres;
2319 void detach_gecko_browser(GeckoBrowser *This)
2321 nsIDOMWindowUtils *window_utils = NULL;
2323 TRACE("(%p)\n", This);
2325 This->doc = NULL;
2327 if(This->content_window) {
2328 get_nsinterface((nsISupports*)This->content_window->nswindow, &IID_nsIDOMWindowUtils, (void**)&window_utils);
2330 IHTMLWindow2_Release(&This->content_window->base.IHTMLWindow2_iface);
2331 This->content_window = NULL;
2334 while(!list_empty(&This->document_nodes)) {
2335 HTMLDocumentNode *doc = LIST_ENTRY(list_head(&This->document_nodes), HTMLDocumentNode, browser_entry);
2336 detach_document_node(doc);
2339 while(!list_empty(&This->outer_windows)) {
2340 HTMLOuterWindow *window = LIST_ENTRY(list_head(&This->outer_windows), HTMLOuterWindow, browser_entry);
2341 list_remove(&window->browser_entry);
2342 window->browser = NULL;
2345 ShowWindow(This->hwnd, SW_HIDE);
2346 SetParent(This->hwnd, NULL);
2348 nsIBaseWindow_SetVisibility(This->window, FALSE);
2349 nsIBaseWindow_Destroy(This->window);
2351 nsIWebBrowser_SetContainerWindow(This->webbrowser, NULL);
2353 nsIWebBrowser_Release(This->webbrowser);
2354 This->webbrowser = NULL;
2356 nsIWebNavigation_Release(This->navigation);
2357 This->navigation = NULL;
2359 nsIBaseWindow_Release(This->window);
2360 This->window = NULL;
2362 nsIWebBrowserFocus_Release(This->focus);
2363 This->focus = NULL;
2365 if(This->editor_controller) {
2366 nsIController_Release(This->editor_controller);
2367 This->editor_controller = NULL;
2370 if(This->editor) {
2371 nsIEditor_Release(This->editor);
2372 This->editor = NULL;
2375 if(This->content_listener) {
2376 nsIURIContentListener_Release(This->content_listener);
2377 This->content_listener = NULL;
2380 if(This->hwnd) {
2381 DestroyWindow(This->hwnd);
2382 This->hwnd = NULL;
2385 nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
2387 /* Force cycle collection */
2388 if(window_utils) {
2389 nsIDOMWindowUtils_CycleCollect(window_utils, NULL, 0);
2390 nsIDOMWindowUtils_Release(window_utils);
2395 * FIXME: nsIScriptObjectPrincipal uses thiscall calling convention, so we need this hack on i386.
2396 * This will be removed after the next Gecko update, that will change calling convention on Gecko side.
2398 #ifdef __i386__
2399 __ASM_GLOBAL_FUNC(call_thiscall_func,
2400 "popl %eax\n\t"
2401 "popl %edx\n\t"
2402 "popl %ecx\n\t"
2403 "pushl %eax\n\t"
2404 "jmp *%edx\n\t")
2405 #define nsIScriptObjectPrincipal_GetPrincipal(this) ((void* (WINAPI*)(void*,void*))&call_thiscall_func)((this)->lpVtbl->GetPrincipal,this)
2406 #endif
2408 nsIXMLHttpRequest *create_nsxhr(nsIDOMWindow *nswindow)
2410 nsIScriptObjectPrincipal *sop;
2411 mozIDOMWindow *inner_window;
2412 nsIPrincipal *nspri;
2413 nsIGlobalObject *nsglo;
2414 nsIXMLHttpRequest *nsxhr;
2415 nsresult nsres;
2417 nsres = nsIDOMWindow_GetInnerWindow(nswindow, &inner_window);
2418 if(NS_FAILED(nsres)) {
2419 ERR("Could not get inner window: %08x\n", nsres);
2420 return NULL;
2423 nsres = mozIDOMWindow_QueryInterface(inner_window, &IID_nsIGlobalObject, (void **)&nsglo);
2424 mozIDOMWindow_Release(inner_window);
2425 assert(nsres == NS_OK);
2427 nsres = nsIGlobalObject_QueryInterface(nsglo, &IID_nsIScriptObjectPrincipal, (void**)&sop);
2428 assert(nsres == NS_OK);
2430 nspri = nsIScriptObjectPrincipal_GetPrincipal(sop);
2431 nsIScriptObjectPrincipal_Release(sop);
2433 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
2434 NS_XMLHTTPREQUEST_CONTRACTID, NULL, &IID_nsIXMLHttpRequest,
2435 (void**)&nsxhr);
2436 if(NS_SUCCEEDED(nsres)) {
2437 nsres = nsIXMLHttpRequest_Init(nsxhr, nspri, NULL, nsglo, NULL, NULL);
2438 if(NS_FAILED(nsres))
2439 nsIXMLHttpRequest_Release(nsxhr);
2441 nsISupports_Release(nspri);
2442 nsIGlobalObject_Release(nsglo);
2443 if(NS_FAILED(nsres)) {
2444 ERR("nsIXMLHttpRequest_Init failed: %08x\n", nsres);
2445 return NULL;
2448 return nsxhr;