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..6244db9ae7e1b70e16ac5acb0359d54a746437af |
--- /dev/null |
+++ b/content/browser/battery_status/battery_status_manager_chromeos.cc |
@@ -0,0 +1,144 @@ |
+// 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 { |
+ |
+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() { |
+ StopOnUI(); |
timvolodine
2014/07/02 19:41:39
nit: so the destructor should be called on the UI
sadrul
2014/07/04 16:58:18
I have changed this so the BatteryStatusManager ob
|
+ } |
+ |
+ private: |
+ bool IsBatteryPresent( |
+ const power_manager::PowerSupplyProperties& proto) const { |
+ return proto.battery_state() != |
+ power_manager::PowerSupplyProperties_BatteryState_NOT_PRESENT; |
timvolodine
2014/07/02 19:41:39
nit: indent +4 spaces as in the method below. this
sadrul
2014/07/04 16:58:18
This is how 'git cl format' formats it. Do you fee
timvolodine
2014/07/09 11:35:01
I don't feel strongly about those indents as long
|
+ } |
+ |
+ 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() { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+ if (!currently_listening_) { |
timvolodine
2014/07/02 19:41:39
nit: early return as in Stop?
sadrul
2014/07/04 16:58:18
Done.
|
+ chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver( |
+ this); |
+ chromeos::DBusThreadManager::Get() |
+ ->GetPowerManagerClient() |
+ ->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())); |
+ 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( |
timvolodine
2014/07/02 19:41:39
just out of curiosity: is this usually called when
sadrul
2014/07/04 16:58:18
From a quick test: this can trigger more often tha
timvolodine
2014/07/09 11:35:01
yeah, looks like this depends on device/system..
|
+ const power_manager::PowerSupplyProperties& proto) OVERRIDE { |
+ blink::WebBatteryStatus status; |
+ if (IsBatteryPresent(proto)) { |
+ // The charging status is unreliable if a low power charger is connected |
+ // (i.e. usb). So set |chargingTime| and |dischargingTime| only when usb |
+ // charger is not connected. |
+ if (!IsUsbChargerConnected(proto)) { |
+ status.charging = IsBatteryCharging(proto); |
+ |
+ // Do not set |chargingTime| and |dischargingTime| if the time is still |
timvolodine
2014/07/02 19:41:39
If the chargingTime cannot be provided (maybe temp
sadrul
2014/07/04 16:58:18
Done.
|
+ // being computed (and not reliable). |
+ if (!proto.is_calculating_battery_time()) { |
+ // Set |chargingTime| if the battery is charging, and it isn't full. |
+ if (status.charging && !IsBatteryFull(proto)) |
+ status.chargingTime = proto.battery_time_to_full_sec(); |
+ |
+ // Set |dischargingTime| if the battery is not charging. |
+ if (!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 |
+BatteryStatusManager* BatteryStatusManager::Create( |
+ const BatteryStatusService::BatteryUpdateCallback& callback) { |
+ return new BatteryStatusManagerChromeOS(callback); |
+} |
+ |
+} // namespace content |