| 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/leveldb_scoped_database.h" | 5 #include "extensions/browser/value_store/leveldb_scoped_database.h" |
| 6 | 6 |
| 7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
| 8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
| 9 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
| 10 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" | 10 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 *scoped_key = scope + kKeyDelimiter + key; | 42 *scoped_key = scope + kKeyDelimiter + key; |
| 43 return true; | 43 return true; |
| 44 } | 44 } |
| 45 | 45 |
| 46 LeveldbScopedDatabase::LeveldbScopedDatabase(const std::string& uma_client_name, | 46 LeveldbScopedDatabase::LeveldbScopedDatabase(const std::string& uma_client_name, |
| 47 const base::FilePath& path) | 47 const base::FilePath& path) |
| 48 : LazyLevelDb(uma_client_name, path) {} | 48 : LazyLevelDb(uma_client_name, path) {} |
| 49 | 49 |
| 50 LeveldbScopedDatabase::~LeveldbScopedDatabase() {} | 50 LeveldbScopedDatabase::~LeveldbScopedDatabase() {} |
| 51 | 51 |
| 52 ValueStore::Status LeveldbScopedDatabase::Read(const std::string& scope, | 52 ValueStore::Status LeveldbScopedDatabase::Read( |
| 53 const std::string& key, | 53 const std::string& scope, |
| 54 scoped_ptr<base::Value>* value) { | 54 const std::string& key, |
| 55 std::unique_ptr<base::Value>* value) { |
| 55 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 56 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 56 | 57 |
| 57 ValueStore::Status status = EnsureDbIsOpen(); | 58 ValueStore::Status status = EnsureDbIsOpen(); |
| 58 if (!status.ok()) | 59 if (!status.ok()) |
| 59 return status; | 60 return status; |
| 60 std::string scoped_key; | 61 std::string scoped_key; |
| 61 if (!CreateKey(scope, key, &scoped_key)) | 62 if (!CreateKey(scope, key, &scoped_key)) |
| 62 return ValueStore::Status(ValueStore::OTHER_ERROR, kInvalidScope); | 63 return ValueStore::Status(ValueStore::OTHER_ERROR, kInvalidScope); |
| 63 | 64 |
| 64 return LazyLevelDb::Read(scoped_key, value); | 65 return LazyLevelDb::Read(scoped_key, value); |
| 65 } | 66 } |
| 66 | 67 |
| 67 ValueStore::Status LeveldbScopedDatabase::Read(const std::string& scope, | 68 ValueStore::Status LeveldbScopedDatabase::Read(const std::string& scope, |
| 68 base::DictionaryValue* values) { | 69 base::DictionaryValue* values) { |
| 69 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 70 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 70 | 71 |
| 71 ValueStore::Status status = EnsureDbIsOpen(); | 72 ValueStore::Status status = EnsureDbIsOpen(); |
| 72 if (!status.ok()) | 73 if (!status.ok()) |
| 73 return status; | 74 return status; |
| 74 | 75 |
| 75 std::string prefix; | 76 std::string prefix; |
| 76 if (!CreateKey(scope, "", &prefix)) | 77 if (!CreateKey(scope, "", &prefix)) |
| 77 return ValueStore::Status(ValueStore::OTHER_ERROR, kInvalidScope); | 78 return ValueStore::Status(ValueStore::OTHER_ERROR, kInvalidScope); |
| 78 | 79 |
| 79 scoped_ptr<leveldb::Iterator> it(db()->NewIterator(read_options())); | 80 std::unique_ptr<leveldb::Iterator> it(db()->NewIterator(read_options())); |
| 80 | 81 |
| 81 base::JSONReader json_reader; | 82 base::JSONReader json_reader; |
| 82 scoped_ptr<base::DictionaryValue> settings(new base::DictionaryValue()); | 83 std::unique_ptr<base::DictionaryValue> settings(new base::DictionaryValue()); |
| 83 | 84 |
| 84 for (it->Seek(prefix); it->Valid() && it->key().starts_with(prefix); | 85 for (it->Seek(prefix); it->Valid() && it->key().starts_with(prefix); |
| 85 it->Next()) { | 86 it->Next()) { |
| 86 leveldb::Slice descoped_key(it->key()); | 87 leveldb::Slice descoped_key(it->key()); |
| 87 descoped_key.remove_prefix(prefix.size()); | 88 descoped_key.remove_prefix(prefix.size()); |
| 88 scoped_ptr<base::Value> value = json_reader.Read( | 89 std::unique_ptr<base::Value> value = json_reader.Read( |
| 89 base::StringPiece(it->value().data(), it->value().size())); | 90 base::StringPiece(it->value().data(), it->value().size())); |
| 90 if (!value) { | 91 if (!value) { |
| 91 return ValueStore::Status(ValueStore::CORRUPTION, | 92 return ValueStore::Status(ValueStore::CORRUPTION, |
| 92 LazyLevelDb::Delete(it->key().ToString()).ok() | 93 LazyLevelDb::Delete(it->key().ToString()).ok() |
| 93 ? ValueStore::VALUE_RESTORE_DELETE_SUCCESS | 94 ? ValueStore::VALUE_RESTORE_DELETE_SUCCESS |
| 94 : ValueStore::VALUE_RESTORE_DELETE_FAILURE, | 95 : ValueStore::VALUE_RESTORE_DELETE_FAILURE, |
| 95 kInvalidJson); | 96 kInvalidJson); |
| 96 } | 97 } |
| 97 values->SetWithoutPathExpansion(descoped_key.ToString(), std::move(value)); | 98 values->SetWithoutPathExpansion(descoped_key.ToString(), std::move(value)); |
| 98 } | 99 } |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 if (!CreateKey(scope, key, &scoped_key)) | 160 if (!CreateKey(scope, key, &scoped_key)) |
| 160 return ValueStore::Status(ValueStore::OTHER_ERROR, kInvalidScope); | 161 return ValueStore::Status(ValueStore::OTHER_ERROR, kInvalidScope); |
| 161 | 162 |
| 162 std::string value_as_json; | 163 std::string value_as_json; |
| 163 if (!base::JSONWriter::Write(value, &value_as_json)) | 164 if (!base::JSONWriter::Write(value, &value_as_json)) |
| 164 return ValueStore::Status(ValueStore::OTHER_ERROR, kCannotSerialize); | 165 return ValueStore::Status(ValueStore::OTHER_ERROR, kCannotSerialize); |
| 165 | 166 |
| 166 batch->Put(scoped_key, value_as_json); | 167 batch->Put(scoped_key, value_as_json); |
| 167 return ValueStore::Status(); | 168 return ValueStore::Status(); |
| 168 } | 169 } |
| OLD | NEW |