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 3 // found in the LICENSE file. 4 5 #ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_PHONE_NUMBER_I18N_H_ 6 #define COMPONENTS_AUTOFILL_CORE_BROWSER_PHONE_NUMBER_I18N_H_ 7 8 #include <string> 9 #include <vector> 10 11 #include "base/compiler_specific.h" 12 #include "base/memory/scoped_ptr.h" 13 #include "base/strings/string16.h" 14 15 namespace i18n { 16 namespace phonenumbers { 17 class PhoneNumber; 18 } 19 } 20 21 namespace autofill { 22 23 // Utilities to process, normalize and compare international phone numbers. 24 namespace i18n { 25 26 // Most of the following functions require |region| to operate. The |region| is 27 // a ISO 3166 standard code ("US" for USA, "CZ" for Czech Republic, etc.). 28 29 // Parses the number stored in |value| as a phone number interpreted in the 30 // given |region|, and stores the results into the remaining arguments. The 31 // |region| should be a 2-letter country code. This is an internal function, 32 // exposed in the header file so that it can be tested. 33 bool ParsePhoneNumber( 34 const base::string16& value, 35 const std::string& region, 36 base::string16* country_code, 37 base::string16* city_code, 38 base::string16* number, 39 ::i18n::phonenumbers::PhoneNumber* i18n_number) WARN_UNUSED_RESULT; 40 41 // Normalizes phone number, by changing digits in the extended fonts 42 // (such as \xFF1x) into '0'-'9'. Also strips out non-digit characters. 43 base::string16 NormalizePhoneNumber(const base::string16& value, 44 const std::string& region); 45 46 // Constructs whole phone number from parts. 47 // |city_code| - area code, could be empty. 48 // |country_code| - country code, could be empty. 49 // |number| - local number, should not be empty. 50 // |region| - current region, the parsing is based on. 51 // |whole_number| - constructed whole number. 52 // Separator characters are stripped before parsing the digits. 53 // Returns true if parsing was successful, false otherwise. 54 bool ConstructPhoneNumber(const base::string16& country_code, 55 const base::string16& city_code, 56 const base::string16& number, 57 const std::string& region, 58 base::string16* whole_number) WARN_UNUSED_RESULT; 59 60 // Returns true if |number_a| and |number_b| parse to the same phone number in 61 // the given |region|. 62 bool PhoneNumbersMatch(const base::string16& number_a, 63 const base::string16& number_b, 64 const std::string& region, 65 const std::string& app_locale); 66 67 // The cached phone number, does parsing only once, improves performance. 68 class PhoneObject { 69 public: 70 PhoneObject(const base::string16& number, 71 const std::string& region); 72 PhoneObject(const PhoneObject&); 73 PhoneObject(); 74 ~PhoneObject(); 75 76 const std::string& region() const { return region_; } 77 78 const base::string16& country_code() const { return country_code_; } 79 const base::string16& city_code() const { return city_code_; } 80 const base::string16& number() const { return number_; } 81 82 const base::string16& GetFormattedNumber() const; 83 base::string16 GetNationallyFormattedNumber() const; 84 const base::string16& GetWholeNumber() const; 85 86 PhoneObject& operator=(const PhoneObject& other); 87 88 bool IsValidNumber() const { return i18n_number_ != NULL; } 89 90 private: 91 // The region code used to parse this number. 92 std::string region_; 93 94 // The parsed number and its components. 95 // 96 scoped_ptr< ::i18n::phonenumbers::PhoneNumber> i18n_number_; 97 base::string16 city_code_; 98 base::string16 country_code_; 99 base::string16 number_; 100 101 // Pretty printed version of the whole number, or empty if parsing failed. 102 // Set on first request. 103 mutable base::string16 formatted_number_; 104 105 // The whole number, normalized to contain only digits if possible. 106 // Set on first request. 107 mutable base::string16 whole_number_; 108 }; 109 110 } // namespace i18n 111 } // namespace autofill 112 113 #endif // COMPONENTS_AUTOFILL_CORE_BROWSER_PHONE_NUMBER_I18N_H_ 114