Componentize component_updater: Copy over test data with executable bit.
[chromium-blink-merge.git] / chrome / browser / io_thread.cc
blob7726fcf5524bddfd32c81c0fd5b953f5d32749a3
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/debug/trace_event.h"
16 #include "base/logging.h"
17 #include "base/metrics/field_trial.h"
18 #include "base/prefs/pref_registry_simple.h"
19 #include "base/prefs/pref_service.h"
20 #include "base/stl_util.h"
21 #include "base/strings/string_number_conversions.h"
22 #include "base/strings/string_split.h"
23 #include "base/strings/string_util.h"
24 #include "base/threading/sequenced_worker_pool.h"
25 #include "base/threading/thread.h"
26 #include "base/threading/worker_pool.h"
27 #include "base/time/time.h"
28 #include "build/build_config.h"
29 #include "chrome/browser/browser_process.h"
30 #include "chrome/browser/net/async_dns_field_trial.h"
31 #include "chrome/browser/net/chrome_net_log.h"
32 #include "chrome/browser/net/chrome_network_delegate.h"
33 #include "chrome/browser/net/connect_interceptor.h"
34 #include "chrome/browser/net/dns_probe_service.h"
35 #include "chrome/browser/net/pref_proxy_config_tracker.h"
36 #include "chrome/browser/net/proxy_service_factory.h"
37 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h"
38 #include "chrome/common/chrome_content_client.h"
39 #include "chrome/common/chrome_switches.h"
40 #include "chrome/common/chrome_version_info.h"
41 #include "chrome/common/pref_names.h"
42 #include "components/data_reduction_proxy/browser/data_reduction_proxy_prefs.h"
43 #include "components/policy/core/common/policy_service.h"
44 #include "components/variations/variations_associated_data.h"
45 #include "content/public/browser/browser_thread.h"
46 #include "content/public/browser/cookie_store_factory.h"
47 #include "net/base/host_mapping_rules.h"
48 #include "net/base/net_util.h"
49 #include "net/cert/cert_verifier.h"
50 #include "net/cert/cert_verify_proc.h"
51 #include "net/cert/ct_known_logs.h"
52 #include "net/cert/ct_verifier.h"
53 #include "net/cert/multi_threaded_cert_verifier.h"
54 #include "net/cookies/cookie_store.h"
55 #include "net/dns/host_cache.h"
56 #include "net/dns/host_resolver.h"
57 #include "net/dns/mapped_host_resolver.h"
58 #include "net/ftp/ftp_network_layer.h"
59 #include "net/http/http_auth_filter.h"
60 #include "net/http/http_auth_handler_factory.h"
61 #include "net/http/http_network_layer.h"
62 #include "net/http/http_server_properties_impl.h"
63 #include "net/proxy/proxy_config_service.h"
64 #include "net/proxy/proxy_script_fetcher_impl.h"
65 #include "net/proxy/proxy_service.h"
66 #include "net/quic/crypto/crypto_protocol.h"
67 #include "net/quic/quic_protocol.h"
68 #include "net/socket/tcp_client_socket.h"
69 #include "net/spdy/spdy_session.h"
70 #include "net/ssl/channel_id_service.h"
71 #include "net/ssl/default_channel_id_store.h"
72 #include "net/url_request/data_protocol_handler.h"
73 #include "net/url_request/file_protocol_handler.h"
74 #include "net/url_request/ftp_protocol_handler.h"
75 #include "net/url_request/static_http_user_agent_settings.h"
76 #include "net/url_request/url_fetcher.h"
77 #include "net/url_request/url_request_context.h"
78 #include "net/url_request/url_request_context_getter.h"
79 #include "net/url_request/url_request_job_factory_impl.h"
80 #include "net/url_request/url_request_throttler_manager.h"
81 #include "url/url_constants.h"
83 #if defined(ENABLE_CONFIGURATION_POLICY)
84 #include "policy/policy_constants.h"
85 #endif
87 #if defined(ENABLE_EXTENSIONS)
88 #include "chrome/browser/extensions/event_router_forwarder.h"
89 #endif
91 #if !defined(USE_OPENSSL)
92 #include "net/cert/ct_log_verifier.h"
93 #include "net/cert/multi_log_ct_verifier.h"
94 #endif
96 #if defined(USE_NSS) || defined(OS_IOS)
97 #include "net/ocsp/nss_ocsp.h"
98 #endif
100 #if defined(SPDY_PROXY_AUTH_ORIGIN)
101 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h"
102 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_factory.h"
103 #include "components/data_reduction_proxy/browser/data_reduction_proxy_auth_request_handler.h"
104 #include "components/data_reduction_proxy/browser/data_reduction_proxy_protocol.h"
105 #endif // defined(SPDY_PROXY_AUTH_ORIGIN)
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 kQuicFieldTrialName[] = "QUIC";
122 const char kQuicFieldTrialEnabledGroupName[] = "Enabled";
123 const char kQuicFieldTrialHttpsEnabledGroupName[] = "HttpsEnabled";
124 const char kQuicFieldTrialPacketLengthSuffix[] = "BytePackets";
125 const char kQuicFieldTrialPacingSuffix[] = "WithPacing";
126 const char kQuicFieldTrialTimeBasedLossDetectionSuffix[] =
127 "WithTimeBasedLossDetection";
129 // The SPDY trial composes two different trial plus control groups:
130 // * A "holdback" group with SPDY disabled, and corresponding control
131 // (SPDY/3.1). The primary purpose of the holdback group is to encourage site
132 // operators to do feature detection rather than UA-sniffing. As such, this
133 // trial runs continuously.
134 // * A SPDY/4 experiment, for SPDY/4 (aka HTTP/2) vs SPDY/3.1 comparisons and
135 // eventual SPDY/4 deployment.
136 const char kSpdyFieldTrialName[] = "SPDY";
137 const char kSpdyFieldTrialHoldbackGroupName[] = "SpdyDisabled";
138 const char kSpdyFieldTrialHoldbackControlGroupName[] = "Control";
139 const char kSpdyFieldTrialSpdy4GroupName[] = "Spdy4Enabled";
140 const char kSpdyFieldTrialSpdy4ControlGroupName[] = "Spdy4Control";
142 #if defined(OS_MACOSX) && !defined(OS_IOS)
143 void ObserveKeychainEvents() {
144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
145 net::CertDatabase::GetInstance()->SetMessageLoopForKeychainEvents();
147 #endif
149 // Used for the "system" URLRequestContext.
150 class SystemURLRequestContext : public net::URLRequestContext {
151 public:
152 SystemURLRequestContext() {
153 #if defined(USE_NSS) || defined(OS_IOS)
154 net::SetURLRequestContextForNSSHttpIO(this);
155 #endif
158 private:
159 virtual ~SystemURLRequestContext() {
160 AssertNoURLRequests();
161 #if defined(USE_NSS) || defined(OS_IOS)
162 net::SetURLRequestContextForNSSHttpIO(NULL);
163 #endif
167 scoped_ptr<net::HostResolver> CreateGlobalHostResolver(net::NetLog* net_log) {
168 TRACE_EVENT0("startup", "IOThread::CreateGlobalHostResolver");
169 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
171 net::HostResolver::Options options;
173 // Use the concurrency override from the command-line, if any.
174 if (command_line.HasSwitch(switches::kHostResolverParallelism)) {
175 std::string s =
176 command_line.GetSwitchValueASCII(switches::kHostResolverParallelism);
178 // Parse the switch (it should be a positive integer formatted as decimal).
179 int n;
180 if (base::StringToInt(s, &n) && n > 0) {
181 options.max_concurrent_resolves = static_cast<size_t>(n);
182 } else {
183 LOG(ERROR) << "Invalid switch for host resolver parallelism: " << s;
187 // Use the retry attempts override from the command-line, if any.
188 if (command_line.HasSwitch(switches::kHostResolverRetryAttempts)) {
189 std::string s =
190 command_line.GetSwitchValueASCII(switches::kHostResolverRetryAttempts);
191 // Parse the switch (it should be a non-negative integer).
192 int n;
193 if (base::StringToInt(s, &n) && n >= 0) {
194 options.max_retry_attempts = static_cast<size_t>(n);
195 } else {
196 LOG(ERROR) << "Invalid switch for host resolver retry attempts: " << s;
200 scoped_ptr<net::HostResolver> global_host_resolver;
201 #if defined OS_CHROMEOS
202 global_host_resolver =
203 chromeos::HostResolverImplChromeOS::CreateSystemResolver(options,
204 net_log);
205 #else
206 global_host_resolver =
207 net::HostResolver::CreateSystemResolver(options, net_log);
208 #endif
210 // Determine if we should disable IPv6 support.
211 if (command_line.HasSwitch(switches::kEnableIPv6)) {
212 // Disable IPv6 probing.
213 global_host_resolver->SetDefaultAddressFamily(
214 net::ADDRESS_FAMILY_UNSPECIFIED);
215 } else if (command_line.HasSwitch(switches::kDisableIPv6)) {
216 global_host_resolver->SetDefaultAddressFamily(net::ADDRESS_FAMILY_IPV4);
219 // If hostname remappings were specified on the command-line, layer these
220 // rules on top of the real host resolver. This allows forwarding all requests
221 // through a designated test server.
222 if (!command_line.HasSwitch(switches::kHostResolverRules))
223 return global_host_resolver.PassAs<net::HostResolver>();
225 scoped_ptr<net::MappedHostResolver> remapped_resolver(
226 new net::MappedHostResolver(global_host_resolver.Pass()));
227 remapped_resolver->SetRulesFromString(
228 command_line.GetSwitchValueASCII(switches::kHostResolverRules));
229 return remapped_resolver.PassAs<net::HostResolver>();
232 // TODO(willchan): Remove proxy script fetcher context since it's not necessary
233 // now that I got rid of refcounting URLRequestContexts.
234 // See IOThread::Globals for details.
235 net::URLRequestContext*
236 ConstructProxyScriptFetcherContext(IOThread::Globals* globals,
237 net::NetLog* net_log) {
238 net::URLRequestContext* context = new net::URLRequestContext;
239 context->set_net_log(net_log);
240 context->set_host_resolver(globals->host_resolver.get());
241 context->set_cert_verifier(globals->cert_verifier.get());
242 context->set_transport_security_state(
243 globals->transport_security_state.get());
244 context->set_cert_transparency_verifier(
245 globals->cert_transparency_verifier.get());
246 context->set_http_auth_handler_factory(
247 globals->http_auth_handler_factory.get());
248 context->set_proxy_service(globals->proxy_script_fetcher_proxy_service.get());
249 context->set_http_transaction_factory(
250 globals->proxy_script_fetcher_http_transaction_factory.get());
251 context->set_job_factory(
252 globals->proxy_script_fetcher_url_request_job_factory.get());
253 context->set_cookie_store(globals->system_cookie_store.get());
254 context->set_channel_id_service(
255 globals->system_channel_id_service.get());
256 context->set_network_delegate(globals->system_network_delegate.get());
257 context->set_http_user_agent_settings(
258 globals->http_user_agent_settings.get());
259 // TODO(rtenneti): We should probably use HttpServerPropertiesManager for the
260 // system URLRequestContext too. There's no reason this should be tied to a
261 // profile.
262 return context;
265 net::URLRequestContext*
266 ConstructSystemRequestContext(IOThread::Globals* globals,
267 net::NetLog* net_log) {
268 net::URLRequestContext* context = new SystemURLRequestContext;
269 context->set_net_log(net_log);
270 context->set_host_resolver(globals->host_resolver.get());
271 context->set_cert_verifier(globals->cert_verifier.get());
272 context->set_transport_security_state(
273 globals->transport_security_state.get());
274 context->set_cert_transparency_verifier(
275 globals->cert_transparency_verifier.get());
276 context->set_http_auth_handler_factory(
277 globals->http_auth_handler_factory.get());
278 context->set_proxy_service(globals->system_proxy_service.get());
279 context->set_http_transaction_factory(
280 globals->system_http_transaction_factory.get());
281 context->set_job_factory(globals->system_url_request_job_factory.get());
282 context->set_cookie_store(globals->system_cookie_store.get());
283 context->set_channel_id_service(
284 globals->system_channel_id_service.get());
285 context->set_throttler_manager(globals->throttler_manager.get());
286 context->set_network_delegate(globals->system_network_delegate.get());
287 context->set_http_user_agent_settings(
288 globals->http_user_agent_settings.get());
289 return context;
292 int GetSwitchValueAsInt(const CommandLine& command_line,
293 const std::string& switch_name) {
294 int value;
295 if (!base::StringToInt(command_line.GetSwitchValueASCII(switch_name),
296 &value)) {
297 return 0;
299 return value;
302 // Returns the value associated with |key| in |params| or "" if the
303 // key is not present in the map.
304 const std::string& GetVariationParam(
305 const std::map<std::string, std::string>& params,
306 const std::string& key) {
307 std::map<std::string, std::string>::const_iterator it = params.find(key);
308 if (it == params.end())
309 return base::EmptyString();
311 return it->second;
314 } // namespace
316 class IOThread::LoggingNetworkChangeObserver
317 : public net::NetworkChangeNotifier::IPAddressObserver,
318 public net::NetworkChangeNotifier::ConnectionTypeObserver,
319 public net::NetworkChangeNotifier::NetworkChangeObserver {
320 public:
321 // |net_log| must remain valid throughout our lifetime.
322 explicit LoggingNetworkChangeObserver(net::NetLog* net_log)
323 : net_log_(net_log) {
324 net::NetworkChangeNotifier::AddIPAddressObserver(this);
325 net::NetworkChangeNotifier::AddConnectionTypeObserver(this);
326 net::NetworkChangeNotifier::AddNetworkChangeObserver(this);
329 virtual ~LoggingNetworkChangeObserver() {
330 net::NetworkChangeNotifier::RemoveIPAddressObserver(this);
331 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
332 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this);
335 // NetworkChangeNotifier::IPAddressObserver implementation.
336 virtual void OnIPAddressChanged() OVERRIDE {
337 VLOG(1) << "Observed a change to the network IP addresses";
339 net_log_->AddGlobalEntry(net::NetLog::TYPE_NETWORK_IP_ADDRESSES_CHANGED);
342 // NetworkChangeNotifier::ConnectionTypeObserver implementation.
343 virtual void OnConnectionTypeChanged(
344 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE {
345 std::string type_as_string =
346 net::NetworkChangeNotifier::ConnectionTypeToString(type);
348 VLOG(1) << "Observed a change to network connectivity state "
349 << type_as_string;
351 net_log_->AddGlobalEntry(
352 net::NetLog::TYPE_NETWORK_CONNECTIVITY_CHANGED,
353 net::NetLog::StringCallback("new_connection_type", &type_as_string));
356 // NetworkChangeNotifier::NetworkChangeObserver implementation.
357 virtual void OnNetworkChanged(
358 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE {
359 std::string type_as_string =
360 net::NetworkChangeNotifier::ConnectionTypeToString(type);
362 VLOG(1) << "Observed a network change to state " << type_as_string;
364 net_log_->AddGlobalEntry(
365 net::NetLog::TYPE_NETWORK_CHANGED,
366 net::NetLog::StringCallback("new_connection_type", &type_as_string));
369 private:
370 net::NetLog* net_log_;
371 DISALLOW_COPY_AND_ASSIGN(LoggingNetworkChangeObserver);
374 class SystemURLRequestContextGetter : public net::URLRequestContextGetter {
375 public:
376 explicit SystemURLRequestContextGetter(IOThread* io_thread);
378 // Implementation for net::UrlRequestContextGetter.
379 virtual net::URLRequestContext* GetURLRequestContext() OVERRIDE;
380 virtual scoped_refptr<base::SingleThreadTaskRunner>
381 GetNetworkTaskRunner() const OVERRIDE;
383 protected:
384 virtual ~SystemURLRequestContextGetter();
386 private:
387 IOThread* const io_thread_; // Weak pointer, owned by BrowserProcess.
388 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
390 base::debug::LeakTracker<SystemURLRequestContextGetter> leak_tracker_;
393 SystemURLRequestContextGetter::SystemURLRequestContextGetter(
394 IOThread* io_thread)
395 : io_thread_(io_thread),
396 network_task_runner_(
397 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)) {
400 SystemURLRequestContextGetter::~SystemURLRequestContextGetter() {}
402 net::URLRequestContext* SystemURLRequestContextGetter::GetURLRequestContext() {
403 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
404 DCHECK(io_thread_->globals()->system_request_context.get());
406 return io_thread_->globals()->system_request_context.get();
409 scoped_refptr<base::SingleThreadTaskRunner>
410 SystemURLRequestContextGetter::GetNetworkTaskRunner() const {
411 return network_task_runner_;
414 IOThread::Globals::
415 SystemRequestContextLeakChecker::SystemRequestContextLeakChecker(
416 Globals* globals)
417 : globals_(globals) {
418 DCHECK(globals_);
421 IOThread::Globals::
422 SystemRequestContextLeakChecker::~SystemRequestContextLeakChecker() {
423 if (globals_->system_request_context.get())
424 globals_->system_request_context->AssertNoURLRequests();
427 IOThread::Globals::Globals()
428 : system_request_context_leak_checker(this),
429 enable_ssl_connect_job_waiting(false),
430 ignore_certificate_errors(false),
431 testing_fixed_http_port(0),
432 testing_fixed_https_port(0),
433 enable_user_alternate_protocol_ports(false) {
436 IOThread::Globals::~Globals() {}
438 // |local_state| is passed in explicitly in order to (1) reduce implicit
439 // dependencies and (2) make IOThread more flexible for testing.
440 IOThread::IOThread(
441 PrefService* local_state,
442 policy::PolicyService* policy_service,
443 ChromeNetLog* net_log,
444 extensions::EventRouterForwarder* extension_event_router_forwarder)
445 : net_log_(net_log),
446 #if defined(ENABLE_EXTENSIONS)
447 extension_event_router_forwarder_(extension_event_router_forwarder),
448 #endif
449 globals_(NULL),
450 is_spdy_disabled_by_policy_(false),
451 weak_factory_(this),
452 creation_time_(base::TimeTicks::Now()) {
453 auth_schemes_ = local_state->GetString(prefs::kAuthSchemes);
454 negotiate_disable_cname_lookup_ = local_state->GetBoolean(
455 prefs::kDisableAuthNegotiateCnameLookup);
456 negotiate_enable_port_ = local_state->GetBoolean(
457 prefs::kEnableAuthNegotiatePort);
458 auth_server_whitelist_ = local_state->GetString(prefs::kAuthServerWhitelist);
459 auth_delegate_whitelist_ = local_state->GetString(
460 prefs::kAuthNegotiateDelegateWhitelist);
461 gssapi_library_name_ = local_state->GetString(prefs::kGSSAPILibraryName);
462 pref_proxy_config_tracker_.reset(
463 ProxyServiceFactory::CreatePrefProxyConfigTrackerOfLocalState(
464 local_state));
465 ChromeNetworkDelegate::InitializePrefsOnUIThread(
466 &system_enable_referrers_,
467 NULL,
468 NULL,
469 local_state);
470 ssl_config_service_manager_.reset(
471 SSLConfigServiceManager::CreateDefaultManager(local_state));
473 base::Value* dns_client_enabled_default = new base::FundamentalValue(
474 chrome_browser_net::ConfigureAsyncDnsFieldTrial());
475 local_state->SetDefaultPrefValue(prefs::kBuiltInDnsClientEnabled,
476 dns_client_enabled_default);
478 dns_client_enabled_.Init(prefs::kBuiltInDnsClientEnabled,
479 local_state,
480 base::Bind(&IOThread::UpdateDnsClientEnabled,
481 base::Unretained(this)));
482 dns_client_enabled_.MoveToThread(
483 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
485 quick_check_enabled_.Init(prefs::kQuickCheckEnabled,
486 local_state);
487 quick_check_enabled_.MoveToThread(
488 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
490 #if defined(ENABLE_CONFIGURATION_POLICY)
491 is_spdy_disabled_by_policy_ = policy_service->GetPolicies(
492 policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME, std::string())).Get(
493 policy::key::kDisableSpdy) != NULL;
494 #endif // ENABLE_CONFIGURATION_POLICY
496 BrowserThread::SetDelegate(BrowserThread::IO, this);
499 IOThread::~IOThread() {
500 // This isn't needed for production code, but in tests, IOThread may
501 // be multiply constructed.
502 BrowserThread::SetDelegate(BrowserThread::IO, NULL);
504 pref_proxy_config_tracker_->DetachFromPrefService();
505 DCHECK(!globals_);
508 IOThread::Globals* IOThread::globals() {
509 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
510 return globals_;
513 void IOThread::SetGlobalsForTesting(Globals* globals) {
514 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
515 DCHECK(!globals || !globals_);
516 globals_ = globals;
519 ChromeNetLog* IOThread::net_log() {
520 return net_log_;
523 void IOThread::ChangedToOnTheRecord() {
524 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
525 BrowserThread::PostTask(
526 BrowserThread::IO,
527 FROM_HERE,
528 base::Bind(&IOThread::ChangedToOnTheRecordOnIOThread,
529 base::Unretained(this)));
532 net::URLRequestContextGetter* IOThread::system_url_request_context_getter() {
533 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
534 if (!system_url_request_context_getter_.get()) {
535 InitSystemRequestContext();
537 return system_url_request_context_getter_.get();
540 void IOThread::Init() {
541 // Prefer to use InitAsync unless you need initialization to block
542 // the UI thread
545 void IOThread::InitAsync() {
546 TRACE_EVENT0("startup", "IOThread::InitAsync");
547 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
549 #if defined(USE_NSS) || defined(OS_IOS)
550 net::SetMessageLoopForNSSHttpIO();
551 #endif
553 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
555 DCHECK(!globals_);
556 globals_ = new Globals;
558 // Add an observer that will emit network change events to the ChromeNetLog.
559 // Assuming NetworkChangeNotifier dispatches in FIFO order, we should be
560 // logging the network change before other IO thread consumers respond to it.
561 network_change_observer_.reset(
562 new LoggingNetworkChangeObserver(net_log_));
564 // Setup the HistogramWatcher to run on the IO thread.
565 net::NetworkChangeNotifier::InitHistogramWatcher();
567 #if defined(ENABLE_EXTENSIONS)
568 globals_->extension_event_router_forwarder =
569 extension_event_router_forwarder_;
570 #endif
572 ChromeNetworkDelegate* network_delegate =
573 new ChromeNetworkDelegate(extension_event_router_forwarder(),
574 &system_enable_referrers_);
576 if (command_line.HasSwitch(switches::kEnableClientHints))
577 network_delegate->SetEnableClientHints();
579 #if defined(ENABLE_EXTENSIONS)
580 if (command_line.HasSwitch(switches::kDisableExtensionsHttpThrottling))
581 network_delegate->NeverThrottleRequests();
582 #endif
584 globals_->system_network_delegate.reset(network_delegate);
585 globals_->host_resolver = CreateGlobalHostResolver(net_log_);
586 UpdateDnsClientEnabled();
587 #if defined(OS_CHROMEOS)
588 // Creates a CertVerifyProc that doesn't allow any profile-provided certs.
589 globals_->cert_verifier.reset(new net::MultiThreadedCertVerifier(
590 new chromeos::CertVerifyProcChromeOS()));
591 #else
592 globals_->cert_verifier.reset(new net::MultiThreadedCertVerifier(
593 net::CertVerifyProc::CreateDefault()));
594 #endif
596 globals_->transport_security_state.reset(new net::TransportSecurityState());
597 #if !defined(USE_OPENSSL)
598 // For now, Certificate Transparency is only implemented for platforms
599 // that use NSS.
600 net::MultiLogCTVerifier* ct_verifier = new net::MultiLogCTVerifier();
601 globals_->cert_transparency_verifier.reset(ct_verifier);
603 // Add built-in logs
604 ct_verifier->AddLogs(net::ct::CreateLogVerifiersForKnownLogs());
606 // Add logs from command line
607 if (command_line.HasSwitch(switches::kCertificateTransparencyLog)) {
608 std::string switch_value = command_line.GetSwitchValueASCII(
609 switches::kCertificateTransparencyLog);
610 std::vector<std::string> logs;
611 base::SplitString(switch_value, ',', &logs);
612 for (std::vector<std::string>::iterator it = logs.begin(); it != logs.end();
613 ++it) {
614 const std::string& curr_log = *it;
615 size_t delim_pos = curr_log.find(":");
616 CHECK(delim_pos != std::string::npos)
617 << "CT log description not provided (switch format"
618 " is 'description:base64_key')";
619 std::string log_description(curr_log.substr(0, delim_pos));
620 std::string ct_public_key_data;
621 CHECK(base::Base64Decode(curr_log.substr(delim_pos + 1),
622 &ct_public_key_data))
623 << "Unable to decode CT public key.";
624 scoped_ptr<net::CTLogVerifier> external_log_verifier(
625 net::CTLogVerifier::Create(ct_public_key_data, log_description));
626 CHECK(external_log_verifier) << "Unable to parse CT public key.";
627 VLOG(1) << "Adding log with description " << log_description;
628 ct_verifier->AddLog(external_log_verifier.Pass());
631 #else
632 if (command_line.HasSwitch(switches::kCertificateTransparencyLog)) {
633 LOG(DFATAL) << "Certificate Transparency is not yet supported in Chrome "
634 "builds using OpenSSL.";
636 #endif
637 globals_->ssl_config_service = GetSSLConfigService();
639 #if defined(SPDY_PROXY_AUTH_ORIGIN)
640 int drp_flags = 0;
641 if (data_reduction_proxy::DataReductionProxyParams::
642 IsIncludedInFieldTrial()) {
643 drp_flags |=
644 (data_reduction_proxy::DataReductionProxyParams::kAllowed |
645 data_reduction_proxy::DataReductionProxyParams::kFallbackAllowed);
647 if (data_reduction_proxy::DataReductionProxyParams::
648 IsIncludedInAlternativeFieldTrial()) {
649 drp_flags |=
650 data_reduction_proxy::DataReductionProxyParams::kAlternativeAllowed;
652 if (data_reduction_proxy::DataReductionProxyParams::
653 IsIncludedInPromoFieldTrial())
654 drp_flags |= data_reduction_proxy::DataReductionProxyParams::kPromoAllowed;
655 if (data_reduction_proxy::DataReductionProxyParams::
656 IsIncludedInHoldbackFieldTrial())
657 drp_flags |= data_reduction_proxy::DataReductionProxyParams::kHoldback;
658 globals_->data_reduction_proxy_params.reset(
659 new data_reduction_proxy::DataReductionProxyParams(drp_flags));
660 globals_->data_reduction_proxy_auth_request_handler.reset(
661 new data_reduction_proxy::DataReductionProxyAuthRequestHandler(
662 DataReductionProxyChromeSettings::GetClient(),
663 DataReductionProxyChromeSettings::GetBuildAndPatchNumber(),
664 globals_->data_reduction_proxy_params.get(),
665 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)));
666 // This is the same as in ProfileImplIOData except that we do not collect
667 // usage stats.
668 network_delegate->set_data_reduction_proxy_params(
669 globals_->data_reduction_proxy_params.get());
670 network_delegate->set_data_reduction_proxy_auth_request_handler(
671 globals_->data_reduction_proxy_auth_request_handler.get());
672 network_delegate->set_on_resolve_proxy_handler(
673 base::Bind(data_reduction_proxy::OnResolveProxyHandler));
674 #endif // defined(SPDY_PROXY_AUTH_ORIGIN)
676 globals_->http_auth_handler_factory.reset(CreateDefaultAuthHandlerFactory(
677 globals_->host_resolver.get()));
678 globals_->http_server_properties.reset(new net::HttpServerPropertiesImpl());
679 // For the ProxyScriptFetcher, we use a direct ProxyService.
680 globals_->proxy_script_fetcher_proxy_service.reset(
681 net::ProxyService::CreateDirectWithNetLog(net_log_));
682 // In-memory cookie store.
683 globals_->system_cookie_store =
684 content::CreateCookieStore(content::CookieStoreConfig());
685 // In-memory channel ID store.
686 globals_->system_channel_id_service.reset(
687 new net::ChannelIDService(
688 new net::DefaultChannelIDStore(NULL),
689 base::WorkerPool::GetTaskRunner(true)));
690 globals_->dns_probe_service.reset(new chrome_browser_net::DnsProbeService());
691 globals_->host_mapping_rules.reset(new net::HostMappingRules());
692 globals_->http_user_agent_settings.reset(
693 new net::StaticHttpUserAgentSettings(std::string(), GetUserAgent()));
694 if (command_line.HasSwitch(switches::kHostRules)) {
695 TRACE_EVENT_BEGIN0("startup", "IOThread::InitAsync:SetRulesFromString");
696 globals_->host_mapping_rules->SetRulesFromString(
697 command_line.GetSwitchValueASCII(switches::kHostRules));
698 TRACE_EVENT_END0("startup", "IOThread::InitAsync:SetRulesFromString");
700 if (command_line.HasSwitch(switches::kEnableSSLConnectJobWaiting))
701 globals_->enable_ssl_connect_job_waiting = true;
702 if (command_line.HasSwitch(switches::kIgnoreCertificateErrors))
703 globals_->ignore_certificate_errors = true;
704 if (command_line.HasSwitch(switches::kTestingFixedHttpPort)) {
705 globals_->testing_fixed_http_port =
706 GetSwitchValueAsInt(command_line, switches::kTestingFixedHttpPort);
708 if (command_line.HasSwitch(switches::kTestingFixedHttpsPort)) {
709 globals_->testing_fixed_https_port =
710 GetSwitchValueAsInt(command_line, switches::kTestingFixedHttpsPort);
712 ConfigureQuic(command_line);
713 if (command_line.HasSwitch(
714 switches::kEnableUserAlternateProtocolPorts)) {
715 globals_->enable_user_alternate_protocol_ports = true;
717 InitializeNetworkOptions(command_line);
719 net::HttpNetworkSession::Params session_params;
720 InitializeNetworkSessionParams(&session_params);
721 session_params.net_log = net_log_;
722 session_params.proxy_service =
723 globals_->proxy_script_fetcher_proxy_service.get();
725 TRACE_EVENT_BEGIN0("startup", "IOThread::InitAsync:HttpNetworkSession");
726 scoped_refptr<net::HttpNetworkSession> network_session(
727 new net::HttpNetworkSession(session_params));
728 globals_->proxy_script_fetcher_http_transaction_factory
729 .reset(new net::HttpNetworkLayer(network_session.get()));
730 TRACE_EVENT_END0("startup", "IOThread::InitAsync:HttpNetworkSession");
731 scoped_ptr<net::URLRequestJobFactoryImpl> job_factory(
732 new net::URLRequestJobFactoryImpl());
733 job_factory->SetProtocolHandler(url::kDataScheme,
734 new net::DataProtocolHandler());
735 job_factory->SetProtocolHandler(
736 url::kFileScheme,
737 new net::FileProtocolHandler(
738 content::BrowserThread::GetBlockingPool()->
739 GetTaskRunnerWithShutdownBehavior(
740 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)));
741 #if !defined(DISABLE_FTP_SUPPORT)
742 globals_->proxy_script_fetcher_ftp_transaction_factory.reset(
743 new net::FtpNetworkLayer(globals_->host_resolver.get()));
744 job_factory->SetProtocolHandler(
745 url::kFtpScheme,
746 new net::FtpProtocolHandler(
747 globals_->proxy_script_fetcher_ftp_transaction_factory.get()));
748 #endif
749 globals_->proxy_script_fetcher_url_request_job_factory =
750 job_factory.PassAs<net::URLRequestJobFactory>();
752 globals_->throttler_manager.reset(new net::URLRequestThrottlerManager());
753 globals_->throttler_manager->set_net_log(net_log_);
754 // Always done in production, disabled only for unit tests.
755 globals_->throttler_manager->set_enable_thread_checks(true);
757 globals_->proxy_script_fetcher_context.reset(
758 ConstructProxyScriptFetcherContext(globals_, net_log_));
760 #if defined(OS_MACOSX) && !defined(OS_IOS)
761 // Start observing Keychain events. This needs to be done on the UI thread,
762 // as Keychain services requires a CFRunLoop.
763 BrowserThread::PostTask(BrowserThread::UI,
764 FROM_HERE,
765 base::Bind(&ObserveKeychainEvents));
766 #endif
768 // InitSystemRequestContext turns right around and posts a task back
769 // to the IO thread, so we can't let it run until we know the IO
770 // thread has started.
772 // Note that since we are at BrowserThread::Init time, the UI thread
773 // is blocked waiting for the thread to start. Therefore, posting
774 // this task to the main thread's message loop here is guaranteed to
775 // get it onto the message loop while the IOThread object still
776 // exists. However, the message might not be processed on the UI
777 // thread until after IOThread is gone, so use a weak pointer.
778 BrowserThread::PostTask(BrowserThread::UI,
779 FROM_HERE,
780 base::Bind(&IOThread::InitSystemRequestContext,
781 weak_factory_.GetWeakPtr()));
784 void IOThread::CleanUp() {
785 base::debug::LeakTracker<SafeBrowsingURLRequestContext>::CheckForLeaks();
787 #if defined(USE_NSS) || defined(OS_IOS)
788 net::ShutdownNSSHttpIO();
789 #endif
791 system_url_request_context_getter_ = NULL;
793 // Release objects that the net::URLRequestContext could have been pointing
794 // to.
796 // Shutdown the HistogramWatcher on the IO thread.
797 net::NetworkChangeNotifier::ShutdownHistogramWatcher();
799 // This must be reset before the ChromeNetLog is destroyed.
800 network_change_observer_.reset();
802 system_proxy_config_service_.reset();
804 delete globals_;
805 globals_ = NULL;
807 base::debug::LeakTracker<SystemURLRequestContextGetter>::CheckForLeaks();
810 void IOThread::InitializeNetworkOptions(const CommandLine& command_line) {
811 // Only handle use-spdy command line flags if "spdy.disabled" preference is
812 // not disabled via policy.
813 if (is_spdy_disabled_by_policy_) {
814 base::FieldTrial* trial = base::FieldTrialList::Find(kSpdyFieldTrialName);
815 if (trial)
816 trial->Disable();
817 } else {
818 if (command_line.HasSwitch(switches::kTrustedSpdyProxy)) {
819 globals_->trusted_spdy_proxy.set(
820 command_line.GetSwitchValueASCII(switches::kTrustedSpdyProxy));
822 if (command_line.HasSwitch(switches::kIgnoreUrlFetcherCertRequests))
823 net::URLFetcher::SetIgnoreCertificateRequests(true);
825 if (command_line.HasSwitch(switches::kUseSpdy)) {
826 std::string spdy_mode =
827 command_line.GetSwitchValueASCII(switches::kUseSpdy);
828 EnableSpdy(spdy_mode);
829 } else if (command_line.HasSwitch(switches::kEnableSpdy4)) {
830 globals_->next_protos = net::NextProtosSpdy4Http2();
831 globals_->use_alternate_protocols.set(true);
832 } else if (command_line.HasSwitch(switches::kDisableSpdy31)) {
833 globals_->next_protos = net::NextProtosSpdy3();
834 globals_->use_alternate_protocols.set(true);
835 } else if (command_line.HasSwitch(switches::kEnableNpnHttpOnly)) {
836 globals_->next_protos = net::NextProtosHttpOnly();
837 globals_->use_alternate_protocols.set(false);
838 } else if (command_line.HasSwitch(switches::kEnableWebSocketOverSpdy)) {
839 // Use the current SPDY default (SPDY/3.1).
840 globals_->next_protos = net::NextProtosSpdy31();
841 globals_->use_alternate_protocols.set(true);
842 } else {
843 // No SPDY command-line flags have been specified. Examine trial groups.
844 ConfigureSpdyFromTrial(
845 base::FieldTrialList::FindFullName(kSpdyFieldTrialName), globals_);
848 if (command_line.HasSwitch(switches::kEnableWebSocketOverSpdy))
849 globals_->enable_websocket_over_spdy.set(true);
852 // TODO(rch): Make the client socket factory a per-network session
853 // instance, constructed from a NetworkSession::Params, to allow us
854 // to move this option to IOThread::Globals &
855 // HttpNetworkSession::Params.
856 if (command_line.HasSwitch(switches::kEnableTcpFastOpen))
857 net::SetTCPFastOpenEnabled(true);
860 void IOThread::ConfigureSpdyFromTrial(const std::string& spdy_trial_group,
861 Globals* globals) {
862 if (spdy_trial_group == kSpdyFieldTrialHoldbackGroupName) {
863 // TODO(jgraettinger): Use net::NextProtosHttpOnly() instead?
864 net::HttpStreamFactory::set_spdy_enabled(false);
865 } else if (spdy_trial_group == kSpdyFieldTrialHoldbackControlGroupName) {
866 // Use the current SPDY default (SPDY/3.1).
867 globals->next_protos = net::NextProtosSpdy31();
868 globals->use_alternate_protocols.set(true);
869 } else if (spdy_trial_group == kSpdyFieldTrialSpdy4GroupName) {
870 globals->next_protos = net::NextProtosSpdy4Http2();
871 globals->use_alternate_protocols.set(true);
872 } else if (spdy_trial_group == kSpdyFieldTrialSpdy4ControlGroupName) {
873 // This control group is pinned at SPDY/3.1.
874 globals->next_protos = net::NextProtosSpdy31();
875 globals->use_alternate_protocols.set(true);
876 } else {
877 // Use the current SPDY default (SPDY/3.1).
878 globals->next_protos = net::NextProtosSpdy31();
879 globals->use_alternate_protocols.set(true);
883 void IOThread::EnableSpdy(const std::string& mode) {
884 static const char kOff[] = "off";
885 static const char kSSL[] = "ssl";
886 static const char kDisableSSL[] = "no-ssl";
887 static const char kDisablePing[] = "no-ping";
888 static const char kExclude[] = "exclude"; // Hosts to exclude
889 static const char kDisableCompression[] = "no-compress";
890 static const char kDisableAltProtocols[] = "no-alt-protocols";
891 static const char kForceAltProtocols[] = "force-alt-protocols";
892 static const char kSingleDomain[] = "single-domain";
894 static const char kInitialMaxConcurrentStreams[] = "init-max-streams";
896 std::vector<std::string> spdy_options;
897 base::SplitString(mode, ',', &spdy_options);
899 for (std::vector<std::string>::iterator it = spdy_options.begin();
900 it != spdy_options.end(); ++it) {
901 const std::string& element = *it;
902 std::vector<std::string> name_value;
903 base::SplitString(element, '=', &name_value);
904 const std::string& option =
905 name_value.size() > 0 ? name_value[0] : std::string();
906 const std::string value =
907 name_value.size() > 1 ? name_value[1] : std::string();
909 if (option == kOff) {
910 net::HttpStreamFactory::set_spdy_enabled(false);
911 } else if (option == kDisableSSL) {
912 globals_->spdy_default_protocol.set(net::kProtoSPDY3);
913 globals_->force_spdy_over_ssl.set(false);
914 globals_->force_spdy_always.set(true);
915 } else if (option == kSSL) {
916 globals_->spdy_default_protocol.set(net::kProtoSPDY3);
917 globals_->force_spdy_over_ssl.set(true);
918 globals_->force_spdy_always.set(true);
919 } else if (option == kDisablePing) {
920 globals_->enable_spdy_ping_based_connection_checking.set(false);
921 } else if (option == kExclude) {
922 globals_->forced_spdy_exclusions.insert(
923 net::HostPortPair::FromURL(GURL(value)));
924 } else if (option == kDisableCompression) {
925 globals_->enable_spdy_compression.set(false);
926 } else if (option == kDisableAltProtocols) {
927 globals_->use_alternate_protocols.set(false);
928 } else if (option == kForceAltProtocols) {
929 net::AlternateProtocolInfo pair(443, net::NPN_SPDY_3, 1);
930 net::HttpServerPropertiesImpl::ForceAlternateProtocol(pair);
931 } else if (option == kSingleDomain) {
932 DVLOG(1) << "FORCING SINGLE DOMAIN";
933 globals_->force_spdy_single_domain.set(true);
934 } else if (option == kInitialMaxConcurrentStreams) {
935 int streams;
936 if (base::StringToInt(value, &streams))
937 globals_->initial_max_spdy_concurrent_streams.set(streams);
938 } else if (option.empty() && it == spdy_options.begin()) {
939 continue;
940 } else {
941 LOG(DFATAL) << "Unrecognized spdy option: " << option;
946 // static
947 void IOThread::RegisterPrefs(PrefRegistrySimple* registry) {
948 registry->RegisterStringPref(prefs::kAuthSchemes,
949 "basic,digest,ntlm,negotiate,"
950 "spdyproxy");
951 registry->RegisterBooleanPref(prefs::kDisableAuthNegotiateCnameLookup, false);
952 registry->RegisterBooleanPref(prefs::kEnableAuthNegotiatePort, false);
953 registry->RegisterStringPref(prefs::kAuthServerWhitelist, std::string());
954 registry->RegisterStringPref(prefs::kAuthNegotiateDelegateWhitelist,
955 std::string());
956 registry->RegisterStringPref(prefs::kGSSAPILibraryName, std::string());
957 registry->RegisterStringPref(
958 data_reduction_proxy::prefs::kDataReductionProxy, std::string());
959 registry->RegisterBooleanPref(prefs::kEnableReferrers, true);
960 data_reduction_proxy::RegisterPrefs(registry);
961 registry->RegisterBooleanPref(prefs::kBuiltInDnsClientEnabled, true);
962 registry->RegisterBooleanPref(prefs::kQuickCheckEnabled, true);
965 net::HttpAuthHandlerFactory* IOThread::CreateDefaultAuthHandlerFactory(
966 net::HostResolver* resolver) {
967 net::HttpAuthFilterWhitelist* auth_filter_default_credentials = NULL;
968 if (!auth_server_whitelist_.empty()) {
969 auth_filter_default_credentials =
970 new net::HttpAuthFilterWhitelist(auth_server_whitelist_);
972 net::HttpAuthFilterWhitelist* auth_filter_delegate = NULL;
973 if (!auth_delegate_whitelist_.empty()) {
974 auth_filter_delegate =
975 new net::HttpAuthFilterWhitelist(auth_delegate_whitelist_);
977 globals_->url_security_manager.reset(
978 net::URLSecurityManager::Create(auth_filter_default_credentials,
979 auth_filter_delegate));
980 std::vector<std::string> supported_schemes;
981 base::SplitString(auth_schemes_, ',', &supported_schemes);
983 scoped_ptr<net::HttpAuthHandlerRegistryFactory> registry_factory(
984 net::HttpAuthHandlerRegistryFactory::Create(
985 supported_schemes, globals_->url_security_manager.get(),
986 resolver, gssapi_library_name_, negotiate_disable_cname_lookup_,
987 negotiate_enable_port_));
988 return registry_factory.release();
991 void IOThread::ClearHostCache() {
992 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
994 net::HostCache* host_cache = globals_->host_resolver->GetHostCache();
995 if (host_cache)
996 host_cache->clear();
999 void IOThread::InitializeNetworkSessionParams(
1000 net::HttpNetworkSession::Params* params) {
1001 InitializeNetworkSessionParamsFromGlobals(*globals_, params);
1004 // static
1005 void IOThread::InitializeNetworkSessionParamsFromGlobals(
1006 const IOThread::Globals& globals,
1007 net::HttpNetworkSession::Params* params) {
1008 params->host_resolver = globals.host_resolver.get();
1009 params->cert_verifier = globals.cert_verifier.get();
1010 params->channel_id_service = globals.system_channel_id_service.get();
1011 params->transport_security_state = globals.transport_security_state.get();
1012 params->ssl_config_service = globals.ssl_config_service.get();
1013 params->http_auth_handler_factory = globals.http_auth_handler_factory.get();
1014 params->http_server_properties =
1015 globals.http_server_properties->GetWeakPtr();
1016 params->network_delegate = globals.system_network_delegate.get();
1017 params->host_mapping_rules = globals.host_mapping_rules.get();
1018 params->enable_ssl_connect_job_waiting =
1019 globals.enable_ssl_connect_job_waiting;
1020 params->ignore_certificate_errors = globals.ignore_certificate_errors;
1021 params->testing_fixed_http_port = globals.testing_fixed_http_port;
1022 params->testing_fixed_https_port = globals.testing_fixed_https_port;
1024 globals.initial_max_spdy_concurrent_streams.CopyToIfSet(
1025 &params->spdy_initial_max_concurrent_streams);
1026 globals.force_spdy_single_domain.CopyToIfSet(
1027 &params->force_spdy_single_domain);
1028 globals.enable_spdy_compression.CopyToIfSet(
1029 &params->enable_spdy_compression);
1030 globals.enable_spdy_ping_based_connection_checking.CopyToIfSet(
1031 &params->enable_spdy_ping_based_connection_checking);
1032 globals.spdy_default_protocol.CopyToIfSet(
1033 &params->spdy_default_protocol);
1034 params->next_protos = globals.next_protos;
1035 globals.trusted_spdy_proxy.CopyToIfSet(&params->trusted_spdy_proxy);
1036 globals.force_spdy_over_ssl.CopyToIfSet(&params->force_spdy_over_ssl);
1037 globals.force_spdy_always.CopyToIfSet(&params->force_spdy_always);
1038 params->forced_spdy_exclusions = globals.forced_spdy_exclusions;
1039 globals.use_alternate_protocols.CopyToIfSet(
1040 &params->use_alternate_protocols);
1041 globals.alternate_protocol_probability_threshold.CopyToIfSet(
1042 &params->alternate_protocol_probability_threshold);
1043 globals.enable_websocket_over_spdy.CopyToIfSet(
1044 &params->enable_websocket_over_spdy);
1046 globals.enable_quic.CopyToIfSet(&params->enable_quic);
1047 globals.enable_quic_time_based_loss_detection.CopyToIfSet(
1048 &params->enable_quic_time_based_loss_detection);
1049 globals.enable_quic_port_selection.CopyToIfSet(
1050 &params->enable_quic_port_selection);
1051 globals.quic_max_packet_length.CopyToIfSet(&params->quic_max_packet_length);
1052 globals.quic_user_agent_id.CopyToIfSet(&params->quic_user_agent_id);
1053 globals.quic_supported_versions.CopyToIfSet(
1054 &params->quic_supported_versions);
1055 params->quic_connection_options = globals.quic_connection_options;
1057 globals.origin_to_force_quic_on.CopyToIfSet(
1058 &params->origin_to_force_quic_on);
1059 params->enable_user_alternate_protocol_ports =
1060 globals.enable_user_alternate_protocol_ports;
1063 base::TimeTicks IOThread::creation_time() const {
1064 return creation_time_;
1067 net::SSLConfigService* IOThread::GetSSLConfigService() {
1068 return ssl_config_service_manager_->Get();
1071 void IOThread::ChangedToOnTheRecordOnIOThread() {
1072 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
1074 // Clear the host cache to avoid showing entries from the OTR session
1075 // in about:net-internals.
1076 ClearHostCache();
1079 void IOThread::InitSystemRequestContext() {
1080 if (system_url_request_context_getter_.get())
1081 return;
1082 // If we're in unit_tests, IOThread may not be run.
1083 if (!BrowserThread::IsMessageLoopValid(BrowserThread::IO))
1084 return;
1085 system_proxy_config_service_.reset(
1086 ProxyServiceFactory::CreateProxyConfigService(
1087 pref_proxy_config_tracker_.get()));
1088 system_url_request_context_getter_ =
1089 new SystemURLRequestContextGetter(this);
1090 // Safe to post an unretained this pointer, since IOThread is
1091 // guaranteed to outlive the IO BrowserThread.
1092 BrowserThread::PostTask(
1093 BrowserThread::IO,
1094 FROM_HERE,
1095 base::Bind(&IOThread::InitSystemRequestContextOnIOThread,
1096 base::Unretained(this)));
1099 void IOThread::InitSystemRequestContextOnIOThread() {
1100 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
1101 DCHECK(!globals_->system_proxy_service.get());
1102 DCHECK(system_proxy_config_service_.get());
1104 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
1105 globals_->system_proxy_service.reset(
1106 ProxyServiceFactory::CreateProxyService(
1107 net_log_,
1108 globals_->proxy_script_fetcher_context.get(),
1109 globals_->system_network_delegate.get(),
1110 system_proxy_config_service_.release(),
1111 command_line,
1112 quick_check_enabled_.GetValue()));
1114 net::HttpNetworkSession::Params system_params;
1115 InitializeNetworkSessionParams(&system_params);
1116 system_params.net_log = net_log_;
1117 system_params.proxy_service = globals_->system_proxy_service.get();
1119 globals_->system_http_transaction_factory.reset(
1120 new net::HttpNetworkLayer(
1121 new net::HttpNetworkSession(system_params)));
1122 globals_->system_url_request_job_factory.reset(
1123 new net::URLRequestJobFactoryImpl());
1124 globals_->system_request_context.reset(
1125 ConstructSystemRequestContext(globals_, net_log_));
1126 globals_->system_request_context->set_ssl_config_service(
1127 globals_->ssl_config_service);
1128 globals_->system_request_context->set_http_server_properties(
1129 globals_->http_server_properties->GetWeakPtr());
1132 void IOThread::UpdateDnsClientEnabled() {
1133 globals()->host_resolver->SetDnsClientEnabled(*dns_client_enabled_);
1136 void IOThread::ConfigureQuic(const CommandLine& command_line) {
1137 // Always fetch the field trial group to ensure it is reported correctly.
1138 // The command line flags will be associated with a group that is reported
1139 // so long as trial is actually queried.
1140 std::string group =
1141 base::FieldTrialList::FindFullName(kQuicFieldTrialName);
1142 VariationParameters params;
1143 if (!variations::GetVariationParams(kQuicFieldTrialName, &params)) {
1144 params.clear();
1147 ConfigureQuicGlobals(command_line, group, params, globals_);
1150 // static
1151 void IOThread::ConfigureQuicGlobals(
1152 const base::CommandLine& command_line,
1153 base::StringPiece quic_trial_group,
1154 const VariationParameters& quic_trial_params,
1155 IOThread::Globals* globals) {
1156 bool enable_quic = ShouldEnableQuic(command_line, quic_trial_group);
1157 globals->enable_quic.set(enable_quic);
1158 if (enable_quic) {
1159 globals->enable_quic_time_based_loss_detection.set(
1160 ShouldEnableQuicTimeBasedLossDetection(command_line, quic_trial_group,
1161 quic_trial_params));
1162 globals->enable_quic_port_selection.set(
1163 ShouldEnableQuicPortSelection(command_line));
1164 globals->quic_connection_options =
1165 GetQuicConnectionOptions(command_line, quic_trial_params);
1166 if (ShouldEnableQuicPacing(command_line, quic_trial_group,
1167 quic_trial_params)) {
1168 globals->quic_connection_options.push_back(net::kPACE);
1172 size_t max_packet_length = GetQuicMaxPacketLength(command_line,
1173 quic_trial_group,
1174 quic_trial_params);
1175 if (max_packet_length != 0) {
1176 globals->quic_max_packet_length.set(max_packet_length);
1179 std::string quic_user_agent_id =
1180 chrome::VersionInfo::GetVersionStringModifier();
1181 if (!quic_user_agent_id.empty())
1182 quic_user_agent_id.push_back(' ');
1183 chrome::VersionInfo version_info;
1184 quic_user_agent_id.append(version_info.ProductNameAndVersionForUserAgent());
1185 globals->quic_user_agent_id.set(quic_user_agent_id);
1187 net::QuicVersion version = GetQuicVersion(command_line, quic_trial_params);
1188 if (version != net::QUIC_VERSION_UNSUPPORTED) {
1189 net::QuicVersionVector supported_versions;
1190 supported_versions.push_back(version);
1191 globals->quic_supported_versions.set(supported_versions);
1194 double threshold =
1195 GetAlternateProtocolProbabilityThreshold(command_line, quic_trial_params);
1196 if (threshold >=0 && threshold <= 1) {
1197 globals->alternate_protocol_probability_threshold.set(threshold);
1198 globals->http_server_properties->SetAlternateProtocolProbabilityThreshold(
1199 threshold);
1202 if (command_line.HasSwitch(switches::kOriginToForceQuicOn)) {
1203 net::HostPortPair quic_origin =
1204 net::HostPortPair::FromString(
1205 command_line.GetSwitchValueASCII(switches::kOriginToForceQuicOn));
1206 if (!quic_origin.IsEmpty()) {
1207 globals->origin_to_force_quic_on.set(quic_origin);
1212 bool IOThread::ShouldEnableQuic(const CommandLine& command_line,
1213 base::StringPiece quic_trial_group) {
1214 if (command_line.HasSwitch(switches::kDisableQuic))
1215 return false;
1217 if (command_line.HasSwitch(switches::kEnableQuic))
1218 return true;
1220 return quic_trial_group.starts_with(kQuicFieldTrialEnabledGroupName) ||
1221 quic_trial_group.starts_with(kQuicFieldTrialHttpsEnabledGroupName);
1224 bool IOThread::ShouldEnableQuicPortSelection(
1225 const CommandLine& command_line) {
1226 if (command_line.HasSwitch(switches::kDisableQuicPortSelection))
1227 return false;
1229 if (command_line.HasSwitch(switches::kEnableQuicPortSelection))
1230 return true;
1232 return false; // Default to disabling port selection on all channels.
1235 bool IOThread::ShouldEnableQuicPacing(
1236 const CommandLine& command_line,
1237 base::StringPiece quic_trial_group,
1238 const VariationParameters& quic_trial_params) {
1239 if (command_line.HasSwitch(switches::kEnableQuicPacing))
1240 return true;
1242 if (command_line.HasSwitch(switches::kDisableQuicPacing))
1243 return false;
1245 if (LowerCaseEqualsASCII(
1246 GetVariationParam(quic_trial_params, "enable_pacing"),
1247 "true"))
1248 return true;
1250 return quic_trial_group.ends_with(kQuicFieldTrialPacingSuffix);
1253 net::QuicTagVector IOThread::GetQuicConnectionOptions(
1254 const CommandLine& command_line,
1255 const VariationParameters& quic_trial_params) {
1256 if (command_line.HasSwitch(switches::kQuicConnectionOptions)) {
1257 return ParseQuicConnectionOptions(
1258 command_line.GetSwitchValueASCII(switches::kQuicConnectionOptions));
1261 VariationParameters::const_iterator it =
1262 quic_trial_params.find("connection_options");
1263 if (it == quic_trial_params.end()) {
1264 // TODO(rch): remove support for deprecated congestion_options.
1265 it = quic_trial_params.find("congestion_options");
1266 if (it == quic_trial_params.end())
1267 return net::QuicTagVector();
1270 return ParseQuicConnectionOptions(it->second);
1273 // static
1274 net::QuicTagVector IOThread::ParseQuicConnectionOptions(
1275 const std::string& connection_options) {
1276 net::QuicTagVector options;
1277 std::vector<std::string> tokens;
1278 base::SplitString(connection_options, ',', &tokens);
1279 // Tokens are expected to be no more than 4 characters long, but we
1280 // handle overflow gracefully.
1281 for (std::vector<std::string>::iterator token = tokens.begin();
1282 token != tokens.end(); ++token) {
1283 uint32 option = 0;
1284 for (size_t i = token->length() ; i > 0; --i) {
1285 option <<= 8;
1286 option |= static_cast<unsigned char>((*token)[i - 1]);
1288 options.push_back(static_cast<net::QuicTag>(option));
1290 return options;
1293 // static
1294 double IOThread::GetAlternateProtocolProbabilityThreshold(
1295 const base::CommandLine& command_line,
1296 const VariationParameters& quic_trial_params) {
1297 double value;
1298 if (command_line.HasSwitch(
1299 switches::kAlternateProtocolProbabilityThreshold)) {
1300 if (base::StringToDouble(
1301 command_line.GetSwitchValueASCII(
1302 switches::kAlternateProtocolProbabilityThreshold),
1303 &value)) {
1304 return value;
1307 if (base::StringToDouble(
1308 GetVariationParam(quic_trial_params,
1309 "alternate_protocol_probability_threshold"),
1310 &value)) {
1311 return value;
1313 return -1;
1316 // static
1317 bool IOThread::ShouldEnableQuicTimeBasedLossDetection(
1318 const CommandLine& command_line,
1319 base::StringPiece quic_trial_group,
1320 const VariationParameters& quic_trial_params) {
1321 if (command_line.HasSwitch(switches::kEnableQuicTimeBasedLossDetection))
1322 return true;
1324 if (command_line.HasSwitch(switches::kDisableQuicTimeBasedLossDetection))
1325 return false;
1327 if (LowerCaseEqualsASCII(
1328 GetVariationParam(quic_trial_params, "enable_time_based_loss_detection"),
1329 "true"))
1330 return true;
1332 return quic_trial_group.ends_with(
1333 kQuicFieldTrialTimeBasedLossDetectionSuffix);
1336 // static
1337 size_t IOThread::GetQuicMaxPacketLength(
1338 const CommandLine& command_line,
1339 base::StringPiece quic_trial_group,
1340 const VariationParameters& quic_trial_params) {
1341 if (command_line.HasSwitch(switches::kQuicMaxPacketLength)) {
1342 unsigned value;
1343 if (!base::StringToUint(
1344 command_line.GetSwitchValueASCII(switches::kQuicMaxPacketLength),
1345 &value)) {
1346 return 0;
1348 return value;
1351 unsigned value;
1352 if (base::StringToUint(GetVariationParam(quic_trial_params,
1353 "max_packet_length"),
1354 &value)) {
1355 return value;
1358 // Format of the packet length group names is:
1359 // (Https)?Enabled<length>BytePackets.
1360 base::StringPiece length_str(quic_trial_group);
1361 if (length_str.starts_with(kQuicFieldTrialEnabledGroupName)) {
1362 length_str.remove_prefix(strlen(kQuicFieldTrialEnabledGroupName));
1363 } else if (length_str.starts_with(kQuicFieldTrialHttpsEnabledGroupName)) {
1364 length_str.remove_prefix(strlen(kQuicFieldTrialHttpsEnabledGroupName));
1365 } else {
1366 return 0;
1368 if (!length_str.ends_with(kQuicFieldTrialPacketLengthSuffix)) {
1369 return 0;
1371 length_str.remove_suffix(strlen(kQuicFieldTrialPacketLengthSuffix));
1372 if (!base::StringToUint(length_str, &value)) {
1373 return 0;
1375 return value;
1378 // static
1379 net::QuicVersion IOThread::GetQuicVersion(
1380 const CommandLine& command_line,
1381 const VariationParameters& quic_trial_params) {
1382 if (command_line.HasSwitch(switches::kQuicVersion)) {
1383 return ParseQuicVersion(
1384 command_line.GetSwitchValueASCII(switches::kQuicVersion));
1387 return ParseQuicVersion(GetVariationParam(quic_trial_params, "quic_version"));
1390 // static
1391 net::QuicVersion IOThread::ParseQuicVersion(const std::string& quic_version) {
1392 net::QuicVersionVector supported_versions = net::QuicSupportedVersions();
1393 for (size_t i = 0; i < supported_versions.size(); ++i) {
1394 net::QuicVersion version = supported_versions[i];
1395 if (net::QuicVersionToString(version) == quic_version) {
1396 return version;
1400 return net::QUIC_VERSION_UNSUPPORTED;