Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(76)

Side by Side Diff: components/payments/core/address_normalization_manager.cc

Issue 2889983002: Adds the AddressNormalizationManager and tests. (Closed)
Patch Set: Addresses comments from mathp@. Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 #include "components/payments/core/address_normalizer.h"
12
13 namespace payments {
14
15 namespace {
16 constexpr int kAddressNormalizationTimeoutSeconds = 5;
17 } // namespace
18
19 // Implements the payments::AddressNormalizer::Delegate interface, and
20 // notifies its parent AddressNormalizationManager when normalization has
21 // completed.
22 class AddressNormalizationManager::NormalizerDelegate
23 : public AddressNormalizer::Delegate {
24 public:
25 // |owner| is the parent AddressNormalizationManager, |address_normalizer| is
26 // a pointer to an instance of AddressNormalizer which will handle
27 // normalization of |profile|. |profile| will be updated when normalization is
28 // complete.
29 NormalizerDelegate(AddressNormalizationManager* owner,
30 AddressNormalizer* address_normalizer,
31 autofill::AutofillProfile* profile);
32
33 // Returns whether this delegate has completed or not.
34 bool has_completed() const { return has_completed_; }
35
36 // payments::AddressNormalizer::Delegate:
37 void OnAddressNormalized(
38 const autofill::AutofillProfile& normalized_profile) override;
39 void OnCouldNotNormalize(const autofill::AutofillProfile& profile) override;
40
41 private:
42 // Helper method that handles when normalization has completed.
43 void OnCompletion(const autofill::AutofillProfile& profile);
44
45 bool has_completed_ = false;
46 AddressNormalizationManager* owner_ = nullptr;
47 autofill::AutofillProfile* profile_ = nullptr;
48
49 DISALLOW_COPY_AND_ASSIGN(NormalizerDelegate);
50 };
51
52 AddressNormalizationManager::AddressNormalizationManager(
53 std::unique_ptr<AddressNormalizer> address_normalizer,
54 const std::string& default_country_code)
55 : default_country_code_(default_country_code),
56 address_normalizer_(std::move(address_normalizer)) {
57 DCHECK(autofill::data_util::IsValidCountryCode(default_country_code));
58 DCHECK(address_normalizer_);
59
60 // Start loading rules for the default country code. This happens
61 // asynchronously, and will speed up normalization later if the rules for the
62 // address' region have already been loaded.
63 address_normalizer_->LoadRulesForRegion(default_country_code);
64 }
65
66 AddressNormalizationManager::~AddressNormalizationManager() {}
67
68 void AddressNormalizationManager::FinalizeWithCompletionCallback(
69 base::OnceClosure completion_callback) {
70 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
71 completion_callback_ = std::move(completion_callback);
72 accepting_requests_ = false;
73 MaybeRunCompletionCallback();
74 }
75
76 void AddressNormalizationManager::StartNormalizingAddress(
77 autofill::AutofillProfile* profile) {
78 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
79 DCHECK(accepting_requests_) << "FinalizeWithCompletionCallback has been "
80 "called, cannot normalize more addresses";
81
82 delegates_.push_back(base::MakeUnique<NormalizerDelegate>(
83 this, address_normalizer_.get(), profile));
84 }
85
86 void AddressNormalizationManager::MaybeRunCompletionCallback() {
87 if (accepting_requests_ || !completion_callback_)
88 return;
89
90 for (const auto& delegate : delegates_) {
91 if (!delegate->has_completed())
92 return;
93 }
94
95 // We're no longer accepting requests, and all the delegates have completed.
96 // Now's the time to run the completion callback.
97 std::move(completion_callback_).Run();
98 }
99
100 AddressNormalizationManager::NormalizerDelegate::NormalizerDelegate(
101 AddressNormalizationManager* owner,
102 AddressNormalizer* address_normalizer,
103 autofill::AutofillProfile* profile)
104 : owner_(owner), profile_(profile) {
105 DCHECK(owner_);
106 DCHECK(profile_);
107
108 std::string country_code =
109 base::UTF16ToUTF8(profile_->GetRawInfo(autofill::ADDRESS_HOME_COUNTRY));
110 if (!autofill::data_util::IsValidCountryCode(country_code))
111 country_code = owner_->default_country_code_;
112
113 address_normalizer->StartAddressNormalization(
114 *profile_, country_code, kAddressNormalizationTimeoutSeconds, this);
115 }
116
117 void AddressNormalizationManager::NormalizerDelegate::OnAddressNormalized(
118 const autofill::AutofillProfile& normalized_profile) {
119 OnCompletion(normalized_profile);
120 }
121
122 void AddressNormalizationManager::NormalizerDelegate::OnCouldNotNormalize(
123 const autofill::AutofillProfile& profile) {
124 // Since the phone number is formatted in either case, this profile should
125 // be used.
126 OnCompletion(profile);
127 }
128
129 void AddressNormalizationManager::NormalizerDelegate::OnCompletion(
130 const autofill::AutofillProfile& profile) {
131 DCHECK(!has_completed_);
132 has_completed_ = true;
133 *profile_ = profile;
134 owner_->MaybeRunCompletionCallback();
135 }
136
137 } // namespace payments
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698