Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(378)

Side by Side Diff: chrome/browser/prefs/pref_model_associator.h

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

Powered by Google App Engine
This is Rietveld 408576698