[ExtensionToolbarMac] Restrict action button drags to the container's bounds
[chromium-blink-merge.git] / chrome / browser / io_thread.cc
blob32bc8146a03b27c5abe59108e7426992fa7c619e
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/io_thread.h"
7 #include <vector>
9 #include "base/base64.h"
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/command_line.h"
13 #include "base/compiler_specific.h"
14 #include "base/debug/leak_tracker.h"
15 #include "base/debug/trace_event.h"
16 #include "base/logging.h"
17 #include "base/metrics/field_trial.h"
18 #include "base/prefs/pref_registry_simple.h"
19 #include "base/prefs/pref_service.h"
20 #include "base/stl_util.h"
21 #include "base/strings/string_number_conversions.h"
22 #include "base/strings/string_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 "build/build_config.h"
30 #include "chrome/browser/browser_process.h"
31 #include "chrome/browser/net/async_dns_field_trial.h"
32 #include "chrome/browser/net/chrome_net_log.h"
33 #include "chrome/browser/net/chrome_network_delegate.h"
34 #include "chrome/browser/net/connect_interceptor.h"
35 #include "chrome/browser/net/dns_probe_service.h"
36 #include "chrome/browser/net/pref_proxy_config_tracker.h"
37 #include "chrome/browser/net/proxy_service_factory.h"
38 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h"
39 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_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_auth_request_handler.h"
45 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_delegate.h"
46 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_prefs.h"
47 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_protocol.h"
48 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h"
49 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h"
50 #include "components/policy/core/common/policy_service.h"
51 #include "components/variations/variations_associated_data.h"
52 #include "content/public/browser/browser_thread.h"
53 #include "content/public/browser/cookie_store_factory.h"
54 #include "net/base/host_mapping_rules.h"
55 #include "net/base/net_util.h"
56 #include "net/cert/cert_verifier.h"
57 #include "net/cert/cert_verify_proc.h"
58 #include "net/cert/ct_known_logs.h"
59 #include "net/cert/ct_log_verifier.h"
60 #include "net/cert/ct_verifier.h"
61 #include "net/cert/multi_log_ct_verifier.h"
62 #include "net/cert/multi_threaded_cert_verifier.h"
63 #include "net/cookies/cookie_store.h"
64 #include "net/dns/host_cache.h"
65 #include "net/dns/host_resolver.h"
66 #include "net/dns/mapped_host_resolver.h"
67 #include "net/ftp/ftp_network_layer.h"
68 #include "net/http/http_auth_filter.h"
69 #include "net/http/http_auth_handler_factory.h"
70 #include "net/http/http_network_layer.h"
71 #include "net/http/http_server_properties_impl.h"
72 #include "net/proxy/proxy_config_service.h"
73 #include "net/proxy/proxy_script_fetcher_impl.h"
74 #include "net/proxy/proxy_service.h"
75 #include "net/quic/crypto/crypto_protocol.h"
76 #include "net/quic/quic_protocol.h"
77 #include "net/quic/quic_utils.h"
78 #include "net/socket/tcp_client_socket.h"
79 #include "net/spdy/spdy_session.h"
80 #include "net/ssl/channel_id_service.h"
81 #include "net/ssl/default_channel_id_store.h"
82 #include "net/url_request/data_protocol_handler.h"
83 #include "net/url_request/file_protocol_handler.h"
84 #include "net/url_request/ftp_protocol_handler.h"
85 #include "net/url_request/static_http_user_agent_settings.h"
86 #include "net/url_request/url_fetcher.h"
87 #include "net/url_request/url_request_context.h"
88 #include "net/url_request/url_request_context_getter.h"
89 #include "net/url_request/url_request_job_factory_impl.h"
90 #include "net/url_request/url_request_throttler_manager.h"
91 #include "url/url_constants.h"
93 #if defined(ENABLE_CONFIGURATION_POLICY)
94 #include "policy/policy_constants.h"
95 #endif
97 #if defined(ENABLE_EXTENSIONS)
98 #include "chrome/browser/extensions/event_router_forwarder.h"
99 #endif
101 #if defined(USE_NSS) || defined(OS_IOS)
102 #include "net/ocsp/nss_ocsp.h"
103 #endif
105 #if defined(OS_CHROMEOS)
106 #include "chrome/browser/chromeos/net/cert_verify_proc_chromeos.h"
107 #include "chromeos/network/host_resolver_impl_chromeos.h"
108 #endif
110 using content::BrowserThread;
112 class SafeBrowsingURLRequestContext;
114 // The IOThread object must outlive any tasks posted to the IO thread before the
115 // Quit task, so base::Bind() calls are not refcounted.
117 namespace {
119 const char kTCPFastOpenFieldTrialName[] = "TCPFastOpen";
120 const char kTCPFastOpenHttpsEnabledGroupName[] = "HttpsEnabled";
122 const char kQuicFieldTrialName[] = "QUIC";
123 const char kQuicFieldTrialEnabledGroupName[] = "Enabled";
124 const char kQuicFieldTrialHttpsEnabledGroupName[] = "HttpsEnabled";
125 const char kQuicFieldTrialPacketLengthSuffix[] = "BytePackets";
126 const char kQuicFieldTrialPacingSuffix[] = "WithPacing";
128 // The SPDY trial composes two different trial plus control groups:
129 // * A "holdback" group with SPDY disabled, and corresponding control
130 // (SPDY/3.1). The primary purpose of the holdback group is to encourage site
131 // operators to do feature detection rather than UA-sniffing. As such, this
132 // trial runs continuously.
133 // * A SPDY/4 experiment, for SPDY/4 (aka HTTP/2) vs SPDY/3.1 comparisons and
134 // eventual SPDY/4 deployment.
135 const char kSpdyFieldTrialName[] = "SPDY";
136 const char kSpdyFieldTrialHoldbackGroupNamePrefix[] = "SpdyDisabled";
137 const char kSpdyFieldTrialHoldbackControlGroupName[] = "Control";
138 const char kSpdyFieldTrialSpdy31GroupNamePrefix[] = "Spdy31Enabled";
139 const char kSpdyFieldTrialSpdy4GroupNamePrefix[] = "Spdy4Enabled";
140 const char kSpdyFieldTrialSpdy4ControlGroupName[] = "Spdy4Control";
142 // Field trial for Cache-Control: stale-while-revalidate directive.
143 const char kStaleWhileRevalidateFieldTrialName[] = "StaleWhileRevalidate";
145 #if defined(OS_MACOSX) && !defined(OS_IOS)
146 void ObserveKeychainEvents() {
147 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
148 net::CertDatabase::GetInstance()->SetMessageLoopForKeychainEvents();
150 #endif
152 // Used for the "system" URLRequestContext.
153 class SystemURLRequestContext : public net::URLRequestContext {
154 public:
155 SystemURLRequestContext() {
156 #if defined(USE_NSS) || defined(OS_IOS)
157 net::SetURLRequestContextForNSSHttpIO(this);
158 #endif
161 private:
162 ~SystemURLRequestContext() override {
163 AssertNoURLRequests();
164 #if defined(USE_NSS) || defined(OS_IOS)
165 net::SetURLRequestContextForNSSHttpIO(NULL);
166 #endif
170 scoped_ptr<net::HostResolver> CreateGlobalHostResolver(net::NetLog* net_log) {
171 TRACE_EVENT0("startup", "IOThread::CreateGlobalHostResolver");
172 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
174 net::HostResolver::Options options;
176 // Use the concurrency override from the command-line, if any.
177 if (command_line.HasSwitch(switches::kHostResolverParallelism)) {
178 std::string s =
179 command_line.GetSwitchValueASCII(switches::kHostResolverParallelism);
181 // Parse the switch (it should be a positive integer formatted as decimal).
182 int n;
183 if (base::StringToInt(s, &n) && n > 0) {
184 options.max_concurrent_resolves = static_cast<size_t>(n);
185 } else {
186 LOG(ERROR) << "Invalid switch for host resolver parallelism: " << s;
190 // Use the retry attempts override from the command-line, if any.
191 if (command_line.HasSwitch(switches::kHostResolverRetryAttempts)) {
192 std::string s =
193 command_line.GetSwitchValueASCII(switches::kHostResolverRetryAttempts);
194 // Parse the switch (it should be a non-negative integer).
195 int n;
196 if (base::StringToInt(s, &n) && n >= 0) {
197 options.max_retry_attempts = static_cast<size_t>(n);
198 } else {
199 LOG(ERROR) << "Invalid switch for host resolver retry attempts: " << s;
203 scoped_ptr<net::HostResolver> global_host_resolver;
204 #if defined OS_CHROMEOS
205 global_host_resolver =
206 chromeos::HostResolverImplChromeOS::CreateSystemResolver(options,
207 net_log);
208 #else
209 global_host_resolver =
210 net::HostResolver::CreateSystemResolver(options, net_log);
211 #endif
213 // Determine if we should disable IPv6 support.
214 if (command_line.HasSwitch(switches::kEnableIPv6)) {
215 // Disable IPv6 probing.
216 global_host_resolver->SetDefaultAddressFamily(
217 net::ADDRESS_FAMILY_UNSPECIFIED);
218 } else if (command_line.HasSwitch(switches::kDisableIPv6)) {
219 global_host_resolver->SetDefaultAddressFamily(net::ADDRESS_FAMILY_IPV4);
222 // If hostname remappings were specified on the command-line, layer these
223 // rules on top of the real host resolver. This allows forwarding all requests
224 // through a designated test server.
225 if (!command_line.HasSwitch(switches::kHostResolverRules))
226 return global_host_resolver.Pass();
228 scoped_ptr<net::MappedHostResolver> remapped_resolver(
229 new net::MappedHostResolver(global_host_resolver.Pass()));
230 remapped_resolver->SetRulesFromString(
231 command_line.GetSwitchValueASCII(switches::kHostResolverRules));
232 return remapped_resolver.Pass();
235 // TODO(willchan): Remove proxy script fetcher context since it's not necessary
236 // now that I got rid of refcounting URLRequestContexts.
237 // See IOThread::Globals for details.
238 net::URLRequestContext*
239 ConstructProxyScriptFetcherContext(IOThread::Globals* globals,
240 net::NetLog* net_log) {
241 net::URLRequestContext* context = new net::URLRequestContext;
242 context->set_net_log(net_log);
243 context->set_host_resolver(globals->host_resolver.get());
244 context->set_cert_verifier(globals->cert_verifier.get());
245 context->set_transport_security_state(
246 globals->transport_security_state.get());
247 context->set_cert_transparency_verifier(
248 globals->cert_transparency_verifier.get());
249 context->set_http_auth_handler_factory(
250 globals->http_auth_handler_factory.get());
251 context->set_proxy_service(globals->proxy_script_fetcher_proxy_service.get());
252 context->set_http_transaction_factory(
253 globals->proxy_script_fetcher_http_transaction_factory.get());
254 context->set_job_factory(
255 globals->proxy_script_fetcher_url_request_job_factory.get());
256 context->set_cookie_store(globals->system_cookie_store.get());
257 context->set_channel_id_service(
258 globals->system_channel_id_service.get());
259 context->set_network_delegate(globals->system_network_delegate.get());
260 context->set_http_user_agent_settings(
261 globals->http_user_agent_settings.get());
262 // TODO(rtenneti): We should probably use HttpServerPropertiesManager for the
263 // system URLRequestContext too. There's no reason this should be tied to a
264 // profile.
265 return context;
268 net::URLRequestContext*
269 ConstructSystemRequestContext(IOThread::Globals* globals,
270 net::NetLog* net_log) {
271 net::URLRequestContext* context = new SystemURLRequestContext;
272 context->set_net_log(net_log);
273 context->set_host_resolver(globals->host_resolver.get());
274 context->set_cert_verifier(globals->cert_verifier.get());
275 context->set_transport_security_state(
276 globals->transport_security_state.get());
277 context->set_cert_transparency_verifier(
278 globals->cert_transparency_verifier.get());
279 context->set_http_auth_handler_factory(
280 globals->http_auth_handler_factory.get());
281 context->set_proxy_service(globals->system_proxy_service.get());
282 context->set_http_transaction_factory(
283 globals->system_http_transaction_factory.get());
284 context->set_job_factory(globals->system_url_request_job_factory.get());
285 context->set_cookie_store(globals->system_cookie_store.get());
286 context->set_channel_id_service(
287 globals->system_channel_id_service.get());
288 context->set_throttler_manager(globals->throttler_manager.get());
289 context->set_network_delegate(globals->system_network_delegate.get());
290 context->set_http_user_agent_settings(
291 globals->http_user_agent_settings.get());
292 return context;
295 int GetSwitchValueAsInt(const CommandLine& command_line,
296 const std::string& switch_name) {
297 int value;
298 if (!base::StringToInt(command_line.GetSwitchValueASCII(switch_name),
299 &value)) {
300 return 0;
302 return value;
305 // Returns the value associated with |key| in |params| or "" if the
306 // key is not present in the map.
307 const std::string& GetVariationParam(
308 const std::map<std::string, std::string>& params,
309 const std::string& key) {
310 std::map<std::string, std::string>::const_iterator it = params.find(key);
311 if (it == params.end())
312 return base::EmptyString();
314 return it->second;
317 // Return true if stale-while-revalidate support should be enabled.
318 bool IsStaleWhileRevalidateEnabled(const base::CommandLine& command_line) {
319 if (command_line.HasSwitch(switches::kEnableStaleWhileRevalidate))
320 return true;
321 const std::string group_name =
322 base::FieldTrialList::FindFullName(kStaleWhileRevalidateFieldTrialName);
323 return group_name == "Enabled";
326 } // namespace
328 class IOThread::LoggingNetworkChangeObserver
329 : public net::NetworkChangeNotifier::IPAddressObserver,
330 public net::NetworkChangeNotifier::ConnectionTypeObserver,
331 public net::NetworkChangeNotifier::NetworkChangeObserver {
332 public:
333 // |net_log| must remain valid throughout our lifetime.
334 explicit LoggingNetworkChangeObserver(net::NetLog* net_log)
335 : net_log_(net_log) {
336 net::NetworkChangeNotifier::AddIPAddressObserver(this);
337 net::NetworkChangeNotifier::AddConnectionTypeObserver(this);
338 net::NetworkChangeNotifier::AddNetworkChangeObserver(this);
341 ~LoggingNetworkChangeObserver() override {
342 net::NetworkChangeNotifier::RemoveIPAddressObserver(this);
343 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
344 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this);
347 // NetworkChangeNotifier::IPAddressObserver implementation.
348 void OnIPAddressChanged() override {
349 VLOG(1) << "Observed a change to the network IP addresses";
351 net_log_->AddGlobalEntry(net::NetLog::TYPE_NETWORK_IP_ADDRESSES_CHANGED);
354 // NetworkChangeNotifier::ConnectionTypeObserver implementation.
355 void OnConnectionTypeChanged(
356 net::NetworkChangeNotifier::ConnectionType type) override {
357 std::string type_as_string =
358 net::NetworkChangeNotifier::ConnectionTypeToString(type);
360 VLOG(1) << "Observed a change to network connectivity state "
361 << type_as_string;
363 net_log_->AddGlobalEntry(
364 net::NetLog::TYPE_NETWORK_CONNECTIVITY_CHANGED,
365 net::NetLog::StringCallback("new_connection_type", &type_as_string));
368 // NetworkChangeNotifier::NetworkChangeObserver implementation.
369 void OnNetworkChanged(
370 net::NetworkChangeNotifier::ConnectionType type) override {
371 std::string type_as_string =
372 net::NetworkChangeNotifier::ConnectionTypeToString(type);
374 VLOG(1) << "Observed a network change to state " << type_as_string;
376 net_log_->AddGlobalEntry(
377 net::NetLog::TYPE_NETWORK_CHANGED,
378 net::NetLog::StringCallback("new_connection_type", &type_as_string));
381 private:
382 net::NetLog* net_log_;
383 DISALLOW_COPY_AND_ASSIGN(LoggingNetworkChangeObserver);
386 class SystemURLRequestContextGetter : public net::URLRequestContextGetter {
387 public:
388 explicit SystemURLRequestContextGetter(IOThread* io_thread);
390 // Implementation for net::UrlRequestContextGetter.
391 net::URLRequestContext* GetURLRequestContext() override;
392 scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner()
393 const override;
395 protected:
396 ~SystemURLRequestContextGetter() override;
398 private:
399 IOThread* const io_thread_; // Weak pointer, owned by BrowserProcess.
400 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
402 base::debug::LeakTracker<SystemURLRequestContextGetter> leak_tracker_;
405 SystemURLRequestContextGetter::SystemURLRequestContextGetter(
406 IOThread* io_thread)
407 : io_thread_(io_thread),
408 network_task_runner_(
409 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)) {
412 SystemURLRequestContextGetter::~SystemURLRequestContextGetter() {}
414 net::URLRequestContext* SystemURLRequestContextGetter::GetURLRequestContext() {
415 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
416 DCHECK(io_thread_->globals()->system_request_context.get());
418 return io_thread_->globals()->system_request_context.get();
421 scoped_refptr<base::SingleThreadTaskRunner>
422 SystemURLRequestContextGetter::GetNetworkTaskRunner() const {
423 return network_task_runner_;
426 IOThread::Globals::
427 SystemRequestContextLeakChecker::SystemRequestContextLeakChecker(
428 Globals* globals)
429 : globals_(globals) {
430 DCHECK(globals_);
433 IOThread::Globals::
434 SystemRequestContextLeakChecker::~SystemRequestContextLeakChecker() {
435 if (globals_->system_request_context.get())
436 globals_->system_request_context->AssertNoURLRequests();
439 IOThread::Globals::Globals()
440 : system_request_context_leak_checker(this),
441 enable_ssl_connect_job_waiting(false),
442 ignore_certificate_errors(false),
443 use_stale_while_revalidate(false),
444 testing_fixed_http_port(0),
445 testing_fixed_https_port(0),
446 enable_user_alternate_protocol_ports(false) {
449 IOThread::Globals::~Globals() {}
451 // |local_state| is passed in explicitly in order to (1) reduce implicit
452 // dependencies and (2) make IOThread more flexible for testing.
453 IOThread::IOThread(
454 PrefService* local_state,
455 policy::PolicyService* policy_service,
456 ChromeNetLog* net_log,
457 extensions::EventRouterForwarder* extension_event_router_forwarder)
458 : net_log_(net_log),
459 #if defined(ENABLE_EXTENSIONS)
460 extension_event_router_forwarder_(extension_event_router_forwarder),
461 #endif
462 globals_(NULL),
463 is_spdy_disabled_by_policy_(false),
464 creation_time_(base::TimeTicks::Now()),
465 weak_factory_(this) {
466 auth_schemes_ = local_state->GetString(prefs::kAuthSchemes);
467 negotiate_disable_cname_lookup_ = local_state->GetBoolean(
468 prefs::kDisableAuthNegotiateCnameLookup);
469 negotiate_enable_port_ = local_state->GetBoolean(
470 prefs::kEnableAuthNegotiatePort);
471 auth_server_whitelist_ = local_state->GetString(prefs::kAuthServerWhitelist);
472 auth_delegate_whitelist_ = local_state->GetString(
473 prefs::kAuthNegotiateDelegateWhitelist);
474 gssapi_library_name_ = local_state->GetString(prefs::kGSSAPILibraryName);
475 pref_proxy_config_tracker_.reset(
476 ProxyServiceFactory::CreatePrefProxyConfigTrackerOfLocalState(
477 local_state));
478 ChromeNetworkDelegate::InitializePrefsOnUIThread(
479 &system_enable_referrers_,
480 NULL,
481 NULL,
482 local_state);
483 ssl_config_service_manager_.reset(
484 SSLConfigServiceManager::CreateDefaultManager(local_state));
486 base::Value* dns_client_enabled_default = new base::FundamentalValue(
487 chrome_browser_net::ConfigureAsyncDnsFieldTrial());
488 local_state->SetDefaultPrefValue(prefs::kBuiltInDnsClientEnabled,
489 dns_client_enabled_default);
491 dns_client_enabled_.Init(prefs::kBuiltInDnsClientEnabled,
492 local_state,
493 base::Bind(&IOThread::UpdateDnsClientEnabled,
494 base::Unretained(this)));
495 dns_client_enabled_.MoveToThread(
496 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
498 quick_check_enabled_.Init(prefs::kQuickCheckEnabled,
499 local_state);
500 quick_check_enabled_.MoveToThread(
501 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
503 #if defined(ENABLE_CONFIGURATION_POLICY)
504 is_spdy_disabled_by_policy_ = policy_service->GetPolicies(
505 policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME, std::string())).Get(
506 policy::key::kDisableSpdy) != NULL;
507 #endif // ENABLE_CONFIGURATION_POLICY
509 BrowserThread::SetDelegate(BrowserThread::IO, this);
512 IOThread::~IOThread() {
513 // This isn't needed for production code, but in tests, IOThread may
514 // be multiply constructed.
515 BrowserThread::SetDelegate(BrowserThread::IO, NULL);
517 pref_proxy_config_tracker_->DetachFromPrefService();
518 DCHECK(!globals_);
521 IOThread::Globals* IOThread::globals() {
522 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
523 return globals_;
526 void IOThread::SetGlobalsForTesting(Globals* globals) {
527 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
528 DCHECK(!globals || !globals_);
529 globals_ = globals;
532 ChromeNetLog* IOThread::net_log() {
533 return net_log_;
536 void IOThread::ChangedToOnTheRecord() {
537 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
538 BrowserThread::PostTask(
539 BrowserThread::IO,
540 FROM_HERE,
541 base::Bind(&IOThread::ChangedToOnTheRecordOnIOThread,
542 base::Unretained(this)));
545 net::URLRequestContextGetter* IOThread::system_url_request_context_getter() {
546 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
547 if (!system_url_request_context_getter_.get()) {
548 InitSystemRequestContext();
550 return system_url_request_context_getter_.get();
553 void IOThread::Init() {
554 // Prefer to use InitAsync unless you need initialization to block
555 // the UI thread
558 void IOThread::InitAsync() {
559 TRACE_EVENT0("startup", "IOThread::InitAsync");
560 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
562 #if defined(USE_NSS) || defined(OS_IOS)
563 net::SetMessageLoopForNSSHttpIO();
564 #endif
566 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
568 DCHECK(!globals_);
569 globals_ = new Globals;
571 // Add an observer that will emit network change events to the ChromeNetLog.
572 // Assuming NetworkChangeNotifier dispatches in FIFO order, we should be
573 // logging the network change before other IO thread consumers respond to it.
574 network_change_observer_.reset(
575 new LoggingNetworkChangeObserver(net_log_));
577 // Setup the HistogramWatcher to run on the IO thread.
578 net::NetworkChangeNotifier::InitHistogramWatcher();
580 #if defined(ENABLE_EXTENSIONS)
581 globals_->extension_event_router_forwarder =
582 extension_event_router_forwarder_;
583 #endif
585 ChromeNetworkDelegate* network_delegate =
586 new ChromeNetworkDelegate(extension_event_router_forwarder(),
587 &system_enable_referrers_);
589 if (command_line.HasSwitch(switches::kEnableClientHints))
590 network_delegate->SetEnableClientHints();
592 #if defined(ENABLE_EXTENSIONS)
593 if (command_line.HasSwitch(switches::kDisableExtensionsHttpThrottling))
594 network_delegate->NeverThrottleRequests();
595 #endif
597 globals_->system_network_delegate.reset(network_delegate);
598 globals_->host_resolver = CreateGlobalHostResolver(net_log_);
599 UpdateDnsClientEnabled();
600 #if defined(OS_CHROMEOS)
601 // Creates a CertVerifyProc that doesn't allow any profile-provided certs.
602 globals_->cert_verifier.reset(new net::MultiThreadedCertVerifier(
603 new chromeos::CertVerifyProcChromeOS()));
604 #else
605 globals_->cert_verifier.reset(new net::MultiThreadedCertVerifier(
606 net::CertVerifyProc::CreateDefault()));
607 #endif
609 globals_->transport_security_state.reset(new net::TransportSecurityState());
611 net::MultiLogCTVerifier* ct_verifier = new net::MultiLogCTVerifier();
612 globals_->cert_transparency_verifier.reset(ct_verifier);
614 // Add built-in logs
615 ct_verifier->AddLogs(net::ct::CreateLogVerifiersForKnownLogs());
617 // Add logs from command line
618 if (command_line.HasSwitch(switches::kCertificateTransparencyLog)) {
619 std::string switch_value = command_line.GetSwitchValueASCII(
620 switches::kCertificateTransparencyLog);
621 std::vector<std::string> logs;
622 base::SplitString(switch_value, ',', &logs);
623 for (std::vector<std::string>::iterator it = logs.begin(); it != logs.end();
624 ++it) {
625 const std::string& curr_log = *it;
626 size_t delim_pos = curr_log.find(":");
627 CHECK(delim_pos != std::string::npos)
628 << "CT log description not provided (switch format"
629 " is 'description:base64_key')";
630 std::string log_description(curr_log.substr(0, delim_pos));
631 std::string ct_public_key_data;
632 CHECK(base::Base64Decode(curr_log.substr(delim_pos + 1),
633 &ct_public_key_data))
634 << "Unable to decode CT public key.";
635 scoped_ptr<net::CTLogVerifier> external_log_verifier(
636 net::CTLogVerifier::Create(ct_public_key_data, log_description));
637 CHECK(external_log_verifier) << "Unable to parse CT public key.";
638 VLOG(1) << "Adding log with description " << log_description;
639 ct_verifier->AddLog(external_log_verifier.Pass());
643 globals_->ssl_config_service = GetSSLConfigService();
645 SetupDataReductionProxy(network_delegate);
647 globals_->http_auth_handler_factory.reset(CreateDefaultAuthHandlerFactory(
648 globals_->host_resolver.get()));
649 globals_->http_server_properties.reset(new net::HttpServerPropertiesImpl());
650 // For the ProxyScriptFetcher, we use a direct ProxyService.
651 globals_->proxy_script_fetcher_proxy_service.reset(
652 net::ProxyService::CreateDirectWithNetLog(net_log_));
653 // In-memory cookie store.
654 globals_->system_cookie_store =
655 content::CreateCookieStore(content::CookieStoreConfig());
656 // In-memory channel ID store.
657 globals_->system_channel_id_service.reset(
658 new net::ChannelIDService(
659 new net::DefaultChannelIDStore(NULL),
660 base::WorkerPool::GetTaskRunner(true)));
661 globals_->dns_probe_service.reset(new chrome_browser_net::DnsProbeService());
662 globals_->host_mapping_rules.reset(new net::HostMappingRules());
663 globals_->http_user_agent_settings.reset(
664 new net::StaticHttpUserAgentSettings(std::string(), GetUserAgent()));
665 if (command_line.HasSwitch(switches::kHostRules)) {
666 TRACE_EVENT_BEGIN0("startup", "IOThread::InitAsync:SetRulesFromString");
667 globals_->host_mapping_rules->SetRulesFromString(
668 command_line.GetSwitchValueASCII(switches::kHostRules));
669 TRACE_EVENT_END0("startup", "IOThread::InitAsync:SetRulesFromString");
671 if (command_line.HasSwitch(switches::kEnableSSLConnectJobWaiting))
672 globals_->enable_ssl_connect_job_waiting = true;
673 if (command_line.HasSwitch(switches::kIgnoreCertificateErrors))
674 globals_->ignore_certificate_errors = true;
675 globals_->use_stale_while_revalidate =
676 IsStaleWhileRevalidateEnabled(command_line);
677 if (command_line.HasSwitch(switches::kTestingFixedHttpPort)) {
678 globals_->testing_fixed_http_port =
679 GetSwitchValueAsInt(command_line, switches::kTestingFixedHttpPort);
681 if (command_line.HasSwitch(switches::kTestingFixedHttpsPort)) {
682 globals_->testing_fixed_https_port =
683 GetSwitchValueAsInt(command_line, switches::kTestingFixedHttpsPort);
685 ConfigureQuic(command_line);
686 if (command_line.HasSwitch(
687 switches::kEnableUserAlternateProtocolPorts)) {
688 globals_->enable_user_alternate_protocol_ports = true;
690 InitializeNetworkOptions(command_line);
692 net::HttpNetworkSession::Params session_params;
693 InitializeNetworkSessionParams(&session_params);
694 session_params.net_log = net_log_;
695 session_params.proxy_service =
696 globals_->proxy_script_fetcher_proxy_service.get();
698 TRACE_EVENT_BEGIN0("startup", "IOThread::InitAsync:HttpNetworkSession");
699 scoped_refptr<net::HttpNetworkSession> network_session(
700 new net::HttpNetworkSession(session_params));
701 globals_->proxy_script_fetcher_http_transaction_factory
702 .reset(new net::HttpNetworkLayer(network_session.get()));
703 TRACE_EVENT_END0("startup", "IOThread::InitAsync:HttpNetworkSession");
704 scoped_ptr<net::URLRequestJobFactoryImpl> job_factory(
705 new net::URLRequestJobFactoryImpl());
706 job_factory->SetProtocolHandler(url::kDataScheme,
707 new net::DataProtocolHandler());
708 job_factory->SetProtocolHandler(
709 url::kFileScheme,
710 new net::FileProtocolHandler(
711 content::BrowserThread::GetBlockingPool()->
712 GetTaskRunnerWithShutdownBehavior(
713 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)));
714 #if !defined(DISABLE_FTP_SUPPORT)
715 globals_->proxy_script_fetcher_ftp_transaction_factory.reset(
716 new net::FtpNetworkLayer(globals_->host_resolver.get()));
717 job_factory->SetProtocolHandler(
718 url::kFtpScheme,
719 new net::FtpProtocolHandler(
720 globals_->proxy_script_fetcher_ftp_transaction_factory.get()));
721 #endif
722 globals_->proxy_script_fetcher_url_request_job_factory = job_factory.Pass();
724 globals_->throttler_manager.reset(new net::URLRequestThrottlerManager());
725 globals_->throttler_manager->set_net_log(net_log_);
726 // Always done in production, disabled only for unit tests.
727 globals_->throttler_manager->set_enable_thread_checks(true);
729 globals_->proxy_script_fetcher_context.reset(
730 ConstructProxyScriptFetcherContext(globals_, net_log_));
732 #if defined(OS_MACOSX) && !defined(OS_IOS)
733 // Start observing Keychain events. This needs to be done on the UI thread,
734 // as Keychain services requires a CFRunLoop.
735 BrowserThread::PostTask(BrowserThread::UI,
736 FROM_HERE,
737 base::Bind(&ObserveKeychainEvents));
738 #endif
740 // InitSystemRequestContext turns right around and posts a task back
741 // to the IO thread, so we can't let it run until we know the IO
742 // thread has started.
744 // Note that since we are at BrowserThread::Init time, the UI thread
745 // is blocked waiting for the thread to start. Therefore, posting
746 // this task to the main thread's message loop here is guaranteed to
747 // get it onto the message loop while the IOThread object still
748 // exists. However, the message might not be processed on the UI
749 // thread until after IOThread is gone, so use a weak pointer.
750 BrowserThread::PostTask(BrowserThread::UI,
751 FROM_HERE,
752 base::Bind(&IOThread::InitSystemRequestContext,
753 weak_factory_.GetWeakPtr()));
756 void IOThread::CleanUp() {
757 base::debug::LeakTracker<SafeBrowsingURLRequestContext>::CheckForLeaks();
759 #if defined(USE_NSS) || defined(OS_IOS)
760 net::ShutdownNSSHttpIO();
761 #endif
763 system_url_request_context_getter_ = NULL;
765 // Release objects that the net::URLRequestContext could have been pointing
766 // to.
768 // Shutdown the HistogramWatcher on the IO thread.
769 net::NetworkChangeNotifier::ShutdownHistogramWatcher();
771 // This must be reset before the ChromeNetLog is destroyed.
772 network_change_observer_.reset();
774 system_proxy_config_service_.reset();
776 delete globals_;
777 globals_ = NULL;
779 base::debug::LeakTracker<SystemURLRequestContextGetter>::CheckForLeaks();
782 void IOThread::InitializeNetworkOptions(const CommandLine& command_line) {
783 // Only handle use-spdy command line flags if "spdy.disabled" preference is
784 // not disabled via policy.
785 if (is_spdy_disabled_by_policy_) {
786 base::FieldTrial* trial = base::FieldTrialList::Find(kSpdyFieldTrialName);
787 if (trial)
788 trial->Disable();
789 } else {
790 if (command_line.HasSwitch(switches::kTrustedSpdyProxy)) {
791 globals_->trusted_spdy_proxy.set(
792 command_line.GetSwitchValueASCII(switches::kTrustedSpdyProxy));
794 if (command_line.HasSwitch(switches::kIgnoreUrlFetcherCertRequests))
795 net::URLFetcher::SetIgnoreCertificateRequests(true);
797 if (command_line.HasSwitch(switches::kUseSpdy)) {
798 std::string spdy_mode =
799 command_line.GetSwitchValueASCII(switches::kUseSpdy);
800 EnableSpdy(spdy_mode);
801 } else if (command_line.HasSwitch(switches::kEnableSpdy4)) {
802 globals_->next_protos = net::NextProtosSpdy4Http2();
803 globals_->use_alternate_protocols.set(true);
804 } else if (command_line.HasSwitch(switches::kEnableNpnHttpOnly)) {
805 globals_->next_protos = net::NextProtosHttpOnly();
806 globals_->use_alternate_protocols.set(false);
807 } else {
808 // No SPDY command-line flags have been specified. Examine trial groups.
809 ConfigureSpdyFromTrial(
810 base::FieldTrialList::FindFullName(kSpdyFieldTrialName), globals_);
814 ConfigureTCPFastOpen(command_line);
816 // TODO(rch): Make the client socket factory a per-network session
817 // instance, constructed from a NetworkSession::Params, to allow us
818 // to move this option to IOThread::Globals &
819 // HttpNetworkSession::Params.
822 void IOThread::ConfigureTCPFastOpen(const CommandLine& command_line) {
823 const std::string trial_group =
824 base::FieldTrialList::FindFullName(kTCPFastOpenFieldTrialName);
825 if (trial_group == kTCPFastOpenHttpsEnabledGroupName)
826 globals_->enable_tcp_fast_open_for_ssl.set(true);
827 bool always_enable_if_supported =
828 command_line.HasSwitch(switches::kEnableTcpFastOpen);
829 // Check for OS support of TCP FastOpen, and turn it on for all connections
830 // if indicated by user.
831 net::CheckSupportAndMaybeEnableTCPFastOpen(always_enable_if_supported);
834 void IOThread::ConfigureSpdyFromTrial(base::StringPiece spdy_trial_group,
835 Globals* globals) {
836 if (spdy_trial_group.starts_with(kSpdyFieldTrialHoldbackGroupNamePrefix)) {
837 // TODO(jgraettinger): Use net::NextProtosHttpOnly() instead?
838 net::HttpStreamFactory::set_spdy_enabled(false);
839 } else if (spdy_trial_group == kSpdyFieldTrialHoldbackControlGroupName) {
840 // Use the current SPDY default (SPDY/3.1).
841 globals->next_protos = net::NextProtosSpdy31();
842 globals->use_alternate_protocols.set(true);
843 } else if (spdy_trial_group.starts_with(
844 kSpdyFieldTrialSpdy31GroupNamePrefix)) {
845 globals->next_protos = net::NextProtosSpdy31();
846 globals->use_alternate_protocols.set(true);
847 } else if (spdy_trial_group.starts_with(
848 kSpdyFieldTrialSpdy4GroupNamePrefix)) {
849 globals->next_protos = net::NextProtosSpdy4Http2();
850 globals->use_alternate_protocols.set(true);
851 } else if (spdy_trial_group == kSpdyFieldTrialSpdy4ControlGroupName) {
852 // This control group is pinned at SPDY/3.1.
853 globals->next_protos = net::NextProtosSpdy31();
854 globals->use_alternate_protocols.set(true);
855 } else {
856 // Use the current SPDY default (SPDY/3.1).
857 globals->next_protos = net::NextProtosSpdy31();
858 globals->use_alternate_protocols.set(true);
862 void IOThread::EnableSpdy(const std::string& mode) {
863 static const char kOff[] = "off";
864 static const char kSSL[] = "ssl";
865 static const char kDisableSSL[] = "no-ssl";
866 static const char kDisablePing[] = "no-ping";
867 static const char kExclude[] = "exclude"; // Hosts to exclude
868 static const char kDisableCompression[] = "no-compress";
869 static const char kDisableAltProtocols[] = "no-alt-protocols";
870 static const char kForceAltProtocols[] = "force-alt-protocols";
871 static const char kSingleDomain[] = "single-domain";
873 static const char kInitialMaxConcurrentStreams[] = "init-max-streams";
875 std::vector<std::string> spdy_options;
876 base::SplitString(mode, ',', &spdy_options);
878 for (std::vector<std::string>::iterator it = spdy_options.begin();
879 it != spdy_options.end(); ++it) {
880 const std::string& element = *it;
881 std::vector<std::string> name_value;
882 base::SplitString(element, '=', &name_value);
883 const std::string& option =
884 name_value.size() > 0 ? name_value[0] : std::string();
885 const std::string value =
886 name_value.size() > 1 ? name_value[1] : std::string();
888 if (option == kOff) {
889 net::HttpStreamFactory::set_spdy_enabled(false);
890 } else if (option == kDisableSSL) {
891 globals_->spdy_default_protocol.set(net::kProtoSPDY31);
892 globals_->force_spdy_over_ssl.set(false);
893 globals_->force_spdy_always.set(true);
894 } else if (option == kSSL) {
895 globals_->spdy_default_protocol.set(net::kProtoSPDY31);
896 globals_->force_spdy_over_ssl.set(true);
897 globals_->force_spdy_always.set(true);
898 } else if (option == kDisablePing) {
899 globals_->enable_spdy_ping_based_connection_checking.set(false);
900 } else if (option == kExclude) {
901 globals_->forced_spdy_exclusions.insert(
902 net::HostPortPair::FromURL(GURL(value)));
903 } else if (option == kDisableCompression) {
904 globals_->enable_spdy_compression.set(false);
905 } else if (option == kDisableAltProtocols) {
906 globals_->use_alternate_protocols.set(false);
907 } else if (option == kForceAltProtocols) {
908 net::AlternateProtocolInfo pair(443, net::NPN_SPDY_3, 1);
909 net::HttpServerPropertiesImpl::ForceAlternateProtocol(pair);
910 } else if (option == kSingleDomain) {
911 DVLOG(1) << "FORCING SINGLE DOMAIN";
912 globals_->force_spdy_single_domain.set(true);
913 } else if (option == kInitialMaxConcurrentStreams) {
914 int streams;
915 if (base::StringToInt(value, &streams))
916 globals_->initial_max_spdy_concurrent_streams.set(streams);
917 } else if (option.empty() && it == spdy_options.begin()) {
918 continue;
919 } else {
920 LOG(DFATAL) << "Unrecognized spdy option: " << option;
925 // static
926 void IOThread::RegisterPrefs(PrefRegistrySimple* registry) {
927 registry->RegisterStringPref(prefs::kAuthSchemes,
928 "basic,digest,ntlm,negotiate,"
929 "spdyproxy");
930 registry->RegisterBooleanPref(prefs::kDisableAuthNegotiateCnameLookup, false);
931 registry->RegisterBooleanPref(prefs::kEnableAuthNegotiatePort, false);
932 registry->RegisterStringPref(prefs::kAuthServerWhitelist, std::string());
933 registry->RegisterStringPref(prefs::kAuthNegotiateDelegateWhitelist,
934 std::string());
935 registry->RegisterStringPref(prefs::kGSSAPILibraryName, std::string());
936 registry->RegisterStringPref(
937 data_reduction_proxy::prefs::kDataReductionProxy, std::string());
938 registry->RegisterBooleanPref(prefs::kEnableReferrers, true);
939 data_reduction_proxy::RegisterPrefs(registry);
940 registry->RegisterBooleanPref(prefs::kBuiltInDnsClientEnabled, true);
941 registry->RegisterBooleanPref(prefs::kQuickCheckEnabled, true);
944 net::HttpAuthHandlerFactory* IOThread::CreateDefaultAuthHandlerFactory(
945 net::HostResolver* resolver) {
946 net::HttpAuthFilterWhitelist* auth_filter_default_credentials = NULL;
947 if (!auth_server_whitelist_.empty()) {
948 auth_filter_default_credentials =
949 new net::HttpAuthFilterWhitelist(auth_server_whitelist_);
951 net::HttpAuthFilterWhitelist* auth_filter_delegate = NULL;
952 if (!auth_delegate_whitelist_.empty()) {
953 auth_filter_delegate =
954 new net::HttpAuthFilterWhitelist(auth_delegate_whitelist_);
956 globals_->url_security_manager.reset(
957 net::URLSecurityManager::Create(auth_filter_default_credentials,
958 auth_filter_delegate));
959 std::vector<std::string> supported_schemes;
960 base::SplitString(auth_schemes_, ',', &supported_schemes);
962 scoped_ptr<net::HttpAuthHandlerRegistryFactory> registry_factory(
963 net::HttpAuthHandlerRegistryFactory::Create(
964 supported_schemes, globals_->url_security_manager.get(),
965 resolver, gssapi_library_name_, negotiate_disable_cname_lookup_,
966 negotiate_enable_port_));
967 return registry_factory.release();
970 void IOThread::ClearHostCache() {
971 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
973 net::HostCache* host_cache = globals_->host_resolver->GetHostCache();
974 if (host_cache)
975 host_cache->clear();
978 void IOThread::InitializeNetworkSessionParams(
979 net::HttpNetworkSession::Params* params) {
980 InitializeNetworkSessionParamsFromGlobals(*globals_, params);
983 // static
984 void IOThread::InitializeNetworkSessionParamsFromGlobals(
985 const IOThread::Globals& globals,
986 net::HttpNetworkSession::Params* params) {
987 params->host_resolver = globals.host_resolver.get();
988 params->cert_verifier = globals.cert_verifier.get();
989 params->channel_id_service = globals.system_channel_id_service.get();
990 params->transport_security_state = globals.transport_security_state.get();
991 params->ssl_config_service = globals.ssl_config_service.get();
992 params->http_auth_handler_factory = globals.http_auth_handler_factory.get();
993 params->http_server_properties =
994 globals.http_server_properties->GetWeakPtr();
995 params->network_delegate = globals.system_network_delegate.get();
996 params->host_mapping_rules = globals.host_mapping_rules.get();
997 params->enable_ssl_connect_job_waiting =
998 globals.enable_ssl_connect_job_waiting;
999 params->ignore_certificate_errors = globals.ignore_certificate_errors;
1000 params->use_stale_while_revalidate = globals.use_stale_while_revalidate;
1001 params->testing_fixed_http_port = globals.testing_fixed_http_port;
1002 params->testing_fixed_https_port = globals.testing_fixed_https_port;
1003 globals.enable_tcp_fast_open_for_ssl.CopyToIfSet(
1004 &params->enable_tcp_fast_open_for_ssl);
1006 globals.initial_max_spdy_concurrent_streams.CopyToIfSet(
1007 &params->spdy_initial_max_concurrent_streams);
1008 globals.force_spdy_single_domain.CopyToIfSet(
1009 &params->force_spdy_single_domain);
1010 globals.enable_spdy_compression.CopyToIfSet(
1011 &params->enable_spdy_compression);
1012 globals.enable_spdy_ping_based_connection_checking.CopyToIfSet(
1013 &params->enable_spdy_ping_based_connection_checking);
1014 globals.spdy_default_protocol.CopyToIfSet(
1015 &params->spdy_default_protocol);
1016 params->next_protos = globals.next_protos;
1017 globals.trusted_spdy_proxy.CopyToIfSet(&params->trusted_spdy_proxy);
1018 globals.force_spdy_over_ssl.CopyToIfSet(&params->force_spdy_over_ssl);
1019 globals.force_spdy_always.CopyToIfSet(&params->force_spdy_always);
1020 params->forced_spdy_exclusions = globals.forced_spdy_exclusions;
1021 globals.use_alternate_protocols.CopyToIfSet(
1022 &params->use_alternate_protocols);
1023 globals.alternate_protocol_probability_threshold.CopyToIfSet(
1024 &params->alternate_protocol_probability_threshold);
1026 globals.enable_quic.CopyToIfSet(&params->enable_quic);
1027 globals.quic_always_require_handshake_confirmation.CopyToIfSet(
1028 &params->quic_always_require_handshake_confirmation);
1029 globals.quic_disable_connection_pooling.CopyToIfSet(
1030 &params->quic_disable_connection_pooling);
1031 globals.quic_load_server_info_timeout_ms.CopyToIfSet(
1032 &params->quic_load_server_info_timeout_ms);
1033 globals.enable_quic_port_selection.CopyToIfSet(
1034 &params->enable_quic_port_selection);
1035 globals.quic_max_packet_length.CopyToIfSet(&params->quic_max_packet_length);
1036 globals.quic_user_agent_id.CopyToIfSet(&params->quic_user_agent_id);
1037 globals.quic_supported_versions.CopyToIfSet(
1038 &params->quic_supported_versions);
1039 params->quic_connection_options = globals.quic_connection_options;
1041 globals.origin_to_force_quic_on.CopyToIfSet(
1042 &params->origin_to_force_quic_on);
1043 params->enable_user_alternate_protocol_ports =
1044 globals.enable_user_alternate_protocol_ports;
1045 params->proxy_delegate = globals.data_reduction_proxy_delegate.get();
1048 base::TimeTicks IOThread::creation_time() const {
1049 return creation_time_;
1052 net::SSLConfigService* IOThread::GetSSLConfigService() {
1053 return ssl_config_service_manager_->Get();
1056 void IOThread::ChangedToOnTheRecordOnIOThread() {
1057 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
1059 // Clear the host cache to avoid showing entries from the OTR session
1060 // in about:net-internals.
1061 ClearHostCache();
1064 void IOThread::InitSystemRequestContext() {
1065 if (system_url_request_context_getter_.get())
1066 return;
1067 // If we're in unit_tests, IOThread may not be run.
1068 if (!BrowserThread::IsMessageLoopValid(BrowserThread::IO))
1069 return;
1070 system_proxy_config_service_.reset(
1071 ProxyServiceFactory::CreateProxyConfigService(
1072 pref_proxy_config_tracker_.get()));
1073 system_url_request_context_getter_ =
1074 new SystemURLRequestContextGetter(this);
1075 // Safe to post an unretained this pointer, since IOThread is
1076 // guaranteed to outlive the IO BrowserThread.
1077 BrowserThread::PostTask(
1078 BrowserThread::IO,
1079 FROM_HERE,
1080 base::Bind(&IOThread::InitSystemRequestContextOnIOThread,
1081 base::Unretained(this)));
1084 void IOThread::InitSystemRequestContextOnIOThread() {
1085 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
1086 DCHECK(!globals_->system_proxy_service.get());
1087 DCHECK(system_proxy_config_service_.get());
1089 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
1090 globals_->system_proxy_service.reset(
1091 ProxyServiceFactory::CreateProxyService(
1092 net_log_,
1093 globals_->proxy_script_fetcher_context.get(),
1094 globals_->system_network_delegate.get(),
1095 system_proxy_config_service_.release(),
1096 command_line,
1097 quick_check_enabled_.GetValue()));
1098 DCHECK(globals_->data_reduction_proxy_params);
1100 net::HttpNetworkSession::Params system_params;
1101 InitializeNetworkSessionParams(&system_params);
1102 system_params.net_log = net_log_;
1103 system_params.proxy_service = globals_->system_proxy_service.get();
1105 globals_->system_http_transaction_factory.reset(
1106 new net::HttpNetworkLayer(
1107 new net::HttpNetworkSession(system_params)));
1108 globals_->system_url_request_job_factory.reset(
1109 new net::URLRequestJobFactoryImpl());
1110 globals_->system_request_context.reset(
1111 ConstructSystemRequestContext(globals_, net_log_));
1112 globals_->system_request_context->set_ssl_config_service(
1113 globals_->ssl_config_service.get());
1114 globals_->system_request_context->set_http_server_properties(
1115 globals_->http_server_properties->GetWeakPtr());
1118 void IOThread::UpdateDnsClientEnabled() {
1119 globals()->host_resolver->SetDnsClientEnabled(*dns_client_enabled_);
1122 void IOThread::ConfigureQuic(const CommandLine& command_line) {
1123 // Always fetch the field trial group to ensure it is reported correctly.
1124 // The command line flags will be associated with a group that is reported
1125 // so long as trial is actually queried.
1126 std::string group =
1127 base::FieldTrialList::FindFullName(kQuicFieldTrialName);
1128 VariationParameters params;
1129 if (!variations::GetVariationParams(kQuicFieldTrialName, &params)) {
1130 params.clear();
1133 ConfigureQuicGlobals(command_line, group, params, globals_);
1136 void IOThread::SetupDataReductionProxy(
1137 ChromeNetworkDelegate* network_delegate) {
1138 // TODO(kundaji): Move flags initialization to DataReductionProxyParams and
1139 // merge with flag initialization in
1140 // data_reduction_proxy_chrome_settings_factory.cc.
1141 int flags = data_reduction_proxy::DataReductionProxyParams::kAllowed |
1142 data_reduction_proxy::DataReductionProxyParams::kFallbackAllowed |
1143 data_reduction_proxy::DataReductionProxyParams::kAlternativeAllowed;
1144 if (data_reduction_proxy::DataReductionProxyParams::
1145 IsIncludedInPromoFieldTrial()) {
1146 flags |= data_reduction_proxy::DataReductionProxyParams::kPromoAllowed;
1148 if (data_reduction_proxy::DataReductionProxyParams::
1149 IsIncludedInHoldbackFieldTrial()) {
1150 flags |= data_reduction_proxy::DataReductionProxyParams::kHoldback;
1152 globals_->data_reduction_proxy_params.reset(
1153 new data_reduction_proxy::DataReductionProxyParams(flags));
1154 globals_->data_reduction_proxy_auth_request_handler.reset(
1155 new data_reduction_proxy::DataReductionProxyAuthRequestHandler(
1156 DataReductionProxyChromeSettings::GetClient(),
1157 globals_->data_reduction_proxy_params.get(),
1158 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)));
1159 globals_->data_reduction_proxy_delegate.reset(
1160 new data_reduction_proxy::DataReductionProxyDelegate(
1161 globals_->data_reduction_proxy_auth_request_handler.get(),
1162 globals_->data_reduction_proxy_params.get()));
1163 // This is the same as in ProfileImplIOData except that we do not collect
1164 // usage stats.
1165 network_delegate->set_data_reduction_proxy_params(
1166 globals_->data_reduction_proxy_params.get());
1167 network_delegate->set_data_reduction_proxy_auth_request_handler(
1168 globals_->data_reduction_proxy_auth_request_handler.get());
1169 network_delegate->set_on_resolve_proxy_handler(
1170 base::Bind(data_reduction_proxy::OnResolveProxyHandler));
1173 // static
1174 void IOThread::ConfigureQuicGlobals(
1175 const base::CommandLine& command_line,
1176 base::StringPiece quic_trial_group,
1177 const VariationParameters& quic_trial_params,
1178 IOThread::Globals* globals) {
1179 bool enable_quic = ShouldEnableQuic(command_line, quic_trial_group);
1180 globals->enable_quic.set(enable_quic);
1181 if (enable_quic) {
1182 globals->quic_always_require_handshake_confirmation.set(
1183 ShouldQuicAlwaysRequireHandshakeConfirmation(quic_trial_params));
1184 globals->quic_disable_connection_pooling.set(
1185 ShouldQuicDisableConnectionPooling(quic_trial_params));
1186 int load_server_info_timeout_ms =
1187 GetQuicLoadServerInfoTimeout(quic_trial_params);
1188 if (load_server_info_timeout_ms != 0) {
1189 globals->quic_load_server_info_timeout_ms.set(
1190 load_server_info_timeout_ms);
1192 globals->enable_quic_port_selection.set(
1193 ShouldEnableQuicPortSelection(command_line));
1194 globals->quic_connection_options =
1195 GetQuicConnectionOptions(command_line, quic_trial_params);
1196 if (ShouldEnableQuicPacing(command_line, quic_trial_group,
1197 quic_trial_params)) {
1198 globals->quic_connection_options.push_back(net::kPACE);
1202 size_t max_packet_length = GetQuicMaxPacketLength(command_line,
1203 quic_trial_group,
1204 quic_trial_params);
1205 if (max_packet_length != 0) {
1206 globals->quic_max_packet_length.set(max_packet_length);
1209 std::string quic_user_agent_id =
1210 chrome::VersionInfo::GetVersionStringModifier();
1211 if (!quic_user_agent_id.empty())
1212 quic_user_agent_id.push_back(' ');
1213 chrome::VersionInfo version_info;
1214 quic_user_agent_id.append(version_info.ProductNameAndVersionForUserAgent());
1215 globals->quic_user_agent_id.set(quic_user_agent_id);
1217 net::QuicVersion version = GetQuicVersion(command_line, quic_trial_params);
1218 if (version != net::QUIC_VERSION_UNSUPPORTED) {
1219 net::QuicVersionVector supported_versions;
1220 supported_versions.push_back(version);
1221 globals->quic_supported_versions.set(supported_versions);
1224 double threshold =
1225 GetAlternateProtocolProbabilityThreshold(command_line, quic_trial_params);
1226 if (threshold >=0 && threshold <= 1) {
1227 globals->alternate_protocol_probability_threshold.set(threshold);
1228 globals->http_server_properties->SetAlternateProtocolProbabilityThreshold(
1229 threshold);
1232 if (command_line.HasSwitch(switches::kOriginToForceQuicOn)) {
1233 net::HostPortPair quic_origin =
1234 net::HostPortPair::FromString(
1235 command_line.GetSwitchValueASCII(switches::kOriginToForceQuicOn));
1236 if (!quic_origin.IsEmpty()) {
1237 globals->origin_to_force_quic_on.set(quic_origin);
1242 bool IOThread::ShouldEnableQuic(const CommandLine& command_line,
1243 base::StringPiece quic_trial_group) {
1244 if (command_line.HasSwitch(switches::kDisableQuic))
1245 return false;
1247 if (command_line.HasSwitch(switches::kEnableQuic))
1248 return true;
1250 return quic_trial_group.starts_with(kQuicFieldTrialEnabledGroupName) ||
1251 quic_trial_group.starts_with(kQuicFieldTrialHttpsEnabledGroupName);
1254 bool IOThread::ShouldEnableQuicPortSelection(
1255 const CommandLine& command_line) {
1256 if (command_line.HasSwitch(switches::kDisableQuicPortSelection))
1257 return false;
1259 if (command_line.HasSwitch(switches::kEnableQuicPortSelection))
1260 return true;
1262 return false; // Default to disabling port selection on all channels.
1265 bool IOThread::ShouldEnableQuicPacing(
1266 const CommandLine& command_line,
1267 base::StringPiece quic_trial_group,
1268 const VariationParameters& quic_trial_params) {
1269 if (command_line.HasSwitch(switches::kEnableQuicPacing))
1270 return true;
1272 if (command_line.HasSwitch(switches::kDisableQuicPacing))
1273 return false;
1275 if (LowerCaseEqualsASCII(
1276 GetVariationParam(quic_trial_params, "enable_pacing"),
1277 "true"))
1278 return true;
1280 return quic_trial_group.ends_with(kQuicFieldTrialPacingSuffix);
1283 net::QuicTagVector IOThread::GetQuicConnectionOptions(
1284 const CommandLine& command_line,
1285 const VariationParameters& quic_trial_params) {
1286 if (command_line.HasSwitch(switches::kQuicConnectionOptions)) {
1287 return net::QuicUtils::ParseQuicConnectionOptions(
1288 command_line.GetSwitchValueASCII(switches::kQuicConnectionOptions));
1291 VariationParameters::const_iterator it =
1292 quic_trial_params.find("connection_options");
1293 if (it == quic_trial_params.end()) {
1294 // TODO(rch): remove support for deprecated congestion_options.
1295 it = quic_trial_params.find("congestion_options");
1296 if (it == quic_trial_params.end())
1297 return net::QuicTagVector();
1300 return net::QuicUtils::ParseQuicConnectionOptions(it->second);
1303 // static
1304 double IOThread::GetAlternateProtocolProbabilityThreshold(
1305 const base::CommandLine& command_line,
1306 const VariationParameters& quic_trial_params) {
1307 double value;
1308 if (command_line.HasSwitch(
1309 switches::kAlternateProtocolProbabilityThreshold)) {
1310 if (base::StringToDouble(
1311 command_line.GetSwitchValueASCII(
1312 switches::kAlternateProtocolProbabilityThreshold),
1313 &value)) {
1314 return value;
1317 if (command_line.HasSwitch(switches::kEnableQuic)) {
1318 return 0;
1320 if (base::StringToDouble(
1321 GetVariationParam(quic_trial_params,
1322 "alternate_protocol_probability_threshold"),
1323 &value)) {
1324 return value;
1326 return -1;
1329 // static
1330 bool IOThread::ShouldQuicAlwaysRequireHandshakeConfirmation(
1331 const VariationParameters& quic_trial_params) {
1332 return LowerCaseEqualsASCII(
1333 GetVariationParam(quic_trial_params,
1334 "always_require_handshake_confirmation"),
1335 "true");
1338 // static
1339 bool IOThread::ShouldQuicDisableConnectionPooling(
1340 const VariationParameters& quic_trial_params) {
1341 return LowerCaseEqualsASCII(
1342 GetVariationParam(quic_trial_params, "disable_connection_pooling"),
1343 "true");
1346 // static
1347 int IOThread::GetQuicLoadServerInfoTimeout(
1348 const VariationParameters& quic_trial_params) {
1349 int value;
1350 if (base::StringToInt(GetVariationParam(quic_trial_params,
1351 "load_server_info_timeout"),
1352 &value)) {
1353 return value;
1355 return 0;
1358 // static
1359 size_t IOThread::GetQuicMaxPacketLength(
1360 const CommandLine& command_line,
1361 base::StringPiece quic_trial_group,
1362 const VariationParameters& quic_trial_params) {
1363 if (command_line.HasSwitch(switches::kQuicMaxPacketLength)) {
1364 unsigned value;
1365 if (!base::StringToUint(
1366 command_line.GetSwitchValueASCII(switches::kQuicMaxPacketLength),
1367 &value)) {
1368 return 0;
1370 return value;
1373 unsigned value;
1374 if (base::StringToUint(GetVariationParam(quic_trial_params,
1375 "max_packet_length"),
1376 &value)) {
1377 return value;
1380 // Format of the packet length group names is:
1381 // (Https)?Enabled<length>BytePackets.
1382 base::StringPiece length_str(quic_trial_group);
1383 if (length_str.starts_with(kQuicFieldTrialEnabledGroupName)) {
1384 length_str.remove_prefix(strlen(kQuicFieldTrialEnabledGroupName));
1385 } else if (length_str.starts_with(kQuicFieldTrialHttpsEnabledGroupName)) {
1386 length_str.remove_prefix(strlen(kQuicFieldTrialHttpsEnabledGroupName));
1387 } else {
1388 return 0;
1390 if (!length_str.ends_with(kQuicFieldTrialPacketLengthSuffix)) {
1391 return 0;
1393 length_str.remove_suffix(strlen(kQuicFieldTrialPacketLengthSuffix));
1394 if (!base::StringToUint(length_str, &value)) {
1395 return 0;
1397 return value;
1400 // static
1401 net::QuicVersion IOThread::GetQuicVersion(
1402 const CommandLine& command_line,
1403 const VariationParameters& quic_trial_params) {
1404 if (command_line.HasSwitch(switches::kQuicVersion)) {
1405 return ParseQuicVersion(
1406 command_line.GetSwitchValueASCII(switches::kQuicVersion));
1409 return ParseQuicVersion(GetVariationParam(quic_trial_params, "quic_version"));
1412 // static
1413 net::QuicVersion IOThread::ParseQuicVersion(const std::string& quic_version) {
1414 net::QuicVersionVector supported_versions = net::QuicSupportedVersions();
1415 for (size_t i = 0; i < supported_versions.size(); ++i) {
1416 net::QuicVersion version = supported_versions[i];
1417 if (net::QuicVersionToString(version) == quic_version) {
1418 return version;
1422 return net::QUIC_VERSION_UNSUPPORTED;