2017-12-01 11:44:45 +00:00
|
|
|
// Copyright (c) 2017 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/lib/power_observer_linux.h"
|
2017-12-01 11:44:45 +00:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <uv.h>
|
|
|
|
#include <iostream>
|
2018-09-13 00:25:56 +00:00
|
|
|
#include <utility>
|
2017-12-01 11:44:45 +00:00
|
|
|
|
|
|
|
#include "base/bind.h"
|
2020-03-24 16:03:29 +00:00
|
|
|
#include "base/command_line.h"
|
|
|
|
#include "base/files/file_path.h"
|
2020-07-14 01:13:34 +00:00
|
|
|
#include "base/logging.h"
|
2019-01-12 01:00:43 +00:00
|
|
|
#include "device/bluetooth/dbus/bluez_dbus_thread_manager.h"
|
2017-12-01 11:44:45 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
const char kLogindServiceName[] = "org.freedesktop.login1";
|
|
|
|
const char kLogindObjectPath[] = "/org/freedesktop/login1";
|
|
|
|
const char kLogindManagerInterface[] = "org.freedesktop.login1.Manager";
|
|
|
|
|
2020-03-24 16:03:29 +00:00
|
|
|
base::FilePath::StringType GetExecutableBaseName() {
|
|
|
|
return base::CommandLine::ForCurrentProcess()
|
|
|
|
->GetProgram()
|
|
|
|
.BaseName()
|
|
|
|
.value();
|
2017-12-01 11:44:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
namespace electron {
|
2017-12-01 11:44:45 +00:00
|
|
|
|
2020-03-24 16:03:29 +00:00
|
|
|
PowerObserverLinux::PowerObserverLinux(base::PowerObserver* observer)
|
|
|
|
: observer_(observer), lock_owner_name_(GetExecutableBaseName()) {
|
2019-01-12 01:00:43 +00:00
|
|
|
auto* bus = bluez::BluezDBusThreadManager::Get()->GetSystemBus();
|
2018-10-09 05:32:19 +00:00
|
|
|
if (!bus) {
|
|
|
|
LOG(WARNING) << "Failed to get system bus connection";
|
|
|
|
return;
|
2017-12-01 11:44:45 +00:00
|
|
|
}
|
2018-10-09 05:32:19 +00:00
|
|
|
|
|
|
|
// set up the logind proxy
|
|
|
|
|
|
|
|
const auto weakThis = weak_ptr_factory_.GetWeakPtr();
|
|
|
|
|
|
|
|
logind_ = bus->GetObjectProxy(kLogindServiceName,
|
|
|
|
dbus::ObjectPath(kLogindObjectPath));
|
|
|
|
logind_->ConnectToSignal(
|
|
|
|
kLogindManagerInterface, "PrepareForShutdown",
|
|
|
|
base::BindRepeating(&PowerObserverLinux::OnPrepareForShutdown, weakThis),
|
|
|
|
base::BindRepeating(&PowerObserverLinux::OnSignalConnected, weakThis));
|
|
|
|
logind_->ConnectToSignal(
|
|
|
|
kLogindManagerInterface, "PrepareForSleep",
|
|
|
|
base::BindRepeating(&PowerObserverLinux::OnPrepareForSleep, weakThis),
|
|
|
|
base::BindRepeating(&PowerObserverLinux::OnSignalConnected, weakThis));
|
|
|
|
logind_->WaitForServiceToBeAvailable(base::BindRepeating(
|
|
|
|
&PowerObserverLinux::OnLoginServiceAvailable, weakThis));
|
2017-12-01 11:44:45 +00:00
|
|
|
}
|
|
|
|
|
2018-05-16 19:12:45 +00:00
|
|
|
PowerObserverLinux::~PowerObserverLinux() = default;
|
|
|
|
|
2017-12-01 11:44:45 +00:00
|
|
|
void PowerObserverLinux::OnLoginServiceAvailable(bool service_available) {
|
|
|
|
if (!service_available) {
|
|
|
|
LOG(WARNING) << kLogindServiceName << " not available";
|
|
|
|
return;
|
|
|
|
}
|
2017-12-07 13:10:40 +00:00
|
|
|
// Take sleep inhibit lock
|
2017-12-21 10:49:54 +00:00
|
|
|
BlockSleep();
|
2017-12-01 11:44:45 +00:00
|
|
|
}
|
|
|
|
|
2017-12-21 10:49:54 +00:00
|
|
|
void PowerObserverLinux::BlockSleep() {
|
2017-12-01 11:44:45 +00:00
|
|
|
dbus::MethodCall sleep_inhibit_call(kLogindManagerInterface, "Inhibit");
|
|
|
|
dbus::MessageWriter inhibit_writer(&sleep_inhibit_call);
|
2018-04-18 01:55:30 +00:00
|
|
|
inhibit_writer.AppendString("sleep"); // what
|
2017-12-01 11:44:45 +00:00
|
|
|
// Use the executable name as the lock owner, which will list rebrands of the
|
|
|
|
// electron executable as separate entities.
|
|
|
|
inhibit_writer.AppendString(lock_owner_name_); // who
|
|
|
|
inhibit_writer.AppendString("Application cleanup before suspend"); // why
|
|
|
|
inhibit_writer.AppendString("delay"); // mode
|
2018-10-09 05:32:19 +00:00
|
|
|
logind_->CallMethod(
|
|
|
|
&sleep_inhibit_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
|
|
|
|
base::BindOnce(&PowerObserverLinux::OnInhibitResponse,
|
|
|
|
weak_ptr_factory_.GetWeakPtr(), &sleep_lock_));
|
2017-12-01 11:44:45 +00:00
|
|
|
}
|
|
|
|
|
2017-12-21 10:49:54 +00:00
|
|
|
void PowerObserverLinux::UnblockSleep() {
|
|
|
|
sleep_lock_.reset();
|
|
|
|
}
|
|
|
|
|
2017-12-07 13:10:40 +00:00
|
|
|
void PowerObserverLinux::BlockShutdown() {
|
2017-12-21 11:12:25 +00:00
|
|
|
if (shutdown_lock_.is_valid()) {
|
2017-12-07 13:10:40 +00:00
|
|
|
LOG(WARNING) << "Trying to subscribe to shutdown multiple times";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dbus::MethodCall shutdown_inhibit_call(kLogindManagerInterface, "Inhibit");
|
|
|
|
dbus::MessageWriter inhibit_writer(&shutdown_inhibit_call);
|
|
|
|
inhibit_writer.AppendString("shutdown"); // what
|
|
|
|
inhibit_writer.AppendString(lock_owner_name_); // who
|
|
|
|
inhibit_writer.AppendString("Ensure a clean shutdown"); // why
|
|
|
|
inhibit_writer.AppendString("delay"); // mode
|
|
|
|
logind_->CallMethod(
|
|
|
|
&shutdown_inhibit_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
|
2018-10-09 05:32:19 +00:00
|
|
|
base::BindOnce(&PowerObserverLinux::OnInhibitResponse,
|
|
|
|
weak_ptr_factory_.GetWeakPtr(), &shutdown_lock_));
|
2017-12-07 13:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PowerObserverLinux::UnblockShutdown() {
|
2017-12-21 11:12:25 +00:00
|
|
|
if (!shutdown_lock_.is_valid()) {
|
2017-12-07 13:10:40 +00:00
|
|
|
LOG(WARNING)
|
|
|
|
<< "Trying to unsubscribe to shutdown without being subscribed";
|
|
|
|
return;
|
|
|
|
}
|
2017-12-21 11:12:25 +00:00
|
|
|
shutdown_lock_.reset();
|
2017-12-07 13:10:40 +00:00
|
|
|
}
|
|
|
|
|
2018-02-05 06:49:43 +00:00
|
|
|
void PowerObserverLinux::SetShutdownHandler(base::Callback<bool()> handler) {
|
2020-03-24 16:03:29 +00:00
|
|
|
// 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();
|
|
|
|
}
|
2018-02-05 06:49:43 +00:00
|
|
|
should_shutdown_ = std::move(handler);
|
|
|
|
}
|
|
|
|
|
2017-12-01 11:44:45 +00:00
|
|
|
void PowerObserverLinux::OnInhibitResponse(base::ScopedFD* scoped_fd,
|
|
|
|
dbus::Response* response) {
|
2018-10-09 05:32:19 +00:00
|
|
|
if (response != nullptr) {
|
|
|
|
dbus::MessageReader reader(response);
|
|
|
|
reader.PopFileDescriptor(scoped_fd);
|
|
|
|
}
|
2017-12-01 11:44:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PowerObserverLinux::OnPrepareForSleep(dbus::Signal* signal) {
|
|
|
|
dbus::MessageReader reader(signal);
|
2017-12-14 11:39:00 +00:00
|
|
|
bool suspending;
|
|
|
|
if (!reader.PopBool(&suspending)) {
|
2017-12-01 11:44:45 +00:00
|
|
|
LOG(ERROR) << "Invalid signal: " << signal->ToString();
|
|
|
|
return;
|
|
|
|
}
|
2017-12-14 11:39:00 +00:00
|
|
|
if (suspending) {
|
2020-03-24 16:03:29 +00:00
|
|
|
observer_->OnSuspend();
|
|
|
|
|
2017-12-21 10:49:54 +00:00
|
|
|
UnblockSleep();
|
2017-12-01 11:44:45 +00:00
|
|
|
} else {
|
2017-12-21 10:49:54 +00:00
|
|
|
BlockSleep();
|
2020-03-24 16:03:29 +00:00
|
|
|
|
|
|
|
observer_->OnResume();
|
2017-12-01 11:44:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-07 13:10:40 +00:00
|
|
|
void PowerObserverLinux::OnPrepareForShutdown(dbus::Signal* signal) {
|
|
|
|
dbus::MessageReader reader(signal);
|
2017-12-14 11:39:00 +00:00
|
|
|
bool shutting_down;
|
|
|
|
if (!reader.PopBool(&shutting_down)) {
|
2017-12-07 13:10:40 +00:00
|
|
|
LOG(ERROR) << "Invalid signal: " << signal->ToString();
|
|
|
|
return;
|
|
|
|
}
|
2017-12-14 11:39:00 +00:00
|
|
|
if (shutting_down) {
|
2018-02-05 07:13:35 +00:00
|
|
|
if (!should_shutdown_ || should_shutdown_.Run()) {
|
2017-12-07 13:10:40 +00:00
|
|
|
// The user didn't try to prevent shutdown. Release the lock and allow the
|
|
|
|
// shutdown to continue normally.
|
2017-12-21 11:12:25 +00:00
|
|
|
shutdown_lock_.reset();
|
2017-12-07 13:10:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-09 05:32:19 +00:00
|
|
|
void PowerObserverLinux::OnSignalConnected(const std::string& /*interface*/,
|
2017-12-01 11:44:45 +00:00
|
|
|
const std::string& signal,
|
|
|
|
bool success) {
|
|
|
|
LOG_IF(WARNING, !success) << "Failed to connect to " << signal;
|
|
|
|
}
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|