| 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" |
| 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 if (has_responded_) |
| 57 return; |
| 58 has_responded_ = true; |
| 59 |
| 41 if (!success) { | 60 if (!success) { |
| 42 delegate_->OnCouldNotNormalize(profile_); | 61 delegate_->OnCouldNotNormalize(profile_); |
| 43 return; | 62 return; |
| 44 } | 63 } |
| 45 | 64 |
| 65 // The rules should be loaded. |
| 46 DCHECK(address_validator_->AreRulesLoadedForRegion(region_code_)); | 66 DCHECK(address_validator_->AreRulesLoadedForRegion(region_code_)); |
| 47 | 67 |
| 48 // Create the AddressData from the profile. | 68 // Create the AddressData from the profile. |
| 49 ::i18n::addressinput::AddressData address_data = | 69 ::i18n::addressinput::AddressData address_data = |
| 50 *autofill::i18n::CreateAddressDataFromAutofillProfile(profile_, | 70 *autofill::i18n::CreateAddressDataFromAutofillProfile(profile_, |
| 51 region_code_); | 71 region_code_); |
| 52 | 72 |
| 53 // Normalize the address. | 73 // Normalize the address. |
| 54 if (address_validator_ && | 74 if (address_validator_ && |
| 55 address_validator_->NormalizeAddress(&address_data)) { | 75 address_validator_->NormalizeAddress(&address_data)) { |
| 56 profile_.SetRawInfo(autofill::ADDRESS_HOME_STATE, | 76 profile_.SetRawInfo(autofill::ADDRESS_HOME_STATE, |
| 57 base::UTF8ToUTF16(address_data.administrative_area)); | 77 base::UTF8ToUTF16(address_data.administrative_area)); |
| 58 profile_.SetRawInfo(autofill::ADDRESS_HOME_CITY, | 78 profile_.SetRawInfo(autofill::ADDRESS_HOME_CITY, |
| 59 base::UTF8ToUTF16(address_data.locality)); | 79 base::UTF8ToUTF16(address_data.locality)); |
| 60 profile_.SetRawInfo(autofill::ADDRESS_HOME_DEPENDENT_LOCALITY, | 80 profile_.SetRawInfo(autofill::ADDRESS_HOME_DEPENDENT_LOCALITY, |
| 61 base::UTF8ToUTF16(address_data.dependent_locality)); | 81 base::UTF8ToUTF16(address_data.dependent_locality)); |
| 62 } | 82 } |
| 63 | 83 |
| 64 delegate_->OnAddressNormalized(profile_); | 84 delegate_->OnAddressNormalized(profile_); |
| 65 } | 85 } |
| 66 | 86 |
| 67 private: | 87 private: |
| 68 AutofillProfile profile_; | 88 AutofillProfile profile_; |
| 69 std::string region_code_; | 89 std::string region_code_; |
| 70 AddressNormalizer::Delegate* delegate_; | 90 AddressNormalizer::Delegate* delegate_; |
| 71 autofill::AddressValidator* address_validator_; | 91 autofill::AddressValidator* address_validator_; |
| 72 | 92 |
| 93 bool has_responded_; |
| 94 base::CancelableCallback<void()> on_timeout_; |
| 95 |
| 73 DISALLOW_COPY_AND_ASSIGN(AddressNormalizationRequest); | 96 DISALLOW_COPY_AND_ASSIGN(AddressNormalizationRequest); |
| 74 }; | 97 }; |
| 75 | 98 |
| 76 } // namespace | 99 } // namespace |
| 77 | 100 |
| 78 AddressNormalizer::AddressNormalizer(std::unique_ptr<Source> source, | 101 AddressNormalizer::AddressNormalizer(std::unique_ptr<Source> source, |
| 79 std::unique_ptr<Storage> storage) | 102 std::unique_ptr<Storage> storage) |
| 80 : address_validator_(std::move(source), std::move(storage), this) {} | 103 : address_validator_(std::move(source), std::move(storage), this) {} |
| 81 | 104 |
| 82 AddressNormalizer::~AddressNormalizer() {} | 105 AddressNormalizer::~AddressNormalizer() {} |
| 83 | 106 |
| 84 void AddressNormalizer::LoadRulesForRegion(const std::string& region_code) { | 107 void AddressNormalizer::LoadRulesForRegion(const std::string& region_code) { |
| 85 address_validator_.LoadRules(region_code); | 108 address_validator_.LoadRules(region_code); |
| 86 } | 109 } |
| 87 | 110 |
| 88 bool AddressNormalizer::AreRulesLoadedForRegion( | 111 bool AddressNormalizer::AreRulesLoadedForRegion( |
| 89 const std::string& region_code) { | 112 const std::string& region_code) { |
| 90 return address_validator_.AreRulesLoadedForRegion(region_code); | 113 return address_validator_.AreRulesLoadedForRegion(region_code); |
| 91 } | 114 } |
| 92 | 115 |
| 93 void AddressNormalizer::StartAddressNormalization( | 116 void AddressNormalizer::StartAddressNormalization( |
| 94 const AutofillProfile& profile, | 117 const AutofillProfile& profile, |
| 95 const std::string& region_code, | 118 const std::string& region_code, |
| 119 int timeout_seconds, |
| 96 AddressNormalizer::Delegate* requester) { | 120 AddressNormalizer::Delegate* requester) { |
| 121 DCHECK(timeout_seconds >= 0); |
| 122 |
| 97 std::unique_ptr<AddressNormalizationRequest> request( | 123 std::unique_ptr<AddressNormalizationRequest> request( |
| 98 new AddressNormalizationRequest(profile, region_code, requester, | 124 base::MakeUnique<AddressNormalizationRequest>(profile, region_code, |
| 99 &address_validator_)); | 125 timeout_seconds, requester, |
| 126 &address_validator_)); |
| 100 | 127 |
| 101 // Check if the rules are already loaded. | 128 // Check if the rules are already loaded. |
| 102 if (AreRulesLoadedForRegion(region_code)) { | 129 if (AreRulesLoadedForRegion(region_code)) { |
| 103 request->OnRulesLoaded(true); | 130 request->OnRulesLoaded(true); |
| 104 } else { | 131 } else { |
| 105 // Setup the variables so the profile gets normalized when the rules have | 132 // Setup the variables so the profile gets normalized when the rules have |
| 106 // finished loading. | 133 // finished loading. |
| 107 auto it = pending_normalization_.find(region_code); | 134 auto it = pending_normalization_.find(region_code); |
| 108 if (it == pending_normalization_.end()) { | 135 if (it == pending_normalization_.end()) { |
| 109 // If no entry exists yet, create the entry and assign it to |it|. | 136 // If no entry exists yet, create the entry and assign it to |it|. |
| 110 it = pending_normalization_ | 137 it = pending_normalization_ |
| 111 .insert(std::make_pair(region_code, | 138 .insert(std::make_pair(region_code, |
| 112 std::vector<std::unique_ptr<Request>>())) | 139 std::vector<std::unique_ptr<Request>>())) |
| 113 .first; | 140 .first; |
| 114 } | 141 } |
| 115 | 142 |
| 116 it->second.push_back(std::move(request)); | 143 it->second.push_back(std::move(request)); |
| 144 |
| 145 // Start loading the rules for that region. If the rules were already in the |
| 146 // process of being loaded, this call will do nothing. |
| 147 LoadRulesForRegion(region_code); |
| 117 } | 148 } |
| 118 } | 149 } |
| 119 | 150 |
| 120 void AddressNormalizer::OnAddressValidationRulesLoaded( | 151 void AddressNormalizer::OnAddressValidationRulesLoaded( |
| 121 const std::string& region_code, | 152 const std::string& region_code, |
| 122 bool success) { | 153 bool success) { |
| 123 // Check if an address normalization is pending. | 154 // Check if an address normalization is pending. |
| 124 auto it = pending_normalization_.find(region_code); | 155 auto it = pending_normalization_.find(region_code); |
| 125 if (it != pending_normalization_.end()) { | 156 if (it != pending_normalization_.end()) { |
| 126 for (size_t i = 0; i < it->second.size(); ++i) { | 157 for (size_t i = 0; i < it->second.size(); ++i) { |
| 127 it->second[i]->OnRulesLoaded(success); | 158 it->second[i]->OnRulesLoaded(success); |
| 128 } | 159 } |
| 129 pending_normalization_.erase(it); | 160 pending_normalization_.erase(it); |
| 130 } | 161 } |
| 131 } | 162 } |
| 132 | 163 |
| 133 } // namespace payments | 164 } // namespace payments |
| OLD | NEW |