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_
12 #include "base/callback.h"
13 #include "base/strings/string16.h"
14 #include "base/threading/non_thread_safe.h"
15 #include "net/base/auth.h"
16 #include "net/base/completion_callback.h"
17 #include "net/cookies/canonical_cookie.h"
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.
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
38 class HttpRequestHeaders
;
39 class HttpResponseHeaders
;
45 class NET_EXPORT NetworkDelegate
: public base::NonThreadSafe
{
47 // 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
,
56 typedef base::Callback
<void(AuthRequiredResponse
)> AuthCallback
;
58 virtual ~NetworkDelegate() {}
60 // Notification interface called by the network stack. Note that these
61 // functions mostly forward to the private virtuals. They also add some sanity
62 // checking on parameters. See the corresponding virtuals for explanations of
63 // the methods and their arguments.
64 int NotifyBeforeURLRequest(URLRequest
* request
,
65 const CompletionCallback
& callback
,
67 void NotifyResolveProxy(const GURL
& url
,
69 const ProxyService
& proxy_service
,
71 void NotifyProxyFallback(const ProxyServer
& bad_proxy
,
73 int NotifyBeforeSendHeaders(URLRequest
* request
,
74 const CompletionCallback
& callback
,
75 HttpRequestHeaders
* headers
);
76 void NotifyBeforeSendProxyHeaders(URLRequest
* request
,
77 const ProxyInfo
& proxy_info
,
78 HttpRequestHeaders
* headers
);
79 void NotifySendHeaders(URLRequest
* request
,
80 const HttpRequestHeaders
& headers
);
81 int NotifyHeadersReceived(
83 const CompletionCallback
& callback
,
84 const HttpResponseHeaders
* original_response_headers
,
85 scoped_refptr
<HttpResponseHeaders
>* override_response_headers
,
86 GURL
* allowed_unsafe_redirect_url
);
87 void NotifyBeforeRedirect(URLRequest
* request
,
88 const GURL
& new_location
);
89 void NotifyResponseStarted(URLRequest
* request
);
90 void NotifyNetworkBytesReceived(const URLRequest
& request
,
91 int64_t bytes_received
);
92 void NotifyCompleted(URLRequest
* request
, bool started
);
93 void NotifyURLRequestDestroyed(URLRequest
* request
);
94 void NotifyPACScriptError(int line_number
, const base::string16
& error
);
95 AuthRequiredResponse
NotifyAuthRequired(URLRequest
* request
,
96 const AuthChallengeInfo
& auth_info
,
97 const AuthCallback
& callback
,
98 AuthCredentials
* credentials
);
99 bool CanGetCookies(const URLRequest
& request
,
100 const CookieList
& cookie_list
);
101 bool CanSetCookie(const URLRequest
& request
,
102 const std::string
& cookie_line
,
103 CookieOptions
* options
);
104 bool CanAccessFile(const URLRequest
& request
,
105 const base::FilePath
& path
) const;
106 bool CanEnablePrivacyMode(const GURL
& url
,
107 const GURL
& first_party_for_cookies
) const;
109 // TODO(mkwst): Remove this once we decide whether or not we wish to ship
110 // first-party cookies. https://crbug.com/459154
111 bool FirstPartyOnlyCookieExperimentEnabled() const;
113 bool CancelURLRequestWithPolicyViolatingReferrerHeader(
114 const URLRequest
& request
,
115 const GURL
& target_url
,
116 const GURL
& referrer_url
) const;
119 // This is the interface for subclasses of NetworkDelegate to implement. These
120 // member functions will be called by the respective public notification
121 // member function, which will perform basic sanity checking.
123 // Called before a request is sent. Allows the delegate to rewrite the URL
124 // being fetched by modifying |new_url|. If set, the URL must be valid. The
125 // reference fragment from the original URL is not automatically appended to
126 // |new_url|; callers are responsible for copying the reference fragment if
128 // |callback| and |new_url| are valid only until OnURLRequestDestroyed is
129 // called for this request. Returns a net status code, generally either OK to
130 // continue with the request or ERR_IO_PENDING if the result is not ready yet.
131 // A status code other than OK and ERR_IO_PENDING will cancel the request and
132 // report the status code as the reason.
134 // The default implementation returns OK (continue with request).
135 virtual int OnBeforeURLRequest(URLRequest
* request
,
136 const CompletionCallback
& callback
,
139 // Called as the proxy is being resolved for |url|. Allows the delegate to
140 // override the proxy resolution decision made by ProxyService. The delegate
141 // may override the decision by modifying the ProxyInfo |result|.
142 virtual void OnResolveProxy(const GURL
& url
,
144 const ProxyService
& proxy_service
,
145 ProxyInfo
* result
) = 0;
147 // Called when use of |bad_proxy| fails due to |net_error|. |net_error| is
148 // the network error encountered, if any, and OK if the fallback was
149 // for a reason other than a network error (e.g. the proxy service was
150 // explicitly directed to skip a proxy).
151 virtual void OnProxyFallback(const ProxyServer
& bad_proxy
, int net_error
) = 0;
153 // Called right before the HTTP headers are sent. Allows the delegate to
154 // read/write |headers| before they get sent out. |callback| and |headers| are
155 // valid only until OnCompleted or OnURLRequestDestroyed is called for this
157 // See OnBeforeURLRequest for return value description. Returns OK by default.
158 virtual int OnBeforeSendHeaders(URLRequest
* request
,
159 const CompletionCallback
& callback
,
160 HttpRequestHeaders
* headers
) = 0;
162 // Called after a proxy connection. Allows the delegate to read/write
163 // |headers| before they get sent out. |headers| is valid only until
164 // OnCompleted or OnURLRequestDestroyed is called for this request.
165 virtual void OnBeforeSendProxyHeaders(URLRequest
* request
,
166 const ProxyInfo
& proxy_info
,
167 HttpRequestHeaders
* headers
) = 0;
169 // Called right before the HTTP request(s) are being sent to the network.
170 // |headers| is only valid until OnCompleted or OnURLRequestDestroyed is
171 // called for this request.
172 virtual void OnSendHeaders(URLRequest
* request
,
173 const HttpRequestHeaders
& headers
) = 0;
175 // Called for HTTP requests when the headers have been received.
176 // |original_response_headers| contains the headers as received over the
177 // network, these must not be modified. |override_response_headers| can be set
178 // to new values, that should be considered as overriding
179 // |original_response_headers|.
180 // If the response is a redirect, and the Location response header value is
181 // identical to |allowed_unsafe_redirect_url|, then the redirect is never
182 // blocked and the reference fragment is not copied from the original URL
183 // to the redirection target.
185 // |callback|, |original_response_headers|, and |override_response_headers|
186 // are only valid until OnURLRequestDestroyed is called for this request.
187 // See OnBeforeURLRequest for return value description. Returns OK by default.
188 virtual int OnHeadersReceived(
190 const CompletionCallback
& callback
,
191 const HttpResponseHeaders
* original_response_headers
,
192 scoped_refptr
<HttpResponseHeaders
>* override_response_headers
,
193 GURL
* allowed_unsafe_redirect_url
) = 0;
195 // Called right after a redirect response code was received.
196 // |new_location| is only valid until OnURLRequestDestroyed is called for this
198 virtual void OnBeforeRedirect(URLRequest
* request
,
199 const GURL
& new_location
) = 0;
201 // This corresponds to URLRequestDelegate::OnResponseStarted.
202 virtual void OnResponseStarted(URLRequest
* request
) = 0;
204 // Called when bytes are received from the network, such as after receiving
205 // headers or reading raw response bytes. This includes localhost requests.
206 // |bytes_received| is the number of bytes measured at the application layer
207 // that have been received over the network for this request since the last
208 // time OnNetworkBytesReceived was called. |bytes_received| will always be
210 // Currently, this is only implemented for HTTP transactions, and
211 // |bytes_received| does not include TLS overhead or TCP retransmits.
212 virtual void OnNetworkBytesReceived(const URLRequest
& request
,
213 int64_t bytes_received
) = 0;
215 // Indicates that the URL request has been completed or failed.
216 // |started| indicates whether the request has been started. If false,
217 // some information like the socket address is not available.
218 virtual void OnCompleted(URLRequest
* request
, bool started
) = 0;
220 // Called when an URLRequest is being destroyed. Note that the request is
221 // being deleted, so it's not safe to call any methods that may result in
222 // a virtual method call.
223 virtual void OnURLRequestDestroyed(URLRequest
* request
) = 0;
225 // Corresponds to ProxyResolverJSBindings::OnError.
226 virtual void OnPACScriptError(int line_number
,
227 const base::string16
& error
) = 0;
229 // Called when a request receives an authentication challenge
230 // specified by |auth_info|, and is unable to respond using cached
231 // credentials. |callback| and |credentials| must be non-NULL, and must
232 // be valid until OnURLRequestDestroyed is called for |request|.
234 // The following return values are allowed:
235 // - AUTH_REQUIRED_RESPONSE_NO_ACTION: |auth_info| is observed, but
236 // no action is being taken on it.
237 // - AUTH_REQUIRED_RESPONSE_SET_AUTH: |credentials| is filled in with
238 // a username and password, which should be used in a response to
240 // - AUTH_REQUIRED_RESPONSE_CANCEL_AUTH: The authentication challenge
241 // should not be attempted.
242 // - AUTH_REQUIRED_RESPONSE_IO_PENDING: The action will be decided
243 // asynchronously. |callback| will be invoked when the decision is made,
244 // and one of the other AuthRequiredResponse values will be passed in with
245 // the same semantics as described above.
246 virtual AuthRequiredResponse
OnAuthRequired(
248 const AuthChallengeInfo
& auth_info
,
249 const AuthCallback
& callback
,
250 AuthCredentials
* credentials
) = 0;
252 // Called when reading cookies to allow the network delegate to block access
253 // to the cookie. This method will never be invoked when
254 // LOAD_DO_NOT_SEND_COOKIES is specified.
255 virtual bool OnCanGetCookies(const URLRequest
& request
,
256 const CookieList
& cookie_list
) = 0;
258 // Called when a cookie is set to allow the network delegate to block access
259 // to the cookie. This method will never be invoked when
260 // LOAD_DO_NOT_SAVE_COOKIES is specified.
261 virtual bool OnCanSetCookie(const URLRequest
& request
,
262 const std::string
& cookie_line
,
263 CookieOptions
* options
) = 0;
265 // Called when a file access is attempted to allow the network delegate to
266 // allow or block access to the given file path. Returns true if access is
268 virtual bool OnCanAccessFile(const URLRequest
& request
,
269 const base::FilePath
& path
) const = 0;
271 // Returns true if the given |url| has to be requested over connection that
272 // is not tracked by the server. Usually is false, unless user privacy
273 // settings block cookies from being get or set.
274 virtual bool OnCanEnablePrivacyMode(
276 const GURL
& first_party_for_cookies
) const = 0;
278 // Returns true if the embedder has enabled the "first-party" cookie
279 // experiment, and false otherwise.
281 // TODO(mkwst): Remove this once we decide whether or not we wish to ship
282 // first-party cookies. https://crbug.com/459154
283 virtual bool OnFirstPartyOnlyCookieExperimentEnabled() const = 0;
285 // Called when the |referrer_url| for requesting |target_url| during handling
286 // of the |request| is does not comply with the referrer policy (e.g. a
287 // secure referrer for an insecure initial target).
288 // Returns true if the request should be cancelled. Otherwise, the referrer
289 // header is stripped from the request.
290 virtual bool OnCancelURLRequestWithPolicyViolatingReferrerHeader(
291 const URLRequest
& request
,
292 const GURL
& target_url
,
293 const GURL
& referrer_url
) const = 0;
298 #endif // NET_BASE_NETWORK_DELEGATE_H_