1 // Copyright 2014 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_IMPL_H_
6 #define NET_BASE_NETWORK_DELEGATE_IMPL_H_
8 #include "base/strings/string16.h"
9 #include "net/base/completion_callback.h"
10 #include "net/base/network_delegate.h"
11 #include "net/cookies/canonical_cookie.h"
22 class HttpRequestHeaders
;
23 class HttpResponseHeaders
;
29 class NET_EXPORT NetworkDelegateImpl
: public NetworkDelegate
{
31 ~NetworkDelegateImpl() override
{}
34 // This is the interface for subclasses of NetworkDelegate to implement. These
35 // member functions will be called by the respective public notification
36 // member function, which will perform basic sanity checking.
38 // Called before a request is sent. Allows the delegate to rewrite the URL
39 // being fetched by modifying |new_url|. If set, the URL must be valid. The
40 // reference fragment from the original URL is not automatically appended to
41 // |new_url|; callers are responsible for copying the reference fragment if
43 // |callback| and |new_url| are valid only until OnURLRequestDestroyed is
44 // called for this request. Returns a net status code, generally either OK to
45 // continue with the request or ERR_IO_PENDING if the result is not ready yet.
46 // A status code other than OK and ERR_IO_PENDING will cancel the request and
47 // report the status code as the reason.
49 // The default implementation returns OK (continue with request).
50 int OnBeforeURLRequest(URLRequest
* request
,
51 const CompletionCallback
& callback
,
52 GURL
* new_url
) override
;
54 // Called as the proxy is being resolved for |url|. Allows the delegate to
55 // override the proxy resolution decision made by ProxyService. The delegate
56 // may override the decision by modifying the ProxyInfo |result|.
57 void OnResolveProxy(const GURL
& url
,
59 const ProxyService
& proxy_service
,
60 ProxyInfo
* result
) override
;
62 // Called when use of |bad_proxy| fails due to |net_error|. |net_error| is
63 // the network error encountered, if any, and OK if the fallback was
64 // for a reason other than a network error (e.g. the proxy service was
65 // explicitly directed to skip a proxy).
66 void OnProxyFallback(const ProxyServer
& bad_proxy
, int net_error
) override
;
68 // Called right before the HTTP headers are sent. Allows the delegate to
69 // read/write |headers| before they get sent out. |callback| and |headers| are
70 // valid only until OnCompleted or OnURLRequestDestroyed is called for this
72 // See OnBeforeURLRequest for return value description. Returns OK by default.
73 int OnBeforeSendHeaders(URLRequest
* request
,
74 const CompletionCallback
& callback
,
75 HttpRequestHeaders
* headers
) override
;
77 // Called after a proxy connection. Allows the delegate to read/write
78 // |headers| before they get sent out. |headers| is valid only until
79 // OnCompleted or OnURLRequestDestroyed is called for this request.
80 void OnBeforeSendProxyHeaders(URLRequest
* request
,
81 const ProxyInfo
& proxy_info
,
82 HttpRequestHeaders
* headers
) override
;
84 // Called right before the HTTP request(s) are being sent to the network.
85 // |headers| is only valid until OnCompleted or OnURLRequestDestroyed is
86 // called for this request.
87 void OnSendHeaders(URLRequest
* request
,
88 const HttpRequestHeaders
& headers
) override
;
90 // Called for HTTP requests when the headers have been received.
91 // |original_response_headers| contains the headers as received over the
92 // network, these must not be modified. |override_response_headers| can be set
93 // to new values, that should be considered as overriding
94 // |original_response_headers|.
95 // If the response is a redirect, and the Location response header value is
96 // identical to |allowed_unsafe_redirect_url|, then the redirect is never
97 // blocked and the reference fragment is not copied from the original URL
98 // to the redirection target.
100 // |callback|, |original_response_headers|, and |override_response_headers|
101 // are only valid until OnURLRequestDestroyed is called for this request.
102 // See OnBeforeURLRequest for return value description. Returns OK by default.
103 int OnHeadersReceived(
105 const CompletionCallback
& callback
,
106 const HttpResponseHeaders
* original_response_headers
,
107 scoped_refptr
<HttpResponseHeaders
>* override_response_headers
,
108 GURL
* allowed_unsafe_redirect_url
) override
;
110 // Called right after a redirect response code was received.
111 // |new_location| is only valid until OnURLRequestDestroyed is called for this
113 void OnBeforeRedirect(URLRequest
* request
, const GURL
& new_location
) override
;
115 // This corresponds to URLRequestDelegate::OnResponseStarted.
116 void OnResponseStarted(URLRequest
* request
) override
;
118 // Called every time we read raw bytes.
119 void OnRawBytesRead(const URLRequest
& request
, int bytes_read
) override
;
121 // Indicates that the URL request has been completed or failed.
122 // |started| indicates whether the request has been started. If false,
123 // some information like the socket address is not available.
124 void OnCompleted(URLRequest
* request
, bool started
) override
;
126 // Called when an URLRequest is being destroyed. Note that the request is
127 // being deleted, so it's not safe to call any methods that may result in
128 // a virtual method call.
129 void OnURLRequestDestroyed(URLRequest
* request
) override
;
131 // Corresponds to ProxyResolverJSBindings::OnError.
132 void OnPACScriptError(int line_number
, const base::string16
& error
) override
;
134 // Called when a request receives an authentication challenge
135 // specified by |auth_info|, and is unable to respond using cached
136 // credentials. |callback| and |credentials| must be non-NULL, and must
137 // be valid until OnURLRequestDestroyed is called for |request|.
139 // The following return values are allowed:
140 // - AUTH_REQUIRED_RESPONSE_NO_ACTION: |auth_info| is observed, but
141 // no action is being taken on it.
142 // - AUTH_REQUIRED_RESPONSE_SET_AUTH: |credentials| is filled in with
143 // a username and password, which should be used in a response to
145 // - AUTH_REQUIRED_RESPONSE_CANCEL_AUTH: The authentication challenge
146 // should not be attempted.
147 // - AUTH_REQUIRED_RESPONSE_IO_PENDING: The action will be decided
148 // asynchronously. |callback| will be invoked when the decision is made,
149 // and one of the other AuthRequiredResponse values will be passed in with
150 // the same semantics as described above.
151 AuthRequiredResponse
OnAuthRequired(URLRequest
* request
,
152 const AuthChallengeInfo
& auth_info
,
153 const AuthCallback
& callback
,
154 AuthCredentials
* credentials
) override
;
156 // Called when reading cookies to allow the network delegate to block access
157 // to the cookie. This method will never be invoked when
158 // LOAD_DO_NOT_SEND_COOKIES is specified.
159 bool OnCanGetCookies(const URLRequest
& request
,
160 const CookieList
& cookie_list
) override
;
162 // Called when a cookie is set to allow the network delegate to block access
163 // to the cookie. This method will never be invoked when
164 // LOAD_DO_NOT_SAVE_COOKIES is specified.
165 bool OnCanSetCookie(const URLRequest
& request
,
166 const std::string
& cookie_line
,
167 CookieOptions
* options
) override
;
169 // Called when a file access is attempted to allow the network delegate to
170 // allow or block access to the given file path. Returns true if access is
172 bool OnCanAccessFile(const URLRequest
& request
,
173 const base::FilePath
& path
) const override
;
175 // Returns true if the given request may be rejected when the
176 // URLRequestThrottlerManager believes the server servicing the
177 // request is overloaded or down.
178 bool OnCanThrottleRequest(const URLRequest
& request
) const override
;
180 // Returns true if the given |url| has to be requested over connection that
181 // is not tracked by the server. Usually is false, unless user privacy
182 // settings block cookies from being get or set.
183 bool OnCanEnablePrivacyMode(
185 const GURL
& first_party_for_cookies
) const override
;
187 // Returns true if the embedder has enabled the "first-party" cookie
188 // experiment, and false otherwise.
190 // TODO(mkwst): Remove this once we decide whether or not we wish to ship
191 // first-party cookies. https://crbug.com/459154
192 bool OnFirstPartyOnlyCookieExperimentEnabled() const override
;
194 // Called when the |referrer_url| for requesting |target_url| during handling
195 // of the |request| is does not comply with the referrer policy (e.g. a
196 // secure referrer for an insecure initial target).
197 // Returns true if the request should be cancelled. Otherwise, the referrer
198 // header is stripped from the request.
199 bool OnCancelURLRequestWithPolicyViolatingReferrerHeader(
200 const URLRequest
& request
,
201 const GURL
& target_url
,
202 const GURL
& referrer_url
) const override
;
207 #endif // NET_BASE_NETWORK_DELEGATE_IMPL_H_