blob: 2eddf632891a8607ee21ec8439ed3fa36b1eb7fb [file] [log] [blame]
[email protected]9c4eff22012-03-20 22:42:291// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
5// This class represents contextual information (cookies, cache, etc.)
ricea29e27d12016-08-10 02:09:186// that's necessary when processing resource requests.
initial.commit586acc5fe2008-07-26 22:42:527
[email protected]43530b32008-08-04 22:21:348#ifndef NET_URL_REQUEST_URL_REQUEST_CONTEXT_H_
9#define NET_URL_REQUEST_URL_REQUEST_CONTEXT_H_
initial.commit586acc5fe2008-07-26 22:42:5210
danakj8522a25b2016-04-16 00:17:3611#include <memory>
[email protected]7c52ed92012-04-06 15:42:4012#include <set>
[email protected]ee4c30d2012-11-07 15:08:4313#include <string>
[email protected]7c52ed92012-04-06 15:42:4014
Avi Drissman13fc8932015-12-20 04:40:4615#include "base/macros.h"
[email protected]3b63f8f42011-03-28 01:54:1516#include "base/memory/ref_counted.h"
[email protected]18590f22011-07-29 16:41:2817#include "base/memory/weak_ptr.h"
gab47aa7da2017-06-02 16:09:4318#include "base/threading/thread_checker.h"
xunjieli96ab36a72016-12-05 21:36:0519#include "base/trace_event/memory_dump_provider.h"
[email protected]172da1b2011-08-12 15:52:2620#include "net/base/net_export.h"
[email protected]2ca01e52013-10-31 22:05:1921#include "net/base/request_priority.h"
[email protected]c2dad292012-09-07 21:27:3522#include "net/http/http_network_session.h"
[email protected]db96a882011-10-09 02:01:5423#include "net/http/http_server_properties.h"
[email protected]536fd0b2013-03-14 17:41:5724#include "net/http/transport_security_state.h"
Scott Violet0caaaf432018-03-24 00:43:5925#include "net/net_buildflags.h"
rhalavatia9b551d2017-02-09 12:03:0026#include "net/traffic_annotation/network_traffic_annotation.h"
[email protected]8a26ff62012-08-24 21:49:2027#include "net/url_request/url_request.h"
initial.commit586acc5fe2008-07-26 22:42:5228
xunjieli96ab36a72016-12-05 21:36:0529namespace base {
30namespace trace_event {
31class ProcessMemoryDump;
32}
33}
34
[email protected]8ac1a752008-07-31 19:40:3735namespace net {
[email protected]822581d2010-12-16 17:27:1536class CertVerifier;
[email protected]6b8a3c742014-07-25 00:25:3537class ChannelIDService;
[email protected]9349cfb2010-08-31 18:00:5338class CookieStore;
rsleevid6de8302016-06-21 01:33:2039class CTPolicyEnforcer;
[email protected]284303b62013-11-28 15:11:5440class CTVerifier;
[email protected]9349cfb2010-08-31 18:00:5341class HostResolver;
[email protected]fa55e192010-02-15 14:25:5042class HttpAuthHandlerFactory;
[email protected]b65ce0942009-03-16 20:13:3343class HttpTransactionFactory;
[email protected]ee4c30d2012-11-07 15:08:4344class HttpUserAgentSettings;
mikecironef22f9812016-10-04 03:40:1945class NetLog;
[email protected]0651b812011-02-24 00:22:5046class NetworkDelegate;
tbansalea2fb8c2015-05-22 22:23:0047class NetworkQualityEstimator;
Lily Houghton8c2f97d2018-01-22 05:06:5948class ProxyResolutionService;
Ryan Sleevib8449e02018-07-15 04:31:0749class SSLConfigService;
[email protected]eaadd9052009-06-23 18:02:2350class URLRequest;
[email protected]a8c1e7452011-05-14 06:17:0751class URLRequestJobFactory;
[email protected]a73a2802012-05-02 19:20:1552class URLRequestThrottlerManager;
initial.commit586acc5fe2008-07-26 22:42:5253
Julia Tuttle3f9acac2017-11-20 22:06:3054#if BUILDFLAG(ENABLE_REPORTING)
Julia Tuttlecba7d222018-02-23 19:37:2755class NetworkErrorLoggingService;
Julia Tuttle3f9acac2017-11-20 22:06:3056class ReportingService;
57#endif // BUILDFLAG(ENABLE_REPORTING)
58
[email protected]27a112c2011-01-06 04:19:3059// Subclass to provide application-specific context for URLRequest
ricea29e27d12016-08-10 02:09:1860// instances. URLRequestContext does not own these member variables, since they
61// may be shared with other contexts. URLRequestContextStorage can be used for
62// automatic lifetime management. Most callers should use an existing
63// URLRequestContext rather than creating a new one, as guaranteeing that the
64// URLRequestContext is destroyed before its members can be difficult.
[email protected]172da1b2011-08-12 15:52:2665class NET_EXPORT URLRequestContext
gab47aa7da2017-06-02 16:09:4366 : public base::trace_event::MemoryDumpProvider {
initial.commit586acc5fe2008-07-26 22:42:5267 public:
[email protected]9349cfb2010-08-31 18:00:5368 URLRequestContext();
xunjieli96ab36a72016-12-05 21:36:0569 ~URLRequestContext() override;
[email protected]18590f22011-07-29 16:41:2870
[email protected]d9696672011-03-15 22:45:0971 // Copies the state from |other| into this context.
[email protected]fa22a6f12012-05-11 17:19:5972 void CopyFrom(const URLRequestContext* other);
[email protected]d9696672011-03-15 22:45:0973
tbansalea2fb8c2015-05-22 22:23:0074 // May return nullptr if this context doesn't have an associated network
75 // session.
[email protected]c2dad292012-09-07 21:27:3576 const HttpNetworkSession::Params* GetNetworkSessionParams() const;
77
mmenke6ddfbea2017-05-31 21:48:4178 // May return nullptr if this context doesn't have an associated network
79 // session.
80 const HttpNetworkSession::Context* GetNetworkSessionContext() const;
81
rhalavatia9b551d2017-02-09 12:03:0082 // This function should not be used in Chromium, please use the version with
83 // NetworkTrafficAnnotationTag in the future.
danakj8522a25b2016-04-16 00:17:3684 std::unique_ptr<URLRequest> CreateRequest(
85 const GURL& url,
86 RequestPriority priority,
87 URLRequest::Delegate* delegate) const;
[email protected]8a26ff62012-08-24 21:49:2088
rhalavatia9b551d2017-02-09 12:03:0089 // |traffic_annotation| is metadata about the network traffic send via this
90 // URLRequest, see net::DefineNetworkTrafficAnnotation. Note that:
91 // - net provides the API for tagging requests with an opaque identifier.
92 // - tools/traffic_annotation/traffic_annotation.proto contains the Chrome
93 // specific .proto describing the verbose annotation format that Chrome's
94 // callsites are expected to follow.
95 // - tools/traffic_annotation/ contains sample and template for annotation and
96 // tools will be added for verification following crbug.com/690323.
97 std::unique_ptr<URLRequest> CreateRequest(
98 const GURL& url,
99 RequestPriority priority,
100 URLRequest::Delegate* delegate,
101 NetworkTrafficAnnotationTag traffic_annotation) const;
102
103 NetLog* net_log() const { return net_log_; }
[email protected]9e743cd2010-03-16 07:03:53104
[email protected]27a112c2011-01-06 04:19:30105 void set_net_log(NetLog* net_log) {
[email protected]2fb629202010-12-23 23:52:57106 net_log_ = net_log;
107 }
108
[email protected]27a112c2011-01-06 04:19:30109 HostResolver* host_resolver() const {
[email protected]8a00f00a2009-06-12 00:49:38110 return host_resolver_;
111 }
112
[email protected]27a112c2011-01-06 04:19:30113 void set_host_resolver(HostResolver* host_resolver) {
[email protected]2fb629202010-12-23 23:52:57114 host_resolver_ = host_resolver;
115 }
116
[email protected]27a112c2011-01-06 04:19:30117 CertVerifier* cert_verifier() const {
[email protected]822581d2010-12-16 17:27:15118 return cert_verifier_;
119 }
120
[email protected]27a112c2011-01-06 04:19:30121 void set_cert_verifier(CertVerifier* cert_verifier) {
[email protected]2fb629202010-12-23 23:52:57122 cert_verifier_ = cert_verifier;
123 }
124
[email protected]6b8a3c742014-07-25 00:25:35125 ChannelIDService* channel_id_service() const {
126 return channel_id_service_;
[email protected]feb79bcd2011-07-21 16:55:17127 }
[email protected]273e37d2011-08-11 01:49:12128
[email protected]6b8a3c742014-07-25 00:25:35129 void set_channel_id_service(
130 ChannelIDService* channel_id_service) {
131 channel_id_service_ = channel_id_service;
[email protected]feb79bcd2011-07-21 16:55:17132 }
133
[email protected]63de95b2008-12-10 04:11:27134 // Get the proxy service for this context.
Lily Houghton8c2f97d2018-01-22 05:06:59135 ProxyResolutionService* proxy_resolution_service() const {
136 return proxy_resolution_service_;
137 }
138 void set_proxy_resolution_service(
139 ProxyResolutionService* proxy_resolution_service) {
140 proxy_resolution_service_ = proxy_resolution_service;
[email protected]2fb629202010-12-23 23:52:57141 }
142
[email protected]db36938c2009-08-19 21:48:42143 // Get the ssl config service for this context.
Ryan Sleevib8449e02018-07-15 04:31:07144 SSLConfigService* ssl_config_service() const { return ssl_config_service_; }
145 void set_ssl_config_service(SSLConfigService* service) {
146 ssl_config_service_ = service;
[email protected]db36938c2009-08-19 21:48:42147 }
148
[email protected]2fb629202010-12-23 23:52:57149 // Gets the HTTP Authentication Handler Factory for this context.
150 // The factory is only valid for the lifetime of this URLRequestContext
[email protected]ef2bf422012-05-11 03:27:09151 HttpAuthHandlerFactory* http_auth_handler_factory() const {
[email protected]2fb629202010-12-23 23:52:57152 return http_auth_handler_factory_;
153 }
[email protected]27a112c2011-01-06 04:19:30154 void set_http_auth_handler_factory(HttpAuthHandlerFactory* factory) {
[email protected]2fb629202010-12-23 23:52:57155 http_auth_handler_factory_ = factory;
156 }
157
initial.commit586acc5fe2008-07-26 22:42:52158 // Gets the http transaction factory for this context.
[email protected]27a112c2011-01-06 04:19:30159 HttpTransactionFactory* http_transaction_factory() const {
[email protected]db8f44c2008-12-13 04:52:01160 return http_transaction_factory_;
initial.commit586acc5fe2008-07-26 22:42:52161 }
[email protected]27a112c2011-01-06 04:19:30162 void set_http_transaction_factory(HttpTransactionFactory* factory) {
[email protected]2fb629202010-12-23 23:52:57163 http_transaction_factory_ = factory;
164 }
165
[email protected]0651b812011-02-24 00:22:50166 void set_network_delegate(NetworkDelegate* network_delegate) {
[email protected]d05ef99c2011-02-01 21:38:16167 network_delegate_ = network_delegate;
168 }
[email protected]0651b812011-02-24 00:22:50169 NetworkDelegate* network_delegate() const { return network_delegate_; }
[email protected]d05ef99c2011-02-01 21:38:16170
[email protected]db96a882011-10-09 02:01:54171 void set_http_server_properties(
bnc525e175a2016-06-20 12:36:40172 HttpServerProperties* http_server_properties) {
[email protected]db96a882011-10-09 02:01:54173 http_server_properties_ = http_server_properties;
174 }
bnc525e175a2016-06-20 12:36:40175 HttpServerProperties* http_server_properties() const {
[email protected]db96a882011-10-09 02:01:54176 return http_server_properties_;
177 }
178
[email protected]be6fca6c2010-01-30 21:48:57179 // Gets the cookie store for this context (may be null, in which case
180 // cookies are not stored).
mmenke606c59c2016-03-07 18:20:55181 CookieStore* cookie_store() const { return cookie_store_; }
[email protected]27a112c2011-01-06 04:19:30182 void set_cookie_store(CookieStore* cookie_store);
[email protected]2fb629202010-12-23 23:52:57183
[email protected]f6c21cb2011-02-16 19:45:41184 TransportSecurityState* transport_security_state() const {
[email protected]284303b62013-11-28 15:11:54185 return transport_security_state_;
[email protected]f6c21cb2011-02-16 19:45:41186 }
187 void set_transport_security_state(
[email protected]7461a402011-03-24 23:19:51188 TransportSecurityState* state) {
[email protected]f6c21cb2011-02-16 19:45:41189 transport_security_state_ = state;
190 }
[email protected]a9cea7542009-05-20 04:30:23191
[email protected]284303b62013-11-28 15:11:54192 CTVerifier* cert_transparency_verifier() const {
193 return cert_transparency_verifier_;
194 }
195 void set_cert_transparency_verifier(CTVerifier* verifier) {
196 cert_transparency_verifier_ = verifier;
197 }
198
rsleevid6de8302016-06-21 01:33:20199 CTPolicyEnforcer* ct_policy_enforcer() const { return ct_policy_enforcer_; }
200 void set_ct_policy_enforcer(CTPolicyEnforcer* enforcer) {
201 ct_policy_enforcer_ = enforcer;
202 }
203
[email protected]a8c1e7452011-05-14 06:17:07204 const URLRequestJobFactory* job_factory() const { return job_factory_; }
205 void set_job_factory(const URLRequestJobFactory* job_factory) {
206 job_factory_ = job_factory;
207 }
208
tbansalea2fb8c2015-05-22 22:23:00209 // May return nullptr.
[email protected]a73a2802012-05-02 19:20:15210 URLRequestThrottlerManager* throttler_manager() const {
211 return throttler_manager_;
212 }
213 void set_throttler_manager(URLRequestThrottlerManager* throttler_manager) {
214 throttler_manager_ = throttler_manager;
215 }
216
[email protected]7c52ed92012-04-06 15:42:40217 // Gets the URLRequest objects that hold a reference to this
218 // URLRequestContext.
Helen Lif5e4a752018-01-02 16:03:02219 std::set<const URLRequest*>* url_requests() const {
220 return url_requests_.get();
[email protected]7c52ed92012-04-06 15:42:40221 }
222
[email protected]424559492014-07-22 00:27:40223 // CHECKs that no URLRequests using this context remain. Subclasses should
224 // additionally call AssertNoURLRequests() within their own destructor,
225 // prior to implicit destruction of subclass-owned state.
[email protected]1e714bba2012-04-10 17:01:05226 void AssertNoURLRequests() const;
227
[email protected]ee4c30d2012-11-07 15:08:43228 // Get the underlying |HttpUserAgentSettings| implementation that provides
[email protected]84f05432013-03-15 01:00:12229 // the HTTP Accept-Language and User-Agent header values.
[email protected]ee4c30d2012-11-07 15:08:43230 const HttpUserAgentSettings* http_user_agent_settings() const {
231 return http_user_agent_settings_;
232 }
233 void set_http_user_agent_settings(
mmenke15992ce2017-05-30 21:05:02234 const HttpUserAgentSettings* http_user_agent_settings) {
[email protected]ee4c30d2012-11-07 15:08:43235 http_user_agent_settings_ = http_user_agent_settings;
236 }
237
tbansalea2fb8c2015-05-22 22:23:00238 // Gets the NetworkQualityEstimator associated with this context.
239 // May return nullptr.
240 NetworkQualityEstimator* network_quality_estimator() const {
241 return network_quality_estimator_;
242 }
243 void set_network_quality_estimator(
244 NetworkQualityEstimator* network_quality_estimator) {
245 network_quality_estimator_ = network_quality_estimator;
246 }
247
Julia Tuttle3f9acac2017-11-20 22:06:30248#if BUILDFLAG(ENABLE_REPORTING)
juliatuttle2b8dd2d12017-04-12 21:27:41249 ReportingService* reporting_service() const { return reporting_service_; }
250 void set_reporting_service(ReportingService* reporting_service) {
251 reporting_service_ = reporting_service;
252 }
253
Julia Tuttlecba7d222018-02-23 19:37:27254 NetworkErrorLoggingService* network_error_logging_service() const {
255 return network_error_logging_service_;
Julia Tuttlef9b74062017-07-27 14:44:05256 }
Julia Tuttlecba7d222018-02-23 19:37:27257 void set_network_error_logging_service(
258 NetworkErrorLoggingService* network_error_logging_service) {
259 network_error_logging_service_ = network_error_logging_service;
Julia Tuttlef9b74062017-07-27 14:44:05260 }
Julia Tuttle3f9acac2017-11-20 22:06:30261#endif // BUILDFLAG(ENABLE_REPORTING)
Julia Tuttlef9b74062017-07-27 14:44:05262
maksim.sisov3d40c812016-05-02 13:27:16263 void set_enable_brotli(bool enable_brotli) { enable_brotli_ = enable_brotli; }
264
265 bool enable_brotli() const { return enable_brotli_; }
266
mgershd21d6d142016-12-14 23:06:36267 // Sets the |check_cleartext_permitted| flag, which controls whether to check
268 // system policy before allowing a cleartext http or ws request.
269 void set_check_cleartext_permitted(bool check_cleartext_permitted) {
270 check_cleartext_permitted_ = check_cleartext_permitted;
271 }
272
273 // Returns current value of the |check_cleartext_permitted| flag.
274 bool check_cleartext_permitted() const { return check_cleartext_permitted_; }
275
xunjieli96ab36a72016-12-05 21:36:05276 // Sets a name for this URLRequestContext. Currently the name is used in
277 // MemoryDumpProvier to annotate memory usage. The name does not need to be
278 // unique.
Matt Menke7d4b66f72017-09-26 04:09:55279 void set_name(const std::string& name) { name_ = name; }
280 const std::string& name() const { return name_; }
xunjieli96ab36a72016-12-05 21:36:05281
282 // MemoryDumpProvider implementation:
xunjieli0c0ed3be2017-06-23 14:08:35283 // This is reported as
284 // "memory:chrome:all_processes:reported_by_chrome:net:effective_size_avg."
xunjieli96ab36a72016-12-05 21:36:05285 bool OnMemoryDump(const base::trace_event::MemoryDumpArgs& args,
286 base::trace_event::ProcessMemoryDump* pmd) override;
287
gab47aa7da2017-06-02 16:09:43288 void AssertCalledOnValidThread() {
289 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
290 }
291
[email protected]f6c21cb2011-02-16 19:45:41292 private:
[email protected]d9696672011-03-15 22:45:09293 // ---------------------------------------------------------------------------
294 // Important: When adding any new members below, consider whether they need to
295 // be added to CopyFrom.
296 // ---------------------------------------------------------------------------
297
[email protected]f6c21cb2011-02-16 19:45:41298 // Ownership for these members are not defined here. Clients should either
299 // provide storage elsewhere or have a subclass take ownership.
[email protected]27a112c2011-01-06 04:19:30300 NetLog* net_log_;
301 HostResolver* host_resolver_;
302 CertVerifier* cert_verifier_;
[email protected]6b8a3c742014-07-25 00:25:35303 ChannelIDService* channel_id_service_;
[email protected]f6c21cb2011-02-16 19:45:41304 HttpAuthHandlerFactory* http_auth_handler_factory_;
Lily Houghton8c2f97d2018-01-22 05:06:59305 ProxyResolutionService* proxy_resolution_service_;
Ryan Sleevib8449e02018-07-15 04:31:07306 SSLConfigService* ssl_config_service_;
[email protected]0651b812011-02-24 00:22:50307 NetworkDelegate* network_delegate_;
bnc525e175a2016-06-20 12:36:40308 HttpServerProperties* http_server_properties_;
mmenke15992ce2017-05-30 21:05:02309 const HttpUserAgentSettings* http_user_agent_settings_;
mmenke606c59c2016-03-07 18:20:55310 CookieStore* cookie_store_;
[email protected]edbc4f92011-09-27 21:04:49311 TransportSecurityState* transport_security_state_;
[email protected]284303b62013-11-28 15:11:54312 CTVerifier* cert_transparency_verifier_;
rsleevid6de8302016-06-21 01:33:20313 CTPolicyEnforcer* ct_policy_enforcer_;
[email protected]f6c21cb2011-02-16 19:45:41314 HttpTransactionFactory* http_transaction_factory_;
[email protected]a8c1e7452011-05-14 06:17:07315 const URLRequestJobFactory* job_factory_;
[email protected]a73a2802012-05-02 19:20:15316 URLRequestThrottlerManager* throttler_manager_;
tbansalea2fb8c2015-05-22 22:23:00317 NetworkQualityEstimator* network_quality_estimator_;
Julia Tuttle3f9acac2017-11-20 22:06:30318#if BUILDFLAG(ENABLE_REPORTING)
juliatuttle2b8dd2d12017-04-12 21:27:41319 ReportingService* reporting_service_;
Julia Tuttlecba7d222018-02-23 19:37:27320 NetworkErrorLoggingService* network_error_logging_service_;
Julia Tuttle3f9acac2017-11-20 22:06:30321#endif // BUILDFLAG(ENABLE_REPORTING)
[email protected]70b92342010-10-12 05:54:06322
[email protected]d9696672011-03-15 22:45:09323 // ---------------------------------------------------------------------------
324 // Important: When adding any new members below, consider whether they need to
325 // be added to CopyFrom.
326 // ---------------------------------------------------------------------------
327
Helen Lif5e4a752018-01-02 16:03:02328 std::unique_ptr<std::set<const URLRequest*>> url_requests_;
[email protected]7c52ed92012-04-06 15:42:40329
maksim.sisov3d40c812016-05-02 13:27:16330 // Enables Brotli Content-Encoding support.
331 bool enable_brotli_;
mgershd21d6d142016-12-14 23:06:36332 // Enables checking system policy before allowing a cleartext http or ws
333 // request. Only used on Android.
334 bool check_cleartext_permitted_;
maksim.sisov3d40c812016-05-02 13:27:16335
xunjieli96ab36a72016-12-05 21:36:05336 // An optional name which can be set to describe this URLRequestContext.
337 // Used in MemoryDumpProvier to annotate memory usage. The name does not need
338 // to be unique.
Matt Menke7d4b66f72017-09-26 04:09:55339 std::string name_;
xunjieli96ab36a72016-12-05 21:36:05340
gab47aa7da2017-06-02 16:09:43341 THREAD_CHECKER(thread_checker_);
342
[email protected]6f681a42009-01-27 22:28:54343 DISALLOW_COPY_AND_ASSIGN(URLRequestContext);
initial.commit586acc5fe2008-07-26 22:42:52344};
345
[email protected]27a112c2011-01-06 04:19:30346} // namespace net
347
[email protected]43530b32008-08-04 22:21:34348#endif // NET_URL_REQUEST_URL_REQUEST_CONTEXT_H_