Chromium Code Reviews| 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 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 116 | 116 |
| 117 protected: | 117 protected: |
| 118 EmbeddedWorkerTestHelper* helper_; | 118 EmbeddedWorkerTestHelper* helper_; |
| 119 ~TestingServiceWorkerDispatcherHost() override {} | 119 ~TestingServiceWorkerDispatcherHost() override {} |
| 120 }; | 120 }; |
| 121 | 121 |
| 122 class FailToStartWorkerTestHelper : public EmbeddedWorkerTestHelper { | 122 class FailToStartWorkerTestHelper : public EmbeddedWorkerTestHelper { |
| 123 public: | 123 public: |
| 124 FailToStartWorkerTestHelper() : EmbeddedWorkerTestHelper(base::FilePath()) {} | 124 FailToStartWorkerTestHelper() : EmbeddedWorkerTestHelper(base::FilePath()) {} |
| 125 | 125 |
| 126 void OnStartWorker(int embedded_worker_id, | 126 void OnStartWorker( |
| 127 int64_t service_worker_version_id, | 127 int embedded_worker_id, |
| 128 const GURL& scope, | 128 int64_t service_worker_version_id, |
| 129 const GURL& script_url, | 129 const GURL& scope, |
| 130 bool pause_after_download, | 130 const GURL& script_url, |
| 131 mojom::ServiceWorkerEventDispatcherRequest request, | 131 bool pause_after_download, |
| 132 mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo | 132 mojom::ServiceWorkerEventDispatcherRequest request, |
| 133 instance_host) override { | 133 mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo instance_host, |
| 134 mojom::ServiceWorkerProviderClientInfoPtr provider_client_info) override { | |
| 134 mojom::EmbeddedWorkerInstanceHostAssociatedPtr instance_host_ptr; | 135 mojom::EmbeddedWorkerInstanceHostAssociatedPtr instance_host_ptr; |
| 135 instance_host_ptr.Bind(std::move(instance_host)); | 136 instance_host_ptr.Bind(std::move(instance_host)); |
| 136 instance_host_ptr->OnStopped(); | 137 instance_host_ptr->OnStopped(); |
| 137 base::RunLoop().RunUntilIdle(); | 138 base::RunLoop().RunUntilIdle(); |
| 138 } | 139 } |
| 139 }; | 140 }; |
| 140 | 141 |
| 141 class ServiceWorkerDispatcherHostTest : public testing::Test { | 142 class ServiceWorkerDispatcherHostTest : public testing::Test { |
| 142 protected: | 143 protected: |
| 143 ServiceWorkerDispatcherHostTest() | 144 ServiceWorkerDispatcherHostTest() |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 189 bool called = false; | 190 bool called = false; |
| 190 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; | 191 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; |
| 191 helper_->context()->storage()->StoreRegistration( | 192 helper_->context()->storage()->StoreRegistration( |
| 192 registration_.get(), version_.get(), | 193 registration_.get(), version_.get(), |
| 193 base::Bind(&SaveStatusCallback, &called, &status)); | 194 base::Bind(&SaveStatusCallback, &called, &status)); |
| 194 base::RunLoop().RunUntilIdle(); | 195 base::RunLoop().RunUntilIdle(); |
| 195 EXPECT_TRUE(called); | 196 EXPECT_TRUE(called); |
| 196 EXPECT_EQ(SERVICE_WORKER_OK, status); | 197 EXPECT_EQ(SERVICE_WORKER_OK, status); |
| 197 } | 198 } |
| 198 | 199 |
| 199 void SendSetHostedVersionId(int provider_id, | |
| 200 int64_t version_id, | |
| 201 int embedded_worker_id) { | |
| 202 dispatcher_host_->OnSetHostedVersionId(provider_id, version_id, | |
| 203 embedded_worker_id); | |
| 204 } | |
| 205 | |
| 206 void SendProviderCreated(ServiceWorkerProviderType type, | 200 void SendProviderCreated(ServiceWorkerProviderType type, |
| 207 const GURL& pattern) { | 201 const GURL& pattern) { |
| 208 const int64_t kProviderId = 99; | 202 const int64_t kProviderId = 99; |
| 209 ServiceWorkerProviderHostInfo info(kProviderId, MSG_ROUTING_NONE, type, | 203 ServiceWorkerProviderHostInfo info(kProviderId, MSG_ROUTING_NONE, type, |
| 210 true /* is_parent_frame_secure */); | 204 true /* is_parent_frame_secure */); |
| 211 remote_endpoint_.BindWithProviderHostInfo(&info); | 205 remote_endpoint_.BindWithProviderHostInfo(&info); |
| 212 | 206 |
| 213 dispatcher_host_->OnProviderCreated(std::move(info)); | 207 dispatcher_host_->OnProviderCreated(std::move(info)); |
| 214 helper_->SimulateAddProcessToPattern(pattern, | 208 helper_->SimulateAddProcessToPattern(pattern, |
| 215 helper_->mock_render_process_id()); | 209 helper_->mock_render_process_id()); |
| 216 provider_host_ = context()->GetProviderHost( | 210 provider_host_ = context()->GetProviderHost( |
| 217 helper_->mock_render_process_id(), kProviderId); | 211 helper_->mock_render_process_id(), kProviderId); |
| 218 } | 212 } |
| 219 | 213 |
| 214 void PrepareProviderForServiceWorkerContext(ServiceWorkerVersion* version, | |
| 215 const GURL& pattern) { | |
| 216 std::unique_ptr<ServiceWorkerProviderHost> host = | |
| 217 CreateProviderHostForServiceWorkerContext( | |
| 218 dispatcher_host_->render_process_id_, | |
| 219 true /* is_parent_frame_secure */, version, | |
| 220 helper_->context()->AsWeakPtr(), &remote_endpoint_); | |
| 221 provider_host_ = host.get(); | |
| 222 helper_->SimulateAddProcessToPattern(pattern, | |
| 223 helper_->mock_render_process_id()); | |
|
falken
2017/06/14 08:10:34
Can you help me understand why we use dispatcher_h
shimazu
2017/06/16 04:18:43
Thanks, these two should be the same. Updated.
| |
| 224 context()->AddProviderHost(std::move(host)); | |
| 225 } | |
| 226 | |
| 220 void SendRegister(int64_t provider_id, GURL pattern, GURL worker_url) { | 227 void SendRegister(int64_t provider_id, GURL pattern, GURL worker_url) { |
| 221 dispatcher_host_->OnMessageReceived( | 228 dispatcher_host_->OnMessageReceived( |
| 222 ServiceWorkerHostMsg_RegisterServiceWorker( | 229 ServiceWorkerHostMsg_RegisterServiceWorker( |
| 223 -1, -1, provider_id, pattern, worker_url)); | 230 -1, -1, provider_id, pattern, worker_url)); |
| 224 base::RunLoop().RunUntilIdle(); | 231 base::RunLoop().RunUntilIdle(); |
| 225 } | 232 } |
| 226 | 233 |
| 227 void Register(int64_t provider_id, | 234 void Register(int64_t provider_id, |
| 228 GURL pattern, | 235 GURL pattern, |
| 229 GURL worker_url, | 236 GURL worker_url, |
| (...skipping 523 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 753 ServiceWorkerRemoteProviderEndpoint remote_endpoint; | 760 ServiceWorkerRemoteProviderEndpoint remote_endpoint; |
| 754 remote_endpoint.BindWithProviderHostInfo(&host_info); | 761 remote_endpoint.BindWithProviderHostInfo(&host_info); |
| 755 new_dispatcher_host->OnProviderCreated(std::move(host_info)); | 762 new_dispatcher_host->OnProviderCreated(std::move(host_info)); |
| 756 EXPECT_EQ(0, new_dispatcher_host->bad_messages_received_count_); | 763 EXPECT_EQ(0, new_dispatcher_host->bad_messages_received_count_); |
| 757 } | 764 } |
| 758 | 765 |
| 759 TEST_F(ServiceWorkerDispatcherHostTest, DispatchExtendableMessageEvent) { | 766 TEST_F(ServiceWorkerDispatcherHostTest, DispatchExtendableMessageEvent) { |
| 760 GURL pattern = GURL("http://www.example.com/"); | 767 GURL pattern = GURL("http://www.example.com/"); |
| 761 GURL script_url = GURL("http://www.example.com/service_worker.js"); | 768 GURL script_url = GURL("http://www.example.com/service_worker.js"); |
| 762 | 769 |
| 763 SendProviderCreated(SERVICE_WORKER_PROVIDER_FOR_CONTROLLER, pattern); | |
| 764 SetUpRegistration(pattern, script_url); | 770 SetUpRegistration(pattern, script_url); |
| 771 PrepareProviderForServiceWorkerContext(version_.get(), pattern); | |
| 765 | 772 |
| 766 // Set the running hosted version so that we can retrieve a valid service | 773 // Set the running hosted version so that we can retrieve a valid service |
| 767 // worker object information for the source attribute of the message event. | 774 // worker object information for the source attribute of the message event. |
| 768 provider_host_->running_hosted_version_ = version_; | 775 provider_host_->running_hosted_version_ = version_; |
| 769 | 776 |
| 770 // Set aside the initial refcount of the worker handle. | 777 // Set aside the initial refcount of the worker handle. |
| 771 provider_host_->GetOrCreateServiceWorkerHandle(version_.get()); | 778 provider_host_->GetOrCreateServiceWorkerHandle(version_.get()); |
| 772 ServiceWorkerHandle* sender_worker_handle = | 779 ServiceWorkerHandle* sender_worker_handle = |
| 773 dispatcher_host_->FindServiceWorkerHandle(provider_host_->provider_id(), | 780 dispatcher_host_->FindServiceWorkerHandle(provider_host_->provider_id(), |
| 774 version_->version_id()); | 781 version_->version_id()); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 828 // worker and to dispatch the event. | 835 // worker and to dispatch the event. |
| 829 std::vector<MessagePort> ports; | 836 std::vector<MessagePort> ports; |
| 830 SetUpDummyMessagePort(&ports); | 837 SetUpDummyMessagePort(&ports); |
| 831 bool called = false; | 838 bool called = false; |
| 832 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; | 839 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; |
| 833 DispatchExtendableMessageEvent( | 840 DispatchExtendableMessageEvent( |
| 834 version_, base::string16(), url::Origin(version_->scope().GetOrigin()), | 841 version_, base::string16(), url::Origin(version_->scope().GetOrigin()), |
| 835 ports, provider_host_, base::Bind(&SaveStatusCallback, &called, &status)); | 842 ports, provider_host_, base::Bind(&SaveStatusCallback, &called, &status)); |
| 836 base::RunLoop().RunUntilIdle(); | 843 base::RunLoop().RunUntilIdle(); |
| 837 EXPECT_TRUE(called); | 844 EXPECT_TRUE(called); |
| 845 EXPECT_NE(SERVICE_WORKER_ERROR_TIMEOUT, status); | |
|
falken
2017/06/14 08:10:34
this is redundant with line 846, no?
shimazu
2017/06/16 04:18:43
Yes, that's right. Removed.
| |
| 838 EXPECT_EQ(SERVICE_WORKER_ERROR_START_WORKER_FAILED, status); | 846 EXPECT_EQ(SERVICE_WORKER_ERROR_START_WORKER_FAILED, status); |
| 839 } | 847 } |
| 840 | 848 |
| 841 TEST_F(ServiceWorkerDispatcherHostTest, OnSetHostedVersionId) { | |
| 842 GURL pattern = GURL("http://www.example.com/"); | |
| 843 GURL script_url = GURL("http://www.example.com/service_worker.js"); | |
| 844 | |
| 845 Initialize(base::WrapUnique(new FailToStartWorkerTestHelper)); | |
| 846 SendProviderCreated(SERVICE_WORKER_PROVIDER_FOR_CONTROLLER, pattern); | |
| 847 SetUpRegistration(pattern, script_url); | |
| 848 | |
| 849 const int64_t kProviderId = 99; // Dummy value | |
| 850 bool called; | |
| 851 ServiceWorkerStatusCode status; | |
| 852 // StartWorker puts the worker in STARTING state but it will have no | |
| 853 // process id yet. | |
| 854 version_->StartWorker(ServiceWorkerMetrics::EventType::UNKNOWN, | |
| 855 base::Bind(&SaveStatusCallback, &called, &status)); | |
| 856 EXPECT_NE(version_->embedded_worker()->process_id(), | |
| 857 provider_host_->process_id()); | |
| 858 // SendSetHostedVersionId should reject because the provider host process id | |
| 859 // is different. It should call BadMessageReceived because it's not an | |
| 860 // expected error state. | |
| 861 SendSetHostedVersionId(kProviderId, version_->version_id(), | |
| 862 version_->embedded_worker()->embedded_worker_id()); | |
| 863 base::RunLoop().RunUntilIdle(); | |
| 864 EXPECT_FALSE(dispatcher_host_->ipc_sink()->GetUniqueMessageMatching( | |
| 865 ServiceWorkerMsg_AssociateRegistration::ID)); | |
| 866 EXPECT_EQ(1, dispatcher_host_->bad_messages_received_count_); | |
| 867 } | |
| 868 | |
| 869 TEST_F(ServiceWorkerDispatcherHostTest, OnSetHostedVersionId_DetachedWorker) { | |
| 870 GURL pattern = GURL("http://www.example.com/"); | |
| 871 GURL script_url = GURL("http://www.example.com/service_worker.js"); | |
| 872 | |
| 873 Initialize(base::WrapUnique(new FailToStartWorkerTestHelper)); | |
| 874 SendProviderCreated(SERVICE_WORKER_PROVIDER_FOR_CONTROLLER, pattern); | |
| 875 SetUpRegistration(pattern, script_url); | |
| 876 | |
| 877 const int64_t kProviderId = 99; // Dummy value | |
| 878 bool called; | |
| 879 ServiceWorkerStatusCode status; | |
| 880 // StartWorker puts the worker in STARTING state. | |
| 881 version_->StartWorker(ServiceWorkerMetrics::EventType::UNKNOWN, | |
| 882 base::Bind(&SaveStatusCallback, &called, &status)); | |
| 883 | |
| 884 // SendSetHostedVersionId should bail because the embedded worker is | |
| 885 // different. It shouldn't call BadMessageReceived because receiving a message | |
| 886 // for a detached worker is a legitimite possibility. | |
| 887 int bad_embedded_worker_id = | |
| 888 version_->embedded_worker()->embedded_worker_id() + 1; | |
| 889 SendSetHostedVersionId(kProviderId, version_->version_id(), | |
| 890 bad_embedded_worker_id); | |
| 891 base::RunLoop().RunUntilIdle(); | |
| 892 EXPECT_FALSE(dispatcher_host_->ipc_sink()->GetUniqueMessageMatching( | |
| 893 ServiceWorkerMsg_AssociateRegistration::ID)); | |
| 894 EXPECT_EQ(0, dispatcher_host_->bad_messages_received_count_); | |
| 895 } | |
| 896 | |
| 897 TEST_F(ServiceWorkerDispatcherHostTest, ReceivedTimedOutRequestResponse) { | 849 TEST_F(ServiceWorkerDispatcherHostTest, ReceivedTimedOutRequestResponse) { |
| 898 GURL pattern = GURL("https://www.example.com/"); | 850 GURL pattern = GURL("https://www.example.com/"); |
| 899 GURL script_url = GURL("https://www.example.com/service_worker.js"); | 851 GURL script_url = GURL("https://www.example.com/service_worker.js"); |
| 900 | 852 |
| 901 SendProviderCreated(SERVICE_WORKER_PROVIDER_FOR_WINDOW, pattern); | 853 SendProviderCreated(SERVICE_WORKER_PROVIDER_FOR_WINDOW, pattern); |
| 902 SetUpRegistration(pattern, script_url); | 854 SetUpRegistration(pattern, script_url); |
| 903 | 855 |
| 904 version_->StartWorker(ServiceWorkerMetrics::EventType::UNKNOWN, | 856 version_->StartWorker(ServiceWorkerMetrics::EventType::UNKNOWN, |
| 905 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 857 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 906 base::RunLoop().RunUntilIdle(); | 858 base::RunLoop().RunUntilIdle(); |
| 907 | 859 |
| 908 // Set the worker status to STOPPING. | 860 // Set the worker status to STOPPING. |
| 909 version_->embedded_worker()->Stop(); | 861 version_->embedded_worker()->Stop(); |
| 910 EXPECT_EQ(EmbeddedWorkerStatus::STOPPING, version_->running_status()); | 862 EXPECT_EQ(EmbeddedWorkerStatus::STOPPING, version_->running_status()); |
| 911 | 863 |
| 912 // Receive a response for a timed out request. The bad message count should | 864 // Receive a response for a timed out request. The bad message count should |
| 913 // not increase. | 865 // not increase. |
| 914 const int kFetchEventId = 91; // Dummy value | 866 const int kFetchEventId = 91; // Dummy value |
| 915 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_FetchEventResponse( | 867 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_FetchEventResponse( |
| 916 version_->embedded_worker()->embedded_worker_id(), kFetchEventId, | 868 version_->embedded_worker()->embedded_worker_id(), kFetchEventId, |
| 917 ServiceWorkerResponse(), base::Time::Now())); | 869 ServiceWorkerResponse(), base::Time::Now())); |
| 918 | 870 |
| 919 base::RunLoop().RunUntilIdle(); | 871 base::RunLoop().RunUntilIdle(); |
| 920 EXPECT_EQ(0, dispatcher_host_->bad_messages_received_count_); | 872 EXPECT_EQ(0, dispatcher_host_->bad_messages_received_count_); |
| 921 } | 873 } |
| 922 | 874 |
| 923 } // namespace content | 875 } // namespace content |
| OLD | NEW |