| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "device/wake_lock/wake_lock_service_context.h" | 5 #include "device/wake_lock/wake_lock_service_context.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/threading/thread_task_runner_handle.h" | 11 #include "base/threading/thread_task_runner_handle.h" |
| 12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
| 13 #include "device/power_save_blocker/power_save_blocker.h" | 13 #include "device/power_save_blocker/power_save_blocker.h" |
| 14 #include "mojo/public/cpp/bindings/strong_binding.h" | 14 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 15 | 15 |
| 16 namespace device { | 16 namespace device { |
| 17 | 17 |
| 18 namespace { |
| 19 |
| 20 const int kInvalidContextId = -1; |
| 21 } |
| 22 |
| 18 WakeLockServiceContext::WakeLockServiceContext( | 23 WakeLockServiceContext::WakeLockServiceContext( |
| 24 mojom::WakeLockServiceContextRequest request, |
| 19 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, | 25 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, |
| 20 base::Callback<gfx::NativeView()> native_view_getter) | 26 WakeLockContextCallback native_view_getter) |
| 21 : main_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 27 : main_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 22 file_task_runner_(file_task_runner), | 28 file_task_runner_(file_task_runner), |
| 29 context_id_(kInvalidContextId), |
| 23 num_lock_requests_(0), | 30 num_lock_requests_(0), |
| 24 native_view_getter_(native_view_getter), | 31 native_view_getter_(native_view_getter), |
| 25 weak_factory_(this) {} | 32 binding_(this, std::move(request)), |
| 33 num_connections_(1), |
| 34 weak_factory_(this) { |
| 35 binding_.set_connection_error_handler(base::Bind( |
| 36 &WakeLockServiceContext::OnConnectionError, base::Unretained(this))); |
| 37 } |
| 26 | 38 |
| 27 WakeLockServiceContext::~WakeLockServiceContext() {} | 39 WakeLockServiceContext::~WakeLockServiceContext() {} |
| 28 | 40 |
| 29 void WakeLockServiceContext::CreateService( | 41 void WakeLockServiceContext::SetContextID(int context_id) { |
| 42 context_id_ = context_id; |
| 43 } |
| 44 |
| 45 void WakeLockServiceContext::BindWakeLock( |
| 30 mojo::InterfaceRequest<mojom::WakeLockService> request) { | 46 mojo::InterfaceRequest<mojom::WakeLockService> request) { |
| 31 mojo::MakeStrongBinding( | 47 num_connections_++; |
| 32 base::MakeUnique<WakeLockServiceImpl>(weak_factory_.GetWeakPtr()), | 48 mojo::StrongBindingPtr<mojom::WakeLockService> binding = |
| 33 std::move(request)); | 49 mojo::MakeStrongBinding( |
| 50 base::MakeUnique<WakeLockServiceImpl>(weak_factory_.GetWeakPtr()), |
| 51 std::move(request)); |
| 52 binding->set_connection_error_handler(base::Bind( |
| 53 &WakeLockServiceContext::OnConnectionError, base::Unretained(this))); |
| 34 } | 54 } |
| 35 | 55 |
| 36 void WakeLockServiceContext::RequestWakeLock() { | 56 void WakeLockServiceContext::RequestWakeLock() { |
| 37 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); | 57 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); |
| 38 num_lock_requests_++; | 58 num_lock_requests_++; |
| 39 UpdateWakeLock(); | 59 UpdateWakeLock(); |
| 40 } | 60 } |
| 41 | 61 |
| 42 void WakeLockServiceContext::CancelWakeLock() { | 62 void WakeLockServiceContext::CancelWakeLock() { |
| 43 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); | 63 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); |
| 44 num_lock_requests_--; | 64 num_lock_requests_--; |
| 45 UpdateWakeLock(); | 65 UpdateWakeLock(); |
| 46 } | 66 } |
| 47 | 67 |
| 48 bool WakeLockServiceContext::HasWakeLockForTests() const { | 68 void WakeLockServiceContext::HasWakeLockForTests( |
| 49 return !!wake_lock_; | 69 const HasWakeLockForTestsCallback& callback) { |
| 70 callback.Run(!!wake_lock_); |
| 50 } | 71 } |
| 51 | 72 |
| 52 void WakeLockServiceContext::CreateWakeLock() { | 73 void WakeLockServiceContext::CreateWakeLock() { |
| 53 DCHECK(!wake_lock_); | 74 DCHECK(!wake_lock_); |
| 54 wake_lock_.reset(new device::PowerSaveBlocker( | 75 wake_lock_.reset(new device::PowerSaveBlocker( |
| 55 device::PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep, | 76 device::PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep, |
| 56 device::PowerSaveBlocker::kReasonOther, "Wake Lock API", | 77 device::PowerSaveBlocker::kReasonOther, "Wake Lock API", |
| 57 main_task_runner_, file_task_runner_)); | 78 main_task_runner_, file_task_runner_)); |
| 58 | 79 |
| 59 #if defined(OS_ANDROID) | 80 #if defined(OS_ANDROID) |
| 60 gfx::NativeView native_view = native_view_getter_.Run(); | 81 if (context_id_ == kInvalidContextId) |
| 82 return; |
| 83 |
| 84 gfx::NativeView native_view = native_view_getter_.Run(context_id_); |
| 61 if (native_view) { | 85 if (native_view) { |
| 62 wake_lock_.get()->InitDisplaySleepBlocker(native_view); | 86 wake_lock_.get()->InitDisplaySleepBlocker(native_view); |
| 63 } | 87 } |
| 64 #endif | 88 #endif |
| 65 } | 89 } |
| 66 | 90 |
| 67 void WakeLockServiceContext::RemoveWakeLock() { | 91 void WakeLockServiceContext::RemoveWakeLock() { |
| 68 DCHECK(wake_lock_); | 92 DCHECK(wake_lock_); |
| 69 wake_lock_.reset(); | 93 wake_lock_.reset(); |
| 70 } | 94 } |
| 71 | 95 |
| 72 void WakeLockServiceContext::UpdateWakeLock() { | 96 void WakeLockServiceContext::UpdateWakeLock() { |
| 73 DCHECK(num_lock_requests_ >= 0); | 97 DCHECK(num_lock_requests_ >= 0); |
| 74 if (num_lock_requests_) { | 98 if (num_lock_requests_) { |
| 75 if (!wake_lock_) | 99 if (!wake_lock_) |
| 76 CreateWakeLock(); | 100 CreateWakeLock(); |
| 77 } else { | 101 } else { |
| 78 if (wake_lock_) | 102 if (wake_lock_) |
| 79 RemoveWakeLock(); | 103 RemoveWakeLock(); |
| 80 } | 104 } |
| 81 } | 105 } |
| 82 | 106 |
| 107 void WakeLockServiceContext::OnConnectionError() { |
| 108 DCHECK(num_connections_ > 0); |
| 109 num_connections_--; |
| 110 if (num_connections_ == 0) |
| 111 delete this; |
| 112 } |
| 113 |
| 83 } // namespace device | 114 } // namespace device |
| OLD | NEW |