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