proxy_list.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef NET_PROXY_RESOLUTION_PROXY_LIST_H_
  5. #define NET_PROXY_RESOLUTION_PROXY_LIST_H_
  6. #include <stddef.h>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "net/base/net_export.h"
  11. #include "net/proxy_resolution/proxy_retry_info.h"
  12. namespace base {
  13. class TimeDelta;
  14. class Value;
  15. }
  16. namespace net {
  17. class ProxyServer;
  18. class NetLogWithSource;
  19. // This class is used to hold a list of proxies returned by GetProxyForUrl or
  20. // manually configured. It handles proxy fallback if multiple servers are
  21. // specified.
  22. class NET_EXPORT_PRIVATE ProxyList {
  23. public:
  24. ProxyList();
  25. ProxyList(const ProxyList& other);
  26. ProxyList(ProxyList&& other);
  27. ProxyList& operator=(const ProxyList& other);
  28. ProxyList& operator=(ProxyList&& other);
  29. ~ProxyList();
  30. // Initializes the proxy list to a string containing one or more proxy servers
  31. // delimited by a semicolon.
  32. void Set(const std::string& proxy_uri_list);
  33. // Set the proxy list to a single entry, |proxy_server|.
  34. void SetSingleProxyServer(const ProxyServer& proxy_server);
  35. // Append a single proxy server to the end of the proxy list.
  36. void AddProxyServer(const ProxyServer& proxy_server);
  37. // De-prioritizes the proxies that are cached as not working but are allowed
  38. // to be reconsidered, by moving them to the end of the fallback list.
  39. void DeprioritizeBadProxies(const ProxyRetryInfoMap& proxy_retry_info);
  40. // Delete any entry which doesn't have one of the specified proxy schemes.
  41. // |scheme_bit_field| is a bunch of ProxyServer::Scheme bitwise ORed together.
  42. void RemoveProxiesWithoutScheme(int scheme_bit_field);
  43. // Clear the proxy list.
  44. void Clear();
  45. // Returns true if there is nothing left in the ProxyList.
  46. bool IsEmpty() const;
  47. // Returns the number of proxy servers in this list.
  48. size_t size() const;
  49. // Returns true if |*this| lists the same proxies as |other|.
  50. bool Equals(const ProxyList& other) const;
  51. // Returns the first proxy server in the list. It is only valid to call
  52. // this if !IsEmpty().
  53. const ProxyServer& Get() const;
  54. // Returns all proxy servers in the list.
  55. const std::vector<ProxyServer>& GetAll() const;
  56. // Sets the list by parsing the PAC result |pac_string|.
  57. // Some examples for |pac_string|:
  58. // "DIRECT"
  59. // "PROXY foopy1"
  60. // "PROXY foopy1; SOCKS4 foopy2:1188"
  61. // Does a best-effort parse, and silently discards any errors.
  62. void SetFromPacString(const std::string& pac_string);
  63. // Returns a PAC-style semicolon-separated list of valid proxy servers.
  64. // For example: "PROXY xxx.xxx.xxx.xxx:xx; SOCKS yyy.yyy.yyy:yy".
  65. std::string ToPacString() const;
  66. // Returns a serialized value for the list.
  67. base::Value ToValue() const;
  68. // Marks the current proxy server as bad and deletes it from the list. The
  69. // list of known bad proxies is given by |proxy_retry_info|. |net_error|
  70. // should contain the network error encountered when this proxy was tried, if
  71. // any. If this fallback is not because of a network error, then |OK| should
  72. // be passed in (eg. for reasons such as local policy). Returns true if there
  73. // is another server available in the list.
  74. bool Fallback(ProxyRetryInfoMap* proxy_retry_info,
  75. int net_error,
  76. const NetLogWithSource& net_log);
  77. // Updates |proxy_retry_info| to indicate that the first proxy in the list
  78. // is bad. This is distinct from Fallback(), above, to allow updating proxy
  79. // retry information without modifying a given transction's proxy list. Will
  80. // retry after |retry_delay| if positive, and will use the default proxy retry
  81. // duration otherwise. It may reconsider the proxy beforehand if |reconsider|
  82. // is true. Additionally updates |proxy_retry_info| with
  83. // |additional_proxies_to_bypass|. |net_error| should contain the network
  84. // error countered when this proxy was tried, or OK if the proxy retry info is
  85. // being updated for a non-network related reason (e.g. local policy).
  86. void UpdateRetryInfoOnFallback(
  87. ProxyRetryInfoMap* proxy_retry_info,
  88. base::TimeDelta retry_delay,
  89. bool reconsider,
  90. const std::vector<ProxyServer>& additional_proxies_to_bypass,
  91. int net_error,
  92. const NetLogWithSource& net_log) const;
  93. private:
  94. // Updates |proxy_retry_info| to indicate that the |proxy_to_retry| in
  95. // |proxies_| is bad for |retry_delay|, but may be reconsidered earlier if
  96. // |try_while_bad| is true. |net_error| should contain the network error
  97. // countered when this proxy was tried, or OK if the proxy retry info is
  98. // being updated for a non-network related reason (e.g. local policy).
  99. void AddProxyToRetryList(ProxyRetryInfoMap* proxy_retry_info,
  100. base::TimeDelta retry_delay,
  101. bool try_while_bad,
  102. const ProxyServer& proxy_to_retry,
  103. int net_error,
  104. const NetLogWithSource& net_log) const;
  105. // List of proxies.
  106. std::vector<ProxyServer> proxies_;
  107. };
  108. } // namespace net
  109. #endif // NET_PROXY_RESOLUTION_PROXY_LIST_H_