fake_fast_pair_repository.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 "ash/quick_pair/repository/fake_fast_pair_repository.h"
  5. #include "ash/quick_pair/common/logging.h"
  6. #include "ash/quick_pair/proto/fastpair.pb.h"
  7. #include "base/base64.h"
  8. #include "base/strings/string_number_conversions.h"
  9. #include "base/strings/string_util.h"
  10. #include "chromeos/services/bluetooth_config/public/cpp/device_image_info.h"
  11. #include "device/bluetooth/bluetooth_device.h"
  12. namespace ash {
  13. namespace quick_pair {
  14. FakeFastPairRepository::FakeFastPairRepository() : FastPairRepository() {}
  15. FakeFastPairRepository::~FakeFastPairRepository() = default;
  16. void FakeFastPairRepository::SetFakeMetadata(const std::string& hex_model_id,
  17. nearby::fastpair::Device metadata,
  18. gfx::Image image) {
  19. nearby::fastpair::GetObservedDeviceResponse response;
  20. response.mutable_device()->CopyFrom(metadata);
  21. data_[base::ToUpperASCII(hex_model_id)] =
  22. std::make_unique<DeviceMetadata>(response, image);
  23. }
  24. void FakeFastPairRepository::ClearFakeMetadata(
  25. const std::string& hex_model_id) {
  26. data_.erase(base::ToUpperASCII(hex_model_id));
  27. }
  28. void FakeFastPairRepository::SetCheckAccountKeysResult(
  29. absl::optional<PairingMetadata> result) {
  30. check_account_keys_result_ = result;
  31. }
  32. bool FakeFastPairRepository::HasKeyForDevice(const std::string& mac_address) {
  33. return saved_account_keys_.contains(mac_address);
  34. }
  35. void FakeFastPairRepository::GetDeviceMetadata(
  36. const std::string& hex_model_id,
  37. DeviceMetadataCallback callback) {
  38. if (!is_network_connected_) {
  39. std::move(callback).Run(/*device=*/nullptr, /*has_retryable_error=*/true);
  40. return;
  41. }
  42. std::string normalized_id = base::ToUpperASCII(hex_model_id);
  43. if (data_.contains(normalized_id)) {
  44. std::move(callback).Run(data_[normalized_id].get(),
  45. /*has_retryable_error=*/false);
  46. return;
  47. }
  48. std::move(callback).Run(nullptr, /*has_retryable_error=*/true);
  49. }
  50. void FakeFastPairRepository::CheckAccountKeys(
  51. const AccountKeyFilter& account_key_filter,
  52. CheckAccountKeysCallback callback) {
  53. std::move(callback).Run(check_account_keys_result_);
  54. }
  55. void FakeFastPairRepository::AssociateAccountKey(
  56. scoped_refptr<Device> device,
  57. const std::vector<uint8_t>& account_key) {
  58. saved_account_keys_[device->ble_address] = account_key;
  59. }
  60. bool FakeFastPairRepository::AssociateAccountKeyLocally(
  61. scoped_refptr<Device> device) {
  62. std::vector<uint8_t> fake_account_key;
  63. saved_account_keys_[device->ble_address] = fake_account_key;
  64. return true;
  65. }
  66. void FakeFastPairRepository::DeleteAssociatedDevice(
  67. const std::string& mac_address,
  68. DeleteAssociatedDeviceCallback callback) {
  69. std::move(callback).Run(saved_account_keys_.erase(mac_address) == 1);
  70. }
  71. void FakeFastPairRepository::SetOptInStatus(
  72. nearby::fastpair::OptInStatus status) {
  73. status_ = status;
  74. }
  75. nearby::fastpair::OptInStatus FakeFastPairRepository::GetOptInStatus() {
  76. return status_;
  77. }
  78. // Unimplemented.
  79. void FakeFastPairRepository::CheckOptInStatus(
  80. CheckOptInStatusCallback callback) {
  81. std::move(callback).Run(status_);
  82. }
  83. void FakeFastPairRepository::DeleteAssociatedDeviceByAccountKey(
  84. const std::vector<uint8_t>& account_key,
  85. DeleteAssociatedDeviceByAccountKeyCallback callback) {
  86. for (auto it = devices_.begin(); it != devices_.end(); it++) {
  87. if (it->has_account_key() &&
  88. base::HexEncode(std::vector<uint8_t>(it->account_key().begin(),
  89. it->account_key().end())) ==
  90. base::HexEncode(account_key)) {
  91. devices_.erase(it);
  92. std::move(callback).Run(/*success=*/true);
  93. return;
  94. }
  95. }
  96. std::move(callback).Run(/*success=*/false);
  97. }
  98. void FakeFastPairRepository::UpdateOptInStatus(
  99. nearby::fastpair::OptInStatus opt_in_status,
  100. UpdateOptInStatusCallback callback) {
  101. status_ = opt_in_status;
  102. std::move(callback).Run(/*success=*/true);
  103. }
  104. // Unimplemented.
  105. void FakeFastPairRepository::FetchDeviceImages(scoped_refptr<Device> device) {
  106. return;
  107. }
  108. bool FakeFastPairRepository::IsAccountKeyPairedLocally(
  109. const std::vector<uint8_t>& account_key) {
  110. return is_account_key_paired_locally_;
  111. }
  112. // Unimplemented.
  113. bool FakeFastPairRepository::PersistDeviceImages(scoped_refptr<Device> device) {
  114. return true;
  115. }
  116. // Unimplemented.
  117. bool FakeFastPairRepository::EvictDeviceImages(
  118. const device::BluetoothDevice* device) {
  119. return true;
  120. }
  121. // Unimplemented.
  122. absl::optional<chromeos::bluetooth_config::DeviceImageInfo>
  123. FakeFastPairRepository::GetImagesForDevice(const std::string& device_id) {
  124. return absl::nullopt;
  125. }
  126. void FakeFastPairRepository::SetSavedDevices(
  127. nearby::fastpair::OptInStatus status,
  128. std::vector<nearby::fastpair::FastPairDevice> devices) {
  129. status_ = status;
  130. devices_ = std::move(devices);
  131. }
  132. void FakeFastPairRepository::GetSavedDevices(GetSavedDevicesCallback callback) {
  133. std::move(callback).Run(status_, devices_);
  134. }
  135. } // namespace quick_pair
  136. } // namespace ash