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> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/observer_list.h" | 13 #include "base/observer_list.h" |
| 14 #include "base/time.h" | |
| 15 #include "base/timer.h" | |
| 16 #include "chrome/browser/chromeos/cros/cros_library.h" | 14 #include "chrome/browser/chromeos/cros/cros_library.h" |
| 17 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 18 #include "third_party/cros/chromeos_power.h" | 16 #include "third_party/cros/chromeos_power.h" |
| 19 #include "third_party/cros/chromeos_resume.h" | 17 #include "third_party/cros/chromeos_resume.h" |
| 20 | 18 |
| 21 using content::BrowserThread; | 19 using content::BrowserThread; |
| 22 | 20 |
| 23 namespace chromeos { | 21 namespace chromeos { |
| 24 | 22 |
| 25 class PowerLibraryImpl : public PowerLibrary { | 23 class PowerLibraryImpl : public PowerLibrary { |
| 26 public: | 24 public: |
| 27 PowerLibraryImpl() | 25 PowerLibraryImpl() |
| 28 : power_status_connection_(NULL), | 26 : resume_status_connection_(NULL) { |
| 29 resume_status_connection_(NULL) { | |
| 30 } | 27 } |
| 31 | 28 |
| 32 virtual ~PowerLibraryImpl() { | 29 virtual ~PowerLibraryImpl() { |
| 33 if (power_status_connection_) { | |
| 34 chromeos::DisconnectPowerStatus(power_status_connection_); | |
| 35 power_status_connection_ = NULL; | |
| 36 } | |
| 37 if (resume_status_connection_) { | 30 if (resume_status_connection_) { |
| 38 chromeos::DisconnectResume(resume_status_connection_); | 31 chromeos::DisconnectResume(resume_status_connection_); |
| 39 resume_status_connection_ = NULL; | 32 resume_status_connection_ = NULL; |
| 40 } | 33 } |
| 41 } | 34 } |
| 42 | 35 |
| 43 // Begin PowerLibrary implementation. | 36 // Begin PowerLibrary implementation. |
| 44 virtual void Init() OVERRIDE { | 37 virtual void Init() OVERRIDE { |
| 45 DCHECK(CrosLibrary::Get()->libcros_loaded()); | 38 DCHECK(CrosLibrary::Get()->libcros_loaded()); |
| 46 power_status_connection_ = | |
| 47 chromeos::MonitorPowerStatus(&PowerStatusChangedHandler, this); | |
| 48 resume_status_connection_ = | 39 resume_status_connection_ = |
| 49 chromeos::MonitorResume(&SystemResumedHandler, this); | 40 chromeos::MonitorResume(&SystemResumedHandler, this); |
| 50 } | 41 } |
| 51 | 42 |
| 52 virtual void AddObserver(Observer* observer) OVERRIDE { | 43 virtual void AddObserver(Observer* observer) OVERRIDE { |
| 53 observers_.AddObserver(observer); | 44 observers_.AddObserver(observer); |
| 54 } | 45 } |
| 55 | 46 |
| 56 virtual void RemoveObserver(Observer* observer) OVERRIDE { | 47 virtual void RemoveObserver(Observer* observer) OVERRIDE { |
| 57 observers_.RemoveObserver(observer); | 48 observers_.RemoveObserver(observer); |
| 58 } | 49 } |
| 59 | 50 |
| 60 virtual void CalculateIdleTime(CalculateIdleTimeCallback* callback) OVERRIDE { | 51 virtual void CalculateIdleTime(CalculateIdleTimeCallback* callback) OVERRIDE { |
| 61 // TODO(sidor): Examine if it's really a good idea to use void* as a second | 52 // TODO(sidor): Examine if it's really a good idea to use void* as a second |
| 62 // argument. | 53 // argument. |
| 63 chromeos::GetIdleTime(&GetIdleTimeCallback, callback); | 54 chromeos::GetIdleTime(&GetIdleTimeCallback, callback); |
| 64 } | 55 } |
| 65 | 56 |
| 66 virtual void EnableScreenLock(bool enable) OVERRIDE { | 57 virtual void EnableScreenLock(bool enable) OVERRIDE { |
| 67 // Called when the screen preference is changed, which should always | 58 // Make sure we run on FILE thread because chromeos::EnableScreenLock |
| 68 // run on UI thread. | |
| 69 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 70 // Post the task to FILE thread as chromeos::EnableScreenLock | |
| 71 // would write power manager config file to disk. | 59 // would write power manager config file to disk. |
| 72 BrowserThread::PostTask( | 60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 73 BrowserThread::FILE, FROM_HERE, | 61 |
| 74 base::Bind(&PowerLibraryImpl::DoEnableScreenLock, enable)); | 62 chromeos::EnableScreenLock(enable); |
|
satorux1
2011/11/03 23:38:47
Why this function is changed? The change does not
Simon Que
2011/11/04 22:35:31
Done.
| |
| 75 } | 63 } |
| 76 | 64 |
| 77 virtual void RequestRestart() OVERRIDE { | 65 virtual void RequestRestart() OVERRIDE { |
| 78 chromeos::RequestRestart(); | 66 chromeos::RequestRestart(); |
| 79 } | 67 } |
| 80 | 68 |
| 81 virtual void RequestShutdown() OVERRIDE { | 69 virtual void RequestShutdown() OVERRIDE { |
| 82 chromeos::RequestShutdown(); | 70 chromeos::RequestShutdown(); |
| 83 } | 71 } |
| 84 | 72 |
| 85 virtual void RequestStatusUpdate() OVERRIDE { | 73 virtual void RequestStatusUpdate() OVERRIDE { |
|
satorux1
2011/11/03 23:38:47
Can you remove this? You now have the same name fu
Simon Que
2011/11/04 22:35:31
Done.
| |
| 86 // TODO(stevenjb): chromeos::RetrievePowerInformation has been deprecated; | 74 // TODO(stevenjb): chromeos::RetrievePowerInformation has been deprecated; |
| 87 // we should add a mechanism to immediately request an update, probably | 75 // we should add a mechanism to immediately request an update, probably |
| 88 // when we migrate the DBus code from libcros to here. | 76 // when we migrate the DBus code from libcros to here. |
|
satorux1
2011/11/03 23:38:47
Please move the comments to power_manager_client.c
Simon Que
2011/11/04 22:35:31
Done.
| |
| 89 } | 77 } |
| 90 | |
| 91 // End PowerLibrary implementation. | 78 // End PowerLibrary implementation. |
| 92 | 79 |
| 93 private: | 80 private: |
| 94 static void DoEnableScreenLock(bool enable) { | 81 static void DoEnableScreenLock(bool enable) { |
| 95 chromeos::EnableScreenLock(enable); | 82 chromeos::EnableScreenLock(enable); |
| 96 } | 83 } |
| 97 | 84 |
| 98 static void GetIdleTimeCallback(void* object, | 85 static void GetIdleTimeCallback(void* object, |
| 99 int64_t time_idle_ms, | 86 int64_t time_idle_ms, |
| 100 bool success) { | 87 bool success) { |
| 101 DCHECK(object); | 88 DCHECK(object); |
| 102 CalculateIdleTimeCallback* notify = | 89 CalculateIdleTimeCallback* notify = |
| 103 static_cast<CalculateIdleTimeCallback*>(object); | 90 static_cast<CalculateIdleTimeCallback*>(object); |
| 104 if (success) { | 91 if (success) { |
| 105 notify->Run(time_idle_ms/1000); | 92 notify->Run(time_idle_ms/1000); |
| 106 } else { | 93 } else { |
| 107 LOG(ERROR) << "Power manager failed to calculate idle time."; | 94 LOG(ERROR) << "Power manager failed to calculate idle time."; |
| 108 notify->Run(-1); | 95 notify->Run(-1); |
| 109 } | 96 } |
| 110 delete notify; | 97 delete notify; |
| 111 } | 98 } |
| 112 | 99 |
| 113 static void PowerStatusChangedHandler( | |
| 114 void* object, const chromeos::PowerStatus& power_status) { | |
| 115 // TODO(sque): this is a temporary copy-over from libcros. Soon libcros | |
| 116 // will be removed and this will not be necessary. | |
| 117 PowerSupplyStatus status; | |
| 118 status.line_power_on = power_status.line_power_on; | |
| 119 status.battery_is_present = power_status.battery_is_present; | |
| 120 status.battery_is_full = | |
| 121 (power_status.battery_state == chromeos::BATTERY_STATE_FULLY_CHARGED); | |
| 122 status.battery_seconds_to_empty = power_status.battery_time_to_empty; | |
| 123 status.battery_seconds_to_full = power_status.battery_time_to_full; | |
| 124 status.battery_percentage = power_status.battery_percentage; | |
| 125 | |
| 126 PowerLibraryImpl* power = static_cast<PowerLibraryImpl*>(object); | |
| 127 power->UpdatePowerStatus(status); | |
| 128 } | |
| 129 | |
| 130 static void SystemResumedHandler(void* object) { | 100 static void SystemResumedHandler(void* object) { |
| 131 PowerLibraryImpl* power = static_cast<PowerLibraryImpl*>(object); | 101 PowerLibraryImpl* power = static_cast<PowerLibraryImpl*>(object); |
| 132 power->SystemResumed(); | 102 power->SystemResumed(); |
| 133 } | 103 } |
| 134 | 104 |
| 135 void UpdatePowerStatus(const PowerSupplyStatus& status) { | |
| 136 // Called from PowerStatusChangedHandler, a libcros callback which | |
| 137 // should always run on UI thread. | |
| 138 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 139 | |
| 140 DVLOG(1) << "Power line_power_on = " << status.line_power_on | |
| 141 << " percentage = " << status.battery_percentage | |
| 142 << " seconds_to_empty = " << status.battery_seconds_to_empty | |
| 143 << " seconds_to_full = " << status.battery_seconds_to_full; | |
| 144 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(status)); | |
| 145 } | |
| 146 | |
| 147 void SystemResumed() { | 105 void SystemResumed() { |
| 148 // Called from SystemResumedHandler, a libcros callback which should | 106 // Called from SystemResumedHandler, a libcros callback which should |
| 149 // always run on UI thread. | 107 // always run on UI thread. |
| 150 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 109 | |
|
satorux1
2011/11/03 23:38:47
Let's not add a blank line here.
Simon Que
2011/11/04 22:35:31
Done.
| |
| 151 FOR_EACH_OBSERVER(Observer, observers_, SystemResumed()); | 110 FOR_EACH_OBSERVER(Observer, observers_, SystemResumed()); |
| 152 } | 111 } |
| 153 | 112 |
| 154 ObserverList<Observer> observers_; | 113 ObserverList<Observer> observers_; |
| 155 | 114 |
| 156 // A reference to the power battery API, to allow callbacks when the battery | |
| 157 // status changes. | |
| 158 chromeos::PowerStatusConnection power_status_connection_; | |
| 159 | |
| 160 // A reference to the resume alerts. | 115 // A reference to the resume alerts. |
| 161 chromeos::ResumeConnection resume_status_connection_; | 116 chromeos::ResumeConnection resume_status_connection_; |
| 162 | 117 |
| 163 DISALLOW_COPY_AND_ASSIGN(PowerLibraryImpl); | 118 DISALLOW_COPY_AND_ASSIGN(PowerLibraryImpl); |
| 164 }; | 119 }; |
| 165 | 120 |
| 166 // The stub implementation runs the battery up and down, pausing at the | 121 // The stub implementation runs the battery up and down, pausing at the |
| 167 // fully charged and fully depleted states. | 122 // fully charged and fully depleted states. |
| 168 class PowerLibraryStubImpl : public PowerLibrary { | 123 class PowerLibraryStubImpl : public PowerLibrary { |
| 169 public: | 124 public: |
| 170 PowerLibraryStubImpl() | 125 PowerLibraryStubImpl() { |
| 171 : discharging_(true), | |
| 172 battery_percentage_(80), | |
| 173 pause_count_(0) { | |
| 174 } | 126 } |
| 175 | 127 |
| 176 virtual ~PowerLibraryStubImpl() {} | 128 virtual ~PowerLibraryStubImpl() {} |
| 177 | 129 |
| 178 // Begin PowerLibrary implementation. | 130 // Begin PowerLibrary implementation. |
| 179 virtual void Init() OVERRIDE {} | 131 virtual void Init() OVERRIDE {} |
| 180 virtual void AddObserver(Observer* observer) OVERRIDE { | 132 virtual void AddObserver(Observer* observer) OVERRIDE { |
| 181 observers_.AddObserver(observer); | 133 observers_.AddObserver(observer); |
| 182 } | 134 } |
| 183 | 135 |
| 184 virtual void RemoveObserver(Observer* observer) OVERRIDE { | 136 virtual void RemoveObserver(Observer* observer) OVERRIDE { |
| 185 observers_.RemoveObserver(observer); | 137 observers_.RemoveObserver(observer); |
| 186 } | 138 } |
| 187 | 139 |
| 188 virtual void CalculateIdleTime(CalculateIdleTimeCallback* callback) OVERRIDE { | 140 virtual void CalculateIdleTime(CalculateIdleTimeCallback* callback) OVERRIDE { |
| 189 callback->Run(0); | 141 callback->Run(0); |
| 190 delete callback; | 142 delete callback; |
| 191 } | 143 } |
| 192 | 144 |
| 193 virtual void EnableScreenLock(bool enable) OVERRIDE {} | 145 virtual void EnableScreenLock(bool enable) OVERRIDE {} |
| 194 | 146 |
| 195 virtual void RequestRestart() OVERRIDE {} | 147 virtual void RequestRestart() OVERRIDE {} |
| 196 | 148 |
| 197 virtual void RequestShutdown() OVERRIDE {} | 149 virtual void RequestShutdown() OVERRIDE {} |
| 198 | 150 |
| 199 virtual void RequestStatusUpdate() OVERRIDE { | |
| 200 if (!timer_.IsRunning()) { | |
| 201 timer_.Start( | |
| 202 FROM_HERE, | |
| 203 base::TimeDelta::FromMilliseconds(100), | |
| 204 this, | |
| 205 &PowerLibraryStubImpl::Update); | |
| 206 } else { | |
| 207 timer_.Stop(); | |
| 208 } | |
| 209 } | |
| 210 | |
| 211 // End PowerLibrary implementation. | 151 // End PowerLibrary implementation. |
| 212 | |
| 213 private: | 152 private: |
| 214 void Update() { | |
| 215 // We pause at 0 and 100% so that it's easier to check those conditions. | |
| 216 if (pause_count_ > 1) { | |
| 217 pause_count_--; | |
| 218 return; | |
| 219 } | |
| 220 | |
| 221 if (battery_percentage_ == 0 || battery_percentage_ == 100) { | |
| 222 if (pause_count_) { | |
| 223 pause_count_ = 0; | |
| 224 discharging_ = !discharging_; | |
| 225 } else { | |
| 226 pause_count_ = 20; | |
| 227 return; | |
| 228 } | |
| 229 } | |
| 230 battery_percentage_ += (discharging_ ? -1 : 1); | |
| 231 | |
| 232 PowerSupplyStatus status; | |
| 233 status.line_power_on = !discharging_; | |
| 234 status.battery_is_present = true; | |
| 235 status.battery_percentage = battery_percentage_; | |
| 236 status.battery_seconds_to_empty = | |
| 237 std::max(1, battery_percentage_ * 180 / 100); | |
| 238 status.battery_seconds_to_full = | |
| 239 std::max(static_cast<int64>(1), 180 - status.battery_seconds_to_empty); | |
| 240 | |
| 241 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(status)); | |
| 242 } | |
| 243 | |
| 244 bool discharging_; | |
| 245 int battery_percentage_; | |
| 246 int pause_count_; | |
| 247 ObserverList<Observer> observers_; | 153 ObserverList<Observer> observers_; |
| 248 base::RepeatingTimer<PowerLibraryStubImpl> timer_; | |
| 249 }; | 154 }; |
| 250 | 155 |
| 251 // static | 156 // static |
| 252 PowerLibrary* PowerLibrary::GetImpl(bool stub) { | 157 PowerLibrary* PowerLibrary::GetImpl(bool stub) { |
| 253 PowerLibrary* impl; | 158 PowerLibrary* impl; |
| 254 if (stub) | 159 if (stub) |
| 255 impl = new PowerLibraryStubImpl(); | 160 impl = new PowerLibraryStubImpl(); |
| 256 else | 161 else |
| 257 impl = new PowerLibraryImpl(); | 162 impl = new PowerLibraryImpl(); |
| 258 impl->Init(); | 163 impl->Init(); |
| 259 return impl; | 164 return impl; |
| 260 } | 165 } |
| 261 | 166 |
| 262 } // namespace chromeos | 167 } // namespace chromeos |
| OLD | NEW |