| 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::SetValue(const std::string& key, Value* value) { |
| 20 if (prefs_.SetValue(key, value)) |
| 21 NotifyPrefValueChanged(key); |
| 22 } |
| 23 |
| 24 void TestingPrefStore::SetValueSilently(const std::string& key, Value* value) { |
| 25 prefs_.SetValue(key, value); |
| 26 } |
| 27 |
| 28 void TestingPrefStore::RemoveValue(const std::string& key) { |
| 29 if (prefs_.RemoveValue(key)) |
| 30 NotifyPrefValueChanged(key); |
| 31 } |
| 32 |
| 33 PersistentPrefStore::PrefReadError TestingPrefStore::ReadPrefs() { |
| 34 prefs_.Clear(); |
| 35 return PersistentPrefStore::PREF_READ_ERROR_NONE; |
| 18 } | 36 } |
| 19 | 37 |
| 20 bool TestingPrefStore::WritePrefs() { | 38 bool TestingPrefStore::WritePrefs() { |
| 21 prefs_written_ = true; | 39 prefs_written_ = true; |
| 22 return prefs_written_; | 40 return prefs_written_; |
| 23 } | 41 } |
| 24 | 42 |
| 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() { | 43 void TestingPrefStore::SetInitializationCompleted() { |
| 34 init_complete_ = true; | 44 init_complete_ = true; |
| 35 NotifyInitializationCompleted(); | 45 NotifyInitializationCompleted(); |
| 36 } | 46 } |
| 47 |
| 48 void TestingPrefStore::NotifyPrefValueChanged(const std::string& key) { |
| 49 FOR_EACH_OBSERVER(ObserverInterface, observers_, OnPrefValueChanged(key)); |
| 50 } |
| 51 |
| 52 void TestingPrefStore::NotifyInitializationCompleted() { |
| 53 FOR_EACH_OBSERVER(ObserverInterface, observers_, OnInitializationCompleted()); |
| 54 } |
| 55 |
| 56 void TestingPrefStore::AddObserver(PrefStore::ObserverInterface* observer) { |
| 57 observers_.AddObserver(observer); |
| 58 } |
| 59 |
| 60 void TestingPrefStore::RemoveObserver(PrefStore::ObserverInterface* observer) { |
| 61 observers_.RemoveObserver(observer); |
| 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) { |
| 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) { |
| 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) { |
| 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 |