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

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

Issue 8347016: chromeos: Simplify power supply info in PowerLibrary (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Various comment fixes Created 9 years, 2 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
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>
8
9 #include "base/basictypes.h"
7 #include "base/bind.h" 10 #include "base/bind.h"
8 #include "base/basictypes.h"
9 #include "base/compiler_specific.h" 11 #include "base/compiler_specific.h"
10 #include "base/logging.h" 12 #include "base/logging.h"
11 #include "base/observer_list.h" 13 #include "base/observer_list.h"
12 #include "base/time.h" 14 #include "base/time.h"
13 #include "base/timer.h" 15 #include "base/timer.h"
14 #include "chrome/browser/chromeos/cros/cros_library.h" 16 #include "chrome/browser/chromeos/cros/cros_library.h"
15 #include "content/browser/browser_thread.h" 17 #include "content/browser/browser_thread.h"
16 #include "third_party/cros/chromeos_power.h" 18 #include "third_party/cros/chromeos_power.h"
17 #include "third_party/cros/chromeos_resume.h" 19 #include "third_party/cros/chromeos_resume.h"
18 20
19 namespace chromeos { 21 namespace chromeos {
20 22
21 class PowerLibraryImpl : public PowerLibrary { 23 class PowerLibraryImpl : public PowerLibrary {
22 public: 24 public:
23 PowerLibraryImpl() 25 PowerLibraryImpl()
24 : power_status_connection_(NULL), 26 : power_status_connection_(NULL),
25 resume_status_connection_(NULL), 27 resume_status_connection_(NULL) {
26 status_(chromeos::PowerStatus()) {
27 } 28 }
28 29
29 virtual ~PowerLibraryImpl() { 30 virtual ~PowerLibraryImpl() {
30 if (power_status_connection_) { 31 if (power_status_connection_) {
31 chromeos::DisconnectPowerStatus(power_status_connection_); 32 chromeos::DisconnectPowerStatus(power_status_connection_);
32 power_status_connection_ = NULL; 33 power_status_connection_ = NULL;
33 } 34 }
34 if (resume_status_connection_) { 35 if (resume_status_connection_) {
35 chromeos::DisconnectResume(resume_status_connection_); 36 chromeos::DisconnectResume(resume_status_connection_);
36 resume_status_connection_ = NULL; 37 resume_status_connection_ = NULL;
(...skipping 10 matching lines...) Expand all
47 } 48 }
48 49
49 virtual void AddObserver(Observer* observer) OVERRIDE { 50 virtual void AddObserver(Observer* observer) OVERRIDE {
50 observers_.AddObserver(observer); 51 observers_.AddObserver(observer);
51 } 52 }
52 53
53 virtual void RemoveObserver(Observer* observer) OVERRIDE { 54 virtual void RemoveObserver(Observer* observer) OVERRIDE {
54 observers_.RemoveObserver(observer); 55 observers_.RemoveObserver(observer);
55 } 56 }
56 57
57 virtual bool IsLinePowerOn() const OVERRIDE {
58 return status_.line_power_on;
59 }
60
61 virtual bool IsBatteryFullyCharged() const OVERRIDE {
62 return status_.battery_state == chromeos::BATTERY_STATE_FULLY_CHARGED;
63 }
64
65 virtual double GetBatteryPercentage() const OVERRIDE {
66 return status_.battery_percentage;
67 }
68
69 virtual bool IsBatteryPresent() const OVERRIDE {
70 return status_.battery_is_present;
71 }
72
73 virtual base::TimeDelta GetBatteryTimeToEmpty() const OVERRIDE {
74 return base::TimeDelta::FromSeconds(status_.battery_time_to_empty);
75 }
76
77 virtual base::TimeDelta GetBatteryTimeToFull() const OVERRIDE {
78 return base::TimeDelta::FromSeconds(status_.battery_time_to_full);
79 }
80
81 virtual void CalculateIdleTime(CalculateIdleTimeCallback* callback) OVERRIDE { 58 virtual void CalculateIdleTime(CalculateIdleTimeCallback* callback) OVERRIDE {
82 // TODO(sidor): Examine if it's really a good idea to use void* as a second 59 // TODO(sidor): Examine if it's really a good idea to use void* as a second
83 // argument. 60 // argument.
84 chromeos::GetIdleTime(&GetIdleTimeCallback, callback); 61 chromeos::GetIdleTime(&GetIdleTimeCallback, callback);
85 } 62 }
86 63
87 virtual void EnableScreenLock(bool enable) OVERRIDE { 64 virtual void EnableScreenLock(bool enable) OVERRIDE {
88 // Called when the screen preference is changed, which should always 65 // Called when the screen preference is changed, which should always
89 // run on UI thread. 66 // run on UI thread.
90 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 static_cast<CalculateIdleTimeCallback*>(object); 101 static_cast<CalculateIdleTimeCallback*>(object);
125 if (success) { 102 if (success) {
126 notify->Run(time_idle_ms/1000); 103 notify->Run(time_idle_ms/1000);
127 } else { 104 } else {
128 LOG(ERROR) << "Power manager failed to calculate idle time."; 105 LOG(ERROR) << "Power manager failed to calculate idle time.";
129 notify->Run(-1); 106 notify->Run(-1);
130 } 107 }
131 delete notify; 108 delete notify;
132 } 109 }
133 110
134 static void PowerStatusChangedHandler(void* object, 111 static void PowerStatusChangedHandler(
135 const chromeos::PowerStatus& status) { 112 void* object, const chromeos::PowerStatus& power_status) {
113 // TODO(sque): this is a temporary copy-over from libcros. Soon libcros
114 // will be removed and this will not be necessary.
115 PowerSupplyStatus status = {};
116 status.line_power_on = power_status.line_power_on;
117 status.battery_is_present = power_status.battery_is_present;
118 status.battery_is_full =
119 (power_status.battery_state == chromeos::BATTERY_STATE_FULLY_CHARGED);
120 status.battery_seconds_to_empty = power_status.battery_time_to_empty;
121 status.battery_seconds_to_full = power_status.battery_time_to_full;
122 status.battery_percentage = power_status.battery_percentage;
123
136 PowerLibraryImpl* power = static_cast<PowerLibraryImpl*>(object); 124 PowerLibraryImpl* power = static_cast<PowerLibraryImpl*>(object);
137 power->UpdatePowerStatus(status); 125 power->UpdatePowerStatus(status);
138 } 126 }
139 127
140 static void SystemResumedHandler(void* object) { 128 static void SystemResumedHandler(void* object) {
141 PowerLibraryImpl* power = static_cast<PowerLibraryImpl*>(object); 129 PowerLibraryImpl* power = static_cast<PowerLibraryImpl*>(object);
142 power->SystemResumed(); 130 power->SystemResumed();
143 } 131 }
144 132
145 void UpdatePowerStatus(const chromeos::PowerStatus& status) { 133 void UpdatePowerStatus(const PowerSupplyStatus& status) {
146 // Called from PowerStatusChangedHandler, a libcros callback which 134 // Called from PowerStatusChangedHandler, a libcros callback which
147 // should always run on UI thread. 135 // should always run on UI thread.
148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 136 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
149 137
150 DVLOG(1) << "Power lpo=" << status.line_power_on 138 DVLOG(1) << "Power line_power_on = " << status.line_power_on
151 << " sta=" << status.battery_state 139 << " percentage = " << status.battery_percentage
152 << " per=" << status.battery_percentage 140 << " seconds_to_empty = " << status.battery_seconds_to_empty
153 << " tte=" << status.battery_time_to_empty 141 << " seconds_to_full = " << status.battery_seconds_to_full;
154 << " ttf=" << status.battery_time_to_full; 142 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(status));
155 status_ = status;
156 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(this));
157 } 143 }
158 144
159 void SystemResumed() { 145 void SystemResumed() {
160 // Called from SystemResumedHandler, a libcros callback which should 146 // Called from SystemResumedHandler, a libcros callback which should
161 // always run on UI thread. 147 // always run on UI thread.
162 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
163 FOR_EACH_OBSERVER(Observer, observers_, SystemResumed()); 149 FOR_EACH_OBSERVER(Observer, observers_, SystemResumed());
164 } 150 }
165 151
166 ObserverList<Observer> observers_; 152 ObserverList<Observer> observers_;
167 153
168 // A reference to the power battery API, to allow callbacks when the battery 154 // A reference to the power battery API, to allow callbacks when the battery
169 // status changes. 155 // status changes.
170 chromeos::PowerStatusConnection power_status_connection_; 156 chromeos::PowerStatusConnection power_status_connection_;
171 157
172 // A reference to the resume alerts. 158 // A reference to the resume alerts.
173 chromeos::ResumeConnection resume_status_connection_; 159 chromeos::ResumeConnection resume_status_connection_;
174 160
175 // The latest power status.
176 chromeos::PowerStatus status_;
177
178 DISALLOW_COPY_AND_ASSIGN(PowerLibraryImpl); 161 DISALLOW_COPY_AND_ASSIGN(PowerLibraryImpl);
179 }; 162 };
180 163
181 // The stub implementation runs the battery up and down, pausing at the 164 // The stub implementation runs the battery up and down, pausing at the
182 // fully charged and fully depleted states. 165 // fully charged and fully depleted states.
183 class PowerLibraryStubImpl : public PowerLibrary { 166 class PowerLibraryStubImpl : public PowerLibrary {
184 public: 167 public:
185 PowerLibraryStubImpl() 168 PowerLibraryStubImpl()
186 : discharging_(true), 169 : discharging_(true),
187 battery_percentage_(80), 170 battery_percentage_(80),
188 pause_count_(0) { 171 pause_count_(0) {
189 } 172 }
190 173
191 virtual ~PowerLibraryStubImpl() {} 174 virtual ~PowerLibraryStubImpl() {}
192 175
193 // Begin PowerLibrary implementation. 176 // Begin PowerLibrary implementation.
194 virtual void Init() OVERRIDE {} 177 virtual void Init() OVERRIDE {}
195 virtual void AddObserver(Observer* observer) OVERRIDE { 178 virtual void AddObserver(Observer* observer) OVERRIDE {
196 observers_.AddObserver(observer); 179 observers_.AddObserver(observer);
197 } 180 }
198 181
199 virtual void RemoveObserver(Observer* observer) OVERRIDE { 182 virtual void RemoveObserver(Observer* observer) OVERRIDE {
200 observers_.RemoveObserver(observer); 183 observers_.RemoveObserver(observer);
201 } 184 }
202 185
203 virtual bool IsLinePowerOn() const OVERRIDE {
204 return !discharging_;
205 }
206
207 virtual bool IsBatteryFullyCharged() const OVERRIDE {
208 return battery_percentage_ == 100;
209 }
210
211 virtual double GetBatteryPercentage() const OVERRIDE {
212 return battery_percentage_;
213 }
214
215 virtual bool IsBatteryPresent() const OVERRIDE {
216 return true;
217 }
218
219 virtual base::TimeDelta GetBatteryTimeToEmpty() const OVERRIDE {
220 if (battery_percentage_ == 0)
221 return base::TimeDelta::FromSeconds(1);
222 else
223 return (base::TimeDelta::FromHours(3) * battery_percentage_) / 100;
224 }
225
226 virtual base::TimeDelta GetBatteryTimeToFull() const OVERRIDE {
227 if (battery_percentage_ == 100)
228 return base::TimeDelta::FromSeconds(1);
229 else
230 return base::TimeDelta::FromHours(3) - GetBatteryTimeToEmpty();
231 }
232
233 virtual void CalculateIdleTime(CalculateIdleTimeCallback* callback) OVERRIDE { 186 virtual void CalculateIdleTime(CalculateIdleTimeCallback* callback) OVERRIDE {
234 callback->Run(0); 187 callback->Run(0);
235 delete callback; 188 delete callback;
236 } 189 }
237 190
238 virtual void EnableScreenLock(bool enable) OVERRIDE {} 191 virtual void EnableScreenLock(bool enable) OVERRIDE {}
239 192
240 virtual void RequestRestart() OVERRIDE {} 193 virtual void RequestRestart() OVERRIDE {}
241 194
242 virtual void RequestShutdown() OVERRIDE {} 195 virtual void RequestShutdown() OVERRIDE {}
(...skipping 23 matching lines...) Expand all
266 if (battery_percentage_ == 0 || battery_percentage_ == 100) { 219 if (battery_percentage_ == 0 || battery_percentage_ == 100) {
267 if (pause_count_) { 220 if (pause_count_) {
268 pause_count_ = 0; 221 pause_count_ = 0;
269 discharging_ = !discharging_; 222 discharging_ = !discharging_;
270 } else { 223 } else {
271 pause_count_ = 20; 224 pause_count_ = 20;
272 return; 225 return;
273 } 226 }
274 } 227 }
275 battery_percentage_ += (discharging_ ? -1 : 1); 228 battery_percentage_ += (discharging_ ? -1 : 1);
276 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(this)); 229
230 PowerSupplyStatus status = {}; // Zero-clear the status.
231 status.line_power_on = !discharging_;
232 status.battery_is_present = true;
233 status.battery_percentage = battery_percentage_;
234 status.battery_seconds_to_empty =
235 std::max(1, battery_percentage_ * 180 / 100);
236 status.battery_seconds_to_full =
237 std::max(static_cast<int64>(1), 180 - status.battery_seconds_to_empty);
238
239 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(status));
277 } 240 }
278 241
279 bool discharging_; 242 bool discharging_;
280 int battery_percentage_; 243 int battery_percentage_;
281 int pause_count_; 244 int pause_count_;
282 ObserverList<Observer> observers_; 245 ObserverList<Observer> observers_;
283 base::RepeatingTimer<PowerLibraryStubImpl> timer_; 246 base::RepeatingTimer<PowerLibraryStubImpl> timer_;
284 }; 247 };
285 248
286 // static 249 // static
287 PowerLibrary* PowerLibrary::GetImpl(bool stub) { 250 PowerLibrary* PowerLibrary::GetImpl(bool stub) {
288 PowerLibrary* impl; 251 PowerLibrary* impl;
289 if (stub) 252 if (stub)
290 impl = new PowerLibraryStubImpl(); 253 impl = new PowerLibraryStubImpl();
291 else 254 else
292 impl = new PowerLibraryImpl(); 255 impl = new PowerLibraryImpl();
293 impl->Init(); 256 impl->Init();
294 return impl; 257 return impl;
295 } 258 }
296 259
297 } // namespace chromeos 260 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/cros/power_library.h ('k') | chrome/browser/chromeos/low_battery_observer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698