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

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

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