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

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

Issue 2653493009: Add two interfaces for ServiceWorkerProviderContext/ProviderHost (Closed)
Patch Set: Addressed comments from falken Created 3 years, 7 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/service_worker/embedded_worker_instance.h" 18 #include "content/browser/service_worker/embedded_worker_instance.h"
19 #include "content/browser/service_worker/embedded_worker_registry.h" 19 #include "content/browser/service_worker/embedded_worker_registry.h"
20 #include "content/browser/service_worker/embedded_worker_status.h" 20 #include "content/browser/service_worker/embedded_worker_status.h"
21 #include "content/browser/service_worker/embedded_worker_test_helper.h" 21 #include "content/browser/service_worker/embedded_worker_test_helper.h"
22 #include "content/browser/service_worker/service_worker_context_core.h" 22 #include "content/browser/service_worker/service_worker_context_core.h"
23 #include "content/browser/service_worker/service_worker_context_wrapper.h" 23 #include "content/browser/service_worker/service_worker_context_wrapper.h"
24 #include "content/browser/service_worker/service_worker_handle.h" 24 #include "content/browser/service_worker/service_worker_handle.h"
25 #include "content/browser/service_worker/service_worker_navigation_handle_core.h "
25 #include "content/browser/service_worker/service_worker_test_utils.h" 26 #include "content/browser/service_worker/service_worker_test_utils.h"
26 #include "content/common/service_worker/embedded_worker_messages.h" 27 #include "content/common/service_worker/embedded_worker_messages.h"
27 #include "content/common/service_worker/service_worker_messages.h" 28 #include "content/common/service_worker/service_worker_messages.h"
28 #include "content/common/service_worker/service_worker_types.h" 29 #include "content/common/service_worker/service_worker_types.h"
29 #include "content/common/service_worker/service_worker_utils.h" 30 #include "content/common/service_worker/service_worker_utils.h"
31 #include "content/public/common/browser_side_navigation_policy.h"
30 #include "content/public/common/content_switches.h" 32 #include "content/public/common/content_switches.h"
31 #include "content/public/test/mock_resource_context.h" 33 #include "content/public/test/mock_resource_context.h"
32 #include "content/public/test/test_browser_thread_bundle.h" 34 #include "content/public/test/test_browser_thread_bundle.h"
33 #include "content/test/test_content_browser_client.h" 35 #include "content/test/test_content_browser_client.h"
34 #include "testing/gtest/include/gtest/gtest.h" 36 #include "testing/gtest/include/gtest/gtest.h"
35 37
36 namespace content { 38 namespace content {
37 39
38 namespace { 40 namespace {
39 41
40 static void SaveStatusCallback(bool* called, 42 static void SaveStatusCallback(bool* called,
41 ServiceWorkerStatusCode* out, 43 ServiceWorkerStatusCode* out,
42 ServiceWorkerStatusCode status) { 44 ServiceWorkerStatusCode status) {
43 *called = true; 45 *called = true;
44 *out = status; 46 *out = status;
45 } 47 }
46 48
47 void SetUpDummyMessagePort(std::vector<MessagePort>* ports) { 49 void SetUpDummyMessagePort(std::vector<MessagePort>* ports) {
48 // Let the other end of the pipe close. 50 // Let the other end of the pipe close.
49 mojo::MessagePipe pipe; 51 mojo::MessagePipe pipe;
50 ports->push_back(MessagePort(std::move(pipe.handle0))); 52 ports->push_back(MessagePort(std::move(pipe.handle0)));
51 } 53 }
52 54
55 struct RemoteProviderInfo {
56 mojom::ServiceWorkerProviderHostAssociatedPtr host_ptr;
57 mojom::ServiceWorkerProviderAssociatedRequest client_request;
58 };
59
60 RemoteProviderInfo SetupProviderHostInfoPtrs(
61 ServiceWorkerProviderHostInfo* host_info) {
62 RemoteProviderInfo remote_info;
63 mojom::ServiceWorkerProviderAssociatedPtr browser_side_client_ptr;
64 remote_info.client_request =
65 mojo::MakeIsolatedRequest(&browser_side_client_ptr);
66 host_info->host_request = mojo::MakeIsolatedRequest(&remote_info.host_ptr);
67 host_info->client_ptr_info = browser_side_client_ptr.PassInterface();
68 EXPECT_TRUE(host_info->host_request.is_pending());
69 EXPECT_TRUE(host_info->client_ptr_info.is_valid());
70 EXPECT_TRUE(remote_info.host_ptr.is_bound());
71 EXPECT_TRUE(remote_info.client_request.is_pending());
72 return remote_info;
73 }
74
75 std::unique_ptr<ServiceWorkerNavigationHandleCore> CreateNavigationHandleCore(
76 ServiceWorkerContextWrapper* context_wrapper) {
77 std::unique_ptr<ServiceWorkerNavigationHandleCore> navigation_handle_core;
78 BrowserThread::PostTaskAndReplyWithResult(
79 BrowserThread::UI, FROM_HERE,
80 base::Bind(
81 [](ServiceWorkerContextWrapper* wrapper) {
82 return base::MakeUnique<ServiceWorkerNavigationHandleCore>(nullptr,
83 wrapper);
84 },
85 context_wrapper),
86 base::Bind(
87 [](std::unique_ptr<ServiceWorkerNavigationHandleCore>* dest,
88 std::unique_ptr<ServiceWorkerNavigationHandleCore> src) {
89 *dest = std::move(src);
90 },
91 &navigation_handle_core));
92 base::RunLoop().RunUntilIdle();
93 return navigation_handle_core;
94 }
95
53 } // namespace 96 } // namespace
54 97
55 static const int kRenderFrameId = 1; 98 static const int kRenderFrameId = 1;
56 99
57 class TestingServiceWorkerDispatcherHost : public ServiceWorkerDispatcherHost { 100 class TestingServiceWorkerDispatcherHost : public ServiceWorkerDispatcherHost {
58 public: 101 public:
59 TestingServiceWorkerDispatcherHost( 102 TestingServiceWorkerDispatcherHost(
60 int process_id, 103 int process_id,
61 ServiceWorkerContextWrapper* context_wrapper, 104 ServiceWorkerContextWrapper* context_wrapper,
62 ResourceContext* resource_context, 105 ResourceContext* resource_context,
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 int embedded_worker_id) { 204 int embedded_worker_id) {
162 dispatcher_host_->OnSetHostedVersionId(provider_id, version_id, 205 dispatcher_host_->OnSetHostedVersionId(provider_id, version_id,
163 embedded_worker_id); 206 embedded_worker_id);
164 } 207 }
165 208
166 void SendProviderCreated(ServiceWorkerProviderType type, 209 void SendProviderCreated(ServiceWorkerProviderType type,
167 const GURL& pattern) { 210 const GURL& pattern) {
168 const int64_t kProviderId = 99; 211 const int64_t kProviderId = 99;
169 ServiceWorkerProviderHostInfo info(kProviderId, MSG_ROUTING_NONE, type, 212 ServiceWorkerProviderHostInfo info(kProviderId, MSG_ROUTING_NONE, type,
170 true /* is_parent_frame_secure */); 213 true /* is_parent_frame_secure */);
214 mojom::ServiceWorkerProviderAssociatedPtr client_ptr;
215 remote_endpoint_.client_request = mojo::MakeIsolatedRequest(&client_ptr);
216 info.client_ptr_info = client_ptr.PassInterface();
217 info.host_request = mojo::MakeIsolatedRequest(&remote_endpoint_.host_ptr);
218
171 dispatcher_host_->OnProviderCreated(std::move(info)); 219 dispatcher_host_->OnProviderCreated(std::move(info));
172 helper_->SimulateAddProcessToPattern(pattern, 220 helper_->SimulateAddProcessToPattern(pattern,
173 helper_->mock_render_process_id()); 221 helper_->mock_render_process_id());
174 provider_host_ = context()->GetProviderHost( 222 provider_host_ = context()->GetProviderHost(
175 helper_->mock_render_process_id(), kProviderId); 223 helper_->mock_render_process_id(), kProviderId);
176 } 224 }
177 225
178 void SendRegister(int64_t provider_id, GURL pattern, GURL worker_url) { 226 void SendRegister(int64_t provider_id, GURL pattern, GURL worker_url) {
179 dispatcher_host_->OnMessageReceived( 227 dispatcher_host_->OnMessageReceived(
180 ServiceWorkerHostMsg_RegisterServiceWorker( 228 ServiceWorkerHostMsg_RegisterServiceWorker(
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 const ServiceWorkerDispatcherHost::StatusCallback& callback) { 294 const ServiceWorkerDispatcherHost::StatusCallback& callback) {
247 dispatcher_host_->DispatchExtendableMessageEvent( 295 dispatcher_host_->DispatchExtendableMessageEvent(
248 std::move(worker), message, source_origin, sent_message_ports, 296 std::move(worker), message, source_origin, sent_message_ports,
249 sender_provider_host, callback); 297 sender_provider_host, callback);
250 } 298 }
251 299
252 std::unique_ptr<ServiceWorkerProviderHost> CreateServiceWorkerProviderHost( 300 std::unique_ptr<ServiceWorkerProviderHost> CreateServiceWorkerProviderHost(
253 int provider_id) { 301 int provider_id) {
254 return CreateProviderHostWithDispatcherHost( 302 return CreateProviderHostWithDispatcherHost(
255 helper_->mock_render_process_id(), provider_id, context()->AsWeakPtr(), 303 helper_->mock_render_process_id(), provider_id, context()->AsWeakPtr(),
256 kRenderFrameId, dispatcher_host_.get()); 304 kRenderFrameId, dispatcher_host_.get(), &remote_endpoint_);
257 } 305 }
258 306
259 TestBrowserThreadBundle browser_thread_bundle_; 307 TestBrowserThreadBundle browser_thread_bundle_;
260 content::MockResourceContext resource_context_; 308 content::MockResourceContext resource_context_;
261 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; 309 std::unique_ptr<EmbeddedWorkerTestHelper> helper_;
262 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host_; 310 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host_;
263 scoped_refptr<ServiceWorkerRegistration> registration_; 311 scoped_refptr<ServiceWorkerRegistration> registration_;
264 scoped_refptr<ServiceWorkerVersion> version_; 312 scoped_refptr<ServiceWorkerVersion> version_;
265 ServiceWorkerProviderHost* provider_host_; 313 ServiceWorkerProviderHost* provider_host_;
314 ServiceWorkerRemoteProviderEndpoint remote_endpoint_;
266 }; 315 };
267 316
268 class ServiceWorkerTestContentBrowserClient : public TestContentBrowserClient { 317 class ServiceWorkerTestContentBrowserClient : public TestContentBrowserClient {
269 public: 318 public:
270 ServiceWorkerTestContentBrowserClient() {} 319 ServiceWorkerTestContentBrowserClient() {}
271 bool AllowServiceWorker( 320 bool AllowServiceWorker(
272 const GURL& scope, 321 const GURL& scope,
273 const GURL& first_party, 322 const GURL& first_party,
274 content::ResourceContext* context, 323 content::ResourceContext* context,
275 const base::Callback<WebContents*(void)>& wc_getter) override { 324 const base::Callback<WebContents*(void)>& wc_getter) override {
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after
502 // Let the shutdown reach the simulated IO thread. 551 // Let the shutdown reach the simulated IO thread.
503 base::RunLoop().RunUntilIdle(); 552 base::RunLoop().RunUntilIdle();
504 553
505 Register(-1, 554 Register(-1,
506 GURL(), 555 GURL(),
507 GURL(), 556 GURL(),
508 ServiceWorkerMsg_ServiceWorkerRegistrationError::ID); 557 ServiceWorkerMsg_ServiceWorkerRegistrationError::ID);
509 } 558 }
510 559
511 TEST_F(ServiceWorkerDispatcherHostTest, ProviderCreatedAndDestroyed) { 560 TEST_F(ServiceWorkerDispatcherHostTest, ProviderCreatedAndDestroyed) {
512 const int kProviderId = 1001; 561 const int kProviderId = (IsBrowserSideNavigationEnabled() ? -2 : 1001);
falken 2017/05/22 08:28:12 Can PlzNavigate be -1001 also to make it look symm
shimazu 2017/05/23 06:29:33 Actually it should be -2 because kProviderId is no
falken 2017/05/23 08:31:49 Can you add a comment like: // For PlzNavigate, k
513 int process_id = helper_->mock_render_process_id(); 562 int process_id = helper_->mock_render_process_id();
514 563
515 dispatcher_host_->OnProviderCreated(ServiceWorkerProviderHostInfo( 564 // Setup ServiceWorkerProviderHostInfo
falken 2017/05/22 08:28:12 nit: period at end of sentence
shimazu 2017/05/23 06:29:33 Done.
516 kProviderId, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW, 565 ServiceWorkerProviderHostInfo host_info_1(kProviderId, 1 /* route_id */,
517 true /* is_parent_frame_secure */)); 566 SERVICE_WORKER_PROVIDER_FOR_WINDOW,
567 true /* is_parent_frame_secure */);
568 ServiceWorkerProviderHostInfo host_info_2(kProviderId, 1 /* route_id */,
569 SERVICE_WORKER_PROVIDER_FOR_WINDOW,
570 true /* is_parent_frame_secure */);
571 ServiceWorkerProviderHostInfo host_info_3(kProviderId, 1 /* route_id */,
572 SERVICE_WORKER_PROVIDER_FOR_WINDOW,
573 true /* is_parent_frame_secure */);
574 RemoteProviderInfo remote_info_1 = SetupProviderHostInfoPtrs(&host_info_1);
575 RemoteProviderInfo remote_info_2 = SetupProviderHostInfoPtrs(&host_info_2);
576 RemoteProviderInfo remote_info_3 = SetupProviderHostInfoPtrs(&host_info_3);
577
578 // PlzNavigate
579 std::unique_ptr<ServiceWorkerNavigationHandleCore> navigation_handle_core;
580 if (IsBrowserSideNavigationEnabled()) {
581 navigation_handle_core =
582 CreateNavigationHandleCore(helper_->context_wrapper());
583 ASSERT_TRUE(navigation_handle_core);
584 // ProviderHost should be created before OnProviderCreated.
585 navigation_handle_core->DidPreCreateProviderHost(
586 ServiceWorkerProviderHost::PreCreateNavigationHost(
587 helper_->context()->AsWeakPtr(), true /* are_ancestors_secure */,
588 base::Callback<WebContents*(void)>()));
589 }
590
591 dispatcher_host_->OnProviderCreated(std::move(host_info_1));
518 EXPECT_TRUE(context()->GetProviderHost(process_id, kProviderId)); 592 EXPECT_TRUE(context()->GetProviderHost(process_id, kProviderId));
519 593
520 // Two with the same ID should be seen as a bad message. 594 // Two with the same ID should be seen as a bad message.
521 dispatcher_host_->OnProviderCreated(ServiceWorkerProviderHostInfo( 595 dispatcher_host_->OnProviderCreated(std::move(host_info_2));
522 kProviderId, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW,
523 true /* is_parent_frame_secure */));
524 EXPECT_EQ(1, dispatcher_host_->bad_messages_received_count_); 596 EXPECT_EQ(1, dispatcher_host_->bad_messages_received_count_);
525 597
526 dispatcher_host_->OnProviderDestroyed(kProviderId); 598 // Releasing the interface pointer destroys the counterpart.
599 remote_info_1.host_ptr.reset();
600 base::RunLoop().RunUntilIdle();
527 EXPECT_FALSE(context()->GetProviderHost(process_id, kProviderId)); 601 EXPECT_FALSE(context()->GetProviderHost(process_id, kProviderId));
528 602
529 // Destroying an ID that does not exist warrants a bad message. 603 // PlzNavigate
530 dispatcher_host_->OnProviderDestroyed(kProviderId); 604 // Prepare another navigation handle to create another provider host.
531 EXPECT_EQ(2, dispatcher_host_->bad_messages_received_count_); 605 if (IsBrowserSideNavigationEnabled()) {
606 navigation_handle_core =
607 CreateNavigationHandleCore(helper_->context_wrapper());
608 ASSERT_TRUE(navigation_handle_core);
609 // ProviderHost should be created before OnProviderCreated.
610 navigation_handle_core->DidPreCreateProviderHost(
611 ServiceWorkerProviderHost::PreCreateNavigationHost(
612 helper_->context()->AsWeakPtr(), true /* are_ancestors_secure */,
613 base::Callback<WebContents*(void)>()));
614 }
532 615
533 // Deletion of the dispatcher_host should cause providers for that 616 // Deletion of the dispatcher_host should cause providers for that
534 // process to get deleted as well. 617 // process to get deleted as well.
535 dispatcher_host_->OnProviderCreated(ServiceWorkerProviderHostInfo( 618 dispatcher_host_->OnProviderCreated(std::move(host_info_3));
536 kProviderId, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW,
537 true /* is_parent_frame_secure */));
538 EXPECT_TRUE(context()->GetProviderHost(process_id, kProviderId)); 619 EXPECT_TRUE(context()->GetProviderHost(process_id, kProviderId));
539 EXPECT_TRUE(dispatcher_host_->HasOneRef()); 620 EXPECT_TRUE(dispatcher_host_->HasOneRef());
540 dispatcher_host_ = nullptr; 621 dispatcher_host_ = nullptr;
541 EXPECT_FALSE(context()->GetProviderHost(process_id, kProviderId)); 622 EXPECT_FALSE(context()->GetProviderHost(process_id, kProviderId));
542 } 623 }
543 624
544 TEST_F(ServiceWorkerDispatcherHostTest, GetRegistration_SameOrigin) { 625 TEST_F(ServiceWorkerDispatcherHostTest, GetRegistration_SameOrigin) {
545 const int64_t kProviderId = 99; // Dummy value 626 const int64_t kProviderId = 99; // Dummy value
546 std::unique_ptr<ServiceWorkerProviderHost> host( 627 std::unique_ptr<ServiceWorkerProviderHost> host(
547 CreateServiceWorkerProviderHost(kProviderId)); 628 CreateServiceWorkerProviderHost(kProviderId));
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
662 // We should be able to hook up a new dispatcher host although the old object 743 // We should be able to hook up a new dispatcher host although the old object
663 // is not yet destroyed. This is what the browser does when reusing a crashed 744 // is not yet destroyed. This is what the browser does when reusing a crashed
664 // render process. 745 // render process.
665 scoped_refptr<TestingServiceWorkerDispatcherHost> new_dispatcher_host( 746 scoped_refptr<TestingServiceWorkerDispatcherHost> new_dispatcher_host(
666 new TestingServiceWorkerDispatcherHost( 747 new TestingServiceWorkerDispatcherHost(
667 process_id, context_wrapper(), &resource_context_, helper_.get())); 748 process_id, context_wrapper(), &resource_context_, helper_.get()));
668 749
669 // To show the new dispatcher can operate, simulate provider creation. Since 750 // To show the new dispatcher can operate, simulate provider creation. Since
670 // the old dispatcher cleaned up the old provider host, the new one won't 751 // the old dispatcher cleaned up the old provider host, the new one won't
671 // complain. 752 // complain.
672 new_dispatcher_host->OnProviderCreated(ServiceWorkerProviderHostInfo( 753 ServiceWorkerProviderHostInfo host_info(provider_id, MSG_ROUTING_NONE,
673 provider_id, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW, 754 SERVICE_WORKER_PROVIDER_FOR_WINDOW,
674 true /* is_parent_frame_secure */)); 755 true /* is_parent_frame_secure */);
756 ServiceWorkerRemoteProviderEndpoint remote_endpoint;
757 mojom::ServiceWorkerProviderAssociatedPtr client_ptr;
758 remote_endpoint.client_request = mojo::MakeIsolatedRequest(&client_ptr);
759 host_info.client_ptr_info = client_ptr.PassInterface();
760 host_info.host_request = mojo::MakeIsolatedRequest(&remote_endpoint.host_ptr);
761 new_dispatcher_host->OnProviderCreated(std::move(host_info));
675 EXPECT_EQ(0, new_dispatcher_host->bad_messages_received_count_); 762 EXPECT_EQ(0, new_dispatcher_host->bad_messages_received_count_);
676 } 763 }
677 764
678 TEST_F(ServiceWorkerDispatcherHostTest, DispatchExtendableMessageEvent) { 765 TEST_F(ServiceWorkerDispatcherHostTest, DispatchExtendableMessageEvent) {
679 GURL pattern = GURL("http://www.example.com/"); 766 GURL pattern = GURL("http://www.example.com/");
680 GURL script_url = GURL("http://www.example.com/service_worker.js"); 767 GURL script_url = GURL("http://www.example.com/service_worker.js");
681 768
682 SendProviderCreated(SERVICE_WORKER_PROVIDER_FOR_CONTROLLER, pattern); 769 SendProviderCreated(SERVICE_WORKER_PROVIDER_FOR_CONTROLLER, pattern);
683 SetUpRegistration(pattern, script_url); 770 SetUpRegistration(pattern, script_url);
684 771
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
833 const int kFetchEventId = 91; // Dummy value 920 const int kFetchEventId = 91; // Dummy value
834 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_FetchEventResponse( 921 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_FetchEventResponse(
835 version_->embedded_worker()->embedded_worker_id(), kFetchEventId, 922 version_->embedded_worker()->embedded_worker_id(), kFetchEventId,
836 ServiceWorkerResponse(), base::Time::Now())); 923 ServiceWorkerResponse(), base::Time::Now()));
837 924
838 base::RunLoop().RunUntilIdle(); 925 base::RunLoop().RunUntilIdle();
839 EXPECT_EQ(0, dispatcher_host_->bad_messages_received_count_); 926 EXPECT_EQ(0, dispatcher_host_->bad_messages_received_count_);
840 } 927 }
841 928
842 } // namespace content 929 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698