blob: b4bfcd69df864a3c7cc9429cea9a39095593440a [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"
[email protected]7286e3fc2011-07-19 22:13:2423#include "base/stl_util.h"
[email protected]3ea1b182013-02-08 22:38:4124#include "base/strings/string_number_conversions.h"
bnce3553d92014-10-30 22:29:4525#include "base/strings/string_piece.h"
[email protected]1988e1c2013-02-28 20:27:4226#include "base/strings/string_split.h"
[email protected]9c7ddc92013-06-11 01:40:5727#include "base/strings/string_util.h"
davidben2a811e4e2015-12-01 10:49:3428#include "base/strings/utf_string_conversions.h"
[email protected]255620da2013-08-19 13:14:2929#include "base/threading/sequenced_worker_pool.h"
[email protected]3fc40c142011-12-01 13:09:0430#include "base/threading/thread.h"
[email protected]5bab49ec2012-05-04 21:13:1931#include "base/threading/worker_pool.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"
[email protected]026876f32012-08-22 23:53:4037#include "chrome/browser/net/async_dns_field_trial.h"
[email protected]c38831a12011-10-28 12:44:4938#include "chrome/browser/net/chrome_network_delegate.h"
[email protected]4588b3d2012-11-14 00:37:3839#include "chrome/browser/net/dns_probe_service.h"
[email protected]db0e86dd2011-03-16 14:47:2140#include "chrome/browser/net/proxy_service_factory.h"
eranm3c2d6432016-06-01 10:17:2741#include "chrome/browser/net/sth_distributor_provider.h"
sdefresne9fb67692015-08-03 18:48:2242#include "chrome/common/channel_info.h"
[email protected]aa051272014-03-10 05:56:5643#include "chrome/common/chrome_content_client.h"
[email protected]0ac83682010-01-22 17:46:2744#include "chrome/common/chrome_switches.h"
[email protected]bcefe0f2010-11-10 16:19:1045#include "chrome/common/pref_names.h"
eranm3c2d6432016-06-01 10:17:2746#include "components/certificate_transparency/tree_state_tracker.h"
megjablon3476e042014-10-14 19:21:5947#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_prefs.h"
sclittle3f98c6c2015-11-19 22:59:2948#include "components/data_usage/core/data_use_aggregator.h"
sclittlec441f782015-11-12 01:12:0949#include "components/data_usage/core/data_use_amortizer.h"
sclittle3f98c6c2015-11-19 22:59:2950#include "components/data_usage/core/data_use_annotator.h"
gayane0b46091c2016-04-07 21:01:0551#include "components/metrics/metrics_service.h"
drogerc690e8802015-09-21 14:29:1652#include "components/net_log/chrome_net_log.h"
bnc210d6f32016-05-24 07:40:4753#include "components/network_session_configurator/network_session_configurator.h"
[email protected]76b4b152013-12-08 21:10:0454#include "components/policy/core/common/policy_service.h"
brettw39d6ba42016-08-24 16:56:3855#include "components/policy/policy_constants.h"
brettwb1fc1b82016-02-02 00:19:0856#include "components/prefs/pref_registry_simple.h"
57#include "components/prefs/pref_service.h"
abhishek.a2171c612852015-08-31 10:48:1958#include "components/proxy_config/pref_proxy_config_tracker.h"
[email protected]488a0e252014-06-25 04:37:4459#include "components/variations/variations_associated_data.h"
sdefresne9fb67692015-08-03 18:48:2260#include "components/version_info/version_info.h"
[email protected]c38831a12011-10-28 12:44:4961#include "content/public/browser/browser_thread.h"
[email protected]7c4b66b2014-01-04 12:28:1362#include "content/public/browser/cookie_store_factory.h"
eustasfbec9132015-12-30 14:56:5163#include "content/public/common/content_features.h"
brettw90e92602015-10-10 00:12:4064#include "content/public/common/content_switches.h"
rtenneti4d126a72015-06-23 17:32:0965#include "content/public/common/user_agent.h"
[email protected]c2dad292012-09-07 21:27:3566#include "net/base/host_mapping_rules.h"
pauljensen7b34e522016-05-12 18:20:5967#include "net/base/logging_network_change_observer.h"
rdsmith60e6e6fb2015-03-05 16:49:2068#include "net/base/sdch_manager.h"
rsleevi6df54182016-06-13 14:34:2369#include "net/cert/caching_cert_verifier.h"
[email protected]6e7845ae2013-03-29 21:48:1170#include "net/cert/cert_verifier.h"
[email protected]f46f6d52014-02-08 04:00:3971#include "net/cert/cert_verify_proc.h"
[email protected]284303b62013-11-28 15:11:5472#include "net/cert/ct_known_logs.h"
davidbeneb5f8ef32014-09-04 14:14:3273#include "net/cert/ct_log_verifier.h"
estark6f9b3d82016-01-12 21:37:0574#include "net/cert/ct_policy_enforcer.h"
[email protected]284303b62013-11-28 15:11:5475#include "net/cert/ct_verifier.h"
davidbeneb5f8ef32014-09-04 14:14:3276#include "net/cert/multi_log_ct_verifier.h"
[email protected]f46f6d52014-02-08 04:00:3977#include "net/cert/multi_threaded_cert_verifier.h"
eranm3c2d6432016-06-01 10:17:2778#include "net/cert/sth_distributor.h"
79#include "net/cert/sth_observer.h"
[email protected]9a6c2aa2014-01-11 22:39:3980#include "net/cookies/cookie_store.h"
[email protected]bc71b8772013-04-10 20:55:1681#include "net/dns/host_cache.h"
[email protected]f2cb3cf2013-03-21 01:40:5382#include "net/dns/host_resolver.h"
83#include "net/dns/mapped_host_resolver.h"
[email protected]b3ae2db2013-05-30 05:00:0584#include "net/ftp/ftp_network_layer.h"
[email protected]eb3cac72010-02-26 21:07:4585#include "net/http/http_auth_filter.h"
[email protected]fa55e192010-02-15 14:25:5086#include "net/http/http_auth_handler_factory.h"
aberentbba302d2015-12-03 10:20:1987#include "net/http/http_auth_preferences.h"
[email protected]2fb629202010-12-23 23:52:5788#include "net/http/http_network_layer.h"
[email protected]17291a022011-10-10 07:32:5389#include "net/http/http_server_properties_impl.h"
tbansalca83c002016-04-28 20:56:2890#include "net/nqe/external_estimate_provider.h"
91#include "net/nqe/network_quality_estimator.h"
[email protected]6104ea5d2011-04-27 21:37:1292#include "net/proxy/proxy_config_service.h"
[email protected]86933612010-10-16 23:10:3393#include "net/proxy/proxy_script_fetcher_impl.h"
[email protected]6104ea5d2011-04-27 21:37:1294#include "net/proxy/proxy_service.h"
zhongyi81f85c6d92015-10-16 19:34:1495#include "net/socket/ssl_client_socket.h"
[email protected]717e4e22013-04-10 20:52:2396#include "net/socket/tcp_client_socket.h"
[email protected]6b8a3c742014-07-25 00:25:3597#include "net/ssl/channel_id_service.h"
98#include "net/ssl/default_channel_id_store.h"
[email protected]b3ae2db2013-05-30 05:00:0599#include "net/url_request/data_protocol_handler.h"
100#include "net/url_request/file_protocol_handler.h"
101#include "net/url_request/ftp_protocol_handler.h"
[email protected]aa051272014-03-10 05:56:56102#include "net/url_request/static_http_user_agent_settings.h"
[email protected]3dc1bc42012-06-19 08:20:53103#include "net/url_request/url_fetcher.h"
[email protected]f9c8c7c2014-07-31 16:42:31104#include "net/url_request/url_request_context.h"
wjmacleanea309f72015-08-25 20:56:59105#include "net/url_request/url_request_context_builder.h"
[email protected]f9c8c7c2014-07-31 16:42:31106#include "net/url_request/url_request_context_getter.h"
[email protected]b3ae2db2013-05-30 05:00:05107#include "net/url_request/url_request_job_factory_impl.h"
brettw03e61962016-03-15 06:27:04108#include "url/url_constants.h"
[email protected]0ac83682010-01-22 17:46:27109
[email protected]84b7a552014-07-19 04:52:06110#if defined(ENABLE_EXTENSIONS)
111#include "chrome/browser/extensions/event_router_forwarder.h"
112#endif
113
jam1c5a91492016-02-24 20:47:53114#if defined(USE_NSS_CERTS)
eromaned744f32015-04-09 06:35:49115#include "net/cert_net/nss_ocsp.h"
[email protected]a592c0432012-12-01 18:10:29116#endif
[email protected]77feb462011-05-16 23:37:25117
sievers2f1e8112015-12-04 18:43:56118#if BUILDFLAG(ANDROID_JAVA_UI)
jeremyimb6c97ae2015-01-05 22:57:14119#include "base/android/build_info.h"
megjablon174bc342015-11-05 00:30:06120#include "chrome/browser/android/data_usage/external_data_use_observer.h"
tbansaldafbb3e2015-08-19 19:55:33121#include "chrome/browser/android/net/external_estimate_provider_android.h"
sclittlec441f782015-11-12 01:12:09122#include "components/data_usage/android/traffic_stats_amortizer.h"
jeremyimb6c97ae2015-01-05 22:57:14123#endif
124
[email protected]f46f6d52014-02-08 04:00:39125#if defined(OS_CHROMEOS)
126#include "chrome/browser/chromeos/net/cert_verify_proc_chromeos.h"
[email protected]106ccd2c2014-06-17 09:21:00127#include "chromeos/network/host_resolver_impl_chromeos.h"
[email protected]f46f6d52014-02-08 04:00:39128#endif
129
davidben45eb19952016-05-05 16:30:44130#if defined(OS_ANDROID) && defined(ARCH_CPU_ARMEL)
131#include <openssl/cpu.h>
132#include "crypto/openssl_util.h"
133#endif
134
[email protected]631bb742011-11-02 11:29:39135using content::BrowserThread;
136
[email protected]075c0322012-02-14 00:56:44137class SafeBrowsingURLRequestContext;
138
[email protected]21ee224e2011-11-21 02:17:53139// The IOThread object must outlive any tasks posted to the IO thread before the
140// Quit task, so base::Bind() calls are not refcounted.
141
[email protected]0ac83682010-01-22 17:46:27142namespace {
143
tbansalb177b5392015-06-25 11:13:02144// Field trial for network quality estimator. Seeds RTT and downstream
145// throughput observations with values that correspond to the connection type
146// determined by the operating system.
147const char kNetworkQualityEstimatorFieldTrialName[] = "NetworkQualityEstimator";
148
jam1c5a91492016-02-24 20:47:53149#if defined(OS_MACOSX)
[email protected]11f5e3a2012-09-27 00:30:13150void ObserveKeychainEvents() {
thestig00844cea2015-09-08 21:44:52151 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]11f5e3a2012-09-27 00:30:13152 net::CertDatabase::GetInstance()->SetMessageLoopForKeychainEvents();
153}
154#endif
155
zhongyi81f85c6d92015-10-16 19:34:14156// Gets file path into ssl_keylog_file from command line argument or
157// environment variable. Command line argument has priority when
158// both specified.
davidben2a811e4e2015-12-01 10:49:34159base::FilePath GetSSLKeyLogFile(const base::CommandLine& command_line) {
zhongyi81f85c6d92015-10-16 19:34:14160 if (command_line.HasSwitch(switches::kSSLKeyLogFile)) {
davidben2a811e4e2015-12-01 10:49:34161 base::FilePath path =
162 command_line.GetSwitchValuePath(switches::kSSLKeyLogFile);
163 if (!path.empty())
164 return path;
zhongyi81f85c6d92015-10-16 19:34:14165 LOG(WARNING) << "ssl-key-log-file argument missing";
166 }
davidben2a811e4e2015-12-01 10:49:34167
dcheng4af48582016-04-19 00:29:35168 std::unique_ptr<base::Environment> env(base::Environment::Create());
davidben2a811e4e2015-12-01 10:49:34169 std::string path_str;
170 env->GetVar("SSLKEYLOGFILE", &path_str);
171#if defined(OS_WIN)
172 // base::Environment returns environment variables in UTF-8 on Windows.
173 return base::FilePath(base::UTF8ToUTF16(path_str));
174#else
175 return base::FilePath(path_str);
176#endif
zhongyi81f85c6d92015-10-16 19:34:14177}
178
[email protected]ee4c30d2012-11-07 15:08:43179// Used for the "system" URLRequestContext.
180class SystemURLRequestContext : public net::URLRequestContext {
[email protected]77feb462011-05-16 23:37:25181 public:
182 SystemURLRequestContext() {
jam1c5a91492016-02-24 20:47:53183#if defined(USE_NSS_CERTS)
[email protected]8c434cbc2012-03-14 14:25:09184 net::SetURLRequestContextForNSSHttpIO(this);
[email protected]a592c0432012-12-01 18:10:29185#endif
[email protected]77feb462011-05-16 23:37:25186 }
187
188 private:
Daniel Chenga542fca2014-10-21 09:51:29189 ~SystemURLRequestContext() override {
[email protected]424559492014-07-22 00:27:40190 AssertNoURLRequests();
jam1c5a91492016-02-24 20:47:53191#if defined(USE_NSS_CERTS)
[email protected]8c434cbc2012-03-14 14:25:09192 net::SetURLRequestContextForNSSHttpIO(NULL);
[email protected]a592c0432012-12-01 18:10:29193#endif
[email protected]77feb462011-05-16 23:37:25194 }
195};
196
dcheng4af48582016-04-19 00:29:35197std::unique_ptr<net::HostResolver> CreateGlobalHostResolver(
198 net::NetLog* net_log) {
[email protected]d22f06e2013-06-11 16:01:17199 TRACE_EVENT0("startup", "IOThread::CreateGlobalHostResolver");
avi556c05022014-12-22 23:31:43200 const base::CommandLine& command_line =
201 *base::CommandLine::ForCurrentProcess();
[email protected]962b98212010-07-17 03:37:51202
[email protected]c54a8912012-10-22 22:09:43203 net::HostResolver::Options options;
[email protected]962b98212010-07-17 03:37:51204
[email protected]06ef6d92011-05-19 04:24:58205 // Use the retry attempts override from the command-line, if any.
206 if (command_line.HasSwitch(switches::kHostResolverRetryAttempts)) {
207 std::string s =
208 command_line.GetSwitchValueASCII(switches::kHostResolverRetryAttempts);
209 // Parse the switch (it should be a non-negative integer).
210 int n;
211 if (base::StringToInt(s, &n) && n >= 0) {
[email protected]c54a8912012-10-22 22:09:43212 options.max_retry_attempts = static_cast<size_t>(n);
[email protected]06ef6d92011-05-19 04:24:58213 } else {
214 LOG(ERROR) << "Invalid switch for host resolver retry attempts: " << s;
215 }
216 }
217
dcheng4af48582016-04-19 00:29:35218 std::unique_ptr<net::HostResolver> global_host_resolver;
[email protected]106ccd2c2014-06-17 09:21:00219#if defined OS_CHROMEOS
220 global_host_resolver =
221 chromeos::HostResolverImplChromeOS::CreateSystemResolver(options,
222 net_log);
223#else
224 global_host_resolver =
225 net::HostResolver::CreateSystemResolver(options, net_log);
226#endif
[email protected]9087aa32010-02-18 08:03:38227
[email protected]3dc5d7ef2014-03-28 19:18:21228 // If hostname remappings were specified on the command-line, layer these
229 // rules on top of the real host resolver. This allows forwarding all requests
230 // through a designated test server.
231 if (!command_line.HasSwitch(switches::kHostResolverRules))
dchenge73d8520c2015-12-27 01:19:09232 return global_host_resolver;
[email protected]0ac83682010-01-22 17:46:27233
dcheng4af48582016-04-19 00:29:35234 std::unique_ptr<net::MappedHostResolver> remapped_resolver(
dchenge73d8520c2015-12-27 01:19:09235 new net::MappedHostResolver(std::move(global_host_resolver)));
[email protected]3dc5d7ef2014-03-28 19:18:21236 remapped_resolver->SetRulesFromString(
237 command_line.GetSwitchValueASCII(switches::kHostResolverRules));
dchenge73d8520c2015-12-27 01:19:09238 return std::move(remapped_resolver);
[email protected]0ac83682010-01-22 17:46:27239}
240
jam1671ed32016-07-27 16:31:24241int GetSwitchValueAsInt(const base::CommandLine& command_line,
242 const std::string& switch_name) {
243 int value;
244 if (!base::StringToInt(command_line.GetSwitchValueASCII(switch_name),
245 &value)) {
246 return 0;
247 }
248 return value;
249}
250
[email protected]0ac83682010-01-22 17:46:27251} // namespace
252
[email protected]abe2c032011-03-31 18:49:34253class SystemURLRequestContextGetter : public net::URLRequestContextGetter {
[email protected]db0e86dd2011-03-16 14:47:21254 public:
255 explicit SystemURLRequestContextGetter(IOThread* io_thread);
[email protected]db0e86dd2011-03-16 14:47:21256
[email protected]abe2c032011-03-31 18:49:34257 // Implementation for net::UrlRequestContextGetter.
Daniel Chenga542fca2014-10-21 09:51:29258 net::URLRequestContext* GetURLRequestContext() override;
259 scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner()
260 const override;
[email protected]db0e86dd2011-03-16 14:47:21261
[email protected]13ed17f82012-04-06 02:27:18262 protected:
Daniel Chenga542fca2014-10-21 09:51:29263 ~SystemURLRequestContextGetter() override;
[email protected]13ed17f82012-04-06 02:27:18264
[email protected]db0e86dd2011-03-16 14:47:21265 private:
266 IOThread* const io_thread_; // Weak pointer, owned by BrowserProcess.
[email protected]4969b0122012-06-16 01:58:28267 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
[email protected]db0e86dd2011-03-16 14:47:21268
269 base::debug::LeakTracker<SystemURLRequestContextGetter> leak_tracker_;
270};
271
272SystemURLRequestContextGetter::SystemURLRequestContextGetter(
273 IOThread* io_thread)
274 : io_thread_(io_thread),
[email protected]4969b0122012-06-16 01:58:28275 network_task_runner_(
thestig529ad8a2016-07-08 20:30:12276 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO)) {}
[email protected]db0e86dd2011-03-16 14:47:21277
278SystemURLRequestContextGetter::~SystemURLRequestContextGetter() {}
279
280net::URLRequestContext* SystemURLRequestContextGetter::GetURLRequestContext() {
thestig00844cea2015-09-08 21:44:52281 DCHECK_CURRENTLY_ON(BrowserThread::IO);
[email protected]ef2bf422012-05-11 03:27:09282 DCHECK(io_thread_->globals()->system_request_context.get());
[email protected]db0e86dd2011-03-16 14:47:21283
[email protected]ef2bf422012-05-11 03:27:09284 return io_thread_->globals()->system_request_context.get();
[email protected]db0e86dd2011-03-16 14:47:21285}
286
[email protected]4969b0122012-06-16 01:58:28287scoped_refptr<base::SingleThreadTaskRunner>
288SystemURLRequestContextGetter::GetNetworkTaskRunner() const {
289 return network_task_runner_;
[email protected]db0e86dd2011-03-16 14:47:21290}
291
[email protected]c93123fa2012-04-19 02:49:48292IOThread::Globals::
293SystemRequestContextLeakChecker::SystemRequestContextLeakChecker(
294 Globals* globals)
295 : globals_(globals) {
296 DCHECK(globals_);
[email protected]7613faae2012-04-18 01:01:19297}
[email protected]1889dc1b2010-10-14 22:03:13298
[email protected]c93123fa2012-04-19 02:49:48299IOThread::Globals::
300SystemRequestContextLeakChecker::~SystemRequestContextLeakChecker() {
301 if (globals_->system_request_context.get())
302 globals_->system_request_context->AssertNoURLRequests();
303}
304
maksim.sisov3d40c812016-05-02 13:27:16305IOThread::Globals::Globals() : system_request_context_leak_checker(this),
306 enable_brotli(false) {}
[email protected]c2dad292012-09-07 21:27:35307
[email protected]c93123fa2012-04-19 02:49:48308IOThread::Globals::~Globals() {}
309
[email protected]bcefe0f2010-11-10 16:19:10310// |local_state| is passed in explicitly in order to (1) reduce implicit
311// dependencies and (2) make IOThread more flexible for testing.
[email protected]3ce02412011-03-01 12:01:15312IOThread::IOThread(
[email protected]b1de2c72013-02-06 02:45:47313 PrefService* local_state,
[email protected]77305422012-11-29 16:51:39314 policy::PolicyService* policy_service,
drogerc690e8802015-09-21 14:29:16315 net_log::ChromeNetLog* net_log,
[email protected]5a38dfd2012-07-23 23:22:10316 extensions::EventRouterForwarder* extension_event_router_forwarder)
[email protected]2e5b60a22011-11-28 15:56:41317 : net_log_(net_log),
[email protected]84b7a552014-07-19 04:52:06318#if defined(ENABLE_EXTENSIONS)
[email protected]3ce02412011-03-01 12:01:15319 extension_event_router_forwarder_(extension_event_router_forwarder),
[email protected]84b7a552014-07-19 04:52:06320#endif
[email protected]d13c3272010-02-04 00:24:51321 globals_(NULL),
peletskyi5df83d42015-04-30 16:37:46322 is_quic_allowed_by_policy_(true),
mohan.reddy14cb4ad42014-09-17 18:15:14323 creation_time_(base::TimeTicks::Now()),
324 weak_factory_(this) {
aberentbba302d2015-12-03 10:20:19325 scoped_refptr<base::SingleThreadTaskRunner> io_thread_proxy =
thestig529ad8a2016-07-08 20:30:12326 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO);
[email protected]bcefe0f2010-11-10 16:19:10327 auth_schemes_ = local_state->GetString(prefs::kAuthSchemes);
aberentbba302d2015-12-03 10:20:19328 negotiate_disable_cname_lookup_.Init(
329 prefs::kDisableAuthNegotiateCnameLookup, local_state,
330 base::Bind(&IOThread::UpdateNegotiateDisableCnameLookup,
331 base::Unretained(this)));
332 negotiate_disable_cname_lookup_.MoveToThread(io_thread_proxy);
333 negotiate_enable_port_.Init(
334 prefs::kEnableAuthNegotiatePort, local_state,
335 base::Bind(&IOThread::UpdateNegotiateEnablePort, base::Unretained(this)));
336 negotiate_enable_port_.MoveToThread(io_thread_proxy);
337 auth_server_whitelist_.Init(
338 prefs::kAuthServerWhitelist, local_state,
339 base::Bind(&IOThread::UpdateServerWhitelist, base::Unretained(this)));
340 auth_server_whitelist_.MoveToThread(io_thread_proxy);
341 auth_delegate_whitelist_.Init(
342 prefs::kAuthNegotiateDelegateWhitelist, local_state,
343 base::Bind(&IOThread::UpdateDelegateWhitelist, base::Unretained(this)));
344 auth_delegate_whitelist_.MoveToThread(io_thread_proxy);
345#if defined(OS_ANDROID)
346 auth_android_negotiate_account_type_.Init(
347 prefs::kAuthAndroidNegotiateAccountType, local_state,
348 base::Bind(&IOThread::UpdateAndroidAuthNegotiateAccountType,
349 base::Unretained(this)));
350 auth_android_negotiate_account_type_.MoveToThread(io_thread_proxy);
351#endif
352#if defined(OS_POSIX) && !defined(OS_ANDROID)
[email protected]ac7f3fdb2010-11-12 12:47:05353 gssapi_library_name_ = local_state->GetString(prefs::kGSSAPILibraryName);
aberentbba302d2015-12-03 10:20:19354#endif
[email protected]6f96cbcb2011-11-04 02:26:07355 pref_proxy_config_tracker_.reset(
[email protected]e2930d0902013-07-17 05:25:42356 ProxyServiceFactory::CreatePrefProxyConfigTrackerOfLocalState(
357 local_state));
[email protected]9d8cfb682012-09-13 16:48:04358 ChromeNetworkDelegate::InitializePrefsOnUIThread(
359 &system_enable_referrers_,
igorcov193d8ad32016-05-27 11:03:21360 nullptr,
361 nullptr,
362 nullptr,
363 nullptr,
[email protected]9d8cfb682012-09-13 16:48:04364 local_state);
[email protected]4d45a6de2011-05-13 05:20:18365 ssl_config_service_manager_.reset(
abhishek.a212849cee2015-10-20 11:27:29366 ssl_config::SSLConfigServiceManager::CreateDefaultManager(
367 local_state,
thestig529ad8a2016-07-08 20:30:12368 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO)));
[email protected]2e5b60a22011-11-28 15:56:41369
[email protected]68a9b0d82013-03-08 07:05:07370 base::Value* dns_client_enabled_default = new base::FundamentalValue(
371 chrome_browser_net::ConfigureAsyncDnsFieldTrial());
372 local_state->SetDefaultPrefValue(prefs::kBuiltInDnsClientEnabled,
373 dns_client_enabled_default);
ttuttle530184962015-01-13 00:24:07374 chrome_browser_net::LogAsyncDnsPrefSource(
375 local_state->FindPreference(prefs::kBuiltInDnsClientEnabled));
[email protected]68a9b0d82013-03-08 07:05:07376
[email protected]fa4b6c32012-11-26 23:02:39377 dns_client_enabled_.Init(prefs::kBuiltInDnsClientEnabled,
378 local_state,
379 base::Bind(&IOThread::UpdateDnsClientEnabled,
380 base::Unretained(this)));
aberentbba302d2015-12-03 10:20:19381 dns_client_enabled_.MoveToThread(io_thread_proxy);
[email protected]fa4b6c32012-11-26 23:02:39382
[email protected]67378142013-12-17 21:57:17383 quick_check_enabled_.Init(prefs::kQuickCheckEnabled,
384 local_state);
aberentbba302d2015-12-03 10:20:19385 quick_check_enabled_.MoveToThread(io_thread_proxy);
[email protected]67378142013-12-17 21:57:17386
eroman9f7ea642016-06-03 21:28:29387 pac_https_url_stripping_enabled_.Init(prefs::kPacHttpsUrlStrippingEnabled,
388 local_state);
389 pac_https_url_stripping_enabled_.MoveToThread(io_thread_proxy);
390
peletskyi5df83d42015-04-30 16:37:46391 const base::Value* value = policy_service->GetPolicies(
392 policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME,
393 std::string())).GetValue(policy::key::kQuicAllowed);
394 if (value)
395 value->GetAsBoolean(&is_quic_allowed_by_policy_);
[email protected]77305422012-11-29 16:51:39396
gayane0b46091c2016-04-07 21:01:05397 // Some unit tests use IOThread but do not initialize MetricsService. In that
398 // case it is fine not to have |metrics_data_use_forwarder_|.
399 if (g_browser_process->metrics_service()) {
400 // Callback for updating data use prefs should be obtained on UI thread.
401 metrics_data_use_forwarder_ =
402 g_browser_process->metrics_service()->GetDataUseForwardingCallback();
403 }
404
eranm3c2d6432016-06-01 10:17:27405 chrome_browser_net::SetGlobalSTHDistributor(
406 std::unique_ptr<net::ct::STHDistributor>(new net::ct::STHDistributor()));
407
[email protected]2e5b60a22011-11-28 15:56:41408 BrowserThread::SetDelegate(BrowserThread::IO, this);
[email protected]bcefe0f2010-11-10 16:19:10409}
[email protected]0ac83682010-01-22 17:46:27410
411IOThread::~IOThread() {
[email protected]2e5b60a22011-11-28 15:56:41412 // This isn't needed for production code, but in tests, IOThread may
413 // be multiply constructed.
414 BrowserThread::SetDelegate(BrowserThread::IO, NULL);
415
[email protected]d461ed22013-01-18 03:18:56416 pref_proxy_config_tracker_->DetachFromPrefService();
[email protected]d13c3272010-02-04 00:24:51417 DCHECK(!globals_);
eranm3c2d6432016-06-01 10:17:27418
419 // Destroy the old distributor to check that the observers list it holds is
420 // empty.
421 chrome_browser_net::SetGlobalSTHDistributor(nullptr);
[email protected]0ac83682010-01-22 17:46:27422}
423
[email protected]d13c3272010-02-04 00:24:51424IOThread::Globals* IOThread::globals() {
thestig00844cea2015-09-08 21:44:52425 DCHECK_CURRENTLY_ON(BrowserThread::IO);
[email protected]d13c3272010-02-04 00:24:51426 return globals_;
[email protected]0ac83682010-01-22 17:46:27427}
428
[email protected]37ac95b2013-07-23 23:39:35429void IOThread::SetGlobalsForTesting(Globals* globals) {
thestig00844cea2015-09-08 21:44:52430 DCHECK_CURRENTLY_ON(BrowserThread::IO);
[email protected]37ac95b2013-07-23 23:39:35431 DCHECK(!globals || !globals_);
432 globals_ = globals;
433}
434
drogerc690e8802015-09-21 14:29:16435net_log::ChromeNetLog* IOThread::net_log() {
[email protected]b2fcd0e2010-12-01 15:19:40436 return net_log_;
437}
438
[email protected]b09f76d62011-12-07 01:51:06439void IOThread::ChangedToOnTheRecord() {
thestig00844cea2015-09-08 21:44:52440 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]b09f76d62011-12-07 01:51:06441 BrowserThread::PostTask(
442 BrowserThread::IO,
443 FROM_HERE,
444 base::Bind(&IOThread::ChangedToOnTheRecordOnIOThread,
445 base::Unretained(this)));
446}
447
[email protected]abe2c032011-03-31 18:49:34448net::URLRequestContextGetter* IOThread::system_url_request_context_getter() {
thestig00844cea2015-09-08 21:44:52449 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]5173de8b2013-06-02 21:16:02450 if (!system_url_request_context_getter_.get()) {
[email protected]addb3242011-06-13 21:39:16451 InitSystemRequestContext();
[email protected]db0e86dd2011-03-16 14:47:21452 }
[email protected]5173de8b2013-06-02 21:16:02453 return system_url_request_context_getter_.get();
[email protected]db0e86dd2011-03-16 14:47:21454}
455
[email protected]0ac83682010-01-22 17:46:27456void IOThread::Init() {
[email protected]a5e73b82013-07-17 08:58:39457 TRACE_EVENT0("startup", "IOThread::InitAsync");
thestig00844cea2015-09-08 21:44:52458 DCHECK_CURRENTLY_ON(BrowserThread::IO);
[email protected]70b92342010-10-12 05:54:06459
jam1c5a91492016-02-24 20:47:53460#if defined(USE_NSS_CERTS)
[email protected]8c434cbc2012-03-14 14:25:09461 net::SetMessageLoopForNSSHttpIO();
[email protected]a592c0432012-12-01 18:10:29462#endif
[email protected]70b92342010-10-12 05:54:06463
avi556c05022014-12-22 23:31:43464 const base::CommandLine& command_line =
465 *base::CommandLine::ForCurrentProcess();
[email protected]c2dad292012-09-07 21:27:35466
zhongyi81f85c6d92015-10-16 19:34:14467 // Export ssl keys if log file specified.
davidben2a811e4e2015-12-01 10:49:34468 base::FilePath ssl_keylog_file = GetSSLKeyLogFile(command_line);
zhongyi81f85c6d92015-10-16 19:34:14469 if (!ssl_keylog_file.empty()) {
davidben2a811e4e2015-12-01 10:49:34470 net::SSLClientSocket::SetSSLKeyLogFile(
471 ssl_keylog_file,
thestig529ad8a2016-07-08 20:30:12472 BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE));
zhongyi81f85c6d92015-10-16 19:34:14473 }
474
[email protected]d13c3272010-02-04 00:24:51475 DCHECK(!globals_);
476 globals_ = new Globals;
477
[email protected]58bc7042010-07-07 18:04:14478 // Add an observer that will emit network change events to the ChromeNetLog.
479 // Assuming NetworkChangeNotifier dispatches in FIFO order, we should be
480 // logging the network change before other IO thread consumers respond to it.
481 network_change_observer_.reset(
pauljensen7b34e522016-05-12 18:20:59482 new net::LoggingNetworkChangeObserver(net_log_));
[email protected]58bc7042010-07-07 18:04:14483
[email protected]cde8b3c2012-08-13 19:20:52484 // Setup the HistogramWatcher to run on the IO thread.
485 net::NetworkChangeNotifier::InitHistogramWatcher();
486
[email protected]84b7a552014-07-19 04:52:06487#if defined(ENABLE_EXTENSIONS)
[email protected]3ce02412011-03-01 12:01:15488 globals_->extension_event_router_forwarder =
489 extension_event_router_forwarder_;
[email protected]84b7a552014-07-19 04:52:06490#endif
491
dcheng4af48582016-04-19 00:29:35492 std::unique_ptr<data_usage::DataUseAmortizer> data_use_amortizer;
sievers2f1e8112015-12-04 18:43:56493#if BUILDFLAG(ANDROID_JAVA_UI)
sclittlec441f782015-11-12 01:12:09494 data_use_amortizer.reset(new data_usage::android::TrafficStatsAmortizer());
495#endif
496
sclittle3f98c6c2015-11-19 22:59:29497 globals_->data_use_aggregator.reset(new data_usage::DataUseAggregator(
dcheng4af48582016-04-19 00:29:35498 std::unique_ptr<data_usage::DataUseAnnotator>(
sclittlec441f782015-11-12 01:12:09499 new chrome_browser_data_usage::TabIdAnnotator()),
dchenge73d8520c2015-12-27 01:19:09500 std::move(data_use_amortizer)));
sclittleae932be2015-10-08 20:53:50501
dcheng4af48582016-04-19 00:29:35502 std::unique_ptr<ChromeNetworkDelegate> chrome_network_delegate(
[email protected]84b7a552014-07-19 04:52:06503 new ChromeNetworkDelegate(extension_event_router_forwarder(),
gayane0b46091c2016-04-07 21:01:05504 &system_enable_referrers_,
505 metrics_data_use_forwarder_));
sclittleae932be2015-10-08 20:53:50506 // By default, data usage is considered off the record.
507 chrome_network_delegate->set_data_use_aggregator(
sclittle3f98c6c2015-11-19 22:59:29508 globals_->data_use_aggregator.get(),
509 true /* is_data_usage_off_the_record */);
tbansal796988c2015-10-14 02:41:22510
sievers2f1e8112015-12-04 18:43:56511#if BUILDFLAG(ANDROID_JAVA_UI)
sclittle3f98c6c2015-11-19 22:59:29512 globals_->external_data_use_observer.reset(
tbansal7c98c0a2015-11-03 21:39:51513 new chrome::android::ExternalDataUseObserver(
sclittle3f98c6c2015-11-19 22:59:29514 globals_->data_use_aggregator.get(),
thestig529ad8a2016-07-08 20:30:12515 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO),
516 BrowserThread::GetTaskRunnerForThread(BrowserThread::UI)));
tbansal796988c2015-10-14 02:41:22517#endif
[email protected]84b7a552014-07-19 04:52:06518
dchenge73d8520c2015-12-27 01:19:09519 globals_->system_network_delegate = std::move(chrome_network_delegate);
[email protected]c54a8912012-10-22 22:09:43520 globals_->host_resolver = CreateGlobalHostResolver(net_log_);
tbansalea2fb8c2015-05-22 22:23:00521
tbansalb177b5392015-06-25 11:13:02522 std::map<std::string, std::string> network_quality_estimator_params;
523 variations::GetVariationParams(kNetworkQualityEstimatorFieldTrialName,
524 &network_quality_estimator_params);
tbansal1c92d5b2015-08-14 20:14:43525
dcheng4af48582016-04-19 00:29:35526 std::unique_ptr<net::ExternalEstimateProvider> external_estimate_provider;
sievers2f1e8112015-12-04 18:43:56527#if BUILDFLAG(ANDROID_JAVA_UI)
tbansal1c92d5b2015-08-14 20:14:43528 external_estimate_provider.reset(
tbansaldafbb3e2015-08-19 19:55:33529 new chrome::android::ExternalEstimateProviderAndroid());
tbansal1c92d5b2015-08-14 20:14:43530#endif
531 // Pass ownership.
532 globals_->network_quality_estimator.reset(new net::NetworkQualityEstimator(
dchenge73d8520c2015-12-27 01:19:09533 std::move(external_estimate_provider), network_quality_estimator_params));
tbansalea2fb8c2015-05-22 22:23:00534
[email protected]fa4b6c32012-11-26 23:02:39535 UpdateDnsClientEnabled();
[email protected]f46f6d52014-02-08 04:00:39536#if defined(OS_CHROMEOS)
[email protected]f9357a442014-05-15 18:44:07537 // Creates a CertVerifyProc that doesn't allow any profile-provided certs.
rsleevi6df54182016-06-13 14:34:23538 globals_->cert_verifier = base::MakeUnique<net::CachingCertVerifier>(
539 base::MakeUnique<net::MultiThreadedCertVerifier>(
540 new chromeos::CertVerifyProcChromeOS()));
[email protected]f9357a442014-05-15 18:44:07541#else
rsleevi6df54182016-06-13 14:34:23542 globals_->cert_verifier = net::CertVerifier::CreateDefault();
[email protected]f9357a442014-05-15 18:44:07543#endif
544
davidbeneb5f8ef32014-09-04 14:14:32545 globals_->transport_security_state.reset(new net::TransportSecurityState());
546
eranm1a79db22015-11-24 10:56:49547 std::vector<scoped_refptr<const net::CTLogVerifier>> ct_logs(
eranm03d454c2015-07-02 09:12:03548 net::ct::CreateLogVerifiersForKnownLogs());
[email protected]284303b62013-11-28 15:11:54549
eranm1a79db22015-11-24 10:56:49550 globals_->ct_logs.assign(ct_logs.begin(), ct_logs.end());
551
eranm03d454c2015-07-02 09:12:03552 net::MultiLogCTVerifier* ct_verifier = new net::MultiLogCTVerifier();
553 globals_->cert_transparency_verifier.reset(ct_verifier);
554 // Add built-in logs
eranm1a79db22015-11-24 10:56:49555 ct_verifier->AddLogs(globals_->ct_logs);
eranm03d454c2015-07-02 09:12:03556
eranm3c2d6432016-06-01 10:17:27557 ct_tree_tracker_.reset(
558 new certificate_transparency::TreeStateTracker(globals_->ct_logs));
559 // Register the ct_tree_tracker_ as observer for new STHs.
560 RegisterSTHObserver(ct_tree_tracker_.get());
561 // Register the ct_tree_tracker_ as observer for verified SCTs.
562 globals_->cert_transparency_verifier->SetObserver(ct_tree_tracker_.get());
563
bnc18fb77f2016-04-04 20:54:04564 globals_->ct_policy_enforcer.reset(new net::CTPolicyEnforcer());
565 params_.ct_policy_enforcer = globals_->ct_policy_enforcer.get();
eranm6571b2b2014-12-03 15:53:23566
[email protected]4d45a6de2011-05-13 05:20:18567 globals_->ssl_config_service = GetSSLConfigService();
[email protected]f4bfa7672014-08-08 09:50:46568
aberentbba302d2015-12-03 10:20:19569 CreateDefaultAuthHandlerFactory();
[email protected]30d4c022013-07-18 22:58:16570 globals_->http_server_properties.reset(new net::HttpServerPropertiesImpl());
[email protected]2fb629202010-12-23 23:52:57571 // For the ProxyScriptFetcher, we use a direct ProxyService.
rdsmith82957ad2015-09-16 19:42:03572 globals_->proxy_script_fetcher_proxy_service =
573 net::ProxyService::CreateDirectWithNetLog(net_log_);
[email protected]273e37d2011-08-11 01:49:12574 // In-memory cookie store.
[email protected]9a6c2aa2014-01-11 22:39:39575 globals_->system_cookie_store =
mmenke606c59c2016-03-07 18:20:55576 content::CreateCookieStore(content::CookieStoreConfig());
[email protected]6b8a3c742014-07-25 00:25:35577 // In-memory channel ID store.
578 globals_->system_channel_id_service.reset(
579 new net::ChannelIDService(
580 new net::DefaultChannelIDStore(NULL),
[email protected]5bab49ec2012-05-04 21:13:19581 base::WorkerPool::GetTaskRunner(true)));
nharper3876dd562016-03-29 22:52:32582 globals_->system_cookie_store->SetChannelIDServiceID(
583 globals_->system_channel_id_service->GetUniqueID());
[email protected]4588b3d2012-11-14 00:37:38584 globals_->dns_probe_service.reset(new chrome_browser_net::DnsProbeService());
[email protected]c2dad292012-09-07 21:27:35585 globals_->host_mapping_rules.reset(new net::HostMappingRules());
bnc18fb77f2016-04-04 20:54:04586 params_.host_mapping_rules = globals_->host_mapping_rules.get();
[email protected]ee4c30d2012-11-07 15:08:43587 globals_->http_user_agent_settings.reset(
[email protected]aa051272014-03-10 05:56:56588 new net::StaticHttpUserAgentSettings(std::string(), GetUserAgent()));
[email protected]c2dad292012-09-07 21:27:35589 if (command_line.HasSwitch(switches::kHostRules)) {
[email protected]a5e73b82013-07-17 08:58:39590 TRACE_EVENT_BEGIN0("startup", "IOThread::InitAsync:SetRulesFromString");
[email protected]c2dad292012-09-07 21:27:35591 globals_->host_mapping_rules->SetRulesFromString(
592 command_line.GetSwitchValueASCII(switches::kHostRules));
[email protected]a5e73b82013-07-17 08:58:39593 TRACE_EVENT_END0("startup", "IOThread::InitAsync:SetRulesFromString");
[email protected]c2dad292012-09-07 21:27:35594 }
maksim.sisov3d40c812016-05-02 13:27:16595 globals_->enable_brotli =
bnc18fb77f2016-04-04 20:54:04596 base::FeatureList::IsEnabled(features::kBrotliEncoding);
597 params_.enable_token_binding =
nharperef2618642016-02-03 22:23:44598 base::FeatureList::IsEnabled(features::kTokenBinding);
bnce87c7c712016-08-01 23:59:48599
600 // Check for OS support of TCP FastOpen, and turn it on for all connections if
601 // indicated by user.
bnc18fb77f2016-04-04 20:54:04602 // TODO(rch): Make the client socket factory a per-network session instance,
603 // constructed from a NetworkSession::Params, to allow us to move this option
604 // to IOThread::Globals & HttpNetworkSession::Params.
bnc18fb77f2016-04-04 20:54:04605 bool always_enable_tfo_if_supported =
606 command_line.HasSwitch(switches::kEnableTcpFastOpen);
bnc18fb77f2016-04-04 20:54:04607 net::CheckSupportAndMaybeEnableTCPFastOpen(always_enable_tfo_if_supported);
[email protected]f9cf5572012-12-04 15:52:09608
bnce87c7c712016-08-01 23:59:48609 ConfigureParamsFromFieldTrialsAndCommandLine(
610 command_line, is_quic_allowed_by_policy_, &params_);
611
wjmacleanea309f72015-08-25 20:56:59612 TRACE_EVENT_BEGIN0("startup",
613 "IOThread::Init:ProxyScriptFetcherRequestContext");
[email protected]ef2bf422012-05-11 03:27:09614 globals_->proxy_script_fetcher_context.reset(
bnc18fb77f2016-04-04 20:54:04615 ConstructProxyScriptFetcherContext(globals_, params_, net_log_));
wjmacleanea309f72015-08-25 20:56:59616 TRACE_EVENT_END0("startup",
617 "IOThread::Init:ProxyScriptFetcherRequestContext");
[email protected]4a109492011-09-24 21:00:12618
jam1c5a91492016-02-24 20:47:53619#if defined(OS_MACOSX)
[email protected]11f5e3a2012-09-27 00:30:13620 // Start observing Keychain events. This needs to be done on the UI thread,
621 // as Keychain services requires a CFRunLoop.
622 BrowserThread::PostTask(BrowserThread::UI,
623 FROM_HERE,
624 base::Bind(&ObserveKeychainEvents));
625#endif
626
[email protected]2e5b60a22011-11-28 15:56:41627 // InitSystemRequestContext turns right around and posts a task back
628 // to the IO thread, so we can't let it run until we know the IO
629 // thread has started.
630 //
631 // Note that since we are at BrowserThread::Init time, the UI thread
632 // is blocked waiting for the thread to start. Therefore, posting
633 // this task to the main thread's message loop here is guaranteed to
634 // get it onto the message loop while the IOThread object still
635 // exists. However, the message might not be processed on the UI
636 // thread until after IOThread is gone, so use a weak pointer.
637 BrowserThread::PostTask(BrowserThread::UI,
638 FROM_HERE,
639 base::Bind(&IOThread::InitSystemRequestContext,
640 weak_factory_.GetWeakPtr()));
davidben45eb19952016-05-05 16:30:44641
642#if defined(OS_ANDROID) && defined(ARCH_CPU_ARMEL)
643 // Record how common CPUs with broken NEON units are. See
644 // https://crbug.com/341598.
645 crypto::EnsureOpenSSLInit();
646 UMA_HISTOGRAM_BOOLEAN("Net.HasBrokenNEON", CRYPTO_has_broken_NEON());
647#endif
[email protected]0ac83682010-01-22 17:46:27648}
649
[email protected]2a92cd92010-04-27 00:01:41650void IOThread::CleanUp() {
[email protected]075c0322012-02-14 00:56:44651 base::debug::LeakTracker<SafeBrowsingURLRequestContext>::CheckForLeaks();
652
jam1c5a91492016-02-24 20:47:53653#if defined(USE_NSS_CERTS)
[email protected]8c434cbc2012-03-14 14:25:09654 net::ShutdownNSSHttpIO();
[email protected]a592c0432012-12-01 18:10:29655#endif
[email protected]59a3b362010-10-21 21:52:41656
[email protected]db0e86dd2011-03-16 14:47:21657 system_url_request_context_getter_ = NULL;
658
eranm3c2d6432016-06-01 10:17:27659 // Unlink the ct_tree_tracker_ from the global cert_transparency_verifier
660 // and unregister it from new STH notifications so it will take no actions
661 // on anything observed during CleanUp process.
662 globals()->cert_transparency_verifier->SetObserver(nullptr);
663 UnregisterSTHObserver(ct_tree_tracker_.get());
664
665 ct_tree_tracker_.reset();
666
[email protected]af669932012-01-17 19:26:58667 // Release objects that the net::URLRequestContext could have been pointing
668 // to.
[email protected]0ee7a3b2010-11-09 06:13:40669
[email protected]7592b41f2014-04-01 01:44:43670 // Shutdown the HistogramWatcher on the IO thread.
671 net::NetworkChangeNotifier::ShutdownHistogramWatcher();
672
[email protected]0ee7a3b2010-11-09 06:13:40673 // This must be reset before the ChromeNetLog is destroyed.
674 network_change_observer_.reset();
675
[email protected]db0e86dd2011-03-16 14:47:21676 system_proxy_config_service_.reset();
[email protected]d13c3272010-02-04 00:24:51677 delete globals_;
678 globals_ = NULL;
[email protected]0ac83682010-01-22 17:46:27679
[email protected]db0e86dd2011-03-16 14:47:21680 base::debug::LeakTracker<SystemURLRequestContextGetter>::CheckForLeaks();
[email protected]0ac83682010-01-22 17:46:27681}
682
bnc18fb77f2016-04-04 20:54:04683// static
[email protected]b1de2c72013-02-06 02:45:47684void IOThread::RegisterPrefs(PrefRegistrySimple* registry) {
685 registry->RegisterStringPref(prefs::kAuthSchemes,
bengr70266e22015-06-12 19:38:17686 "basic,digest,ntlm,negotiate");
[email protected]68a9b0d82013-03-08 07:05:07687 registry->RegisterBooleanPref(prefs::kDisableAuthNegotiateCnameLookup, false);
[email protected]b1de2c72013-02-06 02:45:47688 registry->RegisterBooleanPref(prefs::kEnableAuthNegotiatePort, false);
[email protected]007b3f82013-04-09 08:46:45689 registry->RegisterStringPref(prefs::kAuthServerWhitelist, std::string());
690 registry->RegisterStringPref(prefs::kAuthNegotiateDelegateWhitelist,
691 std::string());
692 registry->RegisterStringPref(prefs::kGSSAPILibraryName, std::string());
aberentec894a52015-07-09 14:45:53693 registry->RegisterStringPref(prefs::kAuthAndroidNegotiateAccountType,
694 std::string());
[email protected]b1de2c72013-02-06 02:45:47695 registry->RegisterBooleanPref(prefs::kEnableReferrers, true);
[email protected]1575e3d2014-05-03 22:21:44696 data_reduction_proxy::RegisterPrefs(registry);
[email protected]68a9b0d82013-03-08 07:05:07697 registry->RegisterBooleanPref(prefs::kBuiltInDnsClientEnabled, true);
[email protected]67378142013-12-17 21:57:17698 registry->RegisterBooleanPref(prefs::kQuickCheckEnabled, true);
eroman9f7ea642016-06-03 21:28:29699 registry->RegisterBooleanPref(prefs::kPacHttpsUrlStrippingEnabled, true);
[email protected]bcefe0f2010-11-10 16:19:10700}
701
aberentbba302d2015-12-03 10:20:19702void IOThread::UpdateServerWhitelist() {
703 globals_->http_auth_preferences->set_server_whitelist(
704 auth_server_whitelist_.GetValue());
705}
[email protected]b7304162010-08-23 17:42:29706
aberentbba302d2015-12-03 10:20:19707void IOThread::UpdateDelegateWhitelist() {
708 globals_->http_auth_preferences->set_delegate_whitelist(
709 auth_delegate_whitelist_.GetValue());
710}
711
712#if defined(OS_ANDROID)
713void IOThread::UpdateAndroidAuthNegotiateAccountType() {
714 globals_->http_auth_preferences->set_auth_android_negotiate_account_type(
715 auth_android_negotiate_account_type_.GetValue());
716}
717#endif
718
719void IOThread::UpdateNegotiateDisableCnameLookup() {
720 globals_->http_auth_preferences->set_negotiate_disable_cname_lookup(
721 negotiate_disable_cname_lookup_.GetValue());
722}
723
724void IOThread::UpdateNegotiateEnablePort() {
725 globals_->http_auth_preferences->set_negotiate_enable_port(
726 negotiate_enable_port_.GetValue());
727}
728
729void IOThread::CreateDefaultAuthHandlerFactory() {
730 std::vector<std::string> supported_schemes = base::SplitString(
731 auth_schemes_, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
732 globals_->http_auth_preferences.reset(new net::HttpAuthPreferences(
733 supported_schemes
734#if defined(OS_POSIX) && !defined(OS_ANDROID)
735 ,
736 gssapi_library_name_
737#endif
738 ));
739 UpdateServerWhitelist();
740 UpdateDelegateWhitelist();
741 UpdateNegotiateDisableCnameLookup();
742 UpdateNegotiateEnablePort();
743#if defined(OS_ANDROID)
744 UpdateAndroidAuthNegotiateAccountType();
745#endif
746 globals_->http_auth_handler_factory =
[email protected]ec44ee02012-09-28 21:31:51747 net::HttpAuthHandlerRegistryFactory::Create(
dchenge73d8520c2015-12-27 01:19:09748 globals_->http_auth_preferences.get(), globals_->host_resolver.get());
[email protected]eb3cac72010-02-26 21:07:45749}
750
[email protected]d6f37fc2011-02-13 23:58:41751void IOThread::ClearHostCache() {
thestig00844cea2015-09-08 21:44:52752 DCHECK_CURRENTLY_ON(BrowserThread::IO);
[email protected]d6f37fc2011-02-13 23:58:41753
[email protected]489d1a82011-10-12 03:09:11754 net::HostCache* host_cache = globals_->host_resolver->GetHostCache();
755 if (host_cache)
756 host_cache->clear();
[email protected]0ac83682010-01-22 17:46:27757}
[email protected]db0e86dd2011-03-16 14:47:21758
bnc18fb77f2016-04-04 20:54:04759const net::HttpNetworkSession::Params& IOThread::NetworkSessionParams() const {
760 return params_;
[email protected]f9cf5572012-12-04 15:52:09761}
762
[email protected]d827e112014-03-31 17:45:05763base::TimeTicks IOThread::creation_time() const {
764 return creation_time_;
765}
766
[email protected]4d45a6de2011-05-13 05:20:18767net::SSLConfigService* IOThread::GetSSLConfigService() {
768 return ssl_config_service_manager_->Get();
769}
770
[email protected]b09f76d62011-12-07 01:51:06771void IOThread::ChangedToOnTheRecordOnIOThread() {
thestig00844cea2015-09-08 21:44:52772 DCHECK_CURRENTLY_ON(BrowserThread::IO);
[email protected]b09f76d62011-12-07 01:51:06773
774 // Clear the host cache to avoid showing entries from the OTR session
775 // in about:net-internals.
776 ClearHostCache();
[email protected]b09f76d62011-12-07 01:51:06777}
778
[email protected]db0e86dd2011-03-16 14:47:21779void IOThread::InitSystemRequestContext() {
[email protected]5173de8b2013-06-02 21:16:02780 if (system_url_request_context_getter_.get())
[email protected]addb3242011-06-13 21:39:16781 return;
[email protected]63e26822011-07-16 19:07:35782 // If we're in unit_tests, IOThread may not be run.
[email protected]dd483702011-12-02 14:47:42783 if (!BrowserThread::IsMessageLoopValid(BrowserThread::IO))
[email protected]63e26822011-07-16 19:07:35784 return;
csharrisonb7e3a082015-09-22 19:13:04785 system_proxy_config_service_ = ProxyServiceFactory::CreateProxyConfigService(
786 pref_proxy_config_tracker_.get());
[email protected]addb3242011-06-13 21:39:16787 system_url_request_context_getter_ =
788 new SystemURLRequestContextGetter(this);
[email protected]2e5b60a22011-11-28 15:56:41789 // Safe to post an unretained this pointer, since IOThread is
790 // guaranteed to outlive the IO BrowserThread.
[email protected]dd483702011-12-02 14:47:42791 BrowserThread::PostTask(
792 BrowserThread::IO,
793 FROM_HERE,
794 base::Bind(&IOThread::InitSystemRequestContextOnIOThread,
795 base::Unretained(this)));
[email protected]addb3242011-06-13 21:39:16796}
797
798void IOThread::InitSystemRequestContextOnIOThread() {
thestig00844cea2015-09-08 21:44:52799 DCHECK_CURRENTLY_ON(BrowserThread::IO);
[email protected]6104ea5d2011-04-27 21:37:12800 DCHECK(!globals_->system_proxy_service.get());
[email protected]db0e86dd2011-03-16 14:47:21801 DCHECK(system_proxy_config_service_.get());
802
avi556c05022014-12-22 23:31:43803 const base::CommandLine& command_line =
804 *base::CommandLine::ForCurrentProcess();
rdsmith82957ad2015-09-16 19:42:03805 globals_->system_proxy_service = ProxyServiceFactory::CreateProxyService(
806 net_log_, globals_->proxy_script_fetcher_context.get(),
807 globals_->system_network_delegate.get(),
dchenge73d8520c2015-12-27 01:19:09808 std::move(system_proxy_config_service_), command_line,
eroman9f7ea642016-06-03 21:28:29809 WpadQuickCheckEnabled(), PacHttpsUrlStrippingEnabled());
[email protected]c2dad292012-09-07 21:27:35810
[email protected]ef2bf422012-05-11 03:27:09811 globals_->system_request_context.reset(
bnc18fb77f2016-04-04 20:54:04812 ConstructSystemRequestContext(globals_, params_, net_log_));
[email protected]db0e86dd2011-03-16 14:47:21813}
[email protected]fa4b6c32012-11-26 23:02:39814
815void IOThread::UpdateDnsClientEnabled() {
816 globals()->host_resolver->SetDnsClientEnabled(*dns_client_enabled_);
817}
[email protected]903e63382013-06-01 00:40:58818
eranm3c2d6432016-06-01 10:17:27819void IOThread::RegisterSTHObserver(net::ct::STHObserver* observer) {
820 chrome_browser_net::GetGlobalSTHDistributor()->RegisterObserver(observer);
821}
822
823void IOThread::UnregisterSTHObserver(net::ct::STHObserver* observer) {
824 chrome_browser_net::GetGlobalSTHDistributor()->UnregisterObserver(observer);
825}
826
eroman9f7ea642016-06-03 21:28:29827bool IOThread::WpadQuickCheckEnabled() const {
828 return quick_check_enabled_.GetValue();
829}
830
831bool IOThread::PacHttpsUrlStrippingEnabled() const {
832 return pac_https_url_stripping_enabled_.GetValue();
833}
834
bnc18fb77f2016-04-04 20:54:04835// static
wjmacleanea309f72015-08-25 20:56:59836net::URLRequestContext* IOThread::ConstructSystemRequestContext(
837 IOThread::Globals* globals,
bnc18fb77f2016-04-04 20:54:04838 const net::HttpNetworkSession::Params& params,
wjmacleanea309f72015-08-25 20:56:59839 net::NetLog* net_log) {
840 net::URLRequestContext* context = new SystemURLRequestContext;
841 context->set_net_log(net_log);
842 context->set_host_resolver(globals->host_resolver.get());
843 context->set_cert_verifier(globals->cert_verifier.get());
844 context->set_transport_security_state(
845 globals->transport_security_state.get());
846 context->set_cert_transparency_verifier(
847 globals->cert_transparency_verifier.get());
rsleevid6de8302016-06-21 01:33:20848 context->set_ct_policy_enforcer(globals->ct_policy_enforcer.get());
wjmacleanea309f72015-08-25 20:56:59849 context->set_ssl_config_service(globals->ssl_config_service.get());
850 context->set_http_auth_handler_factory(
851 globals->http_auth_handler_factory.get());
852 context->set_proxy_service(globals->system_proxy_service.get());
853
854 globals->system_url_request_job_factory.reset(
855 new net::URLRequestJobFactoryImpl());
856 context->set_job_factory(globals->system_url_request_job_factory.get());
857
858 context->set_cookie_store(globals->system_cookie_store.get());
859 context->set_channel_id_service(
860 globals->system_channel_id_service.get());
861 context->set_network_delegate(globals->system_network_delegate.get());
862 context->set_http_user_agent_settings(
863 globals->http_user_agent_settings.get());
864 context->set_network_quality_estimator(
865 globals->network_quality_estimator.get());
wjmacleanea309f72015-08-25 20:56:59866
bnc525e175a2016-06-20 12:36:40867 context->set_http_server_properties(globals->http_server_properties.get());
wjmacleanea309f72015-08-25 20:56:59868
maksim.sisov3d40c812016-05-02 13:27:16869 context->set_enable_brotli(globals->enable_brotli);
870
bnc18fb77f2016-04-04 20:54:04871 net::HttpNetworkSession::Params system_params(params);
wjmacleanea309f72015-08-25 20:56:59872 net::URLRequestContextBuilder::SetHttpNetworkSessionComponents(
873 context, &system_params);
874
mmenkee65e7af2015-10-13 17:16:42875 globals->system_http_network_session.reset(
876 new net::HttpNetworkSession(system_params));
wjmacleanea309f72015-08-25 20:56:59877 globals->system_http_transaction_factory.reset(
mmenkee65e7af2015-10-13 17:16:42878 new net::HttpNetworkLayer(globals->system_http_network_session.get()));
wjmacleanea309f72015-08-25 20:56:59879 context->set_http_transaction_factory(
880 globals->system_http_transaction_factory.get());
881
882 return context;
883}
884
bnc0f6bff62016-04-05 16:51:09885// static
bnce87c7c712016-08-01 23:59:48886void IOThread::ConfigureParamsFromFieldTrialsAndCommandLine(
887 const base::CommandLine& command_line,
888 bool is_quic_allowed_by_policy,
889 net::HttpNetworkSession::Params* params) {
890 std::string quic_user_agent_id = chrome::GetChannelString();
891 if (!quic_user_agent_id.empty())
892 quic_user_agent_id.push_back(' ');
893 quic_user_agent_id.append(
894 version_info::GetProductNameAndVersionForUserAgent());
895 quic_user_agent_id.push_back(' ');
896 quic_user_agent_id.append(content::BuildOSCpuInfo());
897
898 bool is_quic_force_disabled = !is_quic_allowed_by_policy ||
899 command_line.HasSwitch(switches::kDisableQuic);
900 bool is_quic_force_enabled = command_line.HasSwitch(switches::kEnableQuic);
901
902 network_session_configurator::ParseFieldTrials(is_quic_force_disabled,
903 is_quic_force_enabled,
904 quic_user_agent_id, params);
905
906 // Command line flags override field trials.
907 if (command_line.HasSwitch(switches::kIgnoreUrlFetcherCertRequests))
908 net::URLFetcher::SetIgnoreCertificateRequests(true);
909
910 if (command_line.HasSwitch(switches::kDisableHttp2))
911 params->enable_http2 = false;
912
913 if (command_line.HasSwitch(switches::kDisableQuicPortSelection)) {
914 params->enable_quic_port_selection = false;
915 } else if (command_line.HasSwitch(switches::kEnableQuicPortSelection)) {
916 params->enable_quic_port_selection = true;
917 }
918
919 if (params->enable_quic) {
920 if (command_line.HasSwitch(switches::kQuicConnectionOptions)) {
921 params->quic_connection_options =
922 net::QuicUtils::ParseQuicConnectionOptions(
923 command_line.GetSwitchValueASCII(
924 switches::kQuicConnectionOptions));
925 }
926
927 if (command_line.HasSwitch(switches::kQuicHostWhitelist)) {
928 std::string whitelist =
929 command_line.GetSwitchValueASCII(switches::kQuicHostWhitelist);
930 params->quic_host_whitelist.clear();
931 for (const std::string& host : base::SplitString(
932 whitelist, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) {
933 params->quic_host_whitelist.insert(host);
934 }
935 }
936
937 if (command_line.HasSwitch(switches::kQuicMaxPacketLength)) {
938 unsigned value;
939 if (base::StringToUint(
940 command_line.GetSwitchValueASCII(switches::kQuicMaxPacketLength),
941 &value)) {
942 params->quic_max_packet_length = value;
943 }
944 }
945
946 if (command_line.HasSwitch(switches::kQuicVersion)) {
947 net::QuicVersion version = network_session_configurator::ParseQuicVersion(
948 command_line.GetSwitchValueASCII(switches::kQuicVersion));
949 if (version != net::QUIC_VERSION_UNSUPPORTED) {
950 net::QuicVersionVector supported_versions;
951 supported_versions.push_back(version);
952 params->quic_supported_versions = supported_versions;
953 }
954 }
955
956 if (command_line.HasSwitch(switches::kOriginToForceQuicOn)) {
957 std::string origins =
958 command_line.GetSwitchValueASCII(switches::kOriginToForceQuicOn);
959 for (const std::string& host_port : base::SplitString(
960 origins, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) {
961 if (host_port == "*")
962 params->origins_to_force_quic_on.insert(net::HostPortPair());
963 net::HostPortPair quic_origin =
964 net::HostPortPair::FromString(host_port);
965 if (!quic_origin.IsEmpty())
966 params->origins_to_force_quic_on.insert(quic_origin);
967 }
968 }
969 }
970
971 // Parameters only controlled by command line.
972 if (command_line.HasSwitch(switches::kEnableUserAlternateProtocolPorts)) {
973 params->enable_user_alternate_protocol_ports = true;
974 }
975 if (command_line.HasSwitch(switches::kIgnoreCertificateErrors))
976 params->ignore_certificate_errors = true;
977 if (command_line.HasSwitch(switches::kTestingFixedHttpPort)) {
978 params->testing_fixed_http_port =
979 GetSwitchValueAsInt(command_line, switches::kTestingFixedHttpPort);
980 }
981 if (command_line.HasSwitch(switches::kTestingFixedHttpsPort)) {
982 params->testing_fixed_https_port =
983 GetSwitchValueAsInt(command_line, switches::kTestingFixedHttpsPort);
984 }
985}
986
987// static
wjmacleanea309f72015-08-25 20:56:59988net::URLRequestContext* IOThread::ConstructProxyScriptFetcherContext(
989 IOThread::Globals* globals,
bnc18fb77f2016-04-04 20:54:04990 const net::HttpNetworkSession::Params& params,
wjmacleanea309f72015-08-25 20:56:59991 net::NetLog* net_log) {
wjmacleanea309f72015-08-25 20:56:59992 net::URLRequestContext* context = new net::URLRequestContext;
993 context->set_net_log(net_log);
994 context->set_host_resolver(globals->host_resolver.get());
995 context->set_cert_verifier(globals->cert_verifier.get());
996 context->set_transport_security_state(
997 globals->transport_security_state.get());
998 context->set_cert_transparency_verifier(
999 globals->cert_transparency_verifier.get());
rsleevid6de8302016-06-21 01:33:201000 context->set_ct_policy_enforcer(globals->ct_policy_enforcer.get());
wjmacleanea309f72015-08-25 20:56:591001 context->set_ssl_config_service(globals->ssl_config_service.get());
1002 context->set_http_auth_handler_factory(
1003 globals->http_auth_handler_factory.get());
1004 context->set_proxy_service(globals->proxy_script_fetcher_proxy_service.get());
1005
1006 context->set_job_factory(
1007 globals->proxy_script_fetcher_url_request_job_factory.get());
1008
1009 context->set_cookie_store(globals->system_cookie_store.get());
1010 context->set_channel_id_service(
1011 globals->system_channel_id_service.get());
1012 context->set_network_delegate(globals->system_network_delegate.get());
1013 context->set_http_user_agent_settings(
1014 globals->http_user_agent_settings.get());
bnc525e175a2016-06-20 12:36:401015 context->set_http_server_properties(globals->http_server_properties.get());
wjmacleanea309f72015-08-25 20:56:591016
maksim.sisov3d40c812016-05-02 13:27:161017 context->set_enable_brotli(globals->enable_brotli);
1018
bnc18fb77f2016-04-04 20:54:041019 net::HttpNetworkSession::Params session_params(params);
wjmacleanea309f72015-08-25 20:56:591020 net::URLRequestContextBuilder::SetHttpNetworkSessionComponents(
1021 context, &session_params);
1022
mmenkee65e7af2015-10-13 17:16:421023 globals->proxy_script_fetcher_http_network_session.reset(
wjmacleanea309f72015-08-25 20:56:591024 new net::HttpNetworkSession(session_params));
mmenkee65e7af2015-10-13 17:16:421025 globals->proxy_script_fetcher_http_transaction_factory.reset(
1026 new net::HttpNetworkLayer(
1027 globals->proxy_script_fetcher_http_network_session.get()));
wjmacleanea309f72015-08-25 20:56:591028 context->set_http_transaction_factory(
1029 globals->proxy_script_fetcher_http_transaction_factory.get());
1030
dcheng4af48582016-04-19 00:29:351031 std::unique_ptr<net::URLRequestJobFactoryImpl> job_factory(
wjmacleanea309f72015-08-25 20:56:591032 new net::URLRequestJobFactoryImpl());
1033
1034 job_factory->SetProtocolHandler(
dcheng4af48582016-04-19 00:29:351035 url::kDataScheme, base::WrapUnique(new net::DataProtocolHandler()));
wjmacleanea309f72015-08-25 20:56:591036 job_factory->SetProtocolHandler(
1037 url::kFileScheme,
dcheng4af48582016-04-19 00:29:351038 base::WrapUnique(new net::FileProtocolHandler(
wjmacleanea309f72015-08-25 20:56:591039 content::BrowserThread::GetBlockingPool()
1040 ->GetTaskRunnerWithShutdownBehavior(
1041 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN))));
1042#if !defined(DISABLE_FTP_SUPPORT)
1043 globals->proxy_script_fetcher_ftp_transaction_factory.reset(
1044 new net::FtpNetworkLayer(globals->host_resolver.get()));
1045 job_factory->SetProtocolHandler(
1046 url::kFtpScheme,
dcheng4af48582016-04-19 00:29:351047 base::WrapUnique(new net::FtpProtocolHandler(
wjmacleanea309f72015-08-25 20:56:591048 globals->proxy_script_fetcher_ftp_transaction_factory.get())));
1049#endif
dchenge73d8520c2015-12-27 01:19:091050 globals->proxy_script_fetcher_url_request_job_factory =
1051 std::move(job_factory);
wjmacleanea309f72015-08-25 20:56:591052
1053 context->set_job_factory(
1054 globals->proxy_script_fetcher_url_request_job_factory.get());
1055
1056 // TODO(rtenneti): We should probably use HttpServerPropertiesManager for the
1057 // system URLRequestContext too. There's no reason this should be tied to a
1058 // profile.
1059 return context;
1060}
gayane0b46091c2016-04-07 21:01:051061
1062const metrics::UpdateUsagePrefCallbackType&
1063IOThread::GetMetricsDataUseForwarder() {
1064 return metrics_data_use_forwarder_;
1065}