Skip to content

Commit

Permalink
Fix #8911
Browse files Browse the repository at this point in the history
  • Loading branch information
22222 committed Apr 28, 2017
1 parent 79b02ca commit cd80827
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
10 changes: 10 additions & 0 deletions atom/browser/api/atom_api_web_contents.cc
Expand Up @@ -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");
}
Expand Down
1 change: 1 addition & 0 deletions atom/browser/api/atom_api_web_contents.h
Expand Up @@ -340,6 +340,7 @@ class WebContents : public mate::TrackableObject<WebContents>,
void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) override;
void BeforeUnloadDialogCancelled() override;

// brightray::InspectableWebContentsDelegate:
void DevToolsReloadPage() override;
Expand Down
54 changes: 54 additions & 0 deletions spec/api-browser-window-spec.js
Expand Up @@ -1106,6 +1106,60 @@ describe('BrowserWindow module', function () {
})
w.loadURL('file://' + path.join(fixtures, 'api', 'close-beforeunload-empty-string.html'))
})

it('fires for each close attempt', function (done) {
var beforeUnloadCount = 0
w.on('onbeforeunload', function () {
beforeUnloadCount++
if (beforeUnloadCount >= 3) {
done()
} else {
w.close()
}
})
w.webContents.once('did-finish-load', function () {
w.close()
})
w.loadURL('file://' + path.join(fixtures, 'api', 'beforeunload-false-prevent3.html'))
})

it('fires for each reload attempt', function (done) {
var beforeUnloadCount = 0
w.on('onbeforeunload', function () {
beforeUnloadCount++
if (beforeUnloadCount >= 3) {
done()
} else {
w.reload()
}
})
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('fires for each navigation attempt', function (done) {
var beforeUnloadCount = 0
w.on('onbeforeunload', function () {
beforeUnloadCount++
if (beforeUnloadCount >= 3) {
done()
} else {
w.loadURL('about:blank')
}
})
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 () {
Expand Down
17 changes: 17 additions & 0 deletions spec/fixtures/api/beforeunload-false-prevent3.html
@@ -0,0 +1,17 @@
<html>
<body>
<script type="text/javascript" charset="utf-8">
// Only prevent unload on the first three window closes
var unloadPreventedCount = 0;
window.onbeforeunload = function() {
setTimeout(function() {
require('electron').remote.getCurrentWindow().emit('onbeforeunload');
}, 0);
if (unloadPreventedCount < 3) {
unloadPreventedCount++;
return false;
}
}
</script>
</body>
</html>

0 comments on commit cd80827

Please sign in to comment.