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.
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/macros.h"
16 #include "components/pref_registry/pref_registry_syncable.h"
17 #include "components/prefs/pref_service.h"
18 #include "components/prefs/pref_value_store.h"
19 #include "components/sync_preferences/pref_model_associator.h"
20 #include "components/sync_preferences/synced_pref_observer.h"
21 #include "components/sync_preferences/unknown_user_pref_accessor.h"
24 class SyncableService;
27 namespace sync_preferences {
29 class PrefModelAssociatorClient;
30 class PrefServiceSyncableObserver;
32 // A PrefService that can be synced. Users are forced to declare
33 // whether preferences are syncable or not when registering them to
35 class PrefServiceSyncable : public PrefService {
37 // You may wish to use PrefServiceFactory or one of its subclasses
38 // for simplified construction.
40 std::unique_ptr<PrefNotifierImpl> pref_notifier,
41 std::unique_ptr<PrefValueStore> pref_value_store,
42 scoped_refptr<PersistentPrefStore> user_prefs,
43 scoped_refptr<user_prefs::PrefRegistrySyncable> pref_registry,
44 const PrefModelAssociatorClient* pref_model_associator_client,
45 base::RepeatingCallback<void(PersistentPrefStore::PrefReadError)>
48 ~PrefServiceSyncable() override;
50 // Creates an incognito copy of the pref service that shares most pref stores
51 // but uses a fresh non-persistent overlay for the user pref store and an
52 // individual extension pref store (to cache the effective extension prefs for
53 // incognito windows). |persistent_pref_names| is a list of preference names
54 // whose changes will be persisted by the returned incognito pref service.
55 std::unique_ptr<PrefServiceSyncable> CreateIncognitoPrefService(
56 PrefStore* incognito_extension_pref_store,
57 const std::vector<const char*>& persistent_pref_names,
58 std::unique_ptr<PrefValueStore::Delegate> delegate);
60 // Returns true if preferences state has synchronized with the remote
61 // preferences. If true is returned it can be assumed the local preferences
62 // has applied changes from the remote preferences. The two may not be
63 // identical if a change is in flight (from either side).
65 // TODO(albertb): Given that we now support priority preferences, callers of
66 // this method are likely better off making the preferences they care about
67 // into priority preferences and calling IsPrioritySyncing().
70 // Returns true if priority preferences state has synchronized with the remote
71 // priority preferences.
72 bool IsPrioritySyncing();
74 // Returns true if the pref under the given name is pulled down from sync.
75 // Note this does not refer to SYNCABLE_PREF.
76 bool IsPrefSynced(const std::string& name) const;
78 void AddObserver(PrefServiceSyncableObserver* observer);
79 void RemoveObserver(PrefServiceSyncableObserver* observer);
81 void RegisterMergeDataFinishedCallback(const base::Closure& callback);
83 // TODO(zea): Have PrefServiceSyncable implement
84 // syncer::SyncableService directly.
85 syncer::SyncableService* GetSyncableService(const syncer::ModelType& type);
87 // Do not call this after having derived an incognito or per tab pref service.
88 void UpdateCommandLinePrefStore(PrefStore* cmd_line_store) override;
90 void AddSyncedPrefObserver(const std::string& name,
91 SyncedPrefObserver* observer);
92 void RemoveSyncedPrefObserver(const std::string& name,
93 SyncedPrefObserver* observer);
96 friend class PrefModelAssociator;
98 void AddRegisteredSyncablePreference(const std::string& path, uint32_t flags);
100 // Invoked internally when the IsSyncing() state changes.
101 void OnIsSyncingChanged();
103 // Process a local preference change. This can trigger new SyncChanges being
104 // sent to the syncer.
105 void ProcessPrefChange(const std::string& name);
107 // Whether CreateIncognitoPrefService() has been called to create a
108 // "forked" PrefService.
109 bool pref_service_forked_;
111 UnknownUserPrefAccessor unknown_pref_accessor_;
112 PrefModelAssociator pref_sync_associator_;
113 PrefModelAssociator priority_pref_sync_associator_;
114 const scoped_refptr<user_prefs::PrefRegistrySyncable> pref_registry_;
116 base::ObserverList<PrefServiceSyncableObserver>::Unchecked observer_list_;
118 DISALLOW_COPY_AND_ASSIGN(PrefServiceSyncable);
121 } // namespace sync_preferences
123 #endif // COMPONENTS_SYNC_PREFERENCES_PREF_SERVICE_SYNCABLE_H_