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