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 #include <vector>
     19 
     20 #include "base/basictypes.h"
     21 #include "base/files/file_path.h"
     22 #include "base/memory/ref_counted.h"
     23 #include "base/memory/scoped_ptr.h"
     24 #include "build/build_config.h"
     25 #include "net/base/net_export.h"
     26 #include "net/base/network_delegate.h"
     27 #include "net/dns/host_resolver.h"
     28 #include "net/proxy/proxy_config_service.h"
     29 #include "net/proxy/proxy_service.h"
     30 #include "net/socket/next_proto.h"
     31 
     32 namespace net {
     33 
     34 class FtpTransactionFactory;
     35 class HostMappingRules;
     36 class HttpAuthHandlerFactory;
     37 class ProxyConfigService;
     38 class URLRequestContext;
     39 
     40 class NET_EXPORT URLRequestContextBuilder {
     41  public:
     42   struct NET_EXPORT HttpCacheParams {
     43     enum Type {
     44       IN_MEMORY,
     45       DISK,
     46     };
     47 
     48     HttpCacheParams();
     49     ~HttpCacheParams();
     50 
     51     // The type of HTTP cache. Default is IN_MEMORY.
     52     Type type;
     53 
     54     // The max size of the cache in bytes. Default is algorithmically determined
     55     // based off available disk space.
     56     int max_size;
     57 
     58     // The cache path (when type is DISK).
     59     base::FilePath path;
     60   };
     61 
     62   struct NET_EXPORT HttpNetworkSessionParams {
     63     HttpNetworkSessionParams();
     64     ~HttpNetworkSessionParams();
     65 
     66     // These fields mirror those in net::HttpNetworkSession::Params;
     67     bool ignore_certificate_errors;
     68     HostMappingRules* host_mapping_rules;
     69     uint16 testing_fixed_http_port;
     70     uint16 testing_fixed_https_port;
     71     NextProtoVector next_protos;
     72     std::string trusted_spdy_proxy;
     73     bool use_alternate_protocols;
     74     bool enable_quic;
     75   };
     76 
     77   URLRequestContextBuilder();
     78   ~URLRequestContextBuilder();
     79 
     80   // These functions are mutually exclusive.  The ProxyConfigService, if
     81   // set, will be used to construct a ProxyService.
     82   void set_proxy_config_service(ProxyConfigService* proxy_config_service) {
     83     proxy_config_service_.reset(proxy_config_service);
     84   }
     85   void set_proxy_service(ProxyService* proxy_service) {
     86     proxy_service_.reset(proxy_service);
     87   }
     88 
     89   // Call these functions to specify hard-coded Accept-Language
     90   // or User-Agent header values for all requests that don't
     91   // have the headers already set.
     92   void set_accept_language(const std::string& accept_language) {
     93     accept_language_ = accept_language;
     94   }
     95   void set_user_agent(const std::string& user_agent) {
     96     user_agent_ = user_agent;
     97   }
     98 
     99   // Control support for data:// requests. By default it's disabled.
    100   void set_data_enabled(bool enable) {
    101     data_enabled_ = enable;
    102   }
    103 
    104 #if !defined(DISABLE_FILE_SUPPORT)
    105   // Control support for file:// requests. By default it's disabled.
    106   void set_file_enabled(bool enable) {
    107     file_enabled_ = enable;
    108   }
    109 #endif
    110 
    111 #if !defined(DISABLE_FTP_SUPPORT)
    112   // Control support for ftp:// requests. By default it's disabled.
    113   void set_ftp_enabled(bool enable) {
    114     ftp_enabled_ = enable;
    115   }
    116 #endif
    117 
    118   // TODO(mmenke):  Probably makes sense to get rid of this, and have consumers
    119   // set their own NetLog::Observers instead.
    120   void set_net_log(NetLog* net_log) {
    121     net_log_.reset(net_log);
    122   }
    123 
    124   // By default host_resolver is constructed with CreateDefaultResolver.
    125   void set_host_resolver(HostResolver* host_resolver) {
    126     host_resolver_.reset(host_resolver);
    127   }
    128 
    129   // Uses BasicNetworkDelegate by default. Note that calling Build will unset
    130   // any custom delegate in builder, so this must be called each time before
    131   // Build is called.
    132   void set_network_delegate(NetworkDelegate* delegate) {
    133     network_delegate_.reset(delegate);
    134   }
    135 
    136 
    137   // Adds additional auth handler factories to be used in addition to what is
    138   // provided in the default |HttpAuthHandlerRegistryFactory|. The auth |scheme|
    139   // and |factory| are provided. The builder takes ownership of the factory and
    140   // Build() must be called after this method.
    141   void add_http_auth_handler_factory(const std::string& scheme,
    142                                      net::HttpAuthHandlerFactory* factory) {
    143     extra_http_auth_handlers_.push_back(SchemeFactory(scheme, factory));
    144   }
    145 
    146   // By default HttpCache is enabled with a default constructed HttpCacheParams.
    147   void EnableHttpCache(const HttpCacheParams& params);
    148   void DisableHttpCache();
    149 
    150   // Override default net::HttpNetworkSession::Params settings.
    151   void set_http_network_session_params(
    152       const HttpNetworkSessionParams& http_network_session_params) {
    153     http_network_session_params_ = http_network_session_params;
    154   }
    155 
    156   void set_transport_security_persister_path(
    157       const base::FilePath& transport_security_persister_path) {
    158     transport_security_persister_path_ = transport_security_persister_path;
    159   }
    160 
    161   // Adjust |http_network_session_params_.next_protos| to enable SPDY and QUIC.
    162   void SetSpdyAndQuicEnabled(bool spdy_enabled,
    163                              bool quic_enabled);
    164 
    165   void set_throttling_enabled(bool throttling_enabled) {
    166     throttling_enabled_ = throttling_enabled;
    167   }
    168 
    169   void set_channel_id_enabled(bool enable) {
    170     channel_id_enabled_ = enable;
    171   }
    172 
    173   URLRequestContext* Build();
    174 
    175  private:
    176   struct NET_EXPORT SchemeFactory {
    177     SchemeFactory(const std::string& scheme,
    178                   net::HttpAuthHandlerFactory* factory);
    179     ~SchemeFactory();
    180 
    181     std::string scheme;
    182     net::HttpAuthHandlerFactory* factory;
    183   };
    184 
    185   std::string accept_language_;
    186   std::string user_agent_;
    187   // Include support for data:// requests.
    188   bool data_enabled_;
    189 #if !defined(DISABLE_FILE_SUPPORT)
    190   // Include support for file:// requests.
    191   bool file_enabled_;
    192 #endif
    193 #if !defined(DISABLE_FTP_SUPPORT)
    194   // Include support for ftp:// requests.
    195   bool ftp_enabled_;
    196 #endif
    197   bool http_cache_enabled_;
    198   bool throttling_enabled_;
    199   bool channel_id_enabled_;
    200 
    201   HttpCacheParams http_cache_params_;
    202   HttpNetworkSessionParams http_network_session_params_;
    203   base::FilePath transport_security_persister_path_;
    204   scoped_ptr<NetLog> net_log_;
    205   scoped_ptr<HostResolver> host_resolver_;
    206   scoped_ptr<ProxyConfigService> proxy_config_service_;
    207   scoped_ptr<ProxyService> proxy_service_;
    208   scoped_ptr<NetworkDelegate> network_delegate_;
    209   scoped_ptr<FtpTransactionFactory> ftp_transaction_factory_;
    210   std::vector<SchemeFactory> extra_http_auth_handlers_;
    211 
    212   DISALLOW_COPY_AND_ASSIGN(URLRequestContextBuilder);
    213 };
    214 
    215 }  // namespace net
    216 
    217 #endif  // NET_URL_REQUEST_URL_REQUEST_CONTEXT_BUILDER_H_
    218