fast_pair_repository.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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_H_
  5. #define ASH_QUICK_PAIR_REPOSITORY_FAST_PAIR_REPOSITORY_H_
  6. #include "ash/quick_pair/common/device.h"
  7. #include "ash/quick_pair/proto/fastpair.pb.h"
  8. #include "ash/quick_pair/repository/fast_pair/device_metadata.h"
  9. #include "ash/quick_pair/repository/fast_pair/pairing_metadata.h"
  10. #include "base/callback.h"
  11. #include "base/containers/flat_map.h"
  12. #include "chromeos/services/bluetooth_config/public/cpp/device_image_info.h"
  13. #include "third_party/abseil-cpp/absl/types/optional.h"
  14. namespace chromeos {
  15. namespace bluetooth_config {
  16. class DeviceImageInfo;
  17. } // namespace bluetooth_config
  18. } // namespace chromeos
  19. namespace device {
  20. class BluetoothDevice;
  21. } // namespace device
  22. namespace ash {
  23. namespace quick_pair {
  24. class AccountKeyFilter;
  25. using CheckAccountKeysCallback =
  26. base::OnceCallback<void(absl::optional<PairingMetadata>)>;
  27. using DeviceMetadataCallback = base::OnceCallback<void(DeviceMetadata*, bool)>;
  28. using ValidModelIdCallback = base::OnceCallback<void(bool)>;
  29. using CheckOptInStatusCallback =
  30. base::OnceCallback<void(nearby::fastpair::OptInStatus)>;
  31. using UpdateOptInStatusCallback = base::OnceCallback<void(bool)>;
  32. using DeleteAssociatedDeviceCallback = base::OnceCallback<void(bool)>;
  33. using DeleteAssociatedDeviceByAccountKeyCallback =
  34. base::OnceCallback<void(bool)>;
  35. using GetSavedDevicesCallback =
  36. base::OnceCallback<void(nearby::fastpair::OptInStatus,
  37. std::vector<nearby::fastpair::FastPairDevice>)>;
  38. // The entry point for the Repository component in the Quick Pair system,
  39. // responsible for connecting to back-end services.
  40. class FastPairRepository {
  41. public:
  42. static FastPairRepository* Get();
  43. FastPairRepository();
  44. virtual ~FastPairRepository();
  45. // Returns the DeviceMetadata for a given |hex_model_id| to the provided
  46. // |callback|, if available.
  47. virtual void GetDeviceMetadata(const std::string& hex_model_id,
  48. DeviceMetadataCallback callback) = 0;
  49. // Checks all account keys associated with the user's account against the
  50. // given filter. If a match is found, metadata for the associated device will
  51. // be returned through the callback.
  52. virtual void CheckAccountKeys(const AccountKeyFilter& account_key_filter,
  53. CheckAccountKeysCallback callback) = 0;
  54. // Checks account keys saved to the device registry for a match to
  55. // |account_key|. Return true if a match is found and false otherwise.
  56. virtual bool IsAccountKeyPairedLocally(
  57. const std::vector<uint8_t>& account_key) = 0;
  58. // Stores the given |account_key| for a |device| on the server.
  59. virtual void AssociateAccountKey(scoped_refptr<Device> device,
  60. const std::vector<uint8_t>& account_key) = 0;
  61. // Stores the account_key for a |device| locally in the SavedDeviceRegistry,
  62. // skipping the step where we send a request to the server. The account key
  63. // should be stored in the additional data field of the device, fails
  64. // otherwise.
  65. virtual bool AssociateAccountKeyLocally(scoped_refptr<Device> device) = 0;
  66. // Deletes the associated data for device with a given |mac_address|.
  67. // Returns true if a delete will be processed for this device, false
  68. // otherwise.
  69. virtual void DeleteAssociatedDevice(
  70. const std::string& mac_address,
  71. DeleteAssociatedDeviceCallback callback) = 0;
  72. // Deletes the associated data for a given |account_key|.
  73. // Runs true if a delete is successful for this account key, false
  74. // otherwise on |callback|.
  75. virtual void DeleteAssociatedDeviceByAccountKey(
  76. const std::vector<uint8_t>& account_key,
  77. DeleteAssociatedDeviceByAccountKeyCallback callback) = 0;
  78. // Fetches the |device| images and a record of the device ID -> model ID
  79. // mapping to memory.
  80. virtual void FetchDeviceImages(scoped_refptr<Device> device) = 0;
  81. // Persists the images and device ID belonging to |device| to
  82. // disk, if model ID is not already persisted.
  83. virtual bool PersistDeviceImages(scoped_refptr<Device> device) = 0;
  84. // Evicts the images and device ID belonging to |device| from
  85. // disk, if model ID is not in use by other device IDs.
  86. virtual bool EvictDeviceImages(const device::BluetoothDevice* device) = 0;
  87. // Returns device images belonging to |device_id|, if found.
  88. virtual absl::optional<chromeos::bluetooth_config::DeviceImageInfo>
  89. GetImagesForDevice(const std::string& device_id) = 0;
  90. // Fetches the opt in status from Footprints to determine the status for
  91. // saving a user's devices to their account, which is synced all across a
  92. // user's devices.
  93. virtual void CheckOptInStatus(CheckOptInStatusCallback callback) = 0;
  94. // Updates the opt in status for saving a user's devices to their account,
  95. // synced across all of a user's devices, based on |opt_in_status|.
  96. // If |opt_in_status| reflects the user being opted out, then all devices
  97. // saved to their account are removed. The success or failure of updating
  98. // the opt in status is reflected in running |callback|.
  99. virtual void UpdateOptInStatus(nearby::fastpair::OptInStatus opt_in_status,
  100. UpdateOptInStatusCallback callback) = 0;
  101. // Gets a list of devices saved to the user's account and the user's opt in
  102. // status for saving future devices to their account.
  103. virtual void GetSavedDevices(GetSavedDevicesCallback callback) = 0;
  104. protected:
  105. static void SetInstance(FastPairRepository* instance);
  106. };
  107. } // namespace quick_pair
  108. } // namespace ash
  109. #endif // ASH_QUICK_PAIR_REPOSITORY_FAST_PAIR_REPOSITORY_H_