blob: 382b195b139e8259d9fcd56b03d6de9458c21365 [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"
Yuta Hijikata101ed2a2020-11-18 07:50:3918#include "build/chromeos_buildflags.h"
Matt Menked732ea42019-03-08 12:05:0019#include "net/base/http_user_agent_settings.h"
[email protected]aa84a7e2012-03-15 21:29:0620#include "net/cookies/cookie_store.h"
[email protected]f2cb3cf2013-03-21 01:40:5321#include "net/dns/host_resolver.h"
xunjielia0166f42017-02-23 17:44:5722#include "net/http/http_cache.h"
Matt Menke30a878c2021-07-20 22:25:0923#include "net/http/http_network_session.h"
[email protected]f6c21cb2011-02-16 19:45:4124#include "net/http/http_transaction_factory.h"
xunjieli9f8c5fb52016-12-07 22:59:3325#include "net/socket/ssl_client_socket_impl.h"
[email protected]7c52ed92012-04-06 15:42:4026#include "net/url_request/url_request.h"
[email protected]9349cfb2010-08-31 18:00:5327
[email protected]27a112c2011-01-06 04:19:3028namespace net {
29
Maks Orlovich9fdb7272019-12-13 01:28:2330URLRequestContext::URLRequestContext()
tbansalea2fb8c2015-05-22 22:23:0031 : net_log_(nullptr),
32 host_resolver_(nullptr),
33 cert_verifier_(nullptr),
tbansalea2fb8c2015-05-22 22:23:0034 http_auth_handler_factory_(nullptr),
Lily Houghton8c2f97d2018-01-22 05:06:5935 proxy_resolution_service_(nullptr),
Wojciech Dzierżanowski1f823562019-01-18 11:26:0036 proxy_delegate_(nullptr),
Ryan Sleevib8449e02018-07-15 04:31:0737 ssl_config_service_(nullptr),
tbansalea2fb8c2015-05-22 22:23:0038 network_delegate_(nullptr),
bnc525e175a2016-06-20 12:36:4039 http_server_properties_(nullptr),
tbansalea2fb8c2015-05-22 22:23:0040 http_user_agent_settings_(nullptr),
mmenke606c59c2016-03-07 18:20:5541 cookie_store_(nullptr),
tbansalea2fb8c2015-05-22 22:23:0042 transport_security_state_(nullptr),
rsleevid6de8302016-06-21 01:33:2043 ct_policy_enforcer_(nullptr),
Chris Thompsonf31b2492020-07-21 05:47:4244 sct_auditing_delegate_(nullptr),
tbansalea2fb8c2015-05-22 22:23:0045 http_transaction_factory_(nullptr),
46 job_factory_(nullptr),
47 throttler_manager_(nullptr),
Victor Vasiliev7752898d2019-11-14 21:30:2248 quic_context_(nullptr),
tbansalea2fb8c2015-05-22 22:23:0049 network_quality_estimator_(nullptr),
Julia Tuttle3f9acac2017-11-20 22:06:3050#if BUILDFLAG(ENABLE_REPORTING)
juliatuttle2b8dd2d12017-04-12 21:27:4151 reporting_service_(nullptr),
Julia Tuttlecba7d222018-02-23 19:37:2752 network_error_logging_service_(nullptr),
Julia Tuttle3f9acac2017-11-20 22:06:3053#endif // BUILDFLAG(ENABLE_REPORTING)
Helen Lif5e4a752018-01-02 16:03:0254 url_requests_(std::make_unique<std::set<const URLRequest*>>()),
mgershd21d6d142016-12-14 23:06:3655 enable_brotli_(false),
xunjielic4da1de2017-03-28 16:19:5056 check_cleartext_permitted_(false),
Kenichi Ishibashia92b9652021-09-10 23:43:4457 require_network_isolation_key_(false) {
xunjieli96ab36a72016-12-05 21:36:0558}
[email protected]f1d81922010-07-31 17:47:0959
[email protected]ef2bf422012-05-11 03:27:0960URLRequestContext::~URLRequestContext() {
gab47aa7da2017-06-02 16:09:4361 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
[email protected]ef2bf422012-05-11 03:27:0962 AssertNoURLRequests();
63}
64
Matt Menke30a878c2021-07-20 22:25:0965const HttpNetworkSessionParams* URLRequestContext::GetNetworkSessionParams()
66 const {
[email protected]c2dad292012-09-07 21:27:3567 HttpTransactionFactory* transaction_factory = http_transaction_factory();
68 if (!transaction_factory)
tbansalea2fb8c2015-05-22 22:23:0069 return nullptr;
[email protected]c2dad292012-09-07 21:27:3570 HttpNetworkSession* network_session = transaction_factory->GetSession();
71 if (!network_session)
tbansalea2fb8c2015-05-22 22:23:0072 return nullptr;
[email protected]c2dad292012-09-07 21:27:3573 return &network_session->params();
74}
75
Matt Menke30a878c2021-07-20 22:25:0976const HttpNetworkSessionContext* URLRequestContext::GetNetworkSessionContext()
mmenke6ddfbea2017-05-31 21:48:4177 const {
78 HttpTransactionFactory* transaction_factory = http_transaction_factory();
79 if (!transaction_factory)
80 return nullptr;
81 HttpNetworkSession* network_session = transaction_factory->GetSession();
82 if (!network_session)
83 return nullptr;
84 return &network_session->context();
85}
86
Yuta Hijikata101ed2a2020-11-18 07:50:3987// TODO(crbug.com/1052397): Revisit once build flag switch of lacros-chrome is
88// complete.
89#if !defined(OS_WIN) && !(defined(OS_LINUX) || BUILDFLAG(IS_CHROMEOS_LACROS))
danakj8522a25b2016-04-16 00:17:3690std::unique_ptr<URLRequest> URLRequestContext::CreateRequest(
[email protected]2ca01e52013-10-31 22:05:1991 const GURL& url,
92 RequestPriority priority,
davidben151423e2015-03-23 18:48:3693 URLRequest::Delegate* delegate) const {
Matt Menkefe9a9df2021-09-08 17:55:2494 return CreateRequest(url, priority, delegate, MISSING_TRAFFIC_ANNOTATION,
95 /*is_for_websockets=*/false);
[email protected]8a26ff62012-08-24 21:49:2096}
Nicolas Ouellet-Payeur6e833b52019-07-02 17:18:0897#endif
[email protected]8a26ff62012-08-24 21:49:2098
rhalavatia9b551d2017-02-09 12:03:0099std::unique_ptr<URLRequest> URLRequestContext::CreateRequest(
100 const GURL& url,
101 RequestPriority priority,
102 URLRequest::Delegate* delegate,
Matt Menkefe9a9df2021-09-08 17:55:24103 NetworkTrafficAnnotationTag traffic_annotation,
ainozakib2207272021-09-17 01:30:35104 bool is_for_websockets,
105 const absl::optional<uint32_t> net_log_source_id) const {
106 return base::WrapUnique(new URLRequest(url, priority, delegate, this,
107 traffic_annotation, is_for_websockets,
108 net_log_source_id));
rhalavatia9b551d2017-02-09 12:03:00109}
110
[email protected]d100e44f2011-01-26 22:47:11111void URLRequestContext::set_cookie_store(CookieStore* cookie_store) {
112 cookie_store_ = cookie_store;
113}
114
[email protected]1e714bba2012-04-10 17:01:05115void URLRequestContext::AssertNoURLRequests() const {
Helen Lif5e4a752018-01-02 16:03:02116 int num_requests = url_requests_->size();
[email protected]7c52ed92012-04-06 15:42:40117 if (num_requests != 0) {
118 // We're leaking URLRequests :( Dump the URL of the first one and record how
119 // many we leaked so we have an idea of how bad it is.
Helen Lif5e4a752018-01-02 16:03:02120 const URLRequest* request = *url_requests_->begin();
[email protected]eb4ecaca2012-05-04 01:05:03121 int load_flags = request->load_flags();
Lukasz Anforowicz68c21772018-01-13 03:42:44122 DEBUG_ALIAS_FOR_GURL(url_buf, request->url());
[email protected]7c52ed92012-04-06 15:42:40123 base::debug::Alias(&num_requests);
[email protected]eb4ecaca2012-05-04 01:05:03124 base::debug::Alias(&load_flags);
[email protected]1f5f8e22012-09-06 23:40:54125 CHECK(false) << "Leaked " << num_requests << " URLRequest(s). First URL: "
126 << request->url().spec().c_str() << ".";
[email protected]7c52ed92012-04-06 15:42:40127 }
[email protected]9349cfb2010-08-31 18:00:53128}
[email protected]2fb629202010-12-23 23:52:57129
[email protected]27a112c2011-01-06 04:19:30130} // namespace net