Skip to content

Commit

Permalink
headless: do not close WebContents if beforeunload was dismissed
Browse files Browse the repository at this point in the history
Currently, headless can dismiss beforeunload dialogs, but the target
page will still proceed to close since default implementation in content/
disregards BeforeUnload results: https://source.chromium.org/chromium/chromium/src/+/master:content/public/browser/web_contents_delegate.cc;l=64?q=BeforeUnloadFired%20f:content&ss=chromium%2Fchromium%2Fsrc

This patch changes this.

Change-Id: I2c1a04dde390dbb90ac8c547187405fa513d0723
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2516217
Commit-Queue: Andrey Lushnikov <lushnikov@chromium.org>
Reviewed-by: Andrey Kosyakov <caseq@chromium.org>
Cr-Commit-Position: refs/heads/master@{#823751}
  • Loading branch information
aslushnikov authored and Commit Bot committed Nov 3, 2020
1 parent 7007308 commit 82064ac
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
6 changes: 6 additions & 0 deletions headless/lib/browser/headless_web_contents_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ class HeadlessWebContentsImpl::Delegate : public content::WebContentsDelegate {
*visible_security_state.get(), security_style_explanations);
}

void BeforeUnloadFired(content::WebContents* web_contents,
bool proceed,
bool* proceed_to_fire_unload) override {
*proceed_to_fire_unload = proceed;
}

void ActivateContents(content::WebContents* contents) override {
contents->GetMainFrame()->GetRenderViewHost()->GetWidget()->Focus();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Tests beforeunload dialog.
beforeunload
beforeunload
49 changes: 49 additions & 0 deletions headless/test/data/protocol/page/page-before-unload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

(async function(testRunner) {
const {page, session, dp} = await testRunner.startBlank(
`Tests beforeunload dialog.`);

await dp.Page.enable();
dp.Page.navigate({url: testRunner.url('/resources/beforeunload.html')});
await dp.Page.onceLoadEventFired();

// Click to activate beforeunload handling.
await dp.Input.dispatchMouseEvent({
type: 'mousePressed',
button: 'left',
buttons: 0,
clickCount: 1,
x: 1,
y: 1,
});
await dp.Input.dispatchMouseEvent({
type: 'mouseReleased',
button: 'left',
buttons: 0,
clickCount: 1,
x: 1,
y: 1,
});

// Try closing first time.
dp.Page.close();
const dialog = await dp.Page.onceJavascriptDialogOpening();

testRunner.log(dialog.params.type);

dp.Page.handleJavaScriptDialog({ accept: false, });
await dp.Page.javascriptDialogClosed();

// Try closing second time. This will make sure that
// the page didn't close after the first beforeunload dialog
// was canceled.
dp.Page.close();
const dialog2 = await dp.Page.onceJavascriptDialogOpening();
testRunner.log(dialog2.params.type);
await dp.Page.handleJavaScriptDialog({ accept: true, }),

testRunner.completeTest();
})
12 changes: 12 additions & 0 deletions headless/test/data/protocol/page/resources/beforeunload.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
</head>
<body>
</body>
<script>
window.onbeforeunload=function(e){
return 'foo';
}
</script>
</html>
2 changes: 2 additions & 0 deletions headless/test/headless_protocol_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ HEADLESS_PROTOCOL_TEST(VirtualTimeFetchKeepalive,
HEADLESS_PROTOCOL_TEST(VirtualTimeDisposeWhileRunning,
"emulation/virtual-time-dispose-while-running.js")

HEADLESS_PROTOCOL_TEST(PageBeforeUnload, "page/page-before-unload.js")

// http://crbug.com/633321
#if defined(OS_ANDROID)
#define MAYBE_VirtualTimeTimerOrder DISABLED_VirtualTimeTimerOrder
Expand Down

0 comments on commit 82064ac

Please sign in to comment.