Home | History | Annotate | Download | only in url_request
      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 SdchManager;
     40 class ServerBoundCertService;
     41 class ProxyService;
     42 class URLRequest;
     43 class URLRequestJobFactory;
     44 class URLRequestThrottlerManager;
     45 
     46 // Subclass to provide application-specific context for URLRequest
     47 // instances. Note that URLRequestContext typically does not provide storage for
     48 // these member variables, since they may be shared. For the ones that aren't
     49 // shared, URLRequestContextStorage can be helpful in defining their storage.
     50 class NET_EXPORT URLRequestContext
     51     : NON_EXPORTED_BASE(public base::NonThreadSafe) {
     52  public:
     53   URLRequestContext();
     54   virtual ~URLRequestContext();
     55 
     56   // Copies the state from |other| into this context.
     57   void CopyFrom(const URLRequestContext* other);
     58 
     59   // May return NULL if this context doesn't have an associated network session.
     60   const HttpNetworkSession::Params* GetNetworkSessionParams() const;
     61 
     62   // Creates a URLRequest. |cookie_store| optionally specifies a cookie store
     63   // to be used rather than the one represented by the context, or NULL
     64   // otherwise.
     65   scoped_ptr<URLRequest> CreateRequest(const GURL& url,
     66                                        RequestPriority priority,
     67                                        URLRequest::Delegate* delegate,
     68                                        CookieStore* cookie_store) const;
     69 
     70   NetLog* net_log() const {
     71     return net_log_;
     72   }
     73 
     74   void set_net_log(NetLog* net_log) {
     75     net_log_ = net_log;
     76   }
     77 
     78   HostResolver* host_resolver() const {
     79     return host_resolver_;
     80   }
     81 
     82   void set_host_resolver(HostResolver* host_resolver) {
     83     host_resolver_ = host_resolver;
     84   }
     85 
     86   CertVerifier* cert_verifier() const {
     87     return cert_verifier_;
     88   }
     89 
     90   void set_cert_verifier(CertVerifier* cert_verifier) {
     91     cert_verifier_ = cert_verifier;
     92   }
     93 
     94   ServerBoundCertService* server_bound_cert_service() const {
     95     return server_bound_cert_service_;
     96   }
     97 
     98   void set_server_bound_cert_service(
     99       ServerBoundCertService* server_bound_cert_service) {
    100     server_bound_cert_service_ = server_bound_cert_service;
    101   }
    102 
    103   FraudulentCertificateReporter* fraudulent_certificate_reporter() const {
    104     return fraudulent_certificate_reporter_;
    105   }
    106   void set_fraudulent_certificate_reporter(
    107       FraudulentCertificateReporter* fraudulent_certificate_reporter) {
    108     fraudulent_certificate_reporter_ = fraudulent_certificate_reporter;
    109   }
    110 
    111   // Get the proxy service for this context.
    112   ProxyService* proxy_service() const { return proxy_service_; }
    113   void set_proxy_service(ProxyService* proxy_service) {
    114     proxy_service_ = proxy_service;
    115   }
    116 
    117   // Get the ssl config service for this context.
    118   SSLConfigService* ssl_config_service() const {
    119     return ssl_config_service_.get();
    120   }
    121   void set_ssl_config_service(SSLConfigService* service) {
    122     ssl_config_service_ = service;
    123   }
    124 
    125   // Gets the HTTP Authentication Handler Factory for this context.
    126   // The factory is only valid for the lifetime of this URLRequestContext
    127   HttpAuthHandlerFactory* http_auth_handler_factory() const {
    128     return http_auth_handler_factory_;
    129   }
    130   void set_http_auth_handler_factory(HttpAuthHandlerFactory* factory) {
    131     http_auth_handler_factory_ = factory;
    132   }
    133 
    134   // Gets the http transaction factory for this context.
    135   HttpTransactionFactory* http_transaction_factory() const {
    136     return http_transaction_factory_;
    137   }
    138   void set_http_transaction_factory(HttpTransactionFactory* factory) {
    139     http_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       const base::WeakPtr<HttpServerProperties>& http_server_properties) {
    149     http_server_properties_ = http_server_properties;
    150   }
    151   base::WeakPtr<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   CTVerifier* cert_transparency_verifier() const {
    169     return cert_transparency_verifier_;
    170   }
    171   void set_cert_transparency_verifier(CTVerifier* verifier) {
    172     cert_transparency_verifier_ = verifier;
    173   }
    174 
    175   const URLRequestJobFactory* job_factory() const { return job_factory_; }
    176   void set_job_factory(const URLRequestJobFactory* job_factory) {
    177     job_factory_ = job_factory;
    178   }
    179 
    180   // May be NULL.
    181   URLRequestThrottlerManager* throttler_manager() const {
    182     return throttler_manager_;
    183   }
    184   void set_throttler_manager(URLRequestThrottlerManager* throttler_manager) {
    185     throttler_manager_ = throttler_manager;
    186   }
    187 
    188   // May be NULL.
    189   SdchManager* sdch_manager() const {
    190     return sdch_manager_;
    191   }
    192   void set_sdch_manager(SdchManager* sdch_manager) {
    193     sdch_manager_ = sdch_manager;
    194   }
    195 
    196   // Gets the URLRequest objects that hold a reference to this
    197   // URLRequestContext.
    198   std::set<const URLRequest*>* url_requests() const {
    199     return url_requests_.get();
    200   }
    201 
    202   void AssertNoURLRequests() const;
    203 
    204   // Get the underlying |HttpUserAgentSettings| implementation that provides
    205   // the HTTP Accept-Language and User-Agent header values.
    206   const HttpUserAgentSettings* http_user_agent_settings() const {
    207     return http_user_agent_settings_;
    208   }
    209   void set_http_user_agent_settings(
    210       HttpUserAgentSettings* http_user_agent_settings) {
    211     http_user_agent_settings_ = http_user_agent_settings;
    212   }
    213 
    214  private:
    215   // ---------------------------------------------------------------------------
    216   // Important: When adding any new members below, consider whether they need to
    217   // be added to CopyFrom.
    218   // ---------------------------------------------------------------------------
    219 
    220   // Ownership for these members are not defined here. Clients should either
    221   // provide storage elsewhere or have a subclass take ownership.
    222   NetLog* net_log_;
    223   HostResolver* host_resolver_;
    224   CertVerifier* cert_verifier_;
    225   ServerBoundCertService* server_bound_cert_service_;
    226   FraudulentCertificateReporter* fraudulent_certificate_reporter_;
    227   HttpAuthHandlerFactory* http_auth_handler_factory_;
    228   ProxyService* proxy_service_;
    229   scoped_refptr<SSLConfigService> ssl_config_service_;
    230   NetworkDelegate* network_delegate_;
    231   base::WeakPtr<HttpServerProperties> http_server_properties_;
    232   HttpUserAgentSettings* http_user_agent_settings_;
    233   scoped_refptr<CookieStore> cookie_store_;
    234   TransportSecurityState* transport_security_state_;
    235   CTVerifier* cert_transparency_verifier_;
    236   HttpTransactionFactory* http_transaction_factory_;
    237   const URLRequestJobFactory* job_factory_;
    238   URLRequestThrottlerManager* throttler_manager_;
    239   SdchManager* sdch_manager_;
    240 
    241   // ---------------------------------------------------------------------------
    242   // Important: When adding any new members below, consider whether they need to
    243   // be added to CopyFrom.
    244   // ---------------------------------------------------------------------------
    245 
    246   scoped_ptr<std::set<const URLRequest*> > url_requests_;
    247 
    248   DISALLOW_COPY_AND_ASSIGN(URLRequestContext);
    249 };
    250 
    251 }  // namespace net
    252 
    253 #endif  // NET_URL_REQUEST_URL_REQUEST_CONTEXT_H_
    254