contact_info.cc revision 424c4d7b64af9d0d8fd9624f381f469654d5e3d2
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/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"
16
17namespace autofill {
18
19static const ServerFieldType kAutofillNameInfoTypes[] = {
20  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
46void NameInfo::GetSupportedTypes(ServerFieldTypeSet* supported_types) const {
47  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
54base::string16 NameInfo::GetRawInfo(ServerFieldType type) const {
55  DCHECK_EQ(NAME, AutofillType(type).group());
56  switch (type) {
57    case NAME_FIRST:
58      return first();
59
60    case NAME_MIDDLE:
61      return middle();
62
63    case NAME_LAST:
64      return last();
65
66    case NAME_MIDDLE_INITIAL:
67      return MiddleInitial();
68
69    case NAME_FULL:
70      return FullName();
71
72    default:
73      return base::string16();
74  }
75}
76
77void NameInfo::SetRawInfo(ServerFieldType type, const base::string16& value) {
78  DCHECK_EQ(NAME, AutofillType(type).group());
79  switch (type) {
80    case NAME_FIRST:
81      first_ = value;
82      break;
83
84    case NAME_MIDDLE:
85    case NAME_MIDDLE_INITIAL:
86      middle_ = value;
87      break;
88
89    case NAME_LAST:
90      last_ = value;
91      break;
92
93    case NAME_FULL:
94      SetFullName(value);
95      break;
96
97    default:
98      NOTREACHED();
99  }
100}
101
102base::string16 NameInfo::FullName() const {
103  std::vector<base::string16> full_name;
104  if (!first_.empty())
105    full_name.push_back(first_);
106
107  if (!middle_.empty())
108    full_name.push_back(middle_);
109
110  if (!last_.empty())
111    full_name.push_back(last_);
112
113  return JoinString(full_name, ' ');
114}
115
116base::string16 NameInfo::MiddleInitial() const {
117  if (middle_.empty())
118    return base::string16();
119
120  base::string16 middle_name(middle());
121  base::string16 initial;
122  initial.push_back(middle_name[0]);
123  return initial;
124}
125
126void NameInfo::SetFullName(const base::string16& full) {
127  // Clear the names.
128  first_ = base::string16();
129  middle_ = base::string16();
130  last_ = base::string16();
131
132  std::vector<base::string16> full_name_tokens;
133  Tokenize(full, ASCIIToUTF16(" "), &full_name_tokens);
134
135  // There are four possibilities: empty; first name; first and last names;
136  // first, middle (possibly multiple strings) and then the last name.
137  if (full_name_tokens.size() > 0) {
138    first_ = full_name_tokens[0];
139    if (full_name_tokens.size() > 1) {
140      last_ = full_name_tokens.back();
141      if (full_name_tokens.size() > 2) {
142        full_name_tokens.erase(full_name_tokens.begin());
143        full_name_tokens.pop_back();
144        middle_ = JoinString(full_name_tokens, ' ');
145      }
146    }
147  }
148}
149
150EmailInfo::EmailInfo() {}
151
152EmailInfo::EmailInfo(const EmailInfo& info) : FormGroup() {
153  *this = info;
154}
155
156EmailInfo::~EmailInfo() {}
157
158EmailInfo& EmailInfo::operator=(const EmailInfo& info) {
159  if (this == &info)
160    return *this;
161
162  email_ = info.email_;
163  return *this;
164}
165
166void EmailInfo::GetSupportedTypes(ServerFieldTypeSet* supported_types) const {
167  supported_types->insert(EMAIL_ADDRESS);
168}
169
170base::string16 EmailInfo::GetRawInfo(ServerFieldType type) const {
171  if (type == EMAIL_ADDRESS)
172    return email_;
173
174  return base::string16();
175}
176
177void EmailInfo::SetRawInfo(ServerFieldType type, const base::string16& value) {
178  DCHECK_EQ(EMAIL_ADDRESS, type);
179  email_ = value;
180}
181
182CompanyInfo::CompanyInfo() {}
183
184CompanyInfo::CompanyInfo(const CompanyInfo& info) : FormGroup() {
185  *this = info;
186}
187
188CompanyInfo::~CompanyInfo() {}
189
190CompanyInfo& CompanyInfo::operator=(const CompanyInfo& info) {
191  if (this == &info)
192    return *this;
193
194  company_name_ = info.company_name_;
195  return *this;
196}
197
198void CompanyInfo::GetSupportedTypes(ServerFieldTypeSet* supported_types) const {
199  supported_types->insert(COMPANY_NAME);
200}
201
202base::string16 CompanyInfo::GetRawInfo(ServerFieldType type) const {
203  if (type == COMPANY_NAME)
204    return company_name_;
205
206  return base::string16();
207}
208
209void CompanyInfo::SetRawInfo(ServerFieldType type,
210                             const base::string16& value) {
211  DCHECK_EQ(COMPANY_NAME, type);
212  company_name_ = value;
213}
214
215}  // namespace autofill
216