Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(251)

Side by Side Diff: components/autofill/core/browser/webdata/autofill_table.cc

Issue 2626843004: [Payments] Add billing_address_id and has_converted to autofill_table (Closed)
Patch Set: Addressed Rouslan's comments Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 453 matching lines...) Expand 10 before | Expand all | Expand 10 after
464 return MigrateToVersion65AddServerMetadataTables(); 464 return MigrateToVersion65AddServerMetadataTables();
465 case 66: 465 case 66:
466 *update_compatible_version = false; 466 *update_compatible_version = false;
467 return MigrateToVersion66AddCardBillingAddress(); 467 return MigrateToVersion66AddCardBillingAddress();
468 case 67: 468 case 67:
469 *update_compatible_version = false; 469 *update_compatible_version = false;
470 return MigrateToVersion67AddMaskedCardBillingAddress(); 470 return MigrateToVersion67AddMaskedCardBillingAddress();
471 case 70: 471 case 70:
472 *update_compatible_version = false; 472 *update_compatible_version = false;
473 return MigrateToVersion70AddSyncMetadata(); 473 return MigrateToVersion70AddSyncMetadata();
474 case 71:
475 *update_compatible_version = false;
476 return MigrateToVersion71AddHasConvertedAndBillingAddressIdMetadata();
474 } 477 }
475 return true; 478 return true;
476 } 479 }
477 480
478 bool AutofillTable::AddFormFieldValues( 481 bool AutofillTable::AddFormFieldValues(
479 const std::vector<FormFieldData>& elements, 482 const std::vector<FormFieldData>& elements,
480 std::vector<AutofillChange>* changes) { 483 std::vector<AutofillChange>* changes) {
481 return AddFormFieldValuesTime(elements, changes, Time::Now()); 484 return AddFormFieldValuesTime(elements, changes, Time::Now());
482 } 485 }
483 486
(...skipping 719 matching lines...) Expand 10 before | Expand all | Expand 10 after
1203 1206
1204 return s.Succeeded(); 1207 return s.Succeeded();
1205 } 1208 }
1206 1209
1207 bool AutofillTable::GetServerCreditCards( 1210 bool AutofillTable::GetServerCreditCards(
1208 std::vector<std::unique_ptr<CreditCard>>* credit_cards) const { 1211 std::vector<std::unique_ptr<CreditCard>>* credit_cards) const {
1209 credit_cards->clear(); 1212 credit_cards->clear();
1210 1213
1211 sql::Statement s(db_->GetUniqueStatement( 1214 sql::Statement s(db_->GetUniqueStatement(
1212 "SELECT " 1215 "SELECT "
1213 "card_number_encrypted, " // 0 1216 "card_number_encrypted, " // 0
1214 "last_four," // 1 1217 "last_four," // 1
1215 "masked.id," // 2 1218 "masked.id," // 2
1216 "metadata.use_count," // 3 1219 "metadata.use_count," // 3
1217 "metadata.use_date," // 4 1220 "metadata.use_date," // 4
1218 "type," // 5 1221 "type," // 5
1219 "status," // 6 1222 "status," // 6
1220 "name_on_card," // 7 1223 "name_on_card," // 7
1221 "exp_month," // 8 1224 "exp_month," // 8
1222 "exp_year," // 9 1225 "exp_year," // 9
1223 "billing_address_id " // 10 1226 "metadata.billing_address_id " // 10
1224 "FROM masked_credit_cards masked " 1227 "FROM masked_credit_cards masked "
1225 "LEFT OUTER JOIN unmasked_credit_cards USING (id) " 1228 "LEFT OUTER JOIN unmasked_credit_cards USING (id) "
1226 "LEFT OUTER JOIN server_card_metadata metadata USING (id)")); 1229 "LEFT OUTER JOIN server_card_metadata metadata USING (id)"));
1227 while (s.Step()) { 1230 while (s.Step()) {
1228 int index = 0; 1231 int index = 0;
1229 1232
1230 // If the card_number_encrypted field is nonempty, we can assume this card 1233 // If the card_number_encrypted field is nonempty, we can assume this card
1231 // is a full card, otherwise it's masked. 1234 // is a full card, otherwise it's masked.
1232 base::string16 full_card_number = UnencryptedCardFromColumn(s, index++); 1235 base::string16 full_card_number = UnencryptedCardFromColumn(s, index++);
1233 base::string16 last_four = s.ColumnString16(index++); 1236 base::string16 last_four = s.ColumnString16(index++);
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
1301 masked_insert.BindString16(4, card.LastFourDigits()); 1304 masked_insert.BindString16(4, card.LastFourDigits());
1302 masked_insert.BindString16(5, card.GetRawInfo(CREDIT_CARD_EXP_MONTH)); 1305 masked_insert.BindString16(5, card.GetRawInfo(CREDIT_CARD_EXP_MONTH));
1303 masked_insert.BindString16(6, 1306 masked_insert.BindString16(6,
1304 card.GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR)); 1307 card.GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR));
1305 masked_insert.BindString(7, card.billing_address_id()); 1308 masked_insert.BindString(7, card.billing_address_id());
1306 1309
1307 masked_insert.Run(); 1310 masked_insert.Run();
1308 masked_insert.Reset(true); 1311 masked_insert.Reset(true);
1309 1312
1310 // Save the use count and use date of the card. 1313 // Save the use count and use date of the card.
1311 UpdateServerCardUsageStats(card); 1314 UpdateServerCardMetadata(card);
1312 } 1315 }
1313 1316
1314 // Delete all items in the unmasked table that aren't in the new set. 1317 // Delete all items in the unmasked table that aren't in the new set.
1315 sql::Statement unmasked_delete(db_->GetUniqueStatement( 1318 sql::Statement unmasked_delete(db_->GetUniqueStatement(
1316 "DELETE FROM unmasked_credit_cards WHERE id NOT IN " 1319 "DELETE FROM unmasked_credit_cards WHERE id NOT IN "
1317 "(SELECT id FROM masked_credit_cards)")); 1320 "(SELECT id FROM masked_credit_cards)"));
1318 unmasked_delete.Run(); 1321 unmasked_delete.Run();
1319 // Do the same for metadata. 1322 // Do the same for metadata.
1320 sql::Statement metadata_delete(db_->GetUniqueStatement( 1323 sql::Statement metadata_delete(db_->GetUniqueStatement(
1321 "DELETE FROM server_card_metadata WHERE id NOT IN " 1324 "DELETE FROM server_card_metadata WHERE id NOT IN "
(...skipping 20 matching lines...) Expand all
1342 s.BindBlob(1, encrypted_data.data(), 1345 s.BindBlob(1, encrypted_data.data(),
1343 static_cast<int>(encrypted_data.length())); 1346 static_cast<int>(encrypted_data.length()));
1344 s.BindInt64(2, Time::Now().ToInternalValue()); // unmask_date 1347 s.BindInt64(2, Time::Now().ToInternalValue()); // unmask_date
1345 1348
1346 s.Run(); 1349 s.Run();
1347 1350
1348 CreditCard unmasked = masked; 1351 CreditCard unmasked = masked;
1349 unmasked.set_record_type(CreditCard::FULL_SERVER_CARD); 1352 unmasked.set_record_type(CreditCard::FULL_SERVER_CARD);
1350 unmasked.SetNumber(full_number); 1353 unmasked.SetNumber(full_number);
1351 unmasked.RecordAndLogUse(); 1354 unmasked.RecordAndLogUse();
1352 UpdateServerCardUsageStats(unmasked); 1355 UpdateServerCardMetadata(unmasked);
1353 1356
1354 return db_->GetLastChangeCount() > 0; 1357 return db_->GetLastChangeCount() > 0;
1355 } 1358 }
1356 1359
1357 bool AutofillTable::MaskServerCreditCard(const std::string& id) { 1360 bool AutofillTable::MaskServerCreditCard(const std::string& id) {
1358 sql::Statement s(db_->GetUniqueStatement( 1361 sql::Statement s(db_->GetUniqueStatement(
1359 "DELETE FROM unmasked_credit_cards WHERE id = ?")); 1362 "DELETE FROM unmasked_credit_cards WHERE id = ?"));
1360 s.BindString(0, id); 1363 s.BindString(0, id);
1361 s.Run(); 1364 s.Run();
1362 return db_->GetLastChangeCount() > 0; 1365 return db_->GetLastChangeCount() > 0;
1363 } 1366 }
1364 1367
1365 bool AutofillTable::UpdateServerCardUsageStats( 1368 bool AutofillTable::UpdateServerCardMetadata(const CreditCard& credit_card) {
1366 const CreditCard& credit_card) {
1367 DCHECK_NE(CreditCard::LOCAL_CARD, credit_card.record_type()); 1369 DCHECK_NE(CreditCard::LOCAL_CARD, credit_card.record_type());
1368 sql::Transaction transaction(db_); 1370 sql::Transaction transaction(db_);
1369 if (!transaction.Begin()) 1371 if (!transaction.Begin())
1370 return false; 1372 return false;
1371 1373
1372 sql::Statement remove(db_->GetUniqueStatement( 1374 sql::Statement remove(db_->GetUniqueStatement(
1373 "DELETE FROM server_card_metadata WHERE id = ?")); 1375 "DELETE FROM server_card_metadata WHERE id = ?"));
1374 remove.BindString(0, credit_card.server_id()); 1376 remove.BindString(0, credit_card.server_id());
1375 remove.Run(); 1377 remove.Run();
1376 1378
1377 sql::Statement s(db_->GetUniqueStatement( 1379 sql::Statement s(
1378 "INSERT INTO server_card_metadata(use_count, use_date, id)" 1380 db_->GetUniqueStatement("INSERT INTO server_card_metadata(use_count, "
1379 "VALUES (?,?,?)")); 1381 "use_date, billing_address_id, id)"
1382 "VALUES (?,?,?,?)"));
1380 s.BindInt64(0, credit_card.use_count()); 1383 s.BindInt64(0, credit_card.use_count());
1381 s.BindInt64(1, credit_card.use_date().ToInternalValue()); 1384 s.BindInt64(1, credit_card.use_date().ToInternalValue());
1382 s.BindString(2, credit_card.server_id()); 1385 s.BindString(2, credit_card.billing_address_id());
1386 s.BindString(3, credit_card.server_id());
1383 s.Run(); 1387 s.Run();
1384 1388
1385 transaction.Commit(); 1389 transaction.Commit();
1386 1390
1387 return db_->GetLastChangeCount() > 0; 1391 return db_->GetLastChangeCount() > 0;
1388 } 1392 }
1389 1393
1390 bool AutofillTable::UpdateServerAddressUsageStats( 1394 // TODO(crbug.com/680182): Record the address conversion status when a server
1395 // address gets converted.
1396 bool AutofillTable::UpdateServerAddressMetadata(
1391 const AutofillProfile& profile) { 1397 const AutofillProfile& profile) {
1392 DCHECK_EQ(AutofillProfile::SERVER_PROFILE, profile.record_type()); 1398 DCHECK_EQ(AutofillProfile::SERVER_PROFILE, profile.record_type());
1393 1399
1394 sql::Transaction transaction(db_); 1400 sql::Transaction transaction(db_);
1395 if (!transaction.Begin()) 1401 if (!transaction.Begin())
1396 return false; 1402 return false;
1397 1403
1398 sql::Statement remove(db_->GetUniqueStatement( 1404 sql::Statement remove(db_->GetUniqueStatement(
1399 "DELETE FROM server_address_metadata WHERE id = ?")); 1405 "DELETE FROM server_address_metadata WHERE id = ?"));
1400 remove.BindString(0, profile.server_id()); 1406 remove.BindString(0, profile.server_id());
1401 remove.Run(); 1407 remove.Run();
1402 1408
1403 sql::Statement s(db_->GetUniqueStatement( 1409 sql::Statement s(
1404 "INSERT INTO server_address_metadata(use_count, use_date, id)" 1410 db_->GetUniqueStatement("INSERT INTO server_address_metadata(use_count, "
1405 "VALUES (?,?,?)")); 1411 "use_date, has_converted, id)"
1412 "VALUES (?,?,?,?)"));
1406 s.BindInt64(0, profile.use_count()); 1413 s.BindInt64(0, profile.use_count());
1407 s.BindInt64(1, profile.use_date().ToInternalValue()); 1414 s.BindInt64(1, profile.use_date().ToInternalValue());
1408 s.BindString(2, profile.server_id()); 1415 s.BindBool(2, false);
1416 s.BindString(3, profile.server_id());
1409 s.Run(); 1417 s.Run();
1410 1418
1411 transaction.Commit(); 1419 transaction.Commit();
1412 1420
1413 return db_->GetLastChangeCount() > 0; 1421 return db_->GetLastChangeCount() > 0;
1414 } 1422 }
1415 1423
1416 bool AutofillTable::UpdateServerCardBillingAddress(
1417 const CreditCard& credit_card) {
1418 DCHECK_NE(CreditCard::LOCAL_CARD, credit_card.record_type());
1419
1420 sql::Statement update(db_->GetUniqueStatement(
1421 "UPDATE masked_credit_cards SET billing_address_id = ? "
1422 "WHERE id = ?"));
1423 update.BindString(0, credit_card.billing_address_id());
1424 update.BindString(1, credit_card.server_id());
1425 if (!update.Run())
1426 return false;
1427
1428 return db_->GetLastChangeCount() > 0;
1429 }
1430
1431 bool AutofillTable::ClearAllServerData() { 1424 bool AutofillTable::ClearAllServerData() {
1432 sql::Transaction transaction(db_); 1425 sql::Transaction transaction(db_);
1433 if (!transaction.Begin()) 1426 if (!transaction.Begin())
1434 return false; // Some error, nothing was changed. 1427 return false; // Some error, nothing was changed.
1435 1428
1436 sql::Statement masked(db_->GetUniqueStatement( 1429 sql::Statement masked(db_->GetUniqueStatement(
1437 "DELETE FROM masked_credit_cards")); 1430 "DELETE FROM masked_credit_cards"));
1438 masked.Run(); 1431 masked.Run();
1439 bool changed = db_->GetLastChangeCount() > 0; 1432 bool changed = db_->GetLastChangeCount() > 0;
1440 1433
(...skipping 509 matching lines...) Expand 10 before | Expand all | Expand 10 after
1950 } 1943 }
1951 } 1944 }
1952 return true; 1945 return true;
1953 } 1946 }
1954 1947
1955 bool AutofillTable::InitServerCardMetadataTable() { 1948 bool AutofillTable::InitServerCardMetadataTable() {
1956 if (!db_->DoesTableExist("server_card_metadata")) { 1949 if (!db_->DoesTableExist("server_card_metadata")) {
1957 if (!db_->Execute("CREATE TABLE server_card_metadata (" 1950 if (!db_->Execute("CREATE TABLE server_card_metadata ("
1958 "id VARCHAR NOT NULL," 1951 "id VARCHAR NOT NULL,"
1959 "use_count INTEGER NOT NULL DEFAULT 0, " 1952 "use_count INTEGER NOT NULL DEFAULT 0, "
1960 "use_date INTEGER NOT NULL DEFAULT 0)")) { 1953 "use_date INTEGER NOT NULL DEFAULT 0, "
1954 "billing_address_id VARCHAR)")) {
1961 NOTREACHED(); 1955 NOTREACHED();
1962 return false; 1956 return false;
1963 } 1957 }
1964 } 1958 }
1965 return true; 1959 return true;
1966 } 1960 }
1967 1961
1968 bool AutofillTable::InitServerAddressesTable() { 1962 bool AutofillTable::InitServerAddressesTable() {
1969 if (!db_->DoesTableExist("server_addresses")) { 1963 if (!db_->DoesTableExist("server_addresses")) {
1970 // The space after language_code is necessary to match what sqlite does 1964 // The space after language_code is necessary to match what sqlite does
(...skipping 17 matching lines...) Expand all
1988 } 1982 }
1989 } 1983 }
1990 return true; 1984 return true;
1991 } 1985 }
1992 1986
1993 bool AutofillTable::InitServerAddressMetadataTable() { 1987 bool AutofillTable::InitServerAddressMetadataTable() {
1994 if (!db_->DoesTableExist("server_address_metadata")) { 1988 if (!db_->DoesTableExist("server_address_metadata")) {
1995 if (!db_->Execute("CREATE TABLE server_address_metadata (" 1989 if (!db_->Execute("CREATE TABLE server_address_metadata ("
1996 "id VARCHAR NOT NULL," 1990 "id VARCHAR NOT NULL,"
1997 "use_count INTEGER NOT NULL DEFAULT 0, " 1991 "use_count INTEGER NOT NULL DEFAULT 0, "
1998 "use_date INTEGER NOT NULL DEFAULT 0)")) { 1992 "use_date INTEGER NOT NULL DEFAULT 0, "
1993 "has_converted BOOL NOT NULL DEFAULT FALSE)")) {
1999 NOTREACHED(); 1994 NOTREACHED();
2000 return false; 1995 return false;
2001 } 1996 }
2002 } 1997 }
2003 return true; 1998 return true;
2004 } 1999 }
2005 2000
2006 bool AutofillTable::InitAutofillSyncMetadataTable() { 2001 bool AutofillTable::InitAutofillSyncMetadataTable() {
2007 if (!db_->DoesTableExist("autofill_sync_metadata")) { 2002 if (!db_->DoesTableExist("autofill_sync_metadata")) {
2008 if (!db_->Execute("CREATE TABLE autofill_sync_metadata (" 2003 if (!db_->Execute("CREATE TABLE autofill_sync_metadata ("
(...skipping 457 matching lines...) Expand 10 before | Expand all | Expand 10 after
2466 if (!db_->Execute("CREATE TABLE autofill_sync_metadata (" 2461 if (!db_->Execute("CREATE TABLE autofill_sync_metadata ("
2467 "storage_key VARCHAR PRIMARY KEY NOT NULL," 2462 "storage_key VARCHAR PRIMARY KEY NOT NULL,"
2468 "value BLOB)")) { 2463 "value BLOB)")) {
2469 return false; 2464 return false;
2470 } 2465 }
2471 return db_->Execute( 2466 return db_->Execute(
2472 "CREATE TABLE autofill_model_type_state (id INTEGER PRIMARY KEY, value " 2467 "CREATE TABLE autofill_model_type_state (id INTEGER PRIMARY KEY, value "
2473 "BLOB)"); 2468 "BLOB)");
2474 } 2469 }
2475 2470
2471 bool AutofillTable::MigrateToVersion71AddHasConvertedAndBillingAddressIdMetadata () {
Peter Kasting 2017/01/12 23:37:48 Nit: 80 columns (git cl format will find this sort
sebsg 2017/01/13 15:23:10 Done.
2472 sql::Transaction transaction(db_);
2473 if (!transaction.Begin())
2474 return false;
2475
2476 if (!db_->DoesColumnExist("server_card_metadata", "billing_address_id") &&
2477 !db_->Execute("ALTER TABLE server_card_metadata ADD COLUMN "
2478 "billing_address_id VARCHAR")) {
2479 return false;
2480 }
2481
2482 if (!db_->DoesColumnExist("server_address_metadata", "has_converted") &&
2483 !db_->Execute("ALTER TABLE server_address_metadata ADD COLUMN "
2484 "has_converted BOOL NOT NULL DEFAULT FALSE")) {
2485 return false;
2486 }
2487
2488 // Copy over the billing_address_id from the masked_server_cards to
2489 // server_card_metadata.
2490 if (!db_->Execute("UPDATE server_card_metadata "
2491 "SET billing_address_id = "
2492 "(SELECT billing_address_id "
2493 "FROM masked_credit_cards "
2494 "WHERE id = server_card_metadata.id)")) {
2495 return false;
2496 }
2497
2498 return transaction.Commit();
2499 }
2500
2476 } // namespace autofill 2501 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698