diff options
author | derat@chromium.org <derat@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-07-03 00:35:55 +0000 |
---|---|---|
committer | derat@chromium.org <derat@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-07-03 00:35:55 +0000 |
commit | 9fcb3e24ed3208d7be0c0bfc5845833bf2823686 (patch) | |
tree | 73f512dbb85cd2816eff5f5c0baadf39d1737aed /ash | |
parent | b7fa499af1ee55a31f13ca6ec87fd55b592df066 (diff) | |
download | chromium_src-9fcb3e24ed3208d7be0c0bfc5845833bf2823686.zip chromium_src-9fcb3e24ed3208d7be0c0bfc5845833bf2823686.tar.gz chromium_src-9fcb3e24ed3208d7be0c0bfc5845833bf2823686.tar.bz2 |
chromeos: Remove PowerSupplyStatus struct.
This removes the PowerSupplyStatus struct in favor of
directly using PowerSupplyProperties protocol buffers sent
by the power manager.
BUG=254173
Review URL: https://chromiumcodereview.appspot.com/18307002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@209830 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ash')
-rw-r--r-- | ash/ash.gyp | 6 | ||||
-rw-r--r-- | ash/system/chromeos/power/power_status.cc | 65 | ||||
-rw-r--r-- | ash/system/chromeos/power/power_status.h | 14 | ||||
-rw-r--r-- | ash/system/chromeos/power/power_status_unittest.cc | 1 | ||||
-rw-r--r-- | ash/system/chromeos/power/tray_power_unittest.cc | 79 |
5 files changed, 101 insertions, 64 deletions
diff --git a/ash/ash.gyp b/ash/ash.gyp index 6e826a3..3c7531c 100644 --- a/ash/ash.gyp +++ b/ash/ash.gyp @@ -551,6 +551,8 @@ ['chromeos==1', { 'dependencies': [ '../chromeos/chromeos.gyp:chromeos', + # Ash #includes power_supply_properties.pb.h directly. + '../chromeos/chromeos.gyp:power_manager_proto', ], }, { # else: chromeos!=1 'sources/': [ @@ -797,6 +799,10 @@ 'sources/': [ ['exclude', 'display/display_error_dialog_unittest.cc'], ], + }, { # chromeos==1 + 'dependencies': [ + '../chromeos/chromeos.gyp:power_manager_proto', + ], }], ], }, diff --git a/ash/system/chromeos/power/power_status.cc b/ash/system/chromeos/power/power_status.cc index bd00042..7982aa02 100644 --- a/ash/system/chromeos/power/power_status.cc +++ b/ash/system/chromeos/power/power_status.cc @@ -27,6 +27,23 @@ namespace internal { namespace { +// Updates |proto| to ensure that its fields are consistent. +void SanitizeProto(power_manager::PowerSupplyProperties* proto) { + DCHECK(proto); + + if (proto->battery_state() == + power_manager::PowerSupplyProperties_BatteryState_FULL) + proto->set_battery_percent(100.0); + + if (!proto->is_calculating_battery_time()) { + const bool on_line_power = proto->external_power() != + power_manager::PowerSupplyProperties_ExternalPower_DISCONNECTED; + if ((on_line_power && proto->battery_time_to_full_sec() < 0) || + (!on_line_power && proto->battery_time_to_empty_sec() < 0)) + proto->set_is_calculating_battery_time(true); + } +} + base::string16 GetBatteryTimeAccessibilityString(int hour, int min) { DCHECK(hour || min); if (hour && !min) { @@ -99,44 +116,49 @@ void PowerStatus::RequestStatusUpdate() { } bool PowerStatus::IsBatteryPresent() const { - return status_.battery_is_present; + return proto_.battery_state() != + power_manager::PowerSupplyProperties_BatteryState_NOT_PRESENT; } bool PowerStatus::IsBatteryFull() const { - return status_.battery_is_full; + return proto_.battery_state() == + power_manager::PowerSupplyProperties_BatteryState_FULL; } double PowerStatus::GetBatteryPercent() const { - return status_.battery_percentage; + return proto_.battery_percent(); } int PowerStatus::GetRoundedBatteryPercent() const { return std::max(kMinBatteryPercent, - static_cast<int>(status_.battery_percentage + 0.5)); + static_cast<int>(GetBatteryPercent() + 0.5)); } bool PowerStatus::IsBatteryTimeBeingCalculated() const { - return status_.is_calculating_battery_time; + return proto_.is_calculating_battery_time(); } base::TimeDelta PowerStatus::GetBatteryTimeToEmpty() const { - return base::TimeDelta::FromSeconds(status_.battery_seconds_to_empty); + return base::TimeDelta::FromSeconds(proto_.battery_time_to_empty_sec()); } base::TimeDelta PowerStatus::GetBatteryTimeToFull() const { - return base::TimeDelta::FromSeconds(status_.battery_seconds_to_full); + return base::TimeDelta::FromSeconds(proto_.battery_time_to_full_sec()); } bool PowerStatus::IsLinePowerConnected() const { - return status_.line_power_on; + return proto_.external_power() != + power_manager::PowerSupplyProperties_ExternalPower_DISCONNECTED; } bool PowerStatus::IsMainsChargerConnected() const { - return status_.battery_state == chromeos::PowerSupplyStatus::CHARGING; + return proto_.external_power() == + power_manager::PowerSupplyProperties_ExternalPower_AC; } bool PowerStatus::IsUsbChargerConnected() const { - return status_.battery_state == chromeos::PowerSupplyStatus::CONNECTED_TO_USB; + return proto_.external_power() == + power_manager::PowerSupplyProperties_ExternalPower_USB; } gfx::ImageSkia PowerStatus::GetBatteryImage(IconSet icon_set) const { @@ -153,17 +175,17 @@ gfx::ImageSkia PowerStatus::GetBatteryImage(IconSet icon_set) const { } // Get the horizontal offset in the battery icon array image. - int offset = (IsUsbChargerConnected() || !status_.line_power_on) ? 0 : 1; + int offset = (IsUsbChargerConnected() || !IsLinePowerConnected()) ? 0 : 1; // Get the icon index in the battery icon array image. int index = -1; - if (status_.battery_percentage >= 100) { + if (GetBatteryPercent() >= 100.0) { index = kNumPowerImages - 1; - } else if (!status_.battery_is_present) { + } else if (!IsBatteryPresent()) { index = kNumPowerImages; } else { index = static_cast<int>( - status_.battery_percentage / 100.0 * (kNumPowerImages - 1)); + GetBatteryPercent() / 100.0 * (kNumPowerImages - 1)); index = std::max(std::min(index, kNumPowerImages - 2), 0); } @@ -175,7 +197,7 @@ gfx::ImageSkia PowerStatus::GetBatteryImage(IconSet icon_set) const { base::string16 PowerStatus::GetAccessibleNameString() const { ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); - if (status_.line_power_on && status_.battery_is_full) { + if (IsLinePowerConnected() && IsBatteryFull()) { return rb.GetLocalizedString( IDS_ASH_STATUS_TRAY_BATTERY_FULL_CHARGE_ACCESSIBLE); } @@ -228,11 +250,16 @@ PowerStatus::~PowerStatus() { RemoveObserver(this); } -void PowerStatus::PowerChanged(const chromeos::PowerSupplyStatus& status) { - status_ = status; - if (status_.battery_is_full) - status_.battery_percentage = 100.0; +void PowerStatus::SetProtoForTesting( + const power_manager::PowerSupplyProperties& proto) { + proto_ = proto; + SanitizeProto(&proto_); +} +void PowerStatus::PowerChanged( + const power_manager::PowerSupplyProperties& proto) { + proto_ = proto; + SanitizeProto(&proto_); FOR_EACH_OBSERVER(Observer, observers_, OnPowerStatusChanged()); } diff --git a/ash/system/chromeos/power/power_status.h b/ash/system/chromeos/power/power_status.h index b479cb6..4eba247 100644 --- a/ash/system/chromeos/power/power_status.h +++ b/ash/system/chromeos/power/power_status.h @@ -10,8 +10,8 @@ #include "base/observer_list.h" #include "base/strings/string16.h" #include "base/time/time.h" +#include "chromeos/dbus/power_manager/power_supply_properties.pb.h" #include "chromeos/dbus/power_manager_client.h" -#include "chromeos/dbus/power_supply_status.h" #include "ui/gfx/image/image_skia.h" namespace ash { @@ -53,10 +53,6 @@ class ASH_EXPORT PowerStatus : public chromeos::PowerManagerClient::Observer { // Gets the global instance. Initialize must be called first. static PowerStatus* Get(); - void set_status_for_testing(const chromeos::PowerSupplyStatus& status) { - status_ = status; - } - // Adds or removes an observer. void AddObserver(Observer* observer); void RemoveObserver(Observer* observer); @@ -105,17 +101,21 @@ class ASH_EXPORT PowerStatus : public chromeos::PowerManagerClient::Observer { // Returns an string describing the current state for accessibility. base::string16 GetAccessibleNameString() const; + // Updates |proto_|. Does not notify observers. + void SetProtoForTesting(const power_manager::PowerSupplyProperties& proto); + protected: PowerStatus(); private: // Overriden from PowerManagerClient::Observer. - virtual void PowerChanged(const chromeos::PowerSupplyStatus& status) OVERRIDE; + virtual void PowerChanged( + const power_manager::PowerSupplyProperties& proto) OVERRIDE; ObserverList<Observer> observers_; // Current state. - chromeos::PowerSupplyStatus status_; + power_manager::PowerSupplyProperties proto_; DISALLOW_COPY_AND_ASSIGN(PowerStatus); }; diff --git a/ash/system/chromeos/power/power_status_unittest.cc b/ash/system/chromeos/power/power_status_unittest.cc index cfb7341..831dd3dad 100644 --- a/ash/system/chromeos/power/power_status_unittest.cc +++ b/ash/system/chromeos/power/power_status_unittest.cc @@ -11,7 +11,6 @@ #include "base/memory/scoped_ptr.h" #include "base/message_loop.h" #include "chromeos/dbus/dbus_thread_manager.h" -#include "chromeos/dbus/power_supply_status.h" #include "testing/gtest/include/gtest/gtest.h" #include "third_party/cros_system_api/dbus/service_constants.h" diff --git a/ash/system/chromeos/power/tray_power_unittest.cc b/ash/system/chromeos/power/tray_power_unittest.cc index a455c39..82bac27 100644 --- a/ash/system/chromeos/power/tray_power_unittest.cc +++ b/ash/system/chromeos/power/tray_power_unittest.cc @@ -7,11 +7,11 @@ #include "ash/ash_switches.h" #include "ash/test/ash_test_base.h" #include "base/memory/scoped_ptr.h" -#include "chromeos/dbus/power_supply_status.h" +#include "chromeos/dbus/power_manager/power_supply_properties.pb.h" #include "ui/message_center/fake_message_center.h" -using chromeos::PowerSupplyStatus; using message_center::Notification; +using power_manager::PowerSupplyProperties; namespace { @@ -69,13 +69,13 @@ class TrayPowerTest : public test::AshTestBase { return tray_power_->notification_state_; } - bool MaybeShowUsbChargerNotification(const PowerSupplyStatus& status) { - PowerStatus::Get()->set_status_for_testing(status); + bool MaybeShowUsbChargerNotification(const PowerSupplyProperties& proto) { + PowerStatus::Get()->SetProtoForTesting(proto); return tray_power_->MaybeShowUsbChargerNotification(); } - bool UpdateNotificationState(const PowerSupplyStatus& status) { - PowerStatus::Get()->set_status_for_testing(status); + bool UpdateNotificationState(const PowerSupplyProperties& proto) { + PowerStatus::Get()->SetProtoForTesting(proto); return tray_power_->UpdateNotificationState(); } @@ -83,18 +83,18 @@ class TrayPowerTest : public test::AshTestBase { tray_power_->usb_charger_was_connected_ = connected; } - // Returns a discharging PowerSupplyStatus more appropriate for testing. - static PowerSupplyStatus DefaultPowerSupplyStatus() { - PowerSupplyStatus status; - status.line_power_on = false; - status.battery_is_present = true; - status.battery_is_full = false; - status.battery_seconds_to_empty = 3 * 60 * 60; - status.battery_seconds_to_full = 2 * 60 * 60; - status.battery_percentage = 50.0; - status.is_calculating_battery_time = false; - status.battery_state = PowerSupplyStatus::DISCHARGING; - return status; + // Returns a discharging PowerSupplyProperties more appropriate for testing. + static PowerSupplyProperties DefaultPowerSupplyProperties() { + PowerSupplyProperties proto; + proto.set_external_power( + power_manager::PowerSupplyProperties_ExternalPower_DISCONNECTED); + proto.set_battery_state( + power_manager::PowerSupplyProperties_BatteryState_DISCHARGING); + proto.set_battery_percent(50.0); + proto.set_battery_time_to_empty_sec(3 * 60 * 60); + proto.set_battery_time_to_full_sec(2 * 60 * 60); + proto.set_is_calculating_battery_time(false); + return proto; } private: @@ -105,25 +105,25 @@ class TrayPowerTest : public test::AshTestBase { }; TEST_F(TrayPowerTest, MaybeShowUsbChargerNotification) { - PowerSupplyStatus discharging = DefaultPowerSupplyStatus(); + PowerSupplyProperties discharging = DefaultPowerSupplyProperties(); EXPECT_FALSE(MaybeShowUsbChargerNotification(discharging)); EXPECT_EQ(0, message_center()->add_count()); EXPECT_EQ(0, message_center()->remove_count()); // Notification shows when connecting a USB charger. - PowerSupplyStatus usb_connected = DefaultPowerSupplyStatus(); - usb_connected.line_power_on = true; - usb_connected.battery_state = PowerSupplyStatus::CONNECTED_TO_USB; + PowerSupplyProperties usb_connected = DefaultPowerSupplyProperties(); + usb_connected.set_external_power( + power_manager::PowerSupplyProperties_ExternalPower_USB); EXPECT_TRUE(MaybeShowUsbChargerNotification(usb_connected)); EXPECT_EQ(1, message_center()->add_count()); EXPECT_EQ(0, message_center()->remove_count()); // Change in charge does not trigger the notification again. - PowerSupplyStatus more_charge = DefaultPowerSupplyStatus(); - more_charge.line_power_on = true; - more_charge.battery_seconds_to_full = 60 * 60; - more_charge.battery_percentage = 75.0; - more_charge.battery_state = PowerSupplyStatus::CONNECTED_TO_USB; + PowerSupplyProperties more_charge = DefaultPowerSupplyProperties(); + more_charge.set_external_power( + power_manager::PowerSupplyProperties_ExternalPower_USB); + more_charge.set_battery_time_to_full_sec(60 * 60); + more_charge.set_battery_percent(75.0); SetUsbChargerConnected(true); EXPECT_FALSE(MaybeShowUsbChargerNotification(more_charge)); EXPECT_EQ(1, message_center()->add_count()); @@ -138,28 +138,33 @@ TEST_F(TrayPowerTest, MaybeShowUsbChargerNotification) { TEST_F(TrayPowerTest, UpdateNotificationState) { // No notifications when no battery present. - PowerSupplyStatus no_battery = DefaultPowerSupplyStatus(); - no_battery.battery_is_present = false; + PowerSupplyProperties no_battery = DefaultPowerSupplyProperties(); + no_battery.set_external_power( + power_manager::PowerSupplyProperties_ExternalPower_AC); + no_battery.set_battery_state( + power_manager::PowerSupplyProperties_BatteryState_NOT_PRESENT); EXPECT_FALSE(UpdateNotificationState(no_battery)); EXPECT_EQ(TrayPower::NOTIFICATION_NONE, notification_state()); // No notification when calculating remaining battery time. - PowerSupplyStatus calculating = DefaultPowerSupplyStatus(); - calculating.is_calculating_battery_time = true; + PowerSupplyProperties calculating = DefaultPowerSupplyProperties(); + calculating.set_is_calculating_battery_time(true); EXPECT_FALSE(UpdateNotificationState(calculating)); EXPECT_EQ(TrayPower::NOTIFICATION_NONE, notification_state()); // No notification when charging. - PowerSupplyStatus charging = DefaultPowerSupplyStatus(); - charging.line_power_on = true; - charging.battery_state = PowerSupplyStatus::CHARGING; + PowerSupplyProperties charging = DefaultPowerSupplyProperties(); + charging.set_external_power( + power_manager::PowerSupplyProperties_ExternalPower_AC); + charging.set_battery_state( + power_manager::PowerSupplyProperties_BatteryState_CHARGING); EXPECT_FALSE(UpdateNotificationState(charging)); EXPECT_EQ(TrayPower::NOTIFICATION_NONE, notification_state()); // Critical low battery notification. - PowerSupplyStatus critical = DefaultPowerSupplyStatus(); - critical.battery_seconds_to_empty = 60; - critical.battery_percentage = 2.0; + PowerSupplyProperties critical = DefaultPowerSupplyProperties(); + critical.set_battery_time_to_empty_sec(60); + critical.set_battery_percent(2.0); EXPECT_TRUE(UpdateNotificationState(critical)); EXPECT_EQ(TrayPower::NOTIFICATION_CRITICAL, notification_state()); } |