Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / content / browser / service_worker / service_worker_url_request_job.cc
blobddfa92b6d695c61add533da7014bf8c793d1af5a
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
3 // found in the LICENSE file.
5 #include "content/browser/service_worker/service_worker_url_request_job.h"
7 #include <map>
8 #include <string>
9 #include <vector>
11 #include "base/bind.h"
12 #include "base/guid.h"
13 #include "base/location.h"
14 #include "base/memory/scoped_vector.h"
15 #include "base/single_thread_task_runner.h"
16 #include "base/strings/stringprintf.h"
17 #include "base/thread_task_runner_handle.h"
18 #include "base/time/time.h"
19 #include "content/browser/resource_context_impl.h"
20 #include "content/browser/service_worker/service_worker_fetch_dispatcher.h"
21 #include "content/browser/service_worker/service_worker_provider_host.h"
22 #include "content/browser/streams/stream.h"
23 #include "content/browser/streams/stream_context.h"
24 #include "content/browser/streams/stream_registry.h"
25 #include "content/common/resource_request_body.h"
26 #include "content/common/service_worker/service_worker_types.h"
27 #include "content/public/browser/blob_handle.h"
28 #include "content/public/browser/resource_request_info.h"
29 #include "content/public/browser/service_worker_context.h"
30 #include "content/public/common/referrer.h"
31 #include "content/public/common/resource_response_info.h"
32 #include "net/base/net_errors.h"
33 #include "net/http/http_request_headers.h"
34 #include "net/http/http_response_headers.h"
35 #include "net/http/http_response_info.h"
36 #include "net/http/http_util.h"
37 #include "net/log/net_log.h"
38 #include "storage/browser/blob/blob_data_builder.h"
39 #include "storage/browser/blob/blob_data_handle.h"
40 #include "storage/browser/blob/blob_storage_context.h"
41 #include "storage/browser/blob/blob_url_request_job_factory.h"
42 #include "ui/base/page_transition_types.h"
44 namespace content {
46 namespace {
48 net::NetLog::EventType RequestJobResultToNetEventType(
49 ServiceWorkerMetrics::URLRequestJobResult result) {
50 using n = net::NetLog;
51 using m = ServiceWorkerMetrics;
52 switch (result) {
53 case m::REQUEST_JOB_FALLBACK_RESPONSE:
54 return n::TYPE_SERVICE_WORKER_FALLBACK_RESPONSE;
55 case m::REQUEST_JOB_FALLBACK_FOR_CORS:
56 return n::TYPE_SERVICE_WORKER_FALLBACK_FOR_CORS;
57 case m::REQUEST_JOB_HEADERS_ONLY_RESPONSE:
58 return n::TYPE_SERVICE_WORKER_HEADERS_ONLY_RESPONSE;
59 case m::REQUEST_JOB_STREAM_RESPONSE:
60 return n::TYPE_SERVICE_WORKER_STREAM_RESPONSE;
61 case m::REQUEST_JOB_BLOB_RESPONSE:
62 return n::TYPE_SERVICE_WORKER_BLOB_RESPONSE;
63 case m::REQUEST_JOB_ERROR_RESPONSE_STATUS_ZERO:
64 return n::TYPE_SERVICE_WORKER_ERROR_RESPONSE_STATUS_ZERO;
65 case m::REQUEST_JOB_ERROR_BAD_BLOB:
66 return n::TYPE_SERVICE_WORKER_ERROR_BAD_BLOB;
67 case m::REQUEST_JOB_ERROR_NO_PROVIDER_HOST:
68 return n::TYPE_SERVICE_WORKER_ERROR_NO_PROVIDER_HOST;
69 case m::REQUEST_JOB_ERROR_NO_ACTIVE_VERSION:
70 return n::TYPE_SERVICE_WORKER_ERROR_NO_ACTIVE_VERSION;
71 case m::REQUEST_JOB_ERROR_FETCH_EVENT_DISPATCH:
72 return n::TYPE_SERVICE_WORKER_ERROR_FETCH_EVENT_DISPATCH;
73 case m::REQUEST_JOB_ERROR_BLOB_READ:
74 return n::TYPE_SERVICE_WORKER_ERROR_BLOB_READ;
75 case m::REQUEST_JOB_ERROR_STREAM_ABORTED:
76 return n::TYPE_SERVICE_WORKER_ERROR_STREAM_ABORTED;
77 case m::REQUEST_JOB_ERROR_KILLED:
78 return n::TYPE_SERVICE_WORKER_ERROR_KILLED;
79 case m::REQUEST_JOB_ERROR_KILLED_WITH_BLOB:
80 return n::TYPE_SERVICE_WORKER_ERROR_KILLED_WITH_BLOB;
81 case m::REQUEST_JOB_ERROR_KILLED_WITH_STREAM:
82 return n::TYPE_SERVICE_WORKER_ERROR_KILLED_WITH_STREAM;
83 // We can't log if there's no request; fallthrough.
84 case m::REQUEST_JOB_ERROR_NO_REQUEST:
85 // Obsolete types; fallthrough.
86 case m::REQUEST_JOB_ERROR_DESTROYED:
87 case m::REQUEST_JOB_ERROR_DESTROYED_WITH_BLOB:
88 case m::REQUEST_JOB_ERROR_DESTROYED_WITH_STREAM:
89 // Invalid type.
90 case m::NUM_REQUEST_JOB_RESULT_TYPES:
91 NOTREACHED() << result;
93 NOTREACHED() << result;
94 return n::TYPE_FAILED;
97 } // namespace
99 ServiceWorkerURLRequestJob::ServiceWorkerURLRequestJob(
100 net::URLRequest* request,
101 net::NetworkDelegate* network_delegate,
102 base::WeakPtr<ServiceWorkerProviderHost> provider_host,
103 base::WeakPtr<storage::BlobStorageContext> blob_storage_context,
104 const ResourceContext* resource_context,
105 FetchRequestMode request_mode,
106 FetchCredentialsMode credentials_mode,
107 bool is_main_resource_load,
108 RequestContextType request_context_type,
109 RequestContextFrameType frame_type,
110 scoped_refptr<ResourceRequestBody> body)
111 : net::URLRequestJob(request, network_delegate),
112 provider_host_(provider_host),
113 response_type_(NOT_DETERMINED),
114 is_started_(false),
115 service_worker_response_type_(blink::WebServiceWorkerResponseTypeDefault),
116 blob_storage_context_(blob_storage_context),
117 resource_context_(resource_context),
118 stream_pending_buffer_size_(0),
119 request_mode_(request_mode),
120 credentials_mode_(credentials_mode),
121 is_main_resource_load_(is_main_resource_load),
122 request_context_type_(request_context_type),
123 frame_type_(frame_type),
124 fall_back_required_(false),
125 body_(body),
126 weak_factory_(this) {
129 void ServiceWorkerURLRequestJob::FallbackToNetwork() {
130 DCHECK_EQ(NOT_DETERMINED, response_type_);
131 response_type_ = FALLBACK_TO_NETWORK;
132 MaybeStartRequest();
135 void ServiceWorkerURLRequestJob::ForwardToServiceWorker() {
136 DCHECK_EQ(NOT_DETERMINED, response_type_);
137 response_type_ = FORWARD_TO_SERVICE_WORKER;
138 MaybeStartRequest();
141 void ServiceWorkerURLRequestJob::Start() {
142 is_started_ = true;
143 MaybeStartRequest();
146 void ServiceWorkerURLRequestJob::Kill() {
147 net::URLRequestJob::Kill();
148 ClearStream();
149 fetch_dispatcher_.reset();
150 blob_request_.reset();
151 weak_factory_.InvalidateWeakPtrs();
154 net::LoadState ServiceWorkerURLRequestJob::GetLoadState() const {
155 // TODO(kinuko): refine this for better debug.
156 return net::URLRequestJob::GetLoadState();
159 bool ServiceWorkerURLRequestJob::GetCharset(std::string* charset) {
160 if (!http_info())
161 return false;
162 return http_info()->headers->GetCharset(charset);
165 bool ServiceWorkerURLRequestJob::GetMimeType(std::string* mime_type) const {
166 if (!http_info())
167 return false;
168 return http_info()->headers->GetMimeType(mime_type);
171 void ServiceWorkerURLRequestJob::GetResponseInfo(net::HttpResponseInfo* info) {
172 if (!http_info())
173 return;
174 const base::Time request_time = info->request_time;
175 *info = *http_info();
176 info->request_time = request_time;
177 info->response_time = response_time_;
180 void ServiceWorkerURLRequestJob::GetLoadTimingInfo(
181 net::LoadTimingInfo* load_timing_info) const {
182 *load_timing_info = load_timing_info_;
185 int ServiceWorkerURLRequestJob::GetResponseCode() const {
186 if (!http_info())
187 return -1;
188 return http_info()->headers->response_code();
191 void ServiceWorkerURLRequestJob::SetExtraRequestHeaders(
192 const net::HttpRequestHeaders& headers) {
193 std::string range_header;
194 std::vector<net::HttpByteRange> ranges;
195 if (!headers.GetHeader(net::HttpRequestHeaders::kRange, &range_header) ||
196 !net::HttpUtil::ParseRangeHeader(range_header, &ranges)) {
197 return;
200 // We don't support multiple range requests in one single URL request.
201 if (ranges.size() == 1U)
202 byte_range_ = ranges[0];
205 bool ServiceWorkerURLRequestJob::ReadRawData(
206 net::IOBuffer* buf, int buf_size, int *bytes_read) {
207 DCHECK(buf);
208 DCHECK_GE(buf_size, 0);
209 DCHECK(bytes_read);
210 DCHECK(waiting_stream_url_.is_empty());
211 if (stream_.get()) {
212 switch (stream_->ReadRawData(buf, buf_size, bytes_read)) {
213 case Stream::STREAM_HAS_DATA:
214 DCHECK_GT(*bytes_read, 0);
215 return true;
216 case Stream::STREAM_COMPLETE:
217 DCHECK(!*bytes_read);
218 RecordResult(ServiceWorkerMetrics::REQUEST_JOB_STREAM_RESPONSE);
219 return true;
220 case Stream::STREAM_EMPTY:
221 stream_pending_buffer_ = buf;
222 stream_pending_buffer_size_ = buf_size;
223 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0));
224 return false;
225 case Stream::STREAM_ABORTED:
226 // Handle this as connection reset.
227 RecordResult(ServiceWorkerMetrics::REQUEST_JOB_ERROR_STREAM_ABORTED);
228 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED,
229 net::ERR_CONNECTION_RESET));
230 return false;
232 NOTREACHED();
233 return false;
236 if (!blob_request_) {
237 *bytes_read = 0;
238 return true;
240 blob_request_->Read(buf, buf_size, bytes_read);
241 net::URLRequestStatus status = blob_request_->status();
242 SetStatus(status);
243 if (status.is_io_pending())
244 return false;
245 if (status.is_success() && *bytes_read == 0)
246 RecordResult(ServiceWorkerMetrics::REQUEST_JOB_BLOB_RESPONSE);
247 return status.is_success();
250 // TODO(falken): Refactor Blob and Stream specific handling to separate classes.
251 // Overrides for Blob reading -------------------------------------------------
253 void ServiceWorkerURLRequestJob::OnReceivedRedirect(
254 net::URLRequest* request,
255 const net::RedirectInfo& redirect_info,
256 bool* defer_redirect) {
257 NOTREACHED();
260 void ServiceWorkerURLRequestJob::OnAuthRequired(
261 net::URLRequest* request,
262 net::AuthChallengeInfo* auth_info) {
263 NOTREACHED();
266 void ServiceWorkerURLRequestJob::OnCertificateRequested(
267 net::URLRequest* request,
268 net::SSLCertRequestInfo* cert_request_info) {
269 NOTREACHED();
272 void ServiceWorkerURLRequestJob::OnSSLCertificateError(
273 net::URLRequest* request,
274 const net::SSLInfo& ssl_info,
275 bool fatal) {
276 NOTREACHED();
279 void ServiceWorkerURLRequestJob::OnBeforeNetworkStart(net::URLRequest* request,
280 bool* defer) {
281 NOTREACHED();
284 void ServiceWorkerURLRequestJob::OnResponseStarted(net::URLRequest* request) {
285 // TODO(falken): Add Content-Length, Content-Type if they were not provided in
286 // the ServiceWorkerResponse.
287 response_time_ = base::Time::Now();
288 CommitResponseHeader();
291 void ServiceWorkerURLRequestJob::OnReadCompleted(net::URLRequest* request,
292 int bytes_read) {
293 SetStatus(request->status());
294 if (!request->status().is_success()) {
295 RecordResult(ServiceWorkerMetrics::REQUEST_JOB_ERROR_BLOB_READ);
296 NotifyDone(request->status());
297 return;
300 if (bytes_read == 0) {
301 // Protect because NotifyReadComplete() can destroy |this|.
302 scoped_refptr<ServiceWorkerURLRequestJob> protect(this);
303 RecordResult(ServiceWorkerMetrics::REQUEST_JOB_BLOB_RESPONSE);
304 NotifyReadComplete(bytes_read);
305 NotifyDone(request->status());
306 return;
308 NotifyReadComplete(bytes_read);
311 // Overrides for Stream reading -----------------------------------------------
313 void ServiceWorkerURLRequestJob::OnDataAvailable(Stream* stream) {
314 // Clear the IO_PENDING status.
315 SetStatus(net::URLRequestStatus());
316 // Do nothing if stream_pending_buffer_ is empty, i.e. there's no ReadRawData
317 // operation waiting for IO completion.
318 if (!stream_pending_buffer_.get())
319 return;
321 // stream_pending_buffer_ is set to the IOBuffer instance provided to
322 // ReadRawData() by URLRequestJob.
324 int bytes_read = 0;
325 switch (stream_->ReadRawData(
326 stream_pending_buffer_.get(), stream_pending_buffer_size_, &bytes_read)) {
327 case Stream::STREAM_HAS_DATA:
328 DCHECK_GT(bytes_read, 0);
329 break;
330 case Stream::STREAM_COMPLETE:
331 // Calling NotifyReadComplete with 0 signals completion.
332 DCHECK(!bytes_read);
333 RecordResult(ServiceWorkerMetrics::REQUEST_JOB_STREAM_RESPONSE);
334 break;
335 case Stream::STREAM_EMPTY:
336 NOTREACHED();
337 break;
338 case Stream::STREAM_ABORTED:
339 // Handle this as connection reset.
340 RecordResult(ServiceWorkerMetrics::REQUEST_JOB_ERROR_STREAM_ABORTED);
341 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED,
342 net::ERR_CONNECTION_RESET));
343 break;
346 // Clear the buffers before notifying the read is complete, so that it is
347 // safe for the observer to read.
348 stream_pending_buffer_ = nullptr;
349 stream_pending_buffer_size_ = 0;
350 NotifyReadComplete(bytes_read);
353 void ServiceWorkerURLRequestJob::OnStreamRegistered(Stream* stream) {
354 StreamContext* stream_context =
355 GetStreamContextForResourceContext(resource_context_);
356 stream_context->registry()->RemoveRegisterObserver(waiting_stream_url_);
357 waiting_stream_url_ = GURL();
358 stream_ = stream;
359 stream_->SetReadObserver(this);
360 CommitResponseHeader();
363 // Misc -----------------------------------------------------------------------
365 const net::HttpResponseInfo* ServiceWorkerURLRequestJob::http_info() const {
366 if (!http_response_info_)
367 return nullptr;
368 if (range_response_info_)
369 return range_response_info_.get();
370 return http_response_info_.get();
373 void ServiceWorkerURLRequestJob::GetExtraResponseInfo(
374 ResourceResponseInfo* response_info) const {
375 if (response_type_ != FORWARD_TO_SERVICE_WORKER) {
376 response_info->was_fetched_via_service_worker = false;
377 response_info->was_fallback_required_by_service_worker = false;
378 response_info->original_url_via_service_worker = GURL();
379 response_info->response_type_via_service_worker =
380 blink::WebServiceWorkerResponseTypeDefault;
381 return;
383 response_info->was_fetched_via_service_worker = true;
384 response_info->was_fallback_required_by_service_worker = fall_back_required_;
385 response_info->original_url_via_service_worker = response_url_;
386 response_info->response_type_via_service_worker =
387 service_worker_response_type_;
388 response_info->service_worker_start_time = worker_start_time_;
389 response_info->service_worker_ready_time = worker_ready_time_;
393 ServiceWorkerURLRequestJob::~ServiceWorkerURLRequestJob() {
394 ClearStream();
396 if (!ShouldRecordResult())
397 return;
398 ServiceWorkerMetrics::URLRequestJobResult result =
399 ServiceWorkerMetrics::REQUEST_JOB_ERROR_KILLED;
400 if (response_body_type_ == STREAM)
401 result = ServiceWorkerMetrics::REQUEST_JOB_ERROR_KILLED_WITH_STREAM;
402 else if (response_body_type_ == BLOB)
403 result = ServiceWorkerMetrics::REQUEST_JOB_ERROR_KILLED_WITH_BLOB;
404 RecordResult(result);
407 void ServiceWorkerURLRequestJob::MaybeStartRequest() {
408 if (is_started_ && response_type_ != NOT_DETERMINED) {
409 // Start asynchronously.
410 base::ThreadTaskRunnerHandle::Get()->PostTask(
411 FROM_HERE, base::Bind(&ServiceWorkerURLRequestJob::StartRequest,
412 weak_factory_.GetWeakPtr()));
416 void ServiceWorkerURLRequestJob::StartRequest() {
417 if (request()) {
418 request()->net_log().AddEvent(
419 net::NetLog::TYPE_SERVICE_WORKER_START_REQUEST);
422 switch (response_type_) {
423 case NOT_DETERMINED:
424 NOTREACHED();
425 return;
427 case FALLBACK_TO_NETWORK:
428 // Restart the request to create a new job. Our request handler will
429 // return nullptr, and the default job (which will hit network) should be
430 // created.
431 NotifyRestartRequired();
432 return;
434 case FORWARD_TO_SERVICE_WORKER:
435 if (!provider_host_) {
436 RecordResult(ServiceWorkerMetrics::REQUEST_JOB_ERROR_NO_PROVIDER_HOST);
437 DeliverErrorResponse();
438 return;
440 if (!provider_host_->active_version()) {
441 RecordResult(ServiceWorkerMetrics::REQUEST_JOB_ERROR_NO_ACTIVE_VERSION);
442 DeliverErrorResponse();
443 return;
446 DCHECK(!fetch_dispatcher_);
447 // Send a fetch event to the ServiceWorker associated to the
448 // provider_host.
449 fetch_dispatcher_.reset(new ServiceWorkerFetchDispatcher(
450 CreateFetchRequest(),
451 provider_host_->active_version(),
452 base::Bind(&ServiceWorkerURLRequestJob::DidPrepareFetchEvent,
453 weak_factory_.GetWeakPtr()),
454 base::Bind(&ServiceWorkerURLRequestJob::DidDispatchFetchEvent,
455 weak_factory_.GetWeakPtr())));
456 worker_start_time_ = base::TimeTicks::Now();
457 fetch_dispatcher_->Run();
458 return;
461 NOTREACHED();
464 scoped_ptr<ServiceWorkerFetchRequest>
465 ServiceWorkerURLRequestJob::CreateFetchRequest() {
466 std::string blob_uuid;
467 uint64 blob_size = 0;
468 CreateRequestBodyBlob(&blob_uuid, &blob_size);
469 scoped_ptr<ServiceWorkerFetchRequest> request(
470 new ServiceWorkerFetchRequest());
471 request->mode = request_mode_;
472 request->request_context_type = request_context_type_;
473 request->frame_type = frame_type_;
474 request->url = request_->url();
475 request->method = request_->method();
476 const net::HttpRequestHeaders& headers = request_->extra_request_headers();
477 for (net::HttpRequestHeaders::Iterator it(headers); it.GetNext();) {
478 if (ServiceWorkerContext::IsExcludedHeaderNameForFetchEvent(it.name()))
479 continue;
480 request->headers[it.name()] = it.value();
482 request->blob_uuid = blob_uuid;
483 request->blob_size = blob_size;
484 request->credentials_mode = credentials_mode_;
485 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request_);
486 if (info) {
487 request->is_reload = ui::PageTransitionCoreTypeIs(
488 info->GetPageTransition(), ui::PAGE_TRANSITION_RELOAD);
489 request->referrer =
490 Referrer(GURL(request_->referrer()), info->GetReferrerPolicy());
491 } else {
492 CHECK(
493 request_->referrer_policy() ==
494 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE);
495 request->referrer =
496 Referrer(GURL(request_->referrer()), blink::WebReferrerPolicyDefault);
498 return request.Pass();
501 bool ServiceWorkerURLRequestJob::CreateRequestBodyBlob(std::string* blob_uuid,
502 uint64* blob_size) {
503 if (!body_.get() || !blob_storage_context_)
504 return false;
506 // To ensure the blobs stick around until the end of the reading.
507 ScopedVector<storage::BlobDataHandle> handles;
508 ScopedVector<storage::BlobDataSnapshot> snapshots;
509 // TODO(dmurph): Allow blobs to be added below, so that the context can
510 // efficiently re-use blob items for the new blob.
511 std::vector<const ResourceRequestBody::Element*> resolved_elements;
512 for (const ResourceRequestBody::Element& element : (*body_->elements())) {
513 if (element.type() != ResourceRequestBody::Element::TYPE_BLOB) {
514 resolved_elements.push_back(&element);
515 continue;
517 scoped_ptr<storage::BlobDataHandle> handle =
518 blob_storage_context_->GetBlobDataFromUUID(element.blob_uuid());
519 scoped_ptr<storage::BlobDataSnapshot> snapshot = handle->CreateSnapshot();
520 if (snapshot->items().empty())
521 continue;
522 const auto& items = snapshot->items();
523 for (const auto& item : items) {
524 DCHECK_NE(storage::DataElement::TYPE_BLOB, item->type());
525 resolved_elements.push_back(item->data_element_ptr());
527 handles.push_back(handle.release());
528 snapshots.push_back(snapshot.release());
531 const std::string uuid(base::GenerateGUID());
532 uint64 total_size = 0;
534 storage::BlobDataBuilder blob_builder(uuid);
535 for (size_t i = 0; i < resolved_elements.size(); ++i) {
536 const ResourceRequestBody::Element& element = *resolved_elements[i];
537 if (total_size != kuint64max && element.length() != kuint64max)
538 total_size += element.length();
539 else
540 total_size = kuint64max;
541 switch (element.type()) {
542 case ResourceRequestBody::Element::TYPE_BYTES:
543 blob_builder.AppendData(element.bytes(), element.length());
544 break;
545 case ResourceRequestBody::Element::TYPE_FILE:
546 blob_builder.AppendFile(element.path(), element.offset(),
547 element.length(),
548 element.expected_modification_time());
549 break;
550 case ResourceRequestBody::Element::TYPE_BLOB:
551 // Blob elements should be resolved beforehand.
552 NOTREACHED();
553 break;
554 case ResourceRequestBody::Element::TYPE_FILE_FILESYSTEM:
555 blob_builder.AppendFileSystemFile(element.filesystem_url(),
556 element.offset(), element.length(),
557 element.expected_modification_time());
558 break;
559 default:
560 NOTIMPLEMENTED();
564 request_body_blob_data_handle_ =
565 blob_storage_context_->AddFinishedBlob(&blob_builder);
566 *blob_uuid = uuid;
567 *blob_size = total_size;
568 return true;
571 void ServiceWorkerURLRequestJob::DidPrepareFetchEvent() {
572 worker_ready_time_ = base::TimeTicks::Now();
573 load_timing_info_.send_start = worker_ready_time_;
576 void ServiceWorkerURLRequestJob::DidDispatchFetchEvent(
577 ServiceWorkerStatusCode status,
578 ServiceWorkerFetchEventResult fetch_result,
579 const ServiceWorkerResponse& response,
580 scoped_refptr<ServiceWorkerVersion> version) {
581 fetch_dispatcher_.reset();
582 ServiceWorkerMetrics::RecordFetchEventStatus(is_main_resource_load_, status);
584 // Check if we're not orphaned.
585 if (!request()) {
586 RecordResult(ServiceWorkerMetrics::REQUEST_JOB_ERROR_NO_REQUEST);
587 return;
590 if (status != SERVICE_WORKER_OK) {
591 RecordResult(ServiceWorkerMetrics::REQUEST_JOB_ERROR_FETCH_EVENT_DISPATCH);
592 if (is_main_resource_load_) {
593 // Using the service worker failed, so fallback to network. Detach the
594 // controller so subresource requests also skip the worker.
595 provider_host_->NotifyControllerLost();
596 response_type_ = FALLBACK_TO_NETWORK;
597 NotifyRestartRequired();
598 } else {
599 DeliverErrorResponse();
601 return;
604 if (fetch_result == SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK) {
605 // When the request_mode is |CORS| or |CORS-with-forced-preflight| we can't
606 // simply fallback to the network in the browser process. It is because the
607 // CORS preflight logic is implemented in the renderer. So we returns a
608 // fall_back_required response to the renderer.
609 if (request_mode_ == FETCH_REQUEST_MODE_CORS ||
610 request_mode_ == FETCH_REQUEST_MODE_CORS_WITH_FORCED_PREFLIGHT) {
611 fall_back_required_ = true;
612 RecordResult(ServiceWorkerMetrics::REQUEST_JOB_FALLBACK_FOR_CORS);
613 CreateResponseHeader(
614 400, "Service Worker Fallback Required", ServiceWorkerHeaderMap());
615 CommitResponseHeader();
616 return;
618 // Change the response type and restart the request to fallback to
619 // the network.
620 RecordResult(ServiceWorkerMetrics::REQUEST_JOB_FALLBACK_RESPONSE);
621 response_type_ = FALLBACK_TO_NETWORK;
622 NotifyRestartRequired();
623 return;
626 // We should have a response now.
627 DCHECK_EQ(SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE, fetch_result);
629 // A response with status code 0 is Blink telling us to respond with network
630 // error.
631 if (response.status_code == 0) {
632 RecordStatusZeroResponseError(response.error);
633 NotifyDone(
634 net::URLRequestStatus(net::URLRequestStatus::FAILED, net::ERR_FAILED));
635 return;
638 load_timing_info_.send_end = base::TimeTicks::Now();
640 // Creates a new HttpResponseInfo using the the ServiceWorker script's
641 // HttpResponseInfo to show HTTPS padlock.
642 // TODO(horo): When we support mixed-content (HTTP) no-cors requests from a
643 // ServiceWorker, we have to check the security level of the responses.
644 DCHECK(!http_response_info_);
645 DCHECK(version);
646 const net::HttpResponseInfo* main_script_http_info =
647 version->GetMainScriptHttpResponseInfo();
648 if (main_script_http_info) {
649 // In normal case |main_script_http_info| must be set while starting the
650 // ServiceWorker. But when the ServiceWorker registration database was not
651 // written correctly, it may be null.
652 // TODO(horo): Change this line to DCHECK when crbug.com/485900 is fixed.
653 http_response_info_.reset(
654 new net::HttpResponseInfo(*main_script_http_info));
657 // Set up a request for reading the stream.
658 if (response.stream_url.is_valid()) {
659 DCHECK(response.blob_uuid.empty());
660 SetResponseBodyType(STREAM);
661 streaming_version_ = version;
662 streaming_version_->AddStreamingURLRequestJob(this);
663 response_url_ = response.url;
664 service_worker_response_type_ = response.response_type;
665 CreateResponseHeader(
666 response.status_code, response.status_text, response.headers);
667 load_timing_info_.receive_headers_end = base::TimeTicks::Now();
668 StreamContext* stream_context =
669 GetStreamContextForResourceContext(resource_context_);
670 stream_ =
671 stream_context->registry()->GetStream(response.stream_url);
672 if (!stream_.get()) {
673 waiting_stream_url_ = response.stream_url;
674 // Wait for StreamHostMsg_StartBuilding message from the ServiceWorker.
675 stream_context->registry()->SetRegisterObserver(waiting_stream_url_,
676 this);
677 return;
679 stream_->SetReadObserver(this);
680 CommitResponseHeader();
681 return;
684 // Set up a request for reading the blob.
685 if (!response.blob_uuid.empty() && blob_storage_context_) {
686 SetResponseBodyType(BLOB);
687 scoped_ptr<storage::BlobDataHandle> blob_data_handle =
688 blob_storage_context_->GetBlobDataFromUUID(response.blob_uuid);
689 if (!blob_data_handle) {
690 // The renderer gave us a bad blob UUID.
691 RecordResult(ServiceWorkerMetrics::REQUEST_JOB_ERROR_BAD_BLOB);
692 DeliverErrorResponse();
693 return;
695 blob_request_ = storage::BlobProtocolHandler::CreateBlobRequest(
696 blob_data_handle.Pass(), request()->context(), this);
697 blob_request_->Start();
700 response_url_ = response.url;
701 service_worker_response_type_ = response.response_type;
702 CreateResponseHeader(
703 response.status_code, response.status_text, response.headers);
704 load_timing_info_.receive_headers_end = base::TimeTicks::Now();
705 if (!blob_request_) {
706 RecordResult(ServiceWorkerMetrics::REQUEST_JOB_HEADERS_ONLY_RESPONSE);
707 CommitResponseHeader();
711 void ServiceWorkerURLRequestJob::CreateResponseHeader(
712 int status_code,
713 const std::string& status_text,
714 const ServiceWorkerHeaderMap& headers) {
715 // TODO(kinuko): If the response has an identifier to on-disk cache entry,
716 // pull response header from the disk.
717 std::string status_line(
718 base::StringPrintf("HTTP/1.1 %d %s", status_code, status_text.c_str()));
719 status_line.push_back('\0');
720 http_response_headers_ = new net::HttpResponseHeaders(status_line);
721 for (ServiceWorkerHeaderMap::const_iterator it = headers.begin();
722 it != headers.end();
723 ++it) {
724 std::string header;
725 header.reserve(it->first.size() + 2 + it->second.size());
726 header.append(it->first);
727 header.append(": ");
728 header.append(it->second);
729 http_response_headers_->AddHeader(header);
733 void ServiceWorkerURLRequestJob::CommitResponseHeader() {
734 if (!http_response_info_)
735 http_response_info_.reset(new net::HttpResponseInfo());
736 http_response_info_->headers.swap(http_response_headers_);
737 http_response_info_->vary_data = net::HttpVaryData();
738 http_response_info_->metadata = nullptr;
739 NotifyHeadersComplete();
742 void ServiceWorkerURLRequestJob::DeliverErrorResponse() {
743 // TODO(falken): Print an error to the console of the ServiceWorker and of
744 // the requesting page.
745 CreateResponseHeader(
746 500, "Service Worker Response Error", ServiceWorkerHeaderMap());
747 CommitResponseHeader();
750 void ServiceWorkerURLRequestJob::SetResponseBodyType(ResponseBodyType type) {
751 DCHECK_EQ(response_body_type_, UNKNOWN);
752 DCHECK_NE(type, UNKNOWN);
753 response_body_type_ = type;
756 bool ServiceWorkerURLRequestJob::ShouldRecordResult() {
757 return !did_record_result_ && is_started_ &&
758 response_type_ == FORWARD_TO_SERVICE_WORKER;
761 void ServiceWorkerURLRequestJob::RecordResult(
762 ServiceWorkerMetrics::URLRequestJobResult result) {
763 // It violates style guidelines to handle a NOTREACHED() failure but if there
764 // is a bug don't let it corrupt UMA results by double-counting.
765 if (!ShouldRecordResult()) {
766 NOTREACHED();
767 return;
769 did_record_result_ = true;
770 ServiceWorkerMetrics::RecordURLRequestJobResult(is_main_resource_load_,
771 result);
772 if (request())
773 request()->net_log().AddEvent(RequestJobResultToNetEventType(result));
776 void ServiceWorkerURLRequestJob::RecordStatusZeroResponseError(
777 blink::WebServiceWorkerResponseError error) {
778 // It violates style guidelines to handle a NOTREACHED() failure but if there
779 // is a bug don't let it corrupt UMA results by double-counting.
780 if (!ShouldRecordResult()) {
781 NOTREACHED();
782 return;
784 RecordResult(ServiceWorkerMetrics::REQUEST_JOB_ERROR_RESPONSE_STATUS_ZERO);
785 ServiceWorkerMetrics::RecordStatusZeroResponseError(is_main_resource_load_,
786 error);
789 void ServiceWorkerURLRequestJob::ClearStream() {
790 if (streaming_version_) {
791 streaming_version_->RemoveStreamingURLRequestJob(this);
792 streaming_version_ = nullptr;
794 if (stream_) {
795 stream_->RemoveReadObserver(this);
796 stream_->Abort();
797 stream_ = nullptr;
799 if (!waiting_stream_url_.is_empty()) {
800 StreamRegistry* stream_registry =
801 GetStreamContextForResourceContext(resource_context_)->registry();
802 stream_registry->RemoveRegisterObserver(waiting_stream_url_);
803 stream_registry->AbortPendingStream(waiting_stream_url_);
807 } // namespace content