power_observer_linux.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Copyright (c) 2017 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "atom/browser/lib/power_observer_linux.h"
  5. #include <unistd.h>
  6. #include <uv.h>
  7. #include <iostream>
  8. #include "base/bind.h"
  9. #include "device/bluetooth/dbus/dbus_thread_manager_linux.h"
  10. namespace {
  11. const char kLogindServiceName[] = "org.freedesktop.login1";
  12. const char kLogindObjectPath[] = "/org/freedesktop/login1";
  13. const char kLogindManagerInterface[] = "org.freedesktop.login1.Manager";
  14. std::string get_executable_basename() {
  15. char buf[4096];
  16. size_t buf_size = sizeof(buf);
  17. std::string rv("electron");
  18. if (!uv_exepath(buf, &buf_size)) {
  19. rv = strrchr(static_cast<const char*>(buf), '/') + 1;
  20. }
  21. return rv;
  22. }
  23. } // namespace
  24. namespace atom {
  25. PowerObserverLinux::PowerObserverLinux()
  26. : lock_owner_name_(get_executable_basename()), weak_ptr_factory_(this) {
  27. auto* dbus_thread_manager = bluez::DBusThreadManagerLinux::Get();
  28. if (dbus_thread_manager) {
  29. bus_ = dbus_thread_manager->GetSystemBus();
  30. if (bus_) {
  31. logind_ = bus_->GetObjectProxy(kLogindServiceName,
  32. dbus::ObjectPath(kLogindObjectPath));
  33. logind_->WaitForServiceToBeAvailable(
  34. base::Bind(&PowerObserverLinux::OnLoginServiceAvailable,
  35. weak_ptr_factory_.GetWeakPtr()));
  36. } else {
  37. LOG(WARNING) << "Failed to get system bus connection";
  38. }
  39. } else {
  40. LOG(WARNING) << "DBusThreadManagerLinux instance isn't available";
  41. }
  42. }
  43. PowerObserverLinux::~PowerObserverLinux() = default;
  44. void PowerObserverLinux::OnLoginServiceAvailable(bool service_available) {
  45. if (!service_available) {
  46. LOG(WARNING) << kLogindServiceName << " not available";
  47. return;
  48. }
  49. // Connect to PrepareForShutdown/PrepareForSleep signals
  50. logind_->ConnectToSignal(kLogindManagerInterface, "PrepareForShutdown",
  51. base::Bind(&PowerObserverLinux::OnPrepareForShutdown,
  52. weak_ptr_factory_.GetWeakPtr()),
  53. base::Bind(&PowerObserverLinux::OnSignalConnected,
  54. weak_ptr_factory_.GetWeakPtr()));
  55. logind_->ConnectToSignal(kLogindManagerInterface, "PrepareForSleep",
  56. base::Bind(&PowerObserverLinux::OnPrepareForSleep,
  57. weak_ptr_factory_.GetWeakPtr()),
  58. base::Bind(&PowerObserverLinux::OnSignalConnected,
  59. weak_ptr_factory_.GetWeakPtr()));
  60. // Take sleep inhibit lock
  61. BlockSleep();
  62. }
  63. void PowerObserverLinux::BlockSleep() {
  64. dbus::MethodCall sleep_inhibit_call(kLogindManagerInterface, "Inhibit");
  65. dbus::MessageWriter inhibit_writer(&sleep_inhibit_call);
  66. inhibit_writer.AppendString("sleep"); // what
  67. // Use the executable name as the lock owner, which will list rebrands of the
  68. // electron executable as separate entities.
  69. inhibit_writer.AppendString(lock_owner_name_); // who
  70. inhibit_writer.AppendString("Application cleanup before suspend"); // why
  71. inhibit_writer.AppendString("delay"); // mode
  72. logind_->CallMethod(&sleep_inhibit_call,
  73. dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
  74. base::Bind(&PowerObserverLinux::OnInhibitResponse,
  75. weak_ptr_factory_.GetWeakPtr(), &sleep_lock_));
  76. }
  77. void PowerObserverLinux::UnblockSleep() {
  78. sleep_lock_.reset();
  79. }
  80. void PowerObserverLinux::BlockShutdown() {
  81. if (shutdown_lock_.is_valid()) {
  82. LOG(WARNING) << "Trying to subscribe to shutdown multiple times";
  83. return;
  84. }
  85. dbus::MethodCall shutdown_inhibit_call(kLogindManagerInterface, "Inhibit");
  86. dbus::MessageWriter inhibit_writer(&shutdown_inhibit_call);
  87. inhibit_writer.AppendString("shutdown"); // what
  88. inhibit_writer.AppendString(lock_owner_name_); // who
  89. inhibit_writer.AppendString("Ensure a clean shutdown"); // why
  90. inhibit_writer.AppendString("delay"); // mode
  91. logind_->CallMethod(
  92. &shutdown_inhibit_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
  93. base::Bind(&PowerObserverLinux::OnInhibitResponse,
  94. weak_ptr_factory_.GetWeakPtr(), &shutdown_lock_));
  95. }
  96. void PowerObserverLinux::UnblockShutdown() {
  97. if (!shutdown_lock_.is_valid()) {
  98. LOG(WARNING)
  99. << "Trying to unsubscribe to shutdown without being subscribed";
  100. return;
  101. }
  102. shutdown_lock_.reset();
  103. }
  104. void PowerObserverLinux::SetShutdownHandler(base::Callback<bool()> handler) {
  105. should_shutdown_ = std::move(handler);
  106. }
  107. void PowerObserverLinux::OnInhibitResponse(base::ScopedFD* scoped_fd,
  108. dbus::Response* response) {
  109. dbus::MessageReader reader(response);
  110. reader.PopFileDescriptor(scoped_fd);
  111. }
  112. void PowerObserverLinux::OnPrepareForSleep(dbus::Signal* signal) {
  113. dbus::MessageReader reader(signal);
  114. bool suspending;
  115. if (!reader.PopBool(&suspending)) {
  116. LOG(ERROR) << "Invalid signal: " << signal->ToString();
  117. return;
  118. }
  119. if (suspending) {
  120. OnSuspend();
  121. UnblockSleep();
  122. } else {
  123. BlockSleep();
  124. OnResume();
  125. }
  126. }
  127. void PowerObserverLinux::OnPrepareForShutdown(dbus::Signal* signal) {
  128. dbus::MessageReader reader(signal);
  129. bool shutting_down;
  130. if (!reader.PopBool(&shutting_down)) {
  131. LOG(ERROR) << "Invalid signal: " << signal->ToString();
  132. return;
  133. }
  134. if (shutting_down) {
  135. if (!should_shutdown_ || should_shutdown_.Run()) {
  136. // The user didn't try to prevent shutdown. Release the lock and allow the
  137. // shutdown to continue normally.
  138. shutdown_lock_.reset();
  139. }
  140. }
  141. }
  142. void PowerObserverLinux::OnSignalConnected(const std::string& interface,
  143. const std::string& signal,
  144. bool success) {
  145. LOG_IF(WARNING, !success) << "Failed to connect to " << signal;
  146. }
  147. } // namespace atom