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/leveldb_value_store.h" | 5 #include "extensions/browser/value_store/leveldb_value_store.h" |
6 | 6 |
7 #include <inttypes.h> | 7 #include <inttypes.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
13 #include "base/json/json_reader.h" | 13 #include "base/json/json_reader.h" |
14 #include "base/json/json_writer.h" | 14 #include "base/json/json_writer.h" |
15 #include "base/logging.h" | 15 #include "base/logging.h" |
16 #include "base/macros.h" | 16 #include "base/macros.h" |
17 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
18 #include "base/strings/string_util.h" | 18 #include "base/strings/string_util.h" |
19 #include "base/strings/stringprintf.h" | 19 #include "base/strings/stringprintf.h" |
20 #include "base/strings/sys_string_conversions.h" | 20 #include "base/strings/sys_string_conversions.h" |
21 #include "base/threading/thread_task_runner_handle.h" | 21 #include "base/threading/sequenced_task_runner_handle.h" |
| 22 #include "base/threading/thread_restrictions.h" |
22 #include "base/trace_event/memory_dump_manager.h" | 23 #include "base/trace_event/memory_dump_manager.h" |
23 #include "base/trace_event/process_memory_dump.h" | 24 #include "base/trace_event/process_memory_dump.h" |
24 #include "content/public/browser/browser_thread.h" | |
25 #include "third_party/leveldatabase/env_chromium.h" | 25 #include "third_party/leveldatabase/env_chromium.h" |
26 #include "third_party/leveldatabase/src/include/leveldb/iterator.h" | 26 #include "third_party/leveldatabase/src/include/leveldb/iterator.h" |
27 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" | 27 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" |
28 | 28 |
29 using base::StringPiece; | 29 using base::StringPiece; |
30 using content::BrowserThread; | |
31 | 30 |
32 namespace { | 31 namespace { |
33 | 32 |
34 const char kInvalidJson[] = "Invalid JSON"; | 33 const char kInvalidJson[] = "Invalid JSON"; |
35 const char kCannotSerialize[] = "Cannot serialize value to JSON"; | 34 const char kCannotSerialize[] = "Cannot serialize value to JSON"; |
36 | 35 |
37 } // namespace | 36 } // namespace |
38 | 37 |
39 LeveldbValueStore::LeveldbValueStore(const std::string& uma_client_name, | 38 LeveldbValueStore::LeveldbValueStore(const std::string& uma_client_name, |
40 const base::FilePath& db_path) | 39 const base::FilePath& db_path) |
41 : LazyLevelDb(uma_client_name, db_path) { | 40 : LazyLevelDb(uma_client_name, db_path) { |
42 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( | 41 base::trace_event::MemoryDumpManager::GetInstance() |
43 this, "LeveldbValueStore", base::ThreadTaskRunnerHandle::Get()); | 42 ->RegisterDumpProviderWithSequencedTaskRunner( |
| 43 this, "LeveldbValueStore", base::SequencedTaskRunnerHandle::Get(), |
| 44 base::trace_event::MemoryDumpProvider::Options()); |
44 } | 45 } |
45 | 46 |
46 LeveldbValueStore::~LeveldbValueStore() { | 47 LeveldbValueStore::~LeveldbValueStore() { |
47 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 48 base::ThreadRestrictions::AssertIOAllowed(); |
48 base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider( | 49 base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider( |
49 this); | 50 this); |
50 } | 51 } |
51 | 52 |
52 size_t LeveldbValueStore::GetBytesInUse(const std::string& key) { | 53 size_t LeveldbValueStore::GetBytesInUse(const std::string& key) { |
53 // Let SettingsStorageQuotaEnforcer implement this. | 54 // Let SettingsStorageQuotaEnforcer implement this. |
54 NOTREACHED() << "Not implemented"; | 55 NOTREACHED() << "Not implemented"; |
55 return 0; | 56 return 0; |
56 } | 57 } |
57 | 58 |
58 size_t LeveldbValueStore::GetBytesInUse( | 59 size_t LeveldbValueStore::GetBytesInUse( |
59 const std::vector<std::string>& keys) { | 60 const std::vector<std::string>& keys) { |
60 // Let SettingsStorageQuotaEnforcer implement this. | 61 // Let SettingsStorageQuotaEnforcer implement this. |
61 NOTREACHED() << "Not implemented"; | 62 NOTREACHED() << "Not implemented"; |
62 return 0; | 63 return 0; |
63 } | 64 } |
64 | 65 |
65 size_t LeveldbValueStore::GetBytesInUse() { | 66 size_t LeveldbValueStore::GetBytesInUse() { |
66 // Let SettingsStorageQuotaEnforcer implement this. | 67 // Let SettingsStorageQuotaEnforcer implement this. |
67 NOTREACHED() << "Not implemented"; | 68 NOTREACHED() << "Not implemented"; |
68 return 0; | 69 return 0; |
69 } | 70 } |
70 | 71 |
71 ValueStore::ReadResult LeveldbValueStore::Get(const std::string& key) { | 72 ValueStore::ReadResult LeveldbValueStore::Get(const std::string& key) { |
72 return Get(std::vector<std::string>(1, key)); | 73 return Get(std::vector<std::string>(1, key)); |
73 } | 74 } |
74 | 75 |
75 ValueStore::ReadResult LeveldbValueStore::Get( | 76 ValueStore::ReadResult LeveldbValueStore::Get( |
76 const std::vector<std::string>& keys) { | 77 const std::vector<std::string>& keys) { |
77 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 78 base::ThreadRestrictions::AssertIOAllowed(); |
78 | 79 |
79 Status status = EnsureDbIsOpen(); | 80 Status status = EnsureDbIsOpen(); |
80 if (!status.ok()) | 81 if (!status.ok()) |
81 return MakeReadResult(status); | 82 return MakeReadResult(status); |
82 | 83 |
83 std::unique_ptr<base::DictionaryValue> settings(new base::DictionaryValue()); | 84 std::unique_ptr<base::DictionaryValue> settings(new base::DictionaryValue()); |
84 | 85 |
85 for (const std::string& key : keys) { | 86 for (const std::string& key : keys) { |
86 std::unique_ptr<base::Value> setting; | 87 std::unique_ptr<base::Value> setting; |
87 status.Merge(Read(key, &setting)); | 88 status.Merge(Read(key, &setting)); |
88 if (!status.ok()) | 89 if (!status.ok()) |
89 return MakeReadResult(status); | 90 return MakeReadResult(status); |
90 if (setting) | 91 if (setting) |
91 settings->SetWithoutPathExpansion(key, std::move(setting)); | 92 settings->SetWithoutPathExpansion(key, std::move(setting)); |
92 } | 93 } |
93 | 94 |
94 return MakeReadResult(std::move(settings), status); | 95 return MakeReadResult(std::move(settings), status); |
95 } | 96 } |
96 | 97 |
97 ValueStore::ReadResult LeveldbValueStore::Get() { | 98 ValueStore::ReadResult LeveldbValueStore::Get() { |
98 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 99 base::ThreadRestrictions::AssertIOAllowed(); |
99 | 100 |
100 Status status = EnsureDbIsOpen(); | 101 Status status = EnsureDbIsOpen(); |
101 if (!status.ok()) | 102 if (!status.ok()) |
102 return MakeReadResult(status); | 103 return MakeReadResult(status); |
103 | 104 |
104 base::JSONReader json_reader; | 105 base::JSONReader json_reader; |
105 std::unique_ptr<base::DictionaryValue> settings(new base::DictionaryValue()); | 106 std::unique_ptr<base::DictionaryValue> settings(new base::DictionaryValue()); |
106 | 107 |
107 std::unique_ptr<leveldb::Iterator> it(db()->NewIterator(read_options())); | 108 std::unique_ptr<leveldb::Iterator> it(db()->NewIterator(read_options())); |
108 for (it->SeekToFirst(); it->Valid(); it->Next()) { | 109 for (it->SeekToFirst(); it->Valid(); it->Next()) { |
(...skipping 13 matching lines...) Expand all Loading... |
122 status.Merge(ToValueStoreError(it->status())); | 123 status.Merge(ToValueStoreError(it->status())); |
123 return MakeReadResult(status); | 124 return MakeReadResult(status); |
124 } | 125 } |
125 | 126 |
126 return MakeReadResult(std::move(settings), status); | 127 return MakeReadResult(std::move(settings), status); |
127 } | 128 } |
128 | 129 |
129 ValueStore::WriteResult LeveldbValueStore::Set(WriteOptions options, | 130 ValueStore::WriteResult LeveldbValueStore::Set(WriteOptions options, |
130 const std::string& key, | 131 const std::string& key, |
131 const base::Value& value) { | 132 const base::Value& value) { |
132 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 133 base::ThreadRestrictions::AssertIOAllowed(); |
133 | 134 |
134 Status status = EnsureDbIsOpen(); | 135 Status status = EnsureDbIsOpen(); |
135 if (!status.ok()) | 136 if (!status.ok()) |
136 return MakeWriteResult(status); | 137 return MakeWriteResult(status); |
137 | 138 |
138 leveldb::WriteBatch batch; | 139 leveldb::WriteBatch batch; |
139 std::unique_ptr<ValueStoreChangeList> changes(new ValueStoreChangeList()); | 140 std::unique_ptr<ValueStoreChangeList> changes(new ValueStoreChangeList()); |
140 status.Merge(AddToBatch(options, key, value, &batch, changes.get())); | 141 status.Merge(AddToBatch(options, key, value, &batch, changes.get())); |
141 if (!status.ok()) | 142 if (!status.ok()) |
142 return MakeWriteResult(status); | 143 return MakeWriteResult(status); |
143 | 144 |
144 status.Merge(WriteToDb(&batch)); | 145 status.Merge(WriteToDb(&batch)); |
145 return status.ok() ? MakeWriteResult(std::move(changes), status) | 146 return status.ok() ? MakeWriteResult(std::move(changes), status) |
146 : MakeWriteResult(status); | 147 : MakeWriteResult(status); |
147 } | 148 } |
148 | 149 |
149 ValueStore::WriteResult LeveldbValueStore::Set( | 150 ValueStore::WriteResult LeveldbValueStore::Set( |
150 WriteOptions options, | 151 WriteOptions options, |
151 const base::DictionaryValue& settings) { | 152 const base::DictionaryValue& settings) { |
152 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 153 base::ThreadRestrictions::AssertIOAllowed(); |
153 | 154 |
154 Status status = EnsureDbIsOpen(); | 155 Status status = EnsureDbIsOpen(); |
155 if (!status.ok()) | 156 if (!status.ok()) |
156 return MakeWriteResult(status); | 157 return MakeWriteResult(status); |
157 | 158 |
158 leveldb::WriteBatch batch; | 159 leveldb::WriteBatch batch; |
159 std::unique_ptr<ValueStoreChangeList> changes(new ValueStoreChangeList()); | 160 std::unique_ptr<ValueStoreChangeList> changes(new ValueStoreChangeList()); |
160 | 161 |
161 for (base::DictionaryValue::Iterator it(settings); | 162 for (base::DictionaryValue::Iterator it(settings); |
162 !it.IsAtEnd(); it.Advance()) { | 163 !it.IsAtEnd(); it.Advance()) { |
163 status.Merge( | 164 status.Merge( |
164 AddToBatch(options, it.key(), it.value(), &batch, changes.get())); | 165 AddToBatch(options, it.key(), it.value(), &batch, changes.get())); |
165 if (!status.ok()) | 166 if (!status.ok()) |
166 return MakeWriteResult(status); | 167 return MakeWriteResult(status); |
167 } | 168 } |
168 | 169 |
169 status.Merge(WriteToDb(&batch)); | 170 status.Merge(WriteToDb(&batch)); |
170 return status.ok() ? MakeWriteResult(std::move(changes), status) | 171 return status.ok() ? MakeWriteResult(std::move(changes), status) |
171 : MakeWriteResult(status); | 172 : MakeWriteResult(status); |
172 } | 173 } |
173 | 174 |
174 ValueStore::WriteResult LeveldbValueStore::Remove(const std::string& key) { | 175 ValueStore::WriteResult LeveldbValueStore::Remove(const std::string& key) { |
175 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 176 base::ThreadRestrictions::AssertIOAllowed(); |
176 return Remove(std::vector<std::string>(1, key)); | 177 return Remove(std::vector<std::string>(1, key)); |
177 } | 178 } |
178 | 179 |
179 ValueStore::WriteResult LeveldbValueStore::Remove( | 180 ValueStore::WriteResult LeveldbValueStore::Remove( |
180 const std::vector<std::string>& keys) { | 181 const std::vector<std::string>& keys) { |
181 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 182 base::ThreadRestrictions::AssertIOAllowed(); |
182 | 183 |
183 Status status = EnsureDbIsOpen(); | 184 Status status = EnsureDbIsOpen(); |
184 if (!status.ok()) | 185 if (!status.ok()) |
185 return MakeWriteResult(status); | 186 return MakeWriteResult(status); |
186 | 187 |
187 leveldb::WriteBatch batch; | 188 leveldb::WriteBatch batch; |
188 std::unique_ptr<ValueStoreChangeList> changes(new ValueStoreChangeList()); | 189 std::unique_ptr<ValueStoreChangeList> changes(new ValueStoreChangeList()); |
189 | 190 |
190 for (const std::string& key : keys) { | 191 for (const std::string& key : keys) { |
191 std::unique_ptr<base::Value> old_value; | 192 std::unique_ptr<base::Value> old_value; |
192 status.Merge(Read(key, &old_value)); | 193 status.Merge(Read(key, &old_value)); |
193 if (!status.ok()) | 194 if (!status.ok()) |
194 return MakeWriteResult(status); | 195 return MakeWriteResult(status); |
195 | 196 |
196 if (old_value) { | 197 if (old_value) { |
197 changes->push_back(ValueStoreChange(key, std::move(old_value), nullptr)); | 198 changes->push_back(ValueStoreChange(key, std::move(old_value), nullptr)); |
198 batch.Delete(key); | 199 batch.Delete(key); |
199 } | 200 } |
200 } | 201 } |
201 | 202 |
202 leveldb::Status ldb_status = db()->Write(leveldb::WriteOptions(), &batch); | 203 leveldb::Status ldb_status = db()->Write(leveldb::WriteOptions(), &batch); |
203 if (!ldb_status.ok() && !ldb_status.IsNotFound()) { | 204 if (!ldb_status.ok() && !ldb_status.IsNotFound()) { |
204 status.Merge(ToValueStoreError(ldb_status)); | 205 status.Merge(ToValueStoreError(ldb_status)); |
205 return MakeWriteResult(status); | 206 return MakeWriteResult(status); |
206 } | 207 } |
207 return MakeWriteResult(std::move(changes), status); | 208 return MakeWriteResult(std::move(changes), status); |
208 } | 209 } |
209 | 210 |
210 ValueStore::WriteResult LeveldbValueStore::Clear() { | 211 ValueStore::WriteResult LeveldbValueStore::Clear() { |
211 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 212 base::ThreadRestrictions::AssertIOAllowed(); |
212 | 213 |
213 std::unique_ptr<ValueStoreChangeList> changes(new ValueStoreChangeList()); | 214 std::unique_ptr<ValueStoreChangeList> changes(new ValueStoreChangeList()); |
214 | 215 |
215 ReadResult read_result = Get(); | 216 ReadResult read_result = Get(); |
216 if (!read_result->status().ok()) | 217 if (!read_result->status().ok()) |
217 return MakeWriteResult(read_result->status()); | 218 return MakeWriteResult(read_result->status()); |
218 | 219 |
219 base::DictionaryValue& whole_db = read_result->settings(); | 220 base::DictionaryValue& whole_db = read_result->settings(); |
220 while (!whole_db.empty()) { | 221 while (!whole_db.empty()) { |
221 std::string next_key = base::DictionaryValue::Iterator(whole_db).key(); | 222 std::string next_key = base::DictionaryValue::Iterator(whole_db).key(); |
(...skipping 10 matching lines...) Expand all Loading... |
232 bool LeveldbValueStore::WriteToDbForTest(leveldb::WriteBatch* batch) { | 233 bool LeveldbValueStore::WriteToDbForTest(leveldb::WriteBatch* batch) { |
233 Status status = EnsureDbIsOpen(); | 234 Status status = EnsureDbIsOpen(); |
234 if (!status.ok()) | 235 if (!status.ok()) |
235 return false; | 236 return false; |
236 return WriteToDb(batch).ok(); | 237 return WriteToDb(batch).ok(); |
237 } | 238 } |
238 | 239 |
239 bool LeveldbValueStore::OnMemoryDump( | 240 bool LeveldbValueStore::OnMemoryDump( |
240 const base::trace_event::MemoryDumpArgs& args, | 241 const base::trace_event::MemoryDumpArgs& args, |
241 base::trace_event::ProcessMemoryDump* pmd) { | 242 base::trace_event::ProcessMemoryDump* pmd) { |
242 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 243 base::ThreadRestrictions::AssertIOAllowed(); |
243 | 244 |
244 // Return true so that the provider is not disabled. | 245 // Return true so that the provider is not disabled. |
245 if (!db()) | 246 if (!db()) |
246 return true; | 247 return true; |
247 | 248 |
248 std::string value; | 249 std::string value; |
249 uint64_t size; | 250 uint64_t size; |
250 bool res = db()->GetProperty("leveldb.approximate-memory-usage", &value); | 251 bool res = db()->GetProperty("leveldb.approximate-memory-usage", &value); |
251 DCHECK(res); | 252 DCHECK(res); |
252 res = base::StringToUint64(value, &size); | 253 res = base::StringToUint64(value, &size); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 return Status(OTHER_ERROR, kCannotSerialize); | 296 return Status(OTHER_ERROR, kCannotSerialize); |
296 batch->Put(key, value_as_json); | 297 batch->Put(key, value_as_json); |
297 } | 298 } |
298 | 299 |
299 return Status(); | 300 return Status(); |
300 } | 301 } |
301 | 302 |
302 ValueStore::Status LeveldbValueStore::WriteToDb(leveldb::WriteBatch* batch) { | 303 ValueStore::Status LeveldbValueStore::WriteToDb(leveldb::WriteBatch* batch) { |
303 return ToValueStoreError(db()->Write(write_options(), batch)); | 304 return ToValueStoreError(db()->Write(write_options(), batch)); |
304 } | 305 } |
OLD | NEW |