1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_SYNC_PREFERENCES_PREF_SERVICE_SYNCABLE_H_
6 #define COMPONENTS_SYNC_PREFERENCES_PREF_SERVICE_SYNCABLE_H_
14 #include "base/callback_forward.h"
15 #include "base/observer_list.h"
16 #include "build/chromeos_buildflags.h"
17 #include "components/pref_registry/pref_registry_syncable.h"
18 #include "components/prefs/pref_service.h"
19 #include "components/sync_preferences/pref_model_associator.h"
24 class SyncableService;
27 namespace sync_preferences {
29 class PrefModelAssociatorClient;
30 class PrefServiceSyncableObserver;
31 class SyncedPrefObserver;
33 // A PrefService that can be synced. Users are forced to declare
34 // whether preferences are syncable or not when registering them to
36 class PrefServiceSyncable : public PrefService {
38 // You may wish to use PrefServiceFactory or one of its subclasses
39 // for simplified construction.
41 std::unique_ptr<PrefNotifierImpl> pref_notifier,
42 std::unique_ptr<PrefValueStore> pref_value_store,
43 scoped_refptr<PersistentPrefStore> user_prefs,
44 scoped_refptr<PersistentPrefStore> standalone_browser_prefs,
45 scoped_refptr<user_prefs::PrefRegistrySyncable> pref_registry,
46 const PrefModelAssociatorClient* pref_model_associator_client,
47 base::RepeatingCallback<void(PersistentPrefStore::PrefReadError)>
51 PrefServiceSyncable(const PrefServiceSyncable&) = delete;
52 PrefServiceSyncable& operator=(const PrefServiceSyncable&) = delete;
54 ~PrefServiceSyncable() override;
56 // Creates an incognito copy of the pref service that shares most pref stores
57 // but uses a fresh non-persistent overlay for the user pref store and an
58 // individual extension pref store (to cache the effective extension prefs for
59 // incognito windows). |persistent_pref_names| is a list of preference names
60 // whose changes will be persisted by the returned incognito pref service.
61 std::unique_ptr<PrefServiceSyncable> CreateIncognitoPrefService(
62 PrefStore* incognito_extension_pref_store,
63 const std::vector<const char*>& persistent_pref_names);
65 // Returns true if preferences state has synchronized with the remote
66 // preferences. If true is returned it can be assumed the local preferences
67 // has applied changes from the remote preferences. The two may not be
68 // identical if a change is in flight (from either side).
70 // TODO(albertb): Given that we now support priority preferences, callers of
71 // this method are likely better off making the preferences they care about
72 // into priority preferences and calling IsPrioritySyncing().
75 // Returns true if priority preferences state has synchronized with the remote
76 // priority preferences.
77 bool IsPrioritySyncing();
79 #if BUILDFLAG(IS_CHROMEOS_ASH)
80 // As above, but for OS preferences.
81 bool AreOsPrefsSyncing();
83 // As above, but for OS priority preferences.
84 bool AreOsPriorityPrefsSyncing();
87 void AddObserver(PrefServiceSyncableObserver* observer);
88 void RemoveObserver(PrefServiceSyncableObserver* observer);
90 syncer::SyncableService* GetSyncableService(const syncer::ModelType& type);
92 // Do not call this after having derived an incognito or per tab pref service.
93 void UpdateCommandLinePrefStore(PrefStore* cmd_line_store) override;
95 void AddSyncedPrefObserver(const std::string& name,
96 SyncedPrefObserver* observer);
97 void RemoveSyncedPrefObserver(const std::string& name,
98 SyncedPrefObserver* observer);
101 friend class PrefModelAssociator;
103 void AddRegisteredSyncablePreference(const std::string& path, uint32_t flags);
105 // Invoked internally when the syncing state changes for a type of pref.
106 void OnIsSyncingChanged();
108 // Process a local preference change. This can trigger new SyncChanges being
109 // sent to the syncer.
110 void ProcessPrefChange(const std::string& name);
112 // Whether CreateIncognitoPrefService() has been called to create a
113 // "forked" PrefService.
114 bool pref_service_forked_;
116 PrefModelAssociator pref_sync_associator_;
117 PrefModelAssociator priority_pref_sync_associator_;
119 #if BUILDFLAG(IS_CHROMEOS_ASH)
120 // Associators for Chrome OS system preferences.
121 PrefModelAssociator os_pref_sync_associator_;
122 PrefModelAssociator os_priority_pref_sync_associator_;
125 const scoped_refptr<user_prefs::PrefRegistrySyncable> pref_registry_;
127 base::ObserverList<PrefServiceSyncableObserver>::Unchecked observer_list_;
130 } // namespace sync_preferences
132 #endif // COMPONENTS_SYNC_PREFERENCES_PREF_SERVICE_SYNCABLE_H_