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