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