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 // This file contains URLFetcher, a wrapper around URLRequest that handles
      6 // low-level details like thread safety, ref counting, and incremental buffer
      7 // reading.  This is useful for callers who simply want to get the data from a
      8 // URL and don't care about all the nitty-gritty details.
      9 //
     10 // NOTE(willchan): Only one "IO" thread is supported for URLFetcher.  This is a
     11 // temporary situation.  We will work on allowing support for multiple "io"
     12 // threads per process.
     13 
     14 #ifndef NET_URL_REQUEST_URL_FETCHER_IMPL_H_
     15 #define NET_URL_REQUEST_URL_FETCHER_IMPL_H_
     16 
     17 #include "base/basictypes.h"
     18 #include "base/compiler_specific.h"
     19 #include "net/base/net_export.h"
     20 #include "net/url_request/url_fetcher.h"
     21 
     22 namespace net {
     23 class URLFetcherCore;
     24 class URLFetcherDelegate;
     25 class URLFetcherFactory;
     26 
     27 class NET_EXPORT_PRIVATE URLFetcherImpl : public URLFetcher {
     28  public:
     29   // |url| is the URL to send the request to.
     30   // |request_type| is the type of request to make.
     31   // |d| the object that will receive the callback on fetch completion.
     32   URLFetcherImpl(const GURL& url,
     33                  RequestType request_type,
     34                  URLFetcherDelegate* d);
     35   virtual ~URLFetcherImpl();
     36 
     37   // URLFetcher implementation:
     38   virtual void SetUploadData(const std::string& upload_content_type,
     39                              const std::string& upload_content) OVERRIDE;
     40   virtual void SetUploadFilePath(
     41       const std::string& upload_content_type,
     42       const base::FilePath& file_path,
     43       uint64 range_offset,
     44       uint64 range_length,
     45       scoped_refptr<base::TaskRunner> file_task_runner) OVERRIDE;
     46   virtual void SetChunkedUpload(
     47       const std::string& upload_content_type) OVERRIDE;
     48   virtual void AppendChunkToUpload(const std::string& data,
     49                                    bool is_last_chunk) OVERRIDE;
     50   virtual void SetLoadFlags(int load_flags) OVERRIDE;
     51   virtual int GetLoadFlags() const OVERRIDE;
     52   virtual void SetReferrer(const std::string& referrer) OVERRIDE;
     53   virtual void SetExtraRequestHeaders(
     54       const std::string& extra_request_headers) OVERRIDE;
     55   virtual void AddExtraRequestHeader(const std::string& header_line) OVERRIDE;
     56   virtual void GetExtraRequestHeaders(
     57       HttpRequestHeaders* headers) const OVERRIDE;
     58   virtual void SetRequestContext(
     59       URLRequestContextGetter* request_context_getter) OVERRIDE;
     60   virtual void SetFirstPartyForCookies(
     61       const GURL& first_party_for_cookies) OVERRIDE;
     62   virtual void SetURLRequestUserData(
     63       const void* key,
     64       const CreateDataCallback& create_data_callback) OVERRIDE;
     65   virtual void SetStopOnRedirect(bool stop_on_redirect) OVERRIDE;
     66   virtual void SetAutomaticallyRetryOn5xx(bool retry) OVERRIDE;
     67   virtual void SetMaxRetriesOn5xx(int max_retries) OVERRIDE;
     68   virtual int GetMaxRetriesOn5xx() const OVERRIDE;
     69   virtual base::TimeDelta GetBackoffDelay() const OVERRIDE;
     70   virtual void SetAutomaticallyRetryOnNetworkChanges(int max_retries) OVERRIDE;
     71   virtual void SaveResponseToFileAtPath(
     72       const base::FilePath& file_path,
     73       scoped_refptr<base::SequencedTaskRunner> file_task_runner) OVERRIDE;
     74   virtual void SaveResponseToTemporaryFile(
     75       scoped_refptr<base::SequencedTaskRunner> file_task_runner) OVERRIDE;
     76   virtual void SaveResponseWithWriter(
     77       scoped_ptr<URLFetcherResponseWriter> response_writer) OVERRIDE;
     78   virtual HttpResponseHeaders* GetResponseHeaders() const OVERRIDE;
     79   virtual HostPortPair GetSocketAddress() const OVERRIDE;
     80   virtual bool WasFetchedViaProxy() const OVERRIDE;
     81   virtual void Start() OVERRIDE;
     82   virtual const GURL& GetOriginalURL() const OVERRIDE;
     83   virtual const GURL& GetURL() const OVERRIDE;
     84   virtual const URLRequestStatus& GetStatus() const OVERRIDE;
     85   virtual int GetResponseCode() const OVERRIDE;
     86   virtual const ResponseCookies& GetCookies() const OVERRIDE;
     87   virtual void ReceivedContentWasMalformed() OVERRIDE;
     88   virtual bool GetResponseAsString(
     89       std::string* out_response_string) const OVERRIDE;
     90   virtual bool GetResponseAsFilePath(
     91       bool take_ownership,
     92       base::FilePath* out_response_path) const OVERRIDE;
     93 
     94   static void CancelAll();
     95 
     96   static void SetEnableInterceptionForTests(bool enabled);
     97   static void SetIgnoreCertificateRequests(bool ignored);
     98 
     99   // TODO(akalin): Make these private again once URLFetcher::Create()
    100   // is in net/.
    101 
    102   static URLFetcherFactory* factory();
    103 
    104   // Sets the factory used by the static method Create to create a URLFetcher.
    105   // URLFetcher does not take ownership of |factory|. A value of NULL results
    106   // in a URLFetcher being created directly.
    107   //
    108   // NOTE: for safety, this should only be used through ScopedURLFetcherFactory!
    109   static void set_factory(URLFetcherFactory* factory);
    110 
    111  protected:
    112   // Returns the delegate.
    113   URLFetcherDelegate* delegate() const;
    114 
    115  private:
    116   friend class URLFetcherTest;
    117 
    118   // Only used by URLFetcherTest, returns the number of URLFetcher::Core objects
    119   // actively running.
    120   static int GetNumFetcherCores();
    121 
    122   const scoped_refptr<URLFetcherCore> core_;
    123 
    124   DISALLOW_COPY_AND_ASSIGN(URLFetcherImpl);
    125 };
    126 
    127 }  // namespace net
    128 
    129 #endif  // NET_URL_REQUEST_URL_FETCHER_IMPL_H_
    130