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 #include "net/proxy_resolution/proxy_info.h" 6 7 #include "net/proxy_resolution/proxy_retry_info.h" 8 9 namespace net { 10 11 ProxyInfo::ProxyInfo() = default; 12 13 ProxyInfo::ProxyInfo(const ProxyInfo& other) = default; 14 15 ProxyInfo::~ProxyInfo() = default; 16 Use(const ProxyInfo & other)17void ProxyInfo::Use(const ProxyInfo& other) { 18 proxy_resolve_start_time_ = other.proxy_resolve_start_time_; 19 proxy_resolve_end_time_ = other.proxy_resolve_end_time_; 20 is_for_ip_protection_ = other.is_for_ip_protection_; 21 proxy_list_ = other.proxy_list_; 22 proxy_retry_info_ = other.proxy_retry_info_; 23 did_bypass_proxy_ = other.did_bypass_proxy_; 24 } 25 UseDirect()26void ProxyInfo::UseDirect() { 27 Reset(); 28 proxy_list_.SetSingleProxyServer(ProxyServer::Direct()); 29 } 30 UseDirectWithBypassedProxy()31void ProxyInfo::UseDirectWithBypassedProxy() { 32 UseDirect(); 33 did_bypass_proxy_ = true; 34 } 35 UseNamedProxy(const std::string & proxy_uri_list)36void ProxyInfo::UseNamedProxy(const std::string& proxy_uri_list) { 37 Reset(); 38 proxy_list_.Set(proxy_uri_list); 39 } 40 UseProxyChain(const ProxyChain & proxy_chain)41void ProxyInfo::UseProxyChain(const ProxyChain& proxy_chain) { 42 Reset(); 43 proxy_list_.SetSingleProxyChain(proxy_chain); 44 } 45 UsePacString(const std::string & pac_string)46void ProxyInfo::UsePacString(const std::string& pac_string) { 47 Reset(); 48 proxy_list_.SetFromPacString(pac_string); 49 } 50 UseProxyList(const ProxyList & proxy_list)51void ProxyInfo::UseProxyList(const ProxyList& proxy_list) { 52 Reset(); 53 proxy_list_ = proxy_list; 54 } 55 OverrideProxyList(const ProxyList & proxy_list)56void ProxyInfo::OverrideProxyList(const ProxyList& proxy_list) { 57 proxy_list_ = proxy_list; 58 } 59 ContainsMultiProxyChain() const60bool ProxyInfo::ContainsMultiProxyChain() const { 61 auto& proxy_chains = proxy_list_.AllChains(); 62 return std::any_of(proxy_chains.begin(), proxy_chains.end(), 63 [](const ProxyChain& proxy_chain) { 64 return proxy_chain.is_multi_proxy(); 65 }); 66 } 67 ToPacString() const68std::string ProxyInfo::ToPacString() const { 69 return proxy_list_.ToPacString(); 70 } 71 is_https() const72bool ProxyInfo::is_https() const { 73 if (is_empty() || is_direct()) { 74 return false; 75 } 76 if (proxy_chain().is_multi_proxy()) { 77 CHECK(AllChainProxiesAreHttps()); 78 return true; 79 } 80 return proxy_chain().GetProxyServer(/*chain_index=*/0).is_https(); 81 } 82 is_http_like() const83bool ProxyInfo::is_http_like() const { 84 if (is_empty() || is_direct()) { 85 return false; 86 } 87 if (proxy_chain().is_multi_proxy()) { 88 CHECK(AllChainProxiesAreHttps()); 89 return true; 90 } 91 return proxy_chain().GetProxyServer(/*chain_index=*/0).is_http_like(); 92 } 93 is_secure_http_like() const94bool ProxyInfo::is_secure_http_like() const { 95 if (is_empty() || is_direct()) { 96 return false; 97 } 98 if (proxy_chain().is_multi_proxy()) { 99 CHECK(AllChainProxiesAreHttps()); 100 return true; 101 } 102 return proxy_chain().GetProxyServer(/*chain_index=*/0).is_secure_http_like(); 103 } 104 is_http() const105bool ProxyInfo::is_http() const { 106 if (is_empty() || is_direct()) { 107 return false; 108 } 109 if (proxy_chain().is_multi_proxy()) { 110 CHECK(AllChainProxiesAreHttps()); 111 return false; 112 } 113 return proxy_chain().GetProxyServer(/*chain_index=*/0).is_http(); 114 } 115 is_quic() const116bool ProxyInfo::is_quic() const { 117 if (is_empty() || is_direct()) { 118 return false; 119 } 120 if (proxy_chain().is_multi_proxy()) { 121 CHECK(AllChainProxiesAreHttps()); 122 return false; 123 } 124 return proxy_chain().GetProxyServer(/*chain_index=*/0).is_quic(); 125 } 126 is_socks() const127bool ProxyInfo::is_socks() const { 128 if (is_empty() || is_direct()) { 129 return false; 130 } 131 if (proxy_chain().is_multi_proxy()) { 132 CHECK(AllChainProxiesAreHttps()); 133 return false; 134 } 135 return proxy_chain().GetProxyServer(/*chain_index=*/0).is_socks(); 136 } 137 ToDebugString() const138std::string ProxyInfo::ToDebugString() const { 139 return proxy_list_.ToDebugString(); 140 } 141 Fallback(int net_error,const NetLogWithSource & net_log)142bool ProxyInfo::Fallback(int net_error, const NetLogWithSource& net_log) { 143 return proxy_list_.Fallback(&proxy_retry_info_, net_error, net_log); 144 } 145 DeprioritizeBadProxyChains(const ProxyRetryInfoMap & proxy_retry_info)146void ProxyInfo::DeprioritizeBadProxyChains( 147 const ProxyRetryInfoMap& proxy_retry_info) { 148 proxy_list_.DeprioritizeBadProxyChains(proxy_retry_info); 149 } 150 RemoveProxiesWithoutScheme(int scheme_bit_field)151void ProxyInfo::RemoveProxiesWithoutScheme(int scheme_bit_field) { 152 proxy_list_.RemoveProxiesWithoutScheme(scheme_bit_field); 153 } 154 Reset()155void ProxyInfo::Reset() { 156 proxy_resolve_start_time_ = base::TimeTicks(); 157 proxy_resolve_end_time_ = base::TimeTicks(); 158 is_for_ip_protection_ = false; 159 proxy_list_.Clear(); 160 proxy_retry_info_.clear(); 161 did_bypass_proxy_ = false; 162 } 163 AllChainProxiesAreHttps() const164bool ProxyInfo::AllChainProxiesAreHttps() const { 165 const std::vector<ProxyServer>& proxy_servers = proxy_chain().proxy_servers(); 166 return std::all_of( 167 proxy_servers.begin(), proxy_servers.end(), 168 [](const ProxyServer& proxy_server) { return proxy_server.is_https(); }); 169 } 170 171 } // namespace net 172