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 "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/quic/quic_protocol.h"
31 #include "net/socket/next_proto.h"
35 class ChannelIDService
;
37 class FtpTransactionFactory
;
38 class HostMappingRules
;
39 class HttpAuthHandlerFactory
;
40 class ProxyConfigService
;
41 class URLRequestContext
;
43 class NET_EXPORT URLRequestContextBuilder
{
45 struct NET_EXPORT HttpCacheParams
{
54 // The type of HTTP cache. Default is IN_MEMORY.
57 // The max size of the cache in bytes. Default is algorithmically determined
58 // based off available disk space.
61 // The cache path (when type is DISK).
65 struct NET_EXPORT HttpNetworkSessionParams
{
66 HttpNetworkSessionParams();
67 ~HttpNetworkSessionParams();
69 // These fields mirror those in net::HttpNetworkSession::Params;
70 bool ignore_certificate_errors
;
71 HostMappingRules
* host_mapping_rules
;
72 uint16 testing_fixed_http_port
;
73 uint16 testing_fixed_https_port
;
74 NextProtoVector next_protos
;
75 std::string trusted_spdy_proxy
;
76 bool use_alternate_protocols
;
78 QuicTagVector quic_connection_options
;
81 URLRequestContextBuilder();
82 ~URLRequestContextBuilder();
84 // These functions are mutually exclusive. The ProxyConfigService, if
85 // set, will be used to construct a ProxyService.
86 void set_proxy_config_service(ProxyConfigService
* proxy_config_service
) {
87 proxy_config_service_
.reset(proxy_config_service
);
89 void set_proxy_service(ProxyService
* proxy_service
) {
90 proxy_service_
.reset(proxy_service
);
93 // Call these functions to specify hard-coded Accept-Language
94 // or User-Agent header values for all requests that don't
95 // have the headers already set.
96 void set_accept_language(const std::string
& accept_language
) {
97 accept_language_
= accept_language
;
99 void set_user_agent(const std::string
& user_agent
) {
100 user_agent_
= user_agent
;
103 // Control support for data:// requests. By default it's disabled.
104 void set_data_enabled(bool enable
) {
105 data_enabled_
= enable
;
108 #if !defined(DISABLE_FILE_SUPPORT)
109 // Control support for file:// requests. By default it's disabled.
110 void set_file_enabled(bool enable
) {
111 file_enabled_
= enable
;
115 #if !defined(DISABLE_FTP_SUPPORT)
116 // Control support for ftp:// requests. By default it's disabled.
117 void set_ftp_enabled(bool enable
) {
118 ftp_enabled_
= enable
;
122 // TODO(mmenke): Probably makes sense to get rid of this, and have consumers
123 // set their own NetLog::Observers instead.
124 void set_net_log(NetLog
* net_log
) {
125 net_log_
.reset(net_log
);
128 // By default host_resolver is constructed with CreateDefaultResolver.
129 void set_host_resolver(HostResolver
* host_resolver
) {
130 host_resolver_
.reset(host_resolver
);
133 // Uses BasicNetworkDelegate by default. Note that calling Build will unset
134 // any custom delegate in builder, so this must be called each time before
136 void set_network_delegate(NetworkDelegate
* delegate
) {
137 network_delegate_
.reset(delegate
);
141 // Adds additional auth handler factories to be used in addition to what is
142 // provided in the default |HttpAuthHandlerRegistryFactory|. The auth |scheme|
143 // and |factory| are provided. The builder takes ownership of the factory and
144 // Build() must be called after this method.
145 void add_http_auth_handler_factory(const std::string
& scheme
,
146 net::HttpAuthHandlerFactory
* factory
) {
147 extra_http_auth_handlers_
.push_back(SchemeFactory(scheme
, factory
));
150 // By default HttpCache is enabled with a default constructed HttpCacheParams.
151 void EnableHttpCache(const HttpCacheParams
& params
);
152 void DisableHttpCache();
154 // Override default net::HttpNetworkSession::Params settings.
155 void set_http_network_session_params(
156 const HttpNetworkSessionParams
& http_network_session_params
) {
157 http_network_session_params_
= http_network_session_params
;
160 void set_transport_security_persister_path(
161 const base::FilePath
& transport_security_persister_path
) {
162 transport_security_persister_path_
= transport_security_persister_path
;
165 // Adjust |http_network_session_params_.next_protos| to enable SPDY and QUIC.
166 void SetSpdyAndQuicEnabled(bool spdy_enabled
,
169 void set_quic_connection_options(
170 const QuicTagVector
& quic_connection_options
) {
171 http_network_session_params_
.quic_connection_options
=
172 quic_connection_options
;
175 void set_throttling_enabled(bool throttling_enabled
) {
176 throttling_enabled_
= throttling_enabled
;
179 // Override the default in-memory cookie store and channel id service.
180 // |cookie_store| must not be NULL. |channel_id_service| may be NULL to
181 // disable channel id for this context.
182 // Note that a persistent cookie store should not be used with an in-memory
183 // channel id service, and one cookie store should not be shared between
184 // multiple channel-id stores (or used both with and without a channel id
186 void SetCookieAndChannelIdStores(
187 const scoped_refptr
<CookieStore
>& cookie_store
,
188 scoped_ptr
<ChannelIDService
> channel_id_service
);
190 URLRequestContext
* Build();
193 struct NET_EXPORT SchemeFactory
{
194 SchemeFactory(const std::string
& scheme
,
195 net::HttpAuthHandlerFactory
* factory
);
199 net::HttpAuthHandlerFactory
* factory
;
202 std::string accept_language_
;
203 std::string user_agent_
;
204 // Include support for data:// requests.
206 #if !defined(DISABLE_FILE_SUPPORT)
207 // Include support for file:// requests.
210 #if !defined(DISABLE_FTP_SUPPORT)
211 // Include support for ftp:// requests.
214 bool http_cache_enabled_
;
215 bool throttling_enabled_
;
217 HttpCacheParams http_cache_params_
;
218 HttpNetworkSessionParams http_network_session_params_
;
219 base::FilePath transport_security_persister_path_
;
220 scoped_ptr
<NetLog
> net_log_
;
221 scoped_ptr
<HostResolver
> host_resolver_
;
222 scoped_ptr
<ChannelIDService
> channel_id_service_
;
223 scoped_ptr
<ProxyConfigService
> proxy_config_service_
;
224 scoped_ptr
<ProxyService
> proxy_service_
;
225 scoped_ptr
<NetworkDelegate
> network_delegate_
;
226 scoped_refptr
<CookieStore
> cookie_store_
;
227 scoped_ptr
<FtpTransactionFactory
> ftp_transaction_factory_
;
228 std::vector
<SchemeFactory
> extra_http_auth_handlers_
;
230 DISALLOW_COPY_AND_ASSIGN(URLRequestContextBuilder
);
235 #endif // NET_URL_REQUEST_URL_REQUEST_CONTEXT_BUILDER_H_