Chromium Code Reviews| 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/personal_data_manager.h" | 5 #include "components/autofill/core/browser/personal_data_manager.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <list> | 10 #include <list> |
| (...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 367 | 367 |
| 368 switch (result->GetType()) { | 368 switch (result->GetType()) { |
| 369 case AUTOFILL_PROFILES_RESULT: | 369 case AUTOFILL_PROFILES_RESULT: |
| 370 if (h == pending_profiles_query_) { | 370 if (h == pending_profiles_query_) { |
| 371 ReceiveLoadedDbValues(h, result.get(), &pending_profiles_query_, | 371 ReceiveLoadedDbValues(h, result.get(), &pending_profiles_query_, |
| 372 &web_profiles_); | 372 &web_profiles_); |
| 373 LogProfileCount(); // This only logs local profiles. | 373 LogProfileCount(); // This only logs local profiles. |
| 374 } else { | 374 } else { |
| 375 ReceiveLoadedDbValues(h, result.get(), &pending_server_profiles_query_, | 375 ReceiveLoadedDbValues(h, result.get(), &pending_server_profiles_query_, |
| 376 &server_profiles_); | 376 &server_profiles_); |
| 377 | |
| 378 if (!server_profiles_.empty()) { | |
| 379 std::string account_id = signin_manager_->GetAuthenticatedAccountId(); | |
| 380 base::string16 email = | |
| 381 base::UTF8ToUTF16( | |
| 382 account_tracker_->GetAccountInfo(account_id).email); | |
| 383 | |
| 384 // User may have signed out during the fulfillment of the web data | |
| 385 // request, in which case there is no point updating | |
| 386 // |server_profiles_| as it will be cleared. | |
| 387 if (!email.empty()) { | |
| 388 for (auto& profile : server_profiles_) | |
| 389 profile->SetRawInfo(EMAIL_ADDRESS, email); | |
| 390 } | |
| 391 } | |
| 392 } | 377 } |
| 393 break; | 378 break; |
| 394 case AUTOFILL_CREDITCARDS_RESULT: | 379 case AUTOFILL_CREDITCARDS_RESULT: |
| 395 if (h == pending_creditcards_query_) { | 380 if (h == pending_creditcards_query_) { |
| 396 ReceiveLoadedDbValues(h, result.get(), &pending_creditcards_query_, | 381 ReceiveLoadedDbValues(h, result.get(), &pending_creditcards_query_, |
| 397 &local_credit_cards_); | 382 &local_credit_cards_); |
| 398 LogLocalCreditCardCount(); | 383 LogLocalCreditCardCount(); |
| 399 } else { | 384 } else { |
| 400 ReceiveLoadedDbValues(h, result.get(), | 385 ReceiveLoadedDbValues(h, result.get(), |
| 401 &pending_server_creditcards_query_, | 386 &pending_server_creditcards_query_, |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 417 if (pending_profiles_query_ == 0 && | 402 if (pending_profiles_query_ == 0 && |
| 418 pending_creditcards_query_ == 0 && | 403 pending_creditcards_query_ == 0 && |
| 419 pending_server_profiles_query_ == 0 && | 404 pending_server_profiles_query_ == 0 && |
| 420 pending_server_creditcards_query_ == 0) { | 405 pending_server_creditcards_query_ == 0) { |
| 421 is_data_loaded_ = true; | 406 is_data_loaded_ = true; |
| 422 NotifyPersonalDataChanged(); | 407 NotifyPersonalDataChanged(); |
| 423 } | 408 } |
| 424 } | 409 } |
| 425 | 410 |
| 426 void PersonalDataManager::AutofillMultipleChanged() { | 411 void PersonalDataManager::AutofillMultipleChanged() { |
| 412 has_synced_new_data = true; | |
| 427 Refresh(); | 413 Refresh(); |
| 428 } | 414 } |
| 429 | 415 |
| 430 void PersonalDataManager::SyncStarted(syncer::ModelType model_type) { | 416 void PersonalDataManager::SyncStarted(syncer::ModelType model_type) { |
| 431 if (model_type == syncer::AUTOFILL_PROFILE && | 417 if (model_type == syncer::AUTOFILL_PROFILE && |
| 432 is_autofill_profile_cleanup_pending_) { | 418 is_autofill_profile_cleanup_pending_) { |
| 433 // This runs as a one-time fix, tracked in syncable prefs. If it has already | 419 // This runs as a one-time fix, tracked in syncable prefs. If it has already |
| 434 // run, it is a NOP (other than checking the pref). | 420 // run, it is a NOP (other than checking the pref). |
| 435 ApplyProfileUseDatesFix(); | 421 ApplyProfileUseDatesFix(); |
| 436 | 422 |
| (...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 746 return GetProfiles(false); | 732 return GetProfiles(false); |
| 747 } | 733 } |
| 748 | 734 |
| 749 std::vector<AutofillProfile*> PersonalDataManager::web_profiles() const { | 735 std::vector<AutofillProfile*> PersonalDataManager::web_profiles() const { |
| 750 std::vector<AutofillProfile*> result; | 736 std::vector<AutofillProfile*> result; |
| 751 for (const auto& profile : web_profiles_) | 737 for (const auto& profile : web_profiles_) |
| 752 result.push_back(profile.get()); | 738 result.push_back(profile.get()); |
| 753 return result; | 739 return result; |
| 754 } | 740 } |
| 755 | 741 |
| 742 std::vector<AutofillProfile*> PersonalDataManager::server_profiles() const { | |
| 743 std::vector<AutofillProfile*> result; | |
| 744 for (const auto& profile : server_profiles_) | |
| 745 result.push_back(profile.get()); | |
| 746 return result; | |
| 747 } | |
| 748 | |
| 756 std::vector<CreditCard*> PersonalDataManager::GetLocalCreditCards() const { | 749 std::vector<CreditCard*> PersonalDataManager::GetLocalCreditCards() const { |
| 757 std::vector<CreditCard*> result; | 750 std::vector<CreditCard*> result; |
| 758 for (const auto& card : local_credit_cards_) | 751 for (const auto& card : local_credit_cards_) |
| 759 result.push_back(card.get()); | 752 result.push_back(card.get()); |
| 760 return result; | 753 return result; |
| 761 } | 754 } |
| 762 | 755 |
| 763 const std::vector<CreditCard*>& PersonalDataManager::GetCreditCards() const { | 756 const std::vector<CreditCard*>& PersonalDataManager::GetCreditCards() const { |
| 764 credit_cards_.clear(); | 757 credit_cards_.clear(); |
| 765 for (const auto& card : local_credit_cards_) | 758 for (const auto& card : local_credit_cards_) |
| 766 credit_cards_.push_back(card.get()); | 759 credit_cards_.push_back(card.get()); |
| 767 if (pref_service_->GetBoolean(prefs::kAutofillWalletImportEnabled)) { | 760 if (pref_service_->GetBoolean(prefs::kAutofillWalletImportEnabled)) { |
| 768 for (const auto& card : server_credit_cards_) | 761 for (const auto& card : server_credit_cards_) |
| 769 credit_cards_.push_back(card.get()); | 762 credit_cards_.push_back(card.get()); |
| 770 } | 763 } |
| 771 return credit_cards_; | 764 return credit_cards_; |
| 772 } | 765 } |
| 773 | 766 |
| 774 bool PersonalDataManager::HasServerData() const { | 767 bool PersonalDataManager::HasServerData() const { |
|
Mathieu
2017/02/02 01:22:52
This seems unused. Delete?
sebsg
2017/02/02 15:18:51
Done.
| |
| 775 return !server_credit_cards_.empty() || !server_profiles_.empty(); | 768 return !server_credit_cards_.empty() || !server_profiles_.empty(); |
| 776 } | 769 } |
| 777 | 770 |
| 778 void PersonalDataManager::Refresh() { | 771 void PersonalDataManager::Refresh() { |
| 779 LoadProfiles(); | 772 LoadProfiles(); |
| 780 LoadCreditCards(); | 773 LoadCreditCards(); |
| 781 } | 774 } |
| 782 | 775 |
| 783 const std::vector<AutofillProfile*> PersonalDataManager::GetProfilesToSuggest() | 776 const std::vector<AutofillProfile*> PersonalDataManager::GetProfilesToSuggest() |
| 784 const { | 777 const { |
| (...skipping 448 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1233 database_->CancelRequest(*handle); | 1226 database_->CancelRequest(*handle); |
| 1234 } | 1227 } |
| 1235 *handle = 0; | 1228 *handle = 0; |
| 1236 } | 1229 } |
| 1237 | 1230 |
| 1238 std::string PersonalDataManager::SaveImportedProfile( | 1231 std::string PersonalDataManager::SaveImportedProfile( |
| 1239 const AutofillProfile& imported_profile) { | 1232 const AutofillProfile& imported_profile) { |
| 1240 if (is_off_the_record_) | 1233 if (is_off_the_record_) |
| 1241 return std::string(); | 1234 return std::string(); |
| 1242 | 1235 |
| 1243 // Don't save a web profile if the data in the profile is a subset of a | |
| 1244 // server profile, but do record the fact that it was used. | |
| 1245 for (const auto& profile : server_profiles_) { | |
| 1246 if (imported_profile.IsSubsetOf(*profile, app_locale_)) { | |
| 1247 RecordUseOf(*profile); | |
| 1248 return profile->guid(); | |
| 1249 } | |
| 1250 } | |
| 1251 | |
| 1252 std::vector<AutofillProfile> profiles; | 1236 std::vector<AutofillProfile> profiles; |
| 1253 std::string guid = | 1237 std::string guid = |
| 1254 MergeProfile(imported_profile, &web_profiles_, app_locale_, &profiles); | 1238 MergeProfile(imported_profile, &web_profiles_, app_locale_, &profiles); |
| 1255 SetProfiles(&profiles); | 1239 SetProfiles(&profiles); |
| 1256 return guid; | 1240 return guid; |
| 1257 } | 1241 } |
| 1258 | 1242 |
| 1259 void PersonalDataManager::NotifyPersonalDataChanged() { | 1243 void PersonalDataManager::NotifyPersonalDataChanged() { |
| 1260 for (PersonalDataManagerObserver& observer : observers_) | 1244 for (PersonalDataManagerObserver& observer : observers_) |
| 1261 observer.OnPersonalDataChanged(); | 1245 observer.OnPersonalDataChanged(); |
| 1246 | |
| 1247 // If new data was synced, try to convert new server profiles. | |
| 1248 if (has_synced_new_data) { | |
| 1249 has_synced_new_data = false; | |
| 1250 ConvertWalletAddressesToLocalProfiles(); | |
| 1251 } | |
| 1262 } | 1252 } |
| 1263 | 1253 |
| 1264 std::string PersonalDataManager::SaveImportedCreditCard( | 1254 std::string PersonalDataManager::SaveImportedCreditCard( |
| 1265 const CreditCard& imported_card) { | 1255 const CreditCard& imported_card) { |
| 1266 DCHECK(!imported_card.number().empty()); | 1256 DCHECK(!imported_card.number().empty()); |
| 1267 if (is_off_the_record_) | 1257 if (is_off_the_record_) |
| 1268 return std::string(); | 1258 return std::string(); |
| 1269 | 1259 |
| 1270 // Set to true if |imported_card| is merged into the credit card list. | 1260 // Set to true if |imported_card| is merged into the credit card list. |
| 1271 bool merged = false; | 1261 bool merged = false; |
| (...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1569 | 1559 |
| 1570 imported_credit_card->reset(new CreditCard(candidate_credit_card)); | 1560 imported_credit_card->reset(new CreditCard(candidate_credit_card)); |
| 1571 return true; | 1561 return true; |
| 1572 } | 1562 } |
| 1573 | 1563 |
| 1574 const std::vector<AutofillProfile*>& PersonalDataManager::GetProfiles( | 1564 const std::vector<AutofillProfile*>& PersonalDataManager::GetProfiles( |
| 1575 bool record_metrics) const { | 1565 bool record_metrics) const { |
| 1576 profiles_.clear(); | 1566 profiles_.clear(); |
| 1577 for (const auto& profile : web_profiles_) | 1567 for (const auto& profile : web_profiles_) |
| 1578 profiles_.push_back(profile.get()); | 1568 profiles_.push_back(profile.get()); |
| 1579 if (pref_service_->GetBoolean(prefs::kAutofillWalletImportEnabled)) { | |
| 1580 for (const auto& profile : server_profiles_) | |
| 1581 profiles_.push_back(profile.get()); | |
| 1582 } | |
| 1583 return profiles_; | 1569 return profiles_; |
| 1584 } | 1570 } |
| 1585 | 1571 |
| 1586 std::vector<Suggestion> PersonalDataManager::GetSuggestionsForCards( | 1572 std::vector<Suggestion> PersonalDataManager::GetSuggestionsForCards( |
| 1587 const AutofillType& type, | 1573 const AutofillType& type, |
| 1588 const base::string16& field_contents, | 1574 const base::string16& field_contents, |
| 1589 const std::vector<CreditCard*>& cards_to_suggest) const { | 1575 const std::vector<CreditCard*>& cards_to_suggest) const { |
| 1590 std::vector<Suggestion> suggestions; | 1576 std::vector<Suggestion> suggestions; |
| 1591 base::string16 field_contents_lower = base::i18n::ToLower(field_contents); | 1577 base::string16 field_contents_lower = base::i18n::ToLower(field_contents); |
| 1592 for (const CreditCard* credit_card : cards_to_suggest) { | 1578 for (const CreditCard* credit_card : cards_to_suggest) { |
| (...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1850 // If the card was modified, apply the changes to the database. | 1836 // If the card was modified, apply the changes to the database. |
| 1851 if (was_modified) { | 1837 if (was_modified) { |
| 1852 if (credit_card->record_type() == CreditCard::LOCAL_CARD) | 1838 if (credit_card->record_type() == CreditCard::LOCAL_CARD) |
| 1853 database_->UpdateCreditCard(*credit_card); | 1839 database_->UpdateCreditCard(*credit_card); |
| 1854 else | 1840 else |
| 1855 database_->UpdateServerCardMetadata(*credit_card); | 1841 database_->UpdateServerCardMetadata(*credit_card); |
| 1856 } | 1842 } |
| 1857 } | 1843 } |
| 1858 } | 1844 } |
| 1859 | 1845 |
| 1846 void PersonalDataManager::ConvertWalletAddressesToLocalProfiles() { | |
| 1847 // Put the local profiles into a vector<AutofillProfile>. Theses are the | |
|
Mathieu
2017/02/02 01:22:52
Put -> Copy
sebsg
2017/02/02 15:18:51
Done.
| |
| 1848 // existing profiles. Get them sorted in decreasing order of frecency, so the | |
| 1849 // "best" profiles are checked first. Put the verified profiles last so the | |
| 1850 // server addresses have a chance to merge into the non-verified local | |
| 1851 // profiles. | |
| 1852 std::vector<AutofillProfile> local_profiles; | |
| 1853 for (auto* existing_profile : GetProfilesToSuggest()) { | |
|
Mathieu
2017/02/02 01:22:52
auto -> AutofillProfile?
sebsg
2017/02/02 15:18:51
Done.
| |
| 1854 local_profiles.push_back(*existing_profile); | |
| 1855 } | |
| 1856 | |
| 1857 // Create the map used to update credit card's billing addresses after the | |
| 1858 // convertion/merge. | |
| 1859 std::unordered_map<std::string, std::string> guids_merge_map; | |
| 1860 | |
| 1861 bool has_converted_addresses = false; | |
| 1862 for (auto& wallet_address : server_profiles_) { | |
|
Mathieu
2017/02/02 01:22:52
avoid using auto if the type is straightforward
sebsg
2017/02/02 15:18:51
Done.
| |
| 1863 // If the address has not been converted yet, convert it. | |
| 1864 if (!wallet_address->has_converted()) { | |
| 1865 // Try to merge the server address into a similar local profile, or create | |
| 1866 // a new local profile if no similar profile is found. | |
| 1867 std::string address_guid = MergeServerAddressesIntoLocalProfiles( | |
|
Mathieu
2017/02/02 01:22:52
MergeServerAddressIntoProfiles?
sebsg
2017/02/02 15:18:51
Done.
| |
| 1868 *wallet_address, &local_profiles); | |
| 1869 | |
| 1870 // Update the map to transfer the billing address relationship from the | |
| 1871 // server address to the converted/merged local profile. | |
| 1872 guids_merge_map.insert(std::pair<std::string, std::string>( | |
| 1873 wallet_address->server_id(), address_guid)); | |
| 1874 | |
| 1875 // Update the wallet addresses metadata to record the conversion. | |
| 1876 wallet_address->set_has_converted(true); | |
| 1877 database_->UpdateServerAddressMetadata(*wallet_address); | |
| 1878 | |
| 1879 has_converted_addresses = true; | |
| 1880 } | |
| 1881 } | |
| 1882 | |
| 1883 if (has_converted_addresses) { | |
| 1884 // Save the local profiles to the DB. | |
| 1885 SetProfiles(&local_profiles); | |
| 1886 | |
| 1887 // Update the credit cards billing address relationship. | |
| 1888 UpdateCardsBillingAddressReference(guids_merge_map); | |
| 1889 | |
| 1890 // Force a reload of the profiles and cards. | |
| 1891 Refresh(); | |
| 1892 } | |
| 1893 } | |
| 1894 | |
| 1895 std::string PersonalDataManager::MergeServerAddressesIntoLocalProfiles( | |
| 1896 const AutofillProfile& converted_server_address, | |
|
Mathieu
2017/02/02 01:22:52
It's not actually converted yet. |server_address|?
sebsg
2017/02/02 15:18:51
Done.
| |
| 1897 std::vector<AutofillProfile>* local_profiles) { | |
|
Mathieu
2017/02/02 01:22:52
I actually like |existing_profiles|
sebsg
2017/02/02 15:18:51
Done.
| |
| 1898 // Set to true if |existing_profiles| already contains an equivalent profile. | |
| 1899 bool matching_profile_found = false; | |
| 1900 std::string guid = converted_server_address.guid(); | |
| 1901 | |
| 1902 // If there is already a local profile that is very similar, merge in any | |
| 1903 // missing values. Only merge with the first match. | |
| 1904 AutofillProfileComparator comparator(app_locale_); | |
|
Mathieu
2017/02/02 01:22:52
It looks like you took this from https://cs.chromi
sebsg
2017/02/02 15:18:51
Yeah I thought about it for a while but the origin
| |
| 1905 for (auto& local_profile : *local_profiles) { | |
| 1906 if (!matching_profile_found && | |
| 1907 comparator.AreMergeable(converted_server_address, local_profile) && | |
| 1908 local_profile.SaveAdditionalInfo(converted_server_address, | |
| 1909 app_locale_)) { | |
| 1910 matching_profile_found = true; | |
| 1911 local_profile.set_modification_date(base::Time::Now()); | |
| 1912 guid = local_profile.guid(); | |
| 1913 AutofillMetrics::LogWalletAddressConversionType( | |
| 1914 AutofillMetrics::CONVERTED_ADDRESS_MERGED); | |
| 1915 } | |
| 1916 } | |
| 1917 | |
| 1918 // If the server address was not merged with a local profile, add it to the | |
| 1919 // list. | |
| 1920 if (!matching_profile_found) { | |
| 1921 local_profiles->push_back(converted_server_address); | |
| 1922 // Set the profile as being local. | |
| 1923 local_profiles->back().set_record_type(AutofillProfile::LOCAL_PROFILE); | |
| 1924 local_profiles->back().set_modification_date(base::Time::Now()); | |
|
Mathieu
2017/02/02 01:22:52
Are we advising people to use AutofillClock now?
sebsg
2017/02/02 15:18:51
It just became available with the Revase (Patch2)
| |
| 1925 AutofillMetrics::LogWalletAddressConversionType( | |
| 1926 AutofillMetrics::CONVERTED_ADDRESS_ADDED); | |
| 1927 } | |
| 1928 | |
| 1929 return guid; | |
| 1930 } | |
| 1931 | |
| 1860 } // namespace autofill | 1932 } // namespace autofill |
| OLD | NEW |