xref: /aosp_15_r20/external/cronet/net/proxy_resolution/win/proxy_config_service_win.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2011 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_PROXY_CONFIG_SERVICE_WIN_H_
6 #define NET_PROXY_RESOLUTION_WIN_PROXY_CONFIG_SERVICE_WIN_H_
7 
8 #include <windows.h>
9 
10 #include <winhttp.h>
11 
12 #include <memory>
13 #include <vector>
14 
15 #include "base/compiler_specific.h"
16 #include "base/gtest_prod_util.h"
17 #include "net/base/net_export.h"
18 #include "net/base/network_change_notifier.h"
19 #include "net/proxy_resolution/polling_proxy_config_service.h"
20 #include "net/proxy_resolution/proxy_config_with_annotation.h"
21 
22 namespace base::win {
23 class RegKey;
24 }  // namespace base::win
25 
26 namespace net {
27 
28 // Implementation of ProxyConfigService that retrieves the system proxy
29 // settings.
30 //
31 // It works by calling WinHttpGetIEProxyConfigForCurrentUser() to fetch the
32 // Internet Explorer proxy settings.
33 //
34 // We use two different strategies to notice when the configuration has
35 // changed:
36 //
37 // (1) Watch the internet explorer settings registry keys for changes. When
38 //     one of the registry keys pertaining to proxy settings has changed, we
39 //     call WinHttpGetIEProxyConfigForCurrentUser() again to read the
40 //     configuration's new value.
41 //
42 // (2) Do regular polling every 10 seconds during network activity to see if
43 //     WinHttpGetIEProxyConfigForCurrentUser() returns something different.
44 //
45 // Ideally strategy (1) should be sufficient to pick up all of the changes.
46 // However we still do the regular polling as a precaution in case the
47 // implementation details of  WinHttpGetIEProxyConfigForCurrentUser() ever
48 // change, or in case we got it wrong (and are not checking all possible
49 // registry dependencies).
50 class NET_EXPORT_PRIVATE ProxyConfigServiceWin
51     : public PollingProxyConfigService,
52       public NetworkChangeNotifier::NetworkChangeObserver {
53  public:
54   ProxyConfigServiceWin(const NetworkTrafficAnnotationTag& traffic_annotation);
55   ~ProxyConfigServiceWin() override;
56 
57   // Overrides a function from PollingProxyConfigService.
58   void AddObserver(Observer* observer) override;
59 
60   // NetworkChangeObserver implementation.
61   void OnNetworkChanged(NetworkChangeNotifier::ConnectionType type) override;
62 
63  private:
64   FRIEND_TEST_ALL_PREFIXES(ProxyConfigServiceWinTest, SetFromIEConfig);
65 
66   // Registers change observers on the registry keys relating to proxy settings.
67   void StartWatchingRegistryForChanges();
68 
69   // Creates a new key and appends it to |keys_to_watch_|. If the key fails to
70   // be created, it is not appended to the list and we return false.
71   bool AddKeyToWatchList(HKEY rootkey, const wchar_t* subkey);
72 
73   // This is called whenever one of the registry keys we are watching change.
74   void OnObjectSignaled(base::win::RegKey* key);
75 
76   static void GetCurrentProxyConfig(
77       const NetworkTrafficAnnotationTag traffic_annotation,
78       ProxyConfigWithAnnotation* config);
79 
80   // Set |config| using the proxy configuration values of |ie_config|.
81   static void SetFromIEConfig(
82       ProxyConfig* config,
83       const WINHTTP_CURRENT_USER_IE_PROXY_CONFIG& ie_config);
84 
85   std::vector<std::unique_ptr<base::win::RegKey>> keys_to_watch_;
86 };
87 
88 }  // namespace net
89 
90 #endif  // NET_PROXY_RESOLUTION_WIN_PROXY_CONFIG_SERVICE_WIN_H_
91