• 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 from sync specifics.
96   base::Value* ReadPreferenceSpecifics(
97       const sync_pb::PreferenceSpecifics& specifics);
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 PrefsSyncableServiceTest;
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   // |migrated_preference_list| points to a vector that may be updated with a
122   // string containing the old name of the preference described by |pref_name|.
123   // Note: We do not modify the sync data for preferences that are either
124   // controlled by policy (are not user modifiable) or have their default value
125   // (are not user controlled).
126   void InitPrefAndAssociate(const syncer::SyncData& sync_pref,
127                             const std::string& pref_name,
128                             syncer::SyncChangeList* sync_changes,
129                             SyncDataMap* migrated_preference_list);
130 
131   static base::Value* MergeListValues(
132       const base::Value& from_value, const base::Value& to_value);
133   static base::Value* MergeDictionaryValues(const base::Value& from_value,
134                                             const base::Value& to_value);
135 
136   // Returns whether a given preference name is a new name of a migrated
137   // preference. Exposed here for testing.
138   static bool IsMigratedPreference(const char* preference_name);
139   static bool IsOldMigratedPreference(const char* old_preference_name);
140 
141   // Do we have an active association between the preferences and sync models?
142   // Set when start syncing, reset in StopSyncing. While this is not set, we
143   // ignore any local preference changes (when we start syncing we will look
144   // up the most recent values anyways).
145   bool models_associated_;
146 
147   // Whether we're currently processing changes from the syncer. While this is
148   // true, we ignore any local preference changes, since we triggered them.
149   bool processing_syncer_changes_;
150 
151   // A set of preference names.
152   typedef std::set<std::string> PreferenceSet;
153 
154   // All preferences that have registered as being syncable with this profile.
155   PreferenceSet registered_preferences_;
156 
157   // The preferences that are currently synced (excludes those preferences
158   // that have never had sync data and currently have default values or are
159   // policy controlled).
160   // Note: this set never decreases, only grows to eventually match
161   // registered_preferences_ as more preferences are synced. It determines
162   // whether a preference change should update an existing sync node or create
163   // a new sync node.
164   PreferenceSet synced_preferences_;
165 
166   // The PrefService we are syncing to.
167   PrefServiceSyncable* pref_service_;
168 
169   // Sync's syncer::SyncChange handler. We push all our changes through this.
170   scoped_ptr<syncer::SyncChangeProcessor> sync_processor_;
171 
172   // Sync's error handler. We use this to create sync errors.
173   scoped_ptr<syncer::SyncErrorFactory> sync_error_factory_;
174 
175   // The datatype that this associator is responible for, either PREFERENCES or
176   // PRIORITY_PREFERENCES.
177   syncer::ModelType type_;
178 
179  private:
180   // Map prefs to lists of observers. Observers will receive notification when
181   // a pref changes, including the detail of whether or not the change came
182   // from sync.
183   typedef ObserverList<SyncedPrefObserver> SyncedPrefObserverList;
184   typedef base::hash_map<std::string, SyncedPrefObserverList*>
185       SyncedPrefObserverMap;
186 
187   void NotifySyncedPrefObservers(const std::string& path, bool from_sync) const;
188 
189   SyncedPrefObserverMap synced_pref_observers_;
190 
191   DISALLOW_COPY_AND_ASSIGN(PrefModelAssociator);
192 };
193 
194 #endif  // CHROME_BROWSER_PREFS_PREF_MODEL_ASSOCIATOR_H_
195