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_
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"
29 class PrefProxyConfigTracker
;
31 class PrefRegistrySimple
;
32 class SystemURLRequestContextGetter
;
38 namespace chrome_browser_net
{
39 class DnsProbeService
;
42 namespace extensions
{
43 class EventRouterForwarder
;
47 class CertPolicyEnforcer
;
49 class ChannelIDService
;
52 class FtpTransactionFactory
;
53 class HostMappingRules
;
55 class HttpAuthHandlerFactory
;
56 class HttpServerProperties
;
57 class HttpTransactionFactory
;
58 class HttpUserAgentSettings
;
59 class NetworkDelegate
;
60 class NetworkQualityEstimator
;
61 class ProxyConfigService
;
63 class SSLConfigService
;
64 class TransportSecurityState
;
65 class URLRequestBackoffManager
;
66 class URLRequestContext
;
67 class URLRequestContextGetter
;
68 class URLRequestJobFactory
;
69 class URLSecurityManager
;
80 // Contains state associated with, initialized and cleaned up on, and
81 // primarily used on, the IO thread.
83 // If you are looking to interact with the IO thread (e.g. post tasks
84 // to it or check if it is the current thread), see
85 // content::BrowserThread.
86 class IOThread
: public content::BrowserThreadDelegate
{
92 Optional() : set_(false) {}
98 void CopyToIfSet(T
* value
) const {
109 class SystemRequestContextLeakChecker
{
111 explicit SystemRequestContextLeakChecker(Globals
* globals
);
112 ~SystemRequestContextLeakChecker();
115 Globals
* const globals_
;
121 // The "system" NetworkDelegate, used for Profile-agnostic network events.
122 scoped_ptr
<net::NetworkDelegate
> system_network_delegate
;
123 scoped_ptr
<net::HostResolver
> host_resolver
;
124 scoped_ptr
<net::CertVerifier
> cert_verifier
;
125 // The ChannelIDService must outlive the HttpTransactionFactory.
126 scoped_ptr
<net::ChannelIDService
> system_channel_id_service
;
127 // This TransportSecurityState doesn't load or save any state. It's only
128 // used to enforce pinning for system requests and will only use built-in
130 scoped_ptr
<net::TransportSecurityState
> transport_security_state
;
131 scoped_ptr
<net::CTVerifier
> cert_transparency_verifier
;
132 scoped_ptr
<net::CertPolicyEnforcer
> cert_policy_enforcer
;
133 scoped_refptr
<net::SSLConfigService
> ssl_config_service
;
134 scoped_ptr
<net::HttpAuthHandlerFactory
> http_auth_handler_factory
;
135 scoped_ptr
<net::HttpServerProperties
> http_server_properties
;
136 scoped_ptr
<net::ProxyService
> proxy_script_fetcher_proxy_service
;
137 scoped_ptr
<net::HttpTransactionFactory
>
138 proxy_script_fetcher_http_transaction_factory
;
139 scoped_ptr
<net::FtpTransactionFactory
>
140 proxy_script_fetcher_ftp_transaction_factory
;
141 scoped_ptr
<net::URLRequestJobFactory
>
142 proxy_script_fetcher_url_request_job_factory
;
143 scoped_ptr
<net::URLRequestBackoffManager
> url_request_backoff_manager
;
144 scoped_ptr
<net::URLSecurityManager
> url_security_manager
;
145 // TODO(willchan): Remove proxy script fetcher context since it's not
146 // necessary now that I got rid of refcounting URLRequestContexts.
148 // The first URLRequestContext is |system_url_request_context|. We introduce
149 // |proxy_script_fetcher_context| for the second context. It has a direct
150 // ProxyService, since we always directly connect to fetch the PAC script.
151 scoped_ptr
<net::URLRequestContext
> proxy_script_fetcher_context
;
152 scoped_ptr
<net::ProxyService
> system_proxy_service
;
153 scoped_ptr
<net::HttpTransactionFactory
> system_http_transaction_factory
;
154 scoped_ptr
<net::URLRequestJobFactory
> system_url_request_job_factory
;
155 scoped_ptr
<net::URLRequestContext
> system_request_context
;
156 SystemRequestContextLeakChecker system_request_context_leak_checker
;
157 // |system_cookie_store| and |system_channel_id_service| are shared
158 // between |proxy_script_fetcher_context| and |system_request_context|.
159 scoped_refptr
<net::CookieStore
> system_cookie_store
;
160 #if defined(ENABLE_EXTENSIONS)
161 scoped_refptr
<extensions::EventRouterForwarder
>
162 extension_event_router_forwarder
;
164 scoped_ptr
<net::HostMappingRules
> host_mapping_rules
;
165 scoped_ptr
<net::HttpUserAgentSettings
> http_user_agent_settings
;
166 scoped_ptr
<net::NetworkQualityEstimator
> network_quality_estimator
;
167 bool ignore_certificate_errors
;
168 uint16 testing_fixed_http_port
;
169 uint16 testing_fixed_https_port
;
170 Optional
<bool> enable_tcp_fast_open_for_ssl
;
172 Optional
<size_t> initial_max_spdy_concurrent_streams
;
173 Optional
<bool> enable_spdy_compression
;
174 Optional
<bool> enable_spdy_ping_based_connection_checking
;
175 Optional
<net::NextProto
> spdy_default_protocol
;
176 net::NextProtoVector next_protos
;
177 Optional
<std::string
> trusted_spdy_proxy
;
178 std::set
<net::HostPortPair
> forced_spdy_exclusions
;
179 Optional
<bool> use_alternate_protocols
;
180 Optional
<double> alternative_service_probability_threshold
;
182 Optional
<bool> enable_quic
;
183 Optional
<bool> enable_insecure_quic
;
184 Optional
<bool> enable_quic_for_proxies
;
185 Optional
<bool> enable_quic_port_selection
;
186 Optional
<bool> quic_always_require_handshake_confirmation
;
187 Optional
<bool> quic_disable_connection_pooling
;
188 Optional
<float> quic_load_server_info_timeout_srtt_multiplier
;
189 Optional
<bool> quic_enable_connection_racing
;
190 Optional
<bool> quic_enable_non_blocking_io
;
191 Optional
<bool> quic_disable_disk_cache
;
192 Optional
<bool> quic_prefer_aes
;
193 Optional
<int> quic_max_number_of_lossy_connections
;
194 Optional
<float> quic_packet_loss_threshold
;
195 Optional
<int> quic_socket_receive_buffer_size
;
196 Optional
<size_t> quic_max_packet_length
;
197 net::QuicTagVector quic_connection_options
;
198 Optional
<std::string
> quic_user_agent_id
;
199 Optional
<net::QuicVersionVector
> quic_supported_versions
;
200 Optional
<net::HostPortPair
> origin_to_force_quic_on
;
201 bool enable_user_alternate_protocol_ports
;
202 // NetErrorTabHelper uses |dns_probe_service| to send DNS probes when a
203 // main frame load fails with a DNS error in order to provide more useful
204 // information to the renderer so it can show a more specific error page.
205 scoped_ptr
<chrome_browser_net::DnsProbeService
> dns_probe_service
;
208 // |net_log| must either outlive the IOThread or be NULL.
209 IOThread(PrefService
* local_state
,
210 policy::PolicyService
* policy_service
,
211 ChromeNetLog
* net_log
,
212 extensions::EventRouterForwarder
* extension_event_router_forwarder
);
214 ~IOThread() override
;
216 static void RegisterPrefs(PrefRegistrySimple
* registry
);
218 // Can only be called on the IO thread.
221 // Allows overriding Globals in tests where IOThread::Init() and
222 // IOThread::CleanUp() are not called. This allows for injecting mocks into
223 // IOThread global objects.
224 void SetGlobalsForTesting(Globals
* globals
);
226 ChromeNetLog
* net_log();
228 // Handles changing to On The Record mode, discarding confidential data.
229 void ChangedToOnTheRecord();
231 // Returns a getter for the URLRequestContext. Only called on the UI thread.
232 net::URLRequestContextGetter
* system_url_request_context_getter();
234 // Clears the host cache. Intended to be used to prevent exposing recently
235 // visited sites on about:net-internals/#dns and about:dns pages. Must be
236 // called on the IO thread.
237 void ClearHostCache();
239 void InitializeNetworkSessionParams(net::HttpNetworkSession::Params
* params
);
241 base::TimeTicks
creation_time() const;
243 // Returns true if QUIC should be enabled for data reduction proxy, either as
244 // a result of a field trial or a command line flag.
245 static bool ShouldEnableQuicForDataReductionProxy();
248 // Map from name to value for all parameters associate with a field trial.
249 typedef std::map
<std::string
, std::string
> VariationParameters
;
251 // Provide SystemURLRequestContextGetter with access to
252 // InitSystemRequestContext().
253 friend class SystemURLRequestContextGetter
;
255 friend class test::IOThreadPeer
;
257 // BrowserThreadDelegate implementation, runs on the IO thread.
258 // This handles initialization and destruction of state that must
259 // live on the IO thread.
260 void Init() override
;
261 void CleanUp() override
;
263 // Initializes |params| based on the settings in |globals|.
264 static void InitializeNetworkSessionParamsFromGlobals(
265 const Globals
& globals
,
266 net::HttpNetworkSession::Params
* params
);
268 void InitializeNetworkOptions(const base::CommandLine
& parsed_command_line
);
270 // Sets up TCP FastOpen if enabled via field trials or via the command line.
271 void ConfigureTCPFastOpen(const base::CommandLine
& command_line
);
273 // Configures available SPDY protocol versions in |globals| based on the flags
274 // in |command_lin| as well as SPDY field trial group and parameters. Must be
275 // called after ConfigureQuicGlobals.
276 static void ConfigureSpdyGlobals(const base::CommandLine
& command_line
,
277 base::StringPiece quic_trial_group
,
278 const VariationParameters
& quic_trial_params
,
281 // Global state must be initialized on the IO thread, then this
282 // method must be invoked on the UI thread.
283 void InitSystemRequestContext();
285 // Lazy initialization of system request context for
286 // SystemURLRequestContextGetter. To be called on IO thread only
287 // after global state has been initialized on the IO thread, and
288 // SystemRequestContext state has been initialized on the UI thread.
289 void InitSystemRequestContextOnIOThread();
291 net::HttpAuthHandlerFactory
* CreateDefaultAuthHandlerFactory(
292 net::HostResolver
* resolver
);
294 // Returns an SSLConfigService instance.
295 net::SSLConfigService
* GetSSLConfigService();
297 void ChangedToOnTheRecordOnIOThread();
299 void UpdateDnsClientEnabled();
301 // Configures QUIC options based on the flags in |command_line| as
302 // well as the QUIC field trial group.
303 void ConfigureQuic(const base::CommandLine
& command_line
);
305 extensions::EventRouterForwarder
* extension_event_router_forwarder() {
306 #if defined(ENABLE_EXTENSIONS)
307 return extension_event_router_forwarder_
;
312 // Configures QUIC options in |globals| based on the flags in |command_line|
313 // as well as the QUIC field trial group and parameters. Must be called
314 // before ConfigureSpdyGlobals.
315 static void ConfigureQuicGlobals(
316 const base::CommandLine
& command_line
,
317 base::StringPiece quic_trial_group
,
318 const VariationParameters
& quic_trial_params
,
319 bool quic_allowed_by_policy
,
322 // Returns true if QUIC should be enabled, either as a result
323 // of a field trial or a command line flag.
324 static bool ShouldEnableQuic(
325 const base::CommandLine
& command_line
,
326 base::StringPiece quic_trial_group
,
327 bool quic_allowed_by_policy
);
329 // Returns true if QUIC should be enabled for proxies, either as a result
330 // of a field trial or a command line flag.
331 static bool ShouldEnableQuicForProxies(
332 const base::CommandLine
& command_line
,
333 base::StringPiece quic_trial_group
,
334 bool quic_allowed_by_policy
);
336 // Returns true if QUIC should be enabled for http:// URLs, as a result
337 // of a field trial or command line flag.
338 static bool ShouldEnableInsecureQuic(
339 const base::CommandLine
& command_line
,
340 const VariationParameters
& quic_trial_params
);
342 // Returns true if the selection of the ephemeral port in bind() should be
343 // performed by Chromium, and false if the OS should select the port. The OS
344 // option is used to prevent Windows from posting a security security warning
346 static bool ShouldEnableQuicPortSelection(
347 const base::CommandLine
& command_line
);
349 // Returns true if QUIC should always require handshake confirmation during
350 // the QUIC handshake.
351 static bool ShouldQuicAlwaysRequireHandshakeConfirmation(
352 const VariationParameters
& quic_trial_params
);
354 // Returns true if QUIC should disable connection pooling.
355 static bool ShouldQuicDisableConnectionPooling(
356 const VariationParameters
& quic_trial_params
);
358 // Returns the ratio of time to load QUIC sever information from disk cache to
359 // 'smoothed RTT' based on field trial. Returns 0 if there is an error parsing
360 // the field trial params, or if the default value should be used.
361 static float GetQuicLoadServerInfoTimeoutSrttMultiplier(
362 const VariationParameters
& quic_trial_params
);
364 // Returns true if QUIC's connection racing should be enabled.
365 static bool ShouldQuicEnableConnectionRacing(
366 const VariationParameters
& quic_trial_params
);
368 // Returns true if QUIC's should use non-blocking IO.
369 static bool ShouldQuicEnableNonBlockingIO(
370 const VariationParameters
& quic_trial_params
);
372 // Returns true if QUIC shouldn't load QUIC server information from the disk
374 static bool ShouldQuicDisableDiskCache(
375 const VariationParameters
& quic_trial_params
);
377 // Returns true if QUIC should prefer AES-GCN even without hardware support.
378 static bool ShouldQuicPreferAes(const VariationParameters
& quic_trial_params
);
380 // Returns the maximum number of QUIC connections with high packet loss in a
381 // row after which QUIC should be disabled. Returns 0 if the default value
383 static int GetQuicMaxNumberOfLossyConnections(
384 const VariationParameters
& quic_trial_params
);
386 // Returns the packet loss rate in fraction after which a QUIC connection is
387 // closed and is considered as a lossy connection. Returns 0 if the default
388 // value should be used.
389 static float GetQuicPacketLossThreshold(
390 const VariationParameters
& quic_trial_params
);
392 // Returns the size of the QUIC receive buffer to use, or 0 if
393 // the default should be used.
394 static int GetQuicSocketReceiveBufferSize(
395 const VariationParameters
& quic_trial_params
);
397 // Returns the maximum length for QUIC packets, based on any flags in
398 // |command_line| or the field trial. Returns 0 if there is an error
399 // parsing any of the options, or if the default value should be used.
400 static size_t GetQuicMaxPacketLength(
401 const base::CommandLine
& command_line
,
402 const VariationParameters
& quic_trial_params
);
404 // Returns the QUIC versions specified by any flags in |command_line|
405 // or |quic_trial_params|.
406 static net::QuicVersion
GetQuicVersion(
407 const base::CommandLine
& command_line
,
408 const VariationParameters
& quic_trial_params
);
410 // Returns the QUIC version specified by |quic_version| or
411 // QUIC_VERSION_UNSUPPORTED if |quic_version| is invalid.
412 static net::QuicVersion
ParseQuicVersion(const std::string
& quic_version
);
414 // Returns the QUIC connection options specified by any flags in
415 // |command_line| or |quic_trial_params|.
416 static net::QuicTagVector
GetQuicConnectionOptions(
417 const base::CommandLine
& command_line
,
418 const VariationParameters
& quic_trial_params
);
420 // Returns the alternative service probability threshold specified by
421 // any flags in |command_line| or |quic_trial_params|.
422 static double GetAlternativeProtocolProbabilityThreshold(
423 const base::CommandLine
& command_line
,
424 const VariationParameters
& quic_trial_params
);
426 // The NetLog is owned by the browser process, to allow logging from other
427 // threads during shutdown, but is used most frequently on the IOThread.
428 ChromeNetLog
* net_log_
;
430 #if defined(ENABLE_EXTENSIONS)
431 // The extensions::EventRouterForwarder allows for sending events to
432 // extensions from the IOThread.
433 extensions::EventRouterForwarder
* extension_event_router_forwarder_
;
436 // These member variables are basically global, but their lifetimes are tied
437 // to the IOThread. IOThread owns them all, despite not using scoped_ptr.
438 // This is because the destructor of IOThread runs on the wrong thread. All
439 // member variables should be deleted in CleanUp().
441 // These member variables are initialized in Init() and do not change for the
442 // lifetime of the IO thread.
446 // Observer that logs network changes to the ChromeNetLog.
447 class LoggingNetworkChangeObserver
;
448 scoped_ptr
<LoggingNetworkChangeObserver
> network_change_observer_
;
450 BooleanPrefMember system_enable_referrers_
;
452 BooleanPrefMember dns_client_enabled_
;
454 BooleanPrefMember quick_check_enabled_
;
456 // Store HTTP Auth-related policies in this thread.
457 std::string auth_schemes_
;
458 bool negotiate_disable_cname_lookup_
;
459 bool negotiate_enable_port_
;
460 std::string auth_server_whitelist_
;
461 std::string auth_delegate_whitelist_
;
462 std::string gssapi_library_name_
;
463 std::string auth_android_negotiate_account_type_
;
465 // This is an instance of the default SSLConfigServiceManager for the current
466 // platform and it gets SSL preferences from local_state object.
467 scoped_ptr
<SSLConfigServiceManager
> ssl_config_service_manager_
;
469 // These member variables are initialized by a task posted to the IO thread,
470 // which gets posted by calling certain member functions of IOThread.
471 scoped_ptr
<net::ProxyConfigService
> system_proxy_config_service_
;
473 scoped_ptr
<PrefProxyConfigTracker
> pref_proxy_config_tracker_
;
475 scoped_refptr
<net::URLRequestContextGetter
>
476 system_url_request_context_getter_
;
478 // True if SPDY is disabled by policy.
479 bool is_spdy_disabled_by_policy_
;
481 // True if QUIC is allowed by policy.
482 bool is_quic_allowed_by_policy_
;
484 const base::TimeTicks creation_time_
;
486 base::WeakPtrFactory
<IOThread
> weak_factory_
;
488 DISALLOW_COPY_AND_ASSIGN(IOThread
);
491 #endif // CHROME_BROWSER_IO_THREAD_H_