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

Side by Side Diff: components/payments/address_validator_helper.cc

Issue 2686613003: [Payments] Move address normalization code from android to native. (Closed)
Patch Set: Addressed comments Created 3 years, 10 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
OLDNEW
(Empty)
1 // Copyright 2017 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 "components/payments/address_validator_helper.h"
6
7 #include <memory>
8 #include <utility>
9
10 #include "base/strings/utf_string_conversions.h"
11 #include "components/autofill/core/browser/address_i18n.h"
12 #include "third_party/libaddressinput/chromium/chrome_address_validator.h"
13 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_da ta.h"
14 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/source.h"
15 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h"
16
17 namespace {
18 using ::i18n::addressinput::Source;
19 using ::i18n::addressinput::Storage;
20 } // namespace
21
22 namespace payments {
23
24 namespace {
25
26 class AddressNormalizationRequest : public AddressNormalizer::Request {
27 public:
28 AddressNormalizationRequest(const AutofillProfile& profile,
please use gerrit instead 2017/02/15 21:27:45 Please add a comment that delegate and address_val
sebsg 2017/02/15 21:44:04 Done.
29 const std::string& region_code,
30 AddressNormalizer::Delegate* delegate,
31 autofill::AddressValidator* address_validator)
32 : profile_(profile),
33 region_code_(region_code),
34 delegate_(delegate),
35 address_validator_(address_validator) {}
36
37 ~AddressNormalizationRequest() override {}
38
39 void OnRulesLoaded(bool success) override {
40 if (!success) {
41 delegate_->OnCouldNotNormalize(profile_);
42 return;
43 }
44
45 DCHECK(address_validator_->AreRulesLoadedForRegion(region_code_));
46
47 // Create the AddressData from the profile.
48 ::i18n::addressinput::AddressData address_data =
49 *autofill::i18n::CreateAddressDataFromAutofillProfile(profile_,
50 region_code_);
51
52 // Normalize the address.
53 if (address_validator_ &&
54 address_validator_->NormalizeAddress(&address_data)) {
55 profile_.SetRawInfo(autofill::ADDRESS_HOME_STATE,
56 base::UTF8ToUTF16(address_data.administrative_area));
57 profile_.SetRawInfo(autofill::ADDRESS_HOME_CITY,
58 base::UTF8ToUTF16(address_data.locality));
59 profile_.SetRawInfo(autofill::ADDRESS_HOME_DEPENDENT_LOCALITY,
60 base::UTF8ToUTF16(address_data.dependent_locality));
61 }
62
63 delegate_->OnAddressNormalized(profile_);
64 }
65
66 private:
67 AutofillProfile profile_;
68 std::string region_code_;
69 AddressNormalizer::Delegate* delegate_;
70 autofill::AddressValidator* address_validator_;
71
72 DISALLOW_COPY_AND_ASSIGN(AddressNormalizationRequest);
73 };
74
75 } // namespace
76
77 AddressNormalizer::AddressNormalizer(std::unique_ptr<Source> source,
78 std::unique_ptr<Storage> storage)
79 : address_validator_(std::move(source), std::move(storage), this) {}
80
81 AddressNormalizer::~AddressNormalizer() {}
82
83 void AddressNormalizer::LoadRulesForRegion(const std::string& region_code) {
84 address_validator_.LoadRules(region_code);
85 }
86
87 bool AddressNormalizer::AreRulesLoadedForRegion(
88 const std::string& region_code) {
89 return address_validator_.AreRulesLoadedForRegion(region_code);
90 }
91
92 void AddressNormalizer::StartAddressNormalization(
93 const AutofillProfile& profile,
94 const std::string& region_code,
95 AddressNormalizer::Delegate* requester) {
96 std::unique_ptr<AddressNormalizationRequest> request(
97 new AddressNormalizationRequest(profile, region_code, requester,
98 &address_validator_));
99
100 // Check if the rules are already loaded.
101 if (AreRulesLoadedForRegion(region_code)) {
102 request->OnRulesLoaded(true);
103 } else {
104 // Setup the variables so the profile gets normalized when the rules have
105 // finished loading.
106 auto it = pending_normalization_.find(region_code);
107 if (it == pending_normalization_.end()) {
108 // If no entry exists yet, create the entry and assign it to |it|.
109 it = pending_normalization_
110 .insert(std::make_pair(region_code,
111 std::vector<std::unique_ptr<Request>>()))
112 .first;
113 }
114
115 it->second.push_back(std::move(request));
116 }
117 }
118
119 void AddressNormalizer::OnAddressValidationRulesLoaded(
120 const std::string& region_code,
121 bool success) {
122 // Check if an address normalization is pending.
123 auto it = pending_normalization_.find(region_code);
124 if (it != pending_normalization_.end()) {
125 for (size_t i = 0; i < it->second.size(); ++i) {
126 it->second[i]->OnRulesLoaded(success);
127 }
128 pending_normalization_.erase(it);
129 }
130 }
131
132 } // namespace payments
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698