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