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_
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"
23 class MessageLoopForIO
;
24 class SingleThreadTaskRunner
;
29 // Implementation of ProxyConfigService that retrieves the system proxy
30 // settings from environment variables, gconf, gsettings, or kioslaverc (KDE).
31 class NET_EXPORT_PRIVATE ProxyConfigServiceLinux
: public ProxyConfigService
{
34 // Forward declaration of Delegate.
39 // Buffer size used in some implementations of this class when reading
40 // files. Defined here so unit tests can construct worst-case inputs.
41 static const size_t BUFFER_SIZE
= 512;
44 virtual ~SettingGetter() {}
46 // Initializes the class: obtains a gconf/gsettings client, or simulates
47 // one, in the concrete implementations. Returns true on success. Must be
48 // called before using other methods, and should be called on the thread
49 // running the glib main loop.
50 // One of |glib_thread_task_runner| and |file_loop| will be used for
51 // gconf/gsettings calls or reading necessary files, depending on the
53 virtual bool Init(base::SingleThreadTaskRunner
* glib_thread_task_runner
,
54 base::MessageLoopForIO
* file_loop
) = 0;
56 // Releases the gconf/gsettings client, which clears cached directories and
57 // stops notifications.
58 virtual void ShutDown() = 0;
60 // Requests notification of gconf/gsettings changes for proxy
61 // settings. Returns true on success.
62 virtual bool SetUpNotifications(Delegate
* delegate
) = 0;
64 // Returns the message loop for the thread on which this object
65 // handles notifications, and also on which it must be destroyed.
66 // Returns NULL if it does not matter.
67 virtual base::SingleThreadTaskRunner
* GetNotificationTaskRunner() = 0;
69 // Returns the source of proxy settings.
70 virtual ProxyConfigSource
GetConfigSource() = 0;
72 // These are all the values that can be fetched. We used to just use the
73 // corresponding paths in gconf for these, but gconf is now obsolete and
74 // in the future we'll be using mostly gsettings/kioslaverc so we
75 // enumerate them instead to avoid unnecessary string operations.
87 PROXY_USE_AUTHENTICATION
,
95 enum StringListSetting
{
99 // Given a PROXY_*_HOST value, return the corresponding PROXY_*_PORT value.
100 static IntSetting
HostSettingToPortSetting(StringSetting host
) {
102 case PROXY_HTTP_HOST
:
103 return PROXY_HTTP_PORT
;
104 case PROXY_HTTPS_HOST
:
105 return PROXY_HTTPS_PORT
;
107 return PROXY_FTP_PORT
;
108 case PROXY_SOCKS_HOST
:
109 return PROXY_SOCKS_PORT
;
112 return PROXY_HTTP_PORT
; // Placate compiler.
116 // Gets a string type value from the data source and stores it in
117 // |*result|. Returns false if the key is unset or on error. Must only be
118 // called after a successful call to Init(), and not after a failed call
119 // to SetUpNotifications() or after calling Release().
120 virtual bool GetString(StringSetting key
, std::string
* result
) = 0;
121 // Same thing for a bool typed value.
122 virtual bool GetBool(BoolSetting key
, bool* result
) = 0;
123 // Same for an int typed value.
124 virtual bool GetInt(IntSetting key
, int* result
) = 0;
125 // And for a string list.
126 virtual bool GetStringList(StringListSetting key
,
127 std::vector
<std::string
>* result
) = 0;
129 // Returns true if the bypass list should be interpreted as a proxy
130 // whitelist rather than blacklist. (This is KDE-specific.)
131 virtual bool BypassListIsReversed() = 0;
133 // Returns true if the bypass rules should be interpreted as
134 // suffix-matching rules.
135 virtual bool MatchHostsUsingSuffixMatching() = 0;
138 DISALLOW_COPY_AND_ASSIGN(SettingGetter
);
141 // ProxyConfigServiceLinux is created on the UI thread, and
142 // SetUpAndFetchInitialConfig() is immediately called to synchronously
143 // fetch the original configuration and set up change notifications on
146 // Past that point, it is accessed periodically through the
147 // ProxyConfigService interface (GetLatestProxyConfig, AddObserver,
148 // RemoveObserver) from the IO thread.
150 // Setting change notification callbacks can occur at any time and are
151 // run on either the UI thread (gconf/gsettings) or the file thread
152 // (KDE). The new settings are fetched on that thread, and the resulting
153 // proxy config is posted to the IO thread through
154 // Delegate::SetNewProxyConfig(). We then notify observers on the IO
155 // thread of the configuration change.
157 // ProxyConfigServiceLinux is deleted from the IO thread.
159 // The substance of the ProxyConfigServiceLinux implementation is
160 // wrapped in the Delegate ref counted class. On deleting the
161 // ProxyConfigServiceLinux, Delegate::OnDestroy() is posted to either
162 // the UI thread (gconf/gsettings) or the file thread (KDE) where change
163 // notifications will be safely stopped before releasing Delegate.
165 class Delegate
: public base::RefCountedThreadSafe
<Delegate
> {
167 // Constructor receives env var getter implementation to use, and
168 // takes ownership of it. This is the normal constructor.
169 explicit Delegate(base::Environment
* env_var_getter
);
170 // Constructor receives setting and env var getter implementations
171 // to use, and takes ownership of them. Used for testing.
172 Delegate(base::Environment
* env_var_getter
, SettingGetter
* setting_getter
);
174 // Synchronously obtains the proxy configuration. If gconf,
175 // gsettings, or kioslaverc are used, also enables notifications for
176 // setting changes. gconf/gsettings must only be accessed from the
177 // thread running the default glib main loop, and so this method
178 // must be called from the UI thread. The message loop for the IO
179 // thread is specified so that notifications can post tasks to it
180 // (and for assertions). The message loop for the file thread is
181 // used to read any files needed to determine proxy settings.
182 void SetUpAndFetchInitialConfig(
183 base::SingleThreadTaskRunner
* glib_thread_task_runner
,
184 base::SingleThreadTaskRunner
* io_thread_task_runner
,
185 base::MessageLoopForIO
* file_loop
);
187 // Handler for setting change notifications: fetches a new proxy
188 // configuration from settings, and if this config is different
189 // than what we had before, posts a task to have it stored in
191 // Left public for simplicity.
192 void OnCheckProxyConfigSettings();
194 // Called from IO thread.
195 void AddObserver(Observer
* observer
);
196 void RemoveObserver(Observer
* observer
);
197 ProxyConfigService::ConfigAvailability
GetLatestProxyConfig(
198 ProxyConfig
* config
);
200 // Posts a call to OnDestroy() to the UI or FILE thread, depending on the
201 // setting getter in use. Called from ProxyConfigServiceLinux's destructor.
202 void PostDestroyTask();
203 // Safely stops change notifications. Posted to either the UI or FILE
204 // thread, depending on the setting getter in use.
208 friend class base::RefCountedThreadSafe
<Delegate
>;
212 // Obtains an environment variable's value. Parses a proxy server
213 // specification from it and puts it in result. Returns true if the
214 // requested variable is defined and the value valid.
215 bool GetProxyFromEnvVarForScheme(const char* variable
,
216 ProxyServer::Scheme scheme
,
217 ProxyServer
* result_server
);
218 // As above but with scheme set to HTTP, for convenience.
219 bool GetProxyFromEnvVar(const char* variable
, ProxyServer
* result_server
);
220 // Fills proxy config from environment variables. Returns true if
221 // variables were found and the configuration is valid.
222 bool GetConfigFromEnv(ProxyConfig
* config
);
224 // Obtains host and port config settings and parses a proxy server
225 // specification from it and puts it in result. Returns true if the
226 // requested variable is defined and the value valid.
227 bool GetProxyFromSettings(SettingGetter::StringSetting host_key
,
228 ProxyServer
* result_server
);
229 // Fills proxy config from settings. Returns true if settings were found
230 // and the configuration is valid.
231 bool GetConfigFromSettings(ProxyConfig
* config
);
233 // This method is posted from the UI thread to the IO thread to
234 // carry the new config information.
235 void SetNewProxyConfig(const ProxyConfig
& new_config
);
237 // This method is run on the getter's notification thread.
238 void SetUpNotifications();
240 scoped_ptr
<base::Environment
> env_var_getter_
;
241 scoped_ptr
<SettingGetter
> setting_getter_
;
243 // Cached proxy configuration, to be returned by
244 // GetLatestProxyConfig. Initially populated from the UI thread, but
245 // afterwards only accessed from the IO thread.
246 ProxyConfig cached_config_
;
248 // A copy kept on the UI thread of the last seen proxy config, so as
249 // to avoid posting a call to SetNewProxyConfig when we get a
250 // notification but the config has not actually changed.
251 ProxyConfig reference_config_
;
253 // The task runner for the glib thread, aka main browser thread. This thread
254 // is where we run the glib main loop (see
255 // base/message_loop/message_pump_glib.h). It is the glib default loop in
256 // the sense that it runs the glib default context: as in the context where
257 // sources are added by g_timeout_add and g_idle_add, and returned by
258 // g_main_context_default. gconf uses glib timeouts and idles and possibly
259 // other callbacks that will all be dispatched on this thread. Since gconf
260 // is not thread safe, any use of gconf must be done on the thread running
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.
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 base::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
;
302 scoped_refptr
<Delegate
> delegate_
;
304 DISALLOW_COPY_AND_ASSIGN(ProxyConfigServiceLinux
);
309 #endif // NET_PROXY_PROXY_CONFIG_SERVICE_LINUX_H_