| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "content/browser/dom_storage/session_storage_database.h" | 5 #include "content/browser/dom_storage/session_storage_database.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 return false; | 269 return false; |
| 270 DBOperation operation(this); | 270 DBOperation operation(this); |
| 271 | 271 |
| 272 // While ReadNamespacesAndOrigins is in progress, another thread can call | 272 // While ReadNamespacesAndOrigins is in progress, another thread can call |
| 273 // CommitAreaChanges. To protect the reading operation, create a snapshot and | 273 // CommitAreaChanges. To protect the reading operation, create a snapshot and |
| 274 // read from it. | 274 // read from it. |
| 275 leveldb::ReadOptions options; | 275 leveldb::ReadOptions options; |
| 276 options.snapshot = db_->GetSnapshot(); | 276 options.snapshot = db_->GetSnapshot(); |
| 277 | 277 |
| 278 std::string namespace_prefix = NamespacePrefix(); | 278 std::string namespace_prefix = NamespacePrefix(); |
| 279 scoped_ptr<leveldb::Iterator> it(db_->NewIterator(options)); | 279 std::unique_ptr<leveldb::Iterator> it(db_->NewIterator(options)); |
| 280 it->Seek(namespace_prefix); | 280 it->Seek(namespace_prefix); |
| 281 // If the key is not found, the status of the iterator won't be IsNotFound(), | 281 // If the key is not found, the status of the iterator won't be IsNotFound(), |
| 282 // but the iterator will be invalid. | 282 // but the iterator will be invalid. |
| 283 if (!it->Valid()) { | 283 if (!it->Valid()) { |
| 284 db_->ReleaseSnapshot(options.snapshot); | 284 db_->ReleaseSnapshot(options.snapshot); |
| 285 return true; | 285 return true; |
| 286 } | 286 } |
| 287 | 287 |
| 288 if (!DatabaseErrorCheck(it->status().ok())) { | 288 if (!DatabaseErrorCheck(it->status().ok())) { |
| 289 db_->ReleaseSnapshot(options.snapshot); | 289 db_->ReleaseSnapshot(options.snapshot); |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 440 batch->Put(namespace_start_key, ""); | 440 batch->Put(namespace_start_key, ""); |
| 441 return true; | 441 return true; |
| 442 } | 442 } |
| 443 return CallerErrorCheck(ok_if_exists); | 443 return CallerErrorCheck(ok_if_exists); |
| 444 } | 444 } |
| 445 | 445 |
| 446 bool SessionStorageDatabase::GetAreasInNamespace( | 446 bool SessionStorageDatabase::GetAreasInNamespace( |
| 447 const std::string& namespace_id, | 447 const std::string& namespace_id, |
| 448 std::map<std::string, std::string>* areas) { | 448 std::map<std::string, std::string>* areas) { |
| 449 std::string namespace_start_key = NamespaceStartKey(namespace_id); | 449 std::string namespace_start_key = NamespaceStartKey(namespace_id); |
| 450 scoped_ptr<leveldb::Iterator> it(db_->NewIterator(leveldb::ReadOptions())); | 450 std::unique_ptr<leveldb::Iterator> it( |
| 451 db_->NewIterator(leveldb::ReadOptions())); |
| 451 it->Seek(namespace_start_key); | 452 it->Seek(namespace_start_key); |
| 452 // If the key is not found, the status of the iterator won't be IsNotFound(), | 453 // If the key is not found, the status of the iterator won't be IsNotFound(), |
| 453 // but the iterator will be invalid. | 454 // but the iterator will be invalid. |
| 454 if (!it->Valid()) { | 455 if (!it->Valid()) { |
| 455 // The namespace_start_key is not found when the namespace doesn't contain | 456 // The namespace_start_key is not found when the namespace doesn't contain |
| 456 // any areas. We don't need to do anything. | 457 // any areas. We don't need to do anything. |
| 457 return true; | 458 return true; |
| 458 } | 459 } |
| 459 if (!DatabaseErrorCheck(it->status().ok())) | 460 if (!DatabaseErrorCheck(it->status().ok())) |
| 460 return false; | 461 return false; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 493 if (!exists) | 494 if (!exists) |
| 494 return true; // Nothing to delete. | 495 return true; // Nothing to delete. |
| 495 if (!DecreaseMapRefCount(map_id, 1, batch)) | 496 if (!DecreaseMapRefCount(map_id, 1, batch)) |
| 496 return false; | 497 return false; |
| 497 std::string namespace_key = NamespaceKey(namespace_id, origin); | 498 std::string namespace_key = NamespaceKey(namespace_id, origin); |
| 498 batch->Delete(namespace_key); | 499 batch->Delete(namespace_key); |
| 499 | 500 |
| 500 // If this was the only area in the namespace, delete the namespace start key, | 501 // If this was the only area in the namespace, delete the namespace start key, |
| 501 // too. | 502 // too. |
| 502 std::string namespace_start_key = NamespaceStartKey(namespace_id); | 503 std::string namespace_start_key = NamespaceStartKey(namespace_id); |
| 503 scoped_ptr<leveldb::Iterator> it(db_->NewIterator(leveldb::ReadOptions())); | 504 std::unique_ptr<leveldb::Iterator> it( |
| 505 db_->NewIterator(leveldb::ReadOptions())); |
| 504 it->Seek(namespace_start_key); | 506 it->Seek(namespace_start_key); |
| 505 if (!ConsistencyCheck(it->Valid())) | 507 if (!ConsistencyCheck(it->Valid())) |
| 506 return false; | 508 return false; |
| 507 // Advance the iterator 2 times (we still haven't really deleted | 509 // Advance the iterator 2 times (we still haven't really deleted |
| 508 // namespace_key). | 510 // namespace_key). |
| 509 it->Next(); | 511 it->Next(); |
| 510 if (!ConsistencyCheck(it->Valid())) | 512 if (!ConsistencyCheck(it->Valid())) |
| 511 return false; | 513 return false; |
| 512 it->Next(); | 514 it->Next(); |
| 513 if (!it->Valid()) | 515 if (!it->Valid()) |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 552 std::string namespace_key = NamespaceKey(namespace_id, origin.spec()); | 554 std::string namespace_key = NamespaceKey(namespace_id, origin.spec()); |
| 553 batch->Put(namespace_key, *map_id); | 555 batch->Put(namespace_key, *map_id); |
| 554 batch->Put(MapRefCountKey(*map_id), "1"); | 556 batch->Put(MapRefCountKey(*map_id), "1"); |
| 555 return true; | 557 return true; |
| 556 } | 558 } |
| 557 | 559 |
| 558 bool SessionStorageDatabase::ReadMap(const std::string& map_id, | 560 bool SessionStorageDatabase::ReadMap(const std::string& map_id, |
| 559 const leveldb::ReadOptions& options, | 561 const leveldb::ReadOptions& options, |
| 560 DOMStorageValuesMap* result, | 562 DOMStorageValuesMap* result, |
| 561 bool only_keys) { | 563 bool only_keys) { |
| 562 scoped_ptr<leveldb::Iterator> it(db_->NewIterator(options)); | 564 std::unique_ptr<leveldb::Iterator> it(db_->NewIterator(options)); |
| 563 std::string map_start_key = MapRefCountKey(map_id); | 565 std::string map_start_key = MapRefCountKey(map_id); |
| 564 it->Seek(map_start_key); | 566 it->Seek(map_start_key); |
| 565 // If the key is not found, the status of the iterator won't be IsNotFound(), | 567 // If the key is not found, the status of the iterator won't be IsNotFound(), |
| 566 // but the iterator will be invalid. The map needs to exist, otherwise we have | 568 // but the iterator will be invalid. The map needs to exist, otherwise we have |
| 567 // a stale map_id in the database. | 569 // a stale map_id in the database. |
| 568 if (!ConsistencyCheck(it->Valid())) | 570 if (!ConsistencyCheck(it->Valid())) |
| 569 return false; | 571 return false; |
| 570 if (!DatabaseErrorCheck(it->status().ok())) | 572 if (!DatabaseErrorCheck(it->status().ok())) |
| 571 return false; | 573 return false; |
| 572 // Skip the dummy entry "map-<mapid>-". | 574 // Skip the dummy entry "map-<mapid>-". |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 726 std::string SessionStorageDatabase::MapKey(const std::string& map_id, | 728 std::string SessionStorageDatabase::MapKey(const std::string& map_id, |
| 727 const std::string& key) { | 729 const std::string& key) { |
| 728 return base::StringPrintf("map-%s-%s", map_id.c_str(), key.c_str()); | 730 return base::StringPrintf("map-%s-%s", map_id.c_str(), key.c_str()); |
| 729 } | 731 } |
| 730 | 732 |
| 731 const char* SessionStorageDatabase::NextMapIdKey() { | 733 const char* SessionStorageDatabase::NextMapIdKey() { |
| 732 return "next-map-id"; | 734 return "next-map-id"; |
| 733 } | 735 } |
| 734 | 736 |
| 735 } // namespace content | 737 } // namespace content |
| OLD | NEW |