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