• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 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_IN_MEMORY_PREF_STORE_H_
6 #define COMPONENTS_PREFS_IN_MEMORY_PREF_STORE_H_
7 
8 #include <stdint.h>
9 
10 #include <string_view>
11 
12 #include "base/compiler_specific.h"
13 #include "base/observer_list.h"
14 #include "base/values.h"
15 #include "components/prefs/persistent_pref_store.h"
16 #include "components/prefs/pref_value_map.h"
17 
18 // A light-weight prefstore implementation that keeps preferences
19 // in a memory backed store. This is not a persistent prefstore -- we
20 // subclass the PersistentPrefStore here since it is needed by the
21 // PrefService, which in turn is needed by various components.
22 class COMPONENTS_PREFS_EXPORT InMemoryPrefStore : public PersistentPrefStore {
23  public:
24   InMemoryPrefStore();
25 
26   InMemoryPrefStore(const InMemoryPrefStore&) = delete;
27   InMemoryPrefStore& operator=(const InMemoryPrefStore&) = delete;
28 
29   // PrefStore implementation.
30   bool GetValue(std::string_view key,
31                 const base::Value** result) const override;
32   base::Value::Dict GetValues() const override;
33   void AddObserver(PrefStore::Observer* observer) override;
34   void RemoveObserver(PrefStore::Observer* observer) override;
35   bool HasObservers() const override;
36   bool IsInitializationComplete() const override;
37 
38   // PersistentPrefStore implementation.
39   bool GetMutableValue(std::string_view key, base::Value** result) override;
40   void ReportValueChanged(std::string_view key, uint32_t flags) override;
41   void SetValue(std::string_view key,
42                 base::Value value,
43                 uint32_t flags) override;
44   void SetValueSilently(std::string_view key,
45                         base::Value value,
46                         uint32_t flags) override;
47   void RemoveValue(std::string_view key, uint32_t flags) override;
48   bool ReadOnly() const override;
49   PrefReadError GetReadError() const override;
50   PersistentPrefStore::PrefReadError ReadPrefs() override;
51   void ReadPrefsAsync(ReadErrorDelegate* error_delegate) override;
SchedulePendingLossyWrites()52   void SchedulePendingLossyWrites() override {}
OnStoreDeletionFromDisk()53   void OnStoreDeletionFromDisk() override {}
54   bool IsInMemoryPrefStore() const override;
55   void RemoveValuesByPrefixSilently(std::string_view prefix) override;
56   bool HasReadErrorDelegate() const override;
57 
58  protected:
59   ~InMemoryPrefStore() override;
60 
61  private:
62   // Stores the preference values.
63   PrefValueMap prefs_;
64 
65   base::ObserverList<PrefStore::Observer, true> observers_;
66 };
67 
68 #endif  // COMPONENTS_PREFS_IN_MEMORY_PREF_STORE_H_
69