Convert browser_tests to Swarming.
[chromium-blink-merge.git] / chrome / browser / io_thread.cc
blob9db2002d90380a1eec99c67bcc30a0f7bc59f83f
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/prefs/pref_registry_simple.h"
18 #include "base/prefs/pref_service.h"
19 #include "base/profiler/scoped_tracker.h"
20 #include "base/stl_util.h"
21 #include "base/strings/string_number_conversions.h"
22 #include "base/strings/string_piece.h"
23 #include "base/strings/string_split.h"
24 #include "base/strings/string_util.h"
25 #include "base/threading/sequenced_worker_pool.h"
26 #include "base/threading/thread.h"
27 #include "base/threading/worker_pool.h"
28 #include "base/time/time.h"
29 #include "base/trace_event/trace_event.h"
30 #include "build/build_config.h"
31 #include "chrome/browser/browser_process.h"
32 #include "chrome/browser/net/async_dns_field_trial.h"
33 #include "chrome/browser/net/chrome_net_log.h"
34 #include "chrome/browser/net/chrome_network_delegate.h"
35 #include "chrome/browser/net/connect_interceptor.h"
36 #include "chrome/browser/net/dns_probe_service.h"
37 #include "chrome/browser/net/pref_proxy_config_tracker.h"
38 #include "chrome/browser/net/proxy_service_factory.h"
39 #include "chrome/common/chrome_content_client.h"
40 #include "chrome/common/chrome_switches.h"
41 #include "chrome/common/chrome_version_info.h"
42 #include "chrome/common/pref_names.h"
43 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_prefs.h"
44 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h"
45 #include "components/policy/core/common/policy_service.h"
46 #include "components/variations/variations_associated_data.h"
47 #include "content/public/browser/browser_thread.h"
48 #include "content/public/browser/cookie_store_factory.h"
49 #include "net/base/host_mapping_rules.h"
50 #include "net/base/net_util.h"
51 #include "net/base/network_quality_estimator.h"
52 #include "net/base/sdch_manager.h"
53 #include "net/cert/cert_policy_enforcer.h"
54 #include "net/cert/cert_verifier.h"
55 #include "net/cert/cert_verify_proc.h"
56 #include "net/cert/ct_known_logs.h"
57 #include "net/cert/ct_known_logs_static.h"
58 #include "net/cert/ct_log_verifier.h"
59 #include "net/cert/ct_verifier.h"
60 #include "net/cert/multi_log_ct_verifier.h"
61 #include "net/cert/multi_threaded_cert_verifier.h"
62 #include "net/cookies/cookie_store.h"
63 #include "net/dns/host_cache.h"
64 #include "net/dns/host_resolver.h"
65 #include "net/dns/mapped_host_resolver.h"
66 #include "net/ftp/ftp_network_layer.h"
67 #include "net/http/http_auth_filter.h"
68 #include "net/http/http_auth_handler_factory.h"
69 #include "net/http/http_network_layer.h"
70 #include "net/http/http_server_properties_impl.h"
71 #include "net/proxy/proxy_config_service.h"
72 #include "net/proxy/proxy_script_fetcher_impl.h"
73 #include "net/proxy/proxy_service.h"
74 #include "net/quic/crypto/crypto_protocol.h"
75 #include "net/quic/quic_protocol.h"
76 #include "net/quic/quic_utils.h"
77 #include "net/socket/tcp_client_socket.h"
78 #include "net/spdy/spdy_session.h"
79 #include "net/ssl/channel_id_service.h"
80 #include "net/ssl/default_channel_id_store.h"
81 #include "net/url_request/data_protocol_handler.h"
82 #include "net/url_request/file_protocol_handler.h"
83 #include "net/url_request/ftp_protocol_handler.h"
84 #include "net/url_request/static_http_user_agent_settings.h"
85 #include "net/url_request/url_fetcher.h"
86 #include "net/url_request/url_request_context.h"
87 #include "net/url_request/url_request_context_getter.h"
88 #include "net/url_request/url_request_job_factory_impl.h"
89 #include "net/url_request/url_request_throttler_manager.h"
90 #include "url/url_constants.h"
92 #if defined(ENABLE_CONFIGURATION_POLICY)
93 #include "policy/policy_constants.h"
94 #endif
96 #if defined(ENABLE_EXTENSIONS)
97 #include "chrome/browser/extensions/event_router_forwarder.h"
98 #endif
100 #if defined(USE_NSS_CERTS) || defined(OS_IOS)
101 #include "net/cert_net/nss_ocsp.h"
102 #endif
104 #if defined(OS_ANDROID)
105 #include "base/android/build_info.h"
106 #endif
108 #if defined(OS_CHROMEOS)
109 #include "chrome/browser/chromeos/net/cert_verify_proc_chromeos.h"
110 #include "chromeos/network/host_resolver_impl_chromeos.h"
111 #endif
113 using content::BrowserThread;
115 class SafeBrowsingURLRequestContext;
117 // The IOThread object must outlive any tasks posted to the IO thread before the
118 // Quit task, so base::Bind() calls are not refcounted.
120 namespace {
122 const char kTCPFastOpenFieldTrialName[] = "TCPFastOpen";
123 const char kTCPFastOpenHttpsEnabledGroupName[] = "HttpsEnabled";
125 const char kQuicFieldTrialName[] = "QUIC";
126 const char kQuicFieldTrialEnabledGroupName[] = "Enabled";
127 const char kQuicFieldTrialHttpsEnabledGroupName[] = "HttpsEnabled";
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 kSpdyFieldTrialHoldbackGroupNamePrefix[] = "SpdyDisabled";
138 const char kSpdyFieldTrialSpdy31GroupNamePrefix[] = "Spdy31Enabled";
139 const char kSpdyFieldTrialSpdy4GroupNamePrefix[] = "Spdy4Enabled";
140 const char kSpdyFieldTrialParametrizedPrefix[] = "Parametrized";
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_CERTS) || defined(OS_IOS)
154 net::SetURLRequestContextForNSSHttpIO(this);
155 #endif
158 private:
159 ~SystemURLRequestContext() override {
160 AssertNoURLRequests();
161 #if defined(USE_NSS_CERTS) || 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 base::CommandLine& command_line =
170 *base::CommandLine::ForCurrentProcess();
172 net::HostResolver::Options options;
174 // Use the retry attempts override from the command-line, if any.
175 if (command_line.HasSwitch(switches::kHostResolverRetryAttempts)) {
176 std::string s =
177 command_line.GetSwitchValueASCII(switches::kHostResolverRetryAttempts);
178 // Parse the switch (it should be a non-negative integer).
179 int n;
180 if (base::StringToInt(s, &n) && n >= 0) {
181 options.max_retry_attempts = static_cast<size_t>(n);
182 } else {
183 LOG(ERROR) << "Invalid switch for host resolver retry attempts: " << s;
187 scoped_ptr<net::HostResolver> global_host_resolver;
188 #if defined OS_CHROMEOS
189 global_host_resolver =
190 chromeos::HostResolverImplChromeOS::CreateSystemResolver(options,
191 net_log);
192 #else
193 global_host_resolver =
194 net::HostResolver::CreateSystemResolver(options, net_log);
195 #endif
197 // If hostname remappings were specified on the command-line, layer these
198 // rules on top of the real host resolver. This allows forwarding all requests
199 // through a designated test server.
200 if (!command_line.HasSwitch(switches::kHostResolverRules))
201 return global_host_resolver.Pass();
203 scoped_ptr<net::MappedHostResolver> remapped_resolver(
204 new net::MappedHostResolver(global_host_resolver.Pass()));
205 remapped_resolver->SetRulesFromString(
206 command_line.GetSwitchValueASCII(switches::kHostResolverRules));
207 return remapped_resolver.Pass();
210 // TODO(willchan): Remove proxy script fetcher context since it's not necessary
211 // now that I got rid of refcounting URLRequestContexts.
212 // See IOThread::Globals for details.
213 net::URLRequestContext*
214 ConstructProxyScriptFetcherContext(IOThread::Globals* globals,
215 net::NetLog* net_log) {
216 net::URLRequestContext* context = new net::URLRequestContext;
217 context->set_net_log(net_log);
218 context->set_host_resolver(globals->host_resolver.get());
219 context->set_cert_verifier(globals->cert_verifier.get());
220 context->set_transport_security_state(
221 globals->transport_security_state.get());
222 context->set_cert_transparency_verifier(
223 globals->cert_transparency_verifier.get());
224 context->set_http_auth_handler_factory(
225 globals->http_auth_handler_factory.get());
226 context->set_proxy_service(globals->proxy_script_fetcher_proxy_service.get());
227 context->set_http_transaction_factory(
228 globals->proxy_script_fetcher_http_transaction_factory.get());
229 context->set_job_factory(
230 globals->proxy_script_fetcher_url_request_job_factory.get());
231 context->set_cookie_store(globals->system_cookie_store.get());
232 context->set_channel_id_service(
233 globals->system_channel_id_service.get());
234 context->set_network_delegate(globals->system_network_delegate.get());
235 context->set_http_user_agent_settings(
236 globals->http_user_agent_settings.get());
237 // TODO(rtenneti): We should probably use HttpServerPropertiesManager for the
238 // system URLRequestContext too. There's no reason this should be tied to a
239 // profile.
240 return context;
243 net::URLRequestContext*
244 ConstructSystemRequestContext(IOThread::Globals* globals,
245 net::NetLog* net_log) {
246 net::URLRequestContext* context = new SystemURLRequestContext;
247 context->set_net_log(net_log);
248 context->set_host_resolver(globals->host_resolver.get());
249 context->set_cert_verifier(globals->cert_verifier.get());
250 context->set_transport_security_state(
251 globals->transport_security_state.get());
252 context->set_cert_transparency_verifier(
253 globals->cert_transparency_verifier.get());
254 context->set_http_auth_handler_factory(
255 globals->http_auth_handler_factory.get());
256 context->set_proxy_service(globals->system_proxy_service.get());
257 context->set_http_transaction_factory(
258 globals->system_http_transaction_factory.get());
259 context->set_job_factory(globals->system_url_request_job_factory.get());
260 context->set_cookie_store(globals->system_cookie_store.get());
261 context->set_channel_id_service(
262 globals->system_channel_id_service.get());
263 context->set_throttler_manager(globals->throttler_manager.get());
264 context->set_network_delegate(globals->system_network_delegate.get());
265 context->set_http_user_agent_settings(
266 globals->http_user_agent_settings.get());
267 context->set_network_quality_estimator(
268 globals->network_quality_estimator.get());
269 return context;
272 int GetSwitchValueAsInt(const base::CommandLine& command_line,
273 const std::string& switch_name) {
274 int value;
275 if (!base::StringToInt(command_line.GetSwitchValueASCII(switch_name),
276 &value)) {
277 return 0;
279 return value;
282 // Returns the value associated with |key| in |params| or "" if the
283 // key is not present in the map.
284 const std::string& GetVariationParam(
285 const std::map<std::string, std::string>& params,
286 const std::string& key) {
287 std::map<std::string, std::string>::const_iterator it = params.find(key);
288 if (it == params.end())
289 return base::EmptyString();
291 return it->second;
294 // Parse kUseSpdy command line flag options, which may contain the following:
296 // "off" : Disables SPDY support entirely.
297 // "no-ping" : Disables SPDY ping connection testing.
298 // "exclude=<host>" : Disables SPDY support for the host <host>.
299 // "no-compress" : Disables SPDY header compression.
300 // "no-alt-protocols : Disables alternate protocol support.
301 // "init-max-streams=<limit>" : Specifies the maximum number of concurrent
302 // streams for a SPDY session, unless the
303 // specifies a different value via SETTINGS.
304 void ConfigureSpdyGlobalsFromUseSpdyArgument(const std::string& mode,
305 IOThread::Globals* globals) {
306 static const char kOff[] = "off";
307 static const char kDisablePing[] = "no-ping";
308 static const char kExclude[] = "exclude"; // Hosts to exclude
309 static const char kDisableCompression[] = "no-compress";
310 static const char kDisableAltProtocols[] = "no-alt-protocols";
311 static const char kInitialMaxConcurrentStreams[] = "init-max-streams";
313 std::vector<std::string> spdy_options;
314 base::SplitString(mode, ',', &spdy_options);
316 for (const std::string& element : spdy_options) {
317 std::vector<std::string> name_value;
318 base::SplitString(element, '=', &name_value);
319 const std::string& option =
320 name_value.size() > 0 ? name_value[0] : std::string();
321 const std::string value =
322 name_value.size() > 1 ? name_value[1] : std::string();
324 if (option == kOff) {
325 net::HttpStreamFactory::set_spdy_enabled(false);
326 continue;
328 if (option == kDisablePing) {
329 globals->enable_spdy_ping_based_connection_checking.set(false);
330 continue;
332 if (option == kExclude) {
333 globals->forced_spdy_exclusions.insert(
334 net::HostPortPair::FromURL(GURL(value)));
335 continue;
337 if (option == kDisableCompression) {
338 globals->enable_spdy_compression.set(false);
339 continue;
341 if (option == kDisableAltProtocols) {
342 globals->use_alternate_protocols.set(false);
343 continue;
345 if (option == kInitialMaxConcurrentStreams) {
346 int streams;
347 if (base::StringToInt(value, &streams)) {
348 globals->initial_max_spdy_concurrent_streams.set(streams);
349 continue;
352 LOG(DFATAL) << "Unrecognized spdy option: " << option;
356 } // namespace
358 class IOThread::LoggingNetworkChangeObserver
359 : public net::NetworkChangeNotifier::IPAddressObserver,
360 public net::NetworkChangeNotifier::ConnectionTypeObserver,
361 public net::NetworkChangeNotifier::NetworkChangeObserver {
362 public:
363 // |net_log| must remain valid throughout our lifetime.
364 explicit LoggingNetworkChangeObserver(net::NetLog* net_log)
365 : net_log_(net_log) {
366 net::NetworkChangeNotifier::AddIPAddressObserver(this);
367 net::NetworkChangeNotifier::AddConnectionTypeObserver(this);
368 net::NetworkChangeNotifier::AddNetworkChangeObserver(this);
371 ~LoggingNetworkChangeObserver() override {
372 net::NetworkChangeNotifier::RemoveIPAddressObserver(this);
373 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
374 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this);
377 // NetworkChangeNotifier::IPAddressObserver implementation.
378 void OnIPAddressChanged() override {
379 VLOG(1) << "Observed a change to the network IP addresses";
381 net_log_->AddGlobalEntry(net::NetLog::TYPE_NETWORK_IP_ADDRESSES_CHANGED);
384 // NetworkChangeNotifier::ConnectionTypeObserver implementation.
385 void OnConnectionTypeChanged(
386 net::NetworkChangeNotifier::ConnectionType type) override {
387 std::string type_as_string =
388 net::NetworkChangeNotifier::ConnectionTypeToString(type);
390 VLOG(1) << "Observed a change to network connectivity state "
391 << type_as_string;
393 net_log_->AddGlobalEntry(
394 net::NetLog::TYPE_NETWORK_CONNECTIVITY_CHANGED,
395 net::NetLog::StringCallback("new_connection_type", &type_as_string));
398 // NetworkChangeNotifier::NetworkChangeObserver implementation.
399 void OnNetworkChanged(
400 net::NetworkChangeNotifier::ConnectionType type) override {
401 std::string type_as_string =
402 net::NetworkChangeNotifier::ConnectionTypeToString(type);
404 VLOG(1) << "Observed a network change to state " << type_as_string;
406 net_log_->AddGlobalEntry(
407 net::NetLog::TYPE_NETWORK_CHANGED,
408 net::NetLog::StringCallback("new_connection_type", &type_as_string));
411 private:
412 net::NetLog* net_log_;
413 DISALLOW_COPY_AND_ASSIGN(LoggingNetworkChangeObserver);
416 class SystemURLRequestContextGetter : public net::URLRequestContextGetter {
417 public:
418 explicit SystemURLRequestContextGetter(IOThread* io_thread);
420 // Implementation for net::UrlRequestContextGetter.
421 net::URLRequestContext* GetURLRequestContext() override;
422 scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner()
423 const override;
425 protected:
426 ~SystemURLRequestContextGetter() override;
428 private:
429 IOThread* const io_thread_; // Weak pointer, owned by BrowserProcess.
430 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
432 base::debug::LeakTracker<SystemURLRequestContextGetter> leak_tracker_;
435 SystemURLRequestContextGetter::SystemURLRequestContextGetter(
436 IOThread* io_thread)
437 : io_thread_(io_thread),
438 network_task_runner_(
439 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)) {
442 SystemURLRequestContextGetter::~SystemURLRequestContextGetter() {}
444 net::URLRequestContext* SystemURLRequestContextGetter::GetURLRequestContext() {
445 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
446 DCHECK(io_thread_->globals()->system_request_context.get());
448 return io_thread_->globals()->system_request_context.get();
451 scoped_refptr<base::SingleThreadTaskRunner>
452 SystemURLRequestContextGetter::GetNetworkTaskRunner() const {
453 return network_task_runner_;
456 IOThread::Globals::
457 SystemRequestContextLeakChecker::SystemRequestContextLeakChecker(
458 Globals* globals)
459 : globals_(globals) {
460 DCHECK(globals_);
463 IOThread::Globals::
464 SystemRequestContextLeakChecker::~SystemRequestContextLeakChecker() {
465 if (globals_->system_request_context.get())
466 globals_->system_request_context->AssertNoURLRequests();
469 IOThread::Globals::Globals()
470 : system_request_context_leak_checker(this),
471 ignore_certificate_errors(false),
472 testing_fixed_http_port(0),
473 testing_fixed_https_port(0),
474 enable_user_alternate_protocol_ports(false) {
477 IOThread::Globals::~Globals() {}
479 // |local_state| is passed in explicitly in order to (1) reduce implicit
480 // dependencies and (2) make IOThread more flexible for testing.
481 IOThread::IOThread(
482 PrefService* local_state,
483 policy::PolicyService* policy_service,
484 ChromeNetLog* net_log,
485 extensions::EventRouterForwarder* extension_event_router_forwarder)
486 : net_log_(net_log),
487 #if defined(ENABLE_EXTENSIONS)
488 extension_event_router_forwarder_(extension_event_router_forwarder),
489 #endif
490 globals_(NULL),
491 is_spdy_disabled_by_policy_(false),
492 is_quic_allowed_by_policy_(true),
493 creation_time_(base::TimeTicks::Now()),
494 weak_factory_(this) {
495 auth_schemes_ = local_state->GetString(prefs::kAuthSchemes);
496 negotiate_disable_cname_lookup_ = local_state->GetBoolean(
497 prefs::kDisableAuthNegotiateCnameLookup);
498 negotiate_enable_port_ = local_state->GetBoolean(
499 prefs::kEnableAuthNegotiatePort);
500 auth_server_whitelist_ = local_state->GetString(prefs::kAuthServerWhitelist);
501 auth_delegate_whitelist_ = local_state->GetString(
502 prefs::kAuthNegotiateDelegateWhitelist);
503 gssapi_library_name_ = local_state->GetString(prefs::kGSSAPILibraryName);
504 pref_proxy_config_tracker_.reset(
505 ProxyServiceFactory::CreatePrefProxyConfigTrackerOfLocalState(
506 local_state));
507 ChromeNetworkDelegate::InitializePrefsOnUIThread(
508 &system_enable_referrers_,
509 NULL,
510 NULL,
511 NULL,
512 local_state);
513 ssl_config_service_manager_.reset(
514 SSLConfigServiceManager::CreateDefaultManager(local_state));
516 base::Value* dns_client_enabled_default = new base::FundamentalValue(
517 chrome_browser_net::ConfigureAsyncDnsFieldTrial());
518 local_state->SetDefaultPrefValue(prefs::kBuiltInDnsClientEnabled,
519 dns_client_enabled_default);
520 chrome_browser_net::LogAsyncDnsPrefSource(
521 local_state->FindPreference(prefs::kBuiltInDnsClientEnabled));
523 dns_client_enabled_.Init(prefs::kBuiltInDnsClientEnabled,
524 local_state,
525 base::Bind(&IOThread::UpdateDnsClientEnabled,
526 base::Unretained(this)));
527 dns_client_enabled_.MoveToThread(
528 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
530 quick_check_enabled_.Init(prefs::kQuickCheckEnabled,
531 local_state);
532 quick_check_enabled_.MoveToThread(
533 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
535 #if defined(ENABLE_CONFIGURATION_POLICY)
536 is_spdy_disabled_by_policy_ = policy_service->GetPolicies(
537 policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME, std::string())).Get(
538 policy::key::kDisableSpdy) != NULL;
540 const base::Value* value = policy_service->GetPolicies(
541 policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME,
542 std::string())).GetValue(policy::key::kQuicAllowed);
543 if (value)
544 value->GetAsBoolean(&is_quic_allowed_by_policy_);
545 #endif // ENABLE_CONFIGURATION_POLICY
547 BrowserThread::SetDelegate(BrowserThread::IO, this);
550 IOThread::~IOThread() {
551 // This isn't needed for production code, but in tests, IOThread may
552 // be multiply constructed.
553 BrowserThread::SetDelegate(BrowserThread::IO, NULL);
555 pref_proxy_config_tracker_->DetachFromPrefService();
556 DCHECK(!globals_);
559 IOThread::Globals* IOThread::globals() {
560 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
561 return globals_;
564 void IOThread::SetGlobalsForTesting(Globals* globals) {
565 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
566 DCHECK(!globals || !globals_);
567 globals_ = globals;
570 ChromeNetLog* IOThread::net_log() {
571 return net_log_;
574 void IOThread::ChangedToOnTheRecord() {
575 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
576 BrowserThread::PostTask(
577 BrowserThread::IO,
578 FROM_HERE,
579 base::Bind(&IOThread::ChangedToOnTheRecordOnIOThread,
580 base::Unretained(this)));
583 net::URLRequestContextGetter* IOThread::system_url_request_context_getter() {
584 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
585 if (!system_url_request_context_getter_.get()) {
586 InitSystemRequestContext();
588 return system_url_request_context_getter_.get();
591 void IOThread::Init() {
592 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
593 // is fixed.
594 tracked_objects::ScopedTracker tracking_profile1(
595 FROM_HERE_WITH_EXPLICIT_FUNCTION("466432 IOThread::InitAsync::Start"));
596 TRACE_EVENT0("startup", "IOThread::InitAsync");
597 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
599 #if defined(USE_NSS_CERTS) || defined(OS_IOS)
600 net::SetMessageLoopForNSSHttpIO();
601 #endif
603 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
604 // is fixed.
605 tracked_objects::ScopedTracker tracking_profile2(
606 FROM_HERE_WITH_EXPLICIT_FUNCTION(
607 "466432 IOThread::InitAsync::CommandLineForCurrentProcess"));
608 const base::CommandLine& command_line =
609 *base::CommandLine::ForCurrentProcess();
611 DCHECK(!globals_);
612 globals_ = new Globals;
614 // Add an observer that will emit network change events to the ChromeNetLog.
615 // Assuming NetworkChangeNotifier dispatches in FIFO order, we should be
616 // logging the network change before other IO thread consumers respond to it.
617 network_change_observer_.reset(
618 new LoggingNetworkChangeObserver(net_log_));
620 // Setup the HistogramWatcher to run on the IO thread.
621 net::NetworkChangeNotifier::InitHistogramWatcher();
623 #if defined(ENABLE_EXTENSIONS)
624 globals_->extension_event_router_forwarder =
625 extension_event_router_forwarder_;
626 #endif
628 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
629 // is fixed.
630 tracked_objects::ScopedTracker tracking_profile3(
631 FROM_HERE_WITH_EXPLICIT_FUNCTION(
632 "466432 IOThread::InitAsync::ChromeNetworkDelegate"));
633 scoped_ptr<ChromeNetworkDelegate> chrome_network_delegate(
634 new ChromeNetworkDelegate(extension_event_router_forwarder(),
635 &system_enable_referrers_));
637 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
638 // is fixed.
639 tracked_objects::ScopedTracker tracking_profile4(
640 FROM_HERE_WITH_EXPLICIT_FUNCTION(
641 "466432 IOThread::InitAsync::CreateGlobalHostResolver"));
642 globals_->system_network_delegate = chrome_network_delegate.Pass();
643 globals_->host_resolver = CreateGlobalHostResolver(net_log_);
645 globals_->network_quality_estimator.reset(new net::NetworkQualityEstimator());
647 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
648 // is fixed.
649 tracked_objects::ScopedTracker tracking_profile5(
650 FROM_HERE_WITH_EXPLICIT_FUNCTION(
651 "466432 IOThread::InitAsync::UpdateDnsClientEnabled::Start"));
652 UpdateDnsClientEnabled();
653 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
654 // is fixed.
655 tracked_objects::ScopedTracker tracking_profile6(
656 FROM_HERE_WITH_EXPLICIT_FUNCTION(
657 "466432 IOThread::InitAsync::UpdateDnsClientEnabled::End"));
658 #if defined(OS_CHROMEOS)
659 // Creates a CertVerifyProc that doesn't allow any profile-provided certs.
660 globals_->cert_verifier.reset(new net::MultiThreadedCertVerifier(
661 new chromeos::CertVerifyProcChromeOS()));
662 #else
663 globals_->cert_verifier.reset(new net::MultiThreadedCertVerifier(
664 net::CertVerifyProc::CreateDefault()));
665 #endif
667 globals_->transport_security_state.reset(new net::TransportSecurityState());
669 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
670 // is fixed.
671 tracked_objects::ScopedTracker tracking_profile7(
672 FROM_HERE_WITH_EXPLICIT_FUNCTION(
673 "466432 IOThread::InitAsync::CreateMultiLogVerifier"));
674 net::MultiLogCTVerifier* ct_verifier = new net::MultiLogCTVerifier();
675 globals_->cert_transparency_verifier.reset(ct_verifier);
677 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
678 // is fixed.
679 tracked_objects::ScopedTracker tracking_profile8(
680 FROM_HERE_WITH_EXPLICIT_FUNCTION(
681 "466432 IOThread::InitAsync::CreateLogVerifiers::Start"));
682 // Add built-in logs
683 ct_verifier->AddLogs(net::ct::CreateLogVerifiersForKnownLogs());
684 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
685 // is fixed.
686 tracked_objects::ScopedTracker tracking_profile9(
687 FROM_HERE_WITH_EXPLICIT_FUNCTION(
688 "466432 IOThread::InitAsync::CreateLogVerifiers::End"));
690 // Add logs from command line
691 if (command_line.HasSwitch(switches::kCertificateTransparencyLog)) {
692 std::string switch_value = command_line.GetSwitchValueASCII(
693 switches::kCertificateTransparencyLog);
694 std::vector<std::string> logs;
695 base::SplitString(switch_value, ',', &logs);
696 for (std::vector<std::string>::iterator it = logs.begin(); it != logs.end();
697 ++it) {
698 const std::string& curr_log = *it;
699 std::vector<std::string> log_metadata;
700 base::SplitString(curr_log, ':', &log_metadata);
701 CHECK_GE(log_metadata.size(), 3u)
702 << "CT log metadata missing: Switch format is "
703 << "'description:base64_key:url_without_schema'.";
704 std::string log_description(log_metadata[0]);
705 std::string log_url(std::string("https://") + log_metadata[2]);
706 std::string ct_public_key_data;
707 CHECK(base::Base64Decode(log_metadata[1], &ct_public_key_data))
708 << "Unable to decode CT public key.";
709 scoped_ptr<net::CTLogVerifier> external_log_verifier(
710 net::CTLogVerifier::Create(ct_public_key_data, log_description,
711 log_url));
712 CHECK(external_log_verifier) << "Unable to parse CT public key.";
713 VLOG(1) << "Adding log with description " << log_description;
714 ct_verifier->AddLog(external_log_verifier.Pass());
718 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
719 // is fixed.
720 tracked_objects::ScopedTracker tracking_profile10(
721 FROM_HERE_WITH_EXPLICIT_FUNCTION(
722 "466432 IOThread::InitAsync::CertPolicyEnforcer"));
723 net::CertPolicyEnforcer* policy_enforcer = new net::CertPolicyEnforcer;
724 globals_->cert_policy_enforcer.reset(policy_enforcer);
726 globals_->ssl_config_service = GetSSLConfigService();
728 globals_->http_auth_handler_factory.reset(CreateDefaultAuthHandlerFactory(
729 globals_->host_resolver.get()));
730 globals_->http_server_properties.reset(new net::HttpServerPropertiesImpl());
731 // For the ProxyScriptFetcher, we use a direct ProxyService.
732 globals_->proxy_script_fetcher_proxy_service.reset(
733 net::ProxyService::CreateDirectWithNetLog(net_log_));
734 // In-memory cookie store.
735 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
736 // is fixed.
737 tracked_objects::ScopedTracker tracking_profile11(
738 FROM_HERE_WITH_EXPLICIT_FUNCTION(
739 "466432 IOThread::InitAsync::CreateCookieStore::Start"));
740 globals_->system_cookie_store =
741 content::CreateCookieStore(content::CookieStoreConfig());
742 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
743 // is fixed.
744 tracked_objects::ScopedTracker tracking_profile12(
745 FROM_HERE_WITH_EXPLICIT_FUNCTION(
746 "466432 IOThread::InitAsync::CreateCookieStore::End"));
747 // In-memory channel ID store.
748 globals_->system_channel_id_service.reset(
749 new net::ChannelIDService(
750 new net::DefaultChannelIDStore(NULL),
751 base::WorkerPool::GetTaskRunner(true)));
752 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
753 // is fixed.
754 tracked_objects::ScopedTracker tracking_profile12_1(
755 FROM_HERE_WITH_EXPLICIT_FUNCTION(
756 "466432 IOThread::InitAsync::CreateDnsProbeService"));
757 globals_->dns_probe_service.reset(new chrome_browser_net::DnsProbeService());
758 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
759 // is fixed.
760 tracked_objects::ScopedTracker tracking_profile12_2(
761 FROM_HERE_WITH_EXPLICIT_FUNCTION(
762 "466432 IOThread::InitAsync::CreateHostMappingRules"));
763 globals_->host_mapping_rules.reset(new net::HostMappingRules());
764 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
765 // is fixed.
766 tracked_objects::ScopedTracker tracking_profile12_3(
767 FROM_HERE_WITH_EXPLICIT_FUNCTION(
768 "466432 IOThread::InitAsync::CreateHTTPUserAgentSettings"));
769 globals_->http_user_agent_settings.reset(
770 new net::StaticHttpUserAgentSettings(std::string(), GetUserAgent()));
771 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
772 // is fixed.
773 tracked_objects::ScopedTracker tracking_profile12_4(
774 FROM_HERE_WITH_EXPLICIT_FUNCTION(
775 "466432 IOThread::InitAsync::CommandLineConfiguration"));
776 if (command_line.HasSwitch(switches::kHostRules)) {
777 TRACE_EVENT_BEGIN0("startup", "IOThread::InitAsync:SetRulesFromString");
778 globals_->host_mapping_rules->SetRulesFromString(
779 command_line.GetSwitchValueASCII(switches::kHostRules));
780 TRACE_EVENT_END0("startup", "IOThread::InitAsync:SetRulesFromString");
782 if (command_line.HasSwitch(switches::kIgnoreCertificateErrors))
783 globals_->ignore_certificate_errors = true;
784 if (command_line.HasSwitch(switches::kTestingFixedHttpPort)) {
785 globals_->testing_fixed_http_port =
786 GetSwitchValueAsInt(command_line, switches::kTestingFixedHttpPort);
788 if (command_line.HasSwitch(switches::kTestingFixedHttpsPort)) {
789 globals_->testing_fixed_https_port =
790 GetSwitchValueAsInt(command_line, switches::kTestingFixedHttpsPort);
792 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
793 // is fixed.
794 tracked_objects::ScopedTracker tracking_profile12_5(
795 FROM_HERE_WITH_EXPLICIT_FUNCTION(
796 "466432 IOThread::InitAsync::QuicConfiguration"));
797 ConfigureQuic(command_line);
798 if (command_line.HasSwitch(
799 switches::kEnableUserAlternateProtocolPorts)) {
800 globals_->enable_user_alternate_protocol_ports = true;
802 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
803 // is fixed.
804 tracked_objects::ScopedTracker tracking_profile13(
805 FROM_HERE_WITH_EXPLICIT_FUNCTION(
806 "466432 IOThread::InitAsync::InitializeNetworkOptions"));
807 InitializeNetworkOptions(command_line);
809 net::HttpNetworkSession::Params session_params;
810 InitializeNetworkSessionParams(&session_params);
811 session_params.net_log = net_log_;
812 session_params.proxy_service =
813 globals_->proxy_script_fetcher_proxy_service.get();
815 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
816 // is fixed.
817 tracked_objects::ScopedTracker tracking_profile14(
818 FROM_HERE_WITH_EXPLICIT_FUNCTION(
819 "466432 IOThread::InitAsync::HttpNetorkSession::Start"));
820 TRACE_EVENT_BEGIN0("startup", "IOThread::InitAsync:HttpNetworkSession");
821 scoped_refptr<net::HttpNetworkSession> network_session(
822 new net::HttpNetworkSession(session_params));
823 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
824 // is fixed.
825 tracked_objects::ScopedTracker tracking_profile15(
826 FROM_HERE_WITH_EXPLICIT_FUNCTION(
827 "466432 IOThread::InitAsync::HttpNetorkSession::End"));
828 globals_->proxy_script_fetcher_http_transaction_factory
829 .reset(new net::HttpNetworkLayer(network_session.get()));
830 TRACE_EVENT_END0("startup", "IOThread::InitAsync:HttpNetworkSession");
831 scoped_ptr<net::URLRequestJobFactoryImpl> job_factory(
832 new net::URLRequestJobFactoryImpl());
834 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
835 // is fixed.
836 tracked_objects::ScopedTracker tracking_profile16(
837 FROM_HERE_WITH_EXPLICIT_FUNCTION(
838 "466432 IOThread::InitAsync::SetProtocolHandler"));
839 job_factory->SetProtocolHandler(url::kDataScheme,
840 new net::DataProtocolHandler());
841 job_factory->SetProtocolHandler(
842 url::kFileScheme,
843 new net::FileProtocolHandler(
844 content::BrowserThread::GetBlockingPool()->
845 GetTaskRunnerWithShutdownBehavior(
846 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)));
847 #if !defined(DISABLE_FTP_SUPPORT)
848 globals_->proxy_script_fetcher_ftp_transaction_factory.reset(
849 new net::FtpNetworkLayer(globals_->host_resolver.get()));
850 job_factory->SetProtocolHandler(
851 url::kFtpScheme,
852 new net::FtpProtocolHandler(
853 globals_->proxy_script_fetcher_ftp_transaction_factory.get()));
854 #endif
855 globals_->proxy_script_fetcher_url_request_job_factory = job_factory.Pass();
857 globals_->throttler_manager.reset(new net::URLRequestThrottlerManager());
858 globals_->throttler_manager->set_net_log(net_log_);
859 // Always done in production, disabled only for unit tests.
860 globals_->throttler_manager->set_enable_thread_checks(true);
862 globals_->proxy_script_fetcher_context.reset(
863 ConstructProxyScriptFetcherContext(globals_, net_log_));
865 #if defined(OS_MACOSX) && !defined(OS_IOS)
866 // Start observing Keychain events. This needs to be done on the UI thread,
867 // as Keychain services requires a CFRunLoop.
868 BrowserThread::PostTask(BrowserThread::UI,
869 FROM_HERE,
870 base::Bind(&ObserveKeychainEvents));
871 #endif
873 // InitSystemRequestContext turns right around and posts a task back
874 // to the IO thread, so we can't let it run until we know the IO
875 // thread has started.
877 // Note that since we are at BrowserThread::Init time, the UI thread
878 // is blocked waiting for the thread to start. Therefore, posting
879 // this task to the main thread's message loop here is guaranteed to
880 // get it onto the message loop while the IOThread object still
881 // exists. However, the message might not be processed on the UI
882 // thread until after IOThread is gone, so use a weak pointer.
883 BrowserThread::PostTask(BrowserThread::UI,
884 FROM_HERE,
885 base::Bind(&IOThread::InitSystemRequestContext,
886 weak_factory_.GetWeakPtr()));
889 void IOThread::CleanUp() {
890 base::debug::LeakTracker<SafeBrowsingURLRequestContext>::CheckForLeaks();
892 #if defined(USE_NSS_CERTS) || defined(OS_IOS)
893 net::ShutdownNSSHttpIO();
894 #endif
896 system_url_request_context_getter_ = NULL;
898 // Release objects that the net::URLRequestContext could have been pointing
899 // to.
901 // Shutdown the HistogramWatcher on the IO thread.
902 net::NetworkChangeNotifier::ShutdownHistogramWatcher();
904 // This must be reset before the ChromeNetLog is destroyed.
905 network_change_observer_.reset();
907 system_proxy_config_service_.reset();
909 delete globals_;
910 globals_ = NULL;
912 base::debug::LeakTracker<SystemURLRequestContextGetter>::CheckForLeaks();
915 void IOThread::InitializeNetworkOptions(const base::CommandLine& command_line) {
916 // Only handle use-spdy command line flags if "spdy.disabled" preference is
917 // not disabled via policy.
918 if (is_spdy_disabled_by_policy_) {
919 base::FieldTrial* trial = base::FieldTrialList::Find(kSpdyFieldTrialName);
920 if (trial)
921 trial->Disable();
922 } else {
923 std::string group = base::FieldTrialList::FindFullName(kSpdyFieldTrialName);
924 VariationParameters params;
925 if (!variations::GetVariationParams(kSpdyFieldTrialName, &params)) {
926 params.clear();
928 ConfigureSpdyGlobals(command_line, group, params, globals_);
931 ConfigureTCPFastOpen(command_line);
933 // TODO(rch): Make the client socket factory a per-network session
934 // instance, constructed from a NetworkSession::Params, to allow us
935 // to move this option to IOThread::Globals &
936 // HttpNetworkSession::Params.
939 void IOThread::ConfigureTCPFastOpen(const base::CommandLine& command_line) {
940 const std::string trial_group =
941 base::FieldTrialList::FindFullName(kTCPFastOpenFieldTrialName);
942 if (trial_group == kTCPFastOpenHttpsEnabledGroupName)
943 globals_->enable_tcp_fast_open_for_ssl.set(true);
944 bool always_enable_if_supported =
945 command_line.HasSwitch(switches::kEnableTcpFastOpen);
946 // Check for OS support of TCP FastOpen, and turn it on for all connections
947 // if indicated by user.
948 net::CheckSupportAndMaybeEnableTCPFastOpen(always_enable_if_supported);
951 // static
952 void IOThread::ConfigureSpdyGlobals(
953 const base::CommandLine& command_line,
954 base::StringPiece spdy_trial_group,
955 const VariationParameters& spdy_trial_params,
956 IOThread::Globals* globals) {
957 if (command_line.HasSwitch(switches::kTrustedSpdyProxy)) {
958 globals->trusted_spdy_proxy.set(
959 command_line.GetSwitchValueASCII(switches::kTrustedSpdyProxy));
961 if (command_line.HasSwitch(switches::kIgnoreUrlFetcherCertRequests))
962 net::URLFetcher::SetIgnoreCertificateRequests(true);
964 if (command_line.HasSwitch(switches::kUseSpdy)) {
965 std::string spdy_mode =
966 command_line.GetSwitchValueASCII(switches::kUseSpdy);
967 ConfigureSpdyGlobalsFromUseSpdyArgument(spdy_mode, globals);
968 return;
971 globals->next_protos.clear();
972 globals->next_protos.push_back(net::kProtoHTTP11);
973 bool enable_quic = false;
974 globals->enable_quic.CopyToIfSet(&enable_quic);
975 if (enable_quic) {
976 globals->next_protos.push_back(net::kProtoQUIC1SPDY3);
979 // No SPDY command-line flags have been specified. Examine trial groups.
980 if (spdy_trial_group.starts_with(kSpdyFieldTrialHoldbackGroupNamePrefix)) {
981 net::HttpStreamFactory::set_spdy_enabled(false);
982 return;
984 if (spdy_trial_group.starts_with(kSpdyFieldTrialSpdy31GroupNamePrefix)) {
985 globals->next_protos.push_back(net::kProtoSPDY31);
986 globals->use_alternate_protocols.set(true);
987 return;
989 if (spdy_trial_group.starts_with(kSpdyFieldTrialSpdy4GroupNamePrefix)) {
990 globals->next_protos.push_back(net::kProtoSPDY31);
991 globals->next_protos.push_back(net::kProtoSPDY4_14);
992 globals->next_protos.push_back(net::kProtoSPDY4);
993 globals->use_alternate_protocols.set(true);
994 return;
996 if (spdy_trial_group.starts_with(kSpdyFieldTrialParametrizedPrefix)) {
997 bool spdy_enabled = false;
998 if (base::LowerCaseEqualsASCII(
999 GetVariationParam(spdy_trial_params, "enable_spdy31"), "true")) {
1000 globals->next_protos.push_back(net::kProtoSPDY31);
1001 spdy_enabled = true;
1003 if (base::LowerCaseEqualsASCII(
1004 GetVariationParam(spdy_trial_params, "enable_http2_14"), "true")) {
1005 globals->next_protos.push_back(net::kProtoSPDY4_14);
1006 spdy_enabled = true;
1008 if (base::LowerCaseEqualsASCII(
1009 GetVariationParam(spdy_trial_params, "enable_http2"), "true")) {
1010 globals->next_protos.push_back(net::kProtoSPDY4);
1011 spdy_enabled = true;
1013 // TODO(bnc): HttpStreamFactory::spdy_enabled_ is redundant with
1014 // globals->next_protos, can it be eliminated?
1015 net::HttpStreamFactory::set_spdy_enabled(spdy_enabled);
1016 globals->use_alternate_protocols.set(true);
1017 return;
1020 // By default, enable HTTP/2.
1021 globals->next_protos.push_back(net::kProtoSPDY31);
1022 globals->next_protos.push_back(net::kProtoSPDY4_14);
1023 globals->next_protos.push_back(net::kProtoSPDY4);
1024 globals->use_alternate_protocols.set(true);
1027 // static
1028 void IOThread::RegisterPrefs(PrefRegistrySimple* registry) {
1029 registry->RegisterStringPref(prefs::kAuthSchemes,
1030 "basic,digest,ntlm,negotiate");
1031 registry->RegisterBooleanPref(prefs::kDisableAuthNegotiateCnameLookup, false);
1032 registry->RegisterBooleanPref(prefs::kEnableAuthNegotiatePort, false);
1033 registry->RegisterStringPref(prefs::kAuthServerWhitelist, std::string());
1034 registry->RegisterStringPref(prefs::kAuthNegotiateDelegateWhitelist,
1035 std::string());
1036 registry->RegisterStringPref(prefs::kGSSAPILibraryName, std::string());
1037 registry->RegisterStringPref(
1038 data_reduction_proxy::prefs::kDataReductionProxy, std::string());
1039 registry->RegisterBooleanPref(prefs::kEnableReferrers, true);
1040 data_reduction_proxy::RegisterPrefs(registry);
1041 registry->RegisterBooleanPref(prefs::kBuiltInDnsClientEnabled, true);
1042 registry->RegisterBooleanPref(prefs::kQuickCheckEnabled, true);
1045 net::HttpAuthHandlerFactory* IOThread::CreateDefaultAuthHandlerFactory(
1046 net::HostResolver* resolver) {
1047 net::HttpAuthFilterWhitelist* auth_filter_default_credentials = NULL;
1048 if (!auth_server_whitelist_.empty()) {
1049 auth_filter_default_credentials =
1050 new net::HttpAuthFilterWhitelist(auth_server_whitelist_);
1052 net::HttpAuthFilterWhitelist* auth_filter_delegate = NULL;
1053 if (!auth_delegate_whitelist_.empty()) {
1054 auth_filter_delegate =
1055 new net::HttpAuthFilterWhitelist(auth_delegate_whitelist_);
1057 globals_->url_security_manager.reset(
1058 net::URLSecurityManager::Create(auth_filter_default_credentials,
1059 auth_filter_delegate));
1060 std::vector<std::string> supported_schemes;
1061 base::SplitString(auth_schemes_, ',', &supported_schemes);
1063 scoped_ptr<net::HttpAuthHandlerRegistryFactory> registry_factory(
1064 net::HttpAuthHandlerRegistryFactory::Create(
1065 supported_schemes, globals_->url_security_manager.get(),
1066 resolver, gssapi_library_name_, negotiate_disable_cname_lookup_,
1067 negotiate_enable_port_));
1068 return registry_factory.release();
1071 void IOThread::ClearHostCache() {
1072 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
1074 net::HostCache* host_cache = globals_->host_resolver->GetHostCache();
1075 if (host_cache)
1076 host_cache->clear();
1079 void IOThread::InitializeNetworkSessionParams(
1080 net::HttpNetworkSession::Params* params) {
1081 InitializeNetworkSessionParamsFromGlobals(*globals_, params);
1084 // static
1085 void IOThread::InitializeNetworkSessionParamsFromGlobals(
1086 const IOThread::Globals& globals,
1087 net::HttpNetworkSession::Params* params) {
1088 params->host_resolver = globals.host_resolver.get();
1089 params->cert_verifier = globals.cert_verifier.get();
1090 params->cert_policy_enforcer = globals.cert_policy_enforcer.get();
1091 params->channel_id_service = globals.system_channel_id_service.get();
1092 params->transport_security_state = globals.transport_security_state.get();
1093 params->ssl_config_service = globals.ssl_config_service.get();
1094 params->http_auth_handler_factory = globals.http_auth_handler_factory.get();
1095 params->http_server_properties =
1096 globals.http_server_properties->GetWeakPtr();
1097 params->network_delegate = globals.system_network_delegate.get();
1098 params->host_mapping_rules = globals.host_mapping_rules.get();
1099 params->ignore_certificate_errors = globals.ignore_certificate_errors;
1100 params->testing_fixed_http_port = globals.testing_fixed_http_port;
1101 params->testing_fixed_https_port = globals.testing_fixed_https_port;
1102 globals.enable_tcp_fast_open_for_ssl.CopyToIfSet(
1103 &params->enable_tcp_fast_open_for_ssl);
1105 globals.initial_max_spdy_concurrent_streams.CopyToIfSet(
1106 &params->spdy_initial_max_concurrent_streams);
1107 globals.enable_spdy_compression.CopyToIfSet(
1108 &params->enable_spdy_compression);
1109 globals.enable_spdy_ping_based_connection_checking.CopyToIfSet(
1110 &params->enable_spdy_ping_based_connection_checking);
1111 globals.spdy_default_protocol.CopyToIfSet(
1112 &params->spdy_default_protocol);
1113 params->next_protos = globals.next_protos;
1114 globals.trusted_spdy_proxy.CopyToIfSet(&params->trusted_spdy_proxy);
1115 params->forced_spdy_exclusions = globals.forced_spdy_exclusions;
1116 globals.use_alternate_protocols.CopyToIfSet(
1117 &params->use_alternate_protocols);
1118 globals.alternative_service_probability_threshold.CopyToIfSet(
1119 &params->alternative_service_probability_threshold);
1121 globals.enable_quic.CopyToIfSet(&params->enable_quic);
1122 globals.disable_insecure_quic.CopyToIfSet(&params->disable_insecure_quic);
1123 globals.enable_quic_for_proxies.CopyToIfSet(&params->enable_quic_for_proxies);
1124 globals.quic_always_require_handshake_confirmation.CopyToIfSet(
1125 &params->quic_always_require_handshake_confirmation);
1126 globals.quic_disable_connection_pooling.CopyToIfSet(
1127 &params->quic_disable_connection_pooling);
1128 globals.quic_load_server_info_timeout_srtt_multiplier.CopyToIfSet(
1129 &params->quic_load_server_info_timeout_srtt_multiplier);
1130 globals.quic_enable_connection_racing.CopyToIfSet(
1131 &params->quic_enable_connection_racing);
1132 globals.quic_enable_non_blocking_io.CopyToIfSet(
1133 &params->quic_enable_non_blocking_io);
1134 globals.quic_prefer_aes.CopyToIfSet(&params->quic_prefer_aes);
1135 globals.quic_disable_disk_cache.CopyToIfSet(
1136 &params->quic_disable_disk_cache);
1137 globals.quic_max_number_of_lossy_connections.CopyToIfSet(
1138 &params->quic_max_number_of_lossy_connections);
1139 globals.quic_packet_loss_threshold.CopyToIfSet(
1140 &params->quic_packet_loss_threshold);
1141 globals.quic_socket_receive_buffer_size.CopyToIfSet(
1142 &params->quic_socket_receive_buffer_size);
1143 globals.enable_quic_port_selection.CopyToIfSet(
1144 &params->enable_quic_port_selection);
1145 globals.quic_max_packet_length.CopyToIfSet(&params->quic_max_packet_length);
1146 globals.quic_user_agent_id.CopyToIfSet(&params->quic_user_agent_id);
1147 globals.quic_supported_versions.CopyToIfSet(
1148 &params->quic_supported_versions);
1149 params->quic_connection_options = globals.quic_connection_options;
1151 globals.origin_to_force_quic_on.CopyToIfSet(
1152 &params->origin_to_force_quic_on);
1153 params->enable_user_alternate_protocol_ports =
1154 globals.enable_user_alternate_protocol_ports;
1157 base::TimeTicks IOThread::creation_time() const {
1158 return creation_time_;
1161 net::SSLConfigService* IOThread::GetSSLConfigService() {
1162 return ssl_config_service_manager_->Get();
1165 void IOThread::ChangedToOnTheRecordOnIOThread() {
1166 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
1168 // Clear the host cache to avoid showing entries from the OTR session
1169 // in about:net-internals.
1170 ClearHostCache();
1173 void IOThread::InitSystemRequestContext() {
1174 if (system_url_request_context_getter_.get())
1175 return;
1176 // If we're in unit_tests, IOThread may not be run.
1177 if (!BrowserThread::IsMessageLoopValid(BrowserThread::IO))
1178 return;
1179 system_proxy_config_service_.reset(
1180 ProxyServiceFactory::CreateProxyConfigService(
1181 pref_proxy_config_tracker_.get()));
1182 system_url_request_context_getter_ =
1183 new SystemURLRequestContextGetter(this);
1184 // Safe to post an unretained this pointer, since IOThread is
1185 // guaranteed to outlive the IO BrowserThread.
1186 BrowserThread::PostTask(
1187 BrowserThread::IO,
1188 FROM_HERE,
1189 base::Bind(&IOThread::InitSystemRequestContextOnIOThread,
1190 base::Unretained(this)));
1193 void IOThread::InitSystemRequestContextOnIOThread() {
1194 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
1195 DCHECK(!globals_->system_proxy_service.get());
1196 DCHECK(system_proxy_config_service_.get());
1198 const base::CommandLine& command_line =
1199 *base::CommandLine::ForCurrentProcess();
1200 globals_->system_proxy_service.reset(
1201 ProxyServiceFactory::CreateProxyService(
1202 net_log_,
1203 globals_->proxy_script_fetcher_context.get(),
1204 globals_->system_network_delegate.get(),
1205 system_proxy_config_service_.release(),
1206 command_line,
1207 quick_check_enabled_.GetValue()));
1209 net::HttpNetworkSession::Params system_params;
1210 InitializeNetworkSessionParams(&system_params);
1211 system_params.net_log = net_log_;
1212 system_params.proxy_service = globals_->system_proxy_service.get();
1214 globals_->system_http_transaction_factory.reset(
1215 new net::HttpNetworkLayer(
1216 new net::HttpNetworkSession(system_params)));
1217 globals_->system_url_request_job_factory.reset(
1218 new net::URLRequestJobFactoryImpl());
1219 globals_->system_request_context.reset(
1220 ConstructSystemRequestContext(globals_, net_log_));
1221 globals_->system_request_context->set_ssl_config_service(
1222 globals_->ssl_config_service.get());
1223 globals_->system_request_context->set_http_server_properties(
1224 globals_->http_server_properties->GetWeakPtr());
1227 void IOThread::UpdateDnsClientEnabled() {
1228 globals()->host_resolver->SetDnsClientEnabled(*dns_client_enabled_);
1231 void IOThread::ConfigureQuic(const base::CommandLine& command_line) {
1232 // Always fetch the field trial group to ensure it is reported correctly.
1233 // The command line flags will be associated with a group that is reported
1234 // so long as trial is actually queried.
1235 std::string group =
1236 base::FieldTrialList::FindFullName(kQuicFieldTrialName);
1237 VariationParameters params;
1238 if (!variations::GetVariationParams(kQuicFieldTrialName, &params)) {
1239 params.clear();
1242 ConfigureQuicGlobals(command_line, group, params, is_quic_allowed_by_policy_,
1243 globals_);
1246 // static
1247 void IOThread::ConfigureQuicGlobals(
1248 const base::CommandLine& command_line,
1249 base::StringPiece quic_trial_group,
1250 const VariationParameters& quic_trial_params,
1251 bool quic_allowed_by_policy,
1252 IOThread::Globals* globals) {
1253 bool enable_quic = ShouldEnableQuic(command_line, quic_trial_group,
1254 quic_allowed_by_policy);
1255 globals->enable_quic.set(enable_quic);
1256 bool enable_quic_for_proxies = ShouldEnableQuicForProxies(
1257 command_line, quic_trial_group, quic_allowed_by_policy);
1258 globals->enable_quic_for_proxies.set(enable_quic_for_proxies);
1259 if (enable_quic) {
1260 globals->disable_insecure_quic.set(
1261 ShouldDisableInsecureQuic(quic_trial_params));
1262 globals->quic_always_require_handshake_confirmation.set(
1263 ShouldQuicAlwaysRequireHandshakeConfirmation(quic_trial_params));
1264 globals->quic_disable_connection_pooling.set(
1265 ShouldQuicDisableConnectionPooling(quic_trial_params));
1266 int receive_buffer_size = GetQuicSocketReceiveBufferSize(quic_trial_params);
1267 if (receive_buffer_size != 0) {
1268 globals->quic_socket_receive_buffer_size.set(receive_buffer_size);
1270 float load_server_info_timeout_srtt_multiplier =
1271 GetQuicLoadServerInfoTimeoutSrttMultiplier(quic_trial_params);
1272 if (load_server_info_timeout_srtt_multiplier != 0) {
1273 globals->quic_load_server_info_timeout_srtt_multiplier.set(
1274 load_server_info_timeout_srtt_multiplier);
1276 globals->quic_enable_connection_racing.set(
1277 ShouldQuicEnableConnectionRacing(quic_trial_params));
1278 globals->quic_enable_non_blocking_io.set(
1279 ShouldQuicEnableNonBlockingIO(quic_trial_params));
1280 globals->quic_disable_disk_cache.set(
1281 ShouldQuicDisableDiskCache(quic_trial_params));
1282 globals->quic_prefer_aes.set(
1283 ShouldQuicPreferAes(quic_trial_params));
1284 int max_number_of_lossy_connections = GetQuicMaxNumberOfLossyConnections(
1285 quic_trial_params);
1286 if (max_number_of_lossy_connections != 0) {
1287 globals->quic_max_number_of_lossy_connections.set(
1288 max_number_of_lossy_connections);
1290 float packet_loss_threshold = GetQuicPacketLossThreshold(quic_trial_params);
1291 if (packet_loss_threshold != 0)
1292 globals->quic_packet_loss_threshold.set(packet_loss_threshold);
1293 globals->enable_quic_port_selection.set(
1294 ShouldEnableQuicPortSelection(command_line));
1295 globals->quic_connection_options =
1296 GetQuicConnectionOptions(command_line, quic_trial_params);
1299 size_t max_packet_length = GetQuicMaxPacketLength(command_line,
1300 quic_trial_params);
1301 if (max_packet_length != 0) {
1302 globals->quic_max_packet_length.set(max_packet_length);
1305 std::string quic_user_agent_id =
1306 chrome::VersionInfo::GetVersionStringModifier();
1307 if (!quic_user_agent_id.empty())
1308 quic_user_agent_id.push_back(' ');
1309 chrome::VersionInfo version_info;
1310 quic_user_agent_id.append(version_info.ProductNameAndVersionForUserAgent());
1311 globals->quic_user_agent_id.set(quic_user_agent_id);
1313 net::QuicVersion version = GetQuicVersion(command_line, quic_trial_params);
1314 if (version != net::QUIC_VERSION_UNSUPPORTED) {
1315 net::QuicVersionVector supported_versions;
1316 supported_versions.push_back(version);
1317 globals->quic_supported_versions.set(supported_versions);
1320 double threshold = GetAlternativeProtocolProbabilityThreshold(
1321 command_line, quic_trial_params);
1322 if (threshold >=0 && threshold <= 1) {
1323 globals->alternative_service_probability_threshold.set(threshold);
1324 globals->http_server_properties->SetAlternativeServiceProbabilityThreshold(
1325 threshold);
1328 if (command_line.HasSwitch(switches::kOriginToForceQuicOn)) {
1329 net::HostPortPair quic_origin =
1330 net::HostPortPair::FromString(
1331 command_line.GetSwitchValueASCII(switches::kOriginToForceQuicOn));
1332 if (!quic_origin.IsEmpty()) {
1333 globals->origin_to_force_quic_on.set(quic_origin);
1338 bool IOThread::ShouldEnableQuic(const base::CommandLine& command_line,
1339 base::StringPiece quic_trial_group,
1340 bool quic_allowed_by_policy) {
1341 if (command_line.HasSwitch(switches::kDisableQuic) || !quic_allowed_by_policy)
1342 return false;
1344 if (command_line.HasSwitch(switches::kEnableQuic))
1345 return true;
1347 return quic_trial_group.starts_with(kQuicFieldTrialEnabledGroupName) ||
1348 quic_trial_group.starts_with(kQuicFieldTrialHttpsEnabledGroupName);
1351 // static
1352 bool IOThread::ShouldEnableQuicForProxies(const base::CommandLine& command_line,
1353 base::StringPiece quic_trial_group,
1354 bool quic_allowed_by_policy) {
1355 return ShouldEnableQuic(
1356 command_line, quic_trial_group, quic_allowed_by_policy) ||
1357 ShouldEnableQuicForDataReductionProxy();
1360 // static
1361 bool IOThread::ShouldEnableQuicForDataReductionProxy() {
1362 const base::CommandLine& command_line =
1363 *base::CommandLine::ForCurrentProcess();
1365 if (command_line.HasSwitch(switches::kDisableQuic))
1366 return false;
1368 return data_reduction_proxy::params::IsIncludedInQuicFieldTrial();
1371 // static
1372 bool IOThread::ShouldDisableInsecureQuic(
1373 const VariationParameters& quic_trial_params) {
1374 return base::LowerCaseEqualsASCII(
1375 GetVariationParam(quic_trial_params, "disable_insecure_quic"),
1376 "true");
1379 bool IOThread::ShouldEnableQuicPortSelection(
1380 const base::CommandLine& command_line) {
1381 if (command_line.HasSwitch(switches::kDisableQuicPortSelection))
1382 return false;
1384 if (command_line.HasSwitch(switches::kEnableQuicPortSelection))
1385 return true;
1387 return false; // Default to disabling port selection on all channels.
1390 net::QuicTagVector IOThread::GetQuicConnectionOptions(
1391 const base::CommandLine& command_line,
1392 const VariationParameters& quic_trial_params) {
1393 if (command_line.HasSwitch(switches::kQuicConnectionOptions)) {
1394 return net::QuicUtils::ParseQuicConnectionOptions(
1395 command_line.GetSwitchValueASCII(switches::kQuicConnectionOptions));
1398 VariationParameters::const_iterator it =
1399 quic_trial_params.find("connection_options");
1400 if (it == quic_trial_params.end()) {
1401 return net::QuicTagVector();
1404 return net::QuicUtils::ParseQuicConnectionOptions(it->second);
1407 // static
1408 double IOThread::GetAlternativeProtocolProbabilityThreshold(
1409 const base::CommandLine& command_line,
1410 const VariationParameters& quic_trial_params) {
1411 double value;
1412 if (command_line.HasSwitch(
1413 switches::kAlternativeServiceProbabilityThreshold)) {
1414 if (base::StringToDouble(
1415 command_line.GetSwitchValueASCII(
1416 switches::kAlternativeServiceProbabilityThreshold),
1417 &value)) {
1418 return value;
1421 if (command_line.HasSwitch(switches::kEnableQuic)) {
1422 return 0;
1424 // TODO(bnc): Remove when new parameter name rolls out and server
1425 // configuration is changed.
1426 if (base::StringToDouble(
1427 GetVariationParam(quic_trial_params,
1428 "alternate_protocol_probability_threshold"),
1429 &value)) {
1430 return value;
1432 if (base::StringToDouble(
1433 GetVariationParam(quic_trial_params,
1434 "alternative_service_probability_threshold"),
1435 &value)) {
1436 return value;
1438 return -1;
1441 // static
1442 bool IOThread::ShouldQuicAlwaysRequireHandshakeConfirmation(
1443 const VariationParameters& quic_trial_params) {
1444 return base::LowerCaseEqualsASCII(
1445 GetVariationParam(quic_trial_params,
1446 "always_require_handshake_confirmation"),
1447 "true");
1450 // static
1451 bool IOThread::ShouldQuicDisableConnectionPooling(
1452 const VariationParameters& quic_trial_params) {
1453 return base::LowerCaseEqualsASCII(
1454 GetVariationParam(quic_trial_params, "disable_connection_pooling"),
1455 "true");
1458 // static
1459 float IOThread::GetQuicLoadServerInfoTimeoutSrttMultiplier(
1460 const VariationParameters& quic_trial_params) {
1461 double value;
1462 if (base::StringToDouble(GetVariationParam(quic_trial_params,
1463 "load_server_info_time_to_srtt"),
1464 &value)) {
1465 return (float)value;
1467 return 0.0f;
1470 // static
1471 bool IOThread::ShouldQuicEnableConnectionRacing(
1472 const VariationParameters& quic_trial_params) {
1473 return base::LowerCaseEqualsASCII(
1474 GetVariationParam(quic_trial_params, "enable_connection_racing"),
1475 "true");
1478 // static
1479 bool IOThread::ShouldQuicEnableNonBlockingIO(
1480 const VariationParameters& quic_trial_params) {
1481 return base::LowerCaseEqualsASCII(
1482 GetVariationParam(quic_trial_params, "enable_non_blocking_io"),
1483 "true");
1486 // static
1487 bool IOThread::ShouldQuicDisableDiskCache(
1488 const VariationParameters& quic_trial_params) {
1489 return base::LowerCaseEqualsASCII(
1490 GetVariationParam(quic_trial_params, "disable_disk_cache"), "true");
1493 // static
1494 bool IOThread::ShouldQuicPreferAes(
1495 const VariationParameters& quic_trial_params) {
1496 return base::LowerCaseEqualsASCII(
1497 GetVariationParam(quic_trial_params, "prefer_aes"), "true");
1500 // static
1501 int IOThread::GetQuicMaxNumberOfLossyConnections(
1502 const VariationParameters& quic_trial_params) {
1503 int value;
1504 if (base::StringToInt(GetVariationParam(quic_trial_params,
1505 "max_number_of_lossy_connections"),
1506 &value)) {
1507 return value;
1509 return 0;
1512 // static
1513 float IOThread::GetQuicPacketLossThreshold(
1514 const VariationParameters& quic_trial_params) {
1515 double value;
1516 if (base::StringToDouble(GetVariationParam(quic_trial_params,
1517 "packet_loss_threshold"),
1518 &value)) {
1519 return (float)value;
1521 return 0.0f;
1524 // static
1525 int IOThread::GetQuicSocketReceiveBufferSize(
1526 const VariationParameters& quic_trial_params) {
1527 int value;
1528 if (base::StringToInt(GetVariationParam(quic_trial_params,
1529 "receive_buffer_size"),
1530 &value)) {
1531 return value;
1533 return 0;
1536 // static
1537 size_t IOThread::GetQuicMaxPacketLength(
1538 const base::CommandLine& command_line,
1539 const VariationParameters& quic_trial_params) {
1540 if (command_line.HasSwitch(switches::kQuicMaxPacketLength)) {
1541 unsigned value;
1542 if (!base::StringToUint(
1543 command_line.GetSwitchValueASCII(switches::kQuicMaxPacketLength),
1544 &value)) {
1545 return 0;
1547 return value;
1550 unsigned value;
1551 if (base::StringToUint(GetVariationParam(quic_trial_params,
1552 "max_packet_length"),
1553 &value)) {
1554 return value;
1556 return 0;
1559 // static
1560 net::QuicVersion IOThread::GetQuicVersion(
1561 const base::CommandLine& command_line,
1562 const VariationParameters& quic_trial_params) {
1563 if (command_line.HasSwitch(switches::kQuicVersion)) {
1564 return ParseQuicVersion(
1565 command_line.GetSwitchValueASCII(switches::kQuicVersion));
1568 return ParseQuicVersion(GetVariationParam(quic_trial_params, "quic_version"));
1571 // static
1572 net::QuicVersion IOThread::ParseQuicVersion(const std::string& quic_version) {
1573 net::QuicVersionVector supported_versions = net::QuicSupportedVersions();
1574 for (size_t i = 0; i < supported_versions.size(); ++i) {
1575 net::QuicVersion version = supported_versions[i];
1576 if (net::QuicVersionToString(version) == quic_version) {
1577 return version;
1581 return net::QUIC_VERSION_UNSUPPORTED;