Bug 1351808 - Part 1 - Replace some magic numbers in session store. r?sebastian draft
authorJan Henning <jh+bugzilla@buttercookie.de>
Sat, 08 Apr 2017 14:18:23 +0200
changeset 569604 9e1dde56b474a00c7f070385cdea1e0dcda6b136
parent 569603 41c44deb5e78d4402d2cd170f7c2feca933061c9
child 569605 84d995d9822831c10633f776ab309f2f55d4e4d8
push id56232
push usermozilla@buttercookie.de
push dateThu, 27 Apr 2017 18:04:06 +0000
reviewerssebastian
bugs1351808
milestone55.0a1
Bug 1351808 - Part 1 - Replace some magic numbers in session store. r?sebastian MozReview-Commit-ID: BzqieZVi7h4
mobile/android/components/SessionStore.js
--- a/mobile/android/components/SessionStore.js
+++ b/mobile/android/components/SessionStore.js
@@ -35,16 +35,19 @@ function log(a) {
   }
   Log.d("SessionStore", a);
 }
 
 // -----------------------------------------------------------------------
 // Session Store
 // -----------------------------------------------------------------------
 
+const INVALID_TAB_ID = -1;
+const INVALID_TAB_INDEX = -1;
+
 const STATE_STOPPED = 0;
 const STATE_RUNNING = 1;
 const STATE_QUITTING = -1;
 const STATE_QUITTING_FLUSHED = -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";
