Home | History | Annotate | Download | only in autofill
      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
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_BROWSER_AUTOFILL_PHONE_NUMBER_H_
      6 #define CHROME_BROWSER_AUTOFILL_PHONE_NUMBER_H_
      7 #pragma once
      8 
      9 #include <vector>
     10 
     11 #include "base/gtest_prod_util.h"
     12 #include "base/string16.h"
     13 #include "chrome/browser/autofill/autofill_type.h"
     14 #include "chrome/browser/autofill/form_group.h"
     15 
     16 // A form group that stores phone number information.
     17 class PhoneNumber : public FormGroup {
     18  public:
     19   PhoneNumber();
     20   explicit PhoneNumber(const PhoneNumber& number);
     21   virtual ~PhoneNumber();
     22 
     23   PhoneNumber& operator=(const PhoneNumber& number);
     24 
     25   // FormGroup implementation:
     26   virtual void GetPossibleFieldTypes(const string16& text,
     27                                      FieldTypeSet* possible_types) const;
     28   virtual void GetAvailableFieldTypes(FieldTypeSet* available_types) const;
     29   virtual string16 GetInfo(AutofillFieldType type) const;
     30   virtual void SetInfo(AutofillFieldType type, const string16& value);
     31 
     32   // Parses |value| to extract the components of a phone number.  |number|
     33   // returns the trailing 7 digits, |city_code| returns the next 3 digits, and
     34   // |country_code| returns any remaining digits.
     35   // Separator characters are stripped before parsing the digits.
     36   // Returns true if parsing was successful, false otherwise.
     37   static bool ParsePhoneNumber(const string16& value,
     38                                string16* number,
     39                                string16* city_code,
     40                                string16* country_code);
     41 
     42   // Size and offset of the prefix and suffix portions of phone numbers.
     43   static const int kPrefixOffset = 0;
     44   static const int kPrefixLength = 3;
     45   static const int kSuffixOffset = 3;
     46   static const int kSuffixLength = 4;
     47 
     48   // The following functions should return the field type for each part of the
     49   // phone number.  Currently, these are either fax or home phone number types.
     50   virtual AutofillFieldType GetNumberType() const = 0;
     51   virtual AutofillFieldType GetCityCodeType() const = 0;
     52   virtual AutofillFieldType GetCountryCodeType() const = 0;
     53   virtual AutofillFieldType GetCityAndNumberType() const = 0;
     54   virtual AutofillFieldType GetWholeNumberType() const = 0;
     55 
     56  private:
     57   FRIEND_TEST_ALL_PREFIXES(PhoneNumberTest, Matcher);
     58 
     59   const string16& country_code() const { return country_code_; }
     60   const string16& city_code() const { return city_code_; }
     61   const string16& number() const { return number_; }
     62   const string16& extension() const { return extension_; }
     63   string16 CityAndNumber() const { return city_code_ + number_; }
     64 
     65   // Returns the entire phone number as a string, without punctuation.
     66   virtual string16 WholeNumber() const;
     67 
     68   void set_country_code(const string16& country_code) {
     69     country_code_ = country_code;
     70   }
     71   void set_city_code(const string16& city_code) { city_code_ = city_code; }
     72   void set_number(const string16& number);
     73   void set_extension(const string16& extension) { extension_ = extension; }
     74   void set_whole_number(const string16& whole_number);
     75 
     76   // The numbers will be digits only (no punctuation), so any call to the IsX()
     77   // functions should first call StripPunctuation on the text.
     78   bool IsNumber(const string16& text) const;
     79   bool IsCityCode(const string16& text) const;
     80   bool IsCountryCode(const string16& text) const;
     81   bool IsCityAndNumber(const string16& text) const;
     82   bool IsWholeNumber(const string16& text) const;
     83 
     84   // Verifies that |number| is a valid phone number.
     85   bool Validate(const string16& number) const;
     86 
     87   // Removes any punctuation characters from |number|.
     88   static void StripPunctuation(string16* number);
     89 
     90   // The pieces of the phone number.
     91   string16 country_code_;
     92   string16 city_code_;  // city or area code.
     93   string16 number_;
     94   string16 extension_;
     95 };
     96 
     97 #endif  // CHROME_BROWSER_AUTOFILL_PHONE_NUMBER_H_
     98