Update broken references to image assets
[chromium-blink-merge.git] / content / browser / service_worker / service_worker_url_request_job.h
blob4815c0be1e9078be9ab55949a6ae0a586f2e7794
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_
8 #include <map>
9 #include <string>
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/time/time.h"
14 #include "content/browser/service_worker/service_worker_metrics.h"
15 #include "content/browser/streams/stream_read_observer.h"
16 #include "content/browser/streams/stream_register_observer.h"
17 #include "content/common/content_export.h"
18 #include "content/common/service_worker/service_worker_status_code.h"
19 #include "content/common/service_worker/service_worker_types.h"
20 #include "content/public/common/request_context_frame_type.h"
21 #include "content/public/common/request_context_type.h"
22 #include "content/public/common/resource_type.h"
23 #include "net/http/http_byte_range.h"
24 #include "net/url_request/url_request.h"
25 #include "net/url_request/url_request_job.h"
26 #include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWorkerResponseType.h"
27 #include "url/gurl.h"
29 namespace net {
30 class IOBuffer;
33 namespace storage {
34 class BlobDataHandle;
35 class BlobStorageContext;
38 namespace content {
40 class ResourceContext;
41 class ResourceRequestBody;
42 class ServiceWorkerContextCore;
43 class ServiceWorkerFetchDispatcher;
44 class ServiceWorkerProviderHost;
45 class ServiceWorkerVersion;
46 class Stream;
47 struct ResourceResponseInfo;
49 class CONTENT_EXPORT ServiceWorkerURLRequestJob
50 : public net::URLRequestJob,
51 public net::URLRequest::Delegate,
52 public StreamReadObserver,
53 public StreamRegisterObserver {
54 public:
55 ServiceWorkerURLRequestJob(
56 net::URLRequest* request,
57 net::NetworkDelegate* network_delegate,
58 base::WeakPtr<ServiceWorkerProviderHost> provider_host,
59 base::WeakPtr<storage::BlobStorageContext> blob_storage_context,
60 const ResourceContext* resource_context,
61 FetchRequestMode request_mode,
62 FetchCredentialsMode credentials_mode,
63 FetchRedirectMode redirect_mode,
64 bool is_main_resource_load,
65 RequestContextType request_context_type,
66 RequestContextFrameType frame_type,
67 scoped_refptr<ResourceRequestBody> body);
69 // Sets the response type.
70 void FallbackToNetwork();
71 void ForwardToServiceWorker();
73 bool ShouldFallbackToNetwork() const {
74 return response_type_ == FALLBACK_TO_NETWORK;
76 bool ShouldForwardToServiceWorker() const {
77 return response_type_ == FORWARD_TO_SERVICE_WORKER;
80 // net::URLRequestJob overrides:
81 void Start() override;
82 void Kill() override;
83 net::LoadState GetLoadState() const override;
84 bool GetCharset(std::string* charset) override;
85 bool GetMimeType(std::string* mime_type) const override;
86 void GetResponseInfo(net::HttpResponseInfo* info) override;
87 void GetLoadTimingInfo(net::LoadTimingInfo* load_timing_info) const override;
88 int GetResponseCode() const override;
89 void SetExtraRequestHeaders(const net::HttpRequestHeaders& headers) override;
90 bool ReadRawData(net::IOBuffer* buf, int buf_size, int* bytes_read) override;
92 // net::URLRequest::Delegate overrides that read the blob from the
93 // ServiceWorkerFetchResponse.
94 void OnReceivedRedirect(net::URLRequest* request,
95 const net::RedirectInfo& redirect_info,
96 bool* defer_redirect) override;
97 void OnAuthRequired(net::URLRequest* request,
98 net::AuthChallengeInfo* auth_info) override;
99 void OnCertificateRequested(
100 net::URLRequest* request,
101 net::SSLCertRequestInfo* cert_request_info) override;
102 void OnSSLCertificateError(net::URLRequest* request,
103 const net::SSLInfo& ssl_info,
104 bool fatal) override;
105 void OnBeforeNetworkStart(net::URLRequest* request, bool* defer) override;
106 void OnResponseStarted(net::URLRequest* request) override;
107 void OnReadCompleted(net::URLRequest* request, int bytes_read) override;
109 // StreamObserver override:
110 void OnDataAvailable(Stream* stream) override;
112 // StreamRegisterObserver override:
113 void OnStreamRegistered(Stream* stream) override;
115 void GetExtraResponseInfo(ResourceResponseInfo* response_info) const;
117 const base::TimeTicks& worker_start_time() const {
118 return worker_start_time_;
120 const base::TimeTicks& worker_ready_time() const {
121 return worker_ready_time_;
124 protected:
125 ~ServiceWorkerURLRequestJob() override;
127 private:
128 enum ResponseType {
129 NOT_DETERMINED,
130 FALLBACK_TO_NETWORK,
131 FORWARD_TO_SERVICE_WORKER,
134 enum ResponseBodyType {
135 UNKNOWN,
136 BLOB,
137 STREAM,
140 // We start processing the request if Start() is called AND response_type_
141 // is determined.
142 void MaybeStartRequest();
143 void StartRequest();
145 // Creates ServiceWorkerFetchRequest from |request_| and |body_|.
146 scoped_ptr<ServiceWorkerFetchRequest> CreateFetchRequest();
148 // Creates BlobDataHandle of the request body from |body_|. This handle
149 // |request_body_blob_data_handle_| will be deleted when
150 // ServiceWorkerURLRequestJob is deleted.
151 bool CreateRequestBodyBlob(std::string* blob_uuid, uint64* blob_size);
153 // For FORWARD_TO_SERVICE_WORKER case.
154 void DidPrepareFetchEvent();
155 void DidDispatchFetchEvent(ServiceWorkerStatusCode status,
156 ServiceWorkerFetchEventResult fetch_result,
157 const ServiceWorkerResponse& response,
158 scoped_refptr<ServiceWorkerVersion> version);
160 // Populates |http_response_headers_|.
161 void CreateResponseHeader(int status_code,
162 const std::string& status_text,
163 const ServiceWorkerHeaderMap& headers);
165 // Creates |http_response_info_| using |http_response_headers_| and calls
166 // NotifyHeadersComplete.
167 void CommitResponseHeader();
169 // Creates and commits a response header indicating error.
170 void DeliverErrorResponse();
172 // For UMA.
173 void SetResponseBodyType(ResponseBodyType type);
174 bool ShouldRecordResult();
175 void RecordResult(ServiceWorkerMetrics::URLRequestJobResult result);
176 void RecordStatusZeroResponseError(
177 blink::WebServiceWorkerResponseError error);
179 // Releases the resources for streaming.
180 void ClearStream();
182 const net::HttpResponseInfo* http_info() const;
184 base::WeakPtr<ServiceWorkerProviderHost> provider_host_;
186 // Timing info to show on the popup in Devtools' Network tab.
187 net::LoadTimingInfo load_timing_info_;
188 base::TimeTicks worker_start_time_;
189 base::TimeTicks worker_ready_time_;
190 base::Time response_time_;
192 ResponseType response_type_;
193 bool is_started_;
195 net::HttpByteRange byte_range_;
196 scoped_ptr<net::HttpResponseInfo> range_response_info_;
197 scoped_ptr<net::HttpResponseInfo> http_response_info_;
198 // Headers that have not yet been committed to |http_response_info_|.
199 scoped_refptr<net::HttpResponseHeaders> http_response_headers_;
200 GURL response_url_;
201 blink::WebServiceWorkerResponseType service_worker_response_type_;
203 // Used when response type is FORWARD_TO_SERVICE_WORKER.
204 scoped_ptr<ServiceWorkerFetchDispatcher> fetch_dispatcher_;
205 base::WeakPtr<storage::BlobStorageContext> blob_storage_context_;
206 const ResourceContext* resource_context_;
207 scoped_ptr<net::URLRequest> blob_request_;
208 scoped_refptr<Stream> stream_;
209 GURL waiting_stream_url_;
210 scoped_refptr<net::IOBuffer> stream_pending_buffer_;
211 int stream_pending_buffer_size_;
213 FetchRequestMode request_mode_;
214 FetchCredentialsMode credentials_mode_;
215 FetchRedirectMode redirect_mode_;
216 const bool is_main_resource_load_;
217 RequestContextType request_context_type_;
218 RequestContextFrameType frame_type_;
219 bool fall_back_required_;
220 // ResourceRequestBody has a collection of BlobDataHandles attached to it
221 // using the userdata mechanism. So we have to keep it not to free the blobs.
222 scoped_refptr<ResourceRequestBody> body_;
223 scoped_ptr<storage::BlobDataHandle> request_body_blob_data_handle_;
224 scoped_refptr<ServiceWorkerVersion> streaming_version_;
226 ResponseBodyType response_body_type_ = UNKNOWN;
227 bool did_record_result_ = false;
229 base::WeakPtrFactory<ServiceWorkerURLRequestJob> weak_factory_;
231 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerURLRequestJob);
234 } // namespace content
236 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_URL_REQUEST_JOB_H_