diff --git a/atom/browser/api/atom_api_power_monitor.cc b/atom/browser/api/atom_api_power_monitor.cc index 88fbb4c97be5..90f32393f168 100644 --- a/atom/browser/api/atom_api_power_monitor.cc +++ b/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) diff --git a/atom/browser/browser.h b/atom/browser/browser.h index b5e31160c8f8..553d3648afa3 100644 --- a/atom/browser/browser.h +++ b/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 handler); + // Hide the application. void Hide(); diff --git a/atom/browser/browser_mac.mm b/atom/browser/browser_mac.mm index d12de2c14c9d..0faeee36f939 100644 --- a/atom/browser/browser_mac.mm +++ b/atom/browser/browser_mac.mm @@ -21,6 +21,10 @@ namespace atom { +void Browser::SetShutdownHandler(base::Callback handler) { + [[AtomApplication sharedApplication] setShutdownHandler:std::move(handler)]; +} + void Browser::Focus() { [[AtomApplication sharedApplication] activateIgnoringOtherApps:YES]; } diff --git a/atom/browser/lib/power_observer_linux.cc b/atom/browser/lib/power_observer_linux.cc index bff50db6953f..27da2b917624 100644 --- a/atom/browser/lib/power_observer_linux.cc +++ b/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(); diff --git a/atom/browser/mac/atom_application.h b/atom/browser/mac/atom_application.h index 45bb011cd772..243667b46039 100644 --- a/atom/browser/mac/atom_application.h +++ b/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 currentActivity_; NSCondition* handoffLock_; BOOL updateReceived_; + base::Callback shouldShutdown_; } + (AtomApplication*)sharedApplication; +- (void)setShutdownHandler:(base::Callback)handler; + // CrAppProtocol: - (BOOL)isHandlingSendEvent; diff --git a/atom/browser/mac/atom_application.mm b/atom/browser/mac/atom_application.mm index 96c99f5d22f1..68daa8c8b2b6 100644 --- a/atom/browser/mac/atom_application.mm +++ b/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)handler { + shouldShutdown_ = std::move(handler); +} + - (BOOL)isHandlingSendEvent { return handlingSendEvent_; } diff --git a/docs/api/power-monitor.md b/docs/api/power-monitor.md index eac6695c571c..5d600da48a94 100644 --- a/docs/api/power-monitor.md +++ b/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