File: pref_model_associator.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (220 lines) | stat: -rw-r--r-- 9,104 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_SYNC_PREFERENCES_PREF_MODEL_ASSOCIATOR_H_
#define COMPONENTS_SYNC_PREFERENCES_PREF_MODEL_ASSOCIATOR_H_

#include <functional>
#include <memory>
#include <set>
#include <string>
#include <string_view>
#include <unordered_map>

#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/sequence_checker.h"
#include "base/values.h"
#include "components/prefs/transparent_unordered_string_map.h"
#include "components/prefs/writeable_pref_store.h"
#include "components/sync/base/data_type.h"
#include "components/sync/model/sync_data.h"
#include "components/sync/model/syncable_service.h"
#include "components/sync_preferences/pref_model_associator_client.h"
#include "components/sync_preferences/synced_pref_observer.h"

namespace base {
class Value;
}

namespace sync_pb {
class EntitySpecifics;
class PreferenceSpecifics;
}  // namespace sync_pb

namespace sync_preferences {

class DualLayerUserPrefStore;
class PrefModelAssociatorClient;

class PrefServiceForAssociator {
 public:
  virtual base::Value::Type GetRegisteredPrefType(
      std::string_view pref_name) const = 0;
  virtual void OnIsSyncingChanged() = 0;
  virtual uint32_t GetWriteFlags(std::string_view pref_name) const = 0;
};

// Contains all preference sync related logic.
class PrefModelAssociator final : public syncer::SyncableService,
                                  public PrefStore::Observer {
 public:
  // The |client| is not owned and must outlive this object.
  // |user_prefs| is the PrefStore to be hooked up to Sync.
  PrefModelAssociator(scoped_refptr<PrefModelAssociatorClient> client,
                      scoped_refptr<WriteablePrefStore> user_prefs,
                      syncer::DataType type);

  // The |client| is not owned and must outlive this object.
  // |user_prefs| is the PrefStore to be hooked up to Sync.
  // Note: This must be called iff EnablePreferencesAccountStorage feature is
  // enabled.
  PrefModelAssociator(
      scoped_refptr<PrefModelAssociatorClient> client,
      scoped_refptr<DualLayerUserPrefStore> dual_layer_user_prefs,
      syncer::DataType type);

  PrefModelAssociator(const PrefModelAssociator&) = delete;
  PrefModelAssociator& operator=(const PrefModelAssociator&) = delete;

  ~PrefModelAssociator() override;

  // Must be called before anything else.
  void SetPrefService(PrefServiceForAssociator* pref_service);

  // See description above field for details.
  bool models_associated() const { return models_associated_; }

  // Returns the mutable preference from |specifics| for a given model |type|.
  // Exposed for testing.
  static sync_pb::PreferenceSpecifics* GetMutableSpecifics(
      syncer::DataType type,
      sync_pb::EntitySpecifics* specifics);

  // syncer::SyncableService implementation.
  void WaitUntilReadyToSync(base::OnceClosure done) override;
  std::optional<syncer::ModelError> MergeDataAndStartSyncing(
      syncer::DataType type,
      const syncer::SyncDataList& initial_sync_data,
      std::unique_ptr<syncer::SyncChangeProcessor> sync_processor) override;
  void StopSyncing(syncer::DataType type) override;
  void OnBrowserShutdown(syncer::DataType type) override;
  std::optional<syncer::ModelError> ProcessSyncChanges(
      const base::Location& from_here,
      const syncer::SyncChangeList& change_list) override;
  base::WeakPtr<SyncableService> AsWeakPtr() override;

  // PrefStore::Observer implementation.
  void OnPrefValueChanged(std::string_view name) override;
  void OnInitializationCompleted(bool succeeded) override;

  // Register a preference with the specified name for syncing. We do not care
  // about the type at registration time, but when changes arrive from the
  // syncer, we check if they can be applied and if not drop them.
  // Note: This should only be called at profile startup time (before sync
  // begins).
  void RegisterPref(std::string_view name);

  // Fills |sync_data| with a sync representation of the preference data
  // provided.
  // Exposed for testing.
  bool CreatePrefSyncData(std::string_view name,
                          const base::Value& value,
                          syncer::SyncData* sync_data) const;

  // Returns true if the specified preference is registered for syncing.
  bool IsPrefRegistered(std::string_view name) const;

  // Adds a SyncedPrefObserver to watch for changes to a specific pref.
  void AddSyncedPrefObserver(const std::string& name,
                             SyncedPrefObserver* observer);

  // Removes a SyncedPrefObserver from a pref's list of observers.
  void RemoveSyncedPrefObserver(const std::string& name,
                                SyncedPrefObserver* observer);

  // Returns the PrefModelAssociatorClient for this object.
  scoped_refptr<PrefModelAssociatorClient> client() const { return client_; }

  // Returns true if the pref under the given name is pulled down from sync.
  // Note this does not refer to SYNCABLE_PREF.
  bool IsPrefSyncedForTesting(const std::string& name) const;

  bool IsUsingDualLayerUserPrefStoreForTesting() const;

 private:
  // Create an association for a given preference. If |sync_pref| is valid,
  // signifying that sync has data for this preference, we reconcile their data
  // with ours and append a new UPDATE SyncChange to |sync_changes|. If
  // sync_pref is not set, we append an ADD SyncChange to |sync_changes| with
  // the current preference data.
  // Note: We do not modify the sync data for preferences that are either
  // controlled by policy (are not user modifiable) or have their default value
  // (are not user controlled).
  void InitPrefAndAssociate(const syncer::SyncData& sync_pref,
                            std::string_view pref_name,
                            syncer::SyncChangeList* sync_changes);

  void NotifySyncedPrefObservers(std::string_view path, bool from_sync) const;

  // Sets |pref_name| to |new_value| and returns true if |new_value| has an
  // appropriate type for this preference. Otherwise returns false.
  bool SetPrefWithTypeCheck(std::string_view pref_name,
                            const base::Value& new_value);

  // Notifies the synced pref observers that the pref for the given |path| is
  // synced.
  void NotifyStartedSyncing(const std::string& path) const;

  void Stop(bool is_browser_shutdown);

  // The datatype that this associator is responsible for, either PREFERENCES or
  // PRIORITY_PREFERENCES or OS_PREFERENCES or OS_PRIORITY_PREFERENCES.
  const syncer::DataType type_;

  scoped_refptr<PrefModelAssociatorClient> client_;

  // The PrefStore we are syncing to.
  scoped_refptr<WriteablePrefStore> user_prefs_;
  // This is set if EnablePreferencesAccountStorage is enabled. This points to
  // the DualLayerUserPrefStore instance, if one exists, which shares the
  // ownership of `user_prefs_`.
  scoped_refptr<DualLayerUserPrefStore> dual_layer_user_prefs_;

  // The interface to the PrefService.
  raw_ptr<PrefServiceForAssociator> pref_service_ = nullptr;

  // Do we have an active association between the preferences and sync models?
  // Set when start syncing, reset in StopSyncing. While this is not set, we
  // ignore any local preference changes (when we start syncing we will look
  // up the most recent values anyways).
  bool models_associated_ = false;

  // Whether we're currently processing changes from the syncer. While this is
  // true, we ignore any local preference changes, since we triggered them.
  bool processing_syncer_changes_ = false;

  // All preferences that have registered as being syncable with this profile.
  std::set<std::string, std::less<>> registered_preferences_;

  // The preferences that are currently synced (excludes those preferences
  // that have never had sync data and currently have default values).
  // Note: As long as Sync remains enabled, this set never decreases, only grows
  // to eventually match `registered_preferences_` as more preferences are
  // synced. It determines whether a preference change should update an existing
  // sync node or create a new sync node.
  std::set<std::string, std::less<>> synced_preferences_;

  // Sync's handler for outgoing changes. Non-null between
  // MergeDataAndStartSyncing() and StopSyncing().
  std::unique_ptr<syncer::SyncChangeProcessor> sync_processor_;

  // Map prefs to lists of observers. Observers will receive notification when
  // a pref changes, including the detail of whether or not the change came
  // from sync.
  using SyncedPrefObserverList =
      base::ObserverList<SyncedPrefObserver>::Unchecked;
  TransparentUnorderedStringMap<std::unique_ptr<SyncedPrefObserverList>>
      synced_pref_observers_;

  SEQUENCE_CHECKER(sequence_checker_);

  base::WeakPtrFactory<PrefModelAssociator> weak_ptr_factory_{this};
};

}  // namespace sync_preferences

#endif  // COMPONENTS_SYNC_PREFERENCES_PREF_MODEL_ASSOCIATOR_H_