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 |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 registration_.get(), version_.get(), | 149 registration_.get(), version_.get(), |
150 base::Bind(&SaveStatusCallback, &called, &status)); | 150 base::Bind(&SaveStatusCallback, &called, &status)); |
151 base::RunLoop().RunUntilIdle(); | 151 base::RunLoop().RunUntilIdle(); |
152 EXPECT_TRUE(called); | 152 EXPECT_TRUE(called); |
153 EXPECT_EQ(SERVICE_WORKER_OK, status); | 153 EXPECT_EQ(SERVICE_WORKER_OK, status); |
154 } | 154 } |
155 | 155 |
156 void SendSetHostedVersionId(int provider_id, | 156 void SendSetHostedVersionId(int provider_id, |
157 int64_t version_id, | 157 int64_t version_id, |
158 int embedded_worker_id) { | 158 int embedded_worker_id) { |
159 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_SetVersionId( | 159 dispatcher_host_->OnSetHostedVersionId(provider_id, version_id, |
160 provider_id, version_id, embedded_worker_id)); | 160 embedded_worker_id); |
161 } | 161 } |
162 | 162 |
163 void SendProviderCreated(ServiceWorkerProviderType type, | 163 void SendProviderCreated(ServiceWorkerProviderType type, |
164 const GURL& pattern) { | 164 const GURL& pattern) { |
165 const int64_t kProviderId = 99; | 165 const int64_t kProviderId = 99; |
166 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_ProviderCreated( | 166 ServiceWorkerProviderHostInfo info(kProviderId, MSG_ROUTING_NONE, type, |
167 kProviderId, MSG_ROUTING_NONE, type, | 167 true); |
168 true /* is_parent_frame_secure */)); | 168 dispatcher_host_->OnProviderCreated(std::move(info)); |
169 helper_->SimulateAddProcessToPattern(pattern, | 169 helper_->SimulateAddProcessToPattern(pattern, |
170 helper_->mock_render_process_id()); | 170 helper_->mock_render_process_id()); |
171 provider_host_ = context()->GetProviderHost( | 171 provider_host_ = context()->GetProviderHost( |
172 helper_->mock_render_process_id(), kProviderId); | 172 helper_->mock_render_process_id(), kProviderId); |
173 } | 173 } |
174 | 174 |
175 void SendRegister(int64_t provider_id, GURL pattern, GURL worker_url) { | 175 void SendRegister(int64_t provider_id, GURL pattern, GURL worker_url) { |
176 dispatcher_host_->OnMessageReceived( | 176 dispatcher_host_->OnMessageReceived( |
177 ServiceWorkerHostMsg_RegisterServiceWorker( | 177 ServiceWorkerHostMsg_RegisterServiceWorker( |
178 -1, -1, provider_id, pattern, worker_url)); | 178 -1, -1, provider_id, pattern, worker_url)); |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 const base::string16& message, | 239 const base::string16& message, |
240 const url::Origin& source_origin, | 240 const url::Origin& source_origin, |
241 const std::vector<int>& sent_message_ports, | 241 const std::vector<int>& sent_message_ports, |
242 ServiceWorkerProviderHost* sender_provider_host, | 242 ServiceWorkerProviderHost* sender_provider_host, |
243 const ServiceWorkerDispatcherHost::StatusCallback& callback) { | 243 const ServiceWorkerDispatcherHost::StatusCallback& callback) { |
244 dispatcher_host_->DispatchExtendableMessageEvent( | 244 dispatcher_host_->DispatchExtendableMessageEvent( |
245 std::move(worker), message, source_origin, sent_message_ports, | 245 std::move(worker), message, source_origin, sent_message_ports, |
246 sender_provider_host, callback); | 246 sender_provider_host, callback); |
247 } | 247 } |
248 | 248 |
249 ServiceWorkerProviderHost* CreateServiceWorkerProviderHost(int provider_id) { | 249 std::unique_ptr<ServiceWorkerProviderHost> CreateServiceWorkerProviderHost( |
250 return new ServiceWorkerProviderHost( | 250 int provider_id) { |
251 helper_->mock_render_process_id(), kRenderFrameId, provider_id, | 251 std::unique_ptr<ServiceWorkerProviderHost> host = |
252 SERVICE_WORKER_PROVIDER_FOR_WINDOW, | 252 ServiceWorkerProviderHost::CreateForTesting( |
253 ServiceWorkerProviderHost::FrameSecurityLevel::SECURE, | 253 helper_->mock_render_process_id(), provider_id, |
254 context()->AsWeakPtr(), dispatcher_host_.get()); | 254 SERVICE_WORKER_PROVIDER_FOR_WINDOW, context()->AsWeakPtr(), |
| 255 kRenderFrameId, dispatcher_host_.get()); |
| 256 host->set_parent_frame_secure(true); |
| 257 return host; |
255 } | 258 } |
256 | 259 |
257 TestBrowserThreadBundle browser_thread_bundle_; | 260 TestBrowserThreadBundle browser_thread_bundle_; |
258 content::MockResourceContext resource_context_; | 261 content::MockResourceContext resource_context_; |
259 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; | 262 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; |
260 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host_; | 263 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host_; |
261 scoped_refptr<ServiceWorkerRegistration> registration_; | 264 scoped_refptr<ServiceWorkerRegistration> registration_; |
262 scoped_refptr<ServiceWorkerVersion> version_; | 265 scoped_refptr<ServiceWorkerVersion> version_; |
263 ServiceWorkerProviderHost* provider_host_; | 266 ServiceWorkerProviderHost* provider_host_; |
264 }; | 267 }; |
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
503 Register(-1, | 506 Register(-1, |
504 GURL(), | 507 GURL(), |
505 GURL(), | 508 GURL(), |
506 ServiceWorkerMsg_ServiceWorkerRegistrationError::ID); | 509 ServiceWorkerMsg_ServiceWorkerRegistrationError::ID); |
507 } | 510 } |
508 | 511 |
509 TEST_F(ServiceWorkerDispatcherHostTest, ProviderCreatedAndDestroyed) { | 512 TEST_F(ServiceWorkerDispatcherHostTest, ProviderCreatedAndDestroyed) { |
510 const int kProviderId = 1001; | 513 const int kProviderId = 1001; |
511 int process_id = helper_->mock_render_process_id(); | 514 int process_id = helper_->mock_render_process_id(); |
512 | 515 |
513 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_ProviderCreated( | 516 dispatcher_host_->OnProviderCreated(ServiceWorkerProviderHostInfo( |
514 kProviderId, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW, | 517 kProviderId, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW, |
515 true /* is_parent_frame_secure */)); | 518 true /* is_parent_frame_secure */)); |
516 EXPECT_TRUE(context()->GetProviderHost(process_id, kProviderId)); | 519 EXPECT_TRUE(context()->GetProviderHost(process_id, kProviderId)); |
517 | 520 |
518 // Two with the same ID should be seen as a bad message. | 521 // Two with the same ID should be seen as a bad message. |
519 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_ProviderCreated( | 522 dispatcher_host_->OnProviderCreated(ServiceWorkerProviderHostInfo( |
520 kProviderId, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW, | 523 kProviderId, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW, |
521 true /* is_parent_frame_secure */)); | 524 true /* is_parent_frame_secure */)); |
522 EXPECT_EQ(1, dispatcher_host_->bad_messages_received_count_); | 525 EXPECT_EQ(1, dispatcher_host_->bad_messages_received_count_); |
523 | 526 |
524 dispatcher_host_->OnMessageReceived( | 527 dispatcher_host_->OnProviderDestroyed(kProviderId); |
525 ServiceWorkerHostMsg_ProviderDestroyed(kProviderId)); | |
526 EXPECT_FALSE(context()->GetProviderHost(process_id, kProviderId)); | 528 EXPECT_FALSE(context()->GetProviderHost(process_id, kProviderId)); |
527 | 529 |
528 // Destroying an ID that does not exist warrants a bad message. | 530 // Destroying an ID that does not exist warrants a bad message. |
529 dispatcher_host_->OnMessageReceived( | 531 dispatcher_host_->OnProviderDestroyed(kProviderId); |
530 ServiceWorkerHostMsg_ProviderDestroyed(kProviderId)); | |
531 EXPECT_EQ(2, dispatcher_host_->bad_messages_received_count_); | 532 EXPECT_EQ(2, dispatcher_host_->bad_messages_received_count_); |
532 | 533 |
533 // Deletion of the dispatcher_host should cause providers for that | 534 // Deletion of the dispatcher_host should cause providers for that |
534 // process to get deleted as well. | 535 // process to get deleted as well. |
535 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_ProviderCreated( | 536 dispatcher_host_->OnProviderCreated(ServiceWorkerProviderHostInfo( |
536 kProviderId, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW, | 537 kProviderId, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW, |
537 true /* is_parent_frame_secure */)); | 538 true /* is_parent_frame_secure */)); |
538 EXPECT_TRUE(context()->GetProviderHost(process_id, kProviderId)); | 539 EXPECT_TRUE(context()->GetProviderHost(process_id, kProviderId)); |
539 EXPECT_TRUE(dispatcher_host_->HasOneRef()); | 540 EXPECT_TRUE(dispatcher_host_->HasOneRef()); |
540 dispatcher_host_ = NULL; | 541 dispatcher_host_ = nullptr; |
541 EXPECT_FALSE(context()->GetProviderHost(process_id, kProviderId)); | 542 EXPECT_FALSE(context()->GetProviderHost(process_id, kProviderId)); |
542 } | 543 } |
543 | 544 |
544 TEST_F(ServiceWorkerDispatcherHostTest, GetRegistration_SameOrigin) { | 545 TEST_F(ServiceWorkerDispatcherHostTest, GetRegistration_SameOrigin) { |
545 const int64_t kProviderId = 99; // Dummy value | 546 const int64_t kProviderId = 99; // Dummy value |
546 std::unique_ptr<ServiceWorkerProviderHost> host( | 547 std::unique_ptr<ServiceWorkerProviderHost> host( |
547 CreateServiceWorkerProviderHost(kProviderId)); | 548 CreateServiceWorkerProviderHost(kProviderId)); |
548 host->SetDocumentUrl(GURL("https://www.example.com/foo")); | 549 host->SetDocumentUrl(GURL("https://www.example.com/foo")); |
549 context()->AddProviderHost(std::move(host)); | 550 context()->AddProviderHost(std::move(host)); |
550 | 551 |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
662 // We should be able to hook up a new dispatcher host although the old object | 663 // 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 | 664 // is not yet destroyed. This is what the browser does when reusing a crashed |
664 // render process. | 665 // render process. |
665 scoped_refptr<TestingServiceWorkerDispatcherHost> new_dispatcher_host( | 666 scoped_refptr<TestingServiceWorkerDispatcherHost> new_dispatcher_host( |
666 new TestingServiceWorkerDispatcherHost( | 667 new TestingServiceWorkerDispatcherHost( |
667 process_id, context_wrapper(), &resource_context_, helper_.get())); | 668 process_id, context_wrapper(), &resource_context_, helper_.get())); |
668 | 669 |
669 // To show the new dispatcher can operate, simulate provider creation. Since | 670 // 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 | 671 // the old dispatcher cleaned up the old provider host, the new one won't |
671 // complain. | 672 // complain. |
672 new_dispatcher_host->OnMessageReceived(ServiceWorkerHostMsg_ProviderCreated( | 673 new_dispatcher_host->OnProviderCreated(ServiceWorkerProviderHostInfo( |
673 provider_id, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW, | 674 provider_id, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW, |
674 true /* is_parent_frame_secure */)); | 675 true /* is_parent_frame_secure */)); |
675 EXPECT_EQ(0, new_dispatcher_host->bad_messages_received_count_); | 676 EXPECT_EQ(0, new_dispatcher_host->bad_messages_received_count_); |
676 } | 677 } |
677 | 678 |
678 TEST_F(ServiceWorkerDispatcherHostTest, DispatchExtendableMessageEvent) { | 679 TEST_F(ServiceWorkerDispatcherHostTest, DispatchExtendableMessageEvent) { |
679 GURL pattern = GURL("http://www.example.com/"); | 680 GURL pattern = GURL("http://www.example.com/"); |
680 GURL script_url = GURL("http://www.example.com/service_worker.js"); | 681 GURL script_url = GURL("http://www.example.com/service_worker.js"); |
681 | 682 |
682 SendProviderCreated(SERVICE_WORKER_PROVIDER_FOR_CONTROLLER, pattern); | 683 SendProviderCreated(SERVICE_WORKER_PROVIDER_FOR_CONTROLLER, pattern); |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
846 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_FetchEventResponse( | 847 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_FetchEventResponse( |
847 version_->embedded_worker()->embedded_worker_id(), kFetchEventId, | 848 version_->embedded_worker()->embedded_worker_id(), kFetchEventId, |
848 SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK, ServiceWorkerResponse(), | 849 SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK, ServiceWorkerResponse(), |
849 base::Time::Now())); | 850 base::Time::Now())); |
850 | 851 |
851 base::RunLoop().RunUntilIdle(); | 852 base::RunLoop().RunUntilIdle(); |
852 EXPECT_EQ(0, dispatcher_host_->bad_messages_received_count_); | 853 EXPECT_EQ(0, dispatcher_host_->bad_messages_received_count_); |
853 } | 854 } |
854 | 855 |
855 } // namespace content | 856 } // namespace content |
OLD | NEW |