Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(133)

Side by Side Diff: third_party/libaddressinput/chromium/preload_address_validator.cc

Issue 298863012: Use upstream libaddressinput in Chrome. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Self review. Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 <cstddef> 5 #include <cstddef>
6 #include <string> 6 #include <string>
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
11
12 #define I18N_ADDRESSINPUT_UTIL_BASICTYPES_H_
13 #include "third_party/libaddressinput/chromium/preload_address_validator.h" 12 #include "third_party/libaddressinput/chromium/preload_address_validator.h"
14 #include "third_party/libaddressinput/chromium/suggestions.h" 13 #include "third_party/libaddressinput/chromium/suggestions.h"
15 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_da ta.h" 14 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_da ta.h"
15 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_in put_helper.h"
16 #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" 17 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_va lidator.h"
17 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/callback.h " 18 #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/downloader .h"
19 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/preload_su pplier.h" 20 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/preload_su pplier.h"
20 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h" 21 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h"
21 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/synonyms.h "
22 22
23 namespace autofill { 23 namespace autofill {
24 24
25 using ::i18n::addressinput::AddressData; 25 using ::i18n::addressinput::AddressData;
26 using ::i18n::addressinput::AddressField; 26 using ::i18n::addressinput::AddressField;
27 using ::i18n::addressinput::AddressInputHelper;
28 using ::i18n::addressinput::AddressNormalizer;
27 using ::i18n::addressinput::AddressValidator; 29 using ::i18n::addressinput::AddressValidator;
28 using ::i18n::addressinput::BuildCallback; 30 using ::i18n::addressinput::BuildCallback;
29 using ::i18n::addressinput::Downloader; 31 using ::i18n::addressinput::Downloader;
30 using ::i18n::addressinput::FieldProblemMap; 32 using ::i18n::addressinput::FieldProblemMap;
33 using ::i18n::addressinput::INVALID_FORMAT;
34 using ::i18n::addressinput::MISMATCHING_VALUE;
35 using ::i18n::addressinput::POSTAL_CODE;
31 using ::i18n::addressinput::PreloadSupplier; 36 using ::i18n::addressinput::PreloadSupplier;
32 using ::i18n::addressinput::Storage; 37 using ::i18n::addressinput::Storage;
33 using ::i18n::addressinput::Synonyms;
34 38
35 PreloadAddressValidator::PreloadAddressValidator( 39 PreloadAddressValidator::PreloadAddressValidator(
36 const std::string& validation_data_url, 40 const std::string& validation_data_url,
37 scoped_ptr<Downloader> downloader, 41 scoped_ptr<Downloader> downloader,
38 scoped_ptr<Storage> storage) 42 scoped_ptr<Storage> storage)
39 : supplier_( 43 : supplier_(
40 new PreloadSupplier( 44 new PreloadSupplier(
41 validation_data_url, 45 validation_data_url,
42 downloader.release(), 46 downloader.release(),
43 storage.release())), 47 storage.release())),
44 suggestions_( 48 suggestions_(
45 new Suggestions( 49 new Suggestions(
46 supplier_.get())), 50 supplier_.get())),
47 synonyms_( 51 normalizer_(
48 new Synonyms( 52 new AddressNormalizer(
49 supplier_.get())), 53 supplier_.get())),
50 validator_( 54 validator_(
51 new AddressValidator( 55 new AddressValidator(
52 supplier_.get())), 56 supplier_.get())),
57 input_helper_(
58 new AddressInputHelper(supplier_.get())),
53 validated_(BuildCallback(this, &PreloadAddressValidator::Validated)) { 59 validated_(BuildCallback(this, &PreloadAddressValidator::Validated)) {
54 } 60 }
55 61
56 PreloadAddressValidator::~PreloadAddressValidator() { 62 PreloadAddressValidator::~PreloadAddressValidator() {}
57 }
58 63
59 void PreloadAddressValidator::LoadRules(const std::string& region_code, 64 void PreloadAddressValidator::LoadRules(const std::string& region_code,
60 const Callback& loaded) { 65 const Callback& loaded) {
61 assert(supplier_ != NULL); 66 DCHECK(supplier_);
62 supplier_->LoadRules(region_code, loaded); 67 supplier_->LoadRules(region_code, loaded);
63 } 68 }
64 69
65 PreloadAddressValidator::Status PreloadAddressValidator::Validate( 70 PreloadAddressValidator::Status PreloadAddressValidator::Validate(
66 const AddressData& address, 71 const AddressData& address,
67 const FieldProblemMap* filter, 72 const FieldProblemMap* filter,
68 FieldProblemMap* problems) const { 73 FieldProblemMap* problems) const {
69 assert(supplier_ != NULL); 74 DCHECK(supplier_);
70 assert(validator_ != NULL); 75 DCHECK(validator_);
76 DCHECK(normalizer_);
71 77
72 if (supplier_->IsPending(address.region_code)) { 78 if (supplier_->IsPending(address.region_code))
73 return RULES_NOT_READY; 79 return RULES_NOT_READY;
74 }
75 80
76 if (!supplier_->IsLoaded(address.region_code)) { 81 if (!supplier_->IsLoaded(address.region_code))
77 return RULES_UNAVAILABLE; 82 return RULES_UNAVAILABLE;
78 } 83
84 AddressData normalized_address = address;
85 normalizer_->Normalize(&normalized_address);
79 86
80 validator_->Validate( 87 validator_->Validate(
81 address, 88 normalized_address,
82 /*allow_postal*/ false, 89 /*allow_postal*/ false,
83 /*require_name*/ false, 90 /*require_name*/ false,
84 filter, 91 filter,
85 problems, 92 problems,
86 *validated_); 93 *validated_);
87 94
88 return SUCCESS; 95 return SUCCESS;
89 } 96 }
90 97
91 PreloadAddressValidator::Status PreloadAddressValidator::GetSuggestions( 98 PreloadAddressValidator::Status PreloadAddressValidator::GetSuggestions(
92 const AddressData& user_input, 99 const AddressData& user_input,
93 AddressField focused_field, 100 AddressField focused_field,
94 size_t suggestion_limit, 101 size_t suggestion_limit,
95 std::vector<AddressData>* suggestions) const { 102 std::vector<AddressData>* suggestions) const {
96 assert(suggestions_ != NULL); 103 DCHECK(suggestions);
97 assert(supplier_ != NULL); 104 DCHECK(suggestions_);
105 DCHECK(input_helper_);
106 DCHECK(supplier_);
98 107
99 if (supplier_->IsPending(user_input.region_code)) { 108 if (supplier_->IsPending(user_input.region_code))
100 return RULES_NOT_READY; 109 return RULES_NOT_READY;
101 }
102 110
103 if (!supplier_->IsLoaded(user_input.region_code)) { 111 if (!supplier_->IsLoaded(user_input.region_code))
104 return RULES_UNAVAILABLE; 112 return RULES_UNAVAILABLE;
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 = Validate(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);
105 } 128 }
106 129
107 suggestions_->GetSuggestions( 130 suggestions_->GetSuggestions(
108 user_input, 131 address_copy,
109 focused_field, 132 focused_field,
110 suggestion_limit, 133 suggestion_limit,
111 suggestions); 134 suggestions);
112 135
136 filter.clear();
137 filter.insert(std::make_pair(POSTAL_CODE, MISMATCHING_VALUE));
138 std::vector<AddressData> valid_postal_codes;
139 for (std::vector<AddressData>::const_iterator suggestion_it =
140 suggestions->begin();
141 suggestion_it != suggestions->end();
142 ++suggestion_it) {
143 problems.clear();
144
145 Status status = Validate(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);
153
113 return SUCCESS; 154 return SUCCESS;
114 } 155 }
115 156
116 bool PreloadAddressValidator::CanonicalizeAdministrativeArea( 157 bool PreloadAddressValidator::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 PreloadAddressValidator::Validated(bool success,
135 const AddressData&, 175 const AddressData&,
136 const FieldProblemMap&) { 176 const FieldProblemMap&) {
137 assert(success); 177 DCHECK(success);
138 } 178 }
139 179
140 // Stub constructor for use by MockAddressValidator. 180 // Stub constructor for use by MockAddressValidator.
141 PreloadAddressValidator::PreloadAddressValidator() { 181 PreloadAddressValidator::PreloadAddressValidator() {}
142 }
143 182
144 } // namespace autofill 183 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698