1// Copyright (c) 2010 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 "base/ref_counted.h"
14#include "base/string_util.h"
15#include "net/base/cookie_store.h"
16#include "net/base/host_resolver.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/url_request/request_tracker.h"
22
23namespace net {
24class CookiePolicy;
25class FtpTransactionFactory;
26class HttpTransactionFactory;
27class SocketStream;
28}
29class URLRequest;
30
31// Subclass to provide application-specific context for URLRequest instances.
32class URLRequestContext :
33    public base::RefCountedThreadSafe<URLRequestContext> {
34 public:
35  URLRequestContext()
36      : http_transaction_factory_(NULL),
37        ftp_transaction_factory_(NULL),
38        cookie_policy_(NULL),
39        transport_security_state_(NULL) {
40  }
41
42  net::HostResolver* host_resolver() const {
43    return host_resolver_;
44  }
45
46  // Get the proxy service for this context.
47  net::ProxyService* proxy_service() const {
48    return proxy_service_;
49  }
50
51  // Get the ssl config service for this context.
52  net::SSLConfigService* ssl_config_service() const {
53    return ssl_config_service_;
54  }
55
56  // Gets the http transaction factory for this context.
57  net::HttpTransactionFactory* http_transaction_factory() const {
58    return http_transaction_factory_;
59  }
60
61  // Gets the ftp transaction factory for this context.
62  net::FtpTransactionFactory* ftp_transaction_factory() {
63    return ftp_transaction_factory_;
64  }
65
66  // Gets the cookie store for this context (may be null, in which case
67  // cookies are not stored).
68  net::CookieStore* cookie_store() { return cookie_store_.get(); }
69
70  // Gets the cookie policy for this context (may be null, in which case
71  // cookies are allowed).
72  net::CookiePolicy* cookie_policy() { return cookie_policy_; }
73
74  net::TransportSecurityState* transport_security_state() {
75      return transport_security_state_; }
76
77  // Gets the FTP authentication cache for this context.
78  net::FtpAuthCache* ftp_auth_cache() { return &ftp_auth_cache_; }
79
80  // Gets the value of 'Accept-Charset' header field.
81  const std::string& accept_charset() const { return accept_charset_; }
82
83  // Gets the value of 'Accept-Language' header field.
84  const std::string& accept_language() const { return accept_language_; }
85
86  // Gets the tracker for URLRequests associated with this context.
87  RequestTracker<URLRequest>* url_request_tracker() {
88    return &url_request_tracker_;
89  }
90
91  // Gets the tracker for SocketStreams associated with this context.
92  RequestTracker<net::SocketStream>* socket_stream_tracker() {
93    return &socket_stream_tracker_;
94  }
95
96  // Gets the UA string to use for the given URL.  Pass an invalid URL (such as
97  // GURL()) to get the default UA string.  Subclasses should override this
98  // method to provide a UA string.
99  virtual const std::string& GetUserAgent(const GURL& url) const {
100    return EmptyString();
101  }
102
103  // In general, referrer_charset is not known when URLRequestContext is
104  // constructed. So, we need a setter.
105  const std::string& referrer_charset() const { return referrer_charset_; }
106  void set_referrer_charset(const std::string& charset) {
107    referrer_charset_ = charset;
108  }
109
110  // Called before adding cookies to requests. Returns true if cookie can
111  // be added to the request. The cookie might still be modified though.
112  virtual bool InterceptRequestCookies(const URLRequest* request,
113                                       const std::string& cookies) const {
114    return true;
115  }
116
117  // Called before adding cookies from respones to the cookie monster. Returns
118  // true if the cookie can be added. The cookie might still be modified though.
119  virtual bool InterceptResponseCookie(const URLRequest* request,
120                                       const std::string& cookie) const {
121    return true;
122  }
123
124 protected:
125  friend class base::RefCountedThreadSafe<URLRequestContext>;
126
127  virtual ~URLRequestContext() {}
128
129  // The following members are expected to be initialized and owned by
130  // subclasses.
131  scoped_refptr<net::HostResolver> host_resolver_;
132  scoped_refptr<net::ProxyService> proxy_service_;
133  scoped_refptr<net::SSLConfigService> ssl_config_service_;
134  net::HttpTransactionFactory* http_transaction_factory_;
135  net::FtpTransactionFactory* ftp_transaction_factory_;
136  scoped_refptr<net::CookieStore> cookie_store_;
137  net::CookiePolicy* cookie_policy_;
138  scoped_refptr<net::TransportSecurityState> transport_security_state_;
139  net::FtpAuthCache ftp_auth_cache_;
140  std::string accept_language_;
141  std::string accept_charset_;
142  // The charset of the referrer where this request comes from. It's not
143  // used in communication with a server but is used to construct a suggested
144  // filename for file download.
145  std::string referrer_charset_;
146
147  // Tracks the requests associated with this context.
148  RequestTracker<URLRequest> url_request_tracker_;
149
150  // Trakcs the socket streams associated with this context.
151  RequestTracker<net::SocketStream> socket_stream_tracker_;
152
153 private:
154  DISALLOW_COPY_AND_ASSIGN(URLRequestContext);
155};
156
157#endif  // NET_URL_REQUEST_URL_REQUEST_CONTEXT_H_
158