From 109e2c760f51d226ff4fc65e477bbc4e93da3304 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 5 Feb 2018 15:49:43 +0900 Subject: [PATCH] Do not use virtual function to request shutdown Would make it easier to port to other platforms. --- atom/browser/api/atom_api_power_monitor.cc | 25 ++++++++++++++++------ atom/browser/api/atom_api_power_monitor.h | 14 +++++++----- atom/browser/lib/power_observer_linux.cc | 6 +++++- atom/browser/lib/power_observer_linux.h | 4 +++- 4 files changed, 36 insertions(+), 13 deletions(-) diff --git a/atom/browser/api/atom_api_power_monitor.cc b/atom/browser/api/atom_api_power_monitor.cc index 2958c496a32..48194d0e3a9 100644 --- a/atom/browser/api/atom_api_power_monitor.cc +++ b/atom/browser/api/atom_api_power_monitor.cc @@ -16,6 +16,11 @@ namespace atom { 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))); +#endif base::PowerMonitor::Get()->AddObserver(this); Init(isolate); } @@ -24,6 +29,20 @@ PowerMonitor::~PowerMonitor() { base::PowerMonitor::Get()->RemoveObserver(this); } +bool PowerMonitor::ShouldShutdown() { + return Emit("shutdown"); +} + +#if defined(OS_LINUX) +void PowerMonitor::BlockShutdown() { + PowerObserverLinux::BlockShutdown(); +} + +void PowerMonitor::UnblockShutdown() { + PowerObserverLinux::UnblockShutdown(); +} +#endif + void PowerMonitor::OnPowerStateChange(bool on_battery_power) { if (on_battery_power) Emit("on-battery"); @@ -39,12 +58,6 @@ void PowerMonitor::OnResume() { Emit("resume"); } -#if defined(OS_LINUX) -bool PowerMonitor::OnShutdown() { - return Emit("shutdown"); -} -#endif - // static v8::Local PowerMonitor::Create(v8::Isolate* isolate) { if (!Browser::Get()->is_ready()) { diff --git a/atom/browser/api/atom_api_power_monitor.h b/atom/browser/api/atom_api_power_monitor.h index 8724aa50afc..90971556de8 100644 --- a/atom/browser/api/atom_api_power_monitor.h +++ b/atom/browser/api/atom_api_power_monitor.h @@ -26,16 +26,20 @@ class PowerMonitor : public mate::TrackableObject, explicit PowerMonitor(v8::Isolate* isolate); ~PowerMonitor() override; + // Called by native calles. + bool ShouldShutdown(); + +#if defined(OS_LINUX) + // Private JS APIs. + void BlockShutdown(); + void UnblockShutdown(); +#endif + // base::PowerObserver implementations: void OnPowerStateChange(bool on_battery_power) override; void OnSuspend() override; void OnResume() override; -#if defined(OS_LINUX) - // atom::PowerObserver - bool OnShutdown() override; -#endif - private: DISALLOW_COPY_AND_ASSIGN(PowerMonitor); }; diff --git a/atom/browser/lib/power_observer_linux.cc b/atom/browser/lib/power_observer_linux.cc index ad3cc82f478..bff50db6953 100644 --- a/atom/browser/lib/power_observer_linux.cc +++ b/atom/browser/lib/power_observer_linux.cc @@ -114,6 +114,10 @@ void PowerObserverLinux::UnblockShutdown() { shutdown_lock_.reset(); } +void PowerObserverLinux::SetShutdownHandler(base::Callback handler) { + should_shutdown_ = std::move(handler); +} + void PowerObserverLinux::OnInhibitResponse(base::ScopedFD* scoped_fd, dbus::Response* response) { dbus::MessageReader reader(response); @@ -144,7 +148,7 @@ void PowerObserverLinux::OnPrepareForShutdown(dbus::Signal* signal) { return; } if (shutting_down) { - if (!OnShutdown()) { + 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/lib/power_observer_linux.h b/atom/browser/lib/power_observer_linux.h index a76ecaf3540..c2cb1d80150 100644 --- a/atom/browser/lib/power_observer_linux.h +++ b/atom/browser/lib/power_observer_linux.h @@ -26,7 +26,7 @@ class PowerObserverLinux : public base::PowerObserver { void BlockShutdown(); void UnblockShutdown(); - virtual bool OnShutdown() { return false; } + void SetShutdownHandler(base::Callback should_shutdown); private: void OnLoginServiceAvailable(bool available); @@ -37,6 +37,8 @@ class PowerObserverLinux : public base::PowerObserver { const std::string& signal, bool success); + base::Callback should_shutdown_; + scoped_refptr bus_; scoped_refptr logind_; std::string lock_owner_name_;