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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
// 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.
#include "ash/quick_pair/repository/fast_pair/saved_device_registry.h"
#include <string>
#include "ash/quick_pair/common/quick_pair_browser_delegate.h"
#include "base/base64.h"
#include "base/containers/contains.h"
#include "components/cross_device/logging/logging.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "device/bluetooth//bluetooth_adapter.h"
#include "device/bluetooth/bluetooth_device.h"
namespace {
const char kFastPairSavedDevicesPref[] = "fast_pair.saved_devices";
} // namespace
namespace ash {
namespace quick_pair {
SavedDeviceRegistry::SavedDeviceRegistry(
scoped_refptr<device::BluetoothAdapter> adapter)
: adapter_(adapter) {}
SavedDeviceRegistry::~SavedDeviceRegistry() = default;
void SavedDeviceRegistry::RegisterProfilePrefs(PrefRegistrySimple* registry) {
registry->RegisterDictionaryPref(kFastPairSavedDevicesPref);
}
bool SavedDeviceRegistry::SaveAccountAssociation(
const std::string& mac_address,
const std::vector<uint8_t>& account_key) {
PrefService* pref_service =
QuickPairBrowserDelegate::Get()->GetActivePrefService();
if (!pref_service) {
CD_LOG(WARNING, Feature::FP)
<< __func__
<< ": No user pref service available. Failed to write "
"account association to Saved Device Registry.";
return false;
}
std::string encoded = base::Base64Encode(account_key);
ScopedDictPrefUpdate update(pref_service, kFastPairSavedDevicesPref);
update->Set(mac_address, encoded);
CD_LOG(INFO, Feature::FP) << __func__ << ": Saved account key.";
return true;
}
bool SavedDeviceRegistry::DeleteAccountKey(const std::string& mac_address) {
PrefService* pref_service =
QuickPairBrowserDelegate::Get()->GetActivePrefService();
if (!pref_service) {
CD_LOG(WARNING, Feature::FP)
<< __func__ << ": No user pref service available.";
return false;
}
ScopedDictPrefUpdate update(pref_service, kFastPairSavedDevicesPref);
if (!update->Remove(mac_address)) {
CD_LOG(WARNING, Feature::FP)
<< __func__
<< ": Failed to delete mac address -> account key record from prefs";
return false;
}
return true;
}
bool SavedDeviceRegistry::DeleteAccountKey(
const std::vector<uint8_t>& account_key) {
PrefService* pref_service =
QuickPairBrowserDelegate::Get()->GetActivePrefService();
if (!pref_service) {
CD_LOG(WARNING, Feature::FP)
<< __func__ << ": No user pref service available.";
return false;
}
const base::Value::Dict& saved_devices =
pref_service->GetDict(kFastPairSavedDevicesPref);
std::string encoded_key = base::Base64Encode(account_key);
for (const auto it : saved_devices) {
const std::string* value = it.second.GetIfString();
if (value && *value == encoded_key) {
ScopedDictPrefUpdate update(pref_service, kFastPairSavedDevicesPref);
return update->Remove(it.first);
}
}
CD_LOG(WARNING, Feature::FP)
<< __func__
<< ": Failed to delete account key record from prefs: "
"account key not found";
return false;
}
std::optional<const std::vector<uint8_t>> SavedDeviceRegistry::GetAccountKey(
const std::string& mac_address) {
PrefService* pref_service =
QuickPairBrowserDelegate::Get()->GetActivePrefService();
if (!pref_service) {
CD_LOG(WARNING, Feature::FP)
<< __func__ << ": No user pref service available.";
return std::nullopt;
}
const std::string* result = pref_service->GetValue(kFastPairSavedDevicesPref)
.GetDict()
.FindString(mac_address);
if (!result) {
return std::nullopt;
}
std::string decoded;
if (!base::Base64Decode(*result, &decoded)) {
CD_LOG(WARNING, Feature::FP)
<< __func__ << ": Failed to decode the account key from Base64.";
return std::nullopt;
}
return std::vector<uint8_t>(decoded.begin(), decoded.end());
}
bool SavedDeviceRegistry::IsAccountKeySavedToRegistry(
const std::vector<uint8_t>& account_key) {
PrefService* pref_service =
QuickPairBrowserDelegate::Get()->GetActivePrefService();
if (!pref_service) {
CD_LOG(WARNING, Feature::FP)
<< __func__ << ": No user pref service available.";
return false;
}
if (!has_updated_saved_devices_registry_) {
CD_LOG(INFO, Feature::FP)
<< __func__
<< ": checking for changes to the registry by cross checking "
"the adapter before continuing";
RemoveDevicesIfRemovedFromDifferentUser(pref_service);
}
const base::Value::Dict& saved_devices =
pref_service->GetDict(kFastPairSavedDevicesPref);
std::string encoded_key = base::Base64Encode(account_key);
for (const auto it : saved_devices) {
const std::string* value = it.second.GetIfString();
if (value && *value == encoded_key) {
return true;
}
}
return false;
}
void SavedDeviceRegistry::RemoveDevicesIfRemovedFromDifferentUser(
PrefService* pref_service) {
DCHECK(pref_service);
// Set of currently paired devices, stored by Bluetooth address, used to
// cross reference the registry for any devices that need to be removed.
std::set<std::string> paired_devices;
for (device::BluetoothDevice* device : adapter_->GetDevices()) {
if (device->IsPaired()) {
paired_devices.insert(device->GetAddress());
}
}
// Iterate over the list of devices in the registry, and if there are any in
// the registry that are no longer paired to the adapter (determined by mac
// address), remove them from the registry.
const base::Value::Dict& saved_devices =
pref_service->GetDict(kFastPairSavedDevicesPref);
for (const auto it : saved_devices) {
const std::string& mac_address = it.first;
if (!base::Contains(paired_devices, mac_address)) {
ScopedDictPrefUpdate update(pref_service, kFastPairSavedDevicesPref);
update->Remove(it.first);
CD_LOG(VERBOSE, Feature::FP)
<< __func__
<< ": removed device from registry at address= " << mac_address;
}
}
has_updated_saved_devices_registry_ = true;
}
} // namespace quick_pair
} // namespace ash
|