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