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: hack 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 // Don't assume "Ma" is a suffix in John Ma.
92 if (name_tokens.size() > 2)
93 StripSuffixes(&name_tokens);
94
95 NameParts parts;
96
97 if (name_tokens.empty()) {
98 // Bad things have happened; just assume the whole thing is a given name.
99 parts.given = name;
100 return parts;
101 }
102
103 // Only one token, assume given name.
104 if (name_tokens.size() == 1) {
105 parts.given = name_tokens[0];
106 return parts;
107 }
108
109 // 2 or more tokens. Grab the family, which is the last word plus any
110 // recognizable family prefixes.
111 std::vector<base::string16> reverse_family_tokens;
112 reverse_family_tokens.push_back(name_tokens.back());
113 name_tokens.pop_back();
114 while (name_tokens.size() >= 1 &&
115 ContainsString(family_name_prefixes,
116 arraysize(family_name_prefixes),
117 name_tokens.back())) {
118 reverse_family_tokens.push_back(name_tokens.back());
119 name_tokens.pop_back();
120 }
121
122 std::vector<base::string16> family_tokens(reverse_family_tokens.rbegin(),
123 reverse_family_tokens.rend());
124 parts.family = JoinString(family_tokens, base::char16(' '));
125
126 // Take the last remaining token as the middle name (if there are at least 2
127 // tokens).
128 if (name_tokens.size() >= 2) {
129 parts.middle = name_tokens.back();
130 name_tokens.pop_back();
131 }
132
133 // Remainder is given name.
134 parts.given = JoinString(name_tokens, base::char16(' '));
135
136 return parts;
137 }
138
139 } // namespace
140
19 NameInfo::NameInfo() {} 141 NameInfo::NameInfo() {}
20 142
21 NameInfo::NameInfo(const NameInfo& info) : FormGroup() { 143 NameInfo::NameInfo(const NameInfo& info) : FormGroup() {
22 *this = info; 144 *this = info;
23 } 145 }
24 146
25 NameInfo::~NameInfo() {} 147 NameInfo::~NameInfo() {}
26 148
27 NameInfo& NameInfo::operator=(const NameInfo& info) { 149 NameInfo& NameInfo::operator=(const NameInfo& info) {
28 if (this == &info) 150 if (this == &info)
29 return *this; 151 return *this;
30 152
31 first_ = info.first_; 153 given_ = info.given_;
32 middle_ = info.middle_; 154 middle_ = info.middle_;
33 last_ = info.last_; 155 family_ = info.family_;
156 full_ = info.full_;
34 return *this; 157 return *this;
35 } 158 }
36 159
37 void NameInfo::GetSupportedTypes(ServerFieldTypeSet* supported_types) const { 160 void NameInfo::GetSupportedTypes(ServerFieldTypeSet* supported_types) const {
38 supported_types->insert(NAME_FIRST); 161 supported_types->insert(NAME_FIRST);
39 supported_types->insert(NAME_MIDDLE); 162 supported_types->insert(NAME_MIDDLE);
40 supported_types->insert(NAME_LAST); 163 supported_types->insert(NAME_LAST);
41 supported_types->insert(NAME_MIDDLE_INITIAL); 164 supported_types->insert(NAME_MIDDLE_INITIAL);
42 supported_types->insert(NAME_FULL); 165 supported_types->insert(NAME_FULL);
43 } 166 }
44 167
45 base::string16 NameInfo::GetRawInfo(ServerFieldType type) const { 168 base::string16 NameInfo::GetRawInfo(ServerFieldType type) const {
46 DCHECK_EQ(NAME, AutofillType(type).group()); 169 DCHECK_EQ(NAME, AutofillType(type).group());
47 switch (type) { 170 switch (type) {
48 case NAME_FIRST: 171 case NAME_FIRST:
49 return first(); 172 return given_;
50 173
51 case NAME_MIDDLE: 174 case NAME_MIDDLE:
52 return middle(); 175 return middle_;
53 176
54 case NAME_LAST: 177 case NAME_LAST:
55 return last(); 178 return family_;
56 179
57 case NAME_MIDDLE_INITIAL: 180 case NAME_MIDDLE_INITIAL:
58 return MiddleInitial(); 181 return MiddleInitial();
59 182
60 case NAME_FULL: 183 case NAME_FULL:
61 return FullName(); 184 return FullName();
62 185
63 default: 186 default:
64 return base::string16(); 187 return base::string16();
65 } 188 }
66 } 189 }
67 190
68 void NameInfo::SetRawInfo(ServerFieldType type, const base::string16& value) { 191 void NameInfo::SetRawInfo(ServerFieldType type, const base::string16& value) {
69 DCHECK_EQ(NAME, AutofillType(type).group()); 192 DCHECK_EQ(NAME, AutofillType(type).group());
193
194 // Always clear out the full name if we're making a change.
195 if (value != GetRawInfo(type))
196 full_.clear();
197
70 switch (type) { 198 switch (type) {
71 case NAME_FIRST: 199 case NAME_FIRST:
72 first_ = value; 200 given_ = value;
73 break; 201 break;
74 202
75 case NAME_MIDDLE: 203 case NAME_MIDDLE:
76 case NAME_MIDDLE_INITIAL: 204 case NAME_MIDDLE_INITIAL:
77 middle_ = value; 205 middle_ = value;
78 break; 206 break;
79 207
80 case NAME_LAST: 208 case NAME_LAST:
81 last_ = value; 209 family_ = value;
82 break; 210 break;
83 211
84 case NAME_FULL: 212 case NAME_FULL:
213 // TODO(estade): this should just set |full_|; only SetInfo should attempt
214 // to be smart.
85 SetFullName(value); 215 SetFullName(value);
86 break; 216 break;
87 217
88 default: 218 default:
89 NOTREACHED(); 219 NOTREACHED();
90 } 220 }
91 } 221 }
92 222
93 base::string16 NameInfo::FullName() const { 223 base::string16 NameInfo::FullName() const {
224 if (!full_.empty())
225 return full_;
226
94 std::vector<base::string16> full_name; 227 std::vector<base::string16> full_name;
95 if (!first_.empty()) 228 if (!given_.empty())
96 full_name.push_back(first_); 229 full_name.push_back(given_);
97 230
98 if (!middle_.empty()) 231 if (!middle_.empty())
99 full_name.push_back(middle_); 232 full_name.push_back(middle_);
100 233
101 if (!last_.empty()) 234 if (!family_.empty())
102 full_name.push_back(last_); 235 full_name.push_back(family_);
103 236
104 return JoinString(full_name, ' '); 237 return JoinString(full_name, ' ');
105 } 238 }
106 239
107 base::string16 NameInfo::MiddleInitial() const { 240 base::string16 NameInfo::MiddleInitial() const {
108 if (middle_.empty()) 241 if (middle_.empty())
109 return base::string16(); 242 return base::string16();
110 243
111 base::string16 middle_name(middle()); 244 base::string16 middle_name(middle_);
112 base::string16 initial; 245 base::string16 initial;
113 initial.push_back(middle_name[0]); 246 initial.push_back(middle_name[0]);
114 return initial; 247 return initial;
115 } 248 }
116 249
117 void NameInfo::SetFullName(const base::string16& full) { 250 void NameInfo::SetFullName(const base::string16& full) {
118 // Clear the names. 251 // Hack: don't do anything if this wouldn't change the full, concatenated
119 first_ = base::string16(); 252 // name. Otherwise when unpickling data from the database, "First|Middle|"
120 middle_ = base::string16(); 253 // will get parsed as "First||Middle".
121 last_ = base::string16(); 254 // TODO(estade): we should be able to remove this when fixing the TODO in
255 // SetRawInfo.
256 if (FullName() == full)
Evan Stade 2014/06/06 23:10:44 NB
257 return;
122 258
123 std::vector<base::string16> full_name_tokens; 259 full_ = full;
124 Tokenize(full, base::ASCIIToUTF16(" "), &full_name_tokens);
125 260
126 // There are four possibilities: empty; first name; first and last names; 261 // If |full| is empty, leave the other name parts alone. This might occur
127 // first, middle (possibly multiple strings) and then the last name. 262 // due to a migrated database with an empty |full_name| value.
128 if (full_name_tokens.size() > 0) { 263 if (full.empty())
129 first_ = full_name_tokens[0]; 264 return;
130 if (full_name_tokens.size() > 1) { 265
131 last_ = full_name_tokens.back(); 266 NameParts parts = SplitName(full);
132 if (full_name_tokens.size() > 2) { 267 given_ = parts.given;
133 full_name_tokens.erase(full_name_tokens.begin()); 268 middle_ = parts.middle;
134 full_name_tokens.pop_back(); 269 family_ = parts.family;
135 middle_ = JoinString(full_name_tokens, ' ');
136 }
137 }
138 }
139 } 270 }
140 271
141 EmailInfo::EmailInfo() {} 272 EmailInfo::EmailInfo() {}
142 273
143 EmailInfo::EmailInfo(const EmailInfo& info) : FormGroup() { 274 EmailInfo::EmailInfo(const EmailInfo& info) : FormGroup() {
144 *this = info; 275 *this = info;
145 } 276 }
146 277
147 EmailInfo::~EmailInfo() {} 278 EmailInfo::~EmailInfo() {}
148 279
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 return base::string16(); 328 return base::string16();
198 } 329 }
199 330
200 void CompanyInfo::SetRawInfo(ServerFieldType type, 331 void CompanyInfo::SetRawInfo(ServerFieldType type,
201 const base::string16& value) { 332 const base::string16& value) {
202 DCHECK_EQ(COMPANY_NAME, type); 333 DCHECK_EQ(COMPANY_NAME, type);
203 company_name_ = value; 334 company_name_ = value;
204 } 335 }
205 336
206 } // namespace autofill 337 } // 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