Bug 248970 - Private Browsing mode (global toggle for saving/caching everything) [sessionstore part]; r=zeniko

This commit is contained in:
Ehsan Akhgari 2008-11-04 02:52:21 +03:30
Родитель f22388d6b9
Коммит 2554c3a6c3
5 изменённых файлов: 440 добавлений и 9 удалений

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

@ -20,6 +20,7 @@
*
* Contributor(s):
* Dietrich Ayala <dietrich@mozilla.com>
* Ehsan Akhgari <ehsan.akhgari@gmail.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
@ -71,7 +72,8 @@ const NOTIFY_WINDOWS_RESTORED = "sessionstore-windows-restored";
const OBSERVING = [
"domwindowopened", "domwindowclosed",
"quit-application-requested", "quit-application-granted",
"quit-application", "browser:purge-session-history"
"quit-application", "browser:purge-session-history",
"private-browsing"
];
/*
@ -157,6 +159,9 @@ SessionStoreService.prototype = {
// counts the number of crashes since the last clean start
_recentCrashes: 0,
// whether we are in private browsing mode
_inPrivateBrowsing: false,
/* ........ Global Event Handlers .............. */
/**
@ -181,7 +186,11 @@ SessionStoreService.prototype = {
OBSERVING.forEach(function(aTopic) {
observerService.addObserver(this, aTopic, true);
}, this);
var pbs = Cc["@mozilla.org/privatebrowsing;1"].
getService(Ci.nsIPrivateBrowsingService);
this._inPrivateBrowsing = pbs.privateBrowsingEnabled;
// get interval from prefs - used often, so caching/observing instead of fetching on-demand
this._interval = this._prefBranch.getIntPref("sessionstore.interval");
this._prefBranch.addObserver("sessionstore.interval", this, true);
@ -372,6 +381,32 @@ SessionStoreService.prototype = {
this._saveTimer = null;
this.saveState();
break;
case "private-browsing":
switch (aData) {
case "enter":
this.saveState(true);
this._inPrivateBrowsing = true;
this._stateBackup = this._safeEval(this._getCurrentState(true).toSource());
break;
case "exit":
aSubject.QueryInterface(Ci.nsISupportsPRBool);
let quitting = aSubject.data;
if (quitting) {
// save the backed up state with session set to stopped,
// otherwise resuming next time would look like a crash
if ("_stateBackup" in this) {
var oState = this._stateBackup;
oState.session = { state: STATE_STOPPED_STR };
this._saveStateObject(oState);
}
}
else
this._inPrivateBrowsing = false;
delete this._stateBackup;
break;
}
break;
}
},
@ -2141,7 +2176,8 @@ SessionStoreService.prototype = {
this._dirtyWindows[aWindow.__SSi] = true;
}
if (!this._saveTimer && this._resume_from_crash) {
if (!this._saveTimer && this._resume_from_crash &&
!this._inPrivateBrowsing) {
// interval until the next disk operation is allowed
var minimalDelay = this._lastSaveTime + this._interval - Date.now();
@ -2166,7 +2202,11 @@ SessionStoreService.prototype = {
// if crash recovery is disabled, only save session resuming information
if (!this._resume_from_crash && this._loadState == STATE_RUNNING)
return;
// if we're in private browsing mode, do nothing
if (this._inPrivateBrowsing)
return;
var oState = this._getCurrentState(aUpdateAll);
oState.session = {
state: this._loadState == STATE_RUNNING ? STATE_RUNNING_STR : STATE_STOPPED_STR,
@ -2174,19 +2214,26 @@ SessionStoreService.prototype = {
};
if (this._recentCrashes)
oState.session.recentCrashes = this._recentCrashes;
this._saveStateObject(oState);
},
/**
* write a state object to disk
*/
_saveStateObject: function sss_saveStateObject(aStateObj) {
var stateString = Cc["@mozilla.org/supports-string;1"].
createInstance(Ci.nsISupportsString);
stateString.data = oState.toSource();
stateString.data = aStateObj.toSource();
var observerService = Cc["@mozilla.org/observer-service;1"].
getService(Ci.nsIObserverService);
observerService.notifyObservers(stateString, "sessionstore-state-write", "");
// don't touch the file if an observer has deleted all state data
if (stateString.data)
this._writeFile(this._sessionFile, stateString.data);
this._lastSaveTime = Date.now();
},

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

