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

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

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

Powered by Google App Engine
This is Rietveld 408576698