Bug 1499874 - Part 4: Removed closed tabs by ID when using undoCloseTab(). r=esawin
authorJan Henning <jh+bugzilla@buttercookie.de>
Sun, 23 Dec 2018 18:42:08 +0100
changeset 509927 95343ed265a38b4cc4de4f12189e97948bfea845
parent 509926 ddfc4bd7728729de8111b38c3724b6e567ec21d0
child 509928 d801dfa24d7d11e7dca3934ba52a3900c07af660
push id10547
push userffxbld-merge
push dateMon, 21 Jan 2019 13:03:58 +0000
treeherdermozilla-beta@24ec1916bffe [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersesawin
bugs1499874
milestone66.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1499874 - Part 4: Removed closed tabs by ID when using undoCloseTab(). r=esawin Differential Revision: https://phabricator.services.mozilla.com/D15332
mobile/android/components/SessionStore.js
mobile/android/tests/browser/chrome/test_session_undo_close_tab.html
--- a/mobile/android/components/SessionStore.js
+++ b/mobile/android/components/SessionStore.js
@@ -1305,21 +1305,25 @@ SessionStore.prototype = {
 
       window.BrowserApp.addTab(url, params);
     }
   },
 
   // This function iterates through a list of tab data restoring session for each of them.
   _restoreTabs(aData) {
     let window = Services.wm.getMostRecentWindow("navigator:browser");
+    let tabIds = [];
     for (let i = 0; i < aData.tabs.length; i++) {
       let tabData = JSON.parse(aData.tabs[i]);
       let activeSHEntry = tabData.entries[tabData.index - 1];
       let selected = (i == aData.tabs.length - 1);
       let delayLoad = !selected;
+      if (tabData.tabId) {
+        tabIds.push(tabData.tabId);
+      }
 
       let params = {
         title: activeSHEntry.title,
         selected,
         delayLoad,
         isPrivate: tabData.isPrivate,
         desktopMode: tabData.desktopMode,
         cancelEditMode: selected,
@@ -1328,16 +1332,17 @@ SessionStore.prototype = {
 
       let tab = window.BrowserApp.addTab(activeSHEntry.url, params);
       tab.browser.__SS_data = tabData;
       tab.browser.__SS_extdata = tabData.extData;
       if (!delayLoad) {
         this._restoreTab(tabData, tab.browser);
       }
     }
+    this._removeClosedTabs(window, tabIds);
   },
 
   /**
   * Starts the restoration process for a browser. History is restored at this
   * point, but text data must be delayed until the content loads.
   */
   _restoreTab(aTabData, aBrowser) {
     // aTabData shouldn't be empty here, but if it is,
@@ -1508,49 +1513,63 @@ SessionStore.prototype = {
     return this._windows[aWindow.__SSID].closedTabs;
   },
 
   undoCloseTab(aWindow, aCloseTabData) {
     if (!aWindow.__SSID) {
       throw (Components.returnCode = Cr.NS_ERROR_INVALID_ARG);
     }
 
-    let closedTabs = this._windows[aWindow.__SSID].closedTabs;
-    if (!closedTabs) {
-      return null;
+    // If the tab data is in the closedTabs array, remove it.
+    if (aCloseTabData.tabId) {
+      let tabId = [aCloseTabData.tabId];
+      this._removeClosedTabs(aWindow, tabId);
     }
 
-    // If the tab data is in the closedTabs array, remove it.
-    closedTabs.find(function(tabData, i) {
-      if (tabData == aCloseTabData) {
-        closedTabs.splice(i, 1);
-        return true;
-      }
-    });
-
     // create a new tab and bring to front
     let params = {
       selected: true,
       isPrivate: aCloseTabData.isPrivate,
       desktopMode: aCloseTabData.desktopMode,
       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 = INVALID_TAB_INDEX;
 
-    if (this._notifyClosedTabs) {
+    return tab.browser;
+  },
+
+  _removeClosedTabs(aWindow, aTabIds) {
+    if (!aTabIds || aTabIds.length == 0) {
+      return;
+    }
+
+    if (!aWindow || !aWindow.__SSID) {
+      return;
+    }
+
+    const window = this._windows[aWindow.__SSID];
+    let closedTabs = window.closedTabs;
+    if (!closedTabs) {
+      return;
+    }
+    const prevClosedTabCount = closedTabs.length;
+
+    closedTabs = closedTabs.filter(closedTab =>
+      !closedTab.tabId || !aTabIds.includes(closedTab.tabId));
+    window.closedTabs = closedTabs;
+
+    if (this._notifyClosedTabs && closedTabs.length != prevClosedTabCount) {
       this._sendClosedTabsToJava(aWindow);
     }
-
-    return tab.browser;
   },
 
   get canUndoLastCloseTab() {
     return this._lastClosedTabIndex > INVALID_TAB_INDEX;
   },
 
   _sendClosedTabsToJava(aWindow) {
 
--- a/mobile/android/tests/browser/chrome/test_session_undo_close_tab.html
+++ b/mobile/android/tests/browser/chrome/test_session_undo_close_tab.html
@@ -95,20 +95,20 @@ https://bugzilla.mozilla.org/show_bug.cg
       const id = closedTab.tabId;
       let closedTabs = ss.getClosedTabs(chromeWin);
       ok(closedTabs.find(tab => tab.tabId == id), "closed tabs collection contains tab");
 
       restoredTab = BrowserApp.getTabForBrowser(ss.undoCloseTab(chromeWin, closedTab));
       tabs.push(restoredTab);
       await promiseBrowserEvent(restoredTab.browser, "DOMContentLoaded");
       closedTabs = ss.getClosedTabs(chromeWin);
-      todo(!closedTabs.find(tab => tab.tabId == id), "closed tabs collection no longer contains tab");
+      ok(!closedTabs.find(tab => tab.tabId == id), "closed tabs collection no longer contains tab");
     }
     state = ss.getClosedTabs(chromeWin);
-    todo_is(state.length, 0, "no more closed tabs should be left");
+    is(state.length, 0, "no more closed tabs should be left");
 
     cleanupTabs();
   });
 
   </script>
 </head>
 <body>
 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1499874">Mozilla Bug 1499874</a>