Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/browser/phone_number_i18n.h" | 5 #include "components/autofill/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/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
| 11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 13 #include "components/autofill/browser/autofill_country.h" | 13 #include "components/autofill/browser/autofill_country.h" |
| 14 #include "third_party/libphonenumber/src/phonenumber_api.h" | 14 #include "third_party/libphonenumber/src/phonenumber_api.h" |
| 15 | 15 |
| 16 using i18n::phonenumbers::PhoneNumber; | 16 using i18n::phonenumbers::PhoneNumber; |
| 17 using i18n::phonenumbers::PhoneNumberUtil; | 17 using i18n::phonenumbers::PhoneNumberUtil; |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 | 20 |
| 21 std::string SanitizeRegion(const std::string& region) { | 21 std::string SanitizeRegion(const std::string& region, |
| 22 const std::string& app_locale) { | |
| 22 if (region.length() == 2) | 23 if (region.length() == 2) |
| 23 return region; | 24 return region; |
| 24 | 25 |
| 25 return AutofillCountry::CountryCodeForLocale( | 26 return AutofillCountry::CountryCodeForLocale(app_locale); |
| 26 AutofillCountry::ApplicationLocale()); | |
| 27 } | 27 } |
| 28 | 28 |
| 29 // Returns true if |phone_number| is valid. | 29 // Returns true if |phone_number| is valid. |
| 30 bool IsValidPhoneNumber(const PhoneNumber& phone_number) { | 30 bool IsValidPhoneNumber(const PhoneNumber& phone_number) { |
| 31 PhoneNumberUtil* phone_util = PhoneNumberUtil::GetInstance(); | 31 PhoneNumberUtil* phone_util = PhoneNumberUtil::GetInstance(); |
| 32 if (!phone_util->IsPossibleNumber(phone_number)) | 32 if (!phone_util->IsPossibleNumber(phone_number)) |
| 33 return false; | 33 return false; |
| 34 | 34 |
| 35 // Verify that the number has a valid area code (that in some cases could be | 35 // Verify that the number has a valid area code (that in some cases could be |
| 36 // empty) for the parsed country code. Also verify that this is a valid | 36 // empty) for the parsed country code. Also verify that this is a valid |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 142 !StartsWith(normalized_number, *country_code, | 142 !StartsWith(normalized_number, *country_code, |
| 143 true /* case_sensitive */)) { | 143 true /* case_sensitive */)) { |
| 144 country_code->clear(); | 144 country_code->clear(); |
| 145 } | 145 } |
| 146 } | 146 } |
| 147 | 147 |
| 148 return true; | 148 return true; |
| 149 } | 149 } |
| 150 | 150 |
| 151 string16 NormalizePhoneNumber(const string16& value, | 151 string16 NormalizePhoneNumber(const string16& value, |
| 152 std::string const& region) { | 152 std::string const& region, |
|
Ilya Sherman
2013/04/04 04:27:38
Optional nit: As long as you're here, could you fi
jam
2013/04/04 17:58:12
Done.
| |
| 153 const std::string& app_locale) { | |
| 153 string16 country_code; | 154 string16 country_code; |
| 154 string16 unused_city_code; | 155 string16 unused_city_code; |
| 155 string16 unused_number; | 156 string16 unused_number; |
| 156 PhoneNumber phone_number; | 157 PhoneNumber phone_number; |
| 157 if (!ParsePhoneNumber(value, SanitizeRegion(region), &country_code, | 158 if (!ParsePhoneNumber(value, SanitizeRegion(region, app_locale), |
|
Ilya Sherman
2013/04/04 04:36:14
Looking through the call sites, it looks like this
jam
2013/04/04 17:58:12
Done.
| |
| 158 &unused_city_code, &unused_number, &phone_number)) { | 159 &country_code, &unused_city_code, &unused_number, |
| 160 &phone_number)) { | |
| 159 return string16(); // Parsing failed - do not store phone. | 161 return string16(); // Parsing failed - do not store phone. |
| 160 } | 162 } |
| 161 | 163 |
| 162 string16 normalized_number; | 164 string16 normalized_number; |
| 163 FormatValidatedNumber(phone_number, country_code, NULL, &normalized_number); | 165 FormatValidatedNumber(phone_number, country_code, NULL, &normalized_number); |
| 164 return normalized_number; | 166 return normalized_number; |
| 165 } | 167 } |
| 166 | 168 |
| 167 bool ConstructPhoneNumber(const string16& country_code, | 169 bool ConstructPhoneNumber(const string16& country_code, |
| 168 const string16& city_code, | 170 const string16& city_code, |
| 169 const string16& number, | 171 const string16& number, |
| 170 const std::string& region, | 172 const std::string& region, |
| 173 const std::string& app_locale, | |
| 171 string16* whole_number) { | 174 string16* whole_number) { |
| 172 whole_number->clear(); | 175 whole_number->clear(); |
| 173 | 176 |
| 174 string16 unused_country_code; | 177 string16 unused_country_code; |
| 175 string16 unused_city_code; | 178 string16 unused_city_code; |
| 176 string16 unused_number; | 179 string16 unused_number; |
| 177 PhoneNumber phone_number; | 180 PhoneNumber phone_number; |
| 178 if (!ParsePhoneNumber(country_code + city_code + number, | 181 if (!ParsePhoneNumber(country_code + city_code + number, |
| 179 SanitizeRegion(region), | 182 SanitizeRegion(region, app_locale), |
|
Ilya Sherman
2013/04/04 04:36:14
Likewise, this one can be replaced by a DCHECK.
jam
2013/04/04 17:58:12
Done.
| |
| 180 &unused_country_code, &unused_city_code, &unused_number, | 183 &unused_country_code, &unused_city_code, &unused_number, |
| 181 &phone_number)) { | 184 &phone_number)) { |
| 182 return false; | 185 return false; |
| 183 } | 186 } |
| 184 | 187 |
| 185 FormatValidatedNumber(phone_number, country_code, whole_number, NULL); | 188 FormatValidatedNumber(phone_number, country_code, whole_number, NULL); |
| 186 return true; | 189 return true; |
| 187 } | 190 } |
| 188 | 191 |
| 189 bool PhoneNumbersMatch(const string16& number_a, | 192 bool PhoneNumbersMatch(const string16& number_a, |
| 190 const string16& number_b, | 193 const string16& number_b, |
| 191 const std::string& raw_region) { | 194 const std::string& raw_region, |
| 195 const std::string& app_locale) { | |
| 192 // Sanitize the provided |raw_region| before trying to use it for parsing. | 196 // Sanitize the provided |raw_region| before trying to use it for parsing. |
| 193 const std::string region = SanitizeRegion(raw_region); | 197 const std::string region = SanitizeRegion(raw_region, app_locale); |
|
Ilya Sherman
2013/04/04 04:36:14
This one is still needed, though, due to callsites
| |
| 194 | 198 |
| 195 PhoneNumberUtil* phone_util = PhoneNumberUtil::GetInstance(); | 199 PhoneNumberUtil* phone_util = PhoneNumberUtil::GetInstance(); |
| 196 | 200 |
| 197 // Parse phone numbers based on the region | 201 // Parse phone numbers based on the region |
| 198 PhoneNumber i18n_number1; | 202 PhoneNumber i18n_number1; |
| 199 if (phone_util->Parse(UTF16ToUTF8(number_a), region.c_str(), &i18n_number1) != | 203 if (phone_util->Parse(UTF16ToUTF8(number_a), region.c_str(), &i18n_number1) != |
| 200 PhoneNumberUtil::NO_PARSING_ERROR) { | 204 PhoneNumberUtil::NO_PARSING_ERROR) { |
| 201 return false; | 205 return false; |
| 202 } | 206 } |
| 203 | 207 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 215 return false; | 219 return false; |
| 216 case PhoneNumberUtil::NSN_MATCH: | 220 case PhoneNumberUtil::NSN_MATCH: |
| 217 case PhoneNumberUtil::EXACT_MATCH: | 221 case PhoneNumberUtil::EXACT_MATCH: |
| 218 return true; | 222 return true; |
| 219 } | 223 } |
| 220 | 224 |
| 221 NOTREACHED(); | 225 NOTREACHED(); |
| 222 return false; | 226 return false; |
| 223 } | 227 } |
| 224 | 228 |
| 225 PhoneObject::PhoneObject(const string16& number, const std::string& region) | 229 PhoneObject::PhoneObject(const string16& number, |
| 226 : region_(SanitizeRegion(region)), | 230 const std::string& region, |
| 231 const std::string& app_locale) | |
| 232 : region_(SanitizeRegion(region, app_locale)), | |
|
Ilya Sherman
2013/04/04 04:36:14
Likewise, this one can be replaced by a DCHECK.
jam
2013/04/04 17:58:12
Done.
| |
| 227 i18n_number_(NULL) { | 233 i18n_number_(NULL) { |
| 228 // TODO(isherman): Autofill profiles should always have a |region| set, but in | 234 // TODO(isherman): Autofill profiles should always have a |region| set, but in |
| 229 // some cases it should be marked as implicit. Otherwise, phone numbers | 235 // some cases it should be marked as implicit. Otherwise, phone numbers |
| 230 // might behave differently when they are synced across computers: | 236 // might behave differently when they are synced across computers: |
| 231 // [ http://crbug.com/100845 ]. Once the bug is fixed, add a DCHECK here to | 237 // [ http://crbug.com/100845 ]. Once the bug is fixed, add a DCHECK here to |
| 232 // verify. | 238 // verify. |
| 233 | 239 |
| 234 scoped_ptr<PhoneNumber> i18n_number(new PhoneNumber); | 240 scoped_ptr<PhoneNumber> i18n_number(new PhoneNumber); |
| 235 if (ParsePhoneNumber(number, region_, &country_code_, &city_code_, &number_, | 241 if (ParsePhoneNumber(number, region_, &country_code_, &city_code_, &number_, |
| 236 i18n_number.get())) { | 242 i18n_number.get())) { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 287 city_code_ = other.city_code_; | 293 city_code_ = other.city_code_; |
| 288 number_ = other.number_; | 294 number_ = other.number_; |
| 289 | 295 |
| 290 formatted_number_ = other.formatted_number_; | 296 formatted_number_ = other.formatted_number_; |
| 291 whole_number_ = other.whole_number_; | 297 whole_number_ = other.whole_number_; |
| 292 | 298 |
| 293 return *this; | 299 return *this; |
| 294 } | 300 } |
| 295 | 301 |
| 296 } // namespace autofill_i18n | 302 } // namespace autofill_i18n |
| OLD | NEW |