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

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

Issue 2638313002: Manage ServiceWorkerDispatcherHost in ServiceWorkerContextCore (Closed)
Patch Set: Rebased 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
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 54
55 } // namespace 55 } // namespace
56 56
57 static const int kRenderFrameId = 1; 57 static const int kRenderFrameId = 1;
58 58
59 class TestingServiceWorkerDispatcherHost : public ServiceWorkerDispatcherHost { 59 class TestingServiceWorkerDispatcherHost : public ServiceWorkerDispatcherHost {
60 public: 60 public:
61 TestingServiceWorkerDispatcherHost( 61 TestingServiceWorkerDispatcherHost(
62 int process_id, 62 int process_id,
63 ServiceWorkerContextWrapper* context_wrapper, 63 ServiceWorkerContextWrapper* context_wrapper,
64 MessagePortMessageFilter* message_port_message_filter,
64 ResourceContext* resource_context, 65 ResourceContext* resource_context,
65 EmbeddedWorkerTestHelper* helper) 66 EmbeddedWorkerTestHelper* helper)
66 : ServiceWorkerDispatcherHost(process_id, nullptr, resource_context), 67 : ServiceWorkerDispatcherHost(process_id,
68 message_port_message_filter,
69 resource_context),
67 bad_messages_received_count_(0), 70 bad_messages_received_count_(0),
68 helper_(helper) { 71 helper_(helper) {
69 Init(context_wrapper); 72 Init(context_wrapper);
70 } 73 }
71 74
72 bool Send(IPC::Message* message) override { return helper_->Send(message); } 75 bool Send(IPC::Message* message) override { return helper_->Send(message); }
73 76
74 IPC::TestSink* ipc_sink() { return helper_->ipc_sink(); } 77 IPC::TestSink* ipc_sink() { return helper_->ipc_sink(); }
75 78
76 void ShutdownForBadMessage() override { ++bad_messages_received_count_; } 79 void ShutdownForBadMessage() override { ++bad_messages_received_count_; }
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 helper_.reset(); 116 helper_.reset();
114 } 117 }
115 118
116 ServiceWorkerContextCore* context() { return helper_->context(); } 119 ServiceWorkerContextCore* context() { return helper_->context(); }
117 ServiceWorkerContextWrapper* context_wrapper() { 120 ServiceWorkerContextWrapper* context_wrapper() {
118 return helper_->context_wrapper(); 121 return helper_->context_wrapper();
119 } 122 }
120 123
121 void Initialize(std::unique_ptr<EmbeddedWorkerTestHelper> helper) { 124 void Initialize(std::unique_ptr<EmbeddedWorkerTestHelper> helper) {
122 helper_.reset(helper.release()); 125 helper_.reset(helper.release());
126 int process_id = helper_->mock_render_process_id();
127 // Inherit the message filter for message ports.
128 MessagePortMessageFilter* message_port_filter =
129 context()->GetDispatcherHost(process_id)->message_port_message_filter();
130 context()->RemoveDispatcherHost(process_id);
123 dispatcher_host_ = new TestingServiceWorkerDispatcherHost( 131 dispatcher_host_ = new TestingServiceWorkerDispatcherHost(
124 helper_->mock_render_process_id(), context_wrapper(), 132 process_id, context_wrapper(), message_port_filter, &resource_context_,
125 &resource_context_, helper_.get()); 133 helper_.get());
126 } 134 }
127 135
128 void SetUpRegistration(const GURL& scope, const GURL& script_url) { 136 void SetUpRegistration(const GURL& scope, const GURL& script_url) {
129 registration_ = new ServiceWorkerRegistration( 137 registration_ = new ServiceWorkerRegistration(
130 scope, 1L, helper_->context()->AsWeakPtr()); 138 scope, 1L, helper_->context()->AsWeakPtr());
131 version_ = new ServiceWorkerVersion(registration_.get(), script_url, 1L, 139 version_ = new ServiceWorkerVersion(registration_.get(), script_url, 1L,
132 helper_->context()->AsWeakPtr()); 140 helper_->context()->AsWeakPtr());
133 std::vector<ServiceWorkerDatabase::ResourceRecord> records; 141 std::vector<ServiceWorkerDatabase::ResourceRecord> records;
134 records.push_back( 142 records.push_back(
135 ServiceWorkerDatabase::ResourceRecord(10, version_->script_url(), 100)); 143 ServiceWorkerDatabase::ResourceRecord(10, version_->script_url(), 100));
(...skipping 13 matching lines...) Expand all
149 registration_.get(), version_.get(), 157 registration_.get(), version_.get(),
150 base::Bind(&SaveStatusCallback, &called, &status)); 158 base::Bind(&SaveStatusCallback, &called, &status));
151 base::RunLoop().RunUntilIdle(); 159 base::RunLoop().RunUntilIdle();
152 EXPECT_TRUE(called); 160 EXPECT_TRUE(called);
153 EXPECT_EQ(SERVICE_WORKER_OK, status); 161 EXPECT_EQ(SERVICE_WORKER_OK, status);
154 } 162 }
155 163
156 void SendSetHostedVersionId(int provider_id, 164 void SendSetHostedVersionId(int provider_id,
157 int64_t version_id, 165 int64_t version_id,
158 int embedded_worker_id) { 166 int embedded_worker_id) {
159 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_SetVersionId( 167 dispatcher_host_->OnSetHostedVersionId(provider_id, version_id,
160 provider_id, version_id, embedded_worker_id)); 168 embedded_worker_id);
161 } 169 }
162 170
163 void SendProviderCreated(ServiceWorkerProviderType type, 171 void SendProviderCreated(ServiceWorkerProviderType type,
164 const GURL& pattern) { 172 const GURL& pattern) {
165 const int64_t kProviderId = 99; 173 const int64_t kProviderId = 99;
166 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_ProviderCreated( 174 ServiceWorkerProviderHostInfo info(kProviderId, MSG_ROUTING_NONE, type,
167 kProviderId, MSG_ROUTING_NONE, type, 175 true);
falken 2017/02/13 05:59:24 Let's document /* is_parent_frame_secure */ or rei
shimazu 2017/02/13 08:20:19 Oops, I forgot to add the note, thanks:)
168 true /* is_parent_frame_secure */)); 176 dispatcher_host_->OnProviderCreated(std::move(info));
169 helper_->SimulateAddProcessToPattern(pattern, 177 helper_->SimulateAddProcessToPattern(pattern,
170 helper_->mock_render_process_id()); 178 helper_->mock_render_process_id());
171 provider_host_ = context()->GetProviderHost( 179 provider_host_ = context()->GetProviderHost(
172 helper_->mock_render_process_id(), kProviderId); 180 helper_->mock_render_process_id(), kProviderId);
173 } 181 }
174 182
175 void SendRegister(int64_t provider_id, GURL pattern, GURL worker_url) { 183 void SendRegister(int64_t provider_id, GURL pattern, GURL worker_url) {
176 dispatcher_host_->OnMessageReceived( 184 dispatcher_host_->OnMessageReceived(
177 ServiceWorkerHostMsg_RegisterServiceWorker( 185 ServiceWorkerHostMsg_RegisterServiceWorker(
178 -1, -1, provider_id, pattern, worker_url)); 186 -1, -1, provider_id, pattern, worker_url));
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 const base::string16& message, 247 const base::string16& message,
240 const url::Origin& source_origin, 248 const url::Origin& source_origin,
241 const std::vector<int>& sent_message_ports, 249 const std::vector<int>& sent_message_ports,
242 ServiceWorkerProviderHost* sender_provider_host, 250 ServiceWorkerProviderHost* sender_provider_host,
243 const ServiceWorkerDispatcherHost::StatusCallback& callback) { 251 const ServiceWorkerDispatcherHost::StatusCallback& callback) {
244 dispatcher_host_->DispatchExtendableMessageEvent( 252 dispatcher_host_->DispatchExtendableMessageEvent(
245 std::move(worker), message, source_origin, sent_message_ports, 253 std::move(worker), message, source_origin, sent_message_ports,
246 sender_provider_host, callback); 254 sender_provider_host, callback);
247 } 255 }
248 256
249 ServiceWorkerProviderHost* CreateServiceWorkerProviderHost(int provider_id) { 257 std::unique_ptr<ServiceWorkerProviderHost> CreateServiceWorkerProviderHost(
250 return new ServiceWorkerProviderHost( 258 int provider_id) {
251 helper_->mock_render_process_id(), kRenderFrameId, provider_id, 259 return CreateProviderHostWithDispatcherHost(
252 SERVICE_WORKER_PROVIDER_FOR_WINDOW, 260 helper_->mock_render_process_id(), provider_id, context()->AsWeakPtr(),
253 ServiceWorkerProviderHost::FrameSecurityLevel::SECURE, 261 kRenderFrameId, dispatcher_host_.get());
254 context()->AsWeakPtr(), dispatcher_host_.get());
255 } 262 }
256 263
257 TestBrowserThreadBundle browser_thread_bundle_; 264 TestBrowserThreadBundle browser_thread_bundle_;
258 content::MockResourceContext resource_context_; 265 content::MockResourceContext resource_context_;
259 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; 266 std::unique_ptr<EmbeddedWorkerTestHelper> helper_;
260 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host_; 267 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host_;
261 scoped_refptr<ServiceWorkerRegistration> registration_; 268 scoped_refptr<ServiceWorkerRegistration> registration_;
262 scoped_refptr<ServiceWorkerVersion> version_; 269 scoped_refptr<ServiceWorkerVersion> version_;
263 ServiceWorkerProviderHost* provider_host_; 270 ServiceWorkerProviderHost* provider_host_;
264 }; 271 };
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after
503 Register(-1, 510 Register(-1,
504 GURL(), 511 GURL(),
505 GURL(), 512 GURL(),
506 ServiceWorkerMsg_ServiceWorkerRegistrationError::ID); 513 ServiceWorkerMsg_ServiceWorkerRegistrationError::ID);
507 } 514 }
508 515
509 TEST_F(ServiceWorkerDispatcherHostTest, ProviderCreatedAndDestroyed) { 516 TEST_F(ServiceWorkerDispatcherHostTest, ProviderCreatedAndDestroyed) {
510 const int kProviderId = 1001; 517 const int kProviderId = 1001;
511 int process_id = helper_->mock_render_process_id(); 518 int process_id = helper_->mock_render_process_id();
512 519
513 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_ProviderCreated( 520 dispatcher_host_->OnProviderCreated(ServiceWorkerProviderHostInfo(
514 kProviderId, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW, 521 kProviderId, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW,
515 true /* is_parent_frame_secure */)); 522 true /* is_parent_frame_secure */));
516 EXPECT_TRUE(context()->GetProviderHost(process_id, kProviderId)); 523 EXPECT_TRUE(context()->GetProviderHost(process_id, kProviderId));
517 524
518 // Two with the same ID should be seen as a bad message. 525 // Two with the same ID should be seen as a bad message.
519 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_ProviderCreated( 526 dispatcher_host_->OnProviderCreated(ServiceWorkerProviderHostInfo(
520 kProviderId, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW, 527 kProviderId, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW,
521 true /* is_parent_frame_secure */)); 528 true /* is_parent_frame_secure */));
522 EXPECT_EQ(1, dispatcher_host_->bad_messages_received_count_); 529 EXPECT_EQ(1, dispatcher_host_->bad_messages_received_count_);
523 530
524 dispatcher_host_->OnMessageReceived( 531 dispatcher_host_->OnProviderDestroyed(kProviderId);
525 ServiceWorkerHostMsg_ProviderDestroyed(kProviderId));
526 EXPECT_FALSE(context()->GetProviderHost(process_id, kProviderId)); 532 EXPECT_FALSE(context()->GetProviderHost(process_id, kProviderId));
527 533
528 // Destroying an ID that does not exist warrants a bad message. 534 // Destroying an ID that does not exist warrants a bad message.
529 dispatcher_host_->OnMessageReceived( 535 dispatcher_host_->OnProviderDestroyed(kProviderId);
530 ServiceWorkerHostMsg_ProviderDestroyed(kProviderId));
531 EXPECT_EQ(2, dispatcher_host_->bad_messages_received_count_); 536 EXPECT_EQ(2, dispatcher_host_->bad_messages_received_count_);
532 537
533 // Deletion of the dispatcher_host should cause providers for that 538 // Deletion of the dispatcher_host should cause providers for that
534 // process to get deleted as well. 539 // process to get deleted as well.
535 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_ProviderCreated( 540 dispatcher_host_->OnProviderCreated(ServiceWorkerProviderHostInfo(
536 kProviderId, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW, 541 kProviderId, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW,
537 true /* is_parent_frame_secure */)); 542 true /* is_parent_frame_secure */));
538 EXPECT_TRUE(context()->GetProviderHost(process_id, kProviderId)); 543 EXPECT_TRUE(context()->GetProviderHost(process_id, kProviderId));
539 EXPECT_TRUE(dispatcher_host_->HasOneRef()); 544 EXPECT_TRUE(dispatcher_host_->HasOneRef());
540 dispatcher_host_ = NULL; 545 dispatcher_host_ = nullptr;
541 EXPECT_FALSE(context()->GetProviderHost(process_id, kProviderId)); 546 EXPECT_FALSE(context()->GetProviderHost(process_id, kProviderId));
542 } 547 }
543 548
544 TEST_F(ServiceWorkerDispatcherHostTest, GetRegistration_SameOrigin) { 549 TEST_F(ServiceWorkerDispatcherHostTest, GetRegistration_SameOrigin) {
545 const int64_t kProviderId = 99; // Dummy value 550 const int64_t kProviderId = 99; // Dummy value
546 std::unique_ptr<ServiceWorkerProviderHost> host( 551 std::unique_ptr<ServiceWorkerProviderHost> host(
547 CreateServiceWorkerProviderHost(kProviderId)); 552 CreateServiceWorkerProviderHost(kProviderId));
548 host->SetDocumentUrl(GURL("https://www.example.com/foo")); 553 host->SetDocumentUrl(GURL("https://www.example.com/foo"));
549 context()->AddProviderHost(std::move(host)); 554 context()->AddProviderHost(std::move(host));
550 555
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
656 dispatcher_host_->OnFilterRemoved(); 661 dispatcher_host_->OnFilterRemoved();
657 662
658 // The dispatcher host should clean up the state from the process. 663 // The dispatcher host should clean up the state from the process.
659 EXPECT_FALSE(context()->GetProviderHost(process_id, provider_id)); 664 EXPECT_FALSE(context()->GetProviderHost(process_id, provider_id));
660 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, version_->running_status()); 665 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, version_->running_status());
661 666
662 // We should be able to hook up a new dispatcher host although the old object 667 // 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 668 // is not yet destroyed. This is what the browser does when reusing a crashed
664 // render process. 669 // render process.
665 scoped_refptr<TestingServiceWorkerDispatcherHost> new_dispatcher_host( 670 scoped_refptr<TestingServiceWorkerDispatcherHost> new_dispatcher_host(
666 new TestingServiceWorkerDispatcherHost( 671 new TestingServiceWorkerDispatcherHost(process_id, context_wrapper(),
667 process_id, context_wrapper(), &resource_context_, helper_.get())); 672 nullptr, &resource_context_,
673 helper_.get()));
668 674
669 // To show the new dispatcher can operate, simulate provider creation. Since 675 // 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 676 // the old dispatcher cleaned up the old provider host, the new one won't
671 // complain. 677 // complain.
672 new_dispatcher_host->OnMessageReceived(ServiceWorkerHostMsg_ProviderCreated( 678 new_dispatcher_host->OnProviderCreated(ServiceWorkerProviderHostInfo(
673 provider_id, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW, 679 provider_id, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW,
674 true /* is_parent_frame_secure */)); 680 true /* is_parent_frame_secure */));
675 EXPECT_EQ(0, new_dispatcher_host->bad_messages_received_count_); 681 EXPECT_EQ(0, new_dispatcher_host->bad_messages_received_count_);
676 } 682 }
677 683
678 TEST_F(ServiceWorkerDispatcherHostTest, DispatchExtendableMessageEvent) { 684 TEST_F(ServiceWorkerDispatcherHostTest, DispatchExtendableMessageEvent) {
679 GURL pattern = GURL("http://www.example.com/"); 685 GURL pattern = GURL("http://www.example.com/");
680 GURL script_url = GURL("http://www.example.com/service_worker.js"); 686 GURL script_url = GURL("http://www.example.com/service_worker.js");
681 687
682 SendProviderCreated(SERVICE_WORKER_PROVIDER_FOR_CONTROLLER, pattern); 688 SendProviderCreated(SERVICE_WORKER_PROVIDER_FOR_CONTROLLER, pattern);
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
846 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_FetchEventResponse( 852 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_FetchEventResponse(
847 version_->embedded_worker()->embedded_worker_id(), kFetchEventId, 853 version_->embedded_worker()->embedded_worker_id(), kFetchEventId,
848 SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK, ServiceWorkerResponse(), 854 SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK, ServiceWorkerResponse(),
849 base::Time::Now())); 855 base::Time::Now()));
850 856
851 base::RunLoop().RunUntilIdle(); 857 base::RunLoop().RunUntilIdle();
852 EXPECT_EQ(0, dispatcher_host_->bad_messages_received_count_); 858 EXPECT_EQ(0, dispatcher_host_->bad_messages_received_count_);
853 } 859 }
854 860
855 } // namespace content 861 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698