Chromium Code Reviews| Index: components/payments/address_validator_helper.cc |
| diff --git a/components/payments/address_validator_helper.cc b/components/payments/address_validator_helper.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..17d3a1c041c86b7693bd37dfb5144f301da6632f |
| --- /dev/null |
| +++ b/components/payments/address_validator_helper.cc |
| @@ -0,0 +1,137 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/payments/address_validator_helper.h" |
| + |
| +#include <memory> |
| +#include <utility> |
| + |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "components/autofill/core/browser/address_i18n.h" |
| +#include "third_party/libaddressinput/chromium/chrome_address_validator.h" |
| +#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_data.h" |
| +#include "third_party/libaddressinput/src/cpp/include/libaddressinput/source.h" |
| +#include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h" |
| + |
| +namespace { |
| +using ::i18n::addressinput::Source; |
| +using ::i18n::addressinput::Storage; |
| +} // namespace |
| + |
| +namespace payments { |
| + |
| +namespace { |
| + |
| +class AddressNormalizationRequest : public NormalizationRequest { |
| + public: |
| + AddressNormalizationRequest(AutofillProfile profile, |
| + const std::string& region_code, |
|
please use gerrit instead
2017/02/14 21:31:08
const-ref
sebsg
2017/02/15 16:18:30
Done.
|
| + AddressNormalizationRequester* requester, |
| + autofill::AddressValidator* address_validator) { |
|
please use gerrit instead
2017/02/14 21:31:08
nit: Use initializer list, i.e., ClassName(type pa
sebsg
2017/02/15 16:18:30
Done.
|
| + profile_ = profile; |
| + region_code_ = region_code; |
| + requester_ = requester; |
| + address_validator_ = address_validator; |
| + } |
| + |
| + ~AddressNormalizationRequest() override {} |
| + |
| + void OnRulesSuccessfullyLoaded() override { |
| + requester_->OnAddressNormalized(NormalizeAddress(profile_, region_code_)); |
| + } |
| + |
| + private: |
| + AutofillProfile NormalizeAddress(AutofillProfile profile, |
| + const std::string& region_code) { |
|
please use gerrit instead
2017/02/14 21:31:08
nit: move the body of this function inside of OnRu
sebsg
2017/02/15 16:18:30
Done.
|
| + DCHECK(address_validator_->AreRulesLoadedForRegion(region_code)); |
| + |
| + // Create the AddressData from the profile. |
| + ::i18n::addressinput::AddressData address_data = |
| + *autofill::i18n::CreateAddressDataFromAutofillProfile(profile, |
| + region_code); |
| + |
| + // Normalize the address. |
| + if (address_validator_ && |
| + address_validator_->NormalizeAddress(&address_data)) { |
| + profile.SetRawInfo(autofill::ADDRESS_HOME_STATE, |
| + base::UTF8ToUTF16(address_data.administrative_area)); |
| + profile.SetRawInfo(autofill::ADDRESS_HOME_CITY, |
| + base::UTF8ToUTF16(address_data.locality)); |
| + profile.SetRawInfo(autofill::ADDRESS_HOME_DEPENDENT_LOCALITY, |
| + base::UTF8ToUTF16(address_data.dependent_locality)); |
| + } |
| + |
| + return profile; |
| + } |
| + |
| + AddressNormalizationRequester* requester_; |
| + AutofillProfile profile_; |
| + std::string region_code_; |
| + autofill::AddressValidator* address_validator_; |
|
please use gerrit instead
2017/02/14 21:31:08
Please use the same the order for member variables
sebsg
2017/02/15 16:18:30
Done.
|
| + |
| + DISALLOW_COPY_AND_ASSIGN(AddressNormalizationRequest); |
| +}; |
| + |
| +} // namespace |
| + |
| +AddressValidatorHelper::AddressValidatorHelper(std::unique_ptr<Source> source, |
| + std::unique_ptr<Storage> storage) |
| + : address_validator_(std::move(source), std::move(storage), this) {} |
| + |
| +AddressValidatorHelper::~AddressValidatorHelper() {} |
| + |
| +void AddressValidatorHelper::LoadRulesForRegion( |
| + const std::string& region_code) { |
| + address_validator_.LoadRules(region_code); |
| +} |
| + |
| +bool AddressValidatorHelper::AreRulesLoadedForRegion( |
| + const std::string& region_code) { |
| + return address_validator_.AreRulesLoadedForRegion(region_code); |
| +} |
| + |
| +bool AddressValidatorHelper::StartAddressNormalization( |
| + AutofillProfile profile, |
| + const std::string region_code, |
| + AddressNormalizationRequester* requester) { |
| + std::unique_ptr<AddressNormalizationRequest> request( |
| + new AddressNormalizationRequest(profile, region_code, requester, |
| + &address_validator_)); |
| + |
| + // Check if the rules are already loaded. |
| + if (AreRulesLoadedForRegion(region_code)) { |
| + request->OnRulesSuccessfullyLoaded(); |
| + return false; |
| + } else { |
| + // Setup the variables so the profile gets normalized when the rules have |
| + // finished loading. |
| + auto it = pending_normalization_.find(region_code); |
| + if (it == pending_normalization_.end()) { |
| + // If no entry exists yet, create the entry and assign it to |it|. |
| + it = pending_normalization_ |
| + .insert(std::make_pair( |
| + region_code, |
| + std::vector<std::unique_ptr<NormalizationRequest>>())) |
| + .first; |
| + } |
| + |
| + it->second.push_back(std::move(request)); |
| + |
| + return true; |
| + } |
| +} |
| + |
| +void AddressValidatorHelper::OnAddressValidationRulesLoaded( |
| + const std::string& region_code, |
| + bool success) { |
|
please use gerrit instead
2017/02/14 21:31:08
Add a note about the reason for ignoring |success|
sebsg
2017/02/15 16:18:30
I was thinking of using it and adding the onCouldN
|
| + // Check if an address normalization is pending. |
| + auto it = pending_normalization_.find(region_code); |
| + if (it != pending_normalization_.end()) { |
| + for (size_t i = 0; i < it->second.size(); ++i) |
|
please use gerrit instead
2017/02/14 21:31:08
nit {} for the "for" loops.
sebsg
2017/02/15 16:18:30
Done.
|
| + it->second[i]->OnRulesSuccessfullyLoaded(); |
| + pending_normalization_.erase(it); |
| + } |
| +} |
| + |
| +} // namespace payments |