Chromium Code Reviews| 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_impl.h" | 5 #include "device/wake_lock/wake_lock_service_impl.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "device/wake_lock/wake_lock_service_context.h" | 9 #include "base/memory/ptr_util.h" |
| 10 | 10 |
| 11 namespace device { | 11 namespace device { |
| 12 | 12 |
| 13 WakeLockServiceImpl::WakeLockServiceImpl(WakeLockServiceContext* context) | 13 WakeLockServiceImpl::WakeLockServiceImpl( |
| 14 : context_(context), wake_lock_request_outstanding_(false) {} | 14 mojom::WakeLockServiceRequest request, |
| 15 device::PowerSaveBlocker::PowerSaveBlockerType type, | |
| 16 device::PowerSaveBlocker::Reason reason, | |
| 17 const std::string& description, | |
| 18 int context_id, | |
| 19 WakeLockContextCallback native_view_getter, | |
| 20 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) | |
| 21 : type_(type), | |
| 22 reason_(reason), | |
| 23 description_(base::MakeUnique<std::string>(description)), | |
| 24 num_lock_requests_(0), | |
| 25 #if defined(OS_ANDROID) | |
| 26 context_id_(context_id), | |
| 27 native_view_getter_(native_view_getter), | |
| 28 #endif | |
| 29 main_task_runner_(base::ThreadTaskRunnerHandle::Get()), | |
| 30 file_task_runner_(std::move(file_task_runner)) { | |
| 31 AddClient(std::move(request)); | |
| 32 binding_set_.set_connection_error_handler(base::Bind( | |
| 33 &WakeLockServiceImpl::OnConnectionError, base::Unretained(this))); | |
| 34 } | |
| 15 | 35 |
| 16 WakeLockServiceImpl::~WakeLockServiceImpl() { | 36 WakeLockServiceImpl::~WakeLockServiceImpl() {} |
| 17 CancelWakeLock(); | 37 |
| 38 void WakeLockServiceImpl::AddClient(mojom::WakeLockServiceRequest request) { | |
| 39 binding_set_.AddBinding(this, std::move(request), | |
| 40 base::MakeUnique<bool>(false)); | |
| 18 } | 41 } |
| 19 | 42 |
| 20 void WakeLockServiceImpl::RequestWakeLock() { | 43 void WakeLockServiceImpl::RequestWakeLock() { |
| 21 if (wake_lock_request_outstanding_) | 44 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); |
| 45 DCHECK(binding_set_.dispatch_context()); | |
| 46 | |
| 47 // Uses the Context to get the outstanding status of current binding. | |
| 48 // Two consecutive requests from the same client should be coalesced | |
| 49 // as one request. | |
| 50 if (*binding_set_.dispatch_context()) | |
| 22 return; | 51 return; |
| 23 | 52 |
| 24 wake_lock_request_outstanding_ = true; | 53 *binding_set_.dispatch_context() = true; |
| 25 context_->RequestWakeLock(); | 54 num_lock_requests_++; |
| 55 UpdateWakeLock(); | |
| 26 } | 56 } |
| 27 | 57 |
| 28 void WakeLockServiceImpl::CancelWakeLock() { | 58 void WakeLockServiceImpl::CancelWakeLock() { |
| 29 if (!wake_lock_request_outstanding_) | 59 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); |
| 60 DCHECK(binding_set_.dispatch_context()); | |
| 61 | |
| 62 if (!(*binding_set_.dispatch_context())) | |
| 30 return; | 63 return; |
| 31 | 64 |
| 32 wake_lock_request_outstanding_ = false; | 65 *binding_set_.dispatch_context() = false; |
| 33 context_->CancelWakeLock(); | 66 if (num_lock_requests_ > 0) { |
|
blundell
2017/05/09 08:22:44
This can be a DCHECK instead of an if statement, r
| |
| 67 num_lock_requests_--; | |
| 68 UpdateWakeLock(); | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 void WakeLockServiceImpl::HasWakeLockForTests( | |
| 73 const HasWakeLockForTestsCallback& callback) { | |
| 74 callback.Run(!!wake_lock_); | |
| 75 } | |
| 76 void WakeLockServiceImpl::UpdateWakeLock() { | |
| 77 DCHECK(num_lock_requests_ >= 0); | |
| 78 | |
| 79 if (num_lock_requests_) { | |
| 80 if (!wake_lock_) | |
| 81 CreateWakeLock(); | |
| 82 } else { | |
| 83 if (wake_lock_) | |
| 84 RemoveWakeLock(); | |
| 85 } | |
| 86 } | |
| 87 | |
| 88 void WakeLockServiceImpl::CreateWakeLock() { | |
| 89 DCHECK(!wake_lock_); | |
| 90 | |
| 91 if (type_ != device::PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep || | |
| 92 reason_ != device::PowerSaveBlocker::kReasonOther || | |
| 93 *description_ != "Wake Lock API") { | |
| 94 // TODO(ke.he@intel.com): Fully generalize the WakeLock interface and impl. | |
| 95 NOTREACHED(); | |
| 96 return; | |
| 97 } | |
| 98 | |
| 99 wake_lock_ = base::MakeUnique<device::PowerSaveBlocker>( | |
| 100 type_, reason_, *description_, main_task_runner_, file_task_runner_); | |
| 101 | |
| 102 #if defined(OS_ANDROID) | |
| 103 gfx::NativeView native_view = native_view_getter_.Run(context_id_); | |
| 104 if (native_view) | |
| 105 wake_lock_.get()->InitDisplaySleepBlocker(native_view); | |
| 106 #endif | |
| 107 } | |
| 108 | |
| 109 void WakeLockServiceImpl::RemoveWakeLock() { | |
| 110 DCHECK(wake_lock_); | |
| 111 wake_lock_.reset(); | |
| 112 } | |
| 113 | |
| 114 void WakeLockServiceImpl::OnConnectionError() { | |
| 115 DCHECK(binding_set_.dispatch_context()); | |
|
blundell
2017/05/09 08:22:44
We don't need this: It's a basic property of Bindi
| |
| 116 | |
| 117 // If the error-happening client's wakelock is in outstanding status, | |
| 118 // decrease the num_lock_requests and call UpdateWakeLock(). | |
| 119 if (*binding_set_.dispatch_context() && num_lock_requests_ > 0) { | |
| 120 num_lock_requests_--; | |
| 121 UpdateWakeLock(); | |
| 122 } | |
| 123 | |
| 124 // If |binding_set_| is empty, WakeLockServiceImpl should delele itself. | |
|
blundell
2017/05/09 08:22:44
delete
| |
| 125 if (binding_set_.empty()) | |
| 126 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this); | |
| 34 } | 127 } |
| 35 | 128 |
| 36 } // namespace device | 129 } // namespace device |
| OLD | NEW |