Bug 1373604 - Only try to switch focus to targetDocShell if the load succeeds. r=smaug, a=lizzard
authorSamael Wang <freesamael@gmail.com>
Tue, 27 Jun 2017 11:08:00 -0400
changeset 414131 67c173e36b9e19b66bceb4015c82d6efb5746df7
parent 414130 bbf775f1de45e9d512ebd08e8e9efceadf948d94
child 414132 9c661a8902fabed6c8c7fe93e5548e22efbf67df
push id1490
push usermtabara@mozilla.com
push dateMon, 31 Jul 2017 14:08:16 +0000
treeherdermozilla-release@70e32e6bf15e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug, lizzard
bugs1373604
milestone55.0
Bug 1373604 - Only try to switch focus to targetDocShell if the load succeeds. r=smaug, a=lizzard MozReview-Commit-ID: H1TsRcbagg4
docshell/base/nsDocShell.cpp
--- a/docshell/base/nsDocShell.cpp
+++ b/docshell/base/nsDocShell.cpp
@@ -10151,26 +10151,28 @@ nsDocShell::InternalLoad(nsIURI* aURI,
         //
         rv = NS_OK;
       } else if (isNewWindow) {
         // XXX: Once new windows are created hidden, the new
         //      window will need to be made visible...  For now,
         //      do nothing.
       }
 
-      // Switch to target tab if we're currently focused window.
-      // Take loadDivertedInBackground into account so the behavior would be
-      // the same as how the tab first opened.
-      bool isTargetActive = false;
-      targetDocShell->GetIsActive(&isTargetActive);
-      if (mIsActive && !isTargetActive &&
-          !Preferences::GetBool("browser.tabs.loadDivertedInBackground", false)) {
-        if (NS_FAILED(nsContentUtils::DispatchFocusChromeEvent(
-            targetDocShell->GetWindow()))) {
-          return NS_ERROR_FAILURE;
+      if (NS_SUCCEEDED(rv)) {
+        // Switch to target tab if we're currently focused window.
+        // Take loadDivertedInBackground into account so the behavior would be
+        // the same as how the tab first opened.
+        bool isTargetActive = false;
+        targetDocShell->GetIsActive(&isTargetActive);
+        nsCOMPtr<nsPIDOMWindowOuter> domWin = targetDocShell->GetWindow();
+        if (mIsActive && !isTargetActive && domWin &&
+            !Preferences::GetBool("browser.tabs.loadDivertedInBackground", false)) {
+          if (NS_FAILED(nsContentUtils::DispatchFocusChromeEvent(domWin))) {
+            return NS_ERROR_FAILURE;
+          }
         }
       }
     }
 
     // Else we ran out of memory, or were a popup and got blocked,
     // or something.
 
     return rv;