Home | History | Annotate | Download | only in browser
      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 #include "components/autofill/core/browser/address.h"
      6 
      7 #include <stddef.h>
      8 
      9 #include "base/basictypes.h"
     10 #include "base/logging.h"
     11 #include "base/strings/string_util.h"
     12 #include "base/strings/utf_string_conversions.h"
     13 #include "components/autofill/core/browser/autofill_country.h"
     14 #include "components/autofill/core/browser/autofill_field.h"
     15 #include "components/autofill/core/browser/autofill_type.h"
     16 
     17 namespace {
     18 
     19 const char16 kAddressSplitChars[] = {'-', ',', '#', '.', ' ', 0};
     20 
     21 }  // namespace
     22 
     23 namespace autofill {
     24 
     25 Address::Address() {}
     26 
     27 Address::Address(const Address& address) : FormGroup() {
     28   *this = address;
     29 }
     30 
     31 Address::~Address() {}
     32 
     33 Address& Address::operator=(const Address& address) {
     34   if (this == &address)
     35     return *this;
     36 
     37   line1_ = address.line1_;
     38   line2_ = address.line2_;
     39   city_ = address.city_;
     40   state_ = address.state_;
     41   country_code_ = address.country_code_;
     42   zip_code_ = address.zip_code_;
     43   return *this;
     44 }
     45 
     46 base::string16 Address::GetRawInfo(ServerFieldType type) const {
     47   // TODO(isherman): Is GetStorableType even necessary?
     48   switch (AutofillType(type).GetStorableType()) {
     49     case ADDRESS_HOME_LINE1:
     50       return line1_;
     51 
     52     case ADDRESS_HOME_LINE2:
     53       return line2_;
     54 
     55     case ADDRESS_HOME_CITY:
     56       return city_;
     57 
     58     case ADDRESS_HOME_STATE:
     59       return state_;
     60 
     61     case ADDRESS_HOME_ZIP:
     62       return zip_code_;
     63 
     64     case ADDRESS_HOME_COUNTRY:
     65       return ASCIIToUTF16(country_code_);
     66 
     67     default:
     68       return base::string16();
     69   }
     70 }
     71 
     72 void Address::SetRawInfo(ServerFieldType type, const base::string16& value) {
     73   // TODO(isherman): Is GetStorableType even necessary?
     74   switch (AutofillType(type).GetStorableType()) {
     75     case ADDRESS_HOME_LINE1:
     76       line1_ = value;
     77       break;
     78 
     79     case ADDRESS_HOME_LINE2:
     80       line2_ = value;
     81       break;
     82 
     83     case ADDRESS_HOME_CITY:
     84       city_ = value;
     85       break;
     86 
     87     case ADDRESS_HOME_STATE:
     88       state_ = value;
     89       break;
     90 
     91     case ADDRESS_HOME_COUNTRY:
     92       DCHECK(value.empty() ||
     93              (value.length() == 2u && IsStringASCII(value)));
     94       country_code_ = UTF16ToASCII(value);
     95       break;
     96 
     97     case ADDRESS_HOME_ZIP:
     98       zip_code_ = value;
     99       break;
    100 
    101     default:
    102       NOTREACHED();
    103   }
    104 }
    105 
    106 base::string16 Address::GetInfo(const AutofillType& type,
    107                                 const std::string& app_locale) const {
    108   if (type.html_type() == HTML_TYPE_COUNTRY_CODE) {
    109     return ASCIIToUTF16(country_code_);
    110   } else if (type.html_type() == HTML_TYPE_STREET_ADDRESS) {
    111     base::string16 address = line1_;
    112     if (!line2_.empty())
    113       address += ASCIIToUTF16(", ") + line2_;
    114     return address;
    115   }
    116 
    117   ServerFieldType storable_type = type.GetStorableType();
    118   if (storable_type == ADDRESS_HOME_COUNTRY && !country_code_.empty())
    119     return AutofillCountry(country_code_, app_locale).name();
    120 
    121   return GetRawInfo(storable_type);
    122 }
    123 
    124 bool Address::SetInfo(const AutofillType& type,
    125                       const base::string16& value,
    126                       const std::string& app_locale) {
    127   if (type.html_type() == HTML_TYPE_COUNTRY_CODE) {
    128     if (!value.empty() && (value.size() != 2u || !IsStringASCII(value))) {
    129       country_code_ = std::string();
    130       return false;
    131     }
    132 
    133     country_code_ = StringToUpperASCII(UTF16ToASCII(value));
    134     return true;
    135   } else if (type.html_type() == HTML_TYPE_STREET_ADDRESS) {
    136     // Don't attempt to parse the address into lines, since this is potentially
    137     // a user-entered address in the user's own format, so the code would have
    138     // to rely on iffy heuristics at best.  Instead, just give up when importing
    139     // addresses like this.
    140     line1_ = line2_ = base::string16();
    141     return false;
    142   }
    143 
    144   ServerFieldType storable_type = type.GetStorableType();
    145   if (storable_type == ADDRESS_HOME_COUNTRY && !value.empty()) {
    146     country_code_ = AutofillCountry::GetCountryCode(value, app_locale);
    147     return !country_code_.empty();
    148   }
    149 
    150   SetRawInfo(storable_type, value);
    151   return true;
    152 }
    153 
    154 void Address::GetMatchingTypes(const base::string16& text,
    155                                const std::string& app_locale,
    156                                ServerFieldTypeSet* matching_types) const {
    157   FormGroup::GetMatchingTypes(text, app_locale, matching_types);
    158 
    159   // Check to see if the |text| canonicalized as a country name is a match.
    160   std::string country_code = AutofillCountry::GetCountryCode(text, app_locale);
    161   if (!country_code.empty() && country_code_ == country_code)
    162     matching_types->insert(ADDRESS_HOME_COUNTRY);
    163 }
    164 
    165 void Address::GetSupportedTypes(ServerFieldTypeSet* supported_types) const {
    166   supported_types->insert(ADDRESS_HOME_LINE1);
    167   supported_types->insert(ADDRESS_HOME_LINE2);
    168   supported_types->insert(ADDRESS_HOME_CITY);
    169   supported_types->insert(ADDRESS_HOME_STATE);
    170   supported_types->insert(ADDRESS_HOME_ZIP);
    171   supported_types->insert(ADDRESS_HOME_COUNTRY);
    172 }
    173 
    174 }  // namespace autofill
    175