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