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.
5 #ifndef COMPONENTS_SYNC_PREFERENCES_PREF_MODEL_ASSOCIATOR_H_
6 #define COMPONENTS_SYNC_PREFERENCES_PREF_MODEL_ASSOCIATOR_H_
12 #include <unordered_map>
15 #include "base/callback.h"
16 #include "base/compiler_specific.h"
17 #include "base/macros.h"
18 #include "base/observer_list.h"
19 #include "base/sequence_checker.h"
20 #include "components/sync/model/sync_data.h"
21 #include "components/sync/model/syncable_service.h"
22 #include "components/sync_preferences/unknown_user_pref_accessor.h"
29 class PreferenceSpecifics;
32 namespace sync_preferences {
34 class PrefModelAssociatorClient;
35 class PrefServiceSyncable;
36 class SyncedPrefObserver;
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 : public syncer::SyncableService {
43 // Constructs a PrefModelAssociator initializing the |client_| and |type_|
44 // instance variable. |client| and |accessor| are not owned by this object
45 // and the caller must ensure they outlive the PrefModelAssociator.
46 PrefModelAssociator(const PrefModelAssociatorClient* client,
47 syncer::ModelType type,
48 UnknownUserPrefAccessor* accessor);
49 ~PrefModelAssociator() override;
51 // See description above field for details.
52 bool models_associated() const { return models_associated_; }
54 // syncer::SyncableService implementation.
56 // Note for GetAllSyncData: This will build a model of all preferences
57 // registered as syncable with user controlled data. We do not track any
58 // information for preferences not registered locally as syncable and do not
59 // inform the syncer of non-user controlled preferences.
60 syncer::SyncDataList GetAllSyncData(syncer::ModelType type) const override;
61 syncer::SyncError ProcessSyncChanges(
62 const base::Location& from_here,
63 const syncer::SyncChangeList& change_list) override;
64 syncer::SyncMergeResult MergeDataAndStartSyncing(
65 syncer::ModelType type,
66 const syncer::SyncDataList& initial_sync_data,
67 std::unique_ptr<syncer::SyncChangeProcessor> sync_processor,
68 std::unique_ptr<syncer::SyncErrorFactory> sync_error_factory) override;
69 void StopSyncing(syncer::ModelType type) override;
71 // TODO(tschumann): Replace the RegisterPref() call with a
72 // VerifyPersistedPrefType() method. All pref registration checks are now
73 // done via the registry; no need to duplicate that concept.
74 // Register a preference with the specified name for syncing. We do not care
75 // about the type at registration time, but when changes arrive from the
76 // syncer, we check if they can be applied and if not drop them.
77 // Note: This should only be called at profile startup time (before sync
79 void RegisterPref(const std::string& name);
81 // Process a local preference change. This can trigger new SyncChanges being
82 // sent to the syncer.
83 void ProcessPrefChange(const std::string& name);
85 // TODO(tschumann): Remove the associator's dependency on PrefServiceSyncable.
86 // It's only needed for calling OnIsSyncingChanged. This logic can be moved
87 // onto the associator: PrefServiceSyncable forwards the registration calls.
88 void SetPrefService(PrefServiceSyncable* pref_service);
90 // Merges the local_value into the supplied server_value and returns
91 // the result (caller takes ownership). If there is a conflict, the server
92 // value always takes precedence. Note that only certain preferences will
93 // actually be merged, all others will return a copy of the server value. See
94 // the method's implementation for details.
95 std::unique_ptr<base::Value> MergePreference(const std::string& name,
96 const base::Value& local_value,
97 const base::Value& server_value);
99 // Fills |sync_data| with a sync representation of the preference data
101 bool CreatePrefSyncData(const std::string& name,
102 const base::Value& value,
103 syncer::SyncData* sync_data) const;
104 // Returns true if the pref under the given name is pulled down from sync.
105 // Note this does not refer to SYNCABLE_PREF.
106 bool IsPrefSynced(const std::string& name) const;
108 // Returns true if the specified preference is registered for syncing.
109 bool IsPrefRegistered(const std::string& name) const;
111 // Adds a SyncedPrefObserver to watch for changes to a specific pref.
112 void AddSyncedPrefObserver(const std::string& name,
113 SyncedPrefObserver* observer);
115 // Removes a SyncedPrefObserver from a pref's list of observers.
116 void RemoveSyncedPrefObserver(const std::string& name,
117 SyncedPrefObserver* observer);
119 // Returns the PrefModelAssociatorClient for this object.
120 const PrefModelAssociatorClient* client() const { return client_; }
122 // Register callback method which will get called at the end of
123 // PrefModelAssociator::MergeDataAndStartSyncing().
124 void RegisterMergeDataFinishedCallback(const base::Closure& callback);
127 friend class PrefServiceSyncableTest;
129 // Create an association for a given preference. If |sync_pref| is valid,
130 // signifying that sync has data for this preference, we reconcile their data
131 // with ours and append a new UPDATE SyncChange to |sync_changes|. If
132 // sync_pref is not set, we append an ADD SyncChange to |sync_changes| with
133 // the current preference data.
134 // Note: We do not modify the sync data for preferences that are either
135 // controlled by policy (are not user modifiable) or have their default value
136 // (are not user controlled).
137 void InitPrefAndAssociate(const syncer::SyncData& sync_pref,
138 const std::string& pref_name,
139 syncer::SyncChangeList* sync_changes);
141 static std::unique_ptr<base::Value> MergeListValues(
142 const base::Value& from_value,
143 const base::Value& to_value);
145 static base::Value MergeDictionaryValues(const base::Value& from_value,
146 const base::Value& to_value);
148 // Extract preference value from sync specifics.
149 static base::Value* ReadPreferenceSpecifics(
150 const sync_pb::PreferenceSpecifics& specifics);
152 void NotifySyncedPrefObservers(const std::string& path, bool from_sync) const;
154 // Do we have an active association between the preferences and sync models?
155 // Set when start syncing, reset in StopSyncing. While this is not set, we
156 // ignore any local preference changes (when we start syncing we will look
157 // up the most recent values anyways).
158 bool models_associated_ = false;
160 // Whether we're currently processing changes from the syncer. While this is
161 // true, we ignore any local preference changes, since we triggered them.
162 bool processing_syncer_changes_ = false;
164 // A set of preference names.
165 typedef std::set<std::string> PreferenceSet;
167 // All preferences that have registered as being syncable with this profile.
168 PreferenceSet registered_preferences_;
170 // The preferences that are currently synced (excludes those preferences
171 // that have never had sync data and currently have default values or are
172 // policy controlled).
173 // Note: this set never decreases, only grows to eventually match
174 // registered_preferences_ as more preferences are synced. It determines
175 // whether a preference change should update an existing sync node or create
177 PreferenceSet synced_preferences_;
179 // The PrefService we are syncing to.
180 PrefServiceSyncable* pref_service_ = nullptr;
182 // A pref accessor to access prefs which might not be registered.
183 UnknownUserPrefAccessor* pref_accessor_;
185 // Sync's syncer::SyncChange handler. We push all our changes through this.
186 std::unique_ptr<syncer::SyncChangeProcessor> sync_processor_;
188 // Sync's error handler. We use this to create sync errors.
189 std::unique_ptr<syncer::SyncErrorFactory> sync_error_factory_;
191 // The datatype that this associator is responible for, either PREFERENCES or
192 // PRIORITY_PREFERENCES.
193 syncer::ModelType type_;
195 // Map prefs to lists of observers. Observers will receive notification when
196 // a pref changes, including the detail of whether or not the change came
198 using SyncedPrefObserverList =
199 base::ObserverList<SyncedPrefObserver>::Unchecked;
200 std::unordered_map<std::string, std::unique_ptr<SyncedPrefObserverList>>
201 synced_pref_observers_;
203 const PrefModelAssociatorClient* client_; // Weak.
205 std::vector<base::Closure> callback_list_;
207 SEQUENCE_CHECKER(sequence_checker_);
209 DISALLOW_COPY_AND_ASSIGN(PrefModelAssociator);
212 } // namespace sync_preferences
214 #endif // COMPONENTS_SYNC_PREFERENCES_PREF_MODEL_ASSOCIATOR_H_