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

Side by Side Diff: chrome/browser/io_thread.cc

Issue 6292017: Extended: Add "system" URLRequestContext (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Next iteration Created 9 years, 10 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 | Annotate | Revision Log
OLDNEW
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_thread.h" 18 #include "chrome/browser/browser_thread.h"
19 #include "chrome/browser/gpu_process_host.h" 19 #include "chrome/browser/gpu_process_host.h"
20 #include "chrome/browser/in_process_webkit/indexed_db_key_utility_client.h" 20 #include "chrome/browser/in_process_webkit/indexed_db_key_utility_client.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/proxy_service_factory.h"
26 #include "chrome/browser/prefs/pref_service.h" 27 #include "chrome/browser/prefs/pref_service.h"
27 #include "chrome/common/chrome_switches.h" 28 #include "chrome/common/chrome_switches.h"
28 #include "chrome/common/net/raw_host_resolver_proc.h" 29 #include "chrome/common/net/raw_host_resolver_proc.h"
29 #include "chrome/common/net/url_fetcher.h" 30 #include "chrome/common/net/url_fetcher.h"
30 #include "chrome/common/pref_names.h" 31 #include "chrome/common/pref_names.h"
31 #include "net/base/cert_verifier.h" 32 #include "net/base/cert_verifier.h"
32 #include "net/base/cookie_monster.h" 33 #include "net/base/cookie_monster.h"
33 #include "net/base/dnsrr_resolver.h" 34 #include "net/base/dnsrr_resolver.h"
34 #include "net/base/host_cache.h" 35 #include "net/base/host_cache.h"
35 #include "net/base/host_resolver.h" 36 #include "net/base/host_resolver.h"
36 #include "net/base/host_resolver_impl.h" 37 #include "net/base/host_resolver_impl.h"
37 #include "net/base/mapped_host_resolver.h" 38 #include "net/base/mapped_host_resolver.h"
38 #include "net/base/net_util.h" 39 #include "net/base/net_util.h"
40 #include "net/proxy/proxy_config_service.h"
39 #include "net/http/http_auth_filter.h" 41 #include "net/http/http_auth_filter.h"
40 #include "net/http/http_auth_handler_factory.h" 42 #include "net/http/http_auth_handler_factory.h"
41 #include "net/http/http_network_layer.h" 43 #include "net/http/http_network_layer.h"
42 #include "net/http/http_network_session.h" 44 #include "net/http/http_network_session.h"
43 #if defined(USE_NSS) 45 #if defined(USE_NSS)
44 #include "net/ocsp/nss_ocsp.h" 46 #include "net/ocsp/nss_ocsp.h"
45 #endif // defined(USE_NSS) 47 #endif // defined(USE_NSS)
46 #include "net/proxy/proxy_script_fetcher_impl.h" 48 #include "net/proxy/proxy_script_fetcher_impl.h"
47 49
48 namespace { 50 namespace {
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 globals->http_auth_handler_factory.get()); 191 globals->http_auth_handler_factory.get());
190 context->set_proxy_service(globals->proxy_script_fetcher_proxy_service.get()); 192 context->set_proxy_service(globals->proxy_script_fetcher_proxy_service.get());
191 context->set_http_transaction_factory( 193 context->set_http_transaction_factory(
192 globals->proxy_script_fetcher_http_transaction_factory.get()); 194 globals->proxy_script_fetcher_http_transaction_factory.get());
193 // In-memory cookie store. 195 // In-memory cookie store.
194 context->set_cookie_store(new net::CookieMonster(NULL, NULL)); 196 context->set_cookie_store(new net::CookieMonster(NULL, NULL));
195 // TODO(mpcomplete): give it a SystemNetworkDelegate. 197 // TODO(mpcomplete): give it a SystemNetworkDelegate.
196 return context; 198 return context;
197 } 199 }
198 200
201 scoped_refptr<net::URLRequestContext>
202 ConstructSystemRequestContext(IOThread::Globals* globals,
203 net::NetLog* net_log) {
204 scoped_refptr<net::URLRequestContext> context(new net::URLRequestContext);
205 context->set_net_log(net_log);
206 context->set_host_resolver(globals->host_resolver.get());
207 context->set_cert_verifier(globals->cert_verifier.get());
208 context->set_dnsrr_resolver(globals->dnsrr_resolver.get());
209 context->set_http_auth_handler_factory(
210 globals->http_auth_handler_factory.get());
211 context->set_proxy_service(globals->system_proxy_service.get());
212 context->set_http_transaction_factory(
213 globals->system_http_transaction_factory.get());
214 // In-memory cookie store.
215 context->set_cookie_store(new net::CookieMonster(NULL, NULL));
216 return context;
217 }
218
199 } // namespace 219 } // namespace
200 220
221 class SystemURLRequestContextGetter : public URLRequestContextGetter {
222 public:
223 explicit SystemURLRequestContextGetter(IOThread* io_thread);
224 virtual ~SystemURLRequestContextGetter();
225
226 // Implementation for UrlRequestContextGetter.
227 virtual net::URLRequestContext* GetURLRequestContext();
228 virtual scoped_refptr<base::MessageLoopProxy> GetIOMessageLoopProxy() const;
229
230 private:
231 IOThread* const io_thread_; // Weak pointer, owned by BrowserProcess.
232 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_;
233
234 base::debug::LeakTracker<SystemURLRequestContextGetter> leak_tracker_;
235 };
236
237 SystemURLRequestContextGetter::SystemURLRequestContextGetter(
238 IOThread* io_thread)
239 : io_thread_(io_thread),
240 io_message_loop_proxy_(io_thread->message_loop_proxy()) {
241 }
242
243 SystemURLRequestContextGetter::~SystemURLRequestContextGetter() {}
244
245 net::URLRequestContext* SystemURLRequestContextGetter::GetURLRequestContext() {
246 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
247
248 if (!io_thread_->globals()->system_request_context)
249 io_thread_->InitSystemRequestContext();
250
251 return io_thread_->globals()->system_request_context;
252 }
253
254 scoped_refptr<base::MessageLoopProxy>
255 SystemURLRequestContextGetter::GetIOMessageLoopProxy() const {
256 return io_message_loop_proxy_;
257 }
258
201 // The IOThread object must outlive any tasks posted to the IO thread before the 259 // The IOThread object must outlive any tasks posted to the IO thread before the
202 // Quit task. 260 // Quit task.
203 DISABLE_RUNNABLE_METHOD_REFCOUNT(IOThread); 261 DISABLE_RUNNABLE_METHOD_REFCOUNT(IOThread);
204 262
205 IOThread::Globals::Globals() {} 263 IOThread::Globals::Globals() {}
206 264
207 IOThread::Globals::~Globals() {} 265 IOThread::Globals::~Globals() {}
208 266
209 // |local_state| is passed in explicitly in order to (1) reduce implicit 267 // |local_state| is passed in explicitly in order to (1) reduce implicit
210 // dependencies and (2) make IOThread more flexible for testing. 268 // dependencies and (2) make IOThread more flexible for testing.
(...skipping 26 matching lines...) Expand all
237 295
238 IOThread::Globals* IOThread::globals() { 296 IOThread::Globals* IOThread::globals() {
239 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 297 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
240 return globals_; 298 return globals_;
241 } 299 }
242 300
243 ChromeNetLog* IOThread::net_log() { 301 ChromeNetLog* IOThread::net_log() {
244 return net_log_; 302 return net_log_;
245 } 303 }
246 304
305 void IOThread::SetSystemProxyConfigService(
306 net::ProxyConfigService* system_proxy_config_service) {
307 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
308 system_proxy_config_service_.reset(system_proxy_config_service);
309 }
310
247 void IOThread::InitNetworkPredictor( 311 void IOThread::InitNetworkPredictor(
248 bool prefetching_enabled, 312 bool prefetching_enabled,
249 base::TimeDelta max_dns_queue_delay, 313 base::TimeDelta max_dns_queue_delay,
250 size_t max_speculative_parallel_resolves, 314 size_t max_speculative_parallel_resolves,
251 const chrome_common_net::UrlList& startup_urls, 315 const chrome_common_net::UrlList& startup_urls,
252 ListValue* referral_list, 316 ListValue* referral_list,
253 bool preconnect_enabled) { 317 bool preconnect_enabled) {
254 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 318 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
255 message_loop()->PostTask( 319 message_loop()->PostTask(
256 FROM_HERE, 320 FROM_HERE,
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
288 352
289 void IOThread::ChangedToOnTheRecord() { 353 void IOThread::ChangedToOnTheRecord() {
290 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 354 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
291 message_loop()->PostTask( 355 message_loop()->PostTask(
292 FROM_HERE, 356 FROM_HERE,
293 NewRunnableMethod( 357 NewRunnableMethod(
294 this, 358 this,
295 &IOThread::ChangedToOnTheRecordOnIOThread)); 359 &IOThread::ChangedToOnTheRecordOnIOThread));
296 } 360 }
297 361
362 scoped_refptr<URLRequestContextGetter>
363 IOThread::system_url_request_context_getter() {
364 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
365 if (!system_url_request_context_getter_) {
366 // We DCHECK only the first time this function is called as the
367 // SystemURLRequestContextGetter will consume the stored value and reset
368 // the pointer to NULL.
369 DCHECK(system_proxy_config_service_.get());
370 system_url_request_context_getter_ =
371 new SystemURLRequestContextGetter(this);
372 }
373 return system_url_request_context_getter_;
374 }
375
298 void IOThread::Init() { 376 void IOThread::Init() {
299 // Though this thread is called the "IO" thread, it actually just routes 377 // Though this thread is called the "IO" thread, it actually just routes
300 // messages around; it shouldn't be allowed to perform any blocking disk I/O. 378 // messages around; it shouldn't be allowed to perform any blocking disk I/O.
301 base::ThreadRestrictions::SetIOAllowed(false); 379 base::ThreadRestrictions::SetIOAllowed(false);
302 380
303 BrowserProcessSubThread::Init(); 381 BrowserProcessSubThread::Init();
304 382
305 DCHECK_EQ(MessageLoop::TYPE_IO, message_loop()->type()); 383 DCHECK_EQ(MessageLoop::TYPE_IO, message_loop()->type());
306 384
307 #if defined(USE_NSS) 385 #if defined(USE_NSS)
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
374 it != url_request_context_getters.end(); ++it) { 452 it != url_request_context_getters.end(); ++it) {
375 ChromeURLRequestContextGetter* getter = *it; 453 ChromeURLRequestContextGetter* getter = *it;
376 // Stop all pending certificate provenance check uploads 454 // Stop all pending certificate provenance check uploads
377 net::DnsCertProvenanceChecker* checker = 455 net::DnsCertProvenanceChecker* checker =
378 getter->GetURLRequestContext()->dns_cert_checker(); 456 getter->GetURLRequestContext()->dns_cert_checker();
379 if (checker) 457 if (checker)
380 checker->Shutdown(); 458 checker->Shutdown();
381 getter->ReleaseURLRequestContext(); 459 getter->ReleaseURLRequestContext();
382 } 460 }
383 461
462 system_url_request_context_getter_ = NULL;
463
384 // Step 2: Release objects that the net::URLRequestContext could have been 464 // Step 2: Release objects that the net::URLRequestContext could have been
385 // pointing to. 465 // pointing to.
386 466
387 // This must be reset before the ChromeNetLog is destroyed. 467 // This must be reset before the ChromeNetLog is destroyed.
388 network_change_observer_.reset(); 468 network_change_observer_.reset();
389 469
390 // Not initialized in Init(). May not be initialized. 470 // Not initialized in Init(). May not be initialized.
391 if (predictor_) { 471 if (predictor_) {
392 predictor_->Shutdown(); 472 predictor_->Shutdown();
393 473
394 // TODO(willchan): Stop reference counting Predictor. It's owned by 474 // TODO(willchan): Stop reference counting Predictor. It's owned by
395 // IOThread now. 475 // IOThread now.
396 predictor_->Release(); 476 predictor_->Release();
397 predictor_ = NULL; 477 predictor_ = NULL;
398 chrome_browser_net::FreePredictorResources(); 478 chrome_browser_net::FreePredictorResources();
399 } 479 }
400 480
401 // Deletion will unregister this interceptor. 481 // Deletion will unregister this interceptor.
402 delete speculative_interceptor_; 482 delete speculative_interceptor_;
403 speculative_interceptor_ = NULL; 483 speculative_interceptor_ = NULL;
404 484
405 // TODO(eroman): hack for http://crbug.com/15513 485 // TODO(eroman): hack for http://crbug.com/15513
406 if (globals_->host_resolver->GetAsHostResolverImpl()) { 486 if (globals_->host_resolver->GetAsHostResolverImpl()) {
407 globals_->host_resolver.get()->GetAsHostResolverImpl()->Shutdown(); 487 globals_->host_resolver.get()->GetAsHostResolverImpl()->Shutdown();
408 } 488 }
409 489
490 system_proxy_config_service_.reset();
491
410 delete globals_; 492 delete globals_;
411 globals_ = NULL; 493 globals_ = NULL;
412 494
413 BrowserProcessSubThread::CleanUp(); 495 BrowserProcessSubThread::CleanUp();
414 } 496 }
415 497
416 void IOThread::CleanUpAfterMessageLoopDestruction() { 498 void IOThread::CleanUpAfterMessageLoopDestruction() {
417 // This will delete the |notification_service_|. Make sure it's done after 499 // This will delete the |notification_service_|. Make sure it's done after
418 // anything else can reference it. 500 // anything else can reference it.
419 BrowserProcessSubThread::CleanUpAfterMessageLoopDestruction(); 501 BrowserProcessSubThread::CleanUpAfterMessageLoopDestruction();
420 502
421 // net::URLRequest instances must NOT outlive the IO thread. 503 // net::URLRequest instances must NOT outlive the IO thread.
422 // 504 //
423 // To allow for URLRequests to be deleted from 505 // To allow for URLRequests to be deleted from
424 // MessageLoop::DestructionObserver this check has to happen after CleanUp 506 // MessageLoop::DestructionObserver this check has to happen after CleanUp
425 // (which runs before DestructionObservers). 507 // (which runs before DestructionObservers).
426 base::debug::LeakTracker<net::URLRequest>::CheckForLeaks(); 508 base::debug::LeakTracker<net::URLRequest>::CheckForLeaks();
509
510 base::debug::LeakTracker<SystemURLRequestContextGetter>::CheckForLeaks();
427 } 511 }
428 512
429 // static 513 // static
430 void IOThread::RegisterPrefs(PrefService* local_state) { 514 void IOThread::RegisterPrefs(PrefService* local_state) {
431 local_state->RegisterStringPref(prefs::kAuthSchemes, 515 local_state->RegisterStringPref(prefs::kAuthSchemes,
432 "basic,digest,ntlm,negotiate"); 516 "basic,digest,ntlm,negotiate");
433 local_state->RegisterBooleanPref(prefs::kDisableAuthNegotiateCnameLookup, 517 local_state->RegisterBooleanPref(prefs::kDisableAuthNegotiateCnameLookup,
434 false); 518 false);
435 local_state->RegisterBooleanPref(prefs::kEnableAuthNegotiatePort, false); 519 local_state->RegisterBooleanPref(prefs::kEnableAuthNegotiatePort, false);
436 local_state->RegisterStringPref(prefs::kAuthServerWhitelist, ""); 520 local_state->RegisterStringPref(prefs::kAuthServerWhitelist, "");
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
512 void IOThread::ClearHostCache() { 596 void IOThread::ClearHostCache() {
513 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 597 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
514 598
515 if (globals_->host_resolver->GetAsHostResolverImpl()) { 599 if (globals_->host_resolver->GetAsHostResolverImpl()) {
516 net::HostCache* host_cache = 600 net::HostCache* host_cache =
517 globals_->host_resolver.get()->GetAsHostResolverImpl()->cache(); 601 globals_->host_resolver.get()->GetAsHostResolverImpl()->cache();
518 if (host_cache) 602 if (host_cache)
519 host_cache->clear(); 603 host_cache->clear();
520 } 604 }
521 } 605 }
606
607 void IOThread::InitSystemRequestContext() {
608 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
609 DCHECK(!globals_->system_proxy_service);
610 DCHECK(system_proxy_config_service_.get());
611
612 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
613 globals_->system_proxy_service =
614 ProxyServiceFactory::CreateProxyService(
615 net_log_,
616 globals_->proxy_script_fetcher_context,
617 system_proxy_config_service_.release(),
618 command_line);
619 net::HttpNetworkSession::Params system_params;
620 system_params.host_resolver = globals_->host_resolver.get();
621 system_params.cert_verifier = globals_->cert_verifier.get();
622 system_params.dnsrr_resolver = globals_->dnsrr_resolver.get();
623 system_params.dns_cert_checker = NULL;
624 system_params.ssl_host_info_factory = NULL;
625 system_params.proxy_service = globals_->system_proxy_service.get();
626 system_params.ssl_config_service = globals_->ssl_config_service.get();
627 system_params.http_auth_handler_factory =
628 globals_->http_auth_handler_factory.get();
629 system_params.network_delegate = &globals_->network_delegate;
630 system_params.net_log = net_log_;
631 globals_->system_http_transaction_factory.reset(
632 new net::HttpNetworkLayer(
633 new net::HttpNetworkSession(system_params)));
634 globals_->system_request_context =
635 ConstructSystemRequestContext(globals_, net_log_);
636 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698