Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(311)

Unified Diff: device/wake_lock/wake_lock_service_context.cc

Issue 2734943003: Device Service: Decouple Wake Lock from //content (Closed)
Patch Set: Bugfix Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: device/wake_lock/wake_lock_service_context.cc
diff --git a/device/wake_lock/wake_lock_service_context.cc b/device/wake_lock/wake_lock_service_context.cc
index d30f8cdaadd5f8a7224a237932bfb02bc12e1467..5012411287db4df544cb5661d834ff6d0b6fbd4f 100644
--- a/device/wake_lock/wake_lock_service_context.cc
+++ b/device/wake_lock/wake_lock_service_context.cc
@@ -15,22 +15,42 @@
namespace device {
+namespace {
+
+const int kInvalidContextId = -1;
+}
+
WakeLockServiceContext::WakeLockServiceContext(
+ mojom::WakeLockServiceContextRequest request,
scoped_refptr<base::SingleThreadTaskRunner> file_task_runner,
- base::Callback<gfx::NativeView()> native_view_getter)
+ WakeLockContextCallback native_view_getter)
: main_task_runner_(base::ThreadTaskRunnerHandle::Get()),
file_task_runner_(file_task_runner),
+ context_id_(kInvalidContextId),
num_lock_requests_(0),
native_view_getter_(native_view_getter),
- weak_factory_(this) {}
+ binding_(this, std::move(request)),
+ num_connections_(1),
+ weak_factory_(this) {
+ binding_.set_connection_error_handler(base::Bind(
+ &WakeLockServiceContext::OnConnectionError, base::Unretained(this)));
+}
WakeLockServiceContext::~WakeLockServiceContext() {}
-void WakeLockServiceContext::CreateService(
+void WakeLockServiceContext::SetContextID(int context_id) {
+ context_id_ = context_id;
+}
+
+void WakeLockServiceContext::BindWakeLock(
mojo::InterfaceRequest<mojom::WakeLockService> request) {
- mojo::MakeStrongBinding(
- base::MakeUnique<WakeLockServiceImpl>(weak_factory_.GetWeakPtr()),
- std::move(request));
+ num_connections_++;
+ mojo::StrongBindingPtr<mojom::WakeLockService> binding =
+ mojo::MakeStrongBinding(
+ base::MakeUnique<WakeLockServiceImpl>(weak_factory_.GetWeakPtr()),
+ std::move(request));
+ binding->set_connection_error_handler(base::Bind(
+ &WakeLockServiceContext::OnConnectionError, base::Unretained(this)));
}
void WakeLockServiceContext::RequestWakeLock() {
@@ -45,8 +65,9 @@ void WakeLockServiceContext::CancelWakeLock() {
UpdateWakeLock();
}
-bool WakeLockServiceContext::HasWakeLockForTests() const {
- return !!wake_lock_;
+void WakeLockServiceContext::HasWakeLockForTests(
+ const HasWakeLockForTestsCallback& callback) {
+ callback.Run(!!wake_lock_);
}
void WakeLockServiceContext::CreateWakeLock() {
@@ -57,7 +78,10 @@ void WakeLockServiceContext::CreateWakeLock() {
main_task_runner_, file_task_runner_));
#if defined(OS_ANDROID)
- gfx::NativeView native_view = native_view_getter_.Run();
+ if (context_id_ == kInvalidContextId)
+ return;
+
+ gfx::NativeView native_view = native_view_getter_.Run(context_id_);
if (native_view) {
wake_lock_.get()->InitDisplaySleepBlocker(native_view);
}
@@ -80,4 +104,11 @@ void WakeLockServiceContext::UpdateWakeLock() {
}
}
+void WakeLockServiceContext::OnConnectionError() {
+ DCHECK(num_connections_ > 0);
+ num_connections_--;
+ if (num_connections_ == 0)
+ delete this;
+}
+
} // namespace device

Powered by Google App Engine
This is Rietveld 408576698