Chromium Code Reviews| 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 | |
| 7 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 8 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 9 #include "base/logging.h" | 11 #include "base/logging.h" |
| 10 #include "base/observer_list.h" | 12 #include "base/observer_list.h" |
| 11 #include "base/time.h" | 13 #include "base/time.h" |
| 12 #include "base/timer.h" | 14 #include "base/timer.h" |
| 13 #include "chrome/browser/chromeos/cros/cros_library.h" | 15 #include "chrome/browser/chromeos/cros/cros_library.h" |
| 14 #include "content/browser/browser_thread.h" | 16 #include "content/browser/browser_thread.h" |
| 15 #include "third_party/cros/chromeos_power.h" | 17 #include "third_party/cros/chromeos_power.h" |
| 16 #include "third_party/cros/chromeos_resume.h" | 18 #include "third_party/cros/chromeos_resume.h" |
| 17 | 19 |
| 18 namespace chromeos { | 20 namespace chromeos { |
| 19 | 21 |
| 20 class PowerLibraryImpl : public PowerLibrary { | 22 class PowerLibraryImpl : public PowerLibrary { |
| 21 public: | 23 public: |
| 22 PowerLibraryImpl() | 24 PowerLibraryImpl() |
| 23 : power_status_connection_(NULL), | 25 : power_status_connection_(NULL), |
| 24 resume_status_connection_(NULL), | 26 resume_status_connection_(NULL) { |
| 25 status_(chromeos::PowerStatus()) { | |
| 26 } | 27 } |
| 27 | 28 |
| 28 virtual ~PowerLibraryImpl() { | 29 virtual ~PowerLibraryImpl() { |
| 29 if (power_status_connection_) { | 30 if (power_status_connection_) { |
| 30 chromeos::DisconnectPowerStatus(power_status_connection_); | 31 chromeos::DisconnectPowerStatus(power_status_connection_); |
| 31 power_status_connection_ = NULL; | 32 power_status_connection_ = NULL; |
| 32 } | 33 } |
| 33 if (resume_status_connection_) { | 34 if (resume_status_connection_) { |
| 34 chromeos::DisconnectResume(resume_status_connection_); | 35 chromeos::DisconnectResume(resume_status_connection_); |
| 35 resume_status_connection_ = NULL; | 36 resume_status_connection_ = NULL; |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 46 } | 47 } |
| 47 | 48 |
| 48 virtual void AddObserver(Observer* observer) OVERRIDE { | 49 virtual void AddObserver(Observer* observer) OVERRIDE { |
| 49 observers_.AddObserver(observer); | 50 observers_.AddObserver(observer); |
| 50 } | 51 } |
| 51 | 52 |
| 52 virtual void RemoveObserver(Observer* observer) OVERRIDE { | 53 virtual void RemoveObserver(Observer* observer) OVERRIDE { |
| 53 observers_.RemoveObserver(observer); | 54 observers_.RemoveObserver(observer); |
| 54 } | 55 } |
| 55 | 56 |
| 56 virtual bool IsLinePowerOn() const OVERRIDE { | |
| 57 return status_.line_power_on; | |
| 58 } | |
| 59 | |
| 60 virtual bool IsBatteryFullyCharged() const OVERRIDE { | |
| 61 return status_.battery_state == chromeos::BATTERY_STATE_FULLY_CHARGED; | |
| 62 } | |
| 63 | |
| 64 virtual double GetBatteryPercentage() const OVERRIDE { | |
| 65 return status_.battery_percentage; | |
| 66 } | |
| 67 | |
| 68 virtual bool IsBatteryPresent() const OVERRIDE { | |
| 69 return status_.battery_is_present; | |
| 70 } | |
| 71 | |
| 72 virtual base::TimeDelta GetBatteryTimeToEmpty() const OVERRIDE { | |
| 73 return base::TimeDelta::FromSeconds(status_.battery_time_to_empty); | |
| 74 } | |
| 75 | |
| 76 virtual base::TimeDelta GetBatteryTimeToFull() const OVERRIDE { | |
| 77 return base::TimeDelta::FromSeconds(status_.battery_time_to_full); | |
| 78 } | |
| 79 | |
| 80 virtual void CalculateIdleTime(CalculateIdleTimeCallback* callback) OVERRIDE { | 57 virtual void CalculateIdleTime(CalculateIdleTimeCallback* callback) OVERRIDE { |
| 81 // TODO(sidor): Examine if it's really a good idea to use void* as a second | 58 // TODO(sidor): Examine if it's really a good idea to use void* as a second |
| 82 // argument. | 59 // argument. |
| 83 chromeos::GetIdleTime(&GetIdleTimeCallback, callback); | 60 chromeos::GetIdleTime(&GetIdleTimeCallback, callback); |
| 84 } | 61 } |
| 85 | 62 |
| 86 virtual void EnableScreenLock(bool enable) OVERRIDE { | 63 virtual void EnableScreenLock(bool enable) OVERRIDE { |
| 87 // Make sure we run on FILE thread becuase chromeos::EnableScreenLock | 64 // Make sure we run on FILE thread becuase chromeos::EnableScreenLock |
| 88 // would write power manager config file to disk. | 65 // would write power manager config file to disk. |
| 89 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) { | 66 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 122 if (success) { | 99 if (success) { |
| 123 notify->Run(time_idle_ms/1000); | 100 notify->Run(time_idle_ms/1000); |
| 124 } else { | 101 } else { |
| 125 LOG(ERROR) << "Power manager failed to calculate idle time."; | 102 LOG(ERROR) << "Power manager failed to calculate idle time."; |
| 126 notify->Run(-1); | 103 notify->Run(-1); |
| 127 } | 104 } |
| 128 delete notify; | 105 delete notify; |
| 129 } | 106 } |
| 130 | 107 |
| 131 static void PowerStatusChangedHandler(void* object, | 108 static void PowerStatusChangedHandler(void* object, |
| 132 const chromeos::PowerStatus& status) { | 109 const PowerStatus& status) { |
| 133 PowerLibraryImpl* power = static_cast<PowerLibraryImpl*>(object); | 110 PowerLibraryImpl* power = static_cast<PowerLibraryImpl*>(object); |
| 134 power->UpdatePowerStatus(status); | 111 power->UpdatePowerStatus(status); |
| 135 } | 112 } |
| 136 | 113 |
| 137 static void SystemResumedHandler(void* object) { | 114 static void SystemResumedHandler(void* object) { |
| 138 PowerLibraryImpl* power = static_cast<PowerLibraryImpl*>(object); | 115 PowerLibraryImpl* power = static_cast<PowerLibraryImpl*>(object); |
| 139 power->SystemResumed(); | 116 power->SystemResumed(); |
| 140 } | 117 } |
| 141 | 118 |
| 142 void UpdatePowerStatus(const chromeos::PowerStatus& status) { | 119 void UpdatePowerStatus(const PowerStatus& status) { |
| 143 // Make sure we run on UI thread. | 120 // Make sure we run on UI thread. |
| 144 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 121 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 145 BrowserThread::PostTask( | 122 BrowserThread::PostTask( |
| 146 BrowserThread::UI, FROM_HERE, | 123 BrowserThread::UI, FROM_HERE, |
| 147 NewRunnableMethod( | 124 NewRunnableMethod( |
| 148 this, &PowerLibraryImpl::UpdatePowerStatus, status)); | 125 this, &PowerLibraryImpl::UpdatePowerStatus, status)); |
| 149 return; | 126 return; |
| 150 } | 127 } |
| 151 | 128 |
| 152 DVLOG(1) << "Power lpo=" << status.line_power_on | 129 DVLOG(1) << "Power lpo=" << status.line_power_on |
| 153 << " sta=" << status.battery_state | 130 << " sta=" << status.battery_state |
| 154 << " per=" << status.battery_percentage | 131 << " per=" << status.battery_percentage |
| 155 << " tte=" << status.battery_time_to_empty | 132 << " tte=" << status.battery_time_to_empty |
| 156 << " ttf=" << status.battery_time_to_full; | 133 << " ttf=" << status.battery_time_to_full; |
| 157 status_ = status; | 134 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(status)); |
| 158 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(this)); | |
| 159 } | 135 } |
| 160 | 136 |
| 161 void SystemResumed() { | 137 void SystemResumed() { |
| 162 // Make sure we run on the UI thread. | 138 // Make sure we run on the UI thread. |
| 163 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 139 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 164 BrowserThread::PostTask( | 140 BrowserThread::PostTask( |
| 165 BrowserThread::UI, FROM_HERE, | 141 BrowserThread::UI, FROM_HERE, |
| 166 NewRunnableMethod(this, &PowerLibraryImpl::SystemResumed)); | 142 NewRunnableMethod(this, &PowerLibraryImpl::SystemResumed)); |
| 167 return; | 143 return; |
| 168 } | 144 } |
| 169 | 145 |
| 170 FOR_EACH_OBSERVER(Observer, observers_, SystemResumed()); | 146 FOR_EACH_OBSERVER(Observer, observers_, SystemResumed()); |
| 171 } | 147 } |
| 172 | 148 |
| 173 ObserverList<Observer> observers_; | 149 ObserverList<Observer> observers_; |
| 174 | 150 |
| 175 // A reference to the power battery API, to allow callbacks when the battery | 151 // A reference to the power battery API, to allow callbacks when the battery |
| 176 // status changes. | 152 // status changes. |
| 177 chromeos::PowerStatusConnection power_status_connection_; | 153 chromeos::PowerStatusConnection power_status_connection_; |
| 178 | 154 |
| 179 // A reference to the resume alerts. | 155 // A reference to the resume alerts. |
| 180 chromeos::ResumeConnection resume_status_connection_; | 156 chromeos::ResumeConnection resume_status_connection_; |
| 181 | 157 |
| 182 // The latest power status. | |
| 183 chromeos::PowerStatus status_; | |
| 184 | |
| 185 DISALLOW_COPY_AND_ASSIGN(PowerLibraryImpl); | 158 DISALLOW_COPY_AND_ASSIGN(PowerLibraryImpl); |
| 186 }; | 159 }; |
| 187 | 160 |
| 188 // The stub implementation runs the battery up and down, pausing at the | 161 // The stub implementation runs the battery up and down, pausing at the |
| 189 // fully charged and fully depleted states. | 162 // fully charged and fully depleted states. |
| 190 class PowerLibraryStubImpl : public PowerLibrary { | 163 class PowerLibraryStubImpl : public PowerLibrary { |
| 191 public: | 164 public: |
| 192 PowerLibraryStubImpl() | 165 PowerLibraryStubImpl() |
| 193 : discharging_(true), | 166 : discharging_(true), |
| 194 battery_percentage_(80), | 167 battery_percentage_(80), |
| 195 pause_count_(0) { | 168 pause_count_(0) { |
| 196 } | 169 } |
| 197 | 170 |
| 198 virtual ~PowerLibraryStubImpl() {} | 171 virtual ~PowerLibraryStubImpl() {} |
| 199 | 172 |
| 200 // Begin PowerLibrary implementation. | 173 // Begin PowerLibrary implementation. |
| 201 virtual void Init() OVERRIDE {} | 174 virtual void Init() OVERRIDE {} |
| 202 virtual void AddObserver(Observer* observer) OVERRIDE { | 175 virtual void AddObserver(Observer* observer) OVERRIDE { |
| 203 observers_.AddObserver(observer); | 176 observers_.AddObserver(observer); |
| 204 } | 177 } |
| 205 | 178 |
| 206 virtual void RemoveObserver(Observer* observer) OVERRIDE { | 179 virtual void RemoveObserver(Observer* observer) OVERRIDE { |
| 207 observers_.RemoveObserver(observer); | 180 observers_.RemoveObserver(observer); |
| 208 } | 181 } |
| 209 | 182 |
| 210 virtual bool IsLinePowerOn() const OVERRIDE { | |
| 211 return !discharging_; | |
| 212 } | |
| 213 | |
| 214 virtual bool IsBatteryFullyCharged() const OVERRIDE { | |
| 215 return battery_percentage_ == 100; | |
| 216 } | |
| 217 | |
| 218 virtual double GetBatteryPercentage() const OVERRIDE { | |
| 219 return battery_percentage_; | |
| 220 } | |
| 221 | |
| 222 virtual bool IsBatteryPresent() const OVERRIDE { | |
| 223 return true; | |
| 224 } | |
| 225 | |
| 226 virtual base::TimeDelta GetBatteryTimeToEmpty() const OVERRIDE { | |
| 227 if (battery_percentage_ == 0) | |
| 228 return base::TimeDelta::FromSeconds(1); | |
| 229 else | |
| 230 return (base::TimeDelta::FromHours(3) * battery_percentage_) / 100; | |
| 231 } | |
| 232 | |
| 233 virtual base::TimeDelta GetBatteryTimeToFull() const OVERRIDE { | |
| 234 if (battery_percentage_ == 100) | |
| 235 return base::TimeDelta::FromSeconds(1); | |
| 236 else | |
| 237 return base::TimeDelta::FromHours(3) - GetBatteryTimeToEmpty(); | |
| 238 } | |
| 239 | |
| 240 virtual void CalculateIdleTime(CalculateIdleTimeCallback* callback) OVERRIDE { | 183 virtual void CalculateIdleTime(CalculateIdleTimeCallback* callback) OVERRIDE { |
| 241 callback->Run(0); | 184 callback->Run(0); |
| 242 delete callback; | 185 delete callback; |
| 243 } | 186 } |
| 244 | 187 |
| 245 virtual void EnableScreenLock(bool enable) OVERRIDE {} | 188 virtual void EnableScreenLock(bool enable) OVERRIDE {} |
| 246 | 189 |
| 247 virtual void RequestRestart() OVERRIDE {} | 190 virtual void RequestRestart() OVERRIDE {} |
| 248 | 191 |
| 249 virtual void RequestShutdown() OVERRIDE {} | 192 virtual void RequestShutdown() OVERRIDE {} |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 273 if (battery_percentage_ == 0 || battery_percentage_ == 100) { | 216 if (battery_percentage_ == 0 || battery_percentage_ == 100) { |
| 274 if (pause_count_) { | 217 if (pause_count_) { |
| 275 pause_count_ = 0; | 218 pause_count_ = 0; |
| 276 discharging_ = !discharging_; | 219 discharging_ = !discharging_; |
| 277 } else { | 220 } else { |
| 278 pause_count_ = 20; | 221 pause_count_ = 20; |
| 279 return; | 222 return; |
| 280 } | 223 } |
| 281 } | 224 } |
| 282 battery_percentage_ += (discharging_ ? -1 : 1); | 225 battery_percentage_ += (discharging_ ? -1 : 1); |
| 283 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(this)); | 226 |
| 227 PowerStatus status; | |
| 228 status.line_power_on = !discharging_; | |
| 229 status.battery_is_present = true; | |
| 230 status.battery_percentage = battery_percentage_; | |
| 231 status.battery_time_to_empty = std::max(1, battery_percentage_ * 180 / 100); | |
| 232 status.battery_time_to_full = std::max(1LL, | |
| 233 180 - status.battery_time_to_empty); | |
|
stevenjb
2011/10/18 23:08:23
It seems like PowerStatus should just store percen
satorux1
2011/10/19 21:16:55
This is a stub implementation.
PowerStatus data c
| |
| 234 | |
| 235 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(status)); | |
| 284 } | 236 } |
| 285 | 237 |
| 286 bool discharging_; | 238 bool discharging_; |
| 287 int battery_percentage_; | 239 int battery_percentage_; |
| 288 int pause_count_; | 240 int pause_count_; |
| 289 ObserverList<Observer> observers_; | 241 ObserverList<Observer> observers_; |
| 290 base::RepeatingTimer<PowerLibraryStubImpl> timer_; | 242 base::RepeatingTimer<PowerLibraryStubImpl> timer_; |
| 291 }; | 243 }; |
| 292 | 244 |
| 293 // static | 245 // static |
| 294 PowerLibrary* PowerLibrary::GetImpl(bool stub) { | 246 PowerLibrary* PowerLibrary::GetImpl(bool stub) { |
| 295 PowerLibrary* impl; | 247 PowerLibrary* impl; |
| 296 if (stub) | 248 if (stub) |
| 297 impl = new PowerLibraryStubImpl(); | 249 impl = new PowerLibraryStubImpl(); |
| 298 else | 250 else |
| 299 impl = new PowerLibraryImpl(); | 251 impl = new PowerLibraryImpl(); |
| 300 impl->Init(); | 252 impl->Init(); |
| 301 return impl; | 253 return impl; |
| 302 } | 254 } |
| 303 | 255 |
| 304 } // namespace chromeos | 256 } // namespace chromeos |
| 305 | 257 |
| 306 // Allows InvokeLater without adding refcounting. This class is a Singleton and | 258 // Allows InvokeLater without adding refcounting. This class is a Singleton and |
| 307 // won't be deleted until it's last InvokeLater is run. | 259 // won't be deleted until it's last InvokeLater is run. |
| 308 DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::PowerLibraryImpl); | 260 DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::PowerLibraryImpl); |
| OLD | NEW |