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

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

Issue 1042353003: Create syncable metadata table for Wallet credit cards and addresses. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: docs Created 5 years, 8 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 <algorithm> 7 #include <algorithm>
8 #include <cmath> 8 #include <cmath>
9 #include <limits> 9 #include <limits>
10 #include <map> 10 #include <map>
(...skipping 419 matching lines...) Expand 10 before | Expand all | Expand 10 after
430 430
431 WebDatabaseTable::TypeKey AutofillTable::GetTypeKey() const { 431 WebDatabaseTable::TypeKey AutofillTable::GetTypeKey() const {
432 return GetKey(); 432 return GetKey();
433 } 433 }
434 434
435 bool AutofillTable::CreateTablesIfNecessary() { 435 bool AutofillTable::CreateTablesIfNecessary() {
436 return (InitMainTable() && InitCreditCardsTable() && InitProfilesTable() && 436 return (InitMainTable() && InitCreditCardsTable() && InitProfilesTable() &&
437 InitProfileNamesTable() && InitProfileEmailsTable() && 437 InitProfileNamesTable() && InitProfileEmailsTable() &&
438 InitProfilePhonesTable() && InitProfileTrashTable() && 438 InitProfilePhonesTable() && InitProfileTrashTable() &&
439 InitMaskedCreditCardsTable() && InitUnmaskedCreditCardsTable() && 439 InitMaskedCreditCardsTable() && InitUnmaskedCreditCardsTable() &&
440 InitServerAddressesTable()); 440 InitServerCardMetadataTable() && InitServerAddressesTable() &&
441 InitServerAddressMetadataTable());
441 } 442 }
442 443
443 bool AutofillTable::IsSyncable() { 444 bool AutofillTable::IsSyncable() {
444 return true; 445 return true;
445 } 446 }
446 447
447 bool AutofillTable::MigrateToVersion(int version, 448 bool AutofillTable::MigrateToVersion(int version,
448 bool* update_compatible_version) { 449 bool* update_compatible_version) {
449 // Migrate if necessary. 450 // Migrate if necessary.
450 switch (version) { 451 switch (version) {
(...skipping 17 matching lines...) Expand all
468 return MigrateToVersion61AddUsageStats(); 469 return MigrateToVersion61AddUsageStats();
469 case 62: 470 case 62:
470 *update_compatible_version = false; 471 *update_compatible_version = false;
471 return MigrateToVersion62AddUsageStatsForUnmaskedCards(); 472 return MigrateToVersion62AddUsageStatsForUnmaskedCards();
472 case 63: 473 case 63:
473 *update_compatible_version = false; 474 *update_compatible_version = false;
474 return MigrateToVersion63AddServerRecipientName(); 475 return MigrateToVersion63AddServerRecipientName();
475 case 64: 476 case 64:
476 *update_compatible_version = false; 477 *update_compatible_version = false;
477 return MigrateToVersion64AddUnmaskDate(); 478 return MigrateToVersion64AddUnmaskDate();
479 case 65:
480 *update_compatible_version = false;
481 return MigrateToVersion65AddServerMetadataTables();
478 } 482 }
479 return true; 483 return true;
480 } 484 }
481 485
482 bool AutofillTable::AddFormFieldValues( 486 bool AutofillTable::AddFormFieldValues(
483 const std::vector<FormFieldData>& elements, 487 const std::vector<FormFieldData>& elements,
484 std::vector<AutofillChange>* changes) { 488 std::vector<AutofillChange>* changes) {
485 return AddFormFieldValuesTime(elements, changes, Time::Now()); 489 return AddFormFieldValuesTime(elements, changes, Time::Now());
486 } 490 }
487 491
(...skipping 409 matching lines...) Expand 10 before | Expand all | Expand 10 after
897 901
898 return s.Succeeded(); 902 return s.Succeeded();
899 } 903 }
900 904
901 bool AutofillTable::GetServerProfiles(std::vector<AutofillProfile*>* profiles) { 905 bool AutofillTable::GetServerProfiles(std::vector<AutofillProfile*>* profiles) {
902 profiles->clear(); 906 profiles->clear();
903 907
904 sql::Statement s(db_->GetUniqueStatement( 908 sql::Statement s(db_->GetUniqueStatement(
905 "SELECT " 909 "SELECT "
906 "id," 910 "id,"
911 "use_count,"
912 "use_date,"
907 "recipient_name," 913 "recipient_name,"
908 "company_name," 914 "company_name,"
909 "street_address," 915 "street_address,"
910 "address_1," // ADDRESS_HOME_STATE 916 "address_1," // ADDRESS_HOME_STATE
911 "address_2," // ADDRESS_HOME_CITY 917 "address_2," // ADDRESS_HOME_CITY
912 "address_3," // ADDRESS_HOME_DEPENDENT_LOCALITY 918 "address_3," // ADDRESS_HOME_DEPENDENT_LOCALITY
913 "address_4," // Not supported in AutofillProfile yet. 919 "address_4," // Not supported in AutofillProfile yet.
914 "postal_code," // ADDRESS_HOME_ZIP 920 "postal_code," // ADDRESS_HOME_ZIP
915 "sorting_code," // ADDRESS_HOME_SORTING_CODE 921 "sorting_code," // ADDRESS_HOME_SORTING_CODE
916 "country_code," // ADDRESS_HOME_COUNTRY 922 "country_code," // ADDRESS_HOME_COUNTRY
917 "phone_number," // PHONE_HOME_WHOLE_NUMBER 923 "phone_number," // PHONE_HOME_WHOLE_NUMBER
918 "language_code " 924 "language_code "
919 "FROM server_addresses")); 925 "FROM server_addresses addresses "
926 "LEFT OUTER JOIN server_address_metadata USING (id)"));
920 927
921 while (s.Step()) { 928 while (s.Step()) {
922 int index = 0; 929 int index = 0;
923 scoped_ptr<AutofillProfile> profile(new AutofillProfile( 930 scoped_ptr<AutofillProfile> profile(new AutofillProfile(
924 AutofillProfile::SERVER_PROFILE, s.ColumnString(index++))); 931 AutofillProfile::SERVER_PROFILE, s.ColumnString(index++)));
932 profile->set_use_count(s.ColumnInt64(index++));
933 profile->set_use_date(
934 base::Time::FromInternalValue(s.ColumnInt64(index++)));
925 935
926 base::string16 recipient_name = s.ColumnString16(index++); 936 base::string16 recipient_name = s.ColumnString16(index++);
927 profile->SetRawInfo(COMPANY_NAME, s.ColumnString16(index++)); 937 profile->SetRawInfo(COMPANY_NAME, s.ColumnString16(index++));
928 profile->SetRawInfo(ADDRESS_HOME_STREET_ADDRESS, s.ColumnString16(index++)); 938 profile->SetRawInfo(ADDRESS_HOME_STREET_ADDRESS, s.ColumnString16(index++));
929 profile->SetRawInfo(ADDRESS_HOME_STATE, s.ColumnString16(index++)); 939 profile->SetRawInfo(ADDRESS_HOME_STATE, s.ColumnString16(index++));
930 profile->SetRawInfo(ADDRESS_HOME_CITY, s.ColumnString16(index++)); 940 profile->SetRawInfo(ADDRESS_HOME_CITY, s.ColumnString16(index++));
931 profile->SetRawInfo(ADDRESS_HOME_DEPENDENT_LOCALITY, 941 profile->SetRawInfo(ADDRESS_HOME_DEPENDENT_LOCALITY,
932 s.ColumnString16(index++)); 942 s.ColumnString16(index++));
933 index++; // Skip address_4 which we haven't added to AutofillProfile yet. 943 index++; // Skip address_4 which we haven't added to AutofillProfile yet.
934 profile->SetRawInfo(ADDRESS_HOME_ZIP, s.ColumnString16(index++)); 944 profile->SetRawInfo(ADDRESS_HOME_ZIP, s.ColumnString16(index++));
(...skipping 19 matching lines...) Expand all
954 const std::vector<AutofillProfile>& profiles) { 964 const std::vector<AutofillProfile>& profiles) {
955 sql::Transaction transaction(db_); 965 sql::Transaction transaction(db_);
956 if (!transaction.Begin()) 966 if (!transaction.Begin())
957 return; 967 return;
958 968
959 // Delete all old ones first. 969 // Delete all old ones first.
960 sql::Statement delete_old(db_->GetUniqueStatement( 970 sql::Statement delete_old(db_->GetUniqueStatement(
961 "DELETE FROM server_addresses")); 971 "DELETE FROM server_addresses"));
962 delete_old.Run(); 972 delete_old.Run();
963 973
974 // Delete metadata for addresses no longer in the list.
975 sql::Statement get_metadata(db_->GetUniqueStatement(
976 "SELECT id FROM server_address_metadata"));
977 while (get_metadata.Step()) {
978 std::string server_id = get_metadata.ColumnString(0);
979 bool found_address = false;
980 for (const AutofillProfile& cur_address : profiles) {
981 if (cur_address.server_id() == server_id) {
982 found_address = true;
983 break;
984 }
985 }
986 if (!found_address) {
987 sql::Statement metadata_delete(db_->GetUniqueStatement(
brettw 2015/04/07 18:04:06 Are you sure that you can delete a row that you ha
988 "DELETE FROM server_address_metadata WHERE id = ?"));
989 metadata_delete.BindString(0, server_id);
990 metadata_delete.Run();
991 DCHECK_EQ(1, db_->GetLastChangeCount());
992 }
993 }
994
964 sql::Statement insert(db_->GetUniqueStatement( 995 sql::Statement insert(db_->GetUniqueStatement(
965 "INSERT INTO server_addresses(" 996 "INSERT INTO server_addresses("
966 "id," 997 "id,"
967 "recipient_name," 998 "recipient_name,"
968 "company_name," 999 "company_name,"
969 "street_address," 1000 "street_address,"
970 "address_1," // ADDRESS_HOME_STATE 1001 "address_1," // ADDRESS_HOME_STATE
971 "address_2," // ADDRESS_HOME_CITY 1002 "address_2," // ADDRESS_HOME_CITY
972 "address_3," // ADDRESS_HOME_DEPENDENT_LOCALITY 1003 "address_3," // ADDRESS_HOME_DEPENDENT_LOCALITY
973 "address_4," // Not supported in AutofillProfile yet. 1004 "address_4," // Not supported in AutofillProfile yet.
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
1148 1179
1149 bool AutofillTable::GetServerCreditCards( 1180 bool AutofillTable::GetServerCreditCards(
1150 std::vector<CreditCard*>* credit_cards) { 1181 std::vector<CreditCard*>* credit_cards) {
1151 credit_cards->clear(); 1182 credit_cards->clear();
1152 1183
1153 sql::Statement s(db_->GetUniqueStatement( 1184 sql::Statement s(db_->GetUniqueStatement(
1154 "SELECT " 1185 "SELECT "
1155 "card_number_encrypted, " // 0 1186 "card_number_encrypted, " // 0
1156 "last_four," // 1 1187 "last_four," // 1
1157 "masked.id," // 2 1188 "masked.id," // 2
1158 "use_count," // 3 1189 "metadata.use_count," // 3
1159 "use_date," // 4 1190 "metadata.use_date," // 4
1160 "type," // 5 1191 "type," // 5
1161 "status," // 6 1192 "status," // 6
1162 "name_on_card," // 7 1193 "name_on_card," // 7
1163 "exp_month," // 8 1194 "exp_month," // 8
1164 "exp_year " // 9 1195 "exp_year " // 9
1165 "FROM masked_credit_cards masked " 1196 "FROM masked_credit_cards masked "
1166 "LEFT OUTER JOIN unmasked_credit_cards unmasked " 1197 "LEFT OUTER JOIN unmasked_credit_cards USING (id) "
1167 "ON masked.id = unmasked.id")); 1198 "LEFT OUTER JOIN server_card_metadata metadata USING (id)"));
1168 while (s.Step()) { 1199 while (s.Step()) {
1169 int index = 0; 1200 int index = 0;
1170 1201
1171 // If the card_number_encrypted field is nonempty, we can assume this card 1202 // If the card_number_encrypted field is nonempty, we can assume this card
1172 // is a full card, otherwise it's masked. 1203 // is a full card, otherwise it's masked.
1173 base::string16 full_card_number = UnencryptedCardFromColumn(s, index++); 1204 base::string16 full_card_number = UnencryptedCardFromColumn(s, index++);
1174 base::string16 last_four = s.ColumnString16(index++); 1205 base::string16 last_four = s.ColumnString16(index++);
1175 CreditCard::RecordType record_type = full_card_number.empty() ? 1206 CreditCard::RecordType record_type = full_card_number.empty() ?
1176 CreditCard::MASKED_SERVER_CARD : 1207 CreditCard::MASKED_SERVER_CARD :
1177 CreditCard::FULL_SERVER_CARD; 1208 CreditCard::FULL_SERVER_CARD;
1178 std::string server_id = s.ColumnString(index++); 1209 std::string server_id = s.ColumnString(index++);
1179 1210
1180 CreditCard* card = new CreditCard(record_type, server_id); 1211 CreditCard* card = new CreditCard(record_type, server_id);
1181 card->SetRawInfo( 1212 card->SetRawInfo(
1182 CREDIT_CARD_NUMBER, 1213 CREDIT_CARD_NUMBER,
1183 record_type == CreditCard::MASKED_SERVER_CARD ? last_four 1214 record_type == CreditCard::MASKED_SERVER_CARD ? last_four
1184 : full_card_number); 1215 : full_card_number);
1185 int64 use_count = s.ColumnInt64(index++); 1216 card->set_use_count(s.ColumnInt64(index++));
1186 int64 use_date = s.ColumnInt64(index++); 1217 card->set_use_date(base::Time::FromInternalValue(s.ColumnInt64(index++)));
1218
1187 std::string card_type = s.ColumnString(index++); 1219 std::string card_type = s.ColumnString(index++);
1188 if (record_type == CreditCard::MASKED_SERVER_CARD) { 1220 if (record_type == CreditCard::MASKED_SERVER_CARD) {
1189 // The type must be set after setting the number to override the 1221 // The type must be set after setting the number to override the
1190 // autodectected type. 1222 // autodectected type.
1191 card->SetTypeForMaskedCard(card_type.c_str()); 1223 card->SetTypeForMaskedCard(card_type.c_str());
1192 DCHECK_EQ(0, use_count);
1193 DCHECK_EQ(0, use_date);
1194 } else { 1224 } else {
1195 DCHECK_EQ(CreditCard::GetCreditCardType(full_card_number), card_type); 1225 DCHECK_EQ(CreditCard::GetCreditCardType(full_card_number), card_type);
1196 card->set_use_count(use_count);
1197 card->set_use_date(base::Time::FromInternalValue(use_date));
1198 } 1226 }
1199 1227
1200 card->SetServerStatus(ServerStatusStringToEnum(s.ColumnString(index++))); 1228 card->SetServerStatus(ServerStatusStringToEnum(s.ColumnString(index++)));
1201 card->SetRawInfo(CREDIT_CARD_NAME, s.ColumnString16(index++)); 1229 card->SetRawInfo(CREDIT_CARD_NAME, s.ColumnString16(index++));
1202 card->SetRawInfo(CREDIT_CARD_EXP_MONTH, s.ColumnString16(index++)); 1230 card->SetRawInfo(CREDIT_CARD_EXP_MONTH, s.ColumnString16(index++));
1203 card->SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, s.ColumnString16(index++)); 1231 card->SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, s.ColumnString16(index++));
1204 credit_cards->push_back(card); 1232 credit_cards->push_back(card);
1205 } 1233 }
1206 1234
1207 return s.Succeeded(); 1235 return s.Succeeded();
(...skipping 28 matching lines...) Expand all
1236 // is compiled every time because it's much more likely that this is never 1264 // is compiled every time because it's much more likely that this is never
1237 // executed than it runs more than once. 1265 // executed than it runs more than once.
1238 sql::Statement unmasked_delete(db_->GetUniqueStatement( 1266 sql::Statement unmasked_delete(db_->GetUniqueStatement(
1239 "DELETE FROM unmasked_credit_cards WHERE id = ?")); 1267 "DELETE FROM unmasked_credit_cards WHERE id = ?"));
1240 unmasked_delete.BindString(0, server_id); 1268 unmasked_delete.BindString(0, server_id);
1241 unmasked_delete.Run(); 1269 unmasked_delete.Run();
1242 DCHECK_EQ(1, db_->GetLastChangeCount()); 1270 DCHECK_EQ(1, db_->GetLastChangeCount());
1243 } 1271 }
1244 } 1272 }
1245 1273
1274 // Do the same for the metadata table.
1275 sql::Statement get_metadata(db_->GetUniqueStatement(
1276 "SELECT id FROM server_card_metadata"));
1277 while (get_metadata.Step()) {
1278 // We expect relatively few cards, just do brute-force.
1279 std::string server_id = get_metadata.ColumnString(0);
1280 bool found_card = false;
1281 for (const CreditCard& cur_card : credit_cards) {
1282 if (cur_card.server_id() == server_id) {
1283 found_card = true;
1284 break;
1285 }
1286 }
1287 if (!found_card) {
1288 sql::Statement metadata_delete(db_->GetUniqueStatement(
brettw 2015/04/07 18:04:06 Ditto about deleting.
Evan Stade 2015/04/07 19:23:32 This code is essentially a copy-paste of the above
1289 "DELETE FROM server_card_metadata WHERE id = ?"));
1290 metadata_delete.BindString(0, server_id);
1291 metadata_delete.Run();
1292 DCHECK_EQ(1, db_->GetLastChangeCount());
1293 }
1294 }
1295
1246 sql::Statement masked_insert(db_->GetUniqueStatement( 1296 sql::Statement masked_insert(db_->GetUniqueStatement(
1247 "INSERT INTO masked_credit_cards(" 1297 "INSERT INTO masked_credit_cards("
1248 "id," // 0 1298 "id," // 0
1249 "type," // 1 1299 "type," // 1
1250 "status," // 2 1300 "status," // 2
1251 "name_on_card," // 3 1301 "name_on_card," // 3
1252 "last_four," // 4 1302 "last_four," // 4
1253 "exp_month," // 4 1303 "exp_month," // 4
1254 "exp_year) " // 5 1304 "exp_year) " // 5
1255 "VALUES (?,?,?,?,?,?,?)")); 1305 "VALUES (?,?,?,?,?,?,?)"));
(...skipping 10 matching lines...) Expand all
1266 masked_insert.BindString16(6, 1316 masked_insert.BindString16(6,
1267 card.GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR)); 1317 card.GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR));
1268 1318
1269 masked_insert.Run(); 1319 masked_insert.Run();
1270 masked_insert.Reset(true); 1320 masked_insert.Reset(true);
1271 } 1321 }
1272 1322
1273 transaction.Commit(); 1323 transaction.Commit();
1274 } 1324 }
1275 1325
1276 bool AutofillTable::UnmaskServerCreditCard(const std::string& id, 1326 bool AutofillTable::UnmaskServerCreditCard(const CreditCard& masked,
1277 const base::string16& full_number) { 1327 const base::string16& full_number) {
1278 // Make sure there aren't duplicates for this card. 1328 // Make sure there aren't duplicates for this card.
1279 MaskServerCreditCard(id); 1329 MaskServerCreditCard(masked.server_id());
1280 sql::Statement s(db_->GetUniqueStatement( 1330 sql::Statement s(db_->GetUniqueStatement(
1281 "INSERT INTO unmasked_credit_cards(" 1331 "INSERT INTO unmasked_credit_cards("
1282 "id," 1332 "id,"
1283 "card_number_encrypted," 1333 "card_number_encrypted,"
1284 "use_count,"
1285 "use_date,"
1286 "unmask_date)" 1334 "unmask_date)"
1287 "VALUES (?,?,?,?,?)")); 1335 "VALUES (?,?,?)"));
1288 s.BindString(0, id); 1336 s.BindString(0, masked.server_id());
1289 1337
1290 std::string encrypted_data; 1338 std::string encrypted_data;
1291 OSCrypt::EncryptString16(full_number, &encrypted_data); 1339 OSCrypt::EncryptString16(full_number, &encrypted_data);
1292 s.BindBlob(1, encrypted_data.data(), 1340 s.BindBlob(1, encrypted_data.data(),
1293 static_cast<int>(encrypted_data.length())); 1341 static_cast<int>(encrypted_data.length()));
1294 1342 s.BindInt64(2, base::Time::Now().ToInternalValue()); // unmask_date
1295 // Unmasking counts as a usage, so set the stats accordingly.
1296 base::Time now = base::Time::Now();
1297 s.BindInt64(2, 1); // use_count
1298 s.BindInt64(3, now.ToInternalValue()); // use_date
1299
1300 s.BindInt64(4, now.ToInternalValue()); // unmask_date
1301 1343
1302 s.Run(); 1344 s.Run();
1345
1346 CreditCard unmasked = masked;
1347 unmasked.set_record_type(CreditCard::FULL_SERVER_CARD);
1348 unmasked.SetNumber(full_number);
1349 unmasked.RecordUse();
1350 UpdateServerCardUsageStats(unmasked);
1351
1303 return db_->GetLastChangeCount() > 0; 1352 return db_->GetLastChangeCount() > 0;
1304 } 1353 }
1305 1354
1306 bool AutofillTable::MaskServerCreditCard(const std::string& id) { 1355 bool AutofillTable::MaskServerCreditCard(const std::string& id) {
1307 sql::Statement s(db_->GetUniqueStatement( 1356 sql::Statement s(db_->GetUniqueStatement(
1308 "DELETE FROM unmasked_credit_cards WHERE id = ?")); 1357 "DELETE FROM unmasked_credit_cards WHERE id = ?"));
1309 s.BindString(0, id); 1358 s.BindString(0, id);
1310 s.Run(); 1359 s.Run();
1311 return db_->GetLastChangeCount() > 0; 1360 return db_->GetLastChangeCount() > 0;
1312 } 1361 }
1313 1362
1314 bool AutofillTable::UpdateUnmaskedCardUsageStats( 1363 bool AutofillTable::UpdateServerCardUsageStats(
1315 const CreditCard& credit_card) { 1364 const CreditCard& credit_card) {
1316 DCHECK_EQ(CreditCard::FULL_SERVER_CARD, credit_card.record_type()); 1365 DCHECK_NE(CreditCard::LOCAL_CARD, credit_card.record_type());
1366 sql::Transaction transaction(db_);
1367 if (!transaction.Begin())
1368 return false;
1369
1370 sql::Statement remove(db_->GetUniqueStatement(
1371 "DELETE FROM server_card_metadata WHERE id = ?"));
1372 remove.BindString(0, credit_card.server_id());
1373 remove.Run();
1317 1374
1318 sql::Statement s(db_->GetUniqueStatement( 1375 sql::Statement s(db_->GetUniqueStatement(
1319 "UPDATE unmasked_credit_cards " 1376 "INSERT INTO server_card_metadata("
brettw 2015/04/07 18:04:06 I think you can join this line and the next.
Evan Stade 2015/04/07 19:23:32 Done.
1320 "SET use_count=?, use_date=? " 1377 "use_count, use_date, id)"
1321 "WHERE id=?")); 1378 "VALUES (?,?,?)"));
1322 s.BindInt64(0, credit_card.use_count()); 1379 s.BindInt64(0, credit_card.use_count());
1323 s.BindInt64(1, credit_card.use_date().ToInternalValue()); 1380 s.BindInt64(1, credit_card.use_date().ToInternalValue());
1324 s.BindString(2, credit_card.server_id()); 1381 s.BindString(2, credit_card.server_id());
1325 s.Run(); 1382 s.Run();
1383
1384 transaction.Commit();
1385
1326 return db_->GetLastChangeCount() > 0; 1386 return db_->GetLastChangeCount() > 0;
1327 } 1387 }
1328 1388
1389 bool AutofillTable::UpdateServerAddressUsageStats(
1390 const AutofillProfile& profile) {
1391 DCHECK_EQ(AutofillProfile::SERVER_PROFILE, profile.record_type());
1392
1393 sql::Transaction transaction(db_);
1394 if (!transaction.Begin())
1395 return false;
1396
1397 sql::Statement remove(db_->GetUniqueStatement(
1398 "DELETE FROM server_address_metadata WHERE id = ?"));
1399 remove.BindString(0, profile.server_id());
1400 remove.Run();
1401
1402 sql::Statement s(db_->GetUniqueStatement(
1403 "INSERT INTO server_address_metadata("
brettw 2015/04/07 18:04:06 Can this and the next be joined?
Evan Stade 2015/04/07 19:23:32 Done.
1404 "use_count, use_date, id)"
1405 "VALUES (?,?,?)"));
1406 s.BindInt64(0, profile.use_count());
1407 s.BindInt64(1, profile.use_date().ToInternalValue());
1408 s.BindString(2, profile.server_id());
1409 s.Run();
1410
1411 transaction.Commit();
1412
1413 return db_->GetLastChangeCount() > 0;
1414 }
1415
1329 bool AutofillTable::ClearAllServerData() { 1416 bool AutofillTable::ClearAllServerData() {
1330 sql::Transaction transaction(db_); 1417 sql::Transaction transaction(db_);
1331 if (!transaction.Begin()) 1418 if (!transaction.Begin())
1332 return false; // Some error, nothing was changed. 1419 return false; // Some error, nothing was changed.
1333 1420
1334 sql::Statement masked(db_->GetUniqueStatement( 1421 sql::Statement masked(db_->GetUniqueStatement(
1335 "DELETE FROM masked_credit_cards")); 1422 "DELETE FROM masked_credit_cards"));
1336 masked.Run(); 1423 masked.Run();
1337 bool changed = db_->GetLastChangeCount() > 0; 1424 bool changed = db_->GetLastChangeCount() > 0;
1338 1425
1339 sql::Statement unmasked(db_->GetUniqueStatement( 1426 sql::Statement unmasked(db_->GetUniqueStatement(
1340 "DELETE FROM unmasked_credit_cards")); 1427 "DELETE FROM unmasked_credit_cards"));
1341 unmasked.Run(); 1428 unmasked.Run();
1342 changed |= db_->GetLastChangeCount() > 0; 1429 changed |= db_->GetLastChangeCount() > 0;
1343 1430
1344 sql::Statement addresses(db_->GetUniqueStatement( 1431 sql::Statement addresses(db_->GetUniqueStatement(
1345 "DELETE FROM server_addresses")); 1432 "DELETE FROM server_addresses"));
1346 addresses.Run(); 1433 addresses.Run();
1347 changed |= db_->GetLastChangeCount() > 0; 1434 changed |= db_->GetLastChangeCount() > 0;
1348 1435
1436 sql::Statement card_metadata(db_->GetUniqueStatement(
1437 "DELETE FROM server_card_metadata"));
1438 card_metadata.Run();
1439 changed |= db_->GetLastChangeCount() > 0;
1440
1441 sql::Statement address_metadata(db_->GetUniqueStatement(
1442 "DELETE FROM server_address_metadata"));
1443 address_metadata.Run();
1444 changed |= db_->GetLastChangeCount() > 0;
1445
1349 transaction.Commit(); 1446 transaction.Commit();
1350 return changed; 1447 return changed;
1351 } 1448 }
1352 1449
1353 bool AutofillTable::UpdateCreditCard(const CreditCard& credit_card) { 1450 bool AutofillTable::UpdateCreditCard(const CreditCard& credit_card) {
1354 DCHECK(base::IsValidGUID(credit_card.guid())); 1451 DCHECK(base::IsValidGUID(credit_card.guid()));
1355 1452
1356 CreditCard* tmp_credit_card = NULL; 1453 CreditCard* tmp_credit_card = NULL;
1357 if (!GetCreditCard(credit_card.guid(), &tmp_credit_card)) 1454 if (!GetCreditCard(credit_card.guid(), &tmp_credit_card))
1358 return false; 1455 return false;
(...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after
1708 } 1805 }
1709 } 1806 }
1710 return true; 1807 return true;
1711 } 1808 }
1712 1809
1713 bool AutofillTable::InitUnmaskedCreditCardsTable() { 1810 bool AutofillTable::InitUnmaskedCreditCardsTable() {
1714 if (!db_->DoesTableExist("unmasked_credit_cards")) { 1811 if (!db_->DoesTableExist("unmasked_credit_cards")) {
1715 if (!db_->Execute("CREATE TABLE unmasked_credit_cards (" 1812 if (!db_->Execute("CREATE TABLE unmasked_credit_cards ("
1716 "id VARCHAR," 1813 "id VARCHAR,"
1717 "card_number_encrypted VARCHAR, " 1814 "card_number_encrypted VARCHAR, "
1718 "use_count INTEGER NOT NULL DEFAULT 0, "
1719 "use_date INTEGER NOT NULL DEFAULT 0, "
1720 "unmask_date INTEGER NOT NULL DEFAULT 0)")) { 1815 "unmask_date INTEGER NOT NULL DEFAULT 0)")) {
1721 NOTREACHED(); 1816 NOTREACHED();
1722 return false; 1817 return false;
1723 } 1818 }
1724 } 1819 }
1725 return true; 1820 return true;
1726 } 1821 }
1727 1822
1823 bool AutofillTable::InitServerCardMetadataTable() {
1824 if (!db_->DoesTableExist("server_card_metadata")) {
1825 if (!db_->Execute("CREATE TABLE server_card_metadata ("
1826 "id VARCHAR NOT NULL,"
1827 "use_count INTEGER NOT NULL DEFAULT 0, "
1828 "use_date INTEGER NOT NULL DEFAULT 0)")) {
1829 NOTREACHED();
1830 return false;
1831 }
1832 }
1833 return true;
1834 }
1835
1728 bool AutofillTable::InitServerAddressesTable() { 1836 bool AutofillTable::InitServerAddressesTable() {
1729 if (!db_->DoesTableExist("server_addresses")) { 1837 if (!db_->DoesTableExist("server_addresses")) {
1730 // The space after language_code is necessary to match what sqlite does 1838 // The space after language_code is necessary to match what sqlite does
1731 // when it appends the column in migration. 1839 // when it appends the column in migration.
1732 if (!db_->Execute("CREATE TABLE server_addresses (" 1840 if (!db_->Execute("CREATE TABLE server_addresses ("
1733 "id VARCHAR," 1841 "id VARCHAR,"
1734 "company_name VARCHAR," 1842 "company_name VARCHAR,"
1735 "street_address VARCHAR," 1843 "street_address VARCHAR,"
1736 "address_1 VARCHAR," 1844 "address_1 VARCHAR,"
1737 "address_2 VARCHAR," 1845 "address_2 VARCHAR,"
1738 "address_3 VARCHAR," 1846 "address_3 VARCHAR,"
1739 "address_4 VARCHAR," 1847 "address_4 VARCHAR,"
1740 "postal_code VARCHAR," 1848 "postal_code VARCHAR,"
1741 "sorting_code VARCHAR," 1849 "sorting_code VARCHAR,"
1742 "country_code VARCHAR," 1850 "country_code VARCHAR,"
1743 "language_code VARCHAR, " // Space required. 1851 "language_code VARCHAR, " // Space required.
1744 "recipient_name VARCHAR, " // Ditto. 1852 "recipient_name VARCHAR, " // Ditto.
1745 "phone_number VARCHAR)")) { 1853 "phone_number VARCHAR)")) {
1746 NOTREACHED(); 1854 NOTREACHED();
1747 return false; 1855 return false;
1748 } 1856 }
1749 } 1857 }
1750 return true; 1858 return true;
1751 } 1859 }
1752 1860
1861 bool AutofillTable::InitServerAddressMetadataTable() {
1862 if (!db_->DoesTableExist("server_address_metadata")) {
1863 if (!db_->Execute("CREATE TABLE server_address_metadata ("
1864 "id VARCHAR NOT NULL,"
1865 "use_count INTEGER NOT NULL DEFAULT 0, "
1866 "use_date INTEGER NOT NULL DEFAULT 0)")) {
1867 NOTREACHED();
1868 return false;
1869 }
1870 }
1871 return true;
1872 }
1873
1753 bool AutofillTable::MigrateToVersion54AddI18nFieldsAndRemoveDeprecatedFields() { 1874 bool AutofillTable::MigrateToVersion54AddI18nFieldsAndRemoveDeprecatedFields() {
1754 sql::Transaction transaction(db_); 1875 sql::Transaction transaction(db_);
1755 if (!transaction.Begin()) 1876 if (!transaction.Begin())
1756 return false; 1877 return false;
1757 1878
1758 // Test the existence of the |address_line_1| column as an indication that a 1879 // Test the existence of the |address_line_1| column as an indication that a
1759 // migration is needed. It is possible that the new |autofill_profile_phones| 1880 // migration is needed. It is possible that the new |autofill_profile_phones|
1760 // schema is in place because the table was newly created when migrating from 1881 // schema is in place because the table was newly created when migrating from
1761 // a pre-version-23 database. 1882 // a pre-version-23 database.
1762 if (db_->DoesColumnExist("autofill_profiles", "address_line_1")) { 1883 if (db_->DoesColumnExist("autofill_profiles", "address_line_1")) {
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after
2047 } 2168 }
2048 if (!db_->DoesColumnExist("server_addresses", "phone_number") && 2169 if (!db_->DoesColumnExist("server_addresses", "phone_number") &&
2049 !db_->Execute("ALTER TABLE server_addresses ADD COLUMN " 2170 !db_->Execute("ALTER TABLE server_addresses ADD COLUMN "
2050 "phone_number VARCHAR")) { 2171 "phone_number VARCHAR")) {
2051 return false; 2172 return false;
2052 } 2173 }
2053 2174
2054 return transaction.Commit(); 2175 return transaction.Commit();
2055 } 2176 }
2056 2177
2178 bool AutofillTable::MigrateToVersion65AddServerMetadataTables() {
2179 sql::Transaction transaction(db_);
2180 if (!transaction.Begin())
2181 return false;
2182
2183 if (!db_->DoesTableExist("server_card_metadata") &&
2184 !db_->Execute("CREATE TABLE server_card_metadata ("
2185 "id VARCHAR NOT NULL,"
2186 "use_count INTEGER NOT NULL DEFAULT 0, "
2187 "use_date INTEGER NOT NULL DEFAULT 0)")) {
2188 return false;
2189 }
2190
2191 // This clobbers existing usage metadata, which is not synced and only
2192 // applies to unmasked cards. Trying to migrate the usage metadata would be
2193 // tricky as multiple devices for the same user get DB upgrades.
2194 if (!db_->Execute("UPDATE unmasked_credit_cards "
2195 "SET use_count=0, use_date=0")) {
2196 return false;
2197 }
2198
2199 if (!db_->DoesTableExist("server_address_metadata") &&
2200 !db_->Execute("CREATE TABLE server_address_metadata ("
2201 "id VARCHAR NOT NULL,"
2202 "use_count INTEGER NOT NULL DEFAULT 0, "
2203 "use_date INTEGER NOT NULL DEFAULT 0)")) {
2204 return false;
2205 }
2206
2207 // Get existing server addresses and generate IDs for them.
2208 sql::Statement s(db_->GetUniqueStatement(
2209 "SELECT "
2210 "id,"
2211 "recipient_name,"
2212 "company_name,"
2213 "street_address,"
2214 "address_1," // ADDRESS_HOME_STATE
2215 "address_2," // ADDRESS_HOME_CITY
2216 "address_3," // ADDRESS_HOME_DEPENDENT_LOCALITY
2217 "address_4," // Not supported in AutofillProfile yet.
2218 "postal_code," // ADDRESS_HOME_ZIP
2219 "sorting_code," // ADDRESS_HOME_SORTING_CODE
2220 "country_code," // ADDRESS_HOME_COUNTRY
2221 "phone_number," // PHONE_HOME_WHOLE_NUMBER
2222 "language_code "
2223 "FROM server_addresses addresses"));
2224 std::vector<AutofillProfile> profiles;
2225 while (s.Step()) {
2226 int index = 0;
2227 AutofillProfile profile(
2228 AutofillProfile::SERVER_PROFILE, s.ColumnString(index++));
2229
2230 base::string16 recipient_name = s.ColumnString16(index++);
2231 profile.SetRawInfo(COMPANY_NAME, s.ColumnString16(index++));
brettw 2015/04/07 18:04:06 It seems bad to duplicate this. Can we call GetSe
Evan Stade 2015/04/07 19:23:32 I explicitly wanted to avoid that. We generally do
brettw 2015/04/07 21:02:31 Oh I see. I think we should have a comment to this
Evan Stade 2015/04/07 21:26:24 I added a comment in the header.
2232 profile.SetRawInfo(ADDRESS_HOME_STREET_ADDRESS, s.ColumnString16(index++));
2233 profile.SetRawInfo(ADDRESS_HOME_STATE, s.ColumnString16(index++));
2234 profile.SetRawInfo(ADDRESS_HOME_CITY, s.ColumnString16(index++));
2235 profile.SetRawInfo(ADDRESS_HOME_DEPENDENT_LOCALITY,
2236 s.ColumnString16(index++));
2237 index++; // Skip address_4 which we haven't added to AutofillProfile yet.
2238 profile.SetRawInfo(ADDRESS_HOME_ZIP, s.ColumnString16(index++));
2239 profile.SetRawInfo(ADDRESS_HOME_SORTING_CODE, s.ColumnString16(index++));
2240 profile.SetRawInfo(ADDRESS_HOME_COUNTRY, s.ColumnString16(index++));
2241 base::string16 phone_number = s.ColumnString16(index++);
2242 profile.set_language_code(s.ColumnString(index++));
2243 profile.SetInfo(AutofillType(NAME_FULL), recipient_name,
2244 profile.language_code());
2245 profile.SetInfo(AutofillType(PHONE_HOME_WHOLE_NUMBER), phone_number,
2246 profile.language_code());
2247 profile.GenerateServerProfileIdentifier();
2248 profiles.push_back(profile);
2249 }
2250
2251 // Reinsert with the generated IDs.
2252 sql::Statement delete_old(db_->GetUniqueStatement(
2253 "DELETE FROM server_addresses"));
2254 delete_old.Run();
2255
2256 sql::Statement insert(db_->GetUniqueStatement(
2257 "INSERT INTO server_addresses("
2258 "id,"
2259 "recipient_name,"
2260 "company_name,"
2261 "street_address,"
2262 "address_1," // ADDRESS_HOME_STATE
2263 "address_2," // ADDRESS_HOME_CITY
2264 "address_3," // ADDRESS_HOME_DEPENDENT_LOCALITY
2265 "address_4," // Not supported in AutofillProfile yet.
2266 "postal_code," // ADDRESS_HOME_ZIP
2267 "sorting_code," // ADDRESS_HOME_SORTING_CODE
2268 "country_code," // ADDRESS_HOME_COUNTRY
2269 "phone_number," // PHONE_HOME_WHOLE_NUMBER
2270 "language_code) "
2271 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)"));
2272 for (const AutofillProfile& profile : profiles) {
2273 int index = 0;
2274 insert.BindString(index++, profile.server_id());
2275 insert.BindString16(index++, profile.GetRawInfo(NAME_FULL));
2276 insert.BindString16(index++, profile.GetRawInfo(COMPANY_NAME));
2277 insert.BindString16(index++,
2278 profile.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS));
2279 insert.BindString16(index++, profile.GetRawInfo(ADDRESS_HOME_STATE));
2280 insert.BindString16(index++, profile.GetRawInfo(ADDRESS_HOME_CITY));
2281 insert.BindString16(index++,
2282 profile.GetRawInfo(ADDRESS_HOME_DEPENDENT_LOCALITY));
2283 index++; // SKip address_4 which we haven't added to AutofillProfile yet.
2284 insert.BindString16(index++, profile.GetRawInfo(ADDRESS_HOME_ZIP));
2285 insert.BindString16(index++, profile.GetRawInfo(ADDRESS_HOME_SORTING_CODE));
2286 insert.BindString16(index++, profile.GetRawInfo(ADDRESS_HOME_COUNTRY));
2287 insert.BindString16(index++, profile.GetRawInfo(PHONE_HOME_WHOLE_NUMBER));
2288 insert.BindString(index++, profile.language_code());
2289 insert.Run();
2290 insert.Reset(true);
2291 }
2292
2293 return transaction.Commit();
2294 }
2295
2057 } // namespace autofill 2296 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698