OLD | NEW |
---|---|
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 Loading... | |
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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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(void* object, |
135 const chromeos::PowerStatus& status) { | 112 const chromeos::PowerStatus& |
113 power_status) { | |
stevenjb
2011/10/24 20:36:21
nit: put 'void* object' on its own line so that po
Simon Que
2011/10/24 22:49:48
Done.
| |
114 // TODO(sque): this is a temporary copy-over from libcros. Soon libcros | |
115 // will be removed and this will not be necessary. | |
116 PowerSupplyStatus status = {}; | |
117 status.line_power_on = power_status.line_power_on; | |
118 status.battery_is_present = power_status.battery_is_present; | |
119 status.battery_is_full = | |
120 (power_status.battery_state == chromeos::BATTERY_STATE_FULLY_CHARGED); | |
121 status.battery_seconds_to_empty = power_status.battery_time_to_empty; | |
122 status.battery_seconds_to_full = power_status.battery_time_to_full; | |
123 status.battery_percentage = power_status.battery_percentage; | |
124 | |
136 PowerLibraryImpl* power = static_cast<PowerLibraryImpl*>(object); | 125 PowerLibraryImpl* power = static_cast<PowerLibraryImpl*>(object); |
137 power->UpdatePowerStatus(status); | 126 power->UpdatePowerStatus(status); |
138 } | 127 } |
139 | 128 |
140 static void SystemResumedHandler(void* object) { | 129 static void SystemResumedHandler(void* object) { |
141 PowerLibraryImpl* power = static_cast<PowerLibraryImpl*>(object); | 130 PowerLibraryImpl* power = static_cast<PowerLibraryImpl*>(object); |
142 power->SystemResumed(); | 131 power->SystemResumed(); |
143 } | 132 } |
144 | 133 |
145 void UpdatePowerStatus(const chromeos::PowerStatus& status) { | 134 void UpdatePowerStatus(const PowerSupplyStatus& status) { |
146 // Called from PowerStatusChangedHandler, a libcros callback which | 135 // Called from PowerStatusChangedHandler, a libcros callback which |
147 // should always run on UI thread. | 136 // should always run on UI thread. |
148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 137 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
149 | 138 |
150 DVLOG(1) << "Power lpo=" << status.line_power_on | 139 DVLOG(1) << "Power line_power_on = " << status.line_power_on |
151 << " sta=" << status.battery_state | 140 << " percentage = " << status.battery_percentage |
152 << " per=" << status.battery_percentage | 141 << " seconds_to_empty = " << status.battery_seconds_to_empty |
153 << " tte=" << status.battery_time_to_empty | 142 << " seconds_to_full = " << status.battery_seconds_to_full; |
154 << " ttf=" << status.battery_time_to_full; | 143 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(status)); |
155 status_ = status; | |
156 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(this)); | |
157 } | 144 } |
158 | 145 |
159 void SystemResumed() { | 146 void SystemResumed() { |
160 // Called from SystemResumedHandler, a libcros callback which should | 147 // Called from SystemResumedHandler, a libcros callback which should |
161 // always run on UI thread. | 148 // always run on UI thread. |
162 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 149 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
163 FOR_EACH_OBSERVER(Observer, observers_, SystemResumed()); | 150 FOR_EACH_OBSERVER(Observer, observers_, SystemResumed()); |
164 } | 151 } |
165 | 152 |
166 ObserverList<Observer> observers_; | 153 ObserverList<Observer> observers_; |
167 | 154 |
168 // A reference to the power battery API, to allow callbacks when the battery | 155 // A reference to the power battery API, to allow callbacks when the battery |
169 // status changes. | 156 // status changes. |
170 chromeos::PowerStatusConnection power_status_connection_; | 157 chromeos::PowerStatusConnection power_status_connection_; |
171 | 158 |
172 // A reference to the resume alerts. | 159 // A reference to the resume alerts. |
173 chromeos::ResumeConnection resume_status_connection_; | 160 chromeos::ResumeConnection resume_status_connection_; |
174 | 161 |
175 // The latest power status. | |
176 chromeos::PowerStatus status_; | |
177 | |
178 DISALLOW_COPY_AND_ASSIGN(PowerLibraryImpl); | 162 DISALLOW_COPY_AND_ASSIGN(PowerLibraryImpl); |
179 }; | 163 }; |
180 | 164 |
181 // The stub implementation runs the battery up and down, pausing at the | 165 // The stub implementation runs the battery up and down, pausing at the |
182 // fully charged and fully depleted states. | 166 // fully charged and fully depleted states. |
183 class PowerLibraryStubImpl : public PowerLibrary { | 167 class PowerLibraryStubImpl : public PowerLibrary { |
184 public: | 168 public: |
185 PowerLibraryStubImpl() | 169 PowerLibraryStubImpl() |
186 : discharging_(true), | 170 : discharging_(true), |
187 battery_percentage_(80), | 171 battery_percentage_(80), |
188 pause_count_(0) { | 172 pause_count_(0) { |
189 } | 173 } |
190 | 174 |
191 virtual ~PowerLibraryStubImpl() {} | 175 virtual ~PowerLibraryStubImpl() {} |
192 | 176 |
193 // Begin PowerLibrary implementation. | 177 // Begin PowerLibrary implementation. |
194 virtual void Init() OVERRIDE {} | 178 virtual void Init() OVERRIDE {} |
195 virtual void AddObserver(Observer* observer) OVERRIDE { | 179 virtual void AddObserver(Observer* observer) OVERRIDE { |
196 observers_.AddObserver(observer); | 180 observers_.AddObserver(observer); |
197 } | 181 } |
198 | 182 |
199 virtual void RemoveObserver(Observer* observer) OVERRIDE { | 183 virtual void RemoveObserver(Observer* observer) OVERRIDE { |
200 observers_.RemoveObserver(observer); | 184 observers_.RemoveObserver(observer); |
201 } | 185 } |
202 | 186 |
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 { | 187 virtual void CalculateIdleTime(CalculateIdleTimeCallback* callback) OVERRIDE { |
234 callback->Run(0); | 188 callback->Run(0); |
235 delete callback; | 189 delete callback; |
236 } | 190 } |
237 | 191 |
238 virtual void EnableScreenLock(bool enable) OVERRIDE {} | 192 virtual void EnableScreenLock(bool enable) OVERRIDE {} |
239 | 193 |
240 virtual void RequestRestart() OVERRIDE {} | 194 virtual void RequestRestart() OVERRIDE {} |
241 | 195 |
242 virtual void RequestShutdown() OVERRIDE {} | 196 virtual void RequestShutdown() OVERRIDE {} |
(...skipping 23 matching lines...) Expand all Loading... | |
266 if (battery_percentage_ == 0 || battery_percentage_ == 100) { | 220 if (battery_percentage_ == 0 || battery_percentage_ == 100) { |
267 if (pause_count_) { | 221 if (pause_count_) { |
268 pause_count_ = 0; | 222 pause_count_ = 0; |
269 discharging_ = !discharging_; | 223 discharging_ = !discharging_; |
270 } else { | 224 } else { |
271 pause_count_ = 20; | 225 pause_count_ = 20; |
272 return; | 226 return; |
273 } | 227 } |
274 } | 228 } |
275 battery_percentage_ += (discharging_ ? -1 : 1); | 229 battery_percentage_ += (discharging_ ? -1 : 1); |
276 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(this)); | 230 |
231 PowerSupplyStatus status = {}; | |
232 status.line_power_on = !discharging_; | |
233 status.battery_is_present = true; | |
234 status.battery_percentage = battery_percentage_; | |
235 status.battery_seconds_to_empty = | |
236 std::max(1, battery_percentage_ * 180 / 100); | |
237 status.battery_seconds_to_full = | |
238 std::max(static_cast<int64>(1), 180 - status.battery_seconds_to_empty); | |
239 | |
240 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(status)); | |
277 } | 241 } |
278 | 242 |
279 bool discharging_; | 243 bool discharging_; |
280 int battery_percentage_; | 244 int battery_percentage_; |
281 int pause_count_; | 245 int pause_count_; |
282 ObserverList<Observer> observers_; | 246 ObserverList<Observer> observers_; |
283 base::RepeatingTimer<PowerLibraryStubImpl> timer_; | 247 base::RepeatingTimer<PowerLibraryStubImpl> timer_; |
284 }; | 248 }; |
285 | 249 |
286 // static | 250 // static |
287 PowerLibrary* PowerLibrary::GetImpl(bool stub) { | 251 PowerLibrary* PowerLibrary::GetImpl(bool stub) { |
288 PowerLibrary* impl; | 252 PowerLibrary* impl; |
289 if (stub) | 253 if (stub) |
290 impl = new PowerLibraryStubImpl(); | 254 impl = new PowerLibraryStubImpl(); |
291 else | 255 else |
292 impl = new PowerLibraryImpl(); | 256 impl = new PowerLibraryImpl(); |
293 impl->Init(); | 257 impl->Init(); |
294 return impl; | 258 return impl; |
295 } | 259 } |
296 | 260 |
297 } // namespace chromeos | 261 } // namespace chromeos |
OLD | NEW |