 Chromium Code Reviews
 Chromium Code Reviews Issue 2550293002:
  [sync] Add autofill sync metadata to the web db  (Closed)
    
  
    Issue 2550293002:
  [sync] Add autofill sync metadata to the web db  (Closed) 
  | OLD | NEW | 
|---|---|
| 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 Loading... | |
| 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 Loading... | |
| 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 Loading... | |
| 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 Loading... | |
| 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( | |
| 
maxbogue
2016/12/09 00:18:13
As we discussed offline, please combine this and G
 
Patrick Noland
2016/12/10 00:39:35
Done.
 | |
| 1692 syncer::ModelType model_type, | |
| 1693 syncer::EntityMetadataMap* metadata_records) { | |
| 1694 DCHECK_EQ(model_type, syncer::AUTOFILL) | |
| 1695 << "Only the AUTOFILL model type is supported"; | |
| 1696 | |
| 1697 sql::Statement s(db_->GetUniqueStatement( | |
| 1698 "SELECT storage_key, value FROM autofill_sync_metadata")); | |
| 1699 | |
| 1700 while (s.Step()) { | |
| 1701 std::string storage_key = s.ColumnString(0); | |
| 1702 std::string serialized_metadata = s.ColumnString(1); | |
| 1703 sync_pb::EntityMetadata metadata_record; | |
| 1704 if (metadata_record.ParseFromString(serialized_metadata)) { | |
| 1705 metadata_records->insert(std::make_pair(storage_key, metadata_record)); | |
| 1706 } else { | |
| 1707 return false; | |
| 1708 } | |
| 1709 } | |
| 1710 return true; | |
| 1711 } | |
| 1712 | |
| 1713 bool AutofillTable::UpdateSyncMetadata( | |
| 1714 syncer::ModelType model_type, | |
| 1715 const std::string& storage_key, | |
| 1716 const sync_pb::EntityMetadata& metadata) { | |
| 1717 DCHECK_EQ(model_type, syncer::AUTOFILL) | |
| 1718 << "Only the AUTOFILL model type is supported"; | |
| 1719 | |
| 1720 sql::Statement s( | |
| 1721 db_->GetUniqueStatement("INSERT OR REPLACE INTO autofill_sync_metadata " | |
| 1722 "(storage_key, value) VALUES(?, ?)")); | |
| 1723 s.BindString(0, storage_key); | |
| 1724 s.BindString(1, metadata.SerializeAsString()); | |
| 1725 | |
| 1726 return s.Run(); | |
| 1727 } | |
| 1728 | |
| 1729 bool AutofillTable::ClearSyncMetadata(syncer::ModelType model_type, | |
| 1730 const std::string& storage_key) { | |
| 1731 DCHECK_EQ(model_type, syncer::AUTOFILL) | |
| 1732 << "Only the AUTOFILL model type is supported"; | |
| 1733 | |
| 1734 sql::Statement s(db_->GetUniqueStatement( | |
| 1735 "DELETE FROM autofill_sync_metadata WHERE storage_key=?")); | |
| 1736 s.BindString(0, storage_key); | |
| 1737 | |
| 1738 return s.Run(); | |
| 1739 } | |
| 1740 | |
| 1741 bool AutofillTable::GetModelTypeState(syncer::ModelType model_type, | |
| 1742 sync_pb::ModelTypeState* state) { | |
| 1743 DCHECK_EQ(model_type, syncer::AUTOFILL) | |
| 1744 << "Only the AUTOFILL model type is supported"; | |
| 1745 | |
| 1746 sql::Statement s(db_->GetUniqueStatement( | |
| 1747 "SELECT value FROM autofill_model_type_state WHERE rowid=1")); | |
| 1748 | |
| 1749 if (!s.Step()) { | |
| 1750 return false; | |
| 1751 } | |
| 1752 | |
| 1753 std::string serialized_state = s.ColumnString(0); | |
| 1754 return state->ParseFromString(serialized_state); | |
| 1755 } | |
| 1756 | |
| 1757 bool AutofillTable::UpdateModelTypeState( | |
| 1758 syncer::ModelType model_type, | |
| 1759 sync_pb::ModelTypeState& model_type_state) { | |
| 1760 DCHECK_EQ(model_type, syncer::AUTOFILL) | |
| 1761 << "Only the AUTOFILL model type is supported"; | |
| 1762 | |
| 1763 // Hardcode the rowid to force a collision, ensuring that there remains only a | |
| 1764 // single entry. | |
| 1765 sql::Statement s(db_->GetUniqueStatement( | |
| 1766 "INSERT OR REPLACE INTO autofill_model_type_state (rowid, value) " | |
| 1767 "VALUES(1,?)")); | |
| 1768 s.BindString(0, model_type_state.SerializeAsString()); | |
| 1769 | |
| 1770 return s.Run(); | |
| 1771 } | |
| 1772 | |
| 1773 bool AutofillTable::ClearModelTypeState(syncer::ModelType model_type) { | |
| 1774 DCHECK_EQ(model_type, syncer::AUTOFILL) | |
| 1775 << "Only the AUTOFILL model type is supported"; | |
| 1776 | |
| 1777 sql::Statement s(db_->GetUniqueStatement( | |
| 1778 "DELETE FROM autofill_model_type_state WHERE rowid=1")); | |
| 1779 | |
| 1780 return s.Run(); | |
| 1781 } | |
| 1782 | |
| 1683 bool AutofillTable::InitMainTable() { | 1783 bool AutofillTable::InitMainTable() { | 
| 1684 if (!db_->DoesTableExist("autofill")) { | 1784 if (!db_->DoesTableExist("autofill")) { | 
| 1685 if (!db_->Execute("CREATE TABLE autofill (" | 1785 if (!db_->Execute("CREATE TABLE autofill (" | 
| 1686 "name VARCHAR, " | 1786 "name VARCHAR, " | 
| 1687 "value VARCHAR, " | 1787 "value VARCHAR, " | 
| 1688 "value_lower VARCHAR, " | 1788 "value_lower VARCHAR, " | 
| 1689 "date_created INTEGER DEFAULT 0, " | 1789 "date_created INTEGER DEFAULT 0, " | 
| 1690 "date_last_used INTEGER DEFAULT 0, " | 1790 "date_last_used INTEGER DEFAULT 0, " | 
| 1691 "count INTEGER DEFAULT 1, " | 1791 "count INTEGER DEFAULT 1, " | 
| 1692 "PRIMARY KEY (name, value))") || | 1792 "PRIMARY KEY (name, value))") || | 
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1872 "id VARCHAR NOT NULL," | 1972 "id VARCHAR NOT NULL," | 
| 1873 "use_count INTEGER NOT NULL DEFAULT 0, " | 1973 "use_count INTEGER NOT NULL DEFAULT 0, " | 
| 1874 "use_date INTEGER NOT NULL DEFAULT 0)")) { | 1974 "use_date INTEGER NOT NULL DEFAULT 0)")) { | 
| 1875 NOTREACHED(); | 1975 NOTREACHED(); | 
| 1876 return false; | 1976 return false; | 
| 1877 } | 1977 } | 
| 1878 } | 1978 } | 
| 1879 return true; | 1979 return true; | 
| 1880 } | 1980 } | 
| 1881 | 1981 | 
| 1982 bool AutofillTable::InitAutofillSyncMetadataTable() { | |
| 1983 if (!db_->DoesTableExist("autofill_sync_metadata")) { | |
| 1984 if (!db_->Execute("CREATE TABLE autofill_sync_metadata (" | |
| 1985 "storage_key VARCHAR PRIMARY KEY NOT NULL," | |
| 1986 "value BLOB)")) { | |
| 1987 NOTREACHED(); | |
| 1988 return false; | |
| 1989 } | |
| 1990 } | |
| 1991 return true; | |
| 1992 } | |
| 1993 | |
| 1994 bool AutofillTable::InitModelTypeStateTable() { | |
| 1995 if (!db_->DoesTableExist("autofill_model_type_state")) { | |
| 1996 if (!db_->Execute("CREATE TABLE autofill_model_type_state (value BLOB)")) { | |
| 1997 NOTREACHED(); | |
| 1998 return false; | |
| 1999 } | |
| 2000 } | |
| 2001 return true; | |
| 2002 } | |
| 2003 | |
| 1882 bool AutofillTable::MigrateToVersion54AddI18nFieldsAndRemoveDeprecatedFields() { | 2004 bool AutofillTable::MigrateToVersion54AddI18nFieldsAndRemoveDeprecatedFields() { | 
| 1883 sql::Transaction transaction(db_); | 2005 sql::Transaction transaction(db_); | 
| 1884 if (!transaction.Begin()) | 2006 if (!transaction.Begin()) | 
| 1885 return false; | 2007 return false; | 
| 1886 | 2008 | 
| 1887 // Test the existence of the |address_line_1| column as an indication that a | 2009 // 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| | 2010 // 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 | 2011 // schema is in place because the table was newly created when migrating from | 
| 1890 // a pre-version-23 database. | 2012 // a pre-version-23 database. | 
| 1891 if (db_->DoesColumnExist("autofill_profiles", "address_line_1")) { | 2013 if (db_->DoesColumnExist("autofill_profiles", "address_line_1")) { | 
| (...skipping 416 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2308 "ALTER TABLE credit_cards ADD COLUMN billing_address_id VARCHAR"); | 2430 "ALTER TABLE credit_cards ADD COLUMN billing_address_id VARCHAR"); | 
| 2309 } | 2431 } | 
| 2310 | 2432 | 
| 2311 bool AutofillTable::MigrateToVersion67AddMaskedCardBillingAddress() { | 2433 bool AutofillTable::MigrateToVersion67AddMaskedCardBillingAddress() { | 
| 2312 // The default value for this column is null, but Connection::ColumnString() | 2434 // The default value for this column is null, but Connection::ColumnString() | 
| 2313 // returns an empty string for that. | 2435 // returns an empty string for that. | 
| 2314 return db_->Execute( | 2436 return db_->Execute( | 
| 2315 "ALTER TABLE masked_credit_cards ADD COLUMN billing_address_id VARCHAR"); | 2437 "ALTER TABLE masked_credit_cards ADD COLUMN billing_address_id VARCHAR"); | 
| 2316 } | 2438 } | 
| 2317 | 2439 | 
| 2318 } // namespace autofill | 2440 bool AutofillTable::MigrateToVersion70AddSyncMetadata() { | 
| 2441 if (!db_->Execute("CREATE TABLE autofill_sync_metadata (" | |
| 2442 "storage_key VARCHAR PRIMARY KEY NOT NULL," | |
| 2443 "value BLOB)")) { | |
| 2444 return false; | |
| 2445 } | |
| 2446 return db_->Execute("CREATE TABLE autofill_model_type_state (value BLOB)"); | |
| 2447 } | |
| 2448 | |
| 2449 } // namespace autofill | |
| OLD | NEW |