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