• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
24 // Delegate for setting up a connection.
25 class NET_EXPORT ProxyDelegate {
26  public:
27   ProxyDelegate() = default;
28   ProxyDelegate(const ProxyDelegate&) = delete;
29   ProxyDelegate& operator=(const ProxyDelegate&) = delete;
30   virtual ~ProxyDelegate() = default;
31 
32   // Called as the proxy is being resolved for |url| for a |method| request.
33   // The caller may pass an empty string to get method agnostic resoulution.
34   // Allows the delegate to override the proxy resolution decision made by
35   // ProxyResolutionService. The delegate may override the decision by modifying
36   // the ProxyInfo |result|.
37   virtual void OnResolveProxy(
38       const GURL& url,
39       const NetworkAnonymizationKey& network_anonymization_key,
40       const std::string& method,
41       const ProxyRetryInfoMap& proxy_retry_info,
42       ProxyInfo* result) = 0;
43 
44   // Called when use of a proxy chain failed due to `net_error`, but another
45   // proxy chain in the list succeeded. The failed proxy is within `bad_chain`,
46   // but it is undefined at which proxy in that chain. `net_error` is the
47   // network error encountered, if any, and OK if the fallback was for a reason
48   // other than a network error (e.g. the proxy service was explicitly directed
49   // to skip a proxy).
50   virtual void OnFallback(const ProxyChain& bad_chain, int net_error) = 0;
51 
52   // Called immediately before a proxy tunnel request is sent. Provides the
53   // embedder an opportunity to add extra request headers.
54   virtual void OnBeforeTunnelRequest(const ProxyChain& proxy_chain,
55                                      size_t chain_index,
56                                      HttpRequestHeaders* extra_headers) = 0;
57 
58   // Called when the response headers for the proxy tunnel request have been
59   // received. Allows the delegate to override the net error code of the tunnel
60   // request. Returning OK causes the standard tunnel response handling to be
61   // performed. Implementations should make sure they can trust the proxy server
62   // at position `chain_index` in `proxy_chain` before making decisions based on
63   // `response_headers`.
64   virtual Error OnTunnelHeadersReceived(
65       const ProxyChain& proxy_chain,
66       size_t chain_index,
67       const HttpResponseHeaders& response_headers) = 0;
68 
OnBeforeTunnelRequestServerOnly(const ProxyChain & proxy_chain,size_t proxy_chain_index,HttpRequestHeaders * extra_headers)69   void OnBeforeTunnelRequestServerOnly(const ProxyChain& proxy_chain,
70                                        size_t proxy_chain_index,
71                                        HttpRequestHeaders* extra_headers) {
72     DCHECK(!proxy_chain.is_direct());
73     OnBeforeTunnelRequest(proxy_chain, proxy_chain_index, extra_headers);
74   }
75 
OnTunnelHeadersReceivedServerOnly(const ProxyChain & proxy_chain,size_t proxy_chain_index,const HttpResponseHeaders & response_headers)76   Error OnTunnelHeadersReceivedServerOnly(
77       const ProxyChain& proxy_chain,
78       size_t proxy_chain_index,
79       const HttpResponseHeaders& response_headers) {
80     return OnTunnelHeadersReceived(proxy_chain, proxy_chain_index,
81                                    response_headers);
82   }
83 };
84 
85 }  // namespace net
86 
87 #endif  // NET_BASE_PROXY_DELEGATE_H_
88