blob: ea2ec20017e667e0dca35c6595e4797e0702dfe1 [file] [log] [blame]
[email protected]d2e6d592012-02-03 21:49:041// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]861c6c62009-04-20 16:50:562// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "net/proxy/proxy_config_service_linux.h"
6
[email protected]d7395e732009-08-28 23:13:437#include <errno.h>
[email protected]6de53d42010-11-09 07:33:198#if defined(USE_GCONF)
[email protected]861c6c62009-04-20 16:50:569#include <gconf/gconf-client.h>
[email protected]8c20e3d2011-05-19 21:03:5710#endif // defined(USE_GCONF)
[email protected]d7395e732009-08-28 23:13:4311#include <limits.h>
12#include <stdio.h>
[email protected]861c6c62009-04-20 16:50:5613#include <stdlib.h>
[email protected]d7395e732009-08-28 23:13:4314#include <sys/inotify.h>
15#include <unistd.h>
[email protected]861c6c62009-04-20 16:50:5616
[email protected]9bc8cff2010-04-03 01:05:3917#include <map>
18
[email protected]6af889c2011-10-06 23:11:4119#include "base/bind.h"
[email protected]c4c1b482011-07-22 17:24:2620#include "base/compiler_specific.h"
[email protected]4bbb72d2014-06-06 18:05:5121#include "base/debug/leak_annotations.h"
[email protected]76b90d312010-08-03 03:00:5022#include "base/environment.h"
[email protected]57999812013-02-24 05:40:5223#include "base/files/file_path.h"
thestigd8df0332014-09-04 06:33:2924#include "base/files/file_util.h"
[email protected]b9b4a572014-03-17 23:11:1225#include "base/files/scoped_file.h"
[email protected]861c6c62009-04-20 16:50:5626#include "base/logging.h"
[email protected]18b577412013-07-18 04:19:1527#include "base/message_loop/message_loop.h"
[email protected]3a29593d2011-04-11 10:07:5228#include "base/nix/xdg_util.h"
[email protected]76722472012-05-24 08:26:4629#include "base/single_thread_task_runner.h"
[email protected]fc9be5802013-06-11 10:56:5130#include "base/strings/string_number_conversions.h"
brettw8cc24ae22015-07-06 23:53:0031#include "base/strings/string_split.h"
[email protected]f4ebe772013-02-02 00:21:3932#include "base/strings/string_tokenizer.h"
[email protected]66e96c42013-06-28 15:20:3133#include "base/strings/string_util.h"
[email protected]9a8c4022011-01-25 14:25:3334#include "base/threading/thread_restrictions.h"
[email protected]66e96c42013-06-28 15:20:3135#include "base/timer/timer.h"
[email protected]861c6c62009-04-20 16:50:5636#include "net/base/net_errors.h"
37#include "net/http/http_util.h"
38#include "net/proxy/proxy_config.h"
39#include "net/proxy/proxy_server.h"
[email protected]f89276a72013-07-12 06:41:5440#include "url/url_canon.h"
[email protected]861c6c62009-04-20 16:50:5641
[email protected]3fc24f52012-11-30 21:22:3442#if defined(USE_GIO)
43#include "library_loaders/libgio.h"
44#endif // defined(USE_GIO)
45
[email protected]861c6c62009-04-20 16:50:5646namespace net {
47
48namespace {
49
[email protected]861c6c62009-04-20 16:50:5650// Given a proxy hostname from a setting, returns that hostname with
51// an appropriate proxy server scheme prefix.
52// scheme indicates the desired proxy scheme: usually http, with
53// socks 4 or 5 as special cases.
[email protected]87a102b2009-07-14 05:23:3054// TODO(arindam): Remove URI string manipulation by using MapUrlSchemeToProxy.
[email protected]861c6c62009-04-20 16:50:5655std::string FixupProxyHostScheme(ProxyServer::Scheme scheme,
56 std::string host) {
[email protected]e8c50812010-09-28 00:16:1757 if (scheme == ProxyServer::SCHEME_SOCKS5 &&
brettw3a2c6902015-07-06 19:43:2958 base::StartsWith(host, "socks4://",
59 base::CompareCase::INSENSITIVE_ASCII)) {
[email protected]e8c50812010-09-28 00:16:1760 // We default to socks 5, but if the user specifically set it to
61 // socks4://, then use that.
62 scheme = ProxyServer::SCHEME_SOCKS4;
[email protected]861c6c62009-04-20 16:50:5663 }
64 // Strip the scheme if any.
65 std::string::size_type colon = host.find("://");
66 if (colon != std::string::npos)
67 host = host.substr(colon + 3);
68 // If a username and perhaps password are specified, give a warning.
69 std::string::size_type at_sign = host.find("@");
70 // Should this be supported?
71 if (at_sign != std::string::npos) {
[email protected]62749f182009-07-15 13:16:5472 // ProxyConfig does not support authentication parameters, but Chrome
73 // will prompt for the password later. Disregard the
74 // authentication parameters and continue with this hostname.
75 LOG(WARNING) << "Proxy authentication parameters ignored, see bug 16709";
[email protected]861c6c62009-04-20 16:50:5676 host = host.substr(at_sign + 1);
77 }
78 // If this is a socks proxy, prepend a scheme so as to tell
79 // ProxyServer. This also allows ProxyServer to choose the right
80 // default port.
81 if (scheme == ProxyServer::SCHEME_SOCKS4)
82 host = "socks4://" + host;
83 else if (scheme == ProxyServer::SCHEME_SOCKS5)
84 host = "socks5://" + host;
[email protected]d7395e732009-08-28 23:13:4385 // If there is a trailing slash, remove it so |host| will parse correctly
86 // even if it includes a port number (since the slash is not numeric).
87 if (host.length() && host[host.length() - 1] == '/')
88 host.resize(host.length() - 1);
[email protected]861c6c62009-04-20 16:50:5689 return host;
90}
91
92} // namespace
93
[email protected]8e1845e12010-09-15 19:22:2494ProxyConfigServiceLinux::Delegate::~Delegate() {
95}
96
[email protected]3e44697f2009-05-22 14:37:3997bool ProxyConfigServiceLinux::Delegate::GetProxyFromEnvVarForScheme(
[email protected]861c6c62009-04-20 16:50:5698 const char* variable, ProxyServer::Scheme scheme,
99 ProxyServer* result_server) {
100 std::string env_value;
[email protected]3ba7e082010-08-07 02:57:59101 if (env_var_getter_->GetVar(variable, &env_value)) {
[email protected]861c6c62009-04-20 16:50:56102 if (!env_value.empty()) {
103 env_value = FixupProxyHostScheme(scheme, env_value);
[email protected]87a102b2009-07-14 05:23:30104 ProxyServer proxy_server =
105 ProxyServer::FromURI(env_value, ProxyServer::SCHEME_HTTP);
[email protected]861c6c62009-04-20 16:50:56106 if (proxy_server.is_valid() && !proxy_server.is_direct()) {
107 *result_server = proxy_server;
108 return true;
109 } else {
[email protected]3e44697f2009-05-22 14:37:39110 LOG(ERROR) << "Failed to parse environment variable " << variable;
[email protected]861c6c62009-04-20 16:50:56111 }
112 }
113 }
114 return false;
115}
116
[email protected]3e44697f2009-05-22 14:37:39117bool ProxyConfigServiceLinux::Delegate::GetProxyFromEnvVar(
[email protected]861c6c62009-04-20 16:50:56118 const char* variable, ProxyServer* result_server) {
119 return GetProxyFromEnvVarForScheme(variable, ProxyServer::SCHEME_HTTP,
120 result_server);
121}
122
[email protected]3e44697f2009-05-22 14:37:39123bool ProxyConfigServiceLinux::Delegate::GetConfigFromEnv(ProxyConfig* config) {
[email protected]861c6c62009-04-20 16:50:56124 // Check for automatic configuration first, in
125 // "auto_proxy". Possibly only the "environment_proxy" firefox
126 // extension has ever used this, but it still sounds like a good
127 // idea.
128 std::string auto_proxy;
[email protected]3ba7e082010-08-07 02:57:59129 if (env_var_getter_->GetVar("auto_proxy", &auto_proxy)) {
[email protected]861c6c62009-04-20 16:50:56130 if (auto_proxy.empty()) {
131 // Defined and empty => autodetect
[email protected]ed4ed0f2010-02-24 00:20:48132 config->set_auto_detect(true);
[email protected]861c6c62009-04-20 16:50:56133 } else {
134 // specified autoconfig URL
[email protected]ed4ed0f2010-02-24 00:20:48135 config->set_pac_url(GURL(auto_proxy));
[email protected]861c6c62009-04-20 16:50:56136 }
137 return true;
138 }
139 // "all_proxy" is a shortcut to avoid defining {http,https,ftp}_proxy.
140 ProxyServer proxy_server;
141 if (GetProxyFromEnvVar("all_proxy", &proxy_server)) {
[email protected]ed4ed0f2010-02-24 00:20:48142 config->proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY;
[email protected]2189e092013-03-16 18:02:02143 config->proxy_rules().single_proxies.SetSingleProxyServer(proxy_server);
[email protected]861c6c62009-04-20 16:50:56144 } else {
145 bool have_http = GetProxyFromEnvVar("http_proxy", &proxy_server);
146 if (have_http)
[email protected]2189e092013-03-16 18:02:02147 config->proxy_rules().proxies_for_http.SetSingleProxyServer(proxy_server);
[email protected]861c6c62009-04-20 16:50:56148 // It would be tempting to let http_proxy apply for all protocols
149 // if https_proxy and ftp_proxy are not defined. Googling turns up
150 // several documents that mention only http_proxy. But then the
151 // user really might not want to proxy https. And it doesn't seem
152 // like other apps do this. So we will refrain.
153 bool have_https = GetProxyFromEnvVar("https_proxy", &proxy_server);
154 if (have_https)
[email protected]2189e092013-03-16 18:02:02155 config->proxy_rules().proxies_for_https.
156 SetSingleProxyServer(proxy_server);
[email protected]861c6c62009-04-20 16:50:56157 bool have_ftp = GetProxyFromEnvVar("ftp_proxy", &proxy_server);
158 if (have_ftp)
[email protected]2189e092013-03-16 18:02:02159 config->proxy_rules().proxies_for_ftp.SetSingleProxyServer(proxy_server);
[email protected]861c6c62009-04-20 16:50:56160 if (have_http || have_https || have_ftp) {
161 // mustn't change type unless some rules are actually set.
[email protected]ed4ed0f2010-02-24 00:20:48162 config->proxy_rules().type =
163 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME;
[email protected]861c6c62009-04-20 16:50:56164 }
165 }
[email protected]ed4ed0f2010-02-24 00:20:48166 if (config->proxy_rules().empty()) {
[email protected]861c6c62009-04-20 16:50:56167 // If the above were not defined, try for socks.
[email protected]e8c50812010-09-28 00:16:17168 // For environment variables, we default to version 5, per the gnome
169 // documentation: http://library.gnome.org/devel/gnet/stable/gnet-socks.html
170 ProxyServer::Scheme scheme = ProxyServer::SCHEME_SOCKS5;
[email protected]861c6c62009-04-20 16:50:56171 std::string env_version;
[email protected]3ba7e082010-08-07 02:57:59172 if (env_var_getter_->GetVar("SOCKS_VERSION", &env_version)
[email protected]e8c50812010-09-28 00:16:17173 && env_version == "4")
174 scheme = ProxyServer::SCHEME_SOCKS4;
[email protected]861c6c62009-04-20 16:50:56175 if (GetProxyFromEnvVarForScheme("SOCKS_SERVER", scheme, &proxy_server)) {
[email protected]ed4ed0f2010-02-24 00:20:48176 config->proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY;
[email protected]2189e092013-03-16 18:02:02177 config->proxy_rules().single_proxies.SetSingleProxyServer(proxy_server);
[email protected]861c6c62009-04-20 16:50:56178 }
179 }
180 // Look for the proxy bypass list.
181 std::string no_proxy;
[email protected]3ba7e082010-08-07 02:57:59182 env_var_getter_->GetVar("no_proxy", &no_proxy);
[email protected]ed4ed0f2010-02-24 00:20:48183 if (config->proxy_rules().empty()) {
[email protected]861c6c62009-04-20 16:50:56184 // Having only "no_proxy" set, presumably to "*", makes it
185 // explicit that env vars do specify a configuration: having no
186 // rules specified only means the user explicitly asks for direct
187 // connections.
188 return !no_proxy.empty();
189 }
[email protected]7541206c2010-02-19 20:24:06190 // Note that this uses "suffix" matching. So a bypass of "google.com"
191 // is understood to mean a bypass of "*google.com".
[email protected]ed4ed0f2010-02-24 00:20:48192 config->proxy_rules().bypass_rules.ParseFromStringUsingSuffixMatching(
193 no_proxy);
[email protected]861c6c62009-04-20 16:50:56194 return true;
195}
196
197namespace {
198
[email protected]d7395e732009-08-28 23:13:43199const int kDebounceTimeoutMilliseconds = 250;
[email protected]3e44697f2009-05-22 14:37:39200
[email protected]6de53d42010-11-09 07:33:19201#if defined(USE_GCONF)
[email protected]573c0502011-05-17 22:19:50202// This setting getter uses gconf, as used in GNOME 2 and some GNOME 3 desktops.
203class SettingGetterImplGConf : public ProxyConfigServiceLinux::SettingGetter {
[email protected]861c6c62009-04-20 16:50:56204 public:
[email protected]573c0502011-05-17 22:19:50205 SettingGetterImplGConf()
marshall8e5fe942015-03-06 19:22:40206 : client_(NULL),
207 system_proxy_id_(0),
208 system_http_proxy_id_(0),
209 notify_delegate_(NULL),
danakj8c3eb802015-09-24 07:53:00210 debounce_timer_(new base::OneShotTimer()) {}
[email protected]3e44697f2009-05-22 14:37:39211
dcheng67be2b1f2014-10-27 21:47:29212 ~SettingGetterImplGConf() override {
[email protected]3e44697f2009-05-22 14:37:39213 // client_ should have been released before now, from
[email protected]f5b13442009-07-13 15:23:59214 // Delegate::OnDestroy(), while running on the UI thread. However
[email protected]b160df32012-02-06 20:39:41215 // on exiting the process, it may happen that Delegate::OnDestroy()
216 // task is left pending on the glib loop after the loop was quit,
217 // and pending tasks may then be deleted without being run.
[email protected]f5b13442009-07-13 15:23:59218 if (client_) {
219 // gconf client was not cleaned up.
[email protected]76722472012-05-24 08:26:46220 if (task_runner_->BelongsToCurrentThread()) {
[email protected]f5b13442009-07-13 15:23:59221 // We are on the UI thread so we can clean it safely. This is
222 // the case at least for ui_tests running under Valgrind in
223 // bug 16076.
[email protected]573c0502011-05-17 22:19:50224 VLOG(1) << "~SettingGetterImplGConf: releasing gconf client";
[email protected]d3066142011-05-10 02:36:20225 ShutDown();
[email protected]f5b13442009-07-13 15:23:59226 } else {
[email protected]ed348992011-09-19 20:27:57227 // This is very bad! We are deleting the setting getter but we're not on
228 // the UI thread. This is not supposed to happen: the setting getter is
229 // owned by the proxy config service's delegate, which is supposed to be
230 // destroyed on the UI thread only. We will get change notifications to
231 // a deleted object if we continue here, so fail now.
232 LOG(FATAL) << "~SettingGetterImplGConf: deleting on wrong thread!";
[email protected]f5b13442009-07-13 15:23:59233 }
234 }
[email protected]3e44697f2009-05-22 14:37:39235 DCHECK(!client_);
[email protected]861c6c62009-04-20 16:50:56236 }
237
dcheng67be2b1f2014-10-27 21:47:29238 bool Init(const scoped_refptr<base::SingleThreadTaskRunner>& glib_task_runner,
239 const scoped_refptr<base::SingleThreadTaskRunner>& file_task_runner)
mostynbba063d6032014-10-09 11:01:13240 override {
sergeyu3f923062014-09-05 01:39:40241 DCHECK(glib_task_runner->BelongsToCurrentThread());
[email protected]3e44697f2009-05-22 14:37:39242 DCHECK(!client_);
[email protected]90499482013-06-01 00:39:50243 DCHECK(!task_runner_.get());
sergeyu3f923062014-09-05 01:39:40244 task_runner_ = glib_task_runner;
[email protected]3e44697f2009-05-22 14:37:39245 client_ = gconf_client_get_default();
[email protected]861c6c62009-04-20 16:50:56246 if (!client_) {
[email protected]861c6c62009-04-20 16:50:56247 // It's not clear whether/when this can return NULL.
[email protected]3e44697f2009-05-22 14:37:39248 LOG(ERROR) << "Unable to create a gconf client";
[email protected]76722472012-05-24 08:26:46249 task_runner_ = NULL;
[email protected]3e44697f2009-05-22 14:37:39250 return false;
[email protected]861c6c62009-04-20 16:50:56251 }
[email protected]3e44697f2009-05-22 14:37:39252 GError* error = NULL;
[email protected]b160df32012-02-06 20:39:41253 bool added_system_proxy = false;
[email protected]3e44697f2009-05-22 14:37:39254 // We need to add the directories for which we'll be asking
[email protected]b160df32012-02-06 20:39:41255 // for notifications, and we might as well ask to preload them.
256 // These need to be removed again in ShutDown(); we are careful
257 // here to only leave client_ non-NULL if both have been added.
[email protected]3e44697f2009-05-22 14:37:39258 gconf_client_add_dir(client_, "/system/proxy",
259 GCONF_CLIENT_PRELOAD_ONELEVEL, &error);
260 if (error == NULL) {
[email protected]b160df32012-02-06 20:39:41261 added_system_proxy = true;
[email protected]3e44697f2009-05-22 14:37:39262 gconf_client_add_dir(client_, "/system/http_proxy",
263 GCONF_CLIENT_PRELOAD_ONELEVEL, &error);
264 }
265 if (error != NULL) {
266 LOG(ERROR) << "Error requesting gconf directory: " << error->message;
267 g_error_free(error);
[email protected]b160df32012-02-06 20:39:41268 if (added_system_proxy)
269 gconf_client_remove_dir(client_, "/system/proxy", NULL);
270 g_object_unref(client_);
271 client_ = NULL;
[email protected]76722472012-05-24 08:26:46272 task_runner_ = NULL;
[email protected]3e44697f2009-05-22 14:37:39273 return false;
274 }
275 return true;
276 }
277
dcheng67be2b1f2014-10-27 21:47:29278 void ShutDown() override {
[email protected]3e44697f2009-05-22 14:37:39279 if (client_) {
[email protected]76722472012-05-24 08:26:46280 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]b160df32012-02-06 20:39:41281 // We must explicitly disable gconf notifications here, because the gconf
282 // client will be shared between all setting getters, and they do not all
283 // have the same lifetimes. (For instance, incognito sessions get their
284 // own, which is destroyed when the session ends.)
285 gconf_client_notify_remove(client_, system_http_proxy_id_);
286 gconf_client_notify_remove(client_, system_proxy_id_);
287 gconf_client_remove_dir(client_, "/system/http_proxy", NULL);
288 gconf_client_remove_dir(client_, "/system/proxy", NULL);
[email protected]3e44697f2009-05-22 14:37:39289 g_object_unref(client_);
290 client_ = NULL;
[email protected]76722472012-05-24 08:26:46291 task_runner_ = NULL;
[email protected]3e44697f2009-05-22 14:37:39292 }
marshall8e5fe942015-03-06 19:22:40293 debounce_timer_.reset();
[email protected]3e44697f2009-05-22 14:37:39294 }
295
dcheng67be2b1f2014-10-27 21:47:29296 bool SetUpNotifications(
mostynbba063d6032014-10-09 11:01:13297 ProxyConfigServiceLinux::Delegate* delegate) override {
[email protected]3e44697f2009-05-22 14:37:39298 DCHECK(client_);
[email protected]76722472012-05-24 08:26:46299 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]3e44697f2009-05-22 14:37:39300 GError* error = NULL;
[email protected]d7395e732009-08-28 23:13:43301 notify_delegate_ = delegate;
[email protected]b160df32012-02-06 20:39:41302 // We have to keep track of the IDs returned by gconf_client_notify_add() so
303 // that we can remove them in ShutDown(). (Otherwise, notifications will be
304 // delivered to this object after it is deleted, which is bad, m'kay?)
305 system_proxy_id_ = gconf_client_notify_add(
[email protected]3e44697f2009-05-22 14:37:39306 client_, "/system/proxy",
[email protected]d7395e732009-08-28 23:13:43307 OnGConfChangeNotification, this,
[email protected]3e44697f2009-05-22 14:37:39308 NULL, &error);
309 if (error == NULL) {
[email protected]b160df32012-02-06 20:39:41310 system_http_proxy_id_ = gconf_client_notify_add(
[email protected]3e44697f2009-05-22 14:37:39311 client_, "/system/http_proxy",
[email protected]d7395e732009-08-28 23:13:43312 OnGConfChangeNotification, this,
[email protected]3e44697f2009-05-22 14:37:39313 NULL, &error);
314 }
315 if (error != NULL) {
316 LOG(ERROR) << "Error requesting gconf notifications: " << error->message;
317 g_error_free(error);
[email protected]d3066142011-05-10 02:36:20318 ShutDown();
[email protected]3e44697f2009-05-22 14:37:39319 return false;
320 }
[email protected]d3066142011-05-10 02:36:20321 // Simulate a change to avoid possibly losing updates before this point.
322 OnChangeNotification();
[email protected]3e44697f2009-05-22 14:37:39323 return true;
[email protected]861c6c62009-04-20 16:50:56324 }
325
dcheng67be2b1f2014-10-27 21:47:29326 const scoped_refptr<base::SingleThreadTaskRunner>& GetNotificationTaskRunner()
327 override {
sergeyu3f923062014-09-05 01:39:40328 return task_runner_;
[email protected]d7395e732009-08-28 23:13:43329 }
330
dcheng67be2b1f2014-10-27 21:47:29331 ProxyConfigSource GetConfigSource() override {
[email protected]db8ff912012-06-12 23:32:51332 return PROXY_CONFIG_SOURCE_GCONF;
[email protected]d7395e732009-08-28 23:13:43333 }
334
dcheng67be2b1f2014-10-27 21:47:29335 bool GetString(StringSetting key, std::string* result) override {
[email protected]573c0502011-05-17 22:19:50336 switch (key) {
337 case PROXY_MODE:
338 return GetStringByPath("/system/proxy/mode", result);
339 case PROXY_AUTOCONF_URL:
340 return GetStringByPath("/system/proxy/autoconfig_url", result);
341 case PROXY_HTTP_HOST:
342 return GetStringByPath("/system/http_proxy/host", result);
343 case PROXY_HTTPS_HOST:
344 return GetStringByPath("/system/proxy/secure_host", result);
345 case PROXY_FTP_HOST:
346 return GetStringByPath("/system/proxy/ftp_host", result);
347 case PROXY_SOCKS_HOST:
348 return GetStringByPath("/system/proxy/socks_host", result);
[email protected]573c0502011-05-17 22:19:50349 }
[email protected]6b5fe742011-05-20 21:46:48350 return false; // Placate compiler.
[email protected]573c0502011-05-17 22:19:50351 }
dcheng67be2b1f2014-10-27 21:47:29352 bool GetBool(BoolSetting key, bool* result) override {
[email protected]573c0502011-05-17 22:19:50353 switch (key) {
354 case PROXY_USE_HTTP_PROXY:
355 return GetBoolByPath("/system/http_proxy/use_http_proxy", result);
356 case PROXY_USE_SAME_PROXY:
357 return GetBoolByPath("/system/http_proxy/use_same_proxy", result);
358 case PROXY_USE_AUTHENTICATION:
359 return GetBoolByPath("/system/http_proxy/use_authentication", result);
[email protected]573c0502011-05-17 22:19:50360 }
[email protected]6b5fe742011-05-20 21:46:48361 return false; // Placate compiler.
[email protected]573c0502011-05-17 22:19:50362 }
dcheng67be2b1f2014-10-27 21:47:29363 bool GetInt(IntSetting key, int* result) override {
[email protected]573c0502011-05-17 22:19:50364 switch (key) {
365 case PROXY_HTTP_PORT:
366 return GetIntByPath("/system/http_proxy/port", result);
367 case PROXY_HTTPS_PORT:
368 return GetIntByPath("/system/proxy/secure_port", result);
369 case PROXY_FTP_PORT:
370 return GetIntByPath("/system/proxy/ftp_port", result);
371 case PROXY_SOCKS_PORT:
372 return GetIntByPath("/system/proxy/socks_port", result);
[email protected]573c0502011-05-17 22:19:50373 }
[email protected]6b5fe742011-05-20 21:46:48374 return false; // Placate compiler.
[email protected]573c0502011-05-17 22:19:50375 }
dcheng67be2b1f2014-10-27 21:47:29376 bool GetStringList(StringListSetting key,
377 std::vector<std::string>* result) override {
[email protected]573c0502011-05-17 22:19:50378 switch (key) {
379 case PROXY_IGNORE_HOSTS:
380 return GetStringListByPath("/system/http_proxy/ignore_hosts", result);
[email protected]573c0502011-05-17 22:19:50381 }
[email protected]6b5fe742011-05-20 21:46:48382 return false; // Placate compiler.
[email protected]573c0502011-05-17 22:19:50383 }
384
dcheng67be2b1f2014-10-27 21:47:29385 bool BypassListIsReversed() override {
[email protected]573c0502011-05-17 22:19:50386 // This is a KDE-specific setting.
387 return false;
388 }
389
dcheng67be2b1f2014-10-27 21:47:29390 bool MatchHostsUsingSuffixMatching() override { return false; }
[email protected]573c0502011-05-17 22:19:50391
392 private:
393 bool GetStringByPath(const char* key, std::string* result) {
[email protected]3e44697f2009-05-22 14:37:39394 DCHECK(client_);
[email protected]76722472012-05-24 08:26:46395 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]861c6c62009-04-20 16:50:56396 GError* error = NULL;
397 gchar* value = gconf_client_get_string(client_, key, &error);
398 if (HandleGError(error, key))
399 return false;
400 if (!value)
401 return false;
402 *result = value;
403 g_free(value);
404 return true;
405 }
[email protected]573c0502011-05-17 22:19:50406 bool GetBoolByPath(const char* key, bool* result) {
[email protected]3e44697f2009-05-22 14:37:39407 DCHECK(client_);
[email protected]76722472012-05-24 08:26:46408 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]861c6c62009-04-20 16:50:56409 GError* error = NULL;
410 // We want to distinguish unset values from values defaulting to
411 // false. For that we need to use the type-generic
412 // gconf_client_get() rather than gconf_client_get_bool().
413 GConfValue* gconf_value = gconf_client_get(client_, key, &error);
414 if (HandleGError(error, key))
415 return false;
416 if (!gconf_value) {
417 // Unset.
418 return false;
419 }
420 if (gconf_value->type != GCONF_VALUE_BOOL) {
421 gconf_value_free(gconf_value);
422 return false;
423 }
424 gboolean bool_value = gconf_value_get_bool(gconf_value);
425 *result = static_cast<bool>(bool_value);
426 gconf_value_free(gconf_value);
427 return true;
428 }
[email protected]573c0502011-05-17 22:19:50429 bool GetIntByPath(const char* key, int* result) {
[email protected]3e44697f2009-05-22 14:37:39430 DCHECK(client_);
[email protected]76722472012-05-24 08:26:46431 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]861c6c62009-04-20 16:50:56432 GError* error = NULL;
433 int value = gconf_client_get_int(client_, key, &error);
434 if (HandleGError(error, key))
435 return false;
436 // We don't bother to distinguish an unset value because callers
437 // don't care. 0 is returned if unset.
438 *result = value;
439 return true;
440 }
[email protected]573c0502011-05-17 22:19:50441 bool GetStringListByPath(const char* key, std::vector<std::string>* result) {
[email protected]3e44697f2009-05-22 14:37:39442 DCHECK(client_);
[email protected]76722472012-05-24 08:26:46443 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]861c6c62009-04-20 16:50:56444 GError* error = NULL;
445 GSList* list = gconf_client_get_list(client_, key,
446 GCONF_VALUE_STRING, &error);
447 if (HandleGError(error, key))
448 return false;
[email protected]8c20e3d2011-05-19 21:03:57449 if (!list)
[email protected]861c6c62009-04-20 16:50:56450 return false;
[email protected]861c6c62009-04-20 16:50:56451 for (GSList *it = list; it; it = it->next) {
452 result->push_back(static_cast<char*>(it->data));
453 g_free(it->data);
454 }
455 g_slist_free(list);
456 return true;
457 }
458
[email protected]861c6c62009-04-20 16:50:56459 // Logs and frees a glib error. Returns false if there was no error
460 // (error is NULL).
461 bool HandleGError(GError* error, const char* key) {
462 if (error != NULL) {
[email protected]3e44697f2009-05-22 14:37:39463 LOG(ERROR) << "Error getting gconf value for " << key
464 << ": " << error->message;
[email protected]861c6c62009-04-20 16:50:56465 g_error_free(error);
466 return true;
467 }
468 return false;
469 }
470
[email protected]d7395e732009-08-28 23:13:43471 // This is the callback from the debounce timer.
472 void OnDebouncedNotification() {
[email protected]76722472012-05-24 08:26:46473 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]961ac942011-04-28 18:18:14474 CHECK(notify_delegate_);
[email protected]d7395e732009-08-28 23:13:43475 // Forward to a method on the proxy config service delegate object.
476 notify_delegate_->OnCheckProxyConfigSettings();
477 }
478
479 void OnChangeNotification() {
480 // We don't use Reset() because the timer may not yet be running.
481 // (In that case Stop() is a no-op.)
marshall8e5fe942015-03-06 19:22:40482 debounce_timer_->Stop();
483 debounce_timer_->Start(FROM_HERE,
[email protected]8c20e3d2011-05-19 21:03:57484 base::TimeDelta::FromMilliseconds(kDebounceTimeoutMilliseconds),
485 this, &SettingGetterImplGConf::OnDebouncedNotification);
[email protected]d7395e732009-08-28 23:13:43486 }
487
[email protected]8c20e3d2011-05-19 21:03:57488 // gconf notification callback, dispatched on the default glib main loop.
489 static void OnGConfChangeNotification(GConfClient* client, guint cnxn_id,
490 GConfEntry* entry, gpointer user_data) {
[email protected]b30a3f52010-10-16 01:05:46491 VLOG(1) << "gconf change notification for key "
492 << gconf_entry_get_key(entry);
[email protected]d7395e732009-08-28 23:13:43493 // We don't track which key has changed, just that something did change.
[email protected]573c0502011-05-17 22:19:50494 SettingGetterImplGConf* setting_getter =
495 reinterpret_cast<SettingGetterImplGConf*>(user_data);
[email protected]d7395e732009-08-28 23:13:43496 setting_getter->OnChangeNotification();
497 }
498
[email protected]861c6c62009-04-20 16:50:56499 GConfClient* client_;
[email protected]b160df32012-02-06 20:39:41500 // These ids are the values returned from gconf_client_notify_add(), which we
501 // will need in order to later call gconf_client_notify_remove().
502 guint system_proxy_id_;
503 guint system_http_proxy_id_;
504
[email protected]d7395e732009-08-28 23:13:43505 ProxyConfigServiceLinux::Delegate* notify_delegate_;
danakj8c3eb802015-09-24 07:53:00506 scoped_ptr<base::OneShotTimer> debounce_timer_;
[email protected]861c6c62009-04-20 16:50:56507
[email protected]76722472012-05-24 08:26:46508 // Task runner for the thread that we make gconf calls on. It should
[email protected]3e44697f2009-05-22 14:37:39509 // be the UI thread and all our methods should be called on this
510 // thread. Only for assertions.
[email protected]76722472012-05-24 08:26:46511 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
[email protected]3e44697f2009-05-22 14:37:39512
[email protected]573c0502011-05-17 22:19:50513 DISALLOW_COPY_AND_ASSIGN(SettingGetterImplGConf);
[email protected]d7395e732009-08-28 23:13:43514};
[email protected]6de53d42010-11-09 07:33:19515#endif // defined(USE_GCONF)
[email protected]d7395e732009-08-28 23:13:43516
[email protected]8c20e3d2011-05-19 21:03:57517#if defined(USE_GIO)
[email protected]2297bb22014-06-19 06:30:14518const char kProxyGConfSchema[] = "org.gnome.system.proxy";
519
[email protected]8c20e3d2011-05-19 21:03:57520// This setting getter uses gsettings, as used in most GNOME 3 desktops.
521class SettingGetterImplGSettings
522 : public ProxyConfigServiceLinux::SettingGetter {
523 public:
danakj8c3eb802015-09-24 07:53:00524 SettingGetterImplGSettings()
525 : client_(NULL),
526 http_client_(NULL),
527 https_client_(NULL),
528 ftp_client_(NULL),
529 socks_client_(NULL),
530 notify_delegate_(NULL),
531 debounce_timer_(new base::OneShotTimer()) {}
[email protected]8c20e3d2011-05-19 21:03:57532
dcheng67be2b1f2014-10-27 21:47:29533 ~SettingGetterImplGSettings() override {
[email protected]8c20e3d2011-05-19 21:03:57534 // client_ should have been released before now, from
535 // Delegate::OnDestroy(), while running on the UI thread. However
536 // on exiting the process, it may happen that
537 // Delegate::OnDestroy() task is left pending on the glib loop
538 // after the loop was quit, and pending tasks may then be deleted
539 // without being run.
540 if (client_) {
541 // gconf client was not cleaned up.
[email protected]76722472012-05-24 08:26:46542 if (task_runner_->BelongsToCurrentThread()) {
[email protected]8c20e3d2011-05-19 21:03:57543 // We are on the UI thread so we can clean it safely. This is
544 // the case at least for ui_tests running under Valgrind in
545 // bug 16076.
546 VLOG(1) << "~SettingGetterImplGSettings: releasing gsettings client";
547 ShutDown();
548 } else {
549 LOG(WARNING) << "~SettingGetterImplGSettings: leaking gsettings client";
550 client_ = NULL;
551 }
552 }
553 DCHECK(!client_);
[email protected]8c20e3d2011-05-19 21:03:57554 }
555
[email protected]4cf80f0b2011-05-20 20:30:26556 bool SchemaExists(const char* schema_name) {
[email protected]3fc24f52012-11-30 21:22:34557 const gchar* const* schemas = libgio_loader_.g_settings_list_schemas();
[email protected]4cf80f0b2011-05-20 20:30:26558 while (*schemas) {
[email protected]a099f3ae2011-08-16 21:06:58559 if (strcmp(schema_name, static_cast<const char*>(*schemas)) == 0)
[email protected]4cf80f0b2011-05-20 20:30:26560 return true;
561 schemas++;
562 }
563 return false;
564 }
565
[email protected]8c20e3d2011-05-19 21:03:57566 // LoadAndCheckVersion() must be called *before* Init()!
567 bool LoadAndCheckVersion(base::Environment* env);
568
dcheng67be2b1f2014-10-27 21:47:29569 bool Init(const scoped_refptr<base::SingleThreadTaskRunner>& glib_task_runner,
570 const scoped_refptr<base::SingleThreadTaskRunner>& file_task_runner)
mostynbba063d6032014-10-09 11:01:13571 override {
sergeyu3f923062014-09-05 01:39:40572 DCHECK(glib_task_runner->BelongsToCurrentThread());
[email protected]8c20e3d2011-05-19 21:03:57573 DCHECK(!client_);
[email protected]90499482013-06-01 00:39:50574 DCHECK(!task_runner_.get());
[email protected]4cf80f0b2011-05-20 20:30:26575
[email protected]4bbb72d2014-06-06 18:05:51576 if (!SchemaExists(kProxyGConfSchema) ||
577 !(client_ = libgio_loader_.g_settings_new(kProxyGConfSchema))) {
[email protected]8c20e3d2011-05-19 21:03:57578 // It's not clear whether/when this can return NULL.
579 LOG(ERROR) << "Unable to create a gsettings client";
580 return false;
581 }
sergeyu3f923062014-09-05 01:39:40582 task_runner_ = glib_task_runner;
[email protected]8c20e3d2011-05-19 21:03:57583 // We assume these all work if the above call worked.
[email protected]3fc24f52012-11-30 21:22:34584 http_client_ = libgio_loader_.g_settings_get_child(client_, "http");
585 https_client_ = libgio_loader_.g_settings_get_child(client_, "https");
586 ftp_client_ = libgio_loader_.g_settings_get_child(client_, "ftp");
587 socks_client_ = libgio_loader_.g_settings_get_child(client_, "socks");
[email protected]8c20e3d2011-05-19 21:03:57588 DCHECK(http_client_ && https_client_ && ftp_client_ && socks_client_);
589 return true;
590 }
591
dcheng67be2b1f2014-10-27 21:47:29592 void ShutDown() override {
[email protected]8c20e3d2011-05-19 21:03:57593 if (client_) {
[email protected]76722472012-05-24 08:26:46594 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]8c20e3d2011-05-19 21:03:57595 // This also disables gsettings notifications.
596 g_object_unref(socks_client_);
597 g_object_unref(ftp_client_);
598 g_object_unref(https_client_);
599 g_object_unref(http_client_);
600 g_object_unref(client_);
601 // We only need to null client_ because it's the only one that we check.
602 client_ = NULL;
[email protected]76722472012-05-24 08:26:46603 task_runner_ = NULL;
[email protected]8c20e3d2011-05-19 21:03:57604 }
marshall8e5fe942015-03-06 19:22:40605 debounce_timer_.reset();
[email protected]8c20e3d2011-05-19 21:03:57606 }
607
dcheng67be2b1f2014-10-27 21:47:29608 bool SetUpNotifications(
mostynbba063d6032014-10-09 11:01:13609 ProxyConfigServiceLinux::Delegate* delegate) override {
[email protected]8c20e3d2011-05-19 21:03:57610 DCHECK(client_);
[email protected]76722472012-05-24 08:26:46611 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]8c20e3d2011-05-19 21:03:57612 notify_delegate_ = delegate;
613 // We could watch for the change-event signal instead of changed, but
614 // since we have to watch more than one object, we'd still have to
615 // debounce change notifications. This is conceptually simpler.
616 g_signal_connect(G_OBJECT(client_), "changed",
617 G_CALLBACK(OnGSettingsChangeNotification), this);
618 g_signal_connect(G_OBJECT(http_client_), "changed",
619 G_CALLBACK(OnGSettingsChangeNotification), this);
620 g_signal_connect(G_OBJECT(https_client_), "changed",
621 G_CALLBACK(OnGSettingsChangeNotification), this);
622 g_signal_connect(G_OBJECT(ftp_client_), "changed",
623 G_CALLBACK(OnGSettingsChangeNotification), this);
624 g_signal_connect(G_OBJECT(socks_client_), "changed",
625 G_CALLBACK(OnGSettingsChangeNotification), this);
626 // Simulate a change to avoid possibly losing updates before this point.
627 OnChangeNotification();
628 return true;
629 }
630
dcheng67be2b1f2014-10-27 21:47:29631 const scoped_refptr<base::SingleThreadTaskRunner>& GetNotificationTaskRunner()
632 override {
sergeyu3f923062014-09-05 01:39:40633 return task_runner_;
[email protected]8c20e3d2011-05-19 21:03:57634 }
635
dcheng67be2b1f2014-10-27 21:47:29636 ProxyConfigSource GetConfigSource() override {
[email protected]db8ff912012-06-12 23:32:51637 return PROXY_CONFIG_SOURCE_GSETTINGS;
[email protected]8c20e3d2011-05-19 21:03:57638 }
639
dcheng67be2b1f2014-10-27 21:47:29640 bool GetString(StringSetting key, std::string* result) override {
[email protected]8c20e3d2011-05-19 21:03:57641 DCHECK(client_);
642 switch (key) {
643 case PROXY_MODE:
644 return GetStringByPath(client_, "mode", result);
645 case PROXY_AUTOCONF_URL:
646 return GetStringByPath(client_, "autoconfig-url", result);
647 case PROXY_HTTP_HOST:
648 return GetStringByPath(http_client_, "host", result);
649 case PROXY_HTTPS_HOST:
650 return GetStringByPath(https_client_, "host", result);
651 case PROXY_FTP_HOST:
652 return GetStringByPath(ftp_client_, "host", result);
653 case PROXY_SOCKS_HOST:
654 return GetStringByPath(socks_client_, "host", result);
[email protected]8c20e3d2011-05-19 21:03:57655 }
[email protected]6b5fe742011-05-20 21:46:48656 return false; // Placate compiler.
[email protected]8c20e3d2011-05-19 21:03:57657 }
dcheng67be2b1f2014-10-27 21:47:29658 bool GetBool(BoolSetting key, bool* result) override {
[email protected]8c20e3d2011-05-19 21:03:57659 DCHECK(client_);
660 switch (key) {
661 case PROXY_USE_HTTP_PROXY:
662 // Although there is an "enabled" boolean in http_client_, it is not set
663 // to true by the proxy config utility. We ignore it and return false.
664 return false;
665 case PROXY_USE_SAME_PROXY:
666 // Similarly, although there is a "use-same-proxy" boolean in client_,
667 // it is never set to false by the proxy config utility. We ignore it.
668 return false;
669 case PROXY_USE_AUTHENTICATION:
670 // There is also no way to set this in the proxy config utility, but it
671 // doesn't hurt us to get the actual setting (unlike the two above).
672 return GetBoolByPath(http_client_, "use-authentication", result);
[email protected]8c20e3d2011-05-19 21:03:57673 }
[email protected]6b5fe742011-05-20 21:46:48674 return false; // Placate compiler.
[email protected]8c20e3d2011-05-19 21:03:57675 }
dcheng67be2b1f2014-10-27 21:47:29676 bool GetInt(IntSetting key, int* result) override {
[email protected]8c20e3d2011-05-19 21:03:57677 DCHECK(client_);
678 switch (key) {
679 case PROXY_HTTP_PORT:
680 return GetIntByPath(http_client_, "port", result);
681 case PROXY_HTTPS_PORT:
682 return GetIntByPath(https_client_, "port", result);
683 case PROXY_FTP_PORT:
684 return GetIntByPath(ftp_client_, "port", result);
685 case PROXY_SOCKS_PORT:
686 return GetIntByPath(socks_client_, "port", result);
[email protected]8c20e3d2011-05-19 21:03:57687 }
[email protected]6b5fe742011-05-20 21:46:48688 return false; // Placate compiler.
[email protected]8c20e3d2011-05-19 21:03:57689 }
dcheng67be2b1f2014-10-27 21:47:29690 bool GetStringList(StringListSetting key,
691 std::vector<std::string>* result) override {
[email protected]8c20e3d2011-05-19 21:03:57692 DCHECK(client_);
693 switch (key) {
694 case PROXY_IGNORE_HOSTS:
695 return GetStringListByPath(client_, "ignore-hosts", result);
[email protected]8c20e3d2011-05-19 21:03:57696 }
[email protected]6b5fe742011-05-20 21:46:48697 return false; // Placate compiler.
[email protected]8c20e3d2011-05-19 21:03:57698 }
699
dcheng67be2b1f2014-10-27 21:47:29700 bool BypassListIsReversed() override {
[email protected]8c20e3d2011-05-19 21:03:57701 // This is a KDE-specific setting.
702 return false;
703 }
704
dcheng67be2b1f2014-10-27 21:47:29705 bool MatchHostsUsingSuffixMatching() override { return false; }
[email protected]8c20e3d2011-05-19 21:03:57706
707 private:
[email protected]8c20e3d2011-05-19 21:03:57708 bool GetStringByPath(GSettings* client, const char* key,
709 std::string* result) {
[email protected]76722472012-05-24 08:26:46710 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]3fc24f52012-11-30 21:22:34711 gchar* value = libgio_loader_.g_settings_get_string(client, key);
[email protected]8c20e3d2011-05-19 21:03:57712 if (!value)
713 return false;
714 *result = value;
715 g_free(value);
716 return true;
717 }
718 bool GetBoolByPath(GSettings* client, const char* key, bool* result) {
[email protected]76722472012-05-24 08:26:46719 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]3fc24f52012-11-30 21:22:34720 *result = static_cast<bool>(
721 libgio_loader_.g_settings_get_boolean(client, key));
[email protected]8c20e3d2011-05-19 21:03:57722 return true;
723 }
724 bool GetIntByPath(GSettings* client, const char* key, int* result) {
[email protected]76722472012-05-24 08:26:46725 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]3fc24f52012-11-30 21:22:34726 *result = libgio_loader_.g_settings_get_int(client, key);
[email protected]8c20e3d2011-05-19 21:03:57727 return true;
728 }
729 bool GetStringListByPath(GSettings* client, const char* key,
730 std::vector<std::string>* result) {
[email protected]76722472012-05-24 08:26:46731 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]3fc24f52012-11-30 21:22:34732 gchar** list = libgio_loader_.g_settings_get_strv(client, key);
[email protected]8c20e3d2011-05-19 21:03:57733 if (!list)
734 return false;
735 for (size_t i = 0; list[i]; ++i) {
736 result->push_back(static_cast<char*>(list[i]));
737 g_free(list[i]);
738 }
739 g_free(list);
740 return true;
741 }
742
743 // This is the callback from the debounce timer.
744 void OnDebouncedNotification() {
[email protected]76722472012-05-24 08:26:46745 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]8c20e3d2011-05-19 21:03:57746 CHECK(notify_delegate_);
747 // Forward to a method on the proxy config service delegate object.
748 notify_delegate_->OnCheckProxyConfigSettings();
749 }
750
751 void OnChangeNotification() {
752 // We don't use Reset() because the timer may not yet be running.
753 // (In that case Stop() is a no-op.)
marshall8e5fe942015-03-06 19:22:40754 debounce_timer_->Stop();
755 debounce_timer_->Start(FROM_HERE,
[email protected]8c20e3d2011-05-19 21:03:57756 base::TimeDelta::FromMilliseconds(kDebounceTimeoutMilliseconds),
757 this, &SettingGetterImplGSettings::OnDebouncedNotification);
758 }
759
760 // gsettings notification callback, dispatched on the default glib main loop.
761 static void OnGSettingsChangeNotification(GSettings* client, gchar* key,
762 gpointer user_data) {
763 VLOG(1) << "gsettings change notification for key " << key;
764 // We don't track which key has changed, just that something did change.
765 SettingGetterImplGSettings* setting_getter =
766 reinterpret_cast<SettingGetterImplGSettings*>(user_data);
767 setting_getter->OnChangeNotification();
768 }
769
770 GSettings* client_;
771 GSettings* http_client_;
772 GSettings* https_client_;
773 GSettings* ftp_client_;
774 GSettings* socks_client_;
775 ProxyConfigServiceLinux::Delegate* notify_delegate_;
danakj8c3eb802015-09-24 07:53:00776 scoped_ptr<base::OneShotTimer> debounce_timer_;
[email protected]8c20e3d2011-05-19 21:03:57777
[email protected]76722472012-05-24 08:26:46778 // Task runner for the thread that we make gsettings calls on. It should
[email protected]8c20e3d2011-05-19 21:03:57779 // be the UI thread and all our methods should be called on this
780 // thread. Only for assertions.
[email protected]76722472012-05-24 08:26:46781 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
[email protected]8c20e3d2011-05-19 21:03:57782
[email protected]3fc24f52012-11-30 21:22:34783 LibGioLoader libgio_loader_;
784
[email protected]8c20e3d2011-05-19 21:03:57785 DISALLOW_COPY_AND_ASSIGN(SettingGetterImplGSettings);
786};
787
788bool SettingGetterImplGSettings::LoadAndCheckVersion(
789 base::Environment* env) {
790 // LoadAndCheckVersion() must be called *before* Init()!
791 DCHECK(!client_);
792
793 // The APIs to query gsettings were introduced after the minimum glib
794 // version we target, so we can't link directly against them. We load them
795 // dynamically at runtime, and if they don't exist, return false here. (We
796 // support linking directly via gyp flags though.) Additionally, even when
797 // they are present, we do two additional checks to make sure we should use
798 // them and not gconf. First, we attempt to load the schema for proxy
799 // settings. Second, we check for the program that was used in older
800 // versions of GNOME to configure proxy settings, and return false if it
801 // exists. Some distributions (e.g. Ubuntu 11.04) have the API and schema
802 // but don't use gsettings for proxy settings, but they do have the old
803 // binary, so we detect these systems that way.
804
[email protected]ae82cea2012-12-06 22:52:10805 {
806 // TODO(phajdan.jr): Redesign the code to load library on different thread.
807 base::ThreadRestrictions::ScopedAllowIO allow_io;
808
809 // Try also without .0 at the end; on some systems this may be required.
810 if (!libgio_loader_.Load("libgio-2.0.so.0") &&
811 !libgio_loader_.Load("libgio-2.0.so")) {
812 VLOG(1) << "Cannot load gio library. Will fall back to gconf.";
813 return false;
814 }
[email protected]8c20e3d2011-05-19 21:03:57815 }
[email protected]8c20e3d2011-05-19 21:03:57816
[email protected]4bbb72d2014-06-06 18:05:51817 GSettings* client = NULL;
818 if (SchemaExists(kProxyGConfSchema)) {
819 ANNOTATE_SCOPED_MEMORY_LEAK; // http://crbug.com/380782
820 client = libgio_loader_.g_settings_new(kProxyGConfSchema);
821 }
822 if (!client) {
[email protected]8c20e3d2011-05-19 21:03:57823 VLOG(1) << "Cannot create gsettings client. Will fall back to gconf.";
824 return false;
825 }
826 g_object_unref(client);
827
828 std::string path;
829 if (!env->GetVar("PATH", &path)) {
830 LOG(ERROR) << "No $PATH variable. Assuming no gnome-network-properties.";
831 } else {
832 // Yes, we're on the UI thread. Yes, we're accessing the file system.
833 // Sadly, we don't have much choice. We need the proxy settings and we
834 // need them now, and to figure out where to get them, we have to check
835 // for this binary. See http://crbug.com/69057 for additional details.
836 base::ThreadRestrictions::ScopedAllowIO allow_io;
brettw8cc24ae22015-07-06 23:53:00837
838 for (const base::StringPiece& path_str : base::SplitStringPiece(
839 path, ":", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY)) {
840 base::FilePath file(path_str);
[email protected]7567484142013-07-11 17:36:07841 if (base::PathExists(file.Append("gnome-network-properties"))) {
[email protected]8c20e3d2011-05-19 21:03:57842 VLOG(1) << "Found gnome-network-properties. Will fall back to gconf.";
843 return false;
844 }
845 }
846 }
847
848 VLOG(1) << "All gsettings tests OK. Will get proxy config from gsettings.";
849 return true;
850}
851#endif // defined(USE_GIO)
852
[email protected]d7395e732009-08-28 23:13:43853// This is the KDE version that reads kioslaverc and simulates gconf.
854// Doing this allows the main Delegate code, as well as the unit tests
855// for it, to stay the same - and the settings map fairly well besides.
[email protected]573c0502011-05-17 22:19:50856class SettingGetterImplKDE : public ProxyConfigServiceLinux::SettingGetter,
857 public base::MessagePumpLibevent::Watcher {
[email protected]d7395e732009-08-28 23:13:43858 public:
[email protected]573c0502011-05-17 22:19:50859 explicit SettingGetterImplKDE(base::Environment* env_var_getter)
marshall8e5fe942015-03-06 19:22:40860 : inotify_fd_(-1),
861 notify_delegate_(NULL),
danakj8c3eb802015-09-24 07:53:00862 debounce_timer_(new base::OneShotTimer()),
marshall8e5fe942015-03-06 19:22:40863 indirect_manual_(false),
864 auto_no_pac_(false),
865 reversed_bypass_list_(false),
866 env_var_getter_(env_var_getter),
867 file_task_runner_(NULL) {
[email protected]9a8c4022011-01-25 14:25:33868 // This has to be called on the UI thread (http://crbug.com/69057).
869 base::ThreadRestrictions::ScopedAllowIO allow_io;
870
[email protected]f18fde22010-05-18 23:49:54871 // Derive the location of the kde config dir from the environment.
[email protected]92d2dc82010-04-08 17:49:59872 std::string home;
[email protected]3ba7e082010-08-07 02:57:59873 if (env_var_getter->GetVar("KDEHOME", &home) && !home.empty()) {
[email protected]2e8cfe22010-06-12 00:26:24874 // $KDEHOME is set. Use it unconditionally.
[email protected]6cdfd7f2013-02-08 20:40:15875 kde_config_dir_ = KDEHomeToConfigPath(base::FilePath(home));
[email protected]92d2dc82010-04-08 17:49:59876 } else {
[email protected]2e8cfe22010-06-12 00:26:24877 // $KDEHOME is unset. Try to figure out what to use. This seems to be
[email protected]92d2dc82010-04-08 17:49:59878 // the common case on most distributions.
[email protected]3ba7e082010-08-07 02:57:59879 if (!env_var_getter->GetVar(base::env_vars::kHome, &home))
[email protected]d7395e732009-08-28 23:13:43880 // User has no $HOME? Give up. Later we'll report the failure.
881 return;
[email protected]6b0349ef2010-10-16 04:56:06882 if (base::nix::GetDesktopEnvironment(env_var_getter) ==
883 base::nix::DESKTOP_ENVIRONMENT_KDE3) {
[email protected]92d2dc82010-04-08 17:49:59884 // KDE3 always uses .kde for its configuration.
[email protected]6cdfd7f2013-02-08 20:40:15885 base::FilePath kde_path = base::FilePath(home).Append(".kde");
[email protected]92d2dc82010-04-08 17:49:59886 kde_config_dir_ = KDEHomeToConfigPath(kde_path);
edward.baker53bec302015-10-02 16:57:49887 } else if (base::nix::GetDesktopEnvironment(env_var_getter) ==
888 base::nix::DESKTOP_ENVIRONMENT_KDE4) {
[email protected]92d2dc82010-04-08 17:49:59889 // Some distributions patch KDE4 to use .kde4 instead of .kde, so that
[email protected]fad9c8a52010-06-10 22:30:53890 // both can be installed side-by-side. Sadly they don't all do this, and
891 // they don't always do this: some distributions have started switching
892 // back as well. So if there is a .kde4 directory, check the timestamps
893 // of the config directories within and use the newest one.
[email protected]92d2dc82010-04-08 17:49:59894 // Note that we should currently be running in the UI thread, because in
895 // the gconf version, that is the only thread that can access the proxy
896 // settings (a gconf restriction). As noted below, the initial read of
897 // the proxy settings will be done in this thread anyway, so we check
898 // for .kde4 here in this thread as well.
[email protected]6cdfd7f2013-02-08 20:40:15899 base::FilePath kde3_path = base::FilePath(home).Append(".kde");
900 base::FilePath kde3_config = KDEHomeToConfigPath(kde3_path);
901 base::FilePath kde4_path = base::FilePath(home).Append(".kde4");
902 base::FilePath kde4_config = KDEHomeToConfigPath(kde4_path);
[email protected]fad9c8a52010-06-10 22:30:53903 bool use_kde4 = false;
[email protected]dcd16612013-07-15 20:18:09904 if (base::DirectoryExists(kde4_path)) {
[email protected]54124ed02014-01-07 10:06:58905 base::File::Info kde3_info;
906 base::File::Info kde4_info;
[email protected]9eae4e62013-12-04 20:56:49907 if (base::GetFileInfo(kde4_config, &kde4_info)) {
908 if (base::GetFileInfo(kde3_config, &kde3_info)) {
[email protected]fad9c8a52010-06-10 22:30:53909 use_kde4 = kde4_info.last_modified >= kde3_info.last_modified;
910 } else {
911 use_kde4 = true;
912 }
913 }
914 }
915 if (use_kde4) {
[email protected]92d2dc82010-04-08 17:49:59916 kde_config_dir_ = KDEHomeToConfigPath(kde4_path);
917 } else {
[email protected]fad9c8a52010-06-10 22:30:53918 kde_config_dir_ = KDEHomeToConfigPath(kde3_path);
[email protected]92d2dc82010-04-08 17:49:59919 }
edward.baker53bec302015-10-02 16:57:49920 } else {
921 // KDE 5 migrated to ~/.config for storing kioslaverc.
922 kde_config_dir_ = base::FilePath(home).Append(".config");
[email protected]92d2dc82010-04-08 17:49:59923 }
[email protected]d7395e732009-08-28 23:13:43924 }
[email protected]d7395e732009-08-28 23:13:43925 }
926
dcheng67be2b1f2014-10-27 21:47:29927 ~SettingGetterImplKDE() override {
[email protected]d7395e732009-08-28 23:13:43928 // inotify_fd_ should have been closed before now, from
929 // Delegate::OnDestroy(), while running on the file thread. However
930 // on exiting the process, it may happen that Delegate::OnDestroy()
931 // task is left pending on the file loop after the loop was quit,
932 // and pending tasks may then be deleted without being run.
933 // Here in the KDE version, we can safely close the file descriptor
934 // anyway. (Not that it really matters; the process is exiting.)
935 if (inotify_fd_ >= 0)
[email protected]d3066142011-05-10 02:36:20936 ShutDown();
[email protected]d7395e732009-08-28 23:13:43937 DCHECK(inotify_fd_ < 0);
938 }
939
dcheng67be2b1f2014-10-27 21:47:29940 bool Init(const scoped_refptr<base::SingleThreadTaskRunner>& glib_task_runner,
941 const scoped_refptr<base::SingleThreadTaskRunner>& file_task_runner)
mostynbba063d6032014-10-09 11:01:13942 override {
[email protected]9a8c4022011-01-25 14:25:33943 // This has to be called on the UI thread (http://crbug.com/69057).
944 base::ThreadRestrictions::ScopedAllowIO allow_io;
[email protected]d7395e732009-08-28 23:13:43945 DCHECK(inotify_fd_ < 0);
946 inotify_fd_ = inotify_init();
947 if (inotify_fd_ < 0) {
[email protected]57b765672009-10-13 18:27:40948 PLOG(ERROR) << "inotify_init failed";
[email protected]d7395e732009-08-28 23:13:43949 return false;
950 }
tfarina89b4ae1c2015-12-16 18:59:18951 if (!base::SetNonBlocking(inotify_fd_)) {
952 PLOG(ERROR) << "base::SetNonBlocking failed";
[email protected]d7395e732009-08-28 23:13:43953 close(inotify_fd_);
954 inotify_fd_ = -1;
955 return false;
956 }
sergeyu3f923062014-09-05 01:39:40957 file_task_runner_ = file_task_runner;
958 // The initial read is done on the current thread, not
959 // |file_task_runner_|, since we will need to have it for
960 // SetUpAndFetchInitialConfig().
[email protected]d7395e732009-08-28 23:13:43961 UpdateCachedSettings();
962 return true;
963 }
964
dcheng67be2b1f2014-10-27 21:47:29965 void ShutDown() override {
[email protected]d7395e732009-08-28 23:13:43966 if (inotify_fd_ >= 0) {
967 ResetCachedSettings();
968 inotify_watcher_.StopWatchingFileDescriptor();
969 close(inotify_fd_);
970 inotify_fd_ = -1;
971 }
marshall8e5fe942015-03-06 19:22:40972 debounce_timer_.reset();
[email protected]d7395e732009-08-28 23:13:43973 }
974
dcheng67be2b1f2014-10-27 21:47:29975 bool SetUpNotifications(
mostynbba063d6032014-10-09 11:01:13976 ProxyConfigServiceLinux::Delegate* delegate) override {
[email protected]d7395e732009-08-28 23:13:43977 DCHECK(inotify_fd_ >= 0);
sergeyu3f923062014-09-05 01:39:40978 DCHECK(file_task_runner_->BelongsToCurrentThread());
[email protected]d7395e732009-08-28 23:13:43979 // We can't just watch the kioslaverc file directly, since KDE will write
980 // a new copy of it and then rename it whenever settings are changed and
981 // inotify watches inodes (so we'll be watching the old deleted file after
982 // the first change, and it will never change again). So, we watch the
983 // directory instead. We then act only on changes to the kioslaverc entry.
984 if (inotify_add_watch(inotify_fd_, kde_config_dir_.value().c_str(),
sergeyu3f923062014-09-05 01:39:40985 IN_MODIFY | IN_MOVED_TO) < 0) {
[email protected]d7395e732009-08-28 23:13:43986 return false;
sergeyu3f923062014-09-05 01:39:40987 }
[email protected]d7395e732009-08-28 23:13:43988 notify_delegate_ = delegate;
sergeyu3f923062014-09-05 01:39:40989 if (!base::MessageLoopForIO::current()->WatchFileDescriptor(
990 inotify_fd_, true, base::MessageLoopForIO::WATCH_READ,
991 &inotify_watcher_, this)) {
[email protected]d3066142011-05-10 02:36:20992 return false;
sergeyu3f923062014-09-05 01:39:40993 }
[email protected]d3066142011-05-10 02:36:20994 // Simulate a change to avoid possibly losing updates before this point.
995 OnChangeNotification();
996 return true;
[email protected]d7395e732009-08-28 23:13:43997 }
998
dcheng67be2b1f2014-10-27 21:47:29999 const scoped_refptr<base::SingleThreadTaskRunner>& GetNotificationTaskRunner()
1000 override {
sergeyu3f923062014-09-05 01:39:401001 return file_task_runner_;
[email protected]d7395e732009-08-28 23:13:431002 }
1003
[email protected]b160df32012-02-06 20:39:411004 // Implement base::MessagePumpLibevent::Watcher.
dcheng67be2b1f2014-10-27 21:47:291005 void OnFileCanReadWithoutBlocking(int fd) override {
[email protected]d2e6d592012-02-03 21:49:041006 DCHECK_EQ(fd, inotify_fd_);
sergeyu3f923062014-09-05 01:39:401007 DCHECK(file_task_runner_->BelongsToCurrentThread());
[email protected]d7395e732009-08-28 23:13:431008 OnChangeNotification();
1009 }
dcheng67be2b1f2014-10-27 21:47:291010 void OnFileCanWriteWithoutBlocking(int fd) override { NOTREACHED(); }
[email protected]d7395e732009-08-28 23:13:431011
dcheng67be2b1f2014-10-27 21:47:291012 ProxyConfigSource GetConfigSource() override {
[email protected]db8ff912012-06-12 23:32:511013 return PROXY_CONFIG_SOURCE_KDE;
[email protected]d7395e732009-08-28 23:13:431014 }
1015
dcheng67be2b1f2014-10-27 21:47:291016 bool GetString(StringSetting key, std::string* result) override {
[email protected]d7395e732009-08-28 23:13:431017 string_map_type::iterator it = string_table_.find(key);
1018 if (it == string_table_.end())
1019 return false;
1020 *result = it->second;
1021 return true;
1022 }
dcheng67be2b1f2014-10-27 21:47:291023 bool GetBool(BoolSetting key, bool* result) override {
[email protected]d7395e732009-08-28 23:13:431024 // We don't ever have any booleans.
1025 return false;
1026 }
dcheng67be2b1f2014-10-27 21:47:291027 bool GetInt(IntSetting key, int* result) override {
[email protected]d7395e732009-08-28 23:13:431028 // We don't ever have any integers. (See AddProxy() below about ports.)
1029 return false;
1030 }
dcheng67be2b1f2014-10-27 21:47:291031 bool GetStringList(StringListSetting key,
1032 std::vector<std::string>* result) override {
[email protected]d7395e732009-08-28 23:13:431033 strings_map_type::iterator it = strings_table_.find(key);
1034 if (it == strings_table_.end())
1035 return false;
1036 *result = it->second;
1037 return true;
1038 }
1039
dcheng67be2b1f2014-10-27 21:47:291040 bool BypassListIsReversed() override { return reversed_bypass_list_; }
[email protected]a48bf4a2010-06-14 18:24:531041
dcheng67be2b1f2014-10-27 21:47:291042 bool MatchHostsUsingSuffixMatching() override { return true; }
[email protected]1a597192010-07-09 16:58:381043
[email protected]d7395e732009-08-28 23:13:431044 private:
1045 void ResetCachedSettings() {
1046 string_table_.clear();
1047 strings_table_.clear();
1048 indirect_manual_ = false;
1049 auto_no_pac_ = false;
[email protected]a48bf4a2010-06-14 18:24:531050 reversed_bypass_list_ = false;
[email protected]d7395e732009-08-28 23:13:431051 }
1052
[email protected]6cdfd7f2013-02-08 20:40:151053 base::FilePath KDEHomeToConfigPath(const base::FilePath& kde_home) {
[email protected]92d2dc82010-04-08 17:49:591054 return kde_home.Append("share").Append("config");
1055 }
1056
[email protected]6b5fe742011-05-20 21:46:481057 void AddProxy(StringSetting host_key, const std::string& value) {
[email protected]d7395e732009-08-28 23:13:431058 if (value.empty() || value.substr(0, 3) == "//:")
1059 // No proxy.
1060 return;
[email protected]4b90c202012-04-24 23:27:551061 size_t space = value.find(' ');
1062 if (space != std::string::npos) {
1063 // Newer versions of KDE use a space rather than a colon to separate the
1064 // port number from the hostname. If we find this, we need to convert it.
1065 std::string fixed = value;
1066 fixed[space] = ':';
1067 string_table_[host_key] = fixed;
1068 } else {
1069 // We don't need to parse the port number out; GetProxyFromSettings()
1070 // would only append it right back again. So we just leave the port
1071 // number right in the host string.
1072 string_table_[host_key] = value;
1073 }
[email protected]d7395e732009-08-28 23:13:431074 }
1075
[email protected]6b5fe742011-05-20 21:46:481076 void AddHostList(StringListSetting key, const std::string& value) {
[email protected]f18fde22010-05-18 23:49:541077 std::vector<std::string> tokens;
[email protected]f4ebe772013-02-02 00:21:391078 base::StringTokenizer tk(value, ", ");
[email protected]f18fde22010-05-18 23:49:541079 while (tk.GetNext()) {
1080 std::string token = tk.token();
1081 if (!token.empty())
1082 tokens.push_back(token);
1083 }
1084 strings_table_[key] = tokens;
1085 }
1086
[email protected]9a3d8d42009-09-03 17:01:461087 void AddKDESetting(const std::string& key, const std::string& value) {
[email protected]d7395e732009-08-28 23:13:431088 if (key == "ProxyType") {
1089 const char* mode = "none";
1090 indirect_manual_ = false;
1091 auto_no_pac_ = false;
[email protected]e83326f2010-07-31 17:29:251092 int int_value;
1093 base::StringToInt(value, &int_value);
1094 switch (int_value) {
[email protected]d7395e732009-08-28 23:13:431095 case 0: // No proxy, or maybe kioslaverc syntax error.
1096 break;
1097 case 1: // Manual configuration.
1098 mode = "manual";
1099 break;
1100 case 2: // PAC URL.
1101 mode = "auto";
1102 break;
1103 case 3: // WPAD.
1104 mode = "auto";
1105 auto_no_pac_ = true;
1106 break;
1107 case 4: // Indirect manual via environment variables.
1108 mode = "manual";
1109 indirect_manual_ = true;
1110 break;
1111 }
[email protected]573c0502011-05-17 22:19:501112 string_table_[PROXY_MODE] = mode;
[email protected]d7395e732009-08-28 23:13:431113 } else if (key == "Proxy Config Script") {
[email protected]573c0502011-05-17 22:19:501114 string_table_[PROXY_AUTOCONF_URL] = value;
[email protected]d7395e732009-08-28 23:13:431115 } else if (key == "httpProxy") {
[email protected]573c0502011-05-17 22:19:501116 AddProxy(PROXY_HTTP_HOST, value);
[email protected]d7395e732009-08-28 23:13:431117 } else if (key == "httpsProxy") {
[email protected]573c0502011-05-17 22:19:501118 AddProxy(PROXY_HTTPS_HOST, value);
[email protected]d7395e732009-08-28 23:13:431119 } else if (key == "ftpProxy") {
[email protected]573c0502011-05-17 22:19:501120 AddProxy(PROXY_FTP_HOST, value);
[email protected]bfeb7232012-06-08 00:58:371121 } else if (key == "socksProxy") {
1122 // Older versions of KDE configure SOCKS in a weird way involving
1123 // LD_PRELOAD and a library that intercepts network calls to SOCKSify
1124 // them. We don't support it. KDE 4.8 added a proper SOCKS setting.
1125 AddProxy(PROXY_SOCKS_HOST, value);
[email protected]d7395e732009-08-28 23:13:431126 } else if (key == "ReversedException") {
1127 // We count "true" or any nonzero number as true, otherwise false.
1128 // Note that if the value is not actually numeric StringToInt()
1129 // will return 0, which we count as false.
[email protected]e83326f2010-07-31 17:29:251130 int int_value;
1131 base::StringToInt(value, &int_value);
1132 reversed_bypass_list_ = (value == "true" || int_value);
[email protected]d7395e732009-08-28 23:13:431133 } else if (key == "NoProxyFor") {
[email protected]573c0502011-05-17 22:19:501134 AddHostList(PROXY_IGNORE_HOSTS, value);
[email protected]d7395e732009-08-28 23:13:431135 } else if (key == "AuthMode") {
1136 // Check for authentication, just so we can warn.
[email protected]e83326f2010-07-31 17:29:251137 int mode;
1138 base::StringToInt(value, &mode);
[email protected]d7395e732009-08-28 23:13:431139 if (mode) {
1140 // ProxyConfig does not support authentication parameters, but
1141 // Chrome will prompt for the password later. So we ignore this.
1142 LOG(WARNING) <<
1143 "Proxy authentication parameters ignored, see bug 16709";
1144 }
1145 }
1146 }
1147
[email protected]6b5fe742011-05-20 21:46:481148 void ResolveIndirect(StringSetting key) {
[email protected]d7395e732009-08-28 23:13:431149 string_map_type::iterator it = string_table_.find(key);
1150 if (it != string_table_.end()) {
[email protected]f18fde22010-05-18 23:49:541151 std::string value;
[email protected]3ba7e082010-08-07 02:57:591152 if (env_var_getter_->GetVar(it->second.c_str(), &value))
[email protected]d7395e732009-08-28 23:13:431153 it->second = value;
[email protected]8425adc02010-04-18 17:45:311154 else
1155 string_table_.erase(it);
[email protected]d7395e732009-08-28 23:13:431156 }
1157 }
1158
[email protected]6b5fe742011-05-20 21:46:481159 void ResolveIndirectList(StringListSetting key) {
[email protected]f18fde22010-05-18 23:49:541160 strings_map_type::iterator it = strings_table_.find(key);
1161 if (it != strings_table_.end()) {
1162 std::string value;
1163 if (!it->second.empty() &&
[email protected]3ba7e082010-08-07 02:57:591164 env_var_getter_->GetVar(it->second[0].c_str(), &value))
[email protected]f18fde22010-05-18 23:49:541165 AddHostList(key, value);
1166 else
1167 strings_table_.erase(it);
1168 }
1169 }
1170
[email protected]d7395e732009-08-28 23:13:431171 // The settings in kioslaverc could occur in any order, but some affect
1172 // others. Rather than read the whole file in and then query them in an
1173 // order that allows us to handle that, we read the settings in whatever
1174 // order they occur and do any necessary tweaking after we finish.
1175 void ResolveModeEffects() {
1176 if (indirect_manual_) {
[email protected]573c0502011-05-17 22:19:501177 ResolveIndirect(PROXY_HTTP_HOST);
1178 ResolveIndirect(PROXY_HTTPS_HOST);
1179 ResolveIndirect(PROXY_FTP_HOST);
1180 ResolveIndirectList(PROXY_IGNORE_HOSTS);
[email protected]d7395e732009-08-28 23:13:431181 }
1182 if (auto_no_pac_) {
1183 // Remove the PAC URL; we're not supposed to use it.
[email protected]573c0502011-05-17 22:19:501184 string_table_.erase(PROXY_AUTOCONF_URL);
[email protected]d7395e732009-08-28 23:13:431185 }
[email protected]d7395e732009-08-28 23:13:431186 }
1187
1188 // Reads kioslaverc one line at a time and calls AddKDESetting() to add
1189 // each relevant name-value pair to the appropriate value table.
1190 void UpdateCachedSettings() {
[email protected]6cdfd7f2013-02-08 20:40:151191 base::FilePath kioslaverc = kde_config_dir_.Append("kioslaverc");
[email protected]b9b4a572014-03-17 23:11:121192 base::ScopedFILE input(base::OpenFile(kioslaverc, "r"));
[email protected]d7395e732009-08-28 23:13:431193 if (!input.get())
1194 return;
1195 ResetCachedSettings();
1196 bool in_proxy_settings = false;
1197 bool line_too_long = false;
[email protected]9a3d8d42009-09-03 17:01:461198 char line[BUFFER_SIZE];
1199 // fgets() will return NULL on EOF or error.
[email protected]d7395e732009-08-28 23:13:431200 while (fgets(line, sizeof(line), input.get())) {
1201 // fgets() guarantees the line will be properly terminated.
1202 size_t length = strlen(line);
1203 if (!length)
1204 continue;
1205 // This should be true even with CRLF endings.
1206 if (line[length - 1] != '\n') {
1207 line_too_long = true;
1208 continue;
1209 }
1210 if (line_too_long) {
1211 // The previous line had no line ending, but this done does. This is
1212 // the end of the line that was too long, so warn here and skip it.
1213 LOG(WARNING) << "skipped very long line in " << kioslaverc.value();
1214 line_too_long = false;
1215 continue;
1216 }
1217 // Remove the LF at the end, and the CR if there is one.
1218 line[--length] = '\0';
1219 if (length && line[length - 1] == '\r')
1220 line[--length] = '\0';
1221 // Now parse the line.
1222 if (line[0] == '[') {
1223 // Switching sections. All we care about is whether this is
1224 // the (a?) proxy settings section, for both KDE3 and KDE4.
1225 in_proxy_settings = !strncmp(line, "[Proxy Settings]", 16);
1226 } else if (in_proxy_settings) {
1227 // A regular line, in the (a?) proxy settings section.
[email protected]9a3d8d42009-09-03 17:01:461228 char* split = strchr(line, '=');
1229 // Skip this line if it does not contain an = sign.
1230 if (!split)
[email protected]d7395e732009-08-28 23:13:431231 continue;
[email protected]9a3d8d42009-09-03 17:01:461232 // Split the line on the = and advance |split|.
1233 *(split++) = 0;
1234 std::string key = line;
1235 std::string value = split;
[email protected]8af69c6c2014-03-03 19:05:311236 base::TrimWhitespaceASCII(key, base::TRIM_ALL, &key);
1237 base::TrimWhitespaceASCII(value, base::TRIM_ALL, &value);
[email protected]9a3d8d42009-09-03 17:01:461238 // Skip this line if the key name is empty.
1239 if (key.empty())
[email protected]d7395e732009-08-28 23:13:431240 continue;
1241 // Is the value name localized?
[email protected]9a3d8d42009-09-03 17:01:461242 if (key[key.length() - 1] == ']') {
1243 // Find the matching bracket.
1244 length = key.rfind('[');
1245 // Skip this line if the localization indicator is malformed.
1246 if (length == std::string::npos)
[email protected]d7395e732009-08-28 23:13:431247 continue;
1248 // Trim the localization indicator off.
[email protected]9a3d8d42009-09-03 17:01:461249 key.resize(length);
1250 // Remove any resulting trailing whitespace.
[email protected]8af69c6c2014-03-03 19:05:311251 base::TrimWhitespaceASCII(key, base::TRIM_TRAILING, &key);
[email protected]9a3d8d42009-09-03 17:01:461252 // Skip this line if the key name is now empty.
1253 if (key.empty())
1254 continue;
[email protected]d7395e732009-08-28 23:13:431255 }
[email protected]d7395e732009-08-28 23:13:431256 // Now fill in the tables.
[email protected]9a3d8d42009-09-03 17:01:461257 AddKDESetting(key, value);
[email protected]d7395e732009-08-28 23:13:431258 }
1259 }
1260 if (ferror(input.get()))
1261 LOG(ERROR) << "error reading " << kioslaverc.value();
1262 ResolveModeEffects();
1263 }
1264
1265 // This is the callback from the debounce timer.
1266 void OnDebouncedNotification() {
sergeyu3f923062014-09-05 01:39:401267 DCHECK(file_task_runner_->BelongsToCurrentThread());
[email protected]b30a3f52010-10-16 01:05:461268 VLOG(1) << "inotify change notification for kioslaverc";
[email protected]d7395e732009-08-28 23:13:431269 UpdateCachedSettings();
[email protected]961ac942011-04-28 18:18:141270 CHECK(notify_delegate_);
[email protected]d7395e732009-08-28 23:13:431271 // Forward to a method on the proxy config service delegate object.
1272 notify_delegate_->OnCheckProxyConfigSettings();
1273 }
1274
1275 // Called by OnFileCanReadWithoutBlocking() on the file thread. Reads
1276 // from the inotify file descriptor and starts up a debounce timer if
1277 // an event for kioslaverc is seen.
1278 void OnChangeNotification() {
[email protected]d2e6d592012-02-03 21:49:041279 DCHECK_GE(inotify_fd_, 0);
sergeyu3f923062014-09-05 01:39:401280 DCHECK(file_task_runner_->BelongsToCurrentThread());
[email protected]d7395e732009-08-28 23:13:431281 char event_buf[(sizeof(inotify_event) + NAME_MAX + 1) * 4];
1282 bool kioslaverc_touched = false;
1283 ssize_t r;
1284 while ((r = read(inotify_fd_, event_buf, sizeof(event_buf))) > 0) {
1285 // inotify returns variable-length structures, which is why we have
1286 // this strange-looking loop instead of iterating through an array.
1287 char* event_ptr = event_buf;
1288 while (event_ptr < event_buf + r) {
1289 inotify_event* event = reinterpret_cast<inotify_event*>(event_ptr);
1290 // The kernel always feeds us whole events.
[email protected]b1f031dd2010-03-02 23:19:331291 CHECK_LE(event_ptr + sizeof(inotify_event), event_buf + r);
1292 CHECK_LE(event->name + event->len, event_buf + r);
[email protected]d7395e732009-08-28 23:13:431293 if (!strcmp(event->name, "kioslaverc"))
1294 kioslaverc_touched = true;
1295 // Advance the pointer just past the end of the filename.
1296 event_ptr = event->name + event->len;
1297 }
1298 // We keep reading even if |kioslaverc_touched| is true to drain the
1299 // inotify event queue.
1300 }
1301 if (!r)
1302 // Instead of returning -1 and setting errno to EINVAL if there is not
1303 // enough buffer space, older kernels (< 2.6.21) return 0. Simulate the
1304 // new behavior (EINVAL) so we can reuse the code below.
1305 errno = EINVAL;
1306 if (errno != EAGAIN) {
[email protected]57b765672009-10-13 18:27:401307 PLOG(WARNING) << "error reading inotify file descriptor";
[email protected]d7395e732009-08-28 23:13:431308 if (errno == EINVAL) {
1309 // Our buffer is not large enough to read the next event. This should
1310 // not happen (because its size is calculated to always be sufficiently
1311 // large), but if it does we'd warn continuously since |inotify_fd_|
1312 // would be forever ready to read. Close it and stop watching instead.
1313 LOG(ERROR) << "inotify failure; no longer watching kioslaverc!";
1314 inotify_watcher_.StopWatchingFileDescriptor();
1315 close(inotify_fd_);
1316 inotify_fd_ = -1;
1317 }
1318 }
1319 if (kioslaverc_touched) {
1320 // We don't use Reset() because the timer may not yet be running.
1321 // (In that case Stop() is a no-op.)
marshall8e5fe942015-03-06 19:22:401322 debounce_timer_->Stop();
1323 debounce_timer_->Start(FROM_HERE, base::TimeDelta::FromMilliseconds(
[email protected]d7395e732009-08-28 23:13:431324 kDebounceTimeoutMilliseconds), this,
[email protected]573c0502011-05-17 22:19:501325 &SettingGetterImplKDE::OnDebouncedNotification);
[email protected]d7395e732009-08-28 23:13:431326 }
1327 }
1328
[email protected]6b5fe742011-05-20 21:46:481329 typedef std::map<StringSetting, std::string> string_map_type;
1330 typedef std::map<StringListSetting,
1331 std::vector<std::string> > strings_map_type;
[email protected]d7395e732009-08-28 23:13:431332
1333 int inotify_fd_;
1334 base::MessagePumpLibevent::FileDescriptorWatcher inotify_watcher_;
1335 ProxyConfigServiceLinux::Delegate* notify_delegate_;
danakj8c3eb802015-09-24 07:53:001336 scoped_ptr<base::OneShotTimer> debounce_timer_;
[email protected]6cdfd7f2013-02-08 20:40:151337 base::FilePath kde_config_dir_;
[email protected]d7395e732009-08-28 23:13:431338 bool indirect_manual_;
1339 bool auto_no_pac_;
[email protected]a48bf4a2010-06-14 18:24:531340 bool reversed_bypass_list_;
[email protected]f18fde22010-05-18 23:49:541341 // We don't own |env_var_getter_|. It's safe to hold a pointer to it, since
1342 // both it and us are owned by ProxyConfigServiceLinux::Delegate, and have the
1343 // same lifetime.
[email protected]76b90d312010-08-03 03:00:501344 base::Environment* env_var_getter_;
[email protected]d7395e732009-08-28 23:13:431345
1346 // We cache these settings whenever we re-read the kioslaverc file.
1347 string_map_type string_table_;
1348 strings_map_type strings_table_;
1349
sergeyu3f923062014-09-05 01:39:401350 // Task runner of the file thread, for reading kioslaverc. If NULL,
[email protected]d7395e732009-08-28 23:13:431351 // just read it directly (for testing). We also handle inotify events
1352 // on this thread.
sergeyu3f923062014-09-05 01:39:401353 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_;
[email protected]d7395e732009-08-28 23:13:431354
[email protected]573c0502011-05-17 22:19:501355 DISALLOW_COPY_AND_ASSIGN(SettingGetterImplKDE);
[email protected]861c6c62009-04-20 16:50:561356};
1357
1358} // namespace
1359
[email protected]573c0502011-05-17 22:19:501360bool ProxyConfigServiceLinux::Delegate::GetProxyFromSettings(
[email protected]6b5fe742011-05-20 21:46:481361 SettingGetter::StringSetting host_key,
[email protected]573c0502011-05-17 22:19:501362 ProxyServer* result_server) {
[email protected]861c6c62009-04-20 16:50:561363 std::string host;
[email protected]573c0502011-05-17 22:19:501364 if (!setting_getter_->GetString(host_key, &host) || host.empty()) {
[email protected]861c6c62009-04-20 16:50:561365 // Unset or empty.
1366 return false;
1367 }
1368 // Check for an optional port.
[email protected]d7395e732009-08-28 23:13:431369 int port = 0;
[email protected]6b5fe742011-05-20 21:46:481370 SettingGetter::IntSetting port_key =
[email protected]573c0502011-05-17 22:19:501371 SettingGetter::HostSettingToPortSetting(host_key);
1372 setting_getter_->GetInt(port_key, &port);
[email protected]861c6c62009-04-20 16:50:561373 if (port != 0) {
1374 // If a port is set and non-zero:
[email protected]528c56d2010-07-30 19:28:441375 host += ":" + base::IntToString(port);
[email protected]861c6c62009-04-20 16:50:561376 }
[email protected]76960f3d2011-04-30 02:15:231377
[email protected]573c0502011-05-17 22:19:501378 // gconf settings do not appear to distinguish between SOCKS version. We
1379 // default to version 5. For more information on this policy decision, see:
[email protected]76960f3d2011-04-30 02:15:231380 // http://code.google.com/p/chromium/issues/detail?id=55912#c2
[email protected]573c0502011-05-17 22:19:501381 ProxyServer::Scheme scheme = (host_key == SettingGetter::PROXY_SOCKS_HOST) ?
1382 ProxyServer::SCHEME_SOCKS5 : ProxyServer::SCHEME_HTTP;
1383 host = FixupProxyHostScheme(scheme, host);
[email protected]87a102b2009-07-14 05:23:301384 ProxyServer proxy_server = ProxyServer::FromURI(host,
1385 ProxyServer::SCHEME_HTTP);
[email protected]861c6c62009-04-20 16:50:561386 if (proxy_server.is_valid()) {
1387 *result_server = proxy_server;
1388 return true;
1389 }
1390 return false;
1391}
1392
[email protected]573c0502011-05-17 22:19:501393bool ProxyConfigServiceLinux::Delegate::GetConfigFromSettings(
[email protected]3e44697f2009-05-22 14:37:391394 ProxyConfig* config) {
[email protected]861c6c62009-04-20 16:50:561395 std::string mode;
[email protected]573c0502011-05-17 22:19:501396 if (!setting_getter_->GetString(SettingGetter::PROXY_MODE, &mode)) {
[email protected]861c6c62009-04-20 16:50:561397 // We expect this to always be set, so if we don't see it then we
[email protected]573c0502011-05-17 22:19:501398 // probably have a gconf/gsettings problem, and so we don't have a valid
[email protected]861c6c62009-04-20 16:50:561399 // proxy config.
1400 return false;
1401 }
[email protected]3e44697f2009-05-22 14:37:391402 if (mode == "none") {
[email protected]861c6c62009-04-20 16:50:561403 // Specifically specifies no proxy.
1404 return true;
[email protected]3e44697f2009-05-22 14:37:391405 }
[email protected]861c6c62009-04-20 16:50:561406
[email protected]3e44697f2009-05-22 14:37:391407 if (mode == "auto") {
[email protected]aa3ac2cc2012-06-19 00:28:041408 // Automatic proxy config.
[email protected]861c6c62009-04-20 16:50:561409 std::string pac_url_str;
[email protected]573c0502011-05-17 22:19:501410 if (setting_getter_->GetString(SettingGetter::PROXY_AUTOCONF_URL,
1411 &pac_url_str)) {
[email protected]861c6c62009-04-20 16:50:561412 if (!pac_url_str.empty()) {
[email protected]aa3ac2cc2012-06-19 00:28:041413 // If the PAC URL is actually a file path, then put file:// in front.
1414 if (pac_url_str[0] == '/')
1415 pac_url_str = "file://" + pac_url_str;
[email protected]861c6c62009-04-20 16:50:561416 GURL pac_url(pac_url_str);
1417 if (!pac_url.is_valid())
1418 return false;
[email protected]ed4ed0f2010-02-24 00:20:481419 config->set_pac_url(pac_url);
[email protected]861c6c62009-04-20 16:50:561420 return true;
1421 }
1422 }
[email protected]ed4ed0f2010-02-24 00:20:481423 config->set_auto_detect(true);
[email protected]861c6c62009-04-20 16:50:561424 return true;
1425 }
1426
[email protected]3e44697f2009-05-22 14:37:391427 if (mode != "manual") {
[email protected]861c6c62009-04-20 16:50:561428 // Mode is unrecognized.
1429 return false;
1430 }
1431 bool use_http_proxy;
[email protected]573c0502011-05-17 22:19:501432 if (setting_getter_->GetBool(SettingGetter::PROXY_USE_HTTP_PROXY,
1433 &use_http_proxy)
[email protected]861c6c62009-04-20 16:50:561434 && !use_http_proxy) {
1435 // Another master switch for some reason. If set to false, then no
1436 // proxy. But we don't panic if the key doesn't exist.
1437 return true;
1438 }
1439
1440 bool same_proxy = false;
1441 // Indicates to use the http proxy for all protocols. This one may
[email protected]573c0502011-05-17 22:19:501442 // not exist (presumably on older versions); we assume false in that
[email protected]861c6c62009-04-20 16:50:561443 // case.
[email protected]573c0502011-05-17 22:19:501444 setting_getter_->GetBool(SettingGetter::PROXY_USE_SAME_PROXY,
1445 &same_proxy);
[email protected]861c6c62009-04-20 16:50:561446
[email protected]76960f3d2011-04-30 02:15:231447 ProxyServer proxy_for_http;
1448 ProxyServer proxy_for_https;
1449 ProxyServer proxy_for_ftp;
1450 ProxyServer socks_proxy; // (socks)
1451
1452 // This counts how many of the above ProxyServers were defined and valid.
1453 size_t num_proxies_specified = 0;
1454
1455 // Extract the per-scheme proxies. If we failed to parse it, or no proxy was
1456 // specified for the scheme, then the resulting ProxyServer will be invalid.
[email protected]573c0502011-05-17 22:19:501457 if (GetProxyFromSettings(SettingGetter::PROXY_HTTP_HOST, &proxy_for_http))
[email protected]76960f3d2011-04-30 02:15:231458 num_proxies_specified++;
[email protected]573c0502011-05-17 22:19:501459 if (GetProxyFromSettings(SettingGetter::PROXY_HTTPS_HOST, &proxy_for_https))
[email protected]76960f3d2011-04-30 02:15:231460 num_proxies_specified++;
[email protected]573c0502011-05-17 22:19:501461 if (GetProxyFromSettings(SettingGetter::PROXY_FTP_HOST, &proxy_for_ftp))
[email protected]76960f3d2011-04-30 02:15:231462 num_proxies_specified++;
[email protected]573c0502011-05-17 22:19:501463 if (GetProxyFromSettings(SettingGetter::PROXY_SOCKS_HOST, &socks_proxy))
[email protected]76960f3d2011-04-30 02:15:231464 num_proxies_specified++;
1465
1466 if (same_proxy) {
1467 if (proxy_for_http.is_valid()) {
1468 // Use the http proxy for all schemes.
[email protected]ed4ed0f2010-02-24 00:20:481469 config->proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY;
[email protected]2189e092013-03-16 18:02:021470 config->proxy_rules().single_proxies.SetSingleProxyServer(proxy_for_http);
[email protected]861c6c62009-04-20 16:50:561471 }
[email protected]76960f3d2011-04-30 02:15:231472 } else if (num_proxies_specified > 0) {
1473 if (socks_proxy.is_valid() && num_proxies_specified == 1) {
1474 // If the only proxy specified was for SOCKS, use it for all schemes.
1475 config->proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY;
[email protected]2189e092013-03-16 18:02:021476 config->proxy_rules().single_proxies.SetSingleProxyServer(socks_proxy);
[email protected]861c6c62009-04-20 16:50:561477 } else {
[email protected]2189e092013-03-16 18:02:021478 // Otherwise use the indicated proxies per-scheme.
[email protected]76960f3d2011-04-30 02:15:231479 config->proxy_rules().type =
1480 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME;
[email protected]2189e092013-03-16 18:02:021481 config->proxy_rules().proxies_for_http.
1482 SetSingleProxyServer(proxy_for_http);
1483 config->proxy_rules().proxies_for_https.
1484 SetSingleProxyServer(proxy_for_https);
1485 config->proxy_rules().proxies_for_ftp.SetSingleProxyServer(proxy_for_ftp);
1486 config->proxy_rules().fallback_proxies.SetSingleProxyServer(socks_proxy);
[email protected]861c6c62009-04-20 16:50:561487 }
1488 }
1489
[email protected]ed4ed0f2010-02-24 00:20:481490 if (config->proxy_rules().empty()) {
[email protected]861c6c62009-04-20 16:50:561491 // Manual mode but we couldn't parse any rules.
1492 return false;
1493 }
1494
1495 // Check for authentication, just so we can warn.
[email protected]d7395e732009-08-28 23:13:431496 bool use_auth = false;
[email protected]573c0502011-05-17 22:19:501497 setting_getter_->GetBool(SettingGetter::PROXY_USE_AUTHENTICATION,
1498 &use_auth);
[email protected]62749f182009-07-15 13:16:541499 if (use_auth) {
1500 // ProxyConfig does not support authentication parameters, but
1501 // Chrome will prompt for the password later. So we ignore
1502 // /system/http_proxy/*auth* settings.
1503 LOG(WARNING) << "Proxy authentication parameters ignored, see bug 16709";
1504 }
[email protected]861c6c62009-04-20 16:50:561505
1506 // Now the bypass list.
[email protected]7541206c2010-02-19 20:24:061507 std::vector<std::string> ignore_hosts_list;
[email protected]ed4ed0f2010-02-24 00:20:481508 config->proxy_rules().bypass_rules.Clear();
[email protected]573c0502011-05-17 22:19:501509 if (setting_getter_->GetStringList(SettingGetter::PROXY_IGNORE_HOSTS,
1510 &ignore_hosts_list)) {
[email protected]a8185d02010-06-11 00:19:501511 std::vector<std::string>::const_iterator it(ignore_hosts_list.begin());
[email protected]1a597192010-07-09 16:58:381512 for (; it != ignore_hosts_list.end(); ++it) {
[email protected]573c0502011-05-17 22:19:501513 if (setting_getter_->MatchHostsUsingSuffixMatching()) {
[email protected]1a597192010-07-09 16:58:381514 config->proxy_rules().bypass_rules.
1515 AddRuleFromStringUsingSuffixMatching(*it);
1516 } else {
1517 config->proxy_rules().bypass_rules.AddRuleFromString(*it);
1518 }
1519 }
[email protected]a8185d02010-06-11 00:19:501520 }
[email protected]861c6c62009-04-20 16:50:561521 // Note that there are no settings with semantics corresponding to
[email protected]1a597192010-07-09 16:58:381522 // bypass of local names in GNOME. In KDE, "<local>" is supported
1523 // as a hostname rule.
[email protected]861c6c62009-04-20 16:50:561524
[email protected]a48bf4a2010-06-14 18:24:531525 // KDE allows one to reverse the bypass rules.
[email protected]573c0502011-05-17 22:19:501526 config->proxy_rules().reverse_bypass =
1527 setting_getter_->BypassListIsReversed();
[email protected]a48bf4a2010-06-14 18:24:531528
[email protected]861c6c62009-04-20 16:50:561529 return true;
1530}
1531
[email protected]76b90d312010-08-03 03:00:501532ProxyConfigServiceLinux::Delegate::Delegate(base::Environment* env_var_getter)
[email protected]76722472012-05-24 08:26:461533 : env_var_getter_(env_var_getter) {
[email protected]573c0502011-05-17 22:19:501534 // Figure out which SettingGetterImpl to use, if any.
[email protected]6b0349ef2010-10-16 04:56:061535 switch (base::nix::GetDesktopEnvironment(env_var_getter)) {
1536 case base::nix::DESKTOP_ENVIRONMENT_GNOME:
[email protected]9e6c9bde2012-07-17 23:40:171537 case base::nix::DESKTOP_ENVIRONMENT_UNITY:
[email protected]8c20e3d2011-05-19 21:03:571538#if defined(USE_GIO)
1539 {
1540 scoped_ptr<SettingGetterImplGSettings> gs_getter(
1541 new SettingGetterImplGSettings());
1542 // We have to load symbols and check the GNOME version in use to decide
1543 // if we should use the gsettings getter. See LoadAndCheckVersion().
1544 if (gs_getter->LoadAndCheckVersion(env_var_getter))
1545 setting_getter_.reset(gs_getter.release());
1546 }
1547#endif
[email protected]6de53d42010-11-09 07:33:191548#if defined(USE_GCONF)
[email protected]8c20e3d2011-05-19 21:03:571549 // Fall back on gconf if gsettings is unavailable or incorrect.
1550 if (!setting_getter_.get())
1551 setting_getter_.reset(new SettingGetterImplGConf());
[email protected]6de53d42010-11-09 07:33:191552#endif
[email protected]d7395e732009-08-28 23:13:431553 break;
[email protected]6b0349ef2010-10-16 04:56:061554 case base::nix::DESKTOP_ENVIRONMENT_KDE3:
1555 case base::nix::DESKTOP_ENVIRONMENT_KDE4:
edward.baker53bec302015-10-02 16:57:491556 case base::nix::DESKTOP_ENVIRONMENT_KDE5:
[email protected]573c0502011-05-17 22:19:501557 setting_getter_.reset(new SettingGetterImplKDE(env_var_getter));
[email protected]d7395e732009-08-28 23:13:431558 break;
[email protected]6b0349ef2010-10-16 04:56:061559 case base::nix::DESKTOP_ENVIRONMENT_XFCE:
1560 case base::nix::DESKTOP_ENVIRONMENT_OTHER:
[email protected]d7395e732009-08-28 23:13:431561 break;
1562 }
1563}
1564
[email protected]573c0502011-05-17 22:19:501565ProxyConfigServiceLinux::Delegate::Delegate(
1566 base::Environment* env_var_getter, SettingGetter* setting_getter)
[email protected]76722472012-05-24 08:26:461567 : env_var_getter_(env_var_getter), setting_getter_(setting_getter) {
[email protected]861c6c62009-04-20 16:50:561568}
1569
[email protected]d3066142011-05-10 02:36:201570void ProxyConfigServiceLinux::Delegate::SetUpAndFetchInitialConfig(
sergeyu3f923062014-09-05 01:39:401571 const scoped_refptr<base::SingleThreadTaskRunner>& glib_task_runner,
1572 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner,
1573 const scoped_refptr<base::SingleThreadTaskRunner>& file_task_runner) {
[email protected]3e44697f2009-05-22 14:37:391574 // We should be running on the default glib main loop thread right
1575 // now. gconf can only be accessed from this thread.
sergeyu3f923062014-09-05 01:39:401576 DCHECK(glib_task_runner->BelongsToCurrentThread());
1577 glib_task_runner_ = glib_task_runner;
1578 io_task_runner_ = io_task_runner;
[email protected]3e44697f2009-05-22 14:37:391579
sergeyu3f923062014-09-05 01:39:401580 // If we are passed a NULL |io_task_runner| or |file_task_runner|, then we
1581 // don't set up proxy setting change notifications. This should not be the
1582 // usual case but is intended to/ simplify test setups.
Daniel Cheng99adf742014-09-05 06:26:501583 if (!io_task_runner_.get() || !file_task_runner.get())
[email protected]b30a3f52010-10-16 01:05:461584 VLOG(1) << "Monitoring of proxy setting changes is disabled";
[email protected]3e44697f2009-05-22 14:37:391585
1586 // Fetch and cache the current proxy config. The config is left in
[email protected]119655002010-07-23 06:02:401587 // cached_config_, where GetLatestProxyConfig() running on the IO thread
[email protected]3e44697f2009-05-22 14:37:391588 // will expect to find it. This is safe to do because we return
1589 // before this ProxyConfigServiceLinux is passed on to
1590 // the ProxyService.
[email protected]d6cb85b2009-07-23 22:10:531591
1592 // Note: It would be nice to prioritize environment variables
[email protected]92d2dc82010-04-08 17:49:591593 // and only fall back to gconf if env vars were unset. But
[email protected]d6cb85b2009-07-23 22:10:531594 // gnome-terminal "helpfully" sets http_proxy and no_proxy, and it
1595 // does so even if the proxy mode is set to auto, which would
1596 // mislead us.
1597
[email protected]3e44697f2009-05-22 14:37:391598 bool got_config = false;
[email protected]573c0502011-05-17 22:19:501599 if (setting_getter_.get() &&
sergeyu3f923062014-09-05 01:39:401600 setting_getter_->Init(glib_task_runner, file_task_runner) &&
[email protected]573c0502011-05-17 22:19:501601 GetConfigFromSettings(&cached_config_)) {
[email protected]d3066142011-05-10 02:36:201602 cached_config_.set_id(1); // Mark it as valid.
[email protected]db8ff912012-06-12 23:32:511603 cached_config_.set_source(setting_getter_->GetConfigSource());
[email protected]d3066142011-05-10 02:36:201604 VLOG(1) << "Obtained proxy settings from "
[email protected]db8ff912012-06-12 23:32:511605 << ProxyConfigSourceToString(cached_config_.source());
[email protected]d3066142011-05-10 02:36:201606
1607 // If gconf proxy mode is "none", meaning direct, then we take
1608 // that to be a valid config and will not check environment
1609 // variables. The alternative would have been to look for a proxy
1610 // whereever we can find one.
1611 got_config = true;
1612
1613 // Keep a copy of the config for use from this thread for
1614 // comparison with updated settings when we get notifications.
1615 reference_config_ = cached_config_;
1616 reference_config_.set_id(1); // Mark it as valid.
1617
1618 // We only set up notifications if we have IO and file loops available.
1619 // We do this after getting the initial configuration so that we don't have
1620 // to worry about cancelling it if the initial fetch above fails. Note that
1621 // setting up notifications has the side effect of simulating a change, so
1622 // that we won't lose any updates that may have happened after the initial
1623 // fetch and before setting up notifications. We'll detect the common case
1624 // of no changes in OnCheckProxyConfigSettings() (or sooner) and ignore it.
Daniel Cheng99adf742014-09-05 06:26:501625 if (io_task_runner.get() && file_task_runner.get()) {
[email protected]76722472012-05-24 08:26:461626 scoped_refptr<base::SingleThreadTaskRunner> required_loop =
1627 setting_getter_->GetNotificationTaskRunner();
[email protected]90499482013-06-01 00:39:501628 if (!required_loop.get() || required_loop->BelongsToCurrentThread()) {
[email protected]d3066142011-05-10 02:36:201629 // In this case we are already on an acceptable thread.
1630 SetUpNotifications();
[email protected]d7395e732009-08-28 23:13:431631 } else {
[email protected]d3066142011-05-10 02:36:201632 // Post a task to set up notifications. We don't wait for success.
[email protected]6af889c2011-10-06 23:11:411633 required_loop->PostTask(FROM_HERE, base::Bind(
1634 &ProxyConfigServiceLinux::Delegate::SetUpNotifications, this));
[email protected]d6cb85b2009-07-23 22:10:531635 }
[email protected]d7395e732009-08-28 23:13:431636 }
[email protected]861c6c62009-04-20 16:50:561637 }
[email protected]d6cb85b2009-07-23 22:10:531638
[email protected]3e44697f2009-05-22 14:37:391639 if (!got_config) {
[email protected]d6cb85b2009-07-23 22:10:531640 // We fall back on environment variables.
[email protected]3e44697f2009-05-22 14:37:391641 //
[email protected]d3066142011-05-10 02:36:201642 // Consulting environment variables doesn't need to be done from the
1643 // default glib main loop, but it's a tiny enough amount of work.
[email protected]3e44697f2009-05-22 14:37:391644 if (GetConfigFromEnv(&cached_config_)) {
[email protected]db8ff912012-06-12 23:32:511645 cached_config_.set_source(PROXY_CONFIG_SOURCE_ENV);
[email protected]d3066142011-05-10 02:36:201646 cached_config_.set_id(1); // Mark it as valid.
[email protected]b30a3f52010-10-16 01:05:461647 VLOG(1) << "Obtained proxy settings from environment variables";
[email protected]3e44697f2009-05-22 14:37:391648 }
[email protected]861c6c62009-04-20 16:50:561649 }
[email protected]3e44697f2009-05-22 14:37:391650}
1651
[email protected]573c0502011-05-17 22:19:501652// Depending on the SettingGetter in use, this method will be called
[email protected]d3066142011-05-10 02:36:201653// on either the UI thread (GConf) or the file thread (KDE).
1654void ProxyConfigServiceLinux::Delegate::SetUpNotifications() {
[email protected]76722472012-05-24 08:26:461655 scoped_refptr<base::SingleThreadTaskRunner> required_loop =
1656 setting_getter_->GetNotificationTaskRunner();
[email protected]90499482013-06-01 00:39:501657 DCHECK(!required_loop.get() || required_loop->BelongsToCurrentThread());
[email protected]573c0502011-05-17 22:19:501658 if (!setting_getter_->SetUpNotifications(this))
[email protected]d3066142011-05-10 02:36:201659 LOG(ERROR) << "Unable to set up proxy configuration change notifications";
1660}
1661
[email protected]119655002010-07-23 06:02:401662void ProxyConfigServiceLinux::Delegate::AddObserver(Observer* observer) {
1663 observers_.AddObserver(observer);
1664}
1665
1666void ProxyConfigServiceLinux::Delegate::RemoveObserver(Observer* observer) {
1667 observers_.RemoveObserver(observer);
1668}
1669
[email protected]3a29593d2011-04-11 10:07:521670ProxyConfigService::ConfigAvailability
1671 ProxyConfigServiceLinux::Delegate::GetLatestProxyConfig(
1672 ProxyConfig* config) {
[email protected]3e44697f2009-05-22 14:37:391673 // This is called from the IO thread.
sergeyu3f923062014-09-05 01:39:401674 DCHECK(!io_task_runner_.get() ||
1675 io_task_runner_->BelongsToCurrentThread());
[email protected]3e44697f2009-05-22 14:37:391676
1677 // Simply return the last proxy configuration that glib_default_loop
1678 // notified us of.
[email protected]db8ff912012-06-12 23:32:511679 if (cached_config_.is_valid()) {
1680 *config = cached_config_;
1681 } else {
1682 *config = ProxyConfig::CreateDirect();
1683 config->set_source(PROXY_CONFIG_SOURCE_SYSTEM_FAILED);
1684 }
[email protected]119655002010-07-23 06:02:401685
[email protected]3a29593d2011-04-11 10:07:521686 // We return CONFIG_VALID to indicate that *config was filled in. It is always
[email protected]119655002010-07-23 06:02:401687 // going to be available since we initialized eagerly on the UI thread.
1688 // TODO(eroman): do lazy initialization instead, so we no longer need
1689 // to construct ProxyConfigServiceLinux on the UI thread.
1690 // In which case, we may return false here.
[email protected]3a29593d2011-04-11 10:07:521691 return CONFIG_VALID;
[email protected]3e44697f2009-05-22 14:37:391692}
1693
[email protected]573c0502011-05-17 22:19:501694// Depending on the SettingGetter in use, this method will be called
[email protected]d7395e732009-08-28 23:13:431695// on either the UI thread (GConf) or the file thread (KDE).
[email protected]3e44697f2009-05-22 14:37:391696void ProxyConfigServiceLinux::Delegate::OnCheckProxyConfigSettings() {
[email protected]76722472012-05-24 08:26:461697 scoped_refptr<base::SingleThreadTaskRunner> required_loop =
1698 setting_getter_->GetNotificationTaskRunner();
[email protected]90499482013-06-01 00:39:501699 DCHECK(!required_loop.get() || required_loop->BelongsToCurrentThread());
[email protected]3e44697f2009-05-22 14:37:391700 ProxyConfig new_config;
[email protected]573c0502011-05-17 22:19:501701 bool valid = GetConfigFromSettings(&new_config);
[email protected]3e44697f2009-05-22 14:37:391702 if (valid)
1703 new_config.set_id(1); // mark it as valid
1704
[email protected]119655002010-07-23 06:02:401705 // See if it is different from what we had before.
[email protected]3e44697f2009-05-22 14:37:391706 if (new_config.is_valid() != reference_config_.is_valid() ||
1707 !new_config.Equals(reference_config_)) {
[email protected]76722472012-05-24 08:26:461708 // Post a task to the IO thread with the new configuration, so it can
[email protected]3e44697f2009-05-22 14:37:391709 // update |cached_config_|.
sergeyu3f923062014-09-05 01:39:401710 io_task_runner_->PostTask(FROM_HERE, base::Bind(
[email protected]6af889c2011-10-06 23:11:411711 &ProxyConfigServiceLinux::Delegate::SetNewProxyConfig,
1712 this, new_config));
[email protected]d1f9d472009-08-13 19:59:301713 // Update the thread-private copy in |reference_config_| as well.
1714 reference_config_ = new_config;
[email protected]d3066142011-05-10 02:36:201715 } else {
1716 VLOG(1) << "Detected no-op change to proxy settings. Doing nothing.";
[email protected]3e44697f2009-05-22 14:37:391717 }
1718}
1719
1720void ProxyConfigServiceLinux::Delegate::SetNewProxyConfig(
1721 const ProxyConfig& new_config) {
sergeyu3f923062014-09-05 01:39:401722 DCHECK(io_task_runner_->BelongsToCurrentThread());
[email protected]b30a3f52010-10-16 01:05:461723 VLOG(1) << "Proxy configuration changed";
[email protected]3e44697f2009-05-22 14:37:391724 cached_config_ = new_config;
[email protected]3a29593d2011-04-11 10:07:521725 FOR_EACH_OBSERVER(
1726 Observer, observers_,
1727 OnProxyConfigChanged(new_config, ProxyConfigService::CONFIG_VALID));
[email protected]3e44697f2009-05-22 14:37:391728}
1729
1730void ProxyConfigServiceLinux::Delegate::PostDestroyTask() {
[email protected]573c0502011-05-17 22:19:501731 if (!setting_getter_.get())
[email protected]d7395e732009-08-28 23:13:431732 return;
[email protected]76722472012-05-24 08:26:461733 scoped_refptr<base::SingleThreadTaskRunner> shutdown_loop =
1734 setting_getter_->GetNotificationTaskRunner();
[email protected]90499482013-06-01 00:39:501735 if (!shutdown_loop.get() || shutdown_loop->BelongsToCurrentThread()) {
[email protected]3e44697f2009-05-22 14:37:391736 // Already on the right thread, call directly.
1737 // This is the case for the unittests.
1738 OnDestroy();
1739 } else {
[email protected]d7395e732009-08-28 23:13:431740 // Post to shutdown thread. Note that on browser shutdown, we may quit
1741 // this MessageLoop and exit the program before ever running this.
[email protected]6af889c2011-10-06 23:11:411742 shutdown_loop->PostTask(FROM_HERE, base::Bind(
1743 &ProxyConfigServiceLinux::Delegate::OnDestroy, this));
[email protected]3e44697f2009-05-22 14:37:391744 }
1745}
1746void ProxyConfigServiceLinux::Delegate::OnDestroy() {
[email protected]76722472012-05-24 08:26:461747 scoped_refptr<base::SingleThreadTaskRunner> shutdown_loop =
1748 setting_getter_->GetNotificationTaskRunner();
[email protected]90499482013-06-01 00:39:501749 DCHECK(!shutdown_loop.get() || shutdown_loop->BelongsToCurrentThread());
[email protected]573c0502011-05-17 22:19:501750 setting_getter_->ShutDown();
[email protected]3e44697f2009-05-22 14:37:391751}
1752
1753ProxyConfigServiceLinux::ProxyConfigServiceLinux()
[email protected]76b90d312010-08-03 03:00:501754 : delegate_(new Delegate(base::Environment::Create())) {
[email protected]3e44697f2009-05-22 14:37:391755}
1756
[email protected]8e1845e12010-09-15 19:22:241757ProxyConfigServiceLinux::~ProxyConfigServiceLinux() {
1758 delegate_->PostDestroyTask();
1759}
1760
[email protected]3e44697f2009-05-22 14:37:391761ProxyConfigServiceLinux::ProxyConfigServiceLinux(
[email protected]76b90d312010-08-03 03:00:501762 base::Environment* env_var_getter)
[email protected]9a3d8d42009-09-03 17:01:461763 : delegate_(new Delegate(env_var_getter)) {
1764}
1765
1766ProxyConfigServiceLinux::ProxyConfigServiceLinux(
[email protected]573c0502011-05-17 22:19:501767 base::Environment* env_var_getter, SettingGetter* setting_getter)
1768 : delegate_(new Delegate(env_var_getter, setting_getter)) {
[email protected]861c6c62009-04-20 16:50:561769}
1770
[email protected]e4be2dd2010-12-14 00:44:391771void ProxyConfigServiceLinux::AddObserver(Observer* observer) {
1772 delegate_->AddObserver(observer);
1773}
1774
1775void ProxyConfigServiceLinux::RemoveObserver(Observer* observer) {
1776 delegate_->RemoveObserver(observer);
1777}
1778
[email protected]3a29593d2011-04-11 10:07:521779ProxyConfigService::ConfigAvailability
1780 ProxyConfigServiceLinux::GetLatestProxyConfig(ProxyConfig* config) {
[email protected]e4be2dd2010-12-14 00:44:391781 return delegate_->GetLatestProxyConfig(config);
1782}
1783
[email protected]861c6c62009-04-20 16:50:561784} // namespace net