| 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/webdata/autofill_table.h" | 5 #include "components/autofill/core/browser/webdata/autofill_table.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <cmath> | 10 #include <cmath> |
| 11 #include <limits> | 11 #include <limits> |
| 12 #include <map> | 12 #include <map> |
| 13 #include <set> | 13 #include <set> |
| 14 #include <string> | 14 #include <string> |
| 15 #include <vector> | 15 #include <vector> |
| 16 | 16 |
| 17 #include "base/command_line.h" | 17 #include "base/command_line.h" |
| 18 #include "base/guid.h" | 18 #include "base/guid.h" |
| 19 #include "base/i18n/case_conversion.h" | 19 #include "base/i18n/case_conversion.h" |
| 20 #include "base/logging.h" | 20 #include "base/logging.h" |
| 21 #include "base/memory/ptr_util.h" | 21 #include "base/memory/ptr_util.h" |
| 22 #include "base/numerics/safe_conversions.h" | 22 #include "base/numerics/safe_conversions.h" |
| 23 #include "base/strings/string_number_conversions.h" | 23 #include "base/strings/string_number_conversions.h" |
| 24 #include "base/strings/string_util.h" | 24 #include "base/strings/string_util.h" |
| 25 #include "base/strings/utf_string_conversions.h" | 25 #include "base/strings/utf_string_conversions.h" |
| 26 #include "base/time/time.h" | 26 #include "base/time/time.h" |
| 27 #include "components/autofill/core/browser/autofill_clock.h" |
| 27 #include "components/autofill/core/browser/autofill_country.h" | 28 #include "components/autofill/core/browser/autofill_country.h" |
| 28 #include "components/autofill/core/browser/autofill_profile.h" | 29 #include "components/autofill/core/browser/autofill_profile.h" |
| 29 #include "components/autofill/core/browser/autofill_type.h" | 30 #include "components/autofill/core/browser/autofill_type.h" |
| 30 #include "components/autofill/core/browser/credit_card.h" | 31 #include "components/autofill/core/browser/credit_card.h" |
| 31 #include "components/autofill/core/browser/personal_data_manager.h" | 32 #include "components/autofill/core/browser/personal_data_manager.h" |
| 32 #include "components/autofill/core/browser/webdata/autofill_change.h" | 33 #include "components/autofill/core/browser/webdata/autofill_change.h" |
| 33 #include "components/autofill/core/browser/webdata/autofill_entry.h" | 34 #include "components/autofill/core/browser/webdata/autofill_entry.h" |
| 34 #include "components/autofill/core/common/autofill_switches.h" | 35 #include "components/autofill/core/common/autofill_switches.h" |
| 35 #include "components/autofill/core/common/autofill_util.h" | 36 #include "components/autofill/core/common/autofill_util.h" |
| 36 #include "components/autofill/core/common/form_field_data.h" | 37 #include "components/autofill/core/common/form_field_data.h" |
| (...skipping 437 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 474 case 71: | 475 case 71: |
| 475 *update_compatible_version = true; | 476 *update_compatible_version = true; |
| 476 return MigrateToVersion71AddHasConvertedAndBillingAddressIdMetadata(); | 477 return MigrateToVersion71AddHasConvertedAndBillingAddressIdMetadata(); |
| 477 } | 478 } |
| 478 return true; | 479 return true; |
| 479 } | 480 } |
| 480 | 481 |
| 481 bool AutofillTable::AddFormFieldValues( | 482 bool AutofillTable::AddFormFieldValues( |
| 482 const std::vector<FormFieldData>& elements, | 483 const std::vector<FormFieldData>& elements, |
| 483 std::vector<AutofillChange>* changes) { | 484 std::vector<AutofillChange>* changes) { |
| 484 return AddFormFieldValuesTime(elements, changes, Time::Now()); | 485 return AddFormFieldValuesTime(elements, changes, AutofillClock::Now()); |
| 485 } | 486 } |
| 486 | 487 |
| 487 bool AutofillTable::AddFormFieldValue(const FormFieldData& element, | 488 bool AutofillTable::AddFormFieldValue(const FormFieldData& element, |
| 488 std::vector<AutofillChange>* changes) { | 489 std::vector<AutofillChange>* changes) { |
| 489 return AddFormFieldValueTime(element, changes, Time::Now()); | 490 return AddFormFieldValueTime(element, changes, AutofillClock::Now()); |
| 490 } | 491 } |
| 491 | 492 |
| 492 bool AutofillTable::GetFormValuesForElementName( | 493 bool AutofillTable::GetFormValuesForElementName( |
| 493 const base::string16& name, | 494 const base::string16& name, |
| 494 const base::string16& prefix, | 495 const base::string16& prefix, |
| 495 std::vector<base::string16>* values, | 496 std::vector<base::string16>* values, |
| 496 int limit) { | 497 int limit) { |
| 497 DCHECK(values); | 498 DCHECK(values); |
| 498 bool succeeded = false; | 499 bool succeeded = false; |
| 499 | 500 |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 667 if (!transaction.Commit()) | 668 if (!transaction.Commit()) |
| 668 return false; | 669 return false; |
| 669 | 670 |
| 670 *changes = tentative_changes; | 671 *changes = tentative_changes; |
| 671 return true; | 672 return true; |
| 672 } | 673 } |
| 673 | 674 |
| 674 bool AutofillTable::RemoveExpiredFormElements( | 675 bool AutofillTable::RemoveExpiredFormElements( |
| 675 std::vector<AutofillChange>* changes) { | 676 std::vector<AutofillChange>* changes) { |
| 676 Time expiration_time = | 677 Time expiration_time = |
| 677 Time::Now() - TimeDelta::FromDays(kExpirationPeriodInDays); | 678 AutofillClock::Now() - TimeDelta::FromDays(kExpirationPeriodInDays); |
| 678 | 679 |
| 679 // Query for the name and value of all form elements that were last used | 680 // Query for the name and value of all form elements that were last used |
| 680 // before the |expiration_time|. | 681 // before the |expiration_time|. |
| 681 sql::Statement select_for_delete(db_->GetUniqueStatement( | 682 sql::Statement select_for_delete(db_->GetUniqueStatement( |
| 682 "SELECT name, value FROM autofill WHERE date_last_used < ?")); | 683 "SELECT name, value FROM autofill WHERE date_last_used < ?")); |
| 683 select_for_delete.BindInt64(0, expiration_time.ToTimeT()); | 684 select_for_delete.BindInt64(0, expiration_time.ToTimeT()); |
| 684 std::vector<AutofillChange> tentative_changes; | 685 std::vector<AutofillChange> tentative_changes; |
| 685 while (select_for_delete.Step()) { | 686 while (select_for_delete.Step()) { |
| 686 base::string16 name = select_for_delete.ColumnString16(0); | 687 base::string16 name = select_for_delete.ColumnString16(0); |
| 687 base::string16 value = select_for_delete.ColumnString16(1); | 688 base::string16 value = select_for_delete.ColumnString16(1); |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 879 bool AutofillTable::AddAutofillProfile(const AutofillProfile& profile) { | 880 bool AutofillTable::AddAutofillProfile(const AutofillProfile& profile) { |
| 880 if (IsAutofillGUIDInTrash(profile.guid())) | 881 if (IsAutofillGUIDInTrash(profile.guid())) |
| 881 return true; | 882 return true; |
| 882 | 883 |
| 883 sql::Statement s(db_->GetUniqueStatement( | 884 sql::Statement s(db_->GetUniqueStatement( |
| 884 "INSERT INTO autofill_profiles" | 885 "INSERT INTO autofill_profiles" |
| 885 "(guid, company_name, street_address, dependent_locality, city, state," | 886 "(guid, company_name, street_address, dependent_locality, city, state," |
| 886 " zipcode, sorting_code, country_code, use_count, use_date, " | 887 " zipcode, sorting_code, country_code, use_count, use_date, " |
| 887 " date_modified, origin, language_code)" | 888 " date_modified, origin, language_code)" |
| 888 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)")); | 889 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)")); |
| 889 BindAutofillProfileToStatement(profile, Time::Now(), &s); | 890 BindAutofillProfileToStatement(profile, AutofillClock::Now(), &s); |
| 890 | 891 |
| 891 if (!s.Run()) | 892 if (!s.Run()) |
| 892 return false; | 893 return false; |
| 893 | 894 |
| 894 return AddAutofillProfilePieces(profile, db_); | 895 return AddAutofillProfilePieces(profile, db_); |
| 895 } | 896 } |
| 896 | 897 |
| 897 std::unique_ptr<AutofillProfile> AutofillTable::GetAutofillProfile( | 898 std::unique_ptr<AutofillProfile> AutofillTable::GetAutofillProfile( |
| 898 const std::string& guid) { | 899 const std::string& guid) { |
| 899 DCHECK(base::IsValidGUID(guid)); | 900 DCHECK(base::IsValidGUID(guid)); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 969 "LEFT OUTER JOIN server_address_metadata USING (id)")); | 970 "LEFT OUTER JOIN server_address_metadata USING (id)")); |
| 970 | 971 |
| 971 while (s.Step()) { | 972 while (s.Step()) { |
| 972 int index = 0; | 973 int index = 0; |
| 973 std::unique_ptr<AutofillProfile> profile = | 974 std::unique_ptr<AutofillProfile> profile = |
| 974 base::MakeUnique<AutofillProfile>(AutofillProfile::SERVER_PROFILE, | 975 base::MakeUnique<AutofillProfile>(AutofillProfile::SERVER_PROFILE, |
| 975 s.ColumnString(index++)); | 976 s.ColumnString(index++)); |
| 976 profile->set_use_count(s.ColumnInt64(index++)); | 977 profile->set_use_count(s.ColumnInt64(index++)); |
| 977 profile->set_use_date(Time::FromInternalValue(s.ColumnInt64(index++))); | 978 profile->set_use_date(Time::FromInternalValue(s.ColumnInt64(index++))); |
| 978 // Modification date is not tracked for server profiles. Explicitly set it | 979 // Modification date is not tracked for server profiles. Explicitly set it |
| 979 // here to override the default value of Time::Now(). | 980 // here to override the default value of AutofillClock::Now(). |
| 980 profile->set_modification_date(Time()); | 981 profile->set_modification_date(Time()); |
| 981 | 982 |
| 982 base::string16 recipient_name = s.ColumnString16(index++); | 983 base::string16 recipient_name = s.ColumnString16(index++); |
| 983 profile->SetRawInfo(COMPANY_NAME, s.ColumnString16(index++)); | 984 profile->SetRawInfo(COMPANY_NAME, s.ColumnString16(index++)); |
| 984 profile->SetRawInfo(ADDRESS_HOME_STREET_ADDRESS, s.ColumnString16(index++)); | 985 profile->SetRawInfo(ADDRESS_HOME_STREET_ADDRESS, s.ColumnString16(index++)); |
| 985 profile->SetRawInfo(ADDRESS_HOME_STATE, s.ColumnString16(index++)); | 986 profile->SetRawInfo(ADDRESS_HOME_STATE, s.ColumnString16(index++)); |
| 986 profile->SetRawInfo(ADDRESS_HOME_CITY, s.ColumnString16(index++)); | 987 profile->SetRawInfo(ADDRESS_HOME_CITY, s.ColumnString16(index++)); |
| 987 profile->SetRawInfo(ADDRESS_HOME_DEPENDENT_LOCALITY, | 988 profile->SetRawInfo(ADDRESS_HOME_DEPENDENT_LOCALITY, |
| 988 s.ColumnString16(index++)); | 989 s.ColumnString16(index++)); |
| 989 index++; // Skip address_4 which we haven't added to AutofillProfile yet. | 990 index++; // Skip address_4 which we haven't added to AutofillProfile yet. |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1081 | 1082 |
| 1082 bool update_modification_date = *old_profile != profile; | 1083 bool update_modification_date = *old_profile != profile; |
| 1083 | 1084 |
| 1084 sql::Statement s(db_->GetUniqueStatement( | 1085 sql::Statement s(db_->GetUniqueStatement( |
| 1085 "UPDATE autofill_profiles " | 1086 "UPDATE autofill_profiles " |
| 1086 "SET guid=?, company_name=?, street_address=?, dependent_locality=?, " | 1087 "SET guid=?, company_name=?, street_address=?, dependent_locality=?, " |
| 1087 " city=?, state=?, zipcode=?, sorting_code=?, country_code=?, " | 1088 " city=?, state=?, zipcode=?, sorting_code=?, country_code=?, " |
| 1088 " use_count=?, use_date=?, date_modified=?, origin=?, language_code=? " | 1089 " use_count=?, use_date=?, date_modified=?, origin=?, language_code=? " |
| 1089 "WHERE guid=?")); | 1090 "WHERE guid=?")); |
| 1090 BindAutofillProfileToStatement( | 1091 BindAutofillProfileToStatement( |
| 1091 profile, | 1092 profile, update_modification_date ? AutofillClock::Now() |
| 1092 update_modification_date ? Time::Now() : old_profile->modification_date(), | 1093 : old_profile->modification_date(), |
| 1093 &s); | 1094 &s); |
| 1094 s.BindString(14, profile.guid()); | 1095 s.BindString(14, profile.guid()); |
| 1095 | 1096 |
| 1096 bool result = s.Run(); | 1097 bool result = s.Run(); |
| 1097 DCHECK_GT(db_->GetLastChangeCount(), 0); | 1098 DCHECK_GT(db_->GetLastChangeCount(), 0); |
| 1098 if (!result) | 1099 if (!result) |
| 1099 return result; | 1100 return result; |
| 1100 | 1101 |
| 1101 // Remove the old names, emails, and phone numbers. | 1102 // Remove the old names, emails, and phone numbers. |
| 1102 if (!RemoveAutofillProfilePieces(profile.guid(), db_)) | 1103 if (!RemoveAutofillProfilePieces(profile.guid(), db_)) |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1153 return s4.Run(); | 1154 return s4.Run(); |
| 1154 } | 1155 } |
| 1155 | 1156 |
| 1156 bool AutofillTable::AddCreditCard(const CreditCard& credit_card) { | 1157 bool AutofillTable::AddCreditCard(const CreditCard& credit_card) { |
| 1157 sql::Statement s(db_->GetUniqueStatement( | 1158 sql::Statement s(db_->GetUniqueStatement( |
| 1158 "INSERT INTO credit_cards" | 1159 "INSERT INTO credit_cards" |
| 1159 "(guid, name_on_card, expiration_month, expiration_year, " | 1160 "(guid, name_on_card, expiration_month, expiration_year, " |
| 1160 " card_number_encrypted, use_count, use_date, date_modified, origin," | 1161 " card_number_encrypted, use_count, use_date, date_modified, origin," |
| 1161 " billing_address_id)" | 1162 " billing_address_id)" |
| 1162 "VALUES (?,?,?,?,?,?,?,?,?,?)")); | 1163 "VALUES (?,?,?,?,?,?,?,?,?,?)")); |
| 1163 BindCreditCardToStatement(credit_card, Time::Now(), &s); | 1164 BindCreditCardToStatement(credit_card, AutofillClock::Now(), &s); |
| 1164 | 1165 |
| 1165 if (!s.Run()) | 1166 if (!s.Run()) |
| 1166 return false; | 1167 return false; |
| 1167 | 1168 |
| 1168 DCHECK_GT(db_->GetLastChangeCount(), 0); | 1169 DCHECK_GT(db_->GetLastChangeCount(), 0); |
| 1169 return true; | 1170 return true; |
| 1170 } | 1171 } |
| 1171 | 1172 |
| 1172 std::unique_ptr<CreditCard> AutofillTable::GetCreditCard( | 1173 std::unique_ptr<CreditCard> AutofillTable::GetCreditCard( |
| 1173 const std::string& guid) { | 1174 const std::string& guid) { |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1241 | 1242 |
| 1242 std::unique_ptr<CreditCard> card = | 1243 std::unique_ptr<CreditCard> card = |
| 1243 base::MakeUnique<CreditCard>(record_type, server_id); | 1244 base::MakeUnique<CreditCard>(record_type, server_id); |
| 1244 card->SetRawInfo( | 1245 card->SetRawInfo( |
| 1245 CREDIT_CARD_NUMBER, | 1246 CREDIT_CARD_NUMBER, |
| 1246 record_type == CreditCard::MASKED_SERVER_CARD ? last_four | 1247 record_type == CreditCard::MASKED_SERVER_CARD ? last_four |
| 1247 : full_card_number); | 1248 : full_card_number); |
| 1248 card->set_use_count(s.ColumnInt64(index++)); | 1249 card->set_use_count(s.ColumnInt64(index++)); |
| 1249 card->set_use_date(Time::FromInternalValue(s.ColumnInt64(index++))); | 1250 card->set_use_date(Time::FromInternalValue(s.ColumnInt64(index++))); |
| 1250 // Modification date is not tracked for server cards. Explicitly set it here | 1251 // Modification date is not tracked for server cards. Explicitly set it here |
| 1251 // to override the default value of Time::Now(). | 1252 // to override the default value of AutofillClock::Now(). |
| 1252 card->set_modification_date(Time()); | 1253 card->set_modification_date(Time()); |
| 1253 | 1254 |
| 1254 std::string card_type = s.ColumnString(index++); | 1255 std::string card_type = s.ColumnString(index++); |
| 1255 if (record_type == CreditCard::MASKED_SERVER_CARD) { | 1256 if (record_type == CreditCard::MASKED_SERVER_CARD) { |
| 1256 // The type must be set after setting the number to override the | 1257 // The type must be set after setting the number to override the |
| 1257 // autodetected type. | 1258 // autodetected type. |
| 1258 card->SetTypeForMaskedCard(card_type.c_str()); | 1259 card->SetTypeForMaskedCard(card_type.c_str()); |
| 1259 } else { | 1260 } else { |
| 1260 DCHECK_EQ(CreditCard::GetCreditCardType(full_card_number), card_type); | 1261 DCHECK_EQ(CreditCard::GetCreditCardType(full_card_number), card_type); |
| 1261 } | 1262 } |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1335 "id," | 1336 "id," |
| 1336 "card_number_encrypted," | 1337 "card_number_encrypted," |
| 1337 "unmask_date)" | 1338 "unmask_date)" |
| 1338 "VALUES (?,?,?)")); | 1339 "VALUES (?,?,?)")); |
| 1339 s.BindString(0, masked.server_id()); | 1340 s.BindString(0, masked.server_id()); |
| 1340 | 1341 |
| 1341 std::string encrypted_data; | 1342 std::string encrypted_data; |
| 1342 OSCrypt::EncryptString16(full_number, &encrypted_data); | 1343 OSCrypt::EncryptString16(full_number, &encrypted_data); |
| 1343 s.BindBlob(1, encrypted_data.data(), | 1344 s.BindBlob(1, encrypted_data.data(), |
| 1344 static_cast<int>(encrypted_data.length())); | 1345 static_cast<int>(encrypted_data.length())); |
| 1345 s.BindInt64(2, Time::Now().ToInternalValue()); // unmask_date | 1346 s.BindInt64(2, AutofillClock::Now().ToInternalValue()); // unmask_date |
| 1346 | 1347 |
| 1347 s.Run(); | 1348 s.Run(); |
| 1348 | 1349 |
| 1349 CreditCard unmasked = masked; | 1350 CreditCard unmasked = masked; |
| 1350 unmasked.set_record_type(CreditCard::FULL_SERVER_CARD); | 1351 unmasked.set_record_type(CreditCard::FULL_SERVER_CARD); |
| 1351 unmasked.SetNumber(full_number); | 1352 unmasked.SetNumber(full_number); |
| 1352 unmasked.RecordAndLogUse(); | 1353 unmasked.RecordAndLogUse(); |
| 1353 UpdateServerCardMetadata(unmasked); | 1354 UpdateServerCardMetadata(unmasked); |
| 1354 | 1355 |
| 1355 return db_->GetLastChangeCount() > 0; | 1356 return db_->GetLastChangeCount() > 0; |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1462 return false; | 1463 return false; |
| 1463 | 1464 |
| 1464 bool update_modification_date = *old_credit_card != credit_card; | 1465 bool update_modification_date = *old_credit_card != credit_card; |
| 1465 | 1466 |
| 1466 sql::Statement s(db_->GetUniqueStatement( | 1467 sql::Statement s(db_->GetUniqueStatement( |
| 1467 "UPDATE credit_cards " | 1468 "UPDATE credit_cards " |
| 1468 "SET guid=?, name_on_card=?, expiration_month=?," | 1469 "SET guid=?, name_on_card=?, expiration_month=?," |
| 1469 "expiration_year=?, card_number_encrypted=?, use_count=?, use_date=?," | 1470 "expiration_year=?, card_number_encrypted=?, use_count=?, use_date=?," |
| 1470 "date_modified=?, origin=?, billing_address_id=?" | 1471 "date_modified=?, origin=?, billing_address_id=?" |
| 1471 "WHERE guid=?1")); | 1472 "WHERE guid=?1")); |
| 1472 BindCreditCardToStatement( | 1473 BindCreditCardToStatement(credit_card, |
| 1473 credit_card, | 1474 update_modification_date |
| 1474 update_modification_date ? Time::Now() : | 1475 ? AutofillClock::Now() |
| 1475 old_credit_card->modification_date(), | 1476 : old_credit_card->modification_date(), |
| 1476 &s); | 1477 &s); |
| 1477 | 1478 |
| 1478 bool result = s.Run(); | 1479 bool result = s.Run(); |
| 1479 DCHECK_GT(db_->GetLastChangeCount(), 0); | 1480 DCHECK_GT(db_->GetLastChangeCount(), 0); |
| 1480 return result; | 1481 return result; |
| 1481 } | 1482 } |
| 1482 | 1483 |
| 1483 bool AutofillTable::RemoveCreditCard(const std::string& guid) { | 1484 bool AutofillTable::RemoveCreditCard(const std::string& guid) { |
| 1484 DCHECK(base::IsValidGUID(guid)); | 1485 DCHECK(base::IsValidGUID(guid)); |
| 1485 sql::Statement s(db_->GetUniqueStatement( | 1486 sql::Statement s(db_->GetUniqueStatement( |
| 1486 "DELETE FROM credit_cards WHERE guid = ?")); | 1487 "DELETE FROM credit_cards WHERE guid = ?")); |
| (...skipping 1034 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2521 if (!db_->Execute("DROP TABLE masked_credit_cards") || | 2522 if (!db_->Execute("DROP TABLE masked_credit_cards") || |
| 2522 !db_->Execute("ALTER TABLE masked_credit_cards_temp " | 2523 !db_->Execute("ALTER TABLE masked_credit_cards_temp " |
| 2523 "RENAME TO masked_credit_cards")) { | 2524 "RENAME TO masked_credit_cards")) { |
| 2524 return false; | 2525 return false; |
| 2525 } | 2526 } |
| 2526 | 2527 |
| 2527 return transaction.Commit(); | 2528 return transaction.Commit(); |
| 2528 } | 2529 } |
| 2529 | 2530 |
| 2530 } // namespace autofill | 2531 } // namespace autofill |
| OLD | NEW |