diff options
author | isherman@chromium.org <isherman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-12-06 18:27:53 +0000 |
---|---|---|
committer | isherman@chromium.org <isherman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-12-06 18:27:53 +0000 |
commit | 3c2555466e60bc29cdc41cf6c4bba8386eac82e7 (patch) | |
tree | 3cd3809a75bc476b594547b24c477953988d5e52 /chrome/browser/webdata | |
parent | 55678895e730e635bbb6c99e7b0c9462fb660a9d (diff) | |
download | chromium_src-3c2555466e60bc29cdc41cf6c4bba8386eac82e7.zip chromium_src-3c2555466e60bc29cdc41cf6c4bba8386eac82e7.tar.gz chromium_src-3c2555466e60bc29cdc41cf6c4bba8386eac82e7.tar.bz2 |
Don't re-save autofill profiles to the web database if they haven't changed.
BUG=65129
TEST=unit_tests --gtest_filter=WebDatabaseTest.Update*
Review URL: http://codereview.chromium.org/5610002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@68358 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/webdata')
-rw-r--r-- | chrome/browser/webdata/web_database.cc | 16 | ||||
-rw-r--r-- | chrome/browser/webdata/web_database.h | 2 | ||||
-rw-r--r-- | chrome/browser/webdata/web_database_unittest.cc | 162 |
3 files changed, 180 insertions, 0 deletions
diff --git a/chrome/browser/webdata/web_database.cc b/chrome/browser/webdata/web_database.cc index 2b9c029..89325c2 100644 --- a/chrome/browser/webdata/web_database.cc +++ b/chrome/browser/webdata/web_database.cc @@ -1693,6 +1693,14 @@ bool WebDatabase::GetAutoFillProfiles( bool WebDatabase::UpdateAutoFillProfile(const AutoFillProfile& profile) { DCHECK(guid::IsValidGUID(profile.guid())); + + // Preserve appropriate modification dates by not updating unchanged profiles. + AutoFillProfile* tmp_profile = NULL; + DCHECK(GetAutoFillProfileForGUID(profile.guid(), &tmp_profile)); + scoped_ptr<AutoFillProfile> old_profile(tmp_profile); + if (*old_profile == profile) + return true; + sql::Statement s(db_.GetUniqueStatement( "UPDATE autofill_profiles " "SET guid=?, label=?, first_name=?, middle_name=?, last_name=?, " @@ -1813,6 +1821,14 @@ bool WebDatabase::GetCreditCards( bool WebDatabase::UpdateCreditCard(const CreditCard& credit_card) { DCHECK(guid::IsValidGUID(credit_card.guid())); + + // Preserve appropriate modification dates by not updating unchanged cards. + CreditCard* tmp_credit_card = NULL; + DCHECK(GetCreditCardForGUID(credit_card.guid(), &tmp_credit_card)); + scoped_ptr<CreditCard> old_credit_card(tmp_credit_card); + if (*old_credit_card == credit_card) + return true; + sql::Statement s(db_.GetUniqueStatement( "UPDATE credit_cards " "SET guid=?, label=?, name_on_card=?, expiration_month=?, " diff --git a/chrome/browser/webdata/web_database.h b/chrome/browser/webdata/web_database.h index 2d80437..17bc19c 100644 --- a/chrome/browser/webdata/web_database.h +++ b/chrome/browser/webdata/web_database.h @@ -322,6 +322,8 @@ class WebDatabase { FRIEND_TEST_ALL_PREFIXES(WebDatabaseTest, Autofill_AddFormFieldValues); FRIEND_TEST_ALL_PREFIXES(WebDatabaseTest, AutoFillProfile); FRIEND_TEST_ALL_PREFIXES(WebDatabaseTest, CreditCard); + FRIEND_TEST_ALL_PREFIXES(WebDatabaseTest, UpdateAutoFillProfile); + FRIEND_TEST_ALL_PREFIXES(WebDatabaseTest, UpdateCreditCard); FRIEND_TEST_ALL_PREFIXES(WebDatabaseTest, RemoveAutoFillProfilesAndCreditCardsModifiedBetween); diff --git a/chrome/browser/webdata/web_database_unittest.cc b/chrome/browser/webdata/web_database_unittest.cc index 3ac4d2b..dcae787 100644 --- a/chrome/browser/webdata/web_database_unittest.cc +++ b/chrome/browser/webdata/web_database_unittest.cc @@ -1635,6 +1635,168 @@ TEST_F(WebDatabaseTest, CreditCard) { &db_creditcard)); } +TEST_F(WebDatabaseTest, UpdateAutoFillProfile) { + WebDatabase db; + ASSERT_EQ(sql::INIT_OK, db.Init(file_)); + + // Add a profile to the db. + AutoFillProfile profile; + profile.set_label(ASCIIToUTF16("Test Profile")); + profile.SetInfo(AutoFillType(NAME_FIRST), ASCIIToUTF16("John")); + profile.SetInfo(AutoFillType(NAME_MIDDLE), ASCIIToUTF16("Q.")); + profile.SetInfo(AutoFillType(NAME_LAST), ASCIIToUTF16("Smith")); + profile.SetInfo(AutoFillType(EMAIL_ADDRESS), ASCIIToUTF16("js@example.com")); + profile.SetInfo(AutoFillType(COMPANY_NAME), ASCIIToUTF16("Google")); + profile.SetInfo(AutoFillType(ADDRESS_HOME_LINE1), + ASCIIToUTF16("1234 Apple Way")); + profile.SetInfo(AutoFillType(ADDRESS_HOME_LINE2), ASCIIToUTF16("unit 5")); + profile.SetInfo(AutoFillType(ADDRESS_HOME_CITY), ASCIIToUTF16("Los Angeles")); + profile.SetInfo(AutoFillType(ADDRESS_HOME_STATE), ASCIIToUTF16("CA")); + profile.SetInfo(AutoFillType(ADDRESS_HOME_ZIP), ASCIIToUTF16("90025")); + profile.SetInfo(AutoFillType(ADDRESS_HOME_COUNTRY), ASCIIToUTF16("US")); + profile.SetInfo(AutoFillType(PHONE_HOME_WHOLE_NUMBER), + ASCIIToUTF16("18181234567")); + profile.SetInfo(AutoFillType(PHONE_FAX_WHOLE_NUMBER), + ASCIIToUTF16("1915243678")); + db.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.db_.GetUniqueStatement( + "UPDATE autofill_profiles SET date_modified = ?")); + ASSERT_TRUE(s_mock_creation_date); + 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.GetAutoFillProfileForGUID(profile.guid(), &tmp_profile)); + scoped_ptr<AutoFillProfile> db_profile(tmp_profile); + EXPECT_EQ(profile, *db_profile); + sql::Statement s_original(db.db_.GetUniqueStatement( + "SELECT date_modified FROM autofill_profiles")); + ASSERT_TRUE(s_original); + ASSERT_TRUE(s_original.Step()); + EXPECT_EQ(mock_creation_date, s_original.ColumnInt64(0)); + EXPECT_FALSE(s_original.Step()); + + // Now, update the profile and save the update to the database. + // The modification date should change to reflect the update. + profile.SetInfo(AutoFillType(EMAIL_ADDRESS), ASCIIToUTF16("js@smith.xyz")); + db.UpdateAutoFillProfile(profile); + + // Get the profile. + ASSERT_TRUE(db.GetAutoFillProfileForGUID(profile.guid(), &tmp_profile)); + db_profile.reset(tmp_profile); + EXPECT_EQ(profile, *db_profile); + sql::Statement s_updated(db.db_.GetUniqueStatement( + "SELECT date_modified FROM autofill_profiles")); + ASSERT_TRUE(s_updated); + ASSERT_TRUE(s_updated.Step()); + EXPECT_LT(mock_creation_date, s_updated.ColumnInt64(0)); + EXPECT_FALSE(s_updated.Step()); + + // 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.db_.GetUniqueStatement( + "UPDATE autofill_profiles SET date_modified = ?")); + ASSERT_TRUE(s_mock_modification_date); + s_mock_modification_date.BindInt64(0, mock_modification_date); + ASSERT_TRUE(s_mock_modification_date.Run()); + + // Finally, call into |UpdateAutoFillProfile()| without changing the profile. + // The modification date should not change. + db.UpdateAutoFillProfile(profile); + + // Get the profile. + ASSERT_TRUE(db.GetAutoFillProfileForGUID(profile.guid(), &tmp_profile)); + db_profile.reset(tmp_profile); + EXPECT_EQ(profile, *db_profile); + sql::Statement s_unchanged(db.db_.GetUniqueStatement( + "SELECT date_modified FROM autofill_profiles")); + ASSERT_TRUE(s_unchanged); + ASSERT_TRUE(s_unchanged.Step()); + EXPECT_EQ(mock_modification_date, s_unchanged.ColumnInt64(0)); + EXPECT_FALSE(s_unchanged.Step()); +} + +TEST_F(WebDatabaseTest, UpdateCreditCard) { + WebDatabase db; + ASSERT_EQ(sql::INIT_OK, db.Init(file_)); + + // Add a credit card to the db. + CreditCard credit_card; + credit_card.set_label(ASCIIToUTF16("Test Credit Card")); + credit_card.SetInfo(AutoFillType(CREDIT_CARD_NAME), + ASCIIToUTF16("Jack Torrance")); + credit_card.SetInfo(AutoFillType(CREDIT_CARD_NUMBER), + ASCIIToUTF16("1234567890123456")); + credit_card.SetInfo(AutoFillType(CREDIT_CARD_EXP_MONTH), + ASCIIToUTF16("04")); + credit_card.SetInfo(AutoFillType(CREDIT_CARD_EXP_4_DIGIT_YEAR), + ASCIIToUTF16("2013")); + db.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.db_.GetUniqueStatement( + "UPDATE credit_cards SET date_modified = ?")); + ASSERT_TRUE(s_mock_creation_date); + 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.GetCreditCardForGUID(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.db_.GetUniqueStatement( + "SELECT date_modified FROM credit_cards")); + ASSERT_TRUE(s_original); + ASSERT_TRUE(s_original.Step()); + EXPECT_EQ(mock_creation_date, s_original.ColumnInt64(0)); + EXPECT_FALSE(s_original.Step()); + + // Now, update the credit card and save the update to the database. + // The modification date should change to reflect the update. + credit_card.SetInfo(AutoFillType(CREDIT_CARD_EXP_MONTH), ASCIIToUTF16("01")); + db.UpdateCreditCard(credit_card); + + // Get the credit card. + ASSERT_TRUE(db.GetCreditCardForGUID(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.db_.GetUniqueStatement( + "SELECT date_modified FROM credit_cards")); + ASSERT_TRUE(s_updated); + ASSERT_TRUE(s_updated.Step()); + EXPECT_LT(mock_creation_date, s_updated.ColumnInt64(0)); + EXPECT_FALSE(s_updated.Step()); + + // 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.db_.GetUniqueStatement( + "UPDATE credit_cards SET date_modified = ?")); + ASSERT_TRUE(s_mock_modification_date); + s_mock_modification_date.BindInt64(0, mock_modification_date); + ASSERT_TRUE(s_mock_modification_date.Run()); + + // Finally, call into |UpdateCreditCard()| without changing the credit card. + // The modification date should not change. + db.UpdateCreditCard(credit_card); + + // Get the profile. + ASSERT_TRUE(db.GetCreditCardForGUID(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.db_.GetUniqueStatement( + "SELECT date_modified FROM credit_cards")); + ASSERT_TRUE(s_unchanged); + ASSERT_TRUE(s_unchanged.Step()); + EXPECT_EQ(mock_modification_date, s_unchanged.ColumnInt64(0)); + EXPECT_FALSE(s_unchanged.Step()); +} + TEST_F(WebDatabaseTest, RemoveAutoFillProfilesAndCreditCardsModifiedBetween) { WebDatabase db; ASSERT_EQ(sql::INIT_OK, db.Init(file_)); |