OLD | NEW |
1 // Copyright (C) 2013 Google Inc. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // found in the LICENSE file. |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 | 4 |
15 #ifndef I18N_ADDRESSINPUT_ADDRESS_VALIDATOR_H_ | 5 #ifndef THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_PRELOAD_ADDRESS_VALIDATOR_H_ |
16 #define I18N_ADDRESSINPUT_ADDRESS_VALIDATOR_H_ | 6 #define THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_PRELOAD_ADDRESS_VALIDATOR_H_ |
17 | 7 |
18 #include <libaddressinput/address_field.h> | 8 #include <cstddef> |
19 #include <libaddressinput/address_problem.h> | |
20 #include <libaddressinput/util/scoped_ptr.h> | |
21 | |
22 #include <map> | |
23 #include <string> | 9 #include <string> |
24 #include <vector> | 10 #include <vector> |
25 | 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 |
26 namespace i18n { | 18 namespace i18n { |
27 namespace addressinput { | 19 namespace addressinput { |
28 | 20 |
29 class Downloader; | 21 class Downloader; |
30 class LoadRulesDelegate; | 22 class PreloadSupplier; |
31 class Storage; | 23 class Storage; |
| 24 class Synonyms; |
32 struct AddressData; | 25 struct AddressData; |
33 | 26 |
34 typedef std::vector<AddressProblem> AddressProblems; | 27 } // namespace addressinput |
35 typedef std::multimap<AddressField, AddressProblem::Type> AddressProblemFilter; | 28 } // namespace i18n |
36 | 29 |
37 // Validates an AddressData structure. Sample usage: | 30 namespace autofill { |
38 // class MyClass : public LoadRulesDelegate { | 31 |
39 // public: | 32 class Suggestions; |
40 // MyClass() : validator_(AddressValidator::Build( | 33 |
41 // scoped_ptr<Downloader>(new MyDownloader), | 34 // Interface to the libaddressinput AddressValidator for Chromium Autofill. |
42 // scoped_ptr<Storage>(new MyStorage), | 35 class PreloadAddressValidator { |
43 // this)) { | |
44 // validator_->LoadRules("US"); | |
45 // } | |
46 // | |
47 // virtual ~MyClass() {} | |
48 // | |
49 // virtual void OnAddressValidationRulesLoaded( | |
50 // const std::string& country_code, | |
51 // bool success) { | |
52 // ... | |
53 // } | |
54 // | |
55 // void ValidateAddress() { | |
56 // AddressData address; | |
57 // address.country_code = "US"; | |
58 // address.administrative_area = "CA"; | |
59 // AddressProblems problems; | |
60 // AddressProblemFilter filter; | |
61 // AddressValidator::Status status = | |
62 // validator_->ValidateAddress(address, filter, &problems); | |
63 // if (status == AddressValidator::SUCCESS) { | |
64 // Process(problems); | |
65 // } | |
66 // } | |
67 // | |
68 // private: | |
69 // scoped_ptr<AddressValidator> validator_; | |
70 // }; | |
71 class AddressValidator { | |
72 public: | 36 public: |
| 37 typedef ::i18n::addressinput::Callback<std::string, int> Callback; |
| 38 |
73 // The status of address validation. | 39 // The status of address validation. |
74 enum Status { | 40 enum Status { |
75 // Address validation completed successfully. Check |problems| to see if any | 41 // Address validation completed successfully. Check |problems| to see if any |
76 // problems were found. | 42 // problems were found. |
77 SUCCESS, | 43 SUCCESS, |
78 | 44 |
79 // The validation rules are not available, because LoadRules() was not | 45 // The validation rules are not available, because LoadRules() was not |
80 // called or failed. Reload the rules. | 46 // called or failed. Reload the rules. |
81 RULES_UNAVAILABLE, | 47 RULES_UNAVAILABLE, |
82 | 48 |
83 // The validation rules are being loaded. Try again later. | 49 // The validation rules are being loaded. Try again later. |
84 RULES_NOT_READY | 50 RULES_NOT_READY |
85 }; | 51 }; |
86 | 52 |
87 virtual ~AddressValidator(); | 53 // Takes ownership of |downloader| and |storage|. |
| 54 PreloadAddressValidator( |
| 55 const std::string& validation_data_url, |
| 56 scoped_ptr< ::i18n::addressinput::Downloader> downloader, |
| 57 scoped_ptr< ::i18n::addressinput::Storage> storage); |
88 | 58 |
89 // Builds an address validator. Takes ownership of |downloader| and |storage|, | 59 virtual ~PreloadAddressValidator(); |
90 // which cannot be NULL. Does not take ownership of |load_rules_delegate|, | |
91 // which can be NULL. The caller owns the result. | |
92 static scoped_ptr<AddressValidator> Build( | |
93 scoped_ptr<Downloader> downloader, | |
94 scoped_ptr<Storage> storage, | |
95 LoadRulesDelegate* load_rules_delegate); | |
96 | 60 |
97 // Loads the generic validation rules for |country_code| and specific rules | 61 // Loads the generic validation rules for |region_code| and specific rules |
98 // for the country's administrative areas, localities, and dependent | 62 // for the regions's administrative areas, localities, and dependent |
99 // localities. A typical data size is 10KB. The largest is 250KB. If a country | 63 // localities. A typical data size is 10KB. The largest is 250KB. If a region |
100 // has language-specific validation rules, then these are also loaded. | 64 // has language-specific validation rules, then these are also loaded. |
101 // | 65 // |
102 // Example rule: | 66 // Example rule: |
103 // https://i18napis.appspot.com/ssl-aggregate-address/data/US | 67 // https://i18napis.appspot.com/ssl-aggregate-address/data/US |
104 // | 68 // |
105 // If the rules were loaded successfully before or are still being loaded, | 69 // If the rules are already in progress of being loaded, it does nothing. |
106 // then does nothing. Notifies |load_rules_delegate| when the loading | 70 // Calls |loaded| when the loading has finished. |
107 // finishes. | 71 virtual void LoadRules(const std::string& region_code, |
108 virtual void LoadRules(const std::string& country_code) = 0; | 72 const Callback& loaded); |
109 | 73 |
110 // Validates the |address| and populates |problems| with the validation | 74 // Validates the |address| and populates |problems| with the validation |
111 // problems, filtered according to the |filter| parameter. | 75 // problems, filtered according to the |filter| parameter. |
112 // | 76 // |
113 // If the |filter| is empty, then all discovered validation problems are | 77 // If the |filter| is empty, then all discovered validation problems are |
114 // returned. If the |filter| contains problem elements, then only the problems | 78 // returned. If the |filter| contains problem elements, then only the problems |
115 // in the |filter| may be returned. | 79 // in the |filter| may be returned. |
116 // | 80 virtual Status Validate( |
117 // If the |problems| parameter is NULL, then checks whether the validation | 81 const ::i18n::addressinput::AddressData& address, |
118 // rules are available, but does not validate the |address|. | 82 const ::i18n::addressinput::FieldProblemMap* filter, |
119 virtual Status ValidateAddress(const AddressData& address, | 83 ::i18n::addressinput::FieldProblemMap* problems) const; |
120 const AddressProblemFilter& filter, | |
121 AddressProblems* problems) const = 0; | |
122 | 84 |
123 // Fills in |suggestions| for the partially typed in |user_input|, assuming | 85 // Fills in |suggestions| for the partially typed in |user_input|, assuming |
124 // the user is typing in the |focused_field|. If the number of |suggestions| | 86 // the user is typing in the |focused_field|. If the number of |suggestions| |
125 // is over the |suggestion_limit|, then returns no |suggestions| at all. | 87 // is over the |suggestion_limit|, then returns no |suggestions| at all. |
126 // | 88 // |
127 // If the |solutions| parameter is NULL, the checks whether the validation | 89 // If the |solutions| parameter is NULL, the checks whether the validation |
128 // rules are available, but does not fill in suggestions. | 90 // rules are available, but does not fill in suggestions. |
129 // | 91 // |
130 // Sample user input 1: | 92 // Sample user input 1: |
131 // country code = "US" | 93 // country code = "US" |
132 // postal code = "90066" | 94 // postal code = "90066" |
133 // focused field = POSTAL_CODE | 95 // focused field = POSTAL_CODE |
134 // suggestions limit = 1 | 96 // suggestions limit = 1 |
135 // Suggestion: | 97 // Suggestion: |
136 // [{administrative_area: "CA"}] | 98 // [{administrative_area: "CA"}] |
137 // | 99 // |
138 // Sample user input 2: | 100 // Sample user input 2: |
139 // country code = "CN" | 101 // country code = "CN" |
140 // dependent locality = "Zongyang" | 102 // dependent locality = "Zongyang" |
141 // focused field = DEPENDENT_LOCALITY | 103 // focused field = DEPENDENT_LOCALITY |
142 // suggestions limit = 10 | 104 // suggestions limit = 10 |
143 // Suggestion: | 105 // Suggestion: |
144 // [{dependent_locality: "Zongyang Xian", | 106 // [{dependent_locality: "Zongyang Xian", |
145 // locality: "Anqing Shi", | 107 // locality: "Anqing Shi", |
146 // administrative_area: "Anhui Sheng"}] | 108 // administrative_area: "Anhui Sheng"}] |
147 virtual Status GetSuggestions( | 109 virtual Status GetSuggestions( |
148 const AddressData& user_input, | 110 const ::i18n::addressinput::AddressData& user_input, |
149 AddressField focused_field, | 111 ::i18n::addressinput::AddressField focused_field, |
150 size_t suggestion_limit, | 112 size_t suggestion_limit, |
151 std::vector<AddressData>* suggestions) const = 0; | 113 std::vector< ::i18n::addressinput::AddressData>* suggestions) const; |
152 | 114 |
153 // Canonicalizes the administrative area in |address_data|. For example, | 115 // Canonicalizes the administrative area in |address_data|. For example, |
154 // "texas" changes to "TX". Returns true on success, otherwise leaves | 116 // "texas" changes to "TX". Returns true on success, otherwise leaves |
155 // |address_data| alone and returns false. | 117 // |address_data| alone and returns false. |
156 virtual bool CanonicalizeAdministrativeArea(AddressData* address_data) | 118 virtual bool CanonicalizeAdministrativeArea( |
157 const = 0; | 119 ::i18n::addressinput::AddressData* address) const; |
| 120 |
| 121 private: |
| 122 void Validated(bool success, |
| 123 const ::i18n::addressinput::AddressData&, |
| 124 const ::i18n::addressinput::FieldProblemMap&); |
| 125 |
| 126 const scoped_ptr< ::i18n::addressinput::PreloadSupplier> supplier_; |
| 127 const scoped_ptr<Suggestions> suggestions_; |
| 128 const scoped_ptr< ::i18n::addressinput::Synonyms> synonyms_; |
| 129 const scoped_ptr<const ::i18n::addressinput::AddressValidator> validator_; |
| 130 const scoped_ptr<const ::i18n::addressinput::AddressValidator::Callback> |
| 131 validated_; |
| 132 |
| 133 friend class MockAddressValidator; |
| 134 PreloadAddressValidator(); |
| 135 |
| 136 DISALLOW_COPY_AND_ASSIGN(PreloadAddressValidator); |
158 }; | 137 }; |
159 | 138 |
160 } // namespace addressinput | 139 } // namespace autofill |
161 } // namespace i18n | |
162 | 140 |
163 #endif // I18N_ADDRESSINPUT_ADDRESS_VALIDATOR_H_ | 141 #endif // THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_PRELOAD_ADDRESS_VALIDATOR_H_ |
OLD | NEW |