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 <algorithm> | 7 #include <algorithm> |
8 #include <cmath> | 8 #include <cmath> |
9 #include <limits> | 9 #include <limits> |
10 #include <map> | 10 #include <map> |
(...skipping 454 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
465 return MigrateToVersion60AddServerCards(); | 465 return MigrateToVersion60AddServerCards(); |
466 case 61: | 466 case 61: |
467 *update_compatible_version = false; | 467 *update_compatible_version = false; |
468 return MigrateToVersion61AddUsageStats(); | 468 return MigrateToVersion61AddUsageStats(); |
469 case 62: | 469 case 62: |
470 *update_compatible_version = false; | 470 *update_compatible_version = false; |
471 return MigrateToVersion62AddUsageStatsForUnmaskedCards(); | 471 return MigrateToVersion62AddUsageStatsForUnmaskedCards(); |
472 case 63: | 472 case 63: |
473 *update_compatible_version = false; | 473 *update_compatible_version = false; |
474 return MigrateToVersion63AddServerRecipientName(); | 474 return MigrateToVersion63AddServerRecipientName(); |
| 475 case 64: |
| 476 *update_compatible_version = false; |
| 477 return MigrateToVersion64AddUnmaskDate(); |
475 } | 478 } |
476 return true; | 479 return true; |
477 } | 480 } |
478 | 481 |
479 bool AutofillTable::AddFormFieldValues( | 482 bool AutofillTable::AddFormFieldValues( |
480 const std::vector<FormFieldData>& elements, | 483 const std::vector<FormFieldData>& elements, |
481 std::vector<AutofillChange>* changes) { | 484 std::vector<AutofillChange>* changes) { |
482 return AddFormFieldValuesTime(elements, changes, Time::Now()); | 485 return AddFormFieldValuesTime(elements, changes, Time::Now()); |
483 } | 486 } |
484 | 487 |
(...skipping 702 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1187 card->SetRawInfo(CREDIT_CARD_EXP_MONTH, s.ColumnString16(index++)); | 1190 card->SetRawInfo(CREDIT_CARD_EXP_MONTH, s.ColumnString16(index++)); |
1188 card->SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, s.ColumnString16(index++)); | 1191 card->SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, s.ColumnString16(index++)); |
1189 credit_cards->push_back(card); | 1192 credit_cards->push_back(card); |
1190 } | 1193 } |
1191 | 1194 |
1192 return s.Succeeded(); | 1195 return s.Succeeded(); |
1193 } | 1196 } |
1194 | 1197 |
1195 void AutofillTable::SetServerCreditCards( | 1198 void AutofillTable::SetServerCreditCards( |
1196 const std::vector<CreditCard>& credit_cards) { | 1199 const std::vector<CreditCard>& credit_cards) { |
| 1200 sql::Transaction transaction(db_); |
| 1201 if (!transaction.Begin()) |
| 1202 return; |
| 1203 |
1197 // Delete all old values. | 1204 // Delete all old values. |
1198 sql::Statement masked_delete(db_->GetUniqueStatement( | 1205 sql::Statement masked_delete(db_->GetUniqueStatement( |
1199 "DELETE FROM masked_credit_cards")); | 1206 "DELETE FROM masked_credit_cards")); |
1200 masked_delete.Run(); | 1207 masked_delete.Run(); |
1201 | 1208 |
1202 // Delete all items in the unmasked table that aren't in the new set. | 1209 // Delete all items in the unmasked table that aren't in the new set. |
1203 sql::Statement get_unmasked(db_->GetUniqueStatement( | 1210 sql::Statement get_unmasked(db_->GetUniqueStatement( |
1204 "SELECT id FROM unmasked_credit_cards")); | 1211 "SELECT id FROM unmasked_credit_cards")); |
1205 while (get_unmasked.Step()) { | 1212 while (get_unmasked.Step()) { |
1206 // We expect relatively few cards, just do brute-force. | 1213 // We expect relatively few cards, just do brute-force. |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1243 ServerStatusEnumToString(card.GetServerStatus())); | 1250 ServerStatusEnumToString(card.GetServerStatus())); |
1244 masked_insert.BindString16(3, card.GetRawInfo(CREDIT_CARD_NAME)); | 1251 masked_insert.BindString16(3, card.GetRawInfo(CREDIT_CARD_NAME)); |
1245 masked_insert.BindString16(4, card.LastFourDigits()); | 1252 masked_insert.BindString16(4, card.LastFourDigits()); |
1246 masked_insert.BindString16(5, card.GetRawInfo(CREDIT_CARD_EXP_MONTH)); | 1253 masked_insert.BindString16(5, card.GetRawInfo(CREDIT_CARD_EXP_MONTH)); |
1247 masked_insert.BindString16(6, | 1254 masked_insert.BindString16(6, |
1248 card.GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR)); | 1255 card.GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR)); |
1249 | 1256 |
1250 masked_insert.Run(); | 1257 masked_insert.Run(); |
1251 masked_insert.Reset(true); | 1258 masked_insert.Reset(true); |
1252 } | 1259 } |
| 1260 |
| 1261 transaction.Commit(); |
1253 } | 1262 } |
1254 | 1263 |
1255 bool AutofillTable::UnmaskServerCreditCard(const std::string& id, | 1264 bool AutofillTable::UnmaskServerCreditCard(const std::string& id, |
1256 const base::string16& full_number) { | 1265 const base::string16& full_number) { |
1257 // Make sure there aren't duplicates for this card. | 1266 // Make sure there aren't duplicates for this card. |
1258 MaskServerCreditCard(id); | 1267 MaskServerCreditCard(id); |
1259 sql::Statement s(db_->GetUniqueStatement( | 1268 sql::Statement s(db_->GetUniqueStatement( |
1260 "INSERT INTO unmasked_credit_cards(id, card_number_encrypted," | 1269 "INSERT INTO unmasked_credit_cards(" |
1261 " use_count, use_date) " | 1270 "id," |
1262 "VALUES (?,?,?,?)")); | 1271 "card_number_encrypted," |
| 1272 "use_count," |
| 1273 "use_date," |
| 1274 "unmask_date)" |
| 1275 "VALUES (?,?,?,?,?)")); |
1263 s.BindString(0, id); | 1276 s.BindString(0, id); |
1264 | 1277 |
1265 std::string encrypted_data; | 1278 std::string encrypted_data; |
1266 OSCrypt::EncryptString16(full_number, &encrypted_data); | 1279 OSCrypt::EncryptString16(full_number, &encrypted_data); |
1267 s.BindBlob(1, encrypted_data.data(), | 1280 s.BindBlob(1, encrypted_data.data(), |
1268 static_cast<int>(encrypted_data.length())); | 1281 static_cast<int>(encrypted_data.length())); |
1269 | 1282 |
1270 // Unmasking counts as a usage, so set the stats accordingly. | 1283 // Unmasking counts as a usage, so set the stats accordingly. |
1271 s.BindInt64(2, 1); | 1284 base::Time now = base::Time::Now(); |
1272 s.BindInt64(3, base::Time::Now().ToInternalValue()); | 1285 s.BindInt64(2, 1); // use_count |
| 1286 s.BindInt64(3, now.ToInternalValue()); // use_date |
| 1287 |
| 1288 s.BindInt64(4, now.ToInternalValue()); // unmask_date |
1273 | 1289 |
1274 s.Run(); | 1290 s.Run(); |
1275 return db_->GetLastChangeCount() > 0; | 1291 return db_->GetLastChangeCount() > 0; |
1276 } | 1292 } |
1277 | 1293 |
1278 bool AutofillTable::MaskServerCreditCard(const std::string& id) { | 1294 bool AutofillTable::MaskServerCreditCard(const std::string& id) { |
1279 sql::Statement s(db_->GetUniqueStatement( | 1295 sql::Statement s(db_->GetUniqueStatement( |
1280 "DELETE FROM unmasked_credit_cards WHERE id = ?")); | 1296 "DELETE FROM unmasked_credit_cards WHERE id = ?")); |
1281 s.BindString(0, id); | 1297 s.BindString(0, id); |
1282 s.Run(); | 1298 s.Run(); |
(...skipping 21 matching lines...) Expand all Loading... |
1304 CreditCard* tmp_credit_card = NULL; | 1320 CreditCard* tmp_credit_card = NULL; |
1305 if (!GetCreditCard(credit_card.guid(), &tmp_credit_card)) | 1321 if (!GetCreditCard(credit_card.guid(), &tmp_credit_card)) |
1306 return false; | 1322 return false; |
1307 | 1323 |
1308 scoped_ptr<CreditCard> old_credit_card(tmp_credit_card); | 1324 scoped_ptr<CreditCard> old_credit_card(tmp_credit_card); |
1309 bool update_modification_date = *old_credit_card != credit_card; | 1325 bool update_modification_date = *old_credit_card != credit_card; |
1310 | 1326 |
1311 sql::Statement s(db_->GetUniqueStatement( | 1327 sql::Statement s(db_->GetUniqueStatement( |
1312 "UPDATE credit_cards " | 1328 "UPDATE credit_cards " |
1313 "SET guid=?, name_on_card=?, expiration_month=?," | 1329 "SET guid=?, name_on_card=?, expiration_month=?," |
1314 " expiration_year=?, card_number_encrypted=?, use_count=?, use_date=?," | 1330 "expiration_year=?, card_number_encrypted=?, use_count=?, use_date=?," |
1315 " date_modified=?, origin=?" | 1331 "date_modified=?, origin=?" |
1316 "WHERE guid=?")); | 1332 "WHERE guid=?")); |
1317 BindCreditCardToStatement( | 1333 BindCreditCardToStatement( |
1318 credit_card, | 1334 credit_card, |
1319 update_modification_date ? base::Time::Now() : | 1335 update_modification_date ? base::Time::Now() : |
1320 old_credit_card->modification_date(), | 1336 old_credit_card->modification_date(), |
1321 &s); | 1337 &s); |
1322 s.BindString(9, credit_card.guid()); | 1338 s.BindString(9, credit_card.guid()); |
1323 | 1339 |
1324 bool result = s.Run(); | 1340 bool result = s.Run(); |
1325 DCHECK_GT(db_->GetLastChangeCount(), 0); | 1341 DCHECK_GT(db_->GetLastChangeCount(), 0); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1384 } | 1400 } |
1385 if (!s_credit_cards_get.Succeeded()) | 1401 if (!s_credit_cards_get.Succeeded()) |
1386 return false; | 1402 return false; |
1387 | 1403 |
1388 // Remove Autofill credit cards in the time range. | 1404 // Remove Autofill credit cards in the time range. |
1389 sql::Statement s_credit_cards(db_->GetUniqueStatement( | 1405 sql::Statement s_credit_cards(db_->GetUniqueStatement( |
1390 "DELETE FROM credit_cards " | 1406 "DELETE FROM credit_cards " |
1391 "WHERE date_modified >= ? AND date_modified < ?")); | 1407 "WHERE date_modified >= ? AND date_modified < ?")); |
1392 s_credit_cards.BindInt64(0, delete_begin_t); | 1408 s_credit_cards.BindInt64(0, delete_begin_t); |
1393 s_credit_cards.BindInt64(1, delete_end_t); | 1409 s_credit_cards.BindInt64(1, delete_end_t); |
| 1410 if (!s_credit_cards.Run()) |
| 1411 return false; |
1394 | 1412 |
1395 return s_credit_cards.Run(); | 1413 // Remove unmasked credit cards in the time range. |
| 1414 sql::Statement s_unmasked_cards(db_->GetUniqueStatement( |
| 1415 "DELETE FROM unmasked_credit_cards " |
| 1416 "WHERE unmask_date >= ? AND unmask_date < ?")); |
| 1417 s_unmasked_cards.BindInt64(0, delete_begin.ToInternalValue()); |
| 1418 s_unmasked_cards.BindInt64(1, delete_end.ToInternalValue()); |
| 1419 return s_unmasked_cards.Run(); |
1396 } | 1420 } |
1397 | 1421 |
1398 bool AutofillTable::RemoveOriginURLsModifiedBetween( | 1422 bool AutofillTable::RemoveOriginURLsModifiedBetween( |
1399 const Time& delete_begin, | 1423 const Time& delete_begin, |
1400 const Time& delete_end, | 1424 const Time& delete_end, |
1401 ScopedVector<AutofillProfile>* profiles) { | 1425 ScopedVector<AutofillProfile>* profiles) { |
1402 DCHECK(delete_end.is_null() || delete_begin < delete_end); | 1426 DCHECK(delete_end.is_null() || delete_begin < delete_end); |
1403 | 1427 |
1404 time_t delete_begin_t = delete_begin.ToTimeT(); | 1428 time_t delete_begin_t = delete_begin.ToTimeT(); |
1405 time_t delete_end_t = GetEndTime(delete_end); | 1429 time_t delete_end_t = GetEndTime(delete_end); |
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1649 } | 1673 } |
1650 return true; | 1674 return true; |
1651 } | 1675 } |
1652 | 1676 |
1653 bool AutofillTable::InitUnmaskedCreditCardsTable() { | 1677 bool AutofillTable::InitUnmaskedCreditCardsTable() { |
1654 if (!db_->DoesTableExist("unmasked_credit_cards")) { | 1678 if (!db_->DoesTableExist("unmasked_credit_cards")) { |
1655 if (!db_->Execute("CREATE TABLE unmasked_credit_cards (" | 1679 if (!db_->Execute("CREATE TABLE unmasked_credit_cards (" |
1656 "id VARCHAR," | 1680 "id VARCHAR," |
1657 "card_number_encrypted VARCHAR, " | 1681 "card_number_encrypted VARCHAR, " |
1658 "use_count INTEGER NOT NULL DEFAULT 0, " | 1682 "use_count INTEGER NOT NULL DEFAULT 0, " |
1659 "use_date INTEGER NOT NULL DEFAULT 0)")) { | 1683 "use_date INTEGER NOT NULL DEFAULT 0, " |
| 1684 "unmask_date INTEGER NOT NULL DEFAULT 0)")) { |
1660 NOTREACHED(); | 1685 NOTREACHED(); |
1661 return false; | 1686 return false; |
1662 } | 1687 } |
1663 } | 1688 } |
1664 return true; | 1689 return true; |
1665 } | 1690 } |
1666 | 1691 |
1667 bool AutofillTable::InitServerAddressesTable() { | 1692 bool AutofillTable::InitServerAddressesTable() { |
1668 if (!db_->DoesTableExist("server_addresses")) { | 1693 if (!db_->DoesTableExist("server_addresses")) { |
1669 // The space after language_code is necessary to match what sqlite does | 1694 // The space after language_code is necessary to match what sqlite does |
1670 // when it appends the column in migration. | 1695 // when it appends the column in migration. |
1671 if (!db_->Execute("CREATE TABLE server_addresses (" | 1696 if (!db_->Execute("CREATE TABLE server_addresses (" |
1672 "id VARCHAR," | 1697 "id VARCHAR," |
1673 "company_name VARCHAR," | 1698 "company_name VARCHAR," |
1674 "street_address VARCHAR," | 1699 "street_address VARCHAR," |
1675 "address_1 VARCHAR," | 1700 "address_1 VARCHAR," |
1676 "address_2 VARCHAR," | 1701 "address_2 VARCHAR," |
1677 "address_3 VARCHAR," | 1702 "address_3 VARCHAR," |
1678 "address_4 VARCHAR," | 1703 "address_4 VARCHAR," |
1679 "postal_code VARCHAR," | 1704 "postal_code VARCHAR," |
1680 "sorting_code VARCHAR," | 1705 "sorting_code VARCHAR," |
1681 "country_code VARCHAR," | 1706 "country_code VARCHAR," |
1682 "language_code VARCHAR, " // Space required. | 1707 "language_code VARCHAR, " // Space required. |
1683 "recipient_name VARCHAR)")) { | 1708 "recipient_name VARCHAR, " // Ditto. |
| 1709 "phone_number VARCHAR)")) { |
1684 NOTREACHED(); | 1710 NOTREACHED(); |
1685 return false; | 1711 return false; |
1686 } | 1712 } |
1687 } | 1713 } |
1688 return true; | 1714 return true; |
1689 } | 1715 } |
1690 | 1716 |
1691 bool AutofillTable::MigrateToVersion54AddI18nFieldsAndRemoveDeprecatedFields() { | 1717 bool AutofillTable::MigrateToVersion54AddI18nFieldsAndRemoveDeprecatedFields() { |
1692 sql::Transaction transaction(db_); | 1718 sql::Transaction transaction(db_); |
1693 if (!transaction.Begin()) | 1719 if (!transaction.Begin()) |
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1966 | 1992 |
1967 bool AutofillTable::MigrateToVersion63AddServerRecipientName() { | 1993 bool AutofillTable::MigrateToVersion63AddServerRecipientName() { |
1968 if (!db_->DoesColumnExist("server_addresses", "recipient_name") && | 1994 if (!db_->DoesColumnExist("server_addresses", "recipient_name") && |
1969 !db_->Execute("ALTER TABLE server_addresses ADD COLUMN " | 1995 !db_->Execute("ALTER TABLE server_addresses ADD COLUMN " |
1970 "recipient_name VARCHAR")) { | 1996 "recipient_name VARCHAR")) { |
1971 return false; | 1997 return false; |
1972 } | 1998 } |
1973 return true; | 1999 return true; |
1974 } | 2000 } |
1975 | 2001 |
| 2002 bool AutofillTable::MigrateToVersion64AddUnmaskDate() { |
| 2003 sql::Transaction transaction(db_); |
| 2004 if (!transaction.Begin()) |
| 2005 return false; |
| 2006 |
| 2007 if (!db_->DoesColumnExist("unmasked_credit_cards", "unmask_date") && |
| 2008 !db_->Execute("ALTER TABLE unmasked_credit_cards ADD COLUMN " |
| 2009 "unmask_date INTEGER NOT NULL DEFAULT 0")) { |
| 2010 return false; |
| 2011 } |
| 2012 if (!db_->DoesColumnExist("server_addresses", "phone_number") && |
| 2013 !db_->Execute("ALTER TABLE server_addresses ADD COLUMN " |
| 2014 "phone_number VARCHAR")) { |
| 2015 return false; |
| 2016 } |
| 2017 |
| 2018 return transaction.Commit(); |
| 2019 } |
| 2020 |
1976 } // namespace autofill | 2021 } // namespace autofill |
OLD | NEW |