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.
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.
10 // URLRequestContextBuilder and its associated params classes are initially
11 // populated with "sane" default values. Read through the comments to figure out
14 #ifndef NET_URL_REQUEST_URL_REQUEST_CONTEXT_BUILDER_H_
15 #define NET_URL_REQUEST_URL_REQUEST_CONTEXT_BUILDER_H_
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 "base/memory/scoped_vector.h"
25 #include "build/build_config.h"
26 #include "net/base/net_export.h"
27 #include "net/base/network_delegate.h"
28 #include "net/dns/host_resolver.h"
29 #include "net/proxy/proxy_config_service.h"
30 #include "net/proxy/proxy_service.h"
31 #include "net/quic/quic_protocol.h"
32 #include "net/socket/next_proto.h"
35 class SingleThreadTaskRunner
;
40 class ChannelIDService
;
42 class FtpTransactionFactory
;
43 class HostMappingRules
;
44 class HttpAuthHandlerFactory
;
45 class HttpServerProperties
;
46 class ProxyConfigService
;
47 class URLRequestContext
;
48 class URLRequestInterceptor
;
50 class NET_EXPORT URLRequestContextBuilder
{
52 struct NET_EXPORT HttpCacheParams
{
61 // The type of HTTP cache. Default is IN_MEMORY.
64 // The max size of the cache in bytes. Default is algorithmically determined
65 // based off available disk space.
68 // The cache path (when type is DISK).
72 struct NET_EXPORT HttpNetworkSessionParams
{
73 HttpNetworkSessionParams();
74 ~HttpNetworkSessionParams();
76 // These fields mirror those in HttpNetworkSession::Params;
77 bool ignore_certificate_errors
;
78 HostMappingRules
* host_mapping_rules
;
79 uint16 testing_fixed_http_port
;
80 uint16 testing_fixed_https_port
;
81 NextProtoVector next_protos
;
82 std::string trusted_spdy_proxy
;
83 bool use_alternate_protocols
;
85 bool enable_insecure_quic
;
86 QuicTagVector quic_connection_options
;
89 URLRequestContextBuilder();
90 ~URLRequestContextBuilder();
92 // These functions are mutually exclusive. The ProxyConfigService, if
93 // set, will be used to construct a ProxyService.
94 void set_proxy_config_service(ProxyConfigService
* proxy_config_service
) {
95 proxy_config_service_
.reset(proxy_config_service
);
97 void set_proxy_service(ProxyService
* proxy_service
) {
98 proxy_service_
.reset(proxy_service
);
101 // Call these functions to specify hard-coded Accept-Language
102 // or User-Agent header values for all requests that don't
103 // have the headers already set.
104 void set_accept_language(const std::string
& accept_language
) {
105 accept_language_
= accept_language
;
107 void set_user_agent(const std::string
& user_agent
) {
108 user_agent_
= user_agent
;
111 // Control support for data:// requests. By default it's disabled.
112 void set_data_enabled(bool enable
) {
113 data_enabled_
= enable
;
116 #if !defined(DISABLE_FILE_SUPPORT)
117 // Control support for file:// requests. By default it's disabled.
118 void set_file_enabled(bool enable
) {
119 file_enabled_
= enable
;
123 #if !defined(DISABLE_FTP_SUPPORT)
124 // Control support for ftp:// requests. By default it's disabled.
125 void set_ftp_enabled(bool enable
) {
126 ftp_enabled_
= enable
;
130 // TODO(mmenke): Probably makes sense to get rid of this, and have consumers
131 // set their own NetLog::Observers instead.
132 void set_net_log(NetLog
* net_log
) {
133 net_log_
.reset(net_log
);
136 // By default host_resolver is constructed with CreateDefaultResolver.
137 void set_host_resolver(HostResolver
* host_resolver
) {
138 host_resolver_
.reset(host_resolver
);
141 // Uses BasicNetworkDelegate by default. Note that calling Build will unset
142 // any custom delegate in builder, so this must be called each time before
144 void set_network_delegate(NetworkDelegate
* delegate
) {
145 network_delegate_
.reset(delegate
);
148 // Adds additional auth handler factories to be used in addition to what is
149 // provided in the default |HttpAuthHandlerRegistryFactory|. The auth |scheme|
150 // and |factory| are provided. The builder takes ownership of the factory and
151 // Build() must be called after this method.
152 void add_http_auth_handler_factory(const std::string
& scheme
,
153 HttpAuthHandlerFactory
* factory
) {
154 extra_http_auth_handlers_
.push_back(SchemeFactory(scheme
, factory
));
157 // By default HttpCache is enabled with a default constructed HttpCacheParams.
158 void EnableHttpCache(const HttpCacheParams
& params
);
159 void DisableHttpCache();
161 // Override default HttpNetworkSession::Params settings.
162 void set_http_network_session_params(
163 const HttpNetworkSessionParams
& http_network_session_params
) {
164 http_network_session_params_
= http_network_session_params
;
167 void set_transport_security_persister_path(
168 const base::FilePath
& transport_security_persister_path
) {
169 transport_security_persister_path_
= transport_security_persister_path
;
172 // Adjust |http_network_session_params_.next_protos| to enable SPDY and QUIC.
173 void SetSpdyAndQuicEnabled(bool spdy_enabled
,
176 void set_enable_insecure_quic(bool enable_insecure_quic
) {
177 http_network_session_params_
.enable_insecure_quic
= enable_insecure_quic
;
180 void set_quic_connection_options(
181 const QuicTagVector
& quic_connection_options
) {
182 http_network_session_params_
.quic_connection_options
=
183 quic_connection_options
;
186 void set_throttling_enabled(bool throttling_enabled
) {
187 throttling_enabled_
= throttling_enabled
;
190 void set_backoff_enabled(bool backoff_enabled
) {
191 backoff_enabled_
= backoff_enabled
;
194 void SetInterceptors(
195 ScopedVector
<URLRequestInterceptor
> url_request_interceptors
);
197 // Override the default in-memory cookie store and channel id service.
198 // |cookie_store| must not be NULL. |channel_id_service| may be NULL to
199 // disable channel id for this context.
200 // Note that a persistent cookie store should not be used with an in-memory
201 // channel id service, and one cookie store should not be shared between
202 // multiple channel-id stores (or used both with and without a channel id
204 void SetCookieAndChannelIdStores(
205 const scoped_refptr
<CookieStore
>& cookie_store
,
206 scoped_ptr
<ChannelIDService
> channel_id_service
);
208 // Sets the task runner used to perform file operations. If not set, one will
210 void SetFileTaskRunner(
211 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
);
213 // Note that if SDCH is enabled without a policy object observing
214 // the SDCH manager and handling at least Get-Dictionary events, the
215 // result will be "Content-Encoding: sdch" advertisements, but no
216 // dictionaries fetches and no specific dictionaries advertised.
217 // SdchOwner in net/sdch/sdch_owner.h is a simple policy object.
218 void set_sdch_enabled(bool enable
) { sdch_enabled_
= enable
; }
220 // Sets a specific HttpServerProperties for use in the
221 // URLRequestContext rather than creating a default HttpServerPropertiesImpl.
222 void SetHttpServerProperties(
223 scoped_ptr
<HttpServerProperties
> http_server_properties
);
225 URLRequestContext
* Build();
228 struct NET_EXPORT SchemeFactory
{
229 SchemeFactory(const std::string
& scheme
, HttpAuthHandlerFactory
* factory
);
233 HttpAuthHandlerFactory
* factory
;
236 std::string accept_language_
;
237 std::string user_agent_
;
238 // Include support for data:// requests.
240 #if !defined(DISABLE_FILE_SUPPORT)
241 // Include support for file:// requests.
244 #if !defined(DISABLE_FTP_SUPPORT)
245 // Include support for ftp:// requests.
248 bool http_cache_enabled_
;
249 bool throttling_enabled_
;
250 bool backoff_enabled_
;
253 scoped_refptr
<base::SingleThreadTaskRunner
> file_task_runner_
;
254 HttpCacheParams http_cache_params_
;
255 HttpNetworkSessionParams http_network_session_params_
;
256 base::FilePath transport_security_persister_path_
;
257 scoped_ptr
<NetLog
> net_log_
;
258 scoped_ptr
<HostResolver
> host_resolver_
;
259 scoped_ptr
<ChannelIDService
> channel_id_service_
;
260 scoped_ptr
<ProxyConfigService
> proxy_config_service_
;
261 scoped_ptr
<ProxyService
> proxy_service_
;
262 scoped_ptr
<NetworkDelegate
> network_delegate_
;
263 scoped_refptr
<CookieStore
> cookie_store_
;
264 scoped_ptr
<FtpTransactionFactory
> ftp_transaction_factory_
;
265 std::vector
<SchemeFactory
> extra_http_auth_handlers_
;
266 ScopedVector
<URLRequestInterceptor
> url_request_interceptors_
;
267 scoped_ptr
<HttpServerProperties
> http_server_properties_
;
269 DISALLOW_COPY_AND_ASSIGN(URLRequestContextBuilder
);
274 #endif // NET_URL_REQUEST_URL_REQUEST_CONTEXT_BUILDER_H_