| 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. Exposed for testing. |
| 149 virtual base::TimeDelta GetBaseRetryPeriod() const; |
| 150 |
| 151 private: |
| 147 // Verifies that |validator_| succeeded. Invoked by |validated_| callback. | 152 // Verifies that |validator_| succeeded. Invoked by |validated_| callback. |
| 148 void Validated(bool success, | 153 void Validated(bool success, |
| 149 const ::i18n::addressinput::AddressData&, | 154 const ::i18n::addressinput::AddressData&, |
| 150 const ::i18n::addressinput::FieldProblemMap&); | 155 const ::i18n::addressinput::FieldProblemMap&); |
| 151 | 156 |
| 152 // Invokes the |load_rules_listener_|, if it's not NULL. Called by | 157 // Invokes the |load_rules_listener_|, if it's not NULL. Called by |
| 153 // |rules_loaded_| callback. | 158 // |rules_loaded_| callback. |
| 154 void RulesLoaded(bool success, const std::string& country_code, int); | 159 void RulesLoaded(bool success, const std::string& region_code, int); |
| 160 |
| 161 // Retries loading rules without resetting the retry counter. |
| 162 void RetryLoadRules(const std::string& region_code); |
| 155 | 163 |
| 156 // Loads and stores aggregate rules at COUNTRY level. | 164 // Loads and stores aggregate rules at COUNTRY level. |
| 157 const scoped_ptr< ::i18n::addressinput::PreloadSupplier> supplier_; | 165 const scoped_ptr< ::i18n::addressinput::PreloadSupplier> supplier_; |
| 158 | 166 |
| 159 // Suggests addresses based on user input. | 167 // Suggests addresses based on user input. |
| 160 const scoped_ptr<InputSuggester> input_suggester_; | 168 const scoped_ptr<InputSuggester> input_suggester_; |
| 161 | 169 |
| 162 // Normalizes addresses into a canonical form. | 170 // Normalizes addresses into a canonical form. |
| 163 const scoped_ptr< ::i18n::addressinput::AddressNormalizer> normalizer_; | 171 const scoped_ptr< ::i18n::addressinput::AddressNormalizer> normalizer_; |
| 164 | 172 |
| 165 // Validates addresses. | 173 // Validates addresses. |
| 166 const scoped_ptr<const ::i18n::addressinput::AddressValidator> validator_; | 174 const scoped_ptr<const ::i18n::addressinput::AddressValidator> validator_; |
| 167 | 175 |
| 168 // The callback that |validator_| invokes when it finished validating an | 176 // The callback that |validator_| invokes when it finished validating an |
| 169 // address. | 177 // address. |
| 170 const scoped_ptr<const ::i18n::addressinput::AddressValidator::Callback> | 178 const scoped_ptr<const ::i18n::addressinput::AddressValidator::Callback> |
| 171 validated_; | 179 validated_; |
| 172 | 180 |
| 173 // The callback that |supplier_| invokes when it finished loading rules. | 181 // The callback that |supplier_| invokes when it finished loading rules. |
| 174 const scoped_ptr<const ::i18n::addressinput::PreloadSupplier::Callback> | 182 const scoped_ptr<const ::i18n::addressinput::PreloadSupplier::Callback> |
| 175 rules_loaded_; | 183 rules_loaded_; |
| 176 | 184 |
| 177 // Not owned delegate to invoke when |suppler_| finished loading rules. Can be | 185 // Not owned delegate to invoke when |suppler_| finished loading rules. Can be |
| 178 // NULL. | 186 // NULL. |
| 179 LoadRulesListener* const load_rules_listener_; | 187 LoadRulesListener* const load_rules_listener_; |
| 180 | 188 |
| 189 // A mapping of region codes to the number of attempts to retry loading rules. |
| 190 std::map<std::string, int> attempts_number_; |
| 191 |
| 192 // Member variables should appear before the WeakPtrFactory, to ensure that |
| 193 // any WeakPtrs to AddressValidator are invalidated before its members |
| 194 // variable's destructors are executed, rendering them invalid. |
| 195 base::WeakPtrFactory<AddressValidator> weak_factory_; |
| 196 |
| 181 DISALLOW_COPY_AND_ASSIGN(AddressValidator); | 197 DISALLOW_COPY_AND_ASSIGN(AddressValidator); |
| 182 }; | 198 }; |
| 183 | 199 |
| 184 } // namespace autofill | 200 } // namespace autofill |
| 185 | 201 |
| 186 #endif // THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_CHROME_ADDRESS_VALIDATOR_H_ | 202 #endif // THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_CHROME_ADDRESS_VALIDATOR_H_ |
| OLD | NEW |