1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
// Copyright (c) 2012 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.
#ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_WEBDATA_AUTOCOMPLETE_SYNCABLE_SERVICE_H_
#define COMPONENTS_AUTOFILL_CORE_BROWSER_WEBDATA_AUTOCOMPLETE_SYNCABLE_SERVICE_H_
#include <map>
#include <memory>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include "base/macros.h"
#include "base/scoped_observer.h"
#include "base/supports_user_data.h"
#include "base/threading/non_thread_safe.h"
#include "components/autofill/core/browser/webdata/autofill_change.h"
#include "components/autofill/core/browser/webdata/autofill_entry.h"
#include "components/autofill/core/browser/webdata/autofill_webdata_backend.h"
#include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
#include "components/autofill/core/browser/webdata/autofill_webdata_service_observer.h"
#include "components/sync/model/sync_change.h"
#include "components/sync/model/sync_data.h"
#include "components/sync/model/sync_error.h"
#include "components/sync/model/syncable_service.h"
namespace browser_sync {
class FakeServerUpdater;
class ProfileSyncServiceAutofillTest;
} // namespace browser_sync
namespace syncer {
class SyncErrorFactory;
} // namespace syncer
namespace sync_pb {
class AutofillSpecifics;
} // namespace sync_pb
namespace autofill {
class AutofillTable;
// The sync implementation for autocomplete.
// MergeDataAndStartSyncing() called first, it does cloud->local and
// local->cloud syncs. Then for each cloud change we receive
// ProcessSyncChanges() and for each local change Observe() is called.
class AutocompleteSyncableService
: public base::SupportsUserData::Data,
public syncer::SyncableService,
public AutofillWebDataServiceObserverOnDBThread,
public base::NonThreadSafe {
public:
~AutocompleteSyncableService() override;
// Creates a new AutocompleteSyncableService and hangs it off of
// |web_data_service|, which takes ownership.
static void CreateForWebDataServiceAndBackend(
AutofillWebDataService* web_data_service,
AutofillWebDataBackend* web_data_backend);
// Retrieves the AutocompleteSyncableService stored on |web_data_service|.
static AutocompleteSyncableService* FromWebDataService(
AutofillWebDataService* web_data_service);
static syncer::ModelType model_type() { return syncer::AUTOFILL; }
// syncer::SyncableService:
syncer::SyncMergeResult MergeDataAndStartSyncing(
syncer::ModelType type,
const syncer::SyncDataList& initial_sync_data,
std::unique_ptr<syncer::SyncChangeProcessor> sync_processor,
std::unique_ptr<syncer::SyncErrorFactory> error_handler) override;
void StopSyncing(syncer::ModelType type) override;
syncer::SyncDataList GetAllSyncData(syncer::ModelType type) const override;
syncer::SyncError ProcessSyncChanges(
const tracked_objects::Location& from_here,
const syncer::SyncChangeList& change_list) override;
// AutofillWebDataServiceObserverOnDBThread:
void AutofillEntriesChanged(const AutofillChangeList& changes) override;
// Provides a StartSyncFlare to the SyncableService. See sync_start_util for
// more.
void InjectStartSyncFlare(
const syncer::SyncableService::StartSyncFlare& flare);
protected:
explicit AutocompleteSyncableService(
AutofillWebDataBackend* web_data_backend);
// Helper to query WebDatabase for the current autocomplete state.
// Made virtual for ease of mocking in the unit-test.
virtual bool LoadAutofillData(std::vector<AutofillEntry>* entries) const;
// Helper to persist any changes that occured during model association to
// the WebDatabase. |entries| will be either added or updated.
// Made virtual for ease of mocking in the unit-test.
virtual bool SaveChangesToWebData(const std::vector<AutofillEntry>& entries);
private:
friend class browser_sync::FakeServerUpdater;
friend class browser_sync::ProfileSyncServiceAutofillTest;
// This is a helper map used only in Merge/Process* functions. The lifetime
// of the iterator is longer than the map object. The bool in the pair is used
// to indicate if the item needs to be added (true) or updated (false).
typedef std::map<AutofillKey,
std::pair<syncer::SyncChange::SyncChangeType,
std::vector<AutofillEntry>::iterator>>
AutocompleteEntryMap;
// Creates or updates an autocomplete entry based on |data|.
// |data| - an entry for sync.
// |loaded_data| - entries that were loaded from local storage.
// |new_entries| - entries that came from the sync.
// |ignored_entries| - entries that came from the sync, but too old to be
// stored and immediately discarded.
void CreateOrUpdateEntry(const syncer::SyncData& data,
AutocompleteEntryMap* loaded_data,
std::vector<AutofillEntry>* new_entries);
// Writes |entry| data into supplied |autofill_specifics|.
static void WriteAutofillEntry(const AutofillEntry& entry,
sync_pb::EntitySpecifics* autofill_specifics);
// Deletes the database entry corresponding to the |autofill| specifics.
syncer::SyncError AutofillEntryDelete(
const sync_pb::AutofillSpecifics& autofill);
syncer::SyncData CreateSyncData(const AutofillEntry& entry) const;
// Syncs |changes| to the cloud.
void ActOnChanges(const AutofillChangeList& changes);
// Returns the table associated with the |web_data_backend_|.
AutofillTable* GetAutofillTable() const;
static std::string KeyToTag(const std::string& name,
const std::string& value);
// For unit-tests.
AutocompleteSyncableService();
void set_sync_processor(syncer::SyncChangeProcessor* sync_processor) {
sync_processor_.reset(sync_processor);
}
// The |web_data_backend_| is expected to outlive |this|.
AutofillWebDataBackend* const web_data_backend_;
ScopedObserver<AutofillWebDataBackend, AutocompleteSyncableService>
scoped_observer_;
// We receive ownership of |sync_processor_| in MergeDataAndStartSyncing() and
// destroy it in StopSyncing().
std::unique_ptr<syncer::SyncChangeProcessor> sync_processor_;
// We receive ownership of |error_handler_| in MergeDataAndStartSyncing() and
// destroy it in StopSyncing().
std::unique_ptr<syncer::SyncErrorFactory> error_handler_;
syncer::SyncableService::StartSyncFlare flare_;
DISALLOW_COPY_AND_ASSIGN(AutocompleteSyncableService);
};
} // namespace autofill
#endif // COMPONENTS_AUTOFILL_CORE_BROWSER_WEBDATA_AUTOCOMPLETE_SYNCABLE_SERVICE_H_
|