blob: ca522f6d35ea5fb2ed97945a32759e83feff2d75 [file] [log] [blame]
Ben Murdocheb525c52013-07-10 11:40:50 +01001// 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"
Ben Murdocheb525c52013-07-10 11:40:50 +010016
17namespace {
18
19const char16 kAddressSplitChars[] = {'-', ',', '#', '.', ' ', 0};
20
21} // namespace
22
23namespace autofill {
24
25Address::Address() {}
26
27Address::Address(const Address& address) : FormGroup() {
28 *this = address;
29}
30
31Address::~Address() {}
32
33Address& 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
Ben Murdoch32409262013-08-07 11:04:47 +010046base::string16 Address::GetRawInfo(ServerFieldType type) const {
Ben Murdochbb1529c2013-08-08 10:24:53 +010047 // TODO(isherman): Is GetStorableType even necessary?
48 switch (AutofillType(type).GetStorableType()) {
49 case ADDRESS_HOME_LINE1:
50 return line1_;
Ben Murdocheb525c52013-07-10 11:40:50 +010051
Ben Murdochbb1529c2013-08-08 10:24:53 +010052 case ADDRESS_HOME_LINE2:
53 return line2_;
Ben Murdocheb525c52013-07-10 11:40:50 +010054
Ben Murdochbb1529c2013-08-08 10:24:53 +010055 case ADDRESS_HOME_CITY:
56 return city_;
Ben Murdocheb525c52013-07-10 11:40:50 +010057
Ben Murdochbb1529c2013-08-08 10:24:53 +010058 case ADDRESS_HOME_STATE:
59 return state_;
Ben Murdocheb525c52013-07-10 11:40:50 +010060
Ben Murdochbb1529c2013-08-08 10:24:53 +010061 case ADDRESS_HOME_ZIP:
62 return zip_code_;
Ben Murdocheb525c52013-07-10 11:40:50 +010063
Ben Murdochbb1529c2013-08-08 10:24:53 +010064 case ADDRESS_HOME_COUNTRY:
65 return country_code_;
Ben Murdocheb525c52013-07-10 11:40:50 +010066
Ben Murdochbb1529c2013-08-08 10:24:53 +010067 default:
68 return base::string16();
69 }
Ben Murdocheb525c52013-07-10 11:40:50 +010070}
71
Ben Murdoch32409262013-08-07 11:04:47 +010072void Address::SetRawInfo(ServerFieldType type, const base::string16& value) {
Ben Murdochbb1529c2013-08-08 10:24:53 +010073 // 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() || value.length() == 2u);
93 country_code_ = value;
94 break;
95
96 case ADDRESS_HOME_ZIP:
97 zip_code_ = value;
98 break;
99
100 default:
101 NOTREACHED();
Ben Murdocheb525c52013-07-10 11:40:50 +0100102 }
103}
104
Ben Murdoch32409262013-08-07 11:04:47 +0100105base::string16 Address::GetInfo(const AutofillType& type,
Ben Murdocheb525c52013-07-10 11:40:50 +0100106 const std::string& app_locale) const {
Ben Murdochbb1529c2013-08-08 10:24:53 +0100107 ServerFieldType storable_type = type.GetStorableType();
108 if (storable_type == ADDRESS_HOME_COUNTRY && !country_code_.empty())
Ben Murdocheb525c52013-07-10 11:40:50 +0100109 return AutofillCountry(UTF16ToASCII(country_code_), app_locale).name();
110
Ben Murdochbb1529c2013-08-08 10:24:53 +0100111 return GetRawInfo(storable_type);
Ben Murdocheb525c52013-07-10 11:40:50 +0100112}
113
Ben Murdoch32409262013-08-07 11:04:47 +0100114bool Address::SetInfo(const AutofillType& type,
Ben Murdocheb525c52013-07-10 11:40:50 +0100115 const base::string16& value,
116 const std::string& app_locale) {
Ben Murdochbb1529c2013-08-08 10:24:53 +0100117 ServerFieldType storable_type = type.GetStorableType();
118 if (storable_type == ADDRESS_HOME_COUNTRY && !value.empty()) {
Ben Murdocheb525c52013-07-10 11:40:50 +0100119 country_code_ =
120 ASCIIToUTF16(AutofillCountry::GetCountryCode(value, app_locale));
121 return !country_code_.empty();
122 }
123
Ben Murdochbb1529c2013-08-08 10:24:53 +0100124 SetRawInfo(storable_type, value);
Ben Murdocheb525c52013-07-10 11:40:50 +0100125 return true;
126}
127
128void Address::GetMatchingTypes(const base::string16& text,
129 const std::string& app_locale,
Ben Murdoch32409262013-08-07 11:04:47 +0100130 ServerFieldTypeSet* matching_types) const {
Ben Murdocheb525c52013-07-10 11:40:50 +0100131 FormGroup::GetMatchingTypes(text, app_locale, matching_types);
132
133 // Check to see if the |text| canonicalized as a country name is a match.
134 std::string country_code = AutofillCountry::GetCountryCode(text, app_locale);
135 if (!country_code.empty() && country_code_ == ASCIIToUTF16(country_code))
136 matching_types->insert(ADDRESS_HOME_COUNTRY);
137}
138
Ben Murdoch32409262013-08-07 11:04:47 +0100139void Address::GetSupportedTypes(ServerFieldTypeSet* supported_types) const {
140 supported_types->insert(ADDRESS_HOME_LINE1);
141 supported_types->insert(ADDRESS_HOME_LINE2);
142 supported_types->insert(ADDRESS_HOME_CITY);
143 supported_types->insert(ADDRESS_HOME_STATE);
144 supported_types->insert(ADDRESS_HOME_ZIP);
145 supported_types->insert(ADDRESS_HOME_COUNTRY);
146}
147
Ben Murdocheb525c52013-07-10 11:40:50 +0100148} // namespace autofill