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

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

Issue 2889983002: Adds the AddressNormalizationManager and tests. (Closed)
Patch Set: Cleanup. 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 payments::AddressNormalizer::Delegate {
Mathieu 2017/05/17 16:45:14 no need for payments::
macourteau 2017/05/17 18:38:40 Done.
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 HasCompleted() const;
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 CompletionCallback 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>(
Mathieu 2017/05/17 16:45:14 can we use emplace_back and remove the std::unique
macourteau 2017/05/17 18:38:40 NormalizerDelegate is noncopyable (DISALLOW_COPY_A
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->HasCompleted()) {
92 return;
93 }
94 }
95
96 // We're no longer accepting requests, and all the delegates have completed.
97 // Now's the time to run the completion callback.
98 std::move(completion_callback_).Run(*this);
99 }
100
101 AddressNormalizationManager::NormalizerDelegate::NormalizerDelegate(
102 AddressNormalizationManager* owner,
103 AddressNormalizer* address_normalizer,
104 autofill::AutofillProfile* profile)
105 : owner_(owner), profile_(profile) {
106 DCHECK(owner_);
107 DCHECK(profile_);
108
109 std::string country_code =
110 base::UTF16ToUTF8(profile_->GetRawInfo(autofill::ADDRESS_HOME_COUNTRY));
111 if (!autofill::data_util::IsValidCountryCode(country_code)) {
Mathieu 2017/05/17 16:45:14 nit: no curlies
macourteau 2017/05/17 18:38:40 Done.
112 country_code = owner_->default_country_code_;
113 }
114
115 address_normalizer->StartAddressNormalization(
116 *profile_, country_code, kAddressNormalizationTimeoutSeconds, this);
117 }
118
119 bool AddressNormalizationManager::NormalizerDelegate::HasCompleted() const {
Mathieu 2017/05/17 16:45:14 has_completed() can be used
macourteau 2017/05/17 18:38:40 Done.
120 return has_completed_;
121 }
122
123 void AddressNormalizationManager::NormalizerDelegate::OnAddressNormalized(
124 const autofill::AutofillProfile& normalized_profile) {
125 OnCompletion(normalized_profile);
126 }
127
128 void AddressNormalizationManager::NormalizerDelegate::OnCouldNotNormalize(
129 const autofill::AutofillProfile& profile) {
130 // Since the phone number is formatted in either case, this profile should
131 // be used.
132 OnCompletion(profile);
133 }
134
135 void AddressNormalizationManager::NormalizerDelegate::OnCompletion(
136 const autofill::AutofillProfile& profile) {
137 DCHECK(!has_completed_);
138 has_completed_ = true;
139 *profile_ = profile;
140 owner_->MaybeRunCompletionCallback();
141 }
142
143 } // namespace payments
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698