Bug 1964671 - Fire download request navigate event. r=smaug

Download request navigate events are fired from
turn is called e.g from the activation behavior of a and area elements
in the case of a download attribute being present.

Also split nsContentUtils::TriggerLink into separate click and
mouse-over cases.

Differential Revision: https://phabricator.services.mozilla.com/D247903
This commit is contained in:
Andreas Farre
2025-05-07 11:11:48 +00:00
committed by afarre@mozilla.com
parent 15770bdef4
commit d72145c63c
7 changed files with 91 additions and 22 deletions

View File

@@ -12892,6 +12892,7 @@ nsresult nsDocShell::OnLinkClick(
nsIContent* aContent, nsIURI* aURI, const nsAString& aTargetSpec,
const nsAString& aFileName, nsIInputStream* aPostDataStream,
nsIInputStream* aHeadersDataStream, bool aIsUserTriggered,
UserNavigationInvolvement aUserInvolvement,
nsIPrincipal* aTriggeringPrincipal, nsIContentSecurityPolicy* aCsp) {
#ifndef ANDROID
MOZ_ASSERT(aTriggeringPrincipal, "Need a valid triggeringPrincipal");
@@ -12931,6 +12932,31 @@ nsresult nsDocShell::OnLinkClick(
}
}
// https://html.spec.whatwg.org/#downloading-hyperlinks
// Step 6, step 6.1, step 6.2
// aFileName not being void implies a download attribute, since we've already
// checked if the attribute is present in `nsContentUtils::TriggerLinkClick`
// and made it void otherwise.
if (!aFileName.IsVoid() &&
aUserInvolvement != UserNavigationInvolvement::BrowserUI) {
if (nsCOMPtr<nsPIDOMWindowInner> window = ownerDoc->GetInnerWindow()) {
if (RefPtr<Navigation> navigation = window->Navigation()) {
AutoJSAPI jsapi;
if (jsapi.Init(window)) {
RefPtr element = aContent->AsElement();
// Step 6.4
bool shouldContinue = navigation->FireDownloadRequestNavigateEvent(
jsapi.cx(), aURI, aUserInvolvement, element, aFileName);
// Step 6.5
if (!shouldContinue) {
return NS_OK;
}
}
}
}
}
RefPtr<nsDocShellLoadState> loadState = new nsDocShellLoadState(aURI);
loadState->SetTarget(target);
loadState->SetFileName(aFileName);