Bug 460346 - Privacy pref for Always on Private Browsing Mode; r=mconnor

This commit is contained in:
Ehsan Akhgari 2008-11-04 02:52:22 +03:30
Родитель 9c71483ace
Коммит 86d09f0b5d
3 изменённых файлов: 119 добавлений и 38 удалений

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

@ -791,3 +791,6 @@ pref("app.support.baseURL", "http://support.mozilla.com/1/%APP%/%VERSION%/%OS%/%
// Name of alternate about: page for certificate errors (when undefined, defaults to about:neterror)
pref("security.alternate_certificate_error_page", "certerror");
// Whether to start the private browsing mode at application startup
pref("browser.privatebrowsing.autostart", false);

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

@ -43,7 +43,8 @@ const Cr = Components.results;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
function PrivateBrowsingService() {
this._obs.addObserver(this, "quit-application-granted", false);
this._obs.addObserver(this, "profile-after-change", true);
this._obs.addObserver(this, "quit-application-granted", true);
}
PrivateBrowsingService.prototype = {
@ -71,6 +72,9 @@ PrivateBrowsingService.prototype = {
// Make sure we don't allow re-enterant changing of the private mode
_alreadyChangingMode: false,
// Whether we're entering the private browsing mode at application startup
_autoStart: false,
// XPCOM registration
classDescription: "PrivateBrowsing Service",
contractID: "@mozilla.org/privatebrowsing;1",
@ -80,59 +84,63 @@ PrivateBrowsingService.prototype = {
],
QueryInterface: XPCOMUtils.generateQI([Ci.nsIPrivateBrowsingService,
Ci.nsIObserver]),
Ci.nsIObserver,
Ci.nsISupportsWeakReference]),
_unload: function PBS__destroy() {
// Force an exit from the private browsing mode on shutdown
this._quitting = true;
if (this._inPrivateBrowsing)
this.privateBrowsingEnabled = false;
this._obs.removeObserver(this, "quit-application-granted");
},
_onPrivateBrowsingModeChanged: function PBS__onPrivateBrowsingModeChanged() {
// clear all auth tokens
let sdr = Cc["@mozilla.org/security/sdr;1"].
getService(Ci.nsISecretDecoderRing);
sdr.logoutAndTeardown();
// nothing needs to be done here if we're auto-starting
if (!this._autoStart) {
// clear all auth tokens
let sdr = Cc["@mozilla.org/security/sdr;1"].
getService(Ci.nsISecretDecoderRing);
sdr.logoutAndTeardown();
// clear plain HTTP auth sessions
let authMgr = Cc['@mozilla.org/network/http-auth-manager;1'].
getService(Ci.nsIHttpAuthManager);
authMgr.clearAll();
// clear plain HTTP auth sessions
let authMgr = Cc['@mozilla.org/network/http-auth-manager;1'].
getService(Ci.nsIHttpAuthManager);
authMgr.clearAll();
let ss = Cc["@mozilla.org/browser/sessionstore;1"].
getService(Ci.nsISessionStore);
if (this.privateBrowsingEnabled) {
// whether we should save and close the current session
this._saveSession = true;
var prefBranch = Cc["@mozilla.org/preferences-service;1"].
getService(Ci.nsIPrefBranch);
try {
if (prefBranch.getBoolPref("browser.privatebrowsing.keep_current_session"))
this._saveSession = false;
} catch (ex) {}
let ss = Cc["@mozilla.org/browser/sessionstore;1"].
getService(Ci.nsISessionStore);
if (this.privateBrowsingEnabled) {
// whether we should save and close the current session
this._saveSession = true;
var prefBranch = Cc["@mozilla.org/preferences-service;1"].
getService(Ci.nsIPrefBranch);
try {
if (prefBranch.getBoolPref("browser.privatebrowsing.keep_current_session"))
this._saveSession = false;
} catch (ex) {}
// save the whole browser state in order to restore all windows/tabs later
if (this._saveSession && !this._savedBrowserState) {
this._savedBrowserState = ss.getBrowserState();
// save the whole browser state in order to restore all windows/tabs later
if (this._saveSession && !this._savedBrowserState) {
this._savedBrowserState = ss.getBrowserState();
// Close all windows
this._closeAllWindows();
// Close all windows
this._closeAllWindows();
// Open a single window
this._openSingleWindow();
}
}
else {
// restore the windows/tabs which were open before entering the private mode
if (this._saveSession && this._savedBrowserState) {
if (!this._quitting) // don't restore when shutting down!
ss.setBrowserState(this._savedBrowserState);
this._savedBrowserState = null;
// Open a single window
this._openSingleWindow();
}
}
else {
// restore the windows/tabs which were open before entering the private mode
if (this._saveSession && this._savedBrowserState) {
if (!this._quitting) // don't restore when shutting down!
ss.setBrowserState(this._savedBrowserState);
this._savedBrowserState = null;
}
}
}
else
this._saveSession = false;
},
#ifndef XP_WIN
@ -181,6 +189,20 @@ PrivateBrowsingService.prototype = {
observe: function PBS_observe(aSubject, aTopic, aData) {
switch (aTopic) {
case "profile-after-change":
// If the autostart prefs has been set, simulate entering the
// private browsing mode upon startup.
// This won't interfere with the session store component, because
// that component will be initialized on final-ui-startup.
let prefsService = Cc["@mozilla.org/preferences-service;1"].
getService(Ci.nsIPrefBranch);
this._autoStart = prefsService.getBoolPref("browser.privatebrowsing.autostart");
if (this._autoStart) {
this.privateBrowsingEnabled = true;
this._autoStart = false;
}
this._obs.removeObserver(this, "profile-after-change");
break;
case "quit-application-granted":
this._unload();
break;

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

@ -0,0 +1,56 @@
/* ***** 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) 2008
* 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 checks the browser.privatebrowsing.autostart preference.
function run_test() {
// initialization
var prefsService = Cc["@mozilla.org/preferences-service;1"].
getService(Ci.nsIPrefBranch);
prefsService.setBoolPref("browser.privatebrowsing.autostart", true);
do_check_true(prefsService.getBoolPref("browser.privatebrowsing.autostart"));
var pb = Cc["@mozilla.org/privatebrowsing;1"].
getService(Ci.nsIPrivateBrowsingService).
QueryInterface(Ci.nsIObserver);
// simulate startup to make the PB service read the prefs
pb.observe(null, "profile-after-change", "");
// the private mode should be entered automatically
do_check_true(pb.privateBrowsingEnabled);
}