fix: abort more descriptively for beforeunload (#49011)

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
This commit is contained in:
trop[bot] 2025-11-19 17:00:51 -05:00 committed by GitHub
commit aeb5af803f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 46 additions and 3 deletions

View file

@ -2395,6 +2395,9 @@ void WebContents::LoadURL(const GURL& url,
return;
}
if (web_contents()->NeedToFireBeforeUnloadOrUnloadEvents())
pending_unload_url_ = url;
// Discard non-committed entries to ensure we don't re-use a pending entry.
web_contents()->GetController().DiscardNonCommittedEntries();
web_contents()->GetController().LoadURLWithParams(params);
@ -3897,8 +3900,15 @@ void WebContents::RunBeforeUnloadDialog(content::WebContents* web_contents,
content::RenderFrameHost* rfh,
bool is_reload,
DialogClosedCallback callback) {
// TODO: asyncify?
bool default_prevented = Emit("will-prevent-unload");
if (pending_unload_url_.has_value() && !default_prevented) {
Emit("did-fail-load", static_cast<int>(net::ERR_ABORTED),
net::ErrorToShortString(net::ERR_ABORTED),
pending_unload_url_.value().possibly_invalid_spec(), true);
pending_unload_url_.reset();
}
std::move(callback).Run(default_prevented, std::u16string());
}