From 72194ae795f1b9d2034f92487c23bd2b5499bf6a Mon Sep 17 00:00:00 2001 From: Emanuel Hoogeveen Date: Thu, 6 Feb 2014 20:15:59 -0500 Subject: [PATCH] Bug 943339 - Part 1: Only store a subset of session history entries to file. r=Yoric --- browser/app/profile/firefox.js | 4 +++ .../sessionstore/src/SessionHistory.jsm | 25 ++++++++++++++++--- .../sessionstore/test/browser_447951.js | 8 ++++++ docshell/shistory/public/nsISHistory.idl | 1 + 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/browser/app/profile/firefox.js b/browser/app/profile/firefox.js index 3192528a9dc6..c2dc69353a03 100644 --- a/browser/app/profile/firefox.js +++ b/browser/app/profile/firefox.js @@ -858,6 +858,10 @@ pref("browser.sessionstore.max_windows_undo", 3); // number of crashes that can occur before the about:sessionrestore page is displayed // (this pref has no effect if more than 6 hours have passed since the last crash) pref("browser.sessionstore.max_resumed_crashes", 1); +// number of back button session history entries to restore (-1 = all of them) +pref("browser.sessionstore.max_serialize_back", 10); +// number of forward button session history entries to restore (-1 = all of them) +pref("browser.sessionstore.max_serialize_forward", -1); // restore_on_demand overrides MAX_CONCURRENT_TAB_RESTORES (sessionstore constant) // and restore_hidden_tabs. When true, tabs will not be restored until they are // focused (also applies to tabs that aren't visible). When false, the values diff --git a/browser/components/sessionstore/src/SessionHistory.jsm b/browser/components/sessionstore/src/SessionHistory.jsm index 36a30ab4817e..43556a9a675d 100644 --- a/browser/components/sessionstore/src/SessionHistory.jsm +++ b/browser/components/sessionstore/src/SessionHistory.jsm @@ -70,8 +70,26 @@ let SessionHistoryInternal = { let history = webNavigation.sessionHistory; if (history && history.count > 0) { + let oldest; + let maxSerializeBack = + Services.prefs.getIntPref("browser.sessionstore.max_serialize_back"); + if (maxSerializeBack >= 0) { + oldest = Math.max(0, history.index - maxSerializeBack); + } else { // History.getEntryAtIndex(0, ...) is the oldest. + oldest = 0; + } + + let newest; + let maxSerializeFwd = + Services.prefs.getIntPref("browser.sessionstore.max_serialize_forward"); + if (maxSerializeFwd >= 0) { + newest = Math.min(history.count - 1, history.index + maxSerializeFwd); + } else { // History.getEntryAtIndex(history.count - 1, ...) is the newest. + newest = history.count - 1; + } + try { - for (let i = 0; i < history.count; i++) { + for (let i = oldest; i <= newest; i++) { let shEntry = history.getEntryAtIndex(i, false); let entry = this.serializeEntry(shEntry, isPinned); data.entries.push(entry); @@ -85,8 +103,9 @@ let SessionHistoryInternal = { "for the focused window/tab. See bug 669196."); } - // Ensure the index isn't out of bounds if an exception was thrown above. - data.index = Math.min(history.index + 1, data.entries.length); + // Set the one-based index of the currently active tab, + // ensuring it isn't out of bounds if an exception was thrown above. + data.index = Math.min(history.index - oldest + 1, data.entries.length); } // If either the session history isn't available yet or doesn't have any diff --git a/browser/components/sessionstore/test/browser_447951.js b/browser/components/sessionstore/test/browser_447951.js index e4b4c68a5342..8d3d0ee45c6e 100644 --- a/browser/components/sessionstore/test/browser_447951.js +++ b/browser/components/sessionstore/test/browser_447951.js @@ -9,6 +9,14 @@ function test() { const baseURL = "http://mochi.test:8888/browser/" + "browser/components/sessionstore/test/browser_447951_sample.html#"; + // Make sure the functionality added in bug 943339 doesn't affect the results + gPrefService.setIntPref("browser.sessionstore.max_serialize_back", -1); + gPrefService.setIntPref("browser.sessionstore.max_serialize_forward", -1); + registerCleanupFunction(function () { + gPrefService.clearUserPref("browser.sessionstore.max_serialize_back"); + gPrefService.clearUserPref("browser.sessionstore.max_serialize_forward"); + }); + let tab = gBrowser.addTab(); whenBrowserLoaded(tab.linkedBrowser, function() { let tabState = { entries: [] }; diff --git a/docshell/shistory/public/nsISHistory.idl b/docshell/shistory/public/nsISHistory.idl index 27ebbc5ec4d0..03876dc6a601 100644 --- a/docshell/shistory/public/nsISHistory.idl +++ b/docshell/shistory/public/nsISHistory.idl @@ -63,6 +63,7 @@ interface nsISHistory: nsISupports * given index. * * @param index The index value whose entry is requested. + * The oldest entry is located at index == 0. * @param modifyIndex A boolean flag that indicates if the current * index of session history should be modified * to the parameter index.