| 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 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 | 143 |
| 144 void SendSetHostedVersionId(int provider_id, int64_t version_id) { | 144 void SendSetHostedVersionId(int provider_id, int64_t version_id) { |
| 145 dispatcher_host_->OnMessageReceived( | 145 dispatcher_host_->OnMessageReceived( |
| 146 ServiceWorkerHostMsg_SetVersionId(provider_id, version_id)); | 146 ServiceWorkerHostMsg_SetVersionId(provider_id, version_id)); |
| 147 } | 147 } |
| 148 | 148 |
| 149 void SendProviderCreated(ServiceWorkerProviderType type, | 149 void SendProviderCreated(ServiceWorkerProviderType type, |
| 150 const GURL& pattern) { | 150 const GURL& pattern) { |
| 151 const int64_t kProviderId = 99; | 151 const int64_t kProviderId = 99; |
| 152 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_ProviderCreated( | 152 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_ProviderCreated( |
| 153 kProviderId, MSG_ROUTING_NONE, type)); | 153 kProviderId, MSG_ROUTING_NONE, type, |
| 154 true /* is_parent_frame_secure */)); |
| 154 helper_->SimulateAddProcessToPattern(pattern, | 155 helper_->SimulateAddProcessToPattern(pattern, |
| 155 helper_->mock_render_process_id()); | 156 helper_->mock_render_process_id()); |
| 156 provider_host_ = context()->GetProviderHost( | 157 provider_host_ = context()->GetProviderHost( |
| 157 helper_->mock_render_process_id(), kProviderId); | 158 helper_->mock_render_process_id(), kProviderId); |
| 158 } | 159 } |
| 159 | 160 |
| 160 void SendRegister(int64_t provider_id, GURL pattern, GURL worker_url) { | 161 void SendRegister(int64_t provider_id, GURL pattern, GURL worker_url) { |
| 161 dispatcher_host_->OnMessageReceived( | 162 dispatcher_host_->OnMessageReceived( |
| 162 ServiceWorkerHostMsg_RegisterServiceWorker( | 163 ServiceWorkerHostMsg_RegisterServiceWorker( |
| 163 -1, -1, provider_id, pattern, worker_url)); | 164 -1, -1, provider_id, pattern, worker_url)); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 ServiceWorkerProviderHost* sender_provider_host, | 228 ServiceWorkerProviderHost* sender_provider_host, |
| 228 const ServiceWorkerDispatcherHost::StatusCallback& callback) { | 229 const ServiceWorkerDispatcherHost::StatusCallback& callback) { |
| 229 dispatcher_host_->DispatchExtendableMessageEvent( | 230 dispatcher_host_->DispatchExtendableMessageEvent( |
| 230 std::move(worker), message, source_origin, sent_message_ports, | 231 std::move(worker), message, source_origin, sent_message_ports, |
| 231 sender_provider_host, callback); | 232 sender_provider_host, callback); |
| 232 } | 233 } |
| 233 | 234 |
| 234 ServiceWorkerProviderHost* CreateServiceWorkerProviderHost(int provider_id) { | 235 ServiceWorkerProviderHost* CreateServiceWorkerProviderHost(int provider_id) { |
| 235 return new ServiceWorkerProviderHost( | 236 return new ServiceWorkerProviderHost( |
| 236 helper_->mock_render_process_id(), kRenderFrameId, provider_id, | 237 helper_->mock_render_process_id(), kRenderFrameId, provider_id, |
| 237 SERVICE_WORKER_PROVIDER_FOR_WINDOW, context()->AsWeakPtr(), | 238 SERVICE_WORKER_PROVIDER_FOR_WINDOW, |
| 238 dispatcher_host_.get()); | 239 ServiceWorkerProviderHost::FrameSecurityLevel::SECURE, |
| 240 context()->AsWeakPtr(), dispatcher_host_.get()); |
| 239 } | 241 } |
| 240 | 242 |
| 241 TestBrowserThreadBundle browser_thread_bundle_; | 243 TestBrowserThreadBundle browser_thread_bundle_; |
| 242 content::MockResourceContext resource_context_; | 244 content::MockResourceContext resource_context_; |
| 243 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; | 245 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; |
| 244 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host_; | 246 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host_; |
| 245 scoped_refptr<ServiceWorkerRegistration> registration_; | 247 scoped_refptr<ServiceWorkerRegistration> registration_; |
| 246 scoped_refptr<ServiceWorkerVersion> version_; | 248 scoped_refptr<ServiceWorkerVersion> version_; |
| 247 ServiceWorkerProviderHost* provider_host_; | 249 ServiceWorkerProviderHost* provider_host_; |
| 248 }; | 250 }; |
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 489 GURL(), | 491 GURL(), |
| 490 GURL(), | 492 GURL(), |
| 491 ServiceWorkerMsg_ServiceWorkerRegistrationError::ID); | 493 ServiceWorkerMsg_ServiceWorkerRegistrationError::ID); |
| 492 } | 494 } |
| 493 | 495 |
| 494 TEST_F(ServiceWorkerDispatcherHostTest, ProviderCreatedAndDestroyed) { | 496 TEST_F(ServiceWorkerDispatcherHostTest, ProviderCreatedAndDestroyed) { |
| 495 const int kProviderId = 1001; | 497 const int kProviderId = 1001; |
| 496 int process_id = helper_->mock_render_process_id(); | 498 int process_id = helper_->mock_render_process_id(); |
| 497 | 499 |
| 498 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_ProviderCreated( | 500 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_ProviderCreated( |
| 499 kProviderId, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW)); | 501 kProviderId, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW, |
| 502 true /* is_parent_frame_secure */)); |
| 500 EXPECT_TRUE(context()->GetProviderHost(process_id, kProviderId)); | 503 EXPECT_TRUE(context()->GetProviderHost(process_id, kProviderId)); |
| 501 | 504 |
| 502 // Two with the same ID should be seen as a bad message. | 505 // Two with the same ID should be seen as a bad message. |
| 503 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_ProviderCreated( | 506 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_ProviderCreated( |
| 504 kProviderId, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW)); | 507 kProviderId, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW, |
| 508 true /* is_parent_frame_secure */)); |
| 505 EXPECT_EQ(1, dispatcher_host_->bad_messages_received_count_); | 509 EXPECT_EQ(1, dispatcher_host_->bad_messages_received_count_); |
| 506 | 510 |
| 507 dispatcher_host_->OnMessageReceived( | 511 dispatcher_host_->OnMessageReceived( |
| 508 ServiceWorkerHostMsg_ProviderDestroyed(kProviderId)); | 512 ServiceWorkerHostMsg_ProviderDestroyed(kProviderId)); |
| 509 EXPECT_FALSE(context()->GetProviderHost(process_id, kProviderId)); | 513 EXPECT_FALSE(context()->GetProviderHost(process_id, kProviderId)); |
| 510 | 514 |
| 511 // Destroying an ID that does not exist warrants a bad message. | 515 // Destroying an ID that does not exist warrants a bad message. |
| 512 dispatcher_host_->OnMessageReceived( | 516 dispatcher_host_->OnMessageReceived( |
| 513 ServiceWorkerHostMsg_ProviderDestroyed(kProviderId)); | 517 ServiceWorkerHostMsg_ProviderDestroyed(kProviderId)); |
| 514 EXPECT_EQ(2, dispatcher_host_->bad_messages_received_count_); | 518 EXPECT_EQ(2, dispatcher_host_->bad_messages_received_count_); |
| 515 | 519 |
| 516 // Deletion of the dispatcher_host should cause providers for that | 520 // Deletion of the dispatcher_host should cause providers for that |
| 517 // process to get deleted as well. | 521 // process to get deleted as well. |
| 518 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_ProviderCreated( | 522 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_ProviderCreated( |
| 519 kProviderId, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW)); | 523 kProviderId, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW, |
| 524 true /* is_parent_frame_secure */)); |
| 520 EXPECT_TRUE(context()->GetProviderHost(process_id, kProviderId)); | 525 EXPECT_TRUE(context()->GetProviderHost(process_id, kProviderId)); |
| 521 EXPECT_TRUE(dispatcher_host_->HasOneRef()); | 526 EXPECT_TRUE(dispatcher_host_->HasOneRef()); |
| 522 dispatcher_host_ = NULL; | 527 dispatcher_host_ = NULL; |
| 523 EXPECT_FALSE(context()->GetProviderHost(process_id, kProviderId)); | 528 EXPECT_FALSE(context()->GetProviderHost(process_id, kProviderId)); |
| 524 } | 529 } |
| 525 | 530 |
| 526 TEST_F(ServiceWorkerDispatcherHostTest, GetRegistration_SameOrigin) { | 531 TEST_F(ServiceWorkerDispatcherHostTest, GetRegistration_SameOrigin) { |
| 527 const int64_t kProviderId = 99; // Dummy value | 532 const int64_t kProviderId = 99; // Dummy value |
| 528 std::unique_ptr<ServiceWorkerProviderHost> host( | 533 std::unique_ptr<ServiceWorkerProviderHost> host( |
| 529 CreateServiceWorkerProviderHost(kProviderId)); | 534 CreateServiceWorkerProviderHost(kProviderId)); |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 645 // is not yet destroyed. This is what the browser does when reusing a crashed | 650 // is not yet destroyed. This is what the browser does when reusing a crashed |
| 646 // render process. | 651 // render process. |
| 647 scoped_refptr<TestingServiceWorkerDispatcherHost> new_dispatcher_host( | 652 scoped_refptr<TestingServiceWorkerDispatcherHost> new_dispatcher_host( |
| 648 new TestingServiceWorkerDispatcherHost( | 653 new TestingServiceWorkerDispatcherHost( |
| 649 process_id, context_wrapper(), &resource_context_, helper_.get())); | 654 process_id, context_wrapper(), &resource_context_, helper_.get())); |
| 650 | 655 |
| 651 // To show the new dispatcher can operate, simulate provider creation. Since | 656 // To show the new dispatcher can operate, simulate provider creation. Since |
| 652 // the old dispatcher cleaned up the old provider host, the new one won't | 657 // the old dispatcher cleaned up the old provider host, the new one won't |
| 653 // complain. | 658 // complain. |
| 654 new_dispatcher_host->OnMessageReceived(ServiceWorkerHostMsg_ProviderCreated( | 659 new_dispatcher_host->OnMessageReceived(ServiceWorkerHostMsg_ProviderCreated( |
| 655 provider_id, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW)); | 660 provider_id, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW, |
| 661 true /* is_parent_frame_secure */)); |
| 656 EXPECT_EQ(0, new_dispatcher_host->bad_messages_received_count_); | 662 EXPECT_EQ(0, new_dispatcher_host->bad_messages_received_count_); |
| 657 } | 663 } |
| 658 | 664 |
| 659 TEST_F(ServiceWorkerDispatcherHostTest, DispatchExtendableMessageEvent) { | 665 TEST_F(ServiceWorkerDispatcherHostTest, DispatchExtendableMessageEvent) { |
| 660 GURL pattern = GURL("http://www.example.com/"); | 666 GURL pattern = GURL("http://www.example.com/"); |
| 661 GURL script_url = GURL("http://www.example.com/service_worker.js"); | 667 GURL script_url = GURL("http://www.example.com/service_worker.js"); |
| 662 | 668 |
| 663 SendProviderCreated(SERVICE_WORKER_PROVIDER_FOR_CONTROLLER, pattern); | 669 SendProviderCreated(SERVICE_WORKER_PROVIDER_FOR_CONTROLLER, pattern); |
| 664 SetUpRegistration(pattern, script_url); | 670 SetUpRegistration(pattern, script_url); |
| 665 | 671 |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 756 provider_host_->process_id()); | 762 provider_host_->process_id()); |
| 757 // SendSetHostedVersionId should reject because the provider host process id | 763 // SendSetHostedVersionId should reject because the provider host process id |
| 758 // is different. | 764 // is different. |
| 759 SendSetHostedVersionId(kProviderId, version_->version_id()); | 765 SendSetHostedVersionId(kProviderId, version_->version_id()); |
| 760 base::RunLoop().RunUntilIdle(); | 766 base::RunLoop().RunUntilIdle(); |
| 761 EXPECT_FALSE(dispatcher_host_->ipc_sink()->GetUniqueMessageMatching( | 767 EXPECT_FALSE(dispatcher_host_->ipc_sink()->GetUniqueMessageMatching( |
| 762 ServiceWorkerMsg_AssociateRegistration::ID)); | 768 ServiceWorkerMsg_AssociateRegistration::ID)); |
| 763 } | 769 } |
| 764 | 770 |
| 765 } // namespace content | 771 } // namespace content |
| OLD | NEW |