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
{
36 // No flags are specified.
37 NO_REGISTRATION_FLAGS
= 0,
39 // The first 8 bits are reserved for subclasses of PrefRegistry to use.
42 typedef PrefValueMap::const_iterator const_iterator
;
43 typedef base::hash_map
<std::string
, uint32
> PrefRegistrationFlagsMap
;
47 // Retrieve the set of registration flags for the given preference. The return
48 // value is a bitmask of PrefRegistrationFlags.
49 uint32
GetRegistrationFlags(const std::string
& pref_name
) const;
51 // Gets the registered defaults.
52 scoped_refptr
<PrefStore
> defaults();
54 // Allows iteration over defaults.
55 const_iterator
begin() const;
56 const_iterator
end() const;
58 // Changes the default value for a preference. Takes ownership of |value|.
60 // |pref_name| must be a previously registered preference.
61 void SetDefaultPrefValue(const std::string
& pref_name
, base::Value
* value
);
64 friend class base::RefCounted
<PrefRegistry
>;
65 virtual ~PrefRegistry();
67 // Used by subclasses to register a default value and registration flags for
68 // a preference. |flags| is a bitmask of |PrefRegistrationFlags|.
69 void RegisterPreference(const std::string
& path
,
70 base::Value
* default_value
,
73 scoped_refptr
<DefaultPrefStore
> defaults_
;
75 // A map of pref name to a bitmask of PrefRegistrationFlags.
76 PrefRegistrationFlagsMap registration_flags_
;
79 DISALLOW_COPY_AND_ASSIGN(PrefRegistry
);
82 #endif // BASE_PREFS_PREF_REGISTRY_H_