Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/prefs/testing_pref_store.h" | 5 #include "base/prefs/testing_pref_store.h" |
| 6 | 6 |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/values.h" | 8 #include "base/values.h" |
| 9 | 9 |
| 10 TestingPrefStore::TestingPrefStore() | 10 TestingPrefStore::TestingPrefStore() |
| 11 : read_only_(true), | 11 : read_only_(true), |
| 12 init_complete_(false) { | 12 read_success_(true), |
| 13 } | 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) {} | |
| 14 | 18 |
| 15 bool TestingPrefStore::GetValue(const std::string& key, | 19 bool TestingPrefStore::GetValue(const std::string& key, |
| 16 const base::Value** value) const { | 20 const base::Value** value) const { |
| 17 return prefs_.GetValue(key, value); | 21 return prefs_.GetValue(key, value); |
| 18 } | 22 } |
| 19 | 23 |
| 20 bool TestingPrefStore::GetMutableValue(const std::string& key, | 24 bool TestingPrefStore::GetMutableValue(const std::string& key, |
| 21 base::Value** value) { | 25 base::Value** value) { |
| 22 return prefs_.GetValue(key, value); | 26 return prefs_.GetValue(key, value); |
| 23 } | 27 } |
| 24 | 28 |
| 25 void TestingPrefStore::AddObserver(PrefStore::Observer* observer) { | 29 void TestingPrefStore::AddObserver(PrefStore::Observer* observer) { |
| 26 observers_.AddObserver(observer); | 30 observers_.AddObserver(observer); |
| 27 } | 31 } |
| 28 | 32 |
| 29 void TestingPrefStore::RemoveObserver(PrefStore::Observer* observer) { | 33 void TestingPrefStore::RemoveObserver(PrefStore::Observer* observer) { |
| 30 observers_.RemoveObserver(observer); | 34 observers_.RemoveObserver(observer); |
| 31 } | 35 } |
| 32 | 36 |
| 33 bool TestingPrefStore::HasObservers() const { | 37 bool TestingPrefStore::HasObservers() const { |
| 34 return observers_.might_have_observers(); | 38 return observers_.might_have_observers(); |
| 35 } | 39 } |
| 36 | 40 |
| 37 bool TestingPrefStore::IsInitializationComplete() const { | 41 bool TestingPrefStore::IsInitializationComplete() const { |
| 38 return init_complete_; | 42 return init_complete_; |
| 39 } | 43 } |
| 40 | 44 |
| 41 void TestingPrefStore::SetValue(const std::string& key, base::Value* value) { | 45 void TestingPrefStore::SetValue(const std::string& key, base::Value* value) { |
| 42 if (prefs_.SetValue(key, value)) | 46 if (prefs_.SetValue(key, value)) { |
| 47 committed_ = false; | |
| 43 NotifyPrefValueChanged(key); | 48 NotifyPrefValueChanged(key); |
| 49 } | |
| 44 } | 50 } |
| 45 | 51 |
| 46 void TestingPrefStore::SetValueSilently(const std::string& key, | 52 void TestingPrefStore::SetValueSilently(const std::string& key, |
| 47 base::Value* value) { | 53 base::Value* value) { |
| 48 prefs_.SetValue(key, value); | 54 if (prefs_.SetValue(key, value)) |
| 55 committed_ = false; | |
| 49 } | 56 } |
| 50 | 57 |
| 51 void TestingPrefStore::RemoveValue(const std::string& key) { | 58 void TestingPrefStore::RemoveValue(const std::string& key) { |
| 52 if (prefs_.RemoveValue(key)) | 59 if (prefs_.RemoveValue(key)) { |
| 60 committed_ = false; | |
| 53 NotifyPrefValueChanged(key); | 61 NotifyPrefValueChanged(key); |
| 62 } | |
| 54 } | 63 } |
| 55 | 64 |
| 56 bool TestingPrefStore::ReadOnly() const { | 65 bool TestingPrefStore::ReadOnly() const { |
| 57 return read_only_; | 66 return read_only_; |
| 58 } | 67 } |
| 59 | 68 |
| 60 PersistentPrefStore::PrefReadError TestingPrefStore::GetReadError() const { | 69 PersistentPrefStore::PrefReadError TestingPrefStore::GetReadError() const { |
| 61 return PersistentPrefStore::PREF_READ_ERROR_NONE; | 70 return read_error_; |
| 62 } | 71 } |
| 63 | 72 |
| 64 PersistentPrefStore::PrefReadError TestingPrefStore::ReadPrefs() { | 73 PersistentPrefStore::PrefReadError TestingPrefStore::ReadPrefs() { |
| 65 NotifyInitializationCompleted(); | 74 NotifyInitializationCompleted(); |
| 66 return PersistentPrefStore::PREF_READ_ERROR_NONE; | 75 return read_error_; |
| 67 } | 76 } |
| 68 | 77 |
| 69 void TestingPrefStore::ReadPrefsAsync(ReadErrorDelegate* error_delegate_raw) { | 78 void TestingPrefStore::ReadPrefsAsync(ReadErrorDelegate* error_delegate) { |
| 70 scoped_ptr<ReadErrorDelegate> error_delegate(error_delegate_raw); | 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 | |
| 87 void TestingPrefStore::CommitPendingWrite() { committed_ = true; } | |
| 88 | |
| 89 void TestingPrefStore::SetInitializationCompleted() { | |
| 71 NotifyInitializationCompleted(); | 90 NotifyInitializationCompleted(); |
| 72 } | 91 } |
| 73 | 92 |
| 74 void TestingPrefStore::SetInitializationCompleted() { | |
| 75 init_complete_ = true; | |
| 76 NotifyInitializationCompleted(); | |
| 77 } | |
| 78 | |
| 79 void TestingPrefStore::NotifyPrefValueChanged(const std::string& key) { | 93 void TestingPrefStore::NotifyPrefValueChanged(const std::string& key) { |
| 80 FOR_EACH_OBSERVER(Observer, observers_, OnPrefValueChanged(key)); | 94 FOR_EACH_OBSERVER(Observer, observers_, OnPrefValueChanged(key)); |
| 81 } | 95 } |
| 82 | 96 |
| 83 void TestingPrefStore::NotifyInitializationCompleted() { | 97 void TestingPrefStore::NotifyInitializationCompleted() { |
| 84 FOR_EACH_OBSERVER(Observer, observers_, OnInitializationCompleted(true)); | 98 DCHECK(!init_complete_); |
| 99 init_complete_ = true; | |
| 100 if (read_success_ && read_error_ != PREF_READ_ERROR_NONE && error_delegate_) | |
|
Mattias Nissler (ping if slow)
2014/03/27 20:28:05
nit: I'd drop the read_error_ != PREF_READ_ERROR_N
erikwright (departed)
2014/03/28 12:29:22
This logic models the behavior of JsonPrefStore, w
Mattias Nissler (ping if slow)
2014/03/28 12:32:53
Don't you already have that bit in |read_success_|
erikwright (departed)
2014/03/28 12:49:55
No, in JsonPrefStore, read_success_ can be true wh
Mattias Nissler (ping if slow)
2014/03/28 12:57:41
OK, makes sense.
| |
| 101 error_delegate_->OnError(read_error_); | |
| 102 FOR_EACH_OBSERVER( | |
| 103 Observer, observers_, OnInitializationCompleted(read_success_)); | |
| 85 } | 104 } |
| 86 | 105 |
| 87 void TestingPrefStore::ReportValueChanged(const std::string& key) { | 106 void TestingPrefStore::ReportValueChanged(const std::string& key) { |
| 88 FOR_EACH_OBSERVER(Observer, observers_, OnPrefValueChanged(key)); | 107 FOR_EACH_OBSERVER(Observer, observers_, OnPrefValueChanged(key)); |
| 89 } | 108 } |
| 90 | 109 |
| 91 void TestingPrefStore::SetString(const std::string& key, | 110 void TestingPrefStore::SetString(const std::string& key, |
| 92 const std::string& value) { | 111 const std::string& value) { |
| 93 SetValue(key, new base::StringValue(value)); | 112 SetValue(key, new base::StringValue(value)); |
| 94 } | 113 } |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 119 } | 138 } |
| 120 | 139 |
| 121 bool TestingPrefStore::GetBoolean(const std::string& key, bool* value) const { | 140 bool TestingPrefStore::GetBoolean(const std::string& key, bool* value) const { |
| 122 const base::Value* stored_value; | 141 const base::Value* stored_value; |
| 123 if (!prefs_.GetValue(key, &stored_value) || !stored_value) | 142 if (!prefs_.GetValue(key, &stored_value) || !stored_value) |
| 124 return false; | 143 return false; |
| 125 | 144 |
| 126 return stored_value->GetAsBoolean(value); | 145 return stored_value->GetAsBoolean(value); |
| 127 } | 146 } |
| 128 | 147 |
| 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 | |
| 129 void TestingPrefStore::set_read_only(bool read_only) { | 155 void TestingPrefStore::set_read_only(bool read_only) { |
| 130 read_only_ = read_only; | 156 read_only_ = read_only; |
| 131 } | 157 } |
| 132 | 158 |
| 159 void TestingPrefStore::set_read_success(bool read_success) { | |
| 160 DCHECK(!init_complete_); | |
| 161 read_success_ = read_success; | |
| 162 } | |
| 163 | |
| 164 void TestingPrefStore::set_read_error( | |
| 165 PersistentPrefStore::PrefReadError read_error) { | |
| 166 DCHECK(!init_complete_); | |
| 167 read_error_ = read_error; | |
| 168 } | |
| 169 | |
| 133 TestingPrefStore::~TestingPrefStore() {} | 170 TestingPrefStore::~TestingPrefStore() {} |
| OLD | NEW |