• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef COMPONENTS_PREFS_DEFAULT_PREF_STORE_H_
6 #define COMPONENTS_PREFS_DEFAULT_PREF_STORE_H_
7 
8 #include <memory>
9 #include <string_view>
10 
11 #include "base/observer_list.h"
12 #include "base/values.h"
13 #include "components/prefs/pref_store.h"
14 #include "components/prefs/pref_value_map.h"
15 #include "components/prefs/prefs_export.h"
16 
17 // Used within a PrefRegistry to keep track of default preference values.
18 class COMPONENTS_PREFS_EXPORT DefaultPrefStore : public PrefStore {
19  public:
20   typedef PrefValueMap::const_iterator const_iterator;
21 
22   DefaultPrefStore();
23 
24   DefaultPrefStore(const DefaultPrefStore&) = delete;
25   DefaultPrefStore& operator=(const DefaultPrefStore&) = delete;
26 
27   // PrefStore implementation:
28   bool GetValue(std::string_view key,
29                 const base::Value** result) const override;
30   base::Value::Dict GetValues() const override;
31   void AddObserver(PrefStore::Observer* observer) override;
32   void RemoveObserver(PrefStore::Observer* observer) override;
33   bool HasObservers() const override;
34 
35   // Sets a `value` for `key`. Should only be called if a value has not been
36   // set yet; otherwise call ReplaceDefaultValue().
37   void SetDefaultValue(std::string_view key, base::Value value);
38 
39   // Replaces the the value for `key` with a new value. Should only be called
40   // if a value has already been set; otherwise call SetDefaultValue().
41   void ReplaceDefaultValue(std::string_view key, base::Value value);
42 
43   const_iterator begin() const;
44   const_iterator end() const;
45 
46  private:
47   ~DefaultPrefStore() override;
48 
49   PrefValueMap prefs_;
50 
51   base::ObserverList<PrefStore::Observer, true> observers_;
52 };
53 
54 #endif  // COMPONENTS_PREFS_DEFAULT_PREF_STORE_H_
55