| 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 #pragma once | 6 #pragma once |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 class ProfileSyncServiceAutofillTest; | 31 class ProfileSyncServiceAutofillTest; |
| 32 class WebDataService; | 32 class WebDataService; |
| 33 | 33 |
| 34 extern const char kAutofillProfileTag[]; | 34 extern const char kAutofillProfileTag[]; |
| 35 | 35 |
| 36 // The sync implementation for AutofillProfiles. | 36 // The sync implementation for AutofillProfiles. |
| 37 // MergeDataAndStartSyncing() called first, it does cloud->local and | 37 // MergeDataAndStartSyncing() called first, it does cloud->local and |
| 38 // local->cloud syncs. Then for each cloud change we receive | 38 // local->cloud syncs. Then for each cloud change we receive |
| 39 // ProcessSyncChanges() and for each local change Observe() is called. | 39 // ProcessSyncChanges() and for each local change Observe() is called. |
| 40 class AutofillProfileSyncableService | 40 class AutofillProfileSyncableService |
| 41 : public SyncableService, | 41 : public csync::SyncableService, |
| 42 public content::NotificationObserver, | 42 public content::NotificationObserver, |
| 43 public base::NonThreadSafe { | 43 public base::NonThreadSafe { |
| 44 public: | 44 public: |
| 45 explicit AutofillProfileSyncableService(WebDataService* web_data_service); | 45 explicit AutofillProfileSyncableService(WebDataService* web_data_service); |
| 46 virtual ~AutofillProfileSyncableService(); | 46 virtual ~AutofillProfileSyncableService(); |
| 47 | 47 |
| 48 static syncable::ModelType model_type() { return syncable::AUTOFILL_PROFILE; } | 48 static syncable::ModelType model_type() { return syncable::AUTOFILL_PROFILE; } |
| 49 | 49 |
| 50 // SyncableService implementation. | 50 // csync::SyncableService implementation. |
| 51 virtual SyncError MergeDataAndStartSyncing( | 51 virtual csync::SyncError MergeDataAndStartSyncing( |
| 52 syncable::ModelType type, | 52 syncable::ModelType type, |
| 53 const SyncDataList& initial_sync_data, | 53 const csync::SyncDataList& initial_sync_data, |
| 54 scoped_ptr<SyncChangeProcessor> sync_processor, | 54 scoped_ptr<csync::SyncChangeProcessor> sync_processor, |
| 55 scoped_ptr<SyncErrorFactory> sync_error_factory) OVERRIDE; | 55 scoped_ptr<csync::SyncErrorFactory> sync_error_factory) OVERRIDE; |
| 56 virtual void StopSyncing(syncable::ModelType type) OVERRIDE; | 56 virtual void StopSyncing(syncable::ModelType type) OVERRIDE; |
| 57 virtual SyncDataList GetAllSyncData(syncable::ModelType type) const OVERRIDE; | 57 virtual csync::SyncDataList GetAllSyncData( |
| 58 virtual SyncError ProcessSyncChanges( | 58 syncable::ModelType type) const OVERRIDE; |
| 59 virtual csync::SyncError ProcessSyncChanges( |
| 59 const tracked_objects::Location& from_here, | 60 const tracked_objects::Location& from_here, |
| 60 const SyncChangeList& change_list) OVERRIDE; | 61 const csync::SyncChangeList& change_list) OVERRIDE; |
| 61 | 62 |
| 62 // content::NotificationObserver implementation. | 63 // content::NotificationObserver implementation. |
| 63 virtual void Observe(int type, | 64 virtual void Observe(int type, |
| 64 const content::NotificationSource& source, | 65 const content::NotificationSource& source, |
| 65 const content::NotificationDetails& details) OVERRIDE; | 66 const content::NotificationDetails& details) OVERRIDE; |
| 66 | 67 |
| 67 protected: | 68 protected: |
| 68 // A convenience wrapper of a bunch of state we pass around while | 69 // A convenience wrapper of a bunch of state we pass around while |
| 69 // associating models, and send to the WebDatabase for persistence. | 70 // associating models, and send to the WebDatabase for persistence. |
| 70 // We do this so we hold the write lock for only a small period. | 71 // We do this so we hold the write lock for only a small period. |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 // fast processing of the changes. | 116 // fast processing of the changes. |
| 116 void CreateGUIDToProfileMap(const std::vector<AutofillProfile*>& profiles, | 117 void CreateGUIDToProfileMap(const std::vector<AutofillProfile*>& profiles, |
| 117 GUIDToProfileMap* profile_map); | 118 GUIDToProfileMap* profile_map); |
| 118 | 119 |
| 119 // Creates or updates a profile based on |data|. Looks at the guid of the data | 120 // Creates or updates a profile based on |data|. Looks at the guid of the data |
| 120 // and if a profile with such guid is present in |profile_map| updates it. If | 121 // and if a profile with such guid is present in |profile_map| updates it. If |
| 121 // not, searches through it for similar profiles. If similar profile is | 122 // not, searches through it for similar profiles. If similar profile is |
| 122 // found substitutes it for the new one, otherwise adds a new profile. Returns | 123 // found substitutes it for the new one, otherwise adds a new profile. Returns |
| 123 // iterator pointing to added/updated profile. | 124 // iterator pointing to added/updated profile. |
| 124 GUIDToProfileMap::iterator CreateOrUpdateProfile( | 125 GUIDToProfileMap::iterator CreateOrUpdateProfile( |
| 125 const SyncData& data, GUIDToProfileMap* profile_map, DataBundle* bundle); | 126 const csync::SyncData& data, |
| 127 GUIDToProfileMap* profile_map, |
| 128 DataBundle* bundle); |
| 126 | 129 |
| 127 // Syncs |change| to the cloud. | 130 // Syncs |change| to the cloud. |
| 128 void ActOnChange(const AutofillProfileChange& change); | 131 void ActOnChange(const AutofillProfileChange& change); |
| 129 | 132 |
| 130 // Creates SyncData based on supplied |profile|. | 133 // Creates csync::SyncData based on supplied |profile|. |
| 131 static SyncData CreateData(const AutofillProfile& profile); | 134 static csync::SyncData CreateData(const AutofillProfile& profile); |
| 132 | 135 |
| 133 AutofillTable* GetAutofillTable() const; | 136 AutofillTable* GetAutofillTable() const; |
| 134 | 137 |
| 135 // Helper to compare the local value and cloud value of a field, copy into | 138 // Helper to compare the local value and cloud value of a field, copy into |
| 136 // the local value if they differ, and return whether the change happened. | 139 // the local value if they differ, and return whether the change happened. |
| 137 static bool UpdateField(AutofillFieldType field_type, | 140 static bool UpdateField(AutofillFieldType field_type, |
| 138 const std::string& new_value, | 141 const std::string& new_value, |
| 139 AutofillProfile* autofill_profile); | 142 AutofillProfile* autofill_profile); |
| 140 // The same as |UpdateField|, but for multi-valued fields. | 143 // The same as |UpdateField|, but for multi-valued fields. |
| 141 static bool UpdateMultivaluedField( | 144 static bool UpdateMultivaluedField( |
| 142 AutofillFieldType field_type, | 145 AutofillFieldType field_type, |
| 143 const ::google::protobuf::RepeatedPtrField<std::string>& new_value, | 146 const ::google::protobuf::RepeatedPtrField<std::string>& new_value, |
| 144 AutofillProfile* autofill_profile); | 147 AutofillProfile* autofill_profile); |
| 145 | 148 |
| 146 // Calls merge_into->OverwriteWithOrAddTo() and then checks if the | 149 // Calls merge_into->OverwriteWithOrAddTo() and then checks if the |
| 147 // |merge_into| has extra data. Returns |true| if |merge_into| posseses some | 150 // |merge_into| has extra data. Returns |true| if |merge_into| posseses some |
| 148 // multi-valued field values that are not in |merge_from|, false otherwise. | 151 // multi-valued field values that are not in |merge_from|, false otherwise. |
| 149 static bool MergeProfile(const AutofillProfile& merge_from, | 152 static bool MergeProfile(const AutofillProfile& merge_from, |
| 150 AutofillProfile* merge_into); | 153 AutofillProfile* merge_into); |
| 151 | 154 |
| 152 // For unit-tests. | 155 // For unit-tests. |
| 153 AutofillProfileSyncableService(); | 156 AutofillProfileSyncableService(); |
| 154 void set_sync_processor(SyncChangeProcessor* sync_processor) { | 157 void set_sync_processor(csync::SyncChangeProcessor* sync_processor) { |
| 155 sync_processor_.reset(sync_processor); | 158 sync_processor_.reset(sync_processor); |
| 156 } | 159 } |
| 157 | 160 |
| 158 WebDataService* web_data_service_; // WEAK | 161 WebDataService* web_data_service_; // WEAK |
| 159 content::NotificationRegistrar notification_registrar_; | 162 content::NotificationRegistrar notification_registrar_; |
| 160 | 163 |
| 161 // Cached Autofill profiles. *Warning* deleted profiles are still in the | 164 // Cached Autofill profiles. *Warning* deleted profiles are still in the |
| 162 // vector - use the |profiles_map_| to iterate through actual profiles. | 165 // vector - use the |profiles_map_| to iterate through actual profiles. |
| 163 ScopedVector<AutofillProfile> profiles_; | 166 ScopedVector<AutofillProfile> profiles_; |
| 164 GUIDToProfileMap profiles_map_; | 167 GUIDToProfileMap profiles_map_; |
| 165 | 168 |
| 166 scoped_ptr<SyncChangeProcessor> sync_processor_; | 169 scoped_ptr<csync::SyncChangeProcessor> sync_processor_; |
| 167 | 170 |
| 168 scoped_ptr<SyncErrorFactory> sync_error_factory_; | 171 scoped_ptr<csync::SyncErrorFactory> sync_error_factory_; |
| 169 | 172 |
| 170 DISALLOW_COPY_AND_ASSIGN(AutofillProfileSyncableService); | 173 DISALLOW_COPY_AND_ASSIGN(AutofillProfileSyncableService); |
| 171 }; | 174 }; |
| 172 | 175 |
| 173 // This object is used in unit-tests as well, so it defined here. | 176 // This object is used in unit-tests as well, so it defined here. |
| 174 struct AutofillProfileSyncableService::DataBundle { | 177 struct AutofillProfileSyncableService::DataBundle { |
| 175 DataBundle(); | 178 DataBundle(); |
| 176 ~DataBundle(); | 179 ~DataBundle(); |
| 177 | 180 |
| 178 std::vector<std::string> profiles_to_delete; | 181 std::vector<std::string> profiles_to_delete; |
| 179 std::vector<AutofillProfile*> profiles_to_update; | 182 std::vector<AutofillProfile*> profiles_to_update; |
| 180 std::vector<AutofillProfile*> profiles_to_add; | 183 std::vector<AutofillProfile*> profiles_to_add; |
| 181 | 184 |
| 182 // When we go through sync we find profiles that are similar but unmatched. | 185 // When we go through sync we find profiles that are similar but unmatched. |
| 183 // Merge such profiles. | 186 // Merge such profiles. |
| 184 GUIDToProfileMap candidates_to_merge; | 187 GUIDToProfileMap candidates_to_merge; |
| 185 // Profiles that have multi-valued fields that are not in sync. | 188 // Profiles that have multi-valued fields that are not in sync. |
| 186 std::vector<AutofillProfile*> profiles_to_sync_back; | 189 std::vector<AutofillProfile*> profiles_to_sync_back; |
| 187 }; | 190 }; |
| 188 | 191 |
| 189 #endif // CHROME_BROWSER_WEBDATA_AUTOFILL_PROFILE_SYNCABLE_SERVICE_H_ | 192 #endif // CHROME_BROWSER_WEBDATA_AUTOFILL_PROFILE_SYNCABLE_SERVICE_H_ |
| OLD | NEW |