1// Copyright (c) 2011 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#pragma once
8
9#include <list>
10#include <string>
11#include "base/basictypes.h"
12#include "base/memory/ref_counted.h"
13#include "base/memory/scoped_ptr.h"
14#include "chrome/browser/browser_process_sub_thread.h"
15#include "chrome/browser/prefs/pref_member.h"
16#include "chrome/common/net/predictor_common.h"
17#include "net/base/network_change_notifier.h"
18
19class ChromeNetLog;
20class ChromeURLRequestContextGetter;
21class ExtensionEventRouterForwarder;
22class ListValue;
23class PrefProxyConfigTracker;
24class PrefService;
25class SystemURLRequestContextGetter;
26
27namespace chrome_browser_net {
28class ConnectInterceptor;
29class Predictor;
30}  // namespace chrome_browser_net
31
32namespace net {
33class CertVerifier;
34class DnsRRResolver;
35class FtpTransactionFactory;
36class HostResolver;
37class HttpAuthHandlerFactory;
38class HttpTransactionFactory;
39class NetworkDelegate;
40class ProxyConfigService;
41class ProxyScriptFetcher;
42class ProxyService;
43class SSLConfigService;
44class URLRequestContext;
45class URLRequestContextGetter;
46class URLSecurityManager;
47}  // namespace net
48
49class IOThread : public BrowserProcessSubThread {
50 public:
51  struct Globals {
52    Globals();
53    ~Globals();
54
55    // The "system" NetworkDelegate, used for Profile-agnostic network events.
56    scoped_ptr<net::NetworkDelegate> system_network_delegate;
57    scoped_ptr<net::HostResolver> host_resolver;
58    scoped_ptr<net::CertVerifier> cert_verifier;
59    scoped_ptr<net::DnsRRResolver> dnsrr_resolver;
60    scoped_refptr<net::SSLConfigService> ssl_config_service;
61    scoped_ptr<net::HttpAuthHandlerFactory> http_auth_handler_factory;
62    scoped_refptr<net::ProxyService> proxy_script_fetcher_proxy_service;
63    scoped_ptr<net::HttpTransactionFactory>
64        proxy_script_fetcher_http_transaction_factory;
65    scoped_ptr<net::FtpTransactionFactory>
66        proxy_script_fetcher_ftp_transaction_factory;
67    scoped_ptr<net::URLSecurityManager> url_security_manager;
68    scoped_refptr<net::URLRequestContext> proxy_script_fetcher_context;
69    scoped_ptr<net::HttpTransactionFactory> system_http_transaction_factory;
70    scoped_ptr<net::FtpTransactionFactory> system_ftp_transaction_factory;
71    scoped_refptr<net::ProxyService> system_proxy_service;
72    // NOTE(willchan): This request context is unusable until a system
73    // SSLConfigService is provided that doesn't rely on
74    // Profiles. Do NOT use this yet.
75    scoped_refptr<net::URLRequestContext> system_request_context;
76    scoped_refptr<ExtensionEventRouterForwarder>
77        extension_event_router_forwarder;
78  };
79
80  // |net_log| must either outlive the IOThread or be NULL.
81  IOThread(PrefService* local_state,
82           ChromeNetLog* net_log,
83           ExtensionEventRouterForwarder* extension_event_router_forwarder);
84
85  virtual ~IOThread();
86
87  // Can only be called on the IO thread.
88  Globals* globals();
89
90  ChromeNetLog* net_log();
91
92  // Initializes the network predictor, which induces DNS pre-resolution and/or
93  // TCP/IP preconnections.  |prefetching_enabled| indicates whether or not DNS
94  // prefetching should be enabled, and |preconnect_enabled| controls whether
95  // TCP/IP preconnection is enabled.  This should be called by the UI thread.
96  // It will post a task to the IO thread to perform the actual initialization.
97  void InitNetworkPredictor(bool prefetching_enabled,
98                            base::TimeDelta max_dns_queue_delay,
99                            size_t max_speculative_parallel_resolves,
100                            const chrome_common_net::UrlList& startup_urls,
101                            ListValue* referral_list,
102                            bool preconnect_enabled);
103
104  // Registers |url_request_context_getter| into the IO thread.  During
105  // IOThread::CleanUp(), IOThread will iterate through known getters and
106  // release their URLRequestContexts.  Only called on the IO thread.  It does
107  // not acquire a refcount for |url_request_context_getter|.  If
108  // |url_request_context_getter| is being deleted before IOThread::CleanUp() is
109  // invoked, then this needs to be balanced with a call to
110  // UnregisterURLRequestContextGetter().
111  void RegisterURLRequestContextGetter(
112      ChromeURLRequestContextGetter* url_request_context_getter);
113
114  // Unregisters |url_request_context_getter| from the IO thread.  Only called
115  // on the IO thread.
116  void UnregisterURLRequestContextGetter(
117      ChromeURLRequestContextGetter* url_request_context_getter);
118
119  // Handles changing to On The Record mode, discarding confidential data.
120  void ChangedToOnTheRecord();
121
122  // Returns a getter for the URLRequestContext.  Only called on the UI thread.
123  net::URLRequestContextGetter* system_url_request_context_getter();
124
125  // Clear all network stack history, including the host cache, as well as
126  // speculative data about subresources of visited sites, and startup-time
127  // navigations.
128  void ClearNetworkingHistory();
129
130 protected:
131  virtual void Init();
132  virtual void CleanUp();
133
134 private:
135  // Provide SystemURLRequestContextGetter with access to
136  // InitSystemRequestContext().
137  friend class SystemURLRequestContextGetter;
138
139  static void RegisterPrefs(PrefService* local_state);
140
141  net::HttpAuthHandlerFactory* CreateDefaultAuthHandlerFactory(
142      net::HostResolver* resolver);
143
144  // Lazy initialization of system request context for
145  // SystemURLRequestContextGetter. To be called on IO thread.
146  void InitSystemRequestContext();
147
148  void InitNetworkPredictorOnIOThread(
149      bool prefetching_enabled,
150      base::TimeDelta max_dns_queue_delay,
151      size_t max_speculative_parallel_resolves,
152      const chrome_common_net::UrlList& startup_urls,
153      ListValue* referral_list,
154      bool preconnect_enabled);
155
156  void ChangedToOnTheRecordOnIOThread();
157
158  // Clears the host cache.  Intended to be used to prevent exposing recently
159  // visited sites on about:net-internals/#dns and about:dns pages.  Must be
160  // called on the IO thread.
161  void ClearHostCache();
162
163  // The NetLog is owned by the browser process, to allow logging from other
164  // threads during shutdown, but is used most frequently on the IOThread.
165  ChromeNetLog* net_log_;
166
167  // The ExtensionEventRouterForwarder allows for sending events to extensions
168  // from the IOThread.
169  ExtensionEventRouterForwarder* extension_event_router_forwarder_;
170
171  // These member variables are basically global, but their lifetimes are tied
172  // to the IOThread.  IOThread owns them all, despite not using scoped_ptr.
173  // This is because the destructor of IOThread runs on the wrong thread.  All
174  // member variables should be deleted in CleanUp().
175
176  // These member variables are initialized in Init() and do not change for the
177  // lifetime of the IO thread.
178
179  Globals* globals_;
180
181  // Observer that logs network changes to the ChromeNetLog.
182  scoped_ptr<net::NetworkChangeNotifier::IPAddressObserver>
183      network_change_observer_;
184
185  BooleanPrefMember system_enable_referrers_;
186
187  // Store HTTP Auth-related policies in this thread.
188  std::string auth_schemes_;
189  bool negotiate_disable_cname_lookup_;
190  bool negotiate_enable_port_;
191  std::string auth_server_whitelist_;
192  std::string auth_delegate_whitelist_;
193  std::string gssapi_library_name_;
194
195  // These member variables are initialized by a task posted to the IO thread,
196  // which gets posted by calling certain member functions of IOThread.
197
198  // Note: we user explicit pointers rather than smart pointers to be more
199  // explicit about destruction order, and ensure that there is no chance that
200  // these observers would be used accidentally after we have begun to tear
201  // down.
202  chrome_browser_net::ConnectInterceptor* speculative_interceptor_;
203  chrome_browser_net::Predictor* predictor_;
204
205  scoped_ptr<net::ProxyConfigService> system_proxy_config_service_;
206
207  scoped_refptr<PrefProxyConfigTracker> pref_proxy_config_tracker_;
208
209  scoped_refptr<net::URLRequestContextGetter>
210      system_url_request_context_getter_;
211
212  // Keeps track of all live ChromeURLRequestContextGetters, so the
213  // ChromeURLRequestContexts can be released during
214  // IOThread::CleanUp().
215  std::list<ChromeURLRequestContextGetter*> url_request_context_getters_;
216
217  DISALLOW_COPY_AND_ASSIGN(IOThread);
218};
219
220#endif  // CHROME_BROWSER_IO_THREAD_H_
221