blob: 59f74d4989323a5a8bc8845b11d5c4b47b648ae8 [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),
nharper5babb5e62016-03-09 18:58:0750 url_requests_(new std::set<const URLRequest*>),
mgershd21d6d142016-12-14 23:06:3651 enable_brotli_(false),
xunjielic4da1de2017-03-28 16:19:5052 check_cleartext_permitted_(false),
53 name_(nullptr) {
xunjieli96ab36a72016-12-05 21:36:0554 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
55 this, "URLRequestContext", base::ThreadTaskRunnerHandle::Get());
56}
[email protected]f1d81922010-07-31 17:47:0957
[email protected]ef2bf422012-05-11 03:27:0958URLRequestContext::~URLRequestContext() {
59 AssertNoURLRequests();
xunjieli96ab36a72016-12-05 21:36:0560 base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider(
61 this);
[email protected]ef2bf422012-05-11 03:27:0962}
63
[email protected]fa22a6f12012-05-11 17:19:5964void URLRequestContext::CopyFrom(const URLRequestContext* other) {
[email protected]d9696672011-03-15 22:45:0965 // Copy URLRequestContext parameters.
[email protected]fa22a6f12012-05-11 17:19:5966 set_net_log(other->net_log_);
67 set_host_resolver(other->host_resolver_);
68 set_cert_verifier(other->cert_verifier_);
[email protected]6b8a3c742014-07-25 00:25:3569 set_channel_id_service(other->channel_id_service_);
[email protected]fa22a6f12012-05-11 17:19:5970 set_http_auth_handler_factory(other->http_auth_handler_factory_);
71 set_proxy_service(other->proxy_service_);
[email protected]90499482013-06-01 00:39:5072 set_ssl_config_service(other->ssl_config_service_.get());
[email protected]fa22a6f12012-05-11 17:19:5973 set_network_delegate(other->network_delegate_);
74 set_http_server_properties(other->http_server_properties_);
mmenke606c59c2016-03-07 18:20:5575 set_cookie_store(other->cookie_store_);
[email protected]fa22a6f12012-05-11 17:19:5976 set_transport_security_state(other->transport_security_state_);
[email protected]284303b62013-11-28 15:11:5477 set_cert_transparency_verifier(other->cert_transparency_verifier_);
rsleevid6de8302016-06-21 01:33:2078 set_ct_policy_enforcer(other->ct_policy_enforcer_);
[email protected]fa22a6f12012-05-11 17:19:5979 set_http_transaction_factory(other->http_transaction_factory_);
[email protected]fa22a6f12012-05-11 17:19:5980 set_job_factory(other->job_factory_);
81 set_throttler_manager(other->throttler_manager_);
xunjieli3bb781a2015-07-22 22:40:3482 set_backoff_manager(other->backoff_manager_);
rdsmithd6ee1642015-05-29 15:01:3083 set_sdch_manager(other->sdch_manager_);
[email protected]ee4c30d2012-11-07 15:08:4384 set_http_user_agent_settings(other->http_user_agent_settings_);
tbansalea2fb8c2015-05-22 22:23:0085 set_network_quality_estimator(other->network_quality_estimator_);
maksim.sisov3d40c812016-05-02 13:27:1686 set_enable_brotli(other->enable_brotli_);
mgershd21d6d142016-12-14 23:06:3687 set_check_cleartext_permitted(other->check_cleartext_permitted_);
[email protected]d9696672011-03-15 22:45:0988}
89
[email protected]c2dad292012-09-07 21:27:3590const HttpNetworkSession::Params* URLRequestContext::GetNetworkSessionParams(
91 ) const {
92 HttpTransactionFactory* transaction_factory = http_transaction_factory();
93 if (!transaction_factory)
tbansalea2fb8c2015-05-22 22:23:0094 return nullptr;
[email protected]c2dad292012-09-07 21:27:3595 HttpNetworkSession* network_session = transaction_factory->GetSession();
96 if (!network_session)
tbansalea2fb8c2015-05-22 22:23:0097 return nullptr;
[email protected]c2dad292012-09-07 21:27:3598 return &network_session->params();
99}
100
danakj8522a25b2016-04-16 00:17:36101std::unique_ptr<URLRequest> URLRequestContext::CreateRequest(
[email protected]2ca01e52013-10-31 22:05:19102 const GURL& url,
103 RequestPriority priority,
davidben151423e2015-03-23 18:48:36104 URLRequest::Delegate* delegate) const {
danakj8522a25b2016-04-16 00:17:36105 return base::WrapUnique(
davidben151423e2015-03-23 18:48:36106 new URLRequest(url, priority, delegate, this, network_delegate_));
[email protected]8a26ff62012-08-24 21:49:20107}
108
rhalavatia9b551d2017-02-09 12:03:00109std::unique_ptr<URLRequest> URLRequestContext::CreateRequest(
110 const GURL& url,
111 RequestPriority priority,
112 URLRequest::Delegate* delegate,
113 NetworkTrafficAnnotationTag traffic_annotation) const {
114 // |traffic_annotation| is just a tag that is extracted during static
115 // code analysis and can be ignored here.
116 return CreateRequest(url, priority, delegate);
117}
118
[email protected]d100e44f2011-01-26 22:47:11119void URLRequestContext::set_cookie_store(CookieStore* cookie_store) {
120 cookie_store_ = cookie_store;
121}
122
[email protected]1e714bba2012-04-10 17:01:05123void URLRequestContext::AssertNoURLRequests() const {
[email protected]7c52ed92012-04-06 15:42:40124 int num_requests = url_requests_->size();
125 if (num_requests != 0) {
126 // We're leaking URLRequests :( Dump the URL of the first one and record how
127 // many we leaked so we have an idea of how bad it is.
128 char url_buf[128];
129 const URLRequest* request = *url_requests_->begin();
130 base::strlcpy(url_buf, request->url().spec().c_str(), arraysize(url_buf));
[email protected]eb4ecaca2012-05-04 01:05:03131 int load_flags = request->load_flags();
[email protected]7c52ed92012-04-06 15:42:40132 base::debug::Alias(url_buf);
133 base::debug::Alias(&num_requests);
[email protected]eb4ecaca2012-05-04 01:05:03134 base::debug::Alias(&load_flags);
[email protected]1f5f8e22012-09-06 23:40:54135 CHECK(false) << "Leaked " << num_requests << " URLRequest(s). First URL: "
136 << request->url().spec().c_str() << ".";
[email protected]7c52ed92012-04-06 15:42:40137 }
[email protected]9349cfb2010-08-31 18:00:53138}
[email protected]2fb629202010-12-23 23:52:57139
xunjieli96ab36a72016-12-05 21:36:05140bool URLRequestContext::OnMemoryDump(
141 const base::trace_event::MemoryDumpArgs& args,
142 base::trace_event::ProcessMemoryDump* pmd) {
xunjielic4da1de2017-03-28 16:19:50143 if (!name_)
xunjieli96ab36a72016-12-05 21:36:05144 name_ = "unknown";
xunjielid5debfc2017-02-24 15:55:49145
146 SSLClientSocketImpl::DumpSSLClientSessionMemoryStats(pmd);
147
xunjielic4da1de2017-03-28 16:19:50148 std::string dump_name =
149 base::StringPrintf("net/url_request_context/%s/0x%" PRIxPTR, name_,
150 reinterpret_cast<uintptr_t>(this));
xunjielid5debfc2017-02-24 15:55:49151 base::trace_event::MemoryAllocatorDump* dump =
152 pmd->CreateAllocatorDump(dump_name);
xunjieli96ab36a72016-12-05 21:36:05153 dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameObjectCount,
154 base::trace_event::MemoryAllocatorDump::kUnitsObjects,
155 url_requests_->size());
xunjieli9f8c5fb52016-12-07 22:59:33156 HttpTransactionFactory* transaction_factory = http_transaction_factory();
157 if (transaction_factory) {
158 HttpNetworkSession* network_session = transaction_factory->GetSession();
159 if (network_session)
160 network_session->DumpMemoryStats(pmd, dump->absolute_name());
xunjielia0166f42017-02-23 17:44:57161 HttpCache* http_cache = transaction_factory->GetCache();
162 if (http_cache)
163 http_cache->DumpMemoryStats(pmd, dump->absolute_name());
xunjieli9f8c5fb52016-12-07 22:59:33164 }
xunjielid701b6e52017-01-04 22:17:42165 if (sdch_manager_)
xunjielid5debfc2017-02-24 15:55:49166 sdch_manager_->DumpMemoryStats(pmd, dump_name);
xunjieli96ab36a72016-12-05 21:36:05167 return true;
168}
169
[email protected]27a112c2011-01-06 04:19:30170} // namespace net