Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / chrome / browser / io_thread.cc
blob9ba5e1ccd5e52a6a28076e8cef68903556d31714
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/proxy_service_factory.h"
38 #include "chrome/common/channel_info.h"
39 #include "chrome/common/chrome_content_client.h"
40 #include "chrome/common/chrome_switches.h"
41 #include "chrome/common/pref_names.h"
42 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_prefs.h"
43 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h"
44 #include "components/policy/core/common/policy_service.h"
45 #include "components/proxy_config/pref_proxy_config_tracker.h"
46 #include "components/variations/variations_associated_data.h"
47 #include "components/version_info/version_info.h"
48 #include "content/public/browser/browser_thread.h"
49 #include "content/public/browser/cookie_store_factory.h"
50 #include "content/public/common/user_agent.h"
51 #include "net/base/external_estimate_provider.h"
52 #include "net/base/host_mapping_rules.h"
53 #include "net/base/net_util.h"
54 #include "net/base/network_quality_estimator.h"
55 #include "net/base/sdch_manager.h"
56 #include "net/cert/cert_policy_enforcer.h"
57 #include "net/cert/cert_verifier.h"
58 #include "net/cert/cert_verify_proc.h"
59 #include "net/cert/ct_known_logs.h"
60 #include "net/cert/ct_known_logs_static.h"
61 #include "net/cert/ct_log_verifier.h"
62 #include "net/cert/ct_verifier.h"
63 #include "net/cert/multi_log_ct_verifier.h"
64 #include "net/cert/multi_threaded_cert_verifier.h"
65 #include "net/cookies/cookie_store.h"
66 #include "net/dns/host_cache.h"
67 #include "net/dns/host_resolver.h"
68 #include "net/dns/mapped_host_resolver.h"
69 #include "net/ftp/ftp_network_layer.h"
70 #include "net/http/http_auth_filter.h"
71 #include "net/http/http_auth_handler_factory.h"
72 #include "net/http/http_network_layer.h"
73 #include "net/http/http_server_properties_impl.h"
74 #include "net/proxy/proxy_config_service.h"
75 #include "net/proxy/proxy_script_fetcher_impl.h"
76 #include "net/proxy/proxy_service.h"
77 #include "net/quic/crypto/crypto_protocol.h"
78 #include "net/quic/quic_protocol.h"
79 #include "net/quic/quic_utils.h"
80 #include "net/socket/tcp_client_socket.h"
81 #include "net/spdy/spdy_session.h"
82 #include "net/ssl/channel_id_service.h"
83 #include "net/ssl/default_channel_id_store.h"
84 #include "net/url_request/data_protocol_handler.h"
85 #include "net/url_request/file_protocol_handler.h"
86 #include "net/url_request/ftp_protocol_handler.h"
87 #include "net/url_request/static_http_user_agent_settings.h"
88 #include "net/url_request/url_fetcher.h"
89 #include "net/url_request/url_request_backoff_manager.h"
90 #include "net/url_request/url_request_context.h"
91 #include "net/url_request/url_request_context_builder.h"
92 #include "net/url_request/url_request_context_getter.h"
93 #include "net/url_request/url_request_job_factory_impl.h"
94 #include "url/url_constants.h"
96 #if defined(ENABLE_CONFIGURATION_POLICY)
97 #include "policy/policy_constants.h"
98 #endif
100 #if defined(ENABLE_EXTENSIONS)
101 #include "chrome/browser/extensions/event_router_forwarder.h"
102 #endif
104 #if defined(USE_NSS_CERTS) || defined(OS_IOS)
105 #include "net/cert_net/nss_ocsp.h"
106 #endif
108 #if defined(OS_ANDROID)
109 #include "base/android/build_info.h"
110 #include "chrome/browser/android/net/external_estimate_provider_android.h"
111 #endif
113 #if defined(OS_CHROMEOS)
114 #include "chrome/browser/chromeos/net/cert_verify_proc_chromeos.h"
115 #include "chromeos/network/host_resolver_impl_chromeos.h"
116 #endif
118 using content::BrowserThread;
120 class SafeBrowsingURLRequestContext;
122 // The IOThread object must outlive any tasks posted to the IO thread before the
123 // Quit task, so base::Bind() calls are not refcounted.
125 namespace {
127 const char kTCPFastOpenFieldTrialName[] = "TCPFastOpen";
128 const char kTCPFastOpenHttpsEnabledGroupName[] = "HttpsEnabled";
130 const char kQuicFieldTrialName[] = "QUIC";
131 const char kQuicFieldTrialEnabledGroupName[] = "Enabled";
132 const char kQuicFieldTrialHttpsEnabledGroupName[] = "HttpsEnabled";
134 // The SPDY trial composes two different trial plus control groups:
135 // * A "holdback" group with SPDY disabled, and corresponding control
136 // (SPDY/3.1). The primary purpose of the holdback group is to encourage site
137 // operators to do feature detection rather than UA-sniffing. As such, this
138 // trial runs continuously.
139 // * A SPDY/4 experiment, for SPDY/4 (aka HTTP/2) vs SPDY/3.1 comparisons and
140 // eventual SPDY/4 deployment.
141 const char kSpdyFieldTrialName[] = "SPDY";
142 const char kSpdyFieldTrialHoldbackGroupNamePrefix[] = "SpdyDisabled";
143 const char kSpdyFieldTrialSpdy31GroupNamePrefix[] = "Spdy31Enabled";
144 const char kSpdyFieldTrialSpdy4GroupNamePrefix[] = "Spdy4Enabled";
145 const char kSpdyFieldTrialParametrizedPrefix[] = "Parametrized";
147 // Field trial for network quality estimator. Seeds RTT and downstream
148 // throughput observations with values that correspond to the connection type
149 // determined by the operating system.
150 const char kNetworkQualityEstimatorFieldTrialName[] = "NetworkQualityEstimator";
152 #if defined(OS_MACOSX) && !defined(OS_IOS)
153 void ObserveKeychainEvents() {
154 DCHECK_CURRENTLY_ON(BrowserThread::UI);
155 net::CertDatabase::GetInstance()->SetMessageLoopForKeychainEvents();
157 #endif
159 // Used for the "system" URLRequestContext.
160 class SystemURLRequestContext : public net::URLRequestContext {
161 public:
162 SystemURLRequestContext() {
163 #if defined(USE_NSS_CERTS) || defined(OS_IOS)
164 net::SetURLRequestContextForNSSHttpIO(this);
165 #endif
168 private:
169 ~SystemURLRequestContext() override {
170 AssertNoURLRequests();
171 #if defined(USE_NSS_CERTS) || defined(OS_IOS)
172 net::SetURLRequestContextForNSSHttpIO(NULL);
173 #endif
177 scoped_ptr<net::HostResolver> CreateGlobalHostResolver(net::NetLog* net_log) {
178 TRACE_EVENT0("startup", "IOThread::CreateGlobalHostResolver");
179 const base::CommandLine& command_line =
180 *base::CommandLine::ForCurrentProcess();
182 net::HostResolver::Options options;
184 // Use the retry attempts override from the command-line, if any.
185 if (command_line.HasSwitch(switches::kHostResolverRetryAttempts)) {
186 std::string s =
187 command_line.GetSwitchValueASCII(switches::kHostResolverRetryAttempts);
188 // Parse the switch (it should be a non-negative integer).
189 int n;
190 if (base::StringToInt(s, &n) && n >= 0) {
191 options.max_retry_attempts = static_cast<size_t>(n);
192 } else {
193 LOG(ERROR) << "Invalid switch for host resolver retry attempts: " << s;
197 scoped_ptr<net::HostResolver> global_host_resolver;
198 #if defined OS_CHROMEOS
199 global_host_resolver =
200 chromeos::HostResolverImplChromeOS::CreateSystemResolver(options,
201 net_log);
202 #else
203 global_host_resolver =
204 net::HostResolver::CreateSystemResolver(options, net_log);
205 #endif
207 // If hostname remappings were specified on the command-line, layer these
208 // rules on top of the real host resolver. This allows forwarding all requests
209 // through a designated test server.
210 if (!command_line.HasSwitch(switches::kHostResolverRules))
211 return global_host_resolver.Pass();
213 scoped_ptr<net::MappedHostResolver> remapped_resolver(
214 new net::MappedHostResolver(global_host_resolver.Pass()));
215 remapped_resolver->SetRulesFromString(
216 command_line.GetSwitchValueASCII(switches::kHostResolverRules));
217 return remapped_resolver.Pass();
220 int GetSwitchValueAsInt(const base::CommandLine& command_line,
221 const std::string& switch_name) {
222 int value;
223 if (!base::StringToInt(command_line.GetSwitchValueASCII(switch_name),
224 &value)) {
225 return 0;
227 return value;
230 // Returns the value associated with |key| in |params| or "" if the
231 // key is not present in the map.
232 const std::string& GetVariationParam(
233 const std::map<std::string, std::string>& params,
234 const std::string& key) {
235 std::map<std::string, std::string>::const_iterator it = params.find(key);
236 if (it == params.end())
237 return base::EmptyString();
239 return it->second;
242 // Parse kUseSpdy command line flag options, which may contain the following:
244 // "off" : Disables SPDY support entirely.
245 // "no-ping" : Disables SPDY ping connection testing.
246 // "exclude=<host>" : Disables SPDY support for the host <host>.
247 // "no-compress" : Disables SPDY header compression.
248 // "init-max-streams=<limit>" : Specifies the maximum number of concurrent
249 // streams for a SPDY session, unless the
250 // specifies a different value via SETTINGS.
251 void ConfigureSpdyGlobalsFromUseSpdyArgument(const std::string& mode,
252 IOThread::Globals* globals) {
253 static const char kOff[] = "off";
254 static const char kDisablePing[] = "no-ping";
255 static const char kExclude[] = "exclude"; // Hosts to exclude
256 static const char kDisableCompression[] = "no-compress";
257 static const char kInitialMaxConcurrentStreams[] = "init-max-streams";
259 for (const base::StringPiece& element : base::SplitStringPiece(
260 mode, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) {
261 std::vector<base::StringPiece> name_value = base::SplitStringPiece(
262 element, "=", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
263 const base::StringPiece option =
264 name_value.size() > 0 ? name_value[0] : base::StringPiece();
265 const base::StringPiece value =
266 name_value.size() > 1 ? name_value[1] : base::StringPiece();
268 if (option == kOff) {
269 net::HttpStreamFactory::set_spdy_enabled(false);
270 continue;
272 if (option == kDisablePing) {
273 globals->enable_spdy_ping_based_connection_checking.set(false);
274 continue;
276 if (option == kExclude) {
277 globals->forced_spdy_exclusions.insert(
278 net::HostPortPair::FromURL(GURL(value.as_string())));
279 continue;
281 if (option == kDisableCompression) {
282 globals->enable_spdy_compression.set(false);
283 continue;
285 if (option == kInitialMaxConcurrentStreams) {
286 int streams;
287 if (base::StringToInt(value, &streams)) {
288 globals->initial_max_spdy_concurrent_streams.set(streams);
289 continue;
292 LOG(DFATAL) << "Unrecognized spdy option: " << option.as_string();
296 } // namespace
298 class IOThread::LoggingNetworkChangeObserver
299 : public net::NetworkChangeNotifier::IPAddressObserver,
300 public net::NetworkChangeNotifier::ConnectionTypeObserver,
301 public net::NetworkChangeNotifier::NetworkChangeObserver {
302 public:
303 // |net_log| must remain valid throughout our lifetime.
304 explicit LoggingNetworkChangeObserver(net::NetLog* net_log)
305 : net_log_(net_log) {
306 net::NetworkChangeNotifier::AddIPAddressObserver(this);
307 net::NetworkChangeNotifier::AddConnectionTypeObserver(this);
308 net::NetworkChangeNotifier::AddNetworkChangeObserver(this);
311 ~LoggingNetworkChangeObserver() override {
312 net::NetworkChangeNotifier::RemoveIPAddressObserver(this);
313 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
314 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this);
317 // NetworkChangeNotifier::IPAddressObserver implementation.
318 void OnIPAddressChanged() override {
319 VLOG(1) << "Observed a change to the network IP addresses";
321 net_log_->AddGlobalEntry(net::NetLog::TYPE_NETWORK_IP_ADDRESSES_CHANGED);
324 // NetworkChangeNotifier::ConnectionTypeObserver implementation.
325 void OnConnectionTypeChanged(
326 net::NetworkChangeNotifier::ConnectionType type) override {
327 std::string type_as_string =
328 net::NetworkChangeNotifier::ConnectionTypeToString(type);
330 VLOG(1) << "Observed a change to network connectivity state "
331 << type_as_string;
333 net_log_->AddGlobalEntry(
334 net::NetLog::TYPE_NETWORK_CONNECTIVITY_CHANGED,
335 net::NetLog::StringCallback("new_connection_type", &type_as_string));
338 // NetworkChangeNotifier::NetworkChangeObserver implementation.
339 void OnNetworkChanged(
340 net::NetworkChangeNotifier::ConnectionType type) override {
341 std::string type_as_string =
342 net::NetworkChangeNotifier::ConnectionTypeToString(type);
344 VLOG(1) << "Observed a network change to state " << type_as_string;
346 net_log_->AddGlobalEntry(
347 net::NetLog::TYPE_NETWORK_CHANGED,
348 net::NetLog::StringCallback("new_connection_type", &type_as_string));
351 private:
352 net::NetLog* net_log_;
353 DISALLOW_COPY_AND_ASSIGN(LoggingNetworkChangeObserver);
356 class SystemURLRequestContextGetter : public net::URLRequestContextGetter {
357 public:
358 explicit SystemURLRequestContextGetter(IOThread* io_thread);
360 // Implementation for net::UrlRequestContextGetter.
361 net::URLRequestContext* GetURLRequestContext() override;
362 scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner()
363 const override;
365 protected:
366 ~SystemURLRequestContextGetter() override;
368 private:
369 IOThread* const io_thread_; // Weak pointer, owned by BrowserProcess.
370 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
372 base::debug::LeakTracker<SystemURLRequestContextGetter> leak_tracker_;
375 SystemURLRequestContextGetter::SystemURLRequestContextGetter(
376 IOThread* io_thread)
377 : io_thread_(io_thread),
378 network_task_runner_(
379 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)) {
382 SystemURLRequestContextGetter::~SystemURLRequestContextGetter() {}
384 net::URLRequestContext* SystemURLRequestContextGetter::GetURLRequestContext() {
385 DCHECK_CURRENTLY_ON(BrowserThread::IO);
386 DCHECK(io_thread_->globals()->system_request_context.get());
388 return io_thread_->globals()->system_request_context.get();
391 scoped_refptr<base::SingleThreadTaskRunner>
392 SystemURLRequestContextGetter::GetNetworkTaskRunner() const {
393 return network_task_runner_;
396 IOThread::Globals::
397 SystemRequestContextLeakChecker::SystemRequestContextLeakChecker(
398 Globals* globals)
399 : globals_(globals) {
400 DCHECK(globals_);
403 IOThread::Globals::
404 SystemRequestContextLeakChecker::~SystemRequestContextLeakChecker() {
405 if (globals_->system_request_context.get())
406 globals_->system_request_context->AssertNoURLRequests();
409 IOThread::Globals::Globals()
410 : system_request_context_leak_checker(this),
411 ignore_certificate_errors(false),
412 testing_fixed_http_port(0),
413 testing_fixed_https_port(0),
414 enable_user_alternate_protocol_ports(false) {
417 IOThread::Globals::~Globals() {}
419 // |local_state| is passed in explicitly in order to (1) reduce implicit
420 // dependencies and (2) make IOThread more flexible for testing.
421 IOThread::IOThread(
422 PrefService* local_state,
423 policy::PolicyService* policy_service,
424 ChromeNetLog* net_log,
425 extensions::EventRouterForwarder* extension_event_router_forwarder)
426 : net_log_(net_log),
427 #if defined(ENABLE_EXTENSIONS)
428 extension_event_router_forwarder_(extension_event_router_forwarder),
429 #endif
430 globals_(NULL),
431 is_spdy_disabled_by_policy_(false),
432 is_quic_allowed_by_policy_(true),
433 creation_time_(base::TimeTicks::Now()),
434 weak_factory_(this) {
435 auth_schemes_ = local_state->GetString(prefs::kAuthSchemes);
436 negotiate_disable_cname_lookup_ = local_state->GetBoolean(
437 prefs::kDisableAuthNegotiateCnameLookup);
438 negotiate_enable_port_ = local_state->GetBoolean(
439 prefs::kEnableAuthNegotiatePort);
440 auth_server_whitelist_ = local_state->GetString(prefs::kAuthServerWhitelist);
441 auth_delegate_whitelist_ = local_state->GetString(
442 prefs::kAuthNegotiateDelegateWhitelist);
443 gssapi_library_name_ = local_state->GetString(prefs::kGSSAPILibraryName);
444 auth_android_negotiate_account_type_ =
445 local_state->GetString(prefs::kAuthAndroidNegotiateAccountType);
446 pref_proxy_config_tracker_.reset(
447 ProxyServiceFactory::CreatePrefProxyConfigTrackerOfLocalState(
448 local_state));
449 ChromeNetworkDelegate::InitializePrefsOnUIThread(
450 &system_enable_referrers_,
451 NULL,
452 NULL,
453 NULL,
454 local_state);
455 ssl_config_service_manager_.reset(
456 SSLConfigServiceManager::CreateDefaultManager(local_state));
458 base::Value* dns_client_enabled_default = new base::FundamentalValue(
459 chrome_browser_net::ConfigureAsyncDnsFieldTrial());
460 local_state->SetDefaultPrefValue(prefs::kBuiltInDnsClientEnabled,
461 dns_client_enabled_default);
462 chrome_browser_net::LogAsyncDnsPrefSource(
463 local_state->FindPreference(prefs::kBuiltInDnsClientEnabled));
465 dns_client_enabled_.Init(prefs::kBuiltInDnsClientEnabled,
466 local_state,
467 base::Bind(&IOThread::UpdateDnsClientEnabled,
468 base::Unretained(this)));
469 dns_client_enabled_.MoveToThread(
470 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
472 quick_check_enabled_.Init(prefs::kQuickCheckEnabled,
473 local_state);
474 quick_check_enabled_.MoveToThread(
475 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
477 #if defined(ENABLE_CONFIGURATION_POLICY)
478 is_spdy_disabled_by_policy_ = policy_service->GetPolicies(
479 policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME, std::string())).Get(
480 policy::key::kDisableSpdy) != NULL;
482 const base::Value* value = policy_service->GetPolicies(
483 policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME,
484 std::string())).GetValue(policy::key::kQuicAllowed);
485 if (value)
486 value->GetAsBoolean(&is_quic_allowed_by_policy_);
487 #endif // ENABLE_CONFIGURATION_POLICY
489 BrowserThread::SetDelegate(BrowserThread::IO, this);
492 IOThread::~IOThread() {
493 // This isn't needed for production code, but in tests, IOThread may
494 // be multiply constructed.
495 BrowserThread::SetDelegate(BrowserThread::IO, NULL);
497 pref_proxy_config_tracker_->DetachFromPrefService();
498 DCHECK(!globals_);
501 IOThread::Globals* IOThread::globals() {
502 DCHECK_CURRENTLY_ON(BrowserThread::IO);
503 return globals_;
506 void IOThread::SetGlobalsForTesting(Globals* globals) {
507 DCHECK_CURRENTLY_ON(BrowserThread::IO);
508 DCHECK(!globals || !globals_);
509 globals_ = globals;
512 ChromeNetLog* IOThread::net_log() {
513 return net_log_;
516 void IOThread::ChangedToOnTheRecord() {
517 DCHECK_CURRENTLY_ON(BrowserThread::UI);
518 BrowserThread::PostTask(
519 BrowserThread::IO,
520 FROM_HERE,
521 base::Bind(&IOThread::ChangedToOnTheRecordOnIOThread,
522 base::Unretained(this)));
525 net::URLRequestContextGetter* IOThread::system_url_request_context_getter() {
526 DCHECK_CURRENTLY_ON(BrowserThread::UI);
527 if (!system_url_request_context_getter_.get()) {
528 InitSystemRequestContext();
530 return system_url_request_context_getter_.get();
533 void IOThread::Init() {
534 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
535 // is fixed.
536 tracked_objects::ScopedTracker tracking_profile1(
537 FROM_HERE_WITH_EXPLICIT_FUNCTION("466432 IOThread::InitAsync::Start"));
538 TRACE_EVENT0("startup", "IOThread::InitAsync");
539 DCHECK_CURRENTLY_ON(BrowserThread::IO);
541 #if defined(USE_NSS_CERTS) || defined(OS_IOS)
542 net::SetMessageLoopForNSSHttpIO();
543 #endif
545 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
546 // is fixed.
547 tracked_objects::ScopedTracker tracking_profile2(
548 FROM_HERE_WITH_EXPLICIT_FUNCTION(
549 "466432 IOThread::InitAsync::CommandLineForCurrentProcess"));
550 const base::CommandLine& command_line =
551 *base::CommandLine::ForCurrentProcess();
553 DCHECK(!globals_);
554 globals_ = new Globals;
556 // Add an observer that will emit network change events to the ChromeNetLog.
557 // Assuming NetworkChangeNotifier dispatches in FIFO order, we should be
558 // logging the network change before other IO thread consumers respond to it.
559 network_change_observer_.reset(
560 new LoggingNetworkChangeObserver(net_log_));
562 // Setup the HistogramWatcher to run on the IO thread.
563 net::NetworkChangeNotifier::InitHistogramWatcher();
565 #if defined(ENABLE_EXTENSIONS)
566 globals_->extension_event_router_forwarder =
567 extension_event_router_forwarder_;
568 #endif
570 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
571 // is fixed.
572 tracked_objects::ScopedTracker tracking_profile3(
573 FROM_HERE_WITH_EXPLICIT_FUNCTION(
574 "466432 IOThread::InitAsync::ChromeNetworkDelegate"));
575 scoped_ptr<ChromeNetworkDelegate> chrome_network_delegate(
576 new ChromeNetworkDelegate(extension_event_router_forwarder(),
577 &system_enable_referrers_));
579 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
580 // is fixed.
581 tracked_objects::ScopedTracker tracking_profile4(
582 FROM_HERE_WITH_EXPLICIT_FUNCTION(
583 "466432 IOThread::InitAsync::CreateGlobalHostResolver"));
584 globals_->system_network_delegate = chrome_network_delegate.Pass();
585 globals_->host_resolver = CreateGlobalHostResolver(net_log_);
587 std::map<std::string, std::string> network_quality_estimator_params;
588 variations::GetVariationParams(kNetworkQualityEstimatorFieldTrialName,
589 &network_quality_estimator_params);
591 scoped_ptr<net::ExternalEstimateProvider> external_estimate_provider;
592 #if defined(OS_ANDROID)
593 external_estimate_provider.reset(
594 new chrome::android::ExternalEstimateProviderAndroid());
595 #endif
596 // Pass ownership.
597 globals_->network_quality_estimator.reset(new net::NetworkQualityEstimator(
598 external_estimate_provider.Pass(), network_quality_estimator_params));
600 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
601 // is fixed.
602 tracked_objects::ScopedTracker tracking_profile5(
603 FROM_HERE_WITH_EXPLICIT_FUNCTION(
604 "466432 IOThread::InitAsync::UpdateDnsClientEnabled::Start"));
605 UpdateDnsClientEnabled();
606 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
607 // is fixed.
608 tracked_objects::ScopedTracker tracking_profile6(
609 FROM_HERE_WITH_EXPLICIT_FUNCTION(
610 "466432 IOThread::InitAsync::UpdateDnsClientEnabled::End"));
611 #if defined(OS_CHROMEOS)
612 // Creates a CertVerifyProc that doesn't allow any profile-provided certs.
613 globals_->cert_verifier.reset(new net::MultiThreadedCertVerifier(
614 new chromeos::CertVerifyProcChromeOS()));
615 #else
616 globals_->cert_verifier.reset(new net::MultiThreadedCertVerifier(
617 net::CertVerifyProc::CreateDefault()));
618 #endif
620 globals_->transport_security_state.reset(new net::TransportSecurityState());
622 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
623 // is fixed.
624 tracked_objects::ScopedTracker tracking_profile8(
625 FROM_HERE_WITH_EXPLICIT_FUNCTION(
626 "466432 IOThread::InitAsync::CreateLogVerifiers::Start"));
627 std::vector<scoped_refptr<net::CTLogVerifier>> ct_logs(
628 net::ct::CreateLogVerifiersForKnownLogs());
630 // Add logs from command line
631 if (command_line.HasSwitch(switches::kCertificateTransparencyLog)) {
632 std::string switch_value = command_line.GetSwitchValueASCII(
633 switches::kCertificateTransparencyLog);
634 for (const base::StringPiece& curr_log : base::SplitStringPiece(
635 switch_value, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) {
636 std::vector<std::string> log_metadata = base::SplitString(
637 curr_log, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
638 CHECK_GE(log_metadata.size(), 3u)
639 << "CT log metadata missing: Switch format is "
640 << "'description:base64_key:url_without_schema'.";
641 std::string log_description(log_metadata[0]);
642 std::string log_url(std::string("https://") + log_metadata[2]);
643 std::string ct_public_key_data;
644 CHECK(base::Base64Decode(log_metadata[1], &ct_public_key_data))
645 << "Unable to decode CT public key.";
646 scoped_refptr<net::CTLogVerifier> external_log_verifier(
647 net::CTLogVerifier::Create(ct_public_key_data, log_description,
648 log_url));
649 CHECK(external_log_verifier) << "Unable to parse CT public key.";
650 VLOG(1) << "Adding log with description " << log_description;
651 ct_logs.push_back(external_log_verifier);
655 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
656 // is fixed.
657 tracked_objects::ScopedTracker tracking_profile9(
658 FROM_HERE_WITH_EXPLICIT_FUNCTION(
659 "466432 IOThread::InitAsync::CreateLogVerifiers::End"));
660 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
661 // is fixed.
662 tracked_objects::ScopedTracker tracking_profile7(
663 FROM_HERE_WITH_EXPLICIT_FUNCTION(
664 "466432 IOThread::InitAsync::CreateMultiLogVerifier"));
665 net::MultiLogCTVerifier* ct_verifier = new net::MultiLogCTVerifier();
666 globals_->cert_transparency_verifier.reset(ct_verifier);
667 // Add built-in logs
668 ct_verifier->AddLogs(ct_logs);
670 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
671 // is fixed.
672 tracked_objects::ScopedTracker tracking_profile10(
673 FROM_HERE_WITH_EXPLICIT_FUNCTION(
674 "466432 IOThread::InitAsync::CertPolicyEnforcer"));
675 net::CertPolicyEnforcer* policy_enforcer = new net::CertPolicyEnforcer;
676 globals_->cert_policy_enforcer.reset(policy_enforcer);
678 globals_->ssl_config_service = GetSSLConfigService();
680 globals_->http_auth_handler_factory.reset(CreateDefaultAuthHandlerFactory(
681 globals_->host_resolver.get()));
682 globals_->http_server_properties.reset(new net::HttpServerPropertiesImpl());
683 // For the ProxyScriptFetcher, we use a direct ProxyService.
684 globals_->proxy_script_fetcher_proxy_service.reset(
685 net::ProxyService::CreateDirectWithNetLog(net_log_));
686 // In-memory cookie store.
687 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
688 // is fixed.
689 tracked_objects::ScopedTracker tracking_profile11(
690 FROM_HERE_WITH_EXPLICIT_FUNCTION(
691 "466432 IOThread::InitAsync::CreateCookieStore::Start"));
692 globals_->system_cookie_store =
693 content::CreateCookieStore(content::CookieStoreConfig());
694 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
695 // is fixed.
696 tracked_objects::ScopedTracker tracking_profile12(
697 FROM_HERE_WITH_EXPLICIT_FUNCTION(
698 "466432 IOThread::InitAsync::CreateCookieStore::End"));
699 // In-memory channel ID store.
700 globals_->system_channel_id_service.reset(
701 new net::ChannelIDService(
702 new net::DefaultChannelIDStore(NULL),
703 base::WorkerPool::GetTaskRunner(true)));
704 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
705 // is fixed.
706 tracked_objects::ScopedTracker tracking_profile12_1(
707 FROM_HERE_WITH_EXPLICIT_FUNCTION(
708 "466432 IOThread::InitAsync::CreateDnsProbeService"));
709 globals_->dns_probe_service.reset(new chrome_browser_net::DnsProbeService());
710 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
711 // is fixed.
712 tracked_objects::ScopedTracker tracking_profile12_2(
713 FROM_HERE_WITH_EXPLICIT_FUNCTION(
714 "466432 IOThread::InitAsync::CreateHostMappingRules"));
715 globals_->host_mapping_rules.reset(new net::HostMappingRules());
716 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
717 // is fixed.
718 tracked_objects::ScopedTracker tracking_profile12_3(
719 FROM_HERE_WITH_EXPLICIT_FUNCTION(
720 "466432 IOThread::InitAsync::CreateHTTPUserAgentSettings"));
721 globals_->http_user_agent_settings.reset(
722 new net::StaticHttpUserAgentSettings(std::string(), GetUserAgent()));
723 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
724 // is fixed.
725 tracked_objects::ScopedTracker tracking_profile12_4(
726 FROM_HERE_WITH_EXPLICIT_FUNCTION(
727 "466432 IOThread::InitAsync::CommandLineConfiguration"));
728 if (command_line.HasSwitch(switches::kHostRules)) {
729 TRACE_EVENT_BEGIN0("startup", "IOThread::InitAsync:SetRulesFromString");
730 globals_->host_mapping_rules->SetRulesFromString(
731 command_line.GetSwitchValueASCII(switches::kHostRules));
732 TRACE_EVENT_END0("startup", "IOThread::InitAsync:SetRulesFromString");
734 if (command_line.HasSwitch(switches::kIgnoreCertificateErrors))
735 globals_->ignore_certificate_errors = true;
736 if (command_line.HasSwitch(switches::kTestingFixedHttpPort)) {
737 globals_->testing_fixed_http_port =
738 GetSwitchValueAsInt(command_line, switches::kTestingFixedHttpPort);
740 if (command_line.HasSwitch(switches::kTestingFixedHttpsPort)) {
741 globals_->testing_fixed_https_port =
742 GetSwitchValueAsInt(command_line, switches::kTestingFixedHttpsPort);
744 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
745 // is fixed.
746 tracked_objects::ScopedTracker tracking_profile12_5(
747 FROM_HERE_WITH_EXPLICIT_FUNCTION(
748 "466432 IOThread::InitAsync::QuicConfiguration"));
749 ConfigureQuic(command_line);
750 if (command_line.HasSwitch(
751 switches::kEnableUserAlternateProtocolPorts)) {
752 globals_->enable_user_alternate_protocol_ports = true;
754 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
755 // is fixed.
756 tracked_objects::ScopedTracker tracking_profile13(
757 FROM_HERE_WITH_EXPLICIT_FUNCTION(
758 "466432 IOThread::InitAsync::InitializeNetworkOptions"));
759 InitializeNetworkOptions(command_line);
761 TRACE_EVENT_BEGIN0("startup",
762 "IOThread::Init:ProxyScriptFetcherRequestContext");
763 globals_->proxy_script_fetcher_context.reset(
764 ConstructProxyScriptFetcherContext(globals_, net_log_));
765 TRACE_EVENT_END0("startup",
766 "IOThread::Init:ProxyScriptFetcherRequestContext");
768 const version_info::Channel channel = chrome::GetChannel();
769 if (channel == version_info::Channel::UNKNOWN ||
770 channel == version_info::Channel::CANARY ||
771 channel == version_info::Channel::DEV) {
772 globals_->url_request_backoff_manager.reset(
773 new net::URLRequestBackoffManager());
776 #if defined(OS_MACOSX) && !defined(OS_IOS)
777 // Start observing Keychain events. This needs to be done on the UI thread,
778 // as Keychain services requires a CFRunLoop.
779 BrowserThread::PostTask(BrowserThread::UI,
780 FROM_HERE,
781 base::Bind(&ObserveKeychainEvents));
782 #endif
784 // InitSystemRequestContext turns right around and posts a task back
785 // to the IO thread, so we can't let it run until we know the IO
786 // thread has started.
788 // Note that since we are at BrowserThread::Init time, the UI thread
789 // is blocked waiting for the thread to start. Therefore, posting
790 // this task to the main thread's message loop here is guaranteed to
791 // get it onto the message loop while the IOThread object still
792 // exists. However, the message might not be processed on the UI
793 // thread until after IOThread is gone, so use a weak pointer.
794 BrowserThread::PostTask(BrowserThread::UI,
795 FROM_HERE,
796 base::Bind(&IOThread::InitSystemRequestContext,
797 weak_factory_.GetWeakPtr()));
800 void IOThread::CleanUp() {
801 base::debug::LeakTracker<SafeBrowsingURLRequestContext>::CheckForLeaks();
803 #if defined(USE_NSS_CERTS) || defined(OS_IOS)
804 net::ShutdownNSSHttpIO();
805 #endif
807 system_url_request_context_getter_ = NULL;
809 // Release objects that the net::URLRequestContext could have been pointing
810 // to.
812 // Shutdown the HistogramWatcher on the IO thread.
813 net::NetworkChangeNotifier::ShutdownHistogramWatcher();
815 // This must be reset before the ChromeNetLog is destroyed.
816 network_change_observer_.reset();
818 system_proxy_config_service_.reset();
820 delete globals_;
821 globals_ = NULL;
823 base::debug::LeakTracker<SystemURLRequestContextGetter>::CheckForLeaks();
826 void IOThread::InitializeNetworkOptions(const base::CommandLine& command_line) {
827 // Only handle use-spdy command line flags if "spdy.disabled" preference is
828 // not disabled via policy.
829 if (is_spdy_disabled_by_policy_) {
830 base::FieldTrial* trial = base::FieldTrialList::Find(kSpdyFieldTrialName);
831 if (trial)
832 trial->Disable();
833 } else {
834 std::string group = base::FieldTrialList::FindFullName(kSpdyFieldTrialName);
835 VariationParameters params;
836 if (!variations::GetVariationParams(kSpdyFieldTrialName, &params)) {
837 params.clear();
839 ConfigureSpdyGlobals(command_line, group, params, globals_);
842 ConfigureTCPFastOpen(command_line);
844 // TODO(rch): Make the client socket factory a per-network session
845 // instance, constructed from a NetworkSession::Params, to allow us
846 // to move this option to IOThread::Globals &
847 // HttpNetworkSession::Params.
850 void IOThread::ConfigureTCPFastOpen(const base::CommandLine& command_line) {
851 const std::string trial_group =
852 base::FieldTrialList::FindFullName(kTCPFastOpenFieldTrialName);
853 if (trial_group == kTCPFastOpenHttpsEnabledGroupName)
854 globals_->enable_tcp_fast_open_for_ssl.set(true);
855 bool always_enable_if_supported =
856 command_line.HasSwitch(switches::kEnableTcpFastOpen);
857 // Check for OS support of TCP FastOpen, and turn it on for all connections
858 // if indicated by user.
859 net::CheckSupportAndMaybeEnableTCPFastOpen(always_enable_if_supported);
862 void IOThread::ConfigureSpdyGlobals(
863 const base::CommandLine& command_line,
864 base::StringPiece spdy_trial_group,
865 const VariationParameters& spdy_trial_params,
866 IOThread::Globals* globals) {
867 if (command_line.HasSwitch(switches::kTrustedSpdyProxy)) {
868 globals->trusted_spdy_proxy.set(
869 command_line.GetSwitchValueASCII(switches::kTrustedSpdyProxy));
871 if (command_line.HasSwitch(switches::kIgnoreUrlFetcherCertRequests))
872 net::URLFetcher::SetIgnoreCertificateRequests(true);
874 if (command_line.HasSwitch(switches::kUseSpdy)) {
875 std::string spdy_mode =
876 command_line.GetSwitchValueASCII(switches::kUseSpdy);
877 ConfigureSpdyGlobalsFromUseSpdyArgument(spdy_mode, globals);
878 return;
881 globals->next_protos.clear();
882 globals->next_protos.push_back(net::kProtoHTTP11);
883 bool enable_quic = false;
884 globals->enable_quic.CopyToIfSet(&enable_quic);
885 if (enable_quic) {
886 globals->next_protos.push_back(net::kProtoQUIC1SPDY3);
889 // No SPDY command-line flags have been specified. Examine trial groups.
890 if (spdy_trial_group.starts_with(kSpdyFieldTrialHoldbackGroupNamePrefix)) {
891 net::HttpStreamFactory::set_spdy_enabled(false);
892 return;
894 if (spdy_trial_group.starts_with(kSpdyFieldTrialSpdy31GroupNamePrefix)) {
895 globals->next_protos.push_back(net::kProtoSPDY31);
896 return;
898 if (spdy_trial_group.starts_with(kSpdyFieldTrialSpdy4GroupNamePrefix)) {
899 globals->next_protos.push_back(net::kProtoSPDY31);
900 globals->next_protos.push_back(net::kProtoHTTP2);
901 return;
903 if (spdy_trial_group.starts_with(kSpdyFieldTrialParametrizedPrefix)) {
904 bool spdy_enabled = false;
905 if (base::LowerCaseEqualsASCII(
906 GetVariationParam(spdy_trial_params, "enable_spdy31"), "true")) {
907 globals->next_protos.push_back(net::kProtoSPDY31);
908 spdy_enabled = true;
910 if (base::LowerCaseEqualsASCII(
911 GetVariationParam(spdy_trial_params, "enable_http2"), "true")) {
912 globals->next_protos.push_back(net::kProtoHTTP2);
913 spdy_enabled = true;
915 // TODO(bnc): HttpStreamFactory::spdy_enabled_ is redundant with
916 // globals->next_protos, can it be eliminated?
917 net::HttpStreamFactory::set_spdy_enabled(spdy_enabled);
918 return;
921 // By default, enable HTTP/2.
922 globals->next_protos.push_back(net::kProtoSPDY31);
923 globals->next_protos.push_back(net::kProtoHTTP2);
926 void IOThread::RegisterPrefs(PrefRegistrySimple* registry) {
927 registry->RegisterStringPref(prefs::kAuthSchemes,
928 "basic,digest,ntlm,negotiate");
929 registry->RegisterBooleanPref(prefs::kDisableAuthNegotiateCnameLookup, false);
930 registry->RegisterBooleanPref(prefs::kEnableAuthNegotiatePort, false);
931 registry->RegisterStringPref(prefs::kAuthServerWhitelist, std::string());
932 registry->RegisterStringPref(prefs::kAuthNegotiateDelegateWhitelist,
933 std::string());
934 registry->RegisterStringPref(prefs::kGSSAPILibraryName, std::string());
935 registry->RegisterStringPref(prefs::kAuthAndroidNegotiateAccountType,
936 std::string());
937 registry->RegisterStringPref(
938 data_reduction_proxy::prefs::kDataReductionProxy, std::string());
939 registry->RegisterBooleanPref(prefs::kEnableReferrers, true);
940 data_reduction_proxy::RegisterPrefs(registry);
941 registry->RegisterBooleanPref(prefs::kBuiltInDnsClientEnabled, true);
942 registry->RegisterBooleanPref(prefs::kQuickCheckEnabled, true);
945 net::HttpAuthHandlerFactory* IOThread::CreateDefaultAuthHandlerFactory(
946 net::HostResolver* resolver) {
947 net::HttpAuthFilterWhitelist* auth_filter_default_credentials = NULL;
948 if (!auth_server_whitelist_.empty()) {
949 auth_filter_default_credentials =
950 new net::HttpAuthFilterWhitelist(auth_server_whitelist_);
952 net::HttpAuthFilterWhitelist* auth_filter_delegate = NULL;
953 if (!auth_delegate_whitelist_.empty()) {
954 auth_filter_delegate =
955 new net::HttpAuthFilterWhitelist(auth_delegate_whitelist_);
957 globals_->url_security_manager.reset(
958 net::URLSecurityManager::Create(auth_filter_default_credentials,
959 auth_filter_delegate));
960 std::vector<std::string> supported_schemes = base::SplitString(
961 auth_schemes_, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
963 scoped_ptr<net::HttpAuthHandlerRegistryFactory> registry_factory(
964 net::HttpAuthHandlerRegistryFactory::Create(
965 supported_schemes, globals_->url_security_manager.get(), resolver,
966 gssapi_library_name_, auth_android_negotiate_account_type_,
967 negotiate_disable_cname_lookup_, negotiate_enable_port_));
968 return registry_factory.release();
971 void IOThread::ClearHostCache() {
972 DCHECK_CURRENTLY_ON(BrowserThread::IO);
974 net::HostCache* host_cache = globals_->host_resolver->GetHostCache();
975 if (host_cache)
976 host_cache->clear();
979 void IOThread::InitializeNetworkSessionParams(
980 net::HttpNetworkSession::Params* params) {
981 InitializeNetworkSessionParamsFromGlobals(*globals_, params);
984 void IOThread::InitializeNetworkSessionParamsFromGlobals(
985 const IOThread::Globals& globals,
986 net::HttpNetworkSession::Params* params) {
987 // The next two properties of the params don't seem to be
988 // elements of URLRequestContext, so they must be set here.
989 params->cert_policy_enforcer = globals.cert_policy_enforcer.get();
990 params->host_mapping_rules = globals.host_mapping_rules.get();
992 params->ignore_certificate_errors = globals.ignore_certificate_errors;
993 params->testing_fixed_http_port = globals.testing_fixed_http_port;
994 params->testing_fixed_https_port = globals.testing_fixed_https_port;
995 globals.enable_tcp_fast_open_for_ssl.CopyToIfSet(
996 &params->enable_tcp_fast_open_for_ssl);
998 globals.initial_max_spdy_concurrent_streams.CopyToIfSet(
999 &params->spdy_initial_max_concurrent_streams);
1000 globals.enable_spdy_compression.CopyToIfSet(
1001 &params->enable_spdy_compression);
1002 globals.enable_spdy_ping_based_connection_checking.CopyToIfSet(
1003 &params->enable_spdy_ping_based_connection_checking);
1004 globals.spdy_default_protocol.CopyToIfSet(
1005 &params->spdy_default_protocol);
1006 params->next_protos = globals.next_protos;
1007 globals.trusted_spdy_proxy.CopyToIfSet(&params->trusted_spdy_proxy);
1008 params->forced_spdy_exclusions = globals.forced_spdy_exclusions;
1009 globals.use_alternative_services.CopyToIfSet(
1010 &params->use_alternative_services);
1011 globals.alternative_service_probability_threshold.CopyToIfSet(
1012 &params->alternative_service_probability_threshold);
1014 globals.enable_quic.CopyToIfSet(&params->enable_quic);
1015 globals.enable_insecure_quic.CopyToIfSet(&params->enable_insecure_quic);
1016 globals.enable_quic_for_proxies.CopyToIfSet(&params->enable_quic_for_proxies);
1017 globals.quic_always_require_handshake_confirmation.CopyToIfSet(
1018 &params->quic_always_require_handshake_confirmation);
1019 globals.quic_disable_connection_pooling.CopyToIfSet(
1020 &params->quic_disable_connection_pooling);
1021 globals.quic_load_server_info_timeout_srtt_multiplier.CopyToIfSet(
1022 &params->quic_load_server_info_timeout_srtt_multiplier);
1023 globals.quic_enable_connection_racing.CopyToIfSet(
1024 &params->quic_enable_connection_racing);
1025 globals.quic_enable_non_blocking_io.CopyToIfSet(
1026 &params->quic_enable_non_blocking_io);
1027 globals.quic_prefer_aes.CopyToIfSet(&params->quic_prefer_aes);
1028 globals.quic_disable_disk_cache.CopyToIfSet(
1029 &params->quic_disable_disk_cache);
1030 globals.quic_max_number_of_lossy_connections.CopyToIfSet(
1031 &params->quic_max_number_of_lossy_connections);
1032 globals.quic_packet_loss_threshold.CopyToIfSet(
1033 &params->quic_packet_loss_threshold);
1034 globals.quic_socket_receive_buffer_size.CopyToIfSet(
1035 &params->quic_socket_receive_buffer_size);
1036 globals.enable_quic_port_selection.CopyToIfSet(
1037 &params->enable_quic_port_selection);
1038 globals.quic_max_packet_length.CopyToIfSet(&params->quic_max_packet_length);
1039 globals.quic_user_agent_id.CopyToIfSet(&params->quic_user_agent_id);
1040 globals.quic_supported_versions.CopyToIfSet(
1041 &params->quic_supported_versions);
1042 params->quic_connection_options = globals.quic_connection_options;
1044 globals.origin_to_force_quic_on.CopyToIfSet(
1045 &params->origin_to_force_quic_on);
1046 params->enable_user_alternate_protocol_ports =
1047 globals.enable_user_alternate_protocol_ports;
1050 base::TimeTicks IOThread::creation_time() const {
1051 return creation_time_;
1054 net::SSLConfigService* IOThread::GetSSLConfigService() {
1055 return ssl_config_service_manager_->Get();
1058 void IOThread::ChangedToOnTheRecordOnIOThread() {
1059 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1061 // Clear the host cache to avoid showing entries from the OTR session
1062 // in about:net-internals.
1063 ClearHostCache();
1066 void IOThread::InitSystemRequestContext() {
1067 if (system_url_request_context_getter_.get())
1068 return;
1069 // If we're in unit_tests, IOThread may not be run.
1070 if (!BrowserThread::IsMessageLoopValid(BrowserThread::IO))
1071 return;
1072 system_proxy_config_service_.reset(
1073 ProxyServiceFactory::CreateProxyConfigService(
1074 pref_proxy_config_tracker_.get()));
1075 system_url_request_context_getter_ =
1076 new SystemURLRequestContextGetter(this);
1077 // Safe to post an unretained this pointer, since IOThread is
1078 // guaranteed to outlive the IO BrowserThread.
1079 BrowserThread::PostTask(
1080 BrowserThread::IO,
1081 FROM_HERE,
1082 base::Bind(&IOThread::InitSystemRequestContextOnIOThread,
1083 base::Unretained(this)));
1086 void IOThread::InitSystemRequestContextOnIOThread() {
1087 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1088 DCHECK(!globals_->system_proxy_service.get());
1089 DCHECK(system_proxy_config_service_.get());
1091 const base::CommandLine& command_line =
1092 *base::CommandLine::ForCurrentProcess();
1093 globals_->system_proxy_service.reset(
1094 ProxyServiceFactory::CreateProxyService(
1095 net_log_,
1096 globals_->proxy_script_fetcher_context.get(),
1097 globals_->system_network_delegate.get(),
1098 system_proxy_config_service_.release(),
1099 command_line,
1100 quick_check_enabled_.GetValue()));
1102 globals_->system_request_context.reset(
1103 ConstructSystemRequestContext(globals_, net_log_));
1106 void IOThread::UpdateDnsClientEnabled() {
1107 globals()->host_resolver->SetDnsClientEnabled(*dns_client_enabled_);
1110 void IOThread::ConfigureQuic(const base::CommandLine& command_line) {
1111 // Always fetch the field trial group to ensure it is reported correctly.
1112 // The command line flags will be associated with a group that is reported
1113 // so long as trial is actually queried.
1114 std::string group =
1115 base::FieldTrialList::FindFullName(kQuicFieldTrialName);
1116 VariationParameters params;
1117 if (!variations::GetVariationParams(kQuicFieldTrialName, &params)) {
1118 params.clear();
1121 ConfigureQuicGlobals(command_line, group, params, is_quic_allowed_by_policy_,
1122 globals_);
1125 void IOThread::ConfigureQuicGlobals(
1126 const base::CommandLine& command_line,
1127 base::StringPiece quic_trial_group,
1128 const VariationParameters& quic_trial_params,
1129 bool quic_allowed_by_policy,
1130 IOThread::Globals* globals) {
1131 bool enable_quic = ShouldEnableQuic(command_line, quic_trial_group,
1132 quic_allowed_by_policy);
1133 globals->enable_quic.set(enable_quic);
1134 bool enable_quic_for_proxies = ShouldEnableQuicForProxies(
1135 command_line, quic_trial_group, quic_allowed_by_policy);
1136 globals->enable_quic_for_proxies.set(enable_quic_for_proxies);
1137 if (enable_quic) {
1138 globals->enable_insecure_quic.set(
1139 ShouldEnableInsecureQuic(command_line, quic_trial_params));
1140 globals->quic_always_require_handshake_confirmation.set(
1141 ShouldQuicAlwaysRequireHandshakeConfirmation(quic_trial_params));
1142 globals->quic_disable_connection_pooling.set(
1143 ShouldQuicDisableConnectionPooling(quic_trial_params));
1144 int receive_buffer_size = GetQuicSocketReceiveBufferSize(quic_trial_params);
1145 if (receive_buffer_size != 0) {
1146 globals->quic_socket_receive_buffer_size.set(receive_buffer_size);
1148 float load_server_info_timeout_srtt_multiplier =
1149 GetQuicLoadServerInfoTimeoutSrttMultiplier(quic_trial_params);
1150 if (load_server_info_timeout_srtt_multiplier != 0) {
1151 globals->quic_load_server_info_timeout_srtt_multiplier.set(
1152 load_server_info_timeout_srtt_multiplier);
1154 globals->quic_enable_connection_racing.set(
1155 ShouldQuicEnableConnectionRacing(quic_trial_params));
1156 globals->quic_enable_non_blocking_io.set(
1157 ShouldQuicEnableNonBlockingIO(quic_trial_params));
1158 globals->quic_disable_disk_cache.set(
1159 ShouldQuicDisableDiskCache(quic_trial_params));
1160 globals->quic_prefer_aes.set(
1161 ShouldQuicPreferAes(quic_trial_params));
1162 globals->use_alternative_services.set(
1163 ShouldQuicEnableAlternativeServices(quic_trial_params));
1164 int max_number_of_lossy_connections = GetQuicMaxNumberOfLossyConnections(
1165 quic_trial_params);
1166 if (max_number_of_lossy_connections != 0) {
1167 globals->quic_max_number_of_lossy_connections.set(
1168 max_number_of_lossy_connections);
1170 float packet_loss_threshold = GetQuicPacketLossThreshold(quic_trial_params);
1171 if (packet_loss_threshold != 0)
1172 globals->quic_packet_loss_threshold.set(packet_loss_threshold);
1173 globals->enable_quic_port_selection.set(
1174 ShouldEnableQuicPortSelection(command_line));
1175 globals->quic_connection_options =
1176 GetQuicConnectionOptions(command_line, quic_trial_params);
1179 size_t max_packet_length = GetQuicMaxPacketLength(command_line,
1180 quic_trial_params);
1181 if (max_packet_length != 0) {
1182 globals->quic_max_packet_length.set(max_packet_length);
1185 std::string quic_user_agent_id = chrome::GetChannelString();
1186 if (!quic_user_agent_id.empty())
1187 quic_user_agent_id.push_back(' ');
1188 quic_user_agent_id.append(
1189 version_info::GetProductNameAndVersionForUserAgent());
1190 quic_user_agent_id.push_back(' ');
1191 quic_user_agent_id.append(content::BuildOSCpuInfo());
1192 globals->quic_user_agent_id.set(quic_user_agent_id);
1194 net::QuicVersion version = GetQuicVersion(command_line, quic_trial_params);
1195 if (version != net::QUIC_VERSION_UNSUPPORTED) {
1196 net::QuicVersionVector supported_versions;
1197 supported_versions.push_back(version);
1198 globals->quic_supported_versions.set(supported_versions);
1201 double threshold = GetAlternativeProtocolProbabilityThreshold(
1202 command_line, quic_trial_params);
1203 if (threshold >=0 && threshold <= 1) {
1204 globals->alternative_service_probability_threshold.set(threshold);
1205 globals->http_server_properties->SetAlternativeServiceProbabilityThreshold(
1206 threshold);
1209 if (command_line.HasSwitch(switches::kOriginToForceQuicOn)) {
1210 net::HostPortPair quic_origin =
1211 net::HostPortPair::FromString(
1212 command_line.GetSwitchValueASCII(switches::kOriginToForceQuicOn));
1213 if (!quic_origin.IsEmpty()) {
1214 globals->origin_to_force_quic_on.set(quic_origin);
1219 bool IOThread::ShouldEnableQuic(const base::CommandLine& command_line,
1220 base::StringPiece quic_trial_group,
1221 bool quic_allowed_by_policy) {
1222 if (command_line.HasSwitch(switches::kDisableQuic) || !quic_allowed_by_policy)
1223 return false;
1225 if (command_line.HasSwitch(switches::kEnableQuic))
1226 return true;
1228 return quic_trial_group.starts_with(kQuicFieldTrialEnabledGroupName) ||
1229 quic_trial_group.starts_with(kQuicFieldTrialHttpsEnabledGroupName);
1232 bool IOThread::ShouldEnableQuicForProxies(const base::CommandLine& command_line,
1233 base::StringPiece quic_trial_group,
1234 bool quic_allowed_by_policy) {
1235 return ShouldEnableQuic(
1236 command_line, quic_trial_group, quic_allowed_by_policy) ||
1237 ShouldEnableQuicForDataReductionProxy();
1240 bool IOThread::ShouldEnableQuicForDataReductionProxy() {
1241 const base::CommandLine& command_line =
1242 *base::CommandLine::ForCurrentProcess();
1244 if (command_line.HasSwitch(switches::kDisableQuic))
1245 return false;
1247 return data_reduction_proxy::params::IsIncludedInQuicFieldTrial();
1250 bool IOThread::ShouldEnableInsecureQuic(
1251 const base::CommandLine& command_line,
1252 const VariationParameters& quic_trial_params) {
1253 if (command_line.HasSwitch(switches::kEnableInsecureQuic))
1254 return true;
1256 return base::LowerCaseEqualsASCII(
1257 GetVariationParam(quic_trial_params, "enable_insecure_quic"),
1258 "true");
1261 bool IOThread::ShouldEnableQuicPortSelection(
1262 const base::CommandLine& command_line) {
1263 if (command_line.HasSwitch(switches::kDisableQuicPortSelection))
1264 return false;
1266 if (command_line.HasSwitch(switches::kEnableQuicPortSelection))
1267 return true;
1269 return false; // Default to disabling port selection on all channels.
1272 net::QuicTagVector IOThread::GetQuicConnectionOptions(
1273 const base::CommandLine& command_line,
1274 const VariationParameters& quic_trial_params) {
1275 if (command_line.HasSwitch(switches::kQuicConnectionOptions)) {
1276 return net::QuicUtils::ParseQuicConnectionOptions(
1277 command_line.GetSwitchValueASCII(switches::kQuicConnectionOptions));
1280 VariationParameters::const_iterator it =
1281 quic_trial_params.find("connection_options");
1282 if (it == quic_trial_params.end()) {
1283 return net::QuicTagVector();
1286 return net::QuicUtils::ParseQuicConnectionOptions(it->second);
1289 double IOThread::GetAlternativeProtocolProbabilityThreshold(
1290 const base::CommandLine& command_line,
1291 const VariationParameters& quic_trial_params) {
1292 double value;
1293 if (command_line.HasSwitch(
1294 switches::kAlternativeServiceProbabilityThreshold)) {
1295 if (base::StringToDouble(
1296 command_line.GetSwitchValueASCII(
1297 switches::kAlternativeServiceProbabilityThreshold),
1298 &value)) {
1299 return value;
1302 if (command_line.HasSwitch(switches::kEnableQuic)) {
1303 return 0;
1305 // TODO(bnc): Remove when new parameter name rolls out and server
1306 // configuration is changed.
1307 if (base::StringToDouble(
1308 GetVariationParam(quic_trial_params,
1309 "alternate_protocol_probability_threshold"),
1310 &value)) {
1311 return value;
1313 if (base::StringToDouble(
1314 GetVariationParam(quic_trial_params,
1315 "alternative_service_probability_threshold"),
1316 &value)) {
1317 return value;
1319 return -1;
1322 bool IOThread::ShouldQuicAlwaysRequireHandshakeConfirmation(
1323 const VariationParameters& quic_trial_params) {
1324 return base::LowerCaseEqualsASCII(
1325 GetVariationParam(quic_trial_params,
1326 "always_require_handshake_confirmation"),
1327 "true");
1330 bool IOThread::ShouldQuicDisableConnectionPooling(
1331 const VariationParameters& quic_trial_params) {
1332 return base::LowerCaseEqualsASCII(
1333 GetVariationParam(quic_trial_params, "disable_connection_pooling"),
1334 "true");
1337 float IOThread::GetQuicLoadServerInfoTimeoutSrttMultiplier(
1338 const VariationParameters& quic_trial_params) {
1339 double value;
1340 if (base::StringToDouble(GetVariationParam(quic_trial_params,
1341 "load_server_info_time_to_srtt"),
1342 &value)) {
1343 return static_cast<float>(value);
1345 return 0.0f;
1348 bool IOThread::ShouldQuicEnableConnectionRacing(
1349 const VariationParameters& quic_trial_params) {
1350 return base::LowerCaseEqualsASCII(
1351 GetVariationParam(quic_trial_params, "enable_connection_racing"),
1352 "true");
1355 bool IOThread::ShouldQuicEnableNonBlockingIO(
1356 const VariationParameters& quic_trial_params) {
1357 return base::LowerCaseEqualsASCII(
1358 GetVariationParam(quic_trial_params, "enable_non_blocking_io"),
1359 "true");
1362 bool IOThread::ShouldQuicDisableDiskCache(
1363 const VariationParameters& quic_trial_params) {
1364 return base::LowerCaseEqualsASCII(
1365 GetVariationParam(quic_trial_params, "disable_disk_cache"), "true");
1368 bool IOThread::ShouldQuicPreferAes(
1369 const VariationParameters& quic_trial_params) {
1370 return base::LowerCaseEqualsASCII(
1371 GetVariationParam(quic_trial_params, "prefer_aes"), "true");
1374 bool IOThread::ShouldQuicEnableAlternativeServices(
1375 const VariationParameters& quic_trial_params) {
1376 return base::LowerCaseEqualsASCII(
1377 GetVariationParam(quic_trial_params, "use_alternative_services"), "true");
1380 int IOThread::GetQuicMaxNumberOfLossyConnections(
1381 const VariationParameters& quic_trial_params) {
1382 int value;
1383 if (base::StringToInt(GetVariationParam(quic_trial_params,
1384 "max_number_of_lossy_connections"),
1385 &value)) {
1386 return value;
1388 return 0;
1391 float IOThread::GetQuicPacketLossThreshold(
1392 const VariationParameters& quic_trial_params) {
1393 double value;
1394 if (base::StringToDouble(GetVariationParam(quic_trial_params,
1395 "packet_loss_threshold"),
1396 &value)) {
1397 return static_cast<float>(value);
1399 return 0.0f;
1402 int IOThread::GetQuicSocketReceiveBufferSize(
1403 const VariationParameters& quic_trial_params) {
1404 int value;
1405 if (base::StringToInt(GetVariationParam(quic_trial_params,
1406 "receive_buffer_size"),
1407 &value)) {
1408 return value;
1410 return 0;
1413 size_t IOThread::GetQuicMaxPacketLength(
1414 const base::CommandLine& command_line,
1415 const VariationParameters& quic_trial_params) {
1416 if (command_line.HasSwitch(switches::kQuicMaxPacketLength)) {
1417 unsigned value;
1418 if (!base::StringToUint(
1419 command_line.GetSwitchValueASCII(switches::kQuicMaxPacketLength),
1420 &value)) {
1421 return 0;
1423 return value;
1426 unsigned value;
1427 if (base::StringToUint(GetVariationParam(quic_trial_params,
1428 "max_packet_length"),
1429 &value)) {
1430 return value;
1432 return 0;
1435 net::QuicVersion IOThread::GetQuicVersion(
1436 const base::CommandLine& command_line,
1437 const VariationParameters& quic_trial_params) {
1438 if (command_line.HasSwitch(switches::kQuicVersion)) {
1439 return ParseQuicVersion(
1440 command_line.GetSwitchValueASCII(switches::kQuicVersion));
1443 return ParseQuicVersion(GetVariationParam(quic_trial_params, "quic_version"));
1446 net::QuicVersion IOThread::ParseQuicVersion(const std::string& quic_version) {
1447 net::QuicVersionVector supported_versions = net::QuicSupportedVersions();
1448 for (size_t i = 0; i < supported_versions.size(); ++i) {
1449 net::QuicVersion version = supported_versions[i];
1450 if (net::QuicVersionToString(version) == quic_version) {
1451 return version;
1455 return net::QUIC_VERSION_UNSUPPORTED;
1458 net::URLRequestContext* IOThread::ConstructSystemRequestContext(
1459 IOThread::Globals* globals,
1460 net::NetLog* net_log) {
1461 net::URLRequestContext* context = new SystemURLRequestContext;
1462 context->set_net_log(net_log);
1463 context->set_host_resolver(globals->host_resolver.get());
1464 context->set_cert_verifier(globals->cert_verifier.get());
1465 context->set_transport_security_state(
1466 globals->transport_security_state.get());
1467 context->set_cert_transparency_verifier(
1468 globals->cert_transparency_verifier.get());
1469 context->set_ssl_config_service(globals->ssl_config_service.get());
1470 context->set_http_auth_handler_factory(
1471 globals->http_auth_handler_factory.get());
1472 context->set_proxy_service(globals->system_proxy_service.get());
1474 globals->system_url_request_job_factory.reset(
1475 new net::URLRequestJobFactoryImpl());
1476 context->set_job_factory(globals->system_url_request_job_factory.get());
1478 context->set_cookie_store(globals->system_cookie_store.get());
1479 context->set_channel_id_service(
1480 globals->system_channel_id_service.get());
1481 context->set_network_delegate(globals->system_network_delegate.get());
1482 context->set_http_user_agent_settings(
1483 globals->http_user_agent_settings.get());
1484 context->set_network_quality_estimator(
1485 globals->network_quality_estimator.get());
1486 context->set_backoff_manager(globals->url_request_backoff_manager.get());
1488 context->set_http_server_properties(
1489 globals->http_server_properties->GetWeakPtr());
1491 net::HttpNetworkSession::Params system_params;
1492 InitializeNetworkSessionParamsFromGlobals(*globals, &system_params);
1493 net::URLRequestContextBuilder::SetHttpNetworkSessionComponents(
1494 context, &system_params);
1496 globals->system_http_transaction_factory.reset(
1497 new net::HttpNetworkLayer(new net::HttpNetworkSession(system_params)));
1498 context->set_http_transaction_factory(
1499 globals->system_http_transaction_factory.get());
1501 return context;
1504 net::URLRequestContext* IOThread::ConstructProxyScriptFetcherContext(
1505 IOThread::Globals* globals,
1506 net::NetLog* net_log) {
1507 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
1508 // is fixed.
1509 tracked_objects::ScopedTracker tracking_profile1(
1510 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1511 "466432 IOThread::ConstructProxyScriptFetcherContext1"));
1512 net::URLRequestContext* context = new net::URLRequestContext;
1513 context->set_net_log(net_log);
1514 context->set_host_resolver(globals->host_resolver.get());
1515 context->set_cert_verifier(globals->cert_verifier.get());
1516 context->set_transport_security_state(
1517 globals->transport_security_state.get());
1518 context->set_cert_transparency_verifier(
1519 globals->cert_transparency_verifier.get());
1520 context->set_ssl_config_service(globals->ssl_config_service.get());
1521 context->set_http_auth_handler_factory(
1522 globals->http_auth_handler_factory.get());
1523 context->set_proxy_service(globals->proxy_script_fetcher_proxy_service.get());
1525 context->set_job_factory(
1526 globals->proxy_script_fetcher_url_request_job_factory.get());
1528 context->set_cookie_store(globals->system_cookie_store.get());
1529 context->set_channel_id_service(
1530 globals->system_channel_id_service.get());
1531 context->set_network_delegate(globals->system_network_delegate.get());
1532 context->set_http_user_agent_settings(
1533 globals->http_user_agent_settings.get());
1534 context->set_http_server_properties(
1535 globals->http_server_properties->GetWeakPtr());
1537 net::HttpNetworkSession::Params session_params;
1538 InitializeNetworkSessionParamsFromGlobals(*globals, &session_params);
1539 net::URLRequestContextBuilder::SetHttpNetworkSessionComponents(
1540 context, &session_params);
1542 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
1543 // is fixed.
1544 tracked_objects::ScopedTracker tracking_profile2(
1545 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1546 "466432 IOThread::ConstructProxyScriptFetcherContext2"));
1547 scoped_refptr<net::HttpNetworkSession> network_session(
1548 new net::HttpNetworkSession(session_params));
1549 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466432
1550 // is fixed.
1551 tracked_objects::ScopedTracker tracking_profile3(
1552 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1553 "466432 IOThread::ConstructProxyScriptFetcherContext3"));
1554 globals->proxy_script_fetcher_http_transaction_factory
1555 .reset(new net::HttpNetworkLayer(network_session.get()));
1556 context->set_http_transaction_factory(
1557 globals->proxy_script_fetcher_http_transaction_factory.get());
1559 scoped_ptr<net::URLRequestJobFactoryImpl> job_factory(
1560 new net::URLRequestJobFactoryImpl());
1562 job_factory->SetProtocolHandler(
1563 url::kDataScheme, make_scoped_ptr(new net::DataProtocolHandler()));
1564 job_factory->SetProtocolHandler(
1565 url::kFileScheme,
1566 make_scoped_ptr(new net::FileProtocolHandler(
1567 content::BrowserThread::GetBlockingPool()
1568 ->GetTaskRunnerWithShutdownBehavior(
1569 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN))));
1570 #if !defined(DISABLE_FTP_SUPPORT)
1571 globals->proxy_script_fetcher_ftp_transaction_factory.reset(
1572 new net::FtpNetworkLayer(globals->host_resolver.get()));
1573 job_factory->SetProtocolHandler(
1574 url::kFtpScheme,
1575 make_scoped_ptr(new net::FtpProtocolHandler(
1576 globals->proxy_script_fetcher_ftp_transaction_factory.get())));
1577 #endif
1578 globals->proxy_script_fetcher_url_request_job_factory = job_factory.Pass();
1580 context->set_job_factory(
1581 globals->proxy_script_fetcher_url_request_job_factory.get());
1583 // TODO(rtenneti): We should probably use HttpServerPropertiesManager for the
1584 // system URLRequestContext too. There's no reason this should be tied to a
1585 // profile.
1586 return context;