Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(160)

Side by Side Diff: content/browser/service_worker/service_worker_storage_unittest.cc

Issue 355163003: Don't prematurely delete script resources when registration is deleted (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: patch for landing Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 protected:
542 GURL scope_;
543 GURL script_;
544 GURL import_;
545 GURL document_url_;
546 int64 registration_id_;
547 int64 version_id_;
548 int64 resource_id1_;
549 int64 resource_id2_;
550 scoped_refptr<ServiceWorkerRegistration> registration_;
551 };
552
553 TEST_F(ServiceWorkerResourceStorageTest, DeleteRegistration_WaitingVersion) {
539 bool was_called = false; 554 bool was_called = false;
540 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED; 555 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED;
541 verify_ids.clear(); 556 std::set<int64> verify_ids;
557
558 // Deleting the registration should result in the resources being added to the
559 // purgeable list and then doomed in the disk cache and removed from that
560 // list.
542 storage()->DeleteRegistration( 561 storage()->DeleteRegistration(
543 registration->id(), kScope.GetOrigin(), 562 registration_->id(),
563 scope_.GetOrigin(),
544 base::Bind(&VerifyPurgeableListStatusCallback, 564 base::Bind(&VerifyPurgeableListStatusCallback,
545 base::Unretained(storage()->database_.get()), 565 base::Unretained(storage()->database_.get()),
546 &verify_ids, &was_called, &result)); 566 &verify_ids,
567 &was_called,
568 &result));
547 base::RunLoop().RunUntilIdle(); 569 base::RunLoop().RunUntilIdle();
548 ASSERT_TRUE(was_called); 570 ASSERT_TRUE(was_called);
549 EXPECT_EQ(SERVICE_WORKER_OK, result); 571 EXPECT_EQ(SERVICE_WORKER_OK, result);
572 EXPECT_EQ(2u, verify_ids.size());
573 verify_ids.clear();
574 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK,
575 storage()->database_->GetPurgeableResourceIds(&verify_ids));
576 EXPECT_TRUE(verify_ids.empty());
577
578 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id1_, false));
579 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id2_, false));
580 }
581
582 TEST_F(ServiceWorkerResourceStorageTest, DeleteRegistration_ActiveVersion) {
583 // Promote the worker to active and add a controllee.
584 registration_->set_active_version(registration_->waiting_version());
585 registration_->set_waiting_version(NULL);
586 storage()->UpdateToActiveState(
587 registration_, base::Bind(&ServiceWorkerUtils::NoOpStatusCallback));
588 scoped_ptr<ServiceWorkerProviderHost> host(
589 new ServiceWorkerProviderHost(33 /* dummy render process id */,
590 1 /* dummy provider_id */,
591 context_->AsWeakPtr(),
592 NULL));
593 registration_->active_version()->AddControllee(host.get());
594
595 bool was_called = false;
596 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED;
597 std::set<int64> verify_ids;
598
599 // Deleting the registration should move the resources to the purgeable list
600 // but keep them available.
601 storage()->DeleteRegistration(
602 registration_->id(),
603 scope_.GetOrigin(),
604 base::Bind(&VerifyPurgeableListStatusCallback,
605 base::Unretained(storage()->database_.get()),
606 &verify_ids,
607 &was_called,
608 &result));
609 base::RunLoop().RunUntilIdle();
610 ASSERT_TRUE(was_called);
611 EXPECT_EQ(SERVICE_WORKER_OK, result);
612 EXPECT_EQ(2u, verify_ids.size());
613 verify_ids.clear();
614 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK,
615 storage()->database_->GetPurgeableResourceIds(&verify_ids));
616 EXPECT_EQ(2u, verify_ids.size());
617
618 EXPECT_TRUE(VerifyBasicResponse(storage(), resource_id1_, false));
619 EXPECT_TRUE(VerifyBasicResponse(storage(), resource_id2_, false));
620
621 // Removing the controllee should cause the resources to be deleted.
622 registration_->active_version()->RemoveControllee(host.get());
623 base::RunLoop().RunUntilIdle();
624 verify_ids.clear();
625 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK,
626 storage()->database_->GetPurgeableResourceIds(&verify_ids));
627 EXPECT_TRUE(verify_ids.empty());
628
629 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id1_, false));
630 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id2_, false));
631 }
632
633 TEST_F(ServiceWorkerResourceStorageTest, UpdateRegistration) {
634 // Promote the worker to active worker and add a controllee.
635 registration_->set_active_version(registration_->waiting_version());
636 registration_->set_waiting_version(NULL);
637 storage()->UpdateToActiveState(
638 registration_, base::Bind(&ServiceWorkerUtils::NoOpStatusCallback));
639 scoped_ptr<ServiceWorkerProviderHost> host(
640 new ServiceWorkerProviderHost(33 /* dummy render process id */,
641 1 /* dummy provider_id */,
642 context_->AsWeakPtr(),
643 NULL));
644 registration_->active_version()->AddControllee(host.get());
645
646 bool was_called = false;
647 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED;
648 std::set<int64> verify_ids;
649
650 // Make an updated registration.
651 scoped_refptr<ServiceWorkerVersion> live_version = new ServiceWorkerVersion(
652 registration_, storage()->NewVersionId(), context_ptr_);
653 live_version->SetStatus(ServiceWorkerVersion::NEW);
654 registration_->set_waiting_version(live_version);
655
656 // Writing the registration should move the old version's resources to the
657 // purgeable list but keep them available.
658 storage()->StoreRegistration(
659 registration_,
660 registration_->waiting_version(),
661 base::Bind(&VerifyPurgeableListStatusCallback,
662 base::Unretained(storage()->database_.get()),
663 &verify_ids,
664 &was_called,
665 &result));
666 base::RunLoop().RunUntilIdle();
667 ASSERT_TRUE(was_called);
668 EXPECT_EQ(SERVICE_WORKER_OK, result);
550 EXPECT_EQ(2u, verify_ids.size()); 669 EXPECT_EQ(2u, verify_ids.size());
551 verify_ids.clear(); 670 verify_ids.clear();
552 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, 671 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK,
553 storage()->database_->GetPurgeableResourceIds(&verify_ids)); 672 storage()->database_->GetPurgeableResourceIds(&verify_ids));
673 EXPECT_EQ(2u, verify_ids.size());
674
675 EXPECT_TRUE(VerifyBasicResponse(storage(), resource_id1_, false));
676 EXPECT_TRUE(VerifyBasicResponse(storage(), resource_id2_, false));
677
678 // Removing the controllee should cause the old version's resources to be
679 // deleted.
680 registration_->active_version()->RemoveControllee(host.get());
681 base::RunLoop().RunUntilIdle();
682 verify_ids.clear();
683 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK,
684 storage()->database_->GetPurgeableResourceIds(&verify_ids));
554 EXPECT_TRUE(verify_ids.empty()); 685 EXPECT_TRUE(verify_ids.empty());
555 686
556 EXPECT_FALSE(VerifyBasicResponse(storage(), kResourceId1, false)); 687 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id1_, false));
557 EXPECT_FALSE(VerifyBasicResponse(storage(), kResourceId2, false)); 688 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id2_, false));
558 } 689 }
559 690
560 TEST_F(ServiceWorkerStorageTest, FindRegistration_LongestScopeMatch) { 691 TEST_F(ServiceWorkerStorageTest, FindRegistration_LongestScopeMatch) {
561 const GURL kDocumentUrl("http://www.example.com/scope/foo"); 692 const GURL kDocumentUrl("http://www.example.com/scope/foo");
562 scoped_refptr<ServiceWorkerRegistration> found_registration; 693 scoped_refptr<ServiceWorkerRegistration> found_registration;
563 694
564 // Registration for "/scope/*". 695 // Registration for "/scope/*".
565 const GURL kScope1("http://www.example.com/scope/*"); 696 const GURL kScope1("http://www.example.com/scope/*");
566 const GURL kScript1("http://www.example.com/script1.js"); 697 const GURL kScript1("http://www.example.com/script1.js");
567 const int64 kRegistrationId1 = 1; 698 const int64 kRegistrationId1 = 1;
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
630 storage()->NotifyDoneInstallingRegistration( 761 storage()->NotifyDoneInstallingRegistration(
631 live_registration3, NULL, SERVICE_WORKER_OK); 762 live_registration3, NULL, SERVICE_WORKER_OK);
632 763
633 // Find a registration among installed ones. 764 // Find a registration among installed ones.
634 EXPECT_EQ(SERVICE_WORKER_OK, 765 EXPECT_EQ(SERVICE_WORKER_OK,
635 FindRegistrationForDocument(kDocumentUrl, &found_registration)); 766 FindRegistrationForDocument(kDocumentUrl, &found_registration));
636 EXPECT_EQ(live_registration2, found_registration); 767 EXPECT_EQ(live_registration2, found_registration);
637 } 768 }
638 769
639 } // namespace content 770 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/service_worker/service_worker_storage.cc ('k') | content/browser/service_worker/service_worker_version.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698