Do not use virtual function to request shutdown
Would make it easier to port to other platforms.
This commit is contained in:
parent
8ae3d9dd0b
commit
109e2c760f
4 changed files with 36 additions and 13 deletions
|
@ -16,6 +16,11 @@ namespace atom {
|
||||||
namespace api {
|
namespace api {
|
||||||
|
|
||||||
PowerMonitor::PowerMonitor(v8::Isolate* isolate) {
|
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);
|
base::PowerMonitor::Get()->AddObserver(this);
|
||||||
Init(isolate);
|
Init(isolate);
|
||||||
}
|
}
|
||||||
|
@ -24,6 +29,20 @@ PowerMonitor::~PowerMonitor() {
|
||||||
base::PowerMonitor::Get()->RemoveObserver(this);
|
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) {
|
void PowerMonitor::OnPowerStateChange(bool on_battery_power) {
|
||||||
if (on_battery_power)
|
if (on_battery_power)
|
||||||
Emit("on-battery");
|
Emit("on-battery");
|
||||||
|
@ -39,12 +58,6 @@ void PowerMonitor::OnResume() {
|
||||||
Emit("resume");
|
Emit("resume");
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(OS_LINUX)
|
|
||||||
bool PowerMonitor::OnShutdown() {
|
|
||||||
return Emit("shutdown");
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// static
|
// static
|
||||||
v8::Local<v8::Value> PowerMonitor::Create(v8::Isolate* isolate) {
|
v8::Local<v8::Value> PowerMonitor::Create(v8::Isolate* isolate) {
|
||||||
if (!Browser::Get()->is_ready()) {
|
if (!Browser::Get()->is_ready()) {
|
||||||
|
|
|
@ -26,16 +26,20 @@ class PowerMonitor : public mate::TrackableObject<PowerMonitor>,
|
||||||
explicit PowerMonitor(v8::Isolate* isolate);
|
explicit PowerMonitor(v8::Isolate* isolate);
|
||||||
~PowerMonitor() override;
|
~PowerMonitor() override;
|
||||||
|
|
||||||
|
// Called by native calles.
|
||||||
|
bool ShouldShutdown();
|
||||||
|
|
||||||
|
#if defined(OS_LINUX)
|
||||||
|
// Private JS APIs.
|
||||||
|
void BlockShutdown();
|
||||||
|
void UnblockShutdown();
|
||||||
|
#endif
|
||||||
|
|
||||||
// base::PowerObserver implementations:
|
// base::PowerObserver implementations:
|
||||||
void OnPowerStateChange(bool on_battery_power) override;
|
void OnPowerStateChange(bool on_battery_power) override;
|
||||||
void OnSuspend() override;
|
void OnSuspend() override;
|
||||||
void OnResume() override;
|
void OnResume() override;
|
||||||
|
|
||||||
#if defined(OS_LINUX)
|
|
||||||
// atom::PowerObserver
|
|
||||||
bool OnShutdown() override;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
DISALLOW_COPY_AND_ASSIGN(PowerMonitor);
|
DISALLOW_COPY_AND_ASSIGN(PowerMonitor);
|
||||||
};
|
};
|
||||||
|
|
|
@ -114,6 +114,10 @@ void PowerObserverLinux::UnblockShutdown() {
|
||||||
shutdown_lock_.reset();
|
shutdown_lock_.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PowerObserverLinux::SetShutdownHandler(base::Callback<bool()> handler) {
|
||||||
|
should_shutdown_ = std::move(handler);
|
||||||
|
}
|
||||||
|
|
||||||
void PowerObserverLinux::OnInhibitResponse(base::ScopedFD* scoped_fd,
|
void PowerObserverLinux::OnInhibitResponse(base::ScopedFD* scoped_fd,
|
||||||
dbus::Response* response) {
|
dbus::Response* response) {
|
||||||
dbus::MessageReader reader(response);
|
dbus::MessageReader reader(response);
|
||||||
|
@ -144,7 +148,7 @@ void PowerObserverLinux::OnPrepareForShutdown(dbus::Signal* signal) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (shutting_down) {
|
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
|
// 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();
|
||||||
|
|
|
@ -26,7 +26,7 @@ class PowerObserverLinux : public base::PowerObserver {
|
||||||
void BlockShutdown();
|
void BlockShutdown();
|
||||||
void UnblockShutdown();
|
void UnblockShutdown();
|
||||||
|
|
||||||
virtual bool OnShutdown() { return false; }
|
void SetShutdownHandler(base::Callback<bool()> should_shutdown);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void OnLoginServiceAvailable(bool available);
|
void OnLoginServiceAvailable(bool available);
|
||||||
|
@ -37,6 +37,8 @@ class PowerObserverLinux : public base::PowerObserver {
|
||||||
const std::string& signal,
|
const std::string& signal,
|
||||||
bool success);
|
bool success);
|
||||||
|
|
||||||
|
base::Callback<bool()> should_shutdown_;
|
||||||
|
|
||||||
scoped_refptr<dbus::Bus> bus_;
|
scoped_refptr<dbus::Bus> bus_;
|
||||||
scoped_refptr<dbus::ObjectProxy> logind_;
|
scoped_refptr<dbus::ObjectProxy> logind_;
|
||||||
std::string lock_owner_name_;
|
std::string lock_owner_name_;
|
||||||
|
|
Loading…
Reference in a new issue