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