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 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 700 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1185 card->SetServerStatus(ServerStatusStringToEnum(s.ColumnString(index++))); | 1188 card->SetServerStatus(ServerStatusStringToEnum(s.ColumnString(index++))); |
| 1186 card->SetRawInfo(CREDIT_CARD_NAME, s.ColumnString16(index++)); | 1189 card->SetRawInfo(CREDIT_CARD_NAME, s.ColumnString16(index++)); |
| 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( |
|
Evan Stade
2015/03/04 23:12:25
Why does this function support an input vector of
brettw
2015/03/04 23:21:45
It was convenient for testing.
Evan Stade
2015/03/05 01:54:24
I don't think it makes tests that much easier to w
brettw
2015/03/05 20:52:19
I spent a while changing this but it's not worthwh
| |
| 1196 const std::vector<CreditCard>& credit_cards) { | 1199 const std::vector<CreditCard>& credit_cards) { |
| 1197 // Delete all old values. | 1200 // Delete all old values. |
| 1198 sql::Statement masked_delete(db_->GetUniqueStatement( | 1201 sql::Statement masked_delete(db_->GetUniqueStatement( |
| 1199 "DELETE FROM masked_credit_cards")); | 1202 "DELETE FROM masked_credit_cards")); |
| 1200 masked_delete.Run(); | 1203 masked_delete.Run(); |
| 1201 | 1204 |
| 1202 // Delete all items in the unmasked table that aren't in the new set. | 1205 // Delete all items in the unmasked table that aren't in the new set. |
| 1203 sql::Statement get_unmasked(db_->GetUniqueStatement( | 1206 sql::Statement get_unmasked(db_->GetUniqueStatement( |
| 1204 "SELECT id FROM unmasked_credit_cards")); | 1207 "SELECT id FROM unmasked_credit_cards")); |
| 1208 std::set<std::string> kept_unmasked_entries; | |
| 1205 while (get_unmasked.Step()) { | 1209 while (get_unmasked.Step()) { |
| 1206 // We expect relatively few cards, just do brute-force. | 1210 // We expect relatively few cards, just do brute-force. |
| 1207 std::string server_id = get_unmasked.ColumnString(0); | 1211 std::string server_id = get_unmasked.ColumnString(0); |
| 1208 bool found_card = false; | 1212 bool found_card = false; |
| 1209 for (const CreditCard& cur_card : credit_cards) { | 1213 for (const CreditCard& cur_card : credit_cards) { |
| 1210 if (cur_card.server_id() == server_id) { | 1214 if (cur_card.server_id() == server_id) { |
| 1211 found_card = true; | 1215 found_card = true; |
| 1212 break; | 1216 break; |
| 1213 } | 1217 } |
| 1214 } | 1218 } |
| 1215 if (!found_card) { | 1219 if (found_card) { |
| 1220 kept_unmasked_entries.insert(server_id); | |
| 1221 } else { | |
| 1216 // This unmasked card in the DB isn't present in the input. The statement | 1222 // This unmasked card in the DB isn't present in the input. The statement |
| 1217 // is compiled every time because it's much more likely that this is never | 1223 // is compiled every time because it's much more likely that this is never |
| 1218 // executed than it runs more than once. | 1224 // executed than it runs more than once. |
| 1219 sql::Statement unmasked_delete(db_->GetUniqueStatement( | 1225 sql::Statement unmasked_delete(db_->GetUniqueStatement( |
| 1220 "DELETE FROM unmasked_credit_cards WHERE id = ?")); | 1226 "DELETE FROM unmasked_credit_cards WHERE id = ?")); |
| 1221 unmasked_delete.BindString(0, server_id); | 1227 unmasked_delete.BindString(0, server_id); |
| 1222 unmasked_delete.Run(); | 1228 unmasked_delete.Run(); |
| 1223 DCHECK_EQ(1, db_->GetLastChangeCount()); | 1229 DCHECK_EQ(1, db_->GetLastChangeCount()); |
| 1224 } | 1230 } |
| 1225 } | 1231 } |
| 1226 | 1232 |
| 1227 sql::Statement masked_insert(db_->GetUniqueStatement( | 1233 sql::Statement masked_insert(db_->GetUniqueStatement( |
|
Evan Stade
2015/03/04 23:12:25
there are multiple inserts in this function --- wr
brettw
2015/03/04 23:21:45
Sure. I added one. I think this will actually be i
| |
| 1228 "INSERT INTO masked_credit_cards(" | 1234 "INSERT INTO masked_credit_cards(" |
| 1229 "id," // 0 | 1235 "id," // 0 |
| 1230 "type," // 1 | 1236 "type," // 1 |
| 1231 "status," // 2 | 1237 "status," // 2 |
| 1232 "name_on_card," // 3 | 1238 "name_on_card," // 3 |
| 1233 "last_four," // 4 | 1239 "last_four," // 4 |
| 1234 "exp_month," // 4 | 1240 "exp_month," // 4 |
| 1235 "exp_year) " // 5 | 1241 "exp_year) " // 5 |
| 1236 "VALUES (?,?,?,?,?,?,?)")); | 1242 "VALUES (?,?,?,?,?,?,?)")); |
| 1237 sql::Statement unmasked_insert(db_->GetUniqueStatement( | 1243 sql::Statement unmasked_insert(db_->GetUniqueStatement( |
| 1238 "INSERT INTO unmasked_credit_cards(" | 1244 "INSERT INTO unmasked_credit_cards(" |
| 1239 "id," // 0 | 1245 "id," // 0 |
| 1240 "card_number_encrypted)" // 1 | 1246 "card_number_encrypted, " // 1 |
| 1241 "VALUES (?,?)")); | 1247 "unmask_date)" // 2 |
| 1248 "VALUES (?,?,?)")); | |
| 1242 for (const CreditCard& card : credit_cards) { | 1249 for (const CreditCard& card : credit_cards) { |
| 1243 DCHECK(card.record_type() != CreditCard::LOCAL_CARD); | 1250 DCHECK(card.record_type() != CreditCard::LOCAL_CARD); |
| 1244 | 1251 |
| 1245 masked_insert.BindString(0, card.server_id()); | 1252 masked_insert.BindString(0, card.server_id()); |
| 1246 masked_insert.BindString(1, card.type()); | 1253 masked_insert.BindString(1, card.type()); |
| 1247 masked_insert.BindString(2, | 1254 masked_insert.BindString(2, |
| 1248 ServerStatusEnumToString(card.GetServerStatus())); | 1255 ServerStatusEnumToString(card.GetServerStatus())); |
| 1249 masked_insert.BindString16(3, card.GetRawInfo(CREDIT_CARD_NAME)); | 1256 masked_insert.BindString16(3, card.GetRawInfo(CREDIT_CARD_NAME)); |
| 1250 masked_insert.BindString16(4, card.LastFourDigits()); | 1257 masked_insert.BindString16(4, card.LastFourDigits()); |
| 1251 masked_insert.BindString16(5, card.GetRawInfo(CREDIT_CARD_EXP_MONTH)); | 1258 masked_insert.BindString16(5, card.GetRawInfo(CREDIT_CARD_EXP_MONTH)); |
| 1252 masked_insert.BindString16(6, | 1259 masked_insert.BindString16(6, |
| 1253 card.GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR)); | 1260 card.GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR)); |
| 1254 | 1261 |
| 1255 masked_insert.Run(); | 1262 masked_insert.Run(); |
| 1256 masked_insert.Reset(true); | 1263 masked_insert.Reset(true); |
| 1257 | 1264 |
| 1258 if (card.record_type() == CreditCard::FULL_SERVER_CARD) { | 1265 if (kept_unmasked_entries.find(card.server_id()) == |
| 1259 // Unmasked cards also get an entry in the unmasked table. Note that the | 1266 kept_unmasked_entries.end() && |
| 1260 // input card could be MASKED but if we have an UNMASKED entry for that | 1267 card.record_type() == CreditCard::FULL_SERVER_CARD) { |
| 1261 // card already, it will be preserved. | 1268 // New unmasked cards also get an entry in the unmasked table. In |
| 1269 // practice this will only happen for tests since the server cards will | |
| 1270 // all be marked masked (if a server card is manually unmasked, we will | |
| 1271 // have kept the existing entry in the table and won't get here). | |
| 1262 unmasked_insert.BindString(0, card.server_id()); | 1272 unmasked_insert.BindString(0, card.server_id()); |
| 1263 BindEncryptedCardToColumn(&unmasked_insert, 1, | 1273 BindEncryptedCardToColumn(&unmasked_insert, 1, |
| 1264 card.GetRawInfo(CREDIT_CARD_NUMBER)); | 1274 card.GetRawInfo(CREDIT_CARD_NUMBER)); |
| 1275 // Unmask time for this card is now. | |
| 1276 unmasked_insert.BindInt64(2, Time::Now().ToInternalValue()); | |
| 1265 unmasked_insert.Run(); | 1277 unmasked_insert.Run(); |
| 1266 unmasked_insert.Reset(true); | 1278 unmasked_insert.Reset(true); |
| 1267 } | 1279 } |
| 1268 } | 1280 } |
| 1269 } | 1281 } |
| 1270 | 1282 |
| 1271 bool AutofillTable::UnmaskServerCreditCard(const std::string& id, | 1283 bool AutofillTable::UnmaskServerCreditCard(const std::string& id, |
| 1272 const base::string16& full_number) { | 1284 const base::string16& full_number) { |
| 1273 // Make sure there aren't duplicates for this card. | 1285 // Make sure there aren't duplicates for this card. |
| 1274 MaskServerCreditCard(id); | 1286 MaskServerCreditCard(id); |
| 1275 sql::Statement s(db_->GetUniqueStatement( | 1287 sql::Statement s(db_->GetUniqueStatement( |
| 1276 "INSERT INTO unmasked_credit_cards(id, card_number_encrypted," | 1288 "INSERT INTO unmasked_credit_cards(" |
| 1277 " use_count, use_date) " | 1289 "id," |
| 1278 "VALUES (?,?,?,?)")); | 1290 "card_number_encrypted," |
| 1291 "use_count," | |
| 1292 "use_date," | |
| 1293 "unmask_date)" | |
| 1294 "VALUES (?,?,?,?,?)")); | |
| 1279 s.BindString(0, id); | 1295 s.BindString(0, id); |
| 1280 | 1296 |
| 1281 std::string encrypted_data; | 1297 std::string encrypted_data; |
| 1282 OSCrypt::EncryptString16(full_number, &encrypted_data); | 1298 OSCrypt::EncryptString16(full_number, &encrypted_data); |
| 1283 s.BindBlob(1, encrypted_data.data(), | 1299 s.BindBlob(1, encrypted_data.data(), |
| 1284 static_cast<int>(encrypted_data.length())); | 1300 static_cast<int>(encrypted_data.length())); |
| 1285 | 1301 |
| 1286 // Unmasking counts as a usage, so set the stats accordingly. | 1302 // Unmasking counts as a usage, so set the stats accordingly. |
| 1287 s.BindInt64(2, 1); | 1303 base::Time now = base::Time::Now(); |
| 1288 s.BindInt64(3, base::Time::Now().ToInternalValue()); | 1304 s.BindInt64(2, 1); // use_count |
| 1305 s.BindInt64(3, now.ToInternalValue()); // use_date | |
| 1306 | |
| 1307 s.BindInt64(4, now.ToInternalValue()); // unmask_date | |
| 1289 | 1308 |
| 1290 s.Run(); | 1309 s.Run(); |
| 1291 return db_->GetLastChangeCount() > 0; | 1310 return db_->GetLastChangeCount() > 0; |
| 1292 } | 1311 } |
| 1293 | 1312 |
| 1294 bool AutofillTable::MaskServerCreditCard(const std::string& id) { | 1313 bool AutofillTable::MaskServerCreditCard(const std::string& id) { |
| 1295 sql::Statement s(db_->GetUniqueStatement( | 1314 sql::Statement s(db_->GetUniqueStatement( |
| 1296 "DELETE FROM unmasked_credit_cards WHERE id = ?")); | 1315 "DELETE FROM unmasked_credit_cards WHERE id = ?")); |
| 1297 s.BindString(0, id); | 1316 s.BindString(0, id); |
| 1298 s.Run(); | 1317 s.Run(); |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 1320 CreditCard* tmp_credit_card = NULL; | 1339 CreditCard* tmp_credit_card = NULL; |
| 1321 if (!GetCreditCard(credit_card.guid(), &tmp_credit_card)) | 1340 if (!GetCreditCard(credit_card.guid(), &tmp_credit_card)) |
| 1322 return false; | 1341 return false; |
| 1323 | 1342 |
| 1324 scoped_ptr<CreditCard> old_credit_card(tmp_credit_card); | 1343 scoped_ptr<CreditCard> old_credit_card(tmp_credit_card); |
| 1325 bool update_modification_date = *old_credit_card != credit_card; | 1344 bool update_modification_date = *old_credit_card != credit_card; |
| 1326 | 1345 |
| 1327 sql::Statement s(db_->GetUniqueStatement( | 1346 sql::Statement s(db_->GetUniqueStatement( |
| 1328 "UPDATE credit_cards " | 1347 "UPDATE credit_cards " |
| 1329 "SET guid=?, name_on_card=?, expiration_month=?," | 1348 "SET guid=?, name_on_card=?, expiration_month=?," |
| 1330 " expiration_year=?, card_number_encrypted=?, use_count=?, use_date=?," | 1349 "expiration_year=?, card_number_encrypted=?, use_count=?, use_date=?," |
| 1331 " date_modified=?, origin=?" | 1350 "date_modified=?, origin=?" |
| 1332 "WHERE guid=?")); | 1351 "WHERE guid=?")); |
| 1333 BindCreditCardToStatement( | 1352 BindCreditCardToStatement( |
| 1334 credit_card, | 1353 credit_card, |
| 1335 update_modification_date ? base::Time::Now() : | 1354 update_modification_date ? base::Time::Now() : |
| 1336 old_credit_card->modification_date(), | 1355 old_credit_card->modification_date(), |
| 1337 &s); | 1356 &s); |
| 1338 s.BindString(9, credit_card.guid()); | 1357 s.BindString(9, credit_card.guid()); |
| 1339 | 1358 |
| 1340 bool result = s.Run(); | 1359 bool result = s.Run(); |
| 1341 DCHECK_GT(db_->GetLastChangeCount(), 0); | 1360 DCHECK_GT(db_->GetLastChangeCount(), 0); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1400 } | 1419 } |
| 1401 if (!s_credit_cards_get.Succeeded()) | 1420 if (!s_credit_cards_get.Succeeded()) |
| 1402 return false; | 1421 return false; |
| 1403 | 1422 |
| 1404 // Remove Autofill credit cards in the time range. | 1423 // Remove Autofill credit cards in the time range. |
| 1405 sql::Statement s_credit_cards(db_->GetUniqueStatement( | 1424 sql::Statement s_credit_cards(db_->GetUniqueStatement( |
| 1406 "DELETE FROM credit_cards " | 1425 "DELETE FROM credit_cards " |
| 1407 "WHERE date_modified >= ? AND date_modified < ?")); | 1426 "WHERE date_modified >= ? AND date_modified < ?")); |
| 1408 s_credit_cards.BindInt64(0, delete_begin_t); | 1427 s_credit_cards.BindInt64(0, delete_begin_t); |
| 1409 s_credit_cards.BindInt64(1, delete_end_t); | 1428 s_credit_cards.BindInt64(1, delete_end_t); |
| 1429 if (!s_credit_cards.Run()) | |
| 1430 return false; | |
| 1410 | 1431 |
| 1411 return s_credit_cards.Run(); | 1432 // Remove unmasked credit cards in the time range. |
| 1433 sql::Statement s_unmasked_cards(db_->GetUniqueStatement( | |
| 1434 "DELETE FROM unmasked_credit_cards " | |
| 1435 "WHERE unmask_date >= ? AND unmask_date < ?")); | |
| 1436 s_unmasked_cards.BindInt64(0, delete_begin.ToInternalValue()); | |
| 1437 s_unmasked_cards.BindInt64(1, delete_end.ToInternalValue()); | |
| 1438 if (!s_unmasked_cards.Run()) | |
| 1439 return false; | |
| 1440 | |
| 1441 return true; | |
| 1412 } | 1442 } |
| 1413 | 1443 |
| 1414 bool AutofillTable::RemoveOriginURLsModifiedBetween( | 1444 bool AutofillTable::RemoveOriginURLsModifiedBetween( |
| 1415 const Time& delete_begin, | 1445 const Time& delete_begin, |
| 1416 const Time& delete_end, | 1446 const Time& delete_end, |
| 1417 ScopedVector<AutofillProfile>* profiles) { | 1447 ScopedVector<AutofillProfile>* profiles) { |
| 1418 DCHECK(delete_end.is_null() || delete_begin < delete_end); | 1448 DCHECK(delete_end.is_null() || delete_begin < delete_end); |
| 1419 | 1449 |
| 1420 time_t delete_begin_t = delete_begin.ToTimeT(); | 1450 time_t delete_begin_t = delete_begin.ToTimeT(); |
| 1421 time_t delete_end_t = GetEndTime(delete_end); | 1451 time_t delete_end_t = GetEndTime(delete_end); |
| (...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1665 } | 1695 } |
| 1666 return true; | 1696 return true; |
| 1667 } | 1697 } |
| 1668 | 1698 |
| 1669 bool AutofillTable::InitUnmaskedCreditCardsTable() { | 1699 bool AutofillTable::InitUnmaskedCreditCardsTable() { |
| 1670 if (!db_->DoesTableExist("unmasked_credit_cards")) { | 1700 if (!db_->DoesTableExist("unmasked_credit_cards")) { |
| 1671 if (!db_->Execute("CREATE TABLE unmasked_credit_cards (" | 1701 if (!db_->Execute("CREATE TABLE unmasked_credit_cards (" |
| 1672 "id VARCHAR," | 1702 "id VARCHAR," |
| 1673 "card_number_encrypted VARCHAR, " | 1703 "card_number_encrypted VARCHAR, " |
| 1674 "use_count INTEGER NOT NULL DEFAULT 0, " | 1704 "use_count INTEGER NOT NULL DEFAULT 0, " |
| 1675 "use_date INTEGER NOT NULL DEFAULT 0)")) { | 1705 "use_date INTEGER NOT NULL DEFAULT 0, " |
| 1706 "unmask_date INTEGER NOT NULL DEFAULT 0)")) { | |
| 1676 NOTREACHED(); | 1707 NOTREACHED(); |
| 1677 return false; | 1708 return false; |
| 1678 } | 1709 } |
| 1679 } | 1710 } |
| 1680 return true; | 1711 return true; |
| 1681 } | 1712 } |
| 1682 | 1713 |
| 1683 bool AutofillTable::InitServerAddressesTable() { | 1714 bool AutofillTable::InitServerAddressesTable() { |
| 1684 if (!db_->DoesTableExist("server_addresses")) { | 1715 if (!db_->DoesTableExist("server_addresses")) { |
| 1685 // The space after language_code is necessary to match what sqlite does | 1716 // The space after language_code is necessary to match what sqlite does |
| (...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1982 | 2013 |
| 1983 bool AutofillTable::MigrateToVersion63AddServerRecipientName() { | 2014 bool AutofillTable::MigrateToVersion63AddServerRecipientName() { |
| 1984 if (!db_->DoesColumnExist("server_addresses", "recipient_name") && | 2015 if (!db_->DoesColumnExist("server_addresses", "recipient_name") && |
| 1985 !db_->Execute("ALTER TABLE server_addresses ADD COLUMN " | 2016 !db_->Execute("ALTER TABLE server_addresses ADD COLUMN " |
| 1986 "recipient_name VARCHAR")) { | 2017 "recipient_name VARCHAR")) { |
| 1987 return false; | 2018 return false; |
| 1988 } | 2019 } |
| 1989 return true; | 2020 return true; |
| 1990 } | 2021 } |
| 1991 | 2022 |
| 2023 bool AutofillTable::MigrateToVersion64AddUnmaskDate() { | |
| 2024 if (!db_->DoesColumnExist("unmasked_credit_cards", "unmask_date") && | |
| 2025 !db_->Execute("ALTER TABLE unmasked_credit_cards ADD COLUMN " | |
| 2026 "unmask_date INTEGER NOT NULL DEFAULT 0")) { | |
| 2027 return false; | |
| 2028 } | |
| 2029 return true; | |
| 2030 } | |
| 2031 | |
| 1992 } // namespace autofill | 2032 } // namespace autofill |
| OLD | NEW |