| 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 "third_party/libaddressinput/chromium/chrome_address_validator.h" | 21 #include "third_party/libaddressinput/chromium/chrome_address_validator.h" |
| 22 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_da
ta.h" | 22 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_da
ta.h" |
| 23 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/source.h" | 23 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/source.h" |
| 24 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h" | 24 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h" |
| 25 #include "third_party/libphonenumber/phonenumber_api.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; |
| 31 using ::i18n::addressinput::Storage; | 32 using ::i18n::addressinput::Storage; |
| 33 using ::i18n::phonenumbers::PhoneNumberUtil; |
| 32 | 34 |
| 33 class AddressNormalizationRequest : public AddressNormalizer::Request { | 35 class AddressNormalizationRequest : public AddressNormalizer::Request { |
| 34 public: | 36 public: |
| 35 // The |delegate| and |address_validator| need to outlive this Request. | 37 // The |delegate| and |address_validator| need to outlive this Request. |
| 36 AddressNormalizationRequest(const AutofillProfile& profile, | 38 AddressNormalizationRequest(const AutofillProfile& profile, |
| 37 const std::string& region_code, | 39 const std::string& region_code, |
| 38 int timeout_seconds, | 40 int timeout_seconds, |
| 39 AddressNormalizer::Delegate* delegate, | 41 AddressNormalizer::Delegate* delegate, |
| 40 autofill::AddressValidator* address_validator) | 42 autofill::AddressValidator* address_validator) |
| 41 : profile_(profile), | 43 : profile_(profile), |
| (...skipping 13 matching lines...) Expand all Loading... |
| 55 ~AddressNormalizationRequest() override {} | 57 ~AddressNormalizationRequest() override {} |
| 56 | 58 |
| 57 void OnRulesLoaded(bool success) override { | 59 void OnRulesLoaded(bool success) override { |
| 58 on_timeout_.Cancel(); | 60 on_timeout_.Cancel(); |
| 59 | 61 |
| 60 // Check if the timeout happened before the rules were loaded. | 62 // Check if the timeout happened before the rules were loaded. |
| 61 if (has_responded_) | 63 if (has_responded_) |
| 62 return; | 64 return; |
| 63 has_responded_ = true; | 65 has_responded_ = true; |
| 64 | 66 |
| 67 // In either case, format the phone number. |
| 68 FormatPhoneNumberForResponse(); |
| 69 |
| 65 if (!success) { | 70 if (!success) { |
| 66 delegate_->OnCouldNotNormalize(profile_); | 71 delegate_->OnCouldNotNormalize(profile_); |
| 67 return; | 72 return; |
| 68 } | 73 } |
| 69 | 74 |
| 70 // The rules should be loaded. | 75 // The rules should be loaded. |
| 71 DCHECK(address_validator_->AreRulesLoadedForRegion(region_code_)); | 76 DCHECK(address_validator_->AreRulesLoadedForRegion(region_code_)); |
| 72 | 77 |
| 73 // Create the AddressData from the profile. | 78 // Create the AddressData from the profile. |
| 74 ::i18n::addressinput::AddressData address_data = | 79 ::i18n::addressinput::AddressData address_data = |
| 75 *autofill::i18n::CreateAddressDataFromAutofillProfile(profile_, | 80 *autofill::i18n::CreateAddressDataFromAutofillProfile(profile_, |
| 76 region_code_); | 81 region_code_); |
| 77 | 82 |
| 78 // Normalize the address. | 83 // Normalize the address. |
| 79 if (address_validator_ && | 84 if (address_validator_ && |
| 80 address_validator_->NormalizeAddress(&address_data)) { | 85 address_validator_->NormalizeAddress(&address_data)) { |
| 81 profile_.SetRawInfo(autofill::ADDRESS_HOME_STATE, | 86 profile_.SetRawInfo(autofill::ADDRESS_HOME_STATE, |
| 82 base::UTF8ToUTF16(address_data.administrative_area)); | 87 base::UTF8ToUTF16(address_data.administrative_area)); |
| 83 profile_.SetRawInfo(autofill::ADDRESS_HOME_CITY, | 88 profile_.SetRawInfo(autofill::ADDRESS_HOME_CITY, |
| 84 base::UTF8ToUTF16(address_data.locality)); | 89 base::UTF8ToUTF16(address_data.locality)); |
| 85 profile_.SetRawInfo(autofill::ADDRESS_HOME_DEPENDENT_LOCALITY, | 90 profile_.SetRawInfo(autofill::ADDRESS_HOME_DEPENDENT_LOCALITY, |
| 86 base::UTF8ToUTF16(address_data.dependent_locality)); | 91 base::UTF8ToUTF16(address_data.dependent_locality)); |
| 87 } | 92 } |
| 88 | 93 |
| 89 delegate_->OnAddressNormalized(profile_); | 94 delegate_->OnAddressNormalized(profile_); |
| 90 } | 95 } |
| 91 | 96 |
| 92 private: | 97 private: |
| 98 // Tries to format the phone number to the E.164 format to send in the Payment |
| 99 // Response, as defined in the Payment Request spec. Keeps the original |
| 100 // if it cannot be formatted. More info at: |
| 101 // https://w3c.github.io/browser-payment-api/#paymentrequest-updated-algorithm |
| 102 void FormatPhoneNumberForResponse() { |
| 103 const std::string original_number = base::UTF16ToUTF8(profile_.GetInfo( |
| 104 autofill::AutofillType(autofill::PHONE_HOME_WHOLE_NUMBER), |
| 105 region_code_)); |
| 106 i18n::phonenumbers::PhoneNumber parsed_number; |
| 107 PhoneNumberUtil* phone_number_util = PhoneNumberUtil::GetInstance(); |
| 108 if (phone_number_util->Parse(original_number, region_code_, |
| 109 &parsed_number) == |
| 110 PhoneNumberUtil::NO_PARSING_ERROR) { |
| 111 std::string formatted_number; |
| 112 phone_number_util->Format(parsed_number, |
| 113 PhoneNumberUtil::PhoneNumberFormat::E164, |
| 114 &formatted_number); |
| 115 profile_.SetRawInfo(autofill::PHONE_HOME_WHOLE_NUMBER, |
| 116 base::UTF8ToUTF16(formatted_number)); |
| 117 } |
| 118 } |
| 119 |
| 93 AutofillProfile profile_; | 120 AutofillProfile profile_; |
| 94 std::string region_code_; | 121 std::string region_code_; |
| 95 AddressNormalizer::Delegate* delegate_; | 122 AddressNormalizer::Delegate* delegate_; |
| 96 autofill::AddressValidator* address_validator_; | 123 autofill::AddressValidator* address_validator_; |
| 97 | 124 |
| 98 bool has_responded_; | 125 bool has_responded_; |
| 99 base::CancelableCallback<void()> on_timeout_; | 126 base::CancelableCallback<void()> on_timeout_; |
| 100 | 127 |
| 101 DISALLOW_COPY_AND_ASSIGN(AddressNormalizationRequest); | 128 DISALLOW_COPY_AND_ASSIGN(AddressNormalizationRequest); |
| 102 }; | 129 }; |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 auto it = pending_normalization_.find(region_code); | 187 auto it = pending_normalization_.find(region_code); |
| 161 if (it != pending_normalization_.end()) { | 188 if (it != pending_normalization_.end()) { |
| 162 for (size_t i = 0; i < it->second.size(); ++i) { | 189 for (size_t i = 0; i < it->second.size(); ++i) { |
| 163 it->second[i]->OnRulesLoaded(success); | 190 it->second[i]->OnRulesLoaded(success); |
| 164 } | 191 } |
| 165 pending_normalization_.erase(it); | 192 pending_normalization_.erase(it); |
| 166 } | 193 } |
| 167 } | 194 } |
| 168 | 195 |
| 169 } // namespace payments | 196 } // namespace payments |
| OLD | NEW |