url_request_context.h revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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// This class represents contextual information (cookies, cache, etc.) 6// that's useful when processing resource requests. 7// The class is reference-counted so that it can be cleaned up after any 8// requests that are using it have been completed. 9 10#ifndef NET_URL_REQUEST_URL_REQUEST_CONTEXT_H_ 11#define NET_URL_REQUEST_URL_REQUEST_CONTEXT_H_ 12 13#include <set> 14#include <string> 15 16#include "base/memory/ref_counted.h" 17#include "base/memory/scoped_ptr.h" 18#include "base/memory/weak_ptr.h" 19#include "base/threading/non_thread_safe.h" 20#include "net/base/net_export.h" 21#include "net/base/net_log.h" 22#include "net/ftp/ftp_auth_cache.h" 23#include "net/http/http_network_session.h" 24#include "net/http/http_server_properties.h" 25#include "net/http/transport_security_state.h" 26#include "net/ssl/ssl_config_service.h" 27#include "net/url_request/url_request.h" 28 29namespace net { 30class CertVerifier; 31class CookieStore; 32class FraudulentCertificateReporter; 33class FtpTransactionFactory; 34class HostResolver; 35class HttpAuthHandlerFactory; 36class HttpTransactionFactory; 37class HttpUserAgentSettings; 38class NetworkDelegate; 39class ServerBoundCertService; 40class ProxyService; 41class URLRequest; 42class URLRequestJobFactory; 43class URLRequestThrottlerManager; 44 45// Subclass to provide application-specific context for URLRequest 46// instances. Note that URLRequestContext typically does not provide storage for 47// these member variables, since they may be shared. For the ones that aren't 48// shared, URLRequestContextStorage can be helpful in defining their storage. 49class NET_EXPORT URLRequestContext 50 : NON_EXPORTED_BASE(public base::NonThreadSafe) { 51 public: 52 URLRequestContext(); 53 virtual ~URLRequestContext(); 54 55 // Copies the state from |other| into this context. 56 void CopyFrom(const URLRequestContext* other); 57 58 // May return NULL if this context doesn't have an associated network session. 59 const HttpNetworkSession::Params* GetNetworkSessionParams() const; 60 61 URLRequest* CreateRequest( 62 const GURL& url, URLRequest::Delegate* delegate) const; 63 64 NetLog* net_log() const { 65 return net_log_; 66 } 67 68 void set_net_log(NetLog* net_log) { 69 net_log_ = net_log; 70 } 71 72 HostResolver* host_resolver() const { 73 return host_resolver_; 74 } 75 76 void set_host_resolver(HostResolver* host_resolver) { 77 host_resolver_ = host_resolver; 78 } 79 80 CertVerifier* cert_verifier() const { 81 return cert_verifier_; 82 } 83 84 void set_cert_verifier(CertVerifier* cert_verifier) { 85 cert_verifier_ = cert_verifier; 86 } 87 88 ServerBoundCertService* server_bound_cert_service() const { 89 return server_bound_cert_service_; 90 } 91 92 void set_server_bound_cert_service( 93 ServerBoundCertService* server_bound_cert_service) { 94 server_bound_cert_service_ = server_bound_cert_service; 95 } 96 97 FraudulentCertificateReporter* fraudulent_certificate_reporter() const { 98 return fraudulent_certificate_reporter_; 99 } 100 void set_fraudulent_certificate_reporter( 101 FraudulentCertificateReporter* fraudulent_certificate_reporter) { 102 fraudulent_certificate_reporter_ = fraudulent_certificate_reporter; 103 } 104 105 // Get the proxy service for this context. 106 ProxyService* proxy_service() const { return proxy_service_; } 107 void set_proxy_service(ProxyService* proxy_service) { 108 proxy_service_ = proxy_service; 109 } 110 111 // Get the ssl config service for this context. 112 SSLConfigService* ssl_config_service() const { return ssl_config_service_; } 113 void set_ssl_config_service(SSLConfigService* service) { 114 ssl_config_service_ = service; 115 } 116 117 // Gets the HTTP Authentication Handler Factory for this context. 118 // The factory is only valid for the lifetime of this URLRequestContext 119 HttpAuthHandlerFactory* http_auth_handler_factory() const { 120 return http_auth_handler_factory_; 121 } 122 void set_http_auth_handler_factory(HttpAuthHandlerFactory* factory) { 123 http_auth_handler_factory_ = factory; 124 } 125 126 // Gets the http transaction factory for this context. 127 HttpTransactionFactory* http_transaction_factory() const { 128 return http_transaction_factory_; 129 } 130 void set_http_transaction_factory(HttpTransactionFactory* factory) { 131 http_transaction_factory_ = factory; 132 } 133 134 // Gets the ftp transaction factory for this context. 135 FtpTransactionFactory* ftp_transaction_factory() const { 136 return ftp_transaction_factory_; 137 } 138 void set_ftp_transaction_factory(FtpTransactionFactory* factory) { 139 ftp_transaction_factory_ = factory; 140 } 141 142 void set_network_delegate(NetworkDelegate* network_delegate) { 143 network_delegate_ = network_delegate; 144 } 145 NetworkDelegate* network_delegate() const { return network_delegate_; } 146 147 void set_http_server_properties( 148 HttpServerProperties* http_server_properties) { 149 http_server_properties_ = http_server_properties; 150 } 151 HttpServerProperties* http_server_properties() const { 152 return http_server_properties_; 153 } 154 155 // Gets the cookie store for this context (may be null, in which case 156 // cookies are not stored). 157 CookieStore* cookie_store() const { return cookie_store_.get(); } 158 void set_cookie_store(CookieStore* cookie_store); 159 160 TransportSecurityState* transport_security_state() const { 161 return transport_security_state_; 162 } 163 void set_transport_security_state( 164 TransportSecurityState* state) { 165 transport_security_state_ = state; 166 } 167 168 // Gets the FTP authentication cache for this context. 169 FtpAuthCache* ftp_auth_cache() const { 170#if !defined(DISABLE_FTP_SUPPORT) 171 return ftp_auth_cache_.get(); 172#else 173 return NULL; 174#endif 175 } 176 177 // --------------------------------------------------------------------------- 178 // Legacy accessors that delegate to http_user_agent_settings_. 179 // TODO(pauljensen): Remove after all clients are updated to directly access 180 // http_user_agent_settings_. 181 // Gets the value of 'Accept-Language' header field. 182 std::string GetAcceptLanguage() const; 183 // Gets the UA string to use for the given URL. Pass an invalid URL (such as 184 // GURL()) to get the default UA string. 185 std::string GetUserAgent(const GURL& url) const; 186 // --------------------------------------------------------------------------- 187 188 const URLRequestJobFactory* job_factory() const { return job_factory_; } 189 void set_job_factory(const URLRequestJobFactory* job_factory) { 190 job_factory_ = job_factory; 191 } 192 193 // May be NULL. 194 URLRequestThrottlerManager* throttler_manager() const { 195 return throttler_manager_; 196 } 197 void set_throttler_manager(URLRequestThrottlerManager* throttler_manager) { 198 throttler_manager_ = throttler_manager; 199 } 200 201 // Gets the URLRequest objects that hold a reference to this 202 // URLRequestContext. 203 std::set<const URLRequest*>* url_requests() const { 204 return url_requests_.get(); 205 } 206 207 void AssertNoURLRequests() const; 208 209 // Get the underlying |HttpUserAgentSettings| implementation that provides 210 // the HTTP Accept-Language and User-Agent header values. 211 const HttpUserAgentSettings* http_user_agent_settings() const { 212 return http_user_agent_settings_; 213 } 214 void set_http_user_agent_settings( 215 HttpUserAgentSettings* http_user_agent_settings) { 216 http_user_agent_settings_ = http_user_agent_settings; 217 } 218 219 private: 220 // --------------------------------------------------------------------------- 221 // Important: When adding any new members below, consider whether they need to 222 // be added to CopyFrom. 223 // --------------------------------------------------------------------------- 224 225 // Ownership for these members are not defined here. Clients should either 226 // provide storage elsewhere or have a subclass take ownership. 227 NetLog* net_log_; 228 HostResolver* host_resolver_; 229 CertVerifier* cert_verifier_; 230 ServerBoundCertService* server_bound_cert_service_; 231 FraudulentCertificateReporter* fraudulent_certificate_reporter_; 232 HttpAuthHandlerFactory* http_auth_handler_factory_; 233 ProxyService* proxy_service_; 234 scoped_refptr<SSLConfigService> ssl_config_service_; 235 NetworkDelegate* network_delegate_; 236 HttpServerProperties* http_server_properties_; 237 HttpUserAgentSettings* http_user_agent_settings_; 238 scoped_refptr<CookieStore> cookie_store_; 239 TransportSecurityState* transport_security_state_; 240#if !defined(DISABLE_FTP_SUPPORT) 241 scoped_ptr<FtpAuthCache> ftp_auth_cache_; 242#endif 243 HttpTransactionFactory* http_transaction_factory_; 244 FtpTransactionFactory* ftp_transaction_factory_; 245 const URLRequestJobFactory* job_factory_; 246 URLRequestThrottlerManager* throttler_manager_; 247 248 // --------------------------------------------------------------------------- 249 // Important: When adding any new members below, consider whether they need to 250 // be added to CopyFrom. 251 // --------------------------------------------------------------------------- 252 253 scoped_ptr<std::set<const URLRequest*> > url_requests_; 254 255 DISALLOW_COPY_AND_ASSIGN(URLRequestContext); 256}; 257 258} // namespace net 259 260#endif // NET_URL_REQUEST_URL_REQUEST_CONTEXT_H_ 261