blob: caf2a1afaf1daca90ba6242dd0456fc2efe94c86 [file] [log] [blame]
[email protected]b1de2c72013-02-06 02:45:471// 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]3853a4c2013-02-11 17:15:575#ifndef BASE_PREFS_PREF_REGISTRY_H_
6#define BASE_PREFS_PREF_REGISTRY_H_
[email protected]b1de2c72013-02-06 02:45:477
raymes51b41a62015-04-24 02:45:048#include "base/containers/hash_tables.h"
[email protected]b1de2c72013-02-06 02:45:479#include "base/memory/ref_counted.h"
[email protected]3853a4c2013-02-11 17:15:5710#include "base/prefs/base_prefs_export.h"
[email protected]b1de2c72013-02-06 02:45:4711#include "base/prefs/pref_value_map.h"
12
13namespace base {
14class Value;
15}
16
17class DefaultPrefStore;
18class 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]3853a4c2013-02-11 17:15:5729class BASE_PREFS_EXPORT PrefRegistry : public base::RefCounted<PrefRegistry> {
[email protected]b1de2c72013-02-06 02:45:4730 public:
raymes51b41a62015-04-24 02:45:0431 // 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.
raymes76de1af2015-05-06 03:22:2135 enum PrefRegistrationFlags : uint32 {
raymes51b41a62015-04-24 02:45:0436 // No flags are specified.
37 NO_REGISTRATION_FLAGS = 0,
38
39 // The first 8 bits are reserved for subclasses of PrefRegistry to use.
raymes76de1af2015-05-06 03:22:2140
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,
raymes51b41a62015-04-24 02:45:0444 };
45
[email protected]b1de2c72013-02-06 02:45:4746 typedef PrefValueMap::const_iterator const_iterator;
raymes51b41a62015-04-24 02:45:0447 typedef base::hash_map<std::string, uint32> PrefRegistrationFlagsMap;
[email protected]b1de2c72013-02-06 02:45:4748
49 PrefRegistry();
50
raymes51b41a62015-04-24 02:45:0451 // 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]b1de2c72013-02-06 02:45:4755 // 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]5879cef2013-03-02 17:02:2562 // Changes the default value for a preference. Takes ownership of |value|.
63 //
64 // |pref_name| must be a previously registered preference.
georgesak7da6e9d2014-12-03 01:10:2965 void SetDefaultPrefValue(const std::string& pref_name, base::Value* value);
[email protected]5879cef2013-03-02 17:02:2566
[email protected]b1de2c72013-02-06 02:45:4767 protected:
68 friend class base::RefCounted<PrefRegistry>;
69 virtual ~PrefRegistry();
70
raymes51b41a62015-04-24 02:45:0471 // 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]b1de2c72013-02-06 02:45:4776
[email protected]c753f142013-02-10 13:14:0477 scoped_refptr<DefaultPrefStore> defaults_;
78
raymes51b41a62015-04-24 02:45:0479 // A map of pref name to a bitmask of PrefRegistrationFlags.
80 PrefRegistrationFlagsMap registration_flags_;
81
[email protected]b1de2c72013-02-06 02:45:4782 private:
[email protected]b1de2c72013-02-06 02:45:4783 DISALLOW_COPY_AND_ASSIGN(PrefRegistry);
84};
85
[email protected]3853a4c2013-02-11 17:15:5786#endif // BASE_PREFS_PREF_REGISTRY_H_