Bug 1250362 - don't flush prefs on startup unless it's really needed. r=yoric
MozReview-Commit-ID: FLrlrF4A1Om
--- a/browser/base/content/sanitize.js
+++ b/browser/base/content/sanitize.js
@@ -683,18 +683,19 @@ Sanitizer.PREF_DOMAIN = "privacy.sanitiz
// Whether we should sanitize on shutdown.
Sanitizer.PREF_SANITIZE_ON_SHUTDOWN = "privacy.sanitize.sanitizeOnShutdown";
// During a sanitization this is set to a json containing the array of items
// being sanitized, then cleared once the sanitization is complete.
// This allows to retry a sanitization on startup in case it was interrupted
// by a crash.
Sanitizer.PREF_SANITIZE_IN_PROGRESS = "privacy.sanitize.sanitizeInProgress";
// Whether the previous shutdown sanitization completed successfully.
-// Note that PREF_SANITIZE_IN_PROGRESS would be enough to detect an interrupted
-// sanitization, but this is still supported for backwards compatibility.
+// This is used to detect cases where we were supposed to sanitize on shutdown
+// but due to a crash we were unable to. In such cases there may not be any
+// sanitization in progress, cause we didn't have a chance to start it yet.
Sanitizer.PREF_SANITIZE_DID_SHUTDOWN = "privacy.sanitize.didShutdownSanitize";
// Time span constants corresponding to values of the privacy.sanitize.timeSpan
// pref. Used to determine how much history to clear, for various items
Sanitizer.TIMESPAN_EVERYTHING = 0;
Sanitizer.TIMESPAN_HOUR = 1;
Sanitizer.TIMESPAN_2HOURS = 2;
Sanitizer.TIMESPAN_4HOURS = 3;
@@ -774,20 +775,24 @@ Sanitizer.sanitize = function(aParentWin
Sanitizer.showUI(aParentWindow);
};
Sanitizer.onStartup = Task.async(function*() {
// Check if we were interrupted during the last shutdown sanitization.
let shutownSanitizationWasInterrupted =
Preferences.get(Sanitizer.PREF_SANITIZE_ON_SHUTDOWN, false) &&
!Preferences.has(Sanitizer.PREF_SANITIZE_DID_SHUTDOWN);
- // Regardless, reset the pref, since we want to check it at the next startup
- // even if the browser exits abruptly.
- Preferences.reset(Sanitizer.PREF_SANITIZE_DID_SHUTDOWN);
- Services.prefs.savePrefFile(null);
+
+ if (Preferences.has(Sanitizer.PREF_SANITIZE_DID_SHUTDOWN)) {
+ // Reset the pref, so that if we crash before having a chance to
+ // sanitize on shutdown, we will do at the next startup.
+ // Flushing prefs has a cost, so do this only if necessary.
+ Preferences.reset(Sanitizer.PREF_SANITIZE_DID_SHUTDOWN);
+ Services.prefs.savePrefFile(null);
+ }
// Make sure that we are triggered during shutdown, at the right time,
// and only once.
let placesClient = Cc["@mozilla.org/browser/nav-history-service;1"]
.getService(Ci.nsPIPlacesDatabase)
.shutdownClient
.jsclient;
@@ -799,29 +804,27 @@ Sanitizer.onStartup = Task.async(functio
Sanitizer.onShutdown().catch(er => {Promise.reject(er) /* Do not return rejected promise */;}).then(() =>
deferredSanitization.resolve()
);
}
return deferredSanitization.promise;
}
placesClient.addBlocker("sanitize.js: Sanitize on shutdown", doSanitize);
- // Check if Firefox crashed before completing a sanitization.
+ // Check if Firefox crashed during a sanitization.
let lastInterruptedSanitization = Preferences.get(Sanitizer.PREF_SANITIZE_IN_PROGRESS, "");
if (lastInterruptedSanitization) {
let s = new Sanitizer();
// If the json is invalid this will just throw and reject the Task.
let itemsToClear = JSON.parse(lastInterruptedSanitization);
yield s.sanitize(itemsToClear);
} else if (shutownSanitizationWasInterrupted) {
- // Ideally lastInterruptedSanitization should always be set when a
- // sanitization is interrupted, but some add-ons or Firefox previous
- // versions may not set the pref.
- // In such a case, we can still detect an interrupted shutdown sanitization,
- // and just redo it.
+ // Otherwise, could be we were supposed to sanitize on shutdown but we
+ // didn't have a chance, due to an earlier crash.
+ // In such a case, just redo a shutdown sanitize now, during startup.
yield Sanitizer.onShutdown();
}
});
Sanitizer.onShutdown = Task.async(function*() {
if (!Preferences.get(Sanitizer.PREF_SANITIZE_ON_SHUTDOWN)) {
return;
}