blob: 75a701f5704e17987b43232b5130953338ae6904 [file] [log] [blame]
[email protected]aa84a7e2012-03-15 21:29:061// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]f1d81922010-07-31 17:47:092// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "net/url_request/url_request_context.h"
6
xunjielid5debfc2017-02-24 15:55:497#include <inttypes.h>
ainozakib2207272021-09-17 01:30:358#include <stdint.h>
xunjielid5debfc2017-02-24 15:55:499
[email protected]18590f22011-07-29 16:41:2810#include "base/compiler_specific.h"
[email protected]7c52ed92012-04-06 15:42:4011#include "base/debug/alias.h"
danakj8522a25b2016-04-16 00:17:3612#include "base/memory/ptr_util.h"
Ilya Sherman0eb39802017-12-08 20:58:1813#include "base/metrics/histogram_functions.h"
xunjieli3fcbcc12017-04-26 15:51:1914#include "base/metrics/histogram_macros.h"
[email protected]4dc3ad4f2013-06-11 07:15:5015#include "base/strings/string_util.h"
xunjieli96ab36a72016-12-05 21:36:0516#include "base/strings/stringprintf.h"
fdoraya19b7702016-12-23 14:19:3117#include "base/threading/thread_task_runner_handle.h"
Xiaohan Wang2a6845b2022-01-08 04:40:5718#include "build/build_config.h"
Yuta Hijikata101ed2a2020-11-18 07:50:3919#include "build/chromeos_buildflags.h"
Matt Menked732ea42019-03-08 12:05:0020#include "net/base/http_user_agent_settings.h"
[email protected]aa84a7e2012-03-15 21:29:0621#include "net/cookies/cookie_store.h"
[email protected]f2cb3cf2013-03-21 01:40:5322#include "net/dns/host_resolver.h"
xunjielia0166f42017-02-23 17:44:5723#include "net/http/http_cache.h"
Matt Menke30a878c2021-07-20 22:25:0924#include "net/http/http_network_session.h"
[email protected]f6c21cb2011-02-16 19:45:4125#include "net/http/http_transaction_factory.h"
ainozaki7eefe912021-10-12 03:40:2626#include "net/log/net_log_source.h"
xunjieli9f8c5fb52016-12-07 22:59:3327#include "net/socket/ssl_client_socket_impl.h"
[email protected]7c52ed92012-04-06 15:42:4028#include "net/url_request/url_request.h"
[email protected]9349cfb2010-08-31 18:00:5329
[email protected]27a112c2011-01-06 04:19:3030namespace net {
31
Maks Orlovich9fdb7272019-12-13 01:28:2332URLRequestContext::URLRequestContext()
tbansalea2fb8c2015-05-22 22:23:0033 : net_log_(nullptr),
34 host_resolver_(nullptr),
35 cert_verifier_(nullptr),
tbansalea2fb8c2015-05-22 22:23:0036 http_auth_handler_factory_(nullptr),
Lily Houghton8c2f97d2018-01-22 05:06:5937 proxy_resolution_service_(nullptr),
Wojciech Dzierżanowski1f823562019-01-18 11:26:0038 proxy_delegate_(nullptr),
Ryan Sleevib8449e02018-07-15 04:31:0739 ssl_config_service_(nullptr),
tbansalea2fb8c2015-05-22 22:23:0040 network_delegate_(nullptr),
bnc525e175a2016-06-20 12:36:4041 http_server_properties_(nullptr),
tbansalea2fb8c2015-05-22 22:23:0042 http_user_agent_settings_(nullptr),
mmenke606c59c2016-03-07 18:20:5543 cookie_store_(nullptr),
tbansalea2fb8c2015-05-22 22:23:0044 transport_security_state_(nullptr),
rsleevid6de8302016-06-21 01:33:2045 ct_policy_enforcer_(nullptr),
Chris Thompsonf31b2492020-07-21 05:47:4246 sct_auditing_delegate_(nullptr),
tbansalea2fb8c2015-05-22 22:23:0047 http_transaction_factory_(nullptr),
48 job_factory_(nullptr),
49 throttler_manager_(nullptr),
Victor Vasiliev7752898d2019-11-14 21:30:2250 quic_context_(nullptr),
tbansalea2fb8c2015-05-22 22:23:0051 network_quality_estimator_(nullptr),
Julia Tuttle3f9acac2017-11-20 22:06:3052#if BUILDFLAG(ENABLE_REPORTING)
juliatuttle2b8dd2d12017-04-12 21:27:4153 reporting_service_(nullptr),
Julia Tuttlecba7d222018-02-23 19:37:2754 network_error_logging_service_(nullptr),
Julia Tuttle3f9acac2017-11-20 22:06:3055#endif // BUILDFLAG(ENABLE_REPORTING)
Helen Lif5e4a752018-01-02 16:03:0256 url_requests_(std::make_unique<std::set<const URLRequest*>>()),
mgershd21d6d142016-12-14 23:06:3657 enable_brotli_(false),
xunjielic4da1de2017-03-28 16:19:5058 check_cleartext_permitted_(false),
Stefano Duo44af16c2022-02-10 15:23:1159 require_network_isolation_key_(false),
60 bound_network_(NetworkChangeNotifier::kInvalidNetworkHandle) {
xunjieli96ab36a72016-12-05 21:36:0561}
[email protected]f1d81922010-07-31 17:47:0962
[email protected]ef2bf422012-05-11 03:27:0963URLRequestContext::~URLRequestContext() {
gab47aa7da2017-06-02 16:09:4364 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
[email protected]ef2bf422012-05-11 03:27:0965 AssertNoURLRequests();
66}
67
Matt Menke30a878c2021-07-20 22:25:0968const HttpNetworkSessionParams* URLRequestContext::GetNetworkSessionParams()
69 const {
[email protected]c2dad292012-09-07 21:27:3570 HttpTransactionFactory* transaction_factory = http_transaction_factory();
71 if (!transaction_factory)
tbansalea2fb8c2015-05-22 22:23:0072 return nullptr;
[email protected]c2dad292012-09-07 21:27:3573 HttpNetworkSession* network_session = transaction_factory->GetSession();
74 if (!network_session)
tbansalea2fb8c2015-05-22 22:23:0075 return nullptr;
[email protected]c2dad292012-09-07 21:27:3576 return &network_session->params();
77}
78
Matt Menke30a878c2021-07-20 22:25:0979const HttpNetworkSessionContext* URLRequestContext::GetNetworkSessionContext()
mmenke6ddfbea2017-05-31 21:48:4180 const {
81 HttpTransactionFactory* transaction_factory = http_transaction_factory();
82 if (!transaction_factory)
83 return nullptr;
84 HttpNetworkSession* network_session = transaction_factory->GetSession();
85 if (!network_session)
86 return nullptr;
87 return &network_session->context();
88}
89
Yuta Hijikata101ed2a2020-11-18 07:50:3990// TODO(crbug.com/1052397): Revisit once build flag switch of lacros-chrome is
91// complete.
Xiaohan Wang2a6845b2022-01-08 04:40:5792#if !BUILDFLAG(IS_WIN) && \
93 !(BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS_LACROS))
danakj8522a25b2016-04-16 00:17:3694std::unique_ptr<URLRequest> URLRequestContext::CreateRequest(
[email protected]2ca01e52013-10-31 22:05:1995 const GURL& url,
96 RequestPriority priority,
davidben151423e2015-03-23 18:48:3697 URLRequest::Delegate* delegate) const {
Matt Menkefe9a9df2021-09-08 17:55:2498 return CreateRequest(url, priority, delegate, MISSING_TRAFFIC_ANNOTATION,
99 /*is_for_websockets=*/false);
[email protected]8a26ff62012-08-24 21:49:20100}
Nicolas Ouellet-Payeur6e833b52019-07-02 17:18:08101#endif
[email protected]8a26ff62012-08-24 21:49:20102
rhalavatia9b551d2017-02-09 12:03:00103std::unique_ptr<URLRequest> URLRequestContext::CreateRequest(
104 const GURL& url,
105 RequestPriority priority,
106 URLRequest::Delegate* delegate,
Matt Menkefe9a9df2021-09-08 17:55:24107 NetworkTrafficAnnotationTag traffic_annotation,
ainozakib2207272021-09-17 01:30:35108 bool is_for_websockets,
ainozaki7eefe912021-10-12 03:40:26109 const absl::optional<net::NetLogSource> net_log_source) const {
ainozakib2207272021-09-17 01:30:35110 return base::WrapUnique(new URLRequest(url, priority, delegate, this,
111 traffic_annotation, is_for_websockets,
ainozaki7eefe912021-10-12 03:40:26112 net_log_source));
rhalavatia9b551d2017-02-09 12:03:00113}
114
[email protected]d100e44f2011-01-26 22:47:11115void URLRequestContext::set_cookie_store(CookieStore* cookie_store) {
116 cookie_store_ = cookie_store;
117}
118
[email protected]1e714bba2012-04-10 17:01:05119void URLRequestContext::AssertNoURLRequests() const {
Helen Lif5e4a752018-01-02 16:03:02120 int num_requests = url_requests_->size();
[email protected]7c52ed92012-04-06 15:42:40121 if (num_requests != 0) {
122 // We're leaking URLRequests :( Dump the URL of the first one and record how
123 // many we leaked so we have an idea of how bad it is.
Helen Lif5e4a752018-01-02 16:03:02124 const URLRequest* request = *url_requests_->begin();
[email protected]eb4ecaca2012-05-04 01:05:03125 int load_flags = request->load_flags();
Lukasz Anforowicz68c21772018-01-13 03:42:44126 DEBUG_ALIAS_FOR_GURL(url_buf, request->url());
[email protected]7c52ed92012-04-06 15:42:40127 base::debug::Alias(&num_requests);
[email protected]eb4ecaca2012-05-04 01:05:03128 base::debug::Alias(&load_flags);
[email protected]1f5f8e22012-09-06 23:40:54129 CHECK(false) << "Leaked " << num_requests << " URLRequest(s). First URL: "
130 << request->url().spec().c_str() << ".";
[email protected]7c52ed92012-04-06 15:42:40131 }
[email protected]9349cfb2010-08-31 18:00:53132}
[email protected]2fb629202010-12-23 23:52:57133
[email protected]27a112c2011-01-06 04:19:30134} // namespace net