Add third_party/khronos to GN configs in ozone gbm
[chromium-blink-merge.git] / base / prefs / testing_pref_store.cc
blob35c9763eec5b15c3a27ef375750dd45adbd6a3e3
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.
5 #include "base/prefs/testing_pref_store.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/values.h"
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) {}
19 bool TestingPrefStore::GetValue(const std::string& key,
20 const base::Value** value) const {
21 return prefs_.GetValue(key, value);
24 bool TestingPrefStore::GetMutableValue(const std::string& key,
25 base::Value** value) {
26 return prefs_.GetValue(key, value);
29 void TestingPrefStore::AddObserver(PrefStore::Observer* observer) {
30 observers_.AddObserver(observer);
33 void TestingPrefStore::RemoveObserver(PrefStore::Observer* observer) {
34 observers_.RemoveObserver(observer);
37 bool TestingPrefStore::HasObservers() const {
38 return observers_.might_have_observers();
41 bool TestingPrefStore::IsInitializationComplete() const {
42 return init_complete_;
45 void TestingPrefStore::SetValue(const std::string& key,
46 base::Value* value,
47 uint32 flags) {
48 if (prefs_.SetValue(key, value)) {
49 committed_ = false;
50 NotifyPrefValueChanged(key);
54 void TestingPrefStore::SetValueSilently(const std::string& key,
55 base::Value* value,
56 uint32 flags) {
57 if (prefs_.SetValue(key, value))
58 committed_ = false;
61 void TestingPrefStore::RemoveValue(const std::string& key, uint32 flags) {
62 if (prefs_.RemoveValue(key)) {
63 committed_ = false;
64 NotifyPrefValueChanged(key);
68 bool TestingPrefStore::ReadOnly() const {
69 return read_only_;
72 PersistentPrefStore::PrefReadError TestingPrefStore::GetReadError() const {
73 return read_error_;
76 PersistentPrefStore::PrefReadError TestingPrefStore::ReadPrefs() {
77 NotifyInitializationCompleted();
78 return read_error_;
81 void TestingPrefStore::ReadPrefsAsync(ReadErrorDelegate* error_delegate) {
82 DCHECK(!pending_async_read_);
83 error_delegate_.reset(error_delegate);
84 if (block_async_read_)
85 pending_async_read_ = true;
86 else
87 NotifyInitializationCompleted();
90 void TestingPrefStore::CommitPendingWrite() { committed_ = true; }
92 void TestingPrefStore::SetInitializationCompleted() {
93 NotifyInitializationCompleted();
96 void TestingPrefStore::NotifyPrefValueChanged(const std::string& key) {
97 FOR_EACH_OBSERVER(Observer, observers_, OnPrefValueChanged(key));
100 void TestingPrefStore::NotifyInitializationCompleted() {
101 DCHECK(!init_complete_);
102 init_complete_ = true;
103 if (read_success_ && read_error_ != PREF_READ_ERROR_NONE && error_delegate_)
104 error_delegate_->OnError(read_error_);
105 FOR_EACH_OBSERVER(
106 Observer, observers_, OnInitializationCompleted(read_success_));
109 void TestingPrefStore::ReportValueChanged(const std::string& key,
110 uint32 flags) {
111 FOR_EACH_OBSERVER(Observer, observers_, OnPrefValueChanged(key));
114 void TestingPrefStore::SetString(const std::string& key,
115 const std::string& value) {
116 SetValue(key, new base::StringValue(value), DEFAULT_PREF_WRITE_FLAGS);
119 void TestingPrefStore::SetInteger(const std::string& key, int value) {
120 SetValue(key, new base::FundamentalValue(value), DEFAULT_PREF_WRITE_FLAGS);
123 void TestingPrefStore::SetBoolean(const std::string& key, bool value) {
124 SetValue(key, new base::FundamentalValue(value), DEFAULT_PREF_WRITE_FLAGS);
127 bool TestingPrefStore::GetString(const std::string& key,
128 std::string* value) const {
129 const base::Value* stored_value;
130 if (!prefs_.GetValue(key, &stored_value) || !stored_value)
131 return false;
133 return stored_value->GetAsString(value);
136 bool TestingPrefStore::GetInteger(const std::string& key, int* value) const {
137 const base::Value* stored_value;
138 if (!prefs_.GetValue(key, &stored_value) || !stored_value)
139 return false;
141 return stored_value->GetAsInteger(value);
144 bool TestingPrefStore::GetBoolean(const std::string& key, bool* value) const {
145 const base::Value* stored_value;
146 if (!prefs_.GetValue(key, &stored_value) || !stored_value)
147 return false;
149 return stored_value->GetAsBoolean(value);
152 void TestingPrefStore::SetBlockAsyncRead(bool block_async_read) {
153 DCHECK(!init_complete_);
154 block_async_read_ = block_async_read;
155 if (pending_async_read_ && !block_async_read_)
156 NotifyInitializationCompleted();
159 void TestingPrefStore::set_read_only(bool read_only) {
160 read_only_ = read_only;
163 void TestingPrefStore::set_read_success(bool read_success) {
164 DCHECK(!init_complete_);
165 read_success_ = read_success;
168 void TestingPrefStore::set_read_error(
169 PersistentPrefStore::PrefReadError read_error) {
170 DCHECK(!init_complete_);
171 read_error_ = read_error;
174 TestingPrefStore::~TestingPrefStore() {}