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 #ifndef CHROME_BROWSER_SYNC_GLUE_AUTOFILL_PROFILE_SYNCABLE_SERVICE_H_ |
| 5 #define CHROME_BROWSER_SYNC_GLUE_AUTOFILL_PROFILE_SYNCABLE_SERVICE_H_ |
| 6 #pragma once |
| 7 |
| 8 #include <map> |
| 9 #include <set> |
| 10 #include <string> |
| 11 #include <vector> |
| 12 |
| 13 #include "base/basictypes.h" |
| 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/synchronization/lock.h" |
| 16 #include "base/threading/non_thread_safe.h" |
| 17 #include "chrome/browser/autofill/personal_data_manager.h" |
| 18 #include "chrome/browser/sync/api/sync_change.h" |
| 19 #include "chrome/browser/sync/api/sync_data.h" |
| 20 #include "chrome/browser/sync/api/sync_error.h" |
| 21 #include "chrome/browser/sync/api/syncable_service.h" |
| 22 #include "chrome/browser/sync/protocol/autofill_specifics.pb.h" |
| 23 #include "chrome/browser/webdata/autofill_change.h" |
| 24 #include "chrome/browser/webdata/autofill_entry.h" |
| 25 #include "content/common/content_notification_types.h" |
| 26 #include "content/common/notification_observer.h" |
| 27 #include "content/common/notification_registrar.h" |
| 28 |
| 29 class AutofillProfile; |
| 30 class Profile; |
| 31 class ProfileSyncServiceAutofillTest; |
| 32 |
| 33 class WebDatabase; |
| 34 |
| 35 namespace browser_sync { |
| 36 |
| 37 extern const char kAutofillProfileTag[]; |
| 38 |
| 39 class UnrecoverableErrorHandler; |
| 40 |
| 41 // The sync implementation for AutofillProfiles. |
| 42 // MergeDataAndStartSyncing() called first, it does cloud->local and |
| 43 // local->cloud syncs. Then for each cloud change we receive |
| 44 // ProcessSyncChanges() and for each local change Observe() is called. |
| 45 class AutofillProfileSyncableService |
| 46 : public SyncableService, |
| 47 public NotificationObserver, |
| 48 public base::NonThreadSafe { |
| 49 public: |
| 50 AutofillProfileSyncableService(WebDatabase* web_database, |
| 51 PersonalDataManager* data_manager, |
| 52 Profile* profile); |
| 53 virtual ~AutofillProfileSyncableService(); |
| 54 |
| 55 static syncable::ModelType model_type() { return syncable::AUTOFILL_PROFILE; } |
| 56 |
| 57 // SyncableService implementation. |
| 58 virtual SyncError MergeDataAndStartSyncing( |
| 59 syncable::ModelType type, |
| 60 const SyncDataList& initial_sync_data, |
| 61 SyncChangeProcessor* sync_processor) OVERRIDE; |
| 62 virtual void StopSyncing(syncable::ModelType type) OVERRIDE; |
| 63 virtual SyncDataList GetAllSyncData(syncable::ModelType type) const OVERRIDE; |
| 64 virtual SyncError ProcessSyncChanges( |
| 65 const tracked_objects::Location& from_here, |
| 66 const SyncChangeList& change_list) OVERRIDE; |
| 67 |
| 68 // NotificationObserver implementation. |
| 69 virtual void Observe(int type, |
| 70 const NotificationSource& source, |
| 71 const NotificationDetails& details) OVERRIDE; |
| 72 |
| 73 protected: |
| 74 // A convenience wrapper of a bunch of state we pass around while |
| 75 // associating models, and send to the WebDatabase for persistence. |
| 76 // We do this so we hold the write lock for only a small period. |
| 77 // When storing the web db we are out of the write lock. |
| 78 struct DataBundle; |
| 79 |
| 80 // Helper to query WebDatabase for the current autofill state. |
| 81 // Made virtual for ease of mocking in the unit-test. |
| 82 // Caller owns returned |profiles|. |
| 83 virtual bool LoadAutofillData(std::vector<AutofillProfile*>* profiles); |
| 84 |
| 85 // Helper to persist any changes that occured during model association to |
| 86 // the WebDatabase. |
| 87 // Made virtual for ease of mocking in the unit-test. |
| 88 virtual bool SaveChangesToWebData(const DataBundle& bundle); |
| 89 |
| 90 private: |
| 91 friend class ::ProfileSyncServiceAutofillTest; |
| 92 friend class MockAutofillProfileSyncableService; |
| 93 FRIEND_TEST_ALL_PREFIXES(AutofillProfileSyncableServiceTest, |
| 94 MergeDataAndStartSyncing); |
| 95 FRIEND_TEST_ALL_PREFIXES(AutofillProfileSyncableServiceTest, GetAllSyncData); |
| 96 FRIEND_TEST_ALL_PREFIXES(AutofillProfileSyncableServiceTest, |
| 97 ProcessSyncChanges); |
| 98 |
| 99 // The map of the guid to profiles owned by the |profiles_| vector. |
| 100 typedef std::map<std::string, AutofillProfile*> GUIDToProfileMap; |
| 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 std::vector<AutofillProfile*>& profiles, |
| 115 GUIDToProfileMap* profile_map); |
| 116 |
| 117 // Creates or updates a profile based on |data|. Looks at the guid of the data |
| 118 // and if a profile with such guid is present in |profile_map| updates it. If |
| 119 // not, searches through it for similar profiles. If similar profile is |
| 120 // found substitutes it for the new one, otherwise adds a new profile. Returns |
| 121 // iterator pointing to added/updated profile. |
| 122 GUIDToProfileMap::iterator CreateOrUpdateProfile( |
| 123 const SyncData& data, GUIDToProfileMap* profile_map, DataBundle* bundle); |
| 124 |
| 125 // Syncs |change| to the cloud. |
| 126 void ActOnChange(const AutofillProfileChange& change); |
| 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 { |
| 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 |