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