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