Chromium Code Reviews| Index: chrome/browser/chromeos/cros/power_library.cc |
| diff --git a/chrome/browser/chromeos/cros/power_library.cc b/chrome/browser/chromeos/cros/power_library.cc |
| index cb675c9425aa98c2762e4c761d4f7dbde124ea1d..80b6792e58b58c29f8a9a7b19df07b9077d6b1f6 100644 |
| --- a/chrome/browser/chromeos/cros/power_library.cc |
| +++ b/chrome/browser/chromeos/cros/power_library.cc |
| @@ -5,33 +5,54 @@ |
| #include "chrome/browser/chromeos/cros/power_library.h" |
| #include <algorithm> |
| +#include <inttypes.h> |
| #include "base/basictypes.h" |
| #include "base/bind.h" |
| #include "base/compiler_specific.h" |
| #include "base/logging.h" |
| #include "base/observer_list.h" |
| +#include "base/stringprintf.h" |
| #include "base/time.h" |
| #include "base/timer.h" |
| #include "chrome/browser/chromeos/cros/cros_library.h" |
| +#include "chrome/browser/chromeos/dbus/dbus_thread_manager.h" |
| #include "content/browser/browser_thread.h" |
| #include "third_party/cros/chromeos_power.h" |
| #include "third_party/cros/chromeos_resume.h" |
| namespace chromeos { |
| +const std::string& PowerSupplyStatus::ToString() const { |
| + static std::string result = ""; |
| + base::StringAppendF(&result, |
| + "line_power_on = %s ", |
| + line_power_on ? "true" : "false"); |
| + base::StringAppendF(&result, |
| + "battery_is_present = %s ", |
| + battery_is_present ? "true" : "false"); |
| + base::StringAppendF(&result, |
| + "battery_is_full = %s ", |
| + battery_is_full ? "true" : "false"); |
| + base::StringAppendF(&result, |
| + "battery_percentage = %f ", |
| + battery_percentage); |
| + base::StringAppendF(&result, |
| + "battery_seconds_to_empty = %"PRId64" ", |
| + battery_seconds_to_empty); |
| + base::StringAppendF(&result, |
| + "battery_seconds_to_full = %"PRId64" ", |
| + battery_seconds_to_full); |
| + return result; |
| +} |
| + |
| class PowerLibraryImpl : public PowerLibrary { |
| public: |
| PowerLibraryImpl() |
| - : power_status_connection_(NULL), |
| - resume_status_connection_(NULL) { |
| + : resume_status_connection_(NULL) { |
| } |
| virtual ~PowerLibraryImpl() { |
| - if (power_status_connection_) { |
| - chromeos::DisconnectPowerStatus(power_status_connection_); |
| - power_status_connection_ = NULL; |
| - } |
| if (resume_status_connection_) { |
| chromeos::DisconnectResume(resume_status_connection_); |
| resume_status_connection_ = NULL; |
| @@ -41,8 +62,6 @@ class PowerLibraryImpl : public PowerLibrary { |
| // Begin PowerLibrary implementation. |
| virtual void Init() OVERRIDE { |
| DCHECK(CrosLibrary::Get()->libcros_loaded()); |
| - power_status_connection_ = |
| - chromeos::MonitorPowerStatus(&PowerStatusChangedHandler, this); |
| resume_status_connection_ = |
| chromeos::MonitorResume(&SystemResumedHandler, this); |
| } |
| @@ -62,14 +81,11 @@ class PowerLibraryImpl : public PowerLibrary { |
| } |
| virtual void EnableScreenLock(bool enable) OVERRIDE { |
| - // Called when the screen preference is changed, which should always |
| - // run on UI thread. |
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| - // Post the task to FILE thread as chromeos::EnableScreenLock |
| + // Make sure we run on FILE thread because chromeos::EnableScreenLock |
| // would write power manager config file to disk. |
| - BrowserThread::PostTask( |
| - BrowserThread::FILE, FROM_HERE, |
| - base::Bind(&PowerLibraryImpl::DoEnableScreenLock, enable)); |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| + |
| + chromeos::EnableScreenLock(enable); |
| } |
| virtual void RequestRestart() OVERRIDE { |
| @@ -86,6 +102,26 @@ class PowerLibraryImpl : public PowerLibrary { |
| // when we migrate the DBus code from libcros to here. |
| } |
| + // PowerManagerClient::Observer implementation. |
| + virtual void UpdatePowerStatus(const PowerStatus& power_status) OVERRIDE { |
| + // Make this is being called from UI thread. |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + |
| + // TODO(sque): this is a temporary copy-over from libcros. Soon libcros |
| + // will be removed and this will not be necessary. |
| + PowerSupplyStatus status; |
| + status.line_power_on = power_status.line_power_on; |
| + status.battery_is_present = power_status.battery_is_present; |
| + status.battery_is_full = |
| + (power_status.battery_state == chromeos::BATTERY_STATE_FULLY_CHARGED); |
| + status.battery_seconds_to_empty = power_status.battery_time_to_empty; |
| + status.battery_seconds_to_full = power_status.battery_time_to_full; |
| + status.battery_percentage = power_status.battery_percentage; |
| + |
| + VLOG(1) << "Power status: " << status.ToString(); |
| + 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.
|
| + } |
| + |
| // End PowerLibrary implementation. |
| private: |
| @@ -108,53 +144,21 @@ class PowerLibraryImpl : public PowerLibrary { |
| delete notify; |
| } |
| - static void PowerStatusChangedHandler( |
| - void* object, const chromeos::PowerStatus& power_status) { |
| - // TODO(sque): this is a temporary copy-over from libcros. Soon libcros |
| - // will be removed and this will not be necessary. |
| - PowerSupplyStatus status; |
| - status.line_power_on = power_status.line_power_on; |
| - status.battery_is_present = power_status.battery_is_present; |
| - status.battery_is_full = |
| - (power_status.battery_state == chromeos::BATTERY_STATE_FULLY_CHARGED); |
| - status.battery_seconds_to_empty = power_status.battery_time_to_empty; |
| - status.battery_seconds_to_full = power_status.battery_time_to_full; |
| - status.battery_percentage = power_status.battery_percentage; |
| - |
| - PowerLibraryImpl* power = static_cast<PowerLibraryImpl*>(object); |
| - power->UpdatePowerStatus(status); |
| - } |
| - |
| static void SystemResumedHandler(void* object) { |
| PowerLibraryImpl* power = static_cast<PowerLibraryImpl*>(object); |
| power->SystemResumed(); |
| } |
| - void UpdatePowerStatus(const PowerSupplyStatus& status) { |
| - // Called from PowerStatusChangedHandler, a libcros callback which |
| - // should always run on UI thread. |
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| - |
| - DVLOG(1) << "Power line_power_on = " << status.line_power_on |
| - << " percentage = " << status.battery_percentage |
| - << " seconds_to_empty = " << status.battery_seconds_to_empty |
| - << " seconds_to_full = " << status.battery_seconds_to_full; |
| - FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(status)); |
| - } |
| - |
| void SystemResumed() { |
| // Called from SystemResumedHandler, a libcros callback which should |
| // always run on UI thread. |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + |
| FOR_EACH_OBSERVER(Observer, observers_, SystemResumed()); |
| } |
| ObserverList<Observer> observers_; |
| - // A reference to the power battery API, to allow callbacks when the battery |
| - // status changes. |
| - chromeos::PowerStatusConnection power_status_connection_; |
| - |
| // A reference to the resume alerts. |
| chromeos::ResumeConnection resume_status_connection_; |
| @@ -206,6 +210,9 @@ class PowerLibraryStubImpl : public PowerLibrary { |
| } |
| } |
| + virtual void UpdatePowerStatus(const PowerStatus& status) OVERRIDE { |
| + } |
| + |
| // End PowerLibrary implementation. |
| private: |