Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1062)

Unified Diff: chrome/browser/chromeos/dbus/power_manager_client.cc

Issue 8271024: chromeos: Add power supply info reading capability (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Rebased Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/browser/chromeos/dbus/power_manager_client.h ('k') | chrome/browser/chromeos/low_battery_observer.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/chromeos/dbus/power_manager_client.cc
diff --git a/chrome/browser/chromeos/dbus/power_manager_client.cc b/chrome/browser/chromeos/dbus/power_manager_client.cc
index 62971590b145ffbabe3a543830a95c5745fa802d..a4236e013b92c00bd52866bcf421e71147bd1a9c 100644
--- a/chrome/browser/chromeos/dbus/power_manager_client.cc
+++ b/chrome/browser/chromeos/dbus/power_manager_client.cc
@@ -6,6 +6,11 @@
#include "base/bind.h"
#include "base/callback.h"
+#include "base/format_macros.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/stringprintf.h"
+#include "base/time.h"
+#include "base/timer.h"
#include "chrome/browser/chromeos/system/runtime_environment.h"
#include "dbus/bus.h"
#include "dbus/message.h"
@@ -14,6 +19,38 @@
namespace chromeos {
+PowerSupplyStatus::PowerSupplyStatus()
+ : line_power_on(false),
+ battery_is_present(false),
+ battery_is_full(false),
+ battery_seconds_to_empty(0),
+ battery_seconds_to_full(0),
+ battery_percentage(0) {
+}
+
+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);
Nico 2011/11/20 22:38:21 This will append "line_power_on" to result every t
satorux1 2011/11/21 17:28:05 Good catch! I wasn't able to catch this. Simon, pl
+ return result;
+}
+
// The PowerManagerClient implementation used in production.
class PowerManagerClientImpl : public PowerManagerClient {
public:
@@ -32,7 +69,16 @@ class PowerManagerClientImpl : public PowerManagerClient {
power_manager::kBrightnessChangedSignal,
base::Bind(&PowerManagerClientImpl::BrightnessChangedReceived,
weak_ptr_factory_.GetWeakPtr()),
- base::Bind(&PowerManagerClientImpl::BrightnessChangedConnected,
+ base::Bind(&PowerManagerClientImpl::SignalConnected,
+ weak_ptr_factory_.GetWeakPtr()));
+
+ // Monitor the D-Bus signal for power supply polling signals.
+ power_manager_proxy_->ConnectToSignal(
+ power_manager::kPowerManagerInterface,
+ power_manager::kPowerSupplyPollSignal,
+ base::Bind(&PowerManagerClientImpl::PowerSupplyPollReceived,
+ weak_ptr_factory_.GetWeakPtr()),
+ base::Bind(&PowerManagerClientImpl::SignalConnected,
weak_ptr_factory_.GetWeakPtr()));
}
@@ -76,6 +122,21 @@ class PowerManagerClientImpl : public PowerManagerClient {
weak_ptr_factory_.GetWeakPtr()));
}
+ virtual void RequestStatusUpdate() OVERRIDE {
+ // TODO(stevenjb): chromeos::RetrievePowerInformation has been deprecated;
+ // we should add a mechanism to immediately request an update, probably
+ // when we migrate the DBus code from libcros to here.
+ }
+
+ private:
+ // Called when a dbus signal is initially connected.
+ void SignalConnected(const std::string& interface_name,
+ const std::string& signal_name,
+ bool success) {
+ LOG_IF(WARNING, !success) << "Failed to connect to signal "
+ << signal_name << ".";
+ }
+
// Called when a brightness change signal is received.
void BrightnessChangedReceived(dbus::Signal* signal) {
dbus::MessageReader reader(signal);
@@ -93,14 +154,6 @@ class PowerManagerClientImpl : public PowerManagerClient {
BrightnessChanged(brightness_level, user_initiated));
}
- // Called when the brightness change signal is initially connected.
- void BrightnessChangedConnected(const std::string& interface_name,
- const std::string& signal_name,
- bool success) {
- LOG_IF(WARNING, !success)
- << "Failed to connect to brightness changed signal.";
- }
-
// Called when a response for DecreaseScreenBrightness() is received.
void OnDecreaseScreenBrightness(dbus::Response* response) {
if (!response) {
@@ -119,6 +172,54 @@ class PowerManagerClientImpl : public PowerManagerClient {
VLOG(1) << "screen brightness increased: " << response->ToString();
}
+ // Called when a power supply polling signal is received.
+ void PowerSupplyPollReceived(dbus::Signal* signal) {
+ dbus::MessageReader reader(signal);
+ VLOG(1) << "Received power supply poll signal.";
+ GetPowerSupplyInfo();
+ }
+
+ // Gets the state of the power supply (line power and battery) from power
+ // manager.
+ void GetPowerSupplyInfo() {
+ dbus::MethodCall method_call(power_manager::kPowerManagerInterface,
+ power_manager::kGetAllPropertiesMethod);
+ power_manager_proxy_->CallMethod(
+ &method_call,
+ dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
+ base::Bind(&PowerManagerClientImpl::OnGetAllPropertiesMethod,
+ weak_ptr_factory_.GetWeakPtr()));
+ }
+
+ // Called when GetAllPropertiesMethod call is complete.
+ void OnGetAllPropertiesMethod(dbus::Response* response) {
+ if (!response) {
+ LOG(ERROR) << "Error calling " << power_manager::kGetAllPropertiesMethod;
+ return;
+ }
+ dbus::MessageReader reader(response);
+ PowerSupplyStatus status;
+ double unused_battery_voltage = 0.0;
+ double unused_battery_energy = 0.0;
+ double unused_battery_energy_rate = 0.0;
+ if (!reader.PopBool(&status.line_power_on) ||
+ !reader.PopDouble(&unused_battery_energy) ||
+ !reader.PopDouble(&unused_battery_energy_rate) ||
+ !reader.PopDouble(&unused_battery_voltage) ||
+ !reader.PopInt64(&status.battery_seconds_to_empty) ||
+ !reader.PopInt64(&status.battery_seconds_to_full) ||
+ !reader.PopDouble(&status.battery_percentage) ||
+ !reader.PopBool(&status.battery_is_present) ||
+ !reader.PopBool(&status.battery_is_full)) {
+ LOG(ERROR) << "Error reading response from powerd: "
+ << response->ToString();
+ return;
+ }
+
+ VLOG(1) << "Power status: " << status.ToString();
+ FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(status));
+ }
+
dbus::ObjectProxy* power_manager_proxy_;
ObserverList<Observer> observers_;
base::WeakPtrFactory<PowerManagerClientImpl> weak_ptr_factory_;
@@ -129,23 +230,83 @@ class PowerManagerClientImpl : public PowerManagerClient {
// The PowerManagerClient implementation used on Linux desktop,
// which does nothing.
class PowerManagerClientStubImpl : public PowerManagerClient {
+ public:
+ PowerManagerClientStubImpl()
+ : discharging_(true),
+ battery_percentage_(80),
+ pause_count_(0) {
+ }
+
+ virtual ~PowerManagerClientStubImpl() {}
+
// PowerManagerClient override.
- virtual void AddObserver(Observer* observer) {
+ virtual void AddObserver(Observer* observer) OVERRIDE {
+ observers_.AddObserver(observer);
}
// PowerManagerClient override.
- virtual void RemoveObserver(Observer* observer) {
+ virtual void RemoveObserver(Observer* observer) OVERRIDE {
+ observers_.RemoveObserver(observer);
}
// PowerManagerClient override.
- virtual void DecreaseScreenBrightness(bool allow_off) {
+ virtual void DecreaseScreenBrightness(bool allow_off) OVERRIDE {
VLOG(1) << "Requested to descrease screen brightness";
}
// PowerManagerClient override.
- virtual void IncreaseScreenBrightness() {
+ virtual void IncreaseScreenBrightness() OVERRIDE {
VLOG(1) << "Requested to increase screen brightness";
}
+
+ virtual void RequestStatusUpdate() OVERRIDE {
+ if (!timer_.IsRunning()) {
+ timer_.Start(
+ FROM_HERE,
+ base::TimeDelta::FromMilliseconds(100),
+ this,
+ &PowerManagerClientStubImpl::Update);
+ } else {
+ timer_.Stop();
+ }
+ }
+
+ private:
+ void Update() {
+ // We pause at 0 and 100% so that it's easier to check those conditions.
+ if (pause_count_ > 1) {
+ pause_count_--;
+ return;
+ }
+
+ if (battery_percentage_ == 0 || battery_percentage_ == 100) {
+ if (pause_count_) {
+ pause_count_ = 0;
+ discharging_ = !discharging_;
+ } else {
+ pause_count_ = 20;
+ return;
+ }
+ }
+ battery_percentage_ += (discharging_ ? -1 : 1);
+
+ PowerSupplyStatus status;
+ status.line_power_on = !discharging_;
+ status.battery_is_present = true;
+ status.battery_percentage = battery_percentage_;
+ status.battery_seconds_to_empty =
+ std::max(1, battery_percentage_ * 180 / 100);
+ status.battery_seconds_to_full =
+ std::max(static_cast<int64>(1), 180 - status.battery_seconds_to_empty);
+
+ FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(status));
+ }
+
+ bool discharging_;
+ int battery_percentage_;
+ int pause_count_;
+ ObserverList<Observer> observers_;
+ base::RepeatingTimer<PowerManagerClientStubImpl> timer_;
};
PowerManagerClient::PowerManagerClient() {
« no previous file with comments | « chrome/browser/chromeos/dbus/power_manager_client.h ('k') | chrome/browser/chromeos/low_battery_observer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698