Bug 506975 - Write sessionstore.js less often when on battery; r=ttaubert

This commit is contained in:
Manish Goregaokar 2013-08-29 09:09:51 +02:00
parent 954c61b05f
commit e61fde1fbd
2 changed files with 40 additions and 5 deletions

View File

@ -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);

View File

@ -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);