Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(375)

Side by Side Diff: content/browser/service_worker/service_worker_dispatcher_host_unittest.cc

Issue 2422793002: HTML MessagePort as mojo::MessagePipeHandle (Closed)
Patch Set: Eliminate unnecessary PostTask Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 SendGetRegistrations(provider_id); 229 SendGetRegistrations(provider_id);
232 EXPECT_TRUE(dispatcher_host_->ipc_sink()->GetUniqueMessageMatching( 230 EXPECT_TRUE(dispatcher_host_->ipc_sink()->GetUniqueMessageMatching(
233 expected_message)); 231 expected_message));
234 dispatcher_host_->ipc_sink()->ClearMessages(); 232 dispatcher_host_->ipc_sink()->ClearMessages();
235 } 233 }
236 234
237 void DispatchExtendableMessageEvent( 235 void DispatchExtendableMessageEvent(
238 scoped_refptr<ServiceWorkerVersion> worker, 236 scoped_refptr<ServiceWorkerVersion> worker,
239 const base::string16& message, 237 const base::string16& message,
240 const url::Origin& source_origin, 238 const url::Origin& source_origin,
241 const std::vector<int>& sent_message_ports, 239 const std::vector<MessagePort>& sent_message_ports,
242 ServiceWorkerProviderHost* sender_provider_host, 240 ServiceWorkerProviderHost* sender_provider_host,
243 const ServiceWorkerDispatcherHost::StatusCallback& callback) { 241 const ServiceWorkerDispatcherHost::StatusCallback& callback) {
244 dispatcher_host_->DispatchExtendableMessageEvent( 242 dispatcher_host_->DispatchExtendableMessageEvent(
245 std::move(worker), message, source_origin, sent_message_ports, 243 std::move(worker), message, source_origin, sent_message_ports,
246 sender_provider_host, callback); 244 sender_provider_host, callback);
247 } 245 }
248 246
249 ServiceWorkerProviderHost* CreateServiceWorkerProviderHost(int provider_id) { 247 ServiceWorkerProviderHost* CreateServiceWorkerProviderHost(int provider_id) {
250 return new ServiceWorkerProviderHost( 248 return new ServiceWorkerProviderHost(
251 helper_->mock_render_process_id(), kRenderFrameId, provider_id, 249 helper_->mock_render_process_id(), kRenderFrameId, provider_id,
(...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after
710 ServiceWorkerMetrics::EventType::ACTIVATE, 708 ServiceWorkerMetrics::EventType::ACTIVATE,
711 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback), 709 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback),
712 base::TimeDelta::FromSeconds(10), ServiceWorkerVersion::KILL_ON_TIMEOUT); 710 base::TimeDelta::FromSeconds(10), ServiceWorkerVersion::KILL_ON_TIMEOUT);
713 711
714 // Advance clock by a couple seconds. 712 // Advance clock by a couple seconds.
715 tick_clock->Advance(base::TimeDelta::FromSeconds(4)); 713 tick_clock->Advance(base::TimeDelta::FromSeconds(4));
716 base::TimeDelta remaining_time = version_->remaining_timeout(); 714 base::TimeDelta remaining_time = version_->remaining_timeout();
717 EXPECT_EQ(base::TimeDelta::FromSeconds(6), remaining_time); 715 EXPECT_EQ(base::TimeDelta::FromSeconds(6), remaining_time);
718 716
719 // Dispatch ExtendableMessageEvent. 717 // Dispatch ExtendableMessageEvent.
720 std::vector<int> ports; 718 std::vector<MessagePort> ports;
721 SetUpDummyMessagePort(&ports); 719 SetUpDummyMessagePort(&ports);
722 called = false; 720 called = false;
723 status = SERVICE_WORKER_ERROR_MAX_VALUE; 721 status = SERVICE_WORKER_ERROR_MAX_VALUE;
724 DispatchExtendableMessageEvent( 722 DispatchExtendableMessageEvent(
725 version_, base::string16(), url::Origin(version_->scope().GetOrigin()), 723 version_, base::string16(), url::Origin(version_->scope().GetOrigin()),
726 ports, provider_host_, base::Bind(&SaveStatusCallback, &called, &status)); 724 ports, provider_host_, base::Bind(&SaveStatusCallback, &called, &status));
727 for (int port : ports)
728 EXPECT_TRUE(MessagePortService::GetInstance()->AreMessagesHeld(port));
729 EXPECT_EQ(ref_count + 1, sender_worker_handle->ref_count()); 725 EXPECT_EQ(ref_count + 1, sender_worker_handle->ref_count());
730 base::RunLoop().RunUntilIdle(); 726 base::RunLoop().RunUntilIdle();
731 EXPECT_TRUE(called); 727 EXPECT_TRUE(called);
732 EXPECT_EQ(SERVICE_WORKER_OK, status); 728 EXPECT_EQ(SERVICE_WORKER_OK, status);
733 729
734 // Messages should be held until ports are created at the destination.
735 for (int port : ports)
736 EXPECT_TRUE(MessagePortService::GetInstance()->AreMessagesHeld(port));
737
738 EXPECT_EQ(ref_count + 1, sender_worker_handle->ref_count()); 730 EXPECT_EQ(ref_count + 1, sender_worker_handle->ref_count());
739 731
740 // Timeout of message event should not have extended life of service worker. 732 // Timeout of message event should not have extended life of service worker.
741 EXPECT_EQ(remaining_time, version_->remaining_timeout()); 733 EXPECT_EQ(remaining_time, version_->remaining_timeout());
742 } 734 }
743 735
744 TEST_F(ServiceWorkerDispatcherHostTest, DispatchExtendableMessageEvent_Fail) { 736 TEST_F(ServiceWorkerDispatcherHostTest, DispatchExtendableMessageEvent_Fail) {
745 GURL pattern = GURL("http://www.example.com/"); 737 GURL pattern = GURL("http://www.example.com/");
746 GURL script_url = GURL("http://www.example.com/service_worker.js"); 738 GURL script_url = GURL("http://www.example.com/service_worker.js");
747 739
748 Initialize(base::WrapUnique(new FailToStartWorkerTestHelper)); 740 Initialize(base::WrapUnique(new FailToStartWorkerTestHelper));
749 SendProviderCreated(SERVICE_WORKER_PROVIDER_FOR_WORKER, pattern); 741 SendProviderCreated(SERVICE_WORKER_PROVIDER_FOR_WORKER, pattern);
750 SetUpRegistration(pattern, script_url); 742 SetUpRegistration(pattern, script_url);
751 743
752 // Try to dispatch ExtendableMessageEvent. This should fail to start the 744 // Try to dispatch ExtendableMessageEvent. This should fail to start the
753 // worker and to dispatch the event. 745 // worker and to dispatch the event.
754 std::vector<int> ports; 746 std::vector<MessagePort> ports;
755 SetUpDummyMessagePort(&ports); 747 SetUpDummyMessagePort(&ports);
756 bool called = false; 748 bool called = false;
757 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; 749 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE;
758 DispatchExtendableMessageEvent( 750 DispatchExtendableMessageEvent(
759 version_, base::string16(), url::Origin(version_->scope().GetOrigin()), 751 version_, base::string16(), url::Origin(version_->scope().GetOrigin()),
760 ports, provider_host_, base::Bind(&SaveStatusCallback, &called, &status)); 752 ports, provider_host_, base::Bind(&SaveStatusCallback, &called, &status));
761 for (int port : ports)
762 EXPECT_TRUE(MessagePortService::GetInstance()->AreMessagesHeld(port));
763 base::RunLoop().RunUntilIdle(); 753 base::RunLoop().RunUntilIdle();
764 EXPECT_TRUE(called); 754 EXPECT_TRUE(called);
765 EXPECT_EQ(SERVICE_WORKER_ERROR_START_WORKER_FAILED, status); 755 EXPECT_EQ(SERVICE_WORKER_ERROR_START_WORKER_FAILED, status);
766
767 // The error callback should clean up the ports and handle.
768 for (int port : ports)
769 EXPECT_FALSE(MessagePortService::GetInstance()->AreMessagesHeld(port));
770 } 756 }
771 757
772 TEST_F(ServiceWorkerDispatcherHostTest, OnSetHostedVersionId) { 758 TEST_F(ServiceWorkerDispatcherHostTest, OnSetHostedVersionId) {
773 GURL pattern = GURL("http://www.example.com/"); 759 GURL pattern = GURL("http://www.example.com/");
774 GURL script_url = GURL("http://www.example.com/service_worker.js"); 760 GURL script_url = GURL("http://www.example.com/service_worker.js");
775 761
776 Initialize(base::WrapUnique(new FailToStartWorkerTestHelper)); 762 Initialize(base::WrapUnique(new FailToStartWorkerTestHelper));
777 SendProviderCreated(SERVICE_WORKER_PROVIDER_FOR_CONTROLLER, pattern); 763 SendProviderCreated(SERVICE_WORKER_PROVIDER_FOR_CONTROLLER, pattern);
778 SetUpRegistration(pattern, script_url); 764 SetUpRegistration(pattern, script_url);
779 765
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
846 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_FetchEventResponse( 832 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_FetchEventResponse(
847 version_->embedded_worker()->embedded_worker_id(), kFetchEventId, 833 version_->embedded_worker()->embedded_worker_id(), kFetchEventId,
848 SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK, ServiceWorkerResponse(), 834 SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK, ServiceWorkerResponse(),
849 base::Time::Now())); 835 base::Time::Now()));
850 836
851 base::RunLoop().RunUntilIdle(); 837 base::RunLoop().RunUntilIdle();
852 EXPECT_EQ(0, dispatcher_host_->bad_messages_received_count_); 838 EXPECT_EQ(0, dispatcher_host_->bad_messages_received_count_);
853 } 839 }
854 840
855 } // namespace content 841 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698