Bug 1308060 - Implement sessions.restore WebExtensions API, r=aswan
authorBob Silverberg <bsilverberg@mozilla.com>
Mon, 07 Nov 2016 14:53:27 -0500
changeset 351688 d680b3accfc56cf836afed0db24ebee55ae855e4
parent 351687 17198099ff27d75333e6b15a1786da06ab3b2bb8
child 351689 8b6533d17d4c0c81a9c856437127a67df2631a74
push id6795
push userjlund@mozilla.com
push dateMon, 23 Jan 2017 14:19:46 +0000
treeherdermozilla-esr52@76101b503191 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersaswan
bugs1308060
milestone52.0a1
Bug 1308060 - Implement sessions.restore WebExtensions API, r=aswan MozReview-Commit-ID: Ci7WhKYJBN1
browser/components/extensions/ext-sessions.js
browser/components/extensions/schemas/sessions.json
browser/components/extensions/test/browser/browser.ini
browser/components/extensions/test/browser/browser_ext_sessions_restore.js
--- a/browser/components/extensions/ext-sessions.js
+++ b/browser/components/extensions/ext-sessions.js
@@ -1,12 +1,17 @@
 /* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
 /* vim: set sts=2 sw=2 et tw=80: */
 "use strict";
 
+Cu.import("resource://gre/modules/ExtensionUtils.jsm");
+var {
+  promiseObserved,
+} = ExtensionUtils;
+
 XPCOMUtils.defineLazyModuleGetter(this, "SessionStore",
                                   "resource:///modules/sessionstore/SessionStore.jsm");
 
 function getRecentlyClosed(maxResults, extension) {
   let recentlyClosed = [];
 
   // Get closed windows
   let closedWindowData = SessionStore.getClosedWindowData(false);
@@ -26,19 +31,62 @@ function getRecentlyClosed(maxResults, e
     }
   }
 
   // Sort windows and tabs
   recentlyClosed.sort((a, b) => b.lastModified - a.lastModified);
   return recentlyClosed.slice(0, maxResults);
 }
 
+function createSession(restored, extension, sessionId) {
+  if (!restored) {
+    return Promise.reject({message: `Could not restore object using sessionId ${sessionId}.`});
+  }
+  let sessionObj = {lastModified: Date.now()};
+  if (restored instanceof Ci.nsIDOMChromeWindow) {
+    return promiseObserved("sessionstore-single-window-restored", subject => subject == restored).then(() => {
+      sessionObj.window = WindowManager.convert(extension, restored, {populate: true});
+      return Promise.resolve([sessionObj]);
+    });
+  }
+  sessionObj.tab = TabManager.for(extension).convert(restored);
+  return Promise.resolve([sessionObj]);
+}
+
 extensions.registerSchemaAPI("sessions", "addon_parent", context => {
   let {extension} = context;
   return {
     sessions: {
       getRecentlyClosed: function(filter) {
         let maxResults = filter.maxResults == undefined ? this.MAX_SESSION_RESULTS : filter.maxResults;
         return Promise.resolve(getRecentlyClosed(maxResults, extension));
       },
+      restore: function(sessionId) {
+        let session, closedId;
+        if (sessionId) {
+          closedId = sessionId;
+          session = SessionStore.undoCloseById(closedId);
+        } else if (SessionStore.lastClosedObjectType == "window") {
+          // If the most recently closed object is a window, just undo closing the most recent window.
+          session = SessionStore.undoCloseWindow(0);
+        } else {
+          // It is a tab, and we cannot call SessionStore.undoCloseTab without a window,
+          // so we must find the tab in which case we can just use its closedId.
+          let recentlyClosedTabs = [];
+          for (let window of WindowListManager.browserWindows()) {
+            let closedTabData = SessionStore.getClosedTabData(window, false);
+            for (let tab of closedTabData) {
+              recentlyClosedTabs.push(tab);
+            }
+          }
+
+          // Sort the tabs.
+          recentlyClosedTabs.sort((a, b) => b.closedAt - a.closedAt);
+
+          // Use the closedId of the most recently closed tab to restore it.
+          closedId = recentlyClosedTabs[0].closedId;
+          session = SessionStore.undoCloseById(closedId);
+        }
+        return createSession(session, extension, closedId);
+      },
     },
   };
 });
