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