Bug 749187 - Ensure new tabs and windows opened in private browsing mode have docshells set accordingly; r=gavin

This commit is contained in:
Ehsan Akhgari 2012-04-26 14:35:19 -04:00
parent a3835d87d2
commit 38021da8db
3 changed files with 102 additions and 54 deletions

View File

@ -8868,7 +8868,8 @@ let gPrivateBrowsingUI = {
* and the setter should only be used in tests.
*/
get privateWindow() {
return window.getInterface(Ci.nsIWebNavigation)
return window.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIWebNavigation)
.QueryInterface(Ci.nsIDocShellTreeItem)
.treeOwner
.QueryInterface(Ci.nsIInterfaceRequestor)
@ -8878,7 +8879,8 @@ let gPrivateBrowsingUI = {
},
set privateWindow(val) {
return window.getInterface(Ci.nsIWebNavigation)
return window.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIWebNavigation)
.QueryInterface(Ci.nsIDocShellTreeItem)
.treeOwner
.QueryInterface(Ci.nsIInterfaceRequestor)

View File

@ -95,6 +95,7 @@ function PrivateBrowsingService() {
this._obs.addObserver(this, "private-browsing", true);
this._obs.addObserver(this, "command-line-startup", true);
this._obs.addObserver(this, "sessionstore-browser-state-restored", true);
this._obs.addObserver(this, "domwindowopened", true);
// List of nsIXULWindows we are going to be closing during the transition
this._windowsToClose = [];
@ -152,6 +153,17 @@ PrivateBrowsingService.prototype = {
this.privateBrowsingEnabled = false;
},
_setPerWindowPBFlag: function PBS__setPerWindowPBFlag(aWindow, aFlag) {
aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIWebNavigation)
.QueryInterface(Ci.nsIDocShellTreeItem)
.treeOwner
.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIXULWindow)
.docShell.QueryInterface(Ci.nsILoadContext)
.usePrivateBrowsing = aFlag;
},
_onBeforePrivateBrowsingModeChange: function PBS__onBeforePrivateBrowsingModeChange() {
// nothing needs to be done here if we're enabling at startup
if (!this._autoStarted) {
@ -222,23 +234,15 @@ PrivateBrowsingService.prototype = {
.docShell.contentViewer.resetCloseWindow();
}
}
if (!this._quitting) {
var windowsEnum = Services.wm.getEnumerator("navigator:browser");
while (windowsEnum.hasMoreElements()) {
var window = windowsEnum.getNext();
window.getInterface(Ci.nsIWebNavigation)
.QueryInterface(Ci.nsIDocShellTreeItem)
.treeOwner
.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIXULWindow)
.docShell.QueryInterface(Ci.nsILoadContext)
.usePrivateBrowsing = this._inPrivateBrowsing;
}
}
}
else
this._saveSession = false;
var windowsEnum = Services.wm.getEnumerator("navigator:browser");
while (windowsEnum.hasMoreElements()) {
var window = windowsEnum.getNext();
this._setPerWindowPBFlag(window, this._inPrivateBrowsing);
}
},
_onAfterPrivateBrowsingModeChange: function PBS__onAfterPrivateBrowsingModeChange() {
@ -523,6 +527,18 @@ PrivateBrowsingService.prototype = {
this._notifyIfTransitionComplete();
}
break;
case "domwindowopened":
let aWindow = aSubject;
let self = this;
aWindow.addEventListener("load", function PBS__onWindowLoad(aEvent) {
aWindow.removeEventListener("load", arguments.callee);
if (aWindow.document
.documentElement
.getAttribute("windowtype") == "navigator:browser") {
self._setPerWindowPBFlag(aWindow, self._inPrivateBrowsing);
}
}, false);
break;
}
},

View File

