[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 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. | ||||
4 | |||||
[email protected] | 3853a4c | 2013-02-11 17:15:57 | [diff] [blame] | 5 | #ifndef BASE_PREFS_PREF_REGISTRY_H_ |
6 | #define BASE_PREFS_PREF_REGISTRY_H_ | ||||
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 7 | |
raymes | 51b41a6 | 2015-04-24 02:45:04 | [diff] [blame] | 8 | #include "base/containers/hash_tables.h" |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 9 | #include "base/memory/ref_counted.h" |
[email protected] | 3853a4c | 2013-02-11 17:15:57 | [diff] [blame] | 10 | #include "base/prefs/base_prefs_export.h" |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 11 | #include "base/prefs/pref_value_map.h" |
12 | |||||
13 | namespace base { | ||||
14 | class Value; | ||||
15 | } | ||||
16 | |||||
17 | class DefaultPrefStore; | ||||
18 | class PrefStore; | ||||
19 | |||||
20 | // Preferences need to be registered with a type and default value | ||||
21 | // before they are used. | ||||
22 | // | ||||
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. | ||||
26 | // | ||||
27 | // Currently, registrations after constructing the PrefService will | ||||
28 | // also work, but this is being deprecated. | ||||
[email protected] | 3853a4c | 2013-02-11 17:15:57 | [diff] [blame] | 29 | class BASE_PREFS_EXPORT PrefRegistry : public base::RefCounted<PrefRegistry> { |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 30 | public: |
raymes | 51b41a6 | 2015-04-24 02:45:04 | [diff] [blame] | 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. | ||||
raymes | 76de1af | 2015-05-06 03:22:21 | [diff] [blame] | 35 | enum PrefRegistrationFlags : uint32 { |
raymes | 51b41a6 | 2015-04-24 02:45:04 | [diff] [blame] | 36 | // No flags are specified. |
37 | NO_REGISTRATION_FLAGS = 0, | ||||
38 | |||||
39 | // The first 8 bits are reserved for subclasses of PrefRegistry to use. | ||||
raymes | 76de1af | 2015-05-06 03:22:21 | [diff] [blame] | 40 | |
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. | ||||
43 | LOSSY_PREF = 1 << 8, | ||||
raymes | 51b41a6 | 2015-04-24 02:45:04 | [diff] [blame] | 44 | }; |
45 | |||||
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 46 | typedef PrefValueMap::const_iterator const_iterator; |
raymes | 51b41a6 | 2015-04-24 02:45:04 | [diff] [blame] | 47 | typedef base::hash_map<std::string, uint32> PrefRegistrationFlagsMap; |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 48 | |
49 | PrefRegistry(); | ||||
50 | |||||
raymes | 51b41a6 | 2015-04-24 02:45:04 | [diff] [blame] | 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; | ||||
54 | |||||
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 55 | // Gets the registered defaults. |
56 | scoped_refptr<PrefStore> defaults(); | ||||
57 | |||||
58 | // Allows iteration over defaults. | ||||
59 | const_iterator begin() const; | ||||
60 | const_iterator end() const; | ||||
61 | |||||
[email protected] | 5879cef | 2013-03-02 17:02:25 | [diff] [blame] | 62 | // Changes the default value for a preference. Takes ownership of |value|. |
63 | // | ||||
64 | // |pref_name| must be a previously registered preference. | ||||
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 65 | void SetDefaultPrefValue(const std::string& pref_name, base::Value* value); |
[email protected] | 5879cef | 2013-03-02 17:02:25 | [diff] [blame] | 66 | |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 67 | protected: |
68 | friend class base::RefCounted<PrefRegistry>; | ||||
69 | virtual ~PrefRegistry(); | ||||
70 | |||||
raymes | 51b41a6 | 2015-04-24 02:45:04 | [diff] [blame] | 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, | ||||
75 | uint32 flags); | ||||
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 76 | |
[email protected] | c753f14 | 2013-02-10 13:14:04 | [diff] [blame] | 77 | scoped_refptr<DefaultPrefStore> defaults_; |
78 | |||||
raymes | 51b41a6 | 2015-04-24 02:45:04 | [diff] [blame] | 79 | // A map of pref name to a bitmask of PrefRegistrationFlags. |
80 | PrefRegistrationFlagsMap registration_flags_; | ||||
81 | |||||
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 82 | private: |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 83 | DISALLOW_COPY_AND_ASSIGN(PrefRegistry); |
84 | }; | ||||
85 | |||||
[email protected] | 3853a4c | 2013-02-11 17:15:57 | [diff] [blame] | 86 | #endif // BASE_PREFS_PREF_REGISTRY_H_ |