| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_PROXY_PROXY_CONFIG_SERVICE_LINUX_H_ | 5 #ifndef NET_PROXY_PROXY_CONFIG_SERVICE_LINUX_H_ |
| 6 #define NET_PROXY_PROXY_CONFIG_SERVICE_LINUX_H_ | 6 #define NET_PROXY_PROXY_CONFIG_SERVICE_LINUX_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/environment.h" | 13 #include "base/environment.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/observer_list.h" | 16 #include "base/observer_list.h" |
| 17 #include "net/base/net_export.h" | 17 #include "net/base/net_export.h" |
| 18 #include "net/proxy/proxy_config.h" | 18 #include "net/proxy/proxy_config.h" |
| 19 #include "net/proxy/proxy_config_service.h" | 19 #include "net/proxy/proxy_config_service.h" |
| 20 #include "net/proxy/proxy_server.h" | 20 #include "net/proxy/proxy_server.h" |
| 21 | 21 |
| 22 namespace base { | 22 namespace base { |
| 23 class MessageLoopForIO; | |
| 24 class SingleThreadTaskRunner; | 23 class SingleThreadTaskRunner; |
| 25 } // namespace base | 24 } // namespace base |
| 26 | 25 |
| 27 namespace net { | 26 namespace net { |
| 28 | 27 |
| 29 // Implementation of ProxyConfigService that retrieves the system proxy | 28 // Implementation of ProxyConfigService that retrieves the system proxy |
| 30 // settings from environment variables, gconf, gsettings, or kioslaverc (KDE). | 29 // settings from environment variables, gconf, gsettings, or kioslaverc (KDE). |
| 31 class NET_EXPORT_PRIVATE ProxyConfigServiceLinux : public ProxyConfigService { | 30 class NET_EXPORT_PRIVATE ProxyConfigServiceLinux : public ProxyConfigService { |
| 32 public: | 31 public: |
| 33 | 32 |
| 34 // Forward declaration of Delegate. | 33 // Forward declaration of Delegate. |
| 35 class Delegate; | 34 class Delegate; |
| 36 | 35 |
| 37 class SettingGetter { | 36 class SettingGetter { |
| 38 public: | 37 public: |
| 39 // Buffer size used in some implementations of this class when reading | 38 // Buffer size used in some implementations of this class when reading |
| 40 // files. Defined here so unit tests can construct worst-case inputs. | 39 // files. Defined here so unit tests can construct worst-case inputs. |
| 41 static const size_t BUFFER_SIZE = 512; | 40 static const size_t BUFFER_SIZE = 512; |
| 42 | 41 |
| 43 SettingGetter() {} | 42 SettingGetter() {} |
| 44 virtual ~SettingGetter() {} | 43 virtual ~SettingGetter() {} |
| 45 | 44 |
| 46 // Initializes the class: obtains a gconf/gsettings client, or simulates | 45 // Initializes the class: obtains a gconf/gsettings client, or simulates |
| 47 // one, in the concrete implementations. Returns true on success. Must be | 46 // one, in the concrete implementations. Returns true on success. Must be |
| 48 // called before using other methods, and should be called on the thread | 47 // called before using other methods, and should be called on the thread |
| 49 // running the glib main loop. | 48 // running the glib main loop. |
| 50 // One of |glib_thread_task_runner| and |file_loop| will be used for | 49 // One of |glib_task_runner| and |file_task_runner| will be |
| 51 // gconf/gsettings calls or reading necessary files, depending on the | 50 // used for gconf/gsettings calls or reading necessary files, depending on |
| 52 // implementation. | 51 // the implementation. |
| 53 virtual bool Init(base::SingleThreadTaskRunner* glib_thread_task_runner, | 52 virtual bool Init( |
| 54 base::MessageLoopForIO* file_loop) = 0; | 53 const scoped_refptr<base::SingleThreadTaskRunner>& glib_task_runner, |
| 54 const scoped_refptr<base::SingleThreadTaskRunner>& |
| 55 file_task_runner) = 0; |
| 55 | 56 |
| 56 // Releases the gconf/gsettings client, which clears cached directories and | 57 // Releases the gconf/gsettings client, which clears cached directories and |
| 57 // stops notifications. | 58 // stops notifications. |
| 58 virtual void ShutDown() = 0; | 59 virtual void ShutDown() = 0; |
| 59 | 60 |
| 60 // Requests notification of gconf/gsettings changes for proxy | 61 // Requests notification of gconf/gsettings changes for proxy |
| 61 // settings. Returns true on success. | 62 // settings. Returns true on success. |
| 62 virtual bool SetUpNotifications(Delegate* delegate) = 0; | 63 virtual bool SetUpNotifications(Delegate* delegate) = 0; |
| 63 | 64 |
| 64 // Returns the message loop for the thread on which this object | 65 // Returns the message loop for the thread on which this object |
| 65 // handles notifications, and also on which it must be destroyed. | 66 // handles notifications, and also on which it must be destroyed. |
| 66 // Returns NULL if it does not matter. | 67 // Returns NULL if it does not matter. |
| 67 virtual base::SingleThreadTaskRunner* GetNotificationTaskRunner() = 0; | 68 virtual const scoped_refptr<base::SingleThreadTaskRunner>& |
| 69 GetNotificationTaskRunner() = 0; |
| 68 | 70 |
| 69 // Returns the source of proxy settings. | 71 // Returns the source of proxy settings. |
| 70 virtual ProxyConfigSource GetConfigSource() = 0; | 72 virtual ProxyConfigSource GetConfigSource() = 0; |
| 71 | 73 |
| 72 // These are all the values that can be fetched. We used to just use the | 74 // 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 | 75 // 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 | 76 // in the future we'll be using mostly gsettings/kioslaverc so we |
| 75 // enumerate them instead to avoid unnecessary string operations. | 77 // enumerate them instead to avoid unnecessary string operations. |
| 76 enum StringSetting { | 78 enum StringSetting { |
| 77 PROXY_MODE, | 79 PROXY_MODE, |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 | 175 |
| 174 // Synchronously obtains the proxy configuration. If gconf, | 176 // Synchronously obtains the proxy configuration. If gconf, |
| 175 // gsettings, or kioslaverc are used, also enables notifications for | 177 // gsettings, or kioslaverc are used, also enables notifications for |
| 176 // setting changes. gconf/gsettings must only be accessed from the | 178 // setting changes. gconf/gsettings must only be accessed from the |
| 177 // thread running the default glib main loop, and so this method | 179 // 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 | 180 // 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 | 181 // thread is specified so that notifications can post tasks to it |
| 180 // (and for assertions). The message loop for the file thread is | 182 // (and for assertions). The message loop for the file thread is |
| 181 // used to read any files needed to determine proxy settings. | 183 // used to read any files needed to determine proxy settings. |
| 182 void SetUpAndFetchInitialConfig( | 184 void SetUpAndFetchInitialConfig( |
| 183 base::SingleThreadTaskRunner* glib_thread_task_runner, | 185 const scoped_refptr<base::SingleThreadTaskRunner>& glib_task_runner, |
| 184 base::SingleThreadTaskRunner* io_thread_task_runner, | 186 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, |
| 185 base::MessageLoopForIO* file_loop); | 187 const scoped_refptr<base::SingleThreadTaskRunner>& file_task_runner); |
| 186 | 188 |
| 187 // Handler for setting change notifications: fetches a new proxy | 189 // Handler for setting change notifications: fetches a new proxy |
| 188 // configuration from settings, and if this config is different | 190 // configuration from settings, and if this config is different |
| 189 // than what we had before, posts a task to have it stored in | 191 // than what we had before, posts a task to have it stored in |
| 190 // cached_config_. | 192 // cached_config_. |
| 191 // Left public for simplicity. | 193 // Left public for simplicity. |
| 192 void OnCheckProxyConfigSettings(); | 194 void OnCheckProxyConfigSettings(); |
| 193 | 195 |
| 194 // Called from IO thread. | 196 // Called from IO thread. |
| 195 void AddObserver(Observer* observer); | 197 void AddObserver(Observer* observer); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 | 254 |
| 253 // The task runner for the glib thread, aka main browser thread. This thread | 255 // The task runner for the glib thread, aka main browser thread. This thread |
| 254 // is where we run the glib main loop (see | 256 // is where we run the glib main loop (see |
| 255 // base/message_loop/message_pump_glib.h). It is the glib default loop in | 257 // 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 | 258 // 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 | 259 // 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 | 260 // 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 | 261 // 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 // is not thread safe, any use of gconf must be done on the thread running |
| 261 // this loop. | 263 // this loop. |
| 262 scoped_refptr<base::SingleThreadTaskRunner> glib_thread_task_runner_; | 264 scoped_refptr<base::SingleThreadTaskRunner> glib_task_runner_; |
| 263 // Task runner for the IO thread. GetLatestProxyConfig() is called from | 265 // Task runner for the IO thread. GetLatestProxyConfig() is called from |
| 264 // the thread running this loop. | 266 // the thread running this loop. |
| 265 scoped_refptr<base::SingleThreadTaskRunner> io_thread_task_runner_; | 267 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; |
| 266 | 268 |
| 267 ObserverList<Observer> observers_; | 269 ObserverList<Observer> observers_; |
| 268 | 270 |
| 269 DISALLOW_COPY_AND_ASSIGN(Delegate); | 271 DISALLOW_COPY_AND_ASSIGN(Delegate); |
| 270 }; | 272 }; |
| 271 | 273 |
| 272 // Thin wrapper shell around Delegate. | 274 // Thin wrapper shell around Delegate. |
| 273 | 275 |
| 274 // Usual constructor | 276 // Usual constructor |
| 275 ProxyConfigServiceLinux(); | 277 ProxyConfigServiceLinux(); |
| 276 // For testing: take alternate setting and env var getter implementations. | 278 // For testing: take alternate setting and env var getter implementations. |
| 277 explicit ProxyConfigServiceLinux(base::Environment* env_var_getter); | 279 explicit ProxyConfigServiceLinux(base::Environment* env_var_getter); |
| 278 ProxyConfigServiceLinux(base::Environment* env_var_getter, | 280 ProxyConfigServiceLinux(base::Environment* env_var_getter, |
| 279 SettingGetter* setting_getter); | 281 SettingGetter* setting_getter); |
| 280 | 282 |
| 281 virtual ~ProxyConfigServiceLinux(); | 283 virtual ~ProxyConfigServiceLinux(); |
| 282 | 284 |
| 283 void SetupAndFetchInitialConfig( | 285 void SetupAndFetchInitialConfig( |
| 284 base::SingleThreadTaskRunner* glib_thread_task_runner, | 286 const scoped_refptr<base::SingleThreadTaskRunner>& glib_task_runner, |
| 285 base::SingleThreadTaskRunner* io_thread_task_runner, | 287 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, |
| 286 base::MessageLoopForIO* file_loop) { | 288 const scoped_refptr<base::SingleThreadTaskRunner>& file_task_runner) { |
| 287 delegate_->SetUpAndFetchInitialConfig(glib_thread_task_runner, | 289 delegate_->SetUpAndFetchInitialConfig(glib_task_runner, |
| 288 io_thread_task_runner, file_loop); | 290 io_task_runner, |
| 291 file_task_runner); |
| 289 } | 292 } |
| 290 void OnCheckProxyConfigSettings() { | 293 void OnCheckProxyConfigSettings() { |
| 291 delegate_->OnCheckProxyConfigSettings(); | 294 delegate_->OnCheckProxyConfigSettings(); |
| 292 } | 295 } |
| 293 | 296 |
| 294 // ProxyConfigService methods: | 297 // ProxyConfigService methods: |
| 295 // Called from IO thread. | 298 // Called from IO thread. |
| 296 virtual void AddObserver(Observer* observer) OVERRIDE; | 299 virtual void AddObserver(Observer* observer) OVERRIDE; |
| 297 virtual void RemoveObserver(Observer* observer) OVERRIDE; | 300 virtual void RemoveObserver(Observer* observer) OVERRIDE; |
| 298 virtual ProxyConfigService::ConfigAvailability GetLatestProxyConfig( | 301 virtual ProxyConfigService::ConfigAvailability GetLatestProxyConfig( |
| 299 ProxyConfig* config) OVERRIDE; | 302 ProxyConfig* config) OVERRIDE; |
| 300 | 303 |
| 301 private: | 304 private: |
| 302 scoped_refptr<Delegate> delegate_; | 305 scoped_refptr<Delegate> delegate_; |
| 303 | 306 |
| 304 DISALLOW_COPY_AND_ASSIGN(ProxyConfigServiceLinux); | 307 DISALLOW_COPY_AND_ASSIGN(ProxyConfigServiceLinux); |
| 305 }; | 308 }; |
| 306 | 309 |
| 307 } // namespace net | 310 } // namespace net |
| 308 | 311 |
| 309 #endif // NET_PROXY_PROXY_CONFIG_SERVICE_LINUX_H_ | 312 #endif // NET_PROXY_PROXY_CONFIG_SERVICE_LINUX_H_ |
| OLD | NEW |