Simplify the public PowerObserver interface
This commit is contained in:
parent
e0e7dd2a8f
commit
8ae3d9dd0b
5 changed files with 23 additions and 34 deletions
|
@ -24,14 +24,6 @@ PowerMonitor::~PowerMonitor() {
|
||||||
base::PowerMonitor::Get()->RemoveObserver(this);
|
base::PowerMonitor::Get()->RemoveObserver(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PowerMonitor::BlockShutdown(mate::Arguments* args) {
|
|
||||||
atom::PowerObserver::BlockShutdown();
|
|
||||||
}
|
|
||||||
|
|
||||||
void PowerMonitor::UnblockShutdown(mate::Arguments* args) {
|
|
||||||
atom::PowerObserver::UnblockShutdown();
|
|
||||||
}
|
|
||||||
|
|
||||||
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");
|
||||||
|
@ -47,9 +39,11 @@ void PowerMonitor::OnResume() {
|
||||||
Emit("resume");
|
Emit("resume");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if defined(OS_LINUX)
|
||||||
bool PowerMonitor::OnShutdown() {
|
bool PowerMonitor::OnShutdown() {
|
||||||
return Emit("shutdown");
|
return Emit("shutdown");
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
// static
|
// static
|
||||||
v8::Local<v8::Value> PowerMonitor::Create(v8::Isolate* isolate) {
|
v8::Local<v8::Value> PowerMonitor::Create(v8::Isolate* isolate) {
|
||||||
|
@ -67,9 +61,11 @@ v8::Local<v8::Value> PowerMonitor::Create(v8::Isolate* isolate) {
|
||||||
void PowerMonitor::BuildPrototype(
|
void PowerMonitor::BuildPrototype(
|
||||||
v8::Isolate* isolate, v8::Local<v8::FunctionTemplate> prototype) {
|
v8::Isolate* isolate, v8::Local<v8::FunctionTemplate> prototype) {
|
||||||
prototype->SetClassName(mate::StringToV8(isolate, "PowerMonitor"));
|
prototype->SetClassName(mate::StringToV8(isolate, "PowerMonitor"));
|
||||||
|
#if defined(OS_LINUX)
|
||||||
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
|
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
|
||||||
.SetMethod("blockShutdown", &PowerMonitor::BlockShutdown)
|
.SetMethod("blockShutdown", &PowerMonitor::BlockShutdown)
|
||||||
.SetMethod("unblockShutdown", &PowerMonitor::UnblockShutdown);
|
.SetMethod("unblockShutdown", &PowerMonitor::UnblockShutdown);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace api
|
} // namespace api
|
||||||
|
|
|
@ -26,16 +26,15 @@ class PowerMonitor : public mate::TrackableObject<PowerMonitor>,
|
||||||
explicit PowerMonitor(v8::Isolate* isolate);
|
explicit PowerMonitor(v8::Isolate* isolate);
|
||||||
~PowerMonitor() override;
|
~PowerMonitor() override;
|
||||||
|
|
||||||
void BlockShutdown(mate::Arguments* args);
|
|
||||||
void UnblockShutdown(mate::Arguments* args);
|
|
||||||
|
|
||||||
// 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
|
// atom::PowerObserver
|
||||||
bool OnShutdown() override;
|
bool OnShutdown() override;
|
||||||
|
#endif
|
||||||
|
|
||||||
private:
|
private:
|
||||||
DISALLOW_COPY_AND_ASSIGN(PowerMonitor);
|
DISALLOW_COPY_AND_ASSIGN(PowerMonitor);
|
||||||
|
|
|
@ -18,19 +18,7 @@ namespace atom {
|
||||||
#if defined(OS_LINUX)
|
#if defined(OS_LINUX)
|
||||||
typedef PowerObserverLinux PowerObserver;
|
typedef PowerObserverLinux PowerObserver;
|
||||||
#else
|
#else
|
||||||
class PowerObserver : public base::PowerObserver {
|
typedef base::PowerObserver PowerObserver;
|
||||||
public:
|
|
||||||
PowerObserver() {}
|
|
||||||
void BlockShutdown() {}
|
|
||||||
void UnblockShutdown() {}
|
|
||||||
// Notification that the system is rebooting or shutting down. If the
|
|
||||||
// implementation returns true, the PowerObserver instance should try to delay
|
|
||||||
// OS shutdown so the application can perform cleanup before exiting.
|
|
||||||
virtual bool OnShutdown() { return false; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(PowerObserver);
|
|
||||||
};
|
|
||||||
#endif // defined(OS_LINUX)
|
#endif // defined(OS_LINUX)
|
||||||
|
|
||||||
} // namespace atom
|
} // namespace atom
|
||||||
|
|
|
@ -20,10 +20,12 @@ class PowerObserverLinux : public base::PowerObserver {
|
||||||
public:
|
public:
|
||||||
PowerObserverLinux();
|
PowerObserverLinux();
|
||||||
|
|
||||||
|
protected:
|
||||||
void BlockSleep();
|
void BlockSleep();
|
||||||
void UnblockSleep();
|
void UnblockSleep();
|
||||||
void BlockShutdown();
|
void BlockShutdown();
|
||||||
void UnblockShutdown();
|
void UnblockShutdown();
|
||||||
|
|
||||||
virtual bool OnShutdown() { return false; }
|
virtual bool OnShutdown() { return false; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -41,6 +43,7 @@ class PowerObserverLinux : public base::PowerObserver {
|
||||||
base::ScopedFD sleep_lock_;
|
base::ScopedFD sleep_lock_;
|
||||||
base::ScopedFD shutdown_lock_;
|
base::ScopedFD shutdown_lock_;
|
||||||
base::WeakPtrFactory<PowerObserverLinux> weak_ptr_factory_;
|
base::WeakPtrFactory<PowerObserverLinux> weak_ptr_factory_;
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(PowerObserverLinux);
|
DISALLOW_COPY_AND_ASSIGN(PowerObserverLinux);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,8 @@ const {powerMonitor, PowerMonitor} = process.atomBinding('power_monitor')
|
||||||
Object.setPrototypeOf(PowerMonitor.prototype, EventEmitter.prototype)
|
Object.setPrototypeOf(PowerMonitor.prototype, EventEmitter.prototype)
|
||||||
EventEmitter.call(powerMonitor)
|
EventEmitter.call(powerMonitor)
|
||||||
|
|
||||||
|
// On Linux we need to call blockShutdown() to subscribe to shutdown event.
|
||||||
|
if (process.platform === 'linux') {
|
||||||
powerMonitor.on('newListener', (event) => {
|
powerMonitor.on('newListener', (event) => {
|
||||||
if (event === 'shutdown' && powerMonitor.listenerCount('shutdown') === 0) {
|
if (event === 'shutdown' && powerMonitor.listenerCount('shutdown') === 0) {
|
||||||
powerMonitor.blockShutdown()
|
powerMonitor.blockShutdown()
|
||||||
|
@ -16,5 +18,6 @@ powerMonitor.on('removeListener', (event) => {
|
||||||
powerMonitor.unblockShutdown()
|
powerMonitor.unblockShutdown()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = powerMonitor
|
module.exports = powerMonitor
|
||||||
|
|
Loading…
Reference in a new issue