| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/io_thread.h" | 5 #include "chrome/browser/io_thread.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 12 #include "base/debug/leak_tracker.h" | 12 #include "base/debug/leak_tracker.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/metrics/field_trial.h" | 14 #include "base/metrics/field_trial.h" |
| 15 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
| 16 #include "base/string_number_conversions.h" | 16 #include "base/string_number_conversions.h" |
| 17 #include "base/string_split.h" | 17 #include "base/string_split.h" |
| 18 #include "base/string_util.h" | 18 #include "base/string_util.h" |
| 19 #include "base/threading/thread.h" |
| 19 #include "base/threading/thread_restrictions.h" | 20 #include "base/threading/thread_restrictions.h" |
| 20 #include "build/build_config.h" | 21 #include "build/build_config.h" |
| 21 #include "chrome/browser/browser_process.h" | 22 #include "chrome/browser/browser_process.h" |
| 22 #include "chrome/browser/extensions/extension_event_router_forwarder.h" | 23 #include "chrome/browser/extensions/extension_event_router_forwarder.h" |
| 23 #include "chrome/browser/media/media_internals.h" | 24 #include "chrome/browser/media/media_internals.h" |
| 24 #include "chrome/browser/net/chrome_net_log.h" | 25 #include "chrome/browser/net/chrome_net_log.h" |
| 25 #include "chrome/browser/net/chrome_network_delegate.h" | 26 #include "chrome/browser/net/chrome_network_delegate.h" |
| 26 #include "chrome/browser/net/chrome_url_request_context.h" | 27 #include "chrome/browser/net/chrome_url_request_context.h" |
| 27 #include "chrome/browser/net/connect_interceptor.h" | 28 #include "chrome/browser/net/connect_interceptor.h" |
| 28 #include "chrome/browser/net/passive_log_collector.h" | 29 #include "chrome/browser/net/passive_log_collector.h" |
| (...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 309 private: | 310 private: |
| 310 IOThread* const io_thread_; // Weak pointer, owned by BrowserProcess. | 311 IOThread* const io_thread_; // Weak pointer, owned by BrowserProcess. |
| 311 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; | 312 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; |
| 312 | 313 |
| 313 base::debug::LeakTracker<SystemURLRequestContextGetter> leak_tracker_; | 314 base::debug::LeakTracker<SystemURLRequestContextGetter> leak_tracker_; |
| 314 }; | 315 }; |
| 315 | 316 |
| 316 SystemURLRequestContextGetter::SystemURLRequestContextGetter( | 317 SystemURLRequestContextGetter::SystemURLRequestContextGetter( |
| 317 IOThread* io_thread) | 318 IOThread* io_thread) |
| 318 : io_thread_(io_thread), | 319 : io_thread_(io_thread), |
| 319 io_message_loop_proxy_(io_thread->message_loop_proxy()) { | 320 io_message_loop_proxy_( |
| 321 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)) { |
| 320 } | 322 } |
| 321 | 323 |
| 322 SystemURLRequestContextGetter::~SystemURLRequestContextGetter() {} | 324 SystemURLRequestContextGetter::~SystemURLRequestContextGetter() {} |
| 323 | 325 |
| 324 net::URLRequestContext* SystemURLRequestContextGetter::GetURLRequestContext() { | 326 net::URLRequestContext* SystemURLRequestContextGetter::GetURLRequestContext() { |
| 325 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 327 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 326 DCHECK(io_thread_->globals()->system_request_context); | 328 DCHECK(io_thread_->globals()->system_request_context); |
| 327 | 329 |
| 328 return io_thread_->globals()->system_request_context; | 330 return io_thread_->globals()->system_request_context; |
| 329 } | 331 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 340 IOThread::Globals::MediaGlobals::MediaGlobals() {} | 342 IOThread::Globals::MediaGlobals::MediaGlobals() {} |
| 341 | 343 |
| 342 IOThread::Globals::MediaGlobals::~MediaGlobals() {} | 344 IOThread::Globals::MediaGlobals::~MediaGlobals() {} |
| 343 | 345 |
| 344 // |local_state| is passed in explicitly in order to (1) reduce implicit | 346 // |local_state| is passed in explicitly in order to (1) reduce implicit |
| 345 // dependencies and (2) make IOThread more flexible for testing. | 347 // dependencies and (2) make IOThread more flexible for testing. |
| 346 IOThread::IOThread( | 348 IOThread::IOThread( |
| 347 PrefService* local_state, | 349 PrefService* local_state, |
| 348 ChromeNetLog* net_log, | 350 ChromeNetLog* net_log, |
| 349 ExtensionEventRouterForwarder* extension_event_router_forwarder) | 351 ExtensionEventRouterForwarder* extension_event_router_forwarder) |
| 350 : content::BrowserProcessSubThread(BrowserThread::IO), | 352 : net_log_(net_log), |
| 351 net_log_(net_log), | |
| 352 extension_event_router_forwarder_(extension_event_router_forwarder), | 353 extension_event_router_forwarder_(extension_event_router_forwarder), |
| 353 globals_(NULL), | 354 globals_(NULL), |
| 354 sdch_manager_(NULL), | 355 sdch_manager_(NULL), |
| 355 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 356 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
| 356 // We call RegisterPrefs() here (instead of inside browser_prefs.cc) to make | 357 // We call RegisterPrefs() here (instead of inside browser_prefs.cc) to make |
| 357 // sure that everything is initialized in the right order. | 358 // sure that everything is initialized in the right order. |
| 358 RegisterPrefs(local_state); | 359 RegisterPrefs(local_state); |
| 359 auth_schemes_ = local_state->GetString(prefs::kAuthSchemes); | 360 auth_schemes_ = local_state->GetString(prefs::kAuthSchemes); |
| 360 negotiate_disable_cname_lookup_ = local_state->GetBoolean( | 361 negotiate_disable_cname_lookup_ = local_state->GetBoolean( |
| 361 prefs::kDisableAuthNegotiateCnameLookup); | 362 prefs::kDisableAuthNegotiateCnameLookup); |
| 362 negotiate_enable_port_ = local_state->GetBoolean( | 363 negotiate_enable_port_ = local_state->GetBoolean( |
| 363 prefs::kEnableAuthNegotiatePort); | 364 prefs::kEnableAuthNegotiatePort); |
| 364 auth_server_whitelist_ = local_state->GetString(prefs::kAuthServerWhitelist); | 365 auth_server_whitelist_ = local_state->GetString(prefs::kAuthServerWhitelist); |
| 365 auth_delegate_whitelist_ = local_state->GetString( | 366 auth_delegate_whitelist_ = local_state->GetString( |
| 366 prefs::kAuthNegotiateDelegateWhitelist); | 367 prefs::kAuthNegotiateDelegateWhitelist); |
| 367 gssapi_library_name_ = local_state->GetString(prefs::kGSSAPILibraryName); | 368 gssapi_library_name_ = local_state->GetString(prefs::kGSSAPILibraryName); |
| 368 pref_proxy_config_tracker_.reset( | 369 pref_proxy_config_tracker_.reset( |
| 369 ProxyServiceFactory::CreatePrefProxyConfigTracker(local_state)); | 370 ProxyServiceFactory::CreatePrefProxyConfigTracker(local_state)); |
| 370 ChromeNetworkDelegate::InitializeReferrersEnabled(&system_enable_referrers_, | 371 ChromeNetworkDelegate::InitializeReferrersEnabled(&system_enable_referrers_, |
| 371 local_state); | 372 local_state); |
| 372 ssl_config_service_manager_.reset( | 373 ssl_config_service_manager_.reset( |
| 373 SSLConfigServiceManager::CreateDefaultManager(local_state)); | 374 SSLConfigServiceManager::CreateDefaultManager(local_state)); |
| 374 MessageLoop::current()->PostTask( | |
| 375 FROM_HERE, base::Bind(&IOThread::InitSystemRequestContext, | |
| 376 weak_factory_.GetWeakPtr())); | |
| 377 } | 375 } |
| 378 | 376 |
| 379 IOThread::~IOThread() { | 377 IOThread::~IOThread() { |
| 380 if (pref_proxy_config_tracker_.get()) | 378 if (pref_proxy_config_tracker_.get()) |
| 381 pref_proxy_config_tracker_->DetachFromPrefService(); | 379 pref_proxy_config_tracker_->DetachFromPrefService(); |
| 382 // We cannot rely on our base class to stop the thread since we want our | |
| 383 // CleanUp function to run. | |
| 384 Stop(); | |
| 385 DCHECK(!globals_); | 380 DCHECK(!globals_); |
| 386 } | 381 } |
| 387 | 382 |
| 388 IOThread::Globals* IOThread::globals() { | 383 IOThread::Globals* IOThread::globals() { |
| 389 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 384 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 390 return globals_; | 385 return globals_; |
| 391 } | 386 } |
| 392 | 387 |
| 393 ChromeNetLog* IOThread::net_log() { | 388 ChromeNetLog* IOThread::net_log() { |
| 394 return net_log_; | 389 return net_log_; |
| 395 } | 390 } |
| 396 | 391 |
| 397 net::URLRequestContextGetter* IOThread::system_url_request_context_getter() { | 392 net::URLRequestContextGetter* IOThread::system_url_request_context_getter() { |
| 398 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 393 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 399 if (!system_url_request_context_getter_) { | 394 if (!system_url_request_context_getter_) { |
| 400 InitSystemRequestContext(); | 395 InitSystemRequestContext(); |
| 401 } | 396 } |
| 402 return system_url_request_context_getter_; | 397 return system_url_request_context_getter_; |
| 403 } | 398 } |
| 404 | 399 |
| 405 void IOThread::Init() { | 400 void IOThread::InitializeGlobalState() { |
| 406 // Though this thread is called the "IO" thread, it actually just routes | 401 // Though this thread is called the "IO" thread, it actually just routes |
| 407 // messages around; it shouldn't be allowed to perform any blocking disk I/O. | 402 // messages around; it shouldn't be allowed to perform any blocking disk I/O. |
| 408 base::ThreadRestrictions::SetIOAllowed(false); | 403 base::ThreadRestrictions::SetIOAllowed(false); |
| 409 | 404 |
| 410 content::BrowserProcessSubThread::Init(); | 405 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 411 | |
| 412 DCHECK_EQ(MessageLoop::TYPE_IO, message_loop()->type()); | |
| 413 | 406 |
| 414 #if defined(USE_NSS) | 407 #if defined(USE_NSS) |
| 415 net::SetMessageLoopForOCSP(); | 408 net::SetMessageLoopForOCSP(); |
| 416 #endif // defined(USE_NSS) | 409 #endif // defined(USE_NSS) |
| 417 | 410 |
| 418 DCHECK(!globals_); | 411 DCHECK(!globals_); |
| 419 globals_ = new Globals; | 412 globals_ = new Globals; |
| 420 | 413 |
| 421 globals_->media.media_internals.reset(new MediaInternals()); | 414 globals_->media.media_internals.reset(new MediaInternals()); |
| 422 | 415 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 473 globals_->proxy_script_fetcher_http_transaction_factory.reset( | 466 globals_->proxy_script_fetcher_http_transaction_factory.reset( |
| 474 new net::HttpNetworkLayer(network_session)); | 467 new net::HttpNetworkLayer(network_session)); |
| 475 globals_->proxy_script_fetcher_ftp_transaction_factory.reset( | 468 globals_->proxy_script_fetcher_ftp_transaction_factory.reset( |
| 476 new net::FtpNetworkLayer(globals_->host_resolver.get())); | 469 new net::FtpNetworkLayer(globals_->host_resolver.get())); |
| 477 | 470 |
| 478 globals_->proxy_script_fetcher_context = | 471 globals_->proxy_script_fetcher_context = |
| 479 ConstructProxyScriptFetcherContext(globals_, net_log_); | 472 ConstructProxyScriptFetcherContext(globals_, net_log_); |
| 480 | 473 |
| 481 sdch_manager_ = new net::SdchManager(); | 474 sdch_manager_ = new net::SdchManager(); |
| 482 sdch_manager_->set_sdch_fetcher(new SdchDictionaryFetcher); | 475 sdch_manager_->set_sdch_fetcher(new SdchDictionaryFetcher); |
| 476 |
| 477 // The io_thread_ object is guaranteed to outlive the thread, as |
| 478 // it is destroyed only during the call to PostStopThread(IO), |
| 479 // at which point the IO thread has stopped. |
| 480 //message_loop()->AddDestructionObserver(this); |
| 483 } | 481 } |
| 484 | 482 |
| 485 void IOThread::CleanUp() { | 483 void IOThread::WillDestroyCurrentMessageLoop() { |
| 484 MessageLoop::current()->RemoveDestructionObserver(this); |
| 485 |
| 486 delete sdch_manager_; | 486 delete sdch_manager_; |
| 487 sdch_manager_ = NULL; | 487 sdch_manager_ = NULL; |
| 488 | 488 |
| 489 // Step 1: Kill all things that might be holding onto | 489 // Step 1: Kill all things that might be holding onto |
| 490 // net::URLRequest/net::URLRequestContexts. | 490 // net::URLRequest/net::URLRequestContexts. |
| 491 | 491 |
| 492 #if defined(USE_NSS) | 492 #if defined(USE_NSS) |
| 493 net::ShutdownOCSP(); | 493 net::ShutdownOCSP(); |
| 494 #endif // defined(USE_NSS) | 494 #endif // defined(USE_NSS) |
| 495 | 495 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 513 | 513 |
| 514 system_proxy_config_service_.reset(); | 514 system_proxy_config_service_.reset(); |
| 515 | 515 |
| 516 delete globals_; | 516 delete globals_; |
| 517 globals_ = NULL; | 517 globals_ = NULL; |
| 518 | 518 |
| 519 // net::URLRequest instances must NOT outlive the IO thread. | 519 // net::URLRequest instances must NOT outlive the IO thread. |
| 520 base::debug::LeakTracker<net::URLRequest>::CheckForLeaks(); | 520 base::debug::LeakTracker<net::URLRequest>::CheckForLeaks(); |
| 521 | 521 |
| 522 base::debug::LeakTracker<SystemURLRequestContextGetter>::CheckForLeaks(); | 522 base::debug::LeakTracker<SystemURLRequestContextGetter>::CheckForLeaks(); |
| 523 | |
| 524 // This will delete the |notification_service_|. Make sure it's done after | |
| 525 // anything else can reference it. | |
| 526 content::BrowserProcessSubThread::CleanUp(); | |
| 527 } | 523 } |
| 528 | 524 |
| 529 // static | 525 // static |
| 530 void IOThread::RegisterPrefs(PrefService* local_state) { | 526 void IOThread::RegisterPrefs(PrefService* local_state) { |
| 531 local_state->RegisterStringPref(prefs::kAuthSchemes, | 527 local_state->RegisterStringPref(prefs::kAuthSchemes, |
| 532 "basic,digest,ntlm,negotiate"); | 528 "basic,digest,ntlm,negotiate"); |
| 533 local_state->RegisterBooleanPref(prefs::kDisableAuthNegotiateCnameLookup, | 529 local_state->RegisterBooleanPref(prefs::kDisableAuthNegotiateCnameLookup, |
| 534 false); | 530 false); |
| 535 local_state->RegisterBooleanPref(prefs::kEnableAuthNegotiatePort, false); | 531 local_state->RegisterBooleanPref(prefs::kEnableAuthNegotiatePort, false); |
| 536 local_state->RegisterStringPref(prefs::kAuthServerWhitelist, ""); | 532 local_state->RegisterStringPref(prefs::kAuthServerWhitelist, ""); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 567 } | 563 } |
| 568 | 564 |
| 569 void IOThread::ClearHostCache() { | 565 void IOThread::ClearHostCache() { |
| 570 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 566 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 571 | 567 |
| 572 net::HostCache* host_cache = globals_->host_resolver->GetHostCache(); | 568 net::HostCache* host_cache = globals_->host_resolver->GetHostCache(); |
| 573 if (host_cache) | 569 if (host_cache) |
| 574 host_cache->clear(); | 570 host_cache->clear(); |
| 575 } | 571 } |
| 576 | 572 |
| 573 MessageLoop* IOThread::message_loop() const { |
| 574 return BrowserThread::UnsafeGetBrowserThread( |
| 575 BrowserThread::IO)->message_loop(); |
| 576 } |
| 577 |
| 577 net::SSLConfigService* IOThread::GetSSLConfigService() { | 578 net::SSLConfigService* IOThread::GetSSLConfigService() { |
| 578 return ssl_config_service_manager_->Get(); | 579 return ssl_config_service_manager_->Get(); |
| 579 } | 580 } |
| 580 | 581 |
| 581 void IOThread::InitSystemRequestContext() { | 582 void IOThread::InitSystemRequestContext() { |
| 582 if (system_url_request_context_getter_) | 583 if (system_url_request_context_getter_) |
| 583 return; | 584 return; |
| 584 // If we're in unit_tests, IOThread may not be run. | 585 // If we're in unit_tests, IOThread may not be run. |
| 585 if (!message_loop()) | 586 if (!message_loop()) |
| 586 return; | 587 return; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 626 system_params.network_delegate = globals_->system_network_delegate.get(); | 627 system_params.network_delegate = globals_->system_network_delegate.get(); |
| 627 system_params.net_log = net_log_; | 628 system_params.net_log = net_log_; |
| 628 globals_->system_http_transaction_factory.reset( | 629 globals_->system_http_transaction_factory.reset( |
| 629 new net::HttpNetworkLayer( | 630 new net::HttpNetworkLayer( |
| 630 new net::HttpNetworkSession(system_params))); | 631 new net::HttpNetworkSession(system_params))); |
| 631 globals_->system_ftp_transaction_factory.reset( | 632 globals_->system_ftp_transaction_factory.reset( |
| 632 new net::FtpNetworkLayer(globals_->host_resolver.get())); | 633 new net::FtpNetworkLayer(globals_->host_resolver.get())); |
| 633 globals_->system_request_context = | 634 globals_->system_request_context = |
| 634 ConstructSystemRequestContext(globals_, net_log_); | 635 ConstructSystemRequestContext(globals_, net_log_); |
| 635 } | 636 } |
| OLD | NEW |