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