blob: c28d0bb3b67663154c13aa482b215d7ba9b40895 [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;
[email protected]e6d017652013-05-17 18:01:40109 bool CanEnablePrivacyMode(const GURL& url,
Mike Westb85da8ed2017-08-10 14:16:46110 const GURL& site_for_cookies) const;
[email protected]82a37672011-05-03 12:02:41111
estark7625d812015-10-12 20:10:41112 bool AreExperimentalCookieFeaturesEnabled() const;
mkwstc5fa7762016-03-28 09:28:23113
jochen0e3b3a62014-09-16 18:31:23114 bool CancelURLRequestWithPolicyViolatingReferrerHeader(
115 const URLRequest& request,
116 const GURL& target_url,
117 const GURL& referrer_url) const;
118
juliatuttlefcf47202017-05-23 15:53:02119 bool CanQueueReportingReport(const url::Origin& origin) const;
Douglas Creager7b07ea42018-02-27 21:08:08120 void CanSendReportingReports(
121 std::set<url::Origin> origins,
122 base::OnceCallback<void(std::set<url::Origin>)> result_callback) const;
juliatuttlefcf47202017-05-23 15:53:02123 bool CanSetReportingClient(const url::Origin& origin,
124 const GURL& endpoint) const;
125 bool CanUseReportingClient(const url::Origin& origin,
126 const GURL& endpoint) const;
127
gab47aa7da2017-06-02 16:09:43128 protected:
129 THREAD_CHECKER(thread_checker_);
130
[email protected]0651b812011-02-24 00:22:50131 private:
[email protected]4c219e22012-05-05 19:41:04132 // This is the interface for subclasses of NetworkDelegate to implement. These
[email protected]0651b812011-02-24 00:22:50133 // member functions will be called by the respective public notification
134 // member function, which will perform basic sanity checking.
mgersh15715172016-11-16 16:03:25135 //
David Benjamind1f287bf2018-06-12 01:57:20136 // Note that these member functions refer to URLRequests which may be canceled
137 // or destroyed at any time. Implementations which return ERR_IO_PENDING must
138 // also implement OnURLRequestDestroyed and OnCompleted to handle cancelation.
139 // See below for details.
140 //
mgersh15715172016-11-16 16:03:25141 // (NetworkDelegateImpl has default implementations of these member functions.
142 // NetworkDelegate implementations should consider subclassing
143 // NetworkDelegateImpl.)
[email protected]0651b812011-02-24 00:22:50144
[email protected]4c76d7c2011-04-15 19:14:12145 // Called before a request is sent. Allows the delegate to rewrite the URL
[email protected]f878230e2014-04-03 15:36:14146 // being fetched by modifying |new_url|. If set, the URL must be valid. The
147 // reference fragment from the original URL is not automatically appended to
148 // |new_url|; callers are responsible for copying the reference fragment if
149 // desired.
David Benjamind1f287bf2018-06-12 01:57:20150 //
151 // Returns OK to continue with the request, ERR_IO_PENDING if the result is
152 // not ready yet, and any other status code to cancel the request. If
153 // returning ERR_IO_PENDING, call |callback| when the result is ready. Note,
154 // however, that a pending operation may be cancelled by
155 // OnURLRequestDestroyed. Once cancelled, |request| and |new_url| become
156 // invalid and |callback| may not be called.
[email protected]c6c6e5652013-10-29 02:40:30157 //
158 // The default implementation returns OK (continue with request).
[email protected]4875ba12011-03-30 22:31:51159 virtual int OnBeforeURLRequest(URLRequest* request,
David Benjamind1f287bf2018-06-12 01:57:20160 CompletionOnceCallback callback,
megjablonc1751452014-12-09 19:46:47161 GURL* new_url) = 0;
[email protected]0651b812011-02-24 00:22:50162
ryansturm2343cb62016-06-15 01:09:00163 // Called right before the network transaction starts. Allows the delegate to
David Benjamind1f287bf2018-06-12 01:57:20164 // read/write |headers| before they get sent out.
165 //
166 // Returns OK to continue with the request, ERR_IO_PENDING if the result is
167 // not ready yet, and any other status code to cancel the request. If
168 // returning ERR_IO_PENDING, call |callback| when the result is ready. Note,
169 // however, that a pending operation may be cancelled by OnURLRequestDestroyed
170 // or OnCompleted. Once cancelled, |request| and |headers| become invalid and
171 // |callback| may not be called.
172 //
173 // The default implementation returns OK (continue with request) without
174 // modifying |headers|.
ryansturm2343cb62016-06-15 01:09:00175 virtual int OnBeforeStartTransaction(URLRequest* request,
David Benjamind1f287bf2018-06-12 01:57:20176 CompletionOnceCallback callback,
ryansturm2343cb62016-06-15 01:09:00177 HttpRequestHeaders* headers) = 0;
[email protected]0651b812011-02-24 00:22:50178
ryansturm49a8cb12016-06-15 16:51:09179 // Called after a connection is established , and just before headers are sent
180 // to the destination server (i.e., not called for HTTP CONNECT requests). For
181 // non-tunneled requests using HTTP proxies, |headers| will include any
182 // proxy-specific headers as well. Allows the delegate to read/write |headers|
David Benjamind1f287bf2018-06-12 01:57:20183 // before they get sent out. |headers| is valid only for the duration of the
184 // call.
ryansturm49a8cb12016-06-15 16:51:09185 virtual void OnBeforeSendHeaders(URLRequest* request,
186 const ProxyInfo& proxy_info,
187 const ProxyRetryInfoMap& proxy_retry_info,
188 HttpRequestHeaders* headers) = 0;
[email protected]597a1ab2014-06-26 08:12:27189
[email protected]5796dc942011-07-14 19:26:10190 // Called right before the HTTP request(s) are being sent to the network.
David Benjamind1f287bf2018-06-12 01:57:20191 // |headers| is only valid only for the duration of the call.
ryansturm2343cb62016-06-15 01:09:00192 virtual void OnStartTransaction(URLRequest* request,
193 const HttpRequestHeaders& headers) = 0;
[email protected]82b42302011-04-20 16:28:16194
[email protected]c6c6e5652013-10-29 02:40:30195 // Called for HTTP requests when the headers have been received.
[email protected]ea8141e2011-10-05 13:12:51196 // |original_response_headers| contains the headers as received over the
197 // network, these must not be modified. |override_response_headers| can be set
198 // to new values, that should be considered as overriding
199 // |original_response_headers|.
[email protected]f878230e2014-04-03 15:36:14200 // If the response is a redirect, and the Location response header value is
201 // identical to |allowed_unsafe_redirect_url|, then the redirect is never
202 // blocked and the reference fragment is not copied from the original URL
203 // to the redirection target.
204 //
David Benjamind1f287bf2018-06-12 01:57:20205 // Returns OK to continue with the request, ERR_IO_PENDING if the result is
206 // not ready yet, and any other status code to cancel the request. If
207 // returning ERR_IO_PENDING, call |callback| when the result is ready. Note,
208 // however, that a pending operation may be cancelled by
209 // OnURLRequestDestroyed. Once cancelled, |request|,
210 // |original_response_headers|, |override_response_headers|, and
211 // |allowed_unsafe_redirect_url| become invalid and |callback| may not be
212 // called.
[email protected]ea8141e2011-10-05 13:12:51213 virtual int OnHeadersReceived(
214 URLRequest* request,
David Benjamind1f287bf2018-06-12 01:57:20215 CompletionOnceCallback callback,
[email protected]507af8f2012-10-20 00:42:32216 const HttpResponseHeaders* original_response_headers,
[email protected]5f714132014-03-26 10:41:16217 scoped_refptr<HttpResponseHeaders>* override_response_headers,
megjablonc1751452014-12-09 19:46:47218 GURL* allowed_unsafe_redirect_url) = 0;
[email protected]ea8141e2011-10-05 13:12:51219
David Benjamind1f287bf2018-06-12 01:57:20220 // Called right after a redirect response code was received. |new_location| is
221 // only valid for the duration of the call.
[email protected]31b2e5f2011-04-20 16:58:32222 virtual void OnBeforeRedirect(URLRequest* request,
megjablonc1751452014-12-09 19:46:47223 const GURL& new_location) = 0;
[email protected]31b2e5f2011-04-20 16:58:32224
[email protected]0651b812011-02-24 00:22:50225 // This corresponds to URLRequestDelegate::OnResponseStarted.
David Benjamin9df92afb2018-06-12 23:25:24226 virtual void OnResponseStarted(URLRequest* request, int net_error) = 0;
[email protected]0651b812011-02-24 00:22:50227
sclittlece72c482015-08-24 20:20:59228 // Called when bytes are received from the network, such as after receiving
229 // headers or reading raw response bytes. This includes localhost requests.
230 // |bytes_received| is the number of bytes measured at the application layer
231 // that have been received over the network for this request since the last
232 // time OnNetworkBytesReceived was called. |bytes_received| will always be
233 // greater than 0.
234 // Currently, this is only implemented for HTTP transactions, and
235 // |bytes_received| does not include TLS overhead or TCP retransmits.
sclittlea133de02015-11-10 23:54:21236 virtual void OnNetworkBytesReceived(URLRequest* request,
sclittlece72c482015-08-24 20:20:59237 int64_t bytes_received) = 0;
[email protected]8523ba52011-05-22 19:00:58238
sclittle28d558b2015-09-28 21:40:52239 // Called when bytes are sent over the network, such as when sending request
240 // headers or uploading request body bytes. This includes localhost requests.
241 // |bytes_sent| is the number of bytes measured at the application layer that
242 // have been sent over the network for this request since the last time
243 // OnNetworkBytesSent was called. |bytes_sent| will always be greater than 0.
244 // Currently, this is only implemented for HTTP transactions, and |bytes_sent|
245 // does not include TLS overhead or TCP retransmits.
sclittlea133de02015-11-10 23:54:21246 virtual void OnNetworkBytesSent(URLRequest* request, int64_t bytes_sent) = 0;
sclittle28d558b2015-09-28 21:40:52247
[email protected]48944382011-04-23 13:28:16248 // Indicates that the URL request has been completed or failed.
[email protected]9045b8822012-01-13 20:35:35249 // |started| indicates whether the request has been started. If false,
250 // some information like the socket address is not available.
David Benjamin9df92afb2018-06-12 23:25:24251 virtual void OnCompleted(URLRequest* request,
252 bool started,
253 int net_error) = 0;
[email protected]4b50cb52011-03-10 00:29:37254
[email protected]4875ba12011-03-30 22:31:51255 // Called when an URLRequest is being destroyed. Note that the request is
256 // being deleted, so it's not safe to call any methods that may result in
257 // a virtual method call.
megjablonc1751452014-12-09 19:46:47258 virtual void OnURLRequestDestroyed(URLRequest* request) = 0;
[email protected]4875ba12011-03-30 22:31:51259
[email protected]82a37672011-05-03 12:02:41260 // Corresponds to ProxyResolverJSBindings::OnError.
[email protected]42cba2fb2013-03-29 19:58:57261 virtual void OnPACScriptError(int line_number,
megjablonc1751452014-12-09 19:46:47262 const base::string16& error) = 0;
[email protected]7efc582d2011-08-03 20:46:35263
[email protected]c2911d72011-10-03 22:16:36264 // Called when a request receives an authentication challenge
265 // specified by |auth_info|, and is unable to respond using cached
David Benjamind1f287bf2018-06-12 01:57:20266 // credentials. |callback| and |credentials| must be non-NULL.
[email protected]c2911d72011-10-03 22:16:36267 //
268 // The following return values are allowed:
269 // - AUTH_REQUIRED_RESPONSE_NO_ACTION: |auth_info| is observed, but
270 // no action is being taken on it.
271 // - AUTH_REQUIRED_RESPONSE_SET_AUTH: |credentials| is filled in with
272 // a username and password, which should be used in a response to
273 // |auth_info|.
274 // - AUTH_REQUIRED_RESPONSE_CANCEL_AUTH: The authentication challenge
275 // should not be attempted.
276 // - AUTH_REQUIRED_RESPONSE_IO_PENDING: The action will be decided
277 // asynchronously. |callback| will be invoked when the decision is made,
278 // and one of the other AuthRequiredResponse values will be passed in with
David Benjamind1f287bf2018-06-12 01:57:20279 // the same semantics as described above. Note, however, that a pending
280 // operation may be cancelled by OnURLRequestDestroyed. Once cancelled,
281 // |request|, |auth_info|, and |credentials| become invalid and |callback|
282 // may not be called.
[email protected]c2911d72011-10-03 22:16:36283 virtual AuthRequiredResponse OnAuthRequired(
284 URLRequest* request,
285 const AuthChallengeInfo& auth_info,
David Benjamind1f287bf2018-06-12 01:57:20286 AuthCallback callback,
megjablonc1751452014-12-09 19:46:47287 AuthCredentials* credentials) = 0;
[email protected]9c8ae8c2012-03-09 13:13:35288
289 // Called when reading cookies to allow the network delegate to block access
290 // to the cookie. This method will never be invoked when
291 // LOAD_DO_NOT_SEND_COOKIES is specified.
Clark DuValle8737642018-08-31 17:26:34292 // The |allowed_from_caller| param is used to pass whether this operation is
293 // allowed from any higher level delegates (for example, in a
294 // LayeredNetworkDelegate). Any custom logic should be ANDed with this bool.
[email protected]4c219e22012-05-05 19:41:04295 virtual bool OnCanGetCookies(const URLRequest& request,
Clark DuValle8737642018-08-31 17:26:34296 const CookieList& cookie_list,
297 bool allowed_from_caller) = 0;
[email protected]9c8ae8c2012-03-09 13:13:35298
299 // Called when a cookie is set to allow the network delegate to block access
300 // to the cookie. This method will never be invoked when
301 // LOAD_DO_NOT_SAVE_COOKIES is specified.
Clark DuValle8737642018-08-31 17:26:34302 // The |allowed_from_caller| param is used to pass whether this operation is
303 // allowed from any higher level delegates (for example, in a
304 // LayeredNetworkDelegate). Any custom logic should be ANDed with this bool.
[email protected]4c219e22012-05-05 19:41:04305 virtual bool OnCanSetCookie(const URLRequest& request,
Clark DuValle8737642018-08-31 17:26:34306 const CanonicalCookie& cookie,
307 CookieOptions* options,
308 bool allowed_from_caller) = 0;
[email protected]4c219e22012-05-05 19:41:04309
[email protected]4c219e22012-05-05 19:41:04310 // Called when a file access is attempted to allow the network delegate to
satoruxddac0442017-05-29 06:06:18311 // allow or block access to the given file path, provided in the original
312 // and absolute forms (i.e. symbolic link is resolved). It's up to
313 // subclasses of NetworkDelegate to decide which path to use for
314 // checking. Returns true if access is allowed.
[email protected]4c219e22012-05-05 19:41:04315 virtual bool OnCanAccessFile(const URLRequest& request,
satoruxddac0442017-05-29 06:06:18316 const base::FilePath& original_path,
317 const base::FilePath& absolute_path) const = 0;
[email protected]9c8ae8c2012-03-09 13:13:35318
[email protected]e6d017652013-05-17 18:01:40319 // Returns true if the given |url| has to be requested over connection that
320 // is not tracked by the server. Usually is false, unless user privacy
321 // settings block cookies from being get or set.
Mike Westb85da8ed2017-08-10 14:16:46322 virtual bool OnCanEnablePrivacyMode(const GURL& url,
323 const GURL& site_for_cookies) const = 0;
[email protected]e6d017652013-05-17 18:01:40324
jww601411a2015-11-20 19:46:57325 // Returns true if the embedder has enabled the experimental features, and
326 // false otherwise.
estark7625d812015-10-12 20:10:41327 virtual bool OnAreExperimentalCookieFeaturesEnabled() const = 0;
mkwst0513c9d2015-04-01 05:53:15328
jochen0e3b3a62014-09-16 18:31:23329 // Called when the |referrer_url| for requesting |target_url| during handling
330 // of the |request| is does not comply with the referrer policy (e.g. a
331 // secure referrer for an insecure initial target).
332 // Returns true if the request should be cancelled. Otherwise, the referrer
333 // header is stripped from the request.
334 virtual bool OnCancelURLRequestWithPolicyViolatingReferrerHeader(
335 const URLRequest& request,
336 const GURL& target_url,
megjablonc1751452014-12-09 19:46:47337 const GURL& referrer_url) const = 0;
juliatuttlefcf47202017-05-23 15:53:02338
339 virtual bool OnCanQueueReportingReport(const url::Origin& origin) const = 0;
340
Douglas Creager7b07ea42018-02-27 21:08:08341 virtual void OnCanSendReportingReports(
342 std::set<url::Origin> origins,
343 base::OnceCallback<void(std::set<url::Origin>)> result_callback)
344 const = 0;
juliatuttlefcf47202017-05-23 15:53:02345
346 virtual bool OnCanSetReportingClient(const url::Origin& origin,
347 const GURL& endpoint) const = 0;
348
349 virtual bool OnCanUseReportingClient(const url::Origin& origin,
350 const GURL& endpoint) const = 0;
[email protected]0651b812011-02-24 00:22:50351};
352
353} // namespace net
354
355#endif // NET_BASE_NETWORK_DELEGATE_H_