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/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 553 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 564 return MigrateToVersion56AddProfileLanguageCodeForFormatting(); | 564 return MigrateToVersion56AddProfileLanguageCodeForFormatting(); |
| 565 case 57: | 565 case 57: |
| 566 *update_compatible_version = true; | 566 *update_compatible_version = true; |
| 567 return MigrateToVersion57AddFullNameField(); | 567 return MigrateToVersion57AddFullNameField(); |
| 568 case 60: | 568 case 60: |
| 569 *update_compatible_version = false; | 569 *update_compatible_version = false; |
| 570 return MigrateToVersion60AddServerCards(); | 570 return MigrateToVersion60AddServerCards(); |
| 571 case 61: | 571 case 61: |
| 572 *update_compatible_version = false; | 572 *update_compatible_version = false; |
| 573 return MigrateToVersion61AddUsageStats(); | 573 return MigrateToVersion61AddUsageStats(); |
| 574 case 62: | |
| 575 *update_compatible_version = false; | |
| 576 return MigrateToVersion62AddUsageStatsForUnmaskedCards(); | |
| 574 } | 577 } |
| 575 return true; | 578 return true; |
| 576 } | 579 } |
| 577 | 580 |
| 578 bool AutofillTable::AddFormFieldValues( | 581 bool AutofillTable::AddFormFieldValues( |
| 579 const std::vector<FormFieldData>& elements, | 582 const std::vector<FormFieldData>& elements, |
| 580 std::vector<AutofillChange>* changes) { | 583 std::vector<AutofillChange>* changes) { |
| 581 return AddFormFieldValuesTime(elements, changes, Time::Now()); | 584 return AddFormFieldValuesTime(elements, changes, Time::Now()); |
| 582 } | 585 } |
| 583 | 586 |
| (...skipping 640 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1224 | 1227 |
| 1225 bool AutofillTable::GetServerCreditCards( | 1228 bool AutofillTable::GetServerCreditCards( |
| 1226 std::vector<CreditCard*>* credit_cards) { | 1229 std::vector<CreditCard*>* credit_cards) { |
| 1227 credit_cards->clear(); | 1230 credit_cards->clear(); |
| 1228 | 1231 |
| 1229 sql::Statement s(db_->GetUniqueStatement( | 1232 sql::Statement s(db_->GetUniqueStatement( |
| 1230 "SELECT " | 1233 "SELECT " |
| 1231 "card_number_encrypted, " // 0 | 1234 "card_number_encrypted, " // 0 |
| 1232 "last_four," // 1 | 1235 "last_four," // 1 |
| 1233 "masked.id," // 2 | 1236 "masked.id," // 2 |
| 1234 "type," // 3 | 1237 "use_count," // 3 |
| 1235 "status," // 4 | 1238 "use_date," // 4 |
| 1236 "name_on_card," // 5 | 1239 "type," // 5 |
| 1237 "exp_month," // 6 | 1240 "status," // 6 |
| 1238 "exp_year " // 7 | 1241 "name_on_card," // 7 |
| 1242 "exp_month," // 8 | |
| 1243 "exp_year " // 9 | |
| 1239 "FROM masked_credit_cards masked " | 1244 "FROM masked_credit_cards masked " |
| 1240 "LEFT OUTER JOIN unmasked_credit_cards unmasked " | 1245 "LEFT OUTER JOIN unmasked_credit_cards unmasked " |
| 1241 "ON masked.id = unmasked.id")); | 1246 "ON masked.id = unmasked.id")); |
| 1242 while (s.Step()) { | 1247 while (s.Step()) { |
| 1243 int index = 0; | 1248 int index = 0; |
| 1244 | 1249 |
| 1245 // If the card_number_encrypted field is nonempty, we can assume this card | 1250 // If the card_number_encrypted field is nonempty, we can assume this card |
| 1246 // is a full card, otherwise it's masked. | 1251 // is a full card, otherwise it's masked. |
| 1247 base::string16 full_card_number = UnencryptedCardFromColumn(s, index++); | 1252 base::string16 full_card_number = UnencryptedCardFromColumn(s, index++); |
| 1248 base::string16 last_four = s.ColumnString16(index++); | 1253 base::string16 last_four = s.ColumnString16(index++); |
| 1249 CreditCard::RecordType record_type = full_card_number.empty() ? | 1254 CreditCard::RecordType record_type = full_card_number.empty() ? |
| 1250 CreditCard::MASKED_SERVER_CARD : | 1255 CreditCard::MASKED_SERVER_CARD : |
| 1251 CreditCard::FULL_SERVER_CARD; | 1256 CreditCard::FULL_SERVER_CARD; |
| 1252 std::string server_id = s.ColumnString(index++); | 1257 std::string server_id = s.ColumnString(index++); |
| 1253 | 1258 |
| 1254 CreditCard* card = new CreditCard(record_type, server_id); | 1259 CreditCard* card = new CreditCard(record_type, server_id); |
| 1255 card->SetRawInfo( | 1260 card->SetRawInfo( |
| 1256 CREDIT_CARD_NUMBER, | 1261 CREDIT_CARD_NUMBER, |
| 1257 record_type == CreditCard::MASKED_SERVER_CARD ? last_four | 1262 record_type == CreditCard::MASKED_SERVER_CARD ? last_four |
| 1258 : full_card_number); | 1263 : full_card_number); |
| 1264 int64 use_count = s.ColumnInt64(index++); | |
| 1265 int64 use_date = s.ColumnInt64(index++); | |
| 1259 std::string card_type = s.ColumnString(index++); | 1266 std::string card_type = s.ColumnString(index++); |
| 1260 if (record_type == CreditCard::MASKED_SERVER_CARD) { | 1267 if (record_type == CreditCard::MASKED_SERVER_CARD) { |
| 1261 // The type must be set after setting the number to override the | 1268 // The type must be set after setting the number to override the |
| 1262 // autodectected type. | 1269 // autodectected type. |
| 1263 card->SetTypeForMaskedCard(card_type.c_str()); | 1270 card->SetTypeForMaskedCard(card_type.c_str()); |
| 1271 DCHECK_EQ(0, use_count); | |
| 1272 DCHECK_EQ(0, use_date); | |
| 1264 } else { | 1273 } else { |
| 1265 DCHECK_EQ(CreditCard::GetCreditCardType(full_card_number), card_type); | 1274 DCHECK_EQ(CreditCard::GetCreditCardType(full_card_number), card_type); |
| 1275 card->set_use_count(use_count); | |
| 1276 card->set_use_date(base::Time::FromTimeT(use_date)); | |
|
brettw
2015/02/23 19:04:34
I think it's better to use Time::FromInternalValue
Evan Stade
2015/02/23 22:57:22
I think you're right but I wasn't aware of FromInt
| |
| 1266 } | 1277 } |
| 1267 | 1278 |
| 1268 card->SetServerStatus(ServerStatusStringToEnum(s.ColumnString(index++))); | 1279 card->SetServerStatus(ServerStatusStringToEnum(s.ColumnString(index++))); |
| 1269 card->SetRawInfo(CREDIT_CARD_NAME, s.ColumnString16(index++)); | 1280 card->SetRawInfo(CREDIT_CARD_NAME, s.ColumnString16(index++)); |
| 1270 card->SetRawInfo(CREDIT_CARD_EXP_MONTH, s.ColumnString16(index++)); | 1281 card->SetRawInfo(CREDIT_CARD_EXP_MONTH, s.ColumnString16(index++)); |
| 1271 card->SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, s.ColumnString16(index++)); | 1282 card->SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, s.ColumnString16(index++)); |
| 1272 credit_cards->push_back(card); | 1283 credit_cards->push_back(card); |
| 1273 } | 1284 } |
| 1274 | 1285 |
| 1275 return s.Succeeded(); | 1286 return s.Succeeded(); |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1349 unmasked_insert.Reset(true); | 1360 unmasked_insert.Reset(true); |
| 1350 } | 1361 } |
| 1351 } | 1362 } |
| 1352 } | 1363 } |
| 1353 | 1364 |
| 1354 bool AutofillTable::UnmaskServerCreditCard(const std::string& id, | 1365 bool AutofillTable::UnmaskServerCreditCard(const std::string& id, |
| 1355 const base::string16& full_number) { | 1366 const base::string16& full_number) { |
| 1356 // Make sure there aren't duplicates for this card. | 1367 // Make sure there aren't duplicates for this card. |
| 1357 MaskServerCreditCard(id); | 1368 MaskServerCreditCard(id); |
| 1358 sql::Statement s(db_->GetUniqueStatement( | 1369 sql::Statement s(db_->GetUniqueStatement( |
| 1359 "INSERT INTO unmasked_credit_cards(id, card_number_encrypted) " | 1370 "INSERT INTO unmasked_credit_cards(id, card_number_encrypted," |
| 1360 "VALUES (?,?)")); | 1371 " use_count, use_date) " |
| 1372 "VALUES (?,?,?,?)")); | |
| 1361 s.BindString(0, id); | 1373 s.BindString(0, id); |
| 1362 | 1374 |
| 1363 std::string encrypted_data; | 1375 std::string encrypted_data; |
| 1364 OSCrypt::EncryptString16(full_number, &encrypted_data); | 1376 OSCrypt::EncryptString16(full_number, &encrypted_data); |
| 1365 s.BindBlob(1, encrypted_data.data(), | 1377 s.BindBlob(1, encrypted_data.data(), |
| 1366 static_cast<int>(encrypted_data.length())); | 1378 static_cast<int>(encrypted_data.length())); |
| 1367 | 1379 |
| 1380 // Unmasking counts as a usage, so set the stats accordingly. | |
| 1381 s.BindInt64(2, 1); | |
| 1382 s.BindInt64(3, base::Time::Now().ToTimeT()); | |
| 1383 | |
| 1368 s.Run(); | 1384 s.Run(); |
| 1369 return db_->GetLastChangeCount() > 0; | 1385 return db_->GetLastChangeCount() > 0; |
| 1370 } | 1386 } |
| 1371 | 1387 |
| 1372 bool AutofillTable::MaskServerCreditCard(const std::string& id) { | 1388 bool AutofillTable::MaskServerCreditCard(const std::string& id) { |
| 1373 sql::Statement s(db_->GetUniqueStatement( | 1389 sql::Statement s(db_->GetUniqueStatement( |
| 1374 "DELETE FROM unmasked_credit_cards WHERE id = ?")); | 1390 "DELETE FROM unmasked_credit_cards WHERE id = ?")); |
| 1375 s.BindString(0, id); | 1391 s.BindString(0, id); |
| 1376 s.Run(); | 1392 s.Run(); |
| 1377 return db_->GetLastChangeCount() > 0; | 1393 return db_->GetLastChangeCount() > 0; |
| 1378 } | 1394 } |
| 1379 | 1395 |
| 1396 bool AutofillTable::UpdateUnmaskedCardUsageStats( | |
| 1397 const CreditCard& credit_card) { | |
| 1398 DCHECK_EQ(CreditCard::FULL_SERVER_CARD, credit_card.record_type()); | |
| 1399 | |
| 1400 sql::Statement s(db_->GetUniqueStatement( | |
| 1401 "UPDATE unmasked_credit_cards " | |
| 1402 "SET use_count=?, use_date=? " | |
| 1403 "WHERE id=?")); | |
| 1404 s.BindInt64(0, credit_card.use_count()); | |
| 1405 s.BindInt64(1, credit_card.use_date().ToTimeT()); | |
| 1406 s.BindString(2, credit_card.server_id()); | |
| 1407 s.Run(); | |
| 1408 return db_->GetLastChangeCount() > 0; | |
| 1409 } | |
| 1410 | |
| 1380 bool AutofillTable::UpdateCreditCard(const CreditCard& credit_card) { | 1411 bool AutofillTable::UpdateCreditCard(const CreditCard& credit_card) { |
| 1381 DCHECK(base::IsValidGUID(credit_card.guid())); | 1412 DCHECK(base::IsValidGUID(credit_card.guid())); |
| 1382 | 1413 |
| 1383 CreditCard* tmp_credit_card = NULL; | 1414 CreditCard* tmp_credit_card = NULL; |
| 1384 if (!GetCreditCard(credit_card.guid(), &tmp_credit_card)) | 1415 if (!GetCreditCard(credit_card.guid(), &tmp_credit_card)) |
| 1385 return false; | 1416 return false; |
| 1386 | 1417 |
| 1387 scoped_ptr<CreditCard> old_credit_card(tmp_credit_card); | 1418 scoped_ptr<CreditCard> old_credit_card(tmp_credit_card); |
| 1388 bool update_modification_date = *old_credit_card != credit_card; | 1419 bool update_modification_date = *old_credit_card != credit_card; |
| 1389 | 1420 |
| (...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1726 return false; | 1757 return false; |
| 1727 } | 1758 } |
| 1728 } | 1759 } |
| 1729 return true; | 1760 return true; |
| 1730 } | 1761 } |
| 1731 | 1762 |
| 1732 bool AutofillTable::InitUnmaskedCreditCardsTable() { | 1763 bool AutofillTable::InitUnmaskedCreditCardsTable() { |
| 1733 if (!db_->DoesTableExist("unmasked_credit_cards")) { | 1764 if (!db_->DoesTableExist("unmasked_credit_cards")) { |
| 1734 if (!db_->Execute("CREATE TABLE unmasked_credit_cards (" | 1765 if (!db_->Execute("CREATE TABLE unmasked_credit_cards (" |
| 1735 "id VARCHAR," | 1766 "id VARCHAR," |
| 1736 "card_number_encrypted VARCHAR)")) { | 1767 "card_number_encrypted VARCHAR, " |
| 1768 "use_count INTEGER NOT NULL DEFAULT 0, " | |
| 1769 "use_date INTEGER NOT NULL DEFAULT 0)")) { | |
| 1737 NOTREACHED(); | 1770 NOTREACHED(); |
| 1738 return false; | 1771 return false; |
| 1739 } | 1772 } |
| 1740 } | 1773 } |
| 1741 return true; | 1774 return true; |
| 1742 } | 1775 } |
| 1743 | 1776 |
| 1744 bool AutofillTable::InitServerAddressesTable() { | 1777 bool AutofillTable::InitServerAddressesTable() { |
| 1745 if (!db_->DoesTableExist("server_addresses")) { | 1778 if (!db_->DoesTableExist("server_addresses")) { |
| 1746 if (!db_->Execute("CREATE TABLE server_addresses (" | 1779 if (!db_->Execute("CREATE TABLE server_addresses (" |
| (...skipping 956 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2703 return db_->Execute("ALTER TABLE autofill_profiles " | 2736 return db_->Execute("ALTER TABLE autofill_profiles " |
| 2704 "ADD COLUMN language_code VARCHAR"); | 2737 "ADD COLUMN language_code VARCHAR"); |
| 2705 } | 2738 } |
| 2706 | 2739 |
| 2707 bool AutofillTable::MigrateToVersion57AddFullNameField() { | 2740 bool AutofillTable::MigrateToVersion57AddFullNameField() { |
| 2708 return db_->Execute("ALTER TABLE autofill_profile_names " | 2741 return db_->Execute("ALTER TABLE autofill_profile_names " |
| 2709 "ADD COLUMN full_name VARCHAR"); | 2742 "ADD COLUMN full_name VARCHAR"); |
| 2710 } | 2743 } |
| 2711 | 2744 |
| 2712 bool AutofillTable::MigrateToVersion60AddServerCards() { | 2745 bool AutofillTable::MigrateToVersion60AddServerCards() { |
| 2713 return InitMaskedCreditCardsTable() && | 2746 sql::Transaction transaction(db_); |
| 2714 InitUnmaskedCreditCardsTable() && | 2747 if (!transaction.Begin()) |
| 2715 InitServerAddressesTable(); | 2748 return false; |
| 2749 | |
| 2750 if (!db_->DoesTableExist("masked_credit_cards") && | |
| 2751 !db_->Execute("CREATE TABLE masked_credit_cards (" | |
| 2752 "id VARCHAR," | |
| 2753 "status VARCHAR," | |
| 2754 "name_on_card VARCHAR," | |
| 2755 "type VARCHAR," | |
| 2756 "last_four VARCHAR," | |
| 2757 "exp_month INTEGER DEFAULT 0," | |
| 2758 "exp_year INTEGER DEFAULT 0)")) { | |
| 2759 return false; | |
| 2760 } | |
| 2761 | |
| 2762 if (!db_->DoesTableExist("unmasked_credit_cards") && | |
| 2763 !db_->Execute("CREATE TABLE unmasked_credit_cards (" | |
| 2764 "id VARCHAR," | |
| 2765 "card_number_encrypted VARCHAR)")) { | |
| 2766 return false; | |
| 2767 } | |
| 2768 | |
| 2769 if (!db_->DoesTableExist("server_addresses") && | |
| 2770 !db_->Execute("CREATE TABLE server_addresses (" | |
| 2771 "id VARCHAR," | |
| 2772 "company_name VARCHAR," | |
| 2773 "street_address VARCHAR," | |
| 2774 "address_1 VARCHAR," | |
| 2775 "address_2 VARCHAR," | |
| 2776 "address_3 VARCHAR," | |
| 2777 "address_4 VARCHAR," | |
| 2778 "postal_code VARCHAR," | |
| 2779 "sorting_code VARCHAR," | |
| 2780 "country_code VARCHAR," | |
| 2781 "language_code VARCHAR)")) { | |
| 2782 return false; | |
| 2783 } | |
| 2784 | |
| 2785 return transaction.Commit(); | |
| 2716 } | 2786 } |
| 2717 | 2787 |
| 2718 bool AutofillTable::MigrateToVersion61AddUsageStats() { | 2788 bool AutofillTable::MigrateToVersion61AddUsageStats() { |
| 2719 sql::Transaction transaction(db_); | 2789 sql::Transaction transaction(db_); |
| 2720 if (!transaction.Begin()) | 2790 if (!transaction.Begin()) |
| 2721 return false; | 2791 return false; |
| 2722 | 2792 |
| 2723 // Add use_count to autofill_profiles. | 2793 // Add use_count to autofill_profiles. |
| 2724 if (!db_->DoesColumnExist("autofill_profiles", "use_count") && | 2794 if (!db_->DoesColumnExist("autofill_profiles", "use_count") && |
| 2725 !db_->Execute("ALTER TABLE autofill_profiles ADD COLUMN " | 2795 !db_->Execute("ALTER TABLE autofill_profiles ADD COLUMN " |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 2744 // Add use_date to credit_cards. | 2814 // Add use_date to credit_cards. |
| 2745 if (!db_->DoesColumnExist("credit_cards", "use_date") && | 2815 if (!db_->DoesColumnExist("credit_cards", "use_date") && |
| 2746 !db_->Execute("ALTER TABLE credit_cards ADD COLUMN " | 2816 !db_->Execute("ALTER TABLE credit_cards ADD COLUMN " |
| 2747 "use_date INTEGER NOT NULL DEFAULT 0")) { | 2817 "use_date INTEGER NOT NULL DEFAULT 0")) { |
| 2748 return false; | 2818 return false; |
| 2749 } | 2819 } |
| 2750 | 2820 |
| 2751 return transaction.Commit(); | 2821 return transaction.Commit(); |
| 2752 } | 2822 } |
| 2753 | 2823 |
| 2824 bool AutofillTable::MigrateToVersion62AddUsageStatsForUnmaskedCards() { | |
| 2825 sql::Transaction transaction(db_); | |
| 2826 if (!transaction.Begin()) | |
| 2827 return false; | |
| 2828 | |
| 2829 // Add use_count to unmasked_credit_cards. | |
| 2830 if (!db_->DoesColumnExist("unmasked_credit_cards", "use_count") && | |
| 2831 !db_->Execute("ALTER TABLE unmasked_credit_cards ADD COLUMN " | |
| 2832 "use_count INTEGER NOT NULL DEFAULT 0")) { | |
| 2833 return false; | |
| 2834 } | |
| 2835 | |
| 2836 // Add use_date to unmasked_credit_cards. | |
| 2837 if (!db_->DoesColumnExist("unmasked_credit_cards", "use_date") && | |
| 2838 !db_->Execute("ALTER TABLE unmasked_credit_cards ADD COLUMN " | |
| 2839 "use_date INTEGER NOT NULL DEFAULT 0")) { | |
| 2840 return false; | |
| 2841 } | |
| 2842 | |
| 2843 return transaction.Commit(); | |
| 2844 } | |
| 2845 | |
| 2754 } // namespace autofill | 2846 } // namespace autofill |
| OLD | NEW |