Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / io_thread.h
blobef742bfd49a0febbdb40946d72507f2f1f72057a
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 "chrome/browser/net/ssl_config_service_manager.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/browser/browser_thread_delegate.h"
20 #include "net/base/network_change_notifier.h"
21 #include "net/http/http_network_session.h"
22 #include "net/socket/next_proto.h"
24 class ChromeNetLog;
25 class CommandLine;
26 class PrefProxyConfigTracker;
27 class PrefService;
28 class PrefRegistrySimple;
29 class SystemURLRequestContextGetter;
31 namespace chrome_browser_net {
32 class DnsProbeService;
33 class HttpPipeliningCompatibilityClient;
36 namespace extensions {
37 class EventRouterForwarder;
40 namespace net {
41 class CertVerifier;
42 class CookieStore;
43 class CTVerifier;
44 class FtpTransactionFactory;
45 class HostMappingRules;
46 class HostResolver;
47 class HttpAuthHandlerFactory;
48 class HttpServerProperties;
49 class HttpTransactionFactory;
50 class HttpUserAgentSettings;
51 class NetworkDelegate;
52 class NetworkTimeNotifier;
53 class ServerBoundCertService;
54 class ProxyConfigService;
55 class ProxyService;
56 class SdchManager;
57 class SSLConfigService;
58 class TransportSecurityState;
59 class URLRequestContext;
60 class URLRequestContextGetter;
61 class URLRequestJobFactory;
62 class URLRequestThrottlerManager;
63 class URLSecurityManager;
64 } // namespace net
66 namespace policy {
67 class PolicyService;
68 } // namespace policy
70 // Contains state associated with, initialized and cleaned up on, and
71 // primarily used on, the IO thread.
73 // If you are looking to interact with the IO thread (e.g. post tasks
74 // to it or check if it is the current thread), see
75 // content::BrowserThread.
76 class IOThread : public content::BrowserThreadDelegate {
77 public:
78 struct Globals {
79 template <typename T>
80 class Optional {
81 public:
82 Optional() : set_(false) {}
84 void set(T value) {
85 set_ = true;
86 value_ = value;
88 void CopyToIfSet(T* value) {
89 if (set_) {
90 *value = value_;
94 private:
95 bool set_;
96 T value_;
99 class SystemRequestContextLeakChecker {
100 public:
101 explicit SystemRequestContextLeakChecker(Globals* globals);
102 ~SystemRequestContextLeakChecker();
104 private:
105 Globals* const globals_;
108 Globals();
109 ~Globals();
111 // The "system" NetworkDelegate, used for Profile-agnostic network events.
112 scoped_ptr<net::NetworkDelegate> system_network_delegate;
113 scoped_ptr<net::HostResolver> host_resolver;
114 scoped_ptr<net::CertVerifier> cert_verifier;
115 // The ServerBoundCertService must outlive the HttpTransactionFactory.
116 scoped_ptr<net::ServerBoundCertService> system_server_bound_cert_service;
117 // This TransportSecurityState doesn't load or save any state. It's only
118 // used to enforce pinning for system requests and will only use built-in
119 // pins.
120 scoped_ptr<net::TransportSecurityState> transport_security_state;
121 scoped_ptr<net::CTVerifier> cert_transparency_verifier;
122 scoped_refptr<net::SSLConfigService> ssl_config_service;
123 scoped_ptr<net::HttpAuthHandlerFactory> http_auth_handler_factory;
124 scoped_ptr<net::HttpServerProperties> http_server_properties;
125 scoped_ptr<net::ProxyService> proxy_script_fetcher_proxy_service;
126 scoped_ptr<net::HttpTransactionFactory>
127 proxy_script_fetcher_http_transaction_factory;
128 scoped_ptr<net::FtpTransactionFactory>
129 proxy_script_fetcher_ftp_transaction_factory;
130 scoped_ptr<net::URLRequestJobFactory>
131 proxy_script_fetcher_url_request_job_factory;
132 scoped_ptr<net::URLRequestThrottlerManager> throttler_manager;
133 scoped_ptr<net::URLSecurityManager> url_security_manager;
134 // TODO(willchan): Remove proxy script fetcher context since it's not
135 // necessary now that I got rid of refcounting URLRequestContexts.
137 // The first URLRequestContext is |system_url_request_context|. We introduce
138 // |proxy_script_fetcher_context| for the second context. It has a direct
139 // ProxyService, since we always directly connect to fetch the PAC script.
140 scoped_ptr<net::URLRequestContext> proxy_script_fetcher_context;
141 scoped_ptr<net::ProxyService> system_proxy_service;
142 scoped_ptr<net::HttpTransactionFactory> system_http_transaction_factory;
143 scoped_ptr<net::URLRequestContext> system_request_context;
144 SystemRequestContextLeakChecker system_request_context_leak_checker;
145 // |system_cookie_store| and |system_server_bound_cert_service| are shared
146 // between |proxy_script_fetcher_context| and |system_request_context|.
147 scoped_refptr<net::CookieStore> system_cookie_store;
148 scoped_refptr<extensions::EventRouterForwarder>
149 extension_event_router_forwarder;
150 scoped_ptr<chrome_browser_net::HttpPipeliningCompatibilityClient>
151 http_pipelining_compatibility_client;
152 scoped_ptr<net::HostMappingRules> host_mapping_rules;
153 scoped_ptr<net::HttpUserAgentSettings> http_user_agent_settings;
154 bool ignore_certificate_errors;
155 bool http_pipelining_enabled;
156 uint16 testing_fixed_http_port;
157 uint16 testing_fixed_https_port;
158 Optional<size_t> initial_max_spdy_concurrent_streams;
159 Optional<size_t> max_spdy_concurrent_streams_limit;
160 Optional<bool> force_spdy_single_domain;
161 Optional<bool> enable_spdy_ip_pooling;
162 Optional<bool> enable_spdy_compression;
163 Optional<bool> enable_spdy_ping_based_connection_checking;
164 Optional<net::NextProto> spdy_default_protocol;
165 Optional<string> trusted_spdy_proxy;
166 Optional<bool> enable_quic;
167 Optional<bool> enable_quic_https;
168 Optional<size_t> quic_max_packet_length;
169 Optional<net::QuicVersionVector> quic_supported_versions;
170 Optional<net::HostPortPair> origin_to_force_quic_on;
171 bool enable_user_alternate_protocol_ports;
172 // NetErrorTabHelper uses |dns_probe_service| to send DNS probes when a
173 // main frame load fails with a DNS error in order to provide more useful
174 // information to the renderer so it can show a more specific error page.
175 scoped_ptr<chrome_browser_net::DnsProbeService> dns_probe_service;
176 scoped_ptr<net::NetworkTimeNotifier> network_time_notifier;
179 // |net_log| must either outlive the IOThread or be NULL.
180 IOThread(PrefService* local_state,
181 policy::PolicyService* policy_service,
182 ChromeNetLog* net_log,
183 extensions::EventRouterForwarder* extension_event_router_forwarder);
185 virtual ~IOThread();
187 static void RegisterPrefs(PrefRegistrySimple* registry);
189 // Can only be called on the IO thread.
190 Globals* globals();
192 // Allows overriding Globals in tests where IOThread::Init() and
193 // IOThread::CleanUp() are not called. This allows for injecting mocks into
194 // IOThread global objects.
195 void SetGlobalsForTesting(Globals* globals);
197 ChromeNetLog* net_log();
199 // Handles changing to On The Record mode, discarding confidential data.
200 void ChangedToOnTheRecord();
202 // Returns a getter for the URLRequestContext. Only called on the UI thread.
203 net::URLRequestContextGetter* system_url_request_context_getter();
205 // Clears the host cache. Intended to be used to prevent exposing recently
206 // visited sites on about:net-internals/#dns and about:dns pages. Must be
207 // called on the IO thread.
208 void ClearHostCache();
210 void InitializeNetworkSessionParams(net::HttpNetworkSession::Params* params);
212 private:
213 // Provide SystemURLRequestContextGetter with access to
214 // InitSystemRequestContext().
215 friend class SystemURLRequestContextGetter;
217 // BrowserThreadDelegate implementation, runs on the IO thread.
218 // This handles initialization and destruction of state that must
219 // live on the IO thread.
220 virtual void Init() OVERRIDE;
221 virtual void InitAsync() OVERRIDE;
222 virtual void CleanUp() OVERRIDE;
224 void InitializeNetworkOptions(const CommandLine& parsed_command_line);
226 // Enable SPDY with the given mode, which may contain the following:
228 // "off" : Disables SPDY support entirely.
229 // "ssl" : Forces SPDY for all HTTPS requests.
230 // "no-ssl" : Forces SPDY for all HTTP requests.
231 // "no-ping" : Disables SPDY ping connection testing.
232 // "exclude=<host>" : Disables SPDY support for the host <host>.
233 // "no-compress" : Disables SPDY header compression.
234 // "no-alt-protocols : Disables alternate protocol support.
235 // "force-alt-protocols : Forces an alternate protocol of SPDY/3
236 // on port 443.
237 // "single-domain" : Forces all spdy traffic to a single domain.
238 // "init-max-streams=<limit>" : Specifies the maximum number of concurrent
239 // streams for a SPDY session, unless the
240 // specifies a different value via SETTINGS.
241 void EnableSpdy(const std::string& mode);
243 // Global state must be initialized on the IO thread, then this
244 // method must be invoked on the UI thread.
245 void InitSystemRequestContext();
247 // Lazy initialization of system request context for
248 // SystemURLRequestContextGetter. To be called on IO thread only
249 // after global state has been initialized on the IO thread, and
250 // SystemRequestContext state has been initialized on the UI thread.
251 void InitSystemRequestContextOnIOThread();
253 net::HttpAuthHandlerFactory* CreateDefaultAuthHandlerFactory(
254 net::HostResolver* resolver);
256 // Returns an SSLConfigService instance.
257 net::SSLConfigService* GetSSLConfigService();
259 void ChangedToOnTheRecordOnIOThread();
261 void UpdateDnsClientEnabled();
263 // Configures QUIC options based on the flags in |command_line| as
264 // well as the QUIC field trial group.
265 void ConfigureQuic(const CommandLine& command_line);
267 // Returns true if QUIC should be enabled, either as a result
268 // of a field trial or a command line flag.
269 bool ShouldEnableQuic(const CommandLine& command_line,
270 base::StringPiece quic_trial_group);
272 // Returns true if HTTPS over QUIC should be enabled, either as a result
273 // of a field trial or a command line flag.
274 bool ShouldEnableQuicHttps(const CommandLine& command_line,
275 base::StringPiece quic_trial_group);
277 // Returns the maximum length for QUIC packets, based on any flags in
278 // |command_line| or the field trial. Returns 0 if there is an error
279 // parsing any of the options, or if the default value should be used.
280 size_t GetQuicMaxPacketLength(const CommandLine& command_line,
281 base::StringPiece quic_trial_group);
283 // Returns the quic versions specified by any flags in |command_line|.
284 net::QuicVersion GetQuicVersion(const CommandLine& command_line);
286 // The NetLog is owned by the browser process, to allow logging from other
287 // threads during shutdown, but is used most frequently on the IOThread.
288 ChromeNetLog* net_log_;
290 // The extensions::EventRouterForwarder allows for sending events to
291 // extensions from the IOThread.
292 extensions::EventRouterForwarder* extension_event_router_forwarder_;
294 // These member variables are basically global, but their lifetimes are tied
295 // to the IOThread. IOThread owns them all, despite not using scoped_ptr.
296 // This is because the destructor of IOThread runs on the wrong thread. All
297 // member variables should be deleted in CleanUp().
299 // These member variables are initialized in Init() and do not change for the
300 // lifetime of the IO thread.
302 Globals* globals_;
304 // Observer that logs network changes to the ChromeNetLog.
305 class LoggingNetworkChangeObserver;
306 scoped_ptr<LoggingNetworkChangeObserver> network_change_observer_;
308 BooleanPrefMember system_enable_referrers_;
310 BooleanPrefMember dns_client_enabled_;
312 BooleanPrefMember quick_check_enabled_;
314 // Store HTTP Auth-related policies in this thread.
315 std::string auth_schemes_;
316 bool negotiate_disable_cname_lookup_;
317 bool negotiate_enable_port_;
318 std::string auth_server_whitelist_;
319 std::string auth_delegate_whitelist_;
320 std::string gssapi_library_name_;
321 std::vector<GURL> spdyproxy_auth_origins_;
323 // This is an instance of the default SSLConfigServiceManager for the current
324 // platform and it gets SSL preferences from local_state object.
325 scoped_ptr<SSLConfigServiceManager> ssl_config_service_manager_;
327 // These member variables are initialized by a task posted to the IO thread,
328 // which gets posted by calling certain member functions of IOThread.
329 scoped_ptr<net::ProxyConfigService> system_proxy_config_service_;
331 scoped_ptr<PrefProxyConfigTracker> pref_proxy_config_tracker_;
333 scoped_refptr<net::URLRequestContextGetter>
334 system_url_request_context_getter_;
336 net::SdchManager* sdch_manager_;
338 // True if SPDY is disabled by policy.
339 bool is_spdy_disabled_by_policy_;
341 base::WeakPtrFactory<IOThread> weak_factory_;
343 DISALLOW_COPY_AND_ASSIGN(IOThread);
346 #endif // CHROME_BROWSER_IO_THREAD_H_