From eaa22c46f836bae0b85c4183d81d894d6359332b Mon Sep 17 00:00:00 2001 From: Samael Wang Date: Wed, 21 Jun 2017 14:54:15 +0800 Subject: [PATCH] Bug 1373604 - Only try to switch focus to targetDocShell if the load succeeds. r=smaug MozReview-Commit-ID: H1TsRcbagg4 --- docshell/base/nsDocShell.cpp | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/docshell/base/nsDocShell.cpp b/docshell/base/nsDocShell.cpp index 3d9689889cd0..bc393471a89f 100644 --- a/docshell/base/nsDocShell.cpp +++ b/docshell/base/nsDocShell.cpp @@ -10179,16 +10179,18 @@ nsDocShell::InternalLoad(nsIURI* aURI, // 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 domWin = targetDocShell->GetWindow(); + if (mIsActive && !isTargetActive && domWin && + !Preferences::GetBool("browser.tabs.loadDivertedInBackground", false)) { + if (NS_FAILED(nsContentUtils::DispatchFocusChromeEvent(domWin))) { + return NS_ERROR_FAILURE; + } } } }