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