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 <cstddef> |
| 6 #include <string> |
| 7 #include <vector> |
| 8 |
| 9 #include "base/basictypes.h" |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "third_party/libaddressinput/chromium/preload_address_validator.h" |
| 12 #include "third_party/libaddressinput/chromium/suggestions.h" |
| 13 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_da
ta.h" |
| 14 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_va
lidator.h" |
| 15 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/callback.h
" |
| 16 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/downloader
.h" |
| 17 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/preload_su
pplier.h" |
| 18 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h" |
| 19 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/synonyms.h
" |
| 20 |
| 21 namespace autofill { |
| 22 |
| 23 using ::i18n::addressinput::AddressData; |
| 24 using ::i18n::addressinput::AddressField; |
| 25 using ::i18n::addressinput::AddressValidator; |
| 26 using ::i18n::addressinput::BuildCallback; |
| 27 using ::i18n::addressinput::Downloader; |
| 28 using ::i18n::addressinput::FieldProblemMap; |
| 29 using ::i18n::addressinput::PreloadSupplier; |
| 30 using ::i18n::addressinput::Storage; |
| 31 using ::i18n::addressinput::Synonyms; |
| 32 |
| 33 PreloadAddressValidator::PreloadAddressValidator( |
| 34 const std::string& validation_data_url, |
| 35 scoped_ptr<Downloader> downloader, |
| 36 scoped_ptr<Storage> storage) |
| 37 : supplier_( |
| 38 new PreloadSupplier( |
| 39 validation_data_url, |
| 40 downloader.release(), |
| 41 storage.release())), |
| 42 suggestions_( |
| 43 new Suggestions( |
| 44 supplier_.get())), |
| 45 synonyms_( |
| 46 new Synonyms( |
| 47 supplier_.get())), |
| 48 validator_( |
| 49 new AddressValidator( |
| 50 supplier_.get())), |
| 51 validated_(BuildCallback(this, &PreloadAddressValidator::Validated)) { |
| 52 } |
| 53 |
| 54 PreloadAddressValidator::~PreloadAddressValidator() { |
| 55 } |
| 56 |
| 57 void PreloadAddressValidator::LoadRules(const std::string& region_code, |
| 58 const Callback& loaded) { |
| 59 assert(supplier_ != NULL); |
| 60 supplier_->LoadRules(region_code, loaded); |
| 61 } |
| 62 |
| 63 PreloadAddressValidator::Status PreloadAddressValidator::Validate( |
| 64 const AddressData& address, |
| 65 const FieldProblemMap* filter, |
| 66 FieldProblemMap* problems) const { |
| 67 assert(supplier_ != NULL); |
| 68 assert(validator_ != NULL); |
| 69 |
| 70 if (supplier_->IsPending(address.region_code)) { |
| 71 return RULES_NOT_READY; |
| 72 } |
| 73 |
| 74 if (!supplier_->IsLoaded(address.region_code)) { |
| 75 return RULES_UNAVAILABLE; |
| 76 } |
| 77 |
| 78 validator_->Validate( |
| 79 address, |
| 80 /*allow_postal*/ false, |
| 81 /*require_name*/ false, |
| 82 filter, |
| 83 problems, |
| 84 *validated_); |
| 85 |
| 86 return SUCCESS; |
| 87 } |
| 88 |
| 89 PreloadAddressValidator::Status PreloadAddressValidator::GetSuggestions( |
| 90 const AddressData& user_input, |
| 91 AddressField focused_field, |
| 92 size_t suggestion_limit, |
| 93 std::vector<AddressData>* suggestions) const { |
| 94 assert(suggestions_ != NULL); |
| 95 assert(supplier_ != NULL); |
| 96 |
| 97 if (supplier_->IsPending(user_input.region_code)) { |
| 98 return RULES_NOT_READY; |
| 99 } |
| 100 |
| 101 if (!supplier_->IsLoaded(user_input.region_code)) { |
| 102 return RULES_UNAVAILABLE; |
| 103 } |
| 104 |
| 105 suggestions_->GetSuggestions( |
| 106 user_input, |
| 107 focused_field, |
| 108 suggestion_limit, |
| 109 suggestions); |
| 110 |
| 111 return SUCCESS; |
| 112 } |
| 113 |
| 114 bool PreloadAddressValidator::CanonicalizeAdministrativeArea( |
| 115 AddressData* address) const { |
| 116 assert(address != NULL); |
| 117 assert(supplier_ != NULL); |
| 118 assert(synonyms_ != NULL); |
| 119 |
| 120 if (!supplier_->IsLoaded(address->region_code)) { |
| 121 return false; |
| 122 } |
| 123 |
| 124 // TODO: It would probably be beneficial to use the full canonicalization. |
| 125 AddressData tmp(*address); |
| 126 synonyms_->NormalizeForDisplay(&tmp); |
| 127 address->administrative_area = tmp.administrative_area; |
| 128 |
| 129 return true; |
| 130 } |
| 131 |
| 132 void PreloadAddressValidator::Validated(bool success, |
| 133 const AddressData&, |
| 134 const FieldProblemMap&) { |
| 135 assert(success); |
| 136 } |
| 137 |
| 138 // Stub constructor for use by MockAddressValidator. |
| 139 PreloadAddressValidator::PreloadAddressValidator() { |
| 140 } |
| 141 |
| 142 } // namespace autofill |
OLD | NEW |