| 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 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 leveldb::WriteBatch batch; | 186 leveldb::WriteBatch batch; |
| 187 std::unique_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 std::unique_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, std::move(old_value), nullptr)); |
| 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 std::unique_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 std::unique_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( |
| 224 ValueStoreChange(next_key, std::move(next_value), nullptr)); |
| 224 } | 225 } |
| 225 | 226 |
| 226 DeleteDbFile(); | 227 DeleteDbFile(); |
| 227 return MakeWriteResult(std::move(changes), read_result->status()); | 228 return MakeWriteResult(std::move(changes), read_result->status()); |
| 228 } | 229 } |
| 229 | 230 |
| 230 bool LeveldbValueStore::WriteToDbForTest(leveldb::WriteBatch* batch) { | 231 bool LeveldbValueStore::WriteToDbForTest(leveldb::WriteBatch* batch) { |
| 231 Status status = EnsureDbIsOpen(); | 232 Status status = EnsureDbIsOpen(); |
| 232 if (!status.ok()) | 233 if (!status.ok()) |
| 233 return false; | 234 return false; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 273 ValueStoreChangeList* changes) { | 274 ValueStoreChangeList* changes) { |
| 274 bool write_new_value = true; | 275 bool write_new_value = true; |
| 275 | 276 |
| 276 if (!(options & NO_GENERATE_CHANGES)) { | 277 if (!(options & NO_GENERATE_CHANGES)) { |
| 277 std::unique_ptr<base::Value> old_value; | 278 std::unique_ptr<base::Value> old_value; |
| 278 Status status = Read(key, &old_value); | 279 Status status = Read(key, &old_value); |
| 279 if (!status.ok()) | 280 if (!status.ok()) |
| 280 return status; | 281 return status; |
| 281 if (!old_value || !old_value->Equals(&value)) { | 282 if (!old_value || !old_value->Equals(&value)) { |
| 282 changes->push_back( | 283 changes->push_back( |
| 283 ValueStoreChange(key, old_value.release(), value.DeepCopy())); | 284 ValueStoreChange(key, std::move(old_value), value.CreateDeepCopy())); |
| 284 } else { | 285 } else { |
| 285 write_new_value = false; | 286 write_new_value = false; |
| 286 } | 287 } |
| 287 } | 288 } |
| 288 | 289 |
| 289 if (write_new_value) { | 290 if (write_new_value) { |
| 290 std::string value_as_json; | 291 std::string value_as_json; |
| 291 if (!base::JSONWriter::Write(value, &value_as_json)) | 292 if (!base::JSONWriter::Write(value, &value_as_json)) |
| 292 return Status(OTHER_ERROR, kCannotSerialize); | 293 return Status(OTHER_ERROR, kCannotSerialize); |
| 293 batch->Put(key, value_as_json); | 294 batch->Put(key, value_as_json); |
| 294 } | 295 } |
| 295 | 296 |
| 296 return Status(); | 297 return Status(); |
| 297 } | 298 } |
| 298 | 299 |
| 299 ValueStore::Status LeveldbValueStore::WriteToDb(leveldb::WriteBatch* batch) { | 300 ValueStore::Status LeveldbValueStore::WriteToDb(leveldb::WriteBatch* batch) { |
| 300 return ToValueStoreError(db()->Write(write_options(), batch)); | 301 return ToValueStoreError(db()->Write(write_options(), batch)); |
| 301 } | 302 } |
| OLD | NEW |