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

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

Issue 2686613003: [Payments] Move address normalization code from android to native. (Closed)
Patch Set: 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 NormalizationRequest {
27 public:
28 AddressNormalizationRequest(AutofillProfile profile,
29 const std::string& region_code,
please use gerrit instead 2017/02/14 21:31:08 const-ref
sebsg 2017/02/15 16:18:30 Done.
30 AddressNormalizationRequester* requester,
31 autofill::AddressValidator* address_validator) {
please use gerrit instead 2017/02/14 21:31:08 nit: Use initializer list, i.e., ClassName(type pa
sebsg 2017/02/15 16:18:30 Done.
32 profile_ = profile;
33 region_code_ = region_code;
34 requester_ = requester;
35 address_validator_ = address_validator;
36 }
37
38 ~AddressNormalizationRequest() override {}
39
40 void OnRulesSuccessfullyLoaded() override {
41 requester_->OnAddressNormalized(NormalizeAddress(profile_, region_code_));
42 }
43
44 private:
45 AutofillProfile NormalizeAddress(AutofillProfile profile,
46 const std::string& region_code) {
please use gerrit instead 2017/02/14 21:31:08 nit: move the body of this function inside of OnRu
sebsg 2017/02/15 16:18:30 Done.
47 DCHECK(address_validator_->AreRulesLoadedForRegion(region_code));
48
49 // Create the AddressData from the profile.
50 ::i18n::addressinput::AddressData address_data =
51 *autofill::i18n::CreateAddressDataFromAutofillProfile(profile,
52 region_code);
53
54 // Normalize the address.
55 if (address_validator_ &&
56 address_validator_->NormalizeAddress(&address_data)) {
57 profile.SetRawInfo(autofill::ADDRESS_HOME_STATE,
58 base::UTF8ToUTF16(address_data.administrative_area));
59 profile.SetRawInfo(autofill::ADDRESS_HOME_CITY,
60 base::UTF8ToUTF16(address_data.locality));
61 profile.SetRawInfo(autofill::ADDRESS_HOME_DEPENDENT_LOCALITY,
62 base::UTF8ToUTF16(address_data.dependent_locality));
63 }
64
65 return profile;
66 }
67
68 AddressNormalizationRequester* requester_;
69 AutofillProfile profile_;
70 std::string region_code_;
71 autofill::AddressValidator* address_validator_;
please use gerrit instead 2017/02/14 21:31:08 Please use the same the order for member variables
sebsg 2017/02/15 16:18:30 Done.
72
73 DISALLOW_COPY_AND_ASSIGN(AddressNormalizationRequest);
74 };
75
76 } // namespace
77
78 AddressValidatorHelper::AddressValidatorHelper(std::unique_ptr<Source> source,
79 std::unique_ptr<Storage> storage)
80 : address_validator_(std::move(source), std::move(storage), this) {}
81
82 AddressValidatorHelper::~AddressValidatorHelper() {}
83
84 void AddressValidatorHelper::LoadRulesForRegion(
85 const std::string& region_code) {
86 address_validator_.LoadRules(region_code);
87 }
88
89 bool AddressValidatorHelper::AreRulesLoadedForRegion(
90 const std::string& region_code) {
91 return address_validator_.AreRulesLoadedForRegion(region_code);
92 }
93
94 bool AddressValidatorHelper::StartAddressNormalization(
95 AutofillProfile profile,
96 const std::string region_code,
97 AddressNormalizationRequester* requester) {
98 std::unique_ptr<AddressNormalizationRequest> request(
99 new AddressNormalizationRequest(profile, region_code, requester,
100 &address_validator_));
101
102 // Check if the rules are already loaded.
103 if (AreRulesLoadedForRegion(region_code)) {
104 request->OnRulesSuccessfullyLoaded();
105 return false;
106 } else {
107 // Setup the variables so the profile gets normalized when the rules have
108 // finished loading.
109 auto it = pending_normalization_.find(region_code);
110 if (it == pending_normalization_.end()) {
111 // If no entry exists yet, create the entry and assign it to |it|.
112 it = pending_normalization_
113 .insert(std::make_pair(
114 region_code,
115 std::vector<std::unique_ptr<NormalizationRequest>>()))
116 .first;
117 }
118
119 it->second.push_back(std::move(request));
120
121 return true;
122 }
123 }
124
125 void AddressValidatorHelper::OnAddressValidationRulesLoaded(
126 const std::string& region_code,
127 bool success) {
please use gerrit instead 2017/02/14 21:31:08 Add a note about the reason for ignoring |success|
sebsg 2017/02/15 16:18:30 I was thinking of using it and adding the onCouldN
128 // Check if an address normalization is pending.
129 auto it = pending_normalization_.find(region_code);
130 if (it != pending_normalization_.end()) {
131 for (size_t i = 0; i < it->second.size(); ++i)
please use gerrit instead 2017/02/14 21:31:08 nit {} for the "for" loops.
sebsg 2017/02/15 16:18:30 Done.
132 it->second[i]->OnRulesSuccessfullyLoaded();
133 pending_normalization_.erase(it);
134 }
135 }
136
137 } // namespace payments
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698