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