]> khamidou — GitHub mirrors - electron.git/commitdiff
Add Browser class to controll when the application should quit.
authorCheng Zhao <zcbenz@gmail.com>
Thu, 2 May 2013 15:43:23 +0000 (23:43 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Thu, 2 May 2013 15:43:23 +0000 (23:43 +0800)
12 files changed:
atom.gyp
browser/atom_application_mac.mm
browser/atom_browser_main_parts.cc
browser/atom_browser_main_parts.h
browser/browser.cc [new file with mode: 0644]
browser/browser.h [new file with mode: 0644]
browser/browser_mac.mm [new file with mode: 0644]
browser/native_window.cc
browser/native_window.h
browser/window_list.cc
browser/window_list.h
browser/window_list_observer.h

index ae24c3ccc882c5367cde42b50802fe6a4951c0b3..e11e9c46662bcff435b5f84c568781c008d04bce 100644 (file)
--- a/atom.gyp
+++ b/atom.gyp
@@ -46,6 +46,9 @@
       'browser/atom_event_processing_window.mm',
       'browser/atom_javascript_dialog_manager.cc',
       'browser/atom_javascript_dialog_manager.h',
+      'browser/browser.cc',
+      'browser/browser.h',
+      'browser/browser_mac.mm',
       'browser/native_window.cc',
       'browser/native_window.h',
       'browser/native_window_mac.h',
index dbc13a79e597048edaf2b925a3c89ac1e9811e15..95b96111f3df17109327609cf5590d3c3af272f5 100644 (file)
@@ -5,8 +5,7 @@
 #import "browser/atom_application_mac.h"
 
 #include "base/auto_reset.h"
-#include "base/logging.h"
-#include "browser/window_list.h"
+#include "browser/browser.h"
 
 @implementation AtomApplication
 
 }
 
 - (IBAction)closeAllWindows:(id)sender {
-  atom::WindowList* window_list = atom::WindowList::GetInstance();
-  if (window_list->size() == 0)
-    [self terminate:self];
-
-  window_list->CloseAllWindows();
+  atom::Browser::Get()->Quit();
 }
 
 @end
index de11c5698ab8b22730b3215351ec935362e2599d..58d47bc72cf01a0f02ffafaeb4d269734f5eeed8 100644 (file)
@@ -7,6 +7,7 @@
 #include "browser/api/atom_browser_bindings.h"
 #include "browser/atom_browser_client.h"
 #include "browser/atom_browser_context.h"
+#include "browser/browser.h"
 #include "common/node_bindings.h"
 #include "vendor/node/src/node.h"
 #include "vendor/node/src/node_internals.h"
@@ -18,6 +19,7 @@ AtomBrowserMainParts* AtomBrowserMainParts::self_ = NULL;
 
 AtomBrowserMainParts::AtomBrowserMainParts()
     : atom_bindings_(new AtomBrowserBindings),
