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

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

Issue 2844463004: Rename card 'type' into 'issuer network.' (Closed)
Patch Set: More iOS compile fix Created 3 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 <stdint.h> 7 #include <stdint.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <cmath> 10 #include <cmath>
(...skipping 27 matching lines...) Expand all
38 #include "components/autofill/core/common/form_field_data.h" 38 #include "components/autofill/core/common/form_field_data.h"
39 #include "components/sync/base/model_type.h" 39 #include "components/sync/base/model_type.h"
40 #include "components/sync/protocol/entity_metadata.pb.h" 40 #include "components/sync/protocol/entity_metadata.pb.h"
41 #include "components/sync/protocol/model_type_state.pb.h" 41 #include "components/sync/protocol/model_type_state.pb.h"
42 #include "components/webdata/common/web_database.h" 42 #include "components/webdata/common/web_database.h"
43 #include "sql/statement.h" 43 #include "sql/statement.h"
44 #include "sql/transaction.h" 44 #include "sql/transaction.h"
45 #include "ui/base/l10n/l10n_util.h" 45 #include "ui/base/l10n/l10n_util.h"
46 #include "url/gurl.h" 46 #include "url/gurl.h"
47 47
48 using base::ASCIIToUTF16;
49 using base::Time;
50 using base::TimeDelta;
51
52 namespace autofill { 48 namespace autofill {
53 namespace { 49 namespace {
54 50
51 using ::base::ASCIIToUTF16;
52 using ::base::Time;
53 using ::base::TimeDelta;
Peter Kasting 2017/04/26 01:27:28 Nit: Any particular reason to move these? (I'd lo
please use gerrit instead 2017/04/26 16:34:15 There's a wealth of explanations in go/totw/119. T
54
55 // The period after which autocomplete entries should expire in days. 55 // The period after which autocomplete entries should expire in days.
56 const int64_t kExpirationPeriodInDays = 60; 56 const int64_t kExpirationPeriodInDays = 60;
57 57
58 // Helper struct for AutofillTable::RemoveFormElementsAddedBetween(). 58 // Helper struct for AutofillTable::RemoveFormElementsAddedBetween().
59 // Contains all the necessary fields to update a row in the 'autofill' table. 59 // Contains all the necessary fields to update a row in the 'autofill' table.
60 struct AutofillUpdate { 60 struct AutofillUpdate {
61 base::string16 name; 61 base::string16 name;
62 base::string16 value; 62 base::string16 value;
63 time_t date_created; 63 time_t date_created;
64 time_t date_last_used; 64 time_t date_last_used;
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 profile->SetRawInfo(NAME_LAST, s.ColumnString16(3)); 221 profile->SetRawInfo(NAME_LAST, s.ColumnString16(3));
222 profile->SetRawInfo(NAME_FULL, s.ColumnString16(4)); 222 profile->SetRawInfo(NAME_FULL, s.ColumnString16(4));
223 } 223 }
224 return s.Succeeded(); 224 return s.Succeeded();
225 } 225 }
226 226
227 bool AddAutofillProfileEmailsToProfile(sql::Connection* db, 227 bool AddAutofillProfileEmailsToProfile(sql::Connection* db,
228 AutofillProfile* profile) { 228 AutofillProfile* profile) {
229 // TODO(estade): update schema so that multiple emails are not associated per 229 // TODO(estade): update schema so that multiple emails are not associated per
230 // unique profile guid. Please refer https://crbug.com/497934. 230 // unique profile guid. Please refer https://crbug.com/497934.
231 sql::Statement s(db->GetUniqueStatement( 231 sql::Statement s(
232 "SELECT guid, email " 232 db->GetUniqueStatement("SELECT guid, email "
233 "FROM autofill_profile_emails " 233 "FROM autofill_profile_emails "
234 "WHERE guid=?" 234 "WHERE guid=?"
235 "LIMIT 1")); 235 "LIMIT 1"));
236 s.BindString(0, profile->guid()); 236 s.BindString(0, profile->guid());
237 237
238 if (!s.is_valid()) 238 if (!s.is_valid())
239 return false; 239 return false;
240 240
241 if (s.Step()) { 241 if (s.Step()) {
242 DCHECK_EQ(profile->guid(), s.ColumnString(0)); 242 DCHECK_EQ(profile->guid(), s.ColumnString(0));
243 profile->SetRawInfo(EMAIL_ADDRESS, s.ColumnString16(1)); 243 profile->SetRawInfo(EMAIL_ADDRESS, s.ColumnString16(1));
244 } 244 }
245 return s.Succeeded(); 245 return s.Succeeded();
246 } 246 }
247 247
248 bool AddAutofillProfilePhonesToProfile(sql::Connection* db, 248 bool AddAutofillProfilePhonesToProfile(sql::Connection* db,
249 AutofillProfile* profile) { 249 AutofillProfile* profile) {
250 // TODO(estade): update schema so that multiple phone numbers are not 250 // TODO(estade): update schema so that multiple phone numbers are not
251 // associated per unique profile guid. Please refer https://crbug.com/497934. 251 // associated per unique profile guid. Please refer https://crbug.com/497934.
252 sql::Statement s(db->GetUniqueStatement( 252 sql::Statement s(
253 "SELECT guid, number " 253 db->GetUniqueStatement("SELECT guid, number "
254 "FROM autofill_profile_phones " 254 "FROM autofill_profile_phones "
255 "WHERE guid=?" 255 "WHERE guid=?"
256 "LIMIT 1")); 256 "LIMIT 1"));
257 s.BindString(0, profile->guid()); 257 s.BindString(0, profile->guid());
258 258
259 if (!s.is_valid()) 259 if (!s.is_valid())
260 return false; 260 return false;
261 261
262 if (s.Step()) { 262 if (s.Step()) {
263 DCHECK_EQ(profile->guid(), s.ColumnString(0)); 263 DCHECK_EQ(profile->guid(), s.ColumnString(0));
264 profile->SetRawInfo(PHONE_HOME_WHOLE_NUMBER, s.ColumnString16(1)); 264 profile->SetRawInfo(PHONE_HOME_WHOLE_NUMBER, s.ColumnString16(1));
265 } 265 }
266 return s.Succeeded(); 266 return s.Succeeded();
(...skipping 11 matching lines...) Expand all
278 s.BindString16(2, profile.GetRawInfo(NAME_MIDDLE)); 278 s.BindString16(2, profile.GetRawInfo(NAME_MIDDLE));
279 s.BindString16(3, profile.GetRawInfo(NAME_LAST)); 279 s.BindString16(3, profile.GetRawInfo(NAME_LAST));
280 s.BindString16(4, profile.GetRawInfo(NAME_FULL)); 280 s.BindString16(4, profile.GetRawInfo(NAME_FULL));
281 281
282 return s.Run(); 282 return s.Run();
283 } 283 }
284 284
285 bool AddAutofillProfileEmails(const AutofillProfile& profile, 285 bool AddAutofillProfileEmails(const AutofillProfile& profile,
286 sql::Connection* db) { 286 sql::Connection* db) {
287 // Add the new email. 287 // Add the new email.
288 sql::Statement s(db->GetUniqueStatement( 288 sql::Statement s(
289 "INSERT INTO autofill_profile_emails" 289 db->GetUniqueStatement("INSERT INTO autofill_profile_emails"
290 " (guid, email) " 290 " (guid, email) "
291 "VALUES (?,?)")); 291 "VALUES (?,?)"));
292 s.BindString(0, profile.guid()); 292 s.BindString(0, profile.guid());
293 s.BindString16(1, profile.GetRawInfo(EMAIL_ADDRESS)); 293 s.BindString16(1, profile.GetRawInfo(EMAIL_ADDRESS));
294 294
295 return s.Run(); 295 return s.Run();
296 } 296 }
297 297
298 bool AddAutofillProfilePhones(const AutofillProfile& profile, 298 bool AddAutofillProfilePhones(const AutofillProfile& profile,
299 sql::Connection* db) { 299 sql::Connection* db) {
300 // Add the new number. 300 // Add the new number.
301 sql::Statement s(db->GetUniqueStatement( 301 sql::Statement s(
302 "INSERT INTO autofill_profile_phones" 302 db->GetUniqueStatement("INSERT INTO autofill_profile_phones"
303 " (guid, number) " 303 " (guid, number) "
304 "VALUES (?,?)")); 304 "VALUES (?,?)"));
305 s.BindString(0, profile.guid()); 305 s.BindString(0, profile.guid());
306 s.BindString16(1, profile.GetRawInfo(PHONE_HOME_WHOLE_NUMBER)); 306 s.BindString16(1, profile.GetRawInfo(PHONE_HOME_WHOLE_NUMBER));
307 307
308 return s.Run(); 308 return s.Run();
309 } 309 }
310 310
311 bool AddAutofillProfilePieces(const AutofillProfile& profile, 311 bool AddAutofillProfilePieces(const AutofillProfile& profile,
312 sql::Connection* db) { 312 sql::Connection* db) {
313 if (!AddAutofillProfileNames(profile, db)) 313 if (!AddAutofillProfileNames(profile, db))
314 return false; 314 return false;
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
405 405
406 // static 406 // static
407 const size_t AutofillTable::kMaxDataLength = 1024; 407 const size_t AutofillTable::kMaxDataLength = 1024;
408 408
409 AutofillTable::AutofillTable() 409 AutofillTable::AutofillTable()
410 : autofill_table_encryptor_( 410 : autofill_table_encryptor_(
411 AutofillTableEncryptorFactory::GetInstance()->Create()) { 411 AutofillTableEncryptorFactory::GetInstance()->Create()) {
412 DCHECK(autofill_table_encryptor_); 412 DCHECK(autofill_table_encryptor_);
413 } 413 }
414 414
415 AutofillTable::~AutofillTable() { 415 AutofillTable::~AutofillTable() {}
416 }
417 416
418 AutofillTable* AutofillTable::FromWebDatabase(WebDatabase* db) { 417 AutofillTable* AutofillTable::FromWebDatabase(WebDatabase* db) {
419 return static_cast<AutofillTable*>(db->GetTable(GetKey())); 418 return static_cast<AutofillTable*>(db->GetTable(GetKey()));
420 } 419 }
421 420
422 WebDatabaseTable::TypeKey AutofillTable::GetTypeKey() const { 421 WebDatabaseTable::TypeKey AutofillTable::GetTypeKey() const {
423 return GetKey(); 422 return GetKey();
424 } 423 }
425 424
426 bool AutofillTable::CreateTablesIfNecessary() { 425 bool AutofillTable::CreateTablesIfNecessary() {
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
476 return MigrateToVersion66AddCardBillingAddress(); 475 return MigrateToVersion66AddCardBillingAddress();
477 case 67: 476 case 67:
478 *update_compatible_version = false; 477 *update_compatible_version = false;
479 return MigrateToVersion67AddMaskedCardBillingAddress(); 478 return MigrateToVersion67AddMaskedCardBillingAddress();
480 case 70: 479 case 70:
481 *update_compatible_version = false; 480 *update_compatible_version = false;
482 return MigrateToVersion70AddSyncMetadata(); 481 return MigrateToVersion70AddSyncMetadata();
483 case 71: 482 case 71:
484 *update_compatible_version = true; 483 *update_compatible_version = true;
485 return MigrateToVersion71AddHasConvertedAndBillingAddressIdMetadata(); 484 return MigrateToVersion71AddHasConvertedAndBillingAddressIdMetadata();
485 case 72:
486 *update_compatible_version = false;
Peter Kasting 2017/04/26 01:27:28 Wait, shouldn't this be true?
please use gerrit instead 2017/04/26 16:34:15 Is there any documentation to read up on why this
Peter Kasting 2017/04/26 17:47:56 You need to set this to true iff you need to bump
487 return MigrateToVersion72RenameCardTypeToIssuerNetwork();
486 } 488 }
487 return true; 489 return true;
488 } 490 }
489 491
490 bool AutofillTable::AddFormFieldValues( 492 bool AutofillTable::AddFormFieldValues(
491 const std::vector<FormFieldData>& elements, 493 const std::vector<FormFieldData>& elements,
492 std::vector<AutofillChange>* changes) { 494 std::vector<AutofillChange>* changes) {
493 return AddFormFieldValuesTime(elements, changes, AutofillClock::Now()); 495 return AddFormFieldValuesTime(elements, changes, AutofillClock::Now());
494 } 496 }
495 497
496 bool AutofillTable::AddFormFieldValue(const FormFieldData& element, 498 bool AutofillTable::AddFormFieldValue(const FormFieldData& element,
497 std::vector<AutofillChange>* changes) { 499 std::vector<AutofillChange>* changes) {
498 return AddFormFieldValueTime(element, changes, AutofillClock::Now()); 500 return AddFormFieldValueTime(element, changes, AutofillClock::Now());
499 } 501 }
500 502
501 bool AutofillTable::GetFormValuesForElementName( 503 bool AutofillTable::GetFormValuesForElementName(
502 const base::string16& name, 504 const base::string16& name,
503 const base::string16& prefix, 505 const base::string16& prefix,
504 std::vector<base::string16>* values, 506 std::vector<base::string16>* values,
505 int limit) { 507 int limit) {
506 DCHECK(values); 508 DCHECK(values);
507 bool succeeded = false; 509 bool succeeded = false;
508 510
509 if (prefix.empty()) { 511 if (prefix.empty()) {
510 sql::Statement s; 512 sql::Statement s;
511 s.Assign(db_->GetUniqueStatement( 513 s.Assign(
512 "SELECT value FROM autofill " 514 db_->GetUniqueStatement("SELECT value FROM autofill "
513 "WHERE name = ? " 515 "WHERE name = ? "
514 "ORDER BY count DESC " 516 "ORDER BY count DESC "
515 "LIMIT ?")); 517 "LIMIT ?"));
516 s.BindString16(0, name); 518 s.BindString16(0, name);
517 s.BindInt(1, limit); 519 s.BindInt(1, limit);
518 520
519 values->clear(); 521 values->clear();
520 while (s.Step()) 522 while (s.Step())
521 values->push_back(s.ColumnString16(0)); 523 values->push_back(s.ColumnString16(0));
522 524
523 succeeded = s.Succeeded(); 525 succeeded = s.Succeeded();
524 } else { 526 } else {
525 base::string16 prefix_lower = base::i18n::ToLower(prefix); 527 base::string16 prefix_lower = base::i18n::ToLower(prefix);
526 base::string16 next_prefix = prefix_lower; 528 base::string16 next_prefix = prefix_lower;
527 next_prefix.back()++; 529 next_prefix.back()++;
528 530
529 sql::Statement s1; 531 sql::Statement s1;
530 s1.Assign(db_->GetUniqueStatement( 532 s1.Assign(
531 "SELECT value FROM autofill " 533 db_->GetUniqueStatement("SELECT value FROM autofill "
532 "WHERE name = ? AND " 534 "WHERE name = ? AND "
533 "value_lower >= ? AND " 535 "value_lower >= ? AND "
534 "value_lower < ? " 536 "value_lower < ? "
535 "ORDER BY count DESC " 537 "ORDER BY count DESC "
536 "LIMIT ?")); 538 "LIMIT ?"));
537 s1.BindString16(0, name); 539 s1.BindString16(0, name);
538 s1.BindString16(1, prefix_lower); 540 s1.BindString16(1, prefix_lower);
539 s1.BindString16(2, next_prefix); 541 s1.BindString16(2, next_prefix);
540 s1.BindInt(3, limit); 542 s1.BindInt(3, limit);
541 543
542 values->clear(); 544 values->clear();
543 while (s1.Step()) 545 while (s1.Step())
544 values->push_back(s1.ColumnString16(0)); 546 values->push_back(s1.ColumnString16(0));
545 547
546 succeeded = s1.Succeeded(); 548 succeeded = s1.Succeeded();
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
622 // count_new = 1 + (date_last_used_new - date_created_new) / avg_delta 624 // count_new = 1 + (date_last_used_new - date_created_new) / avg_delta
623 // = 1 + ((count - 1) * 625 // = 1 + ((count - 1) *
624 // (date_last_used_new - date_created_new) / 626 // (date_last_used_new - date_created_new) /
625 // (date_last_used_orig - date_created_orig)) 627 // (date_last_used_orig - date_created_orig))
626 // Interpolating might not give a result that completely accurately 628 // Interpolating might not give a result that completely accurately
627 // reflects the user's history, but it's the best that can be done given 629 // reflects the user's history, but it's the best that can be done given
628 // the information in the database. 630 // the information in the database.
629 AutofillUpdate updated_entry; 631 AutofillUpdate updated_entry;
630 updated_entry.name = name; 632 updated_entry.name = name;
631 updated_entry.value = value; 633 updated_entry.value = value;
632 updated_entry.date_created = 634 updated_entry.date_created = date_created_time_t < delete_begin_time_t
633 date_created_time_t < delete_begin_time_t ? 635 ? date_created_time_t
634 date_created_time_t : 636 : delete_end_time_t;
635 delete_end_time_t; 637 updated_entry.date_last_used = date_last_used_time_t >= delete_end_time_t
636 updated_entry.date_last_used = 638 ? date_last_used_time_t
637 date_last_used_time_t >= delete_end_time_t ? 639 : delete_begin_time_t - 1;
638 date_last_used_time_t :
639 delete_begin_time_t - 1;
640 updated_entry.count = 640 updated_entry.count =
641 1 + 641 1 +
642 Round(1.0 * (count - 1) * 642 Round(1.0 * (count - 1) *
643 (updated_entry.date_last_used - updated_entry.date_created) / 643 (updated_entry.date_last_used - updated_entry.date_created) /
644 (date_last_used_time_t - date_created_time_t)); 644 (date_last_used_time_t - date_created_time_t));
645 updates.push_back(updated_entry); 645 updates.push_back(updated_entry);
646 } 646 }
647 647
648 tentative_changes.push_back( 648 tentative_changes.push_back(
649 AutofillChange(change_type, AutofillKey(name, value))); 649 AutofillChange(change_type, AutofillKey(name, value)));
650 } 650 }
651 if (!s.Succeeded()) 651 if (!s.Succeeded())
652 return false; 652 return false;
653 653
654 // As a single transaction, remove or update the elements appropriately. 654 // As a single transaction, remove or update the elements appropriately.
655 sql::Statement s_delete(db_->GetUniqueStatement( 655 sql::Statement s_delete(db_->GetUniqueStatement(
656 "DELETE FROM autofill WHERE date_created >= ? AND date_last_used < ?")); 656 "DELETE FROM autofill WHERE date_created >= ? AND date_last_used < ?"));
657 s_delete.BindInt64(0, delete_begin_time_t); 657 s_delete.BindInt64(0, delete_begin_time_t);
658 s_delete.BindInt64(1, delete_end_time_t); 658 s_delete.BindInt64(1, delete_end_time_t);
659 sql::Transaction transaction(db_); 659 sql::Transaction transaction(db_);
660 if (!transaction.Begin()) 660 if (!transaction.Begin())
661 return false; 661 return false;
662 if (!s_delete.Run()) 662 if (!s_delete.Run())
663 return false; 663 return false;
664 for (size_t i = 0; i < updates.size(); ++i) { 664 for (size_t i = 0; i < updates.size(); ++i) {
665 sql::Statement s_update(db_->GetUniqueStatement( 665 sql::Statement s_update(db_->GetUniqueStatement(
666 "UPDATE autofill SET date_created = ?, date_last_used = ?, count = ?" 666 "UPDATE autofill SET date_created = ?, date_last_used = ?, count = ?"
667 "WHERE name = ? AND value = ?")); 667 "WHERE name = ? AND value = ?"));
668 s_update.BindInt64(0, updates[i].date_created); 668 s_update.BindInt64(0, updates[i].date_created);
669 s_update.BindInt64(1, updates[i].date_last_used); 669 s_update.BindInt64(1, updates[i].date_last_used);
670 s_update.BindInt(2, updates[i].count); 670 s_update.BindInt(2, updates[i].count);
671 s_update.BindString16(3, updates[i].name); 671 s_update.BindString16(3, updates[i].name);
672 s_update.BindString16(4, updates[i].value); 672 s_update.BindString16(4, updates[i].value);
673 if (!s_update.Run()) 673 if (!s_update.Run())
674 return false; 674 return false;
675 } 675 }
676 if (!transaction.Commit()) 676 if (!transaction.Commit())
677 return false; 677 return false;
(...skipping 16 matching lines...) Expand all
694 while (select_for_delete.Step()) { 694 while (select_for_delete.Step()) {
695 base::string16 name = select_for_delete.ColumnString16(0); 695 base::string16 name = select_for_delete.ColumnString16(0);
696 base::string16 value = select_for_delete.ColumnString16(1); 696 base::string16 value = select_for_delete.ColumnString16(1);
697 tentative_changes.push_back( 697 tentative_changes.push_back(
698 AutofillChange(AutofillChange::REMOVE, AutofillKey(name, value))); 698 AutofillChange(AutofillChange::REMOVE, AutofillKey(name, value)));
699 } 699 }
700 700
701 if (!select_for_delete.Succeeded()) 701 if (!select_for_delete.Succeeded())
702 return false; 702 return false;
703 703
704 sql::Statement delete_data_statement(db_->GetUniqueStatement( 704 sql::Statement delete_data_statement(
705 "DELETE FROM autofill WHERE date_last_used < ?")); 705 db_->GetUniqueStatement("DELETE FROM autofill WHERE date_last_used < ?"));
706 delete_data_statement.BindInt64(0, expiration_time.ToTimeT()); 706 delete_data_statement.BindInt64(0, expiration_time.ToTimeT());
707 if (!delete_data_statement.Run()) 707 if (!delete_data_statement.Run())
708 return false; 708 return false;
709 709
710 *changes = tentative_changes; 710 *changes = tentative_changes;
711 return true; 711 return true;
712 } 712 }
713 713
714 bool AutofillTable::AddFormFieldValuesTime( 714 bool AutofillTable::AddFormFieldValuesTime(
715 const std::vector<FormFieldData>& elements, 715 const std::vector<FormFieldData>& elements,
716 std::vector<AutofillChange>* changes, 716 std::vector<AutofillChange>* changes,
717 Time time) { 717 Time time) {
718 // Only add one new entry for each unique element name. Use |seen_names| to 718 // Only add one new entry for each unique element name. Use |seen_names| to
719 // track this. Add up to |kMaximumUniqueNames| unique entries per form. 719 // track this. Add up to |kMaximumUniqueNames| unique entries per form.
720 const size_t kMaximumUniqueNames = 256; 720 const size_t kMaximumUniqueNames = 256;
721 std::set<base::string16> seen_names; 721 std::set<base::string16> seen_names;
722 bool result = true; 722 bool result = true;
723 for (const FormFieldData& element : elements) { 723 for (const FormFieldData& element : elements) {
724 if (seen_names.size() >= kMaximumUniqueNames) 724 if (seen_names.size() >= kMaximumUniqueNames)
725 break; 725 break;
726 if (base::ContainsKey(seen_names, element.name)) 726 if (base::ContainsKey(seen_names, element.name))
727 continue; 727 continue;
728 result = result && AddFormFieldValueTime(element, changes, time); 728 result = result && AddFormFieldValueTime(element, changes, time);
729 seen_names.insert(element.name); 729 seen_names.insert(element.name);
730 } 730 }
731 return result; 731 return result;
732 } 732 }
733 733
734 int AutofillTable::GetCountOfValuesContainedBetween( 734 int AutofillTable::GetCountOfValuesContainedBetween(const Time& begin,
735 const Time& begin, 735 const Time& end) {
736 const Time& end) {
737 const time_t begin_time_t = begin.ToTimeT(); 736 const time_t begin_time_t = begin.ToTimeT();
738 const time_t end_time_t = GetEndTime(end); 737 const time_t end_time_t = GetEndTime(end);
739 738
740 sql::Statement s(db_->GetUniqueStatement( 739 sql::Statement s(db_->GetUniqueStatement(
741 "SELECT COUNT(DISTINCT(value1)) FROM ( " 740 "SELECT COUNT(DISTINCT(value1)) FROM ( "
742 " SELECT value AS value1 FROM autofill " 741 " SELECT value AS value1 FROM autofill "
743 " WHERE NOT EXISTS ( " 742 " WHERE NOT EXISTS ( "
744 " SELECT value AS value2, date_created, date_last_used FROM autofill " 743 " SELECT value AS value2, date_created, date_last_used FROM autofill "
745 " WHERE value1 = value2 AND " 744 " WHERE value1 = value2 AND "
746 " (date_created < ? OR date_last_used >= ?)))")); 745 " (date_created < ? OR date_last_used >= ?)))"));
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
868 return false; 867 return false;
869 } 868 }
870 869
871 AutofillChange::Type change_type = 870 AutofillChange::Type change_type =
872 already_exists ? AutofillChange::UPDATE : AutofillChange::ADD; 871 already_exists ? AutofillChange::UPDATE : AutofillChange::ADD;
873 changes->push_back( 872 changes->push_back(
874 AutofillChange(change_type, AutofillKey(element.name, element.value))); 873 AutofillChange(change_type, AutofillKey(element.name, element.value)));
875 return true; 874 return true;
876 } 875 }
877 876
878
879 bool AutofillTable::RemoveFormElement(const base::string16& name, 877 bool AutofillTable::RemoveFormElement(const base::string16& name,
880 const base::string16& value) { 878 const base::string16& value) {
881 sql::Statement s(db_->GetUniqueStatement( 879 sql::Statement s(db_->GetUniqueStatement(
882 "DELETE FROM autofill WHERE name = ? AND value= ?")); 880 "DELETE FROM autofill WHERE name = ? AND value= ?"));
883 s.BindString16(0, name); 881 s.BindString16(0, name);
884 s.BindString16(1, value); 882 s.BindString16(1, value);
885 return s.Run(); 883 return s.Run();
886 } 884 }
887 885
888 bool AutofillTable::AddAutofillProfile(const AutofillProfile& profile) { 886 bool AutofillTable::AddAutofillProfile(const AutofillProfile& profile) {
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
930 AddAutofillProfilePhonesToProfile(db_, p.get()); 928 AddAutofillProfilePhonesToProfile(db_, p.get());
931 929
932 return p; 930 return p;
933 } 931 }
934 932
935 bool AutofillTable::GetAutofillProfiles( 933 bool AutofillTable::GetAutofillProfiles(
936 std::vector<std::unique_ptr<AutofillProfile>>* profiles) { 934 std::vector<std::unique_ptr<AutofillProfile>>* profiles) {
937 DCHECK(profiles); 935 DCHECK(profiles);
938 profiles->clear(); 936 profiles->clear();
939 937
940 sql::Statement s(db_->GetUniqueStatement( 938 sql::Statement s(
941 "SELECT guid " 939 db_->GetUniqueStatement("SELECT guid "
942 "FROM autofill_profiles " 940 "FROM autofill_profiles "
943 "ORDER BY date_modified DESC, guid")); 941 "ORDER BY date_modified DESC, guid"));
944 942
945 while (s.Step()) { 943 while (s.Step()) {
946 std::string guid = s.ColumnString(0); 944 std::string guid = s.ColumnString(0);
947 std::unique_ptr<AutofillProfile> profile = GetAutofillProfile(guid); 945 std::unique_ptr<AutofillProfile> profile = GetAutofillProfile(guid);
948 if (!profile) 946 if (!profile)
949 return false; 947 return false;
950 profiles->push_back(std::move(profile)); 948 profiles->push_back(std::move(profile));
951 } 949 }
952 950
953 return s.Succeeded(); 951 return s.Succeeded();
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
1017 return s.Succeeded(); 1015 return s.Succeeded();
1018 } 1016 }
1019 1017
1020 void AutofillTable::SetServerProfiles( 1018 void AutofillTable::SetServerProfiles(
1021 const std::vector<AutofillProfile>& profiles) { 1019 const std::vector<AutofillProfile>& profiles) {
1022 sql::Transaction transaction(db_); 1020 sql::Transaction transaction(db_);
1023 if (!transaction.Begin()) 1021 if (!transaction.Begin())
1024 return; 1022 return;
1025 1023
1026 // Delete all old ones first. 1024 // Delete all old ones first.
1027 sql::Statement delete_old(db_->GetUniqueStatement( 1025 sql::Statement delete_old(
1028 "DELETE FROM server_addresses")); 1026 db_->GetUniqueStatement("DELETE FROM server_addresses"));
1029 delete_old.Run(); 1027 delete_old.Run();
1030 1028
1031 sql::Statement insert(db_->GetUniqueStatement( 1029 sql::Statement insert(db_->GetUniqueStatement(
1032 "INSERT INTO server_addresses(" 1030 "INSERT INTO server_addresses("
1033 "id," 1031 "id,"
1034 "recipient_name," 1032 "recipient_name,"
1035 "company_name," 1033 "company_name,"
1036 "street_address," 1034 "street_address,"
1037 "address_1," // ADDRESS_HOME_STATE 1035 "address_1," // ADDRESS_HOME_STATE
1038 "address_2," // ADDRESS_HOME_CITY 1036 "address_2," // ADDRESS_HOME_CITY
1039 "address_3," // ADDRESS_HOME_DEPENDENT_LOCALITY 1037 "address_3," // ADDRESS_HOME_DEPENDENT_LOCALITY
1040 "address_4," // Not supported in AutofillProfile yet. 1038 "address_4," // Not supported in AutofillProfile yet.
1041 "postal_code," // ADDRESS_HOME_ZIP 1039 "postal_code," // ADDRESS_HOME_ZIP
1042 "sorting_code," // ADDRESS_HOME_SORTING_CODE 1040 "sorting_code," // ADDRESS_HOME_SORTING_CODE
1043 "country_code," // ADDRESS_HOME_COUNTRY 1041 "country_code," // ADDRESS_HOME_COUNTRY
1044 "phone_number," // PHONE_HOME_WHOLE_NUMBER 1042 "phone_number," // PHONE_HOME_WHOLE_NUMBER
1045 "language_code) " 1043 "language_code) "
1046 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)")); 1044 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)"));
1047 for (const auto& profile : profiles) { 1045 for (const auto& profile : profiles) {
1048 DCHECK(profile.record_type() == AutofillProfile::SERVER_PROFILE); 1046 DCHECK(profile.record_type() == AutofillProfile::SERVER_PROFILE);
1049 1047
1050 int index = 0; 1048 int index = 0;
1051 insert.BindString(index++, profile.server_id()); 1049 insert.BindString(index++, profile.server_id());
1052 insert.BindString16(index++, profile.GetRawInfo(NAME_FULL)); 1050 insert.BindString16(index++, profile.GetRawInfo(NAME_FULL));
1053 insert.BindString16(index++, profile.GetRawInfo(COMPANY_NAME)); 1051 insert.BindString16(index++, profile.GetRawInfo(COMPANY_NAME));
1054 insert.BindString16(index++, 1052 insert.BindString16(index++,
1055 profile.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS)); 1053 profile.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS));
(...skipping 11 matching lines...) Expand all
1067 insert.Run(); 1065 insert.Run();
1068 insert.Reset(true); 1066 insert.Reset(true);
1069 1067
1070 // Save the use count and use date of the profile. 1068 // Save the use count and use date of the profile.
1071 UpdateServerAddressMetadata(profile); 1069 UpdateServerAddressMetadata(profile);
1072 } 1070 }
1073 1071
1074 // Delete metadata that's no longer relevant. 1072 // Delete metadata that's no longer relevant.
1075 sql::Statement metadata_delete(db_->GetUniqueStatement( 1073 sql::Statement metadata_delete(db_->GetUniqueStatement(
1076 "DELETE FROM server_address_metadata WHERE id NOT IN " 1074 "DELETE FROM server_address_metadata WHERE id NOT IN "
1077 "(SELECT id FROM server_addresses)")); 1075 "(SELECT id FROM server_addresses)"));
1078 metadata_delete.Run(); 1076 metadata_delete.Run();
1079 1077
1080 transaction.Commit(); 1078 transaction.Commit();
1081 } 1079 }
1082 1080
1083 bool AutofillTable::UpdateAutofillProfile(const AutofillProfile& profile) { 1081 bool AutofillTable::UpdateAutofillProfile(const AutofillProfile& profile) {
1084 DCHECK(base::IsValidGUID(profile.guid())); 1082 DCHECK(base::IsValidGUID(profile.guid()));
1085 1083
1086 // Don't update anything until the trash has been emptied. There may be 1084 // Don't update anything until the trash has been emptied. There may be
1087 // pending modifications to process. 1085 // pending modifications to process.
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
1126 if (IsAutofillGUIDInTrash(guid)) { 1124 if (IsAutofillGUIDInTrash(guid)) {
1127 sql::Statement s_trash(db_->GetUniqueStatement( 1125 sql::Statement s_trash(db_->GetUniqueStatement(
1128 "DELETE FROM autofill_profiles_trash WHERE guid = ?")); 1126 "DELETE FROM autofill_profiles_trash WHERE guid = ?"));
1129 s_trash.BindString(0, guid); 1127 s_trash.BindString(0, guid);
1130 1128
1131 bool success = s_trash.Run(); 1129 bool success = s_trash.Run();
1132 DCHECK_GT(db_->GetLastChangeCount(), 0) << "Expected item in trash"; 1130 DCHECK_GT(db_->GetLastChangeCount(), 0) << "Expected item in trash";
1133 return success; 1131 return success;
1134 } 1132 }
1135 1133
1136 sql::Statement s(db_->GetUniqueStatement( 1134 sql::Statement s(
1137 "DELETE FROM autofill_profiles WHERE guid = ?")); 1135 db_->GetUniqueStatement("DELETE FROM autofill_profiles WHERE guid = ?"));
1138 s.BindString(0, guid); 1136 s.BindString(0, guid);
1139 1137
1140 if (!s.Run()) 1138 if (!s.Run())
1141 return false; 1139 return false;
1142 1140
1143 return RemoveAutofillProfilePieces(guid, db_); 1141 return RemoveAutofillProfilePieces(guid, db_);
1144 } 1142 }
1145 1143
1146 bool AutofillTable::ClearAutofillProfiles() { 1144 bool AutofillTable::ClearAutofillProfiles() {
1147 sql::Statement s1(db_->GetUniqueStatement( 1145 sql::Statement s1(db_->GetUniqueStatement("DELETE FROM autofill_profiles"));
1148 "DELETE FROM autofill_profiles"));
1149 1146
1150 if (!s1.Run()) 1147 if (!s1.Run())
1151 return false; 1148 return false;
1152 1149
1153 sql::Statement s2(db_->GetUniqueStatement( 1150 sql::Statement s2(
1154 "DELETE FROM autofill_profile_names")); 1151 db_->GetUniqueStatement("DELETE FROM autofill_profile_names"));
1155 1152
1156 if (!s2.Run()) 1153 if (!s2.Run())
1157 return false; 1154 return false;
1158 1155
1159 sql::Statement s3(db_->GetUniqueStatement( 1156 sql::Statement s3(
1160 "DELETE FROM autofill_profile_emails")); 1157 db_->GetUniqueStatement("DELETE FROM autofill_profile_emails"));
1161 1158
1162 if (!s3.Run()) 1159 if (!s3.Run())
1163 return false; 1160 return false;
1164 1161
1165 sql::Statement s4(db_->GetUniqueStatement( 1162 sql::Statement s4(
1166 "DELETE FROM autofill_profile_phones")); 1163 db_->GetUniqueStatement("DELETE FROM autofill_profile_phones"));
1167 1164
1168 return s4.Run(); 1165 return s4.Run();
1169 } 1166 }
1170 1167
1171 bool AutofillTable::AddCreditCard(const CreditCard& credit_card) { 1168 bool AutofillTable::AddCreditCard(const CreditCard& credit_card) {
1172 sql::Statement s(db_->GetUniqueStatement( 1169 sql::Statement s(db_->GetUniqueStatement(
1173 "INSERT INTO credit_cards" 1170 "INSERT INTO credit_cards"
1174 "(guid, name_on_card, expiration_month, expiration_year, " 1171 "(guid, name_on_card, expiration_month, expiration_year, "
1175 " card_number_encrypted, use_count, use_date, date_modified, origin," 1172 " card_number_encrypted, use_count, use_date, date_modified, origin,"
1176 " billing_address_id)" 1173 " billing_address_id)"
1177 "VALUES (?,?,?,?,?,?,?,?,?,?)")); 1174 "VALUES (?,?,?,?,?,?,?,?,?,?)"));
1178 BindCreditCardToStatement(credit_card, AutofillClock::Now(), &s, 1175 BindCreditCardToStatement(credit_card, AutofillClock::Now(), &s,
1179 *autofill_table_encryptor_); 1176 *autofill_table_encryptor_);
1180 1177
1181 if (!s.Run()) 1178 if (!s.Run())
1182 return false; 1179 return false;
1183 1180
1184 DCHECK_GT(db_->GetLastChangeCount(), 0); 1181 DCHECK_GT(db_->GetLastChangeCount(), 0);
1185 return true; 1182 return true;
1186 } 1183 }
1187 1184
1188 std::unique_ptr<CreditCard> AutofillTable::GetCreditCard( 1185 std::unique_ptr<CreditCard> AutofillTable::GetCreditCard(
1189 const std::string& guid) { 1186 const std::string& guid) {
1190 DCHECK(base::IsValidGUID(guid)); 1187 DCHECK(base::IsValidGUID(guid));
1191 sql::Statement s(db_->GetUniqueStatement( 1188 sql::Statement s(db_->GetUniqueStatement(
1192 "SELECT guid, name_on_card, expiration_month, expiration_year, " 1189 "SELECT guid, name_on_card, expiration_month, expiration_year, "
1193 "card_number_encrypted, use_count, use_date, date_modified, " 1190 "card_number_encrypted, use_count, use_date, date_modified, "
1194 "origin, billing_address_id " 1191 "origin, billing_address_id "
1195 "FROM credit_cards " 1192 "FROM credit_cards "
1196 "WHERE guid = ?")); 1193 "WHERE guid = ?"));
1197 s.BindString(0, guid); 1194 s.BindString(0, guid);
1198 1195
1199 if (!s.Step()) 1196 if (!s.Step())
1200 return std::unique_ptr<CreditCard>(); 1197 return std::unique_ptr<CreditCard>();
1201 1198
1202 return CreditCardFromStatement(s, *autofill_table_encryptor_); 1199 return CreditCardFromStatement(s, *autofill_table_encryptor_);
1203 } 1200 }
1204 1201
1205 bool AutofillTable::GetCreditCards( 1202 bool AutofillTable::GetCreditCards(
1206 std::vector<std::unique_ptr<CreditCard>>* credit_cards) { 1203 std::vector<std::unique_ptr<CreditCard>>* credit_cards) {
1207 DCHECK(credit_cards); 1204 DCHECK(credit_cards);
1208 credit_cards->clear(); 1205 credit_cards->clear();
1209 1206
1210 sql::Statement s(db_->GetUniqueStatement( 1207 sql::Statement s(
1211 "SELECT guid " 1208 db_->GetUniqueStatement("SELECT guid "
1212 "FROM credit_cards " 1209 "FROM credit_cards "
1213 "ORDER BY date_modified DESC, guid")); 1210 "ORDER BY date_modified DESC, guid"));
1214 1211
1215 while (s.Step()) { 1212 while (s.Step()) {
1216 std::string guid = s.ColumnString(0); 1213 std::string guid = s.ColumnString(0);
1217 std::unique_ptr<CreditCard> credit_card = GetCreditCard(guid); 1214 std::unique_ptr<CreditCard> credit_card = GetCreditCard(guid);
1218 if (!credit_card) 1215 if (!credit_card)
1219 return false; 1216 return false;
1220 credit_cards->push_back(std::move(credit_card)); 1217 credit_cards->push_back(std::move(credit_card));
1221 } 1218 }
1222 1219
1223 return s.Succeeded(); 1220 return s.Succeeded();
1224 } 1221 }
1225 1222
1226 bool AutofillTable::GetServerCreditCards( 1223 bool AutofillTable::GetServerCreditCards(
1227 std::vector<std::unique_ptr<CreditCard>>* credit_cards) const { 1224 std::vector<std::unique_ptr<CreditCard>>* credit_cards) const {
1228 credit_cards->clear(); 1225 credit_cards->clear();
1229 1226
1230 sql::Statement s(db_->GetUniqueStatement( 1227 sql::Statement s(db_->GetUniqueStatement(
1231 "SELECT " 1228 "SELECT "
1232 "card_number_encrypted, " // 0 1229 "card_number_encrypted, " // 0
1233 "last_four," // 1 1230 "last_four," // 1
1234 "masked.id," // 2 1231 "masked.id," // 2
1235 "metadata.use_count," // 3 1232 "metadata.use_count," // 3
1236 "metadata.use_date," // 4 1233 "metadata.use_date," // 4
1237 "type," // 5 1234 "network," // 5
1238 "status," // 6 1235 "status," // 6
1239 "name_on_card," // 7 1236 "name_on_card," // 7
1240 "exp_month," // 8 1237 "exp_month," // 8
1241 "exp_year," // 9 1238 "exp_year," // 9
1242 "metadata.billing_address_id " // 10 1239 "metadata.billing_address_id " // 10
1243 "FROM masked_credit_cards masked " 1240 "FROM masked_credit_cards masked "
1244 "LEFT OUTER JOIN unmasked_credit_cards USING (id) " 1241 "LEFT OUTER JOIN unmasked_credit_cards USING (id) "
1245 "LEFT OUTER JOIN server_card_metadata metadata USING (id)")); 1242 "LEFT OUTER JOIN server_card_metadata metadata USING (id)"));
1246 while (s.Step()) { 1243 while (s.Step()) {
1247 int index = 0; 1244 int index = 0;
1248 1245
1249 // If the card_number_encrypted field is nonempty, we can assume this card 1246 // If the card_number_encrypted field is nonempty, we can assume this card
1250 // is a full card, otherwise it's masked. 1247 // is a full card, otherwise it's masked.
1251 base::string16 full_card_number = 1248 base::string16 full_card_number =
1252 UnencryptedCardFromColumn(s, index++, *autofill_table_encryptor_); 1249 UnencryptedCardFromColumn(s, index++, *autofill_table_encryptor_);
1253 base::string16 last_four = s.ColumnString16(index++); 1250 base::string16 last_four = s.ColumnString16(index++);
1254 CreditCard::RecordType record_type = full_card_number.empty() ? 1251 CreditCard::RecordType record_type = full_card_number.empty()
1255 CreditCard::MASKED_SERVER_CARD : 1252 ? CreditCard::MASKED_SERVER_CARD
1256 CreditCard::FULL_SERVER_CARD; 1253 : CreditCard::FULL_SERVER_CARD;
1257 std::string server_id = s.ColumnString(index++); 1254 std::string server_id = s.ColumnString(index++);
1258 std::unique_ptr<CreditCard> card = 1255 std::unique_ptr<CreditCard> card =
1259 base::MakeUnique<CreditCard>(record_type, server_id); 1256 base::MakeUnique<CreditCard>(record_type, server_id);
1260 card->SetRawInfo( 1257 card->SetRawInfo(CREDIT_CARD_NUMBER,
1261 CREDIT_CARD_NUMBER, 1258 record_type == CreditCard::MASKED_SERVER_CARD
1262 record_type == CreditCard::MASKED_SERVER_CARD ? last_four 1259 ? last_four
1263 : full_card_number); 1260 : full_card_number);
1264 card->set_use_count(s.ColumnInt64(index++)); 1261 card->set_use_count(s.ColumnInt64(index++));
1265 card->set_use_date(Time::FromInternalValue(s.ColumnInt64(index++))); 1262 card->set_use_date(Time::FromInternalValue(s.ColumnInt64(index++)));
1266 // Modification date is not tracked for server cards. Explicitly set it here 1263 // Modification date is not tracked for server cards. Explicitly set it here
1267 // to override the default value of AutofillClock::Now(). 1264 // to override the default value of AutofillClock::Now().
1268 card->set_modification_date(Time()); 1265 card->set_modification_date(Time());
1269 1266
1270 std::string card_type = s.ColumnString(index++); 1267 std::string card_network = s.ColumnString(index++);
1271 if (record_type == CreditCard::MASKED_SERVER_CARD) { 1268 if (record_type == CreditCard::MASKED_SERVER_CARD) {
1272 // The type must be set after setting the number to override the 1269 // The issuer network must be set after setting the number to override the
1273 // autodetected type. 1270 // autodetected issuer network.
1274 card->SetTypeForMaskedCard(card_type.c_str()); 1271 card->SetNetworkForMaskedCard(card_network.c_str());
1275 } else { 1272 } else {
1276 DCHECK_EQ(CreditCard::GetCreditCardType(full_card_number), card_type); 1273 DCHECK_EQ(CreditCard::GetCardNetwork(full_card_number), card_network);
1277 } 1274 }
1278 1275
1279 card->SetServerStatus(ServerStatusStringToEnum(s.ColumnString(index++))); 1276 card->SetServerStatus(ServerStatusStringToEnum(s.ColumnString(index++)));
1280 card->SetRawInfo(CREDIT_CARD_NAME_FULL, s.ColumnString16(index++)); 1277 card->SetRawInfo(CREDIT_CARD_NAME_FULL, s.ColumnString16(index++));
1281 card->SetRawInfo(CREDIT_CARD_EXP_MONTH, s.ColumnString16(index++)); 1278 card->SetRawInfo(CREDIT_CARD_EXP_MONTH, s.ColumnString16(index++));
1282 card->SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, s.ColumnString16(index++)); 1279 card->SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, s.ColumnString16(index++));
1283 card->set_billing_address_id(s.ColumnString(index++)); 1280 card->set_billing_address_id(s.ColumnString(index++));
1284 credit_cards->push_back(std::move(card)); 1281 credit_cards->push_back(std::move(card));
1285 } 1282 }
1286 return s.Succeeded(); 1283 return s.Succeeded();
1287 } 1284 }
1288 1285
1289 void AutofillTable::AddMaskedCreditCards( 1286 void AutofillTable::AddMaskedCreditCards(
1290 const std::vector<CreditCard>& credit_cards) { 1287 const std::vector<CreditCard>& credit_cards) {
1291 DCHECK_GT(db_->transaction_nesting(), 0); 1288 DCHECK_GT(db_->transaction_nesting(), 0);
1292 sql::Statement masked_insert( 1289 sql::Statement masked_insert(
1293 db_->GetUniqueStatement("INSERT INTO masked_credit_cards(" 1290 db_->GetUniqueStatement("INSERT INTO masked_credit_cards("
1294 "id," // 0 1291 "id," // 0
1295 "type," // 1 1292 "network," // 1
1296 "status," // 2 1293 "status," // 2
1297 "name_on_card," // 3 1294 "name_on_card," // 3
1298 "last_four," // 4 1295 "last_four," // 4
1299 "exp_month," // 5 1296 "exp_month," // 5
1300 "exp_year)" // 6 1297 "exp_year)" // 6
1301 "VALUES (?,?,?,?,?,?,?)")); 1298 "VALUES (?,?,?,?,?,?,?)"));
1302 for (const CreditCard& card : credit_cards) { 1299 for (const CreditCard& card : credit_cards) {
1303 DCHECK_EQ(CreditCard::MASKED_SERVER_CARD, card.record_type()); 1300 DCHECK_EQ(CreditCard::MASKED_SERVER_CARD, card.record_type());
1304 masked_insert.BindString(0, card.server_id()); 1301 masked_insert.BindString(0, card.server_id());
1305 masked_insert.BindString(1, card.type()); 1302 masked_insert.BindString(1, card.network());
1306 masked_insert.BindString(2, 1303 masked_insert.BindString(2,
1307 ServerStatusEnumToString(card.GetServerStatus())); 1304 ServerStatusEnumToString(card.GetServerStatus()));
1308 masked_insert.BindString16(3, card.GetRawInfo(CREDIT_CARD_NAME_FULL)); 1305 masked_insert.BindString16(3, card.GetRawInfo(CREDIT_CARD_NAME_FULL));
1309 masked_insert.BindString16(4, card.LastFourDigits()); 1306 masked_insert.BindString16(4, card.LastFourDigits());
1310 masked_insert.BindString16(5, card.GetRawInfo(CREDIT_CARD_EXP_MONTH)); 1307 masked_insert.BindString16(5, card.GetRawInfo(CREDIT_CARD_EXP_MONTH));
1311 masked_insert.BindString16(6, 1308 masked_insert.BindString16(6,
1312 card.GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR)); 1309 card.GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR));
1313 1310
1314 masked_insert.Run(); 1311 masked_insert.Run();
1315 masked_insert.Reset(true); 1312 masked_insert.Reset(true);
(...skipping 12 matching lines...) Expand all
1328 // Delete all old values. 1325 // Delete all old values.
1329 sql::Statement masked_delete( 1326 sql::Statement masked_delete(
1330 db_->GetUniqueStatement("DELETE FROM masked_credit_cards")); 1327 db_->GetUniqueStatement("DELETE FROM masked_credit_cards"));
1331 masked_delete.Run(); 1328 masked_delete.Run();
1332 1329
1333 AddMaskedCreditCards(credit_cards); 1330 AddMaskedCreditCards(credit_cards);
1334 1331
1335 // Delete all items in the unmasked table that aren't in the new set. 1332 // Delete all items in the unmasked table that aren't in the new set.
1336 sql::Statement unmasked_delete(db_->GetUniqueStatement( 1333 sql::Statement unmasked_delete(db_->GetUniqueStatement(
1337 "DELETE FROM unmasked_credit_cards WHERE id NOT IN " 1334 "DELETE FROM unmasked_credit_cards WHERE id NOT IN "
1338 "(SELECT id FROM masked_credit_cards)")); 1335 "(SELECT id FROM masked_credit_cards)"));
1339 unmasked_delete.Run(); 1336 unmasked_delete.Run();
1340 // Do the same for metadata. 1337 // Do the same for metadata.
1341 sql::Statement metadata_delete(db_->GetUniqueStatement( 1338 sql::Statement metadata_delete(db_->GetUniqueStatement(
1342 "DELETE FROM server_card_metadata WHERE id NOT IN " 1339 "DELETE FROM server_card_metadata WHERE id NOT IN "
1343 "(SELECT id FROM masked_credit_cards)")); 1340 "(SELECT id FROM masked_credit_cards)"));
1344 metadata_delete.Run(); 1341 metadata_delete.Run();
1345 1342
1346 transaction.Commit(); 1343 transaction.Commit();
1347 } 1344 }
1348 1345
1349 bool AutofillTable::AddFullServerCreditCard(const CreditCard& credit_card) { 1346 bool AutofillTable::AddFullServerCreditCard(const CreditCard& credit_card) {
1350 DCHECK_EQ(CreditCard::FULL_SERVER_CARD, credit_card.record_type()); 1347 DCHECK_EQ(CreditCard::FULL_SERVER_CARD, credit_card.record_type());
1351 DCHECK(!credit_card.number().empty()); 1348 DCHECK(!credit_card.number().empty());
1352 DCHECK(!credit_card.server_id().empty()); 1349 DCHECK(!credit_card.server_id().empty());
1353 1350
1354 sql::Transaction transaction(db_); 1351 sql::Transaction transaction(db_);
1355 if (!transaction.Begin()) 1352 if (!transaction.Begin())
1356 return false; 1353 return false;
1357 1354
1358 // Make sure there aren't duplicates for this card. 1355 // Make sure there aren't duplicates for this card.
1359 DeleteFromUnmaskedCreditCards(credit_card.server_id()); 1356 DeleteFromUnmaskedCreditCards(credit_card.server_id());
1360 DeleteFromMaskedCreditCards(credit_card.server_id()); 1357 DeleteFromMaskedCreditCards(credit_card.server_id());
1361 1358
1362 CreditCard masked(credit_card); 1359 CreditCard masked(credit_card);
1363 masked.set_record_type(CreditCard::MASKED_SERVER_CARD); 1360 masked.set_record_type(CreditCard::MASKED_SERVER_CARD);
1364 masked.SetNumber(credit_card.LastFourDigits()); 1361 masked.SetNumber(credit_card.LastFourDigits());
1365 masked.RecordAndLogUse(); 1362 masked.RecordAndLogUse();
1366 DCHECK(!masked.type().empty()); 1363 DCHECK(!masked.network().empty());
1367 AddMaskedCreditCards({masked}); 1364 AddMaskedCreditCards({masked});
1368 1365
1369 AddUnmaskedCreditCard(credit_card.server_id(), credit_card.number()); 1366 AddUnmaskedCreditCard(credit_card.server_id(), credit_card.number());
1370 1367
1371 transaction.Commit(); 1368 transaction.Commit();
1372 1369
1373 return db_->GetLastChangeCount() > 0; 1370 return db_->GetLastChangeCount() > 0;
1374 } 1371 }
1375 1372
1376 void AutofillTable::AddUnmaskedCreditCard(const std::string& id, 1373 void AutofillTable::AddUnmaskedCreditCard(const std::string& id,
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
1430 return db_->GetLastChangeCount() > 0; 1427 return db_->GetLastChangeCount() > 0;
1431 } 1428 }
1432 1429
1433 bool AutofillTable::MaskServerCreditCard(const std::string& id) { 1430 bool AutofillTable::MaskServerCreditCard(const std::string& id) {
1434 return DeleteFromUnmaskedCreditCards(id); 1431 return DeleteFromUnmaskedCreditCards(id);
1435 } 1432 }
1436 1433
1437 bool AutofillTable::UpdateServerCardMetadata(const CreditCard& credit_card) { 1434 bool AutofillTable::UpdateServerCardMetadata(const CreditCard& credit_card) {
1438 DCHECK_NE(CreditCard::LOCAL_CARD, credit_card.record_type()); 1435 DCHECK_NE(CreditCard::LOCAL_CARD, credit_card.record_type());
1439 1436
1440 sql::Statement remove(db_->GetUniqueStatement( 1437 sql::Statement remove(
1441 "DELETE FROM server_card_metadata WHERE id = ?")); 1438 db_->GetUniqueStatement("DELETE FROM server_card_metadata WHERE id = ?"));
1442 remove.BindString(0, credit_card.server_id()); 1439 remove.BindString(0, credit_card.server_id());
1443 remove.Run(); 1440 remove.Run();
1444 1441
1445 sql::Statement s( 1442 sql::Statement s(
1446 db_->GetUniqueStatement("INSERT INTO server_card_metadata(use_count, " 1443 db_->GetUniqueStatement("INSERT INTO server_card_metadata(use_count, "
1447 "use_date, billing_address_id, id)" 1444 "use_date, billing_address_id, id)"
1448 "VALUES (?,?,?,?)")); 1445 "VALUES (?,?,?,?)"));
1449 s.BindInt64(0, credit_card.use_count()); 1446 s.BindInt64(0, credit_card.use_count());
1450 s.BindInt64(1, credit_card.use_date().ToInternalValue()); 1447 s.BindInt64(1, credit_card.use_date().ToInternalValue());
1451 s.BindString(2, credit_card.billing_address_id()); 1448 s.BindString(2, credit_card.billing_address_id());
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
1483 transaction.Commit(); 1480 transaction.Commit();
1484 1481
1485 return db_->GetLastChangeCount() > 0; 1482 return db_->GetLastChangeCount() > 0;
1486 } 1483 }
1487 1484
1488 bool AutofillTable::ClearAllServerData() { 1485 bool AutofillTable::ClearAllServerData() {
1489 sql::Transaction transaction(db_); 1486 sql::Transaction transaction(db_);
1490 if (!transaction.Begin()) 1487 if (!transaction.Begin())
1491 return false; // Some error, nothing was changed. 1488 return false; // Some error, nothing was changed.
1492 1489
1493 sql::Statement masked(db_->GetUniqueStatement( 1490 sql::Statement masked(
1494 "DELETE FROM masked_credit_cards")); 1491 db_->GetUniqueStatement("DELETE FROM masked_credit_cards"));
1495 masked.Run(); 1492 masked.Run();
1496 bool changed = db_->GetLastChangeCount() > 0; 1493 bool changed = db_->GetLastChangeCount() > 0;
1497 1494
1498 sql::Statement unmasked(db_->GetUniqueStatement( 1495 sql::Statement unmasked(
1499 "DELETE FROM unmasked_credit_cards")); 1496 db_->GetUniqueStatement("DELETE FROM unmasked_credit_cards"));
1500 unmasked.Run(); 1497 unmasked.Run();
1501 changed |= db_->GetLastChangeCount() > 0; 1498 changed |= db_->GetLastChangeCount() > 0;
1502 1499
1503 sql::Statement addresses(db_->GetUniqueStatement( 1500 sql::Statement addresses(
1504 "DELETE FROM server_addresses")); 1501 db_->GetUniqueStatement("DELETE FROM server_addresses"));
1505 addresses.Run(); 1502 addresses.Run();
1506 changed |= db_->GetLastChangeCount() > 0; 1503 changed |= db_->GetLastChangeCount() > 0;
1507 1504
1508 sql::Statement card_metadata(db_->GetUniqueStatement( 1505 sql::Statement card_metadata(
1509 "DELETE FROM server_card_metadata")); 1506 db_->GetUniqueStatement("DELETE FROM server_card_metadata"));
1510 card_metadata.Run(); 1507 card_metadata.Run();
1511 changed |= db_->GetLastChangeCount() > 0; 1508 changed |= db_->GetLastChangeCount() > 0;
1512 1509
1513 sql::Statement address_metadata(db_->GetUniqueStatement( 1510 sql::Statement address_metadata(
1514 "DELETE FROM server_address_metadata")); 1511 db_->GetUniqueStatement("DELETE FROM server_address_metadata"));
1515 address_metadata.Run(); 1512 address_metadata.Run();
1516 changed |= db_->GetLastChangeCount() > 0; 1513 changed |= db_->GetLastChangeCount() > 0;
1517 1514
1518 transaction.Commit(); 1515 transaction.Commit();
1519 return changed; 1516 return changed;
1520 } 1517 }
1521 1518
1522 bool AutofillTable::UpdateCreditCard(const CreditCard& credit_card) { 1519 bool AutofillTable::UpdateCreditCard(const CreditCard& credit_card) {
1523 DCHECK(base::IsValidGUID(credit_card.guid())); 1520 DCHECK(base::IsValidGUID(credit_card.guid()));
1524 1521
1525 std::unique_ptr<CreditCard> old_credit_card = 1522 std::unique_ptr<CreditCard> old_credit_card =
1526 GetCreditCard(credit_card.guid()); 1523 GetCreditCard(credit_card.guid());
1527 if (!old_credit_card) 1524 if (!old_credit_card)
1528 return false; 1525 return false;
1529 1526
1530 bool update_modification_date = *old_credit_card != credit_card; 1527 bool update_modification_date = *old_credit_card != credit_card;
1531 1528
1532 sql::Statement s(db_->GetUniqueStatement( 1529 sql::Statement s(db_->GetUniqueStatement(
1533 "UPDATE credit_cards " 1530 "UPDATE credit_cards "
1534 "SET guid=?, name_on_card=?, expiration_month=?," 1531 "SET guid=?, name_on_card=?, expiration_month=?,"
1535 "expiration_year=?, card_number_encrypted=?, use_count=?, use_date=?," 1532 "expiration_year=?, card_number_encrypted=?, use_count=?, use_date=?,"
1536 "date_modified=?, origin=?, billing_address_id=?" 1533 "date_modified=?, origin=?, billing_address_id=?"
1537 "WHERE guid=?1")); 1534 "WHERE guid=?1"));
1538 BindCreditCardToStatement(credit_card, 1535 BindCreditCardToStatement(credit_card,
1539 update_modification_date 1536 update_modification_date
1540 ? AutofillClock::Now() 1537 ? AutofillClock::Now()
1541 : old_credit_card->modification_date(), 1538 : old_credit_card->modification_date(),
1542 &s, *autofill_table_encryptor_); 1539 &s, *autofill_table_encryptor_);
1543 1540
1544 bool result = s.Run(); 1541 bool result = s.Run();
1545 DCHECK_GT(db_->GetLastChangeCount(), 0); 1542 DCHECK_GT(db_->GetLastChangeCount(), 0);
1546 return result; 1543 return result;
1547 } 1544 }
1548 1545
1549 bool AutofillTable::RemoveCreditCard(const std::string& guid) { 1546 bool AutofillTable::RemoveCreditCard(const std::string& guid) {
1550 DCHECK(base::IsValidGUID(guid)); 1547 DCHECK(base::IsValidGUID(guid));
1551 sql::Statement s(db_->GetUniqueStatement( 1548 sql::Statement s(
1552 "DELETE FROM credit_cards WHERE guid = ?")); 1549 db_->GetUniqueStatement("DELETE FROM credit_cards WHERE guid = ?"));
1553 s.BindString(0, guid); 1550 s.BindString(0, guid);
1554 1551
1555 return s.Run(); 1552 return s.Run();
1556 } 1553 }
1557 1554
1558 bool AutofillTable::RemoveAutofillDataModifiedBetween( 1555 bool AutofillTable::RemoveAutofillDataModifiedBetween(
1559 const Time& delete_begin, 1556 const Time& delete_begin,
1560 const Time& delete_end, 1557 const Time& delete_end,
1561 std::vector<std::string>* profile_guids, 1558 std::vector<std::string>* profile_guids,
1562 std::vector<std::string>* credit_card_guids) { 1559 std::vector<std::string>* credit_card_guids) {
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
1608 // Remove Autofill credit cards in the time range. 1605 // Remove Autofill credit cards in the time range.
1609 sql::Statement s_credit_cards(db_->GetUniqueStatement( 1606 sql::Statement s_credit_cards(db_->GetUniqueStatement(
1610 "DELETE FROM credit_cards " 1607 "DELETE FROM credit_cards "
1611 "WHERE date_modified >= ? AND date_modified < ?")); 1608 "WHERE date_modified >= ? AND date_modified < ?"));
1612 s_credit_cards.BindInt64(0, delete_begin_t); 1609 s_credit_cards.BindInt64(0, delete_begin_t);
1613 s_credit_cards.BindInt64(1, delete_end_t); 1610 s_credit_cards.BindInt64(1, delete_end_t);
1614 if (!s_credit_cards.Run()) 1611 if (!s_credit_cards.Run())
1615 return false; 1612 return false;
1616 1613
1617 // Remove unmasked credit cards in the time range. 1614 // Remove unmasked credit cards in the time range.
1618 sql::Statement s_unmasked_cards(db_->GetUniqueStatement( 1615 sql::Statement s_unmasked_cards(
1619 "DELETE FROM unmasked_credit_cards " 1616 db_->GetUniqueStatement("DELETE FROM unmasked_credit_cards "
1620 "WHERE unmask_date >= ? AND unmask_date < ?")); 1617 "WHERE unmask_date >= ? AND unmask_date < ?"));
1621 s_unmasked_cards.BindInt64(0, delete_begin.ToInternalValue()); 1618 s_unmasked_cards.BindInt64(0, delete_begin.ToInternalValue());
1622 s_unmasked_cards.BindInt64(1, delete_end.ToInternalValue()); 1619 s_unmasked_cards.BindInt64(1, delete_end.ToInternalValue());
1623 return s_unmasked_cards.Run(); 1620 return s_unmasked_cards.Run();
1624 } 1621 }
1625 1622
1626 bool AutofillTable::RemoveOriginURLsModifiedBetween( 1623 bool AutofillTable::RemoveOriginURLsModifiedBetween(
1627 const Time& delete_begin, 1624 const Time& delete_begin,
1628 const Time& delete_end, 1625 const Time& delete_end,
1629 std::vector<std::unique_ptr<AutofillProfile>>* profiles) { 1626 std::vector<std::unique_ptr<AutofillProfile>>* profiles) {
1630 DCHECK(delete_end.is_null() || delete_begin < delete_end); 1627 DCHECK(delete_end.is_null() || delete_begin < delete_end);
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
1690 return false; 1687 return false;
1691 } 1688 }
1692 1689
1693 return true; 1690 return true;
1694 } 1691 }
1695 1692
1696 bool AutofillTable::GetAutofillProfilesInTrash( 1693 bool AutofillTable::GetAutofillProfilesInTrash(
1697 std::vector<std::string>* guids) { 1694 std::vector<std::string>* guids) {
1698 guids->clear(); 1695 guids->clear();
1699 1696
1700 sql::Statement s(db_->GetUniqueStatement( 1697 sql::Statement s(
1701 "SELECT guid " 1698 db_->GetUniqueStatement("SELECT guid "
1702 "FROM autofill_profiles_trash")); 1699 "FROM autofill_profiles_trash"));
1703 1700
1704 while (s.Step()) { 1701 while (s.Step()) {
1705 std::string guid = s.ColumnString(0); 1702 std::string guid = s.ColumnString(0);
1706 guids->push_back(guid); 1703 guids->push_back(guid);
1707 } 1704 }
1708 1705
1709 return s.Succeeded(); 1706 return s.Succeeded();
1710 } 1707 }
1711 1708
1712 bool AutofillTable::EmptyAutofillProfilesTrash() { 1709 bool AutofillTable::EmptyAutofillProfilesTrash() {
1713 sql::Statement s(db_->GetUniqueStatement( 1710 sql::Statement s(
1714 "DELETE FROM autofill_profiles_trash")); 1711 db_->GetUniqueStatement("DELETE FROM autofill_profiles_trash"));
1715 1712
1716 return s.Run(); 1713 return s.Run();
1717 } 1714 }
1718 1715
1719
1720 bool AutofillTable::AddAutofillGUIDToTrash(const std::string& guid) { 1716 bool AutofillTable::AddAutofillGUIDToTrash(const std::string& guid) {
1721 sql::Statement s(db_->GetUniqueStatement( 1717 sql::Statement s(
1722 "INSERT INTO autofill_profiles_trash" 1718 db_->GetUniqueStatement("INSERT INTO autofill_profiles_trash"
1723 " (guid) " 1719 " (guid) "
1724 "VALUES (?)")); 1720 "VALUES (?)"));
1725 s.BindString(0, guid); 1721 s.BindString(0, guid);
1726 1722
1727 return s.Run(); 1723 return s.Run();
1728 } 1724 }
1729 1725
1730 bool AutofillTable::IsAutofillProfilesTrashEmpty() { 1726 bool AutofillTable::IsAutofillProfilesTrashEmpty() {
1731 sql::Statement s(db_->GetUniqueStatement( 1727 sql::Statement s(
1732 "SELECT guid " 1728 db_->GetUniqueStatement("SELECT guid "
1733 "FROM autofill_profiles_trash")); 1729 "FROM autofill_profiles_trash"));
1734 1730
1735 return !s.Step(); 1731 return !s.Step();
1736 } 1732 }
1737 1733
1738 bool AutofillTable::IsAutofillGUIDInTrash(const std::string& guid) { 1734 bool AutofillTable::IsAutofillGUIDInTrash(const std::string& guid) {
1739 sql::Statement s(db_->GetUniqueStatement( 1735 sql::Statement s(
1740 "SELECT guid " 1736 db_->GetUniqueStatement("SELECT guid "
1741 "FROM autofill_profiles_trash " 1737 "FROM autofill_profiles_trash "
1742 "WHERE guid = ?")); 1738 "WHERE guid = ?"));
1743 s.BindString(0, guid); 1739 s.BindString(0, guid);
1744 1740
1745 return s.Step(); 1741 return s.Step();
1746 } 1742 }
1747 1743
1748 bool AutofillTable::GetAllSyncMetadata(syncer::ModelType model_type, 1744 bool AutofillTable::GetAllSyncMetadata(syncer::ModelType model_type,
1749 syncer::MetadataBatch* metadata_batch) { 1745 syncer::MetadataBatch* metadata_batch) {
1750 DCHECK_EQ(model_type, syncer::AUTOFILL) 1746 DCHECK_EQ(model_type, syncer::AUTOFILL)
1751 << "Only the AUTOFILL model type is supported"; 1747 << "Only the AUTOFILL model type is supported";
1752 DCHECK(metadata_batch); 1748 DCHECK(metadata_batch);
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
1865 "name VARCHAR, " 1861 "name VARCHAR, "
1866 "value VARCHAR, " 1862 "value VARCHAR, "
1867 "value_lower VARCHAR, " 1863 "value_lower VARCHAR, "
1868 "date_created INTEGER DEFAULT 0, " 1864 "date_created INTEGER DEFAULT 0, "
1869 "date_last_used INTEGER DEFAULT 0, " 1865 "date_last_used INTEGER DEFAULT 0, "
1870 "count INTEGER DEFAULT 1, " 1866 "count INTEGER DEFAULT 1, "
1871 "PRIMARY KEY (name, value))") || 1867 "PRIMARY KEY (name, value))") ||
1872 !db_->Execute("CREATE INDEX autofill_name ON autofill (name)") || 1868 !db_->Execute("CREATE INDEX autofill_name ON autofill (name)") ||
1873 !db_->Execute("CREATE INDEX autofill_name_value_lower ON " 1869 !db_->Execute("CREATE INDEX autofill_name_value_lower ON "
1874 "autofill (name, value_lower)")) { 1870 "autofill (name, value_lower)")) {
1875 NOTREACHED(); 1871 NOTREACHED();
1876 return false; 1872 return false;
1877 } 1873 }
1878 } 1874 }
1879 return true; 1875 return true;
1880 } 1876 }
1881 1877
1882 bool AutofillTable::InitCreditCardsTable() { 1878 bool AutofillTable::InitCreditCardsTable() {
1883 if (!db_->DoesTableExist("credit_cards")) { 1879 if (!db_->DoesTableExist("credit_cards")) {
1884 if (!db_->Execute("CREATE TABLE credit_cards ( " 1880 if (!db_->Execute("CREATE TABLE credit_cards ( "
1885 "guid VARCHAR PRIMARY KEY, " 1881 "guid VARCHAR PRIMARY KEY, "
1886 "name_on_card VARCHAR, " 1882 "name_on_card VARCHAR, "
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
1973 } 1969 }
1974 return true; 1970 return true;
1975 } 1971 }
1976 1972
1977 bool AutofillTable::InitMaskedCreditCardsTable() { 1973 bool AutofillTable::InitMaskedCreditCardsTable() {
1978 if (!db_->DoesTableExist("masked_credit_cards")) { 1974 if (!db_->DoesTableExist("masked_credit_cards")) {
1979 if (!db_->Execute("CREATE TABLE masked_credit_cards (" 1975 if (!db_->Execute("CREATE TABLE masked_credit_cards ("
1980 "id VARCHAR," 1976 "id VARCHAR,"
1981 "status VARCHAR," 1977 "status VARCHAR,"
1982 "name_on_card VARCHAR," 1978 "name_on_card VARCHAR,"
1983 "type VARCHAR," 1979 "network VARCHAR,"
1984 "last_four VARCHAR," 1980 "last_four VARCHAR,"
1985 "exp_month INTEGER DEFAULT 0," 1981 "exp_month INTEGER DEFAULT 0,"
1986 "exp_year INTEGER DEFAULT 0)")) { 1982 "exp_year INTEGER DEFAULT 0)")) {
1987 NOTREACHED(); 1983 NOTREACHED();
1988 return false; 1984 return false;
1989 } 1985 }
1990 } 1986 }
1991 return true; 1987 return true;
1992 } 1988 }
1993 1989
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
2028 "id VARCHAR," 2024 "id VARCHAR,"
2029 "company_name VARCHAR," 2025 "company_name VARCHAR,"
2030 "street_address VARCHAR," 2026 "street_address VARCHAR,"
2031 "address_1 VARCHAR," 2027 "address_1 VARCHAR,"
2032 "address_2 VARCHAR," 2028 "address_2 VARCHAR,"
2033 "address_3 VARCHAR," 2029 "address_3 VARCHAR,"
2034 "address_4 VARCHAR," 2030 "address_4 VARCHAR,"
2035 "postal_code VARCHAR," 2031 "postal_code VARCHAR,"
2036 "sorting_code VARCHAR," 2032 "sorting_code VARCHAR,"
2037 "country_code VARCHAR," 2033 "country_code VARCHAR,"
2038 "language_code VARCHAR, " // Space required. 2034 "language_code VARCHAR, " // Space required.
2039 "recipient_name VARCHAR, " // Ditto. 2035 "recipient_name VARCHAR, " // Ditto.
2040 "phone_number VARCHAR)")) { 2036 "phone_number VARCHAR)")) {
2041 NOTREACHED(); 2037 NOTREACHED();
2042 return false; 2038 return false;
2043 } 2039 }
2044 } 2040 }
2045 return true; 2041 return true;
2046 } 2042 }
2047 2043
2048 bool AutofillTable::InitServerAddressMetadataTable() { 2044 bool AutofillTable::InitServerAddressMetadataTable() {
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
2236 return false; 2232 return false;
2237 } 2233 }
2238 2234
2239 // Create indices on the new table, for fast lookups. 2235 // Create indices on the new table, for fast lookups.
2240 if (!db_->Execute("CREATE INDEX autofill_name ON autofill (name)") || 2236 if (!db_->Execute("CREATE INDEX autofill_name ON autofill (name)") ||
2241 !db_->Execute("CREATE INDEX autofill_name_value_lower ON " 2237 !db_->Execute("CREATE INDEX autofill_name_value_lower ON "
2242 "autofill (name, value_lower)")) { 2238 "autofill (name, value_lower)")) {
2243 return false; 2239 return false;
2244 } 2240 }
2245 2241
2246
2247 return transaction.Commit(); 2242 return transaction.Commit();
2248 } 2243 }
2249 2244
2250 bool AutofillTable::MigrateToVersion56AddProfileLanguageCodeForFormatting() { 2245 bool AutofillTable::MigrateToVersion56AddProfileLanguageCodeForFormatting() {
2251 return db_->Execute("ALTER TABLE autofill_profiles " 2246 return db_->Execute(
2252 "ADD COLUMN language_code VARCHAR"); 2247 "ALTER TABLE autofill_profiles "
2248 "ADD COLUMN language_code VARCHAR");
Peter Kasting 2017/04/26 01:27:28 Nit: This all fits on one line now (two places)
please use gerrit instead 2017/04/26 16:34:15 Done.
2253 } 2249 }
2254 2250
2255 bool AutofillTable::MigrateToVersion57AddFullNameField() { 2251 bool AutofillTable::MigrateToVersion57AddFullNameField() {
2256 return db_->Execute("ALTER TABLE autofill_profile_names " 2252 return db_->Execute(
2257 "ADD COLUMN full_name VARCHAR"); 2253 "ALTER TABLE autofill_profile_names "
2254 "ADD COLUMN full_name VARCHAR");
2258 } 2255 }
2259 2256
2260 bool AutofillTable::MigrateToVersion60AddServerCards() { 2257 bool AutofillTable::MigrateToVersion60AddServerCards() {
2261 sql::Transaction transaction(db_); 2258 sql::Transaction transaction(db_);
2262 if (!transaction.Begin()) 2259 if (!transaction.Begin())
2263 return false; 2260 return false;
2264 2261
2265 if (!db_->DoesTableExist("masked_credit_cards") && 2262 if (!db_->DoesTableExist("masked_credit_cards") &&
2266 !db_->Execute("CREATE TABLE masked_credit_cards (" 2263 !db_->Execute("CREATE TABLE masked_credit_cards ("
2267 "id VARCHAR," 2264 "id VARCHAR,"
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
2411 !db_->Execute("CREATE TABLE server_address_metadata (" 2408 !db_->Execute("CREATE TABLE server_address_metadata ("
2412 "id VARCHAR NOT NULL," 2409 "id VARCHAR NOT NULL,"
2413 "use_count INTEGER NOT NULL DEFAULT 0, " 2410 "use_count INTEGER NOT NULL DEFAULT 0, "
2414 "use_date INTEGER NOT NULL DEFAULT 0)")) { 2411 "use_date INTEGER NOT NULL DEFAULT 0)")) {
2415 return false; 2412 return false;
2416 } 2413 }
2417 2414
2418 // Get existing server addresses and generate IDs for them. 2415 // Get existing server addresses and generate IDs for them.
2419 sql::Statement s(db_->GetUniqueStatement( 2416 sql::Statement s(db_->GetUniqueStatement(
2420 "SELECT " 2417 "SELECT "
2421 "id," 2418 "id,"
2422 "recipient_name," 2419 "recipient_name,"
2423 "company_name," 2420 "company_name,"
2424 "street_address," 2421 "street_address,"
2425 "address_1," // ADDRESS_HOME_STATE 2422 "address_1," // ADDRESS_HOME_STATE
2426 "address_2," // ADDRESS_HOME_CITY 2423 "address_2," // ADDRESS_HOME_CITY
2427 "address_3," // ADDRESS_HOME_DEPENDENT_LOCALITY 2424 "address_3," // ADDRESS_HOME_DEPENDENT_LOCALITY
2428 "address_4," // Not supported in AutofillProfile yet. 2425 "address_4," // Not supported in AutofillProfile yet.
2429 "postal_code," // ADDRESS_HOME_ZIP 2426 "postal_code," // ADDRESS_HOME_ZIP
2430 "sorting_code," // ADDRESS_HOME_SORTING_CODE 2427 "sorting_code," // ADDRESS_HOME_SORTING_CODE
2431 "country_code," // ADDRESS_HOME_COUNTRY 2428 "country_code," // ADDRESS_HOME_COUNTRY
2432 "phone_number," // PHONE_HOME_WHOLE_NUMBER 2429 "phone_number," // PHONE_HOME_WHOLE_NUMBER
2433 "language_code " 2430 "language_code "
2434 "FROM server_addresses addresses")); 2431 "FROM server_addresses addresses"));
2435 std::vector<AutofillProfile> profiles; 2432 std::vector<AutofillProfile> profiles;
2436 while (s.Step()) { 2433 while (s.Step()) {
2437 int index = 0; 2434 int index = 0;
2438 AutofillProfile profile( 2435 AutofillProfile profile(AutofillProfile::SERVER_PROFILE,
2439 AutofillProfile::SERVER_PROFILE, s.ColumnString(index++)); 2436 s.ColumnString(index++));
2440 2437
2441 base::string16 recipient_name = s.ColumnString16(index++); 2438 base::string16 recipient_name = s.ColumnString16(index++);
2442 profile.SetRawInfo(COMPANY_NAME, s.ColumnString16(index++)); 2439 profile.SetRawInfo(COMPANY_NAME, s.ColumnString16(index++));
2443 profile.SetRawInfo(ADDRESS_HOME_STREET_ADDRESS, s.ColumnString16(index++)); 2440 profile.SetRawInfo(ADDRESS_HOME_STREET_ADDRESS, s.ColumnString16(index++));
2444 profile.SetRawInfo(ADDRESS_HOME_STATE, s.ColumnString16(index++)); 2441 profile.SetRawInfo(ADDRESS_HOME_STATE, s.ColumnString16(index++));
2445 profile.SetRawInfo(ADDRESS_HOME_CITY, s.ColumnString16(index++)); 2442 profile.SetRawInfo(ADDRESS_HOME_CITY, s.ColumnString16(index++));
2446 profile.SetRawInfo(ADDRESS_HOME_DEPENDENT_LOCALITY, 2443 profile.SetRawInfo(ADDRESS_HOME_DEPENDENT_LOCALITY,
2447 s.ColumnString16(index++)); 2444 s.ColumnString16(index++));
2448 index++; // Skip address_4 which we haven't added to AutofillProfile yet. 2445 index++; // Skip address_4 which we haven't added to AutofillProfile yet.
2449 profile.SetRawInfo(ADDRESS_HOME_ZIP, s.ColumnString16(index++)); 2446 profile.SetRawInfo(ADDRESS_HOME_ZIP, s.ColumnString16(index++));
2450 profile.SetRawInfo(ADDRESS_HOME_SORTING_CODE, s.ColumnString16(index++)); 2447 profile.SetRawInfo(ADDRESS_HOME_SORTING_CODE, s.ColumnString16(index++));
2451 profile.SetRawInfo(ADDRESS_HOME_COUNTRY, s.ColumnString16(index++)); 2448 profile.SetRawInfo(ADDRESS_HOME_COUNTRY, s.ColumnString16(index++));
2452 base::string16 phone_number = s.ColumnString16(index++); 2449 base::string16 phone_number = s.ColumnString16(index++);
2453 profile.set_language_code(s.ColumnString(index++)); 2450 profile.set_language_code(s.ColumnString(index++));
2454 profile.SetInfo(AutofillType(NAME_FULL), recipient_name, 2451 profile.SetInfo(AutofillType(NAME_FULL), recipient_name,
2455 profile.language_code()); 2452 profile.language_code());
2456 profile.SetInfo(AutofillType(PHONE_HOME_WHOLE_NUMBER), phone_number, 2453 profile.SetInfo(AutofillType(PHONE_HOME_WHOLE_NUMBER), phone_number,
2457 profile.language_code()); 2454 profile.language_code());
2458 profile.GenerateServerProfileIdentifier(); 2455 profile.GenerateServerProfileIdentifier();
2459 profiles.push_back(profile); 2456 profiles.push_back(profile);
2460 } 2457 }
2461 2458
2462 // Reinsert with the generated IDs. 2459 // Reinsert with the generated IDs.
2463 sql::Statement delete_old(db_->GetUniqueStatement( 2460 sql::Statement delete_old(
2464 "DELETE FROM server_addresses")); 2461 db_->GetUniqueStatement("DELETE FROM server_addresses"));
2465 delete_old.Run(); 2462 delete_old.Run();
2466 2463
2467 sql::Statement insert(db_->GetUniqueStatement( 2464 sql::Statement insert(db_->GetUniqueStatement(
2468 "INSERT INTO server_addresses(" 2465 "INSERT INTO server_addresses("
2469 "id," 2466 "id,"
2470 "recipient_name," 2467 "recipient_name,"
2471 "company_name," 2468 "company_name,"
2472 "street_address," 2469 "street_address,"
2473 "address_1," // ADDRESS_HOME_STATE 2470 "address_1," // ADDRESS_HOME_STATE
2474 "address_2," // ADDRESS_HOME_CITY 2471 "address_2," // ADDRESS_HOME_CITY
2475 "address_3," // ADDRESS_HOME_DEPENDENT_LOCALITY 2472 "address_3," // ADDRESS_HOME_DEPENDENT_LOCALITY
2476 "address_4," // Not supported in AutofillProfile yet. 2473 "address_4," // Not supported in AutofillProfile yet.
2477 "postal_code," // ADDRESS_HOME_ZIP 2474 "postal_code," // ADDRESS_HOME_ZIP
2478 "sorting_code," // ADDRESS_HOME_SORTING_CODE 2475 "sorting_code," // ADDRESS_HOME_SORTING_CODE
2479 "country_code," // ADDRESS_HOME_COUNTRY 2476 "country_code," // ADDRESS_HOME_COUNTRY
2480 "phone_number," // PHONE_HOME_WHOLE_NUMBER 2477 "phone_number," // PHONE_HOME_WHOLE_NUMBER
2481 "language_code) " 2478 "language_code) "
2482 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)")); 2479 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)"));
2483 for (const AutofillProfile& profile : profiles) { 2480 for (const AutofillProfile& profile : profiles) {
2484 int index = 0; 2481 int index = 0;
2485 insert.BindString(index++, profile.server_id()); 2482 insert.BindString(index++, profile.server_id());
2486 insert.BindString16(index++, profile.GetRawInfo(NAME_FULL)); 2483 insert.BindString16(index++, profile.GetRawInfo(NAME_FULL));
2487 insert.BindString16(index++, profile.GetRawInfo(COMPANY_NAME)); 2484 insert.BindString16(index++, profile.GetRawInfo(COMPANY_NAME));
2488 insert.BindString16(index++, 2485 insert.BindString16(index++,
2489 profile.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS)); 2486 profile.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS));
2490 insert.BindString16(index++, profile.GetRawInfo(ADDRESS_HOME_STATE)); 2487 insert.BindString16(index++, profile.GetRawInfo(ADDRESS_HOME_STATE));
2491 insert.BindString16(index++, profile.GetRawInfo(ADDRESS_HOME_CITY)); 2488 insert.BindString16(index++, profile.GetRawInfo(ADDRESS_HOME_CITY));
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
2584 // temporary table. 2581 // temporary table.
2585 if (!db_->Execute("DROP TABLE masked_credit_cards") || 2582 if (!db_->Execute("DROP TABLE masked_credit_cards") ||
2586 !db_->Execute("ALTER TABLE masked_credit_cards_temp " 2583 !db_->Execute("ALTER TABLE masked_credit_cards_temp "
2587 "RENAME TO masked_credit_cards")) { 2584 "RENAME TO masked_credit_cards")) {
2588 return false; 2585 return false;
2589 } 2586 }
2590 2587
2591 return transaction.Commit(); 2588 return transaction.Commit();
2592 } 2589 }
2593 2590
2591 bool AutofillTable::MigrateToVersion72RenameCardTypeToIssuerNetwork() {
2592 sql::Transaction transaction(db_);
2593 if (!transaction.Begin())
2594 return false;
2595
2596 if (db_->DoesTableExist("masked_credit_cards_temp") ||
Peter Kasting 2017/04/26 01:27:28 It seems like if this table exists, it will block
please use gerrit instead 2017/04/26 16:34:15 Good point. Fixed in both places.
2597 !db_->Execute("CREATE TABLE masked_credit_cards_temp ("
2598 "id VARCHAR,"
2599 "status VARCHAR,"
2600 "name_on_card VARCHAR,"
2601 "network VARCHAR,"
2602 "last_four VARCHAR,"
2603 "exp_month INTEGER DEFAULT 0,"
2604 "exp_year INTEGER DEFAULT 0)")) {
2605 return false;
2606 }
2607 if (!db_->Execute("INSERT INTO masked_credit_cards_temp "
2608 "(id, status, name_on_card, network, last_four, "
2609 "exp_month, exp_year) "
2610 "SELECT id, status, name_on_card, type, last_four, "
2611 "exp_month, exp_year "
2612 "FROM masked_credit_cards")) {
2613 return false;
2614 }
2615 if (!db_->Execute("DROP TABLE masked_credit_cards"))
2616 return false;
2617 if (!db_->Execute("ALTER TABLE masked_credit_cards_temp "
2618 "RENAME TO masked_credit_cards")) {
2619 return false;
2620 }
2621
2622 return transaction.Commit();
Peter Kasting 2017/04/26 01:27:28 Nit: Up to you, but you could also write this sort
please use gerrit instead 2017/04/26 16:34:15 Done.
2623 }
2624
2594 } // namespace autofill 2625 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698