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