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/debug/leak_tracker.h" | 10 #include "base/debug/leak_tracker.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/metrics/field_trial.h" | 12 #include "base/metrics/field_trial.h" |
13 #include "base/stl_util-inl.h" | 13 #include "base/stl_util-inl.h" |
14 #include "base/string_number_conversions.h" | 14 #include "base/string_number_conversions.h" |
15 #include "base/string_split.h" | 15 #include "base/string_split.h" |
16 #include "base/string_util.h" | 16 #include "base/string_util.h" |
17 #include "base/threading/thread_restrictions.h" | 17 #include "base/threading/thread_restrictions.h" |
18 #include "chrome/browser/browser_process.h" | 18 #include "chrome/browser/browser_process.h" |
19 #include "chrome/browser/extensions/extension_event_router_forwarder.h" | 19 #include "chrome/browser/extensions/extension_event_router_forwarder.h" |
20 #include "chrome/browser/net/chrome_network_delegate.h" | 20 #include "chrome/browser/net/chrome_network_delegate.h" |
21 #include "chrome/browser/net/chrome_net_log.h" | 21 #include "chrome/browser/net/chrome_net_log.h" |
22 #include "chrome/browser/net/chrome_url_request_context.h" | 22 #include "chrome/browser/net/chrome_url_request_context.h" |
23 #include "chrome/browser/net/connect_interceptor.h" | 23 #include "chrome/browser/net/connect_interceptor.h" |
24 #include "chrome/browser/net/passive_log_collector.h" | 24 #include "chrome/browser/net/passive_log_collector.h" |
25 #include "chrome/browser/net/predictor_api.h" | 25 #include "chrome/browser/net/predictor_api.h" |
| 26 #include "chrome/browser/net/pref_proxy_config_service.h" |
| 27 #include "chrome/browser/net/proxy_service_factory.h" |
26 #include "chrome/browser/prefs/pref_service.h" | 28 #include "chrome/browser/prefs/pref_service.h" |
27 #include "chrome/common/chrome_switches.h" | 29 #include "chrome/common/chrome_switches.h" |
28 #include "chrome/common/net/raw_host_resolver_proc.h" | 30 #include "chrome/common/net/raw_host_resolver_proc.h" |
29 #include "chrome/common/net/url_fetcher.h" | 31 #include "chrome/common/net/url_fetcher.h" |
30 #include "chrome/common/pref_names.h" | 32 #include "chrome/common/pref_names.h" |
31 #include "content/browser/browser_thread.h" | 33 #include "content/browser/browser_thread.h" |
32 #include "content/browser/gpu_process_host.h" | 34 #include "content/browser/gpu_process_host.h" |
33 #include "content/browser/in_process_webkit/indexed_db_key_utility_client.h" | 35 #include "content/browser/in_process_webkit/indexed_db_key_utility_client.h" |
34 #include "net/base/cert_verifier.h" | 36 #include "net/base/cert_verifier.h" |
35 #include "net/base/cookie_monster.h" | 37 #include "net/base/cookie_monster.h" |
36 #include "net/base/dnsrr_resolver.h" | 38 #include "net/base/dnsrr_resolver.h" |
37 #include "net/base/host_cache.h" | 39 #include "net/base/host_cache.h" |
38 #include "net/base/host_resolver.h" | 40 #include "net/base/host_resolver.h" |
39 #include "net/base/host_resolver_impl.h" | 41 #include "net/base/host_resolver_impl.h" |
40 #include "net/base/mapped_host_resolver.h" | 42 #include "net/base/mapped_host_resolver.h" |
41 #include "net/base/net_util.h" | 43 #include "net/base/net_util.h" |
| 44 #include "net/proxy/proxy_config_service.h" |
42 #include "net/http/http_auth_filter.h" | 45 #include "net/http/http_auth_filter.h" |
43 #include "net/http/http_auth_handler_factory.h" | 46 #include "net/http/http_auth_handler_factory.h" |
44 #include "net/http/http_network_layer.h" | 47 #include "net/http/http_network_layer.h" |
45 #include "net/http/http_network_session.h" | 48 #include "net/http/http_network_session.h" |
46 #if defined(USE_NSS) | 49 #if defined(USE_NSS) |
47 #include "net/ocsp/nss_ocsp.h" | 50 #include "net/ocsp/nss_ocsp.h" |
48 #endif // defined(USE_NSS) | 51 #endif // defined(USE_NSS) |
49 #include "net/proxy/proxy_script_fetcher_impl.h" | 52 #include "net/proxy/proxy_script_fetcher_impl.h" |
50 | 53 |
51 namespace { | 54 namespace { |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 globals->http_auth_handler_factory.get()); | 195 globals->http_auth_handler_factory.get()); |
193 context->set_proxy_service(globals->proxy_script_fetcher_proxy_service.get()); | 196 context->set_proxy_service(globals->proxy_script_fetcher_proxy_service.get()); |
194 context->set_http_transaction_factory( | 197 context->set_http_transaction_factory( |
195 globals->proxy_script_fetcher_http_transaction_factory.get()); | 198 globals->proxy_script_fetcher_http_transaction_factory.get()); |
196 // In-memory cookie store. | 199 // In-memory cookie store. |
197 context->set_cookie_store(new net::CookieMonster(NULL, NULL)); | 200 context->set_cookie_store(new net::CookieMonster(NULL, NULL)); |
198 context->set_network_delegate(globals->system_network_delegate.get()); | 201 context->set_network_delegate(globals->system_network_delegate.get()); |
199 return context; | 202 return context; |
200 } | 203 } |
201 | 204 |
| 205 scoped_refptr<net::URLRequestContext> |
| 206 ConstructSystemRequestContext(IOThread::Globals* globals, |
| 207 net::NetLog* net_log) { |
| 208 scoped_refptr<net::URLRequestContext> context(new net::URLRequestContext); |
| 209 context->set_net_log(net_log); |
| 210 context->set_host_resolver(globals->host_resolver.get()); |
| 211 context->set_cert_verifier(globals->cert_verifier.get()); |
| 212 context->set_dnsrr_resolver(globals->dnsrr_resolver.get()); |
| 213 context->set_http_auth_handler_factory( |
| 214 globals->http_auth_handler_factory.get()); |
| 215 context->set_proxy_service(globals->system_proxy_service.get()); |
| 216 context->set_http_transaction_factory( |
| 217 globals->system_http_transaction_factory.get()); |
| 218 // In-memory cookie store. |
| 219 context->set_cookie_store(new net::CookieMonster(NULL, NULL)); |
| 220 return context; |
| 221 } |
| 222 |
202 } // namespace | 223 } // namespace |
203 | 224 |
| 225 class SystemURLRequestContextGetter : public URLRequestContextGetter { |
| 226 public: |
| 227 explicit SystemURLRequestContextGetter(IOThread* io_thread); |
| 228 virtual ~SystemURLRequestContextGetter(); |
| 229 |
| 230 // Implementation for UrlRequestContextGetter. |
| 231 virtual net::URLRequestContext* GetURLRequestContext(); |
| 232 virtual scoped_refptr<base::MessageLoopProxy> GetIOMessageLoopProxy() const; |
| 233 |
| 234 private: |
| 235 IOThread* const io_thread_; // Weak pointer, owned by BrowserProcess. |
| 236 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; |
| 237 |
| 238 base::debug::LeakTracker<SystemURLRequestContextGetter> leak_tracker_; |
| 239 }; |
| 240 |
| 241 SystemURLRequestContextGetter::SystemURLRequestContextGetter( |
| 242 IOThread* io_thread) |
| 243 : io_thread_(io_thread), |
| 244 io_message_loop_proxy_(io_thread->message_loop_proxy()) { |
| 245 } |
| 246 |
| 247 SystemURLRequestContextGetter::~SystemURLRequestContextGetter() {} |
| 248 |
| 249 net::URLRequestContext* SystemURLRequestContextGetter::GetURLRequestContext() { |
| 250 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 251 |
| 252 if (!io_thread_->globals()->system_request_context) |
| 253 io_thread_->InitSystemRequestContext(); |
| 254 |
| 255 return io_thread_->globals()->system_request_context; |
| 256 } |
| 257 |
| 258 scoped_refptr<base::MessageLoopProxy> |
| 259 SystemURLRequestContextGetter::GetIOMessageLoopProxy() const { |
| 260 return io_message_loop_proxy_; |
| 261 } |
| 262 |
204 // The IOThread object must outlive any tasks posted to the IO thread before the | 263 // The IOThread object must outlive any tasks posted to the IO thread before the |
205 // Quit task. | 264 // Quit task. |
206 DISABLE_RUNNABLE_METHOD_REFCOUNT(IOThread); | 265 DISABLE_RUNNABLE_METHOD_REFCOUNT(IOThread); |
207 | 266 |
208 IOThread::Globals::Globals() {} | 267 IOThread::Globals::Globals() {} |
209 | 268 |
210 IOThread::Globals::~Globals() {} | 269 IOThread::Globals::~Globals() {} |
211 | 270 |
212 // |local_state| is passed in explicitly in order to (1) reduce implicit | 271 // |local_state| is passed in explicitly in order to (1) reduce implicit |
213 // dependencies and (2) make IOThread more flexible for testing. | 272 // dependencies and (2) make IOThread more flexible for testing. |
(...skipping 12 matching lines...) Expand all Loading... |
226 RegisterPrefs(local_state); | 285 RegisterPrefs(local_state); |
227 auth_schemes_ = local_state->GetString(prefs::kAuthSchemes); | 286 auth_schemes_ = local_state->GetString(prefs::kAuthSchemes); |
228 negotiate_disable_cname_lookup_ = local_state->GetBoolean( | 287 negotiate_disable_cname_lookup_ = local_state->GetBoolean( |
229 prefs::kDisableAuthNegotiateCnameLookup); | 288 prefs::kDisableAuthNegotiateCnameLookup); |
230 negotiate_enable_port_ = local_state->GetBoolean( | 289 negotiate_enable_port_ = local_state->GetBoolean( |
231 prefs::kEnableAuthNegotiatePort); | 290 prefs::kEnableAuthNegotiatePort); |
232 auth_server_whitelist_ = local_state->GetString(prefs::kAuthServerWhitelist); | 291 auth_server_whitelist_ = local_state->GetString(prefs::kAuthServerWhitelist); |
233 auth_delegate_whitelist_ = local_state->GetString( | 292 auth_delegate_whitelist_ = local_state->GetString( |
234 prefs::kAuthNegotiateDelegateWhitelist); | 293 prefs::kAuthNegotiateDelegateWhitelist); |
235 gssapi_library_name_ = local_state->GetString(prefs::kGSSAPILibraryName); | 294 gssapi_library_name_ = local_state->GetString(prefs::kGSSAPILibraryName); |
| 295 pref_proxy_config_tracker_ = new PrefProxyConfigTracker(local_state); |
236 } | 296 } |
237 | 297 |
238 IOThread::~IOThread() { | 298 IOThread::~IOThread() { |
| 299 if (pref_proxy_config_tracker_) |
| 300 pref_proxy_config_tracker_->DetachFromPrefService(); |
239 // We cannot rely on our base class to stop the thread since we want our | 301 // We cannot rely on our base class to stop the thread since we want our |
240 // CleanUp function to run. | 302 // CleanUp function to run. |
241 Stop(); | 303 Stop(); |
242 DCHECK(!globals_); | 304 DCHECK(!globals_); |
243 } | 305 } |
244 | 306 |
245 IOThread::Globals* IOThread::globals() { | 307 IOThread::Globals* IOThread::globals() { |
246 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 308 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
247 return globals_; | 309 return globals_; |
248 } | 310 } |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 | 357 |
296 void IOThread::ChangedToOnTheRecord() { | 358 void IOThread::ChangedToOnTheRecord() { |
297 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 359 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
298 message_loop()->PostTask( | 360 message_loop()->PostTask( |
299 FROM_HERE, | 361 FROM_HERE, |
300 NewRunnableMethod( | 362 NewRunnableMethod( |
301 this, | 363 this, |
302 &IOThread::ChangedToOnTheRecordOnIOThread)); | 364 &IOThread::ChangedToOnTheRecordOnIOThread)); |
303 } | 365 } |
304 | 366 |
| 367 scoped_refptr<URLRequestContextGetter> |
| 368 IOThread::system_url_request_context_getter() { |
| 369 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 370 if (!system_url_request_context_getter_) { |
| 371 system_proxy_config_service_.reset( |
| 372 ProxyServiceFactory::CreateProxyConfigService( |
| 373 pref_proxy_config_tracker_)); |
| 374 system_url_request_context_getter_ = |
| 375 new SystemURLRequestContextGetter(this); |
| 376 } |
| 377 return system_url_request_context_getter_; |
| 378 } |
| 379 |
305 void IOThread::ClearNetworkingHistory() { | 380 void IOThread::ClearNetworkingHistory() { |
306 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 381 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
307 ClearHostCache(); | 382 ClearHostCache(); |
308 // Discard acrued data used to speculate in the future. | 383 // Discard acrued data used to speculate in the future. |
309 chrome_browser_net::DiscardInitialNavigationHistory(); | 384 chrome_browser_net::DiscardInitialNavigationHistory(); |
310 if (predictor_) | 385 if (predictor_) |
311 predictor_->DiscardAllResults(); | 386 predictor_->DiscardAllResults(); |
312 } | 387 } |
313 | 388 |
314 void IOThread::Init() { | 389 void IOThread::Init() { |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
394 it != url_request_context_getters.end(); ++it) { | 469 it != url_request_context_getters.end(); ++it) { |
395 ChromeURLRequestContextGetter* getter = *it; | 470 ChromeURLRequestContextGetter* getter = *it; |
396 // Stop all pending certificate provenance check uploads | 471 // Stop all pending certificate provenance check uploads |
397 net::DnsCertProvenanceChecker* checker = | 472 net::DnsCertProvenanceChecker* checker = |
398 getter->GetURLRequestContext()->dns_cert_checker(); | 473 getter->GetURLRequestContext()->dns_cert_checker(); |
399 if (checker) | 474 if (checker) |
400 checker->Shutdown(); | 475 checker->Shutdown(); |
401 getter->ReleaseURLRequestContext(); | 476 getter->ReleaseURLRequestContext(); |
402 } | 477 } |
403 | 478 |
| 479 system_url_request_context_getter_ = NULL; |
| 480 |
404 // Step 2: Release objects that the net::URLRequestContext could have been | 481 // Step 2: Release objects that the net::URLRequestContext could have been |
405 // pointing to. | 482 // pointing to. |
406 | 483 |
407 // This must be reset before the ChromeNetLog is destroyed. | 484 // This must be reset before the ChromeNetLog is destroyed. |
408 network_change_observer_.reset(); | 485 network_change_observer_.reset(); |
409 | 486 |
410 // Not initialized in Init(). May not be initialized. | 487 // Not initialized in Init(). May not be initialized. |
411 if (predictor_) { | 488 if (predictor_) { |
412 predictor_->Shutdown(); | 489 predictor_->Shutdown(); |
413 | 490 |
414 // TODO(willchan): Stop reference counting Predictor. It's owned by | 491 // TODO(willchan): Stop reference counting Predictor. It's owned by |
415 // IOThread now. | 492 // IOThread now. |
416 predictor_->Release(); | 493 predictor_->Release(); |
417 predictor_ = NULL; | 494 predictor_ = NULL; |
418 chrome_browser_net::FreePredictorResources(); | 495 chrome_browser_net::FreePredictorResources(); |
419 } | 496 } |
420 | 497 |
421 // Deletion will unregister this interceptor. | 498 // Deletion will unregister this interceptor. |
422 delete speculative_interceptor_; | 499 delete speculative_interceptor_; |
423 speculative_interceptor_ = NULL; | 500 speculative_interceptor_ = NULL; |
424 | 501 |
425 // TODO(eroman): hack for http://crbug.com/15513 | 502 // TODO(eroman): hack for http://crbug.com/15513 |
426 if (globals_->host_resolver->GetAsHostResolverImpl()) { | 503 if (globals_->host_resolver->GetAsHostResolverImpl()) { |
427 globals_->host_resolver.get()->GetAsHostResolverImpl()->Shutdown(); | 504 globals_->host_resolver.get()->GetAsHostResolverImpl()->Shutdown(); |
428 } | 505 } |
429 | 506 |
| 507 system_proxy_config_service_.reset(); |
| 508 |
430 delete globals_; | 509 delete globals_; |
431 globals_ = NULL; | 510 globals_ = NULL; |
432 | 511 |
433 // net::URLRequest instances must NOT outlive the IO thread. | 512 // net::URLRequest instances must NOT outlive the IO thread. |
434 base::debug::LeakTracker<net::URLRequest>::CheckForLeaks(); | 513 base::debug::LeakTracker<net::URLRequest>::CheckForLeaks(); |
435 | 514 |
436 // This will delete the |notification_service_|. Make sure it's done after | 515 // This will delete the |notification_service_|. Make sure it's done after |
437 // anything else can reference it. | 516 // anything else can reference it. |
| 517 BrowserProcessSubThread::CleanUpAfterMessageLoopDestruction(); |
| 518 |
| 519 // net::URLRequest instances must NOT outlive the IO thread. |
| 520 // |
| 521 // To allow for URLRequests to be deleted from |
| 522 // MessageLoop::DestructionObserver this check has to happen after CleanUp |
| 523 // (which runs before DestructionObservers). |
| 524 base::debug::LeakTracker<net::URLRequest>::CheckForLeaks(); |
| 525 |
| 526 base::debug::LeakTracker<SystemURLRequestContextGetter>::CheckForLeaks(); |
| 527 |
438 BrowserProcessSubThread::CleanUp(); | 528 BrowserProcessSubThread::CleanUp(); |
439 } | 529 } |
440 | 530 |
441 // static | 531 // static |
442 void IOThread::RegisterPrefs(PrefService* local_state) { | 532 void IOThread::RegisterPrefs(PrefService* local_state) { |
443 local_state->RegisterStringPref(prefs::kAuthSchemes, | 533 local_state->RegisterStringPref(prefs::kAuthSchemes, |
444 "basic,digest,ntlm,negotiate"); | 534 "basic,digest,ntlm,negotiate"); |
445 local_state->RegisterBooleanPref(prefs::kDisableAuthNegotiateCnameLookup, | 535 local_state->RegisterBooleanPref(prefs::kDisableAuthNegotiateCnameLookup, |
446 false); | 536 false); |
447 local_state->RegisterBooleanPref(prefs::kEnableAuthNegotiatePort, false); | 537 local_state->RegisterBooleanPref(prefs::kEnableAuthNegotiatePort, false); |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
525 void IOThread::ClearHostCache() { | 615 void IOThread::ClearHostCache() { |
526 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 616 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
527 | 617 |
528 if (globals_->host_resolver->GetAsHostResolverImpl()) { | 618 if (globals_->host_resolver->GetAsHostResolverImpl()) { |
529 net::HostCache* host_cache = | 619 net::HostCache* host_cache = |
530 globals_->host_resolver.get()->GetAsHostResolverImpl()->cache(); | 620 globals_->host_resolver.get()->GetAsHostResolverImpl()->cache(); |
531 if (host_cache) | 621 if (host_cache) |
532 host_cache->clear(); | 622 host_cache->clear(); |
533 } | 623 } |
534 } | 624 } |
| 625 |
| 626 void IOThread::InitSystemRequestContext() { |
| 627 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 628 DCHECK(!globals_->system_proxy_service); |
| 629 DCHECK(system_proxy_config_service_.get()); |
| 630 |
| 631 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| 632 globals_->system_proxy_service = |
| 633 ProxyServiceFactory::CreateProxyService( |
| 634 net_log_, |
| 635 globals_->proxy_script_fetcher_context, |
| 636 system_proxy_config_service_.release(), |
| 637 command_line); |
| 638 net::HttpNetworkSession::Params system_params; |
| 639 system_params.host_resolver = globals_->host_resolver.get(); |
| 640 system_params.cert_verifier = globals_->cert_verifier.get(); |
| 641 system_params.dnsrr_resolver = globals_->dnsrr_resolver.get(); |
| 642 system_params.dns_cert_checker = NULL; |
| 643 system_params.ssl_host_info_factory = NULL; |
| 644 system_params.proxy_service = globals_->system_proxy_service.get(); |
| 645 system_params.ssl_config_service = globals_->ssl_config_service.get(); |
| 646 system_params.http_auth_handler_factory = |
| 647 globals_->http_auth_handler_factory.get(); |
| 648 system_params.network_delegate = globals_->system_network_delegate.get(); |
| 649 system_params.net_log = net_log_; |
| 650 globals_->system_http_transaction_factory.reset( |
| 651 new net::HttpNetworkLayer( |
| 652 new net::HttpNetworkSession(system_params))); |
| 653 globals_->system_request_context = |
| 654 ConstructSystemRequestContext(globals_, net_log_); |
| 655 } |
OLD | NEW |