OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 #ifndef CHROME_BROWSER_WEBDATA_AUTOCOMPLETE_SYNCABLE_SERVICE_H_ | 4 #ifndef CHROME_BROWSER_WEBDATA_AUTOCOMPLETE_SYNCABLE_SERVICE_H_ |
5 #define CHROME_BROWSER_WEBDATA_AUTOCOMPLETE_SYNCABLE_SERVICE_H_ | 5 #define CHROME_BROWSER_WEBDATA_AUTOCOMPLETE_SYNCABLE_SERVICE_H_ |
6 #pragma once | 6 #pragma once |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
11 #include <utility> | 11 #include <utility> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
16 #include "base/threading/non_thread_safe.h" | 16 #include "base/threading/non_thread_safe.h" |
17 #include "chrome/browser/webdata/autofill_change.h" | 17 #include "chrome/browser/webdata/autofill_change.h" |
18 #include "chrome/browser/webdata/autofill_entry.h" | 18 #include "chrome/browser/webdata/autofill_entry.h" |
19 #include "chrome/browser/webdata/web_data_service.h" | 19 #include "chrome/browser/webdata/web_data_service.h" |
20 #include "content/public/browser/notification_observer.h" | 20 #include "content/public/browser/notification_observer.h" |
21 #include "content/public/browser/notification_registrar.h" | 21 #include "content/public/browser/notification_registrar.h" |
22 #include "sync/api/sync_change.h" | 22 #include "sync/api/sync_change.h" |
23 #include "sync/api/sync_data.h" | 23 #include "sync/api/sync_data.h" |
24 #include "sync/api/sync_error.h" | 24 #include "sync/api/sync_error.h" |
25 #include "sync/api/syncable_service.h" | 25 #include "sync/api/syncable_service.h" |
26 | 26 |
27 class ProfileSyncServiceAutofillTest; | 27 class ProfileSyncServiceAutofillTest; |
| 28 |
| 29 namespace csync { |
28 class SyncErrorFactory; | 30 class SyncErrorFactory; |
| 31 } |
29 | 32 |
30 namespace sync_pb { | 33 namespace sync_pb { |
31 class AutofillSpecifics; | 34 class AutofillSpecifics; |
32 } | 35 } |
33 | 36 |
34 | |
35 // The sync implementation for autocomplete. | 37 // The sync implementation for autocomplete. |
36 // MergeDataAndStartSyncing() called first, it does cloud->local and | 38 // MergeDataAndStartSyncing() called first, it does cloud->local and |
37 // local->cloud syncs. Then for each cloud change we receive | 39 // local->cloud syncs. Then for each cloud change we receive |
38 // ProcessSyncChanges() and for each local change Observe() is called. | 40 // ProcessSyncChanges() and for each local change Observe() is called. |
39 // TODO(georgey) : remove reliance on the notifications and make it to be called | 41 // TODO(georgey) : remove reliance on the notifications and make it to be called |
40 // from web_data_service directly. | 42 // from web_data_service directly. |
41 class AutocompleteSyncableService | 43 class AutocompleteSyncableService |
42 : public SyncableService, | 44 : public csync::SyncableService, |
43 public content::NotificationObserver, | 45 public content::NotificationObserver, |
44 public base::NonThreadSafe { | 46 public base::NonThreadSafe { |
45 public: | 47 public: |
46 explicit AutocompleteSyncableService(WebDataService* web_data_service); | 48 explicit AutocompleteSyncableService(WebDataService* web_data_service); |
47 virtual ~AutocompleteSyncableService(); | 49 virtual ~AutocompleteSyncableService(); |
48 | 50 |
49 static syncable::ModelType model_type() { return syncable::AUTOFILL; } | 51 static syncable::ModelType model_type() { return syncable::AUTOFILL; } |
50 | 52 |
51 // SyncableService implementation. | 53 // csync::SyncableService implementation. |
52 virtual SyncError MergeDataAndStartSyncing( | 54 virtual csync::SyncError MergeDataAndStartSyncing( |
53 syncable::ModelType type, | 55 syncable::ModelType type, |
54 const SyncDataList& initial_sync_data, | 56 const csync::SyncDataList& initial_sync_data, |
55 scoped_ptr<SyncChangeProcessor> sync_processor, | 57 scoped_ptr<csync::SyncChangeProcessor> sync_processor, |
56 scoped_ptr<SyncErrorFactory> error_handler) OVERRIDE; | 58 scoped_ptr<csync::SyncErrorFactory> error_handler) OVERRIDE; |
57 virtual void StopSyncing(syncable::ModelType type) OVERRIDE; | 59 virtual void StopSyncing(syncable::ModelType type) OVERRIDE; |
58 virtual SyncDataList GetAllSyncData(syncable::ModelType type) const OVERRIDE; | 60 virtual csync::SyncDataList GetAllSyncData( |
59 virtual SyncError ProcessSyncChanges( | 61 syncable::ModelType type) const OVERRIDE; |
| 62 virtual csync::SyncError ProcessSyncChanges( |
60 const tracked_objects::Location& from_here, | 63 const tracked_objects::Location& from_here, |
61 const SyncChangeList& change_list) OVERRIDE; | 64 const csync::SyncChangeList& change_list) OVERRIDE; |
62 | 65 |
63 // NotificationObserver implementation. | 66 // NotificationObserver implementation. |
64 virtual void Observe(int type, | 67 virtual void Observe(int type, |
65 const content::NotificationSource& source, | 68 const content::NotificationSource& source, |
66 const content::NotificationDetails& details) OVERRIDE; | 69 const content::NotificationDetails& details) OVERRIDE; |
67 | 70 |
68 protected: | 71 protected: |
69 // Helper to query WebDatabase for the current autocomplete state. | 72 // Helper to query WebDatabase for the current autocomplete state. |
70 // Made virtual for ease of mocking in the unit-test. | 73 // Made virtual for ease of mocking in the unit-test. |
71 virtual bool LoadAutofillData(std::vector<AutofillEntry>* entries) const; | 74 virtual bool LoadAutofillData(std::vector<AutofillEntry>* entries) const; |
(...skipping 12 matching lines...) Expand all Loading... |
84 FRIEND_TEST_ALL_PREFIXES(AutocompleteSyncableServiceTest, GetAllSyncData); | 87 FRIEND_TEST_ALL_PREFIXES(AutocompleteSyncableServiceTest, GetAllSyncData); |
85 FRIEND_TEST_ALL_PREFIXES(AutocompleteSyncableServiceTest, | 88 FRIEND_TEST_ALL_PREFIXES(AutocompleteSyncableServiceTest, |
86 ProcessSyncChanges); | 89 ProcessSyncChanges); |
87 FRIEND_TEST_ALL_PREFIXES(AutocompleteSyncableServiceTest, | 90 FRIEND_TEST_ALL_PREFIXES(AutocompleteSyncableServiceTest, |
88 ActOnChange); | 91 ActOnChange); |
89 | 92 |
90 // This is a helper map used only in Merge/Process* functions. The lifetime | 93 // This is a helper map used only in Merge/Process* functions. The lifetime |
91 // of the iterator is longer than the map object. The bool in the pair is used | 94 // of the iterator is longer than the map object. The bool in the pair is used |
92 // to indicate if the item needs to be added (true) or updated (false). | 95 // to indicate if the item needs to be added (true) or updated (false). |
93 typedef std::map<AutofillKey, | 96 typedef std::map<AutofillKey, |
94 std::pair<SyncChange::SyncChangeType, | 97 std::pair<csync::SyncChange::SyncChangeType, |
95 std::vector<AutofillEntry>::iterator> > | 98 std::vector<AutofillEntry>::iterator> > |
96 AutocompleteEntryMap; | 99 AutocompleteEntryMap; |
97 | 100 |
98 // Creates or updates an autocomplete entry based on |data|. | 101 // Creates or updates an autocomplete entry based on |data|. |
99 // |data| - an entry for sync. | 102 // |data| - an entry for sync. |
100 // |loaded_data| - entries that were loaded from local storage. | 103 // |loaded_data| - entries that were loaded from local storage. |
101 // |new_entries| - entries that came from the sync. | 104 // |new_entries| - entries that came from the sync. |
102 // |ignored_entries| - entries that came from the sync, but too old to be | 105 // |ignored_entries| - entries that came from the sync, but too old to be |
103 // stored and immediately discarded. | 106 // stored and immediately discarded. |
104 void CreateOrUpdateEntry(const SyncData& data, | 107 void CreateOrUpdateEntry(const csync::SyncData& data, |
105 AutocompleteEntryMap* loaded_data, | 108 AutocompleteEntryMap* loaded_data, |
106 std::vector<AutofillEntry>* new_entries); | 109 std::vector<AutofillEntry>* new_entries); |
107 | 110 |
108 // Writes |entry| data into supplied |autofill_specifics|. | 111 // Writes |entry| data into supplied |autofill_specifics|. |
109 static void WriteAutofillEntry(const AutofillEntry& entry, | 112 static void WriteAutofillEntry(const AutofillEntry& entry, |
110 sync_pb::EntitySpecifics* autofill_specifics); | 113 sync_pb::EntitySpecifics* autofill_specifics); |
111 | 114 |
112 // Deletes the database entry corresponding to the |autofill| specifics. | 115 // Deletes the database entry corresponding to the |autofill| specifics. |
113 SyncError AutofillEntryDelete(const sync_pb::AutofillSpecifics& autofill); | 116 csync::SyncError AutofillEntryDelete( |
| 117 const sync_pb::AutofillSpecifics& autofill); |
114 | 118 |
115 SyncData CreateSyncData(const AutofillEntry& entry) const; | 119 csync::SyncData CreateSyncData(const AutofillEntry& entry) const; |
116 | 120 |
117 // Syncs |changes| to the cloud. | 121 // Syncs |changes| to the cloud. |
118 void ActOnChanges(const AutofillChangeList& changes); | 122 void ActOnChanges(const AutofillChangeList& changes); |
119 | 123 |
120 static std::string KeyToTag(const std::string& name, | 124 static std::string KeyToTag(const std::string& name, |
121 const std::string& value); | 125 const std::string& value); |
122 | 126 |
123 // For unit-tests. | 127 // For unit-tests. |
124 AutocompleteSyncableService(); | 128 AutocompleteSyncableService(); |
125 void set_sync_processor(SyncChangeProcessor* sync_processor) { | 129 void set_sync_processor(csync::SyncChangeProcessor* sync_processor) { |
126 sync_processor_.reset(sync_processor); | 130 sync_processor_.reset(sync_processor); |
127 } | 131 } |
128 | 132 |
129 // Lifetime of AutocompleteSyncableService object is shorter than | 133 // Lifetime of AutocompleteSyncableService object is shorter than |
130 // |web_data_service_| passed to it. | 134 // |web_data_service_| passed to it. |
131 WebDataService* web_data_service_; | 135 WebDataService* web_data_service_; |
132 | 136 |
133 content::NotificationRegistrar notification_registrar_; | 137 content::NotificationRegistrar notification_registrar_; |
134 | 138 |
135 // We receive ownership of |sync_processor_| in MergeDataAndStartSyncing() and | 139 // We receive ownership of |sync_processor_| in MergeDataAndStartSyncing() and |
136 // destroy it in StopSyncing(). | 140 // destroy it in StopSyncing(). |
137 scoped_ptr<SyncChangeProcessor> sync_processor_; | 141 scoped_ptr<csync::SyncChangeProcessor> sync_processor_; |
138 | 142 |
139 // We receive ownership of |error_handler_| in MergeDataAndStartSyncing() and | 143 // We receive ownership of |error_handler_| in MergeDataAndStartSyncing() and |
140 // destroy it in StopSyncing(). | 144 // destroy it in StopSyncing(). |
141 scoped_ptr<SyncErrorFactory> error_handler_; | 145 scoped_ptr<csync::SyncErrorFactory> error_handler_; |
142 | 146 |
143 DISALLOW_COPY_AND_ASSIGN(AutocompleteSyncableService); | 147 DISALLOW_COPY_AND_ASSIGN(AutocompleteSyncableService); |
144 }; | 148 }; |
145 | 149 |
146 #endif // CHROME_BROWSER_WEBDATA_AUTOCOMPLETE_SYNCABLE_SERVICE_H_ | 150 #endif // CHROME_BROWSER_WEBDATA_AUTOCOMPLETE_SYNCABLE_SERVICE_H_ |
OLD | NEW |