Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(386)

Side by Side Diff: net/url_request/url_request_context_builder.cc

Issue 2951813002: Make URLRequestContextBuilder use base/task_scheduler/ (Closed)
Patch Set: Response to comments Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/url_request/url_request_context_builder.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/url_request/url_request_context_builder.h" 5 #include "net/url_request/url_request_context_builder.h"
6 6
7 #include <string> 7 #include <string>
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/compiler_specific.h" 11 #include "base/compiler_specific.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/macros.h" 13 #include "base/macros.h"
14 #include "base/memory/ptr_util.h" 14 #include "base/memory/ptr_util.h"
15 #include "base/message_loop/message_loop.h" 15 #include "base/message_loop/message_loop.h"
16 #include "base/single_thread_task_runner.h" 16 #include "base/single_thread_task_runner.h"
17 #include "base/strings/string_util.h" 17 #include "base/strings/string_util.h"
18 #include "base/threading/thread.h" 18 #include "base/task_scheduler/post_task.h"
19 #include "base/threading/thread_task_runner_handle.h"
20 #include "net/base/cache_type.h" 19 #include "net/base/cache_type.h"
21 #include "net/base/net_errors.h" 20 #include "net/base/net_errors.h"
22 #include "net/base/network_delegate_impl.h" 21 #include "net/base/network_delegate_impl.h"
23 #include "net/base/sdch_manager.h" 22 #include "net/base/sdch_manager.h"
24 #include "net/cert/cert_verifier.h" 23 #include "net/cert/cert_verifier.h"
25 #include "net/cert/ct_known_logs.h" 24 #include "net/cert/ct_known_logs.h"
26 #include "net/cert/ct_log_verifier.h" 25 #include "net/cert/ct_log_verifier.h"
27 #include "net/cert/ct_policy_enforcer.h" 26 #include "net/cert/ct_policy_enforcer.h"
28 #include "net/cert/ct_verifier.h" 27 #include "net/cert/ct_verifier.h"
29 #include "net/cert/multi_log_ct_verifier.h" 28 #include "net/cert/multi_log_ct_verifier.h"
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate); 136 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate);
138 }; 137 };
139 138
140 // A URLRequestContext subclass that owns most of its components 139 // A URLRequestContext subclass that owns most of its components
141 // via a UrlRequestContextStorage object. When URLRequestContextBuilder::Build() 140 // via a UrlRequestContextStorage object. When URLRequestContextBuilder::Build()
142 // is called, ownership of all URLRequestContext components is passed to the 141 // is called, ownership of all URLRequestContext components is passed to the
143 // ContainerURLRequestContext. Since this cancels requests in its destructor, 142 // ContainerURLRequestContext. Since this cancels requests in its destructor,
144 // it's not safe to subclass this. 143 // it's not safe to subclass this.
145 class ContainerURLRequestContext final : public URLRequestContext { 144 class ContainerURLRequestContext final : public URLRequestContext {
146 public: 145 public:
147 explicit ContainerURLRequestContext( 146 ContainerURLRequestContext() : storage_(this) {}
148 const scoped_refptr<base::SingleThreadTaskRunner>& file_task_runner)
149 : file_task_runner_(file_task_runner), storage_(this) {}
150 147
151 ~ContainerURLRequestContext() override { 148 ~ContainerURLRequestContext() override {
152 // Destroy the ReportingService before the rest of the URLRequestContext, so 149 // Destroy the ReportingService before the rest of the URLRequestContext, so
153 // it cancels any pending requests it may have. 150 // it cancels any pending requests it may have.
154 storage_.set_reporting_service(nullptr); 151 storage_.set_reporting_service(nullptr);
155 152
156 // Shut down the ProxyService, as it may have pending URLRequests using this 153 // Shut down the ProxyService, as it may have pending URLRequests using this
157 // context. Since this cancels requests, it's not safe to subclass this, as 154 // context. Since this cancels requests, it's not safe to subclass this, as
158 // some parts of the URLRequestContext may then be torn down before this 155 // some parts of the URLRequestContext may then be torn down before this
159 // cancels the ProxyService's URLRequests. 156 // cancels the ProxyService's URLRequests.
160 proxy_service()->OnShutdown(); 157 proxy_service()->OnShutdown();
161 158
162 AssertNoURLRequests(); 159 AssertNoURLRequests();
163 } 160 }
164 161
165 URLRequestContextStorage* storage() { 162 URLRequestContextStorage* storage() {
166 return &storage_; 163 return &storage_;
167 } 164 }
168 165
169 scoped_refptr<base::SingleThreadTaskRunner>& GetFileTaskRunner() {
170 // Create a new thread to run file tasks, if needed.
171 if (!file_task_runner_) {
172 DCHECK(!file_thread_);
173 file_thread_.reset(new base::Thread("Network File Thread"));
174 file_thread_->StartWithOptions(
175 base::Thread::Options(base::MessageLoop::TYPE_DEFAULT, 0));
176 file_task_runner_ = file_thread_->task_runner();
177 }
178 return file_task_runner_;
179 }
180
181 void set_transport_security_persister( 166 void set_transport_security_persister(
182 std::unique_ptr<TransportSecurityPersister> 167 std::unique_ptr<TransportSecurityPersister>
183 transport_security_persister) { 168 transport_security_persister) {
184 transport_security_persister_ = std::move(transport_security_persister); 169 transport_security_persister_ = std::move(transport_security_persister);
185 } 170 }
186 171
187 private: 172 private:
188 // The thread should be torn down last.
189 std::unique_ptr<base::Thread> file_thread_;
190 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_;
191
192 URLRequestContextStorage storage_; 173 URLRequestContextStorage storage_;
193 std::unique_ptr<TransportSecurityPersister> transport_security_persister_; 174 std::unique_ptr<TransportSecurityPersister> transport_security_persister_;
194 175
195 DISALLOW_COPY_AND_ASSIGN(ContainerURLRequestContext); 176 DISALLOW_COPY_AND_ASSIGN(ContainerURLRequestContext);
196 }; 177 };
197 178
198 } // namespace 179 } // namespace
199 180
200 URLRequestContextBuilder::HttpCacheParams::HttpCacheParams() 181 URLRequestContextBuilder::HttpCacheParams::HttpCacheParams()
201 : type(IN_MEMORY), 182 : type(IN_MEMORY),
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
315 http_auth_handler_factory_ = std::move(factory); 296 http_auth_handler_factory_ = std::move(factory);
316 } 297 }
317 298
318 void URLRequestContextBuilder::SetHttpServerProperties( 299 void URLRequestContextBuilder::SetHttpServerProperties(
319 std::unique_ptr<HttpServerProperties> http_server_properties) { 300 std::unique_ptr<HttpServerProperties> http_server_properties) {
320 http_server_properties_ = std::move(http_server_properties); 301 http_server_properties_ = std::move(http_server_properties);
321 } 302 }
322 303
323 std::unique_ptr<URLRequestContext> URLRequestContextBuilder::Build() { 304 std::unique_ptr<URLRequestContext> URLRequestContextBuilder::Build() {
324 std::unique_ptr<ContainerURLRequestContext> context( 305 std::unique_ptr<ContainerURLRequestContext> context(
325 new ContainerURLRequestContext(file_task_runner_)); 306 new ContainerURLRequestContext());
326 URLRequestContextStorage* storage = context->storage(); 307 URLRequestContextStorage* storage = context->storage();
327 308
328 context->set_name(name_); 309 context->set_name(name_);
329 context->set_enable_brotli(enable_brotli_); 310 context->set_enable_brotli(enable_brotli_);
330 context->set_network_quality_estimator(network_quality_estimator_); 311 context->set_network_quality_estimator(network_quality_estimator_);
331 312
332 storage->set_http_user_agent_settings( 313 storage->set_http_user_agent_settings(
333 base::MakeUnique<StaticHttpUserAgentSettings>(accept_language_, 314 base::MakeUnique<StaticHttpUserAgentSettings>(accept_language_,
334 user_agent_)); 315 user_agent_));
335 316
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
379 } 360 }
380 361
381 if (sdch_enabled_) { 362 if (sdch_enabled_) {
382 storage->set_sdch_manager( 363 storage->set_sdch_manager(
383 std::unique_ptr<net::SdchManager>(new SdchManager())); 364 std::unique_ptr<net::SdchManager>(new SdchManager()));
384 } 365 }
385 366
386 storage->set_transport_security_state( 367 storage->set_transport_security_state(
387 base::MakeUnique<TransportSecurityState>()); 368 base::MakeUnique<TransportSecurityState>());
388 if (!transport_security_persister_path_.empty()) { 369 if (!transport_security_persister_path_.empty()) {
370 // Use a low priority because saving this should not block anything
371 // user-visible. Block shutdown to ensure it does get persisted to disk,
372 // since it contains security-relevant information.
373 scoped_refptr<base::SequencedTaskRunner> task_runner(
374 GetFileSequencedTaskRunner(
375 {base::MayBlock(), base::TaskPriority::BACKGROUND,
376 base::TaskShutdownBehavior::BLOCK_SHUTDOWN}));
377
389 context->set_transport_security_persister( 378 context->set_transport_security_persister(
390 base::WrapUnique<TransportSecurityPersister>( 379 base::WrapUnique<TransportSecurityPersister>(
391 new TransportSecurityPersister(context->transport_security_state(), 380 new TransportSecurityPersister(context->transport_security_state(),
392 transport_security_persister_path_, 381 transport_security_persister_path_,
393 context->GetFileTaskRunner(), 382 task_runner, false)));
394 false)));
395 } 383 }
396 384
397 if (http_server_properties_) { 385 if (http_server_properties_) {
398 storage->set_http_server_properties(std::move(http_server_properties_)); 386 storage->set_http_server_properties(std::move(http_server_properties_));
399 } else { 387 } else {
400 storage->set_http_server_properties( 388 storage->set_http_server_properties(
401 std::unique_ptr<HttpServerProperties>(new HttpServerPropertiesImpl())); 389 std::unique_ptr<HttpServerProperties>(new HttpServerPropertiesImpl()));
402 } 390 }
403 391
404 if (cert_verifier_) { 392 if (cert_verifier_) {
(...skipping 17 matching lines...) Expand all
422 base::MakeUnique<URLRequestThrottlerManager>()); 410 base::MakeUnique<URLRequestThrottlerManager>());
423 } 411 }
424 412
425 if (!proxy_service_) { 413 if (!proxy_service_) {
426 #if !defined(OS_LINUX) && !defined(OS_ANDROID) 414 #if !defined(OS_LINUX) && !defined(OS_ANDROID)
427 // TODO(willchan): Switch to using this code when 415 // TODO(willchan): Switch to using this code when
428 // ProxyService::CreateSystemProxyConfigService()'s signature doesn't suck. 416 // ProxyService::CreateSystemProxyConfigService()'s signature doesn't suck.
429 if (!proxy_config_service_) { 417 if (!proxy_config_service_) {
430 proxy_config_service_ = ProxyService::CreateSystemProxyConfigService( 418 proxy_config_service_ = ProxyService::CreateSystemProxyConfigService(
431 base::ThreadTaskRunnerHandle::Get().get(), 419 base::ThreadTaskRunnerHandle::Get().get(),
432 context->GetFileTaskRunner()); 420 nullptr /* |file_task_runner| is only used on Linux */);
433 } 421 }
434 #endif // !defined(OS_LINUX) && !defined(OS_ANDROID) 422 #endif // !defined(OS_LINUX) && !defined(OS_ANDROID)
435 proxy_service_ = 423 proxy_service_ =
436 CreateProxyService(std::move(proxy_config_service_), context.get(), 424 CreateProxyService(std::move(proxy_config_service_), context.get(),
437 context->host_resolver(), 425 context->host_resolver(),
438 context->network_delegate(), context->net_log()); 426 context->network_delegate(), context->net_log());
439 proxy_service_->set_quick_check_enabled(pac_quick_check_enabled_); 427 proxy_service_->set_quick_check_enabled(pac_quick_check_enabled_);
440 proxy_service_->set_sanitize_url_policy(pac_sanitize_url_policy_); 428 proxy_service_->set_sanitize_url_policy(pac_sanitize_url_policy_);
441 } 429 }
442 storage->set_proxy_service(std::move(proxy_service_)); 430 storage->set_proxy_service(std::move(proxy_service_));
(...skipping 12 matching lines...) Expand all
455 std::unique_ptr<HttpTransactionFactory> http_transaction_factory; 443 std::unique_ptr<HttpTransactionFactory> http_transaction_factory;
456 if (http_cache_enabled_) { 444 if (http_cache_enabled_) {
457 std::unique_ptr<HttpCache::BackendFactory> http_cache_backend; 445 std::unique_ptr<HttpCache::BackendFactory> http_cache_backend;
458 if (http_cache_params_.type != HttpCacheParams::IN_MEMORY) { 446 if (http_cache_params_.type != HttpCacheParams::IN_MEMORY) {
459 BackendType backend_type = 447 BackendType backend_type =
460 http_cache_params_.type == HttpCacheParams::DISK 448 http_cache_params_.type == HttpCacheParams::DISK
461 ? CACHE_BACKEND_DEFAULT 449 ? CACHE_BACKEND_DEFAULT
462 : CACHE_BACKEND_SIMPLE; 450 : CACHE_BACKEND_SIMPLE;
463 http_cache_backend.reset(new HttpCache::DefaultBackend( 451 http_cache_backend.reset(new HttpCache::DefaultBackend(
464 DISK_CACHE, backend_type, http_cache_params_.path, 452 DISK_CACHE, backend_type, http_cache_params_.path,
465 http_cache_params_.max_size, context->GetFileTaskRunner())); 453 http_cache_params_.max_size,
454 GetFileSingleThreadTaskRunner(
455 {base::MayBlock(), base::TaskPriority::USER_BLOCKING,
456 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN})));
Randy Smith (Not in Mondays) 2017/06/21 15:48:52 How confident are you that the cache is resilient
Maks Orlovich 2017/06/21 15:58:28 In case of simple cache, it's "resilient" in the s
mmenke 2017/06/21 16:07:55 So I think you're right about this. I can't remem
466 } else { 457 } else {
467 http_cache_backend = 458 http_cache_backend =
468 HttpCache::DefaultBackend::InMemory(http_cache_params_.max_size); 459 HttpCache::DefaultBackend::InMemory(http_cache_params_.max_size);
469 } 460 }
470 461
471 http_transaction_factory.reset(new HttpCache( 462 http_transaction_factory.reset(new HttpCache(
472 storage->http_network_session(), std::move(http_cache_backend), true)); 463 storage->http_network_session(), std::move(http_cache_backend), true));
473 } else { 464 } else {
474 http_transaction_factory.reset( 465 http_transaction_factory.reset(
475 new HttpNetworkLayer(storage->http_network_session())); 466 new HttpNetworkLayer(storage->http_network_session()));
(...skipping 10 matching lines...) Expand all
486 protocol_handlers_.clear(); 477 protocol_handlers_.clear();
487 478
488 if (data_enabled_) 479 if (data_enabled_)
489 job_factory->SetProtocolHandler(url::kDataScheme, 480 job_factory->SetProtocolHandler(url::kDataScheme,
490 base::WrapUnique(new DataProtocolHandler)); 481 base::WrapUnique(new DataProtocolHandler));
491 482
492 #if !BUILDFLAG(DISABLE_FILE_SUPPORT) 483 #if !BUILDFLAG(DISABLE_FILE_SUPPORT)
493 if (file_enabled_) { 484 if (file_enabled_) {
494 job_factory->SetProtocolHandler( 485 job_factory->SetProtocolHandler(
495 url::kFileScheme, 486 url::kFileScheme,
496 base::MakeUnique<FileProtocolHandler>(context->GetFileTaskRunner())); 487 base::MakeUnique<FileProtocolHandler>(GetFileTaskRunner(
488 {base::MayBlock(), base::TaskPriority::USER_BLOCKING,
489 base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN})));
497 } 490 }
498 #endif // !BUILDFLAG(DISABLE_FILE_SUPPORT) 491 #endif // !BUILDFLAG(DISABLE_FILE_SUPPORT)
499 492
500 #if !BUILDFLAG(DISABLE_FTP_SUPPORT) 493 #if !BUILDFLAG(DISABLE_FTP_SUPPORT)
501 if (ftp_enabled_) { 494 if (ftp_enabled_) {
502 job_factory->SetProtocolHandler( 495 job_factory->SetProtocolHandler(
503 url::kFtpScheme, FtpProtocolHandler::Create(context->host_resolver())); 496 url::kFtpScheme, FtpProtocolHandler::Create(context->host_resolver()));
504 } 497 }
505 #endif // !BUILDFLAG(DISABLE_FTP_SUPPORT) 498 #endif // !BUILDFLAG(DISABLE_FTP_SUPPORT)
506 499
(...skipping 21 matching lines...) Expand all
528 std::unique_ptr<ProxyService> URLRequestContextBuilder::CreateProxyService( 521 std::unique_ptr<ProxyService> URLRequestContextBuilder::CreateProxyService(
529 std::unique_ptr<ProxyConfigService> proxy_config_service, 522 std::unique_ptr<ProxyConfigService> proxy_config_service,
530 URLRequestContext* url_request_context, 523 URLRequestContext* url_request_context,
531 HostResolver* host_resolver, 524 HostResolver* host_resolver,
532 NetworkDelegate* network_delegate, 525 NetworkDelegate* network_delegate,
533 NetLog* net_log) { 526 NetLog* net_log) {
534 return ProxyService::CreateUsingSystemProxyResolver( 527 return ProxyService::CreateUsingSystemProxyResolver(
535 std::move(proxy_config_service), net_log); 528 std::move(proxy_config_service), net_log);
536 } 529 }
537 530
531 scoped_refptr<base::TaskRunner> URLRequestContextBuilder::GetFileTaskRunner(
532 const base::TaskTraits& traits) {
533 if (file_task_runner_)
534 return file_task_runner_;
535 return base::CreateTaskRunnerWithTraits(traits);
536 }
537
538 scoped_refptr<base::SequencedTaskRunner>
539 URLRequestContextBuilder::GetFileSequencedTaskRunner(
540 const base::TaskTraits& traits) {
541 if (file_task_runner_)
542 return file_task_runner_;
543 return base::CreateSequencedTaskRunnerWithTraits(traits);
544 }
545
546 scoped_refptr<base::SingleThreadTaskRunner>
547 URLRequestContextBuilder::GetFileSingleThreadTaskRunner(
548 const base::TaskTraits& traits) {
549 if (file_task_runner_)
550 return file_task_runner_;
551 return base::CreateSingleThreadTaskRunnerWithTraits(traits);
552 }
553
538 } // namespace net 554 } // namespace net
OLDNEW
« no previous file with comments | « net/url_request/url_request_context_builder.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698