push 5aff8350ceade24f8243f07a9cf7ecb816236fb1
[wine/hacks.git] / dlls / mshtml / nsembed.c
blob0464e0597b0b000252e454dec80f4ec468e712fb
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>
23 #define COBJMACROS
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "winreg.h"
29 #include "ole2.h"
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
34 #include "mshtml_private.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
37 WINE_DECLARE_DEBUG_CHANNEL(gecko);
39 #define NS_APPSTARTUPNOTIFIER_CONTRACTID "@mozilla.org/embedcomp/appstartup-notifier;1"
40 #define NS_WEBBROWSER_CONTRACTID "@mozilla.org/embedding/browser/nsWebBrowser;1"
41 #define NS_PROFILE_CONTRACTID "@mozilla.org/profile/manager;1"
42 #define NS_MEMORY_CONTRACTID "@mozilla.org/xpcom/memory-service;1"
43 #define NS_STRINGSTREAM_CONTRACTID "@mozilla.org/io/string-input-stream;1"
44 #define NS_COMMANDPARAMS_CONTRACTID "@mozilla.org/embedcomp/command-params;1"
45 #define NS_HTMLSERIALIZER_CONTRACTID "@mozilla.org/layout/contentserializer;1?mimetype=text/html"
46 #define NS_EDITORCONTROLLER_CONTRACTID "@mozilla.org/editor/editorcontroller;1"
47 #define NS_ARRAY_CONTRACTID "@mozilla.org/array;1"
48 #define NS_VARIANT_CONTRACTID "@mozilla.org/variant;1"
49 #define NS_PREFERENCES_CONTRACTID "@mozilla.org/preferences;1"
51 #define APPSTARTUP_TOPIC "app-startup"
53 #define PR_UINT32_MAX 0xffffffff
55 struct nsCStringContainer {
56 void *v;
57 void *d1;
58 PRUint32 d2;
59 void *d3;
62 static nsresult (*NS_InitXPCOM2)(nsIServiceManager**,void*,void*);
63 static nsresult (*NS_ShutdownXPCOM)(nsIServiceManager*);
64 static nsresult (*NS_GetComponentRegistrar)(nsIComponentRegistrar**);
65 static nsresult (*NS_StringContainerInit)(nsStringContainer*);
66 static nsresult (*NS_CStringContainerInit)(nsCStringContainer*);
67 static nsresult (*NS_StringContainerFinish)(nsStringContainer*);
68 static nsresult (*NS_CStringContainerFinish)(nsCStringContainer*);
69 static nsresult (*NS_StringSetData)(nsAString*,const PRUnichar*,PRUint32);
70 static nsresult (*NS_CStringSetData)(nsACString*,const char*,PRUint32);
71 static nsresult (*NS_NewLocalFile)(const nsAString*,PRBool,nsIFile**);
72 static PRUint32 (*NS_StringGetData)(const nsAString*,const PRUnichar **,PRBool*);
73 static PRUint32 (*NS_CStringGetData)(const nsACString*,const char**,PRBool*);
75 static HINSTANCE hXPCOM = NULL;
77 static nsIServiceManager *pServMgr = NULL;
78 static nsIComponentManager *pCompMgr = NULL;
79 static nsIMemory *nsmem = NULL;
81 static const WCHAR wszNsContainer[] = {'N','s','C','o','n','t','a','i','n','e','r',0};
83 static ATOM nscontainer_class;
85 #define WM_RESETFOCUS_HACK WM_USER+600
87 static LRESULT WINAPI nsembed_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
89 NSContainer *This;
90 nsresult nsres;
92 static const WCHAR wszTHIS[] = {'T','H','I','S',0};
94 if(msg == WM_CREATE) {
95 This = *(NSContainer**)lParam;
96 SetPropW(hwnd, wszTHIS, This);
97 }else {
98 This = GetPropW(hwnd, wszTHIS);
101 switch(msg) {
102 case WM_SIZE:
103 TRACE("(%p)->(WM_SIZE)\n", This);
105 nsres = nsIBaseWindow_SetSize(This->window,
106 LOWORD(lParam), HIWORD(lParam), TRUE);
107 if(NS_FAILED(nsres))
108 WARN("SetSize failed: %08x\n", nsres);
109 break;
111 case WM_RESETFOCUS_HACK:
113 * FIXME
114 * Gecko grabs focus in edit mode and some apps don't like it.
115 * We should somehow prevent grabbing focus.
118 TRACE("WM_RESETFOCUS_HACK\n");
120 if(This->reset_focus) {
121 SetFocus(This->reset_focus);
122 This->reset_focus = NULL;
123 if(This->doc)
124 This->doc->focus = FALSE;
128 return DefWindowProcW(hwnd, msg, wParam, lParam);
132 static void register_nscontainer_class(void)
134 static WNDCLASSEXW wndclass = {
135 sizeof(WNDCLASSEXW),
136 CS_DBLCLKS,
137 nsembed_proc,
138 0, 0, NULL, NULL, NULL, NULL, NULL,
139 wszNsContainer,
140 NULL,
142 wndclass.hInstance = hInst;
143 nscontainer_class = RegisterClassExW(&wndclass);
146 static void set_environment(LPCWSTR gre_path)
148 WCHAR path_env[MAX_PATH], buf[20];
149 int len, debug_level = 0;
151 static const WCHAR pathW[] = {'P','A','T','H',0};
152 static const WCHAR warnW[] = {'w','a','r','n',0};
153 static const WCHAR xpcom_debug_breakW[] =
154 {'X','P','C','O','M','_','D','E','B','U','G','_','B','R','E','A','K',0};
155 static const WCHAR nspr_log_modulesW[] =
156 {'N','S','P','R','_','L','O','G','_','M','O','D','U','L','E','S',0};
157 static const WCHAR debug_formatW[] = {'a','l','l',':','%','d',0};
159 /* We have to modify PATH as XPCOM loads other DLLs from this directory. */
160 GetEnvironmentVariableW(pathW, path_env, sizeof(path_env)/sizeof(WCHAR));
161 len = strlenW(path_env);
162 path_env[len++] = ';';
163 strcpyW(path_env+len, gre_path);
164 SetEnvironmentVariableW(pathW, path_env);
166 SetEnvironmentVariableW(xpcom_debug_breakW, warnW);
168 if(TRACE_ON(gecko))
169 debug_level = 5;
170 else if(WARN_ON(gecko))
171 debug_level = 3;
172 else if(ERR_ON(gecko))
173 debug_level = 2;
175 sprintfW(buf, debug_formatW, debug_level);
176 SetEnvironmentVariableW(nspr_log_modulesW, buf);
179 static BOOL load_xpcom(const PRUnichar *gre_path)
181 static const WCHAR strXPCOM[] = {'x','p','c','o','m','.','d','l','l',0};
183 TRACE("(%s)\n", debugstr_w(gre_path));
185 set_environment(gre_path);
187 hXPCOM = LoadLibraryW(strXPCOM);
188 if(!hXPCOM) {
189 WARN("Could not load XPCOM: %d\n", GetLastError());
190 return FALSE;
193 #define NS_DLSYM(func) \
194 func = (void *)GetProcAddress(hXPCOM, #func); \
195 if(!func) \
196 ERR("Could not GetProcAddress(" #func ") failed\n")
198 NS_DLSYM(NS_InitXPCOM2);
199 NS_DLSYM(NS_ShutdownXPCOM);
200 NS_DLSYM(NS_GetComponentRegistrar);
201 NS_DLSYM(NS_StringContainerInit);
202 NS_DLSYM(NS_CStringContainerInit);
203 NS_DLSYM(NS_StringContainerFinish);
204 NS_DLSYM(NS_CStringContainerFinish);
205 NS_DLSYM(NS_StringSetData);
206 NS_DLSYM(NS_CStringSetData);
207 NS_DLSYM(NS_NewLocalFile);
208 NS_DLSYM(NS_StringGetData);
209 NS_DLSYM(NS_CStringGetData);
211 #undef NS_DLSYM
213 return TRUE;
216 static BOOL check_version(LPCWSTR gre_path, const char *version_string)
218 WCHAR file_name[MAX_PATH];
219 char version[128];
220 DWORD read=0;
221 HANDLE hfile;
223 static const WCHAR wszVersion[] = {'\\','V','E','R','S','I','O','N',0};
225 strcpyW(file_name, gre_path);
226 strcatW(file_name, wszVersion);
228 hfile = CreateFileW(file_name, GENERIC_READ, FILE_SHARE_READ, NULL,
229 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
230 if(hfile == INVALID_HANDLE_VALUE) {
231 ERR("Could not open VERSION file\n");
232 return FALSE;
235 ReadFile(hfile, version, sizeof(version), &read, NULL);
236 version[read] = 0;
237 CloseHandle(hfile);
239 TRACE("%s\n", debugstr_a(version));
241 if(strcmp(version, version_string)) {
242 ERR("Unexpected version %s, expected %s\n", debugstr_a(version),
243 debugstr_a(version_string));
244 return FALSE;
247 return TRUE;
250 static BOOL load_wine_gecko_v(PRUnichar *gre_path, HKEY mshtml_key,
251 const char *version, const char *version_string)
253 DWORD res, type, size = MAX_PATH;
254 HKEY hkey = mshtml_key;
256 static const WCHAR wszGeckoPath[] =
257 {'G','e','c','k','o','P','a','t','h',0};
259 if(version) {
260 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML\<version> */
261 res = RegOpenKeyA(mshtml_key, version, &hkey);
262 if(res != ERROR_SUCCESS)
263 return FALSE;
266 res = RegQueryValueExW(hkey, wszGeckoPath, NULL, &type, (LPBYTE)gre_path, &size);
267 if(hkey != mshtml_key)
268 RegCloseKey(hkey);
269 if(res != ERROR_SUCCESS || type != REG_SZ)
270 return FALSE;
272 if(!check_version(gre_path, version_string))
273 return FALSE;
275 return load_xpcom(gre_path);
278 static BOOL load_wine_gecko(PRUnichar *gre_path)
280 HKEY hkey;
281 DWORD res;
282 BOOL ret;
284 static const WCHAR wszMshtmlKey[] = {
285 'S','o','f','t','w','a','r','e','\\','W','i','n','e',
286 '\\','M','S','H','T','M','L',0};
288 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
289 res = RegOpenKeyW(HKEY_CURRENT_USER, wszMshtmlKey, &hkey);
290 if(res != ERROR_SUCCESS)
291 return FALSE;
293 ret = load_wine_gecko_v(gre_path, hkey, GECKO_VERSION, GECKO_VERSION_STRING);
295 RegCloseKey(hkey);
296 return ret;
299 static void set_lang(nsIPrefBranch *pref)
301 char langs[100];
302 DWORD res, size, type;
303 HKEY hkey;
304 nsresult nsres;
306 static const WCHAR international_keyW[] =
307 {'S','o','f','t','w','a','r','e',
308 '\\','M','i','c','r','o','s','o','f','t',
309 '\\','I','n','t','e','r','n','e','t',' ','E','x','p','l','o','r','e','r',
310 '\\','I','n','t','e','r','n','a','t','i','o','n','a','l',0};
312 res = RegOpenKeyW(HKEY_CURRENT_USER, international_keyW, &hkey);
313 if(res != ERROR_SUCCESS)
314 return;
316 size = sizeof(langs);
317 res = RegQueryValueExA(hkey, "AcceptLanguage", 0, &type, (LPBYTE)langs, &size);
318 RegCloseKey(hkey);
319 if(res != ERROR_SUCCESS || type != REG_SZ)
320 return;
322 TRACE("Setting lang %s\n", debugstr_a(langs));
324 nsres = nsIPrefBranch_SetCharPref(pref, "intl.accept_languages", langs);
325 if(NS_FAILED(nsres))
326 ERR("SetCharPref failed: %08x\n", nsres);
329 static void set_proxy(nsIPrefBranch *pref)
331 char proxy[512];
332 char * proxy_port;
333 int proxy_port_num;
334 DWORD enabled = 0, res, size, type;
335 HKEY hkey;
336 nsresult nsres;
338 static const WCHAR proxy_keyW[] =
339 {'S','o','f','t','w','a','r','e',
340 '\\','M','i','c','r','o','s','o','f','t',
341 '\\','W','i','n','d','o','w','s',
342 '\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n',
343 '\\','I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s',0};
345 res = RegOpenKeyW(HKEY_CURRENT_USER, proxy_keyW, &hkey);
346 if(res != ERROR_SUCCESS)
347 return;
349 size = sizeof(enabled);
350 res = RegQueryValueExA(hkey, "ProxyEnable", 0, &type, (LPBYTE)&enabled, &size);
351 if(res != ERROR_SUCCESS || type != REG_DWORD || enabled == 0)
353 RegCloseKey(hkey);
354 return;
357 size = sizeof(proxy);
358 res = RegQueryValueExA(hkey, "ProxyServer", 0, &type, (LPBYTE)proxy, &size);
359 RegCloseKey(hkey);
360 if(res != ERROR_SUCCESS || type != REG_SZ)
361 return;
363 proxy_port = strchr(proxy, ':');
364 if (!proxy_port)
365 return;
367 *proxy_port = 0;
368 proxy_port_num = atoi(proxy_port + 1);
369 TRACE("Setting proxy to %s, port %d\n", debugstr_a(proxy), proxy_port_num);
371 nsres = nsIPrefBranch_SetIntPref(pref, "network.proxy.type", 1);
372 if(NS_FAILED(nsres))
373 ERR("SetIntPref network.proxy.type failed: %08x\n", nsres);
374 nsres = nsIPrefBranch_SetCharPref(pref, "network.proxy.http", proxy);
375 if(NS_FAILED(nsres))
376 ERR("SetCharPref network.proxy.http failed: %08x\n", nsres);
377 nsres = nsIPrefBranch_SetIntPref(pref, "network.proxy.http_port", proxy_port_num);
378 if(NS_FAILED(nsres))
379 ERR("SetIntPref network.proxy.http_port failed: %08x\n", nsres);
380 nsres = nsIPrefBranch_SetCharPref(pref, "network.proxy.ssl", proxy);
381 if(NS_FAILED(nsres))
382 ERR("SetCharPref network.proxy.ssl failed: %08x\n", nsres);
383 nsres = nsIPrefBranch_SetIntPref(pref, "network.proxy.ssl_port", proxy_port_num);
384 if(NS_FAILED(nsres))
385 ERR("SetIntPref network.proxy.ssl_port failed: %08x\n", nsres);
388 static void set_bool_pref(nsIPrefBranch *pref, const char *pref_name, BOOL val)
390 nsresult nsres;
392 nsres = nsIPrefBranch_SetBoolPref(pref, pref_name, val);
393 if(NS_FAILED(nsres))
394 ERR("Could not set pref %s\n", debugstr_a(pref_name));
397 static void set_preferences(void)
399 nsIPrefBranch *pref;
400 nsresult nsres;
402 nsres = nsIServiceManager_GetServiceByContractID(pServMgr, NS_PREFERENCES_CONTRACTID,
403 &IID_nsIPrefBranch, (void**)&pref);
404 if(NS_FAILED(nsres)) {
405 ERR("Could not get preference service: %08x\n", nsres);
406 return;
409 set_lang(pref);
410 set_proxy(pref);
411 set_bool_pref(pref, "security.warn_entering_secure", FALSE);
412 set_bool_pref(pref, "security.warn_submit_insecure", FALSE);
414 nsIPrefBranch_Release(pref);
417 static BOOL init_xpcom(const PRUnichar *gre_path)
419 nsresult nsres;
420 nsIObserver *pStartNotif;
421 nsIComponentRegistrar *registrar = NULL;
422 nsAString path;
423 nsIFile *gre_dir;
425 nsAString_Init(&path, gre_path);
426 nsres = NS_NewLocalFile(&path, FALSE, &gre_dir);
427 nsAString_Finish(&path);
428 if(NS_FAILED(nsres)) {
429 ERR("NS_NewLocalFile failed: %08x\n", nsres);
430 FreeLibrary(hXPCOM);
431 return FALSE;
434 nsres = NS_InitXPCOM2(&pServMgr, gre_dir, NULL);
435 if(NS_FAILED(nsres)) {
436 ERR("NS_InitXPCOM2 failed: %08x\n", nsres);
437 FreeLibrary(hXPCOM);
438 return FALSE;
441 nsres = nsIServiceManager_QueryInterface(pServMgr, &IID_nsIComponentManager, (void**)&pCompMgr);
442 if(NS_FAILED(nsres))
443 ERR("Could not get nsIComponentManager: %08x\n", nsres);
445 nsres = NS_GetComponentRegistrar(&registrar);
446 if(NS_SUCCEEDED(nsres)) {
447 nsres = nsIComponentRegistrar_AutoRegister(registrar, NULL);
448 if(NS_FAILED(nsres))
449 ERR("AutoRegister(NULL) failed: %08x\n", nsres);
451 init_nsio(pCompMgr, registrar);
452 }else {
453 ERR("NS_GetComponentRegistrar failed: %08x\n", nsres);
456 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_APPSTARTUPNOTIFIER_CONTRACTID,
457 NULL, &IID_nsIObserver, (void**)&pStartNotif);
458 if(NS_SUCCEEDED(nsres)) {
459 nsres = nsIObserver_Observe(pStartNotif, NULL, APPSTARTUP_TOPIC, NULL);
460 if(NS_FAILED(nsres))
461 ERR("Observe failed: %08x\n", nsres);
463 nsIObserver_Release(pStartNotif);
464 }else {
465 ERR("could not get appstartup-notifier: %08x\n", nsres);
468 set_preferences();
470 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_MEMORY_CONTRACTID,
471 NULL, &IID_nsIMemory, (void**)&nsmem);
472 if(NS_FAILED(nsres))
473 ERR("Could not get nsIMemory: %08x\n", nsres);
475 if(registrar) {
476 register_nsservice(registrar, pServMgr);
477 nsIComponentRegistrar_Release(registrar);
480 return TRUE;
483 static CRITICAL_SECTION cs_load_gecko;
484 static CRITICAL_SECTION_DEBUG cs_load_gecko_dbg =
486 0, 0, &cs_load_gecko,
487 { &cs_load_gecko_dbg.ProcessLocksList, &cs_load_gecko_dbg.ProcessLocksList },
488 0, 0, { (DWORD_PTR)(__FILE__ ": load_gecko") }
490 static CRITICAL_SECTION cs_load_gecko = { &cs_load_gecko_dbg, -1, 0, 0, 0, 0 };
492 BOOL load_gecko(BOOL silent)
494 PRUnichar gre_path[MAX_PATH];
495 BOOL ret = FALSE;
497 static DWORD loading_thread;
499 TRACE("()\n");
501 /* load_gecko may be called recursively */
502 if(loading_thread == GetCurrentThreadId())
503 return pCompMgr != NULL;
505 EnterCriticalSection(&cs_load_gecko);
507 if(!loading_thread) {
508 loading_thread = GetCurrentThreadId();
510 if(load_wine_gecko(gre_path)
511 || (install_wine_gecko(silent) && load_wine_gecko(gre_path)))
512 ret = init_xpcom(gre_path);
513 else
514 MESSAGE("Could not load wine-gecko. HTML rendering will be disabled.\n");
515 }else {
516 ret = pCompMgr != NULL;
519 LeaveCriticalSection(&cs_load_gecko);
521 return ret;
524 void *nsalloc(size_t size)
526 return nsIMemory_Alloc(nsmem, size);
529 void nsfree(void *mem)
531 nsIMemory_Free(nsmem, mem);
534 void nsACString_Init(nsACString *str, const char *data)
536 NS_CStringContainerInit(str);
537 if(data)
538 nsACString_SetData(str, data);
541 void nsACString_SetData(nsACString *str, const char *data)
543 NS_CStringSetData(str, data, PR_UINT32_MAX);
546 PRUint32 nsACString_GetData(const nsACString *str, const char **data)
548 return NS_CStringGetData(str, data, NULL);
551 void nsACString_Finish(nsACString *str)
553 NS_CStringContainerFinish(str);
556 void nsAString_Init(nsAString *str, const PRUnichar *data)
558 NS_StringContainerInit(str);
559 if(data)
560 nsAString_SetData(str, data);
563 void nsAString_SetData(nsAString *str, const PRUnichar *data)
565 NS_StringSetData(str, data, PR_UINT32_MAX);
568 PRUint32 nsAString_GetData(const nsAString *str, const PRUnichar **data)
570 return NS_StringGetData(str, data, NULL);
573 void nsAString_Finish(nsAString *str)
575 NS_StringContainerFinish(str);
578 nsIInputStream *create_nsstream(const char *data, PRInt32 data_len)
580 nsIStringInputStream *ret;
581 nsresult nsres;
583 if(!pCompMgr)
584 return NULL;
586 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
587 NS_STRINGSTREAM_CONTRACTID, NULL, &IID_nsIStringInputStream,
588 (void**)&ret);
589 if(NS_FAILED(nsres)) {
590 ERR("Could not get nsIStringInputStream\n");
591 return NULL;
594 nsres = nsIStringInputStream_SetData(ret, data, data_len);
595 if(NS_FAILED(nsres)) {
596 ERR("AdoptData failed: %08x\n", nsres);
597 nsIStringInputStream_Release(ret);
598 return NULL;
601 return (nsIInputStream*)ret;
604 nsICommandParams *create_nscommand_params(void)
606 nsICommandParams *ret = NULL;
607 nsresult nsres;
609 if(!pCompMgr)
610 return NULL;
612 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
613 NS_COMMANDPARAMS_CONTRACTID, NULL, &IID_nsICommandParams,
614 (void**)&ret);
615 if(NS_FAILED(nsres))
616 ERR("Could not get nsICommandParams\n");
618 return ret;
621 nsresult get_nsinterface(nsISupports *iface, REFIID riid, void **ppv)
623 nsIInterfaceRequestor *iface_req;
624 nsresult nsres;
626 nsres = nsISupports_QueryInterface(iface, &IID_nsIInterfaceRequestor, (void**)&iface_req);
627 if(NS_FAILED(nsres))
628 return nsres;
630 nsres = nsIInterfaceRequestor_GetInterface(iface_req, riid, ppv);
631 nsIInterfaceRequestor_Release(iface_req);
633 return nsres;
636 static void nsnode_to_nsstring_rec(nsIContentSerializer *serializer, nsIDOMNode *nsnode, nsAString *str)
638 nsIDOMNodeList *node_list = NULL;
639 PRBool has_children = FALSE;
640 PRUint16 type;
641 nsresult nsres;
643 nsIDOMNode_HasChildNodes(nsnode, &has_children);
645 nsres = nsIDOMNode_GetNodeType(nsnode, &type);
646 if(NS_FAILED(nsres)) {
647 ERR("GetType failed: %08x\n", nsres);
648 return;
651 switch(type) {
652 case ELEMENT_NODE: {
653 nsIDOMElement *nselem;
654 nsIDOMNode_QueryInterface(nsnode, &IID_nsIDOMElement, (void**)&nselem);
655 nsIContentSerializer_AppendElementStart(serializer, nselem, nselem, str);
656 nsIDOMElement_Release(nselem);
657 break;
659 case TEXT_NODE: {
660 nsIDOMText *nstext;
661 nsIDOMNode_QueryInterface(nsnode, &IID_nsIDOMText, (void**)&nstext);
662 nsIContentSerializer_AppendText(serializer, nstext, 0, -1, str);
663 nsIDOMText_Release(nstext);
664 break;
666 case COMMENT_NODE: {
667 nsIDOMComment *nscomment;
668 nsres = nsIDOMNode_QueryInterface(nsnode, &IID_nsIDOMComment, (void**)&nscomment);
669 nsres = nsIContentSerializer_AppendComment(serializer, nscomment, 0, -1, str);
670 break;
672 case DOCUMENT_NODE: {
673 nsIDOMDocument *nsdoc;
674 nsIDOMNode_QueryInterface(nsnode, &IID_nsIDOMDocument, (void**)&nsdoc);
675 nsIContentSerializer_AppendDocumentStart(serializer, nsdoc, str);
676 nsIDOMDocument_Release(nsdoc);
677 break;
679 case DOCUMENT_FRAGMENT_NODE:
680 break;
681 default:
682 FIXME("Unhandled type %u\n", type);
685 if(has_children) {
686 PRUint32 child_cnt, i;
687 nsIDOMNode *child_node;
689 nsIDOMNode_GetChildNodes(nsnode, &node_list);
690 nsIDOMNodeList_GetLength(node_list, &child_cnt);
692 for(i=0; i<child_cnt; i++) {
693 nsres = nsIDOMNodeList_Item(node_list, i, &child_node);
694 if(NS_SUCCEEDED(nsres)) {
695 nsnode_to_nsstring_rec(serializer, child_node, str);
696 nsIDOMNode_Release(child_node);
697 }else {
698 ERR("Item failed: %08x\n", nsres);
702 nsIDOMNodeList_Release(node_list);
705 if(type == ELEMENT_NODE) {
706 nsIDOMElement *nselem;
707 nsIDOMNode_QueryInterface(nsnode, &IID_nsIDOMElement, (void**)&nselem);
708 nsIContentSerializer_AppendElementEnd(serializer, nselem, str);
709 nsIDOMElement_Release(nselem);
713 void nsnode_to_nsstring(nsIDOMNode *nsdoc, nsAString *str)
715 nsIContentSerializer *serializer;
716 nsIDOMNode *nsnode;
717 nsresult nsres;
719 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
720 NS_HTMLSERIALIZER_CONTRACTID, NULL, &IID_nsIContentSerializer,
721 (void**)&serializer);
722 if(NS_FAILED(nsres)) {
723 ERR("Could not get nsIContentSerializer: %08x\n", nsres);
724 return;
727 nsres = nsIContentSerializer_Init(serializer, 0, 100, NULL, FALSE, FALSE /* FIXME */);
728 if(NS_FAILED(nsres))
729 ERR("Init failed: %08x\n", nsres);
731 nsIDOMDocument_QueryInterface(nsdoc, &IID_nsIDOMNode, (void**)&nsnode);
732 nsnode_to_nsstring_rec(serializer, nsnode, str);
733 nsIDOMNode_Release(nsnode);
735 nsres = nsIContentSerializer_Flush(serializer, str);
736 if(NS_FAILED(nsres))
737 ERR("Flush failed: %08x\n", nsres);
739 nsIContentSerializer_Release(serializer);
742 void get_editor_controller(NSContainer *This)
744 nsIEditingSession *editing_session = NULL;
745 nsIControllerContext *ctrlctx;
746 nsresult nsres;
748 if(This->editor) {
749 nsIEditor_Release(This->editor);
750 This->editor = NULL;
753 if(This->editor_controller) {
754 nsIController_Release(This->editor_controller);
755 This->editor_controller = NULL;
758 nsres = get_nsinterface((nsISupports*)This->webbrowser, &IID_nsIEditingSession,
759 (void**)&editing_session);
760 if(NS_FAILED(nsres)) {
761 ERR("Could not get nsIEditingSession: %08x\n", nsres);
762 return;
765 nsres = nsIEditingSession_GetEditorForWindow(editing_session,
766 This->doc->window->nswindow, &This->editor);
767 nsIEditingSession_Release(editing_session);
768 if(NS_FAILED(nsres)) {
769 ERR("Could not get editor: %08x\n", nsres);
770 return;
773 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
774 NS_EDITORCONTROLLER_CONTRACTID, NULL, &IID_nsIControllerContext, (void**)&ctrlctx);
775 if(NS_SUCCEEDED(nsres)) {
776 nsres = nsIControllerContext_SetCommandContext(ctrlctx, (nsISupports *)This->editor);
777 if(NS_FAILED(nsres))
778 ERR("SetCommandContext failed: %08x\n", nsres);
779 nsres = nsIControllerContext_QueryInterface(ctrlctx, &IID_nsIController,
780 (void**)&This->editor_controller);
781 nsIControllerContext_Release(ctrlctx);
782 if(NS_FAILED(nsres))
783 ERR("Could not get nsIController interface: %08x\n", nsres);
784 }else {
785 ERR("Could not create edit controller: %08x\n", nsres);
789 void set_ns_editmode(NSContainer *This)
791 nsIEditingSession *editing_session = NULL;
792 nsIURIContentListener *listener = NULL;
793 nsIDOMWindow *dom_window = NULL;
794 nsresult nsres;
796 nsres = get_nsinterface((nsISupports*)This->webbrowser, &IID_nsIEditingSession,
797 (void**)&editing_session);
798 if(NS_FAILED(nsres)) {
799 ERR("Could not get nsIEditingSession: %08x\n", nsres);
800 return;
803 nsres = nsIWebBrowser_GetContentDOMWindow(This->webbrowser, &dom_window);
804 if(NS_FAILED(nsres)) {
805 ERR("Could not get content DOM window: %08x\n", nsres);
806 nsIEditingSession_Release(editing_session);
807 return;
810 nsres = nsIEditingSession_MakeWindowEditable(editing_session, dom_window,
811 NULL, FALSE, TRUE, TRUE);
812 nsIEditingSession_Release(editing_session);
813 nsIDOMWindow_Release(dom_window);
814 if(NS_FAILED(nsres)) {
815 ERR("MakeWindowEditable failed: %08x\n", nsres);
816 return;
819 /* MakeWindowEditable changes WebBrowser's parent URI content listener.
820 * It seams to be a bug in Gecko. To workaround it we set our content
821 * listener again and Gecko's one as its parent.
823 nsIWebBrowser_GetParentURIContentListener(This->webbrowser, &listener);
824 nsIURIContentListener_SetParentContentListener(NSURICL(This), listener);
825 nsIURIContentListener_Release(listener);
826 nsIWebBrowser_SetParentURIContentListener(This->webbrowser, NSURICL(This));
829 void update_nsdocument(HTMLDocument *doc)
831 nsIDOMHTMLDocument *nsdoc;
832 nsIDOMDocument *nsdomdoc;
833 nsresult nsres;
835 if(!doc->nscontainer || !doc->nscontainer->navigation)
836 return;
838 nsres = nsIWebNavigation_GetDocument(doc->nscontainer->navigation, &nsdomdoc);
839 if(NS_FAILED(nsres) || !nsdomdoc) {
840 ERR("GetDocument failed: %08x\n", nsres);
841 return;
844 nsres = nsIDOMDocument_QueryInterface(nsdomdoc, &IID_nsIDOMHTMLDocument, (void**)&nsdoc);
845 nsIDOMDocument_Release(nsdomdoc);
846 if(NS_FAILED(nsres)) {
847 ERR("Could not get nsIDOMHTMLDocument iface: %08x\n", nsres);
848 return;
851 if(nsdoc == doc->nsdoc) {
852 nsIDOMHTMLDocument_Release(nsdoc);
853 return;
856 if(doc->nsdoc) {
857 remove_mutation_observer(doc->nscontainer, doc->nsdoc);
858 nsIDOMHTMLDocument_Release(doc->nsdoc);
861 doc->nsdoc = nsdoc;
863 if(nsdoc)
864 set_mutation_observer(doc->nscontainer, nsdoc);
867 void close_gecko(void)
869 TRACE("()\n");
871 release_nsio();
873 if(pCompMgr)
874 nsIComponentManager_Release(pCompMgr);
876 if(pServMgr)
877 nsIServiceManager_Release(pServMgr);
879 if(nsmem)
880 nsIMemory_Release(nsmem);
882 /* Gecko doesn't really support being unloaded */
883 /* if (hXPCOM) FreeLibrary(hXPCOM); */
886 /**********************************************************
887 * nsIWebBrowserChrome interface
890 #define NSWBCHROME_THIS(iface) DEFINE_THIS(NSContainer, WebBrowserChrome, iface)
892 static nsresult NSAPI nsWebBrowserChrome_QueryInterface(nsIWebBrowserChrome *iface,
893 nsIIDRef riid, nsQIResult result)
895 NSContainer *This = NSWBCHROME_THIS(iface);
897 *result = NULL;
898 if(IsEqualGUID(&IID_nsISupports, riid)) {
899 TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
900 *result = NSWBCHROME(This);
901 }else if(IsEqualGUID(&IID_nsIWebBrowserChrome, riid)) {
902 TRACE("(%p)->(IID_nsIWebBrowserChrome, %p)\n", This, result);
903 *result = NSWBCHROME(This);
904 }else if(IsEqualGUID(&IID_nsIContextMenuListener, riid)) {
905 TRACE("(%p)->(IID_nsIContextMenuListener, %p)\n", This, result);
906 *result = NSCML(This);
907 }else if(IsEqualGUID(&IID_nsIURIContentListener, riid)) {
908 TRACE("(%p)->(IID_nsIURIContentListener %p)\n", This, result);
909 *result = NSURICL(This);
910 }else if(IsEqualGUID(&IID_nsIEmbeddingSiteWindow, riid)) {
911 TRACE("(%p)->(IID_nsIEmbeddingSiteWindow %p)\n", This, result);
912 *result = NSEMBWNDS(This);
913 }else if(IsEqualGUID(&IID_nsITooltipListener, riid)) {
914 TRACE("(%p)->(IID_nsITooltipListener %p)\n", This, result);
915 *result = NSTOOLTIP(This);
916 }else if(IsEqualGUID(&IID_nsIInterfaceRequestor, riid)) {
917 TRACE("(%p)->(IID_nsIInterfaceRequestor %p)\n", This, result);
918 *result = NSIFACEREQ(This);
919 }else if(IsEqualGUID(&IID_nsIWeakReference, riid)) {
920 TRACE("(%p)->(IID_nsIWeakReference %p)\n", This, result);
921 *result = NSWEAKREF(This);
922 }else if(IsEqualGUID(&IID_nsISupportsWeakReference, riid)) {
923 TRACE("(%p)->(IID_nsISupportsWeakReference %p)\n", This, result);
924 *result = NSSUPWEAKREF(This);
927 if(*result) {
928 nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
929 return NS_OK;
932 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
933 return NS_NOINTERFACE;
936 static nsrefcnt NSAPI nsWebBrowserChrome_AddRef(nsIWebBrowserChrome *iface)
938 NSContainer *This = NSWBCHROME_THIS(iface);
939 LONG ref = InterlockedIncrement(&This->ref);
941 TRACE("(%p) ref=%d\n", This, ref);
943 return ref;
946 static nsrefcnt NSAPI nsWebBrowserChrome_Release(nsIWebBrowserChrome *iface)
948 NSContainer *This = NSWBCHROME_THIS(iface);
949 LONG ref = InterlockedDecrement(&This->ref);
951 TRACE("(%p) ref=%d\n", This, ref);
953 if(!ref) {
954 heap_free(This->event_vector);
955 if(This->parent)
956 nsIWebBrowserChrome_Release(NSWBCHROME(This->parent));
957 heap_free(This);
960 return ref;
963 static nsresult NSAPI nsWebBrowserChrome_SetStatus(nsIWebBrowserChrome *iface,
964 PRUint32 statusType, const PRUnichar *status)
966 NSContainer *This = NSWBCHROME_THIS(iface);
968 TRACE("(%p)->(%d %s)\n", This, statusType, debugstr_w(status));
970 /* FIXME: This hack should be removed when we'll load all pages by URLMoniker */
971 if(This->doc)
972 update_nsdocument(This->doc);
974 return NS_OK;
977 static nsresult NSAPI nsWebBrowserChrome_GetWebBrowser(nsIWebBrowserChrome *iface,
978 nsIWebBrowser **aWebBrowser)
980 NSContainer *This = NSWBCHROME_THIS(iface);
982 TRACE("(%p)->(%p)\n", This, aWebBrowser);
984 if(!aWebBrowser)
985 return NS_ERROR_INVALID_ARG;
987 if(This->webbrowser)
988 nsIWebBrowser_AddRef(This->webbrowser);
989 *aWebBrowser = This->webbrowser;
990 return S_OK;
993 static nsresult NSAPI nsWebBrowserChrome_SetWebBrowser(nsIWebBrowserChrome *iface,
994 nsIWebBrowser *aWebBrowser)
996 NSContainer *This = NSWBCHROME_THIS(iface);
998 TRACE("(%p)->(%p)\n", This, aWebBrowser);
1000 if(aWebBrowser != This->webbrowser)
1001 ERR("Wrong nsWebBrowser!\n");
1003 return NS_OK;
1006 static nsresult NSAPI nsWebBrowserChrome_GetChromeFlags(nsIWebBrowserChrome *iface,
1007 PRUint32 *aChromeFlags)
1009 NSContainer *This = NSWBCHROME_THIS(iface);
1010 WARN("(%p)->(%p)\n", This, aChromeFlags);
1011 return NS_ERROR_NOT_IMPLEMENTED;
1014 static nsresult NSAPI nsWebBrowserChrome_SetChromeFlags(nsIWebBrowserChrome *iface,
1015 PRUint32 aChromeFlags)
1017 NSContainer *This = NSWBCHROME_THIS(iface);
1018 WARN("(%p)->(%08x)\n", This, aChromeFlags);
1019 return NS_ERROR_NOT_IMPLEMENTED;
1022 static nsresult NSAPI nsWebBrowserChrome_DestroyBrowserWindow(nsIWebBrowserChrome *iface)
1024 NSContainer *This = NSWBCHROME_THIS(iface);
1025 TRACE("(%p)\n", This);
1026 return NS_ERROR_NOT_IMPLEMENTED;
1029 static nsresult NSAPI nsWebBrowserChrome_SizeBrowserTo(nsIWebBrowserChrome *iface,
1030 PRInt32 aCX, PRInt32 aCY)
1032 NSContainer *This = NSWBCHROME_THIS(iface);
1033 WARN("(%p)->(%d %d)\n", This, aCX, aCY);
1034 return NS_ERROR_NOT_IMPLEMENTED;
1037 static nsresult NSAPI nsWebBrowserChrome_ShowAsModal(nsIWebBrowserChrome *iface)
1039 NSContainer *This = NSWBCHROME_THIS(iface);
1040 WARN("(%p)\n", This);
1041 return NS_ERROR_NOT_IMPLEMENTED;
1044 static nsresult NSAPI nsWebBrowserChrome_IsWindowModal(nsIWebBrowserChrome *iface, PRBool *_retval)
1046 NSContainer *This = NSWBCHROME_THIS(iface);
1047 WARN("(%p)->(%p)\n", This, _retval);
1048 return NS_ERROR_NOT_IMPLEMENTED;
1051 static nsresult NSAPI nsWebBrowserChrome_ExitModalEventLoop(nsIWebBrowserChrome *iface,
1052 nsresult aStatus)
1054 NSContainer *This = NSWBCHROME_THIS(iface);
1055 WARN("(%p)->(%08x)\n", This, aStatus);
1056 return NS_ERROR_NOT_IMPLEMENTED;
1059 #undef NSWBCHROME_THIS
1061 static const nsIWebBrowserChromeVtbl nsWebBrowserChromeVtbl = {
1062 nsWebBrowserChrome_QueryInterface,
1063 nsWebBrowserChrome_AddRef,
1064 nsWebBrowserChrome_Release,
1065 nsWebBrowserChrome_SetStatus,
1066 nsWebBrowserChrome_GetWebBrowser,
1067 nsWebBrowserChrome_SetWebBrowser,
1068 nsWebBrowserChrome_GetChromeFlags,
1069 nsWebBrowserChrome_SetChromeFlags,
1070 nsWebBrowserChrome_DestroyBrowserWindow,
1071 nsWebBrowserChrome_SizeBrowserTo,
1072 nsWebBrowserChrome_ShowAsModal,
1073 nsWebBrowserChrome_IsWindowModal,
1074 nsWebBrowserChrome_ExitModalEventLoop
1077 /**********************************************************
1078 * nsIContextMenuListener interface
1081 #define NSCML_THIS(iface) DEFINE_THIS(NSContainer, ContextMenuListener, iface)
1083 static nsresult NSAPI nsContextMenuListener_QueryInterface(nsIContextMenuListener *iface,
1084 nsIIDRef riid, nsQIResult result)
1086 NSContainer *This = NSCML_THIS(iface);
1087 return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1090 static nsrefcnt NSAPI nsContextMenuListener_AddRef(nsIContextMenuListener *iface)
1092 NSContainer *This = NSCML_THIS(iface);
1093 return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1096 static nsrefcnt NSAPI nsContextMenuListener_Release(nsIContextMenuListener *iface)
1098 NSContainer *This = NSCML_THIS(iface);
1099 return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1102 static nsresult NSAPI nsContextMenuListener_OnShowContextMenu(nsIContextMenuListener *iface,
1103 PRUint32 aContextFlags, nsIDOMEvent *aEvent, nsIDOMNode *aNode)
1105 NSContainer *This = NSCML_THIS(iface);
1106 nsIDOMMouseEvent *event;
1107 POINT pt;
1108 DWORD dwID = CONTEXT_MENU_DEFAULT;
1109 nsresult nsres;
1111 TRACE("(%p)->(%08x %p %p)\n", This, aContextFlags, aEvent, aNode);
1113 nsres = nsIDOMEvent_QueryInterface(aEvent, &IID_nsIDOMMouseEvent, (void**)&event);
1114 if(NS_FAILED(nsres)) {
1115 ERR("Could not get nsIDOMMouseEvent interface: %08x\n", nsres);
1116 return nsres;
1119 nsIDOMMouseEvent_GetScreenX(event, &pt.x);
1120 nsIDOMMouseEvent_GetScreenY(event, &pt.y);
1121 nsIDOMMouseEvent_Release(event);
1123 switch(aContextFlags) {
1124 case CONTEXT_NONE:
1125 case CONTEXT_DOCUMENT:
1126 case CONTEXT_TEXT:
1127 dwID = CONTEXT_MENU_DEFAULT;
1128 break;
1129 case CONTEXT_IMAGE:
1130 case CONTEXT_IMAGE|CONTEXT_LINK:
1131 dwID = CONTEXT_MENU_IMAGE;
1132 break;
1133 case CONTEXT_LINK:
1134 dwID = CONTEXT_MENU_ANCHOR;
1135 break;
1136 case CONTEXT_INPUT:
1137 dwID = CONTEXT_MENU_CONTROL;
1138 break;
1139 default:
1140 FIXME("aContextFlags=%08x\n", aContextFlags);
1143 show_context_menu(This->doc, dwID, &pt, (IDispatch*)HTMLDOMNODE(get_node(This->doc, aNode, TRUE)));
1145 return NS_OK;
1148 #undef NSCML_THIS
1150 static const nsIContextMenuListenerVtbl nsContextMenuListenerVtbl = {
1151 nsContextMenuListener_QueryInterface,
1152 nsContextMenuListener_AddRef,
1153 nsContextMenuListener_Release,
1154 nsContextMenuListener_OnShowContextMenu
1157 /**********************************************************
1158 * nsIURIContentListener interface
1161 #define NSURICL_THIS(iface) DEFINE_THIS(NSContainer, URIContentListener, iface)
1163 static nsresult NSAPI nsURIContentListener_QueryInterface(nsIURIContentListener *iface,
1164 nsIIDRef riid, nsQIResult result)
1166 NSContainer *This = NSURICL_THIS(iface);
1167 return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1170 static nsrefcnt NSAPI nsURIContentListener_AddRef(nsIURIContentListener *iface)
1172 NSContainer *This = NSURICL_THIS(iface);
1173 return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1176 static nsrefcnt NSAPI nsURIContentListener_Release(nsIURIContentListener *iface)
1178 NSContainer *This = NSURICL_THIS(iface);
1179 return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1182 static nsresult NSAPI nsURIContentListener_OnStartURIOpen(nsIURIContentListener *iface,
1183 nsIURI *aURI, PRBool *_retval)
1185 NSContainer *This = NSURICL_THIS(iface);
1186 nsIWineURI *wine_uri;
1187 nsACString spec_str;
1188 const char *spec;
1189 nsresult nsres;
1191 nsACString_Init(&spec_str, NULL);
1192 nsIURI_GetSpec(aURI, &spec_str);
1193 nsACString_GetData(&spec_str, &spec);
1195 TRACE("(%p)->(%p(%s) %p)\n", This, aURI, debugstr_a(spec), _retval);
1197 nsACString_Finish(&spec_str);
1199 nsres = nsIURI_QueryInterface(aURI, &IID_nsIWineURI, (void**)&wine_uri);
1200 if(NS_FAILED(nsres)) {
1201 WARN("Could not get nsIWineURI interface: %08x\n", nsres);
1202 return NS_ERROR_NOT_IMPLEMENTED;
1205 nsIWineURI_SetNSContainer(wine_uri, This);
1206 nsIWineURI_SetIsDocumentURI(wine_uri, TRUE);
1208 if(This->bscallback) {
1209 IMoniker *mon = get_channelbsc_mon(This->bscallback);
1211 if(mon) {
1212 LPWSTR wine_url;
1213 HRESULT hres;
1215 hres = IMoniker_GetDisplayName(mon, NULL, 0, &wine_url);
1216 if(SUCCEEDED(hres)) {
1217 nsIWineURI_SetWineURL(wine_uri, wine_url);
1218 CoTaskMemFree(wine_url);
1219 }else {
1220 WARN("GetDisplayName failed: %08x\n", hres);
1223 IMoniker_Release(mon);
1227 nsIWineURI_Release(wine_uri);
1229 *_retval = FALSE;
1230 return This->content_listener
1231 ? nsIURIContentListener_OnStartURIOpen(This->content_listener, aURI, _retval)
1232 : NS_OK;
1235 static nsresult NSAPI nsURIContentListener_DoContent(nsIURIContentListener *iface,
1236 const char *aContentType, PRBool aIsContentPreferred, nsIRequest *aRequest,
1237 nsIStreamListener **aContentHandler, PRBool *_retval)
1239 NSContainer *This = NSURICL_THIS(iface);
1241 TRACE("(%p)->(%s %x %p %p %p)\n", This, debugstr_a(aContentType), aIsContentPreferred,
1242 aRequest, aContentHandler, _retval);
1244 return This->content_listener
1245 ? nsIURIContentListener_DoContent(This->content_listener, aContentType,
1246 aIsContentPreferred, aRequest, aContentHandler, _retval)
1247 : NS_ERROR_NOT_IMPLEMENTED;
1250 static nsresult NSAPI nsURIContentListener_IsPreferred(nsIURIContentListener *iface,
1251 const char *aContentType, char **aDesiredContentType, PRBool *_retval)
1253 NSContainer *This = NSURICL_THIS(iface);
1255 TRACE("(%p)->(%s %p %p)\n", This, debugstr_a(aContentType), aDesiredContentType, _retval);
1257 /* FIXME: Should we do something here? */
1258 *_retval = TRUE;
1260 return This->content_listener
1261 ? nsIURIContentListener_IsPreferred(This->content_listener, aContentType,
1262 aDesiredContentType, _retval)
1263 : NS_OK;
1266 static nsresult NSAPI nsURIContentListener_CanHandleContent(nsIURIContentListener *iface,
1267 const char *aContentType, PRBool aIsContentPreferred, char **aDesiredContentType,
1268 PRBool *_retval)
1270 NSContainer *This = NSURICL_THIS(iface);
1272 TRACE("(%p)->(%s %x %p %p)\n", This, debugstr_a(aContentType), aIsContentPreferred,
1273 aDesiredContentType, _retval);
1275 return This->content_listener
1276 ? nsIURIContentListener_CanHandleContent(This->content_listener, aContentType,
1277 aIsContentPreferred, aDesiredContentType, _retval)
1278 : NS_ERROR_NOT_IMPLEMENTED;
1281 static nsresult NSAPI nsURIContentListener_GetLoadCookie(nsIURIContentListener *iface,
1282 nsISupports **aLoadCookie)
1284 NSContainer *This = NSURICL_THIS(iface);
1286 WARN("(%p)->(%p)\n", This, aLoadCookie);
1288 return This->content_listener
1289 ? nsIURIContentListener_GetLoadCookie(This->content_listener, aLoadCookie)
1290 : NS_ERROR_NOT_IMPLEMENTED;
1293 static nsresult NSAPI nsURIContentListener_SetLoadCookie(nsIURIContentListener *iface,
1294 nsISupports *aLoadCookie)
1296 NSContainer *This = NSURICL_THIS(iface);
1298 WARN("(%p)->(%p)\n", This, aLoadCookie);
1300 return This->content_listener
1301 ? nsIURIContentListener_SetLoadCookie(This->content_listener, aLoadCookie)
1302 : NS_ERROR_NOT_IMPLEMENTED;
1305 static nsresult NSAPI nsURIContentListener_GetParentContentListener(nsIURIContentListener *iface,
1306 nsIURIContentListener **aParentContentListener)
1308 NSContainer *This = NSURICL_THIS(iface);
1310 TRACE("(%p)->(%p)\n", This, aParentContentListener);
1312 if(This->content_listener)
1313 nsIURIContentListener_AddRef(This->content_listener);
1315 *aParentContentListener = This->content_listener;
1316 return NS_OK;
1319 static nsresult NSAPI nsURIContentListener_SetParentContentListener(nsIURIContentListener *iface,
1320 nsIURIContentListener *aParentContentListener)
1322 NSContainer *This = NSURICL_THIS(iface);
1324 TRACE("(%p)->(%p)\n", This, aParentContentListener);
1326 if(aParentContentListener == NSURICL(This))
1327 return NS_OK;
1329 if(This->content_listener)
1330 nsIURIContentListener_Release(This->content_listener);
1332 This->content_listener = aParentContentListener;
1333 if(This->content_listener)
1334 nsIURIContentListener_AddRef(This->content_listener);
1336 return NS_OK;
1339 #undef NSURICL_THIS
1341 static const nsIURIContentListenerVtbl nsURIContentListenerVtbl = {
1342 nsURIContentListener_QueryInterface,
1343 nsURIContentListener_AddRef,
1344 nsURIContentListener_Release,
1345 nsURIContentListener_OnStartURIOpen,
1346 nsURIContentListener_DoContent,
1347 nsURIContentListener_IsPreferred,
1348 nsURIContentListener_CanHandleContent,
1349 nsURIContentListener_GetLoadCookie,
1350 nsURIContentListener_SetLoadCookie,
1351 nsURIContentListener_GetParentContentListener,
1352 nsURIContentListener_SetParentContentListener
1355 /**********************************************************
1356 * nsIEmbeddinSiteWindow interface
1359 #define NSEMBWNDS_THIS(iface) DEFINE_THIS(NSContainer, EmbeddingSiteWindow, iface)
1361 static nsresult NSAPI nsEmbeddingSiteWindow_QueryInterface(nsIEmbeddingSiteWindow *iface,
1362 nsIIDRef riid, nsQIResult result)
1364 NSContainer *This = NSEMBWNDS_THIS(iface);
1365 return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1368 static nsrefcnt NSAPI nsEmbeddingSiteWindow_AddRef(nsIEmbeddingSiteWindow *iface)
1370 NSContainer *This = NSEMBWNDS_THIS(iface);
1371 return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1374 static nsrefcnt NSAPI nsEmbeddingSiteWindow_Release(nsIEmbeddingSiteWindow *iface)
1376 NSContainer *This = NSEMBWNDS_THIS(iface);
1377 return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1380 static nsresult NSAPI nsEmbeddingSiteWindow_SetDimensions(nsIEmbeddingSiteWindow *iface,
1381 PRUint32 flags, PRInt32 x, PRInt32 y, PRInt32 cx, PRInt32 cy)
1383 NSContainer *This = NSEMBWNDS_THIS(iface);
1384 WARN("(%p)->(%08x %d %d %d %d)\n", This, flags, x, y, cx, cy);
1385 return NS_ERROR_NOT_IMPLEMENTED;
1388 static nsresult NSAPI nsEmbeddingSiteWindow_GetDimensions(nsIEmbeddingSiteWindow *iface,
1389 PRUint32 flags, PRInt32 *x, PRInt32 *y, PRInt32 *cx, PRInt32 *cy)
1391 NSContainer *This = NSEMBWNDS_THIS(iface);
1392 WARN("(%p)->(%08x %p %p %p %p)\n", This, flags, x, y, cx, cy);
1393 return NS_ERROR_NOT_IMPLEMENTED;
1396 static nsresult NSAPI nsEmbeddingSiteWindow_SetFocus(nsIEmbeddingSiteWindow *iface)
1398 NSContainer *This = NSEMBWNDS_THIS(iface);
1400 TRACE("(%p)\n", This);
1402 if(This->reset_focus)
1403 PostMessageW(This->hwnd, WM_RESETFOCUS_HACK, 0, 0);
1405 return nsIBaseWindow_SetFocus(This->window);
1408 static nsresult NSAPI nsEmbeddingSiteWindow_GetVisibility(nsIEmbeddingSiteWindow *iface,
1409 PRBool *aVisibility)
1411 NSContainer *This = NSEMBWNDS_THIS(iface);
1413 TRACE("(%p)->(%p)\n", This, aVisibility);
1415 *aVisibility = This->doc && This->doc->hwnd && IsWindowVisible(This->doc->hwnd);
1416 return NS_OK;
1419 static nsresult NSAPI nsEmbeddingSiteWindow_SetVisibility(nsIEmbeddingSiteWindow *iface,
1420 PRBool aVisibility)
1422 NSContainer *This = NSEMBWNDS_THIS(iface);
1424 TRACE("(%p)->(%x)\n", This, aVisibility);
1426 return NS_OK;
1429 static nsresult NSAPI nsEmbeddingSiteWindow_GetTitle(nsIEmbeddingSiteWindow *iface,
1430 PRUnichar **aTitle)
1432 NSContainer *This = NSEMBWNDS_THIS(iface);
1433 WARN("(%p)->(%p)\n", This, aTitle);
1434 return NS_ERROR_NOT_IMPLEMENTED;
1437 static nsresult NSAPI nsEmbeddingSiteWindow_SetTitle(nsIEmbeddingSiteWindow *iface,
1438 const PRUnichar *aTitle)
1440 NSContainer *This = NSEMBWNDS_THIS(iface);
1441 WARN("(%p)->(%s)\n", This, debugstr_w(aTitle));
1442 return NS_ERROR_NOT_IMPLEMENTED;
1445 static nsresult NSAPI nsEmbeddingSiteWindow_GetSiteWindow(nsIEmbeddingSiteWindow *iface,
1446 void **aSiteWindow)
1448 NSContainer *This = NSEMBWNDS_THIS(iface);
1450 TRACE("(%p)->(%p)\n", This, aSiteWindow);
1452 *aSiteWindow = This->hwnd;
1453 return NS_OK;
1456 static const nsIEmbeddingSiteWindowVtbl nsEmbeddingSiteWindowVtbl = {
1457 nsEmbeddingSiteWindow_QueryInterface,
1458 nsEmbeddingSiteWindow_AddRef,
1459 nsEmbeddingSiteWindow_Release,
1460 nsEmbeddingSiteWindow_SetDimensions,
1461 nsEmbeddingSiteWindow_GetDimensions,
1462 nsEmbeddingSiteWindow_SetFocus,
1463 nsEmbeddingSiteWindow_GetVisibility,
1464 nsEmbeddingSiteWindow_SetVisibility,
1465 nsEmbeddingSiteWindow_GetTitle,
1466 nsEmbeddingSiteWindow_SetTitle,
1467 nsEmbeddingSiteWindow_GetSiteWindow
1470 #define NSTOOLTIP_THIS(iface) DEFINE_THIS(NSContainer, TooltipListener, iface)
1472 static nsresult NSAPI nsTooltipListener_QueryInterface(nsITooltipListener *iface, nsIIDRef riid,
1473 nsQIResult result)
1475 NSContainer *This = NSTOOLTIP_THIS(iface);
1476 return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1479 static nsrefcnt NSAPI nsTooltipListener_AddRef(nsITooltipListener *iface)
1481 NSContainer *This = NSTOOLTIP_THIS(iface);
1482 return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1485 static nsrefcnt NSAPI nsTooltipListener_Release(nsITooltipListener *iface)
1487 NSContainer *This = NSTOOLTIP_THIS(iface);
1488 return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1491 static nsresult NSAPI nsTooltipListener_OnShowTooltip(nsITooltipListener *iface,
1492 PRInt32 aXCoord, PRInt32 aYCoord, const PRUnichar *aTipText)
1494 NSContainer *This = NSTOOLTIP_THIS(iface);
1496 if (This->doc)
1497 show_tooltip(This->doc, aXCoord, aYCoord, aTipText);
1499 return NS_OK;
1502 static nsresult NSAPI nsTooltipListener_OnHideTooltip(nsITooltipListener *iface)
1504 NSContainer *This = NSTOOLTIP_THIS(iface);
1506 if (This->doc)
1507 hide_tooltip(This->doc);
1509 return NS_OK;
1512 #undef NSTOOLTIM_THIS
1514 static const nsITooltipListenerVtbl nsTooltipListenerVtbl = {
1515 nsTooltipListener_QueryInterface,
1516 nsTooltipListener_AddRef,
1517 nsTooltipListener_Release,
1518 nsTooltipListener_OnShowTooltip,
1519 nsTooltipListener_OnHideTooltip
1522 #define NSIFACEREQ_THIS(iface) DEFINE_THIS(NSContainer, InterfaceRequestor, iface)
1524 static nsresult NSAPI nsInterfaceRequestor_QueryInterface(nsIInterfaceRequestor *iface,
1525 nsIIDRef riid, nsQIResult result)
1527 NSContainer *This = NSIFACEREQ_THIS(iface);
1528 return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1531 static nsrefcnt NSAPI nsInterfaceRequestor_AddRef(nsIInterfaceRequestor *iface)
1533 NSContainer *This = NSIFACEREQ_THIS(iface);
1534 return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1537 static nsrefcnt NSAPI nsInterfaceRequestor_Release(nsIInterfaceRequestor *iface)
1539 NSContainer *This = NSIFACEREQ_THIS(iface);
1540 return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1543 static nsresult NSAPI nsInterfaceRequestor_GetInterface(nsIInterfaceRequestor *iface,
1544 nsIIDRef riid, nsQIResult result)
1546 NSContainer *This = NSIFACEREQ_THIS(iface);
1548 if(IsEqualGUID(&IID_nsIDOMWindow, riid)) {
1549 TRACE("(%p)->(IID_nsIDOMWindow %p)\n", This, result);
1550 return nsIWebBrowser_GetContentDOMWindow(This->webbrowser, (nsIDOMWindow**)result);
1553 return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1556 #undef NSIFACEREQ_THIS
1558 static const nsIInterfaceRequestorVtbl nsInterfaceRequestorVtbl = {
1559 nsInterfaceRequestor_QueryInterface,
1560 nsInterfaceRequestor_AddRef,
1561 nsInterfaceRequestor_Release,
1562 nsInterfaceRequestor_GetInterface
1565 #define NSWEAKREF_THIS(iface) DEFINE_THIS(NSContainer, WeakReference, iface)
1567 static nsresult NSAPI nsWeakReference_QueryInterface(nsIWeakReference *iface,
1568 nsIIDRef riid, nsQIResult result)
1570 NSContainer *This = NSWEAKREF_THIS(iface);
1571 return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1574 static nsrefcnt NSAPI nsWeakReference_AddRef(nsIWeakReference *iface)
1576 NSContainer *This = NSWEAKREF_THIS(iface);
1577 return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1580 static nsrefcnt NSAPI nsWeakReference_Release(nsIWeakReference *iface)
1582 NSContainer *This = NSWEAKREF_THIS(iface);
1583 return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1586 static nsresult NSAPI nsWeakReference_QueryReferent(nsIWeakReference *iface,
1587 const nsIID *riid, void **result)
1589 NSContainer *This = NSWEAKREF_THIS(iface);
1590 return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1593 #undef NSWEAKREF_THIS
1595 static const nsIWeakReferenceVtbl nsWeakReferenceVtbl = {
1596 nsWeakReference_QueryInterface,
1597 nsWeakReference_AddRef,
1598 nsWeakReference_Release,
1599 nsWeakReference_QueryReferent
1602 #define NSSUPWEAKREF_THIS(iface) DEFINE_THIS(NSContainer, SupportsWeakReference, iface)
1604 static nsresult NSAPI nsSupportsWeakReference_QueryInterface(nsISupportsWeakReference *iface,
1605 nsIIDRef riid, nsQIResult result)
1607 NSContainer *This = NSSUPWEAKREF_THIS(iface);
1608 return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1611 static nsrefcnt NSAPI nsSupportsWeakReference_AddRef(nsISupportsWeakReference *iface)
1613 NSContainer *This = NSSUPWEAKREF_THIS(iface);
1614 return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1617 static nsrefcnt NSAPI nsSupportsWeakReference_Release(nsISupportsWeakReference *iface)
1619 NSContainer *This = NSSUPWEAKREF_THIS(iface);
1620 return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1623 static nsresult NSAPI nsSupportsWeakReference_GetWeakReference(nsISupportsWeakReference *iface,
1624 nsIWeakReference **_retval)
1626 NSContainer *This = NSSUPWEAKREF_THIS(iface);
1628 TRACE("(%p)->(%p)\n", This, _retval);
1630 nsIWeakReference_AddRef(NSWEAKREF(This));
1631 *_retval = NSWEAKREF(This);
1632 return NS_OK;
1635 #undef NSWEAKREF_THIS
1637 static const nsISupportsWeakReferenceVtbl nsSupportsWeakReferenceVtbl = {
1638 nsSupportsWeakReference_QueryInterface,
1639 nsSupportsWeakReference_AddRef,
1640 nsSupportsWeakReference_Release,
1641 nsSupportsWeakReference_GetWeakReference
1645 NSContainer *NSContainer_Create(HTMLDocument *doc, NSContainer *parent)
1647 nsIWebBrowserSetup *wbsetup;
1648 nsIScrollable *scrollable;
1649 NSContainer *ret;
1650 nsresult nsres;
1652 if(!load_gecko(FALSE))
1653 return NULL;
1655 ret = heap_alloc_zero(sizeof(NSContainer));
1657 ret->lpWebBrowserChromeVtbl = &nsWebBrowserChromeVtbl;
1658 ret->lpContextMenuListenerVtbl = &nsContextMenuListenerVtbl;
1659 ret->lpURIContentListenerVtbl = &nsURIContentListenerVtbl;
1660 ret->lpEmbeddingSiteWindowVtbl = &nsEmbeddingSiteWindowVtbl;
1661 ret->lpTooltipListenerVtbl = &nsTooltipListenerVtbl;
1662 ret->lpInterfaceRequestorVtbl = &nsInterfaceRequestorVtbl;
1663 ret->lpWeakReferenceVtbl = &nsWeakReferenceVtbl;
1664 ret->lpSupportsWeakReferenceVtbl = &nsSupportsWeakReferenceVtbl;
1666 ret->doc = doc;
1667 ret->ref = 1;
1668 init_mutation(ret);
1670 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_WEBBROWSER_CONTRACTID,
1671 NULL, &IID_nsIWebBrowser, (void**)&ret->webbrowser);
1672 if(NS_FAILED(nsres)) {
1673 ERR("Creating WebBrowser failed: %08x\n", nsres);
1674 heap_free(ret);
1675 return NULL;
1678 if(parent)
1679 nsIWebBrowserChrome_AddRef(NSWBCHROME(parent));
1680 ret->parent = parent;
1682 nsres = nsIWebBrowser_SetContainerWindow(ret->webbrowser, NSWBCHROME(ret));
1683 if(NS_FAILED(nsres))
1684 ERR("SetContainerWindow failed: %08x\n", nsres);
1686 nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIBaseWindow,
1687 (void**)&ret->window);
1688 if(NS_FAILED(nsres))
1689 ERR("Could not get nsIBaseWindow interface: %08x\n", nsres);
1691 nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIWebBrowserSetup,
1692 (void**)&wbsetup);
1693 if(NS_SUCCEEDED(nsres)) {
1694 nsres = nsIWebBrowserSetup_SetProperty(wbsetup, SETUP_IS_CHROME_WRAPPER, FALSE);
1695 nsIWebBrowserSetup_Release(wbsetup);
1696 if(NS_FAILED(nsres))
1697 ERR("SetProperty(SETUP_IS_CHROME_WRAPPER) failed: %08x\n", nsres);
1698 }else {
1699 ERR("Could not get nsIWebBrowserSetup interface\n");
1702 nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIWebNavigation,
1703 (void**)&ret->navigation);
1704 if(NS_FAILED(nsres))
1705 ERR("Could not get nsIWebNavigation interface: %08x\n", nsres);
1707 nsres = nsIWebBrowserFocus_QueryInterface(ret->webbrowser, &IID_nsIWebBrowserFocus,
1708 (void**)&ret->focus);
1709 if(NS_FAILED(nsres))
1710 ERR("Could not get nsIWebBrowserFocus interface: %08x\n", nsres);
1712 if(!nscontainer_class)
1713 register_nscontainer_class();
1715 ret->hwnd = CreateWindowExW(0, wszNsContainer, NULL,
1716 WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 0, 0, 100, 100,
1717 GetDesktopWindow(), NULL, hInst, ret);
1719 nsres = nsIBaseWindow_InitWindow(ret->window, ret->hwnd, NULL, 0, 0, 100, 100);
1720 if(NS_SUCCEEDED(nsres)) {
1721 nsres = nsIBaseWindow_Create(ret->window);
1722 if(NS_FAILED(nsres))
1723 WARN("Creating window failed: %08x\n", nsres);
1725 nsIBaseWindow_SetVisibility(ret->window, FALSE);
1726 nsIBaseWindow_SetEnabled(ret->window, FALSE);
1727 }else {
1728 ERR("InitWindow failed: %08x\n", nsres);
1731 nsres = nsIWebBrowser_SetParentURIContentListener(ret->webbrowser, NSURICL(ret));
1732 if(NS_FAILED(nsres))
1733 ERR("SetParentURIContentListener failed: %08x\n", nsres);
1735 init_nsevents(ret);
1737 nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIScrollable, (void**)&scrollable);
1738 if(NS_SUCCEEDED(nsres)) {
1739 nsres = nsIScrollable_SetDefaultScrollbarPreferences(scrollable,
1740 ScrollOrientation_Y, Scrollbar_Always);
1741 if(NS_FAILED(nsres))
1742 ERR("Could not set default Y scrollbar prefs: %08x\n", nsres);
1744 nsres = nsIScrollable_SetDefaultScrollbarPreferences(scrollable,
1745 ScrollOrientation_X, Scrollbar_Auto);
1746 if(NS_FAILED(nsres))
1747 ERR("Could not set default X scrollbar prefs: %08x\n", nsres);
1749 nsIScrollable_Release(scrollable);
1750 }else {
1751 ERR("Could not get nsIScrollable: %08x\n", nsres);
1754 return ret;
1757 void NSContainer_Release(NSContainer *This)
1759 TRACE("(%p)\n", This);
1761 This->doc = NULL;
1763 ShowWindow(This->hwnd, SW_HIDE);
1764 SetParent(This->hwnd, NULL);
1766 nsIBaseWindow_SetVisibility(This->window, FALSE);
1767 nsIBaseWindow_Destroy(This->window);
1769 nsIWebBrowser_SetContainerWindow(This->webbrowser, NULL);
1771 nsIWebBrowser_Release(This->webbrowser);
1772 This->webbrowser = NULL;
1774 nsIWebNavigation_Release(This->navigation);
1775 This->navigation = NULL;
1777 nsIBaseWindow_Release(This->window);
1778 This->window = NULL;
1780 nsIWebBrowserFocus_Release(This->focus);
1781 This->focus = NULL;
1783 if(This->editor_controller) {
1784 nsIController_Release(This->editor_controller);
1785 This->editor_controller = NULL;
1788 if(This->editor) {
1789 nsIEditor_Release(This->editor);
1790 This->editor = NULL;
1793 if(This->content_listener) {
1794 nsIURIContentListener_Release(This->content_listener);
1795 This->content_listener = NULL;
1798 if(This->hwnd) {
1799 DestroyWindow(This->hwnd);
1800 This->hwnd = NULL;
1803 nsIWebBrowserChrome_Release(NSWBCHROME(This));