contact_info.cc revision 3240926e260ce088908e02ac07a6cf7b0c0cbf44
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 type = AutofillType::GetEquivalentFieldType(type); 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(ServerFieldType type, const base::string16& value) { 75 type = AutofillType::GetEquivalentFieldType(type); 76 DCHECK_EQ(NAME, AutofillType(type).group()); 77 if (type == NAME_FIRST) 78 first_ = value; 79 else if (type == NAME_MIDDLE || type == NAME_MIDDLE_INITIAL) 80 middle_ = value; 81 else if (type == NAME_LAST) 82 last_ = value; 83 else if (type == NAME_FULL) 84 SetFullName(value); 85 else 86 NOTREACHED(); 87} 88 89base::string16 NameInfo::FullName() const { 90 std::vector<base::string16> full_name; 91 if (!first_.empty()) 92 full_name.push_back(first_); 93 94 if (!middle_.empty()) 95 full_name.push_back(middle_); 96 97 if (!last_.empty()) 98 full_name.push_back(last_); 99 100 return JoinString(full_name, ' '); 101} 102 103base::string16 NameInfo::MiddleInitial() const { 104 if (middle_.empty()) 105 return base::string16(); 106 107 base::string16 middle_name(middle()); 108 base::string16 initial; 109 initial.push_back(middle_name[0]); 110 return initial; 111} 112 113void NameInfo::SetFullName(const base::string16& full) { 114 // Clear the names. 115 first_ = base::string16(); 116 middle_ = base::string16(); 117 last_ = base::string16(); 118 119 std::vector<base::string16> full_name_tokens; 120 Tokenize(full, ASCIIToUTF16(" "), &full_name_tokens); 121 122 // There are four possibilities: empty; first name; first and last names; 123 // first, middle (possibly multiple strings) and then the last name. 124 if (full_name_tokens.size() > 0) { 125 first_ = full_name_tokens[0]; 126 if (full_name_tokens.size() > 1) { 127 last_ = full_name_tokens.back(); 128 if (full_name_tokens.size() > 2) { 129 full_name_tokens.erase(full_name_tokens.begin()); 130 full_name_tokens.pop_back(); 131 middle_ = JoinString(full_name_tokens, ' '); 132 } 133 } 134 } 135} 136 137EmailInfo::EmailInfo() {} 138 139EmailInfo::EmailInfo(const EmailInfo& info) : FormGroup() { 140 *this = info; 141} 142 143EmailInfo::~EmailInfo() {} 144 145EmailInfo& EmailInfo::operator=(const EmailInfo& info) { 146 if (this == &info) 147 return *this; 148 149 email_ = info.email_; 150 return *this; 151} 152 153void EmailInfo::GetSupportedTypes(ServerFieldTypeSet* supported_types) const { 154 supported_types->insert(EMAIL_ADDRESS); 155} 156 157base::string16 EmailInfo::GetRawInfo(ServerFieldType type) const { 158 if (type == EMAIL_ADDRESS) 159 return email_; 160 161 return base::string16(); 162} 163 164void EmailInfo::SetRawInfo(ServerFieldType type, 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(ServerFieldTypeSet* supported_types) const { 186 supported_types->insert(COMPANY_NAME); 187} 188 189base::string16 CompanyInfo::GetRawInfo(ServerFieldType type) const { 190 if (type == COMPANY_NAME) 191 return company_name_; 192 193 return base::string16(); 194} 195 196void CompanyInfo::SetRawInfo(ServerFieldType type, 197 const base::string16& value) { 198 DCHECK_EQ(COMPANY_NAME, type); 199 company_name_ = value; 200} 201 202} // namespace autofill 203