ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / chrome / browser / io_thread.h
blobc081e64df5eacfaaacb7cfc5059f4b314a9da5f8
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 <string>
9 #include <vector>
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"
27 class ChromeNetLog;
28 class PrefProxyConfigTracker;
29 class PrefService;
30 class PrefRegistrySimple;
31 class SystemURLRequestContextGetter;
33 namespace base {
34 class CommandLine;
37 namespace chrome_browser_net {
38 class DnsProbeService;
41 namespace extensions {
42 class EventRouterForwarder;
45 namespace net {
46 class CertPolicyEnforcer;
47 class CertVerifier;
48 class ChannelIDService;
49 class CookieStore;
50 class CTVerifier;
51 class FtpTransactionFactory;
52 class HostMappingRules;
53 class HostResolver;
54 class HttpAuthHandlerFactory;
55 class HttpServerProperties;
56 class HttpTransactionFactory;
57 class HttpUserAgentSettings;
58 class NetworkDelegate;
59 class ProxyConfigService;
60 class ProxyService;
61 class SSLConfigService;
62 class TransportSecurityState;
63 class URLRequestContext;
64 class URLRequestContextGetter;
65 class URLRequestJobFactory;
66 class URLRequestThrottlerManager;
67 class URLSecurityManager;
68 } // namespace net
70 namespace policy {
71 class PolicyService;
72 } // namespace policy
74 namespace test {
75 class IOThreadPeer;
76 } // namespace test
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 {
85 public:
86 struct Globals {
87 template <typename T>
88 class Optional {
89 public:
90 Optional() : set_(false) {}
92 void set(T value) {
93 set_ = true;
94 value_ = value;
96 void CopyToIfSet(T* value) const {
97 if (set_) {
98 *value = value_;
102 private:
103 bool set_;
104 T value_;
107 class SystemRequestContextLeakChecker {
108 public:
109 explicit SystemRequestContextLeakChecker(Globals* globals);
110 ~SystemRequestContextLeakChecker();
112 private:
113 Globals* const globals_;
116 Globals();
117 ~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
127 // pins.
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;
161 #endif
162 scoped_ptr<net::HostMappingRules> host_mapping_rules;
163 scoped_ptr<net::HttpUserAgentSettings> http_user_agent_settings;
164 bool enable_ssl_connect_job_waiting;
165 bool ignore_certificate_errors;
166 bool use_stale_while_revalidate;
167 uint16 testing_fixed_http_port;
168 uint16 testing_fixed_https_port;
169 Optional<bool> enable_tcp_fast_open_for_ssl;
171 Optional<size_t> initial_max_spdy_concurrent_streams;
172 Optional<bool> force_spdy_single_domain;
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 Optional<bool> force_spdy_over_ssl;
179 Optional<bool> force_spdy_always;
180 std::set<net::HostPortPair> forced_spdy_exclusions;
181 Optional<bool> use_alternate_protocols;
182 Optional<double> alternate_protocol_probability_threshold;
184 Optional<bool> enable_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<int> quic_load_server_info_timeout_ms;
190 Optional<float> quic_load_server_info_timeout_srtt_multiplier;
191 Optional<bool> quic_enable_truncated_connection_ids;
192 Optional<bool> quic_enable_connection_racing;
193 Optional<bool> quic_enable_non_blocking_io;
194 Optional<bool> quic_disable_disk_cache;
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.
219 Globals* globals();
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();
247 private:
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 InitAsync() 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 // Enable SPDY with the given mode, which may contain the following:
276 // "off" : Disables SPDY support entirely.
277 // "ssl" : Forces SPDY for all HTTPS requests.
278 // "no-ssl" : Forces SPDY for all HTTP requests.
279 // "no-ping" : Disables SPDY ping connection testing.
280 // "exclude=<host>" : Disables SPDY support for the host <host>.
281 // "no-compress" : Disables SPDY header compression.
282 // "no-alt-protocols : Disables alternate protocol support.
283 // "force-alt-protocols : Forces an alternate protocol of SPDY/3
284 // on port 443.
285 // "single-domain" : Forces all spdy traffic to a single domain.
286 // "init-max-streams=<limit>" : Specifies the maximum number of concurrent
287 // streams for a SPDY session, unless the
288 // specifies a different value via SETTINGS.
289 void EnableSpdy(const std::string& mode);
291 // Configures available SPDY protocol versions from the given trial.
292 // Used only if no command-line configuration was present.
293 static void ConfigureSpdyFromTrial(base::StringPiece spdy_trial_group,
294 Globals* globals);
296 // Global state must be initialized on the IO thread, then this
297 // method must be invoked on the UI thread.
298 void InitSystemRequestContext();
300 // Lazy initialization of system request context for
301 // SystemURLRequestContextGetter. To be called on IO thread only
302 // after global state has been initialized on the IO thread, and
303 // SystemRequestContext state has been initialized on the UI thread.
304 void InitSystemRequestContextOnIOThread();
306 net::HttpAuthHandlerFactory* CreateDefaultAuthHandlerFactory(
307 net::HostResolver* resolver);
309 // Returns an SSLConfigService instance.
310 net::SSLConfigService* GetSSLConfigService();
312 void ChangedToOnTheRecordOnIOThread();
314 void UpdateDnsClientEnabled();
316 // Configures QUIC options based on the flags in |command_line| as
317 // well as the QUIC field trial group.
318 void ConfigureQuic(const base::CommandLine& command_line);
320 extensions::EventRouterForwarder* extension_event_router_forwarder() {
321 #if defined(ENABLE_EXTENSIONS)
322 return extension_event_router_forwarder_;
323 #else
324 return NULL;
325 #endif
327 // Configures QUIC options in |globals| based on the flags in |command_line|
328 // as well as the QUIC field trial group and parameters.
329 static void ConfigureQuicGlobals(
330 const base::CommandLine& command_line,
331 base::StringPiece quic_trial_group,
332 const VariationParameters& quic_trial_params,
333 Globals* globals);
335 // Returns true if QUIC should be enabled, either as a result
336 // of a field trial or a command line flag.
337 static bool ShouldEnableQuic(
338 const base::CommandLine& command_line,
339 base::StringPiece quic_trial_group);
341 // Returns true if QUIC should be enabled for proxies, either as a result
342 // of a field trial or a command line flag.
343 static bool ShouldEnableQuicForProxies(
344 const base::CommandLine& command_line,
345 base::StringPiece quic_trial_group);
347 // Returns true if the selection of the ephemeral port in bind() should be
348 // performed by Chromium, and false if the OS should select the port. The OS
349 // option is used to prevent Windows from posting a security security warning
350 // dialog.
351 static bool ShouldEnableQuicPortSelection(
352 const base::CommandLine& command_line);
354 // Returns true if QUIC packet pacing should be negotiated during the
355 // QUIC handshake.
356 static bool ShouldEnableQuicPacing(
357 const base::CommandLine& command_line,
358 const VariationParameters& quic_trial_params);
360 // Returns true if QUIC should always require handshake confirmation during
361 // the QUIC handshake.
362 static bool ShouldQuicAlwaysRequireHandshakeConfirmation(
363 const VariationParameters& quic_trial_params);
365 // Returns true if QUIC should disable connection pooling.
366 static bool ShouldQuicDisableConnectionPooling(
367 const VariationParameters& quic_trial_params);
369 // Returns the timeout value for loading of QUIC sever information from disk
370 // cache based on field trial. Returns 0 if there is an error parsing the
371 // field trial params, or if the default value should be used.
372 static int GetQuicLoadServerInfoTimeout(
373 const VariationParameters& quic_trial_params);
375 // Returns the ratio of time to load QUIC sever information from disk cache to
376 // 'smoothed RTT' based on field trial. Returns 0 if there is an error parsing
377 // the field trial params, or if the default value should be used.
378 static float GetQuicLoadServerInfoTimeoutSrttMultiplier(
379 const VariationParameters& quic_trial_params);
381 // Returns true if QUIC's TruncatedConnectionIds should be enabled.
382 static bool ShouldQuicEnableTruncatedConnectionIds(
383 const VariationParameters& quic_trial_params);
385 // Returns true if QUIC's connection racing should be enabled.
386 static bool ShouldQuicEnableConnectionRacing(
387 const VariationParameters& quic_trial_params);
389 // Returns true if QUIC's should use non-blocking IO.
390 static bool ShouldQuicEnableNonBlockingIO(
391 const VariationParameters& quic_trial_params);
393 // Returns true if QUIC shouldn't load QUIC server information from the disk
394 // cache.
395 static bool ShouldQuicDisableDiskCache(
396 const VariationParameters& quic_trial_params);
398 // Returns the size of the QUIC receive buffer to use, or 0 if
399 // the default should be used.
400 static int GetQuicSocketReceiveBufferSize(
401 const VariationParameters& quic_trial_params);
403 // Returns the maximum length for QUIC packets, based on any flags in
404 // |command_line| or the field trial. Returns 0 if there is an error
405 // parsing any of the options, or if the default value should be used.
406 static size_t GetQuicMaxPacketLength(
407 const base::CommandLine& command_line,
408 const VariationParameters& quic_trial_params);
410 // Returns the QUIC versions specified by any flags in |command_line|
411 // or |quic_trial_params|.
412 static net::QuicVersion GetQuicVersion(
413 const base::CommandLine& command_line,
414 const VariationParameters& quic_trial_params);
416 // Returns the QUIC version specified by |quic_version| or
417 // QUIC_VERSION_UNSUPPORTED if |quic_version| is invalid.
418 static net::QuicVersion ParseQuicVersion(const std::string& quic_version);
420 // Returns the QUIC connection options specified by any flags in
421 // |command_line| or |quic_trial_params|.
422 static net::QuicTagVector GetQuicConnectionOptions(
423 const base::CommandLine& command_line,
424 const VariationParameters& quic_trial_params);
426 // Returns the alternate protocol probability threshold specified by
427 // any flags in |command_line| or |quic_trial_params|.
428 static double GetAlternateProtocolProbabilityThreshold(
429 const base::CommandLine& command_line,
430 const VariationParameters& quic_trial_params);
432 // The NetLog is owned by the browser process, to allow logging from other
433 // threads during shutdown, but is used most frequently on the IOThread.
434 ChromeNetLog* net_log_;
436 #if defined(ENABLE_EXTENSIONS)
437 // The extensions::EventRouterForwarder allows for sending events to
438 // extensions from the IOThread.
439 extensions::EventRouterForwarder* extension_event_router_forwarder_;
440 #endif
442 // These member variables are basically global, but their lifetimes are tied
443 // to the IOThread. IOThread owns them all, despite not using scoped_ptr.
444 // This is because the destructor of IOThread runs on the wrong thread. All
445 // member variables should be deleted in CleanUp().
447 // These member variables are initialized in Init() and do not change for the
448 // lifetime of the IO thread.
450 Globals* globals_;
452 // Observer that logs network changes to the ChromeNetLog.
453 class LoggingNetworkChangeObserver;
454 scoped_ptr<LoggingNetworkChangeObserver> network_change_observer_;
456 BooleanPrefMember system_enable_referrers_;
458 BooleanPrefMember dns_client_enabled_;
460 BooleanPrefMember quick_check_enabled_;
462 // Store HTTP Auth-related policies in this thread.
463 std::string auth_schemes_;
464 bool negotiate_disable_cname_lookup_;
465 bool negotiate_enable_port_;
466 std::string auth_server_whitelist_;
467 std::string auth_delegate_whitelist_;
468 std::string gssapi_library_name_;
470 // This is an instance of the default SSLConfigServiceManager for the current
471 // platform and it gets SSL preferences from local_state object.
472 scoped_ptr<SSLConfigServiceManager> ssl_config_service_manager_;
474 // These member variables are initialized by a task posted to the IO thread,
475 // which gets posted by calling certain member functions of IOThread.
476 scoped_ptr<net::ProxyConfigService> system_proxy_config_service_;
478 scoped_ptr<PrefProxyConfigTracker> pref_proxy_config_tracker_;
480 scoped_refptr<net::URLRequestContextGetter>
481 system_url_request_context_getter_;
483 // True if SPDY is disabled by policy.
484 bool is_spdy_disabled_by_policy_;
486 const base::TimeTicks creation_time_;
488 base::WeakPtrFactory<IOThread> weak_factory_;
490 DISALLOW_COPY_AND_ASSIGN(IOThread);
493 #endif // CHROME_BROWSER_IO_THREAD_H_