| 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_PRELOAD_ADDRESS_VALIDATOR_H_ | 5 #ifndef THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_PRELOAD_ADDRESS_VALIDATOR_H_ |
| 6 #define THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_PRELOAD_ADDRESS_VALIDATOR_H_ | 6 #define THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_PRELOAD_ADDRESS_VALIDATOR_H_ |
| 7 | 7 |
| 8 #include <cstddef> | 8 #include <cstddef> |
| 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 "third_party/libaddressinput/src/cpp/include/libaddressinput/address_fi
eld.h" | 14 #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" | 15 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_va
lidator.h" |
| 16 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/callback.h
" | 16 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/callback.h
" |
| 17 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/preload_su
pplier.h" |
| 17 | 18 |
| 18 namespace i18n { | 19 namespace i18n { |
| 19 namespace addressinput { | 20 namespace addressinput { |
| 20 | 21 class AddressNormalizer; |
| 21 class Downloader; | 22 class Downloader; |
| 22 class PreloadSupplier; | |
| 23 class Storage; | 23 class Storage; |
| 24 class Synonyms; | |
| 25 struct AddressData; | 24 struct AddressData; |
| 26 | 25 struct AddressInputHelper; |
| 27 } // namespace addressinput | 26 } |
| 28 } // namespace i18n | 27 } |
| 29 | 28 |
| 30 namespace autofill { | 29 namespace autofill { |
| 31 | 30 |
| 32 class Suggestions; | 31 class InputSuggester; |
| 32 class LoadRulesDelegate; |
| 33 | 33 |
| 34 // Interface to the libaddressinput AddressValidator for Chromium Autofill. | 34 // Interface to the libaddressinput AddressValidator for Chromium Autofill. The |
| 35 class PreloadAddressValidator { | 35 // class is named AddressValidator to simplify switching between libaddressinput |
| 36 // and this version. |
| 37 // |
| 38 // It's not possible to name this file address_validator.h because some |
| 39 // compilers do not handle multiple files with the same name (although in |
| 40 // different directories) gracefully. This class uses preloaded validation rules |
| 41 // instead of loading them on demand, hence the file name |
| 42 // preload_address_validator.h. |
| 43 class AddressValidator { |
| 36 public: | 44 public: |
| 37 typedef ::i18n::addressinput::Callback<std::string, int> Callback; | |
| 38 | |
| 39 // The status of address validation. | 45 // The status of address validation. |
| 40 enum Status { | 46 enum Status { |
| 41 // Address validation completed successfully. Check |problems| to see if any | 47 // Address validation completed successfully. Check |problems| to see if any |
| 42 // problems were found. | 48 // problems were found. |
| 43 SUCCESS, | 49 SUCCESS, |
| 44 | 50 |
| 45 // The validation rules are not available, because LoadRules() was not | 51 // The validation rules are not available, because LoadRules() was not |
| 46 // called or failed. Reload the rules. | 52 // called or failed. Reload the rules. |
| 47 RULES_UNAVAILABLE, | 53 RULES_UNAVAILABLE, |
| 48 | 54 |
| 49 // The validation rules are being loaded. Try again later. | 55 // The validation rules are being loaded. Try again later. |
| 50 RULES_NOT_READY | 56 RULES_NOT_READY |
| 51 }; | 57 }; |
| 52 | 58 |
| 53 // Takes ownership of |downloader| and |storage|. | 59 // Takes ownership of |downloader| and |storage|. |
| 54 PreloadAddressValidator( | 60 AddressValidator(const std::string& validation_data_url, |
| 55 const std::string& validation_data_url, | 61 scoped_ptr< ::i18n::addressinput::Downloader> downloader, |
| 56 scoped_ptr< ::i18n::addressinput::Downloader> downloader, | 62 scoped_ptr< ::i18n::addressinput::Storage> storage, |
| 57 scoped_ptr< ::i18n::addressinput::Storage> storage); | 63 LoadRulesDelegate* load_rules_delegate); |
| 58 | 64 |
| 59 virtual ~PreloadAddressValidator(); | 65 virtual ~AddressValidator(); |
| 60 | 66 |
| 61 // Loads the generic validation rules for |region_code| and specific rules | 67 // Loads the generic validation rules for |region_code| and specific rules |
| 62 // for the regions's administrative areas, localities, and dependent | 68 // for the regions's administrative areas, localities, and dependent |
| 63 // localities. A typical data size is 10KB. The largest is 250KB. If a region | 69 // localities. A typical data size is 10KB. The largest is 250KB. If a region |
| 64 // has language-specific validation rules, then these are also loaded. | 70 // has language-specific validation rules, then these are also loaded. |
| 65 // | 71 // |
| 66 // Example rule: | 72 // Example rule: |
| 67 // https://i18napis.appspot.com/ssl-aggregate-address/data/US | 73 // https://i18napis.appspot.com/ssl-aggregate-address/data/US |
| 68 // | 74 // |
| 69 // If the rules are already in progress of being loaded, it does nothing. | 75 // If the rules are already in progress of being loaded, it does nothing. |
| 70 // Calls |loaded| when the loading has finished. | 76 // Invokes |load_rules_delegate| when the loading has finished. |
| 71 virtual void LoadRules(const std::string& region_code, | 77 virtual void LoadRules(const std::string& region_code); |
| 72 const Callback& loaded); | |
| 73 | 78 |
| 74 // Validates the |address| and populates |problems| with the validation | 79 // Validates the |address| and populates |problems| with the validation |
| 75 // problems, filtered according to the |filter| parameter. | 80 // problems, filtered according to the |filter| parameter. |
| 76 // | 81 // |
| 77 // If the |filter| is empty, then all discovered validation problems are | 82 // If the |filter| is empty, then all discovered validation problems are |
| 78 // returned. If the |filter| contains problem elements, then only the problems | 83 // returned. If the |filter| contains problem elements, then only the problems |
| 79 // in the |filter| may be returned. | 84 // in the |filter| may be returned. |
| 80 virtual Status Validate( | 85 virtual Status ValidateAddress( |
| 81 const ::i18n::addressinput::AddressData& address, | 86 const ::i18n::addressinput::AddressData& address, |
| 82 const ::i18n::addressinput::FieldProblemMap* filter, | 87 const ::i18n::addressinput::FieldProblemMap* filter, |
| 83 ::i18n::addressinput::FieldProblemMap* problems) const; | 88 ::i18n::addressinput::FieldProblemMap* problems) const; |
| 84 | 89 |
| 85 // Fills in |suggestions| for the partially typed in |user_input|, assuming | 90 // Fills in |suggestions| for the partially typed in |user_input|, assuming |
| 86 // the user is typing in the |focused_field|. If the number of |suggestions| | 91 // the user is typing in the |focused_field|. If the number of |suggestions| |
| 87 // is over the |suggestion_limit|, then returns no |suggestions| at all. | 92 // is over the |suggestion_limit|, then returns no |suggestions| at all. |
| 88 // | 93 // |
| 89 // If the |solutions| parameter is NULL, the checks whether the validation | 94 // If the |solutions| parameter is NULL, the checks whether the validation |
| 90 // rules are available, but does not fill in suggestions. | 95 // rules are available, but does not fill in suggestions. |
| (...skipping 25 matching lines...) Expand all Loading... |
| 116 // "texas" changes to "TX". Returns true on success, otherwise leaves | 121 // "texas" changes to "TX". Returns true on success, otherwise leaves |
| 117 // |address_data| alone and returns false. | 122 // |address_data| alone and returns false. |
| 118 virtual bool CanonicalizeAdministrativeArea( | 123 virtual bool CanonicalizeAdministrativeArea( |
| 119 ::i18n::addressinput::AddressData* address) const; | 124 ::i18n::addressinput::AddressData* address) const; |
| 120 | 125 |
| 121 private: | 126 private: |
| 122 void Validated(bool success, | 127 void Validated(bool success, |
| 123 const ::i18n::addressinput::AddressData&, | 128 const ::i18n::addressinput::AddressData&, |
| 124 const ::i18n::addressinput::FieldProblemMap&); | 129 const ::i18n::addressinput::FieldProblemMap&); |
| 125 | 130 |
| 131 void RulesLoaded(bool success, const std::string& country_code, const int&); |
| 132 |
| 126 const scoped_ptr< ::i18n::addressinput::PreloadSupplier> supplier_; | 133 const scoped_ptr< ::i18n::addressinput::PreloadSupplier> supplier_; |
| 127 const scoped_ptr<Suggestions> suggestions_; | 134 const scoped_ptr<InputSuggester> input_suggester_; |
| 128 const scoped_ptr< ::i18n::addressinput::Synonyms> synonyms_; | 135 const scoped_ptr< ::i18n::addressinput::AddressNormalizer> normalizer_; |
| 129 const scoped_ptr<const ::i18n::addressinput::AddressValidator> validator_; | 136 const scoped_ptr<const ::i18n::addressinput::AddressValidator> validator_; |
| 137 const scoped_ptr<const ::i18n::addressinput::AddressInputHelper> |
| 138 input_helper_; |
| 130 const scoped_ptr<const ::i18n::addressinput::AddressValidator::Callback> | 139 const scoped_ptr<const ::i18n::addressinput::AddressValidator::Callback> |
| 131 validated_; | 140 validated_; |
| 141 const scoped_ptr<const ::i18n::addressinput::PreloadSupplier::Callback> |
| 142 rules_loaded_; |
| 143 LoadRulesDelegate* const load_rules_delegate_; // Not owned. |
| 132 | 144 |
| 133 friend class MockAddressValidator; | 145 friend class MockAddressValidator; |
| 134 PreloadAddressValidator(); | 146 AddressValidator(); |
| 135 | 147 |
| 136 DISALLOW_COPY_AND_ASSIGN(PreloadAddressValidator); | 148 DISALLOW_COPY_AND_ASSIGN(AddressValidator); |
| 137 }; | 149 }; |
| 138 | 150 |
| 139 } // namespace autofill | 151 } // namespace autofill |
| 140 | 152 |
| 141 #endif // THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_PRELOAD_ADDRESS_VALIDATOR_H_ | 153 #endif // THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_PRELOAD_ADDRESS_VALIDATOR_H_ |
| OLD | NEW |