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

Side by Side Diff: chrome/browser/autofill/autofill_profile.cc

Issue 6650014: Autofill extend profiles to include multi-valued fields, part 2. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 9 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 (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/autofill_profile.h" 5 #include "chrome/browser/autofill/autofill_profile.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <set> 8 #include <set>
9 9
10 #include "base/stl_util-inl.h" 10 #include "base/stl_util-inl.h"
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 break; 95 break;
96 } 96 }
97 } 97 }
98 } 98 }
99 } 99 }
100 100
101 } // namespace 101 } // namespace
102 102
103 AutoFillProfile::AutoFillProfile(const std::string& guid) 103 AutoFillProfile::AutoFillProfile(const std::string& guid)
104 : guid_(guid) { 104 : guid_(guid) {
105 InitPersonalInfo(&personal_info_);
106 } 105 }
107 106
108 AutoFillProfile::AutoFillProfile() 107 AutoFillProfile::AutoFillProfile()
109 : guid_(guid::GenerateGUID()) { 108 : guid_(guid::GenerateGUID()) {
110 InitPersonalInfo(&personal_info_);
111 } 109 }
112 110
113 AutoFillProfile::AutoFillProfile(const AutoFillProfile& source) 111 AutoFillProfile::AutoFillProfile(const AutoFillProfile& source)
114 : FormGroup() { 112 : FormGroup() {
Ilya Sherman 2011/03/09 00:50:50 Should we include this in the initialization lists
dhollowa 2011/03/09 01:55:47 It is only required for copy constructors, it is i
115 operator=(source); 113 operator=(source);
116 } 114 }
117 115
118 AutoFillProfile::~AutoFillProfile() { 116 AutoFillProfile::~AutoFillProfile() {
119 STLDeleteContainerPairSecondPointers(personal_info_.begin(), 117 }
120 personal_info_.end()); 118
119 AutoFillProfile& AutoFillProfile::operator=(const AutoFillProfile& profile) {
120 if (this == &profile)
121 return *this;
122
123 label_ = profile.label_;
124 guid_ = profile.guid_;
125
126 name_ = profile.name_;
127 email_ = profile.email_;
128 company_ = profile.company_;
129 home_number_ = profile.home_number_;
130 fax_number_ = profile.fax_number_;
131 address_ = profile.address_;
132
133 return *this;
121 } 134 }
122 135
123 void AutoFillProfile::GetPossibleFieldTypes( 136 void AutoFillProfile::GetPossibleFieldTypes(
124 const string16& text, 137 const string16& text,
125 FieldTypeSet* possible_types) const { 138 FieldTypeSet* possible_types) const {
126 for (FormGroupMap::const_iterator iter = personal_info_.begin(); 139 FormGroupList info = info_list();
127 iter != personal_info_.end(); ++iter) { 140 for (FormGroupList::const_iterator it = info.begin(); it != info.end(); ++it)
128 FormGroup* data = iter->second; 141 (*it)->GetPossibleFieldTypes(text, possible_types);
129 DCHECK(data != NULL);
130 data->GetPossibleFieldTypes(text, possible_types);
131 }
132 } 142 }
133 143
134 void AutoFillProfile::GetAvailableFieldTypes( 144 void AutoFillProfile::GetAvailableFieldTypes(
135 FieldTypeSet* available_types) const { 145 FieldTypeSet* available_types) const {
136 for (FormGroupMap::const_iterator iter = personal_info_.begin(); 146 FormGroupList info = info_list();
137 iter != personal_info_.end(); ++iter) { 147 for (FormGroupList::const_iterator it = info.begin(); it != info.end(); ++it)
138 FormGroup* data = iter->second; 148 (*it)->GetAvailableFieldTypes(available_types);
139 DCHECK(data != NULL);
140 data->GetAvailableFieldTypes(available_types);
141 }
142 } 149 }
143 150
144 string16 AutoFillProfile::GetFieldText(const AutofillType& type) const { 151 string16 AutoFillProfile::GetFieldText(const AutofillType& type) const {
145 AutofillType return_type( 152 AutofillType return_type(
146 AutofillType::GetEquivalentFieldType(type.field_type())); 153 AutofillType::GetEquivalentFieldType(type.field_type()));
147 154
148 FormGroupMap::const_iterator iter = personal_info_.find(return_type.group()); 155 FormGroupMap info = info_map();
Ilya Sherman 2011/03/09 00:50:50 How about creating a wrapper around find() rather
dhollowa 2011/03/09 01:55:47 No, the association between key/value is set up by
Ilya Sherman 2011/03/09 02:01:22 What I meant was: We pretty much always want info_
149 if (iter == personal_info_.end() || iter->second == NULL) 156 FormGroupMap::const_iterator it = info.find(return_type.group());
157 if (it == info.end())
150 return string16(); 158 return string16();
151 159
152 return iter->second->GetFieldText(return_type); 160 return it->second->GetFieldText(return_type);
153 } 161 }
154 162
155 void AutoFillProfile::FindInfoMatches( 163 void AutoFillProfile::FindInfoMatches(
156 const AutofillType& type, 164 const AutofillType& type,
157 const string16& info, 165 const string16& input,
158 std::vector<string16>* matched_text) const { 166 std::vector<string16>* matched_text) const {
159 if (matched_text == NULL) { 167 if (matched_text == NULL) {
160 DLOG(ERROR) << "NULL matched text passed in"; 168 DLOG(ERROR) << "NULL matched text passed in";
161 return; 169 return;
162 } 170 }
163 171
164 string16 clean_info = StringToLowerASCII(CollapseWhitespace(info, false)); 172 string16 clean_info = StringToLowerASCII(CollapseWhitespace(input, false));
165 173
166 // If the field_type is unknown, then match against all field types. 174 // If the field_type is unknown, then match against all field types.
167 if (type.field_type() == UNKNOWN_TYPE) { 175 if (type.field_type() == UNKNOWN_TYPE) {
168 FormGroupMap::const_iterator iter; 176 FormGroupList info = info_list();
169 for (iter = personal_info_.begin(); iter != personal_info_.end(); ++iter) { 177 for (FormGroupList::const_iterator it = info.begin();
170 iter->second->FindInfoMatches(type, clean_info, matched_text); 178 it != info.end(); ++it)
171 } 179 (*it)->FindInfoMatches(type, clean_info, matched_text);
172 } else { 180 } else {
173 FormGroupMap::const_iterator iter = personal_info_.find(type.group()); 181 FormGroupMap info = info_map();
174 DCHECK(iter != personal_info_.end() && iter->second != NULL); 182 FormGroupMap::const_iterator it = info.find(type.group());
175 if (iter != personal_info_.end() && iter->second != NULL) 183 DCHECK(it != info.end());
176 iter->second->FindInfoMatches(type, clean_info, matched_text); 184 it->second->FindInfoMatches(type, clean_info, matched_text);
177 } 185 }
178 } 186 }
179 187
180 void AutoFillProfile::SetInfo(const AutofillType& type, const string16& value) { 188 void AutoFillProfile::SetInfo(const AutofillType& type, const string16& value) {
181 FormGroupMap::const_iterator iter = personal_info_.find(type.group()); 189 MutableFormGroupMap info = mutable_info_map();
182 if (iter == personal_info_.end() || iter->second == NULL) 190 MutableFormGroupMap::iterator it = info.find(type.group());
183 return; 191 DCHECK(it != info.end());
184 192 it->second->SetInfo(type, CollapseWhitespace(value, false));
185 iter->second->SetInfo(type, CollapseWhitespace(value, false));
186 }
187
188 FormGroup* AutoFillProfile::Clone() const {
189 return new AutoFillProfile(*this);
190 } 193 }
191 194
192 const string16 AutoFillProfile::Label() const { 195 const string16 AutoFillProfile::Label() const {
193 return label_; 196 return label_;
194 } 197 }
195 198
196 const std::string AutoFillProfile::CountryCode() const { 199 const std::string AutoFillProfile::CountryCode() const {
197 FormGroup* form_group = 200 return address_.country_code();
198 personal_info_.find(AutofillType::ADDRESS_HOME)->second;
199 DCHECK(form_group);
200 Address* address = static_cast<Address*>(form_group);
201 return address->country_code();
202 } 201 }
203 202
204 void AutoFillProfile::SetCountryCode(const std::string& country_code) { 203 void AutoFillProfile::SetCountryCode(const std::string& country_code) {
205 FormGroup* form_group = 204 address_.set_country_code(country_code);
206 personal_info_.find(AutofillType::ADDRESS_HOME)->second;
207 DCHECK(form_group);
208 Address* address = static_cast<Address*>(form_group);
209 address->set_country_code(country_code);
210 } 205 }
211 206
212 // static 207 // static
213 bool AutoFillProfile::AdjustInferredLabels( 208 bool AutoFillProfile::AdjustInferredLabels(
214 std::vector<AutoFillProfile*>* profiles) { 209 std::vector<AutoFillProfile*>* profiles) {
215 const size_t kMinimalFieldsShown = 2; 210 const size_t kMinimalFieldsShown = 2;
216 211
217 std::vector<string16> created_labels; 212 std::vector<string16> created_labels;
218 CreateInferredLabels(profiles, NULL, UNKNOWN_TYPE, kMinimalFieldsShown, 213 CreateInferredLabels(profiles, NULL, UNKNOWN_TYPE, kMinimalFieldsShown,
219 &created_labels); 214 &created_labels);
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
271 } 266 }
272 } 267 }
273 } 268 }
274 269
275 bool AutoFillProfile::IsEmpty() const { 270 bool AutoFillProfile::IsEmpty() const {
276 FieldTypeSet types; 271 FieldTypeSet types;
277 GetAvailableFieldTypes(&types); 272 GetAvailableFieldTypes(&types);
278 return types.empty(); 273 return types.empty();
279 } 274 }
280 275
281 void AutoFillProfile::operator=(const AutoFillProfile& source) {
282 if (this == &source)
283 return;
284
285 label_ = source.label_;
286 guid_ = source.guid_;
287
288 STLDeleteContainerPairSecondPointers(personal_info_.begin(),
289 personal_info_.end());
290 personal_info_.clear();
291
292 FormGroupMap::const_iterator iter;
293 for (iter = source.personal_info_.begin();
294 iter != source.personal_info_.end();
295 ++iter) {
296 personal_info_[iter->first] = iter->second->Clone();
297 }
298 }
299
300 int AutoFillProfile::Compare(const AutoFillProfile& profile) const { 276 int AutoFillProfile::Compare(const AutoFillProfile& profile) const {
301 // The following AutoFill field types are the only types we store in the WebDB 277 // The following AutoFill field types are the only types we store in the WebDB
302 // so far, so we're only concerned with matching these types in the profile. 278 // so far, so we're only concerned with matching these types in the profile.
303 const AutofillFieldType types[] = { NAME_FIRST, 279 const AutofillFieldType types[] = { NAME_FIRST,
304 NAME_MIDDLE, 280 NAME_MIDDLE,
305 NAME_LAST, 281 NAME_LAST,
306 EMAIL_ADDRESS, 282 EMAIL_ADDRESS,
307 COMPANY_NAME, 283 COMPANY_NAME,
308 ADDRESS_HOME_LINE1, 284 ADDRESS_HOME_LINE1,
309 ADDRESS_HOME_LINE2, 285 ADDRESS_HOME_LINE2,
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
439 label_fields.size() >= num_fields_to_include) 415 label_fields.size() >= num_fields_to_include)
440 break; 416 break;
441 } 417 }
442 418
443 (*created_labels)[*it] = 419 (*created_labels)[*it] =
444 profile->ConstructInferredLabel(label_fields, 420 profile->ConstructInferredLabel(label_fields,
445 label_fields.size()); 421 label_fields.size());
446 } 422 }
447 } 423 }
448 424
449 // static 425 AutoFillProfile::FormGroupList AutoFillProfile::info_list() const {
450 void AutoFillProfile::InitPersonalInfo(FormGroupMap* personal_info) { 426 FormGroupList v(6);
451 (*personal_info)[AutofillType::CONTACT_INFO] = new ContactInfo(); 427 v[0] = &name_;
452 (*personal_info)[AutofillType::PHONE_HOME] = new HomePhoneNumber(); 428 v[1] = &email_;
453 (*personal_info)[AutofillType::PHONE_FAX] = new FaxNumber(); 429 v[2] = &company_;
454 (*personal_info)[AutofillType::ADDRESS_HOME] = new Address(); 430 v[3] = &home_number_;
431 v[4] = &fax_number_;
432 v[5] = &address_;
433 return v;
434 }
Ilya Sherman 2011/03/09 00:50:50 How about making this a constant array data member
dhollowa 2011/03/09 01:55:47 No, I want it like this because I'm planning to ex
435
436 AutoFillProfile::FormGroupMap AutoFillProfile::info_map() const {
437 FormGroupMap m;
438 m[AutofillType::NAME] = &name_;
439 m[AutofillType::EMAIL] = &email_;
440 m[AutofillType::COMPANY] = &company_;
441 m[AutofillType::PHONE_HOME] = &home_number_;
442 m[AutofillType::PHONE_FAX] = &fax_number_;
443 m[AutofillType::ADDRESS_HOME] = &address_;
444 return m;
445 }
446
447 AutoFillProfile::MutableFormGroupMap AutoFillProfile::mutable_info_map() {
448 FormGroupMap m_const = info_map();
449 MutableFormGroupMap m;
450 for (FormGroupMap::const_iterator it = m_const.begin();
451 it != m_const.end(); ++it) {
452 m[it->first] = const_cast<FormGroup*>(it->second);
453 }
454 return m;
455 } 455 }
456 456
457 // So we can compare AutoFillProfiles with EXPECT_EQ(). 457 // So we can compare AutoFillProfiles with EXPECT_EQ().
458 std::ostream& operator<<(std::ostream& os, const AutoFillProfile& profile) { 458 std::ostream& operator<<(std::ostream& os, const AutoFillProfile& profile) {
459 return os 459 return os
460 << UTF16ToUTF8(profile.Label()) 460 << UTF16ToUTF8(profile.Label())
461 << " " 461 << " "
462 << profile.guid() 462 << profile.guid()
463 << " " 463 << " "
464 << UTF16ToUTF8(profile.GetFieldText(AutofillType(NAME_FIRST))) 464 << UTF16ToUTF8(profile.GetFieldText(AutofillType(NAME_FIRST)))
(...skipping 17 matching lines...) Expand all
482 << UTF16ToUTF8(profile.GetFieldText(AutofillType(ADDRESS_HOME_ZIP))) 482 << UTF16ToUTF8(profile.GetFieldText(AutofillType(ADDRESS_HOME_ZIP)))
483 << " " 483 << " "
484 << UTF16ToUTF8(profile.GetFieldText(AutofillType(ADDRESS_HOME_COUNTRY))) 484 << UTF16ToUTF8(profile.GetFieldText(AutofillType(ADDRESS_HOME_COUNTRY)))
485 << " " 485 << " "
486 << UTF16ToUTF8(profile.GetFieldText(AutofillType( 486 << UTF16ToUTF8(profile.GetFieldText(AutofillType(
487 PHONE_HOME_WHOLE_NUMBER))) 487 PHONE_HOME_WHOLE_NUMBER)))
488 << " " 488 << " "
489 << UTF16ToUTF8(profile.GetFieldText(AutofillType( 489 << UTF16ToUTF8(profile.GetFieldText(AutofillType(
490 PHONE_FAX_WHOLE_NUMBER))); 490 PHONE_FAX_WHOLE_NUMBER)));
491 } 491 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698