+      browser_(new Browser),
       node_bindings_(NodeBindings::Create(true)) {
   DCHECK(!self_) << "Cannot have two AtomBrowserMainParts";
   self_ = this;
index eaecd096f6b0aebe8b9c5ec756250cdc5a00e48d..06927e068e9fac9946a5a308b2417cbd532f1826 100644 (file)
@@ -10,6 +10,7 @@
 namespace atom {
 
 class AtomBrowserBindings;
+class Browser;
 class NodeBindings;
 
 class AtomBrowserMainParts : public brightray::BrowserMainParts {
@@ -20,6 +21,7 @@ class AtomBrowserMainParts : public brightray::BrowserMainParts {
   static AtomBrowserMainParts* Get();
 
   AtomBrowserBindings* atom_bindings() { return atom_bindings_.get(); }
+  Browser* browser() { return browser_.get(); }
 
  protected:
   // Implementations of brightray::BrowserMainParts.
@@ -32,6 +34,7 @@ class AtomBrowserMainParts : public brightray::BrowserMainParts {
 
  private:
   scoped_ptr<AtomBrowserBindings> atom_bindings_;
+  scoped_ptr<Browser> browser_;
   scoped_ptr<NodeBindings> node_bindings_;
 
   static AtomBrowserMainParts* self_;
diff --git a/browser/browser.cc b/browser/browser.cc
new file mode 100644 (file)
index 0000000..b85901f
--- /dev/null
@@ -0,0 +1,46 @@
+// 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/browser.h"
+
+#include "browser/atom_browser_main_parts.h"
+#include "browser/window_list.h"
+
+namespace atom {
+
+Browser::Browser()
+    : is_quiting_(false) {
+  WindowList::AddObserver(this);
+}
+
+Browser::~Browser() {
+  WindowList::RemoveObserver(this);
+}
+
+// static
+Browser* Browser::Get() {
+  return AtomBrowserMainParts::Get()->browser();
+}
+
+void Browser::Quit() {
+  atom::WindowList* window_list = atom::WindowList::GetInstance();
+  if (window_list->size() == 0)
+    Terminate();
+
+  is_quiting_ = true;
+  window_list->CloseAllWindows();
+}
+
+void Browser::OnWindowCloseCancelled(NativeWindow* window) {
+  // Once a beforeunload handler has prevented the closing, we think the quit
+  // is cancelled too.
+  is_quiting_ = false;
+}
+
+void Browser::OnWindowAllClosed() {
+  if (is_quiting_)
+    Terminate();
+}
+
+}  // namespace atom
diff --git a/browser/browser.h b/browser/browser.h
new file mode 100644 (file)
index 0000000..ef07e36
--- /dev/null
@@ -0,0 +1,43 @@
+// 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_BROWSER_BROWSER_H_
+#define ATOM_BROWSER_BROWSER_H_
+
+#include "base/basictypes.h"
+#include "base/compiler_specific.h"
+#include "browser/window_list_observer.h"
+
+namespace atom {
+
+// This class is used for control application-wide operations.
+class Browser : public WindowListObserver {
+ public:
+  Browser();
+  ~Browser();
+
+  static Browser* Get();
+
+  // Try to close all windows and quit the application.
+  void Quit();
+
+  // Quit the application immediately without cleanup work.
+  void Terminate();
+
+  bool is_quiting() const { return is_quiting_; }
+
+ protected:
+  bool is_quiting_;
+
+ private:
+  // WindowListObserver implementations:
+  virtual void OnWindowCloseCancelled(NativeWindow* window) OVERRIDE;
+  virtual void OnWindowAllClosed() OVERRIDE;
+
+  DISALLOW_COPY_AND_ASSIGN(Browser);
+};
+
+}  // namespace atom
+
+#endif  // ATOM_BROWSER_BROWSER_H_
diff --git a/browser/browser_mac.mm b/browser/browser_mac.mm
new file mode 100644 (file)
index 0000000..6738416
--- /dev/null
@@ -0,0 +1,15 @@
+// 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/browser.h"
+
+#import "browser/atom_application_mac.h"
+
+namespace atom {
+
+void Browser::Terminate() {
+  [[AtomApplication sharedApplication] terminate:nil];
+}
+
+}  // namespace atom
index ff0bfce71965fba5a24c78614a964216196c74c7..eb912695de218aa6cbb3f551004042e6905b0cd2 100644 (file)
@@ -188,6 +188,15 @@ content::JavaScriptDialogManager* NativeWindow::GetJavaScriptDialogManager() {
   return dialog_manager_.get();
 }
 
+void NativeWindow::BeforeUnloadFired(content::WebContents* tab,
+                                     bool proceed,
+                                     bool* proceed_to_fire_unload) {
+  *proceed_to_fire_unload = proceed;
+
+  if (!proceed)
+    WindowList::WindowCloseCancelled(this);
+}
+
 void NativeWindow::CloseContents(content::WebContents* source) {
   // When the web contents is gone, close the window immediately, but the
   // memory will not be freed until you call delete.
index acc56383351835055535ccd10afba40d0a4f2b8c..06095b9b5b651b169bf72c5d0acb12c471b3f644 100644 (file)
@@ -128,6 +128,9 @@ class NativeWindow : public content::WebContentsDelegate,
                                   content::WebContents* new_contents) OVERRIDE;
   virtual content::JavaScriptDialogManager*
       GetJavaScriptDialogManager() OVERRIDE;
+  virtual void BeforeUnloadFired(content::WebContents* tab,
+                                 bool proceed,
+                                 bool* proceed_to_fire_unload) OVERRIDE;
   virtual void CloseContents(content::WebContents* source) OVERRIDE;
 
   // Implementations of content::WebContentsObserver.
index 4d63b2e33d70e6c59fe2bdc7b5f1f1baf6e4c7e9..891ac5b03e69e8187996d5e09ad416411371da1c 100644 (file)
@@ -51,6 +51,12 @@ void WindowList::RemoveWindow(NativeWindow* window) {
                       OnWindowAllClosed());
 }
 
+// static
+void WindowList::WindowCloseCancelled(NativeWindow* window) {
+  FOR_EACH_OBSERVER(WindowListObserver, observers_.Get(),
+                    OnWindowCloseCancelled(window));
+}
+
 // static
 void WindowList::AddObserver(WindowListObserver* observer) {
   observers_.Get().AddObserver(observer);
index 0b9f0f9706d7cbb7f3230a02ca07f18fecfde842..b2ee7e58b963d4e2bbaf44339385455de557f0fb 100644 (file)
@@ -38,6 +38,9 @@ class WindowList {
   static void AddWindow(NativeWindow* window);
   static void RemoveWindow(NativeWindow* window);
 
+  // Called by window when a close is cancelled by beforeunload handler.
+  static void WindowCloseCancelled(NativeWindow* window);
+
   // Adds and removes |observer| from the observer list.
   static void AddObserver(WindowListObserver* observer);
   static void RemoveObserver(WindowListObserver* observer);
index ed7fa846a657fb97acfe9cc240caad78d6962a1e..76778cda79eb944683a47ab124d63ab0cedee7a3 100644 (file)
@@ -17,6 +17,9 @@ class WindowListObserver {
   // Called immediately after a window is removed from the list.
   virtual void OnWindowRemoved(NativeWindow* window) {}
 
+  // Called when a window close is cancelled by beforeunload handler.
+  virtual void OnWindowCloseCancelled(NativeWindow* window) {}
+
   // Called immediately after all windows are closed.
   virtual void OnWindowAllClosed() {}