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.
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.
10 #ifndef NET_URL_REQUEST_URL_REQUEST_CONTEXT_H_
11 #define NET_URL_REQUEST_URL_REQUEST_CONTEXT_H_
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/request_priority.h"
22 #include "net/base/sdch_manager.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/log/net_log.h"
27 #include "net/ssl/ssl_config_service.h"
28 #include "net/url_request/url_request.h"
32 class ChannelIDService
;
35 class FraudulentCertificateReporter
;
37 class HttpAuthHandlerFactory
;
38 class HttpTransactionFactory
;
39 class HttpUserAgentSettings
;
40 class NetworkDelegate
;
44 class URLRequestJobFactory
;
45 class URLRequestThrottlerManager
;
47 // Subclass to provide application-specific context for URLRequest
48 // instances. Note that URLRequestContext typically does not provide storage for
49 // these member variables, since they may be shared. For the ones that aren't
50 // shared, URLRequestContextStorage can be helpful in defining their storage.
51 class NET_EXPORT URLRequestContext
52 : NON_EXPORTED_BASE(public base::NonThreadSafe
) {
55 virtual ~URLRequestContext();
57 // Copies the state from |other| into this context.
58 void CopyFrom(const URLRequestContext
* other
);
60 // May return NULL if this context doesn't have an associated network session.
61 const HttpNetworkSession::Params
* GetNetworkSessionParams() const;
63 scoped_ptr
<URLRequest
> CreateRequest(const GURL
& url
,
64 RequestPriority priority
,
65 URLRequest::Delegate
* delegate
) const;
67 NetLog
* net_log() const {
71 void set_net_log(NetLog
* net_log
) {
75 HostResolver
* host_resolver() const {
76 return host_resolver_
;
79 void set_host_resolver(HostResolver
* host_resolver
) {
80 host_resolver_
= host_resolver
;
83 CertVerifier
* cert_verifier() const {
84 return cert_verifier_
;
87 void set_cert_verifier(CertVerifier
* cert_verifier
) {
88 cert_verifier_
= cert_verifier
;
91 ChannelIDService
* channel_id_service() const {
92 return channel_id_service_
;
95 void set_channel_id_service(
96 ChannelIDService
* channel_id_service
) {
97 channel_id_service_
= channel_id_service
;
100 FraudulentCertificateReporter
* fraudulent_certificate_reporter() const {
101 return fraudulent_certificate_reporter_
;
103 void set_fraudulent_certificate_reporter(
104 FraudulentCertificateReporter
* fraudulent_certificate_reporter
) {
105 fraudulent_certificate_reporter_
= fraudulent_certificate_reporter
;
108 // Get the proxy service for this context.
109 ProxyService
* proxy_service() const { return proxy_service_
; }
110 void set_proxy_service(ProxyService
* proxy_service
) {
111 proxy_service_
= proxy_service
;
114 // Get the ssl config service for this context.
115 SSLConfigService
* ssl_config_service() const {
116 return ssl_config_service_
.get();
118 void set_ssl_config_service(SSLConfigService
* service
) {
119 ssl_config_service_
= service
;
122 // Gets the HTTP Authentication Handler Factory for this context.
123 // The factory is only valid for the lifetime of this URLRequestContext
124 HttpAuthHandlerFactory
* http_auth_handler_factory() const {
125 return http_auth_handler_factory_
;
127 void set_http_auth_handler_factory(HttpAuthHandlerFactory
* factory
) {
128 http_auth_handler_factory_
= factory
;
131 // Gets the http transaction factory for this context.
132 HttpTransactionFactory
* http_transaction_factory() const {
133 return http_transaction_factory_
;
135 void set_http_transaction_factory(HttpTransactionFactory
* factory
) {
136 http_transaction_factory_
= factory
;
139 void set_network_delegate(NetworkDelegate
* network_delegate
) {
140 network_delegate_
= network_delegate
;
142 NetworkDelegate
* network_delegate() const { return network_delegate_
; }
144 void set_http_server_properties(
145 const base::WeakPtr
<HttpServerProperties
>& http_server_properties
) {
146 http_server_properties_
= http_server_properties
;
148 base::WeakPtr
<HttpServerProperties
> http_server_properties() const {
149 return http_server_properties_
;
152 // Gets the cookie store for this context (may be null, in which case
153 // cookies are not stored).
154 CookieStore
* cookie_store() const { return cookie_store_
.get(); }
155 void set_cookie_store(CookieStore
* cookie_store
);
157 TransportSecurityState
* transport_security_state() const {
158 return transport_security_state_
;
160 void set_transport_security_state(
161 TransportSecurityState
* state
) {
162 transport_security_state_
= state
;
165 CTVerifier
* cert_transparency_verifier() const {
166 return cert_transparency_verifier_
;
168 void set_cert_transparency_verifier(CTVerifier
* verifier
) {
169 cert_transparency_verifier_
= verifier
;
172 const URLRequestJobFactory
* job_factory() const { return job_factory_
; }
173 void set_job_factory(const URLRequestJobFactory
* job_factory
) {
174 job_factory_
= job_factory
;
178 URLRequestThrottlerManager
* throttler_manager() const {
179 return throttler_manager_
;
181 void set_throttler_manager(URLRequestThrottlerManager
* throttler_manager
) {
182 throttler_manager_
= throttler_manager
;
186 SdchManager
* sdch_manager() const {
187 // For investigation of http://crbug.com/454198; remove ?: when resolved.
188 CHECK(!have_sdch_manager_
|| sdch_manager_
.get());
189 return have_sdch_manager_
? sdch_manager_
.get() : NULL
;
191 void set_sdch_manager(SdchManager
* sdch_manager
) {
192 // For investigation of http://crbug.com/454198; simplify when resolved.
193 have_sdch_manager_
= !!sdch_manager
;
194 if (have_sdch_manager_
)
195 sdch_manager_
= sdch_manager
->GetWeakPtr();
197 sdch_manager_
.reset();
200 // Gets the URLRequest objects that hold a reference to this
201 // URLRequestContext.
202 std::set
<const URLRequest
*>* url_requests() const {
203 return url_requests_
.get();
206 // CHECKs that no URLRequests using this context remain. Subclasses should
207 // additionally call AssertNoURLRequests() within their own destructor,
208 // prior to implicit destruction of subclass-owned state.
209 void AssertNoURLRequests() const;
211 // Get the underlying |HttpUserAgentSettings| implementation that provides
212 // the HTTP Accept-Language and User-Agent header values.
213 const HttpUserAgentSettings
* http_user_agent_settings() const {
214 return http_user_agent_settings_
;
216 void set_http_user_agent_settings(
217 HttpUserAgentSettings
* http_user_agent_settings
) {
218 http_user_agent_settings_
= http_user_agent_settings
;
222 // ---------------------------------------------------------------------------
223 // Important: When adding any new members below, consider whether they need to
224 // be added to CopyFrom.
225 // ---------------------------------------------------------------------------
227 // Ownership for these members are not defined here. Clients should either
228 // provide storage elsewhere or have a subclass take ownership.
230 HostResolver
* host_resolver_
;
231 CertVerifier
* cert_verifier_
;
232 ChannelIDService
* channel_id_service_
;
233 FraudulentCertificateReporter
* fraudulent_certificate_reporter_
;
234 HttpAuthHandlerFactory
* http_auth_handler_factory_
;
235 ProxyService
* proxy_service_
;
236 scoped_refptr
<SSLConfigService
> ssl_config_service_
;
237 NetworkDelegate
* network_delegate_
;
238 base::WeakPtr
<HttpServerProperties
> http_server_properties_
;
239 HttpUserAgentSettings
* http_user_agent_settings_
;
240 scoped_refptr
<CookieStore
> cookie_store_
;
241 TransportSecurityState
* transport_security_state_
;
242 CTVerifier
* cert_transparency_verifier_
;
243 HttpTransactionFactory
* http_transaction_factory_
;
244 const URLRequestJobFactory
* job_factory_
;
245 URLRequestThrottlerManager
* throttler_manager_
;
246 // For investigation of http://crbug.com/454198; remove WeakPtr when resolved.
247 bool have_sdch_manager_
;
248 base::WeakPtr
<SdchManager
> sdch_manager_
;
250 // ---------------------------------------------------------------------------
251 // Important: When adding any new members below, consider whether they need to
252 // be added to CopyFrom.
253 // ---------------------------------------------------------------------------
255 scoped_ptr
<std::set
<const URLRequest
*> > url_requests_
;
257 DISALLOW_COPY_AND_ASSIGN(URLRequestContext
);
262 #endif // NET_URL_REQUEST_URL_REQUEST_CONTEXT_H_