OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/callback.h" | 6 #include "base/callback.h" |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "content/browser/fileapi/chrome_blob_storage_context.h" | 10 #include "content/browser/fileapi/chrome_blob_storage_context.h" |
(...skipping 554 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
565 | 565 |
566 AssociateRendererProcessToPattern(pattern); | 566 AssociateRendererProcessToPattern(pattern); |
567 } | 567 } |
568 | 568 |
569 void TimeoutWorkerOnIOThread() { | 569 void TimeoutWorkerOnIOThread() { |
570 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 570 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
571 version_->PingWorker(); | 571 version_->PingWorker(); |
572 version_->OnPingTimeout(); | 572 version_->OnPingTimeout(); |
573 } | 573 } |
574 | 574 |
| 575 void AddControlleeOnIOThread() { |
| 576 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 577 scoped_ptr<ServiceWorkerProviderHost> host(new ServiceWorkerProviderHost( |
| 578 33 /* dummy render process id */, |
| 579 MSG_ROUTING_NONE /* render_frame_id */, 1 /* dummy provider_id */, |
| 580 SERVICE_WORKER_PROVIDER_FOR_WINDOW, wrapper()->context()->AsWeakPtr(), |
| 581 NULL)); |
| 582 host->SetDocumentUrl( |
| 583 embedded_test_server()->GetURL("/service_worker/host")); |
| 584 host->AssociateRegistration(registration_.get(), |
| 585 false /* notify_controllerchange */); |
| 586 wrapper()->context()->AddProviderHost(host.Pass()); |
| 587 } |
| 588 |
| 589 void AddWaitingWorkerOnIOThread(const std::string& worker_url) { |
| 590 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 591 scoped_refptr<ServiceWorkerVersion> waiting_version( |
| 592 new ServiceWorkerVersion( |
| 593 registration_.get(), embedded_test_server()->GetURL(worker_url), |
| 594 wrapper()->context()->storage()->NewVersionId(), |
| 595 wrapper()->context()->AsWeakPtr())); |
| 596 waiting_version->SetStatus(ServiceWorkerVersion::INSTALLED); |
| 597 registration_->SetWaitingVersion(waiting_version.get()); |
| 598 registration_->ActivateWaitingVersionWhenReady(); |
| 599 } |
| 600 |
575 void StartWorker(ServiceWorkerStatusCode expected_status) { | 601 void StartWorker(ServiceWorkerStatusCode expected_status) { |
576 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 602 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
577 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; | 603 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; |
578 base::RunLoop start_run_loop; | 604 base::RunLoop start_run_loop; |
579 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 605 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
580 base::Bind(&self::StartOnIOThread, this, | 606 base::Bind(&self::StartOnIOThread, this, |
581 start_run_loop.QuitClosure(), | 607 start_run_loop.QuitClosure(), |
582 &status)); | 608 &status)); |
583 start_run_loop.Run(); | 609 start_run_loop.Run(); |
584 ASSERT_EQ(expected_status, status); | 610 ASSERT_EQ(expected_status, status); |
(...skipping 22 matching lines...) Expand all Loading... |
607 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 633 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
608 version_->SetStatus(ServiceWorkerVersion::INSTALLING); | 634 version_->SetStatus(ServiceWorkerVersion::INSTALLING); |
609 version_->DispatchInstallEvent( | 635 version_->DispatchInstallEvent( |
610 CreateReceiver(BrowserThread::UI, done, result)); | 636 CreateReceiver(BrowserThread::UI, done, result)); |
611 } | 637 } |
612 | 638 |
613 void ActivateOnIOThread(const base::Closure& done, | 639 void ActivateOnIOThread(const base::Closure& done, |
614 ServiceWorkerStatusCode* result) { | 640 ServiceWorkerStatusCode* result) { |
615 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 641 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
616 version_->SetStatus(ServiceWorkerVersion::ACTIVATING); | 642 version_->SetStatus(ServiceWorkerVersion::ACTIVATING); |
| 643 registration_->SetActiveVersion(version_.get()); |
617 version_->DispatchActivateEvent( | 644 version_->DispatchActivateEvent( |
618 CreateReceiver(BrowserThread::UI, done, result)); | 645 CreateReceiver(BrowserThread::UI, done, result)); |
619 } | 646 } |
620 | 647 |
621 void FetchOnIOThread(const base::Closure& done, | 648 void FetchOnIOThread(const base::Closure& done, |
622 bool* prepare_result, | 649 bool* prepare_result, |
623 FetchResult* result) { | 650 FetchResult* result) { |
624 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 651 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
625 ServiceWorkerFetchRequest request( | 652 ServiceWorkerFetchRequest request( |
626 embedded_test_server()->GetURL("/service_worker/empty.html"), | 653 embedded_test_server()->GetURL("/service_worker/empty.html"), |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
723 } | 750 } |
724 | 751 |
725 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, StartNotFound) { | 752 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, StartNotFound) { |
726 RunOnIOThread(base::Bind(&self::SetUpRegistrationOnIOThread, this, | 753 RunOnIOThread(base::Bind(&self::SetUpRegistrationOnIOThread, this, |
727 "/service_worker/nonexistent.js")); | 754 "/service_worker/nonexistent.js")); |
728 | 755 |
729 // Start a worker for nonexistent URL. | 756 // Start a worker for nonexistent URL. |
730 StartWorker(SERVICE_WORKER_ERROR_NETWORK); | 757 StartWorker(SERVICE_WORKER_ERROR_NETWORK); |
731 } | 758 } |
732 | 759 |
| 760 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, ReadResourceFailure) { |
| 761 RunOnIOThread(base::Bind(&self::SetUpRegistrationOnIOThread, this, |
| 762 "/service_worker/worker.js")); |
| 763 version_->SetStatus(ServiceWorkerVersion::ACTIVATED); |
| 764 |
| 765 // Add a non-existent resource to the version. |
| 766 std::vector<ServiceWorkerDatabase::ResourceRecord> records; |
| 767 records.push_back( |
| 768 ServiceWorkerDatabase::ResourceRecord(30, version_->script_url(), 100)); |
| 769 version_->script_cache_map()->SetResources(records); |
| 770 |
| 771 // We'll fail to read from disk and the worker should be doomed. |
| 772 StartWorker(SERVICE_WORKER_ERROR_DISK_CACHE); |
| 773 EXPECT_EQ(ServiceWorkerVersion::REDUNDANT, version_->status()); |
| 774 |
| 775 // The registration should be deleted since the version was the stored one. |
| 776 EXPECT_TRUE(registration_->is_deleted()); |
| 777 } |
| 778 |
| 779 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, |
| 780 ReadResourceFailure_WaitingWorker) { |
| 781 RunOnIOThread(base::Bind(&self::SetUpRegistrationOnIOThread, this, |
| 782 "/service_worker/worker.js")); |
| 783 base::RunLoop acrivate_run_loop; |
| 784 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; |
| 785 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 786 base::Bind(&self::ActivateOnIOThread, this, |
| 787 acrivate_run_loop.QuitClosure(), &status)); |
| 788 acrivate_run_loop.Run(); |
| 789 ASSERT_TRUE(registration_->active_version()); |
| 790 |
| 791 // Give the version a controllee. |
| 792 RunOnIOThread(base::Bind(&self::AddControlleeOnIOThread, this)); |
| 793 |
| 794 // Add a non-existent resource to the version. |
| 795 std::vector<ServiceWorkerDatabase::ResourceRecord> records; |
| 796 records.push_back( |
| 797 ServiceWorkerDatabase::ResourceRecord(30, version_->script_url(), 100)); |
| 798 version_->script_cache_map()->SetResources(records); |
| 799 |
| 800 // Make a waiting version. |
| 801 RunOnIOThread(base::Bind(&self::AddWaitingWorkerOnIOThread, this, |
| 802 "/service_worker/worker.js")); |
| 803 |
| 804 // Start the worker. We'll fail to read from disk and the worker should be |
| 805 // doomed. |
| 806 StopWorker(SERVICE_WORKER_OK); // in case it's already running |
| 807 StartWorker(SERVICE_WORKER_ERROR_DISK_CACHE); |
| 808 EXPECT_EQ(ServiceWorkerVersion::REDUNDANT, version_->status()); |
| 809 |
| 810 // The registration is still alive since the waiting version was the stored |
| 811 // one. |
| 812 EXPECT_FALSE(registration_->is_deleted()); |
| 813 } |
| 814 |
733 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, Install) { | 815 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, Install) { |
734 InstallTestHelper("/service_worker/worker.js", SERVICE_WORKER_OK); | 816 InstallTestHelper("/service_worker/worker.js", SERVICE_WORKER_OK); |
735 } | 817 } |
736 | 818 |
737 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, | 819 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, |
738 InstallWithWaitUntil_Fulfilled) { | 820 InstallWithWaitUntil_Fulfilled) { |
739 InstallTestHelper("/service_worker/worker_install_fulfilled.js", | 821 InstallTestHelper("/service_worker/worker_install_fulfilled.js", |
740 SERVICE_WORKER_OK); | 822 SERVICE_WORKER_OK); |
741 } | 823 } |
742 | 824 |
(...skipping 543 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1286 ASSERT_EQ(SERVICE_WORKER_OK, status); | 1368 ASSERT_EQ(SERVICE_WORKER_OK, status); |
1287 // Stop the worker. | 1369 // Stop the worker. |
1288 StopWorker(SERVICE_WORKER_OK); | 1370 StopWorker(SERVICE_WORKER_OK); |
1289 // Restart the worker. | 1371 // Restart the worker. |
1290 StartWorker(SERVICE_WORKER_OK); | 1372 StartWorker(SERVICE_WORKER_OK); |
1291 // Stop the worker. | 1373 // Stop the worker. |
1292 StopWorker(SERVICE_WORKER_OK); | 1374 StopWorker(SERVICE_WORKER_OK); |
1293 } | 1375 } |
1294 | 1376 |
1295 } // namespace content | 1377 } // namespace content |
OLD | NEW |