OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/autofill/contact_info.h" | 5 #include "chrome/browser/autofill/contact_info.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "chrome/browser/autofill/autofill_type.h" | 10 #include "chrome/browser/autofill/autofill_type.h" |
11 #include "chrome/browser/autofill/field_types.h" | 11 #include "chrome/browser/autofill/field_types.h" |
12 | 12 |
13 static const string16 kNameSplitChars = ASCIIToUTF16("-'. "); | 13 static const string16 kNameSplitChars = ASCIIToUTF16("-'. "); |
14 | 14 |
15 static const AutofillFieldType kAutoFillContactInfoTypes[] = { | 15 static const AutofillFieldType kAutoFillNameInfoTypes[] = { |
16 NAME_FIRST, | 16 NAME_FIRST, |
17 NAME_MIDDLE, | 17 NAME_MIDDLE, |
18 NAME_LAST, | 18 NAME_LAST |
19 EMAIL_ADDRESS, | |
20 COMPANY_NAME, | |
21 }; | 19 }; |
22 | 20 |
23 static const size_t kAutoFillContactInfoLength = | 21 static const size_t kAutoFillNameInfoLength = |
24 arraysize(kAutoFillContactInfoTypes); | 22 arraysize(kAutoFillNameInfoTypes); |
25 | 23 |
26 ContactInfo::ContactInfo() {} | 24 NameInfo::NameInfo() {} |
27 | 25 |
28 ContactInfo::~ContactInfo() {} | 26 NameInfo::NameInfo(const NameInfo& info) : FormGroup() { |
29 | 27 *this = info; |
30 FormGroup* ContactInfo::Clone() const { | |
31 return new ContactInfo(*this); | |
32 } | 28 } |
33 | 29 |
34 void ContactInfo::GetPossibleFieldTypes(const string16& text, | 30 NameInfo::~NameInfo() {} |
| 31 |
| 32 NameInfo& NameInfo::operator=(const NameInfo& info) { |
| 33 if (this == &info) |
| 34 return *this; |
| 35 |
| 36 first_tokens_ = info.first_tokens_; |
| 37 middle_tokens_ = info.middle_tokens_; |
| 38 last_tokens_ = info.last_tokens_; |
| 39 first_ = info.first_; |
| 40 middle_ = info.middle_; |
| 41 last_ = info.last_; |
| 42 return *this; |
| 43 } |
| 44 |
| 45 void NameInfo::GetPossibleFieldTypes(const string16& text, |
35 FieldTypeSet* possible_types) const { | 46 FieldTypeSet* possible_types) const { |
36 DCHECK(possible_types); | 47 DCHECK(possible_types); |
37 | 48 |
38 if (IsFirstName(text)) | 49 if (IsFirstName(text)) |
39 possible_types->insert(NAME_FIRST); | 50 possible_types->insert(NAME_FIRST); |
40 | 51 |
41 if (IsMiddleName(text)) | 52 if (IsMiddleName(text)) |
42 possible_types->insert(NAME_MIDDLE); | 53 possible_types->insert(NAME_MIDDLE); |
43 | 54 |
44 if (IsLastName(text)) | 55 if (IsLastName(text)) |
45 possible_types->insert(NAME_LAST); | 56 possible_types->insert(NAME_LAST); |
46 | 57 |
47 if (IsMiddleInitial(text)) | 58 if (IsMiddleInitial(text)) |
48 possible_types->insert(NAME_MIDDLE_INITIAL); | 59 possible_types->insert(NAME_MIDDLE_INITIAL); |
49 | 60 |
50 if (IsSuffix(text)) | |
51 possible_types->insert(NAME_SUFFIX); | |
52 | |
53 if (IsFullName(text)) | 61 if (IsFullName(text)) |
54 possible_types->insert(NAME_FULL); | 62 possible_types->insert(NAME_FULL); |
55 | |
56 if (email_ == text) | |
57 possible_types->insert(EMAIL_ADDRESS); | |
58 | |
59 if (company_name_ == text) | |
60 possible_types->insert(COMPANY_NAME); | |
61 } | 63 } |
62 | 64 |
63 void ContactInfo::GetAvailableFieldTypes(FieldTypeSet* available_types) const { | 65 void NameInfo::GetAvailableFieldTypes(FieldTypeSet* available_types) const { |
64 DCHECK(available_types); | 66 DCHECK(available_types); |
65 | 67 |
66 if (!first().empty()) | 68 if (!first().empty()) |
67 available_types->insert(NAME_FIRST); | 69 available_types->insert(NAME_FIRST); |
68 | 70 |
69 if (!middle().empty()) | 71 if (!middle().empty()) |
70 available_types->insert(NAME_MIDDLE); | 72 available_types->insert(NAME_MIDDLE); |
71 | 73 |
72 if (!last().empty()) | 74 if (!last().empty()) |
73 available_types->insert(NAME_LAST); | 75 available_types->insert(NAME_LAST); |
74 | 76 |
75 if (!MiddleInitial().empty()) | 77 if (!MiddleInitial().empty()) |
76 available_types->insert(NAME_MIDDLE_INITIAL); | 78 available_types->insert(NAME_MIDDLE_INITIAL); |
77 | 79 |
78 if (!FullName().empty()) | 80 if (!FullName().empty()) |
79 available_types->insert(NAME_FULL); | 81 available_types->insert(NAME_FULL); |
80 | |
81 if (!suffix().empty()) | |
82 available_types->insert(NAME_SUFFIX); | |
83 | |
84 if (!email().empty()) | |
85 available_types->insert(EMAIL_ADDRESS); | |
86 | |
87 if (!company_name().empty()) | |
88 available_types->insert(COMPANY_NAME); | |
89 } | 82 } |
90 | 83 |
91 void ContactInfo::FindInfoMatches(const AutofillType& type, | 84 void NameInfo::FindInfoMatches(const AutofillType& type, |
92 const string16& info, | 85 const string16& info, |
93 std::vector<string16>* matched_text) const { | 86 std::vector<string16>* matched_text) const { |
94 DCHECK(matched_text); | 87 DCHECK(matched_text); |
95 | 88 |
96 string16 match; | 89 string16 match; |
97 if (type.field_type() == UNKNOWN_TYPE) { | 90 if (type.field_type() == UNKNOWN_TYPE) { |
98 for (size_t i = 0; i < kAutoFillContactInfoLength; i++) { | 91 for (size_t i = 0; i < kAutoFillNameInfoLength; i++) { |
99 if (FindInfoMatchesHelper(kAutoFillContactInfoTypes[i], info, &match)) | 92 if (FindInfoMatchesHelper(kAutoFillNameInfoTypes[i], info, &match)) |
100 matched_text->push_back(match); | 93 matched_text->push_back(match); |
101 } | 94 } |
102 } else if (FindInfoMatchesHelper(type.field_type(), info, &match)) { | 95 } else if (FindInfoMatchesHelper(type.field_type(), info, &match)) { |
103 matched_text->push_back(match); | 96 matched_text->push_back(match); |
104 } | 97 } |
105 } | 98 } |
106 | 99 |
107 string16 ContactInfo::GetFieldText(const AutofillType& type) const { | 100 string16 NameInfo::GetFieldText(const AutofillType& type) const { |
108 AutofillFieldType field_type = type.field_type(); | 101 AutofillFieldType field_type = type.field_type(); |
109 if (field_type == NAME_FIRST) | 102 if (field_type == NAME_FIRST) |
110 return first(); | 103 return first(); |
111 | 104 |
112 if (field_type == NAME_MIDDLE) | 105 if (field_type == NAME_MIDDLE) |
113 return middle(); | 106 return middle(); |
114 | 107 |
115 if (field_type == NAME_LAST) | 108 if (field_type == NAME_LAST) |
116 return last(); | 109 return last(); |
117 | 110 |
118 if (field_type == NAME_MIDDLE_INITIAL) | 111 if (field_type == NAME_MIDDLE_INITIAL) |
119 return MiddleInitial(); | 112 return MiddleInitial(); |
120 | 113 |
121 if (field_type == NAME_FULL) | 114 if (field_type == NAME_FULL) |
122 return FullName(); | 115 return FullName(); |
123 | 116 |
124 if (field_type == NAME_SUFFIX) | |
125 return suffix(); | |
126 | |
127 if (field_type == EMAIL_ADDRESS) | |
128 return email(); | |
129 | |
130 if (field_type == COMPANY_NAME) | |
131 return company_name(); | |
132 | |
133 return string16(); | 117 return string16(); |
134 } | 118 } |
135 | 119 |
136 void ContactInfo::SetInfo(const AutofillType& type, const string16& value) { | 120 void NameInfo::SetInfo(const AutofillType& type, const string16& value) { |
137 AutofillFieldType field_type = type.field_type(); | 121 AutofillFieldType field_type = type.field_type(); |
138 DCHECK_EQ(AutofillType::CONTACT_INFO, type.group()); | 122 DCHECK_EQ(AutofillType::NAME, type.group()); |
139 if (field_type == NAME_FIRST) | 123 if (field_type == NAME_FIRST) |
140 SetFirst(value); | 124 SetFirst(value); |
141 else if (field_type == NAME_MIDDLE || field_type == NAME_MIDDLE_INITIAL) | 125 else if (field_type == NAME_MIDDLE || field_type == NAME_MIDDLE_INITIAL) |
142 SetMiddle(value); | 126 SetMiddle(value); |
143 else if (field_type == NAME_LAST) | 127 else if (field_type == NAME_LAST) |
144 SetLast(value); | 128 SetLast(value); |
145 else if (field_type == NAME_SUFFIX) | |
146 set_suffix(value); | |
147 else if (field_type == EMAIL_ADDRESS) | |
148 email_ = value; | |
149 else if (field_type == COMPANY_NAME) | |
150 company_name_ = value; | |
151 else if (field_type == NAME_FULL) | 129 else if (field_type == NAME_FULL) |
152 SetFullName(value); | 130 SetFullName(value); |
153 else | 131 else |
154 NOTREACHED(); | 132 NOTREACHED(); |
155 } | 133 } |
156 | 134 |
157 ContactInfo::ContactInfo(const ContactInfo& contact_info) | 135 bool NameInfo::FindInfoMatchesHelper(const AutofillFieldType& field_type, |
158 : FormGroup(), | |
159 first_tokens_(contact_info.first_tokens_), | |
160 middle_tokens_(contact_info.middle_tokens_), | |
161 last_tokens_(contact_info.last_tokens_), | |
162 first_(contact_info.first_), | |
163 middle_(contact_info.middle_), | |
164 last_(contact_info.last_), | |
165 suffix_(contact_info.suffix_), | |
166 email_(contact_info.email_), | |
167 company_name_(contact_info.company_name_) { | |
168 } | |
169 | |
170 string16 ContactInfo::FullName() const { | |
171 if (first_.empty()) | |
172 return string16(); | |
173 | |
174 std::vector<string16> full_name; | |
175 full_name.push_back(first_); | |
176 | |
177 if (!middle_.empty()) | |
178 full_name.push_back(middle_); | |
179 | |
180 if (!last_.empty()) | |
181 full_name.push_back(last_); | |
182 | |
183 if (!suffix_.empty()) | |
184 full_name.push_back(suffix_); | |
185 | |
186 return JoinString(full_name, ' '); | |
187 } | |
188 | |
189 string16 ContactInfo::MiddleInitial() const { | |
190 if (middle_.empty()) | |
191 return string16(); | |
192 | |
193 string16 middle_name(middle()); | |
194 string16 initial; | |
195 initial.push_back(middle_name[0]); | |
196 return initial; | |
197 } | |
198 | |
199 bool ContactInfo::FindInfoMatchesHelper(const AutofillFieldType& field_type, | |
200 const string16& info, | 136 const string16& info, |
201 string16* match) const { | 137 string16* match) const { |
202 if (match == NULL) { | 138 if (match == NULL) { |
203 DLOG(ERROR) << "NULL match string passed in"; | 139 DLOG(ERROR) << "NULL match string passed in"; |
204 return false; | 140 return false; |
205 } | 141 } |
206 | 142 |
207 match->clear(); | 143 match->clear(); |
208 if (field_type == NAME_FIRST && | 144 if (field_type == NAME_FIRST && |
209 StartsWith(first(), info, false)) { | 145 StartsWith(first(), info, false)) { |
210 *match = first(); | 146 *match = first(); |
211 } else if (field_type == NAME_MIDDLE && | 147 } else if (field_type == NAME_MIDDLE && |
212 StartsWith(middle(), info, false)) { | 148 StartsWith(middle(), info, false)) { |
213 *match = middle(); | 149 *match = middle(); |
214 } else if (field_type == NAME_LAST && | 150 } else if (field_type == NAME_LAST && |
215 StartsWith(last(), info, false)) { | 151 StartsWith(last(), info, false)) { |
216 *match = last(); | 152 *match = last(); |
217 } else if (field_type == NAME_SUFFIX && | |
218 StartsWith(suffix(), info, false)) { | |
219 *match = suffix(); | |
220 } else if (field_type == NAME_MIDDLE_INITIAL && IsMiddleInitial(info)) { | 153 } else if (field_type == NAME_MIDDLE_INITIAL && IsMiddleInitial(info)) { |
221 *match = MiddleInitial(); | 154 *match = MiddleInitial(); |
222 } else if (field_type == NAME_FULL && | 155 } else if (field_type == NAME_FULL && |
223 StartsWith(FullName(), info, false)) { | 156 StartsWith(FullName(), info, false)) { |
224 *match = FullName(); | 157 *match = FullName(); |
225 } else if (field_type == EMAIL_ADDRESS && | |
226 StartsWith(email(), info, false)) { | |
227 *match = email(); | |
228 } else if (field_type == COMPANY_NAME && | |
229 StartsWith(company_name(), info, false)) { | |
230 *match = company_name(); | |
231 } | 158 } |
232 | 159 |
233 return !match->empty(); | 160 return !match->empty(); |
234 } | 161 } |
235 | 162 |
| 163 string16 NameInfo::FullName() const { |
| 164 if (first_.empty()) |
| 165 return string16(); |
| 166 |
| 167 std::vector<string16> full_name; |
| 168 full_name.push_back(first_); |
| 169 |
| 170 if (!middle_.empty()) |
| 171 full_name.push_back(middle_); |
| 172 |
| 173 if (!last_.empty()) |
| 174 full_name.push_back(last_); |
| 175 |
| 176 return JoinString(full_name, ' '); |
| 177 } |
| 178 |
| 179 string16 NameInfo::MiddleInitial() const { |
| 180 if (middle_.empty()) |
| 181 return string16(); |
| 182 |
| 183 string16 middle_name(middle()); |
| 184 string16 initial; |
| 185 initial.push_back(middle_name[0]); |
| 186 return initial; |
| 187 } |
| 188 |
236 // If each of the 'words' contained in the text are also present in the first | 189 // If each of the 'words' contained in the text are also present in the first |
237 // name then we will consider the text to be of type kFirstName. This means | 190 // name then we will consider the text to be of type kFirstName. This means |
238 // that people with multiple first names will be able to enter any one of | 191 // that people with multiple first names will be able to enter any one of |
239 // their first names and have it correctly recognized. | 192 // their first names and have it correctly recognized. |
240 bool ContactInfo::IsFirstName(const string16& text) const { | 193 bool NameInfo::IsFirstName(const string16& text) const { |
241 return IsNameMatch(text, first_tokens_); | 194 return IsNameMatch(text, first_tokens_); |
242 } | 195 } |
243 | 196 |
244 // If each of the 'words' contained in the text are also present in the middle | 197 // If each of the 'words' contained in the text are also present in the middle |
245 // name then we will consider the text to be of type kMiddleName. | 198 // name then we will consider the text to be of type kMiddleName. |
246 bool ContactInfo::IsMiddleName(const string16& text) const { | 199 bool NameInfo::IsMiddleName(const string16& text) const { |
247 return IsNameMatch(text, middle_tokens_); | 200 return IsNameMatch(text, middle_tokens_); |
248 } | 201 } |
249 | 202 |
250 // If each of the 'words' contained in the text are also present in the last | 203 // If each of the 'words' contained in the text are also present in the last |
251 // name then we will consider the text to be of type kLastName. | 204 // name then we will consider the text to be of type kLastName. |
252 bool ContactInfo::IsLastName(const string16& text) const { | 205 bool NameInfo::IsLastName(const string16& text) const { |
253 return IsNameMatch(text, last_tokens_); | 206 return IsNameMatch(text, last_tokens_); |
254 } | 207 } |
255 | 208 |
256 bool ContactInfo::IsSuffix(const string16& text) const { | 209 bool NameInfo::IsMiddleInitial(const string16& text) const { |
257 string16 lower_suffix = StringToLowerASCII(suffix_); | |
258 return (lower_suffix == text); | |
259 } | |
260 | |
261 bool ContactInfo::IsMiddleInitial(const string16& text) const { | |
262 if (text.length() != 1) | 210 if (text.length() != 1) |
263 return false; | 211 return false; |
264 | 212 |
265 string16 lower_case = StringToLowerASCII(text); | 213 string16 lower_case = StringToLowerASCII(text); |
266 // If the text entered was a single character and it matches the first letter | 214 // If the text entered was a single character and it matches the first letter |
267 // of any of the given middle names then we consider it to be a middle | 215 // of any of the given middle names then we consider it to be a middle |
268 // initial field. | 216 // initial field. |
269 size_t middle_tokens_size = middle_tokens_.size(); | 217 size_t middle_tokens_size = middle_tokens_.size(); |
270 for (size_t i = 0; i < middle_tokens_size; ++i) { | 218 for (size_t i = 0; i < middle_tokens_size; ++i) { |
271 if (middle_tokens_[i][0] == lower_case[0]) | 219 if (middle_tokens_[i][0] == lower_case[0]) |
272 return true; | 220 return true; |
273 } | 221 } |
274 | 222 |
275 return false; | 223 return false; |
276 } | 224 } |
277 | 225 |
278 // A field will be considered to be of type NAME_FULL if: | 226 // A field will be considered to be of type NAME_FULL if: |
279 // 1) it contains at least one word from the first name. | 227 // 1) it contains at least one word from the first name. |
280 // 2) it contains at least one word from the last name. | 228 // 2) it contains at least one word from the last name. |
281 // 3) all of the words in the field match a word in either the first, | 229 // 3) all of the words in the field match a word in either the first, |
282 // middle, or last name. | 230 // middle, or last name. |
283 bool ContactInfo::IsFullName(const string16& text) const { | 231 bool NameInfo::IsFullName(const string16& text) const { |
284 size_t first_tokens_size = first_tokens_.size(); | 232 size_t first_tokens_size = first_tokens_.size(); |
285 if (first_tokens_size == 0) | 233 if (first_tokens_size == 0) |
286 return false; | 234 return false; |
287 | 235 |
288 size_t middle_tokens_size = middle_tokens_.size(); | 236 size_t middle_tokens_size = middle_tokens_.size(); |
289 | 237 |
290 size_t last_tokens_size = last_tokens_.size(); | 238 size_t last_tokens_size = last_tokens_.size(); |
291 if (last_tokens_size == 0) | 239 if (last_tokens_size == 0) |
292 return false; | 240 return false; |
293 | 241 |
294 NameTokens text_tokens; | 242 std::vector<string16> text_tokens; |
295 Tokenize(text, kNameSplitChars, &text_tokens); | 243 Tokenize(text, kNameSplitChars, &text_tokens); |
296 size_t text_tokens_size = text_tokens.size(); | 244 size_t text_tokens_size = text_tokens.size(); |
297 if (text_tokens_size == 0 || text_tokens_size < 2) | 245 if (text_tokens_size == 0 || text_tokens_size < 2) |
298 return false; | 246 return false; |
299 | 247 |
300 size_t name_tokens_size = | 248 size_t name_tokens_size = |
301 first_tokens_size + middle_tokens_size + last_tokens_size; | 249 first_tokens_size + middle_tokens_size + last_tokens_size; |
302 if (text_tokens_size > name_tokens_size) | 250 if (text_tokens_size > name_tokens_size) |
303 return false; | 251 return false; |
304 | 252 |
305 bool first_name_match = false; | 253 bool first_name_match = false; |
306 bool last_name_match = false; | 254 bool last_name_match = false; |
307 NameTokens::iterator iter; | 255 for (std::vector<string16>::iterator iter = text_tokens.begin(); |
308 for (iter = text_tokens.begin(); iter != text_tokens.end(); ++iter) { | 256 iter != text_tokens.end(); ++iter) { |
309 bool match = false; | 257 bool match = false; |
310 if (IsWordInName(*iter, first_tokens_)) { | 258 if (IsWordInName(*iter, first_tokens_)) { |
311 match = true; | 259 match = true; |
312 first_name_match = true; | 260 first_name_match = true; |
313 } | 261 } |
314 | 262 |
315 if (IsWordInName(*iter, last_tokens_)) { | 263 if (IsWordInName(*iter, last_tokens_)) { |
316 match = true; | 264 match = true; |
317 last_name_match = true; | 265 last_name_match = true; |
318 } | 266 } |
319 | 267 |
320 if (IsWordInName(*iter, middle_tokens_)) | 268 if (IsWordInName(*iter, middle_tokens_)) |
321 match = true; | 269 match = true; |
322 | 270 |
323 if (!match) | 271 if (!match) |
324 return false; | 272 return false; |
325 } | 273 } |
326 | 274 |
327 return (first_name_match && last_name_match); | 275 return (first_name_match && last_name_match); |
328 } | 276 } |
329 | 277 |
330 bool ContactInfo::IsNameMatch(const string16& text, | 278 bool NameInfo::IsNameMatch(const string16& text, |
331 const NameTokens& name_tokens) const { | 279 const std::vector<string16>& name_tokens) const { |
332 size_t name_tokens_size = name_tokens.size(); | 280 size_t name_tokens_size = name_tokens.size(); |
333 if (name_tokens_size == 0) | 281 if (name_tokens_size == 0) |
334 return false; | 282 return false; |
335 | 283 |
336 NameTokens text_tokens; | 284 std::vector<string16> text_tokens; |
337 Tokenize(text, kNameSplitChars, &text_tokens); | 285 Tokenize(text, kNameSplitChars, &text_tokens); |
338 size_t text_tokens_size = text_tokens.size(); | 286 size_t text_tokens_size = text_tokens.size(); |
339 if (text_tokens_size == 0) | 287 if (text_tokens_size == 0) |
340 return false; | 288 return false; |
341 | 289 |
342 if (text_tokens_size > name_tokens_size) | 290 if (text_tokens_size > name_tokens_size) |
343 return false; | 291 return false; |
344 | 292 |
345 // If each of the 'words' contained in the text are also present in the name, | 293 // If each of the 'words' contained in the text are also present in the name, |
346 // then we will consider the text to match the name. | 294 // then we will consider the text to match the name. |
347 NameTokens::iterator iter; | 295 for (std::vector<string16>::iterator iter = text_tokens.begin(); |
348 for (iter = text_tokens.begin(); iter != text_tokens.end(); ++iter) { | 296 iter != text_tokens.end(); ++iter) { |
349 if (!IsWordInName(*iter, name_tokens)) | 297 if (!IsWordInName(*iter, name_tokens)) |
350 return false; | 298 return false; |
351 } | 299 } |
352 | 300 |
353 return true; | 301 return true; |
354 } | 302 } |
355 | 303 |
356 bool ContactInfo::IsWordInName(const string16& word, | 304 bool NameInfo::IsWordInName(const string16& word, |
357 const NameTokens& name_tokens) const { | 305 const std::vector<string16>& name_tokens) const { |
358 NameTokens::const_iterator iter; | 306 for (std::vector<string16>::const_iterator iter = name_tokens.begin(); |
359 for (iter = name_tokens.begin(); iter != name_tokens.end(); ++iter) { | 307 iter != name_tokens.end(); ++iter) { |
360 // |*iter| is already lower-cased. | 308 // |*iter| is already lower-cased. |
361 if (StringToLowerASCII(word) == *iter) | 309 if (StringToLowerASCII(word) == *iter) |
362 return true; | 310 return true; |
363 } | 311 } |
364 | 312 |
365 return false; | 313 return false; |
366 } | 314 } |
367 | 315 |
368 void ContactInfo::SetFirst(const string16& first) { | 316 void NameInfo::SetFirst(const string16& first) { |
369 first_ = first; | 317 first_ = first; |
370 first_tokens_.clear(); | 318 first_tokens_.clear(); |
371 Tokenize(first, kNameSplitChars, &first_tokens_); | 319 Tokenize(first, kNameSplitChars, &first_tokens_); |
372 NameTokens::iterator iter; | 320 for (std::vector<string16>::iterator iter = first_tokens_.begin(); |
373 for (iter = first_tokens_.begin(); iter != first_tokens_.end(); ++iter) | 321 iter != first_tokens_.end(); ++iter) { |
374 *iter = StringToLowerASCII(*iter); | 322 *iter = StringToLowerASCII(*iter); |
| 323 } |
375 } | 324 } |
376 | 325 |
377 void ContactInfo::SetMiddle(const string16& middle) { | 326 void NameInfo::SetMiddle(const string16& middle) { |
378 middle_ = middle; | 327 middle_ = middle; |
379 middle_tokens_.clear(); | 328 middle_tokens_.clear(); |
380 Tokenize(middle, kNameSplitChars, &middle_tokens_); | 329 Tokenize(middle, kNameSplitChars, &middle_tokens_); |
381 NameTokens::iterator iter; | 330 for (std::vector<string16>::iterator iter = middle_tokens_.begin(); |
382 for (iter = middle_tokens_.begin(); iter != middle_tokens_.end(); ++iter) | 331 iter != middle_tokens_.end(); ++iter) { |
383 *iter = StringToLowerASCII(*iter); | 332 *iter = StringToLowerASCII(*iter); |
| 333 } |
384 } | 334 } |
385 | 335 |
386 void ContactInfo::SetLast(const string16& last) { | 336 void NameInfo::SetLast(const string16& last) { |
387 last_ = last; | 337 last_ = last; |
388 last_tokens_.clear(); | 338 last_tokens_.clear(); |
389 Tokenize(last, kNameSplitChars, &last_tokens_); | 339 Tokenize(last, kNameSplitChars, &last_tokens_); |
390 NameTokens::iterator iter; | 340 for (std::vector<string16>::iterator iter = last_tokens_.begin(); |
391 for (iter = last_tokens_.begin(); iter != last_tokens_.end(); ++iter) | 341 iter != last_tokens_.end(); ++iter) { |
392 *iter = StringToLowerASCII(*iter); | 342 *iter = StringToLowerASCII(*iter); |
| 343 } |
393 } | 344 } |
394 | 345 |
395 void ContactInfo::SetFullName(const string16& full) { | 346 void NameInfo::SetFullName(const string16& full) { |
396 NameTokens full_name_tokens; | 347 std::vector<string16> full_name_tokens; |
397 Tokenize(full, ASCIIToUTF16(" "), &full_name_tokens); | 348 Tokenize(full, ASCIIToUTF16(" "), &full_name_tokens); |
398 // Clear the names. | 349 // Clear the names. |
399 SetFirst(string16()); | 350 SetFirst(string16()); |
400 SetMiddle(string16()); | 351 SetMiddle(string16()); |
401 SetLast(string16()); | 352 SetLast(string16()); |
402 | 353 |
403 // There are four possibilities: empty; first name; first and last names; | 354 // There are four possibilities: empty; first name; first and last names; |
404 // first, middle (possibly multiple strings) and then the last name. | 355 // first, middle (possibly multiple strings) and then the last name. |
405 if (full_name_tokens.size() > 0) { | 356 if (full_name_tokens.size() > 0) { |
406 SetFirst(full_name_tokens[0]); | 357 SetFirst(full_name_tokens[0]); |
407 if (full_name_tokens.size() > 1) { | 358 if (full_name_tokens.size() > 1) { |
408 SetLast(full_name_tokens.back()); | 359 SetLast(full_name_tokens.back()); |
409 if (full_name_tokens.size() > 2) { | 360 if (full_name_tokens.size() > 2) { |
410 full_name_tokens.erase(full_name_tokens.begin()); | 361 full_name_tokens.erase(full_name_tokens.begin()); |
411 full_name_tokens.pop_back(); | 362 full_name_tokens.pop_back(); |
412 SetMiddle(JoinString(full_name_tokens, ' ')); | 363 SetMiddle(JoinString(full_name_tokens, ' ')); |
413 } | 364 } |
414 } | 365 } |
415 } | 366 } |
416 } | 367 } |
417 | 368 |
| 369 EmailInfo::EmailInfo() {} |
| 370 |
| 371 EmailInfo::EmailInfo(const EmailInfo& info) : FormGroup() { |
| 372 *this = info; |
| 373 } |
| 374 |
| 375 EmailInfo::~EmailInfo() {} |
| 376 |
| 377 EmailInfo& EmailInfo::operator=(const EmailInfo& info) { |
| 378 if (this == &info) |
| 379 return *this; |
| 380 |
| 381 email_ = info.email_; |
| 382 return *this; |
| 383 } |
| 384 |
| 385 void EmailInfo::GetPossibleFieldTypes(const string16& text, |
| 386 FieldTypeSet* possible_types) const { |
| 387 DCHECK(possible_types); |
| 388 // TODO(isherman): Investigate case-insensitive comparison. |
| 389 if (email_ == text) |
| 390 possible_types->insert(EMAIL_ADDRESS); |
| 391 } |
| 392 |
| 393 void EmailInfo::GetAvailableFieldTypes(FieldTypeSet* available_types) const { |
| 394 DCHECK(available_types); |
| 395 if (!email_.empty()) |
| 396 available_types->insert(EMAIL_ADDRESS); |
| 397 } |
| 398 |
| 399 void EmailInfo::FindInfoMatches(const AutofillType& type, |
| 400 const string16& info, |
| 401 std::vector<string16>* matched_text) const { |
| 402 DCHECK(matched_text); |
| 403 |
| 404 string16 match; |
| 405 if (type.field_type() == UNKNOWN_TYPE && StartsWith(email_, info, false)) { |
| 406 matched_text->push_back(email_); |
| 407 } else if (type.field_type() == EMAIL_ADDRESS && |
| 408 StartsWith(email_, info, false)) { |
| 409 matched_text->push_back(email_); |
| 410 } |
| 411 } |
| 412 |
| 413 string16 EmailInfo::GetFieldText(const AutofillType& type) const { |
| 414 AutofillFieldType field_type = type.field_type(); |
| 415 if (field_type == EMAIL_ADDRESS) |
| 416 return email_; |
| 417 |
| 418 return string16(); |
| 419 } |
| 420 |
| 421 void EmailInfo::SetInfo(const AutofillType& type, const string16& value) { |
| 422 DCHECK_EQ(AutofillType::EMAIL, type.group()); |
| 423 email_ = value; |
| 424 } |
| 425 |
| 426 CompanyInfo::CompanyInfo() {} |
| 427 |
| 428 CompanyInfo::CompanyInfo(const CompanyInfo& info) : FormGroup() { |
| 429 *this = info; |
| 430 } |
| 431 |
| 432 CompanyInfo::~CompanyInfo() {} |
| 433 |
| 434 CompanyInfo& CompanyInfo::operator=(const CompanyInfo& info) { |
| 435 if (this == &info) |
| 436 return *this; |
| 437 |
| 438 company_name_ = info.company_name_; |
| 439 return *this; |
| 440 } |
| 441 |
| 442 void CompanyInfo::GetPossibleFieldTypes(const string16& text, |
| 443 FieldTypeSet* possible_types) const { |
| 444 DCHECK(possible_types); |
| 445 |
| 446 if (company_name_ == text) |
| 447 possible_types->insert(COMPANY_NAME); |
| 448 } |
| 449 |
| 450 void CompanyInfo::GetAvailableFieldTypes(FieldTypeSet* available_types) const { |
| 451 DCHECK(available_types); |
| 452 |
| 453 if (!company_name_.empty()) |
| 454 available_types->insert(COMPANY_NAME); |
| 455 } |
| 456 |
| 457 void CompanyInfo::FindInfoMatches(const AutofillType& type, |
| 458 const string16& info, |
| 459 std::vector<string16>* matched_text) const { |
| 460 DCHECK(matched_text); |
| 461 |
| 462 string16 match; |
| 463 if (type.field_type() == UNKNOWN_TYPE && |
| 464 StartsWith(company_name_, info, false)) { |
| 465 matched_text->push_back(company_name_); |
| 466 } else if (type.field_type() == COMPANY_NAME && |
| 467 StartsWith(company_name_, info, false)) { |
| 468 matched_text->push_back(company_name_); |
| 469 } |
| 470 } |
| 471 |
| 472 string16 CompanyInfo::GetFieldText(const AutofillType& type) const { |
| 473 AutofillFieldType field_type = type.field_type(); |
| 474 |
| 475 if (field_type == COMPANY_NAME) |
| 476 return company_name_; |
| 477 |
| 478 return string16(); |
| 479 } |
| 480 |
| 481 void CompanyInfo::SetInfo(const AutofillType& type, const string16& value) { |
| 482 DCHECK_EQ(AutofillType::COMPANY, type.group()); |
| 483 company_name_ = value; |
| 484 } |
OLD | NEW |