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_
13 #include "base/basictypes.h"
14 #include "base/compiler_specific.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/memory/weak_ptr.h"
18 #include "base/prefs/pref_member.h"
19 #include "base/strings/string_piece.h"
20 #include "base/time/time.h"
21 #include "chrome/browser/net/chrome_network_delegate.h"
22 #include "chrome/browser/net/ssl_config_service_manager.h"
23 #include "content/public/browser/browser_thread.h"
24 #include "content/public/browser/browser_thread_delegate.h"
25 #include "net/base/network_change_notifier.h"
26 #include "net/http/http_network_session.h"
27 #include "net/socket/next_proto.h"
30 class PrefProxyConfigTracker
;
32 class PrefRegistrySimple
;
33 class SystemURLRequestContextGetter
;
39 namespace chrome_browser_net
{
40 class DnsProbeService
;
43 namespace extensions
{
44 class EventRouterForwarder
;
48 class CertPolicyEnforcer
;
50 class ChannelIDService
;
53 class FtpTransactionFactory
;
54 class HostMappingRules
;
56 class HttpAuthHandlerFactory
;
57 class HttpServerProperties
;
58 class HttpTransactionFactory
;
59 class HttpUserAgentSettings
;
60 class NetworkDelegate
;
61 class NetworkQualityEstimator
;
62 class ProxyConfigService
;
64 class SSLConfigService
;
65 class TransportSecurityState
;
66 class URLRequestBackoffManager
;
67 class URLRequestContext
;
68 class URLRequestContextGetter
;
69 class URLRequestJobFactory
;
70 class URLSecurityManager
;
81 // Contains state associated with, initialized and cleaned up on, and
82 // primarily used on, the IO thread.
84 // If you are looking to interact with the IO thread (e.g. post tasks
85 // to it or check if it is the current thread), see
86 // content::BrowserThread.
87 class IOThread
: public content::BrowserThreadDelegate
{
93 Optional() : set_(false) {}
99 void CopyToIfSet(T
* value
) const {
110 class SystemRequestContextLeakChecker
{
112 explicit SystemRequestContextLeakChecker(Globals
* globals
);
113 ~SystemRequestContextLeakChecker();
116 Globals
* const globals_
;
122 // The "system" NetworkDelegate, used for Profile-agnostic network events.
123 scoped_ptr
<net::NetworkDelegate
> system_network_delegate
;
124 scoped_ptr
<net::HostResolver
> host_resolver
;
125 scoped_ptr
<net::CertVerifier
> cert_verifier
;
126 // The ChannelIDService must outlive the HttpTransactionFactory.
127 scoped_ptr
<net::ChannelIDService
> system_channel_id_service
;
128 // This TransportSecurityState doesn't load or save any state. It's only
129 // used to enforce pinning for system requests and will only use built-in
131 scoped_ptr
<net::TransportSecurityState
> transport_security_state
;
132 scoped_ptr
<net::CTVerifier
> cert_transparency_verifier
;
133 scoped_ptr
<net::CertPolicyEnforcer
> cert_policy_enforcer
;
134 scoped_refptr
<net::SSLConfigService
> ssl_config_service
;
135 scoped_ptr
<net::HttpAuthHandlerFactory
> http_auth_handler_factory
;
136 scoped_ptr
<net::HttpServerProperties
> http_server_properties
;
137 scoped_ptr
<net::ProxyService
> proxy_script_fetcher_proxy_service
;
138 scoped_ptr
<net::HttpTransactionFactory
>
139 proxy_script_fetcher_http_transaction_factory
;
140 scoped_ptr
<net::FtpTransactionFactory
>
141 proxy_script_fetcher_ftp_transaction_factory
;
142 scoped_ptr
<net::URLRequestJobFactory
>
143 proxy_script_fetcher_url_request_job_factory
;
144 scoped_ptr
<net::URLRequestBackoffManager
> url_request_backoff_manager
;
145 scoped_ptr
<net::URLSecurityManager
> url_security_manager
;
146 // TODO(willchan): Remove proxy script fetcher context since it's not
147 // necessary now that I got rid of refcounting URLRequestContexts.
149 // The first URLRequestContext is |system_url_request_context|. We introduce
150 // |proxy_script_fetcher_context| for the second context. It has a direct
151 // ProxyService, since we always directly connect to fetch the PAC script.
152 scoped_ptr
<net::URLRequestContext
> proxy_script_fetcher_context
;
153 scoped_ptr
<net::ProxyService
> system_proxy_service
;
154 scoped_ptr
<net::HttpTransactionFactory
> system_http_transaction_factory
;
155 scoped_ptr
<net::URLRequestJobFactory
> system_url_request_job_factory
;
156 scoped_ptr
<net::URLRequestContext
> system_request_context
;
157 SystemRequestContextLeakChecker system_request_context_leak_checker
;
158 // |system_cookie_store| and |system_channel_id_service| are shared
159 // between |proxy_script_fetcher_context| and |system_request_context|.
160 scoped_refptr
<net::CookieStore
> system_cookie_store
;
161 #if defined(ENABLE_EXTENSIONS)
162 scoped_refptr
<extensions::EventRouterForwarder
>
163 extension_event_router_forwarder
;
165 scoped_ptr
<net::HostMappingRules
> host_mapping_rules
;
166 scoped_ptr
<net::HttpUserAgentSettings
> http_user_agent_settings
;
167 scoped_ptr
<net::NetworkQualityEstimator
> network_quality_estimator
;
168 bool ignore_certificate_errors
;
169 uint16 testing_fixed_http_port
;
170 uint16 testing_fixed_https_port
;
171 Optional
<bool> enable_tcp_fast_open_for_ssl
;
173 Optional
<size_t> initial_max_spdy_concurrent_streams
;
174 Optional
<bool> enable_spdy_compression
;
175 Optional
<bool> enable_spdy_ping_based_connection_checking
;
176 Optional
<net::NextProto
> spdy_default_protocol
;
177 net::NextProtoVector next_protos
;
178 Optional
<std::string
> trusted_spdy_proxy
;
179 std::set
<net::HostPortPair
> forced_spdy_exclusions
;
180 Optional
<bool> use_alternative_services
;
181 Optional
<double> alternative_service_probability_threshold
;
183 Optional
<bool> enable_quic
;
184 Optional
<bool> enable_insecure_quic
;
185 Optional
<bool> enable_quic_for_proxies
;
186 Optional
<bool> enable_quic_port_selection
;
187 Optional
<bool> quic_always_require_handshake_confirmation
;
188 Optional
<bool> quic_disable_connection_pooling
;
189 Optional
<float> quic_load_server_info_timeout_srtt_multiplier
;
190 Optional
<bool> quic_enable_connection_racing
;
191 Optional
<bool> quic_enable_non_blocking_io
;
192 Optional
<bool> quic_disable_disk_cache
;
193 Optional
<bool> quic_prefer_aes
;
194 Optional
<int> quic_max_number_of_lossy_connections
;
195 Optional
<float> quic_packet_loss_threshold
;
196 Optional
<int> quic_socket_receive_buffer_size
;
197 Optional
<size_t> quic_max_packet_length
;
198 net::QuicTagVector quic_connection_options
;
199 Optional
<std::string
> quic_user_agent_id
;
200 Optional
<net::QuicVersionVector
> quic_supported_versions
;
201 Optional
<net::HostPortPair
> origin_to_force_quic_on
;
202 bool enable_user_alternate_protocol_ports
;
203 // NetErrorTabHelper uses |dns_probe_service| to send DNS probes when a
204 // main frame load fails with a DNS error in order to provide more useful
205 // information to the renderer so it can show a more specific error page.
206 scoped_ptr
<chrome_browser_net::DnsProbeService
> dns_probe_service
;
209 // |net_log| must either outlive the IOThread or be NULL.
210 IOThread(PrefService
* local_state
,
211 policy::PolicyService
* policy_service
,
212 ChromeNetLog
* net_log
,
213 extensions::EventRouterForwarder
* extension_event_router_forwarder
);
215 ~IOThread() override
;
217 static void RegisterPrefs(PrefRegistrySimple
* registry
);
219 // Can only be called on the IO thread.
222 // Allows overriding Globals in tests where IOThread::Init() and
223 // IOThread::CleanUp() are not called. This allows for injecting mocks into
224 // IOThread global objects.
225 void SetGlobalsForTesting(Globals
* globals
);
227 ChromeNetLog
* net_log();
229 // Handles changing to On The Record mode, discarding confidential data.
230 void ChangedToOnTheRecord();
232 // Returns a getter for the URLRequestContext. Only called on the UI thread.
233 net::URLRequestContextGetter
* system_url_request_context_getter();
235 // Clears the host cache. Intended to be used to prevent exposing recently
236 // visited sites on about:net-internals/#dns and about:dns pages. Must be
237 // called on the IO thread.
238 void ClearHostCache();
240 void InitializeNetworkSessionParams(net::HttpNetworkSession::Params
* params
);
242 base::TimeTicks
creation_time() const;
244 // Returns true if QUIC should be enabled for data reduction proxy, either as
245 // a result of a field trial or a command line flag.
246 static bool ShouldEnableQuicForDataReductionProxy();
249 // Map from name to value for all parameters associate with a field trial.
250 typedef std::map
<std::string
, std::string
> VariationParameters
;
252 // Provide SystemURLRequestContextGetter with access to
253 // InitSystemRequestContext().
254 friend class SystemURLRequestContextGetter
;
256 friend class test::IOThreadPeer
;
258 // BrowserThreadDelegate implementation, runs on the IO thread.
259 // This handles initialization and destruction of state that must
260 // live on the IO thread.
261 void Init() override
;
262 void CleanUp() override
;
264 // Initializes |params| based on the settings in |globals|.
265 static void InitializeNetworkSessionParamsFromGlobals(
266 const Globals
& globals
,
267 net::HttpNetworkSession::Params
* params
);
269 void InitializeNetworkOptions(const base::CommandLine
& parsed_command_line
);
271 // Sets up TCP FastOpen if enabled via field trials or via the command line.
272 void ConfigureTCPFastOpen(const base::CommandLine
& command_line
);
274 // Configures available SPDY protocol versions in |globals| based on the flags
275 // in |command_lin| as well as SPDY field trial group and parameters. Must be
276 // called after ConfigureQuicGlobals.
277 static void ConfigureSpdyGlobals(const base::CommandLine
& command_line
,
278 base::StringPiece quic_trial_group
,
279 const VariationParameters
& quic_trial_params
,
282 // Global state must be initialized on the IO thread, then this
283 // method must be invoked on the UI thread.
284 void InitSystemRequestContext();
286 // Lazy initialization of system request context for
287 // SystemURLRequestContextGetter. To be called on IO thread only
288 // after global state has been initialized on the IO thread, and
289 // SystemRequestContext state has been initialized on the UI thread.
290 void InitSystemRequestContextOnIOThread();
292 net::HttpAuthHandlerFactory
* CreateDefaultAuthHandlerFactory(
293 net::HostResolver
* resolver
);
295 // Returns an SSLConfigService instance.
296 net::SSLConfigService
* GetSSLConfigService();
298 void ChangedToOnTheRecordOnIOThread();
300 void UpdateDnsClientEnabled();
302 // Configures QUIC options based on the flags in |command_line| as
303 // well as the QUIC field trial group.
304 void ConfigureQuic(const base::CommandLine
& command_line
);
306 extensions::EventRouterForwarder
* extension_event_router_forwarder() {
307 #if defined(ENABLE_EXTENSIONS)
308 return extension_event_router_forwarder_
;
313 // Configures QUIC options in |globals| based on the flags in |command_line|
314 // as well as the QUIC field trial group and parameters. Must be called
315 // before ConfigureSpdyGlobals.
316 static void ConfigureQuicGlobals(
317 const base::CommandLine
& command_line
,
318 base::StringPiece quic_trial_group
,
319 const VariationParameters
& quic_trial_params
,
320 bool quic_allowed_by_policy
,
323 // Returns true if QUIC should be enabled, either as a result
324 // of a field trial or a command line flag.
325 static bool ShouldEnableQuic(
326 const base::CommandLine
& command_line
,
327 base::StringPiece quic_trial_group
,
328 bool quic_allowed_by_policy
);
330 // Returns true if QUIC should be enabled for proxies, either as a result
331 // of a field trial or a command line flag.
332 static bool ShouldEnableQuicForProxies(
333 const base::CommandLine
& command_line
,
334 base::StringPiece quic_trial_group
,
335 bool quic_allowed_by_policy
);
337 // Returns true if QUIC should be enabled for http:// URLs, as a result
338 // of a field trial or command line flag.
339 static bool ShouldEnableInsecureQuic(
340 const base::CommandLine
& command_line
,
341 const VariationParameters
& quic_trial_params
);
343 // Returns true if the selection of the ephemeral port in bind() should be
344 // performed by Chromium, and false if the OS should select the port. The OS
345 // option is used to prevent Windows from posting a security security warning
347 static bool ShouldEnableQuicPortSelection(
348 const base::CommandLine
& command_line
);
350 // Returns true if QUIC should always require handshake confirmation during
351 // the QUIC handshake.
352 static bool ShouldQuicAlwaysRequireHandshakeConfirmation(
353 const VariationParameters
& quic_trial_params
);
355 // Returns true if QUIC should disable connection pooling.
356 static bool ShouldQuicDisableConnectionPooling(
357 const VariationParameters
& quic_trial_params
);
359 // Returns the ratio of time to load QUIC sever information from disk cache to
360 // 'smoothed RTT' based on field trial. Returns 0 if there is an error parsing
361 // the field trial params, or if the default value should be used.
362 static float GetQuicLoadServerInfoTimeoutSrttMultiplier(
363 const VariationParameters
& quic_trial_params
);
365 // Returns true if QUIC's connection racing should be enabled.
366 static bool ShouldQuicEnableConnectionRacing(
367 const VariationParameters
& quic_trial_params
);
369 // Returns true if QUIC's should use non-blocking IO.
370 static bool ShouldQuicEnableNonBlockingIO(
371 const VariationParameters
& quic_trial_params
);
373 // Returns true if QUIC shouldn't load QUIC server information from the disk
375 static bool ShouldQuicDisableDiskCache(
376 const VariationParameters
& quic_trial_params
);
378 // Returns true if QUIC should prefer AES-GCN even without hardware support.
379 static bool ShouldQuicPreferAes(const VariationParameters
& quic_trial_params
);
381 // Returns true if QUIC should enable alternative services.
382 static bool ShouldQuicEnableAlternativeServices(
383 const VariationParameters
& quic_trial_params
);
385 // Returns the maximum number of QUIC connections with high packet loss in a
386 // row after which QUIC should be disabled. Returns 0 if the default value
388 static int GetQuicMaxNumberOfLossyConnections(
389 const VariationParameters
& quic_trial_params
);
391 // Returns the packet loss rate in fraction after which a QUIC connection is
392 // closed and is considered as a lossy connection. Returns 0 if the default
393 // value should be used.
394 static float GetQuicPacketLossThreshold(
395 const VariationParameters
& quic_trial_params
);
397 // Returns the size of the QUIC receive buffer to use, or 0 if
398 // the default should be used.
399 static int GetQuicSocketReceiveBufferSize(
400 const VariationParameters
& quic_trial_params
);
402 // Returns the maximum length for QUIC packets, based on any flags in
403 // |command_line| or the field trial. Returns 0 if there is an error
404 // parsing any of the options, or if the default value should be used.
405 static size_t GetQuicMaxPacketLength(
406 const base::CommandLine
& command_line
,
407 const VariationParameters
& quic_trial_params
);
409 // Returns the QUIC versions specified by any flags in |command_line|
410 // or |quic_trial_params|.
411 static net::QuicVersion
GetQuicVersion(
412 const base::CommandLine
& command_line
,
413 const VariationParameters
& quic_trial_params
);
415 // Returns the QUIC version specified by |quic_version| or
416 // QUIC_VERSION_UNSUPPORTED if |quic_version| is invalid.
417 static net::QuicVersion
ParseQuicVersion(const std::string
& quic_version
);
419 // Returns the QUIC connection options specified by any flags in
420 // |command_line| or |quic_trial_params|.
421 static net::QuicTagVector
GetQuicConnectionOptions(
422 const base::CommandLine
& command_line
,
423 const VariationParameters
& quic_trial_params
);
425 // Returns the alternative service probability threshold specified by
426 // any flags in |command_line| or |quic_trial_params|.
427 static double GetAlternativeProtocolProbabilityThreshold(
428 const base::CommandLine
& command_line
,
429 const VariationParameters
& quic_trial_params
);
431 static net::URLRequestContext
* ConstructSystemRequestContext(
432 IOThread::Globals
* globals
,
433 net::NetLog
* net_log
);
435 // TODO(willchan): Remove proxy script fetcher context since it's not
436 // necessary now that I got rid of refcounting URLRequestContexts.
437 // See IOThread::Globals for details.
438 static net::URLRequestContext
* ConstructProxyScriptFetcherContext(
439 IOThread::Globals
* globals
,
440 net::NetLog
* net_log
);
442 // The NetLog is owned by the browser process, to allow logging from other
443 // threads during shutdown, but is used most frequently on the IOThread.
444 ChromeNetLog
* net_log_
;
446 #if defined(ENABLE_EXTENSIONS)
447 // The extensions::EventRouterForwarder allows for sending events to
448 // extensions from the IOThread.
449 extensions::EventRouterForwarder
* extension_event_router_forwarder_
;
452 // These member variables are basically global, but their lifetimes are tied
453 // to the IOThread. IOThread owns them all, despite not using scoped_ptr.
454 // This is because the destructor of IOThread runs on the wrong thread. All
455 // member variables should be deleted in CleanUp().
457 // These member variables are initialized in Init() and do not change for the
458 // lifetime of the IO thread.
462 // Observer that logs network changes to the ChromeNetLog.
463 class LoggingNetworkChangeObserver
;
464 scoped_ptr
<LoggingNetworkChangeObserver
> network_change_observer_
;
466 BooleanPrefMember system_enable_referrers_
;
468 BooleanPrefMember dns_client_enabled_
;
470 BooleanPrefMember quick_check_enabled_
;
472 // Store HTTP Auth-related policies in this thread.
473 std::string auth_schemes_
;
474 bool negotiate_disable_cname_lookup_
;
475 bool negotiate_enable_port_
;
476 std::string auth_server_whitelist_
;
477 std::string auth_delegate_whitelist_
;
478 std::string gssapi_library_name_
;
479 std::string auth_android_negotiate_account_type_
;
481 // This is an instance of the default SSLConfigServiceManager for the current
482 // platform and it gets SSL preferences from local_state object.
483 scoped_ptr
<SSLConfigServiceManager
> ssl_config_service_manager_
;
485 // These member variables are initialized by a task posted to the IO thread,
486 // which gets posted by calling certain member functions of IOThread.
487 scoped_ptr
<net::ProxyConfigService
> system_proxy_config_service_
;
489 scoped_ptr
<PrefProxyConfigTracker
> pref_proxy_config_tracker_
;
491 scoped_refptr
<net::URLRequestContextGetter
>
492 system_url_request_context_getter_
;
494 // True if SPDY is disabled by policy.
495 bool is_spdy_disabled_by_policy_
;
497 // True if QUIC is allowed by policy.
498 bool is_quic_allowed_by_policy_
;
500 const base::TimeTicks creation_time_
;
502 base::WeakPtrFactory
<IOThread
> weak_factory_
;
504 DISALLOW_COPY_AND_ASSIGN(IOThread
);
507 #endif // CHROME_BROWSER_IO_THREAD_H_