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 "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 base::AutoLock auto_lock(session_storage_database_->db_lock_); | 70 base::AutoLock auto_lock(session_storage_database_->db_lock_); |
71 --session_storage_database_->operation_count_; | 71 --session_storage_database_->operation_count_; |
72 if ((session_storage_database_->is_inconsistent_ || | 72 if ((session_storage_database_->is_inconsistent_ || |
73 session_storage_database_->db_error_) && | 73 session_storage_database_->db_error_) && |
74 session_storage_database_->operation_count_ == 0 && | 74 session_storage_database_->operation_count_ == 0 && |
75 !session_storage_database_->invalid_db_deleted_) { | 75 !session_storage_database_->invalid_db_deleted_) { |
76 // No other operations are ongoing and the data is bad -> delete it now. | 76 // No other operations are ongoing and the data is bad -> delete it now. |
77 session_storage_database_->db_.reset(); | 77 session_storage_database_->db_.reset(); |
78 #if defined(OS_WIN) | 78 #if defined(OS_WIN) |
79 leveldb::DestroyDB( | 79 leveldb::DestroyDB( |
80 WideToUTF8(session_storage_database_->file_path_.value()), | 80 base::WideToUTF8(session_storage_database_->file_path_.value()), |
81 leveldb::Options()); | 81 leveldb::Options()); |
82 #else | 82 #else |
83 leveldb::DestroyDB(session_storage_database_->file_path_.value(), | 83 leveldb::DestroyDB(session_storage_database_->file_path_.value(), |
84 leveldb::Options()); | 84 leveldb::Options()); |
85 #endif | 85 #endif |
86 session_storage_database_->invalid_db_deleted_ = true; | 86 session_storage_database_->invalid_db_deleted_ = true; |
87 } | 87 } |
88 } | 88 } |
89 | 89 |
90 private: | 90 private: |
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
370 } | 370 } |
371 | 371 |
372 leveldb::Status SessionStorageDatabase::TryToOpen(leveldb::DB** db) { | 372 leveldb::Status SessionStorageDatabase::TryToOpen(leveldb::DB** db) { |
373 leveldb::Options options; | 373 leveldb::Options options; |
374 // The directory exists but a valid leveldb database might not exist inside it | 374 // The directory exists but a valid leveldb database might not exist inside it |
375 // (e.g., a subset of the needed files might be missing). Handle this | 375 // (e.g., a subset of the needed files might be missing). Handle this |
376 // situation gracefully by creating the database now. | 376 // situation gracefully by creating the database now. |
377 options.max_open_files = 0; // Use minimum. | 377 options.max_open_files = 0; // Use minimum. |
378 options.create_if_missing = true; | 378 options.create_if_missing = true; |
379 #if defined(OS_WIN) | 379 #if defined(OS_WIN) |
380 return leveldb::DB::Open(options, WideToUTF8(file_path_.value()), db); | 380 return leveldb::DB::Open(options, base::WideToUTF8(file_path_.value()), db); |
381 #elif defined(OS_POSIX) | 381 #elif defined(OS_POSIX) |
382 return leveldb::DB::Open(options, file_path_.value(), db); | 382 return leveldb::DB::Open(options, file_path_.value(), db); |
383 #endif | 383 #endif |
384 } | 384 } |
385 | 385 |
386 bool SessionStorageDatabase::IsOpen() const { | 386 bool SessionStorageDatabase::IsOpen() const { |
387 return db_.get() != NULL; | 387 return db_.get() != NULL; |
388 } | 388 } |
389 | 389 |
390 bool SessionStorageDatabase::CallerErrorCheck(bool ok) const { | 390 bool SessionStorageDatabase::CallerErrorCheck(bool ok) const { |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
564 if (!DatabaseErrorCheck(it->status().ok())) | 564 if (!DatabaseErrorCheck(it->status().ok())) |
565 return false; | 565 return false; |
566 // Skip the dummy entry "map-<mapid>-". | 566 // Skip the dummy entry "map-<mapid>-". |
567 for (it->Next(); it->Valid(); it->Next()) { | 567 for (it->Next(); it->Valid(); it->Next()) { |
568 std::string key = it->key().ToString(); | 568 std::string key = it->key().ToString(); |
569 if (key.find(map_start_key) != 0) { | 569 if (key.find(map_start_key) != 0) { |
570 // Iterated past the keys in this map. | 570 // Iterated past the keys in this map. |
571 break; | 571 break; |
572 } | 572 } |
573 // Key is of the form "map-<mapid>-<key>". | 573 // Key is of the form "map-<mapid>-<key>". |
574 base::string16 key16 = UTF8ToUTF16(key.substr(map_start_key.length())); | 574 base::string16 key16 = |
| 575 base::UTF8ToUTF16(key.substr(map_start_key.length())); |
575 if (only_keys) { | 576 if (only_keys) { |
576 (*result)[key16] = base::NullableString16(); | 577 (*result)[key16] = base::NullableString16(); |
577 } else { | 578 } else { |
578 // Convert the raw data stored in std::string (it->value()) to raw data | 579 // Convert the raw data stored in std::string (it->value()) to raw data |
579 // stored in base::string16. | 580 // stored in base::string16. |
580 size_t len = it->value().size() / sizeof(char16); | 581 size_t len = it->value().size() / sizeof(char16); |
581 const char16* data_ptr = | 582 const char16* data_ptr = |
582 reinterpret_cast<const char16*>(it->value().data()); | 583 reinterpret_cast<const char16*>(it->value().data()); |
583 (*result)[key16] = | 584 (*result)[key16] = |
584 base::NullableString16(base::string16(data_ptr, len), false); | 585 base::NullableString16(base::string16(data_ptr, len), false); |
585 } | 586 } |
586 } | 587 } |
587 return true; | 588 return true; |
588 } | 589 } |
589 | 590 |
590 void SessionStorageDatabase::WriteValuesToMap(const std::string& map_id, | 591 void SessionStorageDatabase::WriteValuesToMap(const std::string& map_id, |
591 const DOMStorageValuesMap& values, | 592 const DOMStorageValuesMap& values, |
592 leveldb::WriteBatch* batch) { | 593 leveldb::WriteBatch* batch) { |
593 for (DOMStorageValuesMap::const_iterator it = values.begin(); | 594 for (DOMStorageValuesMap::const_iterator it = values.begin(); |
594 it != values.end(); | 595 it != values.end(); |
595 ++it) { | 596 ++it) { |
596 base::NullableString16 value = it->second; | 597 base::NullableString16 value = it->second; |
597 std::string key = MapKey(map_id, UTF16ToUTF8(it->first)); | 598 std::string key = MapKey(map_id, base::UTF16ToUTF8(it->first)); |
598 if (value.is_null()) { | 599 if (value.is_null()) { |
599 batch->Delete(key); | 600 batch->Delete(key); |
600 } else { | 601 } else { |
601 // Convert the raw data stored in base::string16 to raw data stored in | 602 // Convert the raw data stored in base::string16 to raw data stored in |
602 // std::string. | 603 // std::string. |
603 const char* data = reinterpret_cast<const char*>(value.string().data()); | 604 const char* data = reinterpret_cast<const char*>(value.string().data()); |
604 size_t size = value.string().size() * 2; | 605 size_t size = value.string().size() * 2; |
605 batch->Put(key, leveldb::Slice(data, size)); | 606 batch->Put(key, leveldb::Slice(data, size)); |
606 } | 607 } |
607 } | 608 } |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
649 return true; | 650 return true; |
650 } | 651 } |
651 | 652 |
652 bool SessionStorageDatabase::ClearMap(const std::string& map_id, | 653 bool SessionStorageDatabase::ClearMap(const std::string& map_id, |
653 leveldb::WriteBatch* batch) { | 654 leveldb::WriteBatch* batch) { |
654 DOMStorageValuesMap values; | 655 DOMStorageValuesMap values; |
655 if (!ReadMap(map_id, leveldb::ReadOptions(), &values, true)) | 656 if (!ReadMap(map_id, leveldb::ReadOptions(), &values, true)) |
656 return false; | 657 return false; |
657 for (DOMStorageValuesMap::const_iterator it = values.begin(); | 658 for (DOMStorageValuesMap::const_iterator it = values.begin(); |
658 it != values.end(); ++it) | 659 it != values.end(); ++it) |
659 batch->Delete(MapKey(map_id, UTF16ToUTF8(it->first))); | 660 batch->Delete(MapKey(map_id, base::UTF16ToUTF8(it->first))); |
660 return true; | 661 return true; |
661 } | 662 } |
662 | 663 |
663 bool SessionStorageDatabase::DeepCopyArea( | 664 bool SessionStorageDatabase::DeepCopyArea( |
664 const std::string& namespace_id, const GURL& origin, bool copy_data, | 665 const std::string& namespace_id, const GURL& origin, bool copy_data, |
665 std::string* map_id, leveldb::WriteBatch* batch) { | 666 std::string* map_id, leveldb::WriteBatch* batch) { |
666 // Example, data before deep copy: | 667 // Example, data before deep copy: |
667 // | namespace-1- (1 = namespace id)| dummy | | 668 // | namespace-1- (1 = namespace id)| dummy | |
668 // | namespace-1-origin1 | 1 (mapid) | | 669 // | namespace-1-origin1 | 1 (mapid) | |
669 // | namespace-2- | dummy | | 670 // | namespace-2- | dummy | |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
719 std::string SessionStorageDatabase::MapKey(const std::string& map_id, | 720 std::string SessionStorageDatabase::MapKey(const std::string& map_id, |
720 const std::string& key) { | 721 const std::string& key) { |
721 return base::StringPrintf("map-%s-%s", map_id.c_str(), key.c_str()); | 722 return base::StringPrintf("map-%s-%s", map_id.c_str(), key.c_str()); |
722 } | 723 } |
723 | 724 |
724 const char* SessionStorageDatabase::NextMapIdKey() { | 725 const char* SessionStorageDatabase::NextMapIdKey() { |
725 return "next-map-id"; | 726 return "next-map-id"; |
726 } | 727 } |
727 | 728 |
728 } // namespace content | 729 } // namespace content |
OLD | NEW |