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