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