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 17 namespace autofill { 18 19 static const ServerFieldType kAutofillNameInfoTypes[] = { 20 NAME_FIRST, 21 NAME_MIDDLE, 22 NAME_LAST 23 }; 24 25 static const size_t kAutofillNameInfoLength = 26 arraysize(kAutofillNameInfoTypes); 27 28 NameInfo::NameInfo() {} 29 30 NameInfo::NameInfo(const NameInfo& info) : FormGroup() { 31 *this = info; 32 } 33 34 NameInfo::~NameInfo() {} 35 36 NameInfo& 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 46 void 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 54 base::string16 NameInfo::GetRawInfo(ServerFieldType type) const { 55 // TODO(isherman): Is GetStorableType even necessary? 56 switch (AutofillType(type).GetStorableType()) { 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 77 void NameInfo::SetRawInfo(ServerFieldType type, const base::string16& value) { 78 // 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 } 102 } 103 104 base::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 118 base::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 128 void 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 152 EmailInfo::EmailInfo() {} 153 154 EmailInfo::EmailInfo(const EmailInfo& info) : FormGroup() { 155 *this = info; 156 } 157 158 EmailInfo::~EmailInfo() {} 159 160 EmailInfo& EmailInfo::operator=(const EmailInfo& info) { 161 if (this == &info) 162 return *this; 163 164 email_ = info.email_; 165 return *this; 166 } 167 168 void EmailInfo::GetSupportedTypes(ServerFieldTypeSet* supported_types) const { 169 supported_types->insert(EMAIL_ADDRESS); 170 } 171 172 base::string16 EmailInfo::GetRawInfo(ServerFieldType type) const { 173 if (type == EMAIL_ADDRESS) 174 return email_; 175 176 return base::string16(); 177 } 178 179 void EmailInfo::SetRawInfo(ServerFieldType type, const base::string16& value) { 180 DCHECK_EQ(EMAIL_ADDRESS, type); 181 email_ = value; 182 } 183 184 CompanyInfo::CompanyInfo() {} 185 186 CompanyInfo::CompanyInfo(const CompanyInfo& info) : FormGroup() { 187 *this = info; 188 } 189 190 CompanyInfo::~CompanyInfo() {} 191 192 CompanyInfo& CompanyInfo::operator=(const CompanyInfo& info) { 193 if (this == &info) 194 return *this; 195 196 company_name_ = info.company_name_; 197 return *this; 198 } 199 200 void CompanyInfo::GetSupportedTypes(ServerFieldTypeSet* supported_types) const { 201 supported_types->insert(COMPANY_NAME); 202 } 203 204 base::string16 CompanyInfo::GetRawInfo(ServerFieldType type) const { 205 if (type == COMPANY_NAME) 206 return company_name_; 207 208 return base::string16(); 209 } 210 211 void CompanyInfo::SetRawInfo(ServerFieldType type, 212 const base::string16& value) { 213 DCHECK_EQ(COMPANY_NAME, type); 214 company_name_ = value; 215 } 216 217 } // namespace autofill 218