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.
5 #include "components/autofill/core/browser/address.h"
10 #include "base/basictypes.h"
11 #include "base/logging.h"
12 #include "base/strings/string_split.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "components/autofill/core/browser/autofill_country.h"
16 #include "components/autofill/core/browser/autofill_field.h"
17 #include "components/autofill/core/browser/autofill_type.h"
23 Address::Address(const Address
& address
) : FormGroup() {
27 Address::~Address() {}
29 Address
& Address::operator=(const Address
& address
) {
33 street_address_
= address
.street_address_
;
34 dependent_locality_
= address
.dependent_locality_
;
35 city_
= address
.city_
;
36 state_
= address
.state_
;
37 country_code_
= address
.country_code_
;
38 zip_code_
= address
.zip_code_
;
39 sorting_code_
= address
.sorting_code_
;
43 base::string16
Address::GetRawInfo(ServerFieldType type
) const {
44 DCHECK_EQ(ADDRESS_HOME
, AutofillType(type
).group());
46 case ADDRESS_HOME_LINE1
:
47 return street_address_
.size() > 0 ? street_address_
[0] : base::string16();
49 case ADDRESS_HOME_LINE2
:
50 return street_address_
.size() > 1 ? street_address_
[1] : base::string16();
52 case ADDRESS_HOME_LINE3
:
53 return street_address_
.size() > 2 ? street_address_
[2] : base::string16();
55 case ADDRESS_HOME_DEPENDENT_LOCALITY
:
56 return dependent_locality_
;
58 case ADDRESS_HOME_CITY
:
61 case ADDRESS_HOME_STATE
:
64 case ADDRESS_HOME_ZIP
:
67 case ADDRESS_HOME_SORTING_CODE
:
70 case ADDRESS_HOME_COUNTRY
:
71 return base::ASCIIToUTF16(country_code_
);
73 case ADDRESS_HOME_STREET_ADDRESS
:
74 return base::JoinString(street_address_
, base::ASCIIToUTF16("\n"));
78 return base::string16();
82 void Address::SetRawInfo(ServerFieldType type
, const base::string16
& value
) {
83 DCHECK_EQ(ADDRESS_HOME
, AutofillType(type
).group());
85 case ADDRESS_HOME_LINE1
:
86 if (street_address_
.empty())
87 street_address_
.resize(1);
88 street_address_
[0] = value
;
92 case ADDRESS_HOME_LINE2
:
93 if (street_address_
.size() < 2)
94 street_address_
.resize(2);
95 street_address_
[1] = value
;
99 case ADDRESS_HOME_LINE3
:
100 if (street_address_
.size() < 3)
101 street_address_
.resize(3);
102 street_address_
[2] = value
;
106 case ADDRESS_HOME_DEPENDENT_LOCALITY
:
107 dependent_locality_
= value
;
110 case ADDRESS_HOME_CITY
:
114 case ADDRESS_HOME_STATE
:
118 case ADDRESS_HOME_COUNTRY
:
119 DCHECK(value
.empty() ||
120 (value
.length() == 2u && base::IsStringASCII(value
)));
121 country_code_
= base::UTF16ToASCII(value
);
124 case ADDRESS_HOME_ZIP
:
128 case ADDRESS_HOME_SORTING_CODE
:
129 sorting_code_
= value
;
132 case ADDRESS_HOME_STREET_ADDRESS
:
133 street_address_
= base::SplitString(
134 value
, base::ASCIIToUTF16("\n"),
135 base::TRIM_WHITESPACE
, base::SPLIT_WANT_ALL
);
143 base::string16
Address::GetInfo(const AutofillType
& type
,
144 const std::string
& app_locale
) const {
145 if (type
.html_type() == HTML_TYPE_COUNTRY_CODE
)
146 return base::ASCIIToUTF16(country_code_
);
148 ServerFieldType storable_type
= type
.GetStorableType();
149 if (storable_type
== ADDRESS_HOME_COUNTRY
&& !country_code_
.empty())
150 return AutofillCountry(country_code_
, app_locale
).name();
152 return GetRawInfo(storable_type
);
155 bool Address::SetInfo(const AutofillType
& type
,
156 const base::string16
& value
,
157 const std::string
& app_locale
) {
158 if (type
.html_type() == HTML_TYPE_COUNTRY_CODE
) {
159 if (!value
.empty() && (value
.size() != 2u || !base::IsStringASCII(value
))) {
160 country_code_
= std::string();
164 country_code_
= base::ToUpperASCII(base::UTF16ToASCII(value
));
166 } else if (type
.html_type() == HTML_TYPE_FULL_ADDRESS
) {
167 // Parsing a full address is too hard.
171 ServerFieldType storable_type
= type
.GetStorableType();
172 if (storable_type
== ADDRESS_HOME_COUNTRY
&& !value
.empty()) {
173 country_code_
= AutofillCountry::GetCountryCode(value
, app_locale
);
174 return !country_code_
.empty();
177 SetRawInfo(storable_type
, value
);
179 // Give up when importing addresses with any entirely blank lines.
180 // There's a good chance that this formatting is not intentional, but it's
181 // also not obviously safe to just strip the newlines.
182 if (storable_type
== ADDRESS_HOME_STREET_ADDRESS
&&
183 std::find(street_address_
.begin(), street_address_
.end(),
184 base::string16()) != street_address_
.end()) {
185 street_address_
.clear();
192 void Address::GetMatchingTypes(const base::string16
& text
,
193 const std::string
& app_locale
,
194 ServerFieldTypeSet
* matching_types
) const {
195 FormGroup::GetMatchingTypes(text
, app_locale
, matching_types
);
197 // Check to see if the |text| canonicalized as a country name is a match.
198 std::string country_code
= AutofillCountry::GetCountryCode(text
, app_locale
);
199 if (!country_code
.empty() && country_code_
== country_code
)
200 matching_types
->insert(ADDRESS_HOME_COUNTRY
);
203 void Address::GetSupportedTypes(ServerFieldTypeSet
* supported_types
) const {
204 supported_types
->insert(ADDRESS_HOME_LINE1
);
205 supported_types
->insert(ADDRESS_HOME_LINE2
);
206 supported_types
->insert(ADDRESS_HOME_LINE3
);
207 supported_types
->insert(ADDRESS_HOME_STREET_ADDRESS
);
208 supported_types
->insert(ADDRESS_HOME_DEPENDENT_LOCALITY
);
209 supported_types
->insert(ADDRESS_HOME_CITY
);
210 supported_types
->insert(ADDRESS_HOME_STATE
);
211 supported_types
->insert(ADDRESS_HOME_ZIP
);
212 supported_types
->insert(ADDRESS_HOME_SORTING_CODE
);
213 supported_types
->insert(ADDRESS_HOME_COUNTRY
);
216 void Address::TrimStreetAddress() {
217 while (!street_address_
.empty() && street_address_
.back().empty()) {
218 street_address_
.pop_back();
222 } // namespace autofill