Bug 1320550 - Part 2 - Clear closed tabs when max_tabs_undo is set to 0. r=sebastian draft
authorJan Henning <jh+bugzilla@buttercookie.de>
Sun, 27 Nov 2016 16:59:53 +0100
changeset 444198 7c8039db1d1d7c5bc127cdc11fbc0a1387694ef9
parent 444197 37ca55519b30cbd5d263127d3ecd6b893ccfafc9
child 538260 f287a93688f0a4577f4cb6e795842b8694a2d328
push id37227
push usermozilla@buttercookie.de
push dateSun, 27 Nov 2016 17:57:41 +0000
reviewerssebastian
bugs1320550
milestone53.0a1
Bug 1320550 - Part 2 - Clear closed tabs when max_tabs_undo is set to 0. r=sebastian Currently, we stop updating closed tabs if max_tabs_undo is set to 0, however we don't clear that data and carry it around indefinitely unless the user clears the browser history. This means that when closing a tab, we still show the "Undo close tab" snackbar, however with its contents referring to the last tab the user closed before setting browser.sessionstore.max_tabs_undo to 0. With this patch, we clear all closed tabs (and don't reload them from disc on startup) if max_tabs_undo is 0, which also stops the snackbar from showing after closing a tab. MozReview-Commit-ID: PEtminpW4B
mobile/android/components/SessionStore.js
--- a/mobile/android/components/SessionStore.js
+++ b/mobile/android/components/SessionStore.js
@@ -49,16 +49,17 @@ const STATE_RUNNING = 1;
 const STATE_QUITTING = -1;
 
 const PRIVACY_NONE = 0;
 const PRIVACY_ENCRYPTED = 1;
 const PRIVACY_FULL = 2;
 
 const PREFS_RESTORE_FROM_CRASH = "browser.sessionstore.resume_from_crash";
 const PREFS_MAX_CRASH_RESUMES = "browser.sessionstore.max_resumed_crashes";
+const PREFS_MAX_TABS_UNDO = "browser.sessionstore.max_tabs_undo";
 
 const MINIMUM_SAVE_DELAY = 2000;
 // We reduce the delay in background because we could be killed at any moment,
 // however we don't set it to 0 in order to allow for multiple events arriving
 // one after the other to be batched together in one write operation.
 const MINIMUM_SAVE_DELAY_BACKGROUND = 200;
 
 function SessionStore() { }
@@ -114,39 +115,58 @@ SessionStore.prototype = {
     this._sessionFilePrevious.append("sessionstore.old"); // The previous session's file, used for what used to be the "Tabs from last time".
     this._sessionFileTemp.append(this._sessionFile.leafName + ".tmp"); // Temporary file for writing changes to disk.
 
     this._loadState = STATE_STOPPED;
     this._startupRestoreFinished = false;
 
     this._interval = Services.prefs.getIntPref("browser.sessionstore.interval");
     this._backupInterval = Services.prefs.getIntPref("browser.sessionstore.backupInterval");
-    this._maxTabsUndo = Services.prefs.getIntPref("browser.sessionstore.max_tabs_undo");
+
+    this._updateMaxTabsUndo();
+    Services.prefs.addObserver(PREFS_MAX_TABS_UNDO, () => {
+      this._updateMaxTabsUndo();
+    }, false);
 
     // Copy changes in Gecko settings to their Java counterparts,
     // so the startup code can access them
     Services.prefs.addObserver(PREFS_RESTORE_FROM_CRASH, function() {
       SharedPreferences.forApp().setBoolPref(PREFS_RESTORE_FROM_CRASH,
         Services.prefs.getBoolPref(PREFS_RESTORE_FROM_CRASH));
     }, false);
     Services.prefs.addObserver(PREFS_MAX_CRASH_RESUMES, function() {
       SharedPreferences.forApp().setIntPref(PREFS_MAX_CRASH_RESUMES,
         Services.prefs.getIntPref(PREFS_MAX_CRASH_RESUMES));
     }, false);
   },
 
+  _updateMaxTabsUndo: function ss_updateMaxTabsUndo() {
+    this._maxTabsUndo = Services.prefs.getIntPref(PREFS_MAX_TABS_UNDO);
+    if (this._maxTabsUndo == 0) {
+      this._forgetClosedTabs();
+    }
+  },
+
   _clearDisk: function ss_clearDisk() {
     this._sessionDataIsGood = false;
 
     OS.File.remove(this._sessionFile.path);
     OS.File.remove(this._sessionFileBackup.path);
     OS.File.remove(this._sessionFilePrevious.path);
     OS.File.remove(this._sessionFileTemp.path);
   },
 
+  _forgetClosedTabs: function ss_forgetClosedTabs() {
+    for (let [ssid, win] of Object.entries(this._windows)) {
+      win.closedTabs = [];
+    }
+
+    this._lastClosedTabIndex = -1;
+  },
+
   observe: function ss_observe(aSubject, aTopic, aData) {
     let self = this;
     let observerService = Services.obs;
     switch (aTopic) {
       case "app-startup":
         observerService.addObserver(this, "final-ui-startup", true);
         observerService.addObserver(this, "domwindowopened", true);
         observerService.addObserver(this, "domwindowclosed", true);
@@ -209,20 +229,17 @@ SessionStore.prototype = {
           this.flushPendingState();
         }
 
         break;
       case "browser:purge-session-history": // catch sanitization 
         this._clearDisk();
 
         // Clear all data about closed tabs
-        for (let [ssid, win] of Object.entries(this._windows))
-          win.closedTabs = [];
-
-        this._lastClosedTabIndex = -1;
+        this._forgetClosedTabs();
 
         if (this._loadState == STATE_RUNNING) {
           // Save the purged state immediately
           this.saveState();
         } else if (this._loadState == STATE_QUITTING) {
           this.saveStateDelayed();
         }
 
@@ -1640,17 +1657,17 @@ SessionStore.prototype = {
       } else {
         // Mark the browser for delay loading
         tab.browser.__SS_restore = true;
         tab.browser.setAttribute("pending", "true");
       }
     }
 
     // Restore the closed tabs array on the current window.
-    if (state.windows[0].closedTabs) {
+    if (state.windows[0].closedTabs && this._maxTabsUndo > 0) {
       this._windows[window.__SSID].closedTabs = state.windows[0].closedTabs;
       log("_restoreWindow() loaded " + state.windows[0].closedTabs.length + " closed tabs");
     }
   },
 
   getClosedTabCount: function ss_getClosedTabCount(aWindow) {
     if (!aWindow || !aWindow.__SSID || !this._windows[aWindow.__SSID]) {
       return 0; // not a browser window, or not otherwise tracked by SS.