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