| 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 <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 | 7 |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| (...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 ServiceWorkerWriteToCacheJobTest(const std::string& scope, | 281 ServiceWorkerWriteToCacheJobTest(const std::string& scope, |
| 282 const std::string& script_url) | 282 const std::string& script_url) |
| 283 : scope_(scope), | 283 : scope_(scope), |
| 284 script_url_(script_url), | 284 script_url_(script_url), |
| 285 browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), | 285 browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), |
| 286 mock_protocol_handler_(nullptr) {} | 286 mock_protocol_handler_(nullptr) {} |
| 287 ~ServiceWorkerWriteToCacheJobTest() override {} | 287 ~ServiceWorkerWriteToCacheJobTest() override {} |
| 288 | 288 |
| 289 base::WeakPtr<ServiceWorkerProviderHost> CreateHostForVersion( | 289 base::WeakPtr<ServiceWorkerProviderHost> CreateHostForVersion( |
| 290 int process_id, | 290 int process_id, |
| 291 int provider_id, |
| 291 const scoped_refptr<ServiceWorkerVersion>& version) { | 292 const scoped_refptr<ServiceWorkerVersion>& version) { |
| 292 std::unique_ptr<ServiceWorkerProviderHost> host = | 293 std::unique_ptr<ServiceWorkerProviderHost> host = |
| 293 CreateProviderHostForServiceWorkerContext( | 294 CreateProviderHostForServiceWorkerContext( |
| 294 process_id, true /* is_parent_frame_secure */, version.get(), | 295 process_id, provider_id, true /* is_parent_frame_secure */, |
| 295 context()->AsWeakPtr(), &remote_endpoint_); | 296 context()->AsWeakPtr(), &remote_endpoint_); |
| 296 base::WeakPtr<ServiceWorkerProviderHost> host_weakptr = host->AsWeakPtr(); | 297 base::WeakPtr<ServiceWorkerProviderHost> provider_host = host->AsWeakPtr(); |
| 297 context()->AddProviderHost(std::move(host)); | 298 context()->AddProviderHost(std::move(host)); |
| 298 return host_weakptr; | 299 provider_host->running_hosted_version_ = version; |
| 300 return provider_host; |
| 299 } | 301 } |
| 300 | 302 |
| 301 void SetUpScriptRequest(int process_id, int provider_id) { | 303 void SetUpScriptRequest(int process_id, int provider_id) { |
| 302 request_.reset(); | 304 request_.reset(); |
| 303 url_request_context_.reset(); | 305 url_request_context_.reset(); |
| 304 url_request_job_factory_.reset(); | 306 url_request_job_factory_.reset(); |
| 305 mock_protocol_handler_ = nullptr; | 307 mock_protocol_handler_ = nullptr; |
| 306 // URLRequestJobs may post clean-up tasks on destruction. | 308 // URLRequestJobs may post clean-up tasks on destruction. |
| 307 base::RunLoop().RunUntilIdle(); | 309 base::RunLoop().RunUntilIdle(); |
| 308 | 310 |
| 309 url_request_context_.reset(new net::URLRequestContext); | 311 url_request_context_.reset(new net::URLRequestContext); |
| 310 mock_protocol_handler_ = new MockHttpProtocolHandler(&resource_context_); | 312 mock_protocol_handler_ = new MockHttpProtocolHandler(&resource_context_); |
| 311 url_request_job_factory_.reset(new net::URLRequestJobFactoryImpl); | 313 url_request_job_factory_.reset(new net::URLRequestJobFactoryImpl); |
| 312 url_request_job_factory_->SetProtocolHandler( | 314 url_request_job_factory_->SetProtocolHandler( |
| 313 "https", base::WrapUnique(mock_protocol_handler_)); | 315 "https", base::WrapUnique(mock_protocol_handler_)); |
| 314 url_request_context_->set_job_factory(url_request_job_factory_.get()); | 316 url_request_context_->set_job_factory(url_request_job_factory_.get()); |
| 315 | 317 |
| 316 request_ = url_request_context_->CreateRequest( | 318 request_ = url_request_context_->CreateRequest( |
| 317 script_url_, net::DEFAULT_PRIORITY, &url_request_delegate_, | 319 script_url_, net::DEFAULT_PRIORITY, &url_request_delegate_, |
| 318 TRAFFIC_ANNOTATION_FOR_TESTS); | 320 TRAFFIC_ANNOTATION_FOR_TESTS); |
| 319 ServiceWorkerRequestHandler::InitializeHandler( | 321 ServiceWorkerRequestHandler::InitializeHandler( |
| 320 request_.get(), context_wrapper(), &blob_storage_context_, process_id, | 322 request_.get(), context_wrapper(), &blob_storage_context_, process_id, |
| 321 provider_id, false, FETCH_REQUEST_MODE_NO_CORS, | 323 provider_id, false, FETCH_REQUEST_MODE_NO_CORS, |
| 322 FETCH_CREDENTIALS_MODE_OMIT, FetchRedirectMode::FOLLOW_MODE, | 324 FETCH_CREDENTIALS_MODE_OMIT, FetchRedirectMode::FOLLOW_MODE, |
| 323 RESOURCE_TYPE_SERVICE_WORKER, REQUEST_CONTEXT_TYPE_SERVICE_WORKER, | 325 RESOURCE_TYPE_SERVICE_WORKER, REQUEST_CONTEXT_TYPE_SERVICE_WORKER, |
| 324 REQUEST_CONTEXT_FRAME_TYPE_NONE, | 326 REQUEST_CONTEXT_FRAME_TYPE_NONE, |
| 325 scoped_refptr<ResourceRequestBodyImpl>()); | 327 scoped_refptr<ResourceRequestBodyImpl>()); |
| 326 } | 328 } |
| 327 | 329 |
| 330 int NextProviderId() { return next_provider_id_++; } |
| 328 int NextVersionId() { return next_version_id_++; } | 331 int NextVersionId() { return next_version_id_++; } |
| 329 | 332 |
| 330 void SetUp() override { | 333 void SetUp() override { |
| 334 int provider_id = NextProviderId(); |
| 331 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); | 335 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); |
| 332 | 336 |
| 333 // A new unstored registration/version. | 337 // A new unstored registration/version. |
| 334 registration_ = | 338 registration_ = |
| 335 new ServiceWorkerRegistration(scope_, 1L, context()->AsWeakPtr()); | 339 new ServiceWorkerRegistration(scope_, 1L, context()->AsWeakPtr()); |
| 336 version_ = | 340 version_ = |
| 337 new ServiceWorkerVersion(registration_.get(), script_url_, | 341 new ServiceWorkerVersion(registration_.get(), script_url_, |
| 338 NextVersionId(), context()->AsWeakPtr()); | 342 NextVersionId(), context()->AsWeakPtr()); |
| 339 base::WeakPtr<ServiceWorkerProviderHost> host = | 343 base::WeakPtr<ServiceWorkerProviderHost> host = CreateHostForVersion( |
| 340 CreateHostForVersion(helper_->mock_render_process_id(), version_); | 344 helper_->mock_render_process_id(), provider_id, version_); |
| 341 ASSERT_TRUE(host); | 345 ASSERT_TRUE(host); |
| 342 SetUpScriptRequest(helper_->mock_render_process_id(), host->provider_id()); | 346 SetUpScriptRequest(helper_->mock_render_process_id(), host->provider_id()); |
| 343 | 347 |
| 344 context()->storage()->LazyInitialize(base::Bind(&EmptyCallback)); | 348 context()->storage()->LazyInitialize(base::Bind(&EmptyCallback)); |
| 345 base::RunLoop().RunUntilIdle(); | 349 base::RunLoop().RunUntilIdle(); |
| 346 } | 350 } |
| 347 | 351 |
| 348 void TearDown() override { | 352 void TearDown() override { |
| 349 request_.reset(); | 353 request_.reset(); |
| 350 url_request_context_.reset(); | 354 url_request_context_.reset(); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 380 } | 384 } |
| 381 | 385 |
| 382 int GetResourceId(ServiceWorkerVersion* version) { | 386 int GetResourceId(ServiceWorkerVersion* version) { |
| 383 return version->script_cache_map()->LookupResourceId(script_url_); | 387 return version->script_cache_map()->LookupResourceId(script_url_); |
| 384 } | 388 } |
| 385 | 389 |
| 386 // Performs the net request for an update of |registration_|'s incumbent | 390 // Performs the net request for an update of |registration_|'s incumbent |
| 387 // to the script |response|. Returns the new version. | 391 // to the script |response|. Returns the new version. |
| 388 scoped_refptr<ServiceWorkerVersion> UpdateScript( | 392 scoped_refptr<ServiceWorkerVersion> UpdateScript( |
| 389 const std::string& response) { | 393 const std::string& response) { |
| 394 int provider_id = NextProviderId(); |
| 390 scoped_refptr<ServiceWorkerVersion> new_version = | 395 scoped_refptr<ServiceWorkerVersion> new_version = |
| 391 new ServiceWorkerVersion(registration_.get(), script_url_, | 396 new ServiceWorkerVersion(registration_.get(), script_url_, |
| 392 NextVersionId(), context()->AsWeakPtr()); | 397 NextVersionId(), context()->AsWeakPtr()); |
| 393 new_version->set_pause_after_download(true); | 398 new_version->set_pause_after_download(true); |
| 394 base::WeakPtr<ServiceWorkerProviderHost> host = | 399 base::WeakPtr<ServiceWorkerProviderHost> host = CreateHostForVersion( |
| 395 CreateHostForVersion(helper_->mock_render_process_id(), new_version); | 400 helper_->mock_render_process_id(), provider_id, new_version); |
| 396 EXPECT_TRUE(host); | 401 |
| 397 SetUpScriptRequest(helper_->mock_render_process_id(), host->provider_id()); | 402 SetUpScriptRequest(helper_->mock_render_process_id(), host->provider_id()); |
| 398 mock_protocol_handler_->SetCreateJobCallback( | 403 mock_protocol_handler_->SetCreateJobCallback( |
| 399 base::Bind(&CreateResponseJob, response)); | 404 base::Bind(&CreateResponseJob, response)); |
| 400 request_->Start(); | 405 request_->Start(); |
| 401 base::RunLoop().RunUntilIdle(); | 406 base::RunLoop().RunUntilIdle(); |
| 402 return new_version; | 407 return new_version; |
| 403 } | 408 } |
| 404 | 409 |
| 405 void VerifyResource(int id, const std::string& expected) { | 410 void VerifyResource(int id, const std::string& expected) { |
| 406 ASSERT_NE(kInvalidServiceWorkerResourceId, id); | 411 ASSERT_NE(kInvalidServiceWorkerResourceId, id); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 423 void DisableCache() { context()->storage()->disk_cache()->Disable(); } | 428 void DisableCache() { context()->storage()->disk_cache()->Disable(); } |
| 424 | 429 |
| 425 protected: | 430 protected: |
| 426 const GURL scope_; | 431 const GURL scope_; |
| 427 const GURL script_url_; | 432 const GURL script_url_; |
| 428 | 433 |
| 429 TestBrowserThreadBundle browser_thread_bundle_; | 434 TestBrowserThreadBundle browser_thread_bundle_; |
| 430 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; | 435 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; |
| 431 scoped_refptr<ServiceWorkerRegistration> registration_; | 436 scoped_refptr<ServiceWorkerRegistration> registration_; |
| 432 scoped_refptr<ServiceWorkerVersion> version_; | 437 scoped_refptr<ServiceWorkerVersion> version_; |
| 438 base::WeakPtr<ServiceWorkerProviderHost> provider_host_; |
| 433 std::unique_ptr<net::URLRequestContext> url_request_context_; | 439 std::unique_ptr<net::URLRequestContext> url_request_context_; |
| 434 std::unique_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_; | 440 std::unique_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_; |
| 435 std::unique_ptr<net::URLRequest> request_; | 441 std::unique_ptr<net::URLRequest> request_; |
| 436 MockHttpProtocolHandler* mock_protocol_handler_; | 442 MockHttpProtocolHandler* mock_protocol_handler_; |
| 437 | 443 |
| 438 storage::BlobStorageContext blob_storage_context_; | 444 storage::BlobStorageContext blob_storage_context_; |
| 439 content::MockResourceContext resource_context_; | 445 content::MockResourceContext resource_context_; |
| 440 ServiceWorkerRemoteProviderEndpoint remote_endpoint_; | 446 ServiceWorkerRemoteProviderEndpoint remote_endpoint_; |
| 441 | 447 |
| 442 net::TestDelegate url_request_delegate_; | 448 net::TestDelegate url_request_delegate_; |
| (...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 677 mock_protocol_handler_->SetCreateJobCallback( | 683 mock_protocol_handler_->SetCreateJobCallback( |
| 678 base::Bind(&CreateNormalURLRequestJob)); | 684 base::Bind(&CreateNormalURLRequestJob)); |
| 679 DisableCache(); | 685 DisableCache(); |
| 680 request_->Start(); | 686 request_->Start(); |
| 681 base::RunLoop().RunUntilIdle(); | 687 base::RunLoop().RunUntilIdle(); |
| 682 EXPECT_EQ(net::URLRequestStatus::FAILED, request_->status().status()); | 688 EXPECT_EQ(net::URLRequestStatus::FAILED, request_->status().status()); |
| 683 EXPECT_EQ(net::ERR_FAILED, request_->status().error()); | 689 EXPECT_EQ(net::ERR_FAILED, request_->status().error()); |
| 684 } | 690 } |
| 685 | 691 |
| 686 } // namespace content | 692 } // namespace content |
| OLD | NEW |