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 #include <inttypes.h> | |
| 8 | 9 |
| 9 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 10 #include "base/bind.h" | 11 #include "base/bind.h" |
| 11 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 12 #include "base/logging.h" | 13 #include "base/logging.h" |
| 13 #include "base/observer_list.h" | 14 #include "base/observer_list.h" |
| 15 #include "base/stringprintf.h" | |
| 14 #include "base/time.h" | 16 #include "base/time.h" |
| 15 #include "base/timer.h" | 17 #include "base/timer.h" |
| 16 #include "chrome/browser/chromeos/cros/cros_library.h" | 18 #include "chrome/browser/chromeos/cros/cros_library.h" |
| 19 #include "chrome/browser/chromeos/dbus/dbus_thread_manager.h" | |
| 17 #include "content/browser/browser_thread.h" | 20 #include "content/browser/browser_thread.h" |
| 18 #include "third_party/cros/chromeos_power.h" | 21 #include "third_party/cros/chromeos_power.h" |
| 19 #include "third_party/cros/chromeos_resume.h" | 22 #include "third_party/cros/chromeos_resume.h" |
| 20 | 23 |
| 21 namespace chromeos { | 24 namespace chromeos { |
| 22 | 25 |
| 26 const std::string& PowerSupplyStatus::ToString() const { | |
| 27 static std::string result = ""; | |
| 28 base::StringAppendF(&result, | |
| 29 "line_power_on = %s ", | |
| 30 line_power_on ? "true" : "false"); | |
| 31 base::StringAppendF(&result, | |
| 32 "battery_is_present = %s ", | |
| 33 battery_is_present ? "true" : "false"); | |
| 34 base::StringAppendF(&result, | |
| 35 "battery_is_full = %s ", | |
| 36 battery_is_full ? "true" : "false"); | |
| 37 base::StringAppendF(&result, | |
| 38 "battery_percentage = %f ", | |
| 39 battery_percentage); | |
| 40 base::StringAppendF(&result, | |
| 41 "battery_seconds_to_empty = %"PRId64" ", | |
| 42 battery_seconds_to_empty); | |
| 43 base::StringAppendF(&result, | |
| 44 "battery_seconds_to_full = %"PRId64" ", | |
| 45 battery_seconds_to_full); | |
| 46 return result; | |
| 47 } | |
| 48 | |
| 23 class PowerLibraryImpl : public PowerLibrary { | 49 class PowerLibraryImpl : public PowerLibrary { |
| 24 public: | 50 public: |
| 25 PowerLibraryImpl() | 51 PowerLibraryImpl() |
| 26 : power_status_connection_(NULL), | 52 : resume_status_connection_(NULL) { |
| 27 resume_status_connection_(NULL) { | |
| 28 } | 53 } |
| 29 | 54 |
| 30 virtual ~PowerLibraryImpl() { | 55 virtual ~PowerLibraryImpl() { |
| 31 if (power_status_connection_) { | |
| 32 chromeos::DisconnectPowerStatus(power_status_connection_); | |
| 33 power_status_connection_ = NULL; | |
| 34 } | |
| 35 if (resume_status_connection_) { | 56 if (resume_status_connection_) { |
| 36 chromeos::DisconnectResume(resume_status_connection_); | 57 chromeos::DisconnectResume(resume_status_connection_); |
| 37 resume_status_connection_ = NULL; | 58 resume_status_connection_ = NULL; |
| 38 } | 59 } |
| 39 } | 60 } |
| 40 | 61 |
| 41 // Begin PowerLibrary implementation. | 62 // Begin PowerLibrary implementation. |
| 42 virtual void Init() OVERRIDE { | 63 virtual void Init() OVERRIDE { |
| 43 DCHECK(CrosLibrary::Get()->libcros_loaded()); | 64 DCHECK(CrosLibrary::Get()->libcros_loaded()); |
| 44 power_status_connection_ = | |
| 45 chromeos::MonitorPowerStatus(&PowerStatusChangedHandler, this); | |
| 46 resume_status_connection_ = | 65 resume_status_connection_ = |
| 47 chromeos::MonitorResume(&SystemResumedHandler, this); | 66 chromeos::MonitorResume(&SystemResumedHandler, this); |
| 48 } | 67 } |
| 49 | 68 |
| 50 virtual void AddObserver(Observer* observer) OVERRIDE { | 69 virtual void AddObserver(Observer* observer) OVERRIDE { |
| 51 observers_.AddObserver(observer); | 70 observers_.AddObserver(observer); |
| 52 } | 71 } |
| 53 | 72 |
| 54 virtual void RemoveObserver(Observer* observer) OVERRIDE { | 73 virtual void RemoveObserver(Observer* observer) OVERRIDE { |
| 55 observers_.RemoveObserver(observer); | 74 observers_.RemoveObserver(observer); |
| 56 } | 75 } |
| 57 | 76 |
| 58 virtual void CalculateIdleTime(CalculateIdleTimeCallback* callback) OVERRIDE { | 77 virtual void CalculateIdleTime(CalculateIdleTimeCallback* callback) OVERRIDE { |
| 59 // TODO(sidor): Examine if it's really a good idea to use void* as a second | 78 // TODO(sidor): Examine if it's really a good idea to use void* as a second |
| 60 // argument. | 79 // argument. |
| 61 chromeos::GetIdleTime(&GetIdleTimeCallback, callback); | 80 chromeos::GetIdleTime(&GetIdleTimeCallback, callback); |
| 62 } | 81 } |
| 63 | 82 |
| 64 virtual void EnableScreenLock(bool enable) OVERRIDE { | 83 virtual void EnableScreenLock(bool enable) OVERRIDE { |
| 65 // Called when the screen preference is changed, which should always | 84 // Make sure we run on FILE thread because chromeos::EnableScreenLock |
| 66 // run on UI thread. | |
| 67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 68 // Post the task to FILE thread as chromeos::EnableScreenLock | |
| 69 // would write power manager config file to disk. | 85 // would write power manager config file to disk. |
| 70 BrowserThread::PostTask( | 86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 71 BrowserThread::FILE, FROM_HERE, | 87 |
| 72 base::Bind(&PowerLibraryImpl::DoEnableScreenLock, enable)); | 88 chromeos::EnableScreenLock(enable); |
| 73 } | 89 } |
| 74 | 90 |
| 75 virtual void RequestRestart() OVERRIDE { | 91 virtual void RequestRestart() OVERRIDE { |
| 76 chromeos::RequestRestart(); | 92 chromeos::RequestRestart(); |
| 77 } | 93 } |
| 78 | 94 |
| 79 virtual void RequestShutdown() OVERRIDE { | 95 virtual void RequestShutdown() OVERRIDE { |
| 80 chromeos::RequestShutdown(); | 96 chromeos::RequestShutdown(); |
| 81 } | 97 } |
| 82 | 98 |
| 83 virtual void RequestStatusUpdate() OVERRIDE { | 99 virtual void RequestStatusUpdate() OVERRIDE { |
| 84 // TODO(stevenjb): chromeos::RetrievePowerInformation has been deprecated; | 100 // TODO(stevenjb): chromeos::RetrievePowerInformation has been deprecated; |
| 85 // we should add a mechanism to immediately request an update, probably | 101 // we should add a mechanism to immediately request an update, probably |
| 86 // when we migrate the DBus code from libcros to here. | 102 // when we migrate the DBus code from libcros to here. |
| 87 } | 103 } |
| 88 | 104 |
| 105 // PowerManagerClient::Observer implementation. | |
| 106 virtual void UpdatePowerStatus(const PowerStatus& power_status) OVERRIDE { | |
| 107 // Make this is being called from UI thread. | |
| 108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 109 | |
| 110 // TODO(sque): this is a temporary copy-over from libcros. Soon libcros | |
| 111 // will be removed and this will not be necessary. | |
| 112 PowerSupplyStatus status; | |
| 113 status.line_power_on = power_status.line_power_on; | |
| 114 status.battery_is_present = power_status.battery_is_present; | |
| 115 status.battery_is_full = | |
| 116 (power_status.battery_state == chromeos::BATTERY_STATE_FULLY_CHARGED); | |
| 117 status.battery_seconds_to_empty = power_status.battery_time_to_empty; | |
| 118 status.battery_seconds_to_full = power_status.battery_time_to_full; | |
| 119 status.battery_percentage = power_status.battery_percentage; | |
| 120 | |
| 121 VLOG(1) << "Power status: " << status.ToString(); | |
| 122 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(status)); | |
|
satorux1
2011/10/27 21:09:21
The method is essentially redirecting UpdatePowerS
Simon Que
2011/10/27 22:46:20
Done.
| |
| 123 } | |
| 124 | |
| 89 // End PowerLibrary implementation. | 125 // End PowerLibrary implementation. |
| 90 | 126 |
| 91 private: | 127 private: |
| 92 static void DoEnableScreenLock(bool enable) { | 128 static void DoEnableScreenLock(bool enable) { |
| 93 chromeos::EnableScreenLock(enable); | 129 chromeos::EnableScreenLock(enable); |
| 94 } | 130 } |
| 95 | 131 |
| 96 static void GetIdleTimeCallback(void* object, | 132 static void GetIdleTimeCallback(void* object, |
| 97 int64_t time_idle_ms, | 133 int64_t time_idle_ms, |
| 98 bool success) { | 134 bool success) { |
| 99 DCHECK(object); | 135 DCHECK(object); |
| 100 CalculateIdleTimeCallback* notify = | 136 CalculateIdleTimeCallback* notify = |
| 101 static_cast<CalculateIdleTimeCallback*>(object); | 137 static_cast<CalculateIdleTimeCallback*>(object); |
| 102 if (success) { | 138 if (success) { |
| 103 notify->Run(time_idle_ms/1000); | 139 notify->Run(time_idle_ms/1000); |
| 104 } else { | 140 } else { |
| 105 LOG(ERROR) << "Power manager failed to calculate idle time."; | 141 LOG(ERROR) << "Power manager failed to calculate idle time."; |
| 106 notify->Run(-1); | 142 notify->Run(-1); |
| 107 } | 143 } |
| 108 delete notify; | 144 delete notify; |
| 109 } | 145 } |
| 110 | 146 |
| 111 static void PowerStatusChangedHandler( | |
| 112 void* object, const chromeos::PowerStatus& power_status) { | |
| 113 // TODO(sque): this is a temporary copy-over from libcros. Soon libcros | |
| 114 // will be removed and this will not be necessary. | |
| 115 PowerSupplyStatus status; | |
| 116 status.line_power_on = power_status.line_power_on; | |
| 117 status.battery_is_present = power_status.battery_is_present; | |
| 118 status.battery_is_full = | |
| 119 (power_status.battery_state == chromeos::BATTERY_STATE_FULLY_CHARGED); | |
| 120 status.battery_seconds_to_empty = power_status.battery_time_to_empty; | |
| 121 status.battery_seconds_to_full = power_status.battery_time_to_full; | |
| 122 status.battery_percentage = power_status.battery_percentage; | |
| 123 | |
| 124 PowerLibraryImpl* power = static_cast<PowerLibraryImpl*>(object); | |
| 125 power->UpdatePowerStatus(status); | |
| 126 } | |
| 127 | |
| 128 static void SystemResumedHandler(void* object) { | 147 static void SystemResumedHandler(void* object) { |
| 129 PowerLibraryImpl* power = static_cast<PowerLibraryImpl*>(object); | 148 PowerLibraryImpl* power = static_cast<PowerLibraryImpl*>(object); |
| 130 power->SystemResumed(); | 149 power->SystemResumed(); |
| 131 } | 150 } |
| 132 | 151 |
| 133 void UpdatePowerStatus(const PowerSupplyStatus& status) { | |
| 134 // Called from PowerStatusChangedHandler, a libcros callback which | |
| 135 // should always run on UI thread. | |
| 136 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 137 | |
| 138 DVLOG(1) << "Power line_power_on = " << status.line_power_on | |
| 139 << " percentage = " << status.battery_percentage | |
| 140 << " seconds_to_empty = " << status.battery_seconds_to_empty | |
| 141 << " seconds_to_full = " << status.battery_seconds_to_full; | |
| 142 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(status)); | |
| 143 } | |
| 144 | |
| 145 void SystemResumed() { | 152 void SystemResumed() { |
| 146 // Called from SystemResumedHandler, a libcros callback which should | 153 // Called from SystemResumedHandler, a libcros callback which should |
| 147 // always run on UI thread. | 154 // always run on UI thread. |
| 148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 155 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 156 | |
| 149 FOR_EACH_OBSERVER(Observer, observers_, SystemResumed()); | 157 FOR_EACH_OBSERVER(Observer, observers_, SystemResumed()); |
| 150 } | 158 } |
| 151 | 159 |
| 152 ObserverList<Observer> observers_; | 160 ObserverList<Observer> observers_; |
| 153 | 161 |
| 154 // A reference to the power battery API, to allow callbacks when the battery | |
| 155 // status changes. | |
| 156 chromeos::PowerStatusConnection power_status_connection_; | |
| 157 | |
| 158 // A reference to the resume alerts. | 162 // A reference to the resume alerts. |
| 159 chromeos::ResumeConnection resume_status_connection_; | 163 chromeos::ResumeConnection resume_status_connection_; |
| 160 | 164 |
| 161 DISALLOW_COPY_AND_ASSIGN(PowerLibraryImpl); | 165 DISALLOW_COPY_AND_ASSIGN(PowerLibraryImpl); |
| 162 }; | 166 }; |
| 163 | 167 |
| 164 // The stub implementation runs the battery up and down, pausing at the | 168 // The stub implementation runs the battery up and down, pausing at the |
| 165 // fully charged and fully depleted states. | 169 // fully charged and fully depleted states. |
| 166 class PowerLibraryStubImpl : public PowerLibrary { | 170 class PowerLibraryStubImpl : public PowerLibrary { |
| 167 public: | 171 public: |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 199 timer_.Start( | 203 timer_.Start( |
| 200 FROM_HERE, | 204 FROM_HERE, |
| 201 base::TimeDelta::FromMilliseconds(100), | 205 base::TimeDelta::FromMilliseconds(100), |
| 202 this, | 206 this, |
| 203 &PowerLibraryStubImpl::Update); | 207 &PowerLibraryStubImpl::Update); |
| 204 } else { | 208 } else { |
| 205 timer_.Stop(); | 209 timer_.Stop(); |
| 206 } | 210 } |
| 207 } | 211 } |
| 208 | 212 |
| 213 virtual void UpdatePowerStatus(const PowerStatus& status) OVERRIDE { | |
| 214 } | |
| 215 | |
| 209 // End PowerLibrary implementation. | 216 // End PowerLibrary implementation. |
| 210 | 217 |
| 211 private: | 218 private: |
| 212 void Update() { | 219 void Update() { |
| 213 // We pause at 0 and 100% so that it's easier to check those conditions. | 220 // We pause at 0 and 100% so that it's easier to check those conditions. |
| 214 if (pause_count_ > 1) { | 221 if (pause_count_ > 1) { |
| 215 pause_count_--; | 222 pause_count_--; |
| 216 return; | 223 return; |
| 217 } | 224 } |
| 218 | 225 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 251 PowerLibrary* impl; | 258 PowerLibrary* impl; |
| 252 if (stub) | 259 if (stub) |
| 253 impl = new PowerLibraryStubImpl(); | 260 impl = new PowerLibraryStubImpl(); |
| 254 else | 261 else |
| 255 impl = new PowerLibraryImpl(); | 262 impl = new PowerLibraryImpl(); |
| 256 impl->Init(); | 263 impl->Init(); |
| 257 return impl; | 264 return impl; |
| 258 } | 265 } |
| 259 | 266 |
| 260 } // namespace chromeos | 267 } // namespace chromeos |
| OLD | NEW |