xref: /aosp_15_r20/external/cronet/net/proxy_resolution/proxy_info.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 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_PROXY_RESOLUTION_PROXY_INFO_H_
6 #define NET_PROXY_RESOLUTION_PROXY_INFO_H_
7 
8 #include <string>
9 
10 #include "base/gtest_prod_util.h"
11 #include "base/time/time.h"
12 #include "net/base/net_export.h"
13 #include "net/base/proxy_chain.h"
14 #include "net/base/proxy_server.h"
15 #include "net/proxy_resolution/proxy_config.h"
16 #include "net/proxy_resolution/proxy_list.h"
17 #include "net/proxy_resolution/proxy_retry_info.h"
18 #include "net/traffic_annotation/network_traffic_annotation.h"
19 
20 namespace net {
21 
22 class NetLogWithSource;
23 
24 // This object holds proxy information returned by ResolveProxy.
25 class NET_EXPORT ProxyInfo {
26  public:
27   ProxyInfo();
28   ProxyInfo(const ProxyInfo& other);
29   ~ProxyInfo();
30   // Default copy-constructor and assignment operator are OK!
31 
32   // Uses the same proxy server as the given |proxy_info|.
33   void Use(const ProxyInfo& proxy_info);
34 
35   // Uses a direct connection.
36   void UseDirect();
37 
38   // Uses a direct connection. did_bypass_proxy() will return true to indicate
39   // that the direct connection is the result of configured proxy bypass rules.
40   void UseDirectWithBypassedProxy();
41 
42   // Uses a specific proxy server, of the form:
43   //   proxy-uri = [<scheme> "://"] <hostname> [":" <port>]
44   // This may optionally be a semi-colon delimited list of <proxy-uri>.
45   // It is OK to have LWS between entries.
46   void UseNamedProxy(const std::string& proxy_uri_list);
47 
48   // Sets the proxy list to a single entry, |proxy_chain|.
49   void UseProxyChain(const ProxyChain& proxy_chain);
50 
51   // Parses from the given PAC result.
52   void UsePacString(const std::string& pac_string);
53 
54   // Uses the proxies from the given list.
55   void UseProxyList(const ProxyList& proxy_list);
56 
57   // Uses the proxies from the given list, but does not otherwise reset the
58   // proxy configuration.
59   void OverrideProxyList(const ProxyList& proxy_list);
60 
61   // Indicates that the request that uses this proxy config caused a match with
62   // the masked domain list.
63   // This is a temporary workaround to gather initial metrics for IP Protection.
64   // TODO(1507085): Remove once the experiment is concluded.
set_is_mdl_match(bool is_mdl_match)65   void set_is_mdl_match(bool is_mdl_match) { is_mdl_match_ = is_mdl_match; }
66 
67   // Returns true if this proxy info specifies a direct connection.
is_direct()68   bool is_direct() const {
69     // We don't implicitly fallback to DIRECT unless it was added to the list.
70     if (is_empty()) {
71       return false;
72     }
73     return proxy_chain().is_direct();
74   }
75 
is_direct_only()76   bool is_direct_only() const {
77     return is_direct() && proxy_list_.size() == 1 && proxy_retry_info_.empty();
78   }
79 
80   // Return true if there is at least one proxy chain, and at least one proxy
81   // server in that chain matches the given predicate.
82   template <class Predicate>
AnyProxyInChain(Predicate p)83   bool AnyProxyInChain(Predicate p) const {
84     if (is_empty()) {
85       return false;
86     }
87     return proxy_chain().AnyProxy(p);
88   }
89 
90   // Returns true if any of the contained ProxyChains are multi-proxy.
91   bool ContainsMultiProxyChain() const;
92 
93   // Returns true if this proxy info has no proxies left to try.
is_empty()94   bool is_empty() const {
95     return proxy_list_.IsEmpty();
96   }
97 
98   // Returns true if this proxy resolution is using a direct connection due to
99   // proxy bypass rules.
did_bypass_proxy()100   bool did_bypass_proxy() const {
101     return did_bypass_proxy_;
102   }
103 
104   // Returns true if the first proxy chain corresponds to one used for IP
105   // Protection. For more info, see `ProxyChain::is_for_ip_protection()`.
106   bool is_for_ip_protection() const;
107 
108   // Returns true if the request that uses this proxy config caused a match with
109   // the masked domain list.
110   // This is a temporary workaround to gather initial metrics for IP Protection.
111   // TODO(1507085): Remove once the experiment is concluded.
is_mdl_match()112   bool is_mdl_match() const { return is_mdl_match_; }
113 
114   // Returns the first valid proxy chain. is_empty() must be false to be able
115   // to call this function.
proxy_chain()116   const ProxyChain& proxy_chain() const { return proxy_list_.First(); }
117 
118   // Returns the full list of proxies to use.
proxy_list()119   const ProxyList& proxy_list() const { return proxy_list_; }
120 
121   // See description in ProxyList::ToPacString().
122   std::string ToPacString() const;
123 
124   // See description in ProxyList::ToDebugString().
125   std::string ToDebugString() const;
126 
127   // Marks the current proxy as bad. |net_error| should contain the network
128   // error encountered when this proxy was tried, if any. If this fallback
129   // is not because of a network error, then |OK| should be passed in (eg. for
130   // reasons such as local policy). Returns true if there is another proxy
131   // available to try in |proxy_list_|.
132   bool Fallback(int net_error, const NetLogWithSource& net_log);
133 
134   // De-prioritizes the proxies that we have cached as not working, by moving
135   // them to the end of the proxy list.
136   void DeprioritizeBadProxyChains(const ProxyRetryInfoMap& proxy_retry_info);
137 
138   // Deletes any entry which doesn't have one of the specified proxy schemes.
139   void RemoveProxiesWithoutScheme(int scheme_bit_field);
140 
set_proxy_resolve_start_time(const base::TimeTicks & proxy_resolve_start_time)141   void set_proxy_resolve_start_time(
142       const base::TimeTicks& proxy_resolve_start_time) {
143     proxy_resolve_start_time_ = proxy_resolve_start_time;
144   }
145 
proxy_resolve_start_time()146   base::TimeTicks proxy_resolve_start_time() const {
147     return proxy_resolve_start_time_;
148   }
149 
set_proxy_resolve_end_time(const base::TimeTicks & proxy_resolve_end_time)150   void set_proxy_resolve_end_time(
151       const base::TimeTicks& proxy_resolve_end_time) {
152     proxy_resolve_end_time_ = proxy_resolve_end_time;
153   }
154 
proxy_resolve_end_time()155   base::TimeTicks proxy_resolve_end_time() const {
156     return proxy_resolve_end_time_;
157   }
158 
set_traffic_annotation(const MutableNetworkTrafficAnnotationTag & traffic_annotation)159   void set_traffic_annotation(
160       const MutableNetworkTrafficAnnotationTag& traffic_annotation) {
161     traffic_annotation_ = traffic_annotation;
162   }
163 
traffic_annotation()164   MutableNetworkTrafficAnnotationTag traffic_annotation() const {
165     return traffic_annotation_;
166   }
167 
proxy_retry_info()168   const ProxyRetryInfoMap& proxy_retry_info() const {
169     return proxy_retry_info_;
170   }
171 
172  private:
173   // Reset proxy and config settings.
174   void Reset();
175 
176   // Verify that all proxies in the first chain have `SCHEME_HTTPS`. This is
177   // currently enforced by `ProxyChain::IsValid`, and assumed by various `is_..`
178   // methods in this class.
179   bool AllChainProxiesAreHttps() const;
180 
181   // The ordered list of proxy servers (including DIRECT attempts) remaining to
182   // try. If proxy_list_ is empty, then there is nothing left to fall back to.
183   ProxyList proxy_list_;
184 
185   // List of proxies that have been tried already.
186   ProxyRetryInfoMap proxy_retry_info_;
187 
188   // The traffic annotation of the used proxy config.
189   MutableNetworkTrafficAnnotationTag traffic_annotation_;
190 
191   // Whether the proxy result represent a proxy bypass.
192   bool did_bypass_proxy_ = false;
193 
194   // Whether the request that uses this proxy config caused a match with the
195   // masked domain list.
196   // This is a temporary workaround to gather initial metrics for IP Protection.
197   // TODO(1507085): Remove once the experiment is concluded.
198   bool is_mdl_match_ = false;
199 
200   // How long it took to resolve the proxy.  Times are both null if proxy was
201   // determined synchronously without running a PAC.
202   base::TimeTicks proxy_resolve_start_time_;
203   base::TimeTicks proxy_resolve_end_time_;
204 };
205 
206 }  // namespace net
207 
208 #endif  // NET_PROXY_RESOLUTION_PROXY_INFO_H_
209