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