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 // Multiple clients that associate to the same WebContents share the same one | |
| 40 // WakeLockServiceImpl instance. Two consecutive |RequestWakeLock| requests | |
| 41 // from the same client should be coalesced as one request. Everytime a new | |
| 42 // client is being added into the bindingSet, we create an unique_ptr<bool> | |
| 43 // as its context, which records this client's request status. | |
| 44 binding_set_.AddBinding(this, std::move(request), | |
| 45 base::MakeUnique<bool>(false)); | |
| 18 } | 46 } |
| 19 | 47 |
| 20 void WakeLockServiceImpl::RequestWakeLock() { | 48 void WakeLockServiceImpl::RequestWakeLock() { |
| 21 if (wake_lock_request_outstanding_) | 49 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); |
| 50 DCHECK(binding_set_.dispatch_context()); | |
| 51 | |
| 52 // Uses the Context to get the outstanding status of current binding. | |
| 53 // Two consecutive requests from the same client should be coalesced | |
| 54 // as one request. | |
| 55 if (*binding_set_.dispatch_context()) | |
| 22 return; | 56 return; |
| 23 | 57 |
| 24 wake_lock_request_outstanding_ = true; | 58 *binding_set_.dispatch_context() = true; |
| 25 context_->RequestWakeLock(); | 59 num_lock_requests_++; |
| 60 UpdateWakeLock(); | |
| 26 } | 61 } |
| 27 | 62 |
| 28 void WakeLockServiceImpl::CancelWakeLock() { | 63 void WakeLockServiceImpl::CancelWakeLock() { |
| 29 if (!wake_lock_request_outstanding_) | 64 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); |
| 65 DCHECK(binding_set_.dispatch_context()); | |
| 66 | |
| 67 if (!(*binding_set_.dispatch_context())) | |
| 30 return; | 68 return; |
| 31 | 69 |
| 32 wake_lock_request_outstanding_ = false; | 70 *binding_set_.dispatch_context() = false; |
| 33 context_->CancelWakeLock(); | 71 if (num_lock_requests_ > 0) { |
| 72 num_lock_requests_--; | |
| 73 UpdateWakeLock(); | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 void WakeLockServiceImpl::HasWakeLockForTests( | |
| 78 const HasWakeLockForTestsCallback& callback) { | |
| 79 callback.Run(!!wake_lock_); | |
| 80 } | |
| 81 void WakeLockServiceImpl::UpdateWakeLock() { | |
| 82 DCHECK(num_lock_requests_ >= 0); | |
| 83 | |
| 84 if (num_lock_requests_) { | |
| 85 if (!wake_lock_) | |
| 86 CreateWakeLock(); | |
| 87 } else { | |
| 88 if (wake_lock_) | |
| 89 RemoveWakeLock(); | |
| 90 } | |
| 91 } | |
| 92 | |
| 93 void WakeLockServiceImpl::CreateWakeLock() { | |
| 94 DCHECK(!wake_lock_); | |
| 95 | |
| 96 if (type_ != device::PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep || | |
| 97 reason_ != device::PowerSaveBlocker::kReasonOther || | |
| 98 *description_ != "Wake Lock API") | |
| 99 return; | |
|
blundell
2017/05/05 16:28:04
I would do a NOTREACHED() here with a TODO(ke.he@i
ke.he
2017/05/06 03:08:43
Done.
| |
| 100 | |
| 101 wake_lock_ = base::MakeUnique<device::PowerSaveBlocker>( | |
| 102 type_, reason_, *description_, main_task_runner_, file_task_runner_); | |
| 103 | |
| 104 #if defined(OS_ANDROID) | |
| 105 gfx::NativeView native_view = native_view_getter_.Run(context_id_); | |
| 106 if (native_view) | |
| 107 wake_lock_.get()->InitDisplaySleepBlocker(native_view); | |
| 108 #endif | |
| 109 } | |
| 110 | |
| 111 void WakeLockServiceImpl::RemoveWakeLock() { | |
| 112 DCHECK(wake_lock_); | |
| 113 wake_lock_.reset(); | |
| 114 } | |
| 115 | |
| 116 void WakeLockServiceImpl::OnConnectionError() { | |
| 117 DCHECK(binding_set_.dispatch_context()); | |
| 118 | |
| 119 // If the error-happening client's wakelock is in outstanding status, | |
| 120 // decrease the num_lock_requests and call UpdateWakeLock(). | |
| 121 if (*binding_set_.dispatch_context() && num_lock_requests_ > 0) { | |
| 122 num_lock_requests_--; | |
| 123 UpdateWakeLock(); | |
| 124 } | |
| 125 | |
| 126 // If |binding_set_| is empty, WakeLockServiceImpl should delele itself. | |
| 127 if (binding_set_.empty()) | |
| 128 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this); | |
| 34 } | 129 } |
| 35 | 130 |
| 36 } // namespace device | 131 } // namespace device |
| OLD | NEW |