@@ -77,25 +80,25 @@ SessionStore.prototype = {
   _writeInProgress: false,
 
   // We only want to start doing backups if we've successfully
   // written the session data at least once.
   _sessionDataIsGood: false,
 
   // The index where the most recently closed tab was in the tabs array
   // when it was closed.
-  _lastClosedTabIndex: -1,
+  _lastClosedTabIndex: INVALID_TAB_INDEX,
 
   // Whether or not to send notifications for changes to the closed tabs.
   _notifyClosedTabs: false,
 
   // If we're simultaneously closing both a tab and Firefox, we don't want
   // to bother reloading the newly selected tab if it is zombified.
   // The Java UI will tell us which tab to watch out for.
-  _keepAsZombieTabId: -1,
+  _keepAsZombieTabId: INVALID_TAB_ID,
 
   init: function ss_init() {
     loggingEnabled = Services.prefs.getBoolPref("browser.sessionstore.debug_logging");
 
     // Get file references
     this._sessionFile = Services.dirsvc.get("ProfD", Ci.nsILocalFile);
     this._sessionFileBackup = this._sessionFile.clone();
     this._sessionFilePrevious = this._sessionFile.clone();
@@ -152,17 +155,17 @@ SessionStore.prototype = {
     }
   },
 
   _forgetClosedTabs: function ss_forgetClosedTabs() {
     for (let [ssid, win] of Object.entries(this._windows)) {
       win.closedTabs = [];
     }
 
-    this._lastClosedTabIndex = -1;
+    this._lastClosedTabIndex = INVALID_TAB_INDEX;
   },
 
   onEvent: function ss_onEvent(event, data, callback) {
     switch (event) {
       case "ClosedTabs:StartNotifications":
         this._notifyClosedTabs = true;
         log("ClosedTabs:StartNotifications");
         this._sendClosedTabsToJava(Services.wm.getMostRecentWindow("navigator:browser"));
@@ -413,17 +416,17 @@ SessionStore.prototype = {
           }
         }
         break;
       case "last-pb-context-exited":
         // Clear private closed tab data when we leave private browsing.
         for (let window of Object.values(this._windows)) {
           window.closedTabs = window.closedTabs.filter(tab => !tab.isPrivate);
         }
-        this._lastClosedTabIndex = -1;
+        this._lastClosedTabIndex = INVALID_TAB_INDEX;
         break;
     }
   },
 
   handleEvent: function ss_handleEvent(aEvent) {
     let window = aEvent.currentTarget.ownerGlobal;
     switch (aEvent.type) {
       case "TabOpen": {
@@ -677,17 +680,17 @@ SessionStore.prototype = {
     if (!aNoNotification) {
       this.saveStateDelayed();
     }
   },
 
   onTabClose: function ss_onTabClose(aWindow, aBrowser, aTabIndex) {
     let data = aBrowser.__SS_data || {};
     if (this._maxTabsUndo == 0 || this._sessionDataIsEmpty(data)) {
-      this._lastClosedTabIndex = -1;
+      this._lastClosedTabIndex = INVALID_TAB_INDEX;
       return;
     }
 
     if (aWindow.BrowserApp.tabs.length > 0) {
       // Bundle this browser's data and extra data and save in the closedTabs
       // window property
       data.extData = aBrowser.__SS_extdata || {};
 
@@ -804,17 +807,17 @@ SessionStore.prototype = {
 
     // Restore the resurrected browser
     if (tabId != this._keepAsZombieTabId) {
       this.restoreZombieTab(tab);
     } else {
       log("keeping as zombie tab " + tabId);
     }
     // The tab id passed through Tab:KeepZombified is valid for one TabSelect only.
-    this._keepAsZombieTabId = -1;
+    this._keepAsZombieTabId = INVALID_TAB_ID;
 
     log("onTabSelect() ran for tab " + tabId);
     this.saveStateDelayed();
     this._updateCrashReportURL(aWindow);
 
     // If the selected tab has changed while listening for closed tab
     // notifications, we may have switched between different private browsing
     // modes.
@@ -839,17 +842,17 @@ SessionStore.prototype = {
 
   onTabMove: function ss_onTabMove() {
     if (this._loadState != STATE_RUNNING) {
       return;
     }
 
     // The long press that initiated the move canceled any close undo option that may have been
     // present.
-    this._lastClosedTabIndex = -1;
+    this._lastClosedTabIndex = INVALID_TAB_INDEX;
     this.saveStateDelayed();
   },
 
   onTabInput: function ss_onTabInput(aWindow, aBrowser) {
     // If this browser belongs to a zombie tab or the initial restore hasn't yet finished,
     // skip any session save activity.
     if (aBrowser.__SS_restore || !this._startupRestoreFinished || aBrowser.__SS_restoreReloadPending) {
       return;
@@ -1406,17 +1409,17 @@ SessionStore.prototype = {
 
       // Don't restore tab if user has already closed it
       if (tab == null) {
         delete tabData.tabId;
         continue;
       }
 
       let parentId = tabData.parentId;
-      if (parentId > -1) {
+      if (parentId > INVALID_TAB_ID) {
         tab.parentId = parentId;
       }
 
       tab.browser.__SS_data = tabData;
       tab.browser.__SS_extdata = tabData.extData;
 
       if (window.BrowserApp.selectedTab == tab) {
         // After we're done restoring, we can lift the general ban on tab data
@@ -1486,17 +1489,17 @@ SessionStore.prototype = {
       tabIndex: this._lastClosedTabIndex,
       parentId: aCloseTabData.parentId
     };
     let tab = aWindow.BrowserApp.addTab(aCloseTabData.entries[aCloseTabData.index - 1].url, params);
     tab.browser.__SS_data = aCloseTabData;
     tab.browser.__SS_extdata = aCloseTabData.extData;
     this._restoreTab(aCloseTabData, tab.browser);
 
-    this._lastClosedTabIndex = -1;
+    this._lastClosedTabIndex = INVALID_TAB_INDEX;
 
     if (this._notifyClosedTabs) {
       this._sendClosedTabsToJava(aWindow);
     }
 
     return tab.browser;
   },
 
@@ -1513,25 +1516,25 @@ SessionStore.prototype = {
       throw (Components.returnCode = Cr.NS_ERROR_INVALID_ARG);
     }
 
     // remove closed tab from the array
     closedTabs.splice(aIndex, 1);
 
     // Forget the last closed tab index if we're forgetting the last closed tab.
     if (aIndex == 0) {
-      this._lastClosedTabIndex = -1;
+      this._lastClosedTabIndex = INVALID_TAB_INDEX;
     }
     if (this._notifyClosedTabs) {
       this._sendClosedTabsToJava(aWindow);
     }
   },
 
   get canUndoLastCloseTab() {
-    return this._lastClosedTabIndex > -1;
+    return this._lastClosedTabIndex > INVALID_TAB_INDEX;
   },
 
   _sendClosedTabsToJava: function ss_sendClosedTabsToJava(aWindow) {
 
     // If the app is shutting down, we don't need to do anything.
     if (this._loadState <= STATE_QUITTING) {
       return;
     }