cc: Drop skip_shared_out_of_order_tiles field from TilingSetEvictionQueue.
[chromium-blink-merge.git] / chrome / browser / io_thread.cc
bloba5018ff4e6686e9caca9b1720ebafd8d02e73cbb
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/io_thread.h"
7 #include <vector>
9 #include "base/base64.h"
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/command_line.h"
13 #include "base/compiler_specific.h"
14 #include "base/debug/leak_tracker.h"
15 #include "base/logging.h"
16 #include "base/metrics/field_trial.h"
17 #include "base/prefs/pref_registry_simple.h"
18 #include "base/prefs/pref_service.h"
19 #include "base/profiler/scoped_tracker.h"
20 #include "base/stl_util.h"
21 #include "base/strings/string_number_conversions.h"
22 #include "base/strings/string_piece.h"
23 #include "base/strings/string_split.h"
24 #include "base/strings/string_util.h"
25 #include "base/threading/sequenced_worker_pool.h"
26 #include "base/threading/thread.h"
27 #include "base/threading/worker_pool.h"
28 #include "base/time/time.h"
29 #include "base/trace_event/trace_event.h"
30 #include "build/build_config.h"
31 #include "chrome/browser/browser_process.h"
32 #include "chrome/browser/net/async_dns_field_trial.h"
33 #include "chrome/browser/net/chrome_net_log.h"
34 #include "chrome/browser/net/chrome_network_delegate.h"
35 #include "chrome/browser/net/connect_interceptor.h"
36 #include "chrome/browser/net/dns_probe_service.h"
37 #include "chrome/browser/net/pref_proxy_config_tracker.h"
38 #include "chrome/browser/net/proxy_service_factory.h"
39 #include "chrome/common/chrome_content_client.h"
40 #include "chrome/common/chrome_switches.h"
41 #include "chrome/common/chrome_version_info.h"
42 #include "chrome/common/pref_names.h"
43 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_prefs.h"
44 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h"
45 #include "components/policy/core/common/policy_service.h"
46 #include "components/variations/variations_associated_data.h"
47 #include "content/public/browser/browser_thread.h"
48 #include "content/public/browser/cookie_store_factory.h"
49 #include "net/base/host_mapping_rules.h"
50 #include "net/base/net_util.h"
51 #include "net/base/sdch_manager.h"
52 #include "net/cert/cert_policy_enforcer.h"
53 #include "net/cert/cert_verifier.h"
54 #include "net/cert/cert_verify_proc.h"
55 #include "net/cert/ct_known_logs.h"
56 #include "net/cert/ct_known_logs_static.h"
57 #include "net/cert/ct_log_verifier.h"
58 #include "net/cert/ct_verifier.h"
59 #include "net/cert/multi_log_ct_verifier.h"
60 #include "net/cert/multi_threaded_cert_verifier.h"
61 #include "net/cookies/cookie_store.h"
62 #include "net/dns/host_cache.h"
63 #include "net/dns/host_resolver.h"
64 #include "net/dns/mapped_host_resolver.h"
65 #include "net/ftp/ftp_network_layer.h"
66 #include "net/http/http_auth_filter.h"
67 #include "net/http/http_auth_handler_factory.h"
68 #include "net/http/http_network_layer.h"
69 #include "net/http/http_server_properties_impl.h"
70 #include "net/proxy/proxy_config_service.h"
71 #include "net/proxy/proxy_script_fetcher_impl.h"
72 #include "net/proxy/proxy_service.h"
73 #include "net/quic/crypto/crypto_protocol.h"
74 #include "net/quic/quic_protocol.h"
75 #include "net/quic/quic_utils.h"
76 #include "net/socket/tcp_client_socket.h"
77 #include "net/spdy/spdy_session.h"
78 #include "net/ssl/channel_id_service.h"
79 #include "net/ssl/default_channel_id_store.h"
80 #include "net/url_request/data_protocol_handler.h"
81 #include "net/url_request/file_protocol_handler.h"
82 #include "net/url_request/ftp_protocol_handler.h"
83 #include "net/url_request/static_http_user_agent_settings.h"
84 #include "net/url_request/url_fetcher.h"
85 #include "net/url_request/url_request_context.h"
86 #include "net/url_request/url_request_context_getter.h"
87 #include "net/url_request/url_request_job_factory_impl.h"
88 #include "net/url_request/url_request_throttler_manager.h"
89 #include "url/url_constants.h"
91 #if defined(ENABLE_CONFIGURATION_POLICY)
92 #include "policy/policy_constants.h"
93 #endif
95 #if defined(ENABLE_EXTENSIONS)
96 #include "chrome/browser/extensions/event_router_forwarder.h"
97 #endif
99 #if defined(USE_NSS_CERTS) || defined(OS_IOS)
100 #include "net/cert_net/nss_ocsp.h"
101 #endif
103 #if defined(OS_ANDROID)
104 #include "base/android/build_info.h"
105 #endif
107 #if defined(OS_CHROMEOS)
108 #include "chrome/browser/chromeos/net/cert_verify_proc_chromeos.h"
109 #include "chromeos/network/host_resolver_impl_chromeos.h"
110 #endif
112 using content::BrowserThread;
114 class SafeBrowsingURLRequestContext;
116 // The IOThread object must outlive any tasks posted to the IO thread before the
117 // Quit task, so base::Bind() calls are not refcounted.
119 namespace {
121 const char kTCPFastOpenFieldTrialName[] = "TCPFastOpen";
122 const char kTCPFastOpenHttpsEnabledGroupName[] = "HttpsEnabled";
124 const char kQuicFieldTrialName[] = "QUIC";
125 const char kQuicFieldTrialEnabledGroupName[] = "Enabled";
126 const char kQuicFieldTrialHttpsEnabledGroupName[] = "HttpsEnabled";
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 kSpdyFieldTrialSpdy31GroupNamePrefix[] = "Spdy31Enabled";
138 const char kSpdyFieldTrialSpdy4GroupNamePrefix[] = "Spdy4Enabled";
139 const char kSpdyFieldTrialParametrizedPrefix[] = "Parametrized";
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_CERTS) || defined(OS_IOS)
156 net::SetURLRequestContextForNSSHttpIO(this);
157 #endif
160 private:
161 ~SystemURLRequestContext() override {
162 AssertNoURLRequests();
163 #if defined(USE_NSS_CERTS) || 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");
316 if (command_line.HasSwitch(
317 switches::kDisableCertificateTransparencyRequirementForEV))
318 return false;
320 return group_name == "RequirementEnforced";
323 // Parse kUseSpdy command line flag options, which may contain the following:
325 // "off" : Disables SPDY support entirely.
326 // "no-ping" : Disables SPDY ping connection testing.
327 // "exclude=<host>" : Disables SPDY support for the host <host>.
328 // "no-compress" : Disables SPDY header compression.
329 // "no-alt-protocols : Disables alternate protocol support.
330 // "init-max-streams=<limit>" : Specifies the maximum number of concurrent
331 // streams for a SPDY session, unless the
332 // specifies a different value via SETTINGS.
333 void ConfigureSpdyGlobalsFromUseSpdyArgument(const std::string& mode,
334 IOThread::Globals* globals) {
335 static const char kOff[] = "off";
336 static const char kDisablePing[] = "no-ping";
337 static const char kExclude[] = "exclude"; // Hosts to exclude
338 static const char kDisableCompression[] = "no-compress";
339 static const char kDisableAltProtocols[] = "no-alt-protocols";
340 static const char kInitialMaxConcurrentStreams[] = "init-max-streams";
342 std::vector<std::string> spdy_options;
343 base::SplitString(mode, ',', &spdy_options);
345 for (const std::string& element : spdy_options) {
346 std::vector<std::string> name_value;
347 base::SplitString(element, '=', &name_value);
348 const std::string& option =
349 name_value.size() > 0 ? name_value[0] : std::string();
350 const std::string value =
351 name_value.size() > 1 ? name_value[1] : std::string();
353 if (option == kOff) {
354 net::HttpStreamFactory::set_spdy_enabled(false);
355 continue;
357 if (option == kDisablePing) {
358 globals->enable_spdy_ping_based_connection_checking.set(false);
359 continue;
361 if (option == kExclude) {
362 globals->forced_spdy_exclusions.insert(
363 net::HostPortPair::FromURL(GURL(value)));
364 continue;
366 if (option == kDisableCompression) {
367 globals->enable_spdy_compression.set(false);
368 continue;
370 if (option == kDisableAltProtocols) {
371 globals->use_alternate_protocols.set(false);
372 continue;
374 if (option == kInitialMaxConcurrentStreams) {
375 int streams;
376 if (base::StringToInt(value, &streams)) {
377 globals->initial_max_spdy_concurrent_streams.set(streams);
378 continue;
381 LOG(DFATAL) << "Unrecognized spdy option: " << option;
385 } // namespace
387 class IOThread::LoggingNetworkChangeObserver
388 : public net::NetworkChangeNotifier::IPAddressObserver,
389 public net::NetworkChangeNotifier::ConnectionTypeObserver,
390 public net::NetworkChangeNotifier::NetworkChangeObserver {
391 public:
392 // |net_log| must remain valid throughout our lifetime.
393 explicit LoggingNetworkChangeObserver(net::NetLog* net_log)
394 : net_log_(net_log) {
395 net::NetworkChangeNotifier::AddIPAddressObserver(this);
396 net::NetworkChangeNotifier::AddConnectionTypeObserver(this);
397 net::NetworkChangeNotifier::AddNetworkChangeObserver(this);
400 ~LoggingNetworkChangeObserver() override {
401 net::NetworkChangeNotifier::RemoveIPAddressObserver(this);
402 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
403 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this);
406 // NetworkChangeNotifier::IPAddressObserver implementation.
407 void OnIPAddressChanged() override {
408 VLOG(1) << "Observed a change to the network IP addresses";
410 net_log_->AddGlobalEntry(net::NetLog::TYPE_NETWORK_IP_ADDRESSES_CHANGED);
413 // NetworkChangeNotifier::ConnectionTypeObserver implementation.
414 void OnConnectionTypeChanged(
415 net::NetworkChangeNotifier::ConnectionType type) override {
416 std::string type_as_string =
417 net::NetworkChangeNotifier::ConnectionTypeToString(type);
419 VLOG(1) << "Observed a change to network connectivity state "
420 << type_as_string;
422 net_log_->AddGlobalEntry(
423 net::NetLog::TYPE_NETWORK_CONNECTIVITY_CHANGED,
424 net::NetLog::StringCallback("new_connection_type", &type_as_string));
427 // NetworkChangeNotifier::NetworkChangeObserver implementation.
428 void OnNetworkChanged(
429 net::NetworkChangeNotifier::ConnectionType type) override {
430 std::string type_as_string =
431 net::NetworkChangeNotifier::ConnectionTypeToString(type);
433 VLOG(1) << "Observed a network change to state " << type_as_string;
435 net_log_->AddGlobalEntry(
436 net::NetLog::TYPE_NETWORK_CHANGED,
437 net::NetLog::StringCallback("new_connection_type", &type_as_string));
440 private:
441 net::NetLog* net_log_;
442 DISALLOW_COPY_AND_ASSIGN(LoggingNetworkChangeObserver);
445 class SystemURLRequestContextGetter : public net::URLRequestContextGetter {
446 public:
447 explicit SystemURLRequestContextGetter(IOThread* io_thread);
449 // Implementation for net::UrlRequestContextGetter.
450 net::URLRequestContext* GetURLRequestContext() override;
451 scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner()
452 const override;
454 protected:
455 ~SystemURLRequestContextGetter() override;
457 private:
458 IOThread* const io_thread_; // Weak pointer, owned by BrowserProcess.
459 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
461 base::debug::LeakTracker<SystemURLRequestContextGetter> leak_tracker_;
464 SystemURLRequestContextGetter::SystemURLRequestContextGetter(
465 IOThread* io_thread)
466 : io_thread_(io_thread),
467 network_task_runner_(
468 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)) {
471 SystemURLRequestContextGetter::~SystemURLRequestContextGetter() {}
473 net::URLRequestContext* SystemURLRequestContextGetter::GetURLRequestContext() {
474 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
475 DCHECK(io_thread_->globals()->system_request_context.get());
477 return io_thread_->globals()->system_request_context.get();
480 scoped_refptr<base::SingleThreadTaskRunner>
481 SystemURLRequestContextGetter::GetNetworkTaskRunner() const {
482 return network_task_runner_;
485 IOThread::Globals::
486 SystemRequestContextLeakChecker::SystemRequestContextLeakChecker(
487 Globals* globals)
488 : globals_(globals) {
489 DCHECK(globals_);
492 IOThread::Globals::
493 SystemRequestContextLeakChecker::~SystemRequestContextLeakChecker() {
494 if (globals_->system_request_context.get())
495 globals_->system_request_context->AssertNoURLRequests();
498 IOThread::Globals::Globals()
499 : system_request_context_leak_checker(this),
500 ignore_certificate_errors(false),
501 use_stale_while_revalidate(false),
502 testing_fixed_http_port(0),
503 testing_fixed_https_port(0),
504 enable_user_alternate_protocol_ports(false) {
507 IOThread::Globals::~Globals() {}
509 // |local_state| is passed in explicitly in order to (1) reduce implicit
510 // dependencies and (2) make IOThread more flexible for testing.
511 IOThread::IOThread(
512 PrefService* local_state,
513 policy::PolicyService* policy_service,
514 ChromeNetLog* net_log,
515 extensions::EventRouterForwarder* extension_event_router_forwarder)
516 : net_log_(net_log),
517 #if defined(ENABLE_EXTENSIONS)
518 extension_event_router_forwarder_(extension_event_router_forwarder),
519 #endif
520 globals_(NULL),
521 is_spdy_disabled_by_policy_(false),
522 is_quic_allowed_by_policy_(true),
523 creation_time_(base::TimeTicks::Now()),
524 weak_factory_(this) {
525 auth_schemes_ = local_state->GetString(prefs::kAuthSchemes);
526 negotiate_disable_cname_lookup_ = local_state->GetBoolean(
527 prefs::kDisableAuthNegotiateCnameLookup);
528 negotiate_enable_port_ = local_state->GetBoolean(
529 prefs::kEnableAuthNegotiatePort);
530 auth_server_whitelist_ = local_state->GetString(prefs::kAuthServerWhitelist);
531 auth_delegate_whitelist_ = local_state->GetString(
532 prefs::kAuthNegotiateDelegateWhitelist);
533 gssapi_library_name_ = local_state->GetString(prefs::kGSSAPILibraryName);
534 pref_proxy_config_tracker_.reset(
535 ProxyServiceFactory::CreatePrefProxyConfigTrackerOfLocalState(
536 local_state));
537 ChromeNetworkDelegate::InitializePrefsOnUIThread(
538 &system_enable_referrers_,
539 NULL,
540 NULL,
541 NULL,
542 local_state);
543 ssl_config_service_manager_.reset(
544 SSLConfigServiceManager::CreateDefaultManager(local_state));
546 base::Value* dns_client_enabled_default = new base::FundamentalValue(
547 chrome_browser_net::ConfigureAsyncDnsFieldTrial());
548 local_state->SetDefaultPrefValue(prefs::kBuiltInDnsClientEnabled,
549 dns_client_enabled_default);
550 chrome_browser_net::LogAsyncDnsPrefSource(
551 local_state->FindPreference(prefs::kBuiltInDnsClientEnabled));
553 dns_client_enabled_.Init(prefs::kBuiltInDnsClientEnabled,
554 local_state,
555 base::Bind(&IOThread::UpdateDnsClientEnabled,
556 base::Unretained(this)));
557 dns_client_enabled_.MoveToThread(
558 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
560 quick_check_enabled_.Init(prefs::kQuickCheckEnabled,
561 local_state);
562 quick_check_enabled_.MoveToThread(
563 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
565 #if defined(ENABLE_CONFIGURATION_POLICY)
566 is_spdy_disabled_by_policy_ = policy_service->GetPolicies(
567 policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME, std::string())).Get(
568 policy::key::kDisableSpdy) != NULL;
570 const base::Value* value = policy_service->GetPolicies(
571 policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME,
572 std::string())).GetValue(policy::key::kQuicAllowed);
573 if (value)
574 value->GetAsBoolean(&is_quic_allowed_by_policy_);
575 #endif // ENABLE_CONFIGURATION_POLICY
577 BrowserThread::SetDelegate(BrowserThread::IO, this);
580 IOThread::~IOThread() {
581 // This isn't needed for production code, but in tests, IOThread may
582 // be multiply constructed.
583 BrowserThread::SetDelegate(BrowserThread::IO, NULL);
585 pref_proxy_config_tracker_->DetachFromPrefService();
586 DCHECK(!globals_);
589 IOThread::Globals* IOThread::globals() {
590 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
591 return globals_;
594 void IOThread::SetGlobalsForTesting(Globals* globals) {
595 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
596 DCHECK(!globals || !globals_);
597 globals_ = globals;
600 ChromeNetLog* IOThread::net_log() {
601 return net_log_;
604 void IOThread::ChangedToOnTheRecord() {
605 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
606 BrowserThread::PostTask(
607 BrowserThread::IO,
608 FROM_HERE,
609 base::Bind(&IOThread::ChangedToOnTheRecordOnIOThread,
610 base::Unretained(this)));
613 net::URLRequestContextGetter* IOThread::system_url_request_context_getter() {
614 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
615 if (!system_url_request_context_getter_.get()) {
616 InitSystemRequestContext();
618 return system_url_request_context_getter_.get();
621 void IOThread::Init() {
622 // Prefer to use InitAsync unless you need initialization to block
623 // the UI thread
626 void IOThread::InitAsync() {
627 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
628 // is fixed.
629 tracked_objects::ScopedTracker tracking_profile1(
630 FROM_HERE_WITH_EXPLICIT_FUNCTION("466432 IOThread::InitAsync::Start"));
631 TRACE_EVENT0("startup", "IOThread::InitAsync");
632 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
634 #if defined(USE_NSS_CERTS) || defined(OS_IOS)
635 net::SetMessageLoopForNSSHttpIO();
636 #endif
638 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
639 // is fixed.
640 tracked_objects::ScopedTracker tracking_profile2(
641 FROM_HERE_WITH_EXPLICIT_FUNCTION(
642 "466432 IOThread::InitAsync::CommandLineForCurrentProcess"));
643 const base::CommandLine& command_line =
644 *base::CommandLine::ForCurrentProcess();
646 DCHECK(!globals_);
647 globals_ = new Globals;
649 // Add an observer that will emit network change events to the ChromeNetLog.
650 // Assuming NetworkChangeNotifier dispatches in FIFO order, we should be
651 // logging the network change before other IO thread consumers respond to it.
652 network_change_observer_.reset(
653 new LoggingNetworkChangeObserver(net_log_));
655 // Setup the HistogramWatcher to run on the IO thread.
656 net::NetworkChangeNotifier::InitHistogramWatcher();
658 #if defined(ENABLE_EXTENSIONS)
659 globals_->extension_event_router_forwarder =
660 extension_event_router_forwarder_;
661 #endif
663 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
664 // is fixed.
665 tracked_objects::ScopedTracker tracking_profile3(
666 FROM_HERE_WITH_EXPLICIT_FUNCTION(
667 "466432 IOThread::InitAsync::ChromeNetworkDelegate"));
668 scoped_ptr<ChromeNetworkDelegate> chrome_network_delegate(
669 new ChromeNetworkDelegate(extension_event_router_forwarder(),
670 &system_enable_referrers_));
672 #if defined(ENABLE_EXTENSIONS)
673 if (command_line.HasSwitch(switches::kDisableExtensionsHttpThrottling))
674 chrome_network_delegate->NeverThrottleRequests();
675 #endif
677 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
678 // is fixed.
679 tracked_objects::ScopedTracker tracking_profile4(
680 FROM_HERE_WITH_EXPLICIT_FUNCTION(
681 "466432 IOThread::InitAsync::CreateGlobalHostResolver"));
682 globals_->system_network_delegate = chrome_network_delegate.Pass();
683 globals_->host_resolver = CreateGlobalHostResolver(net_log_);
684 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
685 // is fixed.
686 tracked_objects::ScopedTracker tracking_profile5(
687 FROM_HERE_WITH_EXPLICIT_FUNCTION(
688 "466432 IOThread::InitAsync::UpdateDnsClientEnabled::Start"));
689 UpdateDnsClientEnabled();
690 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
691 // is fixed.
692 tracked_objects::ScopedTracker tracking_profile6(
693 FROM_HERE_WITH_EXPLICIT_FUNCTION(
694 "466432 IOThread::InitAsync::UpdateDnsClientEnabled::End"));
695 #if defined(OS_CHROMEOS)
696 // Creates a CertVerifyProc that doesn't allow any profile-provided certs.
697 globals_->cert_verifier.reset(new net::MultiThreadedCertVerifier(
698 new chromeos::CertVerifyProcChromeOS()));
699 #else
700 globals_->cert_verifier.reset(new net::MultiThreadedCertVerifier(
701 net::CertVerifyProc::CreateDefault()));
702 #endif
704 globals_->transport_security_state.reset(new net::TransportSecurityState());
706 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
707 // is fixed.
708 tracked_objects::ScopedTracker tracking_profile7(
709 FROM_HERE_WITH_EXPLICIT_FUNCTION(
710 "466432 IOThread::InitAsync::CreateMultiLogVerifier"));
711 net::MultiLogCTVerifier* ct_verifier = new net::MultiLogCTVerifier();
712 globals_->cert_transparency_verifier.reset(ct_verifier);
714 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
715 // is fixed.
716 tracked_objects::ScopedTracker tracking_profile8(
717 FROM_HERE_WITH_EXPLICIT_FUNCTION(
718 "466432 IOThread::InitAsync::CreateLogVerifiers::Start"));
719 // Add built-in logs
720 ct_verifier->AddLogs(net::ct::CreateLogVerifiersForKnownLogs());
721 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
722 // is fixed.
723 tracked_objects::ScopedTracker tracking_profile9(
724 FROM_HERE_WITH_EXPLICIT_FUNCTION(
725 "466432 IOThread::InitAsync::CreateLogVerifiers::End"));
727 // Add logs from command line
728 if (command_line.HasSwitch(switches::kCertificateTransparencyLog)) {
729 std::string switch_value = command_line.GetSwitchValueASCII(
730 switches::kCertificateTransparencyLog);
731 std::vector<std::string> logs;
732 base::SplitString(switch_value, ',', &logs);
733 for (std::vector<std::string>::iterator it = logs.begin(); it != logs.end();
734 ++it) {
735 const std::string& curr_log = *it;
736 size_t delim_pos = curr_log.find(":");
737 CHECK(delim_pos != std::string::npos)
738 << "CT log description not provided (switch format"
739 " is 'description:base64_key')";
740 std::string log_description(curr_log.substr(0, delim_pos));
741 std::string ct_public_key_data;
742 CHECK(base::Base64Decode(curr_log.substr(delim_pos + 1),
743 &ct_public_key_data))
744 << "Unable to decode CT public key.";
745 scoped_ptr<net::CTLogVerifier> external_log_verifier(
746 net::CTLogVerifier::Create(ct_public_key_data, log_description));
747 CHECK(external_log_verifier) << "Unable to parse CT public key.";
748 VLOG(1) << "Adding log with description " << log_description;
749 ct_verifier->AddLog(external_log_verifier.Pass());
753 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
754 // is fixed.
755 tracked_objects::ScopedTracker tracking_profile10(
756 FROM_HERE_WITH_EXPLICIT_FUNCTION(
757 "466432 IOThread::InitAsync::CertPolicyEnforcer"));
758 net::CertPolicyEnforcer* policy_enforcer = NULL;
759 policy_enforcer = new net::CertPolicyEnforcer(
760 IsCertificateTransparencyRequiredForEV(command_line));
761 globals_->cert_policy_enforcer.reset(policy_enforcer);
763 globals_->ssl_config_service = GetSSLConfigService();
765 globals_->http_auth_handler_factory.reset(CreateDefaultAuthHandlerFactory(
766 globals_->host_resolver.get()));
767 globals_->http_server_properties.reset(new net::HttpServerPropertiesImpl());
768 // For the ProxyScriptFetcher, we use a direct ProxyService.
769 globals_->proxy_script_fetcher_proxy_service.reset(
770 net::ProxyService::CreateDirectWithNetLog(net_log_));
771 // In-memory cookie store.
772 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
773 // is fixed.
774 tracked_objects::ScopedTracker tracking_profile11(
775 FROM_HERE_WITH_EXPLICIT_FUNCTION(
776 "466432 IOThread::InitAsync::CreateCookieStore::Start"));
777 globals_->system_cookie_store =
778 content::CreateCookieStore(content::CookieStoreConfig());
779 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
780 // is fixed.
781 tracked_objects::ScopedTracker tracking_profile12(
782 FROM_HERE_WITH_EXPLICIT_FUNCTION(
783 "466432 IOThread::InitAsync::CreateCookieStore::End"));
784 // In-memory channel ID store.
785 globals_->system_channel_id_service.reset(
786 new net::ChannelIDService(
787 new net::DefaultChannelIDStore(NULL),
788 base::WorkerPool::GetTaskRunner(true)));
789 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
790 // is fixed.
791 tracked_objects::ScopedTracker tracking_profile12_1(
792 FROM_HERE_WITH_EXPLICIT_FUNCTION(
793 "466432 IOThread::InitAsync::CreateDnsProbeService"));
794 globals_->dns_probe_service.reset(new chrome_browser_net::DnsProbeService());
795 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
796 // is fixed.
797 tracked_objects::ScopedTracker tracking_profile12_2(
798 FROM_HERE_WITH_EXPLICIT_FUNCTION(
799 "466432 IOThread::InitAsync::CreateHostMappingRules"));
800 globals_->host_mapping_rules.reset(new net::HostMappingRules());
801 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
802 // is fixed.
803 tracked_objects::ScopedTracker tracking_profile12_3(
804 FROM_HERE_WITH_EXPLICIT_FUNCTION(
805 "466432 IOThread::InitAsync::CreateHTTPUserAgentSettings"));
806 globals_->http_user_agent_settings.reset(
807 new net::StaticHttpUserAgentSettings(std::string(), GetUserAgent()));
808 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
809 // is fixed.
810 tracked_objects::ScopedTracker tracking_profile12_4(
811 FROM_HERE_WITH_EXPLICIT_FUNCTION(
812 "466432 IOThread::InitAsync::CommandLineConfiguration"));
813 if (command_line.HasSwitch(switches::kHostRules)) {
814 TRACE_EVENT_BEGIN0("startup", "IOThread::InitAsync:SetRulesFromString");
815 globals_->host_mapping_rules->SetRulesFromString(
816 command_line.GetSwitchValueASCII(switches::kHostRules));
817 TRACE_EVENT_END0("startup", "IOThread::InitAsync:SetRulesFromString");
819 if (command_line.HasSwitch(switches::kIgnoreCertificateErrors))
820 globals_->ignore_certificate_errors = true;
821 globals_->use_stale_while_revalidate =
822 IsStaleWhileRevalidateEnabled(command_line);
823 if (command_line.HasSwitch(switches::kTestingFixedHttpPort)) {
824 globals_->testing_fixed_http_port =
825 GetSwitchValueAsInt(command_line, switches::kTestingFixedHttpPort);
827 if (command_line.HasSwitch(switches::kTestingFixedHttpsPort)) {
828 globals_->testing_fixed_https_port =
829 GetSwitchValueAsInt(command_line, switches::kTestingFixedHttpsPort);
831 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
832 // is fixed.
833 tracked_objects::ScopedTracker tracking_profile12_5(
834 FROM_HERE_WITH_EXPLICIT_FUNCTION(
835 "466432 IOThread::InitAsync::QuicConfiguration"));
836 ConfigureQuic(command_line);
837 if (command_line.HasSwitch(
838 switches::kEnableUserAlternateProtocolPorts)) {
839 globals_->enable_user_alternate_protocol_ports = true;
841 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
842 // is fixed.
843 tracked_objects::ScopedTracker tracking_profile13(
844 FROM_HERE_WITH_EXPLICIT_FUNCTION(
845 "466432 IOThread::InitAsync::InitializeNetworkOptions"));
846 InitializeNetworkOptions(command_line);
848 net::HttpNetworkSession::Params session_params;
849 InitializeNetworkSessionParams(&session_params);
850 session_params.net_log = net_log_;
851 session_params.proxy_service =
852 globals_->proxy_script_fetcher_proxy_service.get();
854 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
855 // is fixed.
856 tracked_objects::ScopedTracker tracking_profile14(
857 FROM_HERE_WITH_EXPLICIT_FUNCTION(
858 "466432 IOThread::InitAsync::HttpNetorkSession::Start"));
859 TRACE_EVENT_BEGIN0("startup", "IOThread::InitAsync:HttpNetworkSession");
860 scoped_refptr<net::HttpNetworkSession> network_session(
861 new net::HttpNetworkSession(session_params));
862 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
863 // is fixed.
864 tracked_objects::ScopedTracker tracking_profile15(
865 FROM_HERE_WITH_EXPLICIT_FUNCTION(
866 "466432 IOThread::InitAsync::HttpNetorkSession::End"));
867 globals_->proxy_script_fetcher_http_transaction_factory
868 .reset(new net::HttpNetworkLayer(network_session.get()));
869 TRACE_EVENT_END0("startup", "IOThread::InitAsync:HttpNetworkSession");
870 scoped_ptr<net::URLRequestJobFactoryImpl> job_factory(
871 new net::URLRequestJobFactoryImpl());
873 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
874 // is fixed.
875 tracked_objects::ScopedTracker tracking_profile16(
876 FROM_HERE_WITH_EXPLICIT_FUNCTION(
877 "466432 IOThread::InitAsync::SetProtocolHandler"));
878 job_factory->SetProtocolHandler(url::kDataScheme,
879 new net::DataProtocolHandler());
880 job_factory->SetProtocolHandler(
881 url::kFileScheme,
882 new net::FileProtocolHandler(
883 content::BrowserThread::GetBlockingPool()->
884 GetTaskRunnerWithShutdownBehavior(
885 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)));
886 #if !defined(DISABLE_FTP_SUPPORT)
887 globals_->proxy_script_fetcher_ftp_transaction_factory.reset(
888 new net::FtpNetworkLayer(globals_->host_resolver.get()));
889 job_factory->SetProtocolHandler(
890 url::kFtpScheme,
891 new net::FtpProtocolHandler(
892 globals_->proxy_script_fetcher_ftp_transaction_factory.get()));
893 #endif
894 globals_->proxy_script_fetcher_url_request_job_factory = job_factory.Pass();
896 globals_->throttler_manager.reset(new net::URLRequestThrottlerManager());
897 globals_->throttler_manager->set_net_log(net_log_);
898 // Always done in production, disabled only for unit tests.
899 globals_->throttler_manager->set_enable_thread_checks(true);
901 globals_->proxy_script_fetcher_context.reset(
902 ConstructProxyScriptFetcherContext(globals_, net_log_));
904 #if defined(OS_MACOSX) && !defined(OS_IOS)
905 // Start observing Keychain events. This needs to be done on the UI thread,
906 // as Keychain services requires a CFRunLoop.
907 BrowserThread::PostTask(BrowserThread::UI,
908 FROM_HERE,
909 base::Bind(&ObserveKeychainEvents));
910 #endif
912 // InitSystemRequestContext turns right around and posts a task back
913 // to the IO thread, so we can't let it run until we know the IO
914 // thread has started.
916 // Note that since we are at BrowserThread::Init time, the UI thread
917 // is blocked waiting for the thread to start. Therefore, posting
918 // this task to the main thread's message loop here is guaranteed to
919 // get it onto the message loop while the IOThread object still
920 // exists. However, the message might not be processed on the UI
921 // thread until after IOThread is gone, so use a weak pointer.
922 BrowserThread::PostTask(BrowserThread::UI,
923 FROM_HERE,
924 base::Bind(&IOThread::InitSystemRequestContext,
925 weak_factory_.GetWeakPtr()));
928 void IOThread::CleanUp() {
929 base::debug::LeakTracker<SafeBrowsingURLRequestContext>::CheckForLeaks();
931 #if defined(USE_NSS_CERTS) || defined(OS_IOS)
932 net::ShutdownNSSHttpIO();
933 #endif
935 system_url_request_context_getter_ = NULL;
937 // Release objects that the net::URLRequestContext could have been pointing
938 // to.
940 // Shutdown the HistogramWatcher on the IO thread.
941 net::NetworkChangeNotifier::ShutdownHistogramWatcher();
943 // This must be reset before the ChromeNetLog is destroyed.
944 network_change_observer_.reset();
946 system_proxy_config_service_.reset();
948 delete globals_;
949 globals_ = NULL;
951 base::debug::LeakTracker<SystemURLRequestContextGetter>::CheckForLeaks();
954 void IOThread::InitializeNetworkOptions(const base::CommandLine& command_line) {
955 // Only handle use-spdy command line flags if "spdy.disabled" preference is
956 // not disabled via policy.
957 if (is_spdy_disabled_by_policy_) {
958 base::FieldTrial* trial = base::FieldTrialList::Find(kSpdyFieldTrialName);
959 if (trial)
960 trial->Disable();
961 } else {
962 std::string group = base::FieldTrialList::FindFullName(kSpdyFieldTrialName);
963 VariationParameters params;
964 if (!variations::GetVariationParams(kSpdyFieldTrialName, &params)) {
965 params.clear();
967 ConfigureSpdyGlobals(command_line, group, params, globals_);
970 ConfigureTCPFastOpen(command_line);
971 ConfigureSdch();
973 // TODO(rch): Make the client socket factory a per-network session
974 // instance, constructed from a NetworkSession::Params, to allow us
975 // to move this option to IOThread::Globals &
976 // HttpNetworkSession::Params.
979 void IOThread::ConfigureTCPFastOpen(const base::CommandLine& command_line) {
980 const std::string trial_group =
981 base::FieldTrialList::FindFullName(kTCPFastOpenFieldTrialName);
982 if (trial_group == kTCPFastOpenHttpsEnabledGroupName)
983 globals_->enable_tcp_fast_open_for_ssl.set(true);
984 bool always_enable_if_supported =
985 command_line.HasSwitch(switches::kEnableTcpFastOpen);
986 // Check for OS support of TCP FastOpen, and turn it on for all connections
987 // if indicated by user.
988 net::CheckSupportAndMaybeEnableTCPFastOpen(always_enable_if_supported);
991 void IOThread::ConfigureSdch() {
992 // Check SDCH field trial. Default is now that everything is enabled,
993 // so provide options for disabling HTTPS or all of SDCH.
994 const char kSdchFieldTrialName[] = "SDCH";
995 const char kEnabledHttpOnlyGroupName[] = "EnabledHttpOnly";
996 const char kDisabledAllGroupName[] = "DisabledAll";
998 // Store in a string on return to keep underlying storage for
999 // StringPiece stable.
1000 std::string sdch_trial_group_string =
1001 base::FieldTrialList::FindFullName(kSdchFieldTrialName);
1002 base::StringPiece sdch_trial_group(sdch_trial_group_string);
1003 if (sdch_trial_group.starts_with(kEnabledHttpOnlyGroupName)) {
1004 net::SdchManager::EnableSdchSupport(true);
1005 net::SdchManager::EnableSecureSchemeSupport(false);
1006 } else if (sdch_trial_group.starts_with(kDisabledAllGroupName)) {
1007 net::SdchManager::EnableSdchSupport(false);
1011 // static
1012 void IOThread::ConfigureSpdyGlobals(
1013 const base::CommandLine& command_line,
1014 base::StringPiece spdy_trial_group,
1015 const VariationParameters& spdy_trial_params,
1016 IOThread::Globals* globals) {
1017 if (command_line.HasSwitch(switches::kTrustedSpdyProxy)) {
1018 globals->trusted_spdy_proxy.set(
1019 command_line.GetSwitchValueASCII(switches::kTrustedSpdyProxy));
1021 if (command_line.HasSwitch(switches::kIgnoreUrlFetcherCertRequests))
1022 net::URLFetcher::SetIgnoreCertificateRequests(true);
1024 if (command_line.HasSwitch(switches::kUseSpdy)) {
1025 std::string spdy_mode =
1026 command_line.GetSwitchValueASCII(switches::kUseSpdy);
1027 ConfigureSpdyGlobalsFromUseSpdyArgument(spdy_mode, globals);
1028 return;
1031 globals->next_protos.clear();
1032 globals->next_protos.push_back(net::kProtoHTTP11);
1033 bool enable_quic = false;
1034 globals->enable_quic.CopyToIfSet(&enable_quic);
1035 if (enable_quic) {
1036 globals->next_protos.push_back(net::kProtoQUIC1SPDY3);
1039 if (command_line.HasSwitch(switches::kEnableSpdy4)) {
1040 globals->next_protos.push_back(net::kProtoSPDY31);
1041 globals->next_protos.push_back(net::kProtoSPDY4_14);
1042 globals->next_protos.push_back(net::kProtoSPDY4);
1043 globals->use_alternate_protocols.set(true);
1044 return;
1046 if (command_line.HasSwitch(switches::kEnableNpnHttpOnly)) {
1047 globals->use_alternate_protocols.set(false);
1048 return;
1051 // No SPDY command-line flags have been specified. Examine trial groups.
1052 if (spdy_trial_group.starts_with(kSpdyFieldTrialHoldbackGroupNamePrefix)) {
1053 net::HttpStreamFactory::set_spdy_enabled(false);
1054 return;
1056 if (spdy_trial_group.starts_with(kSpdyFieldTrialSpdy31GroupNamePrefix)) {
1057 globals->next_protos.push_back(net::kProtoSPDY31);
1058 globals->use_alternate_protocols.set(true);
1059 return;
1061 if (spdy_trial_group.starts_with(kSpdyFieldTrialSpdy4GroupNamePrefix)) {
1062 globals->next_protos.push_back(net::kProtoSPDY31);
1063 globals->next_protos.push_back(net::kProtoSPDY4_14);
1064 globals->next_protos.push_back(net::kProtoSPDY4);
1065 globals->use_alternate_protocols.set(true);
1066 return;
1068 if (spdy_trial_group.starts_with(kSpdyFieldTrialParametrizedPrefix)) {
1069 bool spdy_enabled = false;
1070 if (LowerCaseEqualsASCII(
1071 GetVariationParam(spdy_trial_params, "enable_spdy31"), "true")) {
1072 globals->next_protos.push_back(net::kProtoSPDY31);
1073 spdy_enabled = true;
1075 if (LowerCaseEqualsASCII(
1076 GetVariationParam(spdy_trial_params, "enable_http2_14"), "true")) {
1077 globals->next_protos.push_back(net::kProtoSPDY4_14);
1078 spdy_enabled = true;
1080 if (LowerCaseEqualsASCII(
1081 GetVariationParam(spdy_trial_params, "enable_http2"), "true")) {
1082 globals->next_protos.push_back(net::kProtoSPDY4);
1083 spdy_enabled = true;
1085 // TODO(bnc): HttpStreamFactory::spdy_enabled_ is redundant with
1086 // globals->next_protos, can it be eliminated?
1087 net::HttpStreamFactory::set_spdy_enabled(spdy_enabled);
1088 globals->use_alternate_protocols.set(true);
1089 return;
1092 // By default, enable HTTP/2.
1093 globals->next_protos.push_back(net::kProtoSPDY31);
1094 globals->next_protos.push_back(net::kProtoSPDY4_14);
1095 globals->next_protos.push_back(net::kProtoSPDY4);
1096 globals->use_alternate_protocols.set(true);
1099 // static
1100 void IOThread::RegisterPrefs(PrefRegistrySimple* registry) {
1101 registry->RegisterStringPref(prefs::kAuthSchemes,
1102 "basic,digest,ntlm,negotiate,"
1103 "spdyproxy");
1104 registry->RegisterBooleanPref(prefs::kDisableAuthNegotiateCnameLookup, false);
1105 registry->RegisterBooleanPref(prefs::kEnableAuthNegotiatePort, false);
1106 registry->RegisterStringPref(prefs::kAuthServerWhitelist, std::string());
1107 registry->RegisterStringPref(prefs::kAuthNegotiateDelegateWhitelist,
1108 std::string());
1109 registry->RegisterStringPref(prefs::kGSSAPILibraryName, std::string());
1110 registry->RegisterStringPref(
1111 data_reduction_proxy::prefs::kDataReductionProxy, std::string());
1112 registry->RegisterBooleanPref(prefs::kEnableReferrers, true);
1113 data_reduction_proxy::RegisterPrefs(registry);
1114 registry->RegisterBooleanPref(prefs::kBuiltInDnsClientEnabled, true);
1115 registry->RegisterBooleanPref(prefs::kQuickCheckEnabled, true);
1118 net::HttpAuthHandlerFactory* IOThread::CreateDefaultAuthHandlerFactory(
1119 net::HostResolver* resolver) {
1120 net::HttpAuthFilterWhitelist* auth_filter_default_credentials = NULL;
1121 if (!auth_server_whitelist_.empty()) {
1122 auth_filter_default_credentials =
1123 new net::HttpAuthFilterWhitelist(auth_server_whitelist_);
1125 net::HttpAuthFilterWhitelist* auth_filter_delegate = NULL;
1126 if (!auth_delegate_whitelist_.empty()) {
1127 auth_filter_delegate =
1128 new net::HttpAuthFilterWhitelist(auth_delegate_whitelist_);
1130 globals_->url_security_manager.reset(
1131 net::URLSecurityManager::Create(auth_filter_default_credentials,
1132 auth_filter_delegate));
1133 std::vector<std::string> supported_schemes;
1134 base::SplitString(auth_schemes_, ',', &supported_schemes);
1136 scoped_ptr<net::HttpAuthHandlerRegistryFactory> registry_factory(
1137 net::HttpAuthHandlerRegistryFactory::Create(
1138 supported_schemes, globals_->url_security_manager.get(),
1139 resolver, gssapi_library_name_, negotiate_disable_cname_lookup_,
1140 negotiate_enable_port_));
1141 return registry_factory.release();
1144 void IOThread::ClearHostCache() {
1145 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
1147 net::HostCache* host_cache = globals_->host_resolver->GetHostCache();
1148 if (host_cache)
1149 host_cache->clear();
1152 void IOThread::InitializeNetworkSessionParams(
1153 net::HttpNetworkSession::Params* params) {
1154 InitializeNetworkSessionParamsFromGlobals(*globals_, params);
1157 // static
1158 void IOThread::InitializeNetworkSessionParamsFromGlobals(
1159 const IOThread::Globals& globals,
1160 net::HttpNetworkSession::Params* params) {
1161 params->host_resolver = globals.host_resolver.get();
1162 params->cert_verifier = globals.cert_verifier.get();
1163 params->cert_policy_enforcer = globals.cert_policy_enforcer.get();
1164 params->channel_id_service = globals.system_channel_id_service.get();
1165 params->transport_security_state = globals.transport_security_state.get();
1166 params->ssl_config_service = globals.ssl_config_service.get();
1167 params->http_auth_handler_factory = globals.http_auth_handler_factory.get();
1168 params->http_server_properties =
1169 globals.http_server_properties->GetWeakPtr();
1170 params->network_delegate = globals.system_network_delegate.get();
1171 params->host_mapping_rules = globals.host_mapping_rules.get();
1172 params->ignore_certificate_errors = globals.ignore_certificate_errors;
1173 params->use_stale_while_revalidate = globals.use_stale_while_revalidate;
1174 params->testing_fixed_http_port = globals.testing_fixed_http_port;
1175 params->testing_fixed_https_port = globals.testing_fixed_https_port;
1176 globals.enable_tcp_fast_open_for_ssl.CopyToIfSet(
1177 &params->enable_tcp_fast_open_for_ssl);
1179 globals.initial_max_spdy_concurrent_streams.CopyToIfSet(
1180 &params->spdy_initial_max_concurrent_streams);
1181 globals.enable_spdy_compression.CopyToIfSet(
1182 &params->enable_spdy_compression);
1183 globals.enable_spdy_ping_based_connection_checking.CopyToIfSet(
1184 &params->enable_spdy_ping_based_connection_checking);
1185 globals.spdy_default_protocol.CopyToIfSet(
1186 &params->spdy_default_protocol);
1187 params->next_protos = globals.next_protos;
1188 globals.trusted_spdy_proxy.CopyToIfSet(&params->trusted_spdy_proxy);
1189 params->forced_spdy_exclusions = globals.forced_spdy_exclusions;
1190 globals.use_alternate_protocols.CopyToIfSet(
1191 &params->use_alternate_protocols);
1192 globals.alternative_service_probability_threshold.CopyToIfSet(
1193 &params->alternative_service_probability_threshold);
1195 globals.enable_quic.CopyToIfSet(&params->enable_quic);
1196 globals.enable_quic_for_proxies.CopyToIfSet(&params->enable_quic_for_proxies);
1197 globals.quic_always_require_handshake_confirmation.CopyToIfSet(
1198 &params->quic_always_require_handshake_confirmation);
1199 globals.quic_disable_connection_pooling.CopyToIfSet(
1200 &params->quic_disable_connection_pooling);
1201 globals.quic_load_server_info_timeout_srtt_multiplier.CopyToIfSet(
1202 &params->quic_load_server_info_timeout_srtt_multiplier);
1203 globals.quic_enable_connection_racing.CopyToIfSet(
1204 &params->quic_enable_connection_racing);
1205 globals.quic_enable_non_blocking_io.CopyToIfSet(
1206 &params->quic_enable_non_blocking_io);
1207 globals.quic_disable_disk_cache.CopyToIfSet(
1208 &params->quic_disable_disk_cache);
1209 globals.quic_max_number_of_lossy_connections.CopyToIfSet(
1210 &params->quic_max_number_of_lossy_connections);
1211 globals.quic_packet_loss_threshold.CopyToIfSet(
1212 &params->quic_packet_loss_threshold);
1213 globals.quic_socket_receive_buffer_size.CopyToIfSet(
1214 &params->quic_socket_receive_buffer_size);
1215 globals.enable_quic_port_selection.CopyToIfSet(
1216 &params->enable_quic_port_selection);
1217 globals.quic_max_packet_length.CopyToIfSet(&params->quic_max_packet_length);
1218 globals.quic_user_agent_id.CopyToIfSet(&params->quic_user_agent_id);
1219 globals.quic_supported_versions.CopyToIfSet(
1220 &params->quic_supported_versions);
1221 params->quic_connection_options = globals.quic_connection_options;
1223 globals.origin_to_force_quic_on.CopyToIfSet(
1224 &params->origin_to_force_quic_on);
1225 params->enable_user_alternate_protocol_ports =
1226 globals.enable_user_alternate_protocol_ports;
1229 base::TimeTicks IOThread::creation_time() const {
1230 return creation_time_;
1233 net::SSLConfigService* IOThread::GetSSLConfigService() {
1234 return ssl_config_service_manager_->Get();
1237 void IOThread::ChangedToOnTheRecordOnIOThread() {
1238 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
1240 // Clear the host cache to avoid showing entries from the OTR session
1241 // in about:net-internals.
1242 ClearHostCache();
1245 void IOThread::InitSystemRequestContext() {
1246 if (system_url_request_context_getter_.get())
1247 return;
1248 // If we're in unit_tests, IOThread may not be run.
1249 if (!BrowserThread::IsMessageLoopValid(BrowserThread::IO))
1250 return;
1251 system_proxy_config_service_.reset(
1252 ProxyServiceFactory::CreateProxyConfigService(
1253 pref_proxy_config_tracker_.get()));
1254 system_url_request_context_getter_ =
1255 new SystemURLRequestContextGetter(this);
1256 // Safe to post an unretained this pointer, since IOThread is
1257 // guaranteed to outlive the IO BrowserThread.
1258 BrowserThread::PostTask(
1259 BrowserThread::IO,
1260 FROM_HERE,
1261 base::Bind(&IOThread::InitSystemRequestContextOnIOThread,
1262 base::Unretained(this)));
1265 void IOThread::InitSystemRequestContextOnIOThread() {
1266 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
1267 DCHECK(!globals_->system_proxy_service.get());
1268 DCHECK(system_proxy_config_service_.get());
1270 const base::CommandLine& command_line =
1271 *base::CommandLine::ForCurrentProcess();
1272 globals_->system_proxy_service.reset(
1273 ProxyServiceFactory::CreateProxyService(
1274 net_log_,
1275 globals_->proxy_script_fetcher_context.get(),
1276 globals_->system_network_delegate.get(),
1277 system_proxy_config_service_.release(),
1278 command_line,
1279 quick_check_enabled_.GetValue()));
1281 net::HttpNetworkSession::Params system_params;
1282 InitializeNetworkSessionParams(&system_params);
1283 system_params.net_log = net_log_;
1284 system_params.proxy_service = globals_->system_proxy_service.get();
1286 globals_->system_http_transaction_factory.reset(
1287 new net::HttpNetworkLayer(
1288 new net::HttpNetworkSession(system_params)));
1289 globals_->system_url_request_job_factory.reset(
1290 new net::URLRequestJobFactoryImpl());
1291 globals_->system_request_context.reset(
1292 ConstructSystemRequestContext(globals_, net_log_));
1293 globals_->system_request_context->set_ssl_config_service(
1294 globals_->ssl_config_service.get());
1295 globals_->system_request_context->set_http_server_properties(
1296 globals_->http_server_properties->GetWeakPtr());
1299 void IOThread::UpdateDnsClientEnabled() {
1300 globals()->host_resolver->SetDnsClientEnabled(*dns_client_enabled_);
1303 void IOThread::ConfigureQuic(const base::CommandLine& command_line) {
1304 // Always fetch the field trial group to ensure it is reported correctly.
1305 // The command line flags will be associated with a group that is reported
1306 // so long as trial is actually queried.
1307 std::string group =
1308 base::FieldTrialList::FindFullName(kQuicFieldTrialName);
1309 VariationParameters params;
1310 if (!variations::GetVariationParams(kQuicFieldTrialName, &params)) {
1311 params.clear();
1314 ConfigureQuicGlobals(command_line, group, params, is_quic_allowed_by_policy_,
1315 globals_);
1318 // static
1319 void IOThread::ConfigureQuicGlobals(
1320 const base::CommandLine& command_line,
1321 base::StringPiece quic_trial_group,
1322 const VariationParameters& quic_trial_params,
1323 bool quic_allowed_by_policy,
1324 IOThread::Globals* globals) {
1325 bool enable_quic = ShouldEnableQuic(command_line, quic_trial_group,
1326 quic_allowed_by_policy);
1327 globals->enable_quic.set(enable_quic);
1328 bool enable_quic_for_proxies = ShouldEnableQuicForProxies(
1329 command_line, quic_trial_group, quic_allowed_by_policy);
1330 globals->enable_quic_for_proxies.set(enable_quic_for_proxies);
1331 if (enable_quic) {
1332 globals->quic_always_require_handshake_confirmation.set(
1333 ShouldQuicAlwaysRequireHandshakeConfirmation(quic_trial_params));
1334 globals->quic_disable_connection_pooling.set(
1335 ShouldQuicDisableConnectionPooling(quic_trial_params));
1336 int receive_buffer_size = GetQuicSocketReceiveBufferSize(quic_trial_params);
1337 if (receive_buffer_size != 0) {
1338 globals->quic_socket_receive_buffer_size.set(receive_buffer_size);
1340 float load_server_info_timeout_srtt_multiplier =
1341 GetQuicLoadServerInfoTimeoutSrttMultiplier(quic_trial_params);
1342 if (load_server_info_timeout_srtt_multiplier != 0) {
1343 globals->quic_load_server_info_timeout_srtt_multiplier.set(
1344 load_server_info_timeout_srtt_multiplier);
1346 globals->quic_enable_connection_racing.set(
1347 ShouldQuicEnableConnectionRacing(quic_trial_params));
1348 globals->quic_enable_non_blocking_io.set(
1349 ShouldQuicEnableNonBlockingIO(quic_trial_params));
1350 globals->quic_disable_disk_cache.set(
1351 ShouldQuicDisableDiskCache(quic_trial_params));
1352 int max_number_of_lossy_connections = GetQuicMaxNumberOfLossyConnections(
1353 quic_trial_params);
1354 if (max_number_of_lossy_connections != 0) {
1355 globals->quic_max_number_of_lossy_connections.set(
1356 max_number_of_lossy_connections);
1358 float packet_loss_threshold = GetQuicPacketLossThreshold(quic_trial_params);
1359 if (packet_loss_threshold != 0)
1360 globals->quic_packet_loss_threshold.set(packet_loss_threshold);
1361 globals->enable_quic_port_selection.set(
1362 ShouldEnableQuicPortSelection(command_line));
1363 globals->quic_connection_options =
1364 GetQuicConnectionOptions(command_line, quic_trial_params);
1365 if (ShouldEnableQuicPacing(command_line, quic_trial_params)) {
1366 globals->quic_connection_options.push_back(net::kPACE);
1370 size_t max_packet_length = GetQuicMaxPacketLength(command_line,
1371 quic_trial_params);
1372 if (max_packet_length != 0) {
1373 globals->quic_max_packet_length.set(max_packet_length);
1376 std::string quic_user_agent_id =
1377 chrome::VersionInfo::GetVersionStringModifier();
1378 if (!quic_user_agent_id.empty())
1379 quic_user_agent_id.push_back(' ');
1380 chrome::VersionInfo version_info;
1381 quic_user_agent_id.append(version_info.ProductNameAndVersionForUserAgent());
1382 globals->quic_user_agent_id.set(quic_user_agent_id);
1384 net::QuicVersion version = GetQuicVersion(command_line, quic_trial_params);
1385 if (version != net::QUIC_VERSION_UNSUPPORTED) {
1386 net::QuicVersionVector supported_versions;
1387 supported_versions.push_back(version);
1388 globals->quic_supported_versions.set(supported_versions);
1391 double threshold = GetAlternativeProtocolProbabilityThreshold(
1392 command_line, quic_trial_params);
1393 if (threshold >=0 && threshold <= 1) {
1394 globals->alternative_service_probability_threshold.set(threshold);
1395 globals->http_server_properties->SetAlternativeServiceProbabilityThreshold(
1396 threshold);
1399 if (command_line.HasSwitch(switches::kOriginToForceQuicOn)) {
1400 net::HostPortPair quic_origin =
1401 net::HostPortPair::FromString(
1402 command_line.GetSwitchValueASCII(switches::kOriginToForceQuicOn));
1403 if (!quic_origin.IsEmpty()) {
1404 globals->origin_to_force_quic_on.set(quic_origin);
1409 bool IOThread::ShouldEnableQuic(const base::CommandLine& command_line,
1410 base::StringPiece quic_trial_group,
1411 bool quic_allowed_by_policy) {
1412 if (command_line.HasSwitch(switches::kDisableQuic) || !quic_allowed_by_policy)
1413 return false;
1415 if (command_line.HasSwitch(switches::kEnableQuic))
1416 return true;
1418 return quic_trial_group.starts_with(kQuicFieldTrialEnabledGroupName) ||
1419 quic_trial_group.starts_with(kQuicFieldTrialHttpsEnabledGroupName);
1422 // static
1423 bool IOThread::ShouldEnableQuicForProxies(const base::CommandLine& command_line,
1424 base::StringPiece quic_trial_group,
1425 bool quic_allowed_by_policy) {
1426 return ShouldEnableQuic(
1427 command_line, quic_trial_group, quic_allowed_by_policy) ||
1428 ShouldEnableQuicForDataReductionProxy();
1431 // static
1432 bool IOThread::ShouldEnableQuicForDataReductionProxy() {
1433 const base::CommandLine& command_line =
1434 *base::CommandLine::ForCurrentProcess();
1436 if (command_line.HasSwitch(switches::kDisableQuic))
1437 return false;
1439 return data_reduction_proxy::DataReductionProxyParams::
1440 IsIncludedInQuicFieldTrial();
1443 bool IOThread::ShouldEnableQuicPortSelection(
1444 const base::CommandLine& command_line) {
1445 if (command_line.HasSwitch(switches::kDisableQuicPortSelection))
1446 return false;
1448 if (command_line.HasSwitch(switches::kEnableQuicPortSelection))
1449 return true;
1451 return false; // Default to disabling port selection on all channels.
1454 bool IOThread::ShouldEnableQuicPacing(
1455 const base::CommandLine& command_line,
1456 const VariationParameters& quic_trial_params) {
1457 if (command_line.HasSwitch(switches::kEnableQuicPacing))
1458 return true;
1460 if (command_line.HasSwitch(switches::kDisableQuicPacing))
1461 return false;
1463 return LowerCaseEqualsASCII(
1464 GetVariationParam(quic_trial_params, "enable_pacing"),
1465 "true");
1468 net::QuicTagVector IOThread::GetQuicConnectionOptions(
1469 const base::CommandLine& command_line,
1470 const VariationParameters& quic_trial_params) {
1471 if (command_line.HasSwitch(switches::kQuicConnectionOptions)) {
1472 return net::QuicUtils::ParseQuicConnectionOptions(
1473 command_line.GetSwitchValueASCII(switches::kQuicConnectionOptions));
1476 VariationParameters::const_iterator it =
1477 quic_trial_params.find("connection_options");
1478 if (it == quic_trial_params.end()) {
1479 return net::QuicTagVector();
1482 return net::QuicUtils::ParseQuicConnectionOptions(it->second);
1485 // static
1486 double IOThread::GetAlternativeProtocolProbabilityThreshold(
1487 const base::CommandLine& command_line,
1488 const VariationParameters& quic_trial_params) {
1489 double value;
1490 if (command_line.HasSwitch(
1491 switches::kAlternativeServiceProbabilityThreshold)) {
1492 if (base::StringToDouble(
1493 command_line.GetSwitchValueASCII(
1494 switches::kAlternativeServiceProbabilityThreshold),
1495 &value)) {
1496 return value;
1499 if (command_line.HasSwitch(switches::kEnableQuic)) {
1500 return 0;
1502 // TODO(bnc): Remove when new parameter name rolls out and server
1503 // configuration is changed.
1504 if (base::StringToDouble(
1505 GetVariationParam(quic_trial_params,
1506 "alternate_protocol_probability_threshold"),
1507 &value)) {
1508 return value;
1510 if (base::StringToDouble(
1511 GetVariationParam(quic_trial_params,
1512 "alternative_service_probability_threshold"),
1513 &value)) {
1514 return value;
1516 return -1;
1519 // static
1520 bool IOThread::ShouldQuicAlwaysRequireHandshakeConfirmation(
1521 const VariationParameters& quic_trial_params) {
1522 return LowerCaseEqualsASCII(
1523 GetVariationParam(quic_trial_params,
1524 "always_require_handshake_confirmation"),
1525 "true");
1528 // static
1529 bool IOThread::ShouldQuicDisableConnectionPooling(
1530 const VariationParameters& quic_trial_params) {
1531 return LowerCaseEqualsASCII(
1532 GetVariationParam(quic_trial_params, "disable_connection_pooling"),
1533 "true");
1536 // static
1537 float IOThread::GetQuicLoadServerInfoTimeoutSrttMultiplier(
1538 const VariationParameters& quic_trial_params) {
1539 double value;
1540 if (base::StringToDouble(GetVariationParam(quic_trial_params,
1541 "load_server_info_time_to_srtt"),
1542 &value)) {
1543 return (float)value;
1545 return 0.0f;
1548 // static
1549 bool IOThread::ShouldQuicEnableConnectionRacing(
1550 const VariationParameters& quic_trial_params) {
1551 return LowerCaseEqualsASCII(
1552 GetVariationParam(quic_trial_params, "enable_connection_racing"),
1553 "true");
1556 // static
1557 bool IOThread::ShouldQuicEnableNonBlockingIO(
1558 const VariationParameters& quic_trial_params) {
1559 return LowerCaseEqualsASCII(
1560 GetVariationParam(quic_trial_params, "enable_non_blocking_io"),
1561 "true");
1564 // static
1565 bool IOThread::ShouldQuicDisableDiskCache(
1566 const VariationParameters& quic_trial_params) {
1567 return LowerCaseEqualsASCII(
1568 GetVariationParam(quic_trial_params, "disable_disk_cache"), "true");
1571 // static
1572 int IOThread::GetQuicMaxNumberOfLossyConnections(
1573 const VariationParameters& quic_trial_params) {
1574 int value;
1575 if (base::StringToInt(GetVariationParam(quic_trial_params,
1576 "max_number_of_lossy_connections"),
1577 &value)) {
1578 return value;
1580 return 0;
1583 // static
1584 float IOThread::GetQuicPacketLossThreshold(
1585 const VariationParameters& quic_trial_params) {
1586 double value;
1587 if (base::StringToDouble(GetVariationParam(quic_trial_params,
1588 "packet_loss_threshold"),
1589 &value)) {
1590 return (float)value;
1592 return 0.0f;
1595 // static
1596 int IOThread::GetQuicSocketReceiveBufferSize(
1597 const VariationParameters& quic_trial_params) {
1598 int value;
1599 if (base::StringToInt(GetVariationParam(quic_trial_params,
1600 "receive_buffer_size"),
1601 &value)) {
1602 return value;
1604 return 0;
1607 // static
1608 size_t IOThread::GetQuicMaxPacketLength(
1609 const base::CommandLine& command_line,
1610 const VariationParameters& quic_trial_params) {
1611 if (command_line.HasSwitch(switches::kQuicMaxPacketLength)) {
1612 unsigned value;
1613 if (!base::StringToUint(
1614 command_line.GetSwitchValueASCII(switches::kQuicMaxPacketLength),
1615 &value)) {
1616 return 0;
1618 return value;
1621 unsigned value;
1622 if (base::StringToUint(GetVariationParam(quic_trial_params,
1623 "max_packet_length"),
1624 &value)) {
1625 return value;
1627 return 0;
1630 // static
1631 net::QuicVersion IOThread::GetQuicVersion(
1632 const base::CommandLine& command_line,
1633 const VariationParameters& quic_trial_params) {
1634 if (command_line.HasSwitch(switches::kQuicVersion)) {
1635 return ParseQuicVersion(
1636 command_line.GetSwitchValueASCII(switches::kQuicVersion));
1639 return ParseQuicVersion(GetVariationParam(quic_trial_params, "quic_version"));
1642 // static
1643 net::QuicVersion IOThread::ParseQuicVersion(const std::string& quic_version) {
1644 net::QuicVersionVector supported_versions = net::QuicSupportedVersions();
1645 for (size_t i = 0; i < supported_versions.size(); ++i) {
1646 net::QuicVersion version = supported_versions[i];
1647 if (net::QuicVersionToString(version) == quic_version) {
1648 return version;
1652 return net::QUIC_VERSION_UNSUPPORTED;