1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This class represents contextual information (cookies, cache, etc.)
// that's necessary when processing resource requests.
#ifndef NET_URL_REQUEST_URL_REQUEST_CONTEXT_H_
#define NET_URL_REQUEST_URL_REQUEST_CONTEXT_H_
#include <stdint.h>
#include <memory>
#include <optional>
#include <set>
#include <string>
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/threading/thread_checker.h"
#include "base/types/pass_key.h"
#include "build/build_config.h"
#include "net/base/net_export.h"
#include "net/base/network_handle.h"
#include "net/base/request_priority.h"
#include "net/log/net_log_source.h"
#include "net/net_buildflags.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "net/url_request/url_request.h"
namespace net {
class CertVerifier;
class ClientSocketFactory;
class CookieStore;
class HostResolver;
class HttpAuthHandlerFactory;
class HttpNetworkSession;
struct HttpNetworkSessionContext;
struct HttpNetworkSessionParams;
class HttpServerProperties;
class HttpTransactionFactory;
class HttpUserAgentSettings;
class NetLog;
class NetworkDelegate;
class NetworkQualityEstimator;
class ProxyDelegate;
class ProxyResolutionService;
class QuicContext;
class SCTAuditingDelegate;
class SSLConfigService;
class TransportSecurityPersister;
class TransportSecurityState;
class URLRequest;
class URLRequestJobFactory;
class URLRequestContextBuilder;
#if BUILDFLAG(ENABLE_REPORTING)
class NetworkErrorLoggingService;
class PersistentReportingAndNelStore;
class ReportingService;
#endif // BUILDFLAG(ENABLE_REPORTING)
namespace device_bound_sessions {
class SessionService;
class SessionStore;
}
// Class that provides application-specific context for URLRequest
// instances. May only be created by URLRequestContextBuilder.
// Owns most of its member variables, except a few that may be shared
// with other contexts.
class NET_EXPORT URLRequestContext final {
public:
// URLRequestContext must be created by URLRequestContextBuilder.
explicit URLRequestContext(base::PassKey<URLRequestContextBuilder> pass_key);
URLRequestContext(const URLRequestContext&) = delete;
URLRequestContext& operator=(const URLRequestContext&) = delete;
~URLRequestContext();
// May return nullptr if this context doesn't have an associated network
// session.
const HttpNetworkSessionParams* GetNetworkSessionParams() const;
// May return nullptr if this context doesn't have an associated network
// session.
const HttpNetworkSessionContext* GetNetworkSessionContext() const;
#if !BUILDFLAG(IS_WIN) && !BUILDFLAG(IS_LINUX)
// This function should not be used in Chromium, please use the version with
// NetworkTrafficAnnotationTag in the future.
//
// The unannotated method is not available on desktop Linux + Windows. It's
// available on other platforms, since we only audit network annotations on
// Linux & Windows.
std::unique_ptr<URLRequest> CreateRequest(
const GURL& url,
RequestPriority priority,
URLRequest::Delegate* delegate) const;
#endif
// `traffic_annotation` is metadata about the network traffic send via this
// URLRequest, see net::DefineNetworkTrafficAnnotation. Note that:
// - net provides the API for tagging requests with an opaque identifier.
// - chrome/browser/privacy/traffic_annotation.proto contains the Chrome
// specific .proto describing the verbose annotation format that Chrome's
// callsites are expected to follow.
// - tools/traffic_annotation/ contains sample and template for annotation and
// tools will be added for verification following crbug.com/690323.
//
// `is_for_websockets` should be true iff this was created for use by a
// websocket. HTTP/HTTPS requests fail if it's true, and WS/WSS requests fail
// if it's false. This is to protect against broken consumers.
//
// `net_log_source_id` is used to construct NetLogWithSource using the
// specified Source ID. This method is expected to be used when URLRequest
// wants to take over existing NetLogSource.
std::unique_ptr<URLRequest> CreateRequest(
const GURL& url,
RequestPriority priority,
URLRequest::Delegate* delegate,
NetworkTrafficAnnotationTag traffic_annotation,
bool is_for_websockets = false,
const std::optional<net::NetLogSource> net_log_source =
std::nullopt) const;
NetLog* net_log() const { return net_log_; }
HostResolver* host_resolver() const { return host_resolver_.get(); }
CertVerifier* cert_verifier() const { return cert_verifier_.get(); }
// Get the proxy service for this context.
ProxyResolutionService* proxy_resolution_service() const {
return proxy_resolution_service_.get();
}
ProxyDelegate* proxy_delegate() const { return proxy_delegate_.get(); }
// Get the ssl config service for this context.
SSLConfigService* ssl_config_service() const {
return ssl_config_service_.get();
}
// Gets the HTTP Authentication Handler Factory for this context.
// The factory is only valid for the lifetime of this URLRequestContext
HttpAuthHandlerFactory* http_auth_handler_factory() const {
return http_auth_handler_factory_.get();
}
// Gets the http transaction factory for this context.
HttpTransactionFactory* http_transaction_factory() const {
return http_transaction_factory_.get();
}
NetworkDelegate* network_delegate() const { return network_delegate_.get(); }
HttpServerProperties* http_server_properties() const {
return http_server_properties_.get();
}
// Gets the cookie store for this context (may be null, in which case
// cookies are not stored).
CookieStore* cookie_store() const { return cookie_store_.get(); }
TransportSecurityState* transport_security_state() const {
return transport_security_state_.get();
}
SCTAuditingDelegate* sct_auditing_delegate() const {
return sct_auditing_delegate_.get();
}
const URLRequestJobFactory* job_factory() const { return job_factory_; }
QuicContext* quic_context() const { return quic_context_.get(); }
// Gets the URLRequest objects that hold a reference to this
// URLRequestContext.
std::set<raw_ptr<const URLRequest, SetExperimental>>* url_requests() const {
return url_requests_.get();
}
// CHECKs that no URLRequests using this context remain. Subclasses should
// additionally call AssertNoURLRequests() within their own destructor,
// prior to implicit destruction of subclass-owned state.
void AssertNoURLRequests() const;
// Get the underlying |HttpUserAgentSettings| implementation that provides
// the HTTP Accept-Language and User-Agent header values.
const HttpUserAgentSettings* http_user_agent_settings() const {
return http_user_agent_settings_.get();
}
// Gets the NetworkQualityEstimator associated with this context.
// May return nullptr.
NetworkQualityEstimator* network_quality_estimator() const {
return network_quality_estimator_.get();
}
#if BUILDFLAG(ENABLE_REPORTING)
ReportingService* reporting_service() const {
return reporting_service_.get();
}
NetworkErrorLoggingService* network_error_logging_service() const {
return network_error_logging_service_.get();
}
#endif // BUILDFLAG(ENABLE_REPORTING)
// May return nullptr if the feature is disabled.
device_bound_sessions::SessionStore* device_bound_session_store() const {
#if BUILDFLAG(ENABLE_DEVICE_BOUND_SESSIONS)
return device_bound_session_store_.get();
#else
return nullptr;
#endif
}
// May return nullptr if the feature is disabled.
device_bound_sessions::SessionService* device_bound_session_service() const {
#if BUILDFLAG(ENABLE_DEVICE_BOUND_SESSIONS)
return device_bound_session_service_.get();
#else
return nullptr;
#endif
}
bool enable_brotli() const { return enable_brotli_; }
bool enable_zstd() const { return enable_zstd_; }
// Returns current value of the |check_cleartext_permitted| flag.
bool check_cleartext_permitted() const { return check_cleartext_permitted_; }
bool require_network_anonymization_key() const {
return require_network_anonymization_key_;
}
// If != handles::kInvalidNetworkHandle, the network which this
// context has been bound to.
handles::NetworkHandle bound_network() const { return bound_network_; }
void AssertCalledOnValidThread() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
}
// DEPRECATED: Do not use this even in tests. This is for a legacy use.
void SetJobFactoryForTesting(const URLRequestJobFactory* job_factory) {
job_factory_ = job_factory;
}
const std::optional<std::string>& cookie_deprecation_label() const {
return cookie_deprecation_label_;
}
void set_cookie_deprecation_label(const std::optional<std::string>& label) {
cookie_deprecation_label_ = label;
}
private:
friend class URLRequestContextBuilder;
HttpNetworkSession* http_network_session() const {
return http_network_session_.get();
}
void set_net_log(NetLog* net_log);
void set_host_resolver(std::unique_ptr<HostResolver> host_resolver);
void set_cert_verifier(std::unique_ptr<CertVerifier> cert_verifier);
void set_proxy_resolution_service(
std::unique_ptr<ProxyResolutionService> proxy_resolution_service);
void set_proxy_delegate(std::unique_ptr<ProxyDelegate> proxy_delegate);
void set_ssl_config_service(std::unique_ptr<SSLConfigService> service);
void set_http_auth_handler_factory(
std::unique_ptr<HttpAuthHandlerFactory> factory);
void set_http_network_session(
std::unique_ptr<HttpNetworkSession> http_network_session);
void set_http_transaction_factory(
std::unique_ptr<HttpTransactionFactory> factory);
void set_network_delegate(std::unique_ptr<NetworkDelegate> network_delegate);
void set_http_server_properties(
std::unique_ptr<HttpServerProperties> http_server_properties);
void set_cookie_store(std::unique_ptr<CookieStore> cookie_store);
void set_transport_security_state(
std::unique_ptr<TransportSecurityState> state);
void set_sct_auditing_delegate(std::unique_ptr<SCTAuditingDelegate> delegate);
void set_job_factory(std::unique_ptr<const URLRequestJobFactory> job_factory);
void set_quic_context(std::unique_ptr<QuicContext> quic_context);
void set_http_user_agent_settings(
std::unique_ptr<const HttpUserAgentSettings> http_user_agent_settings);
void set_network_quality_estimator(
NetworkQualityEstimator* network_quality_estimator);
void set_client_socket_factory(
std::unique_ptr<ClientSocketFactory> client_socket_factory);
#if BUILDFLAG(ENABLE_REPORTING)
void set_persistent_reporting_and_nel_store(
std::unique_ptr<PersistentReportingAndNelStore>
persistent_reporting_and_nel_store);
void set_reporting_service(
std::unique_ptr<ReportingService> reporting_service);
void set_network_error_logging_service(
std::unique_ptr<NetworkErrorLoggingService>
network_error_logging_service);
#endif // BUILDFLAG(ENABLE_REPORTING)
void set_enable_brotli(bool enable_brotli) { enable_brotli_ = enable_brotli; }
void set_enable_zstd(bool enable_zstd) { enable_zstd_ = enable_zstd; }
void set_check_cleartext_permitted(bool check_cleartext_permitted) {
check_cleartext_permitted_ = check_cleartext_permitted;
}
void set_require_network_anonymization_key(
bool require_network_anonymization_key) {
require_network_anonymization_key_ = require_network_anonymization_key;
}
void set_bound_network(handles::NetworkHandle network) {
bound_network_ = network;
}
void set_transport_security_persister(
std::unique_ptr<TransportSecurityPersister> transport_security_persister);
raw_ptr<NetLog> net_log_ = nullptr;
#if BUILDFLAG(ENABLE_DEVICE_BOUND_SESSIONS)
void set_device_bound_session_store(
std::unique_ptr<device_bound_sessions::SessionStore>
device_bound_session_store);
void set_device_bound_session_service(
std::unique_ptr<device_bound_sessions::SessionService>
device_bound_session_service);
#endif // BUILDFLAG(ENABLE_DEVICE_BOUND_SESSIONS)
std::unique_ptr<HostResolver> host_resolver_;
std::unique_ptr<CertVerifier> cert_verifier_;
std::unique_ptr<HttpAuthHandlerFactory> http_auth_handler_factory_;
std::unique_ptr<NetworkDelegate> network_delegate_;
// `proxy_resolution_service_` may store a pointer to `proxy_delegate_`, so
// ensure that the latter outlives the former.
std::unique_ptr<ProxyDelegate> proxy_delegate_;
std::unique_ptr<ProxyResolutionService> proxy_resolution_service_;
std::unique_ptr<SSLConfigService> ssl_config_service_;
std::unique_ptr<HttpServerProperties> http_server_properties_;
std::unique_ptr<const HttpUserAgentSettings> http_user_agent_settings_;
std::unique_ptr<CookieStore> cookie_store_;
std::unique_ptr<TransportSecurityState> transport_security_state_;
std::unique_ptr<SCTAuditingDelegate> sct_auditing_delegate_;
std::unique_ptr<QuicContext> quic_context_;
std::unique_ptr<ClientSocketFactory> client_socket_factory_;
// The storage duplication for URLRequestJobFactory is needed because of
// SetJobFactoryForTesting. Once this method is removable, we can only store a
// unique_ptr similarly to the other fields.
std::unique_ptr<const URLRequestJobFactory> job_factory_storage_;
raw_ptr<const URLRequestJobFactory> job_factory_ = nullptr;
#if BUILDFLAG(ENABLE_REPORTING)
// Must precede |reporting_service_| and |network_error_logging_service_|
std::unique_ptr<PersistentReportingAndNelStore>
persistent_reporting_and_nel_store_;
std::unique_ptr<ReportingService> reporting_service_;
std::unique_ptr<NetworkErrorLoggingService> network_error_logging_service_;
#endif // BUILDFLAG(ENABLE_REPORTING)
// May be used (but not owned) by the HttpTransactionFactory.
std::unique_ptr<HttpNetworkSession> http_network_session_;
// `http_transaction_factory_` might hold a raw pointer on
// `http_network_session_` so it needs to be declared last.
std::unique_ptr<HttpTransactionFactory> http_transaction_factory_;
raw_ptr<NetworkQualityEstimator> network_quality_estimator_ = nullptr;
std::unique_ptr<TransportSecurityPersister> transport_security_persister_;
std::unique_ptr<std::set<raw_ptr<const URLRequest, SetExperimental>>>
url_requests_;
#if BUILDFLAG(ENABLE_DEVICE_BOUND_SESSIONS)
std::unique_ptr<device_bound_sessions::SessionStore>
device_bound_session_store_;
std::unique_ptr<device_bound_sessions::SessionService>
device_bound_session_service_;
#endif // BUILDFLAG(ENABLE_DEVICE_BOUND_SESSIONS)
// Enables Brotli Content-Encoding support.
bool enable_brotli_ = false;
// Enables Zstd Content-Encoding support.
bool enable_zstd_ = false;
// Enables checking system policy before allowing a cleartext http or ws
// request. Only used on Android.
bool check_cleartext_permitted_ = false;
// Triggers a DCHECK if a NetworkAnonymizationKey/IsolationInfo is not
// provided to a request when true.
bool require_network_anonymization_key_ = false;
std::optional<std::string> cookie_deprecation_label_;
handles::NetworkHandle bound_network_;
THREAD_CHECKER(thread_checker_);
};
} // namespace net
#endif // NET_URL_REQUEST_URL_REQUEST_CONTEXT_H_
|