ExtensionSyncService: listen for relevant changes instead of being explicitly called...
[chromium-blink-merge.git] / chrome / browser / io_thread.cc
blob72e4af24879ec5b9574167d51511379a6004e28c
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/channel_info.h"
40 #include "chrome/common/chrome_content_client.h"
41 #include "chrome/common/chrome_switches.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 "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(BrowserThread::CurrentlyOn(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(BrowserThread::CurrentlyOn(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 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(BrowserThread::CurrentlyOn(BrowserThread::IO));
503 return globals_;
506 void IOThread::SetGlobalsForTesting(Globals* globals) {
507 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
508 DCHECK(!globals || !globals_);
509 globals_ = globals;
512 ChromeNetLog* IOThread::net_log() {
513 return net_log_;
516 void IOThread::ChangedToOnTheRecord() {
517 DCHECK(BrowserThread::CurrentlyOn(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(BrowserThread::CurrentlyOn(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(BrowserThread::CurrentlyOn(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.reset(
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 globals->use_alternative_services.set(true);
897 return;
899 if (spdy_trial_group.starts_with(kSpdyFieldTrialSpdy4GroupNamePrefix)) {
900 globals->next_protos.push_back(net::kProtoSPDY31);
901 globals->next_protos.push_back(net::kProtoHTTP2_14);
902 globals->next_protos.push_back(net::kProtoHTTP2);
903 globals->use_alternative_services.set(true);
904 return;
906 if (spdy_trial_group.starts_with(kSpdyFieldTrialParametrizedPrefix)) {
907 bool spdy_enabled = false;
908 if (base::LowerCaseEqualsASCII(
909 GetVariationParam(spdy_trial_params, "enable_spdy31"), "true")) {
910 globals->next_protos.push_back(net::kProtoSPDY31);
911 spdy_enabled = true;
913 if (base::LowerCaseEqualsASCII(
914 GetVariationParam(spdy_trial_params, "enable_http2_14"), "true")) {
915 globals->next_protos.push_back(net::kProtoHTTP2_14);
916 spdy_enabled = true;
918 if (base::LowerCaseEqualsASCII(
919 GetVariationParam(spdy_trial_params, "enable_http2"), "true")) {
920 globals->next_protos.push_back(net::kProtoHTTP2);
921 spdy_enabled = true;
923 // TODO(bnc): HttpStreamFactory::spdy_enabled_ is redundant with
924 // globals->next_protos, can it be eliminated?
925 net::HttpStreamFactory::set_spdy_enabled(spdy_enabled);
926 globals->use_alternative_services.set(true);
927 return;
930 // By default, enable HTTP/2.
931 globals->next_protos.push_back(net::kProtoSPDY31);
932 globals->next_protos.push_back(net::kProtoHTTP2_14);
933 globals->next_protos.push_back(net::kProtoHTTP2);
934 globals->use_alternative_services.set(true);
937 void IOThread::RegisterPrefs(PrefRegistrySimple* registry) {
938 registry->RegisterStringPref(prefs::kAuthSchemes,
939 "basic,digest,ntlm,negotiate");
940 registry->RegisterBooleanPref(prefs::kDisableAuthNegotiateCnameLookup, false);
941 registry->RegisterBooleanPref(prefs::kEnableAuthNegotiatePort, false);
942 registry->RegisterStringPref(prefs::kAuthServerWhitelist, std::string());
943 registry->RegisterStringPref(prefs::kAuthNegotiateDelegateWhitelist,
944 std::string());
945 registry->RegisterStringPref(prefs::kGSSAPILibraryName, std::string());
946 registry->RegisterStringPref(prefs::kAuthAndroidNegotiateAccountType,
947 std::string());
948 registry->RegisterStringPref(
949 data_reduction_proxy::prefs::kDataReductionProxy, std::string());
950 registry->RegisterBooleanPref(prefs::kEnableReferrers, true);
951 data_reduction_proxy::RegisterPrefs(registry);
952 registry->RegisterBooleanPref(prefs::kBuiltInDnsClientEnabled, true);
953 registry->RegisterBooleanPref(prefs::kQuickCheckEnabled, true);
956 net::HttpAuthHandlerFactory* IOThread::CreateDefaultAuthHandlerFactory(
957 net::HostResolver* resolver) {
958 net::HttpAuthFilterWhitelist* auth_filter_default_credentials = NULL;
959 if (!auth_server_whitelist_.empty()) {
960 auth_filter_default_credentials =
961 new net::HttpAuthFilterWhitelist(auth_server_whitelist_);
963 net::HttpAuthFilterWhitelist* auth_filter_delegate = NULL;
964 if (!auth_delegate_whitelist_.empty()) {
965 auth_filter_delegate =
966 new net::HttpAuthFilterWhitelist(auth_delegate_whitelist_);
968 globals_->url_security_manager.reset(
969 net::URLSecurityManager::Create(auth_filter_default_credentials,
970 auth_filter_delegate));
971 std::vector<std::string> supported_schemes = base::SplitString(
972 auth_schemes_, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
974 scoped_ptr<net::HttpAuthHandlerRegistryFactory> registry_factory(
975 net::HttpAuthHandlerRegistryFactory::Create(
976 supported_schemes, globals_->url_security_manager.get(), resolver,
977 gssapi_library_name_, auth_android_negotiate_account_type_,
978 negotiate_disable_cname_lookup_, negotiate_enable_port_));
979 return registry_factory.release();
982 void IOThread::ClearHostCache() {
983 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
985 net::HostCache* host_cache = globals_->host_resolver->GetHostCache();
986 if (host_cache)
987 host_cache->clear();
990 void IOThread::InitializeNetworkSessionParams(
991 net::HttpNetworkSession::Params* params) {
992 InitializeNetworkSessionParamsFromGlobals(*globals_, params);
995 void IOThread::InitializeNetworkSessionParamsFromGlobals(
996 const IOThread::Globals& globals,
997 net::HttpNetworkSession::Params* params) {
998 // The next two properties of the params don't seem to be
999 // elements of URLRequestContext, so they must be set here.
1000 params->cert_policy_enforcer = globals.cert_policy_enforcer.get();
1001 params->host_mapping_rules = globals.host_mapping_rules.get();
1003 params->ignore_certificate_errors = globals.ignore_certificate_errors;
1004 params->testing_fixed_http_port = globals.testing_fixed_http_port;
1005 params->testing_fixed_https_port = globals.testing_fixed_https_port;
1006 globals.enable_tcp_fast_open_for_ssl.CopyToIfSet(
1007 &params->enable_tcp_fast_open_for_ssl);
1009 globals.initial_max_spdy_concurrent_streams.CopyToIfSet(
1010 &params->spdy_initial_max_concurrent_streams);
1011 globals.enable_spdy_compression.CopyToIfSet(
1012 &params->enable_spdy_compression);
1013 globals.enable_spdy_ping_based_connection_checking.CopyToIfSet(
1014 &params->enable_spdy_ping_based_connection_checking);
1015 globals.spdy_default_protocol.CopyToIfSet(
1016 &params->spdy_default_protocol);
1017 params->next_protos = globals.next_protos;
1018 globals.trusted_spdy_proxy.CopyToIfSet(&params->trusted_spdy_proxy);
1019 params->forced_spdy_exclusions = globals.forced_spdy_exclusions;
1020 globals.use_alternative_services.CopyToIfSet(
1021 &params->use_alternative_services);
1022 globals.alternative_service_probability_threshold.CopyToIfSet(
1023 &params->alternative_service_probability_threshold);
1025 globals.enable_quic.CopyToIfSet(&params->enable_quic);
1026 globals.enable_insecure_quic.CopyToIfSet(&params->enable_insecure_quic);
1027 globals.enable_quic_for_proxies.CopyToIfSet(&params->enable_quic_for_proxies);
1028 globals.quic_always_require_handshake_confirmation.CopyToIfSet(
1029 &params->quic_always_require_handshake_confirmation);
1030 globals.quic_disable_connection_pooling.CopyToIfSet(
1031 &params->quic_disable_connection_pooling);
1032 globals.quic_load_server_info_timeout_srtt_multiplier.CopyToIfSet(
1033 &params->quic_load_server_info_timeout_srtt_multiplier);
1034 globals.quic_enable_connection_racing.CopyToIfSet(
1035 &params->quic_enable_connection_racing);
1036 globals.quic_enable_non_blocking_io.CopyToIfSet(
1037 &params->quic_enable_non_blocking_io);
1038 globals.quic_prefer_aes.CopyToIfSet(&params->quic_prefer_aes);
1039 globals.quic_disable_disk_cache.CopyToIfSet(
1040 &params->quic_disable_disk_cache);
1041 globals.quic_max_number_of_lossy_connections.CopyToIfSet(
1042 &params->quic_max_number_of_lossy_connections);
1043 globals.quic_packet_loss_threshold.CopyToIfSet(
1044 &params->quic_packet_loss_threshold);
1045 globals.quic_socket_receive_buffer_size.CopyToIfSet(
1046 &params->quic_socket_receive_buffer_size);
1047 globals.enable_quic_port_selection.CopyToIfSet(
1048 &params->enable_quic_port_selection);
1049 globals.quic_max_packet_length.CopyToIfSet(&params->quic_max_packet_length);
1050 globals.quic_user_agent_id.CopyToIfSet(&params->quic_user_agent_id);
1051 globals.quic_supported_versions.CopyToIfSet(
1052 &params->quic_supported_versions);
1053 params->quic_connection_options = globals.quic_connection_options;
1055 globals.origin_to_force_quic_on.CopyToIfSet(
1056 &params->origin_to_force_quic_on);
1057 params->enable_user_alternate_protocol_ports =
1058 globals.enable_user_alternate_protocol_ports;
1061 base::TimeTicks IOThread::creation_time() const {
1062 return creation_time_;
1065 net::SSLConfigService* IOThread::GetSSLConfigService() {
1066 return ssl_config_service_manager_->Get();
1069 void IOThread::ChangedToOnTheRecordOnIOThread() {
1070 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
1072 // Clear the host cache to avoid showing entries from the OTR session
1073 // in about:net-internals.
1074 ClearHostCache();
1077 void IOThread::InitSystemRequestContext() {
1078 if (system_url_request_context_getter_.get())
1079 return;
1080 // If we're in unit_tests, IOThread may not be run.
1081 if (!BrowserThread::IsMessageLoopValid(BrowserThread::IO))
1082 return;
1083 system_proxy_config_service_.reset(
1084 ProxyServiceFactory::CreateProxyConfigService(
1085 pref_proxy_config_tracker_.get()));
1086 system_url_request_context_getter_ =
1087 new SystemURLRequestContextGetter(this);
1088 // Safe to post an unretained this pointer, since IOThread is
1089 // guaranteed to outlive the IO BrowserThread.
1090 BrowserThread::PostTask(
1091 BrowserThread::IO,
1092 FROM_HERE,
1093 base::Bind(&IOThread::InitSystemRequestContextOnIOThread,
1094 base::Unretained(this)));
1097 void IOThread::InitSystemRequestContextOnIOThread() {
1098 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
1099 DCHECK(!globals_->system_proxy_service.get());
1100 DCHECK(system_proxy_config_service_.get());
1102 const base::CommandLine& command_line =
1103 *base::CommandLine::ForCurrentProcess();
1104 globals_->system_proxy_service.reset(
1105 ProxyServiceFactory::CreateProxyService(
1106 net_log_,
1107 globals_->proxy_script_fetcher_context.get(),
1108 globals_->system_network_delegate.get(),
1109 system_proxy_config_service_.release(),
1110 command_line,
1111 quick_check_enabled_.GetValue()));
1113 globals_->system_request_context.reset(
1114 ConstructSystemRequestContext(globals_, net_log_));
1117 void IOThread::UpdateDnsClientEnabled() {
1118 globals()->host_resolver->SetDnsClientEnabled(*dns_client_enabled_);
1121 void IOThread::ConfigureQuic(const base::CommandLine& command_line) {
1122 // Always fetch the field trial group to ensure it is reported correctly.
1123 // The command line flags will be associated with a group that is reported
1124 // so long as trial is actually queried.
1125 std::string group =
1126 base::FieldTrialList::FindFullName(kQuicFieldTrialName);
1127 VariationParameters params;
1128 if (!variations::GetVariationParams(kQuicFieldTrialName, &params)) {
1129 params.clear();
1132 ConfigureQuicGlobals(command_line, group, params, is_quic_allowed_by_policy_,
1133 globals_);
1136 void IOThread::ConfigureQuicGlobals(
1137 const base::CommandLine& command_line,
1138 base::StringPiece quic_trial_group,
1139 const VariationParameters& quic_trial_params,
1140 bool quic_allowed_by_policy,
1141 IOThread::Globals* globals) {
1142 bool enable_quic = ShouldEnableQuic(command_line, quic_trial_group,
1143 quic_allowed_by_policy);
1144 globals->enable_quic.set(enable_quic);
1145 bool enable_quic_for_proxies = ShouldEnableQuicForProxies(
1146 command_line, quic_trial_group, quic_allowed_by_policy);
1147 globals->enable_quic_for_proxies.set(enable_quic_for_proxies);
1148 if (enable_quic) {
1149 globals->enable_insecure_quic.set(
1150 ShouldEnableInsecureQuic(command_line, quic_trial_params));
1151 globals->quic_always_require_handshake_confirmation.set(
1152 ShouldQuicAlwaysRequireHandshakeConfirmation(quic_trial_params));
1153 globals->quic_disable_connection_pooling.set(
1154 ShouldQuicDisableConnectionPooling(quic_trial_params));
1155 int receive_buffer_size = GetQuicSocketReceiveBufferSize(quic_trial_params);
1156 if (receive_buffer_size != 0) {
1157 globals->quic_socket_receive_buffer_size.set(receive_buffer_size);
1159 float load_server_info_timeout_srtt_multiplier =
1160 GetQuicLoadServerInfoTimeoutSrttMultiplier(quic_trial_params);
1161 if (load_server_info_timeout_srtt_multiplier != 0) {
1162 globals->quic_load_server_info_timeout_srtt_multiplier.set(
1163 load_server_info_timeout_srtt_multiplier);
1165 globals->quic_enable_connection_racing.set(
1166 ShouldQuicEnableConnectionRacing(quic_trial_params));
1167 globals->quic_enable_non_blocking_io.set(
1168 ShouldQuicEnableNonBlockingIO(quic_trial_params));
1169 globals->quic_disable_disk_cache.set(
1170 ShouldQuicDisableDiskCache(quic_trial_params));
1171 globals->quic_prefer_aes.set(
1172 ShouldQuicPreferAes(quic_trial_params));
1173 int max_number_of_lossy_connections = GetQuicMaxNumberOfLossyConnections(
1174 quic_trial_params);
1175 if (max_number_of_lossy_connections != 0) {
1176 globals->quic_max_number_of_lossy_connections.set(
1177 max_number_of_lossy_connections);
1179 float packet_loss_threshold = GetQuicPacketLossThreshold(quic_trial_params);
1180 if (packet_loss_threshold != 0)
1181 globals->quic_packet_loss_threshold.set(packet_loss_threshold);
1182 globals->enable_quic_port_selection.set(
1183 ShouldEnableQuicPortSelection(command_line));
1184 globals->quic_connection_options =
1185 GetQuicConnectionOptions(command_line, quic_trial_params);
1188 size_t max_packet_length = GetQuicMaxPacketLength(command_line,
1189 quic_trial_params);
1190 if (max_packet_length != 0) {
1191 globals->quic_max_packet_length.set(max_packet_length);
1194 std::string quic_user_agent_id = chrome::GetChannelString();
1195 if (!quic_user_agent_id.empty())
1196 quic_user_agent_id.push_back(' ');
1197 quic_user_agent_id.append(
1198 version_info::GetProductNameAndVersionForUserAgent());
1199 quic_user_agent_id.push_back(' ');
1200 quic_user_agent_id.append(content::BuildOSCpuInfo());
1201 globals->quic_user_agent_id.set(quic_user_agent_id);
1203 net::QuicVersion version = GetQuicVersion(command_line, quic_trial_params);
1204 if (version != net::QUIC_VERSION_UNSUPPORTED) {
1205 net::QuicVersionVector supported_versions;
1206 supported_versions.push_back(version);
1207 globals->quic_supported_versions.set(supported_versions);
1210 double threshold = GetAlternativeProtocolProbabilityThreshold(
1211 command_line, quic_trial_params);
1212 if (threshold >=0 && threshold <= 1) {
1213 globals->alternative_service_probability_threshold.set(threshold);
1214 globals->http_server_properties->SetAlternativeServiceProbabilityThreshold(
1215 threshold);
1218 if (command_line.HasSwitch(switches::kOriginToForceQuicOn)) {
1219 net::HostPortPair quic_origin =
1220 net::HostPortPair::FromString(
1221 command_line.GetSwitchValueASCII(switches::kOriginToForceQuicOn));
1222 if (!quic_origin.IsEmpty()) {
1223 globals->origin_to_force_quic_on.set(quic_origin);
1228 bool IOThread::ShouldEnableQuic(const base::CommandLine& command_line,
1229 base::StringPiece quic_trial_group,
1230 bool quic_allowed_by_policy) {
1231 if (command_line.HasSwitch(switches::kDisableQuic) || !quic_allowed_by_policy)
1232 return false;
1234 if (command_line.HasSwitch(switches::kEnableQuic))
1235 return true;
1237 return quic_trial_group.starts_with(kQuicFieldTrialEnabledGroupName) ||
1238 quic_trial_group.starts_with(kQuicFieldTrialHttpsEnabledGroupName);
1241 bool IOThread::ShouldEnableQuicForProxies(const base::CommandLine& command_line,
1242 base::StringPiece quic_trial_group,
1243 bool quic_allowed_by_policy) {
1244 return ShouldEnableQuic(
1245 command_line, quic_trial_group, quic_allowed_by_policy) ||
1246 ShouldEnableQuicForDataReductionProxy();
1249 bool IOThread::ShouldEnableQuicForDataReductionProxy() {
1250 const base::CommandLine& command_line =
1251 *base::CommandLine::ForCurrentProcess();
1253 if (command_line.HasSwitch(switches::kDisableQuic))
1254 return false;
1256 return data_reduction_proxy::params::IsIncludedInQuicFieldTrial();
1259 bool IOThread::ShouldEnableInsecureQuic(
1260 const base::CommandLine& command_line,
1261 const VariationParameters& quic_trial_params) {
1262 if (command_line.HasSwitch(switches::kEnableInsecureQuic))
1263 return true;
1265 return base::LowerCaseEqualsASCII(
1266 GetVariationParam(quic_trial_params, "enable_insecure_quic"),
1267 "true");
1270 bool IOThread::ShouldEnableQuicPortSelection(
1271 const base::CommandLine& command_line) {
1272 if (command_line.HasSwitch(switches::kDisableQuicPortSelection))
1273 return false;
1275 if (command_line.HasSwitch(switches::kEnableQuicPortSelection))
1276 return true;
1278 return false; // Default to disabling port selection on all channels.
1281 net::QuicTagVector IOThread::GetQuicConnectionOptions(
1282 const base::CommandLine& command_line,
1283 const VariationParameters& quic_trial_params) {
1284 if (command_line.HasSwitch(switches::kQuicConnectionOptions)) {
1285 return net::QuicUtils::ParseQuicConnectionOptions(
1286 command_line.GetSwitchValueASCII(switches::kQuicConnectionOptions));
1289 VariationParameters::const_iterator it =
1290 quic_trial_params.find("connection_options");
1291 if (it == quic_trial_params.end()) {
1292 return net::QuicTagVector();
1295 return net::QuicUtils::ParseQuicConnectionOptions(it->second);
1298 double IOThread::GetAlternativeProtocolProbabilityThreshold(
1299 const base::CommandLine& command_line,
1300 const VariationParameters& quic_trial_params) {
1301 double value;
1302 if (command_line.HasSwitch(
1303 switches::kAlternativeServiceProbabilityThreshold)) {
1304 if (base::StringToDouble(
1305 command_line.GetSwitchValueASCII(
1306 switches::kAlternativeServiceProbabilityThreshold),
1307 &value)) {
1308 return value;
1311 if (command_line.HasSwitch(switches::kEnableQuic)) {
1312 return 0;
1314 // TODO(bnc): Remove when new parameter name rolls out and server
1315 // configuration is changed.
1316 if (base::StringToDouble(
1317 GetVariationParam(quic_trial_params,
1318 "alternate_protocol_probability_threshold"),
1319 &value)) {
1320 return value;
1322 if (base::StringToDouble(
1323 GetVariationParam(quic_trial_params,
1324 "alternative_service_probability_threshold"),
1325 &value)) {
1326 return value;
1328 return -1;
1331 bool IOThread::ShouldQuicAlwaysRequireHandshakeConfirmation(
1332 const VariationParameters& quic_trial_params) {
1333 return base::LowerCaseEqualsASCII(
1334 GetVariationParam(quic_trial_params,
1335 "always_require_handshake_confirmation"),
1336 "true");
1339 bool IOThread::ShouldQuicDisableConnectionPooling(
1340 const VariationParameters& quic_trial_params) {
1341 return base::LowerCaseEqualsASCII(
1342 GetVariationParam(quic_trial_params, "disable_connection_pooling"),
1343 "true");
1346 float IOThread::GetQuicLoadServerInfoTimeoutSrttMultiplier(
1347 const VariationParameters& quic_trial_params) {
1348 double value;
1349 if (base::StringToDouble(GetVariationParam(quic_trial_params,
1350 "load_server_info_time_to_srtt"),
1351 &value)) {
1352 return static_cast<float>(value);
1354 return 0.0f;
1357 bool IOThread::ShouldQuicEnableConnectionRacing(
1358 const VariationParameters& quic_trial_params) {
1359 return base::LowerCaseEqualsASCII(
1360 GetVariationParam(quic_trial_params, "enable_connection_racing"),
1361 "true");
1364 bool IOThread::ShouldQuicEnableNonBlockingIO(
1365 const VariationParameters& quic_trial_params) {
1366 return base::LowerCaseEqualsASCII(
1367 GetVariationParam(quic_trial_params, "enable_non_blocking_io"),
1368 "true");
1371 bool IOThread::ShouldQuicDisableDiskCache(
1372 const VariationParameters& quic_trial_params) {
1373 return base::LowerCaseEqualsASCII(
1374 GetVariationParam(quic_trial_params, "disable_disk_cache"), "true");
1377 bool IOThread::ShouldQuicPreferAes(
1378 const VariationParameters& quic_trial_params) {
1379 return base::LowerCaseEqualsASCII(
1380 GetVariationParam(quic_trial_params, "prefer_aes"), "true");
1383 int IOThread::GetQuicMaxNumberOfLossyConnections(
1384 const VariationParameters& quic_trial_params) {
1385 int value;
1386 if (base::StringToInt(GetVariationParam(quic_trial_params,
1387 "max_number_of_lossy_connections"),
1388 &value)) {
1389 return value;
1391 return 0;
1394 float IOThread::GetQuicPacketLossThreshold(
1395 const VariationParameters& quic_trial_params) {
1396 double value;
1397 if (base::StringToDouble(GetVariationParam(quic_trial_params,
1398 "packet_loss_threshold"),
1399 &value)) {
1400 return static_cast<float>(value);
1402 return 0.0f;
1405 int IOThread::GetQuicSocketReceiveBufferSize(
1406 const VariationParameters& quic_trial_params) {
1407 int value;
1408 if (base::StringToInt(GetVariationParam(quic_trial_params,
1409 "receive_buffer_size"),
1410 &value)) {
1411 return value;
1413 return 0;
1416 size_t IOThread::GetQuicMaxPacketLength(
1417 const base::CommandLine& command_line,
1418 const VariationParameters& quic_trial_params) {
1419 if (command_line.HasSwitch(switches::kQuicMaxPacketLength)) {
1420 unsigned value;
1421 if (!base::StringToUint(
1422 command_line.GetSwitchValueASCII(switches::kQuicMaxPacketLength),
1423 &value)) {
1424 return 0;
1426 return value;
1429 unsigned value;
1430 if (base::StringToUint(GetVariationParam(quic_trial_params,
1431 "max_packet_length"),
1432 &value)) {
1433 return value;
1435 return 0;
1438 net::QuicVersion IOThread::GetQuicVersion(
1439 const base::CommandLine& command_line,
1440 const VariationParameters& quic_trial_params) {
1441 if (command_line.HasSwitch(switches::kQuicVersion)) {
1442 return ParseQuicVersion(
1443 command_line.GetSwitchValueASCII(switches::kQuicVersion));
1446 return ParseQuicVersion(GetVariationParam(quic_trial_params, "quic_version"));
1449 net::QuicVersion IOThread::ParseQuicVersion(const std::string& quic_version) {
1450 net::QuicVersionVector supported_versions = net::QuicSupportedVersions();
1451 for (size_t i = 0; i < supported_versions.size(); ++i) {
1452 net::QuicVersion version = supported_versions[i];
1453 if (net::QuicVersionToString(version) == quic_version) {
1454 return version;
1458 return net::QUIC_VERSION_UNSUPPORTED;
1461 net::URLRequestContext* IOThread::ConstructSystemRequestContext(
1462 IOThread::Globals* globals,
1463 net::NetLog* net_log) {
1464 net::URLRequestContext* context = new SystemURLRequestContext;
1465 context->set_net_log(net_log);
1466 context->set_host_resolver(globals->host_resolver.get());
1467 context->set_cert_verifier(globals->cert_verifier.get());
1468 context->set_transport_security_state(
1469 globals->transport_security_state.get());
1470 context->set_cert_transparency_verifier(
1471 globals->cert_transparency_verifier.get());
1472 context->set_ssl_config_service(globals->ssl_config_service.get());
1473 context->set_http_auth_handler_factory(
1474 globals->http_auth_handler_factory.get());
1475 context->set_proxy_service(globals->system_proxy_service.get());
1477 globals->system_url_request_job_factory.reset(
1478 new net::URLRequestJobFactoryImpl());
1479 context->set_job_factory(globals->system_url_request_job_factory.get());
1481 context->set_cookie_store(globals->system_cookie_store.get());
1482 context->set_channel_id_service(
1483 globals->system_channel_id_service.get());
1484 context->set_network_delegate(globals->system_network_delegate.get());
1485 context->set_http_user_agent_settings(
1486 globals->http_user_agent_settings.get());
1487 context->set_network_quality_estimator(
1488 globals->network_quality_estimator.get());
1489 context->set_backoff_manager(globals->url_request_backoff_manager.get());
1491 context->set_http_server_properties(
1492 globals->http_server_properties->GetWeakPtr());
1494 net::HttpNetworkSession::Params system_params;
1495 InitializeNetworkSessionParamsFromGlobals(*globals, &system_params);
1496 net::URLRequestContextBuilder::SetHttpNetworkSessionComponents(
1497 context, &system_params);
1499 globals->system_http_transaction_factory.reset(
1500 new net::HttpNetworkLayer(new net::HttpNetworkSession(system_params)));
1501 context->set_http_transaction_factory(
1502 globals->system_http_transaction_factory.get());
1504 return context;
1507 net::URLRequestContext* IOThread::ConstructProxyScriptFetcherContext(
1508 IOThread::Globals* globals,
1509 net::NetLog* net_log) {
1510 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
1511 // is fixed.
1512 tracked_objects::ScopedTracker tracking_profile1(
1513 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1514 "466432 IOThread::ConstructProxyScriptFetcherContext1"));
1515 net::URLRequestContext* context = new net::URLRequestContext;
1516 context->set_net_log(net_log);
1517 context->set_host_resolver(globals->host_resolver.get());
1518 context->set_cert_verifier(globals->cert_verifier.get());
1519 context->set_transport_security_state(
1520 globals->transport_security_state.get());
1521 context->set_cert_transparency_verifier(
1522 globals->cert_transparency_verifier.get());
1523 context->set_ssl_config_service(globals->ssl_config_service.get());
1524 context->set_http_auth_handler_factory(
1525 globals->http_auth_handler_factory.get());
1526 context->set_proxy_service(globals->proxy_script_fetcher_proxy_service.get());
1528 context->set_job_factory(
1529 globals->proxy_script_fetcher_url_request_job_factory.get());
1531 context->set_cookie_store(globals->system_cookie_store.get());
1532 context->set_channel_id_service(
1533 globals->system_channel_id_service.get());
1534 context->set_network_delegate(globals->system_network_delegate.get());
1535 context->set_http_user_agent_settings(
1536 globals->http_user_agent_settings.get());
1537 context->set_http_server_properties(
1538 globals->http_server_properties->GetWeakPtr());
1540 net::HttpNetworkSession::Params session_params;
1541 InitializeNetworkSessionParamsFromGlobals(*globals, &session_params);
1542 net::URLRequestContextBuilder::SetHttpNetworkSessionComponents(
1543 context, &session_params);
1545 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
1546 // is fixed.
1547 tracked_objects::ScopedTracker tracking_profile2(
1548 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1549 "466432 IOThread::ConstructProxyScriptFetcherContext2"));
1550 scoped_refptr<net::HttpNetworkSession> network_session(
1551 new net::HttpNetworkSession(session_params));
1552 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
1553 // is fixed.
1554 tracked_objects::ScopedTracker tracking_profile3(
1555 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1556 "466432 IOThread::ConstructProxyScriptFetcherContext3"));
1557 globals->proxy_script_fetcher_http_transaction_factory
1558 .reset(new net::HttpNetworkLayer(network_session.get()));
1559 context->set_http_transaction_factory(
1560 globals->proxy_script_fetcher_http_transaction_factory.get());
1562 scoped_ptr<net::URLRequestJobFactoryImpl> job_factory(
1563 new net::URLRequestJobFactoryImpl());
1565 job_factory->SetProtocolHandler(
1566 url::kDataScheme, make_scoped_ptr(new net::DataProtocolHandler()));
1567 job_factory->SetProtocolHandler(
1568 url::kFileScheme,
1569 make_scoped_ptr(new net::FileProtocolHandler(
1570 content::BrowserThread::GetBlockingPool()
1571 ->GetTaskRunnerWithShutdownBehavior(
1572 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN))));
1573 #if !defined(DISABLE_FTP_SUPPORT)
1574 globals->proxy_script_fetcher_ftp_transaction_factory.reset(
1575 new net::FtpNetworkLayer(globals->host_resolver.get()));
1576 job_factory->SetProtocolHandler(
1577 url::kFtpScheme,
1578 make_scoped_ptr(new net::FtpProtocolHandler(
1579 globals->proxy_script_fetcher_ftp_transaction_factory.get())));
1580 #endif
1581 globals->proxy_script_fetcher_url_request_job_factory = job_factory.Pass();
1583 context->set_job_factory(
1584 globals->proxy_script_fetcher_url_request_job_factory.get());
1586 // TODO(rtenneti): We should probably use HttpServerPropertiesManager for the
1587 // system URLRequestContext too. There's no reason this should be tied to a
1588 // profile.
1589 return context;