OLD | NEW |
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/autofill_profile.h" | 5 #include "components/autofill/core/browser/autofill_profile.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <functional> | 8 #include <functional> |
9 #include <map> | 9 #include <map> |
10 #include <ostream> | 10 #include <ostream> |
(...skipping 12 matching lines...) Expand all Loading... |
23 #include "components/autofill/core/browser/phone_number.h" | 23 #include "components/autofill/core/browser/phone_number.h" |
24 #include "components/autofill/core/browser/phone_number_i18n.h" | 24 #include "components/autofill/core/browser/phone_number_i18n.h" |
25 #include "components/autofill/core/browser/validation.h" | 25 #include "components/autofill/core/browser/validation.h" |
26 #include "components/autofill/core/common/form_field_data.h" | 26 #include "components/autofill/core/common/form_field_data.h" |
27 #include "grit/component_strings.h" | 27 #include "grit/component_strings.h" |
28 #include "ui/base/l10n/l10n_util.h" | 28 #include "ui/base/l10n/l10n_util.h" |
29 | 29 |
30 namespace autofill { | 30 namespace autofill { |
31 namespace { | 31 namespace { |
32 | 32 |
33 // Like |AutofillType::GetEquivalentFieldType()|, but also returns |NAME_FULL| | 33 // Like |AutofillType::GetStorableType()|, but also returns |NAME_FULL| for |
34 // for first, middle, and last name field types. | 34 // first, middle, and last name field types. |
35 ServerFieldType GetEquivalentFieldTypeCollapsingNames(ServerFieldType type) { | 35 ServerFieldType GetStorableTypeCollapsingNames(ServerFieldType type) { |
36 if (type == NAME_FIRST || type == NAME_MIDDLE || type == NAME_LAST || | 36 ServerFieldType storable_type = AutofillType(type).GetStorableType(); |
37 type == NAME_MIDDLE_INITIAL) | 37 if (AutofillType(storable_type).group() == NAME) |
38 return NAME_FULL; | 38 return NAME_FULL; |
39 | 39 |
40 return AutofillType::GetEquivalentFieldType(type); | 40 return storable_type; |
41 } | 41 } |
42 | 42 |
43 // Fills |distinguishing_fields| with a list of fields to use when creating | 43 // Fills |distinguishing_fields| with a list of fields to use when creating |
44 // labels that can help to distinguish between two profiles. Draws fields from | 44 // labels that can help to distinguish between two profiles. Draws fields from |
45 // |suggested_fields| if it is non-NULL; otherwise returns a default list. | 45 // |suggested_fields| if it is non-NULL; otherwise returns a default list. |
46 // If |suggested_fields| is non-NULL, does not include |excluded_field| in the | 46 // If |suggested_fields| is non-NULL, does not include |excluded_field| in the |
47 // list. Otherwise, |excluded_field| is ignored, and should be set to | 47 // list. Otherwise, |excluded_field| is ignored, and should be set to |
48 // |UNKNOWN_TYPE| by convention. The resulting list of fields is sorted in | 48 // |UNKNOWN_TYPE| by convention. The resulting list of fields is sorted in |
49 // decreasing order of importance. | 49 // decreasing order of importance. |
50 void GetFieldsForDistinguishingProfiles( | 50 void GetFieldsForDistinguishingProfiles( |
(...skipping 18 matching lines...) Expand all Loading... |
69 distinguishing_fields->assign( | 69 distinguishing_fields->assign( |
70 kDefaultDistinguishingFields, | 70 kDefaultDistinguishingFields, |
71 kDefaultDistinguishingFields + arraysize(kDefaultDistinguishingFields)); | 71 kDefaultDistinguishingFields + arraysize(kDefaultDistinguishingFields)); |
72 return; | 72 return; |
73 } | 73 } |
74 | 74 |
75 // Keep track of which fields we've seen so that we avoid duplicate entries. | 75 // Keep track of which fields we've seen so that we avoid duplicate entries. |
76 // Always ignore fields of unknown type and the excluded field. | 76 // Always ignore fields of unknown type and the excluded field. |
77 std::set<ServerFieldType> seen_fields; | 77 std::set<ServerFieldType> seen_fields; |
78 seen_fields.insert(UNKNOWN_TYPE); | 78 seen_fields.insert(UNKNOWN_TYPE); |
79 seen_fields.insert(GetEquivalentFieldTypeCollapsingNames(excluded_field)); | 79 seen_fields.insert(GetStorableTypeCollapsingNames(excluded_field)); |
80 | 80 |
81 distinguishing_fields->clear(); | 81 distinguishing_fields->clear(); |
82 for (std::vector<ServerFieldType>::const_iterator it = | 82 for (std::vector<ServerFieldType>::const_iterator it = |
83 suggested_fields->begin(); | 83 suggested_fields->begin(); |
84 it != suggested_fields->end(); ++it) { | 84 it != suggested_fields->end(); ++it) { |
85 ServerFieldType suggested_type = GetEquivalentFieldTypeCollapsingNames(*it); | 85 ServerFieldType suggested_type = GetStorableTypeCollapsingNames(*it); |
86 if (seen_fields.insert(suggested_type).second) | 86 if (seen_fields.insert(suggested_type).second) |
87 distinguishing_fields->push_back(suggested_type); | 87 distinguishing_fields->push_back(suggested_type); |
88 } | 88 } |
89 | 89 |
90 // Special case: If the excluded field is a partial name (e.g. first name) and | 90 // Special case: If the excluded field is a partial name (e.g. first name) and |
91 // the suggested fields include other name fields, include |NAME_FULL| in the | 91 // the suggested fields include other name fields, include |NAME_FULL| in the |
92 // list of distinguishing fields as a last-ditch fallback. This allows us to | 92 // list of distinguishing fields as a last-ditch fallback. This allows us to |
93 // distinguish between profiles that are identical except for the name. | 93 // distinguish between profiles that are identical except for the name. |
94 if (excluded_field != NAME_FULL && | 94 if (excluded_field != NAME_FULL && |
95 GetEquivalentFieldTypeCollapsingNames(excluded_field) == NAME_FULL) { | 95 GetStorableTypeCollapsingNames(excluded_field) == NAME_FULL) { |
96 for (std::vector<ServerFieldType>::const_iterator it = | 96 for (std::vector<ServerFieldType>::const_iterator it = |
97 suggested_fields->begin(); | 97 suggested_fields->begin(); |
98 it != suggested_fields->end(); ++it) { | 98 it != suggested_fields->end(); ++it) { |
99 if (*it != excluded_field && | 99 if (*it != excluded_field && |
100 GetEquivalentFieldTypeCollapsingNames(*it) == NAME_FULL) { | 100 GetStorableTypeCollapsingNames(*it) == NAME_FULL) { |
101 distinguishing_fields->push_back(NAME_FULL); | 101 distinguishing_fields->push_back(NAME_FULL); |
102 break; | 102 break; |
103 } | 103 } |
104 } | 104 } |
105 } | 105 } |
106 } | 106 } |
107 | 107 |
108 // A helper function for string streaming. Concatenates multi-valued entries | 108 // A helper function for string streaming. Concatenates multi-valued entries |
109 // stored for a given |type| into a single string. This string is returned. | 109 // stored for a given |type| into a single string. This string is returned. |
110 const base::string16 MultiString(const AutofillProfile& p, | 110 const base::string16 MultiString(const AutofillProfile& p, |
111 ServerFieldType type) { | 111 ServerFieldType type) { |
112 std::vector<base::string16> values; | 112 std::vector<base::string16> values; |
113 p.GetRawMultiInfo(type, &values); | 113 p.GetRawMultiInfo(type, &values); |
114 base::string16 accumulate; | 114 base::string16 accumulate; |
115 for (size_t i = 0; i < values.size(); ++i) { | 115 for (size_t i = 0; i < values.size(); ++i) { |
116 if (i > 0) | 116 if (i > 0) |
117 accumulate += ASCIIToUTF16(" "); | 117 accumulate += ASCIIToUTF16(" "); |
118 accumulate += values[i]; | 118 accumulate += values[i]; |
119 } | 119 } |
120 return accumulate; | 120 return accumulate; |
121 } | 121 } |
122 | 122 |
123 base::string16 GetFormGroupInfo(const FormGroup& form_group, | 123 base::string16 GetFormGroupInfo(const FormGroup& form_group, |
124 const AutofillType& type, | 124 const AutofillType& type, |
125 const std::string& app_locale) { | 125 const std::string& app_locale) { |
126 return app_locale.empty() ? | 126 return app_locale.empty() ? |
127 form_group.GetRawInfo(type.server_type()) : | 127 form_group.GetRawInfo(type.GetStorableType()) : |
128 form_group.GetInfo(type, app_locale); | 128 form_group.GetInfo(type, app_locale); |
129 } | 129 } |
130 | 130 |
131 template <class T> | 131 template <class T> |
132 void CopyValuesToItems(ServerFieldType type, | 132 void CopyValuesToItems(ServerFieldType type, |
133 const std::vector<base::string16>& values, | 133 const std::vector<base::string16>& values, |
134 std::vector<T>* form_group_items, | 134 std::vector<T>* form_group_items, |
135 const T& prototype) { | 135 const T& prototype) { |
136 form_group_items->resize(values.size(), prototype); | 136 form_group_items->resize(values.size(), prototype); |
137 for (size_t i = 0; i < form_group_items->size(); ++i) { | 137 for (size_t i = 0; i < form_group_items->size(); ++i) { |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 void AutofillProfile::GetMatchingTypes( | 268 void AutofillProfile::GetMatchingTypes( |
269 const base::string16& text, | 269 const base::string16& text, |
270 const std::string& app_locale, | 270 const std::string& app_locale, |
271 ServerFieldTypeSet* matching_types) const { | 271 ServerFieldTypeSet* matching_types) const { |
272 FormGroupList info = FormGroups(); | 272 FormGroupList info = FormGroups(); |
273 for (FormGroupList::const_iterator it = info.begin(); it != info.end(); ++it) | 273 for (FormGroupList::const_iterator it = info.begin(); it != info.end(); ++it) |
274 (*it)->GetMatchingTypes(text, app_locale, matching_types); | 274 (*it)->GetMatchingTypes(text, app_locale, matching_types); |
275 } | 275 } |
276 | 276 |
277 base::string16 AutofillProfile::GetRawInfo(ServerFieldType type) const { | 277 base::string16 AutofillProfile::GetRawInfo(ServerFieldType type) const { |
278 ServerFieldType return_type = AutofillType::GetEquivalentFieldType(type); | 278 const FormGroup* form_group = FormGroupForType(AutofillType(type)); |
279 const FormGroup* form_group = FormGroupForType(AutofillType(return_type)); | |
280 if (!form_group) | 279 if (!form_group) |
281 return base::string16(); | 280 return base::string16(); |
282 | 281 |
283 return form_group->GetRawInfo(return_type); | 282 return form_group->GetRawInfo(type); |
284 } | 283 } |
285 | 284 |
286 void AutofillProfile::SetRawInfo(ServerFieldType type, | 285 void AutofillProfile::SetRawInfo(ServerFieldType type, |
287 const base::string16& value) { | 286 const base::string16& value) { |
288 FormGroup* form_group = MutableFormGroupForType(AutofillType(type)); | 287 FormGroup* form_group = MutableFormGroupForType(AutofillType(type)); |
289 if (form_group) | 288 if (form_group) |
290 form_group->SetRawInfo(type, CollapseWhitespace(value, false)); | 289 form_group->SetRawInfo(type, CollapseWhitespace(value, false)); |
291 } | 290 } |
292 | 291 |
293 base::string16 AutofillProfile::GetInfo(const AutofillType& type, | 292 base::string16 AutofillProfile::GetInfo(const AutofillType& type, |
294 const std::string& app_locale) const { | 293 const std::string& app_locale) const { |
295 ServerFieldType return_type = | 294 const FormGroup* form_group = FormGroupForType(type); |
296 AutofillType::GetEquivalentFieldType(type.server_type()); | |
297 const FormGroup* form_group = FormGroupForType(AutofillType(return_type)); | |
298 if (!form_group) | 295 if (!form_group) |
299 return base::string16(); | 296 return base::string16(); |
300 | 297 |
301 return form_group->GetInfo(AutofillType(return_type), app_locale); | 298 return form_group->GetInfo(type, app_locale); |
302 } | 299 } |
303 | 300 |
304 bool AutofillProfile::SetInfo(const AutofillType& type, | 301 bool AutofillProfile::SetInfo(const AutofillType& type, |
305 const base::string16& value, | 302 const base::string16& value, |
306 const std::string& app_locale) { | 303 const std::string& app_locale) { |
307 FormGroup* form_group = MutableFormGroupForType(type); | 304 FormGroup* form_group = MutableFormGroupForType(type); |
308 if (!form_group) | 305 if (!form_group) |
309 return false; | 306 return false; |
310 | 307 |
311 return | 308 return |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
356 } | 353 } |
357 | 354 |
358 void AutofillProfile::FillFormField(const AutofillField& field, | 355 void AutofillProfile::FillFormField(const AutofillField& field, |
359 size_t variant, | 356 size_t variant, |
360 const std::string& app_locale, | 357 const std::string& app_locale, |
361 FormFieldData* field_data) const { | 358 FormFieldData* field_data) const { |
362 AutofillType type = field.Type(); | 359 AutofillType type = field.Type(); |
363 DCHECK_NE(CREDIT_CARD, type.group()); | 360 DCHECK_NE(CREDIT_CARD, type.group()); |
364 DCHECK(field_data); | 361 DCHECK(field_data); |
365 | 362 |
366 if (type.server_type() == PHONE_HOME_NUMBER || | 363 if (type.GetStorableType() == PHONE_HOME_NUMBER) { |
367 type.server_type() == PHONE_BILLING_NUMBER) { | |
368 FillPhoneNumberField(field, variant, app_locale, field_data); | 364 FillPhoneNumberField(field, variant, app_locale, field_data); |
369 } else if (field_data->form_control_type == "select-one") { | 365 } else if (field_data->form_control_type == "select-one") { |
370 FillSelectControl(type, app_locale, field_data); | 366 FillSelectControl(type, app_locale, field_data); |
371 } else { | 367 } else { |
372 std::vector<base::string16> values; | 368 std::vector<base::string16> values; |
373 GetMultiInfo(type, app_locale, &values); | 369 GetMultiInfo(type, app_locale, &values); |
374 if (variant >= values.size()) { | 370 if (variant >= values.size()) { |
375 // If the variant is unavailable, bail. This case is reachable, for | 371 // If the variant is unavailable, bail. This case is reachable, for |
376 // example if Sync updates a profile during the filling process. | 372 // example if Sync updates a profile during the filling process. |
377 return; | 373 return; |
(...skipping 523 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
901 << UTF16ToUTF8(profile.GetRawInfo(ADDRESS_HOME_STATE)) | 897 << UTF16ToUTF8(profile.GetRawInfo(ADDRESS_HOME_STATE)) |
902 << " " | 898 << " " |
903 << UTF16ToUTF8(profile.GetRawInfo(ADDRESS_HOME_ZIP)) | 899 << UTF16ToUTF8(profile.GetRawInfo(ADDRESS_HOME_ZIP)) |
904 << " " | 900 << " " |
905 << UTF16ToUTF8(profile.GetRawInfo(ADDRESS_HOME_COUNTRY)) | 901 << UTF16ToUTF8(profile.GetRawInfo(ADDRESS_HOME_COUNTRY)) |
906 << " " | 902 << " " |
907 << UTF16ToUTF8(MultiString(profile, PHONE_HOME_WHOLE_NUMBER)); | 903 << UTF16ToUTF8(MultiString(profile, PHONE_HOME_WHOLE_NUMBER)); |
908 } | 904 } |
909 | 905 |
910 } // namespace autofill | 906 } // namespace autofill |
OLD | NEW |