Bug 798508 - Part 1: Implement the "private" window.open feature; r=bzbarsky

This patch adds support for a "private" window flag which can be used to
open new chrome windows in private browsing mode.  It also tests to make
sure that the flag is not accessible from content.  A test in the next
part makes sure that the flag is accessible from chrome.
This commit is contained in:
Ehsan Akhgari 2012-10-07 15:04:39 -04:00
Родитель a932f16330
Коммит 5c3d6544e2
5 изменённых файлов: 38 добавлений и 7 удалений

Просмотреть файл

@ -63,6 +63,9 @@ interface nsIWebBrowserChrome : nsISupports
const unsigned long CHROME_WINDOW_MIN = 0x00004000; const unsigned long CHROME_WINDOW_MIN = 0x00004000;
const unsigned long CHROME_WINDOW_POPUP = 0x00008000; const unsigned long CHROME_WINDOW_POPUP = 0x00008000;
// private browsing windows
const unsigned long CHROME_PRIVATE_WINDOW = 0x00010000;
// Prevents new window animations on Mac OS X Lion. Ignored on other // Prevents new window animations on Mac OS X Lion. Ignored on other
// platforms. // platforms.
const unsigned long CHROME_MAC_SUPPRESS_ANIMATION = 0x01000000; const unsigned long CHROME_MAC_SUPPRESS_ANIMATION = 0x01000000;

Просмотреть файл

@ -898,17 +898,25 @@ nsWindowWatcher::OpenWindowInternal(nsIDOMWindow *aParent,
} }
if (windowIsNew) { if (windowIsNew) {
// For top level windows, we want to ensure that the privacy status of the parent // See if the caller has requested a private browsing window.
// is propagated to the new child if it is available. bool isPrivateBrowsingWindow =
nsCOMPtr<nsIDocShellTreeItem> parentItem; !!(chromeFlags & nsIWebBrowserChrome::CHROME_PRIVATE_WINDOW);
GetWindowTreeItem(aParent, getter_AddRefs(parentItem)); // Otherwise, propagate the privacy status of the parent window, if
nsCOMPtr<nsILoadContext> parentContext = do_QueryInterface(parentItem); // available, to the child.
if (!isPrivateBrowsingWindow) {
nsCOMPtr<nsIDocShellTreeItem> parentItem;
GetWindowTreeItem(aParent, getter_AddRefs(parentItem));
nsCOMPtr<nsILoadContext> parentContext = do_QueryInterface(parentItem);
if (parentContext) {
isPrivateBrowsingWindow = parentContext->UsePrivateBrowsing();
}
}
nsCOMPtr<nsIDocShellTreeItem> childRoot; nsCOMPtr<nsIDocShellTreeItem> childRoot;
newDocShellItem->GetRootTreeItem(getter_AddRefs(childRoot)); newDocShellItem->GetRootTreeItem(getter_AddRefs(childRoot));
nsCOMPtr<nsILoadContext> childContext = do_QueryInterface(childRoot); nsCOMPtr<nsILoadContext> childContext = do_QueryInterface(childRoot);
if (parentContext && childContext) { if (childContext) {
childContext->SetUsePrivateBrowsing(parentContext->UsePrivateBrowsing()); childContext->SetUsePrivateBrowsing(isPrivateBrowsingWindow);
} }
} }
@ -1490,6 +1498,12 @@ uint32_t nsWindowWatcher::CalculateChromeFlags(nsIDOMWindow *aParent,
} }
} }
// Determine whether the window is a private browsing window
if (isChrome) {
chromeFlags |= WinHasOption(aFeatures, "private", 0, &presenceFlag) ?
nsIWebBrowserChrome::CHROME_PRIVATE_WINDOW : 0;
}
nsCOMPtr<nsIPrefBranch> prefBranch; nsCOMPtr<nsIPrefBranch> prefBranch;
nsCOMPtr<nsIPrefService> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID, &rv); nsCOMPtr<nsIPrefService> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, true); NS_ENSURE_SUCCESS(rv, true);

Просмотреть файл

@ -19,6 +19,7 @@ MOCHITEST_FILES = \
bug449141_page.html \ bug449141_page.html \
test_bug499115.html \ test_bug499115.html \
test_nsFind.html \ test_nsFind.html \
test_private_window_from_content.html \
test_window_open_units.html \ test_window_open_units.html \
$(NULL) $(NULL)

Просмотреть файл

@ -0,0 +1,8 @@
<!DOCTYPE html>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script>
// Make sure that we cannot open private browsing windows from unprivileged content
var win = window.open("about:blank", "_blank", "private");
ok(!SpecialPowers.isWindowPrivate(win));
win.close();
</script>

Просмотреть файл

@ -11,6 +11,7 @@ var Cu = Components.utils;
Components.utils.import("resource://specialpowers/MockFilePicker.jsm"); Components.utils.import("resource://specialpowers/MockFilePicker.jsm");
Components.utils.import("resource://gre/modules/Services.jsm"); Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import("resource://gre/modules/PrivateBrowsingUtils.jsm");
function SpecialPowersAPI() { function SpecialPowersAPI() {
this._consoleListeners = []; this._consoleListeners = [];
@ -1242,4 +1243,8 @@ SpecialPowersAPI.prototype = {
getMozFullPath: function(file) { getMozFullPath: function(file) {
return file.mozFullPath; return file.mozFullPath;
}, },
isWindowPrivate: function(win) {
return PrivateBrowsingUtils.isWindowPrivate(win);
},
}; };