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