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