Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(295)

Side by Side Diff: content/browser/battery_status/battery_status_manager_chromeos.cc

Issue 356873002: battery-status: Implement the battery-status API for chromeos. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: RefCountedThreadSafe Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/ref_counted.h"
8 #include "base/memory/weak_ptr.h"
timvolodine 2014/07/14 17:38:56 is this still needed?
sadrul 2014/07/14 17:46:31 Nope. Removed.
9 #include "chromeos/dbus/dbus_thread_manager.h"
10 #include "chromeos/dbus/power_manager/power_supply_properties.pb.h"
11 #include "chromeos/dbus/power_manager_client.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "third_party/WebKit/public/platform/WebBatteryStatus.h"
14
15 namespace content {
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. It is safe to call this on any thread.
28 void Start() {
29 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
30 StartOnUI();
31 } else {
32 BrowserThread::PostTask(
33 BrowserThread::UI,
34 FROM_HERE,
35 base::Bind(&PowerManagerObserver::StartOnUI, this));
36 }
37 }
38
39 // Stops listening for updates. It is safe to call this on any thread.
40 void Stop() {
41 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
42 StopOnUI();
43 } else {
44 BrowserThread::PostTask(
45 BrowserThread::UI,
46 FROM_HERE,
47 base::Bind(&PowerManagerObserver::StopOnUI, this));
48 }
49 }
50
51 private:
52 friend class base::RefCountedThreadSafe<PowerManagerObserver>;
53
54 virtual ~PowerManagerObserver() {}
55
56 bool IsBatteryPresent(
57 const power_manager::PowerSupplyProperties& proto) const {
58 return proto.battery_state() !=
59 power_manager::PowerSupplyProperties_BatteryState_NOT_PRESENT;
60 }
61
62 bool IsUsbChargerConnected(
63 const power_manager::PowerSupplyProperties& proto) const {
64 return proto.external_power() ==
65 power_manager::PowerSupplyProperties_ExternalPower_USB;
66 }
67
68 bool IsBatteryCharging(
69 const power_manager::PowerSupplyProperties& proto) const {
70 return proto.battery_state() !=
71 power_manager::PowerSupplyProperties_BatteryState_DISCHARGING;
72 }
73
74 bool IsBatteryFull(const power_manager::PowerSupplyProperties& proto) const {
75 return proto.battery_state() ==
76 power_manager::PowerSupplyProperties_BatteryState_FULL;
77 }
78
79 double GetBatteryLevel(
80 const power_manager::PowerSupplyProperties& proto) const {
81 const double kMaxBatteryLevelProto = 100.f;
82 return proto.battery_percent() / kMaxBatteryLevelProto;
83 }
84
85 void StartOnUI() {
86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
87 if (currently_listening_)
88 return;
89 chromeos::PowerManagerClient* power_client =
90 chromeos::DBusThreadManager::Get()->GetPowerManagerClient();
91 power_client->AddObserver(this);
92 power_client->RequestStatusUpdate();
93 currently_listening_ = true;
94 }
95
96 void StopOnUI() {
97 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
98 if (!currently_listening_)
99 return;
100 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver(
101 this);
102 currently_listening_ = false;
103 }
104
105 // chromeos::PowerManagerClient::Observer:
106 virtual void PowerChanged(
107 const power_manager::PowerSupplyProperties& proto) OVERRIDE {
108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
109 blink::WebBatteryStatus status;
110 // Use the default values if there is no battery in the system.
111 if (IsBatteryPresent(proto)) {
112 // The charging status is unreliable if a low power charger is connected
113 // (i.e. usb).
114 bool status_unreliable = IsUsbChargerConnected(proto);
115 // Battery time is unreliable if it is still being computed.
116 bool time_unreliable =
117 status_unreliable || proto.is_calculating_battery_time();
118
119 // Set |charging| only if the status is reliable. Otherwise, keep the
120 // default (which is |true|).
121 if (!status_unreliable)
122 status.charging = IsBatteryCharging(proto);
123
124 // Set |chargingTime| to +infinity if the battery is discharging, or if
125 // the time is unreliable. Keep the default value (which is 0) if the
126 // battery is full.
127 if (time_unreliable || !status.charging)
128 status.chargingTime = std::numeric_limits<double>::infinity();
129 else if (!IsBatteryFull(proto))
130 status.chargingTime = proto.battery_time_to_full_sec();
131
132 // Keep the default value for |dischargingTime| (which is +infinity) if
133 // the time is unreliable, or if the battery is charging.
134 if (!time_unreliable && !status.charging)
135 status.dischargingTime = proto.battery_time_to_empty_sec();
136
137 status.level = GetBatteryLevel(proto);
138 }
139 callback_.Run(status);
140 }
141
142 BatteryStatusService::BatteryUpdateCallback callback_;
143 bool currently_listening_;
144
145 DISALLOW_COPY_AND_ASSIGN(PowerManagerObserver);
146 };
147
148 class BatteryStatusManagerChromeOS
149 : public BatteryStatusManager,
150 public chromeos::PowerManagerClient::Observer {
151 public:
152 explicit BatteryStatusManagerChromeOS(
153 const BatteryStatusService::BatteryUpdateCallback& callback)
154 : observer_(new PowerManagerObserver(callback)) {}
155
156 virtual ~BatteryStatusManagerChromeOS() { observer_->Stop(); }
157
158 private:
159 // BatteryStatusManager:
160 virtual bool StartListeningBatteryChange() OVERRIDE {
161 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
162 observer_->Start();
163 return true;
164 }
165
166 virtual void StopListeningBatteryChange() OVERRIDE {
167 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
168 observer_->Stop();
169 }
170
171 scoped_refptr<PowerManagerObserver> observer_;
172
173 DISALLOW_COPY_AND_ASSIGN(BatteryStatusManagerChromeOS);
174 };
175
176 } // namespace
177
178 // static
179 scoped_ptr<BatteryStatusManager> BatteryStatusManager::Create(
180 const BatteryStatusService::BatteryUpdateCallback& callback) {
181 return scoped_ptr<BatteryStatusManager>(
182 new BatteryStatusManagerChromeOS(callback));
183 }
184
185 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698