makefiles: Don't use standard libs for programs that specify -nodefaultlibs.
[wine/zf.git] / dlls / mshtml / nsembed.c
blob2122536fc5711af080f876f7768038dfaa18c7cb
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, VARIANT *p)
967 const PRUnichar *str;
969 if(NS_FAILED(nsres)) {
970 ERR("failed: %08x\n", nsres);
971 nsAString_Finish(nsstr);
972 return E_FAIL;
975 if(NS_StringGetIsVoid(nsstr)) {
976 TRACE("ret null\n");
977 V_VT(p) = VT_NULL;
978 }else {
979 nsAString_GetData(nsstr, &str);
980 TRACE("ret %s\n", debugstr_w(str));
981 if(*str) {
982 V_BSTR(p) = SysAllocString(str);
983 if(!V_BSTR(p)) {
984 nsAString_Finish(nsstr);
985 return E_OUTOFMEMORY;
987 }else {
988 V_BSTR(p) = NULL;
990 V_VT(p) = VT_BSTR;
993 nsAString_Finish(nsstr);
994 return S_OK;
998 * Converts VARIANT to string and stores the result in nsAString. To avoid useless
999 * allocations, the function uses an existing string if available, so caller must
1000 * ensure that passed VARIANT is unchanged as long as its string representation is used
1002 HRESULT variant_to_nsstr(VARIANT *v, BOOL hex_int, nsAString *nsstr)
1004 WCHAR buf[32];
1006 static const WCHAR d_formatW[] = {'%','d',0};
1007 static const WCHAR hex_formatW[] = {'#','%','0','6','x',0};
1009 switch(V_VT(v)) {
1010 case VT_NULL:
1011 nsAString_InitDepend(nsstr, NULL);
1012 return S_OK;
1014 case VT_BSTR:
1015 nsAString_InitDepend(nsstr, V_BSTR(v));
1016 break;
1018 case VT_BSTR|VT_BYREF:
1019 nsAString_InitDepend(nsstr, *V_BSTRREF(v));
1020 break;
1022 case VT_I4:
1023 wsprintfW(buf, hex_int ? hex_formatW : d_formatW, V_I4(v));
1024 nsAString_Init(nsstr, buf);
1025 break;
1027 case VT_R8: {
1028 VARIANT strv;
1029 HRESULT hres;
1031 V_VT(&strv) = VT_EMPTY;
1032 hres = VariantChangeTypeEx(&strv, v, MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT),
1033 0, VT_BSTR);
1034 if(FAILED(hres))
1035 return hres;
1037 nsAString_Init(nsstr, V_BSTR(&strv));
1038 SysFreeString(V_BSTR(&strv));
1039 break;
1042 default:
1043 FIXME("not implemented for %s\n", debugstr_variant(v));
1044 return E_NOTIMPL;
1047 return S_OK;
1050 nsICommandParams *create_nscommand_params(void)
1052 nsICommandParams *ret = NULL;
1053 nsresult nsres;
1055 if(!pCompMgr)
1056 return NULL;
1058 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
1059 NS_COMMANDPARAMS_CONTRACTID, NULL, &IID_nsICommandParams,
1060 (void**)&ret);
1061 if(NS_FAILED(nsres))
1062 ERR("Could not get nsICommandParams\n");
1064 return ret;
1067 nsIWritableVariant *create_nsvariant(void)
1069 nsIWritableVariant *ret = NULL;
1070 nsresult nsres;
1072 if(!pCompMgr)
1073 return NULL;
1075 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
1076 NS_VARIANT_CONTRACTID, NULL, &IID_nsIWritableVariant, (void**)&ret);
1077 if(NS_FAILED(nsres))
1078 ERR("Could not get nsIVariant\n");
1080 return ret;
1083 char *get_nscategory_entry(const char *category, const char *entry)
1085 char *ret = NULL;
1086 nsresult nsres;
1088 nsres = nsICategoryManager_GetCategoryEntry(cat_mgr, category, entry, &ret);
1089 return NS_SUCCEEDED(nsres) ? ret : NULL;
1092 nsresult get_nsinterface(nsISupports *iface, REFIID riid, void **ppv)
1094 nsIInterfaceRequestor *iface_req;
1095 nsresult nsres;
1097 nsres = nsISupports_QueryInterface(iface, &IID_nsIInterfaceRequestor, (void**)&iface_req);
1098 if(NS_FAILED(nsres))
1099 return nsres;
1101 nsres = nsIInterfaceRequestor_GetInterface(iface_req, riid, ppv);
1102 nsIInterfaceRequestor_Release(iface_req);
1104 return nsres;
1107 static HRESULT nsnode_to_nsstring_rec(nsIContentSerializer *serializer, nsIDOMNode *nsnode, nsAString *str)
1109 nsIDOMNodeList *node_list = NULL;
1110 cpp_bool has_children = FALSE;
1111 nsIContent *nscontent;
1112 UINT16 type;
1113 nsresult nsres;
1115 nsIDOMNode_HasChildNodes(nsnode, &has_children);
1117 nsres = nsIDOMNode_GetNodeType(nsnode, &type);
1118 if(NS_FAILED(nsres)) {
1119 ERR("GetType failed: %08x\n", nsres);
1120 return E_FAIL;
1123 if(type != DOCUMENT_NODE) {
1124 nsres = nsIDOMNode_QueryInterface(nsnode, &IID_nsIContent, (void**)&nscontent);
1125 if(NS_FAILED(nsres)) {
1126 ERR("Could not get nsIContent interface: %08x\n", nsres);
1127 return E_FAIL;
1131 switch(type) {
1132 case ELEMENT_NODE:
1133 nsIContentSerializer_AppendElementStart(serializer, nscontent, nscontent, str);
1134 break;
1135 case TEXT_NODE:
1136 nsIContentSerializer_AppendText(serializer, nscontent, 0, -1, str);
1137 break;
1138 case COMMENT_NODE:
1139 nsIContentSerializer_AppendComment(serializer, nscontent, 0, -1, str);
1140 break;
1141 case DOCUMENT_NODE: {
1142 nsIDocument *nsdoc;
1143 nsIDOMNode_QueryInterface(nsnode, &IID_nsIDocument, (void**)&nsdoc);
1144 nsIContentSerializer_AppendDocumentStart(serializer, nsdoc, str);
1145 nsIDocument_Release(nsdoc);
1146 break;
1148 case DOCUMENT_TYPE_NODE:
1149 nsIContentSerializer_AppendDoctype(serializer, nscontent, str);
1150 break;
1151 case DOCUMENT_FRAGMENT_NODE:
1152 break;
1153 default:
1154 FIXME("Unhandled type %u\n", type);
1157 if(has_children) {
1158 UINT32 child_cnt, i;
1159 nsIDOMNode *child_node;
1161 nsIDOMNode_GetChildNodes(nsnode, &node_list);
1162 nsIDOMNodeList_GetLength(node_list, &child_cnt);
1164 for(i=0; i<child_cnt; i++) {
1165 nsres = nsIDOMNodeList_Item(node_list, i, &child_node);
1166 if(NS_SUCCEEDED(nsres)) {
1167 nsnode_to_nsstring_rec(serializer, child_node, str);
1168 nsIDOMNode_Release(child_node);
1169 }else {
1170 ERR("Item failed: %08x\n", nsres);
1174 nsIDOMNodeList_Release(node_list);
1177 if(type == ELEMENT_NODE)
1178 nsIContentSerializer_AppendElementEnd(serializer, nscontent, str);
1180 if(type != DOCUMENT_NODE)
1181 nsIContent_Release(nscontent);
1182 return S_OK;
1185 HRESULT nsnode_to_nsstring(nsIDOMNode *nsnode, nsAString *str)
1187 nsIContentSerializer *serializer;
1188 nsresult nsres;
1189 HRESULT hres;
1191 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
1192 NS_HTMLSERIALIZER_CONTRACTID, NULL, &IID_nsIContentSerializer,
1193 (void**)&serializer);
1194 if(NS_FAILED(nsres)) {
1195 ERR("Could not get nsIContentSerializer: %08x\n", nsres);
1196 return E_FAIL;
1199 nsres = nsIContentSerializer_Init(serializer, 0, 100, NULL, FALSE, FALSE /* FIXME */);
1200 if(NS_FAILED(nsres))
1201 ERR("Init failed: %08x\n", nsres);
1203 hres = nsnode_to_nsstring_rec(serializer, nsnode, str);
1204 if(SUCCEEDED(hres)) {
1205 nsres = nsIContentSerializer_Flush(serializer, str);
1206 if(NS_FAILED(nsres))
1207 ERR("Flush failed: %08x\n", nsres);
1210 nsIContentSerializer_Release(serializer);
1211 return hres;
1214 void setup_editor_controller(GeckoBrowser *This)
1216 nsIEditingSession *editing_session = NULL;
1217 nsIControllerContext *ctrlctx;
1218 nsresult nsres;
1220 if(This->editor) {
1221 nsIEditor_Release(This->editor);
1222 This->editor = NULL;
1225 if(This->editor_controller) {
1226 nsIController_Release(This->editor_controller);
1227 This->editor_controller = NULL;
1230 nsres = get_nsinterface((nsISupports*)This->webbrowser, &IID_nsIEditingSession,
1231 (void**)&editing_session);
1232 if(NS_FAILED(nsres)) {
1233 ERR("Could not get nsIEditingSession: %08x\n", nsres);
1234 return;
1237 nsres = nsIEditingSession_GetEditorForWindow(editing_session,
1238 This->doc->basedoc.window->window_proxy, &This->editor);
1239 nsIEditingSession_Release(editing_session);
1240 if(NS_FAILED(nsres)) {
1241 ERR("Could not get editor: %08x\n", nsres);
1242 return;
1245 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
1246 NS_EDITORCONTROLLER_CONTRACTID, NULL, &IID_nsIControllerContext, (void**)&ctrlctx);
1247 if(NS_SUCCEEDED(nsres)) {
1248 nsres = nsIControllerContext_SetCommandContext(ctrlctx, (nsISupports *)This->editor);
1249 if(NS_FAILED(nsres))
1250 ERR("SetCommandContext failed: %08x\n", nsres);
1251 nsres = nsIControllerContext_QueryInterface(ctrlctx, &IID_nsIController,
1252 (void**)&This->editor_controller);
1253 nsIControllerContext_Release(ctrlctx);
1254 if(NS_FAILED(nsres))
1255 ERR("Could not get nsIController interface: %08x\n", nsres);
1256 }else {
1257 ERR("Could not create edit controller: %08x\n", nsres);
1261 void close_gecko(void)
1263 TRACE("()\n");
1265 release_nsio();
1266 init_mutation(NULL);
1268 if(profile_directory) {
1269 nsIFile_Release(profile_directory);
1270 profile_directory = NULL;
1273 if(plugin_directory) {
1274 nsIFile_Release(plugin_directory);
1275 plugin_directory = NULL;
1278 if(pCompMgr)
1279 nsIComponentManager_Release(pCompMgr);
1281 if(pServMgr)
1282 nsIServiceManager_Release(pServMgr);
1284 if(cat_mgr)
1285 nsICategoryManager_Release(cat_mgr);
1287 /* Gecko doesn't really support being unloaded */
1288 /* if (hXPCOM) FreeLibrary(hXPCOM); */
1290 DeleteCriticalSection(&cs_load_gecko);
1293 BOOL is_gecko_path(const char *path)
1295 WCHAR *buf, *ptr;
1296 BOOL ret;
1298 buf = heap_strdupUtoW(path);
1299 if(!buf || lstrlenW(buf) < gecko_path_len)
1300 return FALSE;
1302 for(ptr = buf; *ptr; ptr++) {
1303 if(*ptr == '\\')
1304 *ptr = '/';
1307 UrlUnescapeW(buf, NULL, NULL, URL_UNESCAPE_INPLACE);
1308 buf[gecko_path_len] = 0;
1310 ret = !wcsicmp(buf, gecko_path);
1311 heap_free(buf);
1312 return ret;
1315 void set_viewer_zoom(GeckoBrowser *browser, float factor)
1317 nsIContentViewer *content_viewer;
1318 nsIDocShell *doc_shell;
1319 nsresult nsres;
1321 TRACE("Setting to %f\n", factor);
1323 nsres = get_nsinterface((nsISupports*)browser->navigation, &IID_nsIDocShell, (void**)&doc_shell);
1324 assert(nsres == NS_OK);
1326 nsres = nsIDocShell_GetContentViewer(doc_shell, &content_viewer);
1327 assert(nsres == NS_OK && content_viewer);
1328 nsIDocShell_Release(doc_shell);
1330 nsres = nsIContentViewer_SetFullZoom(content_viewer, factor);
1331 if(NS_FAILED(nsres))
1332 ERR("SetFullZoom failed: %08x\n", nsres);
1334 nsIContentViewer_Release(content_viewer);
1337 float get_viewer_zoom(GeckoBrowser *browser)
1339 nsIContentViewer *content_viewer;
1340 nsIDocShell *doc_shell;
1341 nsresult nsres;
1342 float factor;
1344 nsres = get_nsinterface((nsISupports*)browser->navigation, &IID_nsIDocShell, (void**)&doc_shell);
1345 assert(nsres == NS_OK);
1347 nsres = nsIDocShell_GetContentViewer(doc_shell, &content_viewer);
1348 assert(nsres == NS_OK && content_viewer);
1349 nsIDocShell_Release(doc_shell);
1351 nsres = nsIContentViewer_GetFullZoom(content_viewer, &factor);
1352 if(NS_FAILED(nsres))
1353 ERR("GetFullZoom failed: %08x\n", nsres);
1354 TRACE("Got %f\n", factor);
1356 nsIContentViewer_Release(content_viewer);
1357 return factor;
1360 struct nsWeakReference {
1361 nsIWeakReference nsIWeakReference_iface;
1363 LONG ref;
1365 GeckoBrowser *browser;
1368 static inline nsWeakReference *impl_from_nsIWeakReference(nsIWeakReference *iface)
1370 return CONTAINING_RECORD(iface, nsWeakReference, nsIWeakReference_iface);
1373 static nsresult NSAPI nsWeakReference_QueryInterface(nsIWeakReference *iface,
1374 nsIIDRef riid, void **result)
1376 nsWeakReference *This = impl_from_nsIWeakReference(iface);
1378 if(IsEqualGUID(&IID_nsISupports, riid)) {
1379 TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
1380 *result = &This->nsIWeakReference_iface;
1381 }else if(IsEqualGUID(&IID_nsIWeakReference, riid)) {
1382 TRACE("(%p)->(IID_nsIWeakReference %p)\n", This, result);
1383 *result = &This->nsIWeakReference_iface;
1384 }else {
1385 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
1386 *result = NULL;
1387 return NS_NOINTERFACE;
1390 nsISupports_AddRef((nsISupports*)*result);
1391 return NS_OK;
1394 static nsrefcnt NSAPI nsWeakReference_AddRef(nsIWeakReference *iface)
1396 nsWeakReference *This = impl_from_nsIWeakReference(iface);
1397 LONG ref = InterlockedIncrement(&This->ref);
1399 TRACE("(%p) ref=%d\n", This, ref);
1401 return ref;
1404 static nsrefcnt NSAPI nsWeakReference_Release(nsIWeakReference *iface)
1406 nsWeakReference *This = impl_from_nsIWeakReference(iface);
1407 LONG ref = InterlockedDecrement(&This->ref);
1409 TRACE("(%p) ref=%d\n", This, ref);
1411 if(!ref) {
1412 assert(!This->browser);
1413 heap_free(This);
1416 return ref;
1419 static nsresult NSAPI nsWeakReference_QueryReferent(nsIWeakReference *iface,
1420 const nsIID *riid, void **result)
1422 nsWeakReference *This = impl_from_nsIWeakReference(iface);
1424 if(!This->browser)
1425 return NS_ERROR_NULL_POINTER;
1427 return nsIWebBrowserChrome_QueryInterface(&This->browser->nsIWebBrowserChrome_iface, riid, result);
1430 static const nsIWeakReferenceVtbl nsWeakReferenceVtbl = {
1431 nsWeakReference_QueryInterface,
1432 nsWeakReference_AddRef,
1433 nsWeakReference_Release,
1434 nsWeakReference_QueryReferent
1437 /**********************************************************
1438 * nsIWebBrowserChrome interface
1441 static inline GeckoBrowser *impl_from_nsIWebBrowserChrome(nsIWebBrowserChrome *iface)
1443 return CONTAINING_RECORD(iface, GeckoBrowser, nsIWebBrowserChrome_iface);
1446 static nsresult NSAPI nsWebBrowserChrome_QueryInterface(nsIWebBrowserChrome *iface,
1447 nsIIDRef riid, void **result)
1449 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1451 *result = NULL;
1452 if(IsEqualGUID(&IID_nsISupports, riid)) {
1453 TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
1454 *result = &This->nsIWebBrowserChrome_iface;
1455 }else if(IsEqualGUID(&IID_nsIWebBrowserChrome, riid)) {
1456 TRACE("(%p)->(IID_nsIWebBrowserChrome, %p)\n", This, result);
1457 *result = &This->nsIWebBrowserChrome_iface;
1458 }else if(IsEqualGUID(&IID_nsIContextMenuListener, riid)) {
1459 TRACE("(%p)->(IID_nsIContextMenuListener, %p)\n", This, result);
1460 *result = &This->nsIContextMenuListener_iface;
1461 }else if(IsEqualGUID(&IID_nsIURIContentListener, riid)) {
1462 TRACE("(%p)->(IID_nsIURIContentListener %p)\n", This, result);
1463 *result = &This->nsIURIContentListener_iface;
1464 }else if(IsEqualGUID(&IID_nsIEmbeddingSiteWindow, riid)) {
1465 TRACE("(%p)->(IID_nsIEmbeddingSiteWindow %p)\n", This, result);
1466 *result = &This->nsIEmbeddingSiteWindow_iface;
1467 }else if(IsEqualGUID(&IID_nsITooltipListener, riid)) {
1468 TRACE("(%p)->(IID_nsITooltipListener %p)\n", This, result);
1469 *result = &This->nsITooltipListener_iface;
1470 }else if(IsEqualGUID(&IID_nsIInterfaceRequestor, riid)) {
1471 TRACE("(%p)->(IID_nsIInterfaceRequestor %p)\n", This, result);
1472 *result = &This->nsIInterfaceRequestor_iface;
1473 }else if(IsEqualGUID(&IID_nsISupportsWeakReference, riid)) {
1474 TRACE("(%p)->(IID_nsISupportsWeakReference %p)\n", This, result);
1475 *result = &This->nsISupportsWeakReference_iface;
1478 if(*result) {
1479 nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1480 return NS_OK;
1483 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
1484 return NS_NOINTERFACE;
1487 static nsrefcnt NSAPI nsWebBrowserChrome_AddRef(nsIWebBrowserChrome *iface)
1489 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1490 LONG ref = InterlockedIncrement(&This->ref);
1492 TRACE("(%p) ref=%d\n", This, ref);
1494 return ref;
1497 static nsrefcnt NSAPI nsWebBrowserChrome_Release(nsIWebBrowserChrome *iface)
1499 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1500 LONG ref = InterlockedDecrement(&This->ref);
1502 TRACE("(%p) ref=%d\n", This, ref);
1504 if(!ref) {
1505 if(This->doc)
1506 detach_gecko_browser(This);
1507 if(This->weak_reference) {
1508 This->weak_reference->browser = NULL;
1509 nsIWeakReference_Release(&This->weak_reference->nsIWeakReference_iface);
1511 heap_free(This);
1514 return ref;
1517 static nsresult NSAPI nsWebBrowserChrome_SetStatus(nsIWebBrowserChrome *iface,
1518 UINT32 statusType, const PRUnichar *status)
1520 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1521 TRACE("(%p)->(%d %s)\n", This, statusType, debugstr_w(status));
1522 return NS_OK;
1525 static nsresult NSAPI nsWebBrowserChrome_GetWebBrowser(nsIWebBrowserChrome *iface,
1526 nsIWebBrowser **aWebBrowser)
1528 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1530 TRACE("(%p)->(%p)\n", This, aWebBrowser);
1532 if(!aWebBrowser)
1533 return NS_ERROR_INVALID_ARG;
1535 if(This->webbrowser)
1536 nsIWebBrowser_AddRef(This->webbrowser);
1537 *aWebBrowser = This->webbrowser;
1538 return S_OK;
1541 static nsresult NSAPI nsWebBrowserChrome_SetWebBrowser(nsIWebBrowserChrome *iface,
1542 nsIWebBrowser *aWebBrowser)
1544 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1546 TRACE("(%p)->(%p)\n", This, aWebBrowser);
1548 if(aWebBrowser != This->webbrowser)
1549 ERR("Wrong nsWebBrowser!\n");
1551 return NS_OK;
1554 static nsresult NSAPI nsWebBrowserChrome_GetChromeFlags(nsIWebBrowserChrome *iface,
1555 UINT32 *aChromeFlags)
1557 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1558 WARN("(%p)->(%p)\n", This, aChromeFlags);
1559 return NS_ERROR_NOT_IMPLEMENTED;
1562 static nsresult NSAPI nsWebBrowserChrome_SetChromeFlags(nsIWebBrowserChrome *iface,
1563 UINT32 aChromeFlags)
1565 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1566 WARN("(%p)->(%08x)\n", This, aChromeFlags);
1567 return NS_ERROR_NOT_IMPLEMENTED;
1570 static nsresult NSAPI nsWebBrowserChrome_DestroyBrowserWindow(nsIWebBrowserChrome *iface)
1572 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1573 TRACE("(%p)\n", This);
1574 return NS_ERROR_NOT_IMPLEMENTED;
1577 static nsresult NSAPI nsWebBrowserChrome_SizeBrowserTo(nsIWebBrowserChrome *iface,
1578 LONG aCX, LONG aCY)
1580 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1581 WARN("(%p)->(%d %d)\n", This, aCX, aCY);
1582 return NS_ERROR_NOT_IMPLEMENTED;
1585 static nsresult NSAPI nsWebBrowserChrome_ShowAsModal(nsIWebBrowserChrome *iface)
1587 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1588 WARN("(%p)\n", This);
1589 return NS_ERROR_NOT_IMPLEMENTED;
1592 static nsresult NSAPI nsWebBrowserChrome_IsWindowModal(nsIWebBrowserChrome *iface, cpp_bool *_retval)
1594 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1595 WARN("(%p)->(%p)\n", This, _retval);
1596 return NS_ERROR_NOT_IMPLEMENTED;
1599 static nsresult NSAPI nsWebBrowserChrome_ExitModalEventLoop(nsIWebBrowserChrome *iface,
1600 nsresult aStatus)
1602 GeckoBrowser *This = impl_from_nsIWebBrowserChrome(iface);
1603 WARN("(%p)->(%08x)\n", This, aStatus);
1604 return NS_ERROR_NOT_IMPLEMENTED;
1607 static const nsIWebBrowserChromeVtbl nsWebBrowserChromeVtbl = {
1608 nsWebBrowserChrome_QueryInterface,
1609 nsWebBrowserChrome_AddRef,
1610 nsWebBrowserChrome_Release,
1611 nsWebBrowserChrome_SetStatus,
1612 nsWebBrowserChrome_GetWebBrowser,
1613 nsWebBrowserChrome_SetWebBrowser,
1614 nsWebBrowserChrome_GetChromeFlags,
1615 nsWebBrowserChrome_SetChromeFlags,
1616 nsWebBrowserChrome_DestroyBrowserWindow,
1617 nsWebBrowserChrome_SizeBrowserTo,
1618 nsWebBrowserChrome_ShowAsModal,
1619 nsWebBrowserChrome_IsWindowModal,
1620 nsWebBrowserChrome_ExitModalEventLoop
1623 /**********************************************************
1624 * nsIContextMenuListener interface
1627 static inline GeckoBrowser *impl_from_nsIContextMenuListener(nsIContextMenuListener *iface)
1629 return CONTAINING_RECORD(iface, GeckoBrowser, nsIContextMenuListener_iface);
1632 static nsresult NSAPI nsContextMenuListener_QueryInterface(nsIContextMenuListener *iface,
1633 nsIIDRef riid, void **result)
1635 GeckoBrowser *This = impl_from_nsIContextMenuListener(iface);
1636 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1639 static nsrefcnt NSAPI nsContextMenuListener_AddRef(nsIContextMenuListener *iface)
1641 GeckoBrowser *This = impl_from_nsIContextMenuListener(iface);
1642 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1645 static nsrefcnt NSAPI nsContextMenuListener_Release(nsIContextMenuListener *iface)
1647 GeckoBrowser *This = impl_from_nsIContextMenuListener(iface);
1648 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
1651 static nsresult NSAPI nsContextMenuListener_OnShowContextMenu(nsIContextMenuListener *iface,
1652 UINT32 aContextFlags, nsIDOMEvent *aEvent, nsIDOMNode *aNode)
1654 GeckoBrowser *This = impl_from_nsIContextMenuListener(iface);
1655 nsIDOMMouseEvent *mouse_event;
1656 HTMLDOMNode *node;
1657 DOMEvent *event;
1658 POINT pt;
1659 DWORD dwID = CONTEXT_MENU_DEFAULT;
1660 nsresult nsres;
1661 HRESULT hres;
1663 TRACE("(%p)->(%08x %p %p)\n", This, aContextFlags, aEvent, aNode);
1665 if (!aNode)
1666 return NS_ERROR_FAILURE;
1668 hres = get_node(aNode, TRUE, &node);
1669 if(FAILED(hres))
1670 return NS_ERROR_FAILURE;
1672 hres = create_event_from_nsevent(aEvent, &event);
1673 if(SUCCEEDED(hres)) {
1674 dispatch_event(&node->event_target, event);
1675 IDOMEvent_Release(&event->IDOMEvent_iface);
1678 nsres = nsIDOMEvent_QueryInterface(aEvent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
1679 assert(NS_SUCCEEDED(nsres));
1681 nsIDOMMouseEvent_GetScreenX(mouse_event, &pt.x);
1682 nsIDOMMouseEvent_GetScreenY(mouse_event, &pt.y);
1683 nsIDOMMouseEvent_Release(mouse_event);
1685 switch(aContextFlags) {
1686 case CONTEXT_NONE:
1687 case CONTEXT_DOCUMENT:
1688 case CONTEXT_TEXT: {
1689 nsISelection *selection;
1691 nsres = nsIDOMHTMLDocument_GetSelection(This->doc->basedoc.doc_node->nsdoc, &selection);
1692 if(NS_SUCCEEDED(nsres) && selection) {
1693 cpp_bool is_collapsed;
1695 /* FIXME: Check if the click was inside selection. */
1696 nsres = nsISelection_GetIsCollapsed(selection, &is_collapsed);
1697 nsISelection_Release(selection);
1698 if(NS_SUCCEEDED(nsres) && !is_collapsed)
1699 dwID = CONTEXT_MENU_TEXTSELECT;
1701 break;
1703 case CONTEXT_IMAGE:
1704 case CONTEXT_IMAGE|CONTEXT_LINK:
1705 dwID = CONTEXT_MENU_IMAGE;
1706 break;
1707 case CONTEXT_LINK:
1708 dwID = CONTEXT_MENU_ANCHOR;
1709 break;
1710 case CONTEXT_INPUT:
1711 dwID = CONTEXT_MENU_CONTROL;
1712 break;
1713 default:
1714 FIXME("aContextFlags=%08x\n", aContextFlags);
1717 show_context_menu(This->doc, dwID, &pt, (IDispatch*)&node->IHTMLDOMNode_iface);
1718 node_release(node);
1719 return NS_OK;
1722 static const nsIContextMenuListenerVtbl nsContextMenuListenerVtbl = {
1723 nsContextMenuListener_QueryInterface,
1724 nsContextMenuListener_AddRef,
1725 nsContextMenuListener_Release,
1726 nsContextMenuListener_OnShowContextMenu
1729 /**********************************************************
1730 * nsIURIContentListener interface
1733 static inline GeckoBrowser *impl_from_nsIURIContentListener(nsIURIContentListener *iface)
1735 return CONTAINING_RECORD(iface, GeckoBrowser, nsIURIContentListener_iface);
1738 static nsresult NSAPI nsURIContentListener_QueryInterface(nsIURIContentListener *iface,
1739 nsIIDRef riid, void **result)
1741 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1742 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1745 static nsrefcnt NSAPI nsURIContentListener_AddRef(nsIURIContentListener *iface)
1747 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1748 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1751 static nsrefcnt NSAPI nsURIContentListener_Release(nsIURIContentListener *iface)
1753 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1754 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
1757 static nsresult NSAPI nsURIContentListener_OnStartURIOpen(nsIURIContentListener *iface,
1758 nsIURI *aURI, cpp_bool *_retval)
1760 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1761 nsACString spec_str;
1762 const char *spec;
1764 nsACString_Init(&spec_str, NULL);
1765 nsIURI_GetSpec(aURI, &spec_str);
1766 nsACString_GetData(&spec_str, &spec);
1768 TRACE("(%p)->(%p(%s) %p)\n", This, aURI, debugstr_a(spec), _retval);
1770 nsACString_Finish(&spec_str);
1772 return This->content_listener
1773 ? nsIURIContentListener_OnStartURIOpen(This->content_listener, aURI, _retval)
1774 : NS_OK;
1777 static nsresult NSAPI nsURIContentListener_DoContent(nsIURIContentListener *iface,
1778 const nsACString *aContentType, cpp_bool aIsContentPreferred, nsIRequest *aRequest,
1779 nsIStreamListener **aContentHandler, cpp_bool *_retval)
1781 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1783 TRACE("(%p)->(%p %x %p %p %p)\n", This, aContentType, aIsContentPreferred,
1784 aRequest, aContentHandler, _retval);
1786 return This->content_listener
1787 ? nsIURIContentListener_DoContent(This->content_listener, aContentType,
1788 aIsContentPreferred, aRequest, aContentHandler, _retval)
1789 : NS_ERROR_NOT_IMPLEMENTED;
1792 static nsresult NSAPI nsURIContentListener_IsPreferred(nsIURIContentListener *iface,
1793 const char *aContentType, char **aDesiredContentType, cpp_bool *_retval)
1795 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1797 TRACE("(%p)->(%s %p %p)\n", This, debugstr_a(aContentType), aDesiredContentType, _retval);
1799 /* FIXME: Should we do something here? */
1800 *_retval = TRUE;
1802 return This->content_listener
1803 ? nsIURIContentListener_IsPreferred(This->content_listener, aContentType,
1804 aDesiredContentType, _retval)
1805 : NS_OK;
1808 static nsresult NSAPI nsURIContentListener_CanHandleContent(nsIURIContentListener *iface,
1809 const char *aContentType, cpp_bool aIsContentPreferred, char **aDesiredContentType,
1810 cpp_bool *_retval)
1812 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1814 TRACE("(%p)->(%s %x %p %p)\n", This, debugstr_a(aContentType), aIsContentPreferred,
1815 aDesiredContentType, _retval);
1817 return This->content_listener
1818 ? nsIURIContentListener_CanHandleContent(This->content_listener, aContentType,
1819 aIsContentPreferred, aDesiredContentType, _retval)
1820 : NS_ERROR_NOT_IMPLEMENTED;
1823 static nsresult NSAPI nsURIContentListener_GetLoadCookie(nsIURIContentListener *iface,
1824 nsISupports **aLoadCookie)
1826 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1828 WARN("(%p)->(%p)\n", This, aLoadCookie);
1830 return This->content_listener
1831 ? nsIURIContentListener_GetLoadCookie(This->content_listener, aLoadCookie)
1832 : NS_ERROR_NOT_IMPLEMENTED;
1835 static nsresult NSAPI nsURIContentListener_SetLoadCookie(nsIURIContentListener *iface,
1836 nsISupports *aLoadCookie)
1838 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1840 WARN("(%p)->(%p)\n", This, aLoadCookie);
1842 return This->content_listener
1843 ? nsIURIContentListener_SetLoadCookie(This->content_listener, aLoadCookie)
1844 : NS_ERROR_NOT_IMPLEMENTED;
1847 static nsresult NSAPI nsURIContentListener_GetParentContentListener(nsIURIContentListener *iface,
1848 nsIURIContentListener **aParentContentListener)
1850 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1852 TRACE("(%p)->(%p)\n", This, aParentContentListener);
1854 if(This->content_listener)
1855 nsIURIContentListener_AddRef(This->content_listener);
1857 *aParentContentListener = This->content_listener;
1858 return NS_OK;
1861 static nsresult NSAPI nsURIContentListener_SetParentContentListener(nsIURIContentListener *iface,
1862 nsIURIContentListener *aParentContentListener)
1864 GeckoBrowser *This = impl_from_nsIURIContentListener(iface);
1866 TRACE("(%p)->(%p)\n", This, aParentContentListener);
1868 if(aParentContentListener == &This->nsIURIContentListener_iface)
1869 return NS_OK;
1871 if(This->content_listener)
1872 nsIURIContentListener_Release(This->content_listener);
1874 This->content_listener = aParentContentListener;
1875 if(This->content_listener)
1876 nsIURIContentListener_AddRef(This->content_listener);
1878 return NS_OK;
1881 static const nsIURIContentListenerVtbl nsURIContentListenerVtbl = {
1882 nsURIContentListener_QueryInterface,
1883 nsURIContentListener_AddRef,
1884 nsURIContentListener_Release,
1885 nsURIContentListener_OnStartURIOpen,
1886 nsURIContentListener_DoContent,
1887 nsURIContentListener_IsPreferred,
1888 nsURIContentListener_CanHandleContent,
1889 nsURIContentListener_GetLoadCookie,
1890 nsURIContentListener_SetLoadCookie,
1891 nsURIContentListener_GetParentContentListener,
1892 nsURIContentListener_SetParentContentListener
1895 /**********************************************************
1896 * nsIEmbeddinSiteWindow interface
1899 static inline GeckoBrowser *impl_from_nsIEmbeddingSiteWindow(nsIEmbeddingSiteWindow *iface)
1901 return CONTAINING_RECORD(iface, GeckoBrowser, nsIEmbeddingSiteWindow_iface);
1904 static nsresult NSAPI nsEmbeddingSiteWindow_QueryInterface(nsIEmbeddingSiteWindow *iface,
1905 nsIIDRef riid, void **result)
1907 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1908 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1911 static nsrefcnt NSAPI nsEmbeddingSiteWindow_AddRef(nsIEmbeddingSiteWindow *iface)
1913 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1914 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1917 static nsrefcnt NSAPI nsEmbeddingSiteWindow_Release(nsIEmbeddingSiteWindow *iface)
1919 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1920 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
1923 static nsresult NSAPI nsEmbeddingSiteWindow_SetDimensions(nsIEmbeddingSiteWindow *iface,
1924 UINT32 flags, LONG x, LONG y, LONG cx, LONG cy)
1926 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1927 WARN("(%p)->(%08x %d %d %d %d)\n", This, flags, x, y, cx, cy);
1928 return NS_ERROR_NOT_IMPLEMENTED;
1931 static nsresult NSAPI nsEmbeddingSiteWindow_GetDimensions(nsIEmbeddingSiteWindow *iface,
1932 UINT32 flags, LONG *x, LONG *y, LONG *cx, LONG *cy)
1934 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1935 RECT r;
1937 TRACE("(%p)->(%x %p %p %p %p)\n", This, flags, x, y, cx, cy);
1939 if(!GetWindowRect(This->hwnd, &r)) {
1940 ERR("GetWindowRect failed\n");
1941 return NS_ERROR_FAILURE;
1944 if(x)
1945 *x = r.left;
1946 if(y)
1947 *y = r.top;
1948 if(cx)
1949 *cx = r.right-r.left;
1950 if(cy)
1951 *cy = r.bottom-r.top;
1952 return NS_OK;
1955 static nsresult NSAPI nsEmbeddingSiteWindow_SetFocus(nsIEmbeddingSiteWindow *iface)
1957 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1959 TRACE("(%p)\n", This);
1961 return nsIBaseWindow_SetFocus(This->window);
1964 static nsresult NSAPI nsEmbeddingSiteWindow_GetVisibility(nsIEmbeddingSiteWindow *iface,
1965 cpp_bool *aVisibility)
1967 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1969 TRACE("(%p)->(%p)\n", This, aVisibility);
1971 *aVisibility = This->doc && This->doc->hwnd && IsWindowVisible(This->doc->hwnd);
1972 return NS_OK;
1975 static nsresult NSAPI nsEmbeddingSiteWindow_SetVisibility(nsIEmbeddingSiteWindow *iface,
1976 cpp_bool aVisibility)
1978 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1980 TRACE("(%p)->(%x)\n", This, aVisibility);
1982 return NS_OK;
1985 static nsresult NSAPI nsEmbeddingSiteWindow_GetTitle(nsIEmbeddingSiteWindow *iface,
1986 PRUnichar **aTitle)
1988 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1989 WARN("(%p)->(%p)\n", This, aTitle);
1990 return NS_ERROR_NOT_IMPLEMENTED;
1993 static nsresult NSAPI nsEmbeddingSiteWindow_SetTitle(nsIEmbeddingSiteWindow *iface,
1994 const PRUnichar *aTitle)
1996 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
1997 WARN("(%p)->(%s)\n", This, debugstr_w(aTitle));
1998 return NS_ERROR_NOT_IMPLEMENTED;
2001 static nsresult NSAPI nsEmbeddingSiteWindow_GetSiteWindow(nsIEmbeddingSiteWindow *iface,
2002 void **aSiteWindow)
2004 GeckoBrowser *This = impl_from_nsIEmbeddingSiteWindow(iface);
2006 TRACE("(%p)->(%p)\n", This, aSiteWindow);
2008 *aSiteWindow = This->hwnd;
2009 return NS_OK;
2012 static const nsIEmbeddingSiteWindowVtbl nsEmbeddingSiteWindowVtbl = {
2013 nsEmbeddingSiteWindow_QueryInterface,
2014 nsEmbeddingSiteWindow_AddRef,
2015 nsEmbeddingSiteWindow_Release,
2016 nsEmbeddingSiteWindow_SetDimensions,
2017 nsEmbeddingSiteWindow_GetDimensions,
2018 nsEmbeddingSiteWindow_SetFocus,
2019 nsEmbeddingSiteWindow_GetVisibility,
2020 nsEmbeddingSiteWindow_SetVisibility,
2021 nsEmbeddingSiteWindow_GetTitle,
2022 nsEmbeddingSiteWindow_SetTitle,
2023 nsEmbeddingSiteWindow_GetSiteWindow
2026 static inline GeckoBrowser *impl_from_nsITooltipListener(nsITooltipListener *iface)
2028 return CONTAINING_RECORD(iface, GeckoBrowser, nsITooltipListener_iface);
2031 static nsresult NSAPI nsTooltipListener_QueryInterface(nsITooltipListener *iface, nsIIDRef riid,
2032 void **result)
2034 GeckoBrowser *This = impl_from_nsITooltipListener(iface);
2035 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
2038 static nsrefcnt NSAPI nsTooltipListener_AddRef(nsITooltipListener *iface)
2040 GeckoBrowser *This = impl_from_nsITooltipListener(iface);
2041 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
2044 static nsrefcnt NSAPI nsTooltipListener_Release(nsITooltipListener *iface)
2046 GeckoBrowser *This = impl_from_nsITooltipListener(iface);
2047 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
2050 static nsresult NSAPI nsTooltipListener_OnShowTooltip(nsITooltipListener *iface,
2051 LONG aXCoord, LONG aYCoord, const PRUnichar *aTipText)
2053 GeckoBrowser *This = impl_from_nsITooltipListener(iface);
2055 if (This->doc)
2056 show_tooltip(This->doc, aXCoord, aYCoord, aTipText);
2058 return NS_OK;
2061 static nsresult NSAPI nsTooltipListener_OnHideTooltip(nsITooltipListener *iface)
2063 GeckoBrowser *This = impl_from_nsITooltipListener(iface);
2065 if (This->doc)
2066 hide_tooltip(This->doc);
2068 return NS_OK;
2071 static const nsITooltipListenerVtbl nsTooltipListenerVtbl = {
2072 nsTooltipListener_QueryInterface,
2073 nsTooltipListener_AddRef,
2074 nsTooltipListener_Release,
2075 nsTooltipListener_OnShowTooltip,
2076 nsTooltipListener_OnHideTooltip
2079 static inline GeckoBrowser *impl_from_nsIInterfaceRequestor(nsIInterfaceRequestor *iface)
2081 return CONTAINING_RECORD(iface, GeckoBrowser, nsIInterfaceRequestor_iface);
2084 static nsresult NSAPI nsInterfaceRequestor_QueryInterface(nsIInterfaceRequestor *iface,
2085 nsIIDRef riid, void **result)
2087 GeckoBrowser *This = impl_from_nsIInterfaceRequestor(iface);
2088 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
2091 static nsrefcnt NSAPI nsInterfaceRequestor_AddRef(nsIInterfaceRequestor *iface)
2093 GeckoBrowser *This = impl_from_nsIInterfaceRequestor(iface);
2094 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
2097 static nsrefcnt NSAPI nsInterfaceRequestor_Release(nsIInterfaceRequestor *iface)
2099 GeckoBrowser *This = impl_from_nsIInterfaceRequestor(iface);
2100 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
2103 static nsresult NSAPI nsInterfaceRequestor_GetInterface(nsIInterfaceRequestor *iface,
2104 nsIIDRef riid, void **result)
2106 GeckoBrowser *This = impl_from_nsIInterfaceRequestor(iface);
2108 if(IsEqualGUID(&IID_mozIDOMWindowProxy, riid)) {
2109 TRACE("(%p)->(IID_nsIDOMWindow %p)\n", This, result);
2110 return nsIWebBrowser_GetContentDOMWindow(This->webbrowser, (mozIDOMWindowProxy**)result);
2113 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
2116 static const nsIInterfaceRequestorVtbl nsInterfaceRequestorVtbl = {
2117 nsInterfaceRequestor_QueryInterface,
2118 nsInterfaceRequestor_AddRef,
2119 nsInterfaceRequestor_Release,
2120 nsInterfaceRequestor_GetInterface
2123 static inline GeckoBrowser *impl_from_nsISupportsWeakReference(nsISupportsWeakReference *iface)
2125 return CONTAINING_RECORD(iface, GeckoBrowser, nsISupportsWeakReference_iface);
2128 static nsresult NSAPI nsSupportsWeakReference_QueryInterface(nsISupportsWeakReference *iface,
2129 nsIIDRef riid, void **result)
2131 GeckoBrowser *This = impl_from_nsISupportsWeakReference(iface);
2132 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
2135 static nsrefcnt NSAPI nsSupportsWeakReference_AddRef(nsISupportsWeakReference *iface)
2137 GeckoBrowser *This = impl_from_nsISupportsWeakReference(iface);
2138 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
2141 static nsrefcnt NSAPI nsSupportsWeakReference_Release(nsISupportsWeakReference *iface)
2143 GeckoBrowser *This = impl_from_nsISupportsWeakReference(iface);
2144 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
2147 static nsresult NSAPI nsSupportsWeakReference_GetWeakReference(nsISupportsWeakReference *iface,
2148 nsIWeakReference **_retval)
2150 GeckoBrowser *This = impl_from_nsISupportsWeakReference(iface);
2152 TRACE("(%p)->(%p)\n", This, _retval);
2154 if(!This->weak_reference) {
2155 This->weak_reference = heap_alloc(sizeof(nsWeakReference));
2156 if(!This->weak_reference)
2157 return NS_ERROR_OUT_OF_MEMORY;
2159 This->weak_reference->nsIWeakReference_iface.lpVtbl = &nsWeakReferenceVtbl;
2160 This->weak_reference->ref = 1;
2161 This->weak_reference->browser = This;
2164 *_retval = &This->weak_reference->nsIWeakReference_iface;
2165 nsIWeakReference_AddRef(*_retval);
2166 return NS_OK;
2169 static const nsISupportsWeakReferenceVtbl nsSupportsWeakReferenceVtbl = {
2170 nsSupportsWeakReference_QueryInterface,
2171 nsSupportsWeakReference_AddRef,
2172 nsSupportsWeakReference_Release,
2173 nsSupportsWeakReference_GetWeakReference
2176 static HRESULT init_browser(GeckoBrowser *browser)
2178 mozIDOMWindowProxy *mozwindow;
2179 nsIWebBrowserSetup *wbsetup;
2180 nsIScrollable *scrollable;
2181 nsresult nsres;
2182 HRESULT hres;
2184 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_WEBBROWSER_CONTRACTID,
2185 NULL, &IID_nsIWebBrowser, (void**)&browser->webbrowser);
2186 if(NS_FAILED(nsres)) {
2187 ERR("Creating WebBrowser failed: %08x\n", nsres);
2188 return E_FAIL;
2191 nsres = nsIWebBrowser_SetContainerWindow(browser->webbrowser, &browser->nsIWebBrowserChrome_iface);
2192 if(NS_FAILED(nsres)) {
2193 ERR("SetContainerWindow failed: %08x\n", nsres);
2194 return E_FAIL;
2197 nsres = nsIWebBrowser_QueryInterface(browser->webbrowser, &IID_nsIBaseWindow,
2198 (void**)&browser->window);
2199 if(NS_FAILED(nsres)) {
2200 ERR("Could not get nsIBaseWindow interface: %08x\n", nsres);
2201 return E_FAIL;
2204 nsres = nsIWebBrowser_QueryInterface(browser->webbrowser, &IID_nsIWebBrowserSetup,
2205 (void**)&wbsetup);
2206 if(NS_SUCCEEDED(nsres)) {
2207 nsres = nsIWebBrowserSetup_SetProperty(wbsetup, SETUP_IS_CHROME_WRAPPER, FALSE);
2208 nsIWebBrowserSetup_Release(wbsetup);
2209 if(NS_FAILED(nsres)) {
2210 ERR("SetProperty(SETUP_IS_CHROME_WRAPPER) failed: %08x\n", nsres);
2211 return E_FAIL;
2213 }else {
2214 ERR("Could not get nsIWebBrowserSetup interface\n");
2215 return E_FAIL;
2218 nsres = nsIWebBrowser_QueryInterface(browser->webbrowser, &IID_nsIWebNavigation,
2219 (void**)&browser->navigation);
2220 if(NS_FAILED(nsres)) {
2221 ERR("Could not get nsIWebNavigation interface: %08x\n", nsres);
2222 return E_FAIL;
2225 nsres = nsIWebBrowser_QueryInterface(browser->webbrowser, &IID_nsIWebBrowserFocus,
2226 (void**)&browser->focus);
2227 if(NS_FAILED(nsres)) {
2228 ERR("Could not get nsIWebBrowserFocus interface: %08x\n", nsres);
2229 return E_FAIL;
2232 if(!browser_class) {
2233 register_browser_class();
2234 if(!browser_class)
2235 return E_FAIL;
2238 browser->hwnd = CreateWindowExW(0, wszNsContainer, NULL,
2239 WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 0, 0, 100, 100,
2240 GetDesktopWindow(), NULL, hInst, browser);
2241 if(!browser->hwnd) {
2242 WARN("Could not create window\n");
2243 return E_FAIL;
2246 nsres = nsIBaseWindow_InitWindow(browser->window, browser->hwnd, NULL, 0, 0, 100, 100);
2247 if(NS_SUCCEEDED(nsres)) {
2248 nsres = nsIBaseWindow_Create(browser->window);
2249 if(NS_FAILED(nsres)) {
2250 WARN("Creating window failed: %08x\n", nsres);
2251 return E_FAIL;
2254 nsIBaseWindow_SetVisibility(browser->window, FALSE);
2255 nsIBaseWindow_SetEnabled(browser->window, FALSE);
2256 }else {
2257 ERR("InitWindow failed: %08x\n", nsres);
2258 return E_FAIL;
2261 nsres = nsIWebBrowser_SetParentURIContentListener(browser->webbrowser,
2262 &browser->nsIURIContentListener_iface);
2263 if(NS_FAILED(nsres))
2264 ERR("SetParentURIContentListener failed: %08x\n", nsres);
2266 nsres = nsIWebBrowser_QueryInterface(browser->webbrowser, &IID_nsIScrollable, (void**)&scrollable);
2267 if(NS_SUCCEEDED(nsres)) {
2268 nsres = nsIScrollable_SetDefaultScrollbarPreferences(scrollable,
2269 ScrollOrientation_Y, Scrollbar_Always);
2270 if(NS_FAILED(nsres))
2271 ERR("Could not set default Y scrollbar prefs: %08x\n", nsres);
2273 nsres = nsIScrollable_SetDefaultScrollbarPreferences(scrollable,
2274 ScrollOrientation_X, Scrollbar_Auto);
2275 if(NS_FAILED(nsres))
2276 ERR("Could not set default X scrollbar prefs: %08x\n", nsres);
2278 nsIScrollable_Release(scrollable);
2279 }else {
2280 ERR("Could not get nsIScrollable: %08x\n", nsres);
2283 nsres = nsIWebBrowser_GetContentDOMWindow(browser->webbrowser, &mozwindow);
2284 if(NS_FAILED(nsres)) {
2285 ERR("GetContentDOMWindow failed: %08x\n", nsres);
2286 return E_FAIL;
2289 hres = create_outer_window(browser, mozwindow, NULL, &browser->content_window);
2290 mozIDOMWindowProxy_Release(mozwindow);
2291 return hres;
2294 HRESULT create_gecko_browser(HTMLDocumentObj *doc, GeckoBrowser **_ret)
2296 GeckoBrowser *ret;
2297 HRESULT hres;
2299 if(!load_gecko())
2300 return CLASS_E_CLASSNOTAVAILABLE;
2302 ret = heap_alloc_zero(sizeof(GeckoBrowser));
2303 if(!ret)
2304 return E_OUTOFMEMORY;
2306 ret->nsIWebBrowserChrome_iface.lpVtbl = &nsWebBrowserChromeVtbl;
2307 ret->nsIContextMenuListener_iface.lpVtbl = &nsContextMenuListenerVtbl;
2308 ret->nsIURIContentListener_iface.lpVtbl = &nsURIContentListenerVtbl;
2309 ret->nsIEmbeddingSiteWindow_iface.lpVtbl = &nsEmbeddingSiteWindowVtbl;
2310 ret->nsITooltipListener_iface.lpVtbl = &nsTooltipListenerVtbl;
2311 ret->nsIInterfaceRequestor_iface.lpVtbl = &nsInterfaceRequestorVtbl;
2312 ret->nsISupportsWeakReference_iface.lpVtbl = &nsSupportsWeakReferenceVtbl;
2314 ret->doc = doc;
2315 ret->ref = 1;
2316 ret->script_mode = SCRIPTMODE_ACTIVESCRIPT;
2317 ret->usermode = UNKNOWN_USERMODE;
2318 list_init(&ret->document_nodes);
2319 list_init(&ret->outer_windows);
2321 hres = init_browser(ret);
2322 if(SUCCEEDED(hres))
2323 *_ret = ret;
2324 else
2325 nsIWebBrowserChrome_Release(&ret->nsIWebBrowserChrome_iface);
2326 return hres;
2329 void detach_gecko_browser(GeckoBrowser *This)
2331 nsIDOMWindowUtils *window_utils = NULL;
2333 TRACE("(%p)\n", This);
2335 This->doc = NULL;
2337 if(This->content_window) {
2338 get_nsinterface((nsISupports*)This->content_window->nswindow, &IID_nsIDOMWindowUtils, (void**)&window_utils);
2340 IHTMLWindow2_Release(&This->content_window->base.IHTMLWindow2_iface);
2341 This->content_window = NULL;
2344 while(!list_empty(&This->document_nodes)) {
2345 HTMLDocumentNode *doc = LIST_ENTRY(list_head(&This->document_nodes), HTMLDocumentNode, browser_entry);
2346 detach_document_node(doc);
2349 while(!list_empty(&This->outer_windows)) {
2350 HTMLOuterWindow *window = LIST_ENTRY(list_head(&This->outer_windows), HTMLOuterWindow, browser_entry);
2351 list_remove(&window->browser_entry);
2352 window->browser = NULL;
2355 ShowWindow(This->hwnd, SW_HIDE);
2356 SetParent(This->hwnd, NULL);
2358 nsIBaseWindow_SetVisibility(This->window, FALSE);
2359 nsIBaseWindow_Destroy(This->window);
2361 nsIWebBrowser_SetContainerWindow(This->webbrowser, NULL);
2363 nsIWebBrowser_Release(This->webbrowser);
2364 This->webbrowser = NULL;
2366 nsIWebNavigation_Release(This->navigation);
2367 This->navigation = NULL;
2369 nsIBaseWindow_Release(This->window);
2370 This->window = NULL;
2372 nsIWebBrowserFocus_Release(This->focus);
2373 This->focus = NULL;
2375 if(This->editor_controller) {
2376 nsIController_Release(This->editor_controller);
2377 This->editor_controller = NULL;
2380 if(This->editor) {
2381 nsIEditor_Release(This->editor);
2382 This->editor = NULL;
2385 if(This->content_listener) {
2386 nsIURIContentListener_Release(This->content_listener);
2387 This->content_listener = NULL;
2390 if(This->hwnd) {
2391 DestroyWindow(This->hwnd);
2392 This->hwnd = NULL;
2395 nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
2397 /* Force cycle collection */
2398 if(window_utils) {
2399 nsIDOMWindowUtils_CycleCollect(window_utils, NULL, 0);
2400 nsIDOMWindowUtils_Release(window_utils);
2405 * FIXME: nsIScriptObjectPrincipal uses thiscall calling convention, so we need this hack on i386.
2406 * This will be removed after the next Gecko update, that will change calling convention on Gecko side.
2408 #ifdef __i386__
2409 __ASM_GLOBAL_FUNC(call_thiscall_func,
2410 "popl %eax\n\t"
2411 "popl %edx\n\t"
2412 "popl %ecx\n\t"
2413 "pushl %eax\n\t"
2414 "jmp *%edx\n\t")
2415 #define nsIScriptObjectPrincipal_GetPrincipal(this) ((void* (WINAPI*)(void*,void*))&call_thiscall_func)((this)->lpVtbl->GetPrincipal,this)
2416 #endif
2418 nsIXMLHttpRequest *create_nsxhr(nsIDOMWindow *nswindow)
2420 nsIScriptObjectPrincipal *sop;
2421 mozIDOMWindow *inner_window;
2422 nsIPrincipal *nspri;
2423 nsIGlobalObject *nsglo;
2424 nsIXMLHttpRequest *nsxhr;
2425 nsresult nsres;
2427 nsres = nsIDOMWindow_GetInnerWindow(nswindow, &inner_window);
2428 if(NS_FAILED(nsres)) {
2429 ERR("Could not get inner window: %08x\n", nsres);
2430 return NULL;
2433 nsres = mozIDOMWindow_QueryInterface(inner_window, &IID_nsIGlobalObject, (void **)&nsglo);
2434 mozIDOMWindow_Release(inner_window);
2435 assert(nsres == NS_OK);
2437 nsres = nsIGlobalObject_QueryInterface(nsglo, &IID_nsIScriptObjectPrincipal, (void**)&sop);
2438 assert(nsres == NS_OK);
2440 nspri = nsIScriptObjectPrincipal_GetPrincipal(sop);
2441 nsIScriptObjectPrincipal_Release(sop);
2443 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
2444 NS_XMLHTTPREQUEST_CONTRACTID, NULL, &IID_nsIXMLHttpRequest,
2445 (void**)&nsxhr);
2446 if(NS_SUCCEEDED(nsres)) {
2447 nsres = nsIXMLHttpRequest_Init(nsxhr, nspri, NULL, nsglo, NULL, NULL);
2448 if(NS_FAILED(nsres))
2449 nsIXMLHttpRequest_Release(nsxhr);
2451 nsISupports_Release(nspri);
2452 nsIGlobalObject_Release(nsglo);
2453 if(NS_FAILED(nsres)) {
2454 ERR("nsIXMLHttpRequest_Init failed: %08x\n", nsres);
2455 return NULL;
2458 return nsxhr;