Commit b3b5a94d authored by isherman@chromium.org's avatar isherman@chromium.org

Clean up AutocompleteSyncableService code a bit.

BUG=none
TEST=none
R=estade@chromium.org

Review URL: https://codereview.chromium.org/368733003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@284259 0039d316-1c4b-4281-b951-d872f2087c98
parent eb8bf1ab
...@@ -98,6 +98,7 @@ using syncer::syncable::WriteTransaction; ...@@ -98,6 +98,7 @@ using syncer::syncable::WriteTransaction;
using testing::_; using testing::_;
using testing::DoAll; using testing::DoAll;
using testing::ElementsAre; using testing::ElementsAre;
using testing::Not;
using testing::SetArgumentPointee; using testing::SetArgumentPointee;
using testing::Return; using testing::Return;
...@@ -511,6 +512,12 @@ class ProfileSyncServiceAutofillTest ...@@ -511,6 +512,12 @@ class ProfileSyncServiceAutofillTest
profile_->IsOffTheRecord()); profile_->IsOffTheRecord());
web_data_service_->StartSyncableService(); web_data_service_->StartSyncableService();
// When UpdateAutofillEntries() is called with an empty list, the return
// value should be |true|, rather than the default of |false|.
std::vector<AutofillEntry> empty;
EXPECT_CALL(autofill_table_, UpdateAutofillEntries(empty))
.WillRepeatedly(Return(true));
} }
virtual void TearDown() OVERRIDE { virtual void TearDown() OVERRIDE {
...@@ -693,7 +700,10 @@ class ProfileSyncServiceAutofillTest ...@@ -693,7 +700,10 @@ class ProfileSyncServiceAutofillTest
void SetIdleChangeProcessorExpectations() { void SetIdleChangeProcessorExpectations() {
EXPECT_CALL(autofill_table_, RemoveFormElement(_, _)).Times(0); EXPECT_CALL(autofill_table_, RemoveFormElement(_, _)).Times(0);
EXPECT_CALL(autofill_table_, GetAutofillTimestamps(_, _, _, _)).Times(0); EXPECT_CALL(autofill_table_, GetAutofillTimestamps(_, _, _, _)).Times(0);
EXPECT_CALL(autofill_table_, UpdateAutofillEntries(_)).Times(0);
// Only permit UpdateAutofillEntries() to be called with an empty list.
std::vector<AutofillEntry> empty;
EXPECT_CALL(autofill_table_, UpdateAutofillEntries(Not(empty))).Times(0);
} }
static AutofillEntry MakeAutofillEntry(const char* name, static AutofillEntry MakeAutofillEntry(const char* name,
......
...@@ -69,13 +69,13 @@ void* UserDataKey() { ...@@ -69,13 +69,13 @@ void* UserDataKey() {
} // namespace } // namespace
AutocompleteSyncableService::AutocompleteSyncableService( AutocompleteSyncableService::AutocompleteSyncableService(
AutofillWebDataBackend* webdata_backend) AutofillWebDataBackend* web_data_backend)
: webdata_backend_(webdata_backend), : web_data_backend_(web_data_backend),
scoped_observer_(this) { scoped_observer_(this) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
DCHECK(webdata_backend_); DCHECK(web_data_backend_);
scoped_observer_.Add(webdata_backend_); scoped_observer_.Add(web_data_backend_);
} }
AutocompleteSyncableService::~AutocompleteSyncableService() { AutocompleteSyncableService::~AutocompleteSyncableService() {
...@@ -85,9 +85,9 @@ AutocompleteSyncableService::~AutocompleteSyncableService() { ...@@ -85,9 +85,9 @@ AutocompleteSyncableService::~AutocompleteSyncableService() {
// static // static
void AutocompleteSyncableService::CreateForWebDataServiceAndBackend( void AutocompleteSyncableService::CreateForWebDataServiceAndBackend(
AutofillWebDataService* web_data_service, AutofillWebDataService* web_data_service,
AutofillWebDataBackend* webdata_backend) { AutofillWebDataBackend* web_data_backend) {
web_data_service->GetDBUserData()->SetUserData( web_data_service->GetDBUserData()->SetUserData(
UserDataKey(), new AutocompleteSyncableService(webdata_backend)); UserDataKey(), new AutocompleteSyncableService(web_data_backend));
} }
// static // static
...@@ -98,7 +98,7 @@ AutocompleteSyncableService* AutocompleteSyncableService::FromWebDataService( ...@@ -98,7 +98,7 @@ AutocompleteSyncableService* AutocompleteSyncableService::FromWebDataService(
} }
AutocompleteSyncableService::AutocompleteSyncableService() AutocompleteSyncableService::AutocompleteSyncableService()
: webdata_backend_(NULL), : web_data_backend_(NULL),
scoped_observer_(this) { scoped_observer_(this) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
} }
...@@ -153,8 +153,6 @@ syncer::SyncMergeResult AutocompleteSyncableService::MergeDataAndStartSyncing( ...@@ -153,8 +153,6 @@ syncer::SyncMergeResult AutocompleteSyncableService::MergeDataAndStartSyncing(
return merge_result; return merge_result;
} }
webdata_backend_->NotifyOfMultipleAutofillChanges();
syncer::SyncChangeList new_changes; syncer::SyncChangeList new_changes;
for (AutocompleteEntryMap::iterator it = new_db_entries.begin(); for (AutocompleteEntryMap::iterator it = new_db_entries.begin();
it != new_db_entries.end(); ++it) { it != new_db_entries.end(); ++it) {
...@@ -172,7 +170,7 @@ syncer::SyncMergeResult AutocompleteSyncableService::MergeDataAndStartSyncing( ...@@ -172,7 +170,7 @@ syncer::SyncMergeResult AutocompleteSyncableService::MergeDataAndStartSyncing(
// NOTE: This must be called *after* the ProcessSyncChanges call above. // NOTE: This must be called *after* the ProcessSyncChanges call above.
// Otherwise, an item that Sync is not yet aware of might expire, causing a // Otherwise, an item that Sync is not yet aware of might expire, causing a
// Sync error when that item's deletion is propagated to Sync. // Sync error when that item's deletion is propagated to Sync.
webdata_backend_->RemoveExpiredFormElements(); web_data_backend_->RemoveExpiredFormElements();
return merge_result; return merge_result;
} }
...@@ -181,7 +179,7 @@ void AutocompleteSyncableService::StopSyncing(syncer::ModelType type) { ...@@ -181,7 +179,7 @@ void AutocompleteSyncableService::StopSyncing(syncer::ModelType type) {
DCHECK(CalledOnValidThread()); DCHECK(CalledOnValidThread());
DCHECK_EQ(syncer::AUTOFILL, type); DCHECK_EQ(syncer::AUTOFILL, type);
sync_processor_.reset(NULL); sync_processor_.reset();
error_handler_.reset(); error_handler_.reset();
} }
...@@ -256,11 +254,11 @@ syncer::SyncError AutocompleteSyncableService::ProcessSyncChanges( ...@@ -256,11 +254,11 @@ syncer::SyncError AutocompleteSyncableService::ProcessSyncChanges(
if (autofill.has_value()) if (autofill.has_value())
list_processing_error = AutofillEntryDelete(autofill); list_processing_error = AutofillEntryDelete(autofill);
else else
VLOG(1) << "Delete for old-style autofill profile being dropped!"; DVLOG(1) << "Delete for old-style autofill profile being dropped!";
break; break;
} }
default: case syncer::SyncChange::ACTION_INVALID:
NOTREACHED(); NOTREACHED();
return error_handler_->CreateAndUploadError( return error_handler_->CreateAndUploadError(
FROM_HERE, FROM_HERE,
...@@ -275,11 +273,9 @@ syncer::SyncError AutocompleteSyncableService::ProcessSyncChanges( ...@@ -275,11 +273,9 @@ syncer::SyncError AutocompleteSyncableService::ProcessSyncChanges(
"Failed to update webdata."); "Failed to update webdata.");
} }
webdata_backend_->NotifyOfMultipleAutofillChanges();
// This will schedule a deletion operation on the DB thread, which will // This will schedule a deletion operation on the DB thread, which will
// trigger a notification to propagate the deletion to Sync. // trigger a notification to propagate the deletion to Sync.
webdata_backend_->RemoveExpiredFormElements(); web_data_backend_->RemoveExpiredFormElements();
return list_processing_error; return list_processing_error;
} }
...@@ -287,9 +283,10 @@ syncer::SyncError AutocompleteSyncableService::ProcessSyncChanges( ...@@ -287,9 +283,10 @@ syncer::SyncError AutocompleteSyncableService::ProcessSyncChanges(
void AutocompleteSyncableService::AutofillEntriesChanged( void AutocompleteSyncableService::AutofillEntriesChanged(
const AutofillChangeList& changes) { const AutofillChangeList& changes) {
// Check if sync is on. If we receive this notification prior to sync being // Check if sync is on. If we receive this notification prior to sync being
// started, we'll notify sync to start as soon as it can and later process // started, we'll notify sync to start as soon as it can and later process all
// all entries when MergeData..() is called. If we receive this notification // entries when MergeDataAndStartSyncing() is called. If we receive this
// sync has exited, it will be synced next time Chrome starts. // notification after sync has exited, it will be synced the next time Chrome
// starts.
if (sync_processor_) { if (sync_processor_) {
ActOnChanges(changes); ActOnChanges(changes);
} else if (!flare_.is_null()) { } else if (!flare_.is_null()) {
...@@ -300,20 +297,16 @@ void AutocompleteSyncableService::AutofillEntriesChanged( ...@@ -300,20 +297,16 @@ void AutocompleteSyncableService::AutofillEntriesChanged(
bool AutocompleteSyncableService::LoadAutofillData( bool AutocompleteSyncableService::LoadAutofillData(
std::vector<AutofillEntry>* entries) const { std::vector<AutofillEntry>* entries) const {
return AutofillTable::FromWebDatabase( return GetAutofillTable()->GetAllAutofillEntries(entries);
webdata_backend_->GetDatabase())->GetAllAutofillEntries(entries);
} }
bool AutocompleteSyncableService::SaveChangesToWebData( bool AutocompleteSyncableService::SaveChangesToWebData(
const std::vector<AutofillEntry>& new_entries) { const std::vector<AutofillEntry>& new_entries) {
DCHECK(CalledOnValidThread()); DCHECK(CalledOnValidThread());
if (!GetAutofillTable()->UpdateAutofillEntries(new_entries))
if (!new_entries.empty() &&
!AutofillTable::FromWebDatabase(
webdata_backend_->GetDatabase())->UpdateAutofillEntries(
new_entries)) {
return false; return false;
}
web_data_backend_->NotifyOfMultipleAutofillChanges();
return true; return true;
} }
...@@ -326,7 +319,7 @@ void AutocompleteSyncableService::CreateOrUpdateEntry( ...@@ -326,7 +319,7 @@ void AutocompleteSyncableService::CreateOrUpdateEntry(
const sync_pb::AutofillSpecifics& autofill_specifics(specifics.autofill()); const sync_pb::AutofillSpecifics& autofill_specifics(specifics.autofill());
if (!autofill_specifics.has_value()) { if (!autofill_specifics.has_value()) {
VLOG(1) << "Add/Update for old-style autofill profile being dropped!"; DVLOG(1) << "Add/Update for old-style autofill profile being dropped!";
return; return;
} }
...@@ -375,10 +368,9 @@ void AutocompleteSyncableService::WriteAutofillEntry( ...@@ -375,10 +368,9 @@ void AutocompleteSyncableService::WriteAutofillEntry(
syncer::SyncError AutocompleteSyncableService::AutofillEntryDelete( syncer::SyncError AutocompleteSyncableService::AutofillEntryDelete(
const sync_pb::AutofillSpecifics& autofill) { const sync_pb::AutofillSpecifics& autofill) {
if (!AutofillTable::FromWebDatabase( if (!GetAutofillTable()->RemoveFormElement(
webdata_backend_->GetDatabase())->RemoveFormElement( base::UTF8ToUTF16(autofill.name()),
base::UTF8ToUTF16(autofill.name()), base::UTF8ToUTF16(autofill.value()))) {
base::UTF8ToUTF16(autofill.value()))) {
return error_handler_->CreateAndUploadError( return error_handler_->CreateAndUploadError(
FROM_HERE, FROM_HERE,
"Could not remove autocomplete entry from WebDatabase."); "Could not remove autocomplete entry from WebDatabase.");
...@@ -396,13 +388,10 @@ void AutocompleteSyncableService::ActOnChanges( ...@@ -396,13 +388,10 @@ void AutocompleteSyncableService::ActOnChanges(
case AutofillChange::ADD: case AutofillChange::ADD:
case AutofillChange::UPDATE: { case AutofillChange::UPDATE: {
base::Time date_created, date_last_used; base::Time date_created, date_last_used;
WebDatabase* db = webdata_backend_->GetDatabase(); bool success = GetAutofillTable()->GetAutofillTimestamps(
if (!AutofillTable::FromWebDatabase(db)->GetAutofillTimestamps( change->key().name(), change->key().value(),
change->key().name(), change->key().value(), &date_created, &date_last_used);
&date_created, &date_last_used)) { DCHECK(success);
NOTREACHED();
return;
}
AutofillEntry entry(change->key(), date_created, date_last_used); AutofillEntry entry(change->key(), date_created, date_last_used);
syncer::SyncChange::SyncChangeType change_type = syncer::SyncChange::SyncChangeType change_type =
(change->type() == AutofillChange::ADD) ? (change->type() == AutofillChange::ADD) ?
...@@ -422,16 +411,13 @@ void AutocompleteSyncableService::ActOnChanges( ...@@ -422,16 +411,13 @@ void AutocompleteSyncableService::ActOnChanges(
CreateSyncData(entry))); CreateSyncData(entry)));
break; break;
} }
default:
NOTREACHED();
} }
} }
syncer::SyncError error = syncer::SyncError error =
sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes); sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes);
if (error.IsSet()) { if (error.IsSet()) {
VLOG(1) << "[AUTOCOMPLETE SYNC] Failed processing change. Error: " DVLOG(1) << "[AUTOCOMPLETE SYNC] Failed processing change. Error: "
<< error.message(); << error.message();
} }
} }
...@@ -444,6 +430,10 @@ syncer::SyncData AutocompleteSyncableService::CreateSyncData( ...@@ -444,6 +430,10 @@ syncer::SyncData AutocompleteSyncableService::CreateSyncData(
return syncer::SyncData::CreateLocalData(tag, tag, autofill_specifics); return syncer::SyncData::CreateLocalData(tag, tag, autofill_specifics);
} }
AutofillTable* AutocompleteSyncableService::GetAutofillTable() const {
return AutofillTable::FromWebDatabase(web_data_backend_->GetDatabase());
}
// static // static
std::string AutocompleteSyncableService::KeyToTag(const std::string& name, std::string AutocompleteSyncableService::KeyToTag(const std::string& name,
const std::string& value) { const std::string& value) {
......
...@@ -28,6 +28,10 @@ ...@@ -28,6 +28,10 @@
class ProfileSyncServiceAutofillTest; class ProfileSyncServiceAutofillTest;
namespace autofill {
class AutofillTable;
}
namespace syncer { namespace syncer {
class SyncErrorFactory; class SyncErrorFactory;
} }
...@@ -52,15 +56,15 @@ class AutocompleteSyncableService ...@@ -52,15 +56,15 @@ class AutocompleteSyncableService
// |web_data_service|, which takes ownership. // |web_data_service|, which takes ownership.
static void CreateForWebDataServiceAndBackend( static void CreateForWebDataServiceAndBackend(
autofill::AutofillWebDataService* web_data_service, autofill::AutofillWebDataService* web_data_service,
autofill::AutofillWebDataBackend* webdata_backend); autofill::AutofillWebDataBackend* web_data_backend);
// Retrieves the AutocompleteSyncableService stored on |web_data|. // Retrieves the AutocompleteSyncableService stored on |web_data_service|.
static AutocompleteSyncableService* FromWebDataService( static AutocompleteSyncableService* FromWebDataService(
autofill::AutofillWebDataService* web_data_service); autofill::AutofillWebDataService* web_data_service);
static syncer::ModelType model_type() { return syncer::AUTOFILL; } static syncer::ModelType model_type() { return syncer::AUTOFILL; }
// syncer::SyncableService implementation. // syncer::SyncableService:
virtual syncer::SyncMergeResult MergeDataAndStartSyncing( virtual syncer::SyncMergeResult MergeDataAndStartSyncing(
syncer::ModelType type, syncer::ModelType type,
const syncer::SyncDataList& initial_sync_data, const syncer::SyncDataList& initial_sync_data,
...@@ -73,18 +77,18 @@ class AutocompleteSyncableService ...@@ -73,18 +77,18 @@ class AutocompleteSyncableService
const tracked_objects::Location& from_here, const tracked_objects::Location& from_here,
const syncer::SyncChangeList& change_list) OVERRIDE; const syncer::SyncChangeList& change_list) OVERRIDE;
// AutofillWebDataServiceObserverOnDBThread implementation. // AutofillWebDataServiceObserverOnDBThread:
virtual void AutofillEntriesChanged( virtual void AutofillEntriesChanged(
const autofill::AutofillChangeList& changes) OVERRIDE; const autofill::AutofillChangeList& changes) OVERRIDE;
// Provides a StartSyncFlare to the SyncableService. See // Provides a StartSyncFlare to the SyncableService. See sync_start_util for
// sync_start_util for more. // more.
void InjectStartSyncFlare( void InjectStartSyncFlare(
const syncer::SyncableService::StartSyncFlare& flare); const syncer::SyncableService::StartSyncFlare& flare);
protected: protected:
explicit AutocompleteSyncableService( explicit AutocompleteSyncableService(
autofill::AutofillWebDataBackend* webdata_backend); autofill::AutofillWebDataBackend* web_data_backend);
// Helper to query WebDatabase for the current autocomplete state. // Helper to query WebDatabase for the current autocomplete state.
// Made virtual for ease of mocking in the unit-test. // Made virtual for ease of mocking in the unit-test.
...@@ -118,7 +122,7 @@ class AutocompleteSyncableService ...@@ -118,7 +122,7 @@ class AutocompleteSyncableService
AutocompleteEntryMap; AutocompleteEntryMap;
// Creates or updates an autocomplete entry based on |data|. // Creates or updates an autocomplete entry based on |data|.
// |data| - an entry for sync. // |data| - an entry for sync.
// |loaded_data| - entries that were loaded from local storage. // |loaded_data| - entries that were loaded from local storage.
// |new_entries| - entries that came from the sync. // |new_entries| - entries that came from the sync.
// |ignored_entries| - entries that came from the sync, but too old to be // |ignored_entries| - entries that came from the sync, but too old to be
...@@ -140,6 +144,9 @@ class AutocompleteSyncableService ...@@ -140,6 +144,9 @@ class AutocompleteSyncableService
// Syncs |changes| to the cloud. // Syncs |changes| to the cloud.
void ActOnChanges(const autofill::AutofillChangeList& changes); void ActOnChanges(const autofill::AutofillChangeList& changes);
// Returns the table associated with the |web_data_backend_|.
autofill::AutofillTable* GetAutofillTable() const;
static std::string KeyToTag(const std::string& name, static std::string KeyToTag(const std::string& name,
const std::string& value); const std::string& value);
...@@ -149,9 +156,8 @@ class AutocompleteSyncableService ...@@ -149,9 +156,8 @@ class AutocompleteSyncableService
sync_processor_.reset(sync_processor); sync_processor_.reset(sync_processor);
} }
// Lifetime of AutocompleteSyncableService object is shorter than // The |web_data_backend_| is expected to outlive |this|.
// |autofill_webdata_backend_| passed to it. autofill::AutofillWebDataBackend* const web_data_backend_;
autofill::AutofillWebDataBackend* const webdata_backend_;
ScopedObserver<autofill::AutofillWebDataBackend, AutocompleteSyncableService> ScopedObserver<autofill::AutofillWebDataBackend, AutocompleteSyncableService>
scoped_observer_; scoped_observer_;
......
...@@ -19,11 +19,11 @@ class CreditCard; ...@@ -19,11 +19,11 @@ class CreditCard;
template <typename KeyType> template <typename KeyType>
class GenericAutofillChange { class GenericAutofillChange {
public: public:
typedef enum { enum Type {
ADD, ADD,
UPDATE, UPDATE,
REMOVE REMOVE
} Type; };
virtual ~GenericAutofillChange() {} virtual ~GenericAutofillChange() {}
......
...@@ -780,7 +780,7 @@ bool AutofillTable::GetAutofillTimestamps(const base::string16& name, ...@@ -780,7 +780,7 @@ bool AutofillTable::GetAutofillTimestamps(const base::string16& name,
bool AutofillTable::UpdateAutofillEntries( bool AutofillTable::UpdateAutofillEntries(
const std::vector<AutofillEntry>& entries) { const std::vector<AutofillEntry>& entries) {
if (!entries.size()) if (entries.empty())
return true; return true;
// Remove all existing entries. // Remove all existing entries.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment