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