Add more checks to investigate SupervisedUserPrefStore crash at startup.
[chromium-blink-merge.git] / chrome / browser / io_thread.cc
blob72a745b66e1fb87b5d77f2577e4dca699e46a4e1
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/io_thread.h"
7 #include <vector>
9 #include "base/base64.h"
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/command_line.h"
13 #include "base/compiler_specific.h"
14 #include "base/debug/leak_tracker.h"
15 #include "base/logging.h"
16 #include "base/metrics/field_trial.h"
17 #include "base/metrics/user_metrics.h"
18 #include "base/prefs/pref_registry_simple.h"
19 #include "base/prefs/pref_service.h"
20 #include "base/profiler/scoped_tracker.h"
21 #include "base/stl_util.h"
22 #include "base/strings/string_number_conversions.h"
23 #include "base/strings/string_piece.h"
24 #include "base/strings/string_split.h"
25 #include "base/strings/string_util.h"
26 #include "base/threading/sequenced_worker_pool.h"
27 #include "base/threading/thread.h"
28 #include "base/threading/worker_pool.h"
29 #include "base/time/time.h"
30 #include "base/trace_event/trace_event.h"
31 #include "build/build_config.h"
32 #include "chrome/browser/browser_process.h"
33 #include "chrome/browser/net/async_dns_field_trial.h"
34 #include "chrome/browser/net/chrome_net_log.h"
35 #include "chrome/browser/net/chrome_network_delegate.h"
36 #include "chrome/browser/net/connect_interceptor.h"
37 #include "chrome/browser/net/dns_probe_service.h"
38 #include "chrome/browser/net/pref_proxy_config_tracker.h"
39 #include "chrome/browser/net/proxy_service_factory.h"
40 #include "chrome/common/chrome_content_client.h"
41 #include "chrome/common/chrome_switches.h"
42 #include "chrome/common/chrome_version_info.h"
43 #include "chrome/common/pref_names.h"
44 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_prefs.h"
45 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h"
46 #include "components/policy/core/common/policy_service.h"
47 #include "components/variations/variations_associated_data.h"
48 #include "content/public/browser/browser_thread.h"
49 #include "content/public/browser/cookie_store_factory.h"
50 #include "net/base/host_mapping_rules.h"
51 #include "net/base/net_util.h"
52 #include "net/cert/cert_policy_enforcer.h"
53 #include "net/cert/cert_verifier.h"
54 #include "net/cert/cert_verify_proc.h"
55 #include "net/cert/ct_known_logs.h"
56 #include "net/cert/ct_known_logs_static.h"
57 #include "net/cert/ct_log_verifier.h"
58 #include "net/cert/ct_verifier.h"
59 #include "net/cert/multi_log_ct_verifier.h"
60 #include "net/cert/multi_threaded_cert_verifier.h"
61 #include "net/cookies/cookie_store.h"
62 #include "net/dns/host_cache.h"
63 #include "net/dns/host_resolver.h"
64 #include "net/dns/mapped_host_resolver.h"
65 #include "net/ftp/ftp_network_layer.h"
66 #include "net/http/http_auth_filter.h"
67 #include "net/http/http_auth_handler_factory.h"
68 #include "net/http/http_network_layer.h"
69 #include "net/http/http_server_properties_impl.h"
70 #include "net/proxy/proxy_config_service.h"
71 #include "net/proxy/proxy_script_fetcher_impl.h"
72 #include "net/proxy/proxy_service.h"
73 #include "net/quic/crypto/crypto_protocol.h"
74 #include "net/quic/quic_protocol.h"
75 #include "net/quic/quic_utils.h"
76 #include "net/socket/tcp_client_socket.h"
77 #include "net/spdy/spdy_session.h"
78 #include "net/ssl/channel_id_service.h"
79 #include "net/ssl/default_channel_id_store.h"
80 #include "net/url_request/data_protocol_handler.h"
81 #include "net/url_request/file_protocol_handler.h"
82 #include "net/url_request/ftp_protocol_handler.h"
83 #include "net/url_request/static_http_user_agent_settings.h"
84 #include "net/url_request/url_fetcher.h"
85 #include "net/url_request/url_request_context.h"
86 #include "net/url_request/url_request_context_getter.h"
87 #include "net/url_request/url_request_job_factory_impl.h"
88 #include "net/url_request/url_request_throttler_manager.h"
89 #include "url/url_constants.h"
91 #if defined(ENABLE_CONFIGURATION_POLICY)
92 #include "policy/policy_constants.h"
93 #endif
95 #if defined(ENABLE_EXTENSIONS)
96 #include "chrome/browser/extensions/event_router_forwarder.h"
97 #endif
99 #if defined(USE_NSS) || defined(OS_IOS)
100 #include "net/ocsp/nss_ocsp.h"
101 #endif
103 #if defined(OS_ANDROID)
104 #include "base/android/build_info.h"
105 #endif
107 #if defined(OS_CHROMEOS)
108 #include "chrome/browser/chromeos/net/cert_verify_proc_chromeos.h"
109 #include "chromeos/network/host_resolver_impl_chromeos.h"
110 #endif
112 using content::BrowserThread;
114 class SafeBrowsingURLRequestContext;
116 // The IOThread object must outlive any tasks posted to the IO thread before the
117 // Quit task, so base::Bind() calls are not refcounted.
119 namespace {
121 const char kTCPFastOpenFieldTrialName[] = "TCPFastOpen";
122 const char kTCPFastOpenHttpsEnabledGroupName[] = "HttpsEnabled";
124 const char kQuicFieldTrialName[] = "QUIC";
125 const char kQuicFieldTrialEnabledGroupName[] = "Enabled";
126 const char kQuicFieldTrialHttpsEnabledGroupName[] = "HttpsEnabled";
128 // The SPDY trial composes two different trial plus control groups:
129 // * A "holdback" group with SPDY disabled, and corresponding control
130 // (SPDY/3.1). The primary purpose of the holdback group is to encourage site
131 // operators to do feature detection rather than UA-sniffing. As such, this
132 // trial runs continuously.
133 // * A SPDY/4 experiment, for SPDY/4 (aka HTTP/2) vs SPDY/3.1 comparisons and
134 // eventual SPDY/4 deployment.
135 const char kSpdyFieldTrialName[] = "SPDY";
136 const char kSpdyFieldTrialHoldbackGroupNamePrefix[] = "SpdyDisabled";
137 const char kSpdyFieldTrialHoldbackControlGroupName[] = "Control";
138 const char kSpdyFieldTrialSpdy31GroupNamePrefix[] = "Spdy31Enabled";
139 const char kSpdyFieldTrialSpdy4GroupNamePrefix[] = "Spdy4Enabled";
140 const char kSpdyFieldTrialSpdy4ControlGroupName[] = "Spdy4Control";
142 // Field trial for Cache-Control: stale-while-revalidate directive.
143 const char kStaleWhileRevalidateFieldTrialName[] = "StaleWhileRevalidate";
145 #if defined(OS_MACOSX) && !defined(OS_IOS)
146 void ObserveKeychainEvents() {
147 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
148 net::CertDatabase::GetInstance()->SetMessageLoopForKeychainEvents();
150 #endif
152 // Used for the "system" URLRequestContext.
153 class SystemURLRequestContext : public net::URLRequestContext {
154 public:
155 SystemURLRequestContext() {
156 #if defined(USE_NSS) || defined(OS_IOS)
157 net::SetURLRequestContextForNSSHttpIO(this);
158 #endif
161 private:
162 ~SystemURLRequestContext() override {
163 AssertNoURLRequests();
164 #if defined(USE_NSS) || defined(OS_IOS)
165 net::SetURLRequestContextForNSSHttpIO(NULL);
166 #endif
170 scoped_ptr<net::HostResolver> CreateGlobalHostResolver(net::NetLog* net_log) {
171 TRACE_EVENT0("startup", "IOThread::CreateGlobalHostResolver");
172 const base::CommandLine& command_line =
173 *base::CommandLine::ForCurrentProcess();
175 net::HostResolver::Options options;
177 // Use the retry attempts override from the command-line, if any.
178 if (command_line.HasSwitch(switches::kHostResolverRetryAttempts)) {
179 std::string s =
180 command_line.GetSwitchValueASCII(switches::kHostResolverRetryAttempts);
181 // Parse the switch (it should be a non-negative integer).
182 int n;
183 if (base::StringToInt(s, &n) && n >= 0) {
184 options.max_retry_attempts = static_cast<size_t>(n);
185 } else {
186 LOG(ERROR) << "Invalid switch for host resolver retry attempts: " << s;
190 scoped_ptr<net::HostResolver> global_host_resolver;
191 #if defined OS_CHROMEOS
192 global_host_resolver =
193 chromeos::HostResolverImplChromeOS::CreateSystemResolver(options,
194 net_log);
195 #else
196 global_host_resolver =
197 net::HostResolver::CreateSystemResolver(options, net_log);
198 #endif
200 // Determine if we should disable IPv6 support.
201 if (command_line.HasSwitch(switches::kEnableIPv6)) {
202 // Disable IPv6 probing.
203 global_host_resolver->SetDefaultAddressFamily(
204 net::ADDRESS_FAMILY_UNSPECIFIED);
205 } else if (command_line.HasSwitch(switches::kDisableIPv6)) {
206 global_host_resolver->SetDefaultAddressFamily(net::ADDRESS_FAMILY_IPV4);
209 // If hostname remappings were specified on the command-line, layer these
210 // rules on top of the real host resolver. This allows forwarding all requests
211 // through a designated test server.
212 if (!command_line.HasSwitch(switches::kHostResolverRules))
213 return global_host_resolver.Pass();
215 scoped_ptr<net::MappedHostResolver> remapped_resolver(
216 new net::MappedHostResolver(global_host_resolver.Pass()));
217 remapped_resolver->SetRulesFromString(
218 command_line.GetSwitchValueASCII(switches::kHostResolverRules));
219 return remapped_resolver.Pass();
222 // TODO(willchan): Remove proxy script fetcher context since it's not necessary
223 // now that I got rid of refcounting URLRequestContexts.
224 // See IOThread::Globals for details.
225 net::URLRequestContext*
226 ConstructProxyScriptFetcherContext(IOThread::Globals* globals,
227 net::NetLog* net_log) {
228 net::URLRequestContext* context = new net::URLRequestContext;
229 context->set_net_log(net_log);
230 context->set_host_resolver(globals->host_resolver.get());
231 context->set_cert_verifier(globals->cert_verifier.get());
232 context->set_transport_security_state(
233 globals->transport_security_state.get());
234 context->set_cert_transparency_verifier(
235 globals->cert_transparency_verifier.get());
236 context->set_http_auth_handler_factory(
237 globals->http_auth_handler_factory.get());
238 context->set_proxy_service(globals->proxy_script_fetcher_proxy_service.get());
239 context->set_http_transaction_factory(
240 globals->proxy_script_fetcher_http_transaction_factory.get());
241 context->set_job_factory(
242 globals->proxy_script_fetcher_url_request_job_factory.get());
243 context->set_cookie_store(globals->system_cookie_store.get());
244 context->set_channel_id_service(
245 globals->system_channel_id_service.get());
246 context->set_network_delegate(globals->system_network_delegate.get());
247 context->set_http_user_agent_settings(
248 globals->http_user_agent_settings.get());
249 // TODO(rtenneti): We should probably use HttpServerPropertiesManager for the
250 // system URLRequestContext too. There's no reason this should be tied to a
251 // profile.
252 return context;
255 net::URLRequestContext*
256 ConstructSystemRequestContext(IOThread::Globals* globals,
257 net::NetLog* net_log) {
258 // TODO(michaeln): Remove ScopedTracker below once crbug.com/454983 is fixed.
259 tracked_objects::ScopedTracker tracking_profile(
260 FROM_HERE_WITH_EXPLICIT_FUNCTION(
261 "454983 ConstructSystemRequestContext"));
262 net::URLRequestContext* context = new SystemURLRequestContext;
263 context->set_net_log(net_log);
264 context->set_host_resolver(globals->host_resolver.get());
265 context->set_cert_verifier(globals->cert_verifier.get());
266 context->set_transport_security_state(
267 globals->transport_security_state.get());
268 context->set_cert_transparency_verifier(
269 globals->cert_transparency_verifier.get());
270 context->set_http_auth_handler_factory(
271 globals->http_auth_handler_factory.get());
272 context->set_proxy_service(globals->system_proxy_service.get());
273 context->set_http_transaction_factory(
274 globals->system_http_transaction_factory.get());
275 context->set_job_factory(globals->system_url_request_job_factory.get());
276 context->set_cookie_store(globals->system_cookie_store.get());
277 context->set_channel_id_service(
278 globals->system_channel_id_service.get());
279 context->set_throttler_manager(globals->throttler_manager.get());
280 context->set_network_delegate(globals->system_network_delegate.get());
281 context->set_http_user_agent_settings(
282 globals->http_user_agent_settings.get());
283 return context;
286 int GetSwitchValueAsInt(const base::CommandLine& command_line,
287 const std::string& switch_name) {
288 int value;
289 if (!base::StringToInt(command_line.GetSwitchValueASCII(switch_name),
290 &value)) {
291 return 0;
293 return value;
296 // Returns the value associated with |key| in |params| or "" if the
297 // key is not present in the map.
298 const std::string& GetVariationParam(
299 const std::map<std::string, std::string>& params,
300 const std::string& key) {
301 std::map<std::string, std::string>::const_iterator it = params.find(key);
302 if (it == params.end())
303 return base::EmptyString();
305 return it->second;
308 // Return true if stale-while-revalidate support should be enabled.
309 bool IsStaleWhileRevalidateEnabled(const base::CommandLine& command_line) {
310 if (command_line.HasSwitch(switches::kEnableStaleWhileRevalidate))
311 return true;
312 const std::string group_name =
313 base::FieldTrialList::FindFullName(kStaleWhileRevalidateFieldTrialName);
314 return group_name == "Enabled";
317 bool IsCertificateTransparencyRequiredForEV(
318 const base::CommandLine& command_line) {
319 const std::string group_name =
320 base::FieldTrialList::FindFullName("CTRequiredForEVTrial");
322 if (command_line.HasSwitch(switches::kRequireCTForEV))
323 return true;
325 return group_name == "RequirementEnforced";
328 } // namespace
330 class IOThread::LoggingNetworkChangeObserver
331 : public net::NetworkChangeNotifier::IPAddressObserver,
332 public net::NetworkChangeNotifier::ConnectionTypeObserver,
333 public net::NetworkChangeNotifier::NetworkChangeObserver {
334 public:
335 // |net_log| must remain valid throughout our lifetime.
336 explicit LoggingNetworkChangeObserver(net::NetLog* net_log)
337 : net_log_(net_log) {
338 net::NetworkChangeNotifier::AddIPAddressObserver(this);
339 net::NetworkChangeNotifier::AddConnectionTypeObserver(this);
340 net::NetworkChangeNotifier::AddNetworkChangeObserver(this);
343 ~LoggingNetworkChangeObserver() override {
344 net::NetworkChangeNotifier::RemoveIPAddressObserver(this);
345 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
346 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this);
349 // NetworkChangeNotifier::IPAddressObserver implementation.
350 void OnIPAddressChanged() override {
351 VLOG(1) << "Observed a change to the network IP addresses";
353 net_log_->AddGlobalEntry(net::NetLog::TYPE_NETWORK_IP_ADDRESSES_CHANGED);
356 // NetworkChangeNotifier::ConnectionTypeObserver implementation.
357 void OnConnectionTypeChanged(
358 net::NetworkChangeNotifier::ConnectionType type) override {
359 std::string type_as_string =
360 net::NetworkChangeNotifier::ConnectionTypeToString(type);
362 VLOG(1) << "Observed a change to network connectivity state "
363 << type_as_string;
365 net_log_->AddGlobalEntry(
366 net::NetLog::TYPE_NETWORK_CONNECTIVITY_CHANGED,
367 net::NetLog::StringCallback("new_connection_type", &type_as_string));
370 // NetworkChangeNotifier::NetworkChangeObserver implementation.
371 void OnNetworkChanged(
372 net::NetworkChangeNotifier::ConnectionType type) override {
373 std::string type_as_string =
374 net::NetworkChangeNotifier::ConnectionTypeToString(type);
376 VLOG(1) << "Observed a network change to state " << type_as_string;
378 net_log_->AddGlobalEntry(
379 net::NetLog::TYPE_NETWORK_CHANGED,
380 net::NetLog::StringCallback("new_connection_type", &type_as_string));
383 private:
384 net::NetLog* net_log_;
385 DISALLOW_COPY_AND_ASSIGN(LoggingNetworkChangeObserver);
388 class SystemURLRequestContextGetter : public net::URLRequestContextGetter {
389 public:
390 explicit SystemURLRequestContextGetter(IOThread* io_thread);
392 // Implementation for net::UrlRequestContextGetter.
393 net::URLRequestContext* GetURLRequestContext() override;
394 scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner()
395 const override;
397 protected:
398 ~SystemURLRequestContextGetter() override;
400 private:
401 IOThread* const io_thread_; // Weak pointer, owned by BrowserProcess.
402 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
404 base::debug::LeakTracker<SystemURLRequestContextGetter> leak_tracker_;
407 SystemURLRequestContextGetter::SystemURLRequestContextGetter(
408 IOThread* io_thread)
409 : io_thread_(io_thread),
410 network_task_runner_(
411 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)) {
414 SystemURLRequestContextGetter::~SystemURLRequestContextGetter() {}
416 net::URLRequestContext* SystemURLRequestContextGetter::GetURLRequestContext() {
417 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
418 DCHECK(io_thread_->globals()->system_request_context.get());
420 return io_thread_->globals()->system_request_context.get();
423 scoped_refptr<base::SingleThreadTaskRunner>
424 SystemURLRequestContextGetter::GetNetworkTaskRunner() const {
425 return network_task_runner_;
428 IOThread::Globals::
429 SystemRequestContextLeakChecker::SystemRequestContextLeakChecker(
430 Globals* globals)
431 : globals_(globals) {
432 DCHECK(globals_);
435 IOThread::Globals::
436 SystemRequestContextLeakChecker::~SystemRequestContextLeakChecker() {
437 if (globals_->system_request_context.get())
438 globals_->system_request_context->AssertNoURLRequests();
441 IOThread::Globals::Globals()
442 : system_request_context_leak_checker(this),
443 enable_ssl_connect_job_waiting(false),
444 ignore_certificate_errors(false),
445 use_stale_while_revalidate(false),
446 testing_fixed_http_port(0),
447 testing_fixed_https_port(0),
448 enable_user_alternate_protocol_ports(false) {
451 IOThread::Globals::~Globals() {}
453 // |local_state| is passed in explicitly in order to (1) reduce implicit
454 // dependencies and (2) make IOThread more flexible for testing.
455 IOThread::IOThread(
456 PrefService* local_state,
457 policy::PolicyService* policy_service,
458 ChromeNetLog* net_log,
459 extensions::EventRouterForwarder* extension_event_router_forwarder)
460 : net_log_(net_log),
461 #if defined(ENABLE_EXTENSIONS)
462 extension_event_router_forwarder_(extension_event_router_forwarder),
463 #endif
464 globals_(NULL),
465 is_spdy_disabled_by_policy_(false),
466 creation_time_(base::TimeTicks::Now()),
467 weak_factory_(this) {
468 auth_schemes_ = local_state->GetString(prefs::kAuthSchemes);
469 negotiate_disable_cname_lookup_ = local_state->GetBoolean(
470 prefs::kDisableAuthNegotiateCnameLookup);
471 negotiate_enable_port_ = local_state->GetBoolean(
472 prefs::kEnableAuthNegotiatePort);
473 auth_server_whitelist_ = local_state->GetString(prefs::kAuthServerWhitelist);
474 auth_delegate_whitelist_ = local_state->GetString(
475 prefs::kAuthNegotiateDelegateWhitelist);
476 gssapi_library_name_ = local_state->GetString(prefs::kGSSAPILibraryName);
477 pref_proxy_config_tracker_.reset(
478 ProxyServiceFactory::CreatePrefProxyConfigTrackerOfLocalState(
479 local_state));
480 ChromeNetworkDelegate::InitializePrefsOnUIThread(
481 &system_enable_referrers_,
482 NULL,
483 NULL,
484 NULL,
485 NULL,
486 local_state);
487 ssl_config_service_manager_.reset(
488 SSLConfigServiceManager::CreateDefaultManager(local_state));
490 base::Value* dns_client_enabled_default = new base::FundamentalValue(
491 chrome_browser_net::ConfigureAsyncDnsFieldTrial());
492 local_state->SetDefaultPrefValue(prefs::kBuiltInDnsClientEnabled,
493 dns_client_enabled_default);
494 chrome_browser_net::LogAsyncDnsPrefSource(
495 local_state->FindPreference(prefs::kBuiltInDnsClientEnabled));
497 dns_client_enabled_.Init(prefs::kBuiltInDnsClientEnabled,
498 local_state,
499 base::Bind(&IOThread::UpdateDnsClientEnabled,
500 base::Unretained(this)));
501 dns_client_enabled_.MoveToThread(
502 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
504 quick_check_enabled_.Init(prefs::kQuickCheckEnabled,
505 local_state);
506 quick_check_enabled_.MoveToThread(
507 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
509 #if defined(ENABLE_CONFIGURATION_POLICY)
510 is_spdy_disabled_by_policy_ = policy_service->GetPolicies(
511 policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME, std::string())).Get(
512 policy::key::kDisableSpdy) != NULL;
513 #endif // ENABLE_CONFIGURATION_POLICY
515 BrowserThread::SetDelegate(BrowserThread::IO, this);
518 IOThread::~IOThread() {
519 // This isn't needed for production code, but in tests, IOThread may
520 // be multiply constructed.
521 BrowserThread::SetDelegate(BrowserThread::IO, NULL);
523 pref_proxy_config_tracker_->DetachFromPrefService();
524 DCHECK(!globals_);
527 IOThread::Globals* IOThread::globals() {
528 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
529 return globals_;
532 void IOThread::SetGlobalsForTesting(Globals* globals) {
533 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
534 DCHECK(!globals || !globals_);
535 globals_ = globals;
538 ChromeNetLog* IOThread::net_log() {
539 return net_log_;
542 void IOThread::ChangedToOnTheRecord() {
543 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
544 BrowserThread::PostTask(
545 BrowserThread::IO,
546 FROM_HERE,
547 base::Bind(&IOThread::ChangedToOnTheRecordOnIOThread,
548 base::Unretained(this)));
551 net::URLRequestContextGetter* IOThread::system_url_request_context_getter() {
552 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
553 if (!system_url_request_context_getter_.get()) {
554 InitSystemRequestContext();
556 return system_url_request_context_getter_.get();
559 void IOThread::Init() {
560 // Prefer to use InitAsync unless you need initialization to block
561 // the UI thread
564 void IOThread::InitAsync() {
565 TRACE_EVENT0("startup", "IOThread::InitAsync");
566 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
568 #if defined(USE_NSS) || defined(OS_IOS)
569 net::SetMessageLoopForNSSHttpIO();
570 #endif
572 const base::CommandLine& command_line =
573 *base::CommandLine::ForCurrentProcess();
575 DCHECK(!globals_);
576 globals_ = new Globals;
578 // Add an observer that will emit network change events to the ChromeNetLog.
579 // Assuming NetworkChangeNotifier dispatches in FIFO order, we should be
580 // logging the network change before other IO thread consumers respond to it.
581 network_change_observer_.reset(
582 new LoggingNetworkChangeObserver(net_log_));
584 // Setup the HistogramWatcher to run on the IO thread.
585 net::NetworkChangeNotifier::InitHistogramWatcher();
587 #if defined(ENABLE_EXTENSIONS)
588 globals_->extension_event_router_forwarder =
589 extension_event_router_forwarder_;
590 #endif
592 scoped_ptr<ChromeNetworkDelegate> chrome_network_delegate(
593 new ChromeNetworkDelegate(extension_event_router_forwarder(),
594 &system_enable_referrers_));
596 if (command_line.HasSwitch(switches::kEnableClientHints))
597 chrome_network_delegate->SetEnableClientHints();
599 #if defined(ENABLE_EXTENSIONS)
600 if (command_line.HasSwitch(switches::kDisableExtensionsHttpThrottling))
601 chrome_network_delegate->NeverThrottleRequests();
602 #endif
604 globals_->system_network_delegate = chrome_network_delegate.Pass();
605 globals_->host_resolver = CreateGlobalHostResolver(net_log_);
606 UpdateDnsClientEnabled();
607 #if defined(OS_CHROMEOS)
608 // Creates a CertVerifyProc that doesn't allow any profile-provided certs.
609 globals_->cert_verifier.reset(new net::MultiThreadedCertVerifier(
610 new chromeos::CertVerifyProcChromeOS()));
611 #else
612 globals_->cert_verifier.reset(new net::MultiThreadedCertVerifier(
613 net::CertVerifyProc::CreateDefault()));
614 #endif
616 globals_->transport_security_state.reset(new net::TransportSecurityState());
618 net::MultiLogCTVerifier* ct_verifier = new net::MultiLogCTVerifier();
619 globals_->cert_transparency_verifier.reset(ct_verifier);
621 // Add built-in logs
622 ct_verifier->AddLogs(net::ct::CreateLogVerifiersForKnownLogs());
624 // Add logs from command line
625 if (command_line.HasSwitch(switches::kCertificateTransparencyLog)) {
626 std::string switch_value = command_line.GetSwitchValueASCII(
627 switches::kCertificateTransparencyLog);
628 std::vector<std::string> logs;
629 base::SplitString(switch_value, ',', &logs);
630 for (std::vector<std::string>::iterator it = logs.begin(); it != logs.end();
631 ++it) {
632 const std::string& curr_log = *it;
633 size_t delim_pos = curr_log.find(":");
634 CHECK(delim_pos != std::string::npos)
635 << "CT log description not provided (switch format"
636 " is 'description:base64_key')";
637 std::string log_description(curr_log.substr(0, delim_pos));
638 std::string ct_public_key_data;
639 CHECK(base::Base64Decode(curr_log.substr(delim_pos + 1),
640 &ct_public_key_data))
641 << "Unable to decode CT public key.";
642 scoped_ptr<net::CTLogVerifier> external_log_verifier(
643 net::CTLogVerifier::Create(ct_public_key_data, log_description));
644 CHECK(external_log_verifier) << "Unable to parse CT public key.";
645 VLOG(1) << "Adding log with description " << log_description;
646 ct_verifier->AddLog(external_log_verifier.Pass());
650 net::CertPolicyEnforcer* policy_enforcer = NULL;
651 policy_enforcer = new net::CertPolicyEnforcer(
652 IsCertificateTransparencyRequiredForEV(command_line));
653 globals_->cert_policy_enforcer.reset(policy_enforcer);
655 globals_->ssl_config_service = GetSSLConfigService();
657 globals_->http_auth_handler_factory.reset(CreateDefaultAuthHandlerFactory(
658 globals_->host_resolver.get()));
659 globals_->http_server_properties.reset(new net::HttpServerPropertiesImpl());
660 // For the ProxyScriptFetcher, we use a direct ProxyService.
661 globals_->proxy_script_fetcher_proxy_service.reset(
662 net::ProxyService::CreateDirectWithNetLog(net_log_));
663 // In-memory cookie store.
664 globals_->system_cookie_store =
665 content::CreateCookieStore(content::CookieStoreConfig());
666 // In-memory channel ID store.
667 globals_->system_channel_id_service.reset(
668 new net::ChannelIDService(
669 new net::DefaultChannelIDStore(NULL),
670 base::WorkerPool::GetTaskRunner(true)));
671 globals_->dns_probe_service.reset(new chrome_browser_net::DnsProbeService());
672 globals_->host_mapping_rules.reset(new net::HostMappingRules());
673 globals_->http_user_agent_settings.reset(
674 new net::StaticHttpUserAgentSettings(std::string(), GetUserAgent()));
675 if (command_line.HasSwitch(switches::kHostRules)) {
676 TRACE_EVENT_BEGIN0("startup", "IOThread::InitAsync:SetRulesFromString");
677 globals_->host_mapping_rules->SetRulesFromString(
678 command_line.GetSwitchValueASCII(switches::kHostRules));
679 TRACE_EVENT_END0("startup", "IOThread::InitAsync:SetRulesFromString");
681 if (command_line.HasSwitch(switches::kEnableSSLConnectJobWaiting))
682 globals_->enable_ssl_connect_job_waiting = true;
683 if (command_line.HasSwitch(switches::kIgnoreCertificateErrors))
684 globals_->ignore_certificate_errors = true;
685 globals_->use_stale_while_revalidate =
686 IsStaleWhileRevalidateEnabled(command_line);
687 if (command_line.HasSwitch(switches::kTestingFixedHttpPort)) {
688 globals_->testing_fixed_http_port =
689 GetSwitchValueAsInt(command_line, switches::kTestingFixedHttpPort);
691 if (command_line.HasSwitch(switches::kTestingFixedHttpsPort)) {
692 globals_->testing_fixed_https_port =
693 GetSwitchValueAsInt(command_line, switches::kTestingFixedHttpsPort);
695 ConfigureQuic(command_line);
696 if (command_line.HasSwitch(
697 switches::kEnableUserAlternateProtocolPorts)) {
698 globals_->enable_user_alternate_protocol_ports = true;
700 InitializeNetworkOptions(command_line);
702 net::HttpNetworkSession::Params session_params;
703 InitializeNetworkSessionParams(&session_params);
704 session_params.net_log = net_log_;
705 session_params.proxy_service =
706 globals_->proxy_script_fetcher_proxy_service.get();
708 TRACE_EVENT_BEGIN0("startup", "IOThread::InitAsync:HttpNetworkSession");
709 scoped_refptr<net::HttpNetworkSession> network_session(
710 new net::HttpNetworkSession(session_params));
711 globals_->proxy_script_fetcher_http_transaction_factory
712 .reset(new net::HttpNetworkLayer(network_session.get()));
713 TRACE_EVENT_END0("startup", "IOThread::InitAsync:HttpNetworkSession");
714 scoped_ptr<net::URLRequestJobFactoryImpl> job_factory(
715 new net::URLRequestJobFactoryImpl());
716 job_factory->SetProtocolHandler(url::kDataScheme,
717 new net::DataProtocolHandler());
718 job_factory->SetProtocolHandler(
719 url::kFileScheme,
720 new net::FileProtocolHandler(
721 content::BrowserThread::GetBlockingPool()->
722 GetTaskRunnerWithShutdownBehavior(
723 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)));
724 #if !defined(DISABLE_FTP_SUPPORT)
725 globals_->proxy_script_fetcher_ftp_transaction_factory.reset(
726 new net::FtpNetworkLayer(globals_->host_resolver.get()));
727 job_factory->SetProtocolHandler(
728 url::kFtpScheme,
729 new net::FtpProtocolHandler(
730 globals_->proxy_script_fetcher_ftp_transaction_factory.get()));
731 #endif
732 globals_->proxy_script_fetcher_url_request_job_factory = job_factory.Pass();
734 globals_->throttler_manager.reset(new net::URLRequestThrottlerManager());
735 globals_->throttler_manager->set_net_log(net_log_);
736 // Always done in production, disabled only for unit tests.
737 globals_->throttler_manager->set_enable_thread_checks(true);
739 globals_->proxy_script_fetcher_context.reset(
740 ConstructProxyScriptFetcherContext(globals_, net_log_));
742 #if defined(OS_MACOSX) && !defined(OS_IOS)
743 // Start observing Keychain events. This needs to be done on the UI thread,
744 // as Keychain services requires a CFRunLoop.
745 BrowserThread::PostTask(BrowserThread::UI,
746 FROM_HERE,
747 base::Bind(&ObserveKeychainEvents));
748 #endif
750 // InitSystemRequestContext turns right around and posts a task back
751 // to the IO thread, so we can't let it run until we know the IO
752 // thread has started.
754 // Note that since we are at BrowserThread::Init time, the UI thread
755 // is blocked waiting for the thread to start. Therefore, posting
756 // this task to the main thread's message loop here is guaranteed to
757 // get it onto the message loop while the IOThread object still
758 // exists. However, the message might not be processed on the UI
759 // thread until after IOThread is gone, so use a weak pointer.
760 BrowserThread::PostTask(BrowserThread::UI,
761 FROM_HERE,
762 base::Bind(&IOThread::InitSystemRequestContext,
763 weak_factory_.GetWeakPtr()));
766 void IOThread::CleanUp() {
767 base::debug::LeakTracker<SafeBrowsingURLRequestContext>::CheckForLeaks();
769 #if defined(USE_NSS) || defined(OS_IOS)
770 net::ShutdownNSSHttpIO();
771 #endif
773 system_url_request_context_getter_ = NULL;
775 // Release objects that the net::URLRequestContext could have been pointing
776 // to.
778 // Shutdown the HistogramWatcher on the IO thread.
779 net::NetworkChangeNotifier::ShutdownHistogramWatcher();
781 // This must be reset before the ChromeNetLog is destroyed.
782 network_change_observer_.reset();
784 system_proxy_config_service_.reset();
786 delete globals_;
787 globals_ = NULL;
789 base::debug::LeakTracker<SystemURLRequestContextGetter>::CheckForLeaks();
792 void IOThread::InitializeNetworkOptions(const base::CommandLine& command_line) {
793 // Only handle use-spdy command line flags if "spdy.disabled" preference is
794 // not disabled via policy.
795 if (is_spdy_disabled_by_policy_) {
796 base::FieldTrial* trial = base::FieldTrialList::Find(kSpdyFieldTrialName);
797 if (trial)
798 trial->Disable();
799 } else {
800 if (command_line.HasSwitch(switches::kTrustedSpdyProxy)) {
801 globals_->trusted_spdy_proxy.set(
802 command_line.GetSwitchValueASCII(switches::kTrustedSpdyProxy));
804 if (command_line.HasSwitch(switches::kIgnoreUrlFetcherCertRequests))
805 net::URLFetcher::SetIgnoreCertificateRequests(true);
807 if (command_line.HasSwitch(switches::kUseSpdy)) {
808 std::string spdy_mode =
809 command_line.GetSwitchValueASCII(switches::kUseSpdy);
810 EnableSpdy(spdy_mode);
811 } else if (command_line.HasSwitch(switches::kEnableSpdy4)) {
812 globals_->next_protos = net::NextProtosSpdy4Http2();
813 globals_->use_alternate_protocols.set(true);
814 } else if (command_line.HasSwitch(switches::kEnableNpnHttpOnly)) {
815 globals_->next_protos = net::NextProtosHttpOnly();
816 globals_->use_alternate_protocols.set(false);
817 } else {
818 // No SPDY command-line flags have been specified. Examine trial groups.
819 ConfigureSpdyFromTrial(
820 base::FieldTrialList::FindFullName(kSpdyFieldTrialName), globals_);
824 ConfigureTCPFastOpen(command_line);
826 // TODO(rch): Make the client socket factory a per-network session
827 // instance, constructed from a NetworkSession::Params, to allow us
828 // to move this option to IOThread::Globals &
829 // HttpNetworkSession::Params.
832 void IOThread::ConfigureTCPFastOpen(const base::CommandLine& command_line) {
833 const std::string trial_group =
834 base::FieldTrialList::FindFullName(kTCPFastOpenFieldTrialName);
835 if (trial_group == kTCPFastOpenHttpsEnabledGroupName)
836 globals_->enable_tcp_fast_open_for_ssl.set(true);
837 bool always_enable_if_supported =
838 command_line.HasSwitch(switches::kEnableTcpFastOpen);
839 // Check for OS support of TCP FastOpen, and turn it on for all connections
840 // if indicated by user.
841 net::CheckSupportAndMaybeEnableTCPFastOpen(always_enable_if_supported);
844 void IOThread::ConfigureSpdyFromTrial(base::StringPiece spdy_trial_group,
845 Globals* globals) {
846 if (spdy_trial_group.starts_with(kSpdyFieldTrialHoldbackGroupNamePrefix)) {
847 // TODO(jgraettinger): Use net::NextProtosHttpOnly() instead?
848 net::HttpStreamFactory::set_spdy_enabled(false);
849 } else if (spdy_trial_group == kSpdyFieldTrialHoldbackControlGroupName) {
850 // Use the current SPDY default (SPDY/3.1).
851 globals->next_protos = net::NextProtosSpdy31();
852 globals->use_alternate_protocols.set(true);
853 } else if (spdy_trial_group.starts_with(
854 kSpdyFieldTrialSpdy31GroupNamePrefix)) {
855 globals->next_protos = net::NextProtosSpdy31();
856 globals->use_alternate_protocols.set(true);
857 } else if (spdy_trial_group.starts_with(
858 kSpdyFieldTrialSpdy4GroupNamePrefix)) {
859 globals->next_protos = net::NextProtosSpdy4Http2();
860 globals->use_alternate_protocols.set(true);
861 } else if (spdy_trial_group == kSpdyFieldTrialSpdy4ControlGroupName) {
862 // This control group is pinned at SPDY/3.1.
863 globals->next_protos = net::NextProtosSpdy31();
864 globals->use_alternate_protocols.set(true);
865 } else {
866 // By default, enable HTTP/2.
867 globals->next_protos = net::NextProtosSpdy4Http2();
868 globals->use_alternate_protocols.set(true);
872 void IOThread::EnableSpdy(const std::string& mode) {
873 static const char kOff[] = "off";
874 static const char kSSL[] = "ssl";
875 static const char kDisableSSL[] = "no-ssl";
876 static const char kDisablePing[] = "no-ping";
877 static const char kExclude[] = "exclude"; // Hosts to exclude
878 static const char kDisableCompression[] = "no-compress";
879 static const char kDisableAltProtocols[] = "no-alt-protocols";
880 static const char kForceAltProtocols[] = "force-alt-protocols";
881 static const char kSingleDomain[] = "single-domain";
883 static const char kInitialMaxConcurrentStreams[] = "init-max-streams";
885 std::vector<std::string> spdy_options;
886 base::SplitString(mode, ',', &spdy_options);
888 for (std::vector<std::string>::iterator it = spdy_options.begin();
889 it != spdy_options.end(); ++it) {
890 const std::string& element = *it;
891 std::vector<std::string> name_value;
892 base::SplitString(element, '=', &name_value);
893 const std::string& option =
894 name_value.size() > 0 ? name_value[0] : std::string();
895 const std::string value =
896 name_value.size() > 1 ? name_value[1] : std::string();
898 if (option == kOff) {
899 net::HttpStreamFactory::set_spdy_enabled(false);
900 } else if (option == kDisableSSL) {
901 globals_->spdy_default_protocol.set(net::kProtoSPDY31);
902 globals_->force_spdy_over_ssl.set(false);
903 globals_->force_spdy_always.set(true);
904 } else if (option == kSSL) {
905 globals_->spdy_default_protocol.set(net::kProtoSPDY31);
906 globals_->force_spdy_over_ssl.set(true);
907 globals_->force_spdy_always.set(true);
908 } else if (option == kDisablePing) {
909 globals_->enable_spdy_ping_based_connection_checking.set(false);
910 } else if (option == kExclude) {
911 globals_->forced_spdy_exclusions.insert(
912 net::HostPortPair::FromURL(GURL(value)));
913 } else if (option == kDisableCompression) {
914 globals_->enable_spdy_compression.set(false);
915 } else if (option == kDisableAltProtocols) {
916 globals_->use_alternate_protocols.set(false);
917 } else if (option == kForceAltProtocols) {
918 net::AlternateProtocolInfo pair(443, net::NPN_SPDY_3, 1);
919 base::RecordAction(base::UserMetricsAction("Net.ForceAlternateProtocol"));
920 net::HttpServerPropertiesImpl::ForceAlternateProtocol(pair);
921 } else if (option == kSingleDomain) {
922 DVLOG(1) << "FORCING SINGLE DOMAIN";
923 globals_->force_spdy_single_domain.set(true);
924 } else if (option == kInitialMaxConcurrentStreams) {
925 int streams;
926 if (base::StringToInt(value, &streams))
927 globals_->initial_max_spdy_concurrent_streams.set(streams);
928 } else if (option.empty() && it == spdy_options.begin()) {
929 continue;
930 } else {
931 LOG(DFATAL) << "Unrecognized spdy option: " << option;
936 // static
937 void IOThread::RegisterPrefs(PrefRegistrySimple* registry) {
938 registry->RegisterStringPref(prefs::kAuthSchemes,
939 "basic,digest,ntlm,negotiate,"
940 "spdyproxy");
941 registry->RegisterBooleanPref(prefs::kDisableAuthNegotiateCnameLookup, false);
942 registry->RegisterBooleanPref(prefs::kEnableAuthNegotiatePort, false);
943 registry->RegisterStringPref(prefs::kAuthServerWhitelist, std::string());
944 registry->RegisterStringPref(prefs::kAuthNegotiateDelegateWhitelist,
945 std::string());
946 registry->RegisterStringPref(prefs::kGSSAPILibraryName, std::string());
947 registry->RegisterStringPref(
948 data_reduction_proxy::prefs::kDataReductionProxy, std::string());
949 registry->RegisterBooleanPref(prefs::kEnableReferrers, true);
950 data_reduction_proxy::RegisterPrefs(registry);
951 registry->RegisterBooleanPref(prefs::kBuiltInDnsClientEnabled, true);
952 registry->RegisterBooleanPref(prefs::kQuickCheckEnabled, true);
955 net::HttpAuthHandlerFactory* IOThread::CreateDefaultAuthHandlerFactory(
956 net::HostResolver* resolver) {
957 net::HttpAuthFilterWhitelist* auth_filter_default_credentials = NULL;
958 if (!auth_server_whitelist_.empty()) {
959 auth_filter_default_credentials =
960 new net::HttpAuthFilterWhitelist(auth_server_whitelist_);
962 net::HttpAuthFilterWhitelist* auth_filter_delegate = NULL;
963 if (!auth_delegate_whitelist_.empty()) {
964 auth_filter_delegate =
965 new net::HttpAuthFilterWhitelist(auth_delegate_whitelist_);
967 globals_->url_security_manager.reset(
968 net::URLSecurityManager::Create(auth_filter_default_credentials,
969 auth_filter_delegate));
970 std::vector<std::string> supported_schemes;
971 base::SplitString(auth_schemes_, ',', &supported_schemes);
973 scoped_ptr<net::HttpAuthHandlerRegistryFactory> registry_factory(
974 net::HttpAuthHandlerRegistryFactory::Create(
975 supported_schemes, globals_->url_security_manager.get(),
976 resolver, gssapi_library_name_, negotiate_disable_cname_lookup_,
977 negotiate_enable_port_));
978 return registry_factory.release();
981 void IOThread::ClearHostCache() {
982 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
984 net::HostCache* host_cache = globals_->host_resolver->GetHostCache();
985 if (host_cache)
986 host_cache->clear();
989 void IOThread::InitializeNetworkSessionParams(
990 net::HttpNetworkSession::Params* params) {
991 // TODO(michaeln): Remove ScopedTracker below once crbug.com/454983 is fixed.
992 tracked_objects::ScopedTracker tracking_profile(
993 FROM_HERE_WITH_EXPLICIT_FUNCTION(
994 "454983 IOThread::InitializeNetworkSessionParams"));
995 InitializeNetworkSessionParamsFromGlobals(*globals_, params);
998 // static
999 void IOThread::InitializeNetworkSessionParamsFromGlobals(
1000 const IOThread::Globals& globals,
1001 net::HttpNetworkSession::Params* params) {
1002 params->host_resolver = globals.host_resolver.get();
1003 params->cert_verifier = globals.cert_verifier.get();
1004 params->cert_policy_enforcer = globals.cert_policy_enforcer.get();
1005 params->channel_id_service = globals.system_channel_id_service.get();
1006 params->transport_security_state = globals.transport_security_state.get();
1007 params->ssl_config_service = globals.ssl_config_service.get();
1008 params->http_auth_handler_factory = globals.http_auth_handler_factory.get();
1009 params->http_server_properties =
1010 globals.http_server_properties->GetWeakPtr();
1011 params->network_delegate = globals.system_network_delegate.get();
1012 params->host_mapping_rules = globals.host_mapping_rules.get();
1013 params->enable_ssl_connect_job_waiting =
1014 globals.enable_ssl_connect_job_waiting;
1015 params->ignore_certificate_errors = globals.ignore_certificate_errors;
1016 params->use_stale_while_revalidate = globals.use_stale_while_revalidate;
1017 params->testing_fixed_http_port = globals.testing_fixed_http_port;
1018 params->testing_fixed_https_port = globals.testing_fixed_https_port;
1019 globals.enable_tcp_fast_open_for_ssl.CopyToIfSet(
1020 &params->enable_tcp_fast_open_for_ssl);
1022 globals.initial_max_spdy_concurrent_streams.CopyToIfSet(
1023 &params->spdy_initial_max_concurrent_streams);
1024 globals.force_spdy_single_domain.CopyToIfSet(
1025 &params->force_spdy_single_domain);
1026 globals.enable_spdy_compression.CopyToIfSet(
1027 &params->enable_spdy_compression);
1028 globals.enable_spdy_ping_based_connection_checking.CopyToIfSet(
1029 &params->enable_spdy_ping_based_connection_checking);
1030 globals.spdy_default_protocol.CopyToIfSet(
1031 &params->spdy_default_protocol);
1032 params->next_protos = globals.next_protos;
1033 globals.trusted_spdy_proxy.CopyToIfSet(&params->trusted_spdy_proxy);
1034 globals.force_spdy_over_ssl.CopyToIfSet(&params->force_spdy_over_ssl);
1035 globals.force_spdy_always.CopyToIfSet(&params->force_spdy_always);
1036 params->forced_spdy_exclusions = globals.forced_spdy_exclusions;
1037 globals.use_alternate_protocols.CopyToIfSet(
1038 &params->use_alternate_protocols);
1039 globals.alternate_protocol_probability_threshold.CopyToIfSet(
1040 &params->alternate_protocol_probability_threshold);
1042 globals.enable_quic.CopyToIfSet(&params->enable_quic);
1043 globals.enable_quic_for_proxies.CopyToIfSet(&params->enable_quic_for_proxies);
1044 globals.quic_always_require_handshake_confirmation.CopyToIfSet(
1045 &params->quic_always_require_handshake_confirmation);
1046 globals.quic_disable_connection_pooling.CopyToIfSet(
1047 &params->quic_disable_connection_pooling);
1048 globals.quic_load_server_info_timeout_ms.CopyToIfSet(
1049 &params->quic_load_server_info_timeout_ms);
1050 globals.quic_load_server_info_timeout_srtt_multiplier.CopyToIfSet(
1051 &params->quic_load_server_info_timeout_srtt_multiplier);
1052 globals.quic_enable_truncated_connection_ids.CopyToIfSet(
1053 &params->quic_enable_truncated_connection_ids);
1054 globals.quic_enable_connection_racing.CopyToIfSet(
1055 &params->quic_enable_connection_racing);
1056 globals.enable_quic_port_selection.CopyToIfSet(
1057 &params->enable_quic_port_selection);
1058 globals.quic_max_packet_length.CopyToIfSet(&params->quic_max_packet_length);
1059 globals.quic_user_agent_id.CopyToIfSet(&params->quic_user_agent_id);
1060 globals.quic_supported_versions.CopyToIfSet(
1061 &params->quic_supported_versions);
1062 params->quic_connection_options = globals.quic_connection_options;
1064 globals.origin_to_force_quic_on.CopyToIfSet(
1065 &params->origin_to_force_quic_on);
1066 params->enable_user_alternate_protocol_ports =
1067 globals.enable_user_alternate_protocol_ports;
1070 base::TimeTicks IOThread::creation_time() const {
1071 return creation_time_;
1074 net::SSLConfigService* IOThread::GetSSLConfigService() {
1075 return ssl_config_service_manager_->Get();
1078 void IOThread::ChangedToOnTheRecordOnIOThread() {
1079 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
1081 // Clear the host cache to avoid showing entries from the OTR session
1082 // in about:net-internals.
1083 ClearHostCache();
1086 void IOThread::InitSystemRequestContext() {
1087 if (system_url_request_context_getter_.get())
1088 return;
1089 // If we're in unit_tests, IOThread may not be run.
1090 if (!BrowserThread::IsMessageLoopValid(BrowserThread::IO))
1091 return;
1092 system_proxy_config_service_.reset(
1093 ProxyServiceFactory::CreateProxyConfigService(
1094 pref_proxy_config_tracker_.get()));
1095 system_url_request_context_getter_ =
1096 new SystemURLRequestContextGetter(this);
1097 // Safe to post an unretained this pointer, since IOThread is
1098 // guaranteed to outlive the IO BrowserThread.
1099 BrowserThread::PostTask(
1100 BrowserThread::IO,
1101 FROM_HERE,
1102 base::Bind(&IOThread::InitSystemRequestContextOnIOThread,
1103 base::Unretained(this)));
1106 void IOThread::InitSystemRequestContextOnIOThread() {
1107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
1108 DCHECK(!globals_->system_proxy_service.get());
1109 DCHECK(system_proxy_config_service_.get());
1111 const base::CommandLine& command_line =
1112 *base::CommandLine::ForCurrentProcess();
1113 globals_->system_proxy_service.reset(
1114 ProxyServiceFactory::CreateProxyService(
1115 net_log_,
1116 globals_->proxy_script_fetcher_context.get(),
1117 globals_->system_network_delegate.get(),
1118 system_proxy_config_service_.release(),
1119 command_line,
1120 quick_check_enabled_.GetValue()));
1122 net::HttpNetworkSession::Params system_params;
1123 InitializeNetworkSessionParams(&system_params);
1124 system_params.net_log = net_log_;
1125 system_params.proxy_service = globals_->system_proxy_service.get();
1128 // TODO(michaeln): Remove after crbug.com/454983 is fixed.
1129 tracked_objects::ScopedTracker tracking_profile(
1130 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1131 "454983 IOThread::InitSystemRequestContextOnIOThread"
1132 ".HttpNetworkSession"));
1133 globals_->system_http_transaction_factory.reset(
1134 new net::HttpNetworkLayer(
1135 new net::HttpNetworkSession(system_params)));
1137 globals_->system_url_request_job_factory.reset(
1138 new net::URLRequestJobFactoryImpl());
1139 globals_->system_request_context.reset(
1140 ConstructSystemRequestContext(globals_, net_log_));
1141 globals_->system_request_context->set_ssl_config_service(
1142 globals_->ssl_config_service.get());
1143 globals_->system_request_context->set_http_server_properties(
1144 globals_->http_server_properties->GetWeakPtr());
1147 void IOThread::UpdateDnsClientEnabled() {
1148 globals()->host_resolver->SetDnsClientEnabled(*dns_client_enabled_);
1151 void IOThread::ConfigureQuic(const base::CommandLine& command_line) {
1152 // Always fetch the field trial group to ensure it is reported correctly.
1153 // The command line flags will be associated with a group that is reported
1154 // so long as trial is actually queried.
1155 std::string group =
1156 base::FieldTrialList::FindFullName(kQuicFieldTrialName);
1157 VariationParameters params;
1158 if (!variations::GetVariationParams(kQuicFieldTrialName, &params)) {
1159 params.clear();
1162 ConfigureQuicGlobals(command_line, group, params, globals_);
1165 // static
1166 void IOThread::ConfigureQuicGlobals(
1167 const base::CommandLine& command_line,
1168 base::StringPiece quic_trial_group,
1169 const VariationParameters& quic_trial_params,
1170 IOThread::Globals* globals) {
1171 bool enable_quic = ShouldEnableQuic(command_line, quic_trial_group);
1172 globals->enable_quic.set(enable_quic);
1173 bool enable_quic_for_proxies = ShouldEnableQuicForProxies(command_line,
1174 quic_trial_group);
1175 globals->enable_quic_for_proxies.set(enable_quic_for_proxies);
1176 if (enable_quic) {
1177 globals->quic_always_require_handshake_confirmation.set(
1178 ShouldQuicAlwaysRequireHandshakeConfirmation(quic_trial_params));
1179 globals->quic_disable_connection_pooling.set(
1180 ShouldQuicDisableConnectionPooling(quic_trial_params));
1181 int load_server_info_timeout_ms =
1182 GetQuicLoadServerInfoTimeout(quic_trial_params);
1183 if (load_server_info_timeout_ms != 0) {
1184 globals->quic_load_server_info_timeout_ms.set(
1185 load_server_info_timeout_ms);
1187 float load_server_info_timeout_srtt_multiplier =
1188 GetQuicLoadServerInfoTimeoutSrttMultiplier(quic_trial_params);
1189 globals->quic_enable_truncated_connection_ids.set(
1190 ShouldQuicEnableTruncatedConnectionIds(quic_trial_params));
1191 globals->quic_enable_connection_racing.set(
1192 ShouldQuicEnableConnectionRacing(quic_trial_params));
1193 if (load_server_info_timeout_srtt_multiplier != 0) {
1194 globals->quic_load_server_info_timeout_srtt_multiplier.set(
1195 load_server_info_timeout_srtt_multiplier);
1197 globals->enable_quic_port_selection.set(
1198 ShouldEnableQuicPortSelection(command_line));
1199 globals->quic_connection_options =
1200 GetQuicConnectionOptions(command_line, quic_trial_params);
1201 if (ShouldEnableQuicPacing(command_line, quic_trial_params)) {
1202 globals->quic_connection_options.push_back(net::kPACE);
1206 size_t max_packet_length = GetQuicMaxPacketLength(command_line,
1207 quic_trial_params);
1208 if (max_packet_length != 0) {
1209 globals->quic_max_packet_length.set(max_packet_length);
1212 std::string quic_user_agent_id =
1213 chrome::VersionInfo::GetVersionStringModifier();
1214 if (!quic_user_agent_id.empty())
1215 quic_user_agent_id.push_back(' ');
1216 chrome::VersionInfo version_info;
1217 quic_user_agent_id.append(version_info.ProductNameAndVersionForUserAgent());
1218 globals->quic_user_agent_id.set(quic_user_agent_id);
1220 net::QuicVersion version = GetQuicVersion(command_line, quic_trial_params);
1221 if (version != net::QUIC_VERSION_UNSUPPORTED) {
1222 net::QuicVersionVector supported_versions;
1223 supported_versions.push_back(version);
1224 globals->quic_supported_versions.set(supported_versions);
1227 double threshold =
1228 GetAlternateProtocolProbabilityThreshold(command_line, quic_trial_params);
1229 if (threshold >=0 && threshold <= 1) {
1230 globals->alternate_protocol_probability_threshold.set(threshold);
1231 globals->http_server_properties->SetAlternateProtocolProbabilityThreshold(
1232 threshold);
1235 if (command_line.HasSwitch(switches::kOriginToForceQuicOn)) {
1236 net::HostPortPair quic_origin =
1237 net::HostPortPair::FromString(
1238 command_line.GetSwitchValueASCII(switches::kOriginToForceQuicOn));
1239 if (!quic_origin.IsEmpty()) {
1240 globals->origin_to_force_quic_on.set(quic_origin);
1245 bool IOThread::ShouldEnableQuic(const base::CommandLine& command_line,
1246 base::StringPiece quic_trial_group) {
1247 if (command_line.HasSwitch(switches::kDisableQuic))
1248 return false;
1250 if (command_line.HasSwitch(switches::kEnableQuic))
1251 return true;
1253 return quic_trial_group.starts_with(kQuicFieldTrialEnabledGroupName) ||
1254 quic_trial_group.starts_with(kQuicFieldTrialHttpsEnabledGroupName);
1257 // static
1258 bool IOThread::ShouldEnableQuicForProxies(const base::CommandLine& command_line,
1259 base::StringPiece quic_trial_group) {
1260 return ShouldEnableQuic(command_line, quic_trial_group) ||
1261 ShouldEnableQuicForDataReductionProxy();
1264 // static
1265 bool IOThread::ShouldEnableQuicForDataReductionProxy() {
1266 const base::CommandLine& command_line =
1267 *base::CommandLine::ForCurrentProcess();
1269 if (command_line.HasSwitch(switches::kDisableQuic))
1270 return false;
1272 return data_reduction_proxy::DataReductionProxyParams::
1273 IsIncludedInQuicFieldTrial();
1276 bool IOThread::ShouldEnableQuicPortSelection(
1277 const base::CommandLine& command_line) {
1278 if (command_line.HasSwitch(switches::kDisableQuicPortSelection))
1279 return false;
1281 if (command_line.HasSwitch(switches::kEnableQuicPortSelection))
1282 return true;
1284 return false; // Default to disabling port selection on all channels.
1287 bool IOThread::ShouldEnableQuicPacing(
1288 const base::CommandLine& command_line,
1289 const VariationParameters& quic_trial_params) {
1290 if (command_line.HasSwitch(switches::kEnableQuicPacing))
1291 return true;
1293 if (command_line.HasSwitch(switches::kDisableQuicPacing))
1294 return false;
1296 return LowerCaseEqualsASCII(
1297 GetVariationParam(quic_trial_params, "enable_pacing"),
1298 "true");
1301 net::QuicTagVector IOThread::GetQuicConnectionOptions(
1302 const base::CommandLine& command_line,
1303 const VariationParameters& quic_trial_params) {
1304 if (command_line.HasSwitch(switches::kQuicConnectionOptions)) {
1305 return net::QuicUtils::ParseQuicConnectionOptions(
1306 command_line.GetSwitchValueASCII(switches::kQuicConnectionOptions));
1309 VariationParameters::const_iterator it =
1310 quic_trial_params.find("connection_options");
1311 if (it == quic_trial_params.end()) {
1312 return net::QuicTagVector();
1315 return net::QuicUtils::ParseQuicConnectionOptions(it->second);
1318 // static
1319 double IOThread::GetAlternateProtocolProbabilityThreshold(
1320 const base::CommandLine& command_line,
1321 const VariationParameters& quic_trial_params) {
1322 double value;
1323 if (command_line.HasSwitch(
1324 switches::kAlternateProtocolProbabilityThreshold)) {
1325 if (base::StringToDouble(
1326 command_line.GetSwitchValueASCII(
1327 switches::kAlternateProtocolProbabilityThreshold),
1328 &value)) {
1329 return value;
1332 if (command_line.HasSwitch(switches::kEnableQuic)) {
1333 return 0;
1335 if (base::StringToDouble(
1336 GetVariationParam(quic_trial_params,
1337 "alternate_protocol_probability_threshold"),
1338 &value)) {
1339 return value;
1341 return -1;
1344 // static
1345 bool IOThread::ShouldQuicAlwaysRequireHandshakeConfirmation(
1346 const VariationParameters& quic_trial_params) {
1347 return LowerCaseEqualsASCII(
1348 GetVariationParam(quic_trial_params,
1349 "always_require_handshake_confirmation"),
1350 "true");
1353 // static
1354 bool IOThread::ShouldQuicDisableConnectionPooling(
1355 const VariationParameters& quic_trial_params) {
1356 return LowerCaseEqualsASCII(
1357 GetVariationParam(quic_trial_params, "disable_connection_pooling"),
1358 "true");
1361 // static
1362 int IOThread::GetQuicLoadServerInfoTimeout(
1363 const VariationParameters& quic_trial_params) {
1364 int value;
1365 if (base::StringToInt(GetVariationParam(quic_trial_params,
1366 "load_server_info_timeout"),
1367 &value)) {
1368 return value;
1370 return 0;
1373 // static
1374 float IOThread::GetQuicLoadServerInfoTimeoutSrttMultiplier(
1375 const VariationParameters& quic_trial_params) {
1376 double value;
1377 if (base::StringToDouble(GetVariationParam(quic_trial_params,
1378 "load_server_info_time_to_srtt"),
1379 &value)) {
1380 return (float)value;
1382 return 0.0f;
1385 // static
1386 bool IOThread::ShouldQuicEnableTruncatedConnectionIds(
1387 const VariationParameters& quic_trial_params) {
1388 return LowerCaseEqualsASCII(
1389 GetVariationParam(quic_trial_params, "enable_truncated_connection_ids"),
1390 "true");
1393 // static
1394 bool IOThread::ShouldQuicEnableConnectionRacing(
1395 const VariationParameters& quic_trial_params) {
1396 return LowerCaseEqualsASCII(
1397 GetVariationParam(quic_trial_params, "enable_connection_racing"),
1398 "true");
1401 // static
1402 size_t IOThread::GetQuicMaxPacketLength(
1403 const base::CommandLine& command_line,
1404 const VariationParameters& quic_trial_params) {
1405 if (command_line.HasSwitch(switches::kQuicMaxPacketLength)) {
1406 unsigned value;
1407 if (!base::StringToUint(
1408 command_line.GetSwitchValueASCII(switches::kQuicMaxPacketLength),
1409 &value)) {
1410 return 0;
1412 return value;
1415 unsigned value;
1416 if (base::StringToUint(GetVariationParam(quic_trial_params,
1417 "max_packet_length"),
1418 &value)) {
1419 return value;
1421 return 0;
1424 // static
1425 net::QuicVersion IOThread::GetQuicVersion(
1426 const base::CommandLine& command_line,
1427 const VariationParameters& quic_trial_params) {
1428 if (command_line.HasSwitch(switches::kQuicVersion)) {
1429 return ParseQuicVersion(
1430 command_line.GetSwitchValueASCII(switches::kQuicVersion));
1433 return ParseQuicVersion(GetVariationParam(quic_trial_params, "quic_version"));
1436 // static
1437 net::QuicVersion IOThread::ParseQuicVersion(const std::string& quic_version) {
1438 net::QuicVersionVector supported_versions = net::QuicSupportedVersions();
1439 for (size_t i = 0; i < supported_versions.size(); ++i) {
1440 net::QuicVersion version = supported_versions[i];
1441 if (net::QuicVersionToString(version) == quic_version) {
1442 return version;
1446 return net::QUIC_VERSION_UNSUPPORTED;