- add sources.
[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 "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"
23
24 class ChromeNetLog;
25 class CommandLine;
26 class PrefProxyConfigTracker;
27 class PrefService;
28 class PrefRegistrySimple;
29 class SystemURLRequestContextGetter;
30
31 namespace chrome_browser_net {
32 class DnsProbeService;
33 class HttpPipeliningCompatibilityClient;
34 class LoadTimeStats;
35 }
36
37 namespace extensions {
38 class EventRouterForwarder;
39 }
40
41 namespace net {
42 class CertVerifier;
43 class CookieStore;
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
65
66 namespace policy {
67 class PolicyService;
68 }  // namespace policy
69
70 // Contains state associated with, initialized and cleaned up on, and
71 // primarily used on, the IO thread.
72 //
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) {}
83
84       void set(T value) {
85         set_ = true;
86         value_ = value;
87       }
88       void CopyToIfSet(T* value) {
89         if (set_) {
90           *value = value_;
91         }
92       }
93
94      private:
95       bool set_;
96       T value_;
97     };
98
99     class SystemRequestContextLeakChecker {
100      public:
101       explicit SystemRequestContextLeakChecker(Globals* globals);
102       ~SystemRequestContextLeakChecker();
103
104      private:
105       Globals* const globals_;
106     };
107
108     Globals();
109     ~Globals();
110
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_refptr<net::SSLConfigService> ssl_config_service;
122     scoped_ptr<net::HttpAuthHandlerFactory> http_auth_handler_factory;
123     scoped_ptr<net::HttpServerProperties> http_server_properties;
124     scoped_ptr<net::ProxyService> proxy_script_fetcher_proxy_service;
125     scoped_ptr<net::HttpTransactionFactory>
126         proxy_script_fetcher_http_transaction_factory;
127     scoped_ptr<net::FtpTransactionFactory>
128         proxy_script_fetcher_ftp_transaction_factory;
129     scoped_ptr<net::URLRequestJobFactory>
130         proxy_script_fetcher_url_request_job_factory;
131     scoped_ptr<net::URLRequestThrottlerManager> throttler_manager;
132     scoped_ptr<net::URLSecurityManager> url_security_manager;
133     // TODO(willchan): Remove proxy script fetcher context since it's not
134     // necessary now that I got rid of refcounting URLRequestContexts.
135     //
136     // The first URLRequestContext is |system_url_request_context|. We introduce
137     // |proxy_script_fetcher_context| for the second context. It has a direct
138     // ProxyService, since we always directly connect to fetch the PAC script.
139     scoped_ptr<net::URLRequestContext> proxy_script_fetcher_context;
140     scoped_ptr<net::ProxyService> system_proxy_service;
141     scoped_ptr<net::HttpTransactionFactory> system_http_transaction_factory;
142     scoped_ptr<net::URLRequestContext> system_request_context;
143     SystemRequestContextLeakChecker system_request_context_leak_checker;
144     // |system_cookie_store| and |system_server_bound_cert_service| are shared
145     // between |proxy_script_fetcher_context| and |system_request_context|.
146     scoped_refptr<net::CookieStore> system_cookie_store;
147     scoped_refptr<extensions::EventRouterForwarder>
148         extension_event_router_forwarder;
149     scoped_ptr<chrome_browser_net::HttpPipeliningCompatibilityClient>
150         http_pipelining_compatibility_client;
151     scoped_ptr<chrome_browser_net::LoadTimeStats> load_time_stats;
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<net::HostPortPair> origin_to_force_quic_on;
169     bool enable_user_alternate_protocol_ports;
170     // NetErrorTabHelper uses |dns_probe_service| to send DNS probes when a
171     // main frame load fails with a DNS error in order to provide more useful
172     // information to the renderer so it can show a more specific error page.
173     scoped_ptr<chrome_browser_net::DnsProbeService> dns_probe_service;
174     scoped_ptr<net::NetworkTimeNotifier> network_time_notifier;
175   };
176
177   // |net_log| must either outlive the IOThread or be NULL.
178   IOThread(PrefService* local_state,
179            policy::PolicyService* policy_service,
180            ChromeNetLog* net_log,
181            extensions::EventRouterForwarder* extension_event_router_forwarder);
182
183   virtual ~IOThread();
184
185   static void RegisterPrefs(PrefRegistrySimple* registry);
186
187   // Can only be called on the IO thread.
188   Globals* globals();
189
190   // Allows overriding Globals in tests where IOThread::Init() and
191   // IOThread::CleanUp() are not called.  This allows for injecting mocks into
192   // IOThread global objects.
193   void SetGlobalsForTesting(Globals* globals);
194
195   ChromeNetLog* net_log();
196
197   // Handles changing to On The Record mode, discarding confidential data.
198   void ChangedToOnTheRecord();
199
200   // Returns a getter for the URLRequestContext.  Only called on the UI thread.
201   net::URLRequestContextGetter* system_url_request_context_getter();
202
203   // Clears the host cache.  Intended to be used to prevent exposing recently
204   // visited sites on about:net-internals/#dns and about:dns pages.  Must be
205   // called on the IO thread.
206   void ClearHostCache();
207
208   void InitializeNetworkSessionParams(net::HttpNetworkSession::Params* params);
209
210  private:
211   // Provide SystemURLRequestContextGetter with access to
212   // InitSystemRequestContext().
213   friend class SystemURLRequestContextGetter;
214
215   // BrowserThreadDelegate implementation, runs on the IO thread.
216   // This handles initialization and destruction of state that must
217   // live on the IO thread.
218   virtual void Init() OVERRIDE;
219   virtual void InitAsync() OVERRIDE;
220   virtual void CleanUp() OVERRIDE;
221
222   void InitializeNetworkOptions(const CommandLine& parsed_command_line);
223
224   // Enable SPDY with the given mode, which may contain the following:
225   //
226   //   "off"                      : Disables SPDY support entirely.
227   //   "ssl"                      : Forces SPDY for all HTTPS requests.
228   //   "no-ssl"                   : Forces SPDY for all HTTP requests.
229   //   "no-ping"                  : Disables SPDY ping connection testing.
230   //   "exclude=<host>"           : Disables SPDY support for the host <host>.
231   //   "no-compress"              : Disables SPDY header compression.
232   //   "no-alt-protocols          : Disables alternate protocol support.
233   //   "force-alt-protocols       : Forces an alternate protocol of SPDY/3
234   //                                on port 443.
235   //   "single-domain"            : Forces all spdy traffic to a single domain.
236   //   "init-max-streams=<limit>" : Specifies the maximum number of concurrent
237   //                                streams for a SPDY session, unless the
238   //                                specifies a different value via SETTINGS.
239   void EnableSpdy(const std::string& mode);
240
241   // Global state must be initialized on the IO thread, then this
242   // method must be invoked on the UI thread.
243   void InitSystemRequestContext();
244
245   // Lazy initialization of system request context for
246   // SystemURLRequestContextGetter. To be called on IO thread only
247   // after global state has been initialized on the IO thread, and
248   // SystemRequestContext state has been initialized on the UI thread.
249   void InitSystemRequestContextOnIOThread();
250
251   net::HttpAuthHandlerFactory* CreateDefaultAuthHandlerFactory(
252       net::HostResolver* resolver);
253
254   // Returns an SSLConfigService instance.
255   net::SSLConfigService* GetSSLConfigService();
256
257   void ChangedToOnTheRecordOnIOThread();
258
259   void UpdateDnsClientEnabled();
260
261   // Returns true if QUIC should be enabled, either as a result
262   // of a field trial or a command line flag.
263   bool ShouldEnableQuic(const CommandLine& command_line);
264
265   // Returns true if HTTPS over QUIC should be enabled, either as a result
266   // of a field trial or a command line flag.
267   bool ShouldEnableQuicHttps(const CommandLine& command_line);
268
269   // The NetLog is owned by the browser process, to allow logging from other
270   // threads during shutdown, but is used most frequently on the IOThread.
271   ChromeNetLog* net_log_;
272
273   // The extensions::EventRouterForwarder allows for sending events to
274   // extensions from the IOThread.
275   extensions::EventRouterForwarder* extension_event_router_forwarder_;
276
277   // These member variables are basically global, but their lifetimes are tied
278   // to the IOThread.  IOThread owns them all, despite not using scoped_ptr.
279   // This is because the destructor of IOThread runs on the wrong thread.  All
280   // member variables should be deleted in CleanUp().
281
282   // These member variables are initialized in Init() and do not change for the
283   // lifetime of the IO thread.
284
285   Globals* globals_;
286
287   // Observer that logs network changes to the ChromeNetLog.
288   class LoggingNetworkChangeObserver;
289   scoped_ptr<LoggingNetworkChangeObserver> network_change_observer_;
290
291   BooleanPrefMember system_enable_referrers_;
292
293   BooleanPrefMember dns_client_enabled_;
294
295   // Store HTTP Auth-related policies in this thread.
296   std::string auth_schemes_;
297   bool negotiate_disable_cname_lookup_;
298   bool negotiate_enable_port_;
299   std::string auth_server_whitelist_;
300   std::string auth_delegate_whitelist_;
301   std::string gssapi_library_name_;
302   std::vector<GURL> spdyproxy_auth_origins_;
303
304   // This is an instance of the default SSLConfigServiceManager for the current
305   // platform and it gets SSL preferences from local_state object.
306   scoped_ptr<SSLConfigServiceManager> ssl_config_service_manager_;
307
308   // These member variables are initialized by a task posted to the IO thread,
309   // which gets posted by calling certain member functions of IOThread.
310   scoped_ptr<net::ProxyConfigService> system_proxy_config_service_;
311
312   scoped_ptr<PrefProxyConfigTracker> pref_proxy_config_tracker_;
313
314   scoped_refptr<net::URLRequestContextGetter>
315       system_url_request_context_getter_;
316
317   net::SdchManager* sdch_manager_;
318
319   // True if SPDY is disabled by policy.
320   bool is_spdy_disabled_by_policy_;
321
322   base::WeakPtrFactory<IOThread> weak_factory_;
323
324   DISALLOW_COPY_AND_ASSIGN(IOThread);
325 };
326
327 #endif  // CHROME_BROWSER_IO_THREAD_H_