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/ref_counted.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/time/time.h"
14 #include "content/browser/streams/stream_read_observer.h"
15 #include "content/browser/streams/stream_register_observer.h"
16 #include "content/common/content_export.h"
17 #include "content/common/service_worker/service_worker_status_code.h"
18 #include "content/common/service_worker/service_worker_types.h"
19 #include "content/public/common/request_context_frame_type.h"
20 #include "content/public/common/request_context_type.h"
21 #include "content/public/common/resource_type.h"
22 #include "net/http/http_byte_range.h"
23 #include "net/url_request/url_request.h"
24 #include "net/url_request/url_request_job.h"
25 #include "third_party/WebKit/public/platform/WebServiceWorkerResponseType.h"
34 class BlobStorageContext
;
39 class ResourceContext
;
40 class ResourceRequestBody
;
41 class ServiceWorkerContextCore
;
42 class ServiceWorkerFetchDispatcher
;
43 class ServiceWorkerProviderHost
;
44 class ServiceWorkerVersion
;
47 class CONTENT_EXPORT ServiceWorkerURLRequestJob
48 : public net::URLRequestJob
,
49 public net::URLRequest::Delegate
,
50 public StreamReadObserver
,
51 public StreamRegisterObserver
{
53 ServiceWorkerURLRequestJob(
54 net::URLRequest
* request
,
55 net::NetworkDelegate
* network_delegate
,
56 base::WeakPtr
<ServiceWorkerProviderHost
> provider_host
,
57 base::WeakPtr
<storage::BlobStorageContext
> blob_storage_context
,
58 const ResourceContext
* resource_context
,
59 FetchRequestMode request_mode
,
60 FetchCredentialsMode credentials_mode
,
61 RequestContextType request_context_type
,
62 RequestContextFrameType frame_type
,
63 scoped_refptr
<ResourceRequestBody
> body
);
65 // Sets the response type.
66 void FallbackToNetwork();
67 void ForwardToServiceWorker();
69 bool ShouldFallbackToNetwork() const {
70 return response_type_
== FALLBACK_TO_NETWORK
;
72 bool ShouldForwardToServiceWorker() const {
73 return response_type_
== FORWARD_TO_SERVICE_WORKER
;
76 // net::URLRequestJob overrides:
77 void Start() override
;
79 net::LoadState
GetLoadState() const override
;
80 bool GetCharset(std::string
* charset
) override
;
81 bool GetMimeType(std::string
* mime_type
) const override
;
82 void GetResponseInfo(net::HttpResponseInfo
* info
) override
;
83 void GetLoadTimingInfo(net::LoadTimingInfo
* load_timing_info
) const override
;
84 int GetResponseCode() const override
;
85 void SetExtraRequestHeaders(const net::HttpRequestHeaders
& headers
) override
;
86 bool ReadRawData(net::IOBuffer
* buf
, int buf_size
, int* bytes_read
) override
;
88 // net::URLRequest::Delegate overrides that read the blob from the
89 // ServiceWorkerFetchResponse.
90 void OnReceivedRedirect(net::URLRequest
* request
,
91 const net::RedirectInfo
& redirect_info
,
92 bool* defer_redirect
) override
;
93 void OnAuthRequired(net::URLRequest
* request
,
94 net::AuthChallengeInfo
* auth_info
) override
;
95 void OnCertificateRequested(
96 net::URLRequest
* request
,
97 net::SSLCertRequestInfo
* cert_request_info
) override
;
98 void OnSSLCertificateError(net::URLRequest
* request
,
99 const net::SSLInfo
& ssl_info
,
100 bool fatal
) override
;
101 void OnBeforeNetworkStart(net::URLRequest
* request
, bool* defer
) override
;
102 void OnResponseStarted(net::URLRequest
* request
) override
;
103 void OnReadCompleted(net::URLRequest
* request
, int bytes_read
) override
;
105 // StreamObserver override:
106 void OnDataAvailable(Stream
* stream
) override
;
108 // StreamRegisterObserver override:
109 void OnStreamRegistered(Stream
* stream
) override
;
111 void GetExtraResponseInfo(
112 bool* was_fetched_via_service_worker
,
113 bool* was_fallback_required_by_service_worker
,
114 GURL
* original_url_via_service_worker
,
115 blink::WebServiceWorkerResponseType
* response_type_via_service_worker
,
116 base::TimeTicks
* fetch_start_time
,
117 base::TimeTicks
* fetch_ready_time
,
118 base::TimeTicks
* fetch_end_time
) const;
121 ~ServiceWorkerURLRequestJob() override
;
127 FORWARD_TO_SERVICE_WORKER
,
130 // We start processing the request if Start() is called AND response_type_
132 void MaybeStartRequest();
135 // Creates ServiceWorkerFetchRequest from |request_| and |body_|.
136 scoped_ptr
<ServiceWorkerFetchRequest
> CreateFetchRequest();
138 // Creates BlobDataHandle of the request body from |body_|. This handle
139 // |request_body_blob_data_handle_| will be deleted when
140 // ServiceWorkerURLRequestJob is deleted.
141 bool CreateRequestBodyBlob(std::string
* blob_uuid
, uint64
* blob_size
);
143 // For FORWARD_TO_SERVICE_WORKER case.
144 void DidPrepareFetchEvent();
145 void DidDispatchFetchEvent(ServiceWorkerStatusCode status
,
146 ServiceWorkerFetchEventResult fetch_result
,
147 const ServiceWorkerResponse
& response
,
148 scoped_refptr
<ServiceWorkerVersion
> version
);
150 // Populates |http_response_headers_|.
151 void CreateResponseHeader(int status_code
,
152 const std::string
& status_text
,
153 const ServiceWorkerHeaderMap
& headers
);
155 // Creates |http_response_info_| using |http_response_headers_| and calls
156 // NotifyHeadersComplete.
157 void CommitResponseHeader();
159 // Creates and commits a response header indicating error.
160 void DeliverErrorResponse();
162 // Releases the resources for streaming.
165 const net::HttpResponseInfo
* http_info() const;
167 base::WeakPtr
<ServiceWorkerProviderHost
> provider_host_
;
169 // Timing info to show on the popup in Devtools' Network tab.
170 net::LoadTimingInfo load_timing_info_
;
171 base::TimeTicks fetch_start_time_
;
172 base::TimeTicks fetch_ready_time_
;
173 base::TimeTicks fetch_end_time_
;
174 base::Time response_time_
;
176 ResponseType response_type_
;
179 net::HttpByteRange byte_range_
;
180 scoped_ptr
<net::HttpResponseInfo
> range_response_info_
;
181 scoped_ptr
<net::HttpResponseInfo
> http_response_info_
;
182 // Headers that have not yet been committed to |http_response_info_|.
183 scoped_refptr
<net::HttpResponseHeaders
> http_response_headers_
;
185 blink::WebServiceWorkerResponseType service_worker_response_type_
;
187 // Used when response type is FORWARD_TO_SERVICE_WORKER.
188 scoped_ptr
<ServiceWorkerFetchDispatcher
> fetch_dispatcher_
;
189 base::WeakPtr
<storage::BlobStorageContext
> blob_storage_context_
;
190 const ResourceContext
* resource_context_
;
191 scoped_ptr
<net::URLRequest
> blob_request_
;
192 scoped_refptr
<Stream
> stream_
;
193 GURL waiting_stream_url_
;
194 scoped_refptr
<net::IOBuffer
> stream_pending_buffer_
;
195 int stream_pending_buffer_size_
;
197 FetchRequestMode request_mode_
;
198 FetchCredentialsMode credentials_mode_
;
199 RequestContextType request_context_type_
;
200 RequestContextFrameType frame_type_
;
201 bool fall_back_required_
;
202 // ResourceRequestBody has a collection of BlobDataHandles attached to it
203 // using the userdata mechanism. So we have to keep it not to free the blobs.
204 scoped_refptr
<ResourceRequestBody
> body_
;
205 scoped_ptr
<storage::BlobDataHandle
> request_body_blob_data_handle_
;
206 scoped_refptr
<ServiceWorkerVersion
> streaming_version_
;
208 base::WeakPtrFactory
<ServiceWorkerURLRequestJob
> weak_factory_
;
210 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerURLRequestJob
);
213 } // namespace content
215 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_URL_REQUEST_JOB_H_