testing_pref_store.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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
10TestingPrefStore::TestingPrefStore()
11    : read_only_(true),
12      init_complete_(false) {
13}
14
15bool TestingPrefStore::GetValue(const std::string& key,
16                                const Value** value) const {
17  return prefs_.GetValue(key, value);
18}
19
20bool TestingPrefStore::GetMutableValue(const std::string& key,
21                                       Value** value) {
22  return prefs_.GetValue(key, value);
23}
24
25void TestingPrefStore::AddObserver(PrefStore::Observer* observer) {
26  observers_.AddObserver(observer);
27}
28
29void TestingPrefStore::RemoveObserver(PrefStore::Observer* observer) {
30  observers_.RemoveObserver(observer);
31}
32
33size_t TestingPrefStore::NumberOfObservers() const {
34  return observers_.size();
35}
36
37bool TestingPrefStore::IsInitializationComplete() const {
38  return init_complete_;
39}
40
41void TestingPrefStore::SetValue(const std::string& key, Value* value) {
42  if (prefs_.SetValue(key, value))
43    NotifyPrefValueChanged(key);
44}
45
46void TestingPrefStore::SetValueSilently(const std::string& key, Value* value) {
47  prefs_.SetValue(key, value);
48}
49
50void TestingPrefStore::RemoveValue(const std::string& key) {
51  if (prefs_.RemoveValue(key))
52    NotifyPrefValueChanged(key);
53}
54
55void TestingPrefStore::MarkNeedsEmptyValue(const std::string& key) {
56}
57
58bool TestingPrefStore::ReadOnly() const {
59  return read_only_;
60}
61
62PersistentPrefStore::PrefReadError TestingPrefStore::GetReadError() const {
63  return PersistentPrefStore::PREF_READ_ERROR_NONE;
64}
65
66PersistentPrefStore::PrefReadError TestingPrefStore::ReadPrefs() {
67  NotifyInitializationCompleted();
68  return PersistentPrefStore::PREF_READ_ERROR_NONE;
69}
70
71void TestingPrefStore::ReadPrefsAsync(ReadErrorDelegate* error_delegate_raw) {
72  scoped_ptr<ReadErrorDelegate> error_delegate(error_delegate_raw);
73  NotifyInitializationCompleted();
74}
75
76void TestingPrefStore::SetInitializationCompleted() {
77  init_complete_ = true;
78  NotifyInitializationCompleted();
79}
80
81void TestingPrefStore::NotifyPrefValueChanged(const std::string& key) {
82  FOR_EACH_OBSERVER(Observer, observers_, OnPrefValueChanged(key));
83}
84
85void TestingPrefStore::NotifyInitializationCompleted() {
86  FOR_EACH_OBSERVER(Observer, observers_, OnInitializationCompleted(true));
87}
88
89void TestingPrefStore::ReportValueChanged(const std::string& key) {
90  FOR_EACH_OBSERVER(Observer, observers_, OnPrefValueChanged(key));
91}
92
93void TestingPrefStore::SetString(const std::string& key,
94                                 const std::string& value) {
95  SetValue(key, new base::StringValue(value));
96}
97
98void TestingPrefStore::SetInteger(const std::string& key, int value) {
99  SetValue(key, new base::FundamentalValue(value));
100}
101
102void TestingPrefStore::SetBoolean(const std::string& key, bool value) {
103  SetValue(key, new base::FundamentalValue(value));
104}
105
106bool TestingPrefStore::GetString(const std::string& key,
107                                 std::string* value) const {
108  const Value* stored_value;
109  if (!prefs_.GetValue(key, &stored_value) || !stored_value)
110    return false;
111
112  return stored_value->GetAsString(value);
113}
114
115bool TestingPrefStore::GetInteger(const std::string& key, int* value) const {
116  const Value* stored_value;
117  if (!prefs_.GetValue(key, &stored_value) || !stored_value)
118    return false;
119
120  return stored_value->GetAsInteger(value);
121}
122
123bool TestingPrefStore::GetBoolean(const std::string& key, bool* value) const {
124  const Value* stored_value;
125  if (!prefs_.GetValue(key, &stored_value) || !stored_value)
126    return false;
127
128  return stored_value->GetAsBoolean(value);
129}
130
131void TestingPrefStore::set_read_only(bool read_only) {
132  read_only_ = read_only;
133}
134
135TestingPrefStore::~TestingPrefStore() {}
136