blob: cad221e99db28d8395ad3f35ccfeb196d8e1db55 [file] [log] [blame]
[email protected]9045b8822012-01-13 20:35:351// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]0651b812011-02-24 00:22:502// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef NET_BASE_NETWORK_DELEGATE_H_
6#define NET_BASE_NETWORK_DELEGATE_H_
[email protected]0651b812011-02-24 00:22:507
sclittlece72c482015-08-24 20:20:598#include <stdint.h>
9
[email protected]9c8ae8c2012-03-09 13:13:3510#include <string>
11
[email protected]c2911d72011-10-03 22:16:3612#include "base/callback.h"
[email protected]4b355212013-06-11 10:35:1913#include "base/strings/string16.h"
[email protected]0651b812011-02-24 00:22:5014#include "base/threading/non_thread_safe.h"
[email protected]c2911d72011-10-03 22:16:3615#include "net/base/auth.h"
[email protected]05cc4e72011-03-08 21:29:4816#include "net/base/completion_callback.h"
[email protected]8da4b1812012-07-25 13:54:3817#include "net/cookies/canonical_cookie.h"
[email protected]0651b812011-02-24 00:22:5018
[email protected]4c76d7c2011-04-15 19:14:1219class GURL;
20
[email protected]a3ef4832013-02-02 05:12:3321namespace base {
22class FilePath;
23}
24
[email protected]0651b812011-02-24 00:22:5025namespace net {
26
27// NOTE: Layering violations!
28// We decided to accept these violations (depending
29// on other net/ submodules from net/base/), because otherwise NetworkDelegate
30// would have to be broken up into too many smaller interfaces targeted to each
31// submodule. Also, since the lower levels in net/ may callback into higher
32// levels, we may encounter dangerous casting issues.
33//
34// NOTE: It is not okay to add any compile-time dependencies on symbols outside
35// of net/base here, because we have a net_base library. Forward declarations
36// are ok.
[email protected]9c8ae8c2012-03-09 13:13:3537class CookieOptions;
[email protected]0651b812011-02-24 00:22:5038class HttpRequestHeaders;
[email protected]ea8141e2011-10-05 13:12:5139class HttpResponseHeaders;
[email protected]597a1ab2014-06-26 08:12:2740class ProxyInfo;
[email protected]f09f9682014-08-10 01:21:3941class ProxyServer;
[email protected]12731792014-08-14 11:45:5442class ProxyService;
[email protected]0651b812011-02-24 00:22:5043class URLRequest;
44
[email protected]be363b22012-11-01 17:38:4745class NET_EXPORT NetworkDelegate : public base::NonThreadSafe {
[email protected]0651b812011-02-24 00:22:5046 public:
[email protected]c2911d72011-10-03 22:16:3647 // AuthRequiredResponse indicates how a NetworkDelegate handles an
48 // OnAuthRequired call. It's placed in this file to prevent url_request.h
49 // from having to include network_delegate.h.
50 enum AuthRequiredResponse {
51 AUTH_REQUIRED_RESPONSE_NO_ACTION,
52 AUTH_REQUIRED_RESPONSE_SET_AUTH,
53 AUTH_REQUIRED_RESPONSE_CANCEL_AUTH,
54 AUTH_REQUIRED_RESPONSE_IO_PENDING,
55 };
56 typedef base::Callback<void(AuthRequiredResponse)> AuthCallback;
57
[email protected]0651b812011-02-24 00:22:5058 virtual ~NetworkDelegate() {}
59
60 // Notification interface called by the network stack. Note that these
61 // functions mostly forward to the private virtuals. They also add some sanity
[email protected]4875ba12011-03-30 22:31:5162 // checking on parameters. See the corresponding virtuals for explanations of
63 // the methods and their arguments.
64 int NotifyBeforeURLRequest(URLRequest* request,
[email protected]084262c2011-12-01 21:12:4765 const CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:1266 GURL* new_url);
[email protected]12731792014-08-14 11:45:5467 void NotifyResolveProxy(const GURL& url,
68 int load_flags,
69 const ProxyService& proxy_service,
[email protected]a702da72014-07-09 05:23:5470 ProxyInfo* result);
[email protected]f09f9682014-08-10 01:21:3971 void NotifyProxyFallback(const ProxyServer& bad_proxy,
[email protected]d0483b192014-08-15 23:50:1772 int net_error);
[email protected]636eccd2011-06-28 12:28:0173 int NotifyBeforeSendHeaders(URLRequest* request,
[email protected]084262c2011-12-01 21:12:4774 const CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:1275 HttpRequestHeaders* headers);
[email protected]597a1ab2014-06-26 08:12:2776 void NotifyBeforeSendProxyHeaders(URLRequest* request,
77 const ProxyInfo& proxy_info,
78 HttpRequestHeaders* headers);
[email protected]5796dc942011-07-14 19:26:1079 void NotifySendHeaders(URLRequest* request,
[email protected]783573b2011-05-13 11:05:1580 const HttpRequestHeaders& headers);
[email protected]ea8141e2011-10-05 13:12:5181 int NotifyHeadersReceived(
82 URLRequest* request,
[email protected]084262c2011-12-01 21:12:4783 const CompletionCallback& callback,
[email protected]507af8f2012-10-20 00:42:3284 const HttpResponseHeaders* original_response_headers,
[email protected]5f714132014-03-26 10:41:1685 scoped_refptr<HttpResponseHeaders>* override_response_headers,
86 GURL* allowed_unsafe_redirect_url);
[email protected]31b2e5f2011-04-20 16:58:3287 void NotifyBeforeRedirect(URLRequest* request,
88 const GURL& new_location);
[email protected]0651b812011-02-24 00:22:5089 void NotifyResponseStarted(URLRequest* request);
sclittlece72c482015-08-24 20:20:5990 void NotifyNetworkBytesReceived(const URLRequest& request,
91 int64_t bytes_received);
sclittle28d558b2015-09-28 21:40:5292 void NotifyNetworkBytesSent(const URLRequest& request, int64_t bytes_sent);
[email protected]9045b8822012-01-13 20:35:3593 void NotifyCompleted(URLRequest* request, bool started);
[email protected]4875ba12011-03-30 22:31:5194 void NotifyURLRequestDestroyed(URLRequest* request);
davidbena3ef4382015-09-14 22:36:1095 void NotifyURLRequestJobOrphaned(URLRequest* request);
[email protected]42cba2fb2013-03-29 19:58:5796 void NotifyPACScriptError(int line_number, const base::string16& error);
[email protected]c2911d72011-10-03 22:16:3697 AuthRequiredResponse NotifyAuthRequired(URLRequest* request,
98 const AuthChallengeInfo& auth_info,
99 const AuthCallback& callback,
100 AuthCredentials* credentials);
[email protected]4c219e22012-05-05 19:41:04101 bool CanGetCookies(const URLRequest& request,
102 const CookieList& cookie_list);
103 bool CanSetCookie(const URLRequest& request,
104 const std::string& cookie_line,
105 CookieOptions* options);
106 bool CanAccessFile(const URLRequest& request,
[email protected]a3ef4832013-02-02 05:12:33107 const base::FilePath& path) const;
[email protected]e6d017652013-05-17 18:01:40108 bool CanEnablePrivacyMode(const GURL& url,
109 const GURL& first_party_for_cookies) const;
[email protected]82a37672011-05-03 12:02:41110
mkwst0513c9d2015-04-01 05:53:15111 // TODO(mkwst): Remove this once we decide whether or not we wish to ship
112 // first-party cookies. https://crbug.com/459154
113 bool FirstPartyOnlyCookieExperimentEnabled() const;
114
jochen0e3b3a62014-09-16 18:31:23115 bool CancelURLRequestWithPolicyViolatingReferrerHeader(
116 const URLRequest& request,
117 const GURL& target_url,
118 const GURL& referrer_url) const;
119
[email protected]0651b812011-02-24 00:22:50120 private:
[email protected]4c219e22012-05-05 19:41:04121 // This is the interface for subclasses of NetworkDelegate to implement. These
[email protected]0651b812011-02-24 00:22:50122 // member functions will be called by the respective public notification
123 // member function, which will perform basic sanity checking.
124
[email protected]4c76d7c2011-04-15 19:14:12125 // Called before a request is sent. Allows the delegate to rewrite the URL
[email protected]f878230e2014-04-03 15:36:14126 // being fetched by modifying |new_url|. If set, the URL must be valid. The
127 // reference fragment from the original URL is not automatically appended to
128 // |new_url|; callers are responsible for copying the reference fragment if
129 // desired.
130 // |callback| and |new_url| are valid only until OnURLRequestDestroyed is
131 // called for this request. Returns a net status code, generally either OK to
132 // continue with the request or ERR_IO_PENDING if the result is not ready yet.
133 // A status code other than OK and ERR_IO_PENDING will cancel the request and
134 // report the status code as the reason.
[email protected]c6c6e5652013-10-29 02:40:30135 //
136 // The default implementation returns OK (continue with request).
[email protected]4875ba12011-03-30 22:31:51137 virtual int OnBeforeURLRequest(URLRequest* request,
[email protected]084262c2011-12-01 21:12:47138 const CompletionCallback& callback,
megjablonc1751452014-12-09 19:46:47139 GURL* new_url) = 0;
[email protected]0651b812011-02-24 00:22:50140
[email protected]a702da72014-07-09 05:23:54141 // Called as the proxy is being resolved for |url|. Allows the delegate to
142 // override the proxy resolution decision made by ProxyService. The delegate
143 // may override the decision by modifying the ProxyInfo |result|.
144 virtual void OnResolveProxy(const GURL& url,
145 int load_flags,
[email protected]12731792014-08-14 11:45:54146 const ProxyService& proxy_service,
megjablonc1751452014-12-09 19:46:47147 ProxyInfo* result) = 0;
[email protected]a702da72014-07-09 05:23:54148
[email protected]d0483b192014-08-15 23:50:17149 // Called when use of |bad_proxy| fails due to |net_error|. |net_error| is
150 // the network error encountered, if any, and OK if the fallback was
151 // for a reason other than a network error (e.g. the proxy service was
152 // explicitly directed to skip a proxy).
megjablonc1751452014-12-09 19:46:47153 virtual void OnProxyFallback(const ProxyServer& bad_proxy, int net_error) = 0;
[email protected]f09f9682014-08-10 01:21:39154
[email protected]4875ba12011-03-30 22:31:51155 // Called right before the HTTP headers are sent. Allows the delegate to
[email protected]5aa20132011-04-27 23:11:34156 // read/write |headers| before they get sent out. |callback| and |headers| are
[email protected]9045b8822012-01-13 20:35:35157 // valid only until OnCompleted or OnURLRequestDestroyed is called for this
158 // request.
[email protected]c6c6e5652013-10-29 02:40:30159 // See OnBeforeURLRequest for return value description. Returns OK by default.
[email protected]636eccd2011-06-28 12:28:01160 virtual int OnBeforeSendHeaders(URLRequest* request,
[email protected]084262c2011-12-01 21:12:47161 const CompletionCallback& callback,
megjablonc1751452014-12-09 19:46:47162 HttpRequestHeaders* headers) = 0;
[email protected]0651b812011-02-24 00:22:50163
[email protected]597a1ab2014-06-26 08:12:27164 // Called after a proxy connection. Allows the delegate to read/write
165 // |headers| before they get sent out. |headers| is valid only until
166 // OnCompleted or OnURLRequestDestroyed is called for this request.
167 virtual void OnBeforeSendProxyHeaders(URLRequest* request,
168 const ProxyInfo& proxy_info,
megjablonc1751452014-12-09 19:46:47169 HttpRequestHeaders* headers) = 0;
[email protected]597a1ab2014-06-26 08:12:27170
[email protected]5796dc942011-07-14 19:26:10171 // Called right before the HTTP request(s) are being sent to the network.
[email protected]9045b8822012-01-13 20:35:35172 // |headers| is only valid until OnCompleted or OnURLRequestDestroyed is
173 // called for this request.
[email protected]5796dc942011-07-14 19:26:10174 virtual void OnSendHeaders(URLRequest* request,
megjablonc1751452014-12-09 19:46:47175 const HttpRequestHeaders& headers) = 0;
[email protected]82b42302011-04-20 16:28:16176
[email protected]c6c6e5652013-10-29 02:40:30177 // Called for HTTP requests when the headers have been received.
[email protected]ea8141e2011-10-05 13:12:51178 // |original_response_headers| contains the headers as received over the
179 // network, these must not be modified. |override_response_headers| can be set
180 // to new values, that should be considered as overriding
181 // |original_response_headers|.
[email protected]f878230e2014-04-03 15:36:14182 // If the response is a redirect, and the Location response header value is
183 // identical to |allowed_unsafe_redirect_url|, then the redirect is never
184 // blocked and the reference fragment is not copied from the original URL
185 // to the redirection target.
186 //
[email protected]9045b8822012-01-13 20:35:35187 // |callback|, |original_response_headers|, and |override_response_headers|
188 // are only valid until OnURLRequestDestroyed is called for this request.
[email protected]c6c6e5652013-10-29 02:40:30189 // See OnBeforeURLRequest for return value description. Returns OK by default.
[email protected]ea8141e2011-10-05 13:12:51190 virtual int OnHeadersReceived(
191 URLRequest* request,
[email protected]084262c2011-12-01 21:12:47192 const CompletionCallback& callback,
[email protected]507af8f2012-10-20 00:42:32193 const HttpResponseHeaders* original_response_headers,
[email protected]5f714132014-03-26 10:41:16194 scoped_refptr<HttpResponseHeaders>* override_response_headers,
megjablonc1751452014-12-09 19:46:47195 GURL* allowed_unsafe_redirect_url) = 0;
[email protected]ea8141e2011-10-05 13:12:51196
[email protected]31b2e5f2011-04-20 16:58:32197 // Called right after a redirect response code was received.
[email protected]9045b8822012-01-13 20:35:35198 // |new_location| is only valid until OnURLRequestDestroyed is called for this
199 // request.
[email protected]31b2e5f2011-04-20 16:58:32200 virtual void OnBeforeRedirect(URLRequest* request,
megjablonc1751452014-12-09 19:46:47201 const GURL& new_location) = 0;
[email protected]31b2e5f2011-04-20 16:58:32202
[email protected]0651b812011-02-24 00:22:50203 // This corresponds to URLRequestDelegate::OnResponseStarted.
megjablonc1751452014-12-09 19:46:47204 virtual void OnResponseStarted(URLRequest* request) = 0;
[email protected]0651b812011-02-24 00:22:50205
sclittlece72c482015-08-24 20:20:59206 // Called when bytes are received from the network, such as after receiving
207 // headers or reading raw response bytes. This includes localhost requests.
208 // |bytes_received| is the number of bytes measured at the application layer
209 // that have been received over the network for this request since the last
210 // time OnNetworkBytesReceived was called. |bytes_received| will always be
211 // greater than 0.
212 // Currently, this is only implemented for HTTP transactions, and
213 // |bytes_received| does not include TLS overhead or TCP retransmits.
214 virtual void OnNetworkBytesReceived(const URLRequest& request,
215 int64_t bytes_received) = 0;
[email protected]8523ba52011-05-22 19:00:58216
sclittle28d558b2015-09-28 21:40:52217 // Called when bytes are sent over the network, such as when sending request
218 // headers or uploading request body bytes. This includes localhost requests.
219 // |bytes_sent| is the number of bytes measured at the application layer that
220 // have been sent over the network for this request since the last time
221 // OnNetworkBytesSent was called. |bytes_sent| will always be greater than 0.
222 // Currently, this is only implemented for HTTP transactions, and |bytes_sent|
223 // does not include TLS overhead or TCP retransmits.
224 virtual void OnNetworkBytesSent(const URLRequest& request,
225 int64_t bytes_sent) = 0;
226
[email protected]48944382011-04-23 13:28:16227 // Indicates that the URL request has been completed or failed.
[email protected]9045b8822012-01-13 20:35:35228 // |started| indicates whether the request has been started. If false,
229 // some information like the socket address is not available.
megjablonc1751452014-12-09 19:46:47230 virtual void OnCompleted(URLRequest* request, bool started) = 0;
[email protected]4b50cb52011-03-10 00:29:37231
[email protected]4875ba12011-03-30 22:31:51232 // Called when an URLRequest is being destroyed. Note that the request is
233 // being deleted, so it's not safe to call any methods that may result in
234 // a virtual method call.
megjablonc1751452014-12-09 19:46:47235 virtual void OnURLRequestDestroyed(URLRequest* request) = 0;
[email protected]4875ba12011-03-30 22:31:51236
davidbena3ef4382015-09-14 22:36:10237 // Called when the current job for |request| is orphaned. This is a temporary
238 // callback to diagnose https://crbug.com/289715 and may not be used for other
239 // purposes. Note that it may be called after OnURLRequestDestroyed.
240 //
241 // TODO(davidben): Remove this once data has been gathered.
242 virtual void OnURLRequestJobOrphaned(URLRequest* request) = 0;
243
[email protected]82a37672011-05-03 12:02:41244 // Corresponds to ProxyResolverJSBindings::OnError.
[email protected]42cba2fb2013-03-29 19:58:57245 virtual void OnPACScriptError(int line_number,
megjablonc1751452014-12-09 19:46:47246 const base::string16& error) = 0;
[email protected]7efc582d2011-08-03 20:46:35247
[email protected]c2911d72011-10-03 22:16:36248 // Called when a request receives an authentication challenge
249 // specified by |auth_info|, and is unable to respond using cached
250 // credentials. |callback| and |credentials| must be non-NULL, and must
251 // be valid until OnURLRequestDestroyed is called for |request|.
252 //
253 // The following return values are allowed:
254 // - AUTH_REQUIRED_RESPONSE_NO_ACTION: |auth_info| is observed, but
255 // no action is being taken on it.
256 // - AUTH_REQUIRED_RESPONSE_SET_AUTH: |credentials| is filled in with
257 // a username and password, which should be used in a response to
258 // |auth_info|.
259 // - AUTH_REQUIRED_RESPONSE_CANCEL_AUTH: The authentication challenge
260 // should not be attempted.
261 // - AUTH_REQUIRED_RESPONSE_IO_PENDING: The action will be decided
262 // asynchronously. |callback| will be invoked when the decision is made,
263 // and one of the other AuthRequiredResponse values will be passed in with
264 // the same semantics as described above.
265 virtual AuthRequiredResponse OnAuthRequired(
266 URLRequest* request,
267 const AuthChallengeInfo& auth_info,
268 const AuthCallback& callback,
megjablonc1751452014-12-09 19:46:47269 AuthCredentials* credentials) = 0;
[email protected]9c8ae8c2012-03-09 13:13:35270
271 // Called when reading cookies to allow the network delegate to block access
272 // to the cookie. This method will never be invoked when
273 // LOAD_DO_NOT_SEND_COOKIES is specified.
[email protected]4c219e22012-05-05 19:41:04274 virtual bool OnCanGetCookies(const URLRequest& request,
megjablonc1751452014-12-09 19:46:47275 const CookieList& cookie_list) = 0;
[email protected]9c8ae8c2012-03-09 13:13:35276
277 // Called when a cookie is set to allow the network delegate to block access
278 // to the cookie. This method will never be invoked when
279 // LOAD_DO_NOT_SAVE_COOKIES is specified.
[email protected]4c219e22012-05-05 19:41:04280 virtual bool OnCanSetCookie(const URLRequest& request,
281 const std::string& cookie_line,
megjablonc1751452014-12-09 19:46:47282 CookieOptions* options) = 0;
[email protected]4c219e22012-05-05 19:41:04283
[email protected]4c219e22012-05-05 19:41:04284 // Called when a file access is attempted to allow the network delegate to
285 // allow or block access to the given file path. Returns true if access is
286 // allowed.
287 virtual bool OnCanAccessFile(const URLRequest& request,
megjablonc1751452014-12-09 19:46:47288 const base::FilePath& path) const = 0;
[email protected]9c8ae8c2012-03-09 13:13:35289
[email protected]e6d017652013-05-17 18:01:40290 // Returns true if the given |url| has to be requested over connection that
291 // is not tracked by the server. Usually is false, unless user privacy
292 // settings block cookies from being get or set.
293 virtual bool OnCanEnablePrivacyMode(
294 const GURL& url,
megjablonc1751452014-12-09 19:46:47295 const GURL& first_party_for_cookies) const = 0;
[email protected]e6d017652013-05-17 18:01:40296
mkwst0513c9d2015-04-01 05:53:15297 // Returns true if the embedder has enabled the "first-party" cookie
298 // experiment, and false otherwise.
299 //
300 // TODO(mkwst): Remove this once we decide whether or not we wish to ship
301 // first-party cookies. https://crbug.com/459154
302 virtual bool OnFirstPartyOnlyCookieExperimentEnabled() const = 0;
303
jochen0e3b3a62014-09-16 18:31:23304 // Called when the |referrer_url| for requesting |target_url| during handling
305 // of the |request| is does not comply with the referrer policy (e.g. a
306 // secure referrer for an insecure initial target).
307 // Returns true if the request should be cancelled. Otherwise, the referrer
308 // header is stripped from the request.
309 virtual bool OnCancelURLRequestWithPolicyViolatingReferrerHeader(
310 const URLRequest& request,
311 const GURL& target_url,
megjablonc1751452014-12-09 19:46:47312 const GURL& referrer_url) const = 0;
[email protected]0651b812011-02-24 00:22:50313};
314
315} // namespace net
316
317#endif // NET_BASE_NETWORK_DELEGATE_H_