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

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

Issue 2713033004: Layered component for web payments (Closed)
Patch Set: Rebase Created 3 years, 9 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_normalizer.h"
6
7 #include <memory>
8 #include <utility>
9
10 #include "base/cancelable_callback.h"
11 #include "base/memory/ptr_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/threading/sequenced_task_runner_handle.h"
14 #include "components/autofill/core/browser/address_i18n.h"
15 #include "third_party/libaddressinput/chromium/chrome_address_validator.h"
16 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_da ta.h"
17 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/source.h"
18 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h"
19
20 namespace {
21 using ::i18n::addressinput::Source;
22 using ::i18n::addressinput::Storage;
23 } // namespace
24
25 namespace payments {
26
27 namespace {
28
29 class AddressNormalizationRequest : public AddressNormalizer::Request {
30 public:
31 // The |delegate| and |address_validator| need to outlive this Request.
32 AddressNormalizationRequest(const AutofillProfile& profile,
33 const std::string& region_code,
34 int timeout_seconds,
35 AddressNormalizer::Delegate* delegate,
36 autofill::AddressValidator* address_validator)
37 : profile_(profile),
38 region_code_(region_code),
39 delegate_(delegate),
40 address_validator_(address_validator),
41 has_responded_(false),
42 on_timeout_(
43 base::Bind(&::payments::AddressNormalizationRequest::OnRulesLoaded,
44 base::Unretained(this),
45 false)) {
46 base::SequencedTaskRunnerHandle::Get()->PostDelayedTask(
47 FROM_HERE, on_timeout_.callback(),
48 base::TimeDelta::FromSeconds(timeout_seconds));
49 }
50
51 ~AddressNormalizationRequest() override {}
52
53 void OnRulesLoaded(bool success) override {
54 on_timeout_.Cancel();
55
56 // Check if the timeout happened before the rules were loaded.
57 if (has_responded_)
58 return;
59 has_responded_ = true;
60
61 if (!success) {
62 delegate_->OnCouldNotNormalize(profile_);
63 return;
64 }
65
66 // The rules should be loaded.
67 DCHECK(address_validator_->AreRulesLoadedForRegion(region_code_));
68
69 // Create the AddressData from the profile.
70 ::i18n::addressinput::AddressData address_data =
71 *autofill::i18n::CreateAddressDataFromAutofillProfile(profile_,
72 region_code_);
73
74 // Normalize the address.
75 if (address_validator_ &&
76 address_validator_->NormalizeAddress(&address_data)) {
77 profile_.SetRawInfo(autofill::ADDRESS_HOME_STATE,
78 base::UTF8ToUTF16(address_data.administrative_area));
79 profile_.SetRawInfo(autofill::ADDRESS_HOME_CITY,
80 base::UTF8ToUTF16(address_data.locality));
81 profile_.SetRawInfo(autofill::ADDRESS_HOME_DEPENDENT_LOCALITY,
82 base::UTF8ToUTF16(address_data.dependent_locality));
83 }
84
85 delegate_->OnAddressNormalized(profile_);
86 }
87
88 private:
89 AutofillProfile profile_;
90 std::string region_code_;
91 AddressNormalizer::Delegate* delegate_;
92 autofill::AddressValidator* address_validator_;
93
94 bool has_responded_;
95 base::CancelableCallback<void()> on_timeout_;
96
97 DISALLOW_COPY_AND_ASSIGN(AddressNormalizationRequest);
98 };
99
100 } // namespace
101
102 AddressNormalizer::AddressNormalizer(std::unique_ptr<Source> source,
103 std::unique_ptr<Storage> storage)
104 : address_validator_(std::move(source), std::move(storage), this) {}
105
106 AddressNormalizer::~AddressNormalizer() {}
107
108 void AddressNormalizer::LoadRulesForRegion(const std::string& region_code) {
109 address_validator_.LoadRules(region_code);
110 }
111
112 bool AddressNormalizer::AreRulesLoadedForRegion(
113 const std::string& region_code) {
114 return address_validator_.AreRulesLoadedForRegion(region_code);
115 }
116
117 void AddressNormalizer::StartAddressNormalization(
118 const AutofillProfile& profile,
119 const std::string& region_code,
120 int timeout_seconds,
121 AddressNormalizer::Delegate* requester) {
122 DCHECK(timeout_seconds >= 0);
123
124 std::unique_ptr<AddressNormalizationRequest> request(
125 base::MakeUnique<AddressNormalizationRequest>(profile, region_code,
126 timeout_seconds, requester,
127 &address_validator_));
128
129 // Check if the rules are already loaded.
130 if (AreRulesLoadedForRegion(region_code)) {
131 request->OnRulesLoaded(true);
132 } else {
133 // Setup the variables so the profile gets normalized when the rules have
134 // finished loading.
135 auto it = pending_normalization_.find(region_code);
136 if (it == pending_normalization_.end()) {
137 // If no entry exists yet, create the entry and assign it to |it|.
138 it = pending_normalization_
139 .insert(std::make_pair(region_code,
140 std::vector<std::unique_ptr<Request>>()))
141 .first;
142 }
143
144 it->second.push_back(std::move(request));
145
146 // Start loading the rules for that region. If the rules were already in the
147 // process of being loaded, this call will do nothing.
148 LoadRulesForRegion(region_code);
149 }
150 }
151
152 void AddressNormalizer::OnAddressValidationRulesLoaded(
153 const std::string& region_code,
154 bool success) {
155 // Check if an address normalization is pending.
156 auto it = pending_normalization_.find(region_code);
157 if (it != pending_normalization_.end()) {
158 for (size_t i = 0; i < it->second.size(); ++i) {
159 it->second[i]->OnRulesLoaded(success);
160 }
161 pending_normalization_.erase(it);
162 }
163 }
164
165 } // namespace payments
OLDNEW
« no previous file with comments | « components/payments/address_normalizer.h ('k') | components/payments/address_normalizer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698