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