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
33 #include "wine/debug.h"
34 #include "wine/unicode.h"
36 #include "mshtml_private.h"
37 #include "htmlevent.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_MEMORY_CONTRACTID "@mozilla.org/xpcom/memory-service;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"
50 #define PR_UINT32_MAX 0xffffffff
52 #define NS_STRING_CONTAINER_INIT_DEPEND 0x0002
53 #define NS_CSTRING_CONTAINER_INIT_DEPEND 0x0002
55 static nsresult (CDECL *NS_InitXPCOM2)(nsIServiceManager**,void*,void*);
56 static nsresult (CDECL *NS_ShutdownXPCOM)(nsIServiceManager*);
57 static nsresult (CDECL *NS_GetComponentRegistrar)(nsIComponentRegistrar**);
58 static nsresult (CDECL *NS_StringContainerInit2)(nsStringContainer*,const PRUnichar*,PRUint32,PRUint32);
59 static nsresult (CDECL *NS_CStringContainerInit2)(nsCStringContainer*,const char*,PRUint32,PRUint32);
60 static nsresult (CDECL *NS_StringContainerFinish)(nsStringContainer*);
61 static nsresult (CDECL *NS_CStringContainerFinish)(nsCStringContainer*);
62 static nsresult (CDECL *NS_StringSetData)(nsAString*,const PRUnichar*,PRUint32);
63 static nsresult (CDECL *NS_CStringSetData)(nsACString*,const char*,PRUint32);
64 static nsresult (CDECL *NS_NewLocalFile)(const nsAString*,PRBool,nsIFile**);
65 static PRUint32 (CDECL *NS_StringGetData)(const nsAString*,const PRUnichar **,PRBool*);
66 static PRUint32 (CDECL *NS_CStringGetData)(const nsACString*,const char**,PRBool*);
68 static HINSTANCE xul_handle = NULL;
70 static nsIServiceManager *pServMgr = NULL;
71 static nsIComponentManager *pCompMgr = NULL;
72 static nsIMemory *nsmem = NULL;
73 static nsIFile *profile_directory;
75 static const WCHAR wszNsContainer[] = {'N','s','C','o','n','t','a','i','n','e','r',0};
77 static ATOM nscontainer_class;
78 static WCHAR gecko_path[MAX_PATH];
79 static unsigned gecko_path_len;
81 static nsresult NSAPI nsDirectoryServiceProvider_QueryInterface(nsIDirectoryServiceProvider *iface,
82 nsIIDRef riid, void **result)
84 if(IsEqualGUID(&IID_nsISupports, riid)) {
85 TRACE("(IID_nsISupports %p)\n", result);
87 }else if(IsEqualGUID(&IID_nsIDirectoryServiceProvider, riid)) {
88 TRACE("(IID_nsIDirectoryServiceProvider %p)\n", result);
91 WARN("(%s %p)\n", debugstr_guid(riid), result);
93 return NS_NOINTERFACE;
96 nsISupports_AddRef((nsISupports*)*result);
100 static nsrefcnt NSAPI nsDirectoryServiceProvider_AddRef(nsIDirectoryServiceProvider *iface)
105 static nsrefcnt NSAPI nsDirectoryServiceProvider_Release(nsIDirectoryServiceProvider *iface)
110 static nsresult create_profile_directory(void)
112 static const WCHAR wine_geckoW[] = {'\\','w','i','n','e','_','g','e','c','k','o',0};
114 WCHAR path[MAX_PATH + sizeof(wine_geckoW)/sizeof(WCHAR)];
120 hres = SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_DEFAULT, path);
122 ERR("SHGetFolderPath failed: %08x\n", hres);
123 return NS_ERROR_FAILURE;
126 strcatW(path, wine_geckoW);
127 nsAString_InitDepend(&str, path);
128 nsres = NS_NewLocalFile(&str, FALSE, &profile_directory);
129 nsAString_Finish(&str);
130 if(NS_FAILED(nsres)) {
131 ERR("NS_NewLocalFile failed: %08x\n", nsres);
135 nsres = nsIFile_Exists(profile_directory, &exists);
136 if(NS_FAILED(nsres)) {
137 ERR("Exists failed: %08x\n", nsres);
142 nsres = nsIFile_Create(profile_directory, 1, 0700);
144 ERR("Create failed: %08x\n", nsres);
150 static nsresult NSAPI nsDirectoryServiceProvider_GetFile(nsIDirectoryServiceProvider *iface,
151 const char *prop, PRBool *persistent, nsIFile **_retval)
153 TRACE("(%s %p %p)\n", debugstr_a(prop), persistent, _retval);
155 if(!strcmp(prop, "ProfD")) {
156 if(!profile_directory) {
159 nsres = create_profile_directory();
164 return nsIFile_Clone(profile_directory, _retval);
167 return NS_ERROR_FAILURE;
170 static const nsIDirectoryServiceProviderVtbl nsDirectoryServiceProviderVtbl = {
171 nsDirectoryServiceProvider_QueryInterface,
172 nsDirectoryServiceProvider_AddRef,
173 nsDirectoryServiceProvider_Release,
174 nsDirectoryServiceProvider_GetFile
177 static nsIDirectoryServiceProvider nsDirectoryServiceProvider =
178 { &nsDirectoryServiceProviderVtbl };
180 static LRESULT WINAPI nsembed_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
185 static const WCHAR wszTHIS[] = {'T','H','I','S',0};
187 if(msg == WM_CREATE) {
188 This = *(NSContainer**)lParam;
189 SetPropW(hwnd, wszTHIS, This);
191 This = GetPropW(hwnd, wszTHIS);
196 TRACE("(%p)->(WM_SIZE)\n", This);
198 nsres = nsIBaseWindow_SetSize(This->window,
199 LOWORD(lParam), HIWORD(lParam), TRUE);
201 WARN("SetSize failed: %08x\n", nsres);
204 case WM_PARENTNOTIFY:
205 TRACE("WM_PARENTNOTIFY %x\n", (unsigned)wParam);
210 nsIWebBrowserFocus_Activate(This->focus);
214 return DefWindowProcW(hwnd, msg, wParam, lParam);
218 static void register_nscontainer_class(void)
220 static WNDCLASSEXW wndclass = {
224 0, 0, NULL, NULL, NULL, NULL, NULL,
228 wndclass.hInstance = hInst;
229 nscontainer_class = RegisterClassExW(&wndclass);
232 static BOOL install_wine_gecko(BOOL silent)
234 PROCESS_INFORMATION pi;
241 static const WCHAR controlW[] = {'\\','c','o','n','t','r','o','l','.','e','x','e',0};
242 static const WCHAR argsW[] =
243 {' ','a','p','p','w','i','z','.','c','p','l',' ','i','n','s','t','a','l','l','_','g','e','c','k','o',0};
245 len = GetSystemDirectoryW(app, MAX_PATH-sizeof(controlW)/sizeof(WCHAR));
246 memcpy(app+len, controlW, sizeof(controlW));
248 args = heap_alloc(len*sizeof(WCHAR) + sizeof(controlW) + sizeof(argsW));
252 memcpy(args, app, len*sizeof(WCHAR) + sizeof(controlW));
253 memcpy(args + len + sizeof(controlW)/sizeof(WCHAR)-1, argsW, sizeof(argsW));
255 TRACE("starting %s\n", debugstr_w(args));
257 memset(&si, 0, sizeof(si));
259 ret = CreateProcessW(app, args, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
262 CloseHandle(pi.hThread);
263 WaitForSingleObject(pi.hProcess, INFINITE);
264 CloseHandle(pi.hProcess);
270 static void set_environment(LPCWSTR gre_path)
272 WCHAR path_env[MAX_PATH], buf[20];
273 int len, debug_level = 0;
275 static const WCHAR pathW[] = {'P','A','T','H',0};
276 static const WCHAR warnW[] = {'w','a','r','n',0};
277 static const WCHAR xpcom_debug_breakW[] =
278 {'X','P','C','O','M','_','D','E','B','U','G','_','B','R','E','A','K',0};
279 static const WCHAR nspr_log_modulesW[] =
280 {'N','S','P','R','_','L','O','G','_','M','O','D','U','L','E','S',0};
281 static const WCHAR debug_formatW[] = {'a','l','l',':','%','d',0};
282 static const WCHAR moz_plugin_pathW[] = {'M','O','Z','_','P','L','U','G','I','N','_','P','A','T','H',0};
283 static const WCHAR gecko_pluginW[] = {'\\','g','e','c','k','o','\\','p','l','u','g','i','n',0};
285 /* We have to modify PATH as XPCOM loads other DLLs from this directory. */
286 GetEnvironmentVariableW(pathW, path_env, sizeof(path_env)/sizeof(WCHAR));
287 len = strlenW(path_env);
288 path_env[len++] = ';';
289 strcpyW(path_env+len, gre_path);
290 SetEnvironmentVariableW(pathW, path_env);
292 SetEnvironmentVariableW(xpcom_debug_breakW, warnW);
296 else if(WARN_ON(gecko))
298 else if(ERR_ON(gecko))
301 sprintfW(buf, debug_formatW, debug_level);
302 SetEnvironmentVariableW(nspr_log_modulesW, buf);
304 len = GetSystemDirectoryW(path_env, (sizeof(path_env)-sizeof(gecko_pluginW))/sizeof(WCHAR)+1);
306 strcpyW(path_env+len, gecko_pluginW);
307 SetEnvironmentVariableW(moz_plugin_pathW, path_env);
311 static BOOL load_xul(const PRUnichar *gre_path)
313 static const WCHAR xul_dllW[] = {'\\','x','u','l','.','d','l','l',0};
314 WCHAR file_name[MAX_PATH];
316 strcpyW(file_name, gre_path);
317 strcatW(file_name, xul_dllW);
319 TRACE("(%s)\n", debugstr_w(file_name));
321 set_environment(gre_path);
323 xul_handle = LoadLibraryExW(file_name, 0, LOAD_WITH_ALTERED_SEARCH_PATH);
325 WARN("Could not load XUL: %d\n", GetLastError());
329 #define NS_DLSYM(func) \
330 func = (void *)GetProcAddress(xul_handle, #func "_P"); \
332 ERR("Could not GetProcAddress(" #func ") failed\n")
334 NS_DLSYM(NS_InitXPCOM2);
335 NS_DLSYM(NS_ShutdownXPCOM);
336 NS_DLSYM(NS_GetComponentRegistrar);
337 NS_DLSYM(NS_StringContainerInit2);
338 NS_DLSYM(NS_CStringContainerInit2);
339 NS_DLSYM(NS_StringContainerFinish);
340 NS_DLSYM(NS_CStringContainerFinish);
341 NS_DLSYM(NS_StringSetData);
342 NS_DLSYM(NS_CStringSetData);
343 NS_DLSYM(NS_NewLocalFile);
344 NS_DLSYM(NS_StringGetData);
345 NS_DLSYM(NS_CStringGetData);
352 static BOOL check_version(LPCWSTR gre_path, const char *version_string)
354 WCHAR file_name[MAX_PATH];
359 static const WCHAR wszVersion[] = {'\\','V','E','R','S','I','O','N',0};
361 strcpyW(file_name, gre_path);
362 strcatW(file_name, wszVersion);
364 hfile = CreateFileW(file_name, GENERIC_READ, FILE_SHARE_READ, NULL,
365 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
366 if(hfile == INVALID_HANDLE_VALUE) {
367 ERR("Could not open VERSION file\n");
371 ReadFile(hfile, version, sizeof(version), &read, NULL);
375 TRACE("%s\n", debugstr_a(version));
377 if(strcmp(version, version_string)) {
378 ERR("Unexpected version %s, expected %s\n", debugstr_a(version),
379 debugstr_a(version_string));
386 static BOOL load_wine_gecko_v(PRUnichar *gre_path, HKEY mshtml_key,
387 const char *version, const char *version_string)
389 DWORD res, type, size = MAX_PATH;
390 HKEY hkey = mshtml_key;
392 static const WCHAR wszGeckoPath[] =
393 {'G','e','c','k','o','P','a','t','h',0};
396 /* @@ Wine registry key: HKLM\Software\Wine\MSHTML\<version> */
397 res = RegOpenKeyA(mshtml_key, version, &hkey);
398 if(res != ERROR_SUCCESS)
402 res = RegQueryValueExW(hkey, wszGeckoPath, NULL, &type, (LPBYTE)gre_path, &size);
403 if(hkey != mshtml_key)
405 if(res != ERROR_SUCCESS || type != REG_SZ)
408 if(!check_version(gre_path, version_string))
411 return load_xul(gre_path);
414 static BOOL load_wine_gecko(PRUnichar *gre_path)
420 static const WCHAR wszMshtmlKey[] = {
421 'S','o','f','t','w','a','r','e','\\','W','i','n','e',
422 '\\','M','S','H','T','M','L',0};
424 /* @@ Wine registry key: HKLM\Software\Wine\MSHTML */
425 res = RegOpenKeyW(HKEY_LOCAL_MACHINE, wszMshtmlKey, &hkey);
426 if(res != ERROR_SUCCESS)
429 ret = load_wine_gecko_v(gre_path, hkey, GECKO_VERSION, GECKO_VERSION_STRING);
435 static void set_bool_pref(nsIPrefBranch *pref, const char *pref_name, BOOL val)
439 nsres = nsIPrefBranch_SetBoolPref(pref, pref_name, val);
441 ERR("Could not set pref %s\n", debugstr_a(pref_name));
444 static void set_int_pref(nsIPrefBranch *pref, const char *pref_name, int val)
448 nsres = nsIPrefBranch_SetIntPref(pref, pref_name, val);
450 ERR("Could not set pref %s\n", debugstr_a(pref_name));
453 static void set_string_pref(nsIPrefBranch *pref, const char *pref_name, const char *val)
457 nsres = nsIPrefBranch_SetCharPref(pref, pref_name, val);
459 ERR("Could not set pref %s\n", debugstr_a(pref_name));
462 static void set_lang(nsIPrefBranch *pref)
465 DWORD res, size, type;
468 static const WCHAR international_keyW[] =
469 {'S','o','f','t','w','a','r','e',
470 '\\','M','i','c','r','o','s','o','f','t',
471 '\\','I','n','t','e','r','n','e','t',' ','E','x','p','l','o','r','e','r',
472 '\\','I','n','t','e','r','n','a','t','i','o','n','a','l',0};
474 res = RegOpenKeyW(HKEY_CURRENT_USER, international_keyW, &hkey);
475 if(res != ERROR_SUCCESS)
478 size = sizeof(langs);
479 res = RegQueryValueExA(hkey, "AcceptLanguage", 0, &type, (LPBYTE)langs, &size);
481 if(res != ERROR_SUCCESS || type != REG_SZ)
484 TRACE("Setting lang %s\n", debugstr_a(langs));
486 set_string_pref(pref, "intl.accept_languages", langs);
489 static void set_proxy(nsIPrefBranch *pref)
494 DWORD enabled = 0, res, size, type;
497 static const WCHAR proxy_keyW[] =
498 {'S','o','f','t','w','a','r','e',
499 '\\','M','i','c','r','o','s','o','f','t',
500 '\\','W','i','n','d','o','w','s',
501 '\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n',
502 '\\','I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s',0};
504 res = RegOpenKeyW(HKEY_CURRENT_USER, proxy_keyW, &hkey);
505 if(res != ERROR_SUCCESS)
508 size = sizeof(enabled);
509 res = RegQueryValueExA(hkey, "ProxyEnable", 0, &type, (LPBYTE)&enabled, &size);
510 if(res != ERROR_SUCCESS || type != REG_DWORD || enabled == 0)
516 size = sizeof(proxy);
517 res = RegQueryValueExA(hkey, "ProxyServer", 0, &type, (LPBYTE)proxy, &size);
519 if(res != ERROR_SUCCESS || type != REG_SZ)
522 proxy_port = strchr(proxy, ':');
527 proxy_port_num = atoi(proxy_port + 1);
528 TRACE("Setting proxy to %s, port %d\n", debugstr_a(proxy), proxy_port_num);
530 set_string_pref(pref, "network.proxy.http", proxy);
531 set_string_pref(pref, "network.proxy.ssl", proxy);
533 set_int_pref(pref, "network.proxy.type", 1);
534 set_int_pref(pref, "network.proxy.http_port", proxy_port_num);
535 set_int_pref(pref, "network.proxy.ssl_port", proxy_port_num);
538 static void set_preferences(void)
543 nsres = nsIServiceManager_GetServiceByContractID(pServMgr, NS_PREFERENCES_CONTRACTID,
544 &IID_nsIPrefBranch, (void**)&pref);
545 if(NS_FAILED(nsres)) {
546 ERR("Could not get preference service: %08x\n", nsres);
552 set_bool_pref(pref, "security.warn_entering_secure", FALSE);
553 set_bool_pref(pref, "security.warn_submit_insecure", FALSE);
554 set_int_pref(pref, "layout.spellcheckDefault", 0);
556 nsIPrefBranch_Release(pref);
559 static BOOL init_xpcom(const PRUnichar *gre_path)
561 nsIComponentRegistrar *registrar = NULL;
567 nsAString_InitDepend(&path, gre_path);
568 nsres = NS_NewLocalFile(&path, FALSE, &gre_dir);
569 nsAString_Finish(&path);
570 if(NS_FAILED(nsres)) {
571 ERR("NS_NewLocalFile failed: %08x\n", nsres);
572 FreeLibrary(xul_handle);
576 nsres = NS_InitXPCOM2(&pServMgr, gre_dir, &nsDirectoryServiceProvider);
577 if(NS_FAILED(nsres)) {
578 ERR("NS_InitXPCOM2 failed: %08x\n", nsres);
579 FreeLibrary(xul_handle);
583 strcpyW(gecko_path, gre_path);
584 for(ptr = gecko_path; *ptr; ptr++) {
588 gecko_path_len = ptr-gecko_path;
590 nsres = nsIServiceManager_QueryInterface(pServMgr, &IID_nsIComponentManager, (void**)&pCompMgr);
592 ERR("Could not get nsIComponentManager: %08x\n", nsres);
594 nsres = NS_GetComponentRegistrar(®istrar);
595 if(NS_SUCCEEDED(nsres))
596 init_nsio(pCompMgr, registrar);
598 ERR("NS_GetComponentRegistrar failed: %08x\n", nsres);
600 init_mutation(pCompMgr);
603 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_MEMORY_CONTRACTID,
604 NULL, &IID_nsIMemory, (void**)&nsmem);
606 ERR("Could not get nsIMemory: %08x\n", nsres);
609 register_nsservice(registrar, pServMgr);
610 nsIComponentRegistrar_Release(registrar);
616 static CRITICAL_SECTION cs_load_gecko;
617 static CRITICAL_SECTION_DEBUG cs_load_gecko_dbg =
619 0, 0, &cs_load_gecko,
620 { &cs_load_gecko_dbg.ProcessLocksList, &cs_load_gecko_dbg.ProcessLocksList },
621 0, 0, { (DWORD_PTR)(__FILE__ ": load_gecko") }
623 static CRITICAL_SECTION cs_load_gecko = { &cs_load_gecko_dbg, -1, 0, 0, 0, 0 };
625 BOOL load_gecko(BOOL silent)
627 PRUnichar gre_path[MAX_PATH];
630 static DWORD loading_thread;
634 /* load_gecko may be called recursively */
635 if(loading_thread == GetCurrentThreadId())
636 return pCompMgr != NULL;
638 EnterCriticalSection(&cs_load_gecko);
640 if(!loading_thread) {
641 loading_thread = GetCurrentThreadId();
643 if(load_wine_gecko(gre_path)
644 || (install_wine_gecko(silent) && load_wine_gecko(gre_path)))
645 ret = init_xpcom(gre_path);
647 MESSAGE("Could not load wine-gecko. HTML rendering will be disabled.\n");
649 ret = pCompMgr != NULL;
652 LeaveCriticalSection(&cs_load_gecko);
657 void *nsalloc(size_t size)
659 return nsIMemory_Alloc(nsmem, size);
662 void nsfree(void *mem)
664 nsIMemory_Free(nsmem, mem);
667 static BOOL nsACString_Init(nsACString *str, const char *data)
669 return NS_SUCCEEDED(NS_CStringContainerInit2(str, data, PR_UINT32_MAX, 0));
673 * Initializes nsACString with data owned by caller.
674 * Caller must ensure that data is valid during lifetime of string object.
676 void nsACString_InitDepend(nsACString *str, const char *data)
678 NS_CStringContainerInit2(str, data, PR_UINT32_MAX, NS_CSTRING_CONTAINER_INIT_DEPEND);
681 void nsACString_SetData(nsACString *str, const char *data)
683 NS_CStringSetData(str, data, PR_UINT32_MAX);
686 PRUint32 nsACString_GetData(const nsACString *str, const char **data)
688 return NS_CStringGetData(str, data, NULL);
691 void nsACString_Finish(nsACString *str)
693 NS_CStringContainerFinish(str);
696 BOOL nsAString_Init(nsAString *str, const PRUnichar *data)
698 return NS_SUCCEEDED(NS_StringContainerInit2(str, data, PR_UINT32_MAX, 0));
702 * Initializes nsAString with data owned by caller.
703 * Caller must ensure that data is valid during lifetime of string object.
705 void nsAString_InitDepend(nsAString *str, const PRUnichar *data)
707 NS_StringContainerInit2(str, data, PR_UINT32_MAX, NS_STRING_CONTAINER_INIT_DEPEND);
710 void nsAString_SetData(nsAString *str, const PRUnichar *data)
712 NS_StringSetData(str, data, PR_UINT32_MAX);
715 PRUint32 nsAString_GetData(const nsAString *str, const PRUnichar **data)
717 return NS_StringGetData(str, data, NULL);
720 void nsAString_Finish(nsAString *str)
722 NS_StringContainerFinish(str);
725 HRESULT return_nsstr(nsresult nsres, nsAString *nsstr, BSTR *p)
727 const PRUnichar *str;
729 if(NS_FAILED(nsres)) {
730 ERR("failed: %08x\n", nsres);
731 nsAString_Finish(nsstr);
735 nsAString_GetData(nsstr, &str);
736 TRACE("ret %s\n", debugstr_w(str));
738 *p = SysAllocString(str);
740 return E_OUTOFMEMORY;
745 nsAString_Finish(nsstr);
749 nsICommandParams *create_nscommand_params(void)
751 nsICommandParams *ret = NULL;
757 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
758 NS_COMMANDPARAMS_CONTRACTID, NULL, &IID_nsICommandParams,
761 ERR("Could not get nsICommandParams\n");
766 nsresult get_nsinterface(nsISupports *iface, REFIID riid, void **ppv)
768 nsIInterfaceRequestor *iface_req;
771 nsres = nsISupports_QueryInterface(iface, &IID_nsIInterfaceRequestor, (void**)&iface_req);
775 nsres = nsIInterfaceRequestor_GetInterface(iface_req, riid, ppv);
776 nsIInterfaceRequestor_Release(iface_req);
781 static HRESULT nsnode_to_nsstring_rec(nsIContentSerializer *serializer, nsIDOMNode *nsnode, nsAString *str)
783 nsIDOMNodeList *node_list = NULL;
784 PRBool has_children = FALSE;
785 nsIContent *nscontent;
789 nsIDOMNode_HasChildNodes(nsnode, &has_children);
791 nsres = nsIDOMNode_GetNodeType(nsnode, &type);
792 if(NS_FAILED(nsres)) {
793 ERR("GetType failed: %08x\n", nsres);
797 nsres = nsIDOMNode_QueryInterface(nsnode, &IID_nsIContent, (void**)&nscontent);
798 if(NS_FAILED(nsres)) {
799 ERR("Could not get nsIDontent interface: %08x\n", nsres);
805 nsIContentSerializer_AppendElementStart(serializer, nscontent, nscontent, str);
808 nsIContentSerializer_AppendText(serializer, nscontent, 0, -1, str);
811 nsres = nsIContentSerializer_AppendComment(serializer, nscontent, 0, -1, str);
813 case DOCUMENT_NODE: {
815 nsIDOMNode_QueryInterface(nsnode, &IID_nsIDocument, (void**)&nsdoc);
816 nsIContentSerializer_AppendDocumentStart(serializer, nsdoc, str);
817 nsIDocument_Release(nsdoc);
820 case DOCUMENT_TYPE_NODE:
821 WARN("Ignoring DOCUMENT_TYPE_NODE\n");
823 case DOCUMENT_FRAGMENT_NODE:
826 FIXME("Unhandled type %u\n", type);
830 PRUint32 child_cnt, i;
831 nsIDOMNode *child_node;
833 nsIDOMNode_GetChildNodes(nsnode, &node_list);
834 nsIDOMNodeList_GetLength(node_list, &child_cnt);
836 for(i=0; i<child_cnt; i++) {
837 nsres = nsIDOMNodeList_Item(node_list, i, &child_node);
838 if(NS_SUCCEEDED(nsres)) {
839 nsnode_to_nsstring_rec(serializer, child_node, str);
840 nsIDOMNode_Release(child_node);
842 ERR("Item failed: %08x\n", nsres);
846 nsIDOMNodeList_Release(node_list);
849 if(type == ELEMENT_NODE)
850 nsIContentSerializer_AppendElementEnd(serializer, nscontent, str);
852 nsIContent_Release(nscontent);
856 HRESULT nsnode_to_nsstring(nsIDOMNode *nsnode, nsAString *str)
858 nsIContentSerializer *serializer;
862 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
863 NS_HTMLSERIALIZER_CONTRACTID, NULL, &IID_nsIContentSerializer,
864 (void**)&serializer);
865 if(NS_FAILED(nsres)) {
866 ERR("Could not get nsIContentSerializer: %08x\n", nsres);
870 nsres = nsIContentSerializer_Init(serializer, 0, 100, NULL, FALSE, FALSE /* FIXME */);
872 ERR("Init failed: %08x\n", nsres);
874 hres = nsnode_to_nsstring_rec(serializer, nsnode, str);
875 if(SUCCEEDED(hres)) {
876 nsres = nsIContentSerializer_Flush(serializer, str);
878 ERR("Flush failed: %08x\n", nsres);
881 nsIContentSerializer_Release(serializer);
885 void get_editor_controller(NSContainer *This)
887 nsIEditingSession *editing_session = NULL;
888 nsIControllerContext *ctrlctx;
892 nsIEditor_Release(This->editor);
896 if(This->editor_controller) {
897 nsIController_Release(This->editor_controller);
898 This->editor_controller = NULL;
901 nsres = get_nsinterface((nsISupports*)This->webbrowser, &IID_nsIEditingSession,
902 (void**)&editing_session);
903 if(NS_FAILED(nsres)) {
904 ERR("Could not get nsIEditingSession: %08x\n", nsres);
908 nsres = nsIEditingSession_GetEditorForWindow(editing_session,
909 This->doc->basedoc.window->nswindow, &This->editor);
910 nsIEditingSession_Release(editing_session);
911 if(NS_FAILED(nsres)) {
912 ERR("Could not get editor: %08x\n", nsres);
916 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
917 NS_EDITORCONTROLLER_CONTRACTID, NULL, &IID_nsIControllerContext, (void**)&ctrlctx);
918 if(NS_SUCCEEDED(nsres)) {
919 nsres = nsIControllerContext_SetCommandContext(ctrlctx, (nsISupports *)This->editor);
921 ERR("SetCommandContext failed: %08x\n", nsres);
922 nsres = nsIControllerContext_QueryInterface(ctrlctx, &IID_nsIController,
923 (void**)&This->editor_controller);
924 nsIControllerContext_Release(ctrlctx);
926 ERR("Could not get nsIController interface: %08x\n", nsres);
928 ERR("Could not create edit controller: %08x\n", nsres);
932 void close_gecko(void)
939 if(profile_directory) {
940 nsIFile_Release(profile_directory);
941 profile_directory = NULL;
945 nsIComponentManager_Release(pCompMgr);
948 nsIServiceManager_Release(pServMgr);
951 nsIMemory_Release(nsmem);
953 /* Gecko doesn't really support being unloaded */
954 /* if (hXPCOM) FreeLibrary(hXPCOM); */
957 BOOL is_gecko_path(const char *path)
962 buf = heap_strdupAtoW(path);
963 if(strlenW(buf) < gecko_path_len)
966 buf[gecko_path_len] = 0;
967 for(ptr = buf; *ptr; ptr++) {
972 ret = !strcmpiW(buf, gecko_path);
977 struct nsWeakReference {
978 nsIWeakReference nsIWeakReference_iface;
982 NSContainer *nscontainer;
985 static inline nsWeakReference *impl_from_nsIWeakReference(nsIWeakReference *iface)
987 return CONTAINING_RECORD(iface, nsWeakReference, nsIWeakReference_iface);
990 static nsresult NSAPI nsWeakReference_QueryInterface(nsIWeakReference *iface,
991 nsIIDRef riid, void **result)
993 nsWeakReference *This = impl_from_nsIWeakReference(iface);
995 if(IsEqualGUID(&IID_nsISupports, riid)) {
996 TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
997 *result = &This->nsIWeakReference_iface;
998 }else if(IsEqualGUID(&IID_nsIWeakReference, riid)) {
999 TRACE("(%p)->(IID_nsIWeakReference %p)\n", This, result);
1000 *result = &This->nsIWeakReference_iface;
1002 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
1004 return NS_NOINTERFACE;
1007 nsISupports_AddRef((nsISupports*)*result);
1011 static nsrefcnt NSAPI nsWeakReference_AddRef(nsIWeakReference *iface)
1013 nsWeakReference *This = impl_from_nsIWeakReference(iface);
1014 LONG ref = InterlockedIncrement(&This->ref);
1016 TRACE("(%p) ref=%d\n", This, ref);
1021 static nsrefcnt NSAPI nsWeakReference_Release(nsIWeakReference *iface)
1023 nsWeakReference *This = impl_from_nsIWeakReference(iface);
1024 LONG ref = InterlockedIncrement(&This->ref);
1026 TRACE("(%p) ref=%d\n", This, ref);
1029 assert(!This->nscontainer);
1036 static nsresult NSAPI nsWeakReference_QueryReferent(nsIWeakReference *iface,
1037 const nsIID *riid, void **result)
1039 nsWeakReference *This = impl_from_nsIWeakReference(iface);
1041 if(!This->nscontainer)
1042 return NS_ERROR_NULL_POINTER;
1044 return nsIWebBrowserChrome_QueryInterface(&This->nscontainer->nsIWebBrowserChrome_iface, riid, result);
1047 static const nsIWeakReferenceVtbl nsWeakReferenceVtbl = {
1048 nsWeakReference_QueryInterface,
1049 nsWeakReference_AddRef,
1050 nsWeakReference_Release,
1051 nsWeakReference_QueryReferent
1054 /**********************************************************
1055 * nsIWebBrowserChrome interface
1058 static inline NSContainer *impl_from_nsIWebBrowserChrome(nsIWebBrowserChrome *iface)
1060 return CONTAINING_RECORD(iface, NSContainer, nsIWebBrowserChrome_iface);
1063 static nsresult NSAPI nsWebBrowserChrome_QueryInterface(nsIWebBrowserChrome *iface,
1064 nsIIDRef riid, void **result)
1066 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1069 if(IsEqualGUID(&IID_nsISupports, riid)) {
1070 TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
1071 *result = &This->nsIWebBrowserChrome_iface;
1072 }else if(IsEqualGUID(&IID_nsIWebBrowserChrome, riid)) {
1073 TRACE("(%p)->(IID_nsIWebBrowserChrome, %p)\n", This, result);
1074 *result = &This->nsIWebBrowserChrome_iface;
1075 }else if(IsEqualGUID(&IID_nsIContextMenuListener, riid)) {
1076 TRACE("(%p)->(IID_nsIContextMenuListener, %p)\n", This, result);
1077 *result = &This->nsIContextMenuListener_iface;
1078 }else if(IsEqualGUID(&IID_nsIURIContentListener, riid)) {
1079 TRACE("(%p)->(IID_nsIURIContentListener %p)\n", This, result);
1080 *result = &This->nsIURIContentListener_iface;
1081 }else if(IsEqualGUID(&IID_nsIEmbeddingSiteWindow, riid)) {
1082 TRACE("(%p)->(IID_nsIEmbeddingSiteWindow %p)\n", This, result);
1083 *result = &This->nsIEmbeddingSiteWindow_iface;
1084 }else if(IsEqualGUID(&IID_nsITooltipListener, riid)) {
1085 TRACE("(%p)->(IID_nsITooltipListener %p)\n", This, result);
1086 *result = &This->nsITooltipListener_iface;
1087 }else if(IsEqualGUID(&IID_nsIInterfaceRequestor, riid)) {
1088 TRACE("(%p)->(IID_nsIInterfaceRequestor %p)\n", This, result);
1089 *result = &This->nsIInterfaceRequestor_iface;
1090 }else if(IsEqualGUID(&IID_nsISupportsWeakReference, riid)) {
1091 TRACE("(%p)->(IID_nsISupportsWeakReference %p)\n", This, result);
1092 *result = &This->nsISupportsWeakReference_iface;
1096 nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1100 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
1101 return NS_NOINTERFACE;
1104 static nsrefcnt NSAPI nsWebBrowserChrome_AddRef(nsIWebBrowserChrome *iface)
1106 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1107 LONG ref = InterlockedIncrement(&This->ref);
1109 TRACE("(%p) ref=%d\n", This, ref);
1114 static nsrefcnt NSAPI nsWebBrowserChrome_Release(nsIWebBrowserChrome *iface)
1116 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1117 LONG ref = InterlockedDecrement(&This->ref);
1119 TRACE("(%p) ref=%d\n", This, ref);
1123 nsIWebBrowserChrome_Release(&This->parent->nsIWebBrowserChrome_iface);
1124 if(This->weak_reference) {
1125 This->weak_reference->nscontainer = NULL;
1126 nsIWeakReference_Release(&This->weak_reference->nsIWeakReference_iface);
1134 static nsresult NSAPI nsWebBrowserChrome_SetStatus(nsIWebBrowserChrome *iface,
1135 PRUint32 statusType, const PRUnichar *status)
1137 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1138 TRACE("(%p)->(%d %s)\n", This, statusType, debugstr_w(status));
1142 static nsresult NSAPI nsWebBrowserChrome_GetWebBrowser(nsIWebBrowserChrome *iface,
1143 nsIWebBrowser **aWebBrowser)
1145 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1147 TRACE("(%p)->(%p)\n", This, aWebBrowser);
1150 return NS_ERROR_INVALID_ARG;
1152 if(This->webbrowser)
1153 nsIWebBrowser_AddRef(This->webbrowser);
1154 *aWebBrowser = This->webbrowser;
1158 static nsresult NSAPI nsWebBrowserChrome_SetWebBrowser(nsIWebBrowserChrome *iface,
1159 nsIWebBrowser *aWebBrowser)
1161 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1163 TRACE("(%p)->(%p)\n", This, aWebBrowser);
1165 if(aWebBrowser != This->webbrowser)
1166 ERR("Wrong nsWebBrowser!\n");
1171 static nsresult NSAPI nsWebBrowserChrome_GetChromeFlags(nsIWebBrowserChrome *iface,
1172 PRUint32 *aChromeFlags)
1174 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1175 WARN("(%p)->(%p)\n", This, aChromeFlags);
1176 return NS_ERROR_NOT_IMPLEMENTED;
1179 static nsresult NSAPI nsWebBrowserChrome_SetChromeFlags(nsIWebBrowserChrome *iface,
1180 PRUint32 aChromeFlags)
1182 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1183 WARN("(%p)->(%08x)\n", This, aChromeFlags);
1184 return NS_ERROR_NOT_IMPLEMENTED;
1187 static nsresult NSAPI nsWebBrowserChrome_DestroyBrowserWindow(nsIWebBrowserChrome *iface)
1189 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1190 TRACE("(%p)\n", This);
1191 return NS_ERROR_NOT_IMPLEMENTED;
1194 static nsresult NSAPI nsWebBrowserChrome_SizeBrowserTo(nsIWebBrowserChrome *iface,
1195 PRInt32 aCX, PRInt32 aCY)
1197 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1198 WARN("(%p)->(%d %d)\n", This, aCX, aCY);
1199 return NS_ERROR_NOT_IMPLEMENTED;
1202 static nsresult NSAPI nsWebBrowserChrome_ShowAsModal(nsIWebBrowserChrome *iface)
1204 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1205 WARN("(%p)\n", This);
1206 return NS_ERROR_NOT_IMPLEMENTED;
1209 static nsresult NSAPI nsWebBrowserChrome_IsWindowModal(nsIWebBrowserChrome *iface, PRBool *_retval)
1211 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1212 WARN("(%p)->(%p)\n", This, _retval);
1213 return NS_ERROR_NOT_IMPLEMENTED;
1216 static nsresult NSAPI nsWebBrowserChrome_ExitModalEventLoop(nsIWebBrowserChrome *iface,
1219 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1220 WARN("(%p)->(%08x)\n", This, aStatus);
1221 return NS_ERROR_NOT_IMPLEMENTED;
1224 static const nsIWebBrowserChromeVtbl nsWebBrowserChromeVtbl = {
1225 nsWebBrowserChrome_QueryInterface,
1226 nsWebBrowserChrome_AddRef,
1227 nsWebBrowserChrome_Release,
1228 nsWebBrowserChrome_SetStatus,
1229 nsWebBrowserChrome_GetWebBrowser,
1230 nsWebBrowserChrome_SetWebBrowser,
1231 nsWebBrowserChrome_GetChromeFlags,
1232 nsWebBrowserChrome_SetChromeFlags,
1233 nsWebBrowserChrome_DestroyBrowserWindow,
1234 nsWebBrowserChrome_SizeBrowserTo,
1235 nsWebBrowserChrome_ShowAsModal,
1236 nsWebBrowserChrome_IsWindowModal,
1237 nsWebBrowserChrome_ExitModalEventLoop
1240 /**********************************************************
1241 * nsIContextMenuListener interface
1244 static inline NSContainer *impl_from_nsIContextMenuListener(nsIContextMenuListener *iface)
1246 return CONTAINING_RECORD(iface, NSContainer, nsIContextMenuListener_iface);
1249 static nsresult NSAPI nsContextMenuListener_QueryInterface(nsIContextMenuListener *iface,
1250 nsIIDRef riid, void **result)
1252 NSContainer *This = impl_from_nsIContextMenuListener(iface);
1253 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1256 static nsrefcnt NSAPI nsContextMenuListener_AddRef(nsIContextMenuListener *iface)
1258 NSContainer *This = impl_from_nsIContextMenuListener(iface);
1259 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1262 static nsrefcnt NSAPI nsContextMenuListener_Release(nsIContextMenuListener *iface)
1264 NSContainer *This = impl_from_nsIContextMenuListener(iface);
1265 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
1268 static nsresult NSAPI nsContextMenuListener_OnShowContextMenu(nsIContextMenuListener *iface,
1269 PRUint32 aContextFlags, nsIDOMEvent *aEvent, nsIDOMNode *aNode)
1271 NSContainer *This = impl_from_nsIContextMenuListener(iface);
1272 nsIDOMMouseEvent *event;
1275 DWORD dwID = CONTEXT_MENU_DEFAULT;
1279 TRACE("(%p)->(%08x %p %p)\n", This, aContextFlags, aEvent, aNode);
1281 fire_event(This->doc->basedoc.doc_node /* FIXME */, EVENTID_CONTEXTMENU, TRUE, aNode, aEvent);
1283 nsres = nsIDOMEvent_QueryInterface(aEvent, &IID_nsIDOMMouseEvent, (void**)&event);
1284 if(NS_FAILED(nsres)) {
1285 ERR("Could not get nsIDOMMouseEvent interface: %08x\n", nsres);
1289 nsIDOMMouseEvent_GetScreenX(event, &pt.x);
1290 nsIDOMMouseEvent_GetScreenY(event, &pt.y);
1291 nsIDOMMouseEvent_Release(event);
1293 switch(aContextFlags) {
1295 case CONTEXT_DOCUMENT:
1297 dwID = CONTEXT_MENU_DEFAULT;
1300 case CONTEXT_IMAGE|CONTEXT_LINK:
1301 dwID = CONTEXT_MENU_IMAGE;
1304 dwID = CONTEXT_MENU_ANCHOR;
1307 dwID = CONTEXT_MENU_CONTROL;
1310 FIXME("aContextFlags=%08x\n", aContextFlags);
1313 hres = get_node(This->doc->basedoc.doc_node, aNode, TRUE, &node);
1315 return NS_ERROR_FAILURE;
1317 show_context_menu(This->doc, dwID, &pt, (IDispatch*)&node->IHTMLDOMNode_iface);
1321 static const nsIContextMenuListenerVtbl nsContextMenuListenerVtbl = {
1322 nsContextMenuListener_QueryInterface,
1323 nsContextMenuListener_AddRef,
1324 nsContextMenuListener_Release,
1325 nsContextMenuListener_OnShowContextMenu
1328 /**********************************************************
1329 * nsIURIContentListener interface
1332 static inline NSContainer *impl_from_nsIURIContentListener(nsIURIContentListener *iface)
1334 return CONTAINING_RECORD(iface, NSContainer, nsIURIContentListener_iface);
1337 static nsresult NSAPI nsURIContentListener_QueryInterface(nsIURIContentListener *iface,
1338 nsIIDRef riid, void **result)
1340 NSContainer *This = impl_from_nsIURIContentListener(iface);
1341 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1344 static nsrefcnt NSAPI nsURIContentListener_AddRef(nsIURIContentListener *iface)
1346 NSContainer *This = impl_from_nsIURIContentListener(iface);
1347 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1350 static nsrefcnt NSAPI nsURIContentListener_Release(nsIURIContentListener *iface)
1352 NSContainer *This = impl_from_nsIURIContentListener(iface);
1353 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
1356 static nsresult NSAPI nsURIContentListener_OnStartURIOpen(nsIURIContentListener *iface,
1357 nsIURI *aURI, PRBool *_retval)
1359 NSContainer *This = impl_from_nsIURIContentListener(iface);
1360 nsACString spec_str;
1364 nsACString_Init(&spec_str, NULL);
1365 nsIURI_GetSpec(aURI, &spec_str);
1366 nsACString_GetData(&spec_str, &spec);
1368 TRACE("(%p)->(%p(%s) %p)\n", This, aURI, debugstr_a(spec), _retval);
1370 nsACString_Finish(&spec_str);
1372 nsres = on_start_uri_open(This, aURI, _retval);
1373 if(NS_FAILED(nsres))
1376 return !*_retval && This->content_listener
1377 ? nsIURIContentListener_OnStartURIOpen(This->content_listener, aURI, _retval)
1381 static nsresult NSAPI nsURIContentListener_DoContent(nsIURIContentListener *iface,
1382 const char *aContentType, PRBool aIsContentPreferred, nsIRequest *aRequest,
1383 nsIStreamListener **aContentHandler, PRBool *_retval)
1385 NSContainer *This = impl_from_nsIURIContentListener(iface);
1387 TRACE("(%p)->(%s %x %p %p %p)\n", This, debugstr_a(aContentType), aIsContentPreferred,
1388 aRequest, aContentHandler, _retval);
1390 return This->content_listener
1391 ? nsIURIContentListener_DoContent(This->content_listener, aContentType,
1392 aIsContentPreferred, aRequest, aContentHandler, _retval)
1393 : NS_ERROR_NOT_IMPLEMENTED;
1396 static nsresult NSAPI nsURIContentListener_IsPreferred(nsIURIContentListener *iface,
1397 const char *aContentType, char **aDesiredContentType, PRBool *_retval)
1399 NSContainer *This = impl_from_nsIURIContentListener(iface);
1401 TRACE("(%p)->(%s %p %p)\n", This, debugstr_a(aContentType), aDesiredContentType, _retval);
1403 /* FIXME: Should we do something here? */
1406 return This->content_listener
1407 ? nsIURIContentListener_IsPreferred(This->content_listener, aContentType,
1408 aDesiredContentType, _retval)
1412 static nsresult NSAPI nsURIContentListener_CanHandleContent(nsIURIContentListener *iface,
1413 const char *aContentType, PRBool aIsContentPreferred, char **aDesiredContentType,
1416 NSContainer *This = impl_from_nsIURIContentListener(iface);
1418 TRACE("(%p)->(%s %x %p %p)\n", This, debugstr_a(aContentType), aIsContentPreferred,
1419 aDesiredContentType, _retval);
1421 return This->content_listener
1422 ? nsIURIContentListener_CanHandleContent(This->content_listener, aContentType,
1423 aIsContentPreferred, aDesiredContentType, _retval)
1424 : NS_ERROR_NOT_IMPLEMENTED;
1427 static nsresult NSAPI nsURIContentListener_GetLoadCookie(nsIURIContentListener *iface,
1428 nsISupports **aLoadCookie)
1430 NSContainer *This = impl_from_nsIURIContentListener(iface);
1432 WARN("(%p)->(%p)\n", This, aLoadCookie);
1434 return This->content_listener
1435 ? nsIURIContentListener_GetLoadCookie(This->content_listener, aLoadCookie)
1436 : NS_ERROR_NOT_IMPLEMENTED;
1439 static nsresult NSAPI nsURIContentListener_SetLoadCookie(nsIURIContentListener *iface,
1440 nsISupports *aLoadCookie)
1442 NSContainer *This = impl_from_nsIURIContentListener(iface);
1444 WARN("(%p)->(%p)\n", This, aLoadCookie);
1446 return This->content_listener
1447 ? nsIURIContentListener_SetLoadCookie(This->content_listener, aLoadCookie)
1448 : NS_ERROR_NOT_IMPLEMENTED;
1451 static nsresult NSAPI nsURIContentListener_GetParentContentListener(nsIURIContentListener *iface,
1452 nsIURIContentListener **aParentContentListener)
1454 NSContainer *This = impl_from_nsIURIContentListener(iface);
1456 TRACE("(%p)->(%p)\n", This, aParentContentListener);
1458 if(This->content_listener)
1459 nsIURIContentListener_AddRef(This->content_listener);
1461 *aParentContentListener = This->content_listener;
1465 static nsresult NSAPI nsURIContentListener_SetParentContentListener(nsIURIContentListener *iface,
1466 nsIURIContentListener *aParentContentListener)
1468 NSContainer *This = impl_from_nsIURIContentListener(iface);
1470 TRACE("(%p)->(%p)\n", This, aParentContentListener);
1472 if(aParentContentListener == &This->nsIURIContentListener_iface)
1475 if(This->content_listener)
1476 nsIURIContentListener_Release(This->content_listener);
1478 This->content_listener = aParentContentListener;
1479 if(This->content_listener)
1480 nsIURIContentListener_AddRef(This->content_listener);
1485 static const nsIURIContentListenerVtbl nsURIContentListenerVtbl = {
1486 nsURIContentListener_QueryInterface,
1487 nsURIContentListener_AddRef,
1488 nsURIContentListener_Release,
1489 nsURIContentListener_OnStartURIOpen,
1490 nsURIContentListener_DoContent,
1491 nsURIContentListener_IsPreferred,
1492 nsURIContentListener_CanHandleContent,
1493 nsURIContentListener_GetLoadCookie,
1494 nsURIContentListener_SetLoadCookie,
1495 nsURIContentListener_GetParentContentListener,
1496 nsURIContentListener_SetParentContentListener
1499 /**********************************************************
1500 * nsIEmbeddinSiteWindow interface
1503 static inline NSContainer *impl_from_nsIEmbeddingSiteWindow(nsIEmbeddingSiteWindow *iface)
1505 return CONTAINING_RECORD(iface, NSContainer, nsIEmbeddingSiteWindow_iface);
1508 static nsresult NSAPI nsEmbeddingSiteWindow_QueryInterface(nsIEmbeddingSiteWindow *iface,
1509 nsIIDRef riid, void **result)
1511 NSContainer *This = impl_from_nsIEmbeddingSiteWindow(iface);
1512 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1515 static nsrefcnt NSAPI nsEmbeddingSiteWindow_AddRef(nsIEmbeddingSiteWindow *iface)
1517 NSContainer *This = impl_from_nsIEmbeddingSiteWindow(iface);
1518 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1521 static nsrefcnt NSAPI nsEmbeddingSiteWindow_Release(nsIEmbeddingSiteWindow *iface)
1523 NSContainer *This = impl_from_nsIEmbeddingSiteWindow(iface);
1524 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
1527 static nsresult NSAPI nsEmbeddingSiteWindow_SetDimensions(nsIEmbeddingSiteWindow *iface,
1528 PRUint32 flags, PRInt32 x, PRInt32 y, PRInt32 cx, PRInt32 cy)
1530 NSContainer *This = impl_from_nsIEmbeddingSiteWindow(iface);
1531 WARN("(%p)->(%08x %d %d %d %d)\n", This, flags, x, y, cx, cy);
1532 return NS_ERROR_NOT_IMPLEMENTED;
1535 static nsresult NSAPI nsEmbeddingSiteWindow_GetDimensions(nsIEmbeddingSiteWindow *iface,
1536 PRUint32 flags, PRInt32 *x, PRInt32 *y, PRInt32 *cx, PRInt32 *cy)
1538 NSContainer *This = impl_from_nsIEmbeddingSiteWindow(iface);
1539 WARN("(%p)->(%08x %p %p %p %p)\n", This, flags, x, y, cx, cy);
1540 return NS_ERROR_NOT_IMPLEMENTED;
1543 static nsresult NSAPI nsEmbeddingSiteWindow_SetFocus(nsIEmbeddingSiteWindow *iface)
1545 NSContainer *This = impl_from_nsIEmbeddingSiteWindow(iface);
1547 TRACE("(%p)\n", This);
1549 return nsIBaseWindow_SetFocus(This->window);
1552 static nsresult NSAPI nsEmbeddingSiteWindow_GetVisibility(nsIEmbeddingSiteWindow *iface,
1553 PRBool *aVisibility)
1555 NSContainer *This = impl_from_nsIEmbeddingSiteWindow(iface);
1557 TRACE("(%p)->(%p)\n", This, aVisibility);
1559 *aVisibility = This->doc && This->doc->hwnd && IsWindowVisible(This->doc->hwnd);
1563 static nsresult NSAPI nsEmbeddingSiteWindow_SetVisibility(nsIEmbeddingSiteWindow *iface,
1566 NSContainer *This = impl_from_nsIEmbeddingSiteWindow(iface);
1568 TRACE("(%p)->(%x)\n", This, aVisibility);
1573 static nsresult NSAPI nsEmbeddingSiteWindow_GetTitle(nsIEmbeddingSiteWindow *iface,
1576 NSContainer *This = impl_from_nsIEmbeddingSiteWindow(iface);
1577 WARN("(%p)->(%p)\n", This, aTitle);
1578 return NS_ERROR_NOT_IMPLEMENTED;
1581 static nsresult NSAPI nsEmbeddingSiteWindow_SetTitle(nsIEmbeddingSiteWindow *iface,
1582 const PRUnichar *aTitle)
1584 NSContainer *This = impl_from_nsIEmbeddingSiteWindow(iface);
1585 WARN("(%p)->(%s)\n", This, debugstr_w(aTitle));
1586 return NS_ERROR_NOT_IMPLEMENTED;
1589 static nsresult NSAPI nsEmbeddingSiteWindow_GetSiteWindow(nsIEmbeddingSiteWindow *iface,
1592 NSContainer *This = impl_from_nsIEmbeddingSiteWindow(iface);
1594 TRACE("(%p)->(%p)\n", This, aSiteWindow);
1596 *aSiteWindow = This->hwnd;
1600 static const nsIEmbeddingSiteWindowVtbl nsEmbeddingSiteWindowVtbl = {
1601 nsEmbeddingSiteWindow_QueryInterface,
1602 nsEmbeddingSiteWindow_AddRef,
1603 nsEmbeddingSiteWindow_Release,
1604 nsEmbeddingSiteWindow_SetDimensions,
1605 nsEmbeddingSiteWindow_GetDimensions,
1606 nsEmbeddingSiteWindow_SetFocus,
1607 nsEmbeddingSiteWindow_GetVisibility,
1608 nsEmbeddingSiteWindow_SetVisibility,
1609 nsEmbeddingSiteWindow_GetTitle,
1610 nsEmbeddingSiteWindow_SetTitle,
1611 nsEmbeddingSiteWindow_GetSiteWindow
1614 static inline NSContainer *impl_from_nsITooltipListener(nsITooltipListener *iface)
1616 return CONTAINING_RECORD(iface, NSContainer, nsITooltipListener_iface);
1619 static nsresult NSAPI nsTooltipListener_QueryInterface(nsITooltipListener *iface, nsIIDRef riid,
1622 NSContainer *This = impl_from_nsITooltipListener(iface);
1623 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1626 static nsrefcnt NSAPI nsTooltipListener_AddRef(nsITooltipListener *iface)
1628 NSContainer *This = impl_from_nsITooltipListener(iface);
1629 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1632 static nsrefcnt NSAPI nsTooltipListener_Release(nsITooltipListener *iface)
1634 NSContainer *This = impl_from_nsITooltipListener(iface);
1635 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
1638 static nsresult NSAPI nsTooltipListener_OnShowTooltip(nsITooltipListener *iface,
1639 PRInt32 aXCoord, PRInt32 aYCoord, const PRUnichar *aTipText)
1641 NSContainer *This = impl_from_nsITooltipListener(iface);
1644 show_tooltip(This->doc, aXCoord, aYCoord, aTipText);
1649 static nsresult NSAPI nsTooltipListener_OnHideTooltip(nsITooltipListener *iface)
1651 NSContainer *This = impl_from_nsITooltipListener(iface);
1654 hide_tooltip(This->doc);
1659 static const nsITooltipListenerVtbl nsTooltipListenerVtbl = {
1660 nsTooltipListener_QueryInterface,
1661 nsTooltipListener_AddRef,
1662 nsTooltipListener_Release,
1663 nsTooltipListener_OnShowTooltip,
1664 nsTooltipListener_OnHideTooltip
1667 static inline NSContainer *impl_from_nsIInterfaceRequestor(nsIInterfaceRequestor *iface)
1669 return CONTAINING_RECORD(iface, NSContainer, nsIInterfaceRequestor_iface);
1672 static nsresult NSAPI nsInterfaceRequestor_QueryInterface(nsIInterfaceRequestor *iface,
1673 nsIIDRef riid, void **result)
1675 NSContainer *This = impl_from_nsIInterfaceRequestor(iface);
1676 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1679 static nsrefcnt NSAPI nsInterfaceRequestor_AddRef(nsIInterfaceRequestor *iface)
1681 NSContainer *This = impl_from_nsIInterfaceRequestor(iface);
1682 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1685 static nsrefcnt NSAPI nsInterfaceRequestor_Release(nsIInterfaceRequestor *iface)
1687 NSContainer *This = impl_from_nsIInterfaceRequestor(iface);
1688 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
1691 static nsresult NSAPI nsInterfaceRequestor_GetInterface(nsIInterfaceRequestor *iface,
1692 nsIIDRef riid, void **result)
1694 NSContainer *This = impl_from_nsIInterfaceRequestor(iface);
1696 if(IsEqualGUID(&IID_nsIDOMWindow, riid)) {
1697 TRACE("(%p)->(IID_nsIDOMWindow %p)\n", This, result);
1698 return nsIWebBrowser_GetContentDOMWindow(This->webbrowser, (nsIDOMWindow**)result);
1701 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1704 static const nsIInterfaceRequestorVtbl nsInterfaceRequestorVtbl = {
1705 nsInterfaceRequestor_QueryInterface,
1706 nsInterfaceRequestor_AddRef,
1707 nsInterfaceRequestor_Release,
1708 nsInterfaceRequestor_GetInterface
1711 static inline NSContainer *impl_from_nsISupportsWeakReference(nsISupportsWeakReference *iface)
1713 return CONTAINING_RECORD(iface, NSContainer, nsISupportsWeakReference_iface);
1716 static nsresult NSAPI nsSupportsWeakReference_QueryInterface(nsISupportsWeakReference *iface,
1717 nsIIDRef riid, void **result)
1719 NSContainer *This = impl_from_nsISupportsWeakReference(iface);
1720 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1723 static nsrefcnt NSAPI nsSupportsWeakReference_AddRef(nsISupportsWeakReference *iface)
1725 NSContainer *This = impl_from_nsISupportsWeakReference(iface);
1726 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1729 static nsrefcnt NSAPI nsSupportsWeakReference_Release(nsISupportsWeakReference *iface)
1731 NSContainer *This = impl_from_nsISupportsWeakReference(iface);
1732 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
1735 static nsresult NSAPI nsSupportsWeakReference_GetWeakReference(nsISupportsWeakReference *iface,
1736 nsIWeakReference **_retval)
1738 NSContainer *This = impl_from_nsISupportsWeakReference(iface);
1740 TRACE("(%p)->(%p)\n", This, _retval);
1742 if(!This->weak_reference) {
1743 This->weak_reference = heap_alloc(sizeof(nsWeakReference));
1744 if(!This->weak_reference)
1745 return NS_ERROR_OUT_OF_MEMORY;
1747 This->weak_reference->nsIWeakReference_iface.lpVtbl = &nsWeakReferenceVtbl;
1748 This->weak_reference->ref = 1;
1749 This->weak_reference->nscontainer = This;
1752 *_retval = &This->weak_reference->nsIWeakReference_iface;
1753 nsIWeakReference_AddRef(*_retval);
1757 static const nsISupportsWeakReferenceVtbl nsSupportsWeakReferenceVtbl = {
1758 nsSupportsWeakReference_QueryInterface,
1759 nsSupportsWeakReference_AddRef,
1760 nsSupportsWeakReference_Release,
1761 nsSupportsWeakReference_GetWeakReference
1764 nsresult create_chrome_window(nsIWebBrowserChrome *parent, nsIWebBrowserChrome **ret)
1766 NSContainer *new_container;
1768 if(parent->lpVtbl != &nsWebBrowserChromeVtbl)
1769 return NS_ERROR_UNEXPECTED;
1771 new_container = NSContainer_Create(NULL, impl_from_nsIWebBrowserChrome(parent));
1772 *ret = &new_container->nsIWebBrowserChrome_iface;
1776 NSContainer *NSContainer_Create(HTMLDocumentObj *doc, NSContainer *parent)
1778 nsIWebBrowserSetup *wbsetup;
1779 nsIScrollable *scrollable;
1783 if(!load_gecko(TRUE))
1786 ret = heap_alloc_zero(sizeof(NSContainer));
1788 ret->nsIWebBrowserChrome_iface.lpVtbl = &nsWebBrowserChromeVtbl;
1789 ret->nsIContextMenuListener_iface.lpVtbl = &nsContextMenuListenerVtbl;
1790 ret->nsIURIContentListener_iface.lpVtbl = &nsURIContentListenerVtbl;
1791 ret->nsIEmbeddingSiteWindow_iface.lpVtbl = &nsEmbeddingSiteWindowVtbl;
1792 ret->nsITooltipListener_iface.lpVtbl = &nsTooltipListenerVtbl;
1793 ret->nsIInterfaceRequestor_iface.lpVtbl = &nsInterfaceRequestorVtbl;
1794 ret->nsISupportsWeakReference_iface.lpVtbl = &nsSupportsWeakReferenceVtbl;
1799 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_WEBBROWSER_CONTRACTID,
1800 NULL, &IID_nsIWebBrowser, (void**)&ret->webbrowser);
1801 if(NS_FAILED(nsres)) {
1802 ERR("Creating WebBrowser failed: %08x\n", nsres);
1808 nsIWebBrowserChrome_AddRef(&parent->nsIWebBrowserChrome_iface);
1809 ret->parent = parent;
1811 nsres = nsIWebBrowser_SetContainerWindow(ret->webbrowser, &ret->nsIWebBrowserChrome_iface);
1812 if(NS_FAILED(nsres))
1813 ERR("SetContainerWindow failed: %08x\n", nsres);
1815 nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIBaseWindow,
1816 (void**)&ret->window);
1817 if(NS_FAILED(nsres))
1818 ERR("Could not get nsIBaseWindow interface: %08x\n", nsres);
1820 nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIWebBrowserSetup,
1822 if(NS_SUCCEEDED(nsres)) {
1823 nsres = nsIWebBrowserSetup_SetProperty(wbsetup, SETUP_IS_CHROME_WRAPPER, FALSE);
1824 nsIWebBrowserSetup_Release(wbsetup);
1825 if(NS_FAILED(nsres))
1826 ERR("SetProperty(SETUP_IS_CHROME_WRAPPER) failed: %08x\n", nsres);
1828 ERR("Could not get nsIWebBrowserSetup interface\n");
1831 nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIWebNavigation,
1832 (void**)&ret->navigation);
1833 if(NS_FAILED(nsres))
1834 ERR("Could not get nsIWebNavigation interface: %08x\n", nsres);
1836 nsres = nsIWebBrowserFocus_QueryInterface(ret->webbrowser, &IID_nsIWebBrowserFocus,
1837 (void**)&ret->focus);
1838 if(NS_FAILED(nsres))
1839 ERR("Could not get nsIWebBrowserFocus interface: %08x\n", nsres);
1841 if(!nscontainer_class)
1842 register_nscontainer_class();
1844 ret->hwnd = CreateWindowExW(0, wszNsContainer, NULL,
1845 WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 0, 0, 100, 100,
1846 GetDesktopWindow(), NULL, hInst, ret);
1848 nsres = nsIBaseWindow_InitWindow(ret->window, ret->hwnd, NULL, 0, 0, 100, 100);
1849 if(NS_SUCCEEDED(nsres)) {
1850 nsres = nsIBaseWindow_Create(ret->window);
1851 if(NS_FAILED(nsres))
1852 WARN("Creating window failed: %08x\n", nsres);
1854 nsIBaseWindow_SetVisibility(ret->window, FALSE);
1855 nsIBaseWindow_SetEnabled(ret->window, FALSE);
1857 ERR("InitWindow failed: %08x\n", nsres);
1860 nsres = nsIWebBrowser_SetParentURIContentListener(ret->webbrowser,
1861 &ret->nsIURIContentListener_iface);
1862 if(NS_FAILED(nsres))
1863 ERR("SetParentURIContentListener failed: %08x\n", nsres);
1865 nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIScrollable, (void**)&scrollable);
1866 if(NS_SUCCEEDED(nsres)) {
1867 nsres = nsIScrollable_SetDefaultScrollbarPreferences(scrollable,
1868 ScrollOrientation_Y, Scrollbar_Always);
1869 if(NS_FAILED(nsres))
1870 ERR("Could not set default Y scrollbar prefs: %08x\n", nsres);
1872 nsres = nsIScrollable_SetDefaultScrollbarPreferences(scrollable,
1873 ScrollOrientation_X, Scrollbar_Auto);
1874 if(NS_FAILED(nsres))
1875 ERR("Could not set default X scrollbar prefs: %08x\n", nsres);
1877 nsIScrollable_Release(scrollable);
1879 ERR("Could not get nsIScrollable: %08x\n", nsres);
1885 void NSContainer_Release(NSContainer *This)
1887 TRACE("(%p)\n", This);
1891 ShowWindow(This->hwnd, SW_HIDE);
1892 SetParent(This->hwnd, NULL);
1894 nsIBaseWindow_SetVisibility(This->window, FALSE);
1895 nsIBaseWindow_Destroy(This->window);
1897 nsIWebBrowser_SetContainerWindow(This->webbrowser, NULL);
1899 nsIWebBrowser_Release(This->webbrowser);
1900 This->webbrowser = NULL;
1902 nsIWebNavigation_Release(This->navigation);
1903 This->navigation = NULL;
1905 nsIBaseWindow_Release(This->window);
1906 This->window = NULL;
1908 nsIWebBrowserFocus_Release(This->focus);
1911 if(This->editor_controller) {
1912 nsIController_Release(This->editor_controller);
1913 This->editor_controller = NULL;
1917 nsIEditor_Release(This->editor);
1918 This->editor = NULL;
1921 if(This->content_listener) {
1922 nsIURIContentListener_Release(This->content_listener);
1923 This->content_listener = NULL;
1927 DestroyWindow(This->hwnd);
1931 nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);