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 #include "chrome/browser/autofill/autofill_type.h"
      6 
      7 #include <ostream>
      8 
      9 #include "base/logging.h"
     10 
     11 namespace {
     12 
     13 const AutofillType::AutofillTypeDefinition kUnknownAutofillTypeDefinition = {
     14   /* UNKNOWN_TYPE */ AutofillType::NO_GROUP, AutofillType::NO_SUBGROUP
     15 };
     16 
     17 AutofillType::AutofillTypeDefinition kAutofillTypeDefinitions[] = {
     18   // NO_SERVER_DATA
     19   { AutofillType::NO_GROUP, AutofillType::NO_SUBGROUP },
     20   // UNKNOWN_TYPE
     21   kUnknownAutofillTypeDefinition,
     22   // EMPTY_TYPE
     23   { AutofillType::NO_GROUP, AutofillType::NO_SUBGROUP },
     24 
     25   // NAME_FIRST
     26   { AutofillType::NAME, AutofillType::NO_SUBGROUP },
     27   // NAME_MIDDLE
     28   { AutofillType::NAME, AutofillType::NO_SUBGROUP },
     29   // NAME_LAST
     30   { AutofillType::NAME, AutofillType::NO_SUBGROUP },
     31   // NAME_MIDDLE_INITIAL
     32   { AutofillType::NAME, AutofillType::NO_SUBGROUP },
     33   // NAME_FULL
     34   { AutofillType::NAME, AutofillType::NO_SUBGROUP },
     35   // NAME_SUFFIX
     36   { AutofillType::NAME, AutofillType::NO_SUBGROUP },
     37 
     38   // EMAIL_ADDRESS
     39   { AutofillType::EMAIL, AutofillType::NO_SUBGROUP },
     40 
     41   // PHONE_HOME_NUMBER
     42   { AutofillType::PHONE_HOME, AutofillType::PHONE_NUMBER },
     43   // PHONE_HOME_CITY_CODE
     44   { AutofillType::PHONE_HOME, AutofillType::PHONE_CITY_CODE },
     45   // PHONE_HOME_COUNTRY_CODE
     46   { AutofillType::PHONE_HOME, AutofillType::PHONE_COUNTRY_CODE },
     47   // PHONE_HOME_CITY_AND_NUMBER
     48   { AutofillType::PHONE_HOME, AutofillType::PHONE_CITY_AND_NUMBER },
     49   // PHONE_HOME_WHOLE_NUMBER
     50   { AutofillType::PHONE_HOME, AutofillType::PHONE_WHOLE_NUMBER },
     51 
     52   // Work phone numbers (values [15,19]) are deprecated.
     53   kUnknownAutofillTypeDefinition,
     54   kUnknownAutofillTypeDefinition,
     55   kUnknownAutofillTypeDefinition,
     56   kUnknownAutofillTypeDefinition,
     57   kUnknownAutofillTypeDefinition,
     58 
     59   // PHONE_FAX_NUMBER
     60   { AutofillType::PHONE_FAX, AutofillType::PHONE_NUMBER },
     61   // PHONE_FAX_CITY_CODE
     62   { AutofillType::PHONE_FAX, AutofillType::PHONE_CITY_CODE },
     63   // PHONE_FAX_COUNTRY_CODE
     64   { AutofillType::PHONE_FAX, AutofillType::PHONE_COUNTRY_CODE },
     65   // PHONE_FAX_CITY_AND_NUMBER
     66   { AutofillType::PHONE_FAX, AutofillType::PHONE_CITY_AND_NUMBER },
     67   // PHONE_FAX_WHOLE_NUMBER
     68   { AutofillType::PHONE_FAX, AutofillType::PHONE_WHOLE_NUMBER },
     69 
     70   // Cell phone numbers (values [25, 29]) are deprecated.
     71   kUnknownAutofillTypeDefinition,
     72   kUnknownAutofillTypeDefinition,
     73   kUnknownAutofillTypeDefinition,
     74   kUnknownAutofillTypeDefinition,
     75   kUnknownAutofillTypeDefinition,
     76 
     77   // ADDRESS_HOME_LINE1
     78   { AutofillType::ADDRESS_HOME, AutofillType::ADDRESS_LINE1 },
     79   // ADDRESS_HOME_LINE2
     80   { AutofillType::ADDRESS_HOME, AutofillType::ADDRESS_LINE2 },
     81   // ADDRESS_HOME_APT_NUM
     82   { AutofillType::ADDRESS_HOME, AutofillType::ADDRESS_APT_NUM },
     83   // ADDRESS_HOME_CITY
     84   { AutofillType::ADDRESS_HOME, AutofillType::ADDRESS_CITY },
     85   // ADDRESS_HOME_STATE
     86   { AutofillType::ADDRESS_HOME, AutofillType::ADDRESS_STATE },
     87   // ADDRESS_HOME_ZIP
     88   { AutofillType::ADDRESS_HOME, AutofillType::ADDRESS_ZIP },
     89   // ADDRESS_HOME_COUNTRY
     90   { AutofillType::ADDRESS_HOME, AutofillType::ADDRESS_COUNTRY },
     91 
     92   // ADDRESS_BILLING_LINE1
     93   { AutofillType::ADDRESS_BILLING, AutofillType::ADDRESS_LINE1 },
     94   // ADDRESS_BILLING_LINE2
     95   { AutofillType::ADDRESS_BILLING, AutofillType::ADDRESS_LINE2 },
     96   // ADDRESS_BILLING_APT_NUM
     97   { AutofillType::ADDRESS_BILLING, AutofillType::ADDRESS_APT_NUM },
     98   // ADDRESS_BILLING_CITY
     99   { AutofillType::ADDRESS_BILLING, AutofillType::ADDRESS_CITY },
    100   // ADDRESS_BILLING_STATE
    101   { AutofillType::ADDRESS_BILLING, AutofillType::ADDRESS_STATE },
    102   // ADDRESS_BILLING_ZIP
    103   { AutofillType::ADDRESS_BILLING, AutofillType::ADDRESS_ZIP },
    104   // ADDRESS_BILLING_COUNTRY
    105   { AutofillType::ADDRESS_BILLING, AutofillType::ADDRESS_COUNTRY },
    106 
    107   // ADDRESS_SHIPPING values [44,50] are deprecated.
    108   kUnknownAutofillTypeDefinition,
    109   kUnknownAutofillTypeDefinition,
    110   kUnknownAutofillTypeDefinition,
    111   kUnknownAutofillTypeDefinition,
    112   kUnknownAutofillTypeDefinition,
    113   kUnknownAutofillTypeDefinition,
    114   kUnknownAutofillTypeDefinition,
    115 
    116   // CREDIT_CARD_NAME
    117   { AutofillType::CREDIT_CARD, AutofillType::NO_SUBGROUP },
    118   // CREDIT_CARD_NUMBER
    119   { AutofillType::CREDIT_CARD, AutofillType::NO_SUBGROUP },
    120   // CREDIT_CARD_EXP_MONTH
    121   { AutofillType::CREDIT_CARD, AutofillType::NO_SUBGROUP },
    122   // CREDIT_CARD_EXP_2_DIGIT_YEAR
    123   { AutofillType::CREDIT_CARD, AutofillType::NO_SUBGROUP },
    124   // CREDIT_CARD_EXP_4_DIGIT_YEAR
    125   { AutofillType::CREDIT_CARD, AutofillType::NO_SUBGROUP },
    126   // CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR
    127   { AutofillType::CREDIT_CARD, AutofillType::NO_SUBGROUP },
    128   // CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR
    129   { AutofillType::CREDIT_CARD, AutofillType::NO_SUBGROUP },
    130   // CREDIT_CARD_TYPE
    131   { AutofillType::CREDIT_CARD, AutofillType::NO_SUBGROUP },
    132   // CREDIT_CARD_VERIFICATION_CODE
    133   { AutofillType::CREDIT_CARD, AutofillType::NO_SUBGROUP },
    134 
    135   // COMPANY_NAME
    136   { AutofillType::COMPANY, AutofillType::NO_SUBGROUP },
    137 };
    138 
    139 }  // namespace
    140 
    141 AutofillType::AutofillType(AutofillFieldType field_type) {
    142   if ((field_type < NO_SERVER_DATA || field_type >= MAX_VALID_FIELD_TYPE) ||
    143       (field_type >= 15 && field_type <= 19) ||
    144       (field_type >= 25 && field_type <= 29) ||
    145       (field_type >= 44 && field_type <= 50))
    146     field_type_ = UNKNOWN_TYPE;
    147   else
    148     field_type_ = field_type;
    149 }
    150 
    151 AutofillType::AutofillType(const AutofillType& autofill_type) {
    152   *this = autofill_type;
    153 }
    154 
    155 AutofillType& AutofillType::operator=(const AutofillType& autofill_type) {
    156   if (this != &autofill_type)
    157     this->field_type_ = autofill_type.field_type_;
    158   return *this;
    159 }
    160 
    161 AutofillFieldType AutofillType::field_type() const {
    162   return field_type_;
    163 }
    164 
    165 FieldTypeGroup AutofillType::group() const {
    166   return kAutofillTypeDefinitions[field_type_].group;
    167 }
    168 
    169 FieldTypeSubGroup AutofillType::subgroup() const {
    170   return kAutofillTypeDefinitions[field_type_].subgroup;
    171 }
    172 
    173 // static
    174 AutofillFieldType AutofillType::GetEquivalentFieldType(
    175     AutofillFieldType field_type) {
    176   // When billing information is requested from the profile we map to the
    177   // home address equivalents.
    178   switch (field_type) {
    179     case ADDRESS_BILLING_LINE1:
    180       return ADDRESS_HOME_LINE1;
    181 
    182     case ADDRESS_BILLING_LINE2:
    183       return ADDRESS_HOME_LINE2;
    184 
    185     case ADDRESS_BILLING_APT_NUM:
    186       return ADDRESS_HOME_APT_NUM;
    187 
    188     case ADDRESS_BILLING_CITY:
    189       return ADDRESS_HOME_CITY;
    190 
    191     case ADDRESS_BILLING_STATE:
    192       return ADDRESS_HOME_STATE;
    193 
    194     case ADDRESS_BILLING_ZIP:
    195       return ADDRESS_HOME_ZIP;
    196 
    197     case ADDRESS_BILLING_COUNTRY:
    198       return ADDRESS_HOME_COUNTRY;
    199 
    200     default:
    201       return field_type;
    202   }
    203 }
    204 
    205 // static
    206 std::string AutofillType::FieldTypeToString(AutofillFieldType type) {
    207   switch (type) {
    208     case NO_SERVER_DATA:
    209       return "NO_SERVER_DATA";
    210     case UNKNOWN_TYPE:
    211       return "UNKNOWN_TYPE";
    212     case EMPTY_TYPE:
    213       return "EMPTY_TYPE";
    214     case NAME_FIRST:
    215       return "NAME_FIRST";
    216     case NAME_MIDDLE:
    217       return "NAME_MIDDLE";
    218     case NAME_LAST:
    219       return "NAME_LAST";
    220     case NAME_MIDDLE_INITIAL:
    221       return "NAME_MIDDLE_INITIAL";
    222     case NAME_FULL:
    223       return "NAME_FULL";
    224     case NAME_SUFFIX:
    225       return "NAME_SUFFIX";
    226     case EMAIL_ADDRESS:
    227       return "EMAIL_ADDRESS";
    228     case PHONE_HOME_NUMBER:
    229       return "PHONE_HOME_NUMBER";
    230     case PHONE_HOME_CITY_CODE:
    231       return "PHONE_HOME_CITY_CODE";
    232     case PHONE_HOME_COUNTRY_CODE:
    233       return "PHONE_HOME_COUNTRY_CODE";
    234     case PHONE_HOME_CITY_AND_NUMBER:
    235       return "PHONE_HOME_CITY_AND_NUMBER";
    236     case PHONE_HOME_WHOLE_NUMBER:
    237       return "PHONE_HOME_WHOLE_NUMBER";
    238     case PHONE_FAX_NUMBER:
    239       return "PHONE_FAX_NUMBER";
    240     case PHONE_FAX_CITY_CODE:
    241       return "PHONE_FAX_CITY_CODE";
    242     case PHONE_FAX_COUNTRY_CODE:
    243       return "PHONE_FAX_COUNTRY_CODE";
    244     case PHONE_FAX_CITY_AND_NUMBER:
    245       return "PHONE_FAX_CITY_AND_NUMBER";
    246     case PHONE_FAX_WHOLE_NUMBER:
    247       return "PHONE_FAX_WHOLE_NUMBER";
    248     case ADDRESS_HOME_LINE1:
    249       return "ADDRESS_HOME_LINE1";
    250     case ADDRESS_HOME_LINE2:
    251       return "ADDRESS_HOME_LINE2";
    252     case ADDRESS_HOME_APT_NUM:
    253       return "ADDRESS_HOME_APT_NUM";
    254     case ADDRESS_HOME_CITY:
    255       return "ADDRESS_HOME_CITY";
    256     case ADDRESS_HOME_STATE:
    257       return "ADDRESS_HOME_STATE";
    258     case ADDRESS_HOME_ZIP:
    259       return "ADDRESS_HOME_ZIP";
    260     case ADDRESS_HOME_COUNTRY:
    261       return "ADDRESS_HOME_COUNTRY";
    262     case ADDRESS_BILLING_LINE1:
    263       return "ADDRESS_BILLING_LINE1";
    264     case ADDRESS_BILLING_LINE2:
    265       return "ADDRESS_BILLING_LINE2";
    266     case ADDRESS_BILLING_APT_NUM:
    267       return "ADDRESS_BILLING_APT_NUM";
    268     case ADDRESS_BILLING_CITY:
    269       return "ADDRESS_BILLING_CITY";
    270     case ADDRESS_BILLING_STATE:
    271       return "ADDRESS_BILLING_STATE";
    272     case ADDRESS_BILLING_ZIP:
    273       return "ADDRESS_BILLING_ZIP";
    274     case ADDRESS_BILLING_COUNTRY:
    275       return "ADDRESS_BILLING_COUNTRY";
    276     case CREDIT_CARD_NAME:
    277       return "CREDIT_CARD_NAME";
    278     case CREDIT_CARD_NUMBER:
    279       return "CREDIT_CARD_NUMBER";
    280     case CREDIT_CARD_EXP_MONTH:
    281       return "CREDIT_CARD_EXP_MONTH";
    282     case CREDIT_CARD_EXP_2_DIGIT_YEAR:
    283       return "CREDIT_CARD_EXP_2_DIGIT_YEAR";
    284     case CREDIT_CARD_EXP_4_DIGIT_YEAR:
    285       return "CREDIT_CARD_EXP_4_DIGIT_YEAR";
    286     case CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR:
    287       return "CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR";
    288     case CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR:
    289       return "CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR";
    290     case CREDIT_CARD_TYPE:
    291       return "CREDIT_CARD_TYPE";
    292     case CREDIT_CARD_VERIFICATION_CODE:
    293       return "CREDIT_CARD_VERIFICATION_CODE";
    294     case COMPANY_NAME:
    295       return "COMPANY_NAME";
    296     default:
    297       NOTREACHED() << "Invalid AutofillFieldType value.";
    298   }
    299   return std::string();
    300 }
    301 
    302 // static
    303 AutofillFieldType AutofillType::StringToFieldType(const std::string& str) {
    304   if (str == "NO_SERVER_DATA")
    305     return NO_SERVER_DATA;
    306   if (str == "UNKNOWN_TYPE")
    307     return UNKNOWN_TYPE;
    308   if (str == "EMPTY_TYPE")
    309     return EMPTY_TYPE;
    310   if (str == "NAME_FIRST")
    311     return NAME_FIRST;
    312   if (str == "NAME_MIDDLE")
    313     return NAME_MIDDLE;
    314   if (str == "NAME_LAST")
    315     return NAME_LAST;
    316   if (str == "NAME_MIDDLE_INITIAL")
    317     return NAME_MIDDLE_INITIAL;
    318   if (str == "NAME_FULL")
    319     return NAME_FULL;
    320   if (str == "NAME_SUFFIX")
    321     return NAME_SUFFIX;
    322   if (str == "EMAIL_ADDRESS")
    323     return EMAIL_ADDRESS;
    324   if (str == "PHONE_HOME_NUMBER")
    325     return PHONE_HOME_NUMBER;
    326   if (str == "PHONE_HOME_CITY_CODE")
    327     return PHONE_HOME_CITY_CODE;
    328   if (str == "PHONE_HOME_COUNTRY_CODE")
    329     return PHONE_HOME_COUNTRY_CODE;
    330   if (str == "PHONE_HOME_CITY_AND_NUMBER")
    331     return PHONE_HOME_CITY_AND_NUMBER;
    332   if (str == "PHONE_HOME_WHOLE_NUMBER")
    333     return PHONE_HOME_WHOLE_NUMBER;
    334   if (str == "PHONE_FAX_NUMBER")
    335     return PHONE_FAX_NUMBER;
    336   if (str == "PHONE_FAX_CITY_CODE")
    337     return PHONE_FAX_CITY_CODE;
    338   if (str == "PHONE_FAX_COUNTRY_CODE")
    339     return PHONE_FAX_COUNTRY_CODE;
    340   if (str == "PHONE_FAX_CITY_AND_NUMBER")
    341     return PHONE_FAX_CITY_AND_NUMBER;
    342   if (str == "PHONE_FAX_WHOLE_NUMBER")
    343     return PHONE_FAX_WHOLE_NUMBER;
    344   if (str == "ADDRESS_HOME_LINE1")
    345     return ADDRESS_HOME_LINE1;
    346   if (str == "ADDRESS_HOME_LINE2")
    347     return ADDRESS_HOME_LINE2;
    348   if (str == "ADDRESS_HOME_APT_NUM")
    349     return ADDRESS_HOME_APT_NUM;
    350   if (str == "ADDRESS_HOME_CITY")
    351     return ADDRESS_HOME_CITY;
    352   if (str == "ADDRESS_HOME_STATE")
    353     return ADDRESS_HOME_STATE;
    354   if (str == "ADDRESS_HOME_ZIP")
    355     return ADDRESS_HOME_ZIP;
    356   if (str == "ADDRESS_HOME_COUNTRY")
    357     return ADDRESS_HOME_COUNTRY;
    358   if (str == "ADDRESS_BILLING_LINE1")
    359     return ADDRESS_BILLING_LINE1;
    360   if (str == "ADDRESS_BILLING_LINE2")
    361     return ADDRESS_BILLING_LINE2;
    362   if (str == "ADDRESS_BILLING_APT_NUM")
    363     return ADDRESS_BILLING_APT_NUM;
    364   if (str == "ADDRESS_BILLING_CITY")
    365     return ADDRESS_BILLING_CITY;
    366   if (str == "ADDRESS_BILLING_STATE")
    367     return ADDRESS_BILLING_STATE;
    368   if (str == "ADDRESS_BILLING_ZIP")
    369     return ADDRESS_BILLING_ZIP;
    370   if (str == "ADDRESS_BILLING_COUNTRY")
    371     return ADDRESS_BILLING_COUNTRY;
    372   if (str == "CREDIT_CARD_NAME")
    373     return CREDIT_CARD_NAME;
    374   if (str == "CREDIT_CARD_NUMBER")
    375     return CREDIT_CARD_NUMBER;
    376   if (str == "CREDIT_CARD_EXP_MONTH")
    377     return CREDIT_CARD_EXP_MONTH;
    378   if (str == "CREDIT_CARD_EXP_2_DIGIT_YEAR")
    379     return CREDIT_CARD_EXP_2_DIGIT_YEAR;
    380   if (str == "CREDIT_CARD_EXP_4_DIGIT_YEAR")
    381     return CREDIT_CARD_EXP_4_DIGIT_YEAR;
    382   if (str == "CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR")
    383     return CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR;
    384   if (str == "CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR")
    385     return CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR;
    386   if (str == "CREDIT_CARD_TYPE")
    387     return CREDIT_CARD_TYPE;
    388   if (str == "CREDIT_CARD_VERIFICATION_CODE")
    389     return CREDIT_CARD_VERIFICATION_CODE;
    390   if (str == "COMPANY_NAME")
    391     return COMPANY_NAME;
    392 
    393   NOTREACHED() << "Unknown AutofillFieldType " << str;
    394   return UNKNOWN_TYPE;
    395 }
    396