1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/autofill/autofill_profile.h"
#include <algorithm>
#include <list>
#include <map>
#include <set>
#include <vector>
#include "app/l10n_util.h"
#include "base/stl_util-inl.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/autofill/address.h"
#include "chrome/browser/autofill/autofill_manager.h"
#include "chrome/browser/autofill/contact_info.h"
#include "chrome/browser/autofill/fax_number.h"
#include "chrome/browser/autofill/home_address.h"
#include "chrome/browser/autofill/home_phone_number.h"
#include "chrome/browser/guid.h"
#ifndef ANDROID
#include "grit/generated_resources.h"
#endif
namespace {
void InitPersonalInfo(FormGroupMap* personal_info) {
(*personal_info)[AutoFillType::CONTACT_INFO] = new ContactInfo();
(*personal_info)[AutoFillType::PHONE_HOME] = new HomePhoneNumber();
(*personal_info)[AutoFillType::PHONE_FAX] = new FaxNumber();
(*personal_info)[AutoFillType::ADDRESS_HOME] = new HomeAddress();
}
} // namespace
AutoFillProfile::AutoFillProfile(const std::string& guid)
: guid_(guid) {
InitPersonalInfo(&personal_info_);
}
AutoFillProfile::AutoFillProfile()
: guid_(guid::GenerateGUID()) {
InitPersonalInfo(&personal_info_);
}
AutoFillProfile::AutoFillProfile(const AutoFillProfile& source)
: FormGroup() {
operator=(source);
}
AutoFillProfile::~AutoFillProfile() {
STLDeleteContainerPairSecondPointers(personal_info_.begin(),
personal_info_.end());
}
void AutoFillProfile::GetPossibleFieldTypes(
const string16& text,
FieldTypeSet* possible_types) const {
for (FormGroupMap::const_iterator iter = personal_info_.begin();
iter != personal_info_.end(); ++iter) {
FormGroup* data = iter->second;
DCHECK(data != NULL);
data->GetPossibleFieldTypes(text, possible_types);
}
}
void AutoFillProfile::GetAvailableFieldTypes(
FieldTypeSet* available_types) const {
for (FormGroupMap::const_iterator iter = personal_info_.begin();
iter != personal_info_.end(); ++iter) {
FormGroup* data = iter->second;
DCHECK(data != NULL);
data->GetAvailableFieldTypes(available_types);
}
}
string16 AutoFillProfile::GetFieldText(const AutoFillType& type) const {
AutoFillType return_type = type;
// When billing information is requested from the profile we map to the
// home address equivalents. This indicates the address information within
// this profile is being used to fill billing fields in the form.
switch (type.field_type()) {
case ADDRESS_BILLING_LINE1:
return_type = AutoFillType(ADDRESS_HOME_LINE1);
break;
case ADDRESS_BILLING_LINE2:
return_type = AutoFillType(ADDRESS_HOME_LINE2);
break;
case ADDRESS_BILLING_APT_NUM:
return_type = AutoFillType(ADDRESS_HOME_APT_NUM);
break;
case ADDRESS_BILLING_CITY:
return_type = AutoFillType(ADDRESS_HOME_CITY);
break;
case ADDRESS_BILLING_STATE:
return_type = AutoFillType(ADDRESS_HOME_STATE);
break;
case ADDRESS_BILLING_ZIP:
return_type = AutoFillType(ADDRESS_HOME_ZIP);
break;
case ADDRESS_BILLING_COUNTRY:
return_type = AutoFillType(ADDRESS_HOME_COUNTRY);
break;
default:
break;
}
FormGroupMap::const_iterator iter = personal_info_.find(return_type.group());
if (iter == personal_info_.end() || iter->second == NULL)
return string16();
return iter->second->GetFieldText(return_type);
}
void AutoFillProfile::FindInfoMatches(
const AutoFillType& type,
const string16& info,
std::vector<string16>* matched_text) const {
if (matched_text == NULL) {
DLOG(ERROR) << "NULL matched text passed in";
return;
}
string16 clean_info = StringToLowerASCII(CollapseWhitespace(info, false));
// If the field_type is unknown, then match against all field types.
if (type.field_type() == UNKNOWN_TYPE) {
FormGroupMap::const_iterator iter;
for (iter = personal_info_.begin(); iter != personal_info_.end(); ++iter) {
iter->second->FindInfoMatches(type, clean_info, matched_text);
}
} else {
FormGroupMap::const_iterator iter = personal_info_.find(type.group());
DCHECK(iter != personal_info_.end() && iter->second != NULL);
if (iter != personal_info_.end() && iter->second != NULL)
iter->second->FindInfoMatches(type, clean_info, matched_text);
}
}
void AutoFillProfile::SetInfo(const AutoFillType& type, const string16& value) {
FormGroupMap::const_iterator iter = personal_info_.find(type.group());
if (iter == personal_info_.end() || iter->second == NULL)
return;
iter->second->SetInfo(type, CollapseWhitespace(value, false));
}
FormGroup* AutoFillProfile::Clone() const {
AutoFillProfile* profile = new AutoFillProfile();
profile->label_ = label_;
profile->guid_ = guid();
FormGroupMap::const_iterator iter;
for (iter = personal_info_.begin(); iter != personal_info_.end(); ++iter) {
if (profile->personal_info_.count(iter->first))
delete profile->personal_info_[iter->first];
profile->personal_info_[iter->first] = iter->second->Clone();
}
return profile;
}
const string16 AutoFillProfile::Label() const {
return label_;
}
// static
bool AutoFillProfile::AdjustInferredLabels(
std::vector<AutoFillProfile*>* profiles) {
std::vector<string16> created_labels;
const size_t kMinimalFieldsShown = 2;
CreateInferredLabels(profiles, &created_labels, kMinimalFieldsShown,
UNKNOWN_TYPE, NULL);
DCHECK(profiles->size() == created_labels.size());
bool updated_labels = false;
for (size_t i = 0; i < profiles->size(); ++i) {
if (profiles->at(i)->Label() != created_labels[i]) {
updated_labels = true;
profiles->at(i)->set_label(created_labels[i]);
}
}
return updated_labels;
}
// static
void AutoFillProfile::CreateInferredLabels(
const std::vector<AutoFillProfile*>* profiles,
std::vector<string16>* created_labels,
size_t minimal_fields_shown,
AutoFillFieldType exclude_field,
const std::vector<AutoFillFieldType>* suggested_fields) {
// |suggested_fields| may be NULL.
// The following fields are use to distinguish between two profiles in the
// order of importance, e. g. if both EMAIL_ADDRESS and COMPANY_NAME are
// different, EMAIL_ADDRESS will be used to distinguish them.
const AutoFillFieldType distinguishing_fields[] = {
// First non empty data are always present in the label, even if it matches.
NAME_FULL,
ADDRESS_HOME_LINE1,
ADDRESS_HOME_CITY,
ADDRESS_HOME_STATE,
ADDRESS_HOME_ZIP,
ADDRESS_HOME_COUNTRY,
EMAIL_ADDRESS,
PHONE_HOME_WHOLE_NUMBER,
PHONE_FAX_WHOLE_NUMBER,
COMPANY_NAME,
};
DCHECK(profiles);
DCHECK(created_labels);
std::vector<AutoFillFieldType> fields_to_use;
if (suggested_fields) {
// Limiting the number of possible fields simplifies the following code,
// and 10 fields more than enough to create clear label.
fields_to_use.reserve(std::min(suggested_fields->size(),
arraysize(distinguishing_fields)));
bool name_selected = false;
for (size_t i = 0; i < suggested_fields->size() &&
fields_to_use.size() < arraysize(distinguishing_fields); ++i) {
if (suggested_fields->at(i) == NAME_FIRST ||
suggested_fields->at(i) == NAME_LAST ||
suggested_fields->at(i) == NAME_MIDDLE ||
suggested_fields->at(i) == NAME_FULL) {
if (name_selected)
continue;
name_selected = true;
fields_to_use.push_back(NAME_FULL);
} else {
fields_to_use.push_back(suggested_fields->at(i));
}
}
} else {
fields_to_use.resize(arraysize(distinguishing_fields));
for (size_t i = 0; i < arraysize(distinguishing_fields); ++i) {
fields_to_use[i] = distinguishing_fields[i];
}
}
if (exclude_field == NAME_FIRST || exclude_field == NAME_LAST ||
exclude_field == NAME_MIDDLE)
exclude_field = NAME_FULL;
created_labels->resize(profiles->size());
std::map<string16, std::list<size_t> > labels;
for (size_t it = 0; it < profiles->size(); ++it) {
labels[profiles->at(it)->GetFieldText(
AutoFillType(fields_to_use.size() ? fields_to_use[0] :
NAME_FULL))].push_back(it);
}
std::map<string16, std::list<size_t> >::iterator label_iterator;
for (label_iterator = labels.begin(); label_iterator != labels.end();
++label_iterator) {
if (label_iterator->second.size() > 1) {
// We have more than one item with the same preview, add differentiating
// data.
std::list<size_t>::iterator similar_profiles;
DCHECK(fields_to_use.size() <= arraysize(distinguishing_fields));
std::map<string16, int> tested_fields[arraysize(distinguishing_fields)];
for (similar_profiles = label_iterator->second.begin();
similar_profiles != label_iterator->second.end();
++similar_profiles) {
for (size_t i = 0; i < fields_to_use.size(); ++i) {
string16 key = profiles->at(*similar_profiles)->GetFieldText(
AutoFillType(fields_to_use[i]));
std::map<string16, int>::iterator tested_field =
tested_fields[i].find(key);
if (tested_field == tested_fields[i].end())
(tested_fields[i])[key] = 1;
else
++(tested_field->second);
}
}
std::vector<AutoFillFieldType> fields;
std::vector<AutoFillFieldType> first_non_empty_fields;
size_t added_fields = 0;
bool matched_necessary = false;
// Leave it as a candidate if it is not the same for everybody.
for (size_t i = 0; i < fields_to_use.size(); ++i) {
if (tested_fields[i].size() == label_iterator->second.size()) {
// This field is different for everybody.
if (!matched_necessary) {
matched_necessary = true;
fields.clear();
added_fields = 1;
if (first_non_empty_fields.size()) {
added_fields += first_non_empty_fields.size();
fields.resize(added_fields - 1);
std::copy(first_non_empty_fields.begin(),
first_non_empty_fields.end(),
fields.begin());
}
} else {
++added_fields;
}
fields.push_back(fields_to_use[i]);
if (added_fields >= minimal_fields_shown)
break;
} else if (tested_fields[i].size() != 1) {
// this field is different for some.
if (added_fields < minimal_fields_shown) {
first_non_empty_fields.push_back(fields_to_use[i]);
++added_fields;
if (added_fields == minimal_fields_shown && matched_necessary)
break;
}
fields.push_back(fields_to_use[i]);
} else if (added_fields < minimal_fields_shown &&
exclude_field != fields_to_use[i] &&
!label_iterator->first.empty()) {
fields.push_back(fields_to_use[i]);
first_non_empty_fields.push_back(fields_to_use[i]);
++added_fields;
if (added_fields == minimal_fields_shown && matched_necessary)
break;
}
}
// Update labels if needed.
for (similar_profiles = label_iterator->second.begin();
similar_profiles != label_iterator->second.end();
++similar_profiles) {
size_t field_it = 0;
for (size_t field_id = 0;
field_id < fields_to_use.size() &&
field_it < fields.size(); ++field_id) {
if (fields[field_it] == fields_to_use[field_id]) {
if ((tested_fields[field_id])[
profiles->at(*similar_profiles)->GetFieldText(
AutoFillType(fields_to_use[field_id]))] == 1) {
// this field is unique among the subset.
break;
}
++field_it;
}
}
string16 new_label;
if (field_it < fields.size() && fields.size() > minimal_fields_shown) {
std::vector<AutoFillFieldType> unique_fields;
unique_fields.resize(fields.size());
std::copy(fields.begin(), fields.end(), unique_fields.begin());
unique_fields.resize(std::max(field_it + 1, minimal_fields_shown));
new_label =
profiles->at(*similar_profiles)->ConstructInferredLabel(
&unique_fields);
} else {
new_label =
profiles->at(*similar_profiles)->ConstructInferredLabel(&fields);
}
(*created_labels)[*similar_profiles] = new_label;
}
} else {
std::vector<AutoFillFieldType> non_empty_fields;
size_t include_fields = minimal_fields_shown ? minimal_fields_shown : 1;
non_empty_fields.reserve(include_fields);
for (size_t i = 0; i < fields_to_use.size(); ++i) {
if (exclude_field == fields_to_use[i])
continue;
if (!profiles->at(label_iterator->second.front())->GetFieldText(
AutoFillType(fields_to_use[i])).empty()) {
non_empty_fields.push_back(fields_to_use[i]);
if (non_empty_fields.size() >= include_fields)
break;
}
}
(*created_labels)[label_iterator->second.front()] =
profiles->at(label_iterator->second.front())->ConstructInferredLabel(
&non_empty_fields);
}
}
}
bool AutoFillProfile::IsEmpty() const {
FieldTypeSet types;
GetAvailableFieldTypes(&types);
return types.empty();
}
void AutoFillProfile::operator=(const AutoFillProfile& source) {
label_ = source.label_;
guid_ = source.guid_;
STLDeleteContainerPairSecondPointers(personal_info_.begin(),
personal_info_.end());
personal_info_.clear();
FormGroupMap::const_iterator iter;
for (iter = source.personal_info_.begin();
iter != source.personal_info_.end();
++iter) {
personal_info_[iter->first] = iter->second->Clone();
}
}
int AutoFillProfile::Compare(const AutoFillProfile& profile) const {
// The following AutoFill field types are the only types we store in the WebDB
// so far, so we're only concerned with matching these types in the profile.
const AutoFillFieldType types[] = { NAME_FIRST,
NAME_MIDDLE,
NAME_LAST,
EMAIL_ADDRESS,
COMPANY_NAME,
ADDRESS_HOME_LINE1,
ADDRESS_HOME_LINE2,
ADDRESS_HOME_CITY,
ADDRESS_HOME_STATE,
ADDRESS_HOME_ZIP,
ADDRESS_HOME_COUNTRY,
PHONE_HOME_NUMBER,
PHONE_FAX_NUMBER };
for (size_t index = 0; index < arraysize(types); ++index) {
int comparison = GetFieldText(AutoFillType(types[index])).compare(
profile.GetFieldText(AutoFillType(types[index])));
if (comparison != 0)
return comparison;
}
return 0;
}
bool AutoFillProfile::operator==(const AutoFillProfile& profile) const {
if (label_ != profile.label_ || guid_ != profile.guid_)
return false;
return Compare(profile) == 0;
}
bool AutoFillProfile::operator!=(const AutoFillProfile& profile) const {
return !operator==(profile);
}
Address* AutoFillProfile::GetHomeAddress() {
return static_cast<Address*>(personal_info_[AutoFillType::ADDRESS_HOME]);
}
string16 AutoFillProfile::ConstructInferredLabel(
const std::vector<AutoFillFieldType>* included_fields) const {
DCHECK(included_fields);
string16 label;
#ifdef ANDROID
// TODO: Hook up l10n on Android.
string16 separator;
#else
string16 separator = l10n_util::GetStringUTF16(
IDS_AUTOFILL_DIALOG_ADDRESS_SUMMARY_SEPARATOR);
#endif
for (std::vector<AutoFillFieldType>::const_iterator it =
included_fields->begin(); it != included_fields->end(); ++it) {
string16 field = GetFieldText(AutoFillType(*it));
if (!field.empty()) {
if (!label.empty()) {
label.append(separator);
}
#ifdef ANDROID
// TODO: Hook up l10n on Android.
#else
// Fax number has special format, to indicate that this is a fax number.
if (*it == PHONE_FAX_WHOLE_NUMBER) {
field = l10n_util::GetStringFUTF16(
IDS_AUTOFILL_DIALOG_ADDRESS_SUMMARY_FAX_FORMAT, field);
}
#endif
label.append(field);
}
}
return label;
}
// So we can compare AutoFillProfiles with EXPECT_EQ().
std::ostream& operator<<(std::ostream& os, const AutoFillProfile& profile) {
return os
<< UTF16ToUTF8(profile.Label())
<< " "
<< profile.guid()
<< " "
<< UTF16ToUTF8(profile.GetFieldText(AutoFillType(NAME_FIRST)))
<< " "
<< UTF16ToUTF8(profile.GetFieldText(AutoFillType(NAME_MIDDLE)))
<< " "
<< UTF16ToUTF8(profile.GetFieldText(AutoFillType(NAME_LAST)))
<< " "
<< UTF16ToUTF8(profile.GetFieldText(AutoFillType(EMAIL_ADDRESS)))
<< " "
<< UTF16ToUTF8(profile.GetFieldText(AutoFillType(COMPANY_NAME)))
<< " "
<< UTF16ToUTF8(profile.GetFieldText(AutoFillType(ADDRESS_HOME_LINE1)))
<< " "
<< UTF16ToUTF8(profile.GetFieldText(AutoFillType(ADDRESS_HOME_LINE2)))
<< " "
<< UTF16ToUTF8(profile.GetFieldText(AutoFillType(ADDRESS_HOME_CITY)))
<< " "
<< UTF16ToUTF8(profile.GetFieldText(AutoFillType(ADDRESS_HOME_STATE)))
<< " "
<< UTF16ToUTF8(profile.GetFieldText(AutoFillType(ADDRESS_HOME_ZIP)))
<< " "
<< UTF16ToUTF8(profile.GetFieldText(AutoFillType(ADDRESS_HOME_COUNTRY)))
<< " "
<< UTF16ToUTF8(profile.GetFieldText(AutoFillType(
PHONE_HOME_WHOLE_NUMBER)))
<< " "
<< UTF16ToUTF8(profile.GetFieldText(AutoFillType(
PHONE_FAX_WHOLE_NUMBER)));
}
|