Implement beforeunload event.

Unlike normal browser which would show a dialog to choose whether to
continue, you can just return a empty string in the handler to prevent
unloading.
This commit is contained in:
Cheng Zhao 2013-04-30 23:56:50 +08:00
parent bcf6cd9f1b
commit a674572dd4
5 changed files with 73 additions and 0 deletions

View file

@ -39,6 +39,8 @@
'browser/atom_browser_main_parts.h', 'browser/atom_browser_main_parts.h',
'browser/atom_event_processing_window.h', 'browser/atom_event_processing_window.h',
'browser/atom_event_processing_window.mm', 'browser/atom_event_processing_window.mm',
'browser/atom_javascript_dialog_manager.cc',
'browser/atom_javascript_dialog_manager.h',
'browser/native_window.cc', 'browser/native_window.cc',
'browser/native_window.h', 'browser/native_window.h',
'browser/native_window_mac.h', 'browser/native_window_mac.h',

View file

@ -0,0 +1,22 @@
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "browser/atom_javascript_dialog_manager.h"
#include "base/utf_string_conversions.h"
namespace atom {
void AtomJavaScriptDialogManager::RunBeforeUnloadDialog(
content::WebContents* web_contents,
const string16& message_text,
bool is_reload,
const DialogClosedCallback& callback) {
bool prevent_reload = message_text.empty() ||
message_text == ASCIIToUTF16("false");
callback.Run(!prevent_reload, message_text);
}
} // namespace atom

View file

@ -0,0 +1,35 @@
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef ATOM_BROSER_ATOM_JAVASCRIPT_DIALOG_MANAGER_H_
#define ATOM_BROSER_ATOM_JAVASCRIPT_DIALOG_MANAGER_H_
#include "content/public/browser/javascript_dialog_manager.h"
namespace atom {
class AtomJavaScriptDialogManager : public content::JavaScriptDialogManager {
public:
// content::JavaScriptDialogManager implementations.
virtual void RunJavaScriptDialog(
content::WebContents* web_contents,
const GURL& origin_url,
const std::string& accept_lang,
content::JavaScriptMessageType javascript_message_type,
const string16& message_text,
const string16& default_prompt_text,
const DialogClosedCallback& callback,
bool* did_suppress_message) OVERRIDE {}
virtual void RunBeforeUnloadDialog(
content::WebContents* web_contents,
const string16& message_text,
bool is_reload,
const DialogClosedCallback& callback) OVERRIDE;
virtual void ResetJavaScriptState(
content::WebContents* web_contents) OVERRIDE {}
};
} // namespace atom
#endif // ATOM_BROSER_ATOM_JAVASCRIPT_DIALOG_MANAGER_H_

View file

@ -14,6 +14,7 @@
#include "browser/api/atom_browser_bindings.h" #include "browser/api/atom_browser_bindings.h"
#include "browser/atom_browser_context.h" #include "browser/atom_browser_context.h"
#include "browser/atom_browser_main_parts.h" #include "browser/atom_browser_main_parts.h"
#include "browser/atom_javascript_dialog_manager.h"
#include "content/public/browser/navigation_entry.h" #include "content/public/browser/navigation_entry.h"
#include "content/public/browser/notification_details.h" #include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_source.h" #include "content/public/browser/notification_source.h"
@ -153,6 +154,13 @@ void NativeWindow::WebContentsCreated(
window->InitFromOptions(options.get()); window->InitFromOptions(options.get());
} }
content::JavaScriptDialogManager* NativeWindow::GetJavaScriptDialogManager() {
if (!dialog_manager_)
dialog_manager_.reset(new AtomJavaScriptDialogManager);
return dialog_manager_.get();
}
bool NativeWindow::OnMessageReceived(const IPC::Message& message) { bool NativeWindow::OnMessageReceived(const IPC::Message& message) {
bool handled = true; bool handled = true;
IPC_BEGIN_MESSAGE_MAP(NativeWindow, message) IPC_BEGIN_MESSAGE_MAP(NativeWindow, message)

View file

@ -40,6 +40,8 @@ class Size;
namespace atom { namespace atom {
class AtomJavaScriptDialogManager;
class NativeWindow : public content::WebContentsDelegate, class NativeWindow : public content::WebContentsDelegate,
public content::WebContentsObserver, public content::WebContentsObserver,
public content::NotificationObserver { public content::NotificationObserver {
@ -115,6 +117,8 @@ class NativeWindow : public content::WebContentsDelegate,
const string16& frame_name, const string16& frame_name,
const GURL& target_url, const GURL& target_url,
content::WebContents* new_contents) OVERRIDE; content::WebContents* new_contents) OVERRIDE;
virtual content::JavaScriptDialogManager*
GetJavaScriptDialogManager() OVERRIDE;
// Implementations of content::WebContentsObserver. // Implementations of content::WebContentsObserver.
virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
@ -141,6 +145,8 @@ class NativeWindow : public content::WebContentsDelegate,
// Stores all windows. // Stores all windows.
static std::vector<NativeWindow*> windows_; static std::vector<NativeWindow*> windows_;
scoped_ptr<AtomJavaScriptDialogManager> dialog_manager_;
scoped_ptr<brightray::InspectableWebContents> inspectable_web_contents_; scoped_ptr<brightray::InspectableWebContents> inspectable_web_contents_;
DISALLOW_COPY_AND_ASSIGN(NativeWindow); DISALLOW_COPY_AND_ASSIGN(NativeWindow);