--- a/browser/components/extensions/schemas/sessions.json
+++ b/browser/components/extensions/schemas/sessions.json
@@ -98,17 +98,16 @@
                 "name": "devices", "type": "array", "items": { "$ref": "Device" }, "description": "The list of $(ref:sessions.Device) objects for each synced session, sorted in order from device with most recently modified session to device with least recently modified session. $(ref:tabs.Tab) objects are sorted by recency in the $(ref:windows.Window) of the $(ref:sessions.Session) objects."
               }
             ]
           }
         ]
       },
       {
         "name": "restore",
-        "unsupported": true,
         "type": "function",
         "description": "Reopens a $(ref:windows.Window) or $(ref:tabs.Tab), with an optional callback to run when the entry has been restored.",
         "async": "callback",
         "parameters": [
           {
             "type": "string",
             "name": "sessionId",
             "optional": true,
--- a/browser/components/extensions/test/browser/browser.ini
+++ b/browser/components/extensions/test/browser/browser.ini
@@ -56,16 +56,17 @@ tags = webextensions
 [browser_ext_popup_corners.js]
 [browser_ext_popup_sendMessage.js]
 [browser_ext_popup_shutdown.js]
 [browser_ext_runtime_openOptionsPage.js]
 [browser_ext_runtime_openOptionsPage_uninstall.js]
 [browser_ext_runtime_setUninstallURL.js]
 [browser_ext_sessions_getRecentlyClosed.js]
 [browser_ext_sessions_getRecentlyClosed_private.js]
+[browser_ext_sessions_restore.js]
 [browser_ext_simple.js]
 [browser_ext_tab_runtimeConnect.js]
 [browser_ext_tabs_audio.js]
 [browser_ext_tabs_captureVisibleTab.js]
 [browser_ext_tabs_create.js]
 [browser_ext_tabs_create_invalid_url.js]
 [browser_ext_tabs_detectLanguage.js]
 [browser_ext_tabs_duplicate.js]
new file mode 100644
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_sessions_restore.js
@@ -0,0 +1,134 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+SimpleTest.requestCompleteLog();
+
+XPCOMUtils.defineLazyModuleGetter(this, "SessionStore",
+                                  "resource:///modules/sessionstore/SessionStore.jsm");
+
+add_task(function* test_sessions_restore() {
+  function background() {
+    browser.test.onMessage.addListener((msg, data) => {
+      if (msg == "check-sessions") {
+        browser.sessions.getRecentlyClosed().then(recentlyClosed => {
+          browser.test.sendMessage("recentlyClosed", recentlyClosed);
+        });
+      } else if (msg == "restore") {
+        browser.sessions.restore(data).then(sessions => {
+          browser.test.sendMessage("restored", sessions);
+        });
+      } else if (msg == "restore-reject") {
+        browser.sessions.restore("not-a-valid-session-id").then(
+          sessions => {
+            browser.test.fail("restore rejected with an invalid sessionId");
+          },
+          error => {
+            browser.test.assertTrue(
+              error.message.includes("Could not restore object using sessionId not-a-valid-session-id."));
+            browser.test.sendMessage("restore-rejected");
+          }
+        );
+      }
+    });
+  }
+
+  let extension = ExtensionTestUtils.loadExtension({
+    manifest: {
+      permissions: ["sessions", "tabs"],
+    },
+    background,
+  });
+
+  yield extension.startup();
+
+  let {Management: {global: {WindowManager, TabManager}}} = Cu.import("resource://gre/modules/Extension.jsm", {});
+
+  function checkLocalTab(tab, expectedUrl) {
+    let realTab = TabManager.getTab(tab.id);
+    let tabState = JSON.parse(SessionStore.getTabState(realTab));
+    is(tabState.entries[0].url, expectedUrl, "restored tab has the expected url");
+  }
+
+  let win = yield BrowserTestUtils.openNewBrowserWindow();
+  yield BrowserTestUtils.loadURI(win.gBrowser.selectedBrowser, "about:config");
+  yield BrowserTestUtils.browserLoaded(win.gBrowser.selectedBrowser);
+  for (let url of ["about:robots", "about:mozilla"]) {
+    yield BrowserTestUtils.openNewForegroundTab(win.gBrowser, url);
+  }
+  yield BrowserTestUtils.closeWindow(win);
+
+  extension.sendMessage("check-sessions");
+  let recentlyClosed = yield extension.awaitMessage("recentlyClosed");
+
+  // Check that our expected window is the most recently closed.
+  is(recentlyClosed[0].window.tabs.length, 3, "most recently closed window has the expected number of tabs");
+
+  // Restore the window.
+  extension.sendMessage("restore");
+  let restored = yield extension.awaitMessage("restored");
+
+  is(restored.length, 1, "restore returned the expected number of sessions");
+  is(restored[0].window.tabs.length, 3, "restore returned a window with the expected number of tabs");
+  checkLocalTab(restored[0].window.tabs[0], "about:config");
+  checkLocalTab(restored[0].window.tabs[1], "about:robots");
+  checkLocalTab(restored[0].window.tabs[2], "about:mozilla");
+
+  // Close the window again.
+  let window = WindowManager.getWindow(restored[0].window.id);
+  yield BrowserTestUtils.closeWindow(window);
+
+  // Restore the window using the sessionId.
+  extension.sendMessage("check-sessions");
+  recentlyClosed = yield extension.awaitMessage("recentlyClosed");
+  extension.sendMessage("restore", recentlyClosed[0].window.sessionId);
+  restored = yield extension.awaitMessage("restored");
+
+  is(restored.length, 1, "restore returned the expected number of sessions");
+  is(restored[0].window.tabs.length, 3, "restore returned a window with the expected number of tabs");
+  checkLocalTab(restored[0].window.tabs[0], "about:config");
+  checkLocalTab(restored[0].window.tabs[1], "about:robots");
+  checkLocalTab(restored[0].window.tabs[2], "about:mozilla");
+
+  // Close the window again.
+  window = WindowManager.getWindow(restored[0].window.id);
+  yield BrowserTestUtils.closeWindow(window);
+
+  // Open and close a tab.
+  let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, "about:robots");
+  yield BrowserTestUtils.removeTab(tab);
+
+  // Restore the most recently closed item.
+  extension.sendMessage("restore");
+  restored = yield extension.awaitMessage("restored");
+
+  is(restored.length, 1, "restore returned the expected number of sessions");
+  tab = restored[0].tab;
+  ok(tab, "restore returned a tab");
+  checkLocalTab(tab, "about:robots");
+
+  // Close the tab again.
+  let realTab = TabManager.getTab(tab.id);
+  yield BrowserTestUtils.removeTab(realTab);
+
+  // Restore the tab using the sessionId.
+  extension.sendMessage("check-sessions");
+  recentlyClosed = yield extension.awaitMessage("recentlyClosed");
+  extension.sendMessage("restore", recentlyClosed[0].tab.sessionId);
+  restored = yield extension.awaitMessage("restored");
+
+  is(restored.length, 1, "restore returned the expected number of sessions");
+  tab = restored[0].tab;
+  ok(tab, "restore returned a tab");
+  checkLocalTab(tab, "about:robots");
+
+  // Close the tab again.
+  realTab = TabManager.getTab(tab.id);
+  yield BrowserTestUtils.removeTab(realTab);
+
+  // Try to restore something with an invalid sessionId.
+  extension.sendMessage("restore-reject");
+  restored = yield extension.awaitMessage("restore-rejected");
+
+  yield extension.unload();
+});