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_
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/prefs/pref_member.h"
17 #include "base/strings/string_piece.h"
18 #include "base/time/time.h"
19 #include "chrome/browser/net/chrome_network_delegate.h"
20 #include "chrome/browser/net/ssl_config_service_manager.h"
21 #include "content/public/browser/browser_thread.h"
22 #include "content/public/browser/browser_thread_delegate.h"
23 #include "net/base/network_change_notifier.h"
24 #include "net/http/http_network_session.h"
25 #include "net/socket/next_proto.h"
28 class PrefProxyConfigTracker
;
30 class PrefRegistrySimple
;
31 class SystemURLRequestContextGetter
;
37 namespace chrome_browser_net
{
38 class DnsProbeService
;
41 namespace extensions
{
42 class EventRouterForwarder
;
46 class CertPolicyEnforcer
;
48 class ChannelIDService
;
51 class FtpTransactionFactory
;
52 class HostMappingRules
;
54 class HttpAuthHandlerFactory
;
55 class HttpServerProperties
;
56 class HttpTransactionFactory
;
57 class HttpUserAgentSettings
;
58 class NetworkDelegate
;
59 class ProxyConfigService
;
61 class SSLConfigService
;
62 class TransportSecurityState
;
63 class URLRequestContext
;
64 class URLRequestContextGetter
;
65 class URLRequestJobFactory
;
66 class URLRequestThrottlerManager
;
67 class URLSecurityManager
;
78 // Contains state associated with, initialized and cleaned up on, and
79 // primarily used on, the IO thread.
81 // If you are looking to interact with the IO thread (e.g. post tasks
82 // to it or check if it is the current thread), see
83 // content::BrowserThread.
84 class IOThread
: public content::BrowserThreadDelegate
{
90 Optional() : set_(false) {}
96 void CopyToIfSet(T
* value
) const {
107 class SystemRequestContextLeakChecker
{
109 explicit SystemRequestContextLeakChecker(Globals
* globals
);
110 ~SystemRequestContextLeakChecker();
113 Globals
* const globals_
;
119 // The "system" NetworkDelegate, used for Profile-agnostic network events.
120 scoped_ptr
<net::NetworkDelegate
> system_network_delegate
;
121 scoped_ptr
<net::HostResolver
> host_resolver
;
122 scoped_ptr
<net::CertVerifier
> cert_verifier
;
123 // The ChannelIDService must outlive the HttpTransactionFactory.
124 scoped_ptr
<net::ChannelIDService
> system_channel_id_service
;
125 // This TransportSecurityState doesn't load or save any state. It's only
126 // used to enforce pinning for system requests and will only use built-in
128 scoped_ptr
<net::TransportSecurityState
> transport_security_state
;
129 scoped_ptr
<net::CTVerifier
> cert_transparency_verifier
;
130 scoped_ptr
<net::CertPolicyEnforcer
> cert_policy_enforcer
;
131 scoped_refptr
<net::SSLConfigService
> ssl_config_service
;
132 scoped_ptr
<net::HttpAuthHandlerFactory
> http_auth_handler_factory
;
133 scoped_ptr
<net::HttpServerProperties
> http_server_properties
;
134 scoped_ptr
<net::ProxyService
> proxy_script_fetcher_proxy_service
;
135 scoped_ptr
<net::HttpTransactionFactory
>
136 proxy_script_fetcher_http_transaction_factory
;
137 scoped_ptr
<net::FtpTransactionFactory
>
138 proxy_script_fetcher_ftp_transaction_factory
;
139 scoped_ptr
<net::URLRequestJobFactory
>
140 proxy_script_fetcher_url_request_job_factory
;
141 scoped_ptr
<net::URLRequestThrottlerManager
> throttler_manager
;
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
;
162 scoped_ptr
<net::HostMappingRules
> host_mapping_rules
;
163 scoped_ptr
<net::HttpUserAgentSettings
> http_user_agent_settings
;
164 bool ignore_certificate_errors
;
165 bool use_stale_while_revalidate
;
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> alternate_protocol_probability_threshold
;
180 Optional
<bool> enable_quic
;
181 Optional
<bool> enable_quic_for_proxies
;
182 Optional
<bool> enable_quic_port_selection
;
183 Optional
<bool> quic_always_require_handshake_confirmation
;
184 Optional
<bool> quic_disable_connection_pooling
;
185 Optional
<float> quic_load_server_info_timeout_srtt_multiplier
;
186 Optional
<bool> quic_enable_connection_racing
;
187 Optional
<bool> quic_enable_non_blocking_io
;
188 Optional
<bool> quic_disable_disk_cache
;
189 Optional
<int> quic_max_number_of_lossy_connections
;
190 Optional
<float> quic_packet_loss_threshold
;
191 Optional
<int> quic_socket_receive_buffer_size
;
192 Optional
<size_t> quic_max_packet_length
;
193 net::QuicTagVector quic_connection_options
;
194 Optional
<std::string
> quic_user_agent_id
;
195 Optional
<net::QuicVersionVector
> quic_supported_versions
;
196 Optional
<net::HostPortPair
> origin_to_force_quic_on
;
197 bool enable_user_alternate_protocol_ports
;
198 // NetErrorTabHelper uses |dns_probe_service| to send DNS probes when a
199 // main frame load fails with a DNS error in order to provide more useful
200 // information to the renderer so it can show a more specific error page.
201 scoped_ptr
<chrome_browser_net::DnsProbeService
> dns_probe_service
;
204 // |net_log| must either outlive the IOThread or be NULL.
205 IOThread(PrefService
* local_state
,
206 policy::PolicyService
* policy_service
,
207 ChromeNetLog
* net_log
,
208 extensions::EventRouterForwarder
* extension_event_router_forwarder
);
210 ~IOThread() override
;
212 static void RegisterPrefs(PrefRegistrySimple
* registry
);
214 // Can only be called on the IO thread.
217 // Allows overriding Globals in tests where IOThread::Init() and
218 // IOThread::CleanUp() are not called. This allows for injecting mocks into
219 // IOThread global objects.
220 void SetGlobalsForTesting(Globals
* globals
);
222 ChromeNetLog
* net_log();
224 // Handles changing to On The Record mode, discarding confidential data.
225 void ChangedToOnTheRecord();
227 // Returns a getter for the URLRequestContext. Only called on the UI thread.
228 net::URLRequestContextGetter
* system_url_request_context_getter();
230 // Clears the host cache. Intended to be used to prevent exposing recently
231 // visited sites on about:net-internals/#dns and about:dns pages. Must be
232 // called on the IO thread.
233 void ClearHostCache();
235 void InitializeNetworkSessionParams(net::HttpNetworkSession::Params
* params
);
237 base::TimeTicks
creation_time() const;
239 // Returns true if QUIC should be enabled for data reduction proxy, either as
240 // a result of a field trial or a command line flag.
241 static bool ShouldEnableQuicForDataReductionProxy();
244 // Map from name to value for all parameters associate with a field trial.
245 typedef std::map
<std::string
, std::string
> VariationParameters
;
247 // Provide SystemURLRequestContextGetter with access to
248 // InitSystemRequestContext().
249 friend class SystemURLRequestContextGetter
;
251 friend class test::IOThreadPeer
;
253 // BrowserThreadDelegate implementation, runs on the IO thread.
254 // This handles initialization and destruction of state that must
255 // live on the IO thread.
256 void Init() override
;
257 void InitAsync() override
;
258 void CleanUp() override
;
260 // Initializes |params| based on the settings in |globals|.
261 static void InitializeNetworkSessionParamsFromGlobals(
262 const Globals
& globals
,
263 net::HttpNetworkSession::Params
* params
);
265 void InitializeNetworkOptions(const base::CommandLine
& parsed_command_line
);
267 // Sets up TCP FastOpen if enabled via field trials or via the command line.
268 void ConfigureTCPFastOpen(const base::CommandLine
& command_line
);
270 // Sets up SDCH based on field trials.
271 void ConfigureSdch();
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
,
321 // Returns true if QUIC should be enabled, either as a result
322 // of a field trial or a command line flag.
323 static bool ShouldEnableQuic(
324 const base::CommandLine
& command_line
,
325 base::StringPiece quic_trial_group
);
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
);
333 // Returns true if the selection of the ephemeral port in bind() should be
334 // performed by Chromium, and false if the OS should select the port. The OS
335 // option is used to prevent Windows from posting a security security warning
337 static bool ShouldEnableQuicPortSelection(
338 const base::CommandLine
& command_line
);
340 // Returns true if QUIC packet pacing should be negotiated during the
342 static bool ShouldEnableQuicPacing(
343 const base::CommandLine
& command_line
,
344 const VariationParameters
& quic_trial_params
);
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
371 static bool ShouldQuicDisableDiskCache(
372 const VariationParameters
& quic_trial_params
);
374 // Returns the maximum number of QUIC connections with high packet loss in a
375 // row after which QUIC should be disabled. Returns 0 if the default value
377 static int GetQuicMaxNumberOfLossyConnections(
378 const VariationParameters
& quic_trial_params
);
380 // Returns the packet loss rate in fraction after which a QUIC connection is
381 // closed and is considered as a lossy connection. Returns 0 if the default
382 // value should be used.
383 static float GetQuicPacketLossThreshold(
384 const VariationParameters
& quic_trial_params
);
386 // Returns the size of the QUIC receive buffer to use, or 0 if
387 // the default should be used.
388 static int GetQuicSocketReceiveBufferSize(
389 const VariationParameters
& quic_trial_params
);
391 // Returns the maximum length for QUIC packets, based on any flags in
392 // |command_line| or the field trial. Returns 0 if there is an error
393 // parsing any of the options, or if the default value should be used.
394 static size_t GetQuicMaxPacketLength(
395 const base::CommandLine
& command_line
,
396 const VariationParameters
& quic_trial_params
);
398 // Returns the QUIC versions specified by any flags in |command_line|
399 // or |quic_trial_params|.
400 static net::QuicVersion
GetQuicVersion(
401 const base::CommandLine
& command_line
,
402 const VariationParameters
& quic_trial_params
);
404 // Returns the QUIC version specified by |quic_version| or
405 // QUIC_VERSION_UNSUPPORTED if |quic_version| is invalid.
406 static net::QuicVersion
ParseQuicVersion(const std::string
& quic_version
);
408 // Returns the QUIC connection options specified by any flags in
409 // |command_line| or |quic_trial_params|.
410 static net::QuicTagVector
GetQuicConnectionOptions(
411 const base::CommandLine
& command_line
,
412 const VariationParameters
& quic_trial_params
);
414 // Returns the alternate protocol probability threshold specified by
415 // any flags in |command_line| or |quic_trial_params|.
416 static double GetAlternateProtocolProbabilityThreshold(
417 const base::CommandLine
& command_line
,
418 const VariationParameters
& quic_trial_params
);
420 // The NetLog is owned by the browser process, to allow logging from other
421 // threads during shutdown, but is used most frequently on the IOThread.
422 ChromeNetLog
* net_log_
;
424 #if defined(ENABLE_EXTENSIONS)
425 // The extensions::EventRouterForwarder allows for sending events to
426 // extensions from the IOThread.
427 extensions::EventRouterForwarder
* extension_event_router_forwarder_
;
430 // These member variables are basically global, but their lifetimes are tied
431 // to the IOThread. IOThread owns them all, despite not using scoped_ptr.
432 // This is because the destructor of IOThread runs on the wrong thread. All
433 // member variables should be deleted in CleanUp().
435 // These member variables are initialized in Init() and do not change for the
436 // lifetime of the IO thread.
440 // Observer that logs network changes to the ChromeNetLog.
441 class LoggingNetworkChangeObserver
;
442 scoped_ptr
<LoggingNetworkChangeObserver
> network_change_observer_
;
444 BooleanPrefMember system_enable_referrers_
;
446 BooleanPrefMember dns_client_enabled_
;
448 BooleanPrefMember quick_check_enabled_
;
450 // Store HTTP Auth-related policies in this thread.
451 std::string auth_schemes_
;
452 bool negotiate_disable_cname_lookup_
;
453 bool negotiate_enable_port_
;
454 std::string auth_server_whitelist_
;
455 std::string auth_delegate_whitelist_
;
456 std::string gssapi_library_name_
;
458 // This is an instance of the default SSLConfigServiceManager for the current
459 // platform and it gets SSL preferences from local_state object.
460 scoped_ptr
<SSLConfigServiceManager
> ssl_config_service_manager_
;
462 // These member variables are initialized by a task posted to the IO thread,
463 // which gets posted by calling certain member functions of IOThread.
464 scoped_ptr
<net::ProxyConfigService
> system_proxy_config_service_
;
466 scoped_ptr
<PrefProxyConfigTracker
> pref_proxy_config_tracker_
;
468 scoped_refptr
<net::URLRequestContextGetter
>
469 system_url_request_context_getter_
;
471 // True if SPDY is disabled by policy.
472 bool is_spdy_disabled_by_policy_
;
474 const base::TimeTicks creation_time_
;
476 base::WeakPtrFactory
<IOThread
> weak_factory_
;
478 DISALLOW_COPY_AND_ASSIGN(IOThread
);
481 #endif // CHROME_BROWSER_IO_THREAD_H_