Bug 1690099 - Move focus for window.open() on a targeted tab. r=hsivonen

That doesn't go through BrowsingContext::InternalLoad. This matches the
behavior of clicking links and the behavior of other browsers.

Differential Revision: https://phabricator.services.mozilla.com/D103720
This commit is contained in:
Emilio Cobos Álvarez
2021-02-02 14:33:48 +00:00
parent 37943ca164
commit 7f16942679
7 changed files with 50 additions and 36 deletions

View File

@@ -9156,6 +9156,14 @@ nsresult nsDocShell::InternalLoad(nsDocShellLoadState* aLoadState,
return NS_ERROR_NOT_AVAILABLE;
}
// Take loadDivertedInBackground into account so the behavior would be the
// same as how the tab first opened.
const bool shouldTakeFocus =
aLoadState->SourceBrowsingContext() &&
aLoadState->SourceBrowsingContext()->IsActive() &&
!mBrowsingContext->IsActive() &&
!Preferences::GetBool("browser.tabs.loadDivertedInBackground", false);
mOriginalUriString.Truncate();
MOZ_LOG(gDocShellLeakLog, LogLevel::Debug,
@@ -9177,7 +9185,9 @@ nsresult nsDocShell::InternalLoad(nsDocShellLoadState* aLoadState,
// If we have a target to move to, do that now.
if (!aLoadState->Target().IsEmpty()) {
return PerformRetargeting(aLoadState);
} else if (aLoadState->TargetBrowsingContext().IsNull()) {
}
if (aLoadState->TargetBrowsingContext().IsNull()) {
aLoadState->SetTargetBrowsingContext(GetBrowsingContext());
}
@@ -9295,8 +9305,13 @@ nsresult nsDocShell::InternalLoad(nsDocShellLoadState* aLoadState,
// document. If the process fails, or if we successfully navigate within the
// same document, return.
if (sameDocument) {
return HandleSameDocumentNavigation(aLoadState,
sameDocumentNavigationState);
nsresult rv =
HandleSameDocumentNavigation(aLoadState, sameDocumentNavigationState);
NS_ENSURE_SUCCESS(rv, rv);
if (shouldTakeFocus) {
mBrowsingContext->Focus(CallerType::System, IgnoreErrors());
}
return rv;
}
// mContentViewer->PermitUnload can destroy |this| docShell, which
@@ -9508,6 +9523,12 @@ nsresult nsDocShell::InternalLoad(nsDocShellLoadState* aLoadState,
nsCOMPtr<nsIRequest> req;
rv = DoURILoad(aLoadState, aCacheKey, getter_AddRefs(req));
if (NS_SUCCEEDED(rv)) {
if (shouldTakeFocus) {
mBrowsingContext->Focus(CallerType::System, IgnoreErrors());
}
}
if (NS_FAILED(rv)) {
nsCOMPtr<nsIChannel> chan(do_QueryInterface(req));
UnblockEmbedderLoadEventForFailure();