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