| 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 "base/files/scoped_temp_dir.h" | 5 #include "base/files/scoped_temp_dir.h" |
| 6 #include "base/logging.h" | 6 #include "base/logging.h" |
| 7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
| 8 #include "content/browser/browser_thread_impl.h" | 8 #include "content/browser/browser_thread_impl.h" |
| 9 #include "content/browser/fileapi/mock_url_request_delegate.h" | 9 #include "content/browser/fileapi/mock_url_request_delegate.h" |
| 10 #include "content/browser/service_worker/embedded_worker_test_helper.h" | 10 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
| 11 #include "content/browser/service_worker/service_worker_context_core.h" | 11 #include "content/browser/service_worker/service_worker_context_core.h" |
| 12 #include "content/browser/service_worker/service_worker_controllee_request_handl
er.h" | 12 #include "content/browser/service_worker/service_worker_controllee_request_handl
er.h" |
| 13 #include "content/browser/service_worker/service_worker_provider_host.h" | 13 #include "content/browser/service_worker/service_worker_provider_host.h" |
| 14 #include "content/browser/service_worker/service_worker_registration.h" | 14 #include "content/browser/service_worker/service_worker_registration.h" |
| 15 #include "content/browser/service_worker/service_worker_registration.h" | 15 #include "content/browser/service_worker/service_worker_registration.h" |
| 16 #include "content/browser/service_worker/service_worker_url_request_job.h" | 16 #include "content/browser/service_worker/service_worker_url_request_job.h" |
| 17 #include "content/browser/service_worker/service_worker_utils.h" | 17 #include "content/browser/service_worker/service_worker_utils.h" |
| 18 #include "content/common/resource_request_body.h" | 18 #include "content/common/resource_request_body.h" |
| 19 #include "content/public/browser/resource_context.h" |
| 19 #include "content/public/common/request_context_frame_type.h" | 20 #include "content/public/common/request_context_frame_type.h" |
| 20 #include "content/public/common/request_context_type.h" | 21 #include "content/public/common/request_context_type.h" |
| 21 #include "content/public/common/resource_type.h" | 22 #include "content/public/common/resource_type.h" |
| 23 #include "content/public/test/mock_resource_context.h" |
| 22 #include "content/public/test/test_browser_thread_bundle.h" | 24 #include "content/public/test/test_browser_thread_bundle.h" |
| 25 #include "content/test/test_content_browser_client.h" |
| 23 #include "net/url_request/url_request_context.h" | 26 #include "net/url_request/url_request_context.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 25 | 28 |
| 26 namespace content { | 29 namespace content { |
| 27 | 30 |
| 28 namespace { | 31 namespace { |
| 29 | 32 |
| 30 int kMockRenderProcessId = 1224; | 33 int kMockRenderProcessId = 1224; |
| 31 int kMockProviderId = 1; | 34 int kMockProviderId = 1; |
| 32 | 35 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 ServiceWorkerContextCore* context() const { return helper_->context(); } | 73 ServiceWorkerContextCore* context() const { return helper_->context(); } |
| 71 | 74 |
| 72 protected: | 75 protected: |
| 73 TestBrowserThreadBundle browser_thread_bundle_; | 76 TestBrowserThreadBundle browser_thread_bundle_; |
| 74 scoped_ptr<EmbeddedWorkerTestHelper> helper_; | 77 scoped_ptr<EmbeddedWorkerTestHelper> helper_; |
| 75 scoped_refptr<ServiceWorkerRegistration> registration_; | 78 scoped_refptr<ServiceWorkerRegistration> registration_; |
| 76 scoped_refptr<ServiceWorkerVersion> version_; | 79 scoped_refptr<ServiceWorkerVersion> version_; |
| 77 base::WeakPtr<ServiceWorkerProviderHost> provider_host_; | 80 base::WeakPtr<ServiceWorkerProviderHost> provider_host_; |
| 78 net::URLRequestContext url_request_context_; | 81 net::URLRequestContext url_request_context_; |
| 79 MockURLRequestDelegate url_request_delegate_; | 82 MockURLRequestDelegate url_request_delegate_; |
| 83 MockResourceContext mock_resource_context_; |
| 80 GURL scope_; | 84 GURL scope_; |
| 81 GURL script_url_; | 85 GURL script_url_; |
| 82 }; | 86 }; |
| 83 | 87 |
| 88 class ServiceWorkerTestContentBrowserClient : public TestContentBrowserClient { |
| 89 public: |
| 90 ServiceWorkerTestContentBrowserClient() {} |
| 91 virtual bool AllowServiceWorker(const GURL& scope, |
| 92 const GURL& first_party, |
| 93 content::ResourceContext* context) OVERRIDE { |
| 94 return false; |
| 95 } |
| 96 }; |
| 97 |
| 98 TEST_F(ServiceWorkerControlleeRequestHandlerTest, DisallowServiceWorker) { |
| 99 ServiceWorkerTestContentBrowserClient test_browser_client; |
| 100 ContentBrowserClient* old_browser_client = |
| 101 SetBrowserClientForTesting(&test_browser_client); |
| 102 |
| 103 // Store an activated worker. |
| 104 version_->SetStatus(ServiceWorkerVersion::ACTIVATED); |
| 105 registration_->SetActiveVersion(version_.get()); |
| 106 context()->storage()->StoreRegistration( |
| 107 registration_.get(), |
| 108 version_.get(), |
| 109 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 110 base::RunLoop().RunUntilIdle(); |
| 111 |
| 112 // Conduct a main resource load. |
| 113 const GURL kDocUrl("http://host/scope/doc"); |
| 114 scoped_ptr<net::URLRequest> request = url_request_context_.CreateRequest( |
| 115 kDocUrl, net::DEFAULT_PRIORITY, &url_request_delegate_, NULL); |
| 116 scoped_ptr<ServiceWorkerControlleeRequestHandler> handler( |
| 117 new ServiceWorkerControlleeRequestHandler( |
| 118 context()->AsWeakPtr(), |
| 119 provider_host_, |
| 120 base::WeakPtr<storage::BlobStorageContext>(), |
| 121 FETCH_REQUEST_MODE_NO_CORS, |
| 122 FETCH_CREDENTIALS_MODE_OMIT, |
| 123 RESOURCE_TYPE_MAIN_FRAME, |
| 124 REQUEST_CONTEXT_TYPE_HYPERLINK, |
| 125 REQUEST_CONTEXT_FRAME_TYPE_TOP_LEVEL, |
| 126 scoped_refptr<ResourceRequestBody>())); |
| 127 scoped_refptr<net::URLRequestJob> job = |
| 128 handler->MaybeCreateJob(request.get(), NULL, &mock_resource_context_); |
| 129 ServiceWorkerURLRequestJob* sw_job = |
| 130 static_cast<ServiceWorkerURLRequestJob*>(job.get()); |
| 131 |
| 132 EXPECT_FALSE(sw_job->ShouldFallbackToNetwork()); |
| 133 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); |
| 134 EXPECT_FALSE(version_->HasControllee()); |
| 135 base::RunLoop().RunUntilIdle(); |
| 136 |
| 137 // Verify we did not use the worker. |
| 138 EXPECT_TRUE(sw_job->ShouldFallbackToNetwork()); |
| 139 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); |
| 140 EXPECT_FALSE(version_->HasControllee()); |
| 141 |
| 142 SetBrowserClientForTesting(old_browser_client); |
| 143 } |
| 144 |
| 84 TEST_F(ServiceWorkerControlleeRequestHandlerTest, ActivateWaitingVersion) { | 145 TEST_F(ServiceWorkerControlleeRequestHandlerTest, ActivateWaitingVersion) { |
| 85 // Store a registration that is installed but not activated yet. | 146 // Store a registration that is installed but not activated yet. |
| 86 version_->SetStatus(ServiceWorkerVersion::INSTALLED); | 147 version_->SetStatus(ServiceWorkerVersion::INSTALLED); |
| 87 registration_->SetWaitingVersion(version_.get()); | 148 registration_->SetWaitingVersion(version_.get()); |
| 88 context()->storage()->StoreRegistration( | 149 context()->storage()->StoreRegistration( |
| 89 registration_.get(), | 150 registration_.get(), |
| 90 version_.get(), | 151 version_.get(), |
| 91 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 152 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 92 base::RunLoop().RunUntilIdle(); | 153 base::RunLoop().RunUntilIdle(); |
| 93 | 154 |
| 94 // Conduct a main resource load. | 155 // Conduct a main resource load. |
| 95 const GURL kDocUrl("http://host/scope/doc"); | 156 const GURL kDocUrl("http://host/scope/doc"); |
| 96 scoped_ptr<net::URLRequest> request = url_request_context_.CreateRequest( | 157 scoped_ptr<net::URLRequest> request = url_request_context_.CreateRequest( |
| 97 kDocUrl, | 158 kDocUrl, |
| 98 net::DEFAULT_PRIORITY, | 159 net::DEFAULT_PRIORITY, |
| 99 &url_request_delegate_, | 160 &url_request_delegate_, |
| 100 NULL); | 161 NULL); |
| 101 scoped_ptr<ServiceWorkerControlleeRequestHandler> handler( | 162 scoped_ptr<ServiceWorkerControlleeRequestHandler> handler( |
| 102 new ServiceWorkerControlleeRequestHandler( | 163 new ServiceWorkerControlleeRequestHandler( |
| 103 context()->AsWeakPtr(), | 164 context()->AsWeakPtr(), |
| 104 provider_host_, | 165 provider_host_, |
| 105 base::WeakPtr<storage::BlobStorageContext>(), | 166 base::WeakPtr<storage::BlobStorageContext>(), |
| 106 FETCH_REQUEST_MODE_NO_CORS, | 167 FETCH_REQUEST_MODE_NO_CORS, |
| 107 FETCH_CREDENTIALS_MODE_OMIT, | 168 FETCH_CREDENTIALS_MODE_OMIT, |
| 108 RESOURCE_TYPE_MAIN_FRAME, | 169 RESOURCE_TYPE_MAIN_FRAME, |
| 109 REQUEST_CONTEXT_TYPE_HYPERLINK, | 170 REQUEST_CONTEXT_TYPE_HYPERLINK, |
| 110 REQUEST_CONTEXT_FRAME_TYPE_TOP_LEVEL, | 171 REQUEST_CONTEXT_FRAME_TYPE_TOP_LEVEL, |
| 111 scoped_refptr<ResourceRequestBody>())); | 172 scoped_refptr<ResourceRequestBody>())); |
| 112 scoped_refptr<net::URLRequestJob> job = | 173 scoped_refptr<net::URLRequestJob> job = |
| 113 handler->MaybeCreateJob(request.get(), NULL); | 174 handler->MaybeCreateJob(request.get(), NULL, &mock_resource_context_); |
| 114 ServiceWorkerURLRequestJob* sw_job = | 175 ServiceWorkerURLRequestJob* sw_job = |
| 115 static_cast<ServiceWorkerURLRequestJob*>(job.get()); | 176 static_cast<ServiceWorkerURLRequestJob*>(job.get()); |
| 116 | 177 |
| 117 EXPECT_FALSE(sw_job->ShouldFallbackToNetwork()); | 178 EXPECT_FALSE(sw_job->ShouldFallbackToNetwork()); |
| 118 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); | 179 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); |
| 119 EXPECT_FALSE(version_->HasControllee()); | 180 EXPECT_FALSE(version_->HasControllee()); |
| 120 | 181 |
| 121 base::RunLoop().RunUntilIdle(); | 182 base::RunLoop().RunUntilIdle(); |
| 122 | 183 |
| 123 EXPECT_EQ(ServiceWorkerVersion::ACTIVATED, | 184 EXPECT_EQ(ServiceWorkerVersion::ACTIVATED, |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 context()->AsWeakPtr(), | 218 context()->AsWeakPtr(), |
| 158 provider_host_, | 219 provider_host_, |
| 159 base::WeakPtr<storage::BlobStorageContext>(), | 220 base::WeakPtr<storage::BlobStorageContext>(), |
| 160 FETCH_REQUEST_MODE_NO_CORS, | 221 FETCH_REQUEST_MODE_NO_CORS, |
| 161 FETCH_CREDENTIALS_MODE_OMIT, | 222 FETCH_CREDENTIALS_MODE_OMIT, |
| 162 RESOURCE_TYPE_MAIN_FRAME, | 223 RESOURCE_TYPE_MAIN_FRAME, |
| 163 REQUEST_CONTEXT_TYPE_HYPERLINK, | 224 REQUEST_CONTEXT_TYPE_HYPERLINK, |
| 164 REQUEST_CONTEXT_FRAME_TYPE_TOP_LEVEL, | 225 REQUEST_CONTEXT_FRAME_TYPE_TOP_LEVEL, |
| 165 scoped_refptr<ResourceRequestBody>())); | 226 scoped_refptr<ResourceRequestBody>())); |
| 166 scoped_refptr<net::URLRequestJob> job = | 227 scoped_refptr<net::URLRequestJob> job = |
| 167 handler->MaybeCreateJob(request.get(), NULL); | 228 handler->MaybeCreateJob(request.get(), NULL, &mock_resource_context_); |
| 168 ServiceWorkerURLRequestJob* sw_job = | 229 ServiceWorkerURLRequestJob* sw_job = |
| 169 static_cast<ServiceWorkerURLRequestJob*>(job.get()); | 230 static_cast<ServiceWorkerURLRequestJob*>(job.get()); |
| 170 | 231 |
| 171 EXPECT_FALSE(sw_job->ShouldFallbackToNetwork()); | 232 EXPECT_FALSE(sw_job->ShouldFallbackToNetwork()); |
| 172 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); | 233 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); |
| 173 | 234 |
| 174 // Shouldn't crash if the ProviderHost is deleted prior to completion of | 235 // Shouldn't crash if the ProviderHost is deleted prior to completion of |
| 175 // the database lookup. | 236 // the database lookup. |
| 176 context()->RemoveProviderHost(kMockRenderProcessId, kMockProviderId); | 237 context()->RemoveProviderHost(kMockRenderProcessId, kMockProviderId); |
| 177 EXPECT_FALSE(provider_host_.get()); | 238 EXPECT_FALSE(provider_host_.get()); |
| 178 base::RunLoop().RunUntilIdle(); | 239 base::RunLoop().RunUntilIdle(); |
| 179 EXPECT_TRUE(sw_job->ShouldFallbackToNetwork()); | 240 EXPECT_TRUE(sw_job->ShouldFallbackToNetwork()); |
| 180 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); | 241 EXPECT_FALSE(sw_job->ShouldForwardToServiceWorker()); |
| 181 } | 242 } |
| 182 | 243 |
| 183 } // namespace content | 244 } // namespace content |
| OLD | NEW |