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