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 "content/browser/battery_status/battery_status_manager.h" | |
6 | |
7 #include "base/memory/weak_ptr.h" | |
8 #include "chromeos/dbus/dbus_thread_manager.h" | |
9 #include "chromeos/dbus/power_manager/power_supply_properties.pb.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 | |
14 namespace content { | |
15 | |
16 namespace { | |
17 | |
18 class BatteryStatusManagerChromeOS | |
19 : public BatteryStatusManager, | |
20 public chromeos::PowerManagerClient::Observer { | |
21 public: | |
22 explicit BatteryStatusManagerChromeOS( | |
23 const BatteryStatusService::BatteryUpdateCallback& callback) | |
24 : callback_(callback), currently_listening_(false), weak_factory_(this) { | |
25 } | |
26 | |
27 virtual ~BatteryStatusManagerChromeOS() { | |
28 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
| |
29 } | |
30 | |
31 private: | |
32 bool IsBatteryPresent( | |
33 const power_manager::PowerSupplyProperties& proto) const { | |
34 return proto.battery_state() != | |
35 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
| |
36 } | |
37 | |
38 bool IsUsbChargerConnected( | |
39 const power_manager::PowerSupplyProperties& proto) const { | |
40 return proto.external_power() == | |
41 power_manager::PowerSupplyProperties_ExternalPower_USB; | |
42 } | |
43 | |
44 bool IsBatteryCharging( | |
45 const power_manager::PowerSupplyProperties& proto) const { | |
46 return proto.battery_state() != | |
47 power_manager::PowerSupplyProperties_BatteryState_DISCHARGING; | |
48 } | |
49 | |
50 bool IsBatteryFull(const power_manager::PowerSupplyProperties& proto) const { | |
51 return proto.battery_state() == | |
52 power_manager::PowerSupplyProperties_BatteryState_FULL; | |
53 } | |
54 | |
55 double GetBatteryLevel( | |
56 const power_manager::PowerSupplyProperties& proto) const { | |
57 const double kMaxBatteryLevelProto = 100.f; | |
58 return proto.battery_percent() / kMaxBatteryLevelProto; | |
59 } | |
60 | |
61 void StartOnUI() { | |
62 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
63 if (!currently_listening_) { | |
timvolodine
2014/07/02 19:41:39
nit: early return as in Stop?
sadrul
2014/07/04 16:58:18
Done.
| |
64 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver( | |
65 this); | |
66 chromeos::DBusThreadManager::Get() | |
67 ->GetPowerManagerClient() | |
68 ->RequestStatusUpdate(); | |
69 } | |
70 currently_listening_ = true; | |
71 } | |
72 | |
73 void StopOnUI() { | |
74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
75 if (!currently_listening_) | |
76 return; | |
77 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver( | |
78 this); | |
79 currently_listening_ = false; | |
80 } | |
81 | |
82 // BatteryStatusManager: | |
83 virtual bool StartListeningBatteryChange() OVERRIDE { | |
84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
85 weak_factory_.InvalidateWeakPtrs(); | |
86 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
87 base::Bind(&BatteryStatusManagerChromeOS::StartOnUI, | |
88 weak_factory_.GetWeakPtr())); | |
89 return true; | |
90 } | |
91 | |
92 virtual void StopListeningBatteryChange() OVERRIDE { | |
93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
94 weak_factory_.InvalidateWeakPtrs(); | |
95 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
96 base::Bind(&BatteryStatusManagerChromeOS::StopOnUI, | |
97 weak_factory_.GetWeakPtr())); | |
98 } | |
99 | |
100 // chromeos::PowerManagerClient::Observer: | |
101 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..
| |
102 const power_manager::PowerSupplyProperties& proto) OVERRIDE { | |
103 blink::WebBatteryStatus status; | |
104 if (IsBatteryPresent(proto)) { | |
105 // The charging status is unreliable if a low power charger is connected | |
106 // (i.e. usb). So set |chargingTime| and |dischargingTime| only when usb | |
107 // charger is not connected. | |
108 if (!IsUsbChargerConnected(proto)) { | |
109 status.charging = IsBatteryCharging(proto); | |
110 | |
111 // 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.
| |
112 // being computed (and not reliable). | |
113 if (!proto.is_calculating_battery_time()) { | |
114 // Set |chargingTime| if the battery is charging, and it isn't full. | |
115 if (status.charging && !IsBatteryFull(proto)) | |
116 status.chargingTime = proto.battery_time_to_full_sec(); | |
117 | |
118 // Set |dischargingTime| if the battery is not charging. | |
119 if (!status.charging) | |
120 status.dischargingTime = proto.battery_time_to_empty_sec(); | |
121 } | |
122 } | |
123 | |
124 status.level = GetBatteryLevel(proto); | |
125 } | |
126 callback_.Run(status); | |
127 } | |
128 | |
129 BatteryStatusService::BatteryUpdateCallback callback_; | |
130 bool currently_listening_; | |
131 base::WeakPtrFactory<BatteryStatusManagerChromeOS> weak_factory_; | |
132 | |
133 DISALLOW_COPY_AND_ASSIGN(BatteryStatusManagerChromeOS); | |
134 }; | |
135 | |
136 } // namespace | |
137 | |
138 // static | |
139 BatteryStatusManager* BatteryStatusManager::Create( | |
140 const BatteryStatusService::BatteryUpdateCallback& callback) { | |
141 return new BatteryStatusManagerChromeOS(callback); | |
142 } | |
143 | |
144 } // namespace content | |
OLD | NEW |