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