Enables compositing support for webview.
[chromium-blink-merge.git] / net / url_request / url_request_job.h
blob3178285c976081cc4fa22b66778b95d008183109
1 // Copyright (c) 2012 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 NET_URL_REQUEST_URL_REQUEST_JOB_H_
6 #define NET_URL_REQUEST_URL_REQUEST_JOB_H_
8 #include <string>
9 #include <vector>
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/system_monitor/system_monitor.h"
15 #include "googleurl/src/gurl.h"
16 #include "net/base/filter.h"
17 #include "net/base/host_port_pair.h"
18 #include "net/base/load_states.h"
19 #include "net/base/net_export.h"
20 #include "net/base/upload_progress.h"
21 #include "net/cookies/canonical_cookie.h"
23 namespace net {
25 class AuthChallengeInfo;
26 class AuthCredentials;
27 class CookieOptions;
28 class HttpRequestHeaders;
29 class HttpResponseInfo;
30 class IOBuffer;
31 class NetworkDelegate;
32 class SSLCertRequestInfo;
33 class SSLInfo;
34 class URLRequest;
35 class UploadDataStream;
36 class URLRequestStatus;
37 class X509Certificate;
39 class NET_EXPORT URLRequestJob : public base::RefCounted<URLRequestJob>,
40 public base::SystemMonitor::PowerObserver {
41 public:
42 explicit URLRequestJob(URLRequest* request,
43 NetworkDelegate* network_delegate);
45 // Returns the request that owns this job. THIS POINTER MAY BE NULL if the
46 // request was destroyed.
47 URLRequest* request() const {
48 return request_;
51 // Sets the upload data, most requests have no upload data, so this is a NOP.
52 // Job types supporting upload data will override this.
53 virtual void SetUpload(UploadDataStream* upload_data_stream);
55 // Sets extra request headers for Job types that support request headers.
56 virtual void SetExtraRequestHeaders(const HttpRequestHeaders& headers);
58 // If any error occurs while starting the Job, NotifyStartError should be
59 // called.
60 // This helps ensure that all errors follow more similar notification code
61 // paths, which should simplify testing.
62 virtual void Start() = 0;
64 // This function MUST somehow call NotifyDone/NotifyCanceled or some requests
65 // will get leaked. Certain callers use that message to know when they can
66 // delete their URLRequest object, even when doing a cancel. The default
67 // Kill implementation calls NotifyCanceled, so it is recommended that
68 // subclasses call URLRequestJob::Kill() after doing any additional work.
70 // The job should endeavor to stop working as soon as is convenient, but must
71 // not send and complete notifications from inside this function. Instead,
72 // complete notifications (including "canceled") should be sent from a
73 // callback run from the message loop.
75 // The job is not obliged to immediately stop sending data in response to
76 // this call, nor is it obliged to fail with "canceled" unless not all data
77 // was sent as a result. A typical case would be where the job is almost
78 // complete and can succeed before the canceled notification can be
79 // dispatched (from the message loop).
81 // The job should be prepared to receive multiple calls to kill it, but only
82 // one notification must be issued.
83 virtual void Kill();
85 // Called to detach the request from this Job. Results in the Job being
86 // killed off eventually. The job must not use the request pointer any more.
87 void DetachRequest();
89 // Called to read post-filtered data from this Job, returning the number of
90 // bytes read, 0 when there is no more data, or -1 if there was an error.
91 // This is just the backend for URLRequest::Read, see that function for
92 // more info.
93 bool Read(IOBuffer* buf, int buf_size, int* bytes_read);
95 // Stops further caching of this request, if any. For more info, see
96 // URLRequest::StopCaching().
97 virtual void StopCaching();
99 // Called to fetch the current load state for the job.
100 virtual LoadState GetLoadState() const;
102 // Called to get the upload progress in bytes.
103 virtual UploadProgress GetUploadProgress() const;
105 // Called to fetch the charset for this request. Only makes sense for some
106 // types of requests. Returns true on success. Calling this on a type that
107 // doesn't have a charset will return false.
108 virtual bool GetCharset(std::string* charset);
110 // Called to get response info.
111 virtual void GetResponseInfo(HttpResponseInfo* info);
113 // Returns the cookie values included in the response, if applicable.
114 // Returns true if applicable.
115 // NOTE: This removes the cookies from the job, so it will only return
116 // useful results once per job.
117 virtual bool GetResponseCookies(std::vector<std::string>* cookies);
119 // Called to setup a stream filter for this request. An example of filter is
120 // content encoding/decoding.
121 // Subclasses should return the appropriate Filter, or NULL for no Filter.
122 // This class takes ownership of the returned Filter.
124 // The default implementation returns NULL.
125 virtual Filter* SetupFilter() const;
127 // Called to determine if this response is a redirect. Only makes sense
128 // for some types of requests. This method returns true if the response
129 // is a redirect, and fills in the location param with the URL of the
130 // redirect. The HTTP status code (e.g., 302) is filled into
131 // |*http_status_code| to signify the type of redirect.
133 // The caller is responsible for following the redirect by setting up an
134 // appropriate replacement Job. Note that the redirected location may be
135 // invalid, the caller should be sure it can handle this.
137 // The default implementation inspects the response_info_.
138 virtual bool IsRedirectResponse(GURL* location, int* http_status_code);
140 // Called to determine if it is okay to redirect this job to the specified
141 // location. This may be used to implement protocol-specific restrictions.
142 // If this function returns false, then the URLRequest will fail
143 // reporting ERR_UNSAFE_REDIRECT.
144 virtual bool IsSafeRedirect(const GURL& location);
146 // Called to determine if this response is asking for authentication. Only
147 // makes sense for some types of requests. The caller is responsible for
148 // obtaining the credentials passing them to SetAuth.
149 virtual bool NeedsAuth();
151 // Fills the authentication info with the server's response.
152 virtual void GetAuthChallengeInfo(
153 scoped_refptr<AuthChallengeInfo>* auth_info);
155 // Resend the request with authentication credentials.
156 virtual void SetAuth(const AuthCredentials& credentials);
158 // Display the error page without asking for credentials again.
159 virtual void CancelAuth();
161 virtual void ContinueWithCertificate(X509Certificate* client_cert);
163 // Continue processing the request ignoring the last error.
164 virtual void ContinueDespiteLastError();
166 void FollowDeferredRedirect();
168 // Returns true if the Job is done producing response data and has called
169 // NotifyDone on the request.
170 bool is_done() const { return done_; }
172 // Get/Set expected content size
173 int64 expected_content_size() const { return expected_content_size_; }
174 void set_expected_content_size(const int64& size) {
175 expected_content_size_ = size;
178 // Whether we have processed the response for that request yet.
179 bool has_response_started() const { return has_handled_response_; }
181 // These methods are not applicable to all connections.
182 virtual bool GetMimeType(std::string* mime_type) const;
183 virtual int GetResponseCode() const;
185 // Returns the socket address for the connection.
186 // See url_request.h for details.
187 virtual HostPortPair GetSocketAddress() const;
189 // base::SystemMonitor::PowerObserver methods:
190 // We invoke URLRequestJob::Kill on suspend (crbug.com/4606).
191 virtual void OnSuspend() OVERRIDE;
193 // Called after a NetworkDelegate has been informed that the URLRequest
194 // will be destroyed. This is used to track that no pending callbacks
195 // exist at destruction time of the URLRequestJob, unless they have been
196 // canceled by an explicit NetworkDelegate::NotifyURLRequestDestroyed() call.
197 virtual void NotifyURLRequestDestroyed();
199 protected:
200 friend class base::RefCounted<URLRequestJob>;
201 virtual ~URLRequestJob();
203 // Notifies the job that a certificate is requested.
204 void NotifyCertificateRequested(SSLCertRequestInfo* cert_request_info);
206 // Notifies the job about an SSL certificate error.
207 void NotifySSLCertificateError(const SSLInfo& ssl_info, bool fatal);
209 // Delegates to URLRequest::Delegate.
210 bool CanGetCookies(const CookieList& cookie_list) const;
212 // Delegates to URLRequest::Delegate.
213 bool CanSetCookie(const std::string& cookie_line,
214 CookieOptions* options) const;
216 // Notifies the job that headers have been received.
217 void NotifyHeadersComplete();
219 // Notifies the request that the job has completed a Read operation.
220 void NotifyReadComplete(int bytes_read);
222 // Notifies the request that a start error has occurred.
223 void NotifyStartError(const URLRequestStatus& status);
225 // NotifyDone marks when we are done with a request. It is really
226 // a glorified set_status, but also does internal state checking and
227 // job tracking. It should be called once per request, when the job is
228 // finished doing all IO.
229 void NotifyDone(const URLRequestStatus& status);
231 // Some work performed by NotifyDone must be completed on a separate task
232 // so as to avoid re-entering the delegate. This method exists to perform
233 // that work.
234 void CompleteNotifyDone();
236 // Used as an asynchronous callback for Kill to notify the URLRequest
237 // that we were canceled.
238 void NotifyCanceled();
240 // Notifies the job the request should be restarted.
241 // Should only be called if the job has not started a resposne.
242 void NotifyRestartRequired();
244 // Called when the network delegate blocks or unblocks this request when
245 // intercepting certain requests.
246 void SetBlockedOnDelegate();
247 void SetUnblockedOnDelegate();
249 // Called to read raw (pre-filtered) data from this Job.
250 // If returning true, data was read from the job. buf will contain
251 // the data, and bytes_read will receive the number of bytes read.
252 // If returning true, and bytes_read is returned as 0, there is no
253 // additional data to be read.
254 // If returning false, an error occurred or an async IO is now pending.
255 // If async IO is pending, the status of the request will be
256 // URLRequestStatus::IO_PENDING, and buf must remain available until the
257 // operation is completed. See comments on URLRequest::Read for more
258 // info.
259 virtual bool ReadRawData(IOBuffer* buf, int buf_size, int *bytes_read);
261 // Called to tell the job that a filter has successfully reached the end of
262 // the stream.
263 virtual void DoneReading();
265 // Informs the filter that data has been read into its buffer
266 void FilteredDataRead(int bytes_read);
268 // Reads filtered data from the request. Returns true if successful,
269 // false otherwise. Note, if there is not enough data received to
270 // return data, this call can issue a new async IO request under
271 // the hood.
272 bool ReadFilteredData(int *bytes_read);
274 // Whether the response is being filtered in this job.
275 // Only valid after NotifyHeadersComplete() has been called.
276 bool HasFilter() { return filter_ != NULL; }
278 // At or near destruction time, a derived class may request that the filters
279 // be destroyed so that statistics can be gathered while the derived class is
280 // still present to assist in calculations. This is used by URLRequestHttpJob
281 // to get SDCH to emit stats.
282 void DestroyFilters() { filter_.reset(); }
284 // The status of the job.
285 const URLRequestStatus GetStatus();
287 // Set the status of the job.
288 void SetStatus(const URLRequestStatus& status);
290 // The number of bytes read before passing to the filter.
291 int prefilter_bytes_read() const { return prefilter_bytes_read_; }
293 // The number of bytes read after passing through the filter.
294 int postfilter_bytes_read() const { return postfilter_bytes_read_; }
296 // Total number of bytes read from network (or cache) and typically handed
297 // to filter to process. Used to histogram compression ratios, and error
298 // recovery scenarios in filters.
299 int64 filter_input_byte_count() const { return filter_input_byte_count_; }
301 // The request that initiated this job. This value MAY BE NULL if the
302 // request was released by DetachRequest().
303 URLRequest* request_;
305 private:
306 // When data filtering is enabled, this function is used to read data
307 // for the filter. Returns true if raw data was read. Returns false if
308 // an error occurred (or we are waiting for IO to complete).
309 bool ReadRawDataForFilter(int *bytes_read);
311 // Invokes ReadRawData and records bytes read if the read completes
312 // synchronously.
313 bool ReadRawDataHelper(IOBuffer* buf, int buf_size, int* bytes_read);
315 // Called in response to a redirect that was not canceled to follow the
316 // redirect. The current job will be replaced with a new job loading the
317 // given redirect destination.
318 void FollowRedirect(const GURL& location, int http_status_code);
320 // Called after every raw read. If |bytes_read| is > 0, this indicates
321 // a successful read of |bytes_read| unfiltered bytes. If |bytes_read|
322 // is 0, this indicates that there is no additional data to read. If
323 // |bytes_read| is < 0, an error occurred and no bytes were read.
324 void OnRawReadComplete(int bytes_read);
326 // Updates the profiling info and notifies observers that an additional
327 // |bytes_read| unfiltered bytes have been read for this job.
328 void RecordBytesRead(int bytes_read);
330 // Called to query whether there is data available in the filter to be read
331 // out.
332 bool FilterHasData();
334 // Subclasses may implement this method to record packet arrival times.
335 // The default implementation does nothing.
336 virtual void UpdatePacketReadTimes();
338 // Custom handler for derived classes when the request is detached.
339 virtual void OnDetachRequest() {}
341 // Indicates that the job is done producing data, either it has completed
342 // all the data or an error has been encountered. Set exclusively by
343 // NotifyDone so that it is kept in sync with the request.
344 bool done_;
346 int prefilter_bytes_read_;
347 int postfilter_bytes_read_;
348 int64 filter_input_byte_count_;
350 // The data stream filter which is enabled on demand.
351 scoped_ptr<Filter> filter_;
353 // If the filter filled its output buffer, then there is a change that it
354 // still has internal data to emit, and this flag is set.
355 bool filter_needs_more_output_space_;
357 // When we filter data, we receive data into the filter buffers. After
358 // processing the filtered data, we return the data in the caller's buffer.
359 // While the async IO is in progress, we save the user buffer here, and
360 // when the IO completes, we fill this in.
361 scoped_refptr<IOBuffer> filtered_read_buffer_;
362 int filtered_read_buffer_len_;
364 // We keep a pointer to the read buffer while asynchronous reads are
365 // in progress, so we are able to pass those bytes to job observers.
366 scoped_refptr<IOBuffer> raw_read_buffer_;
368 // Used by HandleResponseIfNecessary to track whether we've sent the
369 // OnResponseStarted callback and potentially redirect callbacks as well.
370 bool has_handled_response_;
372 // Expected content size
373 int64 expected_content_size_;
375 // Set when a redirect is deferred.
376 GURL deferred_redirect_url_;
377 int deferred_redirect_status_code_;
379 NetworkDelegate* network_delegate_;
381 base::WeakPtrFactory<URLRequestJob> weak_factory_;
383 DISALLOW_COPY_AND_ASSIGN(URLRequestJob);
386 } // namespace net
388 #endif // NET_URL_REQUEST_URL_REQUEST_JOB_H_