bluetooth_battery_client.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright 2020 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. #include "device/bluetooth/dbus/bluetooth_battery_client.h"
  5. #include "base/bind.h"
  6. #include "base/logging.h"
  7. #include "base/memory/ptr_util.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/observer_list.h"
  10. #include "dbus/bus.h"
  11. #include "dbus/message.h"
  12. #include "dbus/object_manager.h"
  13. #include "dbus/object_proxy.h"
  14. #include "third_party/cros_system_api/dbus/service_constants.h"
  15. namespace bluez {
  16. BluetoothBatteryClient::Properties::Properties(
  17. dbus::ObjectProxy* object_proxy,
  18. const std::string& interface_name,
  19. const PropertyChangedCallback& callback)
  20. : dbus::PropertySet(object_proxy, interface_name, callback) {
  21. RegisterProperty(bluetooth_battery::kPercentageProperty, &percentage);
  22. }
  23. BluetoothBatteryClient::Properties::~Properties() = default;
  24. // The BluetoothBatteryClient implementation used in production.
  25. class BluetoothBatteryClientImpl : public BluetoothBatteryClient,
  26. public dbus::ObjectManager::Interface {
  27. public:
  28. BluetoothBatteryClientImpl() = default;
  29. BluetoothBatteryClientImpl(const BluetoothBatteryClientImpl&) = delete;
  30. BluetoothBatteryClientImpl& operator=(const BluetoothBatteryClientImpl&) =
  31. delete;
  32. ~BluetoothBatteryClientImpl() override {
  33. // There is an instance of this client that is created but not initialized
  34. // on Linux. See 'Alternate D-Bus Client' note in bluez_dbus_manager.h.
  35. if (object_manager_) {
  36. object_manager_->UnregisterInterface(
  37. bluetooth_adapter::kBluetoothAdapterInterface);
  38. }
  39. }
  40. // BluetoothBatteryClient override.
  41. void AddObserver(BluetoothBatteryClient::Observer* observer) override {
  42. DCHECK(observer);
  43. observers_.AddObserver(observer);
  44. }
  45. // BluetoothBatteryClient override.
  46. void RemoveObserver(BluetoothBatteryClient::Observer* observer) override {
  47. DCHECK(observer);
  48. observers_.RemoveObserver(observer);
  49. }
  50. // dbus::ObjectManager::Interface override.
  51. dbus::PropertySet* CreateProperties(
  52. dbus::ObjectProxy* object_proxy,
  53. const dbus::ObjectPath& object_path,
  54. const std::string& interface_name) override {
  55. return new Properties(
  56. object_proxy, interface_name,
  57. base::BindRepeating(&BluetoothBatteryClientImpl::OnPropertyChanged,
  58. weak_ptr_factory_.GetWeakPtr(), object_path));
  59. }
  60. // BluetoothBatteryClient override.
  61. Properties* GetProperties(const dbus::ObjectPath& object_path) override {
  62. return static_cast<Properties*>(object_manager_->GetProperties(
  63. object_path, bluetooth_battery::kBluetoothBatteryInterface));
  64. }
  65. protected:
  66. void Init(dbus::Bus* bus,
  67. const std::string& bluetooth_service_name) override {
  68. object_manager_ = bus->GetObjectManager(
  69. bluetooth_service_name,
  70. dbus::ObjectPath(
  71. bluetooth_object_manager::kBluetoothObjectManagerServicePath));
  72. object_manager_->RegisterInterface(
  73. bluetooth_battery::kBluetoothBatteryInterface, this);
  74. }
  75. private:
  76. // Called by dbus::ObjectManager when an object with the battery interface
  77. // is created. Informs observers.
  78. void ObjectAdded(const dbus::ObjectPath& object_path,
  79. const std::string& interface_name) override {
  80. for (auto& observer : observers_)
  81. observer.BatteryAdded(object_path);
  82. }
  83. // Called by dbus::ObjectManager when an object with the battery interface
  84. // is removed. Informs observers.
  85. void ObjectRemoved(const dbus::ObjectPath& object_path,
  86. const std::string& interface_name) override {
  87. for (auto& observer : observers_)
  88. observer.BatteryRemoved(object_path);
  89. }
  90. // Called by BluetoothPropertySet when a property value is changed,
  91. // either by result of a signal or response to a GetAll() or Get()
  92. // call. Informs observers.
  93. void OnPropertyChanged(const dbus::ObjectPath& object_path,
  94. const std::string& property_name) {
  95. for (auto& observer : observers_)
  96. observer.BatteryPropertyChanged(object_path, property_name);
  97. }
  98. raw_ptr<dbus::ObjectManager> object_manager_ = nullptr;
  99. // List of observers interested in event notifications from us.
  100. base::ObserverList<BluetoothBatteryClient::Observer>::Unchecked observers_;
  101. // Weak pointer factory for generating 'this' pointers that might live longer
  102. // than we do.
  103. // Note: This should remain the last member so it'll be destroyed and
  104. // invalidate its weak pointers before any other members are destroyed.
  105. base::WeakPtrFactory<BluetoothBatteryClientImpl> weak_ptr_factory_{this};
  106. };
  107. BluetoothBatteryClient::BluetoothBatteryClient() = default;
  108. BluetoothBatteryClient::~BluetoothBatteryClient() = default;
  109. BluetoothBatteryClient* BluetoothBatteryClient::Create() {
  110. return new BluetoothBatteryClientImpl();
  111. }
  112. } // namespace bluez