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 #include "components/prefs/in_memory_pref_store.h"
6
7 #include <memory>
8 #include <string>
9 #include <utility>
10
11 #include "base/observer_list.h"
12 #include "base/strings/string_piece.h"
13 #include "base/values.h"
14
InMemoryPrefStore()15 InMemoryPrefStore::InMemoryPrefStore() {}
16
~InMemoryPrefStore()17 InMemoryPrefStore::~InMemoryPrefStore() {}
18
GetValue(base::StringPiece key,const base::Value ** value) const19 bool InMemoryPrefStore::GetValue(base::StringPiece key,
20 const base::Value** value) const {
21 return prefs_.GetValue(key, value);
22 }
23
GetValues() const24 base::Value::Dict InMemoryPrefStore::GetValues() const {
25 return prefs_.AsDict();
26 }
27
GetMutableValue(const std::string & key,base::Value ** value)28 bool InMemoryPrefStore::GetMutableValue(const std::string& key,
29 base::Value** value) {
30 return prefs_.GetValue(key, value);
31 }
32
AddObserver(PrefStore::Observer * observer)33 void InMemoryPrefStore::AddObserver(PrefStore::Observer* observer) {
34 observers_.AddObserver(observer);
35 }
36
RemoveObserver(PrefStore::Observer * observer)37 void InMemoryPrefStore::RemoveObserver(PrefStore::Observer* observer) {
38 observers_.RemoveObserver(observer);
39 }
40
HasObservers() const41 bool InMemoryPrefStore::HasObservers() const {
42 return !observers_.empty();
43 }
44
IsInitializationComplete() const45 bool InMemoryPrefStore::IsInitializationComplete() const {
46 return true;
47 }
48
SetValue(const std::string & key,base::Value value,uint32_t flags)49 void InMemoryPrefStore::SetValue(const std::string& key,
50 base::Value value,
51 uint32_t flags) {
52 if (prefs_.SetValue(key, std::move(value)))
53 ReportValueChanged(key, flags);
54 }
55
SetValueSilently(const std::string & key,base::Value value,uint32_t flags)56 void InMemoryPrefStore::SetValueSilently(const std::string& key,
57 base::Value value,
58 uint32_t flags) {
59 prefs_.SetValue(key, std::move(value));
60 }
61
RemoveValue(const std::string & key,uint32_t flags)62 void InMemoryPrefStore::RemoveValue(const std::string& key, uint32_t flags) {
63 if (prefs_.RemoveValue(key))
64 ReportValueChanged(key, flags);
65 }
66
RemoveValuesByPrefixSilently(const std::string & prefix)67 void InMemoryPrefStore::RemoveValuesByPrefixSilently(
68 const std::string& prefix) {
69 prefs_.ClearWithPrefix(prefix);
70 }
71
ReadOnly() const72 bool InMemoryPrefStore::ReadOnly() const {
73 return false;
74 }
75
GetReadError() const76 PersistentPrefStore::PrefReadError InMemoryPrefStore::GetReadError() const {
77 return PersistentPrefStore::PREF_READ_ERROR_NONE;
78 }
79
ReadPrefs()80 PersistentPrefStore::PrefReadError InMemoryPrefStore::ReadPrefs() {
81 return PersistentPrefStore::PREF_READ_ERROR_NONE;
82 }
83
ReportValueChanged(const std::string & key,uint32_t flags)84 void InMemoryPrefStore::ReportValueChanged(const std::string& key,
85 uint32_t flags) {
86 for (Observer& observer : observers_)
87 observer.OnPrefValueChanged(key);
88 }
89
IsInMemoryPrefStore() const90 bool InMemoryPrefStore::IsInMemoryPrefStore() const {
91 return true;
92 }
93