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

Side by Side Diff: chrome/browser/chromeos/cros/power_library.cc

Issue 8271024: chromeos: Add power supply info reading capability (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: cleanup comments and mock func Created 9 years, 1 month 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
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "chrome/browser/chromeos/cros/power_library.h" 5 #include "chrome/browser/chromeos/cros/power_library.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/compiler_specific.h" 11 #include "base/compiler_specific.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/observer_list.h" 13 #include "base/observer_list.h"
14 #include "base/time.h"
15 #include "base/timer.h"
16 #include "chrome/browser/chromeos/cros/cros_library.h" 14 #include "chrome/browser/chromeos/cros/cros_library.h"
17 #include "content/public/browser/browser_thread.h" 15 #include "content/public/browser/browser_thread.h"
18 #include "third_party/cros/chromeos_power.h" 16 #include "third_party/cros/chromeos_power.h"
19 #include "third_party/cros/chromeos_resume.h" 17 #include "third_party/cros/chromeos_resume.h"
20 18
21 using content::BrowserThread; 19 using content::BrowserThread;
22 20
23 namespace chromeos { 21 namespace chromeos {
24 22
25 class PowerLibraryImpl : public PowerLibrary { 23 class PowerLibraryImpl : public PowerLibrary {
26 public: 24 public:
27 PowerLibraryImpl() 25 PowerLibraryImpl()
28 : power_status_connection_(NULL), 26 : resume_status_connection_(NULL) {
29 resume_status_connection_(NULL) {
30 } 27 }
31 28
32 virtual ~PowerLibraryImpl() { 29 virtual ~PowerLibraryImpl() {
33 if (power_status_connection_) {
34 chromeos::DisconnectPowerStatus(power_status_connection_);
35 power_status_connection_ = NULL;
36 }
37 if (resume_status_connection_) { 30 if (resume_status_connection_) {
38 chromeos::DisconnectResume(resume_status_connection_); 31 chromeos::DisconnectResume(resume_status_connection_);
39 resume_status_connection_ = NULL; 32 resume_status_connection_ = NULL;
40 } 33 }
41 } 34 }
42 35
43 // Begin PowerLibrary implementation. 36 // Begin PowerLibrary implementation.
44 virtual void Init() OVERRIDE { 37 virtual void Init() OVERRIDE {
45 DCHECK(CrosLibrary::Get()->libcros_loaded()); 38 DCHECK(CrosLibrary::Get()->libcros_loaded());
46 power_status_connection_ =
47 chromeos::MonitorPowerStatus(&PowerStatusChangedHandler, this);
48 resume_status_connection_ = 39 resume_status_connection_ =
49 chromeos::MonitorResume(&SystemResumedHandler, this); 40 chromeos::MonitorResume(&SystemResumedHandler, this);
50 } 41 }
51 42
52 virtual void AddObserver(Observer* observer) OVERRIDE { 43 virtual void AddObserver(Observer* observer) OVERRIDE {
53 observers_.AddObserver(observer); 44 observers_.AddObserver(observer);
54 } 45 }
55 46
56 virtual void RemoveObserver(Observer* observer) OVERRIDE { 47 virtual void RemoveObserver(Observer* observer) OVERRIDE {
57 observers_.RemoveObserver(observer); 48 observers_.RemoveObserver(observer);
(...skipping 17 matching lines...) Expand all
75 } 66 }
76 67
77 virtual void RequestRestart() OVERRIDE { 68 virtual void RequestRestart() OVERRIDE {
78 chromeos::RequestRestart(); 69 chromeos::RequestRestart();
79 } 70 }
80 71
81 virtual void RequestShutdown() OVERRIDE { 72 virtual void RequestShutdown() OVERRIDE {
82 chromeos::RequestShutdown(); 73 chromeos::RequestShutdown();
83 } 74 }
84 75
85 virtual void RequestStatusUpdate() OVERRIDE {
86 // TODO(stevenjb): chromeos::RetrievePowerInformation has been deprecated;
87 // we should add a mechanism to immediately request an update, probably
88 // when we migrate the DBus code from libcros to here.
89 }
90
91 // End PowerLibrary implementation. 76 // End PowerLibrary implementation.
92 77
93 private: 78 private:
94 static void DoEnableScreenLock(bool enable) { 79 static void DoEnableScreenLock(bool enable) {
95 chromeos::EnableScreenLock(enable); 80 chromeos::EnableScreenLock(enable);
96 } 81 }
97 82
98 static void GetIdleTimeCallback(void* object, 83 static void GetIdleTimeCallback(void* object,
99 int64_t time_idle_ms, 84 int64_t time_idle_ms,
100 bool success) { 85 bool success) {
101 DCHECK(object); 86 DCHECK(object);
102 CalculateIdleTimeCallback* notify = 87 CalculateIdleTimeCallback* notify =
103 static_cast<CalculateIdleTimeCallback*>(object); 88 static_cast<CalculateIdleTimeCallback*>(object);
104 if (success) { 89 if (success) {
105 notify->Run(time_idle_ms/1000); 90 notify->Run(time_idle_ms/1000);
106 } else { 91 } else {
107 LOG(ERROR) << "Power manager failed to calculate idle time."; 92 LOG(ERROR) << "Power manager failed to calculate idle time.";
108 notify->Run(-1); 93 notify->Run(-1);
109 } 94 }
110 delete notify; 95 delete notify;
111 } 96 }
112 97
113 static void PowerStatusChangedHandler(
114 void* object, const chromeos::PowerStatus& power_status) {
115 // TODO(sque): this is a temporary copy-over from libcros. Soon libcros
116 // will be removed and this will not be necessary.
117 PowerSupplyStatus status;
118 status.line_power_on = power_status.line_power_on;
119 status.battery_is_present = power_status.battery_is_present;
120 status.battery_is_full =
121 (power_status.battery_state == chromeos::BATTERY_STATE_FULLY_CHARGED);
122 status.battery_seconds_to_empty = power_status.battery_time_to_empty;
123 status.battery_seconds_to_full = power_status.battery_time_to_full;
124 status.battery_percentage = power_status.battery_percentage;
125
126 PowerLibraryImpl* power = static_cast<PowerLibraryImpl*>(object);
127 power->UpdatePowerStatus(status);
128 }
129
130 static void SystemResumedHandler(void* object) { 98 static void SystemResumedHandler(void* object) {
131 PowerLibraryImpl* power = static_cast<PowerLibraryImpl*>(object); 99 PowerLibraryImpl* power = static_cast<PowerLibraryImpl*>(object);
132 power->SystemResumed(); 100 power->SystemResumed();
133 } 101 }
134 102
135 void UpdatePowerStatus(const PowerSupplyStatus& status) {
136 // Called from PowerStatusChangedHandler, a libcros callback which
137 // should always run on UI thread.
138 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
139
140 DVLOG(1) << "Power line_power_on = " << status.line_power_on
141 << " percentage = " << status.battery_percentage
142 << " seconds_to_empty = " << status.battery_seconds_to_empty
143 << " seconds_to_full = " << status.battery_seconds_to_full;
144 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(status));
145 }
146
147 void SystemResumed() { 103 void SystemResumed() {
148 // Called from SystemResumedHandler, a libcros callback which should 104 // Called from SystemResumedHandler, a libcros callback which should
149 // always run on UI thread. 105 // always run on UI thread.
150 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 106 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
151 FOR_EACH_OBSERVER(Observer, observers_, SystemResumed()); 107 FOR_EACH_OBSERVER(Observer, observers_, SystemResumed());
152 } 108 }
153 109
154 ObserverList<Observer> observers_; 110 ObserverList<Observer> observers_;
155 111
156 // A reference to the power battery API, to allow callbacks when the battery
157 // status changes.
158 chromeos::PowerStatusConnection power_status_connection_;
159
160 // A reference to the resume alerts. 112 // A reference to the resume alerts.
161 chromeos::ResumeConnection resume_status_connection_; 113 chromeos::ResumeConnection resume_status_connection_;
162 114
163 DISALLOW_COPY_AND_ASSIGN(PowerLibraryImpl); 115 DISALLOW_COPY_AND_ASSIGN(PowerLibraryImpl);
164 }; 116 };
165 117
166 // The stub implementation runs the battery up and down, pausing at the 118 // The stub implementation runs the battery up and down, pausing at the
167 // fully charged and fully depleted states. 119 // fully charged and fully depleted states.
168 class PowerLibraryStubImpl : public PowerLibrary { 120 class PowerLibraryStubImpl : public PowerLibrary {
169 public: 121 public:
170 PowerLibraryStubImpl() 122 PowerLibraryStubImpl() {
171 : discharging_(true),
172 battery_percentage_(80),
173 pause_count_(0) {
174 } 123 }
175 124
176 virtual ~PowerLibraryStubImpl() {} 125 virtual ~PowerLibraryStubImpl() {}
177 126
178 // Begin PowerLibrary implementation. 127 // Begin PowerLibrary implementation.
179 virtual void Init() OVERRIDE {} 128 virtual void Init() OVERRIDE {}
180 virtual void AddObserver(Observer* observer) OVERRIDE { 129 virtual void AddObserver(Observer* observer) OVERRIDE {
181 observers_.AddObserver(observer); 130 observers_.AddObserver(observer);
182 } 131 }
183 132
184 virtual void RemoveObserver(Observer* observer) OVERRIDE { 133 virtual void RemoveObserver(Observer* observer) OVERRIDE {
185 observers_.RemoveObserver(observer); 134 observers_.RemoveObserver(observer);
186 } 135 }
187 136
188 virtual void CalculateIdleTime(CalculateIdleTimeCallback* callback) OVERRIDE { 137 virtual void CalculateIdleTime(CalculateIdleTimeCallback* callback) OVERRIDE {
189 callback->Run(0); 138 callback->Run(0);
190 delete callback; 139 delete callback;
191 } 140 }
192 141
193 virtual void EnableScreenLock(bool enable) OVERRIDE {} 142 virtual void EnableScreenLock(bool enable) OVERRIDE {}
194 143
195 virtual void RequestRestart() OVERRIDE {} 144 virtual void RequestRestart() OVERRIDE {}
196 145
197 virtual void RequestShutdown() OVERRIDE {} 146 virtual void RequestShutdown() OVERRIDE {}
198 147
199 virtual void RequestStatusUpdate() OVERRIDE {
200 if (!timer_.IsRunning()) {
201 timer_.Start(
202 FROM_HERE,
203 base::TimeDelta::FromMilliseconds(100),
204 this,
205 &PowerLibraryStubImpl::Update);
206 } else {
207 timer_.Stop();
208 }
209 }
210
211 // End PowerLibrary implementation. 148 // End PowerLibrary implementation.
212
213 private: 149 private:
214 void Update() {
215 // We pause at 0 and 100% so that it's easier to check those conditions.
216 if (pause_count_ > 1) {
217 pause_count_--;
218 return;
219 }
220
221 if (battery_percentage_ == 0 || battery_percentage_ == 100) {
222 if (pause_count_) {
223 pause_count_ = 0;
224 discharging_ = !discharging_;
225 } else {
226 pause_count_ = 20;
227 return;
228 }
229 }
230 battery_percentage_ += (discharging_ ? -1 : 1);
231
232 PowerSupplyStatus status;
233 status.line_power_on = !discharging_;
234 status.battery_is_present = true;
235 status.battery_percentage = battery_percentage_;
236 status.battery_seconds_to_empty =
237 std::max(1, battery_percentage_ * 180 / 100);
238 status.battery_seconds_to_full =
239 std::max(static_cast<int64>(1), 180 - status.battery_seconds_to_empty);
240
241 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(status));
242 }
243
244 bool discharging_;
245 int battery_percentage_;
246 int pause_count_;
247 ObserverList<Observer> observers_; 150 ObserverList<Observer> observers_;
248 base::RepeatingTimer<PowerLibraryStubImpl> timer_;
249 }; 151 };
250 152
251 // static 153 // static
252 PowerLibrary* PowerLibrary::GetImpl(bool stub) { 154 PowerLibrary* PowerLibrary::GetImpl(bool stub) {
253 PowerLibrary* impl; 155 PowerLibrary* impl;
254 if (stub) 156 if (stub)
255 impl = new PowerLibraryStubImpl(); 157 impl = new PowerLibraryStubImpl();
256 else 158 else
257 impl = new PowerLibraryImpl(); 159 impl = new PowerLibraryImpl();
258 impl->Init(); 160 impl->Init();
259 return impl; 161 return impl;
260 } 162 }
261 163
262 } // namespace chromeos 164 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698