bluetooth_admin_policy_client.cc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Copyright 2021 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_admin_policy_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. const char kNoResponseError[] = "org.chromium.Error.NoResponse";
  16. const char kUnknownAdminPolicyError[] = "org.chromium.Error.UnknownAdminPolicy";
  17. namespace bluez {
  18. BluetoothAdminPolicyClient::Properties::Properties(
  19. dbus::ObjectProxy* object_proxy,
  20. const std::string& interface_name,
  21. const PropertyChangedCallback& callback)
  22. : dbus::PropertySet(object_proxy, interface_name, callback) {
  23. RegisterProperty(bluetooth_admin_policy::kServiceAllowListProperty,
  24. &service_allow_list);
  25. RegisterProperty(bluetooth_admin_policy::kIsBlockedByPolicyProperty,
  26. &is_blocked_by_policy);
  27. }
  28. BluetoothAdminPolicyClient::Properties::~Properties() = default;
  29. // The BluetoothAdminPolicyClient implementation used in production.
  30. class BluetoothAdminPolicyClientImpl : public BluetoothAdminPolicyClient,
  31. public dbus::ObjectManager::Interface {
  32. public:
  33. BluetoothAdminPolicyClientImpl() = default;
  34. ~BluetoothAdminPolicyClientImpl() override {
  35. // There is an instance of this client that is created but not initialized
  36. // on Linux. See 'Alternate D-Bus Client' note in bluez_dbus_manager.h.
  37. if (object_manager_) {
  38. object_manager_->UnregisterInterface(
  39. bluetooth_adapter::kBluetoothAdapterInterface);
  40. }
  41. }
  42. // BluetoothAdminPolicyClient override.
  43. void AddObserver(BluetoothAdminPolicyClient::Observer* observer) override {
  44. DCHECK(observer);
  45. observers_.AddObserver(observer);
  46. }
  47. // BluetoothAdminPolicyClient override.
  48. void RemoveObserver(BluetoothAdminPolicyClient::Observer* observer) override {
  49. DCHECK(observer);
  50. observers_.RemoveObserver(observer);
  51. }
  52. // dbus::ObjectManager::Interface override.
  53. dbus::PropertySet* CreateProperties(
  54. dbus::ObjectProxy* object_proxy,
  55. const dbus::ObjectPath& object_path,
  56. const std::string& interface_name) override {
  57. return new Properties(
  58. object_proxy, interface_name,
  59. base::BindRepeating(&BluetoothAdminPolicyClientImpl::OnPropertyChanged,
  60. weak_ptr_factory_.GetWeakPtr(), object_path));
  61. }
  62. // BluetoothAdminPolicyClient override.
  63. Properties* GetProperties(const dbus::ObjectPath& object_path) override {
  64. return static_cast<Properties*>(object_manager_->GetProperties(
  65. object_path,
  66. bluetooth_admin_policy::kBluetoothAdminPolicyStatusInterface));
  67. }
  68. void SetServiceAllowList(const dbus::ObjectPath& object_path,
  69. const UUIDList& service_uuids,
  70. base::OnceClosure callback,
  71. ErrorCallback error_callback) override {
  72. std::vector<std::string> uuid_array;
  73. for (const auto& uuid : service_uuids)
  74. uuid_array.push_back(uuid.canonical_value());
  75. dbus::MethodCall method_call(
  76. bluetooth_admin_policy::kBluetoothAdminPolicySetInterface,
  77. bluetooth_admin_policy::kSetServiceAllowList);
  78. dbus::MessageWriter writer(&method_call);
  79. writer.AppendArrayOfStrings(uuid_array);
  80. dbus::ObjectProxy* object_proxy =
  81. object_manager_->GetObjectProxy(object_path);
  82. if (!object_proxy) {
  83. std::move(error_callback).Run(kUnknownAdminPolicyError, "");
  84. return;
  85. }
  86. object_proxy->CallMethodWithErrorCallback(
  87. &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
  88. base::BindOnce(&BluetoothAdminPolicyClientImpl::OnSuccess,
  89. weak_ptr_factory_.GetWeakPtr(), std::move(callback)),
  90. base::BindOnce(&BluetoothAdminPolicyClientImpl::OnError,
  91. weak_ptr_factory_.GetWeakPtr(),
  92. std::move(error_callback)));
  93. }
  94. protected:
  95. void Init(dbus::Bus* bus,
  96. const std::string& bluetooth_service_name) override {
  97. object_manager_ = bus->GetObjectManager(
  98. bluetooth_service_name,
  99. dbus::ObjectPath(
  100. bluetooth_object_manager::kBluetoothObjectManagerServicePath));
  101. object_manager_->RegisterInterface(
  102. bluetooth_admin_policy::kBluetoothAdminPolicyStatusInterface, this);
  103. }
  104. private:
  105. // Called by dbus::ObjectManager when an object with the Admin Policy
  106. // interface is created. Informs observers.
  107. void ObjectAdded(const dbus::ObjectPath& object_path,
  108. const std::string& interface_name) override {
  109. for (auto& observer : observers_)
  110. observer.AdminPolicyAdded(object_path);
  111. }
  112. // Called by dbus::ObjectManager when an object with the Admin Policy
  113. // interface is removed. Informs observers.
  114. void ObjectRemoved(const dbus::ObjectPath& object_path,
  115. const std::string& interface_name) override {
  116. for (auto& observer : observers_)
  117. observer.AdminPolicyRemoved(object_path);
  118. }
  119. // Called by BluetoothPropertySet when a property value is changed,
  120. // either by result of a signal or response to a GetAll() or Get()
  121. // call. Informs observers.
  122. void OnPropertyChanged(const dbus::ObjectPath& object_path,
  123. const std::string& property_name) {
  124. for (auto& observer : observers_)
  125. observer.AdminPolicyPropertyChanged(object_path, property_name);
  126. }
  127. // Called when a response for successful method call is received.
  128. void OnSuccess(base::OnceClosure callback, dbus::Response* response) {
  129. DCHECK(response);
  130. std::move(callback).Run();
  131. }
  132. // Called when a response for a failed method call is received.
  133. void OnError(ErrorCallback error_callback, dbus::ErrorResponse* response) {
  134. // Error response has optional error message argument.
  135. std::string error_name;
  136. std::string error_message;
  137. if (response) {
  138. dbus::MessageReader reader(response);
  139. error_name = response->GetErrorName();
  140. reader.PopString(&error_message);
  141. } else {
  142. error_name = kNoResponseError;
  143. error_message = "";
  144. }
  145. std::move(error_callback).Run(error_name, error_message);
  146. }
  147. raw_ptr<dbus::ObjectManager> object_manager_ = nullptr;
  148. // List of observers interested in event notifications from us.
  149. base::ObserverList<BluetoothAdminPolicyClient::Observer>::Unchecked
  150. observers_;
  151. // Weak pointer factory for generating 'this' pointers that might live longer
  152. // than we do.
  153. // Note: This should remain the last member so it'll be destroyed and
  154. // invalidate its weak pointers before any other members are destroyed.
  155. base::WeakPtrFactory<BluetoothAdminPolicyClientImpl> weak_ptr_factory_{this};
  156. };
  157. BluetoothAdminPolicyClient::BluetoothAdminPolicyClient() = default;
  158. BluetoothAdminPolicyClient::~BluetoothAdminPolicyClient() = default;
  159. std::unique_ptr<BluetoothAdminPolicyClient>
  160. BluetoothAdminPolicyClient::Create() {
  161. return std::make_unique<BluetoothAdminPolicyClientImpl>();
  162. }
  163. } // namespace bluez