msvcirt: Add implementation of streambuf::unbuffered.
[wine.git] / dlls / mshtml / nsembed.c
blobd166ef34a633384f3d9e19a3c0774644e71e5674
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 "config.h"
21 #include <stdarg.h>
22 #include <assert.h>
24 #define COBJMACROS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "winreg.h"
30 #include "ole2.h"
31 #include "shlobj.h"
32 #include "shlwapi.h"
34 #include "wine/debug.h"
36 #include "mshtml_private.h"
37 #include "htmlevent.h"
38 #include "binding.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
41 WINE_DECLARE_DEBUG_CHANNEL(gecko);
43 #define NS_APPSTARTUPNOTIFIER_CONTRACTID "@mozilla.org/embedcomp/appstartup-notifier;1"
44 #define NS_WEBBROWSER_CONTRACTID "@mozilla.org/embedding/browser/nsWebBrowser;1"
45 #define NS_COMMANDPARAMS_CONTRACTID "@mozilla.org/embedcomp/command-params;1"
46 #define NS_HTMLSERIALIZER_CONTRACTID "@mozilla.org/layout/contentserializer;1?mimetype=text/html"
47 #define NS_EDITORCONTROLLER_CONTRACTID "@mozilla.org/editor/editorcontroller;1"
48 #define NS_PREFERENCES_CONTRACTID "@mozilla.org/preferences;1"
49 #define NS_VARIANT_CONTRACTID "@mozilla.org/variant;1"
50 #define NS_CATEGORYMANAGER_CONTRACTID "@mozilla.org/categorymanager;1"
52 #define PR_UINT32_MAX 0xffffffff
54 #define NS_STRING_CONTAINER_INIT_DEPEND 0x0002
55 #define NS_CSTRING_CONTAINER_INIT_DEPEND 0x0002
57 typedef UINT32 PRUint32;
59 static nsresult (CDECL *NS_InitXPCOM2)(nsIServiceManager**,void*,void*);
60 static nsresult (CDECL *NS_ShutdownXPCOM)(nsIServiceManager*);
61 static nsresult (CDECL *NS_GetComponentRegistrar)(nsIComponentRegistrar**);
62 static nsresult (CDECL *NS_StringContainerInit2)(nsStringContainer*,const PRUnichar*,PRUint32,PRUint32);
63 static nsresult (CDECL *NS_CStringContainerInit2)(nsCStringContainer*,const char*,PRUint32,PRUint32);
64 static nsresult (CDECL *NS_StringContainerFinish)(nsStringContainer*);
65 static nsresult (CDECL *NS_CStringContainerFinish)(nsCStringContainer*);
66 static nsresult (CDECL *NS_StringSetData)(nsAString*,const PRUnichar*,PRUint32);
67 static nsresult (CDECL *NS_CStringSetData)(nsACString*,const char*,PRUint32);
68 static nsresult (CDECL *NS_NewLocalFile)(const nsAString*,cpp_bool,nsIFile**);
69 static PRUint32 (CDECL *NS_StringGetData)(const nsAString*,const PRUnichar **,cpp_bool*);
70 static PRUint32 (CDECL *NS_CStringGetData)(const nsACString*,const char**,cpp_bool*);
71 static void* (CDECL *NS_Alloc)(SIZE_T);
72 static void (CDECL *NS_Free)(void*);
74 static HINSTANCE xul_handle = NULL;
76 static nsIServiceManager *pServMgr = NULL;
77 static nsIComponentManager *pCompMgr = NULL;
78 static nsICategoryManager *cat_mgr;
79 static nsIFile *profile_directory, *plugin_directory;
81 static const WCHAR wszNsContainer[] = {'N','s','C','o','n','t','a','i','n','e','r',0};
83 static ATOM nscontainer_class;
84 static WCHAR gecko_path[MAX_PATH];
85 static unsigned gecko_path_len;
87 nsresult create_nsfile(const PRUnichar *path, nsIFile **ret)
89 nsAString str;
90 nsresult nsres;
92 nsAString_InitDepend(&str, path);
93 nsres = NS_NewLocalFile(&str, FALSE, ret);
94 nsAString_Finish(&str);
96 if(NS_FAILED(nsres))
97 WARN("NS_NewLocalFile failed: %08x\n", nsres);
98 return nsres;
101 typedef struct {
102 nsISimpleEnumerator nsISimpleEnumerator_iface;
103 LONG ref;
104 nsISupports *value;
105 } nsSingletonEnumerator;
107 static inline nsSingletonEnumerator *impl_from_nsISimpleEnumerator(nsISimpleEnumerator *iface)
109 return CONTAINING_RECORD(iface, nsSingletonEnumerator, nsISimpleEnumerator_iface);
112 static nsresult NSAPI nsSingletonEnumerator_QueryInterface(nsISimpleEnumerator *iface, nsIIDRef riid, void **ppv)
114 nsSingletonEnumerator *This = impl_from_nsISimpleEnumerator(iface);
116 if(IsEqualGUID(&IID_nsISupports, riid)) {
117 TRACE("(%p)->(IID_nsISupports %p)\n", This, ppv);
118 *ppv = &This->nsISimpleEnumerator_iface;
119 }else if(IsEqualGUID(&IID_nsISimpleEnumerator, riid)) {
120 TRACE("(%p)->(IID_nsISimpleEnumerator %p)\n", This, ppv);
121 *ppv = &This->nsISimpleEnumerator_iface;
122 }else {
123 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
124 *ppv = NULL;
125 return NS_NOINTERFACE;
128 nsISupports_AddRef((nsISupports*)*ppv);
129 return NS_OK;
132 static nsrefcnt NSAPI nsSingletonEnumerator_AddRef(nsISimpleEnumerator *iface)
134 nsSingletonEnumerator *This = impl_from_nsISimpleEnumerator(iface);
135 nsrefcnt ref = InterlockedIncrement(&This->ref);
137 TRACE("(%p) ref=%d\n", This, ref);
139 return ref;
142 static nsrefcnt NSAPI nsSingletonEnumerator_Release(nsISimpleEnumerator *iface)
144 nsSingletonEnumerator *This = impl_from_nsISimpleEnumerator(iface);
145 nsrefcnt ref = InterlockedDecrement(&This->ref);
147 TRACE("(%p) ref=%d\n", This, ref);
149 if(!ref) {
150 if(This->value)
151 nsISupports_Release(This->value);
152 heap_free(This);
155 return ref;
158 static nsresult NSAPI nsSingletonEnumerator_HasMoreElements(nsISimpleEnumerator *iface, cpp_bool *_retval)
160 nsSingletonEnumerator *This = impl_from_nsISimpleEnumerator(iface);
162 TRACE("(%p)->()\n", This);
164 *_retval = This->value != NULL;
165 return NS_OK;
168 static nsresult NSAPI nsSingletonEnumerator_GetNext(nsISimpleEnumerator *iface, nsISupports **_retval)
170 nsSingletonEnumerator *This = impl_from_nsISimpleEnumerator(iface);
172 TRACE("(%p)->()\n", This);
174 if(!This->value)
175 return NS_ERROR_UNEXPECTED;
177 *_retval = This->value;
178 This->value = NULL;
179 return NS_OK;
182 static const nsISimpleEnumeratorVtbl nsSingletonEnumeratorVtbl = {
183 nsSingletonEnumerator_QueryInterface,
184 nsSingletonEnumerator_AddRef,
185 nsSingletonEnumerator_Release,
186 nsSingletonEnumerator_HasMoreElements,
187 nsSingletonEnumerator_GetNext
190 static nsISimpleEnumerator *create_singleton_enumerator(nsISupports *value)
192 nsSingletonEnumerator *ret;
194 ret = heap_alloc(sizeof(*ret));
195 if(!ret)
196 return NULL;
198 ret->nsISimpleEnumerator_iface.lpVtbl = &nsSingletonEnumeratorVtbl;
199 ret->ref = 1;
201 if(value)
202 nsISupports_AddRef(value);
203 ret->value = value;
204 return &ret->nsISimpleEnumerator_iface;
207 static nsresult NSAPI nsDirectoryServiceProvider2_QueryInterface(nsIDirectoryServiceProvider2 *iface,
208 nsIIDRef riid, void **result)
210 if(IsEqualGUID(&IID_nsISupports, riid)) {
211 TRACE("(IID_nsISupports %p)\n", result);
212 *result = iface;
213 }else if(IsEqualGUID(&IID_nsIDirectoryServiceProvider, riid)) {
214 TRACE("(IID_nsIDirectoryServiceProvider %p)\n", result);
215 *result = iface;
216 }else if(IsEqualGUID(&IID_nsIDirectoryServiceProvider2, riid)) {
217 TRACE("(IID_nsIDirectoryServiceProvider2 %p)\n", result);
218 *result = iface;
219 }else {
220 WARN("(%s %p)\n", debugstr_guid(riid), result);
221 *result = NULL;
222 return NS_NOINTERFACE;
225 nsISupports_AddRef((nsISupports*)*result);
226 return NS_OK;
229 static nsrefcnt NSAPI nsDirectoryServiceProvider2_AddRef(nsIDirectoryServiceProvider2 *iface)
231 return 2;
234 static nsrefcnt NSAPI nsDirectoryServiceProvider2_Release(nsIDirectoryServiceProvider2 *iface)
236 return 1;
239 static nsresult create_profile_directory(void)
241 static const WCHAR wine_geckoW[] = {'\\','w','i','n','e','_','g','e','c','k','o',0};
243 WCHAR path[MAX_PATH + sizeof(wine_geckoW)/sizeof(WCHAR)];
244 cpp_bool exists;
245 nsresult nsres;
246 HRESULT hres;
248 hres = SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, path);
249 if(FAILED(hres)) {
250 ERR("SHGetFolderPath failed: %08x\n", hres);
251 return NS_ERROR_FAILURE;
254 strcatW(path, wine_geckoW);
255 nsres = create_nsfile(path, &profile_directory);
256 if(NS_FAILED(nsres))
257 return nsres;
259 nsres = nsIFile_Exists(profile_directory, &exists);
260 if(NS_FAILED(nsres)) {
261 ERR("Exists failed: %08x\n", nsres);
262 return nsres;
265 if(!exists) {
266 nsres = nsIFile_Create(profile_directory, 1, 0700);
267 if(NS_FAILED(nsres))
268 ERR("Create failed: %08x\n", nsres);
271 return nsres;
274 static nsresult NSAPI nsDirectoryServiceProvider2_GetFile(nsIDirectoryServiceProvider2 *iface,
275 const char *prop, cpp_bool *persistent, nsIFile **_retval)
277 TRACE("(%s %p %p)\n", debugstr_a(prop), persistent, _retval);
279 if(!strcmp(prop, "ProfD")) {
280 if(!profile_directory) {
281 nsresult nsres;
283 nsres = create_profile_directory();
284 if(NS_FAILED(nsres))
285 return nsres;
288 assert(profile_directory != NULL);
289 return nsIFile_Clone(profile_directory, _retval);
292 *_retval = NULL;
293 return NS_ERROR_FAILURE;
296 static nsresult NSAPI nsDirectoryServiceProvider2_GetFiles(nsIDirectoryServiceProvider2 *iface,
297 const char *prop, nsISimpleEnumerator **_retval)
299 TRACE("(%s %p)\n", debugstr_a(prop), _retval);
301 if(!strcmp(prop, "APluginsDL")) {
302 WCHAR plugin_path[MAX_PATH];
303 nsIFile *file;
304 int len;
305 nsresult nsres;
307 if(!plugin_directory) {
308 static const WCHAR gecko_pluginW[] = {'\\','g','e','c','k','o','\\','p','l','u','g','i','n',0};
310 len = GetSystemDirectoryW(plugin_path, (sizeof(plugin_path)-sizeof(gecko_pluginW))/sizeof(WCHAR)+1);
311 if(!len)
312 return NS_ERROR_UNEXPECTED;
314 strcpyW(plugin_path+len, gecko_pluginW);
315 nsres = create_nsfile(plugin_path, &plugin_directory);
316 if(NS_FAILED(nsres)) {
317 *_retval = NULL;
318 return nsres;
322 nsres = nsIFile_Clone(plugin_directory, &file);
323 if(NS_FAILED(nsres))
324 return nsres;
326 *_retval = create_singleton_enumerator((nsISupports*)file);
327 nsIFile_Release(file);
328 if(!*_retval)
329 return NS_ERROR_OUT_OF_MEMORY;
331 return NS_OK;
334 *_retval = NULL;
335 return NS_ERROR_FAILURE;
338 static const nsIDirectoryServiceProvider2Vtbl nsDirectoryServiceProvider2Vtbl = {
339 nsDirectoryServiceProvider2_QueryInterface,
340 nsDirectoryServiceProvider2_AddRef,
341 nsDirectoryServiceProvider2_Release,
342 nsDirectoryServiceProvider2_GetFile,
343 nsDirectoryServiceProvider2_GetFiles
346 static nsIDirectoryServiceProvider2 nsDirectoryServiceProvider2 =
347 { &nsDirectoryServiceProvider2Vtbl };
349 static LRESULT WINAPI nsembed_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
351 NSContainer *This;
352 nsresult nsres;
354 static const WCHAR wszTHIS[] = {'T','H','I','S',0};
356 if(msg == WM_CREATE) {
357 This = *(NSContainer**)lParam;
358 SetPropW(hwnd, wszTHIS, This);
359 }else {
360 This = GetPropW(hwnd, wszTHIS);
363 switch(msg) {
364 case WM_SIZE:
365 TRACE("(%p)->(WM_SIZE)\n", This);
367 nsres = nsIBaseWindow_SetSize(This->window,
368 LOWORD(lParam), HIWORD(lParam), TRUE);
369 if(NS_FAILED(nsres))
370 WARN("SetSize failed: %08x\n", nsres);
371 break;
373 case WM_PARENTNOTIFY:
374 TRACE("WM_PARENTNOTIFY %x\n", (unsigned)wParam);
376 switch(wParam) {
377 case WM_LBUTTONDOWN:
378 case WM_RBUTTONDOWN:
379 nsIWebBrowserFocus_Activate(This->focus);
383 return DefWindowProcW(hwnd, msg, wParam, lParam);
387 static void register_nscontainer_class(void)
389 static WNDCLASSEXW wndclass = {
390 sizeof(WNDCLASSEXW),
391 CS_DBLCLKS,
392 nsembed_proc,
393 0, 0, NULL, NULL, NULL, NULL, NULL,
394 wszNsContainer,
395 NULL,
397 wndclass.hInstance = hInst;
398 nscontainer_class = RegisterClassExW(&wndclass);
401 static BOOL install_wine_gecko(void)
403 PROCESS_INFORMATION pi;
404 STARTUPINFOW si;
405 WCHAR app[MAX_PATH];
406 WCHAR *args;
407 LONG len;
408 BOOL ret;
410 static const WCHAR controlW[] = {'\\','c','o','n','t','r','o','l','.','e','x','e',0};
411 static const WCHAR argsW[] =
412 {' ','a','p','p','w','i','z','.','c','p','l',' ','i','n','s','t','a','l','l','_','g','e','c','k','o',0};
414 len = GetSystemDirectoryW(app, MAX_PATH-sizeof(controlW)/sizeof(WCHAR));
415 memcpy(app+len, controlW, sizeof(controlW));
417 args = heap_alloc(len*sizeof(WCHAR) + sizeof(controlW) + sizeof(argsW));
418 if(!args)
419 return FALSE;
421 memcpy(args, app, len*sizeof(WCHAR) + sizeof(controlW));
422 memcpy(args + len + sizeof(controlW)/sizeof(WCHAR)-1, argsW, sizeof(argsW));
424 TRACE("starting %s\n", debugstr_w(args));
426 memset(&si, 0, sizeof(si));
427 si.cb = sizeof(si);
428 ret = CreateProcessW(app, args, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
429 heap_free(args);
430 if (ret) {
431 CloseHandle(pi.hThread);
432 WaitForSingleObject(pi.hProcess, INFINITE);
433 CloseHandle(pi.hProcess);
436 return ret;
439 static void set_environment(LPCWSTR gre_path)
441 size_t len, gre_path_len;
442 int debug_level = 0;
443 WCHAR *path, buf[20];
444 const WCHAR *ptr;
446 static const WCHAR pathW[] = {'P','A','T','H',0};
447 static const WCHAR warnW[] = {'w','a','r','n',0};
448 static const WCHAR xpcom_debug_breakW[] =
449 {'X','P','C','O','M','_','D','E','B','U','G','_','B','R','E','A','K',0};
450 static const WCHAR nspr_log_modulesW[] =
451 {'N','S','P','R','_','L','O','G','_','M','O','D','U','L','E','S',0};
452 static const WCHAR debug_formatW[] = {'a','l','l',':','%','d',0};
454 SetEnvironmentVariableW(xpcom_debug_breakW, warnW);
456 if(TRACE_ON(gecko))
457 debug_level = 5;
458 else if(WARN_ON(gecko))
459 debug_level = 3;
460 else if(ERR_ON(gecko))
461 debug_level = 2;
463 sprintfW(buf, debug_formatW, debug_level);
464 SetEnvironmentVariableW(nspr_log_modulesW, buf);
466 len = GetEnvironmentVariableW(pathW, NULL, 0);
467 gre_path_len = strlenW(gre_path);
468 path = heap_alloc((len+gre_path_len+1)*sizeof(WCHAR));
469 if(!path)
470 return;
471 GetEnvironmentVariableW(pathW, path, len);
473 /* We have to modify PATH as xul.dll loads other DLLs from this directory. */
474 if(!(ptr = strstrW(path, gre_path))
475 || (ptr > path && *(ptr-1) != ';')
476 || (ptr[gre_path_len] && ptr[gre_path_len] != ';')) {
477 if(len)
478 path[len-1] = ';';
479 strcpyW(path+len, gre_path);
480 SetEnvironmentVariableW(pathW, path);
484 static BOOL load_xul(const PRUnichar *gre_path)
486 static const WCHAR xul_dllW[] = {'\\','x','u','l','.','d','l','l',0};
487 WCHAR file_name[MAX_PATH];
489 strcpyW(file_name, gre_path);
490 strcatW(file_name, xul_dllW);
492 TRACE("(%s)\n", debugstr_w(file_name));
494 set_environment(gre_path);
496 xul_handle = LoadLibraryExW(file_name, 0, LOAD_WITH_ALTERED_SEARCH_PATH);
497 if(!xul_handle) {
498 WARN("Could not load XUL: %d\n", GetLastError());
499 return FALSE;
502 #define NS_DLSYM(func) \
503 func = (void *)GetProcAddress(xul_handle, #func); \
504 if(!func) \
505 ERR("Could not GetProcAddress(" #func ") failed\n")
507 NS_DLSYM(NS_InitXPCOM2);
508 NS_DLSYM(NS_ShutdownXPCOM);
509 NS_DLSYM(NS_GetComponentRegistrar);
510 NS_DLSYM(NS_StringContainerInit2);
511 NS_DLSYM(NS_CStringContainerInit2);
512 NS_DLSYM(NS_StringContainerFinish);
513 NS_DLSYM(NS_CStringContainerFinish);
514 NS_DLSYM(NS_StringSetData);
515 NS_DLSYM(NS_CStringSetData);
516 NS_DLSYM(NS_NewLocalFile);
517 NS_DLSYM(NS_StringGetData);
518 NS_DLSYM(NS_CStringGetData);
519 NS_DLSYM(NS_Alloc);
520 NS_DLSYM(NS_Free);
521 NS_DLSYM(ccref_incr);
522 NS_DLSYM(ccref_decr);
523 NS_DLSYM(ccref_init);
524 NS_DLSYM(ccp_init);
525 NS_DLSYM(describe_cc_node);
526 NS_DLSYM(note_cc_edge);
528 #undef NS_DLSYM
530 return TRUE;
533 static BOOL check_version(LPCWSTR gre_path, const char *version_string)
535 WCHAR file_name[MAX_PATH];
536 char version[128];
537 DWORD read=0;
538 HANDLE hfile;
540 static const WCHAR wszVersion[] = {'\\','V','E','R','S','I','O','N',0};
542 strcpyW(file_name, gre_path);
543 strcatW(file_name, wszVersion);
545 hfile = CreateFileW(file_name, GENERIC_READ, FILE_SHARE_READ, NULL,
546 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
547 if(hfile == INVALID_HANDLE_VALUE) {
548 ERR("Could not open VERSION file\n");
549 return FALSE;
552 ReadFile(hfile, version, sizeof(version), &read, NULL);
553 version[read] = 0;
554 CloseHandle(hfile);
556 TRACE("%s\n", debugstr_a(version));
558 if(strcmp(version, version_string)) {
559 ERR("Unexpected version %s, expected %s\n", debugstr_a(version),
560 debugstr_a(version_string));
561 return FALSE;
564 return TRUE;
567 static BOOL load_wine_gecko_v(PRUnichar *gre_path, HKEY mshtml_key,
568 const char *version, const char *version_string)
570 DWORD res, type, size = MAX_PATH;
571 HKEY hkey = mshtml_key;
573 static const WCHAR wszGeckoPath[] =
574 {'G','e','c','k','o','P','a','t','h',0};
576 if(version) {
577 /* @@ Wine registry key: HKLM\Software\Wine\MSHTML\<version> */
578 res = RegOpenKeyA(mshtml_key, version, &hkey);
579 if(res != ERROR_SUCCESS)
580 return FALSE;
583 res = RegQueryValueExW(hkey, wszGeckoPath, NULL, &type, (LPBYTE)gre_path, &size);
584 if(hkey != mshtml_key)
585 RegCloseKey(hkey);
586 if(res != ERROR_SUCCESS || type != REG_SZ)
587 return FALSE;
589 if(!check_version(gre_path, version_string))
590 return FALSE;
592 return load_xul(gre_path);
595 static BOOL load_wine_gecko(PRUnichar *gre_path)
597 HKEY hkey;
598 DWORD res;
599 BOOL ret;
601 static const WCHAR wszMshtmlKey[] = {
602 'S','o','f','t','w','a','r','e','\\','W','i','n','e',
603 '\\','M','S','H','T','M','L',0};
605 /* @@ Wine registry key: HKLM\Software\Wine\MSHTML */
606 res = RegOpenKeyW(HKEY_LOCAL_MACHINE, wszMshtmlKey, &hkey);
607 if(res != ERROR_SUCCESS)
608 return FALSE;
610 ret = load_wine_gecko_v(gre_path, hkey, GECKO_VERSION, GECKO_VERSION_STRING);
612 RegCloseKey(hkey);
613 return ret;
616 static void set_bool_pref(nsIPrefBranch *pref, const char *pref_name, BOOL val)
618 nsresult nsres;
620 nsres = nsIPrefBranch_SetBoolPref(pref, pref_name, val);
621 if(NS_FAILED(nsres))
622 ERR("Could not set pref %s\n", debugstr_a(pref_name));
625 static void set_int_pref(nsIPrefBranch *pref, const char *pref_name, int val)
627 nsresult nsres;
629 nsres = nsIPrefBranch_SetIntPref(pref, pref_name, val);
630 if(NS_FAILED(nsres))
631 ERR("Could not set pref %s\n", debugstr_a(pref_name));
634 static void set_string_pref(nsIPrefBranch *pref, const char *pref_name, const char *val)
636 nsresult nsres;
638 nsres = nsIPrefBranch_SetCharPref(pref, pref_name, val);
639 if(NS_FAILED(nsres))
640 ERR("Could not set pref %s\n", debugstr_a(pref_name));
643 static void set_lang(nsIPrefBranch *pref)
645 char langs[100];
646 DWORD res, size, type;
647 HKEY hkey;
649 static const WCHAR international_keyW[] =
650 {'S','o','f','t','w','a','r','e',
651 '\\','M','i','c','r','o','s','o','f','t',
652 '\\','I','n','t','e','r','n','e','t',' ','E','x','p','l','o','r','e','r',
653 '\\','I','n','t','e','r','n','a','t','i','o','n','a','l',0};
655 res = RegOpenKeyW(HKEY_CURRENT_USER, international_keyW, &hkey);
656 if(res != ERROR_SUCCESS)
657 return;
659 size = sizeof(langs);
660 res = RegQueryValueExA(hkey, "AcceptLanguage", 0, &type, (LPBYTE)langs, &size);
661 RegCloseKey(hkey);
662 if(res != ERROR_SUCCESS || type != REG_SZ)
663 return;
665 TRACE("Setting lang %s\n", debugstr_a(langs));
667 set_string_pref(pref, "intl.accept_languages", langs);
670 static void set_preferences(void)
672 nsIPrefBranch *pref;
673 nsresult nsres;
675 nsres = nsIServiceManager_GetServiceByContractID(pServMgr, NS_PREFERENCES_CONTRACTID,
676 &IID_nsIPrefBranch, (void**)&pref);
677 if(NS_FAILED(nsres)) {
678 ERR("Could not get preference service: %08x\n", nsres);
679 return;
682 set_lang(pref);
683 set_bool_pref(pref, "security.warn_entering_secure", FALSE);
684 set_bool_pref(pref, "security.warn_submit_insecure", FALSE);
685 set_int_pref(pref, "layout.spellcheckDefault", 0);
687 nsIPrefBranch_Release(pref);
690 static BOOL init_xpcom(const PRUnichar *gre_path)
692 nsIComponentRegistrar *registrar = NULL;
693 nsIFile *gre_dir;
694 WCHAR *ptr;
695 nsresult nsres;
697 nsres = create_nsfile(gre_path, &gre_dir);
698 if(NS_FAILED(nsres)) {
699 FreeLibrary(xul_handle);
700 return FALSE;
703 nsres = NS_InitXPCOM2(&pServMgr, gre_dir, (nsIDirectoryServiceProvider*)&nsDirectoryServiceProvider2);
704 if(NS_FAILED(nsres)) {
705 ERR("NS_InitXPCOM2 failed: %08x\n", nsres);
706 FreeLibrary(xul_handle);
707 return FALSE;
710 strcpyW(gecko_path, gre_path);
711 for(ptr = gecko_path; *ptr; ptr++) {
712 if(*ptr == '\\')
713 *ptr = '/';
715 gecko_path_len = ptr-gecko_path;
717 nsres = nsIServiceManager_QueryInterface(pServMgr, &IID_nsIComponentManager, (void**)&pCompMgr);
718 if(NS_FAILED(nsres))
719 ERR("Could not get nsIComponentManager: %08x\n", nsres);
721 nsres = NS_GetComponentRegistrar(&registrar);
722 if(NS_SUCCEEDED(nsres))
723 init_nsio(pCompMgr, registrar);
724 else
725 ERR("NS_GetComponentRegistrar failed: %08x\n", nsres);
727 init_mutation(pCompMgr);
728 set_preferences();
730 nsres = nsIServiceManager_GetServiceByContractID(pServMgr, NS_CATEGORYMANAGER_CONTRACTID,
731 &IID_nsICategoryManager, (void**)&cat_mgr);
732 if(NS_FAILED(nsres))
733 ERR("Could not get category manager service: %08x\n", nsres);
735 if(registrar) {
736 register_nsservice(registrar, pServMgr);
737 nsIComponentRegistrar_Release(registrar);
740 init_node_cc();
742 return TRUE;
745 static CRITICAL_SECTION cs_load_gecko;
746 static CRITICAL_SECTION_DEBUG cs_load_gecko_dbg =
748 0, 0, &cs_load_gecko,
749 { &cs_load_gecko_dbg.ProcessLocksList, &cs_load_gecko_dbg.ProcessLocksList },
750 0, 0, { (DWORD_PTR)(__FILE__ ": load_gecko") }
752 static CRITICAL_SECTION cs_load_gecko = { &cs_load_gecko_dbg, -1, 0, 0, 0, 0 };
754 BOOL load_gecko(void)
756 PRUnichar gre_path[MAX_PATH];
757 BOOL ret = FALSE;
759 static DWORD loading_thread;
761 TRACE("()\n");
763 /* load_gecko may be called recursively */
764 if(loading_thread == GetCurrentThreadId())
765 return pCompMgr != NULL;
767 EnterCriticalSection(&cs_load_gecko);
769 if(!loading_thread) {
770 loading_thread = GetCurrentThreadId();
772 if(load_wine_gecko(gre_path)
773 || (install_wine_gecko() && load_wine_gecko(gre_path)))
774 ret = init_xpcom(gre_path);
775 else
776 MESSAGE("Could not load wine-gecko. HTML rendering will be disabled.\n");
777 }else {
778 ret = pCompMgr != NULL;
781 LeaveCriticalSection(&cs_load_gecko);
783 return ret;
786 void *nsalloc(size_t size)
788 return NS_Alloc(size);
791 void nsfree(void *mem)
793 NS_Free(mem);
796 static BOOL nsACString_Init(nsACString *str, const char *data)
798 return NS_SUCCEEDED(NS_CStringContainerInit2(str, data, PR_UINT32_MAX, 0));
802 * Initializes nsACString with data owned by caller.
803 * Caller must ensure that data is valid during lifetime of string object.
805 void nsACString_InitDepend(nsACString *str, const char *data)
807 NS_CStringContainerInit2(str, data, PR_UINT32_MAX, NS_CSTRING_CONTAINER_INIT_DEPEND);
810 void nsACString_SetData(nsACString *str, const char *data)
812 NS_CStringSetData(str, data, PR_UINT32_MAX);
815 UINT32 nsACString_GetData(const nsACString *str, const char **data)
817 return NS_CStringGetData(str, data, NULL);
820 void nsACString_Finish(nsACString *str)
822 NS_CStringContainerFinish(str);
825 BOOL nsAString_Init(nsAString *str, const PRUnichar *data)
827 return NS_SUCCEEDED(NS_StringContainerInit2(str, data, PR_UINT32_MAX, 0));
831 * Initializes nsAString with data owned by caller.
832 * Caller must ensure that data is valid during lifetime of string object.
834 void nsAString_InitDepend(nsAString *str, const PRUnichar *data)
836 NS_StringContainerInit2(str, data, PR_UINT32_MAX, NS_STRING_CONTAINER_INIT_DEPEND);
839 UINT32 nsAString_GetData(const nsAString *str, const PRUnichar **data)
841 return NS_StringGetData(str, data, NULL);
844 void nsAString_Finish(nsAString *str)
846 NS_StringContainerFinish(str);
849 HRESULT return_nsstr(nsresult nsres, nsAString *nsstr, BSTR *p)
851 const PRUnichar *str;
853 if(NS_FAILED(nsres)) {
854 ERR("failed: %08x\n", nsres);
855 nsAString_Finish(nsstr);
856 return E_FAIL;
859 nsAString_GetData(nsstr, &str);
860 TRACE("ret %s\n", debugstr_w(str));
861 if(*str) {
862 *p = SysAllocString(str);
863 if(!*p)
864 return E_OUTOFMEMORY;
865 }else {
866 *p = NULL;
869 nsAString_Finish(nsstr);
870 return S_OK;
873 nsICommandParams *create_nscommand_params(void)
875 nsICommandParams *ret = NULL;
876 nsresult nsres;
878 if(!pCompMgr)
879 return NULL;
881 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
882 NS_COMMANDPARAMS_CONTRACTID, NULL, &IID_nsICommandParams,
883 (void**)&ret);
884 if(NS_FAILED(nsres))
885 ERR("Could not get nsICommandParams\n");
887 return ret;
890 nsIWritableVariant *create_nsvariant(void)
892 nsIWritableVariant *ret = NULL;
893 nsresult nsres;
895 if(!pCompMgr)
896 return NULL;
898 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
899 NS_VARIANT_CONTRACTID, NULL, &IID_nsIWritableVariant, (void**)&ret);
900 if(NS_FAILED(nsres))
901 ERR("Could not get nsIVariant\n");
903 return ret;
906 char *get_nscategory_entry(const char *category, const char *entry)
908 char *ret = NULL;
909 nsresult nsres;
911 nsres = nsICategoryManager_GetCategoryEntry(cat_mgr, category, entry, &ret);
912 return NS_SUCCEEDED(nsres) ? ret : NULL;
915 nsresult get_nsinterface(nsISupports *iface, REFIID riid, void **ppv)
917 nsIInterfaceRequestor *iface_req;
918 nsresult nsres;
920 nsres = nsISupports_QueryInterface(iface, &IID_nsIInterfaceRequestor, (void**)&iface_req);
921 if(NS_FAILED(nsres))
922 return nsres;
924 nsres = nsIInterfaceRequestor_GetInterface(iface_req, riid, ppv);
925 nsIInterfaceRequestor_Release(iface_req);
927 return nsres;
930 static HRESULT nsnode_to_nsstring_rec(nsIContentSerializer *serializer, nsIDOMNode *nsnode, nsAString *str)
932 nsIDOMNodeList *node_list = NULL;
933 cpp_bool has_children = FALSE;
934 nsIContent *nscontent;
935 UINT16 type;
936 nsresult nsres;
938 nsIDOMNode_HasChildNodes(nsnode, &has_children);
940 nsres = nsIDOMNode_GetNodeType(nsnode, &type);
941 if(NS_FAILED(nsres)) {
942 ERR("GetType failed: %08x\n", nsres);
943 return E_FAIL;
946 if(type != DOCUMENT_NODE) {
947 nsres = nsIDOMNode_QueryInterface(nsnode, &IID_nsIContent, (void**)&nscontent);
948 if(NS_FAILED(nsres)) {
949 ERR("Could not get nsIContent interface: %08x\n", nsres);
950 return E_FAIL;
954 switch(type) {
955 case ELEMENT_NODE:
956 nsIContentSerializer_AppendElementStart(serializer, nscontent, nscontent, str);
957 break;
958 case TEXT_NODE:
959 nsIContentSerializer_AppendText(serializer, nscontent, 0, -1, str);
960 break;
961 case COMMENT_NODE:
962 nsres = nsIContentSerializer_AppendComment(serializer, nscontent, 0, -1, str);
963 break;
964 case DOCUMENT_NODE: {
965 nsIDocument *nsdoc;
966 nsIDOMNode_QueryInterface(nsnode, &IID_nsIDocument, (void**)&nsdoc);
967 nsIContentSerializer_AppendDocumentStart(serializer, nsdoc, str);
968 nsIDocument_Release(nsdoc);
969 break;
971 case DOCUMENT_TYPE_NODE:
972 nsIContentSerializer_AppendDoctype(serializer, nscontent, str);
973 break;
974 case DOCUMENT_FRAGMENT_NODE:
975 break;
976 default:
977 FIXME("Unhandled type %u\n", type);
980 if(has_children) {
981 UINT32 child_cnt, i;
982 nsIDOMNode *child_node;
984 nsIDOMNode_GetChildNodes(nsnode, &node_list);
985 nsIDOMNodeList_GetLength(node_list, &child_cnt);
987 for(i=0; i<child_cnt; i++) {
988 nsres = nsIDOMNodeList_Item(node_list, i, &child_node);
989 if(NS_SUCCEEDED(nsres)) {
990 nsnode_to_nsstring_rec(serializer, child_node, str);
991 nsIDOMNode_Release(child_node);
992 }else {
993 ERR("Item failed: %08x\n", nsres);
997 nsIDOMNodeList_Release(node_list);
1000 if(type == ELEMENT_NODE)
1001 nsIContentSerializer_AppendElementEnd(serializer, nscontent, str);
1003 if(type != DOCUMENT_NODE)
1004 nsIContent_Release(nscontent);
1005 return S_OK;
1008 HRESULT nsnode_to_nsstring(nsIDOMNode *nsnode, nsAString *str)
1010 nsIContentSerializer *serializer;
1011 nsresult nsres;
1012 HRESULT hres;
1014 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
1015 NS_HTMLSERIALIZER_CONTRACTID, NULL, &IID_nsIContentSerializer,
1016 (void**)&serializer);
1017 if(NS_FAILED(nsres)) {
1018 ERR("Could not get nsIContentSerializer: %08x\n", nsres);
1019 return E_FAIL;
1022 nsres = nsIContentSerializer_Init(serializer, 0, 100, NULL, FALSE, FALSE /* FIXME */);
1023 if(NS_FAILED(nsres))
1024 ERR("Init failed: %08x\n", nsres);
1026 hres = nsnode_to_nsstring_rec(serializer, nsnode, str);
1027 if(SUCCEEDED(hres)) {
1028 nsres = nsIContentSerializer_Flush(serializer, str);
1029 if(NS_FAILED(nsres))
1030 ERR("Flush failed: %08x\n", nsres);
1033 nsIContentSerializer_Release(serializer);
1034 return hres;
1037 void get_editor_controller(NSContainer *This)
1039 nsIEditingSession *editing_session = NULL;
1040 nsIControllerContext *ctrlctx;
1041 nsresult nsres;
1043 if(This->editor) {
1044 nsIEditor_Release(This->editor);
1045 This->editor = NULL;
1048 if(This->editor_controller) {
1049 nsIController_Release(This->editor_controller);
1050 This->editor_controller = NULL;
1053 nsres = get_nsinterface((nsISupports*)This->webbrowser, &IID_nsIEditingSession,
1054 (void**)&editing_session);
1055 if(NS_FAILED(nsres)) {
1056 ERR("Could not get nsIEditingSession: %08x\n", nsres);
1057 return;
1060 nsres = nsIEditingSession_GetEditorForWindow(editing_session,
1061 This->doc->basedoc.window->nswindow, &This->editor);
1062 nsIEditingSession_Release(editing_session);
1063 if(NS_FAILED(nsres)) {
1064 ERR("Could not get editor: %08x\n", nsres);
1065 return;
1068 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
1069 NS_EDITORCONTROLLER_CONTRACTID, NULL, &IID_nsIControllerContext, (void**)&ctrlctx);
1070 if(NS_SUCCEEDED(nsres)) {
1071 nsres = nsIControllerContext_SetCommandContext(ctrlctx, (nsISupports *)This->editor);
1072 if(NS_FAILED(nsres))
1073 ERR("SetCommandContext failed: %08x\n", nsres);
1074 nsres = nsIControllerContext_QueryInterface(ctrlctx, &IID_nsIController,
1075 (void**)&This->editor_controller);
1076 nsIControllerContext_Release(ctrlctx);
1077 if(NS_FAILED(nsres))
1078 ERR("Could not get nsIController interface: %08x\n", nsres);
1079 }else {
1080 ERR("Could not create edit controller: %08x\n", nsres);
1084 void close_gecko(void)
1086 TRACE("()\n");
1088 release_nsio();
1089 init_mutation(NULL);
1091 if(profile_directory) {
1092 nsIFile_Release(profile_directory);
1093 profile_directory = NULL;
1096 if(plugin_directory) {
1097 nsIFile_Release(plugin_directory);
1098 plugin_directory = NULL;
1101 if(pCompMgr)
1102 nsIComponentManager_Release(pCompMgr);
1104 if(pServMgr)
1105 nsIServiceManager_Release(pServMgr);
1107 if(cat_mgr)
1108 nsICategoryManager_Release(cat_mgr);
1110 /* Gecko doesn't really support being unloaded */
1111 /* if (hXPCOM) FreeLibrary(hXPCOM); */
1113 DeleteCriticalSection(&cs_load_gecko);
1116 BOOL is_gecko_path(const char *path)
1118 WCHAR *buf, *ptr;
1119 BOOL ret;
1121 buf = heap_strdupUtoW(path);
1122 if(!buf || strlenW(buf) < gecko_path_len)
1123 return FALSE;
1125 for(ptr = buf; *ptr; ptr++) {
1126 if(*ptr == '\\')
1127 *ptr = '/';
1130 UrlUnescapeW(buf, NULL, NULL, URL_UNESCAPE_INPLACE);
1131 buf[gecko_path_len] = 0;
1133 ret = !strcmpiW(buf, gecko_path);
1134 heap_free(buf);
1135 return ret;
1138 void set_viewer_zoom(NSContainer *nscontainer, float factor)
1140 nsIContentViewer *content_viewer;
1141 nsIDocShell *doc_shell;
1142 nsresult nsres;
1144 TRACE("Setting to %f\n", factor);
1146 nsres = get_nsinterface((nsISupports*)nscontainer->navigation, &IID_nsIDocShell, (void**)&doc_shell);
1147 assert(nsres == NS_OK);
1149 nsres = nsIDocShell_GetContentViewer(doc_shell, &content_viewer);
1150 assert(nsres == NS_OK && content_viewer);
1151 nsIDocShell_Release(doc_shell);
1153 nsres = nsIContentViewer_SetFullZoom(content_viewer, factor);
1154 if(NS_FAILED(nsres))
1155 ERR("SetFullZoom failed: %08x\n", nsres);
1157 nsIContentViewer_Release(content_viewer);
1160 struct nsWeakReference {
1161 nsIWeakReference nsIWeakReference_iface;
1163 LONG ref;
1165 NSContainer *nscontainer;
1168 static inline nsWeakReference *impl_from_nsIWeakReference(nsIWeakReference *iface)
1170 return CONTAINING_RECORD(iface, nsWeakReference, nsIWeakReference_iface);
1173 static nsresult NSAPI nsWeakReference_QueryInterface(nsIWeakReference *iface,
1174 nsIIDRef riid, void **result)
1176 nsWeakReference *This = impl_from_nsIWeakReference(iface);
1178 if(IsEqualGUID(&IID_nsISupports, riid)) {
1179 TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
1180 *result = &This->nsIWeakReference_iface;
1181 }else if(IsEqualGUID(&IID_nsIWeakReference, riid)) {
1182 TRACE("(%p)->(IID_nsIWeakReference %p)\n", This, result);
1183 *result = &This->nsIWeakReference_iface;
1184 }else {
1185 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
1186 *result = NULL;
1187 return NS_NOINTERFACE;
1190 nsISupports_AddRef((nsISupports*)*result);
1191 return NS_OK;
1194 static nsrefcnt NSAPI nsWeakReference_AddRef(nsIWeakReference *iface)
1196 nsWeakReference *This = impl_from_nsIWeakReference(iface);
1197 LONG ref = InterlockedIncrement(&This->ref);
1199 TRACE("(%p) ref=%d\n", This, ref);
1201 return ref;
1204 static nsrefcnt NSAPI nsWeakReference_Release(nsIWeakReference *iface)
1206 nsWeakReference *This = impl_from_nsIWeakReference(iface);
1207 LONG ref = InterlockedIncrement(&This->ref);
1209 TRACE("(%p) ref=%d\n", This, ref);
1211 if(!ref) {
1212 assert(!This->nscontainer);
1213 heap_free(This);
1216 return ref;
1219 static nsresult NSAPI nsWeakReference_QueryReferent(nsIWeakReference *iface,
1220 const nsIID *riid, void **result)
1222 nsWeakReference *This = impl_from_nsIWeakReference(iface);
1224 if(!This->nscontainer)
1225 return NS_ERROR_NULL_POINTER;
1227 return nsIWebBrowserChrome_QueryInterface(&This->nscontainer->nsIWebBrowserChrome_iface, riid, result);
1230 static const nsIWeakReferenceVtbl nsWeakReferenceVtbl = {
1231 nsWeakReference_QueryInterface,
1232 nsWeakReference_AddRef,
1233 nsWeakReference_Release,
1234 nsWeakReference_QueryReferent
1237 /**********************************************************
1238 * nsIWebBrowserChrome interface
1241 static inline NSContainer *impl_from_nsIWebBrowserChrome(nsIWebBrowserChrome *iface)
1243 return CONTAINING_RECORD(iface, NSContainer, nsIWebBrowserChrome_iface);
1246 static nsresult NSAPI nsWebBrowserChrome_QueryInterface(nsIWebBrowserChrome *iface,
1247 nsIIDRef riid, void **result)
1249 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1251 *result = NULL;
1252 if(IsEqualGUID(&IID_nsISupports, riid)) {
1253 TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
1254 *result = &This->nsIWebBrowserChrome_iface;
1255 }else if(IsEqualGUID(&IID_nsIWebBrowserChrome, riid)) {
1256 TRACE("(%p)->(IID_nsIWebBrowserChrome, %p)\n", This, result);
1257 *result = &This->nsIWebBrowserChrome_iface;
1258 }else if(IsEqualGUID(&IID_nsIContextMenuListener, riid)) {
1259 TRACE("(%p)->(IID_nsIContextMenuListener, %p)\n", This, result);
1260 *result = &This->nsIContextMenuListener_iface;
1261 }else if(IsEqualGUID(&IID_nsIURIContentListener, riid)) {
1262 TRACE("(%p)->(IID_nsIURIContentListener %p)\n", This, result);
1263 *result = &This->nsIURIContentListener_iface;
1264 }else if(IsEqualGUID(&IID_nsIEmbeddingSiteWindow, riid)) {
1265 TRACE("(%p)->(IID_nsIEmbeddingSiteWindow %p)\n", This, result);
1266 *result = &This->nsIEmbeddingSiteWindow_iface;
1267 }else if(IsEqualGUID(&IID_nsITooltipListener, riid)) {
1268 TRACE("(%p)->(IID_nsITooltipListener %p)\n", This, result);
1269 *result = &This->nsITooltipListener_iface;
1270 }else if(IsEqualGUID(&IID_nsIInterfaceRequestor, riid)) {
1271 TRACE("(%p)->(IID_nsIInterfaceRequestor %p)\n", This, result);
1272 *result = &This->nsIInterfaceRequestor_iface;
1273 }else if(IsEqualGUID(&IID_nsISupportsWeakReference, riid)) {
1274 TRACE("(%p)->(IID_nsISupportsWeakReference %p)\n", This, result);
1275 *result = &This->nsISupportsWeakReference_iface;
1278 if(*result) {
1279 nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1280 return NS_OK;
1283 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
1284 return NS_NOINTERFACE;
1287 static nsrefcnt NSAPI nsWebBrowserChrome_AddRef(nsIWebBrowserChrome *iface)
1289 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1290 LONG ref = InterlockedIncrement(&This->ref);
1292 TRACE("(%p) ref=%d\n", This, ref);
1294 return ref;
1297 static nsrefcnt NSAPI nsWebBrowserChrome_Release(nsIWebBrowserChrome *iface)
1299 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1300 LONG ref = InterlockedDecrement(&This->ref);
1302 TRACE("(%p) ref=%d\n", This, ref);
1304 if(!ref) {
1305 if(This->parent)
1306 nsIWebBrowserChrome_Release(&This->parent->nsIWebBrowserChrome_iface);
1307 if(This->weak_reference) {
1308 This->weak_reference->nscontainer = NULL;
1309 nsIWeakReference_Release(&This->weak_reference->nsIWeakReference_iface);
1311 heap_free(This);
1314 return ref;
1317 static nsresult NSAPI nsWebBrowserChrome_SetStatus(nsIWebBrowserChrome *iface,
1318 UINT32 statusType, const PRUnichar *status)
1320 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1321 TRACE("(%p)->(%d %s)\n", This, statusType, debugstr_w(status));
1322 return NS_OK;
1325 static nsresult NSAPI nsWebBrowserChrome_GetWebBrowser(nsIWebBrowserChrome *iface,
1326 nsIWebBrowser **aWebBrowser)
1328 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1330 TRACE("(%p)->(%p)\n", This, aWebBrowser);
1332 if(!aWebBrowser)
1333 return NS_ERROR_INVALID_ARG;
1335 if(This->webbrowser)
1336 nsIWebBrowser_AddRef(This->webbrowser);
1337 *aWebBrowser = This->webbrowser;
1338 return S_OK;
1341 static nsresult NSAPI nsWebBrowserChrome_SetWebBrowser(nsIWebBrowserChrome *iface,
1342 nsIWebBrowser *aWebBrowser)
1344 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1346 TRACE("(%p)->(%p)\n", This, aWebBrowser);
1348 if(aWebBrowser != This->webbrowser)
1349 ERR("Wrong nsWebBrowser!\n");
1351 return NS_OK;
1354 static nsresult NSAPI nsWebBrowserChrome_GetChromeFlags(nsIWebBrowserChrome *iface,
1355 UINT32 *aChromeFlags)
1357 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1358 WARN("(%p)->(%p)\n", This, aChromeFlags);
1359 return NS_ERROR_NOT_IMPLEMENTED;
1362 static nsresult NSAPI nsWebBrowserChrome_SetChromeFlags(nsIWebBrowserChrome *iface,
1363 UINT32 aChromeFlags)
1365 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1366 WARN("(%p)->(%08x)\n", This, aChromeFlags);
1367 return NS_ERROR_NOT_IMPLEMENTED;
1370 static nsresult NSAPI nsWebBrowserChrome_DestroyBrowserWindow(nsIWebBrowserChrome *iface)
1372 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1373 TRACE("(%p)\n", This);
1374 return NS_ERROR_NOT_IMPLEMENTED;
1377 static nsresult NSAPI nsWebBrowserChrome_SizeBrowserTo(nsIWebBrowserChrome *iface,
1378 LONG aCX, LONG aCY)
1380 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1381 WARN("(%p)->(%d %d)\n", This, aCX, aCY);
1382 return NS_ERROR_NOT_IMPLEMENTED;
1385 static nsresult NSAPI nsWebBrowserChrome_ShowAsModal(nsIWebBrowserChrome *iface)
1387 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1388 WARN("(%p)\n", This);
1389 return NS_ERROR_NOT_IMPLEMENTED;
1392 static nsresult NSAPI nsWebBrowserChrome_IsWindowModal(nsIWebBrowserChrome *iface, cpp_bool *_retval)
1394 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1395 WARN("(%p)->(%p)\n", This, _retval);
1396 return NS_ERROR_NOT_IMPLEMENTED;
1399 static nsresult NSAPI nsWebBrowserChrome_ExitModalEventLoop(nsIWebBrowserChrome *iface,
1400 nsresult aStatus)
1402 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1403 WARN("(%p)->(%08x)\n", This, aStatus);
1404 return NS_ERROR_NOT_IMPLEMENTED;
1407 static const nsIWebBrowserChromeVtbl nsWebBrowserChromeVtbl = {
1408 nsWebBrowserChrome_QueryInterface,
1409 nsWebBrowserChrome_AddRef,
1410 nsWebBrowserChrome_Release,
1411 nsWebBrowserChrome_SetStatus,
1412 nsWebBrowserChrome_GetWebBrowser,
1413 nsWebBrowserChrome_SetWebBrowser,
1414 nsWebBrowserChrome_GetChromeFlags,
1415 nsWebBrowserChrome_SetChromeFlags,
1416 nsWebBrowserChrome_DestroyBrowserWindow,
1417 nsWebBrowserChrome_SizeBrowserTo,
1418 nsWebBrowserChrome_ShowAsModal,
1419 nsWebBrowserChrome_IsWindowModal,
1420 nsWebBrowserChrome_ExitModalEventLoop
1423 /**********************************************************
1424 * nsIContextMenuListener interface
1427 static inline NSContainer *impl_from_nsIContextMenuListener(nsIContextMenuListener *iface)
1429 return CONTAINING_RECORD(iface, NSContainer, nsIContextMenuListener_iface);
1432 static nsresult NSAPI nsContextMenuListener_QueryInterface(nsIContextMenuListener *iface,
1433 nsIIDRef riid, void **result)
1435 NSContainer *This = impl_from_nsIContextMenuListener(iface);
1436 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1439 static nsrefcnt NSAPI nsContextMenuListener_AddRef(nsIContextMenuListener *iface)
1441 NSContainer *This = impl_from_nsIContextMenuListener(iface);
1442 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1445 static nsrefcnt NSAPI nsContextMenuListener_Release(nsIContextMenuListener *iface)
1447 NSContainer *This = impl_from_nsIContextMenuListener(iface);
1448 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
1451 static nsresult NSAPI nsContextMenuListener_OnShowContextMenu(nsIContextMenuListener *iface,
1452 UINT32 aContextFlags, nsIDOMEvent *aEvent, nsIDOMNode *aNode)
1454 NSContainer *This = impl_from_nsIContextMenuListener(iface);
1455 nsIDOMMouseEvent *event;
1456 HTMLDOMNode *node;
1457 POINT pt;
1458 DWORD dwID = CONTEXT_MENU_DEFAULT;
1459 nsresult nsres;
1460 HRESULT hres;
1462 TRACE("(%p)->(%08x %p %p)\n", This, aContextFlags, aEvent, aNode);
1464 fire_event(This->doc->basedoc.doc_node /* FIXME */, EVENTID_CONTEXTMENU, TRUE, aNode, aEvent, NULL);
1466 nsres = nsIDOMEvent_QueryInterface(aEvent, &IID_nsIDOMMouseEvent, (void**)&event);
1467 if(NS_FAILED(nsres)) {
1468 ERR("Could not get nsIDOMMouseEvent interface: %08x\n", nsres);
1469 return nsres;
1472 nsIDOMMouseEvent_GetScreenX(event, &pt.x);
1473 nsIDOMMouseEvent_GetScreenY(event, &pt.y);
1474 nsIDOMMouseEvent_Release(event);
1476 switch(aContextFlags) {
1477 case CONTEXT_NONE:
1478 case CONTEXT_DOCUMENT:
1479 case CONTEXT_TEXT: {
1480 nsISelection *selection;
1482 nsres = nsIDOMHTMLDocument_GetSelection(This->doc->basedoc.doc_node->nsdoc, &selection);
1483 if(NS_SUCCEEDED(nsres) && selection) {
1484 cpp_bool is_collapsed;
1486 /* FIXME: Check if the click was inside selection. */
1487 nsres = nsISelection_GetIsCollapsed(selection, &is_collapsed);
1488 nsISelection_Release(selection);
1489 if(NS_SUCCEEDED(nsres) && !is_collapsed)
1490 dwID = CONTEXT_MENU_TEXTSELECT;
1492 break;
1494 case CONTEXT_IMAGE:
1495 case CONTEXT_IMAGE|CONTEXT_LINK:
1496 dwID = CONTEXT_MENU_IMAGE;
1497 break;
1498 case CONTEXT_LINK:
1499 dwID = CONTEXT_MENU_ANCHOR;
1500 break;
1501 case CONTEXT_INPUT:
1502 dwID = CONTEXT_MENU_CONTROL;
1503 break;
1504 default:
1505 FIXME("aContextFlags=%08x\n", aContextFlags);
1508 hres = get_node(This->doc->basedoc.doc_node, aNode, TRUE, &node);
1509 if(FAILED(hres))
1510 return NS_ERROR_FAILURE;
1512 show_context_menu(This->doc, dwID, &pt, (IDispatch*)&node->IHTMLDOMNode_iface);
1513 node_release(node);
1514 return NS_OK;
1517 static const nsIContextMenuListenerVtbl nsContextMenuListenerVtbl = {
1518 nsContextMenuListener_QueryInterface,
1519 nsContextMenuListener_AddRef,
1520 nsContextMenuListener_Release,
1521 nsContextMenuListener_OnShowContextMenu
1524 /**********************************************************
1525 * nsIURIContentListener interface
1528 static inline NSContainer *impl_from_nsIURIContentListener(nsIURIContentListener *iface)
1530 return CONTAINING_RECORD(iface, NSContainer, nsIURIContentListener_iface);
1533 static nsresult NSAPI nsURIContentListener_QueryInterface(nsIURIContentListener *iface,
1534 nsIIDRef riid, void **result)
1536 NSContainer *This = impl_from_nsIURIContentListener(iface);
1537 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1540 static nsrefcnt NSAPI nsURIContentListener_AddRef(nsIURIContentListener *iface)
1542 NSContainer *This = impl_from_nsIURIContentListener(iface);
1543 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1546 static nsrefcnt NSAPI nsURIContentListener_Release(nsIURIContentListener *iface)
1548 NSContainer *This = impl_from_nsIURIContentListener(iface);
1549 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
1552 static nsresult NSAPI nsURIContentListener_OnStartURIOpen(nsIURIContentListener *iface,
1553 nsIURI *aURI, cpp_bool *_retval)
1555 NSContainer *This = impl_from_nsIURIContentListener(iface);
1556 nsACString spec_str;
1557 const char *spec;
1558 nsresult nsres;
1560 nsACString_Init(&spec_str, NULL);
1561 nsIURI_GetSpec(aURI, &spec_str);
1562 nsACString_GetData(&spec_str, &spec);
1564 TRACE("(%p)->(%p(%s) %p)\n", This, aURI, debugstr_a(spec), _retval);
1566 nsACString_Finish(&spec_str);
1568 nsres = on_start_uri_open(This, aURI, _retval);
1569 if(NS_FAILED(nsres))
1570 return nsres;
1572 return !*_retval && This->content_listener
1573 ? nsIURIContentListener_OnStartURIOpen(This->content_listener, aURI, _retval)
1574 : NS_OK;
1577 static nsresult NSAPI nsURIContentListener_DoContent(nsIURIContentListener *iface,
1578 const char *aContentType, cpp_bool aIsContentPreferred, nsIRequest *aRequest,
1579 nsIStreamListener **aContentHandler, cpp_bool *_retval)
1581 NSContainer *This = impl_from_nsIURIContentListener(iface);
1583 TRACE("(%p)->(%s %x %p %p %p)\n", This, debugstr_a(aContentType), aIsContentPreferred,
1584 aRequest, aContentHandler, _retval);
1586 return This->content_listener
1587 ? nsIURIContentListener_DoContent(This->content_listener, aContentType,
1588 aIsContentPreferred, aRequest, aContentHandler, _retval)
1589 : NS_ERROR_NOT_IMPLEMENTED;
1592 static nsresult NSAPI nsURIContentListener_IsPreferred(nsIURIContentListener *iface,
1593 const char *aContentType, char **aDesiredContentType, cpp_bool *_retval)
1595 NSContainer *This = impl_from_nsIURIContentListener(iface);
1597 TRACE("(%p)->(%s %p %p)\n", This, debugstr_a(aContentType), aDesiredContentType, _retval);
1599 /* FIXME: Should we do something here? */
1600 *_retval = TRUE;
1602 return This->content_listener
1603 ? nsIURIContentListener_IsPreferred(This->content_listener, aContentType,
1604 aDesiredContentType, _retval)
1605 : NS_OK;
1608 static nsresult NSAPI nsURIContentListener_CanHandleContent(nsIURIContentListener *iface,
1609 const char *aContentType, cpp_bool aIsContentPreferred, char **aDesiredContentType,
1610 cpp_bool *_retval)
1612 NSContainer *This = impl_from_nsIURIContentListener(iface);
1614 TRACE("(%p)->(%s %x %p %p)\n", This, debugstr_a(aContentType), aIsContentPreferred,
1615 aDesiredContentType, _retval);
1617 return This->content_listener
1618 ? nsIURIContentListener_CanHandleContent(This->content_listener, aContentType,
1619 aIsContentPreferred, aDesiredContentType, _retval)
1620 : NS_ERROR_NOT_IMPLEMENTED;
1623 static nsresult NSAPI nsURIContentListener_GetLoadCookie(nsIURIContentListener *iface,
1624 nsISupports **aLoadCookie)
1626 NSContainer *This = impl_from_nsIURIContentListener(iface);
1628 WARN("(%p)->(%p)\n", This, aLoadCookie);
1630 return This->content_listener
1631 ? nsIURIContentListener_GetLoadCookie(This->content_listener, aLoadCookie)
1632 : NS_ERROR_NOT_IMPLEMENTED;
1635 static nsresult NSAPI nsURIContentListener_SetLoadCookie(nsIURIContentListener *iface,
1636 nsISupports *aLoadCookie)
1638 NSContainer *This = impl_from_nsIURIContentListener(iface);
1640 WARN("(%p)->(%p)\n", This, aLoadCookie);
1642 return This->content_listener
1643 ? nsIURIContentListener_SetLoadCookie(This->content_listener, aLoadCookie)
1644 : NS_ERROR_NOT_IMPLEMENTED;
1647 static nsresult NSAPI nsURIContentListener_GetParentContentListener(nsIURIContentListener *iface,
1648 nsIURIContentListener **aParentContentListener)
1650 NSContainer *This = impl_from_nsIURIContentListener(iface);
1652 TRACE("(%p)->(%p)\n", This, aParentContentListener);
1654 if(This->content_listener)
1655 nsIURIContentListener_AddRef(This->content_listener);
1657 *aParentContentListener = This->content_listener;
1658 return NS_OK;
1661 static nsresult NSAPI nsURIContentListener_SetParentContentListener(nsIURIContentListener *iface,
1662 nsIURIContentListener *aParentContentListener)
1664 NSContainer *This = impl_from_nsIURIContentListener(iface);
1666 TRACE("(%p)->(%p)\n", This, aParentContentListener);
1668 if(aParentContentListener == &This->nsIURIContentListener_iface)
1669 return NS_OK;
1671 if(This->content_listener)
1672 nsIURIContentListener_Release(This->content_listener);
1674 This->content_listener = aParentContentListener;
1675 if(This->content_listener)
1676 nsIURIContentListener_AddRef(This->content_listener);
1678 return NS_OK;
1681 static const nsIURIContentListenerVtbl nsURIContentListenerVtbl = {
1682 nsURIContentListener_QueryInterface,
1683 nsURIContentListener_AddRef,
1684 nsURIContentListener_Release,
1685 nsURIContentListener_OnStartURIOpen,
1686 nsURIContentListener_DoContent,
1687 nsURIContentListener_IsPreferred,
1688 nsURIContentListener_CanHandleContent,
1689 nsURIContentListener_GetLoadCookie,
1690 nsURIContentListener_SetLoadCookie,
1691 nsURIContentListener_GetParentContentListener,
1692 nsURIContentListener_SetParentContentListener
1695 /**********************************************************
1696 * nsIEmbeddinSiteWindow interface
1699 static inline NSContainer *impl_from_nsIEmbeddingSiteWindow(nsIEmbeddingSiteWindow *iface)
1701 return CONTAINING_RECORD(iface, NSContainer, nsIEmbeddingSiteWindow_iface);
1704 static nsresult NSAPI nsEmbeddingSiteWindow_QueryInterface(nsIEmbeddingSiteWindow *iface,
1705 nsIIDRef riid, void **result)
1707 NSContainer *This = impl_from_nsIEmbeddingSiteWindow(iface);
1708 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1711 static nsrefcnt NSAPI nsEmbeddingSiteWindow_AddRef(nsIEmbeddingSiteWindow *iface)
1713 NSContainer *This = impl_from_nsIEmbeddingSiteWindow(iface);
1714 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1717 static nsrefcnt NSAPI nsEmbeddingSiteWindow_Release(nsIEmbeddingSiteWindow *iface)
1719 NSContainer *This = impl_from_nsIEmbeddingSiteWindow(iface);
1720 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
1723 static nsresult NSAPI nsEmbeddingSiteWindow_SetDimensions(nsIEmbeddingSiteWindow *iface,
1724 UINT32 flags, LONG x, LONG y, LONG cx, LONG cy)
1726 NSContainer *This = impl_from_nsIEmbeddingSiteWindow(iface);
1727 WARN("(%p)->(%08x %d %d %d %d)\n", This, flags, x, y, cx, cy);
1728 return NS_ERROR_NOT_IMPLEMENTED;
1731 static nsresult NSAPI nsEmbeddingSiteWindow_GetDimensions(nsIEmbeddingSiteWindow *iface,
1732 UINT32 flags, LONG *x, LONG *y, LONG *cx, LONG *cy)
1734 NSContainer *This = impl_from_nsIEmbeddingSiteWindow(iface);
1735 RECT r;
1737 TRACE("(%p)->(%x %p %p %p %p)\n", This, flags, x, y, cx, cy);
1739 if(!GetWindowRect(This->hwnd, &r)) {
1740 ERR("GetWindowRect failed\n");
1741 return NS_ERROR_FAILURE;
1744 if(x)
1745 *x = r.left;
1746 if(y)
1747 *y = r.top;
1748 if(cx)
1749 *cx = r.right-r.left;
1750 if(cy)
1751 *cy = r.bottom-r.top;
1752 return NS_OK;
1755 static nsresult NSAPI nsEmbeddingSiteWindow_SetFocus(nsIEmbeddingSiteWindow *iface)
1757 NSContainer *This = impl_from_nsIEmbeddingSiteWindow(iface);
1759 TRACE("(%p)\n", This);
1761 return nsIBaseWindow_SetFocus(This->window);
1764 static nsresult NSAPI nsEmbeddingSiteWindow_GetVisibility(nsIEmbeddingSiteWindow *iface,
1765 cpp_bool *aVisibility)
1767 NSContainer *This = impl_from_nsIEmbeddingSiteWindow(iface);
1769 TRACE("(%p)->(%p)\n", This, aVisibility);
1771 *aVisibility = This->doc && This->doc->hwnd && IsWindowVisible(This->doc->hwnd);
1772 return NS_OK;
1775 static nsresult NSAPI nsEmbeddingSiteWindow_SetVisibility(nsIEmbeddingSiteWindow *iface,
1776 cpp_bool aVisibility)
1778 NSContainer *This = impl_from_nsIEmbeddingSiteWindow(iface);
1780 TRACE("(%p)->(%x)\n", This, aVisibility);
1782 return NS_OK;
1785 static nsresult NSAPI nsEmbeddingSiteWindow_GetTitle(nsIEmbeddingSiteWindow *iface,
1786 PRUnichar **aTitle)
1788 NSContainer *This = impl_from_nsIEmbeddingSiteWindow(iface);
1789 WARN("(%p)->(%p)\n", This, aTitle);
1790 return NS_ERROR_NOT_IMPLEMENTED;
1793 static nsresult NSAPI nsEmbeddingSiteWindow_SetTitle(nsIEmbeddingSiteWindow *iface,
1794 const PRUnichar *aTitle)
1796 NSContainer *This = impl_from_nsIEmbeddingSiteWindow(iface);
1797 WARN("(%p)->(%s)\n", This, debugstr_w(aTitle));
1798 return NS_ERROR_NOT_IMPLEMENTED;
1801 static nsresult NSAPI nsEmbeddingSiteWindow_GetSiteWindow(nsIEmbeddingSiteWindow *iface,
1802 void **aSiteWindow)
1804 NSContainer *This = impl_from_nsIEmbeddingSiteWindow(iface);
1806 TRACE("(%p)->(%p)\n", This, aSiteWindow);
1808 *aSiteWindow = This->hwnd;
1809 return NS_OK;
1812 static const nsIEmbeddingSiteWindowVtbl nsEmbeddingSiteWindowVtbl = {
1813 nsEmbeddingSiteWindow_QueryInterface,
1814 nsEmbeddingSiteWindow_AddRef,
1815 nsEmbeddingSiteWindow_Release,
1816 nsEmbeddingSiteWindow_SetDimensions,
1817 nsEmbeddingSiteWindow_GetDimensions,
1818 nsEmbeddingSiteWindow_SetFocus,
1819 nsEmbeddingSiteWindow_GetVisibility,
1820 nsEmbeddingSiteWindow_SetVisibility,
1821 nsEmbeddingSiteWindow_GetTitle,
1822 nsEmbeddingSiteWindow_SetTitle,
1823 nsEmbeddingSiteWindow_GetSiteWindow
1826 static inline NSContainer *impl_from_nsITooltipListener(nsITooltipListener *iface)
1828 return CONTAINING_RECORD(iface, NSContainer, nsITooltipListener_iface);
1831 static nsresult NSAPI nsTooltipListener_QueryInterface(nsITooltipListener *iface, nsIIDRef riid,
1832 void **result)
1834 NSContainer *This = impl_from_nsITooltipListener(iface);
1835 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1838 static nsrefcnt NSAPI nsTooltipListener_AddRef(nsITooltipListener *iface)
1840 NSContainer *This = impl_from_nsITooltipListener(iface);
1841 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1844 static nsrefcnt NSAPI nsTooltipListener_Release(nsITooltipListener *iface)
1846 NSContainer *This = impl_from_nsITooltipListener(iface);
1847 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
1850 static nsresult NSAPI nsTooltipListener_OnShowTooltip(nsITooltipListener *iface,
1851 LONG aXCoord, LONG aYCoord, const PRUnichar *aTipText)
1853 NSContainer *This = impl_from_nsITooltipListener(iface);
1855 if (This->doc)
1856 show_tooltip(This->doc, aXCoord, aYCoord, aTipText);
1858 return NS_OK;
1861 static nsresult NSAPI nsTooltipListener_OnHideTooltip(nsITooltipListener *iface)
1863 NSContainer *This = impl_from_nsITooltipListener(iface);
1865 if (This->doc)
1866 hide_tooltip(This->doc);
1868 return NS_OK;
1871 static const nsITooltipListenerVtbl nsTooltipListenerVtbl = {
1872 nsTooltipListener_QueryInterface,
1873 nsTooltipListener_AddRef,
1874 nsTooltipListener_Release,
1875 nsTooltipListener_OnShowTooltip,
1876 nsTooltipListener_OnHideTooltip
1879 static inline NSContainer *impl_from_nsIInterfaceRequestor(nsIInterfaceRequestor *iface)
1881 return CONTAINING_RECORD(iface, NSContainer, nsIInterfaceRequestor_iface);
1884 static nsresult NSAPI nsInterfaceRequestor_QueryInterface(nsIInterfaceRequestor *iface,
1885 nsIIDRef riid, void **result)
1887 NSContainer *This = impl_from_nsIInterfaceRequestor(iface);
1888 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1891 static nsrefcnt NSAPI nsInterfaceRequestor_AddRef(nsIInterfaceRequestor *iface)
1893 NSContainer *This = impl_from_nsIInterfaceRequestor(iface);
1894 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1897 static nsrefcnt NSAPI nsInterfaceRequestor_Release(nsIInterfaceRequestor *iface)
1899 NSContainer *This = impl_from_nsIInterfaceRequestor(iface);
1900 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
1903 static nsresult NSAPI nsInterfaceRequestor_GetInterface(nsIInterfaceRequestor *iface,
1904 nsIIDRef riid, void **result)
1906 NSContainer *This = impl_from_nsIInterfaceRequestor(iface);
1908 if(IsEqualGUID(&IID_nsIDOMWindow, riid)) {
1909 TRACE("(%p)->(IID_nsIDOMWindow %p)\n", This, result);
1910 return nsIWebBrowser_GetContentDOMWindow(This->webbrowser, (nsIDOMWindow**)result);
1913 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1916 static const nsIInterfaceRequestorVtbl nsInterfaceRequestorVtbl = {
1917 nsInterfaceRequestor_QueryInterface,
1918 nsInterfaceRequestor_AddRef,
1919 nsInterfaceRequestor_Release,
1920 nsInterfaceRequestor_GetInterface
1923 static inline NSContainer *impl_from_nsISupportsWeakReference(nsISupportsWeakReference *iface)
1925 return CONTAINING_RECORD(iface, NSContainer, nsISupportsWeakReference_iface);
1928 static nsresult NSAPI nsSupportsWeakReference_QueryInterface(nsISupportsWeakReference *iface,
1929 nsIIDRef riid, void **result)
1931 NSContainer *This = impl_from_nsISupportsWeakReference(iface);
1932 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1935 static nsrefcnt NSAPI nsSupportsWeakReference_AddRef(nsISupportsWeakReference *iface)
1937 NSContainer *This = impl_from_nsISupportsWeakReference(iface);
1938 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1941 static nsrefcnt NSAPI nsSupportsWeakReference_Release(nsISupportsWeakReference *iface)
1943 NSContainer *This = impl_from_nsISupportsWeakReference(iface);
1944 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
1947 static nsresult NSAPI nsSupportsWeakReference_GetWeakReference(nsISupportsWeakReference *iface,
1948 nsIWeakReference **_retval)
1950 NSContainer *This = impl_from_nsISupportsWeakReference(iface);
1952 TRACE("(%p)->(%p)\n", This, _retval);
1954 if(!This->weak_reference) {
1955 This->weak_reference = heap_alloc(sizeof(nsWeakReference));
1956 if(!This->weak_reference)
1957 return NS_ERROR_OUT_OF_MEMORY;
1959 This->weak_reference->nsIWeakReference_iface.lpVtbl = &nsWeakReferenceVtbl;
1960 This->weak_reference->ref = 1;
1961 This->weak_reference->nscontainer = This;
1964 *_retval = &This->weak_reference->nsIWeakReference_iface;
1965 nsIWeakReference_AddRef(*_retval);
1966 return NS_OK;
1969 static const nsISupportsWeakReferenceVtbl nsSupportsWeakReferenceVtbl = {
1970 nsSupportsWeakReference_QueryInterface,
1971 nsSupportsWeakReference_AddRef,
1972 nsSupportsWeakReference_Release,
1973 nsSupportsWeakReference_GetWeakReference
1976 static HRESULT init_nscontainer(NSContainer *nscontainer)
1978 nsIWebBrowserSetup *wbsetup;
1979 nsIScrollable *scrollable;
1980 nsresult nsres;
1982 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_WEBBROWSER_CONTRACTID,
1983 NULL, &IID_nsIWebBrowser, (void**)&nscontainer->webbrowser);
1984 if(NS_FAILED(nsres)) {
1985 ERR("Creating WebBrowser failed: %08x\n", nsres);
1986 return E_FAIL;
1989 nsres = nsIWebBrowser_SetContainerWindow(nscontainer->webbrowser, &nscontainer->nsIWebBrowserChrome_iface);
1990 if(NS_FAILED(nsres)) {
1991 ERR("SetContainerWindow failed: %08x\n", nsres);
1992 return E_FAIL;
1995 nsres = nsIWebBrowser_QueryInterface(nscontainer->webbrowser, &IID_nsIBaseWindow,
1996 (void**)&nscontainer->window);
1997 if(NS_FAILED(nsres)) {
1998 ERR("Could not get nsIBaseWindow interface: %08x\n", nsres);
1999 return E_FAIL;
2002 nsres = nsIWebBrowser_QueryInterface(nscontainer->webbrowser, &IID_nsIWebBrowserSetup,
2003 (void**)&wbsetup);
2004 if(NS_SUCCEEDED(nsres)) {
2005 nsres = nsIWebBrowserSetup_SetProperty(wbsetup, SETUP_IS_CHROME_WRAPPER, FALSE);
2006 nsIWebBrowserSetup_Release(wbsetup);
2007 if(NS_FAILED(nsres)) {
2008 ERR("SetProperty(SETUP_IS_CHROME_WRAPPER) failed: %08x\n", nsres);
2009 return E_FAIL;
2011 }else {
2012 ERR("Could not get nsIWebBrowserSetup interface\n");
2013 return E_FAIL;
2016 nsres = nsIWebBrowser_QueryInterface(nscontainer->webbrowser, &IID_nsIWebNavigation,
2017 (void**)&nscontainer->navigation);
2018 if(NS_FAILED(nsres)) {
2019 ERR("Could not get nsIWebNavigation interface: %08x\n", nsres);
2020 return E_FAIL;
2023 nsres = nsIWebBrowser_QueryInterface(nscontainer->webbrowser, &IID_nsIWebBrowserFocus,
2024 (void**)&nscontainer->focus);
2025 if(NS_FAILED(nsres)) {
2026 ERR("Could not get nsIWebBrowserFocus interface: %08x\n", nsres);
2027 return E_FAIL;
2030 if(!nscontainer_class) {
2031 register_nscontainer_class();
2032 if(!nscontainer_class)
2033 return E_FAIL;
2036 nscontainer->hwnd = CreateWindowExW(0, wszNsContainer, NULL,
2037 WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 0, 0, 100, 100,
2038 GetDesktopWindow(), NULL, hInst, nscontainer);
2039 if(!nscontainer->hwnd) {
2040 WARN("Could not create window\n");
2041 return E_FAIL;
2044 nsres = nsIBaseWindow_InitWindow(nscontainer->window, nscontainer->hwnd, NULL, 0, 0, 100, 100);
2045 if(NS_SUCCEEDED(nsres)) {
2046 nsres = nsIBaseWindow_Create(nscontainer->window);
2047 if(NS_FAILED(nsres)) {
2048 WARN("Creating window failed: %08x\n", nsres);
2049 return E_FAIL;
2052 nsIBaseWindow_SetVisibility(nscontainer->window, FALSE);
2053 nsIBaseWindow_SetEnabled(nscontainer->window, FALSE);
2054 }else {
2055 ERR("InitWindow failed: %08x\n", nsres);
2056 return E_FAIL;
2059 nsres = nsIWebBrowser_SetParentURIContentListener(nscontainer->webbrowser,
2060 &nscontainer->nsIURIContentListener_iface);
2061 if(NS_FAILED(nsres))
2062 ERR("SetParentURIContentListener failed: %08x\n", nsres);
2064 nsres = nsIWebBrowser_QueryInterface(nscontainer->webbrowser, &IID_nsIScrollable, (void**)&scrollable);
2065 if(NS_SUCCEEDED(nsres)) {
2066 nsres = nsIScrollable_SetDefaultScrollbarPreferences(scrollable,
2067 ScrollOrientation_Y, Scrollbar_Always);
2068 if(NS_FAILED(nsres))
2069 ERR("Could not set default Y scrollbar prefs: %08x\n", nsres);
2071 nsres = nsIScrollable_SetDefaultScrollbarPreferences(scrollable,
2072 ScrollOrientation_X, Scrollbar_Auto);
2073 if(NS_FAILED(nsres))
2074 ERR("Could not set default X scrollbar prefs: %08x\n", nsres);
2076 nsIScrollable_Release(scrollable);
2077 }else {
2078 ERR("Could not get nsIScrollable: %08x\n", nsres);
2081 return S_OK;
2084 HRESULT create_nscontainer(HTMLDocumentObj *doc, NSContainer **_ret)
2086 NSContainer *ret;
2087 HRESULT hres;
2089 if(!load_gecko())
2090 return CLASS_E_CLASSNOTAVAILABLE;
2092 ret = heap_alloc_zero(sizeof(NSContainer));
2093 if(!ret)
2094 return E_OUTOFMEMORY;
2096 ret->nsIWebBrowserChrome_iface.lpVtbl = &nsWebBrowserChromeVtbl;
2097 ret->nsIContextMenuListener_iface.lpVtbl = &nsContextMenuListenerVtbl;
2098 ret->nsIURIContentListener_iface.lpVtbl = &nsURIContentListenerVtbl;
2099 ret->nsIEmbeddingSiteWindow_iface.lpVtbl = &nsEmbeddingSiteWindowVtbl;
2100 ret->nsITooltipListener_iface.lpVtbl = &nsTooltipListenerVtbl;
2101 ret->nsIInterfaceRequestor_iface.lpVtbl = &nsInterfaceRequestorVtbl;
2102 ret->nsISupportsWeakReference_iface.lpVtbl = &nsSupportsWeakReferenceVtbl;
2104 ret->doc = doc;
2105 ret->ref = 1;
2107 hres = init_nscontainer(ret);
2108 if(SUCCEEDED(hres))
2109 *_ret = ret;
2110 else
2111 nsIWebBrowserChrome_Release(&ret->nsIWebBrowserChrome_iface);
2112 return hres;
2115 void NSContainer_Release(NSContainer *This)
2117 TRACE("(%p)\n", This);
2119 This->doc = NULL;
2121 ShowWindow(This->hwnd, SW_HIDE);
2122 SetParent(This->hwnd, NULL);
2124 nsIBaseWindow_SetVisibility(This->window, FALSE);
2125 nsIBaseWindow_Destroy(This->window);
2127 nsIWebBrowser_SetContainerWindow(This->webbrowser, NULL);
2129 nsIWebBrowser_Release(This->webbrowser);
2130 This->webbrowser = NULL;
2132 nsIWebNavigation_Release(This->navigation);
2133 This->navigation = NULL;
2135 nsIBaseWindow_Release(This->window);
2136 This->window = NULL;
2138 nsIWebBrowserFocus_Release(This->focus);
2139 This->focus = NULL;
2141 if(This->editor_controller) {
2142 nsIController_Release(This->editor_controller);
2143 This->editor_controller = NULL;
2146 if(This->editor) {
2147 nsIEditor_Release(This->editor);
2148 This->editor = NULL;
2151 if(This->content_listener) {
2152 nsIURIContentListener_Release(This->content_listener);
2153 This->content_listener = NULL;
2156 if(This->hwnd) {
2157 DestroyWindow(This->hwnd);
2158 This->hwnd = NULL;
2161 nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);