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