blob: a87d328abcad81bacb3a55cf6926aed6e8557361 [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
ainozakib2207272021-09-17 01:30:3511#include <stdint.h>
danakj8522a25b2016-04-16 00:17:3612#include <memory>
[email protected]7c52ed92012-04-06 15:42:4013#include <set>
[email protected]ee4c30d2012-11-07 15:08:4314#include <string>
[email protected]7c52ed92012-04-06 15:42:4015
Keishi Hattori0e45c022021-11-27 09:25:5216#include "base/memory/raw_ptr.h"
[email protected]3b63f8f42011-03-28 01:54:1517#include "base/memory/ref_counted.h"
[email protected]18590f22011-07-29 16:41:2818#include "base/memory/weak_ptr.h"
gab47aa7da2017-06-02 16:09:4319#include "base/threading/thread_checker.h"
Nicolas Ouellet-Payeur6e833b52019-07-02 17:18:0820#include "build/build_config.h"
Yuta Hijikata101ed2a2020-11-18 07:50:3921#include "build/chromeos_buildflags.h"
[email protected]172da1b2011-08-12 15:52:2622#include "net/base/net_export.h"
Stefano Duo44af16c2022-02-10 15:23:1123#include "net/base/network_change_notifier.h"
[email protected]2ca01e52013-10-31 22:05:1924#include "net/base/request_priority.h"
ainozaki7eefe912021-10-12 03:40:2625#include "net/log/net_log_source.h"
Scott Violet0caaaf432018-03-24 00:43:5926#include "net/net_buildflags.h"
rhalavatia9b551d2017-02-09 12:03:0027#include "net/traffic_annotation/network_traffic_annotation.h"
[email protected]8a26ff62012-08-24 21:49:2028#include "net/url_request/url_request.h"
ainozakib2207272021-09-17 01:30:3529#include "third_party/abseil-cpp/absl/types/optional.h"
initial.commit586acc5fe2008-07-26 22:42:5230
[email protected]8ac1a752008-07-31 19:40:3731namespace net {
[email protected]822581d2010-12-16 17:27:1532class CertVerifier;
[email protected]9349cfb2010-08-31 18:00:5333class CookieStore;
rsleevid6de8302016-06-21 01:33:2034class CTPolicyEnforcer;
[email protected]9349cfb2010-08-31 18:00:5335class HostResolver;
[email protected]fa55e192010-02-15 14:25:5036class HttpAuthHandlerFactory;
Matt Menke30a878c2021-07-20 22:25:0937struct HttpNetworkSessionContext;
38struct HttpNetworkSessionParams;
Matt Menkece5d765372021-08-17 18:24:1239class HttpServerProperties;
[email protected]b65ce0942009-03-16 20:13:3340class HttpTransactionFactory;
[email protected]ee4c30d2012-11-07 15:08:4341class HttpUserAgentSettings;
mikecironef22f9812016-10-04 03:40:1942class NetLog;
[email protected]0651b812011-02-24 00:22:5043class NetworkDelegate;
tbansalea2fb8c2015-05-22 22:23:0044class NetworkQualityEstimator;
Wojciech Dzierżanowski1f823562019-01-18 11:26:0045class ProxyDelegate;
Nicolas Arciniega8ec5bfa2020-03-20 05:07:2646class ProxyResolutionService;
Victor Vasiliev7752898d2019-11-14 21:30:2247class QuicContext;
Chris Thompsonf31b2492020-07-21 05:47:4248class SCTAuditingDelegate;
Ryan Sleevib8449e02018-07-15 04:31:0749class SSLConfigService;
Matt Menkece5d765372021-08-17 18:24:1250class TransportSecurityState;
[email protected]eaadd9052009-06-23 18:02:2351class URLRequest;
[email protected]a8c1e7452011-05-14 06:17:0752class URLRequestJobFactory;
[email protected]a73a2802012-05-02 19:20:1553class URLRequestThrottlerManager;
initial.commit586acc5fe2008-07-26 22:42:5254
Julia Tuttle3f9acac2017-11-20 22:06:3055#if BUILDFLAG(ENABLE_REPORTING)
Julia Tuttlecba7d222018-02-23 19:37:2756class NetworkErrorLoggingService;
Julia Tuttle3f9acac2017-11-20 22:06:3057class ReportingService;
58#endif // BUILDFLAG(ENABLE_REPORTING)
59
[email protected]27a112c2011-01-06 04:19:3060// Subclass to provide application-specific context for URLRequest
ricea29e27d12016-08-10 02:09:1861// instances. URLRequestContext does not own these member variables, since they
62// may be shared with other contexts. URLRequestContextStorage can be used for
63// automatic lifetime management. Most callers should use an existing
64// URLRequestContext rather than creating a new one, as guaranteeing that the
65// URLRequestContext is destroyed before its members can be difficult.
Kenichi Ishibashia92b9652021-09-10 23:43:4466class NET_EXPORT URLRequestContext {
initial.commit586acc5fe2008-07-26 22:42:5267 public:
Maks Orlovich9fdb7272019-12-13 01:28:2368 URLRequestContext();
Peter Boström293b1342021-09-22 17:31:4369
70 URLRequestContext(const URLRequestContext&) = delete;
71 URLRequestContext& operator=(const URLRequestContext&) = delete;
72
Kenichi Ishibashia92b9652021-09-10 23:43:4473 virtual ~URLRequestContext();
[email protected]18590f22011-07-29 16:41:2874
tbansalea2fb8c2015-05-22 22:23:0075 // May return nullptr if this context doesn't have an associated network
76 // session.
Matt Menke30a878c2021-07-20 22:25:0977 const HttpNetworkSessionParams* GetNetworkSessionParams() const;
[email protected]c2dad292012-09-07 21:27:3578
mmenke6ddfbea2017-05-31 21:48:4179 // May return nullptr if this context doesn't have an associated network
80 // session.
Matt Menke30a878c2021-07-20 22:25:0981 const HttpNetworkSessionContext* GetNetworkSessionContext() const;
mmenke6ddfbea2017-05-31 21:48:4182
Yuta Hijikata101ed2a2020-11-18 07:50:3983// TODO(crbug.com/1052397): Revisit once build flag switch of lacros-chrome is
84// complete.
Xiaohan Wang2a6845b2022-01-08 04:40:5785#if !BUILDFLAG(IS_WIN) && \
86 !(BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS_LACROS))
rhalavatia9b551d2017-02-09 12:03:0087 // This function should not be used in Chromium, please use the version with
88 // NetworkTrafficAnnotationTag in the future.
Nicolas Ouellet-Payeur6e833b52019-07-02 17:18:0889 //
90 // The unannotated method is not available on desktop Linux + Windows. It's
91 // available on other platforms, since we only audit network annotations on
92 // Linux & Windows.
danakj8522a25b2016-04-16 00:17:3693 std::unique_ptr<URLRequest> CreateRequest(
94 const GURL& url,
95 RequestPriority priority,
96 URLRequest::Delegate* delegate) const;
Nicolas Ouellet-Payeur6e833b52019-07-02 17:18:0897#endif
[email protected]8a26ff62012-08-24 21:49:2098
Matt Menkefe9a9df2021-09-08 17:55:2499 // `traffic_annotation` is metadata about the network traffic send via this
rhalavatia9b551d2017-02-09 12:03:00100 // URLRequest, see net::DefineNetworkTrafficAnnotation. Note that:
101 // - net provides the API for tagging requests with an opaque identifier.
Nicolas Ouellet-Payeur326a2692021-07-15 01:17:33102 // - chrome/browser/privacy/traffic_annotation.proto contains the Chrome
rhalavatia9b551d2017-02-09 12:03:00103 // specific .proto describing the verbose annotation format that Chrome's
104 // callsites are expected to follow.
105 // - tools/traffic_annotation/ contains sample and template for annotation and
106 // tools will be added for verification following crbug.com/690323.
Matt Menkefe9a9df2021-09-08 17:55:24107 //
108 // `is_for_websockets` should be true iff this was created for use by a
109 // websocket. HTTP/HTTPS requests fail if it's true, and WS/WSS requests fail
110 // if it's false. This is to protect against broken consumers.
ainozakib2207272021-09-17 01:30:35111 //
112 // `net_log_source_id` is used to construct NetLogWithSource using the
113 // specified Source ID. This method is expected to be used when URLRequest
114 // wants to take over existing NetLogSource.
rhalavatia9b551d2017-02-09 12:03:00115 std::unique_ptr<URLRequest> CreateRequest(
116 const GURL& url,
117 RequestPriority priority,
118 URLRequest::Delegate* delegate,
Matt Menkefe9a9df2021-09-08 17:55:24119 NetworkTrafficAnnotationTag traffic_annotation,
ainozakib2207272021-09-17 01:30:35120 bool is_for_websockets = false,
ainozaki7eefe912021-10-12 03:40:26121 const absl::optional<net::NetLogSource> net_log_source =
122 absl::nullopt) const;
rhalavatia9b551d2017-02-09 12:03:00123
124 NetLog* net_log() const { return net_log_; }
[email protected]9e743cd2010-03-16 07:03:53125
[email protected]27a112c2011-01-06 04:19:30126 void set_net_log(NetLog* net_log) {
[email protected]2fb629202010-12-23 23:52:57127 net_log_ = net_log;
128 }
129
[email protected]27a112c2011-01-06 04:19:30130 HostResolver* host_resolver() const {
[email protected]8a00f00a2009-06-12 00:49:38131 return host_resolver_;
132 }
133
[email protected]27a112c2011-01-06 04:19:30134 void set_host_resolver(HostResolver* host_resolver) {
Eric Orth5d9958c2019-09-27 20:43:18135 DCHECK(host_resolver);
[email protected]2fb629202010-12-23 23:52:57136 host_resolver_ = host_resolver;
137 }
138
[email protected]27a112c2011-01-06 04:19:30139 CertVerifier* cert_verifier() const {
[email protected]822581d2010-12-16 17:27:15140 return cert_verifier_;
141 }
142
[email protected]27a112c2011-01-06 04:19:30143 void set_cert_verifier(CertVerifier* cert_verifier) {
[email protected]2fb629202010-12-23 23:52:57144 cert_verifier_ = cert_verifier;
145 }
146
[email protected]63de95b2008-12-10 04:11:27147 // Get the proxy service for this context.
Nicolas Arciniega8ec5bfa2020-03-20 05:07:26148 ProxyResolutionService* proxy_resolution_service() const {
Lily Houghton8c2f97d2018-01-22 05:06:59149 return proxy_resolution_service_;
150 }
151 void set_proxy_resolution_service(
Nicolas Arciniega8ec5bfa2020-03-20 05:07:26152 ProxyResolutionService* proxy_resolution_service) {
Lily Houghton8c2f97d2018-01-22 05:06:59153 proxy_resolution_service_ = proxy_resolution_service;
[email protected]2fb629202010-12-23 23:52:57154 }
155
Wojciech Dzierżanowski1f823562019-01-18 11:26:00156 ProxyDelegate* proxy_delegate() const { return proxy_delegate_; }
157 void set_proxy_delegate(ProxyDelegate* proxy_delegate) {
158 proxy_delegate_ = proxy_delegate;
159 }
160
[email protected]db36938c2009-08-19 21:48:42161 // Get the ssl config service for this context.
Ryan Sleevib8449e02018-07-15 04:31:07162 SSLConfigService* ssl_config_service() const { return ssl_config_service_; }
163 void set_ssl_config_service(SSLConfigService* service) {
164 ssl_config_service_ = service;
[email protected]db36938c2009-08-19 21:48:42165 }
166
[email protected]2fb629202010-12-23 23:52:57167 // Gets the HTTP Authentication Handler Factory for this context.
168 // The factory is only valid for the lifetime of this URLRequestContext
[email protected]ef2bf422012-05-11 03:27:09169 HttpAuthHandlerFactory* http_auth_handler_factory() const {
[email protected]2fb629202010-12-23 23:52:57170 return http_auth_handler_factory_;
171 }
[email protected]27a112c2011-01-06 04:19:30172 void set_http_auth_handler_factory(HttpAuthHandlerFactory* factory) {
[email protected]2fb629202010-12-23 23:52:57173 http_auth_handler_factory_ = factory;
174 }
175
initial.commit586acc5fe2008-07-26 22:42:52176 // Gets the http transaction factory for this context.
[email protected]27a112c2011-01-06 04:19:30177 HttpTransactionFactory* http_transaction_factory() const {
[email protected]db8f44c2008-12-13 04:52:01178 return http_transaction_factory_;
initial.commit586acc5fe2008-07-26 22:42:52179 }
[email protected]27a112c2011-01-06 04:19:30180 void set_http_transaction_factory(HttpTransactionFactory* factory) {
[email protected]2fb629202010-12-23 23:52:57181 http_transaction_factory_ = factory;
182 }
183
[email protected]0651b812011-02-24 00:22:50184 void set_network_delegate(NetworkDelegate* network_delegate) {
[email protected]d05ef99c2011-02-01 21:38:16185 network_delegate_ = network_delegate;
186 }
[email protected]0651b812011-02-24 00:22:50187 NetworkDelegate* network_delegate() const { return network_delegate_; }
[email protected]d05ef99c2011-02-01 21:38:16188
[email protected]db96a882011-10-09 02:01:54189 void set_http_server_properties(
bnc525e175a2016-06-20 12:36:40190 HttpServerProperties* http_server_properties) {
[email protected]db96a882011-10-09 02:01:54191 http_server_properties_ = http_server_properties;
192 }
bnc525e175a2016-06-20 12:36:40193 HttpServerProperties* http_server_properties() const {
[email protected]db96a882011-10-09 02:01:54194 return http_server_properties_;
195 }
196
[email protected]be6fca6c2010-01-30 21:48:57197 // Gets the cookie store for this context (may be null, in which case
198 // cookies are not stored).
mmenke606c59c2016-03-07 18:20:55199 CookieStore* cookie_store() const { return cookie_store_; }
[email protected]27a112c2011-01-06 04:19:30200 void set_cookie_store(CookieStore* cookie_store);
[email protected]2fb629202010-12-23 23:52:57201
[email protected]f6c21cb2011-02-16 19:45:41202 TransportSecurityState* transport_security_state() const {
[email protected]284303b62013-11-28 15:11:54203 return transport_security_state_;
[email protected]f6c21cb2011-02-16 19:45:41204 }
205 void set_transport_security_state(
[email protected]7461a402011-03-24 23:19:51206 TransportSecurityState* state) {
[email protected]f6c21cb2011-02-16 19:45:41207 transport_security_state_ = state;
208 }
[email protected]a9cea7542009-05-20 04:30:23209
rsleevid6de8302016-06-21 01:33:20210 CTPolicyEnforcer* ct_policy_enforcer() const { return ct_policy_enforcer_; }
211 void set_ct_policy_enforcer(CTPolicyEnforcer* enforcer) {
212 ct_policy_enforcer_ = enforcer;
213 }
214
Chris Thompsonf31b2492020-07-21 05:47:42215 SCTAuditingDelegate* sct_auditing_delegate() const {
216 return sct_auditing_delegate_;
217 }
218 void set_sct_auditing_delegate(SCTAuditingDelegate* delegate) {
219 sct_auditing_delegate_ = delegate;
220 }
221
[email protected]a8c1e7452011-05-14 06:17:07222 const URLRequestJobFactory* job_factory() const { return job_factory_; }
223 void set_job_factory(const URLRequestJobFactory* job_factory) {
224 job_factory_ = job_factory;
225 }
226
tbansalea2fb8c2015-05-22 22:23:00227 // May return nullptr.
[email protected]a73a2802012-05-02 19:20:15228 URLRequestThrottlerManager* throttler_manager() const {
229 return throttler_manager_;
230 }
231 void set_throttler_manager(URLRequestThrottlerManager* throttler_manager) {
232 throttler_manager_ = throttler_manager;
233 }
234
Victor Vasiliev7752898d2019-11-14 21:30:22235 QuicContext* quic_context() const { return quic_context_; }
236 void set_quic_context(QuicContext* quic_context) {
237 quic_context_ = quic_context;
238 }
239
[email protected]7c52ed92012-04-06 15:42:40240 // Gets the URLRequest objects that hold a reference to this
241 // URLRequestContext.
Helen Lif5e4a752018-01-02 16:03:02242 std::set<const URLRequest*>* url_requests() const {
243 return url_requests_.get();
[email protected]7c52ed92012-04-06 15:42:40244 }
245
[email protected]424559492014-07-22 00:27:40246 // CHECKs that no URLRequests using this context remain. Subclasses should
247 // additionally call AssertNoURLRequests() within their own destructor,
248 // prior to implicit destruction of subclass-owned state.
[email protected]1e714bba2012-04-10 17:01:05249 void AssertNoURLRequests() const;
250
[email protected]ee4c30d2012-11-07 15:08:43251 // Get the underlying |HttpUserAgentSettings| implementation that provides
[email protected]84f05432013-03-15 01:00:12252 // the HTTP Accept-Language and User-Agent header values.
[email protected]ee4c30d2012-11-07 15:08:43253 const HttpUserAgentSettings* http_user_agent_settings() const {
254 return http_user_agent_settings_;
255 }
256 void set_http_user_agent_settings(
mmenke15992ce2017-05-30 21:05:02257 const HttpUserAgentSettings* http_user_agent_settings) {
[email protected]ee4c30d2012-11-07 15:08:43258 http_user_agent_settings_ = http_user_agent_settings;
259 }
260
tbansalea2fb8c2015-05-22 22:23:00261 // Gets the NetworkQualityEstimator associated with this context.
262 // May return nullptr.
263 NetworkQualityEstimator* network_quality_estimator() const {
264 return network_quality_estimator_;
265 }
266 void set_network_quality_estimator(
267 NetworkQualityEstimator* network_quality_estimator) {
268 network_quality_estimator_ = network_quality_estimator;
269 }
270
Julia Tuttle3f9acac2017-11-20 22:06:30271#if BUILDFLAG(ENABLE_REPORTING)
juliatuttle2b8dd2d12017-04-12 21:27:41272 ReportingService* reporting_service() const { return reporting_service_; }
273 void set_reporting_service(ReportingService* reporting_service) {
274 reporting_service_ = reporting_service;
275 }
276
Julia Tuttlecba7d222018-02-23 19:37:27277 NetworkErrorLoggingService* network_error_logging_service() const {
278 return network_error_logging_service_;
Julia Tuttlef9b74062017-07-27 14:44:05279 }
Julia Tuttlecba7d222018-02-23 19:37:27280 void set_network_error_logging_service(
281 NetworkErrorLoggingService* network_error_logging_service) {
282 network_error_logging_service_ = network_error_logging_service;
Julia Tuttlef9b74062017-07-27 14:44:05283 }
Julia Tuttle3f9acac2017-11-20 22:06:30284#endif // BUILDFLAG(ENABLE_REPORTING)
Julia Tuttlef9b74062017-07-27 14:44:05285
maksim.sisov3d40c812016-05-02 13:27:16286 void set_enable_brotli(bool enable_brotli) { enable_brotli_ = enable_brotli; }
287
288 bool enable_brotli() const { return enable_brotli_; }
289
mgershd21d6d142016-12-14 23:06:36290 // Sets the |check_cleartext_permitted| flag, which controls whether to check
291 // system policy before allowing a cleartext http or ws request.
292 void set_check_cleartext_permitted(bool check_cleartext_permitted) {
293 check_cleartext_permitted_ = check_cleartext_permitted;
294 }
295
296 // Returns current value of the |check_cleartext_permitted| flag.
297 bool check_cleartext_permitted() const { return check_cleartext_permitted_; }
298
Matt Menkeee10f0412020-05-14 14:16:03299 void set_require_network_isolation_key(bool require_network_isolation_key) {
300 require_network_isolation_key_ = require_network_isolation_key;
301 }
302 bool require_network_isolation_key() const {
303 return require_network_isolation_key_;
304 }
305
Stefano Duo44af16c2022-02-10 15:23:11306 // If != NetworkChangeNotifier::kInvalidNetworkHandle, the network which this
307 // context has been bound to.
308 NetworkChangeNotifier::NetworkHandle bound_network() const {
309 return bound_network_;
310 }
311 void set_bound_network(NetworkChangeNotifier::NetworkHandle network) {
312 bound_network_ = network;
313 }
314
gab47aa7da2017-06-02 16:09:43315 void AssertCalledOnValidThread() {
316 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
317 }
318
[email protected]f6c21cb2011-02-16 19:45:41319 private:
[email protected]f6c21cb2011-02-16 19:45:41320 // Ownership for these members are not defined here. Clients should either
321 // provide storage elsewhere or have a subclass take ownership.
Keishi Hattori0e45c022021-11-27 09:25:52322 raw_ptr<NetLog> net_log_;
323 raw_ptr<HostResolver> host_resolver_;
324 raw_ptr<CertVerifier> cert_verifier_;
325 raw_ptr<HttpAuthHandlerFactory> http_auth_handler_factory_;
326 raw_ptr<ProxyResolutionService> proxy_resolution_service_;
327 raw_ptr<ProxyDelegate> proxy_delegate_;
328 raw_ptr<SSLConfigService> ssl_config_service_;
329 raw_ptr<NetworkDelegate> network_delegate_;
330 raw_ptr<HttpServerProperties> http_server_properties_;
331 raw_ptr<const HttpUserAgentSettings> http_user_agent_settings_;
332 raw_ptr<CookieStore> cookie_store_;
333 raw_ptr<TransportSecurityState> transport_security_state_;
334 raw_ptr<CTPolicyEnforcer> ct_policy_enforcer_;
335 raw_ptr<SCTAuditingDelegate> sct_auditing_delegate_;
336 raw_ptr<HttpTransactionFactory> http_transaction_factory_;
337 raw_ptr<const URLRequestJobFactory> job_factory_;
338 raw_ptr<URLRequestThrottlerManager> throttler_manager_;
339 raw_ptr<QuicContext> quic_context_;
340 raw_ptr<NetworkQualityEstimator> network_quality_estimator_;
Julia Tuttle3f9acac2017-11-20 22:06:30341#if BUILDFLAG(ENABLE_REPORTING)
Keishi Hattori0e45c022021-11-27 09:25:52342 raw_ptr<ReportingService> reporting_service_;
343 raw_ptr<NetworkErrorLoggingService> network_error_logging_service_;
Julia Tuttle3f9acac2017-11-20 22:06:30344#endif // BUILDFLAG(ENABLE_REPORTING)
[email protected]70b92342010-10-12 05:54:06345
Helen Lif5e4a752018-01-02 16:03:02346 std::unique_ptr<std::set<const URLRequest*>> url_requests_;
[email protected]7c52ed92012-04-06 15:42:40347
maksim.sisov3d40c812016-05-02 13:27:16348 // Enables Brotli Content-Encoding support.
349 bool enable_brotli_;
mgershd21d6d142016-12-14 23:06:36350 // Enables checking system policy before allowing a cleartext http or ws
351 // request. Only used on Android.
352 bool check_cleartext_permitted_;
maksim.sisov3d40c812016-05-02 13:27:16353
Matt Menkeee10f0412020-05-14 14:16:03354 // Triggers a DCHECK if a NetworkIsolationKey/IsolationInfo is not provided to
355 // a request when true.
356 bool require_network_isolation_key_;
357
Stefano Duo44af16c2022-02-10 15:23:11358 NetworkChangeNotifier::NetworkHandle bound_network_;
359
gab47aa7da2017-06-02 16:09:43360 THREAD_CHECKER(thread_checker_);
initial.commit586acc5fe2008-07-26 22:42:52361};
362
[email protected]27a112c2011-01-06 04:19:30363} // namespace net
364
[email protected]43530b32008-08-04 22:21:34365#endif // NET_URL_REQUEST_URL_REQUEST_CONTEXT_H_