OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_CHROME_ADDRESS_VALIDATOR_H_ | 5 #ifndef THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_CHROME_ADDRESS_VALIDATOR_H_ |
6 #define THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_CHROME_ADDRESS_VALIDATOR_H_ | 6 #define THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_CHROME_ADDRESS_VALIDATOR_H_ |
7 | 7 |
8 #include <cstddef> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/memory/weak_ptr.h" | |
15 #include "base/time/time.h" | |
14 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_fi eld.h" | 16 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_fi eld.h" |
15 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_va lidator.h" | 17 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_va lidator.h" |
16 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/callback.h " | 18 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/callback.h " |
17 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/preload_su pplier.h" | 19 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/preload_su pplier.h" |
18 | 20 |
19 namespace i18n { | 21 namespace i18n { |
20 namespace addressinput { | 22 namespace addressinput { |
21 class AddressNormalizer; | 23 class AddressNormalizer; |
22 class Downloader; | 24 class Downloader; |
23 class Storage; | 25 class Storage; |
24 struct AddressData; | 26 struct AddressData; |
25 } | 27 } |
26 } | 28 } |
27 | 29 |
28 namespace autofill { | 30 namespace autofill { |
29 | 31 |
30 class InputSuggester; | 32 class InputSuggester; |
31 | 33 |
32 // The object to be notified when loading of address validation rules is | 34 // The object to be notified when loading of address validation rules is |
33 // finished. | 35 // finished. |
34 class LoadRulesListener { | 36 class LoadRulesListener { |
35 public: | 37 public: |
36 virtual ~LoadRulesListener() {} | 38 virtual ~LoadRulesListener() {} |
37 | 39 |
38 // Called when the validation rules for the |country_code| have been loaded. | 40 // Called when the validation rules for the |region_code| have been loaded. |
39 // The validation rules include the generic rules for the |country_code| and | 41 // The validation rules include the generic rules for the |region_code| and |
40 // specific rules for the country's administrative areas, localities, and | 42 // specific rules for the country's administrative areas, localities, and |
41 // dependent localities. If a country has language-specific validation rules, | 43 // dependent localities. If a country has language-specific validation rules, |
42 // then these are also loaded. | 44 // then these are also loaded. |
43 // | 45 // |
44 // The |success| parameter is true when the rules were loaded successfully. | 46 // The |success| parameter is true when the rules were loaded successfully. |
45 virtual void OnAddressValidationRulesLoaded(const std::string& country_code, | 47 virtual void OnAddressValidationRulesLoaded(const std::string& region_code, |
46 bool success) = 0; | 48 bool success) = 0; |
47 }; | 49 }; |
48 | 50 |
49 // Interface to the libaddressinput AddressValidator for Chromium Autofill. The | 51 // Interface to the libaddressinput AddressValidator for Chromium Autofill. The |
50 // class is named AddressValidator to simplify switching between libaddressinput | 52 // class is named AddressValidator to simplify switching between libaddressinput |
51 // and this version. | 53 // and this version. |
52 // | 54 // |
53 // It's not possible to name this file address_validator.h because some | 55 // It's not possible to name this file address_validator.h because some |
54 // compilers do not handle multiple files with the same name (although in | 56 // compilers do not handle multiple files with the same name (although in |
55 // different directories) gracefully. This class is a shim between upstream | 57 // different directories) gracefully. This class is a shim between upstream |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
131 ::i18n::addressinput::AddressField focused_field, | 133 ::i18n::addressinput::AddressField focused_field, |
132 size_t suggestion_limit, | 134 size_t suggestion_limit, |
133 std::vector< ::i18n::addressinput::AddressData>* suggestions) const; | 135 std::vector< ::i18n::addressinput::AddressData>* suggestions) const; |
134 | 136 |
135 // Canonicalizes the administrative area in |address_data|. For example, | 137 // Canonicalizes the administrative area in |address_data|. For example, |
136 // "texas" changes to "TX". Returns true on success, otherwise leaves | 138 // "texas" changes to "TX". Returns true on success, otherwise leaves |
137 // |address_data| alone and returns false. | 139 // |address_data| alone and returns false. |
138 virtual bool CanonicalizeAdministrativeArea( | 140 virtual bool CanonicalizeAdministrativeArea( |
139 ::i18n::addressinput::AddressData* address) const; | 141 ::i18n::addressinput::AddressData* address) const; |
140 | 142 |
141 private: | 143 protected: |
142 friend class MockAddressValidator; | |
143 | |
144 // Constructor used only for MockAddressValidator. | 144 // Constructor used only for MockAddressValidator. |
145 AddressValidator(); | 145 AddressValidator(); |
146 | 146 |
147 // Returns the period of time to wait between the first attempt's failure and | |
148 // the second attempt's initiation to load rules. Overridden in | |
Evan Stade
2014/07/16 23:35:06
nit: s/Overridden.../Exposed for testing.
please use gerrit instead
2014/07/16 23:45:17
Done.
| |
149 // FailingAddressValidatorTest. | |
150 virtual base::TimeDelta GetBaseRetryPeriod() const; | |
151 | |
152 private: | |
147 // Verifies that |validator_| succeeded. Invoked by |validated_| callback. | 153 // Verifies that |validator_| succeeded. Invoked by |validated_| callback. |
148 void Validated(bool success, | 154 void Validated(bool success, |
149 const ::i18n::addressinput::AddressData&, | 155 const ::i18n::addressinput::AddressData&, |
150 const ::i18n::addressinput::FieldProblemMap&); | 156 const ::i18n::addressinput::FieldProblemMap&); |
151 | 157 |
152 // Invokes the |load_rules_listener_|, if it's not NULL. Called by | 158 // Invokes the |load_rules_listener_|, if it's not NULL. Called by |
153 // |rules_loaded_| callback. | 159 // |rules_loaded_| callback. |
154 void RulesLoaded(bool success, const std::string& country_code, int); | 160 void RulesLoaded(bool success, const std::string& region_code, int); |
161 | |
162 // Retries loading rules without resetting the retry counter. | |
163 void RetryLoadRules(const std::string& region_code); | |
155 | 164 |
156 // Loads and stores aggregate rules at COUNTRY level. | 165 // Loads and stores aggregate rules at COUNTRY level. |
157 const scoped_ptr< ::i18n::addressinput::PreloadSupplier> supplier_; | 166 const scoped_ptr< ::i18n::addressinput::PreloadSupplier> supplier_; |
158 | 167 |
159 // Suggests addresses based on user input. | 168 // Suggests addresses based on user input. |
160 const scoped_ptr<InputSuggester> input_suggester_; | 169 const scoped_ptr<InputSuggester> input_suggester_; |
161 | 170 |
162 // Normalizes addresses into a canonical form. | 171 // Normalizes addresses into a canonical form. |
163 const scoped_ptr< ::i18n::addressinput::AddressNormalizer> normalizer_; | 172 const scoped_ptr< ::i18n::addressinput::AddressNormalizer> normalizer_; |
164 | 173 |
165 // Validates addresses. | 174 // Validates addresses. |
166 const scoped_ptr<const ::i18n::addressinput::AddressValidator> validator_; | 175 const scoped_ptr<const ::i18n::addressinput::AddressValidator> validator_; |
167 | 176 |
168 // The callback that |validator_| invokes when it finished validating an | 177 // The callback that |validator_| invokes when it finished validating an |
169 // address. | 178 // address. |
170 const scoped_ptr<const ::i18n::addressinput::AddressValidator::Callback> | 179 const scoped_ptr<const ::i18n::addressinput::AddressValidator::Callback> |
171 validated_; | 180 validated_; |
172 | 181 |
173 // The callback that |supplier_| invokes when it finished loading rules. | 182 // The callback that |supplier_| invokes when it finished loading rules. |
174 const scoped_ptr<const ::i18n::addressinput::PreloadSupplier::Callback> | 183 const scoped_ptr<const ::i18n::addressinput::PreloadSupplier::Callback> |
175 rules_loaded_; | 184 rules_loaded_; |
176 | 185 |
177 // Not owned delegate to invoke when |suppler_| finished loading rules. Can be | 186 // Not owned delegate to invoke when |suppler_| finished loading rules. Can be |
178 // NULL. | 187 // NULL. |
179 LoadRulesListener* const load_rules_listener_; | 188 LoadRulesListener* const load_rules_listener_; |
180 | 189 |
190 // A mapping of region codes to the number of attempts to retry loading rules. | |
191 std::map<std::string, int> attempts_number_; | |
192 | |
193 // Member variables should appear before the WeakPtrFactory, to ensure that | |
194 // any WeakPtrs to AddressValidator are invalidated before its members | |
195 // variable's destructors are executed, rendering them invalid. | |
196 base::WeakPtrFactory<AddressValidator> weak_factory_; | |
197 | |
181 DISALLOW_COPY_AND_ASSIGN(AddressValidator); | 198 DISALLOW_COPY_AND_ASSIGN(AddressValidator); |
182 }; | 199 }; |
183 | 200 |
184 } // namespace autofill | 201 } // namespace autofill |
185 | 202 |
186 #endif // THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_CHROME_ADDRESS_VALIDATOR_H_ | 203 #endif // THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_CHROME_ADDRESS_VALIDATOR_H_ |
OLD | NEW |