@ -45,6 +45,9 @@ include $(DEPTH)/config/autoconf.mk
include $(topsrcdir)/config/rules.mk
_BROWSER_TEST_FILES = \
browser_248970_a.js \
browser_248970_b.js \
browser_248970_b_sample.html \
browser_339445.js \
browser_339445_sample.html \
browser_345898.js \

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

@ -0,0 +1,139 @@
/* ***** 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 sessionstore test code.
*
* The Initial Developer of the Original Code is
* Aaron Train <aaron.train@gmail.com>.
* Portions created by the Initial Developer are Copyright (C) 2008
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Ehsan Akhgari <ehsan.akhgari@gmail.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either 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 ***** */
function test() {
/** Test (A) for Bug 248970 **/
// test setup
waitForExplicitFinish();
// private browsing service
let pb = Cc["@mozilla.org/privatebrowsing;1"].
getService(Ci.nsIPrivateBrowsingService);
gPrefService.setBoolPref("browser.privatebrowsing.keep_current_session", true);
function getSessionstorejsModificationTime() {
// directory service
let file = Cc["@mozilla.org/file/directory_service;1"].
getService(Ci.nsIProperties).
get("ProfD", Ci.nsIFile);
// access sessionstore.js
file.append("sessionstore.js");
if (file.exists())
return file.lastModifiedTime;
else
return -1;
}
// sessionstore service
let ss = Cc["@mozilla.org/browser/sessionstore;1"].
getService(Ci.nsISessionStore);
let ss_interval = gPrefService.getIntPref("browser.sessionstore.interval");
let start_mod_time = getSessionstorejsModificationTime();
gPrefService.setIntPref("browser.sessionstore.interval", 0);
isnot(start_mod_time, getSessionstorejsModificationTime(),
"sessionstore.js should be modified when setting the interval to 0");
//////////////////////////////////////////////////////////////////
// Test (A) : No data recording while in private browsing mode //
//////////////////////////////////////////////////////////////////
// public session, add a new tab: (A)
const testURL_A = "http://example.org/";
let tab_A = gBrowser.addTab(testURL_A);
tab_A.linkedBrowser.addEventListener("load", function (aEvent) {
this.removeEventListener("load", arguments.callee, true);
let prePBModeTimeStamp = getSessionstorejsModificationTime();
// enter private browsing mode
pb.privateBrowsingEnabled = true;
ok(pb.privateBrowsingEnabled, "private browsing enabled");
// sessionstore.js should be modified at this point
isnot(prePBModeTimeStamp, getSessionstorejsModificationTime(),
"sessionstore.js should be modified when entering the private browsing mode");
// record the time stamp of sessionstore.js in the private session
let startPBModeTimeStamp = getSessionstorejsModificationTime();
// private browsing session, add new tab: (B)
const testURL_B = "http://test1.example.org/";
let tab_B = gBrowser.addTab(testURL_B);
tab_B.linkedBrowser.addEventListener("load", function (aEvent) {
this.removeEventListener("load", arguments.callee, true);
// private browsing session, add new tab: (C)
const testURL_C = "http://localhost:8888/";
let tab_C = gBrowser.addTab(testURL_C);
tab_C.linkedBrowser.addEventListener("load", function (aEvent) {
this.removeEventListener("load", arguments.callee, true);
// private browsing session, close tab: (C)
gBrowser.removeTab(tab_C);
// private browsing session, close tab: (B)
gBrowser.removeTab(tab_B);
// private browsing session, close tab: (A)
gBrowser.removeTab(tab_A);
// record the timestamp of sessionstore.js at the end of the private session
gPrefService.setIntPref("browser.sessionstore.interval", ss_interval);
gPrefService.setIntPref("browser.sessionstore.interval", 0);
let endPBModeTimeStamp = getSessionstorejsModificationTime();
// exit private browsing mode
pb.privateBrowsingEnabled = false;
ok(!pb.privateBrowsingEnabled, "private browsing disabled");
// compare timestamps: pre and post private browsing session
is(startPBModeTimeStamp, endPBModeTimeStamp,
"outside private browsing - sessionStore.js timestamp has not changed");
// cleanup
gPrefService.setIntPref("browser.sessionstore.interval", ss_interval);
gPrefService.clearUserPref("browser.privatebrowsing.keep_current_session");
finish();
}, true);
}, true);
}, true);
}

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

