From 042b511a274331e0b39c1467b34672038c26d720 Mon Sep 17 00:00:00 2001 From: 22222 <22222@users.noreply.github.com> Date: Tue, 25 Apr 2017 18:14:34 -0500 Subject: [PATCH] Fix #8911 --- atom/browser/api/atom_api_web_contents.cc | 9 ++++ atom/browser/api/atom_api_web_contents.h | 1 + spec/api-browser-window-spec.js | 54 +++++++++++++++++++ .../api/beforeunload-false-prevent3.html | 17 ++++++ 4 files changed, 81 insertions(+) create mode 100644 spec/fixtures/api/beforeunload-false-prevent3.html diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index a06ec2244dde..94fb80ca85ba 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -872,6 +872,15 @@ void WebContents::Observe(int type, } } +void WebContents::BeforeUnloadDialogCancelled() { + if (deferred_load_url_.id) { + auto& controller = web_contents()->GetController(); + if (!controller.GetPendingEntry()) { + deferred_load_url_.id = 0; + } + } +} + void WebContents::DevToolsReloadPage() { Emit("devtools-reload-page"); } diff --git a/atom/browser/api/atom_api_web_contents.h b/atom/browser/api/atom_api_web_contents.h index 2289cdb4a45b..1f926dbd7a5a 100644 --- a/atom/browser/api/atom_api_web_contents.h +++ b/atom/browser/api/atom_api_web_contents.h @@ -340,6 +340,7 @@ class WebContents : public mate::TrackableObject, void Observe(int type, const content::NotificationSource& source, const content::NotificationDetails& details) override; + void BeforeUnloadDialogCancelled() override; // brightray::InspectableWebContentsDelegate: void DevToolsReloadPage() override; diff --git a/spec/api-browser-window-spec.js b/spec/api-browser-window-spec.js index 34db4a6ae79a..f9396e19d008 100644 --- a/spec/api-browser-window-spec.js +++ b/spec/api-browser-window-spec.js @@ -1174,6 +1174,60 @@ describe('BrowserWindow module', function () { }) w.loadURL('file://' + path.join(fixtures, 'api', 'close-beforeunload-empty-string.html')) }) + + it('emits for each close attempt', function (done) { + var beforeUnloadCount = 0 + w.on('onbeforeunload', function () { + beforeUnloadCount++ + if (beforeUnloadCount < 3) { + w.close() + } else if (beforeUnloadCount === 3) { + done() + } + }) + w.webContents.once('did-finish-load', function () { + w.close() + }) + w.loadURL('file://' + path.join(fixtures, 'api', 'beforeunload-false-prevent3.html')) + }) + + it('emits for each reload attempt', function (done) { + var beforeUnloadCount = 0 + w.on('onbeforeunload', function () { + beforeUnloadCount++ + if (beforeUnloadCount < 3) { + w.reload() + } else if (beforeUnloadCount === 3) { + done() + } + }) + w.webContents.once('did-finish-load', function () { + w.webContents.once('did-finish-load', function () { + assert.fail('Reload was not prevented') + }) + w.reload() + }) + w.loadURL('file://' + path.join(fixtures, 'api', 'beforeunload-false-prevent3.html')) + }) + + it('emits for each navigation attempt', function (done) { + var beforeUnloadCount = 0 + w.on('onbeforeunload', function () { + beforeUnloadCount++ + if (beforeUnloadCount < 3) { + w.loadURL('about:blank') + } else if (beforeUnloadCount === 3) { + done() + } + }) + w.webContents.once('did-finish-load', function () { + w.webContents.once('did-finish-load', function () { + assert.fail('Navigation was not prevented') + }) + w.loadURL('about:blank') + }) + w.loadURL('file://' + path.join(fixtures, 'api', 'beforeunload-false-prevent3.html')) + }) }) describe('new-window event', function () { diff --git a/spec/fixtures/api/beforeunload-false-prevent3.html b/spec/fixtures/api/beforeunload-false-prevent3.html new file mode 100644 index 000000000000..6ed2a7d1aa4e --- /dev/null +++ b/spec/fixtures/api/beforeunload-false-prevent3.html @@ -0,0 +1,17 @@ + + + + +