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 frames that associate to the same WebContents share the same one | |
40 // WakeLockServiceImpl instance. Two consecutive |RequestWakeLock| requests | |
41 // from the same frame should be coalesced as one request. Everytime a new | |
blundell
2017/05/05 12:49:38
s/frame/client
ke.he
2017/05/05 14:56:36
Done.
| |
42 // client is being added into the bindingSet, we create an unique_ptr<bool> | |
43 // as its context, this context records the clients' outstanding status. | |
blundell
2017/05/05 12:49:37
, which records this client's request status.
ke.he
2017/05/05 14:56:36
Done.
| |
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; |
69 } | |
70 *binding_set_.dispatch_context() = false; | |
71 if (num_lock_requests_ > 0) { | |
72 num_lock_requests_--; | |
73 UpdateWakeLock(); | |
74 } | |
75 } | |
31 | 76 |
32 wake_lock_request_outstanding_ = false; | 77 void WakeLockServiceImpl::HasWakeLockForTests( |
33 context_->CancelWakeLock(); | 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 wake_lock_ = base::MakeUnique<device::PowerSaveBlocker>( | |
96 type_, reason_, *description_, main_task_runner_, file_task_runner_); | |
97 | |
98 #if defined(OS_ANDROID) | |
blundell
2017/05/05 12:49:37
What I'
| |
99 gfx::NativeView native_view = native_view_getter_.Run(context_id_); | |
blundell
2017/05/05 12:49:37
What I'm saying here is that this class should be
ke.he
2017/05/05 14:56:36
Done.
| |
100 if (native_view) { | |
101 wake_lock_.get()->InitDisplaySleepBlocker(native_view); | |
102 } | |
103 #endif | |
104 } | |
105 | |
106 void WakeLockServiceImpl::RemoveWakeLock() { | |
107 DCHECK(wake_lock_); | |
108 wake_lock_.reset(); | |
109 } | |
110 | |
111 void WakeLockServiceImpl::OnConnectionError() { | |
112 if (binding_set_.dispatch_context()) { | |
blundell
2017/05/05 12:49:37
Hmm, why the need for this if?
ke.he
2017/05/05 14:56:36
it should be a DCHECK() instead of if().
DCHECK(bi
| |
113 // If the error-happening client's wakelock is in outstanding status, | |
114 // decrease the num_lock_requests and call UpdateWakeLock(). | |
115 if (*binding_set_.dispatch_context() && num_lock_requests_ > 0) { | |
116 num_lock_requests_--; | |
117 UpdateWakeLock(); | |
118 } | |
119 } | |
120 | |
121 // If |binding_set_| is empty, WakeLockServiceImpl should delele itself. | |
122 if (binding_set_.empty()) { | |
123 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this); | |
124 } | |
34 } | 125 } |
35 | 126 |
36 } // namespace device | 127 } // namespace device |
OLD | NEW |