blob: 145312e7c6b93120d3de11b6193bc3c2deaff7ce [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>
8#include <fcntl.h>
[email protected]6de53d42010-11-09 07:33:199#if defined(USE_GCONF)
[email protected]861c6c62009-04-20 16:50:5610#include <gconf/gconf-client.h>
[email protected]8c20e3d2011-05-19 21:03:5711#endif // defined(USE_GCONF)
[email protected]d7395e732009-08-28 23:13:4312#include <limits.h>
13#include <stdio.h>
[email protected]861c6c62009-04-20 16:50:5614#include <stdlib.h>
[email protected]d7395e732009-08-28 23:13:4315#include <sys/inotify.h>
16#include <unistd.h>
[email protected]861c6c62009-04-20 16:50:5617
[email protected]9bc8cff2010-04-03 01:05:3918#include <map>
19
[email protected]6af889c2011-10-06 23:11:4120#include "base/bind.h"
[email protected]c4c1b482011-07-22 17:24:2621#include "base/compiler_specific.h"
[email protected]4bbb72d2014-06-06 18:05:5122#include "base/debug/leak_annotations.h"
[email protected]76b90d312010-08-03 03:00:5023#include "base/environment.h"
[email protected]57999812013-02-24 05:40:5224#include "base/files/file_path.h"
thestigd8df0332014-09-04 06:33:2925#include "base/files/file_util.h"
[email protected]b9b4a572014-03-17 23:11:1226#include "base/files/scoped_file.h"
[email protected]861c6c62009-04-20 16:50:5627#include "base/logging.h"
[email protected]18b577412013-07-18 04:19:1528#include "base/message_loop/message_loop.h"
[email protected]3a29593d2011-04-11 10:07:5229#include "base/nix/xdg_util.h"
[email protected]76722472012-05-24 08:26:4630#include "base/single_thread_task_runner.h"
[email protected]fc9be5802013-06-11 10:56:5131#include "base/strings/string_number_conversions.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 &&
58 StartsWithASCII(host, "socks4://", false)) {
59 // We default to socks 5, but if the user specifically set it to
60 // socks4://, then use that.
61 scheme = ProxyServer::SCHEME_SOCKS4;
[email protected]861c6c62009-04-20 16:50:5662 }
63 // Strip the scheme if any.
64 std::string::size_type colon = host.find("://");
65 if (colon != std::string::npos)
66 host = host.substr(colon + 3);
67 // If a username and perhaps password are specified, give a warning.
68 std::string::size_type at_sign = host.find("@");
69 // Should this be supported?
70 if (at_sign != std::string::npos) {
[email protected]62749f182009-07-15 13:16:5471 // ProxyConfig does not support authentication parameters, but Chrome
72 // will prompt for the password later. Disregard the
73 // authentication parameters and continue with this hostname.
74 LOG(WARNING) << "Proxy authentication parameters ignored, see bug 16709";
[email protected]861c6c62009-04-20 16:50:5675 host = host.substr(at_sign + 1);
76 }
77 // If this is a socks proxy, prepend a scheme so as to tell
78 // ProxyServer. This also allows ProxyServer to choose the right
79 // default port.
80 if (scheme == ProxyServer::SCHEME_SOCKS4)
81 host = "socks4://" + host;
82 else if (scheme == ProxyServer::SCHEME_SOCKS5)
83 host = "socks5://" + host;
[email protected]d7395e732009-08-28 23:13:4384 // If there is a trailing slash, remove it so |host| will parse correctly
85 // even if it includes a port number (since the slash is not numeric).
86 if (host.length() && host[host.length() - 1] == '/')
87 host.resize(host.length() - 1);
[email protected]861c6c62009-04-20 16:50:5688 return host;
89}
90
91} // namespace
92
[email protected]8e1845e12010-09-15 19:22:2493ProxyConfigServiceLinux::Delegate::~Delegate() {
94}
95
[email protected]3e44697f2009-05-22 14:37:3996bool ProxyConfigServiceLinux::Delegate::GetProxyFromEnvVarForScheme(
[email protected]861c6c62009-04-20 16:50:5697 const char* variable, ProxyServer::Scheme scheme,
98 ProxyServer* result_server) {
99 std::string env_value;
[email protected]3ba7e082010-08-07 02:57:59100 if (env_var_getter_->GetVar(variable, &env_value)) {
[email protected]861c6c62009-04-20 16:50:56101 if (!env_value.empty()) {
102 env_value = FixupProxyHostScheme(scheme, env_value);
[email protected]87a102b2009-07-14 05:23:30103 ProxyServer proxy_server =
104 ProxyServer::FromURI(env_value, ProxyServer::SCHEME_HTTP);
[email protected]861c6c62009-04-20 16:50:56105 if (proxy_server.is_valid() && !proxy_server.is_direct()) {
106 *result_server = proxy_server;
107 return true;
108 } else {
[email protected]3e44697f2009-05-22 14:37:39109 LOG(ERROR) << "Failed to parse environment variable " << variable;
[email protected]861c6c62009-04-20 16:50:56110 }
111 }
112 }
113 return false;
114}
115
[email protected]3e44697f2009-05-22 14:37:39116bool ProxyConfigServiceLinux::Delegate::GetProxyFromEnvVar(
[email protected]861c6c62009-04-20 16:50:56117 const char* variable, ProxyServer* result_server) {
118 return GetProxyFromEnvVarForScheme(variable, ProxyServer::SCHEME_HTTP,
119 result_server);
120}
121
[email protected]3e44697f2009-05-22 14:37:39122bool ProxyConfigServiceLinux::Delegate::GetConfigFromEnv(ProxyConfig* config) {
[email protected]861c6c62009-04-20 16:50:56123 // Check for automatic configuration first, in
124 // "auto_proxy". Possibly only the "environment_proxy" firefox
125 // extension has ever used this, but it still sounds like a good
126 // idea.
127 std::string auto_proxy;
[email protected]3ba7e082010-08-07 02:57:59128 if (env_var_getter_->GetVar("auto_proxy", &auto_proxy)) {
[email protected]861c6c62009-04-20 16:50:56129 if (auto_proxy.empty()) {
130 // Defined and empty => autodetect
[email protected]ed4ed0f2010-02-24 00:20:48131 config->set_auto_detect(true);
[email protected]861c6c62009-04-20 16:50:56132 } else {
133 // specified autoconfig URL
[email protected]ed4ed0f2010-02-24 00:20:48134 config->set_pac_url(GURL(auto_proxy));
[email protected]861c6c62009-04-20 16:50:56135 }
136 return true;
137 }
138 // "all_proxy" is a shortcut to avoid defining {http,https,ftp}_proxy.
139 ProxyServer proxy_server;
140 if (GetProxyFromEnvVar("all_proxy", &proxy_server)) {
[email protected]ed4ed0f2010-02-24 00:20:48141 config->proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY;
[email protected]2189e092013-03-16 18:02:02142 config->proxy_rules().single_proxies.SetSingleProxyServer(proxy_server);
[email protected]861c6c62009-04-20 16:50:56143 } else {
144 bool have_http = GetProxyFromEnvVar("http_proxy", &proxy_server);
145 if (have_http)
[email protected]2189e092013-03-16 18:02:02146 config->proxy_rules().proxies_for_http.SetSingleProxyServer(proxy_server);
[email protected]861c6c62009-04-20 16:50:56147 // It would be tempting to let http_proxy apply for all protocols
148 // if https_proxy and ftp_proxy are not defined. Googling turns up
149 // several documents that mention only http_proxy. But then the
150 // user really might not want to proxy https. And it doesn't seem
151 // like other apps do this. So we will refrain.
152 bool have_https = GetProxyFromEnvVar("https_proxy", &proxy_server);
153 if (have_https)
[email protected]2189e092013-03-16 18:02:02154 config->proxy_rules().proxies_for_https.
155 SetSingleProxyServer(proxy_server);
[email protected]861c6c62009-04-20 16:50:56156 bool have_ftp = GetProxyFromEnvVar("ftp_proxy", &proxy_server);
157 if (have_ftp)
[email protected]2189e092013-03-16 18:02:02158 config->proxy_rules().proxies_for_ftp.SetSingleProxyServer(proxy_server);
[email protected]861c6c62009-04-20 16:50:56159 if (have_http || have_https || have_ftp) {
160 // mustn't change type unless some rules are actually set.
[email protected]ed4ed0f2010-02-24 00:20:48161 config->proxy_rules().type =
162 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME;
[email protected]861c6c62009-04-20 16:50:56163 }
164 }
[email protected]ed4ed0f2010-02-24 00:20:48165 if (config->proxy_rules().empty()) {
[email protected]861c6c62009-04-20 16:50:56166 // If the above were not defined, try for socks.
[email protected]e8c50812010-09-28 00:16:17167 // For environment variables, we default to version 5, per the gnome
168 // documentation: http://library.gnome.org/devel/gnet/stable/gnet-socks.html
169 ProxyServer::Scheme scheme = ProxyServer::SCHEME_SOCKS5;
[email protected]861c6c62009-04-20 16:50:56170 std::string env_version;
[email protected]3ba7e082010-08-07 02:57:59171 if (env_var_getter_->GetVar("SOCKS_VERSION", &env_version)
[email protected]e8c50812010-09-28 00:16:17172 && env_version == "4")
173 scheme = ProxyServer::SCHEME_SOCKS4;
[email protected]861c6c62009-04-20 16:50:56174 if (GetProxyFromEnvVarForScheme("SOCKS_SERVER", scheme, &proxy_server)) {
[email protected]ed4ed0f2010-02-24 00:20:48175 config->proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY;
[email protected]2189e092013-03-16 18:02:02176 config->proxy_rules().single_proxies.SetSingleProxyServer(proxy_server);
[email protected]861c6c62009-04-20 16:50:56177 }
178 }
179 // Look for the proxy bypass list.
180 std::string no_proxy;
[email protected]3ba7e082010-08-07 02:57:59181 env_var_getter_->GetVar("no_proxy", &no_proxy);
[email protected]ed4ed0f2010-02-24 00:20:48182 if (config->proxy_rules().empty()) {
[email protected]861c6c62009-04-20 16:50:56183 // Having only "no_proxy" set, presumably to "*", makes it
184 // explicit that env vars do specify a configuration: having no
185 // rules specified only means the user explicitly asks for direct
186 // connections.
187 return !no_proxy.empty();
188 }
[email protected]7541206c2010-02-19 20:24:06189 // Note that this uses "suffix" matching. So a bypass of "google.com"
190 // is understood to mean a bypass of "*google.com".
[email protected]ed4ed0f2010-02-24 00:20:48191 config->proxy_rules().bypass_rules.ParseFromStringUsingSuffixMatching(
192 no_proxy);
[email protected]861c6c62009-04-20 16:50:56193 return true;
194}
195
196namespace {
197
[email protected]d7395e732009-08-28 23:13:43198const int kDebounceTimeoutMilliseconds = 250;
[email protected]3e44697f2009-05-22 14:37:39199
[email protected]6de53d42010-11-09 07:33:19200#if defined(USE_GCONF)
[email protected]573c0502011-05-17 22:19:50201// This setting getter uses gconf, as used in GNOME 2 and some GNOME 3 desktops.
202class SettingGetterImplGConf : public ProxyConfigServiceLinux::SettingGetter {
[email protected]861c6c62009-04-20 16:50:56203 public:
[email protected]573c0502011-05-17 22:19:50204 SettingGetterImplGConf()
[email protected]b160df32012-02-06 20:39:41205 : client_(NULL), system_proxy_id_(0), system_http_proxy_id_(0),
[email protected]76722472012-05-24 08:26:46206 notify_delegate_(NULL) {
[email protected]b160df32012-02-06 20:39:41207 }
[email protected]3e44697f2009-05-22 14:37:39208
[email protected]573c0502011-05-17 22:19:50209 virtual ~SettingGetterImplGConf() {
[email protected]3e44697f2009-05-22 14:37:39210 // client_ should have been released before now, from
[email protected]f5b13442009-07-13 15:23:59211 // Delegate::OnDestroy(), while running on the UI thread. However
[email protected]b160df32012-02-06 20:39:41212 // on exiting the process, it may happen that Delegate::OnDestroy()
213 // task is left pending on the glib loop after the loop was quit,
214 // and pending tasks may then be deleted without being run.
[email protected]f5b13442009-07-13 15:23:59215 if (client_) {
216 // gconf client was not cleaned up.
[email protected]76722472012-05-24 08:26:46217 if (task_runner_->BelongsToCurrentThread()) {
[email protected]f5b13442009-07-13 15:23:59218 // We are on the UI thread so we can clean it safely. This is
219 // the case at least for ui_tests running under Valgrind in
220 // bug 16076.
[email protected]573c0502011-05-17 22:19:50221 VLOG(1) << "~SettingGetterImplGConf: releasing gconf client";
[email protected]d3066142011-05-10 02:36:20222 ShutDown();
[email protected]f5b13442009-07-13 15:23:59223 } else {
[email protected]ed348992011-09-19 20:27:57224 // This is very bad! We are deleting the setting getter but we're not on
225 // the UI thread. This is not supposed to happen: the setting getter is
226 // owned by the proxy config service's delegate, which is supposed to be
227 // destroyed on the UI thread only. We will get change notifications to
228 // a deleted object if we continue here, so fail now.
229 LOG(FATAL) << "~SettingGetterImplGConf: deleting on wrong thread!";
[email protected]f5b13442009-07-13 15:23:59230 }
231 }
[email protected]3e44697f2009-05-22 14:37:39232 DCHECK(!client_);
[email protected]861c6c62009-04-20 16:50:56233 }
234
sergeyu3f923062014-09-05 01:39:40235 virtual bool Init(
236 const scoped_refptr<base::SingleThreadTaskRunner>& glib_task_runner,
237 const scoped_refptr<base::SingleThreadTaskRunner>& file_task_runner)
mostynbba063d6032014-10-09 11:01:13238 override {
sergeyu3f923062014-09-05 01:39:40239 DCHECK(glib_task_runner->BelongsToCurrentThread());
[email protected]3e44697f2009-05-22 14:37:39240 DCHECK(!client_);
[email protected]90499482013-06-01 00:39:50241 DCHECK(!task_runner_.get());
sergeyu3f923062014-09-05 01:39:40242 task_runner_ = glib_task_runner;
[email protected]3e44697f2009-05-22 14:37:39243 client_ = gconf_client_get_default();
[email protected]861c6c62009-04-20 16:50:56244 if (!client_) {
[email protected]861c6c62009-04-20 16:50:56245 // It's not clear whether/when this can return NULL.
[email protected]3e44697f2009-05-22 14:37:39246 LOG(ERROR) << "Unable to create a gconf client";
[email protected]76722472012-05-24 08:26:46247 task_runner_ = NULL;
[email protected]3e44697f2009-05-22 14:37:39248 return false;
[email protected]861c6c62009-04-20 16:50:56249 }
[email protected]3e44697f2009-05-22 14:37:39250 GError* error = NULL;
[email protected]b160df32012-02-06 20:39:41251 bool added_system_proxy = false;
[email protected]3e44697f2009-05-22 14:37:39252 // We need to add the directories for which we'll be asking
[email protected]b160df32012-02-06 20:39:41253 // for notifications, and we might as well ask to preload them.
254 // These need to be removed again in ShutDown(); we are careful
255 // here to only leave client_ non-NULL if both have been added.
[email protected]3e44697f2009-05-22 14:37:39256 gconf_client_add_dir(client_, "/system/proxy",
257 GCONF_CLIENT_PRELOAD_ONELEVEL, &error);
258 if (error == NULL) {
[email protected]b160df32012-02-06 20:39:41259 added_system_proxy = true;
[email protected]3e44697f2009-05-22 14:37:39260 gconf_client_add_dir(client_, "/system/http_proxy",
261 GCONF_CLIENT_PRELOAD_ONELEVEL, &error);
262 }
263 if (error != NULL) {
264 LOG(ERROR) << "Error requesting gconf directory: " << error->message;
265 g_error_free(error);
[email protected]b160df32012-02-06 20:39:41266 if (added_system_proxy)
267 gconf_client_remove_dir(client_, "/system/proxy", NULL);
268 g_object_unref(client_);
269 client_ = NULL;
[email protected]76722472012-05-24 08:26:46270 task_runner_ = NULL;
[email protected]3e44697f2009-05-22 14:37:39271 return false;
272 }
273 return true;
274 }
275
mostynbba063d6032014-10-09 11:01:13276 virtual void ShutDown() override {
[email protected]3e44697f2009-05-22 14:37:39277 if (client_) {
[email protected]76722472012-05-24 08:26:46278 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]b160df32012-02-06 20:39:41279 // We must explicitly disable gconf notifications here, because the gconf
280 // client will be shared between all setting getters, and they do not all
281 // have the same lifetimes. (For instance, incognito sessions get their
282 // own, which is destroyed when the session ends.)
283 gconf_client_notify_remove(client_, system_http_proxy_id_);
284 gconf_client_notify_remove(client_, system_proxy_id_);
285 gconf_client_remove_dir(client_, "/system/http_proxy", NULL);
286 gconf_client_remove_dir(client_, "/system/proxy", NULL);
[email protected]3e44697f2009-05-22 14:37:39287 g_object_unref(client_);
288 client_ = NULL;
[email protected]76722472012-05-24 08:26:46289 task_runner_ = NULL;
[email protected]3e44697f2009-05-22 14:37:39290 }
291 }
292
[email protected]749bf5c2012-09-17 03:15:21293 virtual bool SetUpNotifications(
mostynbba063d6032014-10-09 11:01:13294 ProxyConfigServiceLinux::Delegate* delegate) override {
[email protected]3e44697f2009-05-22 14:37:39295 DCHECK(client_);
[email protected]76722472012-05-24 08:26:46296 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]3e44697f2009-05-22 14:37:39297 GError* error = NULL;
[email protected]d7395e732009-08-28 23:13:43298 notify_delegate_ = delegate;
[email protected]b160df32012-02-06 20:39:41299 // We have to keep track of the IDs returned by gconf_client_notify_add() so
300 // that we can remove them in ShutDown(). (Otherwise, notifications will be
301 // delivered to this object after it is deleted, which is bad, m'kay?)
302 system_proxy_id_ = gconf_client_notify_add(
[email protected]3e44697f2009-05-22 14:37:39303 client_, "/system/proxy",
[email protected]d7395e732009-08-28 23:13:43304 OnGConfChangeNotification, this,
[email protected]3e44697f2009-05-22 14:37:39305 NULL, &error);
306 if (error == NULL) {
[email protected]b160df32012-02-06 20:39:41307 system_http_proxy_id_ = gconf_client_notify_add(
[email protected]3e44697f2009-05-22 14:37:39308 client_, "/system/http_proxy",
[email protected]d7395e732009-08-28 23:13:43309 OnGConfChangeNotification, this,
[email protected]3e44697f2009-05-22 14:37:39310 NULL, &error);
311 }
312 if (error != NULL) {
313 LOG(ERROR) << "Error requesting gconf notifications: " << error->message;
314 g_error_free(error);
[email protected]d3066142011-05-10 02:36:20315 ShutDown();
[email protected]3e44697f2009-05-22 14:37:39316 return false;
317 }
[email protected]d3066142011-05-10 02:36:20318 // Simulate a change to avoid possibly losing updates before this point.
319 OnChangeNotification();
[email protected]3e44697f2009-05-22 14:37:39320 return true;
[email protected]861c6c62009-04-20 16:50:56321 }
322
sergeyu3f923062014-09-05 01:39:40323 virtual const scoped_refptr<base::SingleThreadTaskRunner>&
mostynbba063d6032014-10-09 11:01:13324 GetNotificationTaskRunner() override {
sergeyu3f923062014-09-05 01:39:40325 return task_runner_;
[email protected]d7395e732009-08-28 23:13:43326 }
327
mostynbba063d6032014-10-09 11:01:13328 virtual ProxyConfigSource GetConfigSource() override {
[email protected]db8ff912012-06-12 23:32:51329 return PROXY_CONFIG_SOURCE_GCONF;
[email protected]d7395e732009-08-28 23:13:43330 }
331
mostynbba063d6032014-10-09 11:01:13332 virtual bool GetString(StringSetting key, std::string* result) override {
[email protected]573c0502011-05-17 22:19:50333 switch (key) {
334 case PROXY_MODE:
335 return GetStringByPath("/system/proxy/mode", result);
336 case PROXY_AUTOCONF_URL:
337 return GetStringByPath("/system/proxy/autoconfig_url", result);
338 case PROXY_HTTP_HOST:
339 return GetStringByPath("/system/http_proxy/host", result);
340 case PROXY_HTTPS_HOST:
341 return GetStringByPath("/system/proxy/secure_host", result);
342 case PROXY_FTP_HOST:
343 return GetStringByPath("/system/proxy/ftp_host", result);
344 case PROXY_SOCKS_HOST:
345 return GetStringByPath("/system/proxy/socks_host", result);
[email protected]573c0502011-05-17 22:19:50346 }
[email protected]6b5fe742011-05-20 21:46:48347 return false; // Placate compiler.
[email protected]573c0502011-05-17 22:19:50348 }
mostynbba063d6032014-10-09 11:01:13349 virtual bool GetBool(BoolSetting key, bool* result) override {
[email protected]573c0502011-05-17 22:19:50350 switch (key) {
351 case PROXY_USE_HTTP_PROXY:
352 return GetBoolByPath("/system/http_proxy/use_http_proxy", result);
353 case PROXY_USE_SAME_PROXY:
354 return GetBoolByPath("/system/http_proxy/use_same_proxy", result);
355 case PROXY_USE_AUTHENTICATION:
356 return GetBoolByPath("/system/http_proxy/use_authentication", result);
[email protected]573c0502011-05-17 22:19:50357 }
[email protected]6b5fe742011-05-20 21:46:48358 return false; // Placate compiler.
[email protected]573c0502011-05-17 22:19:50359 }
mostynbba063d6032014-10-09 11:01:13360 virtual bool GetInt(IntSetting key, int* result) override {
[email protected]573c0502011-05-17 22:19:50361 switch (key) {
362 case PROXY_HTTP_PORT:
363 return GetIntByPath("/system/http_proxy/port", result);
364 case PROXY_HTTPS_PORT:
365 return GetIntByPath("/system/proxy/secure_port", result);
366 case PROXY_FTP_PORT:
367 return GetIntByPath("/system/proxy/ftp_port", result);
368 case PROXY_SOCKS_PORT:
369 return GetIntByPath("/system/proxy/socks_port", result);
[email protected]573c0502011-05-17 22:19:50370 }
[email protected]6b5fe742011-05-20 21:46:48371 return false; // Placate compiler.
[email protected]573c0502011-05-17 22:19:50372 }
[email protected]6b5fe742011-05-20 21:46:48373 virtual bool GetStringList(StringListSetting key,
mostynbba063d6032014-10-09 11:01:13374 std::vector<std::string>* result) override {
[email protected]573c0502011-05-17 22:19:50375 switch (key) {
376 case PROXY_IGNORE_HOSTS:
377 return GetStringListByPath("/system/http_proxy/ignore_hosts", result);
[email protected]573c0502011-05-17 22:19:50378 }
[email protected]6b5fe742011-05-20 21:46:48379 return false; // Placate compiler.
[email protected]573c0502011-05-17 22:19:50380 }
381
mostynbba063d6032014-10-09 11:01:13382 virtual bool BypassListIsReversed() override {
[email protected]573c0502011-05-17 22:19:50383 // This is a KDE-specific setting.
384 return false;
385 }
386
mostynbba063d6032014-10-09 11:01:13387 virtual bool MatchHostsUsingSuffixMatching() override {
[email protected]573c0502011-05-17 22:19:50388 return false;
389 }
390
391 private:
392 bool GetStringByPath(const char* key, std::string* result) {
[email protected]3e44697f2009-05-22 14:37:39393 DCHECK(client_);
[email protected]76722472012-05-24 08:26:46394 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]861c6c62009-04-20 16:50:56395 GError* error = NULL;
396 gchar* value = gconf_client_get_string(client_, key, &error);
397 if (HandleGError(error, key))
398 return false;
399 if (!value)
400 return false;
401 *result = value;
402 g_free(value);
403 return true;
404 }
[email protected]573c0502011-05-17 22:19:50405 bool GetBoolByPath(const char* key, bool* result) {
[email protected]3e44697f2009-05-22 14:37:39406 DCHECK(client_);
[email protected]76722472012-05-24 08:26:46407 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]861c6c62009-04-20 16:50:56408 GError* error = NULL;
409 // We want to distinguish unset values from values defaulting to
410 // false. For that we need to use the type-generic
411 // gconf_client_get() rather than gconf_client_get_bool().
412 GConfValue* gconf_value = gconf_client_get(client_, key, &error);
413 if (HandleGError(error, key))
414 return false;
415 if (!gconf_value) {
416 // Unset.
417 return false;
418 }
419 if (gconf_value->type != GCONF_VALUE_BOOL) {
420 gconf_value_free(gconf_value);
421 return false;
422 }
423 gboolean bool_value = gconf_value_get_bool(gconf_value);
424 *result = static_cast<bool>(bool_value);
425 gconf_value_free(gconf_value);
426 return true;
427 }
[email protected]573c0502011-05-17 22:19:50428 bool GetIntByPath(const char* key, int* result) {
[email protected]3e44697f2009-05-22 14:37:39429 DCHECK(client_);
[email protected]76722472012-05-24 08:26:46430 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]861c6c62009-04-20 16:50:56431 GError* error = NULL;
432 int value = gconf_client_get_int(client_, key, &error);
433 if (HandleGError(error, key))
434 return false;
435 // We don't bother to distinguish an unset value because callers
436 // don't care. 0 is returned if unset.
437 *result = value;
438 return true;
439 }
[email protected]573c0502011-05-17 22:19:50440 bool GetStringListByPath(const char* key, std::vector<std::string>* result) {
[email protected]3e44697f2009-05-22 14:37:39441 DCHECK(client_);
[email protected]76722472012-05-24 08:26:46442 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]861c6c62009-04-20 16:50:56443 GError* error = NULL;
444 GSList* list = gconf_client_get_list(client_, key,
445 GCONF_VALUE_STRING, &error);
446 if (HandleGError(error, key))
447 return false;
[email protected]8c20e3d2011-05-19 21:03:57448 if (!list)
[email protected]861c6c62009-04-20 16:50:56449 return false;
[email protected]861c6c62009-04-20 16:50:56450 for (GSList *it = list; it; it = it->next) {
451 result->push_back(static_cast<char*>(it->data));
452 g_free(it->data);
453 }
454 g_slist_free(list);
455 return true;
456 }
457
[email protected]861c6c62009-04-20 16:50:56458 // Logs and frees a glib error. Returns false if there was no error
459 // (error is NULL).
460 bool HandleGError(GError* error, const char* key) {
461 if (error != NULL) {
[email protected]3e44697f2009-05-22 14:37:39462 LOG(ERROR) << "Error getting gconf value for " << key
463 << ": " << error->message;
[email protected]861c6c62009-04-20 16:50:56464 g_error_free(error);
465 return true;
466 }
467 return false;
468 }
469
[email protected]d7395e732009-08-28 23:13:43470 // This is the callback from the debounce timer.
471 void OnDebouncedNotification() {
[email protected]76722472012-05-24 08:26:46472 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]961ac942011-04-28 18:18:14473 CHECK(notify_delegate_);
[email protected]d7395e732009-08-28 23:13:43474 // Forward to a method on the proxy config service delegate object.
475 notify_delegate_->OnCheckProxyConfigSettings();
476 }
477
478 void OnChangeNotification() {
479 // We don't use Reset() because the timer may not yet be running.
480 // (In that case Stop() is a no-op.)
481 debounce_timer_.Stop();
[email protected]d323a172011-09-02 18:23:02482 debounce_timer_.Start(FROM_HERE,
[email protected]8c20e3d2011-05-19 21:03:57483 base::TimeDelta::FromMilliseconds(kDebounceTimeoutMilliseconds),
484 this, &SettingGetterImplGConf::OnDebouncedNotification);
[email protected]d7395e732009-08-28 23:13:43485 }
486
[email protected]8c20e3d2011-05-19 21:03:57487 // gconf notification callback, dispatched on the default glib main loop.
488 static void OnGConfChangeNotification(GConfClient* client, guint cnxn_id,
489 GConfEntry* entry, gpointer user_data) {
[email protected]b30a3f52010-10-16 01:05:46490 VLOG(1) << "gconf change notification for key "
491 << gconf_entry_get_key(entry);
[email protected]d7395e732009-08-28 23:13:43492 // We don't track which key has changed, just that something did change.
[email protected]573c0502011-05-17 22:19:50493 SettingGetterImplGConf* setting_getter =
494 reinterpret_cast<SettingGetterImplGConf*>(user_data);
[email protected]d7395e732009-08-28 23:13:43495 setting_getter->OnChangeNotification();
496 }
497
[email protected]861c6c62009-04-20 16:50:56498 GConfClient* client_;
[email protected]b160df32012-02-06 20:39:41499 // These ids are the values returned from gconf_client_notify_add(), which we
500 // will need in order to later call gconf_client_notify_remove().
501 guint system_proxy_id_;
502 guint system_http_proxy_id_;
503
[email protected]d7395e732009-08-28 23:13:43504 ProxyConfigServiceLinux::Delegate* notify_delegate_;
[email protected]573c0502011-05-17 22:19:50505 base::OneShotTimer<SettingGetterImplGConf> debounce_timer_;
[email protected]861c6c62009-04-20 16:50:56506
[email protected]76722472012-05-24 08:26:46507 // Task runner for the thread that we make gconf calls on. It should
[email protected]3e44697f2009-05-22 14:37:39508 // be the UI thread and all our methods should be called on this
509 // thread. Only for assertions.
[email protected]76722472012-05-24 08:26:46510 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
[email protected]3e44697f2009-05-22 14:37:39511
[email protected]573c0502011-05-17 22:19:50512 DISALLOW_COPY_AND_ASSIGN(SettingGetterImplGConf);
[email protected]d7395e732009-08-28 23:13:43513};
[email protected]6de53d42010-11-09 07:33:19514#endif // defined(USE_GCONF)
[email protected]d7395e732009-08-28 23:13:43515
[email protected]8c20e3d2011-05-19 21:03:57516#if defined(USE_GIO)
[email protected]2297bb22014-06-19 06:30:14517const char kProxyGConfSchema[] = "org.gnome.system.proxy";
518
[email protected]8c20e3d2011-05-19 21:03:57519// This setting getter uses gsettings, as used in most GNOME 3 desktops.
520class SettingGetterImplGSettings
521 : public ProxyConfigServiceLinux::SettingGetter {
522 public:
[email protected]0e14e87d2011-06-21 21:24:19523 SettingGetterImplGSettings() :
[email protected]0e14e87d2011-06-21 21:24:19524 client_(NULL),
525 http_client_(NULL),
526 https_client_(NULL),
527 ftp_client_(NULL),
528 socks_client_(NULL),
[email protected]76722472012-05-24 08:26:46529 notify_delegate_(NULL) {
[email protected]8c20e3d2011-05-19 21:03:57530 }
531
532 virtual ~SettingGetterImplGSettings() {
533 // client_ should have been released before now, from
534 // Delegate::OnDestroy(), while running on the UI thread. However
535 // on exiting the process, it may happen that
536 // Delegate::OnDestroy() task is left pending on the glib loop
537 // after the loop was quit, and pending tasks may then be deleted
538 // without being run.
539 if (client_) {
540 // gconf client was not cleaned up.
[email protected]76722472012-05-24 08:26:46541 if (task_runner_->BelongsToCurrentThread()) {
[email protected]8c20e3d2011-05-19 21:03:57542 // We are on the UI thread so we can clean it safely. This is
543 // the case at least for ui_tests running under Valgrind in
544 // bug 16076.
545 VLOG(1) << "~SettingGetterImplGSettings: releasing gsettings client";
546 ShutDown();
547 } else {
548 LOG(WARNING) << "~SettingGetterImplGSettings: leaking gsettings client";
549 client_ = NULL;
550 }
551 }
552 DCHECK(!client_);
[email protected]8c20e3d2011-05-19 21:03:57553 }
554
[email protected]4cf80f0b2011-05-20 20:30:26555 bool SchemaExists(const char* schema_name) {
[email protected]3fc24f52012-11-30 21:22:34556 const gchar* const* schemas = libgio_loader_.g_settings_list_schemas();
[email protected]4cf80f0b2011-05-20 20:30:26557 while (*schemas) {
[email protected]a099f3ae2011-08-16 21:06:58558 if (strcmp(schema_name, static_cast<const char*>(*schemas)) == 0)
[email protected]4cf80f0b2011-05-20 20:30:26559 return true;
560 schemas++;
561 }
562 return false;
563 }
564
[email protected]8c20e3d2011-05-19 21:03:57565 // LoadAndCheckVersion() must be called *before* Init()!
566 bool LoadAndCheckVersion(base::Environment* env);
567
sergeyu3f923062014-09-05 01:39:40568 virtual bool Init(
569 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
mostynbba063d6032014-10-09 11:01:13592 virtual 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 }
605 }
606
[email protected]749bf5c2012-09-17 03:15:21607 virtual bool SetUpNotifications(
mostynbba063d6032014-10-09 11:01:13608 ProxyConfigServiceLinux::Delegate* delegate) override {
[email protected]8c20e3d2011-05-19 21:03:57609 DCHECK(client_);
[email protected]76722472012-05-24 08:26:46610 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]8c20e3d2011-05-19 21:03:57611 notify_delegate_ = delegate;
612 // We could watch for the change-event signal instead of changed, but
613 // since we have to watch more than one object, we'd still have to
614 // debounce change notifications. This is conceptually simpler.
615 g_signal_connect(G_OBJECT(client_), "changed",
616 G_CALLBACK(OnGSettingsChangeNotification), this);
617 g_signal_connect(G_OBJECT(http_client_), "changed",
618 G_CALLBACK(OnGSettingsChangeNotification), this);
619 g_signal_connect(G_OBJECT(https_client_), "changed",
620 G_CALLBACK(OnGSettingsChangeNotification), this);
621 g_signal_connect(G_OBJECT(ftp_client_), "changed",
622 G_CALLBACK(OnGSettingsChangeNotification), this);
623 g_signal_connect(G_OBJECT(socks_client_), "changed",
624 G_CALLBACK(OnGSettingsChangeNotification), this);
625 // Simulate a change to avoid possibly losing updates before this point.
626 OnChangeNotification();
627 return true;
628 }
629
sergeyu3f923062014-09-05 01:39:40630 virtual const scoped_refptr<base::SingleThreadTaskRunner>&
mostynbba063d6032014-10-09 11:01:13631 GetNotificationTaskRunner() override {
sergeyu3f923062014-09-05 01:39:40632 return task_runner_;
[email protected]8c20e3d2011-05-19 21:03:57633 }
634
mostynbba063d6032014-10-09 11:01:13635 virtual ProxyConfigSource GetConfigSource() override {
[email protected]db8ff912012-06-12 23:32:51636 return PROXY_CONFIG_SOURCE_GSETTINGS;
[email protected]8c20e3d2011-05-19 21:03:57637 }
638
mostynbba063d6032014-10-09 11:01:13639 virtual bool GetString(StringSetting key, std::string* result) override {
[email protected]8c20e3d2011-05-19 21:03:57640 DCHECK(client_);
641 switch (key) {
642 case PROXY_MODE:
643 return GetStringByPath(client_, "mode", result);
644 case PROXY_AUTOCONF_URL:
645 return GetStringByPath(client_, "autoconfig-url", result);
646 case PROXY_HTTP_HOST:
647 return GetStringByPath(http_client_, "host", result);
648 case PROXY_HTTPS_HOST:
649 return GetStringByPath(https_client_, "host", result);
650 case PROXY_FTP_HOST:
651 return GetStringByPath(ftp_client_, "host", result);
652 case PROXY_SOCKS_HOST:
653 return GetStringByPath(socks_client_, "host", result);
[email protected]8c20e3d2011-05-19 21:03:57654 }
[email protected]6b5fe742011-05-20 21:46:48655 return false; // Placate compiler.
[email protected]8c20e3d2011-05-19 21:03:57656 }
mostynbba063d6032014-10-09 11:01:13657 virtual bool GetBool(BoolSetting key, bool* result) override {
[email protected]8c20e3d2011-05-19 21:03:57658 DCHECK(client_);
659 switch (key) {
660 case PROXY_USE_HTTP_PROXY:
661 // Although there is an "enabled" boolean in http_client_, it is not set
662 // to true by the proxy config utility. We ignore it and return false.
663 return false;
664 case PROXY_USE_SAME_PROXY:
665 // Similarly, although there is a "use-same-proxy" boolean in client_,
666 // it is never set to false by the proxy config utility. We ignore it.
667 return false;
668 case PROXY_USE_AUTHENTICATION:
669 // There is also no way to set this in the proxy config utility, but it
670 // doesn't hurt us to get the actual setting (unlike the two above).
671 return GetBoolByPath(http_client_, "use-authentication", result);
[email protected]8c20e3d2011-05-19 21:03:57672 }
[email protected]6b5fe742011-05-20 21:46:48673 return false; // Placate compiler.
[email protected]8c20e3d2011-05-19 21:03:57674 }
mostynbba063d6032014-10-09 11:01:13675 virtual bool GetInt(IntSetting key, int* result) override {
[email protected]8c20e3d2011-05-19 21:03:57676 DCHECK(client_);
677 switch (key) {
678 case PROXY_HTTP_PORT:
679 return GetIntByPath(http_client_, "port", result);
680 case PROXY_HTTPS_PORT:
681 return GetIntByPath(https_client_, "port", result);
682 case PROXY_FTP_PORT:
683 return GetIntByPath(ftp_client_, "port", result);
684 case PROXY_SOCKS_PORT:
685 return GetIntByPath(socks_client_, "port", result);
[email protected]8c20e3d2011-05-19 21:03:57686 }
[email protected]6b5fe742011-05-20 21:46:48687 return false; // Placate compiler.
[email protected]8c20e3d2011-05-19 21:03:57688 }
[email protected]6b5fe742011-05-20 21:46:48689 virtual bool GetStringList(StringListSetting key,
mostynbba063d6032014-10-09 11:01:13690 std::vector<std::string>* result) override {
[email protected]8c20e3d2011-05-19 21:03:57691 DCHECK(client_);
692 switch (key) {
693 case PROXY_IGNORE_HOSTS:
694 return GetStringListByPath(client_, "ignore-hosts", result);
[email protected]8c20e3d2011-05-19 21:03:57695 }
[email protected]6b5fe742011-05-20 21:46:48696 return false; // Placate compiler.
[email protected]8c20e3d2011-05-19 21:03:57697 }
698
mostynbba063d6032014-10-09 11:01:13699 virtual bool BypassListIsReversed() override {
[email protected]8c20e3d2011-05-19 21:03:57700 // This is a KDE-specific setting.
701 return false;
702 }
703
mostynbba063d6032014-10-09 11:01:13704 virtual bool MatchHostsUsingSuffixMatching() override {
[email protected]8c20e3d2011-05-19 21:03:57705 return false;
706 }
707
708 private:
[email protected]8c20e3d2011-05-19 21:03:57709 bool GetStringByPath(GSettings* client, const char* key,
710 std::string* result) {
[email protected]76722472012-05-24 08:26:46711 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]3fc24f52012-11-30 21:22:34712 gchar* value = libgio_loader_.g_settings_get_string(client, key);
[email protected]8c20e3d2011-05-19 21:03:57713 if (!value)
714 return false;
715 *result = value;
716 g_free(value);
717 return true;
718 }
719 bool GetBoolByPath(GSettings* client, const char* key, bool* result) {
[email protected]76722472012-05-24 08:26:46720 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]3fc24f52012-11-30 21:22:34721 *result = static_cast<bool>(
722 libgio_loader_.g_settings_get_boolean(client, key));
[email protected]8c20e3d2011-05-19 21:03:57723 return true;
724 }
725 bool GetIntByPath(GSettings* client, const char* key, int* result) {
[email protected]76722472012-05-24 08:26:46726 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]3fc24f52012-11-30 21:22:34727 *result = libgio_loader_.g_settings_get_int(client, key);
[email protected]8c20e3d2011-05-19 21:03:57728 return true;
729 }
730 bool GetStringListByPath(GSettings* client, const char* key,
731 std::vector<std::string>* result) {
[email protected]76722472012-05-24 08:26:46732 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]3fc24f52012-11-30 21:22:34733 gchar** list = libgio_loader_.g_settings_get_strv(client, key);
[email protected]8c20e3d2011-05-19 21:03:57734 if (!list)
735 return false;
736 for (size_t i = 0; list[i]; ++i) {
737 result->push_back(static_cast<char*>(list[i]));
738 g_free(list[i]);
739 }
740 g_free(list);
741 return true;
742 }
743
744 // This is the callback from the debounce timer.
745 void OnDebouncedNotification() {
[email protected]76722472012-05-24 08:26:46746 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]8c20e3d2011-05-19 21:03:57747 CHECK(notify_delegate_);
748 // Forward to a method on the proxy config service delegate object.
749 notify_delegate_->OnCheckProxyConfigSettings();
750 }
751
752 void OnChangeNotification() {
753 // We don't use Reset() because the timer may not yet be running.
754 // (In that case Stop() is a no-op.)
755 debounce_timer_.Stop();
[email protected]d323a172011-09-02 18:23:02756 debounce_timer_.Start(FROM_HERE,
[email protected]8c20e3d2011-05-19 21:03:57757 base::TimeDelta::FromMilliseconds(kDebounceTimeoutMilliseconds),
758 this, &SettingGetterImplGSettings::OnDebouncedNotification);
759 }
760
761 // gsettings notification callback, dispatched on the default glib main loop.
762 static void OnGSettingsChangeNotification(GSettings* client, gchar* key,
763 gpointer user_data) {
764 VLOG(1) << "gsettings change notification for key " << key;
765 // We don't track which key has changed, just that something did change.
766 SettingGetterImplGSettings* setting_getter =
767 reinterpret_cast<SettingGetterImplGSettings*>(user_data);
768 setting_getter->OnChangeNotification();
769 }
770
771 GSettings* client_;
772 GSettings* http_client_;
773 GSettings* https_client_;
774 GSettings* ftp_client_;
775 GSettings* socks_client_;
776 ProxyConfigServiceLinux::Delegate* notify_delegate_;
777 base::OneShotTimer<SettingGetterImplGSettings> debounce_timer_;
778
[email protected]76722472012-05-24 08:26:46779 // Task runner for the thread that we make gsettings calls on. It should
[email protected]8c20e3d2011-05-19 21:03:57780 // be the UI thread and all our methods should be called on this
781 // thread. Only for assertions.
[email protected]76722472012-05-24 08:26:46782 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
[email protected]8c20e3d2011-05-19 21:03:57783
[email protected]3fc24f52012-11-30 21:22:34784 LibGioLoader libgio_loader_;
785
[email protected]8c20e3d2011-05-19 21:03:57786 DISALLOW_COPY_AND_ASSIGN(SettingGetterImplGSettings);
787};
788
789bool SettingGetterImplGSettings::LoadAndCheckVersion(
790 base::Environment* env) {
791 // LoadAndCheckVersion() must be called *before* Init()!
792 DCHECK(!client_);
793
794 // The APIs to query gsettings were introduced after the minimum glib
795 // version we target, so we can't link directly against them. We load them
796 // dynamically at runtime, and if they don't exist, return false here. (We
797 // support linking directly via gyp flags though.) Additionally, even when
798 // they are present, we do two additional checks to make sure we should use
799 // them and not gconf. First, we attempt to load the schema for proxy
800 // settings. Second, we check for the program that was used in older
801 // versions of GNOME to configure proxy settings, and return false if it
802 // exists. Some distributions (e.g. Ubuntu 11.04) have the API and schema
803 // but don't use gsettings for proxy settings, but they do have the old
804 // binary, so we detect these systems that way.
805
[email protected]ae82cea2012-12-06 22:52:10806 {
807 // TODO(phajdan.jr): Redesign the code to load library on different thread.
808 base::ThreadRestrictions::ScopedAllowIO allow_io;
809
810 // Try also without .0 at the end; on some systems this may be required.
811 if (!libgio_loader_.Load("libgio-2.0.so.0") &&
812 !libgio_loader_.Load("libgio-2.0.so")) {
813 VLOG(1) << "Cannot load gio library. Will fall back to gconf.";
814 return false;
815 }
[email protected]8c20e3d2011-05-19 21:03:57816 }
[email protected]8c20e3d2011-05-19 21:03:57817
[email protected]4bbb72d2014-06-06 18:05:51818 GSettings* client = NULL;
819 if (SchemaExists(kProxyGConfSchema)) {
820 ANNOTATE_SCOPED_MEMORY_LEAK; // http://crbug.com/380782
821 client = libgio_loader_.g_settings_new(kProxyGConfSchema);
822 }
823 if (!client) {
[email protected]8c20e3d2011-05-19 21:03:57824 VLOG(1) << "Cannot create gsettings client. Will fall back to gconf.";
825 return false;
826 }
827 g_object_unref(client);
828
829 std::string path;
830 if (!env->GetVar("PATH", &path)) {
831 LOG(ERROR) << "No $PATH variable. Assuming no gnome-network-properties.";
832 } else {
833 // Yes, we're on the UI thread. Yes, we're accessing the file system.
834 // Sadly, we don't have much choice. We need the proxy settings and we
835 // need them now, and to figure out where to get them, we have to check
836 // for this binary. See http://crbug.com/69057 for additional details.
837 base::ThreadRestrictions::ScopedAllowIO allow_io;
838 std::vector<std::string> paths;
839 Tokenize(path, ":", &paths);
840 for (size_t i = 0; i < paths.size(); ++i) {
[email protected]6cdfd7f2013-02-08 20:40:15841 base::FilePath file(paths[i]);
[email protected]7567484142013-07-11 17:36:07842 if (base::PathExists(file.Append("gnome-network-properties"))) {
[email protected]8c20e3d2011-05-19 21:03:57843 VLOG(1) << "Found gnome-network-properties. Will fall back to gconf.";
844 return false;
845 }
846 }
847 }
848
849 VLOG(1) << "All gsettings tests OK. Will get proxy config from gsettings.";
850 return true;
851}
852#endif // defined(USE_GIO)
853
[email protected]d7395e732009-08-28 23:13:43854// This is the KDE version that reads kioslaverc and simulates gconf.
855// Doing this allows the main Delegate code, as well as the unit tests
856// for it, to stay the same - and the settings map fairly well besides.
[email protected]573c0502011-05-17 22:19:50857class SettingGetterImplKDE : public ProxyConfigServiceLinux::SettingGetter,
858 public base::MessagePumpLibevent::Watcher {
[email protected]d7395e732009-08-28 23:13:43859 public:
[email protected]573c0502011-05-17 22:19:50860 explicit SettingGetterImplKDE(base::Environment* env_var_getter)
[email protected]d7395e732009-08-28 23:13:43861 : inotify_fd_(-1), notify_delegate_(NULL), indirect_manual_(false),
[email protected]a48bf4a2010-06-14 18:24:53862 auto_no_pac_(false), reversed_bypass_list_(false),
sergeyu3f923062014-09-05 01:39:40863 env_var_getter_(env_var_getter), file_task_runner_(NULL) {
[email protected]9a8c4022011-01-25 14:25:33864 // This has to be called on the UI thread (http://crbug.com/69057).
865 base::ThreadRestrictions::ScopedAllowIO allow_io;
866
[email protected]f18fde22010-05-18 23:49:54867 // Derive the location of the kde config dir from the environment.
[email protected]92d2dc82010-04-08 17:49:59868 std::string home;
[email protected]3ba7e082010-08-07 02:57:59869 if (env_var_getter->GetVar("KDEHOME", &home) && !home.empty()) {
[email protected]2e8cfe22010-06-12 00:26:24870 // $KDEHOME is set. Use it unconditionally.
[email protected]6cdfd7f2013-02-08 20:40:15871 kde_config_dir_ = KDEHomeToConfigPath(base::FilePath(home));
[email protected]92d2dc82010-04-08 17:49:59872 } else {
[email protected]2e8cfe22010-06-12 00:26:24873 // $KDEHOME is unset. Try to figure out what to use. This seems to be
[email protected]92d2dc82010-04-08 17:49:59874 // the common case on most distributions.
[email protected]3ba7e082010-08-07 02:57:59875 if (!env_var_getter->GetVar(base::env_vars::kHome, &home))
[email protected]d7395e732009-08-28 23:13:43876 // User has no $HOME? Give up. Later we'll report the failure.
877 return;
[email protected]6b0349ef2010-10-16 04:56:06878 if (base::nix::GetDesktopEnvironment(env_var_getter) ==
879 base::nix::DESKTOP_ENVIRONMENT_KDE3) {
[email protected]92d2dc82010-04-08 17:49:59880 // KDE3 always uses .kde for its configuration.
[email protected]6cdfd7f2013-02-08 20:40:15881 base::FilePath kde_path = base::FilePath(home).Append(".kde");
[email protected]92d2dc82010-04-08 17:49:59882 kde_config_dir_ = KDEHomeToConfigPath(kde_path);
883 } else {
884 // Some distributions patch KDE4 to use .kde4 instead of .kde, so that
[email protected]fad9c8a52010-06-10 22:30:53885 // both can be installed side-by-side. Sadly they don't all do this, and
886 // they don't always do this: some distributions have started switching
887 // back as well. So if there is a .kde4 directory, check the timestamps
888 // of the config directories within and use the newest one.
[email protected]92d2dc82010-04-08 17:49:59889 // Note that we should currently be running in the UI thread, because in
890 // the gconf version, that is the only thread that can access the proxy
891 // settings (a gconf restriction). As noted below, the initial read of
892 // the proxy settings will be done in this thread anyway, so we check
893 // for .kde4 here in this thread as well.
[email protected]6cdfd7f2013-02-08 20:40:15894 base::FilePath kde3_path = base::FilePath(home).Append(".kde");
895 base::FilePath kde3_config = KDEHomeToConfigPath(kde3_path);
896 base::FilePath kde4_path = base::FilePath(home).Append(".kde4");
897 base::FilePath kde4_config = KDEHomeToConfigPath(kde4_path);
[email protected]fad9c8a52010-06-10 22:30:53898 bool use_kde4 = false;
[email protected]dcd16612013-07-15 20:18:09899 if (base::DirectoryExists(kde4_path)) {
[email protected]54124ed02014-01-07 10:06:58900 base::File::Info kde3_info;
901 base::File::Info kde4_info;
[email protected]9eae4e62013-12-04 20:56:49902 if (base::GetFileInfo(kde4_config, &kde4_info)) {
903 if (base::GetFileInfo(kde3_config, &kde3_info)) {
[email protected]fad9c8a52010-06-10 22:30:53904 use_kde4 = kde4_info.last_modified >= kde3_info.last_modified;
905 } else {
906 use_kde4 = true;
907 }
908 }
909 }
910 if (use_kde4) {
[email protected]92d2dc82010-04-08 17:49:59911 kde_config_dir_ = KDEHomeToConfigPath(kde4_path);
912 } else {
[email protected]fad9c8a52010-06-10 22:30:53913 kde_config_dir_ = KDEHomeToConfigPath(kde3_path);
[email protected]92d2dc82010-04-08 17:49:59914 }
915 }
[email protected]d7395e732009-08-28 23:13:43916 }
[email protected]d7395e732009-08-28 23:13:43917 }
918
[email protected]573c0502011-05-17 22:19:50919 virtual ~SettingGetterImplKDE() {
[email protected]d7395e732009-08-28 23:13:43920 // inotify_fd_ should have been closed before now, from
921 // Delegate::OnDestroy(), while running on the file thread. However
922 // on exiting the process, it may happen that Delegate::OnDestroy()
923 // task is left pending on the file loop after the loop was quit,
924 // and pending tasks may then be deleted without being run.
925 // Here in the KDE version, we can safely close the file descriptor
926 // anyway. (Not that it really matters; the process is exiting.)
927 if (inotify_fd_ >= 0)
[email protected]d3066142011-05-10 02:36:20928 ShutDown();
[email protected]d7395e732009-08-28 23:13:43929 DCHECK(inotify_fd_ < 0);
930 }
931
sergeyu3f923062014-09-05 01:39:40932 virtual bool Init(
933 const scoped_refptr<base::SingleThreadTaskRunner>& glib_task_runner,
934 const scoped_refptr<base::SingleThreadTaskRunner>& file_task_runner)
mostynbba063d6032014-10-09 11:01:13935 override {
[email protected]9a8c4022011-01-25 14:25:33936 // This has to be called on the UI thread (http://crbug.com/69057).
937 base::ThreadRestrictions::ScopedAllowIO allow_io;
[email protected]d7395e732009-08-28 23:13:43938 DCHECK(inotify_fd_ < 0);
939 inotify_fd_ = inotify_init();
940 if (inotify_fd_ < 0) {
[email protected]57b765672009-10-13 18:27:40941 PLOG(ERROR) << "inotify_init failed";
[email protected]d7395e732009-08-28 23:13:43942 return false;
943 }
944 int flags = fcntl(inotify_fd_, F_GETFL);
945 if (fcntl(inotify_fd_, F_SETFL, flags | O_NONBLOCK) < 0) {
[email protected]57b765672009-10-13 18:27:40946 PLOG(ERROR) << "fcntl failed";
[email protected]d7395e732009-08-28 23:13:43947 close(inotify_fd_);
948 inotify_fd_ = -1;
949 return false;
950 }
sergeyu3f923062014-09-05 01:39:40951 file_task_runner_ = file_task_runner;
952 // The initial read is done on the current thread, not
953 // |file_task_runner_|, since we will need to have it for
954 // SetUpAndFetchInitialConfig().
[email protected]d7395e732009-08-28 23:13:43955 UpdateCachedSettings();
956 return true;
957 }
958
mostynbba063d6032014-10-09 11:01:13959 virtual void ShutDown() override {
[email protected]d7395e732009-08-28 23:13:43960 if (inotify_fd_ >= 0) {
961 ResetCachedSettings();
962 inotify_watcher_.StopWatchingFileDescriptor();
963 close(inotify_fd_);
964 inotify_fd_ = -1;
965 }
966 }
967
[email protected]749bf5c2012-09-17 03:15:21968 virtual bool SetUpNotifications(
mostynbba063d6032014-10-09 11:01:13969 ProxyConfigServiceLinux::Delegate* delegate) override {
[email protected]d7395e732009-08-28 23:13:43970 DCHECK(inotify_fd_ >= 0);
sergeyu3f923062014-09-05 01:39:40971 DCHECK(file_task_runner_->BelongsToCurrentThread());
[email protected]d7395e732009-08-28 23:13:43972 // We can't just watch the kioslaverc file directly, since KDE will write
973 // a new copy of it and then rename it whenever settings are changed and
974 // inotify watches inodes (so we'll be watching the old deleted file after
975 // the first change, and it will never change again). So, we watch the
976 // directory instead. We then act only on changes to the kioslaverc entry.
977 if (inotify_add_watch(inotify_fd_, kde_config_dir_.value().c_str(),
sergeyu3f923062014-09-05 01:39:40978 IN_MODIFY | IN_MOVED_TO) < 0) {
[email protected]d7395e732009-08-28 23:13:43979 return false;
sergeyu3f923062014-09-05 01:39:40980 }
[email protected]d7395e732009-08-28 23:13:43981 notify_delegate_ = delegate;
sergeyu3f923062014-09-05 01:39:40982 if (!base::MessageLoopForIO::current()->WatchFileDescriptor(
983 inotify_fd_, true, base::MessageLoopForIO::WATCH_READ,
984 &inotify_watcher_, this)) {
[email protected]d3066142011-05-10 02:36:20985 return false;
sergeyu3f923062014-09-05 01:39:40986 }
[email protected]d3066142011-05-10 02:36:20987 // Simulate a change to avoid possibly losing updates before this point.
988 OnChangeNotification();
989 return true;
[email protected]d7395e732009-08-28 23:13:43990 }
991
sergeyu3f923062014-09-05 01:39:40992 virtual const scoped_refptr<base::SingleThreadTaskRunner>&
mostynbba063d6032014-10-09 11:01:13993 GetNotificationTaskRunner() override {
sergeyu3f923062014-09-05 01:39:40994 return file_task_runner_;
[email protected]d7395e732009-08-28 23:13:43995 }
996
[email protected]b160df32012-02-06 20:39:41997 // Implement base::MessagePumpLibevent::Watcher.
mostynbba063d6032014-10-09 11:01:13998 virtual void OnFileCanReadWithoutBlocking(int fd) override {
[email protected]d2e6d592012-02-03 21:49:04999 DCHECK_EQ(fd, inotify_fd_);
sergeyu3f923062014-09-05 01:39:401000 DCHECK(file_task_runner_->BelongsToCurrentThread());
[email protected]d7395e732009-08-28 23:13:431001 OnChangeNotification();
1002 }
mostynbba063d6032014-10-09 11:01:131003 virtual void OnFileCanWriteWithoutBlocking(int fd) override {
[email protected]d7395e732009-08-28 23:13:431004 NOTREACHED();
1005 }
1006
mostynbba063d6032014-10-09 11:01:131007 virtual ProxyConfigSource GetConfigSource() override {
[email protected]db8ff912012-06-12 23:32:511008 return PROXY_CONFIG_SOURCE_KDE;
[email protected]d7395e732009-08-28 23:13:431009 }
1010
mostynbba063d6032014-10-09 11:01:131011 virtual bool GetString(StringSetting key, std::string* result) override {
[email protected]d7395e732009-08-28 23:13:431012 string_map_type::iterator it = string_table_.find(key);
1013 if (it == string_table_.end())
1014 return false;
1015 *result = it->second;
1016 return true;
1017 }
mostynbba063d6032014-10-09 11:01:131018 virtual bool GetBool(BoolSetting key, bool* result) override {
[email protected]d7395e732009-08-28 23:13:431019 // We don't ever have any booleans.
1020 return false;
1021 }
mostynbba063d6032014-10-09 11:01:131022 virtual bool GetInt(IntSetting key, int* result) override {
[email protected]d7395e732009-08-28 23:13:431023 // We don't ever have any integers. (See AddProxy() below about ports.)
1024 return false;
1025 }
[email protected]6b5fe742011-05-20 21:46:481026 virtual bool GetStringList(StringListSetting key,
mostynbba063d6032014-10-09 11:01:131027 std::vector<std::string>* result) override {
[email protected]d7395e732009-08-28 23:13:431028 strings_map_type::iterator it = strings_table_.find(key);
1029 if (it == strings_table_.end())
1030 return false;
1031 *result = it->second;
1032 return true;
1033 }
1034
mostynbba063d6032014-10-09 11:01:131035 virtual bool BypassListIsReversed() override {
[email protected]a48bf4a2010-06-14 18:24:531036 return reversed_bypass_list_;
1037 }
1038
mostynbba063d6032014-10-09 11:01:131039 virtual bool MatchHostsUsingSuffixMatching() override {
[email protected]1a597192010-07-09 16:58:381040 return true;
1041 }
1042
[email protected]d7395e732009-08-28 23:13:431043 private:
1044 void ResetCachedSettings() {
1045 string_table_.clear();
1046 strings_table_.clear();
1047 indirect_manual_ = false;
1048 auto_no_pac_ = false;
[email protected]a48bf4a2010-06-14 18:24:531049 reversed_bypass_list_ = false;
[email protected]d7395e732009-08-28 23:13:431050 }
1051
[email protected]6cdfd7f2013-02-08 20:40:151052 base::FilePath KDEHomeToConfigPath(const base::FilePath& kde_home) {
[email protected]92d2dc82010-04-08 17:49:591053 return kde_home.Append("share").Append("config");
1054 }
1055
[email protected]6b5fe742011-05-20 21:46:481056 void AddProxy(StringSetting host_key, const std::string& value) {
[email protected]d7395e732009-08-28 23:13:431057 if (value.empty() || value.substr(0, 3) == "//:")
1058 // No proxy.
1059 return;
[email protected]4b90c202012-04-24 23:27:551060 size_t space = value.find(' ');
1061 if (space != std::string::npos) {
1062 // Newer versions of KDE use a space rather than a colon to separate the
1063 // port number from the hostname. If we find this, we need to convert it.
1064 std::string fixed = value;
1065 fixed[space] = ':';
1066 string_table_[host_key] = fixed;
1067 } else {
1068 // We don't need to parse the port number out; GetProxyFromSettings()
1069 // would only append it right back again. So we just leave the port
1070 // number right in the host string.
1071 string_table_[host_key] = value;
1072 }
[email protected]d7395e732009-08-28 23:13:431073 }
1074
[email protected]6b5fe742011-05-20 21:46:481075 void AddHostList(StringListSetting key, const std::string& value) {
[email protected]f18fde22010-05-18 23:49:541076 std::vector<std::string> tokens;
[email protected]f4ebe772013-02-02 00:21:391077 base::StringTokenizer tk(value, ", ");
[email protected]f18fde22010-05-18 23:49:541078 while (tk.GetNext()) {
1079 std::string token = tk.token();
1080 if (!token.empty())
1081 tokens.push_back(token);
1082 }
1083 strings_table_[key] = tokens;
1084 }
1085
[email protected]9a3d8d42009-09-03 17:01:461086 void AddKDESetting(const std::string& key, const std::string& value) {
[email protected]d7395e732009-08-28 23:13:431087 if (key == "ProxyType") {
1088 const char* mode = "none";
1089 indirect_manual_ = false;
1090 auto_no_pac_ = false;
[email protected]e83326f2010-07-31 17:29:251091 int int_value;
1092 base::StringToInt(value, &int_value);
1093 switch (int_value) {
[email protected]d7395e732009-08-28 23:13:431094 case 0: // No proxy, or maybe kioslaverc syntax error.
1095 break;
1096 case 1: // Manual configuration.
1097 mode = "manual";
1098 break;
1099 case 2: // PAC URL.
1100 mode = "auto";
1101 break;
1102 case 3: // WPAD.
1103 mode = "auto";
1104 auto_no_pac_ = true;
1105 break;
1106 case 4: // Indirect manual via environment variables.
1107 mode = "manual";
1108 indirect_manual_ = true;
1109 break;
1110 }
[email protected]573c0502011-05-17 22:19:501111 string_table_[PROXY_MODE] = mode;
[email protected]d7395e732009-08-28 23:13:431112 } else if (key == "Proxy Config Script") {
[email protected]573c0502011-05-17 22:19:501113 string_table_[PROXY_AUTOCONF_URL] = value;
[email protected]d7395e732009-08-28 23:13:431114 } else if (key == "httpProxy") {
[email protected]573c0502011-05-17 22:19:501115 AddProxy(PROXY_HTTP_HOST, value);
[email protected]d7395e732009-08-28 23:13:431116 } else if (key == "httpsProxy") {
[email protected]573c0502011-05-17 22:19:501117 AddProxy(PROXY_HTTPS_HOST, value);
[email protected]d7395e732009-08-28 23:13:431118 } else if (key == "ftpProxy") {
[email protected]573c0502011-05-17 22:19:501119 AddProxy(PROXY_FTP_HOST, value);
[email protected]bfeb7232012-06-08 00:58:371120 } else if (key == "socksProxy") {
1121 // Older versions of KDE configure SOCKS in a weird way involving
1122 // LD_PRELOAD and a library that intercepts network calls to SOCKSify
1123 // them. We don't support it. KDE 4.8 added a proper SOCKS setting.
1124 AddProxy(PROXY_SOCKS_HOST, value);
[email protected]d7395e732009-08-28 23:13:431125 } else if (key == "ReversedException") {
1126 // We count "true" or any nonzero number as true, otherwise false.
1127 // Note that if the value is not actually numeric StringToInt()
1128 // will return 0, which we count as false.
[email protected]e83326f2010-07-31 17:29:251129 int int_value;
1130 base::StringToInt(value, &int_value);
1131 reversed_bypass_list_ = (value == "true" || int_value);
[email protected]d7395e732009-08-28 23:13:431132 } else if (key == "NoProxyFor") {
[email protected]573c0502011-05-17 22:19:501133 AddHostList(PROXY_IGNORE_HOSTS, value);
[email protected]d7395e732009-08-28 23:13:431134 } else if (key == "AuthMode") {
1135 // Check for authentication, just so we can warn.
[email protected]e83326f2010-07-31 17:29:251136 int mode;
1137 base::StringToInt(value, &mode);
[email protected]d7395e732009-08-28 23:13:431138 if (mode) {
1139 // ProxyConfig does not support authentication parameters, but
1140 // Chrome will prompt for the password later. So we ignore this.
1141 LOG(WARNING) <<
1142 "Proxy authentication parameters ignored, see bug 16709";
1143 }
1144 }
1145 }
1146
[email protected]6b5fe742011-05-20 21:46:481147 void ResolveIndirect(StringSetting key) {
[email protected]d7395e732009-08-28 23:13:431148 string_map_type::iterator it = string_table_.find(key);
1149 if (it != string_table_.end()) {
[email protected]f18fde22010-05-18 23:49:541150 std::string value;
[email protected]3ba7e082010-08-07 02:57:591151 if (env_var_getter_->GetVar(it->second.c_str(), &value))
[email protected]d7395e732009-08-28 23:13:431152 it->second = value;
[email protected]8425adc02010-04-18 17:45:311153 else
1154 string_table_.erase(it);
[email protected]d7395e732009-08-28 23:13:431155 }
1156 }
1157
[email protected]6b5fe742011-05-20 21:46:481158 void ResolveIndirectList(StringListSetting key) {
[email protected]f18fde22010-05-18 23:49:541159 strings_map_type::iterator it = strings_table_.find(key);
1160 if (it != strings_table_.end()) {
1161 std::string value;
1162 if (!it->second.empty() &&
[email protected]3ba7e082010-08-07 02:57:591163 env_var_getter_->GetVar(it->second[0].c_str(), &value))
[email protected]f18fde22010-05-18 23:49:541164 AddHostList(key, value);
1165 else
1166 strings_table_.erase(it);
1167 }
1168 }
1169
[email protected]d7395e732009-08-28 23:13:431170 // The settings in kioslaverc could occur in any order, but some affect
1171 // others. Rather than read the whole file in and then query them in an
1172 // order that allows us to handle that, we read the settings in whatever
1173 // order they occur and do any necessary tweaking after we finish.
1174 void ResolveModeEffects() {
1175 if (indirect_manual_) {
[email protected]573c0502011-05-17 22:19:501176 ResolveIndirect(PROXY_HTTP_HOST);
1177 ResolveIndirect(PROXY_HTTPS_HOST);
1178 ResolveIndirect(PROXY_FTP_HOST);
1179 ResolveIndirectList(PROXY_IGNORE_HOSTS);
[email protected]d7395e732009-08-28 23:13:431180 }
1181 if (auto_no_pac_) {
1182 // Remove the PAC URL; we're not supposed to use it.
[email protected]573c0502011-05-17 22:19:501183 string_table_.erase(PROXY_AUTOCONF_URL);
[email protected]d7395e732009-08-28 23:13:431184 }
[email protected]d7395e732009-08-28 23:13:431185 }
1186
1187 // Reads kioslaverc one line at a time and calls AddKDESetting() to add
1188 // each relevant name-value pair to the appropriate value table.
1189 void UpdateCachedSettings() {
[email protected]6cdfd7f2013-02-08 20:40:151190 base::FilePath kioslaverc = kde_config_dir_.Append("kioslaverc");
[email protected]b9b4a572014-03-17 23:11:121191 base::ScopedFILE input(base::OpenFile(kioslaverc, "r"));
[email protected]d7395e732009-08-28 23:13:431192 if (!input.get())
1193 return;
1194 ResetCachedSettings();
1195 bool in_proxy_settings = false;
1196 bool line_too_long = false;
[email protected]9a3d8d42009-09-03 17:01:461197 char line[BUFFER_SIZE];
1198 // fgets() will return NULL on EOF or error.
[email protected]d7395e732009-08-28 23:13:431199 while (fgets(line, sizeof(line), input.get())) {
1200 // fgets() guarantees the line will be properly terminated.
1201 size_t length = strlen(line);
1202 if (!length)
1203 continue;
1204 // This should be true even with CRLF endings.
1205 if (line[length - 1] != '\n') {
1206 line_too_long = true;
1207 continue;
1208 }
1209 if (line_too_long) {
1210 // The previous line had no line ending, but this done does. This is
1211 // the end of the line that was too long, so warn here and skip it.
1212 LOG(WARNING) << "skipped very long line in " << kioslaverc.value();
1213 line_too_long = false;
1214 continue;
1215 }
1216 // Remove the LF at the end, and the CR if there is one.
1217 line[--length] = '\0';
1218 if (length && line[length - 1] == '\r')
1219 line[--length] = '\0';
1220 // Now parse the line.
1221 if (line[0] == '[') {
1222 // Switching sections. All we care about is whether this is
1223 // the (a?) proxy settings section, for both KDE3 and KDE4.
1224 in_proxy_settings = !strncmp(line, "[Proxy Settings]", 16);
1225 } else if (in_proxy_settings) {
1226 // A regular line, in the (a?) proxy settings section.
[email protected]9a3d8d42009-09-03 17:01:461227 char* split = strchr(line, '=');
1228 // Skip this line if it does not contain an = sign.
1229 if (!split)
[email protected]d7395e732009-08-28 23:13:431230 continue;
[email protected]9a3d8d42009-09-03 17:01:461231 // Split the line on the = and advance |split|.
1232 *(split++) = 0;
1233 std::string key = line;
1234 std::string value = split;
[email protected]8af69c6c2014-03-03 19:05:311235 base::TrimWhitespaceASCII(key, base::TRIM_ALL, &key);
1236 base::TrimWhitespaceASCII(value, base::TRIM_ALL, &value);
[email protected]9a3d8d42009-09-03 17:01:461237 // Skip this line if the key name is empty.
1238 if (key.empty())
[email protected]d7395e732009-08-28 23:13:431239 continue;
1240 // Is the value name localized?
[email protected]9a3d8d42009-09-03 17:01:461241 if (key[key.length() - 1] == ']') {
1242 // Find the matching bracket.
1243 length = key.rfind('[');
1244 // Skip this line if the localization indicator is malformed.
1245 if (length == std::string::npos)
[email protected]d7395e732009-08-28 23:13:431246 continue;
1247 // Trim the localization indicator off.
[email protected]9a3d8d42009-09-03 17:01:461248 key.resize(length);
1249 // Remove any resulting trailing whitespace.
[email protected]8af69c6c2014-03-03 19:05:311250 base::TrimWhitespaceASCII(key, base::TRIM_TRAILING, &key);
[email protected]9a3d8d42009-09-03 17:01:461251 // Skip this line if the key name is now empty.
1252 if (key.empty())
1253 continue;
[email protected]d7395e732009-08-28 23:13:431254 }
[email protected]d7395e732009-08-28 23:13:431255 // Now fill in the tables.
[email protected]9a3d8d42009-09-03 17:01:461256 AddKDESetting(key, value);
[email protected]d7395e732009-08-28 23:13:431257 }
1258 }
1259 if (ferror(input.get()))
1260 LOG(ERROR) << "error reading " << kioslaverc.value();
1261 ResolveModeEffects();
1262 }
1263
1264 // This is the callback from the debounce timer.
1265 void OnDebouncedNotification() {
sergeyu3f923062014-09-05 01:39:401266 DCHECK(file_task_runner_->BelongsToCurrentThread());
[email protected]b30a3f52010-10-16 01:05:461267 VLOG(1) << "inotify change notification for kioslaverc";
[email protected]d7395e732009-08-28 23:13:431268 UpdateCachedSettings();
[email protected]961ac942011-04-28 18:18:141269 CHECK(notify_delegate_);
[email protected]d7395e732009-08-28 23:13:431270 // Forward to a method on the proxy config service delegate object.
1271 notify_delegate_->OnCheckProxyConfigSettings();
1272 }
1273
1274 // Called by OnFileCanReadWithoutBlocking() on the file thread. Reads
1275 // from the inotify file descriptor and starts up a debounce timer if
1276 // an event for kioslaverc is seen.
1277 void OnChangeNotification() {
[email protected]d2e6d592012-02-03 21:49:041278 DCHECK_GE(inotify_fd_, 0);
sergeyu3f923062014-09-05 01:39:401279 DCHECK(file_task_runner_->BelongsToCurrentThread());
[email protected]d7395e732009-08-28 23:13:431280 char event_buf[(sizeof(inotify_event) + NAME_MAX + 1) * 4];
1281 bool kioslaverc_touched = false;
1282 ssize_t r;
1283 while ((r = read(inotify_fd_, event_buf, sizeof(event_buf))) > 0) {
1284 // inotify returns variable-length structures, which is why we have
1285 // this strange-looking loop instead of iterating through an array.
1286 char* event_ptr = event_buf;
1287 while (event_ptr < event_buf + r) {
1288 inotify_event* event = reinterpret_cast<inotify_event*>(event_ptr);
1289 // The kernel always feeds us whole events.
[email protected]b1f031dd2010-03-02 23:19:331290 CHECK_LE(event_ptr + sizeof(inotify_event), event_buf + r);
1291 CHECK_LE(event->name + event->len, event_buf + r);
[email protected]d7395e732009-08-28 23:13:431292 if (!strcmp(event->name, "kioslaverc"))
1293 kioslaverc_touched = true;
1294 // Advance the pointer just past the end of the filename.
1295 event_ptr = event->name + event->len;
1296 }
1297 // We keep reading even if |kioslaverc_touched| is true to drain the
1298 // inotify event queue.
1299 }
1300 if (!r)
1301 // Instead of returning -1 and setting errno to EINVAL if there is not
1302 // enough buffer space, older kernels (< 2.6.21) return 0. Simulate the
1303 // new behavior (EINVAL) so we can reuse the code below.
1304 errno = EINVAL;
1305 if (errno != EAGAIN) {
[email protected]57b765672009-10-13 18:27:401306 PLOG(WARNING) << "error reading inotify file descriptor";
[email protected]d7395e732009-08-28 23:13:431307 if (errno == EINVAL) {
1308 // Our buffer is not large enough to read the next event. This should
1309 // not happen (because its size is calculated to always be sufficiently
1310 // large), but if it does we'd warn continuously since |inotify_fd_|
1311 // would be forever ready to read. Close it and stop watching instead.
1312 LOG(ERROR) << "inotify failure; no longer watching kioslaverc!";
1313 inotify_watcher_.StopWatchingFileDescriptor();
1314 close(inotify_fd_);
1315 inotify_fd_ = -1;
1316 }
1317 }
1318 if (kioslaverc_touched) {
1319 // We don't use Reset() because the timer may not yet be running.
1320 // (In that case Stop() is a no-op.)
1321 debounce_timer_.Stop();
[email protected]d323a172011-09-02 18:23:021322 debounce_timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(
[email protected]d7395e732009-08-28 23:13:431323 kDebounceTimeoutMilliseconds), this,
[email protected]573c0502011-05-17 22:19:501324 &SettingGetterImplKDE::OnDebouncedNotification);
[email protected]d7395e732009-08-28 23:13:431325 }
1326 }
1327
[email protected]6b5fe742011-05-20 21:46:481328 typedef std::map<StringSetting, std::string> string_map_type;
1329 typedef std::map<StringListSetting,
1330 std::vector<std::string> > strings_map_type;
[email protected]d7395e732009-08-28 23:13:431331
1332 int inotify_fd_;
1333 base::MessagePumpLibevent::FileDescriptorWatcher inotify_watcher_;
1334 ProxyConfigServiceLinux::Delegate* notify_delegate_;
[email protected]573c0502011-05-17 22:19:501335 base::OneShotTimer<SettingGetterImplKDE> debounce_timer_;
[email protected]6cdfd7f2013-02-08 20:40:151336 base::FilePath kde_config_dir_;
[email protected]d7395e732009-08-28 23:13:431337 bool indirect_manual_;
1338 bool auto_no_pac_;
[email protected]a48bf4a2010-06-14 18:24:531339 bool reversed_bypass_list_;
[email protected]f18fde22010-05-18 23:49:541340 // We don't own |env_var_getter_|. It's safe to hold a pointer to it, since
1341 // both it and us are owned by ProxyConfigServiceLinux::Delegate, and have the
1342 // same lifetime.
[email protected]76b90d312010-08-03 03:00:501343 base::Environment* env_var_getter_;
[email protected]d7395e732009-08-28 23:13:431344
1345 // We cache these settings whenever we re-read the kioslaverc file.
1346 string_map_type string_table_;
1347 strings_map_type strings_table_;
1348
sergeyu3f923062014-09-05 01:39:401349 // Task runner of the file thread, for reading kioslaverc. If NULL,
[email protected]d7395e732009-08-28 23:13:431350 // just read it directly (for testing). We also handle inotify events
1351 // on this thread.
sergeyu3f923062014-09-05 01:39:401352 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_;
[email protected]d7395e732009-08-28 23:13:431353
[email protected]573c0502011-05-17 22:19:501354 DISALLOW_COPY_AND_ASSIGN(SettingGetterImplKDE);
[email protected]861c6c62009-04-20 16:50:561355};
1356
1357} // namespace
1358
[email protected]573c0502011-05-17 22:19:501359bool ProxyConfigServiceLinux::Delegate::GetProxyFromSettings(
[email protected]6b5fe742011-05-20 21:46:481360 SettingGetter::StringSetting host_key,
[email protected]573c0502011-05-17 22:19:501361 ProxyServer* result_server) {
[email protected]861c6c62009-04-20 16:50:561362 std::string host;
[email protected]573c0502011-05-17 22:19:501363 if (!setting_getter_->GetString(host_key, &host) || host.empty()) {
[email protected]861c6c62009-04-20 16:50:561364 // Unset or empty.
1365 return false;
1366 }
1367 // Check for an optional port.
[email protected]d7395e732009-08-28 23:13:431368 int port = 0;
[email protected]6b5fe742011-05-20 21:46:481369 SettingGetter::IntSetting port_key =
[email protected]573c0502011-05-17 22:19:501370 SettingGetter::HostSettingToPortSetting(host_key);
1371 setting_getter_->GetInt(port_key, &port);
[email protected]861c6c62009-04-20 16:50:561372 if (port != 0) {
1373 // If a port is set and non-zero:
[email protected]528c56d2010-07-30 19:28:441374 host += ":" + base::IntToString(port);
[email protected]861c6c62009-04-20 16:50:561375 }
[email protected]76960f3d2011-04-30 02:15:231376
[email protected]573c0502011-05-17 22:19:501377 // gconf settings do not appear to distinguish between SOCKS version. We
1378 // default to version 5. For more information on this policy decision, see:
[email protected]76960f3d2011-04-30 02:15:231379 // http://code.google.com/p/chromium/issues/detail?id=55912#c2
[email protected]573c0502011-05-17 22:19:501380 ProxyServer::Scheme scheme = (host_key == SettingGetter::PROXY_SOCKS_HOST) ?
1381 ProxyServer::SCHEME_SOCKS5 : ProxyServer::SCHEME_HTTP;
1382 host = FixupProxyHostScheme(scheme, host);
[email protected]87a102b2009-07-14 05:23:301383 ProxyServer proxy_server = ProxyServer::FromURI(host,
1384 ProxyServer::SCHEME_HTTP);
[email protected]861c6c62009-04-20 16:50:561385 if (proxy_server.is_valid()) {
1386 *result_server = proxy_server;
1387 return true;
1388 }
1389 return false;
1390}
1391
[email protected]573c0502011-05-17 22:19:501392bool ProxyConfigServiceLinux::Delegate::GetConfigFromSettings(
[email protected]3e44697f2009-05-22 14:37:391393 ProxyConfig* config) {
[email protected]861c6c62009-04-20 16:50:561394 std::string mode;
[email protected]573c0502011-05-17 22:19:501395 if (!setting_getter_->GetString(SettingGetter::PROXY_MODE, &mode)) {
[email protected]861c6c62009-04-20 16:50:561396 // We expect this to always be set, so if we don't see it then we
[email protected]573c0502011-05-17 22:19:501397 // probably have a gconf/gsettings problem, and so we don't have a valid
[email protected]861c6c62009-04-20 16:50:561398 // proxy config.
1399 return false;
1400 }
[email protected]3e44697f2009-05-22 14:37:391401 if (mode == "none") {
[email protected]861c6c62009-04-20 16:50:561402 // Specifically specifies no proxy.
1403 return true;
[email protected]3e44697f2009-05-22 14:37:391404 }
[email protected]861c6c62009-04-20 16:50:561405
[email protected]3e44697f2009-05-22 14:37:391406 if (mode == "auto") {
[email protected]aa3ac2cc2012-06-19 00:28:041407 // Automatic proxy config.
[email protected]861c6c62009-04-20 16:50:561408 std::string pac_url_str;
[email protected]573c0502011-05-17 22:19:501409 if (setting_getter_->GetString(SettingGetter::PROXY_AUTOCONF_URL,
1410 &pac_url_str)) {
[email protected]861c6c62009-04-20 16:50:561411 if (!pac_url_str.empty()) {
[email protected]aa3ac2cc2012-06-19 00:28:041412 // If the PAC URL is actually a file path, then put file:// in front.
1413 if (pac_url_str[0] == '/')
1414 pac_url_str = "file://" + pac_url_str;
[email protected]861c6c62009-04-20 16:50:561415 GURL pac_url(pac_url_str);
1416 if (!pac_url.is_valid())
1417 return false;
[email protected]ed4ed0f2010-02-24 00:20:481418 config->set_pac_url(pac_url);
[email protected]861c6c62009-04-20 16:50:561419 return true;
1420 }
1421 }
[email protected]ed4ed0f2010-02-24 00:20:481422 config->set_auto_detect(true);
[email protected]861c6c62009-04-20 16:50:561423 return true;
1424 }
1425
[email protected]3e44697f2009-05-22 14:37:391426 if (mode != "manual") {
[email protected]861c6c62009-04-20 16:50:561427 // Mode is unrecognized.
1428 return false;
1429 }
1430 bool use_http_proxy;
[email protected]573c0502011-05-17 22:19:501431 if (setting_getter_->GetBool(SettingGetter::PROXY_USE_HTTP_PROXY,
1432 &use_http_proxy)
[email protected]861c6c62009-04-20 16:50:561433 && !use_http_proxy) {
1434 // Another master switch for some reason. If set to false, then no
1435 // proxy. But we don't panic if the key doesn't exist.
1436 return true;
1437 }
1438
1439 bool same_proxy = false;
1440 // Indicates to use the http proxy for all protocols. This one may
[email protected]573c0502011-05-17 22:19:501441 // not exist (presumably on older versions); we assume false in that
[email protected]861c6c62009-04-20 16:50:561442 // case.
[email protected]573c0502011-05-17 22:19:501443 setting_getter_->GetBool(SettingGetter::PROXY_USE_SAME_PROXY,
1444 &same_proxy);
[email protected]861c6c62009-04-20 16:50:561445
[email protected]76960f3d2011-04-30 02:15:231446 ProxyServer proxy_for_http;
1447 ProxyServer proxy_for_https;
1448 ProxyServer proxy_for_ftp;
1449 ProxyServer socks_proxy; // (socks)
1450
1451 // This counts how many of the above ProxyServers were defined and valid.
1452 size_t num_proxies_specified = 0;
1453
1454 // Extract the per-scheme proxies. If we failed to parse it, or no proxy was
1455 // specified for the scheme, then the resulting ProxyServer will be invalid.
[email protected]573c0502011-05-17 22:19:501456 if (GetProxyFromSettings(SettingGetter::PROXY_HTTP_HOST, &proxy_for_http))
[email protected]76960f3d2011-04-30 02:15:231457 num_proxies_specified++;
[email protected]573c0502011-05-17 22:19:501458 if (GetProxyFromSettings(SettingGetter::PROXY_HTTPS_HOST, &proxy_for_https))
[email protected]76960f3d2011-04-30 02:15:231459 num_proxies_specified++;
[email protected]573c0502011-05-17 22:19:501460 if (GetProxyFromSettings(SettingGetter::PROXY_FTP_HOST, &proxy_for_ftp))
[email protected]76960f3d2011-04-30 02:15:231461 num_proxies_specified++;
[email protected]573c0502011-05-17 22:19:501462 if (GetProxyFromSettings(SettingGetter::PROXY_SOCKS_HOST, &socks_proxy))
[email protected]76960f3d2011-04-30 02:15:231463 num_proxies_specified++;
1464
1465 if (same_proxy) {
1466 if (proxy_for_http.is_valid()) {
1467 // Use the http proxy for all schemes.
[email protected]ed4ed0f2010-02-24 00:20:481468 config->proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY;
[email protected]2189e092013-03-16 18:02:021469 config->proxy_rules().single_proxies.SetSingleProxyServer(proxy_for_http);
[email protected]861c6c62009-04-20 16:50:561470 }
[email protected]76960f3d2011-04-30 02:15:231471 } else if (num_proxies_specified > 0) {
1472 if (socks_proxy.is_valid() && num_proxies_specified == 1) {
1473 // If the only proxy specified was for SOCKS, use it for all schemes.
1474 config->proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY;
[email protected]2189e092013-03-16 18:02:021475 config->proxy_rules().single_proxies.SetSingleProxyServer(socks_proxy);
[email protected]861c6c62009-04-20 16:50:561476 } else {
[email protected]2189e092013-03-16 18:02:021477 // Otherwise use the indicated proxies per-scheme.
[email protected]76960f3d2011-04-30 02:15:231478 config->proxy_rules().type =
1479 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME;
[email protected]2189e092013-03-16 18:02:021480 config->proxy_rules().proxies_for_http.
1481 SetSingleProxyServer(proxy_for_http);
1482 config->proxy_rules().proxies_for_https.
1483 SetSingleProxyServer(proxy_for_https);
1484 config->proxy_rules().proxies_for_ftp.SetSingleProxyServer(proxy_for_ftp);
1485 config->proxy_rules().fallback_proxies.SetSingleProxyServer(socks_proxy);
[email protected]861c6c62009-04-20 16:50:561486 }
1487 }
1488
[email protected]ed4ed0f2010-02-24 00:20:481489 if (config->proxy_rules().empty()) {
[email protected]861c6c62009-04-20 16:50:561490 // Manual mode but we couldn't parse any rules.
1491 return false;
1492 }
1493
1494 // Check for authentication, just so we can warn.
[email protected]d7395e732009-08-28 23:13:431495 bool use_auth = false;
[email protected]573c0502011-05-17 22:19:501496 setting_getter_->GetBool(SettingGetter::PROXY_USE_AUTHENTICATION,
1497 &use_auth);
[email protected]62749f182009-07-15 13:16:541498 if (use_auth) {
1499 // ProxyConfig does not support authentication parameters, but
1500 // Chrome will prompt for the password later. So we ignore
1501 // /system/http_proxy/*auth* settings.
1502 LOG(WARNING) << "Proxy authentication parameters ignored, see bug 16709";
1503 }
[email protected]861c6c62009-04-20 16:50:561504
1505 // Now the bypass list.
[email protected]7541206c2010-02-19 20:24:061506 std::vector<std::string> ignore_hosts_list;
[email protected]ed4ed0f2010-02-24 00:20:481507 config->proxy_rules().bypass_rules.Clear();
[email protected]573c0502011-05-17 22:19:501508 if (setting_getter_->GetStringList(SettingGetter::PROXY_IGNORE_HOSTS,
1509 &ignore_hosts_list)) {
[email protected]a8185d02010-06-11 00:19:501510 std::vector<std::string>::const_iterator it(ignore_hosts_list.begin());
[email protected]1a597192010-07-09 16:58:381511 for (; it != ignore_hosts_list.end(); ++it) {
[email protected]573c0502011-05-17 22:19:501512 if (setting_getter_->MatchHostsUsingSuffixMatching()) {
[email protected]1a597192010-07-09 16:58:381513 config->proxy_rules().bypass_rules.
1514 AddRuleFromStringUsingSuffixMatching(*it);
1515 } else {
1516 config->proxy_rules().bypass_rules.AddRuleFromString(*it);
1517 }
1518 }
[email protected]a8185d02010-06-11 00:19:501519 }
[email protected]861c6c62009-04-20 16:50:561520 // Note that there are no settings with semantics corresponding to
[email protected]1a597192010-07-09 16:58:381521 // bypass of local names in GNOME. In KDE, "<local>" is supported
1522 // as a hostname rule.
[email protected]861c6c62009-04-20 16:50:561523
[email protected]a48bf4a2010-06-14 18:24:531524 // KDE allows one to reverse the bypass rules.
[email protected]573c0502011-05-17 22:19:501525 config->proxy_rules().reverse_bypass =
1526 setting_getter_->BypassListIsReversed();
[email protected]a48bf4a2010-06-14 18:24:531527
[email protected]861c6c62009-04-20 16:50:561528 return true;
1529}
1530
[email protected]76b90d312010-08-03 03:00:501531ProxyConfigServiceLinux::Delegate::Delegate(base::Environment* env_var_getter)
[email protected]76722472012-05-24 08:26:461532 : env_var_getter_(env_var_getter) {
[email protected]573c0502011-05-17 22:19:501533 // Figure out which SettingGetterImpl to use, if any.
[email protected]6b0349ef2010-10-16 04:56:061534 switch (base::nix::GetDesktopEnvironment(env_var_getter)) {
1535 case base::nix::DESKTOP_ENVIRONMENT_GNOME:
[email protected]9e6c9bde2012-07-17 23:40:171536 case base::nix::DESKTOP_ENVIRONMENT_UNITY:
[email protected]8c20e3d2011-05-19 21:03:571537#if defined(USE_GIO)
1538 {
1539 scoped_ptr<SettingGetterImplGSettings> gs_getter(
1540 new SettingGetterImplGSettings());
1541 // We have to load symbols and check the GNOME version in use to decide
1542 // if we should use the gsettings getter. See LoadAndCheckVersion().
1543 if (gs_getter->LoadAndCheckVersion(env_var_getter))
1544 setting_getter_.reset(gs_getter.release());
1545 }
1546#endif
[email protected]6de53d42010-11-09 07:33:191547#if defined(USE_GCONF)
[email protected]8c20e3d2011-05-19 21:03:571548 // Fall back on gconf if gsettings is unavailable or incorrect.
1549 if (!setting_getter_.get())
1550 setting_getter_.reset(new SettingGetterImplGConf());
[email protected]6de53d42010-11-09 07:33:191551#endif
[email protected]d7395e732009-08-28 23:13:431552 break;
[email protected]6b0349ef2010-10-16 04:56:061553 case base::nix::DESKTOP_ENVIRONMENT_KDE3:
1554 case base::nix::DESKTOP_ENVIRONMENT_KDE4:
[email protected]573c0502011-05-17 22:19:501555 setting_getter_.reset(new SettingGetterImplKDE(env_var_getter));
[email protected]d7395e732009-08-28 23:13:431556 break;
[email protected]6b0349ef2010-10-16 04:56:061557 case base::nix::DESKTOP_ENVIRONMENT_XFCE:
1558 case base::nix::DESKTOP_ENVIRONMENT_OTHER:
[email protected]d7395e732009-08-28 23:13:431559 break;
1560 }
1561}
1562
[email protected]573c0502011-05-17 22:19:501563ProxyConfigServiceLinux::Delegate::Delegate(
1564 base::Environment* env_var_getter, SettingGetter* setting_getter)
[email protected]76722472012-05-24 08:26:461565 : env_var_getter_(env_var_getter), setting_getter_(setting_getter) {
[email protected]861c6c62009-04-20 16:50:561566}
1567
[email protected]d3066142011-05-10 02:36:201568void ProxyConfigServiceLinux::Delegate::SetUpAndFetchInitialConfig(
sergeyu3f923062014-09-05 01:39:401569 const scoped_refptr<base::SingleThreadTaskRunner>& glib_task_runner,
1570 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner,
1571 const scoped_refptr<base::SingleThreadTaskRunner>& file_task_runner) {
[email protected]3e44697f2009-05-22 14:37:391572 // We should be running on the default glib main loop thread right
1573 // now. gconf can only be accessed from this thread.
sergeyu3f923062014-09-05 01:39:401574 DCHECK(glib_task_runner->BelongsToCurrentThread());
1575 glib_task_runner_ = glib_task_runner;
1576 io_task_runner_ = io_task_runner;
[email protected]3e44697f2009-05-22 14:37:391577
sergeyu3f923062014-09-05 01:39:401578 // If we are passed a NULL |io_task_runner| or |file_task_runner|, then we
1579 // don't set up proxy setting change notifications. This should not be the
1580 // usual case but is intended to/ simplify test setups.
Daniel Cheng99adf742014-09-05 06:26:501581 if (!io_task_runner_.get() || !file_task_runner.get())
[email protected]b30a3f52010-10-16 01:05:461582 VLOG(1) << "Monitoring of proxy setting changes is disabled";
[email protected]3e44697f2009-05-22 14:37:391583
1584 // Fetch and cache the current proxy config. The config is left in
[email protected]119655002010-07-23 06:02:401585 // cached_config_, where GetLatestProxyConfig() running on the IO thread
[email protected]3e44697f2009-05-22 14:37:391586 // will expect to find it. This is safe to do because we return
1587 // before this ProxyConfigServiceLinux is passed on to
1588 // the ProxyService.
[email protected]d6cb85b2009-07-23 22:10:531589
1590 // Note: It would be nice to prioritize environment variables
[email protected]92d2dc82010-04-08 17:49:591591 // and only fall back to gconf if env vars were unset. But
[email protected]d6cb85b2009-07-23 22:10:531592 // gnome-terminal "helpfully" sets http_proxy and no_proxy, and it
1593 // does so even if the proxy mode is set to auto, which would
1594 // mislead us.
1595
[email protected]3e44697f2009-05-22 14:37:391596 bool got_config = false;
[email protected]573c0502011-05-17 22:19:501597 if (setting_getter_.get() &&
sergeyu3f923062014-09-05 01:39:401598 setting_getter_->Init(glib_task_runner, file_task_runner) &&
[email protected]573c0502011-05-17 22:19:501599 GetConfigFromSettings(&cached_config_)) {
[email protected]d3066142011-05-10 02:36:201600 cached_config_.set_id(1); // Mark it as valid.
[email protected]db8ff912012-06-12 23:32:511601 cached_config_.set_source(setting_getter_->GetConfigSource());
[email protected]d3066142011-05-10 02:36:201602 VLOG(1) << "Obtained proxy settings from "
[email protected]db8ff912012-06-12 23:32:511603 << ProxyConfigSourceToString(cached_config_.source());
[email protected]d3066142011-05-10 02:36:201604
1605 // If gconf proxy mode is "none", meaning direct, then we take
1606 // that to be a valid config and will not check environment
1607 // variables. The alternative would have been to look for a proxy
1608 // whereever we can find one.
1609 got_config = true;
1610
1611 // Keep a copy of the config for use from this thread for
1612 // comparison with updated settings when we get notifications.
1613 reference_config_ = cached_config_;
1614 reference_config_.set_id(1); // Mark it as valid.
1615
1616 // We only set up notifications if we have IO and file loops available.
1617 // We do this after getting the initial configuration so that we don't have
1618 // to worry about cancelling it if the initial fetch above fails. Note that
1619 // setting up notifications has the side effect of simulating a change, so
1620 // that we won't lose any updates that may have happened after the initial
1621 // fetch and before setting up notifications. We'll detect the common case
1622 // of no changes in OnCheckProxyConfigSettings() (or sooner) and ignore it.
Daniel Cheng99adf742014-09-05 06:26:501623 if (io_task_runner.get() && file_task_runner.get()) {
[email protected]76722472012-05-24 08:26:461624 scoped_refptr<base::SingleThreadTaskRunner> required_loop =
1625 setting_getter_->GetNotificationTaskRunner();
[email protected]90499482013-06-01 00:39:501626 if (!required_loop.get() || required_loop->BelongsToCurrentThread()) {
[email protected]d3066142011-05-10 02:36:201627 // In this case we are already on an acceptable thread.
1628 SetUpNotifications();
[email protected]d7395e732009-08-28 23:13:431629 } else {
[email protected]d3066142011-05-10 02:36:201630 // Post a task to set up notifications. We don't wait for success.
[email protected]6af889c2011-10-06 23:11:411631 required_loop->PostTask(FROM_HERE, base::Bind(
1632 &ProxyConfigServiceLinux::Delegate::SetUpNotifications, this));
[email protected]d6cb85b2009-07-23 22:10:531633 }
[email protected]d7395e732009-08-28 23:13:431634 }
[email protected]861c6c62009-04-20 16:50:561635 }
[email protected]d6cb85b2009-07-23 22:10:531636
[email protected]3e44697f2009-05-22 14:37:391637 if (!got_config) {
[email protected]d6cb85b2009-07-23 22:10:531638 // We fall back on environment variables.
[email protected]3e44697f2009-05-22 14:37:391639 //
[email protected]d3066142011-05-10 02:36:201640 // Consulting environment variables doesn't need to be done from the
1641 // default glib main loop, but it's a tiny enough amount of work.
[email protected]3e44697f2009-05-22 14:37:391642 if (GetConfigFromEnv(&cached_config_)) {
[email protected]db8ff912012-06-12 23:32:511643 cached_config_.set_source(PROXY_CONFIG_SOURCE_ENV);
[email protected]d3066142011-05-10 02:36:201644 cached_config_.set_id(1); // Mark it as valid.
[email protected]b30a3f52010-10-16 01:05:461645 VLOG(1) << "Obtained proxy settings from environment variables";
[email protected]3e44697f2009-05-22 14:37:391646 }
[email protected]861c6c62009-04-20 16:50:561647 }
[email protected]3e44697f2009-05-22 14:37:391648}
1649
[email protected]573c0502011-05-17 22:19:501650// Depending on the SettingGetter in use, this method will be called
[email protected]d3066142011-05-10 02:36:201651// on either the UI thread (GConf) or the file thread (KDE).
1652void ProxyConfigServiceLinux::Delegate::SetUpNotifications() {
[email protected]76722472012-05-24 08:26:461653 scoped_refptr<base::SingleThreadTaskRunner> required_loop =
1654 setting_getter_->GetNotificationTaskRunner();
[email protected]90499482013-06-01 00:39:501655 DCHECK(!required_loop.get() || required_loop->BelongsToCurrentThread());
[email protected]573c0502011-05-17 22:19:501656 if (!setting_getter_->SetUpNotifications(this))
[email protected]d3066142011-05-10 02:36:201657 LOG(ERROR) << "Unable to set up proxy configuration change notifications";
1658}
1659
[email protected]119655002010-07-23 06:02:401660void ProxyConfigServiceLinux::Delegate::AddObserver(Observer* observer) {
1661 observers_.AddObserver(observer);
1662}
1663
1664void ProxyConfigServiceLinux::Delegate::RemoveObserver(Observer* observer) {
1665 observers_.RemoveObserver(observer);
1666}
1667
[email protected]3a29593d2011-04-11 10:07:521668ProxyConfigService::ConfigAvailability
1669 ProxyConfigServiceLinux::Delegate::GetLatestProxyConfig(
1670 ProxyConfig* config) {
[email protected]3e44697f2009-05-22 14:37:391671 // This is called from the IO thread.
sergeyu3f923062014-09-05 01:39:401672 DCHECK(!io_task_runner_.get() ||
1673 io_task_runner_->BelongsToCurrentThread());
[email protected]3e44697f2009-05-22 14:37:391674
1675 // Simply return the last proxy configuration that glib_default_loop
1676 // notified us of.
[email protected]db8ff912012-06-12 23:32:511677 if (cached_config_.is_valid()) {
1678 *config = cached_config_;
1679 } else {
1680 *config = ProxyConfig::CreateDirect();
1681 config->set_source(PROXY_CONFIG_SOURCE_SYSTEM_FAILED);
1682 }
[email protected]119655002010-07-23 06:02:401683
[email protected]3a29593d2011-04-11 10:07:521684 // We return CONFIG_VALID to indicate that *config was filled in. It is always
[email protected]119655002010-07-23 06:02:401685 // going to be available since we initialized eagerly on the UI thread.
1686 // TODO(eroman): do lazy initialization instead, so we no longer need
1687 // to construct ProxyConfigServiceLinux on the UI thread.
1688 // In which case, we may return false here.
[email protected]3a29593d2011-04-11 10:07:521689 return CONFIG_VALID;
[email protected]3e44697f2009-05-22 14:37:391690}
1691
[email protected]573c0502011-05-17 22:19:501692// Depending on the SettingGetter in use, this method will be called
[email protected]d7395e732009-08-28 23:13:431693// on either the UI thread (GConf) or the file thread (KDE).
[email protected]3e44697f2009-05-22 14:37:391694void ProxyConfigServiceLinux::Delegate::OnCheckProxyConfigSettings() {
[email protected]76722472012-05-24 08:26:461695 scoped_refptr<base::SingleThreadTaskRunner> required_loop =
1696 setting_getter_->GetNotificationTaskRunner();
[email protected]90499482013-06-01 00:39:501697 DCHECK(!required_loop.get() || required_loop->BelongsToCurrentThread());
[email protected]3e44697f2009-05-22 14:37:391698 ProxyConfig new_config;
[email protected]573c0502011-05-17 22:19:501699 bool valid = GetConfigFromSettings(&new_config);
[email protected]3e44697f2009-05-22 14:37:391700 if (valid)
1701 new_config.set_id(1); // mark it as valid
1702
[email protected]119655002010-07-23 06:02:401703 // See if it is different from what we had before.
[email protected]3e44697f2009-05-22 14:37:391704 if (new_config.is_valid() != reference_config_.is_valid() ||
1705 !new_config.Equals(reference_config_)) {
[email protected]76722472012-05-24 08:26:461706 // Post a task to the IO thread with the new configuration, so it can
[email protected]3e44697f2009-05-22 14:37:391707 // update |cached_config_|.
sergeyu3f923062014-09-05 01:39:401708 io_task_runner_->PostTask(FROM_HERE, base::Bind(
[email protected]6af889c2011-10-06 23:11:411709 &ProxyConfigServiceLinux::Delegate::SetNewProxyConfig,
1710 this, new_config));
[email protected]d1f9d472009-08-13 19:59:301711 // Update the thread-private copy in |reference_config_| as well.
1712 reference_config_ = new_config;
[email protected]d3066142011-05-10 02:36:201713 } else {
1714 VLOG(1) << "Detected no-op change to proxy settings. Doing nothing.";
[email protected]3e44697f2009-05-22 14:37:391715 }
1716}
1717
1718void ProxyConfigServiceLinux::Delegate::SetNewProxyConfig(
1719 const ProxyConfig& new_config) {
sergeyu3f923062014-09-05 01:39:401720 DCHECK(io_task_runner_->BelongsToCurrentThread());
[email protected]b30a3f52010-10-16 01:05:461721 VLOG(1) << "Proxy configuration changed";
[email protected]3e44697f2009-05-22 14:37:391722 cached_config_ = new_config;
[email protected]3a29593d2011-04-11 10:07:521723 FOR_EACH_OBSERVER(
1724 Observer, observers_,
1725 OnProxyConfigChanged(new_config, ProxyConfigService::CONFIG_VALID));
[email protected]3e44697f2009-05-22 14:37:391726}
1727
1728void ProxyConfigServiceLinux::Delegate::PostDestroyTask() {
[email protected]573c0502011-05-17 22:19:501729 if (!setting_getter_.get())
[email protected]d7395e732009-08-28 23:13:431730 return;
[email protected]76722472012-05-24 08:26:461731 scoped_refptr<base::SingleThreadTaskRunner> shutdown_loop =
1732 setting_getter_->GetNotificationTaskRunner();
[email protected]90499482013-06-01 00:39:501733 if (!shutdown_loop.get() || shutdown_loop->BelongsToCurrentThread()) {
[email protected]3e44697f2009-05-22 14:37:391734 // Already on the right thread, call directly.
1735 // This is the case for the unittests.
1736 OnDestroy();
1737 } else {
[email protected]d7395e732009-08-28 23:13:431738 // Post to shutdown thread. Note that on browser shutdown, we may quit
1739 // this MessageLoop and exit the program before ever running this.
[email protected]6af889c2011-10-06 23:11:411740 shutdown_loop->PostTask(FROM_HERE, base::Bind(
1741 &ProxyConfigServiceLinux::Delegate::OnDestroy, this));
[email protected]3e44697f2009-05-22 14:37:391742 }
1743}
1744void ProxyConfigServiceLinux::Delegate::OnDestroy() {
[email protected]76722472012-05-24 08:26:461745 scoped_refptr<base::SingleThreadTaskRunner> shutdown_loop =
1746 setting_getter_->GetNotificationTaskRunner();
[email protected]90499482013-06-01 00:39:501747 DCHECK(!shutdown_loop.get() || shutdown_loop->BelongsToCurrentThread());
[email protected]573c0502011-05-17 22:19:501748 setting_getter_->ShutDown();
[email protected]3e44697f2009-05-22 14:37:391749}
1750
1751ProxyConfigServiceLinux::ProxyConfigServiceLinux()
[email protected]76b90d312010-08-03 03:00:501752 : delegate_(new Delegate(base::Environment::Create())) {
[email protected]3e44697f2009-05-22 14:37:391753}
1754
[email protected]8e1845e12010-09-15 19:22:241755ProxyConfigServiceLinux::~ProxyConfigServiceLinux() {
1756 delegate_->PostDestroyTask();
1757}
1758
[email protected]3e44697f2009-05-22 14:37:391759ProxyConfigServiceLinux::ProxyConfigServiceLinux(
[email protected]76b90d312010-08-03 03:00:501760 base::Environment* env_var_getter)
[email protected]9a3d8d42009-09-03 17:01:461761 : delegate_(new Delegate(env_var_getter)) {
1762}
1763
1764ProxyConfigServiceLinux::ProxyConfigServiceLinux(
[email protected]573c0502011-05-17 22:19:501765 base::Environment* env_var_getter, SettingGetter* setting_getter)
1766 : delegate_(new Delegate(env_var_getter, setting_getter)) {
[email protected]861c6c62009-04-20 16:50:561767}
1768
[email protected]e4be2dd2010-12-14 00:44:391769void ProxyConfigServiceLinux::AddObserver(Observer* observer) {
1770 delegate_->AddObserver(observer);
1771}
1772
1773void ProxyConfigServiceLinux::RemoveObserver(Observer* observer) {
1774 delegate_->RemoveObserver(observer);
1775}
1776
[email protected]3a29593d2011-04-11 10:07:521777ProxyConfigService::ConfigAvailability
1778 ProxyConfigServiceLinux::GetLatestProxyConfig(ProxyConfig* config) {
[email protected]e4be2dd2010-12-14 00:44:391779 return delegate_->GetLatestProxyConfig(config);
1780}
1781
[email protected]861c6c62009-04-20 16:50:561782} // namespace net