Bug 950159: Save state of other desktop windows to be restored when switching back from Metro. r=mbrubeck a=lsblakk
authorMarina Samuel <msamuel@mozilla.com>
Wed, 18 Dec 2013 14:20:39 -0500
changeset 175448 f0c13a12ef1cc460fdcf42a16949abf5a4a251a8
parent 175447 7153471fbb04be31cb29cab8eef510a076d06fb6
child 175449 440cc338dc726edfe4ff798e64e5725f281b23cd
push id445
push userffxbld
push dateMon, 10 Mar 2014 22:05:19 +0000
treeherdermozilla-release@dc38b741b04e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmbrubeck, lsblakk
bugs950159
milestone28.0a2
Bug 950159: Save state of other desktop windows to be restored when switching back from Metro. r=mbrubeck a=lsblakk
browser/metro/components/SessionStore.js
--- a/browser/metro/components/SessionStore.js
+++ b/browser/metro/components/SessionStore.js
@@ -10,16 +10,19 @@ const Cr = Components.results;
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 Cu.import("resource://gre/modules/Services.jsm");
 
 #ifdef MOZ_CRASHREPORTER
 XPCOMUtils.defineLazyServiceGetter(this, "CrashReporter",
   "@mozilla.org/xre/app-info;1", "nsICrashReporter");
 #endif
 
+XPCOMUtils.defineLazyServiceGetter(this, "gUUIDGenerator",
+  "@mozilla.org/uuid-generator;1", "nsIUUIDGenerator");
+
 XPCOMUtils.defineLazyGetter(this, "NetUtil", function() {
   Cu.import("resource://gre/modules/NetUtil.jsm");
   return NetUtil;
 });
 
 // -----------------------------------------------------------------------
 // Session Store
 // -----------------------------------------------------------------------
@@ -34,16 +37,18 @@ SessionStore.prototype = {
   classID: Components.ID("{8c1f07d6-cba3-4226-a315-8bd43d67d032}"),
 
   QueryInterface: XPCOMUtils.generateQI([Ci.nsISessionStore,
                                          Ci.nsIDOMEventListener,
                                          Ci.nsIObserver,
                                          Ci.nsISupportsWeakReference]),
 
   _windows: {},
+  _selectedWindow: 1,
+  _orderedWindows: [],
   _lastSaveTime: 0,
   _lastSessionTime: 0,
   _interval: 10000,
   _maxTabsUndo: 1,
   _shouldRestore: false,
 
   init: function ss_init() {
     // Get file references
@@ -279,18 +284,18 @@ SessionStore.prototype = {
     // Return if window has already been initialized
     if (aWindow && aWindow.__SSID && this._windows[aWindow.__SSID])
       return;
 
     // Ignore non-browser windows and windows opened while shutting down
     if (aWindow.document.documentElement.getAttribute("windowtype") != "navigator:browser" || this._loadState == STATE_QUITTING)
       return;
 
-    // Assign it a unique identifier (timestamp) and create its data object
-    aWindow.__SSID = "window" + Date.now();
+    // Assign it a unique identifier and create its data object
+    aWindow.__SSID = "window" + gUUIDGenerator.generateUUID().toString();
     this._windows[aWindow.__SSID] = { tabs: [], selected: 0, _closedTabs: [] };
 
     // Perform additional initialization when the first window is loading
     if (this._loadState == STATE_STOPPED) {
       this._loadState = STATE_RUNNING;
       this._lastSaveTime = Date.now();
 
       // Nothing to restore, notify observers things are complete
@@ -467,19 +472,19 @@ SessionStore.prototype = {
 
   _getCurrentState: function ss_getCurrentState() {
     let self = this;
     this._forEachBrowserWindow(function(aWindow) {
       self._collectWindowData(aWindow);
     });
 
     let data = { windows: [] };
-    let index;
-    for (index in this._windows)
-      data.windows.push(this._windows[index]);
+    for (let i = 0; i < this._orderedWindows.length; i++)
+      data.windows.push(this._windows[this._orderedWindows[i]]);
+    data.selectedWindow = this._selectedWindow;
     return data;
   },
 
   _collectTabData: function ss__collectTabData(aBrowser, aHistory) {
     // If this browser is being restored, skip any session save activity
     if (aBrowser.__SS_restore)
       return;
 
@@ -719,21 +724,34 @@ SessionStore.prototype = {
 
         if (!data || data.windows.length == 0) {
           notifyObservers("fail");
           return;
         }
 
         let window = Services.wm.getMostRecentWindow("navigator:browser");
 
-        let tabs = data.windows[0].tabs;
-        let selected = data.windows[0].selected;
+        this._selectedWindow = data.selectedWindow;
+        let windowIndex = this._selectedWindow - 1;
+        let tabs = data.windows[windowIndex].tabs;
+        let selected = data.windows[windowIndex].selected;
 
-        if (data.windows[0]._closedTabs)
-          this._windows[window.__SSID]._closedTabs = data.windows[0]._closedTabs;
+        // Move all window data from sessionstore.js to this._windows.
+        for (let i = 0; i < data.windows.length; i++) {
+          let SSID;
+          if (i != windowIndex) {
+            SSID = "window" + gUUIDGenerator.generateUUID().toString();
+            this._windows[SSID] = data.windows[i];
+          } else {
+            SSID = window.__SSID;
+            this._windows[SSID]._closedTabs =
+              this._windows[SSID]._closedTabs.concat(data.windows[windowIndex]._closedTabs);
+          }
+          this._orderedWindows.push(SSID);
+        }
 
         if (selected > tabs.length) // Clamp the selected index if it's bogus
           selected = 1;
 
         for (let i=0; i<tabs.length; i++) {
           let tabData = tabs[i];
 
           // We must have selected tabs as soon as possible, so we let all tabs be selected