Upload upstream chromium 73.0.3683.0
[platform/framework/web/chromium-efl.git] / components / sync_preferences / pref_model_associator.h
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 COMPONENTS_SYNC_PREFERENCES_PREF_MODEL_ASSOCIATOR_H_
6 #define COMPONENTS_SYNC_PREFERENCES_PREF_MODEL_ASSOCIATOR_H_
7
8 #include <map>
9 #include <memory>
10 #include <set>
11 #include <string>
12 #include <unordered_map>
13 #include <vector>
14
15 #include "base/callback.h"
16 #include "base/compiler_specific.h"
17 #include "base/macros.h"
18 #include "base/observer_list.h"
19 #include "base/sequence_checker.h"
20 #include "components/sync/model/sync_data.h"
21 #include "components/sync/model/syncable_service.h"
22 #include "components/sync_preferences/unknown_user_pref_accessor.h"
23
24 namespace base {
25 class Value;
26 }
27
28 namespace sync_pb {
29 class PreferenceSpecifics;
30 }
31
32 namespace sync_preferences {
33
34 class PrefModelAssociatorClient;
35 class PrefServiceSyncable;
36 class SyncedPrefObserver;
37
38 // Contains all preference sync related logic.
39 // TODO(sync): Merge this into PrefService once we separate the profile
40 // PrefService from the local state PrefService.
41 class PrefModelAssociator : public syncer::SyncableService {
42  public:
43   // Constructs a PrefModelAssociator initializing the |client_| and |type_|
44   // instance variable. |client| and |accessor| are not owned by this object
45   // and the caller must ensure they outlive the PrefModelAssociator.
46   PrefModelAssociator(const PrefModelAssociatorClient* client,
47                       syncer::ModelType type,
48                       UnknownUserPrefAccessor* accessor);
49   ~PrefModelAssociator() override;
50
51   // See description above field for details.
52   bool models_associated() const { return models_associated_; }
53
54   // syncer::SyncableService implementation.
55
56   // Note for GetAllSyncData: This will build a model of all preferences
57   // registered as syncable with user controlled data. We do not track any
58   // information for preferences not registered locally as syncable and do not
59   // inform the syncer of non-user controlled preferences.
60   syncer::SyncDataList GetAllSyncData(syncer::ModelType type) const override;
61   syncer::SyncError ProcessSyncChanges(
62       const base::Location& from_here,
63       const syncer::SyncChangeList& change_list) override;
64   syncer::SyncMergeResult MergeDataAndStartSyncing(
65       syncer::ModelType type,
66       const syncer::SyncDataList& initial_sync_data,
67       std::unique_ptr<syncer::SyncChangeProcessor> sync_processor,
68       std::unique_ptr<syncer::SyncErrorFactory> sync_error_factory) override;
69   void StopSyncing(syncer::ModelType type) override;
70
71   // TODO(tschumann): Replace the RegisterPref() call with a
72   // VerifyPersistedPrefType() method. All pref registration checks are now
73   // done via the registry; no need to duplicate that concept.
74   // Register a preference with the specified name for syncing. We do not care
75   // about the type at registration time, but when changes arrive from the
76   // syncer, we check if they can be applied and if not drop them.
77   // Note: This should only be called at profile startup time (before sync
78   // begins).
79   void RegisterPref(const std::string& name);
80
81   // Process a local preference change. This can trigger new SyncChanges being
82   // sent to the syncer.
83   void ProcessPrefChange(const std::string& name);
84
85   // TODO(tschumann): Remove the associator's dependency on PrefServiceSyncable.
86   // It's only needed for calling OnIsSyncingChanged. This logic can be moved
87   // onto the associator: PrefServiceSyncable forwards the registration calls.
88   void SetPrefService(PrefServiceSyncable* pref_service);
89
90   // Merges the local_value into the supplied server_value and returns
91   // the result (caller takes ownership). If there is a conflict, the server
92   // value always takes precedence. Note that only certain preferences will
93   // actually be merged, all others will return a copy of the server value. See
94   // the method's implementation for details.
95   std::unique_ptr<base::Value> MergePreference(const std::string& name,
96                                                const base::Value& local_value,
97                                                const base::Value& server_value);
98
99   // Fills |sync_data| with a sync representation of the preference data
100   // provided.
101   bool CreatePrefSyncData(const std::string& name,
102                           const base::Value& value,
103                           syncer::SyncData* sync_data) const;
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;
107
108   // Returns true if the specified preference is registered for syncing.
109   bool IsPrefRegistered(const std::string& name) const;
110
111   // Adds a SyncedPrefObserver to watch for changes to a specific pref.
112   void AddSyncedPrefObserver(const std::string& name,
113                              SyncedPrefObserver* observer);
114
115   // Removes a SyncedPrefObserver from a pref's list of observers.
116   void RemoveSyncedPrefObserver(const std::string& name,
117                                 SyncedPrefObserver* observer);
118
119   // Returns the PrefModelAssociatorClient for this object.
120   const PrefModelAssociatorClient* client() const { return client_; }
121
122   // Register callback method which will get called at the end of
123   // PrefModelAssociator::MergeDataAndStartSyncing().
124   void RegisterMergeDataFinishedCallback(const base::Closure& callback);
125
126  private:
127   friend class PrefServiceSyncableTest;
128
129   // Create an association for a given preference. If |sync_pref| is valid,
130   // signifying that sync has data for this preference, we reconcile their data
131   // with ours and append a new UPDATE SyncChange to |sync_changes|. If
132   // sync_pref is not set, we append an ADD SyncChange to |sync_changes| with
133   // the current preference data.
134   // Note: We do not modify the sync data for preferences that are either
135   // controlled by policy (are not user modifiable) or have their default value
136   // (are not user controlled).
137   void InitPrefAndAssociate(const syncer::SyncData& sync_pref,
138                             const std::string& pref_name,
139                             syncer::SyncChangeList* sync_changes);
140
141   static std::unique_ptr<base::Value> MergeListValues(
142       const base::Value& from_value,
143       const base::Value& to_value);
144
145   static base::Value MergeDictionaryValues(const base::Value& from_value,
146                                            const base::Value& to_value);
147
148   // Extract preference value from sync specifics.
149   static base::Value* ReadPreferenceSpecifics(
150       const sync_pb::PreferenceSpecifics& specifics);
151
152   void NotifySyncedPrefObservers(const std::string& path, bool from_sync) const;
153
154   // Do we have an active association between the preferences and sync models?
155   // Set when start syncing, reset in StopSyncing. While this is not set, we
156   // ignore any local preference changes (when we start syncing we will look
157   // up the most recent values anyways).
158   bool models_associated_ = false;
159
160   // Whether we're currently processing changes from the syncer. While this is
161   // true, we ignore any local preference changes, since we triggered them.
162   bool processing_syncer_changes_ = false;
163
164   // A set of preference names.
165   typedef std::set<std::string> PreferenceSet;
166
167   // All preferences that have registered as being syncable with this profile.
168   PreferenceSet registered_preferences_;
169
170   // The preferences that are currently synced (excludes those preferences
171   // that have never had sync data and currently have default values or are
172   // policy controlled).
173   // Note: this set never decreases, only grows to eventually match
174   // registered_preferences_ as more preferences are synced. It determines
175   // whether a preference change should update an existing sync node or create
176   // a new sync node.
177   PreferenceSet synced_preferences_;
178
179   // The PrefService we are syncing to.
180   PrefServiceSyncable* pref_service_ = nullptr;
181
182   // A pref accessor to access prefs which might not be registered.
183   UnknownUserPrefAccessor* pref_accessor_;
184
185   // Sync's syncer::SyncChange handler. We push all our changes through this.
186   std::unique_ptr<syncer::SyncChangeProcessor> sync_processor_;
187
188   // Sync's error handler. We use this to create sync errors.
189   std::unique_ptr<syncer::SyncErrorFactory> sync_error_factory_;
190
191   // The datatype that this associator is responible for, either PREFERENCES or
192   // PRIORITY_PREFERENCES.
193   syncer::ModelType type_;
194
195   // Map prefs to lists of observers. Observers will receive notification when
196   // a pref changes, including the detail of whether or not the change came
197   // from sync.
198   using SyncedPrefObserverList =
199       base::ObserverList<SyncedPrefObserver>::Unchecked;
200   std::unordered_map<std::string, std::unique_ptr<SyncedPrefObserverList>>
201       synced_pref_observers_;
202
203   const PrefModelAssociatorClient* client_;  // Weak.
204
205   std::vector<base::Closure> callback_list_;
206
207   SEQUENCE_CHECKER(sequence_checker_);
208
209   DISALLOW_COPY_AND_ASSIGN(PrefModelAssociator);
210 };
211
212 }  // namespace sync_preferences
213
214 #endif  // COMPONENTS_SYNC_PREFERENCES_PREF_MODEL_ASSOCIATOR_H_