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