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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 48 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_da ta.h" | 48 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_da ta.h" |
| 49 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_fo rmatter.h" | 49 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_fo rmatter.h" |
| 50 | 50 |
| 51 namespace autofill { | 51 namespace autofill { |
| 52 namespace { | 52 namespace { |
| 53 | 53 |
| 54 using ::i18n::addressinput::AddressField; | 54 using ::i18n::addressinput::AddressField; |
| 55 using ::i18n::addressinput::GetStreetAddressLinesAsSingleLine; | 55 using ::i18n::addressinput::GetStreetAddressLinesAsSingleLine; |
| 56 using ::i18n::addressinput::STREET_ADDRESS; | 56 using ::i18n::addressinput::STREET_ADDRESS; |
| 57 | 57 |
| 58 const int LOCAL_GUID_LENGTH = 36; | |
|
Mathieu
2017/03/06 20:40:34
comment?
sebsg
2017/03/06 21:26:40
Done.
| |
| 59 | |
| 58 template<typename T> | 60 template<typename T> |
| 59 class FormGroupMatchesByGUIDFunctor { | 61 class FormGroupMatchesByGUIDFunctor { |
| 60 public: | 62 public: |
| 61 explicit FormGroupMatchesByGUIDFunctor(const std::string& guid) | 63 explicit FormGroupMatchesByGUIDFunctor(const std::string& guid) |
| 62 : guid_(guid) { | 64 : guid_(guid) { |
| 63 } | 65 } |
| 64 | 66 |
| 65 bool operator()(const T& form_group) { | 67 bool operator()(const T& form_group) { |
| 66 return form_group.guid() == guid_; | 68 return form_group.guid() == guid_; |
| 67 } | 69 } |
| (...skipping 1171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1239 std::string guid = | 1241 std::string guid = |
| 1240 MergeProfile(imported_profile, &web_profiles_, app_locale_, &profiles); | 1242 MergeProfile(imported_profile, &web_profiles_, app_locale_, &profiles); |
| 1241 SetProfiles(&profiles); | 1243 SetProfiles(&profiles); |
| 1242 return guid; | 1244 return guid; |
| 1243 } | 1245 } |
| 1244 | 1246 |
| 1245 void PersonalDataManager::NotifyPersonalDataChanged() { | 1247 void PersonalDataManager::NotifyPersonalDataChanged() { |
| 1246 for (PersonalDataManagerObserver& observer : observers_) | 1248 for (PersonalDataManagerObserver& observer : observers_) |
| 1247 observer.OnPersonalDataChanged(); | 1249 observer.OnPersonalDataChanged(); |
| 1248 | 1250 |
| 1249 // If new data was synced, try to convert new server profiles. | 1251 // If new data was synced, try to convert new server profiles. |
|
Mathieu
2017/03/06 20:40:34
update comment
sebsg
2017/03/06 21:26:39
Done.
| |
| 1250 if (has_synced_new_data_) { | 1252 if (has_synced_new_data_) { |
| 1251 has_synced_new_data_ = false; | 1253 has_synced_new_data_ = false; |
| 1252 ConvertWalletAddressesToLocalProfiles(); | 1254 ConvertWalletAddressesAndUpdateWalletCards(); |
| 1253 } | 1255 } |
| 1254 } | 1256 } |
| 1255 | 1257 |
| 1256 std::string PersonalDataManager::SaveImportedCreditCard( | 1258 std::string PersonalDataManager::SaveImportedCreditCard( |
| 1257 const CreditCard& imported_card) { | 1259 const CreditCard& imported_card) { |
| 1258 DCHECK(!imported_card.number().empty()); | 1260 DCHECK(!imported_card.number().empty()); |
| 1259 if (is_off_the_record_) | 1261 if (is_off_the_record_) |
| 1260 return std::string(); | 1262 return std::string(); |
| 1261 | 1263 |
| 1262 // Set to true if |imported_card| is merged into the credit card list. | 1264 // Set to true if |imported_card| is merged into the credit card list. |
| (...skipping 580 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1843 // If the card was modified, apply the changes to the database. | 1845 // If the card was modified, apply the changes to the database. |
| 1844 if (was_modified) { | 1846 if (was_modified) { |
| 1845 if (credit_card->record_type() == CreditCard::LOCAL_CARD) | 1847 if (credit_card->record_type() == CreditCard::LOCAL_CARD) |
| 1846 database_->UpdateCreditCard(*credit_card); | 1848 database_->UpdateCreditCard(*credit_card); |
| 1847 else | 1849 else |
| 1848 database_->UpdateServerCardMetadata(*credit_card); | 1850 database_->UpdateServerCardMetadata(*credit_card); |
| 1849 } | 1851 } |
| 1850 } | 1852 } |
| 1851 } | 1853 } |
| 1852 | 1854 |
| 1853 void PersonalDataManager::ConvertWalletAddressesToLocalProfiles() { | 1855 void PersonalDataManager::ConvertWalletAddressesAndUpdateWalletCards() { |
| 1854 // Copy the local profiles into a vector<AutofillProfile>. Theses are the | 1856 // Copy the local profiles into a vector<AutofillProfile>. Theses are the |
| 1855 // existing profiles. Get them sorted in decreasing order of frecency, so the | 1857 // existing profiles. Get them sorted in decreasing order of frecency, so the |
| 1856 // "best" profiles are checked first. Put the verified profiles last so the | 1858 // "best" profiles are checked first. Put the verified profiles last so the |
| 1857 // server addresses have a chance to merge into the non-verified local | 1859 // server addresses have a chance to merge into the non-verified local |
| 1858 // profiles. | 1860 // profiles. |
| 1859 std::vector<AutofillProfile> local_profiles; | 1861 std::vector<AutofillProfile> local_profiles; |
| 1860 for (AutofillProfile* existing_profile : GetProfilesToSuggest()) { | 1862 for (AutofillProfile* existing_profile : GetProfilesToSuggest()) { |
| 1861 local_profiles.push_back(*existing_profile); | 1863 local_profiles.push_back(*existing_profile); |
| 1862 } | 1864 } |
| 1863 | 1865 |
| 1866 // Since we are already iterating on all the server profiles to convert Wallet | |
| 1867 // address and we will need to access them by guid later to update the Wallet | |
|
Mathieu
2017/03/06 20:40:34
*addresses
sebsg
2017/03/06 21:26:39
Done.
| |
| 1868 // cards, create a map here. | |
| 1869 std::unordered_map<std::string, AutofillProfile*> server_profiles_map; | |
|
Mathieu
2017/03/06 20:40:34
|server_id_profile_map| is clearer?
sebsg
2017/03/06 21:26:39
Done.
| |
| 1870 | |
| 1864 // Create the map used to update credit card's billing addresses after the | 1871 // Create the map used to update credit card's billing addresses after the |
| 1865 // convertion/merge. | 1872 // convertion/merge. |
| 1866 std::unordered_map<std::string, std::string> guids_merge_map; | 1873 std::unordered_map<std::string, std::string> guids_merge_map; |
| 1867 | 1874 |
| 1875 bool has_converted_addresses = ConvertWalletAddressesToLocalProfiles( | |
| 1876 &local_profiles, &server_profiles_map, &guids_merge_map); | |
| 1877 bool should_update_cards = UpdateWalletCardsAlreadyConvertedBillingAddresses( | |
| 1878 &local_profiles, &server_profiles_map, &guids_merge_map); | |
| 1879 | |
| 1880 if (has_converted_addresses) { | |
| 1881 // Save the local profiles to the DB. | |
| 1882 SetProfiles(&local_profiles); | |
| 1883 } | |
| 1884 | |
| 1885 if (should_update_cards || has_converted_addresses) { | |
| 1886 // Update the credit cards billing address relationship. | |
| 1887 UpdateCardsBillingAddressReference(guids_merge_map); | |
| 1888 | |
| 1889 // Force a reload of the profiles and cards. | |
| 1890 Refresh(); | |
| 1891 } | |
| 1892 } | |
| 1893 | |
| 1894 bool PersonalDataManager::ConvertWalletAddressesToLocalProfiles( | |
| 1895 std::vector<AutofillProfile>* local_profiles, | |
| 1896 std::unordered_map<std::string, AutofillProfile*>* server_profiles_map, | |
| 1897 std::unordered_map<std::string, std::string>* guids_merge_map) { | |
| 1868 bool has_converted_addresses = false; | 1898 bool has_converted_addresses = false; |
| 1869 for (std::unique_ptr<AutofillProfile>& wallet_address : server_profiles_) { | 1899 for (std::unique_ptr<AutofillProfile>& wallet_address : server_profiles_) { |
| 1900 // Add the profile to the map. | |
| 1901 server_profiles_map->insert(std::pair<std::string, AutofillProfile*>( | |
|
Mathieu
2017/03/06 20:40:34
std::make_pair?
sebsg
2017/03/06 21:26:40
Done.
| |
| 1902 wallet_address->server_id(), wallet_address.get())); | |
| 1903 | |
| 1870 // If the address has not been converted yet, convert it. | 1904 // If the address has not been converted yet, convert it. |
| 1871 if (!wallet_address->has_converted()) { | 1905 if (!wallet_address->has_converted()) { |
| 1872 // Try to merge the server address into a similar local profile, or create | 1906 // Try to merge the server address into a similar local profile, or create |
| 1873 // a new local profile if no similar profile is found. | 1907 // a new local profile if no similar profile is found. |
| 1874 std::string address_guid = | 1908 std::string address_guid = |
| 1875 MergeServerAddressesIntoProfiles(*wallet_address, &local_profiles); | 1909 MergeServerAddressesIntoProfiles(*wallet_address, local_profiles); |
| 1876 | 1910 |
| 1877 // Update the map to transfer the billing address relationship from the | 1911 // Update the map to transfer the billing address relationship from the |
| 1878 // server address to the converted/merged local profile. | 1912 // server address to the converted/merged local profile. |
| 1879 guids_merge_map.insert(std::pair<std::string, std::string>( | 1913 guids_merge_map->insert(std::pair<std::string, std::string>( |
| 1880 wallet_address->server_id(), address_guid)); | 1914 wallet_address->server_id(), address_guid)); |
| 1881 | 1915 |
| 1882 // Update the wallet addresses metadata to record the conversion. | 1916 // Update the wallet addresses metadata to record the conversion. |
| 1883 wallet_address->set_has_converted(true); | 1917 wallet_address->set_has_converted(true); |
| 1884 database_->UpdateServerAddressMetadata(*wallet_address); | 1918 database_->UpdateServerAddressMetadata(*wallet_address); |
| 1885 | 1919 |
| 1886 has_converted_addresses = true; | 1920 has_converted_addresses = true; |
| 1887 } | 1921 } |
| 1888 } | 1922 } |
| 1889 | 1923 |
| 1890 if (has_converted_addresses) { | 1924 return has_converted_addresses; |
| 1891 // Save the local profiles to the DB. | 1925 } |
| 1892 SetProfiles(&local_profiles); | |
| 1893 | 1926 |
| 1894 // Update the credit cards billing address relationship. | 1927 bool PersonalDataManager::UpdateWalletCardsAlreadyConvertedBillingAddresses( |
| 1895 UpdateCardsBillingAddressReference(guids_merge_map); | 1928 std::vector<AutofillProfile>* local_profiles, |
| 1929 std::unordered_map<std::string, AutofillProfile*>* server_profiles_map, | |
| 1930 std::unordered_map<std::string, std::string>* guids_merge_map) { | |
| 1931 // Look for server cards that still refer to server addresses but for which | |
| 1932 // there is no mapping. This can happen if it's a new card for which the | |
| 1933 // billing address has already been converted. This should be a no-op for most | |
| 1934 // situations. Otherwise, it should affect only one Wallet card, sinces users | |
| 1935 // do not add a lot of credit cards. | |
| 1936 AutofillProfileComparator comparator(app_locale_); | |
| 1937 bool should_update_cards = false; | |
| 1938 for (std::unique_ptr<CreditCard>& wallet_card : server_credit_cards_) { | |
| 1939 std::string billing_address_id = wallet_card->billing_address_id(); | |
| 1896 | 1940 |
| 1897 // Force a reload of the profiles and cards. | 1941 // If billing address refers to a server id and that id is not a key in the |
| 1898 Refresh(); | 1942 // guids_merge_map, it means that the card is new but the address was |
| 1943 // already converted. Look for the matching converted profile. | |
| 1944 if (!billing_address_id.empty() && | |
| 1945 billing_address_id.length() != LOCAL_GUID_LENGTH && | |
| 1946 guids_merge_map->find(billing_address_id) == guids_merge_map->end()) { | |
| 1947 // Get the profile. | |
| 1948 auto it = server_profiles_map->find(billing_address_id); | |
| 1949 DCHECK(it != server_profiles_map->end()); | |
| 1950 if (it != server_profiles_map->end()) { | |
| 1951 AutofillProfile* billing_address = it->second; | |
| 1952 | |
| 1953 // Look for a matching local profile (DO NOT MERGE). | |
| 1954 bool matching_profile_found = false; | |
| 1955 for (auto& local_profile : *local_profiles) { | |
| 1956 if (!matching_profile_found && | |
| 1957 comparator.AreMergeable(*billing_address, local_profile)) { | |
| 1958 matching_profile_found = true; | |
| 1959 | |
| 1960 // The Wallet address matches this local profile. Add this to the | |
| 1961 // merge mapping. | |
| 1962 guids_merge_map->insert(std::pair<std::string, std::string>( | |
| 1963 billing_address_id, local_profile.guid())); | |
| 1964 should_update_cards = true; | |
| 1965 } | |
| 1966 } | |
| 1967 } | |
| 1968 } | |
| 1899 } | 1969 } |
| 1970 | |
| 1971 return should_update_cards; | |
| 1900 } | 1972 } |
| 1901 | 1973 |
| 1902 // TODO(crbug.com/687975): Reuse MergeProfiles in this function. | 1974 // TODO(crbug.com/687975): Reuse MergeProfiles in this function. |
| 1903 std::string PersonalDataManager::MergeServerAddressesIntoProfiles( | 1975 std::string PersonalDataManager::MergeServerAddressesIntoProfiles( |
| 1904 const AutofillProfile& server_address, | 1976 const AutofillProfile& server_address, |
| 1905 std::vector<AutofillProfile>* existing_profiles) { | 1977 std::vector<AutofillProfile>* existing_profiles) { |
| 1906 // Set to true if |existing_profiles| already contains an equivalent profile. | 1978 // Set to true if |existing_profiles| already contains an equivalent profile. |
| 1907 bool matching_profile_found = false; | 1979 bool matching_profile_found = false; |
| 1908 std::string guid = server_address.guid(); | 1980 std::string guid = server_address.guid(); |
| 1909 | 1981 |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 1939 existing_profiles->back().SetRawInfo(EMAIL_ADDRESS, email); | 2011 existing_profiles->back().SetRawInfo(EMAIL_ADDRESS, email); |
| 1940 | 2012 |
| 1941 AutofillMetrics::LogWalletAddressConversionType( | 2013 AutofillMetrics::LogWalletAddressConversionType( |
| 1942 AutofillMetrics::CONVERTED_ADDRESS_ADDED); | 2014 AutofillMetrics::CONVERTED_ADDRESS_ADDED); |
| 1943 } | 2015 } |
| 1944 | 2016 |
| 1945 return guid; | 2017 return guid; |
| 1946 } | 2018 } |
| 1947 | 2019 |
| 1948 } // namespace autofill | 2020 } // namespace autofill |
| OLD | NEW |