fast_pair_repository_impl.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. #ifndef ASH_QUICK_PAIR_REPOSITORY_FAST_PAIR_REPOSITORY_IMPL_H_
  5. #define ASH_QUICK_PAIR_REPOSITORY_FAST_PAIR_REPOSITORY_IMPL_H_
  6. #include "ash/quick_pair/common/device.h"
  7. #include "ash/quick_pair/repository/fast_pair/device_metadata.h"
  8. #include "ash/quick_pair/repository/fast_pair_repository.h"
  9. #include "base/callback.h"
  10. #include "base/containers/flat_map.h"
  11. #include "base/time/time.h"
  12. #include "third_party/abseil-cpp/absl/types/optional.h"
  13. namespace chromeos {
  14. namespace bluetooth_config {
  15. class DeviceImageInfo;
  16. } // namespace bluetooth_config
  17. } // namespace chromeos
  18. namespace device {
  19. class BluetoothDevice;
  20. } // namespace device
  21. namespace nearby {
  22. namespace fastpair {
  23. class UserReadDevicesResponse;
  24. } // namespace fastpair
  25. } // namespace nearby
  26. namespace ash {
  27. namespace quick_pair {
  28. class DeviceIdMap;
  29. class DeviceImageStore;
  30. class DeviceMetadataFetcher;
  31. class FastPairImageDecoder;
  32. class FootprintsFetcher;
  33. class SavedDeviceRegistry;
  34. // The entry point for the Repository component in the Quick Pair system,
  35. // responsible for connecting to back-end services.
  36. class FastPairRepositoryImpl : public FastPairRepository {
  37. public:
  38. FastPairRepositoryImpl();
  39. FastPairRepositoryImpl(
  40. std::unique_ptr<DeviceMetadataFetcher> device_metadata_fetcher,
  41. std::unique_ptr<FootprintsFetcher> footprints_fetcher,
  42. std::unique_ptr<FastPairImageDecoder> image_decoder,
  43. std::unique_ptr<DeviceIdMap> device_id_map,
  44. std::unique_ptr<DeviceImageStore> device_image_store,
  45. std::unique_ptr<SavedDeviceRegistry> saved_device_registry);
  46. FastPairRepositoryImpl(const FastPairRepositoryImpl&) = delete;
  47. FastPairRepositoryImpl& operator=(const FastPairRepositoryImpl&) = delete;
  48. ~FastPairRepositoryImpl() override;
  49. // FastPairRepository::
  50. void GetDeviceMetadata(const std::string& hex_model_id,
  51. DeviceMetadataCallback callback) override;
  52. void CheckAccountKeys(const AccountKeyFilter& account_key_filter,
  53. CheckAccountKeysCallback callback) override;
  54. bool IsAccountKeyPairedLocally(
  55. const std::vector<uint8_t>& account_key) override;
  56. void AssociateAccountKey(scoped_refptr<Device> device,
  57. const std::vector<uint8_t>& account_key) override;
  58. bool AssociateAccountKeyLocally(scoped_refptr<Device> device) override;
  59. void DeleteAssociatedDevice(const std::string& mac_address,
  60. DeleteAssociatedDeviceCallback callback) override;
  61. void DeleteAssociatedDeviceByAccountKey(
  62. const std::vector<uint8_t>& account_key,
  63. DeleteAssociatedDeviceByAccountKeyCallback callback) override;
  64. void FetchDeviceImages(scoped_refptr<Device> device) override;
  65. bool PersistDeviceImages(scoped_refptr<Device> device) override;
  66. bool EvictDeviceImages(const device::BluetoothDevice* device) override;
  67. absl::optional<chromeos::bluetooth_config::DeviceImageInfo>
  68. GetImagesForDevice(const std::string& device_id) override;
  69. void CheckOptInStatus(CheckOptInStatusCallback callback) override;
  70. void UpdateOptInStatus(nearby::fastpair::OptInStatus opt_in_status,
  71. UpdateOptInStatusCallback callback) override;
  72. void GetSavedDevices(GetSavedDevicesCallback callback) override;
  73. private:
  74. void CheckAccountKeysImpl(const AccountKeyFilter& account_key_filter,
  75. CheckAccountKeysCallback callback,
  76. bool refresh_cache_on_miss);
  77. void OnMetadataFetched(
  78. const std::string& normalized_model_id,
  79. DeviceMetadataCallback callback,
  80. absl::optional<nearby::fastpair::GetObservedDeviceResponse> response,
  81. bool has_retryable_error);
  82. void OnImageDecoded(const std::string& normalized_model_id,
  83. DeviceMetadataCallback callback,
  84. nearby::fastpair::GetObservedDeviceResponse response,
  85. gfx::Image image);
  86. void RetryCheckAccountKeys(
  87. const AccountKeyFilter& account_key_filter,
  88. CheckAccountKeysCallback callback,
  89. absl::optional<nearby::fastpair::UserReadDevicesResponse> user_devices);
  90. void UpdateUserDevicesCache(
  91. absl::optional<nearby::fastpair::UserReadDevicesResponse> user_devices);
  92. void CompleteAccountKeyLookup(CheckAccountKeysCallback callback,
  93. const std::vector<uint8_t> account_key,
  94. DeviceMetadata* device_metadata,
  95. bool has_retryable_error);
  96. void AddDeviceToFootprints(const std::string& hex_model_id,
  97. const std::string& mac_address,
  98. const std::vector<uint8_t>& account_key,
  99. DeviceMetadata* metadata,
  100. bool has_retryable_error);
  101. void OnAddDeviceToFootprintsComplete(const std::string& mac_address,
  102. const std::vector<uint8_t>& account_key,
  103. bool success);
  104. void OnCheckOptInStatus(
  105. CheckOptInStatusCallback callback,
  106. absl::optional<nearby::fastpair::UserReadDevicesResponse> user_devices);
  107. void OnUpdateOptInStatusComplete(UpdateOptInStatusCallback callback,
  108. bool success);
  109. // Fetches the |device_metadata| images to the DeviceImageStore for
  110. // |hex_model_id|.
  111. void CompleteFetchDeviceImages(const std::string& hex_model_id,
  112. DeviceMetadata* device_metadata,
  113. bool has_retryable_error);
  114. void OnDeleteAssociatedDevice(const std::string& mac_address,
  115. DeleteAssociatedDeviceCallback callback,
  116. bool success);
  117. void OnDeleteAssociatedDeviceByAccountKey(
  118. const std::vector<uint8_t>& account_key,
  119. DeleteAssociatedDeviceByAccountKeyCallback callback,
  120. bool footprints_removal_success);
  121. void OnGetSavedDevices(
  122. GetSavedDevicesCallback callback,
  123. absl::optional<nearby::fastpair::UserReadDevicesResponse> user_devices);
  124. std::unique_ptr<DeviceMetadataFetcher> device_metadata_fetcher_;
  125. std::unique_ptr<FootprintsFetcher> footprints_fetcher_;
  126. std::unique_ptr<FastPairImageDecoder> image_decoder_;
  127. std::unique_ptr<DeviceIdMap> device_id_map_;
  128. std::unique_ptr<DeviceImageStore> device_image_store_;
  129. std::unique_ptr<SavedDeviceRegistry> saved_device_registry_;
  130. base::flat_map<std::string, std::unique_ptr<DeviceMetadata>> metadata_cache_;
  131. nearby::fastpair::UserReadDevicesResponse user_devices_cache_;
  132. base::Time footprints_last_updated_;
  133. base::WeakPtrFactory<FastPairRepositoryImpl> weak_ptr_factory_{this};
  134. };
  135. } // namespace quick_pair
  136. } // namespace ash
  137. #endif // ASH_QUICK_PAIR_REPOSITORY_FAST_PAIR_REPOSITORY_IMPL_H_