OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_controllee_request_handl
er.h" | 5 #include "content/browser/service_worker/service_worker_controllee_request_handl
er.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/callback_helpers.h" | 10 #include "base/callback_helpers.h" |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 return false; | 164 return false; |
165 } | 165 } |
166 }; | 166 }; |
167 | 167 |
168 TEST_F(ServiceWorkerControlleeRequestHandlerTest, DisallowServiceWorker) { | 168 TEST_F(ServiceWorkerControlleeRequestHandlerTest, DisallowServiceWorker) { |
169 ServiceWorkerTestContentBrowserClient test_browser_client; | 169 ServiceWorkerTestContentBrowserClient test_browser_client; |
170 ContentBrowserClient* old_browser_client = | 170 ContentBrowserClient* old_browser_client = |
171 SetBrowserClientForTesting(&test_browser_client); | 171 SetBrowserClientForTesting(&test_browser_client); |
172 | 172 |
173 // Store an activated worker. | 173 // Store an activated worker. |
| 174 version_->set_fetch_handler_existence( |
| 175 ServiceWorkerVersion::FetchHandlerExistence::EXISTS); |
174 version_->SetStatus(ServiceWorkerVersion::ACTIVATED); | 176 version_->SetStatus(ServiceWorkerVersion::ACTIVATED); |
175 version_->set_has_fetch_handler(true); | |
176 registration_->SetActiveVersion(version_); | 177 registration_->SetActiveVersion(version_); |
177 context()->storage()->StoreRegistration( | 178 context()->storage()->StoreRegistration( |
178 registration_.get(), | 179 registration_.get(), |
179 version_.get(), | 180 version_.get(), |
180 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 181 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
181 base::RunLoop().RunUntilIdle(); | 182 base::RunLoop().RunUntilIdle(); |
182 | 183 |
183 // Conduct a main resource load. | 184 // Conduct a main resource load. |
184 ServiceWorkerRequestTestResources test_resources( | 185 ServiceWorkerRequestTestResources test_resources( |
185 this, GURL("https://host/scope/doc"), RESOURCE_TYPE_MAIN_FRAME); | 186 this, GURL("https://host/scope/doc"), RESOURCE_TYPE_MAIN_FRAME); |
186 ServiceWorkerURLRequestJob* sw_job = test_resources.MaybeCreateJob(); | 187 ServiceWorkerURLRequestJob* sw_job = test_resources.MaybeCreateJob(); |
187 | 188 |
188 EXPECT_FALSE(sw_job->ShouldFallbackToNetwork()); | 189 EXPECT_FALSE(sw_job->ShouldFallbackToNetwork()); |
189 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); | 190 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); |
190 EXPECT_FALSE(version_->HasControllee()); | 191 EXPECT_FALSE(version_->HasControllee()); |
191 base::RunLoop().RunUntilIdle(); | 192 base::RunLoop().RunUntilIdle(); |
192 | 193 |
193 // Verify we did not use the worker. | 194 // Verify we did not use the worker. |
194 EXPECT_TRUE(sw_job->ShouldFallbackToNetwork()); | 195 EXPECT_TRUE(sw_job->ShouldFallbackToNetwork()); |
195 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); | 196 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); |
196 EXPECT_FALSE(version_->HasControllee()); | 197 EXPECT_FALSE(version_->HasControllee()); |
197 | 198 |
198 SetBrowserClientForTesting(old_browser_client); | 199 SetBrowserClientForTesting(old_browser_client); |
199 } | 200 } |
200 | 201 |
201 TEST_F(ServiceWorkerControlleeRequestHandlerTest, ActivateWaitingVersion) { | 202 TEST_F(ServiceWorkerControlleeRequestHandlerTest, ActivateWaitingVersion) { |
202 // Store a registration that is installed but not activated yet. | 203 // Store a registration that is installed but not activated yet. |
| 204 version_->set_fetch_handler_existence( |
| 205 ServiceWorkerVersion::FetchHandlerExistence::EXISTS); |
203 version_->SetStatus(ServiceWorkerVersion::INSTALLED); | 206 version_->SetStatus(ServiceWorkerVersion::INSTALLED); |
204 version_->set_has_fetch_handler(true); | |
205 registration_->SetWaitingVersion(version_); | 207 registration_->SetWaitingVersion(version_); |
206 context()->storage()->StoreRegistration( | 208 context()->storage()->StoreRegistration( |
207 registration_.get(), | 209 registration_.get(), |
208 version_.get(), | 210 version_.get(), |
209 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 211 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
210 base::RunLoop().RunUntilIdle(); | 212 base::RunLoop().RunUntilIdle(); |
211 | 213 |
212 // Conduct a main resource load. | 214 // Conduct a main resource load. |
213 ServiceWorkerRequestTestResources test_resources( | 215 ServiceWorkerRequestTestResources test_resources( |
214 this, GURL("https://host/scope/doc"), RESOURCE_TYPE_MAIN_FRAME); | 216 this, GURL("https://host/scope/doc"), RESOURCE_TYPE_MAIN_FRAME); |
(...skipping 13 matching lines...) Expand all Loading... |
228 | 230 |
229 // Navigations should trigger an update too. | 231 // Navigations should trigger an update too. |
230 test_resources.ResetHandler(); | 232 test_resources.ResetHandler(); |
231 EXPECT_TRUE(version_->update_timer_.IsRunning()); | 233 EXPECT_TRUE(version_->update_timer_.IsRunning()); |
232 } | 234 } |
233 | 235 |
234 // Test that an installing registration is associated with a provider host. | 236 // Test that an installing registration is associated with a provider host. |
235 TEST_F(ServiceWorkerControlleeRequestHandlerTest, InstallingRegistration) { | 237 TEST_F(ServiceWorkerControlleeRequestHandlerTest, InstallingRegistration) { |
236 // Create an installing registration. | 238 // Create an installing registration. |
237 version_->SetStatus(ServiceWorkerVersion::INSTALLING); | 239 version_->SetStatus(ServiceWorkerVersion::INSTALLING); |
238 version_->set_has_fetch_handler(true); | 240 version_->set_fetch_handler_existence( |
| 241 ServiceWorkerVersion::FetchHandlerExistence::EXISTS); |
239 registration_->SetInstallingVersion(version_); | 242 registration_->SetInstallingVersion(version_); |
240 context()->storage()->NotifyInstallingRegistration(registration_.get()); | 243 context()->storage()->NotifyInstallingRegistration(registration_.get()); |
241 | 244 |
242 // Conduct a main resource load. | 245 // Conduct a main resource load. |
243 ServiceWorkerRequestTestResources test_resources( | 246 ServiceWorkerRequestTestResources test_resources( |
244 this, GURL("https://host/scope/doc"), RESOURCE_TYPE_MAIN_FRAME); | 247 this, GURL("https://host/scope/doc"), RESOURCE_TYPE_MAIN_FRAME); |
245 ServiceWorkerURLRequestJob* job = test_resources.MaybeCreateJob(); | 248 ServiceWorkerURLRequestJob* job = test_resources.MaybeCreateJob(); |
246 | 249 |
247 base::RunLoop().RunUntilIdle(); | 250 base::RunLoop().RunUntilIdle(); |
248 | 251 |
249 // The handler should have fallen back to network and destroyed the job. The | 252 // The handler should have fallen back to network and destroyed the job. The |
250 // registration should be associated with the provider host, although it is | 253 // registration should be associated with the provider host, although it is |
251 // not controlled since there is no active version. | 254 // not controlled since there is no active version. |
252 EXPECT_FALSE(job); | 255 EXPECT_FALSE(job); |
253 EXPECT_EQ(registration_.get(), provider_host_->associated_registration()); | 256 EXPECT_EQ(registration_.get(), provider_host_->associated_registration()); |
254 EXPECT_EQ(version_.get(), provider_host_->installing_version()); | 257 EXPECT_EQ(version_.get(), provider_host_->installing_version()); |
255 EXPECT_FALSE(version_->HasControllee()); | 258 EXPECT_FALSE(version_->HasControllee()); |
256 EXPECT_FALSE(provider_host_->controlling_version()); | 259 EXPECT_FALSE(provider_host_->controlling_version()); |
257 } | 260 } |
258 | 261 |
259 // Test to not regress crbug/414118. | 262 // Test to not regress crbug/414118. |
260 TEST_F(ServiceWorkerControlleeRequestHandlerTest, DeletedProviderHost) { | 263 TEST_F(ServiceWorkerControlleeRequestHandlerTest, DeletedProviderHost) { |
261 // Store a registration so the call to FindRegistrationForDocument will read | 264 // Store a registration so the call to FindRegistrationForDocument will read |
262 // from the database. | 265 // from the database. |
| 266 version_->set_fetch_handler_existence( |
| 267 ServiceWorkerVersion::FetchHandlerExistence::EXISTS); |
263 version_->SetStatus(ServiceWorkerVersion::ACTIVATED); | 268 version_->SetStatus(ServiceWorkerVersion::ACTIVATED); |
264 version_->set_has_fetch_handler(true); | |
265 registration_->SetActiveVersion(version_); | 269 registration_->SetActiveVersion(version_); |
266 context()->storage()->StoreRegistration( | 270 context()->storage()->StoreRegistration( |
267 registration_.get(), | 271 registration_.get(), |
268 version_.get(), | 272 version_.get(), |
269 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 273 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
270 base::RunLoop().RunUntilIdle(); | 274 base::RunLoop().RunUntilIdle(); |
271 version_ = NULL; | 275 version_ = NULL; |
272 registration_ = NULL; | 276 registration_ = NULL; |
273 | 277 |
274 // Conduct a main resource load. | 278 // Conduct a main resource load. |
275 ServiceWorkerRequestTestResources test_resources( | 279 ServiceWorkerRequestTestResources test_resources( |
276 this, GURL("https://host/scope/doc"), RESOURCE_TYPE_MAIN_FRAME); | 280 this, GURL("https://host/scope/doc"), RESOURCE_TYPE_MAIN_FRAME); |
277 ServiceWorkerURLRequestJob* sw_job = test_resources.MaybeCreateJob(); | 281 ServiceWorkerURLRequestJob* sw_job = test_resources.MaybeCreateJob(); |
278 | 282 |
279 EXPECT_FALSE(sw_job->ShouldFallbackToNetwork()); | 283 EXPECT_FALSE(sw_job->ShouldFallbackToNetwork()); |
280 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); | 284 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); |
281 | 285 |
282 // Shouldn't crash if the ProviderHost is deleted prior to completion of | 286 // Shouldn't crash if the ProviderHost is deleted prior to completion of |
283 // the database lookup. | 287 // the database lookup. |
284 context()->RemoveProviderHost(helper_->mock_render_process_id(), | 288 context()->RemoveProviderHost(helper_->mock_render_process_id(), |
285 kMockProviderId); | 289 kMockProviderId); |
286 EXPECT_FALSE(provider_host_.get()); | 290 EXPECT_FALSE(provider_host_.get()); |
287 base::RunLoop().RunUntilIdle(); | 291 base::RunLoop().RunUntilIdle(); |
288 EXPECT_TRUE(sw_job->ShouldFallbackToNetwork()); | 292 EXPECT_TRUE(sw_job->ShouldFallbackToNetwork()); |
289 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); | 293 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); |
290 } | 294 } |
291 | 295 |
292 TEST_F(ServiceWorkerControlleeRequestHandlerTest, FallbackWithNoFetchHandler) { | 296 TEST_F(ServiceWorkerControlleeRequestHandlerTest, FallbackWithNoFetchHandler) { |
| 297 version_->set_fetch_handler_existence( |
| 298 ServiceWorkerVersion::FetchHandlerExistence::DOES_NOT_EXIST); |
293 version_->SetStatus(ServiceWorkerVersion::ACTIVATED); | 299 version_->SetStatus(ServiceWorkerVersion::ACTIVATED); |
294 version_->set_has_fetch_handler(false); | |
295 registration_->SetActiveVersion(version_); | 300 registration_->SetActiveVersion(version_); |
296 context()->storage()->StoreRegistration( | 301 context()->storage()->StoreRegistration( |
297 registration_.get(), version_.get(), | 302 registration_.get(), version_.get(), |
298 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 303 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
299 base::RunLoop().RunUntilIdle(); | 304 base::RunLoop().RunUntilIdle(); |
300 | 305 |
301 ServiceWorkerRequestTestResources main_test_resources( | 306 ServiceWorkerRequestTestResources main_test_resources( |
302 this, GURL("https://host/scope/doc"), RESOURCE_TYPE_MAIN_FRAME); | 307 this, GURL("https://host/scope/doc"), RESOURCE_TYPE_MAIN_FRAME); |
303 ServiceWorkerURLRequestJob* main_job = main_test_resources.MaybeCreateJob(); | 308 ServiceWorkerURLRequestJob* main_job = main_test_resources.MaybeCreateJob(); |
304 | 309 |
(...skipping 27 matching lines...) Expand all Loading... |
332 EXPECT_FALSE(sub_cors_job->ShouldFallbackToNetwork()); | 337 EXPECT_FALSE(sub_cors_job->ShouldFallbackToNetwork()); |
333 EXPECT_FALSE(sub_cors_job->ShouldForwardToServiceWorker()); | 338 EXPECT_FALSE(sub_cors_job->ShouldForwardToServiceWorker()); |
334 | 339 |
335 base::RunLoop().RunUntilIdle(); | 340 base::RunLoop().RunUntilIdle(); |
336 | 341 |
337 EXPECT_FALSE(sub_cors_job->ShouldFallbackToNetwork()); | 342 EXPECT_FALSE(sub_cors_job->ShouldFallbackToNetwork()); |
338 EXPECT_FALSE(sub_cors_job->ShouldForwardToServiceWorker()); | 343 EXPECT_FALSE(sub_cors_job->ShouldForwardToServiceWorker()); |
339 } | 344 } |
340 | 345 |
341 } // namespace content | 346 } // namespace content |
OLD | NEW |