| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/lazy_leveldb.h" | 5 #include "extensions/browser/value_store/lazy_leveldb.h" |
| 6 | 6 |
| 7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
| 8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
| 9 #include "base/memory/ptr_util.h" |
| 9 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 10 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
| 11 #include "third_party/leveldatabase/env_chromium.h" | 12 #include "third_party/leveldatabase/env_chromium.h" |
| 12 #include "third_party/leveldatabase/src/include/leveldb/iterator.h" | 13 #include "third_party/leveldatabase/src/include/leveldb/iterator.h" |
| 13 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" | 14 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" |
| 14 | 15 |
| 15 using base::StringPiece; | 16 using base::StringPiece; |
| 16 using content::BrowserThread; | 17 using content::BrowserThread; |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 LEVELDB_VALUE_RESTORE_MAX, LEVELDB_VALUE_RESTORE_MAX + 1, | 84 LEVELDB_VALUE_RESTORE_MAX, LEVELDB_VALUE_RESTORE_MAX + 1, |
| 84 base::Histogram::kUmaTargetedHistogramFlag); | 85 base::Histogram::kUmaTargetedHistogramFlag); |
| 85 } | 86 } |
| 86 | 87 |
| 87 LazyLevelDb::~LazyLevelDb() { | 88 LazyLevelDb::~LazyLevelDb() { |
| 88 if (db_ && !BrowserThread::CurrentlyOn(BrowserThread::FILE)) | 89 if (db_ && !BrowserThread::CurrentlyOn(BrowserThread::FILE)) |
| 89 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, db_.release()); | 90 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, db_.release()); |
| 90 } | 91 } |
| 91 | 92 |
| 92 ValueStore::Status LazyLevelDb::Read(const std::string& key, | 93 ValueStore::Status LazyLevelDb::Read(const std::string& key, |
| 93 scoped_ptr<base::Value>* value) { | 94 std::unique_ptr<base::Value>* value) { |
| 94 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 95 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 95 DCHECK(value); | 96 DCHECK(value); |
| 96 | 97 |
| 97 std::string value_as_json; | 98 std::string value_as_json; |
| 98 leveldb::Status s = db_->Get(read_options_, key, &value_as_json); | 99 leveldb::Status s = db_->Get(read_options_, key, &value_as_json); |
| 99 | 100 |
| 100 if (s.IsNotFound()) { | 101 if (s.IsNotFound()) { |
| 101 // Despite there being no value, it was still a success. Check this first | 102 // Despite there being no value, it was still a success. Check this first |
| 102 // because ok() is false on IsNotFound. | 103 // because ok() is false on IsNotFound. |
| 103 return ValueStore::Status(); | 104 return ValueStore::Status(); |
| 104 } | 105 } |
| 105 | 106 |
| 106 if (!s.ok()) | 107 if (!s.ok()) |
| 107 return ToValueStoreError(s); | 108 return ToValueStoreError(s); |
| 108 | 109 |
| 109 scoped_ptr<base::Value> val = base::JSONReader().ReadToValue(value_as_json); | 110 std::unique_ptr<base::Value> val = |
| 111 base::JSONReader().ReadToValue(value_as_json); |
| 110 if (!val) | 112 if (!val) |
| 111 return ValueStore::Status(ValueStore::CORRUPTION, FixCorruption(&key), | 113 return ValueStore::Status(ValueStore::CORRUPTION, FixCorruption(&key), |
| 112 kInvalidJson); | 114 kInvalidJson); |
| 113 | 115 |
| 114 *value = std::move(val); | 116 *value = std::move(val); |
| 115 return ValueStore::Status(); | 117 return ValueStore::Status(); |
| 116 } | 118 } |
| 117 | 119 |
| 118 ValueStore::Status LazyLevelDb::Delete(const std::string& key) { | 120 ValueStore::Status LazyLevelDb::Delete(const std::string& key) { |
| 119 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 121 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 db_.reset(); // release any lock on the directory | 269 db_.reset(); // release any lock on the directory |
| 268 if (!base::DeleteFile(db_path_, true /* recursive */)) { | 270 if (!base::DeleteFile(db_path_, true /* recursive */)) { |
| 269 LOG(WARNING) << "Failed to delete leveldb database at " << db_path_.value(); | 271 LOG(WARNING) << "Failed to delete leveldb database at " << db_path_.value(); |
| 270 return false; | 272 return false; |
| 271 } | 273 } |
| 272 return true; | 274 return true; |
| 273 } | 275 } |
| 274 | 276 |
| 275 ValueStore::Status LazyLevelDb::CreateIterator( | 277 ValueStore::Status LazyLevelDb::CreateIterator( |
| 276 const leveldb::ReadOptions& read_options, | 278 const leveldb::ReadOptions& read_options, |
| 277 scoped_ptr<leveldb::Iterator>* iterator) { | 279 std::unique_ptr<leveldb::Iterator>* iterator) { |
| 278 ValueStore::Status status = EnsureDbIsOpen(); | 280 ValueStore::Status status = EnsureDbIsOpen(); |
| 279 if (!status.ok()) | 281 if (!status.ok()) |
| 280 return status; | 282 return status; |
| 281 *iterator = make_scoped_ptr(db_->NewIterator(read_options)); | 283 *iterator = base::WrapUnique(db_->NewIterator(read_options)); |
| 282 return ValueStore::Status(); | 284 return ValueStore::Status(); |
| 283 } | 285 } |
| OLD | NEW |