| 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/common/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 | |
| 22 // to net::ProxyConfig and proxies the result over to the IO thread for | |
| 23 // PrefProxyConfigService to use. | |
| 24 class PrefProxyConfigTracker | |
| 25 : public base::RefCountedThreadSafe<PrefProxyConfigTracker>, | |
| 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 | |
| 105 // A net::ProxyConfigService implementation that applies preference proxy | 21 // A net::ProxyConfigService implementation that applies preference proxy |
| 106 // settings as overrides to the proxy configuration determined by a baseline | 22 // settings (pushed from PrefProxyConfigTracker) as overrides to the proxy |
| 107 // delegate ProxyConfigService. | 23 // configuration determined by a baseline delegate ProxyConfigService on |
| 108 class PrefProxyConfigService | 24 // non-ChromeOS platforms. ChromeOS has its own implementation of overrides in |
| 25 // chromeos::ProxyConfigServiceImpl. |
| 26 class ChromeProxyConfigService |
| 109 : public net::ProxyConfigService, | 27 : public net::ProxyConfigService, |
| 110 public net::ProxyConfigService::Observer, | 28 public net::ProxyConfigService::Observer { |
| 111 public PrefProxyConfigTracker::Observer { | |
| 112 public: | 29 public: |
| 113 // Takes ownership of the passed |base_service|. | 30 // Takes ownership of the passed |base_service|. |
| 114 PrefProxyConfigService(PrefProxyConfigTracker* tracker, | 31 explicit ChromeProxyConfigService(net::ProxyConfigService* base_service); |
| 115 net::ProxyConfigService* base_service); | 32 virtual ~ChromeProxyConfigService(); |
| 116 virtual ~PrefProxyConfigService(); | |
| 117 | 33 |
| 118 // ProxyConfigService implementation: | 34 // ProxyConfigService implementation: |
| 119 virtual void AddObserver(net::ProxyConfigService::Observer* observer); | 35 virtual void AddObserver(net::ProxyConfigService::Observer* observer); |
| 120 virtual void RemoveObserver(net::ProxyConfigService::Observer* observer); | 36 virtual void RemoveObserver(net::ProxyConfigService::Observer* observer); |
| 121 virtual ConfigAvailability GetLatestProxyConfig(net::ProxyConfig* config); | 37 virtual ConfigAvailability GetLatestProxyConfig(net::ProxyConfig* config); |
| 122 virtual void OnLazyPoll(); | 38 virtual void OnLazyPoll(); |
| 123 | 39 |
| 124 static void RegisterPrefs(PrefService* user_prefs); | 40 // Method on IO thread that receives the preference proxy settings pushed from |
| 41 // PrefProxyConfigTracker. |
| 42 void UpdateProxyConfig(ProxyPrefs::ConfigState config_state, |
| 43 const net::ProxyConfig& config); |
| 125 | 44 |
| 126 private: | 45 private: |
| 127 // ProxyConfigService::Observer implementation: | 46 // ProxyConfigService::Observer implementation: |
| 128 virtual void OnProxyConfigChanged(const net::ProxyConfig& config, | 47 virtual void OnProxyConfigChanged(const net::ProxyConfig& config, |
| 129 ConfigAvailability availability); | 48 ConfigAvailability availability); |
| 130 | 49 |
| 131 // PrefProxyConfigTracker::Observer implementation: | 50 // Makes sure that the observer registration with the base service is set up. |
| 132 virtual void OnPrefProxyConfigChanged(); | 51 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 | 52 |
| 138 scoped_ptr<net::ProxyConfigService> base_service_; | 53 scoped_ptr<net::ProxyConfigService> base_service_; |
| 139 ObserverList<net::ProxyConfigService::Observer, true> observers_; | 54 ObserverList<net::ProxyConfigService::Observer, true> observers_; |
| 140 scoped_refptr<PrefProxyConfigTracker> pref_config_tracker_; | |
| 141 | 55 |
| 142 // Indicates whether the base service and tracker registrations are done. | 56 // Tracks configuration state of |pref_config_|. |pref_config_| is valid only |
| 143 bool registered_observers_; | 57 // if |pref_config_state_| is not CONFIG_UNSET. |
| 58 ProxyPrefs::ConfigState pref_config_state_; |
| 144 | 59 |
| 145 DISALLOW_COPY_AND_ASSIGN(PrefProxyConfigService); | 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); |
| 146 }; | 67 }; |
| 147 | 68 |
| 148 #endif // CHROME_BROWSER_NET_PREF_PROXY_CONFIG_SERVICE_H_ | 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 PrefProxyConfigTracker : public NotificationObserver { |
| 73 public: |
| 74 explicit PrefProxyConfigTracker(PrefService* pref_service); |
| 75 virtual ~PrefProxyConfigTracker(); |
| 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 preferene proxy. |
| 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 // NotificationObserver implementation: |
| 122 virtual void Observe(int type, |
| 123 const NotificationSource& source, |
| 124 const 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 const 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(PrefProxyConfigTracker); |
| 153 }; |
| 154 |
| 155 #endif // CHROME_BROWSER_NET_PREF_PROXY_CONFIG_TRACKER_H_ |
| OLD | NEW |