OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "extensions/browser/value_store/testing_value_store.h" | 5 #include "extensions/browser/value_store/testing_value_store.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/ptr_util.h" |
10 | 11 |
11 namespace { | 12 namespace { |
12 | 13 |
13 const char kGenericErrorMessage[] = "TestingValueStore configured to error"; | 14 const char kGenericErrorMessage[] = "TestingValueStore configured to error"; |
14 | 15 |
15 } // namespace | 16 } // namespace |
16 | 17 |
17 TestingValueStore::TestingValueStore() : read_count_(0), write_count_(0) {} | 18 TestingValueStore::TestingValueStore() : read_count_(0), write_count_(0) {} |
18 | 19 |
19 TestingValueStore::~TestingValueStore() {} | 20 TestingValueStore::~TestingValueStore() {} |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 return MakeReadResult(status_); | 53 return MakeReadResult(status_); |
53 | 54 |
54 base::DictionaryValue* settings = new base::DictionaryValue(); | 55 base::DictionaryValue* settings = new base::DictionaryValue(); |
55 for (std::vector<std::string>::const_iterator it = keys.begin(); | 56 for (std::vector<std::string>::const_iterator it = keys.begin(); |
56 it != keys.end(); ++it) { | 57 it != keys.end(); ++it) { |
57 base::Value* value = NULL; | 58 base::Value* value = NULL; |
58 if (storage_.GetWithoutPathExpansion(*it, &value)) { | 59 if (storage_.GetWithoutPathExpansion(*it, &value)) { |
59 settings->SetWithoutPathExpansion(*it, value->DeepCopy()); | 60 settings->SetWithoutPathExpansion(*it, value->DeepCopy()); |
60 } | 61 } |
61 } | 62 } |
62 return MakeReadResult(make_scoped_ptr(settings), status_); | 63 return MakeReadResult(base::WrapUnique(settings), status_); |
63 } | 64 } |
64 | 65 |
65 ValueStore::ReadResult TestingValueStore::Get() { | 66 ValueStore::ReadResult TestingValueStore::Get() { |
66 read_count_++; | 67 read_count_++; |
67 if (!status_.ok()) | 68 if (!status_.ok()) |
68 return MakeReadResult(status_); | 69 return MakeReadResult(status_); |
69 return MakeReadResult(make_scoped_ptr(storage_.DeepCopy()), status_); | 70 return MakeReadResult(base::WrapUnique(storage_.DeepCopy()), status_); |
70 } | 71 } |
71 | 72 |
72 ValueStore::WriteResult TestingValueStore::Set( | 73 ValueStore::WriteResult TestingValueStore::Set( |
73 WriteOptions options, const std::string& key, const base::Value& value) { | 74 WriteOptions options, const std::string& key, const base::Value& value) { |
74 base::DictionaryValue settings; | 75 base::DictionaryValue settings; |
75 settings.SetWithoutPathExpansion(key, value.DeepCopy()); | 76 settings.SetWithoutPathExpansion(key, value.DeepCopy()); |
76 return Set(options, settings); | 77 return Set(options, settings); |
77 } | 78 } |
78 | 79 |
79 ValueStore::WriteResult TestingValueStore::Set( | 80 ValueStore::WriteResult TestingValueStore::Set( |
80 WriteOptions options, const base::DictionaryValue& settings) { | 81 WriteOptions options, const base::DictionaryValue& settings) { |
81 write_count_++; | 82 write_count_++; |
82 if (!status_.ok()) | 83 if (!status_.ok()) |
83 return MakeWriteResult(status_); | 84 return MakeWriteResult(status_); |
84 | 85 |
85 scoped_ptr<ValueStoreChangeList> changes(new ValueStoreChangeList()); | 86 std::unique_ptr<ValueStoreChangeList> changes(new ValueStoreChangeList()); |
86 for (base::DictionaryValue::Iterator it(settings); | 87 for (base::DictionaryValue::Iterator it(settings); |
87 !it.IsAtEnd(); it.Advance()) { | 88 !it.IsAtEnd(); it.Advance()) { |
88 base::Value* old_value = NULL; | 89 base::Value* old_value = NULL; |
89 if (!storage_.GetWithoutPathExpansion(it.key(), &old_value) || | 90 if (!storage_.GetWithoutPathExpansion(it.key(), &old_value) || |
90 !old_value->Equals(&it.value())) { | 91 !old_value->Equals(&it.value())) { |
91 changes->push_back( | 92 changes->push_back( |
92 ValueStoreChange( | 93 ValueStoreChange( |
93 it.key(), | 94 it.key(), |
94 old_value ? old_value->DeepCopy() : old_value, | 95 old_value ? old_value->DeepCopy() : old_value, |
95 it.value().DeepCopy())); | 96 it.value().DeepCopy())); |
96 storage_.SetWithoutPathExpansion(it.key(), it.value().DeepCopy()); | 97 storage_.SetWithoutPathExpansion(it.key(), it.value().DeepCopy()); |
97 } | 98 } |
98 } | 99 } |
99 return MakeWriteResult(std::move(changes), status_); | 100 return MakeWriteResult(std::move(changes), status_); |
100 } | 101 } |
101 | 102 |
102 ValueStore::WriteResult TestingValueStore::Remove(const std::string& key) { | 103 ValueStore::WriteResult TestingValueStore::Remove(const std::string& key) { |
103 return Remove(std::vector<std::string>(1, key)); | 104 return Remove(std::vector<std::string>(1, key)); |
104 } | 105 } |
105 | 106 |
106 ValueStore::WriteResult TestingValueStore::Remove( | 107 ValueStore::WriteResult TestingValueStore::Remove( |
107 const std::vector<std::string>& keys) { | 108 const std::vector<std::string>& keys) { |
108 write_count_++; | 109 write_count_++; |
109 if (!status_.ok()) | 110 if (!status_.ok()) |
110 return MakeWriteResult(status_); | 111 return MakeWriteResult(status_); |
111 | 112 |
112 scoped_ptr<ValueStoreChangeList> changes(new ValueStoreChangeList()); | 113 std::unique_ptr<ValueStoreChangeList> changes(new ValueStoreChangeList()); |
113 for (std::vector<std::string>::const_iterator it = keys.begin(); | 114 for (std::vector<std::string>::const_iterator it = keys.begin(); |
114 it != keys.end(); ++it) { | 115 it != keys.end(); ++it) { |
115 scoped_ptr<base::Value> old_value; | 116 std::unique_ptr<base::Value> old_value; |
116 if (storage_.RemoveWithoutPathExpansion(*it, &old_value)) { | 117 if (storage_.RemoveWithoutPathExpansion(*it, &old_value)) { |
117 changes->push_back(ValueStoreChange(*it, old_value.release(), NULL)); | 118 changes->push_back(ValueStoreChange(*it, old_value.release(), NULL)); |
118 } | 119 } |
119 } | 120 } |
120 return MakeWriteResult(std::move(changes), status_); | 121 return MakeWriteResult(std::move(changes), status_); |
121 } | 122 } |
122 | 123 |
123 ValueStore::WriteResult TestingValueStore::Clear() { | 124 ValueStore::WriteResult TestingValueStore::Clear() { |
124 std::vector<std::string> keys; | 125 std::vector<std::string> keys; |
125 for (base::DictionaryValue::Iterator it(storage_); | 126 for (base::DictionaryValue::Iterator it(storage_); |
126 !it.IsAtEnd(); it.Advance()) { | 127 !it.IsAtEnd(); it.Advance()) { |
127 keys.push_back(it.key()); | 128 keys.push_back(it.key()); |
128 } | 129 } |
129 return Remove(keys); | 130 return Remove(keys); |
130 } | 131 } |
OLD | NEW |