diff options
Diffstat (limited to 'chrome/browser/webdata')
| -rw-r--r-- | chrome/browser/webdata/autofill_change.cc | 90 | ||||
| -rw-r--r-- | chrome/browser/webdata/autofill_change.h | 107 | ||||
| -rw-r--r-- | chrome/browser/webdata/autofill_entry.cc | 28 | ||||
| -rw-r--r-- | chrome/browser/webdata/autofill_entry.h | 23 | ||||
| -rw-r--r-- | chrome/browser/webdata/web_data_service.cc | 315 | ||||
| -rw-r--r-- | chrome/browser/webdata/web_data_service.h | 47 | ||||
| -rw-r--r-- | chrome/browser/webdata/web_data_service_unittest.cc | 253 | ||||
| -rw-r--r-- | chrome/browser/webdata/web_database.cc | 643 | ||||
| -rw-r--r-- | chrome/browser/webdata/web_database.h | 22 | ||||
| -rw-r--r-- | chrome/browser/webdata/web_database_unittest.cc | 471 |
10 files changed, 1202 insertions, 797 deletions
diff --git a/chrome/browser/webdata/autofill_change.cc b/chrome/browser/webdata/autofill_change.cc new file mode 100644 index 0000000..fd0cfbf --- /dev/null +++ b/chrome/browser/webdata/autofill_change.cc @@ -0,0 +1,90 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/webdata/autofill_change.h" + +#include "chrome/browser/autofill/autofill_profile.h" +#include "chrome/browser/autofill/credit_card.h" + +AutofillChange::AutofillChange(Type type, const AutofillKey& key) + : GenericAutofillChange<AutofillKey>(type, key) { +} + +AutofillChange::~AutofillChange() { +} + +AutofillProfileChange::AutofillProfileChange(Type type, + string16 key, + const AutoFillProfile* profile, + const string16& pre_update_label) + : GenericAutofillChange<string16>(type, key), + profile_(profile), + pre_update_label_(pre_update_label) { +} + +AutofillProfileChange::~AutofillProfileChange() { +} + +bool AutofillProfileChange::operator==( + const AutofillProfileChange& change) const { + if (type() != change.type() || key() != change.key()) + return false; + if (type() == REMOVE) + return true; + if (*profile() != *change.profile()) + return false; + return type() == ADD || pre_update_label_ == change.pre_update_label(); +} + +AutofillCreditCardChange::AutofillCreditCardChange( + Type type, string16 key, const CreditCard* credit_card) + : GenericAutofillChange<string16>(type, key), credit_card_(credit_card) { +} + +AutofillCreditCardChange::~AutofillCreditCardChange() { +} + +bool AutofillCreditCardChange::operator==( + const AutofillCreditCardChange& change) const { + return type() == change.type() && + key() == change.key() && + (type() != REMOVE) ? *credit_card() == *change.credit_card() : true; +} + +AutofillProfileChangeGUID::AutofillProfileChangeGUID( + Type type, std::string key, const AutoFillProfile* profile) + : GenericAutofillChange<std::string>(type, key), + profile_(profile) { + DCHECK(type == ADD ? (profile && profile->guid() == key) : true); + DCHECK(type == UPDATE ? (profile && profile->guid() == key) : true); + DCHECK(type == REMOVE ? !profile : true); +} + +AutofillProfileChangeGUID::~AutofillProfileChangeGUID() { +} + +bool AutofillProfileChangeGUID::operator==( + const AutofillProfileChangeGUID& change) const { + return type() == change.type() && + key() == change.key() && + (type() != REMOVE) ? *profile() == *change.profile() : true; +} + +AutofillCreditCardChangeGUID::AutofillCreditCardChangeGUID( + Type type, std::string key, const CreditCard* credit_card) + : GenericAutofillChange<std::string>(type, key), credit_card_(credit_card) { + DCHECK(type == ADD ? (credit_card && credit_card->guid() == key) : true); + DCHECK(type == UPDATE ? (credit_card && credit_card->guid() == key) : true); + DCHECK(type == REMOVE ? !credit_card : true); +} + +AutofillCreditCardChangeGUID::~AutofillCreditCardChangeGUID() { +} + +bool AutofillCreditCardChangeGUID::operator==( + const AutofillCreditCardChangeGUID& change) const { + return type() == change.type() && + key() == change.key() && + (type() != REMOVE) ? *credit_card() == *change.credit_card() : true; +} diff --git a/chrome/browser/webdata/autofill_change.h b/chrome/browser/webdata/autofill_change.h index 11723d3..2a46399 100644 --- a/chrome/browser/webdata/autofill_change.h +++ b/chrome/browser/webdata/autofill_change.h @@ -6,8 +6,6 @@ #define CHROME_BROWSER_WEBDATA_AUTOFILL_CHANGE_H__ #pragma once -#include "chrome/browser/autofill/autofill_profile.h" -#include "chrome/browser/autofill/credit_card.h" #include "chrome/browser/webdata/autofill_entry.h" class AutoFillProfile; @@ -40,53 +38,102 @@ class GenericAutofillChange { class AutofillChange : public GenericAutofillChange<AutofillKey> { public: - AutofillChange(Type t, const AutofillKey& k) - : GenericAutofillChange<AutofillKey>(t, k) {} + AutofillChange(Type type, const AutofillKey& key); + virtual ~AutofillChange(); bool operator==(const AutofillChange& change) const { return type() == change.type() && key() == change.key(); } }; +// DEPRECATED +// TODO(dhollowa): Remove use of labels for sync. http://crbug.com/58813 class AutofillProfileChange : public GenericAutofillChange<string16> { public: - // If t == REMOVE, |p| should be NULL. |pre_update_label| only applies to - // UPDATE changes. - AutofillProfileChange(Type t, string16 k, const AutoFillProfile* p, - const string16& pre_update_label) - : GenericAutofillChange<string16>(t, k), profile_(p), - pre_update_label_(pre_update_label) {} + // The |type| input specifies the change type. The |key| input is the key, + // which is expected to be the label identifying the |profile|. + // When |type| == ADD, |profile| should be non-NULL. + // When |type| == UPDATE, |profile| should be non-NULL. + // When |type| == REMOVE, |profile| should be NULL. + // The |pre_update_label| input specifies the label as it was prior to the + // change (applicable only for UPDATE). + AutofillProfileChange(Type type, + string16 key, + const AutoFillProfile* profile, + const string16& pre_update_label); + virtual ~AutofillProfileChange(); const AutoFillProfile* profile() const { return profile_; } const string16& pre_update_label() const { return pre_update_label_; } - bool operator==(const AutofillProfileChange& change) const { - if (type() != change.type() || key() != change.key()) - return false; - if (type() == REMOVE) - return true; - // TODO(dhollowa): Replace with |AutoFillProfile::Compare|. - // http://crbug.com/58813 - if (*profile() != *change.profile()) - return false; - return type() == ADD || pre_update_label_ == change.pre_update_label(); - } + bool operator==(const AutofillProfileChange& change) const; + private: - const AutoFillProfile* profile_; // Unowned pointer, can be NULL. + // Weak reference, can be NULL. + const AutoFillProfile* profile_; const string16 pre_update_label_; }; +// DEPRECATED +// TODO(dhollowa): Remove use of labels for sync. http://crbug.com/58813 class AutofillCreditCardChange : public GenericAutofillChange<string16> { public: - // If t == REMOVE, |card| should be NULL. - AutofillCreditCardChange(Type t, string16 k, const CreditCard* card) - : GenericAutofillChange<string16>(t, k), credit_card_(card) {} + // The |type| input specifies the change type. The |key| input is the key, + // which is expected to be the label identifying the |credit_card|. + // When |type| == ADD, |credit_card| should be non-NULL. + // When |type| == UPDATE, |credit_card| should be non-NULL. + // When |type| == REMOVE, |credit_card| should be NULL. + AutofillCreditCardChange(Type type, + string16 key, + const CreditCard* credit_card); + virtual ~AutofillCreditCardChange(); const CreditCard* credit_card() const { return credit_card_; } - bool operator==(const AutofillCreditCardChange& change) const { - return type() == change.type() && key() == change.key() && - (type() != REMOVE) ? *credit_card() == *change.credit_card() : true; - } + bool operator==(const AutofillCreditCardChange& change) const; + + private: + // Weak reference, can be NULL. + const CreditCard* credit_card_; +}; + +// Change notification details for AutoFill profile changes. +class AutofillProfileChangeGUID : public GenericAutofillChange<std::string> { + public: + // The |type| input specifies the change type. The |key| input is the key, + // which is expected to be the GUID identifying the |profile|. + // When |type| == ADD, |profile| should be non-NULL. + // When |type| == UPDATE, |profile| should be non-NULL. + // When |type| == REMOVE, |profile| should be NULL. + AutofillProfileChangeGUID(Type type, + std::string key, + const AutoFillProfile* profile); + virtual ~AutofillProfileChangeGUID(); + + const AutoFillProfile* profile() const { return profile_; } + bool operator==(const AutofillProfileChangeGUID& change) const; + + private: + // Weak reference, can be NULL. + const AutoFillProfile* profile_; +}; + +// Change notification details for AutoFill credit card changes. +class AutofillCreditCardChangeGUID : public GenericAutofillChange<std::string> { + public: + // The |type| input specifies the change type. The |key| input is the key, + // which is expected to be the GUID identifying the |credit_card|. + // When |type| == ADD, |credit_card| should be non-NULL. + // When |type| == UPDATE, |credit_card| should be non-NULL. + // When |type| == REMOVE, |credit_card| should be NULL. + AutofillCreditCardChangeGUID(Type type, + std::string key, + const CreditCard* credit_card); + virtual ~AutofillCreditCardChangeGUID(); + + const CreditCard* credit_card() const { return credit_card_; } + bool operator==(const AutofillCreditCardChangeGUID& change) const; + private: - const CreditCard* credit_card_; // Unowned pointer, can be NULL. + // Weak reference, can be NULL. + const CreditCard* credit_card_; }; #endif // CHROME_BROWSER_WEBDATA_AUTOFILL_CHANGE_H__ diff --git a/chrome/browser/webdata/autofill_entry.cc b/chrome/browser/webdata/autofill_entry.cc index d0f8913..31bc406 100644 --- a/chrome/browser/webdata/autofill_entry.cc +++ b/chrome/browser/webdata/autofill_entry.cc @@ -4,6 +4,26 @@ #include <set> #include "chrome/browser/webdata/autofill_entry.h" +#include "base/utf_string_conversions.h" + +AutofillKey::AutofillKey() {} + +AutofillKey::AutofillKey(const string16& name, const string16& value) + : name_(name), + value_(value) { +} + +AutofillKey::AutofillKey(const char* name, const char* value) + : name_(UTF8ToUTF16(name)), + value_(UTF8ToUTF16(value)) { +} + +AutofillKey::AutofillKey(const AutofillKey& key) + : name_(key.name()), + value_(key.value()) { +} + +AutofillKey::~AutofillKey() {} bool AutofillKey::operator==(const AutofillKey& key) const { return name_ == key.name() && value_ == key.value(); @@ -20,6 +40,14 @@ bool AutofillKey::operator<(const AutofillKey& key) const { } } +AutofillEntry::AutofillEntry(const AutofillKey& key, + const std::vector<base::Time>& timestamps) + : key_(key), + timestamps_(timestamps) { +} + +AutofillEntry::~AutofillEntry() {} + bool AutofillEntry::operator==(const AutofillEntry& entry) const { if (!(key_ == entry.key())) return false; diff --git a/chrome/browser/webdata/autofill_entry.h b/chrome/browser/webdata/autofill_entry.h index 4917b29..7aa9139 100644 --- a/chrome/browser/webdata/autofill_entry.h +++ b/chrome/browser/webdata/autofill_entry.h @@ -9,21 +9,15 @@ #include <vector> #include "base/string16.h" #include "base/time.h" -#include "base/utf_string_conversions.h" class AutofillKey { public: - AutofillKey() {} - AutofillKey(const string16& name, const string16& value) - : name_(name), - value_(value) {} - AutofillKey(const char* name, const char* value) - : name_(UTF8ToUTF16(name)), - value_(UTF8ToUTF16(value)) {} - AutofillKey(const AutofillKey& key) - : name_(key.name()), - value_(key.value()) {} - virtual ~AutofillKey() {} + AutofillKey(); + AutofillKey(const string16& name, const string16& value); + AutofillKey(const char* name, const char* value); + AutofillKey(const AutofillKey& key); + virtual ~AutofillKey(); + const string16& name() const { return name_; } const string16& value() const { return value_; } @@ -38,9 +32,8 @@ class AutofillKey { class AutofillEntry { public: AutofillEntry(const AutofillKey& key, - const std::vector<base::Time>& timestamps) - : key_(key), - timestamps_(timestamps) {} + const std::vector<base::Time>& timestamps); + ~AutofillEntry(); const AutofillKey& key() const { return key_; } const std::vector<base::Time>& timestamps() const { return timestamps_; } diff --git a/chrome/browser/webdata/web_data_service.cc b/chrome/browser/webdata/web_data_service.cc index 0664bbf..d5ecd0f 100644 --- a/chrome/browser/webdata/web_data_service.cc +++ b/chrome/browser/webdata/web_data_service.cc @@ -391,37 +391,27 @@ void WebDataService::RemoveFormValueForElementName( request)); } -void WebDataService::AddAutoFillProfile(const AutoFillProfile& profile) { +void WebDataService::AddAutoFillProfileGUID(const AutoFillProfile& profile) { GenericRequest<AutoFillProfile>* request = new GenericRequest<AutoFillProfile>( this, GetNextRequestHandle(), NULL, profile); RegisterRequest(request); ScheduleTask(NewRunnableMethod(this, - &WebDataService::AddAutoFillProfileImpl, + &WebDataService::AddAutoFillProfileGUIDImpl, request)); } -void WebDataService::UpdateAutoFillProfile(const AutoFillProfile& profile) { +void WebDataService::UpdateAutoFillProfileGUID(const AutoFillProfile& profile) { GenericRequest<AutoFillProfile>* request = new GenericRequest<AutoFillProfile>( this, GetNextRequestHandle(), NULL, profile); RegisterRequest(request); ScheduleTask(NewRunnableMethod(this, - &WebDataService::UpdateAutoFillProfileImpl, + &WebDataService::UpdateAutoFillProfileGUIDImpl, request)); } -void WebDataService::RemoveAutoFillProfile(int profile_id) { - GenericRequest<int>* request = - new GenericRequest<int>( - this, GetNextRequestHandle(), NULL, profile_id); - RegisterRequest(request); - ScheduleTask(NewRunnableMethod(this, - &WebDataService::RemoveAutoFillProfileImpl, - request)); -} - -void WebDataService::RemoveAutoFillProfile(const std::string& guid) { +void WebDataService::RemoveAutoFillProfileGUID(const std::string& guid) { GenericRequest<std::string>* request = new GenericRequest<std::string>( this, GetNextRequestHandle(), NULL, guid); @@ -443,37 +433,27 @@ WebDataService::Handle WebDataService::GetAutoFillProfiles( return request->GetHandle(); } -void WebDataService::AddCreditCard(const CreditCard& creditcard) { +void WebDataService::AddCreditCardGUID(const CreditCard& credit_card) { GenericRequest<CreditCard>* request = new GenericRequest<CreditCard>( - this, GetNextRequestHandle(), NULL, creditcard); + this, GetNextRequestHandle(), NULL, credit_card); RegisterRequest(request); ScheduleTask(NewRunnableMethod(this, - &WebDataService::AddCreditCardImpl, + &WebDataService::AddCreditCardGUIDImpl, request)); } -void WebDataService::UpdateCreditCard(const CreditCard& creditcard) { +void WebDataService::UpdateCreditCardGUID(const CreditCard& credit_card) { GenericRequest<CreditCard>* request = new GenericRequest<CreditCard>( - this, GetNextRequestHandle(), NULL, creditcard); + this, GetNextRequestHandle(), NULL, credit_card); RegisterRequest(request); ScheduleTask(NewRunnableMethod(this, - &WebDataService::UpdateCreditCardImpl, + &WebDataService::UpdateCreditCardGUIDImpl, request)); } -void WebDataService::RemoveCreditCard(int creditcard_id) { - GenericRequest<int>* request = - new GenericRequest<int>( - this, GetNextRequestHandle(), NULL, creditcard_id); - RegisterRequest(request); - ScheduleTask(NewRunnableMethod(this, - &WebDataService::RemoveCreditCardImpl, - request)); -} - -void WebDataService::RemoveCreditCard(const std::string& guid) { +void WebDataService::RemoveCreditCardGUID(const std::string& guid) { GenericRequest<std::string>* request = new GenericRequest<std::string>( this, GetNextRequestHandle(), NULL, guid); @@ -499,11 +479,11 @@ void WebDataService::RemoveAutoFillProfilesAndCreditCardsModifiedBetween( const Time& delete_begin, const Time& delete_end) { GenericRequest2<Time, Time>* request = - new GenericRequest2<Time, Time>(this, - GetNextRequestHandle(), - NULL, - delete_begin, - delete_end); + new GenericRequest2<Time, Time>(this, + GetNextRequestHandle(), + NULL, + delete_begin, + delete_end); RegisterRequest(request); ScheduleTask(NewRunnableMethod( this, @@ -690,8 +670,10 @@ void WebDataService::RemoveKeywordImpl( void WebDataService::UpdateKeywordImpl(GenericRequest<TemplateURL>* request) { InitializeDatabaseIfNecessary(); if (db_ && !request->IsCancelled()) { - if (!db_->UpdateKeyword(request->GetArgument())) + if (!db_->UpdateKeyword(request->GetArgument())) { NOTREACHED(); + return; + } ScheduleCommit(); } request->RequestComplete(); @@ -714,8 +696,10 @@ void WebDataService::SetDefaultSearchProviderImpl( GenericRequest<TemplateURLID>* request) { InitializeDatabaseIfNecessary(); if (db_ && !request->IsCancelled()) { - if (!db_->SetDefaultSearchProviderID(request->GetArgument())) + if (!db_->SetDefaultSearchProviderID(request->GetArgument())) { NOTREACHED(); + return; + } ScheduleCommit(); } request->RequestComplete(); @@ -725,8 +709,10 @@ void WebDataService::SetBuiltinKeywordVersionImpl( GenericRequest<int>* request) { InitializeDatabaseIfNecessary(); if (db_ && !request->IsCancelled()) { - if (!db_->SetBuitinKeywordVersion(request->GetArgument())) + if (!db_->SetBuitinKeywordVersion(request->GetArgument())) { NOTREACHED(); + return; + } ScheduleCommit(); } request->RequestComplete(); @@ -922,8 +908,10 @@ void WebDataService::AddFormElementsImpl( const std::vector<FormField>& form_fields = request->GetArgument(); if (db_ && !request->IsCancelled()) { AutofillChangeList changes; - if (!db_->AddFormFieldValues(form_fields, &changes)) + if (!db_->AddFormFieldValues(form_fields, &changes)) { NOTREACHED(); + return; + } request->SetResult( new WDResult<AutofillChangeList>(AUTOFILL_CHANGES, changes)); ScheduleCommit(); @@ -1003,7 +991,7 @@ void WebDataService::RemoveFormValueForElementNameImpl( request->RequestComplete(); } -void WebDataService::AddAutoFillProfileImpl( +void WebDataService::AddAutoFillProfileGUIDImpl( GenericRequest<AutoFillProfile>* request) { InitializeDatabaseIfNecessary(); if (db_ && !request->IsCancelled()) { @@ -1014,68 +1002,71 @@ void WebDataService::AddAutoFillProfileImpl( } ScheduleCommit(); - AutofillProfileChange change(AutofillProfileChange::ADD, - profile.Label(), &profile, string16()); + // Send GUID-based notification. + AutofillProfileChangeGUID change(AutofillProfileChangeGUID::ADD, + profile.guid(), &profile); + NotificationService::current()->Notify( + NotificationType::AUTOFILL_PROFILE_CHANGED_GUID, + Source<WebDataService>(this), + Details<AutofillProfileChangeGUID>(&change)); + + // TODO(dhollowa): Remove labels. http://crbug.com/58813 + // Send out old Label-based notification until sync can switch over to + // GUID-based notifications. + AutofillProfileChange deprecated_change(AutofillProfileChange::ADD, + profile.Label(), + &profile, + string16()); NotificationService::current()->Notify( NotificationType::AUTOFILL_PROFILE_CHANGED, Source<WebDataService>(this), - Details<AutofillProfileChange>(&change)); + Details<AutofillProfileChange>(&deprecated_change)); } request->RequestComplete(); } -void WebDataService::UpdateAutoFillProfileImpl( +void WebDataService::UpdateAutoFillProfileGUIDImpl( GenericRequest<AutoFillProfile>* request) { InitializeDatabaseIfNecessary(); if (db_ && !request->IsCancelled()) { const AutoFillProfile& profile = request->GetArgument(); - // The AUTOFILL_PROFILE_CHANGED contract for an update requires that we - // send along the label of the un-updated profile, to detect label - // changes separately. So first, we query for the existing profile. - AutoFillProfile* old_profile_ptr = NULL; - if (!db_->GetAutoFillProfileForID(profile.unique_id(), &old_profile_ptr)) - NOTREACHED(); - if (old_profile_ptr) { - scoped_ptr<AutoFillProfile> old_profile(old_profile_ptr); - if (!db_->UpdateAutoFillProfile(profile)) - NOTREACHED(); - ScheduleCommit(); - AutofillProfileChange change(AutofillProfileChange::UPDATE, - profile.Label(), &profile, - old_profile->Label()); - NotificationService::current()->Notify( - NotificationType::AUTOFILL_PROFILE_CHANGED, - Source<WebDataService>(this), - Details<AutofillProfileChange>(&change)); + // TODO(dhollowa): Remove labels. http://crbug.com/58813 + // Send out old Label-based notification until sync can switch over to + // GUID-based notifications. + // Only perform the update if the profile exists. 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. + AutoFillProfile* original_profile = NULL; + if (!db_->GetAutoFillProfileForGUID(profile.guid(), &original_profile)) { + request->RequestComplete(); + return; } - } - request->RequestComplete(); -} + scoped_ptr<AutoFillProfile> scoped_profile(original_profile); -void WebDataService::RemoveAutoFillProfileImpl( - GenericRequest<int>* request) { - InitializeDatabaseIfNecessary(); - if (db_ && !request->IsCancelled()) { - int profile_id = request->GetArgument(); - AutoFillProfile* profile = NULL; - if (!db_->GetAutoFillProfileForID(profile_id, &profile)) + if (!db_->UpdateAutoFillProfile(profile)) { NOTREACHED(); + return; + } + ScheduleCommit(); - if (profile) { - scoped_ptr<AutoFillProfile> dead_profile(profile); - if (!db_->RemoveAutoFillProfile(profile_id)) - NOTREACHED(); - ScheduleCommit(); + // Send GUID-based notification. + AutofillProfileChangeGUID change(AutofillProfileChangeGUID::UPDATE, + profile.guid(), &profile); + NotificationService::current()->Notify( + NotificationType::AUTOFILL_PROFILE_CHANGED_GUID, + Source<WebDataService>(this), + Details<AutofillProfileChangeGUID>(&change)); - AutofillProfileChange change(AutofillProfileChange::REMOVE, - dead_profile->Label(), - NULL, string16()); - NotificationService::current()->Notify( - NotificationType::AUTOFILL_PROFILE_CHANGED, - Source<WebDataService>(this), - Details<AutofillProfileChange>(&change)); - } + // TODO(dhollowa): Remove labels. http://crbug.com/58813 + // Send old Label-based notification. + AutofillProfileChange deprecated_change(AutofillProfileChange::UPDATE, + profile.Label(), &profile, + original_profile->Label()); + NotificationService::current()->Notify( + NotificationType::AUTOFILL_PROFILE_CHANGED, + Source<WebDataService>(this), + Details<AutofillProfileChange>(&deprecated_change)); } request->RequestComplete(); } @@ -1085,25 +1076,40 @@ void WebDataService::RemoveAutoFillProfileGUIDImpl( InitializeDatabaseIfNecessary(); if (db_ && !request->IsCancelled()) { std::string guid = request->GetArgument(); + + // TODO(dhollowa): Remove labels. http://crbug.com/58813 + // Send out old Label-based notification until sync can switch over to + // GUID-based notifications. AutoFillProfile* profile = NULL; - if (!db_->GetAutoFillProfileForGUID(guid, &profile)) + if (!db_->GetAutoFillProfileForGUID(guid, &profile)) { NOTREACHED(); + return; + } + scoped_ptr<AutoFillProfile> scoped_profile(profile); - if (profile) { - scoped_ptr<AutoFillProfile> dead_profile(profile); - if (!db_->RemoveAutoFillProfile(guid)) - NOTREACHED(); - ScheduleCommit(); - - // TODO(dhollowa): Deprecate and label. http://crbug.com/58813 - AutofillProfileChange change(AutofillProfileChange::REMOVE, - dead_profile->Label(), - NULL, string16()); - NotificationService::current()->Notify( - NotificationType::AUTOFILL_PROFILE_CHANGED, - Source<WebDataService>(this), - Details<AutofillProfileChange>(&change)); + if (!db_->RemoveAutoFillProfile(guid)) { + NOTREACHED(); + return; } + ScheduleCommit(); + + // Send GUID-based notification. + AutofillProfileChangeGUID change(AutofillProfileChangeGUID::REMOVE, + guid, NULL); + NotificationService::current()->Notify( + NotificationType::AUTOFILL_PROFILE_CHANGED_GUID, + Source<WebDataService>(this), + Details<AutofillProfileChangeGUID>(&change)); + + // TODO(dhollowa): Remove labels. http://crbug.com/58813 + // Send old Label-based notification. + AutofillProfileChange deprecated_change(AutofillProfileChange::REMOVE, + scoped_profile->Label(), + NULL, string16()); + NotificationService::current()->Notify( + NotificationType::AUTOFILL_PROFILE_CHANGED, + Source<WebDataService>(this), + Details<AutofillProfileChange>(&deprecated_change)); } request->RequestComplete(); } @@ -1120,67 +1126,56 @@ void WebDataService::GetAutoFillProfilesImpl(WebDataRequest* request) { request->RequestComplete(); } -void WebDataService::AddCreditCardImpl( +void WebDataService::AddCreditCardGUIDImpl( GenericRequest<CreditCard>* request) { InitializeDatabaseIfNecessary(); if (db_ && !request->IsCancelled()) { - const CreditCard& creditcard = request->GetArgument(); - if (!db_->AddCreditCard(creditcard)) + const CreditCard& credit_card = request->GetArgument(); + if (!db_->AddCreditCard(credit_card)) { NOTREACHED(); + return; + } ScheduleCommit(); - AutofillCreditCardChange change(AutofillCreditCardChange::ADD, - creditcard.Label(), &creditcard); + // Send GUID-based notification. + AutofillCreditCardChangeGUID change(AutofillCreditCardChangeGUID::ADD, + credit_card.guid(), &credit_card); NotificationService::current()->Notify( - NotificationType::AUTOFILL_CREDIT_CARD_CHANGED, + NotificationType::AUTOFILL_CREDIT_CARD_CHANGED_GUID, Source<WebDataService>(this), - Details<AutofillCreditCardChange>(&change)); + Details<AutofillCreditCardChangeGUID>(&change)); } request->RequestComplete(); } -void WebDataService::UpdateCreditCardImpl( +void WebDataService::UpdateCreditCardGUIDImpl( GenericRequest<CreditCard>* request) { InitializeDatabaseIfNecessary(); if (db_ && !request->IsCancelled()) { - const CreditCard& creditcard = request->GetArgument(); - if (!db_->UpdateCreditCard(creditcard)) + const CreditCard& credit_card = request->GetArgument(); + + // 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_->GetCreditCardForGUID(credit_card.guid(), &original_credit_card)) { + request->RequestComplete(); + return; + } + scoped_ptr<CreditCard> scoped_credit_card(original_credit_card); + + if (!db_->UpdateCreditCard(credit_card)) { NOTREACHED(); + return; + } ScheduleCommit(); - AutofillCreditCardChange change(AutofillCreditCardChange::UPDATE, - creditcard.Label(), &creditcard); + // Send GUID-based notification. + AutofillCreditCardChangeGUID change(AutofillCreditCardChangeGUID::UPDATE, + credit_card.guid(), &credit_card); NotificationService::current()->Notify( - NotificationType::AUTOFILL_CREDIT_CARD_CHANGED, + NotificationType::AUTOFILL_CREDIT_CARD_CHANGED_GUID, Source<WebDataService>(this), - Details<AutofillCreditCardChange>(&change)); - } - request->RequestComplete(); -} - -void WebDataService::RemoveCreditCardImpl( - GenericRequest<int>* request) { - InitializeDatabaseIfNecessary(); - if (db_ && !request->IsCancelled()) { - int creditcard_id = request->GetArgument(); - CreditCard* credit_card = NULL; - if (!db_->GetCreditCardForID(creditcard_id, &credit_card)) - NOTREACHED(); - - if (credit_card) { - scoped_ptr<CreditCard> dead_credit_card(credit_card); - if (!db_->RemoveCreditCard(creditcard_id)) - NOTREACHED(); - - ScheduleCommit(); - - AutofillCreditCardChange change(AutofillCreditCardChange::REMOVE, - dead_credit_card->Label(), NULL); - NotificationService::current()->Notify( - NotificationType::AUTOFILL_CREDIT_CARD_CHANGED, - Source<WebDataService>(this), - Details<AutofillCreditCardChange>(&change)); - } + Details<AutofillCreditCardChangeGUID>(&change)); } request->RequestComplete(); } @@ -1190,25 +1185,19 @@ void WebDataService::RemoveCreditCardGUIDImpl( InitializeDatabaseIfNecessary(); if (db_ && !request->IsCancelled()) { std::string guid = request->GetArgument(); - CreditCard* credit_card = NULL; - if (!db_->GetCreditCardForGUID(guid, &credit_card)) + if (!db_->RemoveCreditCard(guid)) { NOTREACHED(); - - if (credit_card) { - scoped_ptr<CreditCard> dead_credit_card(credit_card); - if (!db_->RemoveCreditCard(guid)) - NOTREACHED(); - - ScheduleCommit(); - - // TODO(dhollowa): Deprecate and label. http://crbug.com/58813 - AutofillCreditCardChange change(AutofillCreditCardChange::REMOVE, - dead_credit_card->Label(), NULL); - NotificationService::current()->Notify( - NotificationType::AUTOFILL_CREDIT_CARD_CHANGED, - Source<WebDataService>(this), - Details<AutofillCreditCardChange>(&change)); + return; } + ScheduleCommit(); + + // Send GUID-based notification. + AutofillCreditCardChangeGUID change(AutofillCreditCardChangeGUID::REMOVE, + guid, NULL); + NotificationService::current()->Notify( + NotificationType::AUTOFILL_CREDIT_CARD_CHANGED_GUID, + Source<WebDataService>(this), + Details<AutofillCreditCardChangeGUID>(&change)); } request->RequestComplete(); } @@ -1216,11 +1205,11 @@ void WebDataService::RemoveCreditCardGUIDImpl( void WebDataService::GetCreditCardsImpl(WebDataRequest* request) { InitializeDatabaseIfNecessary(); if (db_ && !request->IsCancelled()) { - std::vector<CreditCard*> creditcards; - db_->GetCreditCards(&creditcards); + std::vector<CreditCard*> credit_cards; + db_->GetCreditCards(&credit_cards); request->SetResult( new WDResult<std::vector<CreditCard*> >(AUTOFILL_CREDITCARDS_RESULT, - creditcards)); + credit_cards)); } request->RequestComplete(); } diff --git a/chrome/browser/webdata/web_data_service.h b/chrome/browser/webdata/web_data_service.h index 48a41cc..b1fbfb3 100644 --- a/chrome/browser/webdata/web_data_service.h +++ b/chrome/browser/webdata/web_data_service.h @@ -131,7 +131,7 @@ class WDTypedResult { template <class T> class WDResult : public WDTypedResult { public: - WDResult(WDResultType type, T v) : WDTypedResult(type), value_(v) { + WDResult(WDResultType type, const T& v) : WDTypedResult(type), value_(v) { } virtual ~WDResult() { @@ -225,8 +225,9 @@ class WebDataService GenericRequest(WebDataService* service, Handle handle, WebDataServiceConsumer* consumer, - T arg) : WebDataRequest(service, handle, consumer), - arg_(arg) { + const T& arg) + : WebDataRequest(service, handle, consumer), + arg_(arg) { } virtual ~GenericRequest() { @@ -246,8 +247,8 @@ class WebDataService GenericRequest2(WebDataService* service, Handle handle, WebDataServiceConsumer* consumer, - T arg1, - U arg2) + const T& arg1, + const U& arg2) : WebDataRequest(service, handle, consumer), arg1_(arg1), arg2_(arg2) { @@ -442,20 +443,14 @@ class WebDataService const string16& value); // Schedules a task to add an AutoFill profile to the web database. - void AddAutoFillProfile(const AutoFillProfile& profile); + void AddAutoFillProfileGUID(const AutoFillProfile& profile); // Schedules a task to update an AutoFill profile in the web database. - void UpdateAutoFillProfile(const AutoFillProfile& profile); - - // Schedules a task to remove an AutoFill profile from the web database. - // |profile_id| is the unique ID of the profile to remove. - // DEPRECATED - // TODO(dhollowa): Remove unique IDs. http://crbug.com/58813 - void RemoveAutoFillProfile(int profile_id); + void UpdateAutoFillProfileGUID(const AutoFillProfile& profile); // Schedules a task to remove an AutoFill profile from the web database. // |guid| is the identifer of the profile to remove. - void RemoveAutoFillProfile(const std::string& guid); + void RemoveAutoFillProfileGUID(const std::string& guid); // Initiates the request for all AutoFill profiles. The method // OnWebDataServiceRequestDone of |consumer| gets called when the request is @@ -464,20 +459,14 @@ class WebDataService Handle GetAutoFillProfiles(WebDataServiceConsumer* consumer); // Schedules a task to add credit card to the web database. - void AddCreditCard(const CreditCard& creditcard); + void AddCreditCardGUID(const CreditCard& credit_card); // Schedules a task to update credit card in the web database. - void UpdateCreditCard(const CreditCard& creditcard); - - // Schedules a task to remove a credit card from the web database. - // |creditcard_id| is the unique ID of the credit card to remove. - // DEPRECATED - // TODO(dhollowa): Remove unique IDs. http://crbug.com/58813 - void RemoveCreditCard(int creditcard_id); + void UpdateCreditCardGUID(const CreditCard& credit_card); // Schedules a task to remove a credit card from the web database. // |guid| is identifer of the credit card to remove. - void RemoveCreditCard(const std::string& guid); + void RemoveCreditCardGUID(const std::string& guid); // Initiates the request for all credit cards. The method // OnWebDataServiceRequestDone of |consumer| gets called when the request is @@ -615,16 +604,12 @@ class WebDataService GenericRequest2<base::Time, base::Time>* request); void RemoveFormValueForElementNameImpl( GenericRequest2<string16, string16>* request); - void AddAutoFillProfileImpl(GenericRequest<AutoFillProfile>* request); - void UpdateAutoFillProfileImpl(GenericRequest<AutoFillProfile>* request); - // TODO(dhollowa): Remove unique IDs. http://crbug.com/58813 - void RemoveAutoFillProfileImpl(GenericRequest<int>* request); + void AddAutoFillProfileGUIDImpl(GenericRequest<AutoFillProfile>* request); + void UpdateAutoFillProfileGUIDImpl(GenericRequest<AutoFillProfile>* request); void RemoveAutoFillProfileGUIDImpl(GenericRequest<std::string>* request); void GetAutoFillProfilesImpl(WebDataRequest* request); - void AddCreditCardImpl(GenericRequest<CreditCard>* request); - void UpdateCreditCardImpl(GenericRequest<CreditCard>* request); - // TODO(dhollowa): Remove unique IDs. http://crbug.com/58813 - void RemoveCreditCardImpl(GenericRequest<int>* request); + void AddCreditCardGUIDImpl(GenericRequest<CreditCard>* request); + void UpdateCreditCardGUIDImpl(GenericRequest<CreditCard>* request); void RemoveCreditCardGUIDImpl(GenericRequest<std::string>* request); void GetCreditCardsImpl(WebDataRequest* request); void RemoveAutoFillProfilesAndCreditCardsModifiedBetweenImpl( diff --git a/chrome/browser/webdata/web_data_service_unittest.cc b/chrome/browser/webdata/web_data_service_unittest.cc index 81234a0..2b4401c 100644 --- a/chrome/browser/webdata/web_data_service_unittest.cc +++ b/chrome/browser/webdata/web_data_service_unittest.cc @@ -62,8 +62,14 @@ class AutofillDBThreadObserverHelper : public DBThreadObserverHelper { NotificationType::AUTOFILL_PROFILE_CHANGED, NotificationService::AllSources()); registrar_.Add(&observer_, + NotificationType::AUTOFILL_PROFILE_CHANGED_GUID, + NotificationService::AllSources()); + registrar_.Add(&observer_, NotificationType::AUTOFILL_CREDIT_CARD_CHANGED, NotificationService::AllSources()); + registrar_.Add(&observer_, + NotificationType::AUTOFILL_CREDIT_CARD_CHANGED_GUID, + NotificationService::AllSources()); } }; @@ -250,52 +256,57 @@ TEST_F(WebDataServiceAutofillTest, FormFillRemoveMany) { done_event_.TimedWait(test_timeout_); } -TEST_F(WebDataServiceAutofillTest, ProfileAdd) { - AutoFillProfile profile(name1_, unique_id1_); - const AutofillProfileChange expected_change( - AutofillProfileChange::ADD, name1_, &profile, string16()); +TEST_F(WebDataServiceAutofillTest, ProfileAddGUID) { + AutoFillProfile profile; + // TODO(dhollowa): Remove this notification. http://crbug.com/58813 + // Old Label-based notifications will be sent out until Sync can switch over + // to GUID-based notifications. + profile.set_label(name1_); + const AutofillProfileChange deprecated_expected_change( + AutofillProfileChange::ADD, name1_, &profile, string16()); EXPECT_CALL( *observer_helper_->observer(), Observe(NotificationType(NotificationType::AUTOFILL_PROFILE_CHANGED), Source<WebDataService>(wds_.get()), Property(&Details<const AutofillProfileChange>::ptr, - Pointee(expected_change)))). + Pointee(deprecated_expected_change)))). WillOnce(SignalEvent(&done_event_)); - wds_->AddAutoFillProfile(profile); - done_event_.TimedWait(test_timeout_); -} - -TEST_F(WebDataServiceAutofillTest, ProfileRemove) { - AutoFillProfile profile(name1_, unique_id1_); - - EXPECT_CALL(*observer_helper_->observer(), Observe(_, _, _)). - WillOnce(SignalEvent(&done_event_)); - wds_->AddAutoFillProfile(profile); - done_event_.TimedWait(test_timeout_); - - const AutofillProfileChange expected_change( - AutofillProfileChange::REMOVE, name1_, NULL, string16()); + // Check that GUID-based notification was sent. + const AutofillProfileChangeGUID expected_change( + AutofillProfileChangeGUID::ADD, profile.guid(), &profile); EXPECT_CALL( *observer_helper_->observer(), - Observe(NotificationType(NotificationType::AUTOFILL_PROFILE_CHANGED), + Observe(NotificationType(NotificationType::AUTOFILL_PROFILE_CHANGED_GUID), Source<WebDataService>(wds_.get()), - Property(&Details<const AutofillProfileChange>::ptr, + Property(&Details<const AutofillProfileChangeGUID>::ptr, Pointee(expected_change)))). - WillOnce(SignalEvent(&done_event_)); + WillOnce(DoDefault()); - wds_->RemoveAutoFillProfile(profile.unique_id()); + wds_->AddAutoFillProfileGUID(profile); done_event_.TimedWait(test_timeout_); + + // Check that it was added. + AutofillWebDataServiceConsumer<std::vector<AutoFillProfile*> > consumer; + WebDataService::Handle handle = wds_->GetAutoFillProfiles(&consumer); + MessageLoop::current()->Run(); + EXPECT_EQ(handle, consumer.handle()); + ASSERT_EQ(1U, consumer.result().size()); + EXPECT_EQ(profile, *consumer.result()[0]); + STLDeleteElements(&consumer.result()); } TEST_F(WebDataServiceAutofillTest, ProfileRemoveGUID) { - AutoFillProfile profile(name1_, unique_id1_); + AutoFillProfile profile; + profile.set_label(name1_); // Add a profile. EXPECT_CALL(*observer_helper_->observer(), Observe(_, _, _)). + Times(2). + WillOnce(DoDefault()). WillOnce(SignalEvent(&done_event_)); - wds_->AddAutoFillProfile(profile); + wds_->AddAutoFillProfileGUID(profile); done_event_.TimedWait(test_timeout_); // Check that it was added. @@ -307,17 +318,32 @@ TEST_F(WebDataServiceAutofillTest, ProfileRemoveGUID) { EXPECT_EQ(profile, *consumer.result()[0]); STLDeleteElements(&consumer.result()); - // Remove the profile. - const AutofillProfileChange expected_change( + // TODO(dhollowa): Remove this notification. http://crbug.com/58813 + // Old Label-based notifications will be sent out until Sync can switch over + // to GUID-based notifications. + const AutofillProfileChange deprecated_expected_change( AutofillProfileChange::REMOVE, name1_, NULL, string16()); EXPECT_CALL( *observer_helper_->observer(), Observe(NotificationType(NotificationType::AUTOFILL_PROFILE_CHANGED), Source<WebDataService>(wds_.get()), Property(&Details<const AutofillProfileChange>::ptr, - Pointee(expected_change)))). + Pointee(deprecated_expected_change)))). WillOnce(SignalEvent(&done_event_)); - wds_->RemoveAutoFillProfile(profile.guid()); + + // Check that GUID-based notification was sent. + const AutofillProfileChangeGUID expected_change( + AutofillProfileChangeGUID::REMOVE, profile.guid(), NULL); + EXPECT_CALL( + *observer_helper_->observer(), + Observe(NotificationType(NotificationType::AUTOFILL_PROFILE_CHANGED_GUID), + Source<WebDataService>(wds_.get()), + Property(&Details<const AutofillProfileChangeGUID>::ptr, + Pointee(expected_change)))). + WillOnce(DoDefault()); + + // Remove the profile. + wds_->RemoveAutoFillProfileGUID(profile.guid()); done_event_.TimedWait(test_timeout_); // Check that it was removed. @@ -328,83 +354,111 @@ TEST_F(WebDataServiceAutofillTest, ProfileRemoveGUID) { ASSERT_EQ(0U, consumer2.result().size()); } -TEST_F(WebDataServiceAutofillTest, ProfileUpdate) { - AutoFillProfile profile1(name1_, unique_id1_); - AutoFillProfile profile2(name2_, unique_id2_); +TEST_F(WebDataServiceAutofillTest, ProfileUpdateGUID) { + AutoFillProfile profile1; + profile1.SetInfo(AutoFillType(NAME_FIRST), ASCIIToUTF16("Abe")); + AutoFillProfile profile2; + profile2.SetInfo(AutoFillType(NAME_FIRST), ASCIIToUTF16("Alice")); EXPECT_CALL(*observer_helper_->observer(), Observe(_, _, _)). - Times(2). + WillOnce(DoDefault()). + WillOnce(DoDefault()). WillOnce(DoDefault()). WillOnce(SignalEvent(&done_event_)); - wds_->AddAutoFillProfile(profile1); - wds_->AddAutoFillProfile(profile2); - + wds_->AddAutoFillProfileGUID(profile1); + wds_->AddAutoFillProfileGUID(profile2); done_event_.TimedWait(test_timeout_); - AutoFillProfile profile1_delta(profile1); - string16 new_label(ASCIIToUTF16("new_label!")); - profile1_delta.set_label(new_label); - const AutofillProfileChange expected_change( - AutofillProfileChange::UPDATE, new_label, &profile1_delta, name1_); + // Check that they were added. + AutofillWebDataServiceConsumer<std::vector<AutoFillProfile*> > consumer; + WebDataService::Handle handle = wds_->GetAutoFillProfiles(&consumer); + MessageLoop::current()->Run(); + EXPECT_EQ(handle, consumer.handle()); + ASSERT_EQ(2U, consumer.result().size()); + EXPECT_EQ(profile1, *consumer.result()[0]); + EXPECT_EQ(profile2, *consumer.result()[1]); + STLDeleteElements(&consumer.result()); + // TODO(dhollowa): Remove this notification. http://crbug.com/58813 + // Old Label-based notifications will be sent out until Sync can switch over + // to GUID-based notifications. + AutoFillProfile deprecated_profile1_changed(profile1); + deprecated_profile1_changed.SetInfo(AutoFillType(NAME_FIRST), + ASCIIToUTF16("Bill")); + const AutofillProfileChangeGUID deprecated_expected_change( + AutofillProfileChangeGUID::UPDATE, profile1.guid(), + &deprecated_profile1_changed); EXPECT_CALL( *observer_helper_->observer(), Observe(NotificationType(NotificationType::AUTOFILL_PROFILE_CHANGED), Source<WebDataService>(wds_.get()), - Property(&Details<const AutofillProfileChange>::ptr, - Pointee(expected_change)))). + Property(&Details<const AutofillProfileChangeGUID>::ptr, + Pointee(deprecated_expected_change)))). WillOnce(SignalEvent(&done_event_)); - wds_->UpdateAutoFillProfile(profile1_delta); - done_event_.TimedWait(test_timeout_); -} - -TEST_F(WebDataServiceAutofillTest, CreditAdd) { - CreditCard card(name1_, unique_id1_); - const AutofillCreditCardChange expected_change( - AutofillCreditCardChange::ADD, name1_, &card); + AutoFillProfile profile1_changed(profile1); + profile1_changed.SetInfo(AutoFillType(NAME_FIRST), ASCIIToUTF16("Bill")); + const AutofillProfileChangeGUID expected_change( + AutofillProfileChangeGUID::UPDATE, profile1.guid(), &profile1_changed); EXPECT_CALL( *observer_helper_->observer(), - Observe(NotificationType(NotificationType::AUTOFILL_CREDIT_CARD_CHANGED), + Observe(NotificationType(NotificationType::AUTOFILL_PROFILE_CHANGED_GUID), Source<WebDataService>(wds_.get()), - Property(&Details<const AutofillCreditCardChange>::ptr, + Property(&Details<const AutofillProfileChangeGUID>::ptr, Pointee(expected_change)))). - WillOnce(SignalEvent(&done_event_)); + WillOnce(DoDefault()); - wds_->AddCreditCard(card); + // Update the profile. + wds_->UpdateAutoFillProfileGUID(profile1_changed); done_event_.TimedWait(test_timeout_); -} -TEST_F(WebDataServiceAutofillTest, CreditRemove) { - CreditCard card(name1_, unique_id1_); - EXPECT_CALL(*observer_helper_->observer(), Observe(_, _, _)). - WillOnce(SignalEvent(&done_event_)); - wds_->AddCreditCard(card); - done_event_.TimedWait(test_timeout_); + // Check that the updates were made. + AutofillWebDataServiceConsumer<std::vector<AutoFillProfile*> > consumer2; + WebDataService::Handle handle2 = wds_->GetAutoFillProfiles(&consumer2); + MessageLoop::current()->Run(); + EXPECT_EQ(handle2, consumer2.handle()); + ASSERT_EQ(2U, consumer2.result().size()); + EXPECT_NE(profile1, *consumer2.result()[0]); + EXPECT_EQ(profile1_changed, *consumer2.result()[0]); + EXPECT_EQ(profile2, *consumer2.result()[1]); + STLDeleteElements(&consumer2.result()); +} - const AutofillCreditCardChange expected_change( - AutofillCreditCardChange::REMOVE, name1_, NULL); +TEST_F(WebDataServiceAutofillTest, CreditAddGUID) { + CreditCard card; + const AutofillCreditCardChangeGUID expected_change( + AutofillCreditCardChangeGUID::ADD, card.guid(), &card); EXPECT_CALL( *observer_helper_->observer(), - Observe(NotificationType(NotificationType::AUTOFILL_CREDIT_CARD_CHANGED), + Observe( + NotificationType(NotificationType::AUTOFILL_CREDIT_CARD_CHANGED_GUID), Source<WebDataService>(wds_.get()), - Property(&Details<const AutofillCreditCardChange>::ptr, + Property(&Details<const AutofillCreditCardChangeGUID>::ptr, Pointee(expected_change)))). WillOnce(SignalEvent(&done_event_)); - wds_->RemoveCreditCard(card.unique_id()); + wds_->AddCreditCardGUID(card); done_event_.TimedWait(test_timeout_); + + // Check that it was added. + AutofillWebDataServiceConsumer<std::vector<CreditCard*> > consumer; + WebDataService::Handle handle = wds_->GetCreditCards(&consumer); + MessageLoop::current()->Run(); + EXPECT_EQ(handle, consumer.handle()); + ASSERT_EQ(1U, consumer.result().size()); + EXPECT_EQ(card, *consumer.result()[0]); + STLDeleteElements(&consumer.result()); } TEST_F(WebDataServiceAutofillTest, CreditCardRemoveGUID) { - CreditCard card(name1_, unique_id1_); + CreditCard credit_card; // Add a credit card. EXPECT_CALL(*observer_helper_->observer(), Observe(_, _, _)). WillOnce(SignalEvent(&done_event_)); - wds_->AddCreditCard(card); + wds_->AddCreditCardGUID(credit_card); done_event_.TimedWait(test_timeout_); // Check that it was added. @@ -413,20 +467,21 @@ TEST_F(WebDataServiceAutofillTest, CreditCardRemoveGUID) { MessageLoop::current()->Run(); EXPECT_EQ(handle, consumer.handle()); ASSERT_EQ(1U, consumer.result().size()); - EXPECT_EQ(card, *consumer.result()[0]); + EXPECT_EQ(credit_card, *consumer.result()[0]); STLDeleteElements(&consumer.result()); // Remove the credit card. - const AutofillCreditCardChange expected_change( - AutofillCreditCardChange::REMOVE, name1_, NULL); + const AutofillCreditCardChangeGUID expected_change( + AutofillCreditCardChangeGUID::REMOVE, credit_card.guid(), NULL); EXPECT_CALL( *observer_helper_->observer(), - Observe(NotificationType(NotificationType::AUTOFILL_CREDIT_CARD_CHANGED), + Observe( + NotificationType(NotificationType::AUTOFILL_CREDIT_CARD_CHANGED_GUID), Source<WebDataService>(wds_.get()), - Property(&Details<const AutofillCreditCardChange>::ptr, + Property(&Details<const AutofillCreditCardChangeGUID>::ptr, Pointee(expected_change)))). WillOnce(SignalEvent(&done_event_)); - wds_->RemoveCreditCard(card.guid()); + wds_->RemoveCreditCardGUID(credit_card.guid()); done_event_.TimedWait(test_timeout_); // Check that it was removed. @@ -437,31 +492,55 @@ TEST_F(WebDataServiceAutofillTest, CreditCardRemoveGUID) { ASSERT_EQ(0U, consumer2.result().size()); } -TEST_F(WebDataServiceAutofillTest, CreditUpdate) { - CreditCard card1(name1_, unique_id1_); - CreditCard card2(name2_, unique_id2_); +TEST_F(WebDataServiceAutofillTest, CreditUpdateGUID) { + CreditCard card1; + card1.SetInfo(AutoFillType(CREDIT_CARD_NAME), ASCIIToUTF16("Abe")); + CreditCard card2; + card2.SetInfo(AutoFillType(CREDIT_CARD_NAME), ASCIIToUTF16("Alice")); EXPECT_CALL(*observer_helper_->observer(), Observe(_, _, _)). Times(2). WillOnce(DoDefault()). WillOnce(SignalEvent(&done_event_)); - wds_->AddCreditCard(card1); - wds_->AddCreditCard(card2); + wds_->AddCreditCardGUID(card1); + wds_->AddCreditCardGUID(card2); done_event_.TimedWait(test_timeout_); - CreditCard card1_delta(card1); - card1_delta.set_label(ASCIIToUTF16("new_label!")); - const AutofillCreditCardChange expected_change( - AutofillCreditCardChange::UPDATE, name1_, &card1_delta); + // Check that they got added. + AutofillWebDataServiceConsumer<std::vector<CreditCard*> > consumer; + WebDataService::Handle handle = wds_->GetCreditCards(&consumer); + MessageLoop::current()->Run(); + EXPECT_EQ(handle, consumer.handle()); + ASSERT_EQ(2U, consumer.result().size()); + EXPECT_EQ(card1, *consumer.result()[0]); + EXPECT_EQ(card2, *consumer.result()[1]); + STLDeleteElements(&consumer.result()); + + CreditCard card1_changed(card1); + card1_changed.SetInfo(AutoFillType(CREDIT_CARD_NAME), ASCIIToUTF16("Bill")); + const AutofillCreditCardChangeGUID expected_change( + AutofillCreditCardChangeGUID::UPDATE, card1.guid(), &card1_changed); EXPECT_CALL( *observer_helper_->observer(), - Observe(NotificationType(NotificationType::AUTOFILL_CREDIT_CARD_CHANGED), + Observe( + NotificationType(NotificationType::AUTOFILL_CREDIT_CARD_CHANGED_GUID), Source<WebDataService>(wds_.get()), - Property(&Details<const AutofillCreditCardChange>::ptr, + Property(&Details<const AutofillCreditCardChangeGUID>::ptr, Pointee(expected_change)))). WillOnce(SignalEvent(&done_event_)); - wds_->UpdateCreditCard(card1_delta); + wds_->UpdateCreditCardGUID(card1_changed); done_event_.TimedWait(test_timeout_); + + // Check that the updates were made. + AutofillWebDataServiceConsumer<std::vector<CreditCard*> > consumer2; + WebDataService::Handle handle2 = wds_->GetCreditCards(&consumer2); + MessageLoop::current()->Run(); + EXPECT_EQ(handle2, consumer2.handle()); + ASSERT_EQ(2U, consumer2.result().size()); + EXPECT_NE(card1, *consumer2.result()[0]); + EXPECT_EQ(card1_changed, *consumer2.result()[0]); + EXPECT_EQ(card2, *consumer2.result()[1]); + STLDeleteElements(&consumer2.result()); } diff --git a/chrome/browser/webdata/web_database.cc b/chrome/browser/webdata/web_database.cc index 138fc7c..2b9c029 100644 --- a/chrome/browser/webdata/web_database.cc +++ b/chrome/browser/webdata/web_database.cc @@ -109,9 +109,10 @@ using webkit_glue::PasswordForm; // user with the AutoFill dialog. Most of the columns are // standard entries in a contact information form. // +// guid A guid string to uniquely identify the profile. +// Added in version 31. // label The label of the profile. Presented to the user when // selecting profiles. -// unique_id The unique ID of this profile. // first_name // middle_name // last_name @@ -127,45 +128,21 @@ using webkit_glue::PasswordForm; // fax // date_modified The date on which this profile was last modified. // Added in version 30. -// guid A guid string to uniquely identify the profile. This -// will eventually replace the unique_id above. We need -// to keep both during the transition. -// Added in version 31. -// TODO(dhollowa): Deprecate and remove unique_id. -// http://crbug.com/58813 // // credit_cards This table contains credit card data added by the user // with the AutoFill dialog. Most of the columns are // standard entries in a credit card form. // +// guid A guid string to uniquely identify the profile. +// Added in version 31. // label The label of the credit card. Presented to the user // when selecting credit cards. -// unique_id The unique ID of this credit card. // name_on_card -// type -// card_number Before version 23 stores credit card number, 23 and -// after stores empty string. // expiration_month // expiration_year -// verification_code Before version 23 stores the CVC/CVV/CVV2 card security -// code. After that stores the empty string. -// billing_address A foreign key into the autofill_profiles table. -// shipping_address A foreign key into the autofill_profiles table. -// For the following two fields encryption is used. Currently it uses -// Encryptor, that does encryption on windows only. As on the other -// systems this file is readable by owner only, it is good for now. -// For potentially going over the wire other encryption is used, see -// chrome/browser/sync/protocol/autofill_specifics.proto // card_number_encrypted Stores encrypted credit card number. -// verification_code_encrypted The CVC/CVV/CVV2 card security code. // date_modified The date on which this entry was last modified. // Added in version 30. -// guid A guid string to uniquely identify the profile. This -// will eventually replace the unique_id above. We need -// to keep both during the transition. -// Added in version 31. -// TODO(dhollowa): Deprecate and remove unique_id. -// http://crbug.com/58813 // // web_app_icons // url URL of the web app. @@ -188,8 +165,8 @@ typedef std::vector<Tuple3<int64, string16, string16> > AutofillElementList; // Current version number. Note: when changing the current version number, // corresponding changes must happen in the unit tests, and new migration test // added. See |WebDatabaseMigrationTest::kCurrentTestedVersionNumber|. -const int kCurrentVersionNumber = 31; -const int kCompatibleVersionNumber = 31; +const int kCurrentVersionNumber = 32; +const int kCompatibleVersionNumber = 32; // ID of the url column in keywords. const int kUrlIdPosition = 16; @@ -278,8 +255,9 @@ string16 LimitDataSize(const string16& data) { void BindAutoFillProfileToStatement(const AutoFillProfile& profile, sql::Statement* s) { - s->BindString16(0, profile.Label()); - s->BindInt(1, profile.unique_id()); + DCHECK(guid::IsValidGUID(profile.guid())); + s->BindString(0, profile.guid()); + s->BindString16(1, profile.Label()); string16 text = profile.GetFieldText(AutoFillType(NAME_FIRST)); s->BindString16(2, LimitDataSize(text)); @@ -308,13 +286,14 @@ void BindAutoFillProfileToStatement(const AutoFillProfile& profile, text = profile.GetFieldText(AutoFillType(PHONE_FAX_WHOLE_NUMBER)); s->BindString16(14, LimitDataSize(text)); s->BindInt64(15, Time::Now().ToTimeT()); - DCHECK(guid::IsValidGUID(profile.guid())); - s->BindString(16, profile.guid()); } AutoFillProfile* AutoFillProfileFromStatement(const sql::Statement& s) { - AutoFillProfile* profile = new AutoFillProfile(s.ColumnString16(0), - s.ColumnInt(1)); + AutoFillProfile* profile = new AutoFillProfile; + profile->set_guid(s.ColumnString(0)); + DCHECK(guid::IsValidGUID(profile->guid())); + profile->set_label(s.ColumnString16(1)); + profile->SetInfo(AutoFillType(NAME_FIRST), s.ColumnString16(2)); profile->SetInfo(AutoFillType(NAME_MIDDLE), @@ -342,79 +321,53 @@ AutoFillProfile* AutoFillProfileFromStatement(const sql::Statement& s) { profile->SetInfo(AutoFillType(PHONE_FAX_WHOLE_NUMBER), s.ColumnString16(14)); // Intentionally skip column 15, which stores the profile's modification date. - profile->set_guid(s.ColumnString(16)); - DCHECK(guid::IsValidGUID(profile->guid())); return profile; } void BindCreditCardToStatement(const CreditCard& credit_card, sql::Statement* s) { - s->BindString16(0, credit_card.Label()); - s->BindInt(1, credit_card.unique_id()); + DCHECK(guid::IsValidGUID(credit_card.guid())); + s->BindString(0, credit_card.guid()); + s->BindString16(1, credit_card.Label()); string16 text = credit_card.GetFieldText(AutoFillType(CREDIT_CARD_NAME)); s->BindString16(2, LimitDataSize(text)); - text = credit_card.GetFieldText(AutoFillType(CREDIT_CARD_TYPE)); - s->BindString16(3, LimitDataSize(text)); - text.clear(); // No unencrypted cc info. - s->BindString16(4, LimitDataSize(text)); text = credit_card.GetFieldText(AutoFillType(CREDIT_CARD_EXP_MONTH)); - s->BindString16(5, LimitDataSize(text)); + s->BindString16(3, LimitDataSize(text)); text = credit_card.GetFieldText(AutoFillType(CREDIT_CARD_EXP_4_DIGIT_YEAR)); - s->BindString16(6, LimitDataSize(text)); - text.clear(); - s->BindString16(7, LimitDataSize(text)); - s->BindInt(8, credit_card.billing_address_id()); - // We don't store the shipping address anymore. - text.clear(); - s->BindString16(9, LimitDataSize(text)); + s->BindString16(4, LimitDataSize(text)); text = credit_card.GetFieldText(AutoFillType(CREDIT_CARD_NUMBER)); std::string encrypted_data; Encryptor::EncryptString16(text, &encrypted_data); - s->BindBlob(10, encrypted_data.data(), + s->BindBlob(5, encrypted_data.data(), static_cast<int>(encrypted_data.length())); - // We don't store the CVV anymore. - text.clear(); - s->BindBlob(11, text.data(), static_cast<int>(text.length())); - s->BindInt64(12, Time::Now().ToTimeT()); - DCHECK(guid::IsValidGUID(credit_card.guid())); - s->BindString(13, credit_card.guid()); + s->BindInt64(6, Time::Now().ToTimeT()); } CreditCard* CreditCardFromStatement(const sql::Statement& s) { - CreditCard* credit_card = new CreditCard(s.ColumnString16(0), s.ColumnInt(1)); + CreditCard* credit_card = new CreditCard; + + credit_card->set_guid(s.ColumnString(0)); + DCHECK(guid::IsValidGUID(credit_card->guid())); + credit_card->set_label(s.ColumnString16(1)); + credit_card->SetInfo(AutoFillType(CREDIT_CARD_NAME), s.ColumnString16(2)); - credit_card->SetInfo(AutoFillType(CREDIT_CARD_TYPE), + credit_card->SetInfo(AutoFillType(CREDIT_CARD_EXP_MONTH), s.ColumnString16(3)); - string16 credit_card_number = s.ColumnString16(4); - // It could be non-empty prior to version 23. After that it encrypted in - // the column 10. - if (credit_card_number.empty()) { - int encrypted_cc_len = s.ColumnByteLength(10); - std::string encrypted_cc; - if (encrypted_cc_len) { - encrypted_cc.resize(encrypted_cc_len); - memcpy(&encrypted_cc[0], s.ColumnBlob(10), encrypted_cc_len); - Encryptor::DecryptString16(encrypted_cc, &credit_card_number); - } + credit_card->SetInfo(AutoFillType(CREDIT_CARD_EXP_4_DIGIT_YEAR), + s.ColumnString16(4)); + int encrypted_number_len = s.ColumnByteLength(5); + string16 credit_card_number; + if (encrypted_number_len) { + std::string encrypted_number; + encrypted_number.resize(encrypted_number_len); + memcpy(&encrypted_number[0], s.ColumnBlob(5), encrypted_number_len); + Encryptor::DecryptString16(encrypted_number, &credit_card_number); } credit_card->SetInfo(AutoFillType(CREDIT_CARD_NUMBER), credit_card_number); - credit_card->SetInfo(AutoFillType(CREDIT_CARD_EXP_MONTH), - s.ColumnString16(5)); - credit_card->SetInfo(AutoFillType(CREDIT_CARD_EXP_4_DIGIT_YEAR), - s.ColumnString16(6)); - - string16 credit_card_verification_code = s.ColumnString16(7); - // We don't store the CVV anymore. - credit_card->set_billing_address_id(s.ColumnInt(8)); - // We don't store the shipping address anymore. - // Column 10 is processed above. - // We don't store the encrypted CVV anymore. - // Intentionally skip column 12, which stores the modification date. - credit_card->set_guid(s.ColumnString(13)); - DCHECK(guid::IsValidGUID(credit_card->guid())); + // Intentionally skip column 6, which stores the modification date. return credit_card; } @@ -766,8 +719,8 @@ bool WebDatabase::InitAutofillDatesTable() { bool WebDatabase::InitAutoFillProfilesTable() { if (!db_.DoesTableExist("autofill_profiles")) { if (!db_.Execute("CREATE TABLE autofill_profiles ( " + "guid VARCHAR PRIMARY KEY, " "label VARCHAR, " - "unique_id INTEGER PRIMARY KEY, " "first_name VARCHAR, " "middle_name VARCHAR, " "last_name VARCHAR, " @@ -781,8 +734,7 @@ bool WebDatabase::InitAutoFillProfilesTable() { "country VARCHAR, " "phone VARCHAR, " "fax VARCHAR, " - "date_modified INTEGER NOT NULL DEFAULT 0, " - "guid VARCHAR NOT NULL DEFAULT \"\")")) { + "date_modified INTEGER NOT NULL DEFAULT 0)")) { NOTREACHED(); return false; } @@ -798,20 +750,13 @@ bool WebDatabase::InitAutoFillProfilesTable() { bool WebDatabase::InitCreditCardsTable() { if (!db_.DoesTableExist("credit_cards")) { if (!db_.Execute("CREATE TABLE credit_cards ( " + "guid VARCHAR PRIMARY KEY, " "label VARCHAR, " - "unique_id INTEGER PRIMARY KEY, " "name_on_card VARCHAR, " - "type VARCHAR, " - "card_number VARCHAR, " "expiration_month INTEGER, " "expiration_year INTEGER, " - "verification_code VARCHAR, " - "billing_address VARCHAR, " - "shipping_address VARCHAR, " "card_number_encrypted BLOB, " - "verification_code_encrypted BLOB, " - "date_modified INTEGER NOT NULL DEFAULT 0, " - "guid VARCHAR NOT NULL DEFAULT \"\")")) { + "date_modified INTEGER NOT NULL DEFAULT 0)")) { NOTREACHED(); return false; } @@ -1659,10 +1604,10 @@ bool WebDatabase::RemoveFormElement(const string16& name, bool WebDatabase::AddAutoFillProfile(const AutoFillProfile& profile) { sql::Statement s(db_.GetUniqueStatement( "INSERT INTO autofill_profiles" - "(label, unique_id, first_name, middle_name, last_name, email," + "(guid, label, first_name, middle_name, last_name, email," " company_name, address_line_1, address_line_2, city, state, zipcode," - " country, phone, fax, date_modified, guid)" - "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")); + " country, phone, fax, date_modified)" + "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")); if (!s) { NOTREACHED() << "Statement prepare failed"; return false; @@ -1682,9 +1627,9 @@ bool WebDatabase::GetAutoFillProfileForLabel(const string16& label, AutoFillProfile** profile) { DCHECK(profile); sql::Statement s(db_.GetUniqueStatement( - "SELECT label, unique_id, first_name, middle_name, last_name, email, " + "SELECT guid, label, first_name, middle_name, last_name, email, " "company_name, address_line_1, address_line_2, city, state, zipcode, " - "country, phone, fax, date_modified, guid " + "country, phone, fax, date_modified " "FROM autofill_profiles " "WHERE label = ?")); if (!s) { @@ -1706,9 +1651,9 @@ bool WebDatabase::GetAutoFillProfileForGUID(const std::string& guid, DCHECK(guid::IsValidGUID(guid)); DCHECK(profile); sql::Statement s(db_.GetUniqueStatement( - "SELECT label, unique_id, first_name, middle_name, last_name, email, " + "SELECT guid, label, first_name, middle_name, last_name, email, " "company_name, address_line_1, address_line_2, city, state, zipcode, " - "country, phone, fax, date_modified, guid " + "country, phone, fax, date_modified " "FROM autofill_profiles " "WHERE guid = ?")); if (!s) { @@ -1731,9 +1676,9 @@ bool WebDatabase::GetAutoFillProfiles( profiles->clear(); sql::Statement s(db_.GetUniqueStatement( - "SELECT label, unique_id, first_name, middle_name, last_name, email, " + "SELECT guid, label, first_name, middle_name, last_name, email, " "company_name, address_line_1, address_line_2, city, state, zipcode, " - "country, phone, fax, date_modified, guid " + "country, phone, fax, date_modified " "FROM autofill_profiles")); if (!s) { NOTREACHED() << "Statement prepare failed"; @@ -1747,39 +1692,26 @@ bool WebDatabase::GetAutoFillProfiles( } bool WebDatabase::UpdateAutoFillProfile(const AutoFillProfile& profile) { - DCHECK(profile.unique_id()); + DCHECK(guid::IsValidGUID(profile.guid())); sql::Statement s(db_.GetUniqueStatement( "UPDATE autofill_profiles " - "SET label=?, unique_id=?, first_name=?, middle_name=?, last_name=?, " + "SET guid=?, label=?, first_name=?, middle_name=?, last_name=?, " " email=?, company_name=?, address_line_1=?, address_line_2=?, " " city=?, state=?, zipcode=?, country=?, phone=?, fax=?, " - " date_modified=?, guid=? " - "WHERE unique_id=?")); + " date_modified=? " + "WHERE guid=?")); if (!s) { NOTREACHED() << "Statement prepare failed"; return false; } BindAutoFillProfileToStatement(profile, &s); - s.BindInt(17, profile.unique_id()); + s.BindString(16, profile.guid()); bool result = s.Run(); DCHECK_GT(db_.GetLastChangeCount(), 0); return result; } -bool WebDatabase::RemoveAutoFillProfile(int profile_id) { - DCHECK_NE(0, profile_id); - sql::Statement s(db_.GetUniqueStatement( - "DELETE FROM autofill_profiles WHERE unique_id = ?")); - if (!s) { - NOTREACHED() << "Statement prepare failed"; - return false; - } - - s.BindInt(0, profile_id); - return s.Run(); -} - bool WebDatabase::RemoveAutoFillProfile(const std::string& guid) { DCHECK(guid::IsValidGUID(guid)); sql::Statement s(db_.GetUniqueStatement( @@ -1793,34 +1725,12 @@ bool WebDatabase::RemoveAutoFillProfile(const std::string& guid) { return s.Run(); } -bool WebDatabase::GetAutoFillProfileForID(int profile_id, - AutoFillProfile** profile) { - sql::Statement s(db_.GetUniqueStatement( - "SELECT label, unique_id, first_name, middle_name, last_name, email, " - "company_name, address_line_1, address_line_2, city, state, zipcode, " - "country, phone, fax, date_modified, guid " - "FROM autofill_profiles " - "WHERE unique_id = ?")); - if (!s) { - NOTREACHED() << "Statement prepare failed"; - return false; - } - - s.BindInt(0, profile_id); - if (s.Step()) - *profile = AutoFillProfileFromStatement(s); - - return s.Succeeded(); -} - bool WebDatabase::AddCreditCard(const CreditCard& credit_card) { sql::Statement s(db_.GetUniqueStatement( "INSERT INTO credit_cards" - "(label, unique_id, name_on_card, type, card_number, expiration_month," - " expiration_year, verification_code, billing_address, shipping_address," - " card_number_encrypted, verification_code_encrypted, date_modified," - " guid)" - "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)")); + "(guid, label, name_on_card, expiration_month, expiration_year, " + "card_number_encrypted, date_modified)" + "VALUES (?,?,?,?,?,?,?)")); if (!s) { NOTREACHED() << "Statement prepare failed"; return false; @@ -1841,10 +1751,8 @@ bool WebDatabase::GetCreditCardForLabel(const string16& label, CreditCard** credit_card) { DCHECK(credit_card); sql::Statement s(db_.GetUniqueStatement( - "SELECT label, unique_id, name_on_card, type, card_number, " - "expiration_month, expiration_year, verification_code, billing_address, " - "shipping_address, card_number_encrypted, verification_code_encrypted, " - "date_modified, guid " + "SELECT guid, label, name_on_card, expiration_month, expiration_year, " + "card_number_encrypted, date_modified " "FROM credit_cards " "WHERE label = ?")); if (!s) { @@ -1861,37 +1769,12 @@ bool WebDatabase::GetCreditCardForLabel(const string16& label, return s.Succeeded(); } -bool WebDatabase::GetCreditCardForID(int credit_card_id, - CreditCard** credit_card) { - sql::Statement s(db_.GetUniqueStatement( - "SELECT label, unique_id, name_on_card, type, card_number, " - "expiration_month, expiration_year, verification_code, billing_address, " - "shipping_address, card_number_encrypted, verification_code_encrypted, " - "date_modified, guid " - "FROM credit_cards " - "WHERE unique_id = ?")); - if (!s) { - NOTREACHED() << "Statement prepare failed"; - return false; - } - - s.BindInt(0, credit_card_id); - if (!s.Step()) - return false; - - *credit_card = CreditCardFromStatement(s); - - return s.Succeeded(); -} - bool WebDatabase::GetCreditCardForGUID(const std::string& guid, CreditCard** credit_card) { DCHECK(guid::IsValidGUID(guid)); sql::Statement s(db_.GetUniqueStatement( - "SELECT label, unique_id, name_on_card, type, card_number, " - "expiration_month, expiration_year, verification_code, billing_address, " - "shipping_address, card_number_encrypted, verification_code_encrypted, " - "date_modified, guid " + "SELECT guid, label, name_on_card, expiration_month, expiration_year, " + "card_number_encrypted, date_modified " "FROM credit_cards " "WHERE guid = ?")); if (!s) { @@ -1914,10 +1797,8 @@ bool WebDatabase::GetCreditCards( credit_cards->clear(); sql::Statement s(db_.GetUniqueStatement( - "SELECT label, unique_id, name_on_card, type, card_number, " - "expiration_month, expiration_year, verification_code, billing_address, " - "shipping_address, card_number_encrypted, verification_code_encrypted, " - "date_modified, guid " + "SELECT guid, label, name_on_card, expiration_month, expiration_year, " + "card_number_encrypted, date_modified " "FROM credit_cards")); if (!s) { NOTREACHED() << "Statement prepare failed"; @@ -1931,39 +1812,24 @@ bool WebDatabase::GetCreditCards( } bool WebDatabase::UpdateCreditCard(const CreditCard& credit_card) { - DCHECK(credit_card.unique_id()); + DCHECK(guid::IsValidGUID(credit_card.guid())); sql::Statement s(db_.GetUniqueStatement( "UPDATE credit_cards " - "SET label=?, unique_id=?, name_on_card=?, type=?, card_number=?, " - " expiration_month=?, expiration_year=?, verification_code=?, " - " billing_address=?, shipping_address=?, card_number_encrypted=?, " - " verification_code_encrypted=?, date_modified=?, guid=?" - "WHERE unique_id=?")); + "SET guid=?, label=?, name_on_card=?, expiration_month=?, " + " expiration_year=?, card_number_encrypted=?, date_modified=? " + "WHERE guid=?")); if (!s) { NOTREACHED() << "Statement prepare failed"; return false; } BindCreditCardToStatement(credit_card, &s); - s.BindInt(14, credit_card.unique_id()); + s.BindString(7, credit_card.guid()); bool result = s.Run(); DCHECK_GT(db_.GetLastChangeCount(), 0); return result; } -bool WebDatabase::RemoveCreditCard(int credit_card_id) { - DCHECK_NE(0, credit_card_id); - sql::Statement s(db_.GetUniqueStatement( - "DELETE FROM credit_cards WHERE unique_id = ?")); - if (!s) { - NOTREACHED() << "Statement prepare failed"; - return false; - } - - s.BindInt(0, credit_card_id); - return s.Run(); -} - bool WebDatabase::RemoveCreditCard(const std::string& guid) { DCHECK(guid::IsValidGUID(guid)); sql::Statement s(db_.GetUniqueStatement( @@ -2193,13 +2059,23 @@ sql::InitStatus WebDatabase::MigrateOldVersionsAsNeeded(){ NOTREACHED(); return sql::INIT_FAILURE; } - query = "DELETE FROM credit_cards WHERE (" + credit_cards_is_too_big + - ") OR label IN (SELECT label FROM autofill_profiles WHERE " + - autofill_profiles_is_too_big + ")"; - if (!db_.Execute(query.c_str())) { - LOG(WARNING) << "Unable to update web database to version 24."; - NOTREACHED(); - return sql::INIT_FAILURE; + // Only delete from legacy credit card tables where specific columns + // exist. + if (db_.DoesColumnExist("credit_cards", "label") && + db_.DoesColumnExist("credit_cards", "name_on_card") && + db_.DoesColumnExist("credit_cards", "type") && + db_.DoesColumnExist("credit_cards", "expiration_month") && + db_.DoesColumnExist("credit_cards", "expiration_year") && + db_.DoesColumnExist("credit_cards", "billing_address") && + db_.DoesColumnExist("credit_cards", "shipping_address")) { + query = "DELETE FROM credit_cards WHERE (" + credit_cards_is_too_big + + ") OR label IN (SELECT label FROM autofill_profiles WHERE " + + autofill_profiles_is_too_big + ")"; + if (!db_.Execute(query.c_str())) { + LOG(WARNING) << "Unable to update web database to version 24."; + NOTREACHED(); + return sql::INIT_FAILURE; + } } query = "DELETE FROM autofill_profiles WHERE " + autofill_profiles_is_too_big; @@ -2242,30 +2118,19 @@ sql::InitStatus WebDatabase::MigrateOldVersionsAsNeeded(){ // FALL THROUGH case 26: { - // Change the credit_cards.billing_address column from a string to an int. - // The stored string is the label of an address, so we have to select the - // unique ID of this address using the label as a foreign key into the - // |autofill_profiles| table. - std::string stmt = - "SELECT credit_cards.unique_id, autofill_profiles.unique_id " - "FROM autofill_profiles, credit_cards " - "WHERE credit_cards.billing_address = autofill_profiles.label"; - sql::Statement s(db_.GetUniqueStatement(stmt.c_str())); - if (!s) { - LOG(WARNING) << "Statement prepare failed"; - NOTREACHED(); - return sql::INIT_FAILURE; - } - - std::map<int, int> cc_billing_map; - while (s.Step()) - cc_billing_map[s.ColumnInt(0)] = s.ColumnInt(1); - - // Windows already stores the IDs as strings in |billing_address|. Try to - // convert those. - if (cc_billing_map.empty()) { + // Only migrate from legacy credit card tables where specific columns + // exist. + if (db_.DoesColumnExist("credit_cards", "unique_id") && + db_.DoesColumnExist("credit_cards", "billing_address") && + db_.DoesColumnExist("autofill_profiles", "unique_id")) { + // Change the credit_cards.billing_address column from a string to an + // int. The stored string is the label of an address, so we have to + // select the unique ID of this address using the label as a foreign + // key into the |autofill_profiles| table. std::string stmt = - "SELECT unique_id,billing_address FROM credit_cards"; + "SELECT credit_cards.unique_id, autofill_profiles.unique_id " + "FROM autofill_profiles, credit_cards " + "WHERE credit_cards.billing_address = autofill_profiles.label"; sql::Statement s(db_.GetUniqueStatement(stmt.c_str())); if (!s) { LOG(WARNING) << "Statement prepare failed"; @@ -2273,74 +2138,91 @@ sql::InitStatus WebDatabase::MigrateOldVersionsAsNeeded(){ return sql::INIT_FAILURE; } - while (s.Step()) { - int id = 0; - if (base::StringToInt(s.ColumnString(1), &id)) - cc_billing_map[s.ColumnInt(0)] = id; - } - } - - if (!db_.Execute("CREATE TABLE credit_cards_temp ( " - "label VARCHAR, " - "unique_id INTEGER PRIMARY KEY, " - "name_on_card VARCHAR, " - "type VARCHAR, " - "card_number VARCHAR, " - "expiration_month INTEGER, " - "expiration_year INTEGER, " - "verification_code VARCHAR, " - "billing_address INTEGER, " - "shipping_address VARCHAR, " - "card_number_encrypted BLOB, " - "verification_code_encrypted BLOB)")) { - LOG(WARNING) << "Unable to update web database to version 27."; - NOTREACHED(); - return sql::INIT_FAILURE; - } - - if (!db_.Execute( - "INSERT INTO credit_cards_temp " - "SELECT label,unique_id,name_on_card,type,card_number," - "expiration_month,expiration_year,verification_code,0," - "shipping_address,card_number_encrypted,verification_code_encrypted " - "FROM credit_cards")) { - LOG(WARNING) << "Unable to update web database to version 27."; - NOTREACHED(); - return sql::INIT_FAILURE; - } + std::map<int, int> cc_billing_map; + while (s.Step()) + cc_billing_map[s.ColumnInt(0)] = s.ColumnInt(1); + + // Windows already stores the IDs as strings in |billing_address|. Try + // to convert those. + if (cc_billing_map.empty()) { + std::string stmt = + "SELECT unique_id,billing_address FROM credit_cards"; + sql::Statement s(db_.GetUniqueStatement(stmt.c_str())); + if (!s) { + LOG(WARNING) << "Statement prepare failed"; + NOTREACHED(); + return sql::INIT_FAILURE; + } - if (!db_.Execute("DROP TABLE credit_cards")) { - LOG(WARNING) << "Unable to update web database to version 27."; - NOTREACHED(); - return sql::INIT_FAILURE; - } + while (s.Step()) { + int id = 0; + if (base::StringToInt(s.ColumnString(1), &id)) + cc_billing_map[s.ColumnInt(0)] = id; + } + } - if (!db_.Execute( - "ALTER TABLE credit_cards_temp RENAME TO credit_cards")) { - LOG(WARNING) << "Unable to update web database to version 27."; - NOTREACHED(); - return sql::INIT_FAILURE; - } + if (!db_.Execute("CREATE TABLE credit_cards_temp ( " + "label VARCHAR, " + "unique_id INTEGER PRIMARY KEY, " + "name_on_card VARCHAR, " + "type VARCHAR, " + "card_number VARCHAR, " + "expiration_month INTEGER, " + "expiration_year INTEGER, " + "verification_code VARCHAR, " + "billing_address INTEGER, " + "shipping_address VARCHAR, " + "card_number_encrypted BLOB, " + "verification_code_encrypted BLOB)")) { + LOG(WARNING) << "Unable to update web database to version 27."; + NOTREACHED(); + return sql::INIT_FAILURE; + } - for (std::map<int, int>::const_iterator iter = cc_billing_map.begin(); - iter != cc_billing_map.end(); ++iter) { - sql::Statement s(db_.GetCachedStatement( - SQL_FROM_HERE, - "UPDATE credit_cards SET billing_address=? WHERE unique_id=?")); - if (!s) { - LOG(WARNING) << "Statement prepare failed"; + if (!db_.Execute( + "INSERT INTO credit_cards_temp " + "SELECT label,unique_id,name_on_card,type,card_number," + "expiration_month,expiration_year,verification_code,0," + "shipping_address,card_number_encrypted,verification_code_encrypted " + "FROM credit_cards")) { + LOG(WARNING) << "Unable to update web database to version 27."; NOTREACHED(); return sql::INIT_FAILURE; } - s.BindInt(0, (*iter).second); - s.BindInt(1, (*iter).first); + if (!db_.Execute("DROP TABLE credit_cards")) { + LOG(WARNING) << "Unable to update web database to version 27."; + NOTREACHED(); + return sql::INIT_FAILURE; + } - if (!s.Run()) { + if (!db_.Execute( + "ALTER TABLE credit_cards_temp RENAME TO credit_cards")) { LOG(WARNING) << "Unable to update web database to version 27."; NOTREACHED(); return sql::INIT_FAILURE; } + + for (std::map<int, int>::const_iterator iter = cc_billing_map.begin(); + iter != cc_billing_map.end(); ++iter) { + sql::Statement s(db_.GetCachedStatement( + SQL_FROM_HERE, + "UPDATE credit_cards SET billing_address=? WHERE unique_id=?")); + if (!s) { + LOG(WARNING) << "Statement prepare failed"; + NOTREACHED(); + return sql::INIT_FAILURE; + } + + s.BindInt(0, (*iter).second); + s.BindInt(1, (*iter).first); + + if (!s.Run()) { + LOG(WARNING) << "Unable to update web database to version 27."; + NOTREACHED(); + return sql::INIT_FAILURE; + } + } } meta_table_.SetVersionNumber(27); @@ -2498,35 +2380,35 @@ sql::InitStatus WebDatabase::MigrateOldVersionsAsNeeded(){ NOTREACHED(); return sql::INIT_FAILURE; } - } - - // Set all the |guid| fields to valid values. - { - sql::Statement s(db_.GetUniqueStatement("SELECT unique_id " - "FROM autofill_profiles")); - if (!s) { - LOG(WARNING) << "Unable to update web database to version 30."; - NOTREACHED(); - return sql::INIT_FAILURE; - } + // Set all the |guid| fields to valid values. + { + sql::Statement s(db_.GetUniqueStatement("SELECT unique_id " + "FROM autofill_profiles")); - while (s.Step()) { - sql::Statement update_s( - db_.GetUniqueStatement("UPDATE autofill_profiles " - "SET guid=? WHERE unique_id=?")); - if (!update_s) { + if (!s) { LOG(WARNING) << "Unable to update web database to version 30."; NOTREACHED(); return sql::INIT_FAILURE; } - update_s.BindString(0, guid::GenerateGUID()); - update_s.BindInt(1, s.ColumnInt(0)); - if (!update_s.Run()) { - LOG(WARNING) << "Unable to update web database to version 30."; - NOTREACHED(); - return sql::INIT_FAILURE; + while (s.Step()) { + sql::Statement update_s( + db_.GetUniqueStatement("UPDATE autofill_profiles " + "SET guid=? WHERE unique_id=?")); + if (!update_s) { + LOG(WARNING) << "Unable to update web database to version 30."; + NOTREACHED(); + return sql::INIT_FAILURE; + } + update_s.BindString(0, guid::GenerateGUID()); + update_s.BindInt(1, s.ColumnInt(0)); + + if (!update_s.Run()) { + LOG(WARNING) << "Unable to update web database to version 30."; + NOTREACHED(); + return sql::INIT_FAILURE; + } } } } @@ -2542,34 +2424,34 @@ sql::InitStatus WebDatabase::MigrateOldVersionsAsNeeded(){ NOTREACHED(); return sql::INIT_FAILURE; } - } - // Set all the |guid| fields to valid values. - { - sql::Statement s(db_.GetUniqueStatement("SELECT unique_id " - "FROM credit_cards")); - if (!s) { - LOG(WARNING) << "Unable to update web database to version 30."; - NOTREACHED(); - return sql::INIT_FAILURE; - } - - while (s.Step()) { - sql::Statement update_s( - db_.GetUniqueStatement("UPDATE credit_cards " - "set guid=? WHERE unique_id=?")); - if (!update_s) { + // Set all the |guid| fields to valid values. + { + sql::Statement s(db_.GetUniqueStatement("SELECT unique_id " + "FROM credit_cards")); + if (!s) { LOG(WARNING) << "Unable to update web database to version 30."; NOTREACHED(); return sql::INIT_FAILURE; } - update_s.BindString(0, guid::GenerateGUID()); - update_s.BindInt(1, s.ColumnInt(0)); - if (!update_s.Run()) { - LOG(WARNING) << "Unable to update web database to version 30."; - NOTREACHED(); - return sql::INIT_FAILURE; + while (s.Step()) { + sql::Statement update_s( + db_.GetUniqueStatement("UPDATE credit_cards " + "set guid=? WHERE unique_id=?")); + if (!update_s) { + LOG(WARNING) << "Unable to update web database to version 30."; + NOTREACHED(); + return sql::INIT_FAILURE; + } + update_s.BindString(0, guid::GenerateGUID()); + update_s.BindInt(1, s.ColumnInt(0)); + + if (!update_s.Run()) { + LOG(WARNING) << "Unable to update web database to version 30."; + NOTREACHED(); + return sql::INIT_FAILURE; + } } } } @@ -2580,6 +2462,99 @@ sql::InitStatus WebDatabase::MigrateOldVersionsAsNeeded(){ // FALL THROUGH + case 31: + if (db_.DoesColumnExist("autofill_profiles", "unique_id")) { + if (!db_.Execute("CREATE TABLE autofill_profiles_temp ( " + "guid VARCHAR PRIMARY KEY, " + "label VARCHAR, " + "first_name VARCHAR, " + "middle_name VARCHAR, " + "last_name VARCHAR, " + "email VARCHAR, " + "company_name VARCHAR, " + "address_line_1 VARCHAR, " + "address_line_2 VARCHAR, " + "city VARCHAR, " + "state VARCHAR, " + "zipcode VARCHAR, " + "country VARCHAR, " + "phone VARCHAR, " + "fax VARCHAR, " + "date_modified INTEGER NOT NULL DEFAULT 0)")) { + LOG(WARNING) << "Unable to update web database to version 32."; + NOTREACHED(); + return sql::INIT_FAILURE; + } + + if (!db_.Execute( + "INSERT INTO autofill_profiles_temp " + "SELECT guid, label, first_name, middle_name, last_name, email, " + "company_name, address_line_1, address_line_2, city, state, zipcode, " + "country, phone, fax, date_modified " + "FROM autofill_profiles")) { + LOG(WARNING) << "Unable to update web database to version 32."; + NOTREACHED(); + return sql::INIT_FAILURE; + } + + if (!db_.Execute("DROP TABLE autofill_profiles")) { + LOG(WARNING) << "Unable to update web database to version 32."; + NOTREACHED(); + return sql::INIT_FAILURE; + } + + if (!db_.Execute( + "ALTER TABLE autofill_profiles_temp RENAME TO autofill_profiles")) { + LOG(WARNING) << "Unable to update web database to version 32."; + NOTREACHED(); + return sql::INIT_FAILURE; + } + } + + if (db_.DoesColumnExist("credit_cards", "unique_id")) { + if (!db_.Execute("CREATE TABLE credit_cards_temp ( " + "guid VARCHAR PRIMARY KEY, " + "label VARCHAR, " + "name_on_card VARCHAR, " + "expiration_month INTEGER, " + "expiration_year INTEGER, " + "card_number_encrypted BLOB, " + "date_modified INTEGER NOT NULL DEFAULT 0)")) { + LOG(WARNING) << "Unable to update web database to version 32."; + NOTREACHED(); + return sql::INIT_FAILURE; + } + + if (!db_.Execute( + "INSERT INTO credit_cards_temp " + "SELECT guid, label, name_on_card, expiration_month, " + "expiration_year, card_number_encrypted, date_modified " + "FROM credit_cards")) { + LOG(WARNING) << "Unable to update web database to version 32."; + NOTREACHED(); + return sql::INIT_FAILURE; + } + + if (!db_.Execute("DROP TABLE credit_cards")) { + LOG(WARNING) << "Unable to update web database to version 32."; + NOTREACHED(); + return sql::INIT_FAILURE; + } + + if (!db_.Execute( + "ALTER TABLE credit_cards_temp RENAME TO credit_cards")) { + LOG(WARNING) << "Unable to update web database to version 32."; + NOTREACHED(); + return sql::INIT_FAILURE; + } + } + + meta_table_.SetVersionNumber(32); + meta_table_.SetCompatibleVersionNumber( + std::min(32, kCompatibleVersionNumber)); + + // FALL THROUGH + // Add successive versions here. Each should set the version number and // compatible version number as appropriate, then fall through to the next // case. diff --git a/chrome/browser/webdata/web_database.h b/chrome/browser/webdata/web_database.h index e7fd295..2d80437 100644 --- a/chrome/browser/webdata/web_database.h +++ b/chrome/browser/webdata/web_database.h @@ -226,21 +226,10 @@ class WebDatabase { // Updates the database values for the specified profile. virtual bool UpdateAutoFillProfile(const AutoFillProfile& profile); - // Removes a row from the autofill_profiles table. |profile_id| is the - // unique ID of the profile to remove. - // DEPRECATED: In favor of |RemoveAutoFillProfile(const std::string& guid)|. - // TODO(dhollowa): Remove unique IDs. http://crbug.com/58813 - virtual bool RemoveAutoFillProfile(int profile_id); - // Removes a row from the autofill_profiles table. |guid| is the identifier // of the profile to remove. virtual bool RemoveAutoFillProfile(const std::string& guid); - // Retrieves profile for unique id |profile_id|, owned by caller. - // DEPRECATED: In favor of |GetAutoFillProfileForGUID(...)|. - // TODO(dhollowa): Remove unique IDs. http://crbug.com/58813 - bool GetAutoFillProfileForID(int profile_id, AutoFillProfile** profile); - // Retrieves a profile with label |label|. The caller owns |profile|. // DEPRECATED: In favor of |GetAutoFillProfileForGUID(...)|. // TODO(dhollowa): Remove labels. http://crbug.com/58813 @@ -260,12 +249,6 @@ class WebDatabase { // Updates the database values for the specified credit card. bool UpdateCreditCard(const CreditCard& credit_card); - // Removes a row from the credit_cards table. |credit_card_id| is the - // unique ID of the credit card to remove. - // DEPRECATED: In favor of |RemoveCreditCard(const std::string& guid)|. - // TODO(dhollowa): Remove unique IDs. http://crbug.com/58813 - bool RemoveCreditCard(int credit_card_id); - // Removes a row from the credit_cards table. |guid| is the identifer of the // credit card to remove. bool RemoveCreditCard(const std::string& guid); @@ -277,11 +260,6 @@ class WebDatabase { bool GetCreditCardForLabel(const string16& label, CreditCard** credit_card); - // Retrieves credit card for a card with unique id |credit_card_id|. - // DEPRECATED: In favor of |GetCreditCardForGUID()|. - // TODO(dhollowa): Remove unique IDs. http://crbug.com/58813 - bool GetCreditCardForID(int credit_card_id, CreditCard** credit_card); - // Retrieves a credit card with guid |guid|. The caller owns // |credit_card_id|. bool GetCreditCardForGUID(const std::string& guid, CreditCard** credit_card); diff --git a/chrome/browser/webdata/web_database_unittest.cc b/chrome/browser/webdata/web_database_unittest.cc index f9df633..a49b7ee 100644 --- a/chrome/browser/webdata/web_database_unittest.cc +++ b/chrome/browser/webdata/web_database_unittest.cc @@ -94,6 +94,115 @@ bool CompareAutofillEntries(const AutofillEntry& a, const AutofillEntry& b) { return timestamps2.size() != 0U; } +void AutoFillProfile31FromStatement(const sql::Statement& s, + AutoFillProfile* profile, + string16* label, + int* unique_id, + int64* date_modified) { + DCHECK(profile); + DCHECK(label); + DCHECK(unique_id); + DCHECK(date_modified); + *label = s.ColumnString16(0); + *unique_id = s.ColumnInt(1); + profile->SetInfo(AutoFillType(NAME_FIRST), s.ColumnString16(2)); + profile->SetInfo(AutoFillType(NAME_MIDDLE), s.ColumnString16(3)); + profile->SetInfo(AutoFillType(NAME_LAST),s.ColumnString16(4)); + profile->SetInfo(AutoFillType(EMAIL_ADDRESS), s.ColumnString16(5)); + profile->SetInfo(AutoFillType(COMPANY_NAME), s.ColumnString16(6)); + profile->SetInfo(AutoFillType(ADDRESS_HOME_LINE1), s.ColumnString16(7)); + profile->SetInfo(AutoFillType(ADDRESS_HOME_LINE2), s.ColumnString16(8)); + profile->SetInfo(AutoFillType(ADDRESS_HOME_CITY), s.ColumnString16(9)); + profile->SetInfo(AutoFillType(ADDRESS_HOME_STATE), s.ColumnString16(10)); + profile->SetInfo(AutoFillType(ADDRESS_HOME_ZIP), s.ColumnString16(11)); + profile->SetInfo(AutoFillType(ADDRESS_HOME_COUNTRY), s.ColumnString16(12)); + profile->SetInfo(AutoFillType(PHONE_HOME_WHOLE_NUMBER), s.ColumnString16(13)); + profile->SetInfo(AutoFillType(PHONE_FAX_WHOLE_NUMBER), s.ColumnString16(14)); + *date_modified = s.ColumnInt64(15); + profile->set_guid(s.ColumnString(16)); + EXPECT_TRUE(guid::IsValidGUID(profile->guid())); +} + +void AutoFillProfile32FromStatement(const sql::Statement& s, + AutoFillProfile* profile, + string16* label, + int64* date_modified) { + DCHECK(profile); + DCHECK(label); + DCHECK(date_modified); + profile->set_guid(s.ColumnString(0)); + EXPECT_TRUE(guid::IsValidGUID(profile->guid())); + *label = s.ColumnString16(1); + profile->SetInfo(AutoFillType(NAME_FIRST), s.ColumnString16(2)); + profile->SetInfo(AutoFillType(NAME_MIDDLE), s.ColumnString16(3)); + profile->SetInfo(AutoFillType(NAME_LAST),s.ColumnString16(4)); + profile->SetInfo(AutoFillType(EMAIL_ADDRESS), s.ColumnString16(5)); + profile->SetInfo(AutoFillType(COMPANY_NAME), s.ColumnString16(6)); + profile->SetInfo(AutoFillType(ADDRESS_HOME_LINE1), s.ColumnString16(7)); + profile->SetInfo(AutoFillType(ADDRESS_HOME_LINE2), s.ColumnString16(8)); + profile->SetInfo(AutoFillType(ADDRESS_HOME_CITY), s.ColumnString16(9)); + profile->SetInfo(AutoFillType(ADDRESS_HOME_STATE), s.ColumnString16(10)); + profile->SetInfo(AutoFillType(ADDRESS_HOME_ZIP), s.ColumnString16(11)); + profile->SetInfo(AutoFillType(ADDRESS_HOME_COUNTRY), s.ColumnString16(12)); + profile->SetInfo(AutoFillType(PHONE_HOME_WHOLE_NUMBER), s.ColumnString16(13)); + profile->SetInfo(AutoFillType(PHONE_FAX_WHOLE_NUMBER), s.ColumnString16(14)); + *date_modified = s.ColumnInt64(15); +} + +void CreditCard31FromStatement(const sql::Statement& s, + CreditCard* credit_card, + string16* label, + int* unique_id, + std::string* encrypted_number, + int64* date_modified) { + DCHECK(credit_card); + DCHECK(label); + DCHECK(unique_id); + DCHECK(encrypted_number); + DCHECK(date_modified); + *label = s.ColumnString16(0); + *unique_id = s.ColumnInt(1); + credit_card->SetInfo(AutoFillType(CREDIT_CARD_NAME), s.ColumnString16(2)); + credit_card->SetInfo(AutoFillType(CREDIT_CARD_TYPE), s.ColumnString16(3)); + credit_card->SetInfo(AutoFillType(CREDIT_CARD_EXP_MONTH), + s.ColumnString16(5)); + credit_card->SetInfo(AutoFillType(CREDIT_CARD_EXP_4_DIGIT_YEAR), + s.ColumnString16(6)); + int encrypted_number_len = s.ColumnByteLength(10); + if (encrypted_number_len) { + encrypted_number->resize(encrypted_number_len); + memcpy(&(*encrypted_number)[0], s.ColumnBlob(10), encrypted_number_len); + } + *date_modified = s.ColumnInt64(12); + credit_card->set_guid(s.ColumnString(13)); + EXPECT_TRUE(guid::IsValidGUID(credit_card->guid())); +} + +void CreditCard32FromStatement(const sql::Statement& s, + CreditCard* credit_card, + string16* label, + std::string* encrypted_number, + int64* date_modified) { + DCHECK(credit_card); + DCHECK(label); + DCHECK(encrypted_number); + DCHECK(date_modified); + credit_card->set_guid(s.ColumnString(0)); + EXPECT_TRUE(guid::IsValidGUID(credit_card->guid())); + *label = s.ColumnString16(1); + credit_card->SetInfo(AutoFillType(CREDIT_CARD_NAME), s.ColumnString16(2)); + credit_card->SetInfo(AutoFillType(CREDIT_CARD_EXP_MONTH), + s.ColumnString16(3)); + credit_card->SetInfo(AutoFillType(CREDIT_CARD_EXP_4_DIGIT_YEAR), + s.ColumnString16(4)); + int encrypted_number_len = s.ColumnByteLength(5); + if (encrypted_number_len) { + encrypted_number->resize(encrypted_number_len); + memcpy(&(*encrypted_number)[0], s.ColumnBlob(5), encrypted_number_len); + } + *date_modified = s.ColumnInt64(6); +} + } // namespace class WebDatabaseTest : public testing::Test { @@ -1258,7 +1367,8 @@ TEST_F(WebDatabaseTest, AutoFillProfile) { ASSERT_EQ(sql::INIT_OK, db.Init(file_)); // Add a 'Home' profile. - AutoFillProfile home_profile(ASCIIToUTF16("Home"), 17); + AutoFillProfile home_profile; + home_profile.set_label(ASCIIToUTF16("Home")); home_profile.SetInfo(AutoFillType(NAME_FIRST), ASCIIToUTF16("John")); home_profile.SetInfo(AutoFillType(NAME_MIDDLE), ASCIIToUTF16("Q.")); home_profile.SetInfo(AutoFillType(NAME_LAST), ASCIIToUTF16("Smith")); @@ -1288,18 +1398,18 @@ TEST_F(WebDatabaseTest, AutoFillProfile) { ASSERT_TRUE(db.GetAutoFillProfileForLabel(ASCIIToUTF16("Home"), &db_profile)); EXPECT_EQ(home_profile, *db_profile); sql::Statement s_home(db.db_.GetUniqueStatement( - "SELECT * FROM autofill_profiles WHERE label='Home'")); + "SELECT date_modified " + "FROM autofill_profiles WHERE label='Home'")); ASSERT_TRUE(s_home); ASSERT_TRUE(s_home.Step()); - EXPECT_GE(s_home.ColumnInt64(15), pre_creation_time.ToTimeT()); - EXPECT_LE(s_home.ColumnInt64(15), post_creation_time.ToTimeT()); + EXPECT_GE(s_home.ColumnInt64(0), pre_creation_time.ToTimeT()); + EXPECT_LE(s_home.ColumnInt64(0), post_creation_time.ToTimeT()); EXPECT_FALSE(s_home.Step()); delete db_profile; // Add a 'Billing' profile. AutoFillProfile billing_profile = home_profile; billing_profile.set_label(ASCIIToUTF16("Billing")); - billing_profile.set_unique_id(13); billing_profile.SetInfo(AutoFillType(ADDRESS_HOME_LINE1), ASCIIToUTF16("5678 Bottom Street")); billing_profile.SetInfo(AutoFillType(ADDRESS_HOME_LINE2), @@ -1315,11 +1425,11 @@ TEST_F(WebDatabaseTest, AutoFillProfile) { &db_profile)); EXPECT_EQ(billing_profile, *db_profile); sql::Statement s_billing(db.db_.GetUniqueStatement( - "SELECT * FROM autofill_profiles WHERE label='Billing'")); + "SELECT date_modified FROM autofill_profiles WHERE label='Billing'")); ASSERT_TRUE(s_billing); ASSERT_TRUE(s_billing.Step()); - EXPECT_GE(s_billing.ColumnInt64(15), pre_creation_time.ToTimeT()); - EXPECT_LE(s_billing.ColumnInt64(15), post_creation_time.ToTimeT()); + EXPECT_GE(s_billing.ColumnInt64(0), pre_creation_time.ToTimeT()); + EXPECT_LE(s_billing.ColumnInt64(0), post_creation_time.ToTimeT()); EXPECT_FALSE(s_billing.Step()); delete db_profile; @@ -1332,25 +1442,24 @@ TEST_F(WebDatabaseTest, AutoFillProfile) { &db_profile)); EXPECT_EQ(billing_profile, *db_profile); sql::Statement s_billing_updated(db.db_.GetUniqueStatement( - "SELECT * FROM autofill_profiles WHERE label='Billing'")); + "SELECT date_modified FROM autofill_profiles WHERE label='Billing'")); ASSERT_TRUE(s_billing_updated); ASSERT_TRUE(s_billing_updated.Step()); - EXPECT_GE(s_billing_updated.ColumnInt64(15), + EXPECT_GE(s_billing_updated.ColumnInt64(0), pre_modification_time.ToTimeT()); - EXPECT_LE(s_billing_updated.ColumnInt64(15), + EXPECT_LE(s_billing_updated.ColumnInt64(0), post_modification_time.ToTimeT()); EXPECT_FALSE(s_billing_updated.Step()); delete db_profile; // Remove the 'Billing' profile. - EXPECT_TRUE(db.RemoveAutoFillProfile(billing_profile.unique_id())); + EXPECT_TRUE(db.RemoveAutoFillProfile(billing_profile.guid())); EXPECT_FALSE(db.GetAutoFillProfileForLabel(ASCIIToUTF16("Billing"), &db_profile)); // Add a 'GUID' profile. AutoFillProfile guid_profile = home_profile; guid_profile.set_label(ASCIIToUTF16("GUID")); - guid_profile.set_unique_id(14); guid_profile.SetInfo(AutoFillType(ADDRESS_HOME_LINE1), ASCIIToUTF16("5678 Top Street")); guid_profile.SetInfo(AutoFillType(ADDRESS_HOME_LINE2), @@ -1383,18 +1492,16 @@ TEST_F(WebDatabaseTest, CreditCard) { ASSERT_EQ(sql::INIT_OK, db.Init(file_)); // Add a 'Work' credit card. - CreditCard work_creditcard(ASCIIToUTF16("Work"), 13); + CreditCard work_creditcard; + work_creditcard.set_label(ASCIIToUTF16("Work")); work_creditcard.SetInfo(AutoFillType(CREDIT_CARD_NAME), ASCIIToUTF16("Jack Torrance")); - work_creditcard.SetInfo(AutoFillType(CREDIT_CARD_TYPE), - ASCIIToUTF16("Visa")); work_creditcard.SetInfo(AutoFillType(CREDIT_CARD_NUMBER), ASCIIToUTF16("1234567890123456")); work_creditcard.SetInfo(AutoFillType(CREDIT_CARD_EXP_MONTH), ASCIIToUTF16("04")); work_creditcard.SetInfo(AutoFillType(CREDIT_CARD_EXP_4_DIGIT_YEAR), ASCIIToUTF16("2013")); - work_creditcard.set_billing_address_id(1); Time pre_creation_time = Time::Now(); EXPECT_TRUE(db.AddCreditCard(work_creditcard)); @@ -1405,27 +1512,27 @@ TEST_F(WebDatabaseTest, CreditCard) { ASSERT_TRUE(db.GetCreditCardForLabel(ASCIIToUTF16("Work"), &db_creditcard)); EXPECT_EQ(work_creditcard, *db_creditcard); sql::Statement s_work(db.db_.GetUniqueStatement( - "SELECT * FROM credit_cards WHERE label='Work'")); + "SELECT guid, label, name_on_card, expiration_month, expiration_year, " + "card_number_encrypted, date_modified " + "FROM credit_cards WHERE label='Work'")); ASSERT_TRUE(s_work); ASSERT_TRUE(s_work.Step()); - EXPECT_GE(s_work.ColumnInt64(12), pre_creation_time.ToTimeT()); - EXPECT_LE(s_work.ColumnInt64(12), post_creation_time.ToTimeT()); + EXPECT_GE(s_work.ColumnInt64(6), pre_creation_time.ToTimeT()); + EXPECT_LE(s_work.ColumnInt64(6), post_creation_time.ToTimeT()); EXPECT_FALSE(s_work.Step()); delete db_creditcard; // Add a 'Target' credit card. - CreditCard target_creditcard(ASCIIToUTF16("Target"), 7); + CreditCard target_creditcard; + target_creditcard.set_label(ASCIIToUTF16("Target")); target_creditcard.SetInfo(AutoFillType(CREDIT_CARD_NAME), ASCIIToUTF16("Jack Torrance")); - target_creditcard.SetInfo(AutoFillType(CREDIT_CARD_TYPE), - ASCIIToUTF16("Mastercard")); target_creditcard.SetInfo(AutoFillType(CREDIT_CARD_NUMBER), ASCIIToUTF16("1111222233334444")); target_creditcard.SetInfo(AutoFillType(CREDIT_CARD_EXP_MONTH), ASCIIToUTF16("06")); target_creditcard.SetInfo(AutoFillType(CREDIT_CARD_EXP_4_DIGIT_YEAR), ASCIIToUTF16("2012")); - target_creditcard.set_billing_address_id(1); pre_creation_time = Time::Now(); EXPECT_TRUE(db.AddCreditCard(target_creditcard)); @@ -1434,11 +1541,13 @@ TEST_F(WebDatabaseTest, CreditCard) { &db_creditcard)); EXPECT_EQ(target_creditcard, *db_creditcard); sql::Statement s_target(db.db_.GetUniqueStatement( - "SELECT * FROM credit_cards WHERE label='Target'")); + "SELECT guid, label, name_on_card, expiration_month, expiration_year, " + "card_number_encrypted, date_modified " + "FROM credit_cards WHERE label='Target'")); ASSERT_TRUE(s_target); ASSERT_TRUE(s_target.Step()); - EXPECT_GE(s_target.ColumnInt64(12), pre_creation_time.ToTimeT()); - EXPECT_LE(s_target.ColumnInt64(12), post_creation_time.ToTimeT()); + EXPECT_GE(s_target.ColumnInt64(6), pre_creation_time.ToTimeT()); + EXPECT_LE(s_target.ColumnInt64(6), post_creation_time.ToTimeT()); EXPECT_FALSE(s_target.Step()); delete db_creditcard; @@ -1451,34 +1560,34 @@ TEST_F(WebDatabaseTest, CreditCard) { ASSERT_TRUE(db.GetCreditCardForLabel(ASCIIToUTF16("Target"), &db_creditcard)); EXPECT_EQ(target_creditcard, *db_creditcard); sql::Statement s_target_updated(db.db_.GetUniqueStatement( - "SELECT * FROM credit_cards WHERE label='Target'")); + "SELECT guid, label, name_on_card, expiration_month, expiration_year, " + "card_number_encrypted, date_modified " + "FROM credit_cards WHERE label='Target'")); ASSERT_TRUE(s_target_updated); ASSERT_TRUE(s_target_updated.Step()); - EXPECT_GE(s_target_updated.ColumnInt64(12), + EXPECT_GE(s_target_updated.ColumnInt64(6), pre_modification_time.ToTimeT()); - EXPECT_LE(s_target_updated.ColumnInt64(12), + EXPECT_LE(s_target_updated.ColumnInt64(6), post_modification_time.ToTimeT()); EXPECT_FALSE(s_target_updated.Step()); delete db_creditcard; // Remove the 'Target' credit card. - EXPECT_TRUE(db.RemoveCreditCard(target_creditcard.unique_id())); + EXPECT_TRUE(db.RemoveCreditCard(target_creditcard.guid())); EXPECT_FALSE(db.GetCreditCardForLabel(ASCIIToUTF16("Target"), &db_creditcard)); // Add a 'GUID' profile. - CreditCard guid_creditcard(ASCIIToUTF16("GUID"), 7); + CreditCard guid_creditcard; + guid_creditcard.set_label(ASCIIToUTF16("GUID")); guid_creditcard.SetInfo(AutoFillType(CREDIT_CARD_NAME), ASCIIToUTF16("Jimmy Jones")); - guid_creditcard.SetInfo(AutoFillType(CREDIT_CARD_TYPE), - ASCIIToUTF16("Amex")); guid_creditcard.SetInfo(AutoFillType(CREDIT_CARD_NUMBER), ASCIIToUTF16("9999222233334444")); guid_creditcard.SetInfo(AutoFillType(CREDIT_CARD_EXP_MONTH), ASCIIToUTF16("07")); guid_creditcard.SetInfo(AutoFillType(CREDIT_CARD_EXP_4_DIGIT_YEAR), ASCIIToUTF16("2013")); - guid_creditcard.set_billing_address_id(1); EXPECT_TRUE(db.AddCreditCard(guid_creditcard)); ASSERT_TRUE(db.GetCreditCardForGUID(guid_creditcard.guid(), @@ -1506,84 +1615,84 @@ TEST_F(WebDatabaseTest, RemoveAutoFillProfilesAndCreditCardsModifiedBetween) { // Populate the autofill_profiles and credit_cards tables. ASSERT_TRUE(db.db_.Execute( - "INSERT INTO \"autofill_profiles\" VALUES('P1',1,'','','','','','','',''," - "'','','','','',11,'00000000-0000-0000-0000-000000000000');" - "INSERT INTO \"autofill_profiles\" VALUES('P2',2,'','','','','','','',''," - "'','','','','',21,'00000000-0000-0000-0000-000000000001');" - "INSERT INTO \"autofill_profiles\" VALUES('P3',3,'','','','','','','',''," - "'','','','','',31,'00000000-0000-0000-0000-000000000002');" - "INSERT INTO \"autofill_profiles\" VALUES('P4',4,'','','','','','','',''," - "'','','','','',41,'00000000-0000-0000-0000-000000000003');" - "INSERT INTO \"autofill_profiles\" VALUES('P5',5,'','','','','','','',''," - "'','','','','',51,'00000000-0000-0000-0000-000000000004');" - "INSERT INTO \"autofill_profiles\" VALUES('P6',6,'','','','','','','',''," - "'','','','','',61,'00000000-0000-0000-0000-000000000005');" - "INSERT INTO \"credit_cards\" VALUES('C10',10,'','','',10,2010,'','',''," - "X'',X'',17,'00000000-0000-0000-0000-000000000006');" - "INSERT INTO \"credit_cards\" VALUES('C20',20,'','','',10,2010,'','',''," - "X'',X'',27,'00000000-0000-0000-0000-000000000007');" - "INSERT INTO \"credit_cards\" VALUES('C30',30,'','','',10,2010,'','',''," - "X'',X'',37,'00000000-0000-0000-0000-000000000008');" - "INSERT INTO \"credit_cards\" VALUES('C40',40,'','','',10,2010,'','',''," - "X'',X'',47,'00000000-0000-0000-0000-000000000009');" - "INSERT INTO \"credit_cards\" VALUES('C50',50,'','','',10,2010,'','',''," - "X'',X'',57,'00000000-0000-0000-0000-000000000010');" - "INSERT INTO \"credit_cards\" VALUES('C60',60,'','','',10,2010,'','',''," - "X'',X'',67,'00000000-0000-0000-0000-000000000011');")); + "INSERT INTO autofill_profiles (guid, date_modified) " + "VALUES('00000000-0000-0000-0000-000000000000', 11);" + "INSERT INTO autofill_profiles (guid, date_modified) " + "VALUES('00000000-0000-0000-0000-000000000001', 21);" + "INSERT INTO autofill_profiles (guid, date_modified) " + "VALUES('00000000-0000-0000-0000-000000000002', 31);" + "INSERT INTO autofill_profiles (guid, date_modified) " + "VALUES('00000000-0000-0000-0000-000000000003', 41);" + "INSERT INTO autofill_profiles (guid, date_modified) " + "VALUES('00000000-0000-0000-0000-000000000004', 51);" + "INSERT INTO autofill_profiles (guid, date_modified) " + "VALUES('00000000-0000-0000-0000-000000000005', 61);" + "INSERT INTO credit_cards (guid, date_modified) " + "VALUES('00000000-0000-0000-0000-000000000006', 17);" + "INSERT INTO credit_cards (guid, date_modified) " + "VALUES('00000000-0000-0000-0000-000000000007', 27);" + "INSERT INTO credit_cards (guid, date_modified) " + "VALUES('00000000-0000-0000-0000-000000000008', 37);" + "INSERT INTO credit_cards (guid, date_modified) " + "VALUES('00000000-0000-0000-0000-000000000009', 47);" + "INSERT INTO credit_cards (guid, date_modified) " + "VALUES('00000000-0000-0000-0000-000000000010', 57);" + "INSERT INTO credit_cards (guid, date_modified) " + "VALUES('00000000-0000-0000-0000-000000000011', 67);")); // Remove all entries modified in the bounded time range [17,41). db.RemoveAutoFillProfilesAndCreditCardsModifiedBetween( base::Time::FromTimeT(17), base::Time::FromTimeT(41)); sql::Statement s_autofill_profiles_bounded(db.db_.GetUniqueStatement( - "SELECT * FROM autofill_profiles")); + "SELECT date_modified FROM autofill_profiles")); ASSERT_TRUE(s_autofill_profiles_bounded); ASSERT_TRUE(s_autofill_profiles_bounded.Step()); - EXPECT_EQ(11, s_autofill_profiles_bounded.ColumnInt64(15)); + EXPECT_EQ(11, s_autofill_profiles_bounded.ColumnInt64(0)); ASSERT_TRUE(s_autofill_profiles_bounded.Step()); - EXPECT_EQ(41, s_autofill_profiles_bounded.ColumnInt64(15)); + EXPECT_EQ(41, s_autofill_profiles_bounded.ColumnInt64(0)); ASSERT_TRUE(s_autofill_profiles_bounded.Step()); - EXPECT_EQ(51, s_autofill_profiles_bounded.ColumnInt64(15)); + EXPECT_EQ(51, s_autofill_profiles_bounded.ColumnInt64(0)); ASSERT_TRUE(s_autofill_profiles_bounded.Step()); - EXPECT_EQ(61, s_autofill_profiles_bounded.ColumnInt64(15)); + EXPECT_EQ(61, s_autofill_profiles_bounded.ColumnInt64(0)); EXPECT_FALSE(s_autofill_profiles_bounded.Step()); sql::Statement s_credit_cards_bounded(db.db_.GetUniqueStatement( - "SELECT * FROM credit_cards")); + "SELECT date_modified FROM credit_cards")); ASSERT_TRUE(s_credit_cards_bounded); ASSERT_TRUE(s_credit_cards_bounded.Step()); - EXPECT_EQ(47, s_credit_cards_bounded.ColumnInt64(12)); + EXPECT_EQ(47, s_credit_cards_bounded.ColumnInt64(0)); ASSERT_TRUE(s_credit_cards_bounded.Step()); - EXPECT_EQ(57, s_credit_cards_bounded.ColumnInt64(12)); + EXPECT_EQ(57, s_credit_cards_bounded.ColumnInt64(0)); ASSERT_TRUE(s_credit_cards_bounded.Step()); - EXPECT_EQ(67, s_credit_cards_bounded.ColumnInt64(12)); + EXPECT_EQ(67, s_credit_cards_bounded.ColumnInt64(0)); EXPECT_FALSE(s_credit_cards_bounded.Step()); // Remove all entries modified on or after time 51 (unbounded range). db.RemoveAutoFillProfilesAndCreditCardsModifiedBetween( base::Time::FromTimeT(51), base::Time()); sql::Statement s_autofill_profiles_unbounded(db.db_.GetUniqueStatement( - "SELECT * FROM autofill_profiles")); + "SELECT date_modified FROM autofill_profiles")); ASSERT_TRUE(s_autofill_profiles_unbounded); ASSERT_TRUE(s_autofill_profiles_unbounded.Step()); - EXPECT_EQ(11, s_autofill_profiles_unbounded.ColumnInt64(15)); + EXPECT_EQ(11, s_autofill_profiles_unbounded.ColumnInt64(0)); ASSERT_TRUE(s_autofill_profiles_unbounded.Step()); - EXPECT_EQ(41, s_autofill_profiles_unbounded.ColumnInt64(15)); + EXPECT_EQ(41, s_autofill_profiles_unbounded.ColumnInt64(0)); EXPECT_FALSE(s_autofill_profiles_unbounded.Step()); sql::Statement s_credit_cards_unbounded(db.db_.GetUniqueStatement( - "SELECT * FROM credit_cards")); + "SELECT date_modified FROM credit_cards")); ASSERT_TRUE(s_credit_cards_unbounded); ASSERT_TRUE(s_credit_cards_unbounded.Step()); - EXPECT_EQ(47, s_credit_cards_unbounded.ColumnInt64(12)); + EXPECT_EQ(47, s_credit_cards_unbounded.ColumnInt64(0)); EXPECT_FALSE(s_credit_cards_unbounded.Step()); // Remove all remaining entries. db.RemoveAutoFillProfilesAndCreditCardsModifiedBetween(base::Time(), base::Time()); sql::Statement s_autofill_profiles_empty(db.db_.GetUniqueStatement( - "SELECT * FROM autofill_profiles")); + "SELECT date_modified FROM autofill_profiles")); ASSERT_TRUE(s_autofill_profiles_empty); EXPECT_FALSE(s_autofill_profiles_empty.Step()); sql::Statement s_credit_cards_empty(db.db_.GetUniqueStatement( - "SELECT * FROM credit_cards")); + "SELECT date_modified FROM credit_cards")); ASSERT_TRUE(s_credit_cards_empty); EXPECT_FALSE(s_credit_cards_empty.Step()); } @@ -1816,7 +1925,7 @@ class WebDatabaseMigrationTest : public testing::Test { DISALLOW_COPY_AND_ASSIGN(WebDatabaseMigrationTest); }; -const int WebDatabaseMigrationTest::kCurrentTestedVersionNumber = 31; +const int WebDatabaseMigrationTest::kCurrentTestedVersionNumber = 32; void WebDatabaseMigrationTest::LoadDatabase(const FilePath& file) { std::string contents; @@ -1934,7 +2043,7 @@ TEST_F(WebDatabaseMigrationTest, MigrateVersion22ToCurrent) { ASSERT_TRUE(connection.Open(GetDatabasePath())); // No |credit_card| table prior to version 23. - ASSERT_FALSE(connection.DoesColumnExist("credit_cards", "unique_id")); + ASSERT_FALSE(connection.DoesColumnExist("credit_cards", "guid")); ASSERT_FALSE( connection.DoesColumnExist("credit_cards", "card_number_encrypted")); } @@ -1956,7 +2065,7 @@ TEST_F(WebDatabaseMigrationTest, MigrateVersion22ToCurrent) { EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection)); // |credit_card| table now exists. - EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "unique_id")); + EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "guid")); EXPECT_TRUE( connection.DoesColumnExist("credit_cards", "card_number_encrypted")); } @@ -2006,7 +2115,8 @@ TEST_F(WebDatabaseMigrationTest, MigrateVersion22CorruptedToCurrent) { // Columns existing and not existing before version 25. - EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "unique_id")); + EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "unique_id")); + EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "guid")); EXPECT_TRUE( connection.DoesColumnExist("credit_cards", "card_number_encrypted")); EXPECT_TRUE(connection.DoesColumnExist("keywords", "id")); @@ -2149,24 +2259,20 @@ TEST_F(WebDatabaseMigrationTest, MigrateVersion26ToCurrentStringLabels) { // Check version. EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection)); - EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "billing_address")); + EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "billing_address")); - // |billing_address| is an integer. Also Verify the credit card data is - // converted. - std::string stmt = "SELECT * FROM credit_cards"; - sql::Statement s(connection.GetUniqueStatement(stmt.c_str())); + // Verify the credit card data is converted. + sql::Statement s(connection.GetUniqueStatement( + "SELECT guid, label, name_on_card, expiration_month, expiration_year, " + "card_number_encrypted, date_modified " + "FROM credit_cards")); ASSERT_TRUE(s.Step()); - EXPECT_EQ(s.ColumnType(8), sql::COLUMN_TYPE_INTEGER); - EXPECT_EQ("label", s.ColumnString(0)); - EXPECT_EQ(2, s.ColumnInt(1)); + EXPECT_EQ("label", s.ColumnString(1)); EXPECT_EQ("Jack", s.ColumnString(2)); - EXPECT_EQ("Visa", s.ColumnString(3)); - EXPECT_EQ("1234", s.ColumnString(4)); - EXPECT_EQ(2, s.ColumnInt(5)); - EXPECT_EQ(2012, s.ColumnInt(6)); - EXPECT_EQ(std::string(), s.ColumnString(7)); - EXPECT_EQ(1, s.ColumnInt(8)); - // The remaining columns are unused or blobs. + EXPECT_EQ(2, s.ColumnInt(3)); + EXPECT_EQ(2012, s.ColumnInt(4)); + // Column 5 is encrypted number blob. + // Column 6 is date_modified. } } @@ -2229,24 +2335,20 @@ TEST_F(WebDatabaseMigrationTest, MigrateVersion26ToCurrentStringIDs) { // |keywords| |logo_id| column should have been added. EXPECT_TRUE(connection.DoesColumnExist("keywords", "id")); EXPECT_TRUE(connection.DoesColumnExist("keywords", "created_by_policy")); - EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "billing_address")); + EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "billing_address")); - // |billing_address| is an integer. Also Verify the credit card data is - // converted. - std::string stmt = "SELECT * FROM credit_cards"; - sql::Statement s(connection.GetUniqueStatement(stmt.c_str())); + // Verify the credit card data is converted. + sql::Statement s(connection.GetUniqueStatement( + "SELECT guid, label, name_on_card, expiration_month, expiration_year, " + "card_number_encrypted, date_modified " + "FROM credit_cards")); ASSERT_TRUE(s.Step()); - EXPECT_EQ(s.ColumnType(8), sql::COLUMN_TYPE_INTEGER); - EXPECT_EQ("label", s.ColumnString(0)); - EXPECT_EQ(2, s.ColumnInt(1)); + EXPECT_EQ("label", s.ColumnString(1)); EXPECT_EQ("Jack", s.ColumnString(2)); - EXPECT_EQ("Visa", s.ColumnString(3)); - EXPECT_EQ("1234", s.ColumnString(4)); - EXPECT_EQ(2, s.ColumnInt(5)); - EXPECT_EQ(2012, s.ColumnInt(6)); - EXPECT_EQ(std::string(), s.ColumnString(7)); - EXPECT_EQ(1, s.ColumnInt(8)); - // The remaining columns are unused or blobs. + EXPECT_EQ(2, s.ColumnInt(3)); + EXPECT_EQ(2012, s.ColumnInt(4)); + // Column 5 is encrypted credit card number blob. + // Column 6 is date_modified. } } @@ -2333,23 +2435,23 @@ TEST_F(WebDatabaseMigrationTest, MigrateVersion29ToCurrent) { "date_modified")); sql::Statement s_profiles(connection.GetUniqueStatement( - "SELECT * FROM autofill_profiles ")); + "SELECT date_modified FROM autofill_profiles ")); ASSERT_TRUE(s_profiles); while (s_profiles.Step()) { - EXPECT_GE(s_profiles.ColumnInt64(15), + EXPECT_GE(s_profiles.ColumnInt64(0), pre_creation_time.ToTimeT()); - EXPECT_LE(s_profiles.ColumnInt64(15), + EXPECT_LE(s_profiles.ColumnInt64(0), post_creation_time.ToTimeT()); } EXPECT_TRUE(s_profiles.Succeeded()); sql::Statement s_credit_cards(connection.GetUniqueStatement( - "SELECT * FROM credit_cards ")); + "SELECT date_modified FROM credit_cards ")); ASSERT_TRUE(s_credit_cards); while (s_credit_cards.Step()) { - EXPECT_GE(s_credit_cards.ColumnInt64(12), + EXPECT_GE(s_credit_cards.ColumnInt64(0), pre_creation_time.ToTimeT()); - EXPECT_LE(s_credit_cards.ColumnInt64(12), + EXPECT_LE(s_credit_cards.ColumnInt64(0), post_creation_time.ToTimeT()); } EXPECT_TRUE(s_credit_cards.Succeeded()); @@ -2407,3 +2509,142 @@ TEST_F(WebDatabaseMigrationTest, MigrateVersion30ToCurrent) { EXPECT_NE(guid1, guid2); } } + +// Removes unique IDs and make GUIDs the primary key. Also removes unused +// columns. +TEST_F(WebDatabaseMigrationTest, MigrateVersion31ToCurrent) { + // Initialize the database. + ASSERT_NO_FATAL_FAILURE( + LoadDatabase(FilePath(FILE_PATH_LITERAL("version_31.sql")))); + + // Verify pre-conditions. These are expectations for version 30 of the + // database. + AutoFillProfile profile; + string16 profile_label; + int profile_unique_id = 0; + int64 profile_date_modified = 0; + CreditCard credit_card; + string16 cc_label; + int cc_unique_id = 0; + std::string cc_number_encrypted; + int64 cc_date_modified = 0; + { + sql::Connection connection; + ASSERT_TRUE(connection.Open(GetDatabasePath())); + + // Verify existence of columns we'll be changing. + EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "guid")); + EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "unique_id")); + EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "guid")); + EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "unique_id")); + EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "type")); + EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "card_number")); + EXPECT_TRUE(connection.DoesColumnExist("credit_cards", + "verification_code")); + EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "billing_address")); + EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "shipping_address")); + EXPECT_TRUE(connection.DoesColumnExist("credit_cards", + "verification_code_encrypted")); + + // Fetch data in the database prior to migration. + sql::Statement s1( + connection.GetUniqueStatement( + "SELECT label, unique_id, first_name, middle_name, last_name, " + "email, company_name, address_line_1, address_line_2, city, state, " + "zipcode, country, phone, fax, date_modified, guid " + "FROM autofill_profiles")); + ASSERT_TRUE(s1.Step()); + EXPECT_NO_FATAL_FAILURE(AutoFillProfile31FromStatement( + s1, &profile, &profile_label, &profile_unique_id, + &profile_date_modified)); + + sql::Statement s2( + connection.GetUniqueStatement( + "SELECT label, unique_id, name_on_card, type, card_number, " + "expiration_month, expiration_year, verification_code, " + "billing_address, shipping_address, card_number_encrypted, " + "verification_code_encrypted, date_modified, guid " + "FROM credit_cards")); + ASSERT_TRUE(s2.Step()); + EXPECT_NO_FATAL_FAILURE(CreditCard31FromStatement(s2, + &credit_card, + &cc_label, + &cc_unique_id, + &cc_number_encrypted, + &cc_date_modified)); + + EXPECT_NE(profile_unique_id, cc_unique_id); + 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())); + } + + // Verify post-conditions. These are expectations for current version of the + // database. + { + sql::Connection connection; + ASSERT_TRUE(connection.Open(GetDatabasePath())); + + // Check version. + EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection)); + + // Verify existence of columns we'll be changing. + EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "guid")); + EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "unique_id")); + EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "guid")); + EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "unique_id")); + EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "type")); + EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "card_number")); + EXPECT_FALSE(connection.DoesColumnExist("credit_cards", + "verification_code")); + EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "billing_address")); + EXPECT_FALSE(connection.DoesColumnExist("credit_cards", + "shipping_address")); + EXPECT_FALSE(connection.DoesColumnExist("credit_cards", + "verification_code_encrypted")); + + // Verify data in the database after the migration. + sql::Statement s1( + connection.GetUniqueStatement( + "SELECT guid, label, first_name, middle_name, last_name, " + "email, company_name, address_line_1, address_line_2, city, state, " + "zipcode, country, phone, fax, date_modified " + "FROM autofill_profiles")); + ASSERT_TRUE(s1.Step()); + + AutoFillProfile profile_a; + string16 profile_label_a; + int64 profile_date_modified_a = 0; + EXPECT_NO_FATAL_FAILURE(AutoFillProfile32FromStatement( + s1, &profile_a, &profile_label_a, &profile_date_modified_a)); + EXPECT_EQ(profile, profile_a); + EXPECT_EQ(profile_label, profile_label_a); + EXPECT_EQ(profile_date_modified, profile_date_modified_a); + + sql::Statement s2( + connection.GetUniqueStatement( + "SELECT guid, label, name_on_card, expiration_month, " + "expiration_year, card_number_encrypted, date_modified " + "FROM credit_cards")); + ASSERT_TRUE(s2.Step()); + + CreditCard credit_card_a; + string16 cc_label_a; + std::string cc_number_encrypted_a; + int64 cc_date_modified_a = 0; + EXPECT_NO_FATAL_FAILURE(CreditCard32FromStatement(s2, + &credit_card_a, + &cc_label_a, + &cc_number_encrypted_a, + &cc_date_modified_a)); + EXPECT_EQ(credit_card, credit_card_a); + EXPECT_EQ(cc_label, cc_label_a); + EXPECT_EQ(cc_number_encrypted, cc_number_encrypted_a); + EXPECT_EQ(cc_date_modified, cc_date_modified_a); + } +} |
