| 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 <stdint.h> | 5 #include <stdint.h> |
| 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/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 11 #include "content/browser/browser_thread_impl.h" | 11 #include "content/browser/browser_thread_impl.h" |
| 12 #include "content/browser/service_worker/embedded_worker_registry.h" | 12 #include "content/browser/service_worker/embedded_worker_registry.h" |
| 13 #include "content/browser/service_worker/embedded_worker_test_helper.h" | 13 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
| 14 #include "content/browser/service_worker/service_worker_context_core.h" | 14 #include "content/browser/service_worker/service_worker_context_core.h" |
| 15 #include "content/browser/service_worker/service_worker_disk_cache.h" | 15 #include "content/browser/service_worker/service_worker_disk_cache.h" |
| 16 #include "content/browser/service_worker/service_worker_job_coordinator.h" | 16 #include "content/browser/service_worker/service_worker_job_coordinator.h" |
| 17 #include "content/browser/service_worker/service_worker_registration.h" | 17 #include "content/browser/service_worker/service_worker_registration.h" |
| 18 #include "content/browser/service_worker/service_worker_registration_status.h" | 18 #include "content/browser/service_worker/service_worker_registration_status.h" |
| 19 #include "content/browser/service_worker/service_worker_test_utils.h" | 19 #include "content/browser/service_worker/service_worker_test_utils.h" |
| 20 #include "content/common/service_worker/embedded_worker_messages.h" | |
| 21 #include "content/common/service_worker/service_worker_messages.h" | 20 #include "content/common/service_worker/service_worker_messages.h" |
| 22 #include "content/public/test/test_browser_thread_bundle.h" | 21 #include "content/public/test/test_browser_thread_bundle.h" |
| 23 #include "ipc/ipc_test_sink.h" | 22 #include "ipc/ipc_test_sink.h" |
| 24 #include "net/base/io_buffer.h" | 23 #include "net/base/io_buffer.h" |
| 25 #include "net/base/net_errors.h" | 24 #include "net/base/net_errors.h" |
| 26 #include "net/base/test_completion_callback.h" | 25 #include "net/base/test_completion_callback.h" |
| 27 #include "net/http/http_response_headers.h" | 26 #include "net/http/http_response_headers.h" |
| 28 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 29 | 28 |
| 30 using net::IOBuffer; | 29 using net::IOBuffer; |
| (...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 355 ASSERT_EQ(new_registration, old_registration); | 354 ASSERT_EQ(new_registration, old_registration); |
| 356 } | 355 } |
| 357 | 356 |
| 358 class FailToStartWorkerTestHelper : public EmbeddedWorkerTestHelper { | 357 class FailToStartWorkerTestHelper : public EmbeddedWorkerTestHelper { |
| 359 public: | 358 public: |
| 360 FailToStartWorkerTestHelper() : EmbeddedWorkerTestHelper(base::FilePath()) {} | 359 FailToStartWorkerTestHelper() : EmbeddedWorkerTestHelper(base::FilePath()) {} |
| 361 | 360 |
| 362 void OnStartWorker(int embedded_worker_id, | 361 void OnStartWorker(int embedded_worker_id, |
| 363 int64_t service_worker_version_id, | 362 int64_t service_worker_version_id, |
| 364 const GURL& scope, | 363 const GURL& scope, |
| 365 const GURL& script_url, | 364 const GURL& script_url) override { |
| 366 bool pause_after_download) override { | |
| 367 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id); | 365 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id); |
| 368 registry()->OnWorkerStopped(worker->process_id(), embedded_worker_id); | 366 registry()->OnWorkerStopped(worker->process_id(), embedded_worker_id); |
| 369 } | 367 } |
| 370 }; | 368 }; |
| 371 | 369 |
| 372 TEST_F(ServiceWorkerJobTest, Register_FailToStartWorker) { | 370 TEST_F(ServiceWorkerJobTest, Register_FailToStartWorker) { |
| 373 helper_.reset(new FailToStartWorkerTestHelper); | 371 helper_.reset(new FailToStartWorkerTestHelper); |
| 374 | 372 |
| 375 scoped_refptr<ServiceWorkerRegistration> registration = | 373 scoped_refptr<ServiceWorkerRegistration> registration = |
| 376 RunRegisterJob(GURL("http://www.example.com/"), | 374 RunRegisterJob(GURL("http://www.example.com/"), |
| (...skipping 442 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 819 EXPECT_FALSE(registration->installing_version()); | 817 EXPECT_FALSE(registration->installing_version()); |
| 820 EXPECT_FALSE(registration->waiting_version()); | 818 EXPECT_FALSE(registration->waiting_version()); |
| 821 registration_ = registration; | 819 registration_ = registration; |
| 822 return registration; | 820 return registration; |
| 823 } | 821 } |
| 824 | 822 |
| 825 // EmbeddedWorkerTestHelper overrides | 823 // EmbeddedWorkerTestHelper overrides |
| 826 void OnStartWorker(int embedded_worker_id, | 824 void OnStartWorker(int embedded_worker_id, |
| 827 int64_t version_id, | 825 int64_t version_id, |
| 828 const GURL& scope, | 826 const GURL& scope, |
| 829 const GURL& script, | 827 const GURL& script) override { |
| 830 bool pause_after_download) override { | |
| 831 const std::string kMockScriptBody = "mock_script"; | 828 const std::string kMockScriptBody = "mock_script"; |
| 832 const uint64_t kMockScriptSize = 19284; | 829 const uint64_t kMockScriptSize = 19284; |
| 833 ServiceWorkerVersion* version = context()->GetLiveVersion(version_id); | 830 ServiceWorkerVersion* version = context()->GetLiveVersion(version_id); |
| 834 ServiceWorkerRegistration* registration = | 831 ServiceWorkerRegistration* registration = |
| 835 context()->GetLiveRegistration(version->registration_id()); | 832 context()->GetLiveRegistration(version->registration_id()); |
| 836 bool is_update = registration->active_version() && | 833 bool is_update = registration->active_version() && |
| 837 version != registration->active_version(); | 834 version != registration->active_version(); |
| 838 | 835 |
| 839 ASSERT_TRUE(version); | 836 ASSERT_TRUE(version); |
| 840 version->AddListener(this); | 837 version->AddListener(this); |
| 841 | 838 |
| 842 if (force_bypass_cache_for_scripts()) | 839 if (force_bypass_cache_for_scripts()) |
| 843 version->set_force_bypass_cache_for_scripts(true); | 840 version->set_force_bypass_cache_for_scripts(true); |
| 844 | |
| 845 if (!is_update) { | 841 if (!is_update) { |
| 846 // Spoof caching the script for the initial version. | 842 // Spoof caching the script for the initial version. |
| 847 int64_t resource_id = storage()->NewResourceId(); | 843 int64_t resource_id = storage()->NewResourceId(); |
| 848 version->script_cache_map()->NotifyStartedCaching(script, resource_id); | 844 version->script_cache_map()->NotifyStartedCaching(script, resource_id); |
| 849 WriteStringResponse(storage(), resource_id, kMockScriptBody); | 845 WriteStringResponse(storage(), resource_id, kMockScriptBody); |
| 850 version->script_cache_map()->NotifyFinishedCaching( | 846 version->script_cache_map()->NotifyFinishedCaching( |
| 851 script, kMockScriptSize, net::URLRequestStatus(), std::string()); | 847 script, kMockScriptSize, net::URLRequestStatus(), std::string()); |
| 852 } else { | 848 } else { |
| 853 if (script.GetOrigin() == kNoChangeOrigin) { | 849 if (script.GetOrigin() == kNoChangeOrigin) { |
| 854 version->SetStartWorkerStatusCode(SERVICE_WORKER_ERROR_EXISTS); | 850 version->SetStartWorkerStatusCode(SERVICE_WORKER_ERROR_EXISTS); |
| 855 EmbeddedWorkerTestHelper::OnStopWorker(embedded_worker_id); | 851 EmbeddedWorkerTestHelper::OnStopWorker(embedded_worker_id); |
| 856 return; | 852 return; |
| 857 } | 853 } |
| 858 | 854 |
| 859 // Spoof caching the script for the new version. | 855 // Spoof caching the script for the new version. |
| 860 int64_t resource_id = storage()->NewResourceId(); | 856 int64_t resource_id = storage()->NewResourceId(); |
| 861 version->script_cache_map()->NotifyStartedCaching(script, resource_id); | 857 version->script_cache_map()->NotifyStartedCaching(script, resource_id); |
| 862 WriteStringResponse(storage(), resource_id, "mock_different_script"); | 858 WriteStringResponse(storage(), resource_id, "mock_different_script"); |
| 863 version->script_cache_map()->NotifyFinishedCaching( | 859 version->script_cache_map()->NotifyFinishedCaching( |
| 864 script, kMockScriptSize, net::URLRequestStatus(), std::string()); | 860 script, kMockScriptSize, net::URLRequestStatus(), std::string()); |
| 865 } | 861 } |
| 866 | |
| 867 EmbeddedWorkerTestHelper::OnStartWorker( | |
| 868 embedded_worker_id, version_id, scope, script, pause_after_download); | |
| 869 } | |
| 870 | |
| 871 void OnResumeAfterDownload(int embedded_worker_id) override { | |
| 872 if (!force_start_worker_failure_) { | 862 if (!force_start_worker_failure_) { |
| 873 EmbeddedWorkerTestHelper::OnResumeAfterDownload(embedded_worker_id); | 863 EmbeddedWorkerTestHelper::OnStartWorker(embedded_worker_id, version_id, |
| 864 scope, script); |
| 874 } else { | 865 } else { |
| 866 (embedded_worker_id_service_worker_version_id_map())[embedded_worker_id] = |
| 867 version_id; |
| 868 SimulateWorkerReadyForInspection(embedded_worker_id); |
| 869 SimulateWorkerScriptCached(embedded_worker_id); |
| 870 SimulateWorkerScriptLoaded(embedded_worker_id); |
| 875 SimulateWorkerThreadStarted(GetNextThreadId(), embedded_worker_id); | 871 SimulateWorkerThreadStarted(GetNextThreadId(), embedded_worker_id); |
| 876 SimulateWorkerScriptEvaluated(embedded_worker_id, false); | 872 SimulateWorkerScriptEvaluated(embedded_worker_id, false); |
| 877 } | 873 } |
| 878 } | 874 } |
| 879 | 875 |
| 880 // ServiceWorkerRegistration::Listener overrides | 876 // ServiceWorkerRegistration::Listener overrides |
| 881 void OnVersionAttributesChanged( | 877 void OnVersionAttributesChanged( |
| 882 ServiceWorkerRegistration* registration, | 878 ServiceWorkerRegistration* registration, |
| 883 ChangedVersionAttributesMask changed_mask, | 879 ChangedVersionAttributesMask changed_mask, |
| 884 const ServiceWorkerRegistrationInfo& info) override { | 880 const ServiceWorkerRegistrationInfo& info) override { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 917 // Helper class for update tests that evicts the active version when the update | 913 // Helper class for update tests that evicts the active version when the update |
| 918 // worker is about to be started. | 914 // worker is about to be started. |
| 919 class EvictIncumbentVersionHelper : public UpdateJobTestHelper { | 915 class EvictIncumbentVersionHelper : public UpdateJobTestHelper { |
| 920 public: | 916 public: |
| 921 EvictIncumbentVersionHelper() {} | 917 EvictIncumbentVersionHelper() {} |
| 922 ~EvictIncumbentVersionHelper() override {} | 918 ~EvictIncumbentVersionHelper() override {} |
| 923 | 919 |
| 924 void OnStartWorker(int embedded_worker_id, | 920 void OnStartWorker(int embedded_worker_id, |
| 925 int64_t version_id, | 921 int64_t version_id, |
| 926 const GURL& scope, | 922 const GURL& scope, |
| 927 const GURL& script, | 923 const GURL& script) override { |
| 928 bool pause_after_download) override { | |
| 929 ServiceWorkerVersion* version = context()->GetLiveVersion(version_id); | 924 ServiceWorkerVersion* version = context()->GetLiveVersion(version_id); |
| 930 ServiceWorkerRegistration* registration = | 925 ServiceWorkerRegistration* registration = |
| 931 context()->GetLiveRegistration(version->registration_id()); | 926 context()->GetLiveRegistration(version->registration_id()); |
| 932 bool is_update = registration->active_version() && | 927 bool is_update = registration->active_version() && |
| 933 version != registration->active_version(); | 928 version != registration->active_version(); |
| 934 if (is_update) { | 929 if (is_update) { |
| 935 // Evict the incumbent worker. | 930 // Evict the incumbent worker. |
| 936 ASSERT_FALSE(registration->waiting_version()); | 931 ASSERT_FALSE(registration->waiting_version()); |
| 937 registration->DeleteVersion( | 932 registration->DeleteVersion( |
| 938 make_scoped_refptr(registration->active_version())); | 933 make_scoped_refptr(registration->active_version())); |
| 939 } | 934 } |
| 940 UpdateJobTestHelper::OnStartWorker(embedded_worker_id, version_id, scope, | 935 UpdateJobTestHelper::OnStartWorker(embedded_worker_id, version_id, scope, |
| 941 script, pause_after_download); | 936 script); |
| 942 } | 937 } |
| 943 | 938 |
| 944 void OnRegistrationFailed(ServiceWorkerRegistration* registration) override { | 939 void OnRegistrationFailed(ServiceWorkerRegistration* registration) override { |
| 945 registration_failed_ = true; | 940 registration_failed_ = true; |
| 946 } | 941 } |
| 947 | 942 |
| 948 bool registration_failed_ = false; | 943 bool registration_failed_ = false; |
| 949 }; | 944 }; |
| 950 | 945 |
| 951 } // namespace | 946 } // namespace |
| (...skipping 579 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1531 // Verify the registration remains. | 1526 // Verify the registration remains. |
| 1532 EXPECT_FALSE(registration->is_uninstalling()); | 1527 EXPECT_FALSE(registration->is_uninstalling()); |
| 1533 EXPECT_FALSE(registration->is_uninstalled()); | 1528 EXPECT_FALSE(registration->is_uninstalled()); |
| 1534 | 1529 |
| 1535 EXPECT_EQ(ServiceWorkerVersion::STOPPED, old_version->running_status()); | 1530 EXPECT_EQ(ServiceWorkerVersion::STOPPED, old_version->running_status()); |
| 1536 EXPECT_EQ(ServiceWorkerVersion::REDUNDANT, old_version->status()); | 1531 EXPECT_EQ(ServiceWorkerVersion::REDUNDANT, old_version->status()); |
| 1537 | 1532 |
| 1538 FindRegistrationForPattern(pattern, SERVICE_WORKER_OK); | 1533 FindRegistrationForPattern(pattern, SERVICE_WORKER_OK); |
| 1539 } | 1534 } |
| 1540 | 1535 |
| 1541 TEST_F(ServiceWorkerJobTest, Update_PauseAfterDownload) { | |
| 1542 UpdateJobTestHelper* update_helper = new UpdateJobTestHelper; | |
| 1543 helper_.reset(update_helper); | |
| 1544 IPC::TestSink* sink = update_helper->ipc_sink(); | |
| 1545 | |
| 1546 // The initial version should not pause after download. | |
| 1547 scoped_refptr<ServiceWorkerRegistration> registration = | |
| 1548 update_helper->SetupInitialRegistration(kNewVersionOrigin); | |
| 1549 { | |
| 1550 const IPC::Message* start_msg = | |
| 1551 sink->GetUniqueMessageMatching(EmbeddedWorkerMsg_StartWorker::ID); | |
| 1552 ASSERT_TRUE(start_msg); | |
| 1553 EmbeddedWorkerMsg_StartWorker::Param param; | |
| 1554 EmbeddedWorkerMsg_StartWorker::Read(start_msg, ¶m); | |
| 1555 EmbeddedWorkerMsg_StartWorker_Params start_params = base::get<0>(param); | |
| 1556 EXPECT_FALSE(start_params.pause_after_download); | |
| 1557 sink->ClearMessages(); | |
| 1558 } | |
| 1559 | |
| 1560 // The updated version should pause after download. | |
| 1561 registration->AddListener(update_helper); | |
| 1562 registration->active_version()->StartUpdate(); | |
| 1563 base::RunLoop().RunUntilIdle(); | |
| 1564 { | |
| 1565 const IPC::Message* start_msg = | |
| 1566 sink->GetUniqueMessageMatching(EmbeddedWorkerMsg_StartWorker::ID); | |
| 1567 ASSERT_TRUE(start_msg); | |
| 1568 EmbeddedWorkerMsg_StartWorker::Param param; | |
| 1569 EmbeddedWorkerMsg_StartWorker::Read(start_msg, ¶m); | |
| 1570 EmbeddedWorkerMsg_StartWorker_Params start_params = base::get<0>(param); | |
| 1571 EXPECT_TRUE(start_params.pause_after_download); | |
| 1572 sink->ClearMessages(); | |
| 1573 } | |
| 1574 } | |
| 1575 | |
| 1576 } // namespace content | 1536 } // namespace content |
| OLD | NEW |