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

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

Issue 2951813002: Make URLRequestContextBuilder use base/task_scheduler/ (Closed)
Patch Set: Fix 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
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(GetSequencedTaskRunner(
374 {base::MayBlock(), base::TaskPriority::BACKGROUND,
375 base::TaskShutdownBehavior::BLOCK_SHUTDOWN}));
376
389 context->set_transport_security_persister( 377 context->set_transport_security_persister(
390 base::WrapUnique<TransportSecurityPersister>( 378 base::WrapUnique<TransportSecurityPersister>(
391 new TransportSecurityPersister(context->transport_security_state(), 379 new TransportSecurityPersister(context->transport_security_state(),
392 transport_security_persister_path_, 380 transport_security_persister_path_,
393 context->GetFileTaskRunner(), 381 task_runner, false)));
394 false)));
395 } 382 }
396 383
397 if (http_server_properties_) { 384 if (http_server_properties_) {
398 storage->set_http_server_properties(std::move(http_server_properties_)); 385 storage->set_http_server_properties(std::move(http_server_properties_));
399 } else { 386 } else {
400 storage->set_http_server_properties( 387 storage->set_http_server_properties(
401 std::unique_ptr<HttpServerProperties>(new HttpServerPropertiesImpl())); 388 std::unique_ptr<HttpServerProperties>(new HttpServerPropertiesImpl()));
402 } 389 }
403 390
404 if (cert_verifier_) { 391 if (cert_verifier_) {
(...skipping 17 matching lines...) Expand all
422 base::MakeUnique<URLRequestThrottlerManager>()); 409 base::MakeUnique<URLRequestThrottlerManager>());
423 } 410 }
424 411
425 if (!proxy_service_) { 412 if (!proxy_service_) {
426 #if !defined(OS_LINUX) && !defined(OS_ANDROID) 413 #if !defined(OS_LINUX) && !defined(OS_ANDROID)
427 // TODO(willchan): Switch to using this code when 414 // TODO(willchan): Switch to using this code when
428 // ProxyService::CreateSystemProxyConfigService()'s signature doesn't suck. 415 // ProxyService::CreateSystemProxyConfigService()'s signature doesn't suck.
429 if (!proxy_config_service_) { 416 if (!proxy_config_service_) {
430 proxy_config_service_ = ProxyService::CreateSystemProxyConfigService( 417 proxy_config_service_ = ProxyService::CreateSystemProxyConfigService(
431 base::ThreadTaskRunnerHandle::Get().get(), 418 base::ThreadTaskRunnerHandle::Get().get(),
432 context->GetFileTaskRunner()); 419 GetSingleThreadTaskRunner(
Randy Smith (Not in Mondays) 2017/06/21 12:56:26 When I first looked at this, I thought "Oh, of cou
mmenke 2017/06/21 15:13:55 ProxyService::CreateSystemProxyConfigService requi
420 {base::MayBlock(), base::TaskPriority::USER_BLOCKING,
421 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}));
Randy Smith (Not in Mondays) 2017/06/21 12:56:26 Why CONTINUE_ON_SHUTDOWN for the proxy service?
mmenke 2017/06/21 15:13:56 Because we aren't writing anything to disk, so no
433 } 422 }
434 #endif // !defined(OS_LINUX) && !defined(OS_ANDROID) 423 #endif // !defined(OS_LINUX) && !defined(OS_ANDROID)
435 proxy_service_ = 424 proxy_service_ =
436 CreateProxyService(std::move(proxy_config_service_), context.get(), 425 CreateProxyService(std::move(proxy_config_service_), context.get(),
437 context->host_resolver(), 426 context->host_resolver(),
438 context->network_delegate(), context->net_log()); 427 context->network_delegate(), context->net_log());
439 proxy_service_->set_quick_check_enabled(pac_quick_check_enabled_); 428 proxy_service_->set_quick_check_enabled(pac_quick_check_enabled_);
440 proxy_service_->set_sanitize_url_policy(pac_sanitize_url_policy_); 429 proxy_service_->set_sanitize_url_policy(pac_sanitize_url_policy_);
441 } 430 }
442 storage->set_proxy_service(std::move(proxy_service_)); 431 storage->set_proxy_service(std::move(proxy_service_));
(...skipping 12 matching lines...) Expand all
455 std::unique_ptr<HttpTransactionFactory> http_transaction_factory; 444 std::unique_ptr<HttpTransactionFactory> http_transaction_factory;
456 if (http_cache_enabled_) { 445 if (http_cache_enabled_) {
457 std::unique_ptr<HttpCache::BackendFactory> http_cache_backend; 446 std::unique_ptr<HttpCache::BackendFactory> http_cache_backend;
458 if (http_cache_params_.type != HttpCacheParams::IN_MEMORY) { 447 if (http_cache_params_.type != HttpCacheParams::IN_MEMORY) {
459 BackendType backend_type = 448 BackendType backend_type =
460 http_cache_params_.type == HttpCacheParams::DISK 449 http_cache_params_.type == HttpCacheParams::DISK
461 ? CACHE_BACKEND_DEFAULT 450 ? CACHE_BACKEND_DEFAULT
462 : CACHE_BACKEND_SIMPLE; 451 : CACHE_BACKEND_SIMPLE;
463 http_cache_backend.reset(new HttpCache::DefaultBackend( 452 http_cache_backend.reset(new HttpCache::DefaultBackend(
464 DISK_CACHE, backend_type, http_cache_params_.path, 453 DISK_CACHE, backend_type, http_cache_params_.path,
465 http_cache_params_.max_size, context->GetFileTaskRunner())); 454 http_cache_params_.max_size,
455 GetSingleThreadTaskRunner(
456 {base::MayBlock(), base::TaskPriority::USER_BLOCKING,
457 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN})));
466 } else { 458 } else {
467 http_cache_backend = 459 http_cache_backend =
468 HttpCache::DefaultBackend::InMemory(http_cache_params_.max_size); 460 HttpCache::DefaultBackend::InMemory(http_cache_params_.max_size);
469 } 461 }
470 462
471 http_transaction_factory.reset(new HttpCache( 463 http_transaction_factory.reset(new HttpCache(
472 storage->http_network_session(), std::move(http_cache_backend), true)); 464 storage->http_network_session(), std::move(http_cache_backend), true));
473 } else { 465 } else {
474 http_transaction_factory.reset( 466 http_transaction_factory.reset(
475 new HttpNetworkLayer(storage->http_network_session())); 467 new HttpNetworkLayer(storage->http_network_session()));
476 } 468 }
477 storage->set_http_transaction_factory(std::move(http_transaction_factory)); 469 storage->set_http_transaction_factory(std::move(http_transaction_factory));
478 470
479 URLRequestJobFactoryImpl* job_factory = new URLRequestJobFactoryImpl; 471 URLRequestJobFactoryImpl* job_factory = new URLRequestJobFactoryImpl;
480 // Adds caller-provided protocol handlers first so that these handlers are 472 // Adds caller-provided protocol handlers first so that these handlers are
481 // used over data/file/ftp handlers below. 473 // used over data/file/ftp handlers below.
482 for (auto& scheme_handler : protocol_handlers_) { 474 for (auto& scheme_handler : protocol_handlers_) {
483 job_factory->SetProtocolHandler(scheme_handler.first, 475 job_factory->SetProtocolHandler(scheme_handler.first,
484 std::move(scheme_handler.second)); 476 std::move(scheme_handler.second));
485 } 477 }
486 protocol_handlers_.clear(); 478 protocol_handlers_.clear();
487 479
488 if (data_enabled_) 480 if (data_enabled_)
489 job_factory->SetProtocolHandler(url::kDataScheme, 481 job_factory->SetProtocolHandler(url::kDataScheme,
490 base::WrapUnique(new DataProtocolHandler)); 482 base::WrapUnique(new DataProtocolHandler));
491 483
492 #if !BUILDFLAG(DISABLE_FILE_SUPPORT) 484 #if !BUILDFLAG(DISABLE_FILE_SUPPORT)
493 if (file_enabled_) { 485 if (file_enabled_) {
486 scoped_refptr<base::SequencedTaskRunner> task_runner(GetSequencedTaskRunner(
487 {base::MayBlock(), base::TaskPriority::BACKGROUND,
488 base::TaskShutdownBehavior::BLOCK_SHUTDOWN}));
Randy Smith (Not in Mondays) 2017/06/21 12:56:26 It's not doubt due to my mis-reading, but where is
mmenke 2017/06/21 15:13:56 Oops...Copy-paste error.
489
494 job_factory->SetProtocolHandler( 490 job_factory->SetProtocolHandler(
495 url::kFileScheme, 491 url::kFileScheme,
496 base::MakeUnique<FileProtocolHandler>(context->GetFileTaskRunner())); 492 base::MakeUnique<FileProtocolHandler>(
493 GetTaskRunner({base::MayBlock(), base::TaskPriority::USER_VISIBLE,
494 base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN})));
Randy Smith (Not in Mondays) 2017/06/21 12:56:26 Hmmm. Everything else was USER_BLOCKING, which ma
mmenke 2017/06/21 15:13:55 So, actually, despite the CL description, this was
Randy Smith (Not in Mondays) 2017/06/21 15:48:52 Ah, I missed that. I'd suggest being pretty clear
497 } 495 }
498 #endif // !BUILDFLAG(DISABLE_FILE_SUPPORT) 496 #endif // !BUILDFLAG(DISABLE_FILE_SUPPORT)
499 497
500 #if !BUILDFLAG(DISABLE_FTP_SUPPORT) 498 #if !BUILDFLAG(DISABLE_FTP_SUPPORT)
501 if (ftp_enabled_) { 499 if (ftp_enabled_) {
502 job_factory->SetProtocolHandler( 500 job_factory->SetProtocolHandler(
503 url::kFtpScheme, FtpProtocolHandler::Create(context->host_resolver())); 501 url::kFtpScheme, FtpProtocolHandler::Create(context->host_resolver()));
504 } 502 }
505 #endif // !BUILDFLAG(DISABLE_FTP_SUPPORT) 503 #endif // !BUILDFLAG(DISABLE_FTP_SUPPORT)
506 504
(...skipping 21 matching lines...) Expand all
528 std::unique_ptr<ProxyService> URLRequestContextBuilder::CreateProxyService( 526 std::unique_ptr<ProxyService> URLRequestContextBuilder::CreateProxyService(
529 std::unique_ptr<ProxyConfigService> proxy_config_service, 527 std::unique_ptr<ProxyConfigService> proxy_config_service,
530 URLRequestContext* url_request_context, 528 URLRequestContext* url_request_context,
531 HostResolver* host_resolver, 529 HostResolver* host_resolver,
532 NetworkDelegate* network_delegate, 530 NetworkDelegate* network_delegate,
533 NetLog* net_log) { 531 NetLog* net_log) {
534 return ProxyService::CreateUsingSystemProxyResolver( 532 return ProxyService::CreateUsingSystemProxyResolver(
535 std::move(proxy_config_service), net_log); 533 std::move(proxy_config_service), net_log);
536 } 534 }
537 535
536 scoped_refptr<base::TaskRunner> URLRequestContextBuilder::GetTaskRunner(
537 const base::TaskTraits& traits) {
538 if (file_task_runner_)
539 return file_task_runner_;
540 return base::CreateTaskRunnerWithTraits(traits);
541 }
542
543 scoped_refptr<base::SequencedTaskRunner>
544 URLRequestContextBuilder::GetSequencedTaskRunner(
545 const base::TaskTraits& traits) {
546 if (file_task_runner_)
547 return file_task_runner_;
548 return base::CreateSequencedTaskRunnerWithTraits(traits);
549 }
550
551 scoped_refptr<base::SingleThreadTaskRunner>
552 URLRequestContextBuilder::GetSingleThreadTaskRunner(
553 const base::TaskTraits& traits) {
554 if (file_task_runner_)
555 return file_task_runner_;
556 return base::CreateSingleThreadTaskRunnerWithTraits(traits);
557 }
558
538 } // namespace net 559 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698