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.
5 #ifndef BASE_PREFS_PREF_REGISTRY_H_
6 #define BASE_PREFS_PREF_REGISTRY_H_
8 #include "base/containers/hash_tables.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/prefs/base_prefs_export.h"
11 #include "base/prefs/pref_value_map.h"
17 class DefaultPrefStore
;
20 // Preferences need to be registered with a type and default value
21 // before they are used.
23 // The way you use a PrefRegistry is that you register all required
24 // preferences on it (via one of its subclasses), then pass it as a
25 // construction parameter to PrefService.
27 // Currently, registrations after constructing the PrefService will
28 // also work, but this is being deprecated.
29 class BASE_PREFS_EXPORT PrefRegistry
: public base::RefCounted
<PrefRegistry
> {
31 // Registration flags that can be specified which impact how the pref will
32 // behave or be stored. This will be passed in a bitmask when the pref is
33 // registered. Subclasses of PrefRegistry can specify their own flags. Care
34 // must be taken to ensure none of these overlap with the flags below.
35 enum PrefRegistrationFlags
: uint32
{
36 // No flags are specified.
37 NO_REGISTRATION_FLAGS
= 0,
39 // The first 8 bits are reserved for subclasses of PrefRegistry to use.
41 // This marks the pref as "lossy". There is no strict time guarantee on when
42 // a lossy pref will be persisted to permanent storage when it is modified.
46 typedef PrefValueMap::const_iterator const_iterator
;
47 typedef base::hash_map
<std::string
, uint32
> PrefRegistrationFlagsMap
;
51 // Retrieve the set of registration flags for the given preference. The return
52 // value is a bitmask of PrefRegistrationFlags.
53 uint32
GetRegistrationFlags(const std::string
& pref_name
) const;
55 // Gets the registered defaults.
56 scoped_refptr
<PrefStore
> defaults();
58 // Allows iteration over defaults.
59 const_iterator
begin() const;
60 const_iterator
end() const;
62 // Changes the default value for a preference. Takes ownership of |value|.
64 // |pref_name| must be a previously registered preference.
65 void SetDefaultPrefValue(const std::string
& pref_name
, base::Value
* value
);
68 friend class base::RefCounted
<PrefRegistry
>;
69 virtual ~PrefRegistry();
71 // Used by subclasses to register a default value and registration flags for
72 // a preference. |flags| is a bitmask of |PrefRegistrationFlags|.
73 void RegisterPreference(const std::string
& path
,
74 base::Value
* default_value
,
77 scoped_refptr
<DefaultPrefStore
> defaults_
;
79 // A map of pref name to a bitmask of PrefRegistrationFlags.
80 PrefRegistrationFlagsMap registration_flags_
;
83 DISALLOW_COPY_AND_ASSIGN(PrefRegistry
);
86 #endif // BASE_PREFS_PREF_REGISTRY_H_