| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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/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/browser/autofill_type.h" | |
| 16 #include "components/autofill/browser/field_types.h" | |
| 17 | |
| 18 namespace autofill { | |
| 19 | |
| 20 static const AutofillFieldType kAutofillNameInfoTypes[] = { | |
| 21 NAME_FIRST, | |
| 22 NAME_MIDDLE, | |
| 23 NAME_LAST | |
| 24 }; | |
| 25 | |
| 26 static const size_t kAutofillNameInfoLength = | |
| 27 arraysize(kAutofillNameInfoTypes); | |
| 28 | |
| 29 NameInfo::NameInfo() {} | |
| 30 | |
| 31 NameInfo::NameInfo(const NameInfo& info) : FormGroup() { | |
| 32 *this = info; | |
| 33 } | |
| 34 | |
| 35 NameInfo::~NameInfo() {} | |
| 36 | |
| 37 NameInfo& 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 | |
| 47 void 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 | |
| 55 base::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 | |
| 74 void 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 | |
| 88 base::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 | |
| 102 base::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 | |
| 112 void 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 | |
| 136 EmailInfo::EmailInfo() {} | |
| 137 | |
| 138 EmailInfo::EmailInfo(const EmailInfo& info) : FormGroup() { | |
| 139 *this = info; | |
| 140 } | |
| 141 | |
| 142 EmailInfo::~EmailInfo() {} | |
| 143 | |
| 144 EmailInfo& EmailInfo::operator=(const EmailInfo& info) { | |
| 145 if (this == &info) | |
| 146 return *this; | |
| 147 | |
| 148 email_ = info.email_; | |
| 149 return *this; | |
| 150 } | |
| 151 | |
| 152 void EmailInfo::GetSupportedTypes(FieldTypeSet* supported_types) const { | |
| 153 supported_types->insert(EMAIL_ADDRESS); | |
| 154 } | |
| 155 | |
| 156 base::string16 EmailInfo::GetRawInfo(AutofillFieldType type) const { | |
| 157 if (type == EMAIL_ADDRESS) | |
| 158 return email_; | |
| 159 | |
| 160 return base::string16(); | |
| 161 } | |
| 162 | |
| 163 void EmailInfo::SetRawInfo(AutofillFieldType type, | |
| 164 const base::string16& value) { | |
| 165 DCHECK_EQ(EMAIL_ADDRESS, type); | |
| 166 email_ = value; | |
| 167 } | |
| 168 | |
| 169 CompanyInfo::CompanyInfo() {} | |
| 170 | |
| 171 CompanyInfo::CompanyInfo(const CompanyInfo& info) : FormGroup() { | |
| 172 *this = info; | |
| 173 } | |
| 174 | |
| 175 CompanyInfo::~CompanyInfo() {} | |
| 176 | |
| 177 CompanyInfo& CompanyInfo::operator=(const CompanyInfo& info) { | |
| 178 if (this == &info) | |
| 179 return *this; | |
| 180 | |
| 181 company_name_ = info.company_name_; | |
| 182 return *this; | |
| 183 } | |
| 184 | |
| 185 void CompanyInfo::GetSupportedTypes(FieldTypeSet* supported_types) const { | |
| 186 supported_types->insert(COMPANY_NAME); | |
| 187 } | |
| 188 | |
| 189 base::string16 CompanyInfo::GetRawInfo(AutofillFieldType type) const { | |
| 190 if (type == COMPANY_NAME) | |
| 191 return company_name_; | |
| 192 | |
| 193 return base::string16(); | |
| 194 } | |
| 195 | |
| 196 void CompanyInfo::SetRawInfo(AutofillFieldType type, | |
| 197 const base::string16& value) { | |
| 198 DCHECK_EQ(COMPANY_NAME, type); | |
| 199 company_name_ = value; | |
| 200 } | |
| 201 | |
| 202 } // namespace autofill | |
| OLD | NEW |