| 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_SERVICE_H_ |
| 6 #define CHROME_BROWSER_NET_PREF_PROXY_CONFIG_SERVICE_H_ | 6 #define CHROME_BROWSER_NET_PREF_PROXY_CONFIG_SERVICE_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 "content/common/notification_observer.h" | 14 #include "content/common/notification_observer.h" |
| 14 #include "net/proxy/proxy_config.h" | 15 #include "net/proxy/proxy_config.h" |
| 15 #include "net/proxy/proxy_config_service.h" | 16 #include "net/proxy/proxy_config_service.h" |
| 16 | 17 |
| 17 class PrefService; | 18 class PrefService; |
| 18 class PrefSetObserver; | 19 class PrefSetObserver; |
| 19 | 20 |
| 20 // A helper class that tracks proxy preferences. It translates the configuration | 21 // A helper class that tracks proxy preferences. It translates the configuration |
| 21 // to net::ProxyConfig and proxies the result over to the IO thread for | 22 // to net::ProxyConfig and proxies the result over to the IO thread for |
| 22 // PrefProxyConfigService to use. | 23 // PrefProxyConfigService to use. |
| 23 class PrefProxyConfigTracker | 24 class PrefProxyConfigTracker |
| 24 : public base::RefCountedThreadSafe<PrefProxyConfigTracker>, | 25 : public base::RefCountedThreadSafe<PrefProxyConfigTracker>, |
| 25 public NotificationObserver { | 26 public NotificationObserver { |
| 26 public: | 27 public: |
| 27 // Observer interface used to send out notifications on the IO thread about | 28 // Observer interface used to send out notifications on the IO thread about |
| 28 // changes to the proxy configuration. | 29 // changes to the proxy configuration. |
| 29 class Observer { | 30 class Observer { |
| 30 public: | 31 public: |
| 31 virtual ~Observer() {} | 32 virtual ~Observer() {} |
| 32 virtual void OnPrefProxyConfigChanged() = 0; | 33 virtual void OnPrefProxyConfigChanged() = 0; |
| 33 }; | 34 }; |
| 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 |
| 35 explicit PrefProxyConfigTracker(PrefService* pref_service); | 46 explicit PrefProxyConfigTracker(PrefService* pref_service); |
| 36 | 47 |
| 37 // Observer manipulation is only valid on the IO thread. | 48 // Observer manipulation is only valid on the IO thread. |
| 38 void AddObserver(Observer* observer); | 49 void AddObserver(Observer* observer); |
| 39 void RemoveObserver(Observer* observer); | 50 void RemoveObserver(Observer* observer); |
| 40 | 51 |
| 41 // Get the proxy configuration currently defined by preferences. Writes the | 52 // Get the proxy configuration currently defined by preferences. Status is |
| 42 // configuration to |config| and returns true on success. |config| is not | 53 // indicated in the return value. Writes the configuration to |config| unless |
| 43 // touched and false is returned if there is no configuration defined. This | 54 // the return value is CONFIG_UNSET, in which case |config| is not touched. |
| 44 // must be called on the IO thread. | 55 ConfigState GetProxyConfig(net::ProxyConfig* config); |
| 45 bool GetProxyConfig(net::ProxyConfig* config); | |
| 46 | 56 |
| 47 // Notifies the tracker that the pref service passed upon construction is | 57 // Notifies the tracker that the pref service passed upon construction is |
| 48 // about to go away. This must be called from the UI thread. | 58 // about to go away. This must be called from the UI thread. |
| 49 void DetachFromPrefService(); | 59 void DetachFromPrefService(); |
| 50 | 60 |
| 51 private: | 61 private: |
| 52 friend class base::RefCountedThreadSafe<PrefProxyConfigTracker>; | 62 friend class base::RefCountedThreadSafe<PrefProxyConfigTracker>; |
| 53 virtual ~PrefProxyConfigTracker(); | 63 virtual ~PrefProxyConfigTracker(); |
| 54 | 64 |
| 55 // NotificationObserver implementation: | 65 // NotificationObserver implementation: |
| 56 virtual void Observe(NotificationType type, | 66 virtual void Observe(NotificationType type, |
| 57 const NotificationSource& source, | 67 const NotificationSource& source, |
| 58 const NotificationDetails& details); | 68 const NotificationDetails& details); |
| 59 | 69 |
| 60 // Install a new configuration. This is invoked on the IO thread to update | 70 // Install a new configuration. This is invoked on the IO thread to update |
| 61 // the internal state after handling a pref change on the UI thread. |valid| | 71 // the internal state after handling a pref change on the UI thread. |
| 62 // indicates whether there is a preference proxy configuration defined, in | 72 // |config_state| indicates the new state we're switching to, and |config| is |
| 63 // which case this configuration is given by |config|. |config| is ignored if | 73 // the new preference-based proxy configuration if |config_state| is different |
| 64 // |valid| is false. | 74 // from CONFIG_UNSET. |
| 65 void InstallProxyConfig(const net::ProxyConfig& config, bool valid); | 75 void InstallProxyConfig(const net::ProxyConfig& config, ConfigState state); |
| 66 | 76 |
| 67 // Creates a proxy configuration from proxy-related preferences. Configuration | 77 // Creates a proxy configuration from proxy-related preferences. Configuration |
| 68 // is stored in |config| and the return value indicates whether the | 78 // is stored in |config| and the return value indicates whether the |
| 69 // configuration is valid. | 79 // configuration is valid. |
| 70 bool ReadPrefConfig(net::ProxyConfig* config); | 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); |
| 71 | 86 |
| 72 // Configuration as defined by prefs. Only to be accessed from the IO thread | 87 // Configuration as defined by prefs. Only to be accessed from the IO thread |
| 73 // (except for construction). | 88 // (except for construction). |
| 74 net::ProxyConfig pref_config_; | 89 net::ProxyConfig pref_config_; |
| 75 | 90 |
| 76 // Whether |pref_config_| is valid. Only accessed from the IO thread. | 91 // Tracks configuration state. |pref_config_| is valid only if |config_state_| |
| 77 bool valid_; | 92 // is not CONFIG_UNSET. |
| 93 ConfigState config_state_; |
| 78 | 94 |
| 79 // List of observers, accessed exclusively from the IO thread. | 95 // List of observers, accessed exclusively from the IO thread. |
| 80 ObserverList<Observer, true> observers_; | 96 ObserverList<Observer, true> observers_; |
| 81 | 97 |
| 82 // Pref-related members that should only be accessed from the UI thread. | 98 // Pref-related members that should only be accessed from the UI thread. |
| 83 PrefService* pref_service_; | 99 PrefService* pref_service_; |
| 84 scoped_ptr<PrefSetObserver> proxy_prefs_observer_; | 100 scoped_ptr<PrefSetObserver> proxy_prefs_observer_; |
| 85 | 101 |
| 86 DISALLOW_COPY_AND_ASSIGN(PrefProxyConfigTracker); | 102 DISALLOW_COPY_AND_ASSIGN(PrefProxyConfigTracker); |
| 87 }; | 103 }; |
| 88 | 104 |
| 89 // A net::ProxyConfigService implementation that applies preference proxy | 105 // A net::ProxyConfigService implementation that applies preference proxy |
| 90 // settings as overrides to the proxy configuration determined by a baseline | 106 // settings as overrides to the proxy configuration determined by a baseline |
| 91 // delegate ProxyConfigService. | 107 // delegate ProxyConfigService. |
| 92 class PrefProxyConfigService | 108 class PrefProxyConfigService |
| 93 : public net::ProxyConfigService, | 109 : public net::ProxyConfigService, |
| 94 public net::ProxyConfigService::Observer, | 110 public net::ProxyConfigService::Observer, |
| 95 public PrefProxyConfigTracker::Observer { | 111 public PrefProxyConfigTracker::Observer { |
| 96 public: | 112 public: |
| 97 // Takes ownership of the passed |base_service|. | 113 // Takes ownership of the passed |base_service|. |
| 98 PrefProxyConfigService(PrefProxyConfigTracker* tracker, | 114 PrefProxyConfigService(PrefProxyConfigTracker* tracker, |
| 99 net::ProxyConfigService* base_service); | 115 net::ProxyConfigService* base_service); |
| 100 virtual ~PrefProxyConfigService(); | 116 virtual ~PrefProxyConfigService(); |
| 101 | 117 |
| 102 // ProxyConfigService implementation: | 118 // ProxyConfigService implementation: |
| 103 virtual void AddObserver(net::ProxyConfigService::Observer* observer); | 119 virtual void AddObserver(net::ProxyConfigService::Observer* observer); |
| 104 virtual void RemoveObserver(net::ProxyConfigService::Observer* observer); | 120 virtual void RemoveObserver(net::ProxyConfigService::Observer* observer); |
| 105 virtual bool GetLatestProxyConfig(net::ProxyConfig* config); | 121 virtual ConfigAvailability GetLatestProxyConfig(net::ProxyConfig* config); |
| 106 virtual void OnLazyPoll(); | 122 virtual void OnLazyPoll(); |
| 107 | 123 |
| 108 static void RegisterPrefs(PrefService* user_prefs); | 124 static void RegisterPrefs(PrefService* user_prefs); |
| 109 | 125 |
| 110 private: | 126 private: |
| 111 // ProxyConfigService::Observer implementation: | 127 // ProxyConfigService::Observer implementation: |
| 112 virtual void OnProxyConfigChanged(const net::ProxyConfig& config); | 128 virtual void OnProxyConfigChanged(const net::ProxyConfig& config, |
| 129 ConfigAvailability availability); |
| 113 | 130 |
| 114 // PrefProxyConfigTracker::Observer implementation: | 131 // PrefProxyConfigTracker::Observer implementation: |
| 115 virtual void OnPrefProxyConfigChanged(); | 132 virtual void OnPrefProxyConfigChanged(); |
| 116 | 133 |
| 117 // Makes sure that the observer registrations with the base service and the | 134 // Makes sure that the observer registrations with the base service and the |
| 118 // tracker object are set up. | 135 // tracker object are set up. |
| 119 void RegisterObservers(); | 136 void RegisterObservers(); |
| 120 | 137 |
| 121 scoped_ptr<net::ProxyConfigService> base_service_; | 138 scoped_ptr<net::ProxyConfigService> base_service_; |
| 122 ObserverList<net::ProxyConfigService::Observer, true> observers_; | 139 ObserverList<net::ProxyConfigService::Observer, true> observers_; |
| 123 scoped_refptr<PrefProxyConfigTracker> pref_config_tracker_; | 140 scoped_refptr<PrefProxyConfigTracker> pref_config_tracker_; |
| 124 | 141 |
| 125 // Indicates whether the base service and tracker registrations are done. | 142 // Indicates whether the base service and tracker registrations are done. |
| 126 bool registered_observers_; | 143 bool registered_observers_; |
| 127 | 144 |
| 128 DISALLOW_COPY_AND_ASSIGN(PrefProxyConfigService); | 145 DISALLOW_COPY_AND_ASSIGN(PrefProxyConfigService); |
| 129 }; | 146 }; |
| 130 | 147 |
| 131 #endif // CHROME_BROWSER_NET_PREF_PROXY_CONFIG_SERVICE_H_ | 148 #endif // CHROME_BROWSER_NET_PREF_PROXY_CONFIG_SERVICE_H_ |
| OLD | NEW |