OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_CHROME_ADDRESS_VALIDATOR_H_ | |
6 #define THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_CHROME_ADDRESS_VALIDATOR_H_ | |
7 | |
8 #include <cstddef> | |
9 #include <string> | |
10 #include <vector> | |
11 | |
12 #include "base/macros.h" | |
13 #include "base/memory/scoped_ptr.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" | |
16 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/callback.h
" | |
17 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/preload_su
pplier.h" | |
18 | |
19 namespace i18n { | |
20 namespace addressinput { | |
21 class AddressNormalizer; | |
22 class Downloader; | |
23 class Storage; | |
24 struct AddressData; | |
25 } | |
26 } | |
27 | |
28 namespace autofill { | |
29 | |
30 class InputSuggester; | |
31 | |
32 // The object to be notified when loading of address validation rules is | |
33 // finished. | |
34 class LoadRulesListener { | |
35 public: | |
36 virtual ~LoadRulesListener() {} | |
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: | |
60 // The status of address validation. | |
61 enum Status { | |
62 // Address validation completed successfully. Check |problems| to see if any | |
63 // problems were found. | |
64 SUCCESS, | |
65 | |
66 // The validation rules are not available, because LoadRules() was not | |
67 // called or failed. Reload the rules. | |
68 RULES_UNAVAILABLE, | |
69 | |
70 // The validation rules are being loaded. Try again later. | |
71 RULES_NOT_READY | |
72 }; | |
73 | |
74 // Takes ownership of |downloader| and |storage|. | |
75 AddressValidator(const std::string& validation_data_url, | |
76 scoped_ptr< ::i18n::addressinput::Downloader> downloader, | |
77 scoped_ptr< ::i18n::addressinput::Storage> storage, | |
78 LoadRulesListener* load_rules_listener); | |
79 | |
80 virtual ~AddressValidator(); | |
81 | |
82 // Loads the generic validation rules for |region_code| and specific rules | |
83 // for the region's administrative areas, localities, and dependent | |
84 // localities. A typical data size is 10KB. The largest is 250KB. If a region | |
85 // has language-specific validation rules, then these are also loaded. | |
86 // | |
87 // Example rule: | |
88 // https://i18napis.appspot.com/ssl-aggregate-address/data/US | |
89 // | |
90 // If the rules are already in progress of being loaded, it does nothing. | |
91 // Invokes |load_rules_listener| when the loading has finished. | |
92 virtual void LoadRules(const std::string& region_code); | |
93 | |
94 // Validates the |address| and populates |problems| with the validation | |
95 // problems, filtered according to the |filter| parameter. | |
96 // | |
97 // If the |filter| is empty, then all discovered validation problems are | |
98 // returned. If the |filter| contains problem elements, then only the problems | |
99 // in the |filter| may be returned. | |
100 virtual Status ValidateAddress( | |
101 const ::i18n::addressinput::AddressData& address, | |
102 const ::i18n::addressinput::FieldProblemMap* filter, | |
103 ::i18n::addressinput::FieldProblemMap* problems) const; | |
104 | |
105 // Fills in |suggestions| for the partially typed in |user_input|, assuming | |
106 // the user is typing in the |focused_field|. If the number of |suggestions| | |
107 // is over the |suggestion_limit|, then returns no |suggestions| at all. | |
108 // | |
109 // If the |solutions| parameter is NULL, the checks whether the validation | |
110 // rules are available, but does not fill in suggestions. | |
111 // | |
112 // Sample user input 1: | |
113 // country code = "US" | |
114 // postal code = "90066" | |
115 // focused field = POSTAL_CODE | |
116 // suggestions limit = 1 | |
117 // Suggestion: | |
118 // [{administrative_area: "CA"}] | |
119 // | |
120 // Sample user input 2: | |
121 // country code = "CN" | |
122 // dependent locality = "Zongyang" | |
123 // focused field = DEPENDENT_LOCALITY | |
124 // suggestions limit = 10 | |
125 // Suggestion: | |
126 // [{dependent_locality: "Zongyang Xian", | |
127 // locality: "Anqing Shi", | |
128 // administrative_area: "Anhui Sheng"}] | |
129 virtual Status GetSuggestions( | |
130 const ::i18n::addressinput::AddressData& user_input, | |
131 ::i18n::addressinput::AddressField focused_field, | |
132 size_t suggestion_limit, | |
133 std::vector< ::i18n::addressinput::AddressData>* suggestions) const; | |
134 | |
135 // Canonicalizes the administrative area in |address_data|. For example, | |
136 // "texas" changes to "TX". Returns true on success, otherwise leaves | |
137 // |address_data| alone and returns false. | |
138 virtual bool CanonicalizeAdministrativeArea( | |
139 ::i18n::addressinput::AddressData* address) const; | |
140 | |
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. | |
148 void Validated(bool success, | |
149 const ::i18n::addressinput::AddressData&, | |
150 const ::i18n::addressinput::FieldProblemMap&); | |
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. | |
157 const scoped_ptr< ::i18n::addressinput::PreloadSupplier> supplier_; | |
158 | |
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. | |
166 const scoped_ptr<const ::i18n::addressinput::AddressValidator> validator_; | |
167 | |
168 // The callback that |validator_| invokes when it finished validating an | |
169 // address. | |
170 const scoped_ptr<const ::i18n::addressinput::AddressValidator::Callback> | |
171 validated_; | |
172 | |
173 // The callback that |supplier_| invokes when it finished loading rules. | |
174 const scoped_ptr<const ::i18n::addressinput::PreloadSupplier::Callback> | |
175 rules_loaded_; | |
176 | |
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); | |
182 }; | |
183 | |
184 } // namespace autofill | |
185 | |
186 #endif // THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_CHROME_ADDRESS_VALIDATOR_H_ | |
OLD | NEW |