blob: 523a55eff811cc51ac003602656774717270e5e0 [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]d7395e732009-08-28 23:13:4326#include "base/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]528c56d2010-07-30 19:28:4429#include "base/string_number_conversions.h"
[email protected]861c6c62009-04-20 16:50:5630#include "base/string_util.h"
[email protected]f4ebe772013-02-02 00:21:3931#include "base/strings/string_tokenizer.h"
[email protected]9a8c4022011-01-25 14:25:3332#include "base/threading/thread_restrictions.h"
[email protected]d7395e732009-08-28 23:13:4333#include "base/timer.h"
[email protected]861c6c62009-04-20 16:50:5634#include "googleurl/src/url_canon.h"
35#include "net/base/net_errors.h"
36#include "net/http/http_util.h"
37#include "net/proxy/proxy_config.h"
38#include "net/proxy/proxy_server.h"
39
[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;
140 config->proxy_rules().single_proxy = 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]ed4ed0f2010-02-24 00:20:48144 config->proxy_rules().proxy_for_http = 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]ed4ed0f2010-02-24 00:20:48152 config->proxy_rules().proxy_for_https = proxy_server;
[email protected]861c6c62009-04-20 16:50:56153 bool have_ftp = GetProxyFromEnvVar("ftp_proxy", &proxy_server);
154 if (have_ftp)
[email protected]ed4ed0f2010-02-24 00:20:48155 config->proxy_rules().proxy_for_ftp = proxy_server;
[email protected]861c6c62009-04-20 16:50:56156 if (have_http || have_https || have_ftp) {
157 // mustn't change type unless some rules are actually set.
[email protected]ed4ed0f2010-02-24 00:20:48158 config->proxy_rules().type =
159 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME;
[email protected]861c6c62009-04-20 16:50:56160 }
161 }
[email protected]ed4ed0f2010-02-24 00:20:48162 if (config->proxy_rules().empty()) {
[email protected]861c6c62009-04-20 16:50:56163 // If the above were not defined, try for socks.
[email protected]e8c50812010-09-28 00:16:17164 // For environment variables, we default to version 5, per the gnome
165 // documentation: http://library.gnome.org/devel/gnet/stable/gnet-socks.html
166 ProxyServer::Scheme scheme = ProxyServer::SCHEME_SOCKS5;
[email protected]861c6c62009-04-20 16:50:56167 std::string env_version;
[email protected]3ba7e082010-08-07 02:57:59168 if (env_var_getter_->GetVar("SOCKS_VERSION", &env_version)
[email protected]e8c50812010-09-28 00:16:17169 && env_version == "4")
170 scheme = ProxyServer::SCHEME_SOCKS4;
[email protected]861c6c62009-04-20 16:50:56171 if (GetProxyFromEnvVarForScheme("SOCKS_SERVER", scheme, &proxy_server)) {
[email protected]ed4ed0f2010-02-24 00:20:48172 config->proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY;
173 config->proxy_rules().single_proxy = proxy_server;
[email protected]861c6c62009-04-20 16:50:56174 }
175 }
176 // Look for the proxy bypass list.
177 std::string no_proxy;
[email protected]3ba7e082010-08-07 02:57:59178 env_var_getter_->GetVar("no_proxy", &no_proxy);
[email protected]ed4ed0f2010-02-24 00:20:48179 if (config->proxy_rules().empty()) {
[email protected]861c6c62009-04-20 16:50:56180 // Having only "no_proxy" set, presumably to "*", makes it
181 // explicit that env vars do specify a configuration: having no
182 // rules specified only means the user explicitly asks for direct
183 // connections.
184 return !no_proxy.empty();
185 }
[email protected]7541206c2010-02-19 20:24:06186 // Note that this uses "suffix" matching. So a bypass of "google.com"
187 // is understood to mean a bypass of "*google.com".
[email protected]ed4ed0f2010-02-24 00:20:48188 config->proxy_rules().bypass_rules.ParseFromStringUsingSuffixMatching(
189 no_proxy);
[email protected]861c6c62009-04-20 16:50:56190 return true;
191}
192
193namespace {
194
[email protected]d7395e732009-08-28 23:13:43195const int kDebounceTimeoutMilliseconds = 250;
[email protected]3e44697f2009-05-22 14:37:39196
[email protected]6de53d42010-11-09 07:33:19197#if defined(USE_GCONF)
[email protected]573c0502011-05-17 22:19:50198// This setting getter uses gconf, as used in GNOME 2 and some GNOME 3 desktops.
199class SettingGetterImplGConf : public ProxyConfigServiceLinux::SettingGetter {
[email protected]861c6c62009-04-20 16:50:56200 public:
[email protected]573c0502011-05-17 22:19:50201 SettingGetterImplGConf()
[email protected]b160df32012-02-06 20:39:41202 : client_(NULL), system_proxy_id_(0), system_http_proxy_id_(0),
[email protected]76722472012-05-24 08:26:46203 notify_delegate_(NULL) {
[email protected]b160df32012-02-06 20:39:41204 }
[email protected]3e44697f2009-05-22 14:37:39205
[email protected]573c0502011-05-17 22:19:50206 virtual ~SettingGetterImplGConf() {
[email protected]3e44697f2009-05-22 14:37:39207 // client_ should have been released before now, from
[email protected]f5b13442009-07-13 15:23:59208 // Delegate::OnDestroy(), while running on the UI thread. However
[email protected]b160df32012-02-06 20:39:41209 // on exiting the process, it may happen that Delegate::OnDestroy()
210 // task is left pending on the glib loop after the loop was quit,
211 // and pending tasks may then be deleted without being run.
[email protected]f5b13442009-07-13 15:23:59212 if (client_) {
213 // gconf client was not cleaned up.
[email protected]76722472012-05-24 08:26:46214 if (task_runner_->BelongsToCurrentThread()) {
[email protected]f5b13442009-07-13 15:23:59215 // We are on the UI thread so we can clean it safely. This is
216 // the case at least for ui_tests running under Valgrind in
217 // bug 16076.
[email protected]573c0502011-05-17 22:19:50218 VLOG(1) << "~SettingGetterImplGConf: releasing gconf client";
[email protected]d3066142011-05-10 02:36:20219 ShutDown();
[email protected]f5b13442009-07-13 15:23:59220 } else {
[email protected]ed348992011-09-19 20:27:57221 // This is very bad! We are deleting the setting getter but we're not on
222 // the UI thread. This is not supposed to happen: the setting getter is
223 // owned by the proxy config service's delegate, which is supposed to be
224 // destroyed on the UI thread only. We will get change notifications to
225 // a deleted object if we continue here, so fail now.
226 LOG(FATAL) << "~SettingGetterImplGConf: deleting on wrong thread!";
[email protected]f5b13442009-07-13 15:23:59227 }
228 }
[email protected]3e44697f2009-05-22 14:37:39229 DCHECK(!client_);
[email protected]861c6c62009-04-20 16:50:56230 }
231
[email protected]76722472012-05-24 08:26:46232 virtual bool Init(base::SingleThreadTaskRunner* glib_thread_task_runner,
[email protected]c4c1b482011-07-22 17:24:26233 MessageLoopForIO* file_loop) OVERRIDE {
[email protected]76722472012-05-24 08:26:46234 DCHECK(glib_thread_task_runner->BelongsToCurrentThread());
[email protected]3e44697f2009-05-22 14:37:39235 DCHECK(!client_);
[email protected]76722472012-05-24 08:26:46236 DCHECK(!task_runner_);
237 task_runner_ = glib_thread_task_runner;
[email protected]3e44697f2009-05-22 14:37:39238 client_ = gconf_client_get_default();
[email protected]861c6c62009-04-20 16:50:56239 if (!client_) {
[email protected]861c6c62009-04-20 16:50:56240 // It's not clear whether/when this can return NULL.
[email protected]3e44697f2009-05-22 14:37:39241 LOG(ERROR) << "Unable to create a gconf client";
[email protected]76722472012-05-24 08:26:46242 task_runner_ = NULL;
[email protected]3e44697f2009-05-22 14:37:39243 return false;
[email protected]861c6c62009-04-20 16:50:56244 }
[email protected]3e44697f2009-05-22 14:37:39245 GError* error = NULL;
[email protected]b160df32012-02-06 20:39:41246 bool added_system_proxy = false;
[email protected]3e44697f2009-05-22 14:37:39247 // We need to add the directories for which we'll be asking
[email protected]b160df32012-02-06 20:39:41248 // for notifications, and we might as well ask to preload them.
249 // These need to be removed again in ShutDown(); we are careful
250 // here to only leave client_ non-NULL if both have been added.
[email protected]3e44697f2009-05-22 14:37:39251 gconf_client_add_dir(client_, "/system/proxy",
252 GCONF_CLIENT_PRELOAD_ONELEVEL, &error);
253 if (error == NULL) {
[email protected]b160df32012-02-06 20:39:41254 added_system_proxy = true;
[email protected]3e44697f2009-05-22 14:37:39255 gconf_client_add_dir(client_, "/system/http_proxy",
256 GCONF_CLIENT_PRELOAD_ONELEVEL, &error);
257 }
258 if (error != NULL) {
259 LOG(ERROR) << "Error requesting gconf directory: " << error->message;
260 g_error_free(error);
[email protected]b160df32012-02-06 20:39:41261 if (added_system_proxy)
262 gconf_client_remove_dir(client_, "/system/proxy", NULL);
263 g_object_unref(client_);
264 client_ = NULL;
[email protected]76722472012-05-24 08:26:46265 task_runner_ = NULL;
[email protected]3e44697f2009-05-22 14:37:39266 return false;
267 }
268 return true;
269 }
270
[email protected]749bf5c2012-09-17 03:15:21271 virtual void ShutDown() OVERRIDE {
[email protected]3e44697f2009-05-22 14:37:39272 if (client_) {
[email protected]76722472012-05-24 08:26:46273 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]b160df32012-02-06 20:39:41274 // We must explicitly disable gconf notifications here, because the gconf
275 // client will be shared between all setting getters, and they do not all
276 // have the same lifetimes. (For instance, incognito sessions get their
277 // own, which is destroyed when the session ends.)
278 gconf_client_notify_remove(client_, system_http_proxy_id_);
279 gconf_client_notify_remove(client_, system_proxy_id_);
280 gconf_client_remove_dir(client_, "/system/http_proxy", NULL);
281 gconf_client_remove_dir(client_, "/system/proxy", NULL);
[email protected]3e44697f2009-05-22 14:37:39282 g_object_unref(client_);
283 client_ = NULL;
[email protected]76722472012-05-24 08:26:46284 task_runner_ = NULL;
[email protected]3e44697f2009-05-22 14:37:39285 }
286 }
287
[email protected]749bf5c2012-09-17 03:15:21288 virtual bool SetUpNotifications(
289 ProxyConfigServiceLinux::Delegate* delegate) OVERRIDE {
[email protected]3e44697f2009-05-22 14:37:39290 DCHECK(client_);
[email protected]76722472012-05-24 08:26:46291 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]3e44697f2009-05-22 14:37:39292 GError* error = NULL;
[email protected]d7395e732009-08-28 23:13:43293 notify_delegate_ = delegate;
[email protected]b160df32012-02-06 20:39:41294 // We have to keep track of the IDs returned by gconf_client_notify_add() so
295 // that we can remove them in ShutDown(). (Otherwise, notifications will be
296 // delivered to this object after it is deleted, which is bad, m'kay?)
297 system_proxy_id_ = gconf_client_notify_add(
[email protected]3e44697f2009-05-22 14:37:39298 client_, "/system/proxy",
[email protected]d7395e732009-08-28 23:13:43299 OnGConfChangeNotification, this,
[email protected]3e44697f2009-05-22 14:37:39300 NULL, &error);
301 if (error == NULL) {
[email protected]b160df32012-02-06 20:39:41302 system_http_proxy_id_ = gconf_client_notify_add(
[email protected]3e44697f2009-05-22 14:37:39303 client_, "/system/http_proxy",
[email protected]d7395e732009-08-28 23:13:43304 OnGConfChangeNotification, this,
[email protected]3e44697f2009-05-22 14:37:39305 NULL, &error);
306 }
307 if (error != NULL) {
308 LOG(ERROR) << "Error requesting gconf notifications: " << error->message;
309 g_error_free(error);
[email protected]d3066142011-05-10 02:36:20310 ShutDown();
[email protected]3e44697f2009-05-22 14:37:39311 return false;
312 }
[email protected]d3066142011-05-10 02:36:20313 // Simulate a change to avoid possibly losing updates before this point.
314 OnChangeNotification();
[email protected]3e44697f2009-05-22 14:37:39315 return true;
[email protected]861c6c62009-04-20 16:50:56316 }
317
[email protected]76722472012-05-24 08:26:46318 virtual base::SingleThreadTaskRunner* GetNotificationTaskRunner() OVERRIDE {
319 return task_runner_;
[email protected]d7395e732009-08-28 23:13:43320 }
321
[email protected]db8ff912012-06-12 23:32:51322 virtual ProxyConfigSource GetConfigSource() OVERRIDE {
323 return PROXY_CONFIG_SOURCE_GCONF;
[email protected]d7395e732009-08-28 23:13:43324 }
325
[email protected]c4c1b482011-07-22 17:24:26326 virtual bool GetString(StringSetting key, std::string* result) OVERRIDE {
[email protected]573c0502011-05-17 22:19:50327 switch (key) {
328 case PROXY_MODE:
329 return GetStringByPath("/system/proxy/mode", result);
330 case PROXY_AUTOCONF_URL:
331 return GetStringByPath("/system/proxy/autoconfig_url", result);
332 case PROXY_HTTP_HOST:
333 return GetStringByPath("/system/http_proxy/host", result);
334 case PROXY_HTTPS_HOST:
335 return GetStringByPath("/system/proxy/secure_host", result);
336 case PROXY_FTP_HOST:
337 return GetStringByPath("/system/proxy/ftp_host", result);
338 case PROXY_SOCKS_HOST:
339 return GetStringByPath("/system/proxy/socks_host", result);
[email protected]573c0502011-05-17 22:19:50340 }
[email protected]6b5fe742011-05-20 21:46:48341 return false; // Placate compiler.
[email protected]573c0502011-05-17 22:19:50342 }
[email protected]c4c1b482011-07-22 17:24:26343 virtual bool GetBool(BoolSetting key, bool* result) OVERRIDE {
[email protected]573c0502011-05-17 22:19:50344 switch (key) {
345 case PROXY_USE_HTTP_PROXY:
346 return GetBoolByPath("/system/http_proxy/use_http_proxy", result);
347 case PROXY_USE_SAME_PROXY:
348 return GetBoolByPath("/system/http_proxy/use_same_proxy", result);
349 case PROXY_USE_AUTHENTICATION:
350 return GetBoolByPath("/system/http_proxy/use_authentication", result);
[email protected]573c0502011-05-17 22:19:50351 }
[email protected]6b5fe742011-05-20 21:46:48352 return false; // Placate compiler.
[email protected]573c0502011-05-17 22:19:50353 }
[email protected]c4c1b482011-07-22 17:24:26354 virtual bool GetInt(IntSetting key, int* result) OVERRIDE {
[email protected]573c0502011-05-17 22:19:50355 switch (key) {
356 case PROXY_HTTP_PORT:
357 return GetIntByPath("/system/http_proxy/port", result);
358 case PROXY_HTTPS_PORT:
359 return GetIntByPath("/system/proxy/secure_port", result);
360 case PROXY_FTP_PORT:
361 return GetIntByPath("/system/proxy/ftp_port", result);
362 case PROXY_SOCKS_PORT:
363 return GetIntByPath("/system/proxy/socks_port", result);
[email protected]573c0502011-05-17 22:19:50364 }
[email protected]6b5fe742011-05-20 21:46:48365 return false; // Placate compiler.
[email protected]573c0502011-05-17 22:19:50366 }
[email protected]6b5fe742011-05-20 21:46:48367 virtual bool GetStringList(StringListSetting key,
[email protected]c4c1b482011-07-22 17:24:26368 std::vector<std::string>* result) OVERRIDE {
[email protected]573c0502011-05-17 22:19:50369 switch (key) {
370 case PROXY_IGNORE_HOSTS:
371 return GetStringListByPath("/system/http_proxy/ignore_hosts", result);
[email protected]573c0502011-05-17 22:19:50372 }
[email protected]6b5fe742011-05-20 21:46:48373 return false; // Placate compiler.
[email protected]573c0502011-05-17 22:19:50374 }
375
[email protected]c4c1b482011-07-22 17:24:26376 virtual bool BypassListIsReversed() OVERRIDE {
[email protected]573c0502011-05-17 22:19:50377 // This is a KDE-specific setting.
378 return false;
379 }
380
[email protected]c4c1b482011-07-22 17:24:26381 virtual bool MatchHostsUsingSuffixMatching() OVERRIDE {
[email protected]573c0502011-05-17 22:19:50382 return false;
383 }
384
385 private:
386 bool GetStringByPath(const char* key, std::string* result) {
[email protected]3e44697f2009-05-22 14:37:39387 DCHECK(client_);
[email protected]76722472012-05-24 08:26:46388 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]861c6c62009-04-20 16:50:56389 GError* error = NULL;
390 gchar* value = gconf_client_get_string(client_, key, &error);
391 if (HandleGError(error, key))
392 return false;
393 if (!value)
394 return false;
395 *result = value;
396 g_free(value);
397 return true;
398 }
[email protected]573c0502011-05-17 22:19:50399 bool GetBoolByPath(const char* key, bool* result) {
[email protected]3e44697f2009-05-22 14:37:39400 DCHECK(client_);
[email protected]76722472012-05-24 08:26:46401 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]861c6c62009-04-20 16:50:56402 GError* error = NULL;
403 // We want to distinguish unset values from values defaulting to
404 // false. For that we need to use the type-generic
405 // gconf_client_get() rather than gconf_client_get_bool().
406 GConfValue* gconf_value = gconf_client_get(client_, key, &error);
407 if (HandleGError(error, key))
408 return false;
409 if (!gconf_value) {
410 // Unset.
411 return false;
412 }
413 if (gconf_value->type != GCONF_VALUE_BOOL) {
414 gconf_value_free(gconf_value);
415 return false;
416 }
417 gboolean bool_value = gconf_value_get_bool(gconf_value);
418 *result = static_cast<bool>(bool_value);
419 gconf_value_free(gconf_value);
420 return true;
421 }
[email protected]573c0502011-05-17 22:19:50422 bool GetIntByPath(const char* key, int* result) {
[email protected]3e44697f2009-05-22 14:37:39423 DCHECK(client_);
[email protected]76722472012-05-24 08:26:46424 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]861c6c62009-04-20 16:50:56425 GError* error = NULL;
426 int value = gconf_client_get_int(client_, key, &error);
427 if (HandleGError(error, key))
428 return false;
429 // We don't bother to distinguish an unset value because callers
430 // don't care. 0 is returned if unset.
431 *result = value;
432 return true;
433 }
[email protected]573c0502011-05-17 22:19:50434 bool GetStringListByPath(const char* key, std::vector<std::string>* result) {
[email protected]3e44697f2009-05-22 14:37:39435 DCHECK(client_);
[email protected]76722472012-05-24 08:26:46436 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]861c6c62009-04-20 16:50:56437 GError* error = NULL;
438 GSList* list = gconf_client_get_list(client_, key,
439 GCONF_VALUE_STRING, &error);
440 if (HandleGError(error, key))
441 return false;
[email protected]8c20e3d2011-05-19 21:03:57442 if (!list)
[email protected]861c6c62009-04-20 16:50:56443 return false;
[email protected]861c6c62009-04-20 16:50:56444 for (GSList *it = list; it; it = it->next) {
445 result->push_back(static_cast<char*>(it->data));
446 g_free(it->data);
447 }
448 g_slist_free(list);
449 return true;
450 }
451
[email protected]861c6c62009-04-20 16:50:56452 // Logs and frees a glib error. Returns false if there was no error
453 // (error is NULL).
454 bool HandleGError(GError* error, const char* key) {
455 if (error != NULL) {
[email protected]3e44697f2009-05-22 14:37:39456 LOG(ERROR) << "Error getting gconf value for " << key
457 << ": " << error->message;
[email protected]861c6c62009-04-20 16:50:56458 g_error_free(error);
459 return true;
460 }
461 return false;
462 }
463
[email protected]d7395e732009-08-28 23:13:43464 // This is the callback from the debounce timer.
465 void OnDebouncedNotification() {
[email protected]76722472012-05-24 08:26:46466 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]961ac942011-04-28 18:18:14467 CHECK(notify_delegate_);
[email protected]d7395e732009-08-28 23:13:43468 // Forward to a method on the proxy config service delegate object.
469 notify_delegate_->OnCheckProxyConfigSettings();
470 }
471
472 void OnChangeNotification() {
473 // We don't use Reset() because the timer may not yet be running.
474 // (In that case Stop() is a no-op.)
475 debounce_timer_.Stop();
[email protected]d323a172011-09-02 18:23:02476 debounce_timer_.Start(FROM_HERE,
[email protected]8c20e3d2011-05-19 21:03:57477 base::TimeDelta::FromMilliseconds(kDebounceTimeoutMilliseconds),
478 this, &SettingGetterImplGConf::OnDebouncedNotification);
[email protected]d7395e732009-08-28 23:13:43479 }
480
[email protected]8c20e3d2011-05-19 21:03:57481 // gconf notification callback, dispatched on the default glib main loop.
482 static void OnGConfChangeNotification(GConfClient* client, guint cnxn_id,
483 GConfEntry* entry, gpointer user_data) {
[email protected]b30a3f52010-10-16 01:05:46484 VLOG(1) << "gconf change notification for key "
485 << gconf_entry_get_key(entry);
[email protected]d7395e732009-08-28 23:13:43486 // We don't track which key has changed, just that something did change.
[email protected]573c0502011-05-17 22:19:50487 SettingGetterImplGConf* setting_getter =
488 reinterpret_cast<SettingGetterImplGConf*>(user_data);
[email protected]d7395e732009-08-28 23:13:43489 setting_getter->OnChangeNotification();
490 }
491
[email protected]861c6c62009-04-20 16:50:56492 GConfClient* client_;
[email protected]b160df32012-02-06 20:39:41493 // These ids are the values returned from gconf_client_notify_add(), which we
494 // will need in order to later call gconf_client_notify_remove().
495 guint system_proxy_id_;
496 guint system_http_proxy_id_;
497
[email protected]d7395e732009-08-28 23:13:43498 ProxyConfigServiceLinux::Delegate* notify_delegate_;
[email protected]573c0502011-05-17 22:19:50499 base::OneShotTimer<SettingGetterImplGConf> debounce_timer_;
[email protected]861c6c62009-04-20 16:50:56500
[email protected]76722472012-05-24 08:26:46501 // Task runner for the thread that we make gconf calls on. It should
[email protected]3e44697f2009-05-22 14:37:39502 // be the UI thread and all our methods should be called on this
503 // thread. Only for assertions.
[email protected]76722472012-05-24 08:26:46504 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
[email protected]3e44697f2009-05-22 14:37:39505
[email protected]573c0502011-05-17 22:19:50506 DISALLOW_COPY_AND_ASSIGN(SettingGetterImplGConf);
[email protected]d7395e732009-08-28 23:13:43507};
[email protected]6de53d42010-11-09 07:33:19508#endif // defined(USE_GCONF)
[email protected]d7395e732009-08-28 23:13:43509
[email protected]8c20e3d2011-05-19 21:03:57510#if defined(USE_GIO)
511// This setting getter uses gsettings, as used in most GNOME 3 desktops.
512class SettingGetterImplGSettings
513 : public ProxyConfigServiceLinux::SettingGetter {
514 public:
[email protected]0e14e87d2011-06-21 21:24:19515 SettingGetterImplGSettings() :
[email protected]0e14e87d2011-06-21 21:24:19516 client_(NULL),
517 http_client_(NULL),
518 https_client_(NULL),
519 ftp_client_(NULL),
520 socks_client_(NULL),
[email protected]76722472012-05-24 08:26:46521 notify_delegate_(NULL) {
[email protected]8c20e3d2011-05-19 21:03:57522 }
523
524 virtual ~SettingGetterImplGSettings() {
525 // client_ should have been released before now, from
526 // Delegate::OnDestroy(), while running on the UI thread. However
527 // on exiting the process, it may happen that
528 // Delegate::OnDestroy() task is left pending on the glib loop
529 // after the loop was quit, and pending tasks may then be deleted
530 // without being run.
531 if (client_) {
532 // gconf client was not cleaned up.
[email protected]76722472012-05-24 08:26:46533 if (task_runner_->BelongsToCurrentThread()) {
[email protected]8c20e3d2011-05-19 21:03:57534 // We are on the UI thread so we can clean it safely. This is
535 // the case at least for ui_tests running under Valgrind in
536 // bug 16076.
537 VLOG(1) << "~SettingGetterImplGSettings: releasing gsettings client";
538 ShutDown();
539 } else {
540 LOG(WARNING) << "~SettingGetterImplGSettings: leaking gsettings client";
541 client_ = NULL;
542 }
543 }
544 DCHECK(!client_);
[email protected]8c20e3d2011-05-19 21:03:57545 }
546
[email protected]4cf80f0b2011-05-20 20:30:26547 bool SchemaExists(const char* schema_name) {
[email protected]3fc24f52012-11-30 21:22:34548 const gchar* const* schemas = libgio_loader_.g_settings_list_schemas();
[email protected]4cf80f0b2011-05-20 20:30:26549 while (*schemas) {
[email protected]a099f3ae2011-08-16 21:06:58550 if (strcmp(schema_name, static_cast<const char*>(*schemas)) == 0)
[email protected]4cf80f0b2011-05-20 20:30:26551 return true;
552 schemas++;
553 }
554 return false;
555 }
556
[email protected]8c20e3d2011-05-19 21:03:57557 // LoadAndCheckVersion() must be called *before* Init()!
558 bool LoadAndCheckVersion(base::Environment* env);
559
[email protected]76722472012-05-24 08:26:46560 virtual bool Init(base::SingleThreadTaskRunner* glib_thread_task_runner,
[email protected]c4c1b482011-07-22 17:24:26561 MessageLoopForIO* file_loop) OVERRIDE {
[email protected]76722472012-05-24 08:26:46562 DCHECK(glib_thread_task_runner->BelongsToCurrentThread());
[email protected]8c20e3d2011-05-19 21:03:57563 DCHECK(!client_);
[email protected]76722472012-05-24 08:26:46564 DCHECK(!task_runner_);
[email protected]4cf80f0b2011-05-20 20:30:26565
566 if (!SchemaExists("org.gnome.system.proxy") ||
[email protected]3fc24f52012-11-30 21:22:34567 !(client_ = libgio_loader_.g_settings_new("org.gnome.system.proxy"))) {
[email protected]8c20e3d2011-05-19 21:03:57568 // It's not clear whether/when this can return NULL.
569 LOG(ERROR) << "Unable to create a gsettings client";
570 return false;
571 }
[email protected]76722472012-05-24 08:26:46572 task_runner_ = glib_thread_task_runner;
[email protected]8c20e3d2011-05-19 21:03:57573 // We assume these all work if the above call worked.
[email protected]3fc24f52012-11-30 21:22:34574 http_client_ = libgio_loader_.g_settings_get_child(client_, "http");
575 https_client_ = libgio_loader_.g_settings_get_child(client_, "https");
576 ftp_client_ = libgio_loader_.g_settings_get_child(client_, "ftp");
577 socks_client_ = libgio_loader_.g_settings_get_child(client_, "socks");
[email protected]8c20e3d2011-05-19 21:03:57578 DCHECK(http_client_ && https_client_ && ftp_client_ && socks_client_);
579 return true;
580 }
581
[email protected]749bf5c2012-09-17 03:15:21582 virtual void ShutDown() OVERRIDE {
[email protected]8c20e3d2011-05-19 21:03:57583 if (client_) {
[email protected]76722472012-05-24 08:26:46584 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]8c20e3d2011-05-19 21:03:57585 // This also disables gsettings notifications.
586 g_object_unref(socks_client_);
587 g_object_unref(ftp_client_);
588 g_object_unref(https_client_);
589 g_object_unref(http_client_);
590 g_object_unref(client_);
591 // We only need to null client_ because it's the only one that we check.
592 client_ = NULL;
[email protected]76722472012-05-24 08:26:46593 task_runner_ = NULL;
[email protected]8c20e3d2011-05-19 21:03:57594 }
595 }
596
[email protected]749bf5c2012-09-17 03:15:21597 virtual bool SetUpNotifications(
598 ProxyConfigServiceLinux::Delegate* delegate) OVERRIDE {
[email protected]8c20e3d2011-05-19 21:03:57599 DCHECK(client_);
[email protected]76722472012-05-24 08:26:46600 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]8c20e3d2011-05-19 21:03:57601 notify_delegate_ = delegate;
602 // We could watch for the change-event signal instead of changed, but
603 // since we have to watch more than one object, we'd still have to
604 // debounce change notifications. This is conceptually simpler.
605 g_signal_connect(G_OBJECT(client_), "changed",
606 G_CALLBACK(OnGSettingsChangeNotification), this);
607 g_signal_connect(G_OBJECT(http_client_), "changed",
608 G_CALLBACK(OnGSettingsChangeNotification), this);
609 g_signal_connect(G_OBJECT(https_client_), "changed",
610 G_CALLBACK(OnGSettingsChangeNotification), this);
611 g_signal_connect(G_OBJECT(ftp_client_), "changed",
612 G_CALLBACK(OnGSettingsChangeNotification), this);
613 g_signal_connect(G_OBJECT(socks_client_), "changed",
614 G_CALLBACK(OnGSettingsChangeNotification), this);
615 // Simulate a change to avoid possibly losing updates before this point.
616 OnChangeNotification();
617 return true;
618 }
619
[email protected]76722472012-05-24 08:26:46620 virtual base::SingleThreadTaskRunner* GetNotificationTaskRunner() OVERRIDE {
621 return task_runner_;
[email protected]8c20e3d2011-05-19 21:03:57622 }
623
[email protected]db8ff912012-06-12 23:32:51624 virtual ProxyConfigSource GetConfigSource() OVERRIDE {
625 return PROXY_CONFIG_SOURCE_GSETTINGS;
[email protected]8c20e3d2011-05-19 21:03:57626 }
627
[email protected]c4c1b482011-07-22 17:24:26628 virtual bool GetString(StringSetting key, std::string* result) OVERRIDE {
[email protected]8c20e3d2011-05-19 21:03:57629 DCHECK(client_);
630 switch (key) {
631 case PROXY_MODE:
632 return GetStringByPath(client_, "mode", result);
633 case PROXY_AUTOCONF_URL:
634 return GetStringByPath(client_, "autoconfig-url", result);
635 case PROXY_HTTP_HOST:
636 return GetStringByPath(http_client_, "host", result);
637 case PROXY_HTTPS_HOST:
638 return GetStringByPath(https_client_, "host", result);
639 case PROXY_FTP_HOST:
640 return GetStringByPath(ftp_client_, "host", result);
641 case PROXY_SOCKS_HOST:
642 return GetStringByPath(socks_client_, "host", result);
[email protected]8c20e3d2011-05-19 21:03:57643 }
[email protected]6b5fe742011-05-20 21:46:48644 return false; // Placate compiler.
[email protected]8c20e3d2011-05-19 21:03:57645 }
[email protected]c4c1b482011-07-22 17:24:26646 virtual bool GetBool(BoolSetting key, bool* result) OVERRIDE {
[email protected]8c20e3d2011-05-19 21:03:57647 DCHECK(client_);
648 switch (key) {
649 case PROXY_USE_HTTP_PROXY:
650 // Although there is an "enabled" boolean in http_client_, it is not set
651 // to true by the proxy config utility. We ignore it and return false.
652 return false;
653 case PROXY_USE_SAME_PROXY:
654 // Similarly, although there is a "use-same-proxy" boolean in client_,
655 // it is never set to false by the proxy config utility. We ignore it.
656 return false;
657 case PROXY_USE_AUTHENTICATION:
658 // There is also no way to set this in the proxy config utility, but it
659 // doesn't hurt us to get the actual setting (unlike the two above).
660 return GetBoolByPath(http_client_, "use-authentication", result);
[email protected]8c20e3d2011-05-19 21:03:57661 }
[email protected]6b5fe742011-05-20 21:46:48662 return false; // Placate compiler.
[email protected]8c20e3d2011-05-19 21:03:57663 }
[email protected]c4c1b482011-07-22 17:24:26664 virtual bool GetInt(IntSetting key, int* result) OVERRIDE {
[email protected]8c20e3d2011-05-19 21:03:57665 DCHECK(client_);
666 switch (key) {
667 case PROXY_HTTP_PORT:
668 return GetIntByPath(http_client_, "port", result);
669 case PROXY_HTTPS_PORT:
670 return GetIntByPath(https_client_, "port", result);
671 case PROXY_FTP_PORT:
672 return GetIntByPath(ftp_client_, "port", result);
673 case PROXY_SOCKS_PORT:
674 return GetIntByPath(socks_client_, "port", result);
[email protected]8c20e3d2011-05-19 21:03:57675 }
[email protected]6b5fe742011-05-20 21:46:48676 return false; // Placate compiler.
[email protected]8c20e3d2011-05-19 21:03:57677 }
[email protected]6b5fe742011-05-20 21:46:48678 virtual bool GetStringList(StringListSetting key,
[email protected]c4c1b482011-07-22 17:24:26679 std::vector<std::string>* result) OVERRIDE {
[email protected]8c20e3d2011-05-19 21:03:57680 DCHECK(client_);
681 switch (key) {
682 case PROXY_IGNORE_HOSTS:
683 return GetStringListByPath(client_, "ignore-hosts", result);
[email protected]8c20e3d2011-05-19 21:03:57684 }
[email protected]6b5fe742011-05-20 21:46:48685 return false; // Placate compiler.
[email protected]8c20e3d2011-05-19 21:03:57686 }
687
[email protected]c4c1b482011-07-22 17:24:26688 virtual bool BypassListIsReversed() OVERRIDE {
[email protected]8c20e3d2011-05-19 21:03:57689 // This is a KDE-specific setting.
690 return false;
691 }
692
[email protected]c4c1b482011-07-22 17:24:26693 virtual bool MatchHostsUsingSuffixMatching() OVERRIDE {
[email protected]8c20e3d2011-05-19 21:03:57694 return false;
695 }
696
697 private:
[email protected]8c20e3d2011-05-19 21:03:57698 bool GetStringByPath(GSettings* client, const char* key,
699 std::string* result) {
[email protected]76722472012-05-24 08:26:46700 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]3fc24f52012-11-30 21:22:34701 gchar* value = libgio_loader_.g_settings_get_string(client, key);
[email protected]8c20e3d2011-05-19 21:03:57702 if (!value)
703 return false;
704 *result = value;
705 g_free(value);
706 return true;
707 }
708 bool GetBoolByPath(GSettings* client, const char* key, bool* result) {
[email protected]76722472012-05-24 08:26:46709 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]3fc24f52012-11-30 21:22:34710 *result = static_cast<bool>(
711 libgio_loader_.g_settings_get_boolean(client, key));
[email protected]8c20e3d2011-05-19 21:03:57712 return true;
713 }
714 bool GetIntByPath(GSettings* client, const char* key, int* result) {
[email protected]76722472012-05-24 08:26:46715 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]3fc24f52012-11-30 21:22:34716 *result = libgio_loader_.g_settings_get_int(client, key);
[email protected]8c20e3d2011-05-19 21:03:57717 return true;
718 }
719 bool GetStringListByPath(GSettings* client, const char* key,
720 std::vector<std::string>* result) {
[email protected]76722472012-05-24 08:26:46721 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]3fc24f52012-11-30 21:22:34722 gchar** list = libgio_loader_.g_settings_get_strv(client, key);
[email protected]8c20e3d2011-05-19 21:03:57723 if (!list)
724 return false;
725 for (size_t i = 0; list[i]; ++i) {
726 result->push_back(static_cast<char*>(list[i]));
727 g_free(list[i]);
728 }
729 g_free(list);
730 return true;
731 }
732
733 // This is the callback from the debounce timer.
734 void OnDebouncedNotification() {
[email protected]76722472012-05-24 08:26:46735 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]8c20e3d2011-05-19 21:03:57736 CHECK(notify_delegate_);
737 // Forward to a method on the proxy config service delegate object.
738 notify_delegate_->OnCheckProxyConfigSettings();
739 }
740
741 void OnChangeNotification() {
742 // We don't use Reset() because the timer may not yet be running.
743 // (In that case Stop() is a no-op.)
744 debounce_timer_.Stop();
[email protected]d323a172011-09-02 18:23:02745 debounce_timer_.Start(FROM_HERE,
[email protected]8c20e3d2011-05-19 21:03:57746 base::TimeDelta::FromMilliseconds(kDebounceTimeoutMilliseconds),
747 this, &SettingGetterImplGSettings::OnDebouncedNotification);
748 }
749
750 // gsettings notification callback, dispatched on the default glib main loop.
751 static void OnGSettingsChangeNotification(GSettings* client, gchar* key,
752 gpointer user_data) {
753 VLOG(1) << "gsettings change notification for key " << key;
754 // We don't track which key has changed, just that something did change.
755 SettingGetterImplGSettings* setting_getter =
756 reinterpret_cast<SettingGetterImplGSettings*>(user_data);
757 setting_getter->OnChangeNotification();
758 }
759
760 GSettings* client_;
761 GSettings* http_client_;
762 GSettings* https_client_;
763 GSettings* ftp_client_;
764 GSettings* socks_client_;
765 ProxyConfigServiceLinux::Delegate* notify_delegate_;
766 base::OneShotTimer<SettingGetterImplGSettings> debounce_timer_;
767
[email protected]76722472012-05-24 08:26:46768 // Task runner for the thread that we make gsettings calls on. It should
[email protected]8c20e3d2011-05-19 21:03:57769 // be the UI thread and all our methods should be called on this
770 // thread. Only for assertions.
[email protected]76722472012-05-24 08:26:46771 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
[email protected]8c20e3d2011-05-19 21:03:57772
[email protected]3fc24f52012-11-30 21:22:34773 LibGioLoader libgio_loader_;
774
[email protected]8c20e3d2011-05-19 21:03:57775 DISALLOW_COPY_AND_ASSIGN(SettingGetterImplGSettings);
776};
777
778bool SettingGetterImplGSettings::LoadAndCheckVersion(
779 base::Environment* env) {
780 // LoadAndCheckVersion() must be called *before* Init()!
781 DCHECK(!client_);
782
783 // The APIs to query gsettings were introduced after the minimum glib
784 // version we target, so we can't link directly against them. We load them
785 // dynamically at runtime, and if they don't exist, return false here. (We
786 // support linking directly via gyp flags though.) Additionally, even when
787 // they are present, we do two additional checks to make sure we should use
788 // them and not gconf. First, we attempt to load the schema for proxy
789 // settings. Second, we check for the program that was used in older
790 // versions of GNOME to configure proxy settings, and return false if it
791 // exists. Some distributions (e.g. Ubuntu 11.04) have the API and schema
792 // but don't use gsettings for proxy settings, but they do have the old
793 // binary, so we detect these systems that way.
794
[email protected]ae82cea2012-12-06 22:52:10795 {
796 // TODO(phajdan.jr): Redesign the code to load library on different thread.
797 base::ThreadRestrictions::ScopedAllowIO allow_io;
798
799 // Try also without .0 at the end; on some systems this may be required.
800 if (!libgio_loader_.Load("libgio-2.0.so.0") &&
801 !libgio_loader_.Load("libgio-2.0.so")) {
802 VLOG(1) << "Cannot load gio library. Will fall back to gconf.";
803 return false;
804 }
[email protected]8c20e3d2011-05-19 21:03:57805 }
[email protected]8c20e3d2011-05-19 21:03:57806
[email protected]4cf80f0b2011-05-20 20:30:26807 GSettings* client;
808 if (!SchemaExists("org.gnome.system.proxy") ||
[email protected]3fc24f52012-11-30 21:22:34809 !(client = libgio_loader_.g_settings_new("org.gnome.system.proxy"))) {
[email protected]8c20e3d2011-05-19 21:03:57810 VLOG(1) << "Cannot create gsettings client. Will fall back to gconf.";
811 return false;
812 }
813 g_object_unref(client);
814
815 std::string path;
816 if (!env->GetVar("PATH", &path)) {
817 LOG(ERROR) << "No $PATH variable. Assuming no gnome-network-properties.";
818 } else {
819 // Yes, we're on the UI thread. Yes, we're accessing the file system.
820 // Sadly, we don't have much choice. We need the proxy settings and we
821 // need them now, and to figure out where to get them, we have to check
822 // for this binary. See http://crbug.com/69057 for additional details.
823 base::ThreadRestrictions::ScopedAllowIO allow_io;
824 std::vector<std::string> paths;
825 Tokenize(path, ":", &paths);
826 for (size_t i = 0; i < paths.size(); ++i) {
[email protected]6cdfd7f2013-02-08 20:40:15827 base::FilePath file(paths[i]);
[email protected]8c20e3d2011-05-19 21:03:57828 if (file_util::PathExists(file.Append("gnome-network-properties"))) {
829 VLOG(1) << "Found gnome-network-properties. Will fall back to gconf.";
830 return false;
831 }
832 }
833 }
834
835 VLOG(1) << "All gsettings tests OK. Will get proxy config from gsettings.";
836 return true;
837}
838#endif // defined(USE_GIO)
839
[email protected]d7395e732009-08-28 23:13:43840// This is the KDE version that reads kioslaverc and simulates gconf.
841// Doing this allows the main Delegate code, as well as the unit tests
842// for it, to stay the same - and the settings map fairly well besides.
[email protected]573c0502011-05-17 22:19:50843class SettingGetterImplKDE : public ProxyConfigServiceLinux::SettingGetter,
844 public base::MessagePumpLibevent::Watcher {
[email protected]d7395e732009-08-28 23:13:43845 public:
[email protected]573c0502011-05-17 22:19:50846 explicit SettingGetterImplKDE(base::Environment* env_var_getter)
[email protected]d7395e732009-08-28 23:13:43847 : inotify_fd_(-1), notify_delegate_(NULL), indirect_manual_(false),
[email protected]a48bf4a2010-06-14 18:24:53848 auto_no_pac_(false), reversed_bypass_list_(false),
[email protected]f18fde22010-05-18 23:49:54849 env_var_getter_(env_var_getter), file_loop_(NULL) {
[email protected]9a8c4022011-01-25 14:25:33850 // This has to be called on the UI thread (http://crbug.com/69057).
851 base::ThreadRestrictions::ScopedAllowIO allow_io;
852
[email protected]f18fde22010-05-18 23:49:54853 // Derive the location of the kde config dir from the environment.
[email protected]92d2dc82010-04-08 17:49:59854 std::string home;
[email protected]3ba7e082010-08-07 02:57:59855 if (env_var_getter->GetVar("KDEHOME", &home) && !home.empty()) {
[email protected]2e8cfe22010-06-12 00:26:24856 // $KDEHOME is set. Use it unconditionally.
[email protected]6cdfd7f2013-02-08 20:40:15857 kde_config_dir_ = KDEHomeToConfigPath(base::FilePath(home));
[email protected]92d2dc82010-04-08 17:49:59858 } else {
[email protected]2e8cfe22010-06-12 00:26:24859 // $KDEHOME is unset. Try to figure out what to use. This seems to be
[email protected]92d2dc82010-04-08 17:49:59860 // the common case on most distributions.
[email protected]3ba7e082010-08-07 02:57:59861 if (!env_var_getter->GetVar(base::env_vars::kHome, &home))
[email protected]d7395e732009-08-28 23:13:43862 // User has no $HOME? Give up. Later we'll report the failure.
863 return;
[email protected]6b0349ef2010-10-16 04:56:06864 if (base::nix::GetDesktopEnvironment(env_var_getter) ==
865 base::nix::DESKTOP_ENVIRONMENT_KDE3) {
[email protected]92d2dc82010-04-08 17:49:59866 // KDE3 always uses .kde for its configuration.
[email protected]6cdfd7f2013-02-08 20:40:15867 base::FilePath kde_path = base::FilePath(home).Append(".kde");
[email protected]92d2dc82010-04-08 17:49:59868 kde_config_dir_ = KDEHomeToConfigPath(kde_path);
869 } else {
870 // Some distributions patch KDE4 to use .kde4 instead of .kde, so that
[email protected]fad9c8a52010-06-10 22:30:53871 // both can be installed side-by-side. Sadly they don't all do this, and
872 // they don't always do this: some distributions have started switching
873 // back as well. So if there is a .kde4 directory, check the timestamps
874 // of the config directories within and use the newest one.
[email protected]92d2dc82010-04-08 17:49:59875 // Note that we should currently be running in the UI thread, because in
876 // the gconf version, that is the only thread that can access the proxy
877 // settings (a gconf restriction). As noted below, the initial read of
878 // the proxy settings will be done in this thread anyway, so we check
879 // for .kde4 here in this thread as well.
[email protected]6cdfd7f2013-02-08 20:40:15880 base::FilePath kde3_path = base::FilePath(home).Append(".kde");
881 base::FilePath kde3_config = KDEHomeToConfigPath(kde3_path);
882 base::FilePath kde4_path = base::FilePath(home).Append(".kde4");
883 base::FilePath kde4_config = KDEHomeToConfigPath(kde4_path);
[email protected]fad9c8a52010-06-10 22:30:53884 bool use_kde4 = false;
[email protected]92d2dc82010-04-08 17:49:59885 if (file_util::DirectoryExists(kde4_path)) {
[email protected]2f0193c22010-09-03 02:28:37886 base::PlatformFileInfo kde3_info;
887 base::PlatformFileInfo kde4_info;
[email protected]fad9c8a52010-06-10 22:30:53888 if (file_util::GetFileInfo(kde4_config, &kde4_info)) {
889 if (file_util::GetFileInfo(kde3_config, &kde3_info)) {
890 use_kde4 = kde4_info.last_modified >= kde3_info.last_modified;
891 } else {
892 use_kde4 = true;
893 }
894 }
895 }
896 if (use_kde4) {
[email protected]92d2dc82010-04-08 17:49:59897 kde_config_dir_ = KDEHomeToConfigPath(kde4_path);
898 } else {
[email protected]fad9c8a52010-06-10 22:30:53899 kde_config_dir_ = KDEHomeToConfigPath(kde3_path);
[email protected]92d2dc82010-04-08 17:49:59900 }
901 }
[email protected]d7395e732009-08-28 23:13:43902 }
[email protected]d7395e732009-08-28 23:13:43903 }
904
[email protected]573c0502011-05-17 22:19:50905 virtual ~SettingGetterImplKDE() {
[email protected]d7395e732009-08-28 23:13:43906 // inotify_fd_ should have been closed before now, from
907 // Delegate::OnDestroy(), while running on the file thread. However
908 // on exiting the process, it may happen that Delegate::OnDestroy()
909 // task is left pending on the file loop after the loop was quit,
910 // and pending tasks may then be deleted without being run.
911 // Here in the KDE version, we can safely close the file descriptor
912 // anyway. (Not that it really matters; the process is exiting.)
913 if (inotify_fd_ >= 0)
[email protected]d3066142011-05-10 02:36:20914 ShutDown();
[email protected]d7395e732009-08-28 23:13:43915 DCHECK(inotify_fd_ < 0);
916 }
917
[email protected]76722472012-05-24 08:26:46918 virtual bool Init(base::SingleThreadTaskRunner* glib_thread_task_runner,
[email protected]c4c1b482011-07-22 17:24:26919 MessageLoopForIO* file_loop) OVERRIDE {
[email protected]9a8c4022011-01-25 14:25:33920 // This has to be called on the UI thread (http://crbug.com/69057).
921 base::ThreadRestrictions::ScopedAllowIO allow_io;
[email protected]d7395e732009-08-28 23:13:43922 DCHECK(inotify_fd_ < 0);
923 inotify_fd_ = inotify_init();
924 if (inotify_fd_ < 0) {
[email protected]57b765672009-10-13 18:27:40925 PLOG(ERROR) << "inotify_init failed";
[email protected]d7395e732009-08-28 23:13:43926 return false;
927 }
928 int flags = fcntl(inotify_fd_, F_GETFL);
929 if (fcntl(inotify_fd_, F_SETFL, flags | O_NONBLOCK) < 0) {
[email protected]57b765672009-10-13 18:27:40930 PLOG(ERROR) << "fcntl failed";
[email protected]d7395e732009-08-28 23:13:43931 close(inotify_fd_);
932 inotify_fd_ = -1;
933 return false;
934 }
935 file_loop_ = file_loop;
936 // The initial read is done on the current thread, not |file_loop_|,
[email protected]d3066142011-05-10 02:36:20937 // since we will need to have it for SetUpAndFetchInitialConfig().
[email protected]d7395e732009-08-28 23:13:43938 UpdateCachedSettings();
939 return true;
940 }
941
[email protected]749bf5c2012-09-17 03:15:21942 virtual void ShutDown() OVERRIDE {
[email protected]d7395e732009-08-28 23:13:43943 if (inotify_fd_ >= 0) {
944 ResetCachedSettings();
945 inotify_watcher_.StopWatchingFileDescriptor();
946 close(inotify_fd_);
947 inotify_fd_ = -1;
948 }
949 }
950
[email protected]749bf5c2012-09-17 03:15:21951 virtual bool SetUpNotifications(
952 ProxyConfigServiceLinux::Delegate* delegate) OVERRIDE {
[email protected]d7395e732009-08-28 23:13:43953 DCHECK(inotify_fd_ >= 0);
[email protected]d3066142011-05-10 02:36:20954 DCHECK(MessageLoop::current() == file_loop_);
[email protected]d7395e732009-08-28 23:13:43955 // We can't just watch the kioslaverc file directly, since KDE will write
956 // a new copy of it and then rename it whenever settings are changed and
957 // inotify watches inodes (so we'll be watching the old deleted file after
958 // the first change, and it will never change again). So, we watch the
959 // directory instead. We then act only on changes to the kioslaverc entry.
960 if (inotify_add_watch(inotify_fd_, kde_config_dir_.value().c_str(),
961 IN_MODIFY | IN_MOVED_TO) < 0)
962 return false;
963 notify_delegate_ = delegate;
[email protected]d3066142011-05-10 02:36:20964 if (!file_loop_->WatchFileDescriptor(inotify_fd_, true,
965 MessageLoopForIO::WATCH_READ, &inotify_watcher_, this))
966 return false;
967 // Simulate a change to avoid possibly losing updates before this point.
968 OnChangeNotification();
969 return true;
[email protected]d7395e732009-08-28 23:13:43970 }
971
[email protected]76722472012-05-24 08:26:46972 virtual base::SingleThreadTaskRunner* GetNotificationTaskRunner() OVERRIDE {
[email protected]051e71e2012-06-02 22:13:17973 return file_loop_ ? file_loop_->message_loop_proxy() : NULL;
[email protected]d7395e732009-08-28 23:13:43974 }
975
[email protected]b160df32012-02-06 20:39:41976 // Implement base::MessagePumpLibevent::Watcher.
[email protected]749bf5c2012-09-17 03:15:21977 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE {
[email protected]d2e6d592012-02-03 21:49:04978 DCHECK_EQ(fd, inotify_fd_);
[email protected]d7395e732009-08-28 23:13:43979 DCHECK(MessageLoop::current() == file_loop_);
980 OnChangeNotification();
981 }
[email protected]749bf5c2012-09-17 03:15:21982 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE {
[email protected]d7395e732009-08-28 23:13:43983 NOTREACHED();
984 }
985
[email protected]db8ff912012-06-12 23:32:51986 virtual ProxyConfigSource GetConfigSource() OVERRIDE {
987 return PROXY_CONFIG_SOURCE_KDE;
[email protected]d7395e732009-08-28 23:13:43988 }
989
[email protected]c4c1b482011-07-22 17:24:26990 virtual bool GetString(StringSetting key, std::string* result) OVERRIDE {
[email protected]d7395e732009-08-28 23:13:43991 string_map_type::iterator it = string_table_.find(key);
992 if (it == string_table_.end())
993 return false;
994 *result = it->second;
995 return true;
996 }
[email protected]c4c1b482011-07-22 17:24:26997 virtual bool GetBool(BoolSetting key, bool* result) OVERRIDE {
[email protected]d7395e732009-08-28 23:13:43998 // We don't ever have any booleans.
999 return false;
1000 }
[email protected]c4c1b482011-07-22 17:24:261001 virtual bool GetInt(IntSetting key, int* result) OVERRIDE {
[email protected]d7395e732009-08-28 23:13:431002 // We don't ever have any integers. (See AddProxy() below about ports.)
1003 return false;
1004 }
[email protected]6b5fe742011-05-20 21:46:481005 virtual bool GetStringList(StringListSetting key,
[email protected]c4c1b482011-07-22 17:24:261006 std::vector<std::string>* result) OVERRIDE {
[email protected]d7395e732009-08-28 23:13:431007 strings_map_type::iterator it = strings_table_.find(key);
1008 if (it == strings_table_.end())
1009 return false;
1010 *result = it->second;
1011 return true;
1012 }
1013
[email protected]c4c1b482011-07-22 17:24:261014 virtual bool BypassListIsReversed() OVERRIDE {
[email protected]a48bf4a2010-06-14 18:24:531015 return reversed_bypass_list_;
1016 }
1017
[email protected]c4c1b482011-07-22 17:24:261018 virtual bool MatchHostsUsingSuffixMatching() OVERRIDE {
[email protected]1a597192010-07-09 16:58:381019 return true;
1020 }
1021
[email protected]d7395e732009-08-28 23:13:431022 private:
1023 void ResetCachedSettings() {
1024 string_table_.clear();
1025 strings_table_.clear();
1026 indirect_manual_ = false;
1027 auto_no_pac_ = false;
[email protected]a48bf4a2010-06-14 18:24:531028 reversed_bypass_list_ = false;
[email protected]d7395e732009-08-28 23:13:431029 }
1030
[email protected]6cdfd7f2013-02-08 20:40:151031 base::FilePath KDEHomeToConfigPath(const base::FilePath& kde_home) {
[email protected]92d2dc82010-04-08 17:49:591032 return kde_home.Append("share").Append("config");
1033 }
1034
[email protected]6b5fe742011-05-20 21:46:481035 void AddProxy(StringSetting host_key, const std::string& value) {
[email protected]d7395e732009-08-28 23:13:431036 if (value.empty() || value.substr(0, 3) == "//:")
1037 // No proxy.
1038 return;
[email protected]4b90c202012-04-24 23:27:551039 size_t space = value.find(' ');
1040 if (space != std::string::npos) {
1041 // Newer versions of KDE use a space rather than a colon to separate the
1042 // port number from the hostname. If we find this, we need to convert it.
1043 std::string fixed = value;
1044 fixed[space] = ':';
1045 string_table_[host_key] = fixed;
1046 } else {
1047 // We don't need to parse the port number out; GetProxyFromSettings()
1048 // would only append it right back again. So we just leave the port
1049 // number right in the host string.
1050 string_table_[host_key] = value;
1051 }
[email protected]d7395e732009-08-28 23:13:431052 }
1053
[email protected]6b5fe742011-05-20 21:46:481054 void AddHostList(StringListSetting key, const std::string& value) {
[email protected]f18fde22010-05-18 23:49:541055 std::vector<std::string> tokens;
[email protected]f4ebe772013-02-02 00:21:391056 base::StringTokenizer tk(value, ", ");
[email protected]f18fde22010-05-18 23:49:541057 while (tk.GetNext()) {
1058 std::string token = tk.token();
1059 if (!token.empty())
1060 tokens.push_back(token);
1061 }
1062 strings_table_[key] = tokens;
1063 }
1064
[email protected]9a3d8d42009-09-03 17:01:461065 void AddKDESetting(const std::string& key, const std::string& value) {
[email protected]d7395e732009-08-28 23:13:431066 if (key == "ProxyType") {
1067 const char* mode = "none";
1068 indirect_manual_ = false;
1069 auto_no_pac_ = false;
[email protected]e83326f2010-07-31 17:29:251070 int int_value;
1071 base::StringToInt(value, &int_value);
1072 switch (int_value) {
[email protected]d7395e732009-08-28 23:13:431073 case 0: // No proxy, or maybe kioslaverc syntax error.
1074 break;
1075 case 1: // Manual configuration.
1076 mode = "manual";
1077 break;
1078 case 2: // PAC URL.
1079 mode = "auto";
1080 break;
1081 case 3: // WPAD.
1082 mode = "auto";
1083 auto_no_pac_ = true;
1084 break;
1085 case 4: // Indirect manual via environment variables.
1086 mode = "manual";
1087 indirect_manual_ = true;
1088 break;
1089 }
[email protected]573c0502011-05-17 22:19:501090 string_table_[PROXY_MODE] = mode;
[email protected]d7395e732009-08-28 23:13:431091 } else if (key == "Proxy Config Script") {
[email protected]573c0502011-05-17 22:19:501092 string_table_[PROXY_AUTOCONF_URL] = value;
[email protected]d7395e732009-08-28 23:13:431093 } else if (key == "httpProxy") {
[email protected]573c0502011-05-17 22:19:501094 AddProxy(PROXY_HTTP_HOST, value);
[email protected]d7395e732009-08-28 23:13:431095 } else if (key == "httpsProxy") {
[email protected]573c0502011-05-17 22:19:501096 AddProxy(PROXY_HTTPS_HOST, value);
[email protected]d7395e732009-08-28 23:13:431097 } else if (key == "ftpProxy") {
[email protected]573c0502011-05-17 22:19:501098 AddProxy(PROXY_FTP_HOST, value);
[email protected]bfeb7232012-06-08 00:58:371099 } else if (key == "socksProxy") {
1100 // Older versions of KDE configure SOCKS in a weird way involving
1101 // LD_PRELOAD and a library that intercepts network calls to SOCKSify
1102 // them. We don't support it. KDE 4.8 added a proper SOCKS setting.
1103 AddProxy(PROXY_SOCKS_HOST, value);
[email protected]d7395e732009-08-28 23:13:431104 } else if (key == "ReversedException") {
1105 // We count "true" or any nonzero number as true, otherwise false.
1106 // Note that if the value is not actually numeric StringToInt()
1107 // will return 0, which we count as false.
[email protected]e83326f2010-07-31 17:29:251108 int int_value;
1109 base::StringToInt(value, &int_value);
1110 reversed_bypass_list_ = (value == "true" || int_value);
[email protected]d7395e732009-08-28 23:13:431111 } else if (key == "NoProxyFor") {
[email protected]573c0502011-05-17 22:19:501112 AddHostList(PROXY_IGNORE_HOSTS, value);
[email protected]d7395e732009-08-28 23:13:431113 } else if (key == "AuthMode") {
1114 // Check for authentication, just so we can warn.
[email protected]e83326f2010-07-31 17:29:251115 int mode;
1116 base::StringToInt(value, &mode);
[email protected]d7395e732009-08-28 23:13:431117 if (mode) {
1118 // ProxyConfig does not support authentication parameters, but
1119 // Chrome will prompt for the password later. So we ignore this.
1120 LOG(WARNING) <<
1121 "Proxy authentication parameters ignored, see bug 16709";
1122 }
1123 }
1124 }
1125
[email protected]6b5fe742011-05-20 21:46:481126 void ResolveIndirect(StringSetting key) {
[email protected]d7395e732009-08-28 23:13:431127 string_map_type::iterator it = string_table_.find(key);
1128 if (it != string_table_.end()) {
[email protected]f18fde22010-05-18 23:49:541129 std::string value;
[email protected]3ba7e082010-08-07 02:57:591130 if (env_var_getter_->GetVar(it->second.c_str(), &value))
[email protected]d7395e732009-08-28 23:13:431131 it->second = value;
[email protected]8425adc02010-04-18 17:45:311132 else
1133 string_table_.erase(it);
[email protected]d7395e732009-08-28 23:13:431134 }
1135 }
1136
[email protected]6b5fe742011-05-20 21:46:481137 void ResolveIndirectList(StringListSetting key) {
[email protected]f18fde22010-05-18 23:49:541138 strings_map_type::iterator it = strings_table_.find(key);
1139 if (it != strings_table_.end()) {
1140 std::string value;
1141 if (!it->second.empty() &&
[email protected]3ba7e082010-08-07 02:57:591142 env_var_getter_->GetVar(it->second[0].c_str(), &value))
[email protected]f18fde22010-05-18 23:49:541143 AddHostList(key, value);
1144 else
1145 strings_table_.erase(it);
1146 }
1147 }
1148
[email protected]d7395e732009-08-28 23:13:431149 // The settings in kioslaverc could occur in any order, but some affect
1150 // others. Rather than read the whole file in and then query them in an
1151 // order that allows us to handle that, we read the settings in whatever
1152 // order they occur and do any necessary tweaking after we finish.
1153 void ResolveModeEffects() {
1154 if (indirect_manual_) {
[email protected]573c0502011-05-17 22:19:501155 ResolveIndirect(PROXY_HTTP_HOST);
1156 ResolveIndirect(PROXY_HTTPS_HOST);
1157 ResolveIndirect(PROXY_FTP_HOST);
1158 ResolveIndirectList(PROXY_IGNORE_HOSTS);
[email protected]d7395e732009-08-28 23:13:431159 }
1160 if (auto_no_pac_) {
1161 // Remove the PAC URL; we're not supposed to use it.
[email protected]573c0502011-05-17 22:19:501162 string_table_.erase(PROXY_AUTOCONF_URL);
[email protected]d7395e732009-08-28 23:13:431163 }
[email protected]d7395e732009-08-28 23:13:431164 }
1165
1166 // Reads kioslaverc one line at a time and calls AddKDESetting() to add
1167 // each relevant name-value pair to the appropriate value table.
1168 void UpdateCachedSettings() {
[email protected]6cdfd7f2013-02-08 20:40:151169 base::FilePath kioslaverc = kde_config_dir_.Append("kioslaverc");
[email protected]d7395e732009-08-28 23:13:431170 file_util::ScopedFILE input(file_util::OpenFile(kioslaverc, "r"));
1171 if (!input.get())
1172 return;
1173 ResetCachedSettings();
1174 bool in_proxy_settings = false;
1175 bool line_too_long = false;
[email protected]9a3d8d42009-09-03 17:01:461176 char line[BUFFER_SIZE];
1177 // fgets() will return NULL on EOF or error.
[email protected]d7395e732009-08-28 23:13:431178 while (fgets(line, sizeof(line), input.get())) {
1179 // fgets() guarantees the line will be properly terminated.
1180 size_t length = strlen(line);
1181 if (!length)
1182 continue;
1183 // This should be true even with CRLF endings.
1184 if (line[length - 1] != '\n') {
1185 line_too_long = true;
1186 continue;
1187 }
1188 if (line_too_long) {
1189 // The previous line had no line ending, but this done does. This is
1190 // the end of the line that was too long, so warn here and skip it.
1191 LOG(WARNING) << "skipped very long line in " << kioslaverc.value();
1192 line_too_long = false;
1193 continue;
1194 }
1195 // Remove the LF at the end, and the CR if there is one.
1196 line[--length] = '\0';
1197 if (length && line[length - 1] == '\r')
1198 line[--length] = '\0';
1199 // Now parse the line.
1200 if (line[0] == '[') {
1201 // Switching sections. All we care about is whether this is
1202 // the (a?) proxy settings section, for both KDE3 and KDE4.
1203 in_proxy_settings = !strncmp(line, "[Proxy Settings]", 16);
1204 } else if (in_proxy_settings) {
1205 // A regular line, in the (a?) proxy settings section.
[email protected]9a3d8d42009-09-03 17:01:461206 char* split = strchr(line, '=');
1207 // Skip this line if it does not contain an = sign.
1208 if (!split)
[email protected]d7395e732009-08-28 23:13:431209 continue;
[email protected]9a3d8d42009-09-03 17:01:461210 // Split the line on the = and advance |split|.
1211 *(split++) = 0;
1212 std::string key = line;
1213 std::string value = split;
1214 TrimWhitespaceASCII(key, TRIM_ALL, &key);
1215 TrimWhitespaceASCII(value, TRIM_ALL, &value);
1216 // Skip this line if the key name is empty.
1217 if (key.empty())
[email protected]d7395e732009-08-28 23:13:431218 continue;
1219 // Is the value name localized?
[email protected]9a3d8d42009-09-03 17:01:461220 if (key[key.length() - 1] == ']') {
1221 // Find the matching bracket.
1222 length = key.rfind('[');
1223 // Skip this line if the localization indicator is malformed.
1224 if (length == std::string::npos)
[email protected]d7395e732009-08-28 23:13:431225 continue;
1226 // Trim the localization indicator off.
[email protected]9a3d8d42009-09-03 17:01:461227 key.resize(length);
1228 // Remove any resulting trailing whitespace.
1229 TrimWhitespaceASCII(key, TRIM_TRAILING, &key);
1230 // Skip this line if the key name is now empty.
1231 if (key.empty())
1232 continue;
[email protected]d7395e732009-08-28 23:13:431233 }
[email protected]d7395e732009-08-28 23:13:431234 // Now fill in the tables.
[email protected]9a3d8d42009-09-03 17:01:461235 AddKDESetting(key, value);
[email protected]d7395e732009-08-28 23:13:431236 }
1237 }
1238 if (ferror(input.get()))
1239 LOG(ERROR) << "error reading " << kioslaverc.value();
1240 ResolveModeEffects();
1241 }
1242
1243 // This is the callback from the debounce timer.
1244 void OnDebouncedNotification() {
1245 DCHECK(MessageLoop::current() == file_loop_);
[email protected]b30a3f52010-10-16 01:05:461246 VLOG(1) << "inotify change notification for kioslaverc";
[email protected]d7395e732009-08-28 23:13:431247 UpdateCachedSettings();
[email protected]961ac942011-04-28 18:18:141248 CHECK(notify_delegate_);
[email protected]d7395e732009-08-28 23:13:431249 // Forward to a method on the proxy config service delegate object.
1250 notify_delegate_->OnCheckProxyConfigSettings();
1251 }
1252
1253 // Called by OnFileCanReadWithoutBlocking() on the file thread. Reads
1254 // from the inotify file descriptor and starts up a debounce timer if
1255 // an event for kioslaverc is seen.
1256 void OnChangeNotification() {
[email protected]d2e6d592012-02-03 21:49:041257 DCHECK_GE(inotify_fd_, 0);
[email protected]d7395e732009-08-28 23:13:431258 DCHECK(MessageLoop::current() == file_loop_);
1259 char event_buf[(sizeof(inotify_event) + NAME_MAX + 1) * 4];
1260 bool kioslaverc_touched = false;
1261 ssize_t r;
1262 while ((r = read(inotify_fd_, event_buf, sizeof(event_buf))) > 0) {
1263 // inotify returns variable-length structures, which is why we have
1264 // this strange-looking loop instead of iterating through an array.
1265 char* event_ptr = event_buf;
1266 while (event_ptr < event_buf + r) {
1267 inotify_event* event = reinterpret_cast<inotify_event*>(event_ptr);
1268 // The kernel always feeds us whole events.
[email protected]b1f031dd2010-03-02 23:19:331269 CHECK_LE(event_ptr + sizeof(inotify_event), event_buf + r);
1270 CHECK_LE(event->name + event->len, event_buf + r);
[email protected]d7395e732009-08-28 23:13:431271 if (!strcmp(event->name, "kioslaverc"))
1272 kioslaverc_touched = true;
1273 // Advance the pointer just past the end of the filename.
1274 event_ptr = event->name + event->len;
1275 }
1276 // We keep reading even if |kioslaverc_touched| is true to drain the
1277 // inotify event queue.
1278 }
1279 if (!r)
1280 // Instead of returning -1 and setting errno to EINVAL if there is not
1281 // enough buffer space, older kernels (< 2.6.21) return 0. Simulate the
1282 // new behavior (EINVAL) so we can reuse the code below.
1283 errno = EINVAL;
1284 if (errno != EAGAIN) {
[email protected]57b765672009-10-13 18:27:401285 PLOG(WARNING) << "error reading inotify file descriptor";
[email protected]d7395e732009-08-28 23:13:431286 if (errno == EINVAL) {
1287 // Our buffer is not large enough to read the next event. This should
1288 // not happen (because its size is calculated to always be sufficiently
1289 // large), but if it does we'd warn continuously since |inotify_fd_|
1290 // would be forever ready to read. Close it and stop watching instead.
1291 LOG(ERROR) << "inotify failure; no longer watching kioslaverc!";
1292 inotify_watcher_.StopWatchingFileDescriptor();
1293 close(inotify_fd_);
1294 inotify_fd_ = -1;
1295 }
1296 }
1297 if (kioslaverc_touched) {
1298 // We don't use Reset() because the timer may not yet be running.
1299 // (In that case Stop() is a no-op.)
1300 debounce_timer_.Stop();
[email protected]d323a172011-09-02 18:23:021301 debounce_timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(
[email protected]d7395e732009-08-28 23:13:431302 kDebounceTimeoutMilliseconds), this,
[email protected]573c0502011-05-17 22:19:501303 &SettingGetterImplKDE::OnDebouncedNotification);
[email protected]d7395e732009-08-28 23:13:431304 }
1305 }
1306
[email protected]6b5fe742011-05-20 21:46:481307 typedef std::map<StringSetting, std::string> string_map_type;
1308 typedef std::map<StringListSetting,
1309 std::vector<std::string> > strings_map_type;
[email protected]d7395e732009-08-28 23:13:431310
1311 int inotify_fd_;
1312 base::MessagePumpLibevent::FileDescriptorWatcher inotify_watcher_;
1313 ProxyConfigServiceLinux::Delegate* notify_delegate_;
[email protected]573c0502011-05-17 22:19:501314 base::OneShotTimer<SettingGetterImplKDE> debounce_timer_;
[email protected]6cdfd7f2013-02-08 20:40:151315 base::FilePath kde_config_dir_;
[email protected]d7395e732009-08-28 23:13:431316 bool indirect_manual_;
1317 bool auto_no_pac_;
[email protected]a48bf4a2010-06-14 18:24:531318 bool reversed_bypass_list_;
[email protected]f18fde22010-05-18 23:49:541319 // We don't own |env_var_getter_|. It's safe to hold a pointer to it, since
1320 // both it and us are owned by ProxyConfigServiceLinux::Delegate, and have the
1321 // same lifetime.
[email protected]76b90d312010-08-03 03:00:501322 base::Environment* env_var_getter_;
[email protected]d7395e732009-08-28 23:13:431323
1324 // We cache these settings whenever we re-read the kioslaverc file.
1325 string_map_type string_table_;
1326 strings_map_type strings_table_;
1327
1328 // Message loop of the file thread, for reading kioslaverc. If NULL,
1329 // just read it directly (for testing). We also handle inotify events
1330 // on this thread.
1331 MessageLoopForIO* file_loop_;
1332
[email protected]573c0502011-05-17 22:19:501333 DISALLOW_COPY_AND_ASSIGN(SettingGetterImplKDE);
[email protected]861c6c62009-04-20 16:50:561334};
1335
1336} // namespace
1337
[email protected]573c0502011-05-17 22:19:501338bool ProxyConfigServiceLinux::Delegate::GetProxyFromSettings(
[email protected]6b5fe742011-05-20 21:46:481339 SettingGetter::StringSetting host_key,
[email protected]573c0502011-05-17 22:19:501340 ProxyServer* result_server) {
[email protected]861c6c62009-04-20 16:50:561341 std::string host;
[email protected]573c0502011-05-17 22:19:501342 if (!setting_getter_->GetString(host_key, &host) || host.empty()) {
[email protected]861c6c62009-04-20 16:50:561343 // Unset or empty.
1344 return false;
1345 }
1346 // Check for an optional port.
[email protected]d7395e732009-08-28 23:13:431347 int port = 0;
[email protected]6b5fe742011-05-20 21:46:481348 SettingGetter::IntSetting port_key =
[email protected]573c0502011-05-17 22:19:501349 SettingGetter::HostSettingToPortSetting(host_key);
1350 setting_getter_->GetInt(port_key, &port);
[email protected]861c6c62009-04-20 16:50:561351 if (port != 0) {
1352 // If a port is set and non-zero:
[email protected]528c56d2010-07-30 19:28:441353 host += ":" + base::IntToString(port);
[email protected]861c6c62009-04-20 16:50:561354 }
[email protected]76960f3d2011-04-30 02:15:231355
[email protected]573c0502011-05-17 22:19:501356 // gconf settings do not appear to distinguish between SOCKS version. We
1357 // default to version 5. For more information on this policy decision, see:
[email protected]76960f3d2011-04-30 02:15:231358 // http://code.google.com/p/chromium/issues/detail?id=55912#c2
[email protected]573c0502011-05-17 22:19:501359 ProxyServer::Scheme scheme = (host_key == SettingGetter::PROXY_SOCKS_HOST) ?
1360 ProxyServer::SCHEME_SOCKS5 : ProxyServer::SCHEME_HTTP;
1361 host = FixupProxyHostScheme(scheme, host);
[email protected]87a102b2009-07-14 05:23:301362 ProxyServer proxy_server = ProxyServer::FromURI(host,
1363 ProxyServer::SCHEME_HTTP);
[email protected]861c6c62009-04-20 16:50:561364 if (proxy_server.is_valid()) {
1365 *result_server = proxy_server;
1366 return true;
1367 }
1368 return false;
1369}
1370
[email protected]573c0502011-05-17 22:19:501371bool ProxyConfigServiceLinux::Delegate::GetConfigFromSettings(
[email protected]3e44697f2009-05-22 14:37:391372 ProxyConfig* config) {
[email protected]861c6c62009-04-20 16:50:561373 std::string mode;
[email protected]573c0502011-05-17 22:19:501374 if (!setting_getter_->GetString(SettingGetter::PROXY_MODE, &mode)) {
[email protected]861c6c62009-04-20 16:50:561375 // We expect this to always be set, so if we don't see it then we
[email protected]573c0502011-05-17 22:19:501376 // probably have a gconf/gsettings problem, and so we don't have a valid
[email protected]861c6c62009-04-20 16:50:561377 // proxy config.
1378 return false;
1379 }
[email protected]3e44697f2009-05-22 14:37:391380 if (mode == "none") {
[email protected]861c6c62009-04-20 16:50:561381 // Specifically specifies no proxy.
1382 return true;
[email protected]3e44697f2009-05-22 14:37:391383 }
[email protected]861c6c62009-04-20 16:50:561384
[email protected]3e44697f2009-05-22 14:37:391385 if (mode == "auto") {
[email protected]aa3ac2cc2012-06-19 00:28:041386 // Automatic proxy config.
[email protected]861c6c62009-04-20 16:50:561387 std::string pac_url_str;
[email protected]573c0502011-05-17 22:19:501388 if (setting_getter_->GetString(SettingGetter::PROXY_AUTOCONF_URL,
1389 &pac_url_str)) {
[email protected]861c6c62009-04-20 16:50:561390 if (!pac_url_str.empty()) {
[email protected]aa3ac2cc2012-06-19 00:28:041391 // If the PAC URL is actually a file path, then put file:// in front.
1392 if (pac_url_str[0] == '/')
1393 pac_url_str = "file://" + pac_url_str;
[email protected]861c6c62009-04-20 16:50:561394 GURL pac_url(pac_url_str);
1395 if (!pac_url.is_valid())
1396 return false;
[email protected]ed4ed0f2010-02-24 00:20:481397 config->set_pac_url(pac_url);
[email protected]861c6c62009-04-20 16:50:561398 return true;
1399 }
1400 }
[email protected]ed4ed0f2010-02-24 00:20:481401 config->set_auto_detect(true);
[email protected]861c6c62009-04-20 16:50:561402 return true;
1403 }
1404
[email protected]3e44697f2009-05-22 14:37:391405 if (mode != "manual") {
[email protected]861c6c62009-04-20 16:50:561406 // Mode is unrecognized.
1407 return false;
1408 }
1409 bool use_http_proxy;
[email protected]573c0502011-05-17 22:19:501410 if (setting_getter_->GetBool(SettingGetter::PROXY_USE_HTTP_PROXY,
1411 &use_http_proxy)
[email protected]861c6c62009-04-20 16:50:561412 && !use_http_proxy) {
1413 // Another master switch for some reason. If set to false, then no
1414 // proxy. But we don't panic if the key doesn't exist.
1415 return true;
1416 }
1417
1418 bool same_proxy = false;
1419 // Indicates to use the http proxy for all protocols. This one may
[email protected]573c0502011-05-17 22:19:501420 // not exist (presumably on older versions); we assume false in that
[email protected]861c6c62009-04-20 16:50:561421 // case.
[email protected]573c0502011-05-17 22:19:501422 setting_getter_->GetBool(SettingGetter::PROXY_USE_SAME_PROXY,
1423 &same_proxy);
[email protected]861c6c62009-04-20 16:50:561424
[email protected]76960f3d2011-04-30 02:15:231425 ProxyServer proxy_for_http;
1426 ProxyServer proxy_for_https;
1427 ProxyServer proxy_for_ftp;
1428 ProxyServer socks_proxy; // (socks)
1429
1430 // This counts how many of the above ProxyServers were defined and valid.
1431 size_t num_proxies_specified = 0;
1432
1433 // Extract the per-scheme proxies. If we failed to parse it, or no proxy was
1434 // specified for the scheme, then the resulting ProxyServer will be invalid.
[email protected]573c0502011-05-17 22:19:501435 if (GetProxyFromSettings(SettingGetter::PROXY_HTTP_HOST, &proxy_for_http))
[email protected]76960f3d2011-04-30 02:15:231436 num_proxies_specified++;
[email protected]573c0502011-05-17 22:19:501437 if (GetProxyFromSettings(SettingGetter::PROXY_HTTPS_HOST, &proxy_for_https))
[email protected]76960f3d2011-04-30 02:15:231438 num_proxies_specified++;
[email protected]573c0502011-05-17 22:19:501439 if (GetProxyFromSettings(SettingGetter::PROXY_FTP_HOST, &proxy_for_ftp))
[email protected]76960f3d2011-04-30 02:15:231440 num_proxies_specified++;
[email protected]573c0502011-05-17 22:19:501441 if (GetProxyFromSettings(SettingGetter::PROXY_SOCKS_HOST, &socks_proxy))
[email protected]76960f3d2011-04-30 02:15:231442 num_proxies_specified++;
1443
1444 if (same_proxy) {
1445 if (proxy_for_http.is_valid()) {
1446 // Use the http proxy for all schemes.
[email protected]ed4ed0f2010-02-24 00:20:481447 config->proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY;
[email protected]76960f3d2011-04-30 02:15:231448 config->proxy_rules().single_proxy = proxy_for_http;
[email protected]861c6c62009-04-20 16:50:561449 }
[email protected]76960f3d2011-04-30 02:15:231450 } else if (num_proxies_specified > 0) {
1451 if (socks_proxy.is_valid() && num_proxies_specified == 1) {
1452 // If the only proxy specified was for SOCKS, use it for all schemes.
1453 config->proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY;
1454 config->proxy_rules().single_proxy = socks_proxy;
[email protected]861c6c62009-04-20 16:50:561455 } else {
[email protected]76960f3d2011-04-30 02:15:231456 // Otherwise use the indicate proxies per-scheme.
1457 config->proxy_rules().type =
1458 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME;
1459 config->proxy_rules().proxy_for_http = proxy_for_http;
1460 config->proxy_rules().proxy_for_https = proxy_for_https;
1461 config->proxy_rules().proxy_for_ftp = proxy_for_ftp;
1462 config->proxy_rules().fallback_proxy = socks_proxy;
[email protected]861c6c62009-04-20 16:50:561463 }
1464 }
1465
[email protected]ed4ed0f2010-02-24 00:20:481466 if (config->proxy_rules().empty()) {
[email protected]861c6c62009-04-20 16:50:561467 // Manual mode but we couldn't parse any rules.
1468 return false;
1469 }
1470
1471 // Check for authentication, just so we can warn.
[email protected]d7395e732009-08-28 23:13:431472 bool use_auth = false;
[email protected]573c0502011-05-17 22:19:501473 setting_getter_->GetBool(SettingGetter::PROXY_USE_AUTHENTICATION,
1474 &use_auth);
[email protected]62749f182009-07-15 13:16:541475 if (use_auth) {
1476 // ProxyConfig does not support authentication parameters, but
1477 // Chrome will prompt for the password later. So we ignore
1478 // /system/http_proxy/*auth* settings.
1479 LOG(WARNING) << "Proxy authentication parameters ignored, see bug 16709";
1480 }
[email protected]861c6c62009-04-20 16:50:561481
1482 // Now the bypass list.
[email protected]7541206c2010-02-19 20:24:061483 std::vector<std::string> ignore_hosts_list;
[email protected]ed4ed0f2010-02-24 00:20:481484 config->proxy_rules().bypass_rules.Clear();
[email protected]573c0502011-05-17 22:19:501485 if (setting_getter_->GetStringList(SettingGetter::PROXY_IGNORE_HOSTS,
1486 &ignore_hosts_list)) {
[email protected]a8185d02010-06-11 00:19:501487 std::vector<std::string>::const_iterator it(ignore_hosts_list.begin());
[email protected]1a597192010-07-09 16:58:381488 for (; it != ignore_hosts_list.end(); ++it) {
[email protected]573c0502011-05-17 22:19:501489 if (setting_getter_->MatchHostsUsingSuffixMatching()) {
[email protected]1a597192010-07-09 16:58:381490 config->proxy_rules().bypass_rules.
1491 AddRuleFromStringUsingSuffixMatching(*it);
1492 } else {
1493 config->proxy_rules().bypass_rules.AddRuleFromString(*it);
1494 }
1495 }
[email protected]a8185d02010-06-11 00:19:501496 }
[email protected]861c6c62009-04-20 16:50:561497 // Note that there are no settings with semantics corresponding to
[email protected]1a597192010-07-09 16:58:381498 // bypass of local names in GNOME. In KDE, "<local>" is supported
1499 // as a hostname rule.
[email protected]861c6c62009-04-20 16:50:561500
[email protected]a48bf4a2010-06-14 18:24:531501 // KDE allows one to reverse the bypass rules.
[email protected]573c0502011-05-17 22:19:501502 config->proxy_rules().reverse_bypass =
1503 setting_getter_->BypassListIsReversed();
[email protected]a48bf4a2010-06-14 18:24:531504
[email protected]861c6c62009-04-20 16:50:561505 return true;
1506}
1507
[email protected]76b90d312010-08-03 03:00:501508ProxyConfigServiceLinux::Delegate::Delegate(base::Environment* env_var_getter)
[email protected]76722472012-05-24 08:26:461509 : env_var_getter_(env_var_getter) {
[email protected]573c0502011-05-17 22:19:501510 // Figure out which SettingGetterImpl to use, if any.
[email protected]6b0349ef2010-10-16 04:56:061511 switch (base::nix::GetDesktopEnvironment(env_var_getter)) {
1512 case base::nix::DESKTOP_ENVIRONMENT_GNOME:
[email protected]9e6c9bde2012-07-17 23:40:171513 case base::nix::DESKTOP_ENVIRONMENT_UNITY:
[email protected]8c20e3d2011-05-19 21:03:571514#if defined(USE_GIO)
1515 {
1516 scoped_ptr<SettingGetterImplGSettings> gs_getter(
1517 new SettingGetterImplGSettings());
1518 // We have to load symbols and check the GNOME version in use to decide
1519 // if we should use the gsettings getter. See LoadAndCheckVersion().
1520 if (gs_getter->LoadAndCheckVersion(env_var_getter))
1521 setting_getter_.reset(gs_getter.release());
1522 }
1523#endif
[email protected]6de53d42010-11-09 07:33:191524#if defined(USE_GCONF)
[email protected]8c20e3d2011-05-19 21:03:571525 // Fall back on gconf if gsettings is unavailable or incorrect.
1526 if (!setting_getter_.get())
1527 setting_getter_.reset(new SettingGetterImplGConf());
[email protected]6de53d42010-11-09 07:33:191528#endif
[email protected]d7395e732009-08-28 23:13:431529 break;
[email protected]6b0349ef2010-10-16 04:56:061530 case base::nix::DESKTOP_ENVIRONMENT_KDE3:
1531 case base::nix::DESKTOP_ENVIRONMENT_KDE4:
[email protected]573c0502011-05-17 22:19:501532 setting_getter_.reset(new SettingGetterImplKDE(env_var_getter));
[email protected]d7395e732009-08-28 23:13:431533 break;
[email protected]6b0349ef2010-10-16 04:56:061534 case base::nix::DESKTOP_ENVIRONMENT_XFCE:
1535 case base::nix::DESKTOP_ENVIRONMENT_OTHER:
[email protected]d7395e732009-08-28 23:13:431536 break;
1537 }
1538}
1539
[email protected]573c0502011-05-17 22:19:501540ProxyConfigServiceLinux::Delegate::Delegate(
1541 base::Environment* env_var_getter, SettingGetter* setting_getter)
[email protected]76722472012-05-24 08:26:461542 : env_var_getter_(env_var_getter), setting_getter_(setting_getter) {
[email protected]861c6c62009-04-20 16:50:561543}
1544
[email protected]d3066142011-05-10 02:36:201545void ProxyConfigServiceLinux::Delegate::SetUpAndFetchInitialConfig(
[email protected]76722472012-05-24 08:26:461546 base::SingleThreadTaskRunner* glib_thread_task_runner,
1547 base::SingleThreadTaskRunner* io_thread_task_runner,
[email protected]d7395e732009-08-28 23:13:431548 MessageLoopForIO* file_loop) {
[email protected]3e44697f2009-05-22 14:37:391549 // We should be running on the default glib main loop thread right
1550 // now. gconf can only be accessed from this thread.
[email protected]76722472012-05-24 08:26:461551 DCHECK(glib_thread_task_runner->BelongsToCurrentThread());
1552 glib_thread_task_runner_ = glib_thread_task_runner;
1553 io_thread_task_runner_ = io_thread_task_runner;
[email protected]3e44697f2009-05-22 14:37:391554
[email protected]76722472012-05-24 08:26:461555 // If we are passed a NULL |io_thread_task_runner| or |file_loop|,
1556 // then we don't set up proxy setting change notifications. This
1557 // should not be the usual case but is intended to simplify test
1558 // setups.
1559 if (!io_thread_task_runner_ || !file_loop)
[email protected]b30a3f52010-10-16 01:05:461560 VLOG(1) << "Monitoring of proxy setting changes is disabled";
[email protected]3e44697f2009-05-22 14:37:391561
1562 // Fetch and cache the current proxy config. The config is left in
[email protected]119655002010-07-23 06:02:401563 // cached_config_, where GetLatestProxyConfig() running on the IO thread
[email protected]3e44697f2009-05-22 14:37:391564 // will expect to find it. This is safe to do because we return
1565 // before this ProxyConfigServiceLinux is passed on to
1566 // the ProxyService.
[email protected]d6cb85b2009-07-23 22:10:531567
1568 // Note: It would be nice to prioritize environment variables
[email protected]92d2dc82010-04-08 17:49:591569 // and only fall back to gconf if env vars were unset. But
[email protected]d6cb85b2009-07-23 22:10:531570 // gnome-terminal "helpfully" sets http_proxy and no_proxy, and it
1571 // does so even if the proxy mode is set to auto, which would
1572 // mislead us.
1573
[email protected]3e44697f2009-05-22 14:37:391574 bool got_config = false;
[email protected]573c0502011-05-17 22:19:501575 if (setting_getter_.get() &&
[email protected]76722472012-05-24 08:26:461576 setting_getter_->Init(glib_thread_task_runner, file_loop) &&
[email protected]573c0502011-05-17 22:19:501577 GetConfigFromSettings(&cached_config_)) {
[email protected]d3066142011-05-10 02:36:201578 cached_config_.set_id(1); // Mark it as valid.
[email protected]db8ff912012-06-12 23:32:511579 cached_config_.set_source(setting_getter_->GetConfigSource());
[email protected]d3066142011-05-10 02:36:201580 VLOG(1) << "Obtained proxy settings from "
[email protected]db8ff912012-06-12 23:32:511581 << ProxyConfigSourceToString(cached_config_.source());
[email protected]d3066142011-05-10 02:36:201582
1583 // If gconf proxy mode is "none", meaning direct, then we take
1584 // that to be a valid config and will not check environment
1585 // variables. The alternative would have been to look for a proxy
1586 // whereever we can find one.
1587 got_config = true;
1588
1589 // Keep a copy of the config for use from this thread for
1590 // comparison with updated settings when we get notifications.
1591 reference_config_ = cached_config_;
1592 reference_config_.set_id(1); // Mark it as valid.
1593
1594 // We only set up notifications if we have IO and file loops available.
1595 // We do this after getting the initial configuration so that we don't have
1596 // to worry about cancelling it if the initial fetch above fails. Note that
1597 // setting up notifications has the side effect of simulating a change, so
1598 // that we won't lose any updates that may have happened after the initial
1599 // fetch and before setting up notifications. We'll detect the common case
1600 // of no changes in OnCheckProxyConfigSettings() (or sooner) and ignore it.
[email protected]76722472012-05-24 08:26:461601 if (io_thread_task_runner && file_loop) {
1602 scoped_refptr<base::SingleThreadTaskRunner> required_loop =
1603 setting_getter_->GetNotificationTaskRunner();
[email protected]434fff82012-04-24 21:13:281604 if (!required_loop || required_loop->BelongsToCurrentThread()) {
[email protected]d3066142011-05-10 02:36:201605 // In this case we are already on an acceptable thread.
1606 SetUpNotifications();
[email protected]d7395e732009-08-28 23:13:431607 } else {
[email protected]d3066142011-05-10 02:36:201608 // Post a task to set up notifications. We don't wait for success.
[email protected]6af889c2011-10-06 23:11:411609 required_loop->PostTask(FROM_HERE, base::Bind(
1610 &ProxyConfigServiceLinux::Delegate::SetUpNotifications, this));
[email protected]d6cb85b2009-07-23 22:10:531611 }
[email protected]d7395e732009-08-28 23:13:431612 }
[email protected]861c6c62009-04-20 16:50:561613 }
[email protected]d6cb85b2009-07-23 22:10:531614
[email protected]3e44697f2009-05-22 14:37:391615 if (!got_config) {
[email protected]d6cb85b2009-07-23 22:10:531616 // We fall back on environment variables.
[email protected]3e44697f2009-05-22 14:37:391617 //
[email protected]d3066142011-05-10 02:36:201618 // Consulting environment variables doesn't need to be done from the
1619 // default glib main loop, but it's a tiny enough amount of work.
[email protected]3e44697f2009-05-22 14:37:391620 if (GetConfigFromEnv(&cached_config_)) {
[email protected]db8ff912012-06-12 23:32:511621 cached_config_.set_source(PROXY_CONFIG_SOURCE_ENV);
[email protected]d3066142011-05-10 02:36:201622 cached_config_.set_id(1); // Mark it as valid.
[email protected]b30a3f52010-10-16 01:05:461623 VLOG(1) << "Obtained proxy settings from environment variables";
[email protected]3e44697f2009-05-22 14:37:391624 }
[email protected]861c6c62009-04-20 16:50:561625 }
[email protected]3e44697f2009-05-22 14:37:391626}
1627
[email protected]573c0502011-05-17 22:19:501628// Depending on the SettingGetter in use, this method will be called
[email protected]d3066142011-05-10 02:36:201629// on either the UI thread (GConf) or the file thread (KDE).
1630void ProxyConfigServiceLinux::Delegate::SetUpNotifications() {
[email protected]76722472012-05-24 08:26:461631 scoped_refptr<base::SingleThreadTaskRunner> required_loop =
1632 setting_getter_->GetNotificationTaskRunner();
[email protected]434fff82012-04-24 21:13:281633 DCHECK(!required_loop || required_loop->BelongsToCurrentThread());
[email protected]573c0502011-05-17 22:19:501634 if (!setting_getter_->SetUpNotifications(this))
[email protected]d3066142011-05-10 02:36:201635 LOG(ERROR) << "Unable to set up proxy configuration change notifications";
1636}
1637
[email protected]119655002010-07-23 06:02:401638void ProxyConfigServiceLinux::Delegate::AddObserver(Observer* observer) {
1639 observers_.AddObserver(observer);
1640}
1641
1642void ProxyConfigServiceLinux::Delegate::RemoveObserver(Observer* observer) {
1643 observers_.RemoveObserver(observer);
1644}
1645
[email protected]3a29593d2011-04-11 10:07:521646ProxyConfigService::ConfigAvailability
1647 ProxyConfigServiceLinux::Delegate::GetLatestProxyConfig(
1648 ProxyConfig* config) {
[email protected]3e44697f2009-05-22 14:37:391649 // This is called from the IO thread.
[email protected]76722472012-05-24 08:26:461650 DCHECK(!io_thread_task_runner_ ||
1651 io_thread_task_runner_->BelongsToCurrentThread());
[email protected]3e44697f2009-05-22 14:37:391652
1653 // Simply return the last proxy configuration that glib_default_loop
1654 // notified us of.
[email protected]db8ff912012-06-12 23:32:511655 if (cached_config_.is_valid()) {
1656 *config = cached_config_;
1657 } else {
1658 *config = ProxyConfig::CreateDirect();
1659 config->set_source(PROXY_CONFIG_SOURCE_SYSTEM_FAILED);
1660 }
[email protected]119655002010-07-23 06:02:401661
[email protected]3a29593d2011-04-11 10:07:521662 // We return CONFIG_VALID to indicate that *config was filled in. It is always
[email protected]119655002010-07-23 06:02:401663 // going to be available since we initialized eagerly on the UI thread.
1664 // TODO(eroman): do lazy initialization instead, so we no longer need
1665 // to construct ProxyConfigServiceLinux on the UI thread.
1666 // In which case, we may return false here.
[email protected]3a29593d2011-04-11 10:07:521667 return CONFIG_VALID;
[email protected]3e44697f2009-05-22 14:37:391668}
1669
[email protected]573c0502011-05-17 22:19:501670// Depending on the SettingGetter in use, this method will be called
[email protected]d7395e732009-08-28 23:13:431671// on either the UI thread (GConf) or the file thread (KDE).
[email protected]3e44697f2009-05-22 14:37:391672void ProxyConfigServiceLinux::Delegate::OnCheckProxyConfigSettings() {
[email protected]76722472012-05-24 08:26:461673 scoped_refptr<base::SingleThreadTaskRunner> required_loop =
1674 setting_getter_->GetNotificationTaskRunner();
[email protected]434fff82012-04-24 21:13:281675 DCHECK(!required_loop || required_loop->BelongsToCurrentThread());
[email protected]3e44697f2009-05-22 14:37:391676 ProxyConfig new_config;
[email protected]573c0502011-05-17 22:19:501677 bool valid = GetConfigFromSettings(&new_config);
[email protected]3e44697f2009-05-22 14:37:391678 if (valid)
1679 new_config.set_id(1); // mark it as valid
1680
[email protected]119655002010-07-23 06:02:401681 // See if it is different from what we had before.
[email protected]3e44697f2009-05-22 14:37:391682 if (new_config.is_valid() != reference_config_.is_valid() ||
1683 !new_config.Equals(reference_config_)) {
[email protected]76722472012-05-24 08:26:461684 // Post a task to the IO thread with the new configuration, so it can
[email protected]3e44697f2009-05-22 14:37:391685 // update |cached_config_|.
[email protected]76722472012-05-24 08:26:461686 io_thread_task_runner_->PostTask(FROM_HERE, base::Bind(
[email protected]6af889c2011-10-06 23:11:411687 &ProxyConfigServiceLinux::Delegate::SetNewProxyConfig,
1688 this, new_config));
[email protected]d1f9d472009-08-13 19:59:301689 // Update the thread-private copy in |reference_config_| as well.
1690 reference_config_ = new_config;
[email protected]d3066142011-05-10 02:36:201691 } else {
1692 VLOG(1) << "Detected no-op change to proxy settings. Doing nothing.";
[email protected]3e44697f2009-05-22 14:37:391693 }
1694}
1695
1696void ProxyConfigServiceLinux::Delegate::SetNewProxyConfig(
1697 const ProxyConfig& new_config) {
[email protected]76722472012-05-24 08:26:461698 DCHECK(io_thread_task_runner_->BelongsToCurrentThread());
[email protected]b30a3f52010-10-16 01:05:461699 VLOG(1) << "Proxy configuration changed";
[email protected]3e44697f2009-05-22 14:37:391700 cached_config_ = new_config;
[email protected]3a29593d2011-04-11 10:07:521701 FOR_EACH_OBSERVER(
1702 Observer, observers_,
1703 OnProxyConfigChanged(new_config, ProxyConfigService::CONFIG_VALID));
[email protected]3e44697f2009-05-22 14:37:391704}
1705
1706void ProxyConfigServiceLinux::Delegate::PostDestroyTask() {
[email protected]573c0502011-05-17 22:19:501707 if (!setting_getter_.get())
[email protected]d7395e732009-08-28 23:13:431708 return;
[email protected]76722472012-05-24 08:26:461709 scoped_refptr<base::SingleThreadTaskRunner> shutdown_loop =
1710 setting_getter_->GetNotificationTaskRunner();
[email protected]434fff82012-04-24 21:13:281711 if (!shutdown_loop || shutdown_loop->BelongsToCurrentThread()) {
[email protected]3e44697f2009-05-22 14:37:391712 // Already on the right thread, call directly.
1713 // This is the case for the unittests.
1714 OnDestroy();
1715 } else {
[email protected]d7395e732009-08-28 23:13:431716 // Post to shutdown thread. Note that on browser shutdown, we may quit
1717 // this MessageLoop and exit the program before ever running this.
[email protected]6af889c2011-10-06 23:11:411718 shutdown_loop->PostTask(FROM_HERE, base::Bind(
1719 &ProxyConfigServiceLinux::Delegate::OnDestroy, this));
[email protected]3e44697f2009-05-22 14:37:391720 }
1721}
1722void ProxyConfigServiceLinux::Delegate::OnDestroy() {
[email protected]76722472012-05-24 08:26:461723 scoped_refptr<base::SingleThreadTaskRunner> shutdown_loop =
1724 setting_getter_->GetNotificationTaskRunner();
[email protected]434fff82012-04-24 21:13:281725 DCHECK(!shutdown_loop || shutdown_loop->BelongsToCurrentThread());
[email protected]573c0502011-05-17 22:19:501726 setting_getter_->ShutDown();
[email protected]3e44697f2009-05-22 14:37:391727}
1728
1729ProxyConfigServiceLinux::ProxyConfigServiceLinux()
[email protected]76b90d312010-08-03 03:00:501730 : delegate_(new Delegate(base::Environment::Create())) {
[email protected]3e44697f2009-05-22 14:37:391731}
1732
[email protected]8e1845e12010-09-15 19:22:241733ProxyConfigServiceLinux::~ProxyConfigServiceLinux() {
1734 delegate_->PostDestroyTask();
1735}
1736
[email protected]3e44697f2009-05-22 14:37:391737ProxyConfigServiceLinux::ProxyConfigServiceLinux(
[email protected]76b90d312010-08-03 03:00:501738 base::Environment* env_var_getter)
[email protected]9a3d8d42009-09-03 17:01:461739 : delegate_(new Delegate(env_var_getter)) {
1740}
1741
1742ProxyConfigServiceLinux::ProxyConfigServiceLinux(
[email protected]573c0502011-05-17 22:19:501743 base::Environment* env_var_getter, SettingGetter* setting_getter)
1744 : delegate_(new Delegate(env_var_getter, setting_getter)) {
[email protected]861c6c62009-04-20 16:50:561745}
1746
[email protected]e4be2dd2010-12-14 00:44:391747void ProxyConfigServiceLinux::AddObserver(Observer* observer) {
1748 delegate_->AddObserver(observer);
1749}
1750
1751void ProxyConfigServiceLinux::RemoveObserver(Observer* observer) {
1752 delegate_->RemoveObserver(observer);
1753}
1754
[email protected]3a29593d2011-04-11 10:07:521755ProxyConfigService::ConfigAvailability
1756 ProxyConfigServiceLinux::GetLatestProxyConfig(ProxyConfig* config) {
[email protected]e4be2dd2010-12-14 00:44:391757 return delegate_->GetLatestProxyConfig(config);
1758}
1759
[email protected]861c6c62009-04-20 16:50:561760} // namespace net