Implement beforeunload event.
authorCheng Zhao <zcbenz@gmail.com>
Tue, 30 Apr 2013 15:56:50 +0000 (23:56 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Tue, 30 Apr 2013 16:05:19 +0000 (00:05 +0800)
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.

atom.gyp
browser/atom_javascript_dialog_manager.cc [new file with mode: 0644]
browser/atom_javascript_dialog_manager.h [new file with mode: 0644]
browser/native_window.cc
browser/native_window.h

index fd4c746..d50255a 100644 (file)
--- a/atom.gyp
+++ b/atom.gyp
@@ -39,6 +39,8 @@
       'browser/atom_browser_main_parts.h',
       'browser/atom_event_processing_window.h',
       '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.h',
       'browser/native_window_mac.h',
diff --git a/browser/atom_javascript_dialog_manager.cc b/browser/atom_javascript_dialog_manager.cc
new file mode 100644 (file)
index 0000000..83f80a8
--- /dev/null
@@ -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
diff --git a/browser/atom_javascript_dialog_manager.h b/browser/atom_javascript_dialog_manager.h
new file mode 100644 (file)
index 0000000..cdeada7
--- /dev/null
@@ -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_
index 69d0d0f..fe7befb 100644 (file)
@@ -14,6 +14,7 @@
 #include "browser/api/atom_browser_bindings.h"
 #include "browser/atom_browser_context.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/notification_details.h"
 #include "content/public/browser/notification_source.h"
@@ -153,6 +154,13 @@ void NativeWindow::WebContentsCreated(
   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 handled = true;
   IPC_BEGIN_MESSAGE_MAP(NativeWindow, message)
index 440ec80..296c136 100644 (file)
@@ -40,6 +40,8 @@ class Size;
 
 namespace atom {
 
+class AtomJavaScriptDialogManager;
+
 class NativeWindow : public content::WebContentsDelegate,
                      public content::WebContentsObserver,
                      public content::NotificationObserver {
@@ -115,6 +117,8 @@ class NativeWindow : public content::WebContentsDelegate,
                                   const string16& frame_name,
                                   const GURL& target_url,
                                   content::WebContents* new_contents) OVERRIDE;
+  virtual content::JavaScriptDialogManager*
+      GetJavaScriptDialogManager() OVERRIDE;
 
   // Implementations of content::WebContentsObserver.
   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
@@ -141,6 +145,8 @@ class NativeWindow : public content::WebContentsDelegate,
   // Stores all windows.
   static std::vector<NativeWindow*> windows_;
 
+  scoped_ptr<AtomJavaScriptDialogManager> dialog_manager_;
+
   scoped_ptr<brightray::InspectableWebContents> inspectable_web_contents_;
 
   DISALLOW_COPY_AND_ASSIGN(NativeWindow);