1 // Copyright (c) 2011 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 "chrome/browser/prefs/testing_pref_store.h" 6 7 #include "base/values.h" 8 9 TestingPrefStore::TestingPrefStore() 10 : read_only_(true), 11 prefs_written_(false), 12 init_complete_(false) {} 13 14 TestingPrefStore::~TestingPrefStore() {} 15 16 PrefStore::ReadResult TestingPrefStore::GetValue(const std::string& key, 17 const Value** value) const { 18 return prefs_.GetValue(key, value) ? READ_OK : READ_NO_VALUE; 19 } 20 21 PrefStore::ReadResult TestingPrefStore::GetMutableValue(const std::string& key, 22 Value** value) { 23 return prefs_.GetValue(key, value) ? READ_OK : READ_NO_VALUE; 24 } 25 26 void TestingPrefStore::AddObserver(PrefStore::Observer* observer) { 27 observers_.AddObserver(observer); 28 } 29 30 void TestingPrefStore::RemoveObserver(PrefStore::Observer* observer) { 31 observers_.RemoveObserver(observer); 32 } 33 34 bool TestingPrefStore::IsInitializationComplete() const { 35 return init_complete_; 36 } 37 38 void TestingPrefStore::SetValue(const std::string& key, Value* value) { 39 if (prefs_.SetValue(key, value)) 40 NotifyPrefValueChanged(key); 41 } 42 43 void TestingPrefStore::SetValueSilently(const std::string& key, Value* value) { 44 prefs_.SetValue(key, value); 45 } 46 47 void TestingPrefStore::RemoveValue(const std::string& key) { 48 if (prefs_.RemoveValue(key)) 49 NotifyPrefValueChanged(key); 50 } 51 52 bool TestingPrefStore::ReadOnly() const { 53 return read_only_; 54 } 55 56 PersistentPrefStore::PrefReadError TestingPrefStore::ReadPrefs() { 57 prefs_.Clear(); 58 return PersistentPrefStore::PREF_READ_ERROR_NONE; 59 } 60 61 bool TestingPrefStore::WritePrefs() { 62 prefs_written_ = true; 63 return prefs_written_; 64 } 65 66 void TestingPrefStore::SetInitializationCompleted() { 67 init_complete_ = true; 68 NotifyInitializationCompleted(); 69 } 70 71 void TestingPrefStore::NotifyPrefValueChanged(const std::string& key) { 72 FOR_EACH_OBSERVER(Observer, observers_, OnPrefValueChanged(key)); 73 } 74 75 void TestingPrefStore::NotifyInitializationCompleted() { 76 FOR_EACH_OBSERVER(Observer, observers_, OnInitializationCompleted()); 77 } 78 79 void TestingPrefStore::ReportValueChanged(const std::string& key) { 80 FOR_EACH_OBSERVER(Observer, observers_, OnPrefValueChanged(key)); 81 } 82 83 void TestingPrefStore::SetString(const std::string& key, 84 const std::string& value) { 85 SetValue(key, Value::CreateStringValue(value)); 86 } 87 88 void TestingPrefStore::SetInteger(const std::string& key, int value) { 89 SetValue(key, Value::CreateIntegerValue(value)); 90 } 91 92 void TestingPrefStore::SetBoolean(const std::string& key, bool value) { 93 SetValue(key, Value::CreateBooleanValue(value)); 94 } 95 96 bool TestingPrefStore::GetString(const std::string& key, 97 std::string* value) const { 98 const Value* stored_value; 99 if (!prefs_.GetValue(key, &stored_value) || !stored_value) 100 return false; 101 102 return stored_value->GetAsString(value); 103 } 104 105 bool TestingPrefStore::GetInteger(const std::string& key, int* value) const { 106 const Value* stored_value; 107 if (!prefs_.GetValue(key, &stored_value) || !stored_value) 108 return false; 109 110 return stored_value->GetAsInteger(value); 111 } 112 113 bool TestingPrefStore::GetBoolean(const std::string& key, bool* value) const { 114 const Value* stored_value; 115 if (!prefs_.GetValue(key, &stored_value) || !stored_value) 116 return false; 117 118 return stored_value->GetAsBoolean(value); 119 } 120 121 void TestingPrefStore::set_read_only(bool read_only) { 122 read_only_ = read_only; 123 } 124 125 void TestingPrefStore::set_prefs_written(bool status) { 126 prefs_written_ = status; 127 } 128 129 bool TestingPrefStore::get_prefs_written() { 130 return prefs_written_; 131 } 132