Added affiliation IDs, that in future will be used to determine user affiliation...
[chromium-blink-merge.git] / chrome / browser / io_thread.cc
blob1db5edd9c3d458067caed6f8c37fe56533535816
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 "content/public/common/user_agent.h"
50 #include "net/base/host_mapping_rules.h"
51 #include "net/base/net_util.h"
52 #include "net/base/network_quality_estimator.h"
53 #include "net/base/sdch_manager.h"
54 #include "net/cert/cert_policy_enforcer.h"
55 #include "net/cert/cert_verifier.h"
56 #include "net/cert/cert_verify_proc.h"
57 #include "net/cert/ct_known_logs.h"
58 #include "net/cert/ct_known_logs_static.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_backoff_manager.h"
88 #include "net/url_request/url_request_context.h"
89 #include "net/url_request/url_request_context_getter.h"
90 #include "net/url_request/url_request_job_factory_impl.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_CERTS) || defined(OS_IOS)
102 #include "net/cert_net/nss_ocsp.h"
103 #endif
105 #if defined(OS_ANDROID)
106 #include "base/android/build_info.h"
107 #endif
109 #if defined(OS_CHROMEOS)
110 #include "chrome/browser/chromeos/net/cert_verify_proc_chromeos.h"
111 #include "chromeos/network/host_resolver_impl_chromeos.h"
112 #endif
114 using content::BrowserThread;
116 class SafeBrowsingURLRequestContext;
118 // The IOThread object must outlive any tasks posted to the IO thread before the
119 // Quit task, so base::Bind() calls are not refcounted.
121 namespace {
123 const char kTCPFastOpenFieldTrialName[] = "TCPFastOpen";
124 const char kTCPFastOpenHttpsEnabledGroupName[] = "HttpsEnabled";
126 const char kQuicFieldTrialName[] = "QUIC";
127 const char kQuicFieldTrialEnabledGroupName[] = "Enabled";
128 const char kQuicFieldTrialHttpsEnabledGroupName[] = "HttpsEnabled";
130 // The SPDY trial composes two different trial plus control groups:
131 // * A "holdback" group with SPDY disabled, and corresponding control
132 // (SPDY/3.1). The primary purpose of the holdback group is to encourage site
133 // operators to do feature detection rather than UA-sniffing. As such, this
134 // trial runs continuously.
135 // * A SPDY/4 experiment, for SPDY/4 (aka HTTP/2) vs SPDY/3.1 comparisons and
136 // eventual SPDY/4 deployment.
137 const char kSpdyFieldTrialName[] = "SPDY";
138 const char kSpdyFieldTrialHoldbackGroupNamePrefix[] = "SpdyDisabled";
139 const char kSpdyFieldTrialSpdy31GroupNamePrefix[] = "Spdy31Enabled";
140 const char kSpdyFieldTrialSpdy4GroupNamePrefix[] = "Spdy4Enabled";
141 const char kSpdyFieldTrialParametrizedPrefix[] = "Parametrized";
143 // Field trial for network quality estimator. Seeds RTT and downstream
144 // throughput observations with values that correspond to the connection type
145 // determined by the operating system.
146 const char kNetworkQualityEstimatorFieldTrialName[] = "NetworkQualityEstimator";
148 #if defined(OS_MACOSX) && !defined(OS_IOS)
149 void ObserveKeychainEvents() {
150 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
151 net::CertDatabase::GetInstance()->SetMessageLoopForKeychainEvents();
153 #endif
155 // Used for the "system" URLRequestContext.
156 class SystemURLRequestContext : public net::URLRequestContext {
157 public:
158 SystemURLRequestContext() {
159 #if defined(USE_NSS_CERTS) || defined(OS_IOS)
160 net::SetURLRequestContextForNSSHttpIO(this);
161 #endif
164 private:
165 ~SystemURLRequestContext() override {
166 AssertNoURLRequests();
167 #if defined(USE_NSS_CERTS) || defined(OS_IOS)
168 net::SetURLRequestContextForNSSHttpIO(NULL);
169 #endif
173 scoped_ptr<net::HostResolver> CreateGlobalHostResolver(net::NetLog* net_log) {
174 TRACE_EVENT0("startup", "IOThread::CreateGlobalHostResolver");
175 const base::CommandLine& command_line =
176 *base::CommandLine::ForCurrentProcess();
178 net::HostResolver::Options options;
180 // Use the retry attempts override from the command-line, if any.
181 if (command_line.HasSwitch(switches::kHostResolverRetryAttempts)) {
182 std::string s =
183 command_line.GetSwitchValueASCII(switches::kHostResolverRetryAttempts);
184 // Parse the switch (it should be a non-negative integer).
185 int n;
186 if (base::StringToInt(s, &n) && n >= 0) {
187 options.max_retry_attempts = static_cast<size_t>(n);
188 } else {
189 LOG(ERROR) << "Invalid switch for host resolver retry attempts: " << s;
193 scoped_ptr<net::HostResolver> global_host_resolver;
194 #if defined OS_CHROMEOS
195 global_host_resolver =
196 chromeos::HostResolverImplChromeOS::CreateSystemResolver(options,
197 net_log);
198 #else
199 global_host_resolver =
200 net::HostResolver::CreateSystemResolver(options, net_log);
201 #endif
203 // If hostname remappings were specified on the command-line, layer these
204 // rules on top of the real host resolver. This allows forwarding all requests
205 // through a designated test server.
206 if (!command_line.HasSwitch(switches::kHostResolverRules))
207 return global_host_resolver.Pass();
209 scoped_ptr<net::MappedHostResolver> remapped_resolver(
210 new net::MappedHostResolver(global_host_resolver.Pass()));
211 remapped_resolver->SetRulesFromString(
212 command_line.GetSwitchValueASCII(switches::kHostResolverRules));
213 return remapped_resolver.Pass();
216 // TODO(willchan): Remove proxy script fetcher context since it's not necessary
217 // now that I got rid of refcounting URLRequestContexts.
218 // See IOThread::Globals for details.
219 net::URLRequestContext*
220 ConstructProxyScriptFetcherContext(IOThread::Globals* globals,
221 net::NetLog* net_log) {
222 net::URLRequestContext* context = new net::URLRequestContext;
223 context->set_net_log(net_log);
224 context->set_host_resolver(globals->host_resolver.get());
225 context->set_cert_verifier(globals->cert_verifier.get());
226 context->set_transport_security_state(
227 globals->transport_security_state.get());
228 context->set_cert_transparency_verifier(
229 globals->cert_transparency_verifier.get());
230 context->set_http_auth_handler_factory(
231 globals->http_auth_handler_factory.get());
232 context->set_proxy_service(globals->proxy_script_fetcher_proxy_service.get());
233 context->set_http_transaction_factory(
234 globals->proxy_script_fetcher_http_transaction_factory.get());
235 context->set_job_factory(
236 globals->proxy_script_fetcher_url_request_job_factory.get());
237 context->set_cookie_store(globals->system_cookie_store.get());
238 context->set_channel_id_service(
239 globals->system_channel_id_service.get());
240 context->set_network_delegate(globals->system_network_delegate.get());
241 context->set_http_user_agent_settings(
242 globals->http_user_agent_settings.get());
243 // TODO(rtenneti): We should probably use HttpServerPropertiesManager for the
244 // system URLRequestContext too. There's no reason this should be tied to a
245 // profile.
246 return context;
249 net::URLRequestContext*
250 ConstructSystemRequestContext(IOThread::Globals* globals,
251 net::NetLog* net_log) {
252 net::URLRequestContext* context = new SystemURLRequestContext;
253 context->set_net_log(net_log);
254 context->set_host_resolver(globals->host_resolver.get());
255 context->set_cert_verifier(globals->cert_verifier.get());
256 context->set_transport_security_state(
257 globals->transport_security_state.get());
258 context->set_cert_transparency_verifier(
259 globals->cert_transparency_verifier.get());
260 context->set_http_auth_handler_factory(
261 globals->http_auth_handler_factory.get());
262 context->set_proxy_service(globals->system_proxy_service.get());
263 context->set_http_transaction_factory(
264 globals->system_http_transaction_factory.get());
265 context->set_job_factory(globals->system_url_request_job_factory.get());
266 context->set_cookie_store(globals->system_cookie_store.get());
267 context->set_channel_id_service(
268 globals->system_channel_id_service.get());
269 context->set_network_delegate(globals->system_network_delegate.get());
270 context->set_http_user_agent_settings(
271 globals->http_user_agent_settings.get());
272 context->set_network_quality_estimator(
273 globals->network_quality_estimator.get());
274 context->set_backoff_manager(globals->url_request_backoff_manager.get());
275 return context;
278 int GetSwitchValueAsInt(const base::CommandLine& command_line,
279 const std::string& switch_name) {
280 int value;
281 if (!base::StringToInt(command_line.GetSwitchValueASCII(switch_name),
282 &value)) {
283 return 0;
285 return value;
288 // Returns the value associated with |key| in |params| or "" if the
289 // key is not present in the map.
290 const std::string& GetVariationParam(
291 const std::map<std::string, std::string>& params,
292 const std::string& key) {
293 std::map<std::string, std::string>::const_iterator it = params.find(key);
294 if (it == params.end())
295 return base::EmptyString();
297 return it->second;
300 // Parse kUseSpdy command line flag options, which may contain the following:
302 // "off" : Disables SPDY support entirely.
303 // "no-ping" : Disables SPDY ping connection testing.
304 // "exclude=<host>" : Disables SPDY support for the host <host>.
305 // "no-compress" : Disables SPDY header compression.
306 // "no-alt-protocols : Disables alternate protocol support.
307 // "init-max-streams=<limit>" : Specifies the maximum number of concurrent
308 // streams for a SPDY session, unless the
309 // specifies a different value via SETTINGS.
310 void ConfigureSpdyGlobalsFromUseSpdyArgument(const std::string& mode,
311 IOThread::Globals* globals) {
312 static const char kOff[] = "off";
313 static const char kDisablePing[] = "no-ping";
314 static const char kExclude[] = "exclude"; // Hosts to exclude
315 static const char kDisableCompression[] = "no-compress";
316 static const char kDisableAltProtocols[] = "no-alt-protocols";
317 static const char kInitialMaxConcurrentStreams[] = "init-max-streams";
319 for (const base::StringPiece& element : base::SplitStringPiece(
320 mode, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) {
321 std::vector<base::StringPiece> name_value = base::SplitStringPiece(
322 element, "=", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
323 const base::StringPiece option =
324 name_value.size() > 0 ? name_value[0] : base::StringPiece();
325 const base::StringPiece value =
326 name_value.size() > 1 ? name_value[1] : base::StringPiece();
328 if (option == kOff) {
329 net::HttpStreamFactory::set_spdy_enabled(false);
330 continue;
332 if (option == kDisablePing) {
333 globals->enable_spdy_ping_based_connection_checking.set(false);
334 continue;
336 if (option == kExclude) {
337 globals->forced_spdy_exclusions.insert(
338 net::HostPortPair::FromURL(GURL(value.as_string())));
339 continue;
341 if (option == kDisableCompression) {
342 globals->enable_spdy_compression.set(false);
343 continue;
345 if (option == kDisableAltProtocols) {
346 globals->use_alternate_protocols.set(false);
347 continue;
349 if (option == kInitialMaxConcurrentStreams) {
350 int streams;
351 if (base::StringToInt(value, &streams)) {
352 globals->initial_max_spdy_concurrent_streams.set(streams);
353 continue;
356 LOG(DFATAL) << "Unrecognized spdy option: " << option.as_string();
360 } // namespace
362 class IOThread::LoggingNetworkChangeObserver
363 : public net::NetworkChangeNotifier::IPAddressObserver,
364 public net::NetworkChangeNotifier::ConnectionTypeObserver,
365 public net::NetworkChangeNotifier::NetworkChangeObserver {
366 public:
367 // |net_log| must remain valid throughout our lifetime.
368 explicit LoggingNetworkChangeObserver(net::NetLog* net_log)
369 : net_log_(net_log) {
370 net::NetworkChangeNotifier::AddIPAddressObserver(this);
371 net::NetworkChangeNotifier::AddConnectionTypeObserver(this);
372 net::NetworkChangeNotifier::AddNetworkChangeObserver(this);
375 ~LoggingNetworkChangeObserver() override {
376 net::NetworkChangeNotifier::RemoveIPAddressObserver(this);
377 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
378 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this);
381 // NetworkChangeNotifier::IPAddressObserver implementation.
382 void OnIPAddressChanged() override {
383 VLOG(1) << "Observed a change to the network IP addresses";
385 net_log_->AddGlobalEntry(net::NetLog::TYPE_NETWORK_IP_ADDRESSES_CHANGED);
388 // NetworkChangeNotifier::ConnectionTypeObserver implementation.
389 void OnConnectionTypeChanged(
390 net::NetworkChangeNotifier::ConnectionType type) override {
391 std::string type_as_string =
392 net::NetworkChangeNotifier::ConnectionTypeToString(type);
394 VLOG(1) << "Observed a change to network connectivity state "
395 << type_as_string;
397 net_log_->AddGlobalEntry(
398 net::NetLog::TYPE_NETWORK_CONNECTIVITY_CHANGED,
399 net::NetLog::StringCallback("new_connection_type", &type_as_string));
402 // NetworkChangeNotifier::NetworkChangeObserver implementation.
403 void OnNetworkChanged(
404 net::NetworkChangeNotifier::ConnectionType type) override {
405 std::string type_as_string =
406 net::NetworkChangeNotifier::ConnectionTypeToString(type);
408 VLOG(1) << "Observed a network change to state " << type_as_string;
410 net_log_->AddGlobalEntry(
411 net::NetLog::TYPE_NETWORK_CHANGED,
412 net::NetLog::StringCallback("new_connection_type", &type_as_string));
415 private:
416 net::NetLog* net_log_;
417 DISALLOW_COPY_AND_ASSIGN(LoggingNetworkChangeObserver);
420 class SystemURLRequestContextGetter : public net::URLRequestContextGetter {
421 public:
422 explicit SystemURLRequestContextGetter(IOThread* io_thread);
424 // Implementation for net::UrlRequestContextGetter.
425 net::URLRequestContext* GetURLRequestContext() override;
426 scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner()
427 const override;
429 protected:
430 ~SystemURLRequestContextGetter() override;
432 private:
433 IOThread* const io_thread_; // Weak pointer, owned by BrowserProcess.
434 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
436 base::debug::LeakTracker<SystemURLRequestContextGetter> leak_tracker_;
439 SystemURLRequestContextGetter::SystemURLRequestContextGetter(
440 IOThread* io_thread)
441 : io_thread_(io_thread),
442 network_task_runner_(
443 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)) {
446 SystemURLRequestContextGetter::~SystemURLRequestContextGetter() {}
448 net::URLRequestContext* SystemURLRequestContextGetter::GetURLRequestContext() {
449 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
450 DCHECK(io_thread_->globals()->system_request_context.get());
452 return io_thread_->globals()->system_request_context.get();
455 scoped_refptr<base::SingleThreadTaskRunner>
456 SystemURLRequestContextGetter::GetNetworkTaskRunner() const {
457 return network_task_runner_;
460 IOThread::Globals::
461 SystemRequestContextLeakChecker::SystemRequestContextLeakChecker(
462 Globals* globals)
463 : globals_(globals) {
464 DCHECK(globals_);
467 IOThread::Globals::
468 SystemRequestContextLeakChecker::~SystemRequestContextLeakChecker() {
469 if (globals_->system_request_context.get())
470 globals_->system_request_context->AssertNoURLRequests();
473 IOThread::Globals::Globals()
474 : system_request_context_leak_checker(this),
475 ignore_certificate_errors(false),
476 testing_fixed_http_port(0),
477 testing_fixed_https_port(0),
478 enable_user_alternate_protocol_ports(false) {
481 IOThread::Globals::~Globals() {}
483 // |local_state| is passed in explicitly in order to (1) reduce implicit
484 // dependencies and (2) make IOThread more flexible for testing.
485 IOThread::IOThread(
486 PrefService* local_state,
487 policy::PolicyService* policy_service,
488 ChromeNetLog* net_log,
489 extensions::EventRouterForwarder* extension_event_router_forwarder)
490 : net_log_(net_log),
491 #if defined(ENABLE_EXTENSIONS)
492 extension_event_router_forwarder_(extension_event_router_forwarder),
493 #endif
494 globals_(NULL),
495 is_spdy_disabled_by_policy_(false),
496 is_quic_allowed_by_policy_(true),
497 creation_time_(base::TimeTicks::Now()),
498 weak_factory_(this) {
499 auth_schemes_ = local_state->GetString(prefs::kAuthSchemes);
500 negotiate_disable_cname_lookup_ = local_state->GetBoolean(
501 prefs::kDisableAuthNegotiateCnameLookup);
502 negotiate_enable_port_ = local_state->GetBoolean(
503 prefs::kEnableAuthNegotiatePort);
504 auth_server_whitelist_ = local_state->GetString(prefs::kAuthServerWhitelist);
505 auth_delegate_whitelist_ = local_state->GetString(
506 prefs::kAuthNegotiateDelegateWhitelist);
507 gssapi_library_name_ = local_state->GetString(prefs::kGSSAPILibraryName);
508 auth_android_negotiate_account_type_ =
509 local_state->GetString(prefs::kAuthAndroidNegotiateAccountType);
510 pref_proxy_config_tracker_.reset(
511 ProxyServiceFactory::CreatePrefProxyConfigTrackerOfLocalState(
512 local_state));
513 ChromeNetworkDelegate::InitializePrefsOnUIThread(
514 &system_enable_referrers_,
515 NULL,
516 NULL,
517 NULL,
518 local_state);
519 ssl_config_service_manager_.reset(
520 SSLConfigServiceManager::CreateDefaultManager(local_state));
522 base::Value* dns_client_enabled_default = new base::FundamentalValue(
523 chrome_browser_net::ConfigureAsyncDnsFieldTrial());
524 local_state->SetDefaultPrefValue(prefs::kBuiltInDnsClientEnabled,
525 dns_client_enabled_default);
526 chrome_browser_net::LogAsyncDnsPrefSource(
527 local_state->FindPreference(prefs::kBuiltInDnsClientEnabled));
529 dns_client_enabled_.Init(prefs::kBuiltInDnsClientEnabled,
530 local_state,
531 base::Bind(&IOThread::UpdateDnsClientEnabled,
532 base::Unretained(this)));
533 dns_client_enabled_.MoveToThread(
534 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
536 quick_check_enabled_.Init(prefs::kQuickCheckEnabled,
537 local_state);
538 quick_check_enabled_.MoveToThread(
539 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
541 #if defined(ENABLE_CONFIGURATION_POLICY)
542 is_spdy_disabled_by_policy_ = policy_service->GetPolicies(
543 policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME, std::string())).Get(
544 policy::key::kDisableSpdy) != NULL;
546 const base::Value* value = policy_service->GetPolicies(
547 policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME,
548 std::string())).GetValue(policy::key::kQuicAllowed);
549 if (value)
550 value->GetAsBoolean(&is_quic_allowed_by_policy_);
551 #endif // ENABLE_CONFIGURATION_POLICY
553 BrowserThread::SetDelegate(BrowserThread::IO, this);
556 IOThread::~IOThread() {
557 // This isn't needed for production code, but in tests, IOThread may
558 // be multiply constructed.
559 BrowserThread::SetDelegate(BrowserThread::IO, NULL);
561 pref_proxy_config_tracker_->DetachFromPrefService();
562 DCHECK(!globals_);
565 IOThread::Globals* IOThread::globals() {
566 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
567 return globals_;
570 void IOThread::SetGlobalsForTesting(Globals* globals) {
571 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
572 DCHECK(!globals || !globals_);
573 globals_ = globals;
576 ChromeNetLog* IOThread::net_log() {
577 return net_log_;
580 void IOThread::ChangedToOnTheRecord() {
581 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
582 BrowserThread::PostTask(
583 BrowserThread::IO,
584 FROM_HERE,
585 base::Bind(&IOThread::ChangedToOnTheRecordOnIOThread,
586 base::Unretained(this)));
589 net::URLRequestContextGetter* IOThread::system_url_request_context_getter() {
590 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
591 if (!system_url_request_context_getter_.get()) {
592 InitSystemRequestContext();
594 return system_url_request_context_getter_.get();
597 void IOThread::Init() {
598 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
599 // is fixed.
600 tracked_objects::ScopedTracker tracking_profile1(
601 FROM_HERE_WITH_EXPLICIT_FUNCTION("466432 IOThread::InitAsync::Start"));
602 TRACE_EVENT0("startup", "IOThread::InitAsync");
603 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
605 #if defined(USE_NSS_CERTS) || defined(OS_IOS)
606 net::SetMessageLoopForNSSHttpIO();
607 #endif
609 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
610 // is fixed.
611 tracked_objects::ScopedTracker tracking_profile2(
612 FROM_HERE_WITH_EXPLICIT_FUNCTION(
613 "466432 IOThread::InitAsync::CommandLineForCurrentProcess"));
614 const base::CommandLine& command_line =
615 *base::CommandLine::ForCurrentProcess();
617 DCHECK(!globals_);
618 globals_ = new Globals;
620 // Add an observer that will emit network change events to the ChromeNetLog.
621 // Assuming NetworkChangeNotifier dispatches in FIFO order, we should be
622 // logging the network change before other IO thread consumers respond to it.
623 network_change_observer_.reset(
624 new LoggingNetworkChangeObserver(net_log_));
626 // Setup the HistogramWatcher to run on the IO thread.
627 net::NetworkChangeNotifier::InitHistogramWatcher();
629 #if defined(ENABLE_EXTENSIONS)
630 globals_->extension_event_router_forwarder =
631 extension_event_router_forwarder_;
632 #endif
634 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
635 // is fixed.
636 tracked_objects::ScopedTracker tracking_profile3(
637 FROM_HERE_WITH_EXPLICIT_FUNCTION(
638 "466432 IOThread::InitAsync::ChromeNetworkDelegate"));
639 scoped_ptr<ChromeNetworkDelegate> chrome_network_delegate(
640 new ChromeNetworkDelegate(extension_event_router_forwarder(),
641 &system_enable_referrers_));
643 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
644 // is fixed.
645 tracked_objects::ScopedTracker tracking_profile4(
646 FROM_HERE_WITH_EXPLICIT_FUNCTION(
647 "466432 IOThread::InitAsync::CreateGlobalHostResolver"));
648 globals_->system_network_delegate = chrome_network_delegate.Pass();
649 globals_->host_resolver = CreateGlobalHostResolver(net_log_);
651 std::map<std::string, std::string> network_quality_estimator_params;
652 variations::GetVariationParams(kNetworkQualityEstimatorFieldTrialName,
653 &network_quality_estimator_params);
654 globals_->network_quality_estimator.reset(
655 new net::NetworkQualityEstimator(network_quality_estimator_params));
657 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
658 // is fixed.
659 tracked_objects::ScopedTracker tracking_profile5(
660 FROM_HERE_WITH_EXPLICIT_FUNCTION(
661 "466432 IOThread::InitAsync::UpdateDnsClientEnabled::Start"));
662 UpdateDnsClientEnabled();
663 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
664 // is fixed.
665 tracked_objects::ScopedTracker tracking_profile6(
666 FROM_HERE_WITH_EXPLICIT_FUNCTION(
667 "466432 IOThread::InitAsync::UpdateDnsClientEnabled::End"));
668 #if defined(OS_CHROMEOS)
669 // Creates a CertVerifyProc that doesn't allow any profile-provided certs.
670 globals_->cert_verifier.reset(new net::MultiThreadedCertVerifier(
671 new chromeos::CertVerifyProcChromeOS()));
672 #else
673 globals_->cert_verifier.reset(new net::MultiThreadedCertVerifier(
674 net::CertVerifyProc::CreateDefault()));
675 #endif
677 globals_->transport_security_state.reset(new net::TransportSecurityState());
679 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
680 // is fixed.
681 tracked_objects::ScopedTracker tracking_profile8(
682 FROM_HERE_WITH_EXPLICIT_FUNCTION(
683 "466432 IOThread::InitAsync::CreateLogVerifiers::Start"));
684 std::vector<scoped_refptr<net::CTLogVerifier>> ct_logs(
685 net::ct::CreateLogVerifiersForKnownLogs());
687 // Add logs from command line
688 if (command_line.HasSwitch(switches::kCertificateTransparencyLog)) {
689 std::string switch_value = command_line.GetSwitchValueASCII(
690 switches::kCertificateTransparencyLog);
691 for (const base::StringPiece& curr_log : base::SplitStringPiece(
692 switch_value, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) {
693 std::vector<std::string> log_metadata = base::SplitString(
694 curr_log, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
695 CHECK_GE(log_metadata.size(), 3u)
696 << "CT log metadata missing: Switch format is "
697 << "'description:base64_key:url_without_schema'.";
698 std::string log_description(log_metadata[0]);
699 std::string log_url(std::string("https://") + log_metadata[2]);
700 std::string ct_public_key_data;
701 CHECK(base::Base64Decode(log_metadata[1], &ct_public_key_data))
702 << "Unable to decode CT public key.";
703 scoped_refptr<net::CTLogVerifier> external_log_verifier(
704 net::CTLogVerifier::Create(ct_public_key_data, log_description,
705 log_url));
706 CHECK(external_log_verifier) << "Unable to parse CT public key.";
707 VLOG(1) << "Adding log with description " << log_description;
708 ct_logs.push_back(external_log_verifier);
712 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
713 // is fixed.
714 tracked_objects::ScopedTracker tracking_profile9(
715 FROM_HERE_WITH_EXPLICIT_FUNCTION(
716 "466432 IOThread::InitAsync::CreateLogVerifiers::End"));
717 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
718 // is fixed.
719 tracked_objects::ScopedTracker tracking_profile7(
720 FROM_HERE_WITH_EXPLICIT_FUNCTION(
721 "466432 IOThread::InitAsync::CreateMultiLogVerifier"));
722 net::MultiLogCTVerifier* ct_verifier = new net::MultiLogCTVerifier();
723 globals_->cert_transparency_verifier.reset(ct_verifier);
724 // Add built-in logs
725 ct_verifier->AddLogs(ct_logs);
727 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
728 // is fixed.
729 tracked_objects::ScopedTracker tracking_profile10(
730 FROM_HERE_WITH_EXPLICIT_FUNCTION(
731 "466432 IOThread::InitAsync::CertPolicyEnforcer"));
732 net::CertPolicyEnforcer* policy_enforcer = new net::CertPolicyEnforcer;
733 globals_->cert_policy_enforcer.reset(policy_enforcer);
735 globals_->ssl_config_service = GetSSLConfigService();
737 globals_->http_auth_handler_factory.reset(CreateDefaultAuthHandlerFactory(
738 globals_->host_resolver.get()));
739 globals_->http_server_properties.reset(new net::HttpServerPropertiesImpl());
740 // For the ProxyScriptFetcher, we use a direct ProxyService.
741 globals_->proxy_script_fetcher_proxy_service.reset(
742 net::ProxyService::CreateDirectWithNetLog(net_log_));
743 // In-memory cookie store.
744 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
745 // is fixed.
746 tracked_objects::ScopedTracker tracking_profile11(
747 FROM_HERE_WITH_EXPLICIT_FUNCTION(
748 "466432 IOThread::InitAsync::CreateCookieStore::Start"));
749 globals_->system_cookie_store =
750 content::CreateCookieStore(content::CookieStoreConfig());
751 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
752 // is fixed.
753 tracked_objects::ScopedTracker tracking_profile12(
754 FROM_HERE_WITH_EXPLICIT_FUNCTION(
755 "466432 IOThread::InitAsync::CreateCookieStore::End"));
756 // In-memory channel ID store.
757 globals_->system_channel_id_service.reset(
758 new net::ChannelIDService(
759 new net::DefaultChannelIDStore(NULL),
760 base::WorkerPool::GetTaskRunner(true)));
761 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
762 // is fixed.
763 tracked_objects::ScopedTracker tracking_profile12_1(
764 FROM_HERE_WITH_EXPLICIT_FUNCTION(
765 "466432 IOThread::InitAsync::CreateDnsProbeService"));
766 globals_->dns_probe_service.reset(new chrome_browser_net::DnsProbeService());
767 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
768 // is fixed.
769 tracked_objects::ScopedTracker tracking_profile12_2(
770 FROM_HERE_WITH_EXPLICIT_FUNCTION(
771 "466432 IOThread::InitAsync::CreateHostMappingRules"));
772 globals_->host_mapping_rules.reset(new net::HostMappingRules());
773 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
774 // is fixed.
775 tracked_objects::ScopedTracker tracking_profile12_3(
776 FROM_HERE_WITH_EXPLICIT_FUNCTION(
777 "466432 IOThread::InitAsync::CreateHTTPUserAgentSettings"));
778 globals_->http_user_agent_settings.reset(
779 new net::StaticHttpUserAgentSettings(std::string(), GetUserAgent()));
780 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
781 // is fixed.
782 tracked_objects::ScopedTracker tracking_profile12_4(
783 FROM_HERE_WITH_EXPLICIT_FUNCTION(
784 "466432 IOThread::InitAsync::CommandLineConfiguration"));
785 if (command_line.HasSwitch(switches::kHostRules)) {
786 TRACE_EVENT_BEGIN0("startup", "IOThread::InitAsync:SetRulesFromString");
787 globals_->host_mapping_rules->SetRulesFromString(
788 command_line.GetSwitchValueASCII(switches::kHostRules));
789 TRACE_EVENT_END0("startup", "IOThread::InitAsync:SetRulesFromString");
791 if (command_line.HasSwitch(switches::kIgnoreCertificateErrors))
792 globals_->ignore_certificate_errors = true;
793 if (command_line.HasSwitch(switches::kTestingFixedHttpPort)) {
794 globals_->testing_fixed_http_port =
795 GetSwitchValueAsInt(command_line, switches::kTestingFixedHttpPort);
797 if (command_line.HasSwitch(switches::kTestingFixedHttpsPort)) {
798 globals_->testing_fixed_https_port =
799 GetSwitchValueAsInt(command_line, switches::kTestingFixedHttpsPort);
801 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
802 // is fixed.
803 tracked_objects::ScopedTracker tracking_profile12_5(
804 FROM_HERE_WITH_EXPLICIT_FUNCTION(
805 "466432 IOThread::InitAsync::QuicConfiguration"));
806 ConfigureQuic(command_line);
807 if (command_line.HasSwitch(
808 switches::kEnableUserAlternateProtocolPorts)) {
809 globals_->enable_user_alternate_protocol_ports = true;
811 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
812 // is fixed.
813 tracked_objects::ScopedTracker tracking_profile13(
814 FROM_HERE_WITH_EXPLICIT_FUNCTION(
815 "466432 IOThread::InitAsync::InitializeNetworkOptions"));
816 InitializeNetworkOptions(command_line);
818 net::HttpNetworkSession::Params session_params;
819 InitializeNetworkSessionParams(&session_params);
820 session_params.net_log = net_log_;
821 session_params.proxy_service =
822 globals_->proxy_script_fetcher_proxy_service.get();
824 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
825 // is fixed.
826 tracked_objects::ScopedTracker tracking_profile14(
827 FROM_HERE_WITH_EXPLICIT_FUNCTION(
828 "466432 IOThread::InitAsync::HttpNetorkSession::Start"));
829 TRACE_EVENT_BEGIN0("startup", "IOThread::InitAsync:HttpNetworkSession");
830 scoped_refptr<net::HttpNetworkSession> network_session(
831 new net::HttpNetworkSession(session_params));
832 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
833 // is fixed.
834 tracked_objects::ScopedTracker tracking_profile15(
835 FROM_HERE_WITH_EXPLICIT_FUNCTION(
836 "466432 IOThread::InitAsync::HttpNetorkSession::End"));
837 globals_->proxy_script_fetcher_http_transaction_factory
838 .reset(new net::HttpNetworkLayer(network_session.get()));
839 TRACE_EVENT_END0("startup", "IOThread::InitAsync:HttpNetworkSession");
840 scoped_ptr<net::URLRequestJobFactoryImpl> job_factory(
841 new net::URLRequestJobFactoryImpl());
843 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
844 // is fixed.
845 tracked_objects::ScopedTracker tracking_profile16(
846 FROM_HERE_WITH_EXPLICIT_FUNCTION(
847 "466432 IOThread::InitAsync::SetProtocolHandler"));
848 job_factory->SetProtocolHandler(url::kDataScheme,
849 new net::DataProtocolHandler());
850 job_factory->SetProtocolHandler(
851 url::kFileScheme,
852 new net::FileProtocolHandler(
853 content::BrowserThread::GetBlockingPool()->
854 GetTaskRunnerWithShutdownBehavior(
855 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)));
856 #if !defined(DISABLE_FTP_SUPPORT)
857 globals_->proxy_script_fetcher_ftp_transaction_factory.reset(
858 new net::FtpNetworkLayer(globals_->host_resolver.get()));
859 job_factory->SetProtocolHandler(
860 url::kFtpScheme,
861 new net::FtpProtocolHandler(
862 globals_->proxy_script_fetcher_ftp_transaction_factory.get()));
863 #endif
864 globals_->proxy_script_fetcher_url_request_job_factory = job_factory.Pass();
866 globals_->proxy_script_fetcher_context.reset(
867 ConstructProxyScriptFetcherContext(globals_, net_log_));
869 const version_info::Channel channel = chrome::VersionInfo::GetChannel();
870 if (channel == version_info::Channel::UNKNOWN ||
871 channel == version_info::Channel::CANARY ||
872 channel == version_info::Channel::DEV) {
873 globals_->url_request_backoff_manager.reset(
874 new net::URLRequestBackoffManager());
877 #if defined(OS_MACOSX) && !defined(OS_IOS)
878 // Start observing Keychain events. This needs to be done on the UI thread,
879 // as Keychain services requires a CFRunLoop.
880 BrowserThread::PostTask(BrowserThread::UI,
881 FROM_HERE,
882 base::Bind(&ObserveKeychainEvents));
883 #endif
885 // InitSystemRequestContext turns right around and posts a task back
886 // to the IO thread, so we can't let it run until we know the IO
887 // thread has started.
889 // Note that since we are at BrowserThread::Init time, the UI thread
890 // is blocked waiting for the thread to start. Therefore, posting
891 // this task to the main thread's message loop here is guaranteed to
892 // get it onto the message loop while the IOThread object still
893 // exists. However, the message might not be processed on the UI
894 // thread until after IOThread is gone, so use a weak pointer.
895 BrowserThread::PostTask(BrowserThread::UI,
896 FROM_HERE,
897 base::Bind(&IOThread::InitSystemRequestContext,
898 weak_factory_.GetWeakPtr()));
901 void IOThread::CleanUp() {
902 base::debug::LeakTracker<SafeBrowsingURLRequestContext>::CheckForLeaks();
904 #if defined(USE_NSS_CERTS) || defined(OS_IOS)
905 net::ShutdownNSSHttpIO();
906 #endif
908 system_url_request_context_getter_ = NULL;
910 // Release objects that the net::URLRequestContext could have been pointing
911 // to.
913 // Shutdown the HistogramWatcher on the IO thread.
914 net::NetworkChangeNotifier::ShutdownHistogramWatcher();
916 // This must be reset before the ChromeNetLog is destroyed.
917 network_change_observer_.reset();
919 system_proxy_config_service_.reset();
921 delete globals_;
922 globals_ = NULL;
924 base::debug::LeakTracker<SystemURLRequestContextGetter>::CheckForLeaks();
927 void IOThread::InitializeNetworkOptions(const base::CommandLine& command_line) {
928 // Only handle use-spdy command line flags if "spdy.disabled" preference is
929 // not disabled via policy.
930 if (is_spdy_disabled_by_policy_) {
931 base::FieldTrial* trial = base::FieldTrialList::Find(kSpdyFieldTrialName);
932 if (trial)
933 trial->Disable();
934 } else {
935 std::string group = base::FieldTrialList::FindFullName(kSpdyFieldTrialName);
936 VariationParameters params;
937 if (!variations::GetVariationParams(kSpdyFieldTrialName, &params)) {
938 params.clear();
940 ConfigureSpdyGlobals(command_line, group, params, globals_);
943 ConfigureTCPFastOpen(command_line);
945 // TODO(rch): Make the client socket factory a per-network session
946 // instance, constructed from a NetworkSession::Params, to allow us
947 // to move this option to IOThread::Globals &
948 // HttpNetworkSession::Params.
951 void IOThread::ConfigureTCPFastOpen(const base::CommandLine& command_line) {
952 const std::string trial_group =
953 base::FieldTrialList::FindFullName(kTCPFastOpenFieldTrialName);
954 if (trial_group == kTCPFastOpenHttpsEnabledGroupName)
955 globals_->enable_tcp_fast_open_for_ssl.set(true);
956 bool always_enable_if_supported =
957 command_line.HasSwitch(switches::kEnableTcpFastOpen);
958 // Check for OS support of TCP FastOpen, and turn it on for all connections
959 // if indicated by user.
960 net::CheckSupportAndMaybeEnableTCPFastOpen(always_enable_if_supported);
963 // static
964 void IOThread::ConfigureSpdyGlobals(
965 const base::CommandLine& command_line,
966 base::StringPiece spdy_trial_group,
967 const VariationParameters& spdy_trial_params,
968 IOThread::Globals* globals) {
969 if (command_line.HasSwitch(switches::kTrustedSpdyProxy)) {
970 globals->trusted_spdy_proxy.set(
971 command_line.GetSwitchValueASCII(switches::kTrustedSpdyProxy));
973 if (command_line.HasSwitch(switches::kIgnoreUrlFetcherCertRequests))
974 net::URLFetcher::SetIgnoreCertificateRequests(true);
976 if (command_line.HasSwitch(switches::kUseSpdy)) {
977 std::string spdy_mode =
978 command_line.GetSwitchValueASCII(switches::kUseSpdy);
979 ConfigureSpdyGlobalsFromUseSpdyArgument(spdy_mode, globals);
980 return;
983 globals->next_protos.clear();
984 globals->next_protos.push_back(net::kProtoHTTP11);
985 bool enable_quic = false;
986 globals->enable_quic.CopyToIfSet(&enable_quic);
987 if (enable_quic) {
988 globals->next_protos.push_back(net::kProtoQUIC1SPDY3);
991 // No SPDY command-line flags have been specified. Examine trial groups.
992 if (spdy_trial_group.starts_with(kSpdyFieldTrialHoldbackGroupNamePrefix)) {
993 net::HttpStreamFactory::set_spdy_enabled(false);
994 return;
996 if (spdy_trial_group.starts_with(kSpdyFieldTrialSpdy31GroupNamePrefix)) {
997 globals->next_protos.push_back(net::kProtoSPDY31);
998 globals->use_alternate_protocols.set(true);
999 return;
1001 if (spdy_trial_group.starts_with(kSpdyFieldTrialSpdy4GroupNamePrefix)) {
1002 globals->next_protos.push_back(net::kProtoSPDY31);
1003 globals->next_protos.push_back(net::kProtoHTTP2_14);
1004 globals->next_protos.push_back(net::kProtoHTTP2);
1005 globals->use_alternate_protocols.set(true);
1006 return;
1008 if (spdy_trial_group.starts_with(kSpdyFieldTrialParametrizedPrefix)) {
1009 bool spdy_enabled = false;
1010 if (base::LowerCaseEqualsASCII(
1011 GetVariationParam(spdy_trial_params, "enable_spdy31"), "true")) {
1012 globals->next_protos.push_back(net::kProtoSPDY31);
1013 spdy_enabled = true;
1015 if (base::LowerCaseEqualsASCII(
1016 GetVariationParam(spdy_trial_params, "enable_http2_14"), "true")) {
1017 globals->next_protos.push_back(net::kProtoHTTP2_14);
1018 spdy_enabled = true;
1020 if (base::LowerCaseEqualsASCII(
1021 GetVariationParam(spdy_trial_params, "enable_http2"), "true")) {
1022 globals->next_protos.push_back(net::kProtoHTTP2);
1023 spdy_enabled = true;
1025 // TODO(bnc): HttpStreamFactory::spdy_enabled_ is redundant with
1026 // globals->next_protos, can it be eliminated?
1027 net::HttpStreamFactory::set_spdy_enabled(spdy_enabled);
1028 globals->use_alternate_protocols.set(true);
1029 return;
1032 // By default, enable HTTP/2.
1033 globals->next_protos.push_back(net::kProtoSPDY31);
1034 globals->next_protos.push_back(net::kProtoHTTP2_14);
1035 globals->next_protos.push_back(net::kProtoHTTP2);
1036 globals->use_alternate_protocols.set(true);
1039 // static
1040 void IOThread::RegisterPrefs(PrefRegistrySimple* registry) {
1041 registry->RegisterStringPref(prefs::kAuthSchemes,
1042 "basic,digest,ntlm,negotiate");
1043 registry->RegisterBooleanPref(prefs::kDisableAuthNegotiateCnameLookup, false);
1044 registry->RegisterBooleanPref(prefs::kEnableAuthNegotiatePort, false);
1045 registry->RegisterStringPref(prefs::kAuthServerWhitelist, std::string());
1046 registry->RegisterStringPref(prefs::kAuthNegotiateDelegateWhitelist,
1047 std::string());
1048 registry->RegisterStringPref(prefs::kGSSAPILibraryName, std::string());
1049 registry->RegisterStringPref(prefs::kAuthAndroidNegotiateAccountType,
1050 std::string());
1051 registry->RegisterStringPref(
1052 data_reduction_proxy::prefs::kDataReductionProxy, std::string());
1053 registry->RegisterBooleanPref(prefs::kEnableReferrers, true);
1054 data_reduction_proxy::RegisterPrefs(registry);
1055 registry->RegisterBooleanPref(prefs::kBuiltInDnsClientEnabled, true);
1056 registry->RegisterBooleanPref(prefs::kQuickCheckEnabled, true);
1059 net::HttpAuthHandlerFactory* IOThread::CreateDefaultAuthHandlerFactory(
1060 net::HostResolver* resolver) {
1061 net::HttpAuthFilterWhitelist* auth_filter_default_credentials = NULL;
1062 if (!auth_server_whitelist_.empty()) {
1063 auth_filter_default_credentials =
1064 new net::HttpAuthFilterWhitelist(auth_server_whitelist_);
1066 net::HttpAuthFilterWhitelist* auth_filter_delegate = NULL;
1067 if (!auth_delegate_whitelist_.empty()) {
1068 auth_filter_delegate =
1069 new net::HttpAuthFilterWhitelist(auth_delegate_whitelist_);
1071 globals_->url_security_manager.reset(
1072 net::URLSecurityManager::Create(auth_filter_default_credentials,
1073 auth_filter_delegate));
1074 std::vector<std::string> supported_schemes;
1075 base::SplitString(auth_schemes_, ',', &supported_schemes);
1077 scoped_ptr<net::HttpAuthHandlerRegistryFactory> registry_factory(
1078 net::HttpAuthHandlerRegistryFactory::Create(
1079 supported_schemes, globals_->url_security_manager.get(), resolver,
1080 gssapi_library_name_, auth_android_negotiate_account_type_,
1081 negotiate_disable_cname_lookup_, negotiate_enable_port_));
1082 return registry_factory.release();
1085 void IOThread::ClearHostCache() {
1086 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
1088 net::HostCache* host_cache = globals_->host_resolver->GetHostCache();
1089 if (host_cache)
1090 host_cache->clear();
1093 void IOThread::InitializeNetworkSessionParams(
1094 net::HttpNetworkSession::Params* params) {
1095 InitializeNetworkSessionParamsFromGlobals(*globals_, params);
1098 // static
1099 void IOThread::InitializeNetworkSessionParamsFromGlobals(
1100 const IOThread::Globals& globals,
1101 net::HttpNetworkSession::Params* params) {
1102 params->host_resolver = globals.host_resolver.get();
1103 params->cert_verifier = globals.cert_verifier.get();
1104 params->cert_policy_enforcer = globals.cert_policy_enforcer.get();
1105 params->channel_id_service = globals.system_channel_id_service.get();
1106 params->transport_security_state = globals.transport_security_state.get();
1107 params->ssl_config_service = globals.ssl_config_service.get();
1108 params->http_auth_handler_factory = globals.http_auth_handler_factory.get();
1109 params->http_server_properties =
1110 globals.http_server_properties->GetWeakPtr();
1111 params->network_delegate = globals.system_network_delegate.get();
1112 params->host_mapping_rules = globals.host_mapping_rules.get();
1113 params->ignore_certificate_errors = globals.ignore_certificate_errors;
1114 params->testing_fixed_http_port = globals.testing_fixed_http_port;
1115 params->testing_fixed_https_port = globals.testing_fixed_https_port;
1116 globals.enable_tcp_fast_open_for_ssl.CopyToIfSet(
1117 &params->enable_tcp_fast_open_for_ssl);
1119 globals.initial_max_spdy_concurrent_streams.CopyToIfSet(
1120 &params->spdy_initial_max_concurrent_streams);
1121 globals.enable_spdy_compression.CopyToIfSet(
1122 &params->enable_spdy_compression);
1123 globals.enable_spdy_ping_based_connection_checking.CopyToIfSet(
1124 &params->enable_spdy_ping_based_connection_checking);
1125 globals.spdy_default_protocol.CopyToIfSet(
1126 &params->spdy_default_protocol);
1127 params->next_protos = globals.next_protos;
1128 globals.trusted_spdy_proxy.CopyToIfSet(&params->trusted_spdy_proxy);
1129 params->forced_spdy_exclusions = globals.forced_spdy_exclusions;
1130 globals.use_alternate_protocols.CopyToIfSet(
1131 &params->use_alternate_protocols);
1132 globals.alternative_service_probability_threshold.CopyToIfSet(
1133 &params->alternative_service_probability_threshold);
1135 globals.enable_quic.CopyToIfSet(&params->enable_quic);
1136 globals.enable_insecure_quic.CopyToIfSet(&params->enable_insecure_quic);
1137 globals.enable_quic_for_proxies.CopyToIfSet(&params->enable_quic_for_proxies);
1138 globals.quic_always_require_handshake_confirmation.CopyToIfSet(
1139 &params->quic_always_require_handshake_confirmation);
1140 globals.quic_disable_connection_pooling.CopyToIfSet(
1141 &params->quic_disable_connection_pooling);
1142 globals.quic_load_server_info_timeout_srtt_multiplier.CopyToIfSet(
1143 &params->quic_load_server_info_timeout_srtt_multiplier);
1144 globals.quic_enable_connection_racing.CopyToIfSet(
1145 &params->quic_enable_connection_racing);
1146 globals.quic_enable_non_blocking_io.CopyToIfSet(
1147 &params->quic_enable_non_blocking_io);
1148 globals.quic_prefer_aes.CopyToIfSet(&params->quic_prefer_aes);
1149 globals.quic_disable_disk_cache.CopyToIfSet(
1150 &params->quic_disable_disk_cache);
1151 globals.quic_max_number_of_lossy_connections.CopyToIfSet(
1152 &params->quic_max_number_of_lossy_connections);
1153 globals.quic_packet_loss_threshold.CopyToIfSet(
1154 &params->quic_packet_loss_threshold);
1155 globals.quic_socket_receive_buffer_size.CopyToIfSet(
1156 &params->quic_socket_receive_buffer_size);
1157 globals.enable_quic_port_selection.CopyToIfSet(
1158 &params->enable_quic_port_selection);
1159 globals.quic_max_packet_length.CopyToIfSet(&params->quic_max_packet_length);
1160 globals.quic_user_agent_id.CopyToIfSet(&params->quic_user_agent_id);
1161 globals.quic_supported_versions.CopyToIfSet(
1162 &params->quic_supported_versions);
1163 params->quic_connection_options = globals.quic_connection_options;
1165 globals.origin_to_force_quic_on.CopyToIfSet(
1166 &params->origin_to_force_quic_on);
1167 params->enable_user_alternate_protocol_ports =
1168 globals.enable_user_alternate_protocol_ports;
1171 base::TimeTicks IOThread::creation_time() const {
1172 return creation_time_;
1175 net::SSLConfigService* IOThread::GetSSLConfigService() {
1176 return ssl_config_service_manager_->Get();
1179 void IOThread::ChangedToOnTheRecordOnIOThread() {
1180 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
1182 // Clear the host cache to avoid showing entries from the OTR session
1183 // in about:net-internals.
1184 ClearHostCache();
1187 void IOThread::InitSystemRequestContext() {
1188 if (system_url_request_context_getter_.get())
1189 return;
1190 // If we're in unit_tests, IOThread may not be run.
1191 if (!BrowserThread::IsMessageLoopValid(BrowserThread::IO))
1192 return;
1193 system_proxy_config_service_.reset(
1194 ProxyServiceFactory::CreateProxyConfigService(
1195 pref_proxy_config_tracker_.get()));
1196 system_url_request_context_getter_ =
1197 new SystemURLRequestContextGetter(this);
1198 // Safe to post an unretained this pointer, since IOThread is
1199 // guaranteed to outlive the IO BrowserThread.
1200 BrowserThread::PostTask(
1201 BrowserThread::IO,
1202 FROM_HERE,
1203 base::Bind(&IOThread::InitSystemRequestContextOnIOThread,
1204 base::Unretained(this)));
1207 void IOThread::InitSystemRequestContextOnIOThread() {
1208 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
1209 DCHECK(!globals_->system_proxy_service.get());
1210 DCHECK(system_proxy_config_service_.get());
1212 const base::CommandLine& command_line =
1213 *base::CommandLine::ForCurrentProcess();
1214 globals_->system_proxy_service.reset(
1215 ProxyServiceFactory::CreateProxyService(
1216 net_log_,
1217 globals_->proxy_script_fetcher_context.get(),
1218 globals_->system_network_delegate.get(),
1219 system_proxy_config_service_.release(),
1220 command_line,
1221 quick_check_enabled_.GetValue()));
1223 net::HttpNetworkSession::Params system_params;
1224 InitializeNetworkSessionParams(&system_params);
1225 system_params.net_log = net_log_;
1226 system_params.proxy_service = globals_->system_proxy_service.get();
1228 globals_->system_http_transaction_factory.reset(
1229 new net::HttpNetworkLayer(
1230 new net::HttpNetworkSession(system_params)));
1231 globals_->system_url_request_job_factory.reset(
1232 new net::URLRequestJobFactoryImpl());
1233 globals_->system_request_context.reset(
1234 ConstructSystemRequestContext(globals_, net_log_));
1235 globals_->system_request_context->set_ssl_config_service(
1236 globals_->ssl_config_service.get());
1237 globals_->system_request_context->set_http_server_properties(
1238 globals_->http_server_properties->GetWeakPtr());
1241 void IOThread::UpdateDnsClientEnabled() {
1242 globals()->host_resolver->SetDnsClientEnabled(*dns_client_enabled_);
1245 void IOThread::ConfigureQuic(const base::CommandLine& command_line) {
1246 // Always fetch the field trial group to ensure it is reported correctly.
1247 // The command line flags will be associated with a group that is reported
1248 // so long as trial is actually queried.
1249 std::string group =
1250 base::FieldTrialList::FindFullName(kQuicFieldTrialName);
1251 VariationParameters params;
1252 if (!variations::GetVariationParams(kQuicFieldTrialName, &params)) {
1253 params.clear();
1256 ConfigureQuicGlobals(command_line, group, params, is_quic_allowed_by_policy_,
1257 globals_);
1260 // static
1261 void IOThread::ConfigureQuicGlobals(
1262 const base::CommandLine& command_line,
1263 base::StringPiece quic_trial_group,
1264 const VariationParameters& quic_trial_params,
1265 bool quic_allowed_by_policy,
1266 IOThread::Globals* globals) {
1267 bool enable_quic = ShouldEnableQuic(command_line, quic_trial_group,
1268 quic_allowed_by_policy);
1269 globals->enable_quic.set(enable_quic);
1270 bool enable_quic_for_proxies = ShouldEnableQuicForProxies(
1271 command_line, quic_trial_group, quic_allowed_by_policy);
1272 globals->enable_quic_for_proxies.set(enable_quic_for_proxies);
1273 if (enable_quic) {
1274 globals->enable_insecure_quic.set(
1275 ShouldEnableInsecureQuic(command_line, quic_trial_params));
1276 globals->quic_always_require_handshake_confirmation.set(
1277 ShouldQuicAlwaysRequireHandshakeConfirmation(quic_trial_params));
1278 globals->quic_disable_connection_pooling.set(
1279 ShouldQuicDisableConnectionPooling(quic_trial_params));
1280 int receive_buffer_size = GetQuicSocketReceiveBufferSize(quic_trial_params);
1281 if (receive_buffer_size != 0) {
1282 globals->quic_socket_receive_buffer_size.set(receive_buffer_size);
1284 float load_server_info_timeout_srtt_multiplier =
1285 GetQuicLoadServerInfoTimeoutSrttMultiplier(quic_trial_params);
1286 if (load_server_info_timeout_srtt_multiplier != 0) {
1287 globals->quic_load_server_info_timeout_srtt_multiplier.set(
1288 load_server_info_timeout_srtt_multiplier);
1290 globals->quic_enable_connection_racing.set(
1291 ShouldQuicEnableConnectionRacing(quic_trial_params));
1292 globals->quic_enable_non_blocking_io.set(
1293 ShouldQuicEnableNonBlockingIO(quic_trial_params));
1294 globals->quic_disable_disk_cache.set(
1295 ShouldQuicDisableDiskCache(quic_trial_params));
1296 globals->quic_prefer_aes.set(
1297 ShouldQuicPreferAes(quic_trial_params));
1298 int max_number_of_lossy_connections = GetQuicMaxNumberOfLossyConnections(
1299 quic_trial_params);
1300 if (max_number_of_lossy_connections != 0) {
1301 globals->quic_max_number_of_lossy_connections.set(
1302 max_number_of_lossy_connections);
1304 float packet_loss_threshold = GetQuicPacketLossThreshold(quic_trial_params);
1305 if (packet_loss_threshold != 0)
1306 globals->quic_packet_loss_threshold.set(packet_loss_threshold);
1307 globals->enable_quic_port_selection.set(
1308 ShouldEnableQuicPortSelection(command_line));
1309 globals->quic_connection_options =
1310 GetQuicConnectionOptions(command_line, quic_trial_params);
1313 size_t max_packet_length = GetQuicMaxPacketLength(command_line,
1314 quic_trial_params);
1315 if (max_packet_length != 0) {
1316 globals->quic_max_packet_length.set(max_packet_length);
1319 std::string quic_user_agent_id =
1320 chrome::VersionInfo::GetVersionStringModifier();
1321 if (!quic_user_agent_id.empty())
1322 quic_user_agent_id.push_back(' ');
1323 chrome::VersionInfo version_info;
1324 quic_user_agent_id.append(version_info.ProductNameAndVersionForUserAgent());
1325 quic_user_agent_id.push_back(' ');
1326 quic_user_agent_id.append(content::BuildOSCpuInfo());
1327 globals->quic_user_agent_id.set(quic_user_agent_id);
1329 net::QuicVersion version = GetQuicVersion(command_line, quic_trial_params);
1330 if (version != net::QUIC_VERSION_UNSUPPORTED) {
1331 net::QuicVersionVector supported_versions;
1332 supported_versions.push_back(version);
1333 globals->quic_supported_versions.set(supported_versions);
1336 double threshold = GetAlternativeProtocolProbabilityThreshold(
1337 command_line, quic_trial_params);
1338 if (threshold >=0 && threshold <= 1) {
1339 globals->alternative_service_probability_threshold.set(threshold);
1340 globals->http_server_properties->SetAlternativeServiceProbabilityThreshold(
1341 threshold);
1344 if (command_line.HasSwitch(switches::kOriginToForceQuicOn)) {
1345 net::HostPortPair quic_origin =
1346 net::HostPortPair::FromString(
1347 command_line.GetSwitchValueASCII(switches::kOriginToForceQuicOn));
1348 if (!quic_origin.IsEmpty()) {
1349 globals->origin_to_force_quic_on.set(quic_origin);
1354 bool IOThread::ShouldEnableQuic(const base::CommandLine& command_line,
1355 base::StringPiece quic_trial_group,
1356 bool quic_allowed_by_policy) {
1357 if (command_line.HasSwitch(switches::kDisableQuic) || !quic_allowed_by_policy)
1358 return false;
1360 if (command_line.HasSwitch(switches::kEnableQuic))
1361 return true;
1363 return quic_trial_group.starts_with(kQuicFieldTrialEnabledGroupName) ||
1364 quic_trial_group.starts_with(kQuicFieldTrialHttpsEnabledGroupName);
1367 // static
1368 bool IOThread::ShouldEnableQuicForProxies(const base::CommandLine& command_line,
1369 base::StringPiece quic_trial_group,
1370 bool quic_allowed_by_policy) {
1371 return ShouldEnableQuic(
1372 command_line, quic_trial_group, quic_allowed_by_policy) ||
1373 ShouldEnableQuicForDataReductionProxy();
1376 // static
1377 bool IOThread::ShouldEnableQuicForDataReductionProxy() {
1378 const base::CommandLine& command_line =
1379 *base::CommandLine::ForCurrentProcess();
1381 if (command_line.HasSwitch(switches::kDisableQuic))
1382 return false;
1384 return data_reduction_proxy::params::IsIncludedInQuicFieldTrial();
1387 // static
1388 bool IOThread::ShouldEnableInsecureQuic(
1389 const base::CommandLine& command_line,
1390 const VariationParameters& quic_trial_params) {
1391 if (command_line.HasSwitch(switches::kEnableInsecureQuic))
1392 return true;
1394 return base::LowerCaseEqualsASCII(
1395 GetVariationParam(quic_trial_params, "enable_insecure_quic"),
1396 "true");
1399 bool IOThread::ShouldEnableQuicPortSelection(
1400 const base::CommandLine& command_line) {
1401 if (command_line.HasSwitch(switches::kDisableQuicPortSelection))
1402 return false;
1404 if (command_line.HasSwitch(switches::kEnableQuicPortSelection))
1405 return true;
1407 return false; // Default to disabling port selection on all channels.
1410 net::QuicTagVector IOThread::GetQuicConnectionOptions(
1411 const base::CommandLine& command_line,
1412 const VariationParameters& quic_trial_params) {
1413 if (command_line.HasSwitch(switches::kQuicConnectionOptions)) {
1414 return net::QuicUtils::ParseQuicConnectionOptions(
1415 command_line.GetSwitchValueASCII(switches::kQuicConnectionOptions));
1418 VariationParameters::const_iterator it =
1419 quic_trial_params.find("connection_options");
1420 if (it == quic_trial_params.end()) {
1421 return net::QuicTagVector();
1424 return net::QuicUtils::ParseQuicConnectionOptions(it->second);
1427 // static
1428 double IOThread::GetAlternativeProtocolProbabilityThreshold(
1429 const base::CommandLine& command_line,
1430 const VariationParameters& quic_trial_params) {
1431 double value;
1432 if (command_line.HasSwitch(
1433 switches::kAlternativeServiceProbabilityThreshold)) {
1434 if (base::StringToDouble(
1435 command_line.GetSwitchValueASCII(
1436 switches::kAlternativeServiceProbabilityThreshold),
1437 &value)) {
1438 return value;
1441 if (command_line.HasSwitch(switches::kEnableQuic)) {
1442 return 0;
1444 // TODO(bnc): Remove when new parameter name rolls out and server
1445 // configuration is changed.
1446 if (base::StringToDouble(
1447 GetVariationParam(quic_trial_params,
1448 "alternate_protocol_probability_threshold"),
1449 &value)) {
1450 return value;
1452 if (base::StringToDouble(
1453 GetVariationParam(quic_trial_params,
1454 "alternative_service_probability_threshold"),
1455 &value)) {
1456 return value;
1458 return -1;
1461 // static
1462 bool IOThread::ShouldQuicAlwaysRequireHandshakeConfirmation(
1463 const VariationParameters& quic_trial_params) {
1464 return base::LowerCaseEqualsASCII(
1465 GetVariationParam(quic_trial_params,
1466 "always_require_handshake_confirmation"),
1467 "true");
1470 // static
1471 bool IOThread::ShouldQuicDisableConnectionPooling(
1472 const VariationParameters& quic_trial_params) {
1473 return base::LowerCaseEqualsASCII(
1474 GetVariationParam(quic_trial_params, "disable_connection_pooling"),
1475 "true");
1478 // static
1479 float IOThread::GetQuicLoadServerInfoTimeoutSrttMultiplier(
1480 const VariationParameters& quic_trial_params) {
1481 double value;
1482 if (base::StringToDouble(GetVariationParam(quic_trial_params,
1483 "load_server_info_time_to_srtt"),
1484 &value)) {
1485 return (float)value;
1487 return 0.0f;
1490 // static
1491 bool IOThread::ShouldQuicEnableConnectionRacing(
1492 const VariationParameters& quic_trial_params) {
1493 return base::LowerCaseEqualsASCII(
1494 GetVariationParam(quic_trial_params, "enable_connection_racing"),
1495 "true");
1498 // static
1499 bool IOThread::ShouldQuicEnableNonBlockingIO(
1500 const VariationParameters& quic_trial_params) {
1501 return base::LowerCaseEqualsASCII(
1502 GetVariationParam(quic_trial_params, "enable_non_blocking_io"),
1503 "true");
1506 // static
1507 bool IOThread::ShouldQuicDisableDiskCache(
1508 const VariationParameters& quic_trial_params) {
1509 return base::LowerCaseEqualsASCII(
1510 GetVariationParam(quic_trial_params, "disable_disk_cache"), "true");
1513 // static
1514 bool IOThread::ShouldQuicPreferAes(
1515 const VariationParameters& quic_trial_params) {
1516 return base::LowerCaseEqualsASCII(
1517 GetVariationParam(quic_trial_params, "prefer_aes"), "true");
1520 // static
1521 int IOThread::GetQuicMaxNumberOfLossyConnections(
1522 const VariationParameters& quic_trial_params) {
1523 int value;
1524 if (base::StringToInt(GetVariationParam(quic_trial_params,
1525 "max_number_of_lossy_connections"),
1526 &value)) {
1527 return value;
1529 return 0;
1532 // static
1533 float IOThread::GetQuicPacketLossThreshold(
1534 const VariationParameters& quic_trial_params) {
1535 double value;
1536 if (base::StringToDouble(GetVariationParam(quic_trial_params,
1537 "packet_loss_threshold"),
1538 &value)) {
1539 return (float)value;
1541 return 0.0f;
1544 // static
1545 int IOThread::GetQuicSocketReceiveBufferSize(
1546 const VariationParameters& quic_trial_params) {
1547 int value;
1548 if (base::StringToInt(GetVariationParam(quic_trial_params,
1549 "receive_buffer_size"),
1550 &value)) {
1551 return value;
1553 return 0;
1556 // static
1557 size_t IOThread::GetQuicMaxPacketLength(
1558 const base::CommandLine& command_line,
1559 const VariationParameters& quic_trial_params) {
1560 if (command_line.HasSwitch(switches::kQuicMaxPacketLength)) {
1561 unsigned value;
1562 if (!base::StringToUint(
1563 command_line.GetSwitchValueASCII(switches::kQuicMaxPacketLength),
1564 &value)) {
1565 return 0;
1567 return value;
1570 unsigned value;
1571 if (base::StringToUint(GetVariationParam(quic_trial_params,
1572 "max_packet_length"),
1573 &value)) {
1574 return value;
1576 return 0;
1579 // static
1580 net::QuicVersion IOThread::GetQuicVersion(
1581 const base::CommandLine& command_line,
1582 const VariationParameters& quic_trial_params) {
1583 if (command_line.HasSwitch(switches::kQuicVersion)) {
1584 return ParseQuicVersion(
1585 command_line.GetSwitchValueASCII(switches::kQuicVersion));
1588 return ParseQuicVersion(GetVariationParam(quic_trial_params, "quic_version"));
1591 // static
1592 net::QuicVersion IOThread::ParseQuicVersion(const std::string& quic_version) {
1593 net::QuicVersionVector supported_versions = net::QuicSupportedVersions();
1594 for (size_t i = 0; i < supported_versions.size(); ++i) {
1595 net::QuicVersion version = supported_versions[i];
1596 if (net::QuicVersionToString(version) == quic_version) {
1597 return version;
1601 return net::QUIC_VERSION_UNSUPPORTED;