Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(219)

Side by Side Diff: content/browser/service_worker/service_worker_context_request_handler_unittest.cc

Issue 2771823002: Implement updateViaCache flag and no-cache by default for main service worker scripts
Patch Set: fix IPC Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "content/browser/service_worker/service_worker_context_request_handler. h" 5 #include "content/browser/service_worker/service_worker_context_request_handler. h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/files/scoped_temp_dir.h" 9 #include "base/files/scoped_temp_dir.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 class ServiceWorkerContextRequestHandlerTest : public testing::Test { 54 class ServiceWorkerContextRequestHandlerTest : public testing::Test {
55 public: 55 public:
56 ServiceWorkerContextRequestHandlerTest() 56 ServiceWorkerContextRequestHandlerTest()
57 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} 57 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {}
58 58
59 void SetUp() override { 59 void SetUp() override {
60 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); 60 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath()));
61 context()->storage()->LazyInitialize(base::Bind(&base::DoNothing)); 61 context()->storage()->LazyInitialize(base::Bind(&base::DoNothing));
62 base::RunLoop().RunUntilIdle(); 62 base::RunLoop().RunUntilIdle();
63 63
64 // A new unstored registration/version.
65 scope_ = GURL("https://host/scope/"); 64 scope_ = GURL("https://host/scope/");
66 script_url_ = GURL("https://host/script.js"); 65 script_url_ = GURL("https://host/script.js");
67 import_script_url_ = GURL("https://host/import.js"); 66 import_script_url_ = GURL("https://host/import.js");
68 registration_ = new ServiceWorkerRegistration( 67 provider_host_ = nullptr;
69 ServiceWorkerRegistrationOptions(scope_), 1L, context()->AsWeakPtr()); 68
70 version_ = new ServiceWorkerVersion(registration_.get(), script_url_, 69 SetUpServiceWorker(blink::WebServiceWorkerUpdateViaCache::kImports);
71 context()->storage()->NewVersionId(),
72 context()->AsWeakPtr());
73 SetUpProvider(); 70 SetUpProvider();
74 71
75 std::unique_ptr<MockHttpProtocolHandler> handler( 72 std::unique_ptr<MockHttpProtocolHandler> handler(
76 new MockHttpProtocolHandler( 73 new MockHttpProtocolHandler(
77 helper_->browser_context()->GetResourceContext())); 74 helper_->browser_context()->GetResourceContext()));
78 url_request_job_factory_.SetProtocolHandler("https", std::move(handler)); 75 url_request_job_factory_.SetProtocolHandler("https", std::move(handler));
79 url_request_context_.set_job_factory(&url_request_job_factory_); 76 url_request_context_.set_job_factory(&url_request_job_factory_);
80 } 77 }
81 78
82 void TearDown() override { 79 void TearDown() override {
83 version_ = nullptr; 80 version_ = nullptr;
84 registration_ = nullptr; 81 registration_ = nullptr;
85 helper_.reset(); 82 helper_.reset();
86 } 83 }
87 84
88 ServiceWorkerContextCore* context() const { return helper_->context(); } 85 ServiceWorkerContextCore* context() const { return helper_->context(); }
89 86
87 void SetUpServiceWorker(
88 blink::WebServiceWorkerUpdateViaCache update_via_cache) {
89 registration_ = new ServiceWorkerRegistration(
90 ServiceWorkerRegistrationOptions(scope_, update_via_cache),
91 context()->storage()->NewRegistrationId(), context()->AsWeakPtr());
92 version_ = new ServiceWorkerVersion(registration_.get(), script_url_,
93 context()->storage()->NewVersionId(),
94 context()->AsWeakPtr());
95
96 if (provider_host_)
97 provider_host_->running_hosted_version_ = version_;
98 }
99
90 void SetUpProvider() { 100 void SetUpProvider() {
91 std::unique_ptr<ServiceWorkerProviderHost> host = 101 std::unique_ptr<ServiceWorkerProviderHost> host =
92 CreateProviderHostForServiceWorkerContext( 102 CreateProviderHostForServiceWorkerContext(
93 helper_->mock_render_process_id(), 103 helper_->mock_render_process_id(),
94 true /* is_parent_frame_secure */, version_.get(), 104 true /* is_parent_frame_secure */, version_.get(),
95 context()->AsWeakPtr(), &remote_endpoint_); 105 context()->AsWeakPtr(), &remote_endpoint_);
96 provider_host_ = host->AsWeakPtr(); 106 provider_host_ = host->AsWeakPtr();
97 context()->AddProviderHost(std::move(host)); 107 context()->AddProviderHost(std::move(host));
98 } 108 }
99 109
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 153
144 TEST_F(ServiceWorkerContextRequestHandlerTest, UpdateBefore24Hours) { 154 TEST_F(ServiceWorkerContextRequestHandlerTest, UpdateBefore24Hours) {
145 // Give the registration a very recent last update time and pretend 155 // Give the registration a very recent last update time and pretend
146 // we're installing a new version. 156 // we're installing a new version.
147 registration_->set_last_update_check(base::Time::Now()); 157 registration_->set_last_update_check(base::Time::Now());
148 version_->SetStatus(ServiceWorkerVersion::NEW); 158 version_->SetStatus(ServiceWorkerVersion::NEW);
149 159
150 // Conduct a resource fetch for the main script. 160 // Conduct a resource fetch for the main script.
151 base::HistogramTester histograms; 161 base::HistogramTester histograms;
152 std::unique_ptr<net::URLRequest> request(CreateRequest(script_url_)); 162 std::unique_ptr<net::URLRequest> request(CreateRequest(script_url_));
163 std::unique_ptr<ServiceWorkerContextRequestHandler> handler(
164 CreateHandler(RESOURCE_TYPE_SERVICE_WORKER));
165 std::unique_ptr<net::URLRequestJob> job(
166 handler->MaybeCreateJob(request.get(), nullptr, nullptr));
167 ASSERT_TRUE(job.get());
168 ServiceWorkerWriteToCacheJob* sw_job =
169 static_cast<ServiceWorkerWriteToCacheJob*>(job.get());
170 histograms.ExpectUniqueSample(
171 "ServiceWorker.ContextRequestHandlerStatus.NewWorker.MainScript",
172 static_cast<int>(
173 ServiceWorkerContextRequestHandler::CreateJobStatus::WRITE_JOB),
174 1);
175
176 // Verify the net request is initialized to bypass the browser cache.
177 EXPECT_TRUE(sw_job->net_request_->load_flags() & net::LOAD_BYPASS_CACHE);
178 }
179
180 TEST_F(ServiceWorkerContextRequestHandlerTest, UpdateBefore24HoursUsingCache) {
181 SetUpServiceWorker(blink::WebServiceWorkerUpdateViaCache::kAll);
182 // Give the registration a very recent last update time and pretend
183 // we're installing a new version.
184 registration_->set_last_update_check(base::Time::Now());
185 version_->SetStatus(ServiceWorkerVersion::NEW);
186
187 // Conduct a resource fetch for the main script.
188 base::HistogramTester histograms;
189 std::unique_ptr<net::URLRequest> request(CreateRequest(script_url_));
153 std::unique_ptr<ServiceWorkerContextRequestHandler> handler( 190 std::unique_ptr<ServiceWorkerContextRequestHandler> handler(
154 CreateHandler(RESOURCE_TYPE_SERVICE_WORKER)); 191 CreateHandler(RESOURCE_TYPE_SERVICE_WORKER));
155 std::unique_ptr<net::URLRequestJob> job( 192 std::unique_ptr<net::URLRequestJob> job(
156 handler->MaybeCreateJob(request.get(), nullptr, nullptr)); 193 handler->MaybeCreateJob(request.get(), nullptr, nullptr));
157 ASSERT_TRUE(job.get()); 194 ASSERT_TRUE(job.get());
158 ServiceWorkerWriteToCacheJob* sw_job = 195 ServiceWorkerWriteToCacheJob* sw_job =
159 static_cast<ServiceWorkerWriteToCacheJob*>(job.get()); 196 static_cast<ServiceWorkerWriteToCacheJob*>(job.get());
160 histograms.ExpectUniqueSample( 197 histograms.ExpectUniqueSample(
161 "ServiceWorker.ContextRequestHandlerStatus.NewWorker.MainScript", 198 "ServiceWorker.ContextRequestHandlerStatus.NewWorker.MainScript",
162 static_cast<int>( 199 static_cast<int>(
163 ServiceWorkerContextRequestHandler::CreateJobStatus::WRITE_JOB), 200 ServiceWorkerContextRequestHandler::CreateJobStatus::WRITE_JOB),
164 1); 201 1);
165 202
166 // Verify the net request is not initialized to bypass the browser cache. 203 // Verify the net request is not initialized to bypass the browser cache.
167 EXPECT_FALSE(sw_job->net_request_->load_flags() & net::LOAD_BYPASS_CACHE); 204 EXPECT_FALSE(sw_job->net_request_->load_flags() & net::LOAD_BYPASS_CACHE);
168 } 205 }
169 206
170 TEST_F(ServiceWorkerContextRequestHandlerTest, UpdateAfter24Hours) { 207 TEST_F(ServiceWorkerContextRequestHandlerTest, UpdateAfter24HoursUsingCache) {
208 SetUpServiceWorker(blink::WebServiceWorkerUpdateViaCache::kAll);
171 // Give the registration a old update time and pretend 209 // Give the registration a old update time and pretend
172 // we're installing a new version. 210 // we're installing a new version.
173 registration_->set_last_update_check( 211 registration_->set_last_update_check(
174 base::Time::Now() - base::TimeDelta::FromDays(7)); 212 base::Time::Now() - base::TimeDelta::FromDays(7));
175 version_->SetStatus(ServiceWorkerVersion::NEW); 213 version_->SetStatus(ServiceWorkerVersion::NEW);
176 214
177 // Conduct a resource fetch for the main script. 215 // Conduct a resource fetch for the main script.
178 base::HistogramTester histograms; 216 base::HistogramTester histograms;
179 std::unique_ptr<net::URLRequest> request(CreateRequest(script_url_)); 217 std::unique_ptr<net::URLRequest> request(CreateRequest(script_url_));
180 std::unique_ptr<ServiceWorkerContextRequestHandler> handler( 218 std::unique_ptr<ServiceWorkerContextRequestHandler> handler(
181 CreateHandler(RESOURCE_TYPE_SERVICE_WORKER)); 219 CreateHandler(RESOURCE_TYPE_SERVICE_WORKER));
182 std::unique_ptr<net::URLRequestJob> job( 220 std::unique_ptr<net::URLRequestJob> job(
183 handler->MaybeCreateJob(request.get(), nullptr, nullptr)); 221 handler->MaybeCreateJob(request.get(), nullptr, nullptr));
184 ASSERT_TRUE(job.get()); 222 ASSERT_TRUE(job.get());
185 ServiceWorkerWriteToCacheJob* sw_job = 223 ServiceWorkerWriteToCacheJob* sw_job =
186 static_cast<ServiceWorkerWriteToCacheJob*>(job.get()); 224 static_cast<ServiceWorkerWriteToCacheJob*>(job.get());
187 histograms.ExpectUniqueSample( 225 histograms.ExpectUniqueSample(
188 "ServiceWorker.ContextRequestHandlerStatus.NewWorker.MainScript", 226 "ServiceWorker.ContextRequestHandlerStatus.NewWorker.MainScript",
189 static_cast<int>( 227 static_cast<int>(
190 ServiceWorkerContextRequestHandler::CreateJobStatus::WRITE_JOB), 228 ServiceWorkerContextRequestHandler::CreateJobStatus::WRITE_JOB),
191 1); 229 1);
192 230
193 // Verify the net request is initialized to bypass the browser cache. 231 // Verify the net request is initialized to bypass the browser cache.
194 EXPECT_TRUE(sw_job->net_request_->load_flags() & net::LOAD_BYPASS_CACHE); 232 EXPECT_TRUE(sw_job->net_request_->load_flags() & net::LOAD_BYPASS_CACHE);
195 } 233 }
196 234
197 TEST_F(ServiceWorkerContextRequestHandlerTest, UpdateForceBypassCache) { 235 TEST_F(ServiceWorkerContextRequestHandlerTest, UpdateForceBypassCache) {
236 SetUpServiceWorker(blink::WebServiceWorkerUpdateViaCache::kNone);
198 // Give the registration a very recent last update time and pretend 237 // Give the registration a very recent last update time and pretend
199 // we're installing a new version. 238 // we're installing a new version.
200 registration_->set_last_update_check(base::Time::Now()); 239 registration_->set_last_update_check(base::Time::Now());
201 version_->SetStatus(ServiceWorkerVersion::NEW); 240 version_->SetStatus(ServiceWorkerVersion::NEW);
202 version_->set_force_bypass_cache_for_scripts(true); 241 version_->set_force_bypass_cache_for_scripts(true);
203 242
204 // Conduct a resource fetch for the main script. 243 // Conduct a resource fetch for the main script.
205 std::unique_ptr<net::URLRequest> request(CreateRequest(script_url_)); 244 std::unique_ptr<net::URLRequest> request(CreateRequest(script_url_));
206 std::unique_ptr<ServiceWorkerContextRequestHandler> handler( 245 std::unique_ptr<ServiceWorkerContextRequestHandler> handler(
207 CreateHandler(RESOURCE_TYPE_SERVICE_WORKER)); 246 CreateHandler(RESOURCE_TYPE_SERVICE_WORKER));
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after
434 EXPECT_EQ(net::ERR_FAILED, url_request_delegate_.request_status()); 473 EXPECT_EQ(net::ERR_FAILED, url_request_delegate_.request_status());
435 histograms.ExpectUniqueSample( 474 histograms.ExpectUniqueSample(
436 "ServiceWorker.ContextRequestHandlerStatus.NewWorker.MainScript", 475 "ServiceWorker.ContextRequestHandlerStatus.NewWorker.MainScript",
437 static_cast<int>(ServiceWorkerContextRequestHandler::CreateJobStatus:: 476 static_cast<int>(ServiceWorkerContextRequestHandler::CreateJobStatus::
438 ERROR_NO_CONTEXT), 477 ERROR_NO_CONTEXT),
439 1); 478 1);
440 } 479 }
441 } 480 }
442 481
443 } // namespace content 482 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698