contact_info.cc revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
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#include "components/autofill/core/browser/field_types.h"
17
18namespace autofill {
19
20static const AutofillFieldType kAutofillNameInfoTypes[] = {
21  NAME_FIRST,
22  NAME_MIDDLE,
23  NAME_LAST
24};
25
26static const size_t kAutofillNameInfoLength =
27    arraysize(kAutofillNameInfoTypes);
28
29NameInfo::NameInfo() {}
30
31NameInfo::NameInfo(const NameInfo& info) : FormGroup() {
32  *this = info;
33}
34
35NameInfo::~NameInfo() {}
36
37NameInfo& NameInfo::operator=(const NameInfo& info) {
38  if (this == &info)
39    return *this;
40
41  first_ = info.first_;
42  middle_ = info.middle_;
43  last_ = info.last_;
44  return *this;
45}
46
47void NameInfo::GetSupportedTypes(FieldTypeSet* supported_types) const {
48  supported_types->insert(NAME_FIRST);
49  supported_types->insert(NAME_MIDDLE);
50  supported_types->insert(NAME_LAST);
51  supported_types->insert(NAME_MIDDLE_INITIAL);
52  supported_types->insert(NAME_FULL);
53}
54
55base::string16 NameInfo::GetRawInfo(AutofillFieldType type) const {
56  if (type == NAME_FIRST)
57    return first();
58
59  if (type == NAME_MIDDLE)
60    return middle();
61
62  if (type == NAME_LAST)
63    return last();
64
65  if (type == NAME_MIDDLE_INITIAL)
66    return MiddleInitial();
67
68  if (type == NAME_FULL)
69    return FullName();
70
71  return base::string16();
72}
73
74void NameInfo::SetRawInfo(AutofillFieldType type, const base::string16& value) {
75  DCHECK_EQ(AutofillType::NAME, AutofillType(type).group());
76  if (type == NAME_FIRST)
77    first_ = value;
78  else if (type == NAME_MIDDLE || type == NAME_MIDDLE_INITIAL)
79    middle_ = value;
80  else if (type == NAME_LAST)
81    last_ = value;
82  else if (type == NAME_FULL)
83    SetFullName(value);
84  else
85    NOTREACHED();
86}
87
88base::string16 NameInfo::FullName() const {
89  std::vector<base::string16> full_name;
90  if (!first_.empty())
91    full_name.push_back(first_);
92
93  if (!middle_.empty())
94    full_name.push_back(middle_);
95
96  if (!last_.empty())
97    full_name.push_back(last_);
98
99  return JoinString(full_name, ' ');
100}
101
102base::string16 NameInfo::MiddleInitial() const {
103  if (middle_.empty())
104    return base::string16();
105
106  base::string16 middle_name(middle());
107  base::string16 initial;
108  initial.push_back(middle_name[0]);
109  return initial;
110}
111
112void NameInfo::SetFullName(const base::string16& full) {
113  // Clear the names.
114  first_ = base::string16();
115  middle_ = base::string16();
116  last_ = base::string16();
117
118  std::vector<base::string16> full_name_tokens;
119  Tokenize(full, ASCIIToUTF16(" "), &full_name_tokens);
120
121  // There are four possibilities: empty; first name; first and last names;
122  // first, middle (possibly multiple strings) and then the last name.
123  if (full_name_tokens.size() > 0) {
124    first_ = full_name_tokens[0];
125    if (full_name_tokens.size() > 1) {
126      last_ = full_name_tokens.back();
127      if (full_name_tokens.size() > 2) {
128        full_name_tokens.erase(full_name_tokens.begin());
129        full_name_tokens.pop_back();
130        middle_ = JoinString(full_name_tokens, ' ');
131      }
132    }
133  }
134}
135
136EmailInfo::EmailInfo() {}
137
138EmailInfo::EmailInfo(const EmailInfo& info) : FormGroup() {
139  *this = info;
140}
141
142EmailInfo::~EmailInfo() {}
143
144EmailInfo& EmailInfo::operator=(const EmailInfo& info) {
145  if (this == &info)
146    return *this;
147
148  email_ = info.email_;
149  return *this;
150}
151
152void EmailInfo::GetSupportedTypes(FieldTypeSet* supported_types) const {
153  supported_types->insert(EMAIL_ADDRESS);
154}
155
156base::string16 EmailInfo::GetRawInfo(AutofillFieldType type) const {
157  if (type == EMAIL_ADDRESS)
158    return email_;
159
160  return base::string16();
161}
162
163void EmailInfo::SetRawInfo(AutofillFieldType type,
164                           const base::string16& value) {
165  DCHECK_EQ(EMAIL_ADDRESS, type);
166  email_ = value;
167}
168
169CompanyInfo::CompanyInfo() {}
170
171CompanyInfo::CompanyInfo(const CompanyInfo& info) : FormGroup() {
172  *this = info;
173}
174
175CompanyInfo::~CompanyInfo() {}
176
177CompanyInfo& CompanyInfo::operator=(const CompanyInfo& info) {
178  if (this == &info)
179    return *this;
180
181  company_name_ = info.company_name_;
182  return *this;
183}
184
185void CompanyInfo::GetSupportedTypes(FieldTypeSet* supported_types) const {
186  supported_types->insert(COMPANY_NAME);
187}
188
189base::string16 CompanyInfo::GetRawInfo(AutofillFieldType type) const {
190  if (type == COMPANY_NAME)
191    return company_name_;
192
193  return base::string16();
194}
195
196void CompanyInfo::SetRawInfo(AutofillFieldType type,
197                             const base::string16& value) {
198  DCHECK_EQ(COMPANY_NAME, type);
199  company_name_ = value;
200}
201
202}  // namespace autofill
203