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_AUTOFILL_PROFILE_SYNCABLE_SERVICE_H_ | 4 #ifndef CHROME_BROWSER_WEBDATA_AUTOFILL_PROFILE_SYNCABLE_SERVICE_H_ |
5 #define CHROME_BROWSER_WEBDATA_AUTOFILL_PROFILE_SYNCABLE_SERVICE_H_ | 5 #define CHROME_BROWSER_WEBDATA_AUTOFILL_PROFILE_SYNCABLE_SERVICE_H_ |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 11 matching lines...) Expand all Loading... |
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 #include "sync/protocol/autofill_specifics.pb.h" | 26 #include "sync/protocol/autofill_specifics.pb.h" |
27 | 27 |
28 class ProfileSyncServiceAutofillTest; | 28 class ProfileSyncServiceAutofillTest; |
29 class WebDataServiceBase; | 29 class WebDataServiceBase; |
30 | 30 |
31 namespace autofill { | 31 namespace autofill { |
| 32 |
32 class AutofillProfile; | 33 class AutofillProfile; |
33 class AutofillTable; | 34 class AutofillTable; |
34 class AutofillWebDataService; | 35 class AutofillWebDataService; |
35 class FormGroup; | 36 class FormGroup; |
36 } // namespace autofill | |
37 | 37 |
38 extern const char kAutofillProfileTag[]; | 38 extern const char kAutofillProfileTag[]; |
39 | 39 |
40 // The sync implementation for AutofillProfiles. | 40 // The sync implementation for AutofillProfiles. |
41 // MergeDataAndStartSyncing() called first, it does cloud->local and | 41 // MergeDataAndStartSyncing() called first, it does cloud->local and |
42 // local->cloud syncs. Then for each cloud change we receive | 42 // local->cloud syncs. Then for each cloud change we receive |
43 // ProcessSyncChanges() and for each local change Observe() is called. | 43 // ProcessSyncChanges() and for each local change Observe() is called. |
44 class AutofillProfileSyncableService | 44 class AutofillProfileSyncableService |
45 : public base::SupportsUserData::Data, | 45 : public base::SupportsUserData::Data, |
46 public syncer::SyncableService, | 46 public syncer::SyncableService, |
47 public autofill::AutofillWebDataServiceObserverOnDBThread, | 47 public AutofillWebDataServiceObserverOnDBThread, |
48 public base::NonThreadSafe { | 48 public base::NonThreadSafe { |
49 public: | 49 public: |
50 virtual ~AutofillProfileSyncableService(); | 50 virtual ~AutofillProfileSyncableService(); |
51 | 51 |
52 // Creates a new AutofillProfileSyncableService and hangs it off of | 52 // Creates a new AutofillProfileSyncableService and hangs it off of |
53 // |web_data_service|, which takes ownership. | 53 // |web_data_service|, which takes ownership. |
54 static void CreateForWebDataServiceAndBackend( | 54 static void CreateForWebDataServiceAndBackend( |
55 autofill::AutofillWebDataService* web_data_service, | 55 AutofillWebDataService* web_data_service, |
56 autofill::AutofillWebDataBackend* webdata_backend, | 56 AutofillWebDataBackend* webdata_backend, |
57 const std::string& app_locale); | 57 const std::string& app_locale); |
58 | 58 |
59 // Retrieves the AutofillProfileSyncableService stored on |web_data_service|. | 59 // Retrieves the AutofillProfileSyncableService stored on |web_data_service|. |
60 static AutofillProfileSyncableService* FromWebDataService( | 60 static AutofillProfileSyncableService* FromWebDataService( |
61 autofill::AutofillWebDataService* web_data_service); | 61 AutofillWebDataService* web_data_service); |
62 | 62 |
63 static syncer::ModelType model_type() { return syncer::AUTOFILL_PROFILE; } | 63 static syncer::ModelType model_type() { return syncer::AUTOFILL_PROFILE; } |
64 | 64 |
65 // syncer::SyncableService implementation. | 65 // syncer::SyncableService implementation. |
66 virtual syncer::SyncMergeResult MergeDataAndStartSyncing( | 66 virtual syncer::SyncMergeResult MergeDataAndStartSyncing( |
67 syncer::ModelType type, | 67 syncer::ModelType type, |
68 const syncer::SyncDataList& initial_sync_data, | 68 const syncer::SyncDataList& initial_sync_data, |
69 scoped_ptr<syncer::SyncChangeProcessor> sync_processor, | 69 scoped_ptr<syncer::SyncChangeProcessor> sync_processor, |
70 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) OVERRIDE; | 70 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) OVERRIDE; |
71 virtual void StopSyncing(syncer::ModelType type) OVERRIDE; | 71 virtual void StopSyncing(syncer::ModelType type) OVERRIDE; |
72 virtual syncer::SyncDataList GetAllSyncData( | 72 virtual syncer::SyncDataList GetAllSyncData( |
73 syncer::ModelType type) const OVERRIDE; | 73 syncer::ModelType type) const OVERRIDE; |
74 virtual syncer::SyncError ProcessSyncChanges( | 74 virtual syncer::SyncError ProcessSyncChanges( |
75 const tracked_objects::Location& from_here, | 75 const tracked_objects::Location& from_here, |
76 const syncer::SyncChangeList& change_list) OVERRIDE; | 76 const syncer::SyncChangeList& change_list) OVERRIDE; |
77 | 77 |
78 // AutofillWebDataServiceObserverOnDBThread implementation. | 78 // AutofillWebDataServiceObserverOnDBThread implementation. |
79 virtual void AutofillProfileChanged( | 79 virtual void AutofillProfileChanged( |
80 const autofill::AutofillProfileChange& change) OVERRIDE; | 80 const AutofillProfileChange& change) OVERRIDE; |
81 | 81 |
82 // Provides a StartSyncFlare to the SyncableService. See | 82 // Provides a StartSyncFlare to the SyncableService. See |
83 // sync_start_util for more. | 83 // sync_start_util for more. |
84 void InjectStartSyncFlare( | 84 void InjectStartSyncFlare( |
85 const syncer::SyncableService::StartSyncFlare& flare); | 85 const syncer::SyncableService::StartSyncFlare& flare); |
86 | 86 |
87 protected: | 87 protected: |
88 AutofillProfileSyncableService( | 88 AutofillProfileSyncableService(AutofillWebDataBackend* webdata_backend, |
89 autofill::AutofillWebDataBackend* webdata_backend, | 89 const std::string& app_locale); |
90 const std::string& app_locale); | |
91 | 90 |
92 // A convenience wrapper of a bunch of state we pass around while | 91 // A convenience wrapper of a bunch of state we pass around while |
93 // associating models, and send to the WebDatabase for persistence. | 92 // associating models, and send to the WebDatabase for persistence. |
94 // We do this so we hold the write lock for only a small period. | 93 // We do this so we hold the write lock for only a small period. |
95 // When storing the web db we are out of the write lock. | 94 // When storing the web db we are out of the write lock. |
96 struct DataBundle; | 95 struct DataBundle; |
97 | 96 |
98 // Helper to query WebDatabase for the current autofill state. | 97 // Helper to query WebDatabase for the current autofill state. |
99 // Made virtual for ease of mocking in unit tests. | 98 // Made virtual for ease of mocking in unit tests. |
100 // Caller owns returned |profiles|. | 99 // Caller owns returned |profiles|. |
101 virtual bool LoadAutofillData( | 100 virtual bool LoadAutofillData(std::vector<AutofillProfile*>* profiles); |
102 std::vector<autofill::AutofillProfile*>* profiles); | |
103 | 101 |
104 // Helper to persist any changes that occured during model association to | 102 // Helper to persist any changes that occured during model association to |
105 // the WebDatabase. | 103 // the WebDatabase. |
106 // Made virtual for ease of mocking in unit tests. | 104 // Made virtual for ease of mocking in unit tests. |
107 virtual bool SaveChangesToWebData(const DataBundle& bundle); | 105 virtual bool SaveChangesToWebData(const DataBundle& bundle); |
108 | 106 |
109 // For unit tests. | 107 // For unit tests. |
110 AutofillProfileSyncableService(); | 108 AutofillProfileSyncableService(); |
111 void set_sync_processor(syncer::SyncChangeProcessor* sync_processor) { | 109 void set_sync_processor(syncer::SyncChangeProcessor* sync_processor) { |
112 sync_processor_.reset(sync_processor); | 110 sync_processor_.reset(sync_processor); |
113 } | 111 } |
114 | 112 |
115 // Creates syncer::SyncData based on supplied |profile|. | 113 // Creates syncer::SyncData based on supplied |profile|. |
116 // Exposed for unit tests. | 114 // Exposed for unit tests. |
117 static syncer::SyncData CreateData(const autofill::AutofillProfile& profile); | 115 static syncer::SyncData CreateData(const AutofillProfile& profile); |
118 | 116 |
119 private: | 117 private: |
120 friend class ProfileSyncServiceAutofillTest; | 118 friend class ::ProfileSyncServiceAutofillTest; |
121 FRIEND_TEST_ALL_PREFIXES(AutofillProfileSyncableServiceTest, | 119 FRIEND_TEST_ALL_PREFIXES(AutofillProfileSyncableServiceTest, |
122 UpdateField); | 120 UpdateField); |
123 FRIEND_TEST_ALL_PREFIXES(AutofillProfileSyncableServiceTest, | 121 FRIEND_TEST_ALL_PREFIXES(AutofillProfileSyncableServiceTest, |
124 UpdateMultivaluedField); | 122 UpdateMultivaluedField); |
125 FRIEND_TEST_ALL_PREFIXES(AutofillProfileSyncableServiceTest, | 123 FRIEND_TEST_ALL_PREFIXES(AutofillProfileSyncableServiceTest, |
126 MergeProfile); | 124 MergeProfile); |
127 | 125 |
128 // The map of the guid to profiles owned by the |profiles_| vector. | 126 // The map of the guid to profiles owned by the |profiles_| vector. |
129 typedef std::map<std::string, autofill::AutofillProfile*> GUIDToProfileMap; | 127 typedef std::map<std::string, AutofillProfile*> GUIDToProfileMap; |
130 | 128 |
131 // Helper function that overwrites |profile| with data from proto-buffer | 129 // Helper function that overwrites |profile| with data from proto-buffer |
132 // |specifics|. | 130 // |specifics|. |
133 static bool OverwriteProfileWithServerData( | 131 static bool OverwriteProfileWithServerData( |
134 const sync_pb::AutofillProfileSpecifics& specifics, | 132 const sync_pb::AutofillProfileSpecifics& specifics, |
135 autofill::AutofillProfile* profile, | 133 AutofillProfile* profile, |
136 const std::string& app_locale); | 134 const std::string& app_locale); |
137 | 135 |
138 // Writes |profile| data into supplied |profile_specifics|. | 136 // Writes |profile| data into supplied |profile_specifics|. |
139 static void WriteAutofillProfile(const autofill::AutofillProfile& profile, | 137 static void WriteAutofillProfile(const AutofillProfile& profile, |
140 sync_pb::EntitySpecifics* profile_specifics); | 138 sync_pb::EntitySpecifics* profile_specifics); |
141 | 139 |
142 // Creates |profile_map| from the supplied |profiles| vector. Necessary for | 140 // Creates |profile_map| from the supplied |profiles| vector. Necessary for |
143 // fast processing of the changes. | 141 // fast processing of the changes. |
144 void CreateGUIDToProfileMap( | 142 void CreateGUIDToProfileMap(const std::vector<AutofillProfile*>& profiles, |
145 const std::vector<autofill::AutofillProfile*>& profiles, | 143 GUIDToProfileMap* profile_map); |
146 GUIDToProfileMap* profile_map); | |
147 | 144 |
148 // Creates or updates a profile based on |data|. Looks at the guid of the data | 145 // Creates or updates a profile based on |data|. Looks at the guid of the data |
149 // and if a profile with such guid is present in |profile_map| updates it. If | 146 // and if a profile with such guid is present in |profile_map| updates it. If |
150 // not, searches through it for similar profiles. If similar profile is | 147 // not, searches through it for similar profiles. If similar profile is |
151 // found substitutes it for the new one, otherwise adds a new profile. Returns | 148 // found substitutes it for the new one, otherwise adds a new profile. Returns |
152 // iterator pointing to added/updated profile. | 149 // iterator pointing to added/updated profile. |
153 GUIDToProfileMap::iterator CreateOrUpdateProfile( | 150 GUIDToProfileMap::iterator CreateOrUpdateProfile( |
154 const syncer::SyncData& data, | 151 const syncer::SyncData& data, |
155 GUIDToProfileMap* profile_map, | 152 GUIDToProfileMap* profile_map, |
156 DataBundle* bundle); | 153 DataBundle* bundle); |
157 | 154 |
158 // Syncs |change| to the cloud. | 155 // Syncs |change| to the cloud. |
159 void ActOnChange(const autofill::AutofillProfileChange& change); | 156 void ActOnChange(const AutofillProfileChange& change); |
160 | 157 |
161 autofill::AutofillTable* GetAutofillTable() const; | 158 AutofillTable* GetAutofillTable() const; |
162 | 159 |
163 // Helper to compare the local value and cloud value of a field, copy into | 160 // Helper to compare the local value and cloud value of a field, copy into |
164 // the local value if they differ, and return whether the change happened. | 161 // the local value if they differ, and return whether the change happened. |
165 static bool UpdateField(autofill::ServerFieldType field_type, | 162 static bool UpdateField(ServerFieldType field_type, |
166 const std::string& new_value, | 163 const std::string& new_value, |
167 autofill::AutofillProfile* autofill_profile); | 164 AutofillProfile* autofill_profile); |
168 // The same as |UpdateField|, but for multi-valued fields. | 165 // The same as |UpdateField|, but for multi-valued fields. |
169 static bool UpdateMultivaluedField( | 166 static bool UpdateMultivaluedField( |
170 autofill::ServerFieldType field_type, | 167 ServerFieldType field_type, |
171 const ::google::protobuf::RepeatedPtrField<std::string>& new_value, | 168 const ::google::protobuf::RepeatedPtrField<std::string>& new_value, |
172 autofill::AutofillProfile* autofill_profile); | 169 AutofillProfile* autofill_profile); |
173 | 170 |
174 // Calls merge_into->OverwriteWithOrAddTo() and then checks if the | 171 // Calls merge_into->OverwriteWithOrAddTo() and then checks if the |
175 // |merge_into| has extra data. Returns |true| if |merge_into| posseses some | 172 // |merge_into| has extra data. Returns |true| if |merge_into| posseses some |
176 // multi-valued field values that are not in |merge_from| or if the origins | 173 // multi-valued field values that are not in |merge_from| or if the origins |
177 // of the two profiles differ, false otherwise. | 174 // of the two profiles differ, false otherwise. |
178 // TODO(isherman): Seems like this should return |true| if |merge_into| was | 175 // TODO(isherman): Seems like this should return |true| if |merge_into| was |
179 // modified at all: http://crbug.com/248440 | 176 // modified at all: http://crbug.com/248440 |
180 static bool MergeProfile(const autofill::AutofillProfile& merge_from, | 177 static bool MergeProfile(const AutofillProfile& merge_from, |
181 autofill::AutofillProfile* merge_into, | 178 AutofillProfile* merge_into, |
182 const std::string& app_locale); | 179 const std::string& app_locale); |
183 | 180 |
184 autofill::AutofillWebDataBackend* webdata_backend_; // WEAK | 181 AutofillWebDataBackend* webdata_backend_; // WEAK |
185 std::string app_locale_; | 182 std::string app_locale_; |
186 ScopedObserver<autofill::AutofillWebDataBackend, | 183 ScopedObserver<AutofillWebDataBackend, |
187 AutofillProfileSyncableService> scoped_observer_; | 184 AutofillProfileSyncableService> scoped_observer_; |
188 | 185 |
189 // Cached Autofill profiles. *Warning* deleted profiles are still in the | 186 // Cached Autofill profiles. *Warning* deleted profiles are still in the |
190 // vector - use the |profiles_map_| to iterate through actual profiles. | 187 // vector - use the |profiles_map_| to iterate through actual profiles. |
191 ScopedVector<autofill::AutofillProfile> profiles_; | 188 ScopedVector<AutofillProfile> profiles_; |
192 GUIDToProfileMap profiles_map_; | 189 GUIDToProfileMap profiles_map_; |
193 | 190 |
194 scoped_ptr<syncer::SyncChangeProcessor> sync_processor_; | 191 scoped_ptr<syncer::SyncChangeProcessor> sync_processor_; |
195 | 192 |
196 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory_; | 193 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory_; |
197 | 194 |
198 syncer::SyncableService::StartSyncFlare flare_; | 195 syncer::SyncableService::StartSyncFlare flare_; |
199 | 196 |
200 DISALLOW_COPY_AND_ASSIGN(AutofillProfileSyncableService); | 197 DISALLOW_COPY_AND_ASSIGN(AutofillProfileSyncableService); |
201 }; | 198 }; |
202 | 199 |
203 // This object is used in unit tests as well, so it defined here. | 200 // This object is used in unit tests as well, so it defined here. |
204 struct AutofillProfileSyncableService::DataBundle { | 201 struct AutofillProfileSyncableService::DataBundle { |
205 DataBundle(); | 202 DataBundle(); |
206 ~DataBundle(); | 203 ~DataBundle(); |
207 | 204 |
208 std::vector<std::string> profiles_to_delete; | 205 std::vector<std::string> profiles_to_delete; |
209 std::vector<autofill::AutofillProfile*> profiles_to_update; | 206 std::vector<AutofillProfile*> profiles_to_update; |
210 std::vector<autofill::AutofillProfile*> profiles_to_add; | 207 std::vector<AutofillProfile*> profiles_to_add; |
211 | 208 |
212 // When we go through sync we find profiles that are similar but unmatched. | 209 // When we go through sync we find profiles that are similar but unmatched. |
213 // Merge such profiles. | 210 // Merge such profiles. |
214 GUIDToProfileMap candidates_to_merge; | 211 GUIDToProfileMap candidates_to_merge; |
215 // Profiles that have multi-valued fields that are not in sync. | 212 // Profiles that have multi-valued fields that are not in sync. |
216 std::vector<autofill::AutofillProfile*> profiles_to_sync_back; | 213 std::vector<AutofillProfile*> profiles_to_sync_back; |
217 }; | 214 }; |
218 | 215 |
| 216 } // namespace autofill |
| 217 |
219 #endif // CHROME_BROWSER_WEBDATA_AUTOFILL_PROFILE_SYNCABLE_SERVICE_H_ | 218 #endif // CHROME_BROWSER_WEBDATA_AUTOFILL_PROFILE_SYNCABLE_SERVICE_H_ |
OLD | NEW |