Do not use virtual function to request shutdown

Would make it easier to port to other platforms.
This commit is contained in:
Cheng Zhao 2018-02-05 15:49:43 +09:00
Родитель 8ae3d9dd0b
Коммит 109e2c760f
4 изменённых файлов: 36 добавлений и 13 удалений

Просмотреть файл

@ -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<v8::Value> PowerMonitor::Create(v8::Isolate* isolate) {
if (!Browser::Get()->is_ready()) {

Просмотреть файл

@ -26,16 +26,20 @@ class PowerMonitor : public mate::TrackableObject<PowerMonitor>,
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);
};

Просмотреть файл

@ -114,6 +114,10 @@ void PowerObserverLinux::UnblockShutdown() {
shutdown_lock_.reset();
}
void PowerObserverLinux::SetShutdownHandler(base::Callback<bool()> 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();

Просмотреть файл

@ -26,7 +26,7 @@ class PowerObserverLinux : public base::PowerObserver {
void BlockShutdown();
void UnblockShutdown();
virtual bool OnShutdown() { return false; }
void SetShutdownHandler(base::Callback<bool()> should_shutdown);
private:
void OnLoginServiceAvailable(bool available);
@ -37,6 +37,8 @@ class PowerObserverLinux : public base::PowerObserver {
const std::string& signal,
bool success);
base::Callback<bool()> should_shutdown_;
scoped_refptr<dbus::Bus> bus_;
scoped_refptr<dbus::ObjectProxy> logind_;
std::string lock_owner_name_;