File: url_request_context.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (272 lines) | stat: -rw-r--r-- 10,652 bytes parent folder | download | duplicates (3)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// Copyright 2012 The Chromium Authors
// 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 <stdint.h>

#include "base/compiler_specific.h"
#include "base/debug/alias.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/notreached.h"
#include "base/strings/string_util.h"
#include "base/types/pass_key.h"
#include "build/build_config.h"
#include "net/base/http_user_agent_settings.h"
#include "net/base/network_delegate.h"
#include "net/base/proxy_delegate.h"
#include "net/cert/cert_verifier.h"
#include "net/cert/sct_auditing_delegate.h"
#include "net/cookies/cookie_store.h"
#include "net/dns/host_resolver.h"
#include "net/http/http_auth_handler_factory.h"
#include "net/http/http_cache.h"
#include "net/http/http_network_session.h"
#include "net/http/http_server_properties.h"
#include "net/http/http_transaction_factory.h"
#include "net/http/transport_security_persister.h"
#include "net/http/transport_security_state.h"
#include "net/log/net_log.h"
#include "net/log/net_log_source.h"
#include "net/nqe/network_quality_estimator.h"
#include "net/proxy_resolution/proxy_resolution_service.h"
#include "net/quic/quic_context.h"
#include "net/socket/client_socket_factory.h"
#include "net/socket/ssl_client_socket_impl.h"
#include "net/ssl/ssl_config_service.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_job_factory.h"
#include "url/gurl_debug.h"

#if BUILDFLAG(ENABLE_REPORTING)
#include "net/network_error_logging/network_error_logging_service.h"
#include "net/network_error_logging/persistent_reporting_and_nel_store.h"
#include "net/reporting/reporting_service.h"
#endif  // BUILDFLAG(ENABLE_REPORTING)

#if BUILDFLAG(ENABLE_DEVICE_BOUND_SESSIONS)
#include "net/device_bound_sessions/session_service.h"
#include "net/device_bound_sessions/session_store.h"
#endif  // BUILDFLAG(ENABLE_DEVICE_BOUND_SESSIONS)

namespace net {

URLRequestContext::URLRequestContext(
    base::PassKey<URLRequestContextBuilder> pass_key)
    : url_requests_(std::make_unique<
                    std::set<raw_ptr<const URLRequest, SetExperimental>>>()),
      bound_network_(handles::kInvalidNetworkHandle) {}

URLRequestContext::~URLRequestContext() {
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
#if BUILDFLAG(ENABLE_REPORTING)
  // Shut down the NetworkErrorLoggingService so that destroying the
  // ReportingService (which might abort in-flight URLRequests, generating
  // network errors) won't recursively try to queue more network error
  // reports.
  if (network_error_logging_service())
    network_error_logging_service()->OnShutdown();

  // Shut down the ReportingService before the rest of the URLRequestContext,
  // so it cancels any pending requests it may have.
  if (reporting_service())
    reporting_service()->OnShutdown();
#endif  // BUILDFLAG(ENABLE_REPORTING)

  // Shut down the ProxyResolutionService, as it may have pending URLRequests
  // using this context. Since this cancels requests, it's not safe to
  // subclass this, as some parts of the URLRequestContext may then be torn
  // down before this cancels the ProxyResolutionService's URLRequests.
  proxy_resolution_service()->OnShutdown();

  // If a ProxyDelegate is set then the builder gave it a pointer to the
  // ProxyResolutionService, so clear that here to avoid having a dangling
  // pointer. There's no need to clear the ProxyResolutionService's pointer to
  // ProxyDelegate because the member destruction order ensures that
  // ProxyResolutionService is destroyed first.
  if (proxy_delegate()) {
    proxy_delegate()->SetProxyResolutionService(nullptr);
  }

  DCHECK(host_resolver());
  host_resolver()->OnShutdown();

  AssertNoURLRequests();
}

const HttpNetworkSessionParams* 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();
}

const HttpNetworkSessionContext* URLRequestContext::GetNetworkSessionContext()
    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->context();
}

#if !BUILDFLAG(IS_WIN) && !BUILDFLAG(IS_LINUX)
std::unique_ptr<URLRequest> URLRequestContext::CreateRequest(
    const GURL& url,
    RequestPriority priority,
    URLRequest::Delegate* delegate) const {
  return CreateRequest(url, priority, delegate, MISSING_TRAFFIC_ANNOTATION,
                       /*is_for_websockets=*/false);
}
#endif

std::unique_ptr<URLRequest> URLRequestContext::CreateRequest(
    const GURL& url,
    RequestPriority priority,
    URLRequest::Delegate* delegate,
    NetworkTrafficAnnotationTag traffic_annotation,
    bool is_for_websockets,
    const std::optional<net::NetLogSource> net_log_source) const {
  return std::make_unique<URLRequest>(
      base::PassKey<URLRequestContext>(), url, priority, delegate, this,
      traffic_annotation, is_for_websockets, net_log_source);
}

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.
    const URLRequest* request = *url_requests_->begin();
    int load_flags = request->load_flags();
    DEBUG_ALIAS_FOR_GURL(url_buf, request->url());
    base::debug::Alias(&num_requests);
    base::debug::Alias(&load_flags);
    NOTREACHED() << "Leaked " << num_requests << " URLRequest(s). First URL: "
                 << request->url().spec().c_str() << ".";
  }
}

