fake_bluetooth_admin_policy_client.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright (c) 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/fake_bluetooth_admin_policy_client.h"
  5. #include "base/containers/contains.h"
  6. #include "base/logging.h"
  7. #include "base/observer_list.h"
  8. #include "device/bluetooth/dbus/fake_bluetooth_adapter_client.h"
  9. #include "third_party/cros_system_api/dbus/service_constants.h"
  10. namespace bluez {
  11. FakeBluetoothAdminPolicyClient::Properties::Properties(
  12. const PropertyChangedCallback& callback)
  13. : BluetoothAdminPolicyClient::Properties(
  14. nullptr,
  15. bluetooth_admin_policy::kBluetoothAdminPolicyStatusInterface,
  16. callback) {}
  17. FakeBluetoothAdminPolicyClient::Properties::~Properties() = default;
  18. void FakeBluetoothAdminPolicyClient::Properties::Get(
  19. dbus::PropertyBase* property,
  20. dbus::PropertySet::GetCallback callback) {
  21. DVLOG(1) << "Get " << property->name();
  22. std::move(callback).Run(false);
  23. }
  24. void FakeBluetoothAdminPolicyClient::Properties::GetAll() {
  25. DVLOG(1) << "GetAll";
  26. }
  27. void FakeBluetoothAdminPolicyClient::Properties::Set(
  28. dbus::PropertyBase* property,
  29. dbus::PropertySet::SetCallback callback) {
  30. DVLOG(1) << "Set " << property->name();
  31. std::move(callback).Run(false);
  32. }
  33. FakeBluetoothAdminPolicyClient::FakeBluetoothAdminPolicyClient() = default;
  34. FakeBluetoothAdminPolicyClient::~FakeBluetoothAdminPolicyClient() = default;
  35. void FakeBluetoothAdminPolicyClient::Init(
  36. dbus::Bus* bus,
  37. const std::string& bluetooth_service_name) {}
  38. void FakeBluetoothAdminPolicyClient::AddObserver(Observer* observer) {
  39. observers_.AddObserver(observer);
  40. }
  41. void FakeBluetoothAdminPolicyClient::RemoveObserver(Observer* observer) {
  42. observers_.RemoveObserver(observer);
  43. }
  44. void FakeBluetoothAdminPolicyClient::SetServiceAllowList(
  45. const dbus::ObjectPath& object_path,
  46. const UUIDList& service_uuids,
  47. base::OnceClosure callback,
  48. ErrorCallback error_callback) {}
  49. FakeBluetoothAdminPolicyClient::Properties*
  50. FakeBluetoothAdminPolicyClient::GetProperties(
  51. const dbus::ObjectPath& object_path) {
  52. PropertiesMap::const_iterator iter = properties_map_.find(object_path);
  53. if (iter != properties_map_.end())
  54. return iter->second.get();
  55. return nullptr;
  56. }
  57. void FakeBluetoothAdminPolicyClient::CreateAdminPolicy(
  58. const dbus::ObjectPath& path,
  59. bool is_blocked_by_policy) {
  60. DCHECK(!base::Contains(properties_map_, path));
  61. auto properties = std::make_unique<Properties>(
  62. base::BindRepeating(&FakeBluetoothAdminPolicyClient::OnPropertyChanged,
  63. base::Unretained(this), path));
  64. properties->is_blocked_by_policy.ReplaceValue(is_blocked_by_policy);
  65. properties->is_blocked_by_policy.set_valid(true);
  66. properties_map_.emplace(path, std::move(properties));
  67. for (auto& observer : observers_)
  68. observer.AdminPolicyAdded(path);
  69. }
  70. void FakeBluetoothAdminPolicyClient::ChangeAdminPolicy(
  71. const dbus::ObjectPath& path,
  72. bool is_blocked_by_policy) {
  73. DCHECK(base::Contains(properties_map_, path));
  74. properties_map_[path]->is_blocked_by_policy.ReplaceValue(
  75. is_blocked_by_policy);
  76. for (auto& observer : observers_) {
  77. observer.AdminPolicyPropertyChanged(
  78. path, bluetooth_admin_policy::kIsBlockedByPolicyProperty);
  79. }
  80. }
  81. void FakeBluetoothAdminPolicyClient::RemoveAdminPolicy(
  82. const dbus::ObjectPath& path) {
  83. DCHECK(base::Contains(properties_map_, path));
  84. properties_map_.erase(path);
  85. for (auto& observer : observers_)
  86. observer.AdminPolicyRemoved(path);
  87. }
  88. void FakeBluetoothAdminPolicyClient::OnPropertyChanged(
  89. const dbus::ObjectPath& object_path,
  90. const std::string& property_name) {
  91. DVLOG(2) << "Fake Bluetooth admin_policy property changed: "
  92. << object_path.value() << ": " << property_name;
  93. for (auto& observer : observers_)
  94. observer.AdminPolicyPropertyChanged(object_path, property_name);
  95. }
  96. } // namespace bluez