fixup! Implement powerMonitor 'shutdown' event for Linux.

This commit is contained in:
Thiago de Arruda 2017-12-21 08:12:25 -03:00 committed by Cheng Zhao
parent 176c03fa15
commit e0e7dd2a8f
2 changed files with 6 additions and 9 deletions

View file

@ -16,10 +16,6 @@ const char kLogindServiceName[] = "org.freedesktop.login1";
const char kLogindObjectPath[] = "/org/freedesktop/login1"; const char kLogindObjectPath[] = "/org/freedesktop/login1";
const char kLogindManagerInterface[] = "org.freedesktop.login1.Manager"; const char kLogindManagerInterface[] = "org.freedesktop.login1.Manager";
// Store shutdown lock as a global, since we only want to release it when the
// main process exits.
base::ScopedFD shutdown_lock;
std::string get_executable_basename() { std::string get_executable_basename() {
char buf[4096]; char buf[4096];
size_t buf_size = sizeof(buf); size_t buf_size = sizeof(buf);
@ -93,7 +89,7 @@ void PowerObserverLinux::UnblockSleep() {
} }
void PowerObserverLinux::BlockShutdown() { void PowerObserverLinux::BlockShutdown() {
if (shutdown_lock.is_valid()) { if (shutdown_lock_.is_valid()) {
LOG(WARNING) << "Trying to subscribe to shutdown multiple times"; LOG(WARNING) << "Trying to subscribe to shutdown multiple times";
return; return;
} }
@ -106,16 +102,16 @@ void PowerObserverLinux::BlockShutdown() {
logind_->CallMethod( logind_->CallMethod(
&shutdown_inhibit_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, &shutdown_inhibit_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::Bind(&PowerObserverLinux::OnInhibitResponse, base::Bind(&PowerObserverLinux::OnInhibitResponse,
weak_ptr_factory_.GetWeakPtr(), &shutdown_lock)); weak_ptr_factory_.GetWeakPtr(), &shutdown_lock_));
} }
void PowerObserverLinux::UnblockShutdown() { void PowerObserverLinux::UnblockShutdown() {
if (!shutdown_lock.is_valid()) { if (!shutdown_lock_.is_valid()) {
LOG(WARNING) LOG(WARNING)
<< "Trying to unsubscribe to shutdown without being subscribed"; << "Trying to unsubscribe to shutdown without being subscribed";
return; return;
} }
shutdown_lock.reset(); shutdown_lock_.reset();
} }
void PowerObserverLinux::OnInhibitResponse(base::ScopedFD* scoped_fd, void PowerObserverLinux::OnInhibitResponse(base::ScopedFD* scoped_fd,
@ -151,7 +147,7 @@ void PowerObserverLinux::OnPrepareForShutdown(dbus::Signal* signal) {
if (!OnShutdown()) { if (!OnShutdown()) {
// 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

@ -39,6 +39,7 @@ class PowerObserverLinux : public base::PowerObserver {
scoped_refptr<dbus::ObjectProxy> logind_; scoped_refptr<dbus::ObjectProxy> logind_;
std::string lock_owner_name_; std::string lock_owner_name_;
base::ScopedFD sleep_lock_; base::ScopedFD sleep_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);
}; };