File: autofill_country.h

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; 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 (138 lines) | stat: -rw-r--r-- 5,437 bytes parent folder | download | duplicates (2)
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
// 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.

#ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_GEO_AUTOFILL_COUNTRY_H_
#define COMPONENTS_AUTOFILL_CORE_BROWSER_GEO_AUTOFILL_COUNTRY_H_

#include <optional>
#include <string>
#include <string_view>

#include "base/feature_list.h"
#include "components/autofill/core/browser/country_type.h"
#include "components/autofill/core/browser/field_types.h"
#include "components/autofill/core/browser/geo/country_data.h"
#include "components/autofill/core/common/autofill_features.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_field.h"

namespace autofill {

class LogBuffer;

// Stores data associated with a country. Strings are localized to the app
// locale.
class AutofillCountry {
 public:
  // Returns country data corresponding to the two-letter ISO code
  // `country_code`.
  // `locale` is used to translate the `name()` appropriately and can be ignored
  // if the name is not queried.
  explicit AutofillCountry(
      std::string_view country_code,
      std::optional<std::string_view> locale = std::nullopt);

  AutofillCountry(const AutofillCountry&) = delete;
  AutofillCountry& operator=(const AutofillCountry&) = delete;

  ~AutofillCountry();

  // Autofill relies on libaddressinput for its address format.
  // AddressFormatExtensions are used to extend this format on a country-by-
  // country basis. This is needed because while some field types are not
  // strictly required for a valid address, we nonetheless see them in practice
  // and want to offer filling support.
  // This struct defines that a certain `type` is considered part of the address
  // format in Autofill, specifies its `label` and placement after the existing
  // type `placed_after` in the settings-UI.
  // `large_sized` indicates if the field stretches the entire line (true) or
  // half the line (false).
  struct AddressFormatExtension {
    FieldType type;
    int label_id;
    FieldType placed_after;
    // Usually " " or "\n". Should not be empty.
    std::string_view separator_before_label;
    bool large_sized;
  };

  // Returns the likely country code for `locale`, or "US" as a fallback if no
  // mapping from the locale is available.
  static std::string CountryCodeForLocale(std::string_view locale);

  // Returns an uppercase ISO 3166-1 alpha-2 country code, which represents our
  // best guess for the country a user is likely to use when inputting a new
  // address. This is used as the default in settings and on form import, if no
  // country field was observed in the submitted form.
  static AddressCountryCode GetDefaultCountryCodeForNewAddress(
      const GeoIpCountryCode& geo_ip_country_code,
      std::string_view locale);

  // Gets all the `AddressFormatExtension`s available for `country_code()`.
  base::span<const AddressFormatExtension> address_format_extensions() const;

  // Returns true if the given `field_type` is part of Autofill's address
  // format for `country_code()`.
  bool IsAddressFieldSettingAccessible(FieldType field_type) const;

  // Returns true if the given `field_type` is considered required.
  // Not to be confused with libaddressinput's requirements, it has its
  // own set of required fields.
  bool IsAddressFieldRequired(FieldType field_type) const;

  // The `country_code` provided to the constructor, with aliases like "GB"
  // replaced by their canonical version ("UK", in this case).
  const std::string& country_code() const { return country_code_; }

  // Returns the name of the country translated into the `locale` provided to
  // the constructor. If no `locale` was provided, an empty string is returned.
  const std::u16string& name() const { return name_; }

  // City is expected in a complete address for this country.
  bool requires_city() const {
    return (required_fields_for_address_import_ & ADDRESS_REQUIRES_CITY) != 0;
  }

  // State is expected in a complete address for this country.
  bool requires_state() const {
    return (required_fields_for_address_import_ & ADDRESS_REQUIRES_STATE) != 0;
  }

  // Zip is expected in a complete address for this country.
  bool requires_zip() const {
    return (required_fields_for_address_import_ & ADDRESS_REQUIRES_ZIP) != 0;
  }

  // An address line1 is expected in a complete address for this country.
  bool requires_line1() const {
    return (required_fields_for_address_import_ & ADDRESS_REQUIRES_LINE1) != 0;
  }

  // True if a complete address is expected to either contain a state or a ZIP
  // code. Not true if the address explicitly needs both.
  bool requires_zip_or_state() const {
    return (required_fields_for_address_import_ &
            ADDRESS_REQUIRES_ZIP_OR_STATE) != 0;
  }

  bool requires_line1_or_house_number() const {
    return (required_fields_for_address_import_ &
            ADDRESS_REQUIRES_LINE1_OR_HOUSE_NUMBER);
  }

 private:
  // The two-letter ISO-3166 country code.
  std::string country_code_;

  // The country's name, localized to the app locale.
  std::u16string name_;

  // Required fields for an address import for the country.
  RequiredFieldsForAddressImport required_fields_for_address_import_;
};

LogBuffer& operator<<(LogBuffer& buffer, const AutofillCountry& country);

}  // namespace autofill

#endif  // COMPONENTS_AUTOFILL_CORE_BROWSER_GEO_AUTOFILL_COUNTRY_H_