| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "content/browser/battery_status/battery_status_manager.h" | 5 #include "device/battery/battery_status_manager.h" |
| 6 | 6 |
| 7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
| 8 #include "chromeos/dbus/dbus_thread_manager.h" | 8 #include "chromeos/dbus/dbus_thread_manager.h" |
| 9 #include "chromeos/dbus/power_manager/power_supply_properties.pb.h" | 9 #include "chromeos/dbus/power_manager/power_supply_properties.pb.h" |
| 10 #include "chromeos/dbus/power_manager_client.h" | 10 #include "chromeos/dbus/power_manager_client.h" |
| 11 #include "content/public/browser/browser_thread.h" | |
| 12 #include "third_party/WebKit/public/platform/WebBatteryStatus.h" | |
| 13 | 11 |
| 14 namespace content { | 12 namespace device { |
| 15 | 13 |
| 16 namespace { | 14 namespace { |
| 17 | 15 |
| 18 class PowerManagerObserver | 16 class PowerManagerObserver |
| 19 : public chromeos::PowerManagerClient::Observer, | 17 : public chromeos::PowerManagerClient::Observer, |
| 20 public base::RefCountedThreadSafe<PowerManagerObserver> { | 18 public base::RefCountedThreadSafe<PowerManagerObserver> { |
| 21 public: | 19 public: |
| 22 explicit PowerManagerObserver( | 20 explicit PowerManagerObserver( |
| 23 const BatteryStatusService::BatteryUpdateCallback& callback) | 21 const BatteryStatusService::BatteryUpdateCallback& callback) |
| 24 : callback_(callback), currently_listening_(false) {} | 22 : callback_(callback), currently_listening_(false) {} |
| 25 | 23 |
| 26 // Starts listening for updates. It is safe to call this on any thread. | 24 // Starts listening for updates. |
| 27 void Start() { | 25 void Start() { |
| 28 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 26 if (currently_listening_) |
| 29 StartOnUI(); | 27 return; |
| 30 } else { | 28 chromeos::PowerManagerClient* power_client = |
| 31 BrowserThread::PostTask( | 29 chromeos::DBusThreadManager::Get()->GetPowerManagerClient(); |
| 32 BrowserThread::UI, | 30 power_client->AddObserver(this); |
| 33 FROM_HERE, | 31 power_client->RequestStatusUpdate(); |
| 34 base::Bind(&PowerManagerObserver::StartOnUI, this)); | 32 currently_listening_ = true; |
| 35 } | |
| 36 } | 33 } |
| 37 | 34 |
| 38 // Stops listening for updates. It is safe to call this on any thread. | 35 // Stops listening for updates. |
| 39 void Stop() { | 36 void Stop() { |
| 40 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 37 if (!currently_listening_) |
| 41 StopOnUI(); | 38 return; |
| 42 } else { | 39 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver( |
| 43 BrowserThread::PostTask( | 40 this); |
| 44 BrowserThread::UI, | 41 currently_listening_ = false; |
| 45 FROM_HERE, | |
| 46 base::Bind(&PowerManagerObserver::StopOnUI, this)); | |
| 47 } | |
| 48 } | 42 } |
| 49 | 43 |
| 50 private: | 44 private: |
| 51 friend class base::RefCountedThreadSafe<PowerManagerObserver>; | 45 friend class base::RefCountedThreadSafe<PowerManagerObserver>; |
| 52 | 46 |
| 53 virtual ~PowerManagerObserver() {} | 47 virtual ~PowerManagerObserver() {} |
| 54 | 48 |
| 55 bool IsBatteryPresent( | 49 bool IsBatteryPresent( |
| 56 const power_manager::PowerSupplyProperties& proto) const { | 50 const power_manager::PowerSupplyProperties& proto) const { |
| 57 return proto.battery_state() != | 51 return proto.battery_state() != |
| (...skipping 16 matching lines...) Expand all Loading... |
| 74 return proto.battery_state() == | 68 return proto.battery_state() == |
| 75 power_manager::PowerSupplyProperties_BatteryState_FULL; | 69 power_manager::PowerSupplyProperties_BatteryState_FULL; |
| 76 } | 70 } |
| 77 | 71 |
| 78 double GetBatteryLevel( | 72 double GetBatteryLevel( |
| 79 const power_manager::PowerSupplyProperties& proto) const { | 73 const power_manager::PowerSupplyProperties& proto) const { |
| 80 const double kMaxBatteryLevelProto = 100.f; | 74 const double kMaxBatteryLevelProto = 100.f; |
| 81 return proto.battery_percent() / kMaxBatteryLevelProto; | 75 return proto.battery_percent() / kMaxBatteryLevelProto; |
| 82 } | 76 } |
| 83 | 77 |
| 84 void StartOnUI() { | |
| 85 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 86 if (currently_listening_) | |
| 87 return; | |
| 88 chromeos::PowerManagerClient* power_client = | |
| 89 chromeos::DBusThreadManager::Get()->GetPowerManagerClient(); | |
| 90 power_client->AddObserver(this); | |
| 91 power_client->RequestStatusUpdate(); | |
| 92 currently_listening_ = true; | |
| 93 } | |
| 94 | |
| 95 void StopOnUI() { | |
| 96 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 97 if (!currently_listening_) | |
| 98 return; | |
| 99 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver( | |
| 100 this); | |
| 101 currently_listening_ = false; | |
| 102 } | |
| 103 | |
| 104 // chromeos::PowerManagerClient::Observer: | 78 // chromeos::PowerManagerClient::Observer: |
| 105 virtual void PowerChanged( | 79 virtual void PowerChanged( |
| 106 const power_manager::PowerSupplyProperties& proto) override { | 80 const power_manager::PowerSupplyProperties& proto) override { |
| 107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 81 BatteryStatus status; |
| 108 blink::WebBatteryStatus status; | 82 |
| 109 // Use the default values if there is no battery in the system. | 83 // Use the default values if there is no battery in the system. |
| 110 if (IsBatteryPresent(proto)) { | 84 if (IsBatteryPresent(proto)) { |
| 111 // The charging status is unreliable if a low power charger is connected | 85 // The charging status is unreliable if a low power charger is connected |
| 112 // (i.e. usb). | 86 // (i.e. usb). |
| 113 bool status_unreliable = IsUsbChargerConnected(proto); | 87 bool status_unreliable = IsUsbChargerConnected(proto); |
| 114 // Battery time is unreliable if it is still being computed. | 88 // Battery time is unreliable if it is still being computed. |
| 115 bool time_unreliable = | 89 bool time_unreliable = |
| 116 status_unreliable || proto.is_calculating_battery_time(); | 90 status_unreliable || proto.is_calculating_battery_time(); |
| 117 | 91 |
| 118 // Set |charging| only if the status is reliable. Otherwise, keep the | 92 // Set |charging| only if the status is reliable. Otherwise, keep the |
| 119 // default (which is |true|). | 93 // default (which is |true|). |
| 120 if (!status_unreliable) | 94 if (!status_unreliable) |
| 121 status.charging = IsBatteryCharging(proto); | 95 status.charging = IsBatteryCharging(proto); |
| 122 | 96 |
| 123 // Set |chargingTime| to +infinity if the battery is discharging, or if | 97 // Set |chargingTime| to +infinity if the battery is discharging, or if |
| 124 // the time is unreliable. Keep the default value (which is 0) if the | 98 // the time is unreliable. Keep the default value (which is 0) if the |
| 125 // battery is full. | 99 // battery is full. |
| 126 if (time_unreliable || !status.charging) | 100 if (time_unreliable || !status.charging) |
| 127 status.chargingTime = std::numeric_limits<double>::infinity(); | 101 status.charging_time = std::numeric_limits<double>::infinity(); |
| 128 else if (!IsBatteryFull(proto)) | 102 else if (!IsBatteryFull(proto)) |
| 129 status.chargingTime = proto.battery_time_to_full_sec(); | 103 status.charging_time = proto.battery_time_to_full_sec(); |
| 130 | 104 |
| 131 // Keep the default value for |dischargingTime| (which is +infinity) if | 105 // Keep the default value for |dischargingTime| (which is +infinity) if |
| 132 // the time is unreliable, or if the battery is charging. | 106 // the time is unreliable, or if the battery is charging. |
| 133 if (!time_unreliable && !status.charging) | 107 if (!time_unreliable && !status.charging) |
| 134 status.dischargingTime = proto.battery_time_to_empty_sec(); | 108 status.discharging_time = proto.battery_time_to_empty_sec(); |
| 135 | 109 |
| 136 status.level = GetBatteryLevel(proto); | 110 status.level = GetBatteryLevel(proto); |
| 137 } | 111 } |
| 138 callback_.Run(status); | 112 callback_.Run(status); |
| 139 } | 113 } |
| 140 | 114 |
| 141 BatteryStatusService::BatteryUpdateCallback callback_; | 115 BatteryStatusService::BatteryUpdateCallback callback_; |
| 142 bool currently_listening_; | 116 bool currently_listening_; |
| 143 | 117 |
| 144 DISALLOW_COPY_AND_ASSIGN(PowerManagerObserver); | 118 DISALLOW_COPY_AND_ASSIGN(PowerManagerObserver); |
| 145 }; | 119 }; |
| 146 | 120 |
| 147 class BatteryStatusManagerChromeOS | 121 class BatteryStatusManagerChromeOS |
| 148 : public BatteryStatusManager, | 122 : public BatteryStatusManager, |
| 149 public chromeos::PowerManagerClient::Observer { | 123 public chromeos::PowerManagerClient::Observer { |
| 150 public: | 124 public: |
| 151 explicit BatteryStatusManagerChromeOS( | 125 explicit BatteryStatusManagerChromeOS( |
| 152 const BatteryStatusService::BatteryUpdateCallback& callback) | 126 const BatteryStatusService::BatteryUpdateCallback& callback) |
| 153 : observer_(new PowerManagerObserver(callback)) {} | 127 : observer_(new PowerManagerObserver(callback)) {} |
| 154 | 128 |
| 155 virtual ~BatteryStatusManagerChromeOS() { observer_->Stop(); } | 129 virtual ~BatteryStatusManagerChromeOS() { observer_->Stop(); } |
| 156 | 130 |
| 157 private: | 131 private: |
| 158 // BatteryStatusManager: | 132 // BatteryStatusManager: |
| 159 virtual bool StartListeningBatteryChange() override { | 133 virtual bool StartListeningBatteryChange() override { |
| 160 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 161 observer_->Start(); | 134 observer_->Start(); |
| 162 return true; | 135 return true; |
| 163 } | 136 } |
| 164 | 137 |
| 165 virtual void StopListeningBatteryChange() override { | 138 virtual void StopListeningBatteryChange() override { |
| 166 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 167 observer_->Stop(); | 139 observer_->Stop(); |
| 168 } | 140 } |
| 169 | 141 |
| 170 scoped_refptr<PowerManagerObserver> observer_; | 142 scoped_refptr<PowerManagerObserver> observer_; |
| 171 | 143 |
| 172 DISALLOW_COPY_AND_ASSIGN(BatteryStatusManagerChromeOS); | 144 DISALLOW_COPY_AND_ASSIGN(BatteryStatusManagerChromeOS); |
| 173 }; | 145 }; |
| 174 | 146 |
| 175 } // namespace | 147 } // namespace |
| 176 | 148 |
| 177 // static | 149 // static |
| 178 scoped_ptr<BatteryStatusManager> BatteryStatusManager::Create( | 150 scoped_ptr<BatteryStatusManager> BatteryStatusManager::Create( |
| 179 const BatteryStatusService::BatteryUpdateCallback& callback) { | 151 const BatteryStatusService::BatteryUpdateCallback& callback) { |
| 180 return scoped_ptr<BatteryStatusManager>( | 152 return scoped_ptr<BatteryStatusManager>( |
| 181 new BatteryStatusManagerChromeOS(callback)); | 153 new BatteryStatusManagerChromeOS(callback)); |
| 182 } | 154 } |
| 183 | 155 |
| 184 } // namespace content | 156 } // namespace device |
| OLD | NEW |