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/gtest_prod_util.h" | |
12 #include "base/macros.h" | 13 #include "base/macros.h" |
13 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "base/memory/weak_ptr.h" | |
16 #include "base/time/time.h" | |
14 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_fi eld.h" | 17 #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" | 18 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_va lidator.h" |
16 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/callback.h " | 19 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/callback.h " |
17 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/preload_su pplier.h" | 20 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/preload_su pplier.h" |
18 | 21 |
19 namespace i18n { | 22 namespace i18n { |
20 namespace addressinput { | 23 namespace addressinput { |
21 class AddressNormalizer; | 24 class AddressNormalizer; |
22 class Downloader; | 25 class Downloader; |
23 class Storage; | 26 class Storage; |
24 struct AddressData; | 27 struct AddressData; |
25 } | 28 } |
26 } | 29 } |
27 | 30 |
28 namespace autofill { | 31 namespace autofill { |
29 | 32 |
30 class InputSuggester; | 33 class InputSuggester; |
31 | 34 |
32 // The object to be notified when loading of address validation rules is | 35 // The object to be notified when loading of address validation rules is |
33 // finished. | 36 // finished. |
34 class LoadRulesListener { | 37 class LoadRulesListener { |
35 public: | 38 public: |
36 virtual ~LoadRulesListener() {} | 39 virtual ~LoadRulesListener() {} |
37 | 40 |
38 // Called when the validation rules for the |country_code| have been loaded. | 41 // 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 | 42 // The validation rules include the generic rules for the |region_code| and |
40 // specific rules for the country's administrative areas, localities, and | 43 // specific rules for the country's administrative areas, localities, and |
41 // dependent localities. If a country has language-specific validation rules, | 44 // dependent localities. If a country has language-specific validation rules, |
42 // then these are also loaded. | 45 // then these are also loaded. |
43 // | 46 // |
44 // The |success| parameter is true when the rules were loaded successfully. | 47 // The |success| parameter is true when the rules were loaded successfully. |
45 virtual void OnAddressValidationRulesLoaded(const std::string& country_code, | 48 virtual void OnAddressValidationRulesLoaded(const std::string& region_code, |
46 bool success) = 0; | 49 bool success) = 0; |
47 }; | 50 }; |
48 | 51 |
49 // Interface to the libaddressinput AddressValidator for Chromium Autofill. The | 52 // Interface to the libaddressinput AddressValidator for Chromium Autofill. The |
50 // class is named AddressValidator to simplify switching between libaddressinput | 53 // class is named AddressValidator to simplify switching between libaddressinput |
51 // and this version. | 54 // and this version. |
52 // | 55 // |
53 // It's not possible to name this file address_validator.h because some | 56 // 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 | 57 // compilers do not handle multiple files with the same name (although in |
55 // different directories) gracefully. This class is a shim between upstream | 58 // different directories) gracefully. This class is a shim between upstream |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
132 size_t suggestion_limit, | 135 size_t suggestion_limit, |
133 std::vector< ::i18n::addressinput::AddressData>* suggestions) const; | 136 std::vector< ::i18n::addressinput::AddressData>* suggestions) const; |
134 | 137 |
135 // Canonicalizes the administrative area in |address_data|. For example, | 138 // Canonicalizes the administrative area in |address_data|. For example, |
136 // "texas" changes to "TX". Returns true on success, otherwise leaves | 139 // "texas" changes to "TX". Returns true on success, otherwise leaves |
137 // |address_data| alone and returns false. | 140 // |address_data| alone and returns false. |
138 virtual bool CanonicalizeAdministrativeArea( | 141 virtual bool CanonicalizeAdministrativeArea( |
139 ::i18n::addressinput::AddressData* address) const; | 142 ::i18n::addressinput::AddressData* address) const; |
140 | 143 |
141 private: | 144 private: |
145 friend class FailingAddressValidatorTest; | |
142 friend class MockAddressValidator; | 146 friend class MockAddressValidator; |
143 | 147 |
148 // Encapsulates the logic for retrying to load rules with exponential backoff | |
149 // and a maxium number of attempts. | |
150 class Retrier { | |
Evan Stade
2014/07/16 02:11:25
I don't think this class is worthwhile. Just add t
please use gerrit instead
2014/07/16 23:22:03
Done.
| |
151 public: | |
152 explicit Retrier(const base::WeakPtr<AddressValidator>& rule_loader); | |
153 ~Retrier(); | |
154 | |
155 // Resets the number of attempts to enable retrying loading rules again. | |
156 void ResetRetryCount(const std::string& region_code); | |
157 | |
158 // Retries loading rules after a delay, unless the maximum number of | |
159 // attempts is exceeded. | |
160 void RetryLoadRules(const std::string& region_code); | |
161 | |
162 // Sets the period of time to wait between the first and second attempts to | |
163 // load rules, if the first attempt failed. | |
164 void set_retry_period(const base::TimeDelta& retry_period) { | |
165 retry_period_ = retry_period; | |
166 } | |
167 | |
168 private: | |
169 // Rule loader used when retrying to load rules. | |
170 base::WeakPtr<AddressValidator> rule_loader_; | |
Evan Stade
2014/07/16 02:11:25
this member is only necessary because of the priva
please use gerrit instead
2014/07/16 23:22:03
Done.
| |
171 | |
172 // A mapping of region codes to the number of attempts to retry loading | |
173 // rules for the region code. | |
174 std::map<std::string, int> attempts_number_; | |
175 | |
176 // The period of time to wait between the first and second attempts to load | |
177 // rules. | |
178 base::TimeDelta retry_period_; | |
Evan Stade
2014/07/16 02:11:25
This isn't very useful. It's actually a static val
please use gerrit instead
2014/07/16 23:22:03
Interesting idea. I like it. Done.
| |
179 | |
180 DISALLOW_COPY_AND_ASSIGN(Retrier); | |
181 }; | |
182 | |
144 // Constructor used only for MockAddressValidator. | 183 // Constructor used only for MockAddressValidator. |
145 AddressValidator(); | 184 AddressValidator(); |
146 | 185 |
147 // Verifies that |validator_| succeeded. Invoked by |validated_| callback. | 186 // Verifies that |validator_| succeeded. Invoked by |validated_| callback. |
148 void Validated(bool success, | 187 void Validated(bool success, |
149 const ::i18n::addressinput::AddressData&, | 188 const ::i18n::addressinput::AddressData&, |
150 const ::i18n::addressinput::FieldProblemMap&); | 189 const ::i18n::addressinput::FieldProblemMap&); |
151 | 190 |
152 // Invokes the |load_rules_listener_|, if it's not NULL. Called by | 191 // Invokes the |load_rules_listener_|, if it's not NULL. Called by |
153 // |rules_loaded_| callback. | 192 // |rules_loaded_| callback. |
154 void RulesLoaded(bool success, const std::string& country_code, int); | 193 void RulesLoaded(bool success, const std::string& region_code, int); |
194 | |
195 // Retries loading rules without resetting the retry counter. | |
196 void RetryLoadRules(const std::string& region_code); | |
155 | 197 |
156 // Loads and stores aggregate rules at COUNTRY level. | 198 // Loads and stores aggregate rules at COUNTRY level. |
157 const scoped_ptr< ::i18n::addressinput::PreloadSupplier> supplier_; | 199 const scoped_ptr< ::i18n::addressinput::PreloadSupplier> supplier_; |
158 | 200 |
159 // Suggests addresses based on user input. | 201 // Suggests addresses based on user input. |
160 const scoped_ptr<InputSuggester> input_suggester_; | 202 const scoped_ptr<InputSuggester> input_suggester_; |
161 | 203 |
162 // Normalizes addresses into a canonical form. | 204 // Normalizes addresses into a canonical form. |
163 const scoped_ptr< ::i18n::addressinput::AddressNormalizer> normalizer_; | 205 const scoped_ptr< ::i18n::addressinput::AddressNormalizer> normalizer_; |
164 | 206 |
165 // Validates addresses. | 207 // Validates addresses. |
166 const scoped_ptr<const ::i18n::addressinput::AddressValidator> validator_; | 208 const scoped_ptr<const ::i18n::addressinput::AddressValidator> validator_; |
167 | 209 |
168 // The callback that |validator_| invokes when it finished validating an | 210 // The callback that |validator_| invokes when it finished validating an |
169 // address. | 211 // address. |
170 const scoped_ptr<const ::i18n::addressinput::AddressValidator::Callback> | 212 const scoped_ptr<const ::i18n::addressinput::AddressValidator::Callback> |
171 validated_; | 213 validated_; |
172 | 214 |
173 // The callback that |supplier_| invokes when it finished loading rules. | 215 // The callback that |supplier_| invokes when it finished loading rules. |
174 const scoped_ptr<const ::i18n::addressinput::PreloadSupplier::Callback> | 216 const scoped_ptr<const ::i18n::addressinput::PreloadSupplier::Callback> |
175 rules_loaded_; | 217 rules_loaded_; |
176 | 218 |
177 // Not owned delegate to invoke when |suppler_| finished loading rules. Can be | 219 // Not owned delegate to invoke when |suppler_| finished loading rules. Can be |
178 // NULL. | 220 // NULL. |
179 LoadRulesListener* const load_rules_listener_; | 221 LoadRulesListener* const load_rules_listener_; |
180 | 222 |
223 // Retries loading rules after a failed attempt. | |
224 scoped_ptr<Retrier> retrier_; | |
225 | |
226 // Member variables should appear before the WeakPtrFactory, to ensure that | |
227 // any WeakPtrs to AddressValidator are invalidated before its members | |
228 // variable's destructors are executed, rendering them invalid. | |
229 base::WeakPtrFactory<AddressValidator> weak_factory_; | |
230 | |
181 DISALLOW_COPY_AND_ASSIGN(AddressValidator); | 231 DISALLOW_COPY_AND_ASSIGN(AddressValidator); |
182 }; | 232 }; |
183 | 233 |
184 } // namespace autofill | 234 } // namespace autofill |
185 | 235 |
186 #endif // THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_CHROME_ADDRESS_VALIDATOR_H_ | 236 #endif // THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_CHROME_ADDRESS_VALIDATOR_H_ |
OLD | NEW |