sync_prefs.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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_BASE_SYNC_PREFS_H_
  5. #define COMPONENTS_SYNC_BASE_SYNC_PREFS_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <string>
  9. #include "base/compiler_specific.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/observer_list.h"
  12. #include "base/sequence_checker.h"
  13. #include "build/build_config.h"
  14. #include "build/chromeos_buildflags.h"
  15. #include "components/prefs/pref_member.h"
  16. #include "components/sync/base/user_selectable_type.h"
  17. class PrefRegistrySimple;
  18. class PrefService;
  19. namespace syncer {
  20. class SyncPrefObserver {
  21. public:
  22. virtual void OnSyncManagedPrefChange(bool is_sync_managed) = 0;
  23. virtual void OnFirstSetupCompletePrefChange(bool is_first_setup_complete) = 0;
  24. virtual void OnSyncRequestedPrefChange(bool is_sync_requested) = 0;
  25. virtual void OnPreferredDataTypesPrefChange() = 0;
  26. protected:
  27. virtual ~SyncPrefObserver();
  28. };
  29. // SyncPrefs is a helper class that manages getting, setting, and persisting
  30. // global sync preferences. It is not thread-safe, and lives on the UI thread.
  31. class SyncPrefs {
  32. public:
  33. // |pref_service| must not be null and must outlive this object.
  34. explicit SyncPrefs(PrefService* pref_service);
  35. SyncPrefs(const SyncPrefs&) = delete;
  36. SyncPrefs& operator=(const SyncPrefs&) = delete;
  37. ~SyncPrefs();
  38. static void RegisterProfilePrefs(PrefRegistrySimple* registry);
  39. void AddSyncPrefObserver(SyncPrefObserver* sync_pref_observer);
  40. void RemoveSyncPrefObserver(SyncPrefObserver* sync_pref_observer);
  41. // Getters and setters for global sync prefs.
  42. // First-Setup-Complete is conceptually similar to the user's consent to
  43. // enable sync-the-feature.
  44. bool IsFirstSetupComplete() const;
  45. void SetFirstSetupComplete();
  46. void ClearFirstSetupComplete();
  47. bool IsSyncRequested() const;
  48. void SetSyncRequested(bool is_requested);
  49. void SetSyncRequestedIfNotSetExplicitly();
  50. bool HasKeepEverythingSynced() const;
  51. // Returns UserSelectableTypeSet::All() if HasKeepEverythingSynced() is true.
  52. UserSelectableTypeSet GetSelectedTypes() const;
  53. // Sets the selection state for all |registered_types| and "keep everything
  54. // synced" flag.
  55. // |keep_everything_synced| indicates that all current and future types
  56. // should be synced. If this is set to true, then GetSelectedTypes() will
  57. // always return UserSelectableTypeSet::All(), even if not all of them are
  58. // registered or individually marked as selected.
  59. // Changes are still made to the individual selectable type prefs even if
  60. // |keep_everything_synced| is true, but won't be visible until it's set to
  61. // false.
  62. void SetSelectedTypes(bool keep_everything_synced,
  63. UserSelectableTypeSet registered_types,
  64. UserSelectableTypeSet selected_types);
  65. #if BUILDFLAG(IS_CHROMEOS_ASH)
  66. // Chrome OS provides a separate settings UI surface for sync of OS types,
  67. // including a separate "Sync All" toggle for OS types.
  68. bool IsSyncAllOsTypesEnabled() const;
  69. UserSelectableOsTypeSet GetSelectedOsTypes() const;
  70. void SetSelectedOsTypes(bool sync_all_os_types,
  71. UserSelectableOsTypeSet registered_types,
  72. UserSelectableOsTypeSet selected_types);
  73. // Maps |type| to its corresponding preference name. Returns nullptr if |type|
  74. // isn't an OS type.
  75. static const char* GetPrefNameForOsType(UserSelectableOsType type);
  76. #endif
  77. #if BUILDFLAG(IS_CHROMEOS_LACROS)
  78. bool IsAppsSyncEnabledByOs() const;
  79. void SetAppsSyncEnabledByOs(bool apps_sync_enabled);
  80. #endif
  81. // Whether Sync is disabled on the client for all profiles and accounts.
  82. bool IsSyncClientDisabledByPolicy() const;
  83. // Maps |type| to its corresponding preference name.
  84. static const char* GetPrefNameForType(UserSelectableType type);
  85. // Gets the local sync backend enabled state.
  86. bool IsLocalSyncEnabled() const;
  87. // The encryption bootstrap token is used for explicit passphrase users
  88. // (usually custom passphrase) and represents a user-entered passphrase.
  89. std::string GetEncryptionBootstrapToken() const;
  90. void SetEncryptionBootstrapToken(const std::string& token);
  91. void ClearEncryptionBootstrapToken();
  92. // Muting mechanism for passphrase prompts, used on Android.
  93. int GetPassphrasePromptMutedProductVersion() const;
  94. void SetPassphrasePromptMutedProductVersion(int major_version);
  95. void ClearPassphrasePromptMutedProductVersion();
  96. private:
  97. static void RegisterTypeSelectedPref(PrefRegistrySimple* prefs,
  98. UserSelectableType type);
  99. void OnSyncManagedPrefChanged();
  100. void OnFirstSetupCompletePrefChange();
  101. void OnSyncRequestedPrefChange();
  102. // Never null.
  103. const raw_ptr<PrefService> pref_service_;
  104. base::ObserverList<SyncPrefObserver>::Unchecked sync_pref_observers_;
  105. // The preference that controls whether sync is under control by
  106. // configuration management.
  107. BooleanPrefMember pref_sync_managed_;
  108. BooleanPrefMember pref_first_setup_complete_;
  109. BooleanPrefMember pref_sync_requested_;
  110. bool local_sync_enabled_;
  111. SEQUENCE_CHECKER(sequence_checker_);
  112. };
  113. #if BUILDFLAG(IS_ANDROID)
  114. void ClearObsoleteSyncDecoupledFromAndroidMasterSync(PrefService* pref_service);
  115. #endif // BUILDFLAG(IS_ANDROID)
  116. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
  117. void MigrateSyncRequestedPrefPostMice(PrefService* pref_service);
  118. #endif // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
  119. } // namespace syncer
  120. #endif // COMPONENTS_SYNC_BASE_SYNC_PREFS_H_