 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/protocol/entity_metadata.pb.h" | |
| 40 #include "components/sync/protocol/model_type_state.pb.h" | |
| 38 #include "components/webdata/common/web_database.h" | 41 #include "components/webdata/common/web_database.h" | 
| 39 #include "sql/statement.h" | 42 #include "sql/statement.h" | 
| 40 #include "sql/transaction.h" | 43 #include "sql/transaction.h" | 
| 41 #include "ui/base/l10n/l10n_util.h" | 44 #include "ui/base/l10n/l10n_util.h" | 
| 42 #include "url/gurl.h" | 45 #include "url/gurl.h" | 
| 43 | 46 | 
| 44 using base::ASCIIToUTF16; | 47 using base::ASCIIToUTF16; | 
| 45 using base::Time; | 48 using base::Time; | 
| 46 using base::TimeDelta; | 49 using base::TimeDelta; | 
| 47 | 50 | 
| (...skipping 361 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 409 WebDatabaseTable::TypeKey AutofillTable::GetTypeKey() const { | 412 WebDatabaseTable::TypeKey AutofillTable::GetTypeKey() const { | 
| 410 return GetKey(); | 413 return GetKey(); | 
| 411 } | 414 } | 
| 412 | 415 | 
| 413 bool AutofillTable::CreateTablesIfNecessary() { | 416 bool AutofillTable::CreateTablesIfNecessary() { | 
| 414 return (InitMainTable() && InitCreditCardsTable() && InitProfilesTable() && | 417 return (InitMainTable() && InitCreditCardsTable() && InitProfilesTable() && | 
| 415 InitProfileNamesTable() && InitProfileEmailsTable() && | 418 InitProfileNamesTable() && InitProfileEmailsTable() && | 
| 416 InitProfilePhonesTable() && InitProfileTrashTable() && | 419 InitProfilePhonesTable() && InitProfileTrashTable() && | 
| 417 InitMaskedCreditCardsTable() && InitUnmaskedCreditCardsTable() && | 420 InitMaskedCreditCardsTable() && InitUnmaskedCreditCardsTable() && | 
| 418 InitServerCardMetadataTable() && InitServerAddressesTable() && | 421 InitServerCardMetadataTable() && InitServerAddressesTable() && | 
| 419 InitServerAddressMetadataTable()); | 422 InitServerAddressMetadataTable() && InitAutofillSyncMetadataTable() && | 
| 423 InitModelTypeStateTable()); | |
| 420 } | 424 } | 
| 421 | 425 | 
| 422 bool AutofillTable::IsSyncable() { | 426 bool AutofillTable::IsSyncable() { | 
| 423 return true; | 427 return true; | 
| 424 } | 428 } | 
| 425 | 429 | 
| 426 bool AutofillTable::MigrateToVersion(int version, | 430 bool AutofillTable::MigrateToVersion(int version, | 
| 427 bool* update_compatible_version) { | 431 bool* update_compatible_version) { | 
| 428 // Migrate if necessary. | 432 // Migrate if necessary. | 
| 429 switch (version) { | 433 switch (version) { | 
| (...skipping 26 matching lines...) Expand all Loading... | |
| 456 return MigrateToVersion64AddUnmaskDate(); | 460 return MigrateToVersion64AddUnmaskDate(); | 
| 457 case 65: | 461 case 65: | 
| 458 *update_compatible_version = false; | 462 *update_compatible_version = false; | 
| 459 return MigrateToVersion65AddServerMetadataTables(); | 463 return MigrateToVersion65AddServerMetadataTables(); | 
| 460 case 66: | 464 case 66: | 
| 461 *update_compatible_version = false; | 465 *update_compatible_version = false; | 
| 462 return MigrateToVersion66AddCardBillingAddress(); | 466 return MigrateToVersion66AddCardBillingAddress(); | 
| 463 case 67: | 467 case 67: | 
| 464 *update_compatible_version = false; | 468 *update_compatible_version = false; | 
| 465 return MigrateToVersion67AddMaskedCardBillingAddress(); | 469 return MigrateToVersion67AddMaskedCardBillingAddress(); | 
| 470 case 70: | |
| 471 *update_compatible_version = false; | |
| 472 return MigrateToVersion70AddSyncMetadata(); | |
| 466 } | 473 } | 
| 467 return true; | 474 return true; | 
| 468 } | 475 } | 
| 469 | 476 | 
| 470 bool AutofillTable::AddFormFieldValues( | 477 bool AutofillTable::AddFormFieldValues( | 
| 471 const std::vector<FormFieldData>& elements, | 478 const std::vector<FormFieldData>& elements, | 
| 472 std::vector<AutofillChange>* changes) { | 479 std::vector<AutofillChange>* changes) { | 
| 473 return AddFormFieldValuesTime(elements, changes, Time::Now()); | 480 return AddFormFieldValuesTime(elements, changes, Time::Now()); | 
| 474 } | 481 } | 
| 475 | 482 | 
| (...skipping 1197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1673 bool AutofillTable::IsAutofillGUIDInTrash(const std::string& guid) { | 1680 bool AutofillTable::IsAutofillGUIDInTrash(const std::string& guid) { | 
| 1674 sql::Statement s(db_->GetUniqueStatement( | 1681 sql::Statement s(db_->GetUniqueStatement( | 
| 1675 "SELECT guid " | 1682 "SELECT guid " | 
| 1676 "FROM autofill_profiles_trash " | 1683 "FROM autofill_profiles_trash " | 
| 1677 "WHERE guid = ?")); | 1684 "WHERE guid = ?")); | 
| 1678 s.BindString(0, guid); | 1685 s.BindString(0, guid); | 
| 1679 | 1686 | 
| 1680 return s.Step(); | 1687 return s.Step(); | 
| 1681 } | 1688 } | 
| 1682 | 1689 | 
| 1690 bool AutofillTable::GetSyncMetadata(syncer::ModelType model_type, | |
| 1691 const std::string& storage_key, | |
| 1692 sync_pb::EntityMetadata* metadata) { | |
| 1693 DCHECK_EQ(model_type, syncer::AUTOFILL) | |
| 1694 << "Only the AUTOFILL model type is supported"; | |
| 1695 | |
| 1696 sql::Statement s(db_->GetUniqueStatement( | |
| 1697 "SELECT value FROM autofill_sync_metadata WHERE storage_key=?")); | |
| 1698 s.BindString(0, storage_key); | |
| 1699 | |
| 1700 if (!s.Step()) { | |
| 1701 return false; | |
| 1702 } | |
| 1703 | |
| 1704 std::string serialized_metadata = s.ColumnString(0); | |
| 1705 return metadata->ParseFromString(serialized_metadata); | |
| 1706 } | |
| 1707 | |
| 1708 bool AutofillTable::GetAllSyncMetadata( | |
| 1709 syncer::ModelType model_type, | |
| 1710 std::vector<sync_pb::EntityMetadata>* metadata_records) { | |
| 1711 DCHECK_EQ(model_type, syncer::AUTOFILL) | |
| 1712 << "Only the AUTOFILL model type is supported"; | |
| 1713 | |
| 1714 sql::Statement s( | |
| 1715 db_->GetUniqueStatement("SELECT value FROM autofill_sync_metadata")); | |
| 1716 | |
| 1717 while (s.Step()) { | |
| 1718 std::string serialized_metadata = s.ColumnString(0); | |
| 1719 sync_pb::EntityMetadata metadata_record; | |
| 1720 //todo(pnoland): account for unparseable entries. | |
| 1721 if (metadata_record.ParseFromString(serialized_metadata)) { | |
| 1722 metadata_records->push_back(metadata_record); | |
| 1723 } | |
| 1724 } | |
| 1725 return true; | |
| 1726 } | |
| 1727 | |
| 1728 bool AutofillTable::UpdateSyncMetadata( | |
| 1729 syncer::ModelType model_type, | |
| 1730 const std::string& storage_key, | |
| 1731 const sync_pb::EntityMetadata& metadata) { | |
| 1732 DCHECK_EQ(model_type, syncer::AUTOFILL) | |
| 1733 << "Only the AUTOFILL model type is supported"; | |
| 1734 | |
| 1735 sql::Statement s( | |
| 1736 db_->GetUniqueStatement("INSERT OR REPLACE INTO autofill_sync_metadata " | |
| 1737 "(storage_key, value) VALUES(?, ?)")); | |
| 1738 s.BindString(0, storage_key); | |
| 1739 s.BindString(1, metadata.SerializeAsString()); | |
| 1740 | |
| 1741 return s.Run(); | |
| 1742 } | |
| 1743 | |
| 1744 bool AutofillTable::ClearSyncMetadata(syncer::ModelType model_type, | |
| 1745 const std::string& storage_key) { | |
| 1746 DCHECK_EQ(model_type, syncer::AUTOFILL) | |
| 1747 << "Only the AUTOFILL model type is supported"; | |
| 1748 | |
| 1749 sql::Statement s(db_->GetUniqueStatement( | |
| 1750 "DELETE FROM autofill_sync_metadata WHERE storage_key=?")); | |
| 1751 s.BindString(0, storage_key); | |
| 1752 | |
| 1753 return s.Run(); | |
| 1754 } | |
| 1755 | |
| 1756 bool AutofillTable::GetModelTypeState(syncer::ModelType model_type, | |
| 1757 sync_pb::ModelTypeState* state) { | |
| 1758 DCHECK_EQ(model_type, syncer::AUTOFILL) | |
| 1759 << "Only the AUTOFILL model type is supported"; | |
| 1760 | |
| 1761 sql::Statement s( | |
| 1762 db_->GetUniqueStatement("SELECT value FROM autofill_model_type_state")); | |
| 1763 | |
| 1764 if (!s.Step()) { | |
| 1765 return false; | |
| 1766 } | |
| 1767 | |
| 1768 std::string serialized_state = s.ColumnString(0); | |
| 1769 return state->ParseFromString(serialized_state); | |
| 1770 } | |
| 1771 | |
| 1772 bool AutofillTable::UpdateModelTypeState( | |
| 1773 syncer::ModelType model_type, | |
| 1774 sync_pb::ModelTypeState& model_type_state) { | |
| 1775 DCHECK_EQ(model_type, syncer::AUTOFILL) | |
| 1776 << "Only the AUTOFILL model type is supported"; | |
| 1777 | |
| 1778 // Hardcode the rowid to force a collision, ensuring that there remains only a | |
| 1779 // single entry. | |
| 1780 sql::Statement s(db_->GetUniqueStatement( | |
| 1781 "INSERT OR REPLACE INTO autofill_model_type_state (rowid, value) " | |
| 1782 "VALUES(1,?)")); | |
| 1783 s.BindString(0, model_type_state.SerializeAsString()); | |
| 1784 | |
| 1785 return s.Run(); | |
| 1786 } | |
| 1787 | |
| 1788 bool AutofillTable::ClearModelTypeState(syncer::ModelType model_type) { | |
| 1789 DCHECK_EQ(model_type, syncer::AUTOFILL) | |
| 1790 << "Only the AUTOFILL model type is supported"; | |
| 1791 | |
| 1792 sql::Statement s( | |
| 1793 db_->GetUniqueStatement("DELETE FROM autofill_model_type_state")); | |
| 1794 | |
| 1795 return s.Run(); | |
| 1796 } | |
| 1797 | |
| 1683 bool AutofillTable::InitMainTable() { | 1798 bool AutofillTable::InitMainTable() { | 
| 1684 if (!db_->DoesTableExist("autofill")) { | 1799 if (!db_->DoesTableExist("autofill")) { | 
| 1685 if (!db_->Execute("CREATE TABLE autofill (" | 1800 if (!db_->Execute("CREATE TABLE autofill (" | 
| 1686 "name VARCHAR, " | 1801 "name VARCHAR, " | 
| 1687 "value VARCHAR, " | 1802 "value VARCHAR, " | 
| 1688 "value_lower VARCHAR, " | 1803 "value_lower VARCHAR, " | 
| 1689 "date_created INTEGER DEFAULT 0, " | 1804 "date_created INTEGER DEFAULT 0, " | 
| 1690 "date_last_used INTEGER DEFAULT 0, " | 1805 "date_last_used INTEGER DEFAULT 0, " | 
| 1691 "count INTEGER DEFAULT 1, " | 1806 "count INTEGER DEFAULT 1, " | 
| 1692 "PRIMARY KEY (name, value))") || | 1807 "PRIMARY KEY (name, value))") || | 
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1872 "id VARCHAR NOT NULL," | 1987 "id VARCHAR NOT NULL," | 
| 1873 "use_count INTEGER NOT NULL DEFAULT 0, " | 1988 "use_count INTEGER NOT NULL DEFAULT 0, " | 
| 1874 "use_date INTEGER NOT NULL DEFAULT 0)")) { | 1989 "use_date INTEGER NOT NULL DEFAULT 0)")) { | 
| 1875 NOTREACHED(); | 1990 NOTREACHED(); | 
| 1876 return false; | 1991 return false; | 
| 1877 } | 1992 } | 
| 1878 } | 1993 } | 
| 1879 return true; | 1994 return true; | 
| 1880 } | 1995 } | 
| 1881 | 1996 | 
| 1997 bool AutofillTable::InitAutofillSyncMetadataTable() { | |
| 1998 if (!db_->DoesTableExist("autofill_sync_metadata")) { | |
| 1999 if (!db_->Execute("CREATE TABLE autofill_sync_metadata (" | |
| 2000 "storage_key VARCHAR primary key," | |
| 2001 "value BLOB)")) { | |
| 
Scott Hess - ex-Googler
2016/12/07 23:15:20
I'd drop a NOT NULL in there.  PRIMARY KEY does no
 
Patrick Noland
2016/12/08 22:51:13
I've added the NOT NULL constraint, but I'm leavin
 
Scott Hess - ex-Googler
2016/12/08 23:24:01
Acknowledged.
 | |
| 2002 NOTREACHED(); | |
| 2003 return false; | |
| 2004 } | |
| 2005 } | |
| 2006 return true; | |
| 2007 } | |
| 2008 | |
| 2009 bool AutofillTable::InitModelTypeStateTable() { | |
| 2010 if (!db_->DoesTableExist("autofill_model_type_state")) { | |
| 2011 if (!db_->Execute("CREATE TABLE autofill_model_type_state (value BLOB)")) { | |
| 2012 NOTREACHED(); | |
| 2013 return false; | |
| 2014 } | |
| 2015 } | |
| 2016 return true; | |
| 2017 } | |
| 2018 | |
| 1882 bool AutofillTable::MigrateToVersion54AddI18nFieldsAndRemoveDeprecatedFields() { | 2019 bool AutofillTable::MigrateToVersion54AddI18nFieldsAndRemoveDeprecatedFields() { | 
| 1883 sql::Transaction transaction(db_); | 2020 sql::Transaction transaction(db_); | 
| 1884 if (!transaction.Begin()) | 2021 if (!transaction.Begin()) | 
| 1885 return false; | 2022 return false; | 
| 1886 | 2023 | 
| 1887 // Test the existence of the |address_line_1| column as an indication that a | 2024 // 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| | 2025 // 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 | 2026 // schema is in place because the table was newly created when migrating from | 
| 1890 // a pre-version-23 database. | 2027 // a pre-version-23 database. | 
| 1891 if (db_->DoesColumnExist("autofill_profiles", "address_line_1")) { | 2028 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"); | 2445 "ALTER TABLE credit_cards ADD COLUMN billing_address_id VARCHAR"); | 
| 2309 } | 2446 } | 
| 2310 | 2447 | 
| 2311 bool AutofillTable::MigrateToVersion67AddMaskedCardBillingAddress() { | 2448 bool AutofillTable::MigrateToVersion67AddMaskedCardBillingAddress() { | 
| 2312 // The default value for this column is null, but Connection::ColumnString() | 2449 // The default value for this column is null, but Connection::ColumnString() | 
| 2313 // returns an empty string for that. | 2450 // returns an empty string for that. | 
| 2314 return db_->Execute( | 2451 return db_->Execute( | 
| 2315 "ALTER TABLE masked_credit_cards ADD COLUMN billing_address_id VARCHAR"); | 2452 "ALTER TABLE masked_credit_cards ADD COLUMN billing_address_id VARCHAR"); | 
| 2316 } | 2453 } | 
| 2317 | 2454 | 
| 2318 } // namespace autofill | 2455 bool AutofillTable::MigrateToVersion70AddSyncMetadata() { | 
| 2456 if (!db_->Execute("CREATE TABLE autofill_sync_metadata (" | |
| 2457 "storage_key VARCHAR primary key," | |
| 2458 "value BLOB)")) { | |
| 2459 return false; | |
| 2460 } | |
| 2461 | |
| 
Mathieu
2016/12/07 16:17:42
nit: remove extra line to be consistent
 
Patrick Noland
2016/12/08 22:51:14
Done.
 | |
| 2462 return db_->Execute("CREATE TABLE autofill_model_type_state (value BLOB)"); | |
| 
Scott Hess - ex-Googler
2016/12/07 23:15:20
This probably wants a transaction to keep these at
 
Patrick Noland
2016/12/08 22:51:13
Migration functions are called in the context of a
 
Scott Hess - ex-Googler
2016/12/08 23:24:01
Acknowledged.
 | |
| 2463 } | |
| 2464 | |
| 2465 } // namespace autofill | |
| OLD | NEW |