refactor: use public WakeLock interface (#15351)
* refactor: use public WakeLock interface * remove power_save_blocker visibility patch
This commit is contained in:
parent
1f246229b1
commit
1d8ab03146
5 changed files with 52 additions and 67 deletions
7
BUILD.gn
7
BUILD.gn
|
@ -222,6 +222,7 @@ static_library("electron_lib") {
|
||||||
"//ppapi/host",
|
"//ppapi/host",
|
||||||
"//ppapi/proxy",
|
"//ppapi/proxy",
|
||||||
"//ppapi/shared_impl",
|
"//ppapi/shared_impl",
|
||||||
|
"//services/device/public/mojom",
|
||||||
"//services/proxy_resolver:lib",
|
"//services/proxy_resolver:lib",
|
||||||
"//skia",
|
"//skia",
|
||||||
"//third_party/blink/public:blink",
|
"//third_party/blink/public:blink",
|
||||||
|
@ -237,12 +238,6 @@ static_library("electron_lib") {
|
||||||
"//v8",
|
"//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 = [
|
include_dirs = [
|
||||||
"chromium_src",
|
"chromium_src",
|
||||||
".",
|
".",
|
||||||
|
|
|
@ -8,7 +8,11 @@
|
||||||
|
|
||||||
#include "base/task_scheduler/post_task.h"
|
#include "base/task_scheduler/post_task.h"
|
||||||
#include "base/threading/thread_task_runner_handle.h"
|
#include "base/threading/thread_task_runner_handle.h"
|
||||||
|
#include "content/public/common/service_manager_connection.h"
|
||||||
#include "native_mate/dictionary.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"
|
#include "atom/common/node_includes.h"
|
||||||
|
|
||||||
|
@ -39,16 +43,19 @@ namespace atom {
|
||||||
namespace api {
|
namespace api {
|
||||||
|
|
||||||
PowerSaveBlocker::PowerSaveBlocker(v8::Isolate* isolate)
|
PowerSaveBlocker::PowerSaveBlocker(v8::Isolate* isolate)
|
||||||
: current_blocker_type_(
|
: current_lock_type_(device::mojom::WakeLockType::kPreventAppSuspension),
|
||||||
device::mojom::WakeLockType::kPreventAppSuspension) {
|
is_wake_lock_active_(false) {
|
||||||
Init(isolate);
|
Init(isolate);
|
||||||
}
|
}
|
||||||
|
|
||||||
PowerSaveBlocker::~PowerSaveBlocker() {}
|
PowerSaveBlocker::~PowerSaveBlocker() {}
|
||||||
|
|
||||||
void PowerSaveBlocker::UpdatePowerSaveBlocker() {
|
void PowerSaveBlocker::UpdatePowerSaveBlocker() {
|
||||||
if (power_save_blocker_types_.empty()) {
|
if (wake_lock_types_.empty()) {
|
||||||
power_save_blocker_.reset();
|
if (is_wake_lock_active_) {
|
||||||
|
GetWakeLock()->CancelWakeLock();
|
||||||
|
is_wake_lock_active_ = false;
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,45 +65,57 @@ void PowerSaveBlocker::UpdatePowerSaveBlocker() {
|
||||||
// higher precedence level than |WakeLockType::kPreventAppSuspension|.
|
// higher precedence level than |WakeLockType::kPreventAppSuspension|.
|
||||||
//
|
//
|
||||||
// Only the highest-precedence blocker type takes effect.
|
// Only the highest-precedence blocker type takes effect.
|
||||||
device::mojom::WakeLockType new_blocker_type =
|
device::mojom::WakeLockType new_lock_type =
|
||||||
device::mojom::WakeLockType::kPreventAppSuspension;
|
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) {
|
if (element.second == device::mojom::WakeLockType::kPreventDisplaySleep) {
|
||||||
new_blocker_type = device::mojom::WakeLockType::kPreventDisplaySleep;
|
new_lock_type = device::mojom::WakeLockType::kPreventDisplaySleep;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!power_save_blocker_ || new_blocker_type != current_blocker_type_) {
|
if (current_lock_type_ != new_lock_type) {
|
||||||
auto new_blocker = std::make_unique<device::PowerSaveBlocker>(
|
GetWakeLock()->ChangeType(new_lock_type, base::DoNothing());
|
||||||
new_blocker_type, device::mojom::WakeLockReason::kOther,
|
current_lock_type_ = new_lock_type;
|
||||||
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 (!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) {
|
int PowerSaveBlocker::Start(device::mojom::WakeLockType type) {
|
||||||
static int count = 0;
|
static int count = 0;
|
||||||
power_save_blocker_types_[count] = type;
|
wake_lock_types_[count] = type;
|
||||||
UpdatePowerSaveBlocker();
|
UpdatePowerSaveBlocker();
|
||||||
return count++;
|
return count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PowerSaveBlocker::Stop(int id) {
|
bool PowerSaveBlocker::Stop(int id) {
|
||||||
bool success = power_save_blocker_types_.erase(id) > 0;
|
bool success = wake_lock_types_.erase(id) > 0;
|
||||||
UpdatePowerSaveBlocker();
|
UpdatePowerSaveBlocker();
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PowerSaveBlocker::IsStarted(int id) {
|
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
|
// static
|
||||||
|
|
|
@ -10,7 +10,7 @@
|
||||||
|
|
||||||
#include "atom/browser/api/trackable_object.h"
|
#include "atom/browser/api/trackable_object.h"
|
||||||
#include "native_mate/handle.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 {
|
namespace mate {
|
||||||
class Dictionary;
|
class Dictionary;
|
||||||
|
@ -37,14 +37,19 @@ class PowerSaveBlocker : public mate::TrackableObject<PowerSaveBlocker> {
|
||||||
bool Stop(int id);
|
bool Stop(int id);
|
||||||
bool IsStarted(int id);
|
bool IsStarted(int id);
|
||||||
|
|
||||||
std::unique_ptr<device::PowerSaveBlocker> power_save_blocker_;
|
device::mojom::WakeLock* GetWakeLock();
|
||||||
|
|
||||||
// Current blocker type used by |power_save_blocker_|
|
// Current wake lock level.
|
||||||
device::mojom::WakeLockType current_blocker_type_;
|
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.
|
// Map from id to the corresponding blocker type for each request.
|
||||||
using WakeLockTypeMap = std::map<int, device::mojom::WakeLockType>;
|
using WakeLockTypeMap = std::map<int, device::mojom::WakeLockType>;
|
||||||
WakeLockTypeMap power_save_blocker_types_;
|
WakeLockTypeMap wake_lock_types_;
|
||||||
|
|
||||||
|
device::mojom::WakeLockPtr wake_lock_;
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(PowerSaveBlocker);
|
DISALLOW_COPY_AND_ASSIGN(PowerSaveBlocker);
|
||||||
};
|
};
|
||||||
|
|
|
@ -172,10 +172,6 @@ patches:
|
||||||
author: null
|
author: null
|
||||||
file: webui_in_subframes.patch
|
file: webui_in_subframes.patch
|
||||||
description: null
|
description: null
|
||||||
-
|
|
||||||
author: Aleksei Kuzmin <alkuzmin@microsoft.com>
|
|
||||||
file: statically_build_power_save_blocker.patch
|
|
||||||
description: null
|
|
||||||
-
|
-
|
||||||
author: Tomas Rycl <torycl@microsoft.com>
|
author: Tomas Rycl <torycl@microsoft.com>
|
||||||
file: browser_plugin_guest.patch
|
file: browser_plugin_guest.patch
|
||||||
|
|
|
@ -1,30 +0,0 @@
|
||||||
From a99c5f94fd02a391cde00aedfd613937cb72d558 Mon Sep 17 00:00:00 2001
|
|
||||||
From: Aleksei Kuzmin <alkuzmin@microsoft.com>
|
|
||||||
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
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue