File: address.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (330 lines) | stat: -rw-r--r-- 12,924 bytes parent folder | download | duplicates (5)
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// Copyright 2013 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/data_model/addresses/address.h"

#include <stddef.h>

#include <algorithm>
#include <memory>

#include "base/check_op.h"
#include "base/containers/contains.h"
#include "base/feature_list.h"
#include "base/i18n/case_conversion.h"
#include "base/notreached.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "components/autofill/core/browser/autofill_field.h"
#include "components/autofill/core/browser/autofill_type.h"
#include "components/autofill/core/browser/data_model/addresses/autofill_profile.h"
#include "components/autofill/core/browser/data_model/addresses/autofill_profile_comparator.h"
#include "components/autofill/core/browser/data_model/addresses/autofill_structured_address_component.h"
#include "components/autofill/core/browser/data_model/addresses/autofill_structured_address_utils.h"
#include "components/autofill/core/browser/data_quality/autofill_data_util.h"
#include "components/autofill/core/browser/field_types.h"
#include "components/autofill/core/browser/geo/autofill_country.h"
#include "components/autofill/core/browser/geo/country_names.h"
#include "components/autofill/core/browser/geo/state_names.h"
#include "components/autofill/core/common/autofill_features.h"
#include "components/autofill/core/common/autofill_l10n_util.h"

