57d2ff2b18fde15bbc9ad433af2fe72a6c7763eb
[reactos.git] / reactos / dll / win32 / mshtml / nsembed.c
1 /*
2 * Copyright 2005-2007 Jacek Caban for CodeWeavers
3 *
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.
8 *
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.
13 *
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
17 */
18
19 #define WIN32_NO_STATUS
20 #define _INC_WINDOWS
21
22 #include <config.h>
23
24 #include <stdarg.h>
25 #include <assert.h>
26
27 #define COBJMACROS
28
29 #include <windef.h>
30 #include <winbase.h>
31 //#include "winuser.h"
32 #include <winreg.h>
33 #include <ole2.h>
34 #include "shlobj.h"
35 #include "shlwapi.h"
36
37 #include <wine/debug.h>
38
39 #include "mshtml_private.h"
40 #include "htmlevent.h"
41 #include "binding.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
44 WINE_DECLARE_DEBUG_CHANNEL(gecko);
45
46 #define NS_APPSTARTUPNOTIFIER_CONTRACTID "@mozilla.org/embedcomp/appstartup-notifier;1"
47 #define NS_WEBBROWSER_CONTRACTID "@mozilla.org/embedding/browser/nsWebBrowser;1"
48 #define NS_MEMORY_CONTRACTID "@mozilla.org/xpcom/memory-service;1"
49 #define NS_COMMANDPARAMS_CONTRACTID "@mozilla.org/embedcomp/command-params;1"
50 #define NS_HTMLSERIALIZER_CONTRACTID "@mozilla.org/layout/contentserializer;1?mimetype=text/html"
51 #define NS_EDITORCONTROLLER_CONTRACTID "@mozilla.org/editor/editorcontroller;1"
52 #define NS_PREFERENCES_CONTRACTID "@mozilla.org/preferences;1"
53 #define NS_VARIANT_CONTRACTID "@mozilla.org/variant;1"
54
55 #define PR_UINT32_MAX 0xffffffff
56
57 #define NS_STRING_CONTAINER_INIT_DEPEND 0x0002
58 #define NS_CSTRING_CONTAINER_INIT_DEPEND 0x0002
59
60 typedef UINT32 PRUint32;
61
62 static nsresult (CDECL *NS_InitXPCOM2)(nsIServiceManager**,void*,void*);
63 static nsresult (CDECL *NS_ShutdownXPCOM)(nsIServiceManager*);
64 static nsresult (CDECL *NS_GetComponentRegistrar)(nsIComponentRegistrar**);
65 static nsresult (CDECL *NS_StringContainerInit2)(nsStringContainer*,const PRUnichar*,PRUint32,PRUint32);
66 static nsresult (CDECL *NS_CStringContainerInit2)(nsCStringContainer*,const char*,PRUint32,PRUint32);
67 static nsresult (CDECL *NS_StringContainerFinish)(nsStringContainer*);
68 static nsresult (CDECL *NS_CStringContainerFinish)(nsCStringContainer*);
69 static nsresult (CDECL *NS_StringSetData)(nsAString*,const PRUnichar*,PRUint32);
70 static nsresult (CDECL *NS_CStringSetData)(nsACString*,const char*,PRUint32);
71 static nsresult (CDECL *NS_NewLocalFile)(const nsAString*,cpp_bool,nsIFile**);
72 static PRUint32 (CDECL *NS_StringGetData)(const nsAString*,const PRUnichar **,cpp_bool*);
73 static PRUint32 (CDECL *NS_CStringGetData)(const nsACString*,const char**,cpp_bool*);
74
75 static HINSTANCE xul_handle = NULL;
76
77 static nsIServiceManager *pServMgr = NULL;
78 static nsIComponentManager *pCompMgr = NULL;
79 static nsIMemory *nsmem = NULL;
80 static nsIFile *profile_directory, *plugin_directory;
81
82 static const WCHAR wszNsContainer[] = {'N','s','C','o','n','t','a','i','n','e','r',0};
83
84 static ATOM nscontainer_class;
85 static WCHAR gecko_path[MAX_PATH];
86 static unsigned gecko_path_len;
87
88 nsresult create_nsfile(const PRUnichar *path, nsIFile **ret)
89 {
90 nsAString str;
91 nsresult nsres;
92
93 nsAString_InitDepend(&str, path);
94 nsres = NS_NewLocalFile(&str, FALSE, ret);
95 nsAString_Finish(&str);
96
97 if(NS_FAILED(nsres))
98 WARN("NS_NewLocalFile failed: %08x\n", nsres);
99 return nsres;
100 }
101
102 typedef struct {
103 nsISimpleEnumerator nsISimpleEnumerator_iface;
104 LONG ref;
105 nsISupports *value;
106 } nsSingletonEnumerator;
107
108 static inline nsSingletonEnumerator *impl_from_nsISimpleEnumerator(nsISimpleEnumerator *iface)
109 {
110 return CONTAINING_RECORD(iface, nsSingletonEnumerator, nsISimpleEnumerator_iface);
111 }
112
113 static nsresult NSAPI nsSingletonEnumerator_QueryInterface(nsISimpleEnumerator *iface, nsIIDRef riid, void **ppv)
114 {
115 nsSingletonEnumerator *This = impl_from_nsISimpleEnumerator(iface);
116
117 if(IsEqualGUID(&IID_nsISupports, riid)) {
118 TRACE("(%p)->(IID_nsISupports %p)\n", This, ppv);
119 *ppv = &This->nsISimpleEnumerator_iface;
120 }else if(IsEqualGUID(&IID_nsISimpleEnumerator, riid)) {
121 TRACE("(%p)->(IID_nsISimpleEnumerator %p)\n", This, ppv);
122 *ppv = &This->nsISimpleEnumerator_iface;
123 }else {
124 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
125 *ppv = NULL;
126 return NS_NOINTERFACE;
127 }
128
129 nsISupports_AddRef((nsISupports*)*ppv);
130 return NS_OK;
131 }
132
133 static nsrefcnt NSAPI nsSingletonEnumerator_AddRef(nsISimpleEnumerator *iface)
134 {
135 nsSingletonEnumerator *This = impl_from_nsISimpleEnumerator(iface);
136 nsrefcnt ref = InterlockedIncrement(&This->ref);
137
138 TRACE("(%p) ref=%d\n", This, ref);
139
140 return ref;
141 }
142
143 static nsrefcnt NSAPI nsSingletonEnumerator_Release(nsISimpleEnumerator *iface)
144 {
145 nsSingletonEnumerator *This = impl_from_nsISimpleEnumerator(iface);
146 nsrefcnt ref = InterlockedDecrement(&This->ref);
147
148 TRACE("(%p) ref=%d\n", This, ref);
149
150 if(!ref) {
151 if(This->value)
152 nsISupports_Release(This->value);
153 heap_free(This);
154 }
155
156 return ref;
157 }
158
159 static nsresult NSAPI nsSingletonEnumerator_HasMoreElements(nsISimpleEnumerator *iface, cpp_bool *_retval)
160 {
161 nsSingletonEnumerator *This = impl_from_nsISimpleEnumerator(iface);
162
163 TRACE("(%p)->()\n", This);
164
165 *_retval = This->value != NULL;
166 return NS_OK;
167 }
168
169 static nsresult NSAPI nsSingletonEnumerator_GetNext(nsISimpleEnumerator *iface, nsISupports **_retval)
170 {
171 nsSingletonEnumerator *This = impl_from_nsISimpleEnumerator(iface);
172
173 TRACE("(%p)->()\n", This);
174
175 if(!This->value)
176 return NS_ERROR_UNEXPECTED;
177
178 *_retval = This->value;
179 This->value = NULL;
180 return NS_OK;
181 }
182
183 static const nsISimpleEnumeratorVtbl nsSingletonEnumeratorVtbl = {
184 nsSingletonEnumerator_QueryInterface,
185 nsSingletonEnumerator_AddRef,
186 nsSingletonEnumerator_Release,
187 nsSingletonEnumerator_HasMoreElements,
188 nsSingletonEnumerator_GetNext
189 };
190
191 static nsISimpleEnumerator *create_singleton_enumerator(nsISupports *value)
192 {
193 nsSingletonEnumerator *ret;
194
195 ret = heap_alloc(sizeof(*ret));
196 if(!ret)
197 return NULL;
198
199 ret->nsISimpleEnumerator_iface.lpVtbl = &nsSingletonEnumeratorVtbl;
200 ret->ref = 1;
201
202 if(value)
203 nsISupports_AddRef(value);
204 ret->value = value;
205 return &ret->nsISimpleEnumerator_iface;
206 }
207
208 static nsresult NSAPI nsDirectoryServiceProvider2_QueryInterface(nsIDirectoryServiceProvider2 *iface,
209 nsIIDRef riid, void **result)
210 {
211 if(IsEqualGUID(&IID_nsISupports, riid)) {
212 TRACE("(IID_nsISupports %p)\n", result);
213 *result = iface;
214 }else if(IsEqualGUID(&IID_nsIDirectoryServiceProvider, riid)) {
215 TRACE("(IID_nsIDirectoryServiceProvider %p)\n", result);
216 *result = iface;
217 }else if(IsEqualGUID(&IID_nsIDirectoryServiceProvider2, riid)) {
218 TRACE("(IID_nsIDirectoryServiceProvider2 %p)\n", result);
219 *result = iface;
220 }else {
221 WARN("(%s %p)\n", debugstr_guid(riid), result);
222 *result = NULL;
223 return NS_NOINTERFACE;
224 }
225
226 nsISupports_AddRef((nsISupports*)*result);
227 return NS_OK;
228 }
229
230 static nsrefcnt NSAPI nsDirectoryServiceProvider2_AddRef(nsIDirectoryServiceProvider2 *iface)
231 {
232 return 2;
233 }
234
235 static nsrefcnt NSAPI nsDirectoryServiceProvider2_Release(nsIDirectoryServiceProvider2 *iface)
236 {
237 return 1;
238 }
239
240 static nsresult create_profile_directory(void)
241 {
242 static const WCHAR wine_geckoW[] = {'\\','w','i','n','e','_','g','e','c','k','o',0};
243
244 WCHAR path[MAX_PATH + sizeof(wine_geckoW)/sizeof(WCHAR)];
245 cpp_bool exists;
246 nsresult nsres;
247 HRESULT hres;
248
249 hres = SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, path);
250 if(FAILED(hres)) {
251 ERR("SHGetFolderPath failed: %08x\n", hres);
252 return NS_ERROR_FAILURE;
253 }
254
255 strcatW(path, wine_geckoW);
256 nsres = create_nsfile(path, &profile_directory);
257 if(NS_FAILED(nsres))
258 return nsres;
259
260 nsres = nsIFile_Exists(profile_directory, &exists);
261 if(NS_FAILED(nsres)) {
262 ERR("Exists failed: %08x\n", nsres);
263 return nsres;
264 }
265
266 if(!exists) {
267 nsres = nsIFile_Create(profile_directory, 1, 0700);
268 if(NS_FAILED(nsres))
269 ERR("Create failed: %08x\n", nsres);
270 }
271
272 return nsres;
273 }
274
275 static nsresult NSAPI nsDirectoryServiceProvider2_GetFile(nsIDirectoryServiceProvider2 *iface,
276 const char *prop, cpp_bool *persistent, nsIFile **_retval)
277 {
278 TRACE("(%s %p %p)\n", debugstr_a(prop), persistent, _retval);
279
280 if(!strcmp(prop, "ProfD")) {
281 if(!profile_directory) {
282 nsresult nsres;
283
284 nsres = create_profile_directory();
285 if(NS_FAILED(nsres))
286 return nsres;
287 }
288
289 assert(profile_directory != NULL);
290 return nsIFile_Clone(profile_directory, _retval);
291 }
292
293 *_retval = NULL;
294 return NS_ERROR_FAILURE;
295 }
296
297 static nsresult NSAPI nsDirectoryServiceProvider2_GetFiles(nsIDirectoryServiceProvider2 *iface,
298 const char *prop, nsISimpleEnumerator **_retval)
299 {
300 TRACE("(%s %p)\n", debugstr_a(prop), _retval);
301
302 if(!strcmp(prop, "APluginsDL")) {
303 WCHAR plugin_path[MAX_PATH];
304 nsIFile *file;
305 int len;
306 nsresult nsres;
307
308 if(!plugin_directory) {
309 static const WCHAR gecko_pluginW[] = {'\\','g','e','c','k','o','\\','p','l','u','g','i','n',0};
310
311 len = GetSystemDirectoryW(plugin_path, (sizeof(plugin_path)-sizeof(gecko_pluginW))/sizeof(WCHAR)+1);
312 if(!len)
313 return NS_ERROR_UNEXPECTED;
314
315 strcpyW(plugin_path+len, gecko_pluginW);
316 nsres = create_nsfile(plugin_path, &plugin_directory);
317 if(NS_FAILED(nsres)) {
318 *_retval = NULL;
319 return nsres;
320 }
321 }
322
323 nsres = nsIFile_Clone(plugin_directory, &file);
324 if(NS_FAILED(nsres))
325 return nsres;
326
327 *_retval = create_singleton_enumerator((nsISupports*)file);
328 nsIFile_Release(file);
329 if(!*_retval)
330 return NS_ERROR_OUT_OF_MEMORY;
331
332 return NS_OK;
333 }
334
335 *_retval = NULL;
336 return NS_ERROR_FAILURE;
337 }
338
339 static const nsIDirectoryServiceProvider2Vtbl nsDirectoryServiceProvider2Vtbl = {
340 nsDirectoryServiceProvider2_QueryInterface,
341 nsDirectoryServiceProvider2_AddRef,
342 nsDirectoryServiceProvider2_Release,
343 nsDirectoryServiceProvider2_GetFile,
344 nsDirectoryServiceProvider2_GetFiles
345 };
346
347 static nsIDirectoryServiceProvider2 nsDirectoryServiceProvider2 =
348 { &nsDirectoryServiceProvider2Vtbl };
349
350 static LRESULT WINAPI nsembed_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
351 {
352 NSContainer *This;
353 nsresult nsres;
354
355 static const WCHAR wszTHIS[] = {'T','H','I','S',0};
356
357 if(msg == WM_CREATE) {
358 This = *(NSContainer**)lParam;
359 SetPropW(hwnd, wszTHIS, This);
360 }else {
361 This = GetPropW(hwnd, wszTHIS);
362 }
363
364 switch(msg) {
365 case WM_SIZE:
366 TRACE("(%p)->(WM_SIZE)\n", This);
367
368 nsres = nsIBaseWindow_SetSize(This->window,
369 LOWORD(lParam), HIWORD(lParam), TRUE);
370 if(NS_FAILED(nsres))
371 WARN("SetSize failed: %08x\n", nsres);
372 break;
373
374 case WM_PARENTNOTIFY:
375 TRACE("WM_PARENTNOTIFY %x\n", (unsigned)wParam);
376
377 switch(wParam) {
378 case WM_LBUTTONDOWN:
379 case WM_RBUTTONDOWN:
380 nsIWebBrowserFocus_Activate(This->focus);
381 }
382 }
383
384 return DefWindowProcW(hwnd, msg, wParam, lParam);
385 }
386
387
388 static void register_nscontainer_class(void)
389 {
390 static WNDCLASSEXW wndclass = {
391 sizeof(WNDCLASSEXW),
392 CS_DBLCLKS,
393 nsembed_proc,
394 0, 0, NULL, NULL, NULL, NULL, NULL,
395 wszNsContainer,
396 NULL,
397 };
398 wndclass.hInstance = hInst;
399 nscontainer_class = RegisterClassExW(&wndclass);
400 }
401
402 #ifndef __REACTOS__
403 static BOOL install_wine_gecko(void)
404 {
405 PROCESS_INFORMATION pi;
406 STARTUPINFOW si;
407 WCHAR app[MAX_PATH];
408 WCHAR *args;
409 LONG len;
410 BOOL ret;
411
412 static const WCHAR controlW[] = {'\\','c','o','n','t','r','o','l','.','e','x','e',0};
413 static const WCHAR argsW[] =
414 {' ','a','p','p','w','i','z','.','c','p','l',' ','i','n','s','t','a','l','l','_','g','e','c','k','o',0};
415
416 len = GetSystemDirectoryW(app, MAX_PATH-sizeof(controlW)/sizeof(WCHAR));
417 memcpy(app+len, controlW, sizeof(controlW));
418
419 args = heap_alloc(len*sizeof(WCHAR) + sizeof(controlW) + sizeof(argsW));
420 if(!args)
421 return FALSE;
422
423 memcpy(args, app, len*sizeof(WCHAR) + sizeof(controlW));
424 memcpy(args + len + sizeof(controlW)/sizeof(WCHAR)-1, argsW, sizeof(argsW));
425
426 TRACE("starting %s\n", debugstr_w(args));
427
428 memset(&si, 0, sizeof(si));
429 si.cb = sizeof(si);
430 ret = CreateProcessW(app, args, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
431 heap_free(args);
432 if (ret) {
433 CloseHandle(pi.hThread);
434 WaitForSingleObject(pi.hProcess, INFINITE);
435 CloseHandle(pi.hProcess);
436 }
437
438 return ret;
439 }
440 #endif
441
442 static void set_environment(LPCWSTR gre_path)
443 {
444 WCHAR path_env[MAX_PATH], buf[20];
445 int len, debug_level = 0;
446
447 static const WCHAR pathW[] = {'P','A','T','H',0};
448 static const WCHAR warnW[] = {'w','a','r','n',0};
449 static const WCHAR xpcom_debug_breakW[] =
450 {'X','P','C','O','M','_','D','E','B','U','G','_','B','R','E','A','K',0};
451 static const WCHAR nspr_log_modulesW[] =
452 {'N','S','P','R','_','L','O','G','_','M','O','D','U','L','E','S',0};
453 static const WCHAR debug_formatW[] = {'a','l','l',':','%','d',0};
454
455 /* We have to modify PATH as XPCOM loads other DLLs from this directory. */
456 GetEnvironmentVariableW(pathW, path_env, sizeof(path_env)/sizeof(WCHAR));
457 len = strlenW(path_env);
458 path_env[len++] = ';';
459 strcpyW(path_env+len, gre_path);
460 SetEnvironmentVariableW(pathW, path_env);
461
462 SetEnvironmentVariableW(xpcom_debug_breakW, warnW);
463
464 if(TRACE_ON(gecko))
465 debug_level = 5;
466 else if(WARN_ON(gecko))
467 debug_level = 3;
468 else if(ERR_ON(gecko))
469 debug_level = 2;
470
471 sprintfW(buf, debug_formatW, debug_level);
472 SetEnvironmentVariableW(nspr_log_modulesW, buf);
473 }
474
475 static BOOL load_xul(const PRUnichar *gre_path)
476 {
477 static const WCHAR xul_dllW[] = {'\\','x','u','l','.','d','l','l',0};
478 WCHAR file_name[MAX_PATH];
479
480 strcpyW(file_name, gre_path);
481 strcatW(file_name, xul_dllW);
482
483 TRACE("(%s)\n", debugstr_w(file_name));
484
485 set_environment(gre_path);
486
487 xul_handle = LoadLibraryExW(file_name, 0, LOAD_WITH_ALTERED_SEARCH_PATH);
488 if(!xul_handle) {
489 WARN("Could not load XUL: %d\n", GetLastError());
490 return FALSE;
491 }
492
493 #define NS_DLSYM(func) \
494 func = (void *)GetProcAddress(xul_handle, #func "_P"); \
495 if(!func) \
496 ERR("Could not GetProcAddress(" #func ") failed\n")
497
498 NS_DLSYM(NS_InitXPCOM2);
499 NS_DLSYM(NS_ShutdownXPCOM);
500 NS_DLSYM(NS_GetComponentRegistrar);
501 NS_DLSYM(NS_StringContainerInit2);
502 NS_DLSYM(NS_CStringContainerInit2);
503 NS_DLSYM(NS_StringContainerFinish);
504 NS_DLSYM(NS_CStringContainerFinish);
505 NS_DLSYM(NS_StringSetData);
506 NS_DLSYM(NS_CStringSetData);
507 NS_DLSYM(NS_NewLocalFile);
508 NS_DLSYM(NS_StringGetData);
509 NS_DLSYM(NS_CStringGetData);
510
511 #undef NS_DLSYM
512
513 #define NS_DLSYM(func) \
514 func = (void *)GetProcAddress(xul_handle, #func); \
515 if(!func) \
516 ERR("Could not GetProcAddress(" #func ") failed\n")
517
518 NS_DLSYM(ccref_incr);
519 NS_DLSYM(ccref_decr);
520 NS_DLSYM(ccref_init);
521 NS_DLSYM(ccref_unmark_if_purple);
522 NS_DLSYM(ccp_init);
523 NS_DLSYM(describe_cc_node);
524 NS_DLSYM(note_cc_edge);
525
526 #undef NS_DLSYM
527
528 return TRUE;
529 }
530
531 static BOOL check_version(LPCWSTR gre_path, const char *version_string)
532 {
533 WCHAR file_name[MAX_PATH];
534 char version[128];
535 DWORD read=0;
536 HANDLE hfile;
537
538 static const WCHAR wszVersion[] = {'\\','V','E','R','S','I','O','N',0};
539
540 strcpyW(file_name, gre_path);
541 strcatW(file_name, wszVersion);
542
543 hfile = CreateFileW(file_name, GENERIC_READ, FILE_SHARE_READ, NULL,
544 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
545 if(hfile == INVALID_HANDLE_VALUE) {
546 ERR("Could not open VERSION file\n");
547 return FALSE;
548 }
549
550 ReadFile(hfile, version, sizeof(version), &read, NULL);
551 version[read] = 0;
552 CloseHandle(hfile);
553
554 TRACE("%s\n", debugstr_a(version));
555
556 if(strcmp(version, version_string)) {
557 ERR("Unexpected version %s, expected %s\n", debugstr_a(version),
558 debugstr_a(version_string));
559 return FALSE;
560 }
561
562 return TRUE;
563 }
564
565 static BOOL load_wine_gecko_v(PRUnichar *gre_path, HKEY mshtml_key,
566 const char *version, const char *version_string)
567 {
568 DWORD res, type, size = MAX_PATH;
569 HKEY hkey = mshtml_key;
570
571 static const WCHAR wszGeckoPath[] =
572 {'G','e','c','k','o','P','a','t','h',0};
573
574 if(version) {
575 /* @@ Wine registry key: HKLM\Software\Wine\MSHTML\<version> */
576 res = RegOpenKeyA(mshtml_key, version, &hkey);
577 if(res != ERROR_SUCCESS)
578 return FALSE;
579 }
580
581 res = RegQueryValueExW(hkey, wszGeckoPath, NULL, &type, (LPBYTE)gre_path, &size);
582 if(hkey != mshtml_key)
583 RegCloseKey(hkey);
584 if(res != ERROR_SUCCESS || type != REG_SZ)
585 return FALSE;
586
587 if(!check_version(gre_path, version_string))
588 return FALSE;
589
590 return load_xul(gre_path);
591 }
592
593 static BOOL load_wine_gecko(PRUnichar *gre_path)
594 {
595 HKEY hkey;
596 DWORD res;
597 BOOL ret;
598
599 static const WCHAR wszMshtmlKey[] = {
600 'S','o','f','t','w','a','r','e','\\','W','i','n','e',
601 '\\','M','S','H','T','M','L',0};
602
603 /* @@ Wine registry key: HKLM\Software\Wine\MSHTML */
604 res = RegOpenKeyW(HKEY_LOCAL_MACHINE, wszMshtmlKey, &hkey);
605 if(res != ERROR_SUCCESS)
606 return FALSE;
607
608 ret = load_wine_gecko_v(gre_path, hkey, GECKO_VERSION, GECKO_VERSION_STRING);
609
610 RegCloseKey(hkey);
611 return ret;
612 }
613
614 static void set_bool_pref(nsIPrefBranch *pref, const char *pref_name, BOOL val)
615 {
616 nsresult nsres;
617
618 nsres = nsIPrefBranch_SetBoolPref(pref, pref_name, val);
619 if(NS_FAILED(nsres))
620 ERR("Could not set pref %s\n", debugstr_a(pref_name));
621 }
622
623 static void set_int_pref(nsIPrefBranch *pref, const char *pref_name, int val)
624 {
625 nsresult nsres;
626
627 nsres = nsIPrefBranch_SetIntPref(pref, pref_name, val);
628 if(NS_FAILED(nsres))
629 ERR("Could not set pref %s\n", debugstr_a(pref_name));
630 }
631
632 static void set_string_pref(nsIPrefBranch *pref, const char *pref_name, const char *val)
633 {
634 nsresult nsres;
635
636 nsres = nsIPrefBranch_SetCharPref(pref, pref_name, val);
637 if(NS_FAILED(nsres))
638 ERR("Could not set pref %s\n", debugstr_a(pref_name));
639 }
640
641 static void set_lang(nsIPrefBranch *pref)
642 {
643 char langs[100];
644 DWORD res, size, type;
645 HKEY hkey;
646
647 static const WCHAR international_keyW[] =
648 {'S','o','f','t','w','a','r','e',
649 '\\','M','i','c','r','o','s','o','f','t',
650 '\\','I','n','t','e','r','n','e','t',' ','E','x','p','l','o','r','e','r',
651 '\\','I','n','t','e','r','n','a','t','i','o','n','a','l',0};
652
653 res = RegOpenKeyW(HKEY_CURRENT_USER, international_keyW, &hkey);
654 if(res != ERROR_SUCCESS)
655 return;
656
657 size = sizeof(langs);
658 res = RegQueryValueExA(hkey, "AcceptLanguage", 0, &type, (LPBYTE)langs, &size);
659 RegCloseKey(hkey);
660 if(res != ERROR_SUCCESS || type != REG_SZ)
661 return;
662
663 TRACE("Setting lang %s\n", debugstr_a(langs));
664
665 set_string_pref(pref, "intl.accept_languages", langs);
666 }
667
668 static void set_preferences(void)
669 {
670 nsIPrefBranch *pref;
671 nsresult nsres;
672
673 nsres = nsIServiceManager_GetServiceByContractID(pServMgr, NS_PREFERENCES_CONTRACTID,
674 &IID_nsIPrefBranch, (void**)&pref);
675 if(NS_FAILED(nsres)) {
676 ERR("Could not get preference service: %08x\n", nsres);
677 return;
678 }
679
680 set_lang(pref);
681 set_bool_pref(pref, "security.warn_entering_secure", FALSE);
682 set_bool_pref(pref, "security.warn_submit_insecure", FALSE);
683 set_int_pref(pref, "layout.spellcheckDefault", 0);
684
685 nsIPrefBranch_Release(pref);
686 }
687
688 static BOOL init_xpcom(const PRUnichar *gre_path)
689 {
690 nsIComponentRegistrar *registrar = NULL;
691 nsIFile *gre_dir;
692 WCHAR *ptr;
693 nsresult nsres;
694
695 nsres = create_nsfile(gre_path, &gre_dir);
696 if(NS_FAILED(nsres)) {
697 FreeLibrary(xul_handle);
698 return FALSE;
699 }
700
701 nsres = NS_InitXPCOM2(&pServMgr, gre_dir, (nsIDirectoryServiceProvider*)&nsDirectoryServiceProvider2);
702 if(NS_FAILED(nsres)) {
703 ERR("NS_InitXPCOM2 failed: %08x\n", nsres);
704 FreeLibrary(xul_handle);
705 return FALSE;
706 }
707
708 strcpyW(gecko_path, gre_path);
709 for(ptr = gecko_path; *ptr; ptr++) {
710 if(*ptr == '\\')
711 *ptr = '/';
712 }
713 gecko_path_len = ptr-gecko_path;
714
715 nsres = nsIServiceManager_QueryInterface(pServMgr, &IID_nsIComponentManager, (void**)&pCompMgr);
716 if(NS_FAILED(nsres))
717 ERR("Could not get nsIComponentManager: %08x\n", nsres);
718
719 nsres = NS_GetComponentRegistrar(&registrar);
720 if(NS_SUCCEEDED(nsres))
721 init_nsio(pCompMgr, registrar);
722 else
723 ERR("NS_GetComponentRegistrar failed: %08x\n", nsres);
724
725 init_mutation(pCompMgr);
726 set_preferences();
727
728 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_MEMORY_CONTRACTID,
729 NULL, &IID_nsIMemory, (void**)&nsmem);
730 if(NS_FAILED(nsres))
731 ERR("Could not get nsIMemory: %08x\n", nsres);
732
733 if(registrar) {
734 register_nsservice(registrar, pServMgr);
735 nsIComponentRegistrar_Release(registrar);
736 }
737
738 init_node_cc();
739
740 return TRUE;
741 }
742
743 static CRITICAL_SECTION cs_load_gecko;
744 static CRITICAL_SECTION_DEBUG cs_load_gecko_dbg =
745 {
746 0, 0, &cs_load_gecko,
747 { &cs_load_gecko_dbg.ProcessLocksList, &cs_load_gecko_dbg.ProcessLocksList },
748 0, 0, { (DWORD_PTR)(__FILE__ ": load_gecko") }
749 };
750 static CRITICAL_SECTION cs_load_gecko = { &cs_load_gecko_dbg, -1, 0, 0, 0, 0 };
751
752 BOOL load_gecko(void)
753 {
754 PRUnichar gre_path[MAX_PATH];
755 BOOL ret = FALSE;
756
757 static DWORD loading_thread;
758
759 TRACE("()\n");
760
761 /* load_gecko may be called recursively */
762 if(loading_thread == GetCurrentThreadId())
763 return pCompMgr != NULL;
764
765 EnterCriticalSection(&cs_load_gecko);
766
767 if(!loading_thread) {
768 loading_thread = GetCurrentThreadId();
769
770 #ifdef __REACTOS__
771 if(load_wine_gecko(gre_path))
772 #else
773 if(load_wine_gecko(gre_path)
774 || (install_wine_gecko() && load_wine_gecko(gre_path)))
775 #endif
776 ret = init_xpcom(gre_path);
777 else
778 MESSAGE("Could not load wine-gecko. HTML rendering will be disabled.\n");
779 }else {
780 ret = pCompMgr != NULL;
781 }
782
783 LeaveCriticalSection(&cs_load_gecko);
784
785 return ret;
786 }
787
788 void *nsalloc(size_t size)
789 {
790 return nsIMemory_Alloc(nsmem, size);
791 }
792
793 void nsfree(void *mem)
794 {
795 nsIMemory_Free(nsmem, mem);
796 }
797
798 static BOOL nsACString_Init(nsACString *str, const char *data)
799 {
800 return NS_SUCCEEDED(NS_CStringContainerInit2(str, data, PR_UINT32_MAX, 0));
801 }
802
803 /*
804 * Initializes nsACString with data owned by caller.
805 * Caller must ensure that data is valid during lifetime of string object.
806 */
807 void nsACString_InitDepend(nsACString *str, const char *data)
808 {
809 NS_CStringContainerInit2(str, data, PR_UINT32_MAX, NS_CSTRING_CONTAINER_INIT_DEPEND);
810 }
811
812 void nsACString_SetData(nsACString *str, const char *data)
813 {
814 NS_CStringSetData(str, data, PR_UINT32_MAX);
815 }
816
817 UINT32 nsACString_GetData(const nsACString *str, const char **data)
818 {
819 return NS_CStringGetData(str, data, NULL);
820 }
821
822 void nsACString_Finish(nsACString *str)
823 {
824 NS_CStringContainerFinish(str);
825 }
826
827 BOOL nsAString_Init(nsAString *str, const PRUnichar *data)
828 {
829 return NS_SUCCEEDED(NS_StringContainerInit2(str, data, PR_UINT32_MAX, 0));
830 }
831
832 /*
833 * Initializes nsAString with data owned by caller.
834 * Caller must ensure that data is valid during lifetime of string object.
835 */
836 void nsAString_InitDepend(nsAString *str, const PRUnichar *data)
837 {
838 NS_StringContainerInit2(str, data, PR_UINT32_MAX, NS_STRING_CONTAINER_INIT_DEPEND);
839 }
840
841 void nsAString_SetData(nsAString *str, const PRUnichar *data)
842 {
843 NS_StringSetData(str, data, PR_UINT32_MAX);
844 }
845
846 UINT32 nsAString_GetData(const nsAString *str, const PRUnichar **data)
847 {
848 return NS_StringGetData(str, data, NULL);
849 }
850
851 void nsAString_Finish(nsAString *str)
852 {
853 NS_StringContainerFinish(str);
854 }
855
856 HRESULT return_nsstr(nsresult nsres, nsAString *nsstr, BSTR *p)
857 {
858 const PRUnichar *str;
859
860 if(NS_FAILED(nsres)) {
861 ERR("failed: %08x\n", nsres);
862 nsAString_Finish(nsstr);
863 return E_FAIL;
864 }
865
866 nsAString_GetData(nsstr, &str);
867 TRACE("ret %s\n", debugstr_w(str));
868 if(*str) {
869 *p = SysAllocString(str);
870 if(!*p)
871 return E_OUTOFMEMORY;
872 }else {
873 *p = NULL;
874 }
875
876 nsAString_Finish(nsstr);
877 return S_OK;
878 }
879
880 nsICommandParams *create_nscommand_params(void)
881 {
882 nsICommandParams *ret = NULL;
883 nsresult nsres;
884
885 if(!pCompMgr)
886 return NULL;
887
888 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
889 NS_COMMANDPARAMS_CONTRACTID, NULL, &IID_nsICommandParams,
890 (void**)&ret);
891 if(NS_FAILED(nsres))
892 ERR("Could not get nsICommandParams\n");
893
894 return ret;
895 }
896
897 nsIWritableVariant *create_nsvariant(void)
898 {
899 nsIWritableVariant *ret = NULL;
900 nsresult nsres;
901
902 if(!pCompMgr)
903 return NULL;
904
905 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
906 NS_VARIANT_CONTRACTID, NULL, &IID_nsIWritableVariant, (void**)&ret);
907 if(NS_FAILED(nsres))
908 ERR("Could not get nsIVariant\n");
909
910 return ret;
911 }
912
913 nsresult get_nsinterface(nsISupports *iface, REFIID riid, void **ppv)
914 {
915 nsIInterfaceRequestor *iface_req;
916 nsresult nsres;
917
918 nsres = nsISupports_QueryInterface(iface, &IID_nsIInterfaceRequestor, (void**)&iface_req);
919 if(NS_FAILED(nsres))
920 return nsres;
921
922 nsres = nsIInterfaceRequestor_GetInterface(iface_req, riid, ppv);
923 nsIInterfaceRequestor_Release(iface_req);
924
925 return nsres;
926 }
927
928 static HRESULT nsnode_to_nsstring_rec(nsIContentSerializer *serializer, nsIDOMNode *nsnode, nsAString *str)
929 {
930 nsIDOMNodeList *node_list = NULL;
931 cpp_bool has_children = FALSE;
932 nsIContent *nscontent;
933 UINT16 type;
934 nsresult nsres;
935
936 nsIDOMNode_HasChildNodes(nsnode, &has_children);
937
938 nsres = nsIDOMNode_GetNodeType(nsnode, &type);
939 if(NS_FAILED(nsres)) {
940 ERR("GetType failed: %08x\n", nsres);
941 return E_FAIL;
942 }
943
944 if(type != DOCUMENT_NODE) {
945 nsres = nsIDOMNode_QueryInterface(nsnode, &IID_nsIContent, (void**)&nscontent);
946 if(NS_FAILED(nsres)) {
947 ERR("Could not get nsIContent interface: %08x\n", nsres);
948 return E_FAIL;
949 }
950 }
951
952 switch(type) {
953 case ELEMENT_NODE:
954 nsIContentSerializer_AppendElementStart(serializer, nscontent, nscontent, str);
955 break;
956 case TEXT_NODE:
957 nsIContentSerializer_AppendText(serializer, nscontent, 0, -1, str);
958 break;
959 case COMMENT_NODE:
960 nsres = nsIContentSerializer_AppendComment(serializer, nscontent, 0, -1, str);
961 break;
962 case DOCUMENT_NODE: {
963 nsIDocument *nsdoc;
964 nsIDOMNode_QueryInterface(nsnode, &IID_nsIDocument, (void**)&nsdoc);
965 nsIContentSerializer_AppendDocumentStart(serializer, nsdoc, str);
966 nsIDocument_Release(nsdoc);
967 break;
968 }
969 case DOCUMENT_TYPE_NODE:
970 nsIContentSerializer_AppendDoctype(serializer, nscontent, str);
971 break;
972 case DOCUMENT_FRAGMENT_NODE:
973 break;
974 default:
975 FIXME("Unhandled type %u\n", type);
976 }
977
978 if(has_children) {
979 UINT32 child_cnt, i;
980 nsIDOMNode *child_node;
981
982 nsIDOMNode_GetChildNodes(nsnode, &node_list);
983 nsIDOMNodeList_GetLength(node_list, &child_cnt);
984
985 for(i=0; i<child_cnt; i++) {
986 nsres = nsIDOMNodeList_Item(node_list, i, &child_node);
987 if(NS_SUCCEEDED(nsres)) {
988 nsnode_to_nsstring_rec(serializer, child_node, str);
989 nsIDOMNode_Release(child_node);
990 }else {
991 ERR("Item failed: %08x\n", nsres);
992 }
993 }
994
995 nsIDOMNodeList_Release(node_list);
996 }
997
998 if(type == ELEMENT_NODE)
999 nsIContentSerializer_AppendElementEnd(serializer, nscontent, str);
1000
1001 if(type != DOCUMENT_NODE)
1002 nsIContent_Release(nscontent);
1003 return S_OK;
1004 }
1005
1006 HRESULT nsnode_to_nsstring(nsIDOMNode *nsnode, nsAString *str)
1007 {
1008 nsIContentSerializer *serializer;
1009 nsresult nsres;
1010 HRESULT hres;
1011
1012 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
1013 NS_HTMLSERIALIZER_CONTRACTID, NULL, &IID_nsIContentSerializer,
1014 (void**)&serializer);
1015 if(NS_FAILED(nsres)) {
1016 ERR("Could not get nsIContentSerializer: %08x\n", nsres);
1017 return E_FAIL;
1018 }
1019
1020 nsres = nsIContentSerializer_Init(serializer, 0, 100, NULL, FALSE, FALSE /* FIXME */);
1021 if(NS_FAILED(nsres))
1022 ERR("Init failed: %08x\n", nsres);
1023
1024 hres = nsnode_to_nsstring_rec(serializer, nsnode, str);
1025 if(SUCCEEDED(hres)) {
1026 nsres = nsIContentSerializer_Flush(serializer, str);
1027 if(NS_FAILED(nsres))
1028 ERR("Flush failed: %08x\n", nsres);
1029 }
1030
1031 nsIContentSerializer_Release(serializer);
1032 return hres;
1033 }
1034
1035 void get_editor_controller(NSContainer *This)
1036 {
1037 nsIEditingSession *editing_session = NULL;
1038 nsIControllerContext *ctrlctx;
1039 nsresult nsres;
1040
1041 if(This->editor) {
1042 nsIEditor_Release(This->editor);
1043 This->editor = NULL;
1044 }
1045
1046 if(This->editor_controller) {
1047 nsIController_Release(This->editor_controller);
1048 This->editor_controller = NULL;
1049 }
1050
1051 nsres = get_nsinterface((nsISupports*)This->webbrowser, &IID_nsIEditingSession,
1052 (void**)&editing_session);
1053 if(NS_FAILED(nsres)) {
1054 ERR("Could not get nsIEditingSession: %08x\n", nsres);
1055 return;
1056 }
1057
1058 nsres = nsIEditingSession_GetEditorForWindow(editing_session,
1059 This->doc->basedoc.window->nswindow, &This->editor);
1060 nsIEditingSession_Release(editing_session);
1061 if(NS_FAILED(nsres)) {
1062 ERR("Could not get editor: %08x\n", nsres);
1063 return;
1064 }
1065
1066 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
1067 NS_EDITORCONTROLLER_CONTRACTID, NULL, &IID_nsIControllerContext, (void**)&ctrlctx);
1068 if(NS_SUCCEEDED(nsres)) {
1069 nsres = nsIControllerContext_SetCommandContext(ctrlctx, (nsISupports *)This->editor);
1070 if(NS_FAILED(nsres))
1071 ERR("SetCommandContext failed: %08x\n", nsres);
1072 nsres = nsIControllerContext_QueryInterface(ctrlctx, &IID_nsIController,
1073 (void**)&This->editor_controller);
1074 nsIControllerContext_Release(ctrlctx);
1075 if(NS_FAILED(nsres))
1076 ERR("Could not get nsIController interface: %08x\n", nsres);
1077 }else {
1078 ERR("Could not create edit controller: %08x\n", nsres);
1079 }
1080 }
1081
1082 void close_gecko(void)
1083 {
1084 TRACE("()\n");
1085
1086 release_nsio();
1087 init_mutation(NULL);
1088
1089 if(profile_directory) {
1090 nsIFile_Release(profile_directory);
1091 profile_directory = NULL;
1092 }
1093
1094 if(plugin_directory) {
1095 nsIFile_Release(plugin_directory);
1096 plugin_directory = NULL;
1097 }
1098
1099 if(pCompMgr)
1100 nsIComponentManager_Release(pCompMgr);
1101
1102 if(pServMgr)
1103 nsIServiceManager_Release(pServMgr);
1104
1105 if(nsmem)
1106 nsIMemory_Release(nsmem);
1107
1108 /* Gecko doesn't really support being unloaded */
1109 /* if (hXPCOM) FreeLibrary(hXPCOM); */
1110
1111 DeleteCriticalSection(&cs_load_gecko);
1112 }
1113
1114 BOOL is_gecko_path(const char *path)
1115 {
1116 WCHAR *buf, *ptr;
1117 BOOL ret;
1118
1119 buf = heap_strdupUtoW(path);
1120 if(!buf || strlenW(buf) < gecko_path_len)
1121 return FALSE;
1122
1123 for(ptr = buf; *ptr; ptr++) {
1124 if(*ptr == '\\')
1125 *ptr = '/';
1126 }
1127
1128 UrlUnescapeW(buf, NULL, NULL, URL_UNESCAPE_INPLACE);
1129 buf[gecko_path_len] = 0;
1130
1131 ret = !strcmpiW(buf, gecko_path);
1132 heap_free(buf);
1133 return ret;
1134 }
1135
1136 struct nsWeakReference {
1137 nsIWeakReference nsIWeakReference_iface;
1138
1139 LONG ref;
1140
1141 NSContainer *nscontainer;
1142 };
1143
1144 static inline nsWeakReference *impl_from_nsIWeakReference(nsIWeakReference *iface)
1145 {
1146 return CONTAINING_RECORD(iface, nsWeakReference, nsIWeakReference_iface);
1147 }
1148
1149 static nsresult NSAPI nsWeakReference_QueryInterface(nsIWeakReference *iface,
1150 nsIIDRef riid, void **result)
1151 {
1152 nsWeakReference *This = impl_from_nsIWeakReference(iface);
1153
1154 if(IsEqualGUID(&IID_nsISupports, riid)) {
1155 TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
1156 *result = &This->nsIWeakReference_iface;
1157 }else if(IsEqualGUID(&IID_nsIWeakReference, riid)) {
1158 TRACE("(%p)->(IID_nsIWeakReference %p)\n", This, result);
1159 *result = &This->nsIWeakReference_iface;
1160 }else {
1161 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
1162 *result = NULL;
1163 return NS_NOINTERFACE;
1164 }
1165
1166 nsISupports_AddRef((nsISupports*)*result);
1167 return NS_OK;
1168 }
1169
1170 static nsrefcnt NSAPI nsWeakReference_AddRef(nsIWeakReference *iface)
1171 {
1172 nsWeakReference *This = impl_from_nsIWeakReference(iface);
1173 LONG ref = InterlockedIncrement(&This->ref);
1174
1175 TRACE("(%p) ref=%d\n", This, ref);
1176
1177 return ref;
1178 }
1179
1180 static nsrefcnt NSAPI nsWeakReference_Release(nsIWeakReference *iface)
1181 {
1182 nsWeakReference *This = impl_from_nsIWeakReference(iface);
1183 LONG ref = InterlockedIncrement(&This->ref);
1184
1185 TRACE("(%p) ref=%d\n", This, ref);
1186
1187 if(!ref) {
1188 assert(!This->nscontainer);
1189 heap_free(This);
1190 }
1191
1192 return ref;
1193 }
1194
1195 static nsresult NSAPI nsWeakReference_QueryReferent(nsIWeakReference *iface,
1196 const nsIID *riid, void **result)
1197 {
1198 nsWeakReference *This = impl_from_nsIWeakReference(iface);
1199
1200 if(!This->nscontainer)
1201 return NS_ERROR_NULL_POINTER;
1202
1203 return nsIWebBrowserChrome_QueryInterface(&This->nscontainer->nsIWebBrowserChrome_iface, riid, result);
1204 }
1205
1206 static const nsIWeakReferenceVtbl nsWeakReferenceVtbl = {
1207 nsWeakReference_QueryInterface,
1208 nsWeakReference_AddRef,
1209 nsWeakReference_Release,
1210 nsWeakReference_QueryReferent
1211 };
1212
1213 /**********************************************************
1214 * nsIWebBrowserChrome interface
1215 */
1216
1217 static inline NSContainer *impl_from_nsIWebBrowserChrome(nsIWebBrowserChrome *iface)
1218 {
1219 return CONTAINING_RECORD(iface, NSContainer, nsIWebBrowserChrome_iface);
1220 }
1221
1222 static nsresult NSAPI nsWebBrowserChrome_QueryInterface(nsIWebBrowserChrome *iface,
1223 nsIIDRef riid, void **result)
1224 {
1225 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1226
1227 *result = NULL;
1228 if(IsEqualGUID(&IID_nsISupports, riid)) {
1229 TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
1230 *result = &This->nsIWebBrowserChrome_iface;
1231 }else if(IsEqualGUID(&IID_nsIWebBrowserChrome, riid)) {
1232 TRACE("(%p)->(IID_nsIWebBrowserChrome, %p)\n", This, result);
1233 *result = &This->nsIWebBrowserChrome_iface;
1234 }else if(IsEqualGUID(&IID_nsIContextMenuListener, riid)) {
1235 TRACE("(%p)->(IID_nsIContextMenuListener, %p)\n", This, result);
1236 *result = &This->nsIContextMenuListener_iface;
1237 }else if(IsEqualGUID(&IID_nsIURIContentListener, riid)) {
1238 TRACE("(%p)->(IID_nsIURIContentListener %p)\n", This, result);
1239 *result = &This->nsIURIContentListener_iface;
1240 }else if(IsEqualGUID(&IID_nsIEmbeddingSiteWindow, riid)) {
1241 TRACE("(%p)->(IID_nsIEmbeddingSiteWindow %p)\n", This, result);
1242 *result = &This->nsIEmbeddingSiteWindow_iface;
1243 }else if(IsEqualGUID(&IID_nsITooltipListener, riid)) {
1244 TRACE("(%p)->(IID_nsITooltipListener %p)\n", This, result);
1245 *result = &This->nsITooltipListener_iface;
1246 }else if(IsEqualGUID(&IID_nsIInterfaceRequestor, riid)) {
1247 TRACE("(%p)->(IID_nsIInterfaceRequestor %p)\n", This, result);
1248 *result = &This->nsIInterfaceRequestor_iface;
1249 }else if(IsEqualGUID(&IID_nsISupportsWeakReference, riid)) {
1250 TRACE("(%p)->(IID_nsISupportsWeakReference %p)\n", This, result);
1251 *result = &This->nsISupportsWeakReference_iface;
1252 }
1253
1254 if(*result) {
1255 nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1256 return NS_OK;
1257 }
1258
1259 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
1260 return NS_NOINTERFACE;
1261 }
1262
1263 static nsrefcnt NSAPI nsWebBrowserChrome_AddRef(nsIWebBrowserChrome *iface)
1264 {
1265 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1266 LONG ref = InterlockedIncrement(&This->ref);
1267
1268 TRACE("(%p) ref=%d\n", This, ref);
1269
1270 return ref;
1271 }
1272
1273 static nsrefcnt NSAPI nsWebBrowserChrome_Release(nsIWebBrowserChrome *iface)
1274 {
1275 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1276 LONG ref = InterlockedDecrement(&This->ref);
1277
1278 TRACE("(%p) ref=%d\n", This, ref);
1279
1280 if(!ref) {
1281 if(This->parent)
1282 nsIWebBrowserChrome_Release(&This->parent->nsIWebBrowserChrome_iface);
1283 if(This->weak_reference) {
1284 This->weak_reference->nscontainer = NULL;
1285 nsIWeakReference_Release(&This->weak_reference->nsIWeakReference_iface);
1286 }
1287 heap_free(This);
1288 }
1289
1290 return ref;
1291 }
1292
1293 static nsresult NSAPI nsWebBrowserChrome_SetStatus(nsIWebBrowserChrome *iface,
1294 UINT32 statusType, const PRUnichar *status)
1295 {
1296 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1297 TRACE("(%p)->(%d %s)\n", This, statusType, debugstr_w(status));
1298 return NS_OK;
1299 }
1300
1301 static nsresult NSAPI nsWebBrowserChrome_GetWebBrowser(nsIWebBrowserChrome *iface,
1302 nsIWebBrowser **aWebBrowser)
1303 {
1304 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1305
1306 TRACE("(%p)->(%p)\n", This, aWebBrowser);
1307
1308 if(!aWebBrowser)
1309 return NS_ERROR_INVALID_ARG;
1310
1311 if(This->webbrowser)
1312 nsIWebBrowser_AddRef(This->webbrowser);
1313 *aWebBrowser = This->webbrowser;
1314 return S_OK;
1315 }
1316
1317 static nsresult NSAPI nsWebBrowserChrome_SetWebBrowser(nsIWebBrowserChrome *iface,
1318 nsIWebBrowser *aWebBrowser)
1319 {
1320 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1321
1322 TRACE("(%p)->(%p)\n", This, aWebBrowser);
1323
1324 if(aWebBrowser != This->webbrowser)
1325 ERR("Wrong nsWebBrowser!\n");
1326
1327 return NS_OK;
1328 }
1329
1330 static nsresult NSAPI nsWebBrowserChrome_GetChromeFlags(nsIWebBrowserChrome *iface,
1331 UINT32 *aChromeFlags)
1332 {
1333 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1334 WARN("(%p)->(%p)\n", This, aChromeFlags);
1335 return NS_ERROR_NOT_IMPLEMENTED;
1336 }
1337
1338 static nsresult NSAPI nsWebBrowserChrome_SetChromeFlags(nsIWebBrowserChrome *iface,
1339 UINT32 aChromeFlags)
1340 {
1341 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1342 WARN("(%p)->(%08x)\n", This, aChromeFlags);
1343 return NS_ERROR_NOT_IMPLEMENTED;
1344 }
1345
1346 static nsresult NSAPI nsWebBrowserChrome_DestroyBrowserWindow(nsIWebBrowserChrome *iface)
1347 {
1348 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1349 TRACE("(%p)\n", This);
1350 return NS_ERROR_NOT_IMPLEMENTED;
1351 }
1352
1353 static nsresult NSAPI nsWebBrowserChrome_SizeBrowserTo(nsIWebBrowserChrome *iface,
1354 LONG aCX, LONG aCY)
1355 {
1356 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1357 WARN("(%p)->(%d %d)\n", This, aCX, aCY);
1358 return NS_ERROR_NOT_IMPLEMENTED;
1359 }
1360
1361 static nsresult NSAPI nsWebBrowserChrome_ShowAsModal(nsIWebBrowserChrome *iface)
1362 {
1363 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1364 WARN("(%p)\n", This);
1365 return NS_ERROR_NOT_IMPLEMENTED;
1366 }
1367
1368 static nsresult NSAPI nsWebBrowserChrome_IsWindowModal(nsIWebBrowserChrome *iface, cpp_bool *_retval)
1369 {
1370 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1371 WARN("(%p)->(%p)\n", This, _retval);
1372 return NS_ERROR_NOT_IMPLEMENTED;
1373 }
1374
1375 static nsresult NSAPI nsWebBrowserChrome_ExitModalEventLoop(nsIWebBrowserChrome *iface,
1376 nsresult aStatus)
1377 {
1378 NSContainer *This = impl_from_nsIWebBrowserChrome(iface);
1379 WARN("(%p)->(%08x)\n", This, aStatus);
1380 return NS_ERROR_NOT_IMPLEMENTED;
1381 }
1382
1383 static const nsIWebBrowserChromeVtbl nsWebBrowserChromeVtbl = {
1384 nsWebBrowserChrome_QueryInterface,
1385 nsWebBrowserChrome_AddRef,
1386 nsWebBrowserChrome_Release,
1387 nsWebBrowserChrome_SetStatus,
1388 nsWebBrowserChrome_GetWebBrowser,
1389 nsWebBrowserChrome_SetWebBrowser,
1390 nsWebBrowserChrome_GetChromeFlags,
1391 nsWebBrowserChrome_SetChromeFlags,
1392 nsWebBrowserChrome_DestroyBrowserWindow,
1393 nsWebBrowserChrome_SizeBrowserTo,
1394 nsWebBrowserChrome_ShowAsModal,
1395 nsWebBrowserChrome_IsWindowModal,
1396 nsWebBrowserChrome_ExitModalEventLoop
1397 };
1398
1399 /**********************************************************
1400 * nsIContextMenuListener interface
1401 */
1402
1403 static inline NSContainer *impl_from_nsIContextMenuListener(nsIContextMenuListener *iface)
1404 {
1405 return CONTAINING_RECORD(iface, NSContainer, nsIContextMenuListener_iface);
1406 }
1407
1408 static nsresult NSAPI nsContextMenuListener_QueryInterface(nsIContextMenuListener *iface,
1409 nsIIDRef riid, void **result)
1410 {
1411 NSContainer *This = impl_from_nsIContextMenuListener(iface);
1412 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1413 }
1414
1415 static nsrefcnt NSAPI nsContextMenuListener_AddRef(nsIContextMenuListener *iface)
1416 {
1417 NSContainer *This = impl_from_nsIContextMenuListener(iface);
1418 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1419 }
1420
1421 static nsrefcnt NSAPI nsContextMenuListener_Release(nsIContextMenuListener *iface)
1422 {
1423 NSContainer *This = impl_from_nsIContextMenuListener(iface);
1424 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
1425 }
1426
1427 static nsresult NSAPI nsContextMenuListener_OnShowContextMenu(nsIContextMenuListener *iface,
1428 UINT32 aContextFlags, nsIDOMEvent *aEvent, nsIDOMNode *aNode)
1429 {
1430 NSContainer *This = impl_from_nsIContextMenuListener(iface);
1431 nsIDOMMouseEvent *event;
1432 HTMLDOMNode *node;
1433 POINT pt;
1434 DWORD dwID = CONTEXT_MENU_DEFAULT;
1435 nsresult nsres;
1436 HRESULT hres;
1437
1438 TRACE("(%p)->(%08x %p %p)\n", This, aContextFlags, aEvent, aNode);
1439
1440 fire_event(This->doc->basedoc.doc_node /* FIXME */, EVENTID_CONTEXTMENU, TRUE, aNode, aEvent, NULL);
1441
1442 nsres = nsIDOMEvent_QueryInterface(aEvent, &IID_nsIDOMMouseEvent, (void**)&event);
1443 if(NS_FAILED(nsres)) {
1444 ERR("Could not get nsIDOMMouseEvent interface: %08x\n", nsres);
1445 return nsres;
1446 }
1447
1448 nsIDOMMouseEvent_GetScreenX(event, &pt.x);
1449 nsIDOMMouseEvent_GetScreenY(event, &pt.y);
1450 nsIDOMMouseEvent_Release(event);
1451
1452 switch(aContextFlags) {
1453 case CONTEXT_NONE:
1454 case CONTEXT_DOCUMENT:
1455 case CONTEXT_TEXT:
1456 dwID = CONTEXT_MENU_DEFAULT;
1457 break;
1458 case CONTEXT_IMAGE:
1459 case CONTEXT_IMAGE|CONTEXT_LINK:
1460 dwID = CONTEXT_MENU_IMAGE;
1461 break;
1462 case CONTEXT_LINK:
1463 dwID = CONTEXT_MENU_ANCHOR;
1464 break;
1465 case CONTEXT_INPUT:
1466 dwID = CONTEXT_MENU_CONTROL;
1467 break;
1468 default:
1469 FIXME("aContextFlags=%08x\n", aContextFlags);
1470 };
1471
1472 hres = get_node(This->doc->basedoc.doc_node, aNode, TRUE, &node);
1473 if(FAILED(hres))
1474 return NS_ERROR_FAILURE;
1475
1476 show_context_menu(This->doc, dwID, &pt, (IDispatch*)&node->IHTMLDOMNode_iface);
1477 node_release(node);
1478 return NS_OK;
1479 }
1480
1481 static const nsIContextMenuListenerVtbl nsContextMenuListenerVtbl = {
1482 nsContextMenuListener_QueryInterface,
1483 nsContextMenuListener_AddRef,
1484 nsContextMenuListener_Release,
1485 nsContextMenuListener_OnShowContextMenu
1486 };
1487
1488 /**********************************************************
1489 * nsIURIContentListener interface
1490 */
1491
1492 static inline NSContainer *impl_from_nsIURIContentListener(nsIURIContentListener *iface)
1493 {
1494 return CONTAINING_RECORD(iface, NSContainer, nsIURIContentListener_iface);
1495 }
1496
1497 static nsresult NSAPI nsURIContentListener_QueryInterface(nsIURIContentListener *iface,
1498 nsIIDRef riid, void **result)
1499 {
1500 NSContainer *This = impl_from_nsIURIContentListener(iface);
1501 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1502 }
1503
1504 static nsrefcnt NSAPI nsURIContentListener_AddRef(nsIURIContentListener *iface)
1505 {
1506 NSContainer *This = impl_from_nsIURIContentListener(iface);
1507 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1508 }
1509
1510 static nsrefcnt NSAPI nsURIContentListener_Release(nsIURIContentListener *iface)
1511 {
1512 NSContainer *This = impl_from_nsIURIContentListener(iface);
1513 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
1514 }
1515
1516 static nsresult NSAPI nsURIContentListener_OnStartURIOpen(nsIURIContentListener *iface,
1517 nsIURI *aURI, cpp_bool *_retval)
1518 {
1519 NSContainer *This = impl_from_nsIURIContentListener(iface);
1520 nsACString spec_str;
1521 const char *spec;
1522 nsresult nsres;
1523
1524 nsACString_Init(&spec_str, NULL);
1525 nsIURI_GetSpec(aURI, &spec_str);
1526 nsACString_GetData(&spec_str, &spec);
1527
1528 TRACE("(%p)->(%p(%s) %p)\n", This, aURI, debugstr_a(spec), _retval);
1529
1530 nsACString_Finish(&spec_str);
1531
1532 nsres = on_start_uri_open(This, aURI, _retval);
1533 if(NS_FAILED(nsres))
1534 return nsres;
1535
1536 return !*_retval && This->content_listener
1537 ? nsIURIContentListener_OnStartURIOpen(This->content_listener, aURI, _retval)
1538 : NS_OK;
1539 }
1540
1541 static nsresult NSAPI nsURIContentListener_DoContent(nsIURIContentListener *iface,
1542 const char *aContentType, cpp_bool aIsContentPreferred, nsIRequest *aRequest,
1543 nsIStreamListener **aContentHandler, cpp_bool *_retval)
1544 {
1545 NSContainer *This = impl_from_nsIURIContentListener(iface);
1546
1547 TRACE("(%p)->(%s %x %p %p %p)\n", This, debugstr_a(aContentType), aIsContentPreferred,
1548 aRequest, aContentHandler, _retval);
1549
1550 return This->content_listener
1551 ? nsIURIContentListener_DoContent(This->content_listener, aContentType,
1552 aIsContentPreferred, aRequest, aContentHandler, _retval)
1553 : NS_ERROR_NOT_IMPLEMENTED;
1554 }
1555
1556 static nsresult NSAPI nsURIContentListener_IsPreferred(nsIURIContentListener *iface,
1557 const char *aContentType, char **aDesiredContentType, cpp_bool *_retval)
1558 {
1559 NSContainer *This = impl_from_nsIURIContentListener(iface);
1560
1561 TRACE("(%p)->(%s %p %p)\n", This, debugstr_a(aContentType), aDesiredContentType, _retval);
1562
1563 /* FIXME: Should we do something here? */
1564 *_retval = TRUE;
1565
1566 return This->content_listener
1567 ? nsIURIContentListener_IsPreferred(This->content_listener, aContentType,
1568 aDesiredContentType, _retval)
1569 : NS_OK;
1570 }
1571
1572 static nsresult NSAPI nsURIContentListener_CanHandleContent(nsIURIContentListener *iface,
1573 const char *aContentType, cpp_bool aIsContentPreferred, char **aDesiredContentType,
1574 cpp_bool *_retval)
1575 {
1576 NSContainer *This = impl_from_nsIURIContentListener(iface);
1577
1578 TRACE("(%p)->(%s %x %p %p)\n", This, debugstr_a(aContentType), aIsContentPreferred,
1579 aDesiredContentType, _retval);
1580
1581 return This->content_listener
1582 ? nsIURIContentListener_CanHandleContent(This->content_listener, aContentType,
1583 aIsContentPreferred, aDesiredContentType, _retval)
1584 : NS_ERROR_NOT_IMPLEMENTED;
1585 }
1586
1587 static nsresult NSAPI nsURIContentListener_GetLoadCookie(nsIURIContentListener *iface,
1588 nsISupports **aLoadCookie)
1589 {
1590 NSContainer *This = impl_from_nsIURIContentListener(iface);
1591
1592 WARN("(%p)->(%p)\n", This, aLoadCookie);
1593
1594 return This->content_listener
1595 ? nsIURIContentListener_GetLoadCookie(This->content_listener, aLoadCookie)
1596 : NS_ERROR_NOT_IMPLEMENTED;
1597 }
1598
1599 static nsresult NSAPI nsURIContentListener_SetLoadCookie(nsIURIContentListener *iface,
1600 nsISupports *aLoadCookie)
1601 {
1602 NSContainer *This = impl_from_nsIURIContentListener(iface);
1603
1604 WARN("(%p)->(%p)\n", This, aLoadCookie);
1605
1606 return This->content_listener
1607 ? nsIURIContentListener_SetLoadCookie(This->content_listener, aLoadCookie)
1608 : NS_ERROR_NOT_IMPLEMENTED;
1609 }
1610
1611 static nsresult NSAPI nsURIContentListener_GetParentContentListener(nsIURIContentListener *iface,
1612 nsIURIContentListener **aParentContentListener)
1613 {
1614 NSContainer *This = impl_from_nsIURIContentListener(iface);
1615
1616 TRACE("(%p)->(%p)\n", This, aParentContentListener);
1617
1618 if(This->content_listener)
1619 nsIURIContentListener_AddRef(This->content_listener);
1620
1621 *aParentContentListener = This->content_listener;
1622 return NS_OK;
1623 }
1624
1625 static nsresult NSAPI nsURIContentListener_SetParentContentListener(nsIURIContentListener *iface,
1626 nsIURIContentListener *aParentContentListener)
1627 {
1628 NSContainer *This = impl_from_nsIURIContentListener(iface);
1629
1630 TRACE("(%p)->(%p)\n", This, aParentContentListener);
1631
1632 if(aParentContentListener == &This->nsIURIContentListener_iface)
1633 return NS_OK;
1634
1635 if(This->content_listener)
1636 nsIURIContentListener_Release(This->content_listener);
1637
1638 This->content_listener = aParentContentListener;
1639 if(This->content_listener)
1640 nsIURIContentListener_AddRef(This->content_listener);
1641
1642 return NS_OK;
1643 }
1644
1645 static const nsIURIContentListenerVtbl nsURIContentListenerVtbl = {
1646 nsURIContentListener_QueryInterface,
1647 nsURIContentListener_AddRef,
1648 nsURIContentListener_Release,
1649 nsURIContentListener_OnStartURIOpen,
1650 nsURIContentListener_DoContent,
1651 nsURIContentListener_IsPreferred,
1652 nsURIContentListener_CanHandleContent,
1653 nsURIContentListener_GetLoadCookie,
1654 nsURIContentListener_SetLoadCookie,
1655 nsURIContentListener_GetParentContentListener,
1656 nsURIContentListener_SetParentContentListener
1657 };
1658
1659 /**********************************************************
1660 * nsIEmbeddinSiteWindow interface
1661 */
1662
1663 static inline NSContainer *impl_from_nsIEmbeddingSiteWindow(nsIEmbeddingSiteWindow *iface)
1664 {
1665 return CONTAINING_RECORD(iface, NSContainer, nsIEmbeddingSiteWindow_iface);
1666 }
1667
1668 static nsresult NSAPI nsEmbeddingSiteWindow_QueryInterface(nsIEmbeddingSiteWindow *iface,
1669 nsIIDRef riid, void **result)
1670 {
1671 NSContainer *This = impl_from_nsIEmbeddingSiteWindow(iface);
1672 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1673 }
1674
1675 static nsrefcnt NSAPI nsEmbeddingSiteWindow_AddRef(nsIEmbeddingSiteWindow *iface)
1676 {
1677 NSContainer *This = impl_from_nsIEmbeddingSiteWindow(iface);
1678 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1679 }
1680
1681 static nsrefcnt NSAPI nsEmbeddingSiteWindow_Release(nsIEmbeddingSiteWindow *iface)
1682 {
1683 NSContainer *This = impl_from_nsIEmbeddingSiteWindow(iface);
1684 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
1685 }
1686
1687 static nsresult NSAPI nsEmbeddingSiteWindow_SetDimensions(nsIEmbeddingSiteWindow *iface,
1688 UINT32 flags, LONG x, LONG y, LONG cx, LONG cy)
1689 {
1690 NSContainer *This = impl_from_nsIEmbeddingSiteWindow(iface);
1691 WARN("(%p)->(%08x %d %d %d %d)\n", This, flags, x, y, cx, cy);
1692 return NS_ERROR_NOT_IMPLEMENTED;
1693 }
1694
1695 static nsresult NSAPI nsEmbeddingSiteWindow_GetDimensions(nsIEmbeddingSiteWindow *iface,
1696 UINT32 flags, LONG *x, LONG *y, LONG *cx, LONG *cy)
1697 {
1698 NSContainer *This = impl_from_nsIEmbeddingSiteWindow(iface);
1699 WARN("(%p)->(%08x %p %p %p %p)\n", This, flags, x, y, cx, cy);
1700 return NS_ERROR_NOT_IMPLEMENTED;
1701 }
1702
1703 static nsresult NSAPI nsEmbeddingSiteWindow_SetFocus(nsIEmbeddingSiteWindow *iface)
1704 {
1705 NSContainer *This = impl_from_nsIEmbeddingSiteWindow(iface);
1706
1707 TRACE("(%p)\n", This);
1708
1709 return nsIBaseWindow_SetFocus(This->window);
1710 }
1711
1712 static nsresult NSAPI nsEmbeddingSiteWindow_GetVisibility(nsIEmbeddingSiteWindow *iface,
1713 cpp_bool *aVisibility)
1714 {
1715 NSContainer *This = impl_from_nsIEmbeddingSiteWindow(iface);
1716
1717 TRACE("(%p)->(%p)\n", This, aVisibility);
1718
1719 *aVisibility = This->doc && This->doc->hwnd && IsWindowVisible(This->doc->hwnd);
1720 return NS_OK;
1721 }
1722
1723 static nsresult NSAPI nsEmbeddingSiteWindow_SetVisibility(nsIEmbeddingSiteWindow *iface,
1724 cpp_bool aVisibility)
1725 {
1726 NSContainer *This = impl_from_nsIEmbeddingSiteWindow(iface);
1727
1728 TRACE("(%p)->(%x)\n", This, aVisibility);
1729
1730 return NS_OK;
1731 }
1732
1733 static nsresult NSAPI nsEmbeddingSiteWindow_GetTitle(nsIEmbeddingSiteWindow *iface,
1734 PRUnichar **aTitle)
1735 {
1736 NSContainer *This = impl_from_nsIEmbeddingSiteWindow(iface);
1737 WARN("(%p)->(%p)\n", This, aTitle);
1738 return NS_ERROR_NOT_IMPLEMENTED;
1739 }
1740
1741 static nsresult NSAPI nsEmbeddingSiteWindow_SetTitle(nsIEmbeddingSiteWindow *iface,
1742 const PRUnichar *aTitle)
1743 {
1744 NSContainer *This = impl_from_nsIEmbeddingSiteWindow(iface);
1745 WARN("(%p)->(%s)\n", This, debugstr_w(aTitle));
1746 return NS_ERROR_NOT_IMPLEMENTED;
1747 }
1748
1749 static nsresult NSAPI nsEmbeddingSiteWindow_GetSiteWindow(nsIEmbeddingSiteWindow *iface,
1750 void **aSiteWindow)
1751 {
1752 NSContainer *This = impl_from_nsIEmbeddingSiteWindow(iface);
1753
1754 TRACE("(%p)->(%p)\n", This, aSiteWindow);
1755
1756 *aSiteWindow = This->hwnd;
1757 return NS_OK;
1758 }
1759
1760 static const nsIEmbeddingSiteWindowVtbl nsEmbeddingSiteWindowVtbl = {
1761 nsEmbeddingSiteWindow_QueryInterface,
1762 nsEmbeddingSiteWindow_AddRef,
1763 nsEmbeddingSiteWindow_Release,
1764 nsEmbeddingSiteWindow_SetDimensions,
1765 nsEmbeddingSiteWindow_GetDimensions,
1766 nsEmbeddingSiteWindow_SetFocus,
1767 nsEmbeddingSiteWindow_GetVisibility,
1768 nsEmbeddingSiteWindow_SetVisibility,
1769 nsEmbeddingSiteWindow_GetTitle,
1770 nsEmbeddingSiteWindow_SetTitle,
1771 nsEmbeddingSiteWindow_GetSiteWindow
1772 };
1773
1774 static inline NSContainer *impl_from_nsITooltipListener(nsITooltipListener *iface)
1775 {
1776 return CONTAINING_RECORD(iface, NSContainer, nsITooltipListener_iface);
1777 }
1778
1779 static nsresult NSAPI nsTooltipListener_QueryInterface(nsITooltipListener *iface, nsIIDRef riid,
1780 void **result)
1781 {
1782 NSContainer *This = impl_from_nsITooltipListener(iface);
1783 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1784 }
1785
1786 static nsrefcnt NSAPI nsTooltipListener_AddRef(nsITooltipListener *iface)
1787 {
1788 NSContainer *This = impl_from_nsITooltipListener(iface);
1789 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1790 }
1791
1792 static nsrefcnt NSAPI nsTooltipListener_Release(nsITooltipListener *iface)
1793 {
1794 NSContainer *This = impl_from_nsITooltipListener(iface);
1795 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
1796 }
1797
1798 static nsresult NSAPI nsTooltipListener_OnShowTooltip(nsITooltipListener *iface,
1799 LONG aXCoord, LONG aYCoord, const PRUnichar *aTipText)
1800 {
1801 NSContainer *This = impl_from_nsITooltipListener(iface);
1802
1803 if (This->doc)
1804 show_tooltip(This->doc, aXCoord, aYCoord, aTipText);
1805
1806 return NS_OK;
1807 }
1808
1809 static nsresult NSAPI nsTooltipListener_OnHideTooltip(nsITooltipListener *iface)
1810 {
1811 NSContainer *This = impl_from_nsITooltipListener(iface);
1812
1813 if (This->doc)
1814 hide_tooltip(This->doc);
1815
1816 return NS_OK;
1817 }
1818
1819 static const nsITooltipListenerVtbl nsTooltipListenerVtbl = {
1820 nsTooltipListener_QueryInterface,
1821 nsTooltipListener_AddRef,
1822 nsTooltipListener_Release,
1823 nsTooltipListener_OnShowTooltip,
1824 nsTooltipListener_OnHideTooltip
1825 };
1826
1827 static inline NSContainer *impl_from_nsIInterfaceRequestor(nsIInterfaceRequestor *iface)
1828 {
1829 return CONTAINING_RECORD(iface, NSContainer, nsIInterfaceRequestor_iface);
1830 }
1831
1832 static nsresult NSAPI nsInterfaceRequestor_QueryInterface(nsIInterfaceRequestor *iface,
1833 nsIIDRef riid, void **result)
1834 {
1835 NSContainer *This = impl_from_nsIInterfaceRequestor(iface);
1836 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1837 }
1838
1839 static nsrefcnt NSAPI nsInterfaceRequestor_AddRef(nsIInterfaceRequestor *iface)
1840 {
1841 NSContainer *This = impl_from_nsIInterfaceRequestor(iface);
1842 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1843 }
1844
1845 static nsrefcnt NSAPI nsInterfaceRequestor_Release(nsIInterfaceRequestor *iface)
1846 {
1847 NSContainer *This = impl_from_nsIInterfaceRequestor(iface);
1848 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
1849 }
1850
1851 static nsresult NSAPI nsInterfaceRequestor_GetInterface(nsIInterfaceRequestor *iface,
1852 nsIIDRef riid, void **result)
1853 {
1854 NSContainer *This = impl_from_nsIInterfaceRequestor(iface);
1855
1856 if(IsEqualGUID(&IID_nsIDOMWindow, riid)) {
1857 TRACE("(%p)->(IID_nsIDOMWindow %p)\n", This, result);
1858 return nsIWebBrowser_GetContentDOMWindow(This->webbrowser, (nsIDOMWindow**)result);
1859 }
1860
1861 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1862 }
1863
1864 static const nsIInterfaceRequestorVtbl nsInterfaceRequestorVtbl = {
1865 nsInterfaceRequestor_QueryInterface,
1866 nsInterfaceRequestor_AddRef,
1867 nsInterfaceRequestor_Release,
1868 nsInterfaceRequestor_GetInterface
1869 };
1870
1871 static inline NSContainer *impl_from_nsISupportsWeakReference(nsISupportsWeakReference *iface)
1872 {
1873 return CONTAINING_RECORD(iface, NSContainer, nsISupportsWeakReference_iface);
1874 }
1875
1876 static nsresult NSAPI nsSupportsWeakReference_QueryInterface(nsISupportsWeakReference *iface,
1877 nsIIDRef riid, void **result)
1878 {
1879 NSContainer *This = impl_from_nsISupportsWeakReference(iface);
1880 return nsIWebBrowserChrome_QueryInterface(&This->nsIWebBrowserChrome_iface, riid, result);
1881 }
1882
1883 static nsrefcnt NSAPI nsSupportsWeakReference_AddRef(nsISupportsWeakReference *iface)
1884 {
1885 NSContainer *This = impl_from_nsISupportsWeakReference(iface);
1886 return nsIWebBrowserChrome_AddRef(&This->nsIWebBrowserChrome_iface);
1887 }
1888
1889 static nsrefcnt NSAPI nsSupportsWeakReference_Release(nsISupportsWeakReference *iface)
1890 {
1891 NSContainer *This = impl_from_nsISupportsWeakReference(iface);
1892 return nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
1893 }
1894
1895 static nsresult NSAPI nsSupportsWeakReference_GetWeakReference(nsISupportsWeakReference *iface,
1896 nsIWeakReference **_retval)
1897 {
1898 NSContainer *This = impl_from_nsISupportsWeakReference(iface);
1899
1900 TRACE("(%p)->(%p)\n", This, _retval);
1901
1902 if(!This->weak_reference) {
1903 This->weak_reference = heap_alloc(sizeof(nsWeakReference));
1904 if(!This->weak_reference)
1905 return NS_ERROR_OUT_OF_MEMORY;
1906
1907 This->weak_reference->nsIWeakReference_iface.lpVtbl = &nsWeakReferenceVtbl;
1908 This->weak_reference->ref = 1;
1909 This->weak_reference->nscontainer = This;
1910 }
1911
1912 *_retval = &This->weak_reference->nsIWeakReference_iface;
1913 nsIWeakReference_AddRef(*_retval);
1914 return NS_OK;
1915 }
1916
1917 static const nsISupportsWeakReferenceVtbl nsSupportsWeakReferenceVtbl = {
1918 nsSupportsWeakReference_QueryInterface,
1919 nsSupportsWeakReference_AddRef,
1920 nsSupportsWeakReference_Release,
1921 nsSupportsWeakReference_GetWeakReference
1922 };
1923
1924 static HRESULT init_nscontainer(NSContainer *nscontainer)
1925 {
1926 nsIWebBrowserSetup *wbsetup;
1927 nsIScrollable *scrollable;
1928 nsresult nsres;
1929
1930 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_WEBBROWSER_CONTRACTID,
1931 NULL, &IID_nsIWebBrowser, (void**)&nscontainer->webbrowser);
1932 if(NS_FAILED(nsres)) {
1933 ERR("Creating WebBrowser failed: %08x\n", nsres);
1934 return E_FAIL;
1935 }
1936
1937 nsres = nsIWebBrowser_SetContainerWindow(nscontainer->webbrowser, &nscontainer->nsIWebBrowserChrome_iface);
1938 if(NS_FAILED(nsres)) {
1939 ERR("SetContainerWindow failed: %08x\n", nsres);
1940 return E_FAIL;
1941 }
1942
1943 nsres = nsIWebBrowser_QueryInterface(nscontainer->webbrowser, &IID_nsIBaseWindow,
1944 (void**)&nscontainer->window);
1945 if(NS_FAILED(nsres)) {
1946 ERR("Could not get nsIBaseWindow interface: %08x\n", nsres);
1947 return E_FAIL;
1948 }
1949
1950 nsres = nsIWebBrowser_QueryInterface(nscontainer->webbrowser, &IID_nsIWebBrowserSetup,
1951 (void**)&wbsetup);
1952 if(NS_SUCCEEDED(nsres)) {
1953 nsres = nsIWebBrowserSetup_SetProperty(wbsetup, SETUP_IS_CHROME_WRAPPER, FALSE);
1954 nsIWebBrowserSetup_Release(wbsetup);
1955 if(NS_FAILED(nsres)) {
1956 ERR("SetProperty(SETUP_IS_CHROME_WRAPPER) failed: %08x\n", nsres);
1957 return E_FAIL;
1958 }
1959 }else {
1960 ERR("Could not get nsIWebBrowserSetup interface\n");
1961 return E_FAIL;
1962 }
1963
1964 nsres = nsIWebBrowser_QueryInterface(nscontainer->webbrowser, &IID_nsIWebNavigation,
1965 (void**)&nscontainer->navigation);
1966 if(NS_FAILED(nsres)) {
1967 ERR("Could not get nsIWebNavigation interface: %08x\n", nsres);
1968 return E_FAIL;
1969 }
1970
1971 nsres = nsIWebBrowser_QueryInterface(nscontainer->webbrowser, &IID_nsIWebBrowserFocus,
1972 (void**)&nscontainer->focus);
1973 if(NS_FAILED(nsres)) {
1974 ERR("Could not get nsIWebBrowserFocus interface: %08x\n", nsres);
1975 return E_FAIL;
1976 }
1977
1978 if(!nscontainer_class) {
1979 register_nscontainer_class();
1980 if(!nscontainer_class)
1981 return E_FAIL;
1982 }
1983
1984 nscontainer->hwnd = CreateWindowExW(0, wszNsContainer, NULL,
1985 WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 0, 0, 100, 100,
1986 GetDesktopWindow(), NULL, hInst, nscontainer);
1987 if(!nscontainer->hwnd) {
1988 WARN("Could not create window\n");
1989 return E_FAIL;
1990 }
1991
1992 nsres = nsIBaseWindow_InitWindow(nscontainer->window, nscontainer->hwnd, NULL, 0, 0, 100, 100);
1993 if(NS_SUCCEEDED(nsres)) {
1994 nsres = nsIBaseWindow_Create(nscontainer->window);
1995 if(NS_FAILED(nsres)) {
1996 WARN("Creating window failed: %08x\n", nsres);
1997 return E_FAIL;
1998 }
1999
2000 nsIBaseWindow_SetVisibility(nscontainer->window, FALSE);
2001 nsIBaseWindow_SetEnabled(nscontainer->window, FALSE);
2002 }else {
2003 ERR("InitWindow failed: %08x\n", nsres);
2004 return E_FAIL;
2005 }
2006
2007 nsres = nsIWebBrowser_SetParentURIContentListener(nscontainer->webbrowser,
2008 &nscontainer->nsIURIContentListener_iface);
2009 if(NS_FAILED(nsres))
2010 ERR("SetParentURIContentListener failed: %08x\n", nsres);
2011
2012 nsres = nsIWebBrowser_QueryInterface(nscontainer->webbrowser, &IID_nsIScrollable, (void**)&scrollable);
2013 if(NS_SUCCEEDED(nsres)) {
2014 nsres = nsIScrollable_SetDefaultScrollbarPreferences(scrollable,
2015 ScrollOrientation_Y, Scrollbar_Always);
2016 if(NS_FAILED(nsres))
2017 ERR("Could not set default Y scrollbar prefs: %08x\n", nsres);
2018
2019 nsres = nsIScrollable_SetDefaultScrollbarPreferences(scrollable,
2020 ScrollOrientation_X, Scrollbar_Auto);
2021 if(NS_FAILED(nsres))
2022 ERR("Could not set default X scrollbar prefs: %08x\n", nsres);
2023
2024 nsIScrollable_Release(scrollable);
2025 }else {
2026 ERR("Could not get nsIScrollable: %08x\n", nsres);
2027 }
2028
2029 return S_OK;
2030 }
2031
2032 HRESULT create_nscontainer(HTMLDocumentObj *doc, NSContainer **_ret)
2033 {
2034 NSContainer *ret;
2035 HRESULT hres;
2036
2037 if(!load_gecko())
2038 return CLASS_E_CLASSNOTAVAILABLE;
2039
2040 ret = heap_alloc_zero(sizeof(NSContainer));
2041 if(!ret)
2042 return E_OUTOFMEMORY;
2043
2044 ret->nsIWebBrowserChrome_iface.lpVtbl = &nsWebBrowserChromeVtbl;
2045 ret->nsIContextMenuListener_iface.lpVtbl = &nsContextMenuListenerVtbl;
2046 ret->nsIURIContentListener_iface.lpVtbl = &nsURIContentListenerVtbl;
2047 ret->nsIEmbeddingSiteWindow_iface.lpVtbl = &nsEmbeddingSiteWindowVtbl;
2048 ret->nsITooltipListener_iface.lpVtbl = &nsTooltipListenerVtbl;
2049 ret->nsIInterfaceRequestor_iface.lpVtbl = &nsInterfaceRequestorVtbl;
2050 ret->nsISupportsWeakReference_iface.lpVtbl = &nsSupportsWeakReferenceVtbl;
2051
2052 ret->doc = doc;
2053 ret->ref = 1;
2054
2055 hres = init_nscontainer(ret);
2056 if(SUCCEEDED(hres))
2057 *_ret = ret;
2058 else
2059 nsIWebBrowserChrome_Release(&ret->nsIWebBrowserChrome_iface);
2060 return hres;
2061 }
2062
2063 void NSContainer_Release(NSContainer *This)
2064 {
2065 TRACE("(%p)\n", This);
2066
2067 This->doc = NULL;
2068
2069 ShowWindow(This->hwnd, SW_HIDE);
2070 SetParent(This->hwnd, NULL);
2071
2072 nsIBaseWindow_SetVisibility(This->window, FALSE);
2073 nsIBaseWindow_Destroy(This->window);
2074
2075 nsIWebBrowser_SetContainerWindow(This->webbrowser, NULL);
2076
2077 nsIWebBrowser_Release(This->webbrowser);
2078 This->webbrowser = NULL;
2079
2080 nsIWebNavigation_Release(This->navigation);
2081 This->navigation = NULL;
2082
2083 nsIBaseWindow_Release(This->window);
2084 This->window = NULL;
2085
2086 nsIWebBrowserFocus_Release(This->focus);
2087 This->focus = NULL;
2088
2089 if(This->editor_controller) {
2090 nsIController_Release(This->editor_controller);
2091 This->editor_controller = NULL;
2092 }
2093
2094 if(This->editor) {
2095 nsIEditor_Release(This->editor);
2096 This->editor = NULL;
2097 }
2098
2099 if(This->content_listener) {
2100 nsIURIContentListener_Release(This->content_listener);
2101 This->content_listener = NULL;
2102 }
2103
2104 if(This->hwnd) {
2105 DestroyWindow(This->hwnd);
2106 This->hwnd = NULL;
2107 }
2108
2109 nsIWebBrowserChrome_Release(&This->nsIWebBrowserChrome_iface);
2110 }