| 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 "content/browser/service_worker/service_worker_url_request_job.h" | 5 #include "content/browser/service_worker/service_worker_url_request_job.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <limits> | 10 #include <limits> |
| 11 #include <map> | 11 #include <map> |
| 12 #include <memory> | 12 #include <memory> |
| 13 #include <string> | 13 #include <string> |
| 14 #include <utility> | 14 #include <utility> |
| 15 #include <vector> | 15 #include <vector> |
| 16 | 16 |
| 17 #include "base/bind.h" | 17 #include "base/bind.h" |
| 18 #include "base/guid.h" | 18 #include "base/guid.h" |
| 19 #include "base/location.h" | 19 #include "base/location.h" |
| 20 #include "base/single_thread_task_runner.h" | 20 #include "base/single_thread_task_runner.h" |
| 21 #include "base/strings/stringprintf.h" | 21 #include "base/strings/stringprintf.h" |
| 22 #include "base/thread_task_runner_handle.h" | 22 #include "base/thread_task_runner_handle.h" |
| 23 #include "base/time/time.h" | 23 #include "base/time/time.h" |
| 24 #include "content/browser/resource_context_impl.h" | 24 #include "content/browser/resource_context_impl.h" |
| 25 #include "content/browser/service_worker/service_worker_fetch_dispatcher.h" | 25 #include "content/browser/service_worker/service_worker_fetch_dispatcher.h" |
| 26 #include "content/browser/service_worker/service_worker_provider_host.h" | 26 #include "content/browser/service_worker/service_worker_provider_host.h" |
| 27 #include "content/browser/service_worker/service_worker_response_info.h" |
| 27 #include "content/browser/streams/stream.h" | 28 #include "content/browser/streams/stream.h" |
| 28 #include "content/browser/streams/stream_context.h" | 29 #include "content/browser/streams/stream_context.h" |
| 29 #include "content/browser/streams/stream_registry.h" | 30 #include "content/browser/streams/stream_registry.h" |
| 30 #include "content/common/resource_request_body.h" | 31 #include "content/common/resource_request_body.h" |
| 31 #include "content/common/service_worker/service_worker_types.h" | 32 #include "content/common/service_worker/service_worker_types.h" |
| 32 #include "content/common/service_worker/service_worker_utils.h" | 33 #include "content/common/service_worker/service_worker_utils.h" |
| 33 #include "content/public/browser/blob_handle.h" | 34 #include "content/public/browser/blob_handle.h" |
| 34 #include "content/public/browser/resource_request_info.h" | 35 #include "content/public/browser/resource_request_info.h" |
| 35 #include "content/public/browser/service_worker_context.h" | 36 #include "content/public/browser/service_worker_context.h" |
| 36 #include "content/public/common/referrer.h" | 37 #include "content/public/common/referrer.h" |
| (...skipping 569 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 606 ServiceWorkerMetrics::RecordFallbackedRequestMode(request_mode_); | 607 ServiceWorkerMetrics::RecordFallbackedRequestMode(request_mode_); |
| 607 // When the request_mode is |CORS| or |CORS-with-forced-preflight| and the | 608 // When the request_mode is |CORS| or |CORS-with-forced-preflight| and the |
| 608 // origin of the request URL is different from the security origin of the | 609 // origin of the request URL is different from the security origin of the |
| 609 // document, we can't simply fallback to the network in the browser process. | 610 // document, we can't simply fallback to the network in the browser process. |
| 610 // It is because the CORS preflight logic is implemented in the renderer. So | 611 // It is because the CORS preflight logic is implemented in the renderer. So |
| 611 // we returns a fall_back_required response to the renderer. | 612 // we returns a fall_back_required response to the renderer. |
| 612 if ((request_mode_ == FETCH_REQUEST_MODE_CORS || | 613 if ((request_mode_ == FETCH_REQUEST_MODE_CORS || |
| 613 request_mode_ == FETCH_REQUEST_MODE_CORS_WITH_FORCED_PREFLIGHT) && | 614 request_mode_ == FETCH_REQUEST_MODE_CORS_WITH_FORCED_PREFLIGHT) && |
| 614 !request()->initiator().IsSameOriginWith( | 615 !request()->initiator().IsSameOriginWith( |
| 615 url::Origin(request()->url()))) { | 616 url::Origin(request()->url()))) { |
| 616 fall_back_required_ = true; | 617 // TODO(mek): http://crbug.com/604084 Figure out what to do about CORS |
| 618 // preflight and fallbacks for foreign fetch events. |
| 619 fall_back_required_ = |
| 620 fetch_type_ != ServiceWorkerFetchType::FOREIGN_FETCH; |
| 617 RecordResult(ServiceWorkerMetrics::REQUEST_JOB_FALLBACK_FOR_CORS); | 621 RecordResult(ServiceWorkerMetrics::REQUEST_JOB_FALLBACK_FOR_CORS); |
| 618 CreateResponseHeader( | 622 CreateResponseHeader( |
| 619 400, "Service Worker Fallback Required", ServiceWorkerHeaderMap()); | 623 400, "Service Worker Fallback Required", ServiceWorkerHeaderMap()); |
| 620 CommitResponseHeader(); | 624 CommitResponseHeader(); |
| 621 return; | 625 return; |
| 622 } | 626 } |
| 623 // Change the response type and restart the request to fallback to | 627 // Change the response type and restart the request to fallback to |
| 624 // the network. | 628 // the network. |
| 625 RecordResult(ServiceWorkerMetrics::REQUEST_JOB_FALLBACK_RESPONSE); | 629 RecordResult(ServiceWorkerMetrics::REQUEST_JOB_FALLBACK_RESPONSE); |
| 626 response_type_ = FALLBACK_TO_NETWORK; | 630 response_type_ = FALLBACK_TO_NETWORK; |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 817 URLRequestJob::NotifyHeadersComplete(); | 821 URLRequestJob::NotifyHeadersComplete(); |
| 818 } | 822 } |
| 819 | 823 |
| 820 void ServiceWorkerURLRequestJob::NotifyStartError( | 824 void ServiceWorkerURLRequestJob::NotifyStartError( |
| 821 net::URLRequestStatus status) { | 825 net::URLRequestStatus status) { |
| 822 OnStartCompleted(); | 826 OnStartCompleted(); |
| 823 URLRequestJob::NotifyStartError(status); | 827 URLRequestJob::NotifyStartError(status); |
| 824 } | 828 } |
| 825 | 829 |
| 826 void ServiceWorkerURLRequestJob::NotifyRestartRequired() { | 830 void ServiceWorkerURLRequestJob::NotifyRestartRequired() { |
| 827 delegate_->OnPrepareToRestart(worker_start_time_, worker_ready_time_); | 831 ServiceWorkerResponseInfo::ForRequest(request_, true) |
| 832 ->OnPrepareToRestart(worker_start_time_, worker_ready_time_); |
| 833 delegate_->OnPrepareToRestart(); |
| 828 URLRequestJob::NotifyRestartRequired(); | 834 URLRequestJob::NotifyRestartRequired(); |
| 829 } | 835 } |
| 830 | 836 |
| 831 void ServiceWorkerURLRequestJob::OnStartCompleted() const { | 837 void ServiceWorkerURLRequestJob::OnStartCompleted() const { |
| 832 if (response_type_ != FORWARD_TO_SERVICE_WORKER) { | 838 if (response_type_ != FORWARD_TO_SERVICE_WORKER) { |
| 833 delegate_->OnStartCompleted( | 839 ServiceWorkerResponseInfo::ForRequest(request_, true) |
| 834 false /* was_fetched_via_service_worker */, | 840 ->OnStartCompleted( |
| 835 false /* was_fallback_required */, | 841 false /* was_fetched_via_service_worker */, |
| 836 GURL() /* original_url_via_service_worker */, | 842 false /* was_fallback_required */, |
| 837 blink::WebServiceWorkerResponseTypeDefault, | 843 GURL() /* original_url_via_service_worker */, |
| 838 base::TimeTicks() /* service_worker_start_time */, | 844 blink::WebServiceWorkerResponseTypeDefault, |
| 839 base::TimeTicks() /* service_worker_ready_time */, | 845 base::TimeTicks() /* service_worker_start_time */, |
| 840 false /* respons_is_in_cache_storage */, | 846 base::TimeTicks() /* service_worker_ready_time */, |
| 841 std::string() /* response_cache_storage_cache_name */); | 847 false /* respons_is_in_cache_storage */, |
| 848 std::string() /* response_cache_storage_cache_name */); |
| 842 return; | 849 return; |
| 843 } | 850 } |
| 844 delegate_->OnStartCompleted(true /* was_fetched_via_service_worker */, | 851 ServiceWorkerResponseInfo::ForRequest(request_, true) |
| 845 fall_back_required_, response_url_, | 852 ->OnStartCompleted(true /* was_fetched_via_service_worker */, |
| 846 service_worker_response_type_, worker_start_time_, | 853 fall_back_required_, response_url_, |
| 847 worker_ready_time_, response_is_in_cache_storage_, | 854 service_worker_response_type_, worker_start_time_, |
| 848 response_cache_storage_cache_name_); | 855 worker_ready_time_, response_is_in_cache_storage_, |
| 856 response_cache_storage_cache_name_); |
| 849 } | 857 } |
| 850 | 858 |
| 851 bool ServiceWorkerURLRequestJob::IsMainResourceLoad() const { | 859 bool ServiceWorkerURLRequestJob::IsMainResourceLoad() const { |
| 852 return ServiceWorkerUtils::IsMainResourceType(resource_type_); | 860 return ServiceWorkerUtils::IsMainResourceType(resource_type_); |
| 853 } | 861 } |
| 854 | 862 |
| 855 } // namespace content | 863 } // namespace content |
| OLD | NEW |