1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef ASH_QUICK_PAIR_REPOSITORY_FAKE_FAST_PAIR_REPOSITORY_H_
#define ASH_QUICK_PAIR_REPOSITORY_FAKE_FAST_PAIR_REPOSITORY_H_
#include <optional>
#include "ash/quick_pair/common/device.h"
#include "ash/quick_pair/proto/fastpair.pb.h"
#include "ash/quick_pair/repository/fast_pair/device_metadata.h"
#include "ash/quick_pair/repository/fast_pair_repository.h"
#include "base/containers/flat_map.h"
#include "base/containers/flat_set.h"
#include "base/functional/callback.h"
namespace ash::bluetooth_config {
class DeviceImageInfo;
}
namespace ash {
namespace quick_pair {
// The entry point for the Repository component in the Quick Pair system,
// responsible for connecting to back-end services.
class FakeFastPairRepository : public FastPairRepository {
public:
FakeFastPairRepository();
FakeFastPairRepository(const FakeFastPairRepository&) = delete;
FakeFastPairRepository& operator=(const FakeFastPairRepository&) = delete;
~FakeFastPairRepository() override;
void SetFakeMetadata(const std::string& hex_model_id,
nearby::fastpair::Device metadata,
gfx::Image image = gfx::Image());
void ClearFakeMetadata(const std::string& hex_model_id);
void SetCheckAccountKeysResult(std::optional<PairingMetadata> result);
void set_is_account_key_paired_locally(bool is_account_key_paired_locally) {
is_account_key_paired_locally_ = is_account_key_paired_locally;
}
bool HasKeyForDevice(const std::string& mac_address);
bool HasNameForDevice(const std::string& mac_address);
void set_is_network_connected(bool is_connected) {
is_network_connected_ = is_connected;
}
void SetOptInStatus(nearby::fastpair::OptInStatus status);
nearby::fastpair::OptInStatus GetOptInStatus();
void SetSavedDevices(nearby::fastpair::OptInStatus status,
std::vector<nearby::fastpair::FastPairDevice> devices);
void SaveMacAddressToAccount(const std::string& mac_address);
// FastPairRepository::
void GetDeviceMetadata(const std::string& hex_model_id,
DeviceMetadataCallback callback) override;
void CheckAccountKeys(const AccountKeyFilter& account_key_filter,
CheckAccountKeysCallback callback) override;
void WriteAccountAssociationToFootprints(
scoped_refptr<Device> device,
const std::vector<uint8_t>& account_key) override;
bool WriteAccountAssociationToLocalRegistry(
scoped_refptr<Device> device) override;
void DeleteAssociatedDevice(const std::string& mac_address,
DeleteAssociatedDeviceCallback callback) override;
void UpdateAssociatedDeviceFootprintsName(const std::string& mac_address,
const std::string& display_name,
bool cache_may_be_stale) override;
void FetchDeviceImages(scoped_refptr<Device> device) override;
std::optional<std::string> GetDeviceDisplayNameFromCache(
std::vector<uint8_t> account_key) override;
bool PersistDeviceImages(scoped_refptr<Device> device) override;
bool EvictDeviceImages(const std::string& mac_address) override;
std::optional<bluetooth_config::DeviceImageInfo> GetImagesForDevice(
const std::string& mac_address) override;
void CheckOptInStatus(CheckOptInStatusCallback callback) override;
void UpdateOptInStatus(nearby::fastpair::OptInStatus opt_in_status,
UpdateOptInStatusCallback callback) override;
void DeleteAssociatedDeviceByAccountKey(
const std::vector<uint8_t>& account_key,
DeleteAssociatedDeviceByAccountKeyCallback callback) override;
void GetSavedDevices(GetSavedDevicesCallback callback) override;
bool IsAccountKeyPairedLocally(
const std::vector<uint8_t>& account_key) override;
void IsDeviceSavedToAccount(const std::string& mac_address,
IsDeviceSavedToAccountCallback callback) override;
// `SetIsDeviceSavedToAccountCallbackDelay` and
// `TriggerIsDeviceSavedToAccountCallback` are used together to control when
// the callback is triggered.
void TriggerIsDeviceSavedToAccountCallback();
void SetIsDeviceSavedToAccountCallbackDelayed(bool is_delayed) {
saved_to_account_callback_is_delayed_ = is_delayed;
}
private:
static void SetInstance(FastPairRepository* instance);
IsDeviceSavedToAccountCallback saved_to_account_callback_;
bool saved_to_account_callback_is_delayed_ = false;
nearby::fastpair::OptInStatus status_ =
nearby::fastpair::OptInStatus::STATUS_UNKNOWN;
std::vector<nearby::fastpair::FastPairDevice> devices_;
bool is_network_connected_ = true;
bool is_account_key_paired_locally_ = true;
base::flat_set<std::string> saved_mac_addresses_;
// The key for 'data_' is ASCII model ids.
base::flat_map<std::string, std::unique_ptr<DeviceMetadata>> data_;
// The key for 'saved_accout_keys_' is the device's classic address.
base::flat_map<std::string, std::vector<uint8_t>> saved_account_keys_;
// The key for 'saved_display_names_' is the device's classic address.
base::flat_map<std::string, std::string> saved_display_names_;
std::optional<PairingMetadata> check_account_keys_result_;
base::WeakPtrFactory<FakeFastPairRepository> weak_ptr_factory_{this};
};
} // namespace quick_pair
} // namespace ash
#endif // ASH_QUICK_PAIR_REPOSITORY_FAKE_FAST_PAIR_REPOSITORY_H_
|