1 // 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
5 #include "base/prefs/testing_pref_store.h"
6
7 #include "base/memory/scoped_ptr.h"
8 #include "base/values.h"
9
TestingPrefStore()10 TestingPrefStore::TestingPrefStore()
11 : read_only_(true),
12 init_complete_(false) {
13 }
14
GetValue(const std::string & key,const base::Value ** value) const15 bool TestingPrefStore::GetValue(const std::string& key,
16 const base::Value** value) const {
17 return prefs_.GetValue(key, value);
18 }
19
GetMutableValue(const std::string & key,base::Value ** value)20 bool TestingPrefStore::GetMutableValue(const std::string& key,
21 base::Value** value) {
22 return prefs_.GetValue(key, value);
23 }
24
AddObserver(PrefStore::Observer * observer)25 void TestingPrefStore::AddObserver(PrefStore::Observer* observer) {
26 observers_.AddObserver(observer);
27 }
28
RemoveObserver(PrefStore::Observer * observer)29 void TestingPrefStore::RemoveObserver(PrefStore::Observer* observer) {
30 observers_.RemoveObserver(observer);
31 }
32
HasObservers() const33 bool TestingPrefStore::HasObservers() const {
34 return observers_.might_have_observers();
35 }
36
IsInitializationComplete() const37 bool TestingPrefStore::IsInitializationComplete() const {
38 return init_complete_;
39 }
40
SetValue(const std::string & key,base::Value * value)41 void TestingPrefStore::SetValue(const std::string& key, base::Value* value) {
42 if (prefs_.SetValue(key, value))
43 NotifyPrefValueChanged(key);
44 }
45
SetValueSilently(const std::string & key,base::Value * value)46 void TestingPrefStore::SetValueSilently(const std::string& key,
47 base::Value* value) {
48 prefs_.SetValue(key, value);
49 }
50
RemoveValue(const std::string & key)51 void TestingPrefStore::RemoveValue(const std::string& key) {
52 if (prefs_.RemoveValue(key))
53 NotifyPrefValueChanged(key);
54 }
55
ReadOnly() const56 bool TestingPrefStore::ReadOnly() const {
57 return read_only_;
58 }
59
GetReadError() const60 PersistentPrefStore::PrefReadError TestingPrefStore::GetReadError() const {
61 return PersistentPrefStore::PREF_READ_ERROR_NONE;
62 }
63
ReadPrefs()64 PersistentPrefStore::PrefReadError TestingPrefStore::ReadPrefs() {
65 NotifyInitializationCompleted();
66 return PersistentPrefStore::PREF_READ_ERROR_NONE;
67 }
68
ReadPrefsAsync(ReadErrorDelegate * error_delegate_raw)69 void TestingPrefStore::ReadPrefsAsync(ReadErrorDelegate* error_delegate_raw) {
70 scoped_ptr<ReadErrorDelegate> error_delegate(error_delegate_raw);
71 NotifyInitializationCompleted();
72 }
73
SetInitializationCompleted()74 void TestingPrefStore::SetInitializationCompleted() {
75 init_complete_ = true;
76 NotifyInitializationCompleted();
77 }
78
NotifyPrefValueChanged(const std::string & key)79 void TestingPrefStore::NotifyPrefValueChanged(const std::string& key) {
80 FOR_EACH_OBSERVER(Observer, observers_, OnPrefValueChanged(key));
81 }
82
NotifyInitializationCompleted()83 void TestingPrefStore::NotifyInitializationCompleted() {
84 FOR_EACH_OBSERVER(Observer, observers_, OnInitializationCompleted(true));
85 }
86
ReportValueChanged(const std::string & key)87 void TestingPrefStore::ReportValueChanged(const std::string& key) {
88 FOR_EACH_OBSERVER(Observer, observers_, OnPrefValueChanged(key));
89 }
90
SetString(const std::string & key,const std::string & value)91 void TestingPrefStore::SetString(const std::string& key,
92 const std::string& value) {
93 SetValue(key, new base::StringValue(value));
94 }
95
SetInteger(const std::string & key,int value)96 void TestingPrefStore::SetInteger(const std::string& key, int value) {
97 SetValue(key, new base::FundamentalValue(value));
98 }
99
SetBoolean(const std::string & key,bool value)100 void TestingPrefStore::SetBoolean(const std::string& key, bool value) {
101 SetValue(key, new base::FundamentalValue(value));
102 }
103
GetString(const std::string & key,std::string * value) const104 bool TestingPrefStore::GetString(const std::string& key,
105 std::string* value) const {
106 const base::Value* stored_value;
107 if (!prefs_.GetValue(key, &stored_value) || !stored_value)
108 return false;
109
110 return stored_value->GetAsString(value);
111 }
112
GetInteger(const std::string & key,int * value) const113 bool TestingPrefStore::GetInteger(const std::string& key, int* value) const {
114 const base::Value* stored_value;
115 if (!prefs_.GetValue(key, &stored_value) || !stored_value)
116 return false;
117
118 return stored_value->GetAsInteger(value);
119 }
120
GetBoolean(const std::string & key,bool * value) const121 bool TestingPrefStore::GetBoolean(const std::string& key, bool* value) const {
122 const base::Value* stored_value;
123 if (!prefs_.GetValue(key, &stored_value) || !stored_value)
124 return false;
125
126 return stored_value->GetAsBoolean(value);
127 }
128
set_read_only(bool read_only)129 void TestingPrefStore::set_read_only(bool read_only) {
130 read_only_ = read_only;
131 }
132
~TestingPrefStore()133 TestingPrefStore::~TestingPrefStore() {}
134