Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(168)

Side by Side Diff: device/wake_lock/wake_lock_service_impl.cc

Issue 2867303003: Generalize the API of WakeLockContext mojo interface. (Closed)
Patch Set: Use switch case instead of static_cast Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « device/wake_lock/wake_lock_service_impl.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 namespace {
14
15 PowerSaveBlocker::PowerSaveBlockerType GetType(mojom::WakeLockType type) {
16 switch (type) {
17 case mojom::WakeLockType::PreventAppSuspension:
18 return PowerSaveBlocker::PowerSaveBlockerType::
19 kPowerSaveBlockPreventAppSuspension;
20 case mojom::WakeLockType::PreventDisplaySleep:
21 return PowerSaveBlocker::PowerSaveBlockerType::
22 kPowerSaveBlockPreventDisplaySleep;
23 }
24
25 NOTREACHED();
26 return PowerSaveBlocker::PowerSaveBlockerType::
27 kPowerSaveBlockPreventAppSuspension;
28 }
29
30 PowerSaveBlocker::Reason GetReason(mojom::WakeLockReason reason) {
31 switch (reason) {
32 case mojom::WakeLockReason::ReasonAudioPlayback:
33 return PowerSaveBlocker::Reason::kReasonAudioPlayback;
34 case mojom::WakeLockReason::ReasonVideoPlayback:
35 return PowerSaveBlocker::Reason::kReasonVideoPlayback;
36 case mojom::WakeLockReason::ReasonOther:
37 return PowerSaveBlocker::Reason::kReasonOther;
38 }
39
40 NOTREACHED();
41 return PowerSaveBlocker::Reason::kReasonOther;
42 }
43
44 } // namespace
45
13 WakeLockServiceImpl::WakeLockServiceImpl( 46 WakeLockServiceImpl::WakeLockServiceImpl(
14 mojom::WakeLockServiceRequest request, 47 mojom::WakeLockServiceRequest request,
15 device::PowerSaveBlocker::PowerSaveBlockerType type, 48 mojom::WakeLockType type,
16 device::PowerSaveBlocker::Reason reason, 49 mojom::WakeLockReason reason,
17 const std::string& description, 50 const std::string& description,
18 int context_id, 51 int context_id,
19 WakeLockContextCallback native_view_getter, 52 WakeLockContextCallback native_view_getter,
20 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) 53 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner)
21 : type_(type), 54 : type_(type),
22 reason_(reason), 55 reason_(reason),
23 description_(base::MakeUnique<std::string>(description)), 56 description_(base::MakeUnique<std::string>(description)),
24 num_lock_requests_(0), 57 num_lock_requests_(0),
25 #if defined(OS_ANDROID) 58 #if defined(OS_ANDROID)
26 context_id_(context_id), 59 context_id_(context_id),
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
81 CreateWakeLock(); 114 CreateWakeLock();
82 } else { 115 } else {
83 if (wake_lock_) 116 if (wake_lock_)
84 RemoveWakeLock(); 117 RemoveWakeLock();
85 } 118 }
86 } 119 }
87 120
88 void WakeLockServiceImpl::CreateWakeLock() { 121 void WakeLockServiceImpl::CreateWakeLock() {
89 DCHECK(!wake_lock_); 122 DCHECK(!wake_lock_);
90 123
91 if (type_ != device::PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep || 124 // TODO(heke): Switch PowerSaveBlocker to use mojom::WakeLockType and
92 reason_ != device::PowerSaveBlocker::kReasonOther || 125 // mojom::WakeLockReason once all its clients are converted to be the clients
93 *description_ != "Wake Lock API") { 126 // of WakeLock.
94 // TODO(ke.he@intel.com): Fully generalize the WakeLock interface and impl. 127 wake_lock_ = base::MakeUnique<PowerSaveBlocker>(
95 NOTREACHED(); 128 GetType(type_), GetReason(reason_), *description_, main_task_runner_,
blundell 2017/05/10 14:43:47 really tiny nit: Maybe ToPowerSaveBlockerType() an
129 file_task_runner_);
130
131 if (type_ != mojom::WakeLockType::PreventDisplaySleep)
96 return; 132 return;
97 }
98
99 wake_lock_ = base::MakeUnique<device::PowerSaveBlocker>(
100 type_, reason_, *description_, main_task_runner_, file_task_runner_);
101 133
102 #if defined(OS_ANDROID) 134 #if defined(OS_ANDROID)
103 gfx::NativeView native_view = native_view_getter_.Run(context_id_); 135 gfx::NativeView native_view = native_view_getter_.Run(context_id_);
104 if (native_view) 136 if (native_view)
105 wake_lock_.get()->InitDisplaySleepBlocker(native_view); 137 wake_lock_.get()->InitDisplaySleepBlocker(native_view);
106 #endif 138 #endif
107 } 139 }
108 140
109 void WakeLockServiceImpl::RemoveWakeLock() { 141 void WakeLockServiceImpl::RemoveWakeLock() {
110 DCHECK(wake_lock_); 142 DCHECK(wake_lock_);
111 wake_lock_.reset(); 143 wake_lock_.reset();
112 } 144 }
113 145
114 void WakeLockServiceImpl::OnConnectionError() { 146 void WakeLockServiceImpl::OnConnectionError() {
115 DCHECK(binding_set_.dispatch_context()); 147 DCHECK(binding_set_.dispatch_context());
116 148
117 // If the error-happening client's wakelock is in outstanding status, 149 // If the error-happening client's wakelock is in outstanding status,
118 // decrease the num_lock_requests and call UpdateWakeLock(). 150 // decrease the num_lock_requests and call UpdateWakeLock().
119 if (*binding_set_.dispatch_context() && num_lock_requests_ > 0) { 151 if (*binding_set_.dispatch_context() && num_lock_requests_ > 0) {
120 num_lock_requests_--; 152 num_lock_requests_--;
121 UpdateWakeLock(); 153 UpdateWakeLock();
122 } 154 }
123 155
124 // If |binding_set_| is empty, WakeLockServiceImpl should delele itself. 156 // If |binding_set_| is empty, WakeLockServiceImpl should delele itself.
125 if (binding_set_.empty()) 157 if (binding_set_.empty())
126 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this); 158 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this);
127 } 159 }
128 160
129 } // namespace device 161 } // namespace device
OLDNEW
« no previous file with comments | « device/wake_lock/wake_lock_service_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698