Roll src/third_party/WebKit f298044:aa8346d (svn 202628:202629)
[chromium-blink-merge.git] / chrome / browser / io_thread.cc
bloba3558f737567547f83abbde9944a525a835883e3
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_network_delegate.h"
34 #include "chrome/browser/net/connect_interceptor.h"
35 #include "chrome/browser/net/dns_probe_service.h"
36 #include "chrome/browser/net/proxy_service_factory.h"
37 #include "chrome/common/channel_info.h"
38 #include "chrome/common/chrome_content_client.h"
39 #include "chrome/common/chrome_switches.h"
40 #include "chrome/common/pref_names.h"
41 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_prefs.h"
42 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h"
43 #include "components/net_log/chrome_net_log.h"
44 #include "components/policy/core/common/policy_service.h"
45 #include "components/proxy_config/pref_proxy_config_tracker.h"
46 #include "components/variations/variations_associated_data.h"
47 #include "components/version_info/version_info.h"
48 #include "content/public/browser/browser_thread.h"
49 #include "content/public/browser/cookie_store_factory.h"
50 #include "content/public/common/user_agent.h"
51 #include "net/base/external_estimate_provider.h"
52 #include "net/base/host_mapping_rules.h"
53 #include "net/base/net_util.h"
54 #include "net/base/network_quality_estimator.h"
55 #include "net/base/sdch_manager.h"
56 #include "net/cert/cert_policy_enforcer.h"
57 #include "net/cert/cert_verifier.h"
58 #include "net/cert/cert_verify_proc.h"
59 #include "net/cert/ct_known_logs.h"
60 #include "net/cert/ct_known_logs_static.h"
61 #include "net/cert/ct_log_verifier.h"
62 #include "net/cert/ct_verifier.h"
63 #include "net/cert/multi_log_ct_verifier.h"
64 #include "net/cert/multi_threaded_cert_verifier.h"
65 #include "net/cookies/cookie_store.h"
66 #include "net/dns/host_cache.h"
67 #include "net/dns/host_resolver.h"
68 #include "net/dns/mapped_host_resolver.h"
69 #include "net/ftp/ftp_network_layer.h"
70 #include "net/http/http_auth_filter.h"
71 #include "net/http/http_auth_handler_factory.h"
72 #include "net/http/http_network_layer.h"
73 #include "net/http/http_server_properties_impl.h"
74 #include "net/proxy/proxy_config_service.h"
75 #include "net/proxy/proxy_script_fetcher_impl.h"
76 #include "net/proxy/proxy_service.h"
77 #include "net/quic/crypto/crypto_protocol.h"
78 #include "net/quic/quic_protocol.h"
79 #include "net/quic/quic_utils.h"
80 #include "net/socket/tcp_client_socket.h"
81 #include "net/spdy/spdy_session.h"
82 #include "net/ssl/channel_id_service.h"
83 #include "net/ssl/default_channel_id_store.h"
84 #include "net/url_request/data_protocol_handler.h"
85 #include "net/url_request/file_protocol_handler.h"
86 #include "net/url_request/ftp_protocol_handler.h"
87 #include "net/url_request/static_http_user_agent_settings.h"
88 #include "net/url_request/url_fetcher.h"
89 #include "net/url_request/url_request_backoff_manager.h"
90 #include "net/url_request/url_request_context.h"
91 #include "net/url_request/url_request_context_builder.h"
92 #include "net/url_request/url_request_context_getter.h"
93 #include "net/url_request/url_request_job_factory_impl.h"
94 #include "url/url_constants.h"
96 #if defined(ENABLE_CONFIGURATION_POLICY)
97 #include "policy/policy_constants.h"
98 #endif
100 #if defined(ENABLE_EXTENSIONS)
101 #include "chrome/browser/extensions/event_router_forwarder.h"
102 #endif
104 #if defined(USE_NSS_CERTS) || defined(OS_IOS)
105 #include "net/cert_net/nss_ocsp.h"
106 #endif
108 #if defined(OS_ANDROID)
109 #include "base/android/build_info.h"
110 #include "chrome/browser/android/net/external_estimate_provider_android.h"
111 #endif
113 #if defined(OS_CHROMEOS)
114 #include "chrome/browser/chromeos/net/cert_verify_proc_chromeos.h"
115 #include "chromeos/network/host_resolver_impl_chromeos.h"
116 #endif
118 using content::BrowserThread;
120 class SafeBrowsingURLRequestContext;
122 // The IOThread object must outlive any tasks posted to the IO thread before the
123 // Quit task, so base::Bind() calls are not refcounted.
125 namespace {
127 const char kTCPFastOpenFieldTrialName[] = "TCPFastOpen";
128 const char kTCPFastOpenHttpsEnabledGroupName[] = "HttpsEnabled";
130 const char kQuicFieldTrialName[] = "QUIC";
131 const char kQuicFieldTrialEnabledGroupName[] = "Enabled";
132 const char kQuicFieldTrialHttpsEnabledGroupName[] = "HttpsEnabled";
134 // The SPDY trial composes two different trial plus control groups:
135 // * A "holdback" group with SPDY disabled, and corresponding control
136 // (SPDY/3.1). The primary purpose of the holdback group is to encourage site
137 // operators to do feature detection rather than UA-sniffing. As such, this
138 // trial runs continuously.
139 // * A SPDY/4 experiment, for SPDY/4 (aka HTTP/2) vs SPDY/3.1 comparisons and
140 // eventual SPDY/4 deployment.
141 const char kSpdyFieldTrialName[] = "SPDY";
142 const char kSpdyFieldTrialHoldbackGroupNamePrefix[] = "SpdyDisabled";
143 const char kSpdyFieldTrialSpdy31GroupNamePrefix[] = "Spdy31Enabled";
144 const char kSpdyFieldTrialSpdy4GroupNamePrefix[] = "Spdy4Enabled";
145 const char kSpdyFieldTrialParametrizedPrefix[] = "Parametrized";
147 // Field trial for network quality estimator. Seeds RTT and downstream
148 // throughput observations with values that correspond to the connection type
149 // determined by the operating system.
150 const char kNetworkQualityEstimatorFieldTrialName[] = "NetworkQualityEstimator";
152 #if defined(OS_MACOSX) && !defined(OS_IOS)
153 void ObserveKeychainEvents() {
154 DCHECK_CURRENTLY_ON(BrowserThread::UI);
155 net::CertDatabase::GetInstance()->SetMessageLoopForKeychainEvents();
157 #endif
159 // Used for the "system" URLRequestContext.
160 class SystemURLRequestContext : public net::URLRequestContext {
161 public:
162 SystemURLRequestContext() {
163 #if defined(USE_NSS_CERTS) || defined(OS_IOS)
164 net::SetURLRequestContextForNSSHttpIO(this);
165 #endif
168 private:
169 ~SystemURLRequestContext() override {
170 AssertNoURLRequests();
171 #if defined(USE_NSS_CERTS) || defined(OS_IOS)
172 net::SetURLRequestContextForNSSHttpIO(NULL);
173 #endif
177 scoped_ptr<net::HostResolver> CreateGlobalHostResolver(net::NetLog* net_log) {
178 TRACE_EVENT0("startup", "IOThread::CreateGlobalHostResolver");
179 const base::CommandLine& command_line =
180 *base::CommandLine::ForCurrentProcess();
182 net::HostResolver::Options options;
184 // Use the retry attempts override from the command-line, if any.
185 if (command_line.HasSwitch(switches::kHostResolverRetryAttempts)) {
186 std::string s =
187 command_line.GetSwitchValueASCII(switches::kHostResolverRetryAttempts);
188 // Parse the switch (it should be a non-negative integer).
189 int n;
190 if (base::StringToInt(s, &n) && n >= 0) {
191 options.max_retry_attempts = static_cast<size_t>(n);
192 } else {
193 LOG(ERROR) << "Invalid switch for host resolver retry attempts: " << s;
197 scoped_ptr<net::HostResolver> global_host_resolver;
198 #if defined OS_CHROMEOS
199 global_host_resolver =
200 chromeos::HostResolverImplChromeOS::CreateSystemResolver(options,
201 net_log);
202 #else
203 global_host_resolver =
204 net::HostResolver::CreateSystemResolver(options, net_log);
205 #endif
207 // If hostname remappings were specified on the command-line, layer these
208 // rules on top of the real host resolver. This allows forwarding all requests
209 // through a designated test server.
210 if (!command_line.HasSwitch(switches::kHostResolverRules))
211 return global_host_resolver.Pass();
213 scoped_ptr<net::MappedHostResolver> remapped_resolver(
214 new net::MappedHostResolver(global_host_resolver.Pass()));
215 remapped_resolver->SetRulesFromString(
216 command_line.GetSwitchValueASCII(switches::kHostResolverRules));
217 return remapped_resolver.Pass();
220 int GetSwitchValueAsInt(const base::CommandLine& command_line,
221 const std::string& switch_name) {
222 int value;
223 if (!base::StringToInt(command_line.GetSwitchValueASCII(switch_name),
224 &value)) {
225 return 0;
227 return value;
230 // Returns the value associated with |key| in |params| or "" if the
231 // key is not present in the map.
232 const std::string& GetVariationParam(
233 const std::map<std::string, std::string>& params,
234 const std::string& key) {
235 std::map<std::string, std::string>::const_iterator it = params.find(key);
236 if (it == params.end())
237 return base::EmptyString();
239 return it->second;
242 // Parse kUseSpdy command line flag options, which may contain the following:
244 // "off" : Disables SPDY support entirely.
245 // "no-ping" : Disables SPDY ping connection testing.
246 // "exclude=<host>" : Disables SPDY support for the host <host>.
247 // "no-compress" : Disables SPDY header compression.
248 // "init-max-streams=<limit>" : Specifies the maximum number of concurrent
249 // streams for a SPDY session, unless the
250 // specifies a different value via SETTINGS.
251 void ConfigureSpdyGlobalsFromUseSpdyArgument(const std::string& mode,
252 IOThread::Globals* globals) {
253 static const char kOff[] = "off";
254 static const char kDisablePing[] = "no-ping";
255 static const char kExclude[] = "exclude"; // Hosts to exclude
256 static const char kDisableCompression[] = "no-compress";
257 static const char kInitialMaxConcurrentStreams[] = "init-max-streams";
259 for (const base::StringPiece& element : base::SplitStringPiece(
260 mode, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) {
261 std::vector<base::StringPiece> name_value = base::SplitStringPiece(
262 element, "=", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
263 const base::StringPiece option =
264 name_value.size() > 0 ? name_value[0] : base::StringPiece();
265 const base::StringPiece value =
266 name_value.size() > 1 ? name_value[1] : base::StringPiece();
268 if (option == kOff) {
269 net::HttpStreamFactory::set_spdy_enabled(false);
270 continue;
272 if (option == kDisablePing) {
273 globals->enable_spdy_ping_based_connection_checking.set(false);
274 continue;
276 if (option == kExclude) {
277 globals->forced_spdy_exclusions.insert(
278 net::HostPortPair::FromURL(GURL(value.as_string())));
279 continue;
281 if (option == kDisableCompression) {
282 globals->enable_spdy_compression.set(false);
283 continue;
285 if (option == kInitialMaxConcurrentStreams) {
286 int streams;
287 if (base::StringToInt(value, &streams)) {
288 globals->initial_max_spdy_concurrent_streams.set(streams);
289 continue;
292 LOG(DFATAL) << "Unrecognized spdy option: " << option.as_string();
296 } // namespace
298 class IOThread::LoggingNetworkChangeObserver
299 : public net::NetworkChangeNotifier::IPAddressObserver,
300 public net::NetworkChangeNotifier::ConnectionTypeObserver,
301 public net::NetworkChangeNotifier::NetworkChangeObserver {
302 public:
303 // |net_log| must remain valid throughout our lifetime.
304 explicit LoggingNetworkChangeObserver(net::NetLog* net_log)
305 : net_log_(net_log) {
306 net::NetworkChangeNotifier::AddIPAddressObserver(this);
307 net::NetworkChangeNotifier::AddConnectionTypeObserver(this);
308 net::NetworkChangeNotifier::AddNetworkChangeObserver(this);
311 ~LoggingNetworkChangeObserver() override {
312 net::NetworkChangeNotifier::RemoveIPAddressObserver(this);
313 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
314 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this);
317 // NetworkChangeNotifier::IPAddressObserver implementation.
318 void OnIPAddressChanged() override {
319 VLOG(1) << "Observed a change to the network IP addresses";
321 net_log_->AddGlobalEntry(net::NetLog::TYPE_NETWORK_IP_ADDRESSES_CHANGED);
324 // NetworkChangeNotifier::ConnectionTypeObserver implementation.
325 void OnConnectionTypeChanged(
326 net::NetworkChangeNotifier::ConnectionType type) override {
327 std::string type_as_string =
328 net::NetworkChangeNotifier::ConnectionTypeToString(type);
330 VLOG(1) << "Observed a change to network connectivity state "
331 << type_as_string;
333 net_log_->AddGlobalEntry(
334 net::NetLog::TYPE_NETWORK_CONNECTIVITY_CHANGED,
335 net::NetLog::StringCallback("new_connection_type", &type_as_string));
338 // NetworkChangeNotifier::NetworkChangeObserver implementation.
339 void OnNetworkChanged(
340 net::NetworkChangeNotifier::ConnectionType type) override {
341 std::string type_as_string =
342 net::NetworkChangeNotifier::ConnectionTypeToString(type);
344 VLOG(1) << "Observed a network change to state " << type_as_string;
346 net_log_->AddGlobalEntry(
347 net::NetLog::TYPE_NETWORK_CHANGED,
348 net::NetLog::StringCallback("new_connection_type", &type_as_string));
351 private:
352 net::NetLog* net_log_;
353 DISALLOW_COPY_AND_ASSIGN(LoggingNetworkChangeObserver);
356 class SystemURLRequestContextGetter : public net::URLRequestContextGetter {
357 public:
358 explicit SystemURLRequestContextGetter(IOThread* io_thread);
360 // Implementation for net::UrlRequestContextGetter.
361 net::URLRequestContext* GetURLRequestContext() override;
362 scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner()
363 const override;
365 protected:
366 ~SystemURLRequestContextGetter() override;
368 private:
369 IOThread* const io_thread_; // Weak pointer, owned by BrowserProcess.
370 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
372 base::debug::LeakTracker<SystemURLRequestContextGetter> leak_tracker_;
375 SystemURLRequestContextGetter::SystemURLRequestContextGetter(
376 IOThread* io_thread)
377 : io_thread_(io_thread),
378 network_task_runner_(
379 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)) {
382 SystemURLRequestContextGetter::~SystemURLRequestContextGetter() {}
384 net::URLRequestContext* SystemURLRequestContextGetter::GetURLRequestContext() {
385 DCHECK_CURRENTLY_ON(BrowserThread::IO);
386 DCHECK(io_thread_->globals()->system_request_context.get());
388 return io_thread_->globals()->system_request_context.get();
391 scoped_refptr<base::SingleThreadTaskRunner>
392 SystemURLRequestContextGetter::GetNetworkTaskRunner() const {
393 return network_task_runner_;
396 IOThread::Globals::
397 SystemRequestContextLeakChecker::SystemRequestContextLeakChecker(
398 Globals* globals)
399 : globals_(globals) {
400 DCHECK(globals_);
403 IOThread::Globals::
404 SystemRequestContextLeakChecker::~SystemRequestContextLeakChecker() {
405 if (globals_->system_request_context.get())
406 globals_->system_request_context->AssertNoURLRequests();
409 IOThread::Globals::Globals()
410 : system_request_context_leak_checker(this),
411 ignore_certificate_errors(false),
412 testing_fixed_http_port(0),
413 testing_fixed_https_port(0),
414 enable_user_alternate_protocol_ports(false) {
417 IOThread::Globals::~Globals() {}
419 // |local_state| is passed in explicitly in order to (1) reduce implicit
420 // dependencies and (2) make IOThread more flexible for testing.
421 IOThread::IOThread(
422 PrefService* local_state,
423 policy::PolicyService* policy_service,
424 net_log::ChromeNetLog* net_log,
425 extensions::EventRouterForwarder* extension_event_router_forwarder)
426 : net_log_(net_log),
427 #if defined(ENABLE_EXTENSIONS)
428 extension_event_router_forwarder_(extension_event_router_forwarder),
429 #endif
430 globals_(NULL),
431 is_spdy_disabled_by_policy_(false),
432 is_quic_allowed_by_policy_(true),
433 creation_time_(base::TimeTicks::Now()),
434 weak_factory_(this) {
435 auth_schemes_ = local_state->GetString(prefs::kAuthSchemes);
436 negotiate_disable_cname_lookup_ = local_state->GetBoolean(
437 prefs::kDisableAuthNegotiateCnameLookup);
438 negotiate_enable_port_ = local_state->GetBoolean(
439 prefs::kEnableAuthNegotiatePort);
440 auth_server_whitelist_ = local_state->GetString(prefs::kAuthServerWhitelist);
441 auth_delegate_whitelist_ = local_state->GetString(
442 prefs::kAuthNegotiateDelegateWhitelist);
443 gssapi_library_name_ = local_state->GetString(prefs::kGSSAPILibraryName);
444 auth_android_negotiate_account_type_ =
445 local_state->GetString(prefs::kAuthAndroidNegotiateAccountType);
446 pref_proxy_config_tracker_.reset(
447 ProxyServiceFactory::CreatePrefProxyConfigTrackerOfLocalState(
448 local_state));
449 ChromeNetworkDelegate::InitializePrefsOnUIThread(
450 &system_enable_referrers_,
451 NULL,
452 NULL,
453 NULL,
454 local_state);
455 ssl_config_service_manager_.reset(
456 SSLConfigServiceManager::CreateDefaultManager(local_state));
458 base::Value* dns_client_enabled_default = new base::FundamentalValue(
459 chrome_browser_net::ConfigureAsyncDnsFieldTrial());
460 local_state->SetDefaultPrefValue(prefs::kBuiltInDnsClientEnabled,
461 dns_client_enabled_default);
462 chrome_browser_net::LogAsyncDnsPrefSource(
463 local_state->FindPreference(prefs::kBuiltInDnsClientEnabled));
465 dns_client_enabled_.Init(prefs::kBuiltInDnsClientEnabled,
466 local_state,
467 base::Bind(&IOThread::UpdateDnsClientEnabled,
468 base::Unretained(this)));
469 dns_client_enabled_.MoveToThread(
470 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
472 quick_check_enabled_.Init(prefs::kQuickCheckEnabled,
473 local_state);
474 quick_check_enabled_.MoveToThread(
475 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
477 #if defined(ENABLE_CONFIGURATION_POLICY)
478 is_spdy_disabled_by_policy_ = policy_service->GetPolicies(
479 policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME, std::string())).Get(
480 policy::key::kDisableSpdy) != NULL;
482 const base::Value* value = policy_service->GetPolicies(
483 policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME,
484 std::string())).GetValue(policy::key::kQuicAllowed);
485 if (value)
486 value->GetAsBoolean(&is_quic_allowed_by_policy_);
487 #endif // ENABLE_CONFIGURATION_POLICY
489 BrowserThread::SetDelegate(BrowserThread::IO, this);
492 IOThread::~IOThread() {
493 // This isn't needed for production code, but in tests, IOThread may
494 // be multiply constructed.
495 BrowserThread::SetDelegate(BrowserThread::IO, NULL);
497 pref_proxy_config_tracker_->DetachFromPrefService();
498 DCHECK(!globals_);
501 IOThread::Globals* IOThread::globals() {
502 DCHECK_CURRENTLY_ON(BrowserThread::IO);
503 return globals_;
506 void IOThread::SetGlobalsForTesting(Globals* globals) {
507 DCHECK_CURRENTLY_ON(BrowserThread::IO);
508 DCHECK(!globals || !globals_);
509 globals_ = globals;
512 net_log::ChromeNetLog* IOThread::net_log() {
513 return net_log_;
516 void IOThread::ChangedToOnTheRecord() {
517 DCHECK_CURRENTLY_ON(BrowserThread::UI);
518 BrowserThread::PostTask(
519 BrowserThread::IO,
520 FROM_HERE,
521 base::Bind(&IOThread::ChangedToOnTheRecordOnIOThread,
522 base::Unretained(this)));
525 net::URLRequestContextGetter* IOThread::system_url_request_context_getter() {
526 DCHECK_CURRENTLY_ON(BrowserThread::UI);
527 if (!system_url_request_context_getter_.get()) {
528 InitSystemRequestContext();
530 return system_url_request_context_getter_.get();
533 void IOThread::Init() {
534 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
535 // is fixed.
536 tracked_objects::ScopedTracker tracking_profile1(
537 FROM_HERE_WITH_EXPLICIT_FUNCTION("466432 IOThread::InitAsync::Start"));
538 TRACE_EVENT0("startup", "IOThread::InitAsync");
539 DCHECK_CURRENTLY_ON(BrowserThread::IO);
541 #if defined(USE_NSS_CERTS) || defined(OS_IOS)
542 net::SetMessageLoopForNSSHttpIO();
543 #endif
545 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
546 // is fixed.
547 tracked_objects::ScopedTracker tracking_profile2(
548 FROM_HERE_WITH_EXPLICIT_FUNCTION(
549 "466432 IOThread::InitAsync::CommandLineForCurrentProcess"));
550 const base::CommandLine& command_line =
551 *base::CommandLine::ForCurrentProcess();
553 DCHECK(!globals_);
554 globals_ = new Globals;
556 // Add an observer that will emit network change events to the ChromeNetLog.
557 // Assuming NetworkChangeNotifier dispatches in FIFO order, we should be
558 // logging the network change before other IO thread consumers respond to it.
559 network_change_observer_.reset(
560 new LoggingNetworkChangeObserver(net_log_));
562 // Setup the HistogramWatcher to run on the IO thread.
563 net::NetworkChangeNotifier::InitHistogramWatcher();
565 #if defined(ENABLE_EXTENSIONS)
566 globals_->extension_event_router_forwarder =
567 extension_event_router_forwarder_;
568 #endif
570 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
571 // is fixed.
572 tracked_objects::ScopedTracker tracking_profile3(
573 FROM_HERE_WITH_EXPLICIT_FUNCTION(
574 "466432 IOThread::InitAsync::ChromeNetworkDelegate"));
575 scoped_ptr<ChromeNetworkDelegate> chrome_network_delegate(
576 new ChromeNetworkDelegate(extension_event_router_forwarder(),
577 &system_enable_referrers_));
579 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
580 // is fixed.
581 tracked_objects::ScopedTracker tracking_profile4(
582 FROM_HERE_WITH_EXPLICIT_FUNCTION(
583 "466432 IOThread::InitAsync::CreateGlobalHostResolver"));
584 globals_->system_network_delegate = chrome_network_delegate.Pass();
585 globals_->host_resolver = CreateGlobalHostResolver(net_log_);
587 std::map<std::string, std::string> network_quality_estimator_params;
588 variations::GetVariationParams(kNetworkQualityEstimatorFieldTrialName,
589 &network_quality_estimator_params);
591 scoped_ptr<net::ExternalEstimateProvider> external_estimate_provider;
592 #if defined(OS_ANDROID)
593 external_estimate_provider.reset(
594 new chrome::android::ExternalEstimateProviderAndroid());
595 #endif
596 // Pass ownership.
597 globals_->network_quality_estimator.reset(new net::NetworkQualityEstimator(
598 external_estimate_provider.Pass(), network_quality_estimator_params));
600 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
601 // is fixed.
602 tracked_objects::ScopedTracker tracking_profile5(
603 FROM_HERE_WITH_EXPLICIT_FUNCTION(
604 "466432 IOThread::InitAsync::UpdateDnsClientEnabled::Start"));
605 UpdateDnsClientEnabled();
606 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
607 // is fixed.
608 tracked_objects::ScopedTracker tracking_profile6(
609 FROM_HERE_WITH_EXPLICIT_FUNCTION(
610 "466432 IOThread::InitAsync::UpdateDnsClientEnabled::End"));
611 #if defined(OS_CHROMEOS)
612 // Creates a CertVerifyProc that doesn't allow any profile-provided certs.
613 globals_->cert_verifier.reset(new net::MultiThreadedCertVerifier(
614 new chromeos::CertVerifyProcChromeOS()));
615 #else
616 globals_->cert_verifier.reset(new net::MultiThreadedCertVerifier(
617 net::CertVerifyProc::CreateDefault()));
618 #endif
620 globals_->transport_security_state.reset(new net::TransportSecurityState());
622 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
623 // is fixed.
624 tracked_objects::ScopedTracker tracking_profile8(
625 FROM_HERE_WITH_EXPLICIT_FUNCTION(
626 "466432 IOThread::InitAsync::CreateLogVerifiers::Start"));
627 std::vector<scoped_refptr<net::CTLogVerifier>> ct_logs(
628 net::ct::CreateLogVerifiersForKnownLogs());
630 // Add logs from command line
631 if (command_line.HasSwitch(switches::kCertificateTransparencyLog)) {
632 std::string switch_value = command_line.GetSwitchValueASCII(
633 switches::kCertificateTransparencyLog);
634 for (const base::StringPiece& curr_log : base::SplitStringPiece(
635 switch_value, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) {
636 std::vector<std::string> log_metadata = base::SplitString(
637 curr_log, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
638 CHECK_GE(log_metadata.size(), 3u)
639 << "CT log metadata missing: Switch format is "
640 << "'description:base64_key:url_without_schema'.";
641 std::string log_description(log_metadata[0]);
642 std::string log_url(std::string("https://") + log_metadata[2]);
643 std::string ct_public_key_data;
644 CHECK(base::Base64Decode(log_metadata[1], &ct_public_key_data))
645 << "Unable to decode CT public key.";
646 scoped_refptr<net::CTLogVerifier> external_log_verifier(
647 net::CTLogVerifier::Create(ct_public_key_data, log_description,
648 log_url));
649 CHECK(external_log_verifier) << "Unable to parse CT public key.";
650 VLOG(1) << "Adding log with description " << log_description;
651 ct_logs.push_back(external_log_verifier);
655 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
656 // is fixed.
657 tracked_objects::ScopedTracker tracking_profile9(
658 FROM_HERE_WITH_EXPLICIT_FUNCTION(
659 "466432 IOThread::InitAsync::CreateLogVerifiers::End"));
660 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
661 // is fixed.
662 tracked_objects::ScopedTracker tracking_profile7(
663 FROM_HERE_WITH_EXPLICIT_FUNCTION(
664 "466432 IOThread::InitAsync::CreateMultiLogVerifier"));
665 net::MultiLogCTVerifier* ct_verifier = new net::MultiLogCTVerifier();
666 globals_->cert_transparency_verifier.reset(ct_verifier);
667 // Add built-in logs
668 ct_verifier->AddLogs(ct_logs);
670 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
671 // is fixed.
672 tracked_objects::ScopedTracker tracking_profile10(
673 FROM_HERE_WITH_EXPLICIT_FUNCTION(
674 "466432 IOThread::InitAsync::CertPolicyEnforcer"));
675 net::CertPolicyEnforcer* policy_enforcer = new net::CertPolicyEnforcer;
676 globals_->cert_policy_enforcer.reset(policy_enforcer);
678 globals_->ssl_config_service = GetSSLConfigService();
680 globals_->http_auth_handler_factory.reset(CreateDefaultAuthHandlerFactory(
681 globals_->host_resolver.get()));
682 globals_->http_server_properties.reset(new net::HttpServerPropertiesImpl());
683 // For the ProxyScriptFetcher, we use a direct ProxyService.
684 globals_->proxy_script_fetcher_proxy_service =
685 net::ProxyService::CreateDirectWithNetLog(net_log_);
686 // In-memory cookie store.
687 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
688 // is fixed.
689 tracked_objects::ScopedTracker tracking_profile11(
690 FROM_HERE_WITH_EXPLICIT_FUNCTION(
691 "466432 IOThread::InitAsync::CreateCookieStore::Start"));
692 globals_->system_cookie_store =
693 content::CreateCookieStore(content::CookieStoreConfig());
694 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
695 // is fixed.
696 tracked_objects::ScopedTracker tracking_profile12(
697 FROM_HERE_WITH_EXPLICIT_FUNCTION(
698 "466432 IOThread::InitAsync::CreateCookieStore::End"));
699 // In-memory channel ID store.
700 globals_->system_channel_id_service.reset(
701 new net::ChannelIDService(
702 new net::DefaultChannelIDStore(NULL),
703 base::WorkerPool::GetTaskRunner(true)));
704 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
705 // is fixed.
706 tracked_objects::ScopedTracker tracking_profile12_1(
707 FROM_HERE_WITH_EXPLICIT_FUNCTION(
708 "466432 IOThread::InitAsync::CreateDnsProbeService"));
709 globals_->dns_probe_service.reset(new chrome_browser_net::DnsProbeService());
710 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
711 // is fixed.
712 tracked_objects::ScopedTracker tracking_profile12_2(
713 FROM_HERE_WITH_EXPLICIT_FUNCTION(
714 "466432 IOThread::InitAsync::CreateHostMappingRules"));
715 globals_->host_mapping_rules.reset(new net::HostMappingRules());
716 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
717 // is fixed.
718 tracked_objects::ScopedTracker tracking_profile12_3(
719 FROM_HERE_WITH_EXPLICIT_FUNCTION(
720 "466432 IOThread::InitAsync::CreateHTTPUserAgentSettings"));
721 globals_->http_user_agent_settings.reset(
722 new net::StaticHttpUserAgentSettings(std::string(), GetUserAgent()));
723 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
724 // is fixed.
725 tracked_objects::ScopedTracker tracking_profile12_4(
726 FROM_HERE_WITH_EXPLICIT_FUNCTION(
727 "466432 IOThread::InitAsync::CommandLineConfiguration"));
728 if (command_line.HasSwitch(switches::kHostRules)) {
729 TRACE_EVENT_BEGIN0("startup", "IOThread::InitAsync:SetRulesFromString");
730 globals_->host_mapping_rules->SetRulesFromString(
731 command_line.GetSwitchValueASCII(switches::kHostRules));
732 TRACE_EVENT_END0("startup", "IOThread::InitAsync:SetRulesFromString");
734 if (command_line.HasSwitch(switches::kIgnoreCertificateErrors))
735 globals_->ignore_certificate_errors = true;
736 if (command_line.HasSwitch(switches::kTestingFixedHttpPort)) {
737 globals_->testing_fixed_http_port =
738 GetSwitchValueAsInt(command_line, switches::kTestingFixedHttpPort);
740 if (command_line.HasSwitch(switches::kTestingFixedHttpsPort)) {
741 globals_->testing_fixed_https_port =
742 GetSwitchValueAsInt(command_line, switches::kTestingFixedHttpsPort);
744 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
745 // is fixed.
746 tracked_objects::ScopedTracker tracking_profile12_5(
747 FROM_HERE_WITH_EXPLICIT_FUNCTION(
748 "466432 IOThread::InitAsync::QuicConfiguration"));
749 ConfigureQuic(command_line);
750 if (command_line.HasSwitch(
751 switches::kEnableUserAlternateProtocolPorts)) {
752 globals_->enable_user_alternate_protocol_ports = true;
754 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
755 // is fixed.
756 tracked_objects::ScopedTracker tracking_profile13(
757 FROM_HERE_WITH_EXPLICIT_FUNCTION(
758 "466432 IOThread::InitAsync::InitializeNetworkOptions"));
759 InitializeNetworkOptions(command_line);
761 TRACE_EVENT_BEGIN0("startup",
762 "IOThread::Init:ProxyScriptFetcherRequestContext");
763 globals_->proxy_script_fetcher_context.reset(
764 ConstructProxyScriptFetcherContext(globals_, net_log_));
765 TRACE_EVENT_END0("startup",
766 "IOThread::Init:ProxyScriptFetcherRequestContext");
768 const version_info::Channel channel = chrome::GetChannel();
769 if (channel == version_info::Channel::UNKNOWN ||
770 channel == version_info::Channel::CANARY ||
771 channel == version_info::Channel::DEV) {
772 globals_->url_request_backoff_manager.reset(
773 new net::URLRequestBackoffManager());
776 #if defined(OS_MACOSX) && !defined(OS_IOS)
777 // Start observing Keychain events. This needs to be done on the UI thread,
778 // as Keychain services requires a CFRunLoop.
779 BrowserThread::PostTask(BrowserThread::UI,
780 FROM_HERE,
781 base::Bind(&ObserveKeychainEvents));
782 #endif
784 // InitSystemRequestContext turns right around and posts a task back
785 // to the IO thread, so we can't let it run until we know the IO
786 // thread has started.
788 // Note that since we are at BrowserThread::Init time, the UI thread
789 // is blocked waiting for the thread to start. Therefore, posting
790 // this task to the main thread's message loop here is guaranteed to
791 // get it onto the message loop while the IOThread object still
792 // exists. However, the message might not be processed on the UI
793 // thread until after IOThread is gone, so use a weak pointer.
794 BrowserThread::PostTask(BrowserThread::UI,
795 FROM_HERE,
796 base::Bind(&IOThread::InitSystemRequestContext,
797 weak_factory_.GetWeakPtr()));
800 void IOThread::CleanUp() {
801 base::debug::LeakTracker<SafeBrowsingURLRequestContext>::CheckForLeaks();
803 #if defined(USE_NSS_CERTS) || defined(OS_IOS)
804 net::ShutdownNSSHttpIO();
805 #endif
807 system_url_request_context_getter_ = NULL;
809 // Release objects that the net::URLRequestContext could have been pointing
810 // to.
812 // Shutdown the HistogramWatcher on the IO thread.
813 net::NetworkChangeNotifier::ShutdownHistogramWatcher();
815 // This must be reset before the ChromeNetLog is destroyed.
816 network_change_observer_.reset();
818 system_proxy_config_service_.reset();
820 delete globals_;
821 globals_ = NULL;
823 base::debug::LeakTracker<SystemURLRequestContextGetter>::CheckForLeaks();
826 void IOThread::InitializeNetworkOptions(const base::CommandLine& command_line) {
827 // Only handle use-spdy command line flags if "spdy.disabled" preference is
828 // not disabled via policy.
829 if (is_spdy_disabled_by_policy_) {
830 base::FieldTrial* trial = base::FieldTrialList::Find(kSpdyFieldTrialName);
831 if (trial)
832 trial->Disable();
833 } else {
834 std::string group = base::FieldTrialList::FindFullName(kSpdyFieldTrialName);
835 VariationParameters params;
836 if (!variations::GetVariationParams(kSpdyFieldTrialName, &params)) {
837 params.clear();
839 ConfigureSpdyGlobals(command_line, group, params, globals_);
842 ConfigureTCPFastOpen(command_line);
844 // TODO(rch): Make the client socket factory a per-network session
845 // instance, constructed from a NetworkSession::Params, to allow us
846 // to move this option to IOThread::Globals &
847 // HttpNetworkSession::Params.
850 void IOThread::ConfigureTCPFastOpen(const base::CommandLine& command_line) {
851 const std::string trial_group =
852 base::FieldTrialList::FindFullName(kTCPFastOpenFieldTrialName);
853 if (trial_group == kTCPFastOpenHttpsEnabledGroupName)
854 globals_->enable_tcp_fast_open_for_ssl.set(true);
855 bool always_enable_if_supported =
856 command_line.HasSwitch(switches::kEnableTcpFastOpen);
857 // Check for OS support of TCP FastOpen, and turn it on for all connections
858 // if indicated by user.
859 net::CheckSupportAndMaybeEnableTCPFastOpen(always_enable_if_supported);
862 void IOThread::ConfigureSpdyGlobals(
863 const base::CommandLine& command_line,
864 base::StringPiece spdy_trial_group,
865 const VariationParameters& spdy_trial_params,
866 IOThread::Globals* globals) {
867 if (command_line.HasSwitch(switches::kTrustedSpdyProxy)) {
868 globals->trusted_spdy_proxy.set(
869 command_line.GetSwitchValueASCII(switches::kTrustedSpdyProxy));
871 if (command_line.HasSwitch(switches::kIgnoreUrlFetcherCertRequests))
872 net::URLFetcher::SetIgnoreCertificateRequests(true);
874 if (command_line.HasSwitch(switches::kUseSpdy)) {
875 std::string spdy_mode =
876 command_line.GetSwitchValueASCII(switches::kUseSpdy);
877 ConfigureSpdyGlobalsFromUseSpdyArgument(spdy_mode, globals);
878 return;
881 globals->next_protos.clear();
882 globals->next_protos.push_back(net::kProtoHTTP11);
883 bool enable_quic = false;
884 globals->enable_quic.CopyToIfSet(&enable_quic);
885 if (enable_quic) {
886 globals->next_protos.push_back(net::kProtoQUIC1SPDY3);
889 // No SPDY command-line flags have been specified. Examine trial groups.
890 if (spdy_trial_group.starts_with(kSpdyFieldTrialHoldbackGroupNamePrefix)) {
891 net::HttpStreamFactory::set_spdy_enabled(false);
892 return;
894 if (spdy_trial_group.starts_with(kSpdyFieldTrialSpdy31GroupNamePrefix)) {
895 globals->next_protos.push_back(net::kProtoSPDY31);
896 return;
898 if (spdy_trial_group.starts_with(kSpdyFieldTrialSpdy4GroupNamePrefix)) {
899 globals->next_protos.push_back(net::kProtoSPDY31);
900 globals->next_protos.push_back(net::kProtoHTTP2);
901 return;
903 if (spdy_trial_group.starts_with(kSpdyFieldTrialParametrizedPrefix)) {
904 bool spdy_enabled = false;
905 if (base::LowerCaseEqualsASCII(
906 GetVariationParam(spdy_trial_params, "enable_spdy31"), "true")) {
907 globals->next_protos.push_back(net::kProtoSPDY31);
908 spdy_enabled = true;
910 if (base::LowerCaseEqualsASCII(
911 GetVariationParam(spdy_trial_params, "enable_http2"), "true")) {
912 globals->next_protos.push_back(net::kProtoHTTP2);
913 spdy_enabled = true;
915 // TODO(bnc): HttpStreamFactory::spdy_enabled_ is redundant with
916 // globals->next_protos, can it be eliminated?
917 net::HttpStreamFactory::set_spdy_enabled(spdy_enabled);
918 return;
921 // By default, enable HTTP/2.
922 globals->next_protos.push_back(net::kProtoSPDY31);
923 globals->next_protos.push_back(net::kProtoHTTP2);
926 void IOThread::RegisterPrefs(PrefRegistrySimple* registry) {
927 registry->RegisterStringPref(prefs::kAuthSchemes,
928 "basic,digest,ntlm,negotiate");
929 registry->RegisterBooleanPref(prefs::kDisableAuthNegotiateCnameLookup, false);
930 registry->RegisterBooleanPref(prefs::kEnableAuthNegotiatePort, false);
931 registry->RegisterStringPref(prefs::kAuthServerWhitelist, std::string());
932 registry->RegisterStringPref(prefs::kAuthNegotiateDelegateWhitelist,
933 std::string());
934 registry->RegisterStringPref(prefs::kGSSAPILibraryName, std::string());
935 registry->RegisterStringPref(prefs::kAuthAndroidNegotiateAccountType,
936 std::string());
937 registry->RegisterStringPref(
938 data_reduction_proxy::prefs::kDataReductionProxy, std::string());
939 registry->RegisterBooleanPref(prefs::kEnableReferrers, true);
940 data_reduction_proxy::RegisterPrefs(registry);
941 registry->RegisterBooleanPref(prefs::kBuiltInDnsClientEnabled, true);
942 registry->RegisterBooleanPref(prefs::kQuickCheckEnabled, true);
945 net::HttpAuthHandlerFactory* IOThread::CreateDefaultAuthHandlerFactory(
946 net::HostResolver* resolver) {
947 net::HttpAuthFilterWhitelist* auth_filter_default_credentials = NULL;
948 if (!auth_server_whitelist_.empty()) {
949 auth_filter_default_credentials =
950 new net::HttpAuthFilterWhitelist(auth_server_whitelist_);
952 net::HttpAuthFilterWhitelist* auth_filter_delegate = NULL;
953 if (!auth_delegate_whitelist_.empty()) {
954 auth_filter_delegate =
955 new net::HttpAuthFilterWhitelist(auth_delegate_whitelist_);
957 globals_->url_security_manager.reset(
958 net::URLSecurityManager::Create(auth_filter_default_credentials,
959 auth_filter_delegate));
960 std::vector<std::string> supported_schemes = base::SplitString(
961 auth_schemes_, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
963 scoped_ptr<net::HttpAuthHandlerRegistryFactory> registry_factory(
964 net::HttpAuthHandlerRegistryFactory::Create(
965 supported_schemes, globals_->url_security_manager.get(), resolver,
966 gssapi_library_name_, auth_android_negotiate_account_type_,
967 negotiate_disable_cname_lookup_, negotiate_enable_port_));
968 return registry_factory.release();
971 void IOThread::ClearHostCache() {
972 DCHECK_CURRENTLY_ON(BrowserThread::IO);
974 net::HostCache* host_cache = globals_->host_resolver->GetHostCache();
975 if (host_cache)
976 host_cache->clear();
979 void IOThread::InitializeNetworkSessionParams(
980 net::HttpNetworkSession::Params* params) {
981 InitializeNetworkSessionParamsFromGlobals(*globals_, params);
984 void IOThread::InitializeNetworkSessionParamsFromGlobals(
985 const IOThread::Globals& globals,
986 net::HttpNetworkSession::Params* params) {
987 // The next two properties of the params don't seem to be
988 // elements of URLRequestContext, so they must be set here.
989 params->cert_policy_enforcer = globals.cert_policy_enforcer.get();
990 params->host_mapping_rules = globals.host_mapping_rules.get();
992 params->ignore_certificate_errors = globals.ignore_certificate_errors;
993 params->testing_fixed_http_port = globals.testing_fixed_http_port;
994 params->testing_fixed_https_port = globals.testing_fixed_https_port;
995 globals.enable_tcp_fast_open_for_ssl.CopyToIfSet(
996 &params->enable_tcp_fast_open_for_ssl);
998 globals.initial_max_spdy_concurrent_streams.CopyToIfSet(
999 &params->spdy_initial_max_concurrent_streams);
1000 globals.enable_spdy_compression.CopyToIfSet(
1001 &params->enable_spdy_compression);
1002 globals.enable_spdy_ping_based_connection_checking.CopyToIfSet(
1003 &params->enable_spdy_ping_based_connection_checking);
1004 globals.spdy_default_protocol.CopyToIfSet(
1005 &params->spdy_default_protocol);
1006 params->next_protos = globals.next_protos;
1007 globals.trusted_spdy_proxy.CopyToIfSet(&params->trusted_spdy_proxy);
1008 params->forced_spdy_exclusions = globals.forced_spdy_exclusions;
1009 globals.use_alternative_services.CopyToIfSet(
1010 &params->use_alternative_services);
1011 globals.alternative_service_probability_threshold.CopyToIfSet(
1012 &params->alternative_service_probability_threshold);
1014 globals.enable_quic.CopyToIfSet(&params->enable_quic);
1015 globals.enable_insecure_quic.CopyToIfSet(&params->enable_insecure_quic);
1016 globals.enable_quic_for_proxies.CopyToIfSet(&params->enable_quic_for_proxies);
1017 globals.quic_always_require_handshake_confirmation.CopyToIfSet(
1018 &params->quic_always_require_handshake_confirmation);
1019 globals.quic_disable_connection_pooling.CopyToIfSet(
1020 &params->quic_disable_connection_pooling);
1021 globals.quic_load_server_info_timeout_srtt_multiplier.CopyToIfSet(
1022 &params->quic_load_server_info_timeout_srtt_multiplier);
1023 globals.quic_enable_connection_racing.CopyToIfSet(
1024 &params->quic_enable_connection_racing);
1025 globals.quic_enable_non_blocking_io.CopyToIfSet(
1026 &params->quic_enable_non_blocking_io);
1027 globals.quic_prefer_aes.CopyToIfSet(&params->quic_prefer_aes);
1028 globals.quic_disable_disk_cache.CopyToIfSet(
1029 &params->quic_disable_disk_cache);
1030 globals.quic_max_number_of_lossy_connections.CopyToIfSet(
1031 &params->quic_max_number_of_lossy_connections);
1032 globals.quic_packet_loss_threshold.CopyToIfSet(
1033 &params->quic_packet_loss_threshold);
1034 globals.quic_socket_receive_buffer_size.CopyToIfSet(
1035 &params->quic_socket_receive_buffer_size);
1036 globals.quic_delay_tcp_race.CopyToIfSet(&params->quic_delay_tcp_race);
1037 globals.enable_quic_port_selection.CopyToIfSet(
1038 &params->enable_quic_port_selection);
1039 globals.quic_max_packet_length.CopyToIfSet(&params->quic_max_packet_length);
1040 globals.quic_user_agent_id.CopyToIfSet(&params->quic_user_agent_id);
1041 globals.quic_supported_versions.CopyToIfSet(
1042 &params->quic_supported_versions);
1043 params->quic_connection_options = globals.quic_connection_options;
1045 globals.origin_to_force_quic_on.CopyToIfSet(
1046 &params->origin_to_force_quic_on);
1047 params->enable_user_alternate_protocol_ports =
1048 globals.enable_user_alternate_protocol_ports;
1051 base::TimeTicks IOThread::creation_time() const {
1052 return creation_time_;
1055 net::SSLConfigService* IOThread::GetSSLConfigService() {
1056 return ssl_config_service_manager_->Get();
1059 void IOThread::ChangedToOnTheRecordOnIOThread() {
1060 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1062 // Clear the host cache to avoid showing entries from the OTR session
1063 // in about:net-internals.
1064 ClearHostCache();
1067 void IOThread::InitSystemRequestContext() {
1068 if (system_url_request_context_getter_.get())
1069 return;
1070 // If we're in unit_tests, IOThread may not be run.
1071 if (!BrowserThread::IsMessageLoopValid(BrowserThread::IO))
1072 return;
1073 system_proxy_config_service_.reset(
1074 ProxyServiceFactory::CreateProxyConfigService(
1075 pref_proxy_config_tracker_.get()));
1076 system_url_request_context_getter_ =
1077 new SystemURLRequestContextGetter(this);
1078 // Safe to post an unretained this pointer, since IOThread is
1079 // guaranteed to outlive the IO BrowserThread.
1080 BrowserThread::PostTask(
1081 BrowserThread::IO,
1082 FROM_HERE,
1083 base::Bind(&IOThread::InitSystemRequestContextOnIOThread,
1084 base::Unretained(this)));
1087 void IOThread::InitSystemRequestContextOnIOThread() {
1088 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1089 DCHECK(!globals_->system_proxy_service.get());
1090 DCHECK(system_proxy_config_service_.get());
1092 const base::CommandLine& command_line =
1093 *base::CommandLine::ForCurrentProcess();
1094 globals_->system_proxy_service = ProxyServiceFactory::CreateProxyService(
1095 net_log_, globals_->proxy_script_fetcher_context.get(),
1096 globals_->system_network_delegate.get(),
1097 system_proxy_config_service_.release(), command_line,
1098 quick_check_enabled_.GetValue());
1100 globals_->system_request_context.reset(
1101 ConstructSystemRequestContext(globals_, net_log_));
1104 void IOThread::UpdateDnsClientEnabled() {
1105 globals()->host_resolver->SetDnsClientEnabled(*dns_client_enabled_);
1108 void IOThread::ConfigureQuic(const base::CommandLine& command_line) {
1109 // Always fetch the field trial group to ensure it is reported correctly.
1110 // The command line flags will be associated with a group that is reported
1111 // so long as trial is actually queried.
1112 std::string group =
1113 base::FieldTrialList::FindFullName(kQuicFieldTrialName);
1114 VariationParameters params;
1115 if (!variations::GetVariationParams(kQuicFieldTrialName, &params)) {
1116 params.clear();
1119 ConfigureQuicGlobals(command_line, group, params, is_quic_allowed_by_policy_,
1120 globals_);
1123 void IOThread::ConfigureQuicGlobals(
1124 const base::CommandLine& command_line,
1125 base::StringPiece quic_trial_group,
1126 const VariationParameters& quic_trial_params,
1127 bool quic_allowed_by_policy,
1128 IOThread::Globals* globals) {
1129 bool enable_quic = ShouldEnableQuic(command_line, quic_trial_group,
1130 quic_allowed_by_policy);
1131 globals->enable_quic.set(enable_quic);
1132 bool enable_quic_for_proxies = ShouldEnableQuicForProxies(
1133 command_line, quic_trial_group, quic_allowed_by_policy);
1134 globals->enable_quic_for_proxies.set(enable_quic_for_proxies);
1135 globals->use_alternative_services.set(
1136 ShouldQuicEnableAlternativeServices(command_line, quic_trial_params));
1137 if (enable_quic) {
1138 globals->enable_insecure_quic.set(
1139 ShouldEnableInsecureQuic(command_line, quic_trial_params));
1140 globals->quic_always_require_handshake_confirmation.set(
1141 ShouldQuicAlwaysRequireHandshakeConfirmation(quic_trial_params));
1142 globals->quic_disable_connection_pooling.set(
1143 ShouldQuicDisableConnectionPooling(quic_trial_params));
1144 int receive_buffer_size = GetQuicSocketReceiveBufferSize(quic_trial_params);
1145 if (receive_buffer_size != 0) {
1146 globals->quic_socket_receive_buffer_size.set(receive_buffer_size);
1148 globals->quic_delay_tcp_race.set(ShouldQuicDelayTcpRace(quic_trial_params));
1149 float load_server_info_timeout_srtt_multiplier =
1150 GetQuicLoadServerInfoTimeoutSrttMultiplier(quic_trial_params);
1151 if (load_server_info_timeout_srtt_multiplier != 0) {
1152 globals->quic_load_server_info_timeout_srtt_multiplier.set(
1153 load_server_info_timeout_srtt_multiplier);
1155 globals->quic_enable_connection_racing.set(
1156 ShouldQuicEnableConnectionRacing(quic_trial_params));
1157 globals->quic_enable_non_blocking_io.set(
1158 ShouldQuicEnableNonBlockingIO(quic_trial_params));
1159 globals->quic_disable_disk_cache.set(
1160 ShouldQuicDisableDiskCache(quic_trial_params));
1161 globals->quic_prefer_aes.set(
1162 ShouldQuicPreferAes(quic_trial_params));
1163 int max_number_of_lossy_connections = GetQuicMaxNumberOfLossyConnections(
1164 quic_trial_params);
1165 if (max_number_of_lossy_connections != 0) {
1166 globals->quic_max_number_of_lossy_connections.set(
1167 max_number_of_lossy_connections);
1169 float packet_loss_threshold = GetQuicPacketLossThreshold(quic_trial_params);
1170 if (packet_loss_threshold != 0)
1171 globals->quic_packet_loss_threshold.set(packet_loss_threshold);
1172 globals->enable_quic_port_selection.set(
1173 ShouldEnableQuicPortSelection(command_line));
1174 globals->quic_connection_options =
1175 GetQuicConnectionOptions(command_line, quic_trial_params);
1178 size_t max_packet_length = GetQuicMaxPacketLength(command_line,
1179 quic_trial_params);
1180 if (max_packet_length != 0) {
1181 globals->quic_max_packet_length.set(max_packet_length);
1184 std::string quic_user_agent_id = chrome::GetChannelString();
1185 if (!quic_user_agent_id.empty())
1186 quic_user_agent_id.push_back(' ');
1187 quic_user_agent_id.append(
1188 version_info::GetProductNameAndVersionForUserAgent());
1189 quic_user_agent_id.push_back(' ');
1190 quic_user_agent_id.append(content::BuildOSCpuInfo());
1191 globals->quic_user_agent_id.set(quic_user_agent_id);
1193 net::QuicVersion version = GetQuicVersion(command_line, quic_trial_params);
1194 if (version != net::QUIC_VERSION_UNSUPPORTED) {
1195 net::QuicVersionVector supported_versions;
1196 supported_versions.push_back(version);
1197 globals->quic_supported_versions.set(supported_versions);
1200 double threshold = GetAlternativeProtocolProbabilityThreshold(
1201 command_line, quic_trial_params);
1202 if (threshold >=0 && threshold <= 1) {
1203 globals->alternative_service_probability_threshold.set(threshold);
1204 globals->http_server_properties->SetAlternativeServiceProbabilityThreshold(
1205 threshold);
1208 if (command_line.HasSwitch(switches::kOriginToForceQuicOn)) {
1209 net::HostPortPair quic_origin =
1210 net::HostPortPair::FromString(
1211 command_line.GetSwitchValueASCII(switches::kOriginToForceQuicOn));
1212 if (!quic_origin.IsEmpty()) {
1213 globals->origin_to_force_quic_on.set(quic_origin);
1218 bool IOThread::ShouldEnableQuic(const base::CommandLine& command_line,
1219 base::StringPiece quic_trial_group,
1220 bool quic_allowed_by_policy) {
1221 if (command_line.HasSwitch(switches::kDisableQuic) || !quic_allowed_by_policy)
1222 return false;
1224 if (command_line.HasSwitch(switches::kEnableQuic))
1225 return true;
1227 return quic_trial_group.starts_with(kQuicFieldTrialEnabledGroupName) ||
1228 quic_trial_group.starts_with(kQuicFieldTrialHttpsEnabledGroupName);
1231 bool IOThread::ShouldEnableQuicForProxies(const base::CommandLine& command_line,
1232 base::StringPiece quic_trial_group,
1233 bool quic_allowed_by_policy) {
1234 return ShouldEnableQuic(
1235 command_line, quic_trial_group, quic_allowed_by_policy) ||
1236 ShouldEnableQuicForDataReductionProxy();
1239 bool IOThread::ShouldEnableQuicForDataReductionProxy() {
1240 const base::CommandLine& command_line =
1241 *base::CommandLine::ForCurrentProcess();
1243 if (command_line.HasSwitch(switches::kDisableQuic))
1244 return false;
1246 return data_reduction_proxy::params::IsIncludedInQuicFieldTrial();
1249 bool IOThread::ShouldEnableInsecureQuic(
1250 const base::CommandLine& command_line,
1251 const VariationParameters& quic_trial_params) {
1252 if (command_line.HasSwitch(switches::kEnableInsecureQuic))
1253 return true;
1255 return base::LowerCaseEqualsASCII(
1256 GetVariationParam(quic_trial_params, "enable_insecure_quic"),
1257 "true");
1260 bool IOThread::ShouldEnableQuicPortSelection(
1261 const base::CommandLine& command_line) {
1262 if (command_line.HasSwitch(switches::kDisableQuicPortSelection))
1263 return false;
1265 if (command_line.HasSwitch(switches::kEnableQuicPortSelection))
1266 return true;
1268 return false; // Default to disabling port selection on all channels.
1271 net::QuicTagVector IOThread::GetQuicConnectionOptions(
1272 const base::CommandLine& command_line,
1273 const VariationParameters& quic_trial_params) {
1274 if (command_line.HasSwitch(switches::kQuicConnectionOptions)) {
1275 return net::QuicUtils::ParseQuicConnectionOptions(
1276 command_line.GetSwitchValueASCII(switches::kQuicConnectionOptions));
1279 VariationParameters::const_iterator it =
1280 quic_trial_params.find("connection_options");
1281 if (it == quic_trial_params.end()) {
1282 return net::QuicTagVector();
1285 return net::QuicUtils::ParseQuicConnectionOptions(it->second);
1288 double IOThread::GetAlternativeProtocolProbabilityThreshold(
1289 const base::CommandLine& command_line,
1290 const VariationParameters& quic_trial_params) {
1291 double value;
1292 if (command_line.HasSwitch(
1293 switches::kAlternativeServiceProbabilityThreshold)) {
1294 if (base::StringToDouble(
1295 command_line.GetSwitchValueASCII(
1296 switches::kAlternativeServiceProbabilityThreshold),
1297 &value)) {
1298 return value;
1301 if (command_line.HasSwitch(switches::kEnableQuic)) {
1302 return 0;
1304 // TODO(bnc): Remove when new parameter name rolls out and server
1305 // configuration is changed.
1306 if (base::StringToDouble(
1307 GetVariationParam(quic_trial_params,
1308 "alternate_protocol_probability_threshold"),
1309 &value)) {
1310 return value;
1312 if (base::StringToDouble(
1313 GetVariationParam(quic_trial_params,
1314 "alternative_service_probability_threshold"),
1315 &value)) {
1316 return value;
1318 return -1;
1321 bool IOThread::ShouldQuicAlwaysRequireHandshakeConfirmation(
1322 const VariationParameters& quic_trial_params) {
1323 return base::LowerCaseEqualsASCII(
1324 GetVariationParam(quic_trial_params,
1325 "always_require_handshake_confirmation"),
1326 "true");
1329 bool IOThread::ShouldQuicDisableConnectionPooling(
1330 const VariationParameters& quic_trial_params) {
1331 return base::LowerCaseEqualsASCII(
1332 GetVariationParam(quic_trial_params, "disable_connection_pooling"),
1333 "true");
1336 float IOThread::GetQuicLoadServerInfoTimeoutSrttMultiplier(
1337 const VariationParameters& quic_trial_params) {
1338 double value;
1339 if (base::StringToDouble(GetVariationParam(quic_trial_params,
1340 "load_server_info_time_to_srtt"),
1341 &value)) {
1342 return static_cast<float>(value);
1344 return 0.0f;
1347 bool IOThread::ShouldQuicEnableConnectionRacing(
1348 const VariationParameters& quic_trial_params) {
1349 return base::LowerCaseEqualsASCII(
1350 GetVariationParam(quic_trial_params, "enable_connection_racing"),
1351 "true");
1354 bool IOThread::ShouldQuicEnableNonBlockingIO(
1355 const VariationParameters& quic_trial_params) {
1356 return base::LowerCaseEqualsASCII(
1357 GetVariationParam(quic_trial_params, "enable_non_blocking_io"),
1358 "true");
1361 bool IOThread::ShouldQuicDisableDiskCache(
1362 const VariationParameters& quic_trial_params) {
1363 return base::LowerCaseEqualsASCII(
1364 GetVariationParam(quic_trial_params, "disable_disk_cache"), "true");
1367 bool IOThread::ShouldQuicPreferAes(
1368 const VariationParameters& quic_trial_params) {
1369 return base::LowerCaseEqualsASCII(
1370 GetVariationParam(quic_trial_params, "prefer_aes"), "true");
1373 bool IOThread::ShouldQuicEnableAlternativeServices(
1374 const base::CommandLine& command_line,
1375 const VariationParameters& quic_trial_params) {
1376 return command_line.HasSwitch(switches::kEnableAlternativeServices) ||
1377 base::LowerCaseEqualsASCII(
1378 GetVariationParam(quic_trial_params, "use_alternative_services"),
1379 "true");
1382 int IOThread::GetQuicMaxNumberOfLossyConnections(
1383 const VariationParameters& quic_trial_params) {
1384 int value;
1385 if (base::StringToInt(GetVariationParam(quic_trial_params,
1386 "max_number_of_lossy_connections"),
1387 &value)) {
1388 return value;
1390 return 0;
1393 float IOThread::GetQuicPacketLossThreshold(
1394 const VariationParameters& quic_trial_params) {
1395 double value;
1396 if (base::StringToDouble(GetVariationParam(quic_trial_params,
1397 "packet_loss_threshold"),
1398 &value)) {
1399 return static_cast<float>(value);
1401 return 0.0f;
1404 int IOThread::GetQuicSocketReceiveBufferSize(
1405 const VariationParameters& quic_trial_params) {
1406 int value;
1407 if (base::StringToInt(GetVariationParam(quic_trial_params,
1408 "receive_buffer_size"),
1409 &value)) {
1410 return value;
1412 return 0;
1415 bool IOThread::ShouldQuicDelayTcpRace(
1416 const VariationParameters& quic_trial_params) {
1417 return base::LowerCaseEqualsASCII(
1418 GetVariationParam(quic_trial_params, "delay_tcp_race"), "true");
1421 size_t IOThread::GetQuicMaxPacketLength(
1422 const base::CommandLine& command_line,
1423 const VariationParameters& quic_trial_params) {
1424 if (command_line.HasSwitch(switches::kQuicMaxPacketLength)) {
1425 unsigned value;
1426 if (!base::StringToUint(
1427 command_line.GetSwitchValueASCII(switches::kQuicMaxPacketLength),
1428 &value)) {
1429 return 0;
1431 return value;
1434 unsigned value;
1435 if (base::StringToUint(GetVariationParam(quic_trial_params,
1436 "max_packet_length"),
1437 &value)) {
1438 return value;
1440 return 0;
1443 net::QuicVersion IOThread::GetQuicVersion(
1444 const base::CommandLine& command_line,
1445 const VariationParameters& quic_trial_params) {
1446 if (command_line.HasSwitch(switches::kQuicVersion)) {
1447 return ParseQuicVersion(
1448 command_line.GetSwitchValueASCII(switches::kQuicVersion));
1451 return ParseQuicVersion(GetVariationParam(quic_trial_params, "quic_version"));
1454 net::QuicVersion IOThread::ParseQuicVersion(const std::string& quic_version) {
1455 net::QuicVersionVector supported_versions = net::QuicSupportedVersions();
1456 for (size_t i = 0; i < supported_versions.size(); ++i) {
1457 net::QuicVersion version = supported_versions[i];
1458 if (net::QuicVersionToString(version) == quic_version) {
1459 return version;
1463 return net::QUIC_VERSION_UNSUPPORTED;
1466 net::URLRequestContext* IOThread::ConstructSystemRequestContext(
1467 IOThread::Globals* globals,
1468 net::NetLog* net_log) {
1469 net::URLRequestContext* context = new SystemURLRequestContext;
1470 context->set_net_log(net_log);
1471 context->set_host_resolver(globals->host_resolver.get());
1472 context->set_cert_verifier(globals->cert_verifier.get());
1473 context->set_transport_security_state(
1474 globals->transport_security_state.get());
1475 context->set_cert_transparency_verifier(
1476 globals->cert_transparency_verifier.get());
1477 context->set_ssl_config_service(globals->ssl_config_service.get());
1478 context->set_http_auth_handler_factory(
1479 globals->http_auth_handler_factory.get());
1480 context->set_proxy_service(globals->system_proxy_service.get());
1482 globals->system_url_request_job_factory.reset(
1483 new net::URLRequestJobFactoryImpl());
1484 context->set_job_factory(globals->system_url_request_job_factory.get());
1486 context->set_cookie_store(globals->system_cookie_store.get());
1487 context->set_channel_id_service(
1488 globals->system_channel_id_service.get());
1489 context->set_network_delegate(globals->system_network_delegate.get());
1490 context->set_http_user_agent_settings(
1491 globals->http_user_agent_settings.get());
1492 context->set_network_quality_estimator(
1493 globals->network_quality_estimator.get());
1494 context->set_backoff_manager(globals->url_request_backoff_manager.get());
1496 context->set_http_server_properties(
1497 globals->http_server_properties->GetWeakPtr());
1499 net::HttpNetworkSession::Params system_params;
1500 InitializeNetworkSessionParamsFromGlobals(*globals, &system_params);
1501 net::URLRequestContextBuilder::SetHttpNetworkSessionComponents(
1502 context, &system_params);
1504 globals->system_http_transaction_factory.reset(
1505 new net::HttpNetworkLayer(new net::HttpNetworkSession(system_params)));
1506 context->set_http_transaction_factory(
1507 globals->system_http_transaction_factory.get());
1509 return context;
1512 net::URLRequestContext* IOThread::ConstructProxyScriptFetcherContext(
1513 IOThread::Globals* globals,
1514 net::NetLog* net_log) {
1515 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
1516 // is fixed.
1517 tracked_objects::ScopedTracker tracking_profile1(
1518 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1519 "466432 IOThread::ConstructProxyScriptFetcherContext1"));
1520 net::URLRequestContext* context = new net::URLRequestContext;
1521 context->set_net_log(net_log);
1522 context->set_host_resolver(globals->host_resolver.get());
1523 context->set_cert_verifier(globals->cert_verifier.get());
1524 context->set_transport_security_state(
1525 globals->transport_security_state.get());
1526 context->set_cert_transparency_verifier(
1527 globals->cert_transparency_verifier.get());
1528 context->set_ssl_config_service(globals->ssl_config_service.get());
1529 context->set_http_auth_handler_factory(
1530 globals->http_auth_handler_factory.get());
1531 context->set_proxy_service(globals->proxy_script_fetcher_proxy_service.get());
1533 context->set_job_factory(
1534 globals->proxy_script_fetcher_url_request_job_factory.get());
1536 context->set_cookie_store(globals->system_cookie_store.get());
1537 context->set_channel_id_service(
1538 globals->system_channel_id_service.get());
1539 context->set_network_delegate(globals->system_network_delegate.get());
1540 context->set_http_user_agent_settings(
1541 globals->http_user_agent_settings.get());
1542 context->set_http_server_properties(
1543 globals->http_server_properties->GetWeakPtr());
1545 net::HttpNetworkSession::Params session_params;
1546 InitializeNetworkSessionParamsFromGlobals(*globals, &session_params);
1547 net::URLRequestContextBuilder::SetHttpNetworkSessionComponents(
1548 context, &session_params);
1550 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
1551 // is fixed.
1552 tracked_objects::ScopedTracker tracking_profile2(
1553 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1554 "466432 IOThread::ConstructProxyScriptFetcherContext2"));
1555 scoped_refptr<net::HttpNetworkSession> network_session(
1556 new net::HttpNetworkSession(session_params));
1557 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
1558 // is fixed.
1559 tracked_objects::ScopedTracker tracking_profile3(
1560 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1561 "466432 IOThread::ConstructProxyScriptFetcherContext3"));
1562 globals->proxy_script_fetcher_http_transaction_factory
1563 .reset(new net::HttpNetworkLayer(network_session.get()));
1564 context->set_http_transaction_factory(
1565 globals->proxy_script_fetcher_http_transaction_factory.get());
1567 scoped_ptr<net::URLRequestJobFactoryImpl> job_factory(
1568 new net::URLRequestJobFactoryImpl());
1570 job_factory->SetProtocolHandler(
1571 url::kDataScheme, make_scoped_ptr(new net::DataProtocolHandler()));
1572 job_factory->SetProtocolHandler(
1573 url::kFileScheme,
1574 make_scoped_ptr(new net::FileProtocolHandler(
1575 content::BrowserThread::GetBlockingPool()
1576 ->GetTaskRunnerWithShutdownBehavior(
1577 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN))));
1578 #if !defined(DISABLE_FTP_SUPPORT)
1579 globals->proxy_script_fetcher_ftp_transaction_factory.reset(
1580 new net::FtpNetworkLayer(globals->host_resolver.get()));
1581 job_factory->SetProtocolHandler(
1582 url::kFtpScheme,
1583 make_scoped_ptr(new net::FtpProtocolHandler(
1584 globals->proxy_script_fetcher_ftp_transaction_factory.get())));
1585 #endif
1586 globals->proxy_script_fetcher_url_request_job_factory = job_factory.Pass();
1588 context->set_job_factory(
1589 globals->proxy_script_fetcher_url_request_job_factory.get());
1591 // TODO(rtenneti): We should probably use HttpServerPropertiesManager for the
1592 // system URLRequestContext too. There's no reason this should be tied to a
1593 // profile.
1594 return context;