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 "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/observer_list.h" | 10 #include "base/observer_list.h" |
11 #include "base/time.h" | 11 #include "base/time.h" |
12 #include "base/timer.h" | |
12 #include "chrome/browser/chromeos/cros/cros_library.h" | 13 #include "chrome/browser/chromeos/cros/cros_library.h" |
13 #include "content/browser/browser_thread.h" | 14 #include "content/browser/browser_thread.h" |
14 #include "third_party/cros/chromeos_power.h" | 15 #include "third_party/cros/chromeos_power.h" |
15 #include "third_party/cros/chromeos_resume.h" | 16 #include "third_party/cros/chromeos_resume.h" |
16 | 17 |
17 namespace chromeos { | 18 namespace chromeos { |
18 | 19 |
19 class PowerLibraryImpl : public PowerLibrary { | 20 class PowerLibraryImpl : public PowerLibrary { |
20 public: | 21 public: |
21 PowerLibraryImpl() | 22 PowerLibraryImpl() |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
155 | 156 |
156 // A reference to the resume alerts. | 157 // A reference to the resume alerts. |
157 chromeos::ResumeConnection resume_status_connection_; | 158 chromeos::ResumeConnection resume_status_connection_; |
158 | 159 |
159 // The latest power status. | 160 // The latest power status. |
160 chromeos::PowerStatus status_; | 161 chromeos::PowerStatus status_; |
161 | 162 |
162 DISALLOW_COPY_AND_ASSIGN(PowerLibraryImpl); | 163 DISALLOW_COPY_AND_ASSIGN(PowerLibraryImpl); |
163 }; | 164 }; |
164 | 165 |
165 class PowerLibraryStubImpl : public PowerLibrary { | 166 class PowerLibraryStubImpl : public PowerLibrary { |
achuithb
2011/08/09 18:58:07
Maybe a comment here explaining what you're doing?
DaveMoore
2011/08/12 21:03:11
Done.
| |
166 public: | 167 public: |
167 PowerLibraryStubImpl() {} | 168 PowerLibraryStubImpl() |
169 : discharging_(true), | |
170 battery_percentage_(20), | |
171 pause_count_(0) { | |
172 timer_.Start( | |
173 base::TimeDelta::FromMilliseconds(100), | |
174 this, | |
175 &PowerLibraryStubImpl::Update); | |
176 } | |
177 | |
168 virtual ~PowerLibraryStubImpl() {} | 178 virtual ~PowerLibraryStubImpl() {} |
169 | 179 |
170 // Begin PowerLibrary implementation. | 180 // Begin PowerLibrary implementation. |
171 virtual void Init() OVERRIDE {} | 181 virtual void Init() OVERRIDE {} |
172 virtual void AddObserver(Observer* observer) OVERRIDE {} | 182 virtual void AddObserver(Observer* observer) OVERRIDE { |
173 virtual void RemoveObserver(Observer* observer) OVERRIDE {} | 183 observers_.AddObserver(observer); |
174 virtual bool line_power_on() const OVERRIDE { return false; } | 184 } |
175 virtual bool battery_fully_charged() const OVERRIDE { return false; } | 185 |
176 virtual double battery_percentage() const OVERRIDE { return 50.0; } | 186 virtual void RemoveObserver(Observer* observer) OVERRIDE { |
177 virtual bool battery_is_present() const OVERRIDE { return true; } | 187 observers_.RemoveObserver(observer); |
188 } | |
189 | |
190 virtual bool line_power_on() const OVERRIDE { | |
191 return !discharging_; | |
192 } | |
193 | |
194 virtual bool battery_fully_charged() const OVERRIDE { | |
195 return battery_percentage_ == 100; | |
196 } | |
197 | |
198 virtual double battery_percentage() const OVERRIDE { | |
199 return battery_percentage_; | |
200 } | |
201 | |
202 virtual bool battery_is_present() const OVERRIDE { | |
203 return true; | |
204 } | |
205 | |
178 virtual base::TimeDelta battery_time_to_empty() const OVERRIDE { | 206 virtual base::TimeDelta battery_time_to_empty() const OVERRIDE { |
179 return base::TimeDelta::FromSeconds(10 * 60); | 207 if (battery_percentage_ == 0) |
208 return base::TimeDelta::FromSeconds(1); | |
209 else | |
210 return (base::TimeDelta::FromHours(3) * battery_percentage_) / 100; | |
180 } | 211 } |
212 | |
181 virtual base::TimeDelta battery_time_to_full() const OVERRIDE { | 213 virtual base::TimeDelta battery_time_to_full() const OVERRIDE { |
182 return base::TimeDelta::FromSeconds(0); | 214 if (battery_percentage_ == 0) |
215 return base::TimeDelta::FromSeconds(1); | |
216 else | |
217 return base::TimeDelta::FromHours(3) - battery_time_to_empty(); | |
183 } | 218 } |
219 | |
184 virtual void EnableScreenLock(bool enable) OVERRIDE {} | 220 virtual void EnableScreenLock(bool enable) OVERRIDE {} |
185 virtual void RequestRestart() OVERRIDE {} | 221 virtual void RequestRestart() OVERRIDE {} |
186 virtual void RequestShutdown() OVERRIDE {} | 222 virtual void RequestShutdown() OVERRIDE {} |
187 // End PowerLibrary implementation. | 223 // End PowerLibrary implementation. |
224 | |
225 private: | |
226 void Update() { | |
227 // We pause at 0 and 100% so that it's easier to check those conditions. | |
228 if (pause_count_ > 1) { | |
229 pause_count_--; | |
230 return; | |
231 } | |
232 | |
233 if (battery_percentage_ == 0 || battery_percentage_ == 100) { | |
234 if (pause_count_) { | |
235 pause_count_ = 0; | |
236 discharging_ = !discharging_; | |
237 } else { | |
238 pause_count_ = 20; | |
239 return; | |
240 } | |
241 } | |
242 battery_percentage_ += (discharging_ ? -1 : 1); | |
243 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(this)); | |
244 } | |
achuithb
2011/08/09 18:58:07
newline here?
DaveMoore
2011/08/12 21:03:11
Done.
| |
245 bool discharging_; | |
246 int battery_percentage_; | |
247 int pause_count_; | |
248 ObserverList<Observer> observers_; | |
249 base::RepeatingTimer<PowerLibraryStubImpl> timer_; | |
188 }; | 250 }; |
189 | 251 |
190 // static | 252 // static |
191 PowerLibrary* PowerLibrary::GetImpl(bool stub) { | 253 PowerLibrary* PowerLibrary::GetImpl(bool stub) { |
192 PowerLibrary* impl; | 254 PowerLibrary* impl; |
193 if (stub) | 255 if (stub) |
194 impl = new PowerLibraryStubImpl(); | 256 impl = new PowerLibraryStubImpl(); |
195 else | 257 else |
196 impl = new PowerLibraryImpl(); | 258 impl = new PowerLibraryImpl(); |
197 impl->Init(); | 259 impl->Init(); |
198 return impl; | 260 return impl; |
199 } | 261 } |
200 | 262 |
201 } // namespace chromeos | 263 } // namespace chromeos |
202 | 264 |
203 // Allows InvokeLater without adding refcounting. This class is a Singleton and | 265 // Allows InvokeLater without adding refcounting. This class is a Singleton and |
204 // won't be deleted until it's last InvokeLater is run. | 266 // won't be deleted until it's last InvokeLater is run. |
205 DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::PowerLibraryImpl); | 267 DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::PowerLibraryImpl); |
OLD | NEW |