Bug 1360287 - Factor out history/tabs data purging into separate function to reduce session store observer complexity. r?sebastian
MozReview-Commit-ID: IhxMfUKEDgh
--- a/mobile/android/components/SessionStore.js
+++ b/mobile/android/components/SessionStore.js
@@ -133,16 +133,55 @@ SessionStore.prototype = {
_updateMaxTabsUndo: function ss_updateMaxTabsUndo() {
this._maxTabsUndo = Services.prefs.getIntPref(PREFS_MAX_TABS_UNDO);
if (this._maxTabsUndo == 0) {
this._forgetClosedTabs();
}
},
+ _purgeHistory: function ss_purgeHistory(topic) {
+ log(topic);
+ this._clearDisk();
+
+ // Clear all data about closed tabs
+ this._forgetClosedTabs();
+
+ // Clear all cached session history data.
+ if (topic == "browser:purge-session-history") {
+ this._forEachBrowserWindow((window) => {
+ let tabs = window.BrowserApp.tabs;
+ for (let i = 0; i < tabs.length; i++) {
+ let data = tabs[i].browser.__SS_data;
+ let sHistory = data.entries;
+ // Copy the current history entry to the end...
+ sHistory.push(sHistory[data.index - 1]);
+ // ... and then remove everything else.
+ sHistory.splice(0, sHistory.length - 1);
+ data.index = 1;
+ }
+ });
+ }
+
+ if (this._loadState == STATE_RUNNING) {
+ // Save the purged state immediately
+ this.saveState();
+ } else if (this._loadState <= STATE_QUITTING) {
+ this.saveStateDelayed();
+ if (this._loadState == STATE_QUITTING_FLUSHED) {
+ this.flushPendingState();
+ }
+ }
+
+ Services.obs.notifyObservers(null, "sessionstore-state-purge-complete");
+ if (this._notifyClosedTabs) {
+ this._sendClosedTabsToJava(Services.wm.getMostRecentWindow("navigator:browser"));
+ }
+ },
+
_clearDisk: function ss_clearDisk() {
this._sessionDataIsGood = false;
this._lastBackupTime = 0;
if (this._loadState > STATE_QUITTING) {
OS.File.remove(this._sessionFile.path);
OS.File.remove(this._sessionFileBackup.path);
OS.File.remove(this._sessionFilePrevious.path);
@@ -228,18 +267,16 @@ SessionStore.prototype = {
type: "Tabs:TabsOpened"
});
}
break;
}
}
},
- // Removal of line below tracked by bug 1360287
- // eslint-disable-next-line complexity
observe: function ss_observe(aSubject, aTopic, aData) {
let observerService = Services.obs;
switch (aTopic) {
case "app-startup":
EventDispatcher.instance.registerListener(this, [
"ClosedTabs:StartNotifications",
"ClosedTabs:StopNotifications",
"Session:Restore",
@@ -299,52 +336,17 @@ SessionStore.prototype = {
// Flush all pending writes to disk now
this.flushPendingState();
this._loadState = STATE_QUITTING_FLUSHED;
break;
case "browser:purge-session-tabs":
case "browser:purge-session-history": // catch sanitization
- log(aTopic);
- this._clearDisk();
-
- // Clear all data about closed tabs
- this._forgetClosedTabs();
-
- // Clear all cached session history data.
- if (aTopic == "browser:purge-session-history") {
- this._forEachBrowserWindow((window) => {
- let tabs = window.BrowserApp.tabs;
- for (let i = 0; i < tabs.length; i++) {
- let data = tabs[i].browser.__SS_data;
- let sHistory = data.entries;
- // Copy the current history entry to the end...
- sHistory.push(sHistory[data.index - 1]);
- // ... and then remove everything else.
- sHistory.splice(0, sHistory.length - 1);
- data.index = 1;
- }
- });
- }
-
- if (this._loadState == STATE_RUNNING) {
- // Save the purged state immediately
- this.saveState();
- } else if (this._loadState <= STATE_QUITTING) {
- this.saveStateDelayed();
- if (this._loadState == STATE_QUITTING_FLUSHED) {
- this.flushPendingState();
- }
- }
-
- Services.obs.notifyObservers(null, "sessionstore-state-purge-complete");
- if (this._notifyClosedTabs) {
- this._sendClosedTabsToJava(Services.wm.getMostRecentWindow("navigator:browser"));
- }
+ this._purgeHistory(aTopic);
break;
case "timer-callback":
if (this._loadState == STATE_RUNNING) {
// Timer call back for delayed saving
this._saveTimer = null;
log("timer-callback, pendingWrite = " + this._pendingWrite);
if (this._pendingWrite) {
this.saveState();