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 CHROME_BROWSER_PREFS_PREF_MODEL_ASSOCIATOR_H_
6 #define CHROME_BROWSER_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 "chrome/browser/prefs/synced_pref_observer.h"
19 #include "sync/api/sync_data.h"
20 #include "sync/api/syncable_service.h"
22 class PrefRegistrySyncable
;
23 class PrefServiceSyncable
;
26 class PreferenceSpecifics
;
33 // Contains all preference sync related logic.
34 // TODO(sync): Merge this into PrefService once we separate the profile
35 // PrefService from the local state PrefService.
36 class PrefModelAssociator
37 : public syncer::SyncableService
,
38 public base::NonThreadSafe
{
40 explicit PrefModelAssociator(syncer::ModelType type
);
41 ~PrefModelAssociator() override
;
43 // See description above field for details.
44 bool models_associated() const { return models_associated_
; }
46 // syncer::SyncableService implementation.
47 syncer::SyncDataList
GetAllSyncData(syncer::ModelType type
) const override
;
48 syncer::SyncError
ProcessSyncChanges(
49 const tracked_objects::Location
& from_here
,
50 const syncer::SyncChangeList
& change_list
) override
;
51 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 void StopSyncing(syncer::ModelType type
) override
;
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;
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
67 virtual void RegisterPref(const char* name
);
69 // Returns true if the specified preference is registered for syncing.
70 virtual bool IsPrefRegistered(const char* name
);
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
);
76 void SetPrefService(PrefServiceSyncable
* pref_service
);
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
);
88 // Fills |sync_data| with a sync representation of the preference data
90 bool CreatePrefSyncData(const std::string
& name
,
91 const base::Value
& value
,
92 syncer::SyncData
* sync_data
) const;
94 // Extract preference value from sync specifics.
95 base::Value
* ReadPreferenceSpecifics(
96 const sync_pb::PreferenceSpecifics
& specifics
);
98 // Returns true if the pref under the given name is pulled down from sync.
99 // Note this does not refer to SYNCABLE_PREF.
100 bool IsPrefSynced(const std::string
& name
) const;
102 // Adds a SyncedPrefObserver to watch for changes to a specific pref.
103 void AddSyncedPrefObserver(const std::string
& name
,
104 SyncedPrefObserver
* observer
);
106 // Removes a SyncedPrefObserver from a pref's list of observers.
107 void RemoveSyncedPrefObserver(const std::string
& name
,
108 SyncedPrefObserver
* observer
);
111 friend class PrefServiceSyncableTest
;
113 typedef std::map
<std::string
, syncer::SyncData
> SyncDataMap
;
115 // Create an association for a given preference. If |sync_pref| is valid,
116 // signifying that sync has data for this preference, we reconcile their data
117 // with ours and append a new UPDATE SyncChange to |sync_changes|. If
118 // sync_pref is not set, we append an ADD SyncChange to |sync_changes| with
119 // the current preference data.
120 // |migrated_preference_list| points to a vector that may be updated with a
121 // string containing the old name of the preference described by |pref_name|.
122 // Note: We do not modify the sync data for preferences that are either
123 // controlled by policy (are not user modifiable) or have their default value
124 // (are not user controlled).
125 void InitPrefAndAssociate(const syncer::SyncData
& sync_pref
,
126 const std::string
& pref_name
,
127 syncer::SyncChangeList
* sync_changes
,
128 SyncDataMap
* migrated_preference_list
);
130 static base::Value
* MergeListValues(
131 const base::Value
& from_value
, const base::Value
& to_value
);
132 static base::Value
* MergeDictionaryValues(const base::Value
& from_value
,
133 const base::Value
& to_value
);
135 // Returns whether a given preference name is a new name of a migrated
136 // preference. Exposed here for testing.
137 static bool IsMigratedPreference(const char* preference_name
);
138 static bool IsOldMigratedPreference(const char* old_preference_name
);
140 // Do we have an active association between the preferences and sync models?
141 // Set when start syncing, reset in StopSyncing. While this is not set, we
142 // ignore any local preference changes (when we start syncing we will look
143 // up the most recent values anyways).
144 bool models_associated_
;
146 // Whether we're currently processing changes from the syncer. While this is
147 // true, we ignore any local preference changes, since we triggered them.
148 bool processing_syncer_changes_
;
150 // A set of preference names.
151 typedef std::set
<std::string
> PreferenceSet
;
153 // All preferences that have registered as being syncable with this profile.
154 PreferenceSet registered_preferences_
;
156 // The preferences that are currently synced (excludes those preferences
157 // that have never had sync data and currently have default values or are
158 // policy controlled).
159 // Note: this set never decreases, only grows to eventually match
160 // registered_preferences_ as more preferences are synced. It determines
161 // whether a preference change should update an existing sync node or create
163 PreferenceSet synced_preferences_
;
165 // The PrefService we are syncing to.
166 PrefServiceSyncable
* pref_service_
;
168 // Sync's syncer::SyncChange handler. We push all our changes through this.
169 scoped_ptr
<syncer::SyncChangeProcessor
> sync_processor_
;
171 // Sync's error handler. We use this to create sync errors.
172 scoped_ptr
<syncer::SyncErrorFactory
> sync_error_factory_
;
174 // The datatype that this associator is responible for, either PREFERENCES or
175 // PRIORITY_PREFERENCES.
176 syncer::ModelType type_
;
179 // Map prefs to lists of observers. Observers will receive notification when
180 // a pref changes, including the detail of whether or not the change came
182 typedef base::ObserverList
<SyncedPrefObserver
> SyncedPrefObserverList
;
183 typedef base::hash_map
<std::string
, SyncedPrefObserverList
*>
184 SyncedPrefObserverMap
;
186 void NotifySyncedPrefObservers(const std::string
& path
, bool from_sync
) const;
188 SyncedPrefObserverMap synced_pref_observers_
;
190 DISALLOW_COPY_AND_ASSIGN(PrefModelAssociator
);
193 #endif // CHROME_BROWSER_PREFS_PREF_MODEL_ASSOCIATOR_H_