blob: 5742cb7e08696742c22e9ea91ff51f82f1c7844c [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"
gab47aa7da2017-06-02 16:09:4314#include "base/threading/thread_checker.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"
bnc81c46c1f2016-10-04 16:25:5917#include "net/base/net_export.h"
[email protected]8da4b1812012-07-25 13:54:3818#include "net/cookies/canonical_cookie.h"
Lily Houghton582d4622018-01-22 22:43:4019#include "net/proxy_resolution/proxy_retry_info.h"
[email protected]0651b812011-02-24 00:22:5020
[email protected]4c76d7c2011-04-15 19:14:1221class GURL;
22
[email protected]a3ef4832013-02-02 05:12:3323namespace base {
24class FilePath;
25}
26
juliatuttlefcf47202017-05-23 15:53:0227namespace url {
28class Origin;
29}
30
[email protected]0651b812011-02-24 00:22:5031namespace net {
32
33// NOTE: Layering violations!
34// We decided to accept these violations (depending
35// on other net/ submodules from net/base/), because otherwise NetworkDelegate
36// would have to be broken up into too many smaller interfaces targeted to each
37// submodule. Also, since the lower levels in net/ may callback into higher
38// levels, we may encounter dangerous casting issues.
39//
40// NOTE: It is not okay to add any compile-time dependencies on symbols outside
41// of net/base here, because we have a net_base library. Forward declarations
42// are ok.
[email protected]9c8ae8c2012-03-09 13:13:3543class CookieOptions;
[email protected]0651b812011-02-24 00:22:5044class HttpRequestHeaders;
[email protected]ea8141e2011-10-05 13:12:5145class HttpResponseHeaders;
[email protected]597a1ab2014-06-26 08:12:2746class ProxyInfo;
[email protected]0651b812011-02-24 00:22:5047class URLRequest;
48
gab47aa7da2017-06-02 16:09:4349class NET_EXPORT NetworkDelegate {
[email protected]0651b812011-02-24 00:22:5050 public:
[email protected]c2911d72011-10-03 22:16:3651 // AuthRequiredResponse indicates how a NetworkDelegate handles an
52 // OnAuthRequired call. It's placed in this file to prevent url_request.h
53 // from having to include network_delegate.h.
54 enum AuthRequiredResponse {
55 AUTH_REQUIRED_RESPONSE_NO_ACTION,
56 AUTH_REQUIRED_RESPONSE_SET_AUTH,
57 AUTH_REQUIRED_RESPONSE_CANCEL_AUTH,
58 AUTH_REQUIRED_RESPONSE_IO_PENDING,
59 };
60 typedef base::Callback<void(AuthRequiredResponse)> AuthCallback;
61
gab47aa7da2017-06-02 16:09:4362 virtual ~NetworkDelegate();
[email protected]0651b812011-02-24 00:22:5063
64 // Notification interface called by the network stack. Note that these
65 // functions mostly forward to the private virtuals. They also add some sanity
[email protected]4875ba12011-03-30 22:31:5166 // checking on parameters. See the corresponding virtuals for explanations of
67 // the methods and their arguments.
68 int NotifyBeforeURLRequest(URLRequest* request,
[email protected]084262c2011-12-01 21:12:4769 const CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:1270 GURL* new_url);
ryansturm2343cb62016-06-15 01:09:0071 int NotifyBeforeStartTransaction(URLRequest* request,
72 const CompletionCallback& callback,
73 HttpRequestHeaders* headers);
ryansturm49a8cb12016-06-15 16:51:0974 void NotifyBeforeSendHeaders(URLRequest* request,
75 const ProxyInfo& proxy_info,
76 const ProxyRetryInfoMap& proxy_retry_info,
77 HttpRequestHeaders* headers);
ryansturm2343cb62016-06-15 01:09:0078 void NotifyStartTransaction(URLRequest* request,
79 const HttpRequestHeaders& headers);
[email protected]ea8141e2011-10-05 13:12:5180 int NotifyHeadersReceived(
81 URLRequest* request,
[email protected]084262c2011-12-01 21:12:4782 const CompletionCallback& callback,
[email protected]507af8f2012-10-20 00:42:3283 const HttpResponseHeaders* original_response_headers,
[email protected]5f714132014-03-26 10:41:1684 scoped_refptr<HttpResponseHeaders>* override_response_headers,
85 GURL* allowed_unsafe_redirect_url);
[email protected]31b2e5f2011-04-20 16:58:3286 void NotifyBeforeRedirect(URLRequest* request,
87 const GURL& new_location);
maksim.sisov0f4aa142016-09-05 05:55:2888 void NotifyResponseStarted(URLRequest* request, int net_error);
89 // Deprecated.
[email protected]0651b812011-02-24 00:22:5090 void NotifyResponseStarted(URLRequest* request);
sclittlea133de02015-11-10 23:54:2191 void NotifyNetworkBytesReceived(URLRequest* request, int64_t bytes_received);
92 void NotifyNetworkBytesSent(URLRequest* request, int64_t bytes_sent);
maksim.sisov0f4aa142016-09-05 05:55:2893 void NotifyCompleted(URLRequest* request, bool started, int net_error);
94 // Deprecated.
[email protected]9045b8822012-01-13 20:35:3595 void NotifyCompleted(URLRequest* request, bool started);
[email protected]4875ba12011-03-30 22:31:5196 void NotifyURLRequestDestroyed(URLRequest* request);
[email protected]42cba2fb2013-03-29 19:58:5797 void NotifyPACScriptError(int line_number, const base::string16& error);
[email protected]c2911d72011-10-03 22:16:3698 AuthRequiredResponse NotifyAuthRequired(URLRequest* request,
99 const AuthChallengeInfo& auth_info,
100 const AuthCallback& callback,
101 AuthCredentials* credentials);
[email protected]4c219e22012-05-05 19:41:04102 bool CanGetCookies(const URLRequest& request,
103 const CookieList& cookie_list);
104 bool CanSetCookie(const URLRequest& request,
Victor Costan70f85512017-11-20 16:14:46105 const net::CanonicalCookie& cookie,
[email protected]4c219e22012-05-05 19:41:04106 CookieOptions* options);
107 bool CanAccessFile(const URLRequest& request,
satoruxddac0442017-05-29 06:06:18108 const base::FilePath& original_path,
109 const base::FilePath& absolute_path) const;
[email protected]e6d017652013-05-17 18:01:40110 bool CanEnablePrivacyMode(const GURL& url,
Mike Westb85da8ed2017-08-10 14:16:46111 const GURL& site_for_cookies) const;
[email protected]82a37672011-05-03 12:02:41112
estark7625d812015-10-12 20:10:41113 bool AreExperimentalCookieFeaturesEnabled() const;
mkwstc5fa7762016-03-28 09:28:23114
jochen0e3b3a62014-09-16 18:31:23115 bool CancelURLRequestWithPolicyViolatingReferrerHeader(
116 const URLRequest& request,
117 const GURL& target_url,
118 const GURL& referrer_url) const;
119
juliatuttlefcf47202017-05-23 15:53:02120 bool CanQueueReportingReport(const url::Origin& origin) const;
121 bool CanSendReportingReport(const url::Origin& origin) const;
122 bool CanSetReportingClient(const url::Origin& origin,
123 const GURL& endpoint) const;
124 bool CanUseReportingClient(const url::Origin& origin,
125 const GURL& endpoint) const;
126
gab47aa7da2017-06-02 16:09:43127 protected:
128 THREAD_CHECKER(thread_checker_);
129
[email protected]0651b812011-02-24 00:22:50130 private:
[email protected]4c219e22012-05-05 19:41:04131 // This is the interface for subclasses of NetworkDelegate to implement. These
[email protected]0651b812011-02-24 00:22:50132 // member functions will be called by the respective public notification
133 // member function, which will perform basic sanity checking.
mgersh15715172016-11-16 16:03:25134 //
135 // (NetworkDelegateImpl has default implementations of these member functions.
136 // NetworkDelegate implementations should consider subclassing
137 // NetworkDelegateImpl.)
[email protected]0651b812011-02-24 00:22:50138
[email protected]4c76d7c2011-04-15 19:14:12139 // Called before a request is sent. Allows the delegate to rewrite the URL
[email protected]f878230e2014-04-03 15:36:14140 // being fetched by modifying |new_url|. If set, the URL must be valid. The
141 // reference fragment from the original URL is not automatically appended to
142 // |new_url|; callers are responsible for copying the reference fragment if
143 // desired.
144 // |callback| and |new_url| are valid only until OnURLRequestDestroyed is
145 // called for this request. Returns a net status code, generally either OK to
146 // continue with the request or ERR_IO_PENDING if the result is not ready yet.
147 // A status code other than OK and ERR_IO_PENDING will cancel the request and
148 // report the status code as the reason.
[email protected]c6c6e5652013-10-29 02:40:30149 //
150 // The default implementation returns OK (continue with request).
[email protected]4875ba12011-03-30 22:31:51151 virtual int OnBeforeURLRequest(URLRequest* request,
[email protected]084262c2011-12-01 21:12:47152 const CompletionCallback& callback,
megjablonc1751452014-12-09 19:46:47153 GURL* new_url) = 0;
[email protected]0651b812011-02-24 00:22:50154
ryansturm2343cb62016-06-15 01:09:00155 // Called right before the network transaction starts. 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.
ryansturm2343cb62016-06-15 01:09:00160 virtual int OnBeforeStartTransaction(URLRequest* request,
161 const CompletionCallback& callback,
162 HttpRequestHeaders* headers) = 0;
[email protected]0651b812011-02-24 00:22:50163
ryansturm49a8cb12016-06-15 16:51:09164 // Called after a connection is established , and just before headers are sent
165 // to the destination server (i.e., not called for HTTP CONNECT requests). For
166 // non-tunneled requests using HTTP proxies, |headers| will include any
167 // proxy-specific headers as well. Allows the delegate to read/write |headers|
168 // before they get sent out. |headers| is valid only until OnCompleted or
169 // OnURLRequestDestroyed is called for this request.
170 virtual void OnBeforeSendHeaders(URLRequest* request,
171 const ProxyInfo& proxy_info,
172 const ProxyRetryInfoMap& proxy_retry_info,
173 HttpRequestHeaders* headers) = 0;
[email protected]597a1ab2014-06-26 08:12:27174
[email protected]5796dc942011-07-14 19:26:10175 // Called right before the HTTP request(s) are being sent to the network.
[email protected]9045b8822012-01-13 20:35:35176 // |headers| is only valid until OnCompleted or OnURLRequestDestroyed is
177 // called for this request.
ryansturm2343cb62016-06-15 01:09:00178 virtual void OnStartTransaction(URLRequest* request,
179 const HttpRequestHeaders& headers) = 0;
[email protected]82b42302011-04-20 16:28:16180
[email protected]c6c6e5652013-10-29 02:40:30181 // Called for HTTP requests when the headers have been received.
[email protected]ea8141e2011-10-05 13:12:51182 // |original_response_headers| contains the headers as received over the
183 // network, these must not be modified. |override_response_headers| can be set
184 // to new values, that should be considered as overriding
185 // |original_response_headers|.
[email protected]f878230e2014-04-03 15:36:14186 // If the response is a redirect, and the Location response header value is
187 // identical to |allowed_unsafe_redirect_url|, then the redirect is never
188 // blocked and the reference fragment is not copied from the original URL
189 // to the redirection target.
190 //
[email protected]9045b8822012-01-13 20:35:35191 // |callback|, |original_response_headers|, and |override_response_headers|
192 // are only valid until OnURLRequestDestroyed is called for this request.
[email protected]c6c6e5652013-10-29 02:40:30193 // See OnBeforeURLRequest for return value description. Returns OK by default.
[email protected]ea8141e2011-10-05 13:12:51194 virtual int OnHeadersReceived(
195 URLRequest* request,
[email protected]084262c2011-12-01 21:12:47196 const CompletionCallback& callback,
[email protected]507af8f2012-10-20 00:42:32197 const HttpResponseHeaders* original_response_headers,
[email protected]5f714132014-03-26 10:41:16198 scoped_refptr<HttpResponseHeaders>* override_response_headers,
megjablonc1751452014-12-09 19:46:47199 GURL* allowed_unsafe_redirect_url) = 0;
[email protected]ea8141e2011-10-05 13:12:51200
[email protected]31b2e5f2011-04-20 16:58:32201 // Called right after a redirect response code was received.
[email protected]9045b8822012-01-13 20:35:35202 // |new_location| is only valid until OnURLRequestDestroyed is called for this
203 // request.
[email protected]31b2e5f2011-04-20 16:58:32204 virtual void OnBeforeRedirect(URLRequest* request,
megjablonc1751452014-12-09 19:46:47205 const GURL& new_location) = 0;
[email protected]31b2e5f2011-04-20 16:58:32206
[email protected]0651b812011-02-24 00:22:50207 // This corresponds to URLRequestDelegate::OnResponseStarted.
maksim.sisov0f4aa142016-09-05 05:55:28208 virtual void OnResponseStarted(URLRequest* request, int net_error);
[email protected]0651b812011-02-24 00:22:50209
sclittlece72c482015-08-24 20:20:59210 // Called when bytes are received from the network, such as after receiving
211 // headers or reading raw response bytes. This includes localhost requests.
212 // |bytes_received| is the number of bytes measured at the application layer
213 // that have been received over the network for this request since the last
214 // time OnNetworkBytesReceived was called. |bytes_received| will always be
215 // greater than 0.
216 // Currently, this is only implemented for HTTP transactions, and
217 // |bytes_received| does not include TLS overhead or TCP retransmits.
sclittlea133de02015-11-10 23:54:21218 virtual void OnNetworkBytesReceived(URLRequest* request,
sclittlece72c482015-08-24 20:20:59219 int64_t bytes_received) = 0;
[email protected]8523ba52011-05-22 19:00:58220
sclittle28d558b2015-09-28 21:40:52221 // Called when bytes are sent over the network, such as when sending request
222 // headers or uploading request body bytes. This includes localhost requests.
223 // |bytes_sent| is the number of bytes measured at the application layer that
224 // have been sent over the network for this request since the last time
225 // OnNetworkBytesSent was called. |bytes_sent| will always be greater than 0.
226 // Currently, this is only implemented for HTTP transactions, and |bytes_sent|
227 // does not include TLS overhead or TCP retransmits.
sclittlea133de02015-11-10 23:54:21228 virtual void OnNetworkBytesSent(URLRequest* request, int64_t bytes_sent) = 0;
sclittle28d558b2015-09-28 21:40:52229
[email protected]48944382011-04-23 13:28:16230 // Indicates that the URL request has been completed or failed.
[email protected]9045b8822012-01-13 20:35:35231 // |started| indicates whether the request has been started. If false,
232 // some information like the socket address is not available.
maksim.sisov0f4aa142016-09-05 05:55:28233 virtual void OnCompleted(URLRequest* request, bool started, int net_error);
234 // Deprecated.
235 virtual void OnCompleted(URLRequest* request, bool started);
[email protected]4b50cb52011-03-10 00:29:37236
[email protected]4875ba12011-03-30 22:31:51237 // Called when an URLRequest is being destroyed. Note that the request is
238 // being deleted, so it's not safe to call any methods that may result in
239 // a virtual method call.
megjablonc1751452014-12-09 19:46:47240 virtual void OnURLRequestDestroyed(URLRequest* request) = 0;
[email protected]4875ba12011-03-30 22:31:51241
[email protected]82a37672011-05-03 12:02:41242 // Corresponds to ProxyResolverJSBindings::OnError.
[email protected]42cba2fb2013-03-29 19:58:57243 virtual void OnPACScriptError(int line_number,
megjablonc1751452014-12-09 19:46:47244 const base::string16& error) = 0;
[email protected]7efc582d2011-08-03 20:46:35245
[email protected]c2911d72011-10-03 22:16:36246 // Called when a request receives an authentication challenge
247 // specified by |auth_info|, and is unable to respond using cached
248 // credentials. |callback| and |credentials| must be non-NULL, and must
249 // be valid until OnURLRequestDestroyed is called for |request|.
250 //
251 // The following return values are allowed:
252 // - AUTH_REQUIRED_RESPONSE_NO_ACTION: |auth_info| is observed, but
253 // no action is being taken on it.
254 // - AUTH_REQUIRED_RESPONSE_SET_AUTH: |credentials| is filled in with
255 // a username and password, which should be used in a response to
256 // |auth_info|.
257 // - AUTH_REQUIRED_RESPONSE_CANCEL_AUTH: The authentication challenge
258 // should not be attempted.
259 // - AUTH_REQUIRED_RESPONSE_IO_PENDING: The action will be decided
260 // asynchronously. |callback| will be invoked when the decision is made,
261 // and one of the other AuthRequiredResponse values will be passed in with
262 // the same semantics as described above.
263 virtual AuthRequiredResponse OnAuthRequired(
264 URLRequest* request,
265 const AuthChallengeInfo& auth_info,
266 const AuthCallback& callback,
megjablonc1751452014-12-09 19:46:47267 AuthCredentials* credentials) = 0;
[email protected]9c8ae8c2012-03-09 13:13:35268
269 // Called when reading cookies to allow the network delegate to block access
270 // to the cookie. This method will never be invoked when
271 // LOAD_DO_NOT_SEND_COOKIES is specified.
[email protected]4c219e22012-05-05 19:41:04272 virtual bool OnCanGetCookies(const URLRequest& request,
megjablonc1751452014-12-09 19:46:47273 const CookieList& cookie_list) = 0;
[email protected]9c8ae8c2012-03-09 13:13:35274
275 // Called when a cookie is set to allow the network delegate to block access
276 // to the cookie. This method will never be invoked when
277 // LOAD_DO_NOT_SAVE_COOKIES is specified.
[email protected]4c219e22012-05-05 19:41:04278 virtual bool OnCanSetCookie(const URLRequest& request,
Victor Costan70f85512017-11-20 16:14:46279 const net::CanonicalCookie& cookie,
megjablonc1751452014-12-09 19:46:47280 CookieOptions* options) = 0;
[email protected]4c219e22012-05-05 19:41:04281
[email protected]4c219e22012-05-05 19:41:04282 // Called when a file access is attempted to allow the network delegate to
satoruxddac0442017-05-29 06:06:18283 // allow or block access to the given file path, provided in the original
284 // and absolute forms (i.e. symbolic link is resolved). It's up to
285 // subclasses of NetworkDelegate to decide which path to use for
286 // checking. Returns true if access is allowed.
[email protected]4c219e22012-05-05 19:41:04287 virtual bool OnCanAccessFile(const URLRequest& request,
satoruxddac0442017-05-29 06:06:18288 const base::FilePath& original_path,
289 const base::FilePath& absolute_path) const = 0;
[email protected]9c8ae8c2012-03-09 13:13:35290
[email protected]e6d017652013-05-17 18:01:40291 // Returns true if the given |url| has to be requested over connection that
292 // is not tracked by the server. Usually is false, unless user privacy
293 // settings block cookies from being get or set.
Mike Westb85da8ed2017-08-10 14:16:46294 virtual bool OnCanEnablePrivacyMode(const GURL& url,
295 const GURL& site_for_cookies) const = 0;
[email protected]e6d017652013-05-17 18:01:40296
jww601411a2015-11-20 19:46:57297 // Returns true if the embedder has enabled the experimental features, and
298 // false otherwise.
estark7625d812015-10-12 20:10:41299 virtual bool OnAreExperimentalCookieFeaturesEnabled() const = 0;
mkwst0513c9d2015-04-01 05:53:15300
jochen0e3b3a62014-09-16 18:31:23301 // Called when the |referrer_url| for requesting |target_url| during handling
302 // of the |request| is does not comply with the referrer policy (e.g. a
303 // secure referrer for an insecure initial target).
304 // Returns true if the request should be cancelled. Otherwise, the referrer
305 // header is stripped from the request.
306 virtual bool OnCancelURLRequestWithPolicyViolatingReferrerHeader(
307 const URLRequest& request,
308 const GURL& target_url,
megjablonc1751452014-12-09 19:46:47309 const GURL& referrer_url) const = 0;
juliatuttlefcf47202017-05-23 15:53:02310
311 virtual bool OnCanQueueReportingReport(const url::Origin& origin) const = 0;
312
313 virtual bool OnCanSendReportingReport(const url::Origin& origin) const = 0;
314
315 virtual bool OnCanSetReportingClient(const url::Origin& origin,
316 const GURL& endpoint) const = 0;
317
318 virtual bool OnCanUseReportingClient(const url::Origin& origin,
319 const GURL& endpoint) const = 0;
[email protected]0651b812011-02-24 00:22:50320};
321
322} // namespace net
323
324#endif // NET_BASE_NETWORK_DELEGATE_H_