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/http/http_network_session.h" 23 #include "net/http/http_server_properties.h" 24 #include "net/http/transport_security_state.h" 25 #include "net/ssl/ssl_config_service.h" 26 #include "net/url_request/url_request.h" 27 28 namespace net { 29 class CertVerifier; 30 class CookieStore; 31 class FraudulentCertificateReporter; 32 class HostResolver; 33 class HttpAuthHandlerFactory; 34 class HttpTransactionFactory; 35 class HttpUserAgentSettings; 36 class NetworkDelegate; 37 class ServerBoundCertService; 38 class ProxyService; 39 class URLRequest; 40 class URLRequestJobFactory; 41 class URLRequestThrottlerManager; 42 43 // Subclass to provide application-specific context for URLRequest 44 // instances. Note that URLRequestContext typically does not provide storage for 45 // these member variables, since they may be shared. For the ones that aren't 46 // shared, URLRequestContextStorage can be helpful in defining their storage. 47 class NET_EXPORT URLRequestContext 48 : NON_EXPORTED_BASE(public base::NonThreadSafe) { 49 public: 50 URLRequestContext(); 51 virtual ~URLRequestContext(); 52 53 // Copies the state from |other| into this context. 54 void CopyFrom(const URLRequestContext* other); 55 56 // May return NULL if this context doesn't have an associated network session. 57 const HttpNetworkSession::Params* GetNetworkSessionParams() const; 58 59 URLRequest* CreateRequest( 60 const GURL& url, URLRequest::Delegate* delegate) const; 61 62 NetLog* net_log() const { 63 return net_log_; 64 } 65 66 void set_net_log(NetLog* net_log) { 67 net_log_ = net_log; 68 } 69 70 HostResolver* host_resolver() const { 71 return host_resolver_; 72 } 73 74 void set_host_resolver(HostResolver* host_resolver) { 75 host_resolver_ = host_resolver; 76 } 77 78 CertVerifier* cert_verifier() const { 79 return cert_verifier_; 80 } 81 82 void set_cert_verifier(CertVerifier* cert_verifier) { 83 cert_verifier_ = cert_verifier; 84 } 85 86 ServerBoundCertService* server_bound_cert_service() const { 87 return server_bound_cert_service_; 88 } 89 90 void set_server_bound_cert_service( 91 ServerBoundCertService* server_bound_cert_service) { 92 server_bound_cert_service_ = server_bound_cert_service; 93 } 94 95 FraudulentCertificateReporter* fraudulent_certificate_reporter() const { 96 return fraudulent_certificate_reporter_; 97 } 98 void set_fraudulent_certificate_reporter( 99 FraudulentCertificateReporter* fraudulent_certificate_reporter) { 100 fraudulent_certificate_reporter_ = fraudulent_certificate_reporter; 101 } 102 103 // Get the proxy service for this context. 104 ProxyService* proxy_service() const { return proxy_service_; } 105 void set_proxy_service(ProxyService* proxy_service) { 106 proxy_service_ = proxy_service; 107 } 108 109 // Get the ssl config service for this context. 110 SSLConfigService* ssl_config_service() const { 111 return ssl_config_service_.get(); 112 } 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 void set_network_delegate(NetworkDelegate* network_delegate) { 135 network_delegate_ = network_delegate; 136 } 137 NetworkDelegate* network_delegate() const { return network_delegate_; } 138 139 void set_http_server_properties( 140 const base::WeakPtr<HttpServerProperties>& http_server_properties) { 141 http_server_properties_ = http_server_properties; 142 } 143 base::WeakPtr<HttpServerProperties> http_server_properties() const { 144 return http_server_properties_; 145 } 146 147 // Gets the cookie store for this context (may be null, in which case 148 // cookies are not stored). 149 CookieStore* cookie_store() const { return cookie_store_.get(); } 150 void set_cookie_store(CookieStore* cookie_store); 151 152 TransportSecurityState* transport_security_state() const { 153 return transport_security_state_; 154 } 155 void set_transport_security_state( 156 TransportSecurityState* state) { 157 transport_security_state_ = state; 158 } 159 160 // --------------------------------------------------------------------------- 161 // Legacy accessors that delegate to http_user_agent_settings_. 162 // TODO(pauljensen): Remove after all clients are updated to directly access 163 // http_user_agent_settings_. 164 // Gets the value of 'Accept-Language' header field. 165 std::string GetAcceptLanguage() const; 166 // Gets the UA string to use for the given URL. Pass an invalid URL (such as 167 // GURL()) to get the default UA string. 168 std::string GetUserAgent(const GURL& url) const; 169 // --------------------------------------------------------------------------- 170 171 const URLRequestJobFactory* job_factory() const { return job_factory_; } 172 void set_job_factory(const URLRequestJobFactory* job_factory) { 173 job_factory_ = job_factory; 174 } 175 176 // May be NULL. 177 URLRequestThrottlerManager* throttler_manager() const { 178 return throttler_manager_; 179 } 180 void set_throttler_manager(URLRequestThrottlerManager* throttler_manager) { 181 throttler_manager_ = throttler_manager; 182 } 183 184 // Gets the URLRequest objects that hold a reference to this 185 // URLRequestContext. 186 std::set<const URLRequest*>* url_requests() const { 187 return url_requests_.get(); 188 } 189 190 void AssertNoURLRequests() const; 191 192 // Get the underlying |HttpUserAgentSettings| implementation that provides 193 // the HTTP Accept-Language and User-Agent header values. 194 const HttpUserAgentSettings* http_user_agent_settings() const { 195 return http_user_agent_settings_; 196 } 197 void set_http_user_agent_settings( 198 HttpUserAgentSettings* http_user_agent_settings) { 199 http_user_agent_settings_ = http_user_agent_settings; 200 } 201 202 private: 203 // --------------------------------------------------------------------------- 204 // Important: When adding any new members below, consider whether they need to 205 // be added to CopyFrom. 206 // --------------------------------------------------------------------------- 207 208 // Ownership for these members are not defined here. Clients should either 209 // provide storage elsewhere or have a subclass take ownership. 210 NetLog* net_log_; 211 HostResolver* host_resolver_; 212 CertVerifier* cert_verifier_; 213 ServerBoundCertService* server_bound_cert_service_; 214 FraudulentCertificateReporter* fraudulent_certificate_reporter_; 215 HttpAuthHandlerFactory* http_auth_handler_factory_; 216 ProxyService* proxy_service_; 217 scoped_refptr<SSLConfigService> ssl_config_service_; 218 NetworkDelegate* network_delegate_; 219 base::WeakPtr<HttpServerProperties> http_server_properties_; 220 HttpUserAgentSettings* http_user_agent_settings_; 221 scoped_refptr<CookieStore> cookie_store_; 222 TransportSecurityState* transport_security_state_; 223 HttpTransactionFactory* http_transaction_factory_; 224 const URLRequestJobFactory* job_factory_; 225 URLRequestThrottlerManager* throttler_manager_; 226 227 // --------------------------------------------------------------------------- 228 // Important: When adding any new members below, consider whether they need to 229 // be added to CopyFrom. 230 // --------------------------------------------------------------------------- 231 232 scoped_ptr<std::set<const URLRequest*> > url_requests_; 233 234 DISALLOW_COPY_AND_ASSIGN(URLRequestContext); 235 }; 236 237 } // namespace net 238 239 #endif // NET_URL_REQUEST_URL_REQUEST_CONTEXT_H_ 240