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/autofill_manager.h" | 5 #include "components/autofill/core/browser/autofill_manager.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 128 if (parts.size() == 3 && (parts[1].length() == 1 || | 128 if (parts.size() == 3 && (parts[1].length() == 1 || |
| 129 (parts[1].length() == 2 && | 129 (parts[1].length() == 2 && |
| 130 base::EndsWith(parts[1], base::ASCIIToUTF16("."), | 130 base::EndsWith(parts[1], base::ASCIIToUTF16("."), |
| 131 base::CompareCase::SENSITIVE)))) { | 131 base::CompareCase::SENSITIVE)))) { |
| 132 parts.erase(parts.begin() + 1); | 132 parts.erase(parts.begin() + 1); |
| 133 return base::JoinString(parts, base::ASCIIToUTF16(" ")); | 133 return base::JoinString(parts, base::ASCIIToUTF16(" ")); |
| 134 } | 134 } |
| 135 return name; | 135 return name; |
| 136 } | 136 } |
| 137 | 137 |
| 138 // Returns whether the |field| is predicted as being any kind of name. | |
| 139 bool IsNameType(const AutofillField& field) { | |
| 140 return field.Type().group() == NAME || field.Type().group() == NAME_BILLING || | |
| 141 field.Type().GetStorableType() == CREDIT_CARD_NAME_FULL || | |
| 142 field.Type().GetStorableType() == CREDIT_CARD_NAME_FIRST || | |
| 143 field.Type().GetStorableType() == CREDIT_CARD_NAME_LAST; | |
| 144 } | |
| 145 | |
| 146 // Selects the right name type from the |old_types| to insert into the | |
| 147 // |new_types| based on |is_credit_card|. | |
| 148 void SelectRightNameType(const ServerFieldTypeSet& old_types, | |
| 149 ServerFieldTypeSet* new_types, | |
| 150 bool is_credit_card) { | |
| 151 ServerFieldTypeSet upload_types; | |
| 152 if (old_types.count(NAME_FIRST) && old_types.count(CREDIT_CARD_NAME_FIRST)) { | |
| 153 if (is_credit_card) { | |
| 154 new_types->insert(CREDIT_CARD_NAME_FIRST); | |
| 155 } else { | |
| 156 new_types->insert(NAME_FIRST); | |
| 157 } | |
| 158 } else if (old_types.count(NAME_LAST) && | |
| 159 old_types.count(CREDIT_CARD_NAME_LAST)) { | |
| 160 if (is_credit_card) { | |
| 161 new_types->insert(CREDIT_CARD_NAME_LAST); | |
| 162 } else { | |
| 163 new_types->insert(NAME_LAST); | |
| 164 } | |
| 165 } else if (old_types.count(NAME_FULL) && | |
| 166 old_types.count(CREDIT_CARD_NAME_FULL)) { | |
| 167 if (is_credit_card) { | |
| 168 new_types->insert(CREDIT_CARD_NAME_FULL); | |
| 169 } else { | |
| 170 new_types->insert(NAME_FULL); | |
| 171 } | |
| 172 } else { | |
| 173 *new_types = old_types; | |
| 174 } | |
| 175 } | |
| 176 | |
| 138 } // namespace | 177 } // namespace |
| 139 | 178 |
| 140 AutofillManager::AutofillManager( | 179 AutofillManager::AutofillManager( |
| 141 AutofillDriver* driver, | 180 AutofillDriver* driver, |
| 142 AutofillClient* client, | 181 AutofillClient* client, |
| 143 const std::string& app_locale, | 182 const std::string& app_locale, |
| 144 AutofillDownloadManagerState enable_download_manager) | 183 AutofillDownloadManagerState enable_download_manager) |
| 145 : driver_(driver), | 184 : driver_(driver), |
| 146 client_(client), | 185 client_(client), |
| 147 payments_client_( | 186 payments_client_( |
| (...skipping 1638 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1786 for (const AutofillProfile& profile : profiles) | 1825 for (const AutofillProfile& profile : profiles) |
| 1787 profile.GetMatchingTypes(value, app_locale, &matching_types); | 1826 profile.GetMatchingTypes(value, app_locale, &matching_types); |
| 1788 for (const CreditCard& card : credit_cards) | 1827 for (const CreditCard& card : credit_cards) |
| 1789 card.GetMatchingTypes(value, app_locale, &matching_types); | 1828 card.GetMatchingTypes(value, app_locale, &matching_types); |
| 1790 | 1829 |
| 1791 if (matching_types.empty()) | 1830 if (matching_types.empty()) |
| 1792 matching_types.insert(UNKNOWN_TYPE); | 1831 matching_types.insert(UNKNOWN_TYPE); |
| 1793 | 1832 |
| 1794 field->set_possible_types(matching_types); | 1833 field->set_possible_types(matching_types); |
| 1795 } | 1834 } |
| 1835 | |
| 1836 AutofillManager::DisambiguateUploadTypes(submitted_form); | |
| 1837 } | |
| 1838 | |
| 1839 // static | |
| 1840 void AutofillManager::DisambiguateUploadTypes(FormStructure* form) { | |
| 1841 for (size_t i = 0; i < form->field_count(); ++i) { | |
| 1842 AutofillField* field = form->field(i); | |
| 1843 const ServerFieldTypeSet& upload_types = field->possible_types(); | |
| 1844 | |
| 1845 if (upload_types.size() == 2) { | |
| 1846 if (upload_types.count(ADDRESS_HOME_LINE1) && | |
| 1847 upload_types.count(ADDRESS_HOME_STREET_ADDRESS)) { | |
| 1848 AutofillManager::DisambiguateAddressUploadTypes(form, i); | |
| 1849 } else if (upload_types.count(PHONE_HOME_CITY_AND_NUMBER) && | |
| 1850 upload_types.count(PHONE_HOME_WHOLE_NUMBER)) { | |
| 1851 AutofillManager::DisambiguatePhoneUploadTypes(form, i); | |
| 1852 } else if ((upload_types.count(NAME_FULL) && | |
| 1853 upload_types.count(CREDIT_CARD_NAME_FULL)) || | |
| 1854 (upload_types.count(NAME_FIRST) && | |
| 1855 upload_types.count(CREDIT_CARD_NAME_FIRST)) || | |
| 1856 (upload_types.count(NAME_LAST) && | |
| 1857 upload_types.count(CREDIT_CARD_NAME_LAST))) { | |
| 1858 AutofillManager::DisambiguateNameUploadTypes(form, i, upload_types); | |
| 1859 } | |
| 1860 } | |
| 1861 } | |
| 1862 } | |
| 1863 | |
| 1864 // static | |
| 1865 void AutofillManager::DisambiguateAddressUploadTypes(FormStructure* form, | |
| 1866 size_t current_index) { | |
| 1867 // This case happens when the profile has only one address line. | |
| 1868 // Therefore the address line one and the street address (the whole | |
| 1869 // address) have the same value and match. | |
| 1870 | |
| 1871 // If the field is followed by a field that is predicted to be an | |
| 1872 // address line two and is empty, we can safely assume that this field | |
| 1873 // is an address line one field. Otherwise it's a whole address field. | |
| 1874 ServerFieldTypeSet matching_types; | |
| 1875 size_t next_index = current_index + 1; | |
| 1876 if (next_index < form->field_count() && | |
| 1877 form->field(next_index)->Type().GetStorableType() == ADDRESS_HOME_LINE2 && | |
| 1878 form->field(next_index)->possible_types().count(EMPTY_TYPE)) { | |
| 1879 matching_types.insert(ADDRESS_HOME_LINE1); | |
| 1880 } else { | |
| 1881 matching_types.insert(ADDRESS_HOME_STREET_ADDRESS); | |
| 1882 } | |
| 1883 | |
| 1884 AutofillField* field = form->field(current_index); | |
| 1885 field->set_possible_types(matching_types); | |
| 1886 } | |
| 1887 | |
| 1888 // static | |
| 1889 void AutofillManager::DisambiguatePhoneUploadTypes(FormStructure* form, | |
| 1890 size_t current_index) { | |
| 1891 // This case happens for profiles that have no country code saved. | |
| 1892 // Therefore, both the whole number and the city code and number have | |
| 1893 // the same value and match. | |
| 1894 | |
| 1895 // Since the form was submitted, it is safe to assume that the form | |
| 1896 // didn't require a country code. Thus, only PHONE_HOME_CITY_AND_NUMBER | |
| 1897 // needs to be uploaded. | |
| 1898 ServerFieldTypeSet matching_types; | |
| 1899 matching_types.insert(PHONE_HOME_CITY_AND_NUMBER); | |
| 1900 | |
| 1901 AutofillField* field = form->field(current_index); | |
| 1902 field->set_possible_types(matching_types); | |
| 1903 } | |
| 1904 | |
| 1905 // static | |
| 1906 void AutofillManager::DisambiguateNameUploadTypes( | |
| 1907 FormStructure* form, | |
| 1908 size_t current_index, | |
| 1909 const ServerFieldTypeSet& upload_types) { | |
| 1910 // This case happens when both a profile and a credit card have the same | |
| 1911 // name. | |
| 1912 | |
| 1913 // If the ambiguous field has either a previous or next field that is | |
| 1914 // not name related, use that information to determine whether the field | |
| 1915 // is a name or a credit card name. | |
| 1916 // If the ambiguous field has both a previous or next field that is not | |
| 1917 // name related, if they are both from the same group, use that group to | |
| 1918 // decide this field's type. Otherwise, there is no safe way to | |
| 1919 // disambiguate. | |
| 1920 | |
| 1921 // Look for a previous non name related field. | |
| 1922 bool has_found_previous_type = false; | |
| 1923 bool is_previous_credit_card = false; | |
| 1924 bool continue_search = true; | |
|
vabr (Chromium)
2016/03/22 17:08:10
This variable looks unused (only assigned, never r
sebsg
2016/03/22 18:30:26
Done.
| |
| 1925 size_t index = current_index; | |
| 1926 while (index != 0 && !has_found_previous_type) { | |
| 1927 --index; | |
| 1928 AutofillField* prev_field = form->field(index); | |
| 1929 if (!IsNameType(*prev_field)) { | |
| 1930 has_found_previous_type = true; | |
| 1931 is_previous_credit_card = prev_field->Type().group() == CREDIT_CARD; | |
| 1932 } | |
| 1933 } | |
| 1934 | |
| 1935 // Look for a next non name related field. | |
| 1936 bool has_found_next_type = false; | |
| 1937 bool is_next_credit_card = false; | |
| 1938 continue_search = true; | |
| 1939 index = current_index; | |
| 1940 while (++index < form->field_count() && !has_found_next_type) { | |
| 1941 AutofillField* next_field = form->field(index); | |
| 1942 if (!IsNameType(*next_field)) { | |
| 1943 has_found_next_type = true; | |
| 1944 is_next_credit_card = next_field->Type().group() == CREDIT_CARD; | |
| 1945 } | |
| 1946 } | |
| 1947 | |
| 1948 if (has_found_previous_type || has_found_next_type) { | |
| 1949 // If the previous type and next type are not from the same name group | |
| 1950 // there is no sure way to disambiguate. | |
| 1951 if (has_found_previous_type && has_found_next_type && | |
| 1952 (is_previous_credit_card != is_next_credit_card)) { | |
| 1953 return; | |
| 1954 } | |
| 1955 | |
| 1956 // Otherwise, use the previous or next field group to decide whether | |
| 1957 // the field is a name or a credit card name. | |
| 1958 ServerFieldTypeSet matching_types; | |
| 1959 if (has_found_previous_type) { | |
| 1960 SelectRightNameType(upload_types, &matching_types, | |
|
vabr (Chromium)
2016/03/22 17:08:10
Because we already know that is_previous_credit_ca
sebsg
2016/03/22 18:30:26
Not necessarily. The previous check (line 1949) on
vabr (Chromium)
2016/03/23 08:41:22
Thanks for the explanation, you are right.
| |
| 1961 is_previous_credit_card); | |
| 1962 } else { | |
| 1963 SelectRightNameType(upload_types, &matching_types, is_next_credit_card); | |
| 1964 } | |
| 1965 | |
| 1966 AutofillField* field = form->field(current_index); | |
| 1967 field->set_possible_types(matching_types); | |
| 1968 } | |
| 1796 } | 1969 } |
| 1797 | 1970 |
| 1798 #ifdef ENABLE_FORM_DEBUG_DUMP | 1971 #ifdef ENABLE_FORM_DEBUG_DUMP |
| 1799 void AutofillManager::DumpAutofillData(bool imported_cc) const { | 1972 void AutofillManager::DumpAutofillData(bool imported_cc) const { |
| 1800 base::ThreadRestrictions::ScopedAllowIO allow_id; | 1973 base::ThreadRestrictions::ScopedAllowIO allow_id; |
| 1801 | 1974 |
| 1802 // This code dumps the last few forms seen on the current tab to a file on | 1975 // This code dumps the last few forms seen on the current tab to a file on |
| 1803 // the desktop. This is only enabled when a specific command line flag is | 1976 // the desktop. This is only enabled when a specific command line flag is |
| 1804 // passed for manual analysis of the address context information available | 1977 // passed for manual analysis of the address context information available |
| 1805 // when offering to save credit cards in a checkout session. This is to | 1978 // when offering to save credit cards in a checkout session. This is to |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 1829 if (i > 0) | 2002 if (i > 0) |
| 1830 fputs("Next oldest form:\n", file); | 2003 fputs("Next oldest form:\n", file); |
| 1831 } | 2004 } |
| 1832 fputs("\n", file); | 2005 fputs("\n", file); |
| 1833 | 2006 |
| 1834 fclose(file); | 2007 fclose(file); |
| 1835 } | 2008 } |
| 1836 #endif // ENABLE_FORM_DEBUG_DUMP | 2009 #endif // ENABLE_FORM_DEBUG_DUMP |
| 1837 | 2010 |
| 1838 } // namespace autofill | 2011 } // namespace autofill |
| OLD | NEW |