blob: ee4850aa11c8a6a10db9306151f6576f5ce4324d [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>
8
[email protected]18590f22011-07-29 16:41:289#include "base/compiler_specific.h"
[email protected]7c52ed92012-04-06 15:42:4010#include "base/debug/alias.h"
danakj8522a25b2016-04-16 00:17:3611#include "base/memory/ptr_util.h"
Ilya Sherman0eb39802017-12-08 20:58:1812#include "base/metrics/histogram_functions.h"
xunjieli3fcbcc12017-04-26 15:51:1913#include "base/metrics/histogram_macros.h"
[email protected]4dc3ad4f2013-06-11 07:15:5014#include "base/strings/string_util.h"
xunjieli96ab36a72016-12-05 21:36:0515#include "base/strings/stringprintf.h"
fdoraya19b7702016-12-23 14:19:3116#include "base/threading/thread_task_runner_handle.h"
xunjieli96ab36a72016-12-05 21:36:0517#include "base/trace_event/memory_allocator_dump.h"
18#include "base/trace_event/memory_dump_manager.h"
xunjielid5debfc2017-02-24 15:55:4919#include "base/trace_event/memory_dump_request_args.h"
xunjieli96ab36a72016-12-05 21:36:0520#include "base/trace_event/process_memory_dump.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"
[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]ee4c30d2012-11-07 15:08:4326#include "net/url_request/http_user_agent_settings.h"
[email protected]7c52ed92012-04-06 15:42:4027#include "net/url_request/url_request.h"
[email protected]9349cfb2010-08-31 18:00:5328
[email protected]27a112c2011-01-06 04:19:3029namespace net {
30
[email protected]9349cfb2010-08-31 18:00:5331URLRequestContext::URLRequestContext()
tbansalea2fb8c2015-05-22 22:23:0032 : net_log_(nullptr),
33 host_resolver_(nullptr),
34 cert_verifier_(nullptr),
35 channel_id_service_(nullptr),
tbansalea2fb8c2015-05-22 22:23:0036 http_auth_handler_factory_(nullptr),
Lily Houghton8c2f97d2018-01-22 05:06:5937 proxy_resolution_service_(nullptr),
Ryan Sleevib8449e02018-07-15 04:31:0738 ssl_config_service_(nullptr),
tbansalea2fb8c2015-05-22 22:23:0039 network_delegate_(nullptr),
bnc525e175a2016-06-20 12:36:4040 http_server_properties_(nullptr),
tbansalea2fb8c2015-05-22 22:23:0041 http_user_agent_settings_(nullptr),
mmenke606c59c2016-03-07 18:20:5542 cookie_store_(nullptr),
tbansalea2fb8c2015-05-22 22:23:0043 transport_security_state_(nullptr),
44 cert_transparency_verifier_(nullptr),
rsleevid6de8302016-06-21 01:33:2045 ct_policy_enforcer_(nullptr),
tbansalea2fb8c2015-05-22 22:23:0046 http_transaction_factory_(nullptr),
47 job_factory_(nullptr),
48 throttler_manager_(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),
Helen Lif5e4a752018-01-02 16:03:0257 name_("unknown") {
xunjieli96ab36a72016-12-05 21:36:0558 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
59 this, "URLRequestContext", base::ThreadTaskRunnerHandle::Get());
60}
[email protected]f1d81922010-07-31 17:47:0961
[email protected]ef2bf422012-05-11 03:27:0962URLRequestContext::~URLRequestContext() {
gab47aa7da2017-06-02 16:09:4363 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
[email protected]ef2bf422012-05-11 03:27:0964 AssertNoURLRequests();
xunjieli96ab36a72016-12-05 21:36:0565 base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider(
66 this);
[email protected]ef2bf422012-05-11 03:27:0967}
68
[email protected]fa22a6f12012-05-11 17:19:5969void URLRequestContext::CopyFrom(const URLRequestContext* other) {
[email protected]d9696672011-03-15 22:45:0970 // Copy URLRequestContext parameters.
[email protected]fa22a6f12012-05-11 17:19:5971 set_net_log(other->net_log_);
72 set_host_resolver(other->host_resolver_);
73 set_cert_verifier(other->cert_verifier_);
[email protected]6b8a3c742014-07-25 00:25:3574 set_channel_id_service(other->channel_id_service_);
[email protected]fa22a6f12012-05-11 17:19:5975 set_http_auth_handler_factory(other->http_auth_handler_factory_);
Lily Houghton8c2f97d2018-01-22 05:06:5976 set_proxy_resolution_service(other->proxy_resolution_service_);
Ryan Sleevib8449e02018-07-15 04:31:0777 set_ssl_config_service(other->ssl_config_service_);
[email protected]fa22a6f12012-05-11 17:19:5978 set_network_delegate(other->network_delegate_);
79 set_http_server_properties(other->http_server_properties_);
mmenke606c59c2016-03-07 18:20:5580 set_cookie_store(other->cookie_store_);
[email protected]fa22a6f12012-05-11 17:19:5981 set_transport_security_state(other->transport_security_state_);
[email protected]284303b62013-11-28 15:11:5482 set_cert_transparency_verifier(other->cert_transparency_verifier_);
rsleevid6de8302016-06-21 01:33:2083 set_ct_policy_enforcer(other->ct_policy_enforcer_);
[email protected]fa22a6f12012-05-11 17:19:5984 set_http_transaction_factory(other->http_transaction_factory_);
[email protected]fa22a6f12012-05-11 17:19:5985 set_job_factory(other->job_factory_);
86 set_throttler_manager(other->throttler_manager_);
[email protected]ee4c30d2012-11-07 15:08:4387 set_http_user_agent_settings(other->http_user_agent_settings_);
tbansalea2fb8c2015-05-22 22:23:0088 set_network_quality_estimator(other->network_quality_estimator_);
Julia Tuttle3f9acac2017-11-20 22:06:3089#if BUILDFLAG(ENABLE_REPORTING)
juliatuttle2b8dd2d12017-04-12 21:27:4190 set_reporting_service(other->reporting_service_);
Julia Tuttlecba7d222018-02-23 19:37:2791 set_network_error_logging_service(other->network_error_logging_service_);
Julia Tuttle3f9acac2017-11-20 22:06:3092#endif // BUILDFLAG(ENABLE_REPORTING)
maksim.sisov3d40c812016-05-02 13:27:1693 set_enable_brotli(other->enable_brotli_);
mgershd21d6d142016-12-14 23:06:3694 set_check_cleartext_permitted(other->check_cleartext_permitted_);
[email protected]d9696672011-03-15 22:45:0995}
96
[email protected]c2dad292012-09-07 21:27:3597const HttpNetworkSession::Params* URLRequestContext::GetNetworkSessionParams(
98 ) const {
99 HttpTransactionFactory* transaction_factory = http_transaction_factory();
100 if (!transaction_factory)
tbansalea2fb8c2015-05-22 22:23:00101 return nullptr;
[email protected]c2dad292012-09-07 21:27:35102 HttpNetworkSession* network_session = transaction_factory->GetSession();
103 if (!network_session)
tbansalea2fb8c2015-05-22 22:23:00104 return nullptr;
[email protected]c2dad292012-09-07 21:27:35105 return &network_session->params();
106}
107
mmenke6ddfbea2017-05-31 21:48:41108const HttpNetworkSession::Context* URLRequestContext::GetNetworkSessionContext()
109 const {
110 HttpTransactionFactory* transaction_factory = http_transaction_factory();
111 if (!transaction_factory)
112 return nullptr;
113 HttpNetworkSession* network_session = transaction_factory->GetSession();
114 if (!network_session)
115 return nullptr;
116 return &network_session->context();
117}
118
danakj8522a25b2016-04-16 00:17:36119std::unique_ptr<URLRequest> URLRequestContext::CreateRequest(
[email protected]2ca01e52013-10-31 22:05:19120 const GURL& url,
121 RequestPriority priority,
davidben151423e2015-03-23 18:48:36122 URLRequest::Delegate* delegate) const {
rhalavati0e2a2272017-05-23 13:10:32123 return CreateRequest(url, priority, delegate, MISSING_TRAFFIC_ANNOTATION);
[email protected]8a26ff62012-08-24 21:49:20124}
125
rhalavatia9b551d2017-02-09 12:03:00126std::unique_ptr<URLRequest> URLRequestContext::CreateRequest(
127 const GURL& url,
128 RequestPriority priority,
129 URLRequest::Delegate* delegate,
130 NetworkTrafficAnnotationTag traffic_annotation) const {
rhalavati0e2a2272017-05-23 13:10:32131 return base::WrapUnique(new URLRequest(
132 url, priority, delegate, this, network_delegate_, traffic_annotation));
rhalavatia9b551d2017-02-09 12:03:00133}
134
[email protected]d100e44f2011-01-26 22:47:11135void URLRequestContext::set_cookie_store(CookieStore* cookie_store) {
136 cookie_store_ = cookie_store;
137}
138
[email protected]1e714bba2012-04-10 17:01:05139void URLRequestContext::AssertNoURLRequests() const {
Helen Lif5e4a752018-01-02 16:03:02140 int num_requests = url_requests_->size();
[email protected]7c52ed92012-04-06 15:42:40141 if (num_requests != 0) {
142 // We're leaking URLRequests :( Dump the URL of the first one and record how
143 // many we leaked so we have an idea of how bad it is.
Helen Lif5e4a752018-01-02 16:03:02144 const URLRequest* request = *url_requests_->begin();
[email protected]eb4ecaca2012-05-04 01:05:03145 int load_flags = request->load_flags();
Lukasz Anforowicz68c21772018-01-13 03:42:44146 DEBUG_ALIAS_FOR_GURL(url_buf, request->url());
[email protected]7c52ed92012-04-06 15:42:40147 base::debug::Alias(&num_requests);
[email protected]eb4ecaca2012-05-04 01:05:03148 base::debug::Alias(&load_flags);
[email protected]1f5f8e22012-09-06 23:40:54149 CHECK(false) << "Leaked " << num_requests << " URLRequest(s). First URL: "
150 << request->url().spec().c_str() << ".";
[email protected]7c52ed92012-04-06 15:42:40151 }
[email protected]9349cfb2010-08-31 18:00:53152}
[email protected]2fb629202010-12-23 23:52:57153
xunjieli96ab36a72016-12-05 21:36:05154bool URLRequestContext::OnMemoryDump(
155 const base::trace_event::MemoryDumpArgs& args,
156 base::trace_event::ProcessMemoryDump* pmd) {
xunjielid5debfc2017-02-24 15:55:49157 SSLClientSocketImpl::DumpSSLClientSessionMemoryStats(pmd);
158
xunjielic4da1de2017-03-28 16:19:50159 std::string dump_name =
Matt Menke7d4b66f72017-09-26 04:09:55160 base::StringPrintf("net/url_request_context/%s/0x%" PRIxPTR,
161 name_.c_str(), reinterpret_cast<uintptr_t>(this));
xunjielid5debfc2017-02-24 15:55:49162 base::trace_event::MemoryAllocatorDump* dump =
163 pmd->CreateAllocatorDump(dump_name);
xunjieli96ab36a72016-12-05 21:36:05164 dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameObjectCount,
165 base::trace_event::MemoryAllocatorDump::kUnitsObjects,
Helen Lif5e4a752018-01-02 16:03:02166 url_requests_->size());
xunjieli9f8c5fb52016-12-07 22:59:33167 HttpTransactionFactory* transaction_factory = http_transaction_factory();
168 if (transaction_factory) {
169 HttpNetworkSession* network_session = transaction_factory->GetSession();
170 if (network_session)
171 network_session->DumpMemoryStats(pmd, dump->absolute_name());
xunjielia0166f42017-02-23 17:44:57172 HttpCache* http_cache = transaction_factory->GetCache();
173 if (http_cache)
174 http_cache->DumpMemoryStats(pmd, dump->absolute_name());
xunjieli9f8c5fb52016-12-07 22:59:33175 }
Maks Orlovich5cf437b02018-03-27 04:40:42176 if (cookie_store_) {
177 cookie_store_->DumpMemoryStats(pmd, dump->absolute_name());
178 }
xunjieli96ab36a72016-12-05 21:36:05179 return true;
180}
181
[email protected]27a112c2011-01-06 04:19:30182} // namespace net