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 class is useful for building a simple URLRequestContext. Most creators
      6 // of new URLRequestContexts should use this helper class to construct it. Call
      7 // any configuration params, and when done, invoke Build() to construct the
      8 // URLRequestContext. This URLRequestContext will own all its own storage.
      9 //
     10 // URLRequestContextBuilder and its associated params classes are initially
     11 // populated with "sane" default values. Read through the comments to figure out
     12 // what these are.
     13 
     14 #ifndef NET_URL_REQUEST_URL_REQUEST_CONTEXT_BUILDER_H_
     15 #define NET_URL_REQUEST_URL_REQUEST_CONTEXT_BUILDER_H_
     16 
     17 #include <string>
     18 
     19 #include "base/basictypes.h"
     20 #include "base/files/file_path.h"
     21 #include "base/memory/ref_counted.h"
     22 #include "base/memory/scoped_ptr.h"
     23 #include "build/build_config.h"
     24 #include "net/base/net_export.h"
     25 
     26 namespace net {
     27 
     28 class FtpTransactionFactory;
     29 class HostMappingRules;
     30 class ProxyConfigService;
     31 class URLRequestContext;
     32 class NetworkDelegate;
     33 
     34 class NET_EXPORT URLRequestContextBuilder {
     35  public:
     36   struct NET_EXPORT HttpCacheParams {
     37     enum Type {
     38       IN_MEMORY,
     39       DISK,
     40     };
     41 
     42     HttpCacheParams();
     43     ~HttpCacheParams();
     44 
     45     // The type of HTTP cache. Default is IN_MEMORY.
     46     Type type;
     47 
     48     // The max size of the cache in bytes. Default is algorithmically determined
     49     // based off available disk space.
     50     int max_size;
     51 
     52     // The cache path (when type is DISK).
     53     base::FilePath path;
     54   };
     55 
     56   struct NET_EXPORT HttpNetworkSessionParams {
     57     HttpNetworkSessionParams();
     58     ~HttpNetworkSessionParams();
     59 
     60     // These fields mirror those in net::HttpNetworkSession::Params;
     61     bool ignore_certificate_errors;
     62     HostMappingRules* host_mapping_rules;
     63     bool http_pipelining_enabled;
     64     uint16 testing_fixed_http_port;
     65     uint16 testing_fixed_https_port;
     66     std::string trusted_spdy_proxy;
     67   };
     68 
     69   URLRequestContextBuilder();
     70   ~URLRequestContextBuilder();
     71 
     72 #if defined(OS_LINUX) || defined(OS_ANDROID)
     73   void set_proxy_config_service(ProxyConfigService* proxy_config_service);
     74 #endif  // defined(OS_LINUX) || defined(OS_ANDROID)
     75 
     76   // Call these functions to specify hard-coded Accept-Language
     77   // or User-Agent header values for all requests that don't
     78   // have the headers already set.
     79   void set_accept_language(const std::string& accept_language) {
     80     accept_language_ = accept_language;
     81   }
     82   void set_user_agent(const std::string& user_agent) {
     83     user_agent_ = user_agent;
     84   }
     85 
     86   // Control support for data:// requests. By default it's disabled.
     87   void set_data_enabled(bool enable) {
     88     data_enabled_ = enable;
     89   }
     90 
     91   // Control support for file:// requests. By default it's disabled.
     92   void set_file_enabled(bool enable) {
     93     file_enabled_ = enable;
     94   }
     95 
     96 #if !defined(DISABLE_FTP_SUPPORT)
     97   // Control support for ftp:// requests. By default it's disabled.
     98   void set_ftp_enabled(bool enable) {
     99     ftp_enabled_ = enable;
    100   }
    101 #endif
    102 
    103   // Uses BasicNetworkDelegate by default. Note that calling Build will unset
    104   // any custom delegate in builder, so this must be called each time before
    105   // Build is called.
    106   void set_network_delegate(NetworkDelegate* delegate) {
    107     network_delegate_.reset(delegate);
    108   }
    109 
    110   // By default HttpCache is enabled with a default constructed HttpCacheParams.
    111   void EnableHttpCache(const HttpCacheParams& params) {
    112     http_cache_params_ = params;
    113   }
    114 
    115   void DisableHttpCache() {
    116     http_cache_params_ = HttpCacheParams();
    117   }
    118 
    119   // Override default net::HttpNetworkSession::Params settings.
    120   void set_http_network_session_params(
    121       const HttpNetworkSessionParams& http_network_session_params) {
    122     http_network_session_params_ = http_network_session_params;
    123   }
    124 
    125   URLRequestContext* Build();
    126 
    127  private:
    128   std::string accept_language_;
    129   std::string user_agent_;
    130   // Include support for data:// requests.
    131   bool data_enabled_;
    132   // Include support for file:// requests.
    133   bool file_enabled_;
    134 #if !defined(DISABLE_FTP_SUPPORT)
    135   // Include support for ftp:// requests.
    136   bool ftp_enabled_;
    137 #endif
    138   bool http_cache_enabled_;
    139   HttpCacheParams http_cache_params_;
    140   HttpNetworkSessionParams http_network_session_params_;
    141 #if defined(OS_LINUX) || defined(OS_ANDROID)
    142   scoped_ptr<ProxyConfigService> proxy_config_service_;
    143 #endif  // defined(OS_LINUX) || defined(OS_ANDROID)
    144   scoped_ptr<NetworkDelegate> network_delegate_;
    145   scoped_ptr<FtpTransactionFactory> ftp_transaction_factory_;
    146 
    147   DISALLOW_COPY_AND_ASSIGN(URLRequestContextBuilder);
    148 };
    149 
    150 }  // namespace net
    151 
    152 #endif  // NET_URL_REQUEST_URL_REQUEST_CONTEXT_BUILDER_H_
    153