| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <stdint.h> | 5 #include <stdint.h> |
| 6 #include <utility> | 6 #include <utility> |
| 7 | 7 |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 TEST_F(IndexedDBTest, ClearSessionOnlyDatabases) { | 69 TEST_F(IndexedDBTest, ClearSessionOnlyDatabases) { |
| 70 base::ScopedTempDir temp_dir; | 70 base::ScopedTempDir temp_dir; |
| 71 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 71 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 72 | 72 |
| 73 base::FilePath normal_path; | 73 base::FilePath normal_path; |
| 74 base::FilePath session_only_path; | 74 base::FilePath session_only_path; |
| 75 | 75 |
| 76 // Create the scope which will ensure we run the destructor of the context | 76 // Create the scope which will ensure we run the destructor of the context |
| 77 // which should trigger the clean up. | 77 // which should trigger the clean up. |
| 78 { | 78 { |
| 79 scoped_refptr<IndexedDBContextImpl> idb_context = | 79 scoped_refptr<IndexedDBContextImpl> idb_context = new IndexedDBContextImpl( |
| 80 new IndexedDBContextImpl(temp_dir.path(), | 80 temp_dir.GetPath(), special_storage_policy_.get(), |
| 81 special_storage_policy_.get(), | 81 quota_manager_proxy_.get(), task_runner_.get()); |
| 82 quota_manager_proxy_.get(), | |
| 83 task_runner_.get()); | |
| 84 | 82 |
| 85 normal_path = idb_context->GetFilePathForTesting(kNormalOrigin); | 83 normal_path = idb_context->GetFilePathForTesting(kNormalOrigin); |
| 86 session_only_path = idb_context->GetFilePathForTesting(kSessionOnlyOrigin); | 84 session_only_path = idb_context->GetFilePathForTesting(kSessionOnlyOrigin); |
| 87 ASSERT_TRUE(base::CreateDirectory(normal_path)); | 85 ASSERT_TRUE(base::CreateDirectory(normal_path)); |
| 88 ASSERT_TRUE(base::CreateDirectory(session_only_path)); | 86 ASSERT_TRUE(base::CreateDirectory(session_only_path)); |
| 89 FlushIndexedDBTaskRunner(); | 87 FlushIndexedDBTaskRunner(); |
| 90 base::RunLoop().RunUntilIdle(); | 88 base::RunLoop().RunUntilIdle(); |
| 91 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); | 89 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); |
| 92 } | 90 } |
| 93 | 91 |
| 94 FlushIndexedDBTaskRunner(); | 92 FlushIndexedDBTaskRunner(); |
| 95 base::RunLoop().RunUntilIdle(); | 93 base::RunLoop().RunUntilIdle(); |
| 96 | 94 |
| 97 EXPECT_TRUE(base::DirectoryExists(normal_path)); | 95 EXPECT_TRUE(base::DirectoryExists(normal_path)); |
| 98 EXPECT_FALSE(base::DirectoryExists(session_only_path)); | 96 EXPECT_FALSE(base::DirectoryExists(session_only_path)); |
| 99 } | 97 } |
| 100 | 98 |
| 101 TEST_F(IndexedDBTest, SetForceKeepSessionState) { | 99 TEST_F(IndexedDBTest, SetForceKeepSessionState) { |
| 102 base::ScopedTempDir temp_dir; | 100 base::ScopedTempDir temp_dir; |
| 103 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 101 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 104 | 102 |
| 105 base::FilePath normal_path; | 103 base::FilePath normal_path; |
| 106 base::FilePath session_only_path; | 104 base::FilePath session_only_path; |
| 107 | 105 |
| 108 // Create the scope which will ensure we run the destructor of the context. | 106 // Create the scope which will ensure we run the destructor of the context. |
| 109 { | 107 { |
| 110 // Create some indexedDB paths. | 108 // Create some indexedDB paths. |
| 111 // With the levelDB backend, these are directories. | 109 // With the levelDB backend, these are directories. |
| 112 scoped_refptr<IndexedDBContextImpl> idb_context = | 110 scoped_refptr<IndexedDBContextImpl> idb_context = new IndexedDBContextImpl( |
| 113 new IndexedDBContextImpl(temp_dir.path(), | 111 temp_dir.GetPath(), special_storage_policy_.get(), |
| 114 special_storage_policy_.get(), | 112 quota_manager_proxy_.get(), task_runner_.get()); |
| 115 quota_manager_proxy_.get(), | |
| 116 task_runner_.get()); | |
| 117 | 113 |
| 118 // Save session state. This should bypass the destruction-time deletion. | 114 // Save session state. This should bypass the destruction-time deletion. |
| 119 idb_context->SetForceKeepSessionState(); | 115 idb_context->SetForceKeepSessionState(); |
| 120 | 116 |
| 121 normal_path = idb_context->GetFilePathForTesting(kNormalOrigin); | 117 normal_path = idb_context->GetFilePathForTesting(kNormalOrigin); |
| 122 session_only_path = idb_context->GetFilePathForTesting(kSessionOnlyOrigin); | 118 session_only_path = idb_context->GetFilePathForTesting(kSessionOnlyOrigin); |
| 123 ASSERT_TRUE(base::CreateDirectory(normal_path)); | 119 ASSERT_TRUE(base::CreateDirectory(normal_path)); |
| 124 ASSERT_TRUE(base::CreateDirectory(session_only_path)); | 120 ASSERT_TRUE(base::CreateDirectory(session_only_path)); |
| 125 base::RunLoop().RunUntilIdle(); | 121 base::RunLoop().RunUntilIdle(); |
| 126 } | 122 } |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 new MockIndexedDBDatabaseCallbacks()); | 167 new MockIndexedDBDatabaseCallbacks()); |
| 172 | 168 |
| 173 base::FilePath test_path; | 169 base::FilePath test_path; |
| 174 | 170 |
| 175 // Create the scope which will ensure we run the destructor of the context. | 171 // Create the scope which will ensure we run the destructor of the context. |
| 176 { | 172 { |
| 177 TestBrowserContext browser_context; | 173 TestBrowserContext browser_context; |
| 178 | 174 |
| 179 const Origin kTestOrigin(GURL("http://test/")); | 175 const Origin kTestOrigin(GURL("http://test/")); |
| 180 | 176 |
| 181 scoped_refptr<IndexedDBContextImpl> idb_context = | 177 scoped_refptr<IndexedDBContextImpl> idb_context = new IndexedDBContextImpl( |
| 182 new IndexedDBContextImpl(temp_dir.path(), | 178 temp_dir.GetPath(), special_storage_policy_.get(), |
| 183 special_storage_policy_.get(), | 179 quota_manager_proxy_.get(), task_runner_.get()); |
| 184 quota_manager_proxy_.get(), | |
| 185 task_runner_.get()); | |
| 186 | 180 |
| 187 scoped_refptr<ForceCloseDBCallbacks> open_callbacks = | 181 scoped_refptr<ForceCloseDBCallbacks> open_callbacks = |
| 188 new ForceCloseDBCallbacks(idb_context, kTestOrigin); | 182 new ForceCloseDBCallbacks(idb_context, kTestOrigin); |
| 189 | 183 |
| 190 scoped_refptr<ForceCloseDBCallbacks> closed_callbacks = | 184 scoped_refptr<ForceCloseDBCallbacks> closed_callbacks = |
| 191 new ForceCloseDBCallbacks(idb_context, kTestOrigin); | 185 new ForceCloseDBCallbacks(idb_context, kTestOrigin); |
| 192 | 186 |
| 193 IndexedDBFactory* factory = idb_context->GetIDBFactory(); | 187 IndexedDBFactory* factory = idb_context->GetIDBFactory(); |
| 194 | 188 |
| 195 test_path = idb_context->GetFilePathForTesting(kTestOrigin); | 189 test_path = idb_context->GetFilePathForTesting(kTestOrigin); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 EXPECT_FALSE(closed_db_callbacks->forced_close_called()); | 222 EXPECT_FALSE(closed_db_callbacks->forced_close_called()); |
| 229 EXPECT_FALSE(base::DirectoryExists(test_path)); | 223 EXPECT_FALSE(base::DirectoryExists(test_path)); |
| 230 } | 224 } |
| 231 | 225 |
| 232 TEST_F(IndexedDBTest, DeleteFailsIfDirectoryLocked) { | 226 TEST_F(IndexedDBTest, DeleteFailsIfDirectoryLocked) { |
| 233 base::ScopedTempDir temp_dir; | 227 base::ScopedTempDir temp_dir; |
| 234 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 228 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 235 const Origin kTestOrigin(GURL("http://test/")); | 229 const Origin kTestOrigin(GURL("http://test/")); |
| 236 | 230 |
| 237 scoped_refptr<IndexedDBContextImpl> idb_context = new IndexedDBContextImpl( | 231 scoped_refptr<IndexedDBContextImpl> idb_context = new IndexedDBContextImpl( |
| 238 temp_dir.path(), special_storage_policy_.get(), | 232 temp_dir.GetPath(), special_storage_policy_.get(), |
| 239 quota_manager_proxy_.get(), task_runner_.get()); | 233 quota_manager_proxy_.get(), task_runner_.get()); |
| 240 | 234 |
| 241 base::FilePath test_path = idb_context->GetFilePathForTesting(kTestOrigin); | 235 base::FilePath test_path = idb_context->GetFilePathForTesting(kTestOrigin); |
| 242 ASSERT_TRUE(base::CreateDirectory(test_path)); | 236 ASSERT_TRUE(base::CreateDirectory(test_path)); |
| 243 | 237 |
| 244 std::unique_ptr<LevelDBLock> lock = | 238 std::unique_ptr<LevelDBLock> lock = |
| 245 LevelDBDatabase::LockForTesting(test_path); | 239 LevelDBDatabase::LockForTesting(test_path); |
| 246 ASSERT_TRUE(lock); | 240 ASSERT_TRUE(lock); |
| 247 | 241 |
| 248 // TODO(jsbell): Remove static_cast<> when overloads are eliminated. | 242 // TODO(jsbell): Remove static_cast<> when overloads are eliminated. |
| 249 void (IndexedDBContextImpl::* delete_for_origin)(const Origin&) = | 243 void (IndexedDBContextImpl::* delete_for_origin)(const Origin&) = |
| 250 &IndexedDBContextImpl::DeleteForOrigin; | 244 &IndexedDBContextImpl::DeleteForOrigin; |
| 251 idb_context->TaskRunner()->PostTask( | 245 idb_context->TaskRunner()->PostTask( |
| 252 FROM_HERE, | 246 FROM_HERE, |
| 253 base::Bind(delete_for_origin, idb_context, kTestOrigin)); | 247 base::Bind(delete_for_origin, idb_context, kTestOrigin)); |
| 254 FlushIndexedDBTaskRunner(); | 248 FlushIndexedDBTaskRunner(); |
| 255 | 249 |
| 256 EXPECT_TRUE(base::DirectoryExists(test_path)); | 250 EXPECT_TRUE(base::DirectoryExists(test_path)); |
| 257 } | 251 } |
| 258 | 252 |
| 259 TEST_F(IndexedDBTest, ForceCloseOpenDatabasesOnCommitFailure) { | 253 TEST_F(IndexedDBTest, ForceCloseOpenDatabasesOnCommitFailure) { |
| 260 const Origin kTestOrigin(GURL("http://test/")); | 254 const Origin kTestOrigin(GURL("http://test/")); |
| 261 | 255 |
| 262 base::ScopedTempDir temp_dir; | 256 base::ScopedTempDir temp_dir; |
| 263 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 257 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 264 | 258 |
| 265 scoped_refptr<IndexedDBContextImpl> context = | 259 scoped_refptr<IndexedDBContextImpl> context = new IndexedDBContextImpl( |
| 266 new IndexedDBContextImpl(temp_dir.path(), special_storage_policy_.get(), | 260 temp_dir.GetPath(), special_storage_policy_.get(), |
| 267 quota_manager_proxy_.get(), task_runner_.get()); | 261 quota_manager_proxy_.get(), task_runner_.get()); |
| 268 | 262 |
| 269 scoped_refptr<IndexedDBFactoryImpl> factory = | 263 scoped_refptr<IndexedDBFactoryImpl> factory = |
| 270 static_cast<IndexedDBFactoryImpl*>(context->GetIDBFactory()); | 264 static_cast<IndexedDBFactoryImpl*>(context->GetIDBFactory()); |
| 271 | 265 |
| 272 scoped_refptr<MockIndexedDBCallbacks> callbacks(new MockIndexedDBCallbacks()); | 266 scoped_refptr<MockIndexedDBCallbacks> callbacks(new MockIndexedDBCallbacks()); |
| 273 scoped_refptr<MockIndexedDBDatabaseCallbacks> db_callbacks( | 267 scoped_refptr<MockIndexedDBDatabaseCallbacks> db_callbacks( |
| 274 new MockIndexedDBDatabaseCallbacks()); | 268 new MockIndexedDBDatabaseCallbacks()); |
| 275 const int64_t transaction_id = 1; | 269 const int64_t transaction_id = 1; |
| 276 std::unique_ptr<IndexedDBPendingConnection> connection( | 270 std::unique_ptr<IndexedDBPendingConnection> connection( |
| 277 base::MakeUnique<IndexedDBPendingConnection>( | 271 base::MakeUnique<IndexedDBPendingConnection>( |
| 278 callbacks, db_callbacks, 0 /* child_process_id */, transaction_id, | 272 callbacks, db_callbacks, 0 /* child_process_id */, transaction_id, |
| 279 IndexedDBDatabaseMetadata::DEFAULT_VERSION)); | 273 IndexedDBDatabaseMetadata::DEFAULT_VERSION)); |
| 280 factory->Open(base::ASCIIToUTF16("db"), std::move(connection), | 274 factory->Open(base::ASCIIToUTF16("db"), std::move(connection), |
| 281 nullptr /* request_context */, Origin(kTestOrigin), | 275 nullptr /* request_context */, Origin(kTestOrigin), |
| 282 temp_dir.path()); | 276 temp_dir.GetPath()); |
| 283 | 277 |
| 284 EXPECT_TRUE(callbacks->connection()); | 278 EXPECT_TRUE(callbacks->connection()); |
| 285 | 279 |
| 286 // ConnectionOpened() is usually called by the dispatcher. | 280 // ConnectionOpened() is usually called by the dispatcher. |
| 287 context->ConnectionOpened(kTestOrigin, callbacks->connection()); | 281 context->ConnectionOpened(kTestOrigin, callbacks->connection()); |
| 288 | 282 |
| 289 EXPECT_TRUE(factory->IsBackingStoreOpen(kTestOrigin)); | 283 EXPECT_TRUE(factory->IsBackingStoreOpen(kTestOrigin)); |
| 290 | 284 |
| 291 // Simulate the write failure. | 285 // Simulate the write failure. |
| 292 leveldb::Status status = leveldb::Status::IOError("Simulated failure"); | 286 leveldb::Status status = leveldb::Status::IOError("Simulated failure"); |
| 293 callbacks->connection()->database()->TransactionCommitFailed(status); | 287 callbacks->connection()->database()->TransactionCommitFailed(status); |
| 294 | 288 |
| 295 EXPECT_TRUE(db_callbacks->forced_close_called()); | 289 EXPECT_TRUE(db_callbacks->forced_close_called()); |
| 296 EXPECT_FALSE(factory->IsBackingStoreOpen(kTestOrigin)); | 290 EXPECT_FALSE(factory->IsBackingStoreOpen(kTestOrigin)); |
| 297 } | 291 } |
| 298 | 292 |
| 299 } // namespace content | 293 } // namespace content |
| OLD | NEW |