| 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 <limits> | 10 #include <limits> |
| 11 #include <memory> | 11 #include <memory> |
| 12 #include <string> | 12 #include <string> |
| 13 #include <utility> | 13 #include <utility> |
| 14 #include <vector> | 14 #include <vector> |
| 15 | 15 |
| 16 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/metrics/histogram.h" | 17 #include "base/metrics/histogram.h" |
| 18 #include "base/single_thread_task_runner.h" |
| 18 #include "base/threading/thread.h" | 19 #include "base/threading/thread.h" |
| 19 #include "base/values.h" | 20 #include "base/values.h" |
| 20 #include "base/version.h" | 21 #include "base/version.h" |
| 21 #include "dbus/bus.h" | 22 #include "dbus/bus.h" |
| 22 #include "dbus/message.h" | 23 #include "dbus/message.h" |
| 23 #include "dbus/object_path.h" | 24 #include "dbus/object_path.h" |
| 24 #include "dbus/object_proxy.h" | 25 #include "dbus/object_proxy.h" |
| 25 #include "dbus/property.h" | 26 #include "dbus/property.h" |
| 26 #include "dbus/values_util.h" | 27 #include "dbus/values_util.h" |
| 27 #include "device/battery/battery_status_manager_linux-inl.h" | 28 #include "device/battery/battery_status_manager_linux-inl.h" |
| (...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 308 class BatteryStatusManagerLinux::BatteryStatusNotificationThread | 309 class BatteryStatusManagerLinux::BatteryStatusNotificationThread |
| 309 : public base::Thread { | 310 : public base::Thread { |
| 310 public: | 311 public: |
| 311 BatteryStatusNotificationThread( | 312 BatteryStatusNotificationThread( |
| 312 const BatteryStatusService::BatteryUpdateCallback& callback) | 313 const BatteryStatusService::BatteryUpdateCallback& callback) |
| 313 : base::Thread(kBatteryNotifierThreadName), callback_(callback) {} | 314 : base::Thread(kBatteryNotifierThreadName), callback_(callback) {} |
| 314 | 315 |
| 315 ~BatteryStatusNotificationThread() override { | 316 ~BatteryStatusNotificationThread() override { |
| 316 // Make sure to shutdown the dbus connection if it is still open in the very | 317 // Make sure to shutdown the dbus connection if it is still open in the very |
| 317 // end. It needs to happen on the BatteryStatusNotificationThread. | 318 // end. It needs to happen on the BatteryStatusNotificationThread. |
| 318 message_loop()->PostTask( | 319 message_loop()->task_runner()->PostTask( |
| 319 FROM_HERE, | 320 FROM_HERE, |
| 320 base::Bind(&BatteryStatusNotificationThread::ShutdownDBusConnection, | 321 base::Bind(&BatteryStatusNotificationThread::ShutdownDBusConnection, |
| 321 base::Unretained(this))); | 322 base::Unretained(this))); |
| 322 | 323 |
| 323 // Drain the message queue of the BatteryStatusNotificationThread and stop. | 324 // Drain the message queue of the BatteryStatusNotificationThread and stop. |
| 324 Stop(); | 325 Stop(); |
| 325 } | 326 } |
| 326 | 327 |
| 327 void StartListening() { | 328 void StartListening() { |
| 328 DCHECK(OnWatcherThread()); | 329 DCHECK(OnWatcherThread()); |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 457 DCHECK(OnWatcherThread()); | 458 DCHECK(OnWatcherThread()); |
| 458 | 459 |
| 459 if (!system_bus_.get()) | 460 if (!system_bus_.get()) |
| 460 return; | 461 return; |
| 461 | 462 |
| 462 battery_.reset(); // before the system_bus_ is shut down. | 463 battery_.reset(); // before the system_bus_ is shut down. |
| 463 upower_.reset(); | 464 upower_.reset(); |
| 464 | 465 |
| 465 // Shutdown DBus connection later because there may be pending tasks on | 466 // Shutdown DBus connection later because there may be pending tasks on |
| 466 // this thread. | 467 // this thread. |
| 467 message_loop()->PostTask(FROM_HERE, | 468 message_loop()->task_runner()->PostTask( |
| 468 base::Bind(&dbus::Bus::ShutdownAndBlock, | 469 FROM_HERE, base::Bind(&dbus::Bus::ShutdownAndBlock, system_bus_)); |
| 469 system_bus_)); | |
| 470 system_bus_ = NULL; | 470 system_bus_ = NULL; |
| 471 } | 471 } |
| 472 | 472 |
| 473 void OnSignalConnected(const std::string& interface_name, | 473 void OnSignalConnected(const std::string& interface_name, |
| 474 const std::string& signal_name, | 474 const std::string& signal_name, |
| 475 bool success) {} | 475 bool success) {} |
| 476 | 476 |
| 477 std::unique_ptr<BatteryObject> CreateBattery( | 477 std::unique_ptr<BatteryObject> CreateBattery( |
| 478 const dbus::ObjectPath& device_path) { | 478 const dbus::ObjectPath& device_path) { |
| 479 std::unique_ptr<BatteryObject> battery(new BatteryObject( | 479 std::unique_ptr<BatteryObject> battery(new BatteryObject( |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 562 BatteryStatusManagerLinux::BatteryStatusManagerLinux( | 562 BatteryStatusManagerLinux::BatteryStatusManagerLinux( |
| 563 const BatteryStatusService::BatteryUpdateCallback& callback) | 563 const BatteryStatusService::BatteryUpdateCallback& callback) |
| 564 : callback_(callback) {} | 564 : callback_(callback) {} |
| 565 | 565 |
| 566 BatteryStatusManagerLinux::~BatteryStatusManagerLinux() {} | 566 BatteryStatusManagerLinux::~BatteryStatusManagerLinux() {} |
| 567 | 567 |
| 568 bool BatteryStatusManagerLinux::StartListeningBatteryChange() { | 568 bool BatteryStatusManagerLinux::StartListeningBatteryChange() { |
| 569 if (!StartNotifierThreadIfNecessary()) | 569 if (!StartNotifierThreadIfNecessary()) |
| 570 return false; | 570 return false; |
| 571 | 571 |
| 572 notifier_thread_->message_loop()->PostTask( | 572 notifier_thread_->task_runner()->PostTask( |
| 573 FROM_HERE, base::Bind(&BatteryStatusNotificationThread::StartListening, | 573 FROM_HERE, base::Bind(&BatteryStatusNotificationThread::StartListening, |
| 574 base::Unretained(notifier_thread_.get()))); | 574 base::Unretained(notifier_thread_.get()))); |
| 575 return true; | 575 return true; |
| 576 } | 576 } |
| 577 | 577 |
| 578 void BatteryStatusManagerLinux::StopListeningBatteryChange() { | 578 void BatteryStatusManagerLinux::StopListeningBatteryChange() { |
| 579 if (!notifier_thread_) | 579 if (!notifier_thread_) |
| 580 return; | 580 return; |
| 581 | 581 |
| 582 notifier_thread_->message_loop()->PostTask( | 582 notifier_thread_->task_runner()->PostTask( |
| 583 FROM_HERE, base::Bind(&BatteryStatusNotificationThread::StopListening, | 583 FROM_HERE, base::Bind(&BatteryStatusNotificationThread::StopListening, |
| 584 base::Unretained(notifier_thread_.get()))); | 584 base::Unretained(notifier_thread_.get()))); |
| 585 } | 585 } |
| 586 | 586 |
| 587 bool BatteryStatusManagerLinux::StartNotifierThreadIfNecessary() { | 587 bool BatteryStatusManagerLinux::StartNotifierThreadIfNecessary() { |
| 588 if (notifier_thread_) | 588 if (notifier_thread_) |
| 589 return true; | 589 return true; |
| 590 | 590 |
| 591 base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0); | 591 base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0); |
| 592 notifier_thread_.reset(new BatteryStatusNotificationThread(callback_)); | 592 notifier_thread_.reset(new BatteryStatusNotificationThread(callback_)); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 618 } | 618 } |
| 619 | 619 |
| 620 // static | 620 // static |
| 621 std::unique_ptr<BatteryStatusManager> BatteryStatusManager::Create( | 621 std::unique_ptr<BatteryStatusManager> BatteryStatusManager::Create( |
| 622 const BatteryStatusService::BatteryUpdateCallback& callback) { | 622 const BatteryStatusService::BatteryUpdateCallback& callback) { |
| 623 return std::unique_ptr<BatteryStatusManager>( | 623 return std::unique_ptr<BatteryStatusManager>( |
| 624 new BatteryStatusManagerLinux(callback)); | 624 new BatteryStatusManagerLinux(callback)); |
| 625 } | 625 } |
| 626 | 626 |
| 627 } // namespace device | 627 } // namespace device |
| OLD | NEW |