| 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 } | 164 } |
| 165 }; | 165 }; |
| 166 | 166 |
| 167 TEST_F(ServiceWorkerControlleeRequestHandlerTest, DisallowServiceWorker) { | 167 TEST_F(ServiceWorkerControlleeRequestHandlerTest, DisallowServiceWorker) { |
| 168 ServiceWorkerTestContentBrowserClient test_browser_client; | 168 ServiceWorkerTestContentBrowserClient test_browser_client; |
| 169 ContentBrowserClient* old_browser_client = | 169 ContentBrowserClient* old_browser_client = |
| 170 SetBrowserClientForTesting(&test_browser_client); | 170 SetBrowserClientForTesting(&test_browser_client); |
| 171 | 171 |
| 172 // Store an activated worker. | 172 // Store an activated worker. |
| 173 version_->SetStatus(ServiceWorkerVersion::ACTIVATED); | 173 version_->SetStatus(ServiceWorkerVersion::ACTIVATED); |
| 174 version_->set_has_fetch_handler(true); | |
| 175 registration_->SetActiveVersion(version_); | 174 registration_->SetActiveVersion(version_); |
| 176 context()->storage()->StoreRegistration( | 175 context()->storage()->StoreRegistration( |
| 177 registration_.get(), | 176 registration_.get(), |
| 178 version_.get(), | 177 version_.get(), |
| 179 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 178 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 180 base::RunLoop().RunUntilIdle(); | 179 base::RunLoop().RunUntilIdle(); |
| 181 | 180 |
| 182 // Conduct a main resource load. | 181 // Conduct a main resource load. |
| 183 ServiceWorkerRequestTestResources test_resources( | 182 ServiceWorkerRequestTestResources test_resources( |
| 184 this, GURL("https://host/scope/doc"), RESOURCE_TYPE_MAIN_FRAME); | 183 this, GURL("https://host/scope/doc"), RESOURCE_TYPE_MAIN_FRAME); |
| 185 ServiceWorkerURLRequestJob* sw_job = test_resources.MaybeCreateJob(); | 184 ServiceWorkerURLRequestJob* sw_job = test_resources.MaybeCreateJob(); |
| 186 | 185 |
| 187 EXPECT_FALSE(sw_job->ShouldFallbackToNetwork()); | 186 EXPECT_FALSE(sw_job->ShouldFallbackToNetwork()); |
| 188 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); | 187 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); |
| 189 EXPECT_FALSE(version_->HasControllee()); | 188 EXPECT_FALSE(version_->HasControllee()); |
| 190 base::RunLoop().RunUntilIdle(); | 189 base::RunLoop().RunUntilIdle(); |
| 191 | 190 |
| 192 // Verify we did not use the worker. | 191 // Verify we did not use the worker. |
| 193 EXPECT_TRUE(sw_job->ShouldFallbackToNetwork()); | 192 EXPECT_TRUE(sw_job->ShouldFallbackToNetwork()); |
| 194 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); | 193 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); |
| 195 EXPECT_FALSE(version_->HasControllee()); | 194 EXPECT_FALSE(version_->HasControllee()); |
| 196 | 195 |
| 197 SetBrowserClientForTesting(old_browser_client); | 196 SetBrowserClientForTesting(old_browser_client); |
| 198 } | 197 } |
| 199 | 198 |
| 200 TEST_F(ServiceWorkerControlleeRequestHandlerTest, ActivateWaitingVersion) { | 199 TEST_F(ServiceWorkerControlleeRequestHandlerTest, ActivateWaitingVersion) { |
| 201 // Store a registration that is installed but not activated yet. | 200 // Store a registration that is installed but not activated yet. |
| 202 version_->SetStatus(ServiceWorkerVersion::INSTALLED); | 201 version_->SetStatus(ServiceWorkerVersion::INSTALLED); |
| 203 version_->set_has_fetch_handler(true); | |
| 204 registration_->SetWaitingVersion(version_); | 202 registration_->SetWaitingVersion(version_); |
| 205 context()->storage()->StoreRegistration( | 203 context()->storage()->StoreRegistration( |
| 206 registration_.get(), | 204 registration_.get(), |
| 207 version_.get(), | 205 version_.get(), |
| 208 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 206 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 209 base::RunLoop().RunUntilIdle(); | 207 base::RunLoop().RunUntilIdle(); |
| 210 | 208 |
| 211 // Conduct a main resource load. | 209 // Conduct a main resource load. |
| 212 ServiceWorkerRequestTestResources test_resources( | 210 ServiceWorkerRequestTestResources test_resources( |
| 213 this, GURL("https://host/scope/doc"), RESOURCE_TYPE_MAIN_FRAME); | 211 this, GURL("https://host/scope/doc"), RESOURCE_TYPE_MAIN_FRAME); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 227 | 225 |
| 228 // Navigations should trigger an update too. | 226 // Navigations should trigger an update too. |
| 229 test_resources.ResetHandler(); | 227 test_resources.ResetHandler(); |
| 230 EXPECT_TRUE(version_->update_timer_.IsRunning()); | 228 EXPECT_TRUE(version_->update_timer_.IsRunning()); |
| 231 } | 229 } |
| 232 | 230 |
| 233 // Test that an installing registration is associated with a provider host. | 231 // Test that an installing registration is associated with a provider host. |
| 234 TEST_F(ServiceWorkerControlleeRequestHandlerTest, InstallingRegistration) { | 232 TEST_F(ServiceWorkerControlleeRequestHandlerTest, InstallingRegistration) { |
| 235 // Create an installing registration. | 233 // Create an installing registration. |
| 236 version_->SetStatus(ServiceWorkerVersion::INSTALLING); | 234 version_->SetStatus(ServiceWorkerVersion::INSTALLING); |
| 237 version_->set_has_fetch_handler(true); | |
| 238 registration_->SetInstallingVersion(version_); | 235 registration_->SetInstallingVersion(version_); |
| 239 context()->storage()->NotifyInstallingRegistration(registration_.get()); | 236 context()->storage()->NotifyInstallingRegistration(registration_.get()); |
| 240 | 237 |
| 241 // Conduct a main resource load. | 238 // Conduct a main resource load. |
| 242 ServiceWorkerRequestTestResources test_resources( | 239 ServiceWorkerRequestTestResources test_resources( |
| 243 this, GURL("https://host/scope/doc"), RESOURCE_TYPE_MAIN_FRAME); | 240 this, GURL("https://host/scope/doc"), RESOURCE_TYPE_MAIN_FRAME); |
| 244 ServiceWorkerURLRequestJob* job = test_resources.MaybeCreateJob(); | 241 ServiceWorkerURLRequestJob* job = test_resources.MaybeCreateJob(); |
| 245 | 242 |
| 246 base::RunLoop().RunUntilIdle(); | 243 base::RunLoop().RunUntilIdle(); |
| 247 | 244 |
| 248 // The handler should have fallen back to network and destroyed the job. The | 245 // The handler should have fallen back to network and destroyed the job. The |
| 249 // registration should be associated with the provider host, although it is | 246 // registration should be associated with the provider host, although it is |
| 250 // not controlled since there is no active version. | 247 // not controlled since there is no active version. |
| 251 EXPECT_FALSE(job); | 248 EXPECT_FALSE(job); |
| 252 EXPECT_EQ(registration_.get(), provider_host_->associated_registration()); | 249 EXPECT_EQ(registration_.get(), provider_host_->associated_registration()); |
| 253 EXPECT_EQ(version_.get(), provider_host_->installing_version()); | 250 EXPECT_EQ(version_.get(), provider_host_->installing_version()); |
| 254 EXPECT_FALSE(version_->HasControllee()); | 251 EXPECT_FALSE(version_->HasControllee()); |
| 255 EXPECT_FALSE(provider_host_->controlling_version()); | 252 EXPECT_FALSE(provider_host_->controlling_version()); |
| 256 } | 253 } |
| 257 | 254 |
| 258 // Test to not regress crbug/414118. | 255 // Test to not regress crbug/414118. |
| 259 TEST_F(ServiceWorkerControlleeRequestHandlerTest, DeletedProviderHost) { | 256 TEST_F(ServiceWorkerControlleeRequestHandlerTest, DeletedProviderHost) { |
| 260 // Store a registration so the call to FindRegistrationForDocument will read | 257 // Store a registration so the call to FindRegistrationForDocument will read |
| 261 // from the database. | 258 // from the database. |
| 262 version_->SetStatus(ServiceWorkerVersion::ACTIVATED); | 259 version_->SetStatus(ServiceWorkerVersion::ACTIVATED); |
| 263 version_->set_has_fetch_handler(true); | |
| 264 registration_->SetActiveVersion(version_); | 260 registration_->SetActiveVersion(version_); |
| 265 context()->storage()->StoreRegistration( | 261 context()->storage()->StoreRegistration( |
| 266 registration_.get(), | 262 registration_.get(), |
| 267 version_.get(), | 263 version_.get(), |
| 268 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 264 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 269 base::RunLoop().RunUntilIdle(); | 265 base::RunLoop().RunUntilIdle(); |
| 270 version_ = NULL; | 266 version_ = NULL; |
| 271 registration_ = NULL; | 267 registration_ = NULL; |
| 272 | 268 |
| 273 // Conduct a main resource load. | 269 // Conduct a main resource load. |
| 274 ServiceWorkerRequestTestResources test_resources( | 270 ServiceWorkerRequestTestResources test_resources( |
| 275 this, GURL("https://host/scope/doc"), RESOURCE_TYPE_MAIN_FRAME); | 271 this, GURL("https://host/scope/doc"), RESOURCE_TYPE_MAIN_FRAME); |
| 276 ServiceWorkerURLRequestJob* sw_job = test_resources.MaybeCreateJob(); | 272 ServiceWorkerURLRequestJob* sw_job = test_resources.MaybeCreateJob(); |
| 277 | 273 |
| 278 EXPECT_FALSE(sw_job->ShouldFallbackToNetwork()); | 274 EXPECT_FALSE(sw_job->ShouldFallbackToNetwork()); |
| 279 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); | 275 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); |
| 280 | 276 |
| 281 // Shouldn't crash if the ProviderHost is deleted prior to completion of | 277 // Shouldn't crash if the ProviderHost is deleted prior to completion of |
| 282 // the database lookup. | 278 // the database lookup. |
| 283 context()->RemoveProviderHost(helper_->mock_render_process_id(), | 279 context()->RemoveProviderHost(helper_->mock_render_process_id(), |
| 284 kMockProviderId); | 280 kMockProviderId); |
| 285 EXPECT_FALSE(provider_host_.get()); | 281 EXPECT_FALSE(provider_host_.get()); |
| 286 base::RunLoop().RunUntilIdle(); | 282 base::RunLoop().RunUntilIdle(); |
| 287 EXPECT_TRUE(sw_job->ShouldFallbackToNetwork()); | 283 EXPECT_TRUE(sw_job->ShouldFallbackToNetwork()); |
| 288 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); | 284 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); |
| 289 } | 285 } |
| 290 | 286 |
| 291 TEST_F(ServiceWorkerControlleeRequestHandlerTest, FallbackWithNoFetchHandler) { | |
| 292 version_->SetStatus(ServiceWorkerVersion::ACTIVATED); | |
| 293 version_->set_has_fetch_handler(false); | |
| 294 registration_->SetActiveVersion(version_); | |
| 295 context()->storage()->StoreRegistration( | |
| 296 registration_.get(), version_.get(), | |
| 297 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | |
| 298 base::RunLoop().RunUntilIdle(); | |
| 299 | |
| 300 ServiceWorkerRequestTestResources main_test_resources( | |
| 301 this, GURL("https://host/scope/doc"), RESOURCE_TYPE_MAIN_FRAME); | |
| 302 ServiceWorkerURLRequestJob* main_job = main_test_resources.MaybeCreateJob(); | |
| 303 | |
| 304 EXPECT_FALSE(main_job->ShouldFallbackToNetwork()); | |
| 305 EXPECT_FALSE(main_job->ShouldForwardToServiceWorker()); | |
| 306 EXPECT_FALSE(version_->HasControllee()); | |
| 307 | |
| 308 base::RunLoop().RunUntilIdle(); | |
| 309 | |
| 310 EXPECT_TRUE(main_job->ShouldFallbackToNetwork()); | |
| 311 EXPECT_FALSE(main_job->ShouldForwardToServiceWorker()); | |
| 312 EXPECT_TRUE(version_->HasControllee()); | |
| 313 EXPECT_EQ(version_, provider_host_->controlling_version()); | |
| 314 | |
| 315 ServiceWorkerRequestTestResources sub_test_resources( | |
| 316 this, GURL("https://host/scope/doc/subresource"), RESOURCE_TYPE_IMAGE); | |
| 317 ServiceWorkerURLRequestJob* sub_job = sub_test_resources.MaybeCreateJob(); | |
| 318 | |
| 319 // This job shouldn't be created because this worker doesn't have fetch | |
| 320 // handler. | |
| 321 EXPECT_EQ(nullptr, sub_job); | |
| 322 } | |
| 323 | |
| 324 } // namespace content | 287 } // namespace content |
| OLD | NEW |