| 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 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} | 98 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} |
| 99 | 99 |
| 100 void SetUp() override { | 100 void SetUp() override { |
| 101 SetUpWithHelper(new EmbeddedWorkerTestHelper(base::FilePath())); | 101 SetUpWithHelper(new EmbeddedWorkerTestHelper(base::FilePath())); |
| 102 } | 102 } |
| 103 | 103 |
| 104 void SetUpWithHelper(EmbeddedWorkerTestHelper* helper) { | 104 void SetUpWithHelper(EmbeddedWorkerTestHelper* helper) { |
| 105 helper_.reset(helper); | 105 helper_.reset(helper); |
| 106 | 106 |
| 107 // A new unstored registration/version. | 107 // A new unstored registration/version. |
| 108 scope_ = GURL("http://host/scope/"); | 108 scope_ = GURL("https://host/scope/"); |
| 109 script_url_ = GURL("http://host/script.js"); | 109 script_url_ = GURL("https://host/script.js"); |
| 110 registration_ = new ServiceWorkerRegistration( | 110 registration_ = new ServiceWorkerRegistration( |
| 111 scope_, 1L, context()->AsWeakPtr()); | 111 scope_, 1L, context()->AsWeakPtr()); |
| 112 version_ = new ServiceWorkerVersion( | 112 version_ = new ServiceWorkerVersion( |
| 113 registration_.get(), script_url_, 1L, context()->AsWeakPtr()); | 113 registration_.get(), script_url_, 1L, context()->AsWeakPtr()); |
| 114 | 114 |
| 115 std::vector<ServiceWorkerDatabase::ResourceRecord> records; | 115 std::vector<ServiceWorkerDatabase::ResourceRecord> records; |
| 116 records.push_back( | 116 records.push_back( |
| 117 ServiceWorkerDatabase::ResourceRecord(10, version_->script_url(), 100)); | 117 ServiceWorkerDatabase::ResourceRecord(10, version_->script_url(), 100)); |
| 118 version_->script_cache_map()->SetResources(records); | 118 version_->script_cache_map()->SetResources(records); |
| 119 | 119 |
| 120 // An empty host. | 120 // An empty host. |
| 121 std::unique_ptr<ServiceWorkerProviderHost> host( | 121 std::unique_ptr<ServiceWorkerProviderHost> host( |
| 122 new ServiceWorkerProviderHost(helper_->mock_render_process_id(), | 122 new ServiceWorkerProviderHost( |
| 123 MSG_ROUTING_NONE, kMockProviderId, | 123 helper_->mock_render_process_id(), MSG_ROUTING_NONE, |
| 124 SERVICE_WORKER_PROVIDER_FOR_WINDOW, | 124 kMockProviderId, SERVICE_WORKER_PROVIDER_FOR_WINDOW, |
| 125 context()->AsWeakPtr(), NULL)); | 125 ServiceWorkerProviderHost::FrameSecurityLevel::SECURE, |
| 126 context()->AsWeakPtr(), NULL)); |
| 126 provider_host_ = host->AsWeakPtr(); | 127 provider_host_ = host->AsWeakPtr(); |
| 127 context()->AddProviderHost(std::move(host)); | 128 context()->AddProviderHost(std::move(host)); |
| 128 | 129 |
| 129 context()->storage()->LazyInitialize(base::Bind(&base::DoNothing)); | 130 context()->storage()->LazyInitialize(base::Bind(&base::DoNothing)); |
| 130 base::RunLoop().RunUntilIdle(); | 131 base::RunLoop().RunUntilIdle(); |
| 131 } | 132 } |
| 132 | 133 |
| 133 void TearDown() override { | 134 void TearDown() override { |
| 134 version_ = NULL; | 135 version_ = NULL; |
| 135 registration_ = NULL; | 136 registration_ = NULL; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 version_->set_has_fetch_handler(true); | 174 version_->set_has_fetch_handler(true); |
| 174 registration_->SetActiveVersion(version_); | 175 registration_->SetActiveVersion(version_); |
| 175 context()->storage()->StoreRegistration( | 176 context()->storage()->StoreRegistration( |
| 176 registration_.get(), | 177 registration_.get(), |
| 177 version_.get(), | 178 version_.get(), |
| 178 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 179 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 179 base::RunLoop().RunUntilIdle(); | 180 base::RunLoop().RunUntilIdle(); |
| 180 | 181 |
| 181 // Conduct a main resource load. | 182 // Conduct a main resource load. |
| 182 ServiceWorkerRequestTestResources test_resources( | 183 ServiceWorkerRequestTestResources test_resources( |
| 183 this, GURL("http://host/scope/doc"), RESOURCE_TYPE_MAIN_FRAME); | 184 this, GURL("https://host/scope/doc"), RESOURCE_TYPE_MAIN_FRAME); |
| 184 ServiceWorkerURLRequestJob* sw_job = test_resources.MaybeCreateJob(); | 185 ServiceWorkerURLRequestJob* sw_job = test_resources.MaybeCreateJob(); |
| 185 | 186 |
| 186 EXPECT_FALSE(sw_job->ShouldFallbackToNetwork()); | 187 EXPECT_FALSE(sw_job->ShouldFallbackToNetwork()); |
| 187 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); | 188 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); |
| 188 EXPECT_FALSE(version_->HasControllee()); | 189 EXPECT_FALSE(version_->HasControllee()); |
| 189 base::RunLoop().RunUntilIdle(); | 190 base::RunLoop().RunUntilIdle(); |
| 190 | 191 |
| 191 // Verify we did not use the worker. | 192 // Verify we did not use the worker. |
| 192 EXPECT_TRUE(sw_job->ShouldFallbackToNetwork()); | 193 EXPECT_TRUE(sw_job->ShouldFallbackToNetwork()); |
| 193 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); | 194 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); |
| 194 EXPECT_FALSE(version_->HasControllee()); | 195 EXPECT_FALSE(version_->HasControllee()); |
| 195 | 196 |
| 196 SetBrowserClientForTesting(old_browser_client); | 197 SetBrowserClientForTesting(old_browser_client); |
| 197 } | 198 } |
| 198 | 199 |
| 199 TEST_F(ServiceWorkerControlleeRequestHandlerTest, ActivateWaitingVersion) { | 200 TEST_F(ServiceWorkerControlleeRequestHandlerTest, ActivateWaitingVersion) { |
| 200 // Store a registration that is installed but not activated yet. | 201 // Store a registration that is installed but not activated yet. |
| 201 version_->SetStatus(ServiceWorkerVersion::INSTALLED); | 202 version_->SetStatus(ServiceWorkerVersion::INSTALLED); |
| 202 version_->set_has_fetch_handler(true); | 203 version_->set_has_fetch_handler(true); |
| 203 registration_->SetWaitingVersion(version_); | 204 registration_->SetWaitingVersion(version_); |
| 204 context()->storage()->StoreRegistration( | 205 context()->storage()->StoreRegistration( |
| 205 registration_.get(), | 206 registration_.get(), |
| 206 version_.get(), | 207 version_.get(), |
| 207 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 208 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 208 base::RunLoop().RunUntilIdle(); | 209 base::RunLoop().RunUntilIdle(); |
| 209 | 210 |
| 210 // Conduct a main resource load. | 211 // Conduct a main resource load. |
| 211 ServiceWorkerRequestTestResources test_resources( | 212 ServiceWorkerRequestTestResources test_resources( |
| 212 this, GURL("http://host/scope/doc"), RESOURCE_TYPE_MAIN_FRAME); | 213 this, GURL("https://host/scope/doc"), RESOURCE_TYPE_MAIN_FRAME); |
| 213 ServiceWorkerURLRequestJob* sw_job = test_resources.MaybeCreateJob(); | 214 ServiceWorkerURLRequestJob* sw_job = test_resources.MaybeCreateJob(); |
| 214 | 215 |
| 215 EXPECT_FALSE(sw_job->ShouldFallbackToNetwork()); | 216 EXPECT_FALSE(sw_job->ShouldFallbackToNetwork()); |
| 216 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); | 217 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); |
| 217 EXPECT_FALSE(version_->HasControllee()); | 218 EXPECT_FALSE(version_->HasControllee()); |
| 218 | 219 |
| 219 base::RunLoop().RunUntilIdle(); | 220 base::RunLoop().RunUntilIdle(); |
| 220 | 221 |
| 221 EXPECT_EQ(ServiceWorkerVersion::ACTIVATED, | 222 EXPECT_EQ(ServiceWorkerVersion::ACTIVATED, |
| 222 version_->status()); | 223 version_->status()); |
| 223 EXPECT_FALSE(sw_job->ShouldFallbackToNetwork()); | 224 EXPECT_FALSE(sw_job->ShouldFallbackToNetwork()); |
| 224 EXPECT_TRUE(sw_job->ShouldForwardToServiceWorker()); | 225 EXPECT_TRUE(sw_job->ShouldForwardToServiceWorker()); |
| 225 EXPECT_TRUE(version_->HasControllee()); | 226 EXPECT_TRUE(version_->HasControllee()); |
| 226 | 227 |
| 227 // Navigations should trigger an update too. | 228 // Navigations should trigger an update too. |
| 228 test_resources.ResetHandler(); | 229 test_resources.ResetHandler(); |
| 229 EXPECT_TRUE(version_->update_timer_.IsRunning()); | 230 EXPECT_TRUE(version_->update_timer_.IsRunning()); |
| 230 } | 231 } |
| 231 | 232 |
| 232 // Test that an installing registration is associated with a provider host. | 233 // Test that an installing registration is associated with a provider host. |
| 233 TEST_F(ServiceWorkerControlleeRequestHandlerTest, InstallingRegistration) { | 234 TEST_F(ServiceWorkerControlleeRequestHandlerTest, InstallingRegistration) { |
| 234 // Create an installing registration. | 235 // Create an installing registration. |
| 235 version_->SetStatus(ServiceWorkerVersion::INSTALLING); | 236 version_->SetStatus(ServiceWorkerVersion::INSTALLING); |
| 236 version_->set_has_fetch_handler(true); | 237 version_->set_has_fetch_handler(true); |
| 237 registration_->SetInstallingVersion(version_); | 238 registration_->SetInstallingVersion(version_); |
| 238 context()->storage()->NotifyInstallingRegistration(registration_.get()); | 239 context()->storage()->NotifyInstallingRegistration(registration_.get()); |
| 239 | 240 |
| 240 // Conduct a main resource load. | 241 // Conduct a main resource load. |
| 241 ServiceWorkerRequestTestResources test_resources( | 242 ServiceWorkerRequestTestResources test_resources( |
| 242 this, GURL("http://host/scope/doc"), RESOURCE_TYPE_MAIN_FRAME); | 243 this, GURL("https://host/scope/doc"), RESOURCE_TYPE_MAIN_FRAME); |
| 243 ServiceWorkerURLRequestJob* job = test_resources.MaybeCreateJob(); | 244 ServiceWorkerURLRequestJob* job = test_resources.MaybeCreateJob(); |
| 244 | 245 |
| 245 base::RunLoop().RunUntilIdle(); | 246 base::RunLoop().RunUntilIdle(); |
| 246 | 247 |
| 247 // The handler should have fallen back to network and destroyed the job. The | 248 // The handler should have fallen back to network and destroyed the job. The |
| 248 // registration should be associated with the provider host, although it is | 249 // registration should be associated with the provider host, although it is |
| 249 // not controlled since there is no active version. | 250 // not controlled since there is no active version. |
| 250 EXPECT_FALSE(job); | 251 EXPECT_FALSE(job); |
| 251 EXPECT_EQ(registration_.get(), provider_host_->associated_registration()); | 252 EXPECT_EQ(registration_.get(), provider_host_->associated_registration()); |
| 252 EXPECT_EQ(version_.get(), provider_host_->installing_version()); | 253 EXPECT_EQ(version_.get(), provider_host_->installing_version()); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 264 context()->storage()->StoreRegistration( | 265 context()->storage()->StoreRegistration( |
| 265 registration_.get(), | 266 registration_.get(), |
| 266 version_.get(), | 267 version_.get(), |
| 267 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 268 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 268 base::RunLoop().RunUntilIdle(); | 269 base::RunLoop().RunUntilIdle(); |
| 269 version_ = NULL; | 270 version_ = NULL; |
| 270 registration_ = NULL; | 271 registration_ = NULL; |
| 271 | 272 |
| 272 // Conduct a main resource load. | 273 // Conduct a main resource load. |
| 273 ServiceWorkerRequestTestResources test_resources( | 274 ServiceWorkerRequestTestResources test_resources( |
| 274 this, GURL("http://host/scope/doc"), RESOURCE_TYPE_MAIN_FRAME); | 275 this, GURL("https://host/scope/doc"), RESOURCE_TYPE_MAIN_FRAME); |
| 275 ServiceWorkerURLRequestJob* sw_job = test_resources.MaybeCreateJob(); | 276 ServiceWorkerURLRequestJob* sw_job = test_resources.MaybeCreateJob(); |
| 276 | 277 |
| 277 EXPECT_FALSE(sw_job->ShouldFallbackToNetwork()); | 278 EXPECT_FALSE(sw_job->ShouldFallbackToNetwork()); |
| 278 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); | 279 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); |
| 279 | 280 |
| 280 // Shouldn't crash if the ProviderHost is deleted prior to completion of | 281 // Shouldn't crash if the ProviderHost is deleted prior to completion of |
| 281 // the database lookup. | 282 // the database lookup. |
| 282 context()->RemoveProviderHost(helper_->mock_render_process_id(), | 283 context()->RemoveProviderHost(helper_->mock_render_process_id(), |
| 283 kMockProviderId); | 284 kMockProviderId); |
| 284 EXPECT_FALSE(provider_host_.get()); | 285 EXPECT_FALSE(provider_host_.get()); |
| 285 base::RunLoop().RunUntilIdle(); | 286 base::RunLoop().RunUntilIdle(); |
| 286 EXPECT_TRUE(sw_job->ShouldFallbackToNetwork()); | 287 EXPECT_TRUE(sw_job->ShouldFallbackToNetwork()); |
| 287 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); | 288 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); |
| 288 } | 289 } |
| 289 | 290 |
| 290 TEST_F(ServiceWorkerControlleeRequestHandlerTest, FallbackWithNoFetchHandler) { | 291 TEST_F(ServiceWorkerControlleeRequestHandlerTest, FallbackWithNoFetchHandler) { |
| 291 version_->SetStatus(ServiceWorkerVersion::ACTIVATED); | 292 version_->SetStatus(ServiceWorkerVersion::ACTIVATED); |
| 292 version_->set_has_fetch_handler(false); | 293 version_->set_has_fetch_handler(false); |
| 293 registration_->SetActiveVersion(version_); | 294 registration_->SetActiveVersion(version_); |
| 294 context()->storage()->StoreRegistration( | 295 context()->storage()->StoreRegistration( |
| 295 registration_.get(), version_.get(), | 296 registration_.get(), version_.get(), |
| 296 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 297 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 297 base::RunLoop().RunUntilIdle(); | 298 base::RunLoop().RunUntilIdle(); |
| 298 | 299 |
| 299 ServiceWorkerRequestTestResources main_test_resources( | 300 ServiceWorkerRequestTestResources main_test_resources( |
| 300 this, GURL("http://host/scope/doc"), RESOURCE_TYPE_MAIN_FRAME); | 301 this, GURL("https://host/scope/doc"), RESOURCE_TYPE_MAIN_FRAME); |
| 301 ServiceWorkerURLRequestJob* main_job = main_test_resources.MaybeCreateJob(); | 302 ServiceWorkerURLRequestJob* main_job = main_test_resources.MaybeCreateJob(); |
| 302 | 303 |
| 303 EXPECT_FALSE(main_job->ShouldFallbackToNetwork()); | 304 EXPECT_FALSE(main_job->ShouldFallbackToNetwork()); |
| 304 EXPECT_FALSE(main_job->ShouldForwardToServiceWorker()); | 305 EXPECT_FALSE(main_job->ShouldForwardToServiceWorker()); |
| 305 EXPECT_FALSE(version_->HasControllee()); | 306 EXPECT_FALSE(version_->HasControllee()); |
| 306 | 307 |
| 307 base::RunLoop().RunUntilIdle(); | 308 base::RunLoop().RunUntilIdle(); |
| 308 | 309 |
| 309 EXPECT_TRUE(main_job->ShouldFallbackToNetwork()); | 310 EXPECT_TRUE(main_job->ShouldFallbackToNetwork()); |
| 310 EXPECT_FALSE(main_job->ShouldForwardToServiceWorker()); | 311 EXPECT_FALSE(main_job->ShouldForwardToServiceWorker()); |
| 311 EXPECT_TRUE(version_->HasControllee()); | 312 EXPECT_TRUE(version_->HasControllee()); |
| 312 EXPECT_EQ(version_, provider_host_->controlling_version()); | 313 EXPECT_EQ(version_, provider_host_->controlling_version()); |
| 313 | 314 |
| 314 ServiceWorkerRequestTestResources sub_test_resources( | 315 ServiceWorkerRequestTestResources sub_test_resources( |
| 315 this, GURL("http://host/scope/doc/subresource"), RESOURCE_TYPE_IMAGE); | 316 this, GURL("https://host/scope/doc/subresource"), RESOURCE_TYPE_IMAGE); |
| 316 ServiceWorkerURLRequestJob* sub_job = sub_test_resources.MaybeCreateJob(); | 317 ServiceWorkerURLRequestJob* sub_job = sub_test_resources.MaybeCreateJob(); |
| 317 | 318 |
| 318 // This job shouldn't be created because this worker doesn't have fetch | 319 // This job shouldn't be created because this worker doesn't have fetch |
| 319 // handler. | 320 // handler. |
| 320 EXPECT_EQ(nullptr, sub_job); | 321 EXPECT_EQ(nullptr, sub_job); |
| 321 } | 322 } |
| 322 | 323 |
| 323 } // namespace content | 324 } // namespace content |
| OLD | NEW |