| 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" | |
| 10 #include "base/memory/ptr_util.h" | |
| 11 #include "base/threading/thread_task_runner_handle.h" | |
| 12 #include "build/build_config.h" | |
| 13 #include "device/power_save_blocker/power_save_blocker.h" | 9 #include "device/power_save_blocker/power_save_blocker.h" |
| 10 #include "device/wake_lock/wake_lock_service_impl.h" |
| 14 | 11 |
| 15 namespace device { | 12 namespace device { |
| 16 | 13 |
| 17 WakeLockServiceContext::WakeLockServiceContext( | 14 WakeLockServiceContext::WakeLockServiceContext( |
| 18 mojom::WakeLockContextRequest request, | |
| 19 int context_id, | 15 int context_id, |
| 20 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, | 16 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, |
| 21 const WakeLockContextCallback& native_view_getter) | 17 const WakeLockContextCallback& native_view_getter) |
| 22 : main_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 18 : file_task_runner_(std::move(file_task_runner)), |
| 23 file_task_runner_(std::move(file_task_runner)), | |
| 24 num_lock_requests_(0), | |
| 25 #if defined(OS_ANDROID) | |
| 26 context_id_(context_id), | 19 context_id_(context_id), |
| 27 native_view_getter_(native_view_getter), | 20 native_view_getter_(native_view_getter) {} |
| 28 #endif | |
| 29 context_binding_(this, std::move(request)), | |
| 30 context_binding_encountered_error_(false) { | |
| 31 context_binding_.set_connection_error_handler(base::Bind( | |
| 32 &WakeLockServiceContext::OnContextBindingError, base::Unretained(this))); | |
| 33 wake_lock_bindings_.set_connection_error_handler( | |
| 34 base::Bind(&WakeLockServiceContext::DestroyIfNoLongerNeeded, | |
| 35 base::Unretained(this))); | |
| 36 } | |
| 37 | 21 |
| 38 WakeLockServiceContext::~WakeLockServiceContext() {} | 22 WakeLockServiceContext::~WakeLockServiceContext() {} |
| 39 | 23 |
| 40 void WakeLockServiceContext::GetWakeLock( | 24 void WakeLockServiceContext::GetWakeLock( |
| 41 mojo::InterfaceRequest<mojom::WakeLockService> request) { | 25 mojom::WakeLockServiceRequest request) { |
| 42 wake_lock_bindings_.AddBinding(base::MakeUnique<WakeLockServiceImpl>(this), | 26 // WakeLockServiceImpl owns itself. |
| 43 std::move(request)); | 27 new WakeLockServiceImpl(std::move(request), |
| 44 } | 28 device::PowerSaveBlocker::PowerSaveBlockerType:: |
| 45 | 29 kPowerSaveBlockPreventDisplaySleep, |
| 46 void WakeLockServiceContext::RequestWakeLock() { | 30 device::PowerSaveBlocker::Reason::kReasonOther, |
| 47 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); | 31 "Wake Lock API", context_id_, native_view_getter_, |
| 48 num_lock_requests_++; | 32 file_task_runner_); |
| 49 UpdateWakeLock(); | |
| 50 } | |
| 51 | |
| 52 void WakeLockServiceContext::CancelWakeLock() { | |
| 53 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); | |
| 54 num_lock_requests_--; | |
| 55 UpdateWakeLock(); | |
| 56 } | |
| 57 | |
| 58 void WakeLockServiceContext::HasWakeLockForTests( | |
| 59 const HasWakeLockForTestsCallback& callback) { | |
| 60 callback.Run(!!wake_lock_); | |
| 61 } | |
| 62 | |
| 63 void WakeLockServiceContext::CreateWakeLock() { | |
| 64 DCHECK(!wake_lock_); | |
| 65 wake_lock_.reset(new device::PowerSaveBlocker( | |
| 66 device::PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep, | |
| 67 device::PowerSaveBlocker::kReasonOther, "Wake Lock API", | |
| 68 main_task_runner_, file_task_runner_)); | |
| 69 | |
| 70 #if defined(OS_ANDROID) | |
| 71 gfx::NativeView native_view = native_view_getter_.Run(context_id_); | |
| 72 if (native_view) { | |
| 73 wake_lock_.get()->InitDisplaySleepBlocker(native_view); | |
| 74 } | |
| 75 #endif | |
| 76 } | |
| 77 | |
| 78 void WakeLockServiceContext::RemoveWakeLock() { | |
| 79 DCHECK(wake_lock_); | |
| 80 wake_lock_.reset(); | |
| 81 } | |
| 82 | |
| 83 void WakeLockServiceContext::UpdateWakeLock() { | |
| 84 DCHECK(num_lock_requests_ >= 0); | |
| 85 if (num_lock_requests_) { | |
| 86 if (!wake_lock_) | |
| 87 CreateWakeLock(); | |
| 88 } else { | |
| 89 if (wake_lock_) | |
| 90 RemoveWakeLock(); | |
| 91 } | |
| 92 } | |
| 93 | |
| 94 void WakeLockServiceContext::OnContextBindingError() { | |
| 95 context_binding_encountered_error_ = true; | |
| 96 DestroyIfNoLongerNeeded(); | |
| 97 } | |
| 98 | |
| 99 void WakeLockServiceContext::DestroyIfNoLongerNeeded() { | |
| 100 if (context_binding_encountered_error_ && wake_lock_bindings_.empty()) { | |
| 101 // Delete this instance once there are no more live connections to it. | |
| 102 // However, ensure that this instance stays alive throughout the destructor | |
| 103 // of a WakeLockServiceImpl instance that might be triggering this callback. | |
| 104 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this); | |
| 105 } | |
| 106 } | 33 } |
| 107 | 34 |
| 108 } // namespace device | 35 } // namespace device |
| OLD | NEW |