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

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: comment about migration code 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 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
994 insert.BindString16(index++, profile.GetRawInfo(ADDRESS_HOME_ZIP)); 1004 insert.BindString16(index++, profile.GetRawInfo(ADDRESS_HOME_ZIP));
995 insert.BindString16(index++, profile.GetRawInfo(ADDRESS_HOME_SORTING_CODE)); 1005 insert.BindString16(index++, profile.GetRawInfo(ADDRESS_HOME_SORTING_CODE));
996 insert.BindString16(index++, profile.GetRawInfo(ADDRESS_HOME_COUNTRY)); 1006 insert.BindString16(index++, profile.GetRawInfo(ADDRESS_HOME_COUNTRY));
997 insert.BindString16(index++, profile.GetRawInfo(PHONE_HOME_WHOLE_NUMBER)); 1007 insert.BindString16(index++, profile.GetRawInfo(PHONE_HOME_WHOLE_NUMBER));
998 insert.BindString(index++, profile.language_code()); 1008 insert.BindString(index++, profile.language_code());
999 1009
1000 insert.Run(); 1010 insert.Run();
1001 insert.Reset(true); 1011 insert.Reset(true);
1002 } 1012 }
1003 1013
1014 // Delete metadata that's no longer relevant.
1015 sql::Statement metadata_delete(db_->GetUniqueStatement(
1016 "DELETE FROM server_address_metadata WHERE id NOT IN "
1017 "(SELECT id FROM server_addresses)"));
1018 metadata_delete.Run();
1019
1004 transaction.Commit(); 1020 transaction.Commit();
1005 } 1021 }
1006 1022
1007 bool AutofillTable::UpdateAutofillProfile(const AutofillProfile& profile) { 1023 bool AutofillTable::UpdateAutofillProfile(const AutofillProfile& profile) {
1008 DCHECK(base::IsValidGUID(profile.guid())); 1024 DCHECK(base::IsValidGUID(profile.guid()));
1009 1025
1010 // Don't update anything until the trash has been emptied. There may be 1026 // Don't update anything until the trash has been emptied. There may be
1011 // pending modifications to process. 1027 // pending modifications to process.
1012 if (!IsAutofillProfilesTrashEmpty()) 1028 if (!IsAutofillProfilesTrashEmpty())
1013 return true; 1029 return true;
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
1148 1164
1149 bool AutofillTable::GetServerCreditCards( 1165 bool AutofillTable::GetServerCreditCards(
1150 std::vector<CreditCard*>* credit_cards) { 1166 std::vector<CreditCard*>* credit_cards) {
1151 credit_cards->clear(); 1167 credit_cards->clear();
1152 1168
1153 sql::Statement s(db_->GetUniqueStatement( 1169 sql::Statement s(db_->GetUniqueStatement(
1154 "SELECT " 1170 "SELECT "
1155 "card_number_encrypted, " // 0 1171 "card_number_encrypted, " // 0
1156 "last_four," // 1 1172 "last_four," // 1
1157 "masked.id," // 2 1173 "masked.id," // 2
1158 "use_count," // 3 1174 "metadata.use_count," // 3
1159 "use_date," // 4 1175 "metadata.use_date," // 4
1160 "type," // 5 1176 "type," // 5
1161 "status," // 6 1177 "status," // 6
1162 "name_on_card," // 7 1178 "name_on_card," // 7
1163 "exp_month," // 8 1179 "exp_month," // 8
1164 "exp_year " // 9 1180 "exp_year " // 9
1165 "FROM masked_credit_cards masked " 1181 "FROM masked_credit_cards masked "
1166 "LEFT OUTER JOIN unmasked_credit_cards unmasked " 1182 "LEFT OUTER JOIN unmasked_credit_cards USING (id) "
1167 "ON masked.id = unmasked.id")); 1183 "LEFT OUTER JOIN server_card_metadata metadata USING (id)"));
1168 while (s.Step()) { 1184 while (s.Step()) {
1169 int index = 0; 1185 int index = 0;
1170 1186
1171 // If the card_number_encrypted field is nonempty, we can assume this card 1187 // If the card_number_encrypted field is nonempty, we can assume this card
1172 // is a full card, otherwise it's masked. 1188 // is a full card, otherwise it's masked.
1173 base::string16 full_card_number = UnencryptedCardFromColumn(s, index++); 1189 base::string16 full_card_number = UnencryptedCardFromColumn(s, index++);
1174 base::string16 last_four = s.ColumnString16(index++); 1190 base::string16 last_four = s.ColumnString16(index++);
1175 CreditCard::RecordType record_type = full_card_number.empty() ? 1191 CreditCard::RecordType record_type = full_card_number.empty() ?
1176 CreditCard::MASKED_SERVER_CARD : 1192 CreditCard::MASKED_SERVER_CARD :
1177 CreditCard::FULL_SERVER_CARD; 1193 CreditCard::FULL_SERVER_CARD;
1178 std::string server_id = s.ColumnString(index++); 1194 std::string server_id = s.ColumnString(index++);
1179 1195
1180 CreditCard* card = new CreditCard(record_type, server_id); 1196 CreditCard* card = new CreditCard(record_type, server_id);
1181 card->SetRawInfo( 1197 card->SetRawInfo(
1182 CREDIT_CARD_NUMBER, 1198 CREDIT_CARD_NUMBER,
1183 record_type == CreditCard::MASKED_SERVER_CARD ? last_four 1199 record_type == CreditCard::MASKED_SERVER_CARD ? last_four
1184 : full_card_number); 1200 : full_card_number);
1185 int64 use_count = s.ColumnInt64(index++); 1201 card->set_use_count(s.ColumnInt64(index++));
1186 int64 use_date = s.ColumnInt64(index++); 1202 card->set_use_date(base::Time::FromInternalValue(s.ColumnInt64(index++)));
1203
1187 std::string card_type = s.ColumnString(index++); 1204 std::string card_type = s.ColumnString(index++);
1188 if (record_type == CreditCard::MASKED_SERVER_CARD) { 1205 if (record_type == CreditCard::MASKED_SERVER_CARD) {
1189 // The type must be set after setting the number to override the 1206 // The type must be set after setting the number to override the
1190 // autodectected type. 1207 // autodectected type.
1191 card->SetTypeForMaskedCard(card_type.c_str()); 1208 card->SetTypeForMaskedCard(card_type.c_str());
1192 DCHECK_EQ(0, use_count);
1193 DCHECK_EQ(0, use_date);
1194 } else { 1209 } else {
1195 DCHECK_EQ(CreditCard::GetCreditCardType(full_card_number), card_type); 1210 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 } 1211 }
1199 1212
1200 card->SetServerStatus(ServerStatusStringToEnum(s.ColumnString(index++))); 1213 card->SetServerStatus(ServerStatusStringToEnum(s.ColumnString(index++)));
1201 card->SetRawInfo(CREDIT_CARD_NAME, s.ColumnString16(index++)); 1214 card->SetRawInfo(CREDIT_CARD_NAME, s.ColumnString16(index++));
1202 card->SetRawInfo(CREDIT_CARD_EXP_MONTH, s.ColumnString16(index++)); 1215 card->SetRawInfo(CREDIT_CARD_EXP_MONTH, s.ColumnString16(index++));
1203 card->SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, s.ColumnString16(index++)); 1216 card->SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, s.ColumnString16(index++));
1204 credit_cards->push_back(card); 1217 credit_cards->push_back(card);
1205 } 1218 }
1206 1219
1207 return s.Succeeded(); 1220 return s.Succeeded();
1208 } 1221 }
1209 1222
1210 void AutofillTable::SetServerCreditCards( 1223 void AutofillTable::SetServerCreditCards(
1211 const std::vector<CreditCard>& credit_cards) { 1224 const std::vector<CreditCard>& credit_cards) {
1212 sql::Transaction transaction(db_); 1225 sql::Transaction transaction(db_);
1213 if (!transaction.Begin()) 1226 if (!transaction.Begin())
1214 return; 1227 return;
1215 1228
1216 // Delete all old values. 1229 // Delete all old values.
1217 sql::Statement masked_delete(db_->GetUniqueStatement( 1230 sql::Statement masked_delete(db_->GetUniqueStatement(
1218 "DELETE FROM masked_credit_cards")); 1231 "DELETE FROM masked_credit_cards"));
1219 masked_delete.Run(); 1232 masked_delete.Run();
1220 1233
1221 // Delete all items in the unmasked table that aren't in the new set.
1222 sql::Statement get_unmasked(db_->GetUniqueStatement(
1223 "SELECT id FROM unmasked_credit_cards"));
1224 while (get_unmasked.Step()) {
1225 // We expect relatively few cards, just do brute-force.
1226 std::string server_id = get_unmasked.ColumnString(0);
1227 bool found_card = false;
1228 for (const CreditCard& cur_card : credit_cards) {
1229 if (cur_card.server_id() == server_id) {
1230 found_card = true;
1231 break;
1232 }
1233 }
1234 if (!found_card) {
1235 // This unmasked card in the DB isn't present in the input. The statement
1236 // is compiled every time because it's much more likely that this is never
1237 // executed than it runs more than once.
1238 sql::Statement unmasked_delete(db_->GetUniqueStatement(
1239 "DELETE FROM unmasked_credit_cards WHERE id = ?"));
1240 unmasked_delete.BindString(0, server_id);
1241 unmasked_delete.Run();
1242 DCHECK_EQ(1, db_->GetLastChangeCount());
1243 }
1244 }
1245
1246 sql::Statement masked_insert(db_->GetUniqueStatement( 1234 sql::Statement masked_insert(db_->GetUniqueStatement(
1247 "INSERT INTO masked_credit_cards(" 1235 "INSERT INTO masked_credit_cards("
1248 "id," // 0 1236 "id," // 0
1249 "type," // 1 1237 "type," // 1
1250 "status," // 2 1238 "status," // 2
1251 "name_on_card," // 3 1239 "name_on_card," // 3
1252 "last_four," // 4 1240 "last_four," // 4
1253 "exp_month," // 4 1241 "exp_month," // 4
1254 "exp_year) " // 5 1242 "exp_year) " // 5
1255 "VALUES (?,?,?,?,?,?,?)")); 1243 "VALUES (?,?,?,?,?,?,?)"));
1256 for (const CreditCard& card : credit_cards) { 1244 for (const CreditCard& card : credit_cards) {
1257 DCHECK_EQ(CreditCard::MASKED_SERVER_CARD, card.record_type()); 1245 DCHECK_EQ(CreditCard::MASKED_SERVER_CARD, card.record_type());
1258 1246
1259 masked_insert.BindString(0, card.server_id()); 1247 masked_insert.BindString(0, card.server_id());
1260 masked_insert.BindString(1, card.type()); 1248 masked_insert.BindString(1, card.type());
1261 masked_insert.BindString(2, 1249 masked_insert.BindString(2,
1262 ServerStatusEnumToString(card.GetServerStatus())); 1250 ServerStatusEnumToString(card.GetServerStatus()));
1263 masked_insert.BindString16(3, card.GetRawInfo(CREDIT_CARD_NAME)); 1251 masked_insert.BindString16(3, card.GetRawInfo(CREDIT_CARD_NAME));
1264 masked_insert.BindString16(4, card.LastFourDigits()); 1252 masked_insert.BindString16(4, card.LastFourDigits());
1265 masked_insert.BindString16(5, card.GetRawInfo(CREDIT_CARD_EXP_MONTH)); 1253 masked_insert.BindString16(5, card.GetRawInfo(CREDIT_CARD_EXP_MONTH));
1266 masked_insert.BindString16(6, 1254 masked_insert.BindString16(6,
1267 card.GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR)); 1255 card.GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR));
1268 1256
1269 masked_insert.Run(); 1257 masked_insert.Run();
1270 masked_insert.Reset(true); 1258 masked_insert.Reset(true);
1271 } 1259 }
1272 1260
1261 // Delete all items in the unmasked table that aren't in the new set.
1262 sql::Statement unmasked_delete(db_->GetUniqueStatement(
1263 "DELETE FROM unmasked_credit_cards WHERE id NOT IN "
1264 "(SELECT id FROM masked_credit_cards)"));
1265 unmasked_delete.Run();
1266 // Do the same for metadata.
1267 sql::Statement metadata_delete(db_->GetUniqueStatement(
1268 "DELETE FROM server_card_metadata WHERE id NOT IN "
1269 "(SELECT id FROM masked_credit_cards)"));
1270 metadata_delete.Run();
1271
1273 transaction.Commit(); 1272 transaction.Commit();
1274 } 1273 }
1275 1274
1276 bool AutofillTable::UnmaskServerCreditCard(const std::string& id, 1275 bool AutofillTable::UnmaskServerCreditCard(const CreditCard& masked,
1277 const base::string16& full_number) { 1276 const base::string16& full_number) {
1278 // Make sure there aren't duplicates for this card. 1277 // Make sure there aren't duplicates for this card.
1279 MaskServerCreditCard(id); 1278 MaskServerCreditCard(masked.server_id());
1280 sql::Statement s(db_->GetUniqueStatement( 1279 sql::Statement s(db_->GetUniqueStatement(
1281 "INSERT INTO unmasked_credit_cards(" 1280 "INSERT INTO unmasked_credit_cards("
1282 "id," 1281 "id,"
1283 "card_number_encrypted," 1282 "card_number_encrypted,"
1284 "use_count,"
1285 "use_date,"
1286 "unmask_date)" 1283 "unmask_date)"
1287 "VALUES (?,?,?,?,?)")); 1284 "VALUES (?,?,?)"));
1288 s.BindString(0, id); 1285 s.BindString(0, masked.server_id());
1289 1286
1290 std::string encrypted_data; 1287 std::string encrypted_data;
1291 OSCrypt::EncryptString16(full_number, &encrypted_data); 1288 OSCrypt::EncryptString16(full_number, &encrypted_data);
1292 s.BindBlob(1, encrypted_data.data(), 1289 s.BindBlob(1, encrypted_data.data(),
1293 static_cast<int>(encrypted_data.length())); 1290 static_cast<int>(encrypted_data.length()));
1294 1291 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 1292
1302 s.Run(); 1293 s.Run();
1294
1295 CreditCard unmasked = masked;
1296 unmasked.set_record_type(CreditCard::FULL_SERVER_CARD);
1297 unmasked.SetNumber(full_number);
1298 unmasked.RecordUse();
1299 UpdateServerCardUsageStats(unmasked);
1300
1303 return db_->GetLastChangeCount() > 0; 1301 return db_->GetLastChangeCount() > 0;
1304 } 1302 }
1305 1303
1306 bool AutofillTable::MaskServerCreditCard(const std::string& id) { 1304 bool AutofillTable::MaskServerCreditCard(const std::string& id) {
1307 sql::Statement s(db_->GetUniqueStatement( 1305 sql::Statement s(db_->GetUniqueStatement(
1308 "DELETE FROM unmasked_credit_cards WHERE id = ?")); 1306 "DELETE FROM unmasked_credit_cards WHERE id = ?"));
1309 s.BindString(0, id); 1307 s.BindString(0, id);
1310 s.Run(); 1308 s.Run();
1311 return db_->GetLastChangeCount() > 0; 1309 return db_->GetLastChangeCount() > 0;
1312 } 1310 }
1313 1311
1314 bool AutofillTable::UpdateUnmaskedCardUsageStats( 1312 bool AutofillTable::UpdateServerCardUsageStats(
1315 const CreditCard& credit_card) { 1313 const CreditCard& credit_card) {
1316 DCHECK_EQ(CreditCard::FULL_SERVER_CARD, credit_card.record_type()); 1314 DCHECK_NE(CreditCard::LOCAL_CARD, credit_card.record_type());
1315 sql::Transaction transaction(db_);
1316 if (!transaction.Begin())
1317 return false;
1318
1319 sql::Statement remove(db_->GetUniqueStatement(
1320 "DELETE FROM server_card_metadata WHERE id = ?"));
1321 remove.BindString(0, credit_card.server_id());
1322 remove.Run();
1317 1323
1318 sql::Statement s(db_->GetUniqueStatement( 1324 sql::Statement s(db_->GetUniqueStatement(
1319 "UPDATE unmasked_credit_cards " 1325 "INSERT INTO server_card_metadata(use_count, use_date, id)"
1320 "SET use_count=?, use_date=? " 1326 "VALUES (?,?,?)"));
1321 "WHERE id=?"));
1322 s.BindInt64(0, credit_card.use_count()); 1327 s.BindInt64(0, credit_card.use_count());
1323 s.BindInt64(1, credit_card.use_date().ToInternalValue()); 1328 s.BindInt64(1, credit_card.use_date().ToInternalValue());
1324 s.BindString(2, credit_card.server_id()); 1329 s.BindString(2, credit_card.server_id());
1325 s.Run(); 1330 s.Run();
1331
1332 transaction.Commit();
1333
1326 return db_->GetLastChangeCount() > 0; 1334 return db_->GetLastChangeCount() > 0;
1327 } 1335 }
1328 1336
1337 bool AutofillTable::UpdateServerAddressUsageStats(
1338 const AutofillProfile& profile) {
1339 DCHECK_EQ(AutofillProfile::SERVER_PROFILE, profile.record_type());
1340
1341 sql::Transaction transaction(db_);
1342 if (!transaction.Begin())
1343 return false;
1344
1345 sql::Statement remove(db_->GetUniqueStatement(
1346 "DELETE FROM server_address_metadata WHERE id = ?"));
1347 remove.BindString(0, profile.server_id());
1348 remove.Run();
1349
1350 sql::Statement s(db_->GetUniqueStatement(
1351 "INSERT INTO server_address_metadata(use_count, use_date, id)"
1352 "VALUES (?,?,?)"));
1353 s.BindInt64(0, profile.use_count());
1354 s.BindInt64(1, profile.use_date().ToInternalValue());
1355 s.BindString(2, profile.server_id());
1356 s.Run();
1357
1358 transaction.Commit();
1359
1360 return db_->GetLastChangeCount() > 0;
1361 }
1362
1329 bool AutofillTable::ClearAllServerData() { 1363 bool AutofillTable::ClearAllServerData() {
1330 sql::Transaction transaction(db_); 1364 sql::Transaction transaction(db_);
1331 if (!transaction.Begin()) 1365 if (!transaction.Begin())
1332 return false; // Some error, nothing was changed. 1366 return false; // Some error, nothing was changed.
1333 1367
1334 sql::Statement masked(db_->GetUniqueStatement( 1368 sql::Statement masked(db_->GetUniqueStatement(
1335 "DELETE FROM masked_credit_cards")); 1369 "DELETE FROM masked_credit_cards"));
1336 masked.Run(); 1370 masked.Run();
1337 bool changed = db_->GetLastChangeCount() > 0; 1371 bool changed = db_->GetLastChangeCount() > 0;
1338 1372
1339 sql::Statement unmasked(db_->GetUniqueStatement( 1373 sql::Statement unmasked(db_->GetUniqueStatement(
1340 "DELETE FROM unmasked_credit_cards")); 1374 "DELETE FROM unmasked_credit_cards"));
1341 unmasked.Run(); 1375 unmasked.Run();
1342 changed |= db_->GetLastChangeCount() > 0; 1376 changed |= db_->GetLastChangeCount() > 0;
1343 1377
1344 sql::Statement addresses(db_->GetUniqueStatement( 1378 sql::Statement addresses(db_->GetUniqueStatement(
1345 "DELETE FROM server_addresses")); 1379 "DELETE FROM server_addresses"));
1346 addresses.Run(); 1380 addresses.Run();
1347 changed |= db_->GetLastChangeCount() > 0; 1381 changed |= db_->GetLastChangeCount() > 0;
1348 1382
1383 sql::Statement card_metadata(db_->GetUniqueStatement(
1384 "DELETE FROM server_card_metadata"));
1385 card_metadata.Run();
1386 changed |= db_->GetLastChangeCount() > 0;
1387
1388 sql::Statement address_metadata(db_->GetUniqueStatement(
1389 "DELETE FROM server_address_metadata"));
1390 address_metadata.Run();
1391 changed |= db_->GetLastChangeCount() > 0;
1392
1349 transaction.Commit(); 1393 transaction.Commit();
1350 return changed; 1394 return changed;
1351 } 1395 }
1352 1396
1353 bool AutofillTable::UpdateCreditCard(const CreditCard& credit_card) { 1397 bool AutofillTable::UpdateCreditCard(const CreditCard& credit_card) {
1354 DCHECK(base::IsValidGUID(credit_card.guid())); 1398 DCHECK(base::IsValidGUID(credit_card.guid()));
1355 1399
1356 CreditCard* tmp_credit_card = NULL; 1400 CreditCard* tmp_credit_card = NULL;
1357 if (!GetCreditCard(credit_card.guid(), &tmp_credit_card)) 1401 if (!GetCreditCard(credit_card.guid(), &tmp_credit_card))
1358 return false; 1402 return false;
(...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after
1718 "use_count INTEGER NOT NULL DEFAULT 0, " 1762 "use_count INTEGER NOT NULL DEFAULT 0, "
1719 "use_date INTEGER NOT NULL DEFAULT 0, " 1763 "use_date INTEGER NOT NULL DEFAULT 0, "
1720 "unmask_date INTEGER NOT NULL DEFAULT 0)")) { 1764 "unmask_date INTEGER NOT NULL DEFAULT 0)")) {
1721 NOTREACHED(); 1765 NOTREACHED();
1722 return false; 1766 return false;
1723 } 1767 }
1724 } 1768 }
1725 return true; 1769 return true;
1726 } 1770 }
1727 1771
1772 bool AutofillTable::InitServerCardMetadataTable() {
1773 if (!db_->DoesTableExist("server_card_metadata")) {
1774 if (!db_->Execute("CREATE TABLE server_card_metadata ("
1775 "id VARCHAR NOT NULL,"
1776 "use_count INTEGER NOT NULL DEFAULT 0, "
1777 "use_date INTEGER NOT NULL DEFAULT 0)")) {
1778 NOTREACHED();
1779 return false;
1780 }
1781 }
1782 return true;
1783 }
1784
1728 bool AutofillTable::InitServerAddressesTable() { 1785 bool AutofillTable::InitServerAddressesTable() {
1729 if (!db_->DoesTableExist("server_addresses")) { 1786 if (!db_->DoesTableExist("server_addresses")) {
1730 // The space after language_code is necessary to match what sqlite does 1787 // The space after language_code is necessary to match what sqlite does
1731 // when it appends the column in migration. 1788 // when it appends the column in migration.
1732 if (!db_->Execute("CREATE TABLE server_addresses (" 1789 if (!db_->Execute("CREATE TABLE server_addresses ("
1733 "id VARCHAR," 1790 "id VARCHAR,"
1734 "company_name VARCHAR," 1791 "company_name VARCHAR,"
1735 "street_address VARCHAR," 1792 "street_address VARCHAR,"
1736 "address_1 VARCHAR," 1793 "address_1 VARCHAR,"
1737 "address_2 VARCHAR," 1794 "address_2 VARCHAR,"
1738 "address_3 VARCHAR," 1795 "address_3 VARCHAR,"
1739 "address_4 VARCHAR," 1796 "address_4 VARCHAR,"
1740 "postal_code VARCHAR," 1797 "postal_code VARCHAR,"
1741 "sorting_code VARCHAR," 1798 "sorting_code VARCHAR,"
1742 "country_code VARCHAR," 1799 "country_code VARCHAR,"
1743 "language_code VARCHAR, " // Space required. 1800 "language_code VARCHAR, " // Space required.
1744 "recipient_name VARCHAR, " // Ditto. 1801 "recipient_name VARCHAR, " // Ditto.
1745 "phone_number VARCHAR)")) { 1802 "phone_number VARCHAR)")) {
1746 NOTREACHED(); 1803 NOTREACHED();
1747 return false; 1804 return false;
1748 } 1805 }
1749 } 1806 }
1750 return true; 1807 return true;
1751 } 1808 }
1752 1809
1810 bool AutofillTable::InitServerAddressMetadataTable() {
1811 if (!db_->DoesTableExist("server_address_metadata")) {
1812 if (!db_->Execute("CREATE TABLE server_address_metadata ("
1813 "id VARCHAR NOT NULL,"
1814 "use_count INTEGER NOT NULL DEFAULT 0, "
1815 "use_date INTEGER NOT NULL DEFAULT 0)")) {
1816 NOTREACHED();
1817 return false;
1818 }
1819 }
1820 return true;
1821 }
1822
1753 bool AutofillTable::MigrateToVersion54AddI18nFieldsAndRemoveDeprecatedFields() { 1823 bool AutofillTable::MigrateToVersion54AddI18nFieldsAndRemoveDeprecatedFields() {
1754 sql::Transaction transaction(db_); 1824 sql::Transaction transaction(db_);
1755 if (!transaction.Begin()) 1825 if (!transaction.Begin())
1756 return false; 1826 return false;
1757 1827
1758 // Test the existence of the |address_line_1| column as an indication that a 1828 // 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| 1829 // 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 1830 // schema is in place because the table was newly created when migrating from
1761 // a pre-version-23 database. 1831 // a pre-version-23 database.
1762 if (db_->DoesColumnExist("autofill_profiles", "address_line_1")) { 1832 if (db_->DoesColumnExist("autofill_profiles", "address_line_1")) {
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after
2047 } 2117 }
2048 if (!db_->DoesColumnExist("server_addresses", "phone_number") && 2118 if (!db_->DoesColumnExist("server_addresses", "phone_number") &&
2049 !db_->Execute("ALTER TABLE server_addresses ADD COLUMN " 2119 !db_->Execute("ALTER TABLE server_addresses ADD COLUMN "
2050 "phone_number VARCHAR")) { 2120 "phone_number VARCHAR")) {
2051 return false; 2121 return false;
2052 } 2122 }
2053 2123
2054 return transaction.Commit(); 2124 return transaction.Commit();
2055 } 2125 }
2056 2126
2127 bool AutofillTable::MigrateToVersion65AddServerMetadataTables() {
2128 sql::Transaction transaction(db_);
2129 if (!transaction.Begin())
2130 return false;
2131
2132 if (!db_->DoesTableExist("server_card_metadata") &&
2133 !db_->Execute("CREATE TABLE server_card_metadata ("
2134 "id VARCHAR NOT NULL,"
2135 "use_count INTEGER NOT NULL DEFAULT 0, "
2136 "use_date INTEGER NOT NULL DEFAULT 0)")) {
2137 return false;
2138 }
2139
2140 // This clobbers existing usage metadata, which is not synced and only
2141 // applies to unmasked cards. Trying to migrate the usage metadata would be
2142 // tricky as multiple devices for the same user get DB upgrades.
2143 if (!db_->Execute("UPDATE unmasked_credit_cards "
2144 "SET use_count=0, use_date=0")) {
2145 return false;
2146 }
2147
2148 if (!db_->DoesTableExist("server_address_metadata") &&
2149 !db_->Execute("CREATE TABLE server_address_metadata ("
2150 "id VARCHAR NOT NULL,"
2151 "use_count INTEGER NOT NULL DEFAULT 0, "
2152 "use_date INTEGER NOT NULL DEFAULT 0)")) {
2153 return false;
2154 }
2155
2156 // Get existing server addresses and generate IDs for them.
2157 sql::Statement s(db_->GetUniqueStatement(
2158 "SELECT "
2159 "id,"
2160 "recipient_name,"
2161 "company_name,"
2162 "street_address,"
2163 "address_1," // ADDRESS_HOME_STATE
2164 "address_2," // ADDRESS_HOME_CITY
2165 "address_3," // ADDRESS_HOME_DEPENDENT_LOCALITY
2166 "address_4," // Not supported in AutofillProfile yet.
2167 "postal_code," // ADDRESS_HOME_ZIP
2168 "sorting_code," // ADDRESS_HOME_SORTING_CODE
2169 "country_code," // ADDRESS_HOME_COUNTRY
2170 "phone_number," // PHONE_HOME_WHOLE_NUMBER
2171 "language_code "
2172 "FROM server_addresses addresses"));
2173 std::vector<AutofillProfile> profiles;
2174 while (s.Step()) {
2175 int index = 0;
2176 AutofillProfile profile(
2177 AutofillProfile::SERVER_PROFILE, s.ColumnString(index++));
2178
2179 base::string16 recipient_name = s.ColumnString16(index++);
2180 profile.SetRawInfo(COMPANY_NAME, s.ColumnString16(index++));
2181 profile.SetRawInfo(ADDRESS_HOME_STREET_ADDRESS, s.ColumnString16(index++));
2182 profile.SetRawInfo(ADDRESS_HOME_STATE, s.ColumnString16(index++));
2183 profile.SetRawInfo(ADDRESS_HOME_CITY, s.ColumnString16(index++));
2184 profile.SetRawInfo(ADDRESS_HOME_DEPENDENT_LOCALITY,
2185 s.ColumnString16(index++));
2186 index++; // Skip address_4 which we haven't added to AutofillProfile yet.
2187 profile.SetRawInfo(ADDRESS_HOME_ZIP, s.ColumnString16(index++));
2188 profile.SetRawInfo(ADDRESS_HOME_SORTING_CODE, s.ColumnString16(index++));
2189 profile.SetRawInfo(ADDRESS_HOME_COUNTRY, s.ColumnString16(index++));
2190 base::string16 phone_number = s.ColumnString16(index++);
2191 profile.set_language_code(s.ColumnString(index++));
2192 profile.SetInfo(AutofillType(NAME_FULL), recipient_name,
2193 profile.language_code());
2194 profile.SetInfo(AutofillType(PHONE_HOME_WHOLE_NUMBER), phone_number,
2195 profile.language_code());
2196 profile.GenerateServerProfileIdentifier();
2197 profiles.push_back(profile);
2198 }
2199
2200 // Reinsert with the generated IDs.
2201 sql::Statement delete_old(db_->GetUniqueStatement(
2202 "DELETE FROM server_addresses"));
2203 delete_old.Run();
2204
2205 sql::Statement insert(db_->GetUniqueStatement(
2206 "INSERT INTO server_addresses("
2207 "id,"
2208 "recipient_name,"
2209 "company_name,"
2210 "street_address,"
2211 "address_1," // ADDRESS_HOME_STATE
2212 "address_2," // ADDRESS_HOME_CITY
2213 "address_3," // ADDRESS_HOME_DEPENDENT_LOCALITY
2214 "address_4," // Not supported in AutofillProfile yet.
2215 "postal_code," // ADDRESS_HOME_ZIP
2216 "sorting_code," // ADDRESS_HOME_SORTING_CODE
2217 "country_code," // ADDRESS_HOME_COUNTRY
2218 "phone_number," // PHONE_HOME_WHOLE_NUMBER
2219 "language_code) "
2220 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)"));
2221 for (const AutofillProfile& profile : profiles) {
2222 int index = 0;
2223 insert.BindString(index++, profile.server_id());
2224 insert.BindString16(index++, profile.GetRawInfo(NAME_FULL));
2225 insert.BindString16(index++, profile.GetRawInfo(COMPANY_NAME));
2226 insert.BindString16(index++,
2227 profile.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS));
2228 insert.BindString16(index++, profile.GetRawInfo(ADDRESS_HOME_STATE));
2229 insert.BindString16(index++, profile.GetRawInfo(ADDRESS_HOME_CITY));
2230 insert.BindString16(index++,
2231 profile.GetRawInfo(ADDRESS_HOME_DEPENDENT_LOCALITY));
2232 index++; // SKip address_4 which we haven't added to AutofillProfile yet.
2233 insert.BindString16(index++, profile.GetRawInfo(ADDRESS_HOME_ZIP));
2234 insert.BindString16(index++, profile.GetRawInfo(ADDRESS_HOME_SORTING_CODE));
2235 insert.BindString16(index++, profile.GetRawInfo(ADDRESS_HOME_COUNTRY));
2236 insert.BindString16(index++, profile.GetRawInfo(PHONE_HOME_WHOLE_NUMBER));
2237 insert.BindString(index++, profile.language_code());
2238 insert.Run();
2239 insert.Reset(true);
2240 }
2241
2242 return transaction.Commit();
2243 }
2244
2057 } // namespace autofill 2245 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698