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