1 // Copyright 2014 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.
5 #include "extensions/browser/value_store/testing_value_store.h"
7 #include "base/logging.h"
11 const char kGenericErrorMessage
[] = "TestingValueStore configured to error";
15 TestingValueStore::TestingValueStore()
16 : read_count_(0), write_count_(0), error_code_(OK
) {}
18 TestingValueStore::~TestingValueStore() {}
20 size_t TestingValueStore::GetBytesInUse(const std::string
& key
) {
21 // Let SettingsStorageQuotaEnforcer implement this.
22 NOTREACHED() << "Not implemented";
26 size_t TestingValueStore::GetBytesInUse(
27 const std::vector
<std::string
>& keys
) {
28 // Let SettingsStorageQuotaEnforcer implement this.
29 NOTREACHED() << "Not implemented";
33 size_t TestingValueStore::GetBytesInUse() {
34 // Let SettingsStorageQuotaEnforcer implement this.
35 NOTREACHED() << "Not implemented";
39 ValueStore::ReadResult
TestingValueStore::Get(const std::string
& key
) {
40 return Get(std::vector
<std::string
>(1, key
));
43 ValueStore::ReadResult
TestingValueStore::Get(
44 const std::vector
<std::string
>& keys
) {
46 if (error_code_
!= OK
)
47 return MakeReadResult(TestingError());
49 base::DictionaryValue
* settings
= new base::DictionaryValue();
50 for (std::vector
<std::string
>::const_iterator it
= keys
.begin();
51 it
!= keys
.end(); ++it
) {
52 base::Value
* value
= NULL
;
53 if (storage_
.GetWithoutPathExpansion(*it
, &value
)) {
54 settings
->SetWithoutPathExpansion(*it
, value
->DeepCopy());
57 return MakeReadResult(make_scoped_ptr(settings
));
60 ValueStore::ReadResult
TestingValueStore::Get() {
62 if (error_code_
!= OK
)
63 return MakeReadResult(TestingError());
64 return MakeReadResult(make_scoped_ptr(storage_
.DeepCopy()));
67 ValueStore::WriteResult
TestingValueStore::Set(
68 WriteOptions options
, const std::string
& key
, const base::Value
& value
) {
69 base::DictionaryValue settings
;
70 settings
.SetWithoutPathExpansion(key
, value
.DeepCopy());
71 return Set(options
, settings
);
74 ValueStore::WriteResult
TestingValueStore::Set(
75 WriteOptions options
, const base::DictionaryValue
& settings
) {
77 if (error_code_
!= OK
)
78 return MakeWriteResult(TestingError());
80 scoped_ptr
<ValueStoreChangeList
> changes(new ValueStoreChangeList());
81 for (base::DictionaryValue::Iterator
it(settings
);
82 !it
.IsAtEnd(); it
.Advance()) {
83 base::Value
* old_value
= NULL
;
84 if (!storage_
.GetWithoutPathExpansion(it
.key(), &old_value
) ||
85 !old_value
->Equals(&it
.value())) {
89 old_value
? old_value
->DeepCopy() : old_value
,
90 it
.value().DeepCopy()));
91 storage_
.SetWithoutPathExpansion(it
.key(), it
.value().DeepCopy());
94 return MakeWriteResult(changes
.Pass());
97 ValueStore::WriteResult
TestingValueStore::Remove(const std::string
& key
) {
98 return Remove(std::vector
<std::string
>(1, key
));
101 ValueStore::WriteResult
TestingValueStore::Remove(
102 const std::vector
<std::string
>& keys
) {
104 if (error_code_
!= OK
)
105 return MakeWriteResult(TestingError());
107 scoped_ptr
<ValueStoreChangeList
> changes(new ValueStoreChangeList());
108 for (std::vector
<std::string
>::const_iterator it
= keys
.begin();
109 it
!= keys
.end(); ++it
) {
110 scoped_ptr
<base::Value
> old_value
;
111 if (storage_
.RemoveWithoutPathExpansion(*it
, &old_value
)) {
112 changes
->push_back(ValueStoreChange(*it
, old_value
.release(), NULL
));
115 return MakeWriteResult(changes
.Pass());
118 ValueStore::WriteResult
TestingValueStore::Clear() {
119 std::vector
<std::string
> keys
;
120 for (base::DictionaryValue::Iterator
it(storage_
);
121 !it
.IsAtEnd(); it
.Advance()) {
122 keys
.push_back(it
.key());
127 bool TestingValueStore::Restore() {
131 bool TestingValueStore::RestoreKey(const std::string
& key
) {
135 scoped_ptr
<ValueStore::Error
> TestingValueStore::TestingError() {
136 return make_scoped_ptr(new ValueStore::Error(
137 error_code_
, kGenericErrorMessage
, scoped_ptr
<std::string
>()));