Chromium Code Reviews| 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 <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| 11 #include <string> | 11 #include <string> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
| 15 #include "base/environment.h" | 15 #include "base/environment.h" |
| 16 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
| 18 #include "base/observer_list.h" | 18 #include "base/observer_list.h" |
| 19 #include "net/base/net_export.h" | 19 #include "net/base/net_export.h" |
| 20 #include "net/proxy/proxy_config.h" | 20 #include "net/proxy/proxy_config.h" |
| 21 #include "net/proxy/proxy_config_service.h" | 21 #include "net/proxy/proxy_config_service.h" |
| 22 #include "net/proxy/proxy_server.h" | 22 #include "net/proxy/proxy_server.h" |
| 23 | 23 |
| 24 namespace base { | 24 namespace base { |
| 25 class SingleThreadTaskRunner; | 25 class SingleThreadTaskRunner; |
| 26 class SequencedTaskRunner; | |
| 26 } // namespace base | 27 } // namespace base |
| 27 | 28 |
| 28 namespace net { | 29 namespace net { |
| 29 | 30 |
| 30 // Implementation of ProxyConfigService that retrieves the system proxy | 31 // Implementation of ProxyConfigService that retrieves the system proxy |
| 31 // settings from environment variables, gconf, gsettings, or kioslaverc (KDE). | 32 // settings from environment variables, gconf, gsettings, or kioslaverc (KDE). |
| 32 class NET_EXPORT_PRIVATE ProxyConfigServiceLinux : public ProxyConfigService { | 33 class NET_EXPORT_PRIVATE ProxyConfigServiceLinux : public ProxyConfigService { |
| 33 public: | 34 public: |
| 34 class Delegate; | 35 class Delegate; |
| 35 | 36 |
| 36 class SettingGetter { | 37 class SettingGetter { |
| 37 public: | 38 public: |
| 38 // Buffer size used in some implementations of this class when reading | 39 // Buffer size used in some implementations of this class when reading |
| 39 // files. Defined here so unit tests can construct worst-case inputs. | 40 // files. Defined here so unit tests can construct worst-case inputs. |
| 40 static const size_t BUFFER_SIZE = 512; | 41 static const size_t BUFFER_SIZE = 512; |
| 41 | 42 |
| 42 SettingGetter() {} | 43 SettingGetter() {} |
| 43 virtual ~SettingGetter() {} | 44 virtual ~SettingGetter() {} |
| 44 | 45 |
| 45 // Initializes the class: obtains a gconf/gsettings client, or simulates | 46 // Initializes the class: obtains a gconf/gsettings client, or simulates |
| 46 // one, in the concrete implementations. Returns true on success. Must be | 47 // one, in the concrete implementations. Returns true on success. Must be |
| 47 // called before using other methods, and should be called on the thread | 48 // called before using other methods, and should be called on the thread |
| 48 // running the glib main loop. | 49 // running the glib main loop. |
| 49 // One of |glib_task_runner| and |file_task_runner| will be | 50 // Either |glib_task_runner| or an internal sequenced task runner will |
| 50 // used for gconf/gsettings calls or reading necessary files, depending on | 51 // be used for gconf/gsettings calls or reading necessary files, |
| 51 // the implementation. | 52 // depending on the implementation. |
|
mmenke
2017/06/22 18:07:06
Could we make it clearer that we're not performing
eroman
2017/06/22 19:50:47
Done. How about this:
50 // This interface s
mmenke
2017/06/22 19:52:10
SGTM
| |
| 52 virtual bool Init( | 53 virtual bool Init(const scoped_refptr<base::SingleThreadTaskRunner>& |
| 53 const scoped_refptr<base::SingleThreadTaskRunner>& glib_task_runner, | 54 glib_task_runner) = 0; |
| 54 const scoped_refptr<base::SingleThreadTaskRunner>& | |
| 55 file_task_runner) = 0; | |
| 56 | 55 |
| 57 // Releases the gconf/gsettings client, which clears cached directories and | 56 // Releases the gconf/gsettings client, which clears cached directories and |
| 58 // stops notifications. | 57 // stops notifications. |
| 59 virtual void ShutDown() = 0; | 58 virtual void ShutDown() = 0; |
| 60 | 59 |
| 61 // Requests notification of gconf/gsettings changes for proxy | 60 // Requests notification of gconf/gsettings changes for proxy |
| 62 // settings. Returns true on success. | 61 // settings. Returns true on success. |
| 63 virtual bool SetUpNotifications(Delegate* delegate) = 0; | 62 virtual bool SetUpNotifications(Delegate* delegate) = 0; |
| 64 | 63 |
| 65 // Returns the message loop for the thread on which this object | 64 // Returns the message loop for the thread on which this object |
| 66 // handles notifications, and also on which it must be destroyed. | 65 // handles notifications, and also on which it must be destroyed. |
| 67 // Returns NULL if it does not matter. | 66 // Returns NULL if it does not matter. |
| 68 virtual const scoped_refptr<base::SingleThreadTaskRunner>& | 67 virtual const scoped_refptr<base::SequencedTaskRunner>& |
| 69 GetNotificationTaskRunner() = 0; | 68 GetNotificationTaskRunner() = 0; |
| 70 | 69 |
| 71 // Returns the source of proxy settings. | 70 // Returns the source of proxy settings. |
| 72 virtual ProxyConfigSource GetConfigSource() = 0; | 71 virtual ProxyConfigSource GetConfigSource() = 0; |
| 73 | 72 |
| 74 // These are all the values that can be fetched. We used to just use the | 73 // These are all the values that can be fetched. We used to just use the |
| 75 // corresponding paths in gconf for these, but gconf is now obsolete and | 74 // corresponding paths in gconf for these, but gconf is now obsolete and |
| 76 // in the future we'll be using mostly gsettings/kioslaverc so we | 75 // in the future we'll be using mostly gsettings/kioslaverc so we |
| 77 // enumerate them instead to avoid unnecessary string operations. | 76 // enumerate them instead to avoid unnecessary string operations. |
| 78 enum StringSetting { | 77 enum StringSetting { |
| 79 PROXY_MODE, | 78 PROXY_MODE, |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 143 // ProxyConfigServiceLinux is created on the UI thread, and | 142 // ProxyConfigServiceLinux is created on the UI thread, and |
| 144 // SetUpAndFetchInitialConfig() is immediately called to synchronously | 143 // SetUpAndFetchInitialConfig() is immediately called to synchronously |
| 145 // fetch the original configuration and set up change notifications on | 144 // fetch the original configuration and set up change notifications on |
| 146 // the UI thread. | 145 // the UI thread. |
| 147 // | 146 // |
| 148 // Past that point, it is accessed periodically through the | 147 // Past that point, it is accessed periodically through the |
| 149 // ProxyConfigService interface (GetLatestProxyConfig, AddObserver, | 148 // ProxyConfigService interface (GetLatestProxyConfig, AddObserver, |
| 150 // RemoveObserver) from the IO thread. | 149 // RemoveObserver) from the IO thread. |
| 151 // | 150 // |
| 152 // Setting change notification callbacks can occur at any time and are | 151 // Setting change notification callbacks can occur at any time and are |
| 153 // run on either the UI thread (gconf/gsettings) or the file thread | 152 // run on either the UI thread (gconf/gsettings) or the sequenced task |
| 154 // (KDE). The new settings are fetched on that thread, and the resulting | 153 // runner (KDE). The new settings are fetched on that thread, and the |
| 155 // proxy config is posted to the IO thread through | 154 // resulting proxy config is posted to the IO thread through |
| 156 // Delegate::SetNewProxyConfig(). We then notify observers on the IO | 155 // Delegate::SetNewProxyConfig(). We then notify observers on the IO |
| 157 // thread of the configuration change. | 156 // thread of the configuration change. |
| 158 // | 157 // |
| 159 // ProxyConfigServiceLinux is deleted from the IO thread. | 158 // ProxyConfigServiceLinux is deleted from the IO thread. |
| 160 // | 159 // |
| 161 // The substance of the ProxyConfigServiceLinux implementation is | 160 // The substance of the ProxyConfigServiceLinux implementation is |
| 162 // wrapped in the Delegate ref counted class. On deleting the | 161 // wrapped in the Delegate ref counted class. On deleting the |
| 163 // ProxyConfigServiceLinux, Delegate::OnDestroy() is posted to either | 162 // ProxyConfigServiceLinux, Delegate::OnDestroy() is posted to either |
| 164 // the UI thread (gconf/gsettings) or the file thread (KDE) where change | 163 // the UI thread (gconf/gsettings) or a sequenced task runner (KDE) |
| 165 // notifications will be safely stopped before releasing Delegate. | 164 // where change notifications will be safely stopped before releasing |
| 165 // Delegate. | |
| 166 | 166 |
| 167 class Delegate : public base::RefCountedThreadSafe<Delegate> { | 167 class Delegate : public base::RefCountedThreadSafe<Delegate> { |
| 168 public: | 168 public: |
| 169 // Normal constructor. | 169 // Normal constructor. |
| 170 explicit Delegate(std::unique_ptr<base::Environment> env_var_getter); | 170 explicit Delegate(std::unique_ptr<base::Environment> env_var_getter); |
| 171 | 171 |
| 172 // Constructor for testing. | 172 // Constructor for testing. |
| 173 Delegate(std::unique_ptr<base::Environment> env_var_getter, | 173 Delegate(std::unique_ptr<base::Environment> env_var_getter, |
| 174 SettingGetter* setting_getter); | 174 SettingGetter* setting_getter); |
| 175 | 175 |
| 176 // Synchronously obtains the proxy configuration. If gconf, | 176 // Synchronously obtains the proxy configuration. If gconf, |
| 177 // gsettings, or kioslaverc are used, also enables notifications for | 177 // gsettings, or kioslaverc are used, also enables notifications for |
| 178 // setting changes. gconf/gsettings must only be accessed from the | 178 // setting changes. gconf/gsettings must only be accessed from the |
| 179 // thread running the default glib main loop, and so this method | 179 // thread running the default glib main loop, and so this method |
| 180 // 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 |
| 181 // thread is specified so that notifications can post tasks to it | 181 // thread is specified so that notifications can post tasks to it |
| 182 // (and for assertions). The message loop for the file thread is | 182 // (and for assertions). |
| 183 // used to read any files needed to determine proxy settings. | |
| 184 void SetUpAndFetchInitialConfig( | 183 void SetUpAndFetchInitialConfig( |
| 185 const scoped_refptr<base::SingleThreadTaskRunner>& glib_task_runner, | 184 const scoped_refptr<base::SingleThreadTaskRunner>& glib_task_runner, |
| 186 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, | 185 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner); |
| 187 const scoped_refptr<base::SingleThreadTaskRunner>& file_task_runner); | |
| 188 | 186 |
| 189 // Handler for setting change notifications: fetches a new proxy | 187 // Handler for setting change notifications: fetches a new proxy |
| 190 // configuration from settings, and if this config is different | 188 // configuration from settings, and if this config is different |
| 191 // than what we had before, posts a task to have it stored in | 189 // than what we had before, posts a task to have it stored in |
| 192 // cached_config_. | 190 // cached_config_. |
| 193 // Left public for simplicity. | 191 // Left public for simplicity. |
| 194 void OnCheckProxyConfigSettings(); | 192 void OnCheckProxyConfigSettings(); |
| 195 | 193 |
| 196 // Called from IO thread. | 194 // Called from IO thread. |
| 197 void AddObserver(Observer* observer); | 195 void AddObserver(Observer* observer); |
| 198 void RemoveObserver(Observer* observer); | 196 void RemoveObserver(Observer* observer); |
| 199 ProxyConfigService::ConfigAvailability GetLatestProxyConfig( | 197 ProxyConfigService::ConfigAvailability GetLatestProxyConfig( |
| 200 ProxyConfig* config); | 198 ProxyConfig* config); |
| 201 | 199 |
| 202 // Posts a call to OnDestroy() to the UI or FILE thread, depending on the | 200 // Posts a call to OnDestroy() to the UI or sequenced task runner, |
| 203 // setting getter in use. Called from ProxyConfigServiceLinux's destructor. | 201 // depending on the setting getter in use. Called from |
| 202 // ProxyConfigServiceLinux's destructor. | |
| 204 void PostDestroyTask(); | 203 void PostDestroyTask(); |
| 205 // Safely stops change notifications. Posted to either the UI or FILE | 204 // Safely stops change notifications. Posted to either the UI or |
| 206 // thread, depending on the setting getter in use. | 205 // sequenced task runner, depending on the setting getter in use. |
| 207 void OnDestroy(); | 206 void OnDestroy(); |
| 208 | 207 |
| 209 private: | 208 private: |
| 210 friend class base::RefCountedThreadSafe<Delegate>; | 209 friend class base::RefCountedThreadSafe<Delegate>; |
| 211 | 210 |
| 212 ~Delegate(); | 211 ~Delegate(); |
| 213 | 212 |
| 214 // Obtains an environment variable's value. Parses a proxy server | 213 // Obtains an environment variable's value. Parses a proxy server |
| 215 // specification from it and puts it in result. Returns true if the | 214 // specification from it and puts it in result. Returns true if the |
| 216 // requested variable is defined and the value valid. | 215 // requested variable is defined and the value valid. |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 280 // For testing: take alternate setting and env var getter implementations. | 279 // For testing: take alternate setting and env var getter implementations. |
| 281 explicit ProxyConfigServiceLinux( | 280 explicit ProxyConfigServiceLinux( |
| 282 std::unique_ptr<base::Environment> env_var_getter); | 281 std::unique_ptr<base::Environment> env_var_getter); |
| 283 ProxyConfigServiceLinux(std::unique_ptr<base::Environment> env_var_getter, | 282 ProxyConfigServiceLinux(std::unique_ptr<base::Environment> env_var_getter, |
| 284 SettingGetter* setting_getter); | 283 SettingGetter* setting_getter); |
| 285 | 284 |
| 286 ~ProxyConfigServiceLinux() override; | 285 ~ProxyConfigServiceLinux() override; |
| 287 | 286 |
| 288 void SetupAndFetchInitialConfig( | 287 void SetupAndFetchInitialConfig( |
| 289 const scoped_refptr<base::SingleThreadTaskRunner>& glib_task_runner, | 288 const scoped_refptr<base::SingleThreadTaskRunner>& glib_task_runner, |
| 290 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, | 289 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) { |
| 291 const scoped_refptr<base::SingleThreadTaskRunner>& file_task_runner) { | 290 delegate_->SetUpAndFetchInitialConfig(glib_task_runner, io_task_runner); |
| 292 delegate_->SetUpAndFetchInitialConfig(glib_task_runner, | |
| 293 io_task_runner, | |
| 294 file_task_runner); | |
| 295 } | 291 } |
| 296 void OnCheckProxyConfigSettings() { | 292 void OnCheckProxyConfigSettings() { |
| 297 delegate_->OnCheckProxyConfigSettings(); | 293 delegate_->OnCheckProxyConfigSettings(); |
| 298 } | 294 } |
| 299 | 295 |
| 300 // ProxyConfigService methods: | 296 // ProxyConfigService methods: |
| 301 // Called from IO thread. | 297 // Called from IO thread. |
| 302 void AddObserver(Observer* observer) override; | 298 void AddObserver(Observer* observer) override; |
| 303 void RemoveObserver(Observer* observer) override; | 299 void RemoveObserver(Observer* observer) override; |
| 304 ProxyConfigService::ConfigAvailability GetLatestProxyConfig( | 300 ProxyConfigService::ConfigAvailability GetLatestProxyConfig( |
| 305 ProxyConfig* config) override; | 301 ProxyConfig* config) override; |
| 306 | 302 |
| 307 private: | 303 private: |
| 308 scoped_refptr<Delegate> delegate_; | 304 scoped_refptr<Delegate> delegate_; |
| 309 | 305 |
| 310 DISALLOW_COPY_AND_ASSIGN(ProxyConfigServiceLinux); | 306 DISALLOW_COPY_AND_ASSIGN(ProxyConfigServiceLinux); |
| 311 }; | 307 }; |
| 312 | 308 |
| 313 } // namespace net | 309 } // namespace net |
| 314 | 310 |
| 315 #endif // NET_PROXY_PROXY_CONFIG_SERVICE_LINUX_H_ | 311 #endif // NET_PROXY_PROXY_CONFIG_SERVICE_LINUX_H_ |
| OLD | NEW |