namespace autofill {

Address::Address(AddressCountryCode country_code)
    : address_component_store_(
          i18n_model_definition::CreateAddressComponentModel(country_code)),
      is_legacy_address_(
          !i18n_model_definition::IsCustomHierarchyAvailableForCountry(
              country_code)) {}

Address::~Address() = default;

Address::Address(const Address& address) {
  *this = address;
}

Address& Address::operator=(const Address& address) {
  if (this == &address) {
    return *this;
  }

  address_component_store_ = i18n_model_definition::CreateAddressComponentModel(
      address.IsLegacyAddress() ? AddressCountryCode("")
                                : address.GetAddressCountryCode());
  Root()->CopyFrom(address.GetRoot());
  is_legacy_address_ = address.IsLegacyAddress();
  return *this;
}

bool Address::operator==(const Address& other) const {
  if (this == &other)
    return true;
  return GetRoot().SameAs(other.GetRoot());
}

bool Address::FinalizeAfterImport() {
  Root()->MigrateLegacyStructure();
  bool result = Root()->CompleteFullTree();
  // If the address could not be completed, it is possible that it contains an
  // invalid structure.
  if (!result) {
    if (Root()->WipeInvalidStructure()) {
      // If the structure was wiped because it is invalid, try to complete the
      // address again.
      result = Root()->CompleteFullTree();
    }
  }
  // Generate synthesized node values after the tree's values were updated,
  // whether by completion or gaps filled.
  Root()->GenerateTreeSynthesizedNodes();
  return result;
}

bool Address::MergeStructuredAddress(const Address& newer,
                                     bool newer_was_more_recently_used) {
  return Root()->MergeWithComponent(newer.GetRoot(),
                                    newer_was_more_recently_used);
}

std::optional<AlternativeStateNameMap::CanonicalStateName>
Address::GetCanonicalizedStateName() const {
  return AlternativeStateNameMap::GetCanonicalStateName(
      GetAddressCountryCode().value(), GetRawInfo(ADDRESS_HOME_STATE));
}

bool Address::IsStructuredAddressMergeable(const Address& newer) const {
  return GetRoot().IsMergeableWithComponent(newer.GetRoot());
}

bool Address::IsStructuredAddressMergeableForType(FieldType type,
                                                  const Address& other) const {
  return address_component_store_.GetNodeForType(type)
      ->IsMergeableWithComponent(
          *other.address_component_store_.GetNodeForType(type));
}

const AddressComponent& Address::GetRoot() const {
  return *address_component_store_.Root();
}

AddressComponent* Address::Root() {
  return address_component_store_.Root();
}

AddressCountryCode Address::GetAddressCountryCode() const {
  return GetRoot().GetCountryCode();
}

std::u16string Address::GetRawInfo(FieldType type) const {
  DCHECK_EQ(FieldTypeGroup::kAddress, GroupTypeOfFieldType(type));

  return GetRoot().GetValueForType(type);
}

void Address::SetRawInfoWithVerificationStatus(FieldType type,
                                               const std::u16string& value,
                                               VerificationStatus status) {
  DCHECK_EQ(FieldTypeGroup::kAddress, GroupTypeOfFieldType(type));
  // The street address has a structure that may have already been set before
  // using the settings dialog. In case the settings dialog was used to change
  // the address to contain different tokens, the structure must be reset.
  if (type == ADDRESS_HOME_STREET_ADDRESS) {
    const std::u16string current_value = Root()->GetValueForType(type);
    if (!current_value.empty()) {
      AreStringTokenEquivalent(value, Root()->GetValueForType(type))
          ? Root()->SetValueForType(ADDRESS_HOME_STREET_ADDRESS, value, status)
          : Root()->SetValueForType(ADDRESS_HOME_STREET_ADDRESS, value, status,
                                    /*invalidate_child_nodes=*/true);
      return;
    }
  }

  if (type == ADDRESS_HOME_COUNTRY) {
    SetAddressCountryCode(value, status);
    return;
  }

  Root()->SetValueForType(type, value, status);
}

void Address::GetMatchingTypes(const std::u16string& text,
                               const std::string& app_locale,
                               FieldTypeSet* matching_types) const {
  FormGroup::GetMatchingTypes(text, app_locale, matching_types);

  std::string country_code = GetRoot().GetCountryCode().value();

  // Check to see if the |text| canonicalized as a country name is a match.
  std::string entered_country_code =
      CountryNames::GetInstance()->GetCountryCodeForLocalizedCountryName(
          text, app_locale);
  if (!entered_country_code.empty() && country_code == entered_country_code)
    matching_types->insert(ADDRESS_HOME_COUNTRY);

  l10n::CaseInsensitiveCompare compare;
  // Check to see if the |text| could be the full name or abbreviation of a
  // state.
  std::u16string canon_text =
      AutofillProfileComparator::NormalizeForComparison(text);
  std::u16string state_name;
  std::u16string state_abbreviation;
  state_names::GetNameAndAbbreviation(canon_text, &state_name,
                                      &state_abbreviation);

  if (!state_name.empty() || !state_abbreviation.empty()) {
    std::u16string canon_profile_state =
        AutofillProfileComparator::NormalizeForComparison(
            GetInfo(ADDRESS_HOME_STATE, app_locale));
    if ((!state_name.empty() &&
         compare.StringsEqual(state_name, canon_profile_state)) ||
        (!state_abbreviation.empty() &&
         compare.StringsEqual(state_abbreviation, canon_profile_state))) {
      matching_types->insert(ADDRESS_HOME_STATE);
    }
  }
}

FieldTypeSet Address::GetSupportedTypes() const {
  return GetRoot().GetSupportedTypes();
}

std::u16string Address::GetInfo(const AutofillType& type,
                                const std::string& locale) const {
  std::string country_code =
      base::UTF16ToUTF8(GetRoot().GetValueForType(ADDRESS_HOME_COUNTRY));

  if (type.html_type() == HtmlFieldType::kCountryCode) {
    return base::ASCIIToUTF16(country_code);
  }

  FieldType storable_type = type.GetStorableType();
  if (storable_type == ADDRESS_HOME_COUNTRY && !country_code.empty())
    return AutofillCountry(country_code, locale).name();

  return GetRawInfo(storable_type);
}

bool Address::SetInfoWithVerificationStatus(const AutofillType& type,
                                            const std::u16string& value,
                                            const std::string& locale,
                                            VerificationStatus status) {
  if (type.html_type() == HtmlFieldType::kCountryCode) {
    std::string country_code =
        base::IsStringASCII(value)
            ? base::ToUpperASCII(base::UTF16ToASCII(value))
            : std::string();
    if (!data_util::IsValidCountryCode(country_code)) {
      // To counteract the misuse of autocomplete=country attribute when used
      // with full country names, if the supplied country code is not a valid,
      // it is tested if a country code can be derived from the value when it is
      // interpreted as a full country name. Otherwise an empty string is
      // assigned to |country_code|.
      CountryNames* country_names =
          !value.empty() ? CountryNames::GetInstance() : nullptr;
      country_code = country_names
                         ? country_names->GetCountryCodeForLocalizedCountryName(
                               value, locale)
                         : std::string();
    }

    SetRawInfoWithVerificationStatus(ADDRESS_HOME_COUNTRY,
                                     base::UTF8ToUTF16(country_code), status);
    return !country_code.empty();
  }

  FieldType storable_type = type.GetStorableType();
  if (storable_type == ADDRESS_HOME_COUNTRY && !value.empty()) {
    std::string country_code =
        CountryNames::GetInstance()->GetCountryCodeForLocalizedCountryName(
            value, locale);

    SetRawInfoWithVerificationStatus(ADDRESS_HOME_COUNTRY,
                                     base::UTF8ToUTF16(country_code), status);
    return !GetRawInfo(ADDRESS_HOME_COUNTRY).empty();
  }

  SetRawInfoWithVerificationStatus(storable_type, value, status);

  // Give up when importing addresses with any entirely blank lines.
  // There's a good chance that this formatting is not intentional, but it's
  // also not obviously safe to just strip the newlines.
  if (storable_type == ADDRESS_HOME_STREET_ADDRESS) {
    return Root()->IsValueForTypeValid(ADDRESS_HOME_STREET_ADDRESS,
                                       /*wipe_if_not=*/true);
  }

  return true;
}

VerificationStatus Address::GetVerificationStatus(FieldType type) const {
  return GetRoot().GetVerificationStatusForType(type);
}

void Address::SetAddressCountryCode(const std::u16string& country_code,
                                    VerificationStatus verification_status) {
  const AddressCountryCode new_address_country_code =
      AddressCountryCode(base::UTF16ToUTF8(country_code));

  // No restructuring is necessary if the new country is the same as the current
  // one. Only updating the verification status is required.
  if (GetAddressCountryCode() == new_address_country_code) {
    Root()->SetValueForType(ADDRESS_HOME_COUNTRY, country_code,
                            verification_status);
    return;
  }

  // No restructuring is necessary if both countries use the default hierarchy.
  if (IsLegacyAddress() &&
      !i18n_model_definition::IsCustomHierarchyAvailableForCountry(
          new_address_country_code)) {
    Root()->SetValueForType(ADDRESS_HOME_COUNTRY, country_code,
                            verification_status);
    return;
  }

  // Create an updated version of the internal hierarchy.
  AddressComponentsStore updated_address_component_store =
      i18n_model_definition::CreateAddressComponentModel(
          new_address_country_code);
  is_legacy_address_ =
      !i18n_model_definition::IsCustomHierarchyAvailableForCountry(
          new_address_country_code);

  // Transfer the content from the old model into the new one. Note that it
  // is possible that some nodes are not present in the updated model. Those
  // will be ignored.
  FieldTypeSet prev_supported_types = Root()->GetStorableTypes();
  prev_supported_types.erase(ADDRESS_HOME_COUNTRY);

  for (FieldType type : prev_supported_types) {
    updated_address_component_store.Root()->SetValueForType(
        type, Root()->GetValueForType(type),
        Root()->GetVerificationStatusForType(type));
  }

  address_component_store_ = std::move(updated_address_component_store);
  // Update verification status.
  Root()->SetValueForType(ADDRESS_HOME_COUNTRY, country_code,
                          verification_status);
}

bool Address::IsAddressFieldSettingAccessible(FieldType field_type) const {
  // Default to US in case of empty country codes.
  AutofillCountry country(GetAddressCountryCode()->empty()
                              ? "US"
                              : GetAddressCountryCode().value());

  for (AddressComponent* component =
           address_component_store_.GetNodeForType(field_type);
       component != nullptr; component = component->Parent()) {
    if (country.IsAddressFieldSettingAccessible(component->GetStorageType())) {
      return true;
    }
  }
  return false;
}

}  // namespace autofill