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

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

Powered by Google App Engine
This is Rietveld 408576698