pref_registry_syncable.h 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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_PREF_REGISTRY_PREF_REGISTRY_SYNCABLE_H_
  5. #define COMPONENTS_PREF_REGISTRY_PREF_REGISTRY_SYNCABLE_H_
  6. #include <stdint.h>
  7. #include <string>
  8. #include "base/callback.h"
  9. #include "build/chromeos_buildflags.h"
  10. #include "components/prefs/pref_registry_simple.h"
  11. // TODO(tfarina): Change this namespace to pref_registry.
  12. namespace user_prefs {
  13. // A PrefRegistry for syncable prefs.
  14. //
  15. // Classes or components that want to register such preferences should
  16. // define a static function named RegisterUserPrefs that takes a
  17. // PrefRegistrySyncable*, and the top-level application using the
  18. // class or embedding the component should call this function at an
  19. // appropriate time before the PrefService for these preferences is
  20. // constructed. See e.g. chrome/browser/prefs/browser_prefs.cc which
  21. // does this for Chrome.
  22. //
  23. // TODO(raymes): This class only exists to support SyncableRegistrationCallback
  24. // logic which is only required to support pref registration after the
  25. // PrefService has been created which is only used by tests. We can remove this
  26. // entire class and those tests with some work.
  27. class PrefRegistrySyncable : public PrefRegistrySimple {
  28. public:
  29. // Enum of flags used when registering preferences to determine if it should
  30. // be synced or not. These flags are mutually exclusive, only one of them
  31. // should ever be specified.
  32. //
  33. // Note: These must NOT overlap with PrefRegistry::PrefRegistrationFlags.
  34. enum PrefRegistrationFlags : uint32_t {
  35. // The pref will be synced.
  36. SYNCABLE_PREF = 1 << 0,
  37. // The pref will be synced. The pref will never be encrypted and will be
  38. // synced before other datatypes.
  39. // Because they're never encrypted:
  40. // -- they can be synced down on first sync before the user is prompted for
  41. // a passphrase.
  42. // -- they are preferred for receiving server-provided data.
  43. SYNCABLE_PRIORITY_PREF = 1 << 1,
  44. #if BUILDFLAG(IS_CHROMEOS_ASH)
  45. // As above, but the pref is for an OS settings (e.g. keyboard layout).
  46. // This distinction allows OS pref sync to be controlled independently from
  47. // browser pref sync in the UI.
  48. SYNCABLE_OS_PREF = 1 << 2,
  49. SYNCABLE_OS_PRIORITY_PREF = 1 << 3,
  50. #endif
  51. };
  52. using SyncableRegistrationCallback =
  53. base::RepeatingCallback<void(const std::string& path, uint32_t flags)>;
  54. PrefRegistrySyncable();
  55. PrefRegistrySyncable(const PrefRegistrySyncable&) = delete;
  56. PrefRegistrySyncable& operator=(const PrefRegistrySyncable&) = delete;
  57. // Exactly one callback can be set for the event of a syncable
  58. // preference being registered. It will be fired after the
  59. // registration has occurred.
  60. //
  61. // Calling this method after a callback has already been set will
  62. // make the object forget the previous callback and use the new one
  63. // instead.
  64. void SetSyncableRegistrationCallback(SyncableRegistrationCallback cb);
  65. // Returns a new PrefRegistrySyncable that uses the same defaults
  66. // store.
  67. scoped_refptr<PrefRegistrySyncable> ForkForIncognito();
  68. private:
  69. ~PrefRegistrySyncable() override;
  70. // PrefRegistrySimple overrides.
  71. void OnPrefRegistered(const std::string& path,
  72. uint32_t flags) override;
  73. SyncableRegistrationCallback callback_;
  74. };
  75. } // namespace user_prefs
  76. #endif // COMPONENTS_PREF_REGISTRY_PREF_REGISTRY_SYNCABLE_H_