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

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: rename 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 const char* const name_prefixes[] = {
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 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 const char* const family_name_prefixes[] = {
33 "d'", "de", "del", "der", "di", "la", "le", "mc", "san", "st", "ter",
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);
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 }
76
77 struct NameParts {
78 base::string16 given;
79 base::string16 middle;
80 base::string16 family;
81 };
82
83 // TODO(estade): This does Western name splitting. It should do different
84 // splitting based on the app locale.
85 NameParts SplitName(const base::string16& name) {
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)
92 StripSuffixes(&name_tokens);
93
94 NameParts parts;
95
96 if (name_tokens.empty()) {
97 // Bad things have happened; just assume the whole thing is a given name.
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 ContainsString(family_name_prefixes,
115 arraysize(family_name_prefixes),
116 name_tokens.back())) {
117 reverse_family_tokens.push_back(name_tokens.back());
118 name_tokens.pop_back();
119 }
120
121 std::vector<base::string16> family_tokens(reverse_family_tokens.rbegin(),
122 reverse_family_tokens.rend());
123 parts.family = JoinString(family_tokens, base::char16(' '));
124
125 // Take the last remaining token as the middle name (if there are at least 2
126 // tokens).
127 if (name_tokens.size() >= 2) {
128 parts.middle = name_tokens.back();
129 name_tokens.pop_back();
130 }
131
132 // Remainder is given name.
133 parts.given = JoinString(name_tokens, base::char16(' '));
134
135 return parts;
136 }
137
138 } // namespace
139
19 NameInfo::NameInfo() {} 140 NameInfo::NameInfo() {}
20 141
21 NameInfo::NameInfo(const NameInfo& info) : FormGroup() { 142 NameInfo::NameInfo(const NameInfo& info) : FormGroup() {
22 *this = info; 143 *this = info;
23 } 144 }
24 145
25 NameInfo::~NameInfo() {} 146 NameInfo::~NameInfo() {}
26 147
27 NameInfo& NameInfo::operator=(const NameInfo& info) { 148 NameInfo& NameInfo::operator=(const NameInfo& info) {
28 if (this == &info) 149 if (this == &info)
29 return *this; 150 return *this;
30 151
31 first_ = info.first_; 152 given_ = info.given_;
32 middle_ = info.middle_; 153 middle_ = info.middle_;
33 last_ = info.last_; 154 family_ = info.family_;
155 full_name_ = info.full_name_;
34 return *this; 156 return *this;
35 } 157 }
36 158
37 void NameInfo::GetSupportedTypes(ServerFieldTypeSet* supported_types) const { 159 void NameInfo::GetSupportedTypes(ServerFieldTypeSet* supported_types) const {
38 supported_types->insert(NAME_FIRST); 160 supported_types->insert(NAME_FIRST);
39 supported_types->insert(NAME_MIDDLE); 161 supported_types->insert(NAME_MIDDLE);
40 supported_types->insert(NAME_LAST); 162 supported_types->insert(NAME_LAST);
41 supported_types->insert(NAME_MIDDLE_INITIAL); 163 supported_types->insert(NAME_MIDDLE_INITIAL);
42 supported_types->insert(NAME_FULL); 164 supported_types->insert(NAME_FULL);
43 } 165 }
44 166
45 base::string16 NameInfo::GetRawInfo(ServerFieldType type) const { 167 base::string16 NameInfo::GetRawInfo(ServerFieldType type) const {
46 DCHECK_EQ(NAME, AutofillType(type).group()); 168 DCHECK_EQ(NAME, AutofillType(type).group());
47 switch (type) { 169 switch (type) {
48 case NAME_FIRST: 170 case NAME_FIRST:
49 return first(); 171 return given_;
50 172
51 case NAME_MIDDLE: 173 case NAME_MIDDLE:
52 return middle(); 174 return middle_;
53 175
54 case NAME_LAST: 176 case NAME_LAST:
55 return last(); 177 return family_;
56 178
57 case NAME_MIDDLE_INITIAL: 179 case NAME_MIDDLE_INITIAL:
58 return MiddleInitial(); 180 return MiddleInitial();
59 181
60 case NAME_FULL: 182 case NAME_FULL:
61 return FullName(); 183 return FullName();
62 184
63 default: 185 default:
64 return base::string16(); 186 return base::string16();
65 } 187 }
66 } 188 }
67 189
68 void NameInfo::SetRawInfo(ServerFieldType type, const base::string16& value) { 190 void NameInfo::SetRawInfo(ServerFieldType type, const base::string16& value) {
69 DCHECK_EQ(NAME, AutofillType(type).group()); 191 DCHECK_EQ(NAME, AutofillType(type).group());
70 switch (type) { 192 switch (type) {
71 case NAME_FIRST: 193 case NAME_FIRST:
72 first_ = value; 194 given_ = value;
Ilya Sherman 2014/06/04 01:22:38 Btw, what happens if I call SetRawInfo(NAME_FIRST)
Evan Stade 2014/06/04 01:26:29 while working on updating the autofill table, I th
73 break; 195 break;
74 196
75 case NAME_MIDDLE: 197 case NAME_MIDDLE:
76 case NAME_MIDDLE_INITIAL: 198 case NAME_MIDDLE_INITIAL:
77 middle_ = value; 199 middle_ = value;
78 break; 200 break;
79 201
80 case NAME_LAST: 202 case NAME_LAST:
81 last_ = value; 203 family_ = value;
82 break; 204 break;
83 205
84 case NAME_FULL: 206 case NAME_FULL:
85 SetFullName(value); 207 SetFullName(value);
86 break; 208 break;
87 209
88 default: 210 default:
89 NOTREACHED(); 211 NOTREACHED();
90 } 212 }
91 } 213 }
92 214
93 base::string16 NameInfo::FullName() const { 215 base::string16 NameInfo::FullName() const {
216 if (!full_name_.empty())
217 return full_name_;
218
94 std::vector<base::string16> full_name; 219 std::vector<base::string16> full_name;
95 if (!first_.empty()) 220 if (!given_.empty())
96 full_name.push_back(first_); 221 full_name.push_back(given_);
97 222
98 if (!middle_.empty()) 223 if (!middle_.empty())
99 full_name.push_back(middle_); 224 full_name.push_back(middle_);
100 225
101 if (!last_.empty()) 226 if (!family_.empty())
102 full_name.push_back(last_); 227 full_name.push_back(family_);
103 228
104 return JoinString(full_name, ' '); 229 return JoinString(full_name, ' ');
105 } 230 }
106 231
107 base::string16 NameInfo::MiddleInitial() const { 232 base::string16 NameInfo::MiddleInitial() const {
108 if (middle_.empty()) 233 if (middle_.empty())
109 return base::string16(); 234 return base::string16();
110 235
111 base::string16 middle_name(middle()); 236 base::string16 middle_name(middle_);
112 base::string16 initial; 237 base::string16 initial;
113 initial.push_back(middle_name[0]); 238 initial.push_back(middle_name[0]);
114 return initial; 239 return initial;
115 } 240 }
116 241
117 void NameInfo::SetFullName(const base::string16& full) { 242 void NameInfo::SetFullName(const base::string16& full) {
118 // Clear the names. 243 full_name_ = full;
119 first_ = base::string16(); 244 NameParts parts = SplitName(full);
120 middle_ = base::string16(); 245 given_ = parts.given;
121 last_ = base::string16(); 246 middle_ = parts.middle;
122 247 family_ = parts.family;
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 } 248 }
140 249
141 EmailInfo::EmailInfo() {} 250 EmailInfo::EmailInfo() {}
142 251
143 EmailInfo::EmailInfo(const EmailInfo& info) : FormGroup() { 252 EmailInfo::EmailInfo(const EmailInfo& info) : FormGroup() {
144 *this = info; 253 *this = info;
145 } 254 }
146 255
147 EmailInfo::~EmailInfo() {} 256 EmailInfo::~EmailInfo() {}
148 257
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 return base::string16(); 306 return base::string16();
198 } 307 }
199 308
200 void CompanyInfo::SetRawInfo(ServerFieldType type, 309 void CompanyInfo::SetRawInfo(ServerFieldType type,
201 const base::string16& value) { 310 const base::string16& value) {
202 DCHECK_EQ(COMPANY_NAME, type); 311 DCHECK_EQ(COMPANY_NAME, type);
203 company_name_ = value; 312 company_name_ = value;
204 } 313 }
205 314
206 } // namespace autofill 315 } // namespace autofill
OLDNEW
« no previous file with comments | « components/autofill/core/browser/contact_info.h ('k') | components/autofill/core/browser/contact_info_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698