diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index d3e7bae2e412b0..0996914c87f46d 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -861,6 +861,16 @@ void WebContents::Observe(int type, } } +void WebContents::BeforeUnloadDialogCancelled() { + if (deferred_load_url_.id) { + auto web_contents = managed_web_contents()->GetWebContents(); + 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 1301ed15f7fa5b..42062432539997 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 5998768bd72f3b..99c421032df2f0 100644 --- a/spec/api-browser-window-spec.js +++ b/spec/api-browser-window-spec.js @@ -1106,6 +1106,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 00000000000000..6ed2a7d1aa4e2e --- /dev/null +++ b/spec/fixtures/api/beforeunload-false-prevent3.html @@ -0,0 +1,17 @@ + + + + +