| 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 const std::vector<std::unique_ptr<AutofillProfile>>& |
| 743 PersonalDataManager::server_profiles() const { |
| 744 return server_profiles_; |
| 745 } |
| 746 |
| 756 std::vector<CreditCard*> PersonalDataManager::GetLocalCreditCards() const { | 747 std::vector<CreditCard*> PersonalDataManager::GetLocalCreditCards() const { |
| 757 std::vector<CreditCard*> result; | 748 std::vector<CreditCard*> result; |
| 758 for (const auto& card : local_credit_cards_) | 749 for (const auto& card : local_credit_cards_) |
| 759 result.push_back(card.get()); | 750 result.push_back(card.get()); |
| 760 return result; | 751 return result; |
| 761 } | 752 } |
| 762 | 753 |
| 763 const std::vector<CreditCard*>& PersonalDataManager::GetCreditCards() const { | 754 const std::vector<CreditCard*>& PersonalDataManager::GetCreditCards() const { |
| 764 credit_cards_.clear(); | 755 credit_cards_.clear(); |
| 765 for (const auto& card : local_credit_cards_) | 756 for (const auto& card : local_credit_cards_) |
| 766 credit_cards_.push_back(card.get()); | 757 credit_cards_.push_back(card.get()); |
| 767 if (pref_service_->GetBoolean(prefs::kAutofillWalletImportEnabled)) { | 758 if (pref_service_->GetBoolean(prefs::kAutofillWalletImportEnabled)) { |
| 768 for (const auto& card : server_credit_cards_) | 759 for (const auto& card : server_credit_cards_) |
| 769 credit_cards_.push_back(card.get()); | 760 credit_cards_.push_back(card.get()); |
| 770 } | 761 } |
| 771 return credit_cards_; | 762 return credit_cards_; |
| 772 } | 763 } |
| 773 | 764 |
| 774 bool PersonalDataManager::HasServerData() const { | |
| 775 return !server_credit_cards_.empty() || !server_profiles_.empty(); | |
| 776 } | |
| 777 | |
| 778 void PersonalDataManager::Refresh() { | 765 void PersonalDataManager::Refresh() { |
| 779 LoadProfiles(); | 766 LoadProfiles(); |
| 780 LoadCreditCards(); | 767 LoadCreditCards(); |
| 781 } | 768 } |
| 782 | 769 |
| 783 const std::vector<AutofillProfile*> PersonalDataManager::GetProfilesToSuggest() | 770 const std::vector<AutofillProfile*> PersonalDataManager::GetProfilesToSuggest() |
| 784 const { | 771 const { |
| 785 std::vector<AutofillProfile*> profiles = GetProfiles(true); | 772 std::vector<AutofillProfile*> profiles = GetProfiles(true); |
| 786 | 773 |
| 787 // Rank the suggestions by frecency (see AutofillDataModel for details). | 774 // Rank the suggestions by frecency (see AutofillDataModel for details). |
| (...skipping 445 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1233 database_->CancelRequest(*handle); | 1220 database_->CancelRequest(*handle); |
| 1234 } | 1221 } |
| 1235 *handle = 0; | 1222 *handle = 0; |
| 1236 } | 1223 } |
| 1237 | 1224 |
| 1238 std::string PersonalDataManager::SaveImportedProfile( | 1225 std::string PersonalDataManager::SaveImportedProfile( |
| 1239 const AutofillProfile& imported_profile) { | 1226 const AutofillProfile& imported_profile) { |
| 1240 if (is_off_the_record_) | 1227 if (is_off_the_record_) |
| 1241 return std::string(); | 1228 return std::string(); |
| 1242 | 1229 |
| 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; | 1230 std::vector<AutofillProfile> profiles; |
| 1253 std::string guid = | 1231 std::string guid = |
| 1254 MergeProfile(imported_profile, &web_profiles_, app_locale_, &profiles); | 1232 MergeProfile(imported_profile, &web_profiles_, app_locale_, &profiles); |
| 1255 SetProfiles(&profiles); | 1233 SetProfiles(&profiles); |
| 1256 return guid; | 1234 return guid; |
| 1257 } | 1235 } |
| 1258 | 1236 |
| 1259 void PersonalDataManager::NotifyPersonalDataChanged() { | 1237 void PersonalDataManager::NotifyPersonalDataChanged() { |
| 1260 for (PersonalDataManagerObserver& observer : observers_) | 1238 for (PersonalDataManagerObserver& observer : observers_) |
| 1261 observer.OnPersonalDataChanged(); | 1239 observer.OnPersonalDataChanged(); |
| 1240 |
| 1241 // If new data was synced, try to convert new server profiles. |
| 1242 if (has_synced_new_data) { |
| 1243 has_synced_new_data = false; |
| 1244 ConvertWalletAddressesToLocalProfiles(); |
| 1245 } |
| 1262 } | 1246 } |
| 1263 | 1247 |
| 1264 std::string PersonalDataManager::SaveImportedCreditCard( | 1248 std::string PersonalDataManager::SaveImportedCreditCard( |
| 1265 const CreditCard& imported_card) { | 1249 const CreditCard& imported_card) { |
| 1266 DCHECK(!imported_card.number().empty()); | 1250 DCHECK(!imported_card.number().empty()); |
| 1267 if (is_off_the_record_) | 1251 if (is_off_the_record_) |
| 1268 return std::string(); | 1252 return std::string(); |
| 1269 | 1253 |
| 1270 // Set to true if |imported_card| is merged into the credit card list. | 1254 // Set to true if |imported_card| is merged into the credit card list. |
| 1271 bool merged = false; | 1255 bool merged = false; |
| (...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1569 | 1553 |
| 1570 imported_credit_card->reset(new CreditCard(candidate_credit_card)); | 1554 imported_credit_card->reset(new CreditCard(candidate_credit_card)); |
| 1571 return true; | 1555 return true; |
| 1572 } | 1556 } |
| 1573 | 1557 |
| 1574 const std::vector<AutofillProfile*>& PersonalDataManager::GetProfiles( | 1558 const std::vector<AutofillProfile*>& PersonalDataManager::GetProfiles( |
| 1575 bool record_metrics) const { | 1559 bool record_metrics) const { |
| 1576 profiles_.clear(); | 1560 profiles_.clear(); |
| 1577 for (const auto& profile : web_profiles_) | 1561 for (const auto& profile : web_profiles_) |
| 1578 profiles_.push_back(profile.get()); | 1562 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_; | 1563 return profiles_; |
| 1584 } | 1564 } |
| 1585 | 1565 |
| 1586 std::vector<Suggestion> PersonalDataManager::GetSuggestionsForCards( | 1566 std::vector<Suggestion> PersonalDataManager::GetSuggestionsForCards( |
| 1587 const AutofillType& type, | 1567 const AutofillType& type, |
| 1588 const base::string16& field_contents, | 1568 const base::string16& field_contents, |
| 1589 const std::vector<CreditCard*>& cards_to_suggest) const { | 1569 const std::vector<CreditCard*>& cards_to_suggest) const { |
| 1590 std::vector<Suggestion> suggestions; | 1570 std::vector<Suggestion> suggestions; |
| 1591 base::string16 field_contents_lower = base::i18n::ToLower(field_contents); | 1571 base::string16 field_contents_lower = base::i18n::ToLower(field_contents); |
| 1592 for (const CreditCard* credit_card : cards_to_suggest) { | 1572 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. | 1830 // If the card was modified, apply the changes to the database. |
| 1851 if (was_modified) { | 1831 if (was_modified) { |
| 1852 if (credit_card->record_type() == CreditCard::LOCAL_CARD) | 1832 if (credit_card->record_type() == CreditCard::LOCAL_CARD) |
| 1853 database_->UpdateCreditCard(*credit_card); | 1833 database_->UpdateCreditCard(*credit_card); |
| 1854 else | 1834 else |
| 1855 database_->UpdateServerCardMetadata(*credit_card); | 1835 database_->UpdateServerCardMetadata(*credit_card); |
| 1856 } | 1836 } |
| 1857 } | 1837 } |
| 1858 } | 1838 } |
| 1859 | 1839 |
| 1840 void PersonalDataManager::ConvertWalletAddressesToLocalProfiles() { |
| 1841 // Copy the local profiles into a vector<AutofillProfile>. Theses are the |
| 1842 // existing profiles. Get them sorted in decreasing order of frecency, so the |
| 1843 // "best" profiles are checked first. Put the verified profiles last so the |
| 1844 // server addresses have a chance to merge into the non-verified local |
| 1845 // profiles. |
| 1846 std::vector<AutofillProfile> local_profiles; |
| 1847 for (AutofillProfile* existing_profile : GetProfilesToSuggest()) { |
| 1848 local_profiles.push_back(*existing_profile); |
| 1849 } |
| 1850 |
| 1851 // Create the map used to update credit card's billing addresses after the |
| 1852 // convertion/merge. |
| 1853 std::unordered_map<std::string, std::string> guids_merge_map; |
| 1854 |
| 1855 bool has_converted_addresses = false; |
| 1856 for (std::unique_ptr<AutofillProfile>& wallet_address : server_profiles_) { |
| 1857 // If the address has not been converted yet, convert it. |
| 1858 if (!wallet_address->has_converted()) { |
| 1859 // Try to merge the server address into a similar local profile, or create |
| 1860 // a new local profile if no similar profile is found. |
| 1861 std::string address_guid = |
| 1862 MergeServerAddressesIntoProfiles(*wallet_address, &local_profiles); |
| 1863 |
| 1864 // Update the map to transfer the billing address relationship from the |
| 1865 // server address to the converted/merged local profile. |
| 1866 guids_merge_map.insert(std::pair<std::string, std::string>( |
| 1867 wallet_address->server_id(), address_guid)); |
| 1868 |
| 1869 // Update the wallet addresses metadata to record the conversion. |
| 1870 wallet_address->set_has_converted(true); |
| 1871 database_->UpdateServerAddressMetadata(*wallet_address); |
| 1872 |
| 1873 has_converted_addresses = true; |
| 1874 } |
| 1875 } |
| 1876 |
| 1877 if (has_converted_addresses) { |
| 1878 // Save the local profiles to the DB. |
| 1879 SetProfiles(&local_profiles); |
| 1880 |
| 1881 // Update the credit cards billing address relationship. |
| 1882 UpdateCardsBillingAddressReference(guids_merge_map); |
| 1883 |
| 1884 // Force a reload of the profiles and cards. |
| 1885 Refresh(); |
| 1886 } |
| 1887 } |
| 1888 |
| 1889 // TODO(crbug.com/687975): Reuse MergeProfiles in this function. |
| 1890 std::string PersonalDataManager::MergeServerAddressesIntoProfiles( |
| 1891 const AutofillProfile& server_address, |
| 1892 std::vector<AutofillProfile>* existing_profiles) { |
| 1893 // Set to true if |existing_profiles| already contains an equivalent profile. |
| 1894 bool matching_profile_found = false; |
| 1895 std::string guid = server_address.guid(); |
| 1896 |
| 1897 // If there is already a local profile that is very similar, merge in any |
| 1898 // missing values. Only merge with the first match. |
| 1899 AutofillProfileComparator comparator(app_locale_); |
| 1900 for (auto& local_profile : *existing_profiles) { |
| 1901 if (!matching_profile_found && |
| 1902 comparator.AreMergeable(server_address, local_profile) && |
| 1903 local_profile.SaveAdditionalInfo(server_address, app_locale_)) { |
| 1904 matching_profile_found = true; |
| 1905 local_profile.set_modification_date(AutofillClock::Now()); |
| 1906 guid = local_profile.guid(); |
| 1907 AutofillMetrics::LogWalletAddressConversionType( |
| 1908 AutofillMetrics::CONVERTED_ADDRESS_MERGED); |
| 1909 } |
| 1910 } |
| 1911 |
| 1912 // If the server address was not merged with a local profile, add it to the |
| 1913 // list. |
| 1914 if (!matching_profile_found) { |
| 1915 existing_profiles->push_back(server_address); |
| 1916 // Set the profile as being local. |
| 1917 existing_profiles->back().set_record_type(AutofillProfile::LOCAL_PROFILE); |
| 1918 existing_profiles->back().set_modification_date(AutofillClock::Now()); |
| 1919 AutofillMetrics::LogWalletAddressConversionType( |
| 1920 AutofillMetrics::CONVERTED_ADDRESS_ADDED); |
| 1921 } |
| 1922 |
| 1923 return guid; |
| 1924 } |
| 1925 |
| 1860 } // namespace autofill | 1926 } // namespace autofill |
| OLD | NEW |