blob: c39dd87465316cef10ae494c71a70cc9bd02ffab [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]76b90d312010-08-03 03:00:5022#include "base/environment.h"
[email protected]d7395e732009-08-28 23:13:4323#include "base/file_util.h"
[email protected]57999812013-02-24 05:40:5224#include "base/files/file_path.h"
[email protected]861c6c62009-04-20 16:50:5625#include "base/logging.h"
[email protected]18b577412013-07-18 04:19:1526#include "base/message_loop/message_loop.h"
[email protected]3a29593d2011-04-11 10:07:5227#include "base/nix/xdg_util.h"
[email protected]76722472012-05-24 08:26:4628#include "base/single_thread_task_runner.h"
[email protected]fc9be5802013-06-11 10:56:5129#include "base/strings/string_number_conversions.h"
[email protected]f4ebe772013-02-02 00:21:3930#include "base/strings/string_tokenizer.h"
[email protected]66e96c42013-06-28 15:20:3131#include "base/strings/string_util.h"
[email protected]9a8c4022011-01-25 14:25:3332#include "base/threading/thread_restrictions.h"
[email protected]66e96c42013-06-28 15:20:3133#include "base/timer/timer.h"
[email protected]861c6c62009-04-20 16:50:5634#include "net/base/net_errors.h"
35#include "net/http/http_util.h"
36#include "net/proxy/proxy_config.h"
37#include "net/proxy/proxy_server.h"
[email protected]f89276a72013-07-12 06:41:5438#include "url/url_canon.h"
[email protected]861c6c62009-04-20 16:50:5639
[email protected]3fc24f52012-11-30 21:22:3440#if defined(USE_GIO)
41#include "library_loaders/libgio.h"
42#endif // defined(USE_GIO)
43
[email protected]861c6c62009-04-20 16:50:5644namespace net {
45
46namespace {
47
[email protected]861c6c62009-04-20 16:50:5648// Given a proxy hostname from a setting, returns that hostname with
49// an appropriate proxy server scheme prefix.
50// scheme indicates the desired proxy scheme: usually http, with
51// socks 4 or 5 as special cases.
[email protected]87a102b2009-07-14 05:23:3052// TODO(arindam): Remove URI string manipulation by using MapUrlSchemeToProxy.
[email protected]861c6c62009-04-20 16:50:5653std::string FixupProxyHostScheme(ProxyServer::Scheme scheme,
54 std::string host) {
[email protected]e8c50812010-09-28 00:16:1755 if (scheme == ProxyServer::SCHEME_SOCKS5 &&
56 StartsWithASCII(host, "socks4://", false)) {
57 // We default to socks 5, but if the user specifically set it to
58 // socks4://, then use that.
59 scheme = ProxyServer::SCHEME_SOCKS4;
[email protected]861c6c62009-04-20 16:50:5660 }
61 // Strip the scheme if any.
62 std::string::size_type colon = host.find("://");
63 if (colon != std::string::npos)
64 host = host.substr(colon + 3);
65 // If a username and perhaps password are specified, give a warning.
66 std::string::size_type at_sign = host.find("@");
67 // Should this be supported?
68 if (at_sign != std::string::npos) {
[email protected]62749f182009-07-15 13:16:5469 // ProxyConfig does not support authentication parameters, but Chrome
70 // will prompt for the password later. Disregard the
71 // authentication parameters and continue with this hostname.
72 LOG(WARNING) << "Proxy authentication parameters ignored, see bug 16709";
[email protected]861c6c62009-04-20 16:50:5673 host = host.substr(at_sign + 1);
74 }
75 // If this is a socks proxy, prepend a scheme so as to tell
76 // ProxyServer. This also allows ProxyServer to choose the right
77 // default port.
78 if (scheme == ProxyServer::SCHEME_SOCKS4)
79 host = "socks4://" + host;
80 else if (scheme == ProxyServer::SCHEME_SOCKS5)
81 host = "socks5://" + host;
[email protected]d7395e732009-08-28 23:13:4382 // If there is a trailing slash, remove it so |host| will parse correctly
83 // even if it includes a port number (since the slash is not numeric).
84 if (host.length() && host[host.length() - 1] == '/')
85 host.resize(host.length() - 1);
[email protected]861c6c62009-04-20 16:50:5686 return host;
87}
88
89} // namespace
90
[email protected]8e1845e12010-09-15 19:22:2491ProxyConfigServiceLinux::Delegate::~Delegate() {
92}
93
[email protected]3e44697f2009-05-22 14:37:3994bool ProxyConfigServiceLinux::Delegate::GetProxyFromEnvVarForScheme(
[email protected]861c6c62009-04-20 16:50:5695 const char* variable, ProxyServer::Scheme scheme,
96 ProxyServer* result_server) {
97 std::string env_value;
[email protected]3ba7e082010-08-07 02:57:5998 if (env_var_getter_->GetVar(variable, &env_value)) {
[email protected]861c6c62009-04-20 16:50:5699 if (!env_value.empty()) {
100 env_value = FixupProxyHostScheme(scheme, env_value);
[email protected]87a102b2009-07-14 05:23:30101 ProxyServer proxy_server =
102 ProxyServer::FromURI(env_value, ProxyServer::SCHEME_HTTP);
[email protected]861c6c62009-04-20 16:50:56103 if (proxy_server.is_valid() && !proxy_server.is_direct()) {
104 *result_server = proxy_server;
105 return true;
106 } else {
[email protected]3e44697f2009-05-22 14:37:39107 LOG(ERROR) << "Failed to parse environment variable " << variable;
[email protected]861c6c62009-04-20 16:50:56108 }
109 }
110 }
111 return false;
112}
113
[email protected]3e44697f2009-05-22 14:37:39114bool ProxyConfigServiceLinux::Delegate::GetProxyFromEnvVar(
[email protected]861c6c62009-04-20 16:50:56115 const char* variable, ProxyServer* result_server) {
116 return GetProxyFromEnvVarForScheme(variable, ProxyServer::SCHEME_HTTP,
117 result_server);
118}
119
[email protected]3e44697f2009-05-22 14:37:39120bool ProxyConfigServiceLinux::Delegate::GetConfigFromEnv(ProxyConfig* config) {
[email protected]861c6c62009-04-20 16:50:56121 // Check for automatic configuration first, in
122 // "auto_proxy". Possibly only the "environment_proxy" firefox
123 // extension has ever used this, but it still sounds like a good
124 // idea.
125 std::string auto_proxy;
[email protected]3ba7e082010-08-07 02:57:59126 if (env_var_getter_->GetVar("auto_proxy", &auto_proxy)) {
[email protected]861c6c62009-04-20 16:50:56127 if (auto_proxy.empty()) {
128 // Defined and empty => autodetect
[email protected]ed4ed0f2010-02-24 00:20:48129 config->set_auto_detect(true);
[email protected]861c6c62009-04-20 16:50:56130 } else {
131 // specified autoconfig URL
[email protected]ed4ed0f2010-02-24 00:20:48132 config->set_pac_url(GURL(auto_proxy));
[email protected]861c6c62009-04-20 16:50:56133 }
134 return true;
135 }
136 // "all_proxy" is a shortcut to avoid defining {http,https,ftp}_proxy.
137 ProxyServer proxy_server;
138 if (GetProxyFromEnvVar("all_proxy", &proxy_server)) {
[email protected]ed4ed0f2010-02-24 00:20:48139 config->proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY;
[email protected]2189e092013-03-16 18:02:02140 config->proxy_rules().single_proxies.SetSingleProxyServer(proxy_server);
[email protected]861c6c62009-04-20 16:50:56141 } else {
142 bool have_http = GetProxyFromEnvVar("http_proxy", &proxy_server);
143 if (have_http)
[email protected]2189e092013-03-16 18:02:02144 config->proxy_rules().proxies_for_http.SetSingleProxyServer(proxy_server);
[email protected]861c6c62009-04-20 16:50:56145 // It would be tempting to let http_proxy apply for all protocols
146 // if https_proxy and ftp_proxy are not defined. Googling turns up
147 // several documents that mention only http_proxy. But then the
148 // user really might not want to proxy https. And it doesn't seem
149 // like other apps do this. So we will refrain.
150 bool have_https = GetProxyFromEnvVar("https_proxy", &proxy_server);
151 if (have_https)
[email protected]2189e092013-03-16 18:02:02152 config->proxy_rules().proxies_for_https.
153 SetSingleProxyServer(proxy_server);
[email protected]861c6c62009-04-20 16:50:56154 bool have_ftp = GetProxyFromEnvVar("ftp_proxy", &proxy_server);
155 if (have_ftp)
[email protected]2189e092013-03-16 18:02:02156 config->proxy_rules().proxies_for_ftp.SetSingleProxyServer(proxy_server);
[email protected]861c6c62009-04-20 16:50:56157 if (have_http || have_https || have_ftp) {
158 // mustn't change type unless some rules are actually set.
[email protected]ed4ed0f2010-02-24 00:20:48159 config->proxy_rules().type =
160 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME;
[email protected]861c6c62009-04-20 16:50:56161 }
162 }
[email protected]ed4ed0f2010-02-24 00:20:48163 if (config->proxy_rules().empty()) {
[email protected]861c6c62009-04-20 16:50:56164 // If the above were not defined, try for socks.
[email protected]e8c50812010-09-28 00:16:17165 // For environment variables, we default to version 5, per the gnome
166 // documentation: http://library.gnome.org/devel/gnet/stable/gnet-socks.html
167 ProxyServer::Scheme scheme = ProxyServer::SCHEME_SOCKS5;
[email protected]861c6c62009-04-20 16:50:56168 std::string env_version;
[email protected]3ba7e082010-08-07 02:57:59169 if (env_var_getter_->GetVar("SOCKS_VERSION", &env_version)
[email protected]e8c50812010-09-28 00:16:17170 && env_version == "4")
171 scheme = ProxyServer::SCHEME_SOCKS4;
[email protected]861c6c62009-04-20 16:50:56172 if (GetProxyFromEnvVarForScheme("SOCKS_SERVER", scheme, &proxy_server)) {
[email protected]ed4ed0f2010-02-24 00:20:48173 config->proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY;
[email protected]2189e092013-03-16 18:02:02174 config->proxy_rules().single_proxies.SetSingleProxyServer(proxy_server);
[email protected]861c6c62009-04-20 16:50:56175 }
176 }
177 // Look for the proxy bypass list.
178 std::string no_proxy;
[email protected]3ba7e082010-08-07 02:57:59179 env_var_getter_->GetVar("no_proxy", &no_proxy);
[email protected]ed4ed0f2010-02-24 00:20:48180 if (config->proxy_rules().empty()) {
[email protected]861c6c62009-04-20 16:50:56181 // Having only "no_proxy" set, presumably to "*", makes it
182 // explicit that env vars do specify a configuration: having no
183 // rules specified only means the user explicitly asks for direct
184 // connections.
185 return !no_proxy.empty();
186 }
[email protected]7541206c2010-02-19 20:24:06187 // Note that this uses "suffix" matching. So a bypass of "google.com"
188 // is understood to mean a bypass of "*google.com".
[email protected]ed4ed0f2010-02-24 00:20:48189 config->proxy_rules().bypass_rules.ParseFromStringUsingSuffixMatching(
190 no_proxy);
[email protected]861c6c62009-04-20 16:50:56191 return true;
192}
193
194namespace {
195
[email protected]d7395e732009-08-28 23:13:43196const int kDebounceTimeoutMilliseconds = 250;
[email protected]3e44697f2009-05-22 14:37:39197
[email protected]6de53d42010-11-09 07:33:19198#if defined(USE_GCONF)
[email protected]573c0502011-05-17 22:19:50199// This setting getter uses gconf, as used in GNOME 2 and some GNOME 3 desktops.
200class SettingGetterImplGConf : public ProxyConfigServiceLinux::SettingGetter {
[email protected]861c6c62009-04-20 16:50:56201 public:
[email protected]573c0502011-05-17 22:19:50202 SettingGetterImplGConf()
[email protected]b160df32012-02-06 20:39:41203 : client_(NULL), system_proxy_id_(0), system_http_proxy_id_(0),
[email protected]76722472012-05-24 08:26:46204 notify_delegate_(NULL) {
[email protected]b160df32012-02-06 20:39:41205 }
[email protected]3e44697f2009-05-22 14:37:39206
[email protected]573c0502011-05-17 22:19:50207 virtual ~SettingGetterImplGConf() {
[email protected]3e44697f2009-05-22 14:37:39208 // client_ should have been released before now, from
[email protected]f5b13442009-07-13 15:23:59209 // Delegate::OnDestroy(), while running on the UI thread. However
[email protected]b160df32012-02-06 20:39:41210 // on exiting the process, it may happen that Delegate::OnDestroy()
211 // task is left pending on the glib loop after the loop was quit,
212 // and pending tasks may then be deleted without being run.
[email protected]f5b13442009-07-13 15:23:59213 if (client_) {
214 // gconf client was not cleaned up.
[email protected]76722472012-05-24 08:26:46215 if (task_runner_->BelongsToCurrentThread()) {
[email protected]f5b13442009-07-13 15:23:59216 // We are on the UI thread so we can clean it safely. This is
217 // the case at least for ui_tests running under Valgrind in
218 // bug 16076.
[email protected]573c0502011-05-17 22:19:50219 VLOG(1) << "~SettingGetterImplGConf: releasing gconf client";
[email protected]d3066142011-05-10 02:36:20220 ShutDown();
[email protected]f5b13442009-07-13 15:23:59221 } else {
[email protected]ed348992011-09-19 20:27:57222 // This is very bad! We are deleting the setting getter but we're not on
223 // the UI thread. This is not supposed to happen: the setting getter is
224 // owned by the proxy config service's delegate, which is supposed to be
225 // destroyed on the UI thread only. We will get change notifications to
226 // a deleted object if we continue here, so fail now.
227 LOG(FATAL) << "~SettingGetterImplGConf: deleting on wrong thread!";
[email protected]f5b13442009-07-13 15:23:59228 }
229 }
[email protected]3e44697f2009-05-22 14:37:39230 DCHECK(!client_);
[email protected]861c6c62009-04-20 16:50:56231 }
232
[email protected]76722472012-05-24 08:26:46233 virtual bool Init(base::SingleThreadTaskRunner* glib_thread_task_runner,
[email protected]2da659e2013-05-23 20:51:34234 base::MessageLoopForIO* file_loop) OVERRIDE {
[email protected]76722472012-05-24 08:26:46235 DCHECK(glib_thread_task_runner->BelongsToCurrentThread());
[email protected]3e44697f2009-05-22 14:37:39236 DCHECK(!client_);
[email protected]90499482013-06-01 00:39:50237 DCHECK(!task_runner_.get());
[email protected]76722472012-05-24 08:26:46238 task_runner_ = glib_thread_task_runner;
[email protected]3e44697f2009-05-22 14:37:39239 client_ = gconf_client_get_default();
[email protected]861c6c62009-04-20 16:50:56240 if (!client_) {
[email protected]861c6c62009-04-20 16:50:56241 // It's not clear whether/when this can return NULL.
[email protected]3e44697f2009-05-22 14:37:39242 LOG(ERROR) << "Unable to create a gconf client";
[email protected]76722472012-05-24 08:26:46243 task_runner_ = NULL;
[email protected]3e44697f2009-05-22 14:37:39244 return false;
[email protected]861c6c62009-04-20 16:50:56245 }
[email protected]3e44697f2009-05-22 14:37:39246 GError* error = NULL;
[email protected]b160df32012-02-06 20:39:41247 bool added_system_proxy = false;
[email protected]3e44697f2009-05-22 14:37:39248 // We need to add the directories for which we'll be asking
[email protected]b160df32012-02-06 20:39:41249 // for notifications, and we might as well ask to preload them.
250 // These need to be removed again in ShutDown(); we are careful
251 // here to only leave client_ non-NULL if both have been added.
[email protected]3e44697f2009-05-22 14:37:39252 gconf_client_add_dir(client_, "/system/proxy",
253 GCONF_CLIENT_PRELOAD_ONELEVEL, &error);
254 if (error == NULL) {
[email protected]b160df32012-02-06 20:39:41255 added_system_proxy = true;
[email protected]3e44697f2009-05-22 14:37:39256 gconf_client_add_dir(client_, "/system/http_proxy",
257 GCONF_CLIENT_PRELOAD_ONELEVEL, &error);
258 }
259 if (error != NULL) {
260 LOG(ERROR) << "Error requesting gconf directory: " << error->message;
261 g_error_free(error);
[email protected]b160df32012-02-06 20:39:41262 if (added_system_proxy)
263 gconf_client_remove_dir(client_, "/system/proxy", NULL);
264 g_object_unref(client_);
265 client_ = NULL;
[email protected]76722472012-05-24 08:26:46266 task_runner_ = NULL;
[email protected]3e44697f2009-05-22 14:37:39267 return false;
268 }
269 return true;
270 }
271
[email protected]749bf5c2012-09-17 03:15:21272 virtual void ShutDown() OVERRIDE {
[email protected]3e44697f2009-05-22 14:37:39273 if (client_) {
[email protected]76722472012-05-24 08:26:46274 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]b160df32012-02-06 20:39:41275 // We must explicitly disable gconf notifications here, because the gconf
276 // client will be shared between all setting getters, and they do not all
277 // have the same lifetimes. (For instance, incognito sessions get their
278 // own, which is destroyed when the session ends.)
279 gconf_client_notify_remove(client_, system_http_proxy_id_);
280 gconf_client_notify_remove(client_, system_proxy_id_);
281 gconf_client_remove_dir(client_, "/system/http_proxy", NULL);
282 gconf_client_remove_dir(client_, "/system/proxy", NULL);
[email protected]3e44697f2009-05-22 14:37:39283 g_object_unref(client_);
284 client_ = NULL;
[email protected]76722472012-05-24 08:26:46285 task_runner_ = NULL;
[email protected]3e44697f2009-05-22 14:37:39286 }
287 }
288
[email protected]749bf5c2012-09-17 03:15:21289 virtual bool SetUpNotifications(
290 ProxyConfigServiceLinux::Delegate* delegate) OVERRIDE {
[email protected]3e44697f2009-05-22 14:37:39291 DCHECK(client_);
[email protected]76722472012-05-24 08:26:46292 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]3e44697f2009-05-22 14:37:39293 GError* error = NULL;
[email protected]d7395e732009-08-28 23:13:43294 notify_delegate_ = delegate;
[email protected]b160df32012-02-06 20:39:41295 // We have to keep track of the IDs returned by gconf_client_notify_add() so
296 // that we can remove them in ShutDown(). (Otherwise, notifications will be
297 // delivered to this object after it is deleted, which is bad, m'kay?)
298 system_proxy_id_ = gconf_client_notify_add(
[email protected]3e44697f2009-05-22 14:37:39299 client_, "/system/proxy",
[email protected]d7395e732009-08-28 23:13:43300 OnGConfChangeNotification, this,
[email protected]3e44697f2009-05-22 14:37:39301 NULL, &error);
302 if (error == NULL) {
[email protected]b160df32012-02-06 20:39:41303 system_http_proxy_id_ = gconf_client_notify_add(
[email protected]3e44697f2009-05-22 14:37:39304 client_, "/system/http_proxy",
[email protected]d7395e732009-08-28 23:13:43305 OnGConfChangeNotification, this,
[email protected]3e44697f2009-05-22 14:37:39306 NULL, &error);
307 }
308 if (error != NULL) {
309 LOG(ERROR) << "Error requesting gconf notifications: " << error->message;
310 g_error_free(error);
[email protected]d3066142011-05-10 02:36:20311 ShutDown();
[email protected]3e44697f2009-05-22 14:37:39312 return false;
313 }
[email protected]d3066142011-05-10 02:36:20314 // Simulate a change to avoid possibly losing updates before this point.
315 OnChangeNotification();
[email protected]3e44697f2009-05-22 14:37:39316 return true;
[email protected]861c6c62009-04-20 16:50:56317 }
318
[email protected]76722472012-05-24 08:26:46319 virtual base::SingleThreadTaskRunner* GetNotificationTaskRunner() OVERRIDE {
[email protected]90499482013-06-01 00:39:50320 return task_runner_.get();
[email protected]d7395e732009-08-28 23:13:43321 }
322
[email protected]db8ff912012-06-12 23:32:51323 virtual ProxyConfigSource GetConfigSource() OVERRIDE {
324 return PROXY_CONFIG_SOURCE_GCONF;
[email protected]d7395e732009-08-28 23:13:43325 }
326
[email protected]c4c1b482011-07-22 17:24:26327 virtual bool GetString(StringSetting key, std::string* result) OVERRIDE {
[email protected]573c0502011-05-17 22:19:50328 switch (key) {
329 case PROXY_MODE:
330 return GetStringByPath("/system/proxy/mode", result);
331 case PROXY_AUTOCONF_URL:
332 return GetStringByPath("/system/proxy/autoconfig_url", result);
333 case PROXY_HTTP_HOST:
334 return GetStringByPath("/system/http_proxy/host", result);
335 case PROXY_HTTPS_HOST:
336 return GetStringByPath("/system/proxy/secure_host", result);
337 case PROXY_FTP_HOST:
338 return GetStringByPath("/system/proxy/ftp_host", result);
339 case PROXY_SOCKS_HOST:
340 return GetStringByPath("/system/proxy/socks_host", result);
[email protected]573c0502011-05-17 22:19:50341 }
[email protected]6b5fe742011-05-20 21:46:48342 return false; // Placate compiler.
[email protected]573c0502011-05-17 22:19:50343 }
[email protected]c4c1b482011-07-22 17:24:26344 virtual bool GetBool(BoolSetting key, bool* result) OVERRIDE {
[email protected]573c0502011-05-17 22:19:50345 switch (key) {
346 case PROXY_USE_HTTP_PROXY:
347 return GetBoolByPath("/system/http_proxy/use_http_proxy", result);
348 case PROXY_USE_SAME_PROXY:
349 return GetBoolByPath("/system/http_proxy/use_same_proxy", result);
350 case PROXY_USE_AUTHENTICATION:
351 return GetBoolByPath("/system/http_proxy/use_authentication", result);
[email protected]573c0502011-05-17 22:19:50352 }
[email protected]6b5fe742011-05-20 21:46:48353 return false; // Placate compiler.
[email protected]573c0502011-05-17 22:19:50354 }
[email protected]c4c1b482011-07-22 17:24:26355 virtual bool GetInt(IntSetting key, int* result) OVERRIDE {
[email protected]573c0502011-05-17 22:19:50356 switch (key) {
357 case PROXY_HTTP_PORT:
358 return GetIntByPath("/system/http_proxy/port", result);
359 case PROXY_HTTPS_PORT:
360 return GetIntByPath("/system/proxy/secure_port", result);
361 case PROXY_FTP_PORT:
362 return GetIntByPath("/system/proxy/ftp_port", result);
363 case PROXY_SOCKS_PORT:
364 return GetIntByPath("/system/proxy/socks_port", result);
[email protected]573c0502011-05-17 22:19:50365 }
[email protected]6b5fe742011-05-20 21:46:48366 return false; // Placate compiler.
[email protected]573c0502011-05-17 22:19:50367 }
[email protected]6b5fe742011-05-20 21:46:48368 virtual bool GetStringList(StringListSetting key,
[email protected]c4c1b482011-07-22 17:24:26369 std::vector<std::string>* result) OVERRIDE {
[email protected]573c0502011-05-17 22:19:50370 switch (key) {
371 case PROXY_IGNORE_HOSTS:
372 return GetStringListByPath("/system/http_proxy/ignore_hosts", result);
[email protected]573c0502011-05-17 22:19:50373 }
[email protected]6b5fe742011-05-20 21:46:48374 return false; // Placate compiler.
[email protected]573c0502011-05-17 22:19:50375 }
376
[email protected]c4c1b482011-07-22 17:24:26377 virtual bool BypassListIsReversed() OVERRIDE {
[email protected]573c0502011-05-17 22:19:50378 // This is a KDE-specific setting.
379 return false;
380 }
381
[email protected]c4c1b482011-07-22 17:24:26382 virtual bool MatchHostsUsingSuffixMatching() OVERRIDE {
[email protected]573c0502011-05-17 22:19:50383 return false;
384 }
385
386 private:
387 bool GetStringByPath(const char* key, std::string* result) {
[email protected]3e44697f2009-05-22 14:37:39388 DCHECK(client_);
[email protected]76722472012-05-24 08:26:46389 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]861c6c62009-04-20 16:50:56390 GError* error = NULL;
391 gchar* value = gconf_client_get_string(client_, key, &error);
392 if (HandleGError(error, key))
393 return false;
394 if (!value)
395 return false;
396 *result = value;
397 g_free(value);
398 return true;
399 }
[email protected]573c0502011-05-17 22:19:50400 bool GetBoolByPath(const char* key, bool* result) {
[email protected]3e44697f2009-05-22 14:37:39401 DCHECK(client_);
[email protected]76722472012-05-24 08:26:46402 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]861c6c62009-04-20 16:50:56403 GError* error = NULL;
404 // We want to distinguish unset values from values defaulting to
405 // false. For that we need to use the type-generic
406 // gconf_client_get() rather than gconf_client_get_bool().
407 GConfValue* gconf_value = gconf_client_get(client_, key, &error);
408 if (HandleGError(error, key))
409 return false;
410 if (!gconf_value) {
411 // Unset.
412 return false;
413 }
414 if (gconf_value->type != GCONF_VALUE_BOOL) {
415 gconf_value_free(gconf_value);
416 return false;
417 }
418 gboolean bool_value = gconf_value_get_bool(gconf_value);
419 *result = static_cast<bool>(bool_value);
420 gconf_value_free(gconf_value);
421 return true;
422 }
[email protected]573c0502011-05-17 22:19:50423 bool GetIntByPath(const char* key, int* result) {
[email protected]3e44697f2009-05-22 14:37:39424 DCHECK(client_);
[email protected]76722472012-05-24 08:26:46425 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]861c6c62009-04-20 16:50:56426 GError* error = NULL;
427 int value = gconf_client_get_int(client_, key, &error);
428 if (HandleGError(error, key))
429 return false;
430 // We don't bother to distinguish an unset value because callers
431 // don't care. 0 is returned if unset.
432 *result = value;
433 return true;
434 }
[email protected]573c0502011-05-17 22:19:50435 bool GetStringListByPath(const char* key, std::vector<std::string>* result) {
[email protected]3e44697f2009-05-22 14:37:39436 DCHECK(client_);
[email protected]76722472012-05-24 08:26:46437 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]861c6c62009-04-20 16:50:56438 GError* error = NULL;
439 GSList* list = gconf_client_get_list(client_, key,
440 GCONF_VALUE_STRING, &error);
441 if (HandleGError(error, key))
442 return false;
[email protected]8c20e3d2011-05-19 21:03:57443 if (!list)
[email protected]861c6c62009-04-20 16:50:56444 return false;
[email protected]861c6c62009-04-20 16:50:56445 for (GSList *it = list; it; it = it->next) {
446 result->push_back(static_cast<char*>(it->data));
447 g_free(it->data);
448 }
449 g_slist_free(list);
450 return true;
451 }
452
[email protected]861c6c62009-04-20 16:50:56453 // Logs and frees a glib error. Returns false if there was no error
454 // (error is NULL).
455 bool HandleGError(GError* error, const char* key) {
456 if (error != NULL) {
[email protected]3e44697f2009-05-22 14:37:39457 LOG(ERROR) << "Error getting gconf value for " << key
458 << ": " << error->message;
[email protected]861c6c62009-04-20 16:50:56459 g_error_free(error);
460 return true;
461 }
462 return false;
463 }
464
[email protected]d7395e732009-08-28 23:13:43465 // This is the callback from the debounce timer.
466 void OnDebouncedNotification() {
[email protected]76722472012-05-24 08:26:46467 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]961ac942011-04-28 18:18:14468 CHECK(notify_delegate_);
[email protected]d7395e732009-08-28 23:13:43469 // Forward to a method on the proxy config service delegate object.
470 notify_delegate_->OnCheckProxyConfigSettings();
471 }
472
473 void OnChangeNotification() {
474 // We don't use Reset() because the timer may not yet be running.
475 // (In that case Stop() is a no-op.)
476 debounce_timer_.Stop();
[email protected]d323a172011-09-02 18:23:02477 debounce_timer_.Start(FROM_HERE,
[email protected]8c20e3d2011-05-19 21:03:57478 base::TimeDelta::FromMilliseconds(kDebounceTimeoutMilliseconds),
479 this, &SettingGetterImplGConf::OnDebouncedNotification);
[email protected]d7395e732009-08-28 23:13:43480 }
481
[email protected]8c20e3d2011-05-19 21:03:57482 // gconf notification callback, dispatched on the default glib main loop.
483 static void OnGConfChangeNotification(GConfClient* client, guint cnxn_id,
484 GConfEntry* entry, gpointer user_data) {
[email protected]b30a3f52010-10-16 01:05:46485 VLOG(1) << "gconf change notification for key "
486 << gconf_entry_get_key(entry);
[email protected]d7395e732009-08-28 23:13:43487 // We don't track which key has changed, just that something did change.
[email protected]573c0502011-05-17 22:19:50488 SettingGetterImplGConf* setting_getter =
489 reinterpret_cast<SettingGetterImplGConf*>(user_data);
[email protected]d7395e732009-08-28 23:13:43490 setting_getter->OnChangeNotification();
491 }
492
[email protected]861c6c62009-04-20 16:50:56493 GConfClient* client_;
[email protected]b160df32012-02-06 20:39:41494 // These ids are the values returned from gconf_client_notify_add(), which we
495 // will need in order to later call gconf_client_notify_remove().
496 guint system_proxy_id_;
497 guint system_http_proxy_id_;
498
[email protected]d7395e732009-08-28 23:13:43499 ProxyConfigServiceLinux::Delegate* notify_delegate_;
[email protected]573c0502011-05-17 22:19:50500 base::OneShotTimer<SettingGetterImplGConf> debounce_timer_;
[email protected]861c6c62009-04-20 16:50:56501
[email protected]76722472012-05-24 08:26:46502 // Task runner for the thread that we make gconf calls on. It should
[email protected]3e44697f2009-05-22 14:37:39503 // be the UI thread and all our methods should be called on this
504 // thread. Only for assertions.
[email protected]76722472012-05-24 08:26:46505 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
[email protected]3e44697f2009-05-22 14:37:39506
[email protected]573c0502011-05-17 22:19:50507 DISALLOW_COPY_AND_ASSIGN(SettingGetterImplGConf);
[email protected]d7395e732009-08-28 23:13:43508};
[email protected]6de53d42010-11-09 07:33:19509#endif // defined(USE_GCONF)
[email protected]d7395e732009-08-28 23:13:43510
[email protected]8c20e3d2011-05-19 21:03:57511#if defined(USE_GIO)
512// This setting getter uses gsettings, as used in most GNOME 3 desktops.
513class SettingGetterImplGSettings
514 : public ProxyConfigServiceLinux::SettingGetter {
515 public:
[email protected]0e14e87d2011-06-21 21:24:19516 SettingGetterImplGSettings() :
[email protected]0e14e87d2011-06-21 21:24:19517 client_(NULL),
518 http_client_(NULL),
519 https_client_(NULL),
520 ftp_client_(NULL),
521 socks_client_(NULL),
[email protected]76722472012-05-24 08:26:46522 notify_delegate_(NULL) {
[email protected]8c20e3d2011-05-19 21:03:57523 }
524
525 virtual ~SettingGetterImplGSettings() {
526 // client_ should have been released before now, from
527 // Delegate::OnDestroy(), while running on the UI thread. However
528 // on exiting the process, it may happen that
529 // Delegate::OnDestroy() task is left pending on the glib loop
530 // after the loop was quit, and pending tasks may then be deleted
531 // without being run.
532 if (client_) {
533 // gconf client was not cleaned up.
[email protected]76722472012-05-24 08:26:46534 if (task_runner_->BelongsToCurrentThread()) {
[email protected]8c20e3d2011-05-19 21:03:57535 // We are on the UI thread so we can clean it safely. This is
536 // the case at least for ui_tests running under Valgrind in
537 // bug 16076.
538 VLOG(1) << "~SettingGetterImplGSettings: releasing gsettings client";
539 ShutDown();
540 } else {
541 LOG(WARNING) << "~SettingGetterImplGSettings: leaking gsettings client";
542 client_ = NULL;
543 }
544 }
545 DCHECK(!client_);
[email protected]8c20e3d2011-05-19 21:03:57546 }
547
[email protected]4cf80f0b2011-05-20 20:30:26548 bool SchemaExists(const char* schema_name) {
[email protected]3fc24f52012-11-30 21:22:34549 const gchar* const* schemas = libgio_loader_.g_settings_list_schemas();
[email protected]4cf80f0b2011-05-20 20:30:26550 while (*schemas) {
[email protected]a099f3ae2011-08-16 21:06:58551 if (strcmp(schema_name, static_cast<const char*>(*schemas)) == 0)
[email protected]4cf80f0b2011-05-20 20:30:26552 return true;
553 schemas++;
554 }
555 return false;
556 }
557
[email protected]8c20e3d2011-05-19 21:03:57558 // LoadAndCheckVersion() must be called *before* Init()!
559 bool LoadAndCheckVersion(base::Environment* env);
560
[email protected]76722472012-05-24 08:26:46561 virtual bool Init(base::SingleThreadTaskRunner* glib_thread_task_runner,
[email protected]2da659e2013-05-23 20:51:34562 base::MessageLoopForIO* file_loop) OVERRIDE {
[email protected]76722472012-05-24 08:26:46563 DCHECK(glib_thread_task_runner->BelongsToCurrentThread());
[email protected]8c20e3d2011-05-19 21:03:57564 DCHECK(!client_);
[email protected]90499482013-06-01 00:39:50565 DCHECK(!task_runner_.get());
[email protected]4cf80f0b2011-05-20 20:30:26566
567 if (!SchemaExists("org.gnome.system.proxy") ||
[email protected]3fc24f52012-11-30 21:22:34568 !(client_ = libgio_loader_.g_settings_new("org.gnome.system.proxy"))) {
[email protected]8c20e3d2011-05-19 21:03:57569 // It's not clear whether/when this can return NULL.
570 LOG(ERROR) << "Unable to create a gsettings client";
571 return false;
572 }
[email protected]76722472012-05-24 08:26:46573 task_runner_ = glib_thread_task_runner;
[email protected]8c20e3d2011-05-19 21:03:57574 // We assume these all work if the above call worked.
[email protected]3fc24f52012-11-30 21:22:34575 http_client_ = libgio_loader_.g_settings_get_child(client_, "http");
576 https_client_ = libgio_loader_.g_settings_get_child(client_, "https");
577 ftp_client_ = libgio_loader_.g_settings_get_child(client_, "ftp");
578 socks_client_ = libgio_loader_.g_settings_get_child(client_, "socks");
[email protected]8c20e3d2011-05-19 21:03:57579 DCHECK(http_client_ && https_client_ && ftp_client_ && socks_client_);
580 return true;
581 }
582
[email protected]749bf5c2012-09-17 03:15:21583 virtual void ShutDown() OVERRIDE {
[email protected]8c20e3d2011-05-19 21:03:57584 if (client_) {
[email protected]76722472012-05-24 08:26:46585 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]8c20e3d2011-05-19 21:03:57586 // This also disables gsettings notifications.
587 g_object_unref(socks_client_);
588 g_object_unref(ftp_client_);
589 g_object_unref(https_client_);
590 g_object_unref(http_client_);
591 g_object_unref(client_);
592 // We only need to null client_ because it's the only one that we check.
593 client_ = NULL;
[email protected]76722472012-05-24 08:26:46594 task_runner_ = NULL;
[email protected]8c20e3d2011-05-19 21:03:57595 }
596 }
597
[email protected]749bf5c2012-09-17 03:15:21598 virtual bool SetUpNotifications(
599 ProxyConfigServiceLinux::Delegate* delegate) OVERRIDE {
[email protected]8c20e3d2011-05-19 21:03:57600 DCHECK(client_);
[email protected]76722472012-05-24 08:26:46601 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]8c20e3d2011-05-19 21:03:57602 notify_delegate_ = delegate;
603 // We could watch for the change-event signal instead of changed, but
604 // since we have to watch more than one object, we'd still have to
605 // debounce change notifications. This is conceptually simpler.
606 g_signal_connect(G_OBJECT(client_), "changed",
607 G_CALLBACK(OnGSettingsChangeNotification), this);
608 g_signal_connect(G_OBJECT(http_client_), "changed",
609 G_CALLBACK(OnGSettingsChangeNotification), this);
610 g_signal_connect(G_OBJECT(https_client_), "changed",
611 G_CALLBACK(OnGSettingsChangeNotification), this);
612 g_signal_connect(G_OBJECT(ftp_client_), "changed",
613 G_CALLBACK(OnGSettingsChangeNotification), this);
614 g_signal_connect(G_OBJECT(socks_client_), "changed",
615 G_CALLBACK(OnGSettingsChangeNotification), this);
616 // Simulate a change to avoid possibly losing updates before this point.
617 OnChangeNotification();
618 return true;
619 }
620
[email protected]76722472012-05-24 08:26:46621 virtual base::SingleThreadTaskRunner* GetNotificationTaskRunner() OVERRIDE {
[email protected]90499482013-06-01 00:39:50622 return task_runner_.get();
[email protected]8c20e3d2011-05-19 21:03:57623 }
624
[email protected]db8ff912012-06-12 23:32:51625 virtual ProxyConfigSource GetConfigSource() OVERRIDE {
626 return PROXY_CONFIG_SOURCE_GSETTINGS;
[email protected]8c20e3d2011-05-19 21:03:57627 }
628
[email protected]c4c1b482011-07-22 17:24:26629 virtual bool GetString(StringSetting key, std::string* result) OVERRIDE {
[email protected]8c20e3d2011-05-19 21:03:57630 DCHECK(client_);
631 switch (key) {
632 case PROXY_MODE:
633 return GetStringByPath(client_, "mode", result);
634 case PROXY_AUTOCONF_URL:
635 return GetStringByPath(client_, "autoconfig-url", result);
636 case PROXY_HTTP_HOST:
637 return GetStringByPath(http_client_, "host", result);
638 case PROXY_HTTPS_HOST:
639 return GetStringByPath(https_client_, "host", result);
640 case PROXY_FTP_HOST:
641 return GetStringByPath(ftp_client_, "host", result);
642 case PROXY_SOCKS_HOST:
643 return GetStringByPath(socks_client_, "host", result);
[email protected]8c20e3d2011-05-19 21:03:57644 }
[email protected]6b5fe742011-05-20 21:46:48645 return false; // Placate compiler.
[email protected]8c20e3d2011-05-19 21:03:57646 }
[email protected]c4c1b482011-07-22 17:24:26647 virtual bool GetBool(BoolSetting key, bool* result) OVERRIDE {
[email protected]8c20e3d2011-05-19 21:03:57648 DCHECK(client_);
649 switch (key) {
650 case PROXY_USE_HTTP_PROXY:
651 // Although there is an "enabled" boolean in http_client_, it is not set
652 // to true by the proxy config utility. We ignore it and return false.
653 return false;
654 case PROXY_USE_SAME_PROXY:
655 // Similarly, although there is a "use-same-proxy" boolean in client_,
656 // it is never set to false by the proxy config utility. We ignore it.
657 return false;
658 case PROXY_USE_AUTHENTICATION:
659 // There is also no way to set this in the proxy config utility, but it
660 // doesn't hurt us to get the actual setting (unlike the two above).
661 return GetBoolByPath(http_client_, "use-authentication", result);
[email protected]8c20e3d2011-05-19 21:03:57662 }
[email protected]6b5fe742011-05-20 21:46:48663 return false; // Placate compiler.
[email protected]8c20e3d2011-05-19 21:03:57664 }
[email protected]c4c1b482011-07-22 17:24:26665 virtual bool GetInt(IntSetting key, int* result) OVERRIDE {
[email protected]8c20e3d2011-05-19 21:03:57666 DCHECK(client_);
667 switch (key) {
668 case PROXY_HTTP_PORT:
669 return GetIntByPath(http_client_, "port", result);
670 case PROXY_HTTPS_PORT:
671 return GetIntByPath(https_client_, "port", result);
672 case PROXY_FTP_PORT:
673 return GetIntByPath(ftp_client_, "port", result);
674 case PROXY_SOCKS_PORT:
675 return GetIntByPath(socks_client_, "port", result);
[email protected]8c20e3d2011-05-19 21:03:57676 }
[email protected]6b5fe742011-05-20 21:46:48677 return false; // Placate compiler.
[email protected]8c20e3d2011-05-19 21:03:57678 }
[email protected]6b5fe742011-05-20 21:46:48679 virtual bool GetStringList(StringListSetting key,
[email protected]c4c1b482011-07-22 17:24:26680 std::vector<std::string>* result) OVERRIDE {
[email protected]8c20e3d2011-05-19 21:03:57681 DCHECK(client_);
682 switch (key) {
683 case PROXY_IGNORE_HOSTS:
684 return GetStringListByPath(client_, "ignore-hosts", result);
[email protected]8c20e3d2011-05-19 21:03:57685 }
[email protected]6b5fe742011-05-20 21:46:48686 return false; // Placate compiler.
[email protected]8c20e3d2011-05-19 21:03:57687 }
688
[email protected]c4c1b482011-07-22 17:24:26689 virtual bool BypassListIsReversed() OVERRIDE {
[email protected]8c20e3d2011-05-19 21:03:57690 // This is a KDE-specific setting.
691 return false;
692 }
693
[email protected]c4c1b482011-07-22 17:24:26694 virtual bool MatchHostsUsingSuffixMatching() OVERRIDE {
[email protected]8c20e3d2011-05-19 21:03:57695 return false;
696 }
697
698 private:
[email protected]8c20e3d2011-05-19 21:03:57699 bool GetStringByPath(GSettings* client, const char* key,
700 std::string* result) {
[email protected]76722472012-05-24 08:26:46701 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]3fc24f52012-11-30 21:22:34702 gchar* value = libgio_loader_.g_settings_get_string(client, key);
[email protected]8c20e3d2011-05-19 21:03:57703 if (!value)
704 return false;
705 *result = value;
706 g_free(value);
707 return true;
708 }
709 bool GetBoolByPath(GSettings* client, const char* key, bool* result) {
[email protected]76722472012-05-24 08:26:46710 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]3fc24f52012-11-30 21:22:34711 *result = static_cast<bool>(
712 libgio_loader_.g_settings_get_boolean(client, key));
[email protected]8c20e3d2011-05-19 21:03:57713 return true;
714 }
715 bool GetIntByPath(GSettings* client, const char* key, int* result) {
[email protected]76722472012-05-24 08:26:46716 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]3fc24f52012-11-30 21:22:34717 *result = libgio_loader_.g_settings_get_int(client, key);
[email protected]8c20e3d2011-05-19 21:03:57718 return true;
719 }
720 bool GetStringListByPath(GSettings* client, const char* key,
721 std::vector<std::string>* result) {
[email protected]76722472012-05-24 08:26:46722 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]3fc24f52012-11-30 21:22:34723 gchar** list = libgio_loader_.g_settings_get_strv(client, key);
[email protected]8c20e3d2011-05-19 21:03:57724 if (!list)
725 return false;
726 for (size_t i = 0; list[i]; ++i) {
727 result->push_back(static_cast<char*>(list[i]));
728 g_free(list[i]);
729 }
730 g_free(list);
731 return true;
732 }
733
734 // This is the callback from the debounce timer.
735 void OnDebouncedNotification() {
[email protected]76722472012-05-24 08:26:46736 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]8c20e3d2011-05-19 21:03:57737 CHECK(notify_delegate_);
738 // Forward to a method on the proxy config service delegate object.
739 notify_delegate_->OnCheckProxyConfigSettings();
740 }
741
742 void OnChangeNotification() {
743 // We don't use Reset() because the timer may not yet be running.
744 // (In that case Stop() is a no-op.)
745 debounce_timer_.Stop();
[email protected]d323a172011-09-02 18:23:02746 debounce_timer_.Start(FROM_HERE,
[email protected]8c20e3d2011-05-19 21:03:57747 base::TimeDelta::FromMilliseconds(kDebounceTimeoutMilliseconds),
748 this, &SettingGetterImplGSettings::OnDebouncedNotification);
749 }
750
751 // gsettings notification callback, dispatched on the default glib main loop.
752 static void OnGSettingsChangeNotification(GSettings* client, gchar* key,
753 gpointer user_data) {
754 VLOG(1) << "gsettings change notification for key " << key;
755 // We don't track which key has changed, just that something did change.
756 SettingGetterImplGSettings* setting_getter =
757 reinterpret_cast<SettingGetterImplGSettings*>(user_data);
758 setting_getter->OnChangeNotification();
759 }
760
761 GSettings* client_;
762 GSettings* http_client_;
763 GSettings* https_client_;
764 GSettings* ftp_client_;
765 GSettings* socks_client_;
766 ProxyConfigServiceLinux::Delegate* notify_delegate_;
767 base::OneShotTimer<SettingGetterImplGSettings> debounce_timer_;
768
[email protected]76722472012-05-24 08:26:46769 // Task runner for the thread that we make gsettings calls on. It should
[email protected]8c20e3d2011-05-19 21:03:57770 // be the UI thread and all our methods should be called on this
771 // thread. Only for assertions.
[email protected]76722472012-05-24 08:26:46772 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
[email protected]8c20e3d2011-05-19 21:03:57773
[email protected]3fc24f52012-11-30 21:22:34774 LibGioLoader libgio_loader_;
775
[email protected]8c20e3d2011-05-19 21:03:57776 DISALLOW_COPY_AND_ASSIGN(SettingGetterImplGSettings);
777};
778
779bool SettingGetterImplGSettings::LoadAndCheckVersion(
780 base::Environment* env) {
781 // LoadAndCheckVersion() must be called *before* Init()!
782 DCHECK(!client_);
783
784 // The APIs to query gsettings were introduced after the minimum glib
785 // version we target, so we can't link directly against them. We load them
786 // dynamically at runtime, and if they don't exist, return false here. (We
787 // support linking directly via gyp flags though.) Additionally, even when
788 // they are present, we do two additional checks to make sure we should use
789 // them and not gconf. First, we attempt to load the schema for proxy
790 // settings. Second, we check for the program that was used in older
791 // versions of GNOME to configure proxy settings, and return false if it
792 // exists. Some distributions (e.g. Ubuntu 11.04) have the API and schema
793 // but don't use gsettings for proxy settings, but they do have the old
794 // binary, so we detect these systems that way.
795
[email protected]ae82cea2012-12-06 22:52:10796 {
797 // TODO(phajdan.jr): Redesign the code to load library on different thread.
798 base::ThreadRestrictions::ScopedAllowIO allow_io;
799
800 // Try also without .0 at the end; on some systems this may be required.
801 if (!libgio_loader_.Load("libgio-2.0.so.0") &&
802 !libgio_loader_.Load("libgio-2.0.so")) {
803 VLOG(1) << "Cannot load gio library. Will fall back to gconf.";
804 return false;
805 }
[email protected]8c20e3d2011-05-19 21:03:57806 }
[email protected]8c20e3d2011-05-19 21:03:57807
[email protected]4cf80f0b2011-05-20 20:30:26808 GSettings* client;
809 if (!SchemaExists("org.gnome.system.proxy") ||
[email protected]3fc24f52012-11-30 21:22:34810 !(client = libgio_loader_.g_settings_new("org.gnome.system.proxy"))) {
[email protected]8c20e3d2011-05-19 21:03:57811 VLOG(1) << "Cannot create gsettings client. Will fall back to gconf.";
812 return false;
813 }
814 g_object_unref(client);
815
816 std::string path;
817 if (!env->GetVar("PATH", &path)) {
818 LOG(ERROR) << "No $PATH variable. Assuming no gnome-network-properties.";
819 } else {
820 // Yes, we're on the UI thread. Yes, we're accessing the file system.
821 // Sadly, we don't have much choice. We need the proxy settings and we
822 // need them now, and to figure out where to get them, we have to check
823 // for this binary. See http://crbug.com/69057 for additional details.
824 base::ThreadRestrictions::ScopedAllowIO allow_io;
825 std::vector<std::string> paths;
826 Tokenize(path, ":", &paths);
827 for (size_t i = 0; i < paths.size(); ++i) {
[email protected]6cdfd7f2013-02-08 20:40:15828 base::FilePath file(paths[i]);
[email protected]7567484142013-07-11 17:36:07829 if (base::PathExists(file.Append("gnome-network-properties"))) {
[email protected]8c20e3d2011-05-19 21:03:57830 VLOG(1) << "Found gnome-network-properties. Will fall back to gconf.";
831 return false;
832 }
833 }
834 }
835
836 VLOG(1) << "All gsettings tests OK. Will get proxy config from gsettings.";
837 return true;
838}
839#endif // defined(USE_GIO)
840
[email protected]d7395e732009-08-28 23:13:43841// This is the KDE version that reads kioslaverc and simulates gconf.
842// Doing this allows the main Delegate code, as well as the unit tests
843// for it, to stay the same - and the settings map fairly well besides.
[email protected]573c0502011-05-17 22:19:50844class SettingGetterImplKDE : public ProxyConfigServiceLinux::SettingGetter,
845 public base::MessagePumpLibevent::Watcher {
[email protected]d7395e732009-08-28 23:13:43846 public:
[email protected]573c0502011-05-17 22:19:50847 explicit SettingGetterImplKDE(base::Environment* env_var_getter)
[email protected]d7395e732009-08-28 23:13:43848 : inotify_fd_(-1), notify_delegate_(NULL), indirect_manual_(false),
[email protected]a48bf4a2010-06-14 18:24:53849 auto_no_pac_(false), reversed_bypass_list_(false),
[email protected]f18fde22010-05-18 23:49:54850 env_var_getter_(env_var_getter), file_loop_(NULL) {
[email protected]9a8c4022011-01-25 14:25:33851 // This has to be called on the UI thread (http://crbug.com/69057).
852 base::ThreadRestrictions::ScopedAllowIO allow_io;
853
[email protected]f18fde22010-05-18 23:49:54854 // Derive the location of the kde config dir from the environment.
[email protected]92d2dc82010-04-08 17:49:59855 std::string home;
[email protected]3ba7e082010-08-07 02:57:59856 if (env_var_getter->GetVar("KDEHOME", &home) && !home.empty()) {
[email protected]2e8cfe22010-06-12 00:26:24857 // $KDEHOME is set. Use it unconditionally.
[email protected]6cdfd7f2013-02-08 20:40:15858 kde_config_dir_ = KDEHomeToConfigPath(base::FilePath(home));
[email protected]92d2dc82010-04-08 17:49:59859 } else {
[email protected]2e8cfe22010-06-12 00:26:24860 // $KDEHOME is unset. Try to figure out what to use. This seems to be
[email protected]92d2dc82010-04-08 17:49:59861 // the common case on most distributions.
[email protected]3ba7e082010-08-07 02:57:59862 if (!env_var_getter->GetVar(base::env_vars::kHome, &home))
[email protected]d7395e732009-08-28 23:13:43863 // User has no $HOME? Give up. Later we'll report the failure.
864 return;
[email protected]6b0349ef2010-10-16 04:56:06865 if (base::nix::GetDesktopEnvironment(env_var_getter) ==
866 base::nix::DESKTOP_ENVIRONMENT_KDE3) {
[email protected]92d2dc82010-04-08 17:49:59867 // KDE3 always uses .kde for its configuration.
[email protected]6cdfd7f2013-02-08 20:40:15868 base::FilePath kde_path = base::FilePath(home).Append(".kde");
[email protected]92d2dc82010-04-08 17:49:59869 kde_config_dir_ = KDEHomeToConfigPath(kde_path);
870 } else {
871 // Some distributions patch KDE4 to use .kde4 instead of .kde, so that
[email protected]fad9c8a52010-06-10 22:30:53872 // both can be installed side-by-side. Sadly they don't all do this, and
873 // they don't always do this: some distributions have started switching
874 // back as well. So if there is a .kde4 directory, check the timestamps
875 // of the config directories within and use the newest one.
[email protected]92d2dc82010-04-08 17:49:59876 // Note that we should currently be running in the UI thread, because in
877 // the gconf version, that is the only thread that can access the proxy
878 // settings (a gconf restriction). As noted below, the initial read of
879 // the proxy settings will be done in this thread anyway, so we check
880 // for .kde4 here in this thread as well.
[email protected]6cdfd7f2013-02-08 20:40:15881 base::FilePath kde3_path = base::FilePath(home).Append(".kde");
882 base::FilePath kde3_config = KDEHomeToConfigPath(kde3_path);
883 base::FilePath kde4_path = base::FilePath(home).Append(".kde4");
884 base::FilePath kde4_config = KDEHomeToConfigPath(kde4_path);
[email protected]fad9c8a52010-06-10 22:30:53885 bool use_kde4 = false;
[email protected]dcd16612013-07-15 20:18:09886 if (base::DirectoryExists(kde4_path)) {
[email protected]2f0193c22010-09-03 02:28:37887 base::PlatformFileInfo kde3_info;
888 base::PlatformFileInfo kde4_info;
[email protected]9eae4e62013-12-04 20:56:49889 if (base::GetFileInfo(kde4_config, &kde4_info)) {
890 if (base::GetFileInfo(kde3_config, &kde3_info)) {
[email protected]fad9c8a52010-06-10 22:30:53891 use_kde4 = kde4_info.last_modified >= kde3_info.last_modified;
892 } else {
893 use_kde4 = true;
894 }
895 }
896 }
897 if (use_kde4) {
[email protected]92d2dc82010-04-08 17:49:59898 kde_config_dir_ = KDEHomeToConfigPath(kde4_path);
899 } else {
[email protected]fad9c8a52010-06-10 22:30:53900 kde_config_dir_ = KDEHomeToConfigPath(kde3_path);
[email protected]92d2dc82010-04-08 17:49:59901 }
902 }
[email protected]d7395e732009-08-28 23:13:43903 }
[email protected]d7395e732009-08-28 23:13:43904 }
905
[email protected]573c0502011-05-17 22:19:50906 virtual ~SettingGetterImplKDE() {
[email protected]d7395e732009-08-28 23:13:43907 // inotify_fd_ should have been closed before now, from
908 // Delegate::OnDestroy(), while running on the file thread. However
909 // on exiting the process, it may happen that Delegate::OnDestroy()
910 // task is left pending on the file loop after the loop was quit,
911 // and pending tasks may then be deleted without being run.
912 // Here in the KDE version, we can safely close the file descriptor
913 // anyway. (Not that it really matters; the process is exiting.)
914 if (inotify_fd_ >= 0)
[email protected]d3066142011-05-10 02:36:20915 ShutDown();
[email protected]d7395e732009-08-28 23:13:43916 DCHECK(inotify_fd_ < 0);
917 }
918
[email protected]76722472012-05-24 08:26:46919 virtual bool Init(base::SingleThreadTaskRunner* glib_thread_task_runner,
[email protected]2da659e2013-05-23 20:51:34920 base::MessageLoopForIO* file_loop) OVERRIDE {
[email protected]9a8c4022011-01-25 14:25:33921 // This has to be called on the UI thread (http://crbug.com/69057).
922 base::ThreadRestrictions::ScopedAllowIO allow_io;
[email protected]d7395e732009-08-28 23:13:43923 DCHECK(inotify_fd_ < 0);
924 inotify_fd_ = inotify_init();
925 if (inotify_fd_ < 0) {
[email protected]57b765672009-10-13 18:27:40926 PLOG(ERROR) << "inotify_init failed";
[email protected]d7395e732009-08-28 23:13:43927 return false;
928 }
929 int flags = fcntl(inotify_fd_, F_GETFL);
930 if (fcntl(inotify_fd_, F_SETFL, flags | O_NONBLOCK) < 0) {
[email protected]57b765672009-10-13 18:27:40931 PLOG(ERROR) << "fcntl failed";
[email protected]d7395e732009-08-28 23:13:43932 close(inotify_fd_);
933 inotify_fd_ = -1;
934 return false;
935 }
936 file_loop_ = file_loop;
937 // The initial read is done on the current thread, not |file_loop_|,
[email protected]d3066142011-05-10 02:36:20938 // since we will need to have it for SetUpAndFetchInitialConfig().
[email protected]d7395e732009-08-28 23:13:43939 UpdateCachedSettings();
940 return true;
941 }
942
[email protected]749bf5c2012-09-17 03:15:21943 virtual void ShutDown() OVERRIDE {
[email protected]d7395e732009-08-28 23:13:43944 if (inotify_fd_ >= 0) {
945 ResetCachedSettings();
946 inotify_watcher_.StopWatchingFileDescriptor();
947 close(inotify_fd_);
948 inotify_fd_ = -1;
949 }
950 }
951
[email protected]749bf5c2012-09-17 03:15:21952 virtual bool SetUpNotifications(
953 ProxyConfigServiceLinux::Delegate* delegate) OVERRIDE {
[email protected]d7395e732009-08-28 23:13:43954 DCHECK(inotify_fd_ >= 0);
[email protected]2da659e2013-05-23 20:51:34955 DCHECK(base::MessageLoop::current() == file_loop_);
[email protected]d7395e732009-08-28 23:13:43956 // We can't just watch the kioslaverc file directly, since KDE will write
957 // a new copy of it and then rename it whenever settings are changed and
958 // inotify watches inodes (so we'll be watching the old deleted file after
959 // the first change, and it will never change again). So, we watch the
960 // directory instead. We then act only on changes to the kioslaverc entry.
961 if (inotify_add_watch(inotify_fd_, kde_config_dir_.value().c_str(),
962 IN_MODIFY | IN_MOVED_TO) < 0)
963 return false;
964 notify_delegate_ = delegate;
[email protected]2da659e2013-05-23 20:51:34965 if (!file_loop_->WatchFileDescriptor(inotify_fd_,
966 true,
967 base::MessageLoopForIO::WATCH_READ,
968 &inotify_watcher_,
969 this))
[email protected]d3066142011-05-10 02:36:20970 return false;
971 // Simulate a change to avoid possibly losing updates before this point.
972 OnChangeNotification();
973 return true;
[email protected]d7395e732009-08-28 23:13:43974 }
975
[email protected]76722472012-05-24 08:26:46976 virtual base::SingleThreadTaskRunner* GetNotificationTaskRunner() OVERRIDE {
[email protected]198b5902013-06-27 10:36:11977 return file_loop_ ? file_loop_->message_loop_proxy().get() : NULL;
[email protected]d7395e732009-08-28 23:13:43978 }
979
[email protected]b160df32012-02-06 20:39:41980 // Implement base::MessagePumpLibevent::Watcher.
[email protected]749bf5c2012-09-17 03:15:21981 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE {
[email protected]d2e6d592012-02-03 21:49:04982 DCHECK_EQ(fd, inotify_fd_);
[email protected]2da659e2013-05-23 20:51:34983 DCHECK(base::MessageLoop::current() == file_loop_);
[email protected]d7395e732009-08-28 23:13:43984 OnChangeNotification();
985 }
[email protected]749bf5c2012-09-17 03:15:21986 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE {
[email protected]d7395e732009-08-28 23:13:43987 NOTREACHED();
988 }
989
[email protected]db8ff912012-06-12 23:32:51990 virtual ProxyConfigSource GetConfigSource() OVERRIDE {
991 return PROXY_CONFIG_SOURCE_KDE;
[email protected]d7395e732009-08-28 23:13:43992 }
993
[email protected]c4c1b482011-07-22 17:24:26994 virtual bool GetString(StringSetting key, std::string* result) OVERRIDE {
[email protected]d7395e732009-08-28 23:13:43995 string_map_type::iterator it = string_table_.find(key);
996 if (it == string_table_.end())
997 return false;
998 *result = it->second;
999 return true;
1000 }
[email protected]c4c1b482011-07-22 17:24:261001 virtual bool GetBool(BoolSetting key, bool* result) OVERRIDE {
[email protected]d7395e732009-08-28 23:13:431002 // We don't ever have any booleans.
1003 return false;
1004 }
[email protected]c4c1b482011-07-22 17:24:261005 virtual bool GetInt(IntSetting key, int* result) OVERRIDE {
[email protected]d7395e732009-08-28 23:13:431006 // We don't ever have any integers. (See AddProxy() below about ports.)
1007 return false;
1008 }
[email protected]6b5fe742011-05-20 21:46:481009 virtual bool GetStringList(StringListSetting key,
[email protected]c4c1b482011-07-22 17:24:261010 std::vector<std::string>* result) OVERRIDE {
[email protected]d7395e732009-08-28 23:13:431011 strings_map_type::iterator it = strings_table_.find(key);
1012 if (it == strings_table_.end())
1013 return false;
1014 *result = it->second;
1015 return true;
1016 }
1017
[email protected]c4c1b482011-07-22 17:24:261018 virtual bool BypassListIsReversed() OVERRIDE {
[email protected]a48bf4a2010-06-14 18:24:531019 return reversed_bypass_list_;
1020 }
1021
[email protected]c4c1b482011-07-22 17:24:261022 virtual bool MatchHostsUsingSuffixMatching() OVERRIDE {
[email protected]1a597192010-07-09 16:58:381023 return true;
1024 }
1025
[email protected]d7395e732009-08-28 23:13:431026 private:
1027 void ResetCachedSettings() {
1028 string_table_.clear();
1029 strings_table_.clear();
1030 indirect_manual_ = false;
1031 auto_no_pac_ = false;
[email protected]a48bf4a2010-06-14 18:24:531032 reversed_bypass_list_ = false;
[email protected]d7395e732009-08-28 23:13:431033 }
1034
[email protected]6cdfd7f2013-02-08 20:40:151035 base::FilePath KDEHomeToConfigPath(const base::FilePath& kde_home) {
[email protected]92d2dc82010-04-08 17:49:591036 return kde_home.Append("share").Append("config");
1037 }
1038
[email protected]6b5fe742011-05-20 21:46:481039 void AddProxy(StringSetting host_key, const std::string& value) {
[email protected]d7395e732009-08-28 23:13:431040 if (value.empty() || value.substr(0, 3) == "//:")
1041 // No proxy.
1042 return;
[email protected]4b90c202012-04-24 23:27:551043 size_t space = value.find(' ');
1044 if (space != std::string::npos) {
1045 // Newer versions of KDE use a space rather than a colon to separate the
1046 // port number from the hostname. If we find this, we need to convert it.
1047 std::string fixed = value;
1048 fixed[space] = ':';
1049 string_table_[host_key] = fixed;
1050 } else {
1051 // We don't need to parse the port number out; GetProxyFromSettings()
1052 // would only append it right back again. So we just leave the port
1053 // number right in the host string.
1054 string_table_[host_key] = value;
1055 }
[email protected]d7395e732009-08-28 23:13:431056 }
1057
[email protected]6b5fe742011-05-20 21:46:481058 void AddHostList(StringListSetting key, const std::string& value) {
[email protected]f18fde22010-05-18 23:49:541059 std::vector<std::string> tokens;
[email protected]f4ebe772013-02-02 00:21:391060 base::StringTokenizer tk(value, ", ");
[email protected]f18fde22010-05-18 23:49:541061 while (tk.GetNext()) {
1062 std::string token = tk.token();
1063 if (!token.empty())
1064 tokens.push_back(token);
1065 }
1066 strings_table_[key] = tokens;
1067 }
1068
[email protected]9a3d8d42009-09-03 17:01:461069 void AddKDESetting(const std::string& key, const std::string& value) {
[email protected]d7395e732009-08-28 23:13:431070 if (key == "ProxyType") {
1071 const char* mode = "none";
1072 indirect_manual_ = false;
1073 auto_no_pac_ = false;
[email protected]e83326f2010-07-31 17:29:251074 int int_value;
1075 base::StringToInt(value, &int_value);
1076 switch (int_value) {
[email protected]d7395e732009-08-28 23:13:431077 case 0: // No proxy, or maybe kioslaverc syntax error.
1078 break;
1079 case 1: // Manual configuration.
1080 mode = "manual";
1081 break;
1082 case 2: // PAC URL.
1083 mode = "auto";
1084 break;
1085 case 3: // WPAD.
1086 mode = "auto";
1087 auto_no_pac_ = true;
1088 break;
1089 case 4: // Indirect manual via environment variables.
1090 mode = "manual";
1091 indirect_manual_ = true;
1092 break;
1093 }
[email protected]573c0502011-05-17 22:19:501094 string_table_[PROXY_MODE] = mode;
[email protected]d7395e732009-08-28 23:13:431095 } else if (key == "Proxy Config Script") {
[email protected]573c0502011-05-17 22:19:501096 string_table_[PROXY_AUTOCONF_URL] = value;
[email protected]d7395e732009-08-28 23:13:431097 } else if (key == "httpProxy") {
[email protected]573c0502011-05-17 22:19:501098 AddProxy(PROXY_HTTP_HOST, value);
[email protected]d7395e732009-08-28 23:13:431099 } else if (key == "httpsProxy") {
[email protected]573c0502011-05-17 22:19:501100 AddProxy(PROXY_HTTPS_HOST, value);
[email protected]d7395e732009-08-28 23:13:431101 } else if (key == "ftpProxy") {
[email protected]573c0502011-05-17 22:19:501102 AddProxy(PROXY_FTP_HOST, value);
[email protected]bfeb7232012-06-08 00:58:371103 } else if (key == "socksProxy") {
1104 // Older versions of KDE configure SOCKS in a weird way involving
1105 // LD_PRELOAD and a library that intercepts network calls to SOCKSify
1106 // them. We don't support it. KDE 4.8 added a proper SOCKS setting.
1107 AddProxy(PROXY_SOCKS_HOST, value);
[email protected]d7395e732009-08-28 23:13:431108 } else if (key == "ReversedException") {
1109 // We count "true" or any nonzero number as true, otherwise false.
1110 // Note that if the value is not actually numeric StringToInt()
1111 // will return 0, which we count as false.
[email protected]e83326f2010-07-31 17:29:251112 int int_value;
1113 base::StringToInt(value, &int_value);
1114 reversed_bypass_list_ = (value == "true" || int_value);
[email protected]d7395e732009-08-28 23:13:431115 } else if (key == "NoProxyFor") {
[email protected]573c0502011-05-17 22:19:501116 AddHostList(PROXY_IGNORE_HOSTS, value);
[email protected]d7395e732009-08-28 23:13:431117 } else if (key == "AuthMode") {
1118 // Check for authentication, just so we can warn.
[email protected]e83326f2010-07-31 17:29:251119 int mode;
1120 base::StringToInt(value, &mode);
[email protected]d7395e732009-08-28 23:13:431121 if (mode) {
1122 // ProxyConfig does not support authentication parameters, but
1123 // Chrome will prompt for the password later. So we ignore this.
1124 LOG(WARNING) <<
1125 "Proxy authentication parameters ignored, see bug 16709";
1126 }
1127 }
1128 }
1129
[email protected]6b5fe742011-05-20 21:46:481130 void ResolveIndirect(StringSetting key) {
[email protected]d7395e732009-08-28 23:13:431131 string_map_type::iterator it = string_table_.find(key);
1132 if (it != string_table_.end()) {
[email protected]f18fde22010-05-18 23:49:541133 std::string value;
[email protected]3ba7e082010-08-07 02:57:591134 if (env_var_getter_->GetVar(it->second.c_str(), &value))
[email protected]d7395e732009-08-28 23:13:431135 it->second = value;
[email protected]8425adc02010-04-18 17:45:311136 else
1137 string_table_.erase(it);
[email protected]d7395e732009-08-28 23:13:431138 }
1139 }
1140
[email protected]6b5fe742011-05-20 21:46:481141 void ResolveIndirectList(StringListSetting key) {
[email protected]f18fde22010-05-18 23:49:541142 strings_map_type::iterator it = strings_table_.find(key);
1143 if (it != strings_table_.end()) {
1144 std::string value;
1145 if (!it->second.empty() &&
[email protected]3ba7e082010-08-07 02:57:591146 env_var_getter_->GetVar(it->second[0].c_str(), &value))
[email protected]f18fde22010-05-18 23:49:541147 AddHostList(key, value);
1148 else
1149 strings_table_.erase(it);
1150 }
1151 }
1152
[email protected]d7395e732009-08-28 23:13:431153 // The settings in kioslaverc could occur in any order, but some affect
1154 // others. Rather than read the whole file in and then query them in an
1155 // order that allows us to handle that, we read the settings in whatever
1156 // order they occur and do any necessary tweaking after we finish.
1157 void ResolveModeEffects() {
1158 if (indirect_manual_) {
[email protected]573c0502011-05-17 22:19:501159 ResolveIndirect(PROXY_HTTP_HOST);
1160 ResolveIndirect(PROXY_HTTPS_HOST);
1161 ResolveIndirect(PROXY_FTP_HOST);
1162 ResolveIndirectList(PROXY_IGNORE_HOSTS);
[email protected]d7395e732009-08-28 23:13:431163 }
1164 if (auto_no_pac_) {
1165 // Remove the PAC URL; we're not supposed to use it.
[email protected]573c0502011-05-17 22:19:501166 string_table_.erase(PROXY_AUTOCONF_URL);
[email protected]d7395e732009-08-28 23:13:431167 }
[email protected]d7395e732009-08-28 23:13:431168 }
1169
1170 // Reads kioslaverc one line at a time and calls AddKDESetting() to add
1171 // each relevant name-value pair to the appropriate value table.
1172 void UpdateCachedSettings() {
[email protected]6cdfd7f2013-02-08 20:40:151173 base::FilePath kioslaverc = kde_config_dir_.Append("kioslaverc");
[email protected]d7395e732009-08-28 23:13:431174 file_util::ScopedFILE input(file_util::OpenFile(kioslaverc, "r"));
1175 if (!input.get())
1176 return;
1177 ResetCachedSettings();
1178 bool in_proxy_settings = false;
1179 bool line_too_long = false;
[email protected]9a3d8d42009-09-03 17:01:461180 char line[BUFFER_SIZE];
1181 // fgets() will return NULL on EOF or error.
[email protected]d7395e732009-08-28 23:13:431182 while (fgets(line, sizeof(line), input.get())) {
1183 // fgets() guarantees the line will be properly terminated.
1184 size_t length = strlen(line);
1185 if (!length)
1186 continue;
1187 // This should be true even with CRLF endings.
1188 if (line[length - 1] != '\n') {
1189 line_too_long = true;
1190 continue;
1191 }
1192 if (line_too_long) {
1193 // The previous line had no line ending, but this done does. This is
1194 // the end of the line that was too long, so warn here and skip it.
1195 LOG(WARNING) << "skipped very long line in " << kioslaverc.value();
1196 line_too_long = false;
1197 continue;
1198 }
1199 // Remove the LF at the end, and the CR if there is one.
1200 line[--length] = '\0';
1201 if (length && line[length - 1] == '\r')
1202 line[--length] = '\0';
1203 // Now parse the line.
1204 if (line[0] == '[') {
1205 // Switching sections. All we care about is whether this is
1206 // the (a?) proxy settings section, for both KDE3 and KDE4.
1207 in_proxy_settings = !strncmp(line, "[Proxy Settings]", 16);
1208 } else if (in_proxy_settings) {
1209 // A regular line, in the (a?) proxy settings section.
[email protected]9a3d8d42009-09-03 17:01:461210 char* split = strchr(line, '=');
1211 // Skip this line if it does not contain an = sign.
1212 if (!split)
[email protected]d7395e732009-08-28 23:13:431213 continue;
[email protected]9a3d8d42009-09-03 17:01:461214 // Split the line on the = and advance |split|.
1215 *(split++) = 0;
1216 std::string key = line;
1217 std::string value = split;
1218 TrimWhitespaceASCII(key, TRIM_ALL, &key);
1219 TrimWhitespaceASCII(value, TRIM_ALL, &value);
1220 // Skip this line if the key name is empty.
1221 if (key.empty())
[email protected]d7395e732009-08-28 23:13:431222 continue;
1223 // Is the value name localized?
[email protected]9a3d8d42009-09-03 17:01:461224 if (key[key.length() - 1] == ']') {
1225 // Find the matching bracket.
1226 length = key.rfind('[');
1227 // Skip this line if the localization indicator is malformed.
1228 if (length == std::string::npos)
[email protected]d7395e732009-08-28 23:13:431229 continue;
1230 // Trim the localization indicator off.
[email protected]9a3d8d42009-09-03 17:01:461231 key.resize(length);
1232 // Remove any resulting trailing whitespace.
1233 TrimWhitespaceASCII(key, TRIM_TRAILING, &key);
1234 // Skip this line if the key name is now empty.
1235 if (key.empty())
1236 continue;
[email protected]d7395e732009-08-28 23:13:431237 }
[email protected]d7395e732009-08-28 23:13:431238 // Now fill in the tables.
[email protected]9a3d8d42009-09-03 17:01:461239 AddKDESetting(key, value);
[email protected]d7395e732009-08-28 23:13:431240 }
1241 }
1242 if (ferror(input.get()))
1243 LOG(ERROR) << "error reading " << kioslaverc.value();
1244 ResolveModeEffects();
1245 }
1246
1247 // This is the callback from the debounce timer.
1248 void OnDebouncedNotification() {
[email protected]2da659e2013-05-23 20:51:341249 DCHECK(base::MessageLoop::current() == file_loop_);
[email protected]b30a3f52010-10-16 01:05:461250 VLOG(1) << "inotify change notification for kioslaverc";
[email protected]d7395e732009-08-28 23:13:431251 UpdateCachedSettings();
[email protected]961ac942011-04-28 18:18:141252 CHECK(notify_delegate_);
[email protected]d7395e732009-08-28 23:13:431253 // Forward to a method on the proxy config service delegate object.
1254 notify_delegate_->OnCheckProxyConfigSettings();
1255 }
1256
1257 // Called by OnFileCanReadWithoutBlocking() on the file thread. Reads
1258 // from the inotify file descriptor and starts up a debounce timer if
1259 // an event for kioslaverc is seen.
1260 void OnChangeNotification() {
[email protected]d2e6d592012-02-03 21:49:041261 DCHECK_GE(inotify_fd_, 0);
[email protected]2da659e2013-05-23 20:51:341262 DCHECK(base::MessageLoop::current() == file_loop_);
[email protected]d7395e732009-08-28 23:13:431263 char event_buf[(sizeof(inotify_event) + NAME_MAX + 1) * 4];
1264 bool kioslaverc_touched = false;
1265 ssize_t r;
1266 while ((r = read(inotify_fd_, event_buf, sizeof(event_buf))) > 0) {
1267 // inotify returns variable-length structures, which is why we have
1268 // this strange-looking loop instead of iterating through an array.
1269 char* event_ptr = event_buf;
1270 while (event_ptr < event_buf + r) {
1271 inotify_event* event = reinterpret_cast<inotify_event*>(event_ptr);
1272 // The kernel always feeds us whole events.
[email protected]b1f031dd2010-03-02 23:19:331273 CHECK_LE(event_ptr + sizeof(inotify_event), event_buf + r);
1274 CHECK_LE(event->name + event->len, event_buf + r);
[email protected]d7395e732009-08-28 23:13:431275 if (!strcmp(event->name, "kioslaverc"))
1276 kioslaverc_touched = true;
1277 // Advance the pointer just past the end of the filename.
1278 event_ptr = event->name + event->len;
1279 }
1280 // We keep reading even if |kioslaverc_touched| is true to drain the
1281 // inotify event queue.
1282 }
1283 if (!r)
1284 // Instead of returning -1 and setting errno to EINVAL if there is not
1285 // enough buffer space, older kernels (< 2.6.21) return 0. Simulate the
1286 // new behavior (EINVAL) so we can reuse the code below.
1287 errno = EINVAL;
1288 if (errno != EAGAIN) {
[email protected]57b765672009-10-13 18:27:401289 PLOG(WARNING) << "error reading inotify file descriptor";
[email protected]d7395e732009-08-28 23:13:431290 if (errno == EINVAL) {
1291 // Our buffer is not large enough to read the next event. This should
1292 // not happen (because its size is calculated to always be sufficiently
1293 // large), but if it does we'd warn continuously since |inotify_fd_|
1294 // would be forever ready to read. Close it and stop watching instead.
1295 LOG(ERROR) << "inotify failure; no longer watching kioslaverc!";
1296 inotify_watcher_.StopWatchingFileDescriptor();
1297 close(inotify_fd_);
1298 inotify_fd_ = -1;
1299 }
1300 }
1301 if (kioslaverc_touched) {
1302 // We don't use Reset() because the timer may not yet be running.
1303 // (In that case Stop() is a no-op.)
1304 debounce_timer_.Stop();
[email protected]d323a172011-09-02 18:23:021305 debounce_timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(
[email protected]d7395e732009-08-28 23:13:431306 kDebounceTimeoutMilliseconds), this,
[email protected]573c0502011-05-17 22:19:501307 &SettingGetterImplKDE::OnDebouncedNotification);
[email protected]d7395e732009-08-28 23:13:431308 }
1309 }
1310
[email protected]6b5fe742011-05-20 21:46:481311 typedef std::map<StringSetting, std::string> string_map_type;
1312 typedef std::map<StringListSetting,
1313 std::vector<std::string> > strings_map_type;
[email protected]d7395e732009-08-28 23:13:431314
1315 int inotify_fd_;
1316 base::MessagePumpLibevent::FileDescriptorWatcher inotify_watcher_;
1317 ProxyConfigServiceLinux::Delegate* notify_delegate_;
[email protected]573c0502011-05-17 22:19:501318 base::OneShotTimer<SettingGetterImplKDE> debounce_timer_;
[email protected]6cdfd7f2013-02-08 20:40:151319 base::FilePath kde_config_dir_;
[email protected]d7395e732009-08-28 23:13:431320 bool indirect_manual_;
1321 bool auto_no_pac_;
[email protected]a48bf4a2010-06-14 18:24:531322 bool reversed_bypass_list_;
[email protected]f18fde22010-05-18 23:49:541323 // We don't own |env_var_getter_|. It's safe to hold a pointer to it, since
1324 // both it and us are owned by ProxyConfigServiceLinux::Delegate, and have the
1325 // same lifetime.
[email protected]76b90d312010-08-03 03:00:501326 base::Environment* env_var_getter_;
[email protected]d7395e732009-08-28 23:13:431327
1328 // We cache these settings whenever we re-read the kioslaverc file.
1329 string_map_type string_table_;
1330 strings_map_type strings_table_;
1331
1332 // Message loop of the file thread, for reading kioslaverc. If NULL,
1333 // just read it directly (for testing). We also handle inotify events
1334 // on this thread.
[email protected]2da659e2013-05-23 20:51:341335 base::MessageLoopForIO* file_loop_;
[email protected]d7395e732009-08-28 23:13:431336
[email protected]573c0502011-05-17 22:19:501337 DISALLOW_COPY_AND_ASSIGN(SettingGetterImplKDE);
[email protected]861c6c62009-04-20 16:50:561338};
1339
1340} // namespace
1341
[email protected]573c0502011-05-17 22:19:501342bool ProxyConfigServiceLinux::Delegate::GetProxyFromSettings(
[email protected]6b5fe742011-05-20 21:46:481343 SettingGetter::StringSetting host_key,
[email protected]573c0502011-05-17 22:19:501344 ProxyServer* result_server) {
[email protected]861c6c62009-04-20 16:50:561345 std::string host;
[email protected]573c0502011-05-17 22:19:501346 if (!setting_getter_->GetString(host_key, &host) || host.empty()) {
[email protected]861c6c62009-04-20 16:50:561347 // Unset or empty.
1348 return false;
1349 }
1350 // Check for an optional port.
[email protected]d7395e732009-08-28 23:13:431351 int port = 0;
[email protected]6b5fe742011-05-20 21:46:481352 SettingGetter::IntSetting port_key =
[email protected]573c0502011-05-17 22:19:501353 SettingGetter::HostSettingToPortSetting(host_key);
1354 setting_getter_->GetInt(port_key, &port);
[email protected]861c6c62009-04-20 16:50:561355 if (port != 0) {
1356 // If a port is set and non-zero:
[email protected]528c56d2010-07-30 19:28:441357 host += ":" + base::IntToString(port);
[email protected]861c6c62009-04-20 16:50:561358 }
[email protected]76960f3d2011-04-30 02:15:231359
[email protected]573c0502011-05-17 22:19:501360 // gconf settings do not appear to distinguish between SOCKS version. We
1361 // default to version 5. For more information on this policy decision, see:
[email protected]76960f3d2011-04-30 02:15:231362 // http://code.google.com/p/chromium/issues/detail?id=55912#c2
[email protected]573c0502011-05-17 22:19:501363 ProxyServer::Scheme scheme = (host_key == SettingGetter::PROXY_SOCKS_HOST) ?
1364 ProxyServer::SCHEME_SOCKS5 : ProxyServer::SCHEME_HTTP;
1365 host = FixupProxyHostScheme(scheme, host);
[email protected]87a102b2009-07-14 05:23:301366 ProxyServer proxy_server = ProxyServer::FromURI(host,
1367 ProxyServer::SCHEME_HTTP);
[email protected]861c6c62009-04-20 16:50:561368 if (proxy_server.is_valid()) {
1369 *result_server = proxy_server;
1370 return true;
1371 }
1372 return false;
1373}
1374
[email protected]573c0502011-05-17 22:19:501375bool ProxyConfigServiceLinux::Delegate::GetConfigFromSettings(
[email protected]3e44697f2009-05-22 14:37:391376 ProxyConfig* config) {
[email protected]861c6c62009-04-20 16:50:561377 std::string mode;
[email protected]573c0502011-05-17 22:19:501378 if (!setting_getter_->GetString(SettingGetter::PROXY_MODE, &mode)) {
[email protected]861c6c62009-04-20 16:50:561379 // We expect this to always be set, so if we don't see it then we
[email protected]573c0502011-05-17 22:19:501380 // probably have a gconf/gsettings problem, and so we don't have a valid
[email protected]861c6c62009-04-20 16:50:561381 // proxy config.
1382 return false;
1383 }
[email protected]3e44697f2009-05-22 14:37:391384 if (mode == "none") {
[email protected]861c6c62009-04-20 16:50:561385 // Specifically specifies no proxy.
1386 return true;
[email protected]3e44697f2009-05-22 14:37:391387 }
[email protected]861c6c62009-04-20 16:50:561388
[email protected]3e44697f2009-05-22 14:37:391389 if (mode == "auto") {
[email protected]aa3ac2cc2012-06-19 00:28:041390 // Automatic proxy config.
[email protected]861c6c62009-04-20 16:50:561391 std::string pac_url_str;
[email protected]573c0502011-05-17 22:19:501392 if (setting_getter_->GetString(SettingGetter::PROXY_AUTOCONF_URL,
1393 &pac_url_str)) {
[email protected]861c6c62009-04-20 16:50:561394 if (!pac_url_str.empty()) {
[email protected]aa3ac2cc2012-06-19 00:28:041395 // If the PAC URL is actually a file path, then put file:// in front.
1396 if (pac_url_str[0] == '/')
1397 pac_url_str = "file://" + pac_url_str;
[email protected]861c6c62009-04-20 16:50:561398 GURL pac_url(pac_url_str);
1399 if (!pac_url.is_valid())
1400 return false;
[email protected]ed4ed0f2010-02-24 00:20:481401 config->set_pac_url(pac_url);
[email protected]861c6c62009-04-20 16:50:561402 return true;
1403 }
1404 }
[email protected]ed4ed0f2010-02-24 00:20:481405 config->set_auto_detect(true);
[email protected]861c6c62009-04-20 16:50:561406 return true;
1407 }
1408
[email protected]3e44697f2009-05-22 14:37:391409 if (mode != "manual") {
[email protected]861c6c62009-04-20 16:50:561410 // Mode is unrecognized.
1411 return false;
1412 }
1413 bool use_http_proxy;
[email protected]573c0502011-05-17 22:19:501414 if (setting_getter_->GetBool(SettingGetter::PROXY_USE_HTTP_PROXY,
1415 &use_http_proxy)
[email protected]861c6c62009-04-20 16:50:561416 && !use_http_proxy) {
1417 // Another master switch for some reason. If set to false, then no
1418 // proxy. But we don't panic if the key doesn't exist.
1419 return true;
1420 }
1421
1422 bool same_proxy = false;
1423 // Indicates to use the http proxy for all protocols. This one may
[email protected]573c0502011-05-17 22:19:501424 // not exist (presumably on older versions); we assume false in that
[email protected]861c6c62009-04-20 16:50:561425 // case.
[email protected]573c0502011-05-17 22:19:501426 setting_getter_->GetBool(SettingGetter::PROXY_USE_SAME_PROXY,
1427 &same_proxy);
[email protected]861c6c62009-04-20 16:50:561428
[email protected]76960f3d2011-04-30 02:15:231429 ProxyServer proxy_for_http;
1430 ProxyServer proxy_for_https;
1431 ProxyServer proxy_for_ftp;
1432 ProxyServer socks_proxy; // (socks)
1433
1434 // This counts how many of the above ProxyServers were defined and valid.
1435 size_t num_proxies_specified = 0;
1436
1437 // Extract the per-scheme proxies. If we failed to parse it, or no proxy was
1438 // specified for the scheme, then the resulting ProxyServer will be invalid.
[email protected]573c0502011-05-17 22:19:501439 if (GetProxyFromSettings(SettingGetter::PROXY_HTTP_HOST, &proxy_for_http))
[email protected]76960f3d2011-04-30 02:15:231440 num_proxies_specified++;
[email protected]573c0502011-05-17 22:19:501441 if (GetProxyFromSettings(SettingGetter::PROXY_HTTPS_HOST, &proxy_for_https))
[email protected]76960f3d2011-04-30 02:15:231442 num_proxies_specified++;
[email protected]573c0502011-05-17 22:19:501443 if (GetProxyFromSettings(SettingGetter::PROXY_FTP_HOST, &proxy_for_ftp))
[email protected]76960f3d2011-04-30 02:15:231444 num_proxies_specified++;
[email protected]573c0502011-05-17 22:19:501445 if (GetProxyFromSettings(SettingGetter::PROXY_SOCKS_HOST, &socks_proxy))
[email protected]76960f3d2011-04-30 02:15:231446 num_proxies_specified++;
1447
1448 if (same_proxy) {
1449 if (proxy_for_http.is_valid()) {
1450 // Use the http proxy for all schemes.
[email protected]ed4ed0f2010-02-24 00:20:481451 config->proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY;
[email protected]2189e092013-03-16 18:02:021452 config->proxy_rules().single_proxies.SetSingleProxyServer(proxy_for_http);
[email protected]861c6c62009-04-20 16:50:561453 }
[email protected]76960f3d2011-04-30 02:15:231454 } else if (num_proxies_specified > 0) {
1455 if (socks_proxy.is_valid() && num_proxies_specified == 1) {
1456 // If the only proxy specified was for SOCKS, use it for all schemes.
1457 config->proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY;
[email protected]2189e092013-03-16 18:02:021458 config->proxy_rules().single_proxies.SetSingleProxyServer(socks_proxy);
[email protected]861c6c62009-04-20 16:50:561459 } else {
[email protected]2189e092013-03-16 18:02:021460 // Otherwise use the indicated proxies per-scheme.
[email protected]76960f3d2011-04-30 02:15:231461 config->proxy_rules().type =
1462 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME;
[email protected]2189e092013-03-16 18:02:021463 config->proxy_rules().proxies_for_http.
1464 SetSingleProxyServer(proxy_for_http);
1465 config->proxy_rules().proxies_for_https.
1466 SetSingleProxyServer(proxy_for_https);
1467 config->proxy_rules().proxies_for_ftp.SetSingleProxyServer(proxy_for_ftp);
1468 config->proxy_rules().fallback_proxies.SetSingleProxyServer(socks_proxy);
[email protected]861c6c62009-04-20 16:50:561469 }
1470 }
1471
[email protected]ed4ed0f2010-02-24 00:20:481472 if (config->proxy_rules().empty()) {
[email protected]861c6c62009-04-20 16:50:561473 // Manual mode but we couldn't parse any rules.
1474 return false;
1475 }
1476
1477 // Check for authentication, just so we can warn.
[email protected]d7395e732009-08-28 23:13:431478 bool use_auth = false;
[email protected]573c0502011-05-17 22:19:501479 setting_getter_->GetBool(SettingGetter::PROXY_USE_AUTHENTICATION,
1480 &use_auth);
[email protected]62749f182009-07-15 13:16:541481 if (use_auth) {
1482 // ProxyConfig does not support authentication parameters, but
1483 // Chrome will prompt for the password later. So we ignore
1484 // /system/http_proxy/*auth* settings.
1485 LOG(WARNING) << "Proxy authentication parameters ignored, see bug 16709";
1486 }
[email protected]861c6c62009-04-20 16:50:561487
1488 // Now the bypass list.
[email protected]7541206c2010-02-19 20:24:061489 std::vector<std::string> ignore_hosts_list;
[email protected]ed4ed0f2010-02-24 00:20:481490 config->proxy_rules().bypass_rules.Clear();
[email protected]573c0502011-05-17 22:19:501491 if (setting_getter_->GetStringList(SettingGetter::PROXY_IGNORE_HOSTS,
1492 &ignore_hosts_list)) {
[email protected]a8185d02010-06-11 00:19:501493 std::vector<std::string>::const_iterator it(ignore_hosts_list.begin());
[email protected]1a597192010-07-09 16:58:381494 for (; it != ignore_hosts_list.end(); ++it) {
[email protected]573c0502011-05-17 22:19:501495 if (setting_getter_->MatchHostsUsingSuffixMatching()) {
[email protected]1a597192010-07-09 16:58:381496 config->proxy_rules().bypass_rules.
1497 AddRuleFromStringUsingSuffixMatching(*it);
1498 } else {
1499 config->proxy_rules().bypass_rules.AddRuleFromString(*it);
1500 }
1501 }
[email protected]a8185d02010-06-11 00:19:501502 }
[email protected]861c6c62009-04-20 16:50:561503 // Note that there are no settings with semantics corresponding to
[email protected]1a597192010-07-09 16:58:381504 // bypass of local names in GNOME. In KDE, "<local>" is supported
1505 // as a hostname rule.
[email protected]861c6c62009-04-20 16:50:561506
[email protected]a48bf4a2010-06-14 18:24:531507 // KDE allows one to reverse the bypass rules.
[email protected]573c0502011-05-17 22:19:501508 config->proxy_rules().reverse_bypass =
1509 setting_getter_->BypassListIsReversed();
[email protected]a48bf4a2010-06-14 18:24:531510
[email protected]861c6c62009-04-20 16:50:561511 return true;
1512}
1513
[email protected]76b90d312010-08-03 03:00:501514ProxyConfigServiceLinux::Delegate::Delegate(base::Environment* env_var_getter)
[email protected]76722472012-05-24 08:26:461515 : env_var_getter_(env_var_getter) {
[email protected]573c0502011-05-17 22:19:501516 // Figure out which SettingGetterImpl to use, if any.
[email protected]6b0349ef2010-10-16 04:56:061517 switch (base::nix::GetDesktopEnvironment(env_var_getter)) {
1518 case base::nix::DESKTOP_ENVIRONMENT_GNOME:
[email protected]9e6c9bde2012-07-17 23:40:171519 case base::nix::DESKTOP_ENVIRONMENT_UNITY:
[email protected]8c20e3d2011-05-19 21:03:571520#if defined(USE_GIO)
1521 {
1522 scoped_ptr<SettingGetterImplGSettings> gs_getter(
1523 new SettingGetterImplGSettings());
1524 // We have to load symbols and check the GNOME version in use to decide
1525 // if we should use the gsettings getter. See LoadAndCheckVersion().
1526 if (gs_getter->LoadAndCheckVersion(env_var_getter))
1527 setting_getter_.reset(gs_getter.release());
1528 }
1529#endif
[email protected]6de53d42010-11-09 07:33:191530#if defined(USE_GCONF)
[email protected]8c20e3d2011-05-19 21:03:571531 // Fall back on gconf if gsettings is unavailable or incorrect.
1532 if (!setting_getter_.get())
1533 setting_getter_.reset(new SettingGetterImplGConf());
[email protected]6de53d42010-11-09 07:33:191534#endif
[email protected]d7395e732009-08-28 23:13:431535 break;
[email protected]6b0349ef2010-10-16 04:56:061536 case base::nix::DESKTOP_ENVIRONMENT_KDE3:
1537 case base::nix::DESKTOP_ENVIRONMENT_KDE4:
[email protected]573c0502011-05-17 22:19:501538 setting_getter_.reset(new SettingGetterImplKDE(env_var_getter));
[email protected]d7395e732009-08-28 23:13:431539 break;
[email protected]6b0349ef2010-10-16 04:56:061540 case base::nix::DESKTOP_ENVIRONMENT_XFCE:
1541 case base::nix::DESKTOP_ENVIRONMENT_OTHER:
[email protected]d7395e732009-08-28 23:13:431542 break;
1543 }
1544}
1545
[email protected]573c0502011-05-17 22:19:501546ProxyConfigServiceLinux::Delegate::Delegate(
1547 base::Environment* env_var_getter, SettingGetter* setting_getter)
[email protected]76722472012-05-24 08:26:461548 : env_var_getter_(env_var_getter), setting_getter_(setting_getter) {
[email protected]861c6c62009-04-20 16:50:561549}
1550
[email protected]d3066142011-05-10 02:36:201551void ProxyConfigServiceLinux::Delegate::SetUpAndFetchInitialConfig(
[email protected]76722472012-05-24 08:26:461552 base::SingleThreadTaskRunner* glib_thread_task_runner,
1553 base::SingleThreadTaskRunner* io_thread_task_runner,
[email protected]2da659e2013-05-23 20:51:341554 base::MessageLoopForIO* file_loop) {
[email protected]3e44697f2009-05-22 14:37:391555 // We should be running on the default glib main loop thread right
1556 // now. gconf can only be accessed from this thread.
[email protected]76722472012-05-24 08:26:461557 DCHECK(glib_thread_task_runner->BelongsToCurrentThread());
1558 glib_thread_task_runner_ = glib_thread_task_runner;
1559 io_thread_task_runner_ = io_thread_task_runner;
[email protected]3e44697f2009-05-22 14:37:391560
[email protected]76722472012-05-24 08:26:461561 // If we are passed a NULL |io_thread_task_runner| or |file_loop|,
1562 // then we don't set up proxy setting change notifications. This
1563 // should not be the usual case but is intended to simplify test
1564 // setups.
[email protected]90499482013-06-01 00:39:501565 if (!io_thread_task_runner_.get() || !file_loop)
[email protected]b30a3f52010-10-16 01:05:461566 VLOG(1) << "Monitoring of proxy setting changes is disabled";
[email protected]3e44697f2009-05-22 14:37:391567
1568 // Fetch and cache the current proxy config. The config is left in
[email protected]119655002010-07-23 06:02:401569 // cached_config_, where GetLatestProxyConfig() running on the IO thread
[email protected]3e44697f2009-05-22 14:37:391570 // will expect to find it. This is safe to do because we return
1571 // before this ProxyConfigServiceLinux is passed on to
1572 // the ProxyService.
[email protected]d6cb85b2009-07-23 22:10:531573
1574 // Note: It would be nice to prioritize environment variables
[email protected]92d2dc82010-04-08 17:49:591575 // and only fall back to gconf if env vars were unset. But
[email protected]d6cb85b2009-07-23 22:10:531576 // gnome-terminal "helpfully" sets http_proxy and no_proxy, and it
1577 // does so even if the proxy mode is set to auto, which would
1578 // mislead us.
1579
[email protected]3e44697f2009-05-22 14:37:391580 bool got_config = false;
[email protected]573c0502011-05-17 22:19:501581 if (setting_getter_.get() &&
[email protected]76722472012-05-24 08:26:461582 setting_getter_->Init(glib_thread_task_runner, file_loop) &&
[email protected]573c0502011-05-17 22:19:501583 GetConfigFromSettings(&cached_config_)) {
[email protected]d3066142011-05-10 02:36:201584 cached_config_.set_id(1); // Mark it as valid.
[email protected]db8ff912012-06-12 23:32:511585 cached_config_.set_source(setting_getter_->GetConfigSource());
[email protected]d3066142011-05-10 02:36:201586 VLOG(1) << "Obtained proxy settings from "
[email protected]db8ff912012-06-12 23:32:511587 << ProxyConfigSourceToString(cached_config_.source());
[email protected]d3066142011-05-10 02:36:201588
1589 // If gconf proxy mode is "none", meaning direct, then we take
1590 // that to be a valid config and will not check environment
1591 // variables. The alternative would have been to look for a proxy
1592 // whereever we can find one.
1593 got_config = true;
1594
1595 // Keep a copy of the config for use from this thread for
1596 // comparison with updated settings when we get notifications.
1597 reference_config_ = cached_config_;
1598 reference_config_.set_id(1); // Mark it as valid.
1599
1600 // We only set up notifications if we have IO and file loops available.
1601 // We do this after getting the initial configuration so that we don't have
1602 // to worry about cancelling it if the initial fetch above fails. Note that
1603 // setting up notifications has the side effect of simulating a change, so
1604 // that we won't lose any updates that may have happened after the initial
1605 // fetch and before setting up notifications. We'll detect the common case
1606 // of no changes in OnCheckProxyConfigSettings() (or sooner) and ignore it.
[email protected]76722472012-05-24 08:26:461607 if (io_thread_task_runner && file_loop) {
1608 scoped_refptr<base::SingleThreadTaskRunner> required_loop =
1609 setting_getter_->GetNotificationTaskRunner();
[email protected]90499482013-06-01 00:39:501610 if (!required_loop.get() || required_loop->BelongsToCurrentThread()) {
[email protected]d3066142011-05-10 02:36:201611 // In this case we are already on an acceptable thread.
1612 SetUpNotifications();
[email protected]d7395e732009-08-28 23:13:431613 } else {
[email protected]d3066142011-05-10 02:36:201614 // Post a task to set up notifications. We don't wait for success.
[email protected]6af889c2011-10-06 23:11:411615 required_loop->PostTask(FROM_HERE, base::Bind(
1616 &ProxyConfigServiceLinux::Delegate::SetUpNotifications, this));
[email protected]d6cb85b2009-07-23 22:10:531617 }
[email protected]d7395e732009-08-28 23:13:431618 }
[email protected]861c6c62009-04-20 16:50:561619 }
[email protected]d6cb85b2009-07-23 22:10:531620
[email protected]3e44697f2009-05-22 14:37:391621 if (!got_config) {
[email protected]d6cb85b2009-07-23 22:10:531622 // We fall back on environment variables.
[email protected]3e44697f2009-05-22 14:37:391623 //
[email protected]d3066142011-05-10 02:36:201624 // Consulting environment variables doesn't need to be done from the
1625 // default glib main loop, but it's a tiny enough amount of work.
[email protected]3e44697f2009-05-22 14:37:391626 if (GetConfigFromEnv(&cached_config_)) {
[email protected]db8ff912012-06-12 23:32:511627 cached_config_.set_source(PROXY_CONFIG_SOURCE_ENV);
[email protected]d3066142011-05-10 02:36:201628 cached_config_.set_id(1); // Mark it as valid.
[email protected]b30a3f52010-10-16 01:05:461629 VLOG(1) << "Obtained proxy settings from environment variables";
[email protected]3e44697f2009-05-22 14:37:391630 }
[email protected]861c6c62009-04-20 16:50:561631 }
[email protected]3e44697f2009-05-22 14:37:391632}
1633
[email protected]573c0502011-05-17 22:19:501634// Depending on the SettingGetter in use, this method will be called
[email protected]d3066142011-05-10 02:36:201635// on either the UI thread (GConf) or the file thread (KDE).
1636void ProxyConfigServiceLinux::Delegate::SetUpNotifications() {
[email protected]76722472012-05-24 08:26:461637 scoped_refptr<base::SingleThreadTaskRunner> required_loop =
1638 setting_getter_->GetNotificationTaskRunner();
[email protected]90499482013-06-01 00:39:501639 DCHECK(!required_loop.get() || required_loop->BelongsToCurrentThread());
[email protected]573c0502011-05-17 22:19:501640 if (!setting_getter_->SetUpNotifications(this))
[email protected]d3066142011-05-10 02:36:201641 LOG(ERROR) << "Unable to set up proxy configuration change notifications";
1642}
1643
[email protected]119655002010-07-23 06:02:401644void ProxyConfigServiceLinux::Delegate::AddObserver(Observer* observer) {
1645 observers_.AddObserver(observer);
1646}
1647
1648void ProxyConfigServiceLinux::Delegate::RemoveObserver(Observer* observer) {
1649 observers_.RemoveObserver(observer);
1650}
1651
[email protected]3a29593d2011-04-11 10:07:521652ProxyConfigService::ConfigAvailability
1653 ProxyConfigServiceLinux::Delegate::GetLatestProxyConfig(
1654 ProxyConfig* config) {
[email protected]3e44697f2009-05-22 14:37:391655 // This is called from the IO thread.
[email protected]90499482013-06-01 00:39:501656 DCHECK(!io_thread_task_runner_.get() ||
[email protected]76722472012-05-24 08:26:461657 io_thread_task_runner_->BelongsToCurrentThread());
[email protected]3e44697f2009-05-22 14:37:391658
1659 // Simply return the last proxy configuration that glib_default_loop
1660 // notified us of.
[email protected]db8ff912012-06-12 23:32:511661 if (cached_config_.is_valid()) {
1662 *config = cached_config_;
1663 } else {
1664 *config = ProxyConfig::CreateDirect();
1665 config->set_source(PROXY_CONFIG_SOURCE_SYSTEM_FAILED);
1666 }
[email protected]119655002010-07-23 06:02:401667
[email protected]3a29593d2011-04-11 10:07:521668 // We return CONFIG_VALID to indicate that *config was filled in. It is always
[email protected]119655002010-07-23 06:02:401669 // going to be available since we initialized eagerly on the UI thread.
1670 // TODO(eroman): do lazy initialization instead, so we no longer need
1671 // to construct ProxyConfigServiceLinux on the UI thread.
1672 // In which case, we may return false here.
[email protected]3a29593d2011-04-11 10:07:521673 return CONFIG_VALID;
[email protected]3e44697f2009-05-22 14:37:391674}
1675
[email protected]573c0502011-05-17 22:19:501676// Depending on the SettingGetter in use, this method will be called
[email protected]d7395e732009-08-28 23:13:431677// on either the UI thread (GConf) or the file thread (KDE).
[email protected]3e44697f2009-05-22 14:37:391678void ProxyConfigServiceLinux::Delegate::OnCheckProxyConfigSettings() {
[email protected]76722472012-05-24 08:26:461679 scoped_refptr<base::SingleThreadTaskRunner> required_loop =
1680 setting_getter_->GetNotificationTaskRunner();
[email protected]90499482013-06-01 00:39:501681 DCHECK(!required_loop.get() || required_loop->BelongsToCurrentThread());
[email protected]3e44697f2009-05-22 14:37:391682 ProxyConfig new_config;
[email protected]573c0502011-05-17 22:19:501683 bool valid = GetConfigFromSettings(&new_config);
[email protected]3e44697f2009-05-22 14:37:391684 if (valid)
1685 new_config.set_id(1); // mark it as valid
1686
[email protected]119655002010-07-23 06:02:401687 // See if it is different from what we had before.
[email protected]3e44697f2009-05-22 14:37:391688 if (new_config.is_valid() != reference_config_.is_valid() ||
1689 !new_config.Equals(reference_config_)) {
[email protected]76722472012-05-24 08:26:461690 // Post a task to the IO thread with the new configuration, so it can
[email protected]3e44697f2009-05-22 14:37:391691 // update |cached_config_|.
[email protected]76722472012-05-24 08:26:461692 io_thread_task_runner_->PostTask(FROM_HERE, base::Bind(
[email protected]6af889c2011-10-06 23:11:411693 &ProxyConfigServiceLinux::Delegate::SetNewProxyConfig,
1694 this, new_config));
[email protected]d1f9d472009-08-13 19:59:301695 // Update the thread-private copy in |reference_config_| as well.
1696 reference_config_ = new_config;
[email protected]d3066142011-05-10 02:36:201697 } else {
1698 VLOG(1) << "Detected no-op change to proxy settings. Doing nothing.";
[email protected]3e44697f2009-05-22 14:37:391699 }
1700}
1701
1702void ProxyConfigServiceLinux::Delegate::SetNewProxyConfig(
1703 const ProxyConfig& new_config) {
[email protected]76722472012-05-24 08:26:461704 DCHECK(io_thread_task_runner_->BelongsToCurrentThread());
[email protected]b30a3f52010-10-16 01:05:461705 VLOG(1) << "Proxy configuration changed";
[email protected]3e44697f2009-05-22 14:37:391706 cached_config_ = new_config;
[email protected]3a29593d2011-04-11 10:07:521707 FOR_EACH_OBSERVER(
1708 Observer, observers_,
1709 OnProxyConfigChanged(new_config, ProxyConfigService::CONFIG_VALID));
[email protected]3e44697f2009-05-22 14:37:391710}
1711
1712void ProxyConfigServiceLinux::Delegate::PostDestroyTask() {
[email protected]573c0502011-05-17 22:19:501713 if (!setting_getter_.get())
[email protected]d7395e732009-08-28 23:13:431714 return;
[email protected]76722472012-05-24 08:26:461715 scoped_refptr<base::SingleThreadTaskRunner> shutdown_loop =
1716 setting_getter_->GetNotificationTaskRunner();
[email protected]90499482013-06-01 00:39:501717 if (!shutdown_loop.get() || shutdown_loop->BelongsToCurrentThread()) {
[email protected]3e44697f2009-05-22 14:37:391718 // Already on the right thread, call directly.
1719 // This is the case for the unittests.
1720 OnDestroy();
1721 } else {
[email protected]d7395e732009-08-28 23:13:431722 // Post to shutdown thread. Note that on browser shutdown, we may quit
1723 // this MessageLoop and exit the program before ever running this.
[email protected]6af889c2011-10-06 23:11:411724 shutdown_loop->PostTask(FROM_HERE, base::Bind(
1725 &ProxyConfigServiceLinux::Delegate::OnDestroy, this));
[email protected]3e44697f2009-05-22 14:37:391726 }
1727}
1728void ProxyConfigServiceLinux::Delegate::OnDestroy() {
[email protected]76722472012-05-24 08:26:461729 scoped_refptr<base::SingleThreadTaskRunner> shutdown_loop =
1730 setting_getter_->GetNotificationTaskRunner();
[email protected]90499482013-06-01 00:39:501731 DCHECK(!shutdown_loop.get() || shutdown_loop->BelongsToCurrentThread());
[email protected]573c0502011-05-17 22:19:501732 setting_getter_->ShutDown();
[email protected]3e44697f2009-05-22 14:37:391733}
1734
1735ProxyConfigServiceLinux::ProxyConfigServiceLinux()
[email protected]76b90d312010-08-03 03:00:501736 : delegate_(new Delegate(base::Environment::Create())) {
[email protected]3e44697f2009-05-22 14:37:391737}
1738
[email protected]8e1845e12010-09-15 19:22:241739ProxyConfigServiceLinux::~ProxyConfigServiceLinux() {
1740 delegate_->PostDestroyTask();
1741}
1742
[email protected]3e44697f2009-05-22 14:37:391743ProxyConfigServiceLinux::ProxyConfigServiceLinux(
[email protected]76b90d312010-08-03 03:00:501744 base::Environment* env_var_getter)
[email protected]9a3d8d42009-09-03 17:01:461745 : delegate_(new Delegate(env_var_getter)) {
1746}
1747
1748ProxyConfigServiceLinux::ProxyConfigServiceLinux(
[email protected]573c0502011-05-17 22:19:501749 base::Environment* env_var_getter, SettingGetter* setting_getter)
1750 : delegate_(new Delegate(env_var_getter, setting_getter)) {
[email protected]861c6c62009-04-20 16:50:561751}
1752
[email protected]e4be2dd2010-12-14 00:44:391753void ProxyConfigServiceLinux::AddObserver(Observer* observer) {
1754 delegate_->AddObserver(observer);
1755}
1756
1757void ProxyConfigServiceLinux::RemoveObserver(Observer* observer) {
1758 delegate_->RemoveObserver(observer);
1759}
1760
[email protected]3a29593d2011-04-11 10:07:521761ProxyConfigService::ConfigAvailability
1762 ProxyConfigServiceLinux::GetLatestProxyConfig(ProxyConfig* config) {
[email protected]e4be2dd2010-12-14 00:44:391763 return delegate_->GetLatestProxyConfig(config);
1764}
1765
[email protected]861c6c62009-04-20 16:50:561766} // namespace net