Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / device / battery / battery_status_manager_linux.cc
blob0b2da63d0f1c1f769ef0c45288a0a6112525ea94
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.
5 #include "device/battery/battery_status_manager_linux.h"
7 #include "base/macros.h"
8 #include "base/metrics/histogram.h"
9 #include "base/threading/thread.h"
10 #include "base/values.h"
11 #include "dbus/bus.h"
12 #include "dbus/message.h"
13 #include "dbus/object_path.h"
14 #include "dbus/object_proxy.h"
15 #include "dbus/property.h"
16 #include "dbus/values_util.h"
17 #include "device/battery/battery_status_manager.h"
19 namespace device {
21 namespace {
23 const char kUPowerServiceName[] = "org.freedesktop.UPower";
24 const char kUPowerDeviceName[] = "org.freedesktop.UPower.Device";
25 const char kUPowerPath[] = "/org/freedesktop/UPower";
26 const char kUPowerDeviceSignalChanged[] = "Changed";
27 const char kUPowerEnumerateDevices[] = "EnumerateDevices";
28 const char kBatteryNotifierThreadName[] = "BatteryStatusNotifier";
30 // UPowerDeviceType reflects the possible UPower.Device.Type values,
31 // see upower.freedesktop.org/docs/Device.html#Device:Type.
32 enum UPowerDeviceType {
33 UPOWER_DEVICE_TYPE_UNKNOWN = 0,
34 UPOWER_DEVICE_TYPE_LINE_POWER = 1,
35 UPOWER_DEVICE_TYPE_BATTERY = 2,
36 UPOWER_DEVICE_TYPE_UPS = 3,
37 UPOWER_DEVICE_TYPE_MONITOR = 4,
38 UPOWER_DEVICE_TYPE_MOUSE = 5,
39 UPOWER_DEVICE_TYPE_KEYBOARD = 6,
40 UPOWER_DEVICE_TYPE_PDA = 7,
41 UPOWER_DEVICE_TYPE_PHONE = 8,
44 typedef std::vector<dbus::ObjectPath> PathsVector;
46 double GetPropertyAsDouble(const base::DictionaryValue& dictionary,
47 const std::string& property_name,
48 double default_value) {
49 double value = default_value;
50 return dictionary.GetDouble(property_name, &value) ? value : default_value;
53 bool GetPropertyAsBoolean(const base::DictionaryValue& dictionary,
54 const std::string& property_name,
55 bool default_value) {
56 bool value = default_value;
57 return dictionary.GetBoolean(property_name, &value) ? value : default_value;
60 scoped_ptr<base::DictionaryValue> GetPropertiesAsDictionary(
61 dbus::ObjectProxy* proxy) {
62 dbus::MethodCall method_call(dbus::kPropertiesInterface,
63 dbus::kPropertiesGetAll);
64 dbus::MessageWriter builder(&method_call);
65 builder.AppendString(kUPowerDeviceName);
67 scoped_ptr<dbus::Response> response(
68 proxy->CallMethodAndBlock(&method_call,
69 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT));
70 if (response) {
71 dbus::MessageReader reader(response.get());
72 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader));
73 base::DictionaryValue* dictionary_value = NULL;
74 if (value && value->GetAsDictionary(&dictionary_value)) {
75 ignore_result(value.release());
76 return scoped_ptr<base::DictionaryValue>(dictionary_value);
79 return scoped_ptr<base::DictionaryValue>();
82 scoped_ptr<PathsVector> GetPowerSourcesPaths(dbus::ObjectProxy* proxy) {
83 scoped_ptr<PathsVector> paths(new PathsVector());
84 if (!proxy)
85 return paths.Pass();
87 dbus::MethodCall method_call(kUPowerServiceName, kUPowerEnumerateDevices);
88 scoped_ptr<dbus::Response> response(
89 proxy->CallMethodAndBlock(&method_call,
90 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT));
92 if (response) {
93 dbus::MessageReader reader(response.get());
94 reader.PopArrayOfObjectPaths(paths.get());
96 return paths.Pass();;
99 void UpdateNumberBatteriesHistogram(int count) {
100 UMA_HISTOGRAM_CUSTOM_COUNTS(
101 "BatteryStatus.NumberBatteriesLinux", count, 1, 5, 6);
104 // Class that represents a dedicated thread which communicates with DBus to
105 // obtain battery information and receives battery change notifications.
106 class BatteryStatusNotificationThread : public base::Thread {
107 public:
108 BatteryStatusNotificationThread(
109 const BatteryStatusService::BatteryUpdateCallback& callback)
110 : base::Thread(kBatteryNotifierThreadName),
111 callback_(callback),
112 battery_proxy_(NULL) {}
114 ~BatteryStatusNotificationThread() override {
115 // Make sure to shutdown the dbus connection if it is still open in the very
116 // end. It needs to happen on the BatteryStatusNotificationThread.
117 message_loop()->PostTask(
118 FROM_HERE,
119 base::Bind(&BatteryStatusNotificationThread::ShutdownDBusConnection,
120 base::Unretained(this)));
122 // Drain the message queue of the BatteryStatusNotificationThread and stop.
123 Stop();
126 void StartListening() {
127 DCHECK(OnWatcherThread());
129 if (system_bus_.get())
130 return;
132 InitDBus();
133 dbus::ObjectProxy* power_proxy =
134 system_bus_->GetObjectProxy(kUPowerServiceName,
135 dbus::ObjectPath(kUPowerPath));
136 scoped_ptr<PathsVector> device_paths = GetPowerSourcesPaths(power_proxy);
137 int num_batteries = 0;
139 for (size_t i = 0; i < device_paths->size(); ++i) {
140 const dbus::ObjectPath& device_path = device_paths->at(i);
141 dbus::ObjectProxy* device_proxy = system_bus_->GetObjectProxy(
142 kUPowerServiceName, device_path);
143 scoped_ptr<base::DictionaryValue> dictionary =
144 GetPropertiesAsDictionary(device_proxy);
146 if (!dictionary)
147 continue;
149 bool is_present = GetPropertyAsBoolean(*dictionary, "IsPresent", false);
150 uint32 type = static_cast<uint32>(
151 GetPropertyAsDouble(*dictionary, "Type", UPOWER_DEVICE_TYPE_UNKNOWN));
153 if (!is_present || type != UPOWER_DEVICE_TYPE_BATTERY) {
154 system_bus_->RemoveObjectProxy(kUPowerServiceName,
155 device_path,
156 base::Bind(&base::DoNothing));
157 continue;
160 if (battery_proxy_) {
161 // TODO(timvolodine): add support for multiple batteries. Currently we
162 // only collect information from the first battery we encounter
163 // (crbug.com/400780).
164 LOG(WARNING) << "multiple batteries found, "
165 << "using status data of the first battery only.";
166 } else {
167 battery_proxy_ = device_proxy;
169 num_batteries++;
172 UpdateNumberBatteriesHistogram(num_batteries);
174 if (!battery_proxy_) {
175 callback_.Run(BatteryStatus());
176 return;
179 battery_proxy_->ConnectToSignal(
180 kUPowerDeviceName,
181 kUPowerDeviceSignalChanged,
182 base::Bind(&BatteryStatusNotificationThread::BatteryChanged,
183 base::Unretained(this)),
184 base::Bind(&BatteryStatusNotificationThread::OnSignalConnected,
185 base::Unretained(this)));
188 void StopListening() {
189 DCHECK(OnWatcherThread());
190 ShutdownDBusConnection();
193 private:
194 bool OnWatcherThread() {
195 return task_runner()->BelongsToCurrentThread();
198 void InitDBus() {
199 DCHECK(OnWatcherThread());
201 dbus::Bus::Options options;
202 options.bus_type = dbus::Bus::SYSTEM;
203 options.connection_type = dbus::Bus::PRIVATE;
204 system_bus_ = new dbus::Bus(options);
207 void ShutdownDBusConnection() {
208 DCHECK(OnWatcherThread());
210 if (!system_bus_.get())
211 return;
213 // Shutdown DBus connection later because there may be pending tasks on
214 // this thread.
215 message_loop()->PostTask(FROM_HERE,
216 base::Bind(&dbus::Bus::ShutdownAndBlock,
217 system_bus_));
218 system_bus_ = NULL;
219 battery_proxy_ = NULL;
222 void OnSignalConnected(const std::string& interface_name,
223 const std::string& signal_name,
224 bool success) {
225 DCHECK(OnWatcherThread());
227 if (interface_name != kUPowerDeviceName ||
228 signal_name != kUPowerDeviceSignalChanged) {
229 return;
232 if (!system_bus_.get())
233 return;
235 if (success) {
236 BatteryChanged(NULL);
237 } else {
238 // Failed to register for "Changed" signal, execute callback with the
239 // default values.
240 callback_.Run(BatteryStatus());
244 void BatteryChanged(dbus::Signal* signal /* unsused */) {
245 DCHECK(OnWatcherThread());
247 if (!system_bus_.get())
248 return;
250 scoped_ptr<base::DictionaryValue> dictionary =
251 GetPropertiesAsDictionary(battery_proxy_);
252 if (dictionary)
253 callback_.Run(ComputeWebBatteryStatus(*dictionary));
254 else
255 callback_.Run(BatteryStatus());
258 BatteryStatusService::BatteryUpdateCallback callback_;
259 scoped_refptr<dbus::Bus> system_bus_;
260 dbus::ObjectProxy* battery_proxy_; // owned by the bus
262 DISALLOW_COPY_AND_ASSIGN(BatteryStatusNotificationThread);
265 // Creates a notification thread and delegates Start/Stop calls to it.
266 class BatteryStatusManagerLinux : public BatteryStatusManager {
267 public:
268 explicit BatteryStatusManagerLinux(
269 const BatteryStatusService::BatteryUpdateCallback& callback)
270 : callback_(callback) {}
272 ~BatteryStatusManagerLinux() override {}
274 private:
275 // BatteryStatusManager:
276 bool StartListeningBatteryChange() override {
277 if (!StartNotifierThreadIfNecessary())
278 return false;
280 notifier_thread_->message_loop()->PostTask(
281 FROM_HERE,
282 base::Bind(&BatteryStatusNotificationThread::StartListening,
283 base::Unretained(notifier_thread_.get())));
284 return true;
287 void StopListeningBatteryChange() override {
288 if (!notifier_thread_)
289 return;
291 notifier_thread_->message_loop()->PostTask(
292 FROM_HERE,
293 base::Bind(&BatteryStatusNotificationThread::StopListening,
294 base::Unretained(notifier_thread_.get())));
297 // Starts the notifier thread if not already started and returns true on
298 // success.
299 bool StartNotifierThreadIfNecessary() {
300 if (notifier_thread_)
301 return true;
303 base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0);
304 notifier_thread_.reset(new BatteryStatusNotificationThread(callback_));
305 if (!notifier_thread_->StartWithOptions(thread_options)) {
306 notifier_thread_.reset();
307 LOG(ERROR) << "Could not start the " << kBatteryNotifierThreadName
308 << " thread";
309 return false;
311 return true;
314 BatteryStatusService::BatteryUpdateCallback callback_;
315 scoped_ptr<BatteryStatusNotificationThread> notifier_thread_;
317 DISALLOW_COPY_AND_ASSIGN(BatteryStatusManagerLinux);
320 } // namespace
322 BatteryStatus ComputeWebBatteryStatus(const base::DictionaryValue& dictionary) {
323 BatteryStatus status;
324 if (!dictionary.HasKey("State"))
325 return status;
327 uint32 state = static_cast<uint32>(
328 GetPropertyAsDouble(dictionary, "State", UPOWER_DEVICE_STATE_UNKNOWN));
329 status.charging = state != UPOWER_DEVICE_STATE_DISCHARGING &&
330 state != UPOWER_DEVICE_STATE_EMPTY;
331 double percentage = GetPropertyAsDouble(dictionary, "Percentage", 100);
332 // Convert percentage to a value between 0 and 1 with 2 digits of precision.
333 // This is to bring it in line with other platforms like Mac and Android where
334 // we report level with 1% granularity. It also serves the purpose of reducing
335 // the possibility of fingerprinting and triggers less level change events on
336 // the blink side.
337 // TODO(timvolodine): consider moving this rounding to the blink side.
338 status.level = round(percentage) / 100.f;
340 switch (state) {
341 case UPOWER_DEVICE_STATE_CHARGING : {
342 double time_to_full = GetPropertyAsDouble(dictionary, "TimeToFull", 0);
343 status.charging_time =
344 (time_to_full > 0) ? time_to_full
345 : std::numeric_limits<double>::infinity();
346 break;
348 case UPOWER_DEVICE_STATE_DISCHARGING : {
349 double time_to_empty = GetPropertyAsDouble(dictionary, "TimeToEmpty", 0);
350 // Set dischargingTime if it's available. Otherwise leave the default
351 // value which is +infinity.
352 if (time_to_empty > 0)
353 status.discharging_time = time_to_empty;
354 status.charging_time = std::numeric_limits<double>::infinity();
355 break;
357 case UPOWER_DEVICE_STATE_FULL : {
358 break;
360 default: {
361 status.charging_time = std::numeric_limits<double>::infinity();
364 return status;
367 // static
368 scoped_ptr<BatteryStatusManager> BatteryStatusManager::Create(
369 const BatteryStatusService::BatteryUpdateCallback& callback) {
370 return scoped_ptr<BatteryStatusManager>(
371 new BatteryStatusManagerLinux(callback));
374 } // namespace device