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

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

Issue 2808983003: [Payments] Format shipping and billing phone number in normalizer. (Closed)
Patch Set: Created 3 years, 8 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
« no previous file with comments | « components/payments/core/DEPS ('k') | components/payments/core/address_normalizer_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/payments/core/address_normalizer.h" 5 #include "components/payments/core/address_normalizer.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/bind_helpers.h" 11 #include "base/bind_helpers.h"
12 #include "base/cancelable_callback.h" 12 #include "base/cancelable_callback.h"
13 #include "base/location.h" 13 #include "base/location.h"
14 #include "base/logging.h" 14 #include "base/logging.h"
15 #include "base/memory/ptr_util.h" 15 #include "base/memory/ptr_util.h"
16 #include "base/strings/utf_string_conversions.h" 16 #include "base/strings/utf_string_conversions.h"
17 #include "base/threading/sequenced_task_runner_handle.h" 17 #include "base/threading/sequenced_task_runner_handle.h"
18 #include "base/time/time.h" 18 #include "base/time/time.h"
19 #include "components/autofill/core/browser/address_i18n.h" 19 #include "components/autofill/core/browser/address_i18n.h"
20 #include "components/autofill/core/browser/autofill_profile.h" 20 #include "components/autofill/core/browser/autofill_profile.h"
21 #include "third_party/libaddressinput/chromium/chrome_address_validator.h" 21 #include "third_party/libaddressinput/chromium/chrome_address_validator.h"
22 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_da ta.h" 22 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_da ta.h"
23 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/source.h" 23 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/source.h"
24 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h" 24 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h"
25 #include "third_party/libphonenumber/phonenumber_api.h"
25 26
26 namespace payments { 27 namespace payments {
27 namespace { 28 namespace {
28 29
29 using ::autofill::AutofillProfile; 30 using ::autofill::AutofillProfile;
30 using ::i18n::addressinput::Source; 31 using ::i18n::addressinput::Source;
31 using ::i18n::addressinput::Storage; 32 using ::i18n::addressinput::Storage;
33 using ::i18n::phonenumbers::PhoneNumberUtil;
32 34
33 class AddressNormalizationRequest : public AddressNormalizer::Request { 35 class AddressNormalizationRequest : public AddressNormalizer::Request {
34 public: 36 public:
35 // The |delegate| and |address_validator| need to outlive this Request. 37 // The |delegate| and |address_validator| need to outlive this Request.
36 AddressNormalizationRequest(const AutofillProfile& profile, 38 AddressNormalizationRequest(const AutofillProfile& profile,
37 const std::string& region_code, 39 const std::string& region_code,
38 int timeout_seconds, 40 int timeout_seconds,
39 AddressNormalizer::Delegate* delegate, 41 AddressNormalizer::Delegate* delegate,
40 autofill::AddressValidator* address_validator) 42 autofill::AddressValidator* address_validator)
41 : profile_(profile), 43 : profile_(profile),
(...skipping 13 matching lines...) Expand all
55 ~AddressNormalizationRequest() override {} 57 ~AddressNormalizationRequest() override {}
56 58
57 void OnRulesLoaded(bool success) override { 59 void OnRulesLoaded(bool success) override {
58 on_timeout_.Cancel(); 60 on_timeout_.Cancel();
59 61
60 // Check if the timeout happened before the rules were loaded. 62 // Check if the timeout happened before the rules were loaded.
61 if (has_responded_) 63 if (has_responded_)
62 return; 64 return;
63 has_responded_ = true; 65 has_responded_ = true;
64 66
67 // In either case, format the phone number.
68 FormatPhoneNumber();
Mathieu 2017/04/11 18:53:31 is there a possibility we early return above and n
sebsg 2017/04/11 21:34:54 It should not, the first time this method is calle
69
65 if (!success) { 70 if (!success) {
66 delegate_->OnCouldNotNormalize(profile_); 71 delegate_->OnCouldNotNormalize(profile_);
67 return; 72 return;
68 } 73 }
69 74
70 // The rules should be loaded. 75 // The rules should be loaded.
71 DCHECK(address_validator_->AreRulesLoadedForRegion(region_code_)); 76 DCHECK(address_validator_->AreRulesLoadedForRegion(region_code_));
72 77
73 // Create the AddressData from the profile. 78 // Create the AddressData from the profile.
74 ::i18n::addressinput::AddressData address_data = 79 ::i18n::addressinput::AddressData address_data =
75 *autofill::i18n::CreateAddressDataFromAutofillProfile(profile_, 80 *autofill::i18n::CreateAddressDataFromAutofillProfile(profile_,
76 region_code_); 81 region_code_);
77 82
78 // Normalize the address. 83 // Normalize the address.
79 if (address_validator_ && 84 if (address_validator_ &&
80 address_validator_->NormalizeAddress(&address_data)) { 85 address_validator_->NormalizeAddress(&address_data)) {
81 profile_.SetRawInfo(autofill::ADDRESS_HOME_STATE, 86 profile_.SetRawInfo(autofill::ADDRESS_HOME_STATE,
82 base::UTF8ToUTF16(address_data.administrative_area)); 87 base::UTF8ToUTF16(address_data.administrative_area));
83 profile_.SetRawInfo(autofill::ADDRESS_HOME_CITY, 88 profile_.SetRawInfo(autofill::ADDRESS_HOME_CITY,
84 base::UTF8ToUTF16(address_data.locality)); 89 base::UTF8ToUTF16(address_data.locality));
85 profile_.SetRawInfo(autofill::ADDRESS_HOME_DEPENDENT_LOCALITY, 90 profile_.SetRawInfo(autofill::ADDRESS_HOME_DEPENDENT_LOCALITY,
86 base::UTF8ToUTF16(address_data.dependent_locality)); 91 base::UTF8ToUTF16(address_data.dependent_locality));
87 } 92 }
88 93
89 delegate_->OnAddressNormalized(profile_); 94 delegate_->OnAddressNormalized(profile_);
90 } 95 }
91 96
92 private: 97 private:
98 void FormatPhoneNumber() {
Mathieu 2017/04/11 18:53:31 Add a function comment, mentioning the format and
sebsg 2017/04/11 21:34:55 Done.
99 const std::string original_number = base::UTF16ToUTF8(
100 profile_.GetRawInfo(autofill::PHONE_HOME_WHOLE_NUMBER));
Mathieu 2017/04/11 18:53:31 GetRawInfo vs GetInfo? I seem to remember it matte
sebsg 2017/04/11 21:34:55 I changed it to GetInfo to be safe, but it seems l
101 i18n::phonenumbers::PhoneNumber parsed_number;
102 PhoneNumberUtil* phone_number_util = PhoneNumberUtil::GetInstance();
103 if (phone_number_util->Parse(original_number, region_code_,
104 &parsed_number) ==
105 PhoneNumberUtil::NO_PARSING_ERROR) {
106 std::string formatted_number;
107 phone_number_util->Format(parsed_number,
108 PhoneNumberUtil::PhoneNumberFormat::E164,
109 &formatted_number);
110 profile_.SetRawInfo(autofill::PHONE_HOME_WHOLE_NUMBER,
111 base::UTF8ToUTF16(formatted_number));
112 }
113 }
114
93 AutofillProfile profile_; 115 AutofillProfile profile_;
94 std::string region_code_; 116 std::string region_code_;
95 AddressNormalizer::Delegate* delegate_; 117 AddressNormalizer::Delegate* delegate_;
96 autofill::AddressValidator* address_validator_; 118 autofill::AddressValidator* address_validator_;
97 119
98 bool has_responded_; 120 bool has_responded_;
99 base::CancelableCallback<void()> on_timeout_; 121 base::CancelableCallback<void()> on_timeout_;
100 122
101 DISALLOW_COPY_AND_ASSIGN(AddressNormalizationRequest); 123 DISALLOW_COPY_AND_ASSIGN(AddressNormalizationRequest);
102 }; 124 };
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 auto it = pending_normalization_.find(region_code); 182 auto it = pending_normalization_.find(region_code);
161 if (it != pending_normalization_.end()) { 183 if (it != pending_normalization_.end()) {
162 for (size_t i = 0; i < it->second.size(); ++i) { 184 for (size_t i = 0; i < it->second.size(); ++i) {
163 it->second[i]->OnRulesLoaded(success); 185 it->second[i]->OnRulesLoaded(success);
164 } 186 }
165 pending_normalization_.erase(it); 187 pending_normalization_.erase(it);
166 } 188 }
167 } 189 }
168 190
169 } // namespace payments 191 } // namespace payments
OLDNEW
« no previous file with comments | « components/payments/core/DEPS ('k') | components/payments/core/address_normalizer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698