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