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

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

Issue 2958753003: Revert "Create ServiceWorkerProviderHost before starting worker" (Closed)
Patch Set: Created 3 years, 5 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 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
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( 126 void OnStartWorker(int embedded_worker_id,
127 int embedded_worker_id, 127 int64_t service_worker_version_id,
128 int64_t service_worker_version_id, 128 const GURL& scope,
129 const GURL& scope, 129 const GURL& script_url,
130 const GURL& script_url, 130 bool pause_after_download,
131 bool pause_after_download, 131 mojom::ServiceWorkerEventDispatcherRequest request,
132 mojom::ServiceWorkerEventDispatcherRequest request, 132 mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo
133 mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo instance_host, 133 instance_host) override {
134 mojom::ServiceWorkerProviderInfoForStartWorkerPtr provider_info)
135 override {
136 mojom::EmbeddedWorkerInstanceHostAssociatedPtr instance_host_ptr; 134 mojom::EmbeddedWorkerInstanceHostAssociatedPtr instance_host_ptr;
137 instance_host_ptr.Bind(std::move(instance_host)); 135 instance_host_ptr.Bind(std::move(instance_host));
138 instance_host_ptr->OnStopped(); 136 instance_host_ptr->OnStopped();
139 base::RunLoop().RunUntilIdle(); 137 base::RunLoop().RunUntilIdle();
140 } 138 }
141 }; 139 };
142 140
143 class ServiceWorkerDispatcherHostTest : public testing::Test { 141 class ServiceWorkerDispatcherHostTest : public testing::Test {
144 protected: 142 protected:
145 ServiceWorkerDispatcherHostTest() 143 ServiceWorkerDispatcherHostTest()
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
191 bool called = false; 189 bool called = false;
192 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; 190 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE;
193 helper_->context()->storage()->StoreRegistration( 191 helper_->context()->storage()->StoreRegistration(
194 registration_.get(), version_.get(), 192 registration_.get(), version_.get(),
195 base::Bind(&SaveStatusCallback, &called, &status)); 193 base::Bind(&SaveStatusCallback, &called, &status));
196 base::RunLoop().RunUntilIdle(); 194 base::RunLoop().RunUntilIdle();
197 EXPECT_TRUE(called); 195 EXPECT_TRUE(called);
198 EXPECT_EQ(SERVICE_WORKER_OK, status); 196 EXPECT_EQ(SERVICE_WORKER_OK, status);
199 } 197 }
200 198
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
201 void SendProviderCreated(ServiceWorkerProviderType type, 206 void SendProviderCreated(ServiceWorkerProviderType type,
202 const GURL& pattern) { 207 const GURL& pattern) {
203 const int64_t kProviderId = 99; 208 const int64_t kProviderId = 99;
204 ServiceWorkerProviderHostInfo info(kProviderId, MSG_ROUTING_NONE, type, 209 ServiceWorkerProviderHostInfo info(kProviderId, MSG_ROUTING_NONE, type,
205 true /* is_parent_frame_secure */); 210 true /* is_parent_frame_secure */);
206 remote_endpoint_.BindWithProviderHostInfo(&info); 211 remote_endpoint_.BindWithProviderHostInfo(&info);
207 212
208 dispatcher_host_->OnProviderCreated(std::move(info)); 213 dispatcher_host_->OnProviderCreated(std::move(info));
209 helper_->SimulateAddProcessToPattern(pattern, 214 helper_->SimulateAddProcessToPattern(pattern,
210 helper_->mock_render_process_id()); 215 helper_->mock_render_process_id());
211 provider_host_ = context()->GetProviderHost( 216 provider_host_ = context()->GetProviderHost(
212 helper_->mock_render_process_id(), kProviderId); 217 helper_->mock_render_process_id(), kProviderId);
213 } 218 }
214 219
215 void PrepareProviderForServiceWorkerContext(ServiceWorkerVersion* version,
216 const GURL& pattern) {
217 std::unique_ptr<ServiceWorkerProviderHost> host =
218 CreateProviderHostForServiceWorkerContext(
219 helper_->mock_render_process_id(),
220 true /* is_parent_frame_secure */, version,
221 helper_->context()->AsWeakPtr(), &remote_endpoint_);
222 provider_host_ = host.get();
223 helper_->SimulateAddProcessToPattern(pattern,
224 helper_->mock_render_process_id());
225 context()->AddProviderHost(std::move(host));
226 }
227
228 void SendRegister(int64_t provider_id, GURL pattern, GURL worker_url) { 220 void SendRegister(int64_t provider_id, GURL pattern, GURL worker_url) {
229 dispatcher_host_->OnMessageReceived( 221 dispatcher_host_->OnMessageReceived(
230 ServiceWorkerHostMsg_RegisterServiceWorker( 222 ServiceWorkerHostMsg_RegisterServiceWorker(
231 -1, -1, provider_id, pattern, worker_url)); 223 -1, -1, provider_id, pattern, worker_url));
232 base::RunLoop().RunUntilIdle(); 224 base::RunLoop().RunUntilIdle();
233 } 225 }
234 226
235 void Register(int64_t provider_id, 227 void Register(int64_t provider_id,
236 GURL pattern, 228 GURL pattern,
237 GURL worker_url, 229 GURL worker_url,
(...skipping 523 matching lines...) Expand 10 before | Expand all | Expand 10 after
761 ServiceWorkerRemoteProviderEndpoint remote_endpoint; 753 ServiceWorkerRemoteProviderEndpoint remote_endpoint;
762 remote_endpoint.BindWithProviderHostInfo(&host_info); 754 remote_endpoint.BindWithProviderHostInfo(&host_info);
763 new_dispatcher_host->OnProviderCreated(std::move(host_info)); 755 new_dispatcher_host->OnProviderCreated(std::move(host_info));
764 EXPECT_EQ(0, new_dispatcher_host->bad_messages_received_count_); 756 EXPECT_EQ(0, new_dispatcher_host->bad_messages_received_count_);
765 } 757 }
766 758
767 TEST_F(ServiceWorkerDispatcherHostTest, DispatchExtendableMessageEvent) { 759 TEST_F(ServiceWorkerDispatcherHostTest, DispatchExtendableMessageEvent) {
768 GURL pattern = GURL("http://www.example.com/"); 760 GURL pattern = GURL("http://www.example.com/");
769 GURL script_url = GURL("http://www.example.com/service_worker.js"); 761 GURL script_url = GURL("http://www.example.com/service_worker.js");
770 762
763 SendProviderCreated(SERVICE_WORKER_PROVIDER_FOR_CONTROLLER, pattern);
771 SetUpRegistration(pattern, script_url); 764 SetUpRegistration(pattern, script_url);
772 PrepareProviderForServiceWorkerContext(version_.get(), pattern);
773 765
774 // Set the running hosted version so that we can retrieve a valid service 766 // Set the running hosted version so that we can retrieve a valid service
775 // worker object information for the source attribute of the message event. 767 // worker object information for the source attribute of the message event.
776 provider_host_->running_hosted_version_ = version_; 768 provider_host_->running_hosted_version_ = version_;
777 769
778 // Set aside the initial refcount of the worker handle. 770 // Set aside the initial refcount of the worker handle.
779 provider_host_->GetOrCreateServiceWorkerHandle(version_.get()); 771 provider_host_->GetOrCreateServiceWorkerHandle(version_.get());
780 ServiceWorkerHandle* sender_worker_handle = 772 ServiceWorkerHandle* sender_worker_handle =
781 dispatcher_host_->FindServiceWorkerHandle(provider_host_->provider_id(), 773 dispatcher_host_->FindServiceWorkerHandle(provider_host_->provider_id(),
782 version_->version_id()); 774 version_->version_id());
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
839 bool called = false; 831 bool called = false;
840 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; 832 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE;
841 DispatchExtendableMessageEvent( 833 DispatchExtendableMessageEvent(
842 version_, base::string16(), url::Origin(version_->scope().GetOrigin()), 834 version_, base::string16(), url::Origin(version_->scope().GetOrigin()),
843 ports, provider_host_, base::Bind(&SaveStatusCallback, &called, &status)); 835 ports, provider_host_, base::Bind(&SaveStatusCallback, &called, &status));
844 base::RunLoop().RunUntilIdle(); 836 base::RunLoop().RunUntilIdle();
845 EXPECT_TRUE(called); 837 EXPECT_TRUE(called);
846 EXPECT_EQ(SERVICE_WORKER_ERROR_START_WORKER_FAILED, status); 838 EXPECT_EQ(SERVICE_WORKER_ERROR_START_WORKER_FAILED, status);
847 } 839 }
848 840
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
849 TEST_F(ServiceWorkerDispatcherHostTest, ReceivedTimedOutRequestResponse) { 897 TEST_F(ServiceWorkerDispatcherHostTest, ReceivedTimedOutRequestResponse) {
850 GURL pattern = GURL("https://www.example.com/"); 898 GURL pattern = GURL("https://www.example.com/");
851 GURL script_url = GURL("https://www.example.com/service_worker.js"); 899 GURL script_url = GURL("https://www.example.com/service_worker.js");
852 900
853 SendProviderCreated(SERVICE_WORKER_PROVIDER_FOR_WINDOW, pattern); 901 SendProviderCreated(SERVICE_WORKER_PROVIDER_FOR_WINDOW, pattern);
854 SetUpRegistration(pattern, script_url); 902 SetUpRegistration(pattern, script_url);
855 903
856 version_->StartWorker(ServiceWorkerMetrics::EventType::UNKNOWN, 904 version_->StartWorker(ServiceWorkerMetrics::EventType::UNKNOWN,
857 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); 905 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback));
858 base::RunLoop().RunUntilIdle(); 906 base::RunLoop().RunUntilIdle();
859 907
860 // Set the worker status to STOPPING. 908 // Set the worker status to STOPPING.
861 version_->embedded_worker()->Stop(); 909 version_->embedded_worker()->Stop();
862 EXPECT_EQ(EmbeddedWorkerStatus::STOPPING, version_->running_status()); 910 EXPECT_EQ(EmbeddedWorkerStatus::STOPPING, version_->running_status());
863 911
864 // Receive a response for a timed out request. The bad message count should 912 // Receive a response for a timed out request. The bad message count should
865 // not increase. 913 // not increase.
866 const int kFetchEventId = 91; // Dummy value 914 const int kFetchEventId = 91; // Dummy value
867 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_FetchEventResponse( 915 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_FetchEventResponse(
868 version_->embedded_worker()->embedded_worker_id(), kFetchEventId, 916 version_->embedded_worker()->embedded_worker_id(), kFetchEventId,
869 ServiceWorkerResponse(), base::Time::Now())); 917 ServiceWorkerResponse(), base::Time::Now()));
870 918
871 base::RunLoop().RunUntilIdle(); 919 base::RunLoop().RunUntilIdle();
872 EXPECT_EQ(0, dispatcher_host_->bad_messages_received_count_); 920 EXPECT_EQ(0, dispatcher_host_->bad_messages_received_count_);
873 } 921 }
874 922
875 } // namespace content 923 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698