makefiles: Explicitly create destination dirs when installing symlinks.
[wine/zf.git] / dlls / mshtml / nsembed.c
blob9a23785ca677d1c9843fbec52e5a07c003e5e511
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 const WCHAR wszNsContainer[] = {'N','s','C','o','n','t','a','i','n','e','r',0};
94 static ATOM browser_class;
95 static WCHAR gecko_path[MAX_PATH];
96 static unsigned gecko_path_len;
98 nsresult create_nsfile(const PRUnichar *path, nsIFile **ret)
100 nsAString str;
101 nsresult nsres;
103 nsAString_InitDepend(&str, path);
104 nsres = NS_NewLocalFile(&str, FALSE, ret);
105 nsAString_Finish(&str);
107 if(NS_FAILED(nsres))
108 WARN("NS_NewLocalFile failed: %08x\n", nsres);
109 return nsres;
112 typedef struct {
113 nsISimpleEnumerator nsISimpleEnumerator_iface;
114 LONG ref;
115 nsISupports *value;
116 } nsSingletonEnumerator;
118 static inline nsSingletonEnumerator *impl_from_nsISimpleEnumerator(nsISimpleEnumerator *iface)
120 return CONTAINING_RECORD(iface, nsSingletonEnumerator, nsISimpleEnumerator_iface);
123 static nsresult NSAPI nsSingletonEnumerator_QueryInterface(nsISimpleEnumerator *iface, nsIIDRef riid, void **ppv)
125 nsSingletonEnumerator *This = impl_from_nsISimpleEnumerator(iface);
127 if(IsEqualGUID(&IID_nsISupports, riid)) {
128 TRACE("(%p)->(IID_nsISupports %p)\n", This, ppv);
129 *ppv = &This->nsISimpleEnumerator_iface;
130 }else if(IsEqualGUID(&IID_nsISimpleEnumerator, riid)) {
131 TRACE("(%p)->(IID_nsISimpleEnumerator %p)\n", This, ppv);
132 *ppv = &This->nsISimpleEnumerator_iface;
133 }else {
134 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
135 *ppv = NULL;
136 return NS_NOINTERFACE;
139 nsISupports_AddRef((nsISupports*)*ppv);
140 return NS_OK;
143 static nsrefcnt NSAPI nsSingletonEnumerator_AddRef(nsISimpleEnumerator *iface)
145 nsSingletonEnumerator *This = impl_from_nsISimpleEnumerator(iface);
146 nsrefcnt ref = InterlockedIncrement(&This->ref);
148 TRACE("(%p) ref=%d\n", This, ref);
150 return ref;
153 static nsrefcnt NSAPI nsSingletonEnumerator_Release(nsISimpleEnumerator *iface)
155 nsSingletonEnumerator *This = impl_from_nsISimpleEnumerator(iface);
156 nsrefcnt ref = InterlockedDecrement(&This->ref);
158 TRACE("(%p) ref=%d\n", This, ref);
160 if(!ref) {
161 if(This->value)
162 nsISupports_Release(This->value);
163 heap_free(This);
166 return ref;
169 static nsresult NSAPI nsSingletonEnumerator_HasMoreElements(nsISimpleEnumerator *iface, cpp_bool *_retval)
171 nsSingletonEnumerator *This = impl_from_nsISimpleEnumerator(iface);
173 TRACE("(%p)->()\n", This);
175 *_retval = This->value != NULL;
176 return NS_OK;
179 static nsresult NSAPI nsSingletonEnumerator_GetNext(nsISimpleEnumerator *iface, nsISupports **_retval)
181 nsSingletonEnumerator *This = impl_from_nsISimpleEnumerator(iface);
183 TRACE("(%p)->()\n", This);
185 if(!This->value)
186 return NS_ERROR_UNEXPECTED;
188 *_retval = This->value;
189 This->value = NULL;
190 return NS_OK;
193 static const nsISimpleEnumeratorVtbl nsSingletonEnumeratorVtbl = {
194 nsSingletonEnumerator_QueryInterface,
195 nsSingletonEnumerator_AddRef,
196 nsSingletonEnumerator_Release,
197 nsSingletonEnumerator_HasMoreElements,
198 nsSingletonEnumerator_GetNext
201 static nsISimpleEnumerator *create_singleton_enumerator(nsISupports *value)
203 nsSingletonEnumerator *ret;
205 ret = heap_alloc(sizeof(*ret));
206 if(!ret)
207 return NULL;
209 ret->nsISimpleEnumerator_iface.lpVtbl = &nsSingletonEnumeratorVtbl;
210 ret->ref = 1;
212 if(value)
213 nsISupports_AddRef(value);
214 ret->value = value;
215 return &ret->nsISimpleEnumerator_iface;
218 static nsresult NSAPI nsDirectoryServiceProvider2_QueryInterface(nsIDirectoryServiceProvider2 *iface,
219 nsIIDRef riid, void **result)
221 if(IsEqualGUID(&IID_nsISupports, riid)) {
222 TRACE("(IID_nsISupports %p)\n", result);
223 *result = iface;
224 }else if(IsEqualGUID(&IID_nsIDirectoryServiceProvider, riid)) {
225 TRACE("(IID_nsIDirectoryServiceProvider %p)\n", result);
226 *result = iface;
227 }else if(IsEqualGUID(&IID_nsIDirectoryServiceProvider2, riid)) {
228 TRACE("(IID_nsIDirectoryServiceProvider2 %p)\n", result);
229 *result = iface;
230 }else {
231 WARN("(%s %p)\n", debugstr_guid(riid), result);
232 *result = NULL;
233 return NS_NOINTERFACE;
236 nsISupports_AddRef((nsISupports*)*result);
237 return NS_OK;
240 static nsrefcnt NSAPI nsDirectoryServiceProvider2_AddRef(nsIDirectoryServiceProvider2 *iface)
242 return 2;
245 static nsrefcnt NSAPI nsDirectoryServiceProvider2_Release(nsIDirectoryServiceProvider2 *iface)
247 return 1;
250 static nsresult create_profile_directory(void)
252 static const WCHAR wine_geckoW[] = {'\\','w','i','n','e','_','g','e','c','k','o',0};
254 WCHAR path[MAX_PATH + ARRAY_SIZE(wine_geckoW)];
255 cpp_bool exists;
256 nsresult nsres;
257 HRESULT hres;
259 hres = SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, path);
260 if(FAILED(hres)) {
261 ERR("SHGetFolderPath failed: %08x\n", hres);
262 return NS_ERROR_FAILURE;
265 lstrcatW(path, wine_geckoW);
266 nsres = create_nsfile(path, &profile_directory);
267 if(NS_FAILED(nsres))
268 return nsres;
270 nsres = nsIFile_Exists(profile_directory, &exists);
271 if(NS_FAILED(nsres)) {
272 ERR("Exists failed: %08x\n", nsres);
273 return nsres;
276 if(!exists) {
277 nsres = nsIFile_Create(profile_directory, 1, 0700);
278 if(NS_FAILED(nsres))
279 ERR("Create failed: %08x\n", nsres);
282 return nsres;
285 static nsresult NSAPI nsDirectoryServiceProvider2_GetFile(nsIDirectoryServiceProvider2 *iface,
286 const char *prop, cpp_bool *persistent, nsIFile **_retval)
288 TRACE("(%s %p %p)\n", debugstr_a(prop), persistent, _retval);
290 if(!strcmp(prop, "ProfD")) {
291 if(!profile_directory) {
292 nsresult nsres;
294 nsres = create_profile_directory();
295 if(NS_FAILED(nsres))
296 return nsres;
299 assert(profile_directory != NULL);
300 return nsIFile_Clone(profile_directory, _retval);
303 *_retval = NULL;
304 return NS_ERROR_FAILURE;
307 static nsresult NSAPI nsDirectoryServiceProvider2_GetFiles(nsIDirectoryServiceProvider2 *iface,
308 const char *prop, nsISimpleEnumerator **_retval)
310 TRACE("(%s %p)\n", debugstr_a(prop), _retval);
312 if(!strcmp(prop, "APluginsDL")) {
313 WCHAR plugin_path[MAX_PATH];
314 nsIFile *file;
315 int len;
316 nsresult nsres;
318 if(!plugin_directory) {
319 static const WCHAR gecko_pluginW[] = {'\\','g','e','c','k','o','\\','p','l','u','g','i','n',0};
321 len = GetSystemDirectoryW(plugin_path, ARRAY_SIZE(plugin_path)-ARRAY_SIZE(gecko_pluginW)+1);
322 if(!len)
323 return NS_ERROR_UNEXPECTED;
325 lstrcpyW(plugin_path+len, gecko_pluginW);
326 nsres = create_nsfile(plugin_path, &plugin_directory);
327 if(NS_FAILED(nsres)) {
328 *_retval = NULL;
329 return nsres;
333 nsres = nsIFile_Clone(plugin_directory, &file);
334 if(NS_FAILED(nsres))
335 return nsres;
337 *_retval = create_singleton_enumerator((nsISupports*)file);
338 nsIFile_Release(file);
339 if(!*_retval)
340 return NS_ERROR_OUT_OF_MEMORY;
342 return NS_OK;
345 *_retval = NULL;
346 return NS_ERROR_FAILURE;
349 static const nsIDirectoryServiceProvider2Vtbl nsDirectoryServiceProvider2Vtbl = {
350 nsDirectoryServiceProvider2_QueryInterface,
351 nsDirectoryServiceProvider2_AddRef,
352 nsDirectoryServiceProvider2_Release,
353 nsDirectoryServiceProvider2_GetFile,
354 nsDirectoryServiceProvider2_GetFiles
357 static nsIDirectoryServiceProvider2 nsDirectoryServiceProvider2 =
358 { &nsDirectoryServiceProvider2Vtbl };
360 static LRESULT WINAPI nsembed_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
362 GeckoBrowser *This;
363 nsresult nsres;
365 static const WCHAR wszTHIS[] = {'T','H','I','S',0};
367 if(msg == WM_CREATE) {
368 This = *(GeckoBrowser**)lParam;
369 SetPropW(hwnd, wszTHIS, This);
370 }else {
371 This = GetPropW(hwnd, wszTHIS);
374 switch(msg) {
375 case WM_SIZE:
376 TRACE("(%p)->(WM_SIZE)\n", This);
378 nsres = nsIBaseWindow_SetSize(This->window,
379 LOWORD(lParam), HIWORD(lParam), TRUE);
380 if(NS_FAILED(nsres))
381 WARN("SetSize failed: %08x\n", nsres);
382 break;
384 case WM_PARENTNOTIFY:
385 TRACE("WM_PARENTNOTIFY %x\n", (unsigned)wParam);
387 switch(wParam) {
388 case WM_LBUTTONDOWN:
389 case WM_RBUTTONDOWN:
390 nsIWebBrowserFocus_Activate(This->focus);
394 return DefWindowProcW(hwnd, msg, wParam, lParam);
398 static void register_browser_class(void)
400 static WNDCLASSEXW wndclass = {
401 sizeof(WNDCLASSEXW),
402 CS_DBLCLKS,
403 nsembed_proc,
404 0, 0, NULL, NULL, NULL, NULL, NULL,
405 wszNsContainer,
406 NULL,
408 wndclass.hInstance = hInst;
409 browser_class = RegisterClassExW(&wndclass);
412 static BOOL install_wine_gecko(void)
414 PROCESS_INFORMATION pi;
415 STARTUPINFOW si;
416 WCHAR app[MAX_PATH];
417 WCHAR *args;
418 LONG len;
419 BOOL ret;
421 static const WCHAR controlW[] = {'\\','c','o','n','t','r','o','l','.','e','x','e',0};
422 static const WCHAR argsW[] =
423 {' ','a','p','p','w','i','z','.','c','p','l',' ','i','n','s','t','a','l','l','_','g','e','c','k','o',0};
425 len = GetSystemDirectoryW(app, MAX_PATH-ARRAY_SIZE(controlW));
426 memcpy(app+len, controlW, sizeof(controlW));
428 args = heap_alloc(len*sizeof(WCHAR) + sizeof(controlW) + sizeof(argsW));
429 if(!args)
430 return FALSE;
432 memcpy(args, app, len*sizeof(WCHAR) + sizeof(controlW));
433 memcpy(args + len + ARRAY_SIZE(controlW)-1, argsW, sizeof(argsW));
435 TRACE("starting %s\n", debugstr_w(args));
437 memset(&si, 0, sizeof(si));
438 si.cb = sizeof(si);
439 ret = CreateProcessW(app, args, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
440 heap_free(args);
441 if (ret) {
442 CloseHandle(pi.hThread);
443 WaitForSingleObject(pi.hProcess, INFINITE);
444 CloseHandle(pi.hProcess);
445 }else {
446 WARN("installation failed\n");
449 return ret;
452 static void set_environment(LPCWSTR gre_path)
454 size_t len, gre_path_len;
455 int debug_level = 0;
456 WCHAR *path, buf[20];
457 const WCHAR *ptr;
459 static const WCHAR pathW[] = {'P','A','T','H',0};
460 static const WCHAR warnW[] = {'w','a','r','n',0};
461 static const WCHAR xpcom_debug_breakW[] =
462 {'X','P','C','O','M','_','D','E','B','U','G','_','B','R','E','A','K',0};
463 static const WCHAR nspr_log_modulesW[] =
464 {'N','S','P','R','_','L','O','G','_','M','O','D','U','L','E','S',0};
465 static const WCHAR debug_formatW[] = {'a','l','l',':','%','d',0};
467 SetEnvironmentVariableW(xpcom_debug_breakW, warnW);
469 if(TRACE_ON(gecko))
470 debug_level = 5;
471 else if(WARN_ON(gecko))
472 debug_level = 2;
473 else if(ERR_ON(gecko))
474 debug_level = 1;
476 swprintf(buf, ARRAY_SIZE(buf), debug_formatW, debug_level);
477 SetEnvironmentVariableW(nspr_log_modulesW, buf);
479 len = GetEnvironmentVariableW(pathW, NULL, 0);
480 gre_path_len = lstrlenW(gre_path);
481 path = heap_alloc((len+gre_path_len+1)*sizeof(WCHAR));
482 if(!path)
483 return;
484 GetEnvironmentVariableW(pathW, path, len);
486 /* We have to modify PATH as xul.dll loads other DLLs from this directory. */
487 if(!(ptr = wcsstr(path, gre_path))
488 || (ptr > path && *(ptr-1) != ';')
489 || (ptr[gre_path_len] && ptr[gre_path_len] != ';')) {
490 if(len)
491 path[len-1] = ';';
492 lstrcpyW(path+len, gre_path);
493 SetEnvironmentVariableW(pathW, path);
495 heap_free(path);
498 static void set_bool_pref(nsIPrefBranch *pref, const char *pref_name, BOOL val)
500 nsresult nsres;
502 nsres = nsIPrefBranch_SetBoolPref(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_int_pref(nsIPrefBranch *pref, const char *pref_name, int val)
509 nsresult nsres;
511 nsres = nsIPrefBranch_SetIntPref(pref, pref_name, val);
512 if(NS_FAILED(nsres))
513 ERR("Could not set pref %s\n", debugstr_a(pref_name));
516 static void set_string_pref(nsIPrefBranch *pref, const char *pref_name, const char *val)
518 nsresult nsres;
520 nsres = nsIPrefBranch_SetCharPref(pref, pref_name, val);
521 if(NS_FAILED(nsres))
522 ERR("Could not set pref %s\n", debugstr_a(pref_name));
525 static void set_lang(nsIPrefBranch *pref)
527 char langs[100];
528 DWORD res, size, type;
529 HKEY hkey;
531 static const WCHAR international_keyW[] =
532 {'S','o','f','t','w','a','r','e',
533 '\\','M','i','c','r','o','s','o','f','t',
534 '\\','I','n','t','e','r','n','e','t',' ','E','x','p','l','o','r','e','r',
535 '\\','I','n','t','e','r','n','a','t','i','o','n','a','l',0};
537 res = RegOpenKeyW(HKEY_CURRENT_USER, international_keyW, &hkey);
538 if(res != ERROR_SUCCESS)
539 return;
541 size = sizeof(langs);
542 res = RegQueryValueExA(hkey, "AcceptLanguage", 0, &type, (LPBYTE)langs, &size);
543 RegCloseKey(hkey);
544 if(res != ERROR_SUCCESS || type != REG_SZ)
545 return;
547 TRACE("Setting lang %s\n", debugstr_a(langs));
549 set_string_pref(pref, "intl.accept_languages", langs);
552 static void set_preferences(void)
554 nsIPrefBranch *pref;
555 nsresult nsres;
557 nsres = nsIServiceManager_GetServiceByContractID(pServMgr, NS_PREFERENCES_CONTRACTID,
558 &IID_nsIPrefBranch, (void**)&pref);
559 if(NS_FAILED(nsres)) {
560 ERR("Could not get preference service: %08x\n", nsres);
561 return;
564 set_lang(pref);
565 set_bool_pref(pref, "security.warn_entering_secure", FALSE);
566 set_bool_pref(pref, "security.warn_submit_insecure", FALSE);
567 set_int_pref(pref, "layout.spellcheckDefault", 0);
569 nsIPrefBranch_Release(pref);
572 static BOOL init_xpcom(const PRUnichar *gre_path)
574 nsIComponentRegistrar *registrar = NULL;
575 nsIFile *gre_dir;
576 WCHAR *ptr;
577 nsresult nsres;
579 nsres = create_nsfile(gre_path, &gre_dir);
580 if(NS_FAILED(nsres)) {
581 FreeLibrary(xul_handle);
582 return FALSE;
585 nsres = NS_InitXPCOM2(&pServMgr, gre_dir, (nsIDirectoryServiceProvider*)&nsDirectoryServiceProvider2);
586 if(NS_FAILED(nsres)) {
587 ERR("NS_InitXPCOM2 failed: %08x\n", nsres);
588 FreeLibrary(xul_handle);
589 return FALSE;
592 lstrcpyW(gecko_path, gre_path);
593 for(ptr = gecko_path; *ptr; ptr++) {
594 if(*ptr == '\\')
595 *ptr = '/';
597 gecko_path_len = ptr-gecko_path;
599 nsres = nsIServiceManager_QueryInterface(pServMgr, &IID_nsIComponentManager, (void**)&pCompMgr);
600 if(NS_FAILED(nsres))
601 ERR("Could not get nsIComponentManager: %08x\n", nsres);
603 init_nsio(pCompMgr);
604 init_mutation(pCompMgr);
605 set_preferences();
607 nsres = nsIServiceManager_GetServiceByContractID(pServMgr, NS_CATEGORYMANAGER_CONTRACTID,
608 &IID_nsICategoryManager, (void**)&cat_mgr);
609 if(NS_FAILED(nsres))
610 ERR("Could not get category manager service: %08x\n", nsres);
612 nsres = NS_GetComponentRegistrar(&registrar);
613 if(NS_SUCCEEDED(nsres)) {
614 register_nsservice(registrar, pServMgr);
615 nsIComponentRegistrar_Release(registrar);
616 }else {
617 ERR("NS_GetComponentRegistrar failed: %08x\n", nsres);
620 init_node_cc();
622 return TRUE;
625 static BOOL load_xul(WCHAR *gecko_path)
627 size_t len;
629 set_environment(gecko_path);
631 TRACE("(%s)\n", debugstr_w(gecko_path));
633 len = wcslen(gecko_path);
634 wcscpy(gecko_path + len, L"\\xul.dll");
635 xul_handle = LoadLibraryExW(gecko_path, 0, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR);
636 gecko_path[len] = 0;
637 if(!xul_handle) {
638 WARN("Could not load XUL: %d\n", GetLastError());
639 return FALSE;
642 #define NS_DLSYM(func) \
643 func = (void *)GetProcAddress(xul_handle, #func); \
644 if(!func) \
645 ERR("Could not GetProcAddress(" #func ") failed\n")
647 NS_DLSYM(NS_InitXPCOM2);
648 NS_DLSYM(NS_ShutdownXPCOM);
649 NS_DLSYM(NS_GetComponentRegistrar);
650 NS_DLSYM(NS_StringContainerInit2);
651 NS_DLSYM(NS_CStringContainerInit2);
652 NS_DLSYM(NS_StringContainerFinish);
653 NS_DLSYM(NS_CStringContainerFinish);
654 NS_DLSYM(NS_StringSetData);
655 NS_DLSYM(NS_CStringSetData);
656 NS_DLSYM(NS_NewLocalFile);
657 NS_DLSYM(NS_StringGetData);
658 NS_DLSYM(NS_CStringGetData);
659 NS_DLSYM(NS_StringGetIsVoid);
660 NS_DLSYM(NS_Alloc);
661 NS_DLSYM(NS_Free);
662 NS_DLSYM(ccref_incr);
663 NS_DLSYM(ccref_decr);
664 NS_DLSYM(ccref_init);
665 NS_DLSYM(ccp_init);
666 NS_DLSYM(describe_cc_node);
667 NS_DLSYM(note_cc_edge);
669 #undef NS_DLSYM
671 return init_xpcom(gecko_path);
674 static WCHAR *check_version(const WCHAR *path)
676 WCHAR *file_name;
677 char version[128];
678 DWORD read=0;
679 size_t len;
680 HANDLE hfile;
682 if(!wcsncmp(path, L"\\??\\", 4))
683 path += 4;
684 if(path[1] != ':') {
685 TRACE("Skipping %s\n", debugstr_w(path));
686 return FALSE; /* Gecko needs to be accessible via dos path */
689 len = wcslen(path);
690 file_name = heap_alloc((len + 12) * sizeof(WCHAR));
691 if(!file_name)
692 return NULL;
694 PathCanonicalizeW(file_name, path);
695 len = lstrlenW(file_name);
696 wcscpy(file_name + len, L"\\VERSION");
698 hfile = CreateFileW(file_name, GENERIC_READ, FILE_SHARE_READ, NULL,
699 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
700 file_name[len] = 0;
701 if(hfile == INVALID_HANDLE_VALUE) {
702 TRACE("%s not found\n", debugstr_w(file_name));
703 heap_free(file_name);
704 return NULL;
707 ReadFile(hfile, version, sizeof(version), &read, NULL);
708 version[read] = 0;
709 CloseHandle(hfile);
711 TRACE("%s: %s\n", debugstr_w(file_name), debugstr_a(version));
713 if(strcmp(version, GECKO_VERSION_STRING)) {
714 ERR("Unexpected version %s, expected \"%s\"\n", debugstr_a(version),
715 GECKO_VERSION_STRING);
716 heap_free(file_name);
717 return NULL;
720 return file_name;
723 static WCHAR *find_wine_gecko_reg(void)
725 WCHAR buffer[MAX_PATH];
726 DWORD res, type, size;
727 HKEY hkey;
729 /* @@ Wine registry key: HKLM\Software\Wine\MSHTML\<version> */
730 res = RegOpenKeyW(HKEY_LOCAL_MACHINE, L"Software\\Wine\\MSHTML\\" GECKO_VERSION, &hkey);
731 if(res != ERROR_SUCCESS)
732 return NULL;
734 size = ARRAY_SIZE(buffer);
735 res = RegQueryValueExW(hkey, L"GeckoPath", NULL, &type, (LPBYTE)buffer, &size);
736 RegCloseKey(hkey);
737 if(res != ERROR_SUCCESS || type != REG_SZ)
738 return FALSE;
740 return check_version(buffer);
743 static WCHAR *heap_strcat(const WCHAR *str1, const WCHAR *str2)
745 size_t len1 = lstrlenW(str1);
746 size_t len2 = lstrlenW(str2);
747 WCHAR *ret = heap_alloc((len1 + len2 + 1) * sizeof(WCHAR));
748 if(!ret) return NULL;
749 memcpy(ret, str1, len1 * sizeof(WCHAR));
750 memcpy(ret + len1, str2, len2 * sizeof(WCHAR));
751 ret[len1 + len2] = 0;
752 return ret;
755 static WCHAR *find_wine_gecko_datadir(void)
757 const WCHAR *data_dir;
758 WCHAR *path = NULL, *ret;
760 if((data_dir = _wgetenv(L"WINEDATADIR")))
761 path = heap_strcat(data_dir, L"\\gecko\\" GECKO_DIR_NAME);
762 else if((data_dir = _wgetenv(L"WINEBUILDDIR")))
763 path = heap_strcat(data_dir, L"\\..\\gecko\\" GECKO_DIR_NAME);
764 if(!path)
765 return NULL;
767 ret = check_version(path);
768 heap_free(path);
769 return ret;
772 static WCHAR *find_wine_gecko_unix(const char *unix_path)
774 static WCHAR * (CDECL *p_wine_get_dos_file_name)(const char*);
775 WCHAR *dos_dir, *ret;
777 if(!p_wine_get_dos_file_name) {
778 p_wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleA("kernel32"), "wine_get_dos_file_name");
779 if(!p_wine_get_dos_file_name)
780 return FALSE;
783 dos_dir = p_wine_get_dos_file_name(unix_path);
784 if(!dos_dir)
785 return FALSE;
787 ret = check_version(dos_dir);
789 heap_free(dos_dir);
790 return ret;
793 static CRITICAL_SECTION cs_load_gecko;
794 static CRITICAL_SECTION_DEBUG cs_load_gecko_dbg =
796 0, 0, &cs_load_gecko,
797 { &cs_load_gecko_dbg.ProcessLocksList, &cs_load_gecko_dbg.ProcessLocksList },
798 0, 0, { (DWORD_PTR)(__FILE__ ": load_gecko") }
800 static CRITICAL_SECTION cs_load_gecko = { &cs_load_gecko_dbg, -1, 0, 0, 0, 0 };
802 BOOL load_gecko(void)
804 BOOL ret = FALSE;
806 static DWORD loading_thread;
808 TRACE("()\n");
810 if(!GECKO_ARCH_STRING[0]) {
811 FIXME("Wine Gecko is not supported on this architecture.\n");
812 return FALSE;
815 /* load_gecko may be called recursively */
816 if(loading_thread == GetCurrentThreadId())
817 return pCompMgr != NULL;
819 EnterCriticalSection(&cs_load_gecko);
821 if(!loading_thread) {
822 WCHAR *gecko_path;
824 loading_thread = GetCurrentThreadId();
826 if(!(gecko_path = find_wine_gecko_reg())
827 && !(gecko_path = find_wine_gecko_datadir())
828 && !(gecko_path = find_wine_gecko_unix(INSTALL_DATADIR "/wine/gecko/" GECKO_DIR_NAME))
829 && (!strcmp(INSTALL_DATADIR, "/usr/share") ||
830 !(gecko_path = find_wine_gecko_unix("/usr/share/wine/gecko/" GECKO_DIR_NAME)))
831 && !(gecko_path = find_wine_gecko_unix("/opt/wine/gecko/" GECKO_DIR_NAME))
832 && install_wine_gecko())
833 gecko_path = find_wine_gecko_reg();
835 if(gecko_path) {
836 ret = load_xul(gecko_path);
837 heap_free(gecko_path);
838 }else {
839 MESSAGE("Could not find Wine Gecko. HTML rendering will be disabled.\n");
841 }else {
842 ret = pCompMgr != NULL;
845 LeaveCriticalSection(&cs_load_gecko);
847 return ret;
850 void *nsalloc(size_t size)
852 return NS_Alloc(size);
855 void nsfree(void *mem)
857 NS_Free(mem);
860 BOOL nsACString_Init(nsACString *str, const char *data)
862 return NS_SUCCEEDED(NS_CStringContainerInit2(str, data, PR_UINT32_MAX, 0));
866 * Initializes nsACString with data owned by caller.
867 * Caller must ensure that data is valid during lifetime of string object.
869 void nsACString_InitDepend(nsACString *str, const char *data)
871 NS_CStringContainerInit2(str, data, PR_UINT32_MAX, NS_CSTRING_CONTAINER_INIT_DEPEND);
874 void nsACString_SetData(nsACString *str, const char *data)
876 NS_CStringSetData(str, data, PR_UINT32_MAX);
879 UINT32 nsACString_GetData(const nsACString *str, const char **data)
881 return NS_CStringGetData(str, data, NULL);
884 void nsACString_Finish(nsACString *str)
886 NS_CStringContainerFinish(str);
889 BOOL nsAString_Init(nsAString *str, const PRUnichar *data)
891 return NS_SUCCEEDED(NS_StringContainerInit2(str, data, PR_UINT32_MAX, 0));
895 * Initializes nsAString with data owned by caller.
896 * Caller must ensure that data is valid during lifetime of string object.
898 void nsAString_InitDepend(nsAString *str, const PRUnichar *data)
900 NS_StringContainerInit2(str, data, PR_UINT32_MAX, NS_STRING_CONTAINER_INIT_DEPEND);
903 UINT32 nsAString_GetData(const nsAString *str, const PRUnichar **data)
905 return NS_StringGetData(str, data, NULL);
908 void nsAString_SetData(nsAString *str, const PRUnichar *data)
910 NS_StringSetData(str, data, PR_UINT32_MAX);
913 void nsAString_Finish(nsAString *str)
915 NS_StringContainerFinish(str);
918 HRESULT map_nsresult(nsresult nsres)
920 switch(nsres) {
921 case NS_OK:
922 return S_OK;
923 case NS_ERROR_OUT_OF_MEMORY:
924 return E_OUTOFMEMORY;
925 case NS_ERROR_NOT_IMPLEMENTED:
926 return E_NOTIMPL;
927 case NS_NOINTERFACE:
928 return E_NOINTERFACE;
929 case NS_ERROR_INVALID_POINTER:
930 return E_POINTER;
931 case NS_ERROR_INVALID_ARG:
932 return E_INVALIDARG;
933 case NS_ERROR_UNEXPECTED:
934 return E_UNEXPECTED;
935 case NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR:
936 return 0x80700007; /* according to tests */
938 return E_FAIL;
941 HRESULT return_nsstr(nsresult nsres, nsAString *nsstr, BSTR *p)
943 const PRUnichar *str;
945 if(NS_FAILED(nsres)) {
946 WARN("failed: %08x\n", nsres);
947 nsAString_Finish(nsstr);
948 return map_nsresult(nsres);
951 nsAString_GetData(nsstr, &str);
952 TRACE("ret %s\n", debugstr_w(str));
953 if(*str) {
954 *p = SysAllocString(str);
955 if(!*p)
956 return E_OUTOFMEMORY;
957 }else {
958 *p = NULL;
961 nsAString_Finish(nsstr);
962 return S_OK;
965 HRESULT return_nsstr_variant(nsresult nsres, nsAString *nsstr, unsigned flags, VARIANT *p)
967 HRESULT hres = S_OK;
969 if(NS_FAILED(nsres)) {
970 ERR("failed: %08x\n", nsres);
971 nsAString_Finish(nsstr);
972 return map_nsresult(nsres);
975 if(NS_StringGetIsVoid(nsstr)) {
976 V_VT(p) = VT_NULL;
977 }else {
978 const WCHAR *str;
979 size_t len;
980 nsAString_GetData(nsstr, &str);
981 len = wcslen(str);
982 if(flags & NSSTR_IMPLICIT_PX) {
983 const WCHAR *iter;
984 if(len > 2 && !wcscmp(str + len - 2, L"px"))
985 len -= 2;
986 for(iter = str; iter < str + len && is_digit(*iter); iter++);
987 if(*iter == '.') {
988 const WCHAR *dot = iter++;
989 while(iter < str + len && is_digit(*iter)) iter++;
990 if(iter == str + len && dot) len = dot - str;
993 if(flags & NSSTR_COLOR) {
994 hres = nscolor_to_str(str, &V_BSTR(p));
995 }else if(*str) {
996 V_BSTR(p) = SysAllocStringLen(str, len);
997 if(!V_BSTR(p))
998 hres = E_OUTOFMEMORY;
999 }else {
1000 V_BSTR(p) = NULL;
1002 if(SUCCEEDED(hres))
1003 V_VT(p) = VT_BSTR;
1006 nsAString_Finish(nsstr);
1007 if(FAILED(hres))
1008 return hres;
1009 TRACE("ret %s\n", debugstr_variant(p));
1010 return S_OK;
1014 * Converts VARIANT to string and stores the result in nsAString. To avoid useless
1015 * allocations, the function uses an existing string if available, so caller must
1016 * ensure that passed VARIANT is unchanged as long as its string representation is used
1018 HRESULT variant_to_nsstr(VARIANT *v, BOOL hex_int, nsAString *nsstr)
1020 WCHAR buf[32];
1022 static const WCHAR d_formatW[] = {'%','d',0};
1023 static const WCHAR hex_formatW[] = {'#','%','0','6','x',0};
1025 switch(V_VT(v)) {
1026 case VT_NULL:
1027 nsAString_InitDepend(nsstr, NULL);
1028 return S_OK;
1030 case VT_BSTR:
1031 nsAString_InitDepend(nsstr, V_BSTR(v));
1032 break;
1034 case VT_BSTR|VT_BYREF:
1035 nsAString_InitDepend(nsstr, *V_BSTRREF(v));
1036 break;
1038 case VT_I4:
1039 wsprintfW(buf, hex_int ? hex_formatW : d_formatW, V_I4(v));
1040 nsAString_Init(nsstr, buf);
1041 break;
1043 case VT_R8: {
1044 VARIANT strv;
1045 HRESULT hres;
1047 V_VT(&strv) = VT_EMPTY;
1048 hres = VariantChangeTypeEx(&strv, v, MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT),
1049 0, VT_BSTR);
1050 if(FAILED(hres))
1051 return hres;
1053 nsAString_Init(nsstr, V_BSTR(&strv));
1054 SysFreeString(V_BSTR(&strv));
1055 break;
1058 default:
1059 FIXME("not implemented for %s\n", debugstr_variant(v));
1060 return E_NOTIMPL;
1063 return S_OK;
1066 nsICommandParams *create_nscommand_params(void)
1068 nsICommandParams *ret = NULL;
1069 nsresult nsres;
1071 if(!pCompMgr)
1072 return NULL;
1074 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
1075 NS_COMMANDPARAMS_CONTRACTID, NULL, &IID_nsICommandParams,
1076 (void**)&ret);
1077 if(NS_FAILED(nsres))
1078 ERR("Could not get nsICommandParams\n");
1080 return ret;
1083 nsIWritableVariant *create_nsvariant(void)
1085 nsIWritableVariant *ret = NULL;
1086 nsresult nsres;
1088 if(!pCompMgr)
1089 return NULL;
1091 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
1092 NS_VARIANT_CONTRACTID, NULL, &IID_nsIWritableVariant, (void**)&ret);
1093 if(NS_FAILED(nsres))
1094 ERR("Could not get nsIVariant\n");
1096 return ret;
1099 char *get_nscategory_entry(const char *category, const char *entry)
1101 char *ret = NULL;
1102 nsresult nsres;
1104 nsres = nsICategoryManager_GetCategoryEntry(cat_mgr, category, entry, &ret);
1105 return NS_SUCCEEDED(nsres) ? ret : NULL;
1108 nsresult get_nsinterface(nsISupports *iface, REFIID riid, void **ppv)
1110 nsIInterfaceRequestor *iface_req;
1111 nsresult nsres;
1113 nsres = nsISupports_QueryInterface(iface, &IID_nsIInterfaceRequestor, (void**)&iface_req);
1114 if(NS_FAILED(nsres))
1115 return nsres;
1117 nsres = nsIInterfaceRequestor_GetInterface(iface_req, riid, ppv);
1118 nsIInterfaceRequestor_Release(iface_req);
1120 return nsres;
1123 static HRESULT nsnode_to_nsstring_rec(nsIContentSerializer *serializer, nsIDOMNode *nsnode, nsAString *str)
1125 nsIDOMNodeList *node_list = NULL;
1126 cpp_bool has_children = FALSE;
1127 nsIContent *nscontent;
1128 UINT16 type;
1129 nsresult nsres;
1131 nsIDOMNode_HasChildNodes(nsnode, &has_children);
1133 nsres = nsIDOMNode_GetNodeType(nsnode, &type);
1134 if(NS_FAILED(nsres)) {
1135 ERR("GetType failed: %08x\n", nsres);
1136 return E_FAIL;
1139 if(type != DOCUMENT_NODE) {
1140 nsres = nsIDOMNode_QueryInterface(nsnode, &IID_nsIContent, (void**)&nscontent);
1141 if(NS_FAILED(nsres)) {
1142 ERR("Could not get nsIContent interface: %08x\n", nsres);
1143 return E_FAIL;
1147 switch(type) {
1148 case ELEMENT_NODE:
1149 nsIContentSerializer_AppendElementStart(serializer, nscontent, nscontent, str);
1150 break;
1151 case TEXT_NODE:
1152 nsIContentSerializer_AppendText(serializer, nscontent, 0, -1, str);
1153 break;
1154 case COMMENT_NODE:
1155 nsIContentSerializer_AppendComment(serializer, nscontent, 0, -1, str);
1156 break;
1157 case DOCUMENT_NODE: {
1158 nsIDocument *nsdoc;
1159 nsIDOMNode_QueryInterface(nsnode, &IID_nsIDocument, (void**)&nsdoc);
1160 nsIContentSerializer_AppendDocumentStart(serializer, nsdoc, str);
1161 nsIDocument_Release(nsdoc);
1162 break;
1164 case DOCUMENT_TYPE_NODE:
1165 nsIContentSerializer_AppendDoctype(serializer, nscontent, str);
1166 break;
1167 case DOCUMENT_FRAGMENT_NODE:
1168 break;
1169 default:
1170 FIXME("Unhandled type %u\n", type);
1173 if(has_children) {
1174 UINT32 child_cnt, i;
1175 nsIDOMNode *child_node;
1177 nsIDOMNode_GetChildNodes(nsnode, &node_list);
1178 nsIDOMNodeList_GetLength(node_list, &child_cnt);
1180 for(i=0; i<child_cnt; i++) {
1181 nsres = nsIDOMNodeList_Item(node_list, i, &child_node);
1182 if(NS_SUCCEEDED(nsres)) {
1183 nsnode_to_nsstring_rec(serializer, child_node, str);
1184 nsIDOMNode_Release(child_node);
1185 }else {
1186 ERR("Item failed: %08x\n", nsres);
1190 nsIDOMNodeList_Release(node_list);
1193 if(type == ELEMENT_NODE)
1194 nsIContentSerializer_AppendElementEnd(serializer, nscontent, str);
1196 if(type != DOCUMENT_NODE)
1197 nsIContent_Release(nscontent);
1198 return S_OK;
1201 HRESULT nsnode_to_nsstring(nsIDOMNode *nsnode, nsAString *str)
1203 nsIContentSerializer *serializer;
1204 nsresult nsres;
1205 HRESULT hres;
1207 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
1208 NS_HTMLSERIALIZER_CONTRACTID, NULL, &IID_nsIContentSerializer,
1209 (void**)&serializer);
1210 if(NS_FAILED(nsres)) {
1211 ERR("Could not get nsIContentSerializer: %08x\n", nsres);
1212 return E_FAIL;
1215 nsres = nsIContentSerializer_Init(serializer, 0, 100, NULL, FALSE, FALSE /* FIXME */);
1216 if(NS_FAILED(nsres))
1217 ERR("Init failed: %08x\n", nsres);
1219 hres = nsnode_to_nsstring_rec(serializer, nsnode, str);
1220 if(SUCCEEDED(hres)) {
1221 nsres = nsIContentSerializer_Flush(serializer, str);
1222 if(NS_FAILED(nsres))
1223 ERR("Flush failed: %08x\n", nsres);
1226 nsIContentSerializer_Release(serializer);
1227 return hres;
1230 void setup_editor_controller(GeckoBrowser *This)
1232 nsIEditingSession *editing_session = NULL;
1233 nsIControllerContext *ctrlctx;
1234 nsresult nsres;
1236 if(This->editor) {
1237 nsIEditor_Release(This->editor);
1238 This->editor = NULL;
1241 if(This->editor_controller) {
1242 nsIController_Release(This->editor_controller);
1243 This->editor_controller = NULL;
1246 nsres = get_nsinterface((nsISupports*)This->webbrowser, &IID_nsIEditingSession,
1247 (void**)&editing_session);
1248 if(NS_FAILED(nsres)) {
1249 ERR("Could not get nsIEditingSession: %08x\n", nsres);
1250 return;
1253 nsres = nsIEditingSession_GetEditorForWindow(editing_session,
1254 This->doc->basedoc.window->window_proxy, &This->editor);
1255 nsIEditingSession_Release(editing_session);
1256 if(NS_FAILED(nsres)) {
1257 ERR("Could not get editor: %08x\n", nsres);
1258 return;
1261 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
1262 NS_EDITORCONTROLLER_CONTRACTID, NULL, &IID_nsIControllerContext, (void**)&ctrlctx);
1263 if(NS_SUCCEEDED(nsres)) {
1264 nsres = nsIControllerContext_SetCommandContext(ctrlctx, (nsISupports *)This->editor);
1265 if(NS_FAILED(nsres))
1266 ERR("SetCommandContext failed: %08x\n", nsres);
1267 nsres = nsIControllerContext_QueryInterface(ctrlctx, &IID_nsIController,
1268 (void**)&This->editor_controller);
1269 nsIControllerContext_Release(ctrlctx);
1270 if(NS_FAILED(nsres))
1271 ERR("Could not get nsIController interface: %08x\n", nsres);
1272 }else {
1273 ERR("Could not create edit controller: %08x\n", nsres);
1277 void close_gecko(void)
1279 TRACE("()\n");
1281 release_nsio();
1282 init_mutation(NULL);
1284 if(profile_directory) {
1285 nsIFile_Release(profile_directory);
1286 profile_directory = NULL;
1289 if(plugin_directory) {
1290 nsIFile_Release(plugin_directory);
1291 plugin_directory = NULL;
1294 if(pCompMgr)
1295 nsIComponentManager_Release(pCompMgr);
1297 if(pServMgr)
1298 nsIServiceManager_Release(pServMgr);
1300 if(cat_mgr)
1301 nsICategoryManager_Release(cat_mgr);
1303 /* Gecko doesn't really support being unloaded */
1304 /* if (hXPCOM) FreeLibrary(hXPCOM); */
1306 DeleteCriticalSection(&cs_load_gecko);
1309 BOOL is_gecko_path(const char *path)
1311 WCHAR *buf, *ptr;
1312 BOOL ret;
1314 buf = heap_strdupUtoW(path);
1315 if(!buf || lstrlenW(buf) < gecko_path_len)
1316 return FALSE;
1318 for(ptr = buf; *ptr; ptr++) {
1319 if(*ptr == '\\')
1320 *ptr = '/';
1323 UrlUnescapeW(buf, NULL, NULL, URL_UNESCAPE_INPLACE);
1324 buf[gecko_path_len] = 0;
1326 ret = !wcsicmp(buf, gecko_path);
1327 heap_free(buf);
1328 return ret;
1331 void set_viewer_zoom(GeckoBrowser *browser, float factor)
1333 nsIContentViewer *content_viewer;
1334 nsIDocShell *doc_shell;
1335 nsresult nsres;
1337 TRACE("Setting to %f\n", factor);
1339 nsres = get_nsinterface((nsISupports*)browser->navigation, &IID_nsIDocShell, (void**)&doc_shell);
1340 assert(nsres == NS_OK);
1342 nsres = nsIDocShell_GetContentViewer(doc_shell, &content_viewer);
1343 assert(nsres == NS_OK && content_viewer);
1344 nsIDocShell_Release(doc_shell);
1346 nsres = nsIContentViewer_SetFullZoom(content_viewer, factor);
1347 if(NS_FAILED(nsres))
1348 ERR("SetFullZoom failed: %08x\n", nsres);
1350 nsIContentViewer_Release(content_viewer);
1353 float get_viewer_zoom(GeckoBrowser *browser)
1355 nsIContentViewer *content_viewer;
1356 nsIDocShell *doc_shell;
1357 nsresult nsres;
1358 float factor;
1360 nsres = get_nsinterface((nsISupports*)browser->navigation, &IID_nsIDocShell, (void**)&doc_shell);
1361 assert(nsres == NS_OK);
1363 nsres = nsIDocShell_GetContentViewer(doc_shell, &content_viewer);
1364 assert(nsres == NS_OK && content_viewer);
1365 nsIDocShell_Release(doc_shell);
1367 nsres = nsIContentViewer_GetFullZoom(content_viewer, &factor);
1368 if(NS_FAILED(nsres))
1369 ERR("GetFullZoom failed: %08x\n", nsres);
1370 TRACE("Got %f\n", factor);
1372 nsIContentViewer_Release(content_viewer);
1373 return factor;
1376 struct nsWeakReference {
1377 nsIWeakReference nsIWeakReference_iface;
1379 LONG ref;
1381 GeckoBrowser *browser;
1384 static inline nsWeakReference *impl_from_nsIWeakReference(nsIWeakReference *iface)
1386 return CONTAINING_RECORD(iface, nsWeakReference, nsIWeakReference_iface);
1389 static nsresult NSAPI nsWeakReference_QueryInterface(nsIWeakReference *iface,
1390 nsIIDRef riid, void **result)
1392 nsWeakReference *This = impl_from_nsIWeakReference(iface);
1394 if(IsEqualGUID(&IID_nsISupports, riid)) {
1395 TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
1396 *result = &This->nsIWeakReference_iface;
1397 }else if(IsEqualGUID(&IID_nsIWeakReference, riid)) {
1398 TRACE("(%p)->(IID_nsIWeakReference %p)\n", This, result);
1399 *result = &This->nsIWeakReference_iface;
1400 }else {
1401 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
1402 *result = NULL;
1403 return NS_NOINTERFACE;
1406 nsISupports_AddRef((nsISupports*)*result);
1407 return NS_OK;
1410 static nsrefcnt NSAPI nsWeakReference_AddRef(nsIWeakReference *iface)
1412 nsWeakReference *This = impl_from_nsIWeakReference(iface);
1413 LONG ref = InterlockedIncrement(&This->ref);
1415 TRACE("(%p) ref=%d\n", This, ref);
1417 return ref;
1420 static nsrefcnt NSAPI nsWeakReference_Release(nsIWeakReference *iface)
1422 nsWeakReference *This = impl_from_nsIWeakReference(iface);
1423 LONG ref = InterlockedDecrement(&This->ref);
1425 TRACE("(%p) ref=%d\n", This, ref);
1427 if(!ref) {
1428 assert(!This->browser);
1429 heap_free(This);
1432 return ref;
1435 static nsresult NSAPI nsWeakReference_QueryReferent(nsIWeakReference *iface,
1436 const nsIID *riid, void **result)
1438 nsWeakReference *This = impl_from_nsIWeakReference(iface);
1440 if(!This->browser)
1441 return NS_ERROR_NULL_POINTER;
1443 return nsIWebBrowserChrome_QueryInterface(&This->browser->nsIWebBrowserChrome_iface, riid, result);
1446 static const nsIWeakReferenceVtbl nsWeakReferenceVtbl = {
1447 nsWeakReference_QueryInterface,
1448 nsWeakReference_AddRef,
1449 nsWeakReference_Release,
1450 nsWeakReference_QueryReferent
1453 /**********************************************************
1454 * nsIWebBrowserChrome interface
1457 static inline GeckoBrowser *impl_from_nsIWebBrowserChrome(nsIWebBrowserChrome *iface)
1459 return CONTAINING_RECORD(iface, GeckoBrowser, nsIWebBrowserChrome_iface);
1462 static nsresult NSAPI nsWebBrowserChrome_QueryInterface(nsIWebBrowserChrome *iface,
1463 nsIIDRef riid, void **result)
1465 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1467 *result = NULL;
1468 if(IsEqualGUID(&IID_nsISupports, riid)) {
1469 TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
1470 *result = &This->nsIWebBrowserChrome_iface;
1471 }else if(IsEqualGUID(&IID_nsIWebBrowserChrome, riid)) {
1472 TRACE("(%p)->(IID_nsIWebBrowserChrome, %p)\n", This, result);
1473 *result = &This->nsIWebBrowserChrome_iface;
1474 }else if(IsEqualGUID(&IID_nsIContextMenuListener, riid)) {
1475 TRACE("(%p)->(IID_nsIContextMenuListener, %p)\n", This, result);
1476 *result = &This->nsIContextMenuListener_iface;
1477 }else if(IsEqualGUID(&IID_nsIURIContentListener, riid)) {
1478 TRACE("(%p)->(IID_nsIURIContentListener %p)\n", This, result);
1479 *result = &This->nsIURIContentListener_iface;
1480 }else if(IsEqualGUID(&IID_nsIEmbeddingSiteWindow, riid)) {
1481 TRACE("(%p)->(IID_nsIEmbeddingSiteWindow %p)\n", This, result);
1482 *result = &This->nsIEmbeddingSiteWindow_iface;
1483 }else if(IsEqualGUID(&IID_nsITooltipListener, riid)) {
1484 TRACE("(%p)->(IID_nsITooltipListener %p)\n", This, result);
1485 *result = &This->nsITooltipListener_iface;
1486 }else if(IsEqualGUID(&IID_nsIInterfaceRequestor, riid)) {
1487 TRACE("(%p)->(IID_nsIInterfaceRequestor %p)\n", This, result);
1488 *result = &This->nsIInterfaceRequestor_iface;
1489 }else if(IsEqualGUID(&IID_nsISupportsWeakReference, riid)) {
1490 TRACE("(%p)->(IID_nsISupportsWeakReference %p)\n", This, result);
1491 *result = &This->nsISupportsWeakReference_iface;
1494 if(*result) {
1495 nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1496 return NS_OK;
1499 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
1500 return NS_NOINTERFACE;
1503 static nsrefcnt NSAPI nsWebBrowserChrome_AddRef(nsIWebBrowserChrome *iface)
1505 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1506 LONG ref = InterlockedIncrement(&This->ref);
1508 TRACE("(%p) ref=%d\n", This, ref);
1510 return ref;
1513 static nsrefcnt NSAPI nsWebBrowserChrome_Release(nsIWebBrowserChrome *iface)
1515 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1516 LONG ref = InterlockedDecrement(&This->ref);
1518 TRACE("(%p) ref=%d\n", This, ref);
1520 if(!ref) {
1521 if(This->doc)
1522 detach_gecko_browser(This);
1523 if(This->weak_reference) {
1524 This->weak_reference->browser = NULL;
1525 nsIWeakReference_Release(&This->weak_reference->nsIWeakReference_iface);
1527 heap_free(This);
1530 return ref;
1533 static nsresult NSAPI nsWebBrowserChrome_SetStatus(nsIWebBrowserChrome *iface,
1534 UINT32 statusType, const PRUnichar *status)
1536 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1537 TRACE("(%p)->(%d %s)\n", This, statusType, debugstr_w(status));
1538 return NS_OK;
1541 static nsresult NSAPI nsWebBrowserChrome_GetWebBrowser(nsIWebBrowserChrome *iface,
1542 nsIWebBrowser **aWebBrowser)
1544 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1546 TRACE("(%p)->(%p)\n", This, aWebBrowser);
1548 if(!aWebBrowser)
1549 return NS_ERROR_INVALID_ARG;
1551 if(This->webbrowser)
1552 nsIWebBrowser_AddRef(This->webbrowser);
1553 *aWebBrowser = This->webbrowser;
1554 return S_OK;
1557 static nsresult NSAPI nsWebBrowserChrome_SetWebBrowser(nsIWebBrowserChrome *iface,
1558 nsIWebBrowser *aWebBrowser)
1560 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1562 TRACE("(%p)->(%p)\n", This, aWebBrowser);
1564 if(aWebBrowser != This->webbrowser)
1565 ERR("Wrong nsWebBrowser!\n");
1567 return NS_OK;
1570 static nsresult NSAPI nsWebBrowserChrome_GetChromeFlags(nsIWebBrowserChrome *iface,
1571 UINT32 *aChromeFlags)
1573 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1574 WARN("(%p)->(%p)\n", This, aChromeFlags);
1575 return NS_ERROR_NOT_IMPLEMENTED;
1578 static nsresult NSAPI nsWebBrowserChrome_SetChromeFlags(nsIWebBrowserChrome *iface,
1579 UINT32 aChromeFlags)
1581 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1582 WARN("(%p)->(%08x)\n", This, aChromeFlags);
1583 return NS_ERROR_NOT_IMPLEMENTED;
1586 static nsresult NSAPI nsWebBrowserChrome_DestroyBrowserWindow(nsIWebBrowserChrome *iface)
1588 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1589 TRACE("(%p)\n", This);
1590 return NS_ERROR_NOT_IMPLEMENTED;
1593 static nsresult NSAPI nsWebBrowserChrome_SizeBrowserTo(nsIWebBrowserChrome *iface,
1594 LONG aCX, LONG aCY)
1596 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1597 WARN("(%p)->(%d %d)\n", This, aCX, aCY);
1598 return NS_ERROR_NOT_IMPLEMENTED;
1601 static nsresult NSAPI nsWebBrowserChrome_ShowAsModal(nsIWebBrowserChrome *iface)
1603 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1604 WARN("(%p)\n", This);
1605 return NS_ERROR_NOT_IMPLEMENTED;
1608 static nsresult NSAPI nsWebBrowserChrome_IsWindowModal(nsIWebBrowserChrome *iface, cpp_bool *_retval)
1610 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1611 WARN("(%p)->(%p)\n", This, _retval);
1612 return NS_ERROR_NOT_IMPLEMENTED;
1615 static nsresult NSAPI nsWebBrowserChrome_ExitModalEventLoop(nsIWebBrowserChrome *iface,
1616 nsresult aStatus)
1618 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1619 WARN("(%p)->(%08x)\n", This, aStatus);
1620 return NS_ERROR_NOT_IMPLEMENTED;
1623 static const nsIWebBrowserChromeVtbl nsWebBrowserChromeVtbl = {
1624 nsWebBrowserChrome_QueryInterface,
1625 nsWebBrowserChrome_AddRef,
1626 nsWebBrowserChrome_Release,
1627 nsWebBrowserChrome_SetStatus,
1628 nsWebBrowserChrome_GetWebBrowser,
1629 nsWebBrowserChrome_SetWebBrowser,
1630 nsWebBrowserChrome_GetChromeFlags,
1631 nsWebBrowserChrome_SetChromeFlags,
1632 nsWebBrowserChrome_DestroyBrowserWindow,
1633 nsWebBrowserChrome_SizeBrowserTo,
1634 nsWebBrowserChrome_ShowAsModal,
1635 nsWebBrowserChrome_IsWindowModal,
1636 nsWebBrowserChrome_ExitModalEventLoop
1639 /**********************************************************
1640 * nsIContextMenuListener interface
1643 static inline GeckoBrowser *impl_from_nsIContextMenuListener(nsIContextMenuListener *iface)
1645 return CONTAINING_RECORD(iface, GeckoBrowser, nsIContextMenuListener_iface);
1648 static nsresult NSAPI nsContextMenuListener_QueryInterface(nsIContextMenuListener *iface,
1649 nsIIDRef riid, void **result)
1651 GeckoBrowser *This = impl_from_nsIContextMenuListener(iface);
1652 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1655 static nsrefcnt NSAPI nsContextMenuListener_AddRef(nsIContextMenuListener *iface)
1657 GeckoBrowser *This = impl_from_nsIContextMenuListener(iface);
1658 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1661 static nsrefcnt NSAPI nsContextMenuListener_Release(nsIContextMenuListener *iface)
1663 GeckoBrowser *This = impl_from_nsIContextMenuListener(iface);
1664 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
1667 static nsresult NSAPI nsContextMenuListener_OnShowContextMenu(nsIContextMenuListener *iface,
1668 UINT32 aContextFlags, nsIDOMEvent *aEvent, nsIDOMNode *aNode)
1670 GeckoBrowser *This = impl_from_nsIContextMenuListener(iface);
1671 nsIDOMMouseEvent *mouse_event;
1672 HTMLDOMNode *node;
1673 DOMEvent *event;
1674 POINT pt;
1675 DWORD dwID = CONTEXT_MENU_DEFAULT;
1676 nsresult nsres;
1677 HRESULT hres;
1679 TRACE("(%p)->(%08x %p %p)\n", This, aContextFlags, aEvent, aNode);
1681 if (!aNode)
1682 return NS_ERROR_FAILURE;
1684 hres = get_node(aNode, TRUE, &node);
1685 if(FAILED(hres))
1686 return NS_ERROR_FAILURE;
1688 hres = create_event_from_nsevent(aEvent, &event);
1689 if(SUCCEEDED(hres)) {
1690 dispatch_event(&node->event_target, event);
1691 IDOMEvent_Release(&event->IDOMEvent_iface);
1694 nsres = nsIDOMEvent_QueryInterface(aEvent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
1695 assert(NS_SUCCEEDED(nsres));
1697 nsIDOMMouseEvent_GetScreenX(mouse_event, &pt.x);
1698 nsIDOMMouseEvent_GetScreenY(mouse_event, &pt.y);
1699 nsIDOMMouseEvent_Release(mouse_event);
1701 switch(aContextFlags) {
1702 case CONTEXT_NONE:
1703 case CONTEXT_DOCUMENT:
1704 case CONTEXT_TEXT: {
1705 nsISelection *selection;
1707 nsres = nsIDOMHTMLDocument_GetSelection(This->doc->basedoc.doc_node->nsdoc, &selection);
1708 if(NS_SUCCEEDED(nsres) && selection) {
1709 cpp_bool is_collapsed;
1711 /* FIXME: Check if the click was inside selection. */
1712 nsres = nsISelection_GetIsCollapsed(selection, &is_collapsed);
1713 nsISelection_Release(selection);
1714 if(NS_SUCCEEDED(nsres) && !is_collapsed)
1715 dwID = CONTEXT_MENU_TEXTSELECT;
1717 break;
1719 case CONTEXT_IMAGE:
1720 case CONTEXT_IMAGE|CONTEXT_LINK:
1721 dwID = CONTEXT_MENU_IMAGE;
1722 break;
1723 case CONTEXT_LINK:
1724 dwID = CONTEXT_MENU_ANCHOR;
1725 break;
1726 case CONTEXT_INPUT:
1727 dwID = CONTEXT_MENU_CONTROL;
1728 break;
1729 default:
1730 FIXME("aContextFlags=%08x\n", aContextFlags);
1733 show_context_menu(This->doc, dwID, &pt, (IDispatch*)&node->IHTMLDOMNode_iface);
1734 node_release(node);
1735 return NS_OK;
1738 static const nsIContextMenuListenerVtbl nsContextMenuListenerVtbl = {
1739 nsContextMenuListener_QueryInterface,
1740 nsContextMenuListener_AddRef,
1741 nsContextMenuListener_Release,
1742 nsContextMenuListener_OnShowContextMenu
1745 /**********************************************************
1746 * nsIURIContentListener interface
1749 static inline GeckoBrowser *impl_from_nsIURIContentListener(nsIURIContentListener *iface)
1751 return CONTAINING_RECORD(iface, GeckoBrowser, nsIURIContentListener_iface);
1754 static nsresult NSAPI nsURIContentListener_QueryInterface(nsIURIContentListener *iface,
1755 nsIIDRef riid, void **result)
1757 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1758 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1761 static nsrefcnt NSAPI nsURIContentListener_AddRef(nsIURIContentListener *iface)
1763 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1764 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1767 static nsrefcnt NSAPI nsURIContentListener_Release(nsIURIContentListener *iface)
1769 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1770 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
1773 static nsresult NSAPI nsURIContentListener_OnStartURIOpen(nsIURIContentListener *iface,
1774 nsIURI *aURI, cpp_bool *_retval)
1776 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1777 nsACString spec_str;
1778 const char *spec;
1780 nsACString_Init(&spec_str, NULL);
1781 nsIURI_GetSpec(aURI, &spec_str);
1782 nsACString_GetData(&spec_str, &spec);
1784 TRACE("(%p)->(%p(%s) %p)\n", This, aURI, debugstr_a(spec), _retval);
1786 nsACString_Finish(&spec_str);
1788 return This->content_listener
1789 ? nsIURIContentListener_OnStartURIOpen(This->content_listener, aURI, _retval)
1790 : NS_OK;
1793 static nsresult NSAPI nsURIContentListener_DoContent(nsIURIContentListener *iface,
1794 const nsACString *aContentType, cpp_bool aIsContentPreferred, nsIRequest *aRequest,
1795 nsIStreamListener **aContentHandler, cpp_bool *_retval)
1797 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1799 TRACE("(%p)->(%p %x %p %p %p)\n", This, aContentType, aIsContentPreferred,
1800 aRequest, aContentHandler, _retval);
1802 return This->content_listener
1803 ? nsIURIContentListener_DoContent(This->content_listener, aContentType,
1804 aIsContentPreferred, aRequest, aContentHandler, _retval)
1805 : NS_ERROR_NOT_IMPLEMENTED;
1808 static nsresult NSAPI nsURIContentListener_IsPreferred(nsIURIContentListener *iface,
1809 const char *aContentType, char **aDesiredContentType, cpp_bool *_retval)
1811 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1813 TRACE("(%p)->(%s %p %p)\n", This, debugstr_a(aContentType), aDesiredContentType, _retval);
1815 /* FIXME: Should we do something here? */
1816 *_retval = TRUE;
1818 return This->content_listener
1819 ? nsIURIContentListener_IsPreferred(This->content_listener, aContentType,
1820 aDesiredContentType, _retval)
1821 : NS_OK;
1824 static nsresult NSAPI nsURIContentListener_CanHandleContent(nsIURIContentListener *iface,
1825 const char *aContentType, cpp_bool aIsContentPreferred, char **aDesiredContentType,
1826 cpp_bool *_retval)
1828 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1830 TRACE("(%p)->(%s %x %p %p)\n", This, debugstr_a(aContentType), aIsContentPreferred,
1831 aDesiredContentType, _retval);
1833 return This->content_listener
1834 ? nsIURIContentListener_CanHandleContent(This->content_listener, aContentType,
1835 aIsContentPreferred, aDesiredContentType, _retval)
1836 : NS_ERROR_NOT_IMPLEMENTED;
1839 static nsresult NSAPI nsURIContentListener_GetLoadCookie(nsIURIContentListener *iface,
1840 nsISupports **aLoadCookie)
1842 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1844 WARN("(%p)->(%p)\n", This, aLoadCookie);
1846 return This->content_listener
1847 ? nsIURIContentListener_GetLoadCookie(This->content_listener, aLoadCookie)
1848 : NS_ERROR_NOT_IMPLEMENTED;
1851 static nsresult NSAPI nsURIContentListener_SetLoadCookie(nsIURIContentListener *iface,
1852 nsISupports *aLoadCookie)
1854 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1856 WARN("(%p)->(%p)\n", This, aLoadCookie);
1858 return This->content_listener
1859 ? nsIURIContentListener_SetLoadCookie(This->content_listener, aLoadCookie)
1860 : NS_ERROR_NOT_IMPLEMENTED;
1863 static nsresult NSAPI nsURIContentListener_GetParentContentListener(nsIURIContentListener *iface,
1864 nsIURIContentListener **aParentContentListener)
1866 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1868 TRACE("(%p)->(%p)\n", This, aParentContentListener);
1870 if(This->content_listener)
1871 nsIURIContentListener_AddRef(This->content_listener);
1873 *aParentContentListener = This->content_listener;
1874 return NS_OK;
1877 static nsresult NSAPI nsURIContentListener_SetParentContentListener(nsIURIContentListener *iface,
1878 nsIURIContentListener *aParentContentListener)
1880 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1882 TRACE("(%p)->(%p)\n", This, aParentContentListener);
1884 if(aParentContentListener == &This->nsIURIContentListener_iface)
1885 return NS_OK;
1887 if(This->content_listener)
1888 nsIURIContentListener_Release(This->content_listener);
1890 This->content_listener = aParentContentListener;
1891 if(This->content_listener)
1892 nsIURIContentListener_AddRef(This->content_listener);
1894 return NS_OK;
1897 static const nsIURIContentListenerVtbl nsURIContentListenerVtbl = {
1898 nsURIContentListener_QueryInterface,
1899 nsURIContentListener_AddRef,
1900 nsURIContentListener_Release,
1901 nsURIContentListener_OnStartURIOpen,
1902 nsURIContentListener_DoContent,
1903 nsURIContentListener_IsPreferred,
1904 nsURIContentListener_CanHandleContent,
1905 nsURIContentListener_GetLoadCookie,
1906 nsURIContentListener_SetLoadCookie,
1907 nsURIContentListener_GetParentContentListener,
1908 nsURIContentListener_SetParentContentListener
1911 /**********************************************************
1912 * nsIEmbeddinSiteWindow interface
1915 static inline GeckoBrowser *impl_from_nsIEmbeddingSiteWindow(nsIEmbeddingSiteWindow *iface)
1917 return CONTAINING_RECORD(iface, GeckoBrowser, nsIEmbeddingSiteWindow_iface);
1920 static nsresult NSAPI nsEmbeddingSiteWindow_QueryInterface(nsIEmbeddingSiteWindow *iface,
1921 nsIIDRef riid, void **result)
1923 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1924 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1927 static nsrefcnt NSAPI nsEmbeddingSiteWindow_AddRef(nsIEmbeddingSiteWindow *iface)
1929 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1930 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1933 static nsrefcnt NSAPI nsEmbeddingSiteWindow_Release(nsIEmbeddingSiteWindow *iface)
1935 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1936 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
1939 static nsresult NSAPI nsEmbeddingSiteWindow_SetDimensions(nsIEmbeddingSiteWindow *iface,
1940 UINT32 flags, LONG x, LONG y, LONG cx, LONG cy)
1942 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1943 WARN("(%p)->(%08x %d %d %d %d)\n", This, flags, x, y, cx, cy);
1944 return NS_ERROR_NOT_IMPLEMENTED;
1947 static nsresult NSAPI nsEmbeddingSiteWindow_GetDimensions(nsIEmbeddingSiteWindow *iface,
1948 UINT32 flags, LONG *x, LONG *y, LONG *cx, LONG *cy)
1950 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1951 RECT r;
1953 TRACE("(%p)->(%x %p %p %p %p)\n", This, flags, x, y, cx, cy);
1955 if(!GetWindowRect(This->hwnd, &r)) {
1956 ERR("GetWindowRect failed\n");
1957 return NS_ERROR_FAILURE;
1960 if(x)
1961 *x = r.left;
1962 if(y)
1963 *y = r.top;
1964 if(cx)
1965 *cx = r.right-r.left;
1966 if(cy)
1967 *cy = r.bottom-r.top;
1968 return NS_OK;
1971 static nsresult NSAPI nsEmbeddingSiteWindow_SetFocus(nsIEmbeddingSiteWindow *iface)
1973 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1975 TRACE("(%p)\n", This);
1977 return nsIBaseWindow_SetFocus(This->window);
1980 static nsresult NSAPI nsEmbeddingSiteWindow_GetVisibility(nsIEmbeddingSiteWindow *iface,
1981 cpp_bool *aVisibility)
1983 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1985 TRACE("(%p)->(%p)\n", This, aVisibility);
1987 *aVisibility = This->doc && This->doc->hwnd && IsWindowVisible(This->doc->hwnd);
1988 return NS_OK;
1991 static nsresult NSAPI nsEmbeddingSiteWindow_SetVisibility(nsIEmbeddingSiteWindow *iface,
1992 cpp_bool aVisibility)
1994 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1996 TRACE("(%p)->(%x)\n", This, aVisibility);
1998 return NS_OK;
2001 static nsresult NSAPI nsEmbeddingSiteWindow_GetTitle(nsIEmbeddingSiteWindow *iface,
2002 PRUnichar **aTitle)
2004 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
2005 WARN("(%p)->(%p)\n", This, aTitle);
2006 return NS_ERROR_NOT_IMPLEMENTED;
2009 static nsresult NSAPI nsEmbeddingSiteWindow_SetTitle(nsIEmbeddingSiteWindow *iface,
2010 const PRUnichar *aTitle)
2012 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
2013 WARN("(%p)->(%s)\n", This, debugstr_w(aTitle));
2014 return NS_ERROR_NOT_IMPLEMENTED;
2017 static nsresult NSAPI nsEmbeddingSiteWindow_GetSiteWindow(nsIEmbeddingSiteWindow *iface,
2018 void **aSiteWindow)
2020 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
2022 TRACE("(%p)->(%p)\n", This, aSiteWindow);
2024 *aSiteWindow = This->hwnd;
2025 return NS_OK;
2028 static const nsIEmbeddingSiteWindowVtbl nsEmbeddingSiteWindowVtbl = {
2029 nsEmbeddingSiteWindow_QueryInterface,
2030 nsEmbeddingSiteWindow_AddRef,
2031 nsEmbeddingSiteWindow_Release,
2032 nsEmbeddingSiteWindow_SetDimensions,
2033 nsEmbeddingSiteWindow_GetDimensions,
2034 nsEmbeddingSiteWindow_SetFocus,
2035 nsEmbeddingSiteWindow_GetVisibility,
2036 nsEmbeddingSiteWindow_SetVisibility,
2037 nsEmbeddingSiteWindow_GetTitle,
2038 nsEmbeddingSiteWindow_SetTitle,
2039 nsEmbeddingSiteWindow_GetSiteWindow
2042 static inline GeckoBrowser *impl_from_nsITooltipListener(nsITooltipListener *iface)
2044 return CONTAINING_RECORD(iface, GeckoBrowser, nsITooltipListener_iface);
2047 static nsresult NSAPI nsTooltipListener_QueryInterface(nsITooltipListener *iface, nsIIDRef riid,
2048 void **result)
2050 GeckoBrowser *This = impl_from_nsITooltipListener(iface);
2051 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
2054 static nsrefcnt NSAPI nsTooltipListener_AddRef(nsITooltipListener *iface)
2056 GeckoBrowser *This = impl_from_nsITooltipListener(iface);
2057 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
2060 static nsrefcnt NSAPI nsTooltipListener_Release(nsITooltipListener *iface)
2062 GeckoBrowser *This = impl_from_nsITooltipListener(iface);
2063 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
2066 static nsresult NSAPI nsTooltipListener_OnShowTooltip(nsITooltipListener *iface,
2067 LONG aXCoord, LONG aYCoord, const PRUnichar *aTipText)
2069 GeckoBrowser *This = impl_from_nsITooltipListener(iface);
2071 if (This->doc)
2072 show_tooltip(This->doc, aXCoord, aYCoord, aTipText);
2074 return NS_OK;
2077 static nsresult NSAPI nsTooltipListener_OnHideTooltip(nsITooltipListener *iface)
2079 GeckoBrowser *This = impl_from_nsITooltipListener(iface);
2081 if (This->doc)
2082 hide_tooltip(This->doc);
2084 return NS_OK;
2087 static const nsITooltipListenerVtbl nsTooltipListenerVtbl = {
2088 nsTooltipListener_QueryInterface,
2089 nsTooltipListener_AddRef,
2090 nsTooltipListener_Release,
2091 nsTooltipListener_OnShowTooltip,
2092 nsTooltipListener_OnHideTooltip
2095 static inline GeckoBrowser *impl_from_nsIInterfaceRequestor(nsIInterfaceRequestor *iface)
2097 return CONTAINING_RECORD(iface, GeckoBrowser, nsIInterfaceRequestor_iface);
2100 static nsresult NSAPI nsInterfaceRequestor_QueryInterface(nsIInterfaceRequestor *iface,
2101 nsIIDRef riid, void **result)
2103 GeckoBrowser *This = impl_from_nsIInterfaceRequestor(iface);
2104 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
2107 static nsrefcnt NSAPI nsInterfaceRequestor_AddRef(nsIInterfaceRequestor *iface)
2109 GeckoBrowser *This = impl_from_nsIInterfaceRequestor(iface);
2110 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
2113 static nsrefcnt NSAPI nsInterfaceRequestor_Release(nsIInterfaceRequestor *iface)
2115 GeckoBrowser *This = impl_from_nsIInterfaceRequestor(iface);
2116 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
2119 static nsresult NSAPI nsInterfaceRequestor_GetInterface(nsIInterfaceRequestor *iface,
2120 nsIIDRef riid, void **result)
2122 GeckoBrowser *This = impl_from_nsIInterfaceRequestor(iface);
2124 if(IsEqualGUID(&IID_mozIDOMWindowProxy, riid)) {
2125 TRACE("(%p)->(IID_nsIDOMWindow %p)\n", This, result);
2126 return nsIWebBrowser_GetContentDOMWindow(This->webbrowser, (mozIDOMWindowProxy**)result);
2129 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
2132 static const nsIInterfaceRequestorVtbl nsInterfaceRequestorVtbl = {
2133 nsInterfaceRequestor_QueryInterface,
2134 nsInterfaceRequestor_AddRef,
2135 nsInterfaceRequestor_Release,
2136 nsInterfaceRequestor_GetInterface
2139 static inline GeckoBrowser *impl_from_nsISupportsWeakReference(nsISupportsWeakReference *iface)
2141 return CONTAINING_RECORD(iface, GeckoBrowser, nsISupportsWeakReference_iface);
2144 static nsresult NSAPI nsSupportsWeakReference_QueryInterface(nsISupportsWeakReference *iface,
2145 nsIIDRef riid, void **result)
2147 GeckoBrowser *This = impl_from_nsISupportsWeakReference(iface);
2148 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
2151 static nsrefcnt NSAPI nsSupportsWeakReference_AddRef(nsISupportsWeakReference *iface)
2153 GeckoBrowser *This = impl_from_nsISupportsWeakReference(iface);
2154 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
2157 static nsrefcnt NSAPI nsSupportsWeakReference_Release(nsISupportsWeakReference *iface)
2159 GeckoBrowser *This = impl_from_nsISupportsWeakReference(iface);
2160 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
2163 static nsresult NSAPI nsSupportsWeakReference_GetWeakReference(nsISupportsWeakReference *iface,
2164 nsIWeakReference **_retval)
2166 GeckoBrowser *This = impl_from_nsISupportsWeakReference(iface);
2168 TRACE("(%p)->(%p)\n", This, _retval);
2170 if(!This->weak_reference) {
2171 This->weak_reference = heap_alloc(sizeof(nsWeakReference));
2172 if(!This->weak_reference)
2173 return NS_ERROR_OUT_OF_MEMORY;
2175 This->weak_reference->nsIWeakReference_iface.lpVtbl = &nsWeakReferenceVtbl;
2176 This->weak_reference->ref = 1;
2177 This->weak_reference->browser = This;
2180 *_retval = &This->weak_reference->nsIWeakReference_iface;
2181 nsIWeakReference_AddRef(*_retval);
2182 return NS_OK;
2185 static const nsISupportsWeakReferenceVtbl nsSupportsWeakReferenceVtbl = {
2186 nsSupportsWeakReference_QueryInterface,
2187 nsSupportsWeakReference_AddRef,
2188 nsSupportsWeakReference_Release,
2189 nsSupportsWeakReference_GetWeakReference
2192 static HRESULT init_browser(GeckoBrowser *browser)
2194 mozIDOMWindowProxy *mozwindow;
2195 nsIWebBrowserSetup *wbsetup;
2196 nsIScrollable *scrollable;
2197 nsresult nsres;
2198 HRESULT hres;
2200 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_WEBBROWSER_CONTRACTID,
2201 NULL, &IID_nsIWebBrowser, (void**)&browser->webbrowser);
2202 if(NS_FAILED(nsres)) {
2203 ERR("Creating WebBrowser failed: %08x\n", nsres);
2204 return E_FAIL;
2207 nsres = nsIWebBrowser_SetContainerWindow(browser->webbrowser, &browser->nsIWebBrowserChrome_iface);
2208 if(NS_FAILED(nsres)) {
2209 ERR("SetContainerWindow failed: %08x\n", nsres);
2210 return E_FAIL;
2213 nsres = nsIWebBrowser_QueryInterface(browser->webbrowser, &IID_nsIBaseWindow,
2214 (void**)&browser->window);
2215 if(NS_FAILED(nsres)) {
2216 ERR("Could not get nsIBaseWindow interface: %08x\n", nsres);
2217 return E_FAIL;
2220 nsres = nsIWebBrowser_QueryInterface(browser->webbrowser, &IID_nsIWebBrowserSetup,
2221 (void**)&wbsetup);
2222 if(NS_SUCCEEDED(nsres)) {
2223 nsres = nsIWebBrowserSetup_SetProperty(wbsetup, SETUP_IS_CHROME_WRAPPER, FALSE);
2224 nsIWebBrowserSetup_Release(wbsetup);
2225 if(NS_FAILED(nsres)) {
2226 ERR("SetProperty(SETUP_IS_CHROME_WRAPPER) failed: %08x\n", nsres);
2227 return E_FAIL;
2229 }else {
2230 ERR("Could not get nsIWebBrowserSetup interface\n");
2231 return E_FAIL;
2234 nsres = nsIWebBrowser_QueryInterface(browser->webbrowser, &IID_nsIWebNavigation,
2235 (void**)&browser->navigation);
2236 if(NS_FAILED(nsres)) {
2237 ERR("Could not get nsIWebNavigation interface: %08x\n", nsres);
2238 return E_FAIL;
2241 nsres = nsIWebBrowser_QueryInterface(browser->webbrowser, &IID_nsIWebBrowserFocus,
2242 (void**)&browser->focus);
2243 if(NS_FAILED(nsres)) {
2244 ERR("Could not get nsIWebBrowserFocus interface: %08x\n", nsres);
2245 return E_FAIL;
2248 if(!browser_class) {
2249 register_browser_class();
2250 if(!browser_class)
2251 return E_FAIL;
2254 browser->hwnd = CreateWindowExW(0, wszNsContainer, NULL,
2255 WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 0, 0, 100, 100,
2256 GetDesktopWindow(), NULL, hInst, browser);
2257 if(!browser->hwnd) {
2258 WARN("Could not create window\n");
2259 return E_FAIL;
2262 nsres = nsIBaseWindow_InitWindow(browser->window, browser->hwnd, NULL, 0, 0, 100, 100);
2263 if(NS_SUCCEEDED(nsres)) {
2264 nsres = nsIBaseWindow_Create(browser->window);
2265 if(NS_FAILED(nsres)) {
2266 WARN("Creating window failed: %08x\n", nsres);
2267 return E_FAIL;
2270 nsIBaseWindow_SetVisibility(browser->window, FALSE);
2271 nsIBaseWindow_SetEnabled(browser->window, FALSE);
2272 }else {
2273 ERR("InitWindow failed: %08x\n", nsres);
2274 return E_FAIL;
2277 nsres = nsIWebBrowser_SetParentURIContentListener(browser->webbrowser,
2278 &browser->nsIURIContentListener_iface);
2279 if(NS_FAILED(nsres))
2280 ERR("SetParentURIContentListener failed: %08x\n", nsres);
2282 nsres = nsIWebBrowser_QueryInterface(browser->webbrowser, &IID_nsIScrollable, (void**)&scrollable);
2283 if(NS_SUCCEEDED(nsres)) {
2284 nsres = nsIScrollable_SetDefaultScrollbarPreferences(scrollable,
2285 ScrollOrientation_Y, Scrollbar_Always);
2286 if(NS_FAILED(nsres))
2287 ERR("Could not set default Y scrollbar prefs: %08x\n", nsres);
2289 nsres = nsIScrollable_SetDefaultScrollbarPreferences(scrollable,
2290 ScrollOrientation_X, Scrollbar_Auto);
2291 if(NS_FAILED(nsres))
2292 ERR("Could not set default X scrollbar prefs: %08x\n", nsres);
2294 nsIScrollable_Release(scrollable);
2295 }else {
2296 ERR("Could not get nsIScrollable: %08x\n", nsres);
2299 nsres = nsIWebBrowser_GetContentDOMWindow(browser->webbrowser, &mozwindow);
2300 if(NS_FAILED(nsres)) {
2301 ERR("GetContentDOMWindow failed: %08x\n", nsres);
2302 return E_FAIL;
2305 hres = create_outer_window(browser, mozwindow, NULL, &browser->content_window);
2306 mozIDOMWindowProxy_Release(mozwindow);
2307 return hres;
2310 HRESULT create_gecko_browser(HTMLDocumentObj *doc, GeckoBrowser **_ret)
2312 GeckoBrowser *ret;
2313 HRESULT hres;
2315 if(!load_gecko())
2316 return CLASS_E_CLASSNOTAVAILABLE;
2318 ret = heap_alloc_zero(sizeof(GeckoBrowser));
2319 if(!ret)
2320 return E_OUTOFMEMORY;
2322 ret->nsIWebBrowserChrome_iface.lpVtbl = &nsWebBrowserChromeVtbl;
2323 ret->nsIContextMenuListener_iface.lpVtbl = &nsContextMenuListenerVtbl;
2324 ret->nsIURIContentListener_iface.lpVtbl = &nsURIContentListenerVtbl;
2325 ret->nsIEmbeddingSiteWindow_iface.lpVtbl = &nsEmbeddingSiteWindowVtbl;
2326 ret->nsITooltipListener_iface.lpVtbl = &nsTooltipListenerVtbl;
2327 ret->nsIInterfaceRequestor_iface.lpVtbl = &nsInterfaceRequestorVtbl;
2328 ret->nsISupportsWeakReference_iface.lpVtbl = &nsSupportsWeakReferenceVtbl;
2330 ret->doc = doc;
2331 ret->ref = 1;
2332 ret->script_mode = SCRIPTMODE_ACTIVESCRIPT;
2333 ret->usermode = UNKNOWN_USERMODE;
2334 list_init(&ret->document_nodes);
2335 list_init(&ret->outer_windows);
2337 hres = init_browser(ret);
2338 if(SUCCEEDED(hres))
2339 *_ret = ret;
2340 else
2341 nsIWebBrowserChrome_Release(&ret->nsIWebBrowserChrome_iface);
2342 return hres;
2345 void detach_gecko_browser(GeckoBrowser *This)
2347 nsIDOMWindowUtils *window_utils = NULL;
2349 TRACE("(%p)\n", This);
2351 This->doc = NULL;
2353 if(This->content_window) {
2354 get_nsinterface((nsISupports*)This->content_window->nswindow, &IID_nsIDOMWindowUtils, (void**)&window_utils);
2356 IHTMLWindow2_Release(&This->content_window->base.IHTMLWindow2_iface);
2357 This->content_window = NULL;
2360 while(!list_empty(&This->document_nodes)) {
2361 HTMLDocumentNode *doc = LIST_ENTRY(list_head(&This->document_nodes), HTMLDocumentNode, browser_entry);
2362 detach_document_node(doc);
2365 while(!list_empty(&This->outer_windows)) {
2366 HTMLOuterWindow *window = LIST_ENTRY(list_head(&This->outer_windows), HTMLOuterWindow, browser_entry);
2367 list_remove(&window->browser_entry);
2368 window->browser = NULL;
2371 ShowWindow(This->hwnd, SW_HIDE);
2372 SetParent(This->hwnd, NULL);
2374 nsIBaseWindow_SetVisibility(This->window, FALSE);
2375 nsIBaseWindow_Destroy(This->window);
2377 nsIWebBrowser_SetContainerWindow(This->webbrowser, NULL);
2379 nsIWebBrowser_Release(This->webbrowser);
2380 This->webbrowser = NULL;
2382 nsIWebNavigation_Release(This->navigation);
2383 This->navigation = NULL;
2385 nsIBaseWindow_Release(This->window);
2386 This->window = NULL;
2388 nsIWebBrowserFocus_Release(This->focus);
2389 This->focus = NULL;
2391 if(This->editor_controller) {
2392 nsIController_Release(This->editor_controller);
2393 This->editor_controller = NULL;
2396 if(This->editor) {
2397 nsIEditor_Release(This->editor);
2398 This->editor = NULL;
2401 if(This->content_listener) {
2402 nsIURIContentListener_Release(This->content_listener);
2403 This->content_listener = NULL;
2406 if(This->hwnd) {
2407 DestroyWindow(This->hwnd);
2408 This->hwnd = NULL;
2411 nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
2413 /* Force cycle collection */
2414 if(window_utils) {
2415 nsIDOMWindowUtils_CycleCollect(window_utils, NULL, 0);
2416 nsIDOMWindowUtils_Release(window_utils);
2421 * FIXME: nsIScriptObjectPrincipal uses thiscall calling convention, so we need this hack on i386.
2422 * This will be removed after the next Gecko update, that will change calling convention on Gecko side.
2424 #ifdef __i386__
2425 __ASM_GLOBAL_FUNC(call_thiscall_func,
2426 "popl %eax\n\t"
2427 "popl %edx\n\t"
2428 "popl %ecx\n\t"
2429 "pushl %eax\n\t"
2430 "jmp *%edx\n\t")
2431 #define nsIScriptObjectPrincipal_GetPrincipal(this) ((void* (WINAPI*)(void*,void*))&call_thiscall_func)((this)->lpVtbl->GetPrincipal,this)
2432 #endif
2434 nsIXMLHttpRequest *create_nsxhr(nsIDOMWindow *nswindow)
2436 nsIScriptObjectPrincipal *sop;
2437 mozIDOMWindow *inner_window;
2438 nsIPrincipal *nspri;
2439 nsIGlobalObject *nsglo;
2440 nsIXMLHttpRequest *nsxhr;
2441 nsresult nsres;
2443 nsres = nsIDOMWindow_GetInnerWindow(nswindow, &inner_window);
2444 if(NS_FAILED(nsres)) {
2445 ERR("Could not get inner window: %08x\n", nsres);
2446 return NULL;
2449 nsres = mozIDOMWindow_QueryInterface(inner_window, &IID_nsIGlobalObject, (void **)&nsglo);
2450 mozIDOMWindow_Release(inner_window);
2451 assert(nsres == NS_OK);
2453 nsres = nsIGlobalObject_QueryInterface(nsglo, &IID_nsIScriptObjectPrincipal, (void**)&sop);
2454 assert(nsres == NS_OK);
2456 nspri = nsIScriptObjectPrincipal_GetPrincipal(sop);
2457 nsIScriptObjectPrincipal_Release(sop);
2459 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
2460 NS_XMLHTTPREQUEST_CONTRACTID, NULL, &IID_nsIXMLHttpRequest,
2461 (void**)&nsxhr);
2462 if(NS_SUCCEEDED(nsres)) {
2463 nsres = nsIXMLHttpRequest_Init(nsxhr, nspri, NULL, nsglo, NULL, NULL);
2464 if(NS_FAILED(nsres))
2465 nsIXMLHttpRequest_Release(nsxhr);
2467 nsISupports_Release(nspri);
2468 nsIGlobalObject_Release(nsglo);
2469 if(NS_FAILED(nsres)) {
2470 ERR("nsIXMLHttpRequest_Init failed: %08x\n", nsres);
2471 return NULL;
2474 return nsxhr;