[Sync] Add maxbogue to OWNERS for Android files.
[chromium-blink-merge.git] / chrome / browser / io_thread.h
blob21b57d33b9fa6b11e2909fdd99ad4d695e52d517
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 <set>
10 #include <string>
11 #include <vector>
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"
29 class ChromeNetLog;
30 class PrefProxyConfigTracker;
31 class PrefService;
32 class PrefRegistrySimple;
33 class SystemURLRequestContextGetter;
35 namespace base {
36 class CommandLine;
39 namespace chrome_browser_net {
40 class DnsProbeService;
43 namespace extensions {
44 class EventRouterForwarder;
47 namespace net {
48 class CertPolicyEnforcer;
49 class CertVerifier;
50 class ChannelIDService;
51 class CookieStore;
52 class CTVerifier;
53 class FtpTransactionFactory;
54 class HostMappingRules;
55 class HostResolver;
56 class HttpAuthHandlerFactory;
57 class HttpNetworkSession;
58 class HttpServerProperties;
59 class HttpTransactionFactory;
60 class HttpUserAgentSettings;
61 class NetworkDelegate;
62 class NetworkQualityEstimator;
63 class ProxyConfigService;
64 class ProxyService;
65 class SSLConfigService;
66 class TransportSecurityState;
67 class URLRequestBackoffManager;
68 class URLRequestContext;
69 class URLRequestContextGetter;
70 class URLRequestJobFactory;
71 class URLSecurityManager;
72 } // namespace net
74 namespace policy {
75 class PolicyService;
76 } // namespace policy
78 namespace test {
79 class IOThreadPeer;
80 } // namespace test
82 // Contains state associated with, initialized and cleaned up on, and
83 // primarily used on, the IO thread.
85 // If you are looking to interact with the IO thread (e.g. post tasks
86 // to it or check if it is the current thread), see
87 // content::BrowserThread.
88 class IOThread : public content::BrowserThreadDelegate {
89 public:
90 struct Globals {
91 template <typename T>
92 class Optional {
93 public:
94 Optional() : set_(false) {}
96 void set(T value) {
97 set_ = true;
98 value_ = value;
100 void CopyToIfSet(T* value) const {
101 if (set_) {
102 *value = value_;
106 private:
107 bool set_;
108 T value_;
111 class SystemRequestContextLeakChecker {
112 public:
113 explicit SystemRequestContextLeakChecker(Globals* globals);
114 ~SystemRequestContextLeakChecker();
116 private:
117 Globals* const globals_;
120 Globals();
121 ~Globals();
123 // The "system" NetworkDelegate, used for Profile-agnostic network events.
124 scoped_ptr<net::NetworkDelegate> system_network_delegate;
125 scoped_ptr<net::HostResolver> host_resolver;
126 scoped_ptr<net::CertVerifier> cert_verifier;
127 // The ChannelIDService must outlive the HttpTransactionFactory.
128 scoped_ptr<net::ChannelIDService> system_channel_id_service;
129 // This TransportSecurityState doesn't load or save any state. It's only
130 // used to enforce pinning for system requests and will only use built-in
131 // pins.
132 scoped_ptr<net::TransportSecurityState> transport_security_state;
133 scoped_ptr<net::CTVerifier> cert_transparency_verifier;
134 scoped_ptr<net::CertPolicyEnforcer> cert_policy_enforcer;
135 scoped_refptr<net::SSLConfigService> ssl_config_service;
136 scoped_ptr<net::HttpAuthHandlerFactory> http_auth_handler_factory;
137 scoped_ptr<net::HttpServerProperties> http_server_properties;
138 scoped_ptr<net::ProxyService> proxy_script_fetcher_proxy_service;
139 scoped_ptr<net::HttpNetworkSession>
140 proxy_script_fetcher_http_network_session;
141 scoped_ptr<net::HttpTransactionFactory>
142 proxy_script_fetcher_http_transaction_factory;
143 scoped_ptr<net::FtpTransactionFactory>
144 proxy_script_fetcher_ftp_transaction_factory;
145 scoped_ptr<net::URLRequestJobFactory>
146 proxy_script_fetcher_url_request_job_factory;
147 scoped_ptr<net::URLRequestBackoffManager> url_request_backoff_manager;
148 scoped_ptr<net::URLSecurityManager> url_security_manager;
149 // TODO(willchan): Remove proxy script fetcher context since it's not
150 // necessary now that I got rid of refcounting URLRequestContexts.
152 // The first URLRequestContext is |system_url_request_context|. We introduce
153 // |proxy_script_fetcher_context| for the second context. It has a direct
154 // ProxyService, since we always directly connect to fetch the PAC script.
155 scoped_ptr<net::URLRequestContext> proxy_script_fetcher_context;
156 scoped_ptr<net::ProxyService> system_proxy_service;
157 scoped_ptr<net::HttpNetworkSession> system_http_network_session;
158 scoped_ptr<net::HttpTransactionFactory> system_http_transaction_factory;
159 scoped_ptr<net::URLRequestJobFactory> system_url_request_job_factory;
160 scoped_ptr<net::URLRequestContext> system_request_context;
161 SystemRequestContextLeakChecker system_request_context_leak_checker;
162 // |system_cookie_store| and |system_channel_id_service| are shared
163 // between |proxy_script_fetcher_context| and |system_request_context|.
164 scoped_refptr<net::CookieStore> system_cookie_store;
165 #if defined(ENABLE_EXTENSIONS)
166 scoped_refptr<extensions::EventRouterForwarder>
167 extension_event_router_forwarder;
168 #endif
169 scoped_ptr<net::HostMappingRules> host_mapping_rules;
170 scoped_ptr<net::HttpUserAgentSettings> http_user_agent_settings;
171 scoped_ptr<net::NetworkQualityEstimator> network_quality_estimator;
172 bool ignore_certificate_errors;
173 uint16 testing_fixed_http_port;
174 uint16 testing_fixed_https_port;
175 Optional<bool> enable_tcp_fast_open_for_ssl;
177 Optional<size_t> initial_max_spdy_concurrent_streams;
178 Optional<bool> enable_spdy_compression;
179 Optional<bool> enable_spdy_ping_based_connection_checking;
180 Optional<net::NextProto> spdy_default_protocol;
181 net::NextProtoVector next_protos;
182 Optional<std::string> trusted_spdy_proxy;
183 std::set<net::HostPortPair> forced_spdy_exclusions;
184 Optional<bool> use_alternative_services;
185 Optional<double> alternative_service_probability_threshold;
187 Optional<bool> enable_quic;
188 Optional<bool> enable_insecure_quic;
189 Optional<bool> enable_quic_for_proxies;
190 Optional<bool> enable_quic_port_selection;
191 Optional<bool> quic_always_require_handshake_confirmation;
192 Optional<bool> quic_disable_connection_pooling;
193 Optional<float> quic_load_server_info_timeout_srtt_multiplier;
194 Optional<bool> quic_enable_connection_racing;
195 Optional<bool> quic_enable_non_blocking_io;
196 Optional<bool> quic_disable_disk_cache;
197 Optional<bool> quic_prefer_aes;
198 Optional<int> quic_max_number_of_lossy_connections;
199 Optional<float> quic_packet_loss_threshold;
200 Optional<int> quic_socket_receive_buffer_size;
201 Optional<size_t> quic_max_packet_length;
202 net::QuicTagVector quic_connection_options;
203 Optional<std::string> quic_user_agent_id;
204 Optional<net::QuicVersionVector> quic_supported_versions;
205 Optional<net::HostPortPair> origin_to_force_quic_on;
206 bool enable_user_alternate_protocol_ports;
207 // NetErrorTabHelper uses |dns_probe_service| to send DNS probes when a
208 // main frame load fails with a DNS error in order to provide more useful
209 // information to the renderer so it can show a more specific error page.
210 scoped_ptr<chrome_browser_net::DnsProbeService> dns_probe_service;
213 // |net_log| must either outlive the IOThread or be NULL.
214 IOThread(PrefService* local_state,
215 policy::PolicyService* policy_service,
216 ChromeNetLog* net_log,
217 extensions::EventRouterForwarder* extension_event_router_forwarder);
219 ~IOThread() override;
221 static void RegisterPrefs(PrefRegistrySimple* registry);
223 // Can only be called on the IO thread.
224 Globals* globals();
226 // Allows overriding Globals in tests where IOThread::Init() and
227 // IOThread::CleanUp() are not called. This allows for injecting mocks into
228 // IOThread global objects.
229 void SetGlobalsForTesting(Globals* globals);
231 ChromeNetLog* net_log();
233 // Handles changing to On The Record mode, discarding confidential data.
234 void ChangedToOnTheRecord();
236 // Returns a getter for the URLRequestContext. Only called on the UI thread.
237 net::URLRequestContextGetter* system_url_request_context_getter();
239 // Clears the host cache. Intended to be used to prevent exposing recently
240 // visited sites on about:net-internals/#dns and about:dns pages. Must be
241 // called on the IO thread.
242 void ClearHostCache();
244 void InitializeNetworkSessionParams(net::HttpNetworkSession::Params* params);
246 base::TimeTicks creation_time() const;
248 // Returns true if QUIC should be enabled for data reduction proxy, either as
249 // a result of a field trial or a command line flag.
250 static bool ShouldEnableQuicForDataReductionProxy();
252 private:
253 // Map from name to value for all parameters associate with a field trial.
254 typedef std::map<std::string, std::string> VariationParameters;
256 // Provide SystemURLRequestContextGetter with access to
257 // InitSystemRequestContext().
258 friend class SystemURLRequestContextGetter;
260 friend class test::IOThreadPeer;
262 // BrowserThreadDelegate implementation, runs on the IO thread.
263 // This handles initialization and destruction of state that must
264 // live on the IO thread.
265 void Init() override;
266 void CleanUp() override;
268 // Initializes |params| based on the settings in |globals|.
269 static void InitializeNetworkSessionParamsFromGlobals(
270 const Globals& globals,
271 net::HttpNetworkSession::Params* params);
273 void InitializeNetworkOptions(const base::CommandLine& parsed_command_line);
275 // Sets up TCP FastOpen if enabled via field trials or via the command line.
276 void ConfigureTCPFastOpen(const base::CommandLine& command_line);
278 // Configures available SPDY protocol versions in |globals| based on the flags
279 // in |command_lin| as well as SPDY field trial group and parameters. Must be
280 // called after ConfigureQuicGlobals.
281 static void ConfigureSpdyGlobals(const base::CommandLine& command_line,
282 base::StringPiece quic_trial_group,
283 const VariationParameters& quic_trial_params,
284 Globals* globals);
286 // Global state must be initialized on the IO thread, then this
287 // method must be invoked on the UI thread.
288 void InitSystemRequestContext();
290 // Lazy initialization of system request context for
291 // SystemURLRequestContextGetter. To be called on IO thread only
292 // after global state has been initialized on the IO thread, and
293 // SystemRequestContext state has been initialized on the UI thread.
294 void InitSystemRequestContextOnIOThread();
296 net::HttpAuthHandlerFactory* CreateDefaultAuthHandlerFactory(
297 net::HostResolver* resolver);
299 // Returns an SSLConfigService instance.
300 net::SSLConfigService* GetSSLConfigService();
302 void ChangedToOnTheRecordOnIOThread();
304 void UpdateDnsClientEnabled();
306 // Configures QUIC options based on the flags in |command_line| as
307 // well as the QUIC field trial group.
308 void ConfigureQuic(const base::CommandLine& command_line);
310 extensions::EventRouterForwarder* extension_event_router_forwarder() {
311 #if defined(ENABLE_EXTENSIONS)
312 return extension_event_router_forwarder_;
313 #else
314 return NULL;
315 #endif
317 // Configures QUIC options in |globals| based on the flags in |command_line|
318 // as well as the QUIC field trial group and parameters. Must be called
319 // before ConfigureSpdyGlobals.
320 static void ConfigureQuicGlobals(
321 const base::CommandLine& command_line,
322 base::StringPiece quic_trial_group,
323 const VariationParameters& quic_trial_params,
324 bool quic_allowed_by_policy,
325 Globals* globals);
327 // Returns true if QUIC should be enabled, either as a result
328 // of a field trial or a command line flag.
329 static bool ShouldEnableQuic(
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 enabled for proxies, either as a result
335 // of a field trial or a command line flag.
336 static bool ShouldEnableQuicForProxies(
337 const base::CommandLine& command_line,
338 base::StringPiece quic_trial_group,
339 bool quic_allowed_by_policy);
341 // Returns true if QUIC should be enabled for http:// URLs, as a result
342 // of a field trial or command line flag.
343 static bool ShouldEnableInsecureQuic(
344 const base::CommandLine& command_line,
345 const VariationParameters& quic_trial_params);
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 should always require handshake confirmation during
355 // the QUIC handshake.
356 static bool ShouldQuicAlwaysRequireHandshakeConfirmation(
357 const VariationParameters& quic_trial_params);
359 // Returns true if QUIC should disable connection pooling.
360 static bool ShouldQuicDisableConnectionPooling(
361 const VariationParameters& quic_trial_params);
363 // Returns the ratio of time to load QUIC sever information from disk cache to
364 // 'smoothed RTT' based on field trial. Returns 0 if there is an error parsing
365 // the field trial params, or if the default value should be used.
366 static float GetQuicLoadServerInfoTimeoutSrttMultiplier(
367 const VariationParameters& quic_trial_params);
369 // Returns true if QUIC's connection racing should be enabled.
370 static bool ShouldQuicEnableConnectionRacing(
371 const VariationParameters& quic_trial_params);
373 // Returns true if QUIC's should use non-blocking IO.
374 static bool ShouldQuicEnableNonBlockingIO(
375 const VariationParameters& quic_trial_params);
377 // Returns true if QUIC shouldn't load QUIC server information from the disk
378 // cache.
379 static bool ShouldQuicDisableDiskCache(
380 const VariationParameters& quic_trial_params);
382 // Returns true if QUIC should prefer AES-GCN even without hardware support.
383 static bool ShouldQuicPreferAes(const VariationParameters& quic_trial_params);
385 // Returns true if QUIC should enable alternative services.
386 static bool ShouldQuicEnableAlternativeServices(
387 const VariationParameters& quic_trial_params);
389 // Returns the maximum number of QUIC connections with high packet loss in a
390 // row after which QUIC should be disabled. Returns 0 if the default value
391 // should be used.
392 static int GetQuicMaxNumberOfLossyConnections(
393 const VariationParameters& quic_trial_params);
395 // Returns the packet loss rate in fraction after which a QUIC connection is
396 // closed and is considered as a lossy connection. Returns 0 if the default
397 // value should be used.
398 static float GetQuicPacketLossThreshold(
399 const VariationParameters& quic_trial_params);
401 // Returns the size of the QUIC receive buffer to use, or 0 if
402 // the default should be used.
403 static int GetQuicSocketReceiveBufferSize(
404 const VariationParameters& quic_trial_params);
406 // Returns the maximum length for QUIC packets, based on any flags in
407 // |command_line| or the field trial. Returns 0 if there is an error
408 // parsing any of the options, or if the default value should be used.
409 static size_t GetQuicMaxPacketLength(
410 const base::CommandLine& command_line,
411 const VariationParameters& quic_trial_params);
413 // Returns the QUIC versions specified by any flags in |command_line|
414 // or |quic_trial_params|.
415 static net::QuicVersion GetQuicVersion(
416 const base::CommandLine& command_line,
417 const VariationParameters& quic_trial_params);
419 // Returns the QUIC version specified by |quic_version| or
420 // QUIC_VERSION_UNSUPPORTED if |quic_version| is invalid.
421 static net::QuicVersion ParseQuicVersion(const std::string& quic_version);
423 // Returns the QUIC connection options specified by any flags in
424 // |command_line| or |quic_trial_params|.
425 static net::QuicTagVector GetQuicConnectionOptions(
426 const base::CommandLine& command_line,
427 const VariationParameters& quic_trial_params);
429 // Returns the alternative service probability threshold specified by
430 // any flags in |command_line| or |quic_trial_params|.
431 static double GetAlternativeProtocolProbabilityThreshold(
432 const base::CommandLine& command_line,
433 const VariationParameters& quic_trial_params);
435 static net::URLRequestContext* ConstructSystemRequestContext(
436 IOThread::Globals* globals,
437 net::NetLog* net_log);
439 // TODO(willchan): Remove proxy script fetcher context since it's not
440 // necessary now that I got rid of refcounting URLRequestContexts.
441 // See IOThread::Globals for details.
442 static net::URLRequestContext* ConstructProxyScriptFetcherContext(
443 IOThread::Globals* globals,
444 net::NetLog* net_log);
446 // The NetLog is owned by the browser process, to allow logging from other
447 // threads during shutdown, but is used most frequently on the IOThread.
448 ChromeNetLog* net_log_;
450 #if defined(ENABLE_EXTENSIONS)
451 // The extensions::EventRouterForwarder allows for sending events to
452 // extensions from the IOThread.
453 extensions::EventRouterForwarder* extension_event_router_forwarder_;
454 #endif
456 // These member variables are basically global, but their lifetimes are tied
457 // to the IOThread. IOThread owns them all, despite not using scoped_ptr.
458 // This is because the destructor of IOThread runs on the wrong thread. All
459 // member variables should be deleted in CleanUp().
461 // These member variables are initialized in Init() and do not change for the
462 // lifetime of the IO thread.
464 Globals* globals_;
466 // Observer that logs network changes to the ChromeNetLog.
467 class LoggingNetworkChangeObserver;
468 scoped_ptr<LoggingNetworkChangeObserver> network_change_observer_;
470 BooleanPrefMember system_enable_referrers_;
472 BooleanPrefMember dns_client_enabled_;
474 BooleanPrefMember quick_check_enabled_;
476 // Store HTTP Auth-related policies in this thread.
477 std::string auth_schemes_;
478 bool negotiate_disable_cname_lookup_;
479 bool negotiate_enable_port_;
480 std::string auth_server_whitelist_;
481 std::string auth_delegate_whitelist_;
482 std::string gssapi_library_name_;
483 std::string auth_android_negotiate_account_type_;
485 // This is an instance of the default SSLConfigServiceManager for the current
486 // platform and it gets SSL preferences from local_state object.
487 scoped_ptr<SSLConfigServiceManager> ssl_config_service_manager_;
489 // These member variables are initialized by a task posted to the IO thread,
490 // which gets posted by calling certain member functions of IOThread.
491 scoped_ptr<net::ProxyConfigService> system_proxy_config_service_;
493 scoped_ptr<PrefProxyConfigTracker> pref_proxy_config_tracker_;
495 scoped_refptr<net::URLRequestContextGetter>
496 system_url_request_context_getter_;
498 // True if SPDY is disabled by policy.
499 bool is_spdy_disabled_by_policy_;
501 // True if QUIC is allowed by policy.
502 bool is_quic_allowed_by_policy_;
504 const base::TimeTicks creation_time_;
506 base::WeakPtrFactory<IOThread> weak_factory_;
508 DISALLOW_COPY_AND_ASSIGN(IOThread);
511 #endif // CHROME_BROWSER_IO_THREAD_H_