| 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 <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| (...skipping 506 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 517 data.script = script_; | 517 data.script = script_; |
| 518 data.version_id = version_id_; | 518 data.version_id = version_id_; |
| 519 data.is_active = false; | 519 data.is_active = false; |
| 520 std::vector<ResourceRecord> resources; | 520 std::vector<ResourceRecord> resources; |
| 521 resources.push_back(ResourceRecord(resource_id1_, script_)); | 521 resources.push_back(ResourceRecord(resource_id1_, script_)); |
| 522 resources.push_back(ResourceRecord(resource_id2_, import_)); | 522 resources.push_back(ResourceRecord(resource_id2_, import_)); |
| 523 registration_ = storage()->GetOrCreateRegistration(data, resources); | 523 registration_ = storage()->GetOrCreateRegistration(data, resources); |
| 524 registration_->waiting_version()->SetStatus(ServiceWorkerVersion::NEW); | 524 registration_->waiting_version()->SetStatus(ServiceWorkerVersion::NEW); |
| 525 | 525 |
| 526 // Add the resources ids to the uncommitted list. | 526 // Add the resources ids to the uncommitted list. |
| 527 std::set<int64> resource_ids; | 527 storage()->StoreUncommittedResponseId(resource_id1_); |
| 528 resource_ids.insert(resource_id1_); | 528 storage()->StoreUncommittedResponseId(resource_id2_); |
| 529 resource_ids.insert(resource_id2_); | 529 base::RunLoop().RunUntilIdle(); |
| 530 std::set<int64> verify_ids; |
| 530 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, | 531 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, |
| 531 storage()->database_->WriteUncommittedResourceIds(resource_ids)); | 532 storage()->database_->GetUncommittedResourceIds(&verify_ids)); |
| 533 EXPECT_EQ(2u, verify_ids.size()); |
| 532 | 534 |
| 533 // And dump something in the disk cache for them. | 535 // And dump something in the disk cache for them. |
| 534 WriteBasicResponse(storage(), resource_id1_); | 536 WriteBasicResponse(storage(), resource_id1_); |
| 535 WriteBasicResponse(storage(), resource_id2_); | 537 WriteBasicResponse(storage(), resource_id2_); |
| 536 EXPECT_TRUE(VerifyBasicResponse(storage(), resource_id1_, true)); | 538 EXPECT_TRUE(VerifyBasicResponse(storage(), resource_id1_, true)); |
| 537 EXPECT_TRUE(VerifyBasicResponse(storage(), resource_id2_, true)); | 539 EXPECT_TRUE(VerifyBasicResponse(storage(), resource_id2_, true)); |
| 538 | 540 |
| 539 // Storing the registration/version should take the resources ids out | 541 // Storing the registration/version should take the resources ids out |
| 540 // of the uncommitted list. | 542 // of the uncommitted list. |
| 541 EXPECT_EQ( | 543 EXPECT_EQ( |
| 542 SERVICE_WORKER_OK, | 544 SERVICE_WORKER_OK, |
| 543 StoreRegistration(registration_, registration_->waiting_version())); | 545 StoreRegistration(registration_, registration_->waiting_version())); |
| 544 std::set<int64> verify_ids; | 546 verify_ids.clear(); |
| 545 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, | 547 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, |
| 546 storage()->database_->GetUncommittedResourceIds(&verify_ids)); | 548 storage()->database_->GetUncommittedResourceIds(&verify_ids)); |
| 547 EXPECT_TRUE(verify_ids.empty()); | 549 EXPECT_TRUE(verify_ids.empty()); |
| 548 } | 550 } |
| 549 | 551 |
| 550 protected: | 552 protected: |
| 551 GURL scope_; | 553 GURL scope_; |
| 552 GURL script_; | 554 GURL script_; |
| 553 GURL import_; | 555 GURL import_; |
| 554 GURL document_url_; | 556 GURL document_url_; |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 692 EXPECT_EQ(SERVICE_WORKER_OK, result); | 694 EXPECT_EQ(SERVICE_WORKER_OK, result); |
| 693 EXPECT_EQ(2u, verify_ids.size()); | 695 EXPECT_EQ(2u, verify_ids.size()); |
| 694 verify_ids.clear(); | 696 verify_ids.clear(); |
| 695 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, | 697 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, |
| 696 storage()->database_->GetPurgeableResourceIds(&verify_ids)); | 698 storage()->database_->GetPurgeableResourceIds(&verify_ids)); |
| 697 EXPECT_EQ(2u, verify_ids.size()); | 699 EXPECT_EQ(2u, verify_ids.size()); |
| 698 | 700 |
| 699 EXPECT_TRUE(VerifyBasicResponse(storage(), resource_id1_, true)); | 701 EXPECT_TRUE(VerifyBasicResponse(storage(), resource_id1_, true)); |
| 700 EXPECT_TRUE(VerifyBasicResponse(storage(), resource_id2_, true)); | 702 EXPECT_TRUE(VerifyBasicResponse(storage(), resource_id2_, true)); |
| 701 | 703 |
| 702 // Simulate browser restart. This should trigger stale resource purging. | 704 // Also add an uncommitted resource. |
| 705 int64 kStaleUncommittedResourceId = storage()->NewResourceId(); |
| 706 storage()->StoreUncommittedResponseId(kStaleUncommittedResourceId); |
| 707 base::RunLoop().RunUntilIdle(); |
| 708 verify_ids.clear(); |
| 709 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, |
| 710 storage()->database_->GetUncommittedResourceIds(&verify_ids)); |
| 711 EXPECT_EQ(1u, verify_ids.size()); |
| 712 WriteBasicResponse(storage(), kStaleUncommittedResourceId); |
| 713 EXPECT_TRUE( |
| 714 VerifyBasicResponse(storage(), kStaleUncommittedResourceId, true)); |
| 715 |
| 716 // Simulate browser shutdown. The purgeable and uncommitted resources are now |
| 717 // stale. |
| 703 context_.reset(); | 718 context_.reset(); |
| 704 context_.reset(new ServiceWorkerContextCore(GetUserDataDirectory(), | 719 context_.reset(new ServiceWorkerContextCore(GetUserDataDirectory(), |
| 705 base::MessageLoopProxy::current(), | 720 base::MessageLoopProxy::current(), |
| 706 base::MessageLoopProxy::current(), | 721 base::MessageLoopProxy::current(), |
| 707 NULL, | 722 NULL, |
| 708 NULL, | 723 NULL, |
| 709 NULL)); | 724 NULL)); |
| 710 // Use FindRegistration to force storage system initialization. | 725 storage()->LazyInitialize(base::Bind(&base::DoNothing)); |
| 711 scoped_refptr<ServiceWorkerRegistration> found_registration; | |
| 712 FindRegistrationForDocument(document_url_, &found_registration); | |
| 713 base::RunLoop().RunUntilIdle(); | 726 base::RunLoop().RunUntilIdle(); |
| 714 | 727 |
| 715 // Stale resources should be gone. | 728 // Store a new uncommitted resource. This triggers stale resource cleanup. |
| 729 int64 kNewResourceId = storage()->NewResourceId(); |
| 730 WriteBasicResponse(storage(), kNewResourceId); |
| 731 storage()->StoreUncommittedResponseId(kNewResourceId); |
| 732 base::RunLoop().RunUntilIdle(); |
| 733 |
| 734 // The stale resources should be purged, but the new resource should persist. |
| 735 verify_ids.clear(); |
| 736 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, |
| 737 storage()->database_->GetUncommittedResourceIds(&verify_ids)); |
| 738 ASSERT_EQ(1u, verify_ids.size()); |
| 739 EXPECT_EQ(kNewResourceId, *verify_ids.begin()); |
| 740 |
| 716 verify_ids.clear(); | 741 verify_ids.clear(); |
| 717 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, | 742 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, |
| 718 storage()->database_->GetPurgeableResourceIds(&verify_ids)); | 743 storage()->database_->GetPurgeableResourceIds(&verify_ids)); |
| 719 EXPECT_TRUE(verify_ids.empty()); | 744 EXPECT_TRUE(verify_ids.empty()); |
| 720 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id1_, false)); | 745 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id1_, false)); |
| 721 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id2_, false)); | 746 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id2_, false)); |
| 747 EXPECT_FALSE( |
| 748 VerifyBasicResponse(storage(), kStaleUncommittedResourceId, false)); |
| 749 EXPECT_TRUE(VerifyBasicResponse(storage(), kNewResourceId, true)); |
| 722 } | 750 } |
| 723 | 751 |
| 724 TEST_F(ServiceWorkerResourceStorageTest, UpdateRegistration) { | 752 TEST_F(ServiceWorkerResourceStorageTest, UpdateRegistration) { |
| 725 // Promote the worker to active worker and add a controllee. | 753 // Promote the worker to active worker and add a controllee. |
| 726 registration_->SetActiveVersion(registration_->waiting_version()); | 754 registration_->SetActiveVersion(registration_->waiting_version()); |
| 727 storage()->UpdateToActiveState( | 755 storage()->UpdateToActiveState( |
| 728 registration_, base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 756 registration_, base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 729 scoped_ptr<ServiceWorkerProviderHost> host( | 757 scoped_ptr<ServiceWorkerProviderHost> host( |
| 730 new ServiceWorkerProviderHost(33 /* dummy render process id */, | 758 new ServiceWorkerProviderHost(33 /* dummy render process id */, |
| 731 1 /* dummy provider_id */, | 759 1 /* dummy provider_id */, |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 851 storage()->NotifyDoneInstallingRegistration( | 879 storage()->NotifyDoneInstallingRegistration( |
| 852 live_registration3, NULL, SERVICE_WORKER_OK); | 880 live_registration3, NULL, SERVICE_WORKER_OK); |
| 853 | 881 |
| 854 // Find a registration among installed ones. | 882 // Find a registration among installed ones. |
| 855 EXPECT_EQ(SERVICE_WORKER_OK, | 883 EXPECT_EQ(SERVICE_WORKER_OK, |
| 856 FindRegistrationForDocument(kDocumentUrl, &found_registration)); | 884 FindRegistrationForDocument(kDocumentUrl, &found_registration)); |
| 857 EXPECT_EQ(live_registration2, found_registration); | 885 EXPECT_EQ(live_registration2, found_registration); |
| 858 } | 886 } |
| 859 | 887 |
| 860 } // namespace content | 888 } // namespace content |
| OLD | NEW |