QUIC - wait for disk cache to load server config if the server is among
[chromium-blink-merge.git] / net / proxy / proxy_config_service_linux.h
blob311f81f2f5527c2314e5ac962a920ef2a86a68bf
1 // Copyright (c) 2012 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.
5 #ifndef NET_PROXY_PROXY_CONFIG_SERVICE_LINUX_H_
6 #define NET_PROXY_PROXY_CONFIG_SERVICE_LINUX_H_
8 #include <string>
9 #include <vector>
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/environment.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/observer_list.h"
17 #include "net/base/net_export.h"
18 #include "net/proxy/proxy_config.h"
19 #include "net/proxy/proxy_config_service.h"
20 #include "net/proxy/proxy_server.h"
22 namespace base {
23 class SingleThreadTaskRunner;
24 } // namespace base
26 namespace net {
28 // Implementation of ProxyConfigService that retrieves the system proxy
29 // settings from environment variables, gconf, gsettings, or kioslaverc (KDE).
30 class NET_EXPORT_PRIVATE ProxyConfigServiceLinux : public ProxyConfigService {
31 public:
33 // Forward declaration of Delegate.
34 class Delegate;
36 class SettingGetter {
37 public:
38 // Buffer size used in some implementations of this class when reading
39 // files. Defined here so unit tests can construct worst-case inputs.
40 static const size_t BUFFER_SIZE = 512;
42 SettingGetter() {}
43 virtual ~SettingGetter() {}
45 // Initializes the class: obtains a gconf/gsettings client, or simulates
46 // one, in the concrete implementations. Returns true on success. Must be
47 // called before using other methods, and should be called on the thread
48 // running the glib main loop.
49 // One of |glib_task_runner| and |file_task_runner| will be
50 // used for gconf/gsettings calls or reading necessary files, depending on
51 // the implementation.
52 virtual bool Init(
53 const scoped_refptr<base::SingleThreadTaskRunner>& glib_task_runner,
54 const scoped_refptr<base::SingleThreadTaskRunner>&
55 file_task_runner) = 0;
57 // Releases the gconf/gsettings client, which clears cached directories and
58 // stops notifications.
59 virtual void ShutDown() = 0;
61 // Requests notification of gconf/gsettings changes for proxy
62 // settings. Returns true on success.
63 virtual bool SetUpNotifications(Delegate* delegate) = 0;
65 // Returns the message loop for the thread on which this object
66 // handles notifications, and also on which it must be destroyed.
67 // Returns NULL if it does not matter.
68 virtual const scoped_refptr<base::SingleThreadTaskRunner>&
69 GetNotificationTaskRunner() = 0;
71 // Returns the source of proxy settings.
72 virtual ProxyConfigSource GetConfigSource() = 0;
74 // These are all the values that can be fetched. We used to just use the
75 // corresponding paths in gconf for these, but gconf is now obsolete and
76 // in the future we'll be using mostly gsettings/kioslaverc so we
77 // enumerate them instead to avoid unnecessary string operations.
78 enum StringSetting {
79 PROXY_MODE,
80 PROXY_AUTOCONF_URL,
81 PROXY_HTTP_HOST,
82 PROXY_HTTPS_HOST,
83 PROXY_FTP_HOST,
84 PROXY_SOCKS_HOST,
86 enum BoolSetting {
87 PROXY_USE_HTTP_PROXY,
88 PROXY_USE_SAME_PROXY,
89 PROXY_USE_AUTHENTICATION,
91 enum IntSetting {
92 PROXY_HTTP_PORT,
93 PROXY_HTTPS_PORT,
94 PROXY_FTP_PORT,
95 PROXY_SOCKS_PORT,
97 enum StringListSetting {
98 PROXY_IGNORE_HOSTS,
101 // Given a PROXY_*_HOST value, return the corresponding PROXY_*_PORT value.
102 static IntSetting HostSettingToPortSetting(StringSetting host) {
103 switch (host) {
104 case PROXY_HTTP_HOST:
105 return PROXY_HTTP_PORT;
106 case PROXY_HTTPS_HOST:
107 return PROXY_HTTPS_PORT;
108 case PROXY_FTP_HOST:
109 return PROXY_FTP_PORT;
110 case PROXY_SOCKS_HOST:
111 return PROXY_SOCKS_PORT;
112 default:
113 NOTREACHED();
114 return PROXY_HTTP_PORT; // Placate compiler.
118 // Gets a string type value from the data source and stores it in
119 // |*result|. Returns false if the key is unset or on error. Must only be
120 // called after a successful call to Init(), and not after a failed call
121 // to SetUpNotifications() or after calling Release().
122 virtual bool GetString(StringSetting key, std::string* result) = 0;
123 // Same thing for a bool typed value.
124 virtual bool GetBool(BoolSetting key, bool* result) = 0;
125 // Same for an int typed value.
126 virtual bool GetInt(IntSetting key, int* result) = 0;
127 // And for a string list.
128 virtual bool GetStringList(StringListSetting key,
129 std::vector<std::string>* result) = 0;
131 // Returns true if the bypass list should be interpreted as a proxy
132 // whitelist rather than blacklist. (This is KDE-specific.)
133 virtual bool BypassListIsReversed() = 0;
135 // Returns true if the bypass rules should be interpreted as
136 // suffix-matching rules.
137 virtual bool MatchHostsUsingSuffixMatching() = 0;
139 private:
140 DISALLOW_COPY_AND_ASSIGN(SettingGetter);
143 // ProxyConfigServiceLinux is created on the UI thread, and
144 // SetUpAndFetchInitialConfig() is immediately called to synchronously
145 // fetch the original configuration and set up change notifications on
146 // the UI thread.
148 // Past that point, it is accessed periodically through the
149 // ProxyConfigService interface (GetLatestProxyConfig, AddObserver,
150 // RemoveObserver) from the IO thread.
152 // Setting change notification callbacks can occur at any time and are
153 // run on either the UI thread (gconf/gsettings) or the file thread
154 // (KDE). The new settings are fetched on that thread, and the resulting
155 // proxy config is posted to the IO thread through
156 // Delegate::SetNewProxyConfig(). We then notify observers on the IO
157 // thread of the configuration change.
159 // ProxyConfigServiceLinux is deleted from the IO thread.
161 // The substance of the ProxyConfigServiceLinux implementation is
162 // wrapped in the Delegate ref counted class. On deleting the
163 // ProxyConfigServiceLinux, Delegate::OnDestroy() is posted to either
164 // the UI thread (gconf/gsettings) or the file thread (KDE) where change
165 // notifications will be safely stopped before releasing Delegate.
167 class Delegate : public base::RefCountedThreadSafe<Delegate> {
168 public:
169 // Constructor receives env var getter implementation to use, and
170 // takes ownership of it. This is the normal constructor.
171 explicit Delegate(base::Environment* env_var_getter);
172 // Constructor receives setting and env var getter implementations
173 // to use, and takes ownership of them. Used for testing.
174 Delegate(base::Environment* env_var_getter, SettingGetter* setting_getter);
176 // Synchronously obtains the proxy configuration. If gconf,
177 // gsettings, or kioslaverc are used, also enables notifications for
178 // setting changes. gconf/gsettings must only be accessed from the
179 // thread running the default glib main loop, and so this method
180 // must be called from the UI thread. The message loop for the IO
181 // thread is specified so that notifications can post tasks to it
182 // (and for assertions). The message loop for the file thread is
183 // used to read any files needed to determine proxy settings.
184 void SetUpAndFetchInitialConfig(
185 const scoped_refptr<base::SingleThreadTaskRunner>& glib_task_runner,
186 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner,
187 const scoped_refptr<base::SingleThreadTaskRunner>& file_task_runner);
189 // Handler for setting change notifications: fetches a new proxy
190 // configuration from settings, and if this config is different
191 // than what we had before, posts a task to have it stored in
192 // cached_config_.
193 // Left public for simplicity.
194 void OnCheckProxyConfigSettings();
196 // Called from IO thread.
197 void AddObserver(Observer* observer);
198 void RemoveObserver(Observer* observer);
199 ProxyConfigService::ConfigAvailability GetLatestProxyConfig(
200 ProxyConfig* config);
202 // Posts a call to OnDestroy() to the UI or FILE thread, depending on the
203 // setting getter in use. Called from ProxyConfigServiceLinux's destructor.
204 void PostDestroyTask();
205 // Safely stops change notifications. Posted to either the UI or FILE
206 // thread, depending on the setting getter in use.
207 void OnDestroy();
209 private:
210 friend class base::RefCountedThreadSafe<Delegate>;
212 ~Delegate();
214 // Obtains an environment variable's value. Parses a proxy server
215 // specification from it and puts it in result. Returns true if the
216 // requested variable is defined and the value valid.
217 bool GetProxyFromEnvVarForScheme(const char* variable,
218 ProxyServer::Scheme scheme,
219 ProxyServer* result_server);
220 // As above but with scheme set to HTTP, for convenience.
221 bool GetProxyFromEnvVar(const char* variable, ProxyServer* result_server);
222 // Fills proxy config from environment variables. Returns true if
223 // variables were found and the configuration is valid.
224 bool GetConfigFromEnv(ProxyConfig* config);
226 // Obtains host and port config settings and parses a proxy server
227 // specification from it and puts it in result. Returns true if the
228 // requested variable is defined and the value valid.
229 bool GetProxyFromSettings(SettingGetter::StringSetting host_key,
230 ProxyServer* result_server);
231 // Fills proxy config from settings. Returns true if settings were found
232 // and the configuration is valid.
233 bool GetConfigFromSettings(ProxyConfig* config);
235 // This method is posted from the UI thread to the IO thread to
236 // carry the new config information.
237 void SetNewProxyConfig(const ProxyConfig& new_config);
239 // This method is run on the getter's notification thread.
240 void SetUpNotifications();
242 scoped_ptr<base::Environment> env_var_getter_;
243 scoped_ptr<SettingGetter> setting_getter_;
245 // Cached proxy configuration, to be returned by
246 // GetLatestProxyConfig. Initially populated from the UI thread, but
247 // afterwards only accessed from the IO thread.
248 ProxyConfig cached_config_;
250 // A copy kept on the UI thread of the last seen proxy config, so as
251 // to avoid posting a call to SetNewProxyConfig when we get a
252 // notification but the config has not actually changed.
253 ProxyConfig reference_config_;
255 // The task runner for the glib thread, aka main browser thread. This thread
256 // is where we run the glib main loop (see
257 // base/message_loop/message_pump_glib.h). It is the glib default loop in
258 // the sense that it runs the glib default context: as in the context where
259 // sources are added by g_timeout_add and g_idle_add, and returned by
260 // g_main_context_default. gconf uses glib timeouts and idles and possibly
261 // other callbacks that will all be dispatched on this thread. Since gconf
262 // is not thread safe, any use of gconf must be done on the thread running
263 // this loop.
264 scoped_refptr<base::SingleThreadTaskRunner> glib_task_runner_;
265 // Task runner for the IO thread. GetLatestProxyConfig() is called from
266 // the thread running this loop.
267 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
269 base::ObserverList<Observer> observers_;
271 DISALLOW_COPY_AND_ASSIGN(Delegate);
274 // Thin wrapper shell around Delegate.
276 // Usual constructor
277 ProxyConfigServiceLinux();
278 // For testing: take alternate setting and env var getter implementations.
279 explicit ProxyConfigServiceLinux(base::Environment* env_var_getter);
280 ProxyConfigServiceLinux(base::Environment* env_var_getter,
281 SettingGetter* setting_getter);
283 ~ProxyConfigServiceLinux() override;
285 void SetupAndFetchInitialConfig(
286 const scoped_refptr<base::SingleThreadTaskRunner>& glib_task_runner,
287 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner,
288 const scoped_refptr<base::SingleThreadTaskRunner>& file_task_runner) {
289 delegate_->SetUpAndFetchInitialConfig(glib_task_runner,
290 io_task_runner,
291 file_task_runner);
293 void OnCheckProxyConfigSettings() {
294 delegate_->OnCheckProxyConfigSettings();
297 // ProxyConfigService methods:
298 // Called from IO thread.
299 void AddObserver(Observer* observer) override;
300 void RemoveObserver(Observer* observer) override;
301 ProxyConfigService::ConfigAvailability GetLatestProxyConfig(
302 ProxyConfig* config) override;
304 private:
305 scoped_refptr<Delegate> delegate_;
307 DISALLOW_COPY_AND_ASSIGN(ProxyConfigServiceLinux);
310 } // namespace net
312 #endif // NET_PROXY_PROXY_CONFIG_SERVICE_LINUX_H_