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 #ifndef NET_BASE_NETWORK_DELEGATE_H_
6 #define NET_BASE_NETWORK_DELEGATE_H_
10 #include "base/callback.h"
11 #include "base/strings/string16.h"
12 #include "base/threading/non_thread_safe.h"
13 #include "net/base/auth.h"
14 #include "net/base/completion_callback.h"
15 #include "net/cookies/canonical_cookie.h"
25 // NOTE: Layering violations!
26 // We decided to accept these violations (depending
27 // on other net/ submodules from net/base/), because otherwise NetworkDelegate
28 // would have to be broken up into too many smaller interfaces targeted to each
29 // submodule. Also, since the lower levels in net/ may callback into higher
30 // levels, we may encounter dangerous casting issues.
32 // NOTE: It is not okay to add any compile-time dependencies on symbols outside
33 // of net/base here, because we have a net_base library. Forward declarations
36 class HttpRequestHeaders
;
37 class HttpResponseHeaders
;
42 class NET_EXPORT NetworkDelegate
: public base::NonThreadSafe
{
44 // AuthRequiredResponse indicates how a NetworkDelegate handles an
45 // OnAuthRequired call. It's placed in this file to prevent url_request.h
46 // from having to include network_delegate.h.
47 enum AuthRequiredResponse
{
48 AUTH_REQUIRED_RESPONSE_NO_ACTION
,
49 AUTH_REQUIRED_RESPONSE_SET_AUTH
,
50 AUTH_REQUIRED_RESPONSE_CANCEL_AUTH
,
51 AUTH_REQUIRED_RESPONSE_IO_PENDING
,
53 typedef base::Callback
<void(AuthRequiredResponse
)> AuthCallback
;
55 virtual ~NetworkDelegate() {}
57 // Notification interface called by the network stack. Note that these
58 // functions mostly forward to the private virtuals. They also add some sanity
59 // checking on parameters. See the corresponding virtuals for explanations of
60 // the methods and their arguments.
61 int NotifyBeforeURLRequest(URLRequest
* request
,
62 const CompletionCallback
& callback
,
64 void NotifyResolveProxy(const GURL
& url
, int load_flags
,
66 int NotifyBeforeSendHeaders(URLRequest
* request
,
67 const CompletionCallback
& callback
,
68 HttpRequestHeaders
* headers
);
69 void NotifyBeforeSendProxyHeaders(URLRequest
* request
,
70 const ProxyInfo
& proxy_info
,
71 HttpRequestHeaders
* headers
);
72 void NotifySendHeaders(URLRequest
* request
,
73 const HttpRequestHeaders
& headers
);
74 int NotifyHeadersReceived(
76 const CompletionCallback
& callback
,
77 const HttpResponseHeaders
* original_response_headers
,
78 scoped_refptr
<HttpResponseHeaders
>* override_response_headers
,
79 GURL
* allowed_unsafe_redirect_url
);
80 void NotifyBeforeRedirect(URLRequest
* request
,
81 const GURL
& new_location
);
82 void NotifyResponseStarted(URLRequest
* request
);
83 void NotifyRawBytesRead(const URLRequest
& request
, int bytes_read
);
84 void NotifyCompleted(URLRequest
* request
, bool started
);
85 void NotifyURLRequestDestroyed(URLRequest
* request
);
86 void NotifyPACScriptError(int line_number
, const base::string16
& error
);
87 AuthRequiredResponse
NotifyAuthRequired(URLRequest
* request
,
88 const AuthChallengeInfo
& auth_info
,
89 const AuthCallback
& callback
,
90 AuthCredentials
* credentials
);
91 bool CanGetCookies(const URLRequest
& request
,
92 const CookieList
& cookie_list
);
93 bool CanSetCookie(const URLRequest
& request
,
94 const std::string
& cookie_line
,
95 CookieOptions
* options
);
96 bool CanAccessFile(const URLRequest
& request
,
97 const base::FilePath
& path
) const;
98 bool CanThrottleRequest(const URLRequest
& request
) const;
99 bool CanEnablePrivacyMode(const GURL
& url
,
100 const GURL
& first_party_for_cookies
) const;
102 int NotifyBeforeSocketStreamConnect(SocketStream
* socket
,
103 const CompletionCallback
& callback
);
106 // This is the interface for subclasses of NetworkDelegate to implement. These
107 // member functions will be called by the respective public notification
108 // member function, which will perform basic sanity checking.
110 // Called before a request is sent. Allows the delegate to rewrite the URL
111 // being fetched by modifying |new_url|. If set, the URL must be valid. The
112 // reference fragment from the original URL is not automatically appended to
113 // |new_url|; callers are responsible for copying the reference fragment if
115 // |callback| and |new_url| are valid only until OnURLRequestDestroyed is
116 // called for this request. Returns a net status code, generally either OK to
117 // continue with the request or ERR_IO_PENDING if the result is not ready yet.
118 // A status code other than OK and ERR_IO_PENDING will cancel the request and
119 // report the status code as the reason.
121 // The default implementation returns OK (continue with request).
122 virtual int OnBeforeURLRequest(URLRequest
* request
,
123 const CompletionCallback
& callback
,
126 // Called as the proxy is being resolved for |url|. Allows the delegate to
127 // override the proxy resolution decision made by ProxyService. The delegate
128 // may override the decision by modifying the ProxyInfo |result|.
129 virtual void OnResolveProxy(const GURL
& url
,
133 // Called right before the HTTP headers are sent. Allows the delegate to
134 // read/write |headers| before they get sent out. |callback| and |headers| are
135 // valid only until OnCompleted or OnURLRequestDestroyed is called for this
137 // See OnBeforeURLRequest for return value description. Returns OK by default.
138 virtual int OnBeforeSendHeaders(URLRequest
* request
,
139 const CompletionCallback
& callback
,
140 HttpRequestHeaders
* headers
);
142 // Called after a proxy connection. Allows the delegate to read/write
143 // |headers| before they get sent out. |headers| is valid only until
144 // OnCompleted or OnURLRequestDestroyed is called for this request.
145 virtual void OnBeforeSendProxyHeaders(URLRequest
* request
,
146 const ProxyInfo
& proxy_info
,
147 HttpRequestHeaders
* headers
);
149 // Called right before the HTTP request(s) are being sent to the network.
150 // |headers| is only valid until OnCompleted or OnURLRequestDestroyed is
151 // called for this request.
152 virtual void OnSendHeaders(URLRequest
* request
,
153 const HttpRequestHeaders
& headers
);
155 // Called for HTTP requests when the headers have been received.
156 // |original_response_headers| contains the headers as received over the
157 // network, these must not be modified. |override_response_headers| can be set
158 // to new values, that should be considered as overriding
159 // |original_response_headers|.
160 // If the response is a redirect, and the Location response header value is
161 // identical to |allowed_unsafe_redirect_url|, then the redirect is never
162 // blocked and the reference fragment is not copied from the original URL
163 // to the redirection target.
165 // |callback|, |original_response_headers|, and |override_response_headers|
166 // are only valid until OnURLRequestDestroyed is called for this request.
167 // See OnBeforeURLRequest for return value description. Returns OK by default.
168 virtual int OnHeadersReceived(
170 const CompletionCallback
& callback
,
171 const HttpResponseHeaders
* original_response_headers
,
172 scoped_refptr
<HttpResponseHeaders
>* override_response_headers
,
173 GURL
* allowed_unsafe_redirect_url
);
175 // Called right after a redirect response code was received.
176 // |new_location| is only valid until OnURLRequestDestroyed is called for this
178 virtual void OnBeforeRedirect(URLRequest
* request
,
179 const GURL
& new_location
);
181 // This corresponds to URLRequestDelegate::OnResponseStarted.
182 virtual void OnResponseStarted(URLRequest
* request
);
184 // Called every time we read raw bytes.
185 virtual void OnRawBytesRead(const URLRequest
& request
, int bytes_read
);
187 // Indicates that the URL request has been completed or failed.
188 // |started| indicates whether the request has been started. If false,
189 // some information like the socket address is not available.
190 virtual void OnCompleted(URLRequest
* request
, bool started
);
192 // Called when an URLRequest is being destroyed. Note that the request is
193 // being deleted, so it's not safe to call any methods that may result in
194 // a virtual method call.
195 virtual void OnURLRequestDestroyed(URLRequest
* request
);
197 // Corresponds to ProxyResolverJSBindings::OnError.
198 virtual void OnPACScriptError(int line_number
,
199 const base::string16
& error
);
201 // Called when a request receives an authentication challenge
202 // specified by |auth_info|, and is unable to respond using cached
203 // credentials. |callback| and |credentials| must be non-NULL, and must
204 // be valid until OnURLRequestDestroyed is called for |request|.
206 // The following return values are allowed:
207 // - AUTH_REQUIRED_RESPONSE_NO_ACTION: |auth_info| is observed, but
208 // no action is being taken on it.
209 // - AUTH_REQUIRED_RESPONSE_SET_AUTH: |credentials| is filled in with
210 // a username and password, which should be used in a response to
212 // - AUTH_REQUIRED_RESPONSE_CANCEL_AUTH: The authentication challenge
213 // should not be attempted.
214 // - AUTH_REQUIRED_RESPONSE_IO_PENDING: The action will be decided
215 // asynchronously. |callback| will be invoked when the decision is made,
216 // and one of the other AuthRequiredResponse values will be passed in with
217 // the same semantics as described above.
218 virtual AuthRequiredResponse
OnAuthRequired(
220 const AuthChallengeInfo
& auth_info
,
221 const AuthCallback
& callback
,
222 AuthCredentials
* credentials
);
224 // Called when reading cookies to allow the network delegate to block access
225 // to the cookie. This method will never be invoked when
226 // LOAD_DO_NOT_SEND_COOKIES is specified.
227 virtual bool OnCanGetCookies(const URLRequest
& request
,
228 const CookieList
& cookie_list
);
230 // Called when a cookie is set to allow the network delegate to block access
231 // to the cookie. This method will never be invoked when
232 // LOAD_DO_NOT_SAVE_COOKIES is specified.
233 virtual bool OnCanSetCookie(const URLRequest
& request
,
234 const std::string
& cookie_line
,
235 CookieOptions
* options
);
237 // Called when a file access is attempted to allow the network delegate to
238 // allow or block access to the given file path. Returns true if access is
240 virtual bool OnCanAccessFile(const URLRequest
& request
,
241 const base::FilePath
& path
) const;
243 // Returns true if the given request may be rejected when the
244 // URLRequestThrottlerManager believes the server servicing the
245 // request is overloaded or down.
246 virtual bool OnCanThrottleRequest(const URLRequest
& request
) const;
248 // Returns true if the given |url| has to be requested over connection that
249 // is not tracked by the server. Usually is false, unless user privacy
250 // settings block cookies from being get or set.
251 virtual bool OnCanEnablePrivacyMode(
253 const GURL
& first_party_for_cookies
) const;
255 // Called before a SocketStream tries to connect.
256 // See OnBeforeURLRequest for return value description. Returns OK by default.
257 virtual int OnBeforeSocketStreamConnect(
258 SocketStream
* socket
, const CompletionCallback
& callback
);
263 #endif // NET_BASE_NETWORK_DELEGATE_H_