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