| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/indexed_db/indexed_db_backing_store.h" | 5 #include "content/browser/indexed_db/indexed_db_backing_store.h" |
| 6 | 6 |
| 7 #include <stddef.h> |
| 8 #include <stdint.h> |
| 9 |
| 7 #include "base/callback.h" | 10 #include "base/callback.h" |
| 8 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 9 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/logging.h" | 13 #include "base/logging.h" |
| 11 #include "base/macros.h" | 14 #include "base/macros.h" |
| 12 #include "base/sequenced_task_runner.h" | 15 #include "base/sequenced_task_runner.h" |
| 13 #include "base/strings/string16.h" | 16 #include "base/strings/string16.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
| 15 #include "base/test/test_simple_task_runner.h" | 18 #include "base/test/test_simple_task_runner.h" |
| 16 #include "content/browser/indexed_db/indexed_db_context_impl.h" | 19 #include "content/browser/indexed_db/indexed_db_context_impl.h" |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 return scoped_refptr<TestableIndexedDBBackingStore>(); | 105 return scoped_refptr<TestableIndexedDBBackingStore>(); |
| 103 | 106 |
| 104 return backing_store; | 107 return backing_store; |
| 105 } | 108 } |
| 106 | 109 |
| 107 const std::vector<IndexedDBBackingStore::Transaction::WriteDescriptor>& | 110 const std::vector<IndexedDBBackingStore::Transaction::WriteDescriptor>& |
| 108 writes() const { | 111 writes() const { |
| 109 return writes_; | 112 return writes_; |
| 110 } | 113 } |
| 111 void ClearWrites() { writes_.clear(); } | 114 void ClearWrites() { writes_.clear(); } |
| 112 const std::vector<int64>& removals() const { return removals_; } | 115 const std::vector<int64_t>& removals() const { return removals_; } |
| 113 void ClearRemovals() { removals_.clear(); } | 116 void ClearRemovals() { removals_.clear(); } |
| 114 | 117 |
| 115 protected: | 118 protected: |
| 116 ~TestableIndexedDBBackingStore() override {} | 119 ~TestableIndexedDBBackingStore() override {} |
| 117 | 120 |
| 118 bool WriteBlobFile( | 121 bool WriteBlobFile( |
| 119 int64 database_id, | 122 int64_t database_id, |
| 120 const Transaction::WriteDescriptor& descriptor, | 123 const Transaction::WriteDescriptor& descriptor, |
| 121 Transaction::ChainedBlobWriter* chained_blob_writer) override { | 124 Transaction::ChainedBlobWriter* chained_blob_writer) override { |
| 122 if (KeyPrefix::IsValidDatabaseId(database_id_)) { | 125 if (KeyPrefix::IsValidDatabaseId(database_id_)) { |
| 123 if (database_id_ != database_id) { | 126 if (database_id_ != database_id) { |
| 124 return false; | 127 return false; |
| 125 } | 128 } |
| 126 } else { | 129 } else { |
| 127 database_id_ = database_id; | 130 database_id_ = database_id; |
| 128 } | 131 } |
| 129 writes_.push_back(descriptor); | 132 writes_.push_back(descriptor); |
| 130 task_runner()->PostTask( | 133 task_runner()->PostTask( |
| 131 FROM_HERE, | 134 FROM_HERE, |
| 132 base::Bind(&Transaction::ChainedBlobWriter::ReportWriteCompletion, | 135 base::Bind(&Transaction::ChainedBlobWriter::ReportWriteCompletion, |
| 133 chained_blob_writer, | 136 chained_blob_writer, |
| 134 true, | 137 true, |
| 135 1)); | 138 1)); |
| 136 return true; | 139 return true; |
| 137 } | 140 } |
| 138 | 141 |
| 139 bool RemoveBlobFile(int64 database_id, int64 key) const override { | 142 bool RemoveBlobFile(int64_t database_id, int64_t key) const override { |
| 140 if (database_id_ != database_id || | 143 if (database_id_ != database_id || |
| 141 !KeyPrefix::IsValidDatabaseId(database_id)) { | 144 !KeyPrefix::IsValidDatabaseId(database_id)) { |
| 142 return false; | 145 return false; |
| 143 } | 146 } |
| 144 removals_.push_back(key); | 147 removals_.push_back(key); |
| 145 return true; | 148 return true; |
| 146 } | 149 } |
| 147 | 150 |
| 148 // Timers don't play nicely with unit tests. | 151 // Timers don't play nicely with unit tests. |
| 149 void StartJournalCleaningTimer() override { | 152 void StartJournalCleaningTimer() override { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 160 base::SequencedTaskRunner* task_runner) | 163 base::SequencedTaskRunner* task_runner) |
| 161 : IndexedDBBackingStore(indexed_db_factory, | 164 : IndexedDBBackingStore(indexed_db_factory, |
| 162 origin_url, | 165 origin_url, |
| 163 blob_path, | 166 blob_path, |
| 164 request_context, | 167 request_context, |
| 165 db.Pass(), | 168 db.Pass(), |
| 166 comparator.Pass(), | 169 comparator.Pass(), |
| 167 task_runner), | 170 task_runner), |
| 168 database_id_(0) {} | 171 database_id_(0) {} |
| 169 | 172 |
| 170 int64 database_id_; | 173 int64_t database_id_; |
| 171 std::vector<Transaction::WriteDescriptor> writes_; | 174 std::vector<Transaction::WriteDescriptor> writes_; |
| 172 | 175 |
| 173 // This is modified in an overridden virtual function that is properly const | 176 // This is modified in an overridden virtual function that is properly const |
| 174 // in the real implementation, therefore must be mutable here. | 177 // in the real implementation, therefore must be mutable here. |
| 175 mutable std::vector<int64> removals_; | 178 mutable std::vector<int64_t> removals_; |
| 176 | 179 |
| 177 DISALLOW_COPY_AND_ASSIGN(TestableIndexedDBBackingStore); | 180 DISALLOW_COPY_AND_ASSIGN(TestableIndexedDBBackingStore); |
| 178 }; | 181 }; |
| 179 | 182 |
| 180 class TestIDBFactory : public IndexedDBFactoryImpl { | 183 class TestIDBFactory : public IndexedDBFactoryImpl { |
| 181 public: | 184 public: |
| 182 explicit TestIDBFactory(IndexedDBContextImpl* idb_context) | 185 explicit TestIDBFactory(IndexedDBContextImpl* idb_context) |
| 183 : IndexedDBFactoryImpl(idb_context) {} | 186 : IndexedDBFactoryImpl(idb_context) {} |
| 184 | 187 |
| 185 scoped_refptr<TestableIndexedDBBackingStore> OpenBackingStoreForTest( | 188 scoped_refptr<TestableIndexedDBBackingStore> OpenBackingStoreForTest( |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 return false; | 291 return false; |
| 289 } | 292 } |
| 290 } | 293 } |
| 291 return true; | 294 return true; |
| 292 } | 295 } |
| 293 | 296 |
| 294 bool CheckBlobReadsMatchWrites( | 297 bool CheckBlobReadsMatchWrites( |
| 295 const std::vector<IndexedDBBlobInfo>& reads) const { | 298 const std::vector<IndexedDBBlobInfo>& reads) const { |
| 296 if (backing_store_->writes().size() != reads.size()) | 299 if (backing_store_->writes().size() != reads.size()) |
| 297 return false; | 300 return false; |
| 298 std::set<int64> ids; | 301 std::set<int64_t> ids; |
| 299 for (size_t i = 0; i < backing_store_->writes().size(); ++i) | 302 for (size_t i = 0; i < backing_store_->writes().size(); ++i) |
| 300 ids.insert(backing_store_->writes()[i].key()); | 303 ids.insert(backing_store_->writes()[i].key()); |
| 301 if (ids.size() != backing_store_->writes().size()) | 304 if (ids.size() != backing_store_->writes().size()) |
| 302 return false; | 305 return false; |
| 303 for (size_t i = 0; i < reads.size(); ++i) { | 306 for (size_t i = 0; i < reads.size(); ++i) { |
| 304 if (ids.count(reads[i].key()) != 1) | 307 if (ids.count(reads[i].key()) != 1) |
| 305 return false; | 308 return false; |
| 306 } | 309 } |
| 307 return true; | 310 return true; |
| 308 } | 311 } |
| (...skipping 437 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 746 } | 749 } |
| 747 task_runner_->RunUntilIdle(); | 750 task_runner_->RunUntilIdle(); |
| 748 EXPECT_NE(0U, backing_store_->removals().size()); | 751 EXPECT_NE(0U, backing_store_->removals().size()); |
| 749 EXPECT_TRUE(CheckBlobRemovals()); | 752 EXPECT_TRUE(CheckBlobRemovals()); |
| 750 } | 753 } |
| 751 } | 754 } |
| 752 | 755 |
| 753 // Make sure that using very high ( more than 32 bit ) values for database_id | 756 // Make sure that using very high ( more than 32 bit ) values for database_id |
| 754 // and object_store_id still work. | 757 // and object_store_id still work. |
| 755 TEST_F(IndexedDBBackingStoreTest, HighIds) { | 758 TEST_F(IndexedDBBackingStoreTest, HighIds) { |
| 756 const int64 high_database_id = 1ULL << 35; | 759 const int64_t high_database_id = 1ULL << 35; |
| 757 const int64 high_object_store_id = 1ULL << 39; | 760 const int64_t high_object_store_id = 1ULL << 39; |
| 758 // index_ids are capped at 32 bits for storage purposes. | 761 // index_ids are capped at 32 bits for storage purposes. |
| 759 const int64 high_index_id = 1ULL << 29; | 762 const int64_t high_index_id = 1ULL << 29; |
| 760 | 763 |
| 761 const int64 invalid_high_index_id = 1ULL << 37; | 764 const int64_t invalid_high_index_id = 1ULL << 37; |
| 762 | 765 |
| 763 const IndexedDBKey& index_key = m_key2; | 766 const IndexedDBKey& index_key = m_key2; |
| 764 std::string index_key_raw; | 767 std::string index_key_raw; |
| 765 EncodeIDBKey(index_key, &index_key_raw); | 768 EncodeIDBKey(index_key, &index_key_raw); |
| 766 { | 769 { |
| 767 IndexedDBBackingStore::Transaction transaction1(backing_store_.get()); | 770 IndexedDBBackingStore::Transaction transaction1(backing_store_.get()); |
| 768 transaction1.Begin(); | 771 transaction1.Begin(); |
| 769 ScopedVector<storage::BlobDataHandle> handles; | 772 ScopedVector<storage::BlobDataHandle> handles; |
| 770 IndexedDBBackingStore::RecordIdentifier record; | 773 IndexedDBBackingStore::RecordIdentifier record; |
| 771 leveldb::Status s = backing_store_->PutRecord(&transaction1, | 774 leveldb::Status s = backing_store_->PutRecord(&transaction1, |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 838 EXPECT_TRUE(callback->called); | 841 EXPECT_TRUE(callback->called); |
| 839 EXPECT_TRUE(callback->succeeded); | 842 EXPECT_TRUE(callback->succeeded); |
| 840 s = transaction2.CommitPhaseTwo(); | 843 s = transaction2.CommitPhaseTwo(); |
| 841 EXPECT_TRUE(s.ok()); | 844 EXPECT_TRUE(s.ok()); |
| 842 } | 845 } |
| 843 } | 846 } |
| 844 | 847 |
| 845 // Make sure that other invalid ids do not crash. | 848 // Make sure that other invalid ids do not crash. |
| 846 TEST_F(IndexedDBBackingStoreTest, InvalidIds) { | 849 TEST_F(IndexedDBBackingStoreTest, InvalidIds) { |
| 847 // valid ids for use when testing invalid ids | 850 // valid ids for use when testing invalid ids |
| 848 const int64 database_id = 1; | 851 const int64_t database_id = 1; |
| 849 const int64 object_store_id = 1; | 852 const int64_t object_store_id = 1; |
| 850 const int64 index_id = kMinimumIndexId; | 853 const int64_t index_id = kMinimumIndexId; |
| 851 const int64 invalid_low_index_id = 19; // index_ids must be > kMinimumIndexId | 854 const int64_t invalid_low_index_id = |
| 855 19; // index_ids must be > kMinimumIndexId |
| 852 | 856 |
| 853 IndexedDBValue result_value; | 857 IndexedDBValue result_value; |
| 854 | 858 |
| 855 IndexedDBBackingStore::Transaction transaction1(backing_store_.get()); | 859 IndexedDBBackingStore::Transaction transaction1(backing_store_.get()); |
| 856 transaction1.Begin(); | 860 transaction1.Begin(); |
| 857 | 861 |
| 858 ScopedVector<storage::BlobDataHandle> handles; | 862 ScopedVector<storage::BlobDataHandle> handles; |
| 859 IndexedDBBackingStore::RecordIdentifier record; | 863 IndexedDBBackingStore::RecordIdentifier record; |
| 860 leveldb::Status s = backing_store_->PutRecord(&transaction1, | 864 leveldb::Status s = backing_store_->PutRecord(&transaction1, |
| 861 database_id, | 865 database_id, |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 926 database_id, | 930 database_id, |
| 927 KeyPrefix::kInvalidId, | 931 KeyPrefix::kInvalidId, |
| 928 index_id, | 932 index_id, |
| 929 m_key1, | 933 m_key1, |
| 930 &new_primary_key); | 934 &new_primary_key); |
| 931 EXPECT_FALSE(s.ok()); | 935 EXPECT_FALSE(s.ok()); |
| 932 } | 936 } |
| 933 | 937 |
| 934 TEST_F(IndexedDBBackingStoreTest, CreateDatabase) { | 938 TEST_F(IndexedDBBackingStoreTest, CreateDatabase) { |
| 935 const base::string16 database_name(ASCIIToUTF16("db1")); | 939 const base::string16 database_name(ASCIIToUTF16("db1")); |
| 936 int64 database_id; | 940 int64_t database_id; |
| 937 const base::string16 version(ASCIIToUTF16("old_string_version")); | 941 const base::string16 version(ASCIIToUTF16("old_string_version")); |
| 938 const int64 int_version = 9; | 942 const int64_t int_version = 9; |
| 939 | 943 |
| 940 const int64 object_store_id = 99; | 944 const int64_t object_store_id = 99; |
| 941 const base::string16 object_store_name(ASCIIToUTF16("object_store1")); | 945 const base::string16 object_store_name(ASCIIToUTF16("object_store1")); |
| 942 const bool auto_increment = true; | 946 const bool auto_increment = true; |
| 943 const IndexedDBKeyPath object_store_key_path( | 947 const IndexedDBKeyPath object_store_key_path( |
| 944 ASCIIToUTF16("object_store_key")); | 948 ASCIIToUTF16("object_store_key")); |
| 945 | 949 |
| 946 const int64 index_id = 999; | 950 const int64_t index_id = 999; |
| 947 const base::string16 index_name(ASCIIToUTF16("index1")); | 951 const base::string16 index_name(ASCIIToUTF16("index1")); |
| 948 const bool unique = true; | 952 const bool unique = true; |
| 949 const bool multi_entry = true; | 953 const bool multi_entry = true; |
| 950 const IndexedDBKeyPath index_key_path(ASCIIToUTF16("index_key")); | 954 const IndexedDBKeyPath index_key_path(ASCIIToUTF16("index_key")); |
| 951 | 955 |
| 952 { | 956 { |
| 953 leveldb::Status s = backing_store_->CreateIDBDatabaseMetaData( | 957 leveldb::Status s = backing_store_->CreateIDBDatabaseMetaData( |
| 954 database_name, version, int_version, &database_id); | 958 database_name, version, int_version, &database_id); |
| 955 EXPECT_TRUE(s.ok()); | 959 EXPECT_TRUE(s.ok()); |
| 956 EXPECT_GT(database_id, 0); | 960 EXPECT_GT(database_id, 0); |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1014 EXPECT_EQ(index_key_path, index.key_path); | 1018 EXPECT_EQ(index_key_path, index.key_path); |
| 1015 EXPECT_EQ(unique, index.unique); | 1019 EXPECT_EQ(unique, index.unique); |
| 1016 EXPECT_EQ(multi_entry, index.multi_entry); | 1020 EXPECT_EQ(multi_entry, index.multi_entry); |
| 1017 } | 1021 } |
| 1018 } | 1022 } |
| 1019 | 1023 |
| 1020 TEST_F(IndexedDBBackingStoreTest, GetDatabaseNames) { | 1024 TEST_F(IndexedDBBackingStoreTest, GetDatabaseNames) { |
| 1021 const base::string16 string_version(ASCIIToUTF16("string_version")); | 1025 const base::string16 string_version(ASCIIToUTF16("string_version")); |
| 1022 | 1026 |
| 1023 const base::string16 db1_name(ASCIIToUTF16("db1")); | 1027 const base::string16 db1_name(ASCIIToUTF16("db1")); |
| 1024 const int64 db1_version = 1LL; | 1028 const int64_t db1_version = 1LL; |
| 1025 int64 db1_id; | 1029 int64_t db1_id; |
| 1026 | 1030 |
| 1027 // Database records with DEFAULT_INT_VERSION represent stale data, | 1031 // Database records with DEFAULT_INT_VERSION represent stale data, |
| 1028 // and should not be enumerated. | 1032 // and should not be enumerated. |
| 1029 const base::string16 db2_name(ASCIIToUTF16("db2")); | 1033 const base::string16 db2_name(ASCIIToUTF16("db2")); |
| 1030 const int64 db2_version = IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION; | 1034 const int64_t db2_version = IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION; |
| 1031 int64 db2_id; | 1035 int64_t db2_id; |
| 1032 | 1036 |
| 1033 leveldb::Status s = backing_store_->CreateIDBDatabaseMetaData( | 1037 leveldb::Status s = backing_store_->CreateIDBDatabaseMetaData( |
| 1034 db1_name, string_version, db1_version, &db1_id); | 1038 db1_name, string_version, db1_version, &db1_id); |
| 1035 EXPECT_TRUE(s.ok()); | 1039 EXPECT_TRUE(s.ok()); |
| 1036 EXPECT_GT(db1_id, 0LL); | 1040 EXPECT_GT(db1_id, 0LL); |
| 1037 | 1041 |
| 1038 s = backing_store_->CreateIDBDatabaseMetaData( | 1042 s = backing_store_->CreateIDBDatabaseMetaData( |
| 1039 db2_name, string_version, db2_version, &db2_id); | 1043 db2_name, string_version, db2_version, &db2_id); |
| 1040 EXPECT_TRUE(s.ok()); | 1044 EXPECT_TRUE(s.ok()); |
| 1041 EXPECT_GT(db2_id, db1_id); | 1045 EXPECT_GT(db2_id, db1_id); |
| 1042 | 1046 |
| 1043 std::vector<base::string16> names = backing_store_->GetDatabaseNames(&s); | 1047 std::vector<base::string16> names = backing_store_->GetDatabaseNames(&s); |
| 1044 EXPECT_TRUE(s.ok()); | 1048 EXPECT_TRUE(s.ok()); |
| 1045 EXPECT_EQ(names.size(), 1ULL); | 1049 EXPECT_EQ(names.size(), 1ULL); |
| 1046 EXPECT_EQ(names[0], db1_name); | 1050 EXPECT_EQ(names[0], db1_name); |
| 1047 } | 1051 } |
| 1048 | 1052 |
| 1049 } // namespace | 1053 } // namespace |
| 1050 | 1054 |
| 1051 } // namespace content | 1055 } // namespace content |
| OLD | NEW |