| 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 | |
| 5 #ifndef COMPONENTS_SYNCABLE_PREFS_PREF_MODEL_ASSOCIATOR_H_ | |
| 6 #define COMPONENTS_SYNCABLE_PREFS_PREF_MODEL_ASSOCIATOR_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <memory> | |
| 10 #include <set> | |
| 11 #include <string> | |
| 12 #include <vector> | |
| 13 | |
| 14 #include "base/callback.h" | |
| 15 #include "base/compiler_specific.h" | |
| 16 #include "base/containers/hash_tables.h" | |
| 17 #include "base/macros.h" | |
| 18 #include "base/observer_list.h" | |
| 19 #include "base/threading/non_thread_safe.h" | |
| 20 #include "components/sync/model/sync_data.h" | |
| 21 #include "components/sync/model/syncable_service.h" | |
| 22 #include "components/syncable_prefs/synced_pref_observer.h" | |
| 23 | |
| 24 namespace base { | |
| 25 class Value; | |
| 26 } | |
| 27 | |
| 28 namespace sync_pb { | |
| 29 class PreferenceSpecifics; | |
| 30 } | |
| 31 | |
| 32 namespace syncable_prefs { | |
| 33 | |
| 34 class PrefModelAssociatorClient; | |
| 35 class PrefRegistrySyncable; | |
| 36 class PrefServiceSyncable; | |
| 37 | |
| 38 // Contains all preference sync related logic. | |
| 39 // TODO(sync): Merge this into PrefService once we separate the profile | |
| 40 // PrefService from the local state PrefService. | |
| 41 class PrefModelAssociator | |
| 42 : public syncer::SyncableService, | |
| 43 public base::NonThreadSafe { | |
| 44 public: | |
| 45 // Constructs a PrefModelAssociator initializing the |client_| and |type_| | |
| 46 // instance variable. The |client| is not owned by this object and the caller | |
| 47 // must ensure that it oulives the PrefModelAssociator. | |
| 48 PrefModelAssociator(const PrefModelAssociatorClient* client, | |
| 49 syncer::ModelType type); | |
| 50 ~PrefModelAssociator() override; | |
| 51 | |
| 52 // See description above field for details. | |
| 53 bool models_associated() const { return models_associated_; } | |
| 54 | |
| 55 // syncer::SyncableService implementation. | |
| 56 syncer::SyncDataList GetAllSyncData(syncer::ModelType type) const override; | |
| 57 syncer::SyncError ProcessSyncChanges( | |
| 58 const tracked_objects::Location& from_here, | |
| 59 const syncer::SyncChangeList& change_list) override; | |
| 60 syncer::SyncMergeResult MergeDataAndStartSyncing( | |
| 61 syncer::ModelType type, | |
| 62 const syncer::SyncDataList& initial_sync_data, | |
| 63 std::unique_ptr<syncer::SyncChangeProcessor> sync_processor, | |
| 64 std::unique_ptr<syncer::SyncErrorFactory> sync_error_factory) override; | |
| 65 void StopSyncing(syncer::ModelType type) override; | |
| 66 | |
| 67 // Returns the list of preference names that are registered as syncable, and | |
| 68 // hence should be monitored for changes. | |
| 69 std::set<std::string> registered_preferences() const; | |
| 70 | |
| 71 // Register a preference with the specified name for syncing. We do not care | |
| 72 // about the type at registration time, but when changes arrive from the | |
| 73 // syncer, we check if they can be applied and if not drop them. | |
| 74 // Note: This should only be called at profile startup time (before sync | |
| 75 // begins). | |
| 76 virtual void RegisterPref(const char* name); | |
| 77 | |
| 78 // Returns true if the specified preference is registered for syncing. | |
| 79 virtual bool IsPrefRegistered(const char* name); | |
| 80 | |
| 81 // Process a local preference change. This can trigger new SyncChanges being | |
| 82 // sent to the syncer. | |
| 83 virtual void ProcessPrefChange(const std::string& name); | |
| 84 | |
| 85 void SetPrefService(PrefServiceSyncable* pref_service); | |
| 86 | |
| 87 // Merges the local_value into the supplied server_value and returns | |
| 88 // the result (caller takes ownership). If there is a conflict, the server | |
| 89 // value always takes precedence. Note that only certain preferences will | |
| 90 // actually be merged, all others will return a copy of the server value. See | |
| 91 // the method's implementation for details. | |
| 92 std::unique_ptr<base::Value> MergePreference(const std::string& name, | |
| 93 const base::Value& local_value, | |
| 94 const base::Value& server_value); | |
| 95 | |
| 96 // Fills |sync_data| with a sync representation of the preference data | |
| 97 // provided. | |
| 98 bool CreatePrefSyncData(const std::string& name, | |
| 99 const base::Value& value, | |
| 100 syncer::SyncData* sync_data) const; | |
| 101 | |
| 102 // Extract preference value from sync specifics. | |
| 103 base::Value* ReadPreferenceSpecifics( | |
| 104 const sync_pb::PreferenceSpecifics& specifics); | |
| 105 | |
| 106 // Returns true if the pref under the given name is pulled down from sync. | |
| 107 // Note this does not refer to SYNCABLE_PREF. | |
| 108 bool IsPrefSynced(const std::string& name) const; | |
| 109 | |
| 110 // Adds a SyncedPrefObserver to watch for changes to a specific pref. | |
| 111 void AddSyncedPrefObserver(const std::string& name, | |
| 112 SyncedPrefObserver* observer); | |
| 113 | |
| 114 // Removes a SyncedPrefObserver from a pref's list of observers. | |
| 115 void RemoveSyncedPrefObserver(const std::string& name, | |
| 116 SyncedPrefObserver* observer); | |
| 117 | |
| 118 // Returns the PrefModelAssociatorClient for this object. | |
| 119 const PrefModelAssociatorClient* client() const { return client_; } | |
| 120 | |
| 121 // Set the PrefModelAssociatorClient to use for that object during tests. | |
| 122 void SetPrefModelAssociatorClientForTesting( | |
| 123 const PrefModelAssociatorClient* client); | |
| 124 | |
| 125 // Register callback method which will get called at the end of | |
| 126 // PrefModelAssociator::MergeDataAndStartSyncing(). | |
| 127 void RegisterMergeDataFinishedCallback(const base::Closure& callback); | |
| 128 | |
| 129 protected: | |
| 130 friend class PrefServiceSyncableTest; | |
| 131 | |
| 132 typedef std::map<std::string, syncer::SyncData> SyncDataMap; | |
| 133 | |
| 134 // Create an association for a given preference. If |sync_pref| is valid, | |
| 135 // signifying that sync has data for this preference, we reconcile their data | |
| 136 // with ours and append a new UPDATE SyncChange to |sync_changes|. If | |
| 137 // sync_pref is not set, we append an ADD SyncChange to |sync_changes| with | |
| 138 // the current preference data. | |
| 139 // Note: We do not modify the sync data for preferences that are either | |
| 140 // controlled by policy (are not user modifiable) or have their default value | |
| 141 // (are not user controlled). | |
| 142 void InitPrefAndAssociate(const syncer::SyncData& sync_pref, | |
| 143 const std::string& pref_name, | |
| 144 syncer::SyncChangeList* sync_changes); | |
| 145 | |
| 146 static base::Value* MergeListValues( | |
| 147 const base::Value& from_value, const base::Value& to_value); | |
| 148 static base::Value* MergeDictionaryValues(const base::Value& from_value, | |
| 149 const base::Value& to_value); | |
| 150 | |
| 151 // Do we have an active association between the preferences and sync models? | |
| 152 // Set when start syncing, reset in StopSyncing. While this is not set, we | |
| 153 // ignore any local preference changes (when we start syncing we will look | |
| 154 // up the most recent values anyways). | |
| 155 bool models_associated_; | |
| 156 | |
| 157 // Whether we're currently processing changes from the syncer. While this is | |
| 158 // true, we ignore any local preference changes, since we triggered them. | |
| 159 bool processing_syncer_changes_; | |
| 160 | |
| 161 // A set of preference names. | |
| 162 typedef std::set<std::string> PreferenceSet; | |
| 163 | |
| 164 // All preferences that have registered as being syncable with this profile. | |
| 165 PreferenceSet registered_preferences_; | |
| 166 | |
| 167 // The preferences that are currently synced (excludes those preferences | |
| 168 // that have never had sync data and currently have default values or are | |
| 169 // policy controlled). | |
| 170 // Note: this set never decreases, only grows to eventually match | |
| 171 // registered_preferences_ as more preferences are synced. It determines | |
| 172 // whether a preference change should update an existing sync node or create | |
| 173 // a new sync node. | |
| 174 PreferenceSet synced_preferences_; | |
| 175 | |
| 176 // The PrefService we are syncing to. | |
| 177 PrefServiceSyncable* pref_service_; | |
| 178 | |
| 179 // Sync's syncer::SyncChange handler. We push all our changes through this. | |
| 180 std::unique_ptr<syncer::SyncChangeProcessor> sync_processor_; | |
| 181 | |
| 182 // Sync's error handler. We use this to create sync errors. | |
| 183 std::unique_ptr<syncer::SyncErrorFactory> sync_error_factory_; | |
| 184 | |
| 185 // The datatype that this associator is responible for, either PREFERENCES or | |
| 186 // PRIORITY_PREFERENCES. | |
| 187 syncer::ModelType type_; | |
| 188 | |
| 189 private: | |
| 190 // Map prefs to lists of observers. Observers will receive notification when | |
| 191 // a pref changes, including the detail of whether or not the change came | |
| 192 // from sync. | |
| 193 using SyncedPrefObserverList = base::ObserverList<SyncedPrefObserver>; | |
| 194 using SyncedPrefObserverMap = | |
| 195 base::hash_map<std::string, std::unique_ptr<SyncedPrefObserverList>>; | |
| 196 | |
| 197 void NotifySyncedPrefObservers(const std::string& path, bool from_sync) const; | |
| 198 | |
| 199 SyncedPrefObserverMap synced_pref_observers_; | |
| 200 const PrefModelAssociatorClient* client_; // Weak. | |
| 201 | |
| 202 std::vector<base::Closure> callback_list_; | |
| 203 | |
| 204 DISALLOW_COPY_AND_ASSIGN(PrefModelAssociator); | |
| 205 }; | |
| 206 | |
| 207 } // namespace syncable_prefs | |
| 208 | |
| 209 #endif // COMPONENTS_SYNCABLE_PREFS_PREF_MODEL_ASSOCIATOR_H_ | |
| OLD | NEW |