| 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 "content/browser/service_worker/service_worker_version.h" | 5 #include "content/browser/service_worker/service_worker_version.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <tuple> | 8 #include <tuple> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "base/test/histogram_tester.h" | 14 #include "base/test/histogram_tester.h" |
| 15 #include "base/test/simple_test_tick_clock.h" | 15 #include "base/test/simple_test_tick_clock.h" |
| 16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 17 #include "content/browser/service_worker/embedded_worker_registry.h" | 17 #include "content/browser/service_worker/embedded_worker_registry.h" |
| 18 #include "content/browser/service_worker/embedded_worker_status.h" | 18 #include "content/browser/service_worker/embedded_worker_status.h" |
| 19 #include "content/browser/service_worker/embedded_worker_test_helper.h" | 19 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
| 20 #include "content/browser/service_worker/service_worker_context_core.h" | 20 #include "content/browser/service_worker/service_worker_context_core.h" |
| 21 #include "content/browser/service_worker/service_worker_dispatcher_host.h" |
| 21 #include "content/browser/service_worker/service_worker_registration.h" | 22 #include "content/browser/service_worker/service_worker_registration.h" |
| 22 #include "content/browser/service_worker/service_worker_test_utils.h" | 23 #include "content/browser/service_worker/service_worker_test_utils.h" |
| 23 #include "content/common/service_worker/service_worker_utils.h" | 24 #include "content/common/service_worker/service_worker_utils.h" |
| 24 #include "content/public/test/mock_render_process_host.h" | 25 #include "content/public/test/mock_render_process_host.h" |
| 25 #include "content/public/test/test_browser_thread_bundle.h" | 26 #include "content/public/test/test_browser_thread_bundle.h" |
| 26 #include "content/public/test/test_service.mojom.h" | 27 #include "content/public/test/test_service.mojom.h" |
| 27 #include "content/public/test/test_utils.h" | 28 #include "content/public/test/test_utils.h" |
| 28 #include "mojo/public/cpp/bindings/strong_binding.h" | 29 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 29 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
| 30 | 31 |
| (...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerVersionTest); | 263 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerVersionTest); |
| 263 }; | 264 }; |
| 264 | 265 |
| 265 class MessageReceiverDisallowStart : public MessageReceiver { | 266 class MessageReceiverDisallowStart : public MessageReceiver { |
| 266 public: | 267 public: |
| 267 MessageReceiverDisallowStart() : MessageReceiver() {} | 268 MessageReceiverDisallowStart() : MessageReceiver() {} |
| 268 ~MessageReceiverDisallowStart() override {} | 269 ~MessageReceiverDisallowStart() override {} |
| 269 | 270 |
| 270 enum class StartMode { STALL, FAIL, SUCCEED }; | 271 enum class StartMode { STALL, FAIL, SUCCEED }; |
| 271 | 272 |
| 272 void OnStartWorker(int embedded_worker_id, | 273 void OnStartWorker( |
| 273 int64_t service_worker_version_id, | 274 int embedded_worker_id, |
| 274 const GURL& scope, | 275 int64_t service_worker_version_id, |
| 275 const GURL& script_url, | 276 const GURL& scope, |
| 276 bool pause_after_download, | 277 const GURL& script_url, |
| 277 mojom::ServiceWorkerEventDispatcherRequest request, | 278 bool pause_after_download, |
| 278 mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo | 279 mojom::ServiceWorkerEventDispatcherRequest request, |
| 279 instance_host) override { | 280 mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo instance_host, |
| 281 mojom::ServiceWorkerProviderClientInfoPtr provider_client_info) override { |
| 280 switch (mode_) { | 282 switch (mode_) { |
| 281 case StartMode::STALL: | 283 case StartMode::STALL: |
| 282 // Prepare for OnStopWorker(). | 284 // Prepare for OnStopWorker(). |
| 283 instance_host_ptr_map_[embedded_worker_id].Bind( | 285 instance_host_ptr_map_[embedded_worker_id].Bind( |
| 284 std::move(instance_host)); | 286 std::move(instance_host)); |
| 285 break; // Do nothing. | 287 break; // Do nothing. |
| 286 case StartMode::FAIL: | 288 case StartMode::FAIL: |
| 287 ASSERT_EQ(current_mock_instance_index_ + 1, | 289 ASSERT_EQ(current_mock_instance_index_ + 1, |
| 288 mock_instance_clients()->size()); | 290 mock_instance_clients()->size()); |
| 289 // Remove the connection by peer | 291 // Remove the connection by peer |
| 290 mock_instance_clients()->at(current_mock_instance_index_).reset(); | 292 mock_instance_clients()->at(current_mock_instance_index_).reset(); |
| 291 break; | 293 break; |
| 292 case StartMode::SUCCEED: | 294 case StartMode::SUCCEED: |
| 293 MessageReceiver::OnStartWorker( | 295 MessageReceiver::OnStartWorker( |
| 294 embedded_worker_id, service_worker_version_id, scope, script_url, | 296 embedded_worker_id, service_worker_version_id, scope, script_url, |
| 295 pause_after_download, std::move(request), std::move(instance_host)); | 297 pause_after_download, std::move(request), std::move(instance_host), |
| 298 std::move(provider_client_info)); |
| 296 break; | 299 break; |
| 297 } | 300 } |
| 298 current_mock_instance_index_++; | 301 current_mock_instance_index_++; |
| 299 } | 302 } |
| 300 | 303 |
| 301 void OnStopWorker(int embedded_worker_id) override { | 304 void OnStopWorker(int embedded_worker_id) override { |
| 302 if (instance_host_ptr_map_[embedded_worker_id]) { | 305 if (instance_host_ptr_map_[embedded_worker_id]) { |
| 303 instance_host_ptr_map_[embedded_worker_id]->OnStopped(); | 306 instance_host_ptr_map_[embedded_worker_id]->OnStopped(); |
| 304 base::RunLoop().RunUntilIdle(); | 307 base::RunLoop().RunUntilIdle(); |
| 305 return; | 308 return; |
| (...skipping 384 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 690 version_->AddListener(&listener); | 693 version_->AddListener(&listener); |
| 691 | 694 |
| 692 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; | 695 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; |
| 693 version_->StartWorker(ServiceWorkerMetrics::EventType::UNKNOWN, | 696 version_->StartWorker(ServiceWorkerMetrics::EventType::UNKNOWN, |
| 694 CreateReceiverOnCurrentThread(&status)); | 697 CreateReceiverOnCurrentThread(&status)); |
| 695 base::RunLoop().RunUntilIdle(); | 698 base::RunLoop().RunUntilIdle(); |
| 696 EXPECT_EQ(SERVICE_WORKER_OK, status); | 699 EXPECT_EQ(SERVICE_WORKER_OK, status); |
| 697 EXPECT_EQ(EmbeddedWorkerStatus::RUNNING, version_->running_status()); | 700 EXPECT_EQ(EmbeddedWorkerStatus::RUNNING, version_->running_status()); |
| 698 EXPECT_EQ(EmbeddedWorkerStatus::RUNNING, listener.last_status); | 701 EXPECT_EQ(EmbeddedWorkerStatus::RUNNING, listener.last_status); |
| 699 | 702 |
| 703 // Release the reference to |version_|. |
| 704 // Hosting provider has the reference to the |version_|. |
| 705 helper_->context()->RemoveProviderHost( |
| 706 version_->running_controller()->process_id(), |
| 707 version_->running_controller()->provider_id()); |
| 700 version_ = nullptr; | 708 version_ = nullptr; |
| 701 EXPECT_EQ(EmbeddedWorkerStatus::STOPPING, listener.last_status); | 709 EXPECT_EQ(EmbeddedWorkerStatus::STOPPING, listener.last_status); |
| 702 } | 710 } |
| 703 | 711 |
| 704 // Test that update isn't triggered for a non-stale worker. | 712 // Test that update isn't triggered for a non-stale worker. |
| 705 TEST_F(ServiceWorkerVersionTest, StaleUpdate_FreshWorker) { | 713 TEST_F(ServiceWorkerVersionTest, StaleUpdate_FreshWorker) { |
| 706 version_->SetStatus(ServiceWorkerVersion::ACTIVATED); | 714 version_->SetStatus(ServiceWorkerVersion::ACTIVATED); |
| 707 registration_->SetActiveVersion(version_); | 715 registration_->SetActiveVersion(version_); |
| 708 registration_->set_last_update_check(base::Time::Now()); | 716 registration_->set_last_update_check(base::Time::Now()); |
| 709 SimulateDispatchEvent(ServiceWorkerMetrics::EventType::PUSH); | 717 SimulateDispatchEvent(ServiceWorkerMetrics::EventType::PUSH); |
| (...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1096 TEST_F(ServiceWorkerFailToStartTest, RendererCrash) { | 1104 TEST_F(ServiceWorkerFailToStartTest, RendererCrash) { |
| 1097 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_NETWORK; // dummy value | 1105 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_NETWORK; // dummy value |
| 1098 version_->StartWorker(ServiceWorkerMetrics::EventType::UNKNOWN, | 1106 version_->StartWorker(ServiceWorkerMetrics::EventType::UNKNOWN, |
| 1099 CreateReceiverOnCurrentThread(&status)); | 1107 CreateReceiverOnCurrentThread(&status)); |
| 1100 base::RunLoop().RunUntilIdle(); | 1108 base::RunLoop().RunUntilIdle(); |
| 1101 | 1109 |
| 1102 // Callback has not completed yet. | 1110 // Callback has not completed yet. |
| 1103 EXPECT_EQ(SERVICE_WORKER_ERROR_NETWORK, status); | 1111 EXPECT_EQ(SERVICE_WORKER_ERROR_NETWORK, status); |
| 1104 EXPECT_EQ(EmbeddedWorkerStatus::STARTING, version_->running_status()); | 1112 EXPECT_EQ(EmbeddedWorkerStatus::STARTING, version_->running_status()); |
| 1105 | 1113 |
| 1106 // Simulate renderer crash: do what | 1114 // Simulate renderer crash: remove DispatcherHost like what |
| 1107 // ServiceWorkerDispatcherHost::OnFilterRemoved does. | 1115 // ServiceWorkerDispatcherHost::OnFilterRemoved does. |
| 1108 helper_->context()->RemoveDispatcherHost(helper_->mock_render_process_id()); | 1116 helper_->RegisterMockDispatcherHost(helper_->mock_render_process_id(), |
| 1117 nullptr); |
| 1109 base::RunLoop().RunUntilIdle(); | 1118 base::RunLoop().RunUntilIdle(); |
| 1110 | 1119 |
| 1111 // Callback completed. | 1120 // Callback completed. |
| 1112 EXPECT_EQ(SERVICE_WORKER_ERROR_START_WORKER_FAILED, status); | 1121 EXPECT_EQ(SERVICE_WORKER_ERROR_START_WORKER_FAILED, status); |
| 1113 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, version_->running_status()); | 1122 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, version_->running_status()); |
| 1114 } | 1123 } |
| 1115 | 1124 |
| 1116 TEST_F(ServiceWorkerFailToStartTest, Timeout) { | 1125 TEST_F(ServiceWorkerFailToStartTest, Timeout) { |
| 1117 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_NETWORK; // dummy value | 1126 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_NETWORK; // dummy value |
| 1118 | 1127 |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1306 EXPECT_EQ(EmbeddedWorkerStatus::RUNNING, version_->running_status()); | 1315 EXPECT_EQ(EmbeddedWorkerStatus::RUNNING, version_->running_status()); |
| 1307 | 1316 |
| 1308 int request_id = | 1317 int request_id = |
| 1309 version_->StartRequest(ServiceWorkerMetrics::EventType::SYNC, | 1318 version_->StartRequest(ServiceWorkerMetrics::EventType::SYNC, |
| 1310 CreateReceiverOnCurrentThread(&status)); | 1319 CreateReceiverOnCurrentThread(&status)); |
| 1311 base::RunLoop().RunUntilIdle(); | 1320 base::RunLoop().RunUntilIdle(); |
| 1312 | 1321 |
| 1313 // Callback has not completed yet. | 1322 // Callback has not completed yet. |
| 1314 EXPECT_EQ(SERVICE_WORKER_OK, status); | 1323 EXPECT_EQ(SERVICE_WORKER_OK, status); |
| 1315 | 1324 |
| 1316 // Simulate renderer crash: do what | 1325 // Simulate renderer crash: remove DispatcherHost like what |
| 1317 // ServiceWorkerDispatcherHost::OnFilterRemoved does. | 1326 // ServiceWorkerDispatcherHost::OnFilterRemoved does. |
| 1318 helper_->context()->RemoveDispatcherHost(helper_->mock_render_process_id()); | 1327 helper_->RegisterMockDispatcherHost(helper_->mock_render_process_id(), |
| 1328 nullptr); |
| 1319 base::RunLoop().RunUntilIdle(); | 1329 base::RunLoop().RunUntilIdle(); |
| 1320 | 1330 |
| 1321 // Callback completed. | 1331 // Callback completed. |
| 1322 EXPECT_EQ(SERVICE_WORKER_ERROR_FAILED, status); | 1332 EXPECT_EQ(SERVICE_WORKER_ERROR_FAILED, status); |
| 1323 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, version_->running_status()); | 1333 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, version_->running_status()); |
| 1324 | 1334 |
| 1325 // Request already failed, calling finsh should return false. | 1335 // Request already failed, calling finsh should return false. |
| 1326 EXPECT_FALSE(version_->FinishRequest(request_id, true /* was_handled */, | 1336 EXPECT_FALSE(version_->FinishRequest(request_id, true /* was_handled */, |
| 1327 base::Time::Now())); | 1337 base::Time::Now())); |
| 1328 } | 1338 } |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1442 // OnStarted. | 1452 // OnStarted. |
| 1443 version_->StopWorker(CreateReceiverOnCurrentThread(&status2)); | 1453 version_->StopWorker(CreateReceiverOnCurrentThread(&status2)); |
| 1444 base::RunLoop().RunUntilIdle(); | 1454 base::RunLoop().RunUntilIdle(); |
| 1445 | 1455 |
| 1446 EXPECT_EQ(SERVICE_WORKER_OK, status1); | 1456 EXPECT_EQ(SERVICE_WORKER_OK, status1); |
| 1447 EXPECT_EQ(SERVICE_WORKER_OK, status2); | 1457 EXPECT_EQ(SERVICE_WORKER_OK, status2); |
| 1448 EXPECT_EQ(EmbeddedWorkerStatus::RUNNING, version_->running_status()); | 1458 EXPECT_EQ(EmbeddedWorkerStatus::RUNNING, version_->running_status()); |
| 1449 } | 1459 } |
| 1450 | 1460 |
| 1451 } // namespace content | 1461 } // namespace content |
| OLD | NEW |