bluetooth_input_client.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright 2013 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_input_client.h"
  5. #include <map>
  6. #include "base/bind.h"
  7. #include "base/check.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. BluetoothInputClient::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_input::kReconnectModeProperty, &reconnect_mode);
  22. }
  23. BluetoothInputClient::Properties::~Properties() = default;
  24. // The BluetoothInputClient implementation used in production.
  25. class BluetoothInputClientImpl : public BluetoothInputClient,
  26. public dbus::ObjectManager::Interface {
  27. public:
  28. BluetoothInputClientImpl() : object_manager_(nullptr) {}
  29. BluetoothInputClientImpl(const BluetoothInputClientImpl&) = delete;
  30. BluetoothInputClientImpl& operator=(const BluetoothInputClientImpl&) = delete;
  31. ~BluetoothInputClientImpl() override {
  32. object_manager_->UnregisterInterface(
  33. bluetooth_input::kBluetoothInputInterface);
  34. }
  35. // BluetoothInputClient override.
  36. void AddObserver(BluetoothInputClient::Observer* observer) override {
  37. DCHECK(observer);
  38. observers_.AddObserver(observer);
  39. }
  40. // BluetoothInputClient override.
  41. void RemoveObserver(BluetoothInputClient::Observer* observer) override {
  42. DCHECK(observer);
  43. observers_.RemoveObserver(observer);
  44. }
  45. // dbus::ObjectManager::Interface override.
  46. dbus::PropertySet* CreateProperties(
  47. dbus::ObjectProxy* object_proxy,
  48. const dbus::ObjectPath& object_path,
  49. const std::string& interface_name) override {
  50. return new Properties(
  51. object_proxy, interface_name,
  52. base::BindRepeating(&BluetoothInputClientImpl::OnPropertyChanged,
  53. weak_ptr_factory_.GetWeakPtr(), object_path));
  54. }
  55. // BluetoothInputClient override.
  56. Properties* GetProperties(const dbus::ObjectPath& object_path) override {
  57. return static_cast<Properties*>(object_manager_->GetProperties(
  58. object_path, bluetooth_input::kBluetoothInputInterface));
  59. }
  60. protected:
  61. void Init(dbus::Bus* bus,
  62. const std::string& bluetooth_service_name) override {
  63. object_manager_ = bus->GetObjectManager(
  64. bluetooth_service_name,
  65. dbus::ObjectPath(
  66. bluetooth_object_manager::kBluetoothObjectManagerServicePath));
  67. object_manager_->RegisterInterface(
  68. bluetooth_input::kBluetoothInputInterface, this);
  69. }
  70. private:
  71. // Called by dbus::ObjectManager when an object with the input interface
  72. // is created. Informs observers.
  73. void ObjectAdded(const dbus::ObjectPath& object_path,
  74. const std::string& interface_name) override {
  75. for (auto& observer : observers_)
  76. observer.InputAdded(object_path);
  77. }
  78. // Called by dbus::ObjectManager when an object with the input interface
  79. // is removed. Informs observers.
  80. void ObjectRemoved(const dbus::ObjectPath& object_path,
  81. const std::string& interface_name) override {
  82. for (auto& observer : observers_)
  83. observer.InputRemoved(object_path);
  84. }
  85. // Called by BluetoothPropertySet when a property value is changed,
  86. // either by result of a signal or response to a GetAll() or Get()
  87. // call. Informs observers.
  88. void OnPropertyChanged(const dbus::ObjectPath& object_path,
  89. const std::string& property_name) {
  90. for (auto& observer : observers_)
  91. observer.InputPropertyChanged(object_path, property_name);
  92. }
  93. raw_ptr<dbus::ObjectManager> object_manager_;
  94. // List of observers interested in event notifications from us.
  95. base::ObserverList<BluetoothInputClient::Observer>::Unchecked observers_;
  96. // Weak pointer factory for generating 'this' pointers that might live longer
  97. // than we do.
  98. // Note: This should remain the last member so it'll be destroyed and
  99. // invalidate its weak pointers before any other members are destroyed.
  100. base::WeakPtrFactory<BluetoothInputClientImpl> weak_ptr_factory_{this};
  101. };
  102. BluetoothInputClient::BluetoothInputClient() = default;
  103. BluetoothInputClient::~BluetoothInputClient() = default;
  104. BluetoothInputClient* BluetoothInputClient::Create() {
  105. return new BluetoothInputClientImpl();
  106. }
  107. } // namespace bluez