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

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

Issue 2550293002: [sync] Add autofill sync metadata to the web db (Closed)
Patch Set: Return a metadata batch; use id instead of rowid Created 4 years 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 17 matching lines...) Expand all
28 #include "components/autofill/core/browser/autofill_profile.h" 28 #include "components/autofill/core/browser/autofill_profile.h"
29 #include "components/autofill/core/browser/autofill_type.h" 29 #include "components/autofill/core/browser/autofill_type.h"
30 #include "components/autofill/core/browser/credit_card.h" 30 #include "components/autofill/core/browser/credit_card.h"
31 #include "components/autofill/core/browser/personal_data_manager.h" 31 #include "components/autofill/core/browser/personal_data_manager.h"
32 #include "components/autofill/core/browser/webdata/autofill_change.h" 32 #include "components/autofill/core/browser/webdata/autofill_change.h"
33 #include "components/autofill/core/browser/webdata/autofill_entry.h" 33 #include "components/autofill/core/browser/webdata/autofill_entry.h"
34 #include "components/autofill/core/common/autofill_switches.h" 34 #include "components/autofill/core/common/autofill_switches.h"
35 #include "components/autofill/core/common/autofill_util.h" 35 #include "components/autofill/core/common/autofill_util.h"
36 #include "components/autofill/core/common/form_field_data.h" 36 #include "components/autofill/core/common/form_field_data.h"
37 #include "components/os_crypt/os_crypt.h" 37 #include "components/os_crypt/os_crypt.h"
38 #include "components/sync/base/model_type.h"
39 #include "components/sync/model/metadata_batch.h"
40 #include "components/sync/protocol/entity_metadata.pb.h"
41 #include "components/sync/protocol/model_type_state.pb.h"
38 #include "components/webdata/common/web_database.h" 42 #include "components/webdata/common/web_database.h"
39 #include "sql/statement.h" 43 #include "sql/statement.h"
40 #include "sql/transaction.h" 44 #include "sql/transaction.h"
41 #include "ui/base/l10n/l10n_util.h" 45 #include "ui/base/l10n/l10n_util.h"
42 #include "url/gurl.h" 46 #include "url/gurl.h"
43 47
44 using base::ASCIIToUTF16; 48 using base::ASCIIToUTF16;
45 using base::Time; 49 using base::Time;
46 using base::TimeDelta; 50 using base::TimeDelta;
47 51
(...skipping 361 matching lines...) Expand 10 before | Expand all | Expand 10 after
409 WebDatabaseTable::TypeKey AutofillTable::GetTypeKey() const { 413 WebDatabaseTable::TypeKey AutofillTable::GetTypeKey() const {
410 return GetKey(); 414 return GetKey();
411 } 415 }
412 416
413 bool AutofillTable::CreateTablesIfNecessary() { 417 bool AutofillTable::CreateTablesIfNecessary() {
414 return (InitMainTable() && InitCreditCardsTable() && InitProfilesTable() && 418 return (InitMainTable() && InitCreditCardsTable() && InitProfilesTable() &&
415 InitProfileNamesTable() && InitProfileEmailsTable() && 419 InitProfileNamesTable() && InitProfileEmailsTable() &&
416 InitProfilePhonesTable() && InitProfileTrashTable() && 420 InitProfilePhonesTable() && InitProfileTrashTable() &&
417 InitMaskedCreditCardsTable() && InitUnmaskedCreditCardsTable() && 421 InitMaskedCreditCardsTable() && InitUnmaskedCreditCardsTable() &&
418 InitServerCardMetadataTable() && InitServerAddressesTable() && 422 InitServerCardMetadataTable() && InitServerAddressesTable() &&
419 InitServerAddressMetadataTable()); 423 InitServerAddressMetadataTable() && InitAutofillSyncMetadataTable() &&
424 InitModelTypeStateTable());
420 } 425 }
421 426
422 bool AutofillTable::IsSyncable() { 427 bool AutofillTable::IsSyncable() {
423 return true; 428 return true;
424 } 429 }
425 430
426 bool AutofillTable::MigrateToVersion(int version, 431 bool AutofillTable::MigrateToVersion(int version,
427 bool* update_compatible_version) { 432 bool* update_compatible_version) {
428 // Migrate if necessary. 433 // Migrate if necessary.
429 switch (version) { 434 switch (version) {
(...skipping 26 matching lines...) Expand all
456 return MigrateToVersion64AddUnmaskDate(); 461 return MigrateToVersion64AddUnmaskDate();
457 case 65: 462 case 65:
458 *update_compatible_version = false; 463 *update_compatible_version = false;
459 return MigrateToVersion65AddServerMetadataTables(); 464 return MigrateToVersion65AddServerMetadataTables();
460 case 66: 465 case 66:
461 *update_compatible_version = false; 466 *update_compatible_version = false;
462 return MigrateToVersion66AddCardBillingAddress(); 467 return MigrateToVersion66AddCardBillingAddress();
463 case 67: 468 case 67:
464 *update_compatible_version = false; 469 *update_compatible_version = false;
465 return MigrateToVersion67AddMaskedCardBillingAddress(); 470 return MigrateToVersion67AddMaskedCardBillingAddress();
471 case 70:
472 *update_compatible_version = false;
473 return MigrateToVersion70AddSyncMetadata();
466 } 474 }
467 return true; 475 return true;
468 } 476 }
469 477
470 bool AutofillTable::AddFormFieldValues( 478 bool AutofillTable::AddFormFieldValues(
471 const std::vector<FormFieldData>& elements, 479 const std::vector<FormFieldData>& elements,
472 std::vector<AutofillChange>* changes) { 480 std::vector<AutofillChange>* changes) {
473 return AddFormFieldValuesTime(elements, changes, Time::Now()); 481 return AddFormFieldValuesTime(elements, changes, Time::Now());
474 } 482 }
475 483
(...skipping 1197 matching lines...) Expand 10 before | Expand all | Expand 10 after
1673 bool AutofillTable::IsAutofillGUIDInTrash(const std::string& guid) { 1681 bool AutofillTable::IsAutofillGUIDInTrash(const std::string& guid) {
1674 sql::Statement s(db_->GetUniqueStatement( 1682 sql::Statement s(db_->GetUniqueStatement(
1675 "SELECT guid " 1683 "SELECT guid "
1676 "FROM autofill_profiles_trash " 1684 "FROM autofill_profiles_trash "
1677 "WHERE guid = ?")); 1685 "WHERE guid = ?"));
1678 s.BindString(0, guid); 1686 s.BindString(0, guid);
1679 1687
1680 return s.Step(); 1688 return s.Step();
1681 } 1689 }
1682 1690
1691 bool AutofillTable::GetAllSyncMetadata(syncer::ModelType model_type,
1692 syncer::MetadataBatch* metadata_batch) {
1693 DCHECK_EQ(model_type, syncer::AUTOFILL)
1694 << "Only the AUTOFILL model type is supported";
1695 syncer::EntityMetadataMap metadata_records;
1696 if (GetAllSyncEntityMetadata(model_type, &metadata_records)) {
1697 for (const auto& pair : metadata_records) {
1698 // todo(pnoland): add batch transfer of metadata map
1699 metadata_batch->AddMetadata(pair.first, pair.second);
1700 }
1701 } else {
1702 return false;
1703 }
1704
1705 sync_pb::ModelTypeState model_type_state;
1706 if (GetModelTypeState(model_type, &model_type_state)) {
1707 metadata_batch->SetModelTypeState(model_type_state);
1708 } else {
1709 return false;
1710 }
1711
1712 return true;
1713 }
1714
1715 bool AutofillTable::GetAllSyncEntityMetadata(
1716 syncer::ModelType model_type,
1717 syncer::EntityMetadataMap* metadata_records) {
1718 DCHECK_EQ(model_type, syncer::AUTOFILL)
1719 << "Only the AUTOFILL model type is supported";
1720
1721 sql::Statement s(db_->GetUniqueStatement(
1722 "SELECT storage_key, value FROM autofill_sync_metadata"));
1723
1724 while (s.Step()) {
1725 std::string storage_key = s.ColumnString(0);
1726 std::string serialized_metadata = s.ColumnString(1);
1727 sync_pb::EntityMetadata metadata_record;
1728 if (metadata_record.ParseFromString(serialized_metadata)) {
1729 metadata_records->insert(std::make_pair(storage_key, metadata_record));
1730 } else {
1731 return false;
1732 }
1733 }
1734 return true;
1735 }
1736
1737 bool AutofillTable::UpdateSyncMetadata(
1738 syncer::ModelType model_type,
1739 const std::string& storage_key,
1740 const sync_pb::EntityMetadata& metadata) {
1741 DCHECK_EQ(model_type, syncer::AUTOFILL)
1742 << "Only the AUTOFILL model type is supported";
1743
1744 sql::Statement s(
1745 db_->GetUniqueStatement("INSERT OR REPLACE INTO autofill_sync_metadata "
1746 "(storage_key, value) VALUES(?, ?)"));
1747 s.BindString(0, storage_key);
1748 s.BindString(1, metadata.SerializeAsString());
1749
1750 return s.Run();
1751 }
1752
1753 bool AutofillTable::ClearSyncMetadata(syncer::ModelType model_type,
1754 const std::string& storage_key) {
1755 DCHECK_EQ(model_type, syncer::AUTOFILL)
1756 << "Only the AUTOFILL model type is supported";
1757
1758 sql::Statement s(db_->GetUniqueStatement(
1759 "DELETE FROM autofill_sync_metadata WHERE storage_key=?"));
1760 s.BindString(0, storage_key);
1761
1762 return s.Run();
1763 }
1764
1765 bool AutofillTable::GetModelTypeState(syncer::ModelType model_type,
1766 sync_pb::ModelTypeState* state) {
1767 DCHECK_EQ(model_type, syncer::AUTOFILL)
1768 << "Only the AUTOFILL model type is supported";
1769
1770 sql::Statement s(db_->GetUniqueStatement(
1771 "SELECT value FROM autofill_model_type_state WHERE id=1"));
1772
1773 if (!s.Step()) {
1774 return false;
1775 }
1776
1777 std::string serialized_state = s.ColumnString(0);
1778 return state->ParseFromString(serialized_state);
1779 }
1780
1781 bool AutofillTable::UpdateModelTypeState(
1782 syncer::ModelType model_type,
1783 const sync_pb::ModelTypeState& model_type_state) {
1784 DCHECK_EQ(model_type, syncer::AUTOFILL)
1785 << "Only the AUTOFILL model type is supported";
1786
1787 // Hardcode the id to force a collision, ensuring that there remains only a
1788 // single entry.
1789 sql::Statement s(db_->GetUniqueStatement(
1790 "INSERT OR REPLACE INTO autofill_model_type_state (id, value) "
1791 "VALUES(1,?)"));
1792 s.BindString(0, model_type_state.SerializeAsString());
1793
1794 return s.Run();
1795 }
1796
1797 bool AutofillTable::ClearModelTypeState(syncer::ModelType model_type) {
1798 DCHECK_EQ(model_type, syncer::AUTOFILL)
1799 << "Only the AUTOFILL model type is supported";
1800
1801 sql::Statement s(db_->GetUniqueStatement(
1802 "DELETE FROM autofill_model_type_state WHERE id=1"));
1803
1804 return s.Run();
1805 }
1806
1683 bool AutofillTable::InitMainTable() { 1807 bool AutofillTable::InitMainTable() {
1684 if (!db_->DoesTableExist("autofill")) { 1808 if (!db_->DoesTableExist("autofill")) {
1685 if (!db_->Execute("CREATE TABLE autofill (" 1809 if (!db_->Execute("CREATE TABLE autofill ("
1686 "name VARCHAR, " 1810 "name VARCHAR, "
1687 "value VARCHAR, " 1811 "value VARCHAR, "
1688 "value_lower VARCHAR, " 1812 "value_lower VARCHAR, "
1689 "date_created INTEGER DEFAULT 0, " 1813 "date_created INTEGER DEFAULT 0, "
1690 "date_last_used INTEGER DEFAULT 0, " 1814 "date_last_used INTEGER DEFAULT 0, "
1691 "count INTEGER DEFAULT 1, " 1815 "count INTEGER DEFAULT 1, "
1692 "PRIMARY KEY (name, value))") || 1816 "PRIMARY KEY (name, value))") ||
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
1872 "id VARCHAR NOT NULL," 1996 "id VARCHAR NOT NULL,"
1873 "use_count INTEGER NOT NULL DEFAULT 0, " 1997 "use_count INTEGER NOT NULL DEFAULT 0, "
1874 "use_date INTEGER NOT NULL DEFAULT 0)")) { 1998 "use_date INTEGER NOT NULL DEFAULT 0)")) {
1875 NOTREACHED(); 1999 NOTREACHED();
1876 return false; 2000 return false;
1877 } 2001 }
1878 } 2002 }
1879 return true; 2003 return true;
1880 } 2004 }
1881 2005
2006 bool AutofillTable::InitAutofillSyncMetadataTable() {
2007 if (!db_->DoesTableExist("autofill_sync_metadata")) {
2008 if (!db_->Execute("CREATE TABLE autofill_sync_metadata ("
2009 "storage_key VARCHAR PRIMARY KEY NOT NULL,"
2010 "value BLOB)")) {
2011 NOTREACHED();
2012 return false;
2013 }
2014 }
2015 return true;
2016 }
2017
2018 bool AutofillTable::InitModelTypeStateTable() {
2019 if (!db_->DoesTableExist("autofill_model_type_state")) {
2020 if (!db_->Execute("CREATE TABLE autofill_model_type_state (id INTEGER "
2021 "PRIMARY KEY, value BLOB)")) {
2022 NOTREACHED();
2023 return false;
2024 }
2025 }
2026 return true;
2027 }
2028
1882 bool AutofillTable::MigrateToVersion54AddI18nFieldsAndRemoveDeprecatedFields() { 2029 bool AutofillTable::MigrateToVersion54AddI18nFieldsAndRemoveDeprecatedFields() {
1883 sql::Transaction transaction(db_); 2030 sql::Transaction transaction(db_);
1884 if (!transaction.Begin()) 2031 if (!transaction.Begin())
1885 return false; 2032 return false;
1886 2033
1887 // Test the existence of the |address_line_1| column as an indication that a 2034 // Test the existence of the |address_line_1| column as an indication that a
1888 // migration is needed. It is possible that the new |autofill_profile_phones| 2035 // migration is needed. It is possible that the new |autofill_profile_phones|
1889 // schema is in place because the table was newly created when migrating from 2036 // schema is in place because the table was newly created when migrating from
1890 // a pre-version-23 database. 2037 // a pre-version-23 database.
1891 if (db_->DoesColumnExist("autofill_profiles", "address_line_1")) { 2038 if (db_->DoesColumnExist("autofill_profiles", "address_line_1")) {
(...skipping 416 matching lines...) Expand 10 before | Expand all | Expand 10 after
2308 "ALTER TABLE credit_cards ADD COLUMN billing_address_id VARCHAR"); 2455 "ALTER TABLE credit_cards ADD COLUMN billing_address_id VARCHAR");
2309 } 2456 }
2310 2457
2311 bool AutofillTable::MigrateToVersion67AddMaskedCardBillingAddress() { 2458 bool AutofillTable::MigrateToVersion67AddMaskedCardBillingAddress() {
2312 // The default value for this column is null, but Connection::ColumnString() 2459 // The default value for this column is null, but Connection::ColumnString()
2313 // returns an empty string for that. 2460 // returns an empty string for that.
2314 return db_->Execute( 2461 return db_->Execute(
2315 "ALTER TABLE masked_credit_cards ADD COLUMN billing_address_id VARCHAR"); 2462 "ALTER TABLE masked_credit_cards ADD COLUMN billing_address_id VARCHAR");
2316 } 2463 }
2317 2464
2318 } // namespace autofill 2465 bool AutofillTable::MigrateToVersion70AddSyncMetadata() {
2466 if (!db_->Execute("CREATE TABLE autofill_sync_metadata ("
2467 "storage_key VARCHAR PRIMARY KEY NOT NULL,"
2468 "value BLOB)")) {
2469 return false;
2470 }
2471 return db_->Execute(
2472 "CREATE TABLE autofill_model_type_state (id INTEGER PRIMARY KEY, value "
2473 "BLOB)");
2474 }
2475
2476 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698