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

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

Issue 2951813002: Make URLRequestContextBuilder use base/task_scheduler/ (Closed)
Patch Set: Merge Created 3 years, 5 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(
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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
454 std::unique_ptr<HttpTransactionFactory> http_transaction_factory; 442 std::unique_ptr<HttpTransactionFactory> http_transaction_factory;
455 if (http_cache_enabled_) { 443 if (http_cache_enabled_) {
456 std::unique_ptr<HttpCache::BackendFactory> http_cache_backend; 444 std::unique_ptr<HttpCache::BackendFactory> http_cache_backend;
457 if (http_cache_params_.type != HttpCacheParams::IN_MEMORY) { 445 if (http_cache_params_.type != HttpCacheParams::IN_MEMORY) {
458 BackendType backend_type = 446 BackendType backend_type =
459 http_cache_params_.type == HttpCacheParams::DISK 447 http_cache_params_.type == HttpCacheParams::DISK
460 ? CACHE_BACKEND_DEFAULT 448 ? CACHE_BACKEND_DEFAULT
461 : CACHE_BACKEND_SIMPLE; 449 : CACHE_BACKEND_SIMPLE;
462 http_cache_backend.reset(new HttpCache::DefaultBackend( 450 http_cache_backend.reset(new HttpCache::DefaultBackend(
463 DISK_CACHE, backend_type, http_cache_params_.path, 451 DISK_CACHE, backend_type, http_cache_params_.path,
464 http_cache_params_.max_size, context->GetFileTaskRunner())); 452 http_cache_params_.max_size,
453 GetFileSingleThreadTaskRunner(
454 {base::MayBlock(), base::TaskPriority::USER_BLOCKING,
455 base::TaskShutdownBehavior::BLOCK_SHUTDOWN})));
465 } else { 456 } else {
466 http_cache_backend = 457 http_cache_backend =
467 HttpCache::DefaultBackend::InMemory(http_cache_params_.max_size); 458 HttpCache::DefaultBackend::InMemory(http_cache_params_.max_size);
468 } 459 }
469 460
470 http_transaction_factory.reset(new HttpCache( 461 http_transaction_factory.reset(new HttpCache(
471 storage->http_network_session(), std::move(http_cache_backend), true)); 462 storage->http_network_session(), std::move(http_cache_backend), true));
472 } else { 463 } else {
473 http_transaction_factory.reset( 464 http_transaction_factory.reset(
474 new HttpNetworkLayer(storage->http_network_session())); 465 new HttpNetworkLayer(storage->http_network_session()));
(...skipping 10 matching lines...) Expand all
485 protocol_handlers_.clear(); 476 protocol_handlers_.clear();
486 477
487 if (data_enabled_) 478 if (data_enabled_)
488 job_factory->SetProtocolHandler(url::kDataScheme, 479 job_factory->SetProtocolHandler(url::kDataScheme,
489 base::WrapUnique(new DataProtocolHandler)); 480 base::WrapUnique(new DataProtocolHandler));
490 481
491 #if !BUILDFLAG(DISABLE_FILE_SUPPORT) 482 #if !BUILDFLAG(DISABLE_FILE_SUPPORT)
492 if (file_enabled_) { 483 if (file_enabled_) {
493 job_factory->SetProtocolHandler( 484 job_factory->SetProtocolHandler(
494 url::kFileScheme, 485 url::kFileScheme,
495 base::MakeUnique<FileProtocolHandler>(context->GetFileTaskRunner())); 486 base::MakeUnique<FileProtocolHandler>(GetFileTaskRunner(
487 {base::MayBlock(), base::TaskPriority::USER_BLOCKING,
488 base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN})));
496 } 489 }
497 #endif // !BUILDFLAG(DISABLE_FILE_SUPPORT) 490 #endif // !BUILDFLAG(DISABLE_FILE_SUPPORT)
498 491
499 #if !BUILDFLAG(DISABLE_FTP_SUPPORT) 492 #if !BUILDFLAG(DISABLE_FTP_SUPPORT)
500 if (ftp_enabled_) { 493 if (ftp_enabled_) {
501 job_factory->SetProtocolHandler( 494 job_factory->SetProtocolHandler(
502 url::kFtpScheme, FtpProtocolHandler::Create(context->host_resolver())); 495 url::kFtpScheme, FtpProtocolHandler::Create(context->host_resolver()));
503 } 496 }
504 #endif // !BUILDFLAG(DISABLE_FTP_SUPPORT) 497 #endif // !BUILDFLAG(DISABLE_FTP_SUPPORT)
505 498
(...skipping 21 matching lines...) Expand all
527 std::unique_ptr<ProxyService> URLRequestContextBuilder::CreateProxyService( 520 std::unique_ptr<ProxyService> URLRequestContextBuilder::CreateProxyService(
528 std::unique_ptr<ProxyConfigService> proxy_config_service, 521 std::unique_ptr<ProxyConfigService> proxy_config_service,
529 URLRequestContext* url_request_context, 522 URLRequestContext* url_request_context,
530 HostResolver* host_resolver, 523 HostResolver* host_resolver,
531 NetworkDelegate* network_delegate, 524 NetworkDelegate* network_delegate,
532 NetLog* net_log) { 525 NetLog* net_log) {
533 return ProxyService::CreateUsingSystemProxyResolver( 526 return ProxyService::CreateUsingSystemProxyResolver(
534 std::move(proxy_config_service), net_log); 527 std::move(proxy_config_service), net_log);
535 } 528 }
536 529
530 scoped_refptr<base::TaskRunner> URLRequestContextBuilder::GetFileTaskRunner(
531 const base::TaskTraits& traits) {
532 if (file_task_runner_)
533 return file_task_runner_;
534 return base::CreateTaskRunnerWithTraits(traits);
535 }
536
537 scoped_refptr<base::SequencedTaskRunner>
538 URLRequestContextBuilder::GetFileSequencedTaskRunner(
539 const base::TaskTraits& traits) {
540 if (file_task_runner_)
541 return file_task_runner_;
542 return base::CreateSequencedTaskRunnerWithTraits(traits);
543 }
544
545 scoped_refptr<base::SingleThreadTaskRunner>
546 URLRequestContextBuilder::GetFileSingleThreadTaskRunner(
547 const base::TaskTraits& traits) {
548 if (file_task_runner_)
549 return file_task_runner_;
550 return base::CreateSingleThreadTaskRunnerWithTraits(traits);
551 }
552
537 } // namespace net 553 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698