| 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" |
| (...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 EXPECT_FALSE(found_registration); | 299 EXPECT_FALSE(found_registration); |
| 300 | 300 |
| 301 // Store something. | 301 // Store something. |
| 302 scoped_refptr<ServiceWorkerRegistration> live_registration = | 302 scoped_refptr<ServiceWorkerRegistration> live_registration = |
| 303 new ServiceWorkerRegistration( | 303 new ServiceWorkerRegistration( |
| 304 kScope, kScript, kRegistrationId, context_ptr_); | 304 kScope, kScript, kRegistrationId, context_ptr_); |
| 305 scoped_refptr<ServiceWorkerVersion> live_version = | 305 scoped_refptr<ServiceWorkerVersion> live_version = |
| 306 new ServiceWorkerVersion( | 306 new ServiceWorkerVersion( |
| 307 live_registration, kVersionId, context_ptr_); | 307 live_registration, kVersionId, context_ptr_); |
| 308 live_version->SetStatus(ServiceWorkerVersion::INSTALLED); | 308 live_version->SetStatus(ServiceWorkerVersion::INSTALLED); |
| 309 live_registration->set_waiting_version(live_version); | 309 live_registration->SetWaitingVersion(live_version); |
| 310 EXPECT_EQ(SERVICE_WORKER_OK, | 310 EXPECT_EQ(SERVICE_WORKER_OK, |
| 311 StoreRegistration(live_registration, live_version)); | 311 StoreRegistration(live_registration, live_version)); |
| 312 | 312 |
| 313 // Now we should find it and get the live ptr back immediately. | 313 // Now we should find it and get the live ptr back immediately. |
| 314 EXPECT_EQ(SERVICE_WORKER_OK, | 314 EXPECT_EQ(SERVICE_WORKER_OK, |
| 315 FindRegistrationForDocument(kDocumentUrl, &found_registration)); | 315 FindRegistrationForDocument(kDocumentUrl, &found_registration)); |
| 316 EXPECT_EQ(live_registration, found_registration); | 316 EXPECT_EQ(live_registration, found_registration); |
| 317 found_registration = NULL; | 317 found_registration = NULL; |
| 318 | 318 |
| 319 // But FindRegistrationForPattern is always async. | 319 // But FindRegistrationForPattern is always async. |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 353 EXPECT_EQ(kRegistrationId, found_registration->id()); | 353 EXPECT_EQ(kRegistrationId, found_registration->id()); |
| 354 EXPECT_TRUE(found_registration->HasOneRef()); | 354 EXPECT_TRUE(found_registration->HasOneRef()); |
| 355 EXPECT_FALSE(found_registration->active_version()); | 355 EXPECT_FALSE(found_registration->active_version()); |
| 356 ASSERT_TRUE(found_registration->waiting_version()); | 356 ASSERT_TRUE(found_registration->waiting_version()); |
| 357 EXPECT_EQ(ServiceWorkerVersion::INSTALLED, | 357 EXPECT_EQ(ServiceWorkerVersion::INSTALLED, |
| 358 found_registration->waiting_version()->status()); | 358 found_registration->waiting_version()->status()); |
| 359 | 359 |
| 360 // Update to active. | 360 // Update to active. |
| 361 scoped_refptr<ServiceWorkerVersion> temp_version = | 361 scoped_refptr<ServiceWorkerVersion> temp_version = |
| 362 found_registration->waiting_version(); | 362 found_registration->waiting_version(); |
| 363 found_registration->set_waiting_version(NULL); | |
| 364 temp_version->SetStatus(ServiceWorkerVersion::ACTIVE); | 363 temp_version->SetStatus(ServiceWorkerVersion::ACTIVE); |
| 365 found_registration->set_active_version(temp_version); | 364 found_registration->SetActiveVersion(temp_version); |
| 366 temp_version = NULL; | 365 temp_version = NULL; |
| 367 EXPECT_EQ(SERVICE_WORKER_OK, UpdateToActiveState(found_registration)); | 366 EXPECT_EQ(SERVICE_WORKER_OK, UpdateToActiveState(found_registration)); |
| 368 found_registration = NULL; | 367 found_registration = NULL; |
| 369 | 368 |
| 370 // Trying to update a unstored registration to active should fail. | 369 // Trying to update a unstored registration to active should fail. |
| 371 scoped_refptr<ServiceWorkerRegistration> unstored_registration = | 370 scoped_refptr<ServiceWorkerRegistration> unstored_registration = |
| 372 new ServiceWorkerRegistration( | 371 new ServiceWorkerRegistration( |
| 373 kScope, kScript, kRegistrationId + 1, context_ptr_); | 372 kScope, kScript, kRegistrationId + 1, context_ptr_); |
| 374 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, | 373 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, |
| 375 UpdateToActiveState(unstored_registration)); | 374 UpdateToActiveState(unstored_registration)); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 413 scoped_refptr<ServiceWorkerRegistration> found_registration; | 412 scoped_refptr<ServiceWorkerRegistration> found_registration; |
| 414 | 413 |
| 415 // Create an unstored registration. | 414 // Create an unstored registration. |
| 416 scoped_refptr<ServiceWorkerRegistration> live_registration = | 415 scoped_refptr<ServiceWorkerRegistration> live_registration = |
| 417 new ServiceWorkerRegistration( | 416 new ServiceWorkerRegistration( |
| 418 kScope, kScript, kRegistrationId, context_ptr_); | 417 kScope, kScript, kRegistrationId, context_ptr_); |
| 419 scoped_refptr<ServiceWorkerVersion> live_version = | 418 scoped_refptr<ServiceWorkerVersion> live_version = |
| 420 new ServiceWorkerVersion( | 419 new ServiceWorkerVersion( |
| 421 live_registration, kVersionId, context_ptr_); | 420 live_registration, kVersionId, context_ptr_); |
| 422 live_version->SetStatus(ServiceWorkerVersion::INSTALLING); | 421 live_version->SetStatus(ServiceWorkerVersion::INSTALLING); |
| 423 live_registration->set_waiting_version(live_version); | 422 live_registration->SetWaitingVersion(live_version); |
| 424 | 423 |
| 425 // Should not be findable, including by GetAllRegistrations. | 424 // Should not be findable, including by GetAllRegistrations. |
| 426 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, | 425 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, |
| 427 FindRegistrationForId( | 426 FindRegistrationForId( |
| 428 kRegistrationId, kScope.GetOrigin(), &found_registration)); | 427 kRegistrationId, kScope.GetOrigin(), &found_registration)); |
| 429 EXPECT_FALSE(found_registration); | 428 EXPECT_FALSE(found_registration); |
| 430 | 429 |
| 431 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, | 430 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, |
| 432 FindRegistrationForDocument(kDocumentUrl, &found_registration)); | 431 FindRegistrationForDocument(kDocumentUrl, &found_registration)); |
| 433 EXPECT_FALSE(found_registration); | 432 EXPECT_FALSE(found_registration); |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 574 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, | 573 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, |
| 575 storage()->database_->GetPurgeableResourceIds(&verify_ids)); | 574 storage()->database_->GetPurgeableResourceIds(&verify_ids)); |
| 576 EXPECT_TRUE(verify_ids.empty()); | 575 EXPECT_TRUE(verify_ids.empty()); |
| 577 | 576 |
| 578 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id1_, false)); | 577 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id1_, false)); |
| 579 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id2_, false)); | 578 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id2_, false)); |
| 580 } | 579 } |
| 581 | 580 |
| 582 TEST_F(ServiceWorkerResourceStorageTest, DeleteRegistration_ActiveVersion) { | 581 TEST_F(ServiceWorkerResourceStorageTest, DeleteRegistration_ActiveVersion) { |
| 583 // Promote the worker to active and add a controllee. | 582 // Promote the worker to active and add a controllee. |
| 584 registration_->set_active_version(registration_->waiting_version()); | 583 registration_->SetActiveVersion(registration_->waiting_version()); |
| 585 registration_->set_waiting_version(NULL); | |
| 586 storage()->UpdateToActiveState( | 584 storage()->UpdateToActiveState( |
| 587 registration_, base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 585 registration_, base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 588 scoped_ptr<ServiceWorkerProviderHost> host( | 586 scoped_ptr<ServiceWorkerProviderHost> host( |
| 589 new ServiceWorkerProviderHost(33 /* dummy render process id */, | 587 new ServiceWorkerProviderHost(33 /* dummy render process id */, |
| 590 1 /* dummy provider_id */, | 588 1 /* dummy provider_id */, |
| 591 context_->AsWeakPtr(), | 589 context_->AsWeakPtr(), |
| 592 NULL)); | 590 NULL)); |
| 593 registration_->active_version()->AddControllee(host.get()); | 591 registration_->active_version()->AddControllee(host.get()); |
| 594 | 592 |
| 595 bool was_called = false; | 593 bool was_called = false; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 625 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, | 623 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, |
| 626 storage()->database_->GetPurgeableResourceIds(&verify_ids)); | 624 storage()->database_->GetPurgeableResourceIds(&verify_ids)); |
| 627 EXPECT_TRUE(verify_ids.empty()); | 625 EXPECT_TRUE(verify_ids.empty()); |
| 628 | 626 |
| 629 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id1_, false)); | 627 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id1_, false)); |
| 630 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id2_, false)); | 628 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id2_, false)); |
| 631 } | 629 } |
| 632 | 630 |
| 633 TEST_F(ServiceWorkerResourceStorageTest, UpdateRegistration) { | 631 TEST_F(ServiceWorkerResourceStorageTest, UpdateRegistration) { |
| 634 // Promote the worker to active worker and add a controllee. | 632 // Promote the worker to active worker and add a controllee. |
| 635 registration_->set_active_version(registration_->waiting_version()); | 633 registration_->SetActiveVersion(registration_->waiting_version()); |
| 636 registration_->set_waiting_version(NULL); | |
| 637 storage()->UpdateToActiveState( | 634 storage()->UpdateToActiveState( |
| 638 registration_, base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 635 registration_, base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 639 scoped_ptr<ServiceWorkerProviderHost> host( | 636 scoped_ptr<ServiceWorkerProviderHost> host( |
| 640 new ServiceWorkerProviderHost(33 /* dummy render process id */, | 637 new ServiceWorkerProviderHost(33 /* dummy render process id */, |
| 641 1 /* dummy provider_id */, | 638 1 /* dummy provider_id */, |
| 642 context_->AsWeakPtr(), | 639 context_->AsWeakPtr(), |
| 643 NULL)); | 640 NULL)); |
| 644 registration_->active_version()->AddControllee(host.get()); | 641 registration_->active_version()->AddControllee(host.get()); |
| 645 | 642 |
| 646 bool was_called = false; | 643 bool was_called = false; |
| 647 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED; | 644 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED; |
| 648 std::set<int64> verify_ids; | 645 std::set<int64> verify_ids; |
| 649 | 646 |
| 650 // Make an updated registration. | 647 // Make an updated registration. |
| 651 scoped_refptr<ServiceWorkerVersion> live_version = new ServiceWorkerVersion( | 648 scoped_refptr<ServiceWorkerVersion> live_version = new ServiceWorkerVersion( |
| 652 registration_, storage()->NewVersionId(), context_ptr_); | 649 registration_, storage()->NewVersionId(), context_ptr_); |
| 653 live_version->SetStatus(ServiceWorkerVersion::NEW); | 650 live_version->SetStatus(ServiceWorkerVersion::NEW); |
| 654 registration_->set_waiting_version(live_version); | 651 registration_->SetWaitingVersion(live_version); |
| 655 | 652 |
| 656 // Writing the registration should move the old version's resources to the | 653 // Writing the registration should move the old version's resources to the |
| 657 // purgeable list but keep them available. | 654 // purgeable list but keep them available. |
| 658 storage()->StoreRegistration( | 655 storage()->StoreRegistration( |
| 659 registration_, | 656 registration_, |
| 660 registration_->waiting_version(), | 657 registration_->waiting_version(), |
| 661 base::Bind(&VerifyPurgeableListStatusCallback, | 658 base::Bind(&VerifyPurgeableListStatusCallback, |
| 662 base::Unretained(storage()->database_.get()), | 659 base::Unretained(storage()->database_.get()), |
| 663 &verify_ids, | 660 &verify_ids, |
| 664 &was_called, | 661 &was_called, |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 697 const GURL kScript1("http://www.example.com/script1.js"); | 694 const GURL kScript1("http://www.example.com/script1.js"); |
| 698 const int64 kRegistrationId1 = 1; | 695 const int64 kRegistrationId1 = 1; |
| 699 const int64 kVersionId1 = 1; | 696 const int64 kVersionId1 = 1; |
| 700 scoped_refptr<ServiceWorkerRegistration> live_registration1 = | 697 scoped_refptr<ServiceWorkerRegistration> live_registration1 = |
| 701 new ServiceWorkerRegistration( | 698 new ServiceWorkerRegistration( |
| 702 kScope1, kScript1, kRegistrationId1, context_ptr_); | 699 kScope1, kScript1, kRegistrationId1, context_ptr_); |
| 703 scoped_refptr<ServiceWorkerVersion> live_version1 = | 700 scoped_refptr<ServiceWorkerVersion> live_version1 = |
| 704 new ServiceWorkerVersion( | 701 new ServiceWorkerVersion( |
| 705 live_registration1, kVersionId1, context_ptr_); | 702 live_registration1, kVersionId1, context_ptr_); |
| 706 live_version1->SetStatus(ServiceWorkerVersion::INSTALLED); | 703 live_version1->SetStatus(ServiceWorkerVersion::INSTALLED); |
| 707 live_registration1->set_waiting_version(live_version1); | 704 live_registration1->SetWaitingVersion(live_version1); |
| 708 | 705 |
| 709 // Registration for "/scope/foo*". | 706 // Registration for "/scope/foo*". |
| 710 const GURL kScope2("http://www.example.com/scope/foo*"); | 707 const GURL kScope2("http://www.example.com/scope/foo*"); |
| 711 const GURL kScript2("http://www.example.com/script2.js"); | 708 const GURL kScript2("http://www.example.com/script2.js"); |
| 712 const int64 kRegistrationId2 = 2; | 709 const int64 kRegistrationId2 = 2; |
| 713 const int64 kVersionId2 = 2; | 710 const int64 kVersionId2 = 2; |
| 714 scoped_refptr<ServiceWorkerRegistration> live_registration2 = | 711 scoped_refptr<ServiceWorkerRegistration> live_registration2 = |
| 715 new ServiceWorkerRegistration( | 712 new ServiceWorkerRegistration( |
| 716 kScope2, kScript2, kRegistrationId2, context_ptr_); | 713 kScope2, kScript2, kRegistrationId2, context_ptr_); |
| 717 scoped_refptr<ServiceWorkerVersion> live_version2 = | 714 scoped_refptr<ServiceWorkerVersion> live_version2 = |
| 718 new ServiceWorkerVersion( | 715 new ServiceWorkerVersion( |
| 719 live_registration2, kVersionId2, context_ptr_); | 716 live_registration2, kVersionId2, context_ptr_); |
| 720 live_version2->SetStatus(ServiceWorkerVersion::INSTALLED); | 717 live_version2->SetStatus(ServiceWorkerVersion::INSTALLED); |
| 721 live_registration2->set_waiting_version(live_version2); | 718 live_registration2->SetWaitingVersion(live_version2); |
| 722 | 719 |
| 723 // Registration for "/scope/foo". | 720 // Registration for "/scope/foo". |
| 724 const GURL kScope3("http://www.example.com/scope/foo"); | 721 const GURL kScope3("http://www.example.com/scope/foo"); |
| 725 const GURL kScript3("http://www.example.com/script3.js"); | 722 const GURL kScript3("http://www.example.com/script3.js"); |
| 726 const int64 kRegistrationId3 = 3; | 723 const int64 kRegistrationId3 = 3; |
| 727 const int64 kVersionId3 = 3; | 724 const int64 kVersionId3 = 3; |
| 728 scoped_refptr<ServiceWorkerRegistration> live_registration3 = | 725 scoped_refptr<ServiceWorkerRegistration> live_registration3 = |
| 729 new ServiceWorkerRegistration( | 726 new ServiceWorkerRegistration( |
| 730 kScope3, kScript3, kRegistrationId3, context_ptr_); | 727 kScope3, kScript3, kRegistrationId3, context_ptr_); |
| 731 scoped_refptr<ServiceWorkerVersion> live_version3 = | 728 scoped_refptr<ServiceWorkerVersion> live_version3 = |
| 732 new ServiceWorkerVersion( | 729 new ServiceWorkerVersion( |
| 733 live_registration3, kVersionId3, context_ptr_); | 730 live_registration3, kVersionId3, context_ptr_); |
| 734 live_version3->SetStatus(ServiceWorkerVersion::INSTALLED); | 731 live_version3->SetStatus(ServiceWorkerVersion::INSTALLED); |
| 735 live_registration3->set_waiting_version(live_version3); | 732 live_registration3->SetWaitingVersion(live_version3); |
| 736 | 733 |
| 737 // Notify storage of they being installed. | 734 // Notify storage of they being installed. |
| 738 storage()->NotifyInstallingRegistration(live_registration1); | 735 storage()->NotifyInstallingRegistration(live_registration1); |
| 739 storage()->NotifyInstallingRegistration(live_registration2); | 736 storage()->NotifyInstallingRegistration(live_registration2); |
| 740 storage()->NotifyInstallingRegistration(live_registration3); | 737 storage()->NotifyInstallingRegistration(live_registration3); |
| 741 | 738 |
| 742 // Find a registration among installing ones. | 739 // Find a registration among installing ones. |
| 743 EXPECT_EQ(SERVICE_WORKER_OK, | 740 EXPECT_EQ(SERVICE_WORKER_OK, |
| 744 FindRegistrationForDocument(kDocumentUrl, &found_registration)); | 741 FindRegistrationForDocument(kDocumentUrl, &found_registration)); |
| 745 EXPECT_EQ(live_registration2, found_registration); | 742 EXPECT_EQ(live_registration2, found_registration); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 761 storage()->NotifyDoneInstallingRegistration( | 758 storage()->NotifyDoneInstallingRegistration( |
| 762 live_registration3, NULL, SERVICE_WORKER_OK); | 759 live_registration3, NULL, SERVICE_WORKER_OK); |
| 763 | 760 |
| 764 // Find a registration among installed ones. | 761 // Find a registration among installed ones. |
| 765 EXPECT_EQ(SERVICE_WORKER_OK, | 762 EXPECT_EQ(SERVICE_WORKER_OK, |
| 766 FindRegistrationForDocument(kDocumentUrl, &found_registration)); | 763 FindRegistrationForDocument(kDocumentUrl, &found_registration)); |
| 767 EXPECT_EQ(live_registration2, found_registration); | 764 EXPECT_EQ(live_registration2, found_registration); |
| 768 } | 765 } |
| 769 | 766 |
| 770 } // namespace content | 767 } // namespace content |
| OLD | NEW |