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 | |
| 5 #ifndef CHROME_BROWSER_SYNC_GLUE_AUTOFILL_PROFILE_SYNCABLE_SERVICE_H_ | |
| 6 #define CHROME_BROWSER_SYNC_GLUE_AUTOFILL_PROFILE_SYNCABLE_SERVICE_H_ | |
| 7 #pragma once | |
| 8 | |
| 9 #include <map> | |
| 10 #include <set> | |
| 11 #include <string> | |
| 12 #include <vector> | |
| 13 | |
| 14 #include "base/basictypes.h" | |
| 15 #include "base/memory/ref_counted.h" | |
| 16 #include "base/synchronization/lock.h" | |
| 17 #include "base/threading/non_thread_safe.h" | |
| 18 #include "chrome/browser/autofill/personal_data_manager.h" | |
| 19 #include "chrome/browser/sync/api/sync_change.h" | |
| 20 #include "chrome/browser/sync/api/sync_data.h" | |
| 21 #include "chrome/browser/sync/api/sync_error.h" | |
| 22 #include "chrome/browser/sync/api/syncable_service.h" | |
| 23 #include "chrome/browser/sync/protocol/autofill_specifics.pb.h" | |
| 24 #include "chrome/browser/webdata/autofill_change.h" | |
| 25 #include "chrome/browser/webdata/autofill_entry.h" | |
| 26 #include "content/common/content_notification_types.h" | |
| 27 #include "content/common/notification_observer.h" | |
| 28 #include "content/common/notification_registrar.h" | |
| 29 | |
| 30 class AutofillProfile; | |
| 31 class Profile; | |
| 32 class ProfileSyncServiceAutofillTest; | |
| 33 | |
| 34 class WebDatabase; | |
| 35 | |
| 36 namespace browser_sync { | |
| 37 | |
| 38 extern const char kAutofillProfileTag[]; | |
| 39 | |
| 40 class UnrecoverableErrorHandler; | |
| 41 | |
| 42 // Contains all model association related logic: | |
| 43 // * Algorithm to associate autofill model and sync model. | |
| 44 // We do not check if we have local data before this run; we always | |
| 45 // merge and sync. | |
|
Ilya Sherman
2011/09/02 21:49:50
nit: This comment seems really out of context here
| |
| 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 // The map of the guid to index in the |profiles_| vector. | |
| 76 typedef std::map<std::string, size_t> GUIDToProfileMap; | |
|
Ilya Sherman
2011/09/02 21:49:50
nit: Can this be moved to "private:"?
Ilya Sherman
2011/09/02 21:49:50
nit: Why map to an index rather than to an |Autofi
GeorgeY
2011/09/03 00:01:29
All the Load/Save accessors use vector<AutofillPro
GeorgeY
2011/09/03 00:01:29
Done.
Ilya Sherman
2011/09/03 00:10:57
I don't follow what you mean here. Storing the in
| |
| 77 | |
| 78 // A convenience wrapper of a bunch of state we pass around while | |
| 79 // associating models, and send to the WebDatabase for persistence. | |
| 80 // We do this so we hold the write lock for only a small period. | |
| 81 // When storing the web db we are out of the write lock. | |
| 82 struct DataBundle; | |
| 83 | |
| 84 // Helper to query WebDatabase for the current autofill state. | |
| 85 // Made virtual for ease of mocking in the unit-test. | |
| 86 virtual bool LoadAutofillData(std::vector<AutofillProfile*>* profiles) const; | |
|
Ilya Sherman
2011/09/02 21:49:50
nit: Probably worth mentioning in the function com
GeorgeY
2011/09/03 00:01:29
Done.
| |
| 87 | |
| 88 // Helper to persist any changes that occured during model association to | |
| 89 // the WebDatabase. | |
| 90 // Made virtual for ease of mocking in the unit-test. | |
| 91 virtual bool SaveChangesToWebData(const DataBundle& bundle); | |
| 92 | |
| 93 private: | |
| 94 friend class ::ProfileSyncServiceAutofillTest; | |
| 95 friend class MockAutofillProfileSyncableService; | |
| 96 FRIEND_TEST_ALL_PREFIXES(AutofillProfileSyncableServiceTest, | |
| 97 MergeDataAndStartSyncing); | |
| 98 FRIEND_TEST_ALL_PREFIXES(AutofillProfileSyncableServiceTest, GetAllSyncData); | |
| 99 FRIEND_TEST_ALL_PREFIXES(AutofillProfileSyncableServiceTest, | |
| 100 ProcessSyncChanges); | |
| 101 | |
| 102 // Helper function that overwrites |profile| with data from proto-buffer | |
| 103 // |specifics|. | |
| 104 static bool OverwriteProfileWithServerData( | |
| 105 const sync_pb::AutofillProfileSpecifics& specifics, | |
| 106 AutofillProfile* profile); | |
| 107 | |
| 108 // Writes |profile| data into supplied |profile_specifics|. | |
| 109 static void WriteAutofillProfile(const AutofillProfile& profile, | |
| 110 sync_pb::EntitySpecifics* profile_specifics); | |
| 111 | |
| 112 // Creates |profile_map| from the supplied |profiles| vector. Necessary for | |
| 113 // fast processing of the changes. | |
| 114 void CreateGUIDToProfileMap(const ScopedVector<AutofillProfile>& profiles, | |
|
Ilya Sherman
2011/09/02 21:49:50
nit: Passing around ScopedVectors, even by referen
GeorgeY
2011/09/03 00:01:29
Done.
| |
| 115 GUIDToProfileMap* profile_map); | |
| 116 | |
| 117 // Creates or updates a profile based on |data|. Looks at the guid of the data | |
| 118 // if a profile with such guid is present in |profile_map| updates it. If not | |
| 119 // searches through it for similar profiles. if similar profile is | |
| 120 // found substitutes it for the new one, otherwise add a new profile. Returns | |
| 121 // iterator pointing to added/updated profile. | |
|
Ilya Sherman
2011/09/02 21:49:50
nit: There are several sentence breaks in this com
GeorgeY
2011/09/03 00:01:29
Done.
| |
| 122 GUIDToProfileMap::iterator CreateOrUpdateProfile( | |
| 123 const SyncData& data, GUIDToProfileMap* profile_map, DataBundle* bundle); | |
| 124 | |
| 125 // Syncs |change| to the cloud. | |
| 126 void ActOnChange(AutofillProfileChange* change); | |
|
Ilya Sherman
2011/09/02 21:49:50
nit: Should |change| be passed by const-ref here?
GeorgeY
2011/09/03 00:01:29
Done.
| |
| 127 | |
| 128 // Creates SyncData based on supplied |profile|. | |
| 129 static SyncData CreateData(const AutofillProfile& profile); | |
| 130 | |
| 131 // For unit-tests. | |
| 132 AutofillProfileSyncableService(); | |
| 133 void set_sync_processor(SyncChangeProcessor* sync_processor) { | |
| 134 sync_processor_ = sync_processor; | |
| 135 } | |
| 136 | |
| 137 WebDatabase* web_database_; | |
| 138 PersonalDataManager* personal_data_; | |
| 139 NotificationRegistrar notification_registrar_; | |
| 140 | |
| 141 // Cached Autofill profiles. *Warning* deleted profiles are still in the | |
| 142 // vector - use the |profiles_map_| to iterate through actual profiles. | |
| 143 ScopedVector<AutofillProfile> profiles_; | |
| 144 GUIDToProfileMap profiles_map_; | |
| 145 | |
| 146 SyncChangeProcessor* sync_processor_; | |
| 147 | |
| 148 DISALLOW_COPY_AND_ASSIGN(AutofillProfileSyncableService); | |
| 149 }; | |
| 150 | |
| 151 struct AutofillProfileSyncableService::DataBundle { | |
|
Ilya Sherman
2011/09/02 21:49:50
nit: Quoth David, "Move this to the .cc file". Qu
GeorgeY
2011/09/03 00:01:29
Quoth me :): it is used in unit-tests, so it needs
Ilya Sherman
2011/09/03 00:10:57
Ok :)
| |
| 152 DataBundle(); | |
| 153 ~DataBundle(); | |
| 154 | |
| 155 std::vector<std::string> profiles_to_delete; | |
| 156 std::vector<AutofillProfile*> updated_profiles; | |
| 157 std::vector<AutofillProfile*> new_profiles; | |
| 158 }; | |
| 159 | |
| 160 } // namespace browser_sync | |
| 161 | |
| 162 #endif // CHROME_BROWSER_SYNC_GLUE_AUTOFILL_PROFILE_SYNCABLE_SERVICE_H_ | |
| 163 | |
| OLD | NEW |