No dual_mode on Win10+ shortcuts.
[chromium-blink-merge.git] / chrome / browser / io_thread.h
blob54a90ff04ef0db4bf02b59d11ef894f0b26ce792
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_IO_THREAD_H_
6 #define CHROME_BROWSER_IO_THREAD_H_
8 #include <map>
9 #include <string>
10 #include <vector>
12 #include "base/basictypes.h"
13 #include "base/compiler_specific.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/prefs/pref_member.h"
18 #include "base/strings/string_piece.h"
19 #include "base/time/time.h"
20 #include "chrome/browser/net/chrome_network_delegate.h"
21 #include "chrome/browser/net/ssl_config_service_manager.h"
22 #include "content/public/browser/browser_thread.h"
23 #include "content/public/browser/browser_thread_delegate.h"
24 #include "net/base/network_change_notifier.h"
25 #include "net/http/http_network_session.h"
26 #include "net/socket/next_proto.h"
28 class ChromeNetLog;
29 class PrefProxyConfigTracker;
30 class PrefService;
31 class PrefRegistrySimple;
32 class SystemURLRequestContextGetter;
34 namespace base {
35 class CommandLine;
38 namespace chrome_browser_net {
39 class DnsProbeService;
42 namespace extensions {
43 class EventRouterForwarder;
46 namespace net {
47 class CertPolicyEnforcer;
48 class CertVerifier;
49 class ChannelIDService;
50 class CookieStore;
51 class CTVerifier;
52 class FtpTransactionFactory;
53 class HostMappingRules;
54 class HostResolver;
55 class HttpAuthHandlerFactory;
56 class HttpServerProperties;
57 class HttpTransactionFactory;
58 class HttpUserAgentSettings;
59 class NetworkDelegate;
60 class NetworkQualityEstimator;
61 class ProxyConfigService;
62 class ProxyService;
63 class SSLConfigService;
64 class TransportSecurityState;
65 class URLRequestContext;
66 class URLRequestContextGetter;
67 class URLRequestJobFactory;
68 class URLSecurityManager;
69 } // namespace net
71 namespace policy {
72 class PolicyService;
73 } // namespace policy
75 namespace test {
76 class IOThreadPeer;
77 } // namespace test
79 // Contains state associated with, initialized and cleaned up on, and
80 // primarily used on, the IO thread.
82 // If you are looking to interact with the IO thread (e.g. post tasks
83 // to it or check if it is the current thread), see
84 // content::BrowserThread.
85 class IOThread : public content::BrowserThreadDelegate {
86 public:
87 struct Globals {
88 template <typename T>
89 class Optional {
90 public:
91 Optional() : set_(false) {}
93 void set(T value) {
94 set_ = true;
95 value_ = value;
97 void CopyToIfSet(T* value) const {
98 if (set_) {
99 *value = value_;
103 private:
104 bool set_;
105 T value_;
108 class SystemRequestContextLeakChecker {
109 public:
110 explicit SystemRequestContextLeakChecker(Globals* globals);
111 ~SystemRequestContextLeakChecker();
113 private:
114 Globals* const globals_;
117 Globals();
118 ~Globals();
120 // The "system" NetworkDelegate, used for Profile-agnostic network events.
121 scoped_ptr<net::NetworkDelegate> system_network_delegate;
122 scoped_ptr<net::HostResolver> host_resolver;
123 scoped_ptr<net::CertVerifier> cert_verifier;
124 // The ChannelIDService must outlive the HttpTransactionFactory.
125 scoped_ptr<net::ChannelIDService> system_channel_id_service;
126 // This TransportSecurityState doesn't load or save any state. It's only
127 // used to enforce pinning for system requests and will only use built-in
128 // pins.
129 scoped_ptr<net::TransportSecurityState> transport_security_state;
130 scoped_ptr<net::CTVerifier> cert_transparency_verifier;
131 scoped_ptr<net::CertPolicyEnforcer> cert_policy_enforcer;
132 scoped_refptr<net::SSLConfigService> ssl_config_service;
133 scoped_ptr<net::HttpAuthHandlerFactory> http_auth_handler_factory;
134 scoped_ptr<net::HttpServerProperties> http_server_properties;
135 scoped_ptr<net::ProxyService> proxy_script_fetcher_proxy_service;
136 scoped_ptr<net::HttpTransactionFactory>
137 proxy_script_fetcher_http_transaction_factory;
138 scoped_ptr<net::FtpTransactionFactory>
139 proxy_script_fetcher_ftp_transaction_factory;
140 scoped_ptr<net::URLRequestJobFactory>
141 proxy_script_fetcher_url_request_job_factory;
142 scoped_ptr<net::URLSecurityManager> url_security_manager;
143 // TODO(willchan): Remove proxy script fetcher context since it's not
144 // necessary now that I got rid of refcounting URLRequestContexts.
146 // The first URLRequestContext is |system_url_request_context|. We introduce
147 // |proxy_script_fetcher_context| for the second context. It has a direct
148 // ProxyService, since we always directly connect to fetch the PAC script.
149 scoped_ptr<net::URLRequestContext> proxy_script_fetcher_context;
150 scoped_ptr<net::ProxyService> system_proxy_service;
151 scoped_ptr<net::HttpTransactionFactory> system_http_transaction_factory;
152 scoped_ptr<net::URLRequestJobFactory> system_url_request_job_factory;
153 scoped_ptr<net::URLRequestContext> system_request_context;
154 SystemRequestContextLeakChecker system_request_context_leak_checker;
155 // |system_cookie_store| and |system_channel_id_service| are shared
156 // between |proxy_script_fetcher_context| and |system_request_context|.
157 scoped_refptr<net::CookieStore> system_cookie_store;
158 #if defined(ENABLE_EXTENSIONS)
159 scoped_refptr<extensions::EventRouterForwarder>
160 extension_event_router_forwarder;
161 #endif
162 scoped_ptr<net::HostMappingRules> host_mapping_rules;
163 scoped_ptr<net::HttpUserAgentSettings> http_user_agent_settings;
164 scoped_ptr<net::NetworkQualityEstimator> network_quality_estimator;
165 bool ignore_certificate_errors;
166 uint16 testing_fixed_http_port;
167 uint16 testing_fixed_https_port;
168 Optional<bool> enable_tcp_fast_open_for_ssl;
170 Optional<size_t> initial_max_spdy_concurrent_streams;
171 Optional<bool> enable_spdy_compression;
172 Optional<bool> enable_spdy_ping_based_connection_checking;
173 Optional<net::NextProto> spdy_default_protocol;
174 net::NextProtoVector next_protos;
175 Optional<std::string> trusted_spdy_proxy;
176 std::set<net::HostPortPair> forced_spdy_exclusions;
177 Optional<bool> use_alternate_protocols;
178 Optional<double> alternative_service_probability_threshold;
180 Optional<bool> enable_quic;
181 Optional<bool> disable_insecure_quic;
182 Optional<bool> enable_quic_for_proxies;
183 Optional<bool> enable_quic_port_selection;
184 Optional<bool> quic_always_require_handshake_confirmation;
185 Optional<bool> quic_disable_connection_pooling;
186 Optional<float> quic_load_server_info_timeout_srtt_multiplier;
187 Optional<bool> quic_enable_connection_racing;
188 Optional<bool> quic_enable_non_blocking_io;
189 Optional<bool> quic_disable_disk_cache;
190 Optional<bool> quic_prefer_aes;
191 Optional<int> quic_max_number_of_lossy_connections;
192 Optional<float> quic_packet_loss_threshold;
193 Optional<int> quic_socket_receive_buffer_size;
194 Optional<size_t> quic_max_packet_length;
195 net::QuicTagVector quic_connection_options;
196 Optional<std::string> quic_user_agent_id;
197 Optional<net::QuicVersionVector> quic_supported_versions;
198 Optional<net::HostPortPair> origin_to_force_quic_on;
199 bool enable_user_alternate_protocol_ports;
200 // NetErrorTabHelper uses |dns_probe_service| to send DNS probes when a
201 // main frame load fails with a DNS error in order to provide more useful
202 // information to the renderer so it can show a more specific error page.
203 scoped_ptr<chrome_browser_net::DnsProbeService> dns_probe_service;
206 // |net_log| must either outlive the IOThread or be NULL.
207 IOThread(PrefService* local_state,
208 policy::PolicyService* policy_service,
209 ChromeNetLog* net_log,
210 extensions::EventRouterForwarder* extension_event_router_forwarder);
212 ~IOThread() override;
214 static void RegisterPrefs(PrefRegistrySimple* registry);
216 // Can only be called on the IO thread.
217 Globals* globals();
219 // Allows overriding Globals in tests where IOThread::Init() and
220 // IOThread::CleanUp() are not called. This allows for injecting mocks into
221 // IOThread global objects.
222 void SetGlobalsForTesting(Globals* globals);
224 ChromeNetLog* net_log();
226 // Handles changing to On The Record mode, discarding confidential data.
227 void ChangedToOnTheRecord();
229 // Returns a getter for the URLRequestContext. Only called on the UI thread.
230 net::URLRequestContextGetter* system_url_request_context_getter();
232 // Clears the host cache. Intended to be used to prevent exposing recently
233 // visited sites on about:net-internals/#dns and about:dns pages. Must be
234 // called on the IO thread.
235 void ClearHostCache();
237 void InitializeNetworkSessionParams(net::HttpNetworkSession::Params* params);
239 base::TimeTicks creation_time() const;
241 // Returns true if QUIC should be enabled for data reduction proxy, either as
242 // a result of a field trial or a command line flag.
243 static bool ShouldEnableQuicForDataReductionProxy();
245 private:
246 // Map from name to value for all parameters associate with a field trial.
247 typedef std::map<std::string, std::string> VariationParameters;
249 // Provide SystemURLRequestContextGetter with access to
250 // InitSystemRequestContext().
251 friend class SystemURLRequestContextGetter;
253 friend class test::IOThreadPeer;
255 // BrowserThreadDelegate implementation, runs on the IO thread.
256 // This handles initialization and destruction of state that must
257 // live on the IO thread.
258 void Init() override;
259 void CleanUp() override;
261 // Initializes |params| based on the settings in |globals|.
262 static void InitializeNetworkSessionParamsFromGlobals(
263 const Globals& globals,
264 net::HttpNetworkSession::Params* params);
266 void InitializeNetworkOptions(const base::CommandLine& parsed_command_line);
268 // Sets up TCP FastOpen if enabled via field trials or via the command line.
269 void ConfigureTCPFastOpen(const base::CommandLine& command_line);
271 // Configures available SPDY protocol versions in |globals| based on the flags
272 // in |command_lin| as well as SPDY field trial group and parameters. Must be
273 // called after ConfigureQuicGlobals.
274 static void ConfigureSpdyGlobals(const base::CommandLine& command_line,
275 base::StringPiece quic_trial_group,
276 const VariationParameters& quic_trial_params,
277 Globals* globals);
279 // Global state must be initialized on the IO thread, then this
280 // method must be invoked on the UI thread.
281 void InitSystemRequestContext();
283 // Lazy initialization of system request context for
284 // SystemURLRequestContextGetter. To be called on IO thread only
285 // after global state has been initialized on the IO thread, and
286 // SystemRequestContext state has been initialized on the UI thread.
287 void InitSystemRequestContextOnIOThread();
289 net::HttpAuthHandlerFactory* CreateDefaultAuthHandlerFactory(
290 net::HostResolver* resolver);
292 // Returns an SSLConfigService instance.
293 net::SSLConfigService* GetSSLConfigService();
295 void ChangedToOnTheRecordOnIOThread();
297 void UpdateDnsClientEnabled();
299 // Configures QUIC options based on the flags in |command_line| as
300 // well as the QUIC field trial group.
301 void ConfigureQuic(const base::CommandLine& command_line);
303 extensions::EventRouterForwarder* extension_event_router_forwarder() {
304 #if defined(ENABLE_EXTENSIONS)
305 return extension_event_router_forwarder_;
306 #else
307 return NULL;
308 #endif
310 // Configures QUIC options in |globals| based on the flags in |command_line|
311 // as well as the QUIC field trial group and parameters. Must be called
312 // before ConfigureSpdyGlobals.
313 static void ConfigureQuicGlobals(
314 const base::CommandLine& command_line,
315 base::StringPiece quic_trial_group,
316 const VariationParameters& quic_trial_params,
317 bool quic_allowed_by_policy,
318 Globals* globals);
320 // Returns true if QUIC should be enabled, either as a result
321 // of a field trial or a command line flag.
322 static bool ShouldEnableQuic(
323 const base::CommandLine& command_line,
324 base::StringPiece quic_trial_group,
325 bool quic_allowed_by_policy);
327 // Returns true if QUIC should be enabled for proxies, either as a result
328 // of a field trial or a command line flag.
329 static bool ShouldEnableQuicForProxies(
330 const base::CommandLine& command_line,
331 base::StringPiece quic_trial_group,
332 bool quic_allowed_by_policy);
334 // Returns true if QUIC should be disabled for http:// URLs, as a result
335 // of a field trial.
336 static bool ShouldDisableInsecureQuic(
337 const VariationParameters& quic_trial_params);
339 // Returns true if the selection of the ephemeral port in bind() should be
340 // performed by Chromium, and false if the OS should select the port. The OS
341 // option is used to prevent Windows from posting a security security warning
342 // dialog.
343 static bool ShouldEnableQuicPortSelection(
344 const base::CommandLine& command_line);
346 // Returns true if QUIC should always require handshake confirmation during
347 // the QUIC handshake.
348 static bool ShouldQuicAlwaysRequireHandshakeConfirmation(
349 const VariationParameters& quic_trial_params);
351 // Returns true if QUIC should disable connection pooling.
352 static bool ShouldQuicDisableConnectionPooling(
353 const VariationParameters& quic_trial_params);
355 // Returns the ratio of time to load QUIC sever information from disk cache to
356 // 'smoothed RTT' based on field trial. Returns 0 if there is an error parsing
357 // the field trial params, or if the default value should be used.
358 static float GetQuicLoadServerInfoTimeoutSrttMultiplier(
359 const VariationParameters& quic_trial_params);
361 // Returns true if QUIC's connection racing should be enabled.
362 static bool ShouldQuicEnableConnectionRacing(
363 const VariationParameters& quic_trial_params);
365 // Returns true if QUIC's should use non-blocking IO.
366 static bool ShouldQuicEnableNonBlockingIO(
367 const VariationParameters& quic_trial_params);
369 // Returns true if QUIC shouldn't load QUIC server information from the disk
370 // cache.
371 static bool ShouldQuicDisableDiskCache(
372 const VariationParameters& quic_trial_params);
374 // Returns true if QUIC should prefer AES-GCN even without hardware support.
375 static bool ShouldQuicPreferAes(const VariationParameters& quic_trial_params);
377 // Returns the maximum number of QUIC connections with high packet loss in a
378 // row after which QUIC should be disabled. Returns 0 if the default value
379 // should be used.
380 static int GetQuicMaxNumberOfLossyConnections(
381 const VariationParameters& quic_trial_params);
383 // Returns the packet loss rate in fraction after which a QUIC connection is
384 // closed and is considered as a lossy connection. Returns 0 if the default
385 // value should be used.
386 static float GetQuicPacketLossThreshold(
387 const VariationParameters& quic_trial_params);
389 // Returns the size of the QUIC receive buffer to use, or 0 if
390 // the default should be used.
391 static int GetQuicSocketReceiveBufferSize(
392 const VariationParameters& quic_trial_params);
394 // Returns the maximum length for QUIC packets, based on any flags in
395 // |command_line| or the field trial. Returns 0 if there is an error
396 // parsing any of the options, or if the default value should be used.
397 static size_t GetQuicMaxPacketLength(
398 const base::CommandLine& command_line,
399 const VariationParameters& quic_trial_params);
401 // Returns the QUIC versions specified by any flags in |command_line|
402 // or |quic_trial_params|.
403 static net::QuicVersion GetQuicVersion(
404 const base::CommandLine& command_line,
405 const VariationParameters& quic_trial_params);
407 // Returns the QUIC version specified by |quic_version| or
408 // QUIC_VERSION_UNSUPPORTED if |quic_version| is invalid.
409 static net::QuicVersion ParseQuicVersion(const std::string& quic_version);
411 // Returns the QUIC connection options specified by any flags in
412 // |command_line| or |quic_trial_params|.
413 static net::QuicTagVector GetQuicConnectionOptions(
414 const base::CommandLine& command_line,
415 const VariationParameters& quic_trial_params);
417 // Returns the alternative service probability threshold specified by
418 // any flags in |command_line| or |quic_trial_params|.
419 static double GetAlternativeProtocolProbabilityThreshold(
420 const base::CommandLine& command_line,
421 const VariationParameters& quic_trial_params);
423 // The NetLog is owned by the browser process, to allow logging from other
424 // threads during shutdown, but is used most frequently on the IOThread.
425 ChromeNetLog* net_log_;
427 #if defined(ENABLE_EXTENSIONS)
428 // The extensions::EventRouterForwarder allows for sending events to
429 // extensions from the IOThread.
430 extensions::EventRouterForwarder* extension_event_router_forwarder_;
431 #endif
433 // These member variables are basically global, but their lifetimes are tied
434 // to the IOThread. IOThread owns them all, despite not using scoped_ptr.
435 // This is because the destructor of IOThread runs on the wrong thread. All
436 // member variables should be deleted in CleanUp().
438 // These member variables are initialized in Init() and do not change for the
439 // lifetime of the IO thread.
441 Globals* globals_;
443 // Observer that logs network changes to the ChromeNetLog.
444 class LoggingNetworkChangeObserver;
445 scoped_ptr<LoggingNetworkChangeObserver> network_change_observer_;
447 BooleanPrefMember system_enable_referrers_;
449 BooleanPrefMember dns_client_enabled_;
451 BooleanPrefMember quick_check_enabled_;
453 // Store HTTP Auth-related policies in this thread.
454 std::string auth_schemes_;
455 bool negotiate_disable_cname_lookup_;
456 bool negotiate_enable_port_;
457 std::string auth_server_whitelist_;
458 std::string auth_delegate_whitelist_;
459 std::string gssapi_library_name_;
460 std::string auth_android_negotiate_account_type_;
462 // This is an instance of the default SSLConfigServiceManager for the current
463 // platform and it gets SSL preferences from local_state object.
464 scoped_ptr<SSLConfigServiceManager> ssl_config_service_manager_;
466 // These member variables are initialized by a task posted to the IO thread,
467 // which gets posted by calling certain member functions of IOThread.
468 scoped_ptr<net::ProxyConfigService> system_proxy_config_service_;
470 scoped_ptr<PrefProxyConfigTracker> pref_proxy_config_tracker_;
472 scoped_refptr<net::URLRequestContextGetter>
473 system_url_request_context_getter_;
475 // True if SPDY is disabled by policy.
476 bool is_spdy_disabled_by_policy_;
478 // True if QUIC is allowed by policy.
479 bool is_quic_allowed_by_policy_;
481 const base::TimeTicks creation_time_;
483 base::WeakPtrFactory<IOThread> weak_factory_;
485 DISALLOW_COPY_AND_ASSIGN(IOThread);
488 #endif // CHROME_BROWSER_IO_THREAD_H_