| 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 12 matching lines...) Expand all Loading... |
| 23 #include "content/public/test/test_browser_context.h" | 23 #include "content/public/test/test_browser_context.h" |
| 24 #include "storage/browser/quota/quota_manager.h" | 24 #include "storage/browser/quota/quota_manager.h" |
| 25 #include "storage/browser/quota/special_storage_policy.h" | 25 #include "storage/browser/quota/special_storage_policy.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 27 #include "url/origin.h" | 27 #include "url/origin.h" |
| 28 | 28 |
| 29 namespace content { | 29 namespace content { |
| 30 | 30 |
| 31 class IndexedDBTest : public testing::Test { | 31 class IndexedDBTest : public testing::Test { |
| 32 public: | 32 public: |
| 33 const GURL kNormalOrigin; | 33 const url::Origin kNormalOrigin; |
| 34 const GURL kSessionOnlyOrigin; | 34 const url::Origin kSessionOnlyOrigin; |
| 35 | 35 |
| 36 IndexedDBTest() | 36 IndexedDBTest() |
| 37 : kNormalOrigin("http://normal/"), | 37 : kNormalOrigin(GURL("http://normal/")), |
| 38 kSessionOnlyOrigin("http://session-only/"), | 38 kSessionOnlyOrigin(GURL("http://session-only/")), |
| 39 task_runner_(new base::TestSimpleTaskRunner), | 39 task_runner_(new base::TestSimpleTaskRunner), |
| 40 special_storage_policy_(new MockSpecialStoragePolicy), | 40 special_storage_policy_(new MockSpecialStoragePolicy), |
| 41 quota_manager_proxy_(new MockQuotaManagerProxy(nullptr, nullptr)), | 41 quota_manager_proxy_(new MockQuotaManagerProxy(nullptr, nullptr)), |
| 42 file_thread_(BrowserThread::FILE_USER_BLOCKING, &message_loop_), | 42 file_thread_(BrowserThread::FILE_USER_BLOCKING, &message_loop_), |
| 43 io_thread_(BrowserThread::IO, &message_loop_) { | 43 io_thread_(BrowserThread::IO, &message_loop_) { |
| 44 special_storage_policy_->AddSessionOnly(kSessionOnlyOrigin); | 44 special_storage_policy_->AddSessionOnly( |
| 45 GURL(kSessionOnlyOrigin.Serialize())); |
| 45 } | 46 } |
| 46 ~IndexedDBTest() override { | 47 ~IndexedDBTest() override { |
| 47 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); | 48 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); |
| 48 } | 49 } |
| 49 | 50 |
| 50 protected: | 51 protected: |
| 51 void FlushIndexedDBTaskRunner() { task_runner_->RunUntilIdle(); } | 52 void FlushIndexedDBTaskRunner() { task_runner_->RunUntilIdle(); } |
| 52 | 53 |
| 53 base::MessageLoopForIO message_loop_; | 54 base::MessageLoopForIO message_loop_; |
| 54 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 55 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 message_loop_.RunUntilIdle(); | 126 message_loop_.RunUntilIdle(); |
| 126 | 127 |
| 127 // No data was cleared because of SetForceKeepSessionState. | 128 // No data was cleared because of SetForceKeepSessionState. |
| 128 EXPECT_TRUE(base::DirectoryExists(normal_path)); | 129 EXPECT_TRUE(base::DirectoryExists(normal_path)); |
| 129 EXPECT_TRUE(base::DirectoryExists(session_only_path)); | 130 EXPECT_TRUE(base::DirectoryExists(session_only_path)); |
| 130 } | 131 } |
| 131 | 132 |
| 132 class ForceCloseDBCallbacks : public IndexedDBCallbacks { | 133 class ForceCloseDBCallbacks : public IndexedDBCallbacks { |
| 133 public: | 134 public: |
| 134 ForceCloseDBCallbacks(scoped_refptr<IndexedDBContextImpl> idb_context, | 135 ForceCloseDBCallbacks(scoped_refptr<IndexedDBContextImpl> idb_context, |
| 135 const GURL& origin_url) | 136 const url::Origin& origin) |
| 136 : IndexedDBCallbacks(NULL, 0, 0), | 137 : IndexedDBCallbacks(NULL, 0, 0), |
| 137 idb_context_(idb_context), | 138 idb_context_(idb_context), |
| 138 origin_url_(origin_url) {} | 139 origin_(origin) {} |
| 139 | 140 |
| 140 void OnSuccess() override {} | 141 void OnSuccess() override {} |
| 141 void OnSuccess(const std::vector<base::string16>&) override {} | 142 void OnSuccess(const std::vector<base::string16>&) override {} |
| 142 void OnSuccess(scoped_ptr<IndexedDBConnection> connection, | 143 void OnSuccess(scoped_ptr<IndexedDBConnection> connection, |
| 143 const IndexedDBDatabaseMetadata& metadata) override { | 144 const IndexedDBDatabaseMetadata& metadata) override { |
| 144 connection_ = std::move(connection); | 145 connection_ = std::move(connection); |
| 145 idb_context_->ConnectionOpened(origin_url_, connection_.get()); | 146 idb_context_->ConnectionOpened(origin_, connection_.get()); |
| 146 } | 147 } |
| 147 | 148 |
| 148 IndexedDBConnection* connection() { return connection_.get(); } | 149 IndexedDBConnection* connection() { return connection_.get(); } |
| 149 | 150 |
| 150 protected: | 151 protected: |
| 151 ~ForceCloseDBCallbacks() override {} | 152 ~ForceCloseDBCallbacks() override {} |
| 152 | 153 |
| 153 private: | 154 private: |
| 154 scoped_refptr<IndexedDBContextImpl> idb_context_; | 155 scoped_refptr<IndexedDBContextImpl> idb_context_; |
| 155 GURL origin_url_; | 156 url::Origin origin_; |
| 156 scoped_ptr<IndexedDBConnection> connection_; | 157 scoped_ptr<IndexedDBConnection> connection_; |
| 157 DISALLOW_COPY_AND_ASSIGN(ForceCloseDBCallbacks); | 158 DISALLOW_COPY_AND_ASSIGN(ForceCloseDBCallbacks); |
| 158 }; | 159 }; |
| 159 | 160 |
| 160 TEST_F(IndexedDBTest, ForceCloseOpenDatabasesOnDelete) { | 161 TEST_F(IndexedDBTest, ForceCloseOpenDatabasesOnDelete) { |
| 161 base::ScopedTempDir temp_dir; | 162 base::ScopedTempDir temp_dir; |
| 162 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 163 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 163 | 164 |
| 164 scoped_refptr<MockIndexedDBDatabaseCallbacks> open_db_callbacks( | 165 scoped_refptr<MockIndexedDBDatabaseCallbacks> open_db_callbacks( |
| 165 new MockIndexedDBDatabaseCallbacks()); | 166 new MockIndexedDBDatabaseCallbacks()); |
| 166 scoped_refptr<MockIndexedDBDatabaseCallbacks> closed_db_callbacks( | 167 scoped_refptr<MockIndexedDBDatabaseCallbacks> closed_db_callbacks( |
| 167 new MockIndexedDBDatabaseCallbacks()); | 168 new MockIndexedDBDatabaseCallbacks()); |
| 168 | 169 |
| 169 base::FilePath test_path; | 170 base::FilePath test_path; |
| 170 | 171 |
| 171 // Create the scope which will ensure we run the destructor of the context. | 172 // Create the scope which will ensure we run the destructor of the context. |
| 172 { | 173 { |
| 173 TestBrowserContext browser_context; | 174 TestBrowserContext browser_context; |
| 174 | 175 |
| 175 const GURL kTestOrigin("http://test/"); | 176 const url::Origin kTestOrigin(GURL("http://test/")); |
| 176 | 177 |
| 177 scoped_refptr<IndexedDBContextImpl> idb_context = | 178 scoped_refptr<IndexedDBContextImpl> idb_context = |
| 178 new IndexedDBContextImpl(temp_dir.path(), | 179 new IndexedDBContextImpl(temp_dir.path(), |
| 179 special_storage_policy_.get(), | 180 special_storage_policy_.get(), |
| 180 quota_manager_proxy_.get(), | 181 quota_manager_proxy_.get(), |
| 181 task_runner_.get()); | 182 task_runner_.get()); |
| 182 | 183 |
| 183 scoped_refptr<ForceCloseDBCallbacks> open_callbacks = | 184 scoped_refptr<ForceCloseDBCallbacks> open_callbacks = |
| 184 new ForceCloseDBCallbacks(idb_context, kTestOrigin); | 185 new ForceCloseDBCallbacks(idb_context, kTestOrigin); |
| 185 | 186 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 202 closed_db_callbacks, | 203 closed_db_callbacks, |
| 203 0 /* child_process_id */, | 204 0 /* child_process_id */, |
| 204 0 /* host_transaction_id */, | 205 0 /* host_transaction_id */, |
| 205 0 /* version */); | 206 0 /* version */); |
| 206 factory->Open(base::ASCIIToUTF16("closeddb"), closed_connection, | 207 factory->Open(base::ASCIIToUTF16("closeddb"), closed_connection, |
| 207 NULL /* request_context */, url::Origin(kTestOrigin), | 208 NULL /* request_context */, url::Origin(kTestOrigin), |
| 208 idb_context->data_path()); | 209 idb_context->data_path()); |
| 209 | 210 |
| 210 closed_callbacks->connection()->Close(); | 211 closed_callbacks->connection()->Close(); |
| 211 | 212 |
| 213 // TODO(jsbell): Remove static_cast<> when overloads are eliminated. |
| 212 idb_context->TaskRunner()->PostTask( | 214 idb_context->TaskRunner()->PostTask( |
| 213 FROM_HERE, | 215 FROM_HERE, |
| 214 base::Bind( | 216 base::Bind( |
| 215 &IndexedDBContextImpl::DeleteForOrigin, idb_context, kTestOrigin)); | 217 static_cast<void (IndexedDBContextImpl::*)(const url::Origin&)>( |
| 218 &IndexedDBContextImpl::DeleteForOrigin), |
| 219 idb_context, kTestOrigin)); |
| 216 FlushIndexedDBTaskRunner(); | 220 FlushIndexedDBTaskRunner(); |
| 217 message_loop_.RunUntilIdle(); | 221 message_loop_.RunUntilIdle(); |
| 218 } | 222 } |
| 219 | 223 |
| 220 // Make sure we wait until the destructor has run. | 224 // Make sure we wait until the destructor has run. |
| 221 message_loop_.RunUntilIdle(); | 225 message_loop_.RunUntilIdle(); |
| 222 | 226 |
| 223 EXPECT_TRUE(open_db_callbacks->forced_close_called()); | 227 EXPECT_TRUE(open_db_callbacks->forced_close_called()); |
| 224 EXPECT_FALSE(closed_db_callbacks->forced_close_called()); | 228 EXPECT_FALSE(closed_db_callbacks->forced_close_called()); |
| 225 EXPECT_FALSE(base::DirectoryExists(test_path)); | 229 EXPECT_FALSE(base::DirectoryExists(test_path)); |
| 226 } | 230 } |
| 227 | 231 |
| 228 TEST_F(IndexedDBTest, DeleteFailsIfDirectoryLocked) { | 232 TEST_F(IndexedDBTest, DeleteFailsIfDirectoryLocked) { |
| 229 base::ScopedTempDir temp_dir; | 233 base::ScopedTempDir temp_dir; |
| 230 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 234 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 231 const GURL kTestOrigin("http://test/"); | 235 const url::Origin kTestOrigin(GURL("http://test/")); |
| 232 | 236 |
| 233 scoped_refptr<IndexedDBContextImpl> idb_context = new IndexedDBContextImpl( | 237 scoped_refptr<IndexedDBContextImpl> idb_context = new IndexedDBContextImpl( |
| 234 temp_dir.path(), special_storage_policy_.get(), | 238 temp_dir.path(), special_storage_policy_.get(), |
| 235 quota_manager_proxy_.get(), task_runner_.get()); | 239 quota_manager_proxy_.get(), task_runner_.get()); |
| 236 | 240 |
| 237 base::FilePath test_path = idb_context->GetFilePathForTesting(kTestOrigin); | 241 base::FilePath test_path = idb_context->GetFilePathForTesting(kTestOrigin); |
| 238 ASSERT_TRUE(base::CreateDirectory(test_path)); | 242 ASSERT_TRUE(base::CreateDirectory(test_path)); |
| 239 | 243 |
| 240 scoped_ptr<LevelDBLock> lock = | 244 scoped_ptr<LevelDBLock> lock = |
| 241 LevelDBDatabase::LockForTesting(test_path); | 245 LevelDBDatabase::LockForTesting(test_path); |
| 242 ASSERT_TRUE(lock); | 246 ASSERT_TRUE(lock); |
| 243 | 247 |
| 248 // TODO(jsbell): Remove static_cast<> when overloads are eliminated. |
| 244 idb_context->TaskRunner()->PostTask( | 249 idb_context->TaskRunner()->PostTask( |
| 245 FROM_HERE, | 250 FROM_HERE, |
| 246 base::Bind( | 251 base::Bind( |
| 247 &IndexedDBContextImpl::DeleteForOrigin, idb_context, kTestOrigin)); | 252 static_cast<void (IndexedDBContextImpl::*)(const url::Origin&)>( |
| 253 &IndexedDBContextImpl::DeleteForOrigin), |
| 254 idb_context, kTestOrigin)); |
| 248 FlushIndexedDBTaskRunner(); | 255 FlushIndexedDBTaskRunner(); |
| 249 | 256 |
| 250 EXPECT_TRUE(base::DirectoryExists(test_path)); | 257 EXPECT_TRUE(base::DirectoryExists(test_path)); |
| 251 } | 258 } |
| 252 | 259 |
| 253 TEST_F(IndexedDBTest, ForceCloseOpenDatabasesOnCommitFailure) { | 260 TEST_F(IndexedDBTest, ForceCloseOpenDatabasesOnCommitFailure) { |
| 254 const GURL kTestOrigin("http://test/"); | 261 const url::Origin kTestOrigin(GURL("http://test/")); |
| 255 | 262 |
| 256 base::ScopedTempDir temp_dir; | 263 base::ScopedTempDir temp_dir; |
| 257 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 264 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 258 | 265 |
| 259 scoped_refptr<IndexedDBContextImpl> context = | 266 scoped_refptr<IndexedDBContextImpl> context = |
| 260 new IndexedDBContextImpl(temp_dir.path(), special_storage_policy_.get(), | 267 new IndexedDBContextImpl(temp_dir.path(), special_storage_policy_.get(), |
| 261 quota_manager_proxy_.get(), task_runner_.get()); | 268 quota_manager_proxy_.get(), task_runner_.get()); |
| 262 | 269 |
| 263 scoped_refptr<IndexedDBFactoryImpl> factory = | 270 scoped_refptr<IndexedDBFactoryImpl> factory = |
| 264 static_cast<IndexedDBFactoryImpl*>(context->GetIDBFactory()); | 271 static_cast<IndexedDBFactoryImpl*>(context->GetIDBFactory()); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 283 | 290 |
| 284 // Simulate the write failure. | 291 // Simulate the write failure. |
| 285 leveldb::Status status = leveldb::Status::IOError("Simulated failure"); | 292 leveldb::Status status = leveldb::Status::IOError("Simulated failure"); |
| 286 callbacks->connection()->database()->TransactionCommitFailed(status); | 293 callbacks->connection()->database()->TransactionCommitFailed(status); |
| 287 | 294 |
| 288 EXPECT_TRUE(db_callbacks->forced_close_called()); | 295 EXPECT_TRUE(db_callbacks->forced_close_called()); |
| 289 EXPECT_FALSE(factory->IsBackingStoreOpen(kTestOrigin)); | 296 EXPECT_FALSE(factory->IsBackingStoreOpen(kTestOrigin)); |
| 290 } | 297 } |
| 291 | 298 |
| 292 } // namespace content | 299 } // namespace content |
| OLD | NEW |