summaryrefslogtreecommitdiff
path: root/chromium/components/autofill/core/browser/geo/alternative_state_name_map.h
blob: 1ed11eaa6c6634db0f85db0272bf4005fb9add36 (plain)
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
// Copyright 2020 The Chromium Authors. All rights reserved.
// 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_ALTERNATIVE_STATE_NAME_MAP_H_
#define COMPONENTS_AUTOFILL_CORE_BROWSER_GEO_ALTERNATIVE_STATE_NAME_MAP_H_

#include "components/autofill/core/browser/proto/states.pb.h"

#include "base/i18n/case_conversion.h"
#include "base/no_destructor.h"
#include "base/optional.h"
#include "base/sequence_checker.h"
#include "base/strings/string16.h"
#include "base/util/type_safety/strong_alias.h"

namespace autofill {
// AlternativeStateNameMap encapsulates mappings from state names in the
// profiles to their localized and the abbreviated names.
//
// AlternativeStateNameMap is used for the filling of state fields, comparison
// of profiles, determining mergeability of the address profiles and required
// |ADDRESS_HOME_STATE| votes to be sent to the server.
//
// AlternativeStateNameMap can provide the following data for the states:
//  1. The state string stored in the address profile denoted by
//       state_string_from_profile in this class.
//  2. The canonical state name (StateEntry::canonical_name) which acts as the
//       unique identifier representing the state (unique within a country).
//  3. The abbreviations of the state (StateEntry::abbreviations).
//  4. The alternative names of the state (StateEntry::alternative_names).
//
// StateEntry holds the information about the abbreviations and the
// alternative names of the state which is determined after comparison with the
// state values saved in the address profiles if they match.
//
// The main map |localized_state_names_map_| maps the tuple
// (country_code, canonical state name) as the key to the corresponding
// StateEntry object (with the information about the abbreviations and the
// alternative names) as the value.
//
// The |localized_state_names_reverse_lookup_map_| takes in the
// country_code and StateEntry::name, StateEntry::abbreviations or
// ::alternative_names as the key and the canonical state name as the value.
//
// Example: Considering "California" as the state_string_from_profile and
//          the corresponding StateEntry object:
//             {
//                'canonical_name': 'California',
//                'abbreviations': ['CA'],
//                'alternate_names': ['The Golden State']
//             }
//
//          1. StateEntry::canonical_name (i.e "California" in this case) acts
//              as the canonical state name.
//          2. ("US", "California") acts as the key and the above StateEntry
//              object is added as the value in the
//              |localized_state_names_map_|.
//          3. Entries added to |localized_state_names_reverse_lookup_map_|
//              are:
//               a. ("US", "California") -> "California"
//               b. ("US", "CA") -> "California"
//               c. ("US", "TheGoldenState") -> "California"
//
// Example: Assuming the user creates an unknown state in the profile
//          "Random State".
//          1. Entries added to the |localized_state_names_map_| are:
//              ("RandomState", Empty StateEntry object)
//          2. Nothing is added to the
//              |localized_state_names_reverse_lookup_map_| in this case
class AlternativeStateNameMap {
 public:
  // Represents ISO 3166-1 alpha-2 codes (always uppercase ASCII).
  using CountryCode = util::StrongAlias<class CountryCodeTag, std::string>;

  // Represents either a canonical state name, or an abbreviation, or an
  // alternative name or normalized state name from the profile.
  using StateName = util::StrongAlias<class StateNameTag, base::string16>;

  // States can be represented as different strings (different spellings,
  // translations, abbreviations). All representations of a single state in a
  // single country are mapped to the same canonical name.
  using CanonicalStateName =
      util::StrongAlias<class CanonicalStateNameTag, base::string16>;

  static AlternativeStateNameMap* GetInstance();

  ~AlternativeStateNameMap() = delete;
  AlternativeStateNameMap(const AlternativeStateNameMap&) = delete;
  AlternativeStateNameMap& operator=(const AlternativeStateNameMap&) = delete;

  // Removes |kCharsToStrip| from |text| and returns the normalized text.
  static StateName NormalizeStateName(const StateName& text);

  // Returns the canonical name (StateEntry::canonical_name) from the
  // |localized_state_names_map_| based on
  // (|country_code|, |state_name|).
  base::Optional<CanonicalStateName> GetCanonicalStateName(
      const CountryCode& country_code,
      const StateName& state_name,
      bool is_state_name_normalized = false) const;

  // Returns the value present in |localized_state_names_map_| corresponding
  // to (|country_code|, |state_string_from_profile|). In case, the entry does
  // not exist in the map, base::nullopt is returned.
  base::Optional<StateEntry> GetEntry(
      const CountryCode& country_code,
      const StateName& state_string_from_profile) const;

  // Adds ((|country_code|, state key), |state_entry|) to the
  // |localized_state_names_map_|, where state key corresponds to
  // |normalized_canonical_state_name| if it is not null, or to
  // |normalized_state_value_from_profile| otherwise.
  // If |normalized_canonical_state_name| is not null, each entry from
  // |normalized_alternative_state_names| is added as a tuple
  // ((|country_code|, entry), |normalized_canonical_state_name|) to the
  // |localized_state_names_reverse_lookup_map_|.
  void AddEntry(
      const CountryCode& country_code,
      const StateName& normalized_state_value_from_profile,
      const StateEntry& state_entry,
      const std::vector<StateName>& normalized_alternative_state_names,
      CanonicalStateName* normalized_canonical_state_name);

  // Returns true if the |localized_state_names_map_| is empty.
  bool IsLocalisedStateNamesMapEmpty() const;

#if defined(UNIT_TEST)
  // Clears the map for testing purposes.
  void ClearAlternativeStateNameMapForTesting() {
    ClearAlternativeStateNameMap();
  }
#endif

 private:
  AlternativeStateNameMap();

  // Clears the |localized_state_names_map_| and
  // |localized_state_names_reverse_lookup_map_|.
  // Used only for testing purposes.
  void ClearAlternativeStateNameMap();

  // A custom comparator for the
  // |localized_state_names_reverse_lookup_map_| that ignores the case
  // of the string on comparisons.
  struct CaseInsensitiveLessComparator {
    bool operator()(const std::pair<CountryCode, StateName>& lhs,
                    const std::pair<CountryCode, StateName>& rhs) const {
      // Compares the country codes that are always uppercase ASCII.
      if (lhs.first != rhs.first)
        return lhs.first.value() < rhs.first.value();

      return base::i18n::ToLower(lhs.second.value()) <
             base::i18n::ToLower(rhs.second.value());
    }
  };

  // Since the constructor is private, |base::NoDestructor| must be friend to be
  // allowed to construct the class.
  friend class base::NoDestructor<AlternativeStateNameMap>;

  // A map that stores the alternative state names. The map is keyed
  // by the country_code and the canonical state name (or
  // normalized_state_value_from_profile in case no canonical state name is
  // known) while the value is the StateEntry object.
  std::map<std::pair<CountryCode, CanonicalStateName>, StateEntry>
      localized_state_names_map_;

  // The map is keyed by the country_code and the abbreviation or
  // canonical name or the alternative name of the state.
  std::map<std::pair<CountryCode, StateName>,
           CanonicalStateName,
           CaseInsensitiveLessComparator>
      localized_state_names_reverse_lookup_map_;

  SEQUENCE_CHECKER(alternative_state_name_map_sequence_checker_);
};

}  // namespace autofill

#endif  // COMPONENTS_AUTOFILL_CORE_BROWSER_GEO_ALTERNATIVE_STATE_NAME_MAP_H_