1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is Mozilla Communicator client code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
23 * Pierre Phaneuf <pp@ludusdesign.com>
24 * Robert O'Callahan <roc+moz@cs.cmu.edu>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
41 #include "nsIAppShellService.h"
42 #include "nsISupportsArray.h"
43 #include "nsIComponentManager.h"
45 #include "nsNetUtil.h"
46 #include "nsIServiceManager.h"
47 #include "nsIObserverService.h"
48 #include "nsIObserver.h"
49 #include "nsIXPConnect.h"
50 #include "nsIJSContextStack.h"
51 #include "nsIPrefBranch.h"
52 #include "nsIPrefService.h"
54 #include "nsIWidget.h"
55 #include "nsIWindowMediator.h"
56 #include "nsIWindowWatcher.h"
57 #include "nsPIWindowWatcher.h"
58 #include "nsIDOMWindowInternal.h"
59 #include "nsWebShellWindow.h"
61 #include "nsIEnumerator.h"
63 #include "nsITimelineService.h"
66 #include "nsWidgetsCID.h"
67 #include "nsIRequestObserver.h"
69 /* For implementing GetHiddenWindowAndJSContext */
70 #include "nsIScriptGlobalObject.h"
71 #include "nsIScriptContext.h"
74 #include "nsAppShellService.h"
75 #include "nsISupportsPrimitives.h"
76 #include "nsIPlatformCharset.h"
77 #include "nsICharsetConverterManager.h"
78 #include "nsIUnicodeDecoder.h"
80 // Default URL for the hidden window, can be overridden by a pref on Mac
81 #define DEFAULT_HIDDENWINDOW_URL "resource://gre/res/hiddenWindow.html"
85 nsAppShellService::nsAppShellService() :
86 mXPCOMShuttingDown(PR_FALSE
),
88 mApplicationProvidedHiddenWindow(PR_FALSE
)
90 nsCOMPtr
<nsIObserverService
> obs
91 (do_GetService("@mozilla.org/observer-service;1"));
94 obs
->AddObserver(this, "xpcom-shutdown", PR_FALSE
);
97 nsAppShellService::~nsAppShellService()
103 * Implement the nsISupports methods...
105 NS_IMPL_ISUPPORTS2(nsAppShellService
,
110 nsAppShellService::SetXPConnectSafeContext()
114 nsCOMPtr
<nsIThreadJSContextStack
> cxstack
=
115 do_GetService("@mozilla.org/js/xpc/ContextStack;1", &rv
);
116 NS_ENSURE_SUCCESS(rv
, rv
);
118 nsCOMPtr
<nsIDOMWindowInternal
> junk
;
120 rv
= GetHiddenWindowAndJSContext(getter_AddRefs(junk
), &cx
);
121 NS_ENSURE_SUCCESS(rv
, rv
);
123 return cxstack
->SetSafeJSContext(cx
);
126 nsresult
nsAppShellService::ClearXPConnectSafeContext()
130 nsCOMPtr
<nsIThreadJSContextStack
> cxstack
=
131 do_GetService("@mozilla.org/js/xpc/ContextStack;1", &rv
);
133 NS_ERROR("XPConnect ContextStack gone before XPCOM shutdown?");
137 nsCOMPtr
<nsIDOMWindowInternal
> junk
;
139 rv
= GetHiddenWindowAndJSContext(getter_AddRefs(junk
), &cx
);
140 NS_ENSURE_SUCCESS(rv
, rv
);
143 rv
= cxstack
->GetSafeJSContext(&safe_cx
);
144 NS_ENSURE_SUCCESS(rv
, rv
);
147 rv
= cxstack
->SetSafeJSContext(nsnull
);
153 nsAppShellService::CreateHiddenWindow(nsIAppShell
* aAppShell
)
156 PRInt32 initialHeight
= 100, initialWidth
= 100;
159 PRUint32 chromeMask
= 0;
160 nsCOMPtr
<nsIPrefBranch
> prefBranch
;
161 nsCOMPtr
<nsIPrefService
> prefs
= do_GetService(NS_PREFSERVICE_CONTRACTID
);
162 prefs
->GetBranch(nsnull
, getter_AddRefs(prefBranch
));
163 nsXPIDLCString prefVal
;
164 rv
= prefBranch
->GetCharPref("browser.hiddenWindowChromeURL", getter_Copies(prefVal
));
165 const char* hiddenWindowURL
= prefVal
.get() ? prefVal
.get() : DEFAULT_HIDDENWINDOW_URL
;
166 mApplicationProvidedHiddenWindow
= prefVal
.get() ? PR_TRUE
: PR_FALSE
;
168 static const char hiddenWindowURL
[] = DEFAULT_HIDDENWINDOW_URL
;
169 PRUint32 chromeMask
= nsIWebBrowserChrome::CHROME_ALL
;
172 nsCOMPtr
<nsIURI
> url
;
173 rv
= NS_NewURI(getter_AddRefs(url
), hiddenWindowURL
);
174 NS_ENSURE_SUCCESS(rv
, rv
);
176 nsRefPtr
<nsWebShellWindow
> newWindow
;
177 rv
= JustCreateTopWindow(nsnull
, url
,
178 chromeMask
, initialWidth
, initialHeight
,
179 PR_TRUE
, aAppShell
, getter_AddRefs(newWindow
));
180 NS_ENSURE_SUCCESS(rv
, rv
);
182 mHiddenWindow
.swap(newWindow
);
185 // hide the hidden window by launching it into outer space. This
186 // way, we can keep it visible and let the OS send it activates
187 // to keep menus happy. This will cause it to show up in window
188 // lists under osx, but I think that's ok.
189 mHiddenWindow
->SetPosition ( -32000, -32000 );
190 mHiddenWindow
->SetVisibility ( PR_TRUE
);
193 // Set XPConnect's fallback JSContext (used for JS Components)
194 // to the DOM JSContext for this thread, so that DOM-to-XPConnect
195 // conversions get the JSContext private magic they need to
197 SetXPConnectSafeContext();
199 // RegisterTopLevelWindow(newWindow); -- Mac only
205 nsAppShellService::DestroyHiddenWindow()
208 ClearXPConnectSafeContext();
209 mHiddenWindow
->Destroy();
211 mHiddenWindow
= nsnull
;
218 * Create a new top level window and display the given URL within it...
221 nsAppShellService::CreateTopLevelWindow(nsIXULWindow
*aParent
,
223 PRUint32 aChromeMask
,
224 PRInt32 aInitialWidth
,
225 PRInt32 aInitialHeight
,
226 nsIAppShell
* aAppShell
,
227 nsIXULWindow
**aResult
)
232 nsWebShellWindow
*newWindow
= nsnull
;
233 rv
= JustCreateTopWindow(aParent
, aUrl
,
234 aChromeMask
, aInitialWidth
, aInitialHeight
,
235 PR_FALSE
, aAppShell
, &newWindow
); // addrefs
237 *aResult
= newWindow
; // transfer ref
239 if (NS_SUCCEEDED(rv
)) {
240 // the addref resulting from this is the owning addref for this window
241 RegisterTopLevelWindow(*aResult
);
242 (*aResult
)->SetZLevel(CalculateWindowZLevel(aParent
, aChromeMask
));
249 nsAppShellService::CalculateWindowZLevel(nsIXULWindow
*aParent
,
250 PRUint32 aChromeMask
)
254 zLevel
= nsIXULWindow::normalZ
;
255 if (aChromeMask
& nsIWebBrowserChrome::CHROME_WINDOW_RAISED
)
256 zLevel
= nsIXULWindow::raisedZ
;
257 else if (aChromeMask
& nsIWebBrowserChrome::CHROME_WINDOW_LOWERED
)
258 zLevel
= nsIXULWindow::loweredZ
;
261 /* Platforms on which modal windows are always application-modal, not
262 window-modal (that's just the Mac, right?) want modal windows to
263 be stacked on top of everyone else.
265 On Mac OS X, bind modality to parent window instead of app (ala Mac OS 9)
267 PRUint32 modalDepMask
= nsIWebBrowserChrome::CHROME_MODAL
|
268 nsIWebBrowserChrome::CHROME_DEPENDENT
;
269 if (aParent
&& (aChromeMask
& modalDepMask
)) {
270 aParent
->GetZLevel(&zLevel
);
273 /* Platforms with native support for dependent windows (that's everyone
274 but pre-Mac OS X, right?) know how to stack dependent windows. On these
275 platforms, give the dependent window the same level as its parent,
276 so we won't try to override the normal platform behaviour. */
277 if ((aChromeMask
& nsIWebBrowserChrome::CHROME_DEPENDENT
) && aParent
)
278 aParent
->GetZLevel(&zLevel
);
285 * Just do the window-making part of CreateTopLevelWindow
288 nsAppShellService::JustCreateTopWindow(nsIXULWindow
*aParent
,
290 PRUint32 aChromeMask
,
291 PRInt32 aInitialWidth
,
292 PRInt32 aInitialHeight
,
293 PRBool aIsHiddenWindow
,
294 nsIAppShell
* aAppShell
,
295 nsWebShellWindow
**aResult
)
299 nsRefPtr
<nsWebShellWindow
> window
= new nsWebShellWindow();
300 NS_ENSURE_TRUE(window
, NS_ERROR_OUT_OF_MEMORY
);
302 nsWidgetInitData widgetInitData
;
305 widgetInitData
.mWindowType
= eWindowType_invisible
;
307 widgetInitData
.mWindowType
= aChromeMask
& nsIWebBrowserChrome::CHROME_OPENAS_DIALOG
?
308 eWindowType_dialog
: eWindowType_toplevel
;
310 if (aChromeMask
& nsIWebBrowserChrome::CHROME_WINDOW_POPUP
)
311 widgetInitData
.mWindowType
= eWindowType_popup
;
314 // Mac OS X sheet support
315 // Adding CHROME_OPENAS_CHROME to sheetMask makes modal windows opened from
316 // nsGlobalWindow::ShowModalDialog() be dialogs (not sheets), while modal
317 // windows opened from nsPromptService::DoDialog() still are sheets. This
318 // fixes bmo bug 395465 (see nsCocoaWindow::StandardCreate() and
319 // nsCocoaWindow::SetModal()).
320 PRUint32 sheetMask
= nsIWebBrowserChrome::CHROME_OPENAS_DIALOG
|
321 nsIWebBrowserChrome::CHROME_MODAL
|
322 nsIWebBrowserChrome::CHROME_OPENAS_CHROME
;
323 if (aParent
&& ((aChromeMask
& sheetMask
) == sheetMask
))
324 widgetInitData
.mWindowType
= eWindowType_sheet
;
327 widgetInitData
.mContentType
= eContentTypeUI
;
329 // note default chrome overrides other OS chrome settings, but
330 // not internal chrome
331 if (aChromeMask
& nsIWebBrowserChrome::CHROME_DEFAULT
)
332 widgetInitData
.mBorderStyle
= eBorderStyle_default
;
333 else if ((aChromeMask
& nsIWebBrowserChrome::CHROME_ALL
) == nsIWebBrowserChrome::CHROME_ALL
)
334 widgetInitData
.mBorderStyle
= eBorderStyle_all
;
336 widgetInitData
.mBorderStyle
= eBorderStyle_none
; // assumes none == 0x00
337 if (aChromeMask
& nsIWebBrowserChrome::CHROME_WINDOW_BORDERS
)
338 widgetInitData
.mBorderStyle
= static_cast<enum nsBorderStyle
>(widgetInitData
.mBorderStyle
| eBorderStyle_border
);
339 if (aChromeMask
& nsIWebBrowserChrome::CHROME_TITLEBAR
)
340 widgetInitData
.mBorderStyle
= static_cast<enum nsBorderStyle
>(widgetInitData
.mBorderStyle
| eBorderStyle_title
);
341 if (aChromeMask
& nsIWebBrowserChrome::CHROME_WINDOW_CLOSE
)
342 widgetInitData
.mBorderStyle
= static_cast<enum nsBorderStyle
>(widgetInitData
.mBorderStyle
| eBorderStyle_close
);
343 if (aChromeMask
& nsIWebBrowserChrome::CHROME_WINDOW_RESIZE
) {
344 widgetInitData
.mBorderStyle
= static_cast<enum nsBorderStyle
>(widgetInitData
.mBorderStyle
| eBorderStyle_resizeh
);
345 // only resizable windows get the maximize button (but not dialogs)
346 if (!(aChromeMask
& nsIWebBrowserChrome::CHROME_OPENAS_DIALOG
))
347 widgetInitData
.mBorderStyle
= static_cast<enum nsBorderStyle
>(widgetInitData
.mBorderStyle
| eBorderStyle_maximize
);
349 // all windows (except dialogs) get minimize buttons and the system menu
350 if (!(aChromeMask
& nsIWebBrowserChrome::CHROME_OPENAS_DIALOG
))
351 widgetInitData
.mBorderStyle
= static_cast<enum nsBorderStyle
>(widgetInitData
.mBorderStyle
| eBorderStyle_minimize
| eBorderStyle_menu
);
352 // but anyone can explicitly ask for a minimize button
353 if (aChromeMask
& nsIWebBrowserChrome::CHROME_WINDOW_MIN
) {
354 widgetInitData
.mBorderStyle
= static_cast<enum nsBorderStyle
>(widgetInitData
.mBorderStyle
| eBorderStyle_minimize
);
358 if (aInitialWidth
== nsIAppShellService::SIZE_TO_CONTENT
||
359 aInitialHeight
== nsIAppShellService::SIZE_TO_CONTENT
) {
362 window
->SetIntrinsicallySized(PR_TRUE
);
365 nsresult rv
= window
->Initialize(aParent
, aAppShell
, aUrl
,
366 aInitialWidth
, aInitialHeight
,
367 aIsHiddenWindow
, widgetInitData
);
369 NS_ENSURE_SUCCESS(rv
, rv
);
371 window
.swap(*aResult
); // transfer reference
373 aParent
->AddChildWindow(*aResult
);
375 if (aChromeMask
& nsIWebBrowserChrome::CHROME_CENTER_SCREEN
)
376 rv
= (*aResult
)->Center(aParent
, aParent
? PR_FALSE
: PR_TRUE
, PR_FALSE
);
382 nsAppShellService::GetHiddenWindow(nsIXULWindow
**aWindow
)
384 NS_ENSURE_ARG_POINTER(aWindow
);
386 *aWindow
= mHiddenWindow
;
387 NS_IF_ADDREF(*aWindow
);
388 return *aWindow
? NS_OK
: NS_ERROR_FAILURE
;
392 nsAppShellService::GetHiddenDOMWindow(nsIDOMWindowInternal
**aWindow
)
395 nsCOMPtr
<nsIDocShell
> docShell
;
396 NS_ENSURE_TRUE(mHiddenWindow
, NS_ERROR_FAILURE
);
398 rv
= mHiddenWindow
->GetDocShell(getter_AddRefs(docShell
));
399 NS_ENSURE_SUCCESS(rv
, rv
);
401 nsCOMPtr
<nsIDOMWindowInternal
> hiddenDOMWindow(do_GetInterface(docShell
, &rv
));
402 NS_ENSURE_SUCCESS(rv
, rv
);
404 *aWindow
= hiddenDOMWindow
;
405 NS_IF_ADDREF(*aWindow
);
410 nsAppShellService::GetHiddenWindowAndJSContext(nsIDOMWindowInternal
**aWindow
,
411 JSContext
**aJSContext
)
414 if ( aWindow
&& aJSContext
) {
416 *aJSContext
= nsnull
;
418 if ( mHiddenWindow
) {
419 // Convert hidden window to nsIDOMWindowInternal and extract its JSContext.
421 // 1. Get doc for hidden window.
422 nsCOMPtr
<nsIDocShell
> docShell
;
423 rv
= mHiddenWindow
->GetDocShell(getter_AddRefs(docShell
));
424 if (NS_FAILED(rv
)) break;
426 // 2. Convert that to an nsIDOMWindowInternal.
427 nsCOMPtr
<nsIDOMWindowInternal
> hiddenDOMWindow(do_GetInterface(docShell
));
428 if(!hiddenDOMWindow
) break;
430 // 3. Get script global object for the window.
431 nsCOMPtr
<nsIScriptGlobalObject
> sgo
;
432 sgo
= do_QueryInterface( hiddenDOMWindow
);
433 if (!sgo
) { rv
= NS_ERROR_FAILURE
; break; }
435 // 4. Get script context from that.
436 nsIScriptContext
*scriptContext
= sgo
->GetContext();
437 if (!scriptContext
) { rv
= NS_ERROR_FAILURE
; break; }
439 // 5. Get JSContext from the script context.
440 JSContext
*jsContext
= (JSContext
*)scriptContext
->GetNativeContext();
441 if (!jsContext
) { rv
= NS_ERROR_FAILURE
; break; }
443 // Now, give results to caller.
444 *aWindow
= hiddenDOMWindow
.get();
445 NS_IF_ADDREF( *aWindow
);
446 *aJSContext
= jsContext
;
449 rv
= NS_ERROR_FAILURE
;
452 rv
= NS_ERROR_NULL_POINTER
;
458 nsAppShellService::GetApplicationProvidedHiddenWindow(PRBool
* aAPHW
)
460 *aAPHW
= mApplicationProvidedHiddenWindow
;
465 * Register a new top level window (created elsewhere)
468 nsAppShellService::RegisterTopLevelWindow(nsIXULWindow
* aWindow
)
470 // tell the window mediator about the new window
471 nsCOMPtr
<nsIWindowMediator
> mediator
472 ( do_GetService(NS_WINDOWMEDIATOR_CONTRACTID
) );
473 NS_ASSERTION(mediator
, "Couldn't get window mediator.");
476 mediator
->RegisterWindow(aWindow
);
478 // tell the window watcher about the new window
479 nsCOMPtr
<nsPIWindowWatcher
> wwatcher ( do_GetService(NS_WINDOWWATCHER_CONTRACTID
) );
480 NS_ASSERTION(wwatcher
, "No windowwatcher?");
482 nsCOMPtr
<nsIDocShell
> docShell
;
483 aWindow
->GetDocShell(getter_AddRefs(docShell
));
484 NS_ASSERTION(docShell
, "Window has no docshell");
486 nsCOMPtr
<nsIDOMWindow
> domWindow(do_GetInterface(docShell
));
487 NS_ASSERTION(domWindow
, "Couldn't get DOM window.");
489 wwatcher
->AddWindow(domWindow
, 0);
493 // an ongoing attempt to quit is stopped by a newly opened window
494 nsCOMPtr
<nsIObserverService
> obssvc
=
495 do_GetService("@mozilla.org/observer-service;1");
496 NS_ASSERTION(obssvc
, "Couldn't get observer service.");
499 obssvc
->NotifyObservers(aWindow
, "xul-window-registered", nsnull
);
506 nsAppShellService::UnregisterTopLevelWindow(nsIXULWindow
* aWindow
)
508 if (mXPCOMShuttingDown
) {
509 /* return an error code in order to:
510 - avoid doing anything with other member variables while we are in
512 - notify the caller not to release the AppShellService after
513 unregistering the window
514 (we don't want to be deleted twice consecutively to
515 mHiddenWindow->Destroy() in our destructor)
517 return NS_ERROR_FAILURE
;
520 NS_ENSURE_ARG_POINTER(aWindow
);
522 if (aWindow
== mHiddenWindow
) {
523 // CreateHiddenWindow() does not register the window, so we're done.
527 // tell the window mediator
528 nsCOMPtr
<nsIWindowMediator
> mediator
529 ( do_GetService(NS_WINDOWMEDIATOR_CONTRACTID
) );
530 NS_ASSERTION(mediator
, "Couldn't get window mediator. Doing xpcom shutdown?");
533 mediator
->UnregisterWindow(aWindow
);
535 // tell the window watcher
536 nsCOMPtr
<nsPIWindowWatcher
> wwatcher ( do_GetService(NS_WINDOWWATCHER_CONTRACTID
) );
537 NS_ASSERTION(wwatcher
, "Couldn't get windowwatcher, doing xpcom shutdown?");
539 nsCOMPtr
<nsIDocShell
> docShell
;
540 aWindow
->GetDocShell(getter_AddRefs(docShell
));
542 nsCOMPtr
<nsIDOMWindow
> domWindow(do_GetInterface(docShell
));
544 wwatcher
->RemoveWindow(domWindow
);
551 /* Old function, needs to be removed... it was only used on Mac OS9 */
553 nsAppShellService::TopLevelWindowIsModal(nsIXULWindow
*aWindow
, PRBool aModal
)
559 nsAppShellService::Observe(nsISupports
* aSubject
, const char *aTopic
,
560 const PRUnichar
*aData
)
562 NS_ASSERTION(!strcmp(aTopic
, "xpcom-shutdown"), "Unexpected observer topic!");
564 mXPCOMShuttingDown
= PR_TRUE
;
566 ClearXPConnectSafeContext();
567 mHiddenWindow
->Destroy();