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 // 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 #pragma once 13 14 #include "base/memory/ref_counted.h" 15 #include "base/threading/non_thread_safe.h" 16 #include "net/base/net_export.h" 17 #include "net/base/net_log.h" 18 #include "net/base/ssl_config_service.h" 19 #include "net/base/transport_security_state.h" 20 #include "net/ftp/ftp_auth_cache.h" 21 #include "net/proxy/proxy_service.h" 22 #include "net/socket/dns_cert_provenance_checker.h" 23 24 namespace net { 25 class CertVerifier; 26 class CookiePolicy; 27 class CookieStore; 28 class DnsCertProvenanceChecker; 29 class DnsRRResolver; 30 class FtpTransactionFactory; 31 class HostResolver; 32 class HttpAuthHandlerFactory; 33 class HttpTransactionFactory; 34 class NetworkDelegate; 35 class SSLConfigService; 36 class URLRequest; 37 38 // Subclass to provide application-specific context for URLRequest 39 // instances. Note that URLRequestContext typically does not provide storage for 40 // these member variables, since they may be shared. For the ones that aren't 41 // shared, URLRequestContextStorage can be helpful in defining their storage. 42 class NET_EXPORT URLRequestContext 43 : public base::RefCountedThreadSafe<URLRequestContext>, 44 public base::NonThreadSafe { 45 public: 46 URLRequestContext(); 47 48 // Copies the state from |other| into this context. 49 void CopyFrom(URLRequestContext* other); 50 net_log()51 NetLog* net_log() const { 52 return net_log_; 53 } 54 set_net_log(NetLog * net_log)55 void set_net_log(NetLog* net_log) { 56 net_log_ = net_log; 57 } 58 host_resolver()59 HostResolver* host_resolver() const { 60 return host_resolver_; 61 } 62 set_host_resolver(HostResolver * host_resolver)63 void set_host_resolver(HostResolver* host_resolver) { 64 host_resolver_ = host_resolver; 65 } 66 cert_verifier()67 CertVerifier* cert_verifier() const { 68 return cert_verifier_; 69 } 70 set_cert_verifier(CertVerifier * cert_verifier)71 void set_cert_verifier(CertVerifier* cert_verifier) { 72 cert_verifier_ = cert_verifier; 73 } 74 dnsrr_resolver()75 DnsRRResolver* dnsrr_resolver() const { 76 return dnsrr_resolver_; 77 } 78 set_dnsrr_resolver(DnsRRResolver * dnsrr_resolver)79 void set_dnsrr_resolver(DnsRRResolver* dnsrr_resolver) { 80 dnsrr_resolver_ = dnsrr_resolver; 81 } 82 dns_cert_checker()83 DnsCertProvenanceChecker* dns_cert_checker() const { 84 return dns_cert_checker_; 85 } set_dns_cert_checker(DnsCertProvenanceChecker * dns_cert_checker)86 void set_dns_cert_checker(DnsCertProvenanceChecker* dns_cert_checker) { 87 dns_cert_checker_ = dns_cert_checker; 88 } 89 90 // Get the proxy service for this context. proxy_service()91 ProxyService* proxy_service() const { return proxy_service_; } set_proxy_service(ProxyService * proxy_service)92 void set_proxy_service(ProxyService* proxy_service) { 93 proxy_service_ = proxy_service; 94 } 95 96 // Get the ssl config service for this context. ssl_config_service()97 SSLConfigService* ssl_config_service() const { return ssl_config_service_; } set_ssl_config_service(SSLConfigService * service)98 void set_ssl_config_service(SSLConfigService* service) { 99 ssl_config_service_ = service; 100 } 101 102 // Gets the HTTP Authentication Handler Factory for this context. 103 // The factory is only valid for the lifetime of this URLRequestContext http_auth_handler_factory()104 HttpAuthHandlerFactory* http_auth_handler_factory() { 105 return http_auth_handler_factory_; 106 } set_http_auth_handler_factory(HttpAuthHandlerFactory * factory)107 void set_http_auth_handler_factory(HttpAuthHandlerFactory* factory) { 108 http_auth_handler_factory_ = factory; 109 } 110 111 // Gets the http transaction factory for this context. http_transaction_factory()112 HttpTransactionFactory* http_transaction_factory() const { 113 return http_transaction_factory_; 114 } set_http_transaction_factory(HttpTransactionFactory * factory)115 void set_http_transaction_factory(HttpTransactionFactory* factory) { 116 http_transaction_factory_ = factory; 117 } 118 119 // Gets the ftp transaction factory for this context. ftp_transaction_factory()120 FtpTransactionFactory* ftp_transaction_factory() { 121 return ftp_transaction_factory_; 122 } set_ftp_transaction_factory(FtpTransactionFactory * factory)123 void set_ftp_transaction_factory(FtpTransactionFactory* factory) { 124 ftp_transaction_factory_ = factory; 125 } 126 set_network_delegate(NetworkDelegate * network_delegate)127 void set_network_delegate(NetworkDelegate* network_delegate) { 128 network_delegate_ = network_delegate; 129 } network_delegate()130 NetworkDelegate* network_delegate() const { return network_delegate_; } 131 132 // Gets the cookie store for this context (may be null, in which case 133 // cookies are not stored). cookie_store()134 CookieStore* cookie_store() const { return cookie_store_.get(); } 135 void set_cookie_store(CookieStore* cookie_store); 136 137 // Gets the cookie policy for this context (may be null, in which case 138 // cookies are allowed). cookie_policy()139 CookiePolicy* cookie_policy() const { return cookie_policy_; } set_cookie_policy(CookiePolicy * cookie_policy)140 void set_cookie_policy(CookiePolicy* cookie_policy) { 141 cookie_policy_ = cookie_policy; 142 } 143 transport_security_state()144 TransportSecurityState* transport_security_state() const { 145 return transport_security_state_; 146 } set_transport_security_state(TransportSecurityState * state)147 void set_transport_security_state( 148 TransportSecurityState* state) { 149 transport_security_state_ = state; 150 } 151 152 // Gets the FTP authentication cache for this context. ftp_auth_cache()153 FtpAuthCache* ftp_auth_cache() { return &ftp_auth_cache_; } 154 155 // Gets the value of 'Accept-Charset' header field. accept_charset()156 const std::string& accept_charset() const { return accept_charset_; } set_accept_charset(const std::string & accept_charset)157 void set_accept_charset(const std::string& accept_charset) { 158 accept_charset_ = accept_charset; 159 } 160 161 // Gets the value of 'Accept-Language' header field. 162 #ifdef ANDROID 163 virtual 164 #endif accept_language()165 const std::string& accept_language() const { return accept_language_; } 166 set_accept_language(const std::string & accept_language)167 void set_accept_language(const std::string& accept_language) { 168 accept_language_ = accept_language; 169 } 170 171 // Gets the UA string to use for the given URL. Pass an invalid URL (such as 172 // GURL()) to get the default UA string. Subclasses should override this 173 // method to provide a UA string. 174 virtual const std::string& GetUserAgent(const GURL& url) const; 175 176 // In general, referrer_charset is not known when URLRequestContext is 177 // constructed. So, we need a setter. referrer_charset()178 const std::string& referrer_charset() const { return referrer_charset_; } set_referrer_charset(const std::string & charset)179 void set_referrer_charset(const std::string& charset) { 180 referrer_charset_ = charset; 181 } 182 183 // Controls whether or not the URLRequestContext considers itself to be the 184 // "main" URLRequestContext. is_main()185 bool is_main() const { return is_main_; } set_is_main(bool is_main)186 void set_is_main(bool is_main) { is_main_ = is_main; } 187 188 // Is SNI available in this request context? 189 bool IsSNIAvailable() const; 190 191 #ifdef ANDROID 192 // Gets the UID of the calling process 193 bool getUID(uid_t *uid) const; 194 void setUID(uid_t uid); 195 #endif 196 197 protected: 198 friend class base::RefCountedThreadSafe<URLRequestContext>; 199 200 virtual ~URLRequestContext(); 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 // Indicates whether or not this is the main URLRequestContext. 209 bool is_main_; 210 211 // Ownership for these members are not defined here. Clients should either 212 // provide storage elsewhere or have a subclass take ownership. 213 NetLog* net_log_; 214 HostResolver* host_resolver_; 215 CertVerifier* cert_verifier_; 216 DnsRRResolver* dnsrr_resolver_; 217 DnsCertProvenanceChecker* dns_cert_checker_; 218 HttpAuthHandlerFactory* http_auth_handler_factory_; 219 scoped_refptr<ProxyService> proxy_service_; 220 scoped_refptr<SSLConfigService> ssl_config_service_; 221 NetworkDelegate* network_delegate_; 222 scoped_refptr<CookieStore> cookie_store_; 223 CookiePolicy* cookie_policy_; 224 scoped_refptr<TransportSecurityState> transport_security_state_; 225 FtpAuthCache ftp_auth_cache_; 226 std::string accept_language_; 227 std::string accept_charset_; 228 // The charset of the referrer where this request comes from. It's not 229 // used in communication with a server but is used to construct a suggested 230 // filename for file download. 231 std::string referrer_charset_; 232 233 HttpTransactionFactory* http_transaction_factory_; 234 FtpTransactionFactory* ftp_transaction_factory_; 235 236 // --------------------------------------------------------------------------- 237 // Important: When adding any new members below, consider whether they need to 238 // be added to CopyFrom. 239 // --------------------------------------------------------------------------- 240 241 #ifdef ANDROID 242 bool valid_uid_; 243 uid_t calling_uid_; 244 #endif 245 246 DISALLOW_COPY_AND_ASSIGN(URLRequestContext); 247 }; 248 249 } // namespace net 250 251 #endif // NET_URL_REQUEST_URL_REQUEST_CONTEXT_H_ 252