blob: 0ec1db63efcd31e5648fe898b009bf4dda0b5730 [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"
[email protected]4dc3ad4f2013-06-11 07:15:5012#include "base/strings/string_util.h"
xunjieli96ab36a72016-12-05 21:36:0513#include "base/strings/stringprintf.h"
fdoraya19b7702016-12-23 14:19:3114#include "base/threading/thread_task_runner_handle.h"
xunjieli96ab36a72016-12-05 21:36:0515#include "base/trace_event/memory_allocator_dump.h"
16#include "base/trace_event/memory_dump_manager.h"
xunjielid5debfc2017-02-24 15:55:4917#include "base/trace_event/memory_dump_request_args.h"
xunjieli96ab36a72016-12-05 21:36:0518#include "base/trace_event/process_memory_dump.h"
xunjielid701b6e52017-01-04 22:17:4219#include "net/base/sdch_manager.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"
[email protected]f6c21cb2011-02-16 19:45:4123#include "net/http/http_transaction_factory.h"
xunjieli9f8c5fb52016-12-07 22:59:3324#include "net/socket/ssl_client_socket_impl.h"
[email protected]ee4c30d2012-11-07 15:08:4325#include "net/url_request/http_user_agent_settings.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
[email protected]9349cfb2010-08-31 18:00:5330URLRequestContext::URLRequestContext()
tbansalea2fb8c2015-05-22 22:23:0031 : net_log_(nullptr),
32 host_resolver_(nullptr),
33 cert_verifier_(nullptr),
34 channel_id_service_(nullptr),
tbansalea2fb8c2015-05-22 22:23:0035 http_auth_handler_factory_(nullptr),
36 proxy_service_(nullptr),
37 network_delegate_(nullptr),
bnc525e175a2016-06-20 12:36:4038 http_server_properties_(nullptr),
tbansalea2fb8c2015-05-22 22:23:0039 http_user_agent_settings_(nullptr),
mmenke606c59c2016-03-07 18:20:5540 cookie_store_(nullptr),
tbansalea2fb8c2015-05-22 22:23:0041 transport_security_state_(nullptr),
42 cert_transparency_verifier_(nullptr),
rsleevid6de8302016-06-21 01:33:2043 ct_policy_enforcer_(nullptr),
tbansalea2fb8c2015-05-22 22:23:0044 http_transaction_factory_(nullptr),
45 job_factory_(nullptr),
46 throttler_manager_(nullptr),
xunjieli3bb781a2015-07-22 22:40:3447 backoff_manager_(nullptr),
rdsmithd6ee1642015-05-29 15:01:3048 sdch_manager_(nullptr),
tbansalea2fb8c2015-05-22 22:23:0049 network_quality_estimator_(nullptr),
juliatuttle2b8dd2d12017-04-12 21:27:4150 reporting_service_(nullptr),
nharper5babb5e62016-03-09 18:58:0751 url_requests_(new std::set<const URLRequest*>),
mgershd21d6d142016-12-14 23:06:3652 enable_brotli_(false),
xunjielic4da1de2017-03-28 16:19:5053 check_cleartext_permitted_(false),
54 name_(nullptr) {
xunjieli96ab36a72016-12-05 21:36:0555 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
56 this, "URLRequestContext", base::ThreadTaskRunnerHandle::Get());
57}
[email protected]f1d81922010-07-31 17:47:0958
[email protected]ef2bf422012-05-11 03:27:0959URLRequestContext::~URLRequestContext() {
60 AssertNoURLRequests();
xunjieli96ab36a72016-12-05 21:36:0561 base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider(
62 this);
[email protected]ef2bf422012-05-11 03:27:0963}
64
[email protected]fa22a6f12012-05-11 17:19:5965void URLRequestContext::CopyFrom(const URLRequestContext* other) {
[email protected]d9696672011-03-15 22:45:0966 // Copy URLRequestContext parameters.
[email protected]fa22a6f12012-05-11 17:19:5967 set_net_log(other->net_log_);
68 set_host_resolver(other->host_resolver_);
69 set_cert_verifier(other->cert_verifier_);
[email protected]6b8a3c742014-07-25 00:25:3570 set_channel_id_service(other->channel_id_service_);
[email protected]fa22a6f12012-05-11 17:19:5971 set_http_auth_handler_factory(other->http_auth_handler_factory_);
72 set_proxy_service(other->proxy_service_);
[email protected]90499482013-06-01 00:39:5073 set_ssl_config_service(other->ssl_config_service_.get());
[email protected]fa22a6f12012-05-11 17:19:5974 set_network_delegate(other->network_delegate_);
75 set_http_server_properties(other->http_server_properties_);
mmenke606c59c2016-03-07 18:20:5576 set_cookie_store(other->cookie_store_);
[email protected]fa22a6f12012-05-11 17:19:5977 set_transport_security_state(other->transport_security_state_);
[email protected]284303b62013-11-28 15:11:5478 set_cert_transparency_verifier(other->cert_transparency_verifier_);
rsleevid6de8302016-06-21 01:33:2079 set_ct_policy_enforcer(other->ct_policy_enforcer_);
[email protected]fa22a6f12012-05-11 17:19:5980 set_http_transaction_factory(other->http_transaction_factory_);
[email protected]fa22a6f12012-05-11 17:19:5981 set_job_factory(other->job_factory_);
82 set_throttler_manager(other->throttler_manager_);
xunjieli3bb781a2015-07-22 22:40:3483 set_backoff_manager(other->backoff_manager_);
rdsmithd6ee1642015-05-29 15:01:3084 set_sdch_manager(other->sdch_manager_);
[email protected]ee4c30d2012-11-07 15:08:4385 set_http_user_agent_settings(other->http_user_agent_settings_);
tbansalea2fb8c2015-05-22 22:23:0086 set_network_quality_estimator(other->network_quality_estimator_);
juliatuttle2b8dd2d12017-04-12 21:27:4187 set_reporting_service(other->reporting_service_);
maksim.sisov3d40c812016-05-02 13:27:1688 set_enable_brotli(other->enable_brotli_);
mgershd21d6d142016-12-14 23:06:3689 set_check_cleartext_permitted(other->check_cleartext_permitted_);
[email protected]d9696672011-03-15 22:45:0990}
91
[email protected]c2dad292012-09-07 21:27:3592const HttpNetworkSession::Params* URLRequestContext::GetNetworkSessionParams(
93 ) const {
94 HttpTransactionFactory* transaction_factory = http_transaction_factory();
95 if (!transaction_factory)
tbansalea2fb8c2015-05-22 22:23:0096 return nullptr;
[email protected]c2dad292012-09-07 21:27:3597 HttpNetworkSession* network_session = transaction_factory->GetSession();
98 if (!network_session)
tbansalea2fb8c2015-05-22 22:23:0099 return nullptr;
[email protected]c2dad292012-09-07 21:27:35100 return &network_session->params();
101}
102
danakj8522a25b2016-04-16 00:17:36103std::unique_ptr<URLRequest> URLRequestContext::CreateRequest(
[email protected]2ca01e52013-10-31 22:05:19104 const GURL& url,
105 RequestPriority priority,
davidben151423e2015-03-23 18:48:36106 URLRequest::Delegate* delegate) const {
danakj8522a25b2016-04-16 00:17:36107 return base::WrapUnique(
davidben151423e2015-03-23 18:48:36108 new URLRequest(url, priority, delegate, this, network_delegate_));
[email protected]8a26ff62012-08-24 21:49:20109}
110
rhalavatia9b551d2017-02-09 12:03:00111std::unique_ptr<URLRequest> URLRequestContext::CreateRequest(
112 const GURL& url,
113 RequestPriority priority,
114 URLRequest::Delegate* delegate,
115 NetworkTrafficAnnotationTag traffic_annotation) const {
116 // |traffic_annotation| is just a tag that is extracted during static
117 // code analysis and can be ignored here.
118 return CreateRequest(url, priority, delegate);
119}
120
[email protected]d100e44f2011-01-26 22:47:11121void URLRequestContext::set_cookie_store(CookieStore* cookie_store) {
122 cookie_store_ = cookie_store;
123}
124
[email protected]1e714bba2012-04-10 17:01:05125void URLRequestContext::AssertNoURLRequests() const {
[email protected]7c52ed92012-04-06 15:42:40126 int num_requests = url_requests_->size();
127 if (num_requests != 0) {
128 // We're leaking URLRequests :( Dump the URL of the first one and record how
129 // many we leaked so we have an idea of how bad it is.
130 char url_buf[128];
131 const URLRequest* request = *url_requests_->begin();
132 base::strlcpy(url_buf, request->url().spec().c_str(), arraysize(url_buf));
[email protected]eb4ecaca2012-05-04 01:05:03133 int load_flags = request->load_flags();
[email protected]7c52ed92012-04-06 15:42:40134 base::debug::Alias(url_buf);
135 base::debug::Alias(&num_requests);
[email protected]eb4ecaca2012-05-04 01:05:03136 base::debug::Alias(&load_flags);
[email protected]1f5f8e22012-09-06 23:40:54137 CHECK(false) << "Leaked " << num_requests << " URLRequest(s). First URL: "
138 << request->url().spec().c_str() << ".";
[email protected]7c52ed92012-04-06 15:42:40139 }
[email protected]9349cfb2010-08-31 18:00:53140}
[email protected]2fb629202010-12-23 23:52:57141
xunjieli96ab36a72016-12-05 21:36:05142bool URLRequestContext::OnMemoryDump(
143 const base::trace_event::MemoryDumpArgs& args,
144 base::trace_event::ProcessMemoryDump* pmd) {
xunjielic4da1de2017-03-28 16:19:50145 if (!name_)
xunjieli96ab36a72016-12-05 21:36:05146 name_ = "unknown";
xunjielid5debfc2017-02-24 15:55:49147
148 SSLClientSocketImpl::DumpSSLClientSessionMemoryStats(pmd);
149
xunjielic4da1de2017-03-28 16:19:50150 std::string dump_name =
151 base::StringPrintf("net/url_request_context/%s/0x%" PRIxPTR, name_,
152 reinterpret_cast<uintptr_t>(this));
xunjielid5debfc2017-02-24 15:55:49153 base::trace_event::MemoryAllocatorDump* dump =
154 pmd->CreateAllocatorDump(dump_name);
xunjieli96ab36a72016-12-05 21:36:05155 dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameObjectCount,
156 base::trace_event::MemoryAllocatorDump::kUnitsObjects,
157 url_requests_->size());
xunjieli9f8c5fb52016-12-07 22:59:33158 HttpTransactionFactory* transaction_factory = http_transaction_factory();
159 if (transaction_factory) {
160 HttpNetworkSession* network_session = transaction_factory->GetSession();
161 if (network_session)
162 network_session->DumpMemoryStats(pmd, dump->absolute_name());
xunjielia0166f42017-02-23 17:44:57163 HttpCache* http_cache = transaction_factory->GetCache();
164 if (http_cache)
165 http_cache->DumpMemoryStats(pmd, dump->absolute_name());
xunjieli9f8c5fb52016-12-07 22:59:33166 }
xunjielid701b6e52017-01-04 22:17:42167 if (sdch_manager_)
xunjielid5debfc2017-02-24 15:55:49168 sdch_manager_->DumpMemoryStats(pmd, dump_name);
xunjieli96ab36a72016-12-05 21:36:05169 return true;
170}
171
[email protected]27a112c2011-01-06 04:19:30172} // namespace net