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 #include "components/prefs/value_map_pref_store.h"
6
7 #include <algorithm>
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
ValueMapPrefStore()15 ValueMapPrefStore::ValueMapPrefStore() {}
16
GetValue(base::StringPiece key,const base::Value ** value) const17 bool ValueMapPrefStore::GetValue(base::StringPiece key,
18 const base::Value** value) const {
19 return prefs_.GetValue(key, value);
20 }
21
GetValues() const22 base::Value::Dict ValueMapPrefStore::GetValues() const {
23 return prefs_.AsDict();
24 }
25
AddObserver(PrefStore::Observer * observer)26 void ValueMapPrefStore::AddObserver(PrefStore::Observer* observer) {
27 observers_.AddObserver(observer);
28 }
29
RemoveObserver(PrefStore::Observer * observer)30 void ValueMapPrefStore::RemoveObserver(PrefStore::Observer* observer) {
31 observers_.RemoveObserver(observer);
32 }
33
HasObservers() const34 bool ValueMapPrefStore::HasObservers() const {
35 return !observers_.empty();
36 }
37
SetValue(const std::string & key,base::Value value,uint32_t flags)38 void ValueMapPrefStore::SetValue(const std::string& key,
39 base::Value value,
40 uint32_t flags) {
41 if (prefs_.SetValue(key, std::move(value))) {
42 for (Observer& observer : observers_)
43 observer.OnPrefValueChanged(key);
44 }
45 }
46
RemoveValue(const std::string & key,uint32_t flags)47 void ValueMapPrefStore::RemoveValue(const std::string& key, uint32_t flags) {
48 if (prefs_.RemoveValue(key)) {
49 for (Observer& observer : observers_)
50 observer.OnPrefValueChanged(key);
51 }
52 }
53
GetMutableValue(const std::string & key,base::Value ** value)54 bool ValueMapPrefStore::GetMutableValue(const std::string& key,
55 base::Value** value) {
56 return prefs_.GetValue(key, value);
57 }
58
ReportValueChanged(const std::string & key,uint32_t flags)59 void ValueMapPrefStore::ReportValueChanged(const std::string& key,
60 uint32_t flags) {
61 for (Observer& observer : observers_)
62 observer.OnPrefValueChanged(key);
63 }
64
SetValueSilently(const std::string & key,base::Value value,uint32_t flags)65 void ValueMapPrefStore::SetValueSilently(const std::string& key,
66 base::Value value,
67 uint32_t flags) {
68 prefs_.SetValue(key, std::move(value));
69 }
70
~ValueMapPrefStore()71 ValueMapPrefStore::~ValueMapPrefStore() {}
72
NotifyInitializationCompleted()73 void ValueMapPrefStore::NotifyInitializationCompleted() {
74 for (Observer& observer : observers_)
75 observer.OnInitializationCompleted(true);
76 }
77
RemoveValuesByPrefixSilently(const std::string & prefix)78 void ValueMapPrefStore::RemoveValuesByPrefixSilently(
79 const std::string& prefix) {
80 prefs_.ClearWithPrefix(prefix);
81 }
82