phone_number_i18n.h revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
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 |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. 33bool 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. 43base::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. 54bool 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|. 62bool 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. 68class PhoneObject { 69 public: 70 PhoneObject(const base::string16& number, 71 const std::string& region); 72 PhoneObject(const PhoneObject&); 73 PhoneObject(); 74 ~PhoneObject(); 75 76 std::string region() const { return region_; } 77 78 base::string16 country_code() const { return country_code_; } 79 base::string16 city_code() const { return city_code_; } 80 base::string16 number() const { return number_; } 81 82 base::string16 GetFormattedNumber() const; 83 base::string16 GetWholeNumber() const; 84 85 PhoneObject& operator=(const PhoneObject& other); 86 87 bool IsValidNumber() const { return i18n_number_ != NULL; } 88 89 private: 90 // The region code used to parse this number. 91 std::string region_; 92 93 // The parsed number and its components. 94 // 95 scoped_ptr< ::i18n::phonenumbers::PhoneNumber> i18n_number_; 96 base::string16 city_code_; 97 base::string16 country_code_; 98 base::string16 number_; 99 100 // Pretty printed version of the whole number, or empty if parsing failed. 101 // Set on first request. 102 mutable base::string16 formatted_number_; 103 104 // The whole number, normalized to contain only digits if possible. 105 // Set on first request. 106 mutable base::string16 whole_number_; 107}; 108 109} // namespace i18n 110} // namespace autofill 111 112#endif // COMPONENTS_AUTOFILL_CORE_BROWSER_PHONE_NUMBER_I18N_H_ 113