OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/payments/core/address_normalization_manager.h" |
| 6 |
| 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "components/autofill/core/browser/autofill_data_util.h" |
| 10 #include "components/autofill/core/browser/field_types.h" |
| 11 |
| 12 namespace payments { |
| 13 |
| 14 namespace { |
| 15 constexpr int kAddressNormalizationTimeoutSeconds = 5; |
| 16 } // namespace |
| 17 |
| 18 AddressNormalizationManager::AddressNormalizationManager( |
| 19 std::unique_ptr<AddressNormalizer> address_normalizer, |
| 20 const std::string& default_country_code) |
| 21 : default_country_code_(default_country_code), |
| 22 address_normalizer_(std::move(address_normalizer)) { |
| 23 DCHECK(autofill::data_util::IsValidCountryCode(default_country_code)); |
| 24 DCHECK(address_normalizer_); |
| 25 |
| 26 // Start loading rules for the default country code. This happens |
| 27 // asynchronously, and will speed up normalization later if the rules for the |
| 28 // address' region have already been loaded. |
| 29 address_normalizer_->LoadRulesForRegion(default_country_code); |
| 30 } |
| 31 |
| 32 AddressNormalizationManager::~AddressNormalizationManager() {} |
| 33 |
| 34 void AddressNormalizationManager::FinalizeWithCompletionCallback( |
| 35 base::OnceClosure completion_callback) { |
| 36 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 37 completion_callback_ = std::move(completion_callback); |
| 38 accepting_requests_ = false; |
| 39 MaybeRunCompletionCallback(); |
| 40 } |
| 41 |
| 42 void AddressNormalizationManager::StartNormalizingAddress( |
| 43 autofill::AutofillProfile* profile) { |
| 44 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 45 DCHECK(accepting_requests_) << "FinalizeWithCompletionCallback has been " |
| 46 "called, cannot normalize more addresses"; |
| 47 |
| 48 delegates_.push_back(base::MakeUnique<NormalizerDelegate>( |
| 49 this, address_normalizer_.get(), profile)); |
| 50 } |
| 51 |
| 52 void AddressNormalizationManager::MaybeRunCompletionCallback() { |
| 53 if (accepting_requests_ || !completion_callback_) |
| 54 return; |
| 55 |
| 56 for (const auto& delegate : delegates_) { |
| 57 if (!delegate->has_completed()) |
| 58 return; |
| 59 } |
| 60 |
| 61 // We're no longer accepting requests, and all the delegates have completed. |
| 62 // Now's the time to run the completion callback. |
| 63 std::move(completion_callback_).Run(); |
| 64 } |
| 65 |
| 66 AddressNormalizationManager::NormalizerDelegate::NormalizerDelegate( |
| 67 AddressNormalizationManager* owner, |
| 68 AddressNormalizer* address_normalizer, |
| 69 autofill::AutofillProfile* profile) |
| 70 : owner_(owner), profile_(profile) { |
| 71 DCHECK(owner_); |
| 72 DCHECK(profile_); |
| 73 |
| 74 std::string country_code = |
| 75 base::UTF16ToUTF8(profile_->GetRawInfo(autofill::ADDRESS_HOME_COUNTRY)); |
| 76 if (!autofill::data_util::IsValidCountryCode(country_code)) |
| 77 country_code = owner_->default_country_code_; |
| 78 |
| 79 address_normalizer->StartAddressNormalization( |
| 80 *profile_, country_code, kAddressNormalizationTimeoutSeconds, this); |
| 81 } |
| 82 |
| 83 void AddressNormalizationManager::NormalizerDelegate::OnAddressNormalized( |
| 84 const autofill::AutofillProfile& normalized_profile) { |
| 85 OnCompletion(normalized_profile); |
| 86 } |
| 87 |
| 88 void AddressNormalizationManager::NormalizerDelegate::OnCouldNotNormalize( |
| 89 const autofill::AutofillProfile& profile) { |
| 90 // Since the phone number is formatted in either case, this profile should |
| 91 // be used. |
| 92 OnCompletion(profile); |
| 93 } |
| 94 |
| 95 void AddressNormalizationManager::NormalizerDelegate::OnCompletion( |
| 96 const autofill::AutofillProfile& profile) { |
| 97 DCHECK(!has_completed_); |
| 98 has_completed_ = true; |
| 99 *profile_ = profile; |
| 100 owner_->MaybeRunCompletionCallback(); |
| 101 } |
| 102 |
| 103 } // namespace payments |
OLD | NEW |