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

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

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