xref: /aosp_15_r20/external/cronet/net/base/proxy_delegate.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef NET_BASE_PROXY_DELEGATE_H_
6 #define NET_BASE_PROXY_DELEGATE_H_
7 
8 #include <string>
9 
10 #include "net/base/net_errors.h"
11 #include "net/base/net_export.h"
12 #include "net/base/network_anonymization_key.h"
13 #include "net/base/proxy_chain.h"
14 #include "net/proxy_resolution/proxy_retry_info.h"
15 
16 class GURL;
17 
18 namespace net {
19 
20 class HttpRequestHeaders;
21 class HttpResponseHeaders;
22 class ProxyInfo;
23 class ProxyResolutionService;
24 
25 // Delegate for setting up a connection.
26 class NET_EXPORT ProxyDelegate {
27  public:
28   ProxyDelegate() = default;
29   ProxyDelegate(const ProxyDelegate&) = delete;
30   ProxyDelegate& operator=(const ProxyDelegate&) = delete;
31   virtual ~ProxyDelegate() = default;
32 
33   // Called as the proxy is being resolved for |url| for a |method| request.
34   // The caller may pass an empty string to get method agnostic resoulution.
35   // Allows the delegate to override the proxy resolution decision made by
36   // ProxyResolutionService. The delegate may override the decision by modifying
37   // the ProxyInfo |result|.
38   virtual void OnResolveProxy(
39       const GURL& url,
40       const NetworkAnonymizationKey& network_anonymization_key,
41       const std::string& method,
42       const ProxyRetryInfoMap& proxy_retry_info,
43       ProxyInfo* result) = 0;
44 
45   // Called when use of a proxy chain failed due to `net_error`, but another
46   // proxy chain in the list succeeded. The failed proxy is within `bad_chain`,
47   // but it is undefined at which proxy in that chain. `net_error` is the
48   // network error encountered, if any, and OK if the fallback was for a reason
49   // other than a network error (e.g. the proxy service was explicitly directed
50   // to skip a proxy).
51   //
52   // This method is called for each bad chain in the proxy list after a request
53   // has ultimately been successful. If the request fails for all proxies in the
54   // list, this method will not be called.
55   virtual void OnFallback(const ProxyChain& bad_chain, int net_error) = 0;
56 
57   // Called when a request is successful after failing with one or more proxy
58   // chains in the list. This is called before OnFallback is called for each new
59   // failing proxy chain.
60   virtual void OnSuccessfulRequestAfterFailures(
61       const ProxyRetryInfoMap& proxy_retry_info) = 0;
62 
63   // Called immediately before a proxy tunnel request is sent. Provides the
64   // embedder an opportunity to add extra request headers.
65   virtual void OnBeforeTunnelRequest(const ProxyChain& proxy_chain,
66                                      size_t chain_index,
67                                      HttpRequestHeaders* extra_headers) = 0;
68 
69   // Called when the response headers for the proxy tunnel request have been
70   // received. Allows the delegate to override the net error code of the tunnel
71   // request. Returning OK causes the standard tunnel response handling to be
72   // performed. Implementations should make sure they can trust the proxy server
73   // at position `chain_index` in `proxy_chain` before making decisions based on
74   // `response_headers`.
75   virtual Error OnTunnelHeadersReceived(
76       const ProxyChain& proxy_chain,
77       size_t chain_index,
78       const HttpResponseHeaders& response_headers) = 0;
79 
80   // Associates a `ProxyResolutionService` with this `ProxyDelegate`.
81   // `proxy_resolution_service` must outlive `this`.
82   virtual void SetProxyResolutionService(
83       ProxyResolutionService* proxy_resolution_service) = 0;
84 };
85 
86 }  // namespace net
87 
88 #endif  // NET_BASE_PROXY_DELEGATE_H_
89