| 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> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 private: | 64 private: |
| 65 DISALLOW_COPY_AND_ASSIGN(DefaultLevelDBFactory); | 65 DISALLOW_COPY_AND_ASSIGN(DefaultLevelDBFactory); |
| 66 }; | 66 }; |
| 67 | 67 |
| 68 class TestableIndexedDBBackingStore : public IndexedDBBackingStore { | 68 class TestableIndexedDBBackingStore : public IndexedDBBackingStore { |
| 69 public: | 69 public: |
| 70 static scoped_refptr<TestableIndexedDBBackingStore> Open( | 70 static scoped_refptr<TestableIndexedDBBackingStore> Open( |
| 71 IndexedDBFactory* indexed_db_factory, | 71 IndexedDBFactory* indexed_db_factory, |
| 72 const Origin& origin, | 72 const Origin& origin, |
| 73 const base::FilePath& path_base, | 73 const base::FilePath& path_base, |
| 74 net::URLRequestContext* request_context, | 74 scoped_refptr<net::URLRequestContextGetter> request_context_getter, |
| 75 LevelDBFactory* leveldb_factory, | 75 LevelDBFactory* leveldb_factory, |
| 76 base::SequencedTaskRunner* task_runner, | 76 base::SequencedTaskRunner* task_runner, |
| 77 leveldb::Status* status) { | 77 leveldb::Status* status) { |
| 78 DCHECK(!path_base.empty()); | 78 DCHECK(!path_base.empty()); |
| 79 | 79 |
| 80 std::unique_ptr<LevelDBComparator> comparator = | 80 std::unique_ptr<LevelDBComparator> comparator = |
| 81 base::MakeUnique<Comparator>(); | 81 base::MakeUnique<Comparator>(); |
| 82 | 82 |
| 83 if (!base::CreateDirectory(path_base)) { | 83 if (!base::CreateDirectory(path_base)) { |
| 84 *status = leveldb::Status::IOError("Unable to create base dir"); | 84 *status = leveldb::Status::IOError("Unable to create base dir"); |
| 85 return scoped_refptr<TestableIndexedDBBackingStore>(); | 85 return scoped_refptr<TestableIndexedDBBackingStore>(); |
| 86 } | 86 } |
| 87 | 87 |
| 88 const base::FilePath file_path = path_base.AppendASCII("test_db_path"); | 88 const base::FilePath file_path = path_base.AppendASCII("test_db_path"); |
| 89 const base::FilePath blob_path = path_base.AppendASCII("test_blob_path"); | 89 const base::FilePath blob_path = path_base.AppendASCII("test_blob_path"); |
| 90 | 90 |
| 91 std::unique_ptr<LevelDBDatabase> db; | 91 std::unique_ptr<LevelDBDatabase> db; |
| 92 bool is_disk_full = false; | 92 bool is_disk_full = false; |
| 93 *status = leveldb_factory->OpenLevelDB( | 93 *status = leveldb_factory->OpenLevelDB( |
| 94 file_path, comparator.get(), &db, &is_disk_full); | 94 file_path, comparator.get(), &db, &is_disk_full); |
| 95 | 95 |
| 96 if (!db || !status->ok()) | 96 if (!db || !status->ok()) |
| 97 return scoped_refptr<TestableIndexedDBBackingStore>(); | 97 return scoped_refptr<TestableIndexedDBBackingStore>(); |
| 98 | 98 |
| 99 scoped_refptr<TestableIndexedDBBackingStore> backing_store( | 99 scoped_refptr<TestableIndexedDBBackingStore> backing_store( |
| 100 new TestableIndexedDBBackingStore(indexed_db_factory, origin, blob_path, | 100 new TestableIndexedDBBackingStore(indexed_db_factory, origin, blob_path, |
| 101 request_context, std::move(db), | 101 request_context_getter, std::move(db), |
| 102 std::move(comparator), task_runner)); | 102 std::move(comparator), task_runner)); |
| 103 | 103 |
| 104 *status = backing_store->SetUpMetadata(); | 104 *status = backing_store->SetUpMetadata(); |
| 105 if (!status->ok()) | 105 if (!status->ok()) |
| 106 return scoped_refptr<TestableIndexedDBBackingStore>(); | 106 return scoped_refptr<TestableIndexedDBBackingStore>(); |
| 107 | 107 |
| 108 return backing_store; | 108 return backing_store; |
| 109 } | 109 } |
| 110 | 110 |
| 111 const std::vector<IndexedDBBackingStore::Transaction::WriteDescriptor>& | 111 const std::vector<IndexedDBBackingStore::Transaction::WriteDescriptor>& |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 removals_.push_back(key); | 148 removals_.push_back(key); |
| 149 return true; | 149 return true; |
| 150 } | 150 } |
| 151 | 151 |
| 152 // Timers don't play nicely with unit tests. | 152 // Timers don't play nicely with unit tests. |
| 153 void StartJournalCleaningTimer() override { | 153 void StartJournalCleaningTimer() override { |
| 154 CleanPrimaryJournalIgnoreReturn(); | 154 CleanPrimaryJournalIgnoreReturn(); |
| 155 } | 155 } |
| 156 | 156 |
| 157 private: | 157 private: |
| 158 TestableIndexedDBBackingStore(IndexedDBFactory* indexed_db_factory, | 158 TestableIndexedDBBackingStore( |
| 159 const Origin& origin, | 159 IndexedDBFactory* indexed_db_factory, |
| 160 const base::FilePath& blob_path, | 160 const Origin& origin, |
| 161 net::URLRequestContext* request_context, | 161 const base::FilePath& blob_path, |
| 162 std::unique_ptr<LevelDBDatabase> db, | 162 scoped_refptr<net::URLRequestContextGetter> request_context_getter, |
| 163 std::unique_ptr<LevelDBComparator> comparator, | 163 std::unique_ptr<LevelDBDatabase> db, |
| 164 base::SequencedTaskRunner* task_runner) | 164 std::unique_ptr<LevelDBComparator> comparator, |
| 165 base::SequencedTaskRunner* task_runner) |
| 165 : IndexedDBBackingStore(indexed_db_factory, | 166 : IndexedDBBackingStore(indexed_db_factory, |
| 166 origin, | 167 origin, |
| 167 blob_path, | 168 blob_path, |
| 168 request_context, | 169 request_context_getter, |
| 169 std::move(db), | 170 std::move(db), |
| 170 std::move(comparator), | 171 std::move(comparator), |
| 171 task_runner), | 172 task_runner), |
| 172 database_id_(0) {} | 173 database_id_(0) {} |
| 173 | 174 |
| 174 int64_t database_id_; | 175 int64_t database_id_; |
| 175 std::vector<Transaction::WriteDescriptor> writes_; | 176 std::vector<Transaction::WriteDescriptor> writes_; |
| 176 | 177 |
| 177 // This is modified in an overridden virtual function that is properly const | 178 // This is modified in an overridden virtual function that is properly const |
| 178 // in the real implementation, therefore must be mutable here. | 179 // in the real implementation, therefore must be mutable here. |
| 179 mutable std::vector<int64_t> removals_; | 180 mutable std::vector<int64_t> removals_; |
| 180 | 181 |
| 181 DISALLOW_COPY_AND_ASSIGN(TestableIndexedDBBackingStore); | 182 DISALLOW_COPY_AND_ASSIGN(TestableIndexedDBBackingStore); |
| 182 }; | 183 }; |
| 183 | 184 |
| 184 class TestIDBFactory : public IndexedDBFactoryImpl { | 185 class TestIDBFactory : public IndexedDBFactoryImpl { |
| 185 public: | 186 public: |
| 186 explicit TestIDBFactory(IndexedDBContextImpl* idb_context) | 187 explicit TestIDBFactory(IndexedDBContextImpl* idb_context) |
| 187 : IndexedDBFactoryImpl(idb_context) {} | 188 : IndexedDBFactoryImpl(idb_context) {} |
| 188 | 189 |
| 189 scoped_refptr<TestableIndexedDBBackingStore> OpenBackingStoreForTest( | 190 scoped_refptr<TestableIndexedDBBackingStore> OpenBackingStoreForTest( |
| 190 const Origin& origin, | 191 const Origin& origin, |
| 191 net::URLRequestContext* url_request_context) { | 192 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter) { |
| 192 IndexedDBDataLossInfo data_loss_info; | 193 IndexedDBDataLossInfo data_loss_info; |
| 193 bool disk_full; | 194 bool disk_full; |
| 194 leveldb::Status status; | 195 leveldb::Status status; |
| 195 scoped_refptr<IndexedDBBackingStore> backing_store = | 196 scoped_refptr<IndexedDBBackingStore> backing_store = OpenBackingStore( |
| 196 OpenBackingStore(origin, context()->data_path(), url_request_context, | 197 origin, context()->data_path(), url_request_context_getter, |
| 197 &data_loss_info, &disk_full, &status); | 198 &data_loss_info, &disk_full, &status); |
| 198 scoped_refptr<TestableIndexedDBBackingStore> testable_store = | 199 scoped_refptr<TestableIndexedDBBackingStore> testable_store = |
| 199 static_cast<TestableIndexedDBBackingStore*>(backing_store.get()); | 200 static_cast<TestableIndexedDBBackingStore*>(backing_store.get()); |
| 200 return testable_store; | 201 return testable_store; |
| 201 } | 202 } |
| 202 | 203 |
| 203 protected: | 204 protected: |
| 204 ~TestIDBFactory() override {} | 205 ~TestIDBFactory() override {} |
| 205 | 206 |
| 206 scoped_refptr<IndexedDBBackingStore> OpenBackingStoreHelper( | 207 scoped_refptr<IndexedDBBackingStore> OpenBackingStoreHelper( |
| 207 const Origin& origin, | 208 const Origin& origin, |
| 208 const base::FilePath& data_directory, | 209 const base::FilePath& data_directory, |
| 209 net::URLRequestContext* request_context, | 210 scoped_refptr<net::URLRequestContextGetter> request_context_getter, |
| 210 IndexedDBDataLossInfo* data_loss_info, | 211 IndexedDBDataLossInfo* data_loss_info, |
| 211 bool* disk_full, | 212 bool* disk_full, |
| 212 bool first_time, | 213 bool first_time, |
| 213 leveldb::Status* status) override { | 214 leveldb::Status* status) override { |
| 214 DefaultLevelDBFactory leveldb_factory; | 215 DefaultLevelDBFactory leveldb_factory; |
| 215 return TestableIndexedDBBackingStore::Open( | 216 return TestableIndexedDBBackingStore::Open( |
| 216 this, origin, data_directory, request_context, &leveldb_factory, | 217 this, origin, data_directory, request_context_getter, &leveldb_factory, |
| 217 context()->TaskRunner(), status); | 218 context()->TaskRunner(), status); |
| 218 } | 219 } |
| 219 | 220 |
| 220 private: | 221 private: |
| 221 DISALLOW_COPY_AND_ASSIGN(TestIDBFactory); | 222 DISALLOW_COPY_AND_ASSIGN(TestIDBFactory); |
| 222 }; | 223 }; |
| 223 | 224 |
| 224 class IndexedDBBackingStoreTest : public testing::Test { | 225 class IndexedDBBackingStoreTest : public testing::Test { |
| 225 public: | 226 public: |
| 226 IndexedDBBackingStoreTest() {} | 227 IndexedDBBackingStoreTest() {} |
| 227 void SetUp() override { | 228 void SetUp() override { |
| 228 const Origin origin(GURL("http://localhost:81")); | 229 const Origin origin(GURL("http://localhost:81")); |
| 229 task_runner_ = new base::TestSimpleTaskRunner(); | 230 task_runner_ = new base::TestSimpleTaskRunner(); |
| 231 url_request_context_getter_ = |
| 232 new net::TestURLRequestContextGetter(task_runner_); |
| 230 special_storage_policy_ = new MockSpecialStoragePolicy(); | 233 special_storage_policy_ = new MockSpecialStoragePolicy(); |
| 231 quota_manager_proxy_ = new MockQuotaManagerProxy(nullptr, nullptr); | 234 quota_manager_proxy_ = new MockQuotaManagerProxy(nullptr, nullptr); |
| 232 special_storage_policy_->SetAllUnlimited(true); | 235 special_storage_policy_->SetAllUnlimited(true); |
| 233 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 236 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 234 idb_context_ = new IndexedDBContextImpl( | 237 idb_context_ = new IndexedDBContextImpl( |
| 235 temp_dir_.path(), special_storage_policy_.get(), | 238 temp_dir_.path(), special_storage_policy_.get(), |
| 236 quota_manager_proxy_.get(), task_runner_.get()); | 239 quota_manager_proxy_.get(), task_runner_.get()); |
| 237 idb_factory_ = new TestIDBFactory(idb_context_.get()); | 240 idb_factory_ = new TestIDBFactory(idb_context_.get()); |
| 238 backing_store_ = | 241 backing_store_ = idb_factory_->OpenBackingStoreForTest( |
| 239 idb_factory_->OpenBackingStoreForTest(origin, &url_request_context_); | 242 origin, url_request_context_getter_); |
| 240 | 243 |
| 241 // useful keys and values during tests | 244 // useful keys and values during tests |
| 242 m_value1 = IndexedDBValue("value1", std::vector<IndexedDBBlobInfo>()); | 245 m_value1 = IndexedDBValue("value1", std::vector<IndexedDBBlobInfo>()); |
| 243 m_value2 = IndexedDBValue("value2", std::vector<IndexedDBBlobInfo>()); | 246 m_value2 = IndexedDBValue("value2", std::vector<IndexedDBBlobInfo>()); |
| 244 | 247 |
| 245 m_blob_info.push_back( | 248 m_blob_info.push_back( |
| 246 IndexedDBBlobInfo("uuid 3", base::UTF8ToUTF16("blob type"), 1)); | 249 IndexedDBBlobInfo("uuid 3", base::UTF8ToUTF16("blob type"), 1)); |
| 247 m_blob_info.push_back( | 250 m_blob_info.push_back( |
| 248 IndexedDBBlobInfo("uuid 4", | 251 IndexedDBBlobInfo("uuid 4", |
| 249 base::FilePath(FILE_PATH_LITERAL("path/to/file")), | 252 base::FilePath(FILE_PATH_LITERAL("path/to/file")), |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 329 if (backing_store_->removals().size() != backing_store_->writes().size()) | 332 if (backing_store_->removals().size() != backing_store_->writes().size()) |
| 330 return false; | 333 return false; |
| 331 for (size_t i = 0; i < backing_store_->writes().size(); ++i) { | 334 for (size_t i = 0; i < backing_store_->writes().size(); ++i) { |
| 332 if (backing_store_->writes()[i].key() != backing_store_->removals()[i]) | 335 if (backing_store_->writes()[i].key() != backing_store_->removals()[i]) |
| 333 return false; | 336 return false; |
| 334 } | 337 } |
| 335 return true; | 338 return true; |
| 336 } | 339 } |
| 337 | 340 |
| 338 protected: | 341 protected: |
| 339 // Must be initialized before url_request_context_ | 342 // Must be initialized before url_request_context_getter_ |
| 340 content::TestBrowserThreadBundle thread_bundle_; | 343 content::TestBrowserThreadBundle thread_bundle_; |
| 341 | 344 |
| 342 base::ScopedTempDir temp_dir_; | 345 base::ScopedTempDir temp_dir_; |
| 343 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 346 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 344 scoped_refptr<MockSpecialStoragePolicy> special_storage_policy_; | 347 scoped_refptr<MockSpecialStoragePolicy> special_storage_policy_; |
| 345 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_; | 348 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_; |
| 346 scoped_refptr<IndexedDBContextImpl> idb_context_; | 349 scoped_refptr<IndexedDBContextImpl> idb_context_; |
| 347 scoped_refptr<TestIDBFactory> idb_factory_; | 350 scoped_refptr<TestIDBFactory> idb_factory_; |
| 348 net::TestURLRequestContext url_request_context_; | 351 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
| 349 | 352 |
| 350 scoped_refptr<TestableIndexedDBBackingStore> backing_store_; | 353 scoped_refptr<TestableIndexedDBBackingStore> backing_store_; |
| 351 | 354 |
| 352 // Sample keys and values that are consistent. | 355 // Sample keys and values that are consistent. |
| 353 IndexedDBKey m_key1; | 356 IndexedDBKey m_key1; |
| 354 IndexedDBKey m_key2; | 357 IndexedDBKey m_key2; |
| 355 IndexedDBKey m_key3; | 358 IndexedDBKey m_key3; |
| 356 IndexedDBValue m_value1; | 359 IndexedDBValue m_value1; |
| 357 IndexedDBValue m_value2; | 360 IndexedDBValue m_value2; |
| 358 IndexedDBValue m_value3; | 361 IndexedDBValue m_value3; |
| (...skipping 689 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1048 | 1051 |
| 1049 std::vector<base::string16> names = backing_store_->GetDatabaseNames(&s); | 1052 std::vector<base::string16> names = backing_store_->GetDatabaseNames(&s); |
| 1050 EXPECT_TRUE(s.ok()); | 1053 EXPECT_TRUE(s.ok()); |
| 1051 EXPECT_EQ(names.size(), 1ULL); | 1054 EXPECT_EQ(names.size(), 1ULL); |
| 1052 EXPECT_EQ(names[0], db1_name); | 1055 EXPECT_EQ(names[0], db1_name); |
| 1053 } | 1056 } |
| 1054 | 1057 |
| 1055 } // namespace | 1058 } // namespace |
| 1056 | 1059 |
| 1057 } // namespace content | 1060 } // namespace content |
| OLD | NEW |