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