blob: 49fe875c7d22b4880a7a97dda36ffa401855bdcc [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
Douglas Creager7b07ea42018-02-27 21:08:0810#include <set>
[email protected]9c8ae8c2012-03-09 13:13:3511#include <string>
12
[email protected]c2911d72011-10-03 22:16:3613#include "base/callback.h"
[email protected]4b355212013-06-11 10:35:1914#include "base/strings/string16.h"
gab47aa7da2017-06-02 16:09:4315#include "base/threading/thread_checker.h"
[email protected]c2911d72011-10-03 22:16:3616#include "net/base/auth.h"
David Benjamind1f287bf2018-06-12 01:57:2017#include "net/base/completion_once_callback.h"
bnc81c46c1f2016-10-04 16:25:5918#include "net/base/net_export.h"
[email protected]8da4b1812012-07-25 13:54:3819#include "net/cookies/canonical_cookie.h"
Lily Houghton582d4622018-01-22 22:43:4020#include "net/proxy_resolution/proxy_retry_info.h"
[email protected]0651b812011-02-24 00:22:5021
[email protected]4c76d7c2011-04-15 19:14:1222class GURL;
23
[email protected]a3ef4832013-02-02 05:12:3324namespace base {
25class FilePath;
26}
27
juliatuttlefcf47202017-05-23 15:53:0228namespace url {
29class Origin;
30}
31
[email protected]0651b812011-02-24 00:22:5032namespace net {
33
34// NOTE: Layering violations!
35// We decided to accept these violations (depending
36// on other net/ submodules from net/base/), because otherwise NetworkDelegate
37// would have to be broken up into too many smaller interfaces targeted to each
38// submodule. Also, since the lower levels in net/ may callback into higher
39// levels, we may encounter dangerous casting issues.
40//
41// NOTE: It is not okay to add any compile-time dependencies on symbols outside
42// of net/base here, because we have a net_base library. Forward declarations
43// are ok.
[email protected]9c8ae8c2012-03-09 13:13:3544class CookieOptions;
[email protected]0651b812011-02-24 00:22:5045class HttpRequestHeaders;
[email protected]ea8141e2011-10-05 13:12:5146class HttpResponseHeaders;
[email protected]597a1ab2014-06-26 08:12:2747class ProxyInfo;
[email protected]0651b812011-02-24 00:22:5048class URLRequest;
49
gab47aa7da2017-06-02 16:09:4350class NET_EXPORT NetworkDelegate {
[email protected]0651b812011-02-24 00:22:5051 public:
[email protected]c2911d72011-10-03 22:16:3652 // AuthRequiredResponse indicates how a NetworkDelegate handles an
53 // OnAuthRequired call. It's placed in this file to prevent url_request.h
54 // from having to include network_delegate.h.
55 enum AuthRequiredResponse {
56 AUTH_REQUIRED_RESPONSE_NO_ACTION,
57 AUTH_REQUIRED_RESPONSE_SET_AUTH,
58 AUTH_REQUIRED_RESPONSE_CANCEL_AUTH,
59 AUTH_REQUIRED_RESPONSE_IO_PENDING,
60 };
David Benjamind1f287bf2018-06-12 01:57:2061 using AuthCallback = base::OnceCallback<void(AuthRequiredResponse)>;
[email protected]c2911d72011-10-03 22:16:3662
gab47aa7da2017-06-02 16:09:4363 virtual ~NetworkDelegate();
[email protected]0651b812011-02-24 00:22:5064
65 // Notification interface called by the network stack. Note that these
66 // functions mostly forward to the private virtuals. They also add some sanity
[email protected]4875ba12011-03-30 22:31:5167 // checking on parameters. See the corresponding virtuals for explanations of
68 // the methods and their arguments.
69 int NotifyBeforeURLRequest(URLRequest* request,
David Benjamind1f287bf2018-06-12 01:57:2070 CompletionOnceCallback callback,
[email protected]4c76d7c2011-04-15 19:14:1271 GURL* new_url);
ryansturm2343cb62016-06-15 01:09:0072 int NotifyBeforeStartTransaction(URLRequest* request,
David Benjamind1f287bf2018-06-12 01:57:2073 CompletionOnceCallback callback,
ryansturm2343cb62016-06-15 01:09:0074 HttpRequestHeaders* headers);
ryansturm49a8cb12016-06-15 16:51:0975 void NotifyBeforeSendHeaders(URLRequest* request,
76 const ProxyInfo& proxy_info,
77 const ProxyRetryInfoMap& proxy_retry_info,
78 HttpRequestHeaders* headers);
ryansturm2343cb62016-06-15 01:09:0079 void NotifyStartTransaction(URLRequest* request,
80 const HttpRequestHeaders& headers);
[email protected]ea8141e2011-10-05 13:12:5181 int NotifyHeadersReceived(
82 URLRequest* request,
David Benjamind1f287bf2018-06-12 01:57:2083 CompletionOnceCallback 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);
maksim.sisov0f4aa142016-09-05 05:55:2889 void NotifyResponseStarted(URLRequest* request, int net_error);
sclittlea133de02015-11-10 23:54:2190 void NotifyNetworkBytesReceived(URLRequest* request, int64_t bytes_received);
91 void NotifyNetworkBytesSent(URLRequest* request, int64_t bytes_sent);
maksim.sisov0f4aa142016-09-05 05:55:2892 void NotifyCompleted(URLRequest* request, bool started, int net_error);
[email protected]4875ba12011-03-30 22:31:5193 void NotifyURLRequestDestroyed(URLRequest* request);
[email protected]42cba2fb2013-03-29 19:58:5794 void NotifyPACScriptError(int line_number, const base::string16& error);
[email protected]c2911d72011-10-03 22:16:3695 AuthRequiredResponse NotifyAuthRequired(URLRequest* request,
96 const AuthChallengeInfo& auth_info,
David Benjamind1f287bf2018-06-12 01:57:2097 AuthCallback callback,
[email protected]c2911d72011-10-03 22:16:3698 AuthCredentials* credentials);
Clark DuValle8737642018-08-31 17:26:3499 bool CanGetCookies(const URLRequest& request,
100 const CookieList& cookie_list,
101 bool allowed_from_caller);
[email protected]4c219e22012-05-05 19:41:04102 bool CanSetCookie(const URLRequest& request,
Victor Costan70f85512017-11-20 16:14:46103 const net::CanonicalCookie& cookie,
Clark DuValle8737642018-08-31 17:26:34104 CookieOptions* options,
105 bool allowed_from_caller);
[email protected]4c219e22012-05-05 19:41:04106 bool CanAccessFile(const URLRequest& request,
satoruxddac0442017-05-29 06:06:18107 const base::FilePath& original_path,
108 const base::FilePath& absolute_path) const;
Matt Menke7ad78edc2018-11-21 19:22:57109 bool ForcePrivacyMode(const GURL& url, const GURL& site_for_cookies) const;
[email protected]82a37672011-05-03 12:02:41110
jochen0e3b3a62014-09-16 18:31:23111 bool CancelURLRequestWithPolicyViolatingReferrerHeader(
112 const URLRequest& request,
113 const GURL& target_url,
114 const GURL& referrer_url) const;
115
juliatuttlefcf47202017-05-23 15:53:02116 bool CanQueueReportingReport(const url::Origin& origin) const;
Douglas Creager7b07ea42018-02-27 21:08:08117 void CanSendReportingReports(
118 std::set<url::Origin> origins,
119 base::OnceCallback<void(std::set<url::Origin>)> result_callback) const;
juliatuttlefcf47202017-05-23 15:53:02120 bool CanSetReportingClient(const url::Origin& origin,
121 const GURL& endpoint) const;
122 bool CanUseReportingClient(const url::Origin& origin,
123 const GURL& endpoint) const;
124
gab47aa7da2017-06-02 16:09:43125 protected:
126 THREAD_CHECKER(thread_checker_);
127
[email protected]0651b812011-02-24 00:22:50128 private:
[email protected]4c219e22012-05-05 19:41:04129 // This is the interface for subclasses of NetworkDelegate to implement. These
[email protected]0651b812011-02-24 00:22:50130 // member functions will be called by the respective public notification
131 // member function, which will perform basic sanity checking.
mgersh15715172016-11-16 16:03:25132 //
David Benjamind1f287bf2018-06-12 01:57:20133 // Note that these member functions refer to URLRequests which may be canceled
134 // or destroyed at any time. Implementations which return ERR_IO_PENDING must
135 // also implement OnURLRequestDestroyed and OnCompleted to handle cancelation.
136 // See below for details.
137 //
mgersh15715172016-11-16 16:03:25138 // (NetworkDelegateImpl has default implementations of these member functions.
139 // NetworkDelegate implementations should consider subclassing
140 // NetworkDelegateImpl.)
[email protected]0651b812011-02-24 00:22:50141
[email protected]4c76d7c2011-04-15 19:14:12142 // Called before a request is sent. Allows the delegate to rewrite the URL
[email protected]f878230e2014-04-03 15:36:14143 // being fetched by modifying |new_url|. If set, the URL must be valid. The
144 // reference fragment from the original URL is not automatically appended to
145 // |new_url|; callers are responsible for copying the reference fragment if
146 // desired.
David Benjamind1f287bf2018-06-12 01:57:20147 //
148 // Returns OK to continue with the request, ERR_IO_PENDING if the result is
149 // not ready yet, and any other status code to cancel the request. If
150 // returning ERR_IO_PENDING, call |callback| when the result is ready. Note,
151 // however, that a pending operation may be cancelled by
152 // OnURLRequestDestroyed. Once cancelled, |request| and |new_url| become
153 // invalid and |callback| may not be called.
[email protected]c6c6e5652013-10-29 02:40:30154 //
155 // The default implementation returns OK (continue with request).
[email protected]4875ba12011-03-30 22:31:51156 virtual int OnBeforeURLRequest(URLRequest* request,
David Benjamind1f287bf2018-06-12 01:57:20157 CompletionOnceCallback callback,
megjablonc1751452014-12-09 19:46:47158 GURL* new_url) = 0;
[email protected]0651b812011-02-24 00:22:50159
ryansturm2343cb62016-06-15 01:09:00160 // Called right before the network transaction starts. Allows the delegate to
David Benjamind1f287bf2018-06-12 01:57:20161 // read/write |headers| before they get sent out.
162 //
163 // Returns OK to continue with the request, ERR_IO_PENDING if the result is
164 // not ready yet, and any other status code to cancel the request. If
165 // returning ERR_IO_PENDING, call |callback| when the result is ready. Note,
166 // however, that a pending operation may be cancelled by OnURLRequestDestroyed
167 // or OnCompleted. Once cancelled, |request| and |headers| become invalid and
168 // |callback| may not be called.
169 //
170 // The default implementation returns OK (continue with request) without
171 // modifying |headers|.
ryansturm2343cb62016-06-15 01:09:00172 virtual int OnBeforeStartTransaction(URLRequest* request,
David Benjamind1f287bf2018-06-12 01:57:20173 CompletionOnceCallback callback,
ryansturm2343cb62016-06-15 01:09:00174 HttpRequestHeaders* headers) = 0;
[email protected]0651b812011-02-24 00:22:50175
ryansturm49a8cb12016-06-15 16:51:09176 // Called after a connection is established , and just before headers are sent
177 // to the destination server (i.e., not called for HTTP CONNECT requests). For
178 // non-tunneled requests using HTTP proxies, |headers| will include any
179 // proxy-specific headers as well. Allows the delegate to read/write |headers|
David Benjamind1f287bf2018-06-12 01:57:20180 // before they get sent out. |headers| is valid only for the duration of the
181 // call.
ryansturm49a8cb12016-06-15 16:51:09182 virtual void OnBeforeSendHeaders(URLRequest* request,
183 const ProxyInfo& proxy_info,
184 const ProxyRetryInfoMap& proxy_retry_info,
185 HttpRequestHeaders* headers) = 0;
[email protected]597a1ab2014-06-26 08:12:27186
[email protected]5796dc942011-07-14 19:26:10187 // Called right before the HTTP request(s) are being sent to the network.
David Benjamind1f287bf2018-06-12 01:57:20188 // |headers| is only valid only for the duration of the call.
ryansturm2343cb62016-06-15 01:09:00189 virtual void OnStartTransaction(URLRequest* request,
190 const HttpRequestHeaders& headers) = 0;
[email protected]82b42302011-04-20 16:28:16191
[email protected]c6c6e5652013-10-29 02:40:30192 // Called for HTTP requests when the headers have been received.
[email protected]ea8141e2011-10-05 13:12:51193 // |original_response_headers| contains the headers as received over the
194 // network, these must not be modified. |override_response_headers| can be set
195 // to new values, that should be considered as overriding
196 // |original_response_headers|.
[email protected]f878230e2014-04-03 15:36:14197 // If the response is a redirect, and the Location response header value is
198 // identical to |allowed_unsafe_redirect_url|, then the redirect is never
199 // blocked and the reference fragment is not copied from the original URL
200 // to the redirection target.
201 //
David Benjamind1f287bf2018-06-12 01:57:20202 // Returns OK to continue with the request, ERR_IO_PENDING if the result is
203 // not ready yet, and any other status code to cancel the request. If
204 // returning ERR_IO_PENDING, call |callback| when the result is ready. Note,
205 // however, that a pending operation may be cancelled by
206 // OnURLRequestDestroyed. Once cancelled, |request|,
207 // |original_response_headers|, |override_response_headers|, and
208 // |allowed_unsafe_redirect_url| become invalid and |callback| may not be
209 // called.
[email protected]ea8141e2011-10-05 13:12:51210 virtual int OnHeadersReceived(
211 URLRequest* request,
David Benjamind1f287bf2018-06-12 01:57:20212 CompletionOnceCallback callback,
[email protected]507af8f2012-10-20 00:42:32213 const HttpResponseHeaders* original_response_headers,
[email protected]5f714132014-03-26 10:41:16214 scoped_refptr<HttpResponseHeaders>* override_response_headers,
megjablonc1751452014-12-09 19:46:47215 GURL* allowed_unsafe_redirect_url) = 0;
[email protected]ea8141e2011-10-05 13:12:51216
David Benjamind1f287bf2018-06-12 01:57:20217 // Called right after a redirect response code was received. |new_location| is
218 // only valid for the duration of the call.
[email protected]31b2e5f2011-04-20 16:58:32219 virtual void OnBeforeRedirect(URLRequest* request,
megjablonc1751452014-12-09 19:46:47220 const GURL& new_location) = 0;
[email protected]31b2e5f2011-04-20 16:58:32221
[email protected]0651b812011-02-24 00:22:50222 // This corresponds to URLRequestDelegate::OnResponseStarted.
David Benjamin9df92afb2018-06-12 23:25:24223 virtual void OnResponseStarted(URLRequest* request, int net_error) = 0;
[email protected]0651b812011-02-24 00:22:50224
sclittlece72c482015-08-24 20:20:59225 // Called when bytes are received from the network, such as after receiving
226 // headers or reading raw response bytes. This includes localhost requests.
227 // |bytes_received| is the number of bytes measured at the application layer
228 // that have been received over the network for this request since the last
229 // time OnNetworkBytesReceived was called. |bytes_received| will always be
230 // greater than 0.
231 // Currently, this is only implemented for HTTP transactions, and
232 // |bytes_received| does not include TLS overhead or TCP retransmits.
sclittlea133de02015-11-10 23:54:21233 virtual void OnNetworkBytesReceived(URLRequest* request,
sclittlece72c482015-08-24 20:20:59234 int64_t bytes_received) = 0;
[email protected]8523ba52011-05-22 19:00:58235
sclittle28d558b2015-09-28 21:40:52236 // Called when bytes are sent over the network, such as when sending request
237 // headers or uploading request body bytes. This includes localhost requests.
238 // |bytes_sent| is the number of bytes measured at the application layer that
239 // have been sent over the network for this request since the last time
240 // OnNetworkBytesSent was called. |bytes_sent| will always be greater than 0.
241 // Currently, this is only implemented for HTTP transactions, and |bytes_sent|
242 // does not include TLS overhead or TCP retransmits.
sclittlea133de02015-11-10 23:54:21243 virtual void OnNetworkBytesSent(URLRequest* request, int64_t bytes_sent) = 0;
sclittle28d558b2015-09-28 21:40:52244
[email protected]48944382011-04-23 13:28:16245 // Indicates that the URL request has been completed or failed.
[email protected]9045b8822012-01-13 20:35:35246 // |started| indicates whether the request has been started. If false,
247 // some information like the socket address is not available.
David Benjamin9df92afb2018-06-12 23:25:24248 virtual void OnCompleted(URLRequest* request,
249 bool started,
250 int net_error) = 0;
[email protected]4b50cb52011-03-10 00:29:37251
[email protected]4875ba12011-03-30 22:31:51252 // Called when an URLRequest is being destroyed. Note that the request is
253 // being deleted, so it's not safe to call any methods that may result in
254 // a virtual method call.
megjablonc1751452014-12-09 19:46:47255 virtual void OnURLRequestDestroyed(URLRequest* request) = 0;
[email protected]4875ba12011-03-30 22:31:51256
[email protected]82a37672011-05-03 12:02:41257 // Corresponds to ProxyResolverJSBindings::OnError.
[email protected]42cba2fb2013-03-29 19:58:57258 virtual void OnPACScriptError(int line_number,
megjablonc1751452014-12-09 19:46:47259 const base::string16& error) = 0;
[email protected]7efc582d2011-08-03 20:46:35260
[email protected]c2911d72011-10-03 22:16:36261 // Called when a request receives an authentication challenge
262 // specified by |auth_info|, and is unable to respond using cached
David Benjamind1f287bf2018-06-12 01:57:20263 // credentials. |callback| and |credentials| must be non-NULL.
[email protected]c2911d72011-10-03 22:16:36264 //
265 // The following return values are allowed:
266 // - AUTH_REQUIRED_RESPONSE_NO_ACTION: |auth_info| is observed, but
267 // no action is being taken on it.
268 // - AUTH_REQUIRED_RESPONSE_SET_AUTH: |credentials| is filled in with
269 // a username and password, which should be used in a response to
270 // |auth_info|.
271 // - AUTH_REQUIRED_RESPONSE_CANCEL_AUTH: The authentication challenge
272 // should not be attempted.
273 // - AUTH_REQUIRED_RESPONSE_IO_PENDING: The action will be decided
274 // asynchronously. |callback| will be invoked when the decision is made,
275 // and one of the other AuthRequiredResponse values will be passed in with
David Benjamind1f287bf2018-06-12 01:57:20276 // the same semantics as described above. Note, however, that a pending
277 // operation may be cancelled by OnURLRequestDestroyed. Once cancelled,
278 // |request|, |auth_info|, and |credentials| become invalid and |callback|
279 // may not be called.
[email protected]c2911d72011-10-03 22:16:36280 virtual AuthRequiredResponse OnAuthRequired(
281 URLRequest* request,
282 const AuthChallengeInfo& auth_info,
David Benjamind1f287bf2018-06-12 01:57:20283 AuthCallback callback,
megjablonc1751452014-12-09 19:46:47284 AuthCredentials* credentials) = 0;
[email protected]9c8ae8c2012-03-09 13:13:35285
286 // Called when reading cookies to allow the network delegate to block access
287 // to the cookie. This method will never be invoked when
288 // LOAD_DO_NOT_SEND_COOKIES is specified.
Clark DuValle8737642018-08-31 17:26:34289 // The |allowed_from_caller| param is used to pass whether this operation is
290 // allowed from any higher level delegates (for example, in a
291 // LayeredNetworkDelegate). Any custom logic should be ANDed with this bool.
[email protected]4c219e22012-05-05 19:41:04292 virtual bool OnCanGetCookies(const URLRequest& request,
Clark DuValle8737642018-08-31 17:26:34293 const CookieList& cookie_list,
294 bool allowed_from_caller) = 0;
[email protected]9c8ae8c2012-03-09 13:13:35295
296 // Called when a cookie is set to allow the network delegate to block access
297 // to the cookie. This method will never be invoked when
298 // LOAD_DO_NOT_SAVE_COOKIES is specified.
Clark DuValle8737642018-08-31 17:26:34299 // The |allowed_from_caller| param is used to pass whether this operation is
300 // allowed from any higher level delegates (for example, in a
301 // LayeredNetworkDelegate). Any custom logic should be ANDed with this bool.
[email protected]4c219e22012-05-05 19:41:04302 virtual bool OnCanSetCookie(const URLRequest& request,
Clark DuValle8737642018-08-31 17:26:34303 const CanonicalCookie& cookie,
304 CookieOptions* options,
305 bool allowed_from_caller) = 0;
[email protected]4c219e22012-05-05 19:41:04306
[email protected]4c219e22012-05-05 19:41:04307 // Called when a file access is attempted to allow the network delegate to
satoruxddac0442017-05-29 06:06:18308 // allow or block access to the given file path, provided in the original
309 // and absolute forms (i.e. symbolic link is resolved). It's up to
310 // subclasses of NetworkDelegate to decide which path to use for
311 // checking. Returns true if access is allowed.
[email protected]4c219e22012-05-05 19:41:04312 virtual bool OnCanAccessFile(const URLRequest& request,
satoruxddac0442017-05-29 06:06:18313 const base::FilePath& original_path,
314 const base::FilePath& absolute_path) const = 0;
[email protected]9c8ae8c2012-03-09 13:13:35315
[email protected]e6d017652013-05-17 18:01:40316 // Returns true if the given |url| has to be requested over connection that
317 // is not tracked by the server. Usually is false, unless user privacy
318 // settings block cookies from being get or set.
Matt Menke7ad78edc2018-11-21 19:22:57319 virtual bool OnForcePrivacyMode(const GURL& url,
320 const GURL& site_for_cookies) const = 0;
[email protected]e6d017652013-05-17 18:01:40321
jochen0e3b3a62014-09-16 18:31:23322 // Called when the |referrer_url| for requesting |target_url| during handling
323 // of the |request| is does not comply with the referrer policy (e.g. a
324 // secure referrer for an insecure initial target).
325 // Returns true if the request should be cancelled. Otherwise, the referrer
326 // header is stripped from the request.
327 virtual bool OnCancelURLRequestWithPolicyViolatingReferrerHeader(
328 const URLRequest& request,
329 const GURL& target_url,
megjablonc1751452014-12-09 19:46:47330 const GURL& referrer_url) const = 0;
juliatuttlefcf47202017-05-23 15:53:02331
332 virtual bool OnCanQueueReportingReport(const url::Origin& origin) const = 0;
333
Douglas Creager7b07ea42018-02-27 21:08:08334 virtual void OnCanSendReportingReports(
335 std::set<url::Origin> origins,
336 base::OnceCallback<void(std::set<url::Origin>)> result_callback)
337 const = 0;
juliatuttlefcf47202017-05-23 15:53:02338
339 virtual bool OnCanSetReportingClient(const url::Origin& origin,
340 const GURL& endpoint) const = 0;
341
342 virtual bool OnCanUseReportingClient(const url::Origin& origin,
343 const GURL& endpoint) const = 0;
[email protected]0651b812011-02-24 00:22:50344};
345
346} // namespace net
347
348#endif // NET_BASE_NETWORK_DELEGATE_H_