Chromium Code Reviews
|
| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 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 // The sync implementation for AutofillProfiles. | |
| 5 // MergeDataAndStartSyncing() called first, it does cloud->local and | |
| 6 // local->cloud syncs. Then for each cloud change we receive | |
| 7 // ProcessSyncChanges() and for each local change Observe() is called. | |
|
Ilya Sherman
2011/09/03 00:10:57
nit: This comment should still be right above the
GeorgeY
2011/09/07 00:28:14
Done.
| |
| 8 | |
| 9 #ifndef CHROME_BROWSER_SYNC_GLUE_AUTOFILL_PROFILE_SYNCABLE_SERVICE_H_ | |
| 10 #define CHROME_BROWSER_SYNC_GLUE_AUTOFILL_PROFILE_SYNCABLE_SERVICE_H_ | |
| 11 #pragma once | |
| 12 | |
| 13 #include <map> | |
| 14 #include <set> | |
| 15 #include <string> | |
| 16 #include <vector> | |
| 17 | |
| 18 #include "base/basictypes.h" | |
| 19 #include "base/memory/ref_counted.h" | |
| 20 #include "base/synchronization/lock.h" | |
| 21 #include "base/threading/non_thread_safe.h" | |
| 22 #include "chrome/browser/autofill/personal_data_manager.h" | |
| 23 #include "chrome/browser/sync/api/sync_change.h" | |
| 24 #include "chrome/browser/sync/api/sync_data.h" | |
| 25 #include "chrome/browser/sync/api/sync_error.h" | |
| 26 #include "chrome/browser/sync/api/syncable_service.h" | |
| 27 #include "chrome/browser/sync/protocol/autofill_specifics.pb.h" | |
| 28 #include "chrome/browser/webdata/autofill_change.h" | |
| 29 #include "chrome/browser/webdata/autofill_entry.h" | |
| 30 #include "content/common/content_notification_types.h" | |
| 31 #include "content/common/notification_observer.h" | |
| 32 #include "content/common/notification_registrar.h" | |
| 33 | |
| 34 class AutofillProfile; | |
| 35 class Profile; | |
| 36 class ProfileSyncServiceAutofillTest; | |
| 37 | |
| 38 class WebDatabase; | |
| 39 | |
| 40 namespace browser_sync { | |
| 41 | |
| 42 extern const char kAutofillProfileTag[]; | |
| 43 | |
| 44 class UnrecoverableErrorHandler; | |
| 45 | |
| 46 class AutofillProfileSyncableService | |
| 47 : public SyncableService, | |
| 48 public NotificationObserver, | |
| 49 public base::NonThreadSafe { | |
| 50 public: | |
| 51 AutofillProfileSyncableService(WebDatabase* web_database, | |
| 52 PersonalDataManager* data_manager, | |
| 53 Profile* profile); | |
| 54 virtual ~AutofillProfileSyncableService(); | |
| 55 | |
| 56 static syncable::ModelType model_type() { return syncable::AUTOFILL_PROFILE; } | |
| 57 | |
| 58 // SyncableService implementation. | |
| 59 virtual SyncError MergeDataAndStartSyncing( | |
| 60 syncable::ModelType type, | |
| 61 const SyncDataList& initial_sync_data, | |
| 62 SyncChangeProcessor* sync_processor) OVERRIDE; | |
| 63 virtual void StopSyncing(syncable::ModelType type) OVERRIDE; | |
| 64 virtual SyncDataList GetAllSyncData(syncable::ModelType type) const OVERRIDE; | |
| 65 virtual SyncError ProcessSyncChanges( | |
| 66 const tracked_objects::Location& from_here, | |
| 67 const SyncChangeList& change_list) OVERRIDE; | |
| 68 | |
| 69 // NotificationObserver implementation. | |
| 70 virtual void Observe(int type, | |
| 71 const NotificationSource& source, | |
| 72 const NotificationDetails& details) OVERRIDE; | |
| 73 | |
| 74 protected: | |
| 75 // A convenience wrapper of a bunch of state we pass around while | |
| 76 // associating models, and send to the WebDatabase for persistence. | |
| 77 // We do this so we hold the write lock for only a small period. | |
| 78 // When storing the web db we are out of the write lock. | |
| 79 struct DataBundle; | |
| 80 | |
| 81 // Helper to query WebDatabase for the current autofill state. | |
| 82 // Made virtual for ease of mocking in the unit-test. | |
| 83 // Caller owns returned |profiles|. | |
| 84 virtual bool LoadAutofillData(std::vector<AutofillProfile*>* profiles); | |
| 85 | |
| 86 // Helper to persist any changes that occured during model association to | |
| 87 // the WebDatabase. | |
| 88 // Made virtual for ease of mocking in the unit-test. | |
| 89 virtual bool SaveChangesToWebData(const DataBundle& bundle); | |
| 90 | |
| 91 private: | |
| 92 friend class ::ProfileSyncServiceAutofillTest; | |
| 93 friend class MockAutofillProfileSyncableService; | |
| 94 FRIEND_TEST_ALL_PREFIXES(AutofillProfileSyncableServiceTest, | |
| 95 MergeDataAndStartSyncing); | |
| 96 FRIEND_TEST_ALL_PREFIXES(AutofillProfileSyncableServiceTest, GetAllSyncData); | |
| 97 FRIEND_TEST_ALL_PREFIXES(AutofillProfileSyncableServiceTest, | |
| 98 ProcessSyncChanges); | |
| 99 | |
| 100 // The map of the guid to index in the |profiles_| vector. | |
| 101 typedef std::map<std::string, size_t> GUIDToProfileMap; | |
| 102 | |
| 103 // Helper function that overwrites |profile| with data from proto-buffer | |
| 104 // |specifics|. | |
| 105 static bool OverwriteProfileWithServerData( | |
| 106 const sync_pb::AutofillProfileSpecifics& specifics, | |
| 107 AutofillProfile* profile); | |
| 108 | |
| 109 // Writes |profile| data into supplied |profile_specifics|. | |
| 110 static void WriteAutofillProfile(const AutofillProfile& profile, | |
| 111 sync_pb::EntitySpecifics* profile_specifics); | |
| 112 | |
| 113 // Creates |profile_map| from the supplied |profiles| vector. Necessary for | |
| 114 // fast processing of the changes. | |
| 115 void CreateGUIDToProfileMap(const std::vector<AutofillProfile *>& profiles, | |
|
Ilya Sherman
2011/09/03 00:10:57
nit: No space before the "*"
GeorgeY
2011/09/07 00:28:14
Done.
| |
| 116 GUIDToProfileMap* profile_map); | |
| 117 | |
| 118 // Creates or updates a profile based on |data|. Looks at the guid of the data | |
| 119 // and if a profile with such guid is present in |profile_map| updates it. If | |
| 120 // not, searches through it for similar profiles. If similar profile is | |
| 121 // found substitutes it for the new one, otherwise adds a new profile. Returns | |
| 122 // iterator pointing to added/updated profile. | |
| 123 GUIDToProfileMap::iterator CreateOrUpdateProfile( | |
| 124 const SyncData& data, GUIDToProfileMap* profile_map, DataBundle* bundle); | |
| 125 | |
| 126 // Syncs |change| to the cloud. | |
| 127 void ActOnChange(const AutofillProfileChange& change); | |
| 128 | |
| 129 // Creates SyncData based on supplied |profile|. | |
| 130 static SyncData CreateData(const AutofillProfile& profile); | |
| 131 | |
| 132 // For unit-tests. | |
| 133 AutofillProfileSyncableService(); | |
| 134 void set_sync_processor(SyncChangeProcessor* sync_processor) { | |
| 135 sync_processor_ = sync_processor; | |
| 136 } | |
| 137 | |
| 138 WebDatabase* web_database_; | |
| 139 PersonalDataManager* personal_data_; | |
| 140 NotificationRegistrar notification_registrar_; | |
| 141 | |
| 142 // Cached Autofill profiles. *Warning* deleted profiles are still in the | |
| 143 // vector - use the |profiles_map_| to iterate through actual profiles. | |
| 144 ScopedVector<AutofillProfile> profiles_; | |
| 145 GUIDToProfileMap profiles_map_; | |
| 146 | |
| 147 SyncChangeProcessor* sync_processor_; | |
| 148 | |
| 149 DISALLOW_COPY_AND_ASSIGN(AutofillProfileSyncableService); | |
| 150 }; | |
| 151 | |
| 152 struct AutofillProfileSyncableService::DataBundle { | |
| 153 DataBundle(); | |
| 154 ~DataBundle(); | |
| 155 | |
| 156 std::vector<std::string> profiles_to_delete; | |
| 157 std::vector<AutofillProfile*> updated_profiles; | |
| 158 std::vector<AutofillProfile*> new_profiles; | |
| 159 }; | |
| 160 | |
| 161 } // namespace browser_sync | |
| 162 | |
| 163 #endif // CHROME_BROWSER_SYNC_GLUE_AUTOFILL_PROFILE_SYNCABLE_SERVICE_H_ | |
| 164 | |
| OLD | NEW |