| 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_(io_thread->message_loop_proxy()) { | 319 io_message_loop_proxy_( |
| 320 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)) { |
| 320 } | 321 } |
| 321 | 322 |
| 322 SystemURLRequestContextGetter::~SystemURLRequestContextGetter() {} | 323 SystemURLRequestContextGetter::~SystemURLRequestContextGetter() {} |
| 323 | 324 |
| 324 net::URLRequestContext* SystemURLRequestContextGetter::GetURLRequestContext() { | 325 net::URLRequestContext* SystemURLRequestContextGetter::GetURLRequestContext() { |
| 325 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 326 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 326 DCHECK(io_thread_->globals()->system_request_context); | 327 DCHECK(io_thread_->globals()->system_request_context); |
| 327 | 328 |
| 328 return io_thread_->globals()->system_request_context; | 329 return io_thread_->globals()->system_request_context; |
| 329 } | 330 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 340 IOThread::Globals::MediaGlobals::MediaGlobals() {} | 341 IOThread::Globals::MediaGlobals::MediaGlobals() {} |
| 341 | 342 |
| 342 IOThread::Globals::MediaGlobals::~MediaGlobals() {} | 343 IOThread::Globals::MediaGlobals::~MediaGlobals() {} |
| 343 | 344 |
| 344 // |local_state| is passed in explicitly in order to (1) reduce implicit | 345 // |local_state| is passed in explicitly in order to (1) reduce implicit |
| 345 // dependencies and (2) make IOThread more flexible for testing. | 346 // dependencies and (2) make IOThread more flexible for testing. |
| 346 IOThread::IOThread( | 347 IOThread::IOThread( |
| 347 PrefService* local_state, | 348 PrefService* local_state, |
| 348 ChromeNetLog* net_log, | 349 ChromeNetLog* net_log, |
| 349 ExtensionEventRouterForwarder* extension_event_router_forwarder) | 350 ExtensionEventRouterForwarder* extension_event_router_forwarder) |
| 350 : content::BrowserProcessSubThread(BrowserThread::IO), | 351 : net_log_(net_log), |
| 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 MessageLoop::current()->PostTask( | 374 |
| 375 FROM_HERE, base::Bind(&IOThread::InitSystemRequestContext, | 375 BrowserThread::SetDelegate(BrowserThread::IO, this); |
| 376 weak_factory_.GetWeakPtr())); | |
| 377 } | 376 } |
| 378 | 377 |
| 379 IOThread::~IOThread() { | 378 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 |
| 380 if (pref_proxy_config_tracker_.get()) | 383 if (pref_proxy_config_tracker_.get()) |
| 381 pref_proxy_config_tracker_->DetachFromPrefService(); | 384 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 content::BrowserProcessSubThread::Init(); | 410 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 411 | |
| 412 DCHECK_EQ(MessageLoop::TYPE_IO, message_loop()->type()); | |
| 413 | 411 |
| 414 #if defined(USE_NSS) | 412 #if defined(USE_NSS) |
| 415 net::SetMessageLoopForOCSP(); | 413 net::SetMessageLoopForOCSP(); |
| 416 #endif // defined(USE_NSS) | 414 #endif // defined(USE_NSS) |
| 417 | 415 |
| 418 DCHECK(!globals_); | 416 DCHECK(!globals_); |
| 419 globals_ = new Globals; | 417 globals_ = new Globals; |
| 420 | 418 |
| 421 globals_->media.media_internals.reset(new MediaInternals()); | 419 globals_->media.media_internals.reset(new MediaInternals()); |
| 422 | 420 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 473 globals_->proxy_script_fetcher_http_transaction_factory.reset( | 471 globals_->proxy_script_fetcher_http_transaction_factory.reset( |
| 474 new net::HttpNetworkLayer(network_session)); | 472 new net::HttpNetworkLayer(network_session)); |
| 475 globals_->proxy_script_fetcher_ftp_transaction_factory.reset( | 473 globals_->proxy_script_fetcher_ftp_transaction_factory.reset( |
| 476 new net::FtpNetworkLayer(globals_->host_resolver.get())); | 474 new net::FtpNetworkLayer(globals_->host_resolver.get())); |
| 477 | 475 |
| 478 globals_->proxy_script_fetcher_context = | 476 globals_->proxy_script_fetcher_context = |
| 479 ConstructProxyScriptFetcherContext(globals_, net_log_); | 477 ConstructProxyScriptFetcherContext(globals_, net_log_); |
| 480 | 478 |
| 481 sdch_manager_ = new net::SdchManager(); | 479 sdch_manager_ = new net::SdchManager(); |
| 482 sdch_manager_->set_sdch_fetcher(new SdchDictionaryFetcher); | 480 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(); |
| 483 } | 501 } |
| 484 | 502 |
| 485 void IOThread::CleanUp() { | 503 void IOThread::CleanUp() { |
| 486 delete sdch_manager_; | 504 delete sdch_manager_; |
| 487 sdch_manager_ = NULL; | 505 sdch_manager_ = NULL; |
| 488 | 506 |
| 489 // Step 1: Kill all things that might be holding onto | 507 // Step 1: Kill all things that might be holding onto |
| 490 // net::URLRequest/net::URLRequestContexts. | 508 // net::URLRequest/net::URLRequestContexts. |
| 491 | 509 |
| 492 #if defined(USE_NSS) | 510 #if defined(USE_NSS) |
| (...skipping 20 matching lines...) Expand all Loading... |
| 513 | 531 |
| 514 system_proxy_config_service_.reset(); | 532 system_proxy_config_service_.reset(); |
| 515 | 533 |
| 516 delete globals_; | 534 delete globals_; |
| 517 globals_ = NULL; | 535 globals_ = NULL; |
| 518 | 536 |
| 519 // net::URLRequest instances must NOT outlive the IO thread. | 537 // net::URLRequest instances must NOT outlive the IO thread. |
| 520 base::debug::LeakTracker<net::URLRequest>::CheckForLeaks(); | 538 base::debug::LeakTracker<net::URLRequest>::CheckForLeaks(); |
| 521 | 539 |
| 522 base::debug::LeakTracker<SystemURLRequestContextGetter>::CheckForLeaks(); | 540 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 } | 541 } |
| 528 | 542 |
| 529 // static | 543 // static |
| 530 void IOThread::RegisterPrefs(PrefService* local_state) { | 544 void IOThread::RegisterPrefs(PrefService* local_state) { |
| 531 local_state->RegisterStringPref(prefs::kAuthSchemes, | 545 local_state->RegisterStringPref(prefs::kAuthSchemes, |
| 532 "basic,digest,ntlm,negotiate"); | 546 "basic,digest,ntlm,negotiate"); |
| 533 local_state->RegisterBooleanPref(prefs::kDisableAuthNegotiateCnameLookup, | 547 local_state->RegisterBooleanPref(prefs::kDisableAuthNegotiateCnameLookup, |
| 534 false); | 548 false); |
| 535 local_state->RegisterBooleanPref(prefs::kEnableAuthNegotiatePort, false); | 549 local_state->RegisterBooleanPref(prefs::kEnableAuthNegotiatePort, false); |
| 536 local_state->RegisterStringPref(prefs::kAuthServerWhitelist, ""); | 550 local_state->RegisterStringPref(prefs::kAuthServerWhitelist, ""); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 567 } | 581 } |
| 568 | 582 |
| 569 void IOThread::ClearHostCache() { | 583 void IOThread::ClearHostCache() { |
| 570 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 584 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 571 | 585 |
| 572 net::HostCache* host_cache = globals_->host_resolver->GetHostCache(); | 586 net::HostCache* host_cache = globals_->host_resolver->GetHostCache(); |
| 573 if (host_cache) | 587 if (host_cache) |
| 574 host_cache->clear(); | 588 host_cache->clear(); |
| 575 } | 589 } |
| 576 | 590 |
| 591 MessageLoop* IOThread::message_loop() const { |
| 592 return BrowserThread::UnsafeGetBrowserThread( |
| 593 BrowserThread::IO)->message_loop(); |
| 594 } |
| 595 |
| 577 net::SSLConfigService* IOThread::GetSSLConfigService() { | 596 net::SSLConfigService* IOThread::GetSSLConfigService() { |
| 578 return ssl_config_service_manager_->Get(); | 597 return ssl_config_service_manager_->Get(); |
| 579 } | 598 } |
| 580 | 599 |
| 581 void IOThread::InitSystemRequestContext() { | 600 void IOThread::InitSystemRequestContext() { |
| 582 if (system_url_request_context_getter_) | 601 if (system_url_request_context_getter_) |
| 583 return; | 602 return; |
| 584 // If we're in unit_tests, IOThread may not be run. | 603 // If we're in unit_tests, IOThread may not be run. |
| 585 if (!message_loop()) | 604 if (!message_loop()) |
| 586 return; | 605 return; |
| 587 ChromeProxyConfigService* proxy_config_service = | 606 ChromeProxyConfigService* proxy_config_service = |
| 588 ProxyServiceFactory::CreateProxyConfigService(); | 607 ProxyServiceFactory::CreateProxyConfigService(); |
| 589 system_proxy_config_service_.reset(proxy_config_service); | 608 system_proxy_config_service_.reset(proxy_config_service); |
| 590 if (pref_proxy_config_tracker_.get()) { | 609 if (pref_proxy_config_tracker_.get()) { |
| 591 pref_proxy_config_tracker_->SetChromeProxyConfigService( | 610 pref_proxy_config_tracker_->SetChromeProxyConfigService( |
| 592 proxy_config_service); | 611 proxy_config_service); |
| 593 } | 612 } |
| 594 system_url_request_context_getter_ = | 613 system_url_request_context_getter_ = |
| 595 new SystemURLRequestContextGetter(this); | 614 new SystemURLRequestContextGetter(this); |
| 615 // Safe to post an unretained this pointer, since IOThread is |
| 616 // guaranteed to outlive the IO BrowserThread. |
| 596 message_loop()->PostTask( | 617 message_loop()->PostTask( |
| 597 FROM_HERE, base::Bind(&IOThread::InitSystemRequestContextOnIOThread, | 618 FROM_HERE, base::Bind(&IOThread::InitSystemRequestContextOnIOThread, |
| 598 base::Unretained(this))); | 619 base::Unretained(this))); |
| 599 } | 620 } |
| 600 | 621 |
| 601 void IOThread::InitSystemRequestContextOnIOThread() { | 622 void IOThread::InitSystemRequestContextOnIOThread() { |
| 602 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 623 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 603 DCHECK(!globals_->system_proxy_service.get()); | 624 DCHECK(!globals_->system_proxy_service.get()); |
| 604 DCHECK(system_proxy_config_service_.get()); | 625 DCHECK(system_proxy_config_service_.get()); |
| 605 | 626 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 626 system_params.network_delegate = globals_->system_network_delegate.get(); | 647 system_params.network_delegate = globals_->system_network_delegate.get(); |
| 627 system_params.net_log = net_log_; | 648 system_params.net_log = net_log_; |
| 628 globals_->system_http_transaction_factory.reset( | 649 globals_->system_http_transaction_factory.reset( |
| 629 new net::HttpNetworkLayer( | 650 new net::HttpNetworkLayer( |
| 630 new net::HttpNetworkSession(system_params))); | 651 new net::HttpNetworkSession(system_params))); |
| 631 globals_->system_ftp_transaction_factory.reset( | 652 globals_->system_ftp_transaction_factory.reset( |
| 632 new net::FtpNetworkLayer(globals_->host_resolver.get())); | 653 new net::FtpNetworkLayer(globals_->host_resolver.get())); |
| 633 globals_->system_request_context = | 654 globals_->system_request_context = |
| 634 ConstructSystemRequestContext(globals_, net_log_); | 655 ConstructSystemRequestContext(globals_, net_log_); |
| 635 } | 656 } |
| OLD | NEW |