blob: 992f985f0332b441af1c21614e27a5c48974cd4f [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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"
Torne (Richard Coles)0f1bc082013-11-06 12:27:47 +000022#include "net/base/request_priority.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000023#include "net/http/http_network_session.h"
24#include "net/http/http_server_properties.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000025#include "net/http/transport_security_state.h"
26#include "net/ssl/ssl_config_service.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000027#include "net/url_request/url_request.h"
28
29namespace net {
30class CertVerifier;
Torne (Richard Coles)5f1c9432014-08-12 13:47:38 +010031class ChannelIDService;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000032class CookieStore;
Torne (Richard Coles)a3f6a492013-12-18 16:25:09 +000033class CTVerifier;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000034class FraudulentCertificateReporter;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000035class HostResolver;
36class HttpAuthHandlerFactory;
37class HttpTransactionFactory;
38class HttpUserAgentSettings;
39class NetworkDelegate;
Torne (Richard Coles)f8ee7882014-06-20 14:52:04 +010040class SdchManager;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000041class ProxyService;
42class URLRequest;
43class URLRequestJobFactory;
44class 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.
50class NET_EXPORT URLRequestContext
Ben Murdocheb525c52013-07-10 11:40:50 +010051 : NON_EXPORTED_BASE(public base::NonThreadSafe) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +000052 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
Torne (Richard Coles)03b57e02014-08-28 12:05:23 +010062 // Creates a URLRequest. If |cookie_store| is non-NULL, it will be used
63 // instead of the context's cookie store.
Torne (Richard Coles)0f1bc082013-11-06 12:27:47 +000064 scoped_ptr<URLRequest> CreateRequest(const GURL& url,
65 RequestPriority priority,
Torne (Richard Coles)a1401312014-03-18 10:20:56 +000066 URLRequest::Delegate* delegate,
67 CookieStore* cookie_store) const;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000068
69 NetLog* net_log() const {
70 return net_log_;
71 }
72
73 void set_net_log(NetLog* net_log) {
74 net_log_ = net_log;
75 }
76
77 HostResolver* host_resolver() const {
78 return host_resolver_;
79 }
80
81 void set_host_resolver(HostResolver* host_resolver) {
82 host_resolver_ = host_resolver;
83 }
84
85 CertVerifier* cert_verifier() const {
86 return cert_verifier_;
87 }
88
89 void set_cert_verifier(CertVerifier* cert_verifier) {
90 cert_verifier_ = cert_verifier;
91 }
92
Torne (Richard Coles)5f1c9432014-08-12 13:47:38 +010093 ChannelIDService* channel_id_service() const {
94 return channel_id_service_;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000095 }
96
Torne (Richard Coles)5f1c9432014-08-12 13:47:38 +010097 void set_channel_id_service(
98 ChannelIDService* channel_id_service) {
99 channel_id_service_ = channel_id_service;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000100 }
101
102 FraudulentCertificateReporter* fraudulent_certificate_reporter() const {
103 return fraudulent_certificate_reporter_;
104 }
105 void set_fraudulent_certificate_reporter(
106 FraudulentCertificateReporter* fraudulent_certificate_reporter) {
107 fraudulent_certificate_reporter_ = fraudulent_certificate_reporter;
108 }
109
110 // Get the proxy service for this context.
111 ProxyService* proxy_service() const { return proxy_service_; }
112 void set_proxy_service(ProxyService* proxy_service) {
113 proxy_service_ = proxy_service;
114 }
115
116 // Get the ssl config service for this context.
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100117 SSLConfigService* ssl_config_service() const {
118 return ssl_config_service_.get();
119 }
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000120 void set_ssl_config_service(SSLConfigService* service) {
121 ssl_config_service_ = service;
122 }
123
124 // Gets the HTTP Authentication Handler Factory for this context.
125 // The factory is only valid for the lifetime of this URLRequestContext
126 HttpAuthHandlerFactory* http_auth_handler_factory() const {
127 return http_auth_handler_factory_;
128 }
129 void set_http_auth_handler_factory(HttpAuthHandlerFactory* factory) {
130 http_auth_handler_factory_ = factory;
131 }
132
133 // Gets the http transaction factory for this context.
134 HttpTransactionFactory* http_transaction_factory() const {
135 return http_transaction_factory_;
136 }
137 void set_http_transaction_factory(HttpTransactionFactory* factory) {
138 http_transaction_factory_ = factory;
139 }
140
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000141 void set_network_delegate(NetworkDelegate* network_delegate) {
142 network_delegate_ = network_delegate;
143 }
144 NetworkDelegate* network_delegate() const { return network_delegate_; }
145
146 void set_http_server_properties(
Ben Murdochca12bfa2013-07-23 11:17:05 +0100147 const base::WeakPtr<HttpServerProperties>& http_server_properties) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000148 http_server_properties_ = http_server_properties;
149 }
Ben Murdochca12bfa2013-07-23 11:17:05 +0100150 base::WeakPtr<HttpServerProperties> http_server_properties() const {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000151 return http_server_properties_;
152 }
153
154 // Gets the cookie store for this context (may be null, in which case
155 // cookies are not stored).
156 CookieStore* cookie_store() const { return cookie_store_.get(); }
157 void set_cookie_store(CookieStore* cookie_store);
158
159 TransportSecurityState* transport_security_state() const {
Torne (Richard Coles)a3f6a492013-12-18 16:25:09 +0000160 return transport_security_state_;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000161 }
162 void set_transport_security_state(
163 TransportSecurityState* state) {
164 transport_security_state_ = state;
165 }
166
Torne (Richard Coles)a3f6a492013-12-18 16:25:09 +0000167 CTVerifier* cert_transparency_verifier() const {
168 return cert_transparency_verifier_;
169 }
170 void set_cert_transparency_verifier(CTVerifier* verifier) {
171 cert_transparency_verifier_ = verifier;
172 }
173
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000174 const URLRequestJobFactory* job_factory() const { return job_factory_; }
175 void set_job_factory(const URLRequestJobFactory* job_factory) {
176 job_factory_ = job_factory;
177 }
178
179 // May be NULL.
180 URLRequestThrottlerManager* throttler_manager() const {
181 return throttler_manager_;
182 }
183 void set_throttler_manager(URLRequestThrottlerManager* throttler_manager) {
184 throttler_manager_ = throttler_manager;
185 }
186
Torne (Richard Coles)f8ee7882014-06-20 14:52:04 +0100187 // May be NULL.
188 SdchManager* sdch_manager() const {
189 return sdch_manager_;
190 }
191 void set_sdch_manager(SdchManager* sdch_manager) {
192 sdch_manager_ = sdch_manager;
193 }
194
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000195 // Gets the URLRequest objects that hold a reference to this
196 // URLRequestContext.
197 std::set<const URLRequest*>* url_requests() const {
198 return url_requests_.get();
199 }
200
Torne (Richard Coles)5f1c9432014-08-12 13:47:38 +0100201 // CHECKs that no URLRequests using this context remain. Subclasses should
202 // additionally call AssertNoURLRequests() within their own destructor,
203 // prior to implicit destruction of subclass-owned state.
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000204 void AssertNoURLRequests() const;
205
206 // Get the underlying |HttpUserAgentSettings| implementation that provides
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000207 // the HTTP Accept-Language and User-Agent header values.
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000208 const HttpUserAgentSettings* http_user_agent_settings() const {
209 return http_user_agent_settings_;
210 }
211 void set_http_user_agent_settings(
212 HttpUserAgentSettings* http_user_agent_settings) {
213 http_user_agent_settings_ = http_user_agent_settings;
214 }
215
216 private:
217 // ---------------------------------------------------------------------------
218 // Important: When adding any new members below, consider whether they need to
219 // be added to CopyFrom.
220 // ---------------------------------------------------------------------------
221
222 // Ownership for these members are not defined here. Clients should either
223 // provide storage elsewhere or have a subclass take ownership.
224 NetLog* net_log_;
225 HostResolver* host_resolver_;
226 CertVerifier* cert_verifier_;
Torne (Richard Coles)5f1c9432014-08-12 13:47:38 +0100227 ChannelIDService* channel_id_service_;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000228 FraudulentCertificateReporter* fraudulent_certificate_reporter_;
229 HttpAuthHandlerFactory* http_auth_handler_factory_;
230 ProxyService* proxy_service_;
231 scoped_refptr<SSLConfigService> ssl_config_service_;
232 NetworkDelegate* network_delegate_;
Ben Murdochca12bfa2013-07-23 11:17:05 +0100233 base::WeakPtr<HttpServerProperties> http_server_properties_;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000234 HttpUserAgentSettings* http_user_agent_settings_;
235 scoped_refptr<CookieStore> cookie_store_;
236 TransportSecurityState* transport_security_state_;
Torne (Richard Coles)a3f6a492013-12-18 16:25:09 +0000237 CTVerifier* cert_transparency_verifier_;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000238 HttpTransactionFactory* http_transaction_factory_;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000239 const URLRequestJobFactory* job_factory_;
240 URLRequestThrottlerManager* throttler_manager_;
Torne (Richard Coles)f8ee7882014-06-20 14:52:04 +0100241 SdchManager* sdch_manager_;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000242
243 // ---------------------------------------------------------------------------
244 // Important: When adding any new members below, consider whether they need to
245 // be added to CopyFrom.
246 // ---------------------------------------------------------------------------
247
248 scoped_ptr<std::set<const URLRequest*> > url_requests_;
249
250 DISALLOW_COPY_AND_ASSIGN(URLRequestContext);
251};
252
253} // namespace net
254
255#endif // NET_URL_REQUEST_URL_REQUEST_CONTEXT_H_