Bug 711515 - Opening links from external apps doesn't work if session restore is active [r=mbrubeck]
authorMark Finkle <mfinkle@mozilla.com>
Wed, 04 Jan 2012 01:21:49 -0500
changeset 84968 acd18cd340b016d19a61ae0279a65064a2082984
parent 84967 64d814cd18b35b73ddbb568b026d1df11a85b2dd
child 84969 a137f5f41545aea15d28371b59eeb57f7a85cc3c
push id805
push userakeybl@mozilla.com
push dateWed, 01 Feb 2012 18:17:35 +0000
treeherdermozilla-aurora@6fb3bf232436 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmbrubeck
bugs711515
milestone12.0a1
Bug 711515 - Opening links from external apps doesn't work if session restore is active [r=mbrubeck]
mobile/android/chrome/content/browser.js
mobile/android/components/SessionStore.js
--- a/mobile/android/chrome/content/browser.js
+++ b/mobile/android/chrome/content/browser.js
@@ -220,34 +220,57 @@ var BrowserApp = {
     ClipboardHelper.init();
     PermissionsHelper.init();
 
     // Init LoginManager
     Cc["@mozilla.org/login-manager;1"].getService(Ci.nsILoginManager);
     // Init FormHistory
     Cc["@mozilla.org/satchel/form-history;1"].getService(Ci.nsIFormHistory2);
 
-    let uri = "about:home";
+    let url = "about:home";
     if ("arguments" in window && window.arguments[0])
-      uri = window.arguments[0];
+      url = window.arguments[0];
 
     // XXX maybe we don't do this if the launch was kicked off from external
     Services.io.offline = false;
 
     // Broadcast a UIReady message so add-ons know we are finished with startup
     let event = document.createEvent("Events");
     event.initEvent("UIReady", true, false);
     window.dispatchEvent(event);
 
     // restore the previous session
     let ss = Cc["@mozilla.org/browser/sessionstore;1"].getService(Ci.nsISessionStore);
-    if (ss.shouldRestore())
-      ss.restoreLastSession(true);
-    else
-      this.addTab(uri);
+    if (ss.shouldRestore()) {
+      // A restored tab should not be active if we are loading a URL
+      let restoreToFront = false;
+
+      // Open any commandline URLs, except the homepage
+      if (url && url != "about:home") {
+        this.addTab(url);
+      } else {
+        // Let the session make a restored tab active
+        restoreToFront = true;
+
+        // Be ready to handle any restore failures by making sure we have a valid tab opened
+        let restoreCleanup = {
+          observe: function(aSubject, aTopic, aData) {
+            Services.obs.removeObserver(restoreCleanup, "sessionstore-windows-restored");
+            if (aData == "fail")
+              BrowserApp.addTab("about:home");
+          }
+        };
+        Services.obs.addObserver(restoreCleanup, "sessionstore-windows-restored", false);
+      }
+
+      // Start the restore
+      ss.restoreLastSession(restoreToFront);
+    } else {
+      this.addTab(url);
+    }
 
     // notify java that gecko has loaded
     sendMessageToJava({
       gecko: {
         type: "Gecko:Ready"
       }
     });
 
--- a/mobile/android/components/SessionStore.js
+++ b/mobile/android/components/SessionStore.js
@@ -960,17 +960,17 @@ SessionStore.prototype = {
 
         let tabs = data.windows[0].tabs;
         let selected = data.windows[0].selected;
         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];
-          let isSelected = i + 1 == selected;
+          let isSelected = (i + 1 <= selected) && aBringToFront;
           let entry = tabData.entries[tabData.index - 1];
 
           // Add a tab, but don't load the URL until we need to
           let params = { selected: isSelected, delayLoad: !isSelected, title: entry.title };
           let tab = window.BrowserApp.addTab(entry.url, params);
 
           if (!isSelected) {
             // Make sure the browser has its session data for the delay reload