Bug 1309880 - Support undoing closeTab and closeWindow by ID, r?mikedeboer draft
authorBob Silverberg <bsilverberg@mozilla.com>
Fri, 28 Oct 2016 08:15:11 -0400
changeset 432178 9ff57714e98c8ee82f72eefce92bee22d295a263
parent 431796 8c9eed5227f8681cf08d2ee8fb3bfd5d743e4096
child 432196 87c5defffcdad6e6194362d2205433c84e563124
push id34226
push userbmo:bob.silverberg@gmail.com
push dateTue, 01 Nov 2016 11:48:30 +0000
reviewersmikedeboer
bugs1309880
milestone52.0a1
Bug 1309880 - Support undoing closeTab and closeWindow by ID, r?mikedeboer MozReview-Commit-ID: 6CoJPyjUpHH
browser/components/sessionstore/SessionStore.jsm
browser/components/sessionstore/test/browser.ini
browser/components/sessionstore/test/browser_switch_remoteness.js
browser/components/sessionstore/test/browser_undoCloseById.js
browser/components/sessionstore/test/head.js
--- a/browser/components/sessionstore/SessionStore.jsm
+++ b/browser/components/sessionstore/SessionStore.jsm
@@ -206,16 +206,20 @@ this.SessionStore = {
   get canRestoreLastSession() {
     return SessionStoreInternal.canRestoreLastSession;
   },
 
   set canRestoreLastSession(val) {
     SessionStoreInternal.canRestoreLastSession = val;
   },
 
+  get lastClosedObjectType() {
+    return SessionStoreInternal.lastClosedObjectType;
+  },
+
   init: function ss_init() {
     SessionStoreInternal.init();
   },
 
   getBrowserState: function ss_getBrowserState() {
     return SessionStoreInternal.getBrowserState();
   },
 
@@ -334,16 +338,20 @@ this.SessionStore = {
   navigateAndRestore(tab, loadArguments, historyIndex) {
     return SessionStoreInternal.navigateAndRestore(tab, loadArguments, historyIndex);
   },
 
   getSessionHistory(tab, updatedCallback) {
     return SessionStoreInternal.getSessionHistory(tab, updatedCallback);
   },
 
+  undoCloseById(aClosedId) {
+    return SessionStoreInternal.undoCloseById(aClosedId);
+  },
+
   /**
    * Determines whether the passed version number is compatible with
    * the current version number of the SessionStore.
    *
    * @param version The format and version of the file, as an array, e.g.
    * ["sessionrestore", 1]
    */
   isFormatVersionCompatible(version) {
@@ -373,16 +381,19 @@ var SessionStoreInternal = {
   QueryInterface: XPCOMUtils.generateQI([
     Ci.nsIDOMEventListener,
     Ci.nsIObserver,
     Ci.nsISupportsWeakReference
   ]),
 
   _globalState: new GlobalState(),
 
+  // A counter to be used to generate a unique ID for each closed tab or window.
+  _nextClosedId: 0,
+
   // During the initial restore and setBrowserState calls tracks the number of
   // windows yet to be restored
   _restoreCount: -1,
 
   // For each <browser> element, records the current epoch.
   _browserEpochs: new WeakMap(),
 
   // Any browsers that fires the oop-browser-crashed event gets stored in
@@ -492,16 +503,43 @@ var SessionStoreInternal = {
   set canRestoreLastSession(val) {
     // Cheat a bit; only allow false.
     if (!val) {
       LastSession.clear();
     }
   },
 
   /**
+   * Returns a string describing the last closed object, either "tab" or "window".
+   *
+   * This was added to support the sessions.restore WebExtensions API.
+   */
+  get lastClosedObjectType() {
+    if (this._closedWindows.length) {
+      // Since there are closed windows, we need to check if there's a closed tab
+      // in one of the currently open windows that was closed after the
+      // last-closed window.
+      let tabTimestamps = [];
+      let windowsEnum = Services.wm.getEnumerator("navigator:browser");
+      while (windowsEnum.hasMoreElements()) {
+        let window = windowsEnum.getNext();
+        let windowState = this._windows[window.__SSi];
+        if (windowState && windowState._closedTabs[0]) {
+          tabTimestamps.push(windowState._closedTabs[0].closedAt);
+        }
+      }
+      if (!tabTimestamps.length ||
+          (tabTimestamps.sort((a, b) => b - a)[0] < this._closedWindows[0].closedAt)) {
+        return "window";
+      }
+    }
+    return "tab";
+  },
+
+  /**
    * Initialize the sessionstore service.
    */
   init: function () {
     if (this._initialized) {
       throw new Error("SessionStore.init() must only be called once!");
     }
 
     TelemetryTimestamps.add("sessionRestoreInitialized");
@@ -1426,16 +1464,19 @@ var SessionStoreInternal = {
         });
 
         // If we found no tab closed before our
         // tab then just append it to the list.
         if (index == -1) {
           index = this._closedWindows.length;
         }
 
+        // About to save the closed window, add a unique ID.
+        winData.closedId = this._nextClosedId++;
+
         // Insert tabData at the right position.
         this._closedWindows.splice(index, 0, winData);
         this._capClosedWindows();
       } else if (!shouldStore && alreadyStored) {
         this._closedWindows.splice(winIndex, 1);
       }
     }
   },
@@ -1847,16 +1888,19 @@ var SessionStoreInternal = {
     });
 
     // If we found no tab closed before our
     // tab then just append it to the list.
     if (index == -1) {
       index = closedTabs.length;
     }
 
+    // About to save the closed tab, add a unique ID.
+    tabData.closedId = this._nextClosedId++;
+
     // Insert tabData at the right position.
     closedTabs.splice(index, 0, tabData);
 
     // Truncate the list of closed tabs, if needed.
     if (closedTabs.length > this._max_tabs_undo) {
       closedTabs.splice(this._max_tabs_undo, closedTabs.length);
     }
   },
