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 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_URL_REQUEST_JOB_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_URL_REQUEST_JOB_H_
11 #include "base/memory/weak_ptr.h"
12 #include "base/time/time.h"
13 #include "content/common/content_export.h"
14 #include "content/common/service_worker/service_worker_status_code.h"
15 #include "content/common/service_worker/service_worker_types.h"
16 #include "content/public/common/request_context_frame_type.h"
17 #include "content/public/common/request_context_type.h"
18 #include "content/public/common/resource_type.h"
19 #include "net/http/http_byte_range.h"
20 #include "net/url_request/url_request.h"
21 #include "net/url_request/url_request_job.h"
22 #include "third_party/WebKit/public/platform/WebServiceWorkerResponseType.h"
26 class BlobStorageContext
;
31 class ResourceRequestBody
;
32 class ServiceWorkerContextCore
;
33 class ServiceWorkerFetchDispatcher
;
34 class ServiceWorkerProviderHost
;
36 class CONTENT_EXPORT ServiceWorkerURLRequestJob
37 : public net::URLRequestJob
,
38 public net::URLRequest::Delegate
{
40 ServiceWorkerURLRequestJob(
41 net::URLRequest
* request
,
42 net::NetworkDelegate
* network_delegate
,
43 base::WeakPtr
<ServiceWorkerProviderHost
> provider_host
,
44 base::WeakPtr
<storage::BlobStorageContext
> blob_storage_context
,
45 FetchRequestMode request_mode
,
46 FetchCredentialsMode credentials_mode
,
47 RequestContextType request_context_type
,
48 RequestContextFrameType frame_type
,
49 scoped_refptr
<ResourceRequestBody
> body
);
51 // Sets the response type.
52 void FallbackToNetwork();
53 void ForwardToServiceWorker();
55 bool ShouldFallbackToNetwork() const {
56 return response_type_
== FALLBACK_TO_NETWORK
;
58 bool ShouldForwardToServiceWorker() const {
59 return response_type_
== FORWARD_TO_SERVICE_WORKER
;
62 // net::URLRequestJob overrides:
63 void Start() override
;
65 net::LoadState
GetLoadState() const override
;
66 bool GetCharset(std::string
* charset
) override
;
67 bool GetMimeType(std::string
* mime_type
) const override
;
68 void GetResponseInfo(net::HttpResponseInfo
* info
) override
;
69 void GetLoadTimingInfo(net::LoadTimingInfo
* load_timing_info
) const override
;
70 int GetResponseCode() const override
;
71 void SetExtraRequestHeaders(const net::HttpRequestHeaders
& headers
) override
;
72 bool ReadRawData(net::IOBuffer
* buf
, int buf_size
, int* bytes_read
) override
;
74 // net::URLRequest::Delegate overrides that read the blob from the
75 // ServiceWorkerFetchResponse.
76 void OnReceivedRedirect(net::URLRequest
* request
,
77 const net::RedirectInfo
& redirect_info
,
78 bool* defer_redirect
) override
;
79 void OnAuthRequired(net::URLRequest
* request
,
80 net::AuthChallengeInfo
* auth_info
) override
;
81 void OnCertificateRequested(
82 net::URLRequest
* request
,
83 net::SSLCertRequestInfo
* cert_request_info
) override
;
84 void OnSSLCertificateError(net::URLRequest
* request
,
85 const net::SSLInfo
& ssl_info
,
87 void OnBeforeNetworkStart(net::URLRequest
* request
, bool* defer
) override
;
88 void OnResponseStarted(net::URLRequest
* request
) override
;
89 void OnReadCompleted(net::URLRequest
* request
, int bytes_read
) override
;
91 const net::HttpResponseInfo
* http_info() const;
93 void GetExtraResponseInfo(
94 bool* was_fetched_via_service_worker
,
95 bool* was_fallback_required_by_service_worker
,
96 GURL
* original_url_via_service_worker
,
97 blink::WebServiceWorkerResponseType
* response_type_via_service_worker
,
98 base::TimeTicks
* fetch_start_time
,
99 base::TimeTicks
* fetch_ready_time
,
100 base::TimeTicks
* fetch_end_time
) const;
103 ~ServiceWorkerURLRequestJob() override
;
109 FORWARD_TO_SERVICE_WORKER
,
112 // We start processing the request if Start() is called AND response_type_
114 void MaybeStartRequest();
117 // Creates ServiceWorkerFetchRequest from |request_| and |body_|.
118 scoped_ptr
<ServiceWorkerFetchRequest
> CreateFetchRequest();
120 // Creates BlobDataHandle of the request body from |body_|. This handle
121 // |request_body_blob_data_handle_| will be deleted when
122 // ServiceWorkerURLRequestJob is deleted.
123 bool CreateRequestBodyBlob(std::string
* blob_uuid
, uint64
* blob_size
);
125 // For FORWARD_TO_SERVICE_WORKER case.
126 void DidPrepareFetchEvent();
127 void DidDispatchFetchEvent(ServiceWorkerStatusCode status
,
128 ServiceWorkerFetchEventResult fetch_result
,
129 const ServiceWorkerResponse
& response
);
131 // Populates |http_response_headers_|.
132 void CreateResponseHeader(int status_code
,
133 const std::string
& status_text
,
134 const ServiceWorkerHeaderMap
& headers
);
136 // Creates |http_response_info_| using |http_response_headers_| and calls
137 // NotifyHeadersComplete.
138 void CommitResponseHeader();
140 // Creates and commits a response header indicating error.
141 void DeliverErrorResponse();
143 base::WeakPtr
<ServiceWorkerProviderHost
> provider_host_
;
145 // Timing info to show on the popup in Devtools' Network tab.
146 net::LoadTimingInfo load_timing_info_
;
147 base::TimeTicks fetch_start_time_
;
148 base::TimeTicks fetch_ready_time_
;
149 base::TimeTicks fetch_end_time_
;
150 base::Time response_time_
;
152 ResponseType response_type_
;
155 net::HttpByteRange byte_range_
;
156 scoped_ptr
<net::HttpResponseInfo
> range_response_info_
;
157 scoped_ptr
<net::HttpResponseInfo
> http_response_info_
;
158 // Headers that have not yet been committed to |http_response_info_|.
159 scoped_refptr
<net::HttpResponseHeaders
> http_response_headers_
;
161 blink::WebServiceWorkerResponseType service_worker_response_type_
;
163 // Used when response type is FORWARD_TO_SERVICE_WORKER.
164 scoped_ptr
<ServiceWorkerFetchDispatcher
> fetch_dispatcher_
;
165 base::WeakPtr
<storage::BlobStorageContext
> blob_storage_context_
;
166 scoped_ptr
<net::URLRequest
> blob_request_
;
167 FetchRequestMode request_mode_
;
168 FetchCredentialsMode credentials_mode_
;
169 RequestContextType request_context_type_
;
170 RequestContextFrameType frame_type_
;
171 bool fall_back_required_
;
172 // ResourceRequestBody has a collection of BlobDataHandles attached to it
173 // using the userdata mechanism. So we have to keep it not to free the blobs.
174 scoped_refptr
<ResourceRequestBody
> body_
;
175 scoped_ptr
<storage::BlobDataHandle
> request_body_blob_data_handle_
;
177 base::WeakPtrFactory
<ServiceWorkerURLRequestJob
> weak_factory_
;
179 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerURLRequestJob
);
182 } // namespace content
184 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_URL_REQUEST_JOB_H_