Add long running gmail memory benchmark for background tab.
[chromium-blink-merge.git] / content / browser / service_worker / service_worker_url_request_job.h
blob2c080de1051202466805fdda9bef40bb8f613877
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/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 bool is_main_resource_load,
64 RequestContextType request_context_type,
65 RequestContextFrameType frame_type,
66 scoped_refptr<ResourceRequestBody> body);
68 // Sets the response type.
69 void FallbackToNetwork();
70 void ForwardToServiceWorker();
72 bool ShouldFallbackToNetwork() const {
73 return response_type_ == FALLBACK_TO_NETWORK;
75 bool ShouldForwardToServiceWorker() const {
76 return response_type_ == FORWARD_TO_SERVICE_WORKER;
79 // net::URLRequestJob overrides:
80 void Start() override;
81 void Kill() override;
82 net::LoadState GetLoadState() const override;
83 bool GetCharset(std::string* charset) override;
84 bool GetMimeType(std::string* mime_type) const override;
85 void GetResponseInfo(net::HttpResponseInfo* info) override;
86 void GetLoadTimingInfo(net::LoadTimingInfo* load_timing_info) const override;
87 int GetResponseCode() const override;
88 void SetExtraRequestHeaders(const net::HttpRequestHeaders& headers) override;
89 bool ReadRawData(net::IOBuffer* buf, int buf_size, int* bytes_read) override;
91 // net::URLRequest::Delegate overrides that read the blob from the
92 // ServiceWorkerFetchResponse.
93 void OnReceivedRedirect(net::URLRequest* request,
94 const net::RedirectInfo& redirect_info,
95 bool* defer_redirect) override;
96 void OnAuthRequired(net::URLRequest* request,
97 net::AuthChallengeInfo* auth_info) override;
98 void OnCertificateRequested(
99 net::URLRequest* request,
100 net::SSLCertRequestInfo* cert_request_info) override;
101 void OnSSLCertificateError(net::URLRequest* request,
102 const net::SSLInfo& ssl_info,
103 bool fatal) override;
104 void OnBeforeNetworkStart(net::URLRequest* request, bool* defer) override;
105 void OnResponseStarted(net::URLRequest* request) override;
106 void OnReadCompleted(net::URLRequest* request, int bytes_read) override;
108 // StreamObserver override:
109 void OnDataAvailable(Stream* stream) override;
111 // StreamRegisterObserver override:
112 void OnStreamRegistered(Stream* stream) override;
114 void GetExtraResponseInfo(ResourceResponseInfo* response_info) const;
116 const base::TimeTicks& worker_start_time() const {
117 return worker_start_time_;
119 const base::TimeTicks& worker_ready_time() const {
120 return worker_ready_time_;
123 protected:
124 ~ServiceWorkerURLRequestJob() override;
126 private:
127 enum ResponseType {
128 NOT_DETERMINED,
129 FALLBACK_TO_NETWORK,
130 FORWARD_TO_SERVICE_WORKER,
133 enum ResponseBodyType {
134 UNKNOWN,
135 BLOB,
136 STREAM,
139 // We start processing the request if Start() is called AND response_type_
140 // is determined.
141 void MaybeStartRequest();
142 void StartRequest();
144 // Creates ServiceWorkerFetchRequest from |request_| and |body_|.
145 scoped_ptr<ServiceWorkerFetchRequest> CreateFetchRequest();
147 // Creates BlobDataHandle of the request body from |body_|. This handle
148 // |request_body_blob_data_handle_| will be deleted when
149 // ServiceWorkerURLRequestJob is deleted.
150 bool CreateRequestBodyBlob(std::string* blob_uuid, uint64* blob_size);
152 // For FORWARD_TO_SERVICE_WORKER case.
153 void DidPrepareFetchEvent();
154 void DidDispatchFetchEvent(ServiceWorkerStatusCode status,
155 ServiceWorkerFetchEventResult fetch_result,
156 const ServiceWorkerResponse& response,
157 scoped_refptr<ServiceWorkerVersion> version);
159 // Populates |http_response_headers_|.
160 void CreateResponseHeader(int status_code,
161 const std::string& status_text,
162 const ServiceWorkerHeaderMap& headers);
164 // Creates |http_response_info_| using |http_response_headers_| and calls
165 // NotifyHeadersComplete.
166 void CommitResponseHeader();
168 // Creates and commits a response header indicating error.
169 void DeliverErrorResponse();
171 // For UMA.
172 void SetResponseBodyType(ResponseBodyType type);
173 bool ShouldRecordResult();
174 void RecordResult(ServiceWorkerMetrics::URLRequestJobResult result);
175 void RecordStatusZeroResponseError(
176 blink::WebServiceWorkerResponseError error);
178 // Releases the resources for streaming.
179 void ClearStream();
181 const net::HttpResponseInfo* http_info() const;
183 base::WeakPtr<ServiceWorkerProviderHost> provider_host_;
185 // Timing info to show on the popup in Devtools' Network tab.
186 net::LoadTimingInfo load_timing_info_;
187 base::TimeTicks worker_start_time_;
188 base::TimeTicks worker_ready_time_;
189 base::Time response_time_;
191 ResponseType response_type_;
192 bool is_started_;
194 net::HttpByteRange byte_range_;
195 scoped_ptr<net::HttpResponseInfo> range_response_info_;
196 scoped_ptr<net::HttpResponseInfo> http_response_info_;
197 // Headers that have not yet been committed to |http_response_info_|.
198 scoped_refptr<net::HttpResponseHeaders> http_response_headers_;
199 GURL response_url_;
200 blink::WebServiceWorkerResponseType service_worker_response_type_;
202 // Used when response type is FORWARD_TO_SERVICE_WORKER.
203 scoped_ptr<ServiceWorkerFetchDispatcher> fetch_dispatcher_;
204 base::WeakPtr<storage::BlobStorageContext> blob_storage_context_;
205 const ResourceContext* resource_context_;
206 scoped_ptr<net::URLRequest> blob_request_;
207 scoped_refptr<Stream> stream_;
208 GURL waiting_stream_url_;
209 scoped_refptr<net::IOBuffer> stream_pending_buffer_;
210 int stream_pending_buffer_size_;
212 FetchRequestMode request_mode_;
213 FetchCredentialsMode credentials_mode_;
214 const bool is_main_resource_load_;
215 RequestContextType request_context_type_;
216 RequestContextFrameType frame_type_;
217 bool fall_back_required_;
218 // ResourceRequestBody has a collection of BlobDataHandles attached to it
219 // using the userdata mechanism. So we have to keep it not to free the blobs.
220 scoped_refptr<ResourceRequestBody> body_;
221 scoped_ptr<storage::BlobDataHandle> request_body_blob_data_handle_;
222 scoped_refptr<ServiceWorkerVersion> streaming_version_;
224 ResponseBodyType response_body_type_ = UNKNOWN;
225 bool did_record_result_ = false;
227 base::WeakPtrFactory<ServiceWorkerURLRequestJob> weak_factory_;
229 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerURLRequestJob);
232 } // namespace content
234 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_URL_REQUEST_JOB_H_