| 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 21 matching lines...) Expand all Loading... |
| 32 | 32 |
| 33 namespace { | 33 namespace { |
| 34 | 34 |
| 35 static void SaveStatusCallback(bool* called, | 35 static void SaveStatusCallback(bool* called, |
| 36 ServiceWorkerStatusCode* out, | 36 ServiceWorkerStatusCode* out, |
| 37 ServiceWorkerStatusCode status) { | 37 ServiceWorkerStatusCode status) { |
| 38 *called = true; | 38 *called = true; |
| 39 *out = status; | 39 *out = status; |
| 40 } | 40 } |
| 41 | 41 |
| 42 void SetUpDummyMessagePort(std::vector<TransferredMessagePort>* ports) { | 42 void SetUpDummyMessagePort(std::vector<int>* ports) { |
| 43 int port_id = -1; | 43 int port_id = -1; |
| 44 MessagePortService::GetInstance()->Create(MSG_ROUTING_NONE, nullptr, | 44 MessagePortService::GetInstance()->Create(MSG_ROUTING_NONE, nullptr, |
| 45 &port_id); | 45 &port_id); |
| 46 TransferredMessagePort dummy_port; | 46 ports->push_back(port_id); |
| 47 dummy_port.id = port_id; | |
| 48 ports->push_back(dummy_port); | |
| 49 } | 47 } |
| 50 | 48 |
| 51 } // namespace | 49 } // namespace |
| 52 | 50 |
| 53 static const int kRenderFrameId = 1; | 51 static const int kRenderFrameId = 1; |
| 54 | 52 |
| 55 class TestingServiceWorkerDispatcherHost : public ServiceWorkerDispatcherHost { | 53 class TestingServiceWorkerDispatcherHost : public ServiceWorkerDispatcherHost { |
| 56 public: | 54 public: |
| 57 TestingServiceWorkerDispatcherHost( | 55 TestingServiceWorkerDispatcherHost( |
| 58 int process_id, | 56 int process_id, |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 SendGetRegistrations(provider_id); | 216 SendGetRegistrations(provider_id); |
| 219 EXPECT_TRUE(dispatcher_host_->ipc_sink()->GetUniqueMessageMatching( | 217 EXPECT_TRUE(dispatcher_host_->ipc_sink()->GetUniqueMessageMatching( |
| 220 expected_message)); | 218 expected_message)); |
| 221 dispatcher_host_->ipc_sink()->ClearMessages(); | 219 dispatcher_host_->ipc_sink()->ClearMessages(); |
| 222 } | 220 } |
| 223 | 221 |
| 224 void DispatchExtendableMessageEvent( | 222 void DispatchExtendableMessageEvent( |
| 225 scoped_refptr<ServiceWorkerVersion> worker, | 223 scoped_refptr<ServiceWorkerVersion> worker, |
| 226 const base::string16& message, | 224 const base::string16& message, |
| 227 const url::Origin& source_origin, | 225 const url::Origin& source_origin, |
| 228 const std::vector<TransferredMessagePort>& sent_message_ports, | 226 const std::vector<int>& sent_message_ports, |
| 229 ServiceWorkerProviderHost* sender_provider_host, | 227 ServiceWorkerProviderHost* sender_provider_host, |
| 230 const ServiceWorkerDispatcherHost::StatusCallback& callback) { | 228 const ServiceWorkerDispatcherHost::StatusCallback& callback) { |
| 231 dispatcher_host_->DispatchExtendableMessageEvent( | 229 dispatcher_host_->DispatchExtendableMessageEvent( |
| 232 std::move(worker), message, source_origin, sent_message_ports, | 230 std::move(worker), message, source_origin, sent_message_ports, |
| 233 sender_provider_host, callback); | 231 sender_provider_host, callback); |
| 234 } | 232 } |
| 235 | 233 |
| 236 ServiceWorkerProviderHost* CreateServiceWorkerProviderHost(int provider_id) { | 234 ServiceWorkerProviderHost* CreateServiceWorkerProviderHost(int provider_id) { |
| 237 return new ServiceWorkerProviderHost( | 235 return new ServiceWorkerProviderHost( |
| 238 helper_->mock_render_process_id(), kRenderFrameId, provider_id, | 236 helper_->mock_render_process_id(), kRenderFrameId, provider_id, |
| (...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 670 provider_host_->running_hosted_version_ = version_; | 668 provider_host_->running_hosted_version_ = version_; |
| 671 | 669 |
| 672 // Set aside the initial refcount of the worker handle. | 670 // Set aside the initial refcount of the worker handle. |
| 673 provider_host_->GetOrCreateServiceWorkerHandle(version_.get()); | 671 provider_host_->GetOrCreateServiceWorkerHandle(version_.get()); |
| 674 ServiceWorkerHandle* sender_worker_handle = | 672 ServiceWorkerHandle* sender_worker_handle = |
| 675 dispatcher_host_->FindServiceWorkerHandle(provider_host_->provider_id(), | 673 dispatcher_host_->FindServiceWorkerHandle(provider_host_->provider_id(), |
| 676 version_->version_id()); | 674 version_->version_id()); |
| 677 const int ref_count = sender_worker_handle->ref_count(); | 675 const int ref_count = sender_worker_handle->ref_count(); |
| 678 | 676 |
| 679 // Dispatch ExtendableMessageEvent. | 677 // Dispatch ExtendableMessageEvent. |
| 680 std::vector<TransferredMessagePort> ports; | 678 std::vector<int> ports; |
| 681 SetUpDummyMessagePort(&ports); | 679 SetUpDummyMessagePort(&ports); |
| 682 bool called = false; | 680 bool called = false; |
| 683 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; | 681 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; |
| 684 DispatchExtendableMessageEvent( | 682 DispatchExtendableMessageEvent( |
| 685 version_, base::string16(), url::Origin(version_->scope().GetOrigin()), | 683 version_, base::string16(), url::Origin(version_->scope().GetOrigin()), |
| 686 ports, provider_host_, base::Bind(&SaveStatusCallback, &called, &status)); | 684 ports, provider_host_, base::Bind(&SaveStatusCallback, &called, &status)); |
| 687 for (TransferredMessagePort port : ports) | 685 for (int port : ports) |
| 688 EXPECT_TRUE(MessagePortService::GetInstance()->AreMessagesHeld(port.id)); | 686 EXPECT_TRUE(MessagePortService::GetInstance()->AreMessagesHeld(port)); |
| 689 EXPECT_EQ(ref_count + 1, sender_worker_handle->ref_count()); | 687 EXPECT_EQ(ref_count + 1, sender_worker_handle->ref_count()); |
| 690 base::RunLoop().RunUntilIdle(); | 688 base::RunLoop().RunUntilIdle(); |
| 691 EXPECT_TRUE(called); | 689 EXPECT_TRUE(called); |
| 692 EXPECT_EQ(SERVICE_WORKER_OK, status); | 690 EXPECT_EQ(SERVICE_WORKER_OK, status); |
| 693 | 691 |
| 694 // Messages should be held until ports are created at the destination. | 692 // Messages should be held until ports are created at the destination. |
| 695 for (TransferredMessagePort port : ports) | 693 for (int port : ports) |
| 696 EXPECT_TRUE(MessagePortService::GetInstance()->AreMessagesHeld(port.id)); | 694 EXPECT_TRUE(MessagePortService::GetInstance()->AreMessagesHeld(port)); |
| 697 | 695 |
| 698 EXPECT_EQ(ref_count + 1, sender_worker_handle->ref_count()); | 696 EXPECT_EQ(ref_count + 1, sender_worker_handle->ref_count()); |
| 699 } | 697 } |
| 700 | 698 |
| 701 TEST_F(ServiceWorkerDispatcherHostTest, DispatchExtendableMessageEvent_Fail) { | 699 TEST_F(ServiceWorkerDispatcherHostTest, DispatchExtendableMessageEvent_Fail) { |
| 702 GURL pattern = GURL("http://www.example.com/"); | 700 GURL pattern = GURL("http://www.example.com/"); |
| 703 GURL script_url = GURL("http://www.example.com/service_worker.js"); | 701 GURL script_url = GURL("http://www.example.com/service_worker.js"); |
| 704 | 702 |
| 705 Initialize(base::WrapUnique(new FailToStartWorkerTestHelper)); | 703 Initialize(base::WrapUnique(new FailToStartWorkerTestHelper)); |
| 706 SendProviderCreated(SERVICE_WORKER_PROVIDER_FOR_CONTROLLER, pattern); | 704 SendProviderCreated(SERVICE_WORKER_PROVIDER_FOR_CONTROLLER, pattern); |
| 707 SetUpRegistration(pattern, script_url); | 705 SetUpRegistration(pattern, script_url); |
| 708 | 706 |
| 709 // Set the running hosted version so that we can retrieve a valid service | 707 // Set the running hosted version so that we can retrieve a valid service |
| 710 // worker object information for the source attribute of the message event. | 708 // worker object information for the source attribute of the message event. |
| 711 provider_host_->running_hosted_version_ = version_; | 709 provider_host_->running_hosted_version_ = version_; |
| 712 | 710 |
| 713 // Set aside the initial refcount of the worker handle. | 711 // Set aside the initial refcount of the worker handle. |
| 714 provider_host_->GetOrCreateServiceWorkerHandle(version_.get()); | 712 provider_host_->GetOrCreateServiceWorkerHandle(version_.get()); |
| 715 ServiceWorkerHandle* sender_worker_handle = | 713 ServiceWorkerHandle* sender_worker_handle = |
| 716 dispatcher_host_->FindServiceWorkerHandle(provider_host_->provider_id(), | 714 dispatcher_host_->FindServiceWorkerHandle(provider_host_->provider_id(), |
| 717 version_->version_id()); | 715 version_->version_id()); |
| 718 const int ref_count = sender_worker_handle->ref_count(); | 716 const int ref_count = sender_worker_handle->ref_count(); |
| 719 | 717 |
| 720 // Try to dispatch ExtendableMessageEvent. This should fail to start the | 718 // Try to dispatch ExtendableMessageEvent. This should fail to start the |
| 721 // worker and to dispatch the event. | 719 // worker and to dispatch the event. |
| 722 std::vector<TransferredMessagePort> ports; | 720 std::vector<int> ports; |
| 723 SetUpDummyMessagePort(&ports); | 721 SetUpDummyMessagePort(&ports); |
| 724 bool called = false; | 722 bool called = false; |
| 725 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; | 723 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; |
| 726 DispatchExtendableMessageEvent( | 724 DispatchExtendableMessageEvent( |
| 727 version_, base::string16(), url::Origin(version_->scope().GetOrigin()), | 725 version_, base::string16(), url::Origin(version_->scope().GetOrigin()), |
| 728 ports, provider_host_, base::Bind(&SaveStatusCallback, &called, &status)); | 726 ports, provider_host_, base::Bind(&SaveStatusCallback, &called, &status)); |
| 729 for (TransferredMessagePort port : ports) | 727 for (int port : ports) |
| 730 EXPECT_TRUE(MessagePortService::GetInstance()->AreMessagesHeld(port.id)); | 728 EXPECT_TRUE(MessagePortService::GetInstance()->AreMessagesHeld(port)); |
| 731 EXPECT_EQ(ref_count + 1, sender_worker_handle->ref_count()); | 729 EXPECT_EQ(ref_count + 1, sender_worker_handle->ref_count()); |
| 732 base::RunLoop().RunUntilIdle(); | 730 base::RunLoop().RunUntilIdle(); |
| 733 EXPECT_TRUE(called); | 731 EXPECT_TRUE(called); |
| 734 EXPECT_EQ(SERVICE_WORKER_ERROR_START_WORKER_FAILED, status); | 732 EXPECT_EQ(SERVICE_WORKER_ERROR_START_WORKER_FAILED, status); |
| 735 | 733 |
| 736 // The error callback should clean up the ports and handle. | 734 // The error callback should clean up the ports and handle. |
| 737 for (TransferredMessagePort port : ports) | 735 for (int port : ports) |
| 738 EXPECT_FALSE(MessagePortService::GetInstance()->AreMessagesHeld(port.id)); | 736 EXPECT_FALSE(MessagePortService::GetInstance()->AreMessagesHeld(port)); |
| 739 EXPECT_EQ(ref_count, sender_worker_handle->ref_count()); | 737 EXPECT_EQ(ref_count, sender_worker_handle->ref_count()); |
| 740 } | 738 } |
| 741 | 739 |
| 742 TEST_F(ServiceWorkerDispatcherHostTest, OnSetHostedVersionId) { | 740 TEST_F(ServiceWorkerDispatcherHostTest, OnSetHostedVersionId) { |
| 743 GURL pattern = GURL("http://www.example.com/"); | 741 GURL pattern = GURL("http://www.example.com/"); |
| 744 GURL script_url = GURL("http://www.example.com/service_worker.js"); | 742 GURL script_url = GURL("http://www.example.com/service_worker.js"); |
| 745 | 743 |
| 746 Initialize(base::WrapUnique(new FailToStartWorkerTestHelper)); | 744 Initialize(base::WrapUnique(new FailToStartWorkerTestHelper)); |
| 747 SendProviderCreated(SERVICE_WORKER_PROVIDER_FOR_CONTROLLER, pattern); | 745 SendProviderCreated(SERVICE_WORKER_PROVIDER_FOR_CONTROLLER, pattern); |
| 748 SetUpRegistration(pattern, script_url); | 746 SetUpRegistration(pattern, script_url); |
| 749 | 747 |
| 750 const int64_t kProviderId = 99; // Dummy value | 748 const int64_t kProviderId = 99; // Dummy value |
| 751 bool called; | 749 bool called; |
| 752 ServiceWorkerStatusCode status; | 750 ServiceWorkerStatusCode status; |
| 753 // StartWorker puts the worker in STARTING state but it will have no | 751 // StartWorker puts the worker in STARTING state but it will have no |
| 754 // process id yet. | 752 // process id yet. |
| 755 version_->StartWorker(ServiceWorkerMetrics::EventType::UNKNOWN, | 753 version_->StartWorker(ServiceWorkerMetrics::EventType::UNKNOWN, |
| 756 base::Bind(&SaveStatusCallback, &called, &status)); | 754 base::Bind(&SaveStatusCallback, &called, &status)); |
| 757 EXPECT_NE(version_->embedded_worker()->process_id(), | 755 EXPECT_NE(version_->embedded_worker()->process_id(), |
| 758 provider_host_->process_id()); | 756 provider_host_->process_id()); |
| 759 // SendSetHostedVersionId should reject because the provider host process id | 757 // SendSetHostedVersionId should reject because the provider host process id |
| 760 // is different. | 758 // is different. |
| 761 SendSetHostedVersionId(kProviderId, version_->version_id()); | 759 SendSetHostedVersionId(kProviderId, version_->version_id()); |
| 762 base::RunLoop().RunUntilIdle(); | 760 base::RunLoop().RunUntilIdle(); |
| 763 EXPECT_FALSE(dispatcher_host_->ipc_sink()->GetUniqueMessageMatching( | 761 EXPECT_FALSE(dispatcher_host_->ipc_sink()->GetUniqueMessageMatching( |
| 764 ServiceWorkerMsg_AssociateRegistration::ID)); | 762 ServiceWorkerMsg_AssociateRegistration::ID)); |
| 765 } | 763 } |
| 766 | 764 |
| 767 } // namespace content | 765 } // namespace content |
| OLD | NEW |