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> |
(...skipping 1237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1248 | 1248 |
1249 // If the card_number_encrypted field is nonempty, we can assume this card | 1249 // If the card_number_encrypted field is nonempty, we can assume this card |
1250 // is a full card, otherwise it's masked. | 1250 // is a full card, otherwise it's masked. |
1251 base::string16 full_card_number = | 1251 base::string16 full_card_number = |
1252 UnencryptedCardFromColumn(s, index++, *autofill_table_encryptor_); | 1252 UnencryptedCardFromColumn(s, index++, *autofill_table_encryptor_); |
1253 base::string16 last_four = s.ColumnString16(index++); | 1253 base::string16 last_four = s.ColumnString16(index++); |
1254 CreditCard::RecordType record_type = full_card_number.empty() ? | 1254 CreditCard::RecordType record_type = full_card_number.empty() ? |
1255 CreditCard::MASKED_SERVER_CARD : | 1255 CreditCard::MASKED_SERVER_CARD : |
1256 CreditCard::FULL_SERVER_CARD; | 1256 CreditCard::FULL_SERVER_CARD; |
1257 std::string server_id = s.ColumnString(index++); | 1257 std::string server_id = s.ColumnString(index++); |
1258 | |
1259 std::unique_ptr<CreditCard> card = | 1258 std::unique_ptr<CreditCard> card = |
1260 base::MakeUnique<CreditCard>(record_type, server_id); | 1259 base::MakeUnique<CreditCard>(record_type, server_id); |
1261 card->SetRawInfo( | 1260 card->SetRawInfo( |
1262 CREDIT_CARD_NUMBER, | 1261 CREDIT_CARD_NUMBER, |
1263 record_type == CreditCard::MASKED_SERVER_CARD ? last_four | 1262 record_type == CreditCard::MASKED_SERVER_CARD ? last_four |
1264 : full_card_number); | 1263 : full_card_number); |
1265 card->set_use_count(s.ColumnInt64(index++)); | 1264 card->set_use_count(s.ColumnInt64(index++)); |
1266 card->set_use_date(Time::FromInternalValue(s.ColumnInt64(index++))); | 1265 card->set_use_date(Time::FromInternalValue(s.ColumnInt64(index++))); |
1267 // Modification date is not tracked for server cards. Explicitly set it here | 1266 // Modification date is not tracked for server cards. Explicitly set it here |
1268 // to override the default value of AutofillClock::Now(). | 1267 // to override the default value of AutofillClock::Now(). |
1269 card->set_modification_date(Time()); | 1268 card->set_modification_date(Time()); |
1270 | 1269 |
1271 std::string card_type = s.ColumnString(index++); | 1270 std::string card_type = s.ColumnString(index++); |
1272 if (record_type == CreditCard::MASKED_SERVER_CARD) { | 1271 if (record_type == CreditCard::MASKED_SERVER_CARD) { |
1273 // The type must be set after setting the number to override the | 1272 // The type must be set after setting the number to override the |
1274 // autodetected type. | 1273 // autodetected type. |
1275 card->SetTypeForMaskedCard(card_type.c_str()); | 1274 card->SetTypeForMaskedCard(card_type.c_str()); |
1276 } else { | 1275 } else { |
1277 DCHECK_EQ(CreditCard::GetCreditCardType(full_card_number), card_type); | 1276 DCHECK_EQ(CreditCard::GetCreditCardType(full_card_number), card_type); |
1278 } | 1277 } |
1279 | 1278 |
1280 card->SetServerStatus(ServerStatusStringToEnum(s.ColumnString(index++))); | 1279 card->SetServerStatus(ServerStatusStringToEnum(s.ColumnString(index++))); |
1281 card->SetRawInfo(CREDIT_CARD_NAME_FULL, s.ColumnString16(index++)); | 1280 card->SetRawInfo(CREDIT_CARD_NAME_FULL, s.ColumnString16(index++)); |
1282 card->SetRawInfo(CREDIT_CARD_EXP_MONTH, s.ColumnString16(index++)); | 1281 card->SetRawInfo(CREDIT_CARD_EXP_MONTH, s.ColumnString16(index++)); |
1283 card->SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, s.ColumnString16(index++)); | 1282 card->SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, s.ColumnString16(index++)); |
1284 card->set_billing_address_id(s.ColumnString(index++)); | 1283 card->set_billing_address_id(s.ColumnString(index++)); |
1285 credit_cards->push_back(std::move(card)); | 1284 credit_cards->push_back(std::move(card)); |
1286 } | 1285 } |
1287 | |
1288 return s.Succeeded(); | 1286 return s.Succeeded(); |
1289 } | 1287 } |
1290 | 1288 |
1291 void AutofillTable::SetServerCreditCards( | 1289 void AutofillTable::AddMaskedCreditCards( |
1292 const std::vector<CreditCard>& credit_cards) { | 1290 const std::vector<CreditCard>& credit_cards) { |
1293 sql::Transaction transaction(db_); | 1291 DCHECK_GT(db_->transaction_nesting(), 0); |
1294 if (!transaction.Begin()) | |
1295 return; | |
1296 | |
1297 // Delete all old values. | |
1298 sql::Statement masked_delete(db_->GetUniqueStatement( | |
1299 "DELETE FROM masked_credit_cards")); | |
1300 masked_delete.Run(); | |
1301 | |
1302 sql::Statement masked_insert( | 1292 sql::Statement masked_insert( |
1303 db_->GetUniqueStatement("INSERT INTO masked_credit_cards(" | 1293 db_->GetUniqueStatement("INSERT INTO masked_credit_cards(" |
1304 "id," // 0 | 1294 "id," // 0 |
1305 "type," // 1 | 1295 "type," // 1 |
1306 "status," // 2 | 1296 "status," // 2 |
1307 "name_on_card," // 3 | 1297 "name_on_card," // 3 |
1308 "last_four," // 4 | 1298 "last_four," // 4 |
1309 "exp_month," // 5 | 1299 "exp_month," // 5 |
1310 "exp_year)" // 6 | 1300 "exp_year)" // 6 |
1311 "VALUES (?,?,?,?,?,?,?)")); | 1301 "VALUES (?,?,?,?,?,?,?)")); |
1312 for (const CreditCard& card : credit_cards) { | 1302 for (const CreditCard& card : credit_cards) { |
1313 DCHECK_EQ(CreditCard::MASKED_SERVER_CARD, card.record_type()); | 1303 DCHECK_EQ(CreditCard::MASKED_SERVER_CARD, card.record_type()); |
1314 | |
1315 masked_insert.BindString(0, card.server_id()); | 1304 masked_insert.BindString(0, card.server_id()); |
1316 masked_insert.BindString(1, card.type()); | 1305 masked_insert.BindString(1, card.type()); |
1317 masked_insert.BindString(2, | 1306 masked_insert.BindString(2, |
1318 ServerStatusEnumToString(card.GetServerStatus())); | 1307 ServerStatusEnumToString(card.GetServerStatus())); |
1319 masked_insert.BindString16(3, card.GetRawInfo(CREDIT_CARD_NAME_FULL)); | 1308 masked_insert.BindString16(3, card.GetRawInfo(CREDIT_CARD_NAME_FULL)); |
1320 masked_insert.BindString16(4, card.LastFourDigits()); | 1309 masked_insert.BindString16(4, card.LastFourDigits()); |
1321 masked_insert.BindString16(5, card.GetRawInfo(CREDIT_CARD_EXP_MONTH)); | 1310 masked_insert.BindString16(5, card.GetRawInfo(CREDIT_CARD_EXP_MONTH)); |
1322 masked_insert.BindString16(6, | 1311 masked_insert.BindString16(6, |
1323 card.GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR)); | 1312 card.GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR)); |
1324 | 1313 |
1325 masked_insert.Run(); | 1314 masked_insert.Run(); |
1326 masked_insert.Reset(true); | 1315 masked_insert.Reset(true); |
1327 | 1316 |
1328 // Save the use count and use date of the card. | 1317 // Save the use count and use date of the card. |
1329 UpdateServerCardMetadata(card); | 1318 UpdateServerCardMetadata(card); |
1330 } | 1319 } |
| 1320 } |
| 1321 |
| 1322 void AutofillTable::SetServerCreditCards( |
| 1323 const std::vector<CreditCard>& credit_cards) { |
| 1324 sql::Transaction transaction(db_); |
| 1325 if (!transaction.Begin()) |
| 1326 return; |
| 1327 |
| 1328 // Delete all old values. |
| 1329 sql::Statement masked_delete( |
| 1330 db_->GetUniqueStatement("DELETE FROM masked_credit_cards")); |
| 1331 masked_delete.Run(); |
| 1332 |
| 1333 AddMaskedCreditCards(credit_cards); |
1331 | 1334 |
1332 // Delete all items in the unmasked table that aren't in the new set. | 1335 // Delete all items in the unmasked table that aren't in the new set. |
1333 sql::Statement unmasked_delete(db_->GetUniqueStatement( | 1336 sql::Statement unmasked_delete(db_->GetUniqueStatement( |
1334 "DELETE FROM unmasked_credit_cards WHERE id NOT IN " | 1337 "DELETE FROM unmasked_credit_cards WHERE id NOT IN " |
1335 "(SELECT id FROM masked_credit_cards)")); | 1338 "(SELECT id FROM masked_credit_cards)")); |
1336 unmasked_delete.Run(); | 1339 unmasked_delete.Run(); |
1337 // Do the same for metadata. | 1340 // Do the same for metadata. |
1338 sql::Statement metadata_delete(db_->GetUniqueStatement( | 1341 sql::Statement metadata_delete(db_->GetUniqueStatement( |
1339 "DELETE FROM server_card_metadata WHERE id NOT IN " | 1342 "DELETE FROM server_card_metadata WHERE id NOT IN " |
1340 "(SELECT id FROM masked_credit_cards)")); | 1343 "(SELECT id FROM masked_credit_cards)")); |
1341 metadata_delete.Run(); | 1344 metadata_delete.Run(); |
1342 | 1345 |
1343 transaction.Commit(); | 1346 transaction.Commit(); |
1344 } | 1347 } |
1345 | 1348 |
1346 bool AutofillTable::UnmaskServerCreditCard(const CreditCard& masked, | 1349 bool AutofillTable::AddFullServerCreditCard(const CreditCard& credit_card) { |
1347 const base::string16& full_number) { | 1350 DCHECK_EQ(CreditCard::FULL_SERVER_CARD, credit_card.record_type()); |
| 1351 DCHECK(!credit_card.number().empty()); |
| 1352 DCHECK(!credit_card.server_id().empty()); |
| 1353 |
| 1354 sql::Transaction transaction(db_); |
| 1355 if (!transaction.Begin()) |
| 1356 return false; |
| 1357 |
1348 // Make sure there aren't duplicates for this card. | 1358 // Make sure there aren't duplicates for this card. |
1349 MaskServerCreditCard(masked.server_id()); | 1359 DeleteFromUnmaskedCreditCards(credit_card.server_id()); |
| 1360 DeleteFromMaskedCreditCards(credit_card.server_id()); |
| 1361 |
| 1362 CreditCard masked(credit_card); |
| 1363 masked.set_record_type(CreditCard::MASKED_SERVER_CARD); |
| 1364 masked.SetNumber(credit_card.LastFourDigits()); |
| 1365 masked.RecordAndLogUse(); |
| 1366 DCHECK(!masked.type().empty()); |
| 1367 AddMaskedCreditCards({masked}); |
| 1368 |
| 1369 AddUnmaskedCreditCard(credit_card.server_id(), credit_card.number()); |
| 1370 |
| 1371 transaction.Commit(); |
| 1372 |
| 1373 return db_->GetLastChangeCount() > 0; |
| 1374 } |
| 1375 |
| 1376 void AutofillTable::AddUnmaskedCreditCard(const std::string& id, |
| 1377 const base::string16& full_number) { |
1350 sql::Statement s(db_->GetUniqueStatement( | 1378 sql::Statement s(db_->GetUniqueStatement( |
1351 "INSERT INTO unmasked_credit_cards(" | 1379 "INSERT INTO unmasked_credit_cards(" |
1352 "id," | 1380 "id," |
1353 "card_number_encrypted," | 1381 "card_number_encrypted," |
1354 "unmask_date)" | 1382 "unmask_date)" |
1355 "VALUES (?,?,?)")); | 1383 "VALUES (?,?,?)")); |
1356 s.BindString(0, masked.server_id()); | 1384 s.BindString(0, id); |
1357 | 1385 |
1358 std::string encrypted_data; | 1386 std::string encrypted_data; |
1359 autofill_table_encryptor_->EncryptString16(full_number, &encrypted_data); | 1387 autofill_table_encryptor_->EncryptString16(full_number, &encrypted_data); |
1360 s.BindBlob(1, encrypted_data.data(), | 1388 s.BindBlob(1, encrypted_data.data(), |
1361 static_cast<int>(encrypted_data.length())); | 1389 static_cast<int>(encrypted_data.length())); |
1362 s.BindInt64(2, AutofillClock::Now().ToInternalValue()); // unmask_date | 1390 s.BindInt64(2, AutofillClock::Now().ToInternalValue()); // unmask_date |
1363 | 1391 |
1364 s.Run(); | 1392 s.Run(); |
| 1393 } |
| 1394 |
| 1395 bool AutofillTable::UnmaskServerCreditCard(const CreditCard& masked, |
| 1396 const base::string16& full_number) { |
| 1397 sql::Transaction transaction(db_); |
| 1398 if (!transaction.Begin()) |
| 1399 return false; |
| 1400 |
| 1401 // Make sure there aren't duplicates for this card. |
| 1402 DeleteFromUnmaskedCreditCards(masked.server_id()); |
| 1403 |
| 1404 AddUnmaskedCreditCard(masked.server_id(), full_number); |
1365 | 1405 |
1366 CreditCard unmasked = masked; | 1406 CreditCard unmasked = masked; |
1367 unmasked.set_record_type(CreditCard::FULL_SERVER_CARD); | 1407 unmasked.set_record_type(CreditCard::FULL_SERVER_CARD); |
1368 unmasked.SetNumber(full_number); | 1408 unmasked.SetNumber(full_number); |
1369 unmasked.RecordAndLogUse(); | 1409 unmasked.RecordAndLogUse(); |
1370 UpdateServerCardMetadata(unmasked); | 1410 UpdateServerCardMetadata(unmasked); |
1371 | 1411 |
| 1412 transaction.Commit(); |
| 1413 |
1372 return db_->GetLastChangeCount() > 0; | 1414 return db_->GetLastChangeCount() > 0; |
1373 } | 1415 } |
1374 | 1416 |
1375 bool AutofillTable::MaskServerCreditCard(const std::string& id) { | 1417 bool AutofillTable::DeleteFromMaskedCreditCards(const std::string& id) { |
| 1418 sql::Statement s( |
| 1419 db_->GetUniqueStatement("DELETE FROM masked_credit_cards WHERE id = ?")); |
| 1420 s.BindString(0, id); |
| 1421 s.Run(); |
| 1422 return db_->GetLastChangeCount() > 0; |
| 1423 } |
| 1424 |
| 1425 bool AutofillTable::DeleteFromUnmaskedCreditCards(const std::string& id) { |
1376 sql::Statement s(db_->GetUniqueStatement( | 1426 sql::Statement s(db_->GetUniqueStatement( |
1377 "DELETE FROM unmasked_credit_cards WHERE id = ?")); | 1427 "DELETE FROM unmasked_credit_cards WHERE id = ?")); |
1378 s.BindString(0, id); | 1428 s.BindString(0, id); |
1379 s.Run(); | 1429 s.Run(); |
1380 return db_->GetLastChangeCount() > 0; | 1430 return db_->GetLastChangeCount() > 0; |
1381 } | 1431 } |
1382 | 1432 |
| 1433 bool AutofillTable::MaskServerCreditCard(const std::string& id) { |
| 1434 return DeleteFromUnmaskedCreditCards(id); |
| 1435 } |
| 1436 |
1383 bool AutofillTable::UpdateServerCardMetadata(const CreditCard& credit_card) { | 1437 bool AutofillTable::UpdateServerCardMetadata(const CreditCard& credit_card) { |
1384 DCHECK_NE(CreditCard::LOCAL_CARD, credit_card.record_type()); | 1438 DCHECK_NE(CreditCard::LOCAL_CARD, credit_card.record_type()); |
1385 sql::Transaction transaction(db_); | |
1386 if (!transaction.Begin()) | |
1387 return false; | |
1388 | 1439 |
1389 sql::Statement remove(db_->GetUniqueStatement( | 1440 sql::Statement remove(db_->GetUniqueStatement( |
1390 "DELETE FROM server_card_metadata WHERE id = ?")); | 1441 "DELETE FROM server_card_metadata WHERE id = ?")); |
1391 remove.BindString(0, credit_card.server_id()); | 1442 remove.BindString(0, credit_card.server_id()); |
1392 remove.Run(); | 1443 remove.Run(); |
1393 | 1444 |
1394 sql::Statement s( | 1445 sql::Statement s( |
1395 db_->GetUniqueStatement("INSERT INTO server_card_metadata(use_count, " | 1446 db_->GetUniqueStatement("INSERT INTO server_card_metadata(use_count, " |
1396 "use_date, billing_address_id, id)" | 1447 "use_date, billing_address_id, id)" |
1397 "VALUES (?,?,?,?)")); | 1448 "VALUES (?,?,?,?)")); |
1398 s.BindInt64(0, credit_card.use_count()); | 1449 s.BindInt64(0, credit_card.use_count()); |
1399 s.BindInt64(1, credit_card.use_date().ToInternalValue()); | 1450 s.BindInt64(1, credit_card.use_date().ToInternalValue()); |
1400 s.BindString(2, credit_card.billing_address_id()); | 1451 s.BindString(2, credit_card.billing_address_id()); |
1401 s.BindString(3, credit_card.server_id()); | 1452 s.BindString(3, credit_card.server_id()); |
1402 s.Run(); | 1453 s.Run(); |
1403 | 1454 |
1404 transaction.Commit(); | |
1405 | |
1406 return db_->GetLastChangeCount() > 0; | 1455 return db_->GetLastChangeCount() > 0; |
1407 } | 1456 } |
1408 | 1457 |
1409 // TODO(crbug.com/680182): Record the address conversion status when a server | 1458 // TODO(crbug.com/680182): Record the address conversion status when a server |
1410 // address gets converted. | 1459 // address gets converted. |
1411 bool AutofillTable::UpdateServerAddressMetadata( | 1460 bool AutofillTable::UpdateServerAddressMetadata( |
1412 const AutofillProfile& profile) { | 1461 const AutofillProfile& profile) { |
1413 DCHECK_EQ(AutofillProfile::SERVER_PROFILE, profile.record_type()); | 1462 DCHECK_EQ(AutofillProfile::SERVER_PROFILE, profile.record_type()); |
1414 | 1463 |
1415 sql::Transaction transaction(db_); | 1464 sql::Transaction transaction(db_); |
(...skipping 1120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2536 if (!db_->Execute("DROP TABLE masked_credit_cards") || | 2585 if (!db_->Execute("DROP TABLE masked_credit_cards") || |
2537 !db_->Execute("ALTER TABLE masked_credit_cards_temp " | 2586 !db_->Execute("ALTER TABLE masked_credit_cards_temp " |
2538 "RENAME TO masked_credit_cards")) { | 2587 "RENAME TO masked_credit_cards")) { |
2539 return false; | 2588 return false; |
2540 } | 2589 } |
2541 | 2590 |
2542 return transaction.Commit(); | 2591 return transaction.Commit(); |
2543 } | 2592 } |
2544 | 2593 |
2545 } // namespace autofill | 2594 } // namespace autofill |
OLD | NEW |