| 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_); | |
| 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( | 1291 sql::Statement masked_insert( |
| 1303 db_->GetUniqueStatement("INSERT INTO masked_credit_cards(" | 1292 db_->GetUniqueStatement("INSERT INTO masked_credit_cards(" |
| 1304 "id," // 0 | 1293 "id," // 0 |
| 1305 "type," // 1 | 1294 "type," // 1 |
| 1306 "status," // 2 | 1295 "status," // 2 |
| 1307 "name_on_card," // 3 | 1296 "name_on_card," // 3 |
| 1308 "last_four," // 4 | 1297 "last_four," // 4 |
| 1309 "exp_month," // 5 | 1298 "exp_month," // 5 |
| 1310 "exp_year)" // 6 | 1299 "exp_year)" // 6 |
| 1311 "VALUES (?,?,?,?,?,?,?)")); | 1300 "VALUES (?,?,?,?,?,?,?)")); |
| 1312 for (const CreditCard& card : credit_cards) { | 1301 for (const CreditCard& card : credit_cards) { |
| 1313 DCHECK_EQ(CreditCard::MASKED_SERVER_CARD, card.record_type()); | 1302 DCHECK_EQ(CreditCard::MASKED_SERVER_CARD, card.record_type()); |
| 1314 | |
| 1315 masked_insert.BindString(0, card.server_id()); | 1303 masked_insert.BindString(0, card.server_id()); |
| 1316 masked_insert.BindString(1, card.type()); | 1304 masked_insert.BindString(1, card.type()); |
| 1317 masked_insert.BindString(2, | 1305 masked_insert.BindString(2, |
| 1318 ServerStatusEnumToString(card.GetServerStatus())); | 1306 ServerStatusEnumToString(card.GetServerStatus())); |
| 1319 masked_insert.BindString16(3, card.GetRawInfo(CREDIT_CARD_NAME_FULL)); | 1307 masked_insert.BindString16(3, card.GetRawInfo(CREDIT_CARD_NAME_FULL)); |
| 1320 masked_insert.BindString16(4, card.LastFourDigits()); | 1308 masked_insert.BindString16(4, card.LastFourDigits()); |
| 1321 masked_insert.BindString16(5, card.GetRawInfo(CREDIT_CARD_EXP_MONTH)); | 1309 masked_insert.BindString16(5, card.GetRawInfo(CREDIT_CARD_EXP_MONTH)); |
| 1322 masked_insert.BindString16(6, | 1310 masked_insert.BindString16(6, |
| 1323 card.GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR)); | 1311 card.GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR)); |
| 1324 | 1312 |
| 1325 masked_insert.Run(); | 1313 masked_insert.Run(); |
| 1326 masked_insert.Reset(true); | 1314 masked_insert.Reset(true); |
| 1327 | 1315 |
| 1328 // Save the use count and use date of the card. | 1316 // Save the use count and use date of the card. |
| 1329 UpdateServerCardMetadata(card); | 1317 UpdateServerCardMetadata(card); |
| 1330 } | 1318 } |
| 1319 } |
| 1320 |
| 1321 void AutofillTable::SetServerCreditCards( |
| 1322 const std::vector<CreditCard>& credit_cards) { |
| 1323 sql::Transaction transaction(db_); |
| 1324 if (!transaction.Begin()) |
| 1325 return; |
| 1326 |
| 1327 // Delete all old values. |
| 1328 sql::Statement masked_delete( |
| 1329 db_->GetUniqueStatement("DELETE FROM masked_credit_cards")); |
| 1330 masked_delete.Run(); |
| 1331 |
| 1332 AddMaskedCreditCards(credit_cards); |
| 1331 | 1333 |
| 1332 // Delete all items in the unmasked table that aren't in the new set. | 1334 // Delete all items in the unmasked table that aren't in the new set. |
| 1333 sql::Statement unmasked_delete(db_->GetUniqueStatement( | 1335 sql::Statement unmasked_delete(db_->GetUniqueStatement( |
| 1334 "DELETE FROM unmasked_credit_cards WHERE id NOT IN " | 1336 "DELETE FROM unmasked_credit_cards WHERE id NOT IN " |
| 1335 "(SELECT id FROM masked_credit_cards)")); | 1337 "(SELECT id FROM masked_credit_cards)")); |
| 1336 unmasked_delete.Run(); | 1338 unmasked_delete.Run(); |
| 1337 // Do the same for metadata. | 1339 // Do the same for metadata. |
| 1338 sql::Statement metadata_delete(db_->GetUniqueStatement( | 1340 sql::Statement metadata_delete(db_->GetUniqueStatement( |
| 1339 "DELETE FROM server_card_metadata WHERE id NOT IN " | 1341 "DELETE FROM server_card_metadata WHERE id NOT IN " |
| 1340 "(SELECT id FROM masked_credit_cards)")); | 1342 "(SELECT id FROM masked_credit_cards)")); |
| 1341 metadata_delete.Run(); | 1343 metadata_delete.Run(); |
| 1342 | 1344 |
| 1343 transaction.Commit(); | 1345 transaction.Commit(); |
| 1344 } | 1346 } |
| 1345 | 1347 |
| 1346 bool AutofillTable::UnmaskServerCreditCard(const CreditCard& masked, | 1348 bool AutofillTable::AddServerCreditCard(const CreditCard& credit_card) { |
| 1347 const base::string16& full_number) { | 1349 DCHECK_EQ(CreditCard::FULL_SERVER_CARD, credit_card.record_type()); |
| 1350 DCHECK(!credit_card.number().empty()); |
| 1351 DCHECK(!credit_card.server_id().empty()); |
| 1352 |
| 1353 sql::Transaction transaction(db_); |
| 1354 if (!transaction.Begin()) |
| 1355 return false; |
| 1356 |
| 1348 // Make sure there aren't duplicates for this card. | 1357 // Make sure there aren't duplicates for this card. |
| 1349 MaskServerCreditCard(masked.server_id()); | 1358 DeleteFromUnmaskedCreditCards(credit_card.server_id()); |
| 1359 DeleteFromMaskedCreditCards(credit_card.server_id()); |
| 1360 |
| 1361 CreditCard masked(credit_card); |
| 1362 masked.set_record_type(CreditCard::MASKED_SERVER_CARD); |
| 1363 masked.SetNumber(credit_card.LastFourDigits()); |
| 1364 masked.RecordAndLogUse(); |
| 1365 DCHECK(!masked.type().empty()); |
| 1366 AddMaskedCreditCards({masked}); |
| 1367 |
| 1368 AddUnmaskedCreditCard(credit_card.server_id(), credit_card.number()); |
| 1369 |
| 1370 transaction.Commit(); |
| 1371 |
| 1372 return db_->GetLastChangeCount() > 0; |
| 1373 } |
| 1374 |
| 1375 void AutofillTable::AddUnmaskedCreditCard(const std::string& id, |
| 1376 const base::string16& full_number) { |
| 1350 sql::Statement s(db_->GetUniqueStatement( | 1377 sql::Statement s(db_->GetUniqueStatement( |
| 1351 "INSERT INTO unmasked_credit_cards(" | 1378 "INSERT INTO unmasked_credit_cards(" |
| 1352 "id," | 1379 "id," |
| 1353 "card_number_encrypted," | 1380 "card_number_encrypted," |
| 1354 "unmask_date)" | 1381 "unmask_date)" |
| 1355 "VALUES (?,?,?)")); | 1382 "VALUES (?,?,?)")); |
| 1356 s.BindString(0, masked.server_id()); | 1383 s.BindString(0, id); |
| 1357 | 1384 |
| 1358 std::string encrypted_data; | 1385 std::string encrypted_data; |
| 1359 autofill_table_encryptor_->EncryptString16(full_number, &encrypted_data); | 1386 autofill_table_encryptor_->EncryptString16(full_number, &encrypted_data); |
| 1360 s.BindBlob(1, encrypted_data.data(), | 1387 s.BindBlob(1, encrypted_data.data(), |
| 1361 static_cast<int>(encrypted_data.length())); | 1388 static_cast<int>(encrypted_data.length())); |
| 1362 s.BindInt64(2, AutofillClock::Now().ToInternalValue()); // unmask_date | 1389 s.BindInt64(2, AutofillClock::Now().ToInternalValue()); // unmask_date |
| 1363 | 1390 |
| 1364 s.Run(); | 1391 s.Run(); |
| 1392 } |
| 1393 |
| 1394 bool AutofillTable::UnmaskServerCreditCard(const CreditCard& masked, |
| 1395 const base::string16& full_number) { |
| 1396 // Make sure there aren't duplicates for this card. |
| 1397 DeleteFromUnmaskedCreditCards(masked.server_id()); |
| 1398 |
| 1399 AddUnmaskedCreditCard(masked.server_id(), full_number); |
| 1365 | 1400 |
| 1366 CreditCard unmasked = masked; | 1401 CreditCard unmasked = masked; |
| 1367 unmasked.set_record_type(CreditCard::FULL_SERVER_CARD); | 1402 unmasked.set_record_type(CreditCard::FULL_SERVER_CARD); |
| 1368 unmasked.SetNumber(full_number); | 1403 unmasked.SetNumber(full_number); |
| 1369 unmasked.RecordAndLogUse(); | 1404 unmasked.RecordAndLogUse(); |
| 1370 UpdateServerCardMetadata(unmasked); | 1405 UpdateServerCardMetadata(unmasked); |
| 1371 | 1406 |
| 1372 return db_->GetLastChangeCount() > 0; | 1407 return db_->GetLastChangeCount() > 0; |
| 1373 } | 1408 } |
| 1374 | 1409 |
| 1375 bool AutofillTable::MaskServerCreditCard(const std::string& id) { | 1410 bool AutofillTable::DeleteFromMaskedCreditCards(const std::string& id) { |
| 1411 sql::Statement s( |
| 1412 db_->GetUniqueStatement("DELETE FROM masked_credit_cards WHERE id = ?")); |
| 1413 s.BindString(0, id); |
| 1414 s.Run(); |
| 1415 return db_->GetLastChangeCount() > 0; |
| 1416 } |
| 1417 |
| 1418 bool AutofillTable::DeleteFromUnmaskedCreditCards(const std::string& id) { |
| 1376 sql::Statement s(db_->GetUniqueStatement( | 1419 sql::Statement s(db_->GetUniqueStatement( |
| 1377 "DELETE FROM unmasked_credit_cards WHERE id = ?")); | 1420 "DELETE FROM unmasked_credit_cards WHERE id = ?")); |
| 1378 s.BindString(0, id); | 1421 s.BindString(0, id); |
| 1379 s.Run(); | 1422 s.Run(); |
| 1380 return db_->GetLastChangeCount() > 0; | 1423 return db_->GetLastChangeCount() > 0; |
| 1381 } | 1424 } |
| 1382 | 1425 |
| 1426 bool AutofillTable::MaskServerCreditCard(const std::string& id) { |
| 1427 return DeleteFromUnmaskedCreditCards(id); |
| 1428 } |
| 1429 |
| 1383 bool AutofillTable::UpdateServerCardMetadata(const CreditCard& credit_card) { | 1430 bool AutofillTable::UpdateServerCardMetadata(const CreditCard& credit_card) { |
| 1384 DCHECK_NE(CreditCard::LOCAL_CARD, credit_card.record_type()); | 1431 DCHECK_NE(CreditCard::LOCAL_CARD, credit_card.record_type()); |
| 1385 sql::Transaction transaction(db_); | |
| 1386 if (!transaction.Begin()) | |
| 1387 return false; | |
| 1388 | 1432 |
| 1389 sql::Statement remove(db_->GetUniqueStatement( | 1433 sql::Statement remove(db_->GetUniqueStatement( |
| 1390 "DELETE FROM server_card_metadata WHERE id = ?")); | 1434 "DELETE FROM server_card_metadata WHERE id = ?")); |
| 1391 remove.BindString(0, credit_card.server_id()); | 1435 remove.BindString(0, credit_card.server_id()); |
| 1392 remove.Run(); | 1436 remove.Run(); |
| 1393 | 1437 |
| 1394 sql::Statement s( | 1438 sql::Statement s( |
| 1395 db_->GetUniqueStatement("INSERT INTO server_card_metadata(use_count, " | 1439 db_->GetUniqueStatement("INSERT INTO server_card_metadata(use_count, " |
| 1396 "use_date, billing_address_id, id)" | 1440 "use_date, billing_address_id, id)" |
| 1397 "VALUES (?,?,?,?)")); | 1441 "VALUES (?,?,?,?)")); |
| 1398 s.BindInt64(0, credit_card.use_count()); | 1442 s.BindInt64(0, credit_card.use_count()); |
| 1399 s.BindInt64(1, credit_card.use_date().ToInternalValue()); | 1443 s.BindInt64(1, credit_card.use_date().ToInternalValue()); |
| 1400 s.BindString(2, credit_card.billing_address_id()); | 1444 s.BindString(2, credit_card.billing_address_id()); |
| 1401 s.BindString(3, credit_card.server_id()); | 1445 s.BindString(3, credit_card.server_id()); |
| 1402 s.Run(); | 1446 s.Run(); |
| 1403 | 1447 |
| 1404 transaction.Commit(); | |
| 1405 | |
| 1406 return db_->GetLastChangeCount() > 0; | 1448 return db_->GetLastChangeCount() > 0; |
| 1407 } | 1449 } |
| 1408 | 1450 |
| 1409 // TODO(crbug.com/680182): Record the address conversion status when a server | 1451 // TODO(crbug.com/680182): Record the address conversion status when a server |
| 1410 // address gets converted. | 1452 // address gets converted. |
| 1411 bool AutofillTable::UpdateServerAddressMetadata( | 1453 bool AutofillTable::UpdateServerAddressMetadata( |
| 1412 const AutofillProfile& profile) { | 1454 const AutofillProfile& profile) { |
| 1413 DCHECK_EQ(AutofillProfile::SERVER_PROFILE, profile.record_type()); | 1455 DCHECK_EQ(AutofillProfile::SERVER_PROFILE, profile.record_type()); |
| 1414 | 1456 |
| 1415 sql::Transaction transaction(db_); | 1457 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") || | 2578 if (!db_->Execute("DROP TABLE masked_credit_cards") || |
| 2537 !db_->Execute("ALTER TABLE masked_credit_cards_temp " | 2579 !db_->Execute("ALTER TABLE masked_credit_cards_temp " |
| 2538 "RENAME TO masked_credit_cards")) { | 2580 "RENAME TO masked_credit_cards")) { |
| 2539 return false; | 2581 return false; |
| 2540 } | 2582 } |
| 2541 | 2583 |
| 2542 return transaction.Commit(); | 2584 return transaction.Commit(); |
| 2543 } | 2585 } |
| 2544 | 2586 |
| 2545 } // namespace autofill | 2587 } // namespace autofill |
| OLD | NEW |