| 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 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 } | 202 } |
| 203 | 203 |
| 204 base::string16 GetFormGroupInfo(const FormGroup& form_group, | 204 base::string16 GetFormGroupInfo(const FormGroup& form_group, |
| 205 const AutofillType& type, | 205 const AutofillType& type, |
| 206 const std::string& app_locale) { | 206 const std::string& app_locale) { |
| 207 return app_locale.empty() ? | 207 return app_locale.empty() ? |
| 208 form_group.GetRawInfo(type.GetStorableType()) : | 208 form_group.GetRawInfo(type.GetStorableType()) : |
| 209 form_group.GetInfo(type, app_locale); | 209 form_group.GetInfo(type, app_locale); |
| 210 } | 210 } |
| 211 | 211 |
| 212 template <class T> | |
| 213 void CopyRawValuesToItems(ServerFieldType type, | |
| 214 const std::vector<base::string16>& values, | |
| 215 const T& prototype, | |
| 216 std::vector<T>* form_group_items) { | |
| 217 form_group_items->resize(values.size(), prototype); | |
| 218 for (size_t i = 0; i < form_group_items->size(); ++i) { | |
| 219 (*form_group_items)[i].SetRawInfo(type, values[i]); | |
| 220 } | |
| 221 // Must have at least one (possibly empty) element. | |
| 222 form_group_items->resize(std::max<size_t>(1UL, values.size()), prototype); | |
| 223 } | |
| 224 | |
| 225 template <class T> | |
| 226 void CopyValuesToItems(AutofillType type, | |
| 227 const std::vector<base::string16>& values, | |
| 228 const T& prototype, | |
| 229 const std::string& app_locale, | |
| 230 std::vector<T>* form_group_items) { | |
| 231 form_group_items->resize(values.size(), prototype); | |
| 232 for (size_t i = 0; i < form_group_items->size(); ++i) { | |
| 233 (*form_group_items)[i].SetInfo(type, values[i], app_locale); | |
| 234 } | |
| 235 // Must have at least one (possibly empty) element. | |
| 236 form_group_items->resize(std::max<size_t>(1UL, values.size()), prototype); | |
| 237 } | |
| 238 | |
| 239 template <class T> | |
| 240 void CopyItemsToValues(const AutofillType& type, | |
| 241 const std::vector<T>& form_group_items, | |
| 242 const std::string& app_locale, | |
| 243 std::vector<base::string16>* values) { | |
| 244 values->resize(form_group_items.size()); | |
| 245 for (size_t i = 0; i < values->size(); ++i) { | |
| 246 (*values)[i] = GetFormGroupInfo(form_group_items[i], type, app_locale); | |
| 247 } | |
| 248 } | |
| 249 | |
| 250 // Collapse compound field types to their "full" type. I.e. First name | 212 // Collapse compound field types to their "full" type. I.e. First name |
| 251 // collapses to full name, area code collapses to full phone, etc. | 213 // collapses to full name, area code collapses to full phone, etc. |
| 252 void CollapseCompoundFieldTypes(ServerFieldTypeSet* type_set) { | 214 void CollapseCompoundFieldTypes(ServerFieldTypeSet* type_set) { |
| 253 ServerFieldTypeSet collapsed_set; | 215 ServerFieldTypeSet collapsed_set; |
| 254 for (ServerFieldTypeSet::iterator it = type_set->begin(); | 216 for (ServerFieldTypeSet::iterator it = type_set->begin(); |
| 255 it != type_set->end(); ++it) { | 217 it != type_set->end(); ++it) { |
| 256 switch (*it) { | 218 switch (*it) { |
| 257 case NAME_FIRST: | 219 case NAME_FIRST: |
| 258 case NAME_MIDDLE: | 220 case NAME_MIDDLE: |
| 259 case NAME_LAST: | 221 case NAME_LAST: |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 std::string country_code_; | 258 std::string country_code_; |
| 297 std::string app_locale_; | 259 std::string app_locale_; |
| 298 }; | 260 }; |
| 299 | 261 |
| 300 } // namespace | 262 } // namespace |
| 301 | 263 |
| 302 AutofillProfile::AutofillProfile(const std::string& guid, | 264 AutofillProfile::AutofillProfile(const std::string& guid, |
| 303 const std::string& origin) | 265 const std::string& origin) |
| 304 : AutofillDataModel(guid, origin), | 266 : AutofillDataModel(guid, origin), |
| 305 record_type_(LOCAL_PROFILE), | 267 record_type_(LOCAL_PROFILE), |
| 306 email_(1), | 268 phone_number_(this) { |
| 307 phone_number_(1, PhoneNumber(this)) { | |
| 308 } | 269 } |
| 309 | 270 |
| 310 AutofillProfile::AutofillProfile(RecordType type, const std::string& server_id) | 271 AutofillProfile::AutofillProfile(RecordType type, const std::string& server_id) |
| 311 : AutofillDataModel(base::GenerateGUID(), std::string()), | 272 : AutofillDataModel(base::GenerateGUID(), std::string()), |
| 312 record_type_(type), | 273 record_type_(type), |
| 313 email_(1), | 274 phone_number_(this), |
| 314 phone_number_(1, PhoneNumber(this)), | |
| 315 server_id_(server_id) { | 275 server_id_(server_id) { |
| 316 DCHECK(type == SERVER_PROFILE); | 276 DCHECK(type == SERVER_PROFILE); |
| 317 } | 277 } |
| 318 | 278 |
| 319 AutofillProfile::AutofillProfile() | 279 AutofillProfile::AutofillProfile() |
| 320 : AutofillDataModel(base::GenerateGUID(), std::string()), | 280 : AutofillDataModel(base::GenerateGUID(), std::string()), |
| 321 record_type_(LOCAL_PROFILE), | 281 record_type_(LOCAL_PROFILE), |
| 322 email_(1), | 282 phone_number_(this) { |
| 323 phone_number_(1, PhoneNumber(this)) { | |
| 324 } | 283 } |
| 325 | 284 |
| 326 AutofillProfile::AutofillProfile(const AutofillProfile& profile) | 285 AutofillProfile::AutofillProfile(const AutofillProfile& profile) |
| 327 : AutofillDataModel(std::string(), std::string()) { | 286 : AutofillDataModel(std::string(), std::string()), |
| 287 phone_number_(this) { |
| 328 operator=(profile); | 288 operator=(profile); |
| 329 } | 289 } |
| 330 | 290 |
| 331 AutofillProfile::~AutofillProfile() { | 291 AutofillProfile::~AutofillProfile() { |
| 332 } | 292 } |
| 333 | 293 |
| 334 AutofillProfile& AutofillProfile::operator=(const AutofillProfile& profile) { | 294 AutofillProfile& AutofillProfile::operator=(const AutofillProfile& profile) { |
| 335 set_use_count(profile.use_count()); | 295 set_use_count(profile.use_count()); |
| 336 set_use_date(profile.use_date()); | 296 set_use_date(profile.use_date()); |
| 337 set_modification_date(profile.modification_date()); | 297 set_modification_date(profile.modification_date()); |
| 338 | 298 |
| 339 if (this == &profile) | 299 if (this == &profile) |
| 340 return *this; | 300 return *this; |
| 341 | 301 |
| 342 set_guid(profile.guid()); | 302 set_guid(profile.guid()); |
| 343 set_origin(profile.origin()); | 303 set_origin(profile.origin()); |
| 344 | 304 |
| 345 record_type_ = profile.record_type_; | 305 record_type_ = profile.record_type_; |
| 346 | 306 |
| 347 name_ = profile.name_; | 307 name_ = profile.name_; |
| 348 email_ = profile.email_; | 308 email_ = profile.email_; |
| 349 company_ = profile.company_; | 309 company_ = profile.company_; |
| 350 phone_number_ = profile.phone_number_; | 310 phone_number_ = profile.phone_number_; |
| 351 | 311 phone_number_.set_profile(this); |
| 352 for (size_t i = 0; i < phone_number_.size(); ++i) | |
| 353 phone_number_[i].set_profile(this); | |
| 354 | 312 |
| 355 address_ = profile.address_; | 313 address_ = profile.address_; |
| 356 set_language_code(profile.language_code()); | 314 set_language_code(profile.language_code()); |
| 357 | 315 |
| 358 server_id_ = profile.server_id(); | 316 server_id_ = profile.server_id(); |
| 359 | 317 |
| 360 return *this; | 318 return *this; |
| 361 } | 319 } |
| 362 | 320 |
| 363 void AutofillProfile::GetMatchingTypes( | 321 void AutofillProfile::GetMatchingTypes( |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 416 return form_group->SetInfo(type, trimmed_value, app_locale); | 374 return form_group->SetInfo(type, trimmed_value, app_locale); |
| 417 } | 375 } |
| 418 | 376 |
| 419 // TODO(estade): remove this function. | 377 // TODO(estade): remove this function. |
| 420 void AutofillProfile::SetRawMultiInfo( | 378 void AutofillProfile::SetRawMultiInfo( |
| 421 ServerFieldType type, | 379 ServerFieldType type, |
| 422 const std::vector<base::string16>& values) { | 380 const std::vector<base::string16>& values) { |
| 423 switch (AutofillType(type).group()) { | 381 switch (AutofillType(type).group()) { |
| 424 case NAME: | 382 case NAME: |
| 425 case NAME_BILLING: | 383 case NAME_BILLING: |
| 384 case EMAIL: |
| 385 case PHONE_HOME: |
| 386 case PHONE_BILLING: |
| 426 SetRawInfo(type, values.empty() ? base::string16() : values[0]); | 387 SetRawInfo(type, values.empty() ? base::string16() : values[0]); |
| 427 break; | 388 break; |
| 428 | 389 |
| 429 case EMAIL: | |
| 430 CopyRawValuesToItems(type, values, EmailInfo(), &email_); | |
| 431 break; | |
| 432 | |
| 433 case PHONE_HOME: | |
| 434 case PHONE_BILLING: | |
| 435 CopyRawValuesToItems(type, values, PhoneNumber(this), &phone_number_); | |
| 436 break; | |
| 437 | |
| 438 default: | 390 default: |
| 439 if (values.size() == 1U) { | 391 if (values.size() == 1U) { |
| 440 SetRawInfo(type, values[0]); | 392 SetRawInfo(type, values[0]); |
| 441 } else if (values.empty()) { | 393 } else if (values.empty()) { |
| 442 SetRawInfo(type, base::string16()); | 394 SetRawInfo(type, base::string16()); |
| 443 } else { | 395 } else { |
| 444 NOTREACHED(); | 396 NOTREACHED(); |
| 445 } | 397 } |
| 446 break; | 398 break; |
| 447 } | 399 } |
| (...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 838 return CanonicalizeProfileString(a) == CanonicalizeProfileString(b); | 790 return CanonicalizeProfileString(a) == CanonicalizeProfileString(b); |
| 839 } | 791 } |
| 840 | 792 |
| 841 void AutofillProfile::GetSupportedTypes( | 793 void AutofillProfile::GetSupportedTypes( |
| 842 ServerFieldTypeSet* supported_types) const { | 794 ServerFieldTypeSet* supported_types) const { |
| 843 FormGroupList info = FormGroups(); | 795 FormGroupList info = FormGroups(); |
| 844 for (FormGroupList::const_iterator it = info.begin(); it != info.end(); ++it) | 796 for (FormGroupList::const_iterator it = info.begin(); it != info.end(); ++it) |
| 845 (*it)->GetSupportedTypes(supported_types); | 797 (*it)->GetSupportedTypes(supported_types); |
| 846 } | 798 } |
| 847 | 799 |
| 800 // TODO(estade): remove this function. |
| 848 void AutofillProfile::GetMultiInfoImpl( | 801 void AutofillProfile::GetMultiInfoImpl( |
| 849 const AutofillType& type, | 802 const AutofillType& type, |
| 850 const std::string& app_locale, | 803 const std::string& app_locale, |
| 851 std::vector<base::string16>* values) const { | 804 std::vector<base::string16>* values) const { |
| 852 switch (type.group()) { | 805 values->resize(1); |
| 853 case EMAIL: | 806 (*values)[0] = GetFormGroupInfo(*this, type, app_locale); |
| 854 CopyItemsToValues(type, email_, app_locale, values); | |
| 855 break; | |
| 856 case PHONE_HOME: | |
| 857 case PHONE_BILLING: | |
| 858 CopyItemsToValues(type, phone_number_, app_locale, values); | |
| 859 break; | |
| 860 default: | |
| 861 values->resize(1); | |
| 862 (*values)[0] = GetFormGroupInfo(*this, type, app_locale); | |
| 863 } | |
| 864 } | 807 } |
| 865 | 808 |
| 866 base::string16 AutofillProfile::ConstructInferredLabel( | 809 base::string16 AutofillProfile::ConstructInferredLabel( |
| 867 const std::vector<ServerFieldType>& included_fields, | 810 const std::vector<ServerFieldType>& included_fields, |
| 868 size_t num_fields_to_use, | 811 size_t num_fields_to_use, |
| 869 const std::string& app_locale) const { | 812 const std::string& app_locale) const { |
| 870 // TODO(estade): use libaddressinput? | 813 // TODO(estade): use libaddressinput? |
| 871 base::string16 separator = | 814 base::string16 separator = |
| 872 l10n_util::GetStringUTF16(IDS_AUTOFILL_ADDRESS_SUMMARY_SEPARATOR); | 815 l10n_util::GetStringUTF16(IDS_AUTOFILL_ADDRESS_SUMMARY_SEPARATOR); |
| 873 | 816 |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1010 } | 953 } |
| 1011 | 954 |
| 1012 (*labels)[*it] = profile->ConstructInferredLabel( | 955 (*labels)[*it] = profile->ConstructInferredLabel( |
| 1013 label_fields, label_fields.size(), app_locale); | 956 label_fields, label_fields.size(), app_locale); |
| 1014 } | 957 } |
| 1015 } | 958 } |
| 1016 | 959 |
| 1017 AutofillProfile::FormGroupList AutofillProfile::FormGroups() const { | 960 AutofillProfile::FormGroupList AutofillProfile::FormGroups() const { |
| 1018 FormGroupList v(5); | 961 FormGroupList v(5); |
| 1019 v[0] = &name_; | 962 v[0] = &name_; |
| 1020 v[1] = &email_[0]; | 963 v[1] = &email_; |
| 1021 v[2] = &company_; | 964 v[2] = &company_; |
| 1022 v[3] = &phone_number_[0]; | 965 v[3] = &phone_number_; |
| 1023 v[4] = &address_; | 966 v[4] = &address_; |
| 1024 return v; | 967 return v; |
| 1025 } | 968 } |
| 1026 | 969 |
| 1027 const FormGroup* AutofillProfile::FormGroupForType( | 970 const FormGroup* AutofillProfile::FormGroupForType( |
| 1028 const AutofillType& type) const { | 971 const AutofillType& type) const { |
| 1029 return const_cast<AutofillProfile*>(this)->MutableFormGroupForType(type); | 972 return const_cast<AutofillProfile*>(this)->MutableFormGroupForType(type); |
| 1030 } | 973 } |
| 1031 | 974 |
| 1032 FormGroup* AutofillProfile::MutableFormGroupForType(const AutofillType& type) { | 975 FormGroup* AutofillProfile::MutableFormGroupForType(const AutofillType& type) { |
| 1033 switch (type.group()) { | 976 switch (type.group()) { |
| 1034 case NAME: | 977 case NAME: |
| 1035 case NAME_BILLING: | 978 case NAME_BILLING: |
| 1036 return &name_; | 979 return &name_; |
| 1037 | 980 |
| 1038 case EMAIL: | 981 case EMAIL: |
| 1039 return &email_[0]; | 982 return &email_; |
| 1040 | 983 |
| 1041 case COMPANY: | 984 case COMPANY: |
| 1042 return &company_; | 985 return &company_; |
| 1043 | 986 |
| 1044 case PHONE_HOME: | 987 case PHONE_HOME: |
| 1045 case PHONE_BILLING: | 988 case PHONE_BILLING: |
| 1046 return &phone_number_[0]; | 989 return &phone_number_; |
| 1047 | 990 |
| 1048 case ADDRESS_HOME: | 991 case ADDRESS_HOME: |
| 1049 case ADDRESS_BILLING: | 992 case ADDRESS_BILLING: |
| 1050 return &address_; | 993 return &address_; |
| 1051 | 994 |
| 1052 case NO_GROUP: | 995 case NO_GROUP: |
| 1053 case CREDIT_CARD: | 996 case CREDIT_CARD: |
| 1054 case PASSWORD_FIELD: | 997 case PASSWORD_FIELD: |
| 1055 case USERNAME_FIELD: | 998 case USERNAME_FIELD: |
| 1056 case TRANSACTION: | 999 case TRANSACTION: |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1099 << UTF16ToUTF8(profile.GetRawInfo(ADDRESS_HOME_SORTING_CODE)) | 1042 << UTF16ToUTF8(profile.GetRawInfo(ADDRESS_HOME_SORTING_CODE)) |
| 1100 << " " | 1043 << " " |
| 1101 << UTF16ToUTF8(profile.GetRawInfo(ADDRESS_HOME_COUNTRY)) | 1044 << UTF16ToUTF8(profile.GetRawInfo(ADDRESS_HOME_COUNTRY)) |
| 1102 << " " | 1045 << " " |
| 1103 << profile.language_code() | 1046 << profile.language_code() |
| 1104 << " " | 1047 << " " |
| 1105 << UTF16ToUTF8(MultiString(profile, PHONE_HOME_WHOLE_NUMBER)); | 1048 << UTF16ToUTF8(MultiString(profile, PHONE_HOME_WHOLE_NUMBER)); |
| 1106 } | 1049 } |
| 1107 | 1050 |
| 1108 } // namespace autofill | 1051 } // namespace autofill |
| OLD | NEW |