OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_PREFS_PREF_REGISTRY_H_ | 5 #ifndef BASE_PREFS_PREF_REGISTRY_H_ |
6 #define BASE_PREFS_PREF_REGISTRY_H_ | 6 #define BASE_PREFS_PREF_REGISTRY_H_ |
7 | 7 |
| 8 #include <stdint.h> |
| 9 |
8 #include "base/containers/hash_tables.h" | 10 #include "base/containers/hash_tables.h" |
| 11 #include "base/macros.h" |
9 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
10 #include "base/prefs/base_prefs_export.h" | 13 #include "base/prefs/base_prefs_export.h" |
11 #include "base/prefs/pref_value_map.h" | 14 #include "base/prefs/pref_value_map.h" |
12 | 15 |
13 namespace base { | 16 namespace base { |
14 class Value; | 17 class Value; |
15 } | 18 } |
16 | 19 |
17 class DefaultPrefStore; | 20 class DefaultPrefStore; |
18 class PrefStore; | 21 class PrefStore; |
19 | 22 |
20 // Preferences need to be registered with a type and default value | 23 // Preferences need to be registered with a type and default value |
21 // before they are used. | 24 // before they are used. |
22 // | 25 // |
23 // The way you use a PrefRegistry is that you register all required | 26 // 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 | 27 // preferences on it (via one of its subclasses), then pass it as a |
25 // construction parameter to PrefService. | 28 // construction parameter to PrefService. |
26 // | 29 // |
27 // Currently, registrations after constructing the PrefService will | 30 // Currently, registrations after constructing the PrefService will |
28 // also work, but this is being deprecated. | 31 // also work, but this is being deprecated. |
29 class BASE_PREFS_EXPORT PrefRegistry : public base::RefCounted<PrefRegistry> { | 32 class BASE_PREFS_EXPORT PrefRegistry : public base::RefCounted<PrefRegistry> { |
30 public: | 33 public: |
31 // Registration flags that can be specified which impact how the pref will | 34 // 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 | 35 // 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 | 36 // registered. Subclasses of PrefRegistry can specify their own flags. Care |
34 // must be taken to ensure none of these overlap with the flags below. | 37 // must be taken to ensure none of these overlap with the flags below. |
35 enum PrefRegistrationFlags : uint32 { | 38 enum PrefRegistrationFlags : uint32_t { |
36 // No flags are specified. | 39 // No flags are specified. |
37 NO_REGISTRATION_FLAGS = 0, | 40 NO_REGISTRATION_FLAGS = 0, |
38 | 41 |
39 // The first 8 bits are reserved for subclasses of PrefRegistry to use. | 42 // The first 8 bits are reserved for subclasses of PrefRegistry to use. |
40 | 43 |
41 // This marks the pref as "lossy". There is no strict time guarantee on when | 44 // 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. | 45 // a lossy pref will be persisted to permanent storage when it is modified. |
43 LOSSY_PREF = 1 << 8, | 46 LOSSY_PREF = 1 << 8, |
44 }; | 47 }; |
45 | 48 |
46 typedef PrefValueMap::const_iterator const_iterator; | 49 typedef PrefValueMap::const_iterator const_iterator; |
47 typedef base::hash_map<std::string, uint32> PrefRegistrationFlagsMap; | 50 typedef base::hash_map<std::string, uint32_t> PrefRegistrationFlagsMap; |
48 | 51 |
49 PrefRegistry(); | 52 PrefRegistry(); |
50 | 53 |
51 // Retrieve the set of registration flags for the given preference. The return | 54 // Retrieve the set of registration flags for the given preference. The return |
52 // value is a bitmask of PrefRegistrationFlags. | 55 // value is a bitmask of PrefRegistrationFlags. |
53 uint32 GetRegistrationFlags(const std::string& pref_name) const; | 56 uint32_t GetRegistrationFlags(const std::string& pref_name) const; |
54 | 57 |
55 // Gets the registered defaults. | 58 // Gets the registered defaults. |
56 scoped_refptr<PrefStore> defaults(); | 59 scoped_refptr<PrefStore> defaults(); |
57 | 60 |
58 // Allows iteration over defaults. | 61 // Allows iteration over defaults. |
59 const_iterator begin() const; | 62 const_iterator begin() const; |
60 const_iterator end() const; | 63 const_iterator end() const; |
61 | 64 |
62 // Changes the default value for a preference. Takes ownership of |value|. | 65 // Changes the default value for a preference. Takes ownership of |value|. |
63 // | 66 // |
64 // |pref_name| must be a previously registered preference. | 67 // |pref_name| must be a previously registered preference. |
65 void SetDefaultPrefValue(const std::string& pref_name, base::Value* value); | 68 void SetDefaultPrefValue(const std::string& pref_name, base::Value* value); |
66 | 69 |
67 protected: | 70 protected: |
68 friend class base::RefCounted<PrefRegistry>; | 71 friend class base::RefCounted<PrefRegistry>; |
69 virtual ~PrefRegistry(); | 72 virtual ~PrefRegistry(); |
70 | 73 |
71 // Used by subclasses to register a default value and registration flags for | 74 // Used by subclasses to register a default value and registration flags for |
72 // a preference. |flags| is a bitmask of |PrefRegistrationFlags|. | 75 // a preference. |flags| is a bitmask of |PrefRegistrationFlags|. |
73 void RegisterPreference(const std::string& path, | 76 void RegisterPreference(const std::string& path, |
74 base::Value* default_value, | 77 base::Value* default_value, |
75 uint32 flags); | 78 uint32_t flags); |
76 | 79 |
77 scoped_refptr<DefaultPrefStore> defaults_; | 80 scoped_refptr<DefaultPrefStore> defaults_; |
78 | 81 |
79 // A map of pref name to a bitmask of PrefRegistrationFlags. | 82 // A map of pref name to a bitmask of PrefRegistrationFlags. |
80 PrefRegistrationFlagsMap registration_flags_; | 83 PrefRegistrationFlagsMap registration_flags_; |
81 | 84 |
82 private: | 85 private: |
83 DISALLOW_COPY_AND_ASSIGN(PrefRegistry); | 86 DISALLOW_COPY_AND_ASSIGN(PrefRegistry); |
84 }; | 87 }; |
85 | 88 |
86 #endif // BASE_PREFS_PREF_REGISTRY_H_ | 89 #endif // BASE_PREFS_PREF_REGISTRY_H_ |
OLD | NEW |