proxy_config_service_linux.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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.
  4. #ifndef NET_PROXY_RESOLUTION_PROXY_CONFIG_SERVICE_LINUX_H_
  5. #define NET_PROXY_RESOLUTION_PROXY_CONFIG_SERVICE_LINUX_H_
  6. #include <stddef.h>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "base/compiler_specific.h"
  11. #include "base/environment.h"
  12. #include "base/memory/ref_counted.h"
  13. #include "base/notreached.h"
  14. #include "base/observer_list.h"
  15. #include "net/base/net_export.h"
  16. #include "net/base/proxy_server.h"
  17. #include "net/proxy_resolution/proxy_config_service.h"
  18. #include "net/proxy_resolution/proxy_config_with_annotation.h"
  19. #include "third_party/abseil-cpp/absl/types/optional.h"
  20. namespace base {
  21. class SingleThreadTaskRunner;
  22. class SequencedTaskRunner;
  23. } // namespace base
  24. namespace net {
  25. // Implementation of ProxyConfigService that retrieves the system proxy
  26. // settings from environment variables, gconf, gsettings, or kioslaverc (KDE).
  27. class NET_EXPORT_PRIVATE ProxyConfigServiceLinux : public ProxyConfigService {
  28. public:
  29. class Delegate;
  30. class SettingGetter {
  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. SettingGetter() = default;
  36. SettingGetter(const SettingGetter&) = delete;
  37. SettingGetter& operator=(const SettingGetter&) = delete;
  38. virtual ~SettingGetter() = default;
  39. // Initializes the class: obtains a gconf/gsettings client, or simulates
  40. // one, in the concrete implementations. Returns true on success. Must be
  41. // called before using other methods, and should be called on the thread
  42. // running the glib main loop.
  43. // This interface supports both GNOME and KDE implementations. In the
  44. // case of GNOME, the glib_task_runner will be used for interacting with
  45. // gconf/gsettings as those APIs have thread affinity. Whereas in the case
  46. // of KDE, its configuration files will be monitored at well-known locations
  47. // and glib_task_runner will not be used. Instead, blocking file I/O
  48. // operations will be posted directly using the task scheduler.
  49. virtual bool Init(const scoped_refptr<base::SingleThreadTaskRunner>&
  50. glib_task_runner) = 0;
  51. // Releases the gconf/gsettings client, which clears cached directories and
  52. // stops notifications.
  53. virtual void ShutDown() = 0;
  54. // Requests notification of gconf/gsettings changes for proxy
  55. // settings. Returns true on success.
  56. virtual bool SetUpNotifications(Delegate* delegate) = 0;
  57. // Returns the message loop for the thread on which this object
  58. // handles notifications, and also on which it must be destroyed.
  59. // Returns NULL if it does not matter.
  60. virtual const scoped_refptr<base::SequencedTaskRunner>&
  61. GetNotificationTaskRunner() = 0;
  62. // These are all the values that can be fetched. We used to just use the
  63. // corresponding paths in gconf for these, but gconf is now obsolete and
  64. // in the future we'll be using mostly gsettings/kioslaverc so we
  65. // enumerate them instead to avoid unnecessary string operations.
  66. enum StringSetting {
  67. PROXY_MODE,
  68. PROXY_AUTOCONF_URL,
  69. PROXY_HTTP_HOST,
  70. PROXY_HTTPS_HOST,
  71. PROXY_FTP_HOST,
  72. PROXY_SOCKS_HOST,
  73. };
  74. enum BoolSetting {
  75. PROXY_USE_HTTP_PROXY,
  76. PROXY_USE_SAME_PROXY,
  77. PROXY_USE_AUTHENTICATION,
  78. };
  79. enum IntSetting {
  80. PROXY_HTTP_PORT,
  81. PROXY_HTTPS_PORT,
  82. PROXY_FTP_PORT,
  83. PROXY_SOCKS_PORT,
  84. };
  85. enum StringListSetting {
  86. PROXY_IGNORE_HOSTS,
  87. };
  88. // Given a PROXY_*_HOST value, return the corresponding PROXY_*_PORT value.
  89. static IntSetting HostSettingToPortSetting(StringSetting host) {
  90. switch (host) {
  91. case PROXY_HTTP_HOST:
  92. return PROXY_HTTP_PORT;
  93. case PROXY_HTTPS_HOST:
  94. return PROXY_HTTPS_PORT;
  95. case PROXY_FTP_HOST:
  96. return PROXY_FTP_PORT;
  97. case PROXY_SOCKS_HOST:
  98. return PROXY_SOCKS_PORT;
  99. default:
  100. NOTREACHED();
  101. return PROXY_HTTP_PORT; // Placate compiler.
  102. }
  103. }
  104. // Gets a string type value from the data source and stores it in
  105. // |*result|. Returns false if the key is unset or on error. Must only be
  106. // called after a successful call to Init(), and not after a failed call
  107. // to SetUpNotifications() or after calling Release().
  108. virtual bool GetString(StringSetting key, std::string* result) = 0;
  109. // Same thing for a bool typed value.
  110. virtual bool GetBool(BoolSetting key, bool* result) = 0;
  111. // Same for an int typed value.
  112. virtual bool GetInt(IntSetting key, int* result) = 0;
  113. // And for a string list.
  114. virtual bool GetStringList(StringListSetting key,
  115. std::vector<std::string>* result) = 0;
  116. // Returns true if the bypass list should be interpreted as a proxy
  117. // allow list rather than block list. (This is KDE-specific.)
  118. virtual bool BypassListIsReversed() = 0;
  119. // Returns true if bypass rules should evaluate using dumb string suffix
  120. // matches on the host. For instance when true, "notgoogle.com" will be
  121. // considered a match for "google.com", even though the bypass rule does not
  122. // include a wildcard, and the matched host is not a subdomain.
  123. virtual bool UseSuffixMatching() = 0;
  124. };
  125. // ProxyConfigServiceLinux is created on the glib thread, and
  126. // SetUpAndFetchInitialConfig() is immediately called to synchronously
  127. // fetch the original configuration and set up change notifications on
  128. // the ProxyConfigService's main SequencedTaskRunner, which is passed to its
  129. // constructor (Which may or may not run tasks on the glib thread).
  130. //
  131. // Past that point, it is accessed periodically through the
  132. // ProxyConfigService interface (GetLatestProxyConfig, AddObserver,
  133. // RemoveObserver) from the main TaskRunner.
  134. //
  135. // Setting change notification callbacks can occur at any time and are
  136. // run on either the glib thread (gconf/gsettings) or a separate file thread
  137. // (KDE). The new settings are fetched on that thread, and the resulting proxy
  138. // config is posted to the main TaskRunner through
  139. // Delegate::SetNewProxyConfig(). We then notify observers on that TaskRunner
  140. // of the configuration change.
  141. //
  142. // ProxyConfigServiceLinux is deleted from the main TaskRunner.
  143. //
  144. // The substance of the ProxyConfigServiceLinux implementation is
  145. // wrapped in the Delegate ref counted class. On deleting the
  146. // ProxyConfigServiceLinux, Delegate::OnDestroy() is posted to either
  147. // the glib thread (gconf/gsettings) or a file thread (KDE) where change
  148. // notifications will be safely stopped before releasing Delegate.
  149. class Delegate : public base::RefCountedThreadSafe<Delegate> {
  150. public:
  151. // Test code can set |setting_getter| and |traffic_annotation|. If left
  152. // unspecified, reasonable defaults will be used.
  153. Delegate(std::unique_ptr<base::Environment> env_var_getter,
  154. absl::optional<std::unique_ptr<SettingGetter>> setting_getter,
  155. absl::optional<NetworkTrafficAnnotationTag> traffic_annotation);
  156. Delegate(const Delegate&) = delete;
  157. Delegate& operator=(const Delegate&) = delete;
  158. // Synchronously obtains the proxy configuration. If gconf,
  159. // gsettings, or kioslaverc are used, also enables notifications for
  160. // setting changes. gconf/gsettings must only be accessed from the
  161. // thread running the default glib main loop, and so this method
  162. // must be called from the glib thread. The message loop for the
  163. // ProxyConfigService's main SequencedTaskRunner is specified so that
  164. // notifications can post tasks to it (and for assertions).
  165. void SetUpAndFetchInitialConfig(
  166. const scoped_refptr<base::SingleThreadTaskRunner>& glib_task_runner,
  167. const scoped_refptr<base::SequencedTaskRunner>& main_task_runner,
  168. const NetworkTrafficAnnotationTag& traffic_annotation);
  169. // Handler for setting change notifications: fetches a new proxy
  170. // configuration from settings, and if this config is different
  171. // than what we had before, posts a task to have it stored in
  172. // cached_config_.
  173. // Left public for simplicity.
  174. void OnCheckProxyConfigSettings();
  175. // Called on the service's main TaskRunner.
  176. void AddObserver(Observer* observer);
  177. void RemoveObserver(Observer* observer);
  178. ProxyConfigService::ConfigAvailability GetLatestProxyConfig(
  179. ProxyConfigWithAnnotation* config);
  180. // Posts a call to OnDestroy() to the glib or a file task runner,
  181. // depending on the setting getter in use. Called from
  182. // ProxyConfigServiceLinux's destructor.
  183. void PostDestroyTask();
  184. // Safely stops change notifications. Posted to either the glib thread or
  185. // sequenced task runner, depending on the setting getter in use.
  186. void OnDestroy();
  187. private:
  188. friend class base::RefCountedThreadSafe<Delegate>;
  189. ~Delegate();
  190. // Obtains an environment variable's value. Parses a proxy server
  191. // specification from it and puts it in result. Returns true if the
  192. // requested variable is defined and the value valid.
  193. bool GetProxyFromEnvVarForScheme(base::StringPiece variable,
  194. ProxyServer::Scheme scheme,
  195. ProxyServer* result_server);
  196. // As above but with scheme set to HTTP, for convenience.
  197. bool GetProxyFromEnvVar(base::StringPiece variable,
  198. ProxyServer* result_server);
  199. // Returns a proxy config based on the environment variables, or empty value
  200. // on failure.
  201. absl::optional<ProxyConfigWithAnnotation> GetConfigFromEnv();
  202. // Obtains host and port config settings and parses a proxy server
  203. // specification from it and puts it in result. Returns true if the
  204. // requested variable is defined and the value valid.
  205. bool GetProxyFromSettings(SettingGetter::StringSetting host_key,
  206. ProxyServer* result_server);
  207. // Returns a proxy config based on the settings, or empty value
  208. // on failure.
  209. absl::optional<ProxyConfigWithAnnotation> GetConfigFromSettings();
  210. // This method is posted from the glib thread to the main TaskRunner to
  211. // carry the new config information.
  212. void SetNewProxyConfig(
  213. const absl::optional<ProxyConfigWithAnnotation>& new_config);
  214. // This method is run on the getter's notification thread.
  215. void SetUpNotifications();
  216. std::unique_ptr<base::Environment> env_var_getter_;
  217. std::unique_ptr<SettingGetter> setting_getter_;
  218. // Cached proxy configuration, to be returned by
  219. // GetLatestProxyConfig. Initially populated from the glib thread, but
  220. // afterwards only accessed from the main TaskRunner.
  221. absl::optional<ProxyConfigWithAnnotation> cached_config_;
  222. // A copy kept on the glib thread of the last seen proxy config, so as
  223. // to avoid posting a call to SetNewProxyConfig when we get a
  224. // notification but the config has not actually changed.
  225. absl::optional<ProxyConfigWithAnnotation> reference_config_;
  226. // The task runner for the glib thread, aka main browser thread. This thread
  227. // is where we run the glib main loop (see
  228. // base/message_loop/message_pump_glib.h). It is the glib default loop in
  229. // the sense that it runs the glib default context: as in the context where
  230. // sources are added by g_timeout_add and g_idle_add, and returned by
  231. // g_main_context_default. gconf uses glib timeouts and idles and possibly
  232. // other callbacks that will all be dispatched on this thread. Since gconf
  233. // is not thread safe, any use of gconf must be done on the thread running
  234. // this loop.
  235. scoped_refptr<base::SingleThreadTaskRunner> glib_task_runner_;
  236. // Task runner for the main TaskRunner. GetLatestProxyConfig() is called
  237. // from the thread running this loop.
  238. scoped_refptr<base::SequencedTaskRunner> main_task_runner_;
  239. base::ObserverList<Observer>::Unchecked observers_;
  240. MutableNetworkTrafficAnnotationTag traffic_annotation_;
  241. };
  242. // Thin wrapper shell around Delegate.
  243. // Usual constructor
  244. ProxyConfigServiceLinux();
  245. // For testing: take alternate setting and env var getter implementations.
  246. explicit ProxyConfigServiceLinux(
  247. std::unique_ptr<base::Environment> env_var_getter,
  248. const NetworkTrafficAnnotationTag& traffic_annotation);
  249. ProxyConfigServiceLinux(
  250. std::unique_ptr<base::Environment> env_var_getter,
  251. std::unique_ptr<SettingGetter> setting_getter,
  252. const NetworkTrafficAnnotationTag& traffic_annotation);
  253. ProxyConfigServiceLinux(const ProxyConfigServiceLinux&) = delete;
  254. ProxyConfigServiceLinux& operator=(const ProxyConfigServiceLinux&) = delete;
  255. ~ProxyConfigServiceLinux() override;
  256. void SetupAndFetchInitialConfig(
  257. const scoped_refptr<base::SingleThreadTaskRunner>& glib_task_runner,
  258. const scoped_refptr<base::SequencedTaskRunner>& main_task_runner,
  259. const NetworkTrafficAnnotationTag& traffic_annotation) {
  260. delegate_->SetUpAndFetchInitialConfig(glib_task_runner, main_task_runner,
  261. traffic_annotation);
  262. }
  263. void OnCheckProxyConfigSettings() {
  264. delegate_->OnCheckProxyConfigSettings();
  265. }
  266. // ProxyConfigService methods:
  267. // Called from main TaskRunner.
  268. void AddObserver(Observer* observer) override;
  269. void RemoveObserver(Observer* observer) override;
  270. ProxyConfigService::ConfigAvailability GetLatestProxyConfig(
  271. ProxyConfigWithAnnotation* config) override;
  272. private:
  273. scoped_refptr<Delegate> delegate_;
  274. };
  275. } // namespace net
  276. #endif // NET_PROXY_RESOLUTION_PROXY_CONFIG_SERVICE_LINUX_H_