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 "content/browser/service_worker/service_worker_dispatcher_host.h" | 5 #include "content/browser/service_worker/service_worker_dispatcher_host.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
14 #include "base/run_loop.h" | 14 #include "base/run_loop.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/browser_thread_impl.h" | 17 #include "content/browser/browser_thread_impl.h" |
18 #include "content/browser/message_port_service.h" | |
19 #include "content/browser/service_worker/embedded_worker_instance.h" | 18 #include "content/browser/service_worker/embedded_worker_instance.h" |
20 #include "content/browser/service_worker/embedded_worker_registry.h" | 19 #include "content/browser/service_worker/embedded_worker_registry.h" |
21 #include "content/browser/service_worker/embedded_worker_status.h" | 20 #include "content/browser/service_worker/embedded_worker_status.h" |
22 #include "content/browser/service_worker/embedded_worker_test_helper.h" | 21 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
23 #include "content/browser/service_worker/service_worker_context_core.h" | 22 #include "content/browser/service_worker/service_worker_context_core.h" |
24 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 23 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
25 #include "content/browser/service_worker/service_worker_handle.h" | 24 #include "content/browser/service_worker/service_worker_handle.h" |
26 #include "content/browser/service_worker/service_worker_test_utils.h" | 25 #include "content/browser/service_worker/service_worker_test_utils.h" |
27 #include "content/common/service_worker/embedded_worker_messages.h" | 26 #include "content/common/service_worker/embedded_worker_messages.h" |
28 #include "content/common/service_worker/service_worker_messages.h" | 27 #include "content/common/service_worker/service_worker_messages.h" |
29 #include "content/common/service_worker/service_worker_types.h" | 28 #include "content/common/service_worker/service_worker_types.h" |
30 #include "content/common/service_worker/service_worker_utils.h" | 29 #include "content/common/service_worker/service_worker_utils.h" |
31 #include "content/public/common/content_switches.h" | 30 #include "content/public/common/content_switches.h" |
32 #include "content/public/test/mock_resource_context.h" | 31 #include "content/public/test/mock_resource_context.h" |
33 #include "content/public/test/test_browser_thread_bundle.h" | 32 #include "content/public/test/test_browser_thread_bundle.h" |
34 #include "content/test/test_content_browser_client.h" | 33 #include "content/test/test_content_browser_client.h" |
35 #include "testing/gtest/include/gtest/gtest.h" | 34 #include "testing/gtest/include/gtest/gtest.h" |
36 | 35 |
37 namespace content { | 36 namespace content { |
38 | 37 |
39 namespace { | 38 namespace { |
40 | 39 |
41 static void SaveStatusCallback(bool* called, | 40 static void SaveStatusCallback(bool* called, |
42 ServiceWorkerStatusCode* out, | 41 ServiceWorkerStatusCode* out, |
43 ServiceWorkerStatusCode status) { | 42 ServiceWorkerStatusCode status) { |
44 *called = true; | 43 *called = true; |
45 *out = status; | 44 *out = status; |
46 } | 45 } |
47 | 46 |
48 void SetUpDummyMessagePort(std::vector<int>* ports) { | 47 void SetUpDummyMessagePort(std::vector<MessagePort>* ports) { |
49 int port_id = -1; | 48 // Let the other end of the pipe close. |
50 MessagePortService::GetInstance()->Create(MSG_ROUTING_NONE, nullptr, | 49 mojo::MessagePipe pipe; |
51 &port_id); | 50 ports->push_back(MessagePort(std::move(pipe.handle0))); |
52 ports->push_back(port_id); | |
53 } | 51 } |
54 | 52 |
55 } // namespace | 53 } // namespace |
56 | 54 |
57 static const int kRenderFrameId = 1; | 55 static const int kRenderFrameId = 1; |
58 | 56 |
59 class TestingServiceWorkerDispatcherHost : public ServiceWorkerDispatcherHost { | 57 class TestingServiceWorkerDispatcherHost : public ServiceWorkerDispatcherHost { |
60 public: | 58 public: |
61 TestingServiceWorkerDispatcherHost( | 59 TestingServiceWorkerDispatcherHost( |
62 int process_id, | 60 int process_id, |
63 ServiceWorkerContextWrapper* context_wrapper, | 61 ServiceWorkerContextWrapper* context_wrapper, |
64 ResourceContext* resource_context, | 62 ResourceContext* resource_context, |
65 EmbeddedWorkerTestHelper* helper) | 63 EmbeddedWorkerTestHelper* helper) |
66 : ServiceWorkerDispatcherHost(process_id, nullptr, resource_context), | 64 : ServiceWorkerDispatcherHost(process_id, resource_context), |
67 bad_messages_received_count_(0), | 65 bad_messages_received_count_(0), |
68 helper_(helper) { | 66 helper_(helper) { |
69 Init(context_wrapper); | 67 Init(context_wrapper); |
70 } | 68 } |
71 | 69 |
72 bool Send(IPC::Message* message) override { return helper_->Send(message); } | 70 bool Send(IPC::Message* message) override { return helper_->Send(message); } |
73 | 71 |
74 IPC::TestSink* ipc_sink() { return helper_->ipc_sink(); } | 72 IPC::TestSink* ipc_sink() { return helper_->ipc_sink(); } |
75 | 73 |
76 void ShutdownForBadMessage() override { ++bad_messages_received_count_; } | 74 void ShutdownForBadMessage() override { ++bad_messages_received_count_; } |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 SendGetRegistrations(provider_id); | 227 SendGetRegistrations(provider_id); |
230 EXPECT_TRUE(dispatcher_host_->ipc_sink()->GetUniqueMessageMatching( | 228 EXPECT_TRUE(dispatcher_host_->ipc_sink()->GetUniqueMessageMatching( |
231 expected_message)); | 229 expected_message)); |
232 dispatcher_host_->ipc_sink()->ClearMessages(); | 230 dispatcher_host_->ipc_sink()->ClearMessages(); |
233 } | 231 } |
234 | 232 |
235 void DispatchExtendableMessageEvent( | 233 void DispatchExtendableMessageEvent( |
236 scoped_refptr<ServiceWorkerVersion> worker, | 234 scoped_refptr<ServiceWorkerVersion> worker, |
237 const base::string16& message, | 235 const base::string16& message, |
238 const url::Origin& source_origin, | 236 const url::Origin& source_origin, |
239 const std::vector<int>& sent_message_ports, | 237 const std::vector<MessagePort>& sent_message_ports, |
240 ServiceWorkerProviderHost* sender_provider_host, | 238 ServiceWorkerProviderHost* sender_provider_host, |
241 const ServiceWorkerDispatcherHost::StatusCallback& callback) { | 239 const ServiceWorkerDispatcherHost::StatusCallback& callback) { |
242 dispatcher_host_->DispatchExtendableMessageEvent( | 240 dispatcher_host_->DispatchExtendableMessageEvent( |
243 std::move(worker), message, source_origin, sent_message_ports, | 241 std::move(worker), message, source_origin, sent_message_ports, |
244 sender_provider_host, callback); | 242 sender_provider_host, callback); |
245 } | 243 } |
246 | 244 |
247 ServiceWorkerProviderHost* CreateServiceWorkerProviderHost(int provider_id) { | 245 ServiceWorkerProviderHost* CreateServiceWorkerProviderHost(int provider_id) { |
248 return new ServiceWorkerProviderHost( | 246 return new ServiceWorkerProviderHost( |
249 helper_->mock_render_process_id(), kRenderFrameId, provider_id, | 247 helper_->mock_render_process_id(), kRenderFrameId, provider_id, |
(...skipping 464 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
714 ServiceWorkerMetrics::EventType::ACTIVATE, | 712 ServiceWorkerMetrics::EventType::ACTIVATE, |
715 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback), | 713 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback), |
716 base::TimeDelta::FromSeconds(10), ServiceWorkerVersion::KILL_ON_TIMEOUT); | 714 base::TimeDelta::FromSeconds(10), ServiceWorkerVersion::KILL_ON_TIMEOUT); |
717 | 715 |
718 // Advance clock by a couple seconds. | 716 // Advance clock by a couple seconds. |
719 tick_clock->Advance(base::TimeDelta::FromSeconds(4)); | 717 tick_clock->Advance(base::TimeDelta::FromSeconds(4)); |
720 base::TimeDelta remaining_time = version_->remaining_timeout(); | 718 base::TimeDelta remaining_time = version_->remaining_timeout(); |
721 EXPECT_EQ(base::TimeDelta::FromSeconds(6), remaining_time); | 719 EXPECT_EQ(base::TimeDelta::FromSeconds(6), remaining_time); |
722 | 720 |
723 // Dispatch ExtendableMessageEvent. | 721 // Dispatch ExtendableMessageEvent. |
724 std::vector<int> ports; | 722 std::vector<MessagePort> ports; |
725 SetUpDummyMessagePort(&ports); | 723 SetUpDummyMessagePort(&ports); |
726 called = false; | 724 called = false; |
727 status = SERVICE_WORKER_ERROR_MAX_VALUE; | 725 status = SERVICE_WORKER_ERROR_MAX_VALUE; |
728 DispatchExtendableMessageEvent( | 726 DispatchExtendableMessageEvent( |
729 version_, base::string16(), url::Origin(version_->scope().GetOrigin()), | 727 version_, base::string16(), url::Origin(version_->scope().GetOrigin()), |
730 ports, provider_host_, base::Bind(&SaveStatusCallback, &called, &status)); | 728 ports, provider_host_, base::Bind(&SaveStatusCallback, &called, &status)); |
731 for (int port : ports) | |
732 EXPECT_TRUE(MessagePortService::GetInstance()->AreMessagesHeld(port)); | |
733 EXPECT_EQ(ref_count + 1, sender_worker_handle->ref_count()); | 729 EXPECT_EQ(ref_count + 1, sender_worker_handle->ref_count()); |
734 base::RunLoop().RunUntilIdle(); | 730 base::RunLoop().RunUntilIdle(); |
735 EXPECT_TRUE(called); | 731 EXPECT_TRUE(called); |
736 EXPECT_EQ(SERVICE_WORKER_OK, status); | 732 EXPECT_EQ(SERVICE_WORKER_OK, status); |
737 | 733 |
738 // Messages should be held until ports are created at the destination. | |
739 for (int port : ports) | |
740 EXPECT_TRUE(MessagePortService::GetInstance()->AreMessagesHeld(port)); | |
741 | |
742 EXPECT_EQ(ref_count + 1, sender_worker_handle->ref_count()); | 734 EXPECT_EQ(ref_count + 1, sender_worker_handle->ref_count()); |
743 | 735 |
744 // Timeout of message event should not have extended life of service worker. | 736 // Timeout of message event should not have extended life of service worker. |
745 EXPECT_EQ(remaining_time, version_->remaining_timeout()); | 737 EXPECT_EQ(remaining_time, version_->remaining_timeout()); |
746 } | 738 } |
747 | 739 |
748 TEST_P(ServiceWorkerDispatcherHostTestP, DispatchExtendableMessageEvent_Fail) { | 740 TEST_P(ServiceWorkerDispatcherHostTestP, DispatchExtendableMessageEvent_Fail) { |
749 GURL pattern = GURL("http://www.example.com/"); | 741 GURL pattern = GURL("http://www.example.com/"); |
750 GURL script_url = GURL("http://www.example.com/service_worker.js"); | 742 GURL script_url = GURL("http://www.example.com/service_worker.js"); |
751 | 743 |
752 Initialize(base::WrapUnique(new FailToStartWorkerTestHelper)); | 744 Initialize(base::WrapUnique(new FailToStartWorkerTestHelper)); |
753 SendProviderCreated(SERVICE_WORKER_PROVIDER_FOR_WORKER, pattern); | 745 SendProviderCreated(SERVICE_WORKER_PROVIDER_FOR_WORKER, pattern); |
754 SetUpRegistration(pattern, script_url); | 746 SetUpRegistration(pattern, script_url); |
755 | 747 |
756 // Try to dispatch ExtendableMessageEvent. This should fail to start the | 748 // Try to dispatch ExtendableMessageEvent. This should fail to start the |
757 // worker and to dispatch the event. | 749 // worker and to dispatch the event. |
758 std::vector<int> ports; | 750 std::vector<MessagePort> ports; |
759 SetUpDummyMessagePort(&ports); | 751 SetUpDummyMessagePort(&ports); |
760 bool called = false; | 752 bool called = false; |
761 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; | 753 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; |
762 DispatchExtendableMessageEvent( | 754 DispatchExtendableMessageEvent( |
763 version_, base::string16(), url::Origin(version_->scope().GetOrigin()), | 755 version_, base::string16(), url::Origin(version_->scope().GetOrigin()), |
764 ports, provider_host_, base::Bind(&SaveStatusCallback, &called, &status)); | 756 ports, provider_host_, base::Bind(&SaveStatusCallback, &called, &status)); |
765 for (int port : ports) | |
766 EXPECT_TRUE(MessagePortService::GetInstance()->AreMessagesHeld(port)); | |
767 base::RunLoop().RunUntilIdle(); | 757 base::RunLoop().RunUntilIdle(); |
768 EXPECT_TRUE(called); | 758 EXPECT_TRUE(called); |
769 EXPECT_EQ(SERVICE_WORKER_ERROR_START_WORKER_FAILED, status); | 759 EXPECT_EQ(SERVICE_WORKER_ERROR_START_WORKER_FAILED, status); |
770 | |
771 // The error callback should clean up the ports and handle. | |
772 for (int port : ports) | |
773 EXPECT_FALSE(MessagePortService::GetInstance()->AreMessagesHeld(port)); | |
774 } | 760 } |
775 | 761 |
776 TEST_P(ServiceWorkerDispatcherHostTestP, OnSetHostedVersionId) { | 762 TEST_P(ServiceWorkerDispatcherHostTestP, OnSetHostedVersionId) { |
777 GURL pattern = GURL("http://www.example.com/"); | 763 GURL pattern = GURL("http://www.example.com/"); |
778 GURL script_url = GURL("http://www.example.com/service_worker.js"); | 764 GURL script_url = GURL("http://www.example.com/service_worker.js"); |
779 | 765 |
780 Initialize(base::WrapUnique(new FailToStartWorkerTestHelper)); | 766 Initialize(base::WrapUnique(new FailToStartWorkerTestHelper)); |
781 SendProviderCreated(SERVICE_WORKER_PROVIDER_FOR_CONTROLLER, pattern); | 767 SendProviderCreated(SERVICE_WORKER_PROVIDER_FOR_CONTROLLER, pattern); |
782 SetUpRegistration(pattern, script_url); | 768 SetUpRegistration(pattern, script_url); |
783 | 769 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
854 | 840 |
855 base::RunLoop().RunUntilIdle(); | 841 base::RunLoop().RunUntilIdle(); |
856 EXPECT_EQ(0, dispatcher_host_->bad_messages_received_count_); | 842 EXPECT_EQ(0, dispatcher_host_->bad_messages_received_count_); |
857 } | 843 } |
858 | 844 |
859 INSTANTIATE_TEST_CASE_P(ServiceWorkerDispatcherHostTest, | 845 INSTANTIATE_TEST_CASE_P(ServiceWorkerDispatcherHostTest, |
860 ServiceWorkerDispatcherHostTestP, | 846 ServiceWorkerDispatcherHostTestP, |
861 testing::Bool()); | 847 testing::Bool()); |
862 | 848 |
863 } // namespace content | 849 } // namespace content |
OLD | NEW |