blob: 14d6f548f1c3d285b45b22951c536674628c9471 [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
[email protected]18590f22011-07-29 16:41:287#include "base/compiler_specific.h"
[email protected]7c52ed92012-04-06 15:42:408#include "base/debug/alias.h"
danakj8522a25b2016-04-16 00:17:369#include "base/memory/ptr_util.h"
[email protected]4dc3ad4f2013-06-11 07:15:5010#include "base/strings/string_util.h"
xunjieli96ab36a72016-12-05 21:36:0511#include "base/strings/stringprintf.h"
12#include "base/trace_event/memory_allocator_dump.h"
13#include "base/trace_event/memory_dump_manager.h"
14#include "base/trace_event/process_memory_dump.h"
[email protected]aa84a7e2012-03-15 21:29:0615#include "net/cookies/cookie_store.h"
[email protected]f2cb3cf2013-03-21 01:40:5316#include "net/dns/host_resolver.h"
[email protected]f6c21cb2011-02-16 19:45:4117#include "net/http/http_transaction_factory.h"
[email protected]ee4c30d2012-11-07 15:08:4318#include "net/url_request/http_user_agent_settings.h"
[email protected]7c52ed92012-04-06 15:42:4019#include "net/url_request/url_request.h"
[email protected]9349cfb2010-08-31 18:00:5320
[email protected]27a112c2011-01-06 04:19:3021namespace net {
22
[email protected]9349cfb2010-08-31 18:00:5323URLRequestContext::URLRequestContext()
tbansalea2fb8c2015-05-22 22:23:0024 : net_log_(nullptr),
25 host_resolver_(nullptr),
26 cert_verifier_(nullptr),
27 channel_id_service_(nullptr),
tbansalea2fb8c2015-05-22 22:23:0028 http_auth_handler_factory_(nullptr),
29 proxy_service_(nullptr),
30 network_delegate_(nullptr),
bnc525e175a2016-06-20 12:36:4031 http_server_properties_(nullptr),
tbansalea2fb8c2015-05-22 22:23:0032 http_user_agent_settings_(nullptr),
mmenke606c59c2016-03-07 18:20:5533 cookie_store_(nullptr),
tbansalea2fb8c2015-05-22 22:23:0034 transport_security_state_(nullptr),
35 cert_transparency_verifier_(nullptr),
rsleevid6de8302016-06-21 01:33:2036 ct_policy_enforcer_(nullptr),
tbansalea2fb8c2015-05-22 22:23:0037 http_transaction_factory_(nullptr),
38 job_factory_(nullptr),
39 throttler_manager_(nullptr),
xunjieli3bb781a2015-07-22 22:40:3440 backoff_manager_(nullptr),
rdsmithd6ee1642015-05-29 15:01:3041 sdch_manager_(nullptr),
tbansalea2fb8c2015-05-22 22:23:0042 network_quality_estimator_(nullptr),
nharper5babb5e62016-03-09 18:58:0743 url_requests_(new std::set<const URLRequest*>),
xunjieli96ab36a72016-12-05 21:36:0544 enable_brotli_(false) {
45 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
46 this, "URLRequestContext", base::ThreadTaskRunnerHandle::Get());
47}
[email protected]f1d81922010-07-31 17:47:0948
[email protected]ef2bf422012-05-11 03:27:0949URLRequestContext::~URLRequestContext() {
50 AssertNoURLRequests();
xunjieli96ab36a72016-12-05 21:36:0551 base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider(
52 this);
[email protected]ef2bf422012-05-11 03:27:0953}
54
[email protected]fa22a6f12012-05-11 17:19:5955void URLRequestContext::CopyFrom(const URLRequestContext* other) {
[email protected]d9696672011-03-15 22:45:0956 // Copy URLRequestContext parameters.
[email protected]fa22a6f12012-05-11 17:19:5957 set_net_log(other->net_log_);
58 set_host_resolver(other->host_resolver_);
59 set_cert_verifier(other->cert_verifier_);
[email protected]6b8a3c742014-07-25 00:25:3560 set_channel_id_service(other->channel_id_service_);
[email protected]fa22a6f12012-05-11 17:19:5961 set_http_auth_handler_factory(other->http_auth_handler_factory_);
62 set_proxy_service(other->proxy_service_);
[email protected]90499482013-06-01 00:39:5063 set_ssl_config_service(other->ssl_config_service_.get());
[email protected]fa22a6f12012-05-11 17:19:5964 set_network_delegate(other->network_delegate_);
65 set_http_server_properties(other->http_server_properties_);
mmenke606c59c2016-03-07 18:20:5566 set_cookie_store(other->cookie_store_);
[email protected]fa22a6f12012-05-11 17:19:5967 set_transport_security_state(other->transport_security_state_);
[email protected]284303b62013-11-28 15:11:5468 set_cert_transparency_verifier(other->cert_transparency_verifier_);
rsleevid6de8302016-06-21 01:33:2069 set_ct_policy_enforcer(other->ct_policy_enforcer_);
[email protected]fa22a6f12012-05-11 17:19:5970 set_http_transaction_factory(other->http_transaction_factory_);
[email protected]fa22a6f12012-05-11 17:19:5971 set_job_factory(other->job_factory_);
72 set_throttler_manager(other->throttler_manager_);
xunjieli3bb781a2015-07-22 22:40:3473 set_backoff_manager(other->backoff_manager_);
rdsmithd6ee1642015-05-29 15:01:3074 set_sdch_manager(other->sdch_manager_);
[email protected]ee4c30d2012-11-07 15:08:4375 set_http_user_agent_settings(other->http_user_agent_settings_);
tbansalea2fb8c2015-05-22 22:23:0076 set_network_quality_estimator(other->network_quality_estimator_);
maksim.sisov3d40c812016-05-02 13:27:1677 set_enable_brotli(other->enable_brotli_);
[email protected]d9696672011-03-15 22:45:0978}
79
[email protected]c2dad292012-09-07 21:27:3580const HttpNetworkSession::Params* URLRequestContext::GetNetworkSessionParams(
81 ) const {
82 HttpTransactionFactory* transaction_factory = http_transaction_factory();
83 if (!transaction_factory)
tbansalea2fb8c2015-05-22 22:23:0084 return nullptr;
[email protected]c2dad292012-09-07 21:27:3585 HttpNetworkSession* network_session = transaction_factory->GetSession();
86 if (!network_session)
tbansalea2fb8c2015-05-22 22:23:0087 return nullptr;
[email protected]c2dad292012-09-07 21:27:3588 return &network_session->params();
89}
90
danakj8522a25b2016-04-16 00:17:3691std::unique_ptr<URLRequest> URLRequestContext::CreateRequest(
[email protected]2ca01e52013-10-31 22:05:1992 const GURL& url,
93 RequestPriority priority,
davidben151423e2015-03-23 18:48:3694 URLRequest::Delegate* delegate) const {
danakj8522a25b2016-04-16 00:17:3695 return base::WrapUnique(
davidben151423e2015-03-23 18:48:3696 new URLRequest(url, priority, delegate, this, network_delegate_));
[email protected]8a26ff62012-08-24 21:49:2097}
98
[email protected]d100e44f2011-01-26 22:47:1199void URLRequestContext::set_cookie_store(CookieStore* cookie_store) {
100 cookie_store_ = cookie_store;
101}
102
[email protected]1e714bba2012-04-10 17:01:05103void URLRequestContext::AssertNoURLRequests() const {
[email protected]7c52ed92012-04-06 15:42:40104 int num_requests = url_requests_->size();
105 if (num_requests != 0) {
106 // We're leaking URLRequests :( Dump the URL of the first one and record how
107 // many we leaked so we have an idea of how bad it is.
108 char url_buf[128];
109 const URLRequest* request = *url_requests_->begin();
110 base::strlcpy(url_buf, request->url().spec().c_str(), arraysize(url_buf));
[email protected]eb4ecaca2012-05-04 01:05:03111 int load_flags = request->load_flags();
[email protected]7c52ed92012-04-06 15:42:40112 base::debug::Alias(url_buf);
113 base::debug::Alias(&num_requests);
[email protected]eb4ecaca2012-05-04 01:05:03114 base::debug::Alias(&load_flags);
[email protected]1f5f8e22012-09-06 23:40:54115 CHECK(false) << "Leaked " << num_requests << " URLRequest(s). First URL: "
116 << request->url().spec().c_str() << ".";
[email protected]7c52ed92012-04-06 15:42:40117 }
[email protected]9349cfb2010-08-31 18:00:53118}
[email protected]2fb629202010-12-23 23:52:57119
xunjieli96ab36a72016-12-05 21:36:05120bool URLRequestContext::OnMemoryDump(
121 const base::trace_event::MemoryDumpArgs& args,
122 base::trace_event::ProcessMemoryDump* pmd) {
123 if (name_.empty())
124 name_ = "unknown";
125 base::trace_event::MemoryAllocatorDump* dump = pmd->CreateAllocatorDump(
126 base::StringPrintf("net/url_request_context/%s_%p", name_.c_str(), this));
127 dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameObjectCount,
128 base::trace_event::MemoryAllocatorDump::kUnitsObjects,
129 url_requests_->size());
130 return true;
131}
132
[email protected]27a112c2011-01-06 04:19:30133} // namespace net