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 #include "third_party/libaddressinput/chromium/preload_address_validator.h" |
| 6 |
5 #include <cstddef> | 7 #include <cstddef> |
6 #include <string> | 8 #include <string> |
7 #include <vector> | 9 #include <vector> |
8 | 10 |
9 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
11 | 14 #include "third_party/libaddressinput/chromium/load_rules_delegate.h" |
12 #define I18N_ADDRESSINPUT_UTIL_BASICTYPES_H_ | |
13 #include "third_party/libaddressinput/chromium/preload_address_validator.h" | |
14 #include "third_party/libaddressinput/chromium/suggestions.h" | 15 #include "third_party/libaddressinput/chromium/suggestions.h" |
15 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_da
ta.h" | 16 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_da
ta.h" |
| 17 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_in
put_helper.h" |
| 18 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_no
rmalizer.h" |
16 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_va
lidator.h" | 19 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_va
lidator.h" |
17 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/callback.h
" | 20 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/callback.h
" |
18 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/downloader
.h" | 21 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/downloader
.h" |
19 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/preload_su
pplier.h" | 22 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/preload_su
pplier.h" |
20 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h" | 23 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h" |
21 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/synonyms.h
" | |
22 | 24 |
23 namespace autofill { | 25 namespace autofill { |
24 | 26 |
25 using ::i18n::addressinput::AddressData; | 27 using ::i18n::addressinput::AddressData; |
26 using ::i18n::addressinput::AddressField; | 28 using ::i18n::addressinput::AddressField; |
27 using ::i18n::addressinput::AddressValidator; | 29 using ::i18n::addressinput::AddressInputHelper; |
| 30 using ::i18n::addressinput::AddressNormalizer; |
| 31 using ::i18n::addressinput::ADMIN_AREA; |
28 using ::i18n::addressinput::BuildCallback; | 32 using ::i18n::addressinput::BuildCallback; |
| 33 using ::i18n::addressinput::DEPENDENT_LOCALITY; |
29 using ::i18n::addressinput::Downloader; | 34 using ::i18n::addressinput::Downloader; |
30 using ::i18n::addressinput::FieldProblemMap; | 35 using ::i18n::addressinput::FieldProblemMap; |
| 36 using ::i18n::addressinput::INVALID_FORMAT; |
| 37 using ::i18n::addressinput::MISMATCHING_VALUE; |
| 38 using ::i18n::addressinput::POSTAL_CODE; |
31 using ::i18n::addressinput::PreloadSupplier; | 39 using ::i18n::addressinput::PreloadSupplier; |
32 using ::i18n::addressinput::Storage; | 40 using ::i18n::addressinput::Storage; |
33 using ::i18n::addressinput::Synonyms; | |
34 | 41 |
35 PreloadAddressValidator::PreloadAddressValidator( | 42 AddressValidator::AddressValidator(const std::string& validation_data_url, |
36 const std::string& validation_data_url, | 43 scoped_ptr<Downloader> downloader, |
37 scoped_ptr<Downloader> downloader, | 44 scoped_ptr<Storage> storage, |
38 scoped_ptr<Storage> storage) | 45 LoadRulesDelegate* load_rules_delegate) |
39 : supplier_( | 46 : supplier_(new PreloadSupplier(validation_data_url, downloader.release(), |
40 new PreloadSupplier( | 47 storage.release())), |
41 validation_data_url, | 48 suggestions_(new Suggestions(supplier_.get())), |
42 downloader.release(), | 49 normalizer_(new AddressNormalizer(supplier_.get())), |
43 storage.release())), | 50 validator_(new ::i18n::addressinput::AddressValidator(supplier_.get())), |
44 suggestions_( | 51 input_helper_(new AddressInputHelper(supplier_.get())), |
45 new Suggestions( | 52 validated_(BuildCallback(this, &AddressValidator::Validated)), |
46 supplier_.get())), | 53 rules_loaded_(BuildCallback(this, &AddressValidator::RulesLoaded)), |
47 synonyms_( | 54 load_rules_delegate_(load_rules_delegate) {} |
48 new Synonyms( | 55 |
49 supplier_.get())), | 56 AddressValidator::~AddressValidator() {} |
50 validator_( | 57 |
51 new AddressValidator( | 58 void AddressValidator::LoadRules(const std::string& region_code) { |
52 supplier_.get())), | 59 DCHECK(supplier_); |
53 validated_(BuildCallback(this, &PreloadAddressValidator::Validated)) { | 60 supplier_->LoadRules(region_code, *rules_loaded_); |
54 } | 61 } |
55 | 62 |
56 PreloadAddressValidator::~PreloadAddressValidator() { | 63 AddressValidator::Status AddressValidator::ValidateAddress( |
57 } | |
58 | |
59 void PreloadAddressValidator::LoadRules(const std::string& region_code, | |
60 const Callback& loaded) { | |
61 assert(supplier_ != NULL); | |
62 supplier_->LoadRules(region_code, loaded); | |
63 } | |
64 | |
65 PreloadAddressValidator::Status PreloadAddressValidator::Validate( | |
66 const AddressData& address, | 64 const AddressData& address, |
67 const FieldProblemMap* filter, | 65 const FieldProblemMap* filter, |
68 FieldProblemMap* problems) const { | 66 FieldProblemMap* problems) const { |
69 assert(supplier_ != NULL); | 67 DCHECK(supplier_); |
70 assert(validator_ != NULL); | 68 DCHECK(validator_); |
| 69 DCHECK(normalizer_); |
71 | 70 |
72 if (supplier_->IsPending(address.region_code)) { | 71 if (supplier_->IsPending(address.region_code)) |
73 return RULES_NOT_READY; | 72 return RULES_NOT_READY; |
74 } | |
75 | 73 |
76 if (!supplier_->IsLoaded(address.region_code)) { | 74 if (!supplier_->IsLoaded(address.region_code)) |
77 return RULES_UNAVAILABLE; | 75 return RULES_UNAVAILABLE; |
78 } | 76 |
| 77 if (!problems) |
| 78 return SUCCESS; |
| 79 |
| 80 AddressData normalized_address = address; |
| 81 normalizer_->Normalize(&normalized_address); |
79 | 82 |
80 validator_->Validate( | 83 validator_->Validate( |
81 address, | 84 normalized_address, |
82 /*allow_postal*/ false, | 85 /*allow_postal*/ false, |
83 /*require_name*/ false, | 86 /*require_name*/ false, |
84 filter, | 87 filter, |
85 problems, | 88 problems, |
86 *validated_); | 89 *validated_); |
87 | 90 |
88 return SUCCESS; | 91 return SUCCESS; |
89 } | 92 } |
90 | 93 |
91 PreloadAddressValidator::Status PreloadAddressValidator::GetSuggestions( | 94 AddressValidator::Status AddressValidator::GetSuggestions( |
92 const AddressData& user_input, | 95 const AddressData& user_input, |
93 AddressField focused_field, | 96 AddressField focused_field, |
94 size_t suggestion_limit, | 97 size_t suggestion_limit, |
95 std::vector<AddressData>* suggestions) const { | 98 std::vector<AddressData>* suggestions) const { |
96 assert(suggestions_ != NULL); | 99 DCHECK(suggestions_); |
97 assert(supplier_ != NULL); | 100 DCHECK(input_helper_); |
| 101 DCHECK(supplier_); |
98 | 102 |
99 if (supplier_->IsPending(user_input.region_code)) { | 103 if (supplier_->IsPending(user_input.region_code)) |
100 return RULES_NOT_READY; | 104 return RULES_NOT_READY; |
| 105 |
| 106 if (!supplier_->IsLoaded(user_input.region_code)) |
| 107 return RULES_UNAVAILABLE; |
| 108 |
| 109 if (!suggestions) |
| 110 return SUCCESS; |
| 111 |
| 112 suggestions->clear(); |
| 113 |
| 114 AddressData address_copy = user_input; |
| 115 FieldProblemMap filter; |
| 116 FieldProblemMap problems; |
| 117 if (focused_field == POSTAL_CODE) { |
| 118 filter.insert(std::make_pair(POSTAL_CODE, INVALID_FORMAT)); |
| 119 |
| 120 Status status = ValidateAddress(address_copy, &filter, &problems); |
| 121 DCHECK(status == SUCCESS); |
| 122 (void)status; |
| 123 |
| 124 if (!problems.empty()) |
| 125 return SUCCESS; |
| 126 |
| 127 input_helper_->FillAddress(&address_copy); |
101 } | 128 } |
102 | 129 |
103 if (!supplier_->IsLoaded(user_input.region_code)) { | 130 if (focused_field == POSTAL_CODE || |
104 return RULES_UNAVAILABLE; | 131 (focused_field >= ADMIN_AREA && focused_field <= DEPENDENT_LOCALITY)) { |
| 132 suggestions_->GetSuggestions(address_copy, focused_field, suggestion_limit, |
| 133 suggestions); |
105 } | 134 } |
106 | 135 |
107 suggestions_->GetSuggestions( | 136 filter.clear(); |
108 user_input, | 137 filter.insert(std::make_pair(POSTAL_CODE, MISMATCHING_VALUE)); |
109 focused_field, | 138 std::vector<AddressData> valid_postal_codes; |
110 suggestion_limit, | 139 for (std::vector<AddressData>::const_iterator suggestion_it = |
111 suggestions); | 140 suggestions->begin(); |
| 141 suggestion_it != suggestions->end(); |
| 142 ++suggestion_it) { |
| 143 problems.clear(); |
| 144 |
| 145 Status status = ValidateAddress(address_copy, &filter, &problems); |
| 146 DCHECK(status == SUCCESS); |
| 147 (void)status; |
| 148 |
| 149 if (problems.empty()) |
| 150 valid_postal_codes.push_back(*suggestion_it); |
| 151 } |
| 152 suggestions->swap(valid_postal_codes); |
112 | 153 |
113 return SUCCESS; | 154 return SUCCESS; |
114 } | 155 } |
115 | 156 |
116 bool PreloadAddressValidator::CanonicalizeAdministrativeArea( | 157 bool AddressValidator::CanonicalizeAdministrativeArea( |
117 AddressData* address) const { | 158 AddressData* address) const { |
118 assert(address != NULL); | 159 DCHECK(address); |
119 assert(supplier_ != NULL); | 160 DCHECK(supplier_); |
120 assert(synonyms_ != NULL); | 161 DCHECK(normalizer_); |
121 | 162 |
122 if (!supplier_->IsLoaded(address->region_code)) { | 163 if (!supplier_->IsLoaded(address->region_code)) |
123 return false; | 164 return false; |
124 } | |
125 | 165 |
126 // TODO: It would probably be beneficial to use the full canonicalization. | 166 // TODO: It would probably be beneficial to use the full canonicalization. |
127 AddressData tmp(*address); | 167 AddressData tmp(*address); |
128 synonyms_->NormalizeForDisplay(&tmp); | 168 normalizer_->Normalize(&tmp); |
129 address->administrative_area = tmp.administrative_area; | 169 address->administrative_area = tmp.administrative_area; |
130 | 170 |
131 return true; | 171 return true; |
132 } | 172 } |
133 | 173 |
134 void PreloadAddressValidator::Validated(bool success, | 174 void AddressValidator::Validated(bool success, |
135 const AddressData&, | 175 const AddressData&, |
136 const FieldProblemMap&) { | 176 const FieldProblemMap&) { |
137 assert(success); | 177 DCHECK(success); |
| 178 } |
| 179 |
| 180 void AddressValidator::RulesLoaded(bool success, |
| 181 const std::string& country_code, |
| 182 const int&) { |
| 183 if (load_rules_delegate_) |
| 184 load_rules_delegate_->OnAddressValidationRulesLoaded(country_code, success); |
138 } | 185 } |
139 | 186 |
140 // Stub constructor for use by MockAddressValidator. | 187 // Stub constructor for use by MockAddressValidator. |
141 PreloadAddressValidator::PreloadAddressValidator() { | 188 AddressValidator::AddressValidator() : load_rules_delegate_(NULL) {} |
142 } | |
143 | 189 |
144 } // namespace autofill | 190 } // namespace autofill |
OLD | NEW |