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 10 #include "base/basictypes.h" 11 #include "base/compiler_specific.h" 12 #include "base/memory/ref_counted.h" 13 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/weak_ptr.h" 15 #include "base/prefs/pref_member.h" 16 #include "chrome/browser/net/ssl_config_service_manager.h" 17 #include "content/public/browser/browser_thread.h" 18 #include "content/public/browser/browser_thread_delegate.h" 19 #include "net/base/network_change_notifier.h" 20 #include "net/http/http_network_session.h" 21 #include "net/socket/next_proto.h" 22 23 class ChromeNetLog; 24 class CommandLine; 25 class PrefProxyConfigTracker; 26 class PrefService; 27 class PrefRegistrySimple; 28 class SystemURLRequestContextGetter; 29 30 namespace chrome_browser_net { 31 class DnsProbeService; 32 class HttpPipeliningCompatibilityClient; 33 class LoadTimeStats; 34 } 35 36 namespace extensions { 37 class EventRouterForwarder; 38 } 39 40 namespace net { 41 class CertVerifier; 42 class CookieStore; 43 class FtpTransactionFactory; 44 class HostMappingRules; 45 class HostResolver; 46 class HttpAuthHandlerFactory; 47 class HttpServerProperties; 48 class HttpTransactionFactory; 49 class HttpUserAgentSettings; 50 class NetworkDelegate; 51 class NetworkTimeNotifier; 52 class ServerBoundCertService; 53 class ProxyConfigService; 54 class ProxyService; 55 class SdchManager; 56 class SSLConfigService; 57 class TransportSecurityState; 58 class URLRequestContext; 59 class URLRequestContextGetter; 60 class URLRequestJobFactory; 61 class URLRequestThrottlerManager; 62 class URLSecurityManager; 63 } // namespace net 64 65 namespace policy { 66 class PolicyService; 67 } // namespace policy 68 69 // Contains state associated with, initialized and cleaned up on, and 70 // primarily used on, the IO thread. 71 // 72 // If you are looking to interact with the IO thread (e.g. post tasks 73 // to it or check if it is the current thread), see 74 // content::BrowserThread. 75 class IOThread : public content::BrowserThreadDelegate { 76 public: 77 struct Globals { 78 template <typename T> 79 class Optional { 80 public: 81 Optional() : set_(false) {} 82 83 void set(T value) { 84 set_ = true; 85 value_ = value; 86 } 87 void CopyToIfSet(T* value) { 88 if (set_) { 89 *value = value_; 90 } 91 } 92 93 private: 94 bool set_; 95 T value_; 96 }; 97 98 class SystemRequestContextLeakChecker { 99 public: 100 explicit SystemRequestContextLeakChecker(Globals* globals); 101 ~SystemRequestContextLeakChecker(); 102 103 private: 104 Globals* const globals_; 105 }; 106 107 Globals(); 108 ~Globals(); 109 110 // The "system" NetworkDelegate, used for Profile-agnostic network events. 111 scoped_ptr<net::NetworkDelegate> system_network_delegate; 112 scoped_ptr<net::HostResolver> host_resolver; 113 scoped_ptr<net::CertVerifier> cert_verifier; 114 // The ServerBoundCertService must outlive the HttpTransactionFactory. 115 scoped_ptr<net::ServerBoundCertService> system_server_bound_cert_service; 116 // This TransportSecurityState doesn't load or save any state. It's only 117 // used to enforce pinning for system requests and will only use built-in 118 // pins. 119 scoped_ptr<net::TransportSecurityState> transport_security_state; 120 scoped_refptr<net::SSLConfigService> ssl_config_service; 121 scoped_ptr<net::HttpAuthHandlerFactory> http_auth_handler_factory; 122 scoped_ptr<net::HttpServerProperties> http_server_properties; 123 scoped_ptr<net::ProxyService> proxy_script_fetcher_proxy_service; 124 scoped_ptr<net::HttpTransactionFactory> 125 proxy_script_fetcher_http_transaction_factory; 126 scoped_ptr<net::FtpTransactionFactory> 127 proxy_script_fetcher_ftp_transaction_factory; 128 scoped_ptr<net::URLRequestJobFactory> 129 proxy_script_fetcher_url_request_job_factory; 130 scoped_ptr<net::URLRequestThrottlerManager> throttler_manager; 131 scoped_ptr<net::URLSecurityManager> url_security_manager; 132 // TODO(willchan): Remove proxy script fetcher context since it's not 133 // necessary now that I got rid of refcounting URLRequestContexts. 134 // 135 // The first URLRequestContext is |system_url_request_context|. We introduce 136 // |proxy_script_fetcher_context| for the second context. It has a direct 137 // ProxyService, since we always directly connect to fetch the PAC script. 138 scoped_ptr<net::URLRequestContext> proxy_script_fetcher_context; 139 scoped_ptr<net::ProxyService> system_proxy_service; 140 scoped_ptr<net::HttpTransactionFactory> system_http_transaction_factory; 141 scoped_ptr<net::URLRequestContext> system_request_context; 142 SystemRequestContextLeakChecker system_request_context_leak_checker; 143 // |system_cookie_store| and |system_server_bound_cert_service| are shared 144 // between |proxy_script_fetcher_context| and |system_request_context|. 145 scoped_refptr<net::CookieStore> system_cookie_store; 146 scoped_refptr<extensions::EventRouterForwarder> 147 extension_event_router_forwarder; 148 scoped_ptr<chrome_browser_net::HttpPipeliningCompatibilityClient> 149 http_pipelining_compatibility_client; 150 scoped_ptr<chrome_browser_net::LoadTimeStats> load_time_stats; 151 scoped_ptr<net::HostMappingRules> host_mapping_rules; 152 scoped_ptr<net::HttpUserAgentSettings> http_user_agent_settings; 153 bool ignore_certificate_errors; 154 bool http_pipelining_enabled; 155 uint16 testing_fixed_http_port; 156 uint16 testing_fixed_https_port; 157 Optional<size_t> initial_max_spdy_concurrent_streams; 158 Optional<size_t> max_spdy_concurrent_streams_limit; 159 Optional<bool> force_spdy_single_domain; 160 Optional<bool> enable_spdy_ip_pooling; 161 Optional<bool> enable_spdy_credential_frames; 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 the SPDY protocol. If this function is not called, SPDY/3 225 // will be enabled. 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/2 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::string spdyproxy_auth_origin_; 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_ 328