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 "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/bind.h" | |
| 8 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 9 #include "base/logging.h" | 10 #include "base/logging.h" |
| 10 #include "base/observer_list.h" | 11 #include "base/observer_list.h" |
| 11 #include "base/time.h" | 12 #include "base/time.h" |
| 12 #include "base/timer.h" | 13 #include "base/timer.h" |
| 13 #include "chrome/browser/chromeos/cros/cros_library.h" | 14 #include "chrome/browser/chromeos/cros/cros_library.h" |
| 15 #include "chrome/browser/chromeos/dbus/dbus_thread_manager.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 : resume_status_connection_(NULL), |
| 24 resume_status_connection_(NULL), | |
| 25 status_(chromeos::PowerStatus()) { | 26 status_(chromeos::PowerStatus()) { |
| 26 } | 27 } |
| 27 | 28 |
| 28 virtual ~PowerLibraryImpl() { | 29 virtual ~PowerLibraryImpl() { |
| 29 if (power_status_connection_) { | |
| 30 chromeos::DisconnectPowerStatus(power_status_connection_); | |
| 31 power_status_connection_ = NULL; | |
| 32 } | |
| 33 if (resume_status_connection_) { | 30 if (resume_status_connection_) { |
| 34 chromeos::DisconnectResume(resume_status_connection_); | 31 chromeos::DisconnectResume(resume_status_connection_); |
| 35 resume_status_connection_ = NULL; | 32 resume_status_connection_ = NULL; |
| 36 } | 33 } |
| 37 } | 34 } |
| 38 | 35 |
| 39 // Begin PowerLibrary implementation. | 36 // Begin PowerLibrary implementation. |
| 40 virtual void Init() OVERRIDE { | 37 virtual void Init() OVERRIDE { |
| 41 DCHECK(CrosLibrary::Get()->libcros_loaded()); | 38 DCHECK(CrosLibrary::Get()->libcros_loaded()); |
| 42 power_status_connection_ = | |
| 43 chromeos::MonitorPowerStatus(&PowerStatusChangedHandler, this); | |
| 44 resume_status_connection_ = | 39 resume_status_connection_ = |
| 45 chromeos::MonitorResume(&SystemResumedHandler, this); | 40 chromeos::MonitorResume(&SystemResumedHandler, this); |
| 46 } | 41 } |
| 47 | 42 |
| 48 virtual void AddObserver(Observer* observer) OVERRIDE { | 43 virtual void AddObserver(Observer* observer) OVERRIDE { |
| 49 observers_.AddObserver(observer); | 44 observers_.AddObserver(observer); |
| 50 } | 45 } |
| 51 | 46 |
| 52 virtual void RemoveObserver(Observer* observer) OVERRIDE { | 47 virtual void RemoveObserver(Observer* observer) OVERRIDE { |
| 53 observers_.RemoveObserver(observer); | 48 observers_.RemoveObserver(observer); |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 77 return base::TimeDelta::FromSeconds(status_.battery_time_to_full); | 72 return base::TimeDelta::FromSeconds(status_.battery_time_to_full); |
| 78 } | 73 } |
| 79 | 74 |
| 80 virtual void CalculateIdleTime(CalculateIdleTimeCallback* callback) OVERRIDE { | 75 virtual void CalculateIdleTime(CalculateIdleTimeCallback* callback) OVERRIDE { |
| 81 // TODO(sidor): Examine if it's really a good idea to use void* as a second | 76 // TODO(sidor): Examine if it's really a good idea to use void* as a second |
| 82 // argument. | 77 // argument. |
| 83 chromeos::GetIdleTime(&GetIdleTimeCallback, callback); | 78 chromeos::GetIdleTime(&GetIdleTimeCallback, callback); |
| 84 } | 79 } |
| 85 | 80 |
| 86 virtual void EnableScreenLock(bool enable) OVERRIDE { | 81 virtual void EnableScreenLock(bool enable) OVERRIDE { |
| 87 // Make sure we run on FILE thread becuase chromeos::EnableScreenLock | 82 // Make sure we run on FILE thread because chromeos::EnableScreenLock |
| 88 // would write power manager config file to disk. | 83 // would write power manager config file to disk. |
| 89 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) { | 84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 90 BrowserThread::PostTask( | |
| 91 BrowserThread::FILE, FROM_HERE, | |
| 92 NewRunnableMethod(this, &PowerLibraryImpl::EnableScreenLock, enable)); | |
| 93 return; | |
| 94 } | |
| 95 | 85 |
| 96 chromeos::EnableScreenLock(enable); | 86 chromeos::EnableScreenLock(enable); |
| 97 } | 87 } |
| 98 | 88 |
| 99 virtual void RequestRestart() OVERRIDE { | 89 virtual void RequestRestart() OVERRIDE { |
| 100 chromeos::RequestRestart(); | 90 chromeos::RequestRestart(); |
| 101 } | 91 } |
| 102 | 92 |
| 103 virtual void RequestShutdown() OVERRIDE { | 93 virtual void RequestShutdown() OVERRIDE { |
| 104 chromeos::RequestShutdown(); | 94 chromeos::RequestShutdown(); |
| 105 } | 95 } |
| 106 | 96 |
| 107 virtual void RequestStatusUpdate() OVERRIDE { | 97 virtual void RequestStatusUpdate() OVERRIDE { |
| 108 // TODO(stevenjb): chromeos::RetrievePowerInformation has been deprecated; | 98 // TODO(stevenjb): chromeos::RetrievePowerInformation has been deprecated; |
| 109 // we should add a mechanism to immediately request an update, probably | 99 // we should add a mechanism to immediately request an update, probably |
| 110 // when we migrate the DBus code from libcros to here. | 100 // when we migrate the DBus code from libcros to here. |
| 111 } | 101 } |
| 112 | 102 |
| 103 // PowerManagerClient::Observer implementation. | |
| 104 virtual void UpdatePowerStatus(const chromeos::PowerStatus& status) OVERRIDE { | |
| 105 // Make this is being called from UI thread. | |
| 106 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 107 | |
| 108 DVLOG(1) << "Power lpo=" << status.line_power_on | |
|
satorux1
2011/10/14 05:56:28
Please just use VLOG(1). DVLOG is rarely useful.
Simon Que
2011/10/18 19:23:06
Since this is just being moved, can I make this ch
| |
| 109 << " sta=" << status.battery_state | |
|
satorux1
2011/10/14 05:56:28
We eschew abbreviations like "sta" per our C++ sty
| |
| 110 << " per=" << status.battery_percentage | |
| 111 << " tte=" << status.battery_time_to_empty | |
| 112 << " ttf=" << status.battery_time_to_full; | |
|
satorux1
2011/10/14 05:56:28
I'd suggest to add ToString() function to PowerSta
| |
| 113 status_ = status; | |
| 114 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(this)); | |
|
satorux1
2011/10/14 05:56:28
This looks rather redundant.
UpdatePowerStatus()
Simon Que
2011/10/18 21:09:34
What about in src/chrome/browser/automation/testin
satorux1
2011/10/18 22:07:14
Ugh, you are right. We cannot get rid of the gette
| |
| 115 } | |
| 116 | |
| 113 // End PowerLibrary implementation. | 117 // End PowerLibrary implementation. |
| 114 | 118 |
| 115 private: | 119 private: |
| 116 static void GetIdleTimeCallback(void* object, | 120 static void GetIdleTimeCallback(void* object, |
| 117 int64_t time_idle_ms, | 121 int64_t time_idle_ms, |
| 118 bool success) { | 122 bool success) { |
| 119 DCHECK(object); | 123 DCHECK(object); |
| 120 CalculateIdleTimeCallback* notify = | 124 CalculateIdleTimeCallback* notify = |
| 121 static_cast<CalculateIdleTimeCallback*>(object); | 125 static_cast<CalculateIdleTimeCallback*>(object); |
| 122 if (success) { | 126 if (success) { |
| 123 notify->Run(time_idle_ms/1000); | 127 notify->Run(time_idle_ms/1000); |
| 124 } else { | 128 } else { |
| 125 LOG(ERROR) << "Power manager failed to calculate idle time."; | 129 LOG(ERROR) << "Power manager failed to calculate idle time."; |
| 126 notify->Run(-1); | 130 notify->Run(-1); |
| 127 } | 131 } |
| 128 delete notify; | 132 delete notify; |
| 129 } | 133 } |
| 130 | 134 |
| 131 static void PowerStatusChangedHandler(void* object, | 135 static void PowerStatusChangedHandler(void* object, |
| 132 const chromeos::PowerStatus& status) { | 136 const chromeos::PowerStatus& status) { |
| 133 PowerLibraryImpl* power = static_cast<PowerLibraryImpl*>(object); | 137 PowerLibraryImpl* power = static_cast<PowerLibraryImpl*>(object); |
| 134 power->UpdatePowerStatus(status); | 138 power->UpdatePowerStatus(status); |
| 135 } | 139 } |
|
satorux1
2011/10/14 05:56:28
I think you can remove this function. No longer, u
Simon Que
2011/10/18 19:23:06
Done.
| |
| 136 | 140 |
| 137 static void SystemResumedHandler(void* object) { | 141 static void SystemResumedHandler(void* object) { |
| 138 PowerLibraryImpl* power = static_cast<PowerLibraryImpl*>(object); | 142 PowerLibraryImpl* power = static_cast<PowerLibraryImpl*>(object); |
| 139 power->SystemResumed(); | 143 power->SystemResumed(); |
| 140 } | 144 } |
| 141 | 145 |
| 142 void UpdatePowerStatus(const chromeos::PowerStatus& status) { | |
| 143 // Make sure we run on UI thread. | |
| 144 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | |
| 145 BrowserThread::PostTask( | |
| 146 BrowserThread::UI, FROM_HERE, | |
| 147 NewRunnableMethod( | |
| 148 this, &PowerLibraryImpl::UpdatePowerStatus, status)); | |
| 149 return; | |
| 150 } | |
| 151 | |
| 152 DVLOG(1) << "Power lpo=" << status.line_power_on | |
| 153 << " sta=" << status.battery_state | |
| 154 << " per=" << status.battery_percentage | |
| 155 << " tte=" << status.battery_time_to_empty | |
| 156 << " ttf=" << status.battery_time_to_full; | |
| 157 status_ = status; | |
| 158 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(this)); | |
| 159 } | |
| 160 | |
| 161 void SystemResumed() { | 146 void SystemResumed() { |
| 162 // Make sure we run on the UI thread. | 147 // Make sure we run on the UI thread. |
| 163 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 164 BrowserThread::PostTask( | |
| 165 BrowserThread::UI, FROM_HERE, | |
| 166 NewRunnableMethod(this, &PowerLibraryImpl::SystemResumed)); | |
| 167 return; | |
| 168 } | |
| 169 | 149 |
| 170 FOR_EACH_OBSERVER(Observer, observers_, SystemResumed()); | 150 FOR_EACH_OBSERVER(Observer, observers_, SystemResumed()); |
| 171 } | 151 } |
| 172 | 152 |
| 173 ObserverList<Observer> observers_; | 153 ObserverList<Observer> observers_; |
| 174 | 154 |
| 175 // A reference to the power battery API, to allow callbacks when the battery | |
| 176 // status changes. | |
| 177 chromeos::PowerStatusConnection power_status_connection_; | |
| 178 | |
| 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. | 158 // The latest power status. |
| 183 chromeos::PowerStatus status_; | 159 chromeos::PowerStatus status_; |
| 184 | 160 |
| 185 DISALLOW_COPY_AND_ASSIGN(PowerLibraryImpl); | 161 DISALLOW_COPY_AND_ASSIGN(PowerLibraryImpl); |
| 186 }; | 162 }; |
| 187 | 163 |
| 188 // The stub implementation runs the battery up and down, pausing at the | 164 // The stub implementation runs the battery up and down, pausing at the |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 253 timer_.Start( | 229 timer_.Start( |
| 254 FROM_HERE, | 230 FROM_HERE, |
| 255 base::TimeDelta::FromMilliseconds(100), | 231 base::TimeDelta::FromMilliseconds(100), |
| 256 this, | 232 this, |
| 257 &PowerLibraryStubImpl::Update); | 233 &PowerLibraryStubImpl::Update); |
| 258 } else { | 234 } else { |
| 259 timer_.Stop(); | 235 timer_.Stop(); |
| 260 } | 236 } |
| 261 } | 237 } |
| 262 | 238 |
| 239 virtual void UpdatePowerStatus(const chromeos::PowerStatus& status) OVERRIDE { | |
| 240 } | |
| 241 | |
| 263 // End PowerLibrary implementation. | 242 // End PowerLibrary implementation. |
| 264 | 243 |
| 265 private: | 244 private: |
| 266 void Update() { | 245 void Update() { |
| 267 // We pause at 0 and 100% so that it's easier to check those conditions. | 246 // We pause at 0 and 100% so that it's easier to check those conditions. |
| 268 if (pause_count_ > 1) { | 247 if (pause_count_ > 1) { |
| 269 pause_count_--; | 248 pause_count_--; |
| 270 return; | 249 return; |
| 271 } | 250 } |
| 272 | 251 |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 299 impl = new PowerLibraryImpl(); | 278 impl = new PowerLibraryImpl(); |
| 300 impl->Init(); | 279 impl->Init(); |
| 301 return impl; | 280 return impl; |
| 302 } | 281 } |
| 303 | 282 |
| 304 } // namespace chromeos | 283 } // namespace chromeos |
| 305 | 284 |
| 306 // Allows InvokeLater without adding refcounting. This class is a Singleton and | 285 // Allows InvokeLater without adding refcounting. This class is a Singleton and |
| 307 // won't be deleted until it's last InvokeLater is run. | 286 // won't be deleted until it's last InvokeLater is run. |
| 308 DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::PowerLibraryImpl); | 287 DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::PowerLibraryImpl); |
| OLD | NEW |