blob: 603c4fa3b54353a82003c240ae3e70f3844cafa4 [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/contact_info.h"
6
7#include <stddef.h>
8#include <ostream>
9#include <string>
10
11#include "base/basictypes.h"
12#include "base/logging.h"
13#include "base/strings/string_util.h"
14#include "base/strings/utf_string_conversions.h"
15#include "components/autofill/core/browser/autofill_type.h"
Ben Murdocheb525c52013-07-10 11:40:50 +010016
17namespace autofill {
18
Ben Murdoch32409262013-08-07 11:04:47 +010019static const ServerFieldType kAutofillNameInfoTypes[] = {
Ben Murdocheb525c52013-07-10 11:40:50 +010020 NAME_FIRST,
21 NAME_MIDDLE,
22 NAME_LAST
23};
24
25static const size_t kAutofillNameInfoLength =
26 arraysize(kAutofillNameInfoTypes);
27
28NameInfo::NameInfo() {}
29
30NameInfo::NameInfo(const NameInfo& info) : FormGroup() {
31 *this = info;
32}
33
34NameInfo::~NameInfo() {}
35
36NameInfo& NameInfo::operator=(const NameInfo& info) {
37 if (this == &info)
38 return *this;
39
40 first_ = info.first_;
41 middle_ = info.middle_;
42 last_ = info.last_;
43 return *this;
44}
45
Ben Murdoch32409262013-08-07 11:04:47 +010046void NameInfo::GetSupportedTypes(ServerFieldTypeSet* supported_types) const {
Ben Murdocheb525c52013-07-10 11:40:50 +010047 supported_types->insert(NAME_FIRST);
48 supported_types->insert(NAME_MIDDLE);
49 supported_types->insert(NAME_LAST);
50 supported_types->insert(NAME_MIDDLE_INITIAL);
51 supported_types->insert(NAME_FULL);
52}
53
Ben Murdoch32409262013-08-07 11:04:47 +010054base::string16 NameInfo::GetRawInfo(ServerFieldType type) const {
Ben Murdochbb1529c2013-08-08 10:24:53 +010055 // TODO(isherman): Is GetStorableType even necessary?
56 switch (AutofillType(type).GetStorableType()) {
57 case NAME_FIRST:
58 return first();
Ben Murdocheb525c52013-07-10 11:40:50 +010059
Ben Murdochbb1529c2013-08-08 10:24:53 +010060 case NAME_MIDDLE:
61 return middle();
Ben Murdocheb525c52013-07-10 11:40:50 +010062
Ben Murdochbb1529c2013-08-08 10:24:53 +010063 case NAME_LAST:
64 return last();
Ben Murdocheb525c52013-07-10 11:40:50 +010065
Ben Murdochbb1529c2013-08-08 10:24:53 +010066 case NAME_MIDDLE_INITIAL:
67 return MiddleInitial();
Ben Murdocheb525c52013-07-10 11:40:50 +010068
Ben Murdochbb1529c2013-08-08 10:24:53 +010069 case NAME_FULL:
70 return FullName();
Ben Murdocheb525c52013-07-10 11:40:50 +010071
Ben Murdochbb1529c2013-08-08 10:24:53 +010072 default:
73 return base::string16();
74 }
Ben Murdocheb525c52013-07-10 11:40:50 +010075}
76
Ben Murdoch32409262013-08-07 11:04:47 +010077void NameInfo::SetRawInfo(ServerFieldType type, const base::string16& value) {
Ben Murdochbb1529c2013-08-08 10:24:53 +010078 // TODO(isherman): Is GetStorableType even necessary?
79 ServerFieldType storable_type = AutofillType(type).GetStorableType();
80 DCHECK_EQ(NAME, AutofillType(storable_type).group());
81 switch (storable_type) {
82 case NAME_FIRST:
83 first_ = value;
84 break;
85
86 case NAME_MIDDLE:
87 case NAME_MIDDLE_INITIAL:
88 middle_ = value;
89 break;
90
91 case NAME_LAST:
92 last_ = value;
93 break;
94
95 case NAME_FULL:
96 SetFullName(value);
97 break;
98
99 default:
100 NOTREACHED();
101 }
Ben Murdocheb525c52013-07-10 11:40:50 +0100102}
103
104base::string16 NameInfo::FullName() const {
105 std::vector<base::string16> full_name;
106 if (!first_.empty())
107 full_name.push_back(first_);
108
109 if (!middle_.empty())
110 full_name.push_back(middle_);
111
112 if (!last_.empty())
113 full_name.push_back(last_);
114
115 return JoinString(full_name, ' ');
116}
117
118base::string16 NameInfo::MiddleInitial() const {
119 if (middle_.empty())
120 return base::string16();
121
122 base::string16 middle_name(middle());
123 base::string16 initial;
124 initial.push_back(middle_name[0]);
125 return initial;
126}
127
128void NameInfo::SetFullName(const base::string16& full) {
129 // Clear the names.
130 first_ = base::string16();
131 middle_ = base::string16();
132 last_ = base::string16();
133
134 std::vector<base::string16> full_name_tokens;
135 Tokenize(full, ASCIIToUTF16(" "), &full_name_tokens);
136
137 // There are four possibilities: empty; first name; first and last names;
138 // first, middle (possibly multiple strings) and then the last name.
139 if (full_name_tokens.size() > 0) {
140 first_ = full_name_tokens[0];
141 if (full_name_tokens.size() > 1) {
142 last_ = full_name_tokens.back();
143 if (full_name_tokens.size() > 2) {
144 full_name_tokens.erase(full_name_tokens.begin());
145 full_name_tokens.pop_back();
146 middle_ = JoinString(full_name_tokens, ' ');
147 }
148 }
149 }
150}
151
152EmailInfo::EmailInfo() {}
153
154EmailInfo::EmailInfo(const EmailInfo& info) : FormGroup() {
155 *this = info;
156}
157
158EmailInfo::~EmailInfo() {}
159
160EmailInfo& EmailInfo::operator=(const EmailInfo& info) {
161 if (this == &info)
162 return *this;
163
164 email_ = info.email_;
165 return *this;
166}
167
Ben Murdoch32409262013-08-07 11:04:47 +0100168void EmailInfo::GetSupportedTypes(ServerFieldTypeSet* supported_types) const {
Ben Murdocheb525c52013-07-10 11:40:50 +0100169 supported_types->insert(EMAIL_ADDRESS);
170}
171
Ben Murdoch32409262013-08-07 11:04:47 +0100172base::string16 EmailInfo::GetRawInfo(ServerFieldType type) const {
Ben Murdocheb525c52013-07-10 11:40:50 +0100173 if (type == EMAIL_ADDRESS)
174 return email_;
175
176 return base::string16();
177}
178
Ben Murdoch32409262013-08-07 11:04:47 +0100179void EmailInfo::SetRawInfo(ServerFieldType type, const base::string16& value) {
Ben Murdocheb525c52013-07-10 11:40:50 +0100180 DCHECK_EQ(EMAIL_ADDRESS, type);
181 email_ = value;
182}
183
184CompanyInfo::CompanyInfo() {}
185
186CompanyInfo::CompanyInfo(const CompanyInfo& info) : FormGroup() {
187 *this = info;
188}
189
190CompanyInfo::~CompanyInfo() {}
191
192CompanyInfo& CompanyInfo::operator=(const CompanyInfo& info) {
193 if (this == &info)
194 return *this;
195
196 company_name_ = info.company_name_;
197 return *this;
198}
199
Ben Murdoch32409262013-08-07 11:04:47 +0100200void CompanyInfo::GetSupportedTypes(ServerFieldTypeSet* supported_types) const {
Ben Murdocheb525c52013-07-10 11:40:50 +0100201 supported_types->insert(COMPANY_NAME);
202}
203
Ben Murdoch32409262013-08-07 11:04:47 +0100204base::string16 CompanyInfo::GetRawInfo(ServerFieldType type) const {
Ben Murdocheb525c52013-07-10 11:40:50 +0100205 if (type == COMPANY_NAME)
206 return company_name_;
207
208 return base::string16();
209}
210
Ben Murdoch32409262013-08-07 11:04:47 +0100211void CompanyInfo::SetRawInfo(ServerFieldType type,
Ben Murdocheb525c52013-07-10 11:40:50 +0100212 const base::string16& value) {
213 DCHECK_EQ(COMPANY_NAME, type);
214 company_name_ = value;
215}
216
217} // namespace autofill