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