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.
5 #ifndef COMPONENTS_PREFS_PREF_REGISTRY_H_
6 #define COMPONENTS_PREFS_PREF_REGISTRY_H_
11 #include <unordered_map>
13 #include "base/memory/ref_counted.h"
14 #include "components/prefs/pref_value_map.h"
15 #include "components/prefs/prefs_export.h"
21 class DefaultPrefStore;
24 // Preferences need to be registered with a type and default value
25 // before they are used.
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.
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> {
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;
42 // No flags are specified.
43 static constexpr PrefRegistrationFlags NO_REGISTRATION_FLAGS = 0;
45 // The first 8 bits are reserved for subclasses of PrefRegistry to use.
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;
51 // Registering a pref as public allows other services to access it.
52 static constexpr PrefRegistrationFlags PUBLIC = 1 << 9;
54 typedef PrefValueMap::const_iterator const_iterator;
55 typedef std::unordered_map<std::string, uint32_t> PrefRegistrationFlagsMap;
59 PrefRegistry(const PrefRegistry&) = delete;
60 PrefRegistry& operator=(const PrefRegistry&) = delete;
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;
66 // Gets the registered defaults.
67 scoped_refptr<PrefStore> defaults();
69 // Allows iteration over defaults.
70 const_iterator begin() const;
71 const_iterator end() const;
73 // Changes the default value for a preference.
75 // |pref_name| must be a previously registered preference.
76 void SetDefaultPrefValue(const std::string& pref_name, base::Value value);
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);
82 // Sets the default value and flags of a previously-registered foreign pref
84 void SetDefaultForeignPrefValue(const std::string& path,
85 base::Value default_value,
88 const std::set<std::string>& foreign_pref_keys() const {
89 return foreign_pref_keys_;
93 friend class base::RefCounted<PrefRegistry>;
94 virtual ~PrefRegistry();
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,
102 // Allows subclasses to hook into pref registration.
103 virtual void OnPrefRegistered(const std::string& path,
106 scoped_refptr<DefaultPrefStore> defaults_;
108 // A map of pref name to a bitmask of PrefRegistrationFlags.
109 PrefRegistrationFlagsMap registration_flags_;
111 std::set<std::string> foreign_pref_keys_;
114 #endif // COMPONENTS_PREFS_PREF_REGISTRY_H_