File: manual_testing_import.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (103 lines) | stat: -rw-r--r-- 4,681 bytes parent folder | download | duplicates (6)
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
// 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.

#ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_MANUAL_TESTING_IMPORT_H_
#define COMPONENTS_AUTOFILL_CORE_BROWSER_MANUAL_TESTING_IMPORT_H_

#include <optional>
#include <vector>

#include "base/memory/weak_ptr.h"
#include "base/values.h"
#include "components/autofill/core/browser/data_manager/personal_data_manager.h"
#include "components/autofill/core/browser/data_model/addresses/autofill_profile.h"

namespace autofill {

// Command line flags that enable importing AutofillProfiles for manual testing:
//   --autofill-profiles-content-for-manual-testing
//       Expects profile and credit card descriptions as a string in JSON
//       format.
//   --autofill-profiles-for-manual-testing
//       The same as above, but instead of the JSON content, expects the path
//       to a file with profile and credit card descriptions in JSON format.
// The following format is expected:
// {
//   "profiles" : [
//     {
//       "record_type" : "localOrSyncable",
//       "initial_creator_id" : 123,
//       "field-type" : "value",
//       ...
//     },
//     ...
//   ],
//   "credit-cards" : [
//     {
//       "nickname" : "...",
//       "field-type" : "value",
//       ...
//     }
//   ]
// }
// The "record_type" is optional and can either be "account" or
// "localOrSyncable". This corresponds to the AutofillProfile::RecordType of the
// resulting profile. It default to "localOrSyncable". The "initial_creator_id"
// is an optional int value which sets the profile's property of the same name.
// The "nickname" in credit cards optional as well. "field-type" corresponds to
// FieldTypes like "NAME_FULL". For profiles and credit cards, only field types
// valid for AutofillProfile or CreditCard are accepted. All profiles and credit
// cards specified in the file are imported. They replace any existing data. The
// profiles are expected to be fully structured.
// TODO(crbug.com/40255599): remove `profiles` from flags' name.
constexpr char kManualFileImportForTestingFlag[] =
    "autofill-profiles-for-manual-testing";
constexpr char kManualContentImportForTestingFlag[] =
    "autofill-profiles-content-for-manual-testing";

// Reads the contents of `file`, parses it as a JSON file and converts its
// content into a list of AutofillProfiles.
// If any step fails, an error message is logged and std::nullopt is returned.
std::optional<std::vector<AutofillProfile>> LoadProfilesFromFile(
    base::FilePath file);

// Reads the contents of `file`, parses it as a JSON file and converts its
// content into a list of CreditCards.
// If any step fails, an error message is logged and std::nullopt is returned.
std::optional<std::vector<CreditCard>> LoadCreditCardsFromFile(
    base::FilePath file);

// Given the array of descriptions of fully structured profiles in the
// aforementioned JSON format, converts it to a vector of AutofillProfiles.
// If the JSON list doesn't adhere to the above format, or if any of the
// profiles is not fully structured, an error is logged and std::nullopt is
// returned. A profile is considered "fully structured" if
// `FinalizeAfterImport()` doesn't change it. This condition exists to prevent
// profiles from silently changing, since `FinalizeAfterImport()` is called when
// retrieving a profile from the database. For example, if the structure is
// invalid because the last name is not part of the full name, the routine will
// clear this information.
std::optional<std::vector<AutofillProfile>> AutofillProfilesFromJSON(
    const base::Value::List* const profiles_json);

// Given the array of valid credit cards in the aforementioned JSON format,
// converts it to a vector of CreditCards.
// If the JSON list doesn't adhere to the above format, an error message is
// logged and std::nullopt is returned.
std::optional<std::vector<CreditCard>> CreditCardsFromJSON(
    const base::Value::List* const cards_json);

// Checks if the `kManualImportForTestingFlag` flag is present. If so,
// reads the specified file, parses the profiles and credit cards description
// and imports them into the `pdm`.
// In case the import fails, an error message is logged and the browser
// intentionally exits ungracefully. This is to prevent manual testing with
// incorrect data.
// Since importing is done in a separate thread, the `pdm` is passed as a weak
// ptr. It is updated once the import has finished.
void MaybeImportDataForManualTesting(base::WeakPtr<PersonalDataManager> pdm);

}  // namespace autofill

#endif  // COMPONENTS_AUTOFILL_CORE_BROWSER_MANUAL_TESTING_IMPORT_H_