• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2009 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 NET_PROXY_PROXY_CONFIG_SERVICE_LINUX_H_
6 #define NET_PROXY_PROXY_CONFIG_SERVICE_LINUX_H_
7 
8 #include <string>
9 #include <vector>
10 
11 #include "base/basictypes.h"
12 #include "base/linux_util.h"
13 #include "base/message_loop.h"
14 #include "base/ref_counted.h"
15 #include "base/scoped_ptr.h"
16 #include "net/proxy/proxy_config.h"
17 #include "net/proxy/proxy_config_service.h"
18 #include "net/proxy/proxy_server.h"
19 
20 namespace net {
21 
22 // Implementation of ProxyConfigService that retrieves the system proxy
23 // settings from environment variables or gconf.
24 class ProxyConfigServiceLinux : public ProxyConfigService {
25  public:
26 
27   // Forward declaration of Delegate.
28   class Delegate;
29 
30   class GConfSettingGetter {
31    public:
32     // Buffer size used in some implementations of this class when reading
33     // files. Defined here so unit tests can construct worst-case inputs.
34     static const size_t BUFFER_SIZE = 512;
35 
GConfSettingGetter()36     GConfSettingGetter() {}
~GConfSettingGetter()37     virtual ~GConfSettingGetter() {}
38 
39     // Initializes the class: obtains a gconf client, or simulates one, in
40     // the concrete implementations. Returns true on success. Must be called
41     // before using other methods, and should be called on the thread running
42     // the glib main loop.
43     // One of |glib_default_loop| and |file_loop| will be used for gconf calls
44     // or reading necessary files, depending on the implementation.
45     virtual bool Init(MessageLoop* glib_default_loop,
46                       MessageLoopForIO* file_loop) = 0;
47 
48     // Releases the gconf client, which clears cached directories and
49     // stops notifications.
50     virtual void Shutdown() = 0;
51 
52     // Requests notification of gconf setting changes for proxy
53     // settings. Returns true on success.
54     virtual bool SetupNotification(Delegate* delegate) = 0;
55 
56     // Returns the message loop for the thread on which this object
57     // handles notifications, and also on which it must be destroyed.
58     // Returns NULL if it does not matter.
59     virtual MessageLoop* GetNotificationLoop() = 0;
60 
61     // Returns the data source's name (e.g. "gconf", "KDE", "test").
62     // Used only for diagnostic purposes (e.g. LOG(INFO) etc.).
63     virtual const char* GetDataSource() = 0;
64 
65     // Gets a string type value from gconf and stores it in
66     // result. Returns false if the key is unset or on error. Must
67     // only be called after a successful call to Init(), and not
68     // after a failed call to SetupNotification() or after calling
69     // Release().
70     virtual bool GetString(const char* key, std::string* result) = 0;
71     // Same thing for a bool typed value.
72     virtual bool GetBoolean(const char* key, bool* result) = 0;
73     // Same for an int typed value.
74     virtual bool GetInt(const char* key, int* result) = 0;
75     // And for a string list.
76     virtual bool GetStringList(const char* key,
77                                std::vector<std::string>* result) = 0;
78 
79    private:
80     DISALLOW_COPY_AND_ASSIGN(GConfSettingGetter);
81   };
82 
83   // ProxyConfigServiceLinux is created on the UI thread, and
84   // SetupAndFetchInitialConfig() is immediately called to
85   // synchronously fetch the original configuration and setup gconf
86   // notifications on the UI thread.
87   //
88   // Passed that point, it is accessed periodically through
89   // GetProxyConfig() from the IO thread.
90   //
91   // gconf change notification callbacks can occur at any time and are
92   // run on the UI thread. The new gconf settings are fetched on the
93   // UI thread, and the new resulting proxy config is posted to the IO
94   // thread through Delegate::SetNewProxyConfig().
95   //
96   // ProxyConfigServiceLinux is deleted from the IO thread.
97   //
98   // The substance of the ProxyConfigServiceLinux implementation is
99   // wrapped in the Delegate ref counted class. On deleting the
100   // ProxyConfigServiceLinux, Delegate::OnDestroy() is posted to the
101   // UI thread where gconf notifications will be safely stopped before
102   // releasing Delegate.
103 
104   class Delegate : public base::RefCountedThreadSafe<Delegate> {
105    public:
106     // Constructor receives env var getter implementation to use, and
107     // takes ownership of it. This is the normal constructor.
108     explicit Delegate(base::EnvironmentVariableGetter* env_var_getter);
109     // Constructor receives gconf and env var getter implementations
110     // to use, and takes ownership of them. Used for testing.
111     Delegate(base::EnvironmentVariableGetter* env_var_getter,
112              GConfSettingGetter* gconf_getter);
113     // Synchronously obtains the proxy configuration. If gconf is
114     // used, also enables gconf notification for setting
115     // changes. gconf must only be accessed from the thread running
116     // the default glib main loop, and so this method must be called
117     // from the UI thread. The message loop for the IO thread is
118     // specified so that notifications can post tasks to it (and for
119     // assertions). The message loop for the file thread is used to
120     // read any files needed to determine proxy settings.
121     void SetupAndFetchInitialConfig(MessageLoop* glib_default_loop,
122                                     MessageLoop* io_loop,
123                                     MessageLoopForIO* file_loop);
124 
125     // Handler for gconf change notifications: fetches a new proxy
126     // configuration from gconf settings, and if this config is
127     // different than what we had before, posts a task to have it
128     // stored in cached_config_.
129     // Left public for simplicity.
130     void OnCheckProxyConfigSettings();
131 
132     // Called from IO thread.
133     int GetProxyConfig(ProxyConfig* config);
134 
135     // Posts a call to OnDestroy() to the UI thread. Called from
136     // ProxyConfigServiceLinux's destructor.
137     void PostDestroyTask();
138     // Safely stops gconf notifications. Posted to the UI thread.
139     void OnDestroy();
140 
141    private:
142     friend class base::RefCountedThreadSafe<Delegate>;
143 
~Delegate()144     ~Delegate() {}
145 
146     // Obtains an environment variable's value. Parses a proxy server
147     // specification from it and puts it in result. Returns true if the
148     // requested variable is defined and the value valid.
149     bool GetProxyFromEnvVarForScheme(const char* variable,
150                                      ProxyServer::Scheme scheme,
151                                      ProxyServer* result_server);
152     // As above but with scheme set to HTTP, for convenience.
153     bool GetProxyFromEnvVar(const char* variable, ProxyServer* result_server);
154     // Fills proxy config from environment variables. Returns true if
155     // variables were found and the configuration is valid.
156     bool GetConfigFromEnv(ProxyConfig* config);
157 
158     // Obtains host and port gconf settings and parses a proxy server
159     // specification from it and puts it in result. Returns true if the
160     // requested variable is defined and the value valid.
161     bool GetProxyFromGConf(const char* key_prefix, bool is_socks,
162                            ProxyServer* result_server);
163     // Fills proxy config from gconf. Returns true if settings were found
164     // and the configuration is valid.
165     bool GetConfigFromGConf(ProxyConfig* config);
166 
167     // This method is posted from the UI thread to the IO thread to
168     // carry the new config information.
169     void SetNewProxyConfig(const ProxyConfig& new_config);
170 
171     scoped_ptr<base::EnvironmentVariableGetter> env_var_getter_;
172     scoped_ptr<GConfSettingGetter> gconf_getter_;
173 
174     // Cached proxy configuration, to be returned by
175     // GetProxyConfig. Initially populated from the UI thread, but
176     // afterwards only accessed from the IO thread.
177     ProxyConfig cached_config_;
178 
179     // A copy kept on the UI thread of the last seen proxy config, so as
180     // to avoid posting a call to SetNewProxyConfig when we get a
181     // notification but the config has not actually changed.
182     ProxyConfig reference_config_;
183 
184     // The MessageLoop for the UI thread, aka main browser thread. This
185     // thread is where we run the glib main loop (see
186     // base/message_pump_glib.h). It is the glib default loop in the
187     // sense that it runs the glib default context: as in the context
188     // where sources are added by g_timeout_add and g_idle_add, and
189     // returned by g_main_context_default. gconf uses glib timeouts and
190     // idles and possibly other callbacks that will all be dispatched on
191     // this thread. Since gconf is not thread safe, any use of gconf
192     // must be done on the thread running this loop.
193     MessageLoop* glib_default_loop_;
194     // MessageLoop for the IO thread. GetProxyConfig() is called from
195     // the thread running this loop.
196     MessageLoop* io_loop_;
197 
198     DISALLOW_COPY_AND_ASSIGN(Delegate);
199   };
200 
201   // Thin wrapper shell around Delegate.
202 
203   // Usual constructor
204   ProxyConfigServiceLinux();
205   // For testing: take alternate gconf and env var getter implementations.
206   explicit ProxyConfigServiceLinux(
207       base::EnvironmentVariableGetter* env_var_getter);
208   ProxyConfigServiceLinux(base::EnvironmentVariableGetter* env_var_getter,
209                           GConfSettingGetter* gconf_getter);
210 
~ProxyConfigServiceLinux()211   virtual ~ProxyConfigServiceLinux() {
212     delegate_->PostDestroyTask();
213   }
214 
SetupAndFetchInitialConfig(MessageLoop * glib_default_loop,MessageLoop * io_loop,MessageLoopForIO * file_loop)215   void SetupAndFetchInitialConfig(MessageLoop* glib_default_loop,
216                                   MessageLoop* io_loop,
217                                   MessageLoopForIO* file_loop) {
218     delegate_->SetupAndFetchInitialConfig(glib_default_loop, io_loop,
219                                           file_loop);
220   }
OnCheckProxyConfigSettings()221   void OnCheckProxyConfigSettings() {
222     delegate_->OnCheckProxyConfigSettings();
223   }
224 
225   // ProxyConfigService methods:
226   // Called from IO thread.
GetProxyConfig(ProxyConfig * config)227   virtual int GetProxyConfig(ProxyConfig* config) {
228     return delegate_->GetProxyConfig(config);
229   }
230 
231  private:
232   scoped_refptr<Delegate> delegate_;
233 
234   DISALLOW_COPY_AND_ASSIGN(ProxyConfigServiceLinux);
235 };
236 
237 }  // namespace net
238 
239 #endif  // NET_PROXY_PROXY_CONFIG_SERVICE_LINUX_H_
240