| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "device/battery/battery_status_manager.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 | |
| 9 #include "base/macros.h" | |
| 10 #include "base/memory/ref_counted.h" | |
| 11 #include "chromeos/dbus/dbus_thread_manager.h" | |
| 12 #include "chromeos/dbus/power_manager/power_supply_properties.pb.h" | |
| 13 #include "chromeos/dbus/power_manager_client.h" | |
| 14 | |
| 15 namespace device { | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 class PowerManagerObserver | |
| 20 : public chromeos::PowerManagerClient::Observer, | |
| 21 public base::RefCountedThreadSafe<PowerManagerObserver> { | |
| 22 public: | |
| 23 explicit PowerManagerObserver( | |
| 24 const BatteryStatusService::BatteryUpdateCallback& callback) | |
| 25 : callback_(callback), currently_listening_(false) {} | |
| 26 | |
| 27 // Starts listening for updates. | |
| 28 void Start() { | |
| 29 if (currently_listening_) | |
| 30 return; | |
| 31 chromeos::PowerManagerClient* power_client = | |
| 32 chromeos::DBusThreadManager::Get()->GetPowerManagerClient(); | |
| 33 power_client->AddObserver(this); | |
| 34 power_client->RequestStatusUpdate(); | |
| 35 currently_listening_ = true; | |
| 36 } | |
| 37 | |
| 38 // Stops listening for updates. | |
| 39 void Stop() { | |
| 40 if (!currently_listening_) | |
| 41 return; | |
| 42 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver( | |
| 43 this); | |
| 44 currently_listening_ = false; | |
| 45 } | |
| 46 | |
| 47 private: | |
| 48 friend class base::RefCountedThreadSafe<PowerManagerObserver>; | |
| 49 | |
| 50 ~PowerManagerObserver() override {} | |
| 51 | |
| 52 bool IsBatteryPresent( | |
| 53 const power_manager::PowerSupplyProperties& proto) const { | |
| 54 return proto.battery_state() != | |
| 55 power_manager::PowerSupplyProperties_BatteryState_NOT_PRESENT; | |
| 56 } | |
| 57 | |
| 58 bool IsUsbChargerConnected( | |
| 59 const power_manager::PowerSupplyProperties& proto) const { | |
| 60 return proto.external_power() == | |
| 61 power_manager::PowerSupplyProperties_ExternalPower_USB; | |
| 62 } | |
| 63 | |
| 64 bool IsBatteryCharging( | |
| 65 const power_manager::PowerSupplyProperties& proto) const { | |
| 66 return proto.battery_state() != | |
| 67 power_manager::PowerSupplyProperties_BatteryState_DISCHARGING; | |
| 68 } | |
| 69 | |
| 70 bool IsBatteryFull(const power_manager::PowerSupplyProperties& proto) const { | |
| 71 return proto.battery_state() == | |
| 72 power_manager::PowerSupplyProperties_BatteryState_FULL; | |
| 73 } | |
| 74 | |
| 75 double GetBatteryLevel( | |
| 76 const power_manager::PowerSupplyProperties& proto) const { | |
| 77 const double kMaxBatteryLevelProto = 100.f; | |
| 78 return proto.battery_percent() / kMaxBatteryLevelProto; | |
| 79 } | |
| 80 | |
| 81 // chromeos::PowerManagerClient::Observer: | |
| 82 void PowerChanged( | |
| 83 const power_manager::PowerSupplyProperties& proto) override { | |
| 84 mojom::BatteryStatus status; | |
| 85 | |
| 86 // Use the default values if there is no battery in the system. | |
| 87 if (IsBatteryPresent(proto)) { | |
| 88 // The charging status is unreliable if a low power charger is connected | |
| 89 // (i.e. usb). | |
| 90 bool status_unreliable = IsUsbChargerConnected(proto); | |
| 91 // Battery time is unreliable if it is still being computed. | |
| 92 bool time_unreliable = | |
| 93 status_unreliable || proto.is_calculating_battery_time(); | |
| 94 | |
| 95 // Set |charging| only if the status is reliable. Otherwise, keep the | |
| 96 // default (which is |true|). | |
| 97 if (!status_unreliable) | |
| 98 status.charging = IsBatteryCharging(proto); | |
| 99 | |
| 100 // Set |chargingTime| to +infinity if the battery is discharging, or if | |
| 101 // the time is unreliable. Keep the default value (which is 0) if the | |
| 102 // battery is full. | |
| 103 if (time_unreliable || !status.charging) | |
| 104 status.charging_time = std::numeric_limits<double>::infinity(); | |
| 105 else if (!IsBatteryFull(proto)) | |
| 106 status.charging_time = proto.battery_time_to_full_sec(); | |
| 107 | |
| 108 // Keep the default value for |dischargingTime| (which is +infinity) if | |
| 109 // the time is unreliable, or if the battery is charging. | |
| 110 if (!time_unreliable && !status.charging) | |
| 111 status.discharging_time = proto.battery_time_to_empty_sec(); | |
| 112 | |
| 113 status.level = GetBatteryLevel(proto); | |
| 114 } | |
| 115 callback_.Run(status); | |
| 116 } | |
| 117 | |
| 118 BatteryStatusService::BatteryUpdateCallback callback_; | |
| 119 bool currently_listening_; | |
| 120 | |
| 121 DISALLOW_COPY_AND_ASSIGN(PowerManagerObserver); | |
| 122 }; | |
| 123 | |
| 124 class BatteryStatusManagerChromeOS | |
| 125 : public BatteryStatusManager, | |
| 126 public chromeos::PowerManagerClient::Observer { | |
| 127 public: | |
| 128 explicit BatteryStatusManagerChromeOS( | |
| 129 const BatteryStatusService::BatteryUpdateCallback& callback) | |
| 130 : observer_(new PowerManagerObserver(callback)) {} | |
| 131 | |
| 132 ~BatteryStatusManagerChromeOS() override { observer_->Stop(); } | |
| 133 | |
| 134 private: | |
| 135 // BatteryStatusManager: | |
| 136 bool StartListeningBatteryChange() override { | |
| 137 observer_->Start(); | |
| 138 return true; | |
| 139 } | |
| 140 | |
| 141 void StopListeningBatteryChange() override { observer_->Stop(); } | |
| 142 | |
| 143 scoped_refptr<PowerManagerObserver> observer_; | |
| 144 | |
| 145 DISALLOW_COPY_AND_ASSIGN(BatteryStatusManagerChromeOS); | |
| 146 }; | |
| 147 | |
| 148 } // namespace | |
| 149 | |
| 150 // static | |
| 151 std::unique_ptr<BatteryStatusManager> BatteryStatusManager::Create( | |
| 152 const BatteryStatusService::BatteryUpdateCallback& callback) { | |
| 153 return std::unique_ptr<BatteryStatusManager>( | |
| 154 new BatteryStatusManagerChromeOS(callback)); | |
| 155 } | |
| 156 | |
| 157 } // namespace device | |
| OLD | NEW |