| 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("https://host/scope/"); | 108 scope_ = GURL("http://host/scope/"); |
| 109 script_url_ = GURL("https://host/script.js"); | 109 script_url_ = GURL("http://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( | 122 new ServiceWorkerProviderHost(helper_->mock_render_process_id(), |
| 123 helper_->mock_render_process_id(), MSG_ROUTING_NONE, | 123 MSG_ROUTING_NONE, kMockProviderId, |
| 124 kMockProviderId, SERVICE_WORKER_PROVIDER_FOR_WINDOW, | 124 SERVICE_WORKER_PROVIDER_FOR_WINDOW, |
| 125 ServiceWorkerProviderHost::FrameSecurityLevel::SECURE, | 125 context()->AsWeakPtr(), NULL)); |
| 126 context()->AsWeakPtr(), NULL)); | |
| 127 provider_host_ = host->AsWeakPtr(); | 126 provider_host_ = host->AsWeakPtr(); |
| 128 context()->AddProviderHost(std::move(host)); | 127 context()->AddProviderHost(std::move(host)); |
| 129 | 128 |
| 130 context()->storage()->LazyInitialize(base::Bind(&base::DoNothing)); | 129 context()->storage()->LazyInitialize(base::Bind(&base::DoNothing)); |
| 131 base::RunLoop().RunUntilIdle(); | 130 base::RunLoop().RunUntilIdle(); |
| 132 } | 131 } |
| 133 | 132 |
| 134 void TearDown() override { | 133 void TearDown() override { |
| 135 version_ = NULL; | 134 version_ = NULL; |
| 136 registration_ = NULL; | 135 registration_ = NULL; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 version_->set_has_fetch_handler(true); | 173 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("http://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); | 202 version_->set_has_fetch_handler(true); |
| 204 registration_->SetWaitingVersion(version_); | 203 registration_->SetWaitingVersion(version_); |
| 205 context()->storage()->StoreRegistration( | 204 context()->storage()->StoreRegistration( |
| 206 registration_.get(), | 205 registration_.get(), |
| 207 version_.get(), | 206 version_.get(), |
| 208 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 207 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 209 base::RunLoop().RunUntilIdle(); | 208 base::RunLoop().RunUntilIdle(); |
| 210 | 209 |
| 211 // Conduct a main resource load. | 210 // Conduct a main resource load. |
| 212 ServiceWorkerRequestTestResources test_resources( | 211 ServiceWorkerRequestTestResources test_resources( |
| 213 this, GURL("https://host/scope/doc"), RESOURCE_TYPE_MAIN_FRAME); | 212 this, GURL("http://host/scope/doc"), RESOURCE_TYPE_MAIN_FRAME); |
| 214 ServiceWorkerURLRequestJob* sw_job = test_resources.MaybeCreateJob(); | 213 ServiceWorkerURLRequestJob* sw_job = test_resources.MaybeCreateJob(); |
| 215 | 214 |
| 216 EXPECT_FALSE(sw_job->ShouldFallbackToNetwork()); | 215 EXPECT_FALSE(sw_job->ShouldFallbackToNetwork()); |
| 217 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); | 216 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); |
| 218 EXPECT_FALSE(version_->HasControllee()); | 217 EXPECT_FALSE(version_->HasControllee()); |
| 219 | 218 |
| 220 base::RunLoop().RunUntilIdle(); | 219 base::RunLoop().RunUntilIdle(); |
| 221 | 220 |
| 222 EXPECT_EQ(ServiceWorkerVersion::ACTIVATED, | 221 EXPECT_EQ(ServiceWorkerVersion::ACTIVATED, |
| 223 version_->status()); | 222 version_->status()); |
| 224 EXPECT_FALSE(sw_job->ShouldFallbackToNetwork()); | 223 EXPECT_FALSE(sw_job->ShouldFallbackToNetwork()); |
| 225 EXPECT_TRUE(sw_job->ShouldForwardToServiceWorker()); | 224 EXPECT_TRUE(sw_job->ShouldForwardToServiceWorker()); |
| 226 EXPECT_TRUE(version_->HasControllee()); | 225 EXPECT_TRUE(version_->HasControllee()); |
| 227 | 226 |
| 228 // Navigations should trigger an update too. | 227 // Navigations should trigger an update too. |
| 229 test_resources.ResetHandler(); | 228 test_resources.ResetHandler(); |
| 230 EXPECT_TRUE(version_->update_timer_.IsRunning()); | 229 EXPECT_TRUE(version_->update_timer_.IsRunning()); |
| 231 } | 230 } |
| 232 | 231 |
| 233 // Test that an installing registration is associated with a provider host. | 232 // Test that an installing registration is associated with a provider host. |
| 234 TEST_F(ServiceWorkerControlleeRequestHandlerTest, InstallingRegistration) { | 233 TEST_F(ServiceWorkerControlleeRequestHandlerTest, InstallingRegistration) { |
| 235 // Create an installing registration. | 234 // Create an installing registration. |
| 236 version_->SetStatus(ServiceWorkerVersion::INSTALLING); | 235 version_->SetStatus(ServiceWorkerVersion::INSTALLING); |
| 237 version_->set_has_fetch_handler(true); | 236 version_->set_has_fetch_handler(true); |
| 238 registration_->SetInstallingVersion(version_); | 237 registration_->SetInstallingVersion(version_); |
| 239 context()->storage()->NotifyInstallingRegistration(registration_.get()); | 238 context()->storage()->NotifyInstallingRegistration(registration_.get()); |
| 240 | 239 |
| 241 // Conduct a main resource load. | 240 // Conduct a main resource load. |
| 242 ServiceWorkerRequestTestResources test_resources( | 241 ServiceWorkerRequestTestResources test_resources( |
| 243 this, GURL("https://host/scope/doc"), RESOURCE_TYPE_MAIN_FRAME); | 242 this, GURL("http://host/scope/doc"), RESOURCE_TYPE_MAIN_FRAME); |
| 244 ServiceWorkerURLRequestJob* job = test_resources.MaybeCreateJob(); | 243 ServiceWorkerURLRequestJob* job = test_resources.MaybeCreateJob(); |
| 245 | 244 |
| 246 base::RunLoop().RunUntilIdle(); | 245 base::RunLoop().RunUntilIdle(); |
| 247 | 246 |
| 248 // The handler should have fallen back to network and destroyed the job. The | 247 // 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 | 248 // registration should be associated with the provider host, although it is |
| 250 // not controlled since there is no active version. | 249 // not controlled since there is no active version. |
| 251 EXPECT_FALSE(job); | 250 EXPECT_FALSE(job); |
| 252 EXPECT_EQ(registration_.get(), provider_host_->associated_registration()); | 251 EXPECT_EQ(registration_.get(), provider_host_->associated_registration()); |
| 253 EXPECT_EQ(version_.get(), provider_host_->installing_version()); | 252 EXPECT_EQ(version_.get(), provider_host_->installing_version()); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 265 context()->storage()->StoreRegistration( | 264 context()->storage()->StoreRegistration( |
| 266 registration_.get(), | 265 registration_.get(), |
| 267 version_.get(), | 266 version_.get(), |
| 268 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 267 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 269 base::RunLoop().RunUntilIdle(); | 268 base::RunLoop().RunUntilIdle(); |
| 270 version_ = NULL; | 269 version_ = NULL; |
| 271 registration_ = NULL; | 270 registration_ = NULL; |
| 272 | 271 |
| 273 // Conduct a main resource load. | 272 // Conduct a main resource load. |
| 274 ServiceWorkerRequestTestResources test_resources( | 273 ServiceWorkerRequestTestResources test_resources( |
| 275 this, GURL("https://host/scope/doc"), RESOURCE_TYPE_MAIN_FRAME); | 274 this, GURL("http://host/scope/doc"), RESOURCE_TYPE_MAIN_FRAME); |
| 276 ServiceWorkerURLRequestJob* sw_job = test_resources.MaybeCreateJob(); | 275 ServiceWorkerURLRequestJob* sw_job = test_resources.MaybeCreateJob(); |
| 277 | 276 |
| 278 EXPECT_FALSE(sw_job->ShouldFallbackToNetwork()); | 277 EXPECT_FALSE(sw_job->ShouldFallbackToNetwork()); |
| 279 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); | 278 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); |
| 280 | 279 |
| 281 // Shouldn't crash if the ProviderHost is deleted prior to completion of | 280 // Shouldn't crash if the ProviderHost is deleted prior to completion of |
| 282 // the database lookup. | 281 // the database lookup. |
| 283 context()->RemoveProviderHost(helper_->mock_render_process_id(), | 282 context()->RemoveProviderHost(helper_->mock_render_process_id(), |
| 284 kMockProviderId); | 283 kMockProviderId); |
| 285 EXPECT_FALSE(provider_host_.get()); | 284 EXPECT_FALSE(provider_host_.get()); |
| 286 base::RunLoop().RunUntilIdle(); | 285 base::RunLoop().RunUntilIdle(); |
| 287 EXPECT_TRUE(sw_job->ShouldFallbackToNetwork()); | 286 EXPECT_TRUE(sw_job->ShouldFallbackToNetwork()); |
| 288 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); | 287 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); |
| 289 } | 288 } |
| 290 | 289 |
| 291 TEST_F(ServiceWorkerControlleeRequestHandlerTest, FallbackWithNoFetchHandler) { | 290 TEST_F(ServiceWorkerControlleeRequestHandlerTest, FallbackWithNoFetchHandler) { |
| 292 version_->SetStatus(ServiceWorkerVersion::ACTIVATED); | 291 version_->SetStatus(ServiceWorkerVersion::ACTIVATED); |
| 293 version_->set_has_fetch_handler(false); | 292 version_->set_has_fetch_handler(false); |
| 294 registration_->SetActiveVersion(version_); | 293 registration_->SetActiveVersion(version_); |
| 295 context()->storage()->StoreRegistration( | 294 context()->storage()->StoreRegistration( |
| 296 registration_.get(), version_.get(), | 295 registration_.get(), version_.get(), |
| 297 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 296 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 298 base::RunLoop().RunUntilIdle(); | 297 base::RunLoop().RunUntilIdle(); |
| 299 | 298 |
| 300 ServiceWorkerRequestTestResources main_test_resources( | 299 ServiceWorkerRequestTestResources main_test_resources( |
| 301 this, GURL("https://host/scope/doc"), RESOURCE_TYPE_MAIN_FRAME); | 300 this, GURL("http://host/scope/doc"), RESOURCE_TYPE_MAIN_FRAME); |
| 302 ServiceWorkerURLRequestJob* main_job = main_test_resources.MaybeCreateJob(); | 301 ServiceWorkerURLRequestJob* main_job = main_test_resources.MaybeCreateJob(); |
| 303 | 302 |
| 304 EXPECT_FALSE(main_job->ShouldFallbackToNetwork()); | 303 EXPECT_FALSE(main_job->ShouldFallbackToNetwork()); |
| 305 EXPECT_FALSE(main_job->ShouldForwardToServiceWorker()); | 304 EXPECT_FALSE(main_job->ShouldForwardToServiceWorker()); |
| 306 EXPECT_FALSE(version_->HasControllee()); | 305 EXPECT_FALSE(version_->HasControllee()); |
| 307 | 306 |
| 308 base::RunLoop().RunUntilIdle(); | 307 base::RunLoop().RunUntilIdle(); |
| 309 | 308 |
| 310 EXPECT_TRUE(main_job->ShouldFallbackToNetwork()); | 309 EXPECT_TRUE(main_job->ShouldFallbackToNetwork()); |
| 311 EXPECT_FALSE(main_job->ShouldForwardToServiceWorker()); | 310 EXPECT_FALSE(main_job->ShouldForwardToServiceWorker()); |
| 312 EXPECT_TRUE(version_->HasControllee()); | 311 EXPECT_TRUE(version_->HasControllee()); |
| 313 EXPECT_EQ(version_, provider_host_->controlling_version()); | 312 EXPECT_EQ(version_, provider_host_->controlling_version()); |
| 314 | 313 |
| 315 ServiceWorkerRequestTestResources sub_test_resources( | 314 ServiceWorkerRequestTestResources sub_test_resources( |
| 316 this, GURL("https://host/scope/doc/subresource"), RESOURCE_TYPE_IMAGE); | 315 this, GURL("http://host/scope/doc/subresource"), RESOURCE_TYPE_IMAGE); |
| 317 ServiceWorkerURLRequestJob* sub_job = sub_test_resources.MaybeCreateJob(); | 316 ServiceWorkerURLRequestJob* sub_job = sub_test_resources.MaybeCreateJob(); |
| 318 | 317 |
| 319 // This job shouldn't be created because this worker doesn't have fetch | 318 // This job shouldn't be created because this worker doesn't have fetch |
| 320 // handler. | 319 // handler. |
| 321 EXPECT_EQ(nullptr, sub_job); | 320 EXPECT_EQ(nullptr, sub_job); |
| 322 } | 321 } |
| 323 | 322 |
| 324 } // namespace content | 323 } // namespace content |
| OLD | NEW |