| 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 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 it != suggested_fields->end(); ++it) { | 179 it != suggested_fields->end(); ++it) { |
| 180 if (*it != excluded_field && | 180 if (*it != excluded_field && |
| 181 GetStorableTypeCollapsingGroups(*it) == effective_excluded_type) { | 181 GetStorableTypeCollapsingGroups(*it) == effective_excluded_type) { |
| 182 distinguishing_fields->push_back(effective_excluded_type); | 182 distinguishing_fields->push_back(effective_excluded_type); |
| 183 break; | 183 break; |
| 184 } | 184 } |
| 185 } | 185 } |
| 186 } | 186 } |
| 187 } | 187 } |
| 188 | 188 |
| 189 // A helper function for string streaming. Concatenates multi-valued entries | |
| 190 // stored for a given |type| into a single string. This string is returned. | |
| 191 const base::string16 MultiString(const AutofillProfile& p, | |
| 192 ServerFieldType type) { | |
| 193 std::vector<base::string16> values; | |
| 194 p.GetRawMultiInfo(type, &values); | |
| 195 base::string16 accumulate; | |
| 196 for (size_t i = 0; i < values.size(); ++i) { | |
| 197 if (i > 0) | |
| 198 accumulate += ASCIIToUTF16(" "); | |
| 199 accumulate += values[i]; | |
| 200 } | |
| 201 return accumulate; | |
| 202 } | |
| 203 | |
| 204 base::string16 GetFormGroupInfo(const FormGroup& form_group, | |
| 205 const AutofillType& type, | |
| 206 const std::string& app_locale) { | |
| 207 return app_locale.empty() ? | |
| 208 form_group.GetRawInfo(type.GetStorableType()) : | |
| 209 form_group.GetInfo(type, app_locale); | |
| 210 } | |
| 211 | |
| 212 // Collapse compound field types to their "full" type. I.e. First name | 189 // Collapse compound field types to their "full" type. I.e. First name |
| 213 // collapses to full name, area code collapses to full phone, etc. | 190 // collapses to full name, area code collapses to full phone, etc. |
| 214 void CollapseCompoundFieldTypes(ServerFieldTypeSet* type_set) { | 191 void CollapseCompoundFieldTypes(ServerFieldTypeSet* type_set) { |
| 215 ServerFieldTypeSet collapsed_set; | 192 ServerFieldTypeSet collapsed_set; |
| 216 for (ServerFieldTypeSet::iterator it = type_set->begin(); | 193 for (ServerFieldTypeSet::iterator it = type_set->begin(); |
| 217 it != type_set->end(); ++it) { | 194 it != type_set->end(); ++it) { |
| 218 switch (*it) { | 195 switch (*it) { |
| 219 case NAME_FIRST: | 196 case NAME_FIRST: |
| 220 case NAME_MIDDLE: | 197 case NAME_MIDDLE: |
| 221 case NAME_LAST: | 198 case NAME_LAST: |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 DCHECK(type == SERVER_PROFILE); | 253 DCHECK(type == SERVER_PROFILE); |
| 277 } | 254 } |
| 278 | 255 |
| 279 AutofillProfile::AutofillProfile() | 256 AutofillProfile::AutofillProfile() |
| 280 : AutofillDataModel(base::GenerateGUID(), std::string()), | 257 : AutofillDataModel(base::GenerateGUID(), std::string()), |
| 281 record_type_(LOCAL_PROFILE), | 258 record_type_(LOCAL_PROFILE), |
| 282 phone_number_(this) { | 259 phone_number_(this) { |
| 283 } | 260 } |
| 284 | 261 |
| 285 AutofillProfile::AutofillProfile(const AutofillProfile& profile) | 262 AutofillProfile::AutofillProfile(const AutofillProfile& profile) |
| 286 : AutofillDataModel(std::string(), std::string()), | 263 : AutofillDataModel(std::string(), std::string()), phone_number_(this) { |
| 287 phone_number_(this) { | |
| 288 operator=(profile); | 264 operator=(profile); |
| 289 } | 265 } |
| 290 | 266 |
| 291 AutofillProfile::~AutofillProfile() { | 267 AutofillProfile::~AutofillProfile() { |
| 292 } | 268 } |
| 293 | 269 |
| 294 AutofillProfile& AutofillProfile::operator=(const AutofillProfile& profile) { | 270 AutofillProfile& AutofillProfile::operator=(const AutofillProfile& profile) { |
| 295 set_use_count(profile.use_count()); | 271 set_use_count(profile.use_count()); |
| 296 set_use_date(profile.use_date()); | 272 set_use_date(profile.use_date()); |
| 297 set_modification_date(profile.modification_date()); | 273 set_modification_date(profile.modification_date()); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 367 const std::string& app_locale) { | 343 const std::string& app_locale) { |
| 368 FormGroup* form_group = MutableFormGroupForType(type); | 344 FormGroup* form_group = MutableFormGroupForType(type); |
| 369 if (!form_group) | 345 if (!form_group) |
| 370 return false; | 346 return false; |
| 371 | 347 |
| 372 base::string16 trimmed_value; | 348 base::string16 trimmed_value; |
| 373 base::TrimWhitespace(value, base::TRIM_ALL, &trimmed_value); | 349 base::TrimWhitespace(value, base::TRIM_ALL, &trimmed_value); |
| 374 return form_group->SetInfo(type, trimmed_value, app_locale); | 350 return form_group->SetInfo(type, trimmed_value, app_locale); |
| 375 } | 351 } |
| 376 | 352 |
| 377 // TODO(estade): remove this function. | |
| 378 void AutofillProfile::SetRawMultiInfo( | |
| 379 ServerFieldType type, | |
| 380 const std::vector<base::string16>& values) { | |
| 381 switch (AutofillType(type).group()) { | |
| 382 case NAME: | |
| 383 case NAME_BILLING: | |
| 384 case EMAIL: | |
| 385 case PHONE_HOME: | |
| 386 case PHONE_BILLING: | |
| 387 SetRawInfo(type, values.empty() ? base::string16() : values[0]); | |
| 388 break; | |
| 389 | |
| 390 default: | |
| 391 if (values.size() == 1U) { | |
| 392 SetRawInfo(type, values[0]); | |
| 393 } else if (values.empty()) { | |
| 394 SetRawInfo(type, base::string16()); | |
| 395 } else { | |
| 396 NOTREACHED(); | |
| 397 } | |
| 398 break; | |
| 399 } | |
| 400 } | |
| 401 | |
| 402 void AutofillProfile::GetRawMultiInfo( | |
| 403 ServerFieldType type, | |
| 404 std::vector<base::string16>* values) const { | |
| 405 GetMultiInfoImpl(AutofillType(type), std::string(), values); | |
| 406 } | |
| 407 | |
| 408 bool AutofillProfile::IsEmpty(const std::string& app_locale) const { | 353 bool AutofillProfile::IsEmpty(const std::string& app_locale) const { |
| 409 ServerFieldTypeSet types; | 354 ServerFieldTypeSet types; |
| 410 GetNonEmptyTypes(app_locale, &types); | 355 GetNonEmptyTypes(app_locale, &types); |
| 411 return types.empty(); | 356 return types.empty(); |
| 412 } | 357 } |
| 413 | 358 |
| 414 bool AutofillProfile::IsPresentButInvalid(ServerFieldType type) const { | 359 bool AutofillProfile::IsPresentButInvalid(ServerFieldType type) const { |
| 415 std::string country = UTF16ToUTF8(GetRawInfo(ADDRESS_HOME_COUNTRY)); | 360 std::string country = UTF16ToUTF8(GetRawInfo(ADDRESS_HOME_COUNTRY)); |
| 416 base::string16 data = GetRawInfo(type); | 361 base::string16 data = GetRawInfo(type); |
| 417 if (data.empty()) | 362 if (data.empty()) |
| (...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 699 // We have more than one profile with the same label, so add | 644 // We have more than one profile with the same label, so add |
| 700 // differentiating fields. | 645 // differentiating fields. |
| 701 CreateInferredLabelsHelper(profiles, it->second, fields_to_use, | 646 CreateInferredLabelsHelper(profiles, it->second, fields_to_use, |
| 702 minimal_fields_shown, app_locale, labels); | 647 minimal_fields_shown, app_locale, labels); |
| 703 } | 648 } |
| 704 } | 649 } |
| 705 } | 650 } |
| 706 | 651 |
| 707 void AutofillProfile::GenerateServerProfileIdentifier() { | 652 void AutofillProfile::GenerateServerProfileIdentifier() { |
| 708 DCHECK_EQ(SERVER_PROFILE, record_type()); | 653 DCHECK_EQ(SERVER_PROFILE, record_type()); |
| 709 base::string16 contents = MultiString(*this, NAME_FIRST); | 654 base::string16 contents = GetRawInfo(NAME_FIRST); |
| 710 contents.append(MultiString(*this, NAME_MIDDLE)); | 655 contents.append(GetRawInfo(NAME_MIDDLE)); |
| 711 contents.append(MultiString(*this, NAME_LAST)); | 656 contents.append(GetRawInfo(NAME_LAST)); |
| 712 contents.append(MultiString(*this, EMAIL_ADDRESS)); | 657 contents.append(GetRawInfo(EMAIL_ADDRESS)); |
| 713 contents.append(GetRawInfo(COMPANY_NAME)); | 658 contents.append(GetRawInfo(COMPANY_NAME)); |
| 714 contents.append(GetRawInfo(ADDRESS_HOME_STREET_ADDRESS)); | 659 contents.append(GetRawInfo(ADDRESS_HOME_STREET_ADDRESS)); |
| 715 contents.append(GetRawInfo(ADDRESS_HOME_DEPENDENT_LOCALITY)); | 660 contents.append(GetRawInfo(ADDRESS_HOME_DEPENDENT_LOCALITY)); |
| 716 contents.append(GetRawInfo(ADDRESS_HOME_CITY)); | 661 contents.append(GetRawInfo(ADDRESS_HOME_CITY)); |
| 717 contents.append(GetRawInfo(ADDRESS_HOME_STATE)); | 662 contents.append(GetRawInfo(ADDRESS_HOME_STATE)); |
| 718 contents.append(GetRawInfo(ADDRESS_HOME_ZIP)); | 663 contents.append(GetRawInfo(ADDRESS_HOME_ZIP)); |
| 719 contents.append(GetRawInfo(ADDRESS_HOME_SORTING_CODE)); | 664 contents.append(GetRawInfo(ADDRESS_HOME_SORTING_CODE)); |
| 720 contents.append(GetRawInfo(ADDRESS_HOME_COUNTRY)); | 665 contents.append(GetRawInfo(ADDRESS_HOME_COUNTRY)); |
| 721 contents.append(MultiString(*this, PHONE_HOME_WHOLE_NUMBER)); | 666 contents.append(GetRawInfo(PHONE_HOME_WHOLE_NUMBER)); |
| 722 std::string contents_utf8 = UTF16ToUTF8(contents); | 667 std::string contents_utf8 = UTF16ToUTF8(contents); |
| 723 contents_utf8.append(language_code()); | 668 contents_utf8.append(language_code()); |
| 724 server_id_ = base::SHA1HashString(contents_utf8); | 669 server_id_ = base::SHA1HashString(contents_utf8); |
| 725 } | 670 } |
| 726 | 671 |
| 727 // static | 672 // static |
| 728 base::string16 AutofillProfile::CanonicalizeProfileString( | 673 base::string16 AutofillProfile::CanonicalizeProfileString( |
| 729 const base::string16& str) { | 674 const base::string16& str) { |
| 730 base::string16 ret; | 675 base::string16 ret; |
| 731 ret.reserve(str.size()); | 676 ret.reserve(str.size()); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 790 return CanonicalizeProfileString(a) == CanonicalizeProfileString(b); | 735 return CanonicalizeProfileString(a) == CanonicalizeProfileString(b); |
| 791 } | 736 } |
| 792 | 737 |
| 793 void AutofillProfile::GetSupportedTypes( | 738 void AutofillProfile::GetSupportedTypes( |
| 794 ServerFieldTypeSet* supported_types) const { | 739 ServerFieldTypeSet* supported_types) const { |
| 795 FormGroupList info = FormGroups(); | 740 FormGroupList info = FormGroups(); |
| 796 for (FormGroupList::const_iterator it = info.begin(); it != info.end(); ++it) | 741 for (FormGroupList::const_iterator it = info.begin(); it != info.end(); ++it) |
| 797 (*it)->GetSupportedTypes(supported_types); | 742 (*it)->GetSupportedTypes(supported_types); |
| 798 } | 743 } |
| 799 | 744 |
| 800 // TODO(estade): remove this function. | |
| 801 void AutofillProfile::GetMultiInfoImpl( | |
| 802 const AutofillType& type, | |
| 803 const std::string& app_locale, | |
| 804 std::vector<base::string16>* values) const { | |
| 805 values->resize(1); | |
| 806 (*values)[0] = GetFormGroupInfo(*this, type, app_locale); | |
| 807 } | |
| 808 | |
| 809 base::string16 AutofillProfile::ConstructInferredLabel( | 745 base::string16 AutofillProfile::ConstructInferredLabel( |
| 810 const std::vector<ServerFieldType>& included_fields, | 746 const std::vector<ServerFieldType>& included_fields, |
| 811 size_t num_fields_to_use, | 747 size_t num_fields_to_use, |
| 812 const std::string& app_locale) const { | 748 const std::string& app_locale) const { |
| 813 // TODO(estade): use libaddressinput? | 749 // TODO(estade): use libaddressinput? |
| 814 base::string16 separator = | 750 base::string16 separator = |
| 815 l10n_util::GetStringUTF16(IDS_AUTOFILL_ADDRESS_SUMMARY_SEPARATOR); | 751 l10n_util::GetStringUTF16(IDS_AUTOFILL_ADDRESS_SUMMARY_SEPARATOR); |
| 816 | 752 |
| 817 AutofillType region_code_type(HTML_TYPE_COUNTRY_CODE, HTML_MODE_NONE); | 753 AutofillType region_code_type(HTML_TYPE_COUNTRY_CODE, HTML_MODE_NONE); |
| 818 const base::string16& profile_region_code = | 754 const base::string16& profile_region_code = |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1005 } | 941 } |
| 1006 | 942 |
| 1007 bool AutofillProfile::EqualsSansGuid(const AutofillProfile& profile) const { | 943 bool AutofillProfile::EqualsSansGuid(const AutofillProfile& profile) const { |
| 1008 return origin() == profile.origin() && | 944 return origin() == profile.origin() && |
| 1009 language_code() == profile.language_code() && | 945 language_code() == profile.language_code() && |
| 1010 Compare(profile) == 0; | 946 Compare(profile) == 0; |
| 1011 } | 947 } |
| 1012 | 948 |
| 1013 // So we can compare AutofillProfiles with EXPECT_EQ(). | 949 // So we can compare AutofillProfiles with EXPECT_EQ(). |
| 1014 std::ostream& operator<<(std::ostream& os, const AutofillProfile& profile) { | 950 std::ostream& operator<<(std::ostream& os, const AutofillProfile& profile) { |
| 1015 return os | 951 return os << profile.guid() << " " << profile.origin() << " " |
| 1016 << profile.guid() | 952 << UTF16ToUTF8(profile.GetRawInfo(NAME_FIRST)) << " " |
| 1017 << " " | 953 << UTF16ToUTF8(profile.GetRawInfo(NAME_MIDDLE)) << " " |
| 1018 << profile.origin() | 954 << UTF16ToUTF8(profile.GetRawInfo(NAME_LAST)) << " " |
| 1019 << " " | 955 << UTF16ToUTF8(profile.GetRawInfo(EMAIL_ADDRESS)) << " " |
| 1020 << UTF16ToUTF8(MultiString(profile, NAME_FIRST)) | 956 << UTF16ToUTF8(profile.GetRawInfo(COMPANY_NAME)) << " " |
| 1021 << " " | 957 << UTF16ToUTF8(profile.GetRawInfo(ADDRESS_HOME_LINE1)) << " " |
| 1022 << UTF16ToUTF8(MultiString(profile, NAME_MIDDLE)) | 958 << UTF16ToUTF8(profile.GetRawInfo(ADDRESS_HOME_LINE2)) << " " |
| 1023 << " " | 959 << UTF16ToUTF8(profile.GetRawInfo(ADDRESS_HOME_DEPENDENT_LOCALITY)) |
| 1024 << UTF16ToUTF8(MultiString(profile, NAME_LAST)) | 960 << " " << UTF16ToUTF8(profile.GetRawInfo(ADDRESS_HOME_CITY)) << " " |
| 1025 << " " | 961 << UTF16ToUTF8(profile.GetRawInfo(ADDRESS_HOME_STATE)) << " " |
| 1026 << UTF16ToUTF8(MultiString(profile, EMAIL_ADDRESS)) | 962 << UTF16ToUTF8(profile.GetRawInfo(ADDRESS_HOME_ZIP)) << " " |
| 1027 << " " | 963 << UTF16ToUTF8(profile.GetRawInfo(ADDRESS_HOME_SORTING_CODE)) << " " |
| 1028 << UTF16ToUTF8(profile.GetRawInfo(COMPANY_NAME)) | 964 << UTF16ToUTF8(profile.GetRawInfo(ADDRESS_HOME_COUNTRY)) << " " |
| 1029 << " " | 965 << profile.language_code() << " " |
| 1030 << UTF16ToUTF8(profile.GetRawInfo(ADDRESS_HOME_LINE1)) | 966 << UTF16ToUTF8(profile.GetRawInfo(PHONE_HOME_WHOLE_NUMBER)); |
| 1031 << " " | |
| 1032 << UTF16ToUTF8(profile.GetRawInfo(ADDRESS_HOME_LINE2)) | |
| 1033 << " " | |
| 1034 << UTF16ToUTF8(profile.GetRawInfo(ADDRESS_HOME_DEPENDENT_LOCALITY)) | |
| 1035 << " " | |
| 1036 << UTF16ToUTF8(profile.GetRawInfo(ADDRESS_HOME_CITY)) | |
| 1037 << " " | |
| 1038 << UTF16ToUTF8(profile.GetRawInfo(ADDRESS_HOME_STATE)) | |
| 1039 << " " | |
| 1040 << UTF16ToUTF8(profile.GetRawInfo(ADDRESS_HOME_ZIP)) | |
| 1041 << " " | |
| 1042 << UTF16ToUTF8(profile.GetRawInfo(ADDRESS_HOME_SORTING_CODE)) | |
| 1043 << " " | |
| 1044 << UTF16ToUTF8(profile.GetRawInfo(ADDRESS_HOME_COUNTRY)) | |
| 1045 << " " | |
| 1046 << profile.language_code() | |
| 1047 << " " | |
| 1048 << UTF16ToUTF8(MultiString(profile, PHONE_HOME_WHOLE_NUMBER)); | |
| 1049 } | 967 } |
| 1050 | 968 |
| 1051 } // namespace autofill | 969 } // namespace autofill |
| OLD | NEW |