blob: a730d142064ab6be67c3112667264af7945a6655 [file] [log] [blame]
[email protected]d24fc3a02012-02-11 02:08:341// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]0ac83682010-01-22 17:46:272// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/io_thread.h"
[email protected]e83326f2010-07-31 17:29:256
dchenge73d8520c2015-12-27 01:19:097#include <utility>
[email protected]bcefe0f2010-11-10 16:19:108#include <vector>
9
[email protected]284303b62013-11-28 15:11:5410#include "base/base64.h"
[email protected]21ee224e2011-11-21 02:17:5311#include "base/bind.h"
12#include "base/bind_helpers.h"
[email protected]aa84a7e2012-03-15 21:29:0613#include "base/command_line.h"
[email protected]c93123fa2012-04-19 02:49:4814#include "base/compiler_specific.h"
[email protected]58580352010-10-26 04:07:5015#include "base/debug/leak_tracker.h"
zhongyi81f85c6d92015-10-16 19:34:1416#include "base/environment.h"
davidben2a811e4e2015-12-01 10:49:3417#include "base/files/file_path.h"
[email protected]0ac83682010-01-22 17:46:2718#include "base/logging.h"
avi6846aef2015-12-26 01:09:3819#include "base/macros.h"
dcheng4af48582016-04-19 00:29:3520#include "base/memory/ptr_util.h"
[email protected]903e63382013-06-01 00:40:5821#include "base/metrics/field_trial.h"
davidben45eb19952016-05-05 16:30:4422#include "base/metrics/histogram_macros.h"
Gabriel Charette5ff87ce2017-05-16 18:03:4523#include "base/single_thread_task_runner.h"
[email protected]7286e3fc2011-07-19 22:13:2424#include "base/stl_util.h"
[email protected]3ea1b182013-02-08 22:38:4125#include "base/strings/string_number_conversions.h"
bnce3553d92014-10-30 22:29:4526#include "base/strings/string_piece.h"
[email protected]1988e1c2013-02-28 20:27:4227#include "base/strings/string_split.h"
[email protected]9c7ddc92013-06-11 01:40:5728#include "base/strings/string_util.h"
davidben2a811e4e2015-12-01 10:49:3429#include "base/strings/utf_string_conversions.h"
fdoraydd7eb402017-04-26 21:44:2530#include "base/task_scheduler/post_task.h"
[email protected]3fc40c142011-12-01 13:09:0431#include "base/threading/thread.h"
[email protected]d827e112014-03-31 17:45:0532#include "base/time/time.h"
primiano6221e572015-01-28 12:14:3433#include "base/trace_event/trace_event.h"
[email protected]addb3242011-06-13 21:39:1634#include "build/build_config.h"
[email protected]df2840d2011-02-20 16:32:3235#include "chrome/browser/browser_process.h"
sclittlea133de02015-11-10 23:54:2136#include "chrome/browser/data_usage/tab_id_annotator.h"
kundajif617523b2016-09-09 22:41:2437#include "chrome/browser/data_use_measurement/chrome_data_use_ascriber.h"
[email protected]026876f32012-08-22 23:53:4038#include "chrome/browser/net/async_dns_field_trial.h"
[email protected]c38831a12011-10-28 12:44:4939#include "chrome/browser/net/chrome_network_delegate.h"
[email protected]4588b3d2012-11-14 00:37:3840#include "chrome/browser/net/dns_probe_service.h"
[email protected]db0e86dd2011-03-16 14:47:2141#include "chrome/browser/net/proxy_service_factory.h"
eranm3c2d6432016-06-01 10:17:2742#include "chrome/browser/net/sth_distributor_provider.h"
martinkr8402d3c2017-05-23 20:10:4743#include "chrome/browser/ssl/ignore_errors_cert_verifier.h"
sdefresne9fb67692015-08-03 18:48:2244#include "chrome/common/channel_info.h"
[email protected]aa051272014-03-10 05:56:5645#include "chrome/common/chrome_content_client.h"
[email protected]0ac83682010-01-22 17:46:2746#include "chrome/common/chrome_switches.h"
[email protected]bcefe0f2010-11-10 16:19:1047#include "chrome/common/pref_names.h"
eranm3c2d6432016-06-01 10:17:2748#include "components/certificate_transparency/tree_state_tracker.h"
megjablon3476e042014-10-14 19:21:5949#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_prefs.h"
sclittle3f98c6c2015-11-19 22:59:2950#include "components/data_usage/core/data_use_aggregator.h"
sclittlec441f782015-11-12 01:12:0951#include "components/data_usage/core/data_use_amortizer.h"
sclittle3f98c6c2015-11-19 22:59:2952#include "components/data_usage/core/data_use_annotator.h"
kundajic6135962016-09-26 17:54:2853#include "components/data_use_measurement/core/data_use_ascriber.h"
gayane0b46091c2016-04-07 21:01:0554#include "components/metrics/metrics_service.h"
drogerc690e8802015-09-21 14:29:1655#include "components/net_log/chrome_net_log.h"
bnc210d6f32016-05-24 07:40:4756#include "components/network_session_configurator/network_session_configurator.h"
[email protected]76b4b152013-12-08 21:10:0457#include "components/policy/core/common/policy_service.h"
brettw39d6ba42016-08-24 16:56:3858#include "components/policy/policy_constants.h"
brettwb1fc1b82016-02-02 00:19:0859#include "components/prefs/pref_registry_simple.h"
60#include "components/prefs/pref_service.h"
abhishek.a2171c612852015-08-31 10:48:1961#include "components/proxy_config/pref_proxy_config_tracker.h"
[email protected]488a0e252014-06-25 04:37:4462#include "components/variations/variations_associated_data.h"
sdefresne9fb67692015-08-03 18:48:2263#include "components/version_info/version_info.h"
[email protected]c38831a12011-10-28 12:44:4964#include "content/public/browser/browser_thread.h"
[email protected]7c4b66b2014-01-04 12:28:1365#include "content/public/browser/cookie_store_factory.h"
tbansal15973c32017-05-10 18:40:4466#include "content/public/browser/network_quality_observer_factory.h"
eustasfbec9132015-12-30 14:56:5167#include "content/public/common/content_features.h"
brettw90e92602015-10-10 00:12:4068#include "content/public/common/content_switches.h"
rtenneti4d126a72015-06-23 17:32:0969#include "content/public/common/user_agent.h"
brettw00899e62016-11-12 02:10:1770#include "extensions/features/features.h"
[email protected]c2dad292012-09-07 21:27:3571#include "net/base/host_mapping_rules.h"
pauljensen7b34e522016-05-12 18:20:5972#include "net/base/logging_network_change_observer.h"
rdsmith60e6e6fb2015-03-05 16:49:2073#include "net/base/sdch_manager.h"
rsleevi6df54182016-06-13 14:34:2374#include "net/cert/caching_cert_verifier.h"
[email protected]6e7845ae2013-03-29 21:48:1175#include "net/cert/cert_verifier.h"
[email protected]f46f6d52014-02-08 04:00:3976#include "net/cert/cert_verify_proc.h"
[email protected]284303b62013-11-28 15:11:5477#include "net/cert/ct_known_logs.h"
davidbeneb5f8ef32014-09-04 14:14:3278#include "net/cert/ct_log_verifier.h"
estark6f9b3d82016-01-12 21:37:0579#include "net/cert/ct_policy_enforcer.h"
[email protected]284303b62013-11-28 15:11:5480#include "net/cert/ct_verifier.h"
davidbeneb5f8ef32014-09-04 14:14:3281#include "net/cert/multi_log_ct_verifier.h"
[email protected]f46f6d52014-02-08 04:00:3982#include "net/cert/multi_threaded_cert_verifier.h"
eranm3c2d6432016-06-01 10:17:2783#include "net/cert/sth_distributor.h"
84#include "net/cert/sth_observer.h"
[email protected]9a6c2aa2014-01-11 22:39:3985#include "net/cookies/cookie_store.h"
[email protected]bc71b8772013-04-10 20:55:1686#include "net/dns/host_cache.h"
[email protected]f2cb3cf2013-03-21 01:40:5387#include "net/dns/host_resolver.h"
88#include "net/dns/mapped_host_resolver.h"
[email protected]eb3cac72010-02-26 21:07:4589#include "net/http/http_auth_filter.h"
[email protected]fa55e192010-02-15 14:25:5090#include "net/http/http_auth_handler_factory.h"
aberentbba302d2015-12-03 10:20:1991#include "net/http/http_auth_preferences.h"
[email protected]2fb629202010-12-23 23:52:5792#include "net/http/http_network_layer.h"
[email protected]17291a022011-10-10 07:32:5393#include "net/http/http_server_properties_impl.h"
brettwa1228ebb2016-10-28 03:51:3494#include "net/net_features.h"
tbansalca83c002016-04-28 20:56:2895#include "net/nqe/external_estimate_provider.h"
tbansald3e08ca62017-05-04 19:51:0996#include "net/nqe/network_quality_estimator_params.h"
[email protected]6104ea5d2011-04-27 21:37:1297#include "net/proxy/proxy_config_service.h"
[email protected]86933612010-10-16 23:10:3398#include "net/proxy/proxy_script_fetcher_impl.h"
[email protected]6104ea5d2011-04-27 21:37:1299#include "net/proxy/proxy_service.h"
rch4c967ea2016-12-22 01:41:07100#include "net/quic/chromium/quic_utils_chromium.h"
zhongyi81f85c6d92015-10-16 19:34:14101#include "net/socket/ssl_client_socket.h"
[email protected]717e4e22013-04-10 20:52:23102#include "net/socket/tcp_client_socket.h"
[email protected]6b8a3c742014-07-25 00:25:35103#include "net/ssl/channel_id_service.h"
104#include "net/ssl/default_channel_id_store.h"
[email protected]b3ae2db2013-05-30 05:00:05105#include "net/url_request/data_protocol_handler.h"
106#include "net/url_request/file_protocol_handler.h"
107#include "net/url_request/ftp_protocol_handler.h"
[email protected]aa051272014-03-10 05:56:56108#include "net/url_request/static_http_user_agent_settings.h"
[email protected]3dc1bc42012-06-19 08:20:53109#include "net/url_request/url_fetcher.h"
[email protected]f9c8c7c2014-07-31 16:42:31110#include "net/url_request/url_request_context.h"
wjmacleanea309f72015-08-25 20:56:59111#include "net/url_request/url_request_context_builder.h"
[email protected]f9c8c7c2014-07-31 16:42:31112#include "net/url_request/url_request_context_getter.h"
mmenkef2e53b22017-05-15 18:40:18113#include "net/url_request/url_request_context_storage.h"
[email protected]b3ae2db2013-05-30 05:00:05114#include "net/url_request/url_request_job_factory_impl.h"
brettw03e61962016-03-15 06:27:04115#include "url/url_constants.h"
[email protected]0ac83682010-01-22 17:46:27116
brettw00899e62016-11-12 02:10:17117#if BUILDFLAG(ENABLE_EXTENSIONS)
[email protected]84b7a552014-07-19 04:52:06118#include "chrome/browser/extensions/event_router_forwarder.h"
119#endif
120
jam1c5a91492016-02-24 20:47:53121#if defined(USE_NSS_CERTS)
eromaned744f32015-04-09 06:35:49122#include "net/cert_net/nss_ocsp.h"
[email protected]a592c0432012-12-01 18:10:29123#endif
[email protected]77feb462011-05-16 23:37:25124
zpengdb4a58e2017-01-10 17:40:32125#if defined(OS_ANDROID)
jeremyimb6c97ae2015-01-05 22:57:14126#include "base/android/build_info.h"
megjablon174bc342015-11-05 00:30:06127#include "chrome/browser/android/data_usage/external_data_use_observer.h"
tbansaldafbb3e2015-08-19 19:55:33128#include "chrome/browser/android/net/external_estimate_provider_android.h"
sclittlec441f782015-11-12 01:12:09129#include "components/data_usage/android/traffic_stats_amortizer.h"
estark9a263782017-01-20 02:13:04130#include "net/cert/cert_net_fetcher.h"
131#include "net/cert/cert_verify_proc_android.h"
132#include "net/cert_net/cert_net_fetcher_impl.h"
zpengdb4a58e2017-01-10 17:40:32133#endif // defined(OS_ANDROID)
jeremyimb6c97ae2015-01-05 22:57:14134
[email protected]f46f6d52014-02-08 04:00:39135#if defined(OS_CHROMEOS)
136#include "chrome/browser/chromeos/net/cert_verify_proc_chromeos.h"
zentaroe0326352017-05-09 13:11:51137#include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
[email protected]106ccd2c2014-06-17 09:21:00138#include "chromeos/network/host_resolver_impl_chromeos.h"
[email protected]f46f6d52014-02-08 04:00:39139#endif
140
davidben45eb19952016-05-05 16:30:44141#if defined(OS_ANDROID) && defined(ARCH_CPU_ARMEL)
davidben45eb19952016-05-05 16:30:44142#include "crypto/openssl_util.h"
tfarina29a3a1742016-10-28 18:47:33143#include "third_party/boringssl/src/include/openssl/cpu.h"
davidben45eb19952016-05-05 16:30:44144#endif
145
[email protected]631bb742011-11-02 11:29:39146using content::BrowserThread;
147
[email protected]075c0322012-02-14 00:56:44148class SafeBrowsingURLRequestContext;
149
[email protected]21ee224e2011-11-21 02:17:53150// The IOThread object must outlive any tasks posted to the IO thread before the
151// Quit task, so base::Bind() calls are not refcounted.
152
[email protected]0ac83682010-01-22 17:46:27153namespace {
154
tbansalb177b5392015-06-25 11:13:02155// Field trial for network quality estimator. Seeds RTT and downstream
156// throughput observations with values that correspond to the connection type
157// determined by the operating system.
158const char kNetworkQualityEstimatorFieldTrialName[] = "NetworkQualityEstimator";
159
jam1c5a91492016-02-24 20:47:53160#if defined(OS_MACOSX)
[email protected]11f5e3a2012-09-27 00:30:13161void ObserveKeychainEvents() {
thestig00844cea2015-09-08 21:44:52162 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]11f5e3a2012-09-27 00:30:13163 net::CertDatabase::GetInstance()->SetMessageLoopForKeychainEvents();
164}
165#endif
166
zhongyi81f85c6d92015-10-16 19:34:14167// Gets file path into ssl_keylog_file from command line argument or
168// environment variable. Command line argument has priority when
169// both specified.
davidben2a811e4e2015-12-01 10:49:34170base::FilePath GetSSLKeyLogFile(const base::CommandLine& command_line) {
zhongyi81f85c6d92015-10-16 19:34:14171 if (command_line.HasSwitch(switches::kSSLKeyLogFile)) {
davidben2a811e4e2015-12-01 10:49:34172 base::FilePath path =
173 command_line.GetSwitchValuePath(switches::kSSLKeyLogFile);
174 if (!path.empty())
175 return path;
zhongyi81f85c6d92015-10-16 19:34:14176 LOG(WARNING) << "ssl-key-log-file argument missing";
177 }
davidben2a811e4e2015-12-01 10:49:34178
dcheng4af48582016-04-19 00:29:35179 std::unique_ptr<base::Environment> env(base::Environment::Create());
davidben2a811e4e2015-12-01 10:49:34180 std::string path_str;
181 env->GetVar("SSLKEYLOGFILE", &path_str);
182#if defined(OS_WIN)
183 // base::Environment returns environment variables in UTF-8 on Windows.
184 return base::FilePath(base::UTF8ToUTF16(path_str));
185#else
186 return base::FilePath(path_str);
187#endif
zhongyi81f85c6d92015-10-16 19:34:14188}
189
[email protected]ee4c30d2012-11-07 15:08:43190// Used for the "system" URLRequestContext.
191class SystemURLRequestContext : public net::URLRequestContext {
[email protected]77feb462011-05-16 23:37:25192 public:
193 SystemURLRequestContext() {
jam1c5a91492016-02-24 20:47:53194#if defined(USE_NSS_CERTS)
[email protected]8c434cbc2012-03-14 14:25:09195 net::SetURLRequestContextForNSSHttpIO(this);
[email protected]a592c0432012-12-01 18:10:29196#endif
estark9a263782017-01-20 02:13:04197#if defined(OS_ANDROID)
198 net::CertVerifyProcAndroid::SetCertNetFetcher(
199 net::CreateCertNetFetcher(this));
200#endif
[email protected]77feb462011-05-16 23:37:25201 }
202
Daniel Chenga542fca2014-10-21 09:51:29203 ~SystemURLRequestContext() override {
[email protected]424559492014-07-22 00:27:40204 AssertNoURLRequests();
jam1c5a91492016-02-24 20:47:53205#if defined(USE_NSS_CERTS)
[email protected]8c434cbc2012-03-14 14:25:09206 net::SetURLRequestContextForNSSHttpIO(NULL);
[email protected]a592c0432012-12-01 18:10:29207#endif
mmenke02505da2017-05-15 16:37:34208
209#if defined(OS_ANDROID)
210 net::CertVerifyProcAndroid::ShutdownCertNetFetcher();
211#endif
[email protected]77feb462011-05-16 23:37:25212 }
mmenkef2e53b22017-05-15 18:40:18213
214 private:
215 DISALLOW_COPY_AND_ASSIGN(SystemURLRequestContext);
[email protected]77feb462011-05-16 23:37:25216};
217
dcheng4af48582016-04-19 00:29:35218std::unique_ptr<net::HostResolver> CreateGlobalHostResolver(
219 net::NetLog* net_log) {
[email protected]d22f06e2013-06-11 16:01:17220 TRACE_EVENT0("startup", "IOThread::CreateGlobalHostResolver");
avi556c05022014-12-22 23:31:43221 const base::CommandLine& command_line =
222 *base::CommandLine::ForCurrentProcess();
[email protected]962b98212010-07-17 03:37:51223
[email protected]c54a8912012-10-22 22:09:43224 net::HostResolver::Options options;
dcheng4af48582016-04-19 00:29:35225 std::unique_ptr<net::HostResolver> global_host_resolver;
[email protected]106ccd2c2014-06-17 09:21:00226#if defined OS_CHROMEOS
227 global_host_resolver =
228 chromeos::HostResolverImplChromeOS::CreateSystemResolver(options,
229 net_log);
230#else
231 global_host_resolver =
232 net::HostResolver::CreateSystemResolver(options, net_log);
233#endif
[email protected]9087aa32010-02-18 08:03:38234
[email protected]3dc5d7ef2014-03-28 19:18:21235 // If hostname remappings were specified on the command-line, layer these
236 // rules on top of the real host resolver. This allows forwarding all requests
237 // through a designated test server.
238 if (!command_line.HasSwitch(switches::kHostResolverRules))
dchenge73d8520c2015-12-27 01:19:09239 return global_host_resolver;
[email protected]0ac83682010-01-22 17:46:27240
dcheng4af48582016-04-19 00:29:35241 std::unique_ptr<net::MappedHostResolver> remapped_resolver(
dchenge73d8520c2015-12-27 01:19:09242 new net::MappedHostResolver(std::move(global_host_resolver)));
[email protected]3dc5d7ef2014-03-28 19:18:21243 remapped_resolver->SetRulesFromString(
244 command_line.GetSwitchValueASCII(switches::kHostResolverRules));
dchenge73d8520c2015-12-27 01:19:09245 return std::move(remapped_resolver);
[email protected]0ac83682010-01-22 17:46:27246}
247
jam1671ed32016-07-27 16:31:24248int GetSwitchValueAsInt(const base::CommandLine& command_line,
249 const std::string& switch_name) {
250 int value;
251 if (!base::StringToInt(command_line.GetSwitchValueASCII(switch_name),
252 &value)) {
253 return 0;
254 }
255 return value;
256}
257
robliao7253fd22016-12-01 18:41:38258// This function is for forwarding metrics usage pref changes to the metrics
259// service on the appropriate thread.
260// TODO(gayane): Reduce the frequency of posting tasks from IO to UI thread.
261void UpdateMetricsUsagePrefsOnUIThread(const std::string& service_name,
262 int message_size,
263 bool is_cellular) {
tzik3f7781d2017-04-20 17:09:33264 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
265 base::BindOnce(
266 [](const std::string& service_name,
267 int message_size, bool is_cellular) {
268 // Some unit tests use IOThread but do not
269 // initialize MetricsService. In that case it's
270 // fine to skip the update.
271 auto* metrics_service =
272 g_browser_process->metrics_service();
273 if (metrics_service) {
274 metrics_service->UpdateMetricsUsagePrefs(
275 service_name, message_size, is_cellular);
276 }
277 },
278 service_name, message_size, is_cellular));
robliao7253fd22016-12-01 18:41:38279}
280
[email protected]0ac83682010-01-22 17:46:27281} // namespace
282
[email protected]abe2c032011-03-31 18:49:34283class SystemURLRequestContextGetter : public net::URLRequestContextGetter {
[email protected]db0e86dd2011-03-16 14:47:21284 public:
285 explicit SystemURLRequestContextGetter(IOThread* io_thread);
[email protected]db0e86dd2011-03-16 14:47:21286
[email protected]abe2c032011-03-31 18:49:34287 // Implementation for net::UrlRequestContextGetter.
Daniel Chenga542fca2014-10-21 09:51:29288 net::URLRequestContext* GetURLRequestContext() override;
289 scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner()
290 const override;
[email protected]db0e86dd2011-03-16 14:47:21291
[email protected]13ed17f82012-04-06 02:27:18292 protected:
Daniel Chenga542fca2014-10-21 09:51:29293 ~SystemURLRequestContextGetter() override;
[email protected]13ed17f82012-04-06 02:27:18294
[email protected]db0e86dd2011-03-16 14:47:21295 private:
296 IOThread* const io_thread_; // Weak pointer, owned by BrowserProcess.
[email protected]4969b0122012-06-16 01:58:28297 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
[email protected]db0e86dd2011-03-16 14:47:21298
299 base::debug::LeakTracker<SystemURLRequestContextGetter> leak_tracker_;
300};
301
302SystemURLRequestContextGetter::SystemURLRequestContextGetter(
303 IOThread* io_thread)
304 : io_thread_(io_thread),
[email protected]4969b0122012-06-16 01:58:28305 network_task_runner_(
thestig529ad8a2016-07-08 20:30:12306 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO)) {}
[email protected]db0e86dd2011-03-16 14:47:21307
308SystemURLRequestContextGetter::~SystemURLRequestContextGetter() {}
309
310net::URLRequestContext* SystemURLRequestContextGetter::GetURLRequestContext() {
thestig00844cea2015-09-08 21:44:52311 DCHECK_CURRENTLY_ON(BrowserThread::IO);
[email protected]ef2bf422012-05-11 03:27:09312 DCHECK(io_thread_->globals()->system_request_context.get());
[email protected]db0e86dd2011-03-16 14:47:21313
[email protected]ef2bf422012-05-11 03:27:09314 return io_thread_->globals()->system_request_context.get();
[email protected]db0e86dd2011-03-16 14:47:21315}
316
[email protected]4969b0122012-06-16 01:58:28317scoped_refptr<base::SingleThreadTaskRunner>
318SystemURLRequestContextGetter::GetNetworkTaskRunner() const {
319 return network_task_runner_;
[email protected]db0e86dd2011-03-16 14:47:21320}
321
[email protected]c93123fa2012-04-19 02:49:48322IOThread::Globals::
323SystemRequestContextLeakChecker::SystemRequestContextLeakChecker(
324 Globals* globals)
325 : globals_(globals) {
326 DCHECK(globals_);
[email protected]7613faae2012-04-18 01:01:19327}
[email protected]1889dc1b2010-10-14 22:03:13328
[email protected]c93123fa2012-04-19 02:49:48329IOThread::Globals::
330SystemRequestContextLeakChecker::~SystemRequestContextLeakChecker() {
331 if (globals_->system_request_context.get())
332 globals_->system_request_context->AssertNoURLRequests();
333}
334
maksim.sisov3d40c812016-05-02 13:27:16335IOThread::Globals::Globals() : system_request_context_leak_checker(this),
336 enable_brotli(false) {}
[email protected]c2dad292012-09-07 21:27:35337
[email protected]c93123fa2012-04-19 02:49:48338IOThread::Globals::~Globals() {}
339
[email protected]bcefe0f2010-11-10 16:19:10340// |local_state| is passed in explicitly in order to (1) reduce implicit
341// dependencies and (2) make IOThread more flexible for testing.
[email protected]3ce02412011-03-01 12:01:15342IOThread::IOThread(
[email protected]b1de2c72013-02-06 02:45:47343 PrefService* local_state,
[email protected]77305422012-11-29 16:51:39344 policy::PolicyService* policy_service,
drogerc690e8802015-09-21 14:29:16345 net_log::ChromeNetLog* net_log,
[email protected]5a38dfd2012-07-23 23:22:10346 extensions::EventRouterForwarder* extension_event_router_forwarder)
[email protected]2e5b60a22011-11-28 15:56:41347 : net_log_(net_log),
brettw00899e62016-11-12 02:10:17348#if BUILDFLAG(ENABLE_EXTENSIONS)
[email protected]3ce02412011-03-01 12:01:15349 extension_event_router_forwarder_(extension_event_router_forwarder),
[email protected]84b7a552014-07-19 04:52:06350#endif
mmenkea7da6da2016-09-01 21:56:52351 globals_(nullptr),
peletskyi5df83d42015-04-30 16:37:46352 is_quic_allowed_by_policy_(true),
mmenke82d19a582017-01-27 23:26:32353 http_09_on_non_default_ports_enabled_(false),
mohan.reddy14cb4ad42014-09-17 18:15:14354 creation_time_(base::TimeTicks::Now()),
355 weak_factory_(this) {
aberentbba302d2015-12-03 10:20:19356 scoped_refptr<base::SingleThreadTaskRunner> io_thread_proxy =
thestig529ad8a2016-07-08 20:30:12357 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO);
[email protected]bcefe0f2010-11-10 16:19:10358 auth_schemes_ = local_state->GetString(prefs::kAuthSchemes);
aberentbba302d2015-12-03 10:20:19359 negotiate_disable_cname_lookup_.Init(
360 prefs::kDisableAuthNegotiateCnameLookup, local_state,
361 base::Bind(&IOThread::UpdateNegotiateDisableCnameLookup,
362 base::Unretained(this)));
363 negotiate_disable_cname_lookup_.MoveToThread(io_thread_proxy);
364 negotiate_enable_port_.Init(
365 prefs::kEnableAuthNegotiatePort, local_state,
366 base::Bind(&IOThread::UpdateNegotiateEnablePort, base::Unretained(this)));
367 negotiate_enable_port_.MoveToThread(io_thread_proxy);
368 auth_server_whitelist_.Init(
369 prefs::kAuthServerWhitelist, local_state,
370 base::Bind(&IOThread::UpdateServerWhitelist, base::Unretained(this)));
371 auth_server_whitelist_.MoveToThread(io_thread_proxy);
372 auth_delegate_whitelist_.Init(
373 prefs::kAuthNegotiateDelegateWhitelist, local_state,
374 base::Bind(&IOThread::UpdateDelegateWhitelist, base::Unretained(this)));
375 auth_delegate_whitelist_.MoveToThread(io_thread_proxy);
376#if defined(OS_ANDROID)
377 auth_android_negotiate_account_type_.Init(
378 prefs::kAuthAndroidNegotiateAccountType, local_state,
379 base::Bind(&IOThread::UpdateAndroidAuthNegotiateAccountType,
380 base::Unretained(this)));
381 auth_android_negotiate_account_type_.MoveToThread(io_thread_proxy);
382#endif
383#if defined(OS_POSIX) && !defined(OS_ANDROID)
[email protected]ac7f3fdb2010-11-12 12:47:05384 gssapi_library_name_ = local_state->GetString(prefs::kGSSAPILibraryName);
aberentbba302d2015-12-03 10:20:19385#endif
zentaroe0326352017-05-09 13:11:51386#if defined(OS_CHROMEOS)
387 policy::BrowserPolicyConnectorChromeOS* connector =
388 g_browser_process->platform_part()->browser_policy_connector_chromeos();
389 allow_gssapi_library_load_ = connector->IsActiveDirectoryManaged();
390#endif
[email protected]6f96cbcb2011-11-04 02:26:07391 pref_proxy_config_tracker_.reset(
[email protected]e2930d0902013-07-17 05:25:42392 ProxyServiceFactory::CreatePrefProxyConfigTrackerOfLocalState(
393 local_state));
mmenke02505da2017-05-15 16:37:34394 system_proxy_config_service_ = ProxyServiceFactory::CreateProxyConfigService(
395 pref_proxy_config_tracker_.get());
[email protected]9d8cfb682012-09-13 16:48:04396 ChromeNetworkDelegate::InitializePrefsOnUIThread(
397 &system_enable_referrers_,
igorcov193d8ad32016-05-27 11:03:21398 nullptr,
399 nullptr,
400 nullptr,
401 nullptr,
[email protected]9d8cfb682012-09-13 16:48:04402 local_state);
[email protected]4d45a6de2011-05-13 05:20:18403 ssl_config_service_manager_.reset(
abhishek.a212849cee2015-10-20 11:27:29404 ssl_config::SSLConfigServiceManager::CreateDefaultManager(
405 local_state,
thestig529ad8a2016-07-08 20:30:12406 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO)));
[email protected]2e5b60a22011-11-28 15:56:41407
jdoerrie239723572017-03-02 12:09:19408 base::Value* dns_client_enabled_default =
409 new base::Value(chrome_browser_net::ConfigureAsyncDnsFieldTrial());
[email protected]68a9b0d82013-03-08 07:05:07410 local_state->SetDefaultPrefValue(prefs::kBuiltInDnsClientEnabled,
411 dns_client_enabled_default);
ttuttle530184962015-01-13 00:24:07412 chrome_browser_net::LogAsyncDnsPrefSource(
413 local_state->FindPreference(prefs::kBuiltInDnsClientEnabled));
[email protected]68a9b0d82013-03-08 07:05:07414
[email protected]fa4b6c32012-11-26 23:02:39415 dns_client_enabled_.Init(prefs::kBuiltInDnsClientEnabled,
416 local_state,
417 base::Bind(&IOThread::UpdateDnsClientEnabled,
418 base::Unretained(this)));
aberentbba302d2015-12-03 10:20:19419 dns_client_enabled_.MoveToThread(io_thread_proxy);
[email protected]fa4b6c32012-11-26 23:02:39420
[email protected]67378142013-12-17 21:57:17421 quick_check_enabled_.Init(prefs::kQuickCheckEnabled,
422 local_state);
aberentbba302d2015-12-03 10:20:19423 quick_check_enabled_.MoveToThread(io_thread_proxy);
[email protected]67378142013-12-17 21:57:17424
eroman9f7ea642016-06-03 21:28:29425 pac_https_url_stripping_enabled_.Init(prefs::kPacHttpsUrlStrippingEnabled,
426 local_state);
427 pac_https_url_stripping_enabled_.MoveToThread(io_thread_proxy);
428
peletskyi5df83d42015-04-30 16:37:46429 const base::Value* value = policy_service->GetPolicies(
430 policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME,
431 std::string())).GetValue(policy::key::kQuicAllowed);
432 if (value)
433 value->GetAsBoolean(&is_quic_allowed_by_policy_);
[email protected]77305422012-11-29 16:51:39434
mmenkea7da6da2016-09-01 21:56:52435 value = policy_service
436 ->GetPolicies(policy::PolicyNamespace(
437 policy::POLICY_DOMAIN_CHROME, std::string()))
438 .GetValue(policy::key::kHttp09OnNonDefaultPortsEnabled);
439 if (value)
440 value->GetAsBoolean(&http_09_on_non_default_ports_enabled_);
441
eranm3c2d6432016-06-01 10:17:27442 chrome_browser_net::SetGlobalSTHDistributor(
443 std::unique_ptr<net::ct::STHDistributor>(new net::ct::STHDistributor()));
444
gab90becc92016-12-08 17:19:34445 BrowserThread::SetIOThreadDelegate(this);
[email protected]bcefe0f2010-11-10 16:19:10446}
[email protected]0ac83682010-01-22 17:46:27447
448IOThread::~IOThread() {
[email protected]2e5b60a22011-11-28 15:56:41449 // This isn't needed for production code, but in tests, IOThread may
450 // be multiply constructed.
gab90becc92016-12-08 17:19:34451 BrowserThread::SetIOThreadDelegate(nullptr);
[email protected]2e5b60a22011-11-28 15:56:41452
[email protected]d461ed22013-01-18 03:18:56453 pref_proxy_config_tracker_->DetachFromPrefService();
[email protected]d13c3272010-02-04 00:24:51454 DCHECK(!globals_);
eranm3c2d6432016-06-01 10:17:27455
456 // Destroy the old distributor to check that the observers list it holds is
457 // empty.
458 chrome_browser_net::SetGlobalSTHDistributor(nullptr);
[email protected]0ac83682010-01-22 17:46:27459}
460
[email protected]d13c3272010-02-04 00:24:51461IOThread::Globals* IOThread::globals() {
thestig00844cea2015-09-08 21:44:52462 DCHECK_CURRENTLY_ON(BrowserThread::IO);
[email protected]d13c3272010-02-04 00:24:51463 return globals_;
[email protected]0ac83682010-01-22 17:46:27464}
465
[email protected]37ac95b2013-07-23 23:39:35466void IOThread::SetGlobalsForTesting(Globals* globals) {
thestig00844cea2015-09-08 21:44:52467 DCHECK_CURRENTLY_ON(BrowserThread::IO);
[email protected]37ac95b2013-07-23 23:39:35468 DCHECK(!globals || !globals_);
469 globals_ = globals;
470}
471
drogerc690e8802015-09-21 14:29:16472net_log::ChromeNetLog* IOThread::net_log() {
[email protected]b2fcd0e2010-12-01 15:19:40473 return net_log_;
474}
475
[email protected]b09f76d62011-12-07 01:51:06476void IOThread::ChangedToOnTheRecord() {
thestig00844cea2015-09-08 21:44:52477 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]b09f76d62011-12-07 01:51:06478 BrowserThread::PostTask(
tzik3f7781d2017-04-20 17:09:33479 BrowserThread::IO, FROM_HERE,
480 base::BindOnce(&IOThread::ChangedToOnTheRecordOnIOThread,
481 base::Unretained(this)));
[email protected]b09f76d62011-12-07 01:51:06482}
483
[email protected]abe2c032011-03-31 18:49:34484net::URLRequestContextGetter* IOThread::system_url_request_context_getter() {
thestig00844cea2015-09-08 21:44:52485 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]5173de8b2013-06-02 21:16:02486 if (!system_url_request_context_getter_.get()) {
mmenke02505da2017-05-15 16:37:34487 system_url_request_context_getter_ =
488 new SystemURLRequestContextGetter(this);
[email protected]db0e86dd2011-03-16 14:47:21489 }
[email protected]5173de8b2013-06-02 21:16:02490 return system_url_request_context_getter_.get();
[email protected]db0e86dd2011-03-16 14:47:21491}
492
[email protected]0ac83682010-01-22 17:46:27493void IOThread::Init() {
[email protected]a5e73b82013-07-17 08:58:39494 TRACE_EVENT0("startup", "IOThread::InitAsync");
thestig00844cea2015-09-08 21:44:52495 DCHECK_CURRENTLY_ON(BrowserThread::IO);
[email protected]70b92342010-10-12 05:54:06496
jam1c5a91492016-02-24 20:47:53497#if defined(USE_NSS_CERTS)
[email protected]8c434cbc2012-03-14 14:25:09498 net::SetMessageLoopForNSSHttpIO();
[email protected]a592c0432012-12-01 18:10:29499#endif
[email protected]70b92342010-10-12 05:54:06500
avi556c05022014-12-22 23:31:43501 const base::CommandLine& command_line =
502 *base::CommandLine::ForCurrentProcess();
[email protected]c2dad292012-09-07 21:27:35503
zhongyi81f85c6d92015-10-16 19:34:14504 // Export ssl keys if log file specified.
davidben2a811e4e2015-12-01 10:49:34505 base::FilePath ssl_keylog_file = GetSSLKeyLogFile(command_line);
zhongyi81f85c6d92015-10-16 19:34:14506 if (!ssl_keylog_file.empty()) {
davidben2a811e4e2015-12-01 10:49:34507 net::SSLClientSocket::SetSSLKeyLogFile(
508 ssl_keylog_file,
thestig529ad8a2016-07-08 20:30:12509 BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE));
zhongyi81f85c6d92015-10-16 19:34:14510 }
511
[email protected]d13c3272010-02-04 00:24:51512 DCHECK(!globals_);
513 globals_ = new Globals;
514
[email protected]58bc7042010-07-07 18:04:14515 // Add an observer that will emit network change events to the ChromeNetLog.
516 // Assuming NetworkChangeNotifier dispatches in FIFO order, we should be
517 // logging the network change before other IO thread consumers respond to it.
518 network_change_observer_.reset(
pauljensen7b34e522016-05-12 18:20:59519 new net::LoggingNetworkChangeObserver(net_log_));
[email protected]58bc7042010-07-07 18:04:14520
[email protected]cde8b3c2012-08-13 19:20:52521 // Setup the HistogramWatcher to run on the IO thread.
522 net::NetworkChangeNotifier::InitHistogramWatcher();
523
brettw00899e62016-11-12 02:10:17524#if BUILDFLAG(ENABLE_EXTENSIONS)
[email protected]3ce02412011-03-01 12:01:15525 globals_->extension_event_router_forwarder =
526 extension_event_router_forwarder_;
[email protected]84b7a552014-07-19 04:52:06527#endif
528
dcheng4af48582016-04-19 00:29:35529 std::unique_ptr<data_usage::DataUseAmortizer> data_use_amortizer;
zpengdb4a58e2017-01-10 17:40:32530#if defined(OS_ANDROID)
sclittlec441f782015-11-12 01:12:09531 data_use_amortizer.reset(new data_usage::android::TrafficStatsAmortizer());
zpengdb4a58e2017-01-10 17:40:32532#endif // defined(OS_ANDROID)
sclittlec441f782015-11-12 01:12:09533
kundajif617523b2016-09-09 22:41:24534 globals_->data_use_ascriber =
535 base::MakeUnique<data_use_measurement::ChromeDataUseAscriber>();
536
sclittle3f98c6c2015-11-19 22:59:29537 globals_->data_use_aggregator.reset(new data_usage::DataUseAggregator(
dcheng4af48582016-04-19 00:29:35538 std::unique_ptr<data_usage::DataUseAnnotator>(
sclittlec441f782015-11-12 01:12:09539 new chrome_browser_data_usage::TabIdAnnotator()),
dchenge73d8520c2015-12-27 01:19:09540 std::move(data_use_amortizer)));
sclittleae932be2015-10-08 20:53:50541
zpengdb4a58e2017-01-10 17:40:32542#if defined(OS_ANDROID)
sclittle3f98c6c2015-11-19 22:59:29543 globals_->external_data_use_observer.reset(
tbansal7c98c0a2015-11-03 21:39:51544 new chrome::android::ExternalDataUseObserver(
sclittle3f98c6c2015-11-19 22:59:29545 globals_->data_use_aggregator.get(),
thestig529ad8a2016-07-08 20:30:12546 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO),
547 BrowserThread::GetTaskRunnerForThread(BrowserThread::UI)));
zpengdb4a58e2017-01-10 17:40:32548#endif // defined(OS_ANDROID)
[email protected]84b7a552014-07-19 04:52:06549
[email protected]c54a8912012-10-22 22:09:43550 globals_->host_resolver = CreateGlobalHostResolver(net_log_);
tbansalea2fb8c2015-05-22 22:23:00551
tbansalb177b5392015-06-25 11:13:02552 std::map<std::string, std::string> network_quality_estimator_params;
553 variations::GetVariationParams(kNetworkQualityEstimatorFieldTrialName,
554 &network_quality_estimator_params);
tbansal1c92d5b2015-08-14 20:14:43555
tbansald3e08ca62017-05-04 19:51:09556 if (command_line.HasSwitch(switches::kForceEffectiveConnectionType)) {
557 const std::string force_ect_value =
558 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
559 switches::kForceEffectiveConnectionType);
560
561 if (!force_ect_value.empty()) {
562 // If the effective connection type is forced using command line switch,
563 // it overrides the one set by field trial.
564 network_quality_estimator_params[net::kForceEffectiveConnectionType] =
565 force_ect_value;
566 }
567 }
568
dcheng4af48582016-04-19 00:29:35569 std::unique_ptr<net::ExternalEstimateProvider> external_estimate_provider;
zpengdb4a58e2017-01-10 17:40:32570#if defined(OS_ANDROID)
tbansal1c92d5b2015-08-14 20:14:43571 external_estimate_provider.reset(
tbansaldafbb3e2015-08-19 19:55:33572 new chrome::android::ExternalEstimateProviderAndroid());
zpengdb4a58e2017-01-10 17:40:32573#endif // defined(OS_ANDROID)
tbansal1c92d5b2015-08-14 20:14:43574 // Pass ownership.
575 globals_->network_quality_estimator.reset(new net::NetworkQualityEstimator(
tbansal97e38a22017-01-20 20:43:02576 std::move(external_estimate_provider), network_quality_estimator_params,
577 net_log_));
tbansal15973c32017-05-10 18:40:44578 globals_->network_quality_observer = content::CreateNetworkQualityObserver(
579 globals_->network_quality_estimator.get());
tbansalea2fb8c2015-05-22 22:23:00580
[email protected]fa4b6c32012-11-26 23:02:39581 UpdateDnsClientEnabled();
[email protected]f46f6d52014-02-08 04:00:39582#if defined(OS_CHROMEOS)
[email protected]f9357a442014-05-15 18:44:07583 // Creates a CertVerifyProc that doesn't allow any profile-provided certs.
rsleevi6df54182016-06-13 14:34:23584 globals_->cert_verifier = base::MakeUnique<net::CachingCertVerifier>(
585 base::MakeUnique<net::MultiThreadedCertVerifier>(
586 new chromeos::CertVerifyProcChromeOS()));
[email protected]f9357a442014-05-15 18:44:07587#else
martinkr8402d3c2017-05-23 20:10:47588 globals_->cert_verifier = IgnoreErrorsCertVerifier::MaybeWrapCertVerifier(
589 command_line, net::CertVerifier::CreateDefault());
590 UMA_HISTOGRAM_BOOLEAN(
591 "Net.Certificate.IgnoreCertificateErrorsSPKIListPresent",
592 command_line.HasSwitch(switches::kIgnoreCertificateErrorsSPKIList));
[email protected]f9357a442014-05-15 18:44:07593#endif
594
eranm1a79db22015-11-24 10:56:49595 std::vector<scoped_refptr<const net::CTLogVerifier>> ct_logs(
eranm03d454c2015-07-02 09:12:03596 net::ct::CreateLogVerifiersForKnownLogs());
[email protected]284303b62013-11-28 15:11:54597
eranm1a79db22015-11-24 10:56:49598 globals_->ct_logs.assign(ct_logs.begin(), ct_logs.end());
599
eranm03d454c2015-07-02 09:12:03600 net::MultiLogCTVerifier* ct_verifier = new net::MultiLogCTVerifier();
601 globals_->cert_transparency_verifier.reset(ct_verifier);
602 // Add built-in logs
eranm1a79db22015-11-24 10:56:49603 ct_verifier->AddLogs(globals_->ct_logs);
eranm03d454c2015-07-02 09:12:03604
eranmbbf5af702017-02-02 16:06:18605 ct_tree_tracker_.reset(new certificate_transparency::TreeStateTracker(
606 globals_->ct_logs, net_log_));
eranm3c2d6432016-06-01 10:17:27607 // Register the ct_tree_tracker_ as observer for new STHs.
608 RegisterSTHObserver(ct_tree_tracker_.get());
609 // Register the ct_tree_tracker_ as observer for verified SCTs.
610 globals_->cert_transparency_verifier->SetObserver(ct_tree_tracker_.get());
611
aberentbba302d2015-12-03 10:20:19612 CreateDefaultAuthHandlerFactory();
[email protected]4588b3d2012-11-14 00:37:38613 globals_->dns_probe_service.reset(new chrome_browser_net::DnsProbeService());
[email protected]c2dad292012-09-07 21:27:35614 globals_->host_mapping_rules.reset(new net::HostMappingRules());
615 if (command_line.HasSwitch(switches::kHostRules)) {
[email protected]a5e73b82013-07-17 08:58:39616 TRACE_EVENT_BEGIN0("startup", "IOThread::InitAsync:SetRulesFromString");
[email protected]c2dad292012-09-07 21:27:35617 globals_->host_mapping_rules->SetRulesFromString(
618 command_line.GetSwitchValueASCII(switches::kHostRules));
[email protected]a5e73b82013-07-17 08:58:39619 TRACE_EVENT_END0("startup", "IOThread::InitAsync:SetRulesFromString");
[email protected]c2dad292012-09-07 21:27:35620 }
mmenke6ddfbea2017-05-31 21:48:41621
622 session_params_.host_mapping_rules = *globals_->host_mapping_rules.get();
maksim.sisov3d40c812016-05-02 13:27:16623 globals_->enable_brotli =
bnc18fb77f2016-04-04 20:54:04624 base::FeatureList::IsEnabled(features::kBrotliEncoding);
mmenke6ddfbea2017-05-31 21:48:41625 session_params_.enable_token_binding =
nharperef2618642016-02-03 22:23:44626 base::FeatureList::IsEnabled(features::kTokenBinding);
bnce87c7c712016-08-01 23:59:48627
628 // Check for OS support of TCP FastOpen, and turn it on for all connections if
629 // indicated by user.
bnc18fb77f2016-04-04 20:54:04630 // TODO(rch): Make the client socket factory a per-network session instance,
631 // constructed from a NetworkSession::Params, to allow us to move this option
632 // to IOThread::Globals & HttpNetworkSession::Params.
bnc18fb77f2016-04-04 20:54:04633 bool always_enable_tfo_if_supported =
634 command_line.HasSwitch(switches::kEnableTcpFastOpen);
bnc18fb77f2016-04-04 20:54:04635 net::CheckSupportAndMaybeEnableTCPFastOpen(always_enable_tfo_if_supported);
[email protected]f9cf5572012-12-04 15:52:09636
bnce87c7c712016-08-01 23:59:48637 ConfigureParamsFromFieldTrialsAndCommandLine(
mmenkea7da6da2016-09-01 21:56:52638 command_line, is_quic_allowed_by_policy_,
mmenke6ddfbea2017-05-31 21:48:41639 http_09_on_non_default_ports_enabled_, &session_params_);
bnce87c7c712016-08-01 23:59:48640
jam1c5a91492016-02-24 20:47:53641#if defined(OS_MACOSX)
[email protected]11f5e3a2012-09-27 00:30:13642 // Start observing Keychain events. This needs to be done on the UI thread,
643 // as Keychain services requires a CFRunLoop.
644 BrowserThread::PostTask(BrowserThread::UI,
645 FROM_HERE,
646 base::Bind(&ObserveKeychainEvents));
647#endif
648
davidben45eb19952016-05-05 16:30:44649#if defined(OS_ANDROID) && defined(ARCH_CPU_ARMEL)
650 // Record how common CPUs with broken NEON units are. See
651 // https://crbug.com/341598.
652 crypto::EnsureOpenSSLInit();
653 UMA_HISTOGRAM_BOOLEAN("Net.HasBrokenNEON", CRYPTO_has_broken_NEON());
654#endif
mmenke02505da2017-05-15 16:37:34655
mmenkef2e53b22017-05-15 18:40:18656 ConstructSystemRequestContext();
[email protected]0ac83682010-01-22 17:46:27657}
658
[email protected]2a92cd92010-04-27 00:01:41659void IOThread::CleanUp() {
[email protected]075c0322012-02-14 00:56:44660 base::debug::LeakTracker<SafeBrowsingURLRequestContext>::CheckForLeaks();
661
jam1c5a91492016-02-24 20:47:53662#if defined(USE_NSS_CERTS)
[email protected]8c434cbc2012-03-14 14:25:09663 net::ShutdownNSSHttpIO();
[email protected]a592c0432012-12-01 18:10:29664#endif
[email protected]59a3b362010-10-21 21:52:41665
[email protected]db0e86dd2011-03-16 14:47:21666 system_url_request_context_getter_ = NULL;
667
eranm3c2d6432016-06-01 10:17:27668 // Unlink the ct_tree_tracker_ from the global cert_transparency_verifier
669 // and unregister it from new STH notifications so it will take no actions
670 // on anything observed during CleanUp process.
mmenke02505da2017-05-15 16:37:34671 //
672 // Null checks are just for tests that use TestingIOThreadState.
673 if (globals()->cert_transparency_verifier)
674 globals()->cert_transparency_verifier->SetObserver(nullptr);
675 if (ct_tree_tracker_.get()) {
676 UnregisterSTHObserver(ct_tree_tracker_.get());
677 ct_tree_tracker_.reset();
678 }
eranm3c2d6432016-06-01 10:17:27679
mmenkeab0c11d2017-05-30 17:15:17680 if (globals_->system_request_context)
681 globals_->system_request_context->proxy_service()->OnShutdown();
682
[email protected]af669932012-01-17 19:26:58683 // Release objects that the net::URLRequestContext could have been pointing
684 // to.
[email protected]0ee7a3b2010-11-09 06:13:40685
[email protected]7592b41f2014-04-01 01:44:43686 // Shutdown the HistogramWatcher on the IO thread.
687 net::NetworkChangeNotifier::ShutdownHistogramWatcher();
688
[email protected]0ee7a3b2010-11-09 06:13:40689 // This must be reset before the ChromeNetLog is destroyed.
690 network_change_observer_.reset();
691
[email protected]db0e86dd2011-03-16 14:47:21692 system_proxy_config_service_.reset();
[email protected]d13c3272010-02-04 00:24:51693 delete globals_;
694 globals_ = NULL;
[email protected]0ac83682010-01-22 17:46:27695
[email protected]db0e86dd2011-03-16 14:47:21696 base::debug::LeakTracker<SystemURLRequestContextGetter>::CheckForLeaks();
[email protected]0ac83682010-01-22 17:46:27697}
698
bnc18fb77f2016-04-04 20:54:04699// static
[email protected]b1de2c72013-02-06 02:45:47700void IOThread::RegisterPrefs(PrefRegistrySimple* registry) {
701 registry->RegisterStringPref(prefs::kAuthSchemes,
bengr70266e22015-06-12 19:38:17702 "basic,digest,ntlm,negotiate");
[email protected]68a9b0d82013-03-08 07:05:07703 registry->RegisterBooleanPref(prefs::kDisableAuthNegotiateCnameLookup, false);
[email protected]b1de2c72013-02-06 02:45:47704 registry->RegisterBooleanPref(prefs::kEnableAuthNegotiatePort, false);
[email protected]007b3f82013-04-09 08:46:45705 registry->RegisterStringPref(prefs::kAuthServerWhitelist, std::string());
706 registry->RegisterStringPref(prefs::kAuthNegotiateDelegateWhitelist,
707 std::string());
708 registry->RegisterStringPref(prefs::kGSSAPILibraryName, std::string());
aberentec894a52015-07-09 14:45:53709 registry->RegisterStringPref(prefs::kAuthAndroidNegotiateAccountType,
710 std::string());
[email protected]b1de2c72013-02-06 02:45:47711 registry->RegisterBooleanPref(prefs::kEnableReferrers, true);
[email protected]1575e3d2014-05-03 22:21:44712 data_reduction_proxy::RegisterPrefs(registry);
[email protected]68a9b0d82013-03-08 07:05:07713 registry->RegisterBooleanPref(prefs::kBuiltInDnsClientEnabled, true);
[email protected]67378142013-12-17 21:57:17714 registry->RegisterBooleanPref(prefs::kQuickCheckEnabled, true);
eroman9f7ea642016-06-03 21:28:29715 registry->RegisterBooleanPref(prefs::kPacHttpsUrlStrippingEnabled, true);
[email protected]bcefe0f2010-11-10 16:19:10716}
717
aberentbba302d2015-12-03 10:20:19718void IOThread::UpdateServerWhitelist() {
719 globals_->http_auth_preferences->set_server_whitelist(
720 auth_server_whitelist_.GetValue());
721}
[email protected]b7304162010-08-23 17:42:29722
aberentbba302d2015-12-03 10:20:19723void IOThread::UpdateDelegateWhitelist() {
724 globals_->http_auth_preferences->set_delegate_whitelist(
725 auth_delegate_whitelist_.GetValue());
726}
727
728#if defined(OS_ANDROID)
729void IOThread::UpdateAndroidAuthNegotiateAccountType() {
730 globals_->http_auth_preferences->set_auth_android_negotiate_account_type(
731 auth_android_negotiate_account_type_.GetValue());
732}
733#endif
734
735void IOThread::UpdateNegotiateDisableCnameLookup() {
736 globals_->http_auth_preferences->set_negotiate_disable_cname_lookup(
737 negotiate_disable_cname_lookup_.GetValue());
738}
739
740void IOThread::UpdateNegotiateEnablePort() {
741 globals_->http_auth_preferences->set_negotiate_enable_port(
742 negotiate_enable_port_.GetValue());
743}
744
745void IOThread::CreateDefaultAuthHandlerFactory() {
746 std::vector<std::string> supported_schemes = base::SplitString(
747 auth_schemes_, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
748 globals_->http_auth_preferences.reset(new net::HttpAuthPreferences(
749 supported_schemes
750#if defined(OS_POSIX) && !defined(OS_ANDROID)
751 ,
752 gssapi_library_name_
753#endif
zentaroe0326352017-05-09 13:11:51754#if defined(OS_CHROMEOS)
755 ,
756 allow_gssapi_library_load_
757#endif
aberentbba302d2015-12-03 10:20:19758 ));
759 UpdateServerWhitelist();
760 UpdateDelegateWhitelist();
761 UpdateNegotiateDisableCnameLookup();
762 UpdateNegotiateEnablePort();
763#if defined(OS_ANDROID)
764 UpdateAndroidAuthNegotiateAccountType();
765#endif
766 globals_->http_auth_handler_factory =
[email protected]ec44ee02012-09-28 21:31:51767 net::HttpAuthHandlerRegistryFactory::Create(
dchenge73d8520c2015-12-27 01:19:09768 globals_->http_auth_preferences.get(), globals_->host_resolver.get());
[email protected]eb3cac72010-02-26 21:07:45769}
770
msramek94f5656c2016-08-31 17:08:29771void IOThread::ClearHostCache(
772 const base::Callback<bool(const std::string&)>& host_filter) {
thestig00844cea2015-09-08 21:44:52773 DCHECK_CURRENTLY_ON(BrowserThread::IO);
[email protected]d6f37fc2011-02-13 23:58:41774
[email protected]489d1a82011-10-12 03:09:11775 net::HostCache* host_cache = globals_->host_resolver->GetHostCache();
776 if (host_cache)
msramek94f5656c2016-08-31 17:08:29777 host_cache->ClearForHosts(host_filter);
[email protected]0ac83682010-01-22 17:46:27778}
[email protected]db0e86dd2011-03-16 14:47:21779
bnc18fb77f2016-04-04 20:54:04780const net::HttpNetworkSession::Params& IOThread::NetworkSessionParams() const {
mmenke6ddfbea2017-05-31 21:48:41781 return session_params_;
[email protected]f9cf5572012-12-04 15:52:09782}
783
pmarko6ab8be242017-01-11 11:02:55784void IOThread::DisableQuic() {
mmenke6ddfbea2017-05-31 21:48:41785 session_params_.enable_quic = false;
pmarko6ab8be242017-01-11 11:02:55786
mmenkef2e53b22017-05-15 18:40:18787 if (globals_->system_request_context_storage)
788 globals_->system_request_context_storage->http_network_session()
789 ->DisableQuic();
pmarko6ab8be242017-01-11 11:02:55790}
791
[email protected]d827e112014-03-31 17:45:05792base::TimeTicks IOThread::creation_time() const {
793 return creation_time_;
794}
795
[email protected]4d45a6de2011-05-13 05:20:18796net::SSLConfigService* IOThread::GetSSLConfigService() {
797 return ssl_config_service_manager_->Get();
798}
799
[email protected]b09f76d62011-12-07 01:51:06800void IOThread::ChangedToOnTheRecordOnIOThread() {
thestig00844cea2015-09-08 21:44:52801 DCHECK_CURRENTLY_ON(BrowserThread::IO);
[email protected]b09f76d62011-12-07 01:51:06802
803 // Clear the host cache to avoid showing entries from the OTR session
804 // in about:net-internals.
msramek94f5656c2016-08-31 17:08:29805 ClearHostCache(base::Callback<bool(const std::string&)>());
[email protected]b09f76d62011-12-07 01:51:06806}
807
[email protected]fa4b6c32012-11-26 23:02:39808void IOThread::UpdateDnsClientEnabled() {
809 globals()->host_resolver->SetDnsClientEnabled(*dns_client_enabled_);
810}
[email protected]903e63382013-06-01 00:40:58811
eranm3c2d6432016-06-01 10:17:27812void IOThread::RegisterSTHObserver(net::ct::STHObserver* observer) {
813 chrome_browser_net::GetGlobalSTHDistributor()->RegisterObserver(observer);
814}
815
816void IOThread::UnregisterSTHObserver(net::ct::STHObserver* observer) {
817 chrome_browser_net::GetGlobalSTHDistributor()->UnregisterObserver(observer);
818}
819
eroman9f7ea642016-06-03 21:28:29820bool IOThread::WpadQuickCheckEnabled() const {
821 return quick_check_enabled_.GetValue();
822}
823
824bool IOThread::PacHttpsUrlStrippingEnabled() const {
825 return pac_https_url_stripping_enabled_.GetValue();
826}
827
mmenkef2e53b22017-05-15 18:40:18828void IOThread::ConstructSystemRequestContext() {
829 globals_->system_request_context =
830 base::MakeUnique<SystemURLRequestContext>();
831 net::URLRequestContext* context = globals_->system_request_context.get();
832 globals_->system_request_context_storage =
833 base::MakeUnique<net::URLRequestContextStorage>(context);
834 net::URLRequestContextStorage* context_storage =
835 globals_->system_request_context_storage.get();
mmenke518c27b2017-05-04 23:15:21836
837 context->set_network_quality_estimator(
mmenkef2e53b22017-05-15 18:40:18838 globals_->network_quality_estimator.get());
839 context->set_enable_brotli(globals_->enable_brotli);
mmenke518c27b2017-05-04 23:15:21840 context->set_name("system");
841
mmenke15992ce2017-05-30 21:05:02842 context_storage->set_http_user_agent_settings(
843 base::MakeUnique<net::StaticHttpUserAgentSettings>(std::string(),
844 GetUserAgent()));
845 std::unique_ptr<ChromeNetworkDelegate> chrome_network_delegate(
846 new ChromeNetworkDelegate(extension_event_router_forwarder(),
847 &system_enable_referrers_));
848 // By default, data usage is considered off the record.
849 chrome_network_delegate->set_data_use_aggregator(
850 globals_->data_use_aggregator.get(),
851 true /* is_data_usage_off_the_record */);
852 context_storage->set_network_delegate(
853 globals_->data_use_ascriber->CreateNetworkDelegate(
854 std::move(chrome_network_delegate), GetMetricsDataUseForwarder()));
mmenkef2e53b22017-05-15 18:40:18855 context->set_net_log(net_log_);
856 context->set_host_resolver(globals_->host_resolver.get());
857
mmenke15992ce2017-05-30 21:05:02858 context_storage->set_ssl_config_service(GetSSLConfigService());
wjmacleanea309f72015-08-25 20:56:59859 context->set_http_auth_handler_factory(
mmenkef2e53b22017-05-15 18:40:18860 globals_->http_auth_handler_factory.get());
wjmacleanea309f72015-08-25 20:56:59861
mmenkef2e53b22017-05-15 18:40:18862 // In-memory cookie store.
863 context_storage->set_cookie_store(
864 content::CreateCookieStore(content::CookieStoreConfig()));
865 // In-memory channel ID store.
866 context_storage->set_channel_id_service(
867 base::MakeUnique<net::ChannelIDService>(
868 new net::DefaultChannelIDStore(nullptr)));
869 context->cookie_store()->SetChannelIDServiceID(
870 context->channel_id_service()->GetUniqueID());
871
mmenke15992ce2017-05-30 21:05:02872 context_storage->set_transport_security_state(
873 base::MakeUnique<net::TransportSecurityState>());
wjmacleanea309f72015-08-25 20:56:59874
mmenke15992ce2017-05-30 21:05:02875 context_storage->set_http_server_properties(
876 base::MakeUnique<net::HttpServerPropertiesImpl>());
wjmacleanea309f72015-08-25 20:56:59877
mmenkef2e53b22017-05-15 18:40:18878 context->set_cert_verifier(globals_->cert_verifier.get());
mmenke518c27b2017-05-04 23:15:21879 context->set_cert_transparency_verifier(
mmenkef2e53b22017-05-15 18:40:18880 globals_->cert_transparency_verifier.get());
mmenke6ddfbea2017-05-31 21:48:41881
mmenke15992ce2017-05-30 21:05:02882 context_storage->set_ct_policy_enforcer(
883 base::MakeUnique<net::CTPolicyEnforcer>());
maksim.sisov3d40c812016-05-02 13:27:16884
mmenkef2e53b22017-05-15 18:40:18885 const base::CommandLine& command_line =
886 *base::CommandLine::ForCurrentProcess();
887 context_storage->set_proxy_service(ProxyServiceFactory::CreateProxyService(
mmenke15992ce2017-05-30 21:05:02888 net_log_, context, context->network_delegate(),
mmenkef2e53b22017-05-15 18:40:18889 std::move(system_proxy_config_service_), command_line,
890 WpadQuickCheckEnabled(), PacHttpsUrlStrippingEnabled()));
891
mmenke6ddfbea2017-05-31 21:48:41892 net::HttpNetworkSession::Context session_context;
wjmacleanea309f72015-08-25 20:56:59893 net::URLRequestContextBuilder::SetHttpNetworkSessionComponents(
mmenke6ddfbea2017-05-31 21:48:41894 context, &session_context);
wjmacleanea309f72015-08-25 20:56:59895
mmenkef2e53b22017-05-15 18:40:18896 context_storage->set_http_network_session(
mmenke6ddfbea2017-05-31 21:48:41897 base::MakeUnique<net::HttpNetworkSession>(session_params_,
898 session_context));
mmenkef2e53b22017-05-15 18:40:18899 context_storage->set_http_transaction_factory(
900 base::MakeUnique<net::HttpNetworkLayer>(
901 context_storage->http_network_session()));
mmenke518c27b2017-05-04 23:15:21902
mmenkeab0c11d2017-05-30 17:15:17903 std::unique_ptr<net::URLRequestJobFactoryImpl> job_factory(
904 new net::URLRequestJobFactoryImpl());
905
906 job_factory->SetProtocolHandler(url::kDataScheme,
907 base::MakeUnique<net::DataProtocolHandler>());
908 job_factory->SetProtocolHandler(
909 url::kFileScheme,
910 base::MakeUnique<net::FileProtocolHandler>(
911 base::CreateTaskRunnerWithTraits(
912 {base::MayBlock(), base::TaskPriority::USER_VISIBLE,
913 base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN})));
914#if !BUILDFLAG(DISABLE_FTP_SUPPORT)
915 job_factory->SetProtocolHandler(
916 url::kFtpScheme,
917 net::FtpProtocolHandler::Create(globals_->host_resolver.get()));
918#endif
919
920 context_storage->set_job_factory(std::move(job_factory));
wjmacleanea309f72015-08-25 20:56:59921}
922
bnc0f6bff62016-04-05 16:51:09923// static
bnce87c7c712016-08-01 23:59:48924void IOThread::ConfigureParamsFromFieldTrialsAndCommandLine(
925 const base::CommandLine& command_line,
926 bool is_quic_allowed_by_policy,
mmenkea7da6da2016-09-01 21:56:52927 bool http_09_on_non_default_ports_enabled,
bnce87c7c712016-08-01 23:59:48928 net::HttpNetworkSession::Params* params) {
929 std::string quic_user_agent_id = chrome::GetChannelString();
930 if (!quic_user_agent_id.empty())
931 quic_user_agent_id.push_back(' ');
932 quic_user_agent_id.append(
933 version_info::GetProductNameAndVersionForUserAgent());
934 quic_user_agent_id.push_back(' ');
935 quic_user_agent_id.append(content::BuildOSCpuInfo());
936
937 bool is_quic_force_disabled = !is_quic_allowed_by_policy ||
938 command_line.HasSwitch(switches::kDisableQuic);
939 bool is_quic_force_enabled = command_line.HasSwitch(switches::kEnableQuic);
940
941 network_session_configurator::ParseFieldTrials(is_quic_force_disabled,
942 is_quic_force_enabled,
943 quic_user_agent_id, params);
944
945 // Command line flags override field trials.
946 if (command_line.HasSwitch(switches::kIgnoreUrlFetcherCertRequests))
947 net::URLFetcher::SetIgnoreCertificateRequests(true);
948
949 if (command_line.HasSwitch(switches::kDisableHttp2))
950 params->enable_http2 = false;
951
bnce87c7c712016-08-01 23:59:48952 if (params->enable_quic) {
953 if (command_line.HasSwitch(switches::kQuicConnectionOptions)) {
954 params->quic_connection_options =
rch4c967ea2016-12-22 01:41:07955 net::ParseQuicConnectionOptions(
bnce87c7c712016-08-01 23:59:48956 command_line.GetSwitchValueASCII(
957 switches::kQuicConnectionOptions));
958 }
959
bnce87c7c712016-08-01 23:59:48960 if (command_line.HasSwitch(switches::kQuicMaxPacketLength)) {
961 unsigned value;
962 if (base::StringToUint(
963 command_line.GetSwitchValueASCII(switches::kQuicMaxPacketLength),
964 &value)) {
965 params->quic_max_packet_length = value;
966 }
967 }
968
969 if (command_line.HasSwitch(switches::kQuicVersion)) {
970 net::QuicVersion version = network_session_configurator::ParseQuicVersion(
971 command_line.GetSwitchValueASCII(switches::kQuicVersion));
972 if (version != net::QUIC_VERSION_UNSUPPORTED) {
973 net::QuicVersionVector supported_versions;
974 supported_versions.push_back(version);
975 params->quic_supported_versions = supported_versions;
976 }
977 }
978
979 if (command_line.HasSwitch(switches::kOriginToForceQuicOn)) {
980 std::string origins =
981 command_line.GetSwitchValueASCII(switches::kOriginToForceQuicOn);
982 for (const std::string& host_port : base::SplitString(
983 origins, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) {
984 if (host_port == "*")
985 params->origins_to_force_quic_on.insert(net::HostPortPair());
986 net::HostPortPair quic_origin =
987 net::HostPortPair::FromString(host_port);
988 if (!quic_origin.IsEmpty())
989 params->origins_to_force_quic_on.insert(quic_origin);
990 }
991 }
992 }
993
994 // Parameters only controlled by command line.
995 if (command_line.HasSwitch(switches::kEnableUserAlternateProtocolPorts)) {
996 params->enable_user_alternate_protocol_ports = true;
997 }
martinkrfc8514d2017-01-20 01:37:11998 if (command_line.HasSwitch(switches::kIgnoreCertificateErrors)) {
bnce87c7c712016-08-01 23:59:48999 params->ignore_certificate_errors = true;
martinkrfc8514d2017-01-20 01:37:111000 }
1001 UMA_HISTOGRAM_BOOLEAN(
1002 "Net.Certificate.IgnoreErrors",
1003 command_line.HasSwitch(switches::kIgnoreCertificateErrors));
bnce87c7c712016-08-01 23:59:481004 if (command_line.HasSwitch(switches::kTestingFixedHttpPort)) {
1005 params->testing_fixed_http_port =
1006 GetSwitchValueAsInt(command_line, switches::kTestingFixedHttpPort);
1007 }
1008 if (command_line.HasSwitch(switches::kTestingFixedHttpsPort)) {
1009 params->testing_fixed_https_port =
1010 GetSwitchValueAsInt(command_line, switches::kTestingFixedHttpsPort);
1011 }
mmenkea7da6da2016-09-01 21:56:521012
1013 params->http_09_on_non_default_ports_enabled =
1014 http_09_on_non_default_ports_enabled;
bnce87c7c712016-08-01 23:59:481015}
1016
robliao7253fd22016-12-01 18:41:381017metrics::UpdateUsagePrefCallbackType IOThread::GetMetricsDataUseForwarder() {
1018 return base::Bind(&UpdateMetricsUsagePrefsOnUIThread);
gayane0b46091c2016-04-07 21:01:051019}