diff options
29 files changed, 649 insertions, 741 deletions
diff --git a/chrome/browser/sync/profile_sync_service_autofill_unittest.cc b/chrome/browser/sync/profile_sync_service_autofill_unittest.cc index 8290e04..6ffaf1b 100644 --- a/chrome/browser/sync/profile_sync_service_autofill_unittest.cc +++ b/chrome/browser/sync/profile_sync_service_autofill_unittest.cc @@ -95,7 +95,7 @@ class HistoryService; class AutofillTableMock : public AutofillTable { public: - AutofillTableMock() : AutofillTable(NULL, NULL) {} + AutofillTableMock() : AutofillTable() {} MOCK_METHOD2(RemoveFormElement, bool(const string16& name, const string16& value)); // NOLINT MOCK_METHOD1(GetAllAutofillEntries, @@ -122,15 +122,9 @@ MATCHER_P(MatchProfiles, profile, "") { class WebDatabaseFake : public WebDatabase { public: - explicit WebDatabaseFake(AutofillTable* autofill_table) - : autofill_table_(autofill_table) {} - - virtual AutofillTable* GetAutofillTable() OVERRIDE { - return autofill_table_; + explicit WebDatabaseFake(AutofillTable* autofill_table) { + AddTable(autofill_table); } - - private: - AutofillTable* autofill_table_; }; class ProfileSyncServiceAutofillTest; diff --git a/chrome/browser/sync/test/integration/autofill_helper.cc b/chrome/browser/sync/test/integration/autofill_helper.cc index 753ae8c..777c628 100644 --- a/chrome/browser/sync/test/integration/autofill_helper.cc +++ b/chrome/browser/sync/test/integration/autofill_helper.cc @@ -86,7 +86,8 @@ void RunOnDBThreadAndBlock(base::Closure task) { void GetAllAutofillEntriesOnDBThread(WebDataService* wds, std::vector<AutofillEntry>* entries) { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); - wds->GetDatabase()->GetAutofillTable()->GetAllAutofillEntries(entries); + AutofillTable::FromWebDatabase( + wds->GetDatabase())->GetAllAutofillEntries(entries); } std::vector<AutofillEntry> GetAllAutofillEntries(WebDataService* wds) { diff --git a/chrome/browser/webdata/autocomplete_syncable_service.cc b/chrome/browser/webdata/autocomplete_syncable_service.cc index f635e60..f039075 100644 --- a/chrome/browser/webdata/autocomplete_syncable_service.cc +++ b/chrome/browser/webdata/autocomplete_syncable_service.cc @@ -299,8 +299,8 @@ void AutocompleteSyncableService::Observe(int type, bool AutocompleteSyncableService::LoadAutofillData( std::vector<AutofillEntry>* entries) const { - return web_data_service_->GetDatabase()-> - GetAutofillTable()->GetAllAutofillEntries(entries); + return AutofillTable::FromWebDatabase( + web_data_service_->GetDatabase())->GetAllAutofillEntries(entries); } bool AutocompleteSyncableService::SaveChangesToWebData( @@ -308,8 +308,9 @@ bool AutocompleteSyncableService::SaveChangesToWebData( DCHECK(CalledOnValidThread()); if (!new_entries.empty() && - !web_data_service_->GetDatabase()-> - GetAutofillTable()->UpdateAutofillEntries(new_entries)) { + !AutofillTable::FromWebDatabase( + web_data_service_->GetDatabase())->UpdateAutofillEntries( + new_entries)) { return false; } return true; @@ -381,8 +382,9 @@ void AutocompleteSyncableService::WriteAutofillEntry( syncer::SyncError AutocompleteSyncableService::AutofillEntryDelete( const sync_pb::AutofillSpecifics& autofill) { - if (!web_data_service_->GetDatabase()->GetAutofillTable()->RemoveFormElement( - UTF8ToUTF16(autofill.name()), UTF8ToUTF16(autofill.value()))) { + if (!AutofillTable::FromWebDatabase( + web_data_service_->GetDatabase())->RemoveFormElement( + UTF8ToUTF16(autofill.name()), UTF8ToUTF16(autofill.value()))) { return error_handler_->CreateAndUploadError( FROM_HERE, "Could not remove autocomplete entry from WebDatabase."); @@ -400,8 +402,8 @@ void AutocompleteSyncableService::ActOnChanges( case AutofillChange::ADD: case AutofillChange::UPDATE: { std::vector<base::Time> timestamps; - if (!web_data_service_->GetDatabase()-> - GetAutofillTable()->GetAutofillTimestamps( + if (!AutofillTable::FromWebDatabase( + web_data_service_->GetDatabase())->GetAutofillTimestamps( change->key().name(), change->key().value(), ×tamps)) { diff --git a/chrome/browser/webdata/autofill_profile_syncable_service.cc b/chrome/browser/webdata/autofill_profile_syncable_service.cc index 967a793..980ca80 100644 --- a/chrome/browser/webdata/autofill_profile_syncable_service.cc +++ b/chrome/browser/webdata/autofill_profile_syncable_service.cc @@ -546,7 +546,7 @@ bool AutofillProfileSyncableService::MergeProfile( } AutofillTable* AutofillProfileSyncableService::GetAutofillTable() const { - return web_data_service_->GetDatabase()->GetAutofillTable(); + return AutofillTable::FromWebDatabase(web_data_service_->GetDatabase()); } AutofillProfileSyncableService::DataBundle::DataBundle() {} diff --git a/chrome/browser/webdata/autofill_table.cc b/chrome/browser/webdata/autofill_table.cc index 734c28a..b3ccb52 100644 --- a/chrome/browser/webdata/autofill_table.cc +++ b/chrome/browser/webdata/autofill_table.cc @@ -20,6 +20,7 @@ #include "chrome/browser/password_manager/encryptor.h" #include "chrome/browser/webdata/autofill_change.h" #include "chrome/browser/webdata/autofill_entry.h" +#include "chrome/browser/webdata/web_database.h" #include "components/autofill/browser/autofill_country.h" #include "components/autofill/browser/autofill_profile.h" #include "components/autofill/browser/autofill_type.h" @@ -319,23 +320,37 @@ bool RemoveAutofillProfilePieces(const std::string& guid, sql::Connection* db) { return s3.Run(); } +int table_key = 0; + +WebDatabaseTable::TypeKey GetKey() { + return reinterpret_cast<void*>(&table_key); +} + } // namespace // The maximum length allowed for form data. const size_t AutofillTable::kMaxDataLength = 1024; -AutofillTable::AutofillTable(sql::Connection* db, sql::MetaTable* meta_table) - : WebDatabaseTable(db, meta_table) { +AutofillTable::AutofillTable() { } AutofillTable::~AutofillTable() { } -bool AutofillTable::Init() { - return (InitMainTable() && InitCreditCardsTable() && InitDatesTable() && - InitProfilesTable() && InitProfileNamesTable() && - InitProfileEmailsTable() && InitProfilePhonesTable() && - InitProfileTrashTable()); +AutofillTable* AutofillTable::FromWebDatabase(WebDatabase* db) { + return static_cast<AutofillTable*>(db->GetTable(GetKey())); +} + +WebDatabaseTable::TypeKey AutofillTable::GetTypeKey() const { + return GetKey(); +} + +bool AutofillTable::Init(sql::Connection* db, sql::MetaTable* meta_table) { + WebDatabaseTable::Init(db, meta_table); + return (InitMainTable() && InitCreditCardsTable() && InitDatesTable() && + InitProfilesTable() && InitProfileNamesTable() && + InitProfileEmailsTable() && InitProfilePhonesTable() && + InitProfileTrashTable()); } bool AutofillTable::IsSyncable() { diff --git a/chrome/browser/webdata/autofill_table.h b/chrome/browser/webdata/autofill_table.h index c0a076c..72e5850 100644 --- a/chrome/browser/webdata/autofill_table.h +++ b/chrome/browser/webdata/autofill_table.h @@ -17,6 +17,7 @@ class AutofillEntry; class AutofillProfile; class AutofillTableTest; class CreditCard; +class WebDatabase; struct FormFieldData; @@ -114,9 +115,14 @@ class Time; // class AutofillTable : public WebDatabaseTable { public: - AutofillTable(sql::Connection* db, sql::MetaTable* meta_table); + AutofillTable(); virtual ~AutofillTable(); - virtual bool Init() OVERRIDE; + + // Retrieves the AutofillTable* owned by |database|. + static AutofillTable* FromWebDatabase(WebDatabase* db); + + virtual WebDatabaseTable::TypeKey GetTypeKey() const OVERRIDE; + virtual bool Init(sql::Connection* db, sql::MetaTable* meta_table) OVERRIDE; virtual bool IsSyncable() OVERRIDE; virtual bool MigrateToVersion(int version, const std::string& app_locale, diff --git a/chrome/browser/webdata/autofill_table_unittest.cc b/chrome/browser/webdata/autofill_table_unittest.cc index b0af88d..92de23d 100644 --- a/chrome/browser/webdata/autofill_table_unittest.cc +++ b/chrome/browser/webdata/autofill_table_unittest.cc @@ -99,6 +99,11 @@ class AutofillTableTest : public testing::Test { #endif ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); file_ = temp_dir_.path().AppendASCII("TestWebDatabase"); + + table_.reset(new AutofillTable); + db_.reset(new WebDatabase); + db_->AddTable(table_.get()); + ASSERT_EQ(sql::INIT_OK, db_->Init(file_, std::string())); } static AutofillEntry MakeAutofillEntry(const char* name, @@ -116,16 +121,14 @@ class AutofillTableTest : public testing::Test { base::FilePath file_; base::ScopedTempDir temp_dir_; + scoped_ptr<AutofillTable> table_; + scoped_ptr<WebDatabase> db_; private: DISALLOW_COPY_AND_ASSIGN(AutofillTableTest); }; TEST_F(AutofillTableTest, Autofill) { - WebDatabase db; - - ASSERT_EQ(sql::INIT_OK, db.Init(file_, std::string())); - Time t1 = Time::Now(); // Simulate the submission of a handful of entries in a field called "Name", @@ -136,23 +139,23 @@ TEST_F(AutofillTableTest, Autofill) { field.value = ASCIIToUTF16("Superman"); base::Time now = base::Time::Now(); base::TimeDelta two_seconds = base::TimeDelta::FromSeconds(2); - EXPECT_TRUE(db.GetAutofillTable()->AddFormFieldValue(field, &changes)); + EXPECT_TRUE(table_->AddFormFieldValue(field, &changes)); std::vector<string16> v; for (int i = 0; i < 5; i++) { field.value = ASCIIToUTF16("Clark Kent"); - EXPECT_TRUE(db.GetAutofillTable()->AddFormFieldValueTime(field, &changes, - now + i * two_seconds)); + EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, + now + i * two_seconds)); } for (int i = 0; i < 3; i++) { field.value = ASCIIToUTF16("Clark Sutter"); - EXPECT_TRUE(db.GetAutofillTable()->AddFormFieldValueTime(field, &changes, - now + i * two_seconds)); + EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, + now + i * two_seconds)); } for (int i = 0; i < 2; i++) { field.name = ASCIIToUTF16("Favorite Color"); field.value = ASCIIToUTF16("Green"); - EXPECT_TRUE(db.GetAutofillTable()->AddFormFieldValueTime(field, &changes, - now + i * two_seconds)); + EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, + now + i * two_seconds)); } int count = 0; @@ -162,29 +165,26 @@ TEST_F(AutofillTableTest, Autofill) { // should be somthing non-zero. field.name = ASCIIToUTF16("Name"); field.value = ASCIIToUTF16("Clark Kent"); - EXPECT_TRUE(db.GetAutofillTable()->GetIDAndCountOfFormElement(field, &pair_id, - &count)); + EXPECT_TRUE(table_->GetIDAndCountOfFormElement(field, &pair_id, &count)); EXPECT_EQ(5, count); EXPECT_NE(0, pair_id); // Storing in the data base should be case sensitive, so there should be no // database entry for clark kent lowercase. field.value = ASCIIToUTF16("clark kent"); - EXPECT_TRUE(db.GetAutofillTable()->GetIDAndCountOfFormElement(field, &pair_id, - &count)); + EXPECT_TRUE(table_->GetIDAndCountOfFormElement(field, &pair_id, &count)); EXPECT_EQ(0, count); field.name = ASCIIToUTF16("Favorite Color"); field.value = ASCIIToUTF16("Green"); - EXPECT_TRUE(db.GetAutofillTable()->GetIDAndCountOfFormElement(field, &pair_id, - &count)); + EXPECT_TRUE(table_->GetIDAndCountOfFormElement(field, &pair_id, &count)); EXPECT_EQ(2, count); // This is meant to get a list of suggestions for Name. The empty prefix // in the second argument means it should return all suggestions for a name // no matter what they start with. The order that the names occur in the list // should be decreasing order by count. - EXPECT_TRUE(db.GetAutofillTable()->GetFormValuesForElementName( + EXPECT_TRUE(table_->GetFormValuesForElementName( ASCIIToUTF16("Name"), string16(), &v, 6)); EXPECT_EQ(3U, v.size()); if (v.size() == 3) { @@ -195,7 +195,7 @@ TEST_F(AutofillTableTest, Autofill) { // If we query again limiting the list size to 1, we should only get the most // frequent entry. - EXPECT_TRUE(db.GetAutofillTable()->GetFormValuesForElementName( + EXPECT_TRUE(table_->GetFormValuesForElementName( ASCIIToUTF16("Name"), string16(), &v, 1)); EXPECT_EQ(1U, v.size()); if (v.size() == 1) { @@ -204,7 +204,7 @@ TEST_F(AutofillTableTest, Autofill) { // Querying for suggestions given a prefix is case-insensitive, so the prefix // "cLa" shoud get suggestions for both Clarks. - EXPECT_TRUE(db.GetAutofillTable()->GetFormValuesForElementName( + EXPECT_TRUE(table_->GetFormValuesForElementName( ASCIIToUTF16("Name"), ASCIIToUTF16("cLa"), &v, 6)); EXPECT_EQ(2U, v.size()); if (v.size() == 2) { @@ -215,8 +215,7 @@ TEST_F(AutofillTableTest, Autofill) { // Removing all elements since the beginning of this function should remove // everything from the database. changes.clear(); - EXPECT_TRUE(db.GetAutofillTable()->RemoveFormElementsAddedBetween( - t1, Time(), &changes)); + EXPECT_TRUE(table_->RemoveFormElementsAddedBetween(t1, Time(), &changes)); const AutofillChange expected_changes[] = { AutofillChange(AutofillChange::REMOVE, @@ -239,11 +238,10 @@ TEST_F(AutofillTableTest, Autofill) { field.name = ASCIIToUTF16("Name"); field.value = ASCIIToUTF16("Clark Kent"); - EXPECT_TRUE(db.GetAutofillTable()->GetIDAndCountOfFormElement(field, &pair_id, - &count)); + EXPECT_TRUE(table_->GetIDAndCountOfFormElement(field, &pair_id, &count)); EXPECT_EQ(0, count); - EXPECT_TRUE(db.GetAutofillTable()->GetFormValuesForElementName( + EXPECT_TRUE(table_->GetFormValuesForElementName( ASCIIToUTF16("Name"), string16(), &v, 6)); EXPECT_EQ(0U, v.size()); @@ -251,29 +249,29 @@ TEST_F(AutofillTableTest, Autofill) { const string16 kValue = ASCIIToUTF16(" toto "); field.name = ASCIIToUTF16("blank"); field.value = string16(); - EXPECT_TRUE(db.GetAutofillTable()->AddFormFieldValue(field, &changes)); + EXPECT_TRUE(table_->AddFormFieldValue(field, &changes)); field.name = ASCIIToUTF16("blank"); field.value = ASCIIToUTF16(" "); - EXPECT_TRUE(db.GetAutofillTable()->AddFormFieldValue(field, &changes)); + EXPECT_TRUE(table_->AddFormFieldValue(field, &changes)); field.name = ASCIIToUTF16("blank"); field.value = ASCIIToUTF16(" "); - EXPECT_TRUE(db.GetAutofillTable()->AddFormFieldValue(field, &changes)); + EXPECT_TRUE(table_->AddFormFieldValue(field, &changes)); field.name = ASCIIToUTF16("blank"); field.value = kValue; - EXPECT_TRUE(db.GetAutofillTable()->AddFormFieldValue(field, &changes)); + EXPECT_TRUE(table_->AddFormFieldValue(field, &changes)); // They should be stored normally as the DB layer does not check for empty // values. v.clear(); - EXPECT_TRUE(db.GetAutofillTable()->GetFormValuesForElementName( + EXPECT_TRUE(table_->GetFormValuesForElementName( ASCIIToUTF16("blank"), string16(), &v, 10)); EXPECT_EQ(4U, v.size()); // Now we'll check that ClearAutofillEmptyValueElements() works as expected. - db.GetAutofillTable()->ClearAutofillEmptyValueElements(); + table_->ClearAutofillEmptyValueElements(); v.clear(); - EXPECT_TRUE(db.GetAutofillTable()->GetFormValuesForElementName( + EXPECT_TRUE(table_->GetFormValuesForElementName( ASCIIToUTF16("blank"), string16(), &v, 10)); ASSERT_EQ(1U, v.size()); @@ -281,9 +279,6 @@ TEST_F(AutofillTableTest, Autofill) { } TEST_F(AutofillTableTest, Autofill_RemoveBetweenChanges) { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(file_, std::string())); - TimeDelta one_day(TimeDelta::FromDays(1)); Time t1 = Time::Now(); Time t2 = t1 + one_day; @@ -292,14 +287,11 @@ TEST_F(AutofillTableTest, Autofill_RemoveBetweenChanges) { FormFieldData field; field.name = ASCIIToUTF16("Name"); field.value = ASCIIToUTF16("Superman"); - EXPECT_TRUE( - db.GetAutofillTable()->AddFormFieldValueTime(field, &changes, t1)); - EXPECT_TRUE( - db.GetAutofillTable()->AddFormFieldValueTime(field, &changes, t2)); + EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, t1)); + EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, t2)); changes.clear(); - EXPECT_TRUE(db.GetAutofillTable()->RemoveFormElementsAddedBetween( - t1, t2, &changes)); + EXPECT_TRUE(table_->RemoveFormElementsAddedBetween(t1, t2, &changes)); ASSERT_EQ(1U, changes.size()); EXPECT_EQ(AutofillChange(AutofillChange::UPDATE, AutofillKey(ASCIIToUTF16("Name"), @@ -307,8 +299,8 @@ TEST_F(AutofillTableTest, Autofill_RemoveBetweenChanges) { changes[0]); changes.clear(); - EXPECT_TRUE(db.GetAutofillTable()->RemoveFormElementsAddedBetween( - t2, t2 + one_day, &changes)); + EXPECT_TRUE( + table_->RemoveFormElementsAddedBetween(t2, t2 + one_day, &changes)); ASSERT_EQ(1U, changes.size()); EXPECT_EQ(AutofillChange(AutofillChange::REMOVE, AutofillKey(ASCIIToUTF16("Name"), @@ -317,9 +309,6 @@ TEST_F(AutofillTableTest, Autofill_RemoveBetweenChanges) { } TEST_F(AutofillTableTest, Autofill_AddChanges) { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(file_, std::string())); - TimeDelta one_day(TimeDelta::FromDays(1)); Time t1 = Time::Now(); Time t2 = t1 + one_day; @@ -328,8 +317,7 @@ TEST_F(AutofillTableTest, Autofill_AddChanges) { FormFieldData field; field.name = ASCIIToUTF16("Name"); field.value = ASCIIToUTF16("Superman"); - EXPECT_TRUE( - db.GetAutofillTable()->AddFormFieldValueTime(field, &changes, t1)); + EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, t1)); ASSERT_EQ(1U, changes.size()); EXPECT_EQ(AutofillChange(AutofillChange::ADD, AutofillKey(ASCIIToUTF16("Name"), @@ -338,7 +326,7 @@ TEST_F(AutofillTableTest, Autofill_AddChanges) { changes.clear(); EXPECT_TRUE( - db.GetAutofillTable()->AddFormFieldValueTime(field, &changes, t2)); + table_->AddFormFieldValueTime(field, &changes, t2)); ASSERT_EQ(1U, changes.size()); EXPECT_EQ(AutofillChange(AutofillChange::UPDATE, AutofillKey(ASCIIToUTF16("Name"), @@ -347,129 +335,107 @@ TEST_F(AutofillTableTest, Autofill_AddChanges) { } TEST_F(AutofillTableTest, Autofill_UpdateOneWithOneTimestamp) { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(file_, std::string())); - AutofillEntry entry(MakeAutofillEntry("foo", "bar", 1, -1)); std::vector<AutofillEntry> entries; entries.push_back(entry); - ASSERT_TRUE(db.GetAutofillTable()->UpdateAutofillEntries(entries)); + ASSERT_TRUE(table_->UpdateAutofillEntries(entries)); FormFieldData field; field.name = ASCIIToUTF16("foo"); field.value = ASCIIToUTF16("bar"); int64 pair_id; int count; - ASSERT_TRUE(db.GetAutofillTable()->GetIDAndCountOfFormElement( - field, &pair_id, &count)); + ASSERT_TRUE(table_->GetIDAndCountOfFormElement(field, &pair_id, &count)); EXPECT_LE(0, pair_id); EXPECT_EQ(1, count); std::vector<AutofillEntry> all_entries; - ASSERT_TRUE(db.GetAutofillTable()->GetAllAutofillEntries(&all_entries)); + ASSERT_TRUE(table_->GetAllAutofillEntries(&all_entries)); ASSERT_EQ(1U, all_entries.size()); EXPECT_TRUE(entry == all_entries[0]); } TEST_F(AutofillTableTest, Autofill_UpdateOneWithTwoTimestamps) { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(file_, std::string())); - AutofillEntry entry(MakeAutofillEntry("foo", "bar", 1, 2)); std::vector<AutofillEntry> entries; entries.push_back(entry); - ASSERT_TRUE(db.GetAutofillTable()->UpdateAutofillEntries(entries)); + ASSERT_TRUE(table_->UpdateAutofillEntries(entries)); FormFieldData field; field.name = ASCIIToUTF16("foo"); field.value = ASCIIToUTF16("bar"); int64 pair_id; int count; - ASSERT_TRUE(db.GetAutofillTable()->GetIDAndCountOfFormElement( - field, &pair_id, &count)); + ASSERT_TRUE(table_->GetIDAndCountOfFormElement(field, &pair_id, &count)); EXPECT_LE(0, pair_id); EXPECT_EQ(2, count); std::vector<AutofillEntry> all_entries; - ASSERT_TRUE(db.GetAutofillTable()->GetAllAutofillEntries(&all_entries)); + ASSERT_TRUE(table_->GetAllAutofillEntries(&all_entries)); ASSERT_EQ(1U, all_entries.size()); EXPECT_TRUE(entry == all_entries[0]); } TEST_F(AutofillTableTest, Autofill_GetAutofillTimestamps) { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(file_, std::string())); - AutofillEntry entry(MakeAutofillEntry("foo", "bar", 1, 2)); std::vector<AutofillEntry> entries; entries.push_back(entry); - ASSERT_TRUE(db.GetAutofillTable()->UpdateAutofillEntries(entries)); + ASSERT_TRUE(table_->UpdateAutofillEntries(entries)); std::vector<Time> timestamps; - ASSERT_TRUE(db.GetAutofillTable()->GetAutofillTimestamps(ASCIIToUTF16("foo"), - ASCIIToUTF16("bar"), - ×tamps)); + ASSERT_TRUE(table_->GetAutofillTimestamps(ASCIIToUTF16("foo"), + ASCIIToUTF16("bar"), + ×tamps)); ASSERT_EQ(2U, timestamps.size()); EXPECT_TRUE(Time::FromTimeT(1) == timestamps[0]); EXPECT_TRUE(Time::FromTimeT(2) == timestamps[1]); } TEST_F(AutofillTableTest, Autofill_UpdateTwo) { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(file_, std::string())); - AutofillEntry entry0(MakeAutofillEntry("foo", "bar0", 1, -1)); AutofillEntry entry1(MakeAutofillEntry("foo", "bar1", 2, 3)); std::vector<AutofillEntry> entries; entries.push_back(entry0); entries.push_back(entry1); - ASSERT_TRUE(db.GetAutofillTable()->UpdateAutofillEntries(entries)); + ASSERT_TRUE(table_->UpdateAutofillEntries(entries)); FormFieldData field0; field0.name = ASCIIToUTF16("foo"); field0.value = ASCIIToUTF16("bar0"); int64 pair_id; int count; - ASSERT_TRUE(db.GetAutofillTable()->GetIDAndCountOfFormElement( - field0, &pair_id, &count)); + ASSERT_TRUE(table_->GetIDAndCountOfFormElement(field0, &pair_id, &count)); EXPECT_LE(0, pair_id); EXPECT_EQ(1, count); FormFieldData field1; field1.name = ASCIIToUTF16("foo"); field1.value = ASCIIToUTF16("bar1"); - ASSERT_TRUE(db.GetAutofillTable()->GetIDAndCountOfFormElement( - field1, &pair_id, &count)); + ASSERT_TRUE(table_->GetIDAndCountOfFormElement(field1, &pair_id, &count)); EXPECT_LE(0, pair_id); EXPECT_EQ(2, count); } TEST_F(AutofillTableTest, Autofill_UpdateReplace) { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(file_, std::string())); - AutofillChangeList changes; // Add a form field. This will be replaced. FormFieldData field; field.name = ASCIIToUTF16("Name"); field.value = ASCIIToUTF16("Superman"); - EXPECT_TRUE(db.GetAutofillTable()->AddFormFieldValue(field, &changes)); + EXPECT_TRUE(table_->AddFormFieldValue(field, &changes)); AutofillEntry entry(MakeAutofillEntry("Name", "Superman", 1, 2)); std::vector<AutofillEntry> entries; entries.push_back(entry); - ASSERT_TRUE(db.GetAutofillTable()->UpdateAutofillEntries(entries)); + ASSERT_TRUE(table_->UpdateAutofillEntries(entries)); std::vector<AutofillEntry> all_entries; - ASSERT_TRUE(db.GetAutofillTable()->GetAllAutofillEntries(&all_entries)); + ASSERT_TRUE(table_->GetAllAutofillEntries(&all_entries)); ASSERT_EQ(1U, all_entries.size()); EXPECT_TRUE(entry == all_entries[0]); } TEST_F(AutofillTableTest, Autofill_UpdateDontReplace) { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(file_, std::string())); - Time t = Time::Now(); AutofillEntry existing( MakeAutofillEntry("Name", "Superman", t.ToTimeT(), -1)); @@ -479,14 +445,14 @@ TEST_F(AutofillTableTest, Autofill_UpdateDontReplace) { FormFieldData field; field.name = existing.key().name(); field.value = existing.key().value(); - EXPECT_TRUE(db.GetAutofillTable()->AddFormFieldValueTime(field, &changes, t)); + EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, t)); AutofillEntry entry(MakeAutofillEntry("Name", "Clark Kent", 1, 2)); std::vector<AutofillEntry> entries; entries.push_back(entry); - ASSERT_TRUE(db.GetAutofillTable()->UpdateAutofillEntries(entries)); + ASSERT_TRUE(table_->UpdateAutofillEntries(entries)); std::vector<AutofillEntry> all_entries; - ASSERT_TRUE(db.GetAutofillTable()->GetAllAutofillEntries(&all_entries)); + ASSERT_TRUE(table_->GetAllAutofillEntries(&all_entries)); ASSERT_EQ(2U, all_entries.size()); AutofillEntrySet expected_entries(all_entries.begin(), all_entries.end(), @@ -496,9 +462,6 @@ TEST_F(AutofillTableTest, Autofill_UpdateDontReplace) { } TEST_F(AutofillTableTest, Autofill_AddFormFieldValues) { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(file_, std::string())); - Time t = Time::Now(); // Add multiple values for "firstname" and "lastname" names. Test that only @@ -523,7 +486,7 @@ TEST_F(AutofillTableTest, Autofill_AddFormFieldValues) { elements.push_back(field); std::vector<AutofillChange> changes; - db.GetAutofillTable()->AddFormFieldValuesTime(elements, &changes, t); + table_->AddFormFieldValuesTime(elements, &changes, t); ASSERT_EQ(2U, changes.size()); EXPECT_EQ(changes[0], AutofillChange(AutofillChange::ADD, @@ -534,15 +497,11 @@ TEST_F(AutofillTableTest, Autofill_AddFormFieldValues) { ASCIIToUTF16("Smith")))); std::vector<AutofillEntry> all_entries; - ASSERT_TRUE(db.GetAutofillTable()->GetAllAutofillEntries(&all_entries)); + ASSERT_TRUE(table_->GetAllAutofillEntries(&all_entries)); ASSERT_EQ(2U, all_entries.size()); } TEST_F(AutofillTableTest, AutofillProfile) { - WebDatabase db; - - ASSERT_EQ(sql::INIT_OK, db.Init(file_, std::string())); - // Add a 'Home' profile. AutofillProfile home_profile; home_profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("John")); @@ -559,15 +518,14 @@ TEST_F(AutofillTableTest, AutofillProfile) { home_profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("18181234567")); Time pre_creation_time = Time::Now(); - EXPECT_TRUE(db.GetAutofillTable()->AddAutofillProfile(home_profile)); + EXPECT_TRUE(table_->AddAutofillProfile(home_profile)); Time post_creation_time = Time::Now(); // Get the 'Home' profile. AutofillProfile* db_profile; - ASSERT_TRUE(db.GetAutofillTable()->GetAutofillProfile( - home_profile.guid(), &db_profile)); + ASSERT_TRUE(table_->GetAutofillProfile(home_profile.guid(), &db_profile)); EXPECT_EQ(home_profile, *db_profile); - sql::Statement s_home(db.GetSQLConnection()->GetUniqueStatement( + sql::Statement s_home(db_->GetSQLConnection()->GetUniqueStatement( "SELECT date_modified " "FROM autofill_profiles WHERE guid=?")); s_home.BindString(0, home_profile.guid()); @@ -586,14 +544,13 @@ TEST_F(AutofillTableTest, AutofillProfile) { billing_profile.SetRawInfo(ADDRESS_HOME_LINE2, ASCIIToUTF16("suite 3")); pre_creation_time = Time::Now(); - EXPECT_TRUE(db.GetAutofillTable()->AddAutofillProfile(billing_profile)); + EXPECT_TRUE(table_->AddAutofillProfile(billing_profile)); post_creation_time = Time::Now(); // Get the 'Billing' profile. - ASSERT_TRUE(db.GetAutofillTable()->GetAutofillProfile( - billing_profile.guid(), &db_profile)); + ASSERT_TRUE(table_->GetAutofillProfile(billing_profile.guid(), &db_profile)); EXPECT_EQ(billing_profile, *db_profile); - sql::Statement s_billing(db.GetSQLConnection()->GetUniqueStatement( + sql::Statement s_billing(db_->GetSQLConnection()->GetUniqueStatement( "SELECT date_modified FROM autofill_profiles WHERE guid=?")); s_billing.BindString(0, billing_profile.guid()); ASSERT_TRUE(s_billing.is_valid()); @@ -606,13 +563,11 @@ TEST_F(AutofillTableTest, AutofillProfile) { // Update the 'Billing' profile, name only. billing_profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Jane")); Time pre_modification_time = Time::Now(); - EXPECT_TRUE(db.GetAutofillTable()->UpdateAutofillProfileMulti( - billing_profile)); + EXPECT_TRUE(table_->UpdateAutofillProfileMulti(billing_profile)); Time post_modification_time = Time::Now(); - ASSERT_TRUE(db.GetAutofillTable()->GetAutofillProfile( - billing_profile.guid(), &db_profile)); + ASSERT_TRUE(table_->GetAutofillProfile(billing_profile.guid(), &db_profile)); EXPECT_EQ(billing_profile, *db_profile); - sql::Statement s_billing_updated(db.GetSQLConnection()->GetUniqueStatement( + sql::Statement s_billing_updated(db_->GetSQLConnection()->GetUniqueStatement( "SELECT date_modified FROM autofill_profiles WHERE guid=?")); s_billing_updated.BindString(0, billing_profile.guid()); ASSERT_TRUE(s_billing_updated.is_valid()); @@ -639,14 +594,13 @@ TEST_F(AutofillTableTest, AutofillProfile) { billing_profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("18181230000")); Time pre_modification_time_2 = Time::Now(); - EXPECT_TRUE(db.GetAutofillTable()->UpdateAutofillProfileMulti( - billing_profile)); + EXPECT_TRUE(table_->UpdateAutofillProfileMulti(billing_profile)); Time post_modification_time_2 = Time::Now(); - ASSERT_TRUE(db.GetAutofillTable()->GetAutofillProfile( - billing_profile.guid(), &db_profile)); + ASSERT_TRUE(table_->GetAutofillProfile(billing_profile.guid(), &db_profile)); EXPECT_EQ(billing_profile, *db_profile); - sql::Statement s_billing_updated_2(db.GetSQLConnection()->GetUniqueStatement( - "SELECT date_modified FROM autofill_profiles WHERE guid=?")); + sql::Statement s_billing_updated_2( + db_->GetSQLConnection()->GetUniqueStatement( + "SELECT date_modified FROM autofill_profiles WHERE guid=?")); s_billing_updated_2.BindString(0, billing_profile.guid()); ASSERT_TRUE(s_billing_updated_2.is_valid()); ASSERT_TRUE(s_billing_updated_2.Step()); @@ -658,16 +612,11 @@ TEST_F(AutofillTableTest, AutofillProfile) { delete db_profile; // Remove the 'Billing' profile. - EXPECT_TRUE(db.GetAutofillTable()->RemoveAutofillProfile( - billing_profile.guid())); - EXPECT_FALSE(db.GetAutofillTable()->GetAutofillProfile( - billing_profile.guid(), &db_profile)); + EXPECT_TRUE(table_->RemoveAutofillProfile(billing_profile.guid())); + EXPECT_FALSE(table_->GetAutofillProfile(billing_profile.guid(), &db_profile)); } TEST_F(AutofillTableTest, AutofillProfileMultiValueNames) { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(file_, std::string())); - AutofillProfile p; const string16 kJohnDoe(ASCIIToUTF16("John Doe")); const string16 kJohnPDoe(ASCIIToUTF16("John P. Doe")); @@ -676,10 +625,10 @@ TEST_F(AutofillTableTest, AutofillProfileMultiValueNames) { set_values.push_back(kJohnPDoe); p.SetRawMultiInfo(NAME_FULL, set_values); - EXPECT_TRUE(db.GetAutofillTable()->AddAutofillProfile(p)); + EXPECT_TRUE(table_->AddAutofillProfile(p)); AutofillProfile* db_profile; - ASSERT_TRUE(db.GetAutofillTable()->GetAutofillProfile(p.guid(), &db_profile)); + ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile)); EXPECT_EQ(p, *db_profile); EXPECT_EQ(0, p.Compare(*db_profile)); delete db_profile; @@ -688,8 +637,8 @@ TEST_F(AutofillTableTest, AutofillProfileMultiValueNames) { const string16 kNoOne(ASCIIToUTF16("No One")); set_values[1] = kNoOne; p.SetRawMultiInfo(NAME_FULL, set_values); - EXPECT_TRUE(db.GetAutofillTable()->UpdateAutofillProfileMulti(p)); - ASSERT_TRUE(db.GetAutofillTable()->GetAutofillProfile(p.guid(), &db_profile)); + EXPECT_TRUE(table_->UpdateAutofillProfileMulti(p)); + ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile)); EXPECT_EQ(p, *db_profile); EXPECT_EQ(0, p.Compare(*db_profile)); delete db_profile; @@ -697,8 +646,8 @@ TEST_F(AutofillTableTest, AutofillProfileMultiValueNames) { // Delete values. set_values.clear(); p.SetRawMultiInfo(NAME_FULL, set_values); - EXPECT_TRUE(db.GetAutofillTable()->UpdateAutofillProfileMulti(p)); - ASSERT_TRUE(db.GetAutofillTable()->GetAutofillProfile(p.guid(), &db_profile)); + EXPECT_TRUE(table_->UpdateAutofillProfileMulti(p)); + ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile)); EXPECT_EQ(p, *db_profile); EXPECT_EQ(0, p.Compare(*db_profile)); EXPECT_EQ(string16(), db_profile->GetRawInfo(NAME_FULL)); @@ -706,9 +655,6 @@ TEST_F(AutofillTableTest, AutofillProfileMultiValueNames) { } TEST_F(AutofillTableTest, AutofillProfileSingleValue) { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(file_, std::string())); - AutofillProfile p; const string16 kJohnDoe(ASCIIToUTF16("John Doe")); const string16 kJohnPDoe(ASCIIToUTF16("John P. Doe")); @@ -717,10 +663,10 @@ TEST_F(AutofillTableTest, AutofillProfileSingleValue) { set_values.push_back(kJohnPDoe); p.SetRawMultiInfo(NAME_FULL, set_values); - EXPECT_TRUE(db.GetAutofillTable()->AddAutofillProfile(p)); + EXPECT_TRUE(table_->AddAutofillProfile(p)); AutofillProfile* db_profile; - ASSERT_TRUE(db.GetAutofillTable()->GetAutofillProfile(p.guid(), &db_profile)); + ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile)); EXPECT_EQ(p, *db_profile); EXPECT_EQ(0, p.Compare(*db_profile)); delete db_profile; @@ -729,8 +675,8 @@ TEST_F(AutofillTableTest, AutofillProfileSingleValue) { set_values.resize(1); set_values[0] = kNoOne; p.SetRawMultiInfo(NAME_FULL, set_values); - EXPECT_TRUE(db.GetAutofillTable()->UpdateAutofillProfile(p)); - ASSERT_TRUE(db.GetAutofillTable()->GetAutofillProfile(p.guid(), &db_profile)); + EXPECT_TRUE(table_->UpdateAutofillProfile(p)); + ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile)); EXPECT_EQ(p.PrimaryValue(), db_profile->PrimaryValue()); EXPECT_EQ(p.guid(), db_profile->guid()); EXPECT_NE(0, p.Compare(*db_profile)); @@ -742,9 +688,6 @@ TEST_F(AutofillTableTest, AutofillProfileSingleValue) { } TEST_F(AutofillTableTest, AutofillProfileMultiValueEmails) { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(file_, std::string())); - AutofillProfile p; const string16 kJohnDoe(ASCIIToUTF16("john@doe.com")); const string16 kJohnPDoe(ASCIIToUTF16("john_p@doe.com")); @@ -753,10 +696,10 @@ TEST_F(AutofillTableTest, AutofillProfileMultiValueEmails) { set_values.push_back(kJohnPDoe); p.SetRawMultiInfo(EMAIL_ADDRESS, set_values); - EXPECT_TRUE(db.GetAutofillTable()->AddAutofillProfile(p)); + EXPECT_TRUE(table_->AddAutofillProfile(p)); AutofillProfile* db_profile; - ASSERT_TRUE(db.GetAutofillTable()->GetAutofillProfile(p.guid(), &db_profile)); + ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile)); EXPECT_EQ(p, *db_profile); EXPECT_EQ(0, p.Compare(*db_profile)); delete db_profile; @@ -765,8 +708,8 @@ TEST_F(AutofillTableTest, AutofillProfileMultiValueEmails) { const string16 kNoOne(ASCIIToUTF16("no@one.com")); set_values[1] = kNoOne; p.SetRawMultiInfo(EMAIL_ADDRESS, set_values); - EXPECT_TRUE(db.GetAutofillTable()->UpdateAutofillProfileMulti(p)); - ASSERT_TRUE(db.GetAutofillTable()->GetAutofillProfile(p.guid(), &db_profile)); + EXPECT_TRUE(table_->UpdateAutofillProfileMulti(p)); + ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile)); EXPECT_EQ(p, *db_profile); EXPECT_EQ(0, p.Compare(*db_profile)); delete db_profile; @@ -774,8 +717,8 @@ TEST_F(AutofillTableTest, AutofillProfileMultiValueEmails) { // Delete values. set_values.clear(); p.SetRawMultiInfo(EMAIL_ADDRESS, set_values); - EXPECT_TRUE(db.GetAutofillTable()->UpdateAutofillProfileMulti(p)); - ASSERT_TRUE(db.GetAutofillTable()->GetAutofillProfile(p.guid(), &db_profile)); + EXPECT_TRUE(table_->UpdateAutofillProfileMulti(p)); + ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile)); EXPECT_EQ(p, *db_profile); EXPECT_EQ(0, p.Compare(*db_profile)); EXPECT_EQ(string16(), db_profile->GetRawInfo(EMAIL_ADDRESS)); @@ -783,9 +726,6 @@ TEST_F(AutofillTableTest, AutofillProfileMultiValueEmails) { } TEST_F(AutofillTableTest, AutofillProfileMultiValuePhone) { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(file_, std::string())); - AutofillProfile p; const string16 kJohnDoe(ASCIIToUTF16("4151112222")); const string16 kJohnPDoe(ASCIIToUTF16("4151113333")); @@ -794,10 +734,10 @@ TEST_F(AutofillTableTest, AutofillProfileMultiValuePhone) { set_values.push_back(kJohnPDoe); p.SetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, set_values); - EXPECT_TRUE(db.GetAutofillTable()->AddAutofillProfile(p)); + EXPECT_TRUE(table_->AddAutofillProfile(p)); AutofillProfile* db_profile; - ASSERT_TRUE(db.GetAutofillTable()->GetAutofillProfile(p.guid(), &db_profile)); + ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile)); EXPECT_EQ(p, *db_profile); EXPECT_EQ(0, p.Compare(*db_profile)); delete db_profile; @@ -806,8 +746,8 @@ TEST_F(AutofillTableTest, AutofillProfileMultiValuePhone) { const string16 kNoOne(ASCIIToUTF16("4151110000")); set_values[1] = kNoOne; p.SetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, set_values); - EXPECT_TRUE(db.GetAutofillTable()->UpdateAutofillProfileMulti(p)); - ASSERT_TRUE(db.GetAutofillTable()->GetAutofillProfile(p.guid(), &db_profile)); + EXPECT_TRUE(table_->UpdateAutofillProfileMulti(p)); + ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile)); EXPECT_EQ(p, *db_profile); EXPECT_EQ(0, p.Compare(*db_profile)); delete db_profile; @@ -815,8 +755,8 @@ TEST_F(AutofillTableTest, AutofillProfileMultiValuePhone) { // Delete values. set_values.clear(); p.SetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, set_values); - EXPECT_TRUE(db.GetAutofillTable()->UpdateAutofillProfileMulti(p)); - ASSERT_TRUE(db.GetAutofillTable()->GetAutofillProfile(p.guid(), &db_profile)); + EXPECT_TRUE(table_->UpdateAutofillProfileMulti(p)); + ASSERT_TRUE(table_->GetAutofillProfile(p.guid(), &db_profile)); EXPECT_EQ(p, *db_profile); EXPECT_EQ(0, p.Compare(*db_profile)); EXPECT_EQ(string16(), db_profile->GetRawInfo(EMAIL_ADDRESS)); @@ -824,35 +764,27 @@ TEST_F(AutofillTableTest, AutofillProfileMultiValuePhone) { } TEST_F(AutofillTableTest, AutofillProfileTrash) { - WebDatabase db; - - ASSERT_EQ(sql::INIT_OK, db.Init(file_, std::string())); - std::vector<std::string> guids; - db.GetAutofillTable()->GetAutofillProfilesInTrash(&guids); + table_->GetAutofillProfilesInTrash(&guids); EXPECT_TRUE(guids.empty()); - ASSERT_TRUE(db.GetAutofillTable()->AddAutofillGUIDToTrash( + ASSERT_TRUE(table_->AddAutofillGUIDToTrash( "00000000-0000-0000-0000-000000000000")); - ASSERT_TRUE(db.GetAutofillTable()->AddAutofillGUIDToTrash( + ASSERT_TRUE(table_->AddAutofillGUIDToTrash( "00000000-0000-0000-0000-000000000001")); - ASSERT_TRUE(db.GetAutofillTable()->GetAutofillProfilesInTrash(&guids)); + ASSERT_TRUE(table_->GetAutofillProfilesInTrash(&guids)); EXPECT_EQ(2UL, guids.size()); EXPECT_EQ("00000000-0000-0000-0000-000000000000", guids[0]); EXPECT_EQ("00000000-0000-0000-0000-000000000001", guids[1]); - ASSERT_TRUE(db.GetAutofillTable()->EmptyAutofillProfilesTrash()); - ASSERT_TRUE(db.GetAutofillTable()->GetAutofillProfilesInTrash(&guids)); + ASSERT_TRUE(table_->EmptyAutofillProfilesTrash()); + ASSERT_TRUE(table_->GetAutofillProfilesInTrash(&guids)); EXPECT_TRUE(guids.empty()); } TEST_F(AutofillTableTest, AutofillProfileTrashInteraction) { - WebDatabase db; - - ASSERT_EQ(sql::INIT_OK, db.Init(file_, std::string())); - std::vector<std::string> guids; - db.GetAutofillTable()->GetAutofillProfilesInTrash(&guids); + table_->GetAutofillProfilesInTrash(&guids); EXPECT_TRUE(guids.empty()); AutofillProfile profile; @@ -868,19 +800,18 @@ TEST_F(AutofillTableTest, AutofillProfileTrashInteraction) { // Mark this profile as in the trash. This stops |AddAutofillProfile| from // adding it. - EXPECT_TRUE(db.GetAutofillTable()->AddAutofillGUIDToTrash(profile.guid())); - EXPECT_TRUE(db.GetAutofillTable()->AddAutofillProfile(profile)); + EXPECT_TRUE(table_->AddAutofillGUIDToTrash(profile.guid())); + EXPECT_TRUE(table_->AddAutofillProfile(profile)); AutofillProfile* added_profile = NULL; - EXPECT_FALSE(db.GetAutofillTable()->GetAutofillProfile( - profile.guid(), &added_profile)); + EXPECT_FALSE(table_->GetAutofillProfile(profile.guid(), &added_profile)); EXPECT_EQ(static_cast<AutofillProfile*>(NULL), added_profile); // Add the profile for real this time. - EXPECT_TRUE(db.GetAutofillTable()->EmptyAutofillProfilesTrash()); - EXPECT_TRUE(db.GetAutofillTable()->GetAutofillProfilesInTrash(&guids)); + EXPECT_TRUE(table_->EmptyAutofillProfilesTrash()); + EXPECT_TRUE(table_->GetAutofillProfilesInTrash(&guids)); EXPECT_TRUE(guids.empty()); - EXPECT_TRUE(db.GetAutofillTable()->AddAutofillProfile(profile)); - EXPECT_TRUE(db.GetAutofillTable()->GetAutofillProfile(profile.guid(), + EXPECT_TRUE(table_->AddAutofillProfile(profile)); + EXPECT_TRUE(table_->GetAutofillProfile(profile.guid(), &added_profile)); ASSERT_NE(static_cast<AutofillProfile*>(NULL), added_profile); delete added_profile; @@ -888,12 +819,11 @@ TEST_F(AutofillTableTest, AutofillProfileTrashInteraction) { // Mark this profile as in the trash. This stops |UpdateAutofillProfileMulti| // from updating it. In normal operation a profile should not be both in the // trash and in the profiles table simultaneously. - EXPECT_TRUE(db.GetAutofillTable()->AddAutofillGUIDToTrash(profile.guid())); + EXPECT_TRUE(table_->AddAutofillGUIDToTrash(profile.guid())); profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Jane")); - EXPECT_TRUE(db.GetAutofillTable()->UpdateAutofillProfileMulti(profile)); + EXPECT_TRUE(table_->UpdateAutofillProfileMulti(profile)); AutofillProfile* updated_profile = NULL; - EXPECT_TRUE(db.GetAutofillTable()->GetAutofillProfile( - profile.guid(), &updated_profile)); + EXPECT_TRUE(table_->GetAutofillProfile(profile.guid(), &updated_profile)); ASSERT_NE(static_cast<AutofillProfile*>(NULL), added_profile); EXPECT_EQ(ASCIIToUTF16("John"), updated_profile->GetRawInfo(NAME_FIRST)); delete updated_profile; @@ -904,28 +834,22 @@ TEST_F(AutofillTableTest, AutofillProfileTrashInteraction) { // does remove the item from the trash if it is found however, so that if // other clients remove it (via Sync say) then it is gone and doesn't need to // be processed further by |WebDataService|. - EXPECT_TRUE(db.GetAutofillTable()->RemoveAutofillProfile(profile.guid())); + EXPECT_TRUE(table_->RemoveAutofillProfile(profile.guid())); AutofillProfile* removed_profile = NULL; - EXPECT_TRUE(db.GetAutofillTable()->GetAutofillProfile(profile.guid(), - &removed_profile)); - EXPECT_FALSE(db.GetAutofillTable()->IsAutofillGUIDInTrash(profile.guid())); + EXPECT_TRUE(table_->GetAutofillProfile(profile.guid(), &removed_profile)); + EXPECT_FALSE(table_->IsAutofillGUIDInTrash(profile.guid())); ASSERT_NE(static_cast<AutofillProfile*>(NULL), removed_profile); delete removed_profile; // Check that emptying the trash now allows removal to occur. - EXPECT_TRUE(db.GetAutofillTable()->EmptyAutofillProfilesTrash()); - EXPECT_TRUE(db.GetAutofillTable()->RemoveAutofillProfile(profile.guid())); + EXPECT_TRUE(table_->EmptyAutofillProfilesTrash()); + EXPECT_TRUE(table_->RemoveAutofillProfile(profile.guid())); removed_profile = NULL; - EXPECT_FALSE(db.GetAutofillTable()->GetAutofillProfile(profile.guid(), - &removed_profile)); + EXPECT_FALSE(table_->GetAutofillProfile(profile.guid(), &removed_profile)); EXPECT_EQ(static_cast<AutofillProfile*>(NULL), removed_profile); } TEST_F(AutofillTableTest, CreditCard) { - WebDatabase db; - - ASSERT_EQ(sql::INIT_OK, db.Init(file_, std::string())); - // Add a 'Work' credit card. CreditCard work_creditcard; work_creditcard.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Jack Torrance")); @@ -936,15 +860,14 @@ TEST_F(AutofillTableTest, CreditCard) { ASCIIToUTF16("2013")); Time pre_creation_time = Time::Now(); - EXPECT_TRUE(db.GetAutofillTable()->AddCreditCard(work_creditcard)); + EXPECT_TRUE(table_->AddCreditCard(work_creditcard)); Time post_creation_time = Time::Now(); // Get the 'Work' credit card. CreditCard* db_creditcard; - ASSERT_TRUE(db.GetAutofillTable()->GetCreditCard(work_creditcard.guid(), - &db_creditcard)); + ASSERT_TRUE(table_->GetCreditCard(work_creditcard.guid(), &db_creditcard)); EXPECT_EQ(work_creditcard, *db_creditcard); - sql::Statement s_work(db.GetSQLConnection()->GetUniqueStatement( + sql::Statement s_work(db_->GetSQLConnection()->GetUniqueStatement( "SELECT guid, name_on_card, expiration_month, expiration_year, " "card_number_encrypted, date_modified " "FROM credit_cards WHERE guid=?")); @@ -966,12 +889,11 @@ TEST_F(AutofillTableTest, CreditCard) { ASCIIToUTF16("2012")); pre_creation_time = Time::Now(); - EXPECT_TRUE(db.GetAutofillTable()->AddCreditCard(target_creditcard)); + EXPECT_TRUE(table_->AddCreditCard(target_creditcard)); post_creation_time = Time::Now(); - ASSERT_TRUE(db.GetAutofillTable()->GetCreditCard(target_creditcard.guid(), - &db_creditcard)); + ASSERT_TRUE(table_->GetCreditCard(target_creditcard.guid(), &db_creditcard)); EXPECT_EQ(target_creditcard, *db_creditcard); - sql::Statement s_target(db.GetSQLConnection()->GetUniqueStatement( + sql::Statement s_target(db_->GetSQLConnection()->GetUniqueStatement( "SELECT guid, name_on_card, expiration_month, expiration_year, " "card_number_encrypted, date_modified " "FROM credit_cards WHERE guid=?")); @@ -986,12 +908,11 @@ TEST_F(AutofillTableTest, CreditCard) { // Update the 'Target' credit card. target_creditcard.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Charles Grady")); Time pre_modification_time = Time::Now(); - EXPECT_TRUE(db.GetAutofillTable()->UpdateCreditCard(target_creditcard)); + EXPECT_TRUE(table_->UpdateCreditCard(target_creditcard)); Time post_modification_time = Time::Now(); - ASSERT_TRUE(db.GetAutofillTable()->GetCreditCard(target_creditcard.guid(), - &db_creditcard)); + ASSERT_TRUE(table_->GetCreditCard(target_creditcard.guid(), &db_creditcard)); EXPECT_EQ(target_creditcard, *db_creditcard); - sql::Statement s_target_updated(db.GetSQLConnection()->GetUniqueStatement( + sql::Statement s_target_updated(db_->GetSQLConnection()->GetUniqueStatement( "SELECT guid, name_on_card, expiration_month, expiration_year, " "card_number_encrypted, date_modified " "FROM credit_cards WHERE guid=?")); @@ -1004,16 +925,11 @@ TEST_F(AutofillTableTest, CreditCard) { delete db_creditcard; // Remove the 'Target' credit card. - EXPECT_TRUE(db.GetAutofillTable()->RemoveCreditCard( - target_creditcard.guid())); - EXPECT_FALSE(db.GetAutofillTable()->GetCreditCard(target_creditcard.guid(), - &db_creditcard)); + EXPECT_TRUE(table_->RemoveCreditCard(target_creditcard.guid())); + EXPECT_FALSE(table_->GetCreditCard(target_creditcard.guid(), &db_creditcard)); } TEST_F(AutofillTableTest, UpdateAutofillProfile) { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(file_, std::string())); - // Add a profile to the db. AutofillProfile profile; profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("John")); @@ -1028,23 +944,23 @@ TEST_F(AutofillTableTest, UpdateAutofillProfile) { profile.SetRawInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("90025")); profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US")); profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("18181234567")); - db.GetAutofillTable()->AddAutofillProfile(profile); + table_->AddAutofillProfile(profile); // Set a mocked value for the profile's creation time. const time_t mock_creation_date = Time::Now().ToTimeT() - 13; - sql::Statement s_mock_creation_date(db.GetSQLConnection()->GetUniqueStatement( - "UPDATE autofill_profiles SET date_modified = ?")); + sql::Statement s_mock_creation_date( + db_->GetSQLConnection()->GetUniqueStatement( + "UPDATE autofill_profiles SET date_modified = ?")); ASSERT_TRUE(s_mock_creation_date.is_valid()); s_mock_creation_date.BindInt64(0, mock_creation_date); ASSERT_TRUE(s_mock_creation_date.Run()); // Get the profile. AutofillProfile* tmp_profile; - ASSERT_TRUE(db.GetAutofillTable()->GetAutofillProfile(profile.guid(), - &tmp_profile)); + ASSERT_TRUE(table_->GetAutofillProfile(profile.guid(), &tmp_profile)); scoped_ptr<AutofillProfile> db_profile(tmp_profile); EXPECT_EQ(profile, *db_profile); - sql::Statement s_original(db.GetSQLConnection()->GetUniqueStatement( + sql::Statement s_original(db_->GetSQLConnection()->GetUniqueStatement( "SELECT date_modified FROM autofill_profiles")); ASSERT_TRUE(s_original.is_valid()); ASSERT_TRUE(s_original.Step()); @@ -1054,14 +970,13 @@ TEST_F(AutofillTableTest, UpdateAutofillProfile) { // Now, update the profile and save the update to the database. // The modification date should change to reflect the update. profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("js@smith.xyz")); - db.GetAutofillTable()->UpdateAutofillProfileMulti(profile); + table_->UpdateAutofillProfileMulti(profile); // Get the profile. - ASSERT_TRUE(db.GetAutofillTable()->GetAutofillProfile(profile.guid(), - &tmp_profile)); + ASSERT_TRUE(table_->GetAutofillProfile(profile.guid(), &tmp_profile)); db_profile.reset(tmp_profile); EXPECT_EQ(profile, *db_profile); - sql::Statement s_updated(db.GetSQLConnection()->GetUniqueStatement( + sql::Statement s_updated(db_->GetSQLConnection()->GetUniqueStatement( "SELECT date_modified FROM autofill_profiles")); ASSERT_TRUE(s_updated.is_valid()); ASSERT_TRUE(s_updated.Step()); @@ -1071,7 +986,7 @@ TEST_F(AutofillTableTest, UpdateAutofillProfile) { // Set a mocked value for the profile's modification time. const time_t mock_modification_date = Time::Now().ToTimeT() - 7; sql::Statement s_mock_modification_date( - db.GetSQLConnection()->GetUniqueStatement( + db_->GetSQLConnection()->GetUniqueStatement( "UPDATE autofill_profiles SET date_modified = ?")); ASSERT_TRUE(s_mock_modification_date.is_valid()); s_mock_modification_date.BindInt64(0, mock_modification_date); @@ -1079,14 +994,13 @@ TEST_F(AutofillTableTest, UpdateAutofillProfile) { // Finally, call into |UpdateAutofillProfileMulti()| without changing the // profile. The modification date should not change. - db.GetAutofillTable()->UpdateAutofillProfileMulti(profile); + table_->UpdateAutofillProfileMulti(profile); // Get the profile. - ASSERT_TRUE(db.GetAutofillTable()->GetAutofillProfile(profile.guid(), - &tmp_profile)); + ASSERT_TRUE(table_->GetAutofillProfile(profile.guid(), &tmp_profile)); db_profile.reset(tmp_profile); EXPECT_EQ(profile, *db_profile); - sql::Statement s_unchanged(db.GetSQLConnection()->GetUniqueStatement( + sql::Statement s_unchanged(db_->GetSQLConnection()->GetUniqueStatement( "SELECT date_modified FROM autofill_profiles")); ASSERT_TRUE(s_unchanged.is_valid()); ASSERT_TRUE(s_unchanged.Step()); @@ -1095,32 +1009,29 @@ TEST_F(AutofillTableTest, UpdateAutofillProfile) { } TEST_F(AutofillTableTest, UpdateCreditCard) { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(file_, std::string())); - // Add a credit card to the db. CreditCard credit_card; credit_card.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("Jack Torrance")); credit_card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("1234567890123456")); credit_card.SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("04")); credit_card.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, ASCIIToUTF16("2013")); - db.GetAutofillTable()->AddCreditCard(credit_card); + table_->AddCreditCard(credit_card); // Set a mocked value for the credit card's creation time. const time_t mock_creation_date = Time::Now().ToTimeT() - 13; - sql::Statement s_mock_creation_date(db.GetSQLConnection()->GetUniqueStatement( - "UPDATE credit_cards SET date_modified = ?")); + sql::Statement s_mock_creation_date( + db_->GetSQLConnection()->GetUniqueStatement( + "UPDATE credit_cards SET date_modified = ?")); ASSERT_TRUE(s_mock_creation_date.is_valid()); s_mock_creation_date.BindInt64(0, mock_creation_date); ASSERT_TRUE(s_mock_creation_date.Run()); // Get the credit card. CreditCard* tmp_credit_card; - ASSERT_TRUE(db.GetAutofillTable()->GetCreditCard(credit_card.guid(), - &tmp_credit_card)); + ASSERT_TRUE(table_->GetCreditCard(credit_card.guid(), &tmp_credit_card)); scoped_ptr<CreditCard> db_credit_card(tmp_credit_card); EXPECT_EQ(credit_card, *db_credit_card); - sql::Statement s_original(db.GetSQLConnection()->GetUniqueStatement( + sql::Statement s_original(db_->GetSQLConnection()->GetUniqueStatement( "SELECT date_modified FROM credit_cards")); ASSERT_TRUE(s_original.is_valid()); ASSERT_TRUE(s_original.Step()); @@ -1130,14 +1041,13 @@ TEST_F(AutofillTableTest, UpdateCreditCard) { // Now, update the credit card and save the update to the database. // The modification date should change to reflect the update. credit_card.SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("01")); - db.GetAutofillTable()->UpdateCreditCard(credit_card); + table_->UpdateCreditCard(credit_card); // Get the credit card. - ASSERT_TRUE(db.GetAutofillTable()->GetCreditCard(credit_card.guid(), - &tmp_credit_card)); + ASSERT_TRUE(table_->GetCreditCard(credit_card.guid(), &tmp_credit_card)); db_credit_card.reset(tmp_credit_card); EXPECT_EQ(credit_card, *db_credit_card); - sql::Statement s_updated(db.GetSQLConnection()->GetUniqueStatement( + sql::Statement s_updated(db_->GetSQLConnection()->GetUniqueStatement( "SELECT date_modified FROM credit_cards")); ASSERT_TRUE(s_updated.is_valid()); ASSERT_TRUE(s_updated.Step()); @@ -1147,7 +1057,7 @@ TEST_F(AutofillTableTest, UpdateCreditCard) { // Set a mocked value for the credit card's modification time. const time_t mock_modification_date = Time::Now().ToTimeT() - 7; sql::Statement s_mock_modification_date( - db.GetSQLConnection()->GetUniqueStatement( + db_->GetSQLConnection()->GetUniqueStatement( "UPDATE credit_cards SET date_modified = ?")); ASSERT_TRUE(s_mock_modification_date.is_valid()); s_mock_modification_date.BindInt64(0, mock_modification_date); @@ -1155,14 +1065,13 @@ TEST_F(AutofillTableTest, UpdateCreditCard) { // Finally, call into |UpdateCreditCard()| without changing the credit card. // The modification date should not change. - db.GetAutofillTable()->UpdateCreditCard(credit_card); + table_->UpdateCreditCard(credit_card); // Get the profile. - ASSERT_TRUE(db.GetAutofillTable()->GetCreditCard(credit_card.guid(), - &tmp_credit_card)); + ASSERT_TRUE(table_->GetCreditCard(credit_card.guid(), &tmp_credit_card)); db_credit_card.reset(tmp_credit_card); EXPECT_EQ(credit_card, *db_credit_card); - sql::Statement s_unchanged(db.GetSQLConnection()->GetUniqueStatement( + sql::Statement s_unchanged(db_->GetSQLConnection()->GetUniqueStatement( "SELECT date_modified FROM credit_cards")); ASSERT_TRUE(s_unchanged.is_valid()); ASSERT_TRUE(s_unchanged.Step()); @@ -1171,11 +1080,8 @@ TEST_F(AutofillTableTest, UpdateCreditCard) { } TEST_F(AutofillTableTest, RemoveAutofillProfilesAndCreditCardsModifiedBetween) { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(file_, std::string())); - // Populate the autofill_profiles and credit_cards tables. - ASSERT_TRUE(db.GetSQLConnection()->Execute( + ASSERT_TRUE(db_->GetSQLConnection()->Execute( "INSERT INTO autofill_profiles (guid, date_modified) " "VALUES('00000000-0000-0000-0000-000000000000', 11);" "INSERT INTO autofill_profiles (guid, date_modified) " @@ -1204,14 +1110,14 @@ TEST_F(AutofillTableTest, RemoveAutofillProfilesAndCreditCardsModifiedBetween) { // Remove all entries modified in the bounded time range [17,41). std::vector<std::string> profile_guids; std::vector<std::string> credit_card_guids; - db.GetAutofillTable()->RemoveAutofillProfilesAndCreditCardsModifiedBetween( + table_->RemoveAutofillProfilesAndCreditCardsModifiedBetween( Time::FromTimeT(17), Time::FromTimeT(41), &profile_guids, &credit_card_guids); ASSERT_EQ(2UL, profile_guids.size()); EXPECT_EQ("00000000-0000-0000-0000-000000000001", profile_guids[0]); EXPECT_EQ("00000000-0000-0000-0000-000000000002", profile_guids[1]); sql::Statement s_autofill_profiles_bounded( - db.GetSQLConnection()->GetUniqueStatement( + db_->GetSQLConnection()->GetUniqueStatement( "SELECT date_modified FROM autofill_profiles")); ASSERT_TRUE(s_autofill_profiles_bounded.is_valid()); ASSERT_TRUE(s_autofill_profiles_bounded.Step()); @@ -1228,7 +1134,7 @@ TEST_F(AutofillTableTest, RemoveAutofillProfilesAndCreditCardsModifiedBetween) { EXPECT_EQ("00000000-0000-0000-0000-000000000007", credit_card_guids[1]); EXPECT_EQ("00000000-0000-0000-0000-000000000008", credit_card_guids[2]); sql::Statement s_credit_cards_bounded( - db.GetSQLConnection()->GetUniqueStatement( + db_->GetSQLConnection()->GetUniqueStatement( "SELECT date_modified FROM credit_cards")); ASSERT_TRUE(s_credit_cards_bounded.is_valid()); ASSERT_TRUE(s_credit_cards_bounded.Step()); @@ -1240,14 +1146,14 @@ TEST_F(AutofillTableTest, RemoveAutofillProfilesAndCreditCardsModifiedBetween) { EXPECT_FALSE(s_credit_cards_bounded.Step()); // Remove all entries modified on or after time 51 (unbounded range). - db.GetAutofillTable()->RemoveAutofillProfilesAndCreditCardsModifiedBetween( + table_->RemoveAutofillProfilesAndCreditCardsModifiedBetween( Time::FromTimeT(51), Time(), &profile_guids, &credit_card_guids); ASSERT_EQ(2UL, profile_guids.size()); EXPECT_EQ("00000000-0000-0000-0000-000000000004", profile_guids[0]); EXPECT_EQ("00000000-0000-0000-0000-000000000005", profile_guids[1]); sql::Statement s_autofill_profiles_unbounded( - db.GetSQLConnection()->GetUniqueStatement( + db_->GetSQLConnection()->GetUniqueStatement( "SELECT date_modified FROM autofill_profiles")); ASSERT_TRUE(s_autofill_profiles_unbounded.is_valid()); ASSERT_TRUE(s_autofill_profiles_unbounded.Step()); @@ -1259,7 +1165,7 @@ TEST_F(AutofillTableTest, RemoveAutofillProfilesAndCreditCardsModifiedBetween) { EXPECT_EQ("00000000-0000-0000-0000-000000000010", credit_card_guids[0]); EXPECT_EQ("00000000-0000-0000-0000-000000000011", credit_card_guids[1]); sql::Statement s_credit_cards_unbounded( - db.GetSQLConnection()->GetUniqueStatement( + db_->GetSQLConnection()->GetUniqueStatement( "SELECT date_modified FROM credit_cards")); ASSERT_TRUE(s_credit_cards_unbounded.is_valid()); ASSERT_TRUE(s_credit_cards_unbounded.Step()); @@ -1267,42 +1173,34 @@ TEST_F(AutofillTableTest, RemoveAutofillProfilesAndCreditCardsModifiedBetween) { EXPECT_FALSE(s_credit_cards_unbounded.Step()); // Remove all remaining entries. - db.GetAutofillTable()->RemoveAutofillProfilesAndCreditCardsModifiedBetween( + table_->RemoveAutofillProfilesAndCreditCardsModifiedBetween( Time(), Time(), &profile_guids, &credit_card_guids); ASSERT_EQ(2UL, profile_guids.size()); EXPECT_EQ("00000000-0000-0000-0000-000000000000", profile_guids[0]); EXPECT_EQ("00000000-0000-0000-0000-000000000003", profile_guids[1]); sql::Statement s_autofill_profiles_empty( - db.GetSQLConnection()->GetUniqueStatement( + db_->GetSQLConnection()->GetUniqueStatement( "SELECT date_modified FROM autofill_profiles")); ASSERT_TRUE(s_autofill_profiles_empty.is_valid()); EXPECT_FALSE(s_autofill_profiles_empty.Step()); ASSERT_EQ(1UL, credit_card_guids.size()); EXPECT_EQ("00000000-0000-0000-0000-000000000009", credit_card_guids[0]); sql::Statement s_credit_cards_empty( - db.GetSQLConnection()->GetUniqueStatement( + db_->GetSQLConnection()->GetUniqueStatement( "SELECT date_modified FROM credit_cards")); ASSERT_TRUE(s_credit_cards_empty.is_valid()); EXPECT_FALSE(s_credit_cards_empty.Step()); } TEST_F(AutofillTableTest, Autofill_GetAllAutofillEntries_NoResults) { - WebDatabase db; - - ASSERT_EQ(sql::INIT_OK, db.Init(file_, std::string())); - std::vector<AutofillEntry> entries; - ASSERT_TRUE(db.GetAutofillTable()->GetAllAutofillEntries(&entries)); + ASSERT_TRUE(table_->GetAllAutofillEntries(&entries)); EXPECT_EQ(0U, entries.size()); } TEST_F(AutofillTableTest, Autofill_GetAllAutofillEntries_OneResult) { - WebDatabase db; - - ASSERT_EQ(sql::INIT_OK, db.Init(file_, std::string())); - AutofillChangeList changes; std::map<std::string, std::vector<Time> > name_value_times_map; @@ -1311,9 +1209,8 @@ TEST_F(AutofillTableTest, Autofill_GetAllAutofillEntries_OneResult) { FormFieldData field; field.name = ASCIIToUTF16("Name"); field.value = ASCIIToUTF16("Superman"); - EXPECT_TRUE( - db.GetAutofillTable()->AddFormFieldValueTime(field, &changes, - Time::FromTimeT(start))); + EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, + Time::FromTimeT(start))); timestamps1.push_back(Time::FromTimeT(start)); std::string key1("NameSuperman"); name_value_times_map.insert(std::pair<std::string, @@ -1326,7 +1223,7 @@ TEST_F(AutofillTableTest, Autofill_GetAllAutofillEntries_OneResult) { expected_entries.insert(ae1); std::vector<AutofillEntry> entries; - ASSERT_TRUE(db.GetAutofillTable()->GetAllAutofillEntries(&entries)); + ASSERT_TRUE(table_->GetAllAutofillEntries(&entries)); AutofillEntrySet entry_set(entries.begin(), entries.end(), CompareAutofillEntries); @@ -1341,10 +1238,6 @@ TEST_F(AutofillTableTest, Autofill_GetAllAutofillEntries_OneResult) { } TEST_F(AutofillTableTest, Autofill_GetAllAutofillEntries_TwoDistinct) { - WebDatabase db; - - ASSERT_EQ(sql::INIT_OK, db.Init(file_, std::string())); - AutofillChangeList changes; std::map<std::string, std::vector<Time> > name_value_times_map; time_t start = 0; @@ -1353,9 +1246,8 @@ TEST_F(AutofillTableTest, Autofill_GetAllAutofillEntries_TwoDistinct) { FormFieldData field; field.name = ASCIIToUTF16("Name"); field.value = ASCIIToUTF16("Superman"); - EXPECT_TRUE( - db.GetAutofillTable()->AddFormFieldValueTime(field, &changes, - Time::FromTimeT(start))); + EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, + Time::FromTimeT(start))); timestamps1.push_back(Time::FromTimeT(start)); std::string key1("NameSuperman"); name_value_times_map.insert(std::pair<std::string, @@ -1365,9 +1257,8 @@ TEST_F(AutofillTableTest, Autofill_GetAllAutofillEntries_TwoDistinct) { std::vector<Time> timestamps2; field.name = ASCIIToUTF16("Name"); field.value = ASCIIToUTF16("Clark Kent"); - EXPECT_TRUE( - db.GetAutofillTable()->AddFormFieldValueTime(field, &changes, - Time::FromTimeT(start))); + EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, + Time::FromTimeT(start))); timestamps2.push_back(Time::FromTimeT(start)); std::string key2("NameClark Kent"); name_value_times_map.insert(std::pair<std::string, @@ -1383,7 +1274,7 @@ TEST_F(AutofillTableTest, Autofill_GetAllAutofillEntries_TwoDistinct) { expected_entries.insert(ae2); std::vector<AutofillEntry> entries; - ASSERT_TRUE(db.GetAutofillTable()->GetAllAutofillEntries(&entries)); + ASSERT_TRUE(table_->GetAllAutofillEntries(&entries)); AutofillEntrySet entry_set(entries.begin(), entries.end(), CompareAutofillEntries); @@ -1398,10 +1289,6 @@ TEST_F(AutofillTableTest, Autofill_GetAllAutofillEntries_TwoDistinct) { } TEST_F(AutofillTableTest, Autofill_GetAllAutofillEntries_TwoSame) { - WebDatabase db; - - ASSERT_EQ(sql::INIT_OK, db.Init(file_, std::string())); - AutofillChangeList changes; std::map<std::string, std::vector<Time> > name_value_times_map; @@ -1411,9 +1298,8 @@ TEST_F(AutofillTableTest, Autofill_GetAllAutofillEntries_TwoSame) { FormFieldData field; field.name = ASCIIToUTF16("Name"); field.value = ASCIIToUTF16("Superman"); - EXPECT_TRUE( - db.GetAutofillTable()->AddFormFieldValueTime(field, &changes, - Time::FromTimeT(start))); + EXPECT_TRUE(table_->AddFormFieldValueTime(field, &changes, + Time::FromTimeT(start))); timestamps.push_back(Time::FromTimeT(start)); start++; } @@ -1429,7 +1315,7 @@ TEST_F(AutofillTableTest, Autofill_GetAllAutofillEntries_TwoSame) { expected_entries.insert(ae1); std::vector<AutofillEntry> entries; - ASSERT_TRUE(db.GetAutofillTable()->GetAllAutofillEntries(&entries)); + ASSERT_TRUE(table_->GetAllAutofillEntries(&entries)); AutofillEntrySet entry_set(entries.begin(), entries.end(), CompareAutofillEntries); diff --git a/chrome/browser/webdata/keyword_table.cc b/chrome/browser/webdata/keyword_table.cc index 15fba41..d412d84 100644 --- a/chrome/browser/webdata/keyword_table.cc +++ b/chrome/browser/webdata/keyword_table.cc @@ -116,15 +116,29 @@ void BindURLToStatement(const TemplateURLData& data, s->BindString(starting_column + 17, data.search_terms_replacement_key); } -} // anonymous namespace +int table_key = 0; -KeywordTable::KeywordTable(sql::Connection* db, sql::MetaTable* meta_table) - : WebDatabaseTable(db, meta_table) { +WebDatabaseTable::TypeKey GetKey() { + return reinterpret_cast<void*>(&table_key); +} + +} // namespace + +KeywordTable::KeywordTable() { } KeywordTable::~KeywordTable() {} -bool KeywordTable::Init() { +KeywordTable* KeywordTable::FromWebDatabase(WebDatabase* db) { + return static_cast<KeywordTable*>(db->GetTable(GetKey())); +} + +WebDatabaseTable::TypeKey KeywordTable::GetTypeKey() const { + return GetKey(); +} + +bool KeywordTable::Init(sql::Connection* db, sql::MetaTable* meta_table) { + WebDatabaseTable::Init(db, meta_table); return db_->DoesTableExist("keywords") || db_->Execute("CREATE TABLE keywords (" "id INTEGER PRIMARY KEY," diff --git a/chrome/browser/webdata/keyword_table.h b/chrome/browser/webdata/keyword_table.h index 4e3eb9c..5cee197 100644 --- a/chrome/browser/webdata/keyword_table.h +++ b/chrome/browser/webdata/keyword_table.h @@ -15,6 +15,7 @@ #include "chrome/browser/search_engines/template_url_id.h" struct TemplateURLData; +class WebDatabase; namespace sql { class Statement; @@ -71,9 +72,14 @@ class KeywordTable : public WebDatabaseTable { static const char kDefaultSearchProviderKey[]; - KeywordTable(sql::Connection* db, sql::MetaTable* meta_table); + KeywordTable(); virtual ~KeywordTable(); - virtual bool Init() OVERRIDE; + + // Retrieves the KeywordTable* owned by |database|. + static KeywordTable* FromWebDatabase(WebDatabase* db); + + virtual WebDatabaseTable::TypeKey GetTypeKey() const OVERRIDE; + virtual bool Init(sql::Connection* db, sql::MetaTable* meta_table) OVERRIDE; virtual bool IsSyncable() OVERRIDE; virtual bool MigrateToVersion(int version, const std::string& app_locale, diff --git a/chrome/browser/webdata/keyword_table_unittest.cc b/chrome/browser/webdata/keyword_table_unittest.cc index b848302..3af911b 100644 --- a/chrome/browser/webdata/keyword_table_unittest.cc +++ b/chrome/browser/webdata/keyword_table_unittest.cc @@ -31,10 +31,17 @@ class KeywordTableTest : public testing::Test { virtual void SetUp() { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); file_ = temp_dir_.path().AppendASCII("TestWebDatabase"); + + table_.reset(new KeywordTable); + db_.reset(new WebDatabase); + db_->AddTable(table_.get()); + ASSERT_EQ(sql::INIT_OK, db_->Init(file_, std::string())); } base::FilePath file_; base::ScopedTempDir temp_dir_; + scoped_ptr<KeywordTable> table_; + scoped_ptr<WebDatabase> db_; private: DISALLOW_COPY_AND_ASSIGN(KeywordTableTest); @@ -42,10 +49,6 @@ class KeywordTableTest : public testing::Test { TEST_F(KeywordTableTest, Keywords) { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(file_, std::string())); - KeywordTable* keyword_table = db.GetKeywordTable(); - TemplateURLData keyword; keyword.short_name = ASCIIToUTF16("short_name"); keyword.SetKeyword(ASCIIToUTF16("keyword")); @@ -63,10 +66,10 @@ TEST_F(KeywordTableTest, Keywords) { keyword.created_by_policy = true; keyword.usage_count = 32; keyword.prepopulate_id = 10; - EXPECT_TRUE(keyword_table->AddKeyword(keyword)); + EXPECT_TRUE(table_->AddKeyword(keyword)); KeywordTable::Keywords keywords; - EXPECT_TRUE(keyword_table->GetKeywords(&keywords)); + EXPECT_TRUE(table_->GetKeywords(&keywords)); EXPECT_EQ(1U, keywords.size()); const TemplateURLData& restored_keyword = keywords.front(); @@ -92,20 +95,16 @@ TEST_F(KeywordTableTest, Keywords) { EXPECT_EQ(keyword.usage_count, restored_keyword.usage_count); EXPECT_EQ(keyword.prepopulate_id, restored_keyword.prepopulate_id); - EXPECT_TRUE(keyword_table->RemoveKeyword(restored_keyword.id)); + EXPECT_TRUE(table_->RemoveKeyword(restored_keyword.id)); KeywordTable::Keywords empty_keywords; - EXPECT_TRUE(keyword_table->GetKeywords(&empty_keywords)); + EXPECT_TRUE(table_->GetKeywords(&empty_keywords)); EXPECT_EQ(0U, empty_keywords.size()); } TEST_F(KeywordTableTest, KeywordMisc) { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(file_, std::string())); - KeywordTable* keyword_table = db.GetKeywordTable(); - - EXPECT_EQ(kInvalidTemplateURLID, keyword_table->GetDefaultSearchProviderID()); - EXPECT_EQ(0, keyword_table->GetBuiltinKeywordVersion()); + EXPECT_EQ(kInvalidTemplateURLID, table_->GetDefaultSearchProviderID()); + EXPECT_EQ(0, table_->GetBuiltinKeywordVersion()); TemplateURLData keyword; keyword.short_name = ASCIIToUTF16("short_name"); @@ -124,20 +123,16 @@ TEST_F(KeywordTableTest, KeywordMisc) { keyword.created_by_policy = true; keyword.usage_count = 32; keyword.prepopulate_id = 10; - EXPECT_TRUE(keyword_table->AddKeyword(keyword)); + EXPECT_TRUE(table_->AddKeyword(keyword)); - EXPECT_TRUE(keyword_table->SetDefaultSearchProviderID(10)); - EXPECT_TRUE(keyword_table->SetBuiltinKeywordVersion(11)); + EXPECT_TRUE(table_->SetDefaultSearchProviderID(10)); + EXPECT_TRUE(table_->SetBuiltinKeywordVersion(11)); - EXPECT_EQ(10, keyword_table->GetDefaultSearchProviderID()); - EXPECT_EQ(11, keyword_table->GetBuiltinKeywordVersion()); + EXPECT_EQ(10, table_->GetDefaultSearchProviderID()); + EXPECT_EQ(11, table_->GetBuiltinKeywordVersion()); } TEST_F(KeywordTableTest, GetTableContents) { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(file_, std::string())); - KeywordTable* keyword_table = db.GetKeywordTable(); - TemplateURLData keyword; keyword.short_name = ASCIIToUTF16("short_name"); keyword.SetKeyword(ASCIIToUTF16("keyword")); @@ -153,7 +148,7 @@ TEST_F(KeywordTableTest, GetTableContents) { keyword.alternate_urls.push_back("a_url1"); keyword.alternate_urls.push_back("a_url2"); keyword.search_terms_replacement_key = "espv"; - EXPECT_TRUE(keyword_table->AddKeyword(keyword)); + EXPECT_TRUE(table_->AddKeyword(keyword)); keyword.SetKeyword(ASCIIToUTF16("url")); keyword.instant_url = "http://instant2/"; @@ -164,7 +159,7 @@ TEST_F(KeywordTableTest, GetTableContents) { keyword.sync_guid = "FEDC-BA09-8765-4321"; keyword.alternate_urls.clear(); keyword.search_terms_replacement_key.clear(); - EXPECT_TRUE(keyword_table->AddKeyword(keyword)); + EXPECT_TRUE(table_->AddKeyword(keyword)); const char kTestContents[] = "1short_namekeywordhttp://favicon.url/" "http://url/1001url20001234-5678-90AB-CDEF[\"a_url1\",\"a_url2\"]espv" @@ -172,16 +167,12 @@ TEST_F(KeywordTableTest, GetTableContents) { "Shift_JIS1url250http://instant2/0FEDC-BA09-8765-4321[]"; std::string contents; - EXPECT_TRUE(keyword_table->GetTableContents("keywords", + EXPECT_TRUE(table_->GetTableContents("keywords", WebDatabase::kCurrentVersionNumber, &contents)); EXPECT_EQ(kTestContents, contents); } TEST_F(KeywordTableTest, GetTableContentsOrdering) { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(file_, std::string())); - KeywordTable* keyword_table = db.GetKeywordTable(); - TemplateURLData keyword; keyword.short_name = ASCIIToUTF16("short_name"); keyword.SetKeyword(ASCIIToUTF16("keyword")); @@ -197,7 +188,7 @@ TEST_F(KeywordTableTest, GetTableContentsOrdering) { keyword.alternate_urls.push_back("a_url1"); keyword.alternate_urls.push_back("a_url2"); keyword.search_terms_replacement_key = "espv"; - EXPECT_TRUE(keyword_table->AddKeyword(keyword)); + EXPECT_TRUE(table_->AddKeyword(keyword)); keyword.SetKeyword(ASCIIToUTF16("url")); keyword.instant_url = "http://instant2/"; @@ -208,7 +199,7 @@ TEST_F(KeywordTableTest, GetTableContentsOrdering) { keyword.sync_guid = "FEDC-BA09-8765-4321"; keyword.alternate_urls.clear(); keyword.search_terms_replacement_key.clear(); - EXPECT_TRUE(keyword_table->AddKeyword(keyword)); + EXPECT_TRUE(table_->AddKeyword(keyword)); const char kTestContents[] = "1short_nameurlhttp://favicon.url/http://url/1" "http://originating.url/00Shift_JIS1url250http://instant2/0" @@ -217,16 +208,12 @@ TEST_F(KeywordTableTest, GetTableContentsOrdering) { "url20001234-5678-90AB-CDEF[\"a_url1\",\"a_url2\"]espv"; std::string contents; - EXPECT_TRUE(keyword_table->GetTableContents("keywords", + EXPECT_TRUE(table_->GetTableContents("keywords", WebDatabase::kCurrentVersionNumber, &contents)); EXPECT_EQ(kTestContents, contents); } TEST_F(KeywordTableTest, UpdateKeyword) { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(file_, std::string())); - KeywordTable* keyword_table = db.GetKeywordTable(); - TemplateURLData keyword; keyword.short_name = ASCIIToUTF16("short_name"); keyword.SetKeyword(ASCIIToUTF16("keyword")); @@ -236,17 +223,17 @@ TEST_F(KeywordTableTest, UpdateKeyword) { keyword.show_in_default_list = true; keyword.safe_for_autoreplace = true; keyword.id = 1; - EXPECT_TRUE(keyword_table->AddKeyword(keyword)); + EXPECT_TRUE(table_->AddKeyword(keyword)); keyword.SetKeyword(ASCIIToUTF16("url")); keyword.instant_url = "http://instant2/"; keyword.originating_url = GURL("http://originating.url/"); keyword.input_encodings.push_back("Shift_JIS"); keyword.prepopulate_id = 5; - EXPECT_TRUE(keyword_table->UpdateKeyword(keyword)); + EXPECT_TRUE(table_->UpdateKeyword(keyword)); KeywordTable::Keywords keywords; - EXPECT_TRUE(keyword_table->GetKeywords(&keywords)); + EXPECT_TRUE(table_->GetKeywords(&keywords)); EXPECT_EQ(1U, keywords.size()); const TemplateURLData& restored_keyword = keywords.front(); @@ -266,20 +253,16 @@ TEST_F(KeywordTableTest, UpdateKeyword) { } TEST_F(KeywordTableTest, KeywordWithNoFavicon) { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(file_, std::string())); - KeywordTable* keyword_table = db.GetKeywordTable(); - TemplateURLData keyword; keyword.short_name = ASCIIToUTF16("short_name"); keyword.SetKeyword(ASCIIToUTF16("keyword")); keyword.SetURL("http://url/"); keyword.safe_for_autoreplace = true; keyword.id = -100; - EXPECT_TRUE(keyword_table->AddKeyword(keyword)); + EXPECT_TRUE(table_->AddKeyword(keyword)); KeywordTable::Keywords keywords; - EXPECT_TRUE(keyword_table->GetKeywords(&keywords)); + EXPECT_TRUE(table_->GetKeywords(&keywords)); EXPECT_EQ(1U, keywords.size()); const TemplateURLData& restored_keyword = keywords.front(); @@ -292,36 +275,32 @@ TEST_F(KeywordTableTest, KeywordWithNoFavicon) { } TEST_F(KeywordTableTest, SanitizeURLs) { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(file_, std::string())); - KeywordTable* keyword_table = db.GetKeywordTable(); - TemplateURLData keyword; keyword.short_name = ASCIIToUTF16("legit"); keyword.SetKeyword(ASCIIToUTF16("legit")); keyword.SetURL("http://url/"); keyword.id = 1000; - EXPECT_TRUE(keyword_table->AddKeyword(keyword)); + EXPECT_TRUE(table_->AddKeyword(keyword)); keyword.short_name = ASCIIToUTF16("bogus"); keyword.SetKeyword(ASCIIToUTF16("bogus")); keyword.id = 2000; - EXPECT_TRUE(keyword_table->AddKeyword(keyword)); + EXPECT_TRUE(table_->AddKeyword(keyword)); KeywordTable::Keywords keywords; - EXPECT_TRUE(keyword_table->GetKeywords(&keywords)); + EXPECT_TRUE(table_->GetKeywords(&keywords)); EXPECT_EQ(2U, keywords.size()); keywords.clear(); // Erase the URL field for the second keyword to simulate having bogus data // previously saved into the database. - sql::Statement s(keyword_table->db_->GetUniqueStatement( + sql::Statement s(table_->db_->GetUniqueStatement( "UPDATE keywords SET url=? WHERE id=?")); s.BindString16(0, string16()); s.BindInt64(1, 2000); EXPECT_TRUE(s.Run()); // GetKeywords() should erase the entry with the empty URL field. - EXPECT_TRUE(keyword_table->GetKeywords(&keywords)); + EXPECT_TRUE(table_->GetKeywords(&keywords)); EXPECT_EQ(1U, keywords.size()); } diff --git a/chrome/browser/webdata/logins_table.cc b/chrome/browser/webdata/logins_table.cc index 35a1be2..e97bfdc 100644 --- a/chrome/browser/webdata/logins_table.cc +++ b/chrome/browser/webdata/logins_table.cc @@ -7,9 +7,30 @@ #include <limits> #include "base/logging.h" +#include "chrome/browser/webdata/web_database.h" #include "sql/statement.h" -bool LoginsTable::Init() { +namespace { + +int table_key = 0; + +WebDatabaseTable::TypeKey GetKey() { + return reinterpret_cast<void*>(&table_key); +} + +} // namespace + +LoginsTable* LoginsTable::FromWebDatabase(WebDatabase* db) { + return static_cast<LoginsTable*>(db->GetTable(GetKey())); +} + +WebDatabaseTable::TypeKey LoginsTable::GetTypeKey() const { + return GetKey(); +} + +bool LoginsTable::Init(sql::Connection* db, sql::MetaTable* meta_table) { + WebDatabaseTable::Init(db, meta_table); + if (db_->DoesTableExist("logins")) { // We don't check for success. It doesn't matter that much. // If we fail we'll just try again later anyway. diff --git a/chrome/browser/webdata/logins_table.h b/chrome/browser/webdata/logins_table.h index eece326..e035609 100644 --- a/chrome/browser/webdata/logins_table.h +++ b/chrome/browser/webdata/logins_table.h @@ -14,16 +14,22 @@ struct IE7PasswordInfo; #endif +class WebDatabase; + // This class manages the logins table within the SQLite database passed to the // constructor. We no longer store passwords here except for imported IE // passwords, so this class is now mostly responsible for deleting the table if // it is found to exist. (The data was migrated out long ago.) class LoginsTable : public WebDatabaseTable { public: - LoginsTable(sql::Connection* db, sql::MetaTable* meta_table) - : WebDatabaseTable(db, meta_table) {} + LoginsTable() {} virtual ~LoginsTable() {} - virtual bool Init() OVERRIDE; + + // Retrieves the LoginsTable* owned by |database|. + static LoginsTable* FromWebDatabase(WebDatabase* db); + + virtual WebDatabaseTable::TypeKey GetTypeKey() const OVERRIDE; + virtual bool Init(sql::Connection* db, sql::MetaTable* meta_table) OVERRIDE; virtual bool IsSyncable() OVERRIDE; virtual bool MigrateToVersion(int version, const std::string& app_locale, diff --git a/chrome/browser/webdata/token_service_table.cc b/chrome/browser/webdata/token_service_table.cc index df8f1f1..ee8410f 100644 --- a/chrome/browser/webdata/token_service_table.cc +++ b/chrome/browser/webdata/token_service_table.cc @@ -9,9 +9,30 @@ #include "base/logging.h" #include "chrome/browser/password_manager/encryptor.h" +#include "chrome/browser/webdata/web_database.h" #include "sql/statement.h" -bool TokenServiceTable::Init() { +namespace { + +int table_key = 0; + +WebDatabaseTable::TypeKey GetKey() { + return reinterpret_cast<void*>(&table_key); +} + +} // namespace + +TokenServiceTable* TokenServiceTable::FromWebDatabase(WebDatabase* db) { + return static_cast<TokenServiceTable*>(db->GetTable(GetKey())); + +} + +WebDatabaseTable::TypeKey TokenServiceTable::GetTypeKey() const { + return GetKey(); +} + +bool TokenServiceTable::Init(sql::Connection* db, sql::MetaTable* meta_table) { + WebDatabaseTable::Init(db, meta_table); if (!db_->DoesTableExist("token_service")) { if (!db_->Execute("CREATE TABLE token_service (" "service VARCHAR PRIMARY KEY NOT NULL," diff --git a/chrome/browser/webdata/token_service_table.h b/chrome/browser/webdata/token_service_table.h index 775ad9b..b75ec0b 100644 --- a/chrome/browser/webdata/token_service_table.h +++ b/chrome/browser/webdata/token_service_table.h @@ -11,12 +11,18 @@ #include "base/compiler_specific.h" #include "chrome/browser/webdata/web_database_table.h" +class WebDatabase; + class TokenServiceTable : public WebDatabaseTable { public: - TokenServiceTable(sql::Connection* db, sql::MetaTable* meta_table) - : WebDatabaseTable(db, meta_table) {} + TokenServiceTable() {} virtual ~TokenServiceTable() {} - virtual bool Init() OVERRIDE; + + // Retrieves the TokenServiceTable* owned by |database|. + static TokenServiceTable* FromWebDatabase(WebDatabase* db); + + virtual WebDatabaseTable::TypeKey GetTypeKey() const OVERRIDE; + virtual bool Init(sql::Connection* db, sql::MetaTable* meta_table) OVERRIDE; virtual bool IsSyncable() OVERRIDE; virtual bool MigrateToVersion(int version, const std::string& app_locale, diff --git a/chrome/browser/webdata/token_service_table_unittest.cc b/chrome/browser/webdata/token_service_table_unittest.cc index d7fec29..3676b9b 100644 --- a/chrome/browser/webdata/token_service_table_unittest.cc +++ b/chrome/browser/webdata/token_service_table_unittest.cc @@ -23,73 +23,71 @@ class TokenServiceTableTest : public testing::Test { virtual void SetUp() { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); file_ = temp_dir_.path().AppendASCII("TestWebDatabase"); + + table_.reset(new TokenServiceTable); + db_.reset(new WebDatabase); + db_->AddTable(table_.get()); + ASSERT_EQ(sql::INIT_OK, db_->Init(file_, std::string())); } base::FilePath file_; base::ScopedTempDir temp_dir_; - + scoped_ptr<TokenServiceTable> table_; + scoped_ptr<WebDatabase> db_; private: DISALLOW_COPY_AND_ASSIGN(TokenServiceTableTest); }; TEST_F(TokenServiceTableTest, TokenServiceGetAllRemoveAll) { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(file_, std::string())); - std::map<std::string, std::string> out_map; std::string service; std::string service2; service = "testservice"; service2 = "othertestservice"; - EXPECT_TRUE(db.GetTokenServiceTable()->GetAllTokens(&out_map)); + EXPECT_TRUE(table_->GetAllTokens(&out_map)); EXPECT_TRUE(out_map.empty()); // Check that get all tokens works - EXPECT_TRUE(db.GetTokenServiceTable()->SetTokenForService(service, - "pepperoni")); - EXPECT_TRUE(db.GetTokenServiceTable()->SetTokenForService(service2, "steak")); - EXPECT_TRUE(db.GetTokenServiceTable()->GetAllTokens(&out_map)); + EXPECT_TRUE(table_->SetTokenForService(service, "pepperoni")); + EXPECT_TRUE(table_->SetTokenForService(service2, "steak")); + EXPECT_TRUE(table_->GetAllTokens(&out_map)); EXPECT_EQ(out_map.find(service)->second, "pepperoni"); EXPECT_EQ(out_map.find(service2)->second, "steak"); out_map.clear(); // Purge - EXPECT_TRUE(db.GetTokenServiceTable()->RemoveAllTokens()); - EXPECT_TRUE(db.GetTokenServiceTable()->GetAllTokens(&out_map)); + EXPECT_TRUE(table_->RemoveAllTokens()); + EXPECT_TRUE(table_->GetAllTokens(&out_map)); EXPECT_TRUE(out_map.empty()); // Check that you can still add it back in - EXPECT_TRUE(db.GetTokenServiceTable()->SetTokenForService(service, "cheese")); - EXPECT_TRUE(db.GetTokenServiceTable()->GetAllTokens(&out_map)); + EXPECT_TRUE(table_->SetTokenForService(service, "cheese")); + EXPECT_TRUE(table_->GetAllTokens(&out_map)); EXPECT_EQ(out_map.find(service)->second, "cheese"); } TEST_F(TokenServiceTableTest, TokenServiceGetSet) { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(file_, std::string())); - std::map<std::string, std::string> out_map; std::string service; service = "testservice"; - EXPECT_TRUE(db.GetTokenServiceTable()->GetAllTokens(&out_map)); + EXPECT_TRUE(table_->GetAllTokens(&out_map)); EXPECT_TRUE(out_map.empty()); - EXPECT_TRUE(db.GetTokenServiceTable()->SetTokenForService(service, - "pepperoni")); - EXPECT_TRUE(db.GetTokenServiceTable()->GetAllTokens(&out_map)); + EXPECT_TRUE(table_->SetTokenForService(service, "pepperoni")); + EXPECT_TRUE(table_->GetAllTokens(&out_map)); EXPECT_EQ(out_map.find(service)->second, "pepperoni"); out_map.clear(); // try blanking it - won't remove it from the db though! - EXPECT_TRUE(db.GetTokenServiceTable()->SetTokenForService(service, "")); - EXPECT_TRUE(db.GetTokenServiceTable()->GetAllTokens(&out_map)); + EXPECT_TRUE(table_->SetTokenForService(service, "")); + EXPECT_TRUE(table_->GetAllTokens(&out_map)); EXPECT_EQ(out_map.find(service)->second, ""); out_map.clear(); // try mutating it - EXPECT_TRUE(db.GetTokenServiceTable()->SetTokenForService(service, "ham")); - EXPECT_TRUE(db.GetTokenServiceTable()->GetAllTokens(&out_map)); + EXPECT_TRUE(table_->SetTokenForService(service, "ham")); + EXPECT_TRUE(table_->GetAllTokens(&out_map)); EXPECT_EQ(out_map.find(service)->second, "ham"); } diff --git a/chrome/browser/webdata/web_apps_table.cc b/chrome/browser/webdata/web_apps_table.cc index 927b934..ffa8cb0 100644 --- a/chrome/browser/webdata/web_apps_table.cc +++ b/chrome/browser/webdata/web_apps_table.cc @@ -6,12 +6,33 @@ #include "base/logging.h" #include "chrome/browser/history/history_database.h" +#include "chrome/browser/webdata/web_database.h" #include "googleurl/src/gurl.h" #include "sql/statement.h" #include "third_party/skia/include/core/SkBitmap.h" #include "ui/gfx/codec/png_codec.h" -bool WebAppsTable::Init() { +namespace { + +int table_key = 0; + +WebDatabaseTable::TypeKey GetKey() { + return reinterpret_cast<void*>(&table_key); +} + +} // namespace + +WebAppsTable* WebAppsTable::FromWebDatabase(WebDatabase* db) { + return static_cast<WebAppsTable*>(db->GetTable(GetKey())); +} + +WebDatabaseTable::TypeKey WebAppsTable::GetTypeKey() const { + return GetKey(); +} + +bool WebAppsTable::Init(sql::Connection* db, sql::MetaTable* meta_table) { + WebDatabaseTable::Init(db, meta_table); + return (InitWebAppIconsTable() && InitWebAppsTable()); } diff --git a/chrome/browser/webdata/web_apps_table.h b/chrome/browser/webdata/web_apps_table.h index 82ebda3..1f80f50 100644 --- a/chrome/browser/webdata/web_apps_table.h +++ b/chrome/browser/webdata/web_apps_table.h @@ -12,6 +12,7 @@ class GURL; class SkBitmap; +class WebDatabase; // This class manages the WebApps tables within the SQLite database passed to // the constructor. It expects the following schema: @@ -30,10 +31,14 @@ class SkBitmap; // class WebAppsTable : public WebDatabaseTable { public: - WebAppsTable(sql::Connection* db, sql::MetaTable* meta_table) - : WebDatabaseTable(db, meta_table) {} + WebAppsTable() {} virtual ~WebAppsTable() {} - virtual bool Init() OVERRIDE; + + // Retrieves the WebAppsTable* owned by |database|. + static WebAppsTable* FromWebDatabase(WebDatabase* database); + + virtual WebDatabaseTable::TypeKey GetTypeKey() const OVERRIDE; + virtual bool Init(sql::Connection* db, sql::MetaTable* meta_table) OVERRIDE; virtual bool IsSyncable() OVERRIDE; virtual bool MigrateToVersion(int version, const std::string& app_locale, diff --git a/chrome/browser/webdata/web_apps_table_unittest.cc b/chrome/browser/webdata/web_apps_table_unittest.cc index 8d288bc..6434f82 100644 --- a/chrome/browser/webdata/web_apps_table_unittest.cc +++ b/chrome/browser/webdata/web_apps_table_unittest.cc @@ -25,10 +25,17 @@ class WebAppsTableTest : public testing::Test { virtual void SetUp() { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); file_ = temp_dir_.path().AppendASCII("TestWebDatabase"); + + table_.reset(new WebAppsTable); + db_.reset(new WebDatabase); + db_->AddTable(table_.get()); + ASSERT_EQ(sql::INIT_OK, db_->Init(file_, std::string())); } base::FilePath file_; base::ScopedTempDir temp_dir_; + scoped_ptr<WebAppsTable> table_; + scoped_ptr<WebDatabase> db_; private: DISALLOW_COPY_AND_ASSIGN(WebAppsTableTest); @@ -36,32 +43,26 @@ class WebAppsTableTest : public testing::Test { TEST_F(WebAppsTableTest, WebAppHasAllImages) { - WebDatabase db; - - ASSERT_EQ(sql::INIT_OK, db.Init(file_, std::string())); GURL url("http://google.com/"); // Initial value for unknown web app should be false. - EXPECT_FALSE(db.GetWebAppsTable()->GetWebAppHasAllImages(url)); + EXPECT_FALSE(table_->GetWebAppHasAllImages(url)); // Set the value and make sure it took. - EXPECT_TRUE(db.GetWebAppsTable()->SetWebAppHasAllImages(url, true)); - EXPECT_TRUE(db.GetWebAppsTable()->GetWebAppHasAllImages(url)); + EXPECT_TRUE(table_->SetWebAppHasAllImages(url, true)); + EXPECT_TRUE(table_->GetWebAppHasAllImages(url)); // Remove the app and make sure value reverts to default. - EXPECT_TRUE(db.GetWebAppsTable()->RemoveWebApp(url)); - EXPECT_FALSE(db.GetWebAppsTable()->GetWebAppHasAllImages(url)); + EXPECT_TRUE(table_->RemoveWebApp(url)); + EXPECT_FALSE(table_->GetWebAppHasAllImages(url)); } TEST_F(WebAppsTableTest, WebAppImages) { - WebDatabase db; - - ASSERT_EQ(sql::INIT_OK, db.Init(file_, std::string())); GURL url("http://google.com/"); // Web app should initially have no images. std::vector<SkBitmap> images; - ASSERT_TRUE(db.GetWebAppsTable()->GetWebAppImages(url, &images)); + ASSERT_TRUE(table_->GetWebAppImages(url, &images)); ASSERT_EQ(0U, images.size()); // Add an image. @@ -69,10 +70,10 @@ TEST_F(WebAppsTableTest, WebAppImages) { image.setConfig(SkBitmap::kARGB_8888_Config, 16, 16); image.allocPixels(); image.eraseColor(SK_ColorBLACK); - ASSERT_TRUE(db.GetWebAppsTable()->SetWebAppImage(url, image)); + ASSERT_TRUE(table_->SetWebAppImage(url, image)); // Make sure we get the image back. - ASSERT_TRUE(db.GetWebAppsTable()->GetWebAppImages(url, &images)); + ASSERT_TRUE(table_->GetWebAppImages(url, &images)); ASSERT_EQ(1U, images.size()); ASSERT_EQ(16, images[0].width()); ASSERT_EQ(16, images[0].height()); @@ -91,9 +92,9 @@ TEST_F(WebAppsTableTest, WebAppImages) { image.getAddr32(0, 1)[1] = test_pixel_2; image.getAddr32(0, 1)[2] = test_pixel_3; - ASSERT_TRUE(db.GetWebAppsTable()->SetWebAppImage(url, image)); + ASSERT_TRUE(table_->SetWebAppImage(url, image)); images.clear(); - ASSERT_TRUE(db.GetWebAppsTable()->GetWebAppImages(url, &images)); + ASSERT_TRUE(table_->GetWebAppImages(url, &images)); ASSERT_EQ(1U, images.size()); ASSERT_EQ(16, images[0].width()); ASSERT_EQ(16, images[0].height()); @@ -108,11 +109,11 @@ TEST_F(WebAppsTableTest, WebAppImages) { image.setConfig(SkBitmap::kARGB_8888_Config, 32, 32); image.allocPixels(); image.eraseColor(SK_ColorBLACK); - ASSERT_TRUE(db.GetWebAppsTable()->SetWebAppImage(url, image)); + ASSERT_TRUE(table_->SetWebAppImage(url, image)); // Make sure we get both images back. images.clear(); - ASSERT_TRUE(db.GetWebAppsTable()->GetWebAppImages(url, &images)); + ASSERT_TRUE(table_->GetWebAppImages(url, &images)); ASSERT_EQ(2U, images.size()); if (images[0].width() == 16) { ASSERT_EQ(16, images[0].width()); diff --git a/chrome/browser/webdata/web_data_service.cc b/chrome/browser/webdata/web_data_service.cc index ddc0004..50acf2e 100644 --- a/chrome/browser/webdata/web_data_service.cc +++ b/chrome/browser/webdata/web_data_service.cc @@ -312,20 +312,20 @@ void WebDataService::ShutdownSyncableServices() { WebDatabase::State WebDataService::AddKeywordImpl( const TemplateURLData& data, WebDatabase* db) { - db->GetKeywordTable()->AddKeyword(data); + KeywordTable::FromWebDatabase(db)->AddKeyword(data); return WebDatabase::COMMIT_NEEDED; } WebDatabase::State WebDataService::RemoveKeywordImpl( TemplateURLID id, WebDatabase* db) { DCHECK(id); - db->GetKeywordTable()->RemoveKeyword(id); + KeywordTable::FromWebDatabase(db)->RemoveKeyword(id); return WebDatabase::COMMIT_NEEDED; } WebDatabase::State WebDataService::UpdateKeywordImpl( const TemplateURLData& data, WebDatabase* db) { - if (!db->GetKeywordTable()->UpdateKeyword(data)) { + if (!KeywordTable::FromWebDatabase(db)->UpdateKeyword(data)) { NOTREACHED(); return WebDatabase::COMMIT_NOT_NEEDED; } @@ -334,18 +334,18 @@ WebDatabase::State WebDataService::UpdateKeywordImpl( scoped_ptr<WDTypedResult> WebDataService::GetKeywordsImpl(WebDatabase* db) { WDKeywordsResult result; - db->GetKeywordTable()->GetKeywords(&result.keywords); + KeywordTable::FromWebDatabase(db)->GetKeywords(&result.keywords); result.default_search_provider_id = - db->GetKeywordTable()->GetDefaultSearchProviderID(); + KeywordTable::FromWebDatabase(db)->GetDefaultSearchProviderID(); result.builtin_keyword_version = - db->GetKeywordTable()->GetBuiltinKeywordVersion(); + KeywordTable::FromWebDatabase(db)->GetBuiltinKeywordVersion(); return scoped_ptr<WDTypedResult>( new WDResult<WDKeywordsResult>(KEYWORDS_RESULT, result)); } WebDatabase::State WebDataService::SetDefaultSearchProviderImpl( TemplateURLID id, WebDatabase* db) { - if (!db->GetKeywordTable()->SetDefaultSearchProviderID(id)) { + if (!KeywordTable::FromWebDatabase(db)->SetDefaultSearchProviderID(id)) { NOTREACHED(); return WebDatabase::COMMIT_NOT_NEEDED; } @@ -354,7 +354,7 @@ WebDatabase::State WebDataService::SetDefaultSearchProviderImpl( WebDatabase::State WebDataService::SetBuiltinKeywordVersionImpl( int version, WebDatabase* db) { - if (!db->GetKeywordTable()->SetBuiltinKeywordVersion(version)) { + if (!KeywordTable::FromWebDatabase(db)->SetBuiltinKeywordVersion(version)) { NOTREACHED(); return WebDatabase::COMMIT_NOT_NEEDED; } @@ -369,28 +369,29 @@ WebDatabase::State WebDataService::SetBuiltinKeywordVersionImpl( WebDatabase::State WebDataService::SetWebAppImageImpl( const GURL& app_url, const SkBitmap& image, WebDatabase* db) { - db->GetWebAppsTable()->SetWebAppImage(app_url, image); + WebAppsTable::FromWebDatabase(db)->SetWebAppImage(app_url, image); return WebDatabase::COMMIT_NEEDED; } WebDatabase::State WebDataService::SetWebAppHasAllImagesImpl( const GURL& app_url, bool has_all_images, WebDatabase* db) { - db->GetWebAppsTable()-> - SetWebAppHasAllImages(app_url, has_all_images); + WebAppsTable::FromWebDatabase(db)->SetWebAppHasAllImages(app_url, + has_all_images); return WebDatabase::COMMIT_NEEDED; } WebDatabase::State WebDataService::RemoveWebAppImpl( const GURL& app_url, WebDatabase* db) { - db->GetWebAppsTable()->RemoveWebApp(app_url); + WebAppsTable::FromWebDatabase(db)->RemoveWebApp(app_url); return WebDatabase::COMMIT_NEEDED; } scoped_ptr<WDTypedResult> WebDataService::GetWebAppImagesImpl( const GURL& app_url, WebDatabase* db) { WDAppImagesResult result; - result.has_all_images = db->GetWebAppsTable()->GetWebAppHasAllImages(app_url); - db->GetWebAppsTable()->GetWebAppImages(app_url, &result.images); + result.has_all_images = + WebAppsTable::FromWebDatabase(db)->GetWebAppHasAllImages(app_url); + WebAppsTable::FromWebDatabase(db)->GetWebAppImages(app_url, &result.images); return scoped_ptr<WDTypedResult>( new WDResult<WDAppImagesResult>(WEB_APP_IMAGES, result)); } @@ -402,7 +403,7 @@ scoped_ptr<WDTypedResult> WebDataService::GetWebAppImagesImpl( //////////////////////////////////////////////////////////////////////////////// WebDatabase::State WebDataService::RemoveAllTokensImpl(WebDatabase* db) { - if (db->GetTokenServiceTable()->RemoveAllTokens()) { + if (TokenServiceTable::FromWebDatabase(db)->RemoveAllTokens()) { return WebDatabase::COMMIT_NEEDED; } return WebDatabase::COMMIT_NOT_NEEDED; @@ -410,7 +411,8 @@ WebDatabase::State WebDataService::RemoveAllTokensImpl(WebDatabase* db) { WebDatabase::State WebDataService::SetTokenForServiceImpl( const std::string& service, const std::string& token, WebDatabase* db) { - if (db->GetTokenServiceTable()->SetTokenForService(service, token)) { + if (TokenServiceTable::FromWebDatabase(db)->SetTokenForService(service, + token)) { return WebDatabase::COMMIT_NEEDED; } return WebDatabase::COMMIT_NOT_NEEDED; @@ -418,7 +420,7 @@ WebDatabase::State WebDataService::SetTokenForServiceImpl( scoped_ptr<WDTypedResult> WebDataService::GetAllTokensImpl(WebDatabase* db) { std::map<std::string, std::string> map; - db->GetTokenServiceTable()->GetAllTokens(&map); + TokenServiceTable::FromWebDatabase(db)->GetAllTokens(&map); return scoped_ptr<WDTypedResult>( new WDResult<std::map<std::string, std::string> >(TOKEN_RESULT, map)); } @@ -432,7 +434,8 @@ scoped_ptr<WDTypedResult> WebDataService::GetAllTokensImpl(WebDatabase* db) { WebDatabase::State WebDataService::AddFormElementsImpl( const std::vector<FormFieldData>& fields, WebDatabase* db) { AutofillChangeList changes; - if (!db->GetAutofillTable()->AddFormFieldValues(fields, &changes)) { + if (!AutofillTable::FromWebDatabase(db)->AddFormFieldValues( + fields, &changes)) { NOTREACHED(); return WebDatabase::COMMIT_NOT_NEEDED; } @@ -451,7 +454,7 @@ WebDatabase::State WebDataService::AddFormElementsImpl( scoped_ptr<WDTypedResult> WebDataService::GetFormValuesForElementNameImpl( const string16& name, const string16& prefix, int limit, WebDatabase* db) { std::vector<string16> values; - db->GetAutofillTable()->GetFormValuesForElementName( + AutofillTable::FromWebDatabase(db)->GetFormValuesForElementName( name, prefix, &values, limit); return scoped_ptr<WDTypedResult>( new WDResult<std::vector<string16> >(AUTOFILL_VALUE_RESULT, values)); @@ -462,7 +465,7 @@ WebDatabase::State WebDataService::RemoveFormElementsAddedBetweenImpl( WebDatabase* db) { AutofillChangeList changes; - if (db->GetAutofillTable()->RemoveFormElementsAddedBetween( + if (AutofillTable::FromWebDatabase(db)->RemoveFormElementsAddedBetween( delete_begin, delete_end, &changes)) { if (!changes.empty()) { // Post the notifications including the list of affected keys. @@ -482,7 +485,7 @@ WebDatabase::State WebDataService::RemoveExpiredFormElementsImpl( WebDatabase* db) { AutofillChangeList changes; - if (db->GetAutofillTable()->RemoveExpiredFormElements(&changes)) { + if (AutofillTable::FromWebDatabase(db)->RemoveExpiredFormElements(&changes)) { if (!changes.empty()) { // Post the notifications including the list of affected keys. // This is sent here so that work resulting from this notification @@ -500,7 +503,7 @@ WebDatabase::State WebDataService::RemoveExpiredFormElementsImpl( WebDatabase::State WebDataService::RemoveFormValueForElementNameImpl( const string16& name, const string16& value, WebDatabase* db) { - if (db->GetAutofillTable()->RemoveFormElement(name, value)) { + if (AutofillTable::FromWebDatabase(db)->RemoveFormElement(name, value)) { AutofillChangeList changes; changes.push_back(AutofillChange(AutofillChange::REMOVE, AutofillKey(name, value))); @@ -518,7 +521,7 @@ WebDatabase::State WebDataService::RemoveFormValueForElementNameImpl( WebDatabase::State WebDataService::AddAutofillProfileImpl( const AutofillProfile& profile, WebDatabase* db) { - if (!db->GetAutofillTable()->AddAutofillProfile(profile)) { + if (!AutofillTable::FromWebDatabase(db)->AddAutofillProfile(profile)) { NOTREACHED(); return WebDatabase::COMMIT_NOT_NEEDED; } @@ -540,13 +543,14 @@ WebDatabase::State WebDataService::UpdateAutofillProfileImpl( // valid to try to update a missing profile. We simply drop the write and // the caller will detect this on the next refresh. AutofillProfile* original_profile = NULL; - if (!db->GetAutofillTable()->GetAutofillProfile(profile.guid(), + if (!AutofillTable::FromWebDatabase(db)->GetAutofillProfile(profile.guid(), &original_profile)) { return WebDatabase::COMMIT_NOT_NEEDED; } scoped_ptr<AutofillProfile> scoped_profile(original_profile); - if (!db->GetAutofillTable()->UpdateAutofillProfileMulti(profile)) { + if (!AutofillTable::FromWebDatabase(db)->UpdateAutofillProfileMulti( + profile)) { NOTREACHED(); return WebDatabase::COMMIT_NEEDED; } @@ -565,13 +569,13 @@ WebDatabase::State WebDataService::UpdateAutofillProfileImpl( WebDatabase::State WebDataService::RemoveAutofillProfileImpl( const std::string& guid, WebDatabase* db) { AutofillProfile* profile = NULL; - if (!db->GetAutofillTable()->GetAutofillProfile(guid, &profile)) { + if (!AutofillTable::FromWebDatabase(db)->GetAutofillProfile(guid, &profile)) { NOTREACHED(); return WebDatabase::COMMIT_NOT_NEEDED; } scoped_ptr<AutofillProfile> scoped_profile(profile); - if (!db->GetAutofillTable()->RemoveAutofillProfile(guid)) { + if (!AutofillTable::FromWebDatabase(db)->RemoveAutofillProfile(guid)) { NOTREACHED(); return WebDatabase::COMMIT_NOT_NEEDED; } @@ -589,7 +593,7 @@ WebDatabase::State WebDataService::RemoveAutofillProfileImpl( scoped_ptr<WDTypedResult> WebDataService::GetAutofillProfilesImpl( WebDatabase* db) { std::vector<AutofillProfile*> profiles; - db->GetAutofillTable()->GetAutofillProfiles(&profiles); + AutofillTable::FromWebDatabase(db)->GetAutofillProfiles(&profiles); return scoped_ptr<WDTypedResult>( new WDDestroyableResult<std::vector<AutofillProfile*> >( AUTOFILL_PROFILES_RESULT, @@ -600,7 +604,7 @@ scoped_ptr<WDTypedResult> WebDataService::GetAutofillProfilesImpl( WebDatabase::State WebDataService::AddCreditCardImpl( const CreditCard& credit_card, WebDatabase* db) { - if (!db->GetAutofillTable()->AddCreditCard(credit_card)) { + if (!AutofillTable::FromWebDatabase(db)->AddCreditCard(credit_card)) { NOTREACHED(); return WebDatabase::COMMIT_NOT_NEEDED; } @@ -613,13 +617,13 @@ WebDatabase::State WebDataService::UpdateCreditCardImpl( // It is currently valid to try to update a missing profile. We simply drop // the write and the caller will detect this on the next refresh. CreditCard* original_credit_card = NULL; - if (!db->GetAutofillTable()->GetCreditCard(credit_card.guid(), + if (!AutofillTable::FromWebDatabase(db)->GetCreditCard(credit_card.guid(), &original_credit_card)) { return WebDatabase::COMMIT_NOT_NEEDED; } scoped_ptr<CreditCard> scoped_credit_card(original_credit_card); - if (!db->GetAutofillTable()->UpdateCreditCard(credit_card)) { + if (!AutofillTable::FromWebDatabase(db)->UpdateCreditCard(credit_card)) { NOTREACHED(); return WebDatabase::COMMIT_NOT_NEEDED; } @@ -628,7 +632,7 @@ WebDatabase::State WebDataService::UpdateCreditCardImpl( WebDatabase::State WebDataService::RemoveCreditCardImpl( const std::string& guid, WebDatabase* db) { - if (!db->GetAutofillTable()->RemoveCreditCard(guid)) { + if (!AutofillTable::FromWebDatabase(db)->RemoveCreditCard(guid)) { NOTREACHED(); return WebDatabase::COMMIT_NOT_NEEDED; } @@ -637,7 +641,7 @@ WebDatabase::State WebDataService::RemoveCreditCardImpl( scoped_ptr<WDTypedResult> WebDataService::GetCreditCardsImpl(WebDatabase* db) { std::vector<CreditCard*> credit_cards; - db->GetAutofillTable()->GetCreditCards(&credit_cards); + AutofillTable::FromWebDatabase(db)->GetCreditCards(&credit_cards); return scoped_ptr<WDTypedResult>( new WDDestroyableResult<std::vector<CreditCard*> >( AUTOFILL_CREDITCARDS_RESULT, @@ -652,7 +656,7 @@ WebDataService::RemoveAutofillProfilesAndCreditCardsModifiedBetweenImpl( WebDatabase* db) { std::vector<std::string> profile_guids; std::vector<std::string> credit_card_guids; - if (db->GetAutofillTable()-> + if (AutofillTable::FromWebDatabase(db)-> RemoveAutofillProfilesAndCreditCardsModifiedBetween( delete_begin, delete_end, diff --git a/chrome/browser/webdata/web_data_service_win.cc b/chrome/browser/webdata/web_data_service_win.cc index e0e1624..9d5eece 100644 --- a/chrome/browser/webdata/web_data_service_win.cc +++ b/chrome/browser/webdata/web_data_service_win.cc @@ -30,14 +30,14 @@ WebDataService::Handle WebDataService::GetIE7Login( WebDatabase::State WebDataService::AddIE7LoginImpl( const IE7PasswordInfo& info, WebDatabase* db) { - if (db->GetLoginsTable()->AddIE7Login(info)) + if (LoginsTable::FromWebDatabase(db)->AddIE7Login(info)) return WebDatabase::COMMIT_NEEDED; return WebDatabase::COMMIT_NOT_NEEDED; } WebDatabase::State WebDataService::RemoveIE7LoginImpl( const IE7PasswordInfo& info, WebDatabase* db) { - if (db->GetLoginsTable()->RemoveIE7Login(info)) + if (LoginsTable::FromWebDatabase(db)->RemoveIE7Login(info)) return WebDatabase::COMMIT_NEEDED; return WebDatabase::COMMIT_NOT_NEEDED; } @@ -45,7 +45,7 @@ WebDatabase::State WebDataService::RemoveIE7LoginImpl( scoped_ptr<WDTypedResult> WebDataService::GetIE7LoginImpl( const IE7PasswordInfo& info, WebDatabase* db) { IE7PasswordInfo result; - db->GetLoginsTable()->GetIE7Login(info, &result); + LoginsTable::FromWebDatabase(db)->GetIE7Login(info, &result); return scoped_ptr<WDTypedResult>( new WDResult<IE7PasswordInfo>(PASSWORD_IE7_RESULT, result)); } diff --git a/chrome/browser/webdata/web_database.cc b/chrome/browser/webdata/web_database.cc index 9cb7f23..71adced 100644 --- a/chrome/browser/webdata/web_database.cc +++ b/chrome/browser/webdata/web_database.cc @@ -6,12 +6,7 @@ #include <algorithm> -#include "chrome/browser/webdata/autofill_table.h" -#include "chrome/browser/webdata/keyword_table.h" -#include "chrome/browser/webdata/logins_table.h" -#include "chrome/browser/webdata/token_service_table.h" -#include "chrome/browser/webdata/web_apps_table.h" -#include "chrome/browser/webdata/web_intents_table.h" +#include "base/stl_util.h" #include "content/public/browser/notification_service.h" #include "sql/statement.h" #include "sql/transaction.h" @@ -51,34 +46,23 @@ sql::InitStatus FailedMigrationTo(int version_num) { WebDatabase::WebDatabase() {} -WebDatabase::~WebDatabase() {} - -void WebDatabase::BeginTransaction() { - db_.BeginTransaction(); -} - -void WebDatabase::CommitTransaction() { - db_.CommitTransaction(); +WebDatabase::~WebDatabase() { } -AutofillTable* WebDatabase::GetAutofillTable() { - return autofill_table_; +void WebDatabase::AddTable(WebDatabaseTable* table) { + tables_[table->GetTypeKey()] = table; } -KeywordTable* WebDatabase::GetKeywordTable() { - return keyword_table_; +WebDatabaseTable* WebDatabase::GetTable(WebDatabaseTable::TypeKey key) { + return tables_[key]; } -LoginsTable* WebDatabase::GetLoginsTable() { - return logins_table_; -} - -TokenServiceTable* WebDatabase::GetTokenServiceTable() { - return token_service_table_; +void WebDatabase::BeginTransaction() { + db_.BeginTransaction(); } -WebAppsTable* WebDatabase::GetWebAppsTable() { - return web_apps_table_; +void WebDatabase::CommitTransaction() { + db_.CommitTransaction(); } sql::Connection* WebDatabase::GetSQLConnection() { @@ -123,36 +107,11 @@ sql::InitStatus WebDatabase::Init(const base::FilePath& db_name, return sql::INIT_TOO_NEW; } - // TODO(joi): Table creation should move out of this class; switch - // to a two-phase init to accomplish this. - - // Create the tables. - autofill_table_ = new AutofillTable(&db_, &meta_table_); - tables_.push_back(autofill_table_); - - keyword_table_ = new KeywordTable(&db_, &meta_table_); - tables_.push_back(keyword_table_); - - // TODO(mdm): We only really need the LoginsTable on Windows for IE7 password - // access, but for now, we still create it on all platforms since it deletes - // the old logins table. We can remove this after a while, e.g. in M22 or so. - logins_table_ = new LoginsTable(&db_, &meta_table_); - tables_.push_back(logins_table_); - - token_service_table_ = new TokenServiceTable(&db_, &meta_table_); - tables_.push_back(token_service_table_); - - web_apps_table_ = new WebAppsTable(&db_, &meta_table_); - tables_.push_back(web_apps_table_); - - web_intents_table_ = new WebIntentsTable(&db_, &meta_table_); - tables_.push_back(web_intents_table_); - // Initialize the tables. - for (ScopedVector<WebDatabaseTable>::iterator it = tables_.begin(); + for (TableMap::iterator it = tables_.begin(); it != tables_.end(); ++it) { - if (!(*it)->Init()) { + if (!it->second->Init(&db_, &meta_table_)) { LOG(WARNING) << "Unable to initialize the web database."; return sql::INIT_FAILURE; } @@ -199,14 +158,14 @@ sql::InitStatus WebDatabase::MigrateOldVersionsAsNeeded( next_version <= kCurrentVersionNumber; ++next_version) { // Give each table a chance to migrate to this version. - for (ScopedVector<WebDatabaseTable>::iterator it = tables_.begin(); + for (TableMap::iterator it = tables_.begin(); it != tables_.end(); ++it) { // Any of the tables may set this to true, but by default it is false. bool update_compatible_version = false; - if (!(*it)->MigrateToVersion(next_version, - app_locale, - &update_compatible_version)) { + if (!it->second->MigrateToVersion(next_version, + app_locale, + &update_compatible_version)) { return FailedMigrationTo(next_version); } diff --git a/chrome/browser/webdata/web_database.h b/chrome/browser/webdata/web_database.h index 6a24985..e186f34 100644 --- a/chrome/browser/webdata/web_database.h +++ b/chrome/browser/webdata/web_database.h @@ -5,20 +5,14 @@ #ifndef CHROME_BROWSER_WEBDATA_WEB_DATABASE_H_ #define CHROME_BROWSER_WEBDATA_WEB_DATABASE_H_ +#include <map> + #include "base/memory/scoped_ptr.h" -#include "base/memory/scoped_vector.h" +#include "chrome/browser/webdata/web_database_table.h" #include "sql/connection.h" #include "sql/init_status.h" #include "sql/meta_table.h" -class AutofillTable; -class KeywordTable; -class LoginsTable; -class TokenServiceTable; -class WebAppsTable; -class WebDatabaseTable; -class WebIntentsTable; - namespace base { class FilePath; } @@ -40,10 +34,22 @@ class WebDatabase { WebDatabase(); virtual ~WebDatabase(); + // Adds a database table. Ownership remains with the caller, which + // must ensure that the lifetime of |table| exceeds this object's + // lifetime. Must only be called before Init. + void AddTable(WebDatabaseTable* table); + + // Retrieves a table based on its |key|. + WebDatabaseTable* GetTable(WebDatabaseTable::TypeKey key); + // Initialize the database given a name. The name defines where the SQLite // file is. If this returns an error code, no other method should be called. // Requires the |app_locale| to be passed as a parameter as the locale can // only safely be queried on the UI thread. + // + // Before calling this method, you must call AddTable for any + // WebDatabaseTable objects that are supposed to participate in + // managing the database. sql::InitStatus Init( const base::FilePath& db_name, const std::string& app_locale); @@ -51,12 +57,6 @@ class WebDatabase { void BeginTransaction(); void CommitTransaction(); - virtual AutofillTable* GetAutofillTable(); - virtual KeywordTable* GetKeywordTable(); - virtual LoginsTable* GetLoginsTable(); - virtual TokenServiceTable* GetTokenServiceTable(); - virtual WebAppsTable* GetWebAppsTable(); - // Exposed for testing only. sql::Connection* GetSQLConnection(); @@ -69,20 +69,10 @@ class WebDatabase { sql::Connection db_; sql::MetaTable meta_table_; - // TODO(joi): All of the typed pointers are going in a future - // change, as we remove knowledge of the specific types from this - // class. - AutofillTable* autofill_table_; - KeywordTable* keyword_table_; - LoginsTable* logins_table_; - TokenServiceTable* token_service_table_; - WebAppsTable* web_apps_table_; - // TODO(thakis): Add a migration to delete this table, then remove this. - WebIntentsTable* web_intents_table_; - - // Owns all the different database tables that have been added to - // this object. - ScopedVector<WebDatabaseTable> tables_; + // Map of all the different tables that have been added to this + // object. Non-owning. + typedef std::map<WebDatabaseTable::TypeKey, WebDatabaseTable*> TableMap; + TableMap tables_; scoped_ptr<content::NotificationService> notification_service_; diff --git a/chrome/browser/webdata/web_database_migration_unittest.cc b/chrome/browser/webdata/web_database_migration_unittest.cc index c36fd73..969f1ed 100644 --- a/chrome/browser/webdata/web_database_migration_unittest.cc +++ b/chrome/browser/webdata/web_database_migration_unittest.cc @@ -16,7 +16,11 @@ #include "base/values.h" #include "chrome/browser/webdata/autofill_change.h" #include "chrome/browser/webdata/autofill_entry.h" +#include "chrome/browser/webdata/autofill_table.h" #include "chrome/browser/webdata/keyword_table.h" +#include "chrome/browser/webdata/logins_table.h" +#include "chrome/browser/webdata/token_service_table.h" +#include "chrome/browser/webdata/web_apps_table.h" #include "chrome/browser/webdata/web_database.h" #include "chrome/browser/webdata/web_intents_table.h" #include "chrome/common/chrome_paths.h" @@ -164,6 +168,32 @@ class WebDatabaseMigrationTest : public testing::Test { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); } + // Load the database via the WebDatabase class and migrate the database to + // the current version. + void DoMigration() { + // TODO(joi): This whole unit test file needs to stay in //chrome + // for now, as it needs to know about all the different table + // types. Once all webdata datatypes have been componentized, this + // could move to components_unittests. + AutofillTable autofill_table; + KeywordTable keyword_table; + LoginsTable logins_table; + TokenServiceTable token_service_table; + WebAppsTable web_apps_table; + WebIntentsTable web_intents_table; + + WebDatabase db; + db.AddTable(&autofill_table); + db.AddTable(&keyword_table); + db.AddTable(&logins_table); + db.AddTable(&token_service_table); + db.AddTable(&web_apps_table); + db.AddTable(&web_intents_table); + + // This causes the migration to occur. + ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath(), std::string())); + } + protected: // Current tested version number. When adding a migration in // |WebDatabase::MigrateOldVersionsAsNeeded()| and changing the version number @@ -227,12 +257,7 @@ void WebDatabaseMigrationTest::LoadDatabase( // Tests that the all migrations from an empty database succeed. TEST_F(WebDatabaseMigrationTest, MigrateEmptyToCurrent) { - // Load the database via the WebDatabase class and migrate the database to - // the current version. - { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath(), std::string())); - } + DoMigration(); // Verify post-conditions. These are expectations for current version of the // database. @@ -279,12 +304,7 @@ TEST_F(WebDatabaseMigrationTest, MigrateVersion22ToCurrent) { connection.DoesColumnExist("credit_cards", "card_number_encrypted")); } - // Load the database via the WebDatabase class and migrate the database to - // the current version. - { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath(), std::string())); - } + DoMigration(); // Verify post-conditions. These are expectations for current version of the // database. @@ -327,12 +347,7 @@ TEST_F(WebDatabaseMigrationTest, MigrateVersion22CorruptedToCurrent) { ASSERT_TRUE(connection.DoesColumnExist("keywords", "id")); } - // Load the database via the WebDatabase class and migrate the database to - // the current version. - { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath(), std::string())); - } + DoMigration(); // Verify post-conditions. These are expectations for current version of the // database. @@ -367,12 +382,7 @@ TEST_F(WebDatabaseMigrationTest, MigrateVersion25ToCurrent) { ASSERT_TRUE(connection.Open(GetDatabasePath())); } - // Load the database via the WebDatabase class and migrate the database to - // the current version. - { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath(), std::string())); - } + DoMigration(); // Verify post-conditions. These are expectations for current version of the // database. @@ -430,12 +440,7 @@ TEST_F(WebDatabaseMigrationTest, MigrateVersion26ToCurrentStringLabels) { EXPECT_EQ(s3.ColumnType(0), sql::COLUMN_TYPE_TEXT); } - // Load the database via the WebDatabase class and migrate the database to - // the current version. - { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath(), std::string())); - } + DoMigration(); // Verify post-conditions. These are expectations for current version of the // database. @@ -500,12 +505,7 @@ TEST_F(WebDatabaseMigrationTest, MigrateVersion26ToCurrentStringIDs) { EXPECT_EQ(s3.ColumnType(0), sql::COLUMN_TYPE_TEXT); } - // Load the database via the WebDatabase class and migrate the database to - // the current version. - { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath(), std::string())); - } + DoMigration(); // Verify post-conditions. These are expectations for current version of the // database. @@ -549,12 +549,7 @@ TEST_F(WebDatabaseMigrationTest, MigrateVersion27ToCurrent) { ASSERT_FALSE(connection.DoesColumnExist("keywords", "instant_url")); } - // Load the database via the WebDatabase class and migrate the database to - // the current version. - { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath(), std::string())); - } + DoMigration(); // Verify post-conditions. These are expectations for current version of the // database. @@ -622,13 +617,8 @@ TEST_F(WebDatabaseMigrationTest, MigrateVersion29ToCurrent) { "date_modified")); } - // Load the database via the WebDatabase class and migrate the database to - // the current version. Time pre_creation_time = Time::Now(); - { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath(), std::string())); - } + DoMigration(); Time post_creation_time = Time::Now(); // Verify post-conditions. These are expectations for current version of the @@ -685,12 +675,7 @@ TEST_F(WebDatabaseMigrationTest, MigrateVersion30ToCurrent) { EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "guid")); } - // Load the database via the WebDatabase class and migrate the database to - // the current version. - { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath(), std::string())); - } + DoMigration(); // Verify post-conditions. These are expectations for current version of the // database. @@ -787,12 +772,7 @@ TEST_F(WebDatabaseMigrationTest, MigrateVersion31ToCurrent) { EXPECT_NE(profile.guid(), credit_card.guid()); } - // Load the database via the WebDatabase class and migrate the database to - // the current version. - { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath(), std::string())); - } + DoMigration(); // Verify post-conditions. These are expectations for current version of the // database. @@ -910,12 +890,7 @@ TEST_F(WebDatabaseMigrationTest, MigrateVersion32ToCurrent) { EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "label")); } - // Load the database via the WebDatabase class and migrate the database to - // the current version. - { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath(), std::string())); - } + DoMigration(); // Verify post-conditions. These are expectations for current version of the // database. @@ -1195,12 +1170,7 @@ TEST_F(WebDatabaseMigrationTest, MigrateVersion33ToCurrent) { EXPECT_EQ("United States", country); } - // Load the database via the WebDatabase class and migrate the database to - // the current version. - { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath(), std::string())); - } + DoMigration(); // Verify post-conditions. These are expectations for current version of the // database. @@ -1251,12 +1221,7 @@ TEST_F(WebDatabaseMigrationTest, MigrateVersion34ToCurrent) { ASSERT_FALSE(s.Step()); } - // Load the database via the WebDatabase class and migrate the database to - // the current version. - { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath(), std::string())); - } + DoMigration(); // Verify post-conditions. These are expectations for current version of the // database. @@ -1308,12 +1273,7 @@ TEST_F(WebDatabaseMigrationTest, MigrateVersion35ToCurrent) { EXPECT_EQ(6, i); } - // Load the database via the WebDatabase class and migrate the database to - // the current version. - { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath(), std::string())); - } + DoMigration(); // Verify post-conditions. These are expectations for current version of the // database. @@ -1392,12 +1352,7 @@ TEST_F(WebDatabaseMigrationTest, MigrateVersion37ToCurrent) { ASSERT_FALSE(connection.DoesColumnExist("keywords", "last_modified")); } - // Load the database via the WebDatabase class and migrate the database to - // the current version. - { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath(), std::string())); - } + DoMigration(); // Verify post-conditions. These are expectations for current version of the // database. @@ -1432,12 +1387,7 @@ TEST_F(WebDatabaseMigrationTest, MigrateVersion38ToCurrent) { ASSERT_FALSE(connection.DoesColumnExist("keywords", "sync_guid")); } - // Load the database via the WebDatabase class and migrate the database to - // the current version. - { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath(), std::string())); - } + DoMigration(); // Verify post-conditions. These are expectations for current version of the // database. @@ -1477,12 +1427,7 @@ TEST_F(WebDatabaseMigrationTest, MigrateVersion39ToCurrent) { EXPECT_NO_FATAL_FAILURE(CheckNoBackupData(connection, &meta_table)); } - // Load the database via the WebDatabase class and migrate the database to - // the current version. - { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath(), std::string())); - } + DoMigration(); // Verify post-conditions. These are expectations for current version of the // database. @@ -1528,12 +1473,7 @@ TEST_F(WebDatabaseMigrationTest, MigrateVersion40ToCurrent) { EXPECT_NO_FATAL_FAILURE(CheckHasBackupData(&meta_table)); } - // Load the database via the WebDatabase class and migrate the database to - // the current version. - { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath(), std::string())); - } + DoMigration(); // Verify post-conditions. These are expectations for current version of the // database. @@ -1579,12 +1519,7 @@ TEST_F(WebDatabaseMigrationTest, MigrateVersion41ToCurrent) { EXPECT_NO_FATAL_FAILURE(CheckHasBackupData(&meta_table)); } - // Load the database via the WebDatabase class and migrate the database to - // the current version. - { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath(), std::string())); - } + DoMigration(); // Verify post-conditions. These are expectations for current version of the // database. @@ -1632,12 +1567,7 @@ TEST_F(WebDatabaseMigrationTest, MigrateVersion42ToCurrent) { EXPECT_FALSE(connection.DoesTableExist("keywords_backup")); } - // Load the database via the WebDatabase class and migrate the database to - // the current version. - { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath(), std::string())); - } + DoMigration(); // Verify post-conditions. These are expectations for current version of the // database. @@ -1688,12 +1618,7 @@ TEST_F(WebDatabaseMigrationTest, MigrateVersion43ToCurrent) { EXPECT_TRUE(connection.DoesTableExist("keywords_backup")); } - // Load the database via the WebDatabase class and migrate the database to - // the current version. - { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath(), std::string())); - } + DoMigration(); // Verify post-conditions. These are expectations for current version of the // database. @@ -1740,12 +1665,7 @@ TEST_F(WebDatabaseMigrationTest, MigrateVersion44ToCurrent) { ASSERT_TRUE(connection.DoesColumnExist("keywords", "logo_id")); } - // Load the database via the WebDatabase class and migrate the database to - // the current version. - { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath(), std::string())); - } + DoMigration(); // Verify post-conditions. These are expectations for current version of the // database. @@ -1796,12 +1716,7 @@ TEST_F(WebDatabaseMigrationTest, MigrateVersion45ToCurrent) { "scheme", "web_intents_defaults")); } - // Load the database via the WebDatabase class and migrate the database to - // the current version. - { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath(), std::string())); - } + DoMigration(); // Verify post-conditions. These are expectations for current version of the // database. @@ -1877,12 +1792,7 @@ TEST_F(WebDatabaseMigrationTest, MigrateVersion45InvalidToCurrent) { "scheme", "web_intents_defaults")); } - // Load the database via the WebDatabase class and migrate the database to - // the current version. - { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath(), std::string())); - } + DoMigration(); // Verify post-conditions. These are expectations for current version of the // database. @@ -1942,12 +1852,7 @@ TEST_F(WebDatabaseMigrationTest, MigrateVersion45CompatibleToCurrent) { ASSERT_TRUE(meta_table.Init(&connection, 40, 45)); } - // Load the database via the WebDatabase class and migrate the database to - // the current version. - { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath(), std::string())); - } + DoMigration(); // Verify post-conditions. These are expectations for current version of the // database. @@ -1983,12 +1888,7 @@ TEST_F(WebDatabaseMigrationTest, MigrateVersion46ToCurrent) { "alternate_urls")); } - // Load the database via the WebDatabase class and migrate the database to - // the current version. - { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath(), std::string())); - } + DoMigration(); // Verify post-conditions. These are expectations for current version of the // database. @@ -2028,12 +1928,7 @@ TEST_F(WebDatabaseMigrationTest, MigrateVersion47ToCurrent) { EXPECT_TRUE(connection.DoesTableExist("keywords_backup")); } - // Load the database via the WebDatabase class and migrate the database to - // the current version. - { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath(), std::string())); - } + DoMigration(); // Verify post-conditions. These are expectations for current version of the // database. @@ -2078,12 +1973,7 @@ TEST_F(WebDatabaseMigrationTest, MigrateVersion48ToCurrent) { "search_terms_replacement_key")); } - // Load the database via the WebDatabase class and migrate the database to - // the current version. - { - WebDatabase db; - ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath(), std::string())); - } + DoMigration(); // Verify post-conditions. These are expectations for current version of the // database. diff --git a/chrome/browser/webdata/web_database_service.cc b/chrome/browser/webdata/web_database_service.cc index 543d9ba..f9ab568 100644 --- a/chrome/browser/webdata/web_database_service.cc +++ b/chrome/browser/webdata/web_database_service.cc @@ -8,8 +8,14 @@ #include "base/location.h" #include "chrome/browser/api/webdata/web_data_results.h" #include "chrome/browser/api/webdata/web_data_service_consumer.h" +#include "chrome/browser/webdata/autofill_table.h" +#include "chrome/browser/webdata/keyword_table.h" +#include "chrome/browser/webdata/logins_table.h" +#include "chrome/browser/webdata/token_service_table.h" +#include "chrome/browser/webdata/web_apps_table.h" #include "chrome/browser/webdata/web_data_request_manager.h" #include "chrome/browser/webdata/web_data_service.h" +#include "chrome/browser/webdata/web_intents_table.h" // TODO(caitkp): Remove this autofill dependency. #include "components/autofill/browser/autofill_country.h" @@ -72,6 +78,13 @@ class WebDataServiceBackend // Path to database file. FilePath db_path_; + scoped_ptr<AutofillTable> autofill_table_; + scoped_ptr<KeywordTable> keyword_table_; + scoped_ptr<LoginsTable> logins_table_; + scoped_ptr<TokenServiceTable> token_service_table_; + scoped_ptr<WebAppsTable> web_apps_table_; + scoped_ptr<WebIntentsTable> web_intents_table_; + scoped_ptr<WebDatabase> db_; // Keeps track of all pending requests made to the db. @@ -114,6 +127,32 @@ sql::InitStatus WebDataServiceBackend::LoadDatabaseIfNecessary() { } init_complete_ = true; db_.reset(new WebDatabase()); + + // All tables objects that participate in managing the database must + // be added here. + autofill_table_.reset(new AutofillTable()); + db_->AddTable(autofill_table_.get()); + + keyword_table_.reset(new KeywordTable()); + db_->AddTable(keyword_table_.get()); + + // TODO(mdm): We only really need the LoginsTable on Windows for IE7 password + // access, but for now, we still create it on all platforms since it deletes + // the old logins table. We can remove this after a while, e.g. in M22 or so. + logins_table_.reset(new LoginsTable()); + db_->AddTable(logins_table_.get()); + + token_service_table_.reset(new TokenServiceTable()); + db_->AddTable(token_service_table_.get()); + + web_apps_table_.reset(new WebAppsTable()); + db_->AddTable(web_apps_table_.get()); + + // TODO(thakis): Add a migration to delete the SQL table used by + // WebIntentsTable, then remove this. + web_intents_table_.reset(new WebIntentsTable()); + db_->AddTable(web_intents_table_.get()); + init_status_ = db_->Init(db_path_, app_locale_); if (init_status_ != sql::INIT_OK) { LOG(ERROR) << "Cannot initialize the web database: " << init_status_; diff --git a/chrome/browser/webdata/web_database_table.cc b/chrome/browser/webdata/web_database_table.cc index c95358d..3f6548f 100644 --- a/chrome/browser/webdata/web_database_table.cc +++ b/chrome/browser/webdata/web_database_table.cc @@ -4,12 +4,14 @@ #include "chrome/browser/webdata/web_database_table.h" -WebDatabaseTable::WebDatabaseTable( - sql::Connection* db, sql::MetaTable* meta_table) - : db_(db), meta_table_(meta_table) { +WebDatabaseTable::WebDatabaseTable() : db_(NULL), meta_table_(NULL) { } WebDatabaseTable::~WebDatabaseTable() { - db_ = NULL; - meta_table_ = NULL; +} + +bool WebDatabaseTable::Init(sql::Connection* db, sql::MetaTable* meta_table) { + db_ = db; + meta_table_ = meta_table; + return true; } diff --git a/chrome/browser/webdata/web_database_table.h b/chrome/browser/webdata/web_database_table.h index 7622695..881d5e7 100644 --- a/chrome/browser/webdata/web_database_table.h +++ b/chrome/browser/webdata/web_database_table.h @@ -16,11 +16,23 @@ class MetaTable; // Each table should subclass this, adding type-specific methods as needed. class WebDatabaseTable { public: - WebDatabaseTable(sql::Connection* db, sql::MetaTable* meta_table); + // To look up a WebDatabaseTable of a certain type from WebDatabase, + // we use a void* key, so that we can simply use the address of one + // of the type's statics. + typedef void* TypeKey; + + // The object is not ready for use until Init() has been called. + WebDatabaseTable(); virtual ~WebDatabaseTable(); + // Retrieves the TypeKey for the concrete subtype. + virtual TypeKey GetTypeKey() const = 0; + // Attempts to initialize the table and returns true if successful. - virtual bool Init() = 0; + // + // The base class stores the members passed and always return true; + // subclasses may perform other initialization as needed. + virtual bool Init(sql::Connection* db, sql::MetaTable* meta_table); // In order to encourage developers to think about sync when adding or // or altering new tables, this method must be implemented. Please get in @@ -43,6 +55,10 @@ class WebDatabaseTable { bool* update_compatible_version) = 0; protected: + // Non-owning. These are owned by WebDatabase, valid as long as that + // class exists. Since lifetime of WebDatabaseTable objects slightly + // exceeds that of WebDatabase, they should not be used in + // ~WebDatabaseTable. sql::Connection* db_; sql::MetaTable* meta_table_; diff --git a/chrome/browser/webdata/web_intents_table.cc b/chrome/browser/webdata/web_intents_table.cc index 3650c95..0214ac0 100644 --- a/chrome/browser/webdata/web_intents_table.cc +++ b/chrome/browser/webdata/web_intents_table.cc @@ -9,20 +9,39 @@ #include "base/logging.h" #include "base/string_util.h" #include "base/utf_string_conversions.h" +#include "chrome/browser/webdata/web_database.h" #include "googleurl/src/gurl.h" #include "net/base/mime_util.h" #include "sql/statement.h" #include "third_party/sqlite/sqlite3.h" -WebIntentsTable::WebIntentsTable(sql::Connection* db, - sql::MetaTable* meta_table) - : WebDatabaseTable(db, meta_table) { +namespace { + +int table_key = 0; + +WebDatabaseTable::TypeKey GetKey() { + return reinterpret_cast<void*>(&table_key); +} + +} // namespace + +WebIntentsTable::WebIntentsTable() { } WebIntentsTable::~WebIntentsTable() { } -bool WebIntentsTable::Init() { +WebIntentsTable* WebIntentsTable::FromWebDatabase(WebDatabase* db) { + return static_cast<WebIntentsTable*>(db->GetTable(GetKey())); +} + +WebDatabaseTable::TypeKey WebIntentsTable::GetTypeKey() const { + return GetKey(); +} + +bool WebIntentsTable::Init(sql::Connection* db, sql::MetaTable* meta_table) { + WebDatabaseTable::Init(db, meta_table); + if (!db_->DoesTableExist("web_intents")) { if (!db_->Execute("CREATE TABLE web_intents (" " service_url LONGVARCHAR," @@ -97,7 +116,7 @@ bool WebIntentsTable::MigrateToVersion46AddSchemeColumn() { return false; } - if (!Init()) return false; + if (!Init(db_, meta_table_)) return false; int error = db_->ExecuteAndReturnErrorCode( "INSERT INTO web_intents" diff --git a/chrome/browser/webdata/web_intents_table.h b/chrome/browser/webdata/web_intents_table.h index 0064341..8a8805d 100644 --- a/chrome/browser/webdata/web_intents_table.h +++ b/chrome/browser/webdata/web_intents_table.h @@ -18,6 +18,7 @@ class MetaTable; } struct DefaultWebIntentService; +class WebDatabase; // TODO(thakis): Delete this class once there's a migration that drops the // table backing it. @@ -50,11 +51,15 @@ struct DefaultWebIntentService; // class WebIntentsTable : public WebDatabaseTable { public: - WebIntentsTable(sql::Connection* db, sql::MetaTable* meta_table); + WebIntentsTable(); virtual ~WebIntentsTable(); + // Retrieves the WebIntentsTable* owned by |database|. + static WebIntentsTable* FromWebDatabase(WebDatabase* database); + // WebDatabaseTable implementation. - virtual bool Init() OVERRIDE; + virtual WebDatabaseTable::TypeKey GetTypeKey() const OVERRIDE; + virtual bool Init(sql::Connection* db, sql::MetaTable* meta_table) OVERRIDE; virtual bool IsSyncable() OVERRIDE; virtual bool MigrateToVersion(int version, const std::string& app_locale, diff --git a/chrome_frame/test/delete_chrome_history_test.cc b/chrome_frame/test/delete_chrome_history_test.cc index 0c44799..69e3a5a 100644 --- a/chrome_frame/test/delete_chrome_history_test.cc +++ b/chrome_frame/test/delete_chrome_history_test.cc @@ -102,13 +102,15 @@ ACTION_P2(ExpectFormValuesForElementNameMatch, element_name, matcher) { base::FilePath profile_path( root_path.Append(L"Default").Append(chrome::kWebDataFilename)); + AutofillTable autofill_table; WebDatabase web_database; + web_database.AddTable(&autofill_table); sql::InitStatus init_status = web_database.Init(profile_path, std::string()); EXPECT_EQ(sql::INIT_OK, init_status); if (init_status == sql::INIT_OK) { std::vector<string16> values; - web_database.GetAutofillTable()->GetFormValuesForElementName( + autofill_table.GetFormValuesForElementName( element_name, L"", &values, 9999); EXPECT_THAT(values, matcher); } |