Merge pull request #1540 from atom/webview-beforeunload

Make beforeunload handler work in webview
This commit is contained in:
Cheng Zhao 2015-05-01 14:55:52 +08:00
commit 52240750a1
5 changed files with 41 additions and 1 deletions

View file

@ -7,6 +7,7 @@
#include <set>
#include "atom/browser/atom_browser_context.h"
#include "atom/browser/atom_javascript_dialog_manager.h"
#include "atom/browser/native_window.h"
#include "atom/browser/web_dialog_helper.h"
#include "atom/browser/web_view_manager.h"
@ -146,6 +147,14 @@ content::WebContents* WebContents::OpenURLFromTab(
return web_contents();
}
content::JavaScriptDialogManager* WebContents::GetJavaScriptDialogManager(
content::WebContents* source) {
if (!dialog_manager_)
dialog_manager_.reset(new AtomJavaScriptDialogManager);
return dialog_manager_.get();
}
void WebContents::RunFileChooser(content::WebContents* guest,
const content::FileChooserParams& params) {
if (!web_dialog_helper_)

View file

@ -27,6 +27,7 @@ class Dictionary;
namespace atom {
class AtomJavaScriptDialogManager;
class WebDialogHelper;
namespace api {
@ -126,6 +127,8 @@ class WebContents : public mate::EventEmitter,
content::WebContents* OpenURLFromTab(
content::WebContents* source,
const content::OpenURLParams& params) override;
content::JavaScriptDialogManager* GetJavaScriptDialogManager(
content::WebContents* source) override;
void RunFileChooser(content::WebContents* web_contents,
const content::FileChooserParams& params) override;
void EnumerateDirectory(content::WebContents* web_contents,
@ -201,6 +204,7 @@ class WebContents : public mate::EventEmitter,
const gfx::Size& new_size);
scoped_ptr<WebDialogHelper> web_dialog_helper_;
scoped_ptr<AtomJavaScriptDialogManager> dialog_manager_;
// Unique ID for a guest WebContents.
int guest_instance_id_;

View file

@ -27,7 +27,6 @@ void AtomJavaScriptDialogManager::RunBeforeUnloadDialog(
const base::string16& message_text,
bool is_reload,
const DialogClosedCallback& callback) {
bool prevent_reload = message_text.empty() ||
message_text == base::ASCIIToUTF16("false");
callback.Run(!prevent_reload, message_text);

View file

@ -0,0 +1,13 @@
<html>
<body>
<script type="text/javascript" charset="utf-8">
window.onbeforeunload = function() {
setTimeout(function() {
var ipc = require('ipc');
ipc.sendToHost('onbeforeunload');
}, 0);
return false;
}
</script>
</body>
</html>

View file

@ -178,3 +178,18 @@ describe '<webview> tag', ->
done()
webview.src = "file://#{fixtures}/pages/a.html"
document.body.appendChild webview
describe '<webview>.reload()', ->
it 'should emit beforeunload handler', (done) ->
webview.addEventListener 'did-finish-load', (e) ->
webview.reload()
listener = (e) ->
assert.equal e.channel, 'onbeforeunload'
webview.removeEventListener 'ipc-message', listener
done()
webview.addEventListener 'console-message', (e) ->
console.log(e)
webview.addEventListener 'ipc-message', listener
webview.setAttribute 'nodeintegration', 'on'
webview.src = "file://#{fixtures}/pages/beforeunload-false.html"
document.body.appendChild webview