| 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/core/address_normalizer.h" | 5 #include "components/payments/core/address_normalizer.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 12 #include "base/cancelable_callback.h" | 12 #include "base/cancelable_callback.h" |
| 13 #include "base/location.h" | 13 #include "base/location.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
| 16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 17 #include "base/threading/sequenced_task_runner_handle.h" | 17 #include "base/threading/sequenced_task_runner_handle.h" |
| 18 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 19 #include "components/autofill/core/browser/address_i18n.h" | 19 #include "components/autofill/core/browser/address_i18n.h" |
| 20 #include "components/autofill/core/browser/autofill_profile.h" | 20 #include "components/autofill/core/browser/autofill_profile.h" |
| 21 #include "components/payments/core/payment_request_data_util.h" |
| 21 #include "third_party/libaddressinput/chromium/chrome_address_validator.h" | 22 #include "third_party/libaddressinput/chromium/chrome_address_validator.h" |
| 22 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_da
ta.h" | 23 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_da
ta.h" |
| 23 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/source.h" | 24 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/source.h" |
| 24 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h" | 25 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h" |
| 25 | 26 |
| 26 namespace payments { | 27 namespace payments { |
| 27 namespace { | 28 namespace { |
| 28 | 29 |
| 29 using ::autofill::AutofillProfile; | 30 using ::autofill::AutofillProfile; |
| 30 using ::i18n::addressinput::Source; | 31 using ::i18n::addressinput::Source; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 55 ~AddressNormalizationRequest() override {} | 56 ~AddressNormalizationRequest() override {} |
| 56 | 57 |
| 57 void OnRulesLoaded(bool success) override { | 58 void OnRulesLoaded(bool success) override { |
| 58 on_timeout_.Cancel(); | 59 on_timeout_.Cancel(); |
| 59 | 60 |
| 60 // Check if the timeout happened before the rules were loaded. | 61 // Check if the timeout happened before the rules were loaded. |
| 61 if (has_responded_) | 62 if (has_responded_) |
| 62 return; | 63 return; |
| 63 has_responded_ = true; | 64 has_responded_ = true; |
| 64 | 65 |
| 66 // In either case, format the phone number. |
| 67 FormatPhoneNumberForResponse(); |
| 68 |
| 65 if (!success) { | 69 if (!success) { |
| 66 delegate_->OnCouldNotNormalize(profile_); | 70 delegate_->OnCouldNotNormalize(profile_); |
| 67 return; | 71 return; |
| 68 } | 72 } |
| 69 | 73 |
| 70 // The rules should be loaded. | 74 // The rules should be loaded. |
| 71 DCHECK(address_validator_->AreRulesLoadedForRegion(region_code_)); | 75 DCHECK(address_validator_->AreRulesLoadedForRegion(region_code_)); |
| 72 | 76 |
| 73 // Create the AddressData from the profile. | 77 // Create the AddressData from the profile. |
| 74 ::i18n::addressinput::AddressData address_data = | 78 ::i18n::addressinput::AddressData address_data = |
| 75 *autofill::i18n::CreateAddressDataFromAutofillProfile(profile_, | 79 *autofill::i18n::CreateAddressDataFromAutofillProfile(profile_, |
| 76 region_code_); | 80 region_code_); |
| 77 | 81 |
| 78 // Normalize the address. | 82 // Normalize the address. |
| 79 if (address_validator_ && | 83 if (address_validator_ && |
| 80 address_validator_->NormalizeAddress(&address_data)) { | 84 address_validator_->NormalizeAddress(&address_data)) { |
| 81 profile_.SetRawInfo(autofill::ADDRESS_HOME_STATE, | 85 profile_.SetRawInfo(autofill::ADDRESS_HOME_STATE, |
| 82 base::UTF8ToUTF16(address_data.administrative_area)); | 86 base::UTF8ToUTF16(address_data.administrative_area)); |
| 83 profile_.SetRawInfo(autofill::ADDRESS_HOME_CITY, | 87 profile_.SetRawInfo(autofill::ADDRESS_HOME_CITY, |
| 84 base::UTF8ToUTF16(address_data.locality)); | 88 base::UTF8ToUTF16(address_data.locality)); |
| 85 profile_.SetRawInfo(autofill::ADDRESS_HOME_DEPENDENT_LOCALITY, | 89 profile_.SetRawInfo(autofill::ADDRESS_HOME_DEPENDENT_LOCALITY, |
| 86 base::UTF8ToUTF16(address_data.dependent_locality)); | 90 base::UTF8ToUTF16(address_data.dependent_locality)); |
| 87 } | 91 } |
| 88 | 92 |
| 89 delegate_->OnAddressNormalized(profile_); | 93 delegate_->OnAddressNormalized(profile_); |
| 90 } | 94 } |
| 91 | 95 |
| 92 private: | 96 private: |
| 97 // Tries to format the phone number to the E.164 format to send in the Payment |
| 98 // Response, as defined in the Payment Request spec. Keeps the original |
| 99 // if it cannot be formatted. More info at: |
| 100 // https://w3c.github.io/browser-payment-api/#paymentrequest-updated-algorithm |
| 101 void FormatPhoneNumberForResponse() { |
| 102 const std::string original_number = base::UTF16ToUTF8(profile_.GetInfo( |
| 103 autofill::AutofillType(autofill::PHONE_HOME_WHOLE_NUMBER), |
| 104 region_code_)); |
| 105 |
| 106 std::string formatted_number = |
| 107 data_util::FormatPhoneForResponse(original_number, region_code_); |
| 108 |
| 109 profile_.SetRawInfo(autofill::PHONE_HOME_WHOLE_NUMBER, |
| 110 base::UTF8ToUTF16(formatted_number)); |
| 111 } |
| 112 |
| 93 AutofillProfile profile_; | 113 AutofillProfile profile_; |
| 94 std::string region_code_; | 114 std::string region_code_; |
| 95 AddressNormalizer::Delegate* delegate_; | 115 AddressNormalizer::Delegate* delegate_; |
| 96 autofill::AddressValidator* address_validator_; | 116 autofill::AddressValidator* address_validator_; |
| 97 | 117 |
| 98 bool has_responded_; | 118 bool has_responded_; |
| 99 base::CancelableCallback<void()> on_timeout_; | 119 base::CancelableCallback<void()> on_timeout_; |
| 100 | 120 |
| 101 DISALLOW_COPY_AND_ASSIGN(AddressNormalizationRequest); | 121 DISALLOW_COPY_AND_ASSIGN(AddressNormalizationRequest); |
| 102 }; | 122 }; |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 auto it = pending_normalization_.find(region_code); | 180 auto it = pending_normalization_.find(region_code); |
| 161 if (it != pending_normalization_.end()) { | 181 if (it != pending_normalization_.end()) { |
| 162 for (size_t i = 0; i < it->second.size(); ++i) { | 182 for (size_t i = 0; i < it->second.size(); ++i) { |
| 163 it->second[i]->OnRulesLoaded(success); | 183 it->second[i]->OnRulesLoaded(success); |
| 164 } | 184 } |
| 165 pending_normalization_.erase(it); | 185 pending_normalization_.erase(it); |
| 166 } | 186 } |
| 167 } | 187 } |
| 168 | 188 |
| 169 } // namespace payments | 189 } // namespace payments |
| OLD | NEW |