blob: 0c0847c8b561ebc25d19e23fa382bcecfdabd5c2 [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
Yutaka Hirano53c9e852022-03-29 18:13:3132URLRequestContext::URLRequestContext(
33 base::PassKey<URLRequestContextBuilder> pass_key)
tbansalea2fb8c2015-05-22 22:23:0034 : net_log_(nullptr),
35 host_resolver_(nullptr),
36 cert_verifier_(nullptr),
tbansalea2fb8c2015-05-22 22:23:0037 http_auth_handler_factory_(nullptr),
Lily Houghton8c2f97d2018-01-22 05:06:5938 proxy_resolution_service_(nullptr),
Wojciech Dzierżanowski1f823562019-01-18 11:26:0039 proxy_delegate_(nullptr),
Ryan Sleevib8449e02018-07-15 04:31:0740 ssl_config_service_(nullptr),
tbansalea2fb8c2015-05-22 22:23:0041 network_delegate_(nullptr),
bnc525e175a2016-06-20 12:36:4042 http_server_properties_(nullptr),
tbansalea2fb8c2015-05-22 22:23:0043 http_user_agent_settings_(nullptr),
mmenke606c59c2016-03-07 18:20:5544 cookie_store_(nullptr),
tbansalea2fb8c2015-05-22 22:23:0045 transport_security_state_(nullptr),
rsleevid6de8302016-06-21 01:33:2046 ct_policy_enforcer_(nullptr),
Chris Thompsonf31b2492020-07-21 05:47:4247 sct_auditing_delegate_(nullptr),
tbansalea2fb8c2015-05-22 22:23:0048 http_transaction_factory_(nullptr),
49 job_factory_(nullptr),
50 throttler_manager_(nullptr),
Victor Vasiliev7752898d2019-11-14 21:30:2251 quic_context_(nullptr),
tbansalea2fb8c2015-05-22 22:23:0052 network_quality_estimator_(nullptr),
Julia Tuttle3f9acac2017-11-20 22:06:3053#if BUILDFLAG(ENABLE_REPORTING)
juliatuttle2b8dd2d12017-04-12 21:27:4154 reporting_service_(nullptr),
Julia Tuttlecba7d222018-02-23 19:37:2755 network_error_logging_service_(nullptr),
Julia Tuttle3f9acac2017-11-20 22:06:3056#endif // BUILDFLAG(ENABLE_REPORTING)
Helen Lif5e4a752018-01-02 16:03:0257 url_requests_(std::make_unique<std::set<const URLRequest*>>()),
Stefano Duo44af16c2022-02-10 15:23:1158 bound_network_(NetworkChangeNotifier::kInvalidNetworkHandle) {
xunjieli96ab36a72016-12-05 21:36:0559}
[email protected]f1d81922010-07-31 17:47:0960
[email protected]ef2bf422012-05-11 03:27:0961URLRequestContext::~URLRequestContext() {
gab47aa7da2017-06-02 16:09:4362 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
[email protected]ef2bf422012-05-11 03:27:0963 AssertNoURLRequests();
64}
65
Matt Menke30a878c2021-07-20 22:25:0966const HttpNetworkSessionParams* URLRequestContext::GetNetworkSessionParams()
67 const {
[email protected]c2dad292012-09-07 21:27:3568 HttpTransactionFactory* transaction_factory = http_transaction_factory();
69 if (!transaction_factory)
tbansalea2fb8c2015-05-22 22:23:0070 return nullptr;
[email protected]c2dad292012-09-07 21:27:3571 HttpNetworkSession* network_session = transaction_factory->GetSession();
72 if (!network_session)
tbansalea2fb8c2015-05-22 22:23:0073 return nullptr;
[email protected]c2dad292012-09-07 21:27:3574 return &network_session->params();
75}
76
Matt Menke30a878c2021-07-20 22:25:0977const HttpNetworkSessionContext* URLRequestContext::GetNetworkSessionContext()
mmenke6ddfbea2017-05-31 21:48:4178 const {
79 HttpTransactionFactory* transaction_factory = http_transaction_factory();
80 if (!transaction_factory)
81 return nullptr;
82 HttpNetworkSession* network_session = transaction_factory->GetSession();
83 if (!network_session)
84 return nullptr;
85 return &network_session->context();
86}
87
Yuta Hijikata101ed2a2020-11-18 07:50:3988// TODO(crbug.com/1052397): Revisit once build flag switch of lacros-chrome is
89// complete.
Xiaohan Wang2a6845b2022-01-08 04:40:5790#if !BUILDFLAG(IS_WIN) && \
91 !(BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS_LACROS))
danakj8522a25b2016-04-16 00:17:3692std::unique_ptr<URLRequest> URLRequestContext::CreateRequest(
[email protected]2ca01e52013-10-31 22:05:1993 const GURL& url,
94 RequestPriority priority,
davidben151423e2015-03-23 18:48:3695 URLRequest::Delegate* delegate) const {
Matt Menkefe9a9df2021-09-08 17:55:2496 return CreateRequest(url, priority, delegate, MISSING_TRAFFIC_ANNOTATION,
97 /*is_for_websockets=*/false);
[email protected]8a26ff62012-08-24 21:49:2098}
Nicolas Ouellet-Payeur6e833b52019-07-02 17:18:0899#endif
[email protected]8a26ff62012-08-24 21:49:20100
rhalavatia9b551d2017-02-09 12:03:00101std::unique_ptr<URLRequest> URLRequestContext::CreateRequest(
102 const GURL& url,
103 RequestPriority priority,
104 URLRequest::Delegate* delegate,
Matt Menkefe9a9df2021-09-08 17:55:24105 NetworkTrafficAnnotationTag traffic_annotation,
ainozakib2207272021-09-17 01:30:35106 bool is_for_websockets,
ainozaki7eefe912021-10-12 03:40:26107 const absl::optional<net::NetLogSource> net_log_source) const {
ainozakib2207272021-09-17 01:30:35108 return base::WrapUnique(new URLRequest(url, priority, delegate, this,
109 traffic_annotation, is_for_websockets,
ainozaki7eefe912021-10-12 03:40:26110 net_log_source));
rhalavatia9b551d2017-02-09 12:03:00111}
112
[email protected]d100e44f2011-01-26 22:47:11113void URLRequestContext::set_cookie_store(CookieStore* cookie_store) {
114 cookie_store_ = cookie_store;
115}
116
[email protected]1e714bba2012-04-10 17:01:05117void URLRequestContext::AssertNoURLRequests() const {
Helen Lif5e4a752018-01-02 16:03:02118 int num_requests = url_requests_->size();
[email protected]7c52ed92012-04-06 15:42:40119 if (num_requests != 0) {
120 // We're leaking URLRequests :( Dump the URL of the first one and record how
121 // many we leaked so we have an idea of how bad it is.
Helen Lif5e4a752018-01-02 16:03:02122 const URLRequest* request = *url_requests_->begin();
[email protected]eb4ecaca2012-05-04 01:05:03123 int load_flags = request->load_flags();
Lukasz Anforowicz68c21772018-01-13 03:42:44124 DEBUG_ALIAS_FOR_GURL(url_buf, request->url());
[email protected]7c52ed92012-04-06 15:42:40125 base::debug::Alias(&num_requests);
[email protected]eb4ecaca2012-05-04 01:05:03126 base::debug::Alias(&load_flags);
[email protected]1f5f8e22012-09-06 23:40:54127 CHECK(false) << "Leaked " << num_requests << " URLRequest(s). First URL: "
128 << request->url().spec().c_str() << ".";
[email protected]7c52ed92012-04-06 15:42:40129 }
[email protected]9349cfb2010-08-31 18:00:53130}
[email protected]2fb629202010-12-23 23:52:57131
[email protected]27a112c2011-01-06 04:19:30132} // namespace net