pref_registry.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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_PREFS_PREF_REGISTRY_H_
  5. #define COMPONENTS_PREFS_PREF_REGISTRY_H_
  6. #include <stdint.h>
  7. #include <set>
  8. #include <unordered_map>
  9. #include "base/memory/ref_counted.h"
  10. #include "components/prefs/pref_value_map.h"
  11. #include "components/prefs/prefs_export.h"
  12. namespace base {
  13. class Value;
  14. }
  15. class DefaultPrefStore;
  16. class PrefStore;
  17. // Preferences need to be registered with a type and default value
  18. // before they are used.
  19. //
  20. // The way you use a PrefRegistry is that you register all required
  21. // preferences on it (via one of its subclasses), then pass it as a
  22. // construction parameter to PrefService.
  23. //
  24. // Currently, registrations after constructing the PrefService will
  25. // also work, but this is being deprecated.
  26. class COMPONENTS_PREFS_EXPORT PrefRegistry
  27. : public base::RefCounted<PrefRegistry> {
  28. public:
  29. // Registration flags that can be specified which impact how the pref will
  30. // behave or be stored. This will be passed in a bitmask when the pref is
  31. // registered. Subclasses of PrefRegistry can specify their own flags. Care
  32. // must be taken to ensure none of these overlap with the flags below.
  33. using PrefRegistrationFlags = uint32_t;
  34. // No flags are specified.
  35. static constexpr PrefRegistrationFlags NO_REGISTRATION_FLAGS = 0;
  36. // The first 8 bits are reserved for subclasses of PrefRegistry to use.
  37. // This marks the pref as "lossy". There is no strict time guarantee on when
  38. // a lossy pref will be persisted to permanent storage when it is modified.
  39. static constexpr PrefRegistrationFlags LOSSY_PREF = 1 << 8;
  40. // Registering a pref as public allows other services to access it.
  41. static constexpr PrefRegistrationFlags PUBLIC = 1 << 9;
  42. typedef PrefValueMap::const_iterator const_iterator;
  43. typedef std::unordered_map<std::string, uint32_t> PrefRegistrationFlagsMap;
  44. PrefRegistry();
  45. PrefRegistry(const PrefRegistry&) = delete;
  46. PrefRegistry& operator=(const PrefRegistry&) = delete;
  47. // Retrieve the set of registration flags for the given preference. The return
  48. // value is a bitmask of PrefRegistrationFlags.
  49. uint32_t GetRegistrationFlags(const std::string& pref_name) const;
  50. // Gets the registered defaults.
  51. scoped_refptr<PrefStore> defaults();
  52. // Allows iteration over defaults.
  53. const_iterator begin() const;
  54. const_iterator end() const;
  55. // Changes the default value for a preference.
  56. //
  57. // |pref_name| must be a previously registered preference.
  58. void SetDefaultPrefValue(const std::string& pref_name, base::Value value);
  59. // Registers a pref owned by another service for use with the current service.
  60. // The owning service must register that pref with the |PUBLIC| flag.
  61. void RegisterForeignPref(const std::string& path);
  62. // Sets the default value and flags of a previously-registered foreign pref
  63. // value.
  64. void SetDefaultForeignPrefValue(const std::string& path,
  65. base::Value default_value,
  66. uint32_t flags);
  67. const std::set<std::string>& foreign_pref_keys() const {
  68. return foreign_pref_keys_;
  69. }
  70. protected:
  71. friend class base::RefCounted<PrefRegistry>;
  72. virtual ~PrefRegistry();
  73. // Used by subclasses to register a default value and registration flags for
  74. // a preference. |flags| is a bitmask of |PrefRegistrationFlags|.
  75. void RegisterPreference(const std::string& path,
  76. base::Value default_value,
  77. uint32_t flags);
  78. // Allows subclasses to hook into pref registration.
  79. virtual void OnPrefRegistered(const std::string& path,
  80. uint32_t flags);
  81. scoped_refptr<DefaultPrefStore> defaults_;
  82. // A map of pref name to a bitmask of PrefRegistrationFlags.
  83. PrefRegistrationFlagsMap registration_flags_;
  84. std::set<std::string> foreign_pref_keys_;
  85. };
  86. #endif // COMPONENTS_PREFS_PREF_REGISTRY_H_