| 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" |
| (...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 309 private: | 309 private: |
| 310 IOThread* const io_thread_; // Weak pointer, owned by BrowserProcess. | 310 IOThread* const io_thread_; // Weak pointer, owned by BrowserProcess. |
| 311 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; | 311 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; |
| 312 | 312 |
| 313 base::debug::LeakTracker<SystemURLRequestContextGetter> leak_tracker_; | 313 base::debug::LeakTracker<SystemURLRequestContextGetter> leak_tracker_; |
| 314 }; | 314 }; |
| 315 | 315 |
| 316 SystemURLRequestContextGetter::SystemURLRequestContextGetter( | 316 SystemURLRequestContextGetter::SystemURLRequestContextGetter( |
| 317 IOThread* io_thread) | 317 IOThread* io_thread) |
| 318 : io_thread_(io_thread), | 318 : io_thread_(io_thread), |
| 319 io_message_loop_proxy_( | 319 io_message_loop_proxy_(io_thread->message_loop_proxy()) { |
| 320 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)) { | |
| 321 } | 320 } |
| 322 | 321 |
| 323 SystemURLRequestContextGetter::~SystemURLRequestContextGetter() {} | 322 SystemURLRequestContextGetter::~SystemURLRequestContextGetter() {} |
| 324 | 323 |
| 325 net::URLRequestContext* SystemURLRequestContextGetter::GetURLRequestContext() { | 324 net::URLRequestContext* SystemURLRequestContextGetter::GetURLRequestContext() { |
| 326 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 325 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 327 DCHECK(io_thread_->globals()->system_request_context); | 326 DCHECK(io_thread_->globals()->system_request_context); |
| 328 | 327 |
| 329 return io_thread_->globals()->system_request_context; | 328 return io_thread_->globals()->system_request_context; |
| 330 } | 329 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 341 IOThread::Globals::MediaGlobals::MediaGlobals() {} | 340 IOThread::Globals::MediaGlobals::MediaGlobals() {} |
| 342 | 341 |
| 343 IOThread::Globals::MediaGlobals::~MediaGlobals() {} | 342 IOThread::Globals::MediaGlobals::~MediaGlobals() {} |
| 344 | 343 |
| 345 // |local_state| is passed in explicitly in order to (1) reduce implicit | 344 // |local_state| is passed in explicitly in order to (1) reduce implicit |
| 346 // dependencies and (2) make IOThread more flexible for testing. | 345 // dependencies and (2) make IOThread more flexible for testing. |
| 347 IOThread::IOThread( | 346 IOThread::IOThread( |
| 348 PrefService* local_state, | 347 PrefService* local_state, |
| 349 ChromeNetLog* net_log, | 348 ChromeNetLog* net_log, |
| 350 ExtensionEventRouterForwarder* extension_event_router_forwarder) | 349 ExtensionEventRouterForwarder* extension_event_router_forwarder) |
| 351 : net_log_(net_log), | 350 : content::BrowserProcessSubThread(BrowserThread::IO), |
| 351 net_log_(net_log), |
| 352 extension_event_router_forwarder_(extension_event_router_forwarder), | 352 extension_event_router_forwarder_(extension_event_router_forwarder), |
| 353 globals_(NULL), | 353 globals_(NULL), |
| 354 sdch_manager_(NULL), | 354 sdch_manager_(NULL), |
| 355 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 355 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
| 356 // We call RegisterPrefs() here (instead of inside browser_prefs.cc) to make | 356 // We call RegisterPrefs() here (instead of inside browser_prefs.cc) to make |
| 357 // sure that everything is initialized in the right order. | 357 // sure that everything is initialized in the right order. |
| 358 RegisterPrefs(local_state); | 358 RegisterPrefs(local_state); |
| 359 auth_schemes_ = local_state->GetString(prefs::kAuthSchemes); | 359 auth_schemes_ = local_state->GetString(prefs::kAuthSchemes); |
| 360 negotiate_disable_cname_lookup_ = local_state->GetBoolean( | 360 negotiate_disable_cname_lookup_ = local_state->GetBoolean( |
| 361 prefs::kDisableAuthNegotiateCnameLookup); | 361 prefs::kDisableAuthNegotiateCnameLookup); |
| 362 negotiate_enable_port_ = local_state->GetBoolean( | 362 negotiate_enable_port_ = local_state->GetBoolean( |
| 363 prefs::kEnableAuthNegotiatePort); | 363 prefs::kEnableAuthNegotiatePort); |
| 364 auth_server_whitelist_ = local_state->GetString(prefs::kAuthServerWhitelist); | 364 auth_server_whitelist_ = local_state->GetString(prefs::kAuthServerWhitelist); |
| 365 auth_delegate_whitelist_ = local_state->GetString( | 365 auth_delegate_whitelist_ = local_state->GetString( |
| 366 prefs::kAuthNegotiateDelegateWhitelist); | 366 prefs::kAuthNegotiateDelegateWhitelist); |
| 367 gssapi_library_name_ = local_state->GetString(prefs::kGSSAPILibraryName); | 367 gssapi_library_name_ = local_state->GetString(prefs::kGSSAPILibraryName); |
| 368 pref_proxy_config_tracker_.reset( | 368 pref_proxy_config_tracker_.reset( |
| 369 ProxyServiceFactory::CreatePrefProxyConfigTracker(local_state)); | 369 ProxyServiceFactory::CreatePrefProxyConfigTracker(local_state)); |
| 370 ChromeNetworkDelegate::InitializeReferrersEnabled(&system_enable_referrers_, | 370 ChromeNetworkDelegate::InitializeReferrersEnabled(&system_enable_referrers_, |
| 371 local_state); | 371 local_state); |
| 372 ssl_config_service_manager_.reset( | 372 ssl_config_service_manager_.reset( |
| 373 SSLConfigServiceManager::CreateDefaultManager(local_state)); | 373 SSLConfigServiceManager::CreateDefaultManager(local_state)); |
| 374 | 374 MessageLoop::current()->PostTask( |
| 375 BrowserThread::SetDelegate(BrowserThread::IO, this); | 375 FROM_HERE, base::Bind(&IOThread::InitSystemRequestContext, |
| 376 weak_factory_.GetWeakPtr())); |
| 376 } | 377 } |
| 377 | 378 |
| 378 IOThread::~IOThread() { | 379 IOThread::~IOThread() { |
| 379 // This isn't needed for production code, but in tests, IOThread may | |
| 380 // be multiply constructed. | |
| 381 BrowserThread::SetDelegate(BrowserThread::IO, NULL); | |
| 382 | |
| 383 if (pref_proxy_config_tracker_.get()) | 380 if (pref_proxy_config_tracker_.get()) |
| 384 pref_proxy_config_tracker_->DetachFromPrefService(); | 381 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_); | 385 DCHECK(!globals_); |
| 386 } | 386 } |
| 387 | 387 |
| 388 IOThread::Globals* IOThread::globals() { | 388 IOThread::Globals* IOThread::globals() { |
| 389 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 389 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 390 return globals_; | 390 return globals_; |
| 391 } | 391 } |
| 392 | 392 |
| 393 ChromeNetLog* IOThread::net_log() { | 393 ChromeNetLog* IOThread::net_log() { |
| 394 return net_log_; | 394 return net_log_; |
| 395 } | 395 } |
| 396 | 396 |
| 397 net::URLRequestContextGetter* IOThread::system_url_request_context_getter() { | 397 net::URLRequestContextGetter* IOThread::system_url_request_context_getter() { |
| 398 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 398 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 399 if (!system_url_request_context_getter_) { | 399 if (!system_url_request_context_getter_) { |
| 400 InitSystemRequestContext(); | 400 InitSystemRequestContext(); |
| 401 } | 401 } |
| 402 return system_url_request_context_getter_; | 402 return system_url_request_context_getter_; |
| 403 } | 403 } |
| 404 | 404 |
| 405 void IOThread::Init() { | 405 void IOThread::Init() { |
| 406 // Though this thread is called the "IO" thread, it actually just routes | 406 // 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. | 407 // messages around; it shouldn't be allowed to perform any blocking disk I/O. |
| 408 base::ThreadRestrictions::SetIOAllowed(false); | 408 base::ThreadRestrictions::SetIOAllowed(false); |
| 409 | 409 |
| 410 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 410 content::BrowserProcessSubThread::Init(); |
| 411 |
| 412 DCHECK_EQ(MessageLoop::TYPE_IO, message_loop()->type()); |
| 411 | 413 |
| 412 #if defined(USE_NSS) | 414 #if defined(USE_NSS) |
| 413 net::SetMessageLoopForOCSP(); | 415 net::SetMessageLoopForOCSP(); |
| 414 #endif // defined(USE_NSS) | 416 #endif // defined(USE_NSS) |
| 415 | 417 |
| 416 DCHECK(!globals_); | 418 DCHECK(!globals_); |
| 417 globals_ = new Globals; | 419 globals_ = new Globals; |
| 418 | 420 |
| 419 globals_->media.media_internals.reset(new MediaInternals()); | 421 globals_->media.media_internals.reset(new MediaInternals()); |
| 420 | 422 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 471 globals_->proxy_script_fetcher_http_transaction_factory.reset( | 473 globals_->proxy_script_fetcher_http_transaction_factory.reset( |
| 472 new net::HttpNetworkLayer(network_session)); | 474 new net::HttpNetworkLayer(network_session)); |
| 473 globals_->proxy_script_fetcher_ftp_transaction_factory.reset( | 475 globals_->proxy_script_fetcher_ftp_transaction_factory.reset( |
| 474 new net::FtpNetworkLayer(globals_->host_resolver.get())); | 476 new net::FtpNetworkLayer(globals_->host_resolver.get())); |
| 475 | 477 |
| 476 globals_->proxy_script_fetcher_context = | 478 globals_->proxy_script_fetcher_context = |
| 477 ConstructProxyScriptFetcherContext(globals_, net_log_); | 479 ConstructProxyScriptFetcherContext(globals_, net_log_); |
| 478 | 480 |
| 479 sdch_manager_ = new net::SdchManager(); | 481 sdch_manager_ = new net::SdchManager(); |
| 480 sdch_manager_->set_sdch_fetcher(new SdchDictionaryFetcher); | 482 sdch_manager_->set_sdch_fetcher(new SdchDictionaryFetcher); |
| 481 | |
| 482 // InitSystemRequestContext turns right around and posts a task back | |
| 483 // to the IO thread, so we can't let it run until we know the IO | |
| 484 // thread has started. | |
| 485 // | |
| 486 // Note that since we are at BrowserThread::Init time, the UI thread | |
| 487 // is blocked waiting for the thread to start. Therefore, posting | |
| 488 // this task to the main thread's message loop here is guaranteed to | |
| 489 // get it onto the message loop while the IOThread object still | |
| 490 // exists. However, the message might not be processed on the UI | |
| 491 // thread until after IOThread is gone, so use a weak pointer. | |
| 492 BrowserThread::PostTask(BrowserThread::UI, | |
| 493 FROM_HERE, | |
| 494 base::Bind(&IOThread::InitSystemRequestContext, | |
| 495 weak_factory_.GetWeakPtr())); | |
| 496 | |
| 497 // We constructed the weak pointer on the IO thread but it will be | |
| 498 // used on the UI thread. Call this to avoid a thread checker | |
| 499 // error. | |
| 500 weak_factory_.DetachFromThread(); | |
| 501 } | 483 } |
| 502 | 484 |
| 503 void IOThread::CleanUp() { | 485 void IOThread::CleanUp() { |
| 504 delete sdch_manager_; | 486 delete sdch_manager_; |
| 505 sdch_manager_ = NULL; | 487 sdch_manager_ = NULL; |
| 506 | 488 |
| 507 // Step 1: Kill all things that might be holding onto | 489 // Step 1: Kill all things that might be holding onto |
| 508 // net::URLRequest/net::URLRequestContexts. | 490 // net::URLRequest/net::URLRequestContexts. |
| 509 | 491 |
| 510 #if defined(USE_NSS) | 492 #if defined(USE_NSS) |
| (...skipping 20 matching lines...) Expand all Loading... |
| 531 | 513 |
| 532 system_proxy_config_service_.reset(); | 514 system_proxy_config_service_.reset(); |
| 533 | 515 |
| 534 delete globals_; | 516 delete globals_; |
| 535 globals_ = NULL; | 517 globals_ = NULL; |
| 536 | 518 |
| 537 // net::URLRequest instances must NOT outlive the IO thread. | 519 // net::URLRequest instances must NOT outlive the IO thread. |
| 538 base::debug::LeakTracker<net::URLRequest>::CheckForLeaks(); | 520 base::debug::LeakTracker<net::URLRequest>::CheckForLeaks(); |
| 539 | 521 |
| 540 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(); |
| 541 } | 527 } |
| 542 | 528 |
| 543 // static | 529 // static |
| 544 void IOThread::RegisterPrefs(PrefService* local_state) { | 530 void IOThread::RegisterPrefs(PrefService* local_state) { |
| 545 local_state->RegisterStringPref(prefs::kAuthSchemes, | 531 local_state->RegisterStringPref(prefs::kAuthSchemes, |
| 546 "basic,digest,ntlm,negotiate"); | 532 "basic,digest,ntlm,negotiate"); |
| 547 local_state->RegisterBooleanPref(prefs::kDisableAuthNegotiateCnameLookup, | 533 local_state->RegisterBooleanPref(prefs::kDisableAuthNegotiateCnameLookup, |
| 548 false); | 534 false); |
| 549 local_state->RegisterBooleanPref(prefs::kEnableAuthNegotiatePort, false); | 535 local_state->RegisterBooleanPref(prefs::kEnableAuthNegotiatePort, false); |
| 550 local_state->RegisterStringPref(prefs::kAuthServerWhitelist, ""); | 536 local_state->RegisterStringPref(prefs::kAuthServerWhitelist, ""); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 581 } | 567 } |
| 582 | 568 |
| 583 void IOThread::ClearHostCache() { | 569 void IOThread::ClearHostCache() { |
| 584 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 570 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 585 | 571 |
| 586 net::HostCache* host_cache = globals_->host_resolver->GetHostCache(); | 572 net::HostCache* host_cache = globals_->host_resolver->GetHostCache(); |
| 587 if (host_cache) | 573 if (host_cache) |
| 588 host_cache->clear(); | 574 host_cache->clear(); |
| 589 } | 575 } |
| 590 | 576 |
| 591 MessageLoop* IOThread::message_loop() const { | |
| 592 return BrowserThread::UnsafeGetBrowserThread( | |
| 593 BrowserThread::IO)->message_loop(); | |
| 594 } | |
| 595 | |
| 596 net::SSLConfigService* IOThread::GetSSLConfigService() { | 577 net::SSLConfigService* IOThread::GetSSLConfigService() { |
| 597 return ssl_config_service_manager_->Get(); | 578 return ssl_config_service_manager_->Get(); |
| 598 } | 579 } |
| 599 | 580 |
| 600 void IOThread::InitSystemRequestContext() { | 581 void IOThread::InitSystemRequestContext() { |
| 601 if (system_url_request_context_getter_) | 582 if (system_url_request_context_getter_) |
| 602 return; | 583 return; |
| 603 // If we're in unit_tests, IOThread may not be run. | 584 // If we're in unit_tests, IOThread may not be run. |
| 604 if (!message_loop()) | 585 if (!message_loop()) |
| 605 return; | 586 return; |
| 606 ChromeProxyConfigService* proxy_config_service = | 587 ChromeProxyConfigService* proxy_config_service = |
| 607 ProxyServiceFactory::CreateProxyConfigService(); | 588 ProxyServiceFactory::CreateProxyConfigService(); |
| 608 system_proxy_config_service_.reset(proxy_config_service); | 589 system_proxy_config_service_.reset(proxy_config_service); |
| 609 if (pref_proxy_config_tracker_.get()) { | 590 if (pref_proxy_config_tracker_.get()) { |
| 610 pref_proxy_config_tracker_->SetChromeProxyConfigService( | 591 pref_proxy_config_tracker_->SetChromeProxyConfigService( |
| 611 proxy_config_service); | 592 proxy_config_service); |
| 612 } | 593 } |
| 613 system_url_request_context_getter_ = | 594 system_url_request_context_getter_ = |
| 614 new SystemURLRequestContextGetter(this); | 595 new SystemURLRequestContextGetter(this); |
| 615 // Safe to post an unretained this pointer, since IOThread is | |
| 616 // guaranteed to outlive the IO BrowserThread. | |
| 617 message_loop()->PostTask( | 596 message_loop()->PostTask( |
| 618 FROM_HERE, base::Bind(&IOThread::InitSystemRequestContextOnIOThread, | 597 FROM_HERE, base::Bind(&IOThread::InitSystemRequestContextOnIOThread, |
| 619 base::Unretained(this))); | 598 base::Unretained(this))); |
| 620 } | 599 } |
| 621 | 600 |
| 622 void IOThread::InitSystemRequestContextOnIOThread() { | 601 void IOThread::InitSystemRequestContextOnIOThread() { |
| 623 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 602 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 624 DCHECK(!globals_->system_proxy_service.get()); | 603 DCHECK(!globals_->system_proxy_service.get()); |
| 625 DCHECK(system_proxy_config_service_.get()); | 604 DCHECK(system_proxy_config_service_.get()); |
| 626 | 605 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 647 system_params.network_delegate = globals_->system_network_delegate.get(); | 626 system_params.network_delegate = globals_->system_network_delegate.get(); |
| 648 system_params.net_log = net_log_; | 627 system_params.net_log = net_log_; |
| 649 globals_->system_http_transaction_factory.reset( | 628 globals_->system_http_transaction_factory.reset( |
| 650 new net::HttpNetworkLayer( | 629 new net::HttpNetworkLayer( |
| 651 new net::HttpNetworkSession(system_params))); | 630 new net::HttpNetworkSession(system_params))); |
| 652 globals_->system_ftp_transaction_factory.reset( | 631 globals_->system_ftp_transaction_factory.reset( |
| 653 new net::FtpNetworkLayer(globals_->host_resolver.get())); | 632 new net::FtpNetworkLayer(globals_->host_resolver.get())); |
| 654 globals_->system_request_context = | 633 globals_->system_request_context = |
| 655 ConstructSystemRequestContext(globals_, net_log_); | 634 ConstructSystemRequestContext(globals_, net_log_); |
| 656 } | 635 } |
| OLD | NEW |