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