Browse Source

Implement shutdown event for macOS

Cheng Zhao 7 years ago
parent
commit
983e1b1a70

+ 4 - 2
atom/browser/api/atom_api_power_monitor.cc

@@ -18,8 +18,10 @@ namespace api {
 PowerMonitor::PowerMonitor(v8::Isolate* isolate) {
 #if defined(OS_LINUX)
   SetShutdownHandler(base::Bind(&PowerMonitor::ShouldShutdown,
-                                // Passed to base class, no need for weak ptr.
                                 base::Unretained(this)));
+#elif defined(OS_MACOSX)
+  Browser::Get()->SetShutdownHandler(base::Bind(&PowerMonitor::ShouldShutdown,
+                                                base::Unretained(this)));
 #endif
   base::PowerMonitor::Get()->AddObserver(this);
   Init(isolate);
@@ -30,7 +32,7 @@ PowerMonitor::~PowerMonitor() {
 }
 
 bool PowerMonitor::ShouldShutdown() {
-  return Emit("shutdown");
+  return !Emit("shutdown");
 }
 
 #if defined(OS_LINUX)

+ 3 - 0
atom/browser/browser.h

@@ -105,6 +105,9 @@ class Browser : public WindowListObserver {
   LoginItemSettings GetLoginItemSettings(const LoginItemSettings& options);
 
 #if defined(OS_MACOSX)
+  // Set the handler which decides whether to shutdown.
+  void SetShutdownHandler(base::Callback<bool()> handler);
+
   // Hide the application.
   void Hide();
 

+ 4 - 0
atom/browser/browser_mac.mm

@@ -21,6 +21,10 @@
 
 namespace atom {
 
+void Browser::SetShutdownHandler(base::Callback<bool()> handler) {
+  [[AtomApplication sharedApplication] setShutdownHandler:std::move(handler)];
+}
+
 void Browser::Focus() {
   [[AtomApplication sharedApplication] activateIgnoringOtherApps:YES];
 }

+ 1 - 1
atom/browser/lib/power_observer_linux.cc

@@ -148,7 +148,7 @@ void PowerObserverLinux::OnPrepareForShutdown(dbus::Signal* signal) {
     return;
   }
   if (shutting_down) {
-    if (!should_shutdown_ || !should_shutdown_.Run()) {
+    if (!should_shutdown_ || should_shutdown_.Run()) {
       // The user didn't try to prevent shutdown. Release the lock and allow the
       // shutdown to continue normally.
       shutdown_lock_.reset();

+ 6 - 2
atom/browser/mac/atom_application.h

@@ -2,8 +2,9 @@
 // Use of this source code is governed by the MIT license that can be
 // found in the LICENSE file.
 
-#import "base/mac/scoped_sending_event.h"
-#import "base/mac/scoped_nsobject.h"
+#include "base/callback.h"
+#include "base/mac/scoped_sending_event.h"
+#include "base/mac/scoped_nsobject.h"
 
 @interface AtomApplication : NSApplication<CrAppProtocol,
                                            CrAppControlProtocol,
@@ -13,10 +14,13 @@
   base::scoped_nsobject<NSUserActivity> currentActivity_;
   NSCondition* handoffLock_;
   BOOL updateReceived_;
+  base::Callback<bool()> shouldShutdown_;
 }
 
 + (AtomApplication*)sharedApplication;
 
+- (void)setShutdownHandler:(base::Callback<bool()>)handler;
+
 // CrAppProtocol:
 - (BOOL)isHandlingSendEvent;
 

+ 6 - 0
atom/browser/mac/atom_application.mm

@@ -29,10 +29,16 @@ inline void dispatch_sync_main(dispatch_block_t block) {
 }
 
 - (void)terminate:(id)sender {
+  if (shouldShutdown_ && !shouldShutdown_.Run())
+    return;  // User will call Quit later.
   AtomApplicationDelegate* atomDelegate = (AtomApplicationDelegate*) [NSApp delegate];
   [atomDelegate tryToTerminateApp:self];
 }
 
+- (void)setShutdownHandler:(base::Callback<bool()>)handler {
+  shouldShutdown_ = std::move(handler);
+}
+
 - (BOOL)isHandlingSendEvent {
   return handlingSendEvent_;
 }

+ 1 - 1
docs/api/power-monitor.md

@@ -40,7 +40,7 @@ Emitted when the system changes to AC power.
 
 Emitted when system changes to battery power.
 
-### Event: 'shutdown' _Linux_
+### Event: 'shutdown' _Linux_ _macOS_
 
 Emitted when the system is about to reboot or shut down. If the event handler
 invokes `e.preventDefault()`, Electron will attempt to delay system shutdown in