Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2010 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_SERVICE_H_ | |
| 6 #define CHROME_BROWSER_NET_PREF_PROXY_CONFIG_SERVICE_H_ | |
| 7 #pragma once | |
| 8 | |
| 9 #include "base/basictypes.h" | |
| 10 #include "base/observer_list.h" | |
| 11 #include "base/ref_counted.h" | |
| 12 #include "base/scoped_ptr.h" | |
| 13 #include "chrome/common/notification_observer.h" | |
| 14 #include "net/proxy/proxy_config.h" | |
| 15 #include "net/proxy/proxy_config_service.h" | |
| 16 | |
| 17 class PrefService; | |
| 18 class PrefSetObserver; | |
| 19 | |
| 20 // 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 // PrefProxyConfigService to use. | |
| 23 class PrefProxyConfigTracker | |
| 24 : public base::RefCountedThreadSafe<PrefProxyConfigTracker>, | |
| 25 public NotificationObserver { | |
| 26 public: | |
| 27 // Observer interface used to send out notifications on the IO thread about | |
| 28 // changes to the proxy configuration. | |
| 29 class ObserverInterface { | |
|
eroman
2010/11/30 02:58:54
nit: why not call this "Observer", which is more t
battre (please use the other)
2010/12/02 18:06:12
Done.
| |
| 30 public: | |
| 31 virtual ~ObserverInterface() {} | |
| 32 virtual void OnPrefProxyConfigChanged() = 0; | |
| 33 }; | |
| 34 | |
| 35 explicit PrefProxyConfigTracker(PrefService* pref_service); | |
| 36 virtual ~PrefProxyConfigTracker(); | |
| 37 | |
| 38 // Observer manipulation is only valid on the IO thread. | |
| 39 void AddObserver(ObserverInterface* observer); | |
| 40 void RemoveObserver(ObserverInterface* observer); | |
| 41 | |
| 42 // Get the proxy configuration currently defined by preferences. Writes the | |
| 43 // configuration to |config| and returns true on success. |config| is not | |
| 44 // touched and false is returned if there is no configuration defined. This | |
| 45 // must be called on the IO thread. | |
| 46 bool GetProxyConfig(net::ProxyConfig* config); | |
| 47 | |
| 48 // Notifies the tracker that the pref service passed upon construction is | |
| 49 // about to go away. This must be called from the UI thread. | |
| 50 void DetachFromPrefService(); | |
| 51 | |
| 52 private: | |
| 53 // NotificationObserver implementation: | |
| 54 virtual void Observe(NotificationType type, | |
| 55 const NotificationSource& source, | |
| 56 const NotificationDetails& details); | |
| 57 | |
| 58 // 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| | |
| 60 // indicates whether there is a preference proxy configuration defined, in | |
| 61 // which case this configuration is given by |config|. |config| is ignored if | |
| 62 // |valid| is false. | |
| 63 void InstallProxyConfig(const net::ProxyConfig& config, bool valid); | |
| 64 | |
| 65 // Creates a proxy configuration from proxy-related preferences. Configuration | |
| 66 // is stored in |config| and the return value indicates whether the | |
| 67 // configuration is valid. | |
| 68 bool ReadPrefConfig(net::ProxyConfig* config); | |
| 69 | |
| 70 // Configuration as defined by prefs. Only to be accessed from the IO thread | |
| 71 // (expect for construction). | |
|
eroman
2010/11/30 02:58:54
typo: "expect" --> "except".
battre (please use the other)
2010/12/02 18:06:12
Done.
| |
| 72 net::ProxyConfig pref_config_; | |
| 73 | |
| 74 // Whether |pref_config_| is valid. Only accessed from the IO thread. | |
| 75 bool valid_; | |
| 76 | |
| 77 // List of observers, accessed exclusively from the IO thread. | |
| 78 ObserverList<ObserverInterface, true> observers_; | |
| 79 | |
| 80 // Pref-related members that should only be accessed from the UI thread. | |
| 81 PrefService* pref_service_; | |
| 82 scoped_ptr<PrefSetObserver> proxy_prefs_observer_; | |
| 83 | |
| 84 DISALLOW_COPY_AND_ASSIGN(PrefProxyConfigTracker); | |
| 85 }; | |
| 86 | |
| 87 // A net::ProxyConfigService implementation that applies preference proxy | |
| 88 // settings as overrides to the proxy configuration determined by a baseline | |
| 89 // delegate ProxyConfigService. | |
| 90 class PrefProxyConfigService | |
| 91 : public net::ProxyConfigService, | |
| 92 public net::ProxyConfigService::Observer, | |
| 93 public PrefProxyConfigTracker::ObserverInterface { | |
| 94 public: | |
| 95 // Takes ownership of the passed |base_service|. | |
| 96 PrefProxyConfigService(PrefProxyConfigTracker* tracker, | |
| 97 net::ProxyConfigService* base_service); | |
| 98 virtual ~PrefProxyConfigService(); | |
| 99 | |
| 100 // ProxyConfigService implementation: | |
| 101 virtual void AddObserver(net::ProxyConfigService::Observer* observer); | |
| 102 virtual void RemoveObserver(net::ProxyConfigService::Observer* observer); | |
| 103 virtual bool GetLatestProxyConfig(net::ProxyConfig* config); | |
| 104 virtual void OnLazyPoll(); | |
| 105 | |
| 106 private: | |
| 107 // ProxyConfigService::Observer implementation: | |
| 108 virtual void OnProxyConfigChanged(const net::ProxyConfig& config); | |
| 109 | |
| 110 // PrefProxyConfigTracker::Observer implementation: | |
| 111 virtual void OnPrefProxyConfigChanged(); | |
| 112 | |
| 113 // Makes sure that the observer registrations with the base service and the | |
| 114 // tracker object are set up. | |
| 115 void RegisterObservers(); | |
| 116 | |
| 117 scoped_ptr<net::ProxyConfigService> base_service_; | |
| 118 ObserverList<net::ProxyConfigService::Observer, true> observers_; | |
| 119 scoped_refptr<PrefProxyConfigTracker> pref_config_tracker_; | |
| 120 | |
| 121 // Indicates whether the base service and tracker registrations are done. | |
| 122 bool registered_observers_; | |
| 123 | |
| 124 DISALLOW_COPY_AND_ASSIGN(PrefProxyConfigService); | |
| 125 }; | |
| 126 | |
| 127 #endif // CHROME_BROWSER_NET_PREF_PROXY_CONFIG_SERVICE_H_ | |
| OLD | NEW |