Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / extensions / browser / value_store / leveldb_value_store.cc
blobad8076dfdaf792193262e64504dbe918a16f2644
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/leveldb_value_store.h"
7 #include "base/files/file_util.h"
8 #include "base/json/json_reader.h"
9 #include "base/json/json_writer.h"
10 #include "base/logging.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/strings/sys_string_conversions.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "extensions/browser/value_store/value_store_util.h"
16 #include "third_party/leveldatabase/env_chromium.h"
17 #include "third_party/leveldatabase/src/include/leveldb/iterator.h"
18 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h"
20 namespace util = value_store_util;
21 using content::BrowserThread;
23 namespace {
25 const char kInvalidJson[] = "Invalid JSON";
26 const char kCannotSerialize[] = "Cannot serialize value to JSON";
28 // Scoped leveldb snapshot which releases the snapshot on destruction.
29 class ScopedSnapshot {
30 public:
31 explicit ScopedSnapshot(leveldb::DB* db)
32 : db_(db), snapshot_(db->GetSnapshot()) {}
34 ~ScopedSnapshot() {
35 db_->ReleaseSnapshot(snapshot_);
38 const leveldb::Snapshot* get() {
39 return snapshot_;
42 private:
43 leveldb::DB* db_;
44 const leveldb::Snapshot* snapshot_;
46 DISALLOW_COPY_AND_ASSIGN(ScopedSnapshot);
49 } // namespace
51 LeveldbValueStore::LeveldbValueStore(const base::FilePath& db_path)
52 : db_path_(db_path) {
53 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
56 LeveldbValueStore::~LeveldbValueStore() {
57 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
59 // Delete the database from disk if it's empty (but only if we managed to
60 // open it!). This is safe on destruction, assuming that we have exclusive
61 // access to the database.
62 if (db_ && IsEmpty())
63 DeleteDbFile();
66 size_t LeveldbValueStore::GetBytesInUse(const std::string& key) {
67 // Let SettingsStorageQuotaEnforcer implement this.
68 NOTREACHED() << "Not implemented";
69 return 0;
72 size_t LeveldbValueStore::GetBytesInUse(
73 const std::vector<std::string>& keys) {
74 // Let SettingsStorageQuotaEnforcer implement this.
75 NOTREACHED() << "Not implemented";
76 return 0;
79 size_t LeveldbValueStore::GetBytesInUse() {
80 // Let SettingsStorageQuotaEnforcer implement this.
81 NOTREACHED() << "Not implemented";
82 return 0;
85 ValueStore::ReadResult LeveldbValueStore::Get(const std::string& key) {
86 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
88 scoped_ptr<Error> open_error = EnsureDbIsOpen();
89 if (open_error)
90 return MakeReadResult(open_error.Pass());
92 scoped_ptr<base::Value> setting;
93 scoped_ptr<Error> error = ReadFromDb(leveldb::ReadOptions(), key, &setting);
94 if (error)
95 return MakeReadResult(error.Pass());
97 base::DictionaryValue* settings = new base::DictionaryValue();
98 if (setting)
99 settings->SetWithoutPathExpansion(key, setting.release());
100 return MakeReadResult(make_scoped_ptr(settings));
103 ValueStore::ReadResult LeveldbValueStore::Get(
104 const std::vector<std::string>& keys) {
105 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
107 scoped_ptr<Error> open_error = EnsureDbIsOpen();
108 if (open_error)
109 return MakeReadResult(open_error.Pass());
111 leveldb::ReadOptions options;
112 scoped_ptr<base::DictionaryValue> settings(new base::DictionaryValue());
114 // All interaction with the db is done on the same thread, so snapshotting
115 // isn't strictly necessary. This is just defensive.
116 ScopedSnapshot snapshot(db_.get());
117 options.snapshot = snapshot.get();
118 for (std::vector<std::string>::const_iterator it = keys.begin();
119 it != keys.end(); ++it) {
120 scoped_ptr<base::Value> setting;
121 scoped_ptr<Error> error = ReadFromDb(options, *it, &setting);
122 if (error)
123 return MakeReadResult(error.Pass());
124 if (setting)
125 settings->SetWithoutPathExpansion(*it, setting.release());
128 return MakeReadResult(settings.Pass());
131 ValueStore::ReadResult LeveldbValueStore::Get() {
132 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
134 scoped_ptr<Error> open_error = EnsureDbIsOpen();
135 if (open_error)
136 return MakeReadResult(open_error.Pass());
138 base::JSONReader json_reader;
139 leveldb::ReadOptions options = leveldb::ReadOptions();
140 // All interaction with the db is done on the same thread, so snapshotting
141 // isn't strictly necessary. This is just defensive.
142 scoped_ptr<base::DictionaryValue> settings(new base::DictionaryValue());
144 ScopedSnapshot snapshot(db_.get());
145 options.snapshot = snapshot.get();
146 scoped_ptr<leveldb::Iterator> it(db_->NewIterator(options));
147 for (it->SeekToFirst(); it->Valid(); it->Next()) {
148 std::string key = it->key().ToString();
149 base::Value* value = json_reader.ReadToValue(it->value().ToString());
150 if (!value) {
151 return MakeReadResult(
152 Error::Create(CORRUPTION, kInvalidJson, util::NewKey(key)));
154 settings->SetWithoutPathExpansion(key, value);
157 if (it->status().IsNotFound()) {
158 NOTREACHED() << "IsNotFound() but iterating over all keys?!";
159 return MakeReadResult(settings.Pass());
162 if (!it->status().ok())
163 return MakeReadResult(ToValueStoreError(it->status(), util::NoKey()));
165 return MakeReadResult(settings.Pass());
168 ValueStore::WriteResult LeveldbValueStore::Set(
169 WriteOptions options, const std::string& key, const base::Value& value) {
170 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
172 scoped_ptr<Error> open_error = EnsureDbIsOpen();
173 if (open_error)
174 return MakeWriteResult(open_error.Pass());
176 leveldb::WriteBatch batch;
177 scoped_ptr<ValueStoreChangeList> changes(new ValueStoreChangeList());
178 scoped_ptr<Error> batch_error =
179 AddToBatch(options, key, value, &batch, changes.get());
180 if (batch_error)
181 return MakeWriteResult(batch_error.Pass());
183 scoped_ptr<Error> write_error = WriteToDb(&batch);
184 return write_error ? MakeWriteResult(write_error.Pass())
185 : MakeWriteResult(changes.Pass());
188 ValueStore::WriteResult LeveldbValueStore::Set(
189 WriteOptions options, const base::DictionaryValue& settings) {
190 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
192 scoped_ptr<Error> open_error = EnsureDbIsOpen();
193 if (open_error)
194 return MakeWriteResult(open_error.Pass());
196 leveldb::WriteBatch batch;
197 scoped_ptr<ValueStoreChangeList> changes(new ValueStoreChangeList());
199 for (base::DictionaryValue::Iterator it(settings);
200 !it.IsAtEnd(); it.Advance()) {
201 scoped_ptr<Error> batch_error =
202 AddToBatch(options, it.key(), it.value(), &batch, changes.get());
203 if (batch_error)
204 return MakeWriteResult(batch_error.Pass());
207 scoped_ptr<Error> write_error = WriteToDb(&batch);
208 return write_error ? MakeWriteResult(write_error.Pass())
209 : MakeWriteResult(changes.Pass());
212 ValueStore::WriteResult LeveldbValueStore::Remove(const std::string& key) {
213 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
214 return Remove(std::vector<std::string>(1, key));
217 ValueStore::WriteResult LeveldbValueStore::Remove(
218 const std::vector<std::string>& keys) {
219 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
221 scoped_ptr<Error> open_error = EnsureDbIsOpen();
222 if (open_error)
223 return MakeWriteResult(open_error.Pass());
225 leveldb::WriteBatch batch;
226 scoped_ptr<ValueStoreChangeList> changes(new ValueStoreChangeList());
228 for (std::vector<std::string>::const_iterator it = keys.begin();
229 it != keys.end(); ++it) {
230 scoped_ptr<base::Value> old_value;
231 scoped_ptr<Error> read_error =
232 ReadFromDb(leveldb::ReadOptions(), *it, &old_value);
233 if (read_error)
234 return MakeWriteResult(read_error.Pass());
236 if (old_value) {
237 changes->push_back(ValueStoreChange(*it, old_value.release(), NULL));
238 batch.Delete(*it);
242 leveldb::Status status = db_->Write(leveldb::WriteOptions(), &batch);
243 if (!status.ok() && !status.IsNotFound())
244 return MakeWriteResult(ToValueStoreError(status, util::NoKey()));
245 return MakeWriteResult(changes.Pass());
248 ValueStore::WriteResult LeveldbValueStore::Clear() {
249 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
251 scoped_ptr<ValueStoreChangeList> changes(new ValueStoreChangeList());
253 ReadResult read_result = Get();
254 if (read_result->HasError())
255 return MakeWriteResult(read_result->PassError());
257 base::DictionaryValue& whole_db = read_result->settings();
258 while (!whole_db.empty()) {
259 std::string next_key = base::DictionaryValue::Iterator(whole_db).key();
260 scoped_ptr<base::Value> next_value;
261 whole_db.RemoveWithoutPathExpansion(next_key, &next_value);
262 changes->push_back(
263 ValueStoreChange(next_key, next_value.release(), NULL));
266 DeleteDbFile();
267 return MakeWriteResult(changes.Pass());
270 bool LeveldbValueStore::Restore() {
271 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
273 ReadResult result = Get();
274 std::string previous_key;
275 while (result->IsCorrupted()) {
276 // If we don't have a specific corrupted key, or we've tried and failed to
277 // clear this specific key, or we fail to restore the key, then wipe the
278 // whole database.
279 if (!result->error().key.get() || *result->error().key == previous_key ||
280 !RestoreKey(*result->error().key)) {
281 DeleteDbFile();
282 result = Get();
283 break;
286 // Otherwise, re-Get() the database to check if there is still any
287 // corruption.
288 previous_key = *result->error().key;
289 result = Get();
292 // If we still have an error, it means we've tried deleting the database file,
293 // and failed. There's nothing more we can do.
294 return !result->IsCorrupted();
297 bool LeveldbValueStore::RestoreKey(const std::string& key) {
298 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
300 ReadResult result = Get(key);
301 if (result->IsCorrupted()) {
302 leveldb::WriteBatch batch;
303 batch.Delete(key);
304 scoped_ptr<ValueStore::Error> error = WriteToDb(&batch);
305 // If we can't delete the key, the restore failed.
306 if (error.get())
307 return false;
308 result = Get(key);
311 // The restore succeeded if there is no corruption error.
312 return !result->IsCorrupted();
315 bool LeveldbValueStore::WriteToDbForTest(leveldb::WriteBatch* batch) {
316 return !WriteToDb(batch).get();
319 scoped_ptr<ValueStore::Error> LeveldbValueStore::EnsureDbIsOpen() {
320 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
322 if (db_)
323 return util::NoError();
325 leveldb::Options options;
326 options.max_open_files = 0; // Use minimum.
327 options.create_if_missing = true;
328 options.reuse_logs = leveldb_env::kDefaultLogReuseOptionValue;
330 leveldb::DB* db = NULL;
331 leveldb::Status status =
332 leveldb::DB::Open(options, db_path_.AsUTF8Unsafe(), &db);
333 if (!status.ok())
334 return ToValueStoreError(status, util::NoKey());
336 CHECK(db);
337 db_.reset(db);
338 return util::NoError();
341 scoped_ptr<ValueStore::Error> LeveldbValueStore::ReadFromDb(
342 leveldb::ReadOptions options,
343 const std::string& key,
344 scoped_ptr<base::Value>* setting) {
345 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
346 DCHECK(setting);
348 std::string value_as_json;
349 leveldb::Status s = db_->Get(options, key, &value_as_json);
351 if (s.IsNotFound()) {
352 // Despite there being no value, it was still a success. Check this first
353 // because ok() is false on IsNotFound.
354 return util::NoError();
357 if (!s.ok())
358 return ToValueStoreError(s, util::NewKey(key));
360 base::Value* value = base::JSONReader().ReadToValue(value_as_json);
361 if (!value)
362 return Error::Create(CORRUPTION, kInvalidJson, util::NewKey(key));
364 setting->reset(value);
365 return util::NoError();
368 scoped_ptr<ValueStore::Error> LeveldbValueStore::AddToBatch(
369 ValueStore::WriteOptions options,
370 const std::string& key,
371 const base::Value& value,
372 leveldb::WriteBatch* batch,
373 ValueStoreChangeList* changes) {
374 bool write_new_value = true;
376 if (!(options & NO_GENERATE_CHANGES)) {
377 scoped_ptr<base::Value> old_value;
378 scoped_ptr<Error> read_error =
379 ReadFromDb(leveldb::ReadOptions(), key, &old_value);
380 if (read_error)
381 return read_error.Pass();
382 if (!old_value || !old_value->Equals(&value)) {
383 changes->push_back(
384 ValueStoreChange(key, old_value.release(), value.DeepCopy()));
385 } else {
386 write_new_value = false;
390 if (write_new_value) {
391 std::string value_as_json;
392 if (!base::JSONWriter::Write(&value, &value_as_json))
393 return Error::Create(OTHER_ERROR, kCannotSerialize, util::NewKey(key));
394 batch->Put(key, value_as_json);
397 return util::NoError();
400 scoped_ptr<ValueStore::Error> LeveldbValueStore::WriteToDb(
401 leveldb::WriteBatch* batch) {
402 leveldb::Status status = db_->Write(leveldb::WriteOptions(), batch);
403 return status.ok() ? util::NoError()
404 : ToValueStoreError(status, util::NoKey());
407 bool LeveldbValueStore::IsEmpty() {
408 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
409 scoped_ptr<leveldb::Iterator> it(db_->NewIterator(leveldb::ReadOptions()));
411 it->SeekToFirst();
412 bool is_empty = !it->Valid();
413 if (!it->status().ok()) {
414 LOG(ERROR) << "Checking DB emptiness failed: " << it->status().ToString();
415 return false;
417 return is_empty;
420 void LeveldbValueStore::DeleteDbFile() {
421 db_.reset(); // release any lock on the directory
422 if (!base::DeleteFile(db_path_, true /* recursive */)) {
423 LOG(WARNING) << "Failed to delete LeveldbValueStore database at " <<
424 db_path_.value();
428 scoped_ptr<ValueStore::Error> LeveldbValueStore::ToValueStoreError(
429 const leveldb::Status& status,
430 scoped_ptr<std::string> key) {
431 CHECK(!status.ok());
432 CHECK(!status.IsNotFound()); // not an error
434 std::string message = status.ToString();
435 // The message may contain |db_path_|, which may be considered sensitive
436 // data, and those strings are passed to the extension, so strip it out.
437 ReplaceSubstringsAfterOffset(&message, 0u, db_path_.AsUTF8Unsafe(), "...");
439 return Error::Create(CORRUPTION, message, key.Pass());