blob: 41db45a7a74edda314cf423a37ab14e907f5eb85 [file] [log] [blame]
[email protected]e26fe072012-04-17 21:38:171// 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_
[email protected]e26fe072012-04-17 21:38:1716
17#include <string>
[email protected]1575e3d2014-05-03 22:21:4418#include <vector>
[email protected]e26fe072012-04-17 21:38:1719
20#include "base/basictypes.h"
[email protected]57999812013-02-24 05:40:5221#include "base/files/file_path.h"
[email protected]e26fe072012-04-17 21:38:1722#include "base/memory/ref_counted.h"
[email protected]e52acc82012-04-21 01:21:0623#include "base/memory/scoped_ptr.h"
24#include "build/build_config.h"
[email protected]e26fe072012-04-17 21:38:1725#include "net/base/net_export.h"
26
27namespace net {
28
[email protected]e0f35c92013-05-08 16:04:3429class FtpTransactionFactory;
[email protected]72d8220d2013-11-27 15:31:2130class HostResolver;
[email protected]c2dad292012-09-07 21:27:3531class HostMappingRules;
[email protected]1575e3d2014-05-03 22:21:4432class HttpAuthHandlerFactory;
[email protected]e52acc82012-04-21 01:21:0633class ProxyConfigService;
[email protected]e26fe072012-04-17 21:38:1734class URLRequestContext;
[email protected]03a2e982012-10-27 03:20:2935class NetworkDelegate;
[email protected]e26fe072012-04-17 21:38:1736
37class NET_EXPORT URLRequestContextBuilder {
38 public:
[email protected]e26fe072012-04-17 21:38:1739 struct NET_EXPORT HttpCacheParams {
40 enum Type {
41 IN_MEMORY,
42 DISK,
43 };
44
45 HttpCacheParams();
46 ~HttpCacheParams();
47
[email protected]393e5ec62012-07-09 19:08:2848 // The type of HTTP cache. Default is IN_MEMORY.
[email protected]e26fe072012-04-17 21:38:1749 Type type;
50
51 // The max size of the cache in bytes. Default is algorithmically determined
52 // based off available disk space.
53 int max_size;
54
55 // The cache path (when type is DISK).
[email protected]a3ef4832013-02-02 05:12:3356 base::FilePath path;
[email protected]e26fe072012-04-17 21:38:1757 };
58
[email protected]c2dad292012-09-07 21:27:3559 struct NET_EXPORT HttpNetworkSessionParams {
60 HttpNetworkSessionParams();
61 ~HttpNetworkSessionParams();
62
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;
70 };
71
[email protected]e26fe072012-04-17 21:38:1772 URLRequestContextBuilder();
73 ~URLRequestContextBuilder();
74
[email protected]e52acc82012-04-21 01:21:0675 void set_proxy_config_service(ProxyConfigService* proxy_config_service);
[email protected]e52acc82012-04-21 01:21:0676
[email protected]84f05432013-03-15 01:00:1277 // Call these functions to specify hard-coded Accept-Language
78 // or User-Agent header values for all requests that don't
[email protected]ee4c30d2012-11-07 15:08:4379 // have the headers already set.
80 void set_accept_language(const std::string& accept_language) {
81 accept_language_ = accept_language;
82 }
[email protected]e26fe072012-04-17 21:38:1783 void set_user_agent(const std::string& user_agent) {
84 user_agent_ = user_agent;
85 }
86
[email protected]e0f35c92013-05-08 16:04:3487 // Control support for data:// requests. By default it's disabled.
88 void set_data_enabled(bool enable) {
89 data_enabled_ = enable;
90 }
91
[email protected]02494ec2014-05-07 15:05:2992#if !defined(DISABLE_FILE_SUPPORT)
[email protected]e0f35c92013-05-08 16:04:3493 // Control support for file:// requests. By default it's disabled.
94 void set_file_enabled(bool enable) {
95 file_enabled_ = enable;
96 }
[email protected]02494ec2014-05-07 15:05:2997#endif
[email protected]e0f35c92013-05-08 16:04:3498
99#if !defined(DISABLE_FTP_SUPPORT)
100 // Control support for ftp:// requests. By default it's disabled.
[email protected]e26fe072012-04-17 21:38:17101 void set_ftp_enabled(bool enable) {
102 ftp_enabled_ = enable;
103 }
[email protected]e0f35c92013-05-08 16:04:34104#endif
[email protected]e26fe072012-04-17 21:38:17105
[email protected]72d8220d2013-11-27 15:31:21106 // By default host_resolver is constructed with CreateDefaultResolver.
107 void set_host_resolver(HostResolver* host_resolver) {
108 host_resolver_.reset(host_resolver);
109 }
110
[email protected]03a2e982012-10-27 03:20:29111 // Uses BasicNetworkDelegate by default. Note that calling Build will unset
112 // any custom delegate in builder, so this must be called each time before
113 // Build is called.
114 void set_network_delegate(NetworkDelegate* delegate) {
115 network_delegate_.reset(delegate);
116 }
117
[email protected]1575e3d2014-05-03 22:21:44118
119 // Adds additional auth handler factories to be used in addition to what is
120 // provided in the default |HttpAuthHandlerRegistryFactory|. The auth |scheme|
121 // and |factory| are provided. The builder takes ownership of the factory and
122 // Build() must be called after this method.
123 void add_http_auth_handler_factory(const std::string& scheme,
124 net::HttpAuthHandlerFactory* factory) {
125 extra_http_auth_handlers_.push_back(SchemeFactory(scheme, factory));
126 }
127
[email protected]e26fe072012-04-17 21:38:17128 // By default HttpCache is enabled with a default constructed HttpCacheParams.
[email protected]03a2e982012-10-27 03:20:29129 void EnableHttpCache(const HttpCacheParams& params) {
[email protected]ff316f12014-01-19 00:08:20130 http_cache_enabled_ = true;
[email protected]03a2e982012-10-27 03:20:29131 http_cache_params_ = params;
132 }
133
134 void DisableHttpCache() {
[email protected]ff316f12014-01-19 00:08:20135 http_cache_enabled_ = false;
[email protected]03a2e982012-10-27 03:20:29136 http_cache_params_ = HttpCacheParams();
137 }
[email protected]e26fe072012-04-17 21:38:17138
[email protected]c2dad292012-09-07 21:27:35139 // Override default net::HttpNetworkSession::Params settings.
140 void set_http_network_session_params(
141 const HttpNetworkSessionParams& http_network_session_params) {
142 http_network_session_params_ = http_network_session_params;
143 }
144
[email protected]ef2bf422012-05-11 03:27:09145 URLRequestContext* Build();
[email protected]e26fe072012-04-17 21:38:17146
147 private:
[email protected]1575e3d2014-05-03 22:21:44148
149 struct SchemeFactory {
150 SchemeFactory(const std::string& scheme,
151 net::HttpAuthHandlerFactory* factory);
152 ~SchemeFactory();
153
154 std::string scheme;
155 net::HttpAuthHandlerFactory* factory;
156 };
157
[email protected]ee4c30d2012-11-07 15:08:43158 std::string accept_language_;
[email protected]e26fe072012-04-17 21:38:17159 std::string user_agent_;
[email protected]e0f35c92013-05-08 16:04:34160 // Include support for data:// requests.
161 bool data_enabled_;
[email protected]02494ec2014-05-07 15:05:29162#if !defined(DISABLE_FILE_SUPPORT)
[email protected]e0f35c92013-05-08 16:04:34163 // Include support for file:// requests.
164 bool file_enabled_;
[email protected]02494ec2014-05-07 15:05:29165#endif
[email protected]e0f35c92013-05-08 16:04:34166#if !defined(DISABLE_FTP_SUPPORT)
167 // Include support for ftp:// requests.
[email protected]e26fe072012-04-17 21:38:17168 bool ftp_enabled_;
[email protected]e0f35c92013-05-08 16:04:34169#endif
[email protected]e26fe072012-04-17 21:38:17170 bool http_cache_enabled_;
171 HttpCacheParams http_cache_params_;
[email protected]c2dad292012-09-07 21:27:35172 HttpNetworkSessionParams http_network_session_params_;
[email protected]72d8220d2013-11-27 15:31:21173 scoped_ptr<HostResolver> host_resolver_;
[email protected]e52acc82012-04-21 01:21:06174 scoped_ptr<ProxyConfigService> proxy_config_service_;
[email protected]03a2e982012-10-27 03:20:29175 scoped_ptr<NetworkDelegate> network_delegate_;
[email protected]e0f35c92013-05-08 16:04:34176 scoped_ptr<FtpTransactionFactory> ftp_transaction_factory_;
[email protected]1575e3d2014-05-03 22:21:44177 std::vector<SchemeFactory> extra_http_auth_handlers_;
[email protected]e26fe072012-04-17 21:38:17178
179 DISALLOW_COPY_AND_ASSIGN(URLRequestContextBuilder);
180};
181
182} // namespace net
183
184#endif // NET_URL_REQUEST_URL_REQUEST_CONTEXT_BUILDER_H_