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