@ -40,6 +40,7 @@
function test() {
// initialization
waitForExplicitFinish();
gPrefService.setBoolPref("browser.privatebrowsing.keep_current_session", true);
let pb = Cc["@mozilla.org/privatebrowsing;1"].
getService(Ci.nsIPrivateBrowsingService);
@ -54,6 +55,27 @@ function test() {
gBrowser.selectedTab = gBrowser.addTab();
let originalTitle = document.title;
function testNewWindow(aCallback, expected) {
Services.obs.addObserver(function observer1(aSubject, aTopic, aData) {
aSubject.addEventListener("load", function() {
aSubject.removeEventListener("load", arguments.callee);
executeSoon(function() {
let ui = aSubject.gPrivateBrowsingUI;
is(ui.privateBrowsingEnabled, expected, "The privateBrowsingEnabled property on the new window is set correctly");
is(ui.privateWindow, expected, "The privateWindow property on the new window is set correctly");
Services.obs.addObserver(function observer2(aSubject, aTopic, aData) {
aCallback();
Services.obs.removeObserver(observer2, "domwindowclosed");
}, "domwindowclosed", false);
aSubject.close();
});
Services.obs.removeObserver(observer1, "domwindowopened");
}, false);
}, "domwindowopened", false);
OpenBrowserWindow();
}
// test the gPrivateBrowsingUI object
ok(gPrivateBrowsingUI, "The gPrivateBrowsingUI object exists");
is(pb.privateBrowsingEnabled, false, "The private browsing mode should not be started initially");
@ -61,45 +83,53 @@ function test() {
is(gPrivateBrowsingUI.privateWindow, false, "gPrivateBrowsingUI should expose the correct per-window private browsing status");
ok(pbMenuItem, "The Private Browsing menu item exists");
is(pbMenuItem.getAttribute("label"), pbMenuItem.getAttribute("startlabel"), "The Private Browsing menu item should read \"Start Private Browsing\"");
gPrivateBrowsingUI.toggleMode();
is(pb.privateBrowsingEnabled, true, "The private browsing mode should be started");
is(gPrivateBrowsingUI.privateBrowsingEnabled, true, "gPrivateBrowsingUI should expose the correct private browsing status");
is(gPrivateBrowsingUI.privateWindow, true, "gPrivateBrowsingUI should expose the correct per-window private browsing status");
// check to see if the Private Browsing mode was activated successfully
is(observerData, "enter", "Private Browsing mode was activated using the gPrivateBrowsingUI object");
is(pbMenuItem.getAttribute("label"), pbMenuItem.getAttribute("stoplabel"), "The Private Browsing menu item should read \"Stop Private Browsing\"");
gPrivateBrowsingUI.toggleMode()
is(pb.privateBrowsingEnabled, false, "The private browsing mode should not be started");
is(gPrivateBrowsingUI.privateBrowsingEnabled, false, "gPrivateBrowsingUI should expose the correct private browsing status");
is(gPrivateBrowsingUI.privateWindow, false, "gPrivateBrowsingUI should expose the correct per-window private browsing status");
// check to see if the Private Browsing mode was deactivated successfully
is(observerData, "exit", "Private Browsing mode was deactivated using the gPrivateBrowsingUI object");
is(pbMenuItem.getAttribute("label"), pbMenuItem.getAttribute("startlabel"), "The Private Browsing menu item should read \"Start Private Browsing\"");
testNewWindow(function() {
gPrivateBrowsingUI.toggleMode();
is(pb.privateBrowsingEnabled, true, "The private browsing mode should be started");
is(gPrivateBrowsingUI.privateBrowsingEnabled, true, "gPrivateBrowsingUI should expose the correct private browsing status");
is(gPrivateBrowsingUI.privateWindow, true, "gPrivateBrowsingUI should expose the correct per-window private browsing status");
// check to see if the Private Browsing mode was activated successfully
is(observerData, "enter", "Private Browsing mode was activated using the gPrivateBrowsingUI object");
is(pbMenuItem.getAttribute("label"), pbMenuItem.getAttribute("stoplabel"), "The Private Browsing menu item should read \"Stop Private Browsing\"");
testNewWindow(function() {
gPrivateBrowsingUI.toggleMode()
is(pb.privateBrowsingEnabled, false, "The private browsing mode should not be started");
is(gPrivateBrowsingUI.privateBrowsingEnabled, false, "gPrivateBrowsingUI should expose the correct private browsing status");
is(gPrivateBrowsingUI.privateWindow, false, "gPrivateBrowsingUI should expose the correct per-window private browsing status");
// check to see if the Private Browsing mode was deactivated successfully
is(observerData, "exit", "Private Browsing mode was deactivated using the gPrivateBrowsingUI object");
is(pbMenuItem.getAttribute("label"), pbMenuItem.getAttribute("startlabel"), "The Private Browsing menu item should read \"Start Private Browsing\"");
// These are tests for the privateWindow setter. Note that the setter should
// not be used anywhere else for now!
gPrivateBrowsingUI.privateWindow = true;
is(gPrivateBrowsingUI.privateWindow, true, "gPrivateBrowsingUI should accept the correct per-window private browsing status");
gPrivateBrowsingUI.privateWindow = false;
is(gPrivateBrowsingUI.privateWindow, false, "gPrivateBrowsingUI should accept the correct per-window private browsing status");
testNewWindow(function() {
// These are tests for the privateWindow setter. Note that the setter should
// not be used anywhere else for now!
gPrivateBrowsingUI.privateWindow = true;
is(gPrivateBrowsingUI.privateWindow, true, "gPrivateBrowsingUI should accept the correct per-window private browsing status");
gPrivateBrowsingUI.privateWindow = false;
is(gPrivateBrowsingUI.privateWindow, false, "gPrivateBrowsingUI should accept the correct per-window private browsing status");
// now, test using the <command> object
let cmd = document.getElementById("Tools:PrivateBrowsing");
isnot(cmd, null, "XUL command object for the private browsing service exists");
var func = new Function("", cmd.getAttribute("oncommand"));
func.call(cmd);
// check to see if the Private Browsing mode was activated successfully
is(observerData, "enter", "Private Browsing mode was activated using the command object");
// check to see that the window title has been changed correctly
isnot(document.title, originalTitle, "Private browsing mode has correctly changed the title");
func.call(cmd);
// check to see if the Private Browsing mode was deactivated successfully
is(observerData, "exit", "Private Browsing mode was deactivated using the command object");
// check to see that the window title has been restored correctly
is(document.title, originalTitle, "Private browsing mode has correctly restored the title");
// now, test using the <command> object
let cmd = document.getElementById("Tools:PrivateBrowsing");
isnot(cmd, null, "XUL command object for the private browsing service exists");
var func = new Function("", cmd.getAttribute("oncommand"));
func.call(cmd);
// check to see if the Private Browsing mode was activated successfully
is(observerData, "enter", "Private Browsing mode was activated using the command object");
// check to see that the window title has been changed correctly
isnot(document.title, originalTitle, "Private browsing mode has correctly changed the title");
func.call(cmd);
// check to see if the Private Browsing mode was deactivated successfully
is(observerData, "exit", "Private Browsing mode was deactivated using the command object");
// check to see that the window title has been restored correctly
is(document.title, originalTitle, "Private browsing mode has correctly restored the title");
// cleanup
gBrowser.removeCurrentTab();
Services.obs.removeObserver(observer, "private-browsing");
gPrefService.clearUserPref("browser.privatebrowsing.keep_current_session");
// cleanup
gBrowser.removeCurrentTab();
Services.obs.removeObserver(observer, "private-browsing");
gPrefService.clearUserPref("browser.privatebrowsing.keep_current_session");
finish();
}, false);
}, true);
}, false);
}