Home | History | Annotate | Download | only in net
      1 // Copyright (c) 2012 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 CHROME_BROWSER_NET_PREF_PROXY_CONFIG_TRACKER_IMPL_H_
      6 #define CHROME_BROWSER_NET_PREF_PROXY_CONFIG_TRACKER_IMPL_H_
      7 
      8 #include "base/basictypes.h"
      9 #include "base/memory/ref_counted.h"
     10 #include "base/memory/scoped_ptr.h"
     11 #include "base/observer_list.h"
     12 #include "base/prefs/pref_change_registrar.h"
     13 #include "chrome/browser/net/pref_proxy_config_tracker.h"
     14 #include "chrome/browser/prefs/proxy_config_dictionary.h"
     15 #include "net/proxy/proxy_config.h"
     16 #include "net/proxy/proxy_config_service.h"
     17 
     18 class PrefService;
     19 class PrefRegistrySimple;
     20 
     21 namespace user_prefs {
     22 class PrefRegistrySyncable;
     23 }
     24 
     25 // A net::ProxyConfigService implementation that applies preference proxy
     26 // settings (pushed from PrefProxyConfigTrackerImpl) as overrides to the proxy
     27 // configuration determined by a baseline delegate ProxyConfigService on
     28 // non-ChromeOS platforms. ChromeOS has its own implementation of overrides in
     29 // chromeos::ProxyConfigServiceImpl.
     30 class ChromeProxyConfigService
     31     : public net::ProxyConfigService,
     32       public net::ProxyConfigService::Observer {
     33  public:
     34   // Takes ownership of the passed |base_service|.
     35   // GetLatestProxyConfig returns ConfigAvailability::CONFIG_PENDING until
     36   // UpdateProxyConfig has been called.
     37   explicit ChromeProxyConfigService(net::ProxyConfigService* base_service);
     38   virtual ~ChromeProxyConfigService();
     39 
     40   // ProxyConfigService implementation:
     41   virtual void AddObserver(
     42       net::ProxyConfigService::Observer* observer) OVERRIDE;
     43   virtual void RemoveObserver(
     44       net::ProxyConfigService::Observer* observer) OVERRIDE;
     45   virtual ConfigAvailability GetLatestProxyConfig(
     46       net::ProxyConfig* config) OVERRIDE;
     47   virtual void OnLazyPoll() OVERRIDE;
     48 
     49   // Method on IO thread that receives the preference proxy settings pushed from
     50   // PrefProxyConfigTrackerImpl.
     51   void UpdateProxyConfig(ProxyPrefs::ConfigState config_state,
     52                          const net::ProxyConfig& config);
     53 
     54  private:
     55   // ProxyConfigService::Observer implementation:
     56   virtual void OnProxyConfigChanged(const net::ProxyConfig& config,
     57                                     ConfigAvailability availability) OVERRIDE;
     58 
     59   // Makes sure that the observer registration with the base service is set up.
     60   void RegisterObserver();
     61 
     62   scoped_ptr<net::ProxyConfigService> base_service_;
     63   ObserverList<net::ProxyConfigService::Observer, true> observers_;
     64 
     65   // Tracks configuration state of |pref_config_|. |pref_config_| is valid only
     66   // if |pref_config_state_| is not CONFIG_UNSET.
     67   ProxyPrefs::ConfigState pref_config_state_;
     68 
     69   // Configuration as defined by prefs.
     70   net::ProxyConfig pref_config_;
     71 
     72   // Flag that indicates that a PrefProxyConfigTracker needs to inform us
     73   // about a proxy configuration before we may return any configuration.
     74   bool pref_config_read_pending_;
     75 
     76   // Indicates whether the base service registration is done.
     77   bool registered_observer_;
     78 
     79   DISALLOW_COPY_AND_ASSIGN(ChromeProxyConfigService);
     80 };
     81 
     82 // A class that tracks proxy preferences. It translates the configuration
     83 // to net::ProxyConfig and pushes the result over to the IO thread for
     84 // ChromeProxyConfigService::UpdateProxyConfig to use.
     85 class PrefProxyConfigTrackerImpl : public PrefProxyConfigTracker {
     86  public:
     87   explicit PrefProxyConfigTrackerImpl(PrefService* pref_service);
     88   virtual ~PrefProxyConfigTrackerImpl();
     89 
     90   // PrefProxyConfigTracker implementation:
     91   virtual scoped_ptr<net::ProxyConfigService> CreateTrackingProxyConfigService(
     92       scoped_ptr<net::ProxyConfigService> base_service) OVERRIDE;
     93 
     94   // Notifies the tracker that the pref service passed upon construction is
     95   // about to go away. This must be called from the UI thread.
     96   virtual void DetachFromPrefService() OVERRIDE;
     97 
     98   // Determines if |config_state| takes precedence regardless, which happens if
     99   // config is from policy or extension or other-precede.
    100   static bool PrefPrecedes(ProxyPrefs::ConfigState config_state);
    101 
    102   // Determines the proxy configuration that should take effect in the network
    103   // layer, based on prefs and system configurations.
    104   // |pref_state| refers to state of |pref_config|.
    105   // |system_availability| refers to availability of |system_config|.
    106   // |ignore_fallback_config| indicates if fallback config from prefs should
    107   // be ignored.
    108   // Returns effective |effective_config| and its state in
    109   // |effective_config_source|.
    110   static net::ProxyConfigService::ConfigAvailability GetEffectiveProxyConfig(
    111       ProxyPrefs::ConfigState pref_state,
    112       const net::ProxyConfig& pref_config,
    113       net::ProxyConfigService::ConfigAvailability system_availability,
    114       const net::ProxyConfig& system_config,
    115       bool ignore_fallback_config,
    116       ProxyPrefs::ConfigState* effective_config_state,
    117       net::ProxyConfig* effective_config);
    118 
    119   // Converts a ProxyConfigDictionary to net::ProxyConfig representation.
    120   // Returns true if the data from in the dictionary is valid, false otherwise.
    121   static bool PrefConfigToNetConfig(const ProxyConfigDictionary& proxy_dict,
    122                                     net::ProxyConfig* config);
    123 
    124   // Registers the proxy preferences. These are actually registered
    125   // the same way in local state and in user prefs.
    126   static void RegisterPrefs(PrefRegistrySimple* registry);
    127   static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
    128 
    129   // Creates a proxy configuration from proxy-related preferences of
    130   // |pref_service|. Configuration is stored in |config|, return value indicates
    131   // whether the configuration is valid.
    132   static ProxyPrefs::ConfigState ReadPrefConfig(const PrefService* pref_service,
    133                                                 net::ProxyConfig* config);
    134 
    135  protected:
    136   // Get the proxy configuration currently defined by preferences.
    137   // Status is indicated in the return value.
    138   // Writes the configuration to |config| unless the return value is
    139   // CONFIG_UNSET, in which case |config| and |config_source| are not touched.
    140   ProxyPrefs::ConfigState GetProxyConfig(net::ProxyConfig* config);
    141 
    142   // Called when there's a change in prefs proxy config.
    143   // Subclasses can extend it for changes in other sources of proxy config.
    144   virtual void OnProxyConfigChanged(ProxyPrefs::ConfigState config_state,
    145                                     const net::ProxyConfig& config);
    146 
    147   void OnProxyPrefChanged();
    148 
    149   const PrefService* prefs() const { return pref_service_; }
    150   bool update_pending() const { return update_pending_; }
    151 
    152  private:
    153   // Tracks configuration state. |pref_config_| is valid only if |config_state_|
    154   // is not CONFIG_UNSET.
    155   ProxyPrefs::ConfigState config_state_;
    156 
    157   // Configuration as defined by prefs.
    158   net::ProxyConfig pref_config_;
    159 
    160   PrefService* pref_service_;
    161   ChromeProxyConfigService* chrome_proxy_config_service_;  // Weak ptr.
    162   bool update_pending_;  // True if config has not been pushed to network stack.
    163   PrefChangeRegistrar proxy_prefs_;
    164 
    165   DISALLOW_COPY_AND_ASSIGN(PrefProxyConfigTrackerImpl);
    166 };
    167 
    168 #endif  // CHROME_BROWSER_NET_PREF_PROXY_CONFIG_TRACKER_IMPL_H_
    169