Commit 279f1642 authored by michaeln's avatar michaeln Committed by Commit bot

Fix a ServiceWorker crasher. We have to test the provider_host_ weakptr in...

Fix a ServiceWorker crasher. We have to test the provider_host_ weakptr in async callbacks because the document can be closed, and the host deleted, in the interim.

BUG=414118

Review URL: https://codereview.chromium.org/569423002

Cr-Commit-Position: refs/heads/master@{#294999}
parent 479878c1
......@@ -110,6 +110,7 @@ void ServiceWorkerControlleeRequestHandler::PrepareForMainResource(
const GURL& url) {
DCHECK(job_.get());
DCHECK(context_);
DCHECK(provider_host_);
TRACE_EVENT_ASYNC_BEGIN1(
"ServiceWorker",
"ServiceWorkerControlleeRequestHandler::PrepareForMainResource",
......@@ -136,8 +137,9 @@ ServiceWorkerControlleeRequestHandler::DidLookupRegistrationForMainResource(
ServiceWorkerStatusCode status,
const scoped_refptr<ServiceWorkerRegistration>& registration) {
DCHECK(job_.get());
provider_host_->SetAllowAssociation(true);
if (status != SERVICE_WORKER_OK) {
if (provider_host_)
provider_host_->SetAllowAssociation(true);
if (status != SERVICE_WORKER_OK || !provider_host_) {
job_->FallbackToNetwork();
TRACE_EVENT_ASYNC_END1(
"ServiceWorker",
......@@ -205,9 +207,11 @@ ServiceWorkerControlleeRequestHandler::DidLookupRegistrationForMainResource(
void ServiceWorkerControlleeRequestHandler::OnVersionStatusChanged(
ServiceWorkerRegistration* registration,
ServiceWorkerVersion* version) {
provider_host_->SetAllowAssociation(true);
if (provider_host_)
provider_host_->SetAllowAssociation(true);
if (version != registration->active_version() ||
version->status() != ServiceWorkerVersion::ACTIVATED) {
version->status() != ServiceWorkerVersion::ACTIVATED ||
!provider_host_) {
job_->FallbackToNetwork();
return;
}
......
......@@ -25,6 +25,7 @@ namespace content {
namespace {
int kMockRenderProcessId = 1224;
int kMockProviderId = 1;
void EmptyCallback() {}
......@@ -48,7 +49,7 @@ class ServiceWorkerControlleeRequestHandlerTest : public testing::Test {
// An empty host.
scoped_ptr<ServiceWorkerProviderHost> host(new ServiceWorkerProviderHost(
kMockRenderProcessId, 1 /* provider_id */,
kMockRenderProcessId, kMockProviderId,
context()->AsWeakPtr(), NULL));
provider_host_ = host->AsWeakPtr();
context()->AddProviderHost(host.Pass());
......@@ -123,4 +124,49 @@ TEST_F(ServiceWorkerControlleeRequestHandlerTest, ActivateWaitingVersion) {
EXPECT_TRUE(version_->update_timer_.IsRunning());
}
// Test to not regress crbug/414118.
TEST_F(ServiceWorkerControlleeRequestHandlerTest, DeletedProviderHost) {
// Store a registration so the call to FindRegistrationForDocument will read
// from the database.
version_->SetStatus(ServiceWorkerVersion::ACTIVATED);
registration_->SetActiveVersion(version_.get());
context()->storage()->StoreRegistration(
registration_.get(),
version_.get(),
base::Bind(&ServiceWorkerUtils::NoOpStatusCallback));
base::RunLoop().RunUntilIdle();
version_ = NULL;
registration_ = NULL;
// Conduct a main resource load.
const GURL kDocUrl("http://host/scope/doc");
scoped_ptr<net::URLRequest> request = url_request_context_.CreateRequest(
kDocUrl,
net::DEFAULT_PRIORITY,
&url_request_delegate_,
NULL);
scoped_ptr<ServiceWorkerControlleeRequestHandler> handler(
new ServiceWorkerControlleeRequestHandler(
context()->AsWeakPtr(),
provider_host_,
base::WeakPtr<storage::BlobStorageContext>(),
RESOURCE_TYPE_MAIN_FRAME,
scoped_refptr<ResourceRequestBody>()));
scoped_refptr<net::URLRequestJob> job =
handler->MaybeCreateJob(request.get(), NULL);
ServiceWorkerURLRequestJob* sw_job =
static_cast<ServiceWorkerURLRequestJob*>(job.get());
EXPECT_FALSE(sw_job->ShouldFallbackToNetwork());
EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker());
// Shouldn't crash if the ProviderHost is deleted prior to completion of
// the database lookup.
context()->RemoveProviderHost(kMockRenderProcessId, kMockProviderId);
EXPECT_FALSE(provider_host_.get());
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(sw_job->ShouldFallbackToNetwork());
EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker());
}
} // namespace content
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment