Implement shutdown event for macOS

This commit is contained in:
Cheng Zhao 2018-02-05 16:13:35 +09:00
parent c470e758cc
commit 983e1b1a70
7 changed files with 25 additions and 6 deletions

View file

@ -18,7 +18,9 @@ namespace api {
PowerMonitor::PowerMonitor(v8::Isolate* isolate) { PowerMonitor::PowerMonitor(v8::Isolate* isolate) {
#if defined(OS_LINUX) #if defined(OS_LINUX)
SetShutdownHandler(base::Bind(&PowerMonitor::ShouldShutdown, 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))); base::Unretained(this)));
#endif #endif
base::PowerMonitor::Get()->AddObserver(this); base::PowerMonitor::Get()->AddObserver(this);
@ -30,7 +32,7 @@ PowerMonitor::~PowerMonitor() {
} }
bool PowerMonitor::ShouldShutdown() { bool PowerMonitor::ShouldShutdown() {
return Emit("shutdown"); return !Emit("shutdown");
} }
#if defined(OS_LINUX) #if defined(OS_LINUX)

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -40,7 +40,7 @@ Emitted when the system changes to AC power.
Emitted when system changes to battery 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 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 invokes `e.preventDefault()`, Electron will attempt to delay system shutdown in