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/base/request_priority.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 29 namespace net { 30 class CertVerifier; 31 class CookieStore; 32 class CTVerifier; 33 class FraudulentCertificateReporter; 34 class HostResolver; 35 class HttpAuthHandlerFactory; 36 class HttpTransactionFactory; 37 class HttpUserAgentSettings; 38 class NetworkDelegate; 39 class ServerBoundCertService; 40 class ProxyService; 41 class URLRequest; 42 class URLRequestJobFactory; 43 class 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. 49 class 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 scoped_ptr<URLRequest> CreateRequest(const GURL& url, 62 RequestPriority priority, 63 URLRequest::Delegate* delegate) const; 64 65 NetLog* net_log() const { 66 return net_log_; 67 } 68 69 void set_net_log(NetLog* net_log) { 70 net_log_ = net_log; 71 } 72 73 HostResolver* host_resolver() const { 74 return host_resolver_; 75 } 76 77 void set_host_resolver(HostResolver* host_resolver) { 78 host_resolver_ = host_resolver; 79 } 80 81 CertVerifier* cert_verifier() const { 82 return cert_verifier_; 83 } 84 85 void set_cert_verifier(CertVerifier* cert_verifier) { 86 cert_verifier_ = cert_verifier; 87 } 88 89 ServerBoundCertService* server_bound_cert_service() const { 90 return server_bound_cert_service_; 91 } 92 93 void set_server_bound_cert_service( 94 ServerBoundCertService* server_bound_cert_service) { 95 server_bound_cert_service_ = server_bound_cert_service; 96 } 97 98 FraudulentCertificateReporter* fraudulent_certificate_reporter() const { 99 return fraudulent_certificate_reporter_; 100 } 101 void set_fraudulent_certificate_reporter( 102 FraudulentCertificateReporter* fraudulent_certificate_reporter) { 103 fraudulent_certificate_reporter_ = fraudulent_certificate_reporter; 104 } 105 106 // Get the proxy service for this context. 107 ProxyService* proxy_service() const { return proxy_service_; } 108 void set_proxy_service(ProxyService* proxy_service) { 109 proxy_service_ = proxy_service; 110 } 111 112 // Get the ssl config service for this context. 113 SSLConfigService* ssl_config_service() const { 114 return ssl_config_service_.get(); 115 } 116 void set_ssl_config_service(SSLConfigService* service) { 117 ssl_config_service_ = service; 118 } 119 120 // Gets the HTTP Authentication Handler Factory for this context. 121 // The factory is only valid for the lifetime of this URLRequestContext 122 HttpAuthHandlerFactory* http_auth_handler_factory() const { 123 return http_auth_handler_factory_; 124 } 125 void set_http_auth_handler_factory(HttpAuthHandlerFactory* factory) { 126 http_auth_handler_factory_ = factory; 127 } 128 129 // Gets the http transaction factory for this context. 130 HttpTransactionFactory* http_transaction_factory() const { 131 return http_transaction_factory_; 132 } 133 void set_http_transaction_factory(HttpTransactionFactory* factory) { 134 http_transaction_factory_ = factory; 135 } 136 137 void set_network_delegate(NetworkDelegate* network_delegate) { 138 network_delegate_ = network_delegate; 139 } 140 NetworkDelegate* network_delegate() const { return network_delegate_; } 141 142 void set_http_server_properties( 143 const base::WeakPtr<HttpServerProperties>& http_server_properties) { 144 http_server_properties_ = http_server_properties; 145 } 146 base::WeakPtr<HttpServerProperties> http_server_properties() const { 147 return http_server_properties_; 148 } 149 150 // Gets the cookie store for this context (may be null, in which case 151 // cookies are not stored). 152 CookieStore* cookie_store() const { return cookie_store_.get(); } 153 void set_cookie_store(CookieStore* cookie_store); 154 155 TransportSecurityState* transport_security_state() const { 156 return transport_security_state_; 157 } 158 void set_transport_security_state( 159 TransportSecurityState* state) { 160 transport_security_state_ = state; 161 } 162 163 CTVerifier* cert_transparency_verifier() const { 164 return cert_transparency_verifier_; 165 } 166 void set_cert_transparency_verifier(CTVerifier* verifier) { 167 cert_transparency_verifier_ = verifier; 168 } 169 170 // --------------------------------------------------------------------------- 171 // Legacy accessors that delegate to http_user_agent_settings_. 172 // TODO(pauljensen): Remove after all clients are updated to directly access 173 // http_user_agent_settings_. 174 // Gets the value of 'Accept-Language' header field. 175 std::string GetAcceptLanguage() const; 176 // Gets the UA string to use for the given URL. Pass an invalid URL (such as 177 // GURL()) to get the default UA string. 178 std::string GetUserAgent(const GURL& url) const; 179 // --------------------------------------------------------------------------- 180 181 const URLRequestJobFactory* job_factory() const { return job_factory_; } 182 void set_job_factory(const URLRequestJobFactory* job_factory) { 183 job_factory_ = job_factory; 184 } 185 186 // May be NULL. 187 URLRequestThrottlerManager* throttler_manager() const { 188 return throttler_manager_; 189 } 190 void set_throttler_manager(URLRequestThrottlerManager* throttler_manager) { 191 throttler_manager_ = throttler_manager; 192 } 193 194 // Gets the URLRequest objects that hold a reference to this 195 // URLRequestContext. 196 std::set<const URLRequest*>* url_requests() const { 197 return url_requests_.get(); 198 } 199 200 void AssertNoURLRequests() const; 201 202 // Get the underlying |HttpUserAgentSettings| implementation that provides 203 // the HTTP Accept-Language and User-Agent header values. 204 const HttpUserAgentSettings* http_user_agent_settings() const { 205 return http_user_agent_settings_; 206 } 207 void set_http_user_agent_settings( 208 HttpUserAgentSettings* http_user_agent_settings) { 209 http_user_agent_settings_ = http_user_agent_settings; 210 } 211 212 private: 213 // --------------------------------------------------------------------------- 214 // Important: When adding any new members below, consider whether they need to 215 // be added to CopyFrom. 216 // --------------------------------------------------------------------------- 217 218 // Ownership for these members are not defined here. Clients should either 219 // provide storage elsewhere or have a subclass take ownership. 220 NetLog* net_log_; 221 HostResolver* host_resolver_; 222 CertVerifier* cert_verifier_; 223 ServerBoundCertService* server_bound_cert_service_; 224 FraudulentCertificateReporter* fraudulent_certificate_reporter_; 225 HttpAuthHandlerFactory* http_auth_handler_factory_; 226 ProxyService* proxy_service_; 227 scoped_refptr<SSLConfigService> ssl_config_service_; 228 NetworkDelegate* network_delegate_; 229 base::WeakPtr<HttpServerProperties> http_server_properties_; 230 HttpUserAgentSettings* http_user_agent_settings_; 231 scoped_refptr<CookieStore> cookie_store_; 232 TransportSecurityState* transport_security_state_; 233 CTVerifier* cert_transparency_verifier_; 234 HttpTransactionFactory* http_transaction_factory_; 235 const URLRequestJobFactory* job_factory_; 236 URLRequestThrottlerManager* throttler_manager_; 237 238 // --------------------------------------------------------------------------- 239 // Important: When adding any new members below, consider whether they need to 240 // be added to CopyFrom. 241 // --------------------------------------------------------------------------- 242 243 scoped_ptr<std::set<const URLRequest*> > url_requests_; 244 245 DISALLOW_COPY_AND_ASSIGN(URLRequestContext); 246 }; 247 248 } // namespace net 249 250 #endif // NET_URL_REQUEST_URL_REQUEST_CONTEXT_H_ 251