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

Side by Side Diff: components/autofill/core/browser/phone_number_i18n.cc

Issue 355823007: Adjust autofill phone number format. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update code and add tests. Created 6 years, 5 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
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/autofill/core/browser/phone_number_i18n.h" 5 #include "components/autofill/core/browser/phone_number_i18n.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/strings/string_number_conversions.h" 9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 const base::string16& country_code, 56 const base::string16& country_code,
57 base::string16* formatted_number, 57 base::string16* formatted_number,
58 base::string16* normalized_number) { 58 base::string16* normalized_number) {
59 PhoneNumberUtil::PhoneNumberFormat format = 59 PhoneNumberUtil::PhoneNumberFormat format =
60 country_code.empty() ? 60 country_code.empty() ?
61 PhoneNumberUtil::NATIONAL : 61 PhoneNumberUtil::NATIONAL :
62 PhoneNumberUtil::INTERNATIONAL; 62 PhoneNumberUtil::INTERNATIONAL;
63 63
64 PhoneNumberUtil* phone_util = PhoneNumberUtil::GetInstance(); 64 PhoneNumberUtil* phone_util = PhoneNumberUtil::GetInstance();
65 std::string processed_number; 65 std::string processed_number;
66 phone_util->Format(number, format, &processed_number); 66
67 std::string region_code;
68 phone_util->GetRegionCodeForNumber(number, &region_code);
69
70 // Formats a phone number in the specified format using default rules.
71 // This applies to the following cases:
72 // The phone number contains alphabetic letter(s).
73 // The phone number with a leading IDD.
Ilya Sherman 2014/07/02 22:23:54 nit: Please spell out the acronym "IDD", and clari
74 // The phone number without a leading '+' sign.
75 if (phone_util->IsAlphaNumber(number.raw_input()) ||
76 (number.has_country_code_source() &&
77 (number.country_code_source() == PhoneNumber::FROM_NUMBER_WITH_IDD ||
78 number.country_code_source() ==
79 PhoneNumber::FROM_NUMBER_WITHOUT_PLUS_SIGN))) {
80 phone_util->Format(number, format, &processed_number);
81 } else {
82 phone_util->FormatInOriginalFormat (number, region_code, &processed_number);
Ilya Sherman 2014/07/02 22:23:54 nit: Please omit the space before the paren.
83 }
67 84
68 if (formatted_number) 85 if (formatted_number)
69 *formatted_number = base::UTF8ToUTF16(processed_number); 86 *formatted_number = base::UTF8ToUTF16(processed_number);
Ilya Sherman 2014/07/02 22:23:54 Where is the |formatted_number| used? Should the
70 87
71 if (normalized_number) { 88 if (normalized_number) {
72 phone_util->NormalizeDigitsOnly(&processed_number); 89 // Keep the leading '+' while normalising numbers, excluding US numbers.
90 if (processed_number[0] == '+' && region_code.compare("US")) {
Ilya Sherman 2014/07/02 22:23:54 nit: Please write the second condition as |region_
91 processed_number = processed_number.substr(1, processed_number.length());
92 phone_util->NormalizeDigitsOnly(&processed_number);
93 processed_number.insert(processed_number.begin(), '+');
94 } else {
95 phone_util->NormalizeDigitsOnly(&processed_number);
96 }
73 *normalized_number = base::UTF8ToUTF16(processed_number); 97 *normalized_number = base::UTF8ToUTF16(processed_number);
74 } 98 }
75 } 99 }
76 100
77 } // namespace 101 } // namespace
78 102
79 namespace i18n { 103 namespace i18n {
80 104
81 // Parses the number stored in |value| as it should be interpreted in the given 105 // Parses the number stored in |value| as it should be interpreted in the given
82 // |default_region|, and stores the results into the remaining arguments. 106 // |default_region|, and stores the results into the remaining arguments.
(...skipping 10 matching lines...) Expand all
93 number->clear(); 117 number->clear();
94 *i18n_number = PhoneNumber(); 118 *i18n_number = PhoneNumber();
95 119
96 std::string number_text(base::UTF16ToUTF8(value)); 120 std::string number_text(base::UTF16ToUTF8(value));
97 121
98 // Parse phone number based on the region. 122 // Parse phone number based on the region.
99 PhoneNumberUtil* phone_util = PhoneNumberUtil::GetInstance(); 123 PhoneNumberUtil* phone_util = PhoneNumberUtil::GetInstance();
100 124
101 // The |default_region| should already be sanitized. 125 // The |default_region| should already be sanitized.
102 DCHECK_EQ(2U, default_region.size()); 126 DCHECK_EQ(2U, default_region.size());
103 if (phone_util->Parse(number_text, default_region, i18n_number) != 127 if (phone_util->ParseAndKeepRawInput(number_text,
128 default_region,
129 i18n_number) !=
104 PhoneNumberUtil::NO_PARSING_ERROR) { 130 PhoneNumberUtil::NO_PARSING_ERROR) {
105 return false; 131 return false;
106 } 132 }
107 133
108 if (!IsValidPhoneNumber(*i18n_number)) 134 if (!IsValidPhoneNumber(*i18n_number))
109 return false; 135 return false;
110 136
111 std::string national_significant_number; 137 std::string national_significant_number;
112 phone_util->GetNationalSignificantNumber(*i18n_number, 138 phone_util->GetNationalSignificantNumber(*i18n_number,
113 &national_significant_number); 139 &national_significant_number);
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
300 number_ = other.number_; 326 number_ = other.number_;
301 327
302 formatted_number_ = other.formatted_number_; 328 formatted_number_ = other.formatted_number_;
303 whole_number_ = other.whole_number_; 329 whole_number_ = other.whole_number_;
304 330
305 return *this; 331 return *this;
306 } 332 }
307 333
308 } // namespace i18n 334 } // namespace i18n
309 } // namespace autofill 335 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698