From e61fde1fbd88fe7288bbb60dfc7093220c461199 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Thu, 29 Aug 2013 09:09:51 +0200 Subject: [PATCH] Bug 506975 - Write sessionstore.js less often when on battery; r=ttaubert --- browser/app/profile/firefox.js | 2 + .../sessionstore/src/SessionSaver.jsm | 43 ++++++++++++++++--- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/browser/app/profile/firefox.js b/browser/app/profile/firefox.js index 801ccf845f7..719661b84f9 100644 --- a/browser/app/profile/firefox.js +++ b/browser/app/profile/firefox.js @@ -820,6 +820,8 @@ pref("browser.sessionstore.resume_session_once", false); // minimal interval between two save operations in milliseconds pref("browser.sessionstore.interval", 15000); +// interval when device is unplugged +pref("browser.sessionstore.interval_battery", 60000); // maximum amount of POSTDATA to be saved in bytes per history entry (-1 = all of it) // (NB: POSTDATA will be saved either entirely or not at all) pref("browser.sessionstore.postdata", 0); diff --git a/browser/components/sessionstore/src/SessionSaver.jsm b/browser/components/sessionstore/src/SessionSaver.jsm index b1977d9727c..06fbb312e02 100644 --- a/browser/components/sessionstore/src/SessionSaver.jsm +++ b/browser/components/sessionstore/src/SessionSaver.jsm @@ -29,15 +29,48 @@ XPCOMUtils.defineLazyGetter(this, "gInterval", function () { Services.prefs.addObserver(PREF, () => { this.gInterval = Services.prefs.getIntPref(PREF); - // Cancel any pending runs and call runDelayed() with - // zero to apply the newly configured interval. - SessionSaverInternal.cancel(); - SessionSaverInternal.runDelayed(0); + if (isBatteryCharging()) { + // Cancel any pending runs and call runDelayed() + // to apply the newly configured interval. + SessionSaverInternal.cancel(); + SessionSaverInternal.runDelayed(0); + } }, false); return Services.prefs.getIntPref(PREF); }); +XPCOMUtils.defineLazyGetter(this, "gIntervalBattery", function () { + const PREF = "browser.sessionstore.interval_battery"; + + // Observer that updates the cached value when the preference changes. + Services.prefs.addObserver(PREF, () => { + this.gIntervalBattery = Services.prefs.getIntPref(PREF); + + if (!isBatteryCharging()) { + // Cancel any pending runs and call runDelayed() + // to apply the newly configured interval. + SessionSaverInternal.cancel(); + SessionSaverInternal.runDelayed(0); + } + }, false); + + return Services.prefs.getIntPref(PREF); +}); + +// Check if battery is charging +function isBatteryCharging() { + return Services.appShell.hiddenDOMWindow.navigator.battery.charging; +} + +// Get the current session store interval based on battery status +function getInterval() { + if (isBatteryCharging()) { + return gInterval; + } + return gIntervalBattery; +} + // Wrap a string as a nsISupports. function createSupportsString(data) { let string = Cc["@mozilla.org/supports-string;1"] @@ -148,7 +181,7 @@ let SessionSaverInternal = { } // Interval until the next disk operation is allowed. - delay = Math.max(this._lastSaveTime + gInterval - Date.now(), delay, 0); + delay = Math.max(this._lastSaveTime + getInterval() - Date.now(), delay, 0); // Schedule a state save. this._timeoutID = setTimeout(() => this._saveState(), delay);