| 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/dbus/power_manager_client.h" | 5 #include "chrome/browser/chromeos/dbus/power_manager_client.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 | 269 |
| 270 // PowerManagerClient override. | 270 // PowerManagerClient override. |
| 271 virtual void IncreaseScreenBrightness() OVERRIDE { | 271 virtual void IncreaseScreenBrightness() OVERRIDE { |
| 272 VLOG(1) << "Requested to increase screen brightness"; | 272 VLOG(1) << "Requested to increase screen brightness"; |
| 273 } | 273 } |
| 274 | 274 |
| 275 virtual void RequestStatusUpdate() OVERRIDE { | 275 virtual void RequestStatusUpdate() OVERRIDE { |
| 276 if (!timer_.IsRunning()) { | 276 if (!timer_.IsRunning()) { |
| 277 timer_.Start( | 277 timer_.Start( |
| 278 FROM_HERE, | 278 FROM_HERE, |
| 279 base::TimeDelta::FromMilliseconds(100), | 279 base::TimeDelta::FromMilliseconds(1000), |
| 280 this, | 280 this, |
| 281 &PowerManagerClientStubImpl::Update); | 281 &PowerManagerClientStubImpl::Update); |
| 282 } else { | 282 } else { |
| 283 timer_.Stop(); | 283 timer_.Stop(); |
| 284 } | 284 } |
| 285 } | 285 } |
| 286 | 286 |
| 287 virtual void RequestRestart() OVERRIDE {} | 287 virtual void RequestRestart() OVERRIDE {} |
| 288 | 288 |
| 289 virtual void RequestShutdown() OVERRIDE {} | 289 virtual void RequestShutdown() OVERRIDE {} |
| 290 | 290 |
| 291 private: | 291 private: |
| 292 void Update() { | 292 void Update() { |
| 293 // We pause at 0 and 100% so that it's easier to check those conditions. | 293 // We pause at 0 and 100% so that it's easier to check those conditions. |
| 294 if (pause_count_ > 1) { | 294 if (pause_count_ > 1) { |
| 295 pause_count_--; | 295 pause_count_--; |
| 296 return; | 296 return; |
| 297 } | 297 } |
| 298 | 298 |
| 299 if (battery_percentage_ == 0 || battery_percentage_ == 100) { | 299 if (battery_percentage_ == 0 || battery_percentage_ == 100) { |
| 300 if (pause_count_) { | 300 if (pause_count_) { |
| 301 pause_count_ = 0; | 301 pause_count_ = 0; |
| 302 discharging_ = !discharging_; | 302 discharging_ = !discharging_; |
| 303 } else { | 303 } else { |
| 304 pause_count_ = 20; | 304 // Pause twice (i.e. skip updating the menu), including the current |
| 305 // call to this function. |
| 306 pause_count_ = 2; |
| 305 return; | 307 return; |
| 306 } | 308 } |
| 307 } | 309 } |
| 308 battery_percentage_ += (discharging_ ? -1 : 1); | 310 battery_percentage_ += (discharging_ ? -1 : 1); |
| 309 | 311 |
| 312 const int kSecondsToEmptyFullBattery(3 * 60 * 60); // 3 hours. |
| 313 |
| 310 PowerSupplyStatus status; | 314 PowerSupplyStatus status; |
| 311 status.line_power_on = !discharging_; | 315 status.line_power_on = !discharging_; |
| 312 status.battery_is_present = true; | 316 status.battery_is_present = true; |
| 313 status.battery_percentage = battery_percentage_; | 317 status.battery_percentage = battery_percentage_; |
| 314 status.battery_seconds_to_empty = | 318 status.battery_seconds_to_empty = |
| 315 std::max(1, battery_percentage_ * 180 / 100); | 319 std::max(1, battery_percentage_ * kSecondsToEmptyFullBattery / 100); |
| 316 status.battery_seconds_to_full = | 320 status.battery_seconds_to_full = |
| 317 std::max(static_cast<int64>(1), 180 - status.battery_seconds_to_empty); | 321 std::max(static_cast<int64>(1), |
| 322 kSecondsToEmptyFullBattery - status.battery_seconds_to_empty); |
| 318 | 323 |
| 319 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(status)); | 324 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(status)); |
| 320 } | 325 } |
| 321 | 326 |
| 322 bool discharging_; | 327 bool discharging_; |
| 323 int battery_percentage_; | 328 int battery_percentage_; |
| 324 int pause_count_; | 329 int pause_count_; |
| 325 ObserverList<Observer> observers_; | 330 ObserverList<Observer> observers_; |
| 326 base::RepeatingTimer<PowerManagerClientStubImpl> timer_; | 331 base::RepeatingTimer<PowerManagerClientStubImpl> timer_; |
| 327 }; | 332 }; |
| 328 | 333 |
| 329 PowerManagerClient::PowerManagerClient() { | 334 PowerManagerClient::PowerManagerClient() { |
| 330 } | 335 } |
| 331 | 336 |
| 332 PowerManagerClient::~PowerManagerClient() { | 337 PowerManagerClient::~PowerManagerClient() { |
| 333 } | 338 } |
| 334 | 339 |
| 335 PowerManagerClient* PowerManagerClient::Create(dbus::Bus* bus) { | 340 PowerManagerClient* PowerManagerClient::Create(dbus::Bus* bus) { |
| 336 if (system::runtime_environment::IsRunningOnChromeOS()) { | 341 if (system::runtime_environment::IsRunningOnChromeOS()) { |
| 337 return new PowerManagerClientImpl(bus); | 342 return new PowerManagerClientImpl(bus); |
| 338 } else { | 343 } else { |
| 339 return new PowerManagerClientStubImpl(); | 344 return new PowerManagerClientStubImpl(); |
| 340 } | 345 } |
| 341 } | 346 } |
| 342 | 347 |
| 343 } // namespace chromeos | 348 } // namespace chromeos |
| OLD | NEW |