OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/browser/prefs/testing_pref_store.h" | 5 #include "chrome/browser/prefs/testing_pref_store.h" |
6 | 6 |
7 #include "base/values.h" | 7 #include "base/values.h" |
8 | 8 |
9 TestingPrefStore::TestingPrefStore() | 9 TestingPrefStore::TestingPrefStore() |
10 : prefs_(new DictionaryValue()), | 10 : read_only_(true), |
11 read_only_(true), | |
12 prefs_written_(false), | 11 prefs_written_(false), |
13 init_complete_(false) { } | 12 init_complete_(false) { } |
14 | 13 |
15 PrefStore::PrefReadError TestingPrefStore::ReadPrefs() { | 14 PrefStore::ReadResult TestingPrefStore::GetValue(const std::string& key, |
16 prefs_.reset(new DictionaryValue()); | 15 Value** value) const { |
17 return PrefStore::PREF_READ_ERROR_NONE; | 16 return prefs_.GetValue(key, value) ? READ_OK : READ_NO_VALUE; |
| 17 } |
| 18 |
| 19 void TestingPrefStore::AddObserver(PrefStore::Observer* observer) { |
| 20 observers_.AddObserver(observer); |
| 21 } |
| 22 |
| 23 void TestingPrefStore::RemoveObserver(PrefStore::Observer* observer) { |
| 24 observers_.RemoveObserver(observer); |
| 25 } |
| 26 |
| 27 void TestingPrefStore::SetValue(const std::string& key, Value* value) { |
| 28 if (prefs_.SetValue(key, value)) |
| 29 NotifyPrefValueChanged(key); |
| 30 } |
| 31 |
| 32 void TestingPrefStore::SetValueSilently(const std::string& key, Value* value) { |
| 33 prefs_.SetValue(key, value); |
| 34 } |
| 35 |
| 36 void TestingPrefStore::RemoveValue(const std::string& key) { |
| 37 if (prefs_.RemoveValue(key)) |
| 38 NotifyPrefValueChanged(key); |
| 39 } |
| 40 |
| 41 PersistentPrefStore::PrefReadError TestingPrefStore::ReadPrefs() { |
| 42 prefs_.Clear(); |
| 43 return PersistentPrefStore::PREF_READ_ERROR_NONE; |
18 } | 44 } |
19 | 45 |
20 bool TestingPrefStore::WritePrefs() { | 46 bool TestingPrefStore::WritePrefs() { |
21 prefs_written_ = true; | 47 prefs_written_ = true; |
22 return prefs_written_; | 48 return prefs_written_; |
23 } | 49 } |
24 | 50 |
25 void TestingPrefStore::NotifyPrefValueChanged(const std::string& key) { | |
26 PrefStoreBase::NotifyPrefValueChanged(key); | |
27 } | |
28 | |
29 void TestingPrefStore::NotifyInitializationCompleted() { | |
30 PrefStoreBase::NotifyInitializationCompleted(); | |
31 } | |
32 | |
33 void TestingPrefStore::SetInitializationCompleted() { | 51 void TestingPrefStore::SetInitializationCompleted() { |
34 init_complete_ = true; | 52 init_complete_ = true; |
35 NotifyInitializationCompleted(); | 53 NotifyInitializationCompleted(); |
36 } | 54 } |
| 55 |
| 56 void TestingPrefStore::NotifyPrefValueChanged(const std::string& key) { |
| 57 FOR_EACH_OBSERVER(Observer, observers_, OnPrefValueChanged(key)); |
| 58 } |
| 59 |
| 60 void TestingPrefStore::NotifyInitializationCompleted() { |
| 61 FOR_EACH_OBSERVER(Observer, observers_, OnInitializationCompleted()); |
| 62 } |
| 63 |
| 64 void TestingPrefStore::SetString(const std::string& key, |
| 65 const std::string& value) { |
| 66 SetValue(key, Value::CreateStringValue(value)); |
| 67 } |
| 68 |
| 69 void TestingPrefStore::SetInteger(const std::string& key, int value) { |
| 70 SetValue(key, Value::CreateIntegerValue(value)); |
| 71 } |
| 72 |
| 73 void TestingPrefStore::SetBoolean(const std::string& key, bool value) { |
| 74 SetValue(key, Value::CreateBooleanValue(value)); |
| 75 } |
| 76 |
| 77 bool TestingPrefStore::GetString(const std::string& key, |
| 78 std::string* value) const { |
| 79 Value* stored_value; |
| 80 if (!prefs_.GetValue(key, &stored_value) || !stored_value) |
| 81 return false; |
| 82 |
| 83 return stored_value->GetAsString(value); |
| 84 } |
| 85 |
| 86 bool TestingPrefStore::GetInteger(const std::string& key, int* value) const { |
| 87 Value* stored_value; |
| 88 if (!prefs_.GetValue(key, &stored_value) || !stored_value) |
| 89 return false; |
| 90 |
| 91 return stored_value->GetAsInteger(value); |
| 92 } |
| 93 |
| 94 bool TestingPrefStore::GetBoolean(const std::string& key, bool* value) const { |
| 95 Value* stored_value; |
| 96 if (!prefs_.GetValue(key, &stored_value) || !stored_value) |
| 97 return false; |
| 98 |
| 99 return stored_value->GetAsBoolean(value); |
| 100 } |
OLD | NEW |