@ -0,0 +1,206 @@
/* ***** 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 sessionstore test code.
*
* The Initial Developer of the Original Code is
* Aaron Train <aaron.train@gmail.com>.
* Portions created by the Initial Developer are Copyright (C) 2008
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Ehsan Akhgari <ehsan.akhgari@gmail.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either 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 ***** */
function test() {
/** Test (B) for Bug 248970 **/
function test(aLambda) {
try {
return aLambda() || true;
} catch(ex) { }
return false;
}
let fieldList = {
"//input[@name='input']": Date.now().toString(),
"//input[@name='spaced 1']": Math.random().toString(),
"//input[3]": "three",
"//input[@type='checkbox']": true,
"//input[@name='uncheck']": false,
"//input[@type='radio'][1]": false,
"//input[@type='radio'][2]": true,
"//input[@type='radio'][3]": false,
"//select": 2,
"//select[@multiple]": [1, 3],
"//textarea[1]": "",
"//textarea[2]": "Some text... " + Math.random(),
"//textarea[3]": "Some more text\n" + new Date(),
"//input[@type='file']": "/dev/null"
};
function getElementByXPath(aTab, aQuery) {
let doc = aTab.linkedBrowser.contentDocument;
let xptype = Ci.nsIDOMXPathResult.FIRST_ORDERED_NODE_TYPE;
return doc.evaluate(aQuery, doc, null, xptype, null).singleNodeValue;
}
function setFormValue(aTab, aQuery, aValue) {
let node = getElementByXPath(aTab, aQuery);
if (typeof aValue == "string")
node.value = aValue;
else if (typeof aValue == "boolean")
node.checked = aValue;
else if (typeof aValue == "number")
node.selectedIndex = aValue;
else
Array.forEach(node.options, function(aOpt, aIx)
(aOpt.selected = aValue.indexOf(aIx) > -1));
}
function compareFormValue(aTab, aQuery, aValue) {
let node = getElementByXPath(aTab, aQuery);
if (!node)
return false;
if (node instanceof Ci.nsIDOMHTMLInputElement)
return aValue == (node.type == "checkbox" || node.type == "radio" ?
node.checked : node.value);
if (node instanceof Ci.nsIDOMHTMLTextAreaElement)
return aValue == node.value;
if (!node.multiple)
return aValue == node.selectedIndex;
return Array.every(node.options, function(aOpt, aIx)
(aValue.indexOf(aIx) > -1) == aOpt.selected);
}
// test setup
waitForExplicitFinish();
// private browsing service
let pb = Cc["@mozilla.org/privatebrowsing;1"].
getService(Ci.nsIPrivateBrowsingService);
gPrefService.setBoolPref("browser.privatebrowsing.keep_current_session", true);
// sessionstore service
let ss = test(function() Cc["@mozilla.org/browser/sessionstore;1"].
getService(Ci.nsISessionStore));
//////////////////////////////////////////////////////////////////
// Test (B) : Session data restoration between modes //
//////////////////////////////////////////////////////////////////
const testURL = "chrome://mochikit/content/browser/" +
"browser/components/sessionstore/test/browser/browser_248970_b_sample.html";
const testURL2 = "http://localhost:8888/browser/" +
"browser/components/sessionstore/test/browser/browser_248970_b_sample.html";
// get closed tab count
let count = ss.getClosedTabCount(window);
let max_tabs_undo = gPrefService.getIntPref("browser.sessionstore.max_tabs_undo");
ok(0 <= count && count <= max_tabs_undo,
"getClosedTabCount should return zero or at most max_tabs_undo");
// setup a state for tab (A) so we can check later that is restored
let key = "key";
let value = "Value " + Math.random();
let state = { entries: [{ url: testURL }], extData: { key: value } };
// public session, add new tab: (A)
tab_A = gBrowser.addTab(testURL);
ss.setTabState(tab_A, state.toSource());
tab_A.linkedBrowser.addEventListener("load", function(aEvent) {
this.removeEventListener("load", arguments.callee, true);
// make sure that the next closed tab will increase getClosedTabCount
gPrefService.setIntPref("browser.sessionstore.max_tabs_undo", max_tabs_undo + 1)
// populate tab_A with form data
for (let i in fieldList)
setFormValue(tab_A, i, fieldList[i]);
// public session, close tab: (A)
gBrowser.removeTab(tab_A);
// verify that closedTabCount increased
ok(ss.getClosedTabCount(window) > count, "getClosedTabCount has increased after closing a tab");
// verify tab: (A), in undo list
let tab_A_restored = test(function() ss.undoCloseTab(window, 0));
ok(tab_A_restored, "a tab is in undo list");
tab_A_restored.linkedBrowser.addEventListener("load", function(aEvent) {
this.removeEventListener("load", arguments.callee, true);
is(testURL, this.currentURI.spec, "it's the same tab that we expect");
gBrowser.removeTab(tab_A_restored);
// enter private browsing mode
pb.privateBrowsingEnabled = true;
ok(pb.privateBrowsingEnabled, "private browsing enabled");
// setup a state for tab (B) so we can check that its duplicated properly
let key1 = "key1";
let value1 = "Value " + Math.random();
let state1 = { entries: [{ url: testURL2 }], extData: { key1: value1 } };
// private browsing session, new tab: (B)
tab_B = gBrowser.addTab(testURL2);
ss.setTabState(tab_B, state1.toSource());
tab_B.linkedBrowser.addEventListener("load", function(aEvent) {
this.removeEventListener("load", arguments.callee, true);
// populate tab: (B) with different form data
for (let item in fieldList)
setFormValue(tab_B, item, fieldList[item]);
// duplicate tab: (B)
let tab_C = gBrowser.duplicateTab(tab_B);
tab_C.linkedBrowser.addEventListener("load", function(aEvent) {
this.removeEventListener("load", arguments.callee, true);
// verify the correctness of the duplicated tab
is(ss.getTabValue(tab_C, key1), value1,
"tab successfully duplicated - correct state");
for (let item in fieldList)
ok(compareFormValue(tab_C, item, fieldList[item]),
"The value for \"" + item + "\" was correctly duplicated");
// private browsing session, close tab: (C) and (B)
gBrowser.removeTab(tab_C);
gBrowser.removeTab(tab_B);
// exit private browsing mode
pb.privateBrowsingEnabled = false;
ok(!pb.privateBrowsingEnabled, "private browsing disabled");
// cleanup
gPrefService.clearUserPref("browser.privatebrowsing.keep_current_session");
finish();
}, true);
}, true);
}, true);
}, true);
}

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

@ -0,0 +1,36 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<title>Test for bug 248970</title>
<h3>Text Fields</h3>
<input type="text" name="input">
<input type="text" name="spaced 1">
<input>
<h3>Checkboxes and Radio buttons</h3>
<input type="checkbox" name="check"> Check 1
<input type="checkbox" name="uncheck" checked> Check 2
<p>
<input type="radio" name="group" value="1"> Radio 1
<input type="radio" name="group" value="some"> Radio 2
<input type="radio" name="group" checked> Radio 3
<h3>Selects</h3>
<select name="any">
<option value="1"> Select 1
<option value="some"> Select 2
<option>Select 3
</select>
<select multiple="multiple">
<option value=1> Multi-select 1
<option value=2> Multi-select 2
<option value=3> Multi-select 3
<option value=4> Multi-select 4
</select>
<h3>Text Areas</h3>
<textarea name="testarea"></textarea>
<textarea name="sized one" rows="5" cols="25"></textarea>
<textarea></textarea>
<h3>File Selector</h3>
<input type="file">