Chromium Code Reviews| 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_CHROME_ADDRESS_VALIDATOR_H_ |
| 6 #define THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_PRELOAD_ADDRESS_VALIDATOR_H_ | 6 #define THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_CHROME_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 } |
| 27 } // namespace addressinput | 26 } |
| 28 } // namespace i18n | |
| 29 | 27 |
| 30 namespace autofill { | 28 namespace autofill { |
| 31 | 29 |
| 32 class Suggestions; | 30 class InputSuggester; |
| 31 class LoadRulesListener; | |
|
Evan Stade
2014/07/07 21:59:07
you can define LoadRulesListener in this file
please use gerrit instead
2014/07/08 22:47:58
Done.
| |
| 33 | 32 |
| 34 // Interface to the libaddressinput AddressValidator for Chromium Autofill. | 33 // Interface to the libaddressinput AddressValidator for Chromium Autofill. The |
| 35 class PreloadAddressValidator { | 34 // class is named AddressValidator to simplify switching between libaddressinput |
| 35 // and this version. | |
| 36 // | |
| 37 // It's not possible to name this file address_validator.h because some | |
| 38 // compilers do not handle multiple files with the same name (although in | |
| 39 // different directories) gracefully. This class is a shim between upstream | |
| 40 // libaddressinput API and the API that Chrome expects, hence the file name | |
| 41 // chrome_address_validator.h. | |
| 42 class AddressValidator { | |
| 36 public: | 43 public: |
| 37 typedef ::i18n::addressinput::Callback<std::string, int> Callback; | |
| 38 | |
| 39 // The status of address validation. | 44 // The status of address validation. |
| 40 enum Status { | 45 enum Status { |
| 41 // Address validation completed successfully. Check |problems| to see if any | 46 // Address validation completed successfully. Check |problems| to see if any |
| 42 // problems were found. | 47 // problems were found. |
| 43 SUCCESS, | 48 SUCCESS, |
| 44 | 49 |
| 45 // The validation rules are not available, because LoadRules() was not | 50 // The validation rules are not available, because LoadRules() was not |
| 46 // called or failed. Reload the rules. | 51 // called or failed. Reload the rules. |
| 47 RULES_UNAVAILABLE, | 52 RULES_UNAVAILABLE, |
| 48 | 53 |
| 49 // The validation rules are being loaded. Try again later. | 54 // The validation rules are being loaded. Try again later. |
| 50 RULES_NOT_READY | 55 RULES_NOT_READY |
| 51 }; | 56 }; |
| 52 | 57 |
| 53 // Takes ownership of |downloader| and |storage|. | 58 // Takes ownership of |downloader| and |storage|. |
| 54 PreloadAddressValidator( | 59 AddressValidator(const std::string& validation_data_url, |
| 55 const std::string& validation_data_url, | 60 scoped_ptr< ::i18n::addressinput::Downloader> downloader, |
| 56 scoped_ptr< ::i18n::addressinput::Downloader> downloader, | 61 scoped_ptr< ::i18n::addressinput::Storage> storage, |
| 57 scoped_ptr< ::i18n::addressinput::Storage> storage); | 62 LoadRulesListener* load_rules_listener); |
| 58 | 63 |
| 59 virtual ~PreloadAddressValidator(); | 64 virtual ~AddressValidator(); |
| 60 | 65 |
| 61 // Loads the generic validation rules for |region_code| and specific rules | 66 // Loads the generic validation rules for |region_code| and specific rules |
| 62 // for the regions's administrative areas, localities, and dependent | 67 // for the region's administrative areas, localities, and dependent |
| 63 // localities. A typical data size is 10KB. The largest is 250KB. If a region | 68 // 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. | 69 // has language-specific validation rules, then these are also loaded. |
| 65 // | 70 // |
| 66 // Example rule: | 71 // Example rule: |
| 67 // https://i18napis.appspot.com/ssl-aggregate-address/data/US | 72 // https://i18napis.appspot.com/ssl-aggregate-address/data/US |
| 68 // | 73 // |
| 69 // If the rules are already in progress of being loaded, it does nothing. | 74 // If the rules are already in progress of being loaded, it does nothing. |
| 70 // Calls |loaded| when the loading has finished. | 75 // Invokes |load_rules_listener| when the loading has finished. |
| 71 virtual void LoadRules(const std::string& region_code, | 76 virtual void LoadRules(const std::string& region_code); |
| 72 const Callback& loaded); | |
| 73 | 77 |
| 74 // Validates the |address| and populates |problems| with the validation | 78 // Validates the |address| and populates |problems| with the validation |
| 75 // problems, filtered according to the |filter| parameter. | 79 // problems, filtered according to the |filter| parameter. |
| 76 // | 80 // |
| 77 // If the |filter| is empty, then all discovered validation problems are | 81 // If the |filter| is empty, then all discovered validation problems are |
| 78 // returned. If the |filter| contains problem elements, then only the problems | 82 // returned. If the |filter| contains problem elements, then only the problems |
| 79 // in the |filter| may be returned. | 83 // in the |filter| may be returned. |
| 80 virtual Status Validate( | 84 virtual Status ValidateAddress( |
| 81 const ::i18n::addressinput::AddressData& address, | 85 const ::i18n::addressinput::AddressData& address, |
| 82 const ::i18n::addressinput::FieldProblemMap* filter, | 86 const ::i18n::addressinput::FieldProblemMap* filter, |
| 83 ::i18n::addressinput::FieldProblemMap* problems) const; | 87 ::i18n::addressinput::FieldProblemMap* problems) const; |
| 84 | 88 |
| 85 // Fills in |suggestions| for the partially typed in |user_input|, assuming | 89 // 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| | 90 // 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. | 91 // is over the |suggestion_limit|, then returns no |suggestions| at all. |
| 88 // | 92 // |
| 89 // If the |solutions| parameter is NULL, the checks whether the validation | 93 // If the |solutions| parameter is NULL, the checks whether the validation |
| 90 // rules are available, but does not fill in suggestions. | 94 // rules are available, but does not fill in suggestions. |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 112 size_t suggestion_limit, | 116 size_t suggestion_limit, |
| 113 std::vector< ::i18n::addressinput::AddressData>* suggestions) const; | 117 std::vector< ::i18n::addressinput::AddressData>* suggestions) const; |
| 114 | 118 |
| 115 // Canonicalizes the administrative area in |address_data|. For example, | 119 // Canonicalizes the administrative area in |address_data|. For example, |
| 116 // "texas" changes to "TX". Returns true on success, otherwise leaves | 120 // "texas" changes to "TX". Returns true on success, otherwise leaves |
| 117 // |address_data| alone and returns false. | 121 // |address_data| alone and returns false. |
| 118 virtual bool CanonicalizeAdministrativeArea( | 122 virtual bool CanonicalizeAdministrativeArea( |
| 119 ::i18n::addressinput::AddressData* address) const; | 123 ::i18n::addressinput::AddressData* address) const; |
| 120 | 124 |
| 121 private: | 125 private: |
| 126 friend class MockAddressValidator; | |
| 127 | |
| 128 // Constructor used only for MockAddressValidator. | |
| 129 AddressValidator(); | |
| 130 | |
| 131 // Verifies that |validator_| succeeded. Invoked by |validated_| callback. | |
| 122 void Validated(bool success, | 132 void Validated(bool success, |
| 123 const ::i18n::addressinput::AddressData&, | 133 const ::i18n::addressinput::AddressData&, |
| 124 const ::i18n::addressinput::FieldProblemMap&); | 134 const ::i18n::addressinput::FieldProblemMap&); |
| 125 | 135 |
| 136 // Invokes the |load_rules_listener_|, if it's not NULL. Called by | |
| 137 // |rules_loaded_| callback. | |
| 138 void RulesLoaded(bool success, const std::string& country_code, int); | |
| 139 | |
| 140 // Loads and stores aggregate rules at COUNTRY level. | |
| 126 const scoped_ptr< ::i18n::addressinput::PreloadSupplier> supplier_; | 141 const scoped_ptr< ::i18n::addressinput::PreloadSupplier> supplier_; |
| 127 const scoped_ptr<Suggestions> suggestions_; | 142 |
| 128 const scoped_ptr< ::i18n::addressinput::Synonyms> synonyms_; | 143 // Suggests addresses based on user input. |
| 144 const scoped_ptr<InputSuggester> input_suggester_; | |
| 145 | |
| 146 // Normalizes addresses into a canonical form. | |
| 147 const scoped_ptr< ::i18n::addressinput::AddressNormalizer> normalizer_; | |
| 148 | |
| 149 // Validates addresses. | |
| 129 const scoped_ptr<const ::i18n::addressinput::AddressValidator> validator_; | 150 const scoped_ptr<const ::i18n::addressinput::AddressValidator> validator_; |
| 151 | |
| 152 // The callback that |validator_| invokes when it finished validating an | |
| 153 // address. | |
| 130 const scoped_ptr<const ::i18n::addressinput::AddressValidator::Callback> | 154 const scoped_ptr<const ::i18n::addressinput::AddressValidator::Callback> |
| 131 validated_; | 155 validated_; |
| 132 | 156 |
| 133 friend class MockAddressValidator; | 157 // The callback that |supplier_| invokes when it finished loading rules. |
| 134 PreloadAddressValidator(); | 158 const scoped_ptr<const ::i18n::addressinput::PreloadSupplier::Callback> |
| 159 rules_loaded_; | |
| 135 | 160 |
| 136 DISALLOW_COPY_AND_ASSIGN(PreloadAddressValidator); | 161 // Not owned delegate to invoke when |suppler_| finished loading rules. Can be |
| 162 // NULL. | |
| 163 LoadRulesListener* const load_rules_listener_; | |
| 164 | |
| 165 DISALLOW_COPY_AND_ASSIGN(AddressValidator); | |
| 137 }; | 166 }; |
| 138 | 167 |
| 139 } // namespace autofill | 168 } // namespace autofill |
| 140 | 169 |
| 141 #endif // THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_PRELOAD_ADDRESS_VALIDATOR_H_ | 170 #endif // THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_CHROME_ADDRESS_VALIDATOR_H_ |
| OLD | NEW |