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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/autofill/core/browser/manual_testing_import.h"
#include <string>
#include <string_view>
#include "base/check.h"
#include "base/command_line.h"
#include "base/containers/fixed_flat_map.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/json/json_reader.h"
#include "base/location.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "components/autofill/core/browser/autofill_type.h"
#include "components/autofill/core/browser/data_manager/addresses/address_data_manager.h"
#include "components/autofill/core/browser/data_manager/payments/payments_data_manager.h"
#include "components/autofill/core/browser/data_model/addresses/autofill_structured_address_component.h"
#include "components/autofill/core/browser/field_type_utils.h"
#include "components/autofill/core/browser/field_types.h"
namespace autofill {
namespace {
// Util struct for storing the list of profiles and credit cards to be imported.
// If any of `profiles` or `credit_cards` are std::nullopt, then the data used
// for import is malformed, and this will cause a crash.
// When any of `profiles` or `credit_cards` is empty, it means that the JSON
// file used for import did not include the corresponding key. It'll be treated
// as valid but won't be imported so that existing data in the PDM isn't
// cleared without replacement.
struct AutofillProfilesAndCreditCards {
std::optional<std::vector<AutofillProfile>> profiles;
std::optional<std::vector<CreditCard>> credit_cards;
};
constexpr std::string_view kKeyProfiles = "profiles";
constexpr std::string_view kKeyCreditCards = "credit-cards";
constexpr std::string_view kKeyRecordType = "record_type";
constexpr std::string_view kKeyNickname = "nickname";
constexpr auto kRecordTypeMapping =
base::MakeFixedFlatMap<std::string_view, AutofillProfile::RecordType>(
{{"account", AutofillProfile::RecordType::kAccount},
{"localOrSyncable", AutofillProfile::RecordType::kLocalOrSyncable}});
constexpr std::string_view kKeyInitialCreatorId = "initial_creator_id";
// Checks if the `profile` is changed by `FinalizeAfterImport()`. See
// documentation of `AutofillProfilesFromJSON()` for a rationale.
// The return value of `FinalizeAfterImport()` doesn't suffice to check that,
// since structured address and name components are updated separately.
bool IsFullyStructuredProfile(const AutofillProfile& profile) {
AutofillProfile finalized_profile = profile;
finalized_profile.FinalizeAfterImport();
// TODO(crbug.com/40268162): Re-enable this check.
// return profile == finalized_profile;
return true;
}
// Extracts the `kKeyRecordType` value of the `dict` and translates it into an
// AutofillProfile::RecordType. If no value is present,
// RecordType::kLocalOrSyncable is returned. If a record type with invalid value
// is specified, an error message is logged and std::nullopt is returned.
std::optional<AutofillProfile::RecordType> GetRecordTypeFromDict(
const base::Value::Dict& dict) {
if (!dict.contains(kKeyRecordType)) {
return AutofillProfile::RecordType::kLocalOrSyncable;
}
if (const std::string* record_type_value = dict.FindString(kKeyRecordType)) {
if (auto it = kRecordTypeMapping.find(*record_type_value);
it != kRecordTypeMapping.end()) {
return it->second;
}
}
LOG(ERROR) << "Invalid " << kKeyRecordType << " value.";
return std::nullopt;
}
// Given a `dict` of "field-type" : "value" mappings, constructs an
// AutofillProfile where each "field-type" is set to the provided "value".
// All verification statuses are set to `kObserved`. Setting them to
// `kUserVerified` is problematic, since the data model expects that only root
// level (= setting-visible) nodes are user verified.
// If a field type cannot be mapped, or if the resulting profile is not
// `IsFullyStructuredProfile()`, std::nullopt is returned.
std::optional<AutofillProfile> MakeProfile(const base::Value::Dict& dict) {
std::optional<AutofillProfile::RecordType> record_type =
GetRecordTypeFromDict(dict);
if (!record_type.has_value()) {
return std::nullopt;
}
const std::string* country_code =
dict.FindString(FieldTypeToStringView(ADDRESS_HOME_COUNTRY));
AddressCountryCode address_country_code =
country_code ? AddressCountryCode(*country_code) : AddressCountryCode("");
AutofillProfile profile(*record_type, address_country_code);
// `dict` is a dictionary of std::string -> base::Value.
for (const auto [key, value] : dict) {
if (key == kKeyRecordType) {
continue;
}
if (key == kKeyInitialCreatorId) {
if (const std::optional<int> creator_id = dict.FindInt(key)) {
profile.set_initial_creator_id(*creator_id);
continue;
} else {
LOG(ERROR) << "Incorrect value for " << key << ".";
return std::nullopt;
}
}
const FieldType type = TypeNameToFieldType(key);
if (type == UNKNOWN_TYPE || !IsAddressType(type)) {
LOG(ERROR) << "Unknown or non-address type " << key << ".";
return std::nullopt;
}
profile.SetRawInfoWithVerificationStatus(
type, base::UTF8ToUTF16(value.GetString()),
VerificationStatus::kObserved);
}
if (!IsFullyStructuredProfile(profile)) {
LOG(ERROR) << "Some profile is not fully structured.";
return std::nullopt;
}
return profile;
}
std::optional<CreditCard> MakeCard(const base::Value::Dict& dict) {
CreditCard card;
// `dict` is a dictionary of std::string -> base::Value.
for (const auto [key, value] : dict) {
if (key == kKeyNickname) {
card.SetNickname(base::UTF8ToUTF16(value.GetString()));
continue;
}
const FieldType type = TypeNameToFieldType(key);
if (type == UNKNOWN_TYPE ||
GroupTypeOfFieldType(type) != FieldTypeGroup::kCreditCard) {
LOG(ERROR) << "Unknown or non-credit card type " << key << ".";
return std::nullopt;
}
card.SetRawInfo(type, base::UTF8ToUTF16(value.GetString()));
}
if (!card.IsValid()) {
LOG(ERROR) << "Some credit card is not valid.";
return std::nullopt;
}
return card;
}
// Removes all AutofillProfiles from the `adm`. Since `ADM::RemoveProfile()`
// invalidates the pointers returned by `ADM::GetProfiles()`, this is done by
// collecting all GUIDs to remove first.
void RemoveAllExistingProfiles(AddressDataManager& adm) {
std::vector<std::string> existing_guids;
std::ranges::transform(adm.GetProfiles(), std::back_inserter(existing_guids),
&AutofillProfile::guid);
for (const std::string& guid : existing_guids) {
adm.RemoveProfile(guid);
}
}
// Sets all of the `pdm`'s profiles or credit cards to `profiles` or
// `credit_cards`, if the `pdm` still exists.
void SetData(
base::WeakPtr<PersonalDataManager> pdm,
std::optional<AutofillProfilesAndCreditCards> profiles_or_credit_cards) {
// This check intentionally crashes when the data is malformed, to prevent
// testing with incorrect data.
LOG_IF(FATAL, !profiles_or_credit_cards.has_value() ||
!profiles_or_credit_cards->profiles.has_value() ||
!profiles_or_credit_cards->credit_cards.has_value())
<< "Intentional crash, the provided JSON import data is incorrect.";
if (pdm == nullptr) {
return;
}
// If a list in `profiles_or_credit_cards` is empty, do not trigger the PDM
// because this will clear all corresponding existing data.
if (!profiles_or_credit_cards->profiles->empty()) {
RemoveAllExistingProfiles(pdm->address_data_manager());
for (const AutofillProfile& profile : *profiles_or_credit_cards->profiles) {
pdm->address_data_manager().AddProfile(profile);
}
}
if (!profiles_or_credit_cards->credit_cards->empty()) {
pdm->payments_data_manager().SetCreditCards(
&*profiles_or_credit_cards->credit_cards);
}
}
// Converts all `entries of `json_array` to a vector of Ts using
// `to_data_model`. In case any conversion fails, nullopt is returned.
template <class T>
std::optional<std::vector<T>> DataModelsFromJSON(
const base::Value::List* const json_array,
base::RepeatingCallback<std::optional<T>(const base::Value::Dict&)>
to_data_model) {
if (!json_array) {
return std::vector<T>{};
}
std::vector<T> data_models;
for (const base::Value& json : *json_array) {
if (!json.is_dict()) {
LOG(ERROR) << "Description is not a dictionary.";
return std::nullopt;
}
std::optional<T> data_model = to_data_model.Run(json.GetDict());
if (!data_model.has_value()) {
return std::nullopt;
}
data_models.push_back(std::move(*data_model));
}
// Move due to implicit type conversion.
return std::move(data_models);
}
// Parses AutofillProfiles from the JSON `content` string.
// If parsing fails the error is logged and std::nullopt is returned.
std::optional<AutofillProfilesAndCreditCards> LoadDataFromJSONContent(
const std::string& file_content) {
std::optional<base::Value::Dict> json =
base::JSONReader::ReadDict(file_content);
if (!json) {
LOG(ERROR) << "Failed to parse JSON file.";
return std::nullopt;
}
const base::Value::List* const profiles_json = json->FindList(kKeyProfiles);
const base::Value::List* const cards_json = json->FindList(kKeyCreditCards);
if (!cards_json && !profiles_json) {
LOG(ERROR) << "JSON has no " << kKeyProfiles << " or " << kKeyCreditCards
<< " keys.";
return std::nullopt;
}
return AutofillProfilesAndCreditCards{
.profiles = AutofillProfilesFromJSON(profiles_json),
.credit_cards = CreditCardsFromJSON(cards_json)};
}
std::optional<AutofillProfilesAndCreditCards> LoadDataFromFile(
base::FilePath file) {
std::string file_content;
if (!base::ReadFileToString(file, &file_content)) {
LOG(ERROR) << "Failed to read file " << file.MaybeAsASCII() << ".";
return std::nullopt;
}
return LoadDataFromJSONContent(file_content);
}
} // namespace
std::optional<std::vector<AutofillProfile>> LoadProfilesFromFile(
base::FilePath file) {
if (std::optional<AutofillProfilesAndCreditCards> profiles_and_credit_cards =
LoadDataFromFile(file)) {
return profiles_and_credit_cards->profiles;
}
return std::nullopt;
}
std::optional<std::vector<CreditCard>> LoadCreditCardsFromFile(
base::FilePath file) {
if (std::optional<AutofillProfilesAndCreditCards> profiles_and_credit_cards =
LoadDataFromFile(file)) {
return profiles_and_credit_cards->credit_cards;
}
return std::nullopt;
}
std::optional<std::vector<AutofillProfile>> AutofillProfilesFromJSON(
const base::Value::List* const profiles_json) {
return DataModelsFromJSON(profiles_json, base::BindRepeating(&MakeProfile));
}
std::optional<std::vector<CreditCard>> CreditCardsFromJSON(
const base::Value::List* const cards_json) {
return DataModelsFromJSON(cards_json, base::BindRepeating(&MakeCard));
}
void MaybeImportDataForManualTesting(base::WeakPtr<PersonalDataManager> pdm) {
const auto* kCommandLine = base::CommandLine::ForCurrentProcess();
if (kCommandLine->HasSwitch(kManualFileImportForTestingFlag)) {
base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE, {base::TaskPriority::BEST_EFFORT, base::MayBlock()},
base::BindOnce(&LoadDataFromFile, kCommandLine->GetSwitchValuePath(
kManualFileImportForTestingFlag)),
base::BindOnce(&SetData, pdm));
} else if (kCommandLine->HasSwitch(kManualContentImportForTestingFlag)) {
SetData(pdm, LoadDataFromJSONContent(kCommandLine->GetSwitchValueASCII(
kManualContentImportForTestingFlag)));
}
}
} // namespace autofill
|