Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(29)

Side by Side Diff: components/autofill/core/browser/contact_info.cc

Issue 310463005: Fill in more name fields with requestAutocomplete (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: abuse SetRawInfo Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 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 "components/autofill/core/browser/contact_info.h" 5 #include "components/autofill/core/browser/contact_info.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <ostream> 8 #include <ostream>
9 #include <string> 9 #include <string>
10 10
11 #include "base/basictypes.h" 11 #include "base/basictypes.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/strings/string_util.h" 13 #include "base/strings/string_util.h"
14 #include "base/strings/utf_string_conversions.h" 14 #include "base/strings/utf_string_conversions.h"
15 #include "components/autofill/core/browser/autofill_type.h" 15 #include "components/autofill/core/browser/autofill_type.h"
16 16
17 namespace autofill { 17 namespace autofill {
18 18
19 namespace {
20
21 static const char* const name_prefixes[] = {
Ilya Sherman 2014/06/03 22:53:35 nit: No need for "static"; you're already in an an
Evan Stade 2014/06/03 23:55:35 Done.
22 "1lt", "1st", "2lt", "2nd", "3rd", "admiral", "capt", "captain", "col",
23 "cpt", "dr", "gen", "general", "lcdr", "lt", "ltc", "ltg", "ltjg", "maj",
24 "major", "mg", "mr", "mrs", "ms", "pastor", "prof", "rep", "reverend",
25 "rev", "sen", "st" };
26
27 static const char* const name_suffixes[] = {
28 "b.a", "ba", "d.d.s", "dds", "i", "ii", "iii", "iv", "ix", "jr", "m.a",
29 "m.d", "ma", "md", "ms", "ph.d", "phd", "sr", "v", "vi", "vii", "viii",
30 "x" };
31
32 static const char* const family_name_prefixes[] = {
33 "d\'", "de", "del", "der", "di", "la", "le", "mc", "san", "st", "ter",
Ilya Sherman 2014/06/03 22:53:35 Out of curiousity, why does the apostrophe need to
Evan Stade 2014/06/03 23:55:35 I just copy-pasta'd that. Turns out it doesn't nee
34 "van", "von" };
35
36 // Returns true if |set| contains |element|, modulo a final period.
37 bool ContainsString(const char* const set[],
38 size_t set_size,
39 const base::string16& element) {
40 if (!base::IsStringASCII(element))
41 return false;
42
43 base::string16 trimmed_element;
44 base::TrimString(element, base::ASCIIToUTF16("."), &trimmed_element);
Ilya Sherman 2014/06/03 22:53:35 Why not just remove all dots, rather than removing
Evan Stade 2014/06/03 23:55:35 I copied this behavior from the original code (alt
Ilya Sherman 2014/06/04 00:13:45 In that case, how about not stripping trailing dot
Evan Stade 2014/06/04 00:24:23 But then "John Smith Jr." wouldn't match against "
Ilya Sherman 2014/06/04 00:34:32 *sigh*, alright, I cave :P
45
46 for (size_t i = 0; i < set_size; ++i) {
47 if (LowerCaseEqualsASCII(trimmed_element, set[i]))
48 return true;
49 }
50
51 return false;
52 }
53
54 // Removes common name prefixes from |name_tokens|.
55 void StripPrefixes(std::vector<base::string16>* name_tokens) {
56 std::vector<base::string16>::iterator iter = name_tokens->begin();
57 while(iter != name_tokens->end()) {
58 if (!ContainsString(name_prefixes, arraysize(name_prefixes), *iter))
59 break;
60 ++iter;
61 }
62
63 name_tokens->assign(iter, name_tokens->end());
64 }
65
66 // Removes common name suffixes from |name_tokens|.
67 void StripSuffixes(std::vector<base::string16>* name_tokens) {
68 std::vector<base::string16>::iterator iter = name_tokens->end();
69 while(iter != name_tokens->begin()) {
70 if (!ContainsString(name_suffixes, arraysize(name_suffixes), *--iter))
71 break;
72 }
73
74 name_tokens->assign(name_tokens->begin(), ++iter);
75 }
Ilya Sherman 2014/06/03 22:53:35 I think this can be written more simply using std:
Evan Stade 2014/06/03 23:55:35 but this breaks out of the loop the first time it
Ilya Sherman 2014/06/04 00:13:45 Hrm, you're right. Nevermind.
76
77 struct NameParts {
78 base::string16 given;
79 base::string16 middle;
80 base::string16 family;
Ilya Sherman 2014/06/03 22:53:35 nit: I think we should be consistent about first/m
Evan Stade 2014/06/03 23:55:35 I'd rather tackle that when we get around to imple
Ilya Sherman 2014/06/04 00:13:45 I'd really prefer to keep the file internally cons
Evan Stade 2014/06/04 00:24:23 Done.
81 };
82
83 // TODO(estade): this does Western name splitting. It should do different
Ilya Sherman 2014/06/03 22:53:35 nit: "this" -> "This"
Evan Stade 2014/06/03 23:55:35 done, although the precedent that exists through C
84 // splitting based on the app locale.
85 NameParts SplitName(const base::string16& name) {
Ilya Sherman 2014/06/03 22:53:35 Optional nit: Perhaps "ParseName()" rather than "S
Evan Stade 2014/06/03 23:55:35 name inspired by com.android.providers.contacts.Na
86 std::vector<base::string16> name_tokens;
87 Tokenize(name, base::ASCIIToUTF16(" ,"), &name_tokens);
88
89 StripPrefixes(&name_tokens);
90
91 if (name_tokens.size() > 2)
Ilya Sherman 2014/06/03 22:53:34 Why > 2? Can suffixes ever be valid names?
Evan Stade 2014/06/03 23:55:35 Yes. John Ma.
Ilya Sherman 2014/06/04 00:13:45 Ok... in that case, is "Ma" more likely to be a su
Evan Stade 2014/06/04 00:24:23 Well, if John has a middle name he's SOL. But this
Ilya Sherman 2014/06/04 00:34:32 Well, sure, I suppose heuristics are always going
Evan Stade 2014/06/04 01:26:29 Done.
92 StripSuffixes(&name_tokens);
93
94 NameParts parts;
95
96 // Bad things have happened; just assume the whole thing is a given name.
Ilya Sherman 2014/06/03 22:53:34 nit: Please move this comment into the if-stmt. U
Evan Stade 2014/06/03 23:55:35 Done.
97 if (name_tokens.empty()) {
98 parts.given = name;
99 return parts;
100 }
101
102 // Only one token, assume given name.
103 if (name_tokens.size() == 1) {
104 parts.given = name_tokens[0];
105 return parts;
106 }
107
108 // 2 or more tokens. Grab the family, which is the last word plus any
109 // recognizable family prefixes.
110 std::vector<base::string16> reverse_family_tokens;
111 reverse_family_tokens.push_back(name_tokens.back());
112 name_tokens.pop_back();
113 while (name_tokens.size() >= 1) {
114 if (!ContainsString(family_name_prefixes,
115 arraysize(family_name_prefixes),
116 name_tokens.back())) {
Ilya Sherman 2014/06/03 22:53:34 nit: Please move this into the loop condition.
Evan Stade 2014/06/03 23:55:35 Done.
117 break;
118 }
119
120 reverse_family_tokens.push_back(name_tokens.back());
121 name_tokens.pop_back();
122 }
123
124 std::vector<base::string16> family_tokens(reverse_family_tokens.rbegin(),
125 reverse_family_tokens.rend());
Ilya Sherman 2014/06/03 22:53:34 nit: alignment
126 parts.family = JoinString(family_tokens, base::char16(' '));
127
128 // Take the last remaining token as the middle name (if there are at least 2
129 // tokens).
130 if (name_tokens.size() > 1) {
Ilya Sherman 2014/06/03 22:53:35 Optional nit: I think ">= 2" conveys the intent sl
Evan Stade 2014/06/03 23:55:35 sure, why not
131 parts.middle = name_tokens.back();
132 name_tokens.pop_back();
133 }
134
135 // Remainder is given name.
136 parts.given = JoinString(name_tokens, base::char16(' '));
Ilya Sherman 2014/06/03 22:53:34 Are multi-word given names really more common than
Evan Stade 2014/06/03 23:55:35 I don't know. Someone thought about it and decided
137
138 return parts;
139 }
140
141 } // namespace
142
19 NameInfo::NameInfo() {} 143 NameInfo::NameInfo() {}
20 144
21 NameInfo::NameInfo(const NameInfo& info) : FormGroup() { 145 NameInfo::NameInfo(const NameInfo& info) : FormGroup() {
22 *this = info; 146 *this = info;
23 } 147 }
24 148
25 NameInfo::~NameInfo() {} 149 NameInfo::~NameInfo() {}
26 150
27 NameInfo& NameInfo::operator=(const NameInfo& info) { 151 NameInfo& NameInfo::operator=(const NameInfo& info) {
28 if (this == &info) 152 if (this == &info)
29 return *this; 153 return *this;
30 154
31 first_ = info.first_; 155 first_ = info.first_;
32 middle_ = info.middle_; 156 middle_ = info.middle_;
33 last_ = info.last_; 157 last_ = info.last_;
158 full_name_ = info.full_name_;
34 return *this; 159 return *this;
35 } 160 }
36 161
37 void NameInfo::GetSupportedTypes(ServerFieldTypeSet* supported_types) const { 162 void NameInfo::GetSupportedTypes(ServerFieldTypeSet* supported_types) const {
38 supported_types->insert(NAME_FIRST); 163 supported_types->insert(NAME_FIRST);
39 supported_types->insert(NAME_MIDDLE); 164 supported_types->insert(NAME_MIDDLE);
40 supported_types->insert(NAME_LAST); 165 supported_types->insert(NAME_LAST);
41 supported_types->insert(NAME_MIDDLE_INITIAL); 166 supported_types->insert(NAME_MIDDLE_INITIAL);
42 supported_types->insert(NAME_FULL); 167 supported_types->insert(NAME_FULL);
43 } 168 }
44 169
45 base::string16 NameInfo::GetRawInfo(ServerFieldType type) const { 170 base::string16 NameInfo::GetRawInfo(ServerFieldType type) const {
46 DCHECK_EQ(NAME, AutofillType(type).group()); 171 DCHECK_EQ(NAME, AutofillType(type).group());
47 switch (type) { 172 switch (type) {
48 case NAME_FIRST: 173 case NAME_FIRST:
49 return first(); 174 return first_;
50 175
51 case NAME_MIDDLE: 176 case NAME_MIDDLE:
52 return middle(); 177 return middle_;
53 178
54 case NAME_LAST: 179 case NAME_LAST:
55 return last(); 180 return last_;
56 181
57 case NAME_MIDDLE_INITIAL: 182 case NAME_MIDDLE_INITIAL:
58 return MiddleInitial(); 183 return MiddleInitial();
59 184
60 case NAME_FULL: 185 case NAME_FULL:
61 return FullName(); 186 return FullName();
62 187
63 default: 188 default:
64 return base::string16(); 189 return base::string16();
65 } 190 }
66 } 191 }
67 192
68 void NameInfo::SetRawInfo(ServerFieldType type, const base::string16& value) { 193 void NameInfo::SetRawInfo(ServerFieldType type, const base::string16& value) {
69 DCHECK_EQ(NAME, AutofillType(type).group()); 194 DCHECK_EQ(NAME, AutofillType(type).group());
70 switch (type) { 195 switch (type) {
71 case NAME_FIRST: 196 case NAME_FIRST:
72 first_ = value; 197 first_ = value;
73 break; 198 break;
74 199
75 case NAME_MIDDLE: 200 case NAME_MIDDLE:
76 case NAME_MIDDLE_INITIAL: 201 case NAME_MIDDLE_INITIAL:
77 middle_ = value; 202 middle_ = value;
78 break; 203 break;
79 204
80 case NAME_LAST: 205 case NAME_LAST:
81 last_ = value; 206 last_ = value;
82 break; 207 break;
83 208
84 case NAME_FULL: 209 case NAME_FULL:
85 SetFullName(value); 210 SetFullName(value);
Evan Stade 2014/06/03 01:36:36 I think this needs to happen in SetInfo, so app_lo
Ilya Sherman 2014/06/03 22:53:35 How does it break tests? Just because they call S
Evan Stade 2014/06/03 23:55:35 Well, a lot of tests do that, yes, and switching t
86 break; 211 break;
87 212
88 default: 213 default:
89 NOTREACHED(); 214 NOTREACHED();
90 } 215 }
91 } 216 }
92 217
93 base::string16 NameInfo::FullName() const { 218 base::string16 NameInfo::FullName() const {
219 if (!full_name_.empty())
220 return full_name_;
221
94 std::vector<base::string16> full_name; 222 std::vector<base::string16> full_name;
95 if (!first_.empty()) 223 if (!first_.empty())
96 full_name.push_back(first_); 224 full_name.push_back(first_);
97 225
98 if (!middle_.empty()) 226 if (!middle_.empty())
99 full_name.push_back(middle_); 227 full_name.push_back(middle_);
100 228
101 if (!last_.empty()) 229 if (!last_.empty())
102 full_name.push_back(last_); 230 full_name.push_back(last_);
103 231
104 return JoinString(full_name, ' '); 232 return JoinString(full_name, ' ');
105 } 233 }
106 234
107 base::string16 NameInfo::MiddleInitial() const { 235 base::string16 NameInfo::MiddleInitial() const {
108 if (middle_.empty()) 236 if (middle_.empty())
109 return base::string16(); 237 return base::string16();
110 238
111 base::string16 middle_name(middle()); 239 base::string16 middle_name(middle_);
112 base::string16 initial; 240 base::string16 initial;
113 initial.push_back(middle_name[0]); 241 initial.push_back(middle_name[0]);
114 return initial; 242 return initial;
115 } 243 }
116 244
117 void NameInfo::SetFullName(const base::string16& full) { 245 void NameInfo::SetFullName(const base::string16& full) {
118 // Clear the names. 246 full_name_ = full;
119 first_ = base::string16(); 247 NameParts parts = SplitName(full);
120 middle_ = base::string16(); 248 first_ = parts.given;
121 last_ = base::string16(); 249 middle_ = parts.middle;
122 250 last_ = parts.family;
Ilya Sherman 2014/06/03 22:53:35 Optional nit: It might be cleaner to just pass in
Evan Stade 2014/06/03 23:55:35 That's true, but I guess I have an intrinsic avers
123 std::vector<base::string16> full_name_tokens;
124 Tokenize(full, base::ASCIIToUTF16(" "), &full_name_tokens);
125
126 // There are four possibilities: empty; first name; first and last names;
127 // first, middle (possibly multiple strings) and then the last name.
128 if (full_name_tokens.size() > 0) {
129 first_ = full_name_tokens[0];
130 if (full_name_tokens.size() > 1) {
131 last_ = full_name_tokens.back();
132 if (full_name_tokens.size() > 2) {
133 full_name_tokens.erase(full_name_tokens.begin());
134 full_name_tokens.pop_back();
135 middle_ = JoinString(full_name_tokens, ' ');
136 }
137 }
138 }
139 } 251 }
140 252
141 EmailInfo::EmailInfo() {} 253 EmailInfo::EmailInfo() {}
142 254
143 EmailInfo::EmailInfo(const EmailInfo& info) : FormGroup() { 255 EmailInfo::EmailInfo(const EmailInfo& info) : FormGroup() {
144 *this = info; 256 *this = info;
145 } 257 }
146 258
147 EmailInfo::~EmailInfo() {} 259 EmailInfo::~EmailInfo() {}
148 260
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 return base::string16(); 309 return base::string16();
198 } 310 }
199 311
200 void CompanyInfo::SetRawInfo(ServerFieldType type, 312 void CompanyInfo::SetRawInfo(ServerFieldType type,
201 const base::string16& value) { 313 const base::string16& value) {
202 DCHECK_EQ(COMPANY_NAME, type); 314 DCHECK_EQ(COMPANY_NAME, type);
203 company_name_ = value; 315 company_name_ = value;
204 } 316 }
205 317
206 } // namespace autofill 318 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698