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_FETCHER_H_
      6 #define NET_URL_REQUEST_URL_FETCHER_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/callback_forward.h"
     12 #include "base/memory/ref_counted.h"
     13 #include "base/memory/scoped_ptr.h"
     14 #include "base/supports_user_data.h"
     15 #include "net/base/net_export.h"
     16 
     17 class GURL;
     18 
     19 namespace base {
     20 class FilePath;
     21 class MessageLoopProxy;
     22 class SequencedTaskRunner;
     23 class TaskRunner;
     24 class TimeDelta;
     25 }
     26 
     27 namespace net {
     28 class HostPortPair;
     29 class HttpRequestHeaders;
     30 class HttpResponseHeaders;
     31 class URLFetcherDelegate;
     32 class URLFetcherResponseWriter;
     33 class URLRequestContextGetter;
     34 class URLRequestStatus;
     35 typedef std::vector<std::string> ResponseCookies;
     36 
     37 // To use this class, create an instance with the desired URL and a pointer to
     38 // the object to be notified when the URL has been loaded:
     39 //   URLFetcher* fetcher = URLFetcher::Create("http://www.google.com",
     40 //                                            URLFetcher::GET, this);
     41 //
     42 // You must also set a request context getter:
     43 //
     44 //   fetcher->SetRequestContext(&my_request_context_getter);
     45 //
     46 // Then, optionally set properties on this object, like the request context or
     47 // extra headers:
     48 //   fetcher->set_extra_request_headers("X-Foo: bar");
     49 //
     50 // Finally, start the request:
     51 //   fetcher->Start();
     52 //
     53 //
     54 // The object you supply as a delegate must inherit from
     55 // URLFetcherDelegate; when the fetch is completed,
     56 // OnURLFetchComplete() will be called with a pointer to the URLFetcher.  From
     57 // that point until the original URLFetcher instance is destroyed, you may use
     58 // accessor methods to see the result of the fetch. You should copy these
     59 // objects if you need them to live longer than the URLFetcher instance. If the
     60 // URLFetcher instance is destroyed before the callback happens, the fetch will
     61 // be canceled and no callback will occur.
     62 //
     63 // You may create the URLFetcher instance on any thread; OnURLFetchComplete()
     64 // will be called back on the same thread you use to create the instance.
     65 //
     66 //
     67 // NOTE: By default URLFetcher requests are NOT intercepted, except when
     68 // interception is explicitly enabled in tests.
     69 class NET_EXPORT URLFetcher {
     70  public:
     71   // Imposible http response code. Used to signal that no http response code
     72   // was received.
     73   enum ResponseCode {
     74     RESPONSE_CODE_INVALID = -1
     75   };
     76 
     77   enum RequestType {
     78     GET,
     79     POST,
     80     HEAD,
     81     DELETE_REQUEST,   // DELETE is already taken on Windows.
     82                       // <winnt.h> defines a DELETE macro.
     83     PUT,
     84     PATCH,
     85   };
     86 
     87   // Used by SetURLRequestUserData.  The callback should make a fresh
     88   // base::SupportsUserData::Data object every time it's called.
     89   typedef base::Callback<base::SupportsUserData::Data*()> CreateDataCallback;
     90 
     91   virtual ~URLFetcher();
     92 
     93   // |url| is the URL to send the request to.
     94   // |request_type| is the type of request to make.
     95   // |d| the object that will receive the callback on fetch completion.
     96   static URLFetcher* Create(const GURL& url,
     97                             URLFetcher::RequestType request_type,
     98                             URLFetcherDelegate* d);
     99 
    100   // Like above, but if there's a URLFetcherFactory registered with the
    101   // implementation it will be used. |id| may be used during testing to identify
    102   // who is creating the URLFetcher.
    103   static URLFetcher* Create(int id,
    104                             const GURL& url,
    105                             URLFetcher::RequestType request_type,
    106                             URLFetcherDelegate* d);
    107 
    108   // Cancels all existing URLFetchers.  Will notify the URLFetcherDelegates.
    109   // Note that any new URLFetchers created while this is running will not be
    110   // cancelled.  Typically, one would call this in the CleanUp() method of an IO
    111   // thread, so that no new URLRequests would be able to start on the IO thread
    112   // anyway.  This doesn't prevent new URLFetchers from trying to post to the IO
    113   // thread though, even though the task won't ever run.
    114   static void CancelAll();
    115 
    116   // Normally interception is disabled for URLFetcher, but you can use this
    117   // to enable it for tests. Also see ScopedURLFetcherFactory for another way
    118   // of testing code that uses an URLFetcher.
    119   static void SetEnableInterceptionForTests(bool enabled);
    120 
    121   // Normally, URLFetcher will abort loads that request SSL client certificate
    122   // authentication, but this method may be used to cause URLFetchers to ignore
    123   // requests for client certificates and continue anonymously. Because such
    124   // behaviour affects the URLRequestContext's shared network state and socket
    125   // pools, it should only be used for testing.
    126   static void SetIgnoreCertificateRequests(bool ignored);
    127 
    128   // Sets data only needed by POSTs.  All callers making POST requests should
    129   // call one of the SetUpload* methods before the request is started.
    130   // |upload_content_type| is the MIME type of the content, while
    131   // |upload_content| is the data to be sent (the Content-Length header value
    132   // will be set to the length of this data).
    133   virtual void SetUploadData(const std::string& upload_content_type,
    134                              const std::string& upload_content) = 0;
    135 
    136   // Sets data only needed by POSTs.  All callers making POST requests should
    137   // call one of the SetUpload* methods before the request is started.
    138   // |upload_content_type| is the MIME type of the content, while
    139   // |file_path| is the path to the file containing the data to be sent (the
    140   // Content-Length header value will be set to the length of this file).
    141   // |range_offset| and |range_length| specify the range of the part
    142   // to be uploaded. To upload the whole file, (0, kuint64max) can be used.
    143   // |file_task_runner| will be used for all file operations.
    144   virtual void SetUploadFilePath(
    145       const std::string& upload_content_type,
    146       const base::FilePath& file_path,
    147       uint64 range_offset,
    148       uint64 range_length,
    149       scoped_refptr<base::TaskRunner> file_task_runner) = 0;
    150 
    151   // Indicates that the POST data is sent via chunked transfer encoding.
    152   // This may only be called before calling Start().
    153   // Use AppendChunkToUpload() to give the data chunks after calling Start().
    154   virtual void SetChunkedUpload(const std::string& upload_content_type) = 0;
    155 
    156   // Adds the given bytes to a request's POST data transmitted using chunked
    157   // transfer encoding.
    158   // This method should be called ONLY after calling Start().
    159   virtual void AppendChunkToUpload(const std::string& data,
    160                                    bool is_last_chunk) = 0;
    161 
    162   // Set one or more load flags as defined in net/base/load_flags.h.  Must be
    163   // called before the request is started.
    164   virtual void SetLoadFlags(int load_flags) = 0;
    165 
    166   // Returns the current load flags.
    167   virtual int GetLoadFlags() const = 0;
    168 
    169   // The referrer URL for the request. Must be called before the request is
    170   // started.
    171   virtual void SetReferrer(const std::string& referrer) = 0;
    172 
    173   // Set extra headers on the request.  Must be called before the request
    174   // is started.
    175   // This replaces the entire extra request headers.
    176   virtual void SetExtraRequestHeaders(
    177       const std::string& extra_request_headers) = 0;
    178 
    179   // Add header (with format field-name ":" [ field-value ]) to the request
    180   // headers.  Must be called before the request is started.
    181   // This appends the header to the current extra request headers.
    182   virtual void AddExtraRequestHeader(const std::string& header_line) = 0;
    183 
    184   virtual void GetExtraRequestHeaders(
    185       HttpRequestHeaders* headers) const = 0;
    186 
    187   // Set the URLRequestContext on the request.  Must be called before the
    188   // request is started.
    189   virtual void SetRequestContext(
    190       URLRequestContextGetter* request_context_getter) = 0;
    191 
    192   // Set the URL that should be consulted for the third-party cookie
    193   // blocking policy.
    194   virtual void SetFirstPartyForCookies(
    195       const GURL& first_party_for_cookies) = 0;
    196 
    197   // Set the key and data callback that is used when setting the user
    198   // data on any URLRequest objects this object creates.
    199   virtual void SetURLRequestUserData(
    200       const void* key,
    201       const CreateDataCallback& create_data_callback) = 0;
    202 
    203   // If |stop_on_redirect| is true, 3xx responses will cause the fetch to halt
    204   // immediately rather than continue through the redirect.  OnURLFetchComplete
    205   // will be called, with the URLFetcher's URL set to the redirect destination,
    206   // its status set to CANCELED, and its response code set to the relevant 3xx
    207   // server response code.
    208   virtual void SetStopOnRedirect(bool stop_on_redirect) = 0;
    209 
    210   // If |retry| is false, 5xx responses will be propagated to the observer,
    211   // if it is true URLFetcher will automatically re-execute the request,
    212   // after backoff_delay() elapses. URLFetcher has it set to true by default.
    213   virtual void SetAutomaticallyRetryOn5xx(bool retry) = 0;
    214 
    215   virtual void SetMaxRetriesOn5xx(int max_retries) = 0;
    216   virtual int GetMaxRetriesOn5xx() const = 0;
    217 
    218   // Returns the back-off delay before the request will be retried,
    219   // when a 5xx response was received.
    220   virtual base::TimeDelta GetBackoffDelay() const = 0;
    221 
    222   // Retries up to |max_retries| times when requests fail with
    223   // ERR_NETWORK_CHANGED. If ERR_NETWORK_CHANGED is received after having
    224   // retried |max_retries| times then it is propagated to the observer.
    225   virtual void SetAutomaticallyRetryOnNetworkChanges(int max_retries) = 0;
    226 
    227   // By default, the response is saved in a string. Call this method to save the
    228   // response to a file instead. Must be called before Start().
    229   // |file_task_runner| will be used for all file operations.
    230   // To save to a temporary file, use SaveResponseToTemporaryFile().
    231   // The created file is removed when the URLFetcher is deleted unless you
    232   // take ownership by calling GetResponseAsFilePath().
    233   virtual void SaveResponseToFileAtPath(
    234       const base::FilePath& file_path,
    235       scoped_refptr<base::SequencedTaskRunner> file_task_runner) = 0;
    236 
    237   // By default, the response is saved in a string. Call this method to save the
    238   // response to a temporary file instead. Must be called before Start().
    239   // |file_task_runner| will be used for all file operations.
    240   // The created file is removed when the URLFetcher is deleted unless you
    241   // take ownership by calling GetResponseAsFilePath().
    242   virtual void SaveResponseToTemporaryFile(
    243       scoped_refptr<base::SequencedTaskRunner> file_task_runner) = 0;
    244 
    245   // By default, the response is saved in a string. Call this method to use the
    246   // specified writer to save the response. Must be called before Start().
    247   virtual void SaveResponseWithWriter(
    248       scoped_ptr<URLFetcherResponseWriter> response_writer) = 0;
    249 
    250   // Retrieve the response headers from the request.  Must only be called after
    251   // the OnURLFetchComplete callback has run.
    252   virtual HttpResponseHeaders* GetResponseHeaders() const = 0;
    253 
    254   // Retrieve the remote socket address from the request.  Must only
    255   // be called after the OnURLFetchComplete callback has run and if
    256   // the request has not failed.
    257   virtual HostPortPair GetSocketAddress() const = 0;
    258 
    259   // Returns true if the request was delivered through a proxy.  Must only
    260   // be called after the OnURLFetchComplete callback has run and the request
    261   // has not failed.
    262   virtual bool WasFetchedViaProxy() const = 0;
    263 
    264   // Start the request.  After this is called, you may not change any other
    265   // settings.
    266   virtual void Start() = 0;
    267 
    268   // Return the URL that we were asked to fetch.
    269   virtual const GURL& GetOriginalURL() const = 0;
    270 
    271   // Return the URL that this fetcher is processing.
    272   virtual const GURL& GetURL() const = 0;
    273 
    274   // The status of the URL fetch.
    275   virtual const URLRequestStatus& GetStatus() const = 0;
    276 
    277   // The http response code received. Will return RESPONSE_CODE_INVALID
    278   // if an error prevented any response from being received.
    279   virtual int GetResponseCode() const = 0;
    280 
    281   // Cookies recieved.
    282   virtual const ResponseCookies& GetCookies() const = 0;
    283 
    284   // Reports that the received content was malformed.
    285   virtual void ReceivedContentWasMalformed() = 0;
    286 
    287   // Get the response as a string. Return false if the fetcher was not
    288   // set to store the response as a string.
    289   virtual bool GetResponseAsString(std::string* out_response_string) const = 0;
    290 
    291   // Get the path to the file containing the response body. Returns false
    292   // if the response body was not saved to a file. If take_ownership is
    293   // true, caller takes responsibility for the file, and it will not
    294   // be removed once the URLFetcher is destroyed.  User should not take
    295   // ownership more than once, or call this method after taking ownership.
    296   virtual bool GetResponseAsFilePath(
    297       bool take_ownership,
    298       base::FilePath* out_response_path) const = 0;
    299 };
    300 
    301 }  // namespace net
    302 
    303 #endif  // NET_URL_REQUEST_URL_FETCHER_H_
    304