OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "device/battery/battery_status_manager_linux.h" | 5 #include "device/battery/battery_status_manager_linux.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
11 | 11 |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
| 14 #include "base/single_thread_task_runner.h" |
14 #include "base/threading/thread.h" | 15 #include "base/threading/thread.h" |
15 #include "base/values.h" | 16 #include "base/values.h" |
16 #include "dbus/bus.h" | 17 #include "dbus/bus.h" |
17 #include "dbus/message.h" | 18 #include "dbus/message.h" |
18 #include "dbus/object_path.h" | 19 #include "dbus/object_path.h" |
19 #include "dbus/object_proxy.h" | 20 #include "dbus/object_proxy.h" |
20 #include "dbus/property.h" | 21 #include "dbus/property.h" |
21 #include "dbus/values_util.h" | 22 #include "dbus/values_util.h" |
22 #include "device/battery/battery_status_manager.h" | 23 #include "device/battery/battery_status_manager.h" |
23 | 24 |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 public: | 111 public: |
111 BatteryStatusNotificationThread( | 112 BatteryStatusNotificationThread( |
112 const BatteryStatusService::BatteryUpdateCallback& callback) | 113 const BatteryStatusService::BatteryUpdateCallback& callback) |
113 : base::Thread(kBatteryNotifierThreadName), | 114 : base::Thread(kBatteryNotifierThreadName), |
114 callback_(callback), | 115 callback_(callback), |
115 battery_proxy_(NULL) {} | 116 battery_proxy_(NULL) {} |
116 | 117 |
117 ~BatteryStatusNotificationThread() override { | 118 ~BatteryStatusNotificationThread() override { |
118 // Make sure to shutdown the dbus connection if it is still open in the very | 119 // Make sure to shutdown the dbus connection if it is still open in the very |
119 // end. It needs to happen on the BatteryStatusNotificationThread. | 120 // end. It needs to happen on the BatteryStatusNotificationThread. |
120 message_loop()->PostTask( | 121 message_loop()->task_runner()->PostTask( |
121 FROM_HERE, | 122 FROM_HERE, |
122 base::Bind(&BatteryStatusNotificationThread::ShutdownDBusConnection, | 123 base::Bind(&BatteryStatusNotificationThread::ShutdownDBusConnection, |
123 base::Unretained(this))); | 124 base::Unretained(this))); |
124 | 125 |
125 // Drain the message queue of the BatteryStatusNotificationThread and stop. | 126 // Drain the message queue of the BatteryStatusNotificationThread and stop. |
126 Stop(); | 127 Stop(); |
127 } | 128 } |
128 | 129 |
129 void StartListening() { | 130 void StartListening() { |
130 DCHECK(OnWatcherThread()); | 131 DCHECK(OnWatcherThread()); |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 } | 210 } |
210 | 211 |
211 void ShutdownDBusConnection() { | 212 void ShutdownDBusConnection() { |
212 DCHECK(OnWatcherThread()); | 213 DCHECK(OnWatcherThread()); |
213 | 214 |
214 if (!system_bus_.get()) | 215 if (!system_bus_.get()) |
215 return; | 216 return; |
216 | 217 |
217 // Shutdown DBus connection later because there may be pending tasks on | 218 // Shutdown DBus connection later because there may be pending tasks on |
218 // this thread. | 219 // this thread. |
219 message_loop()->PostTask(FROM_HERE, | 220 message_loop()->task_runner()->PostTask( |
220 base::Bind(&dbus::Bus::ShutdownAndBlock, | 221 FROM_HERE, base::Bind(&dbus::Bus::ShutdownAndBlock, system_bus_)); |
221 system_bus_)); | |
222 system_bus_ = NULL; | 222 system_bus_ = NULL; |
223 battery_proxy_ = NULL; | 223 battery_proxy_ = NULL; |
224 } | 224 } |
225 | 225 |
226 void OnSignalConnected(const std::string& interface_name, | 226 void OnSignalConnected(const std::string& interface_name, |
227 const std::string& signal_name, | 227 const std::string& signal_name, |
228 bool success) { | 228 bool success) { |
229 DCHECK(OnWatcherThread()); | 229 DCHECK(OnWatcherThread()); |
230 | 230 |
231 if (interface_name != kUPowerDeviceName || | 231 if (interface_name != kUPowerDeviceName || |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 : callback_(callback) {} | 274 : callback_(callback) {} |
275 | 275 |
276 ~BatteryStatusManagerLinux() override {} | 276 ~BatteryStatusManagerLinux() override {} |
277 | 277 |
278 private: | 278 private: |
279 // BatteryStatusManager: | 279 // BatteryStatusManager: |
280 bool StartListeningBatteryChange() override { | 280 bool StartListeningBatteryChange() override { |
281 if (!StartNotifierThreadIfNecessary()) | 281 if (!StartNotifierThreadIfNecessary()) |
282 return false; | 282 return false; |
283 | 283 |
284 notifier_thread_->message_loop()->PostTask( | 284 notifier_thread_->task_runner()->PostTask( |
285 FROM_HERE, | 285 FROM_HERE, base::Bind(&BatteryStatusNotificationThread::StartListening, |
286 base::Bind(&BatteryStatusNotificationThread::StartListening, | 286 base::Unretained(notifier_thread_.get()))); |
287 base::Unretained(notifier_thread_.get()))); | |
288 return true; | 287 return true; |
289 } | 288 } |
290 | 289 |
291 void StopListeningBatteryChange() override { | 290 void StopListeningBatteryChange() override { |
292 if (!notifier_thread_) | 291 if (!notifier_thread_) |
293 return; | 292 return; |
294 | 293 |
295 notifier_thread_->message_loop()->PostTask( | 294 notifier_thread_->task_runner()->PostTask( |
296 FROM_HERE, | 295 FROM_HERE, base::Bind(&BatteryStatusNotificationThread::StopListening, |
297 base::Bind(&BatteryStatusNotificationThread::StopListening, | 296 base::Unretained(notifier_thread_.get()))); |
298 base::Unretained(notifier_thread_.get()))); | |
299 } | 297 } |
300 | 298 |
301 // Starts the notifier thread if not already started and returns true on | 299 // Starts the notifier thread if not already started and returns true on |
302 // success. | 300 // success. |
303 bool StartNotifierThreadIfNecessary() { | 301 bool StartNotifierThreadIfNecessary() { |
304 if (notifier_thread_) | 302 if (notifier_thread_) |
305 return true; | 303 return true; |
306 | 304 |
307 base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0); | 305 base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0); |
308 notifier_thread_.reset(new BatteryStatusNotificationThread(callback_)); | 306 notifier_thread_.reset(new BatteryStatusNotificationThread(callback_)); |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
369 } | 367 } |
370 | 368 |
371 // static | 369 // static |
372 std::unique_ptr<BatteryStatusManager> BatteryStatusManager::Create( | 370 std::unique_ptr<BatteryStatusManager> BatteryStatusManager::Create( |
373 const BatteryStatusService::BatteryUpdateCallback& callback) { | 371 const BatteryStatusService::BatteryUpdateCallback& callback) { |
374 return std::unique_ptr<BatteryStatusManager>( | 372 return std::unique_ptr<BatteryStatusManager>( |
375 new BatteryStatusManagerLinux(callback)); | 373 new BatteryStatusManagerLinux(callback)); |
376 } | 374 } |
377 | 375 |
378 } // namespace device | 376 } // namespace device |
OLD | NEW |