| 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.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 | 10 |
| 11 namespace device { | 11 namespace device { |
| 12 | 12 |
| 13 namespace { | 13 namespace { |
| 14 | 14 |
| 15 PowerSaveBlocker::PowerSaveBlockerType ToPowerSaveBlockerType( | 15 PowerSaveBlocker::PowerSaveBlockerType ToPowerSaveBlockerType( |
| (...skipping 22 matching lines...) Expand all Loading... |
| 38 case mojom::WakeLockReason::ReasonOther: | 38 case mojom::WakeLockReason::ReasonOther: |
| 39 return PowerSaveBlocker::Reason::kReasonOther; | 39 return PowerSaveBlocker::Reason::kReasonOther; |
| 40 } | 40 } |
| 41 | 41 |
| 42 NOTREACHED(); | 42 NOTREACHED(); |
| 43 return PowerSaveBlocker::Reason::kReasonOther; | 43 return PowerSaveBlocker::Reason::kReasonOther; |
| 44 } | 44 } |
| 45 | 45 |
| 46 } // namespace | 46 } // namespace |
| 47 | 47 |
| 48 WakeLockServiceImpl::WakeLockServiceImpl( | 48 WakeLock::WakeLock(mojom::WakeLockRequest request, |
| 49 mojom::WakeLockServiceRequest request, | 49 mojom::WakeLockType type, |
| 50 mojom::WakeLockType type, | 50 mojom::WakeLockReason reason, |
| 51 mojom::WakeLockReason reason, | 51 const std::string& description, |
| 52 const std::string& description, | 52 int context_id, |
| 53 int context_id, | 53 WakeLockContextCallback native_view_getter, |
| 54 WakeLockContextCallback native_view_getter, | 54 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) |
| 55 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) | |
| 56 : num_lock_requests_(0), | 55 : num_lock_requests_(0), |
| 57 type_(type), | 56 type_(type), |
| 58 reason_(reason), | 57 reason_(reason), |
| 59 description_(base::MakeUnique<std::string>(description)), | 58 description_(base::MakeUnique<std::string>(description)), |
| 60 #if defined(OS_ANDROID) | 59 #if defined(OS_ANDROID) |
| 61 context_id_(context_id), | 60 context_id_(context_id), |
| 62 native_view_getter_(native_view_getter), | 61 native_view_getter_(native_view_getter), |
| 63 #endif | 62 #endif |
| 64 main_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 63 main_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 65 file_task_runner_(std::move(file_task_runner)) { | 64 file_task_runner_(std::move(file_task_runner)) { |
| 66 AddClient(std::move(request)); | 65 AddClient(std::move(request)); |
| 67 binding_set_.set_connection_error_handler(base::Bind( | 66 binding_set_.set_connection_error_handler( |
| 68 &WakeLockServiceImpl::OnConnectionError, base::Unretained(this))); | 67 base::Bind(&WakeLock::OnConnectionError, base::Unretained(this))); |
| 69 } | 68 } |
| 70 | 69 |
| 71 WakeLockServiceImpl::~WakeLockServiceImpl() {} | 70 WakeLock::~WakeLock() {} |
| 72 | 71 |
| 73 void WakeLockServiceImpl::AddClient(mojom::WakeLockServiceRequest request) { | 72 void WakeLock::AddClient(mojom::WakeLockRequest request) { |
| 74 binding_set_.AddBinding(this, std::move(request), | 73 binding_set_.AddBinding(this, std::move(request), |
| 75 base::MakeUnique<bool>(false)); | 74 base::MakeUnique<bool>(false)); |
| 76 } | 75 } |
| 77 | 76 |
| 78 void WakeLockServiceImpl::RequestWakeLock() { | 77 void WakeLock::RequestWakeLock() { |
| 79 DCHECK(main_task_runner_->RunsTasksInCurrentSequence()); | 78 DCHECK(main_task_runner_->RunsTasksInCurrentSequence()); |
| 80 DCHECK(binding_set_.dispatch_context()); | 79 DCHECK(binding_set_.dispatch_context()); |
| 81 | 80 |
| 82 // Uses the Context to get the outstanding status of current binding. | 81 // Uses the Context to get the outstanding status of current binding. |
| 83 // Two consecutive requests from the same client should be coalesced | 82 // Two consecutive requests from the same client should be coalesced |
| 84 // as one request. | 83 // as one request. |
| 85 if (*binding_set_.dispatch_context()) | 84 if (*binding_set_.dispatch_context()) |
| 86 return; | 85 return; |
| 87 | 86 |
| 88 *binding_set_.dispatch_context() = true; | 87 *binding_set_.dispatch_context() = true; |
| 89 num_lock_requests_++; | 88 num_lock_requests_++; |
| 90 UpdateWakeLock(); | 89 UpdateWakeLock(); |
| 91 } | 90 } |
| 92 | 91 |
| 93 void WakeLockServiceImpl::CancelWakeLock() { | 92 void WakeLock::CancelWakeLock() { |
| 94 DCHECK(main_task_runner_->RunsTasksInCurrentSequence()); | 93 DCHECK(main_task_runner_->RunsTasksInCurrentSequence()); |
| 95 DCHECK(binding_set_.dispatch_context()); | 94 DCHECK(binding_set_.dispatch_context()); |
| 96 | 95 |
| 97 if (!(*binding_set_.dispatch_context())) | 96 if (!(*binding_set_.dispatch_context())) |
| 98 return; | 97 return; |
| 99 | 98 |
| 100 DCHECK(num_lock_requests_ > 0); | 99 DCHECK(num_lock_requests_ > 0); |
| 101 *binding_set_.dispatch_context() = false; | 100 *binding_set_.dispatch_context() = false; |
| 102 num_lock_requests_--; | 101 num_lock_requests_--; |
| 103 UpdateWakeLock(); | 102 UpdateWakeLock(); |
| 104 } | 103 } |
| 105 | 104 |
| 106 void WakeLockServiceImpl::HasWakeLockForTests( | 105 void WakeLock::HasWakeLockForTests(HasWakeLockForTestsCallback callback) { |
| 107 HasWakeLockForTestsCallback callback) { | |
| 108 std::move(callback).Run(!!wake_lock_); | 106 std::move(callback).Run(!!wake_lock_); |
| 109 } | 107 } |
| 110 void WakeLockServiceImpl::UpdateWakeLock() { | 108 |
| 109 void WakeLock::UpdateWakeLock() { |
| 111 DCHECK(num_lock_requests_ >= 0); | 110 DCHECK(num_lock_requests_ >= 0); |
| 112 | 111 |
| 113 if (num_lock_requests_) { | 112 if (num_lock_requests_) { |
| 114 if (!wake_lock_) | 113 if (!wake_lock_) |
| 115 CreateWakeLock(); | 114 CreateWakeLock(); |
| 116 } else { | 115 } else { |
| 117 if (wake_lock_) | 116 if (wake_lock_) |
| 118 RemoveWakeLock(); | 117 RemoveWakeLock(); |
| 119 } | 118 } |
| 120 } | 119 } |
| 121 | 120 |
| 122 void WakeLockServiceImpl::CreateWakeLock() { | 121 void WakeLock::CreateWakeLock() { |
| 123 DCHECK(!wake_lock_); | 122 DCHECK(!wake_lock_); |
| 124 | 123 |
| 125 // TODO(heke): Switch PowerSaveBlocker to use mojom::WakeLockType and | 124 // TODO(heke): Switch PowerSaveBlocker to use mojom::WakeLockType and |
| 126 // mojom::WakeLockReason once all its clients are converted to be the clients | 125 // mojom::WakeLockReason once all its clients are converted to be the clients |
| 127 // of WakeLock. | 126 // of WakeLock. |
| 128 wake_lock_ = base::MakeUnique<PowerSaveBlocker>( | 127 wake_lock_ = base::MakeUnique<PowerSaveBlocker>( |
| 129 ToPowerSaveBlockerType(type_), ToPowerSaveBlockerReason(reason_), | 128 ToPowerSaveBlockerType(type_), ToPowerSaveBlockerReason(reason_), |
| 130 *description_, main_task_runner_, file_task_runner_); | 129 *description_, main_task_runner_, file_task_runner_); |
| 131 | 130 |
| 132 if (type_ != mojom::WakeLockType::PreventDisplaySleep) | 131 if (type_ != mojom::WakeLockType::PreventDisplaySleep) |
| 133 return; | 132 return; |
| 134 | 133 |
| 135 #if defined(OS_ANDROID) | 134 #if defined(OS_ANDROID) |
| 136 if (context_id_ == WakeLockServiceContext::WakeLockInvalidContextId) { | 135 if (context_id_ == WakeLockContext::WakeLockInvalidContextId) { |
| 137 LOG(ERROR) << "Client must pass a valid context_id when requests wake lock " | 136 LOG(ERROR) << "Client must pass a valid context_id when requests wake lock " |
| 138 "on Android."; | 137 "on Android."; |
| 139 return; | 138 return; |
| 140 } | 139 } |
| 141 | 140 |
| 142 gfx::NativeView native_view = native_view_getter_.Run(context_id_); | 141 gfx::NativeView native_view = native_view_getter_.Run(context_id_); |
| 143 if (native_view) | 142 if (native_view) |
| 144 wake_lock_.get()->InitDisplaySleepBlocker(native_view); | 143 wake_lock_.get()->InitDisplaySleepBlocker(native_view); |
| 145 #endif | 144 #endif |
| 146 } | 145 } |
| 147 | 146 |
| 148 void WakeLockServiceImpl::RemoveWakeLock() { | 147 void WakeLock::RemoveWakeLock() { |
| 149 DCHECK(wake_lock_); | 148 DCHECK(wake_lock_); |
| 150 wake_lock_.reset(); | 149 wake_lock_.reset(); |
| 151 } | 150 } |
| 152 | 151 |
| 153 void WakeLockServiceImpl::OnConnectionError() { | 152 void WakeLock::OnConnectionError() { |
| 154 // If this client has an outstanding wake lock request, decrease the | 153 // If this client has an outstanding wake lock request, decrease the |
| 155 // num_lock_requests and call UpdateWakeLock(). | 154 // num_lock_requests and call UpdateWakeLock(). |
| 156 if (*binding_set_.dispatch_context() && num_lock_requests_ > 0) { | 155 if (*binding_set_.dispatch_context() && num_lock_requests_ > 0) { |
| 157 num_lock_requests_--; | 156 num_lock_requests_--; |
| 158 UpdateWakeLock(); | 157 UpdateWakeLock(); |
| 159 } | 158 } |
| 160 | 159 |
| 161 if (binding_set_.empty()) | 160 if (binding_set_.empty()) |
| 162 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this); | 161 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this); |
| 163 } | 162 } |
| 164 | 163 |
| 165 } // namespace device | 164 } // namespace device |
| OLD | NEW |