| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "chrome/browser/chrome_thread.h" | 9 #include "chrome/browser/chrome_thread.h" |
| 10 #include "chrome/browser/chromeos/cros/cros_library.h" | 10 #include "chrome/browser/chromeos/cros/cros_library.h" |
| 11 | 11 |
| 12 namespace chromeos { |
| 13 |
| 14 class PowerLibraryImpl : public PowerLibrary { |
| 15 public: |
| 16 PowerLibraryImpl() |
| 17 : power_status_connection_(NULL), |
| 18 status_(chromeos::PowerStatus()) { |
| 19 if (CrosLibrary::Get()->EnsureLoaded()) { |
| 20 Init(); |
| 21 } |
| 22 } |
| 23 |
| 24 ~PowerLibraryImpl() { |
| 25 if (power_status_connection_) { |
| 26 chromeos::DisconnectPowerStatus(power_status_connection_); |
| 27 } |
| 28 } |
| 29 |
| 30 void AddObserver(Observer* observer) { |
| 31 observers_.AddObserver(observer); |
| 32 } |
| 33 |
| 34 void RemoveObserver(Observer* observer) { |
| 35 observers_.RemoveObserver(observer); |
| 36 } |
| 37 |
| 38 bool line_power_on() const { |
| 39 return status_.line_power_on; |
| 40 } |
| 41 |
| 42 bool battery_is_present() const { |
| 43 return status_.battery_is_present; |
| 44 } |
| 45 |
| 46 bool battery_fully_charged() const { |
| 47 return status_.battery_state == chromeos::BATTERY_STATE_FULLY_CHARGED; |
| 48 } |
| 49 |
| 50 double battery_percentage() const { |
| 51 return status_.battery_percentage; |
| 52 } |
| 53 |
| 54 base::TimeDelta battery_time_to_empty() const { |
| 55 return base::TimeDelta::FromSeconds(status_.battery_time_to_empty); |
| 56 } |
| 57 |
| 58 base::TimeDelta battery_time_to_full() const { |
| 59 return base::TimeDelta::FromSeconds(status_.battery_time_to_full); |
| 60 } |
| 61 |
| 62 private: |
| 63 static void PowerStatusChangedHandler(void* object, |
| 64 const chromeos::PowerStatus& status) { |
| 65 PowerLibraryImpl* power = static_cast<PowerLibraryImpl*>(object); |
| 66 power->UpdatePowerStatus(status); |
| 67 } |
| 68 |
| 69 void Init() { |
| 70 power_status_connection_ = chromeos::MonitorPowerStatus( |
| 71 &PowerStatusChangedHandler, this); |
| 72 } |
| 73 |
| 74 void UpdatePowerStatus(const chromeos::PowerStatus& status) { |
| 75 // Make sure we run on UI thread. |
| 76 if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) { |
| 77 ChromeThread::PostTask( |
| 78 ChromeThread::UI, FROM_HERE, |
| 79 NewRunnableMethod( |
| 80 this, &PowerLibraryImpl::UpdatePowerStatus, status)); |
| 81 return; |
| 82 } |
| 83 |
| 84 DLOG(INFO) << "Power" << |
| 85 " lpo=" << status.line_power_on << |
| 86 " sta=" << status.battery_state << |
| 87 " per=" << status.battery_percentage << |
| 88 " tte=" << status.battery_time_to_empty << |
| 89 " ttf=" << status.battery_time_to_full; |
| 90 status_ = status; |
| 91 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(this)); |
| 92 } |
| 93 |
| 94 ObserverList<Observer> observers_; |
| 95 |
| 96 // A reference to the battery power api, to allow callbacks when the battery |
| 97 // status changes. |
| 98 chromeos::PowerStatusConnection power_status_connection_; |
| 99 |
| 100 // The latest power status. |
| 101 chromeos::PowerStatus status_; |
| 102 |
| 103 DISALLOW_COPY_AND_ASSIGN(PowerLibraryImpl); |
| 104 }; |
| 105 |
| 106 class PowerLibraryStubImpl : public PowerLibrary { |
| 107 public: |
| 108 PowerLibraryStubImpl() {} |
| 109 ~PowerLibraryStubImpl() {} |
| 110 void AddObserver(Observer* observer) {} |
| 111 void RemoveObserver(Observer* observer) {} |
| 112 bool line_power_on() const { return false; } |
| 113 bool battery_is_present() const { return false; } |
| 114 bool battery_fully_charged() const { return false; } |
| 115 double battery_percentage() const { return false; } |
| 116 base::TimeDelta battery_time_to_empty() const { |
| 117 return base::TimeDelta::FromSeconds(0); |
| 118 } |
| 119 base::TimeDelta battery_time_to_full() const { |
| 120 return base::TimeDelta::FromSeconds(0); |
| 121 } |
| 122 }; |
| 123 |
| 124 // static |
| 125 PowerLibrary* PowerLibrary::GetImpl(bool stub) { |
| 126 if (stub) |
| 127 return new PowerLibraryStubImpl(); |
| 128 else |
| 129 return new PowerLibraryImpl(); |
| 130 } |
| 131 |
| 132 } // namespace chromeos |
| 133 |
| 12 // Allows InvokeLater without adding refcounting. This class is a Singleton and | 134 // Allows InvokeLater without adding refcounting. This class is a Singleton and |
| 13 // won't be deleted until it's last InvokeLater is run. | 135 // won't be deleted until it's last InvokeLater is run. |
| 14 DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::PowerLibraryImpl); | 136 DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::PowerLibraryImpl); |
| 15 | 137 |
| 16 namespace chromeos { | |
| 17 | |
| 18 PowerLibraryImpl::PowerLibraryImpl() | |
| 19 : power_status_connection_(NULL), | |
| 20 status_(chromeos::PowerStatus()) { | |
| 21 if (CrosLibrary::Get()->EnsureLoaded()) { | |
| 22 Init(); | |
| 23 } | |
| 24 } | |
| 25 | |
| 26 PowerLibraryImpl::~PowerLibraryImpl() { | |
| 27 if (power_status_connection_) { | |
| 28 chromeos::DisconnectPowerStatus(power_status_connection_); | |
| 29 } | |
| 30 } | |
| 31 | |
| 32 void PowerLibraryImpl::AddObserver(Observer* observer) { | |
| 33 observers_.AddObserver(observer); | |
| 34 } | |
| 35 | |
| 36 void PowerLibraryImpl::RemoveObserver(Observer* observer) { | |
| 37 observers_.RemoveObserver(observer); | |
| 38 } | |
| 39 | |
| 40 bool PowerLibraryImpl::line_power_on() const { | |
| 41 return status_.line_power_on; | |
| 42 } | |
| 43 | |
| 44 bool PowerLibraryImpl::battery_is_present() const { | |
| 45 return status_.battery_is_present; | |
| 46 } | |
| 47 | |
| 48 bool PowerLibraryImpl::battery_fully_charged() const { | |
| 49 return status_.battery_state == chromeos::BATTERY_STATE_FULLY_CHARGED; | |
| 50 } | |
| 51 | |
| 52 double PowerLibraryImpl::battery_percentage() const { | |
| 53 return status_.battery_percentage; | |
| 54 } | |
| 55 | |
| 56 base::TimeDelta PowerLibraryImpl::battery_time_to_empty() const { | |
| 57 return base::TimeDelta::FromSeconds(status_.battery_time_to_empty); | |
| 58 } | |
| 59 | |
| 60 base::TimeDelta PowerLibraryImpl::battery_time_to_full() const { | |
| 61 return base::TimeDelta::FromSeconds(status_.battery_time_to_full); | |
| 62 } | |
| 63 | |
| 64 // static | |
| 65 void PowerLibraryImpl::PowerStatusChangedHandler(void* object, | |
| 66 const chromeos::PowerStatus& status) { | |
| 67 PowerLibraryImpl* power = static_cast<PowerLibraryImpl*>(object); | |
| 68 power->UpdatePowerStatus(status); | |
| 69 } | |
| 70 | |
| 71 void PowerLibraryImpl::Init() { | |
| 72 power_status_connection_ = chromeos::MonitorPowerStatus( | |
| 73 &PowerStatusChangedHandler, this); | |
| 74 } | |
| 75 | |
| 76 void PowerLibraryImpl::UpdatePowerStatus(const chromeos::PowerStatus& status) { | |
| 77 // Make sure we run on UI thread. | |
| 78 if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) { | |
| 79 ChromeThread::PostTask( | |
| 80 ChromeThread::UI, FROM_HERE, | |
| 81 NewRunnableMethod(this, &PowerLibraryImpl::UpdatePowerStatus, status)); | |
| 82 return; | |
| 83 } | |
| 84 | |
| 85 DLOG(INFO) << "Power" << | |
| 86 " lpo=" << status.line_power_on << | |
| 87 " sta=" << status.battery_state << | |
| 88 " per=" << status.battery_percentage << | |
| 89 " tte=" << status.battery_time_to_empty << | |
| 90 " ttf=" << status.battery_time_to_full; | |
| 91 status_ = status; | |
| 92 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(this)); | |
| 93 } | |
| 94 | |
| 95 } // namespace chromeos | |
| OLD | NEW |