void URLRequestContext::set_net_log(NetLog* net_log) {
  net_log_ = net_log;
}
void URLRequestContext::set_host_resolver(
    std::unique_ptr<HostResolver> host_resolver) {
  DCHECK(host_resolver.get());
  host_resolver_ = std::move(host_resolver);
}
void URLRequestContext::set_cert_verifier(
    std::unique_ptr<CertVerifier> cert_verifier) {
  cert_verifier_ = std::move(cert_verifier);
}
void URLRequestContext::set_proxy_resolution_service(
    std::unique_ptr<ProxyResolutionService> proxy_resolution_service) {
  proxy_resolution_service_ = std::move(proxy_resolution_service);
}
void URLRequestContext::set_proxy_delegate(
    std::unique_ptr<ProxyDelegate> proxy_delegate) {
  proxy_delegate_ = std::move(proxy_delegate);
}
void URLRequestContext::set_ssl_config_service(
    std::unique_ptr<SSLConfigService> service) {
  ssl_config_service_ = std::move(service);
}
void URLRequestContext::set_http_auth_handler_factory(
    std::unique_ptr<HttpAuthHandlerFactory> factory) {
  http_auth_handler_factory_ = std::move(factory);
}
void URLRequestContext::set_http_network_session(
    std::unique_ptr<HttpNetworkSession> http_network_session) {
  http_network_session_ = std::move(http_network_session);
}
void URLRequestContext::set_http_transaction_factory(
    std::unique_ptr<HttpTransactionFactory> factory) {
  http_transaction_factory_ = std::move(factory);
}
void URLRequestContext::set_network_delegate(
    std::unique_ptr<NetworkDelegate> network_delegate) {
  network_delegate_ = std::move(network_delegate);
}
void URLRequestContext::set_http_server_properties(
    std::unique_ptr<HttpServerProperties> http_server_properties) {
  http_server_properties_ = std::move(http_server_properties);
}
void URLRequestContext::set_cookie_store(
    std::unique_ptr<CookieStore> cookie_store) {
  cookie_store_ = std::move(cookie_store);
}
void URLRequestContext::set_transport_security_state(
    std::unique_ptr<TransportSecurityState> state) {
  transport_security_state_ = std::move(state);
}
void URLRequestContext::set_sct_auditing_delegate(
    std::unique_ptr<SCTAuditingDelegate> delegate) {
  sct_auditing_delegate_ = std::move(delegate);
}
void URLRequestContext::set_job_factory(
    std::unique_ptr<const URLRequestJobFactory> job_factory) {
  job_factory_storage_ = std::move(job_factory);
  job_factory_ = job_factory_storage_.get();
}
void URLRequestContext::set_quic_context(
    std::unique_ptr<QuicContext> quic_context) {
  quic_context_ = std::move(quic_context);
}
void URLRequestContext::set_http_user_agent_settings(
    std::unique_ptr<const HttpUserAgentSettings> http_user_agent_settings) {
  http_user_agent_settings_ = std::move(http_user_agent_settings);
}
void URLRequestContext::set_network_quality_estimator(
    NetworkQualityEstimator* network_quality_estimator) {
  network_quality_estimator_ = network_quality_estimator;
}
void URLRequestContext::set_client_socket_factory(
    std::unique_ptr<ClientSocketFactory> client_socket_factory) {
  client_socket_factory_ = std::move(client_socket_factory);
}
#if BUILDFLAG(ENABLE_REPORTING)
void URLRequestContext::set_persistent_reporting_and_nel_store(
    std::unique_ptr<PersistentReportingAndNelStore>
        persistent_reporting_and_nel_store) {
  persistent_reporting_and_nel_store_ =
      std::move(persistent_reporting_and_nel_store);
}
void URLRequestContext::set_reporting_service(
    std::unique_ptr<ReportingService> reporting_service) {
  reporting_service_ = std::move(reporting_service);
}
void URLRequestContext::set_network_error_logging_service(
    std::unique_ptr<NetworkErrorLoggingService> network_error_logging_service) {
  network_error_logging_service_ = std::move(network_error_logging_service);
}
#endif  // BUILDFLAG(ENABLE_REPORTING)

void URLRequestContext::set_transport_security_persister(
    std::unique_ptr<TransportSecurityPersister> transport_security_persister) {
  transport_security_persister_ = std::move(transport_security_persister);
}

#if BUILDFLAG(ENABLE_DEVICE_BOUND_SESSIONS)
void URLRequestContext::set_device_bound_session_service(
    std::unique_ptr<device_bound_sessions::SessionService>
        device_bound_session_service) {
  device_bound_session_service_ = std::move(device_bound_session_service);
}
void URLRequestContext::set_device_bound_session_store(
    std::unique_ptr<device_bound_sessions::SessionStore>
        device_bound_session_store) {
  device_bound_session_store_ = std::move(device_bound_session_store);
}
#endif  // BUILDFLAG(ENABLE_DEVICE_BOUND_SESSIONS)

}  // namespace net