blob: 5c608e0ed32b85e4068339b129787de3cae08626 [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
[email protected]76722472012-05-24 08:26:46235 virtual bool Init(base::SingleThreadTaskRunner* glib_thread_task_runner,
[email protected]2da659e2013-05-23 20:51:34236 base::MessageLoopForIO* file_loop) OVERRIDE {
[email protected]76722472012-05-24 08:26:46237 DCHECK(glib_thread_task_runner->BelongsToCurrentThread());
[email protected]3e44697f2009-05-22 14:37:39238 DCHECK(!client_);
[email protected]90499482013-06-01 00:39:50239 DCHECK(!task_runner_.get());
[email protected]76722472012-05-24 08:26:46240 task_runner_ = glib_thread_task_runner;
[email protected]3e44697f2009-05-22 14:37:39241 client_ = gconf_client_get_default();
[email protected]861c6c62009-04-20 16:50:56242 if (!client_) {
[email protected]861c6c62009-04-20 16:50:56243 // It's not clear whether/when this can return NULL.
[email protected]3e44697f2009-05-22 14:37:39244 LOG(ERROR) << "Unable to create a gconf client";
[email protected]76722472012-05-24 08:26:46245 task_runner_ = NULL;
[email protected]3e44697f2009-05-22 14:37:39246 return false;
[email protected]861c6c62009-04-20 16:50:56247 }
[email protected]3e44697f2009-05-22 14:37:39248 GError* error = NULL;
[email protected]b160df32012-02-06 20:39:41249 bool added_system_proxy = false;
[email protected]3e44697f2009-05-22 14:37:39250 // We need to add the directories for which we'll be asking
[email protected]b160df32012-02-06 20:39:41251 // for notifications, and we might as well ask to preload them.
252 // These need to be removed again in ShutDown(); we are careful
253 // here to only leave client_ non-NULL if both have been added.
[email protected]3e44697f2009-05-22 14:37:39254 gconf_client_add_dir(client_, "/system/proxy",
255 GCONF_CLIENT_PRELOAD_ONELEVEL, &error);
256 if (error == NULL) {
[email protected]b160df32012-02-06 20:39:41257 added_system_proxy = true;
[email protected]3e44697f2009-05-22 14:37:39258 gconf_client_add_dir(client_, "/system/http_proxy",
259 GCONF_CLIENT_PRELOAD_ONELEVEL, &error);
260 }
261 if (error != NULL) {
262 LOG(ERROR) << "Error requesting gconf directory: " << error->message;
263 g_error_free(error);
[email protected]b160df32012-02-06 20:39:41264 if (added_system_proxy)
265 gconf_client_remove_dir(client_, "/system/proxy", NULL);
266 g_object_unref(client_);
267 client_ = NULL;
[email protected]76722472012-05-24 08:26:46268 task_runner_ = NULL;
[email protected]3e44697f2009-05-22 14:37:39269 return false;
270 }
271 return true;
272 }
273
[email protected]749bf5c2012-09-17 03:15:21274 virtual void ShutDown() OVERRIDE {
[email protected]3e44697f2009-05-22 14:37:39275 if (client_) {
[email protected]76722472012-05-24 08:26:46276 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]b160df32012-02-06 20:39:41277 // We must explicitly disable gconf notifications here, because the gconf
278 // client will be shared between all setting getters, and they do not all
279 // have the same lifetimes. (For instance, incognito sessions get their
280 // own, which is destroyed when the session ends.)
281 gconf_client_notify_remove(client_, system_http_proxy_id_);
282 gconf_client_notify_remove(client_, system_proxy_id_);
283 gconf_client_remove_dir(client_, "/system/http_proxy", NULL);
284 gconf_client_remove_dir(client_, "/system/proxy", NULL);
[email protected]3e44697f2009-05-22 14:37:39285 g_object_unref(client_);
286 client_ = NULL;
[email protected]76722472012-05-24 08:26:46287 task_runner_ = NULL;
[email protected]3e44697f2009-05-22 14:37:39288 }
289 }
290
[email protected]749bf5c2012-09-17 03:15:21291 virtual bool SetUpNotifications(
292 ProxyConfigServiceLinux::Delegate* delegate) OVERRIDE {
[email protected]3e44697f2009-05-22 14:37:39293 DCHECK(client_);
[email protected]76722472012-05-24 08:26:46294 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]3e44697f2009-05-22 14:37:39295 GError* error = NULL;
[email protected]d7395e732009-08-28 23:13:43296 notify_delegate_ = delegate;
[email protected]b160df32012-02-06 20:39:41297 // We have to keep track of the IDs returned by gconf_client_notify_add() so
298 // that we can remove them in ShutDown(). (Otherwise, notifications will be
299 // delivered to this object after it is deleted, which is bad, m'kay?)
300 system_proxy_id_ = gconf_client_notify_add(
[email protected]3e44697f2009-05-22 14:37:39301 client_, "/system/proxy",
[email protected]d7395e732009-08-28 23:13:43302 OnGConfChangeNotification, this,
[email protected]3e44697f2009-05-22 14:37:39303 NULL, &error);
304 if (error == NULL) {
[email protected]b160df32012-02-06 20:39:41305 system_http_proxy_id_ = gconf_client_notify_add(
[email protected]3e44697f2009-05-22 14:37:39306 client_, "/system/http_proxy",
[email protected]d7395e732009-08-28 23:13:43307 OnGConfChangeNotification, this,
[email protected]3e44697f2009-05-22 14:37:39308 NULL, &error);
309 }
310 if (error != NULL) {
311 LOG(ERROR) << "Error requesting gconf notifications: " << error->message;
312 g_error_free(error);
[email protected]d3066142011-05-10 02:36:20313 ShutDown();
[email protected]3e44697f2009-05-22 14:37:39314 return false;
315 }
[email protected]d3066142011-05-10 02:36:20316 // Simulate a change to avoid possibly losing updates before this point.
317 OnChangeNotification();
[email protected]3e44697f2009-05-22 14:37:39318 return true;
[email protected]861c6c62009-04-20 16:50:56319 }
320
[email protected]76722472012-05-24 08:26:46321 virtual base::SingleThreadTaskRunner* GetNotificationTaskRunner() OVERRIDE {
[email protected]90499482013-06-01 00:39:50322 return task_runner_.get();
[email protected]d7395e732009-08-28 23:13:43323 }
324
[email protected]db8ff912012-06-12 23:32:51325 virtual ProxyConfigSource GetConfigSource() OVERRIDE {
326 return PROXY_CONFIG_SOURCE_GCONF;
[email protected]d7395e732009-08-28 23:13:43327 }
328
[email protected]c4c1b482011-07-22 17:24:26329 virtual bool GetString(StringSetting key, std::string* result) OVERRIDE {
[email protected]573c0502011-05-17 22:19:50330 switch (key) {
331 case PROXY_MODE:
332 return GetStringByPath("/system/proxy/mode", result);
333 case PROXY_AUTOCONF_URL:
334 return GetStringByPath("/system/proxy/autoconfig_url", result);
335 case PROXY_HTTP_HOST:
336 return GetStringByPath("/system/http_proxy/host", result);
337 case PROXY_HTTPS_HOST:
338 return GetStringByPath("/system/proxy/secure_host", result);
339 case PROXY_FTP_HOST:
340 return GetStringByPath("/system/proxy/ftp_host", result);
341 case PROXY_SOCKS_HOST:
342 return GetStringByPath("/system/proxy/socks_host", result);
[email protected]573c0502011-05-17 22:19:50343 }
[email protected]6b5fe742011-05-20 21:46:48344 return false; // Placate compiler.
[email protected]573c0502011-05-17 22:19:50345 }
[email protected]c4c1b482011-07-22 17:24:26346 virtual bool GetBool(BoolSetting key, bool* result) OVERRIDE {
[email protected]573c0502011-05-17 22:19:50347 switch (key) {
348 case PROXY_USE_HTTP_PROXY:
349 return GetBoolByPath("/system/http_proxy/use_http_proxy", result);
350 case PROXY_USE_SAME_PROXY:
351 return GetBoolByPath("/system/http_proxy/use_same_proxy", result);
352 case PROXY_USE_AUTHENTICATION:
353 return GetBoolByPath("/system/http_proxy/use_authentication", result);
[email protected]573c0502011-05-17 22:19:50354 }
[email protected]6b5fe742011-05-20 21:46:48355 return false; // Placate compiler.
[email protected]573c0502011-05-17 22:19:50356 }
[email protected]c4c1b482011-07-22 17:24:26357 virtual bool GetInt(IntSetting key, int* result) OVERRIDE {
[email protected]573c0502011-05-17 22:19:50358 switch (key) {
359 case PROXY_HTTP_PORT:
360 return GetIntByPath("/system/http_proxy/port", result);
361 case PROXY_HTTPS_PORT:
362 return GetIntByPath("/system/proxy/secure_port", result);
363 case PROXY_FTP_PORT:
364 return GetIntByPath("/system/proxy/ftp_port", result);
365 case PROXY_SOCKS_PORT:
366 return GetIntByPath("/system/proxy/socks_port", result);
[email protected]573c0502011-05-17 22:19:50367 }
[email protected]6b5fe742011-05-20 21:46:48368 return false; // Placate compiler.
[email protected]573c0502011-05-17 22:19:50369 }
[email protected]6b5fe742011-05-20 21:46:48370 virtual bool GetStringList(StringListSetting key,
[email protected]c4c1b482011-07-22 17:24:26371 std::vector<std::string>* result) OVERRIDE {
[email protected]573c0502011-05-17 22:19:50372 switch (key) {
373 case PROXY_IGNORE_HOSTS:
374 return GetStringListByPath("/system/http_proxy/ignore_hosts", result);
[email protected]573c0502011-05-17 22:19:50375 }
[email protected]6b5fe742011-05-20 21:46:48376 return false; // Placate compiler.
[email protected]573c0502011-05-17 22:19:50377 }
378
[email protected]c4c1b482011-07-22 17:24:26379 virtual bool BypassListIsReversed() OVERRIDE {
[email protected]573c0502011-05-17 22:19:50380 // This is a KDE-specific setting.
381 return false;
382 }
383
[email protected]c4c1b482011-07-22 17:24:26384 virtual bool MatchHostsUsingSuffixMatching() OVERRIDE {
[email protected]573c0502011-05-17 22:19:50385 return false;
386 }
387
388 private:
389 bool GetStringByPath(const char* key, std::string* result) {
[email protected]3e44697f2009-05-22 14:37:39390 DCHECK(client_);
[email protected]76722472012-05-24 08:26:46391 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]861c6c62009-04-20 16:50:56392 GError* error = NULL;
393 gchar* value = gconf_client_get_string(client_, key, &error);
394 if (HandleGError(error, key))
395 return false;
396 if (!value)
397 return false;
398 *result = value;
399 g_free(value);
400 return true;
401 }
[email protected]573c0502011-05-17 22:19:50402 bool GetBoolByPath(const char* key, bool* result) {
[email protected]3e44697f2009-05-22 14:37:39403 DCHECK(client_);
[email protected]76722472012-05-24 08:26:46404 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]861c6c62009-04-20 16:50:56405 GError* error = NULL;
406 // We want to distinguish unset values from values defaulting to
407 // false. For that we need to use the type-generic
408 // gconf_client_get() rather than gconf_client_get_bool().
409 GConfValue* gconf_value = gconf_client_get(client_, key, &error);
410 if (HandleGError(error, key))
411 return false;
412 if (!gconf_value) {
413 // Unset.
414 return false;
415 }
416 if (gconf_value->type != GCONF_VALUE_BOOL) {
417 gconf_value_free(gconf_value);
418 return false;
419 }
420 gboolean bool_value = gconf_value_get_bool(gconf_value);
421 *result = static_cast<bool>(bool_value);
422 gconf_value_free(gconf_value);
423 return true;
424 }
[email protected]573c0502011-05-17 22:19:50425 bool GetIntByPath(const char* key, int* result) {
[email protected]3e44697f2009-05-22 14:37:39426 DCHECK(client_);
[email protected]76722472012-05-24 08:26:46427 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]861c6c62009-04-20 16:50:56428 GError* error = NULL;
429 int value = gconf_client_get_int(client_, key, &error);
430 if (HandleGError(error, key))
431 return false;
432 // We don't bother to distinguish an unset value because callers
433 // don't care. 0 is returned if unset.
434 *result = value;
435 return true;
436 }
[email protected]573c0502011-05-17 22:19:50437 bool GetStringListByPath(const char* key, std::vector<std::string>* result) {
[email protected]3e44697f2009-05-22 14:37:39438 DCHECK(client_);
[email protected]76722472012-05-24 08:26:46439 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]861c6c62009-04-20 16:50:56440 GError* error = NULL;
441 GSList* list = gconf_client_get_list(client_, key,
442 GCONF_VALUE_STRING, &error);
443 if (HandleGError(error, key))
444 return false;
[email protected]8c20e3d2011-05-19 21:03:57445 if (!list)
[email protected]861c6c62009-04-20 16:50:56446 return false;
[email protected]861c6c62009-04-20 16:50:56447 for (GSList *it = list; it; it = it->next) {
448 result->push_back(static_cast<char*>(it->data));
449 g_free(it->data);
450 }
451 g_slist_free(list);
452 return true;
453 }
454
[email protected]861c6c62009-04-20 16:50:56455 // Logs and frees a glib error. Returns false if there was no error
456 // (error is NULL).
457 bool HandleGError(GError* error, const char* key) {
458 if (error != NULL) {
[email protected]3e44697f2009-05-22 14:37:39459 LOG(ERROR) << "Error getting gconf value for " << key
460 << ": " << error->message;
[email protected]861c6c62009-04-20 16:50:56461 g_error_free(error);
462 return true;
463 }
464 return false;
465 }
466
[email protected]d7395e732009-08-28 23:13:43467 // This is the callback from the debounce timer.
468 void OnDebouncedNotification() {
[email protected]76722472012-05-24 08:26:46469 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]961ac942011-04-28 18:18:14470 CHECK(notify_delegate_);
[email protected]d7395e732009-08-28 23:13:43471 // Forward to a method on the proxy config service delegate object.
472 notify_delegate_->OnCheckProxyConfigSettings();
473 }
474
475 void OnChangeNotification() {
476 // We don't use Reset() because the timer may not yet be running.
477 // (In that case Stop() is a no-op.)
478 debounce_timer_.Stop();
[email protected]d323a172011-09-02 18:23:02479 debounce_timer_.Start(FROM_HERE,
[email protected]8c20e3d2011-05-19 21:03:57480 base::TimeDelta::FromMilliseconds(kDebounceTimeoutMilliseconds),
481 this, &SettingGetterImplGConf::OnDebouncedNotification);
[email protected]d7395e732009-08-28 23:13:43482 }
483
[email protected]8c20e3d2011-05-19 21:03:57484 // gconf notification callback, dispatched on the default glib main loop.
485 static void OnGConfChangeNotification(GConfClient* client, guint cnxn_id,
486 GConfEntry* entry, gpointer user_data) {
[email protected]b30a3f52010-10-16 01:05:46487 VLOG(1) << "gconf change notification for key "
488 << gconf_entry_get_key(entry);
[email protected]d7395e732009-08-28 23:13:43489 // We don't track which key has changed, just that something did change.
[email protected]573c0502011-05-17 22:19:50490 SettingGetterImplGConf* setting_getter =
491 reinterpret_cast<SettingGetterImplGConf*>(user_data);
[email protected]d7395e732009-08-28 23:13:43492 setting_getter->OnChangeNotification();
493 }
494
[email protected]861c6c62009-04-20 16:50:56495 GConfClient* client_;
[email protected]b160df32012-02-06 20:39:41496 // These ids are the values returned from gconf_client_notify_add(), which we
497 // will need in order to later call gconf_client_notify_remove().
498 guint system_proxy_id_;
499 guint system_http_proxy_id_;
500
[email protected]d7395e732009-08-28 23:13:43501 ProxyConfigServiceLinux::Delegate* notify_delegate_;
[email protected]573c0502011-05-17 22:19:50502 base::OneShotTimer<SettingGetterImplGConf> debounce_timer_;
[email protected]861c6c62009-04-20 16:50:56503
[email protected]76722472012-05-24 08:26:46504 // Task runner for the thread that we make gconf calls on. It should
[email protected]3e44697f2009-05-22 14:37:39505 // be the UI thread and all our methods should be called on this
506 // thread. Only for assertions.
[email protected]76722472012-05-24 08:26:46507 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
[email protected]3e44697f2009-05-22 14:37:39508
[email protected]573c0502011-05-17 22:19:50509 DISALLOW_COPY_AND_ASSIGN(SettingGetterImplGConf);
[email protected]d7395e732009-08-28 23:13:43510};
[email protected]6de53d42010-11-09 07:33:19511#endif // defined(USE_GCONF)
[email protected]d7395e732009-08-28 23:13:43512
[email protected]8c20e3d2011-05-19 21:03:57513#if defined(USE_GIO)
[email protected]2297bb22014-06-19 06:30:14514const char kProxyGConfSchema[] = "org.gnome.system.proxy";
515
[email protected]8c20e3d2011-05-19 21:03:57516// This setting getter uses gsettings, as used in most GNOME 3 desktops.
517class SettingGetterImplGSettings
518 : public ProxyConfigServiceLinux::SettingGetter {
519 public:
[email protected]0e14e87d2011-06-21 21:24:19520 SettingGetterImplGSettings() :
[email protected]0e14e87d2011-06-21 21:24:19521 client_(NULL),
522 http_client_(NULL),
523 https_client_(NULL),
524 ftp_client_(NULL),
525 socks_client_(NULL),
[email protected]76722472012-05-24 08:26:46526 notify_delegate_(NULL) {
[email protected]8c20e3d2011-05-19 21:03:57527 }
528
529 virtual ~SettingGetterImplGSettings() {
530 // client_ should have been released before now, from
531 // Delegate::OnDestroy(), while running on the UI thread. However
532 // on exiting the process, it may happen that
533 // Delegate::OnDestroy() task is left pending on the glib loop
534 // after the loop was quit, and pending tasks may then be deleted
535 // without being run.
536 if (client_) {
537 // gconf client was not cleaned up.
[email protected]76722472012-05-24 08:26:46538 if (task_runner_->BelongsToCurrentThread()) {
[email protected]8c20e3d2011-05-19 21:03:57539 // We are on the UI thread so we can clean it safely. This is
540 // the case at least for ui_tests running under Valgrind in
541 // bug 16076.
542 VLOG(1) << "~SettingGetterImplGSettings: releasing gsettings client";
543 ShutDown();
544 } else {
545 LOG(WARNING) << "~SettingGetterImplGSettings: leaking gsettings client";
546 client_ = NULL;
547 }
548 }
549 DCHECK(!client_);
[email protected]8c20e3d2011-05-19 21:03:57550 }
551
[email protected]4cf80f0b2011-05-20 20:30:26552 bool SchemaExists(const char* schema_name) {
[email protected]3fc24f52012-11-30 21:22:34553 const gchar* const* schemas = libgio_loader_.g_settings_list_schemas();
[email protected]4cf80f0b2011-05-20 20:30:26554 while (*schemas) {
[email protected]a099f3ae2011-08-16 21:06:58555 if (strcmp(schema_name, static_cast<const char*>(*schemas)) == 0)
[email protected]4cf80f0b2011-05-20 20:30:26556 return true;
557 schemas++;
558 }
559 return false;
560 }
561
[email protected]8c20e3d2011-05-19 21:03:57562 // LoadAndCheckVersion() must be called *before* Init()!
563 bool LoadAndCheckVersion(base::Environment* env);
564
[email protected]76722472012-05-24 08:26:46565 virtual bool Init(base::SingleThreadTaskRunner* glib_thread_task_runner,
[email protected]2da659e2013-05-23 20:51:34566 base::MessageLoopForIO* file_loop) OVERRIDE {
[email protected]76722472012-05-24 08:26:46567 DCHECK(glib_thread_task_runner->BelongsToCurrentThread());
[email protected]8c20e3d2011-05-19 21:03:57568 DCHECK(!client_);
[email protected]90499482013-06-01 00:39:50569 DCHECK(!task_runner_.get());
[email protected]4cf80f0b2011-05-20 20:30:26570
[email protected]4bbb72d2014-06-06 18:05:51571 if (!SchemaExists(kProxyGConfSchema) ||
572 !(client_ = libgio_loader_.g_settings_new(kProxyGConfSchema))) {
[email protected]8c20e3d2011-05-19 21:03:57573 // It's not clear whether/when this can return NULL.
574 LOG(ERROR) << "Unable to create a gsettings client";
575 return false;
576 }
[email protected]76722472012-05-24 08:26:46577 task_runner_ = glib_thread_task_runner;
[email protected]8c20e3d2011-05-19 21:03:57578 // We assume these all work if the above call worked.
[email protected]3fc24f52012-11-30 21:22:34579 http_client_ = libgio_loader_.g_settings_get_child(client_, "http");
580 https_client_ = libgio_loader_.g_settings_get_child(client_, "https");
581 ftp_client_ = libgio_loader_.g_settings_get_child(client_, "ftp");
582 socks_client_ = libgio_loader_.g_settings_get_child(client_, "socks");
[email protected]8c20e3d2011-05-19 21:03:57583 DCHECK(http_client_ && https_client_ && ftp_client_ && socks_client_);
584 return true;
585 }
586
[email protected]749bf5c2012-09-17 03:15:21587 virtual void ShutDown() OVERRIDE {
[email protected]8c20e3d2011-05-19 21:03:57588 if (client_) {
[email protected]76722472012-05-24 08:26:46589 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]8c20e3d2011-05-19 21:03:57590 // This also disables gsettings notifications.
591 g_object_unref(socks_client_);
592 g_object_unref(ftp_client_);
593 g_object_unref(https_client_);
594 g_object_unref(http_client_);
595 g_object_unref(client_);
596 // We only need to null client_ because it's the only one that we check.
597 client_ = NULL;
[email protected]76722472012-05-24 08:26:46598 task_runner_ = NULL;
[email protected]8c20e3d2011-05-19 21:03:57599 }
600 }
601
[email protected]749bf5c2012-09-17 03:15:21602 virtual bool SetUpNotifications(
603 ProxyConfigServiceLinux::Delegate* delegate) OVERRIDE {
[email protected]8c20e3d2011-05-19 21:03:57604 DCHECK(client_);
[email protected]76722472012-05-24 08:26:46605 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]8c20e3d2011-05-19 21:03:57606 notify_delegate_ = delegate;
607 // We could watch for the change-event signal instead of changed, but
608 // since we have to watch more than one object, we'd still have to
609 // debounce change notifications. This is conceptually simpler.
610 g_signal_connect(G_OBJECT(client_), "changed",
611 G_CALLBACK(OnGSettingsChangeNotification), this);
612 g_signal_connect(G_OBJECT(http_client_), "changed",
613 G_CALLBACK(OnGSettingsChangeNotification), this);
614 g_signal_connect(G_OBJECT(https_client_), "changed",
615 G_CALLBACK(OnGSettingsChangeNotification), this);
616 g_signal_connect(G_OBJECT(ftp_client_), "changed",
617 G_CALLBACK(OnGSettingsChangeNotification), this);
618 g_signal_connect(G_OBJECT(socks_client_), "changed",
619 G_CALLBACK(OnGSettingsChangeNotification), this);
620 // Simulate a change to avoid possibly losing updates before this point.
621 OnChangeNotification();
622 return true;
623 }
624
[email protected]76722472012-05-24 08:26:46625 virtual base::SingleThreadTaskRunner* GetNotificationTaskRunner() OVERRIDE {
[email protected]90499482013-06-01 00:39:50626 return task_runner_.get();
[email protected]8c20e3d2011-05-19 21:03:57627 }
628
[email protected]db8ff912012-06-12 23:32:51629 virtual ProxyConfigSource GetConfigSource() OVERRIDE {
630 return PROXY_CONFIG_SOURCE_GSETTINGS;
[email protected]8c20e3d2011-05-19 21:03:57631 }
632
[email protected]c4c1b482011-07-22 17:24:26633 virtual bool GetString(StringSetting key, std::string* result) OVERRIDE {
[email protected]8c20e3d2011-05-19 21:03:57634 DCHECK(client_);
635 switch (key) {
636 case PROXY_MODE:
637 return GetStringByPath(client_, "mode", result);
638 case PROXY_AUTOCONF_URL:
639 return GetStringByPath(client_, "autoconfig-url", result);
640 case PROXY_HTTP_HOST:
641 return GetStringByPath(http_client_, "host", result);
642 case PROXY_HTTPS_HOST:
643 return GetStringByPath(https_client_, "host", result);
644 case PROXY_FTP_HOST:
645 return GetStringByPath(ftp_client_, "host", result);
646 case PROXY_SOCKS_HOST:
647 return GetStringByPath(socks_client_, "host", result);
[email protected]8c20e3d2011-05-19 21:03:57648 }
[email protected]6b5fe742011-05-20 21:46:48649 return false; // Placate compiler.
[email protected]8c20e3d2011-05-19 21:03:57650 }
[email protected]c4c1b482011-07-22 17:24:26651 virtual bool GetBool(BoolSetting key, bool* result) OVERRIDE {
[email protected]8c20e3d2011-05-19 21:03:57652 DCHECK(client_);
653 switch (key) {
654 case PROXY_USE_HTTP_PROXY:
655 // Although there is an "enabled" boolean in http_client_, it is not set
656 // to true by the proxy config utility. We ignore it and return false.
657 return false;
658 case PROXY_USE_SAME_PROXY:
659 // Similarly, although there is a "use-same-proxy" boolean in client_,
660 // it is never set to false by the proxy config utility. We ignore it.
661 return false;
662 case PROXY_USE_AUTHENTICATION:
663 // There is also no way to set this in the proxy config utility, but it
664 // doesn't hurt us to get the actual setting (unlike the two above).
665 return GetBoolByPath(http_client_, "use-authentication", result);
[email protected]8c20e3d2011-05-19 21:03:57666 }
[email protected]6b5fe742011-05-20 21:46:48667 return false; // Placate compiler.
[email protected]8c20e3d2011-05-19 21:03:57668 }
[email protected]c4c1b482011-07-22 17:24:26669 virtual bool GetInt(IntSetting key, int* result) OVERRIDE {
[email protected]8c20e3d2011-05-19 21:03:57670 DCHECK(client_);
671 switch (key) {
672 case PROXY_HTTP_PORT:
673 return GetIntByPath(http_client_, "port", result);
674 case PROXY_HTTPS_PORT:
675 return GetIntByPath(https_client_, "port", result);
676 case PROXY_FTP_PORT:
677 return GetIntByPath(ftp_client_, "port", result);
678 case PROXY_SOCKS_PORT:
679 return GetIntByPath(socks_client_, "port", result);
[email protected]8c20e3d2011-05-19 21:03:57680 }
[email protected]6b5fe742011-05-20 21:46:48681 return false; // Placate compiler.
[email protected]8c20e3d2011-05-19 21:03:57682 }
[email protected]6b5fe742011-05-20 21:46:48683 virtual bool GetStringList(StringListSetting key,
[email protected]c4c1b482011-07-22 17:24:26684 std::vector<std::string>* result) OVERRIDE {
[email protected]8c20e3d2011-05-19 21:03:57685 DCHECK(client_);
686 switch (key) {
687 case PROXY_IGNORE_HOSTS:
688 return GetStringListByPath(client_, "ignore-hosts", result);
[email protected]8c20e3d2011-05-19 21:03:57689 }
[email protected]6b5fe742011-05-20 21:46:48690 return false; // Placate compiler.
[email protected]8c20e3d2011-05-19 21:03:57691 }
692
[email protected]c4c1b482011-07-22 17:24:26693 virtual bool BypassListIsReversed() OVERRIDE {
[email protected]8c20e3d2011-05-19 21:03:57694 // This is a KDE-specific setting.
695 return false;
696 }
697
[email protected]c4c1b482011-07-22 17:24:26698 virtual bool MatchHostsUsingSuffixMatching() OVERRIDE {
[email protected]8c20e3d2011-05-19 21:03:57699 return false;
700 }
701
702 private:
[email protected]8c20e3d2011-05-19 21:03:57703 bool GetStringByPath(GSettings* client, const char* key,
704 std::string* result) {
[email protected]76722472012-05-24 08:26:46705 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]3fc24f52012-11-30 21:22:34706 gchar* value = libgio_loader_.g_settings_get_string(client, key);
[email protected]8c20e3d2011-05-19 21:03:57707 if (!value)
708 return false;
709 *result = value;
710 g_free(value);
711 return true;
712 }
713 bool GetBoolByPath(GSettings* client, const char* key, bool* result) {
[email protected]76722472012-05-24 08:26:46714 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]3fc24f52012-11-30 21:22:34715 *result = static_cast<bool>(
716 libgio_loader_.g_settings_get_boolean(client, key));
[email protected]8c20e3d2011-05-19 21:03:57717 return true;
718 }
719 bool GetIntByPath(GSettings* client, const char* key, int* result) {
[email protected]76722472012-05-24 08:26:46720 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]3fc24f52012-11-30 21:22:34721 *result = libgio_loader_.g_settings_get_int(client, key);
[email protected]8c20e3d2011-05-19 21:03:57722 return true;
723 }
724 bool GetStringListByPath(GSettings* client, const char* key,
725 std::vector<std::string>* result) {
[email protected]76722472012-05-24 08:26:46726 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]3fc24f52012-11-30 21:22:34727 gchar** list = libgio_loader_.g_settings_get_strv(client, key);
[email protected]8c20e3d2011-05-19 21:03:57728 if (!list)
729 return false;
730 for (size_t i = 0; list[i]; ++i) {
731 result->push_back(static_cast<char*>(list[i]));
732 g_free(list[i]);
733 }
734 g_free(list);
735 return true;
736 }
737
738 // This is the callback from the debounce timer.
739 void OnDebouncedNotification() {
[email protected]76722472012-05-24 08:26:46740 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]8c20e3d2011-05-19 21:03:57741 CHECK(notify_delegate_);
742 // Forward to a method on the proxy config service delegate object.
743 notify_delegate_->OnCheckProxyConfigSettings();
744 }
745
746 void OnChangeNotification() {
747 // We don't use Reset() because the timer may not yet be running.
748 // (In that case Stop() is a no-op.)
749 debounce_timer_.Stop();
[email protected]d323a172011-09-02 18:23:02750 debounce_timer_.Start(FROM_HERE,
[email protected]8c20e3d2011-05-19 21:03:57751 base::TimeDelta::FromMilliseconds(kDebounceTimeoutMilliseconds),
752 this, &SettingGetterImplGSettings::OnDebouncedNotification);
753 }
754
755 // gsettings notification callback, dispatched on the default glib main loop.
756 static void OnGSettingsChangeNotification(GSettings* client, gchar* key,
757 gpointer user_data) {
758 VLOG(1) << "gsettings change notification for key " << key;
759 // We don't track which key has changed, just that something did change.
760 SettingGetterImplGSettings* setting_getter =
761 reinterpret_cast<SettingGetterImplGSettings*>(user_data);
762 setting_getter->OnChangeNotification();
763 }
764
765 GSettings* client_;
766 GSettings* http_client_;
767 GSettings* https_client_;
768 GSettings* ftp_client_;
769 GSettings* socks_client_;
770 ProxyConfigServiceLinux::Delegate* notify_delegate_;
771 base::OneShotTimer<SettingGetterImplGSettings> debounce_timer_;
772
[email protected]76722472012-05-24 08:26:46773 // Task runner for the thread that we make gsettings calls on. It should
[email protected]8c20e3d2011-05-19 21:03:57774 // be the UI thread and all our methods should be called on this
775 // thread. Only for assertions.
[email protected]76722472012-05-24 08:26:46776 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
[email protected]8c20e3d2011-05-19 21:03:57777
[email protected]3fc24f52012-11-30 21:22:34778 LibGioLoader libgio_loader_;
779
[email protected]8c20e3d2011-05-19 21:03:57780 DISALLOW_COPY_AND_ASSIGN(SettingGetterImplGSettings);
781};
782
783bool SettingGetterImplGSettings::LoadAndCheckVersion(
784 base::Environment* env) {
785 // LoadAndCheckVersion() must be called *before* Init()!
786 DCHECK(!client_);
787
788 // The APIs to query gsettings were introduced after the minimum glib
789 // version we target, so we can't link directly against them. We load them
790 // dynamically at runtime, and if they don't exist, return false here. (We
791 // support linking directly via gyp flags though.) Additionally, even when
792 // they are present, we do two additional checks to make sure we should use
793 // them and not gconf. First, we attempt to load the schema for proxy
794 // settings. Second, we check for the program that was used in older
795 // versions of GNOME to configure proxy settings, and return false if it
796 // exists. Some distributions (e.g. Ubuntu 11.04) have the API and schema
797 // but don't use gsettings for proxy settings, but they do have the old
798 // binary, so we detect these systems that way.
799
[email protected]ae82cea2012-12-06 22:52:10800 {
801 // TODO(phajdan.jr): Redesign the code to load library on different thread.
802 base::ThreadRestrictions::ScopedAllowIO allow_io;
803
804 // Try also without .0 at the end; on some systems this may be required.
805 if (!libgio_loader_.Load("libgio-2.0.so.0") &&
806 !libgio_loader_.Load("libgio-2.0.so")) {
807 VLOG(1) << "Cannot load gio library. Will fall back to gconf.";
808 return false;
809 }
[email protected]8c20e3d2011-05-19 21:03:57810 }
[email protected]8c20e3d2011-05-19 21:03:57811
[email protected]4bbb72d2014-06-06 18:05:51812 GSettings* client = NULL;
813 if (SchemaExists(kProxyGConfSchema)) {
814 ANNOTATE_SCOPED_MEMORY_LEAK; // http://crbug.com/380782
815 client = libgio_loader_.g_settings_new(kProxyGConfSchema);
816 }
817 if (!client) {
[email protected]8c20e3d2011-05-19 21:03:57818 VLOG(1) << "Cannot create gsettings client. Will fall back to gconf.";
819 return false;
820 }
821 g_object_unref(client);
822
823 std::string path;
824 if (!env->GetVar("PATH", &path)) {
825 LOG(ERROR) << "No $PATH variable. Assuming no gnome-network-properties.";
826 } else {
827 // Yes, we're on the UI thread. Yes, we're accessing the file system.
828 // Sadly, we don't have much choice. We need the proxy settings and we
829 // need them now, and to figure out where to get them, we have to check
830 // for this binary. See http://crbug.com/69057 for additional details.
831 base::ThreadRestrictions::ScopedAllowIO allow_io;
832 std::vector<std::string> paths;
833 Tokenize(path, ":", &paths);
834 for (size_t i = 0; i < paths.size(); ++i) {
[email protected]6cdfd7f2013-02-08 20:40:15835 base::FilePath file(paths[i]);
[email protected]7567484142013-07-11 17:36:07836 if (base::PathExists(file.Append("gnome-network-properties"))) {
[email protected]8c20e3d2011-05-19 21:03:57837 VLOG(1) << "Found gnome-network-properties. Will fall back to gconf.";
838 return false;
839 }
840 }
841 }
842
843 VLOG(1) << "All gsettings tests OK. Will get proxy config from gsettings.";
844 return true;
845}
846#endif // defined(USE_GIO)
847
[email protected]d7395e732009-08-28 23:13:43848// This is the KDE version that reads kioslaverc and simulates gconf.
849// Doing this allows the main Delegate code, as well as the unit tests
850// for it, to stay the same - and the settings map fairly well besides.
[email protected]573c0502011-05-17 22:19:50851class SettingGetterImplKDE : public ProxyConfigServiceLinux::SettingGetter,
852 public base::MessagePumpLibevent::Watcher {
[email protected]d7395e732009-08-28 23:13:43853 public:
[email protected]573c0502011-05-17 22:19:50854 explicit SettingGetterImplKDE(base::Environment* env_var_getter)
[email protected]d7395e732009-08-28 23:13:43855 : inotify_fd_(-1), notify_delegate_(NULL), indirect_manual_(false),
[email protected]a48bf4a2010-06-14 18:24:53856 auto_no_pac_(false), reversed_bypass_list_(false),
[email protected]f18fde22010-05-18 23:49:54857 env_var_getter_(env_var_getter), file_loop_(NULL) {
[email protected]9a8c4022011-01-25 14:25:33858 // This has to be called on the UI thread (http://crbug.com/69057).
859 base::ThreadRestrictions::ScopedAllowIO allow_io;
860
[email protected]f18fde22010-05-18 23:49:54861 // Derive the location of the kde config dir from the environment.
[email protected]92d2dc82010-04-08 17:49:59862 std::string home;
[email protected]3ba7e082010-08-07 02:57:59863 if (env_var_getter->GetVar("KDEHOME", &home) && !home.empty()) {
[email protected]2e8cfe22010-06-12 00:26:24864 // $KDEHOME is set. Use it unconditionally.
[email protected]6cdfd7f2013-02-08 20:40:15865 kde_config_dir_ = KDEHomeToConfigPath(base::FilePath(home));
[email protected]92d2dc82010-04-08 17:49:59866 } else {
[email protected]2e8cfe22010-06-12 00:26:24867 // $KDEHOME is unset. Try to figure out what to use. This seems to be
[email protected]92d2dc82010-04-08 17:49:59868 // the common case on most distributions.
[email protected]3ba7e082010-08-07 02:57:59869 if (!env_var_getter->GetVar(base::env_vars::kHome, &home))
[email protected]d7395e732009-08-28 23:13:43870 // User has no $HOME? Give up. Later we'll report the failure.
871 return;
[email protected]6b0349ef2010-10-16 04:56:06872 if (base::nix::GetDesktopEnvironment(env_var_getter) ==
873 base::nix::DESKTOP_ENVIRONMENT_KDE3) {
[email protected]92d2dc82010-04-08 17:49:59874 // KDE3 always uses .kde for its configuration.
[email protected]6cdfd7f2013-02-08 20:40:15875 base::FilePath kde_path = base::FilePath(home).Append(".kde");
[email protected]92d2dc82010-04-08 17:49:59876 kde_config_dir_ = KDEHomeToConfigPath(kde_path);
877 } else {
878 // Some distributions patch KDE4 to use .kde4 instead of .kde, so that
[email protected]fad9c8a52010-06-10 22:30:53879 // both can be installed side-by-side. Sadly they don't all do this, and
880 // they don't always do this: some distributions have started switching
881 // back as well. So if there is a .kde4 directory, check the timestamps
882 // of the config directories within and use the newest one.
[email protected]92d2dc82010-04-08 17:49:59883 // Note that we should currently be running in the UI thread, because in
884 // the gconf version, that is the only thread that can access the proxy
885 // settings (a gconf restriction). As noted below, the initial read of
886 // the proxy settings will be done in this thread anyway, so we check
887 // for .kde4 here in this thread as well.
[email protected]6cdfd7f2013-02-08 20:40:15888 base::FilePath kde3_path = base::FilePath(home).Append(".kde");
889 base::FilePath kde3_config = KDEHomeToConfigPath(kde3_path);
890 base::FilePath kde4_path = base::FilePath(home).Append(".kde4");
891 base::FilePath kde4_config = KDEHomeToConfigPath(kde4_path);
[email protected]fad9c8a52010-06-10 22:30:53892 bool use_kde4 = false;
[email protected]dcd16612013-07-15 20:18:09893 if (base::DirectoryExists(kde4_path)) {
[email protected]54124ed02014-01-07 10:06:58894 base::File::Info kde3_info;
895 base::File::Info kde4_info;
[email protected]9eae4e62013-12-04 20:56:49896 if (base::GetFileInfo(kde4_config, &kde4_info)) {
897 if (base::GetFileInfo(kde3_config, &kde3_info)) {
[email protected]fad9c8a52010-06-10 22:30:53898 use_kde4 = kde4_info.last_modified >= kde3_info.last_modified;
899 } else {
900 use_kde4 = true;
901 }
902 }
903 }
904 if (use_kde4) {
[email protected]92d2dc82010-04-08 17:49:59905 kde_config_dir_ = KDEHomeToConfigPath(kde4_path);
906 } else {
[email protected]fad9c8a52010-06-10 22:30:53907 kde_config_dir_ = KDEHomeToConfigPath(kde3_path);
[email protected]92d2dc82010-04-08 17:49:59908 }
909 }
[email protected]d7395e732009-08-28 23:13:43910 }
[email protected]d7395e732009-08-28 23:13:43911 }
912
[email protected]573c0502011-05-17 22:19:50913 virtual ~SettingGetterImplKDE() {
[email protected]d7395e732009-08-28 23:13:43914 // inotify_fd_ should have been closed before now, from
915 // Delegate::OnDestroy(), while running on the file thread. However
916 // on exiting the process, it may happen that Delegate::OnDestroy()
917 // task is left pending on the file loop after the loop was quit,
918 // and pending tasks may then be deleted without being run.
919 // Here in the KDE version, we can safely close the file descriptor
920 // anyway. (Not that it really matters; the process is exiting.)
921 if (inotify_fd_ >= 0)
[email protected]d3066142011-05-10 02:36:20922 ShutDown();
[email protected]d7395e732009-08-28 23:13:43923 DCHECK(inotify_fd_ < 0);
924 }
925
[email protected]76722472012-05-24 08:26:46926 virtual bool Init(base::SingleThreadTaskRunner* glib_thread_task_runner,
[email protected]2da659e2013-05-23 20:51:34927 base::MessageLoopForIO* file_loop) OVERRIDE {
[email protected]9a8c4022011-01-25 14:25:33928 // This has to be called on the UI thread (http://crbug.com/69057).
929 base::ThreadRestrictions::ScopedAllowIO allow_io;
[email protected]d7395e732009-08-28 23:13:43930 DCHECK(inotify_fd_ < 0);
931 inotify_fd_ = inotify_init();
932 if (inotify_fd_ < 0) {
[email protected]57b765672009-10-13 18:27:40933 PLOG(ERROR) << "inotify_init failed";
[email protected]d7395e732009-08-28 23:13:43934 return false;
935 }
936 int flags = fcntl(inotify_fd_, F_GETFL);
937 if (fcntl(inotify_fd_, F_SETFL, flags | O_NONBLOCK) < 0) {
[email protected]57b765672009-10-13 18:27:40938 PLOG(ERROR) << "fcntl failed";
[email protected]d7395e732009-08-28 23:13:43939 close(inotify_fd_);
940 inotify_fd_ = -1;
941 return false;
942 }
943 file_loop_ = file_loop;
944 // The initial read is done on the current thread, not |file_loop_|,
[email protected]d3066142011-05-10 02:36:20945 // since we will need to have it for SetUpAndFetchInitialConfig().
[email protected]d7395e732009-08-28 23:13:43946 UpdateCachedSettings();
947 return true;
948 }
949
[email protected]749bf5c2012-09-17 03:15:21950 virtual void ShutDown() OVERRIDE {
[email protected]d7395e732009-08-28 23:13:43951 if (inotify_fd_ >= 0) {
952 ResetCachedSettings();
953 inotify_watcher_.StopWatchingFileDescriptor();
954 close(inotify_fd_);
955 inotify_fd_ = -1;
956 }
957 }
958
[email protected]749bf5c2012-09-17 03:15:21959 virtual bool SetUpNotifications(
960 ProxyConfigServiceLinux::Delegate* delegate) OVERRIDE {
[email protected]d7395e732009-08-28 23:13:43961 DCHECK(inotify_fd_ >= 0);
[email protected]2da659e2013-05-23 20:51:34962 DCHECK(base::MessageLoop::current() == file_loop_);
[email protected]d7395e732009-08-28 23:13:43963 // We can't just watch the kioslaverc file directly, since KDE will write
964 // a new copy of it and then rename it whenever settings are changed and
965 // inotify watches inodes (so we'll be watching the old deleted file after
966 // the first change, and it will never change again). So, we watch the
967 // directory instead. We then act only on changes to the kioslaverc entry.
968 if (inotify_add_watch(inotify_fd_, kde_config_dir_.value().c_str(),
969 IN_MODIFY | IN_MOVED_TO) < 0)
970 return false;
971 notify_delegate_ = delegate;
[email protected]2da659e2013-05-23 20:51:34972 if (!file_loop_->WatchFileDescriptor(inotify_fd_,
973 true,
974 base::MessageLoopForIO::WATCH_READ,
975 &inotify_watcher_,
976 this))
[email protected]d3066142011-05-10 02:36:20977 return false;
978 // Simulate a change to avoid possibly losing updates before this point.
979 OnChangeNotification();
980 return true;
[email protected]d7395e732009-08-28 23:13:43981 }
982
[email protected]76722472012-05-24 08:26:46983 virtual base::SingleThreadTaskRunner* GetNotificationTaskRunner() OVERRIDE {
[email protected]198b5902013-06-27 10:36:11984 return file_loop_ ? file_loop_->message_loop_proxy().get() : NULL;
[email protected]d7395e732009-08-28 23:13:43985 }
986
[email protected]b160df32012-02-06 20:39:41987 // Implement base::MessagePumpLibevent::Watcher.
[email protected]749bf5c2012-09-17 03:15:21988 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE {
[email protected]d2e6d592012-02-03 21:49:04989 DCHECK_EQ(fd, inotify_fd_);
[email protected]2da659e2013-05-23 20:51:34990 DCHECK(base::MessageLoop::current() == file_loop_);
[email protected]d7395e732009-08-28 23:13:43991 OnChangeNotification();
992 }
[email protected]749bf5c2012-09-17 03:15:21993 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE {
[email protected]d7395e732009-08-28 23:13:43994 NOTREACHED();
995 }
996
[email protected]db8ff912012-06-12 23:32:51997 virtual ProxyConfigSource GetConfigSource() OVERRIDE {
998 return PROXY_CONFIG_SOURCE_KDE;
[email protected]d7395e732009-08-28 23:13:43999 }
1000
[email protected]c4c1b482011-07-22 17:24:261001 virtual bool GetString(StringSetting key, std::string* result) OVERRIDE {
[email protected]d7395e732009-08-28 23:13:431002 string_map_type::iterator it = string_table_.find(key);
1003 if (it == string_table_.end())
1004 return false;
1005 *result = it->second;
1006 return true;
1007 }
[email protected]c4c1b482011-07-22 17:24:261008 virtual bool GetBool(BoolSetting key, bool* result) OVERRIDE {
[email protected]d7395e732009-08-28 23:13:431009 // We don't ever have any booleans.
1010 return false;
1011 }
[email protected]c4c1b482011-07-22 17:24:261012 virtual bool GetInt(IntSetting key, int* result) OVERRIDE {
[email protected]d7395e732009-08-28 23:13:431013 // We don't ever have any integers. (See AddProxy() below about ports.)
1014 return false;
1015 }
[email protected]6b5fe742011-05-20 21:46:481016 virtual bool GetStringList(StringListSetting key,
[email protected]c4c1b482011-07-22 17:24:261017 std::vector<std::string>* result) OVERRIDE {
[email protected]d7395e732009-08-28 23:13:431018 strings_map_type::iterator it = strings_table_.find(key);
1019 if (it == strings_table_.end())
1020 return false;
1021 *result = it->second;
1022 return true;
1023 }
1024
[email protected]c4c1b482011-07-22 17:24:261025 virtual bool BypassListIsReversed() OVERRIDE {
[email protected]a48bf4a2010-06-14 18:24:531026 return reversed_bypass_list_;
1027 }
1028
[email protected]c4c1b482011-07-22 17:24:261029 virtual bool MatchHostsUsingSuffixMatching() OVERRIDE {
[email protected]1a597192010-07-09 16:58:381030 return true;
1031 }
1032
[email protected]d7395e732009-08-28 23:13:431033 private:
1034 void ResetCachedSettings() {
1035 string_table_.clear();
1036 strings_table_.clear();
1037 indirect_manual_ = false;
1038 auto_no_pac_ = false;
[email protected]a48bf4a2010-06-14 18:24:531039 reversed_bypass_list_ = false;
[email protected]d7395e732009-08-28 23:13:431040 }
1041
[email protected]6cdfd7f2013-02-08 20:40:151042 base::FilePath KDEHomeToConfigPath(const base::FilePath& kde_home) {
[email protected]92d2dc82010-04-08 17:49:591043 return kde_home.Append("share").Append("config");
1044 }
1045
[email protected]6b5fe742011-05-20 21:46:481046 void AddProxy(StringSetting host_key, const std::string& value) {
[email protected]d7395e732009-08-28 23:13:431047 if (value.empty() || value.substr(0, 3) == "//:")
1048 // No proxy.
1049 return;
[email protected]4b90c202012-04-24 23:27:551050 size_t space = value.find(' ');
1051 if (space != std::string::npos) {
1052 // Newer versions of KDE use a space rather than a colon to separate the
1053 // port number from the hostname. If we find this, we need to convert it.
1054 std::string fixed = value;
1055 fixed[space] = ':';
1056 string_table_[host_key] = fixed;
1057 } else {
1058 // We don't need to parse the port number out; GetProxyFromSettings()
1059 // would only append it right back again. So we just leave the port
1060 // number right in the host string.
1061 string_table_[host_key] = value;
1062 }
[email protected]d7395e732009-08-28 23:13:431063 }
1064
[email protected]6b5fe742011-05-20 21:46:481065 void AddHostList(StringListSetting key, const std::string& value) {
[email protected]f18fde22010-05-18 23:49:541066 std::vector<std::string> tokens;
[email protected]f4ebe772013-02-02 00:21:391067 base::StringTokenizer tk(value, ", ");
[email protected]f18fde22010-05-18 23:49:541068 while (tk.GetNext()) {
1069 std::string token = tk.token();
1070 if (!token.empty())
1071 tokens.push_back(token);
1072 }
1073 strings_table_[key] = tokens;
1074 }
1075
[email protected]9a3d8d42009-09-03 17:01:461076 void AddKDESetting(const std::string& key, const std::string& value) {
[email protected]d7395e732009-08-28 23:13:431077 if (key == "ProxyType") {
1078 const char* mode = "none";
1079 indirect_manual_ = false;
1080 auto_no_pac_ = false;
[email protected]e83326f2010-07-31 17:29:251081 int int_value;
1082 base::StringToInt(value, &int_value);
1083 switch (int_value) {
[email protected]d7395e732009-08-28 23:13:431084 case 0: // No proxy, or maybe kioslaverc syntax error.
1085 break;
1086 case 1: // Manual configuration.
1087 mode = "manual";
1088 break;
1089 case 2: // PAC URL.
1090 mode = "auto";
1091 break;
1092 case 3: // WPAD.
1093 mode = "auto";
1094 auto_no_pac_ = true;
1095 break;
1096 case 4: // Indirect manual via environment variables.
1097 mode = "manual";
1098 indirect_manual_ = true;
1099 break;
1100 }
[email protected]573c0502011-05-17 22:19:501101 string_table_[PROXY_MODE] = mode;
[email protected]d7395e732009-08-28 23:13:431102 } else if (key == "Proxy Config Script") {
[email protected]573c0502011-05-17 22:19:501103 string_table_[PROXY_AUTOCONF_URL] = value;
[email protected]d7395e732009-08-28 23:13:431104 } else if (key == "httpProxy") {
[email protected]573c0502011-05-17 22:19:501105 AddProxy(PROXY_HTTP_HOST, value);
[email protected]d7395e732009-08-28 23:13:431106 } else if (key == "httpsProxy") {
[email protected]573c0502011-05-17 22:19:501107 AddProxy(PROXY_HTTPS_HOST, value);
[email protected]d7395e732009-08-28 23:13:431108 } else if (key == "ftpProxy") {
[email protected]573c0502011-05-17 22:19:501109 AddProxy(PROXY_FTP_HOST, value);
[email protected]bfeb7232012-06-08 00:58:371110 } else if (key == "socksProxy") {
1111 // Older versions of KDE configure SOCKS in a weird way involving
1112 // LD_PRELOAD and a library that intercepts network calls to SOCKSify
1113 // them. We don't support it. KDE 4.8 added a proper SOCKS setting.
1114 AddProxy(PROXY_SOCKS_HOST, value);
[email protected]d7395e732009-08-28 23:13:431115 } else if (key == "ReversedException") {
1116 // We count "true" or any nonzero number as true, otherwise false.
1117 // Note that if the value is not actually numeric StringToInt()
1118 // will return 0, which we count as false.
[email protected]e83326f2010-07-31 17:29:251119 int int_value;
1120 base::StringToInt(value, &int_value);
1121 reversed_bypass_list_ = (value == "true" || int_value);
[email protected]d7395e732009-08-28 23:13:431122 } else if (key == "NoProxyFor") {
[email protected]573c0502011-05-17 22:19:501123 AddHostList(PROXY_IGNORE_HOSTS, value);
[email protected]d7395e732009-08-28 23:13:431124 } else if (key == "AuthMode") {
1125 // Check for authentication, just so we can warn.
[email protected]e83326f2010-07-31 17:29:251126 int mode;
1127 base::StringToInt(value, &mode);
[email protected]d7395e732009-08-28 23:13:431128 if (mode) {
1129 // ProxyConfig does not support authentication parameters, but
1130 // Chrome will prompt for the password later. So we ignore this.
1131 LOG(WARNING) <<
1132 "Proxy authentication parameters ignored, see bug 16709";
1133 }
1134 }
1135 }
1136
[email protected]6b5fe742011-05-20 21:46:481137 void ResolveIndirect(StringSetting key) {
[email protected]d7395e732009-08-28 23:13:431138 string_map_type::iterator it = string_table_.find(key);
1139 if (it != string_table_.end()) {
[email protected]f18fde22010-05-18 23:49:541140 std::string value;
[email protected]3ba7e082010-08-07 02:57:591141 if (env_var_getter_->GetVar(it->second.c_str(), &value))
[email protected]d7395e732009-08-28 23:13:431142 it->second = value;
[email protected]8425adc02010-04-18 17:45:311143 else
1144 string_table_.erase(it);
[email protected]d7395e732009-08-28 23:13:431145 }
1146 }
1147
[email protected]6b5fe742011-05-20 21:46:481148 void ResolveIndirectList(StringListSetting key) {
[email protected]f18fde22010-05-18 23:49:541149 strings_map_type::iterator it = strings_table_.find(key);
1150 if (it != strings_table_.end()) {
1151 std::string value;
1152 if (!it->second.empty() &&
[email protected]3ba7e082010-08-07 02:57:591153 env_var_getter_->GetVar(it->second[0].c_str(), &value))
[email protected]f18fde22010-05-18 23:49:541154 AddHostList(key, value);
1155 else
1156 strings_table_.erase(it);
1157 }
1158 }
1159
[email protected]d7395e732009-08-28 23:13:431160 // The settings in kioslaverc could occur in any order, but some affect
1161 // others. Rather than read the whole file in and then query them in an
1162 // order that allows us to handle that, we read the settings in whatever
1163 // order they occur and do any necessary tweaking after we finish.
1164 void ResolveModeEffects() {
1165 if (indirect_manual_) {
[email protected]573c0502011-05-17 22:19:501166 ResolveIndirect(PROXY_HTTP_HOST);
1167 ResolveIndirect(PROXY_HTTPS_HOST);
1168 ResolveIndirect(PROXY_FTP_HOST);
1169 ResolveIndirectList(PROXY_IGNORE_HOSTS);
[email protected]d7395e732009-08-28 23:13:431170 }
1171 if (auto_no_pac_) {
1172 // Remove the PAC URL; we're not supposed to use it.
[email protected]573c0502011-05-17 22:19:501173 string_table_.erase(PROXY_AUTOCONF_URL);
[email protected]d7395e732009-08-28 23:13:431174 }
[email protected]d7395e732009-08-28 23:13:431175 }
1176
1177 // Reads kioslaverc one line at a time and calls AddKDESetting() to add
1178 // each relevant name-value pair to the appropriate value table.
1179 void UpdateCachedSettings() {
[email protected]6cdfd7f2013-02-08 20:40:151180 base::FilePath kioslaverc = kde_config_dir_.Append("kioslaverc");
[email protected]b9b4a572014-03-17 23:11:121181 base::ScopedFILE input(base::OpenFile(kioslaverc, "r"));
[email protected]d7395e732009-08-28 23:13:431182 if (!input.get())
1183 return;
1184 ResetCachedSettings();
1185 bool in_proxy_settings = false;
1186 bool line_too_long = false;
[email protected]9a3d8d42009-09-03 17:01:461187 char line[BUFFER_SIZE];
1188 // fgets() will return NULL on EOF or error.
[email protected]d7395e732009-08-28 23:13:431189 while (fgets(line, sizeof(line), input.get())) {
1190 // fgets() guarantees the line will be properly terminated.
1191 size_t length = strlen(line);
1192 if (!length)
1193 continue;
1194 // This should be true even with CRLF endings.
1195 if (line[length - 1] != '\n') {
1196 line_too_long = true;
1197 continue;
1198 }
1199 if (line_too_long) {
1200 // The previous line had no line ending, but this done does. This is
1201 // the end of the line that was too long, so warn here and skip it.
1202 LOG(WARNING) << "skipped very long line in " << kioslaverc.value();
1203 line_too_long = false;
1204 continue;
1205 }
1206 // Remove the LF at the end, and the CR if there is one.
1207 line[--length] = '\0';
1208 if (length && line[length - 1] == '\r')
1209 line[--length] = '\0';
1210 // Now parse the line.
1211 if (line[0] == '[') {
1212 // Switching sections. All we care about is whether this is
1213 // the (a?) proxy settings section, for both KDE3 and KDE4.
1214 in_proxy_settings = !strncmp(line, "[Proxy Settings]", 16);
1215 } else if (in_proxy_settings) {
1216 // A regular line, in the (a?) proxy settings section.
[email protected]9a3d8d42009-09-03 17:01:461217 char* split = strchr(line, '=');
1218 // Skip this line if it does not contain an = sign.
1219 if (!split)
[email protected]d7395e732009-08-28 23:13:431220 continue;
[email protected]9a3d8d42009-09-03 17:01:461221 // Split the line on the = and advance |split|.
1222 *(split++) = 0;
1223 std::string key = line;
1224 std::string value = split;
[email protected]8af69c6c2014-03-03 19:05:311225 base::TrimWhitespaceASCII(key, base::TRIM_ALL, &key);
1226 base::TrimWhitespaceASCII(value, base::TRIM_ALL, &value);
[email protected]9a3d8d42009-09-03 17:01:461227 // Skip this line if the key name is empty.
1228 if (key.empty())
[email protected]d7395e732009-08-28 23:13:431229 continue;
1230 // Is the value name localized?
[email protected]9a3d8d42009-09-03 17:01:461231 if (key[key.length() - 1] == ']') {
1232 // Find the matching bracket.
1233 length = key.rfind('[');
1234 // Skip this line if the localization indicator is malformed.
1235 if (length == std::string::npos)
[email protected]d7395e732009-08-28 23:13:431236 continue;
1237 // Trim the localization indicator off.
[email protected]9a3d8d42009-09-03 17:01:461238 key.resize(length);
1239 // Remove any resulting trailing whitespace.
[email protected]8af69c6c2014-03-03 19:05:311240 base::TrimWhitespaceASCII(key, base::TRIM_TRAILING, &key);
[email protected]9a3d8d42009-09-03 17:01:461241 // Skip this line if the key name is now empty.
1242 if (key.empty())
1243 continue;
[email protected]d7395e732009-08-28 23:13:431244 }
[email protected]d7395e732009-08-28 23:13:431245 // Now fill in the tables.
[email protected]9a3d8d42009-09-03 17:01:461246 AddKDESetting(key, value);
[email protected]d7395e732009-08-28 23:13:431247 }
1248 }
1249 if (ferror(input.get()))
1250 LOG(ERROR) << "error reading " << kioslaverc.value();
1251 ResolveModeEffects();
1252 }
1253
1254 // This is the callback from the debounce timer.
1255 void OnDebouncedNotification() {
[email protected]2da659e2013-05-23 20:51:341256 DCHECK(base::MessageLoop::current() == file_loop_);
[email protected]b30a3f52010-10-16 01:05:461257 VLOG(1) << "inotify change notification for kioslaverc";
[email protected]d7395e732009-08-28 23:13:431258 UpdateCachedSettings();
[email protected]961ac942011-04-28 18:18:141259 CHECK(notify_delegate_);
[email protected]d7395e732009-08-28 23:13:431260 // Forward to a method on the proxy config service delegate object.
1261 notify_delegate_->OnCheckProxyConfigSettings();
1262 }
1263
1264 // Called by OnFileCanReadWithoutBlocking() on the file thread. Reads
1265 // from the inotify file descriptor and starts up a debounce timer if
1266 // an event for kioslaverc is seen.
1267 void OnChangeNotification() {
[email protected]d2e6d592012-02-03 21:49:041268 DCHECK_GE(inotify_fd_, 0);
[email protected]2da659e2013-05-23 20:51:341269 DCHECK(base::MessageLoop::current() == file_loop_);
[email protected]d7395e732009-08-28 23:13:431270 char event_buf[(sizeof(inotify_event) + NAME_MAX + 1) * 4];
1271 bool kioslaverc_touched = false;
1272 ssize_t r;
1273 while ((r = read(inotify_fd_, event_buf, sizeof(event_buf))) > 0) {
1274 // inotify returns variable-length structures, which is why we have
1275 // this strange-looking loop instead of iterating through an array.
1276 char* event_ptr = event_buf;
1277 while (event_ptr < event_buf + r) {
1278 inotify_event* event = reinterpret_cast<inotify_event*>(event_ptr);
1279 // The kernel always feeds us whole events.
[email protected]b1f031dd2010-03-02 23:19:331280 CHECK_LE(event_ptr + sizeof(inotify_event), event_buf + r);
1281 CHECK_LE(event->name + event->len, event_buf + r);
[email protected]d7395e732009-08-28 23:13:431282 if (!strcmp(event->name, "kioslaverc"))
1283 kioslaverc_touched = true;
1284 // Advance the pointer just past the end of the filename.
1285 event_ptr = event->name + event->len;
1286 }
1287 // We keep reading even if |kioslaverc_touched| is true to drain the
1288 // inotify event queue.
1289 }
1290 if (!r)
1291 // Instead of returning -1 and setting errno to EINVAL if there is not
1292 // enough buffer space, older kernels (< 2.6.21) return 0. Simulate the
1293 // new behavior (EINVAL) so we can reuse the code below.
1294 errno = EINVAL;
1295 if (errno != EAGAIN) {
[email protected]57b765672009-10-13 18:27:401296 PLOG(WARNING) << "error reading inotify file descriptor";
[email protected]d7395e732009-08-28 23:13:431297 if (errno == EINVAL) {
1298 // Our buffer is not large enough to read the next event. This should
1299 // not happen (because its size is calculated to always be sufficiently
1300 // large), but if it does we'd warn continuously since |inotify_fd_|
1301 // would be forever ready to read. Close it and stop watching instead.
1302 LOG(ERROR) << "inotify failure; no longer watching kioslaverc!";
1303 inotify_watcher_.StopWatchingFileDescriptor();
1304 close(inotify_fd_);
1305 inotify_fd_ = -1;
1306 }
1307 }
1308 if (kioslaverc_touched) {
1309 // We don't use Reset() because the timer may not yet be running.
1310 // (In that case Stop() is a no-op.)
1311 debounce_timer_.Stop();
[email protected]d323a172011-09-02 18:23:021312 debounce_timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(
[email protected]d7395e732009-08-28 23:13:431313 kDebounceTimeoutMilliseconds), this,
[email protected]573c0502011-05-17 22:19:501314 &SettingGetterImplKDE::OnDebouncedNotification);
[email protected]d7395e732009-08-28 23:13:431315 }
1316 }
1317
[email protected]6b5fe742011-05-20 21:46:481318 typedef std::map<StringSetting, std::string> string_map_type;
1319 typedef std::map<StringListSetting,
1320 std::vector<std::string> > strings_map_type;
[email protected]d7395e732009-08-28 23:13:431321
1322 int inotify_fd_;
1323 base::MessagePumpLibevent::FileDescriptorWatcher inotify_watcher_;
1324 ProxyConfigServiceLinux::Delegate* notify_delegate_;
[email protected]573c0502011-05-17 22:19:501325 base::OneShotTimer<SettingGetterImplKDE> debounce_timer_;
[email protected]6cdfd7f2013-02-08 20:40:151326 base::FilePath kde_config_dir_;
[email protected]d7395e732009-08-28 23:13:431327 bool indirect_manual_;
1328 bool auto_no_pac_;
[email protected]a48bf4a2010-06-14 18:24:531329 bool reversed_bypass_list_;
[email protected]f18fde22010-05-18 23:49:541330 // We don't own |env_var_getter_|. It's safe to hold a pointer to it, since
1331 // both it and us are owned by ProxyConfigServiceLinux::Delegate, and have the
1332 // same lifetime.
[email protected]76b90d312010-08-03 03:00:501333 base::Environment* env_var_getter_;
[email protected]d7395e732009-08-28 23:13:431334
1335 // We cache these settings whenever we re-read the kioslaverc file.
1336 string_map_type string_table_;
1337 strings_map_type strings_table_;
1338
1339 // Message loop of the file thread, for reading kioslaverc. If NULL,
1340 // just read it directly (for testing). We also handle inotify events
1341 // on this thread.
[email protected]2da659e2013-05-23 20:51:341342 base::MessageLoopForIO* file_loop_;
[email protected]d7395e732009-08-28 23:13:431343
[email protected]573c0502011-05-17 22:19:501344 DISALLOW_COPY_AND_ASSIGN(SettingGetterImplKDE);
[email protected]861c6c62009-04-20 16:50:561345};
1346
1347} // namespace
1348
[email protected]573c0502011-05-17 22:19:501349bool ProxyConfigServiceLinux::Delegate::GetProxyFromSettings(
[email protected]6b5fe742011-05-20 21:46:481350 SettingGetter::StringSetting host_key,
[email protected]573c0502011-05-17 22:19:501351 ProxyServer* result_server) {
[email protected]861c6c62009-04-20 16:50:561352 std::string host;
[email protected]573c0502011-05-17 22:19:501353 if (!setting_getter_->GetString(host_key, &host) || host.empty()) {
[email protected]861c6c62009-04-20 16:50:561354 // Unset or empty.
1355 return false;
1356 }
1357 // Check for an optional port.
[email protected]d7395e732009-08-28 23:13:431358 int port = 0;
[email protected]6b5fe742011-05-20 21:46:481359 SettingGetter::IntSetting port_key =
[email protected]573c0502011-05-17 22:19:501360 SettingGetter::HostSettingToPortSetting(host_key);
1361 setting_getter_->GetInt(port_key, &port);
[email protected]861c6c62009-04-20 16:50:561362 if (port != 0) {
1363 // If a port is set and non-zero:
[email protected]528c56d2010-07-30 19:28:441364 host += ":" + base::IntToString(port);
[email protected]861c6c62009-04-20 16:50:561365 }
[email protected]76960f3d2011-04-30 02:15:231366
[email protected]573c0502011-05-17 22:19:501367 // gconf settings do not appear to distinguish between SOCKS version. We
1368 // default to version 5. For more information on this policy decision, see:
[email protected]76960f3d2011-04-30 02:15:231369 // http://code.google.com/p/chromium/issues/detail?id=55912#c2
[email protected]573c0502011-05-17 22:19:501370 ProxyServer::Scheme scheme = (host_key == SettingGetter::PROXY_SOCKS_HOST) ?
1371 ProxyServer::SCHEME_SOCKS5 : ProxyServer::SCHEME_HTTP;
1372 host = FixupProxyHostScheme(scheme, host);
[email protected]87a102b2009-07-14 05:23:301373 ProxyServer proxy_server = ProxyServer::FromURI(host,
1374 ProxyServer::SCHEME_HTTP);
[email protected]861c6c62009-04-20 16:50:561375 if (proxy_server.is_valid()) {
1376 *result_server = proxy_server;
1377 return true;
1378 }
1379 return false;
1380}
1381
[email protected]573c0502011-05-17 22:19:501382bool ProxyConfigServiceLinux::Delegate::GetConfigFromSettings(
[email protected]3e44697f2009-05-22 14:37:391383 ProxyConfig* config) {
[email protected]861c6c62009-04-20 16:50:561384 std::string mode;
[email protected]573c0502011-05-17 22:19:501385 if (!setting_getter_->GetString(SettingGetter::PROXY_MODE, &mode)) {
[email protected]861c6c62009-04-20 16:50:561386 // We expect this to always be set, so if we don't see it then we
[email protected]573c0502011-05-17 22:19:501387 // probably have a gconf/gsettings problem, and so we don't have a valid
[email protected]861c6c62009-04-20 16:50:561388 // proxy config.
1389 return false;
1390 }
[email protected]3e44697f2009-05-22 14:37:391391 if (mode == "none") {
[email protected]861c6c62009-04-20 16:50:561392 // Specifically specifies no proxy.
1393 return true;
[email protected]3e44697f2009-05-22 14:37:391394 }
[email protected]861c6c62009-04-20 16:50:561395
[email protected]3e44697f2009-05-22 14:37:391396 if (mode == "auto") {
[email protected]aa3ac2cc2012-06-19 00:28:041397 // Automatic proxy config.
[email protected]861c6c62009-04-20 16:50:561398 std::string pac_url_str;
[email protected]573c0502011-05-17 22:19:501399 if (setting_getter_->GetString(SettingGetter::PROXY_AUTOCONF_URL,
1400 &pac_url_str)) {
[email protected]861c6c62009-04-20 16:50:561401 if (!pac_url_str.empty()) {
[email protected]aa3ac2cc2012-06-19 00:28:041402 // If the PAC URL is actually a file path, then put file:// in front.
1403 if (pac_url_str[0] == '/')
1404 pac_url_str = "file://" + pac_url_str;
[email protected]861c6c62009-04-20 16:50:561405 GURL pac_url(pac_url_str);
1406 if (!pac_url.is_valid())
1407 return false;
[email protected]ed4ed0f2010-02-24 00:20:481408 config->set_pac_url(pac_url);
[email protected]861c6c62009-04-20 16:50:561409 return true;
1410 }
1411 }
[email protected]ed4ed0f2010-02-24 00:20:481412 config->set_auto_detect(true);
[email protected]861c6c62009-04-20 16:50:561413 return true;
1414 }
1415
[email protected]3e44697f2009-05-22 14:37:391416 if (mode != "manual") {
[email protected]861c6c62009-04-20 16:50:561417 // Mode is unrecognized.
1418 return false;
1419 }
1420 bool use_http_proxy;
[email protected]573c0502011-05-17 22:19:501421 if (setting_getter_->GetBool(SettingGetter::PROXY_USE_HTTP_PROXY,
1422 &use_http_proxy)
[email protected]861c6c62009-04-20 16:50:561423 && !use_http_proxy) {
1424 // Another master switch for some reason. If set to false, then no
1425 // proxy. But we don't panic if the key doesn't exist.
1426 return true;
1427 }
1428
1429 bool same_proxy = false;
1430 // Indicates to use the http proxy for all protocols. This one may
[email protected]573c0502011-05-17 22:19:501431 // not exist (presumably on older versions); we assume false in that
[email protected]861c6c62009-04-20 16:50:561432 // case.
[email protected]573c0502011-05-17 22:19:501433 setting_getter_->GetBool(SettingGetter::PROXY_USE_SAME_PROXY,
1434 &same_proxy);
[email protected]861c6c62009-04-20 16:50:561435
[email protected]76960f3d2011-04-30 02:15:231436 ProxyServer proxy_for_http;
1437 ProxyServer proxy_for_https;
1438 ProxyServer proxy_for_ftp;
1439 ProxyServer socks_proxy; // (socks)
1440
1441 // This counts how many of the above ProxyServers were defined and valid.
1442 size_t num_proxies_specified = 0;
1443
1444 // Extract the per-scheme proxies. If we failed to parse it, or no proxy was
1445 // specified for the scheme, then the resulting ProxyServer will be invalid.
[email protected]573c0502011-05-17 22:19:501446 if (GetProxyFromSettings(SettingGetter::PROXY_HTTP_HOST, &proxy_for_http))
[email protected]76960f3d2011-04-30 02:15:231447 num_proxies_specified++;
[email protected]573c0502011-05-17 22:19:501448 if (GetProxyFromSettings(SettingGetter::PROXY_HTTPS_HOST, &proxy_for_https))
[email protected]76960f3d2011-04-30 02:15:231449 num_proxies_specified++;
[email protected]573c0502011-05-17 22:19:501450 if (GetProxyFromSettings(SettingGetter::PROXY_FTP_HOST, &proxy_for_ftp))
[email protected]76960f3d2011-04-30 02:15:231451 num_proxies_specified++;
[email protected]573c0502011-05-17 22:19:501452 if (GetProxyFromSettings(SettingGetter::PROXY_SOCKS_HOST, &socks_proxy))
[email protected]76960f3d2011-04-30 02:15:231453 num_proxies_specified++;
1454
1455 if (same_proxy) {
1456 if (proxy_for_http.is_valid()) {
1457 // Use the http proxy for all schemes.
[email protected]ed4ed0f2010-02-24 00:20:481458 config->proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY;
[email protected]2189e092013-03-16 18:02:021459 config->proxy_rules().single_proxies.SetSingleProxyServer(proxy_for_http);
[email protected]861c6c62009-04-20 16:50:561460 }
[email protected]76960f3d2011-04-30 02:15:231461 } else if (num_proxies_specified > 0) {
1462 if (socks_proxy.is_valid() && num_proxies_specified == 1) {
1463 // If the only proxy specified was for SOCKS, use it for all schemes.
1464 config->proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY;
[email protected]2189e092013-03-16 18:02:021465 config->proxy_rules().single_proxies.SetSingleProxyServer(socks_proxy);
[email protected]861c6c62009-04-20 16:50:561466 } else {
[email protected]2189e092013-03-16 18:02:021467 // Otherwise use the indicated proxies per-scheme.
[email protected]76960f3d2011-04-30 02:15:231468 config->proxy_rules().type =
1469 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME;
[email protected]2189e092013-03-16 18:02:021470 config->proxy_rules().proxies_for_http.
1471 SetSingleProxyServer(proxy_for_http);
1472 config->proxy_rules().proxies_for_https.
1473 SetSingleProxyServer(proxy_for_https);
1474 config->proxy_rules().proxies_for_ftp.SetSingleProxyServer(proxy_for_ftp);
1475 config->proxy_rules().fallback_proxies.SetSingleProxyServer(socks_proxy);
[email protected]861c6c62009-04-20 16:50:561476 }
1477 }
1478
[email protected]ed4ed0f2010-02-24 00:20:481479 if (config->proxy_rules().empty()) {
[email protected]861c6c62009-04-20 16:50:561480 // Manual mode but we couldn't parse any rules.
1481 return false;
1482 }
1483
1484 // Check for authentication, just so we can warn.
[email protected]d7395e732009-08-28 23:13:431485 bool use_auth = false;
[email protected]573c0502011-05-17 22:19:501486 setting_getter_->GetBool(SettingGetter::PROXY_USE_AUTHENTICATION,
1487 &use_auth);
[email protected]62749f182009-07-15 13:16:541488 if (use_auth) {
1489 // ProxyConfig does not support authentication parameters, but
1490 // Chrome will prompt for the password later. So we ignore
1491 // /system/http_proxy/*auth* settings.
1492 LOG(WARNING) << "Proxy authentication parameters ignored, see bug 16709";
1493 }
[email protected]861c6c62009-04-20 16:50:561494
1495 // Now the bypass list.
[email protected]7541206c2010-02-19 20:24:061496 std::vector<std::string> ignore_hosts_list;
[email protected]ed4ed0f2010-02-24 00:20:481497 config->proxy_rules().bypass_rules.Clear();
[email protected]573c0502011-05-17 22:19:501498 if (setting_getter_->GetStringList(SettingGetter::PROXY_IGNORE_HOSTS,
1499 &ignore_hosts_list)) {
[email protected]a8185d02010-06-11 00:19:501500 std::vector<std::string>::const_iterator it(ignore_hosts_list.begin());
[email protected]1a597192010-07-09 16:58:381501 for (; it != ignore_hosts_list.end(); ++it) {
[email protected]573c0502011-05-17 22:19:501502 if (setting_getter_->MatchHostsUsingSuffixMatching()) {
[email protected]1a597192010-07-09 16:58:381503 config->proxy_rules().bypass_rules.
1504 AddRuleFromStringUsingSuffixMatching(*it);
1505 } else {
1506 config->proxy_rules().bypass_rules.AddRuleFromString(*it);
1507 }
1508 }
[email protected]a8185d02010-06-11 00:19:501509 }
[email protected]861c6c62009-04-20 16:50:561510 // Note that there are no settings with semantics corresponding to
[email protected]1a597192010-07-09 16:58:381511 // bypass of local names in GNOME. In KDE, "<local>" is supported
1512 // as a hostname rule.
[email protected]861c6c62009-04-20 16:50:561513
[email protected]a48bf4a2010-06-14 18:24:531514 // KDE allows one to reverse the bypass rules.
[email protected]573c0502011-05-17 22:19:501515 config->proxy_rules().reverse_bypass =
1516 setting_getter_->BypassListIsReversed();
[email protected]a48bf4a2010-06-14 18:24:531517
[email protected]861c6c62009-04-20 16:50:561518 return true;
1519}
1520
[email protected]76b90d312010-08-03 03:00:501521ProxyConfigServiceLinux::Delegate::Delegate(base::Environment* env_var_getter)
[email protected]76722472012-05-24 08:26:461522 : env_var_getter_(env_var_getter) {
[email protected]573c0502011-05-17 22:19:501523 // Figure out which SettingGetterImpl to use, if any.
[email protected]6b0349ef2010-10-16 04:56:061524 switch (base::nix::GetDesktopEnvironment(env_var_getter)) {
1525 case base::nix::DESKTOP_ENVIRONMENT_GNOME:
[email protected]9e6c9bde2012-07-17 23:40:171526 case base::nix::DESKTOP_ENVIRONMENT_UNITY:
[email protected]8c20e3d2011-05-19 21:03:571527#if defined(USE_GIO)
1528 {
1529 scoped_ptr<SettingGetterImplGSettings> gs_getter(
1530 new SettingGetterImplGSettings());
1531 // We have to load symbols and check the GNOME version in use to decide
1532 // if we should use the gsettings getter. See LoadAndCheckVersion().
1533 if (gs_getter->LoadAndCheckVersion(env_var_getter))
1534 setting_getter_.reset(gs_getter.release());
1535 }
1536#endif
[email protected]6de53d42010-11-09 07:33:191537#if defined(USE_GCONF)
[email protected]8c20e3d2011-05-19 21:03:571538 // Fall back on gconf if gsettings is unavailable or incorrect.
1539 if (!setting_getter_.get())
1540 setting_getter_.reset(new SettingGetterImplGConf());
[email protected]6de53d42010-11-09 07:33:191541#endif
[email protected]d7395e732009-08-28 23:13:431542 break;
[email protected]6b0349ef2010-10-16 04:56:061543 case base::nix::DESKTOP_ENVIRONMENT_KDE3:
1544 case base::nix::DESKTOP_ENVIRONMENT_KDE4:
[email protected]573c0502011-05-17 22:19:501545 setting_getter_.reset(new SettingGetterImplKDE(env_var_getter));
[email protected]d7395e732009-08-28 23:13:431546 break;
[email protected]6b0349ef2010-10-16 04:56:061547 case base::nix::DESKTOP_ENVIRONMENT_XFCE:
1548 case base::nix::DESKTOP_ENVIRONMENT_OTHER:
[email protected]d7395e732009-08-28 23:13:431549 break;
1550 }
1551}
1552
[email protected]573c0502011-05-17 22:19:501553ProxyConfigServiceLinux::Delegate::Delegate(
1554 base::Environment* env_var_getter, SettingGetter* setting_getter)
[email protected]76722472012-05-24 08:26:461555 : env_var_getter_(env_var_getter), setting_getter_(setting_getter) {
[email protected]861c6c62009-04-20 16:50:561556}
1557
[email protected]d3066142011-05-10 02:36:201558void ProxyConfigServiceLinux::Delegate::SetUpAndFetchInitialConfig(
[email protected]76722472012-05-24 08:26:461559 base::SingleThreadTaskRunner* glib_thread_task_runner,
1560 base::SingleThreadTaskRunner* io_thread_task_runner,
[email protected]2da659e2013-05-23 20:51:341561 base::MessageLoopForIO* file_loop) {
[email protected]3e44697f2009-05-22 14:37:391562 // We should be running on the default glib main loop thread right
1563 // now. gconf can only be accessed from this thread.
[email protected]76722472012-05-24 08:26:461564 DCHECK(glib_thread_task_runner->BelongsToCurrentThread());
1565 glib_thread_task_runner_ = glib_thread_task_runner;
1566 io_thread_task_runner_ = io_thread_task_runner;
[email protected]3e44697f2009-05-22 14:37:391567
[email protected]76722472012-05-24 08:26:461568 // If we are passed a NULL |io_thread_task_runner| or |file_loop|,
1569 // then we don't set up proxy setting change notifications. This
1570 // should not be the usual case but is intended to simplify test
1571 // setups.
[email protected]90499482013-06-01 00:39:501572 if (!io_thread_task_runner_.get() || !file_loop)
[email protected]b30a3f52010-10-16 01:05:461573 VLOG(1) << "Monitoring of proxy setting changes is disabled";
[email protected]3e44697f2009-05-22 14:37:391574
1575 // Fetch and cache the current proxy config. The config is left in
[email protected]119655002010-07-23 06:02:401576 // cached_config_, where GetLatestProxyConfig() running on the IO thread
[email protected]3e44697f2009-05-22 14:37:391577 // will expect to find it. This is safe to do because we return
1578 // before this ProxyConfigServiceLinux is passed on to
1579 // the ProxyService.
[email protected]d6cb85b2009-07-23 22:10:531580
1581 // Note: It would be nice to prioritize environment variables
[email protected]92d2dc82010-04-08 17:49:591582 // and only fall back to gconf if env vars were unset. But
[email protected]d6cb85b2009-07-23 22:10:531583 // gnome-terminal "helpfully" sets http_proxy and no_proxy, and it
1584 // does so even if the proxy mode is set to auto, which would
1585 // mislead us.
1586
[email protected]3e44697f2009-05-22 14:37:391587 bool got_config = false;
[email protected]573c0502011-05-17 22:19:501588 if (setting_getter_.get() &&
[email protected]76722472012-05-24 08:26:461589 setting_getter_->Init(glib_thread_task_runner, file_loop) &&
[email protected]573c0502011-05-17 22:19:501590 GetConfigFromSettings(&cached_config_)) {
[email protected]d3066142011-05-10 02:36:201591 cached_config_.set_id(1); // Mark it as valid.
[email protected]db8ff912012-06-12 23:32:511592 cached_config_.set_source(setting_getter_->GetConfigSource());
[email protected]d3066142011-05-10 02:36:201593 VLOG(1) << "Obtained proxy settings from "
[email protected]db8ff912012-06-12 23:32:511594 << ProxyConfigSourceToString(cached_config_.source());
[email protected]d3066142011-05-10 02:36:201595
1596 // If gconf proxy mode is "none", meaning direct, then we take
1597 // that to be a valid config and will not check environment
1598 // variables. The alternative would have been to look for a proxy
1599 // whereever we can find one.
1600 got_config = true;
1601
1602 // Keep a copy of the config for use from this thread for
1603 // comparison with updated settings when we get notifications.
1604 reference_config_ = cached_config_;
1605 reference_config_.set_id(1); // Mark it as valid.
1606
1607 // We only set up notifications if we have IO and file loops available.
1608 // We do this after getting the initial configuration so that we don't have
1609 // to worry about cancelling it if the initial fetch above fails. Note that
1610 // setting up notifications has the side effect of simulating a change, so
1611 // that we won't lose any updates that may have happened after the initial
1612 // fetch and before setting up notifications. We'll detect the common case
1613 // of no changes in OnCheckProxyConfigSettings() (or sooner) and ignore it.
[email protected]76722472012-05-24 08:26:461614 if (io_thread_task_runner && file_loop) {
1615 scoped_refptr<base::SingleThreadTaskRunner> required_loop =
1616 setting_getter_->GetNotificationTaskRunner();
[email protected]90499482013-06-01 00:39:501617 if (!required_loop.get() || required_loop->BelongsToCurrentThread()) {
[email protected]d3066142011-05-10 02:36:201618 // In this case we are already on an acceptable thread.
1619 SetUpNotifications();
[email protected]d7395e732009-08-28 23:13:431620 } else {
[email protected]d3066142011-05-10 02:36:201621 // Post a task to set up notifications. We don't wait for success.
[email protected]6af889c2011-10-06 23:11:411622 required_loop->PostTask(FROM_HERE, base::Bind(
1623 &ProxyConfigServiceLinux::Delegate::SetUpNotifications, this));
[email protected]d6cb85b2009-07-23 22:10:531624 }
[email protected]d7395e732009-08-28 23:13:431625 }
[email protected]861c6c62009-04-20 16:50:561626 }
[email protected]d6cb85b2009-07-23 22:10:531627
[email protected]3e44697f2009-05-22 14:37:391628 if (!got_config) {
[email protected]d6cb85b2009-07-23 22:10:531629 // We fall back on environment variables.
[email protected]3e44697f2009-05-22 14:37:391630 //
[email protected]d3066142011-05-10 02:36:201631 // Consulting environment variables doesn't need to be done from the
1632 // default glib main loop, but it's a tiny enough amount of work.
[email protected]3e44697f2009-05-22 14:37:391633 if (GetConfigFromEnv(&cached_config_)) {
[email protected]db8ff912012-06-12 23:32:511634 cached_config_.set_source(PROXY_CONFIG_SOURCE_ENV);
[email protected]d3066142011-05-10 02:36:201635 cached_config_.set_id(1); // Mark it as valid.
[email protected]b30a3f52010-10-16 01:05:461636 VLOG(1) << "Obtained proxy settings from environment variables";
[email protected]3e44697f2009-05-22 14:37:391637 }
[email protected]861c6c62009-04-20 16:50:561638 }
[email protected]3e44697f2009-05-22 14:37:391639}
1640
[email protected]573c0502011-05-17 22:19:501641// Depending on the SettingGetter in use, this method will be called
[email protected]d3066142011-05-10 02:36:201642// on either the UI thread (GConf) or the file thread (KDE).
1643void ProxyConfigServiceLinux::Delegate::SetUpNotifications() {
[email protected]76722472012-05-24 08:26:461644 scoped_refptr<base::SingleThreadTaskRunner> required_loop =
1645 setting_getter_->GetNotificationTaskRunner();
[email protected]90499482013-06-01 00:39:501646 DCHECK(!required_loop.get() || required_loop->BelongsToCurrentThread());
[email protected]573c0502011-05-17 22:19:501647 if (!setting_getter_->SetUpNotifications(this))
[email protected]d3066142011-05-10 02:36:201648 LOG(ERROR) << "Unable to set up proxy configuration change notifications";
1649}
1650
[email protected]119655002010-07-23 06:02:401651void ProxyConfigServiceLinux::Delegate::AddObserver(Observer* observer) {
1652 observers_.AddObserver(observer);
1653}
1654
1655void ProxyConfigServiceLinux::Delegate::RemoveObserver(Observer* observer) {
1656 observers_.RemoveObserver(observer);
1657}
1658
[email protected]3a29593d2011-04-11 10:07:521659ProxyConfigService::ConfigAvailability
1660 ProxyConfigServiceLinux::Delegate::GetLatestProxyConfig(
1661 ProxyConfig* config) {
[email protected]3e44697f2009-05-22 14:37:391662 // This is called from the IO thread.
[email protected]90499482013-06-01 00:39:501663 DCHECK(!io_thread_task_runner_.get() ||
[email protected]76722472012-05-24 08:26:461664 io_thread_task_runner_->BelongsToCurrentThread());
[email protected]3e44697f2009-05-22 14:37:391665
1666 // Simply return the last proxy configuration that glib_default_loop
1667 // notified us of.
[email protected]db8ff912012-06-12 23:32:511668 if (cached_config_.is_valid()) {
1669 *config = cached_config_;
1670 } else {
1671 *config = ProxyConfig::CreateDirect();
1672 config->set_source(PROXY_CONFIG_SOURCE_SYSTEM_FAILED);
1673 }
[email protected]119655002010-07-23 06:02:401674
[email protected]3a29593d2011-04-11 10:07:521675 // We return CONFIG_VALID to indicate that *config was filled in. It is always
[email protected]119655002010-07-23 06:02:401676 // going to be available since we initialized eagerly on the UI thread.
1677 // TODO(eroman): do lazy initialization instead, so we no longer need
1678 // to construct ProxyConfigServiceLinux on the UI thread.
1679 // In which case, we may return false here.
[email protected]3a29593d2011-04-11 10:07:521680 return CONFIG_VALID;
[email protected]3e44697f2009-05-22 14:37:391681}
1682
[email protected]573c0502011-05-17 22:19:501683// Depending on the SettingGetter in use, this method will be called
[email protected]d7395e732009-08-28 23:13:431684// on either the UI thread (GConf) or the file thread (KDE).
[email protected]3e44697f2009-05-22 14:37:391685void ProxyConfigServiceLinux::Delegate::OnCheckProxyConfigSettings() {
[email protected]76722472012-05-24 08:26:461686 scoped_refptr<base::SingleThreadTaskRunner> required_loop =
1687 setting_getter_->GetNotificationTaskRunner();
[email protected]90499482013-06-01 00:39:501688 DCHECK(!required_loop.get() || required_loop->BelongsToCurrentThread());
[email protected]3e44697f2009-05-22 14:37:391689 ProxyConfig new_config;
[email protected]573c0502011-05-17 22:19:501690 bool valid = GetConfigFromSettings(&new_config);
[email protected]3e44697f2009-05-22 14:37:391691 if (valid)
1692 new_config.set_id(1); // mark it as valid
1693
[email protected]119655002010-07-23 06:02:401694 // See if it is different from what we had before.
[email protected]3e44697f2009-05-22 14:37:391695 if (new_config.is_valid() != reference_config_.is_valid() ||
1696 !new_config.Equals(reference_config_)) {
[email protected]76722472012-05-24 08:26:461697 // Post a task to the IO thread with the new configuration, so it can
[email protected]3e44697f2009-05-22 14:37:391698 // update |cached_config_|.
[email protected]76722472012-05-24 08:26:461699 io_thread_task_runner_->PostTask(FROM_HERE, base::Bind(
[email protected]6af889c2011-10-06 23:11:411700 &ProxyConfigServiceLinux::Delegate::SetNewProxyConfig,
1701 this, new_config));
[email protected]d1f9d472009-08-13 19:59:301702 // Update the thread-private copy in |reference_config_| as well.
1703 reference_config_ = new_config;
[email protected]d3066142011-05-10 02:36:201704 } else {
1705 VLOG(1) << "Detected no-op change to proxy settings. Doing nothing.";
[email protected]3e44697f2009-05-22 14:37:391706 }
1707}
1708
1709void ProxyConfigServiceLinux::Delegate::SetNewProxyConfig(
1710 const ProxyConfig& new_config) {
[email protected]76722472012-05-24 08:26:461711 DCHECK(io_thread_task_runner_->BelongsToCurrentThread());
[email protected]b30a3f52010-10-16 01:05:461712 VLOG(1) << "Proxy configuration changed";
[email protected]3e44697f2009-05-22 14:37:391713 cached_config_ = new_config;
[email protected]3a29593d2011-04-11 10:07:521714 FOR_EACH_OBSERVER(
1715 Observer, observers_,
1716 OnProxyConfigChanged(new_config, ProxyConfigService::CONFIG_VALID));
[email protected]3e44697f2009-05-22 14:37:391717}
1718
1719void ProxyConfigServiceLinux::Delegate::PostDestroyTask() {
[email protected]573c0502011-05-17 22:19:501720 if (!setting_getter_.get())
[email protected]d7395e732009-08-28 23:13:431721 return;
[email protected]76722472012-05-24 08:26:461722 scoped_refptr<base::SingleThreadTaskRunner> shutdown_loop =
1723 setting_getter_->GetNotificationTaskRunner();
[email protected]90499482013-06-01 00:39:501724 if (!shutdown_loop.get() || shutdown_loop->BelongsToCurrentThread()) {
[email protected]3e44697f2009-05-22 14:37:391725 // Already on the right thread, call directly.
1726 // This is the case for the unittests.
1727 OnDestroy();
1728 } else {
[email protected]d7395e732009-08-28 23:13:431729 // Post to shutdown thread. Note that on browser shutdown, we may quit
1730 // this MessageLoop and exit the program before ever running this.
[email protected]6af889c2011-10-06 23:11:411731 shutdown_loop->PostTask(FROM_HERE, base::Bind(
1732 &ProxyConfigServiceLinux::Delegate::OnDestroy, this));
[email protected]3e44697f2009-05-22 14:37:391733 }
1734}
1735void ProxyConfigServiceLinux::Delegate::OnDestroy() {
[email protected]76722472012-05-24 08:26:461736 scoped_refptr<base::SingleThreadTaskRunner> shutdown_loop =
1737 setting_getter_->GetNotificationTaskRunner();
[email protected]90499482013-06-01 00:39:501738 DCHECK(!shutdown_loop.get() || shutdown_loop->BelongsToCurrentThread());
[email protected]573c0502011-05-17 22:19:501739 setting_getter_->ShutDown();
[email protected]3e44697f2009-05-22 14:37:391740}
1741
1742ProxyConfigServiceLinux::ProxyConfigServiceLinux()
[email protected]76b90d312010-08-03 03:00:501743 : delegate_(new Delegate(base::Environment::Create())) {
[email protected]3e44697f2009-05-22 14:37:391744}
1745
[email protected]8e1845e12010-09-15 19:22:241746ProxyConfigServiceLinux::~ProxyConfigServiceLinux() {
1747 delegate_->PostDestroyTask();
1748}
1749
[email protected]3e44697f2009-05-22 14:37:391750ProxyConfigServiceLinux::ProxyConfigServiceLinux(
[email protected]76b90d312010-08-03 03:00:501751 base::Environment* env_var_getter)
[email protected]9a3d8d42009-09-03 17:01:461752 : delegate_(new Delegate(env_var_getter)) {
1753}
1754
1755ProxyConfigServiceLinux::ProxyConfigServiceLinux(
[email protected]573c0502011-05-17 22:19:501756 base::Environment* env_var_getter, SettingGetter* setting_getter)
1757 : delegate_(new Delegate(env_var_getter, setting_getter)) {
[email protected]861c6c62009-04-20 16:50:561758}
1759
[email protected]e4be2dd2010-12-14 00:44:391760void ProxyConfigServiceLinux::AddObserver(Observer* observer) {
1761 delegate_->AddObserver(observer);
1762}
1763
1764void ProxyConfigServiceLinux::RemoveObserver(Observer* observer) {
1765 delegate_->RemoveObserver(observer);
1766}
1767
[email protected]3a29593d2011-04-11 10:07:521768ProxyConfigService::ConfigAvailability
1769 ProxyConfigServiceLinux::GetLatestProxyConfig(ProxyConfig* config) {
[email protected]e4be2dd2010-12-14 00:44:391770 return delegate_->GetLatestProxyConfig(config);
1771}
1772
[email protected]861c6c62009-04-20 16:50:561773} // namespace net