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_
10 #include "base/callback.h"
11 #include "base/strings/string16.h"
12 #include "base/threading/non_thread_safe.h"
13 #include "net/base/auth.h"
14 #include "net/base/completion_callback.h"
15 #include "net/cookies/canonical_cookie.h"
25 // NOTE: Layering violations!
26 // We decided to accept these violations (depending
27 // on other net/ submodules from net/base/), because otherwise NetworkDelegate
28 // would have to be broken up into too many smaller interfaces targeted to each
29 // submodule. Also, since the lower levels in net/ may callback into higher
30 // levels, we may encounter dangerous casting issues.
32 // NOTE: It is not okay to add any compile-time dependencies on symbols outside
33 // of net/base here, because we have a net_base library. Forward declarations
36 class HttpRequestHeaders
;
37 class HttpResponseHeaders
;
43 class NET_EXPORT NetworkDelegate
: public base::NonThreadSafe
{
45 // AuthRequiredResponse indicates how a NetworkDelegate handles an
46 // OnAuthRequired call. It's placed in this file to prevent url_request.h
47 // from having to include network_delegate.h.
48 enum AuthRequiredResponse
{
49 AUTH_REQUIRED_RESPONSE_NO_ACTION
,
50 AUTH_REQUIRED_RESPONSE_SET_AUTH
,
51 AUTH_REQUIRED_RESPONSE_CANCEL_AUTH
,
52 AUTH_REQUIRED_RESPONSE_IO_PENDING
,
54 typedef base::Callback
<void(AuthRequiredResponse
)> AuthCallback
;
56 virtual ~NetworkDelegate() {}
58 // Notification interface called by the network stack. Note that these
59 // functions mostly forward to the private virtuals. They also add some sanity
60 // checking on parameters. See the corresponding virtuals for explanations of
61 // the methods and their arguments.
62 int NotifyBeforeURLRequest(URLRequest
* request
,
63 const CompletionCallback
& callback
,
65 void NotifyResolveProxy(const GURL
& url
,
67 const ProxyService
& proxy_service
,
69 void NotifyProxyFallback(const ProxyServer
& bad_proxy
,
71 int NotifyBeforeSendHeaders(URLRequest
* request
,
72 const CompletionCallback
& callback
,
73 HttpRequestHeaders
* headers
);
74 void NotifyBeforeSendProxyHeaders(URLRequest
* request
,
75 const ProxyInfo
& proxy_info
,
76 HttpRequestHeaders
* headers
);
77 void NotifySendHeaders(URLRequest
* request
,
78 const HttpRequestHeaders
& headers
);
79 int NotifyHeadersReceived(
81 const CompletionCallback
& callback
,
82 const HttpResponseHeaders
* original_response_headers
,
83 scoped_refptr
<HttpResponseHeaders
>* override_response_headers
,
84 GURL
* allowed_unsafe_redirect_url
);
85 void NotifyBeforeRedirect(URLRequest
* request
,
86 const GURL
& new_location
);
87 void NotifyResponseStarted(URLRequest
* request
);
88 void NotifyRawBytesRead(const URLRequest
& request
, int bytes_read
);
89 void NotifyCompleted(URLRequest
* request
, bool started
);
90 void NotifyURLRequestDestroyed(URLRequest
* request
);
91 void NotifyPACScriptError(int line_number
, const base::string16
& error
);
92 AuthRequiredResponse
NotifyAuthRequired(URLRequest
* request
,
93 const AuthChallengeInfo
& auth_info
,
94 const AuthCallback
& callback
,
95 AuthCredentials
* credentials
);
96 bool CanGetCookies(const URLRequest
& request
,
97 const CookieList
& cookie_list
);
98 bool CanSetCookie(const URLRequest
& request
,
99 const std::string
& cookie_line
,
100 CookieOptions
* options
);
101 bool CanAccessFile(const URLRequest
& request
,
102 const base::FilePath
& path
) const;
103 bool CanEnablePrivacyMode(const GURL
& url
,
104 const GURL
& first_party_for_cookies
) const;
106 // TODO(mkwst): Remove this once we decide whether or not we wish to ship
107 // first-party cookies. https://crbug.com/459154
108 bool FirstPartyOnlyCookieExperimentEnabled() const;
110 bool CancelURLRequestWithPolicyViolatingReferrerHeader(
111 const URLRequest
& request
,
112 const GURL
& target_url
,
113 const GURL
& referrer_url
) const;
116 // This is the interface for subclasses of NetworkDelegate to implement. These
117 // member functions will be called by the respective public notification
118 // member function, which will perform basic sanity checking.
120 // Called before a request is sent. Allows the delegate to rewrite the URL
121 // being fetched by modifying |new_url|. If set, the URL must be valid. The
122 // reference fragment from the original URL is not automatically appended to
123 // |new_url|; callers are responsible for copying the reference fragment if
125 // |callback| and |new_url| are valid only until OnURLRequestDestroyed is
126 // called for this request. Returns a net status code, generally either OK to
127 // continue with the request or ERR_IO_PENDING if the result is not ready yet.
128 // A status code other than OK and ERR_IO_PENDING will cancel the request and
129 // report the status code as the reason.
131 // The default implementation returns OK (continue with request).
132 virtual int OnBeforeURLRequest(URLRequest
* request
,
133 const CompletionCallback
& callback
,
136 // Called as the proxy is being resolved for |url|. Allows the delegate to
137 // override the proxy resolution decision made by ProxyService. The delegate
138 // may override the decision by modifying the ProxyInfo |result|.
139 virtual void OnResolveProxy(const GURL
& url
,
141 const ProxyService
& proxy_service
,
142 ProxyInfo
* result
) = 0;
144 // Called when use of |bad_proxy| fails due to |net_error|. |net_error| is
145 // the network error encountered, if any, and OK if the fallback was
146 // for a reason other than a network error (e.g. the proxy service was
147 // explicitly directed to skip a proxy).
148 virtual void OnProxyFallback(const ProxyServer
& bad_proxy
, int net_error
) = 0;
150 // Called right before the HTTP headers are sent. Allows the delegate to
151 // read/write |headers| before they get sent out. |callback| and |headers| are
152 // valid only until OnCompleted or OnURLRequestDestroyed is called for this
154 // See OnBeforeURLRequest for return value description. Returns OK by default.
155 virtual int OnBeforeSendHeaders(URLRequest
* request
,
156 const CompletionCallback
& callback
,
157 HttpRequestHeaders
* headers
) = 0;
159 // Called after a proxy connection. Allows the delegate to read/write
160 // |headers| before they get sent out. |headers| is valid only until
161 // OnCompleted or OnURLRequestDestroyed is called for this request.
162 virtual void OnBeforeSendProxyHeaders(URLRequest
* request
,
163 const ProxyInfo
& proxy_info
,
164 HttpRequestHeaders
* headers
) = 0;
166 // Called right before the HTTP request(s) are being sent to the network.
167 // |headers| is only valid until OnCompleted or OnURLRequestDestroyed is
168 // called for this request.
169 virtual void OnSendHeaders(URLRequest
* request
,
170 const HttpRequestHeaders
& headers
) = 0;
172 // Called for HTTP requests when the headers have been received.
173 // |original_response_headers| contains the headers as received over the
174 // network, these must not be modified. |override_response_headers| can be set
175 // to new values, that should be considered as overriding
176 // |original_response_headers|.
177 // If the response is a redirect, and the Location response header value is
178 // identical to |allowed_unsafe_redirect_url|, then the redirect is never
179 // blocked and the reference fragment is not copied from the original URL
180 // to the redirection target.
182 // |callback|, |original_response_headers|, and |override_response_headers|
183 // are only valid until OnURLRequestDestroyed is called for this request.
184 // See OnBeforeURLRequest for return value description. Returns OK by default.
185 virtual int OnHeadersReceived(
187 const CompletionCallback
& callback
,
188 const HttpResponseHeaders
* original_response_headers
,
189 scoped_refptr
<HttpResponseHeaders
>* override_response_headers
,
190 GURL
* allowed_unsafe_redirect_url
) = 0;
192 // Called right after a redirect response code was received.
193 // |new_location| is only valid until OnURLRequestDestroyed is called for this
195 virtual void OnBeforeRedirect(URLRequest
* request
,
196 const GURL
& new_location
) = 0;
198 // This corresponds to URLRequestDelegate::OnResponseStarted.
199 virtual void OnResponseStarted(URLRequest
* request
) = 0;
201 // Called every time we read raw bytes.
202 virtual void OnRawBytesRead(const URLRequest
& request
, int bytes_read
) = 0;
204 // Indicates that the URL request has been completed or failed.
205 // |started| indicates whether the request has been started. If false,
206 // some information like the socket address is not available.
207 virtual void OnCompleted(URLRequest
* request
, bool started
) = 0;
209 // Called when an URLRequest is being destroyed. Note that the request is
210 // being deleted, so it's not safe to call any methods that may result in
211 // a virtual method call.
212 virtual void OnURLRequestDestroyed(URLRequest
* request
) = 0;
214 // Corresponds to ProxyResolverJSBindings::OnError.
215 virtual void OnPACScriptError(int line_number
,
216 const base::string16
& error
) = 0;
218 // Called when a request receives an authentication challenge
219 // specified by |auth_info|, and is unable to respond using cached
220 // credentials. |callback| and |credentials| must be non-NULL, and must
221 // be valid until OnURLRequestDestroyed is called for |request|.
223 // The following return values are allowed:
224 // - AUTH_REQUIRED_RESPONSE_NO_ACTION: |auth_info| is observed, but
225 // no action is being taken on it.
226 // - AUTH_REQUIRED_RESPONSE_SET_AUTH: |credentials| is filled in with
227 // a username and password, which should be used in a response to
229 // - AUTH_REQUIRED_RESPONSE_CANCEL_AUTH: The authentication challenge
230 // should not be attempted.
231 // - AUTH_REQUIRED_RESPONSE_IO_PENDING: The action will be decided
232 // asynchronously. |callback| will be invoked when the decision is made,
233 // and one of the other AuthRequiredResponse values will be passed in with
234 // the same semantics as described above.
235 virtual AuthRequiredResponse
OnAuthRequired(
237 const AuthChallengeInfo
& auth_info
,
238 const AuthCallback
& callback
,
239 AuthCredentials
* credentials
) = 0;
241 // Called when reading cookies to allow the network delegate to block access
242 // to the cookie. This method will never be invoked when
243 // LOAD_DO_NOT_SEND_COOKIES is specified.
244 virtual bool OnCanGetCookies(const URLRequest
& request
,
245 const CookieList
& cookie_list
) = 0;
247 // Called when a cookie is set to allow the network delegate to block access
248 // to the cookie. This method will never be invoked when
249 // LOAD_DO_NOT_SAVE_COOKIES is specified.
250 virtual bool OnCanSetCookie(const URLRequest
& request
,
251 const std::string
& cookie_line
,
252 CookieOptions
* options
) = 0;
254 // Called when a file access is attempted to allow the network delegate to
255 // allow or block access to the given file path. Returns true if access is
257 virtual bool OnCanAccessFile(const URLRequest
& request
,
258 const base::FilePath
& path
) const = 0;
260 // Returns true if the given |url| has to be requested over connection that
261 // is not tracked by the server. Usually is false, unless user privacy
262 // settings block cookies from being get or set.
263 virtual bool OnCanEnablePrivacyMode(
265 const GURL
& first_party_for_cookies
) const = 0;
267 // Returns true if the embedder has enabled the "first-party" cookie
268 // experiment, and false otherwise.
270 // TODO(mkwst): Remove this once we decide whether or not we wish to ship
271 // first-party cookies. https://crbug.com/459154
272 virtual bool OnFirstPartyOnlyCookieExperimentEnabled() const = 0;
274 // Called when the |referrer_url| for requesting |target_url| during handling
275 // of the |request| is does not comply with the referrer policy (e.g. a
276 // secure referrer for an insecure initial target).
277 // Returns true if the request should be cancelled. Otherwise, the referrer
278 // header is stripped from the request.
279 virtual bool OnCancelURLRequestWithPolicyViolatingReferrerHeader(
280 const URLRequest
& request
,
281 const GURL
& target_url
,
282 const GURL
& referrer_url
) const = 0;
287 #endif // NET_BASE_NETWORK_DELEGATE_H_