fix: beforeunload and unload firing in BrowserViews (#28382)

* fix: beforeunload and unload firing in BrowserViews

* Ensure UserGesture is sent for BV webContents

* spec: add tests

* refactor: clean up logic

* spec: fixup specs

* docs: document event behavior for BrowserViews
This commit is contained in:
Shelley Vohr 2021-04-07 07:16:10 +00:00 committed by GitHub
parent e454bded3c
commit 7d04f729d8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 56 additions and 9 deletions

View file

@ -238,18 +238,35 @@ void BrowserWindow::OnCloseButtonClicked(bool* prevent_default) {
if (window_unresponsive_closure_.IsCancelled())
ScheduleUnresponsiveEvent(5000);
// Already closed by renderer.
if (!web_contents())
// Already closed by renderer
return;
// Required to make beforeunload handler work.
api_web_contents_->NotifyUserActivation();
if (web_contents()->NeedToFireBeforeUnloadOrUnloadEvents())
// Trigger beforeunload events for associated BrowserViews.
for (NativeBrowserView* view : window_->browser_views()) {
auto* vwc = view->web_contents();
auto* api_web_contents = api::WebContents::From(vwc);
// Required to make beforeunload handler work.
if (api_web_contents)
api_web_contents->NotifyUserActivation();
if (vwc) {
if (vwc->NeedToFireBeforeUnloadOrUnloadEvents()) {
vwc->DispatchBeforeUnload(false /* auto_cancel */);
}
}
}
if (web_contents()->NeedToFireBeforeUnloadOrUnloadEvents()) {
web_contents()->DispatchBeforeUnload(false /* auto_cancel */);
else
} else {
web_contents()->Close();
}
}
} // namespace api
void BrowserWindow::OnWindowBlur() {
if (api_web_contents_)