pref_service_syncable.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. #ifndef COMPONENTS_SYNC_PREFERENCES_PREF_SERVICE_SYNCABLE_H_
  5. #define COMPONENTS_SYNC_PREFERENCES_PREF_SERVICE_SYNCABLE_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "base/callback_forward.h"
  11. #include "base/observer_list.h"
  12. #include "build/chromeos_buildflags.h"
  13. #include "components/pref_registry/pref_registry_syncable.h"
  14. #include "components/prefs/pref_service.h"
  15. #include "components/sync_preferences/pref_model_associator.h"
  16. class PrefValueStore;
  17. namespace syncer {
  18. class SyncableService;
  19. }
  20. namespace sync_preferences {
  21. class PrefModelAssociatorClient;
  22. class PrefServiceSyncableObserver;
  23. class SyncedPrefObserver;
  24. // A PrefService that can be synced. Users are forced to declare
  25. // whether preferences are syncable or not when registering them to
  26. // this PrefService.
  27. class PrefServiceSyncable : public PrefService {
  28. public:
  29. // You may wish to use PrefServiceFactory or one of its subclasses
  30. // for simplified construction.
  31. PrefServiceSyncable(
  32. std::unique_ptr<PrefNotifierImpl> pref_notifier,
  33. std::unique_ptr<PrefValueStore> pref_value_store,
  34. scoped_refptr<PersistentPrefStore> user_prefs,
  35. scoped_refptr<PersistentPrefStore> standalone_browser_prefs,
  36. scoped_refptr<user_prefs::PrefRegistrySyncable> pref_registry,
  37. const PrefModelAssociatorClient* pref_model_associator_client,
  38. base::RepeatingCallback<void(PersistentPrefStore::PrefReadError)>
  39. read_error_callback,
  40. bool async);
  41. PrefServiceSyncable(const PrefServiceSyncable&) = delete;
  42. PrefServiceSyncable& operator=(const PrefServiceSyncable&) = delete;
  43. ~PrefServiceSyncable() override;
  44. // Creates an incognito copy of the pref service that shares most pref stores
  45. // but uses a fresh non-persistent overlay for the user pref store and an
  46. // individual extension pref store (to cache the effective extension prefs for
  47. // incognito windows). |persistent_pref_names| is a list of preference names
  48. // whose changes will be persisted by the returned incognito pref service.
  49. std::unique_ptr<PrefServiceSyncable> CreateIncognitoPrefService(
  50. PrefStore* incognito_extension_pref_store,
  51. const std::vector<const char*>& persistent_pref_names);
  52. // Returns true if preferences state has synchronized with the remote
  53. // preferences. If true is returned it can be assumed the local preferences
  54. // has applied changes from the remote preferences. The two may not be
  55. // identical if a change is in flight (from either side).
  56. //
  57. // TODO(albertb): Given that we now support priority preferences, callers of
  58. // this method are likely better off making the preferences they care about
  59. // into priority preferences and calling IsPrioritySyncing().
  60. bool IsSyncing();
  61. // Returns true if priority preferences state has synchronized with the remote
  62. // priority preferences.
  63. bool IsPrioritySyncing();
  64. #if BUILDFLAG(IS_CHROMEOS_ASH)
  65. // As above, but for OS preferences.
  66. bool AreOsPrefsSyncing();
  67. // As above, but for OS priority preferences.
  68. bool AreOsPriorityPrefsSyncing();
  69. #endif
  70. void AddObserver(PrefServiceSyncableObserver* observer);
  71. void RemoveObserver(PrefServiceSyncableObserver* observer);
  72. syncer::SyncableService* GetSyncableService(const syncer::ModelType& type);
  73. // Do not call this after having derived an incognito or per tab pref service.
  74. void UpdateCommandLinePrefStore(PrefStore* cmd_line_store) override;
  75. void AddSyncedPrefObserver(const std::string& name,
  76. SyncedPrefObserver* observer);
  77. void RemoveSyncedPrefObserver(const std::string& name,
  78. SyncedPrefObserver* observer);
  79. private:
  80. friend class PrefModelAssociator;
  81. void AddRegisteredSyncablePreference(const std::string& path, uint32_t flags);
  82. // Invoked internally when the syncing state changes for a type of pref.
  83. void OnIsSyncingChanged();
  84. // Process a local preference change. This can trigger new SyncChanges being
  85. // sent to the syncer.
  86. void ProcessPrefChange(const std::string& name);
  87. // Whether CreateIncognitoPrefService() has been called to create a
  88. // "forked" PrefService.
  89. bool pref_service_forked_;
  90. PrefModelAssociator pref_sync_associator_;
  91. PrefModelAssociator priority_pref_sync_associator_;
  92. #if BUILDFLAG(IS_CHROMEOS_ASH)
  93. // Associators for Chrome OS system preferences.
  94. PrefModelAssociator os_pref_sync_associator_;
  95. PrefModelAssociator os_priority_pref_sync_associator_;
  96. #endif
  97. const scoped_refptr<user_prefs::PrefRegistrySyncable> pref_registry_;
  98. base::ObserverList<PrefServiceSyncableObserver>::Unchecked observer_list_;
  99. };
  100. } // namespace sync_preferences
  101. #endif // COMPONENTS_SYNC_PREFERENCES_PREF_SERVICE_SYNCABLE_H_