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/socket/next_proto.h"
34 class FtpTransactionFactory
;
35 class HostMappingRules
;
36 class HttpAuthHandlerFactory
;
37 class ProxyConfigService
;
38 class URLRequestContext
;
40 class NET_EXPORT URLRequestContextBuilder
{
42 struct NET_EXPORT HttpCacheParams
{
51 // The type of HTTP cache. Default is IN_MEMORY.
54 // The max size of the cache in bytes. Default is algorithmically determined
55 // based off available disk space.
58 // The cache path (when type is DISK).
62 struct NET_EXPORT HttpNetworkSessionParams
{
63 HttpNetworkSessionParams();
64 ~HttpNetworkSessionParams();
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
;
76 URLRequestContextBuilder();
77 ~URLRequestContextBuilder();
79 // These functions are mutually exclusive. The ProxyConfigService, if
80 // set, will be used to construct a ProxyService.
81 void set_proxy_config_service(ProxyConfigService
* proxy_config_service
) {
82 proxy_config_service_
.reset(proxy_config_service
);
84 void set_proxy_service(ProxyService
* proxy_service
) {
85 proxy_service_
.reset(proxy_service
);
88 // Call these functions to specify hard-coded Accept-Language
89 // or User-Agent header values for all requests that don't
90 // have the headers already set.
91 void set_accept_language(const std::string
& accept_language
) {
92 accept_language_
= accept_language
;
94 void set_user_agent(const std::string
& user_agent
) {
95 user_agent_
= user_agent
;
98 // Control support for data:// requests. By default it's disabled.
99 void set_data_enabled(bool enable
) {
100 data_enabled_
= enable
;
103 #if !defined(DISABLE_FILE_SUPPORT)
104 // Control support for file:// requests. By default it's disabled.
105 void set_file_enabled(bool enable
) {
106 file_enabled_
= enable
;
110 #if !defined(DISABLE_FTP_SUPPORT)
111 // Control support for ftp:// requests. By default it's disabled.
112 void set_ftp_enabled(bool enable
) {
113 ftp_enabled_
= enable
;
117 // TODO(mmenke): Probably makes sense to get rid of this, and have consumers
118 // set their own NetLog::Observers instead.
119 void set_net_log(NetLog
* net_log
) {
120 net_log_
.reset(net_log
);
123 // By default host_resolver is constructed with CreateDefaultResolver.
124 void set_host_resolver(HostResolver
* host_resolver
) {
125 host_resolver_
.reset(host_resolver
);
128 // Uses BasicNetworkDelegate by default. Note that calling Build will unset
129 // any custom delegate in builder, so this must be called each time before
131 void set_network_delegate(NetworkDelegate
* delegate
) {
132 network_delegate_
.reset(delegate
);
136 // Adds additional auth handler factories to be used in addition to what is
137 // provided in the default |HttpAuthHandlerRegistryFactory|. The auth |scheme|
138 // and |factory| are provided. The builder takes ownership of the factory and
139 // Build() must be called after this method.
140 void add_http_auth_handler_factory(const std::string
& scheme
,
141 net::HttpAuthHandlerFactory
* factory
) {
142 extra_http_auth_handlers_
.push_back(SchemeFactory(scheme
, factory
));
145 // By default HttpCache is enabled with a default constructed HttpCacheParams.
146 void EnableHttpCache(const HttpCacheParams
& params
);
147 void DisableHttpCache();
149 // Override default net::HttpNetworkSession::Params settings.
150 void set_http_network_session_params(
151 const HttpNetworkSessionParams
& http_network_session_params
) {
152 http_network_session_params_
= http_network_session_params
;
155 void set_transport_security_persister_path(
156 const base::FilePath
& transport_security_persister_path
) {
157 transport_security_persister_path_
= transport_security_persister_path
;
160 // Adjust |http_network_session_params_.next_protos| to enable SPDY and QUIC.
161 void SetSpdyAndQuicEnabled(bool spdy_enabled
,
164 void set_throttling_enabled(bool throttling_enabled
) {
165 throttling_enabled_
= throttling_enabled
;
168 URLRequestContext
* Build();
171 struct SchemeFactory
{
172 SchemeFactory(const std::string
& scheme
,
173 net::HttpAuthHandlerFactory
* factory
);
177 net::HttpAuthHandlerFactory
* factory
;
180 std::string accept_language_
;
181 std::string user_agent_
;
182 // Include support for data:// requests.
184 #if !defined(DISABLE_FILE_SUPPORT)
185 // Include support for file:// requests.
188 #if !defined(DISABLE_FTP_SUPPORT)
189 // Include support for ftp:// requests.
192 bool http_cache_enabled_
;
193 bool throttling_enabled_
;
195 HttpCacheParams http_cache_params_
;
196 HttpNetworkSessionParams http_network_session_params_
;
197 base::FilePath transport_security_persister_path_
;
198 scoped_ptr
<NetLog
> net_log_
;
199 scoped_ptr
<HostResolver
> host_resolver_
;
200 scoped_ptr
<ProxyConfigService
> proxy_config_service_
;
201 scoped_ptr
<ProxyService
> proxy_service_
;
202 scoped_ptr
<NetworkDelegate
> network_delegate_
;
203 scoped_ptr
<FtpTransactionFactory
> ftp_transaction_factory_
;
204 std::vector
<SchemeFactory
> extra_http_auth_handlers_
;
206 DISALLOW_COPY_AND_ASSIGN(URLRequestContextBuilder
);
211 #endif // NET_URL_REQUEST_URL_REQUEST_CONTEXT_BUILDER_H_