diff --git a/BUILD.gn b/BUILD.gn index a8cf07488..f59fa772b 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -222,6 +222,7 @@ static_library("electron_lib") { "//ppapi/host", "//ppapi/proxy", "//ppapi/shared_impl", + "//services/device/public/mojom", "//services/proxy_resolver:lib", "//skia", "//third_party/blink/public:blink", @@ -237,12 +238,6 @@ static_library("electron_lib") { "//v8", ] - # TODO: this requires a visibility patch to chromium src. it would be better - # to use the publicly-available API, which I think is mojo-based. Once - # electron switches to using the public API, we can remove this from the deps - # list and remove the visibility patch from chromium. - deps += [ "//services/device/wake_lock/power_save_blocker" ] - include_dirs = [ "chromium_src", ".", diff --git a/atom/browser/api/atom_api_power_save_blocker.cc b/atom/browser/api/atom_api_power_save_blocker.cc index 8d2ba1f7a..96889242e 100644 --- a/atom/browser/api/atom_api_power_save_blocker.cc +++ b/atom/browser/api/atom_api_power_save_blocker.cc @@ -8,7 +8,11 @@ #include "base/task_scheduler/post_task.h" #include "base/threading/thread_task_runner_handle.h" +#include "content/public/common/service_manager_connection.h" #include "native_mate/dictionary.h" +#include "services/device/public/mojom/constants.mojom.h" +#include "services/device/public/mojom/wake_lock_provider.mojom.h" +#include "services/service_manager/public/cpp/connector.h" #include "atom/common/node_includes.h" @@ -39,16 +43,19 @@ namespace atom { namespace api { PowerSaveBlocker::PowerSaveBlocker(v8::Isolate* isolate) - : current_blocker_type_( - device::mojom::WakeLockType::kPreventAppSuspension) { + : current_lock_type_(device::mojom::WakeLockType::kPreventAppSuspension), + is_wake_lock_active_(false) { Init(isolate); } PowerSaveBlocker::~PowerSaveBlocker() {} void PowerSaveBlocker::UpdatePowerSaveBlocker() { - if (power_save_blocker_types_.empty()) { - power_save_blocker_.reset(); + if (wake_lock_types_.empty()) { + if (is_wake_lock_active_) { + GetWakeLock()->CancelWakeLock(); + is_wake_lock_active_ = false; + } return; } @@ -58,45 +65,57 @@ void PowerSaveBlocker::UpdatePowerSaveBlocker() { // higher precedence level than |WakeLockType::kPreventAppSuspension|. // // Only the highest-precedence blocker type takes effect. - device::mojom::WakeLockType new_blocker_type = + device::mojom::WakeLockType new_lock_type = device::mojom::WakeLockType::kPreventAppSuspension; - for (const auto& element : power_save_blocker_types_) { + for (const auto& element : wake_lock_types_) { if (element.second == device::mojom::WakeLockType::kPreventDisplaySleep) { - new_blocker_type = device::mojom::WakeLockType::kPreventDisplaySleep; + new_lock_type = device::mojom::WakeLockType::kPreventDisplaySleep; break; } } - if (!power_save_blocker_ || new_blocker_type != current_blocker_type_) { - auto new_blocker = std::make_unique( - new_blocker_type, device::mojom::WakeLockReason::kOther, - ATOM_PRODUCT_NAME, base::ThreadTaskRunnerHandle::Get(), - // This task runner may be used by some device service - // implementation bits to interface with dbus client code, which in - // turn imposes some subtle thread affinity on the clients. We - // therefore require a single-thread runner. - base::CreateSingleThreadTaskRunnerWithTraits( - {base::MayBlock(), base::TaskPriority::BACKGROUND})); - power_save_blocker_.swap(new_blocker); - current_blocker_type_ = new_blocker_type; + if (current_lock_type_ != new_lock_type) { + GetWakeLock()->ChangeType(new_lock_type, base::DoNothing()); + current_lock_type_ = new_lock_type; } + if (!is_wake_lock_active_) { + GetWakeLock()->RequestWakeLock(); + is_wake_lock_active_ = true; + } +} + +device::mojom::WakeLock* PowerSaveBlocker::GetWakeLock() { + if (!wake_lock_) { + device::mojom::WakeLockProviderPtr wake_lock_provider; + DCHECK(content::ServiceManagerConnection::GetForProcess()); + auto* connector = + content::ServiceManagerConnection::GetForProcess()->GetConnector(); + connector->BindInterface(device::mojom::kServiceName, + mojo::MakeRequest(&wake_lock_provider)); + + wake_lock_provider->GetWakeLockWithoutContext( + device::mojom::WakeLockType::kPreventAppSuspension, + device::mojom::WakeLockReason::kOther, ATOM_PRODUCT_NAME, + mojo::MakeRequest(&wake_lock_)); + } + return wake_lock_.get(); } int PowerSaveBlocker::Start(device::mojom::WakeLockType type) { static int count = 0; - power_save_blocker_types_[count] = type; + wake_lock_types_[count] = type; UpdatePowerSaveBlocker(); return count++; } bool PowerSaveBlocker::Stop(int id) { - bool success = power_save_blocker_types_.erase(id) > 0; + bool success = wake_lock_types_.erase(id) > 0; UpdatePowerSaveBlocker(); return success; } bool PowerSaveBlocker::IsStarted(int id) { - return power_save_blocker_types_.find(id) != power_save_blocker_types_.end(); + return wake_lock_types_.find(id) != wake_lock_types_.end(); } // static diff --git a/atom/browser/api/atom_api_power_save_blocker.h b/atom/browser/api/atom_api_power_save_blocker.h index d56222db9..d2feffc09 100644 --- a/atom/browser/api/atom_api_power_save_blocker.h +++ b/atom/browser/api/atom_api_power_save_blocker.h @@ -10,7 +10,7 @@ #include "atom/browser/api/trackable_object.h" #include "native_mate/handle.h" -#include "services/device/wake_lock/power_save_blocker/power_save_blocker.h" +#include "services/device/public/mojom/wake_lock.mojom.h" namespace mate { class Dictionary; @@ -37,14 +37,19 @@ class PowerSaveBlocker : public mate::TrackableObject { bool Stop(int id); bool IsStarted(int id); - std::unique_ptr power_save_blocker_; + device::mojom::WakeLock* GetWakeLock(); - // Current blocker type used by |power_save_blocker_| - device::mojom::WakeLockType current_blocker_type_; + // Current wake lock level. + device::mojom::WakeLockType current_lock_type_; + + // Whether the wake lock is currently active. + bool is_wake_lock_active_; // Map from id to the corresponding blocker type for each request. using WakeLockTypeMap = std::map; - WakeLockTypeMap power_save_blocker_types_; + WakeLockTypeMap wake_lock_types_; + + device::mojom::WakeLockPtr wake_lock_; DISALLOW_COPY_AND_ASSIGN(PowerSaveBlocker); }; diff --git a/patches/common/chromium/.patches.yaml b/patches/common/chromium/.patches.yaml index d550036dd..0321da1bb 100644 --- a/patches/common/chromium/.patches.yaml +++ b/patches/common/chromium/.patches.yaml @@ -172,10 +172,6 @@ patches: author: null file: webui_in_subframes.patch description: null -- - author: Aleksei Kuzmin - file: statically_build_power_save_blocker.patch - description: null - author: Tomas Rycl file: browser_plugin_guest.patch diff --git a/patches/common/chromium/statically_build_power_save_blocker.patch b/patches/common/chromium/statically_build_power_save_blocker.patch deleted file mode 100644 index 9af88c80a..000000000 --- a/patches/common/chromium/statically_build_power_save_blocker.patch +++ /dev/null @@ -1,30 +0,0 @@ -From a99c5f94fd02a391cde00aedfd613937cb72d558 Mon Sep 17 00:00:00 2001 -From: Aleksei Kuzmin -Date: Thu, 20 Sep 2018 17:47:22 -0700 -Subject: statically_build_power_save_blocker.patch - - -diff --git a/services/device/wake_lock/power_save_blocker/BUILD.gn b/services/device/wake_lock/power_save_blocker/BUILD.gn -index dfce90f6b791..a2f7f776f4d8 100644 ---- a/services/device/wake_lock/power_save_blocker/BUILD.gn -+++ b/services/device/wake_lock/power_save_blocker/BUILD.gn -@@ -9,7 +9,7 @@ if (is_android) { - import("//build/config/android/rules.gni") - } - --source_set("power_save_blocker") { -+static_library("power_save_blocker") { - visibility = [ - # //remoting runs in a separate process which is outside of the context of - # the ServiceManager-based world. Instead of embedding a Service Manager -@@ -18,6 +18,7 @@ source_set("power_save_blocker") { - "//remoting/host:*", - "//remoting/host/win:*", - "//services/device/wake_lock:*", -+ "//electron:*", - ] - - sources = [ --- -2.17.0 -