| 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 <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 } | 72 } |
| 73 | 73 |
| 74 ValueStore::ReadResult LeveldbValueStore::Get( | 74 ValueStore::ReadResult LeveldbValueStore::Get( |
| 75 const std::vector<std::string>& keys) { | 75 const std::vector<std::string>& keys) { |
| 76 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 76 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 77 | 77 |
| 78 Status status = EnsureDbIsOpen(); | 78 Status status = EnsureDbIsOpen(); |
| 79 if (!status.ok()) | 79 if (!status.ok()) |
| 80 return MakeReadResult(status); | 80 return MakeReadResult(status); |
| 81 | 81 |
| 82 scoped_ptr<base::DictionaryValue> settings(new base::DictionaryValue()); | 82 std::unique_ptr<base::DictionaryValue> settings(new base::DictionaryValue()); |
| 83 | 83 |
| 84 for (const std::string& key : keys) { | 84 for (const std::string& key : keys) { |
| 85 scoped_ptr<base::Value> setting; | 85 std::unique_ptr<base::Value> setting; |
| 86 status.Merge(Read(key, &setting)); | 86 status.Merge(Read(key, &setting)); |
| 87 if (!status.ok()) | 87 if (!status.ok()) |
| 88 return MakeReadResult(status); | 88 return MakeReadResult(status); |
| 89 if (setting) | 89 if (setting) |
| 90 settings->SetWithoutPathExpansion(key, setting.release()); | 90 settings->SetWithoutPathExpansion(key, setting.release()); |
| 91 } | 91 } |
| 92 | 92 |
| 93 return MakeReadResult(std::move(settings), status); | 93 return MakeReadResult(std::move(settings), status); |
| 94 } | 94 } |
| 95 | 95 |
| 96 ValueStore::ReadResult LeveldbValueStore::Get() { | 96 ValueStore::ReadResult LeveldbValueStore::Get() { |
| 97 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 97 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 98 | 98 |
| 99 Status status = EnsureDbIsOpen(); | 99 Status status = EnsureDbIsOpen(); |
| 100 if (!status.ok()) | 100 if (!status.ok()) |
| 101 return MakeReadResult(status); | 101 return MakeReadResult(status); |
| 102 | 102 |
| 103 base::JSONReader json_reader; | 103 base::JSONReader json_reader; |
| 104 scoped_ptr<base::DictionaryValue> settings(new base::DictionaryValue()); | 104 std::unique_ptr<base::DictionaryValue> settings(new base::DictionaryValue()); |
| 105 | 105 |
| 106 scoped_ptr<leveldb::Iterator> it(db()->NewIterator(read_options())); | 106 std::unique_ptr<leveldb::Iterator> it(db()->NewIterator(read_options())); |
| 107 for (it->SeekToFirst(); it->Valid(); it->Next()) { | 107 for (it->SeekToFirst(); it->Valid(); it->Next()) { |
| 108 std::string key = it->key().ToString(); | 108 std::string key = it->key().ToString(); |
| 109 scoped_ptr<base::Value> value = | 109 std::unique_ptr<base::Value> value = |
| 110 json_reader.Read(StringPiece(it->value().data(), it->value().size())); | 110 json_reader.Read(StringPiece(it->value().data(), it->value().size())); |
| 111 if (!value) { | 111 if (!value) { |
| 112 return MakeReadResult( | 112 return MakeReadResult( |
| 113 Status(CORRUPTION, Delete(key).ok() ? VALUE_RESTORE_DELETE_SUCCESS | 113 Status(CORRUPTION, Delete(key).ok() ? VALUE_RESTORE_DELETE_SUCCESS |
| 114 : VALUE_RESTORE_DELETE_FAILURE, | 114 : VALUE_RESTORE_DELETE_FAILURE, |
| 115 kInvalidJson)); | 115 kInvalidJson)); |
| 116 } | 116 } |
| 117 settings->SetWithoutPathExpansion(key, std::move(value)); | 117 settings->SetWithoutPathExpansion(key, std::move(value)); |
| 118 } | 118 } |
| 119 | 119 |
| 120 if (!it->status().ok()) { | 120 if (!it->status().ok()) { |
| 121 status.Merge(ToValueStoreError(it->status())); | 121 status.Merge(ToValueStoreError(it->status())); |
| 122 return MakeReadResult(status); | 122 return MakeReadResult(status); |
| 123 } | 123 } |
| 124 | 124 |
| 125 return MakeReadResult(std::move(settings), status); | 125 return MakeReadResult(std::move(settings), status); |
| 126 } | 126 } |
| 127 | 127 |
| 128 ValueStore::WriteResult LeveldbValueStore::Set(WriteOptions options, | 128 ValueStore::WriteResult LeveldbValueStore::Set(WriteOptions options, |
| 129 const std::string& key, | 129 const std::string& key, |
| 130 const base::Value& value) { | 130 const base::Value& value) { |
| 131 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 131 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 132 | 132 |
| 133 Status status = EnsureDbIsOpen(); | 133 Status status = EnsureDbIsOpen(); |
| 134 if (!status.ok()) | 134 if (!status.ok()) |
| 135 return MakeWriteResult(status); | 135 return MakeWriteResult(status); |
| 136 | 136 |
| 137 leveldb::WriteBatch batch; | 137 leveldb::WriteBatch batch; |
| 138 scoped_ptr<ValueStoreChangeList> changes(new ValueStoreChangeList()); | 138 std::unique_ptr<ValueStoreChangeList> changes(new ValueStoreChangeList()); |
| 139 status.Merge(AddToBatch(options, key, value, &batch, changes.get())); | 139 status.Merge(AddToBatch(options, key, value, &batch, changes.get())); |
| 140 if (!status.ok()) | 140 if (!status.ok()) |
| 141 return MakeWriteResult(status); | 141 return MakeWriteResult(status); |
| 142 | 142 |
| 143 status.Merge(WriteToDb(&batch)); | 143 status.Merge(WriteToDb(&batch)); |
| 144 return status.ok() ? MakeWriteResult(std::move(changes), status) | 144 return status.ok() ? MakeWriteResult(std::move(changes), status) |
| 145 : MakeWriteResult(status); | 145 : MakeWriteResult(status); |
| 146 } | 146 } |
| 147 | 147 |
| 148 ValueStore::WriteResult LeveldbValueStore::Set( | 148 ValueStore::WriteResult LeveldbValueStore::Set( |
| 149 WriteOptions options, | 149 WriteOptions options, |
| 150 const base::DictionaryValue& settings) { | 150 const base::DictionaryValue& settings) { |
| 151 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 151 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 152 | 152 |
| 153 Status status = EnsureDbIsOpen(); | 153 Status status = EnsureDbIsOpen(); |
| 154 if (!status.ok()) | 154 if (!status.ok()) |
| 155 return MakeWriteResult(status); | 155 return MakeWriteResult(status); |
| 156 | 156 |
| 157 leveldb::WriteBatch batch; | 157 leveldb::WriteBatch batch; |
| 158 scoped_ptr<ValueStoreChangeList> changes(new ValueStoreChangeList()); | 158 std::unique_ptr<ValueStoreChangeList> changes(new ValueStoreChangeList()); |
| 159 | 159 |
| 160 for (base::DictionaryValue::Iterator it(settings); | 160 for (base::DictionaryValue::Iterator it(settings); |
| 161 !it.IsAtEnd(); it.Advance()) { | 161 !it.IsAtEnd(); it.Advance()) { |
| 162 status.Merge( | 162 status.Merge( |
| 163 AddToBatch(options, it.key(), it.value(), &batch, changes.get())); | 163 AddToBatch(options, it.key(), it.value(), &batch, changes.get())); |
| 164 if (!status.ok()) | 164 if (!status.ok()) |
| 165 return MakeWriteResult(status); | 165 return MakeWriteResult(status); |
| 166 } | 166 } |
| 167 | 167 |
| 168 status.Merge(WriteToDb(&batch)); | 168 status.Merge(WriteToDb(&batch)); |
| 169 return status.ok() ? MakeWriteResult(std::move(changes), status) | 169 return status.ok() ? MakeWriteResult(std::move(changes), status) |
| 170 : MakeWriteResult(status); | 170 : MakeWriteResult(status); |
| 171 } | 171 } |
| 172 | 172 |
| 173 ValueStore::WriteResult LeveldbValueStore::Remove(const std::string& key) { | 173 ValueStore::WriteResult LeveldbValueStore::Remove(const std::string& key) { |
| 174 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 174 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 175 return Remove(std::vector<std::string>(1, key)); | 175 return Remove(std::vector<std::string>(1, key)); |
| 176 } | 176 } |
| 177 | 177 |
| 178 ValueStore::WriteResult LeveldbValueStore::Remove( | 178 ValueStore::WriteResult LeveldbValueStore::Remove( |
| 179 const std::vector<std::string>& keys) { | 179 const std::vector<std::string>& keys) { |
| 180 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 180 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 181 | 181 |
| 182 Status status = EnsureDbIsOpen(); | 182 Status status = EnsureDbIsOpen(); |
| 183 if (!status.ok()) | 183 if (!status.ok()) |
| 184 return MakeWriteResult(status); | 184 return MakeWriteResult(status); |
| 185 | 185 |
| 186 leveldb::WriteBatch batch; | 186 leveldb::WriteBatch batch; |
| 187 scoped_ptr<ValueStoreChangeList> changes(new ValueStoreChangeList()); | 187 std::unique_ptr<ValueStoreChangeList> changes(new ValueStoreChangeList()); |
| 188 | 188 |
| 189 for (const std::string& key : keys) { | 189 for (const std::string& key : keys) { |
| 190 scoped_ptr<base::Value> old_value; | 190 std::unique_ptr<base::Value> old_value; |
| 191 status.Merge(Read(key, &old_value)); | 191 status.Merge(Read(key, &old_value)); |
| 192 if (!status.ok()) | 192 if (!status.ok()) |
| 193 return MakeWriteResult(status); | 193 return MakeWriteResult(status); |
| 194 | 194 |
| 195 if (old_value) { | 195 if (old_value) { |
| 196 changes->push_back(ValueStoreChange(key, old_value.release(), NULL)); | 196 changes->push_back(ValueStoreChange(key, old_value.release(), NULL)); |
| 197 batch.Delete(key); | 197 batch.Delete(key); |
| 198 } | 198 } |
| 199 } | 199 } |
| 200 | 200 |
| 201 leveldb::Status ldb_status = db()->Write(leveldb::WriteOptions(), &batch); | 201 leveldb::Status ldb_status = db()->Write(leveldb::WriteOptions(), &batch); |
| 202 if (!ldb_status.ok() && !ldb_status.IsNotFound()) { | 202 if (!ldb_status.ok() && !ldb_status.IsNotFound()) { |
| 203 status.Merge(ToValueStoreError(ldb_status)); | 203 status.Merge(ToValueStoreError(ldb_status)); |
| 204 return MakeWriteResult(status); | 204 return MakeWriteResult(status); |
| 205 } | 205 } |
| 206 return MakeWriteResult(std::move(changes), status); | 206 return MakeWriteResult(std::move(changes), status); |
| 207 } | 207 } |
| 208 | 208 |
| 209 ValueStore::WriteResult LeveldbValueStore::Clear() { | 209 ValueStore::WriteResult LeveldbValueStore::Clear() { |
| 210 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 210 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 211 | 211 |
| 212 scoped_ptr<ValueStoreChangeList> changes(new ValueStoreChangeList()); | 212 std::unique_ptr<ValueStoreChangeList> changes(new ValueStoreChangeList()); |
| 213 | 213 |
| 214 ReadResult read_result = Get(); | 214 ReadResult read_result = Get(); |
| 215 if (!read_result->status().ok()) | 215 if (!read_result->status().ok()) |
| 216 return MakeWriteResult(read_result->status()); | 216 return MakeWriteResult(read_result->status()); |
| 217 | 217 |
| 218 base::DictionaryValue& whole_db = read_result->settings(); | 218 base::DictionaryValue& whole_db = read_result->settings(); |
| 219 while (!whole_db.empty()) { | 219 while (!whole_db.empty()) { |
| 220 std::string next_key = base::DictionaryValue::Iterator(whole_db).key(); | 220 std::string next_key = base::DictionaryValue::Iterator(whole_db).key(); |
| 221 scoped_ptr<base::Value> next_value; | 221 std::unique_ptr<base::Value> next_value; |
| 222 whole_db.RemoveWithoutPathExpansion(next_key, &next_value); | 222 whole_db.RemoveWithoutPathExpansion(next_key, &next_value); |
| 223 changes->push_back(ValueStoreChange(next_key, next_value.release(), NULL)); | 223 changes->push_back(ValueStoreChange(next_key, next_value.release(), NULL)); |
| 224 } | 224 } |
| 225 | 225 |
| 226 DeleteDbFile(); | 226 DeleteDbFile(); |
| 227 return MakeWriteResult(std::move(changes), read_result->status()); | 227 return MakeWriteResult(std::move(changes), read_result->status()); |
| 228 } | 228 } |
| 229 | 229 |
| 230 bool LeveldbValueStore::WriteToDbForTest(leveldb::WriteBatch* batch) { | 230 bool LeveldbValueStore::WriteToDbForTest(leveldb::WriteBatch* batch) { |
| 231 Status status = EnsureDbIsOpen(); | 231 Status status = EnsureDbIsOpen(); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 | 267 |
| 268 ValueStore::Status LeveldbValueStore::AddToBatch( | 268 ValueStore::Status LeveldbValueStore::AddToBatch( |
| 269 ValueStore::WriteOptions options, | 269 ValueStore::WriteOptions options, |
| 270 const std::string& key, | 270 const std::string& key, |
| 271 const base::Value& value, | 271 const base::Value& value, |
| 272 leveldb::WriteBatch* batch, | 272 leveldb::WriteBatch* batch, |
| 273 ValueStoreChangeList* changes) { | 273 ValueStoreChangeList* changes) { |
| 274 bool write_new_value = true; | 274 bool write_new_value = true; |
| 275 | 275 |
| 276 if (!(options & NO_GENERATE_CHANGES)) { | 276 if (!(options & NO_GENERATE_CHANGES)) { |
| 277 scoped_ptr<base::Value> old_value; | 277 std::unique_ptr<base::Value> old_value; |
| 278 Status status = Read(key, &old_value); | 278 Status status = Read(key, &old_value); |
| 279 if (!status.ok()) | 279 if (!status.ok()) |
| 280 return status; | 280 return status; |
| 281 if (!old_value || !old_value->Equals(&value)) { | 281 if (!old_value || !old_value->Equals(&value)) { |
| 282 changes->push_back( | 282 changes->push_back( |
| 283 ValueStoreChange(key, old_value.release(), value.DeepCopy())); | 283 ValueStoreChange(key, old_value.release(), value.DeepCopy())); |
| 284 } else { | 284 } else { |
| 285 write_new_value = false; | 285 write_new_value = false; |
| 286 } | 286 } |
| 287 } | 287 } |
| 288 | 288 |
| 289 if (write_new_value) { | 289 if (write_new_value) { |
| 290 std::string value_as_json; | 290 std::string value_as_json; |
| 291 if (!base::JSONWriter::Write(value, &value_as_json)) | 291 if (!base::JSONWriter::Write(value, &value_as_json)) |
| 292 return Status(OTHER_ERROR, kCannotSerialize); | 292 return Status(OTHER_ERROR, kCannotSerialize); |
| 293 batch->Put(key, value_as_json); | 293 batch->Put(key, value_as_json); |
| 294 } | 294 } |
| 295 | 295 |
| 296 return Status(); | 296 return Status(); |
| 297 } | 297 } |
| 298 | 298 |
| 299 ValueStore::Status LeveldbValueStore::WriteToDb(leveldb::WriteBatch* batch) { | 299 ValueStore::Status LeveldbValueStore::WriteToDb(leveldb::WriteBatch* batch) { |
| 300 return ToValueStoreError(db()->Write(write_options(), batch)); | 300 return ToValueStoreError(db()->Write(write_options(), batch)); |
| 301 } | 301 } |
| OLD | NEW |