refactor: ginify powerMonitor (#22751)

This commit is contained in:
Jeremy Apthorp 2020-03-24 09:03:29 -07:00 committed by GitHub
parent aeae0d47bd
commit 07cd70a37e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 216 additions and 176 deletions

View file

@ -9,6 +9,8 @@
#include <utility>
#include "base/bind.h"
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "device/bluetooth/dbus/bluez_dbus_thread_manager.h"
namespace {
@ -17,22 +19,19 @@ const char kLogindServiceName[] = "org.freedesktop.login1";
const char kLogindObjectPath[] = "/org/freedesktop/login1";
const char kLogindManagerInterface[] = "org.freedesktop.login1.Manager";
std::string get_executable_basename() {
char buf[4096];
size_t buf_size = sizeof(buf);
std::string rv("electron");
if (!uv_exepath(buf, &buf_size)) {
rv = strrchr(static_cast<const char*>(buf), '/') + 1;
}
return rv;
base::FilePath::StringType GetExecutableBaseName() {
return base::CommandLine::ForCurrentProcess()
->GetProgram()
.BaseName()
.value();
}
} // namespace
namespace electron {
PowerObserverLinux::PowerObserverLinux()
: lock_owner_name_(get_executable_basename()), weak_ptr_factory_(this) {
PowerObserverLinux::PowerObserverLinux(base::PowerObserver* observer)
: observer_(observer), lock_owner_name_(GetExecutableBaseName()) {
auto* bus = bluez::BluezDBusThreadManager::Get()->GetSystemBus();
if (!bus) {
LOG(WARNING) << "Failed to get system bus connection";
@ -114,6 +113,15 @@ void PowerObserverLinux::UnblockShutdown() {
}
void PowerObserverLinux::SetShutdownHandler(base::Callback<bool()> handler) {
// In order to delay system shutdown when e.preventDefault() is invoked
// on a powerMonitor 'shutdown' event, we need an org.freedesktop.login1
// shutdown delay lock. For more details see the "Taking Delay Locks"
// section of https://www.freedesktop.org/wiki/Software/systemd/inhibit/
if (handler && !should_shutdown_) {
BlockShutdown();
} else if (!handler && should_shutdown_) {
UnblockShutdown();
}
should_shutdown_ = std::move(handler);
}
@ -133,11 +141,13 @@ void PowerObserverLinux::OnPrepareForSleep(dbus::Signal* signal) {
return;
}
if (suspending) {
OnSuspend();
observer_->OnSuspend();
UnblockSleep();
} else {
BlockSleep();
OnResume();
observer_->OnResume();
}
}