forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl_request_context.cc
170 lines (154 loc) · 6.4 KB
/
url_request_context.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/url_request/url_request_context.h"
#include <inttypes.h>
#include "base/compiler_specific.h"
#include "base/debug/alias.h"
#include "base/memory/ptr_util.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/trace_event/memory_allocator_dump.h"
#include "base/trace_event/memory_dump_manager.h"
#include "base/trace_event/memory_dump_request_args.h"
#include "base/trace_event/process_memory_dump.h"
#include "net/base/sdch_manager.h"
#include "net/cookies/cookie_store.h"
#include "net/dns/host_resolver.h"
#include "net/http/http_cache.h"
#include "net/http/http_transaction_factory.h"
#include "net/socket/ssl_client_socket_impl.h"
#include "net/url_request/http_user_agent_settings.h"
#include "net/url_request/url_request.h"
namespace net {
URLRequestContext::URLRequestContext()
: net_log_(nullptr),
host_resolver_(nullptr),
cert_verifier_(nullptr),
channel_id_service_(nullptr),
http_auth_handler_factory_(nullptr),
proxy_service_(nullptr),
network_delegate_(nullptr),
http_server_properties_(nullptr),
http_user_agent_settings_(nullptr),
cookie_store_(nullptr),
transport_security_state_(nullptr),
cert_transparency_verifier_(nullptr),
ct_policy_enforcer_(nullptr),
http_transaction_factory_(nullptr),
job_factory_(nullptr),
throttler_manager_(nullptr),
backoff_manager_(nullptr),
sdch_manager_(nullptr),
network_quality_estimator_(nullptr),
url_requests_(new std::set<const URLRequest*>),
enable_brotli_(false),
check_cleartext_permitted_(false),
name_(nullptr) {
base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
this, "URLRequestContext", base::ThreadTaskRunnerHandle::Get());
}
URLRequestContext::~URLRequestContext() {
AssertNoURLRequests();
base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider(
this);
}
void URLRequestContext::CopyFrom(const URLRequestContext* other) {
// Copy URLRequestContext parameters.
set_net_log(other->net_log_);
set_host_resolver(other->host_resolver_);
set_cert_verifier(other->cert_verifier_);
set_channel_id_service(other->channel_id_service_);
set_http_auth_handler_factory(other->http_auth_handler_factory_);
set_proxy_service(other->proxy_service_);
set_ssl_config_service(other->ssl_config_service_.get());
set_network_delegate(other->network_delegate_);
set_http_server_properties(other->http_server_properties_);
set_cookie_store(other->cookie_store_);
set_transport_security_state(other->transport_security_state_);
set_cert_transparency_verifier(other->cert_transparency_verifier_);
set_ct_policy_enforcer(other->ct_policy_enforcer_);
set_http_transaction_factory(other->http_transaction_factory_);
set_job_factory(other->job_factory_);
set_throttler_manager(other->throttler_manager_);
set_backoff_manager(other->backoff_manager_);
set_sdch_manager(other->sdch_manager_);
set_http_user_agent_settings(other->http_user_agent_settings_);
set_network_quality_estimator(other->network_quality_estimator_);
set_enable_brotli(other->enable_brotli_);
set_check_cleartext_permitted(other->check_cleartext_permitted_);
}
const HttpNetworkSession::Params* URLRequestContext::GetNetworkSessionParams(
) const {
HttpTransactionFactory* transaction_factory = http_transaction_factory();
if (!transaction_factory)
return nullptr;
HttpNetworkSession* network_session = transaction_factory->GetSession();
if (!network_session)
return nullptr;
return &network_session->params();
}
std::unique_ptr<URLRequest> URLRequestContext::CreateRequest(
const GURL& url,
RequestPriority priority,
URLRequest::Delegate* delegate) const {
return base::WrapUnique(
new URLRequest(url, priority, delegate, this, network_delegate_));
}
std::unique_ptr<URLRequest> URLRequestContext::CreateRequest(
const GURL& url,
RequestPriority priority,
URLRequest::Delegate* delegate,
NetworkTrafficAnnotationTag traffic_annotation) const {
// |traffic_annotation| is just a tag that is extracted during static
// code analysis and can be ignored here.
return CreateRequest(url, priority, delegate);
}
void URLRequestContext::set_cookie_store(CookieStore* cookie_store) {
cookie_store_ = cookie_store;
}
void URLRequestContext::AssertNoURLRequests() const {
int num_requests = url_requests_->size();
if (num_requests != 0) {
// We're leaking URLRequests :( Dump the URL of the first one and record how
// many we leaked so we have an idea of how bad it is.
char url_buf[128];
const URLRequest* request = *url_requests_->begin();
base::strlcpy(url_buf, request->url().spec().c_str(), arraysize(url_buf));
int load_flags = request->load_flags();
base::debug::Alias(url_buf);
base::debug::Alias(&num_requests);
base::debug::Alias(&load_flags);
CHECK(false) << "Leaked " << num_requests << " URLRequest(s). First URL: "
<< request->url().spec().c_str() << ".";
}
}
bool URLRequestContext::OnMemoryDump(
const base::trace_event::MemoryDumpArgs& args,
base::trace_event::ProcessMemoryDump* pmd) {
if (!name_)
name_ = "unknown";
SSLClientSocketImpl::DumpSSLClientSessionMemoryStats(pmd);
std::string dump_name =
base::StringPrintf("net/url_request_context/%s/0x%" PRIxPTR, name_,
reinterpret_cast<uintptr_t>(this));
base::trace_event::MemoryAllocatorDump* dump =
pmd->CreateAllocatorDump(dump_name);
dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameObjectCount,
base::trace_event::MemoryAllocatorDump::kUnitsObjects,
url_requests_->size());
HttpTransactionFactory* transaction_factory = http_transaction_factory();
if (transaction_factory) {
HttpNetworkSession* network_session = transaction_factory->GetSession();
if (network_session)
network_session->DumpMemoryStats(pmd, dump->absolute_name());
HttpCache* http_cache = transaction_factory->GetCache();
if (http_cache)
http_cache->DumpMemoryStats(pmd, dump->absolute_name());
}
if (sdch_manager_)
sdch_manager_->DumpMemoryStats(pmd, dump_name);
return true;
}
} // namespace net