Extract SIGPIPE ignoring code to a common place.
[chromium-blink-merge.git] / net / proxy / proxy_config_service_linux.h
blob8d46d998c6396b6599a83779dc29f4b3d3914d4d
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 class MessageLoopForIO;
24 namespace base {
25 class SingleThreadTaskRunner;
26 } // namespace base
28 namespace net {
30 // Implementation of ProxyConfigService that retrieves the system proxy
31 // settings from environment variables, gconf, gsettings, or kioslaverc (KDE).
32 class NET_EXPORT_PRIVATE ProxyConfigServiceLinux : public ProxyConfigService {
33 public:
35 // Forward declaration of Delegate.
36 class Delegate;
38 class SettingGetter {
39 public:
40 // Buffer size used in some implementations of this class when reading
41 // files. Defined here so unit tests can construct worst-case inputs.
42 static const size_t BUFFER_SIZE = 512;
44 SettingGetter() {}
45 virtual ~SettingGetter() {}
47 // Initializes the class: obtains a gconf/gsettings client, or simulates
48 // one, in the concrete implementations. Returns true on success. Must be
49 // called before using other methods, and should be called on the thread
50 // running the glib main loop.
51 // One of |glib_thread_task_runner| and |file_loop| will be used for
52 // gconf/gsettings calls or reading necessary files, depending on the
53 // implementation.
54 virtual bool Init(base::SingleThreadTaskRunner* glib_thread_task_runner,
55 MessageLoopForIO* file_loop) = 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 base::SingleThreadTaskRunner* GetNotificationTaskRunner() = 0;
70 // Returns the source of proxy settings.
71 virtual ProxyConfigSource GetConfigSource() = 0;
73 // These are all the values that can be fetched. We used to just use the
74 // corresponding paths in gconf for these, but gconf is now obsolete and
75 // in the future we'll be using mostly gsettings/kioslaverc so we
76 // enumerate them instead to avoid unnecessary string operations.
77 enum StringSetting {
78 PROXY_MODE,
79 PROXY_AUTOCONF_URL,
80 PROXY_HTTP_HOST,
81 PROXY_HTTPS_HOST,
82 PROXY_FTP_HOST,
83 PROXY_SOCKS_HOST,
85 enum BoolSetting {
86 PROXY_USE_HTTP_PROXY,
87 PROXY_USE_SAME_PROXY,
88 PROXY_USE_AUTHENTICATION,
90 enum IntSetting {
91 PROXY_HTTP_PORT,
92 PROXY_HTTPS_PORT,
93 PROXY_FTP_PORT,
94 PROXY_SOCKS_PORT,
96 enum StringListSetting {
97 PROXY_IGNORE_HOSTS,
100 // Given a PROXY_*_HOST value, return the corresponding PROXY_*_PORT value.
101 static IntSetting HostSettingToPortSetting(StringSetting host) {
102 switch (host) {
103 case PROXY_HTTP_HOST:
104 return PROXY_HTTP_PORT;
105 case PROXY_HTTPS_HOST:
106 return PROXY_HTTPS_PORT;
107 case PROXY_FTP_HOST:
108 return PROXY_FTP_PORT;
109 case PROXY_SOCKS_HOST:
110 return PROXY_SOCKS_PORT;
111 default:
112 NOTREACHED();
113 return PROXY_HTTP_PORT; // Placate compiler.
117 // Gets a string type value from the data source and stores it in
118 // |*result|. Returns false if the key is unset or on error. Must only be
119 // called after a successful call to Init(), and not after a failed call
120 // to SetUpNotifications() or after calling Release().
121 virtual bool GetString(StringSetting key, std::string* result) = 0;
122 // Same thing for a bool typed value.
123 virtual bool GetBool(BoolSetting key, bool* result) = 0;
124 // Same for an int typed value.
125 virtual bool GetInt(IntSetting key, int* result) = 0;
126 // And for a string list.
127 virtual bool GetStringList(StringListSetting key,
128 std::vector<std::string>* result) = 0;
130 // Returns true if the bypass list should be interpreted as a proxy
131 // whitelist rather than blacklist. (This is KDE-specific.)
132 virtual bool BypassListIsReversed() = 0;
134 // Returns true if the bypass rules should be interpreted as
135 // suffix-matching rules.
136 virtual bool MatchHostsUsingSuffixMatching() = 0;
138 private:
139 DISALLOW_COPY_AND_ASSIGN(SettingGetter);
142 // ProxyConfigServiceLinux is created on the UI thread, and
143 // SetUpAndFetchInitialConfig() is immediately called to synchronously
144 // fetch the original configuration and set up change notifications on
145 // the UI thread.
147 // Past that point, it is accessed periodically through the
148 // ProxyConfigService interface (GetLatestProxyConfig, AddObserver,
149 // RemoveObserver) from the IO thread.
151 // Setting change notification callbacks can occur at any time and are
152 // run on either the UI thread (gconf/gsettings) or the file thread
153 // (KDE). The new settings are fetched on that thread, and the resulting
154 // proxy config is posted to the IO thread through
155 // Delegate::SetNewProxyConfig(). We then notify observers on the IO
156 // thread of the configuration change.
158 // ProxyConfigServiceLinux is deleted from the IO thread.
160 // The substance of the ProxyConfigServiceLinux implementation is
161 // wrapped in the Delegate ref counted class. On deleting the
162 // ProxyConfigServiceLinux, Delegate::OnDestroy() is posted to either
163 // the UI thread (gconf/gsettings) or the file thread (KDE) where change
164 // notifications will be safely stopped before releasing Delegate.
166 class Delegate : public base::RefCountedThreadSafe<Delegate> {
167 public:
168 // Constructor receives env var getter implementation to use, and
169 // takes ownership of it. This is the normal constructor.
170 explicit Delegate(base::Environment* env_var_getter);
171 // Constructor receives setting and env var getter implementations
172 // to use, and takes ownership of them. Used for testing.
173 Delegate(base::Environment* env_var_getter, SettingGetter* setting_getter);
175 // Synchronously obtains the proxy configuration. If gconf,
176 // gsettings, or kioslaverc are used, also enables notifications for
177 // setting changes. gconf/gsettings must only be accessed from the
178 // thread running the default glib main loop, and so this method
179 // must be called from the UI thread. The message loop for the IO
180 // thread is specified so that notifications can post tasks to it
181 // (and for assertions). The message loop for the file thread is
182 // used to read any files needed to determine proxy settings.
183 void SetUpAndFetchInitialConfig(
184 base::SingleThreadTaskRunner* glib_thread_task_runner,
185 base::SingleThreadTaskRunner* io_thread_task_runner,
186 MessageLoopForIO* file_loop);
188 // Handler for setting change notifications: fetches a new proxy
189 // configuration from settings, and if this config is different
190 // than what we had before, posts a task to have it stored in
191 // cached_config_.
192 // Left public for simplicity.
193 void OnCheckProxyConfigSettings();
195 // Called from IO thread.
196 void AddObserver(Observer* observer);
197 void RemoveObserver(Observer* observer);
198 ProxyConfigService::ConfigAvailability GetLatestProxyConfig(
199 ProxyConfig* config);
201 // Posts a call to OnDestroy() to the UI or FILE thread, depending on the
202 // setting getter in use. Called from ProxyConfigServiceLinux's destructor.
203 void PostDestroyTask();
204 // Safely stops change notifications. Posted to either the UI or FILE
205 // thread, depending on the setting getter in use.
206 void OnDestroy();
208 private:
209 friend class base::RefCountedThreadSafe<Delegate>;
211 ~Delegate();
213 // Obtains an environment variable's value. Parses a proxy server
214 // specification from it and puts it in result. Returns true if the
215 // requested variable is defined and the value valid.
216 bool GetProxyFromEnvVarForScheme(const char* variable,
217 ProxyServer::Scheme scheme,
218 ProxyServer* result_server);
219 // As above but with scheme set to HTTP, for convenience.
220 bool GetProxyFromEnvVar(const char* variable, ProxyServer* result_server);
221 // Fills proxy config from environment variables. Returns true if
222 // variables were found and the configuration is valid.
223 bool GetConfigFromEnv(ProxyConfig* config);
225 // Obtains host and port config settings and parses a proxy server
226 // specification from it and puts it in result. Returns true if the
227 // requested variable is defined and the value valid.
228 bool GetProxyFromSettings(SettingGetter::StringSetting host_key,
229 ProxyServer* result_server);
230 // Fills proxy config from settings. Returns true if settings were found
231 // and the configuration is valid.
232 bool GetConfigFromSettings(ProxyConfig* config);
234 // This method is posted from the UI thread to the IO thread to
235 // carry the new config information.
236 void SetNewProxyConfig(const ProxyConfig& new_config);
238 // This method is run on the getter's notification thread.
239 void SetUpNotifications();
241 scoped_ptr<base::Environment> env_var_getter_;
242 scoped_ptr<SettingGetter> setting_getter_;
244 // Cached proxy configuration, to be returned by
245 // GetLatestProxyConfig. Initially populated from the UI thread, but
246 // afterwards only accessed from the IO thread.
247 ProxyConfig cached_config_;
249 // A copy kept on the UI thread of the last seen proxy config, so as
250 // to avoid posting a call to SetNewProxyConfig when we get a
251 // notification but the config has not actually changed.
252 ProxyConfig reference_config_;
254 // The task runner for the glib thread, aka main browser thread. This thread
255 // is where we run the glib main loop (see base/message_pump_glib.h). It is
256 // the glib default loop in the sense that it runs the glib default context:
257 // as in the context where sources are added by g_timeout_add and
258 // g_idle_add, and returned by g_main_context_default. gconf uses glib
259 // timeouts and idles and possibly other callbacks that will all be
260 // dispatched on this thread. Since gconf is not thread safe, any use of
261 // gconf must be done on the thread running this loop.
262 scoped_refptr<base::SingleThreadTaskRunner> glib_thread_task_runner_;
263 // Task runner for the IO thread. GetLatestProxyConfig() is called from
264 // the thread running this loop.
265 scoped_refptr<base::SingleThreadTaskRunner> io_thread_task_runner_;
267 ObserverList<Observer> observers_;
269 DISALLOW_COPY_AND_ASSIGN(Delegate);
272 // Thin wrapper shell around Delegate.
274 // Usual constructor
275 ProxyConfigServiceLinux();
276 // For testing: take alternate setting and env var getter implementations.
277 explicit ProxyConfigServiceLinux(base::Environment* env_var_getter);
278 ProxyConfigServiceLinux(base::Environment* env_var_getter,
279 SettingGetter* setting_getter);
281 virtual ~ProxyConfigServiceLinux();
283 void SetupAndFetchInitialConfig(
284 base::SingleThreadTaskRunner* glib_thread_task_runner,
285 base::SingleThreadTaskRunner* io_thread_task_runner,
286 MessageLoopForIO* file_loop) {
287 delegate_->SetUpAndFetchInitialConfig(glib_thread_task_runner,
288 io_thread_task_runner, file_loop);
290 void OnCheckProxyConfigSettings() {
291 delegate_->OnCheckProxyConfigSettings();
294 // ProxyConfigService methods:
295 // Called from IO thread.
296 virtual void AddObserver(Observer* observer) OVERRIDE;
297 virtual void RemoveObserver(Observer* observer) OVERRIDE;
298 virtual ProxyConfigService::ConfigAvailability GetLatestProxyConfig(
299 ProxyConfig* config) OVERRIDE;
301 private:
302 scoped_refptr<Delegate> delegate_;
304 DISALLOW_COPY_AND_ASSIGN(ProxyConfigServiceLinux);
307 } // namespace net
309 #endif // NET_PROXY_PROXY_CONFIG_SERVICE_LINUX_H_