| 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 <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/logging.h" | 10 #include "base/logging.h" |
| (...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 const Origin origin(GURL("http://localhost:81")); | 246 const Origin origin(GURL("http://localhost:81")); |
| 247 base::ScopedTempDir temp_directory; | 247 base::ScopedTempDir temp_directory; |
| 248 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); | 248 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); |
| 249 | 249 |
| 250 scoped_refptr<DiskFullFactory> factory = new DiskFullFactory(context()); | 250 scoped_refptr<DiskFullFactory> factory = new DiskFullFactory(context()); |
| 251 scoped_refptr<LookingForQuotaErrorMockCallbacks> callbacks = | 251 scoped_refptr<LookingForQuotaErrorMockCallbacks> callbacks = |
| 252 new LookingForQuotaErrorMockCallbacks; | 252 new LookingForQuotaErrorMockCallbacks; |
| 253 scoped_refptr<IndexedDBDatabaseCallbacks> dummy_database_callbacks = | 253 scoped_refptr<IndexedDBDatabaseCallbacks> dummy_database_callbacks = |
| 254 new IndexedDBDatabaseCallbacks(NULL, 0, 0); | 254 new IndexedDBDatabaseCallbacks(NULL, 0, 0); |
| 255 const base::string16 name(ASCIIToUTF16("name")); | 255 const base::string16 name(ASCIIToUTF16("name")); |
| 256 IndexedDBPendingConnection connection(callbacks, | 256 std::unique_ptr<IndexedDBPendingConnection> connection( |
| 257 dummy_database_callbacks, | 257 new IndexedDBPendingConnection(callbacks, dummy_database_callbacks, |
| 258 0, /* child_process_id */ | 258 0, /* child_process_id */ |
| 259 2, /* transaction_id */ | 259 2, /* transaction_id */ |
| 260 1 /* version */); | 260 1 /* version */)); |
| 261 factory->Open(name, connection, NULL /* request_context */, origin, | 261 factory->Open(name, std::move(connection), NULL /* request_context */, origin, |
| 262 temp_directory.path()); | 262 temp_directory.path()); |
| 263 EXPECT_TRUE(callbacks->error_called()); | 263 EXPECT_TRUE(callbacks->error_called()); |
| 264 } | 264 } |
| 265 | 265 |
| 266 TEST_F(IndexedDBFactoryTest, BackingStoreReleasedOnForcedClose) { | 266 TEST_F(IndexedDBFactoryTest, BackingStoreReleasedOnForcedClose) { |
| 267 const Origin origin(GURL("http://localhost:81")); | 267 const Origin origin(GURL("http://localhost:81")); |
| 268 | 268 |
| 269 base::ScopedTempDir temp_directory; | 269 base::ScopedTempDir temp_directory; |
| 270 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); | 270 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); |
| 271 | 271 |
| 272 scoped_refptr<MockIndexedDBCallbacks> callbacks(new MockIndexedDBCallbacks()); | 272 scoped_refptr<MockIndexedDBCallbacks> callbacks(new MockIndexedDBCallbacks()); |
| 273 scoped_refptr<MockIndexedDBDatabaseCallbacks> db_callbacks( | 273 scoped_refptr<MockIndexedDBDatabaseCallbacks> db_callbacks( |
| 274 new MockIndexedDBDatabaseCallbacks()); | 274 new MockIndexedDBDatabaseCallbacks()); |
| 275 const int64_t transaction_id = 1; | 275 const int64_t transaction_id = 1; |
| 276 IndexedDBPendingConnection connection( | 276 std::unique_ptr<IndexedDBPendingConnection> connection( |
| 277 callbacks, db_callbacks, 0, /* child_process_id */ | 277 new IndexedDBPendingConnection( |
| 278 transaction_id, IndexedDBDatabaseMetadata::DEFAULT_VERSION); | 278 callbacks, db_callbacks, 0, /* child_process_id */ |
| 279 factory()->Open(ASCIIToUTF16("db"), connection, NULL /* request_context */, | 279 transaction_id, IndexedDBDatabaseMetadata::DEFAULT_VERSION)); |
| 280 origin, temp_directory.path()); | 280 factory()->Open(ASCIIToUTF16("db"), std::move(connection), |
| 281 NULL /* request_context */, origin, temp_directory.path()); |
| 281 | 282 |
| 282 EXPECT_TRUE(callbacks->connection()); | 283 EXPECT_TRUE(callbacks->connection()); |
| 283 | 284 |
| 284 EXPECT_TRUE(factory()->IsBackingStoreOpen(origin)); | 285 EXPECT_TRUE(factory()->IsBackingStoreOpen(origin)); |
| 285 EXPECT_FALSE(factory()->IsBackingStorePendingClose(origin)); | 286 EXPECT_FALSE(factory()->IsBackingStorePendingClose(origin)); |
| 286 | 287 |
| 287 callbacks->connection()->ForceClose(); | 288 callbacks->connection()->ForceClose(); |
| 288 | 289 |
| 289 EXPECT_FALSE(factory()->IsBackingStoreOpen(origin)); | 290 EXPECT_FALSE(factory()->IsBackingStoreOpen(origin)); |
| 290 EXPECT_FALSE(factory()->IsBackingStorePendingClose(origin)); | 291 EXPECT_FALSE(factory()->IsBackingStorePendingClose(origin)); |
| 291 } | 292 } |
| 292 | 293 |
| 293 TEST_F(IndexedDBFactoryTest, BackingStoreReleaseDelayedOnClose) { | 294 TEST_F(IndexedDBFactoryTest, BackingStoreReleaseDelayedOnClose) { |
| 294 const Origin origin(GURL("http://localhost:81")); | 295 const Origin origin(GURL("http://localhost:81")); |
| 295 | 296 |
| 296 base::ScopedTempDir temp_directory; | 297 base::ScopedTempDir temp_directory; |
| 297 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); | 298 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); |
| 298 | 299 |
| 299 scoped_refptr<MockIndexedDBCallbacks> callbacks(new MockIndexedDBCallbacks()); | 300 scoped_refptr<MockIndexedDBCallbacks> callbacks(new MockIndexedDBCallbacks()); |
| 300 scoped_refptr<MockIndexedDBDatabaseCallbacks> db_callbacks( | 301 scoped_refptr<MockIndexedDBDatabaseCallbacks> db_callbacks( |
| 301 new MockIndexedDBDatabaseCallbacks()); | 302 new MockIndexedDBDatabaseCallbacks()); |
| 302 const int64_t transaction_id = 1; | 303 const int64_t transaction_id = 1; |
| 303 IndexedDBPendingConnection connection( | 304 std::unique_ptr<IndexedDBPendingConnection> connection( |
| 304 callbacks, db_callbacks, 0, /* child_process_id */ | 305 new IndexedDBPendingConnection( |
| 305 transaction_id, IndexedDBDatabaseMetadata::DEFAULT_VERSION); | 306 callbacks, db_callbacks, 0, /* child_process_id */ |
| 306 factory()->Open(ASCIIToUTF16("db"), connection, NULL /* request_context */, | 307 transaction_id, IndexedDBDatabaseMetadata::DEFAULT_VERSION)); |
| 307 origin, temp_directory.path()); | 308 factory()->Open(ASCIIToUTF16("db"), std::move(connection), |
| 309 NULL /* request_context */, origin, temp_directory.path()); |
| 308 | 310 |
| 309 EXPECT_TRUE(callbacks->connection()); | 311 EXPECT_TRUE(callbacks->connection()); |
| 310 IndexedDBBackingStore* store = | 312 IndexedDBBackingStore* store = |
| 311 callbacks->connection()->database()->backing_store(); | 313 callbacks->connection()->database()->backing_store(); |
| 312 EXPECT_FALSE(store->HasOneRef()); // Factory and database. | 314 EXPECT_FALSE(store->HasOneRef()); // Factory and database. |
| 313 | 315 |
| 314 EXPECT_TRUE(factory()->IsBackingStoreOpen(origin)); | 316 EXPECT_TRUE(factory()->IsBackingStoreOpen(origin)); |
| 315 callbacks->connection()->Close(); | 317 callbacks->connection()->Close(); |
| 316 EXPECT_TRUE(store->HasOneRef()); // Factory. | 318 EXPECT_TRUE(store->HasOneRef()); // Factory. |
| 317 EXPECT_TRUE(factory()->IsBackingStoreOpen(origin)); | 319 EXPECT_TRUE(factory()->IsBackingStoreOpen(origin)); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 379 TEST_F(IndexedDBFactoryTest, ForceCloseReleasesBackingStore) { | 381 TEST_F(IndexedDBFactoryTest, ForceCloseReleasesBackingStore) { |
| 380 const Origin origin(GURL("http://localhost:81")); | 382 const Origin origin(GURL("http://localhost:81")); |
| 381 | 383 |
| 382 base::ScopedTempDir temp_directory; | 384 base::ScopedTempDir temp_directory; |
| 383 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); | 385 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); |
| 384 | 386 |
| 385 scoped_refptr<MockIndexedDBCallbacks> callbacks(new MockIndexedDBCallbacks()); | 387 scoped_refptr<MockIndexedDBCallbacks> callbacks(new MockIndexedDBCallbacks()); |
| 386 scoped_refptr<MockIndexedDBDatabaseCallbacks> db_callbacks( | 388 scoped_refptr<MockIndexedDBDatabaseCallbacks> db_callbacks( |
| 387 new MockIndexedDBDatabaseCallbacks()); | 389 new MockIndexedDBDatabaseCallbacks()); |
| 388 const int64_t transaction_id = 1; | 390 const int64_t transaction_id = 1; |
| 389 IndexedDBPendingConnection connection( | 391 std::unique_ptr<IndexedDBPendingConnection> connection( |
| 390 callbacks, db_callbacks, 0, /* child_process_id */ | 392 new IndexedDBPendingConnection( |
| 391 transaction_id, IndexedDBDatabaseMetadata::DEFAULT_VERSION); | 393 callbacks, db_callbacks, 0, /* child_process_id */ |
| 392 factory()->Open(ASCIIToUTF16("db"), connection, NULL /* request_context */, | 394 transaction_id, IndexedDBDatabaseMetadata::DEFAULT_VERSION)); |
| 393 origin, temp_directory.path()); | 395 factory()->Open(ASCIIToUTF16("db"), std::move(connection), |
| 396 NULL /* request_context */, origin, temp_directory.path()); |
| 394 | 397 |
| 395 EXPECT_TRUE(callbacks->connection()); | 398 EXPECT_TRUE(callbacks->connection()); |
| 396 EXPECT_TRUE(factory()->IsBackingStoreOpen(origin)); | 399 EXPECT_TRUE(factory()->IsBackingStoreOpen(origin)); |
| 397 EXPECT_FALSE(factory()->IsBackingStorePendingClose(origin)); | 400 EXPECT_FALSE(factory()->IsBackingStorePendingClose(origin)); |
| 398 | 401 |
| 399 callbacks->connection()->Close(); | 402 callbacks->connection()->Close(); |
| 400 | 403 |
| 401 EXPECT_TRUE(factory()->IsBackingStoreOpen(origin)); | 404 EXPECT_TRUE(factory()->IsBackingStoreOpen(origin)); |
| 402 EXPECT_TRUE(factory()->IsBackingStorePendingClose(origin)); | 405 EXPECT_TRUE(factory()->IsBackingStorePendingClose(origin)); |
| 403 | 406 |
| 404 factory()->ForceClose(origin); | 407 factory()->ForceClose(origin); |
| 405 | 408 |
| 406 EXPECT_FALSE(factory()->IsBackingStoreOpen(origin)); | 409 EXPECT_FALSE(factory()->IsBackingStoreOpen(origin)); |
| 407 EXPECT_FALSE(factory()->IsBackingStorePendingClose(origin)); | 410 EXPECT_FALSE(factory()->IsBackingStorePendingClose(origin)); |
| 408 | 411 |
| 409 // Ensure it is safe if the store is not open. | 412 // Ensure it is safe if the store is not open. |
| 410 factory()->ForceClose(origin); | 413 factory()->ForceClose(origin); |
| 411 } | 414 } |
| 412 | 415 |
| 413 class UpgradeNeededCallbacks : public MockIndexedDBCallbacks { | 416 class UpgradeNeededCallbacks : public MockIndexedDBCallbacks { |
| 414 public: | 417 public: |
| 415 UpgradeNeededCallbacks() {} | 418 UpgradeNeededCallbacks() {} |
| 416 | 419 |
| 417 void OnSuccess(std::unique_ptr<IndexedDBConnection> connection, | 420 void OnSuccess(std::unique_ptr<IndexedDBConnection> connection, |
| 418 const IndexedDBDatabaseMetadata& metadata) override { | 421 const IndexedDBDatabaseMetadata& metadata) override { |
| 419 EXPECT_TRUE(connection_.get()); | 422 EXPECT_TRUE(connection_.get()); |
| 420 EXPECT_FALSE(connection.get()); | 423 EXPECT_FALSE(connection.get()); |
| 421 } | 424 } |
| 422 | 425 |
| 423 void OnUpgradeNeeded( | 426 void OnUpgradeNeeded(int64_t old_version, |
| 424 int64_t old_version, | 427 std::unique_ptr<IndexedDBConnection> connection, |
| 425 std::unique_ptr<IndexedDBConnection> connection, | 428 const content::IndexedDBDatabaseMetadata& metadata, |
| 426 const content::IndexedDBDatabaseMetadata& metadata) override { | 429 const IndexedDBDataLossInfo& data_loss_info) override { |
| 427 connection_ = std::move(connection); | 430 connection_ = std::move(connection); |
| 428 } | 431 } |
| 429 | 432 |
| 430 protected: | 433 protected: |
| 431 ~UpgradeNeededCallbacks() override {} | 434 ~UpgradeNeededCallbacks() override {} |
| 432 | 435 |
| 433 private: | 436 private: |
| 434 DISALLOW_COPY_AND_ASSIGN(UpgradeNeededCallbacks); | 437 DISALLOW_COPY_AND_ASSIGN(UpgradeNeededCallbacks); |
| 435 }; | 438 }; |
| 436 | 439 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 459 const base::string16 db_name(ASCIIToUTF16("db")); | 462 const base::string16 db_name(ASCIIToUTF16("db")); |
| 460 const int64_t db_version = 2; | 463 const int64_t db_version = 2; |
| 461 const int64_t transaction_id = 1; | 464 const int64_t transaction_id = 1; |
| 462 scoped_refptr<IndexedDBDatabaseCallbacks> db_callbacks( | 465 scoped_refptr<IndexedDBDatabaseCallbacks> db_callbacks( |
| 463 new MockIndexedDBDatabaseCallbacks()); | 466 new MockIndexedDBDatabaseCallbacks()); |
| 464 | 467 |
| 465 // Open at version 2, then close. | 468 // Open at version 2, then close. |
| 466 { | 469 { |
| 467 scoped_refptr<MockIndexedDBCallbacks> callbacks( | 470 scoped_refptr<MockIndexedDBCallbacks> callbacks( |
| 468 new UpgradeNeededCallbacks()); | 471 new UpgradeNeededCallbacks()); |
| 469 IndexedDBPendingConnection connection(callbacks, | 472 std::unique_ptr<IndexedDBPendingConnection> connection( |
| 470 db_callbacks, | 473 new IndexedDBPendingConnection(callbacks, db_callbacks, |
| 471 0, /* child_process_id */ | 474 0, /* child_process_id */ |
| 472 transaction_id, | 475 transaction_id, db_version)); |
| 473 db_version); | 476 factory()->Open(db_name, std::move(connection), NULL /* request_context */, |
| 474 factory()->Open(db_name, connection, NULL /* request_context */, origin, | 477 origin, temp_directory.path()); |
| 475 temp_directory.path()); | |
| 476 EXPECT_TRUE(factory()->IsDatabaseOpen(origin, db_name)); | 478 EXPECT_TRUE(factory()->IsDatabaseOpen(origin, db_name)); |
| 477 | 479 |
| 478 // Pump the message loop so the upgrade transaction can run. | 480 // Pump the message loop so the upgrade transaction can run. |
| 479 base::RunLoop().RunUntilIdle(); | 481 base::RunLoop().RunUntilIdle(); |
| 480 EXPECT_TRUE(callbacks->connection()); | 482 EXPECT_TRUE(callbacks->connection()); |
| 481 callbacks->connection()->database()->Commit(transaction_id); | 483 callbacks->connection()->database()->Commit(transaction_id); |
| 482 | 484 |
| 483 callbacks->connection()->Close(); | 485 callbacks->connection()->Close(); |
| 484 EXPECT_FALSE(factory()->IsDatabaseOpen(origin, db_name)); | 486 EXPECT_FALSE(factory()->IsDatabaseOpen(origin, db_name)); |
| 485 } | 487 } |
| 486 | 488 |
| 487 // Open at version < 2, which will fail; ensure factory doesn't retain | 489 // Open at version < 2, which will fail; ensure factory doesn't retain |
| 488 // the database object. | 490 // the database object. |
| 489 { | 491 { |
| 490 scoped_refptr<ErrorCallbacks> callbacks(new ErrorCallbacks()); | 492 scoped_refptr<ErrorCallbacks> callbacks(new ErrorCallbacks()); |
| 491 IndexedDBPendingConnection connection(callbacks, | 493 std::unique_ptr<IndexedDBPendingConnection> connection( |
| 492 db_callbacks, | 494 new IndexedDBPendingConnection(callbacks, db_callbacks, |
| 493 0, /* child_process_id */ | 495 0, /* child_process_id */ |
| 494 transaction_id, | 496 transaction_id, db_version - 1)); |
| 495 db_version - 1); | 497 factory()->Open(db_name, std::move(connection), NULL /* request_context */, |
| 496 factory()->Open(db_name, connection, NULL /* request_context */, origin, | 498 origin, temp_directory.path()); |
| 497 temp_directory.path()); | |
| 498 EXPECT_TRUE(callbacks->saw_error()); | 499 EXPECT_TRUE(callbacks->saw_error()); |
| 499 EXPECT_FALSE(factory()->IsDatabaseOpen(origin, db_name)); | 500 EXPECT_FALSE(factory()->IsDatabaseOpen(origin, db_name)); |
| 500 } | 501 } |
| 501 | 502 |
| 502 // Terminate all pending-close timers. | 503 // Terminate all pending-close timers. |
| 503 factory()->ForceClose(origin); | 504 factory()->ForceClose(origin); |
| 504 } | 505 } |
| 505 | 506 |
| 506 } // namespace content | 507 } // namespace content |
| OLD | NEW |