@@ -2393,16 +2437,52 @@ var SessionStoreInternal = {
   },
 
   persistTabAttribute: function ssi_persistTabAttribute(aName) {
     if (TabAttributes.persist(aName)) {
       this.saveStateDelayed();
     }
   },
 
+
+  /**
+   * Undoes the closing of a tab or window which corresponds
+   * to the closedId passed in.
+   *
+   * @param aClosedId
+   *        The closedId of the tab or window
+   *
+   * @returns a tab or window object
+   */
+  undoCloseById(aClosedId) {
+    // Check for a window first.
+    for (let i = 0, l = this._closedWindows.length; i < l; i++) {
+      if (this._closedWindows[i].closedId == aClosedId) {
+        return this.undoCloseWindow(i);
+      }
+    }
+
+    // Check for a tab.
+    let windowsEnum = Services.wm.getEnumerator("navigator:browser");
+    while (windowsEnum.hasMoreElements()) {
+      let window = windowsEnum.getNext();
+      let windowState = this._windows[window.__SSi];
+      if (windowState) {
+        for (let j = 0, l = windowState._closedTabs.length; j < l; j++) {
+          if (windowState._closedTabs[j].closedId == aClosedId) {
+            return this.undoCloseTab(window, j);
+          }
+        }
+      }
+    }
+
+    // Neither a tab nor a window was found, return undefined and let the caller decide what to do about it.
+    return undefined;
+  },
+
   /**
    * Restores the session state stored in LastSession. This will attempt
    * to merge data into the current session. If a window was opened at startup
    * with pinned tab(s), then the remaining data from the previous session for
    * that window will be opened into that window. Otherwise new windows will
    * be opened.
    */
   restoreLastSession: function ssi_restoreLastSession() {
--- a/browser/components/sessionstore/test/browser.ini
+++ b/browser/components/sessionstore/test/browser.ini
@@ -228,9 +228,13 @@ run-if = e10s
 [browser_parentProcessRestoreHash.js]
 run-if = e10s
 [browser_sessionStoreContainer.js]
 [browser_windowStateContainer.js]
 [browser_1234021.js]
 [browser_remoteness_flip_on_restore.js]
 run-if = e10s
 [browser_background_tab_crash.js]
-run-if = e10s && crashreporter
\ No newline at end of file
+run-if = e10s && crashreporter
+
+# Disabled on Linux debug for frequent intermittent failures:
+[browser_undoCloseById.js]
+skip-if = os == "linux" && debug
--- a/browser/components/sessionstore/test/browser_switch_remoteness.js
+++ b/browser/components/sessionstore/test/browser_switch_remoteness.js
@@ -8,18 +8,21 @@ function countHistoryEntries(browser, ex
     let webNavigation = docShell.QueryInterface(Ci.nsIWebNavigation);
     let history = webNavigation.sessionHistory.QueryInterface(Ci.nsISHistoryInternal);
     Assert.equal(history && history.count, args.expected,
       "correct number of shistory entries");
   });
 }
 
 add_task(function* () {
+  // Open a new window.
+  let win = yield promiseNewWindowLoaded();
+
   // Add a new tab.
-  let tab = gBrowser.addTab("about:blank");
+  let tab = win.gBrowser.addTab("about:blank");
   let browser = tab.linkedBrowser;
   yield promiseBrowserLoaded(browser);
   ok(browser.isRemoteBrowser, "browser is remote");
 
   // Get the maximum number of preceding entries to save.
   const MAX_BACK = Services.prefs.getIntPref("browser.sessionstore.max_serialize_back");
   ok(MAX_BACK > -1, "check that the default has a value that caps data");
 
@@ -37,10 +40,10 @@ add_task(function* () {
   browser.loadURI("about:robots");
   yield promiseTabRestored(tab);
   ok(!browser.isRemoteBrowser, "browser is not remote anymore");
 
   // Check that we didn't lose any shistory entries.
   yield countHistoryEntries(browser, MAX_BACK + 3);
 
   // Cleanup.
-  gBrowser.removeTab(tab);
+  yield BrowserTestUtils.closeWindow(win);
 });
new file mode 100644
--- /dev/null
+++ b/browser/components/sessionstore/test/browser_undoCloseById.js
@@ -0,0 +1,118 @@
+"use strict";
+
+/**
+ * This test is for the undoCloseById function.
+ */
+
+Cu.import("resource:///modules/sessionstore/SessionStore.jsm");
+
+function openAndCloseTab(window, url) {
+  let tab = window.gBrowser.addTab(url);
+  yield promiseBrowserLoaded(tab.linkedBrowser, true, url);
+  yield TabStateFlusher.flush(tab.linkedBrowser);
+  yield promiseRemoveTab(tab);
+}
+
+function* openWindow(url) {
+  let win = yield promiseNewWindowLoaded();
+  let flags = Ci.nsIWebNavigation.LOAD_FLAGS_REPLACE_HISTORY;
+  win.gBrowser.selectedBrowser.loadURIWithFlags(url, flags);
+  yield promiseBrowserLoaded(win.gBrowser.selectedBrowser, true, url);
+  return win;
+}
+
+function closeWindow(win) {
+  yield BrowserTestUtils.closeWindow(win);
+  // Wait 20 ms to allow SessionStorage a chance to register the closed window.
+  yield new Promise(resolve => setTimeout(resolve, 20));
+}
+
+add_task(function* test_undoCloseById() {
+  // Clear the lists of closed windows and tabs.
+  forgetClosedWindows();
+  while (SessionStore.getClosedTabCount(window)) {
+    SessionStore.forgetClosedTab(window, 0);
+  }
+
+  // Open a new window.
+  let win = yield openWindow("about:robots");
+
+  // Open and close a tab.
+  yield openAndCloseTab(win, "about:mozilla");
+  is(SessionStore.lastClosedObjectType, "tab", "The last closed object is a tab");
+
+  // Record the first closedId created.
+  let initialClosedId = SessionStore.getClosedTabData(win, false)[0].closedId;
+
+  // Open and close another window.
+  let win2 = yield openWindow("about:mozilla");
+  yield closeWindow(win2);  // closedId == initialClosedId + 1
+  is(SessionStore.lastClosedObjectType, "window", "The last closed object is a window");
+
+  // Open and close another tab in the first window.
+  yield openAndCloseTab(win, "about:robots");  // closedId == initialClosedId + 2
+  is(SessionStore.lastClosedObjectType, "tab", "The last closed object is a tab");
+
+  // Undo closing the second tab.
+  let tab = SessionStore.undoCloseById(initialClosedId + 2);
+  yield promiseBrowserLoaded(tab.linkedBrowser);
+  is(tab.linkedBrowser.currentURI.spec, "about:robots", "The expected tab was re-opened");
+
+  let notTab = SessionStore.undoCloseById(initialClosedId + 2);
+  is(notTab, undefined, "Re-opened tab cannot be unClosed again by closedId");
+
+  // Now the last closed object should be a window again.
+  is(SessionStore.lastClosedObjectType, "window", "The last closed object is a window");
+
+  // Undo closing the first tab.
+  let tab2 = SessionStore.undoCloseById(initialClosedId);
+  yield promiseBrowserLoaded(tab2.linkedBrowser);
+  is(tab2.linkedBrowser.currentURI.spec, "about:mozilla", "The expected tab was re-opened");
+
+  // Close the two tabs we re-opened.
+  yield promiseRemoveTab(tab); // closedId == initialClosedId + 3
+  is(SessionStore.lastClosedObjectType, "tab", "The last closed object is a tab");
+  yield promiseRemoveTab(tab2); // closedId == initialClosedId + 4
+  is(SessionStore.lastClosedObjectType, "tab", "The last closed object is a tab");
+
+  // Open another new window.
+  let win3 = yield openWindow("about:mozilla");
+
+  // Close both windows.
+  yield closeWindow(win); // closedId == initialClosedId + 5
+  is(SessionStore.lastClosedObjectType, "window", "The last closed object is a window");
+  yield closeWindow(win3); // closedId == initialClosedId + 6
+  is(SessionStore.lastClosedObjectType, "window", "The last closed object is a window");
+
+  // Undo closing the second window.
+  win = SessionStore.undoCloseById(initialClosedId + 6);
+  yield BrowserTestUtils.waitForEvent(win, "load");
+
+  // Make sure we wait until this window is restored.
+  yield BrowserTestUtils.waitForEvent(win.gBrowser.tabContainer,
+                                      "SSTabRestored");
+
+  is(win.gBrowser.selectedBrowser.currentURI.spec, "about:mozilla", "The expected window was re-opened");
+
+  let notWin = SessionStore.undoCloseById(initialClosedId + 6);
+  is(notWin, undefined, "Re-opened window cannot be unClosed again by closedId");
+
+  // Close the window again.
+  yield closeWindow(win);
+  is(SessionStore.lastClosedObjectType, "window", "The last closed object is a window");
+
+  // Undo closing the first window.
+  win = SessionStore.undoCloseById(initialClosedId + 5);
+
+  yield BrowserTestUtils.waitForEvent(win, "load");
+
+  // Make sure we wait until this window is restored.
+  yield BrowserTestUtils.waitForEvent(win.gBrowser.tabContainer,
+                                      "SSTabRestored");
+
+  is(win.gBrowser.selectedBrowser.currentURI.spec, "about:robots", "The expected window was re-opened");
+
+  // Close the window again.
+  yield closeWindow(win);
+  is(SessionStore.lastClosedObjectType, "window", "The last closed object is a window");
+});
--- a/browser/components/sessionstore/test/head.js
+++ b/browser/components/sessionstore/test/head.js
@@ -278,18 +278,18 @@ var promiseForEachSessionRestoreFile = T
       if (!(ex instanceof OS.File.Error && ex.becauseNoSuchFile)) {
         throw ex;
       }
     }
     cb(data, key);
   }
 });
 
-function promiseBrowserLoaded(aBrowser, ignoreSubFrames = true) {
-  return BrowserTestUtils.browserLoaded(aBrowser, !ignoreSubFrames);
+function promiseBrowserLoaded(aBrowser, ignoreSubFrames = true, wantLoad = null) {
+  return BrowserTestUtils.browserLoaded(aBrowser, !ignoreSubFrames, wantLoad);
 }
 
 function whenWindowLoaded(aWindow, aCallback = next) {
   aWindow.addEventListener("load", function windowLoadListener() {
     aWindow.removeEventListener("load", windowLoadListener, false);
     executeSoon(function executeWhenWindowLoaded() {
       aCallback(aWindow);
     });