1 // Copyright 2020 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_WIN_WINDOWS_SYSTEM_PROXY_RESOLUTION_SERVICE_H_ 6 #define NET_PROXY_RESOLUTION_WIN_WINDOWS_SYSTEM_PROXY_RESOLUTION_SERVICE_H_ 7 8 #include "base/memory/raw_ptr.h" 9 #include "net/proxy_resolution/proxy_resolution_service.h" 10 11 #include <memory> 12 #include <set> 13 #include <string> 14 15 #include "base/sequence_checker.h" 16 #include "net/base/net_export.h" 17 #include "net/proxy_resolution/win/winhttp_status.h" 18 19 namespace net { 20 21 class NetLog; 22 class WindowsSystemProxyResolutionRequest; 23 class WindowsSystemProxyResolver; 24 25 // This class decides which proxy server(s) to use for a particular URL request. 26 // It does NOT support passing in fetched proxy configurations. Instead, it 27 // relies entirely on WinHttp APIs to determine the proxy that should be used 28 // for each network request. 29 class NET_EXPORT WindowsSystemProxyResolutionService 30 : public ProxyResolutionService { 31 public: 32 [[nodiscard]] static bool IsSupported(); 33 34 // Creates a WindowsSystemProxyResolutionService or returns nullptr if the 35 // runtime dependencies are not satisfied. 36 static std::unique_ptr<WindowsSystemProxyResolutionService> Create( 37 std::unique_ptr<WindowsSystemProxyResolver> windows_system_proxy_resolver, 38 NetLog* net_log); 39 40 WindowsSystemProxyResolutionService( 41 const WindowsSystemProxyResolutionService&) = delete; 42 WindowsSystemProxyResolutionService& operator=( 43 const WindowsSystemProxyResolutionService&) = delete; 44 45 ~WindowsSystemProxyResolutionService() override; 46 47 // ProxyResolutionService implementation 48 int ResolveProxy(const GURL& url, 49 const std::string& method, 50 const NetworkAnonymizationKey& network_anonymization_key, 51 ProxyInfo* results, 52 CompletionOnceCallback callback, 53 std::unique_ptr<ProxyResolutionRequest>* request, 54 const NetLogWithSource& net_log) override; 55 void ReportSuccess(const ProxyInfo& proxy_info) override; 56 void SetProxyDelegate(ProxyDelegate* delegate) override; 57 void OnShutdown() override; 58 bool MarkProxiesAsBadUntil( 59 const ProxyInfo& results, 60 base::TimeDelta retry_delay, 61 const std::vector<ProxyServer>& additional_bad_proxies, 62 const NetLogWithSource& net_log) override; 63 void ClearBadProxiesCache() override; 64 const ProxyRetryInfoMap& proxy_retry_info() const override; 65 base::Value::Dict GetProxyNetLogValues() override; 66 [[nodiscard]] bool CastToConfiguredProxyResolutionService( 67 ConfiguredProxyResolutionService** configured_proxy_resolution_service) 68 override; 69 70 private: 71 friend class WindowsSystemProxyResolutionRequest; 72 73 WindowsSystemProxyResolutionService( 74 std::unique_ptr<WindowsSystemProxyResolver> windows_system_proxy_resolver, 75 NetLog* net_log); 76 77 typedef std::set<WindowsSystemProxyResolutionRequest*> PendingRequests; 78 79 [[nodiscard]] bool ContainsPendingRequest( 80 WindowsSystemProxyResolutionRequest* req); 81 void RemovePendingRequest(WindowsSystemProxyResolutionRequest* req); 82 PendingRequestSizeForTesting()83 size_t PendingRequestSizeForTesting() const { 84 return pending_requests_.size(); 85 } 86 87 // Called when proxy resolution has completed (either synchronously or 88 // asynchronously). Handles logging the result, and cleaning out 89 // bad entries from the results list. 90 int DidFinishResolvingProxy(const GURL& url, 91 const std::string& method, 92 ProxyInfo* result, 93 WinHttpStatus winhttp_status, 94 const NetLogWithSource& net_log); 95 96 // Map of the known bad proxies and the information about the retry time. 97 ProxyRetryInfoMap proxy_retry_info_; 98 99 // Set of pending/in-progress requests. 100 PendingRequests pending_requests_; 101 102 // This is used to launch cross-process proxy resolution requests. Individual 103 // WindowsSystemProxyResolutionRequest will use this to initiate proxy 104 // resolution. 105 std::unique_ptr<WindowsSystemProxyResolver> windows_system_proxy_resolver_; 106 107 // This is the log for any generated events. 108 raw_ptr<NetLog> net_log_; 109 110 SEQUENCE_CHECKER(sequence_checker_); 111 }; 112 113 } // namespace net 114 115 #endif // NET_PROXY_RESOLUTION_WIN_WINDOWS_SYSTEM_PROXY_RESOLUTION_SERVICE_H_ 116