Roll src/third_party/skia d32087a:1052f51
[chromium-blink-merge.git] / net / base / network_delegate.h
blob3a29e26c9f7695a2b7a83fe59bb73f695c6ebf97
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_
8 #include <stdint.h>
10 #include <string>
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"
19 class GURL;
21 namespace base {
22 class FilePath;
25 namespace net {
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
36 // are ok.
37 class CookieOptions;
38 class HttpRequestHeaders;
39 class HttpResponseHeaders;
40 class ProxyInfo;
41 class ProxyServer;
42 class ProxyService;
43 class URLRequest;
45 class NET_EXPORT NetworkDelegate : public base::NonThreadSafe {
46 public:
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,
66 GURL* new_url);
67 void NotifyResolveProxy(const GURL& url,
68 int load_flags,
69 const ProxyService& proxy_service,
70 ProxyInfo* result);
71 void NotifyProxyFallback(const ProxyServer& bad_proxy,
72 int net_error);
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(
82 URLRequest* request,
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 NotifyURLRequestJobOrphaned(URLRequest* request);
95 void NotifyPACScriptError(int line_number, const base::string16& error);
96 AuthRequiredResponse NotifyAuthRequired(URLRequest* request,
97 const AuthChallengeInfo& auth_info,
98 const AuthCallback& callback,
99 AuthCredentials* credentials);
100 bool CanGetCookies(const URLRequest& request,
101 const CookieList& cookie_list);
102 bool CanSetCookie(const URLRequest& request,
103 const std::string& cookie_line,
104 CookieOptions* options);
105 bool CanAccessFile(const URLRequest& request,
106 const base::FilePath& path) const;
107 bool CanEnablePrivacyMode(const GURL& url,
108 const GURL& first_party_for_cookies) const;
110 // TODO(mkwst): Remove this once we decide whether or not we wish to ship
111 // first-party cookies. https://crbug.com/459154
112 bool FirstPartyOnlyCookieExperimentEnabled() const;
114 bool CancelURLRequestWithPolicyViolatingReferrerHeader(
115 const URLRequest& request,
116 const GURL& target_url,
117 const GURL& referrer_url) const;
119 private:
120 // This is the interface for subclasses of NetworkDelegate to implement. These
121 // member functions will be called by the respective public notification
122 // member function, which will perform basic sanity checking.
124 // Called before a request is sent. Allows the delegate to rewrite the URL
125 // being fetched by modifying |new_url|. If set, the URL must be valid. The
126 // reference fragment from the original URL is not automatically appended to
127 // |new_url|; callers are responsible for copying the reference fragment if
128 // desired.
129 // |callback| and |new_url| are valid only until OnURLRequestDestroyed is
130 // called for this request. Returns a net status code, generally either OK to
131 // continue with the request or ERR_IO_PENDING if the result is not ready yet.
132 // A status code other than OK and ERR_IO_PENDING will cancel the request and
133 // report the status code as the reason.
135 // The default implementation returns OK (continue with request).
136 virtual int OnBeforeURLRequest(URLRequest* request,
137 const CompletionCallback& callback,
138 GURL* new_url) = 0;
140 // Called as the proxy is being resolved for |url|. Allows the delegate to
141 // override the proxy resolution decision made by ProxyService. The delegate
142 // may override the decision by modifying the ProxyInfo |result|.
143 virtual void OnResolveProxy(const GURL& url,
144 int load_flags,
145 const ProxyService& proxy_service,
146 ProxyInfo* result) = 0;
148 // Called when use of |bad_proxy| fails due to |net_error|. |net_error| is
149 // the network error encountered, if any, and OK if the fallback was
150 // for a reason other than a network error (e.g. the proxy service was
151 // explicitly directed to skip a proxy).
152 virtual void OnProxyFallback(const ProxyServer& bad_proxy, int net_error) = 0;
154 // Called right before the HTTP headers are sent. Allows the delegate to
155 // read/write |headers| before they get sent out. |callback| and |headers| are
156 // valid only until OnCompleted or OnURLRequestDestroyed is called for this
157 // request.
158 // See OnBeforeURLRequest for return value description. Returns OK by default.
159 virtual int OnBeforeSendHeaders(URLRequest* request,
160 const CompletionCallback& callback,
161 HttpRequestHeaders* headers) = 0;
163 // Called after a proxy connection. Allows the delegate to read/write
164 // |headers| before they get sent out. |headers| is valid only until
165 // OnCompleted or OnURLRequestDestroyed is called for this request.
166 virtual void OnBeforeSendProxyHeaders(URLRequest* request,
167 const ProxyInfo& proxy_info,
168 HttpRequestHeaders* headers) = 0;
170 // Called right before the HTTP request(s) are being sent to the network.
171 // |headers| is only valid until OnCompleted or OnURLRequestDestroyed is
172 // called for this request.
173 virtual void OnSendHeaders(URLRequest* request,
174 const HttpRequestHeaders& headers) = 0;
176 // Called for HTTP requests when the headers have been received.
177 // |original_response_headers| contains the headers as received over the
178 // network, these must not be modified. |override_response_headers| can be set
179 // to new values, that should be considered as overriding
180 // |original_response_headers|.
181 // If the response is a redirect, and the Location response header value is
182 // identical to |allowed_unsafe_redirect_url|, then the redirect is never
183 // blocked and the reference fragment is not copied from the original URL
184 // to the redirection target.
186 // |callback|, |original_response_headers|, and |override_response_headers|
187 // are only valid until OnURLRequestDestroyed is called for this request.
188 // See OnBeforeURLRequest for return value description. Returns OK by default.
189 virtual int OnHeadersReceived(
190 URLRequest* request,
191 const CompletionCallback& callback,
192 const HttpResponseHeaders* original_response_headers,
193 scoped_refptr<HttpResponseHeaders>* override_response_headers,
194 GURL* allowed_unsafe_redirect_url) = 0;
196 // Called right after a redirect response code was received.
197 // |new_location| is only valid until OnURLRequestDestroyed is called for this
198 // request.
199 virtual void OnBeforeRedirect(URLRequest* request,
200 const GURL& new_location) = 0;
202 // This corresponds to URLRequestDelegate::OnResponseStarted.
203 virtual void OnResponseStarted(URLRequest* request) = 0;
205 // Called when bytes are received from the network, such as after receiving
206 // headers or reading raw response bytes. This includes localhost requests.
207 // |bytes_received| is the number of bytes measured at the application layer
208 // that have been received over the network for this request since the last
209 // time OnNetworkBytesReceived was called. |bytes_received| will always be
210 // greater than 0.
211 // Currently, this is only implemented for HTTP transactions, and
212 // |bytes_received| does not include TLS overhead or TCP retransmits.
213 virtual void OnNetworkBytesReceived(const URLRequest& request,
214 int64_t bytes_received) = 0;
216 // Indicates that the URL request has been completed or failed.
217 // |started| indicates whether the request has been started. If false,
218 // some information like the socket address is not available.
219 virtual void OnCompleted(URLRequest* request, bool started) = 0;
221 // Called when an URLRequest is being destroyed. Note that the request is
222 // being deleted, so it's not safe to call any methods that may result in
223 // a virtual method call.
224 virtual void OnURLRequestDestroyed(URLRequest* request) = 0;
226 // Called when the current job for |request| is orphaned. This is a temporary
227 // callback to diagnose https://crbug.com/289715 and may not be used for other
228 // purposes. Note that it may be called after OnURLRequestDestroyed.
230 // TODO(davidben): Remove this once data has been gathered.
231 virtual void OnURLRequestJobOrphaned(URLRequest* request) = 0;
233 // Corresponds to ProxyResolverJSBindings::OnError.
234 virtual void OnPACScriptError(int line_number,
235 const base::string16& error) = 0;
237 // Called when a request receives an authentication challenge
238 // specified by |auth_info|, and is unable to respond using cached
239 // credentials. |callback| and |credentials| must be non-NULL, and must
240 // be valid until OnURLRequestDestroyed is called for |request|.
242 // The following return values are allowed:
243 // - AUTH_REQUIRED_RESPONSE_NO_ACTION: |auth_info| is observed, but
244 // no action is being taken on it.
245 // - AUTH_REQUIRED_RESPONSE_SET_AUTH: |credentials| is filled in with
246 // a username and password, which should be used in a response to
247 // |auth_info|.
248 // - AUTH_REQUIRED_RESPONSE_CANCEL_AUTH: The authentication challenge
249 // should not be attempted.
250 // - AUTH_REQUIRED_RESPONSE_IO_PENDING: The action will be decided
251 // asynchronously. |callback| will be invoked when the decision is made,
252 // and one of the other AuthRequiredResponse values will be passed in with
253 // the same semantics as described above.
254 virtual AuthRequiredResponse OnAuthRequired(
255 URLRequest* request,
256 const AuthChallengeInfo& auth_info,
257 const AuthCallback& callback,
258 AuthCredentials* credentials) = 0;
260 // Called when reading cookies to allow the network delegate to block access
261 // to the cookie. This method will never be invoked when
262 // LOAD_DO_NOT_SEND_COOKIES is specified.
263 virtual bool OnCanGetCookies(const URLRequest& request,
264 const CookieList& cookie_list) = 0;
266 // Called when a cookie is set to allow the network delegate to block access
267 // to the cookie. This method will never be invoked when
268 // LOAD_DO_NOT_SAVE_COOKIES is specified.
269 virtual bool OnCanSetCookie(const URLRequest& request,
270 const std::string& cookie_line,
271 CookieOptions* options) = 0;
273 // Called when a file access is attempted to allow the network delegate to
274 // allow or block access to the given file path. Returns true if access is
275 // allowed.
276 virtual bool OnCanAccessFile(const URLRequest& request,
277 const base::FilePath& path) const = 0;
279 // Returns true if the given |url| has to be requested over connection that
280 // is not tracked by the server. Usually is false, unless user privacy
281 // settings block cookies from being get or set.
282 virtual bool OnCanEnablePrivacyMode(
283 const GURL& url,
284 const GURL& first_party_for_cookies) const = 0;
286 // Returns true if the embedder has enabled the "first-party" cookie
287 // experiment, and false otherwise.
289 // TODO(mkwst): Remove this once we decide whether or not we wish to ship
290 // first-party cookies. https://crbug.com/459154
291 virtual bool OnFirstPartyOnlyCookieExperimentEnabled() const = 0;
293 // Called when the |referrer_url| for requesting |target_url| during handling
294 // of the |request| is does not comply with the referrer policy (e.g. a
295 // secure referrer for an insecure initial target).
296 // Returns true if the request should be cancelled. Otherwise, the referrer
297 // header is stripped from the request.
298 virtual bool OnCancelURLRequestWithPolicyViolatingReferrerHeader(
299 const URLRequest& request,
300 const GURL& target_url,
301 const GURL& referrer_url) const = 0;
304 } // namespace net
306 #endif // NET_BASE_NETWORK_DELEGATE_H_