Bug 462639 - Handle view-source windows in Private Browsing mode; r=mconnor

This commit is contained in:
Ehsan Akhgari 2009-08-02 22:23:17 +04:30
parent bce3c0d044
commit 49083c2784
3 changed files with 239 additions and 0 deletions

View File

@ -123,6 +123,9 @@ PrivateBrowsingService.prototype = {
// Whether the private browsing mode has been started automatically
_autoStarted: false,
// List of view source window URIs for restoring later
_viewSrcURLs: [],
// XPCOM registration
classDescription: "PrivateBrowsing Service",
contractID: "@mozilla.org/privatebrowsing;1",
@ -177,6 +180,22 @@ PrivateBrowsingService.prototype = {
this._closePageInfoWindows();
// save view-source windows URIs and close them
let viewSrcWindowsEnum = Cc["@mozilla.org/appshell/window-mediator;1"].
getService(Ci.nsIWindowMediator).
getEnumerator("navigator:view-source");
while (viewSrcWindowsEnum.hasMoreElements()) {
let win = viewSrcWindowsEnum.getNext();
if (this._inPrivateBrowsing) {
let plainURL = win.getBrowser().currentURI.spec;
if (plainURL.indexOf("view-source:") == 0) {
plainURL = plainURL.substr(12);
this._viewSrcURLs.push(plainURL);
}
}
win.close();
}
if (!this._quitting && this._saveSession) {
let browserWindow = this._getBrowserWindow();
@ -215,6 +234,28 @@ PrivateBrowsingService.prototype = {
this._savedBrowserState = null;
this._closePageInfoWindows();
// re-open all view-source windows
let windowWatcher = Cc["@mozilla.org/embedcomp/window-watcher;1"].
getService(Ci.nsIWindowWatcher);
this._viewSrcURLs.forEach(function(uri) {
let args = Cc["@mozilla.org/supports-array;1"].
createInstance(Ci.nsISupportsArray);
let str = Cc["@mozilla.org/supports-string;1"].
createInstance(Ci.nsISupportsString);
str.data = uri;
args.AppendElement(str);
args.AppendElement(null); // charset
args.AppendElement(null); // page descriptor
args.AppendElement(null); // line number
let forcedCharset = Cc["@mozilla.org/supports-PRBool;1"].
createInstance(Ci.nsISupportsPRBool);
forcedCharset.data = false;
args.AppendElement(forcedCharset);
windowWatcher.openWindow(null, "chrome://global/content/viewSource.xul",
"_blank", "all,dialog=no", args);
});
this._viewSrcURLs = [];
}
else {
// otherwise, if we have transitioned into private browsing mode, load

View File

@ -68,6 +68,7 @@ _BROWSER_TEST_FILES = \
browser_privatebrowsing_downloadmonitor.js \
staller.sjs \
browser_privatebrowsing_certexceptionsui.js \
browser_privatebrowsing_viewsource.js \
$(NULL)
libs:: $(_BROWSER_TEST_FILES)

View File

@ -0,0 +1,197 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Private Browsing Tests.
*
* The Initial Developer of the Original Code is
* Ehsan Akhgari.
* Portions created by the Initial Developer are Copyright (C) 2009
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Ehsan Akhgari <ehsan.akhgari@gmail.com> (Original Author)
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
// This test makes sure that entering the private browsing mode closes
// all view source windows, and leaving it restores them
function test() {
// initialization
let pb = Cc["@mozilla.org/privatebrowsing;1"].
getService(Ci.nsIPrivateBrowsingService);
waitForExplicitFinish();
let tabAbout = gBrowser.addTab();
gBrowser.selectedTab = tabAbout;
let aboutBrowser = gBrowser.getBrowserForTab(tabAbout);
aboutBrowser.addEventListener("load", function () {
aboutBrowser.removeEventListener("load", arguments.callee, true);
let ww = Cc["@mozilla.org/embedcomp/window-watcher;1"].
getService(Ci.nsIWindowWatcher);
let observer = {
observe: function(aSubject, aTopic, aData) {
if (aTopic == "domwindowopened") {
ww.unregisterNotification(this);
let win = aSubject.QueryInterface(Ci.nsIDOMEventTarget);
win.addEventListener("load", function() {
win.removeEventListener("load", arguments.callee, false);
let browser = win.document.defaultView.getBrowser();
browser.addEventListener("load", function() {
browser.removeEventListener("load", arguments.callee, true);
// view source window is loaded, proceed with the rest of the test
step1();
}, true);
}, false);
}
}
};
ww.registerNotification(observer);
openViewSource();
function openViewSource() {
// invoke the View Source command
let event = document.createEvent("Events");
event.initEvent("command", true, true);
document.getElementById("View:PageSource").dispatchEvent(event);
}
function step1() {
observer = {
observe: function(aSubject, aTopic, aData) {
if (aTopic == "domwindowclosed") {
ok(true, "Entering the private browsing mode should close the view source window");
ww.unregisterNotification(observer);
step2();
}
else if (aTopic == "domwindowopened")
ok(false, "Entering the private browsing mode should not open any view source window");
}
};
ww.registerNotification(observer);
gBrowser.addTabsProgressListener({
onLocationChange: function() {},
onProgressChange: function() {},
onSecurityChange: function() {},
onStatusChange: function() {},
onRefreshAttempted: function() {},
onLinkIconAvailable: function() {},
onStateChange: function(aBrowser, aWebProgress, aRequest, aStateFlags, aStatus) {
if (aStateFlags & (Ci.nsIWebProgressListener.STATE_STOP |
Ci.nsIWebProgressListener.STATE_IS_WINDOW)) {
gBrowser.removeTabsProgressListener(this);
step3();
}
}
});
// enter private browsing mode
pb.privateBrowsingEnabled = true;
}
let events = 0, step2, step3;
step2 = step3 = function() {
if (++events == 2)
step4();
}
function step4() {
observer = {
observe: function(aSubject, aTopic, aData) {
if (aTopic == "domwindowopened") {
ww.unregisterNotification(this);
let win = aSubject.QueryInterface(Ci.nsIDOMEventTarget);
win.addEventListener("load", function() {
win.removeEventListener("load", arguments.callee, false);
let browser = win.document.defaultView.getBrowser();
browser.addEventListener("load", function() {
browser.removeEventListener("load", arguments.callee, true);
// view source window inside private browsing mode opened
step5();
}, true);
}, false);
}
}
};
ww.registerNotification(observer);
openViewSource();
}
function step5() {
let events = 0;
observer = {
observe: function(aSubject, aTopic, aData) {
if (aTopic == "domwindowclosed") {
ok(true, "Leaving the private browsing mode should close the existing view source window");
if (++events == 2)
ww.unregisterNotification(observer);
}
else if (aTopic == "domwindowopened") {
ok(true, "Leaving the private browsing mode should restore the previous view source window");
if (++events == 2)
ww.unregisterNotification(observer);
let win = aSubject.QueryInterface(Ci.nsIDOMEventTarget);
win.addEventListener("load", function() {
win.removeEventListener("load", arguments.callee, false);
let browser = win.document.defaultView.getBrowser();
browser.addEventListener("load", function() {
browser.removeEventListener("load", arguments.callee, true);
is(browser.currentURI.spec, "view-source:about:",
"The correct view source window should be restored");
// cleanup
win.close();
gBrowser.removeTab(gBrowser.selectedTab);
finish();
}, true);
}, false);
}
}
};
ww.registerNotification(observer);
// exit private browsing mode
pb.privateBrowsingEnabled = false;
}
}, true);
aboutBrowser.loadURI("about:");
}