xref: /aosp_15_r20/external/cronet/net/proxy_resolution/win/windows_system_proxy_resolution_service.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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   void ClearBadProxiesCache() override;
59   const ProxyRetryInfoMap& proxy_retry_info() const override;
60   base::Value::Dict GetProxyNetLogValues() override;
61   [[nodiscard]] bool CastToConfiguredProxyResolutionService(
62       ConfiguredProxyResolutionService** configured_proxy_resolution_service)
63       override;
64 
65  private:
66   friend class WindowsSystemProxyResolutionRequest;
67 
68   WindowsSystemProxyResolutionService(
69       std::unique_ptr<WindowsSystemProxyResolver> windows_system_proxy_resolver,
70       NetLog* net_log);
71 
72   typedef std::set<
73       raw_ptr<WindowsSystemProxyResolutionRequest, SetExperimental>>
74       PendingRequests;
75 
76   [[nodiscard]] bool ContainsPendingRequest(
77       WindowsSystemProxyResolutionRequest* req);
78   void RemovePendingRequest(WindowsSystemProxyResolutionRequest* req);
79 
PendingRequestSizeForTesting()80   size_t PendingRequestSizeForTesting() const {
81     return pending_requests_.size();
82   }
83 
84   // Called when proxy resolution has completed (either synchronously or
85   // asynchronously). Handles logging the result, and cleaning out
86   // bad entries from the results list.
87   int DidFinishResolvingProxy(const GURL& url,
88                               const std::string& method,
89                               ProxyInfo* result,
90                               WinHttpStatus winhttp_status,
91                               const NetLogWithSource& net_log);
92 
93   // Map of the known bad proxies and the information about the retry time.
94   ProxyRetryInfoMap proxy_retry_info_;
95 
96   // Set of pending/in-progress requests.
97   PendingRequests pending_requests_;
98 
99   // This is used to launch cross-process proxy resolution requests. Individual
100   // WindowsSystemProxyResolutionRequest will use this to initiate proxy
101   // resolution.
102   std::unique_ptr<WindowsSystemProxyResolver> windows_system_proxy_resolver_;
103 
104   // This is the log for any generated events.
105   raw_ptr<NetLog> net_log_;
106 
107   SEQUENCE_CHECKER(sequence_checker_);
108 };
109 
110 }  // namespace net
111 
112 #endif  // NET_PROXY_RESOLUTION_WIN_WINDOWS_SYSTEM_PROXY_RESOLUTION_SERVICE_H_
113