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 |
| 166 // The stub implementation runs the battery up and down, pausing at the |
| 167 // fully charged and fully depleted states. |
165 class PowerLibraryStubImpl : public PowerLibrary { | 168 class PowerLibraryStubImpl : public PowerLibrary { |
166 public: | 169 public: |
167 PowerLibraryStubImpl() {} | 170 PowerLibraryStubImpl() |
| 171 : discharging_(true), |
| 172 battery_percentage_(20), |
| 173 pause_count_(0) { |
| 174 timer_.Start( |
| 175 base::TimeDelta::FromMilliseconds(100), |
| 176 this, |
| 177 &PowerLibraryStubImpl::Update); |
| 178 } |
| 179 |
168 virtual ~PowerLibraryStubImpl() {} | 180 virtual ~PowerLibraryStubImpl() {} |
169 | 181 |
170 // Begin PowerLibrary implementation. | 182 // Begin PowerLibrary implementation. |
171 virtual void Init() OVERRIDE {} | 183 virtual void Init() OVERRIDE {} |
172 virtual void AddObserver(Observer* observer) OVERRIDE {} | 184 virtual void AddObserver(Observer* observer) OVERRIDE { |
173 virtual void RemoveObserver(Observer* observer) OVERRIDE {} | 185 observers_.AddObserver(observer); |
174 virtual bool line_power_on() const OVERRIDE { return false; } | 186 } |
175 virtual bool battery_fully_charged() const OVERRIDE { return false; } | 187 |
176 virtual double battery_percentage() const OVERRIDE { return 50.0; } | 188 virtual void RemoveObserver(Observer* observer) OVERRIDE { |
177 virtual bool battery_is_present() const OVERRIDE { return true; } | 189 observers_.RemoveObserver(observer); |
| 190 } |
| 191 |
| 192 virtual bool line_power_on() const OVERRIDE { |
| 193 return !discharging_; |
| 194 } |
| 195 |
| 196 virtual bool battery_fully_charged() const OVERRIDE { |
| 197 return battery_percentage_ == 100; |
| 198 } |
| 199 |
| 200 virtual double battery_percentage() const OVERRIDE { |
| 201 return battery_percentage_; |
| 202 } |
| 203 |
| 204 virtual bool battery_is_present() const OVERRIDE { |
| 205 return true; |
| 206 } |
| 207 |
178 virtual base::TimeDelta battery_time_to_empty() const OVERRIDE { | 208 virtual base::TimeDelta battery_time_to_empty() const OVERRIDE { |
179 return base::TimeDelta::FromSeconds(10 * 60); | 209 if (battery_percentage_ == 0) |
| 210 return base::TimeDelta::FromSeconds(1); |
| 211 else |
| 212 return (base::TimeDelta::FromHours(3) * battery_percentage_) / 100; |
180 } | 213 } |
| 214 |
181 virtual base::TimeDelta battery_time_to_full() const OVERRIDE { | 215 virtual base::TimeDelta battery_time_to_full() const OVERRIDE { |
182 return base::TimeDelta::FromSeconds(0); | 216 if (battery_percentage_ == 100) |
| 217 return base::TimeDelta::FromSeconds(1); |
| 218 else |
| 219 return base::TimeDelta::FromHours(3) - battery_time_to_empty(); |
183 } | 220 } |
| 221 |
184 virtual void EnableScreenLock(bool enable) OVERRIDE {} | 222 virtual void EnableScreenLock(bool enable) OVERRIDE {} |
185 virtual void RequestRestart() OVERRIDE {} | 223 virtual void RequestRestart() OVERRIDE {} |
186 virtual void RequestShutdown() OVERRIDE {} | 224 virtual void RequestShutdown() OVERRIDE {} |
187 // End PowerLibrary implementation. | 225 // End PowerLibrary implementation. |
| 226 |
| 227 private: |
| 228 void Update() { |
| 229 // We pause at 0 and 100% so that it's easier to check those conditions. |
| 230 if (pause_count_ > 1) { |
| 231 pause_count_--; |
| 232 return; |
| 233 } |
| 234 |
| 235 if (battery_percentage_ == 0 || battery_percentage_ == 100) { |
| 236 if (pause_count_) { |
| 237 pause_count_ = 0; |
| 238 discharging_ = !discharging_; |
| 239 } else { |
| 240 pause_count_ = 20; |
| 241 return; |
| 242 } |
| 243 } |
| 244 battery_percentage_ += (discharging_ ? -1 : 1); |
| 245 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(this)); |
| 246 } |
| 247 |
| 248 bool discharging_; |
| 249 int battery_percentage_; |
| 250 int pause_count_; |
| 251 ObserverList<Observer> observers_; |
| 252 base::RepeatingTimer<PowerLibraryStubImpl> timer_; |
188 }; | 253 }; |
189 | 254 |
190 // static | 255 // static |
191 PowerLibrary* PowerLibrary::GetImpl(bool stub) { | 256 PowerLibrary* PowerLibrary::GetImpl(bool stub) { |
192 PowerLibrary* impl; | 257 PowerLibrary* impl; |
193 if (stub) | 258 if (stub) |
194 impl = new PowerLibraryStubImpl(); | 259 impl = new PowerLibraryStubImpl(); |
195 else | 260 else |
196 impl = new PowerLibraryImpl(); | 261 impl = new PowerLibraryImpl(); |
197 impl->Init(); | 262 impl->Init(); |
198 return impl; | 263 return impl; |
199 } | 264 } |
200 | 265 |
201 } // namespace chromeos | 266 } // namespace chromeos |
202 | 267 |
203 // Allows InvokeLater without adding refcounting. This class is a Singleton and | 268 // Allows InvokeLater without adding refcounting. This class is a Singleton and |
204 // won't be deleted until it's last InvokeLater is run. | 269 // won't be deleted until it's last InvokeLater is run. |
205 DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::PowerLibraryImpl); | 270 DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::PowerLibraryImpl); |
OLD | NEW |