1 // Copyright (c) 2011 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 5 #ifndef NET_PROXY_RESOLUTION_PROXY_CONFIG_SERVICE_WIN_H_ 6 #define NET_PROXY_RESOLUTION_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 "base/strings/string16.h" 17 #include "net/base/net_export.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 { 22 namespace win { 23 class RegKey; 24 } 25 } // namespace base. 26 27 namespace net { 28 29 // Implementation of ProxyConfigService that retrieves the system proxy 30 // settings. 31 // 32 // It works by calling WinHttpGetIEProxyConfigForCurrentUser() to fetch the 33 // Internet Explorer proxy settings. 34 // 35 // We use two different strategies to notice when the configuration has 36 // changed: 37 // 38 // (1) Watch the internet explorer settings registry keys for changes. When 39 // one of the registry keys pertaining to proxy settings has changed, we 40 // call WinHttpGetIEProxyConfigForCurrentUser() again to read the 41 // configuration's new value. 42 // 43 // (2) Do regular polling every 10 seconds during network activity to see if 44 // WinHttpGetIEProxyConfigForCurrentUser() returns something different. 45 // 46 // Ideally strategy (1) should be sufficient to pick up all of the changes. 47 // However we still do the regular polling as a precaution in case the 48 // implementation details of WinHttpGetIEProxyConfigForCurrentUser() ever 49 // change, or in case we got it wrong (and are not checking all possible 50 // registry dependencies). 51 class NET_EXPORT_PRIVATE ProxyConfigServiceWin 52 : public PollingProxyConfigService { 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 private: 61 FRIEND_TEST_ALL_PREFIXES(ProxyConfigServiceWinTest, SetFromIEConfig); 62 63 // Registers change observers on the registry keys relating to proxy settings. 64 void StartWatchingRegistryForChanges(); 65 66 // Creates a new key and appends it to |keys_to_watch_|. If the key fails to 67 // be created, it is not appended to the list and we return false. 68 bool AddKeyToWatchList(HKEY rootkey, const base::char16* subkey); 69 70 // This is called whenever one of the registry keys we are watching change. 71 void OnObjectSignaled(base::win::RegKey* key); 72 73 static void GetCurrentProxyConfig( 74 const NetworkTrafficAnnotationTag traffic_annotation, 75 ProxyConfigWithAnnotation* config); 76 77 // Set |config| using the proxy configuration values of |ie_config|. 78 static void SetFromIEConfig( 79 ProxyConfig* config, 80 const WINHTTP_CURRENT_USER_IE_PROXY_CONFIG& ie_config); 81 82 std::vector<std::unique_ptr<base::win::RegKey>> keys_to_watch_; 83 }; 84 85 } // namespace net 86 87 #endif // NET_PROXY_RESOLUTION_PROXY_CONFIG_SERVICE_WIN_H_ 88