| 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/address_normalizer.h" |
| 6 |
| 7 #include <memory> |
| 8 #include <utility> |
| 9 |
| 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "components/autofill/core/browser/address_i18n.h" |
| 12 #include "third_party/libaddressinput/chromium/chrome_address_validator.h" |
| 13 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_da
ta.h" |
| 14 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/source.h" |
| 15 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h" |
| 16 |
| 17 namespace { |
| 18 using ::i18n::addressinput::Source; |
| 19 using ::i18n::addressinput::Storage; |
| 20 } // namespace |
| 21 |
| 22 namespace payments { |
| 23 |
| 24 namespace { |
| 25 |
| 26 class AddressNormalizationRequest : public AddressNormalizer::Request { |
| 27 public: |
| 28 // The |delegate| and |address_validator| need to outlive this Request. |
| 29 AddressNormalizationRequest(const AutofillProfile& profile, |
| 30 const std::string& region_code, |
| 31 AddressNormalizer::Delegate* delegate, |
| 32 autofill::AddressValidator* address_validator) |
| 33 : profile_(profile), |
| 34 region_code_(region_code), |
| 35 delegate_(delegate), |
| 36 address_validator_(address_validator) {} |
| 37 |
| 38 ~AddressNormalizationRequest() override {} |
| 39 |
| 40 void OnRulesLoaded(bool success) override { |
| 41 if (!success) { |
| 42 delegate_->OnCouldNotNormalize(profile_); |
| 43 return; |
| 44 } |
| 45 |
| 46 DCHECK(address_validator_->AreRulesLoadedForRegion(region_code_)); |
| 47 |
| 48 // Create the AddressData from the profile. |
| 49 ::i18n::addressinput::AddressData address_data = |
| 50 *autofill::i18n::CreateAddressDataFromAutofillProfile(profile_, |
| 51 region_code_); |
| 52 |
| 53 // Normalize the address. |
| 54 if (address_validator_ && |
| 55 address_validator_->NormalizeAddress(&address_data)) { |
| 56 profile_.SetRawInfo(autofill::ADDRESS_HOME_STATE, |
| 57 base::UTF8ToUTF16(address_data.administrative_area)); |
| 58 profile_.SetRawInfo(autofill::ADDRESS_HOME_CITY, |
| 59 base::UTF8ToUTF16(address_data.locality)); |
| 60 profile_.SetRawInfo(autofill::ADDRESS_HOME_DEPENDENT_LOCALITY, |
| 61 base::UTF8ToUTF16(address_data.dependent_locality)); |
| 62 } |
| 63 |
| 64 delegate_->OnAddressNormalized(profile_); |
| 65 } |
| 66 |
| 67 private: |
| 68 AutofillProfile profile_; |
| 69 std::string region_code_; |
| 70 AddressNormalizer::Delegate* delegate_; |
| 71 autofill::AddressValidator* address_validator_; |
| 72 |
| 73 DISALLOW_COPY_AND_ASSIGN(AddressNormalizationRequest); |
| 74 }; |
| 75 |
| 76 } // namespace |
| 77 |
| 78 AddressNormalizer::AddressNormalizer(std::unique_ptr<Source> source, |
| 79 std::unique_ptr<Storage> storage) |
| 80 : address_validator_(std::move(source), std::move(storage), this) {} |
| 81 |
| 82 AddressNormalizer::~AddressNormalizer() {} |
| 83 |
| 84 void AddressNormalizer::LoadRulesForRegion(const std::string& region_code) { |
| 85 address_validator_.LoadRules(region_code); |
| 86 } |
| 87 |
| 88 bool AddressNormalizer::AreRulesLoadedForRegion( |
| 89 const std::string& region_code) { |
| 90 return address_validator_.AreRulesLoadedForRegion(region_code); |
| 91 } |
| 92 |
| 93 void AddressNormalizer::StartAddressNormalization( |
| 94 const AutofillProfile& profile, |
| 95 const std::string& region_code, |
| 96 AddressNormalizer::Delegate* requester) { |
| 97 std::unique_ptr<AddressNormalizationRequest> request( |
| 98 new AddressNormalizationRequest(profile, region_code, requester, |
| 99 &address_validator_)); |
| 100 |
| 101 // Check if the rules are already loaded. |
| 102 if (AreRulesLoadedForRegion(region_code)) { |
| 103 request->OnRulesLoaded(true); |
| 104 } else { |
| 105 // Setup the variables so the profile gets normalized when the rules have |
| 106 // finished loading. |
| 107 auto it = pending_normalization_.find(region_code); |
| 108 if (it == pending_normalization_.end()) { |
| 109 // If no entry exists yet, create the entry and assign it to |it|. |
| 110 it = pending_normalization_ |
| 111 .insert(std::make_pair(region_code, |
| 112 std::vector<std::unique_ptr<Request>>())) |
| 113 .first; |
| 114 } |
| 115 |
| 116 it->second.push_back(std::move(request)); |
| 117 } |
| 118 } |
| 119 |
| 120 void AddressNormalizer::OnAddressValidationRulesLoaded( |
| 121 const std::string& region_code, |
| 122 bool success) { |
| 123 // Check if an address normalization is pending. |
| 124 auto it = pending_normalization_.find(region_code); |
| 125 if (it != pending_normalization_.end()) { |
| 126 for (size_t i = 0; i < it->second.size(); ++i) { |
| 127 it->second[i]->OnRulesLoaded(success); |
| 128 } |
| 129 pending_normalization_.erase(it); |
| 130 } |
| 131 } |
| 132 |
| 133 } // namespace payments |
| OLD | NEW |