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"
29 class FtpTransactionFactory
;
31 class HostMappingRules
;
32 class HttpAuthHandlerFactory
;
33 class ProxyConfigService
;
34 class URLRequestContext
;
35 class NetworkDelegate
;
37 class NET_EXPORT URLRequestContextBuilder
{
39 struct NET_EXPORT HttpCacheParams
{
48 // The type of HTTP cache. Default is IN_MEMORY.
51 // The max size of the cache in bytes. Default is algorithmically determined
52 // based off available disk space.
55 // The cache path (when type is DISK).
59 struct NET_EXPORT HttpNetworkSessionParams
{
60 HttpNetworkSessionParams();
61 ~HttpNetworkSessionParams();
63 // These fields mirror those in net::HttpNetworkSession::Params;
64 bool ignore_certificate_errors
;
65 HostMappingRules
* host_mapping_rules
;
66 bool http_pipelining_enabled
;
67 uint16 testing_fixed_http_port
;
68 uint16 testing_fixed_https_port
;
69 std::string trusted_spdy_proxy
;
72 URLRequestContextBuilder();
73 ~URLRequestContextBuilder();
75 void set_proxy_config_service(ProxyConfigService
* proxy_config_service
);
77 // Call these functions to specify hard-coded Accept-Language
78 // or User-Agent header values for all requests that don't
79 // have the headers already set.
80 void set_accept_language(const std::string
& accept_language
) {
81 accept_language_
= accept_language
;
83 void set_user_agent(const std::string
& user_agent
) {
84 user_agent_
= user_agent
;
87 // Control support for data:// requests. By default it's disabled.
88 void set_data_enabled(bool enable
) {
89 data_enabled_
= enable
;
92 // Control support for file:// requests. By default it's disabled.
93 void set_file_enabled(bool enable
) {
94 file_enabled_
= enable
;
97 #if !defined(DISABLE_FTP_SUPPORT)
98 // Control support for ftp:// requests. By default it's disabled.
99 void set_ftp_enabled(bool enable
) {
100 ftp_enabled_
= enable
;
104 // By default host_resolver is constructed with CreateDefaultResolver.
105 void set_host_resolver(HostResolver
* host_resolver
) {
106 host_resolver_
.reset(host_resolver
);
109 // Uses BasicNetworkDelegate by default. Note that calling Build will unset
110 // any custom delegate in builder, so this must be called each time before
112 void set_network_delegate(NetworkDelegate
* delegate
) {
113 network_delegate_
.reset(delegate
);
117 // Adds additional auth handler factories to be used in addition to what is
118 // provided in the default |HttpAuthHandlerRegistryFactory|. The auth |scheme|
119 // and |factory| are provided. The builder takes ownership of the factory and
120 // Build() must be called after this method.
121 void add_http_auth_handler_factory(const std::string
& scheme
,
122 net::HttpAuthHandlerFactory
* factory
) {
123 extra_http_auth_handlers_
.push_back(SchemeFactory(scheme
, factory
));
126 // By default HttpCache is enabled with a default constructed HttpCacheParams.
127 void EnableHttpCache(const HttpCacheParams
& params
) {
128 http_cache_enabled_
= true;
129 http_cache_params_
= params
;
132 void DisableHttpCache() {
133 http_cache_enabled_
= false;
134 http_cache_params_
= HttpCacheParams();
137 // Override default net::HttpNetworkSession::Params settings.
138 void set_http_network_session_params(
139 const HttpNetworkSessionParams
& http_network_session_params
) {
140 http_network_session_params_
= http_network_session_params
;
143 URLRequestContext
* Build();
147 struct SchemeFactory
{
148 SchemeFactory(const std::string
& scheme
,
149 net::HttpAuthHandlerFactory
* factory
);
153 net::HttpAuthHandlerFactory
* factory
;
156 std::string accept_language_
;
157 std::string user_agent_
;
158 // Include support for data:// requests.
160 // Include support for file:// requests.
162 #if !defined(DISABLE_FTP_SUPPORT)
163 // Include support for ftp:// requests.
166 bool http_cache_enabled_
;
167 HttpCacheParams http_cache_params_
;
168 HttpNetworkSessionParams http_network_session_params_
;
169 scoped_ptr
<HostResolver
> host_resolver_
;
170 scoped_ptr
<ProxyConfigService
> proxy_config_service_
;
171 scoped_ptr
<NetworkDelegate
> network_delegate_
;
172 scoped_ptr
<FtpTransactionFactory
> ftp_transaction_factory_
;
173 std::vector
<SchemeFactory
> extra_http_auth_handlers_
;
175 DISALLOW_COPY_AND_ASSIGN(URLRequestContextBuilder
);
180 #endif // NET_URL_REQUEST_URL_REQUEST_CONTEXT_BUILDER_H_