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_
10 #include "base/strings/string16.h"
11 #include "net/base/completion_callback.h"
12 #include "net/base/network_delegate.h"
13 #include "net/cookies/canonical_cookie.h"
24 class HttpRequestHeaders
;
25 class HttpResponseHeaders
;
31 class NET_EXPORT NetworkDelegateImpl
: public NetworkDelegate
{
33 ~NetworkDelegateImpl() override
{}
36 // This is the interface for subclasses of NetworkDelegate to implement. These
37 // member functions will be called by the respective public notification
38 // member function, which will perform basic sanity checking.
40 // Called before a request is sent. Allows the delegate to rewrite the URL
41 // being fetched by modifying |new_url|. If set, the URL must be valid. The
42 // reference fragment from the original URL is not automatically appended to
43 // |new_url|; callers are responsible for copying the reference fragment if
45 // |callback| and |new_url| are valid only until OnURLRequestDestroyed is
46 // called for this request. Returns a net status code, generally either OK to
47 // continue with the request or ERR_IO_PENDING if the result is not ready yet.
48 // A status code other than OK and ERR_IO_PENDING will cancel the request and
49 // report the status code as the reason.
51 // The default implementation returns OK (continue with request).
52 int OnBeforeURLRequest(URLRequest
* request
,
53 const CompletionCallback
& callback
,
54 GURL
* new_url
) override
;
56 // Called as the proxy is being resolved for |url|. Allows the delegate to
57 // override the proxy resolution decision made by ProxyService. The delegate
58 // may override the decision by modifying the ProxyInfo |result|.
59 void OnResolveProxy(const GURL
& url
,
61 const ProxyService
& proxy_service
,
62 ProxyInfo
* result
) override
;
64 // Called when use of |bad_proxy| fails due to |net_error|. |net_error| is
65 // the network error encountered, if any, and OK if the fallback was
66 // for a reason other than a network error (e.g. the proxy service was
67 // explicitly directed to skip a proxy).
68 void OnProxyFallback(const ProxyServer
& bad_proxy
, int net_error
) override
;
70 // Called right before the HTTP headers are sent. Allows the delegate to
71 // read/write |headers| before they get sent out. |callback| and |headers| are
72 // valid only until OnCompleted or OnURLRequestDestroyed is called for this
74 // See OnBeforeURLRequest for return value description. Returns OK by default.
75 int OnBeforeSendHeaders(URLRequest
* request
,
76 const CompletionCallback
& callback
,
77 HttpRequestHeaders
* headers
) override
;
79 // Called after a proxy connection. Allows the delegate to read/write
80 // |headers| before they get sent out. |headers| is valid only until
81 // OnCompleted or OnURLRequestDestroyed is called for this request.
82 void OnBeforeSendProxyHeaders(URLRequest
* request
,
83 const ProxyInfo
& proxy_info
,
84 HttpRequestHeaders
* headers
) override
;
86 // Called right before the HTTP request(s) are being sent to the network.
87 // |headers| is only valid until OnCompleted or OnURLRequestDestroyed is
88 // called for this request.
89 void OnSendHeaders(URLRequest
* request
,
90 const HttpRequestHeaders
& headers
) override
;
92 // Called for HTTP requests when the headers have been received.
93 // |original_response_headers| contains the headers as received over the
94 // network, these must not be modified. |override_response_headers| can be set
95 // to new values, that should be considered as overriding
96 // |original_response_headers|.
97 // If the response is a redirect, and the Location response header value is
98 // identical to |allowed_unsafe_redirect_url|, then the redirect is never
99 // blocked and the reference fragment is not copied from the original URL
100 // to the redirection target.
102 // |callback|, |original_response_headers|, and |override_response_headers|
103 // are only valid until OnURLRequestDestroyed is called for this request.
104 // See OnBeforeURLRequest for return value description. Returns OK by default.
105 int OnHeadersReceived(
107 const CompletionCallback
& callback
,
108 const HttpResponseHeaders
* original_response_headers
,
109 scoped_refptr
<HttpResponseHeaders
>* override_response_headers
,
110 GURL
* allowed_unsafe_redirect_url
) override
;
112 // Called right after a redirect response code was received.
113 // |new_location| is only valid until OnURLRequestDestroyed is called for this
115 void OnBeforeRedirect(URLRequest
* request
, const GURL
& new_location
) override
;
117 // This corresponds to URLRequestDelegate::OnResponseStarted.
118 void OnResponseStarted(URLRequest
* request
) override
;
120 // Called when bytes are received from the network, such as after receiving
121 // headers or reading raw response bytes. This includes localhost requests.
122 // |bytes_received| is the number of bytes measured at the application layer
123 // that have been received over the network for this request since the last
124 // time OnNetworkBytesReceived was called. |bytes_received| will always be
126 // Currently, this is only implemented for HTTP transactions, and
127 // |bytes_received| does not include TLS overhead or TCP retransmits.
128 void OnNetworkBytesReceived(const URLRequest
& request
,
129 int64_t bytes_received
) override
;
131 // Indicates that the URL request has been completed or failed.
132 // |started| indicates whether the request has been started. If false,
133 // some information like the socket address is not available.
134 void OnCompleted(URLRequest
* request
, bool started
) override
;
136 // Called when an URLRequest is being destroyed. Note that the request is
137 // being deleted, so it's not safe to call any methods that may result in
138 // a virtual method call.
139 void OnURLRequestDestroyed(URLRequest
* request
) override
;
141 // Called when the current job for |request| is orphaned. This is a temporary
142 // callback to diagnose https://crbug.com/289715 and may not be used for other
143 // purposes. Note that it may be called after OnURLRequestDestroyed.
145 // TODO(davidben): Remove this once data has been gathered.
146 void OnURLRequestJobOrphaned(URLRequest
* request
) override
;
148 // Corresponds to ProxyResolverJSBindings::OnError.
149 void OnPACScriptError(int line_number
, const base::string16
& error
) override
;
151 // Called when a request receives an authentication challenge
152 // specified by |auth_info|, and is unable to respond using cached
153 // credentials. |callback| and |credentials| must be non-NULL, and must
154 // be valid until OnURLRequestDestroyed is called for |request|.
156 // The following return values are allowed:
157 // - AUTH_REQUIRED_RESPONSE_NO_ACTION: |auth_info| is observed, but
158 // no action is being taken on it.
159 // - AUTH_REQUIRED_RESPONSE_SET_AUTH: |credentials| is filled in with
160 // a username and password, which should be used in a response to
162 // - AUTH_REQUIRED_RESPONSE_CANCEL_AUTH: The authentication challenge
163 // should not be attempted.
164 // - AUTH_REQUIRED_RESPONSE_IO_PENDING: The action will be decided
165 // asynchronously. |callback| will be invoked when the decision is made,
166 // and one of the other AuthRequiredResponse values will be passed in with
167 // the same semantics as described above.
168 AuthRequiredResponse
OnAuthRequired(URLRequest
* request
,
169 const AuthChallengeInfo
& auth_info
,
170 const AuthCallback
& callback
,
171 AuthCredentials
* credentials
) override
;
173 // Called when reading cookies to allow the network delegate to block access
174 // to the cookie. This method will never be invoked when
175 // LOAD_DO_NOT_SEND_COOKIES is specified.
176 bool OnCanGetCookies(const URLRequest
& request
,
177 const CookieList
& cookie_list
) override
;
179 // Called when a cookie is set to allow the network delegate to block access
180 // to the cookie. This method will never be invoked when
181 // LOAD_DO_NOT_SAVE_COOKIES is specified.
182 bool OnCanSetCookie(const URLRequest
& request
,
183 const std::string
& cookie_line
,
184 CookieOptions
* options
) override
;
186 // Called when a file access is attempted to allow the network delegate to
187 // allow or block access to the given file path. Returns true if access is
189 bool OnCanAccessFile(const URLRequest
& request
,
190 const base::FilePath
& path
) const override
;
192 // Returns true if the given |url| has to be requested over connection that
193 // is not tracked by the server. Usually is false, unless user privacy
194 // settings block cookies from being get or set.
195 bool OnCanEnablePrivacyMode(
197 const GURL
& first_party_for_cookies
) const override
;
199 // Returns true if the embedder has enabled the "first-party" cookie
200 // experiment, and false otherwise.
202 // TODO(mkwst): Remove this once we decide whether or not we wish to ship
203 // first-party cookies. https://crbug.com/459154
204 bool OnFirstPartyOnlyCookieExperimentEnabled() const override
;
206 // Called when the |referrer_url| for requesting |target_url| during handling
207 // of the |request| is does not comply with the referrer policy (e.g. a
208 // secure referrer for an insecure initial target).
209 // Returns true if the request should be cancelled. Otherwise, the referrer
210 // header is stripped from the request.
211 bool OnCancelURLRequestWithPolicyViolatingReferrerHeader(
212 const URLRequest
& request
,
213 const GURL
& target_url
,
214 const GURL
& referrer_url
) const override
;
219 #endif // NET_BASE_NETWORK_DELEGATE_IMPL_H_