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