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