Chromium Code Reviews| 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/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "content/browser/browser_thread_impl.h" | 10 #include "content/browser/browser_thread_impl.h" |
| 11 #include "content/browser/service_worker/service_worker_context_core.h" | 11 #include "content/browser/service_worker/service_worker_context_core.h" |
| 12 #include "content/browser/service_worker/service_worker_disk_cache.h" | 12 #include "content/browser/service_worker/service_worker_disk_cache.h" |
| 13 #include "content/browser/service_worker/service_worker_registration.h" | 13 #include "content/browser/service_worker/service_worker_registration.h" |
| 14 #include "content/browser/service_worker/service_worker_storage.h" | 14 #include "content/browser/service_worker/service_worker_storage.h" |
| 15 #include "content/browser/service_worker/service_worker_utils.h" | |
| 15 #include "content/browser/service_worker/service_worker_version.h" | 16 #include "content/browser/service_worker/service_worker_version.h" |
| 16 #include "content/common/service_worker/service_worker_status_code.h" | 17 #include "content/common/service_worker/service_worker_status_code.h" |
| 17 #include "content/public/test/test_browser_thread_bundle.h" | 18 #include "content/public/test/test_browser_thread_bundle.h" |
| 18 #include "net/base/io_buffer.h" | 19 #include "net/base/io_buffer.h" |
| 19 #include "net/base/net_errors.h" | 20 #include "net/base/net_errors.h" |
| 20 #include "net/http/http_response_headers.h" | 21 #include "net/http/http_response_headers.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 23 |
| 23 using net::IOBuffer; | 24 using net::IOBuffer; |
| 24 using net::WrappedIOBuffer; | 25 using net::WrappedIOBuffer; |
| (...skipping 453 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 478 EXPECT_FALSE(found_registration); | 479 EXPECT_FALSE(found_registration); |
| 479 | 480 |
| 480 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, | 481 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, |
| 481 FindRegistrationForPattern(kScope, &found_registration)); | 482 FindRegistrationForPattern(kScope, &found_registration)); |
| 482 EXPECT_FALSE(found_registration); | 483 EXPECT_FALSE(found_registration); |
| 483 | 484 |
| 484 GetAllRegistrations(&all_registrations); | 485 GetAllRegistrations(&all_registrations); |
| 485 EXPECT_TRUE(all_registrations.empty()); | 486 EXPECT_TRUE(all_registrations.empty()); |
| 486 } | 487 } |
| 487 | 488 |
| 488 TEST_F(ServiceWorkerStorageTest, ResourceIdsAreStoredAndPurged) { | 489 class ServiceWorkerResourceStorageTest : public ServiceWorkerStorageTest { |
| 489 storage()->LazyInitialize(base::Bind(&base::DoNothing)); | 490 virtual void SetUp() OVERRIDE { |
| 490 base::RunLoop().RunUntilIdle(); | 491 ServiceWorkerStorageTest::SetUp(); |
| 491 const GURL kScope("http://www.test.not/scope/*"); | |
| 492 const GURL kScript("http://www.test.not/script.js"); | |
| 493 const GURL kImport("http://www.test.not/import.js"); | |
| 494 const GURL kDocumentUrl("http://www.test.not/scope/document.html"); | |
| 495 const int64 kRegistrationId = storage()->NewRegistrationId(); | |
| 496 const int64 kVersionId = storage()->NewVersionId(); | |
| 497 const int64 kResourceId1 = storage()->NewResourceId(); | |
| 498 const int64 kResourceId2 = storage()->NewResourceId(); | |
| 499 | 492 |
| 500 // Cons up a new registration+version with two script resources. | 493 storage()->LazyInitialize(base::Bind(&base::DoNothing)); |
| 501 RegistrationData data; | 494 base::RunLoop().RunUntilIdle(); |
| 502 data.registration_id = kRegistrationId; | 495 scope_ = GURL("http://www.test.not/scope/*"); |
| 503 data.scope = kScope; | 496 script_ = GURL("http://www.test.not/script.js"); |
| 504 data.script = kScript; | 497 import_ = GURL("http://www.test.not/import.js"); |
| 505 data.version_id = kVersionId; | 498 document_url_ = GURL("http://www.test.not/scope/document.html"); |
| 506 data.is_active = false; | 499 registration_id_ = storage()->NewRegistrationId(); |
| 507 std::vector<ResourceRecord> resources; | 500 version_id_ = storage()->NewVersionId(); |
| 508 resources.push_back(ResourceRecord(kResourceId1, kScript)); | 501 resource_id1_ = storage()->NewResourceId(); |
| 509 resources.push_back(ResourceRecord(kResourceId2, kImport)); | 502 resource_id2_ = storage()->NewResourceId(); |
| 510 scoped_refptr<ServiceWorkerRegistration> registration = | |
| 511 storage()->GetOrCreateRegistration(data, resources); | |
| 512 registration->waiting_version()->SetStatus(ServiceWorkerVersion::NEW); | |
| 513 | 503 |
| 514 // Add the resources ids to the uncommitted list. | 504 // Cons up a new registration+version with two script resources. |
| 515 std::set<int64> resource_ids; | 505 RegistrationData data; |
| 516 resource_ids.insert(kResourceId1); | 506 data.registration_id = registration_id_; |
| 517 resource_ids.insert(kResourceId2); | 507 data.scope = scope_; |
| 518 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, | 508 data.script = script_; |
| 519 storage()->database_->WriteUncommittedResourceIds(resource_ids)); | 509 data.version_id = version_id_; |
| 510 data.is_active = false; | |
| 511 std::vector<ResourceRecord> resources; | |
| 512 resources.push_back(ResourceRecord(resource_id1_, script_)); | |
| 513 resources.push_back(ResourceRecord(resource_id2_, import_)); | |
| 514 registration_ = storage()->GetOrCreateRegistration(data, resources); | |
| 515 registration_->waiting_version()->SetStatus(ServiceWorkerVersion::NEW); | |
| 520 | 516 |
| 521 // And dump something in the disk cache for them. | 517 // Add the resources ids to the uncommitted list. |
| 522 WriteBasicResponse(storage(), kResourceId1); | 518 std::set<int64> resource_ids; |
| 523 WriteBasicResponse(storage(), kResourceId2); | 519 resource_ids.insert(resource_id1_); |
| 524 EXPECT_TRUE(VerifyBasicResponse(storage(), kResourceId1, true)); | 520 resource_ids.insert(resource_id2_); |
| 525 EXPECT_TRUE(VerifyBasicResponse(storage(), kResourceId2, true)); | 521 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, |
| 522 storage()->database_->WriteUncommittedResourceIds(resource_ids)); | |
| 526 | 523 |
| 527 // Storing the registration/version should take the resources ids out | 524 // And dump something in the disk cache for them. |
| 528 // of the uncommitted list. | 525 WriteBasicResponse(storage(), resource_id1_); |
| 529 EXPECT_EQ(SERVICE_WORKER_OK, | 526 WriteBasicResponse(storage(), resource_id2_); |
| 530 StoreRegistration(registration, registration->waiting_version())); | 527 EXPECT_TRUE(VerifyBasicResponse(storage(), resource_id1_, true)); |
| 531 std::set<int64> verify_ids; | 528 EXPECT_TRUE(VerifyBasicResponse(storage(), resource_id2_, true)); |
| 532 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, | |
| 533 storage()->database_->GetUncommittedResourceIds(&verify_ids)); | |
| 534 EXPECT_TRUE(verify_ids.empty()); | |
| 535 | 529 |
| 536 // Deleting it should result in the resources being added to the | 530 // Storing the registration/version should take the resources ids out |
| 537 // purgeable list and then doomed in the disk cache and removed from | 531 // of the uncommitted list. |
| 538 // that list. | 532 EXPECT_EQ( |
| 533 SERVICE_WORKER_OK, | |
| 534 StoreRegistration(registration_, registration_->waiting_version())); | |
| 535 std::set<int64> verify_ids; | |
| 536 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, | |
| 537 storage()->database_->GetUncommittedResourceIds(&verify_ids)); | |
| 538 EXPECT_TRUE(verify_ids.empty()); | |
| 539 } | |
| 540 | |
| 541 virtual void TearDown() OVERRIDE { ServiceWorkerStorageTest::TearDown(); } | |
|
michaeln
2014/07/01 20:54:07
not needed
falken
2014/07/02 04:04:24
oops, done
| |
| 542 | |
| 543 protected: | |
| 544 GURL scope_; | |
| 545 GURL script_; | |
| 546 GURL import_; | |
| 547 GURL document_url_; | |
| 548 int64 registration_id_; | |
| 549 int64 version_id_; | |
| 550 int64 resource_id1_; | |
| 551 int64 resource_id2_; | |
| 552 scoped_refptr<ServiceWorkerRegistration> registration_; | |
| 553 }; | |
| 554 | |
| 555 TEST_F(ServiceWorkerResourceStorageTest, DeleteRegistration_WaitingVersion) { | |
| 539 bool was_called = false; | 556 bool was_called = false; |
| 540 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED; | 557 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED; |
| 541 verify_ids.clear(); | 558 std::set<int64> verify_ids; |
| 559 | |
| 560 // Deleting the registration should result in the resources being added to the | |
| 561 // purgeable list and then doomed in the disk cache and removed from that | |
| 562 // list. | |
| 542 storage()->DeleteRegistration( | 563 storage()->DeleteRegistration( |
| 543 registration->id(), kScope.GetOrigin(), | 564 registration_->id(), |
| 565 scope_.GetOrigin(), | |
| 544 base::Bind(&VerifyPurgeableListStatusCallback, | 566 base::Bind(&VerifyPurgeableListStatusCallback, |
| 545 base::Unretained(storage()->database_.get()), | 567 base::Unretained(storage()->database_.get()), |
| 546 &verify_ids, &was_called, &result)); | 568 &verify_ids, |
| 569 &was_called, | |
| 570 &result)); | |
| 547 base::RunLoop().RunUntilIdle(); | 571 base::RunLoop().RunUntilIdle(); |
| 548 ASSERT_TRUE(was_called); | 572 ASSERT_TRUE(was_called); |
| 549 EXPECT_EQ(SERVICE_WORKER_OK, result); | 573 EXPECT_EQ(SERVICE_WORKER_OK, result); |
| 574 EXPECT_EQ(2u, verify_ids.size()); | |
| 575 verify_ids.clear(); | |
| 576 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, | |
| 577 storage()->database_->GetPurgeableResourceIds(&verify_ids)); | |
| 578 EXPECT_TRUE(verify_ids.empty()); | |
| 579 | |
| 580 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id1_, false)); | |
| 581 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id2_, false)); | |
| 582 } | |
| 583 | |
| 584 TEST_F(ServiceWorkerResourceStorageTest, DeleteRegistration_ActiveVersion) { | |
| 585 // Promote the worker to active and add a controllee. | |
| 586 registration_->set_active_version(registration_->waiting_version()); | |
| 587 registration_->set_waiting_version(NULL); | |
| 588 storage()->UpdateToActiveState( | |
| 589 registration_, base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | |
| 590 scoped_ptr<ServiceWorkerProviderHost> host( | |
| 591 new ServiceWorkerProviderHost(33 /* dummy render process id */, | |
| 592 1 /* dummy provider_id */, | |
| 593 context_->AsWeakPtr(), | |
| 594 NULL)); | |
| 595 registration_->active_version()->AddControllee(host.get()); | |
| 596 | |
| 597 bool was_called = false; | |
| 598 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED; | |
| 599 std::set<int64> verify_ids; | |
| 600 | |
| 601 // Deleting the registration should move the resources to the purgeable list | |
| 602 // but keep them available. | |
| 603 storage()->DeleteRegistration( | |
| 604 registration_->id(), | |
| 605 scope_.GetOrigin(), | |
| 606 base::Bind(&VerifyPurgeableListStatusCallback, | |
| 607 base::Unretained(storage()->database_.get()), | |
| 608 &verify_ids, | |
| 609 &was_called, | |
| 610 &result)); | |
| 611 base::RunLoop().RunUntilIdle(); | |
| 612 ASSERT_TRUE(was_called); | |
| 613 EXPECT_EQ(SERVICE_WORKER_OK, result); | |
| 614 EXPECT_EQ(2u, verify_ids.size()); | |
| 615 verify_ids.clear(); | |
| 616 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, | |
| 617 storage()->database_->GetPurgeableResourceIds(&verify_ids)); | |
| 618 EXPECT_EQ(2u, verify_ids.size()); | |
| 619 | |
| 620 EXPECT_TRUE(VerifyBasicResponse(storage(), resource_id1_, false)); | |
| 621 EXPECT_TRUE(VerifyBasicResponse(storage(), resource_id2_, false)); | |
| 622 | |
| 623 // Removing the controllee should cause the resources to be deleted. | |
| 624 registration_->active_version()->RemoveControllee(host.get()); | |
| 625 base::RunLoop().RunUntilIdle(); | |
| 626 verify_ids.clear(); | |
| 627 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, | |
| 628 storage()->database_->GetPurgeableResourceIds(&verify_ids)); | |
| 629 EXPECT_TRUE(verify_ids.empty()); | |
| 630 | |
| 631 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id1_, false)); | |
| 632 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id2_, false)); | |
| 633 } | |
| 634 | |
| 635 TEST_F(ServiceWorkerResourceStorageTest, UpdateRegistration) { | |
| 636 // Promote the worker to active worker and add a controllee. | |
| 637 registration_->set_active_version(registration_->waiting_version()); | |
| 638 registration_->set_waiting_version(NULL); | |
| 639 storage()->UpdateToActiveState( | |
| 640 registration_, base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | |
| 641 scoped_ptr<ServiceWorkerProviderHost> host( | |
| 642 new ServiceWorkerProviderHost(33 /* dummy render process id */, | |
| 643 1 /* dummy provider_id */, | |
| 644 context_->AsWeakPtr(), | |
| 645 NULL)); | |
| 646 registration_->active_version()->AddControllee(host.get()); | |
| 647 | |
| 648 bool was_called = false; | |
| 649 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED; | |
| 650 std::set<int64> verify_ids; | |
| 651 | |
| 652 // Make an updated registration. | |
| 653 scoped_refptr<ServiceWorkerVersion> live_version = new ServiceWorkerVersion( | |
| 654 registration_, storage()->NewVersionId(), context_ptr_); | |
| 655 live_version->SetStatus(ServiceWorkerVersion::NEW); | |
| 656 registration_->set_waiting_version(live_version); | |
| 657 | |
| 658 // Writing the registration should move the old version's resources to the | |
| 659 // purgeable list but keep them available. | |
| 660 storage()->StoreRegistration( | |
| 661 registration_, | |
| 662 registration_->waiting_version(), | |
| 663 base::Bind(&VerifyPurgeableListStatusCallback, | |
| 664 base::Unretained(storage()->database_.get()), | |
| 665 &verify_ids, | |
| 666 &was_called, | |
| 667 &result)); | |
| 668 base::RunLoop().RunUntilIdle(); | |
| 669 ASSERT_TRUE(was_called); | |
| 670 EXPECT_EQ(SERVICE_WORKER_OK, result); | |
| 550 EXPECT_EQ(2u, verify_ids.size()); | 671 EXPECT_EQ(2u, verify_ids.size()); |
| 551 verify_ids.clear(); | 672 verify_ids.clear(); |
| 552 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, | 673 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, |
| 553 storage()->database_->GetPurgeableResourceIds(&verify_ids)); | 674 storage()->database_->GetPurgeableResourceIds(&verify_ids)); |
| 675 EXPECT_EQ(2u, verify_ids.size()); | |
| 676 | |
| 677 EXPECT_TRUE(VerifyBasicResponse(storage(), resource_id1_, false)); | |
| 678 EXPECT_TRUE(VerifyBasicResponse(storage(), resource_id2_, false)); | |
| 679 | |
| 680 // Removing the controllee should cause the old version's resources to be | |
| 681 // deleted. | |
| 682 registration_->active_version()->RemoveControllee(host.get()); | |
| 683 base::RunLoop().RunUntilIdle(); | |
| 684 verify_ids.clear(); | |
| 685 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, | |
| 686 storage()->database_->GetPurgeableResourceIds(&verify_ids)); | |
| 554 EXPECT_TRUE(verify_ids.empty()); | 687 EXPECT_TRUE(verify_ids.empty()); |
| 555 | 688 |
| 556 EXPECT_FALSE(VerifyBasicResponse(storage(), kResourceId1, false)); | 689 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id1_, false)); |
| 557 EXPECT_FALSE(VerifyBasicResponse(storage(), kResourceId2, false)); | 690 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id2_, false)); |
| 558 } | 691 } |
| 559 | 692 |
| 560 TEST_F(ServiceWorkerStorageTest, FindRegistration_LongestScopeMatch) { | 693 TEST_F(ServiceWorkerStorageTest, FindRegistration_LongestScopeMatch) { |
| 561 const GURL kDocumentUrl("http://www.example.com/scope/foo"); | 694 const GURL kDocumentUrl("http://www.example.com/scope/foo"); |
| 562 scoped_refptr<ServiceWorkerRegistration> found_registration; | 695 scoped_refptr<ServiceWorkerRegistration> found_registration; |
| 563 | 696 |
| 564 // Registration for "/scope/*". | 697 // Registration for "/scope/*". |
| 565 const GURL kScope1("http://www.example.com/scope/*"); | 698 const GURL kScope1("http://www.example.com/scope/*"); |
| 566 const GURL kScript1("http://www.example.com/script1.js"); | 699 const GURL kScript1("http://www.example.com/script1.js"); |
| 567 const int64 kRegistrationId1 = 1; | 700 const int64 kRegistrationId1 = 1; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 630 storage()->NotifyDoneInstallingRegistration( | 763 storage()->NotifyDoneInstallingRegistration( |
| 631 live_registration3, NULL, SERVICE_WORKER_OK); | 764 live_registration3, NULL, SERVICE_WORKER_OK); |
| 632 | 765 |
| 633 // Find a registration among installed ones. | 766 // Find a registration among installed ones. |
| 634 EXPECT_EQ(SERVICE_WORKER_OK, | 767 EXPECT_EQ(SERVICE_WORKER_OK, |
| 635 FindRegistrationForDocument(kDocumentUrl, &found_registration)); | 768 FindRegistrationForDocument(kDocumentUrl, &found_registration)); |
| 636 EXPECT_EQ(live_registration2, found_registration); | 769 EXPECT_EQ(live_registration2, found_registration); |
| 637 } | 770 } |
| 638 | 771 |
| 639 } // namespace content | 772 } // namespace content |
| OLD | NEW |