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/input_suggester.h" |
| 16 #include "third_party/libaddressinput/chromium/libaddressinput_util.h" |
| 17 #include "third_party/libaddressinput/chromium/load_rules_listener.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_no
rmalizer.h" |
| 21 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_va
lidator.h" |
| 22 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/callback.h
" |
| 23 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/downloader
.h" |
| 24 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/preload_su
pplier.h" |
| 25 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h" |
| 26 |
| 27 namespace autofill { |
| 28 |
| 29 using ::i18n::addressinput::AddressData; |
| 30 using ::i18n::addressinput::AddressField; |
| 31 using ::i18n::addressinput::AddressNormalizer; |
| 32 using ::i18n::addressinput::BuildCallback; |
| 33 using ::i18n::addressinput::Downloader; |
| 34 using ::i18n::addressinput::FieldProblemMap; |
| 35 using ::i18n::addressinput::PreloadSupplier; |
| 36 using ::i18n::addressinput::Storage; |
| 37 |
| 38 using ::i18n::addressinput::ADMIN_AREA; |
| 39 using ::i18n::addressinput::DEPENDENT_LOCALITY; |
| 40 using ::i18n::addressinput::POSTAL_CODE; |
| 41 |
| 42 AddressValidator::AddressValidator(const std::string& validation_data_url, |
| 43 scoped_ptr<Downloader> downloader, |
| 44 scoped_ptr<Storage> storage, |
| 45 LoadRulesListener* load_rules_listener) |
| 46 : supplier_(new PreloadSupplier(validation_data_url, |
| 47 downloader.release(), |
| 48 storage.release())), |
| 49 input_suggester_(new InputSuggester(supplier_.get())), |
| 50 normalizer_(new AddressNormalizer(supplier_.get())), |
| 51 validator_(new ::i18n::addressinput::AddressValidator(supplier_.get())), |
| 52 validated_(BuildCallback(this, &AddressValidator::Validated)), |
| 53 rules_loaded_(BuildCallback(this, &AddressValidator::RulesLoaded)), |
| 54 load_rules_listener_(load_rules_listener) {} |
| 55 |
| 56 AddressValidator::~AddressValidator() {} |
| 57 |
| 58 void AddressValidator::LoadRules(const std::string& region_code) { |
| 59 DCHECK(supplier_); |
| 60 supplier_->LoadRules(region_code, *rules_loaded_); |
| 61 } |
| 62 |
| 63 AddressValidator::Status AddressValidator::ValidateAddress( |
| 64 const AddressData& address, |
| 65 const FieldProblemMap* filter, |
| 66 FieldProblemMap* problems) const { |
| 67 DCHECK(supplier_); |
| 68 DCHECK(validator_); |
| 69 |
| 70 if (supplier_->IsPending(address.region_code)) { |
| 71 if (problems) |
| 72 addressinput::ValidateRequiredFields(address, filter, problems); |
| 73 return RULES_NOT_READY; |
| 74 } |
| 75 |
| 76 if (!supplier_->IsLoaded(address.region_code)) { |
| 77 if (problems) |
| 78 addressinput::ValidateRequiredFields(address, filter, problems); |
| 79 return RULES_UNAVAILABLE; |
| 80 } |
| 81 |
| 82 if (!problems) |
| 83 return SUCCESS; |
| 84 |
| 85 validator_->Validate(address, |
| 86 true, // Allow postal office boxes. |
| 87 true, // Require recipient name. |
| 88 filter, |
| 89 problems, |
| 90 *validated_); |
| 91 |
| 92 return SUCCESS; |
| 93 } |
| 94 |
| 95 AddressValidator::Status AddressValidator::GetSuggestions( |
| 96 const AddressData& user_input, |
| 97 AddressField focused_field, |
| 98 size_t suggestion_limit, |
| 99 std::vector<AddressData>* suggestions) const { |
| 100 DCHECK(supplier_); |
| 101 DCHECK(input_suggester_); |
| 102 |
| 103 if (supplier_->IsPending(user_input.region_code)) |
| 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 if (focused_field == POSTAL_CODE || |
| 115 (focused_field >= ADMIN_AREA && focused_field <= DEPENDENT_LOCALITY)) { |
| 116 input_suggester_->GetSuggestions( |
| 117 user_input, focused_field, suggestion_limit, suggestions); |
| 118 } |
| 119 |
| 120 return SUCCESS; |
| 121 } |
| 122 |
| 123 bool AddressValidator::CanonicalizeAdministrativeArea( |
| 124 AddressData* address) const { |
| 125 DCHECK(address); |
| 126 DCHECK(supplier_); |
| 127 DCHECK(normalizer_); |
| 128 |
| 129 if (!supplier_->IsLoaded(address->region_code)) |
| 130 return false; |
| 131 |
| 132 // TODO: It would probably be beneficial to use the full canonicalization. |
| 133 AddressData tmp(*address); |
| 134 normalizer_->Normalize(&tmp); |
| 135 address->administrative_area = tmp.administrative_area; |
| 136 |
| 137 return true; |
| 138 } |
| 139 |
| 140 AddressValidator::AddressValidator() : load_rules_listener_(NULL) {} |
| 141 |
| 142 void AddressValidator::Validated(bool success, |
| 143 const AddressData&, |
| 144 const FieldProblemMap&) { |
| 145 DCHECK(success); |
| 146 } |
| 147 |
| 148 void AddressValidator::RulesLoaded(bool success, |
| 149 const std::string& country_code, |
| 150 int) { |
| 151 if (load_rules_listener_) |
| 152 load_rules_listener_->OnAddressValidationRulesLoaded(country_code, success); |
| 153 } |
| 154 |
| 155 } // namespace autofill |
OLD | NEW |