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 read_success_(true),
13 read_error_(PersistentPrefStore::PREF_READ_ERROR_NONE),
14 block_async_read_(false),
15 pending_async_read_(false),
16 init_complete_(false),
17 committed_(true) {}
18
GetValue(const std::string & key,const base::Value ** value) const19 bool TestingPrefStore::GetValue(const std::string& key,
20 const base::Value** value) const {
21 return prefs_.GetValue(key, value);
22 }
23
GetMutableValue(const std::string & key,base::Value ** value)24 bool TestingPrefStore::GetMutableValue(const std::string& key,
25 base::Value** value) {
26 return prefs_.GetValue(key, value);
27 }
28
AddObserver(PrefStore::Observer * observer)29 void TestingPrefStore::AddObserver(PrefStore::Observer* observer) {
30 observers_.AddObserver(observer);
31 }
32
RemoveObserver(PrefStore::Observer * observer)33 void TestingPrefStore::RemoveObserver(PrefStore::Observer* observer) {
34 observers_.RemoveObserver(observer);
35 }
36
HasObservers() const37 bool TestingPrefStore::HasObservers() const {
38 return observers_.might_have_observers();
39 }
40
IsInitializationComplete() const41 bool TestingPrefStore::IsInitializationComplete() const {
42 return init_complete_;
43 }
44
SetValue(const std::string & key,base::Value * value)45 void TestingPrefStore::SetValue(const std::string& key, base::Value* value) {
46 if (prefs_.SetValue(key, value)) {
47 committed_ = false;
48 NotifyPrefValueChanged(key);
49 }
50 }
51
SetValueSilently(const std::string & key,base::Value * value)52 void TestingPrefStore::SetValueSilently(const std::string& key,
53 base::Value* value) {
54 if (prefs_.SetValue(key, value))
55 committed_ = false;
56 }
57
RemoveValue(const std::string & key)58 void TestingPrefStore::RemoveValue(const std::string& key) {
59 if (prefs_.RemoveValue(key)) {
60 committed_ = false;
61 NotifyPrefValueChanged(key);
62 }
63 }
64
ReadOnly() const65 bool TestingPrefStore::ReadOnly() const {
66 return read_only_;
67 }
68
GetReadError() const69 PersistentPrefStore::PrefReadError TestingPrefStore::GetReadError() const {
70 return read_error_;
71 }
72
ReadPrefs()73 PersistentPrefStore::PrefReadError TestingPrefStore::ReadPrefs() {
74 NotifyInitializationCompleted();
75 return read_error_;
76 }
77
ReadPrefsAsync(ReadErrorDelegate * error_delegate)78 void TestingPrefStore::ReadPrefsAsync(ReadErrorDelegate* error_delegate) {
79 DCHECK(!pending_async_read_);
80 error_delegate_.reset(error_delegate);
81 if (block_async_read_)
82 pending_async_read_ = true;
83 else
84 NotifyInitializationCompleted();
85 }
86
CommitPendingWrite()87 void TestingPrefStore::CommitPendingWrite() { committed_ = true; }
88
SetInitializationCompleted()89 void TestingPrefStore::SetInitializationCompleted() {
90 NotifyInitializationCompleted();
91 }
92
NotifyPrefValueChanged(const std::string & key)93 void TestingPrefStore::NotifyPrefValueChanged(const std::string& key) {
94 FOR_EACH_OBSERVER(Observer, observers_, OnPrefValueChanged(key));
95 }
96
NotifyInitializationCompleted()97 void TestingPrefStore::NotifyInitializationCompleted() {
98 DCHECK(!init_complete_);
99 init_complete_ = true;
100 if (read_success_ && read_error_ != PREF_READ_ERROR_NONE && error_delegate_)
101 error_delegate_->OnError(read_error_);
102 FOR_EACH_OBSERVER(
103 Observer, observers_, OnInitializationCompleted(read_success_));
104 }
105
ReportValueChanged(const std::string & key)106 void TestingPrefStore::ReportValueChanged(const std::string& key) {
107 FOR_EACH_OBSERVER(Observer, observers_, OnPrefValueChanged(key));
108 }
109
SetString(const std::string & key,const std::string & value)110 void TestingPrefStore::SetString(const std::string& key,
111 const std::string& value) {
112 SetValue(key, new base::StringValue(value));
113 }
114
SetInteger(const std::string & key,int value)115 void TestingPrefStore::SetInteger(const std::string& key, int value) {
116 SetValue(key, new base::FundamentalValue(value));
117 }
118
SetBoolean(const std::string & key,bool value)119 void TestingPrefStore::SetBoolean(const std::string& key, bool value) {
120 SetValue(key, new base::FundamentalValue(value));
121 }
122
GetString(const std::string & key,std::string * value) const123 bool TestingPrefStore::GetString(const std::string& key,
124 std::string* value) const {
125 const base::Value* stored_value;
126 if (!prefs_.GetValue(key, &stored_value) || !stored_value)
127 return false;
128
129 return stored_value->GetAsString(value);
130 }
131
GetInteger(const std::string & key,int * value) const132 bool TestingPrefStore::GetInteger(const std::string& key, int* value) const {
133 const base::Value* stored_value;
134 if (!prefs_.GetValue(key, &stored_value) || !stored_value)
135 return false;
136
137 return stored_value->GetAsInteger(value);
138 }
139
GetBoolean(const std::string & key,bool * value) const140 bool TestingPrefStore::GetBoolean(const std::string& key, bool* value) const {
141 const base::Value* stored_value;
142 if (!prefs_.GetValue(key, &stored_value) || !stored_value)
143 return false;
144
145 return stored_value->GetAsBoolean(value);
146 }
147
SetBlockAsyncRead(bool block_async_read)148 void TestingPrefStore::SetBlockAsyncRead(bool block_async_read) {
149 DCHECK(!init_complete_);
150 block_async_read_ = block_async_read;
151 if (pending_async_read_ && !block_async_read_)
152 NotifyInitializationCompleted();
153 }
154
set_read_only(bool read_only)155 void TestingPrefStore::set_read_only(bool read_only) {
156 read_only_ = read_only;
157 }
158
set_read_success(bool read_success)159 void TestingPrefStore::set_read_success(bool read_success) {
160 DCHECK(!init_complete_);
161 read_success_ = read_success;
162 }
163
set_read_error(PersistentPrefStore::PrefReadError read_error)164 void TestingPrefStore::set_read_error(
165 PersistentPrefStore::PrefReadError read_error) {
166 DCHECK(!init_complete_);
167 read_error_ = read_error;
168 }
169
~TestingPrefStore()170 TestingPrefStore::~TestingPrefStore() {}
171