#include "content/browser/service_worker/service_worker_registration.h"
#include "content/browser/service_worker/service_worker_url_request_job.h"
#include "content/browser/service_worker/service_worker_utils.h"
+#include "content/common/resource_request_body.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "net/url_request/url_request_context.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
int kMockRenderProcessId = 1224;
+int kMockProviderId = 1;
void EmptyCallback() {}
scope_ = GURL("http://host/scope/");
script_url_ = GURL("http://host/script.js");
registration_ = new ServiceWorkerRegistration(
- scope_, script_url_, 1L, context()->AsWeakPtr());
+ scope_, 1L, context()->AsWeakPtr());
version_ = new ServiceWorkerVersion(
- registration_,
- 1L, context()->AsWeakPtr());
+ registration_.get(), script_url_, 1L, context()->AsWeakPtr());
// 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());
TEST_F(ServiceWorkerControlleeRequestHandlerTest, ActivateWaitingVersion) {
// Store a registration that is installed but not activated yet.
version_->SetStatus(ServiceWorkerVersion::INSTALLED);
- registration_->SetWaitingVersion(version_);
+ registration_->SetWaitingVersion(version_.get());
context()->storage()->StoreRegistration(
- registration_, version_,
+ registration_.get(),
+ version_.get(),
base::Bind(&ServiceWorkerUtils::NoOpStatusCallback));
base::RunLoop().RunUntilIdle();
new ServiceWorkerControlleeRequestHandler(
context()->AsWeakPtr(),
provider_host_,
- base::WeakPtr<webkit_blob::BlobStorageContext>(),
- RESOURCE_TYPE_MAIN_FRAME));
+ base::WeakPtr<storage::BlobStorageContext>(),
+ RESOURCE_TYPE_MAIN_FRAME,
+ scoped_refptr<ResourceRequestBody>()));
scoped_refptr<net::URLRequestJob> job =
handler->MaybeCreateJob(request.get(), NULL);
ServiceWorkerURLRequestJob* sw_job =
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