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 "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 | 10 |
| 11 namespace device { | 11 namespace device { |
| 12 | 12 |
| 13 WakeLockServiceImpl::WakeLockServiceImpl( | 13 WakeLockServiceImpl::WakeLockServiceImpl( |
| 14 mojom::WakeLockServiceRequest request, | 14 mojom::WakeLockServiceRequest request, |
| 15 device::PowerSaveBlocker::PowerSaveBlockerType type, | 15 mojom::WakeLockType type, |
| 16 device::PowerSaveBlocker::Reason reason, | 16 mojom::WakeLockReason reason, |
| 17 const std::string& description, | 17 const std::string& description, |
| 18 int context_id, | 18 int context_id, |
| 19 WakeLockContextCallback native_view_getter, | 19 WakeLockContextCallback native_view_getter, |
| 20 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) | 20 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) |
| 21 : type_(type), | 21 : type_(type), |
| 22 reason_(reason), | 22 reason_(reason), |
| 23 description_(base::MakeUnique<std::string>(description)), | 23 description_(base::MakeUnique<std::string>(description)), |
| 24 num_lock_requests_(0), | 24 num_lock_requests_(0), |
| 25 #if defined(OS_ANDROID) | 25 #if defined(OS_ANDROID) |
| 26 context_id_(context_id), | 26 context_id_(context_id), |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 81 CreateWakeLock(); | 81 CreateWakeLock(); |
| 82 } else { | 82 } else { |
| 83 if (wake_lock_) | 83 if (wake_lock_) |
| 84 RemoveWakeLock(); | 84 RemoveWakeLock(); |
| 85 } | 85 } |
| 86 } | 86 } |
| 87 | 87 |
| 88 void WakeLockServiceImpl::CreateWakeLock() { | 88 void WakeLockServiceImpl::CreateWakeLock() { |
| 89 DCHECK(!wake_lock_); | 89 DCHECK(!wake_lock_); |
| 90 | 90 |
| 91 if (type_ != device::PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep || | 91 // TODO(heke): Remove below static_cast, and switch PowerSaveBlocker to use |
| 92 reason_ != device::PowerSaveBlocker::kReasonOther || | 92 // mojom::WakeLockType and mojom::WakeLockReason once all its clients are |
| 93 *description_ != "Wake Lock API") { | 93 // converted to be the clients of WakeLock. |
| 94 // TODO(ke.he@intel.com): Fully generalize the WakeLock interface and impl. | 94 |
| 95 NOTREACHED(); | 95 // It is safe to static_cast because the definition of mojom::WakeLockType |
| 96 // and mojom::WakeLockReason are just copied from | |
| 97 // PowerSaveBlocker::PowerSaveBlockerType and PowerSaveBlocker::Reason. | |
| 98 PowerSaveBlocker::PowerSaveBlockerType type = | |
| 99 static_cast<PowerSaveBlocker::PowerSaveBlockerType>(type_); | |
|
blundell
2017/05/10 10:05:11
hmm, I think it would be safer to explicitly do th
ke.he
2017/05/10 13:12:03
Done.
| |
| 100 PowerSaveBlocker::Reason reason = | |
| 101 static_cast<PowerSaveBlocker::Reason>(reason_); | |
| 102 | |
| 103 wake_lock_ = base::MakeUnique<PowerSaveBlocker>( | |
| 104 type, reason, *description_, main_task_runner_, file_task_runner_); | |
| 105 | |
| 106 if (type != PowerSaveBlocker::PowerSaveBlockerType:: | |
| 107 kPowerSaveBlockPreventDisplaySleep) | |
| 96 return; | 108 return; |
| 97 } | |
| 98 | |
| 99 wake_lock_ = base::MakeUnique<device::PowerSaveBlocker>( | |
| 100 type_, reason_, *description_, main_task_runner_, file_task_runner_); | |
| 101 | 109 |
| 102 #if defined(OS_ANDROID) | 110 #if defined(OS_ANDROID) |
| 103 gfx::NativeView native_view = native_view_getter_.Run(context_id_); | 111 gfx::NativeView native_view = native_view_getter_.Run(context_id_); |
| 104 if (native_view) | 112 if (native_view) |
| 105 wake_lock_.get()->InitDisplaySleepBlocker(native_view); | 113 wake_lock_.get()->InitDisplaySleepBlocker(native_view); |
| 106 #endif | 114 #endif |
| 107 } | 115 } |
| 108 | 116 |
| 109 void WakeLockServiceImpl::RemoveWakeLock() { | 117 void WakeLockServiceImpl::RemoveWakeLock() { |
| 110 DCHECK(wake_lock_); | 118 DCHECK(wake_lock_); |
| 111 wake_lock_.reset(); | 119 wake_lock_.reset(); |
| 112 } | 120 } |
| 113 | 121 |
| 114 void WakeLockServiceImpl::OnConnectionError() { | 122 void WakeLockServiceImpl::OnConnectionError() { |
| 115 DCHECK(binding_set_.dispatch_context()); | 123 DCHECK(binding_set_.dispatch_context()); |
| 116 | 124 |
| 117 // If the error-happening client's wakelock is in outstanding status, | 125 // If the error-happening client's wakelock is in outstanding status, |
| 118 // decrease the num_lock_requests and call UpdateWakeLock(). | 126 // decrease the num_lock_requests and call UpdateWakeLock(). |
| 119 if (*binding_set_.dispatch_context() && num_lock_requests_ > 0) { | 127 if (*binding_set_.dispatch_context() && num_lock_requests_ > 0) { |
| 120 num_lock_requests_--; | 128 num_lock_requests_--; |
| 121 UpdateWakeLock(); | 129 UpdateWakeLock(); |
| 122 } | 130 } |
| 123 | 131 |
| 124 // If |binding_set_| is empty, WakeLockServiceImpl should delele itself. | 132 // If |binding_set_| is empty, WakeLockServiceImpl should delele itself. |
| 125 if (binding_set_.empty()) | 133 if (binding_set_.empty()) |
| 126 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this); | 134 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this); |
| 127 } | 135 } |
| 128 | 136 |
| 129 } // namespace device | 137 } // namespace device |
| OLD | NEW |