Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / chrome / browser / io_thread.h
blob8559217696215011d52c245d80441f8a38d241d3
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/time/time.h"
18 #include "chrome/browser/net/chrome_network_delegate.h"
19 #include "chrome/browser/net/ssl_config_service_manager.h"
20 #include "content/public/browser/browser_thread.h"
21 #include "content/public/browser/browser_thread_delegate.h"
22 #include "net/base/network_change_notifier.h"
23 #include "net/http/http_network_session.h"
24 #include "net/socket/next_proto.h"
26 class ChromeNetLog;
27 class PrefProxyConfigTracker;
28 class PrefService;
29 class PrefRegistrySimple;
30 class SystemURLRequestContextGetter;
32 namespace base {
33 class CommandLine;
36 namespace chrome_browser_net {
37 class DnsProbeService;
40 #if defined(SPDY_PROXY_AUTH_ORIGIN)
41 namespace data_reduction_proxy {
42 class DataReductionProxyAuthRequestHandler;
43 class DataReductionProxyParams;
45 #endif // defined(SPDY_PROXY_AUTH_ORIGIN)
47 namespace extensions {
48 class EventRouterForwarder;
51 namespace net {
52 class CertVerifier;
53 class ChannelIDService;
54 class CookieStore;
55 class CTVerifier;
56 class FtpTransactionFactory;
57 class HostMappingRules;
58 class HostResolver;
59 class HttpAuthHandlerFactory;
60 class HttpServerProperties;
61 class HttpTransactionFactory;
62 class HttpUserAgentSettings;
63 class NetworkDelegate;
64 class ProxyConfigService;
65 class ProxyService;
66 class SSLConfigService;
67 class TransportSecurityState;
68 class URLRequestContext;
69 class URLRequestContextGetter;
70 class URLRequestJobFactory;
71 class URLRequestThrottlerManager;
72 class URLSecurityManager;
73 } // namespace net
75 namespace policy {
76 class PolicyService;
77 } // namespace policy
79 namespace test {
80 class IOThreadPeer;
81 } // namespace test
83 // Contains state associated with, initialized and cleaned up on, and
84 // primarily used on, the IO thread.
86 // If you are looking to interact with the IO thread (e.g. post tasks
87 // to it or check if it is the current thread), see
88 // content::BrowserThread.
89 class IOThread : public content::BrowserThreadDelegate {
90 public:
91 struct Globals {
92 template <typename T>
93 class Optional {
94 public:
95 Optional() : set_(false) {}
97 void set(T value) {
98 set_ = true;
99 value_ = value;
101 void CopyToIfSet(T* value) const {
102 if (set_) {
103 *value = value_;
107 private:
108 bool set_;
109 T value_;
112 class SystemRequestContextLeakChecker {
113 public:
114 explicit SystemRequestContextLeakChecker(Globals* globals);
115 ~SystemRequestContextLeakChecker();
117 private:
118 Globals* const globals_;
121 Globals();
122 ~Globals();
124 // The "system" NetworkDelegate, used for Profile-agnostic network events.
125 scoped_ptr<net::NetworkDelegate> system_network_delegate;
126 scoped_ptr<net::HostResolver> host_resolver;
127 scoped_ptr<net::CertVerifier> cert_verifier;
128 // The ChannelIDService must outlive the HttpTransactionFactory.
129 scoped_ptr<net::ChannelIDService> system_channel_id_service;
130 // This TransportSecurityState doesn't load or save any state. It's only
131 // used to enforce pinning for system requests and will only use built-in
132 // pins.
133 scoped_ptr<net::TransportSecurityState> transport_security_state;
134 scoped_ptr<net::CTVerifier> cert_transparency_verifier;
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::HttpTransactionFactory>
140 proxy_script_fetcher_http_transaction_factory;
141 scoped_ptr<net::FtpTransactionFactory>
142 proxy_script_fetcher_ftp_transaction_factory;
143 scoped_ptr<net::URLRequestJobFactory>
144 proxy_script_fetcher_url_request_job_factory;
145 scoped_ptr<net::URLRequestThrottlerManager> throttler_manager;
146 scoped_ptr<net::URLSecurityManager> url_security_manager;
147 // TODO(willchan): Remove proxy script fetcher context since it's not
148 // necessary now that I got rid of refcounting URLRequestContexts.
150 // The first URLRequestContext is |system_url_request_context|. We introduce
151 // |proxy_script_fetcher_context| for the second context. It has a direct
152 // ProxyService, since we always directly connect to fetch the PAC script.
153 scoped_ptr<net::URLRequestContext> proxy_script_fetcher_context;
154 scoped_ptr<net::ProxyService> system_proxy_service;
155 scoped_ptr<net::HttpTransactionFactory> system_http_transaction_factory;
156 scoped_ptr<net::URLRequestJobFactory> system_url_request_job_factory;
157 scoped_ptr<net::URLRequestContext> system_request_context;
158 SystemRequestContextLeakChecker system_request_context_leak_checker;
159 // |system_cookie_store| and |system_channel_id_service| are shared
160 // between |proxy_script_fetcher_context| and |system_request_context|.
161 scoped_refptr<net::CookieStore> system_cookie_store;
162 #if defined(ENABLE_EXTENSIONS)
163 scoped_refptr<extensions::EventRouterForwarder>
164 extension_event_router_forwarder;
165 #endif
166 scoped_ptr<net::HostMappingRules> host_mapping_rules;
167 scoped_ptr<net::HttpUserAgentSettings> http_user_agent_settings;
168 bool enable_ssl_connect_job_waiting;
169 bool ignore_certificate_errors;
170 uint16 testing_fixed_http_port;
171 uint16 testing_fixed_https_port;
173 Optional<size_t> initial_max_spdy_concurrent_streams;
174 Optional<bool> force_spdy_single_domain;
175 Optional<bool> enable_spdy_compression;
176 Optional<bool> enable_spdy_ping_based_connection_checking;
177 Optional<net::NextProto> spdy_default_protocol;
178 net::NextProtoVector next_protos;
179 Optional<string> trusted_spdy_proxy;
180 Optional<bool> force_spdy_over_ssl;
181 Optional<bool> force_spdy_always;
182 std::set<net::HostPortPair> forced_spdy_exclusions;
183 Optional<bool> use_alternate_protocols;
184 Optional<double> alternate_protocol_probability_threshold;
185 Optional<bool> enable_websocket_over_spdy;
187 Optional<bool> enable_quic;
188 Optional<bool> enable_quic_time_based_loss_detection;
189 Optional<bool> enable_quic_port_selection;
190 Optional<size_t> quic_max_packet_length;
191 net::QuicTagVector quic_connection_options;
192 Optional<std::string> quic_user_agent_id;
193 Optional<net::QuicVersionVector> quic_supported_versions;
194 Optional<net::HostPortPair> origin_to_force_quic_on;
195 bool enable_user_alternate_protocol_ports;
196 // NetErrorTabHelper uses |dns_probe_service| to send DNS probes when a
197 // main frame load fails with a DNS error in order to provide more useful
198 // information to the renderer so it can show a more specific error page.
199 scoped_ptr<chrome_browser_net::DnsProbeService> dns_probe_service;
200 #if defined(SPDY_PROXY_AUTH_ORIGIN)
201 scoped_ptr<data_reduction_proxy::DataReductionProxyParams>
202 data_reduction_proxy_params;
203 scoped_ptr<data_reduction_proxy::DataReductionProxyAuthRequestHandler>
204 data_reduction_proxy_auth_request_handler;
205 #endif
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 virtual ~IOThread();
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 private:
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 virtual void Init() OVERRIDE;
257 virtual void InitAsync() OVERRIDE;
258 virtual 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 // Enable SPDY with the given mode, which may contain the following:
269 // "off" : Disables SPDY support entirely.
270 // "ssl" : Forces SPDY for all HTTPS requests.
271 // "no-ssl" : Forces SPDY for all HTTP requests.
272 // "no-ping" : Disables SPDY ping connection testing.
273 // "exclude=<host>" : Disables SPDY support for the host <host>.
274 // "no-compress" : Disables SPDY header compression.
275 // "no-alt-protocols : Disables alternate protocol support.
276 // "force-alt-protocols : Forces an alternate protocol of SPDY/3
277 // on port 443.
278 // "single-domain" : Forces all spdy traffic to a single domain.
279 // "init-max-streams=<limit>" : Specifies the maximum number of concurrent
280 // streams for a SPDY session, unless the
281 // specifies a different value via SETTINGS.
282 void EnableSpdy(const std::string& mode);
284 // Configures available SPDY protocol versions from the given trial.
285 // Used only if no command-line configuration was present.
286 static void ConfigureSpdyFromTrial(const std::string& spdy_trial_group,
287 Globals* globals);
289 // Global state must be initialized on the IO thread, then this
290 // method must be invoked on the UI thread.
291 void InitSystemRequestContext();
293 // Lazy initialization of system request context for
294 // SystemURLRequestContextGetter. To be called on IO thread only
295 // after global state has been initialized on the IO thread, and
296 // SystemRequestContext state has been initialized on the UI thread.
297 void InitSystemRequestContextOnIOThread();
299 net::HttpAuthHandlerFactory* CreateDefaultAuthHandlerFactory(
300 net::HostResolver* resolver);
302 // Returns an SSLConfigService instance.
303 net::SSLConfigService* GetSSLConfigService();
305 void ChangedToOnTheRecordOnIOThread();
307 void UpdateDnsClientEnabled();
309 // Configures QUIC options based on the flags in |command_line| as
310 // well as the QUIC field trial group.
311 void ConfigureQuic(const base::CommandLine& command_line);
313 extensions::EventRouterForwarder* extension_event_router_forwarder() {
314 #if defined(ENABLE_EXTENSIONS)
315 return extension_event_router_forwarder_;
316 #else
317 return NULL;
318 #endif
320 // Configures QUIC options in |globals| based on the flags in |command_line|
321 // as well as the QUIC field trial group and parameters.
322 static void ConfigureQuicGlobals(
323 const base::CommandLine& command_line,
324 base::StringPiece quic_trial_group,
325 const VariationParameters& quic_trial_params,
326 Globals* globals);
328 // Returns true if QUIC should be enabled, either as a result
329 // of a field trial or a command line flag.
330 static bool ShouldEnableQuic(
331 const base::CommandLine& command_line,
332 base::StringPiece quic_trial_group);
334 // Returns true if the selection of the ephemeral port in bind() should be
335 // performed by Chromium, and false if the OS should select the port. The OS
336 // option is used to prevent Windows from posting a security security warning
337 // dialog.
338 static bool ShouldEnableQuicPortSelection(
339 const base::CommandLine& command_line);
341 // Returns true if QUIC packet pacing should be negotiated during the
342 // QUIC handshake.
343 static bool ShouldEnableQuicPacing(
344 const base::CommandLine& command_line,
345 base::StringPiece quic_trial_group,
346 const VariationParameters& quic_trial_params);
348 // Returns true if QUIC time-base loss detection should be negotiated during
349 // the QUIC handshake.
350 static bool ShouldEnableQuicTimeBasedLossDetection(
351 const base::CommandLine& command_line,
352 base::StringPiece quic_trial_group,
353 const VariationParameters& quic_trial_params);
355 // Returns the maximum length for QUIC packets, based on any flags in
356 // |command_line| or the field trial. Returns 0 if there is an error
357 // parsing any of the options, or if the default value should be used.
358 static size_t GetQuicMaxPacketLength(
359 const base::CommandLine& command_line,
360 base::StringPiece quic_trial_group,
361 const VariationParameters& quic_trial_params);
363 // Returns the QUIC versions specified by any flags in |command_line|
364 // or |quic_trial_params|.
365 static net::QuicVersion GetQuicVersion(
366 const base::CommandLine& command_line,
367 const VariationParameters& quic_trial_params);
369 // Returns the QUIC version specified by |quic_version| or
370 // QUIC_VERSION_UNSUPPORTED if |quic_version| is invalid.
371 static net::QuicVersion ParseQuicVersion(const std::string& quic_version);
373 // Returns the QUIC connection options specified by any flags in
374 // |command_line| or |quic_trial_params|.
375 static net::QuicTagVector GetQuicConnectionOptions(
376 const base::CommandLine& command_line,
377 const VariationParameters& quic_trial_params);
379 // Returns the list of QUIC tags represented by the comma separated
380 // string in |connection_options|.
381 static net::QuicTagVector ParseQuicConnectionOptions(
382 const std::string& connection_options);
384 // Returns the alternate protocol probability threshold specified by
385 // any flags in |command_line| or |quic_trial_params|.
386 static double GetAlternateProtocolProbabilityThreshold(
387 const base::CommandLine& command_line,
388 const VariationParameters& quic_trial_params);
390 // The NetLog is owned by the browser process, to allow logging from other
391 // threads during shutdown, but is used most frequently on the IOThread.
392 ChromeNetLog* net_log_;
394 #if defined(ENABLE_EXTENSIONS)
395 // The extensions::EventRouterForwarder allows for sending events to
396 // extensions from the IOThread.
397 extensions::EventRouterForwarder* extension_event_router_forwarder_;
398 #endif
400 // These member variables are basically global, but their lifetimes are tied
401 // to the IOThread. IOThread owns them all, despite not using scoped_ptr.
402 // This is because the destructor of IOThread runs on the wrong thread. All
403 // member variables should be deleted in CleanUp().
405 // These member variables are initialized in Init() and do not change for the
406 // lifetime of the IO thread.
408 Globals* globals_;
410 // Observer that logs network changes to the ChromeNetLog.
411 class LoggingNetworkChangeObserver;
412 scoped_ptr<LoggingNetworkChangeObserver> network_change_observer_;
414 BooleanPrefMember system_enable_referrers_;
416 BooleanPrefMember dns_client_enabled_;
418 BooleanPrefMember quick_check_enabled_;
420 // Store HTTP Auth-related policies in this thread.
421 std::string auth_schemes_;
422 bool negotiate_disable_cname_lookup_;
423 bool negotiate_enable_port_;
424 std::string auth_server_whitelist_;
425 std::string auth_delegate_whitelist_;
426 std::string gssapi_library_name_;
428 // This is an instance of the default SSLConfigServiceManager for the current
429 // platform and it gets SSL preferences from local_state object.
430 scoped_ptr<SSLConfigServiceManager> ssl_config_service_manager_;
432 // These member variables are initialized by a task posted to the IO thread,
433 // which gets posted by calling certain member functions of IOThread.
434 scoped_ptr<net::ProxyConfigService> system_proxy_config_service_;
436 scoped_ptr<PrefProxyConfigTracker> pref_proxy_config_tracker_;
438 scoped_refptr<net::URLRequestContextGetter>
439 system_url_request_context_getter_;
441 // True if SPDY is disabled by policy.
442 bool is_spdy_disabled_by_policy_;
444 base::WeakPtrFactory<IOThread> weak_factory_;
446 const base::TimeTicks creation_time_;
448 DISALLOW_COPY_AND_ASSIGN(IOThread);
451 #endif // CHROME_BROWSER_IO_THREAD_H_