Index: content/browser/battery_status/battery_status_manager_chromeos.cc |
diff --git a/content/browser/battery_status/battery_status_manager_chromeos.cc b/content/browser/battery_status/battery_status_manager_chromeos.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..86d3aa8997ed7b47d1a98da2a03fe20a7b6a8992 |
--- /dev/null |
+++ b/content/browser/battery_status/battery_status_manager_chromeos.cc |
@@ -0,0 +1,167 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "content/browser/battery_status/battery_status_manager.h" |
+ |
+#include "base/memory/weak_ptr.h" |
+#include "chromeos/dbus/dbus_thread_manager.h" |
+#include "chromeos/dbus/power_manager/power_supply_properties.pb.h" |
+#include "chromeos/dbus/power_manager_client.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "third_party/WebKit/public/platform/WebBatteryStatus.h" |
+ |
+namespace content { |
+ |
+namespace { |
+ |
+void RemoveObserverOnUI(chromeos::PowerManagerClient::Observer* observer) { |
+ chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver( |
+ observer); |
+} |
+ |
+class BatteryStatusManagerChromeOS |
+ : public BatteryStatusManager, |
+ public chromeos::PowerManagerClient::Observer { |
+ public: |
+ explicit BatteryStatusManagerChromeOS( |
+ const BatteryStatusService::BatteryUpdateCallback& callback) |
+ : callback_(callback), currently_listening_(false), weak_factory_(this) {} |
+ |
+ virtual ~BatteryStatusManagerChromeOS() { |
+ if (!currently_listening_) |
timvolodine
2014/07/09 11:35:01
This looks like there is a potential race conditio
sadrul
2014/07/13 09:04:54
I have moved the main code out of this into a RefC
|
+ return; |
+ |
+ if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
+ StopOnUI(); |
+ } else { |
+ BrowserThread::PostTask( |
+ BrowserThread::UI, |
+ FROM_HERE, |
+ base::Bind(&RemoveObserverOnUI, base::Unretained(this))); |
+ } |
+ } |
+ |
+ private: |
+ bool IsBatteryPresent( |
+ const power_manager::PowerSupplyProperties& proto) const { |
+ return proto.battery_state() != |
+ power_manager::PowerSupplyProperties_BatteryState_NOT_PRESENT; |
+ } |
+ |
+ bool IsUsbChargerConnected( |
+ const power_manager::PowerSupplyProperties& proto) const { |
+ return proto.external_power() == |
+ power_manager::PowerSupplyProperties_ExternalPower_USB; |
+ } |
+ |
+ bool IsBatteryCharging( |
+ const power_manager::PowerSupplyProperties& proto) const { |
+ return proto.battery_state() != |
+ power_manager::PowerSupplyProperties_BatteryState_DISCHARGING; |
+ } |
+ |
+ bool IsBatteryFull(const power_manager::PowerSupplyProperties& proto) const { |
+ return proto.battery_state() == |
+ power_manager::PowerSupplyProperties_BatteryState_FULL; |
+ } |
+ |
+ double GetBatteryLevel( |
+ const power_manager::PowerSupplyProperties& proto) const { |
+ const double kMaxBatteryLevelProto = 100.f; |
+ return proto.battery_percent() / kMaxBatteryLevelProto; |
+ } |
+ |
+ void StartOnUI() { |
timvolodine
2014/07/09 11:35:01
not sure if this is relevant for chromeOS, but alt
sadrul
2014/07/13 09:04:54
In the case of Chrome OS, the interaction with the
timvolodine
2014/07/14 17:38:56
fyi: on macOS it is sufficient to have a thread wi
|
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+ if (currently_listening_) |
+ return; |
+ chromeos::PowerManagerClient* power_client = |
+ chromeos::DBusThreadManager::Get()->GetPowerManagerClient(); |
+ power_client->AddObserver(this); |
+ power_client->RequestStatusUpdate(); |
+ currently_listening_ = true; |
+ } |
+ |
+ void StopOnUI() { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+ if (!currently_listening_) |
+ return; |
+ chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver( |
+ this); |
+ currently_listening_ = false; |
+ } |
+ |
+ // BatteryStatusManager: |
+ virtual bool StartListeningBatteryChange() OVERRIDE { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ weak_factory_.InvalidateWeakPtrs(); |
+ BrowserThread::PostTask(BrowserThread::UI, |
+ FROM_HERE, |
+ base::Bind(&BatteryStatusManagerChromeOS::StartOnUI, |
+ weak_factory_.GetWeakPtr())); |
timvolodine
2014/07/09 11:35:01
Probably false alarm, but I was curious whether We
sadrul
2014/07/13 09:04:54
Good catch! It looks like WeakPtr indeed is not th
|
+ return true; |
+ } |
+ |
+ virtual void StopListeningBatteryChange() OVERRIDE { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ weak_factory_.InvalidateWeakPtrs(); |
+ BrowserThread::PostTask(BrowserThread::UI, |
+ FROM_HERE, |
+ base::Bind(&BatteryStatusManagerChromeOS::StopOnUI, |
+ weak_factory_.GetWeakPtr())); |
+ } |
+ |
+ // chromeos::PowerManagerClient::Observer: |
+ virtual void PowerChanged( |
+ const power_manager::PowerSupplyProperties& proto) OVERRIDE { |
+ blink::WebBatteryStatus status; |
+ // Use the default values if there is no battery in the system. |
+ if (IsBatteryPresent(proto)) { |
+ // The charging status is unreliable if a low power charger is connected |
+ // (i.e. usb). |
+ bool status_unreliable = IsUsbChargerConnected(proto); |
+ // Battery time is unreliable if it is still being computed. |
+ bool time_unreliable = |
+ status_unreliable || proto.is_calculating_battery_time(); |
+ |
+ // Set |charging| only if the status is reliable. Otherwise, keep the |
+ // default (which is |true|). |
+ if (!status_unreliable) |
+ status.charging = IsBatteryCharging(proto); |
+ |
+ // Set |chargingTime| to +infinity if the battery is discharging, or if |
+ // the time is unreliable. Keep the default value (which is 0) if the |
+ // battery is full. |
+ if (time_unreliable || !status.charging) |
+ status.chargingTime = std::numeric_limits<double>::infinity(); |
+ else if (!IsBatteryFull(proto)) |
+ status.chargingTime = proto.battery_time_to_full_sec(); |
+ |
+ // Keep the default value for |dischargingTime| (which is +infinity) if |
+ // the time is unreliable, or if the battery is charging. |
+ if (!time_unreliable && !status.charging) |
+ status.dischargingTime = proto.battery_time_to_empty_sec(); |
+ |
+ status.level = GetBatteryLevel(proto); |
+ } |
+ callback_.Run(status); |
+ } |
+ |
+ BatteryStatusService::BatteryUpdateCallback callback_; |
+ bool currently_listening_; |
+ base::WeakPtrFactory<BatteryStatusManagerChromeOS> weak_factory_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(BatteryStatusManagerChromeOS); |
+}; |
+ |
+} // namespace |
+ |
+// static |
+scoped_ptr<BatteryStatusManager> BatteryStatusManager::Create( |
+ const BatteryStatusService::BatteryUpdateCallback& callback) { |
+ return scoped_ptr<BatteryStatusManager>( |
+ new BatteryStatusManagerChromeOS(callback)); |
+} |
+ |
+} // namespace content |