blob: fc85947465aacd47fe33a29af3c8198dd3868958 [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)
12#if defined(USE_GIO)
13#include <gio/gio.h>
14#if defined(DLOPEN_GSETTINGS)
15#include <dlfcn.h>
16#endif // defined(DLOPEN_GSETTINGS)
17#endif // defined(USE_GIO)
[email protected]d7395e732009-08-28 23:13:4318#include <limits.h>
19#include <stdio.h>
[email protected]861c6c62009-04-20 16:50:5620#include <stdlib.h>
[email protected]d7395e732009-08-28 23:13:4321#include <sys/inotify.h>
22#include <unistd.h>
[email protected]861c6c62009-04-20 16:50:5623
[email protected]9bc8cff2010-04-03 01:05:3924#include <map>
25
[email protected]6af889c2011-10-06 23:11:4126#include "base/bind.h"
[email protected]c4c1b482011-07-22 17:24:2627#include "base/compiler_specific.h"
[email protected]76b90d312010-08-03 03:00:5028#include "base/environment.h"
[email protected]d7395e732009-08-28 23:13:4329#include "base/file_path.h"
30#include "base/file_util.h"
[email protected]861c6c62009-04-20 16:50:5631#include "base/logging.h"
[email protected]d7395e732009-08-28 23:13:4332#include "base/message_loop.h"
[email protected]3a29593d2011-04-11 10:07:5233#include "base/nix/xdg_util.h"
[email protected]76722472012-05-24 08:26:4634#include "base/single_thread_task_runner.h"
[email protected]528c56d2010-07-30 19:28:4435#include "base/string_number_conversions.h"
[email protected]861c6c62009-04-20 16:50:5636#include "base/string_tokenizer.h"
37#include "base/string_util.h"
[email protected]9a8c4022011-01-25 14:25:3338#include "base/threading/thread_restrictions.h"
[email protected]d7395e732009-08-28 23:13:4339#include "base/timer.h"
[email protected]861c6c62009-04-20 16:50:5640#include "googleurl/src/url_canon.h"
41#include "net/base/net_errors.h"
42#include "net/http/http_util.h"
43#include "net/proxy/proxy_config.h"
44#include "net/proxy/proxy_server.h"
45
46namespace net {
47
48namespace {
49
[email protected]861c6c62009-04-20 16:50:5650// Given a proxy hostname from a setting, returns that hostname with
51// an appropriate proxy server scheme prefix.
52// scheme indicates the desired proxy scheme: usually http, with
53// socks 4 or 5 as special cases.
[email protected]87a102b2009-07-14 05:23:3054// TODO(arindam): Remove URI string manipulation by using MapUrlSchemeToProxy.
[email protected]861c6c62009-04-20 16:50:5655std::string FixupProxyHostScheme(ProxyServer::Scheme scheme,
56 std::string host) {
[email protected]e8c50812010-09-28 00:16:1757 if (scheme == ProxyServer::SCHEME_SOCKS5 &&
58 StartsWithASCII(host, "socks4://", false)) {
59 // We default to socks 5, but if the user specifically set it to
60 // socks4://, then use that.
61 scheme = ProxyServer::SCHEME_SOCKS4;
[email protected]861c6c62009-04-20 16:50:5662 }
63 // Strip the scheme if any.
64 std::string::size_type colon = host.find("://");
65 if (colon != std::string::npos)
66 host = host.substr(colon + 3);
67 // If a username and perhaps password are specified, give a warning.
68 std::string::size_type at_sign = host.find("@");
69 // Should this be supported?
70 if (at_sign != std::string::npos) {
[email protected]62749f182009-07-15 13:16:5471 // ProxyConfig does not support authentication parameters, but Chrome
72 // will prompt for the password later. Disregard the
73 // authentication parameters and continue with this hostname.
74 LOG(WARNING) << "Proxy authentication parameters ignored, see bug 16709";
[email protected]861c6c62009-04-20 16:50:5675 host = host.substr(at_sign + 1);
76 }
77 // If this is a socks proxy, prepend a scheme so as to tell
78 // ProxyServer. This also allows ProxyServer to choose the right
79 // default port.
80 if (scheme == ProxyServer::SCHEME_SOCKS4)
81 host = "socks4://" + host;
82 else if (scheme == ProxyServer::SCHEME_SOCKS5)
83 host = "socks5://" + host;
[email protected]d7395e732009-08-28 23:13:4384 // If there is a trailing slash, remove it so |host| will parse correctly
85 // even if it includes a port number (since the slash is not numeric).
86 if (host.length() && host[host.length() - 1] == '/')
87 host.resize(host.length() - 1);
[email protected]861c6c62009-04-20 16:50:5688 return host;
89}
90
91} // namespace
92
[email protected]8e1845e12010-09-15 19:22:2493ProxyConfigServiceLinux::Delegate::~Delegate() {
94}
95
[email protected]3e44697f2009-05-22 14:37:3996bool ProxyConfigServiceLinux::Delegate::GetProxyFromEnvVarForScheme(
[email protected]861c6c62009-04-20 16:50:5697 const char* variable, ProxyServer::Scheme scheme,
98 ProxyServer* result_server) {
99 std::string env_value;
[email protected]3ba7e082010-08-07 02:57:59100 if (env_var_getter_->GetVar(variable, &env_value)) {
[email protected]861c6c62009-04-20 16:50:56101 if (!env_value.empty()) {
102 env_value = FixupProxyHostScheme(scheme, env_value);
[email protected]87a102b2009-07-14 05:23:30103 ProxyServer proxy_server =
104 ProxyServer::FromURI(env_value, ProxyServer::SCHEME_HTTP);
[email protected]861c6c62009-04-20 16:50:56105 if (proxy_server.is_valid() && !proxy_server.is_direct()) {
106 *result_server = proxy_server;
107 return true;
108 } else {
[email protected]3e44697f2009-05-22 14:37:39109 LOG(ERROR) << "Failed to parse environment variable " << variable;
[email protected]861c6c62009-04-20 16:50:56110 }
111 }
112 }
113 return false;
114}
115
[email protected]3e44697f2009-05-22 14:37:39116bool ProxyConfigServiceLinux::Delegate::GetProxyFromEnvVar(
[email protected]861c6c62009-04-20 16:50:56117 const char* variable, ProxyServer* result_server) {
118 return GetProxyFromEnvVarForScheme(variable, ProxyServer::SCHEME_HTTP,
119 result_server);
120}
121
[email protected]3e44697f2009-05-22 14:37:39122bool ProxyConfigServiceLinux::Delegate::GetConfigFromEnv(ProxyConfig* config) {
[email protected]861c6c62009-04-20 16:50:56123 // Check for automatic configuration first, in
124 // "auto_proxy". Possibly only the "environment_proxy" firefox
125 // extension has ever used this, but it still sounds like a good
126 // idea.
127 std::string auto_proxy;
[email protected]3ba7e082010-08-07 02:57:59128 if (env_var_getter_->GetVar("auto_proxy", &auto_proxy)) {
[email protected]861c6c62009-04-20 16:50:56129 if (auto_proxy.empty()) {
130 // Defined and empty => autodetect
[email protected]ed4ed0f2010-02-24 00:20:48131 config->set_auto_detect(true);
[email protected]861c6c62009-04-20 16:50:56132 } else {
133 // specified autoconfig URL
[email protected]ed4ed0f2010-02-24 00:20:48134 config->set_pac_url(GURL(auto_proxy));
[email protected]861c6c62009-04-20 16:50:56135 }
136 return true;
137 }
138 // "all_proxy" is a shortcut to avoid defining {http,https,ftp}_proxy.
139 ProxyServer proxy_server;
140 if (GetProxyFromEnvVar("all_proxy", &proxy_server)) {
[email protected]ed4ed0f2010-02-24 00:20:48141 config->proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY;
142 config->proxy_rules().single_proxy = proxy_server;
[email protected]861c6c62009-04-20 16:50:56143 } else {
144 bool have_http = GetProxyFromEnvVar("http_proxy", &proxy_server);
145 if (have_http)
[email protected]ed4ed0f2010-02-24 00:20:48146 config->proxy_rules().proxy_for_http = proxy_server;
[email protected]861c6c62009-04-20 16:50:56147 // It would be tempting to let http_proxy apply for all protocols
148 // if https_proxy and ftp_proxy are not defined. Googling turns up
149 // several documents that mention only http_proxy. But then the
150 // user really might not want to proxy https. And it doesn't seem
151 // like other apps do this. So we will refrain.
152 bool have_https = GetProxyFromEnvVar("https_proxy", &proxy_server);
153 if (have_https)
[email protected]ed4ed0f2010-02-24 00:20:48154 config->proxy_rules().proxy_for_https = proxy_server;
[email protected]861c6c62009-04-20 16:50:56155 bool have_ftp = GetProxyFromEnvVar("ftp_proxy", &proxy_server);
156 if (have_ftp)
[email protected]ed4ed0f2010-02-24 00:20:48157 config->proxy_rules().proxy_for_ftp = proxy_server;
[email protected]861c6c62009-04-20 16:50:56158 if (have_http || have_https || have_ftp) {
159 // mustn't change type unless some rules are actually set.
[email protected]ed4ed0f2010-02-24 00:20:48160 config->proxy_rules().type =
161 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME;
[email protected]861c6c62009-04-20 16:50:56162 }
163 }
[email protected]ed4ed0f2010-02-24 00:20:48164 if (config->proxy_rules().empty()) {
[email protected]861c6c62009-04-20 16:50:56165 // If the above were not defined, try for socks.
[email protected]e8c50812010-09-28 00:16:17166 // For environment variables, we default to version 5, per the gnome
167 // documentation: http://library.gnome.org/devel/gnet/stable/gnet-socks.html
168 ProxyServer::Scheme scheme = ProxyServer::SCHEME_SOCKS5;
[email protected]861c6c62009-04-20 16:50:56169 std::string env_version;
[email protected]3ba7e082010-08-07 02:57:59170 if (env_var_getter_->GetVar("SOCKS_VERSION", &env_version)
[email protected]e8c50812010-09-28 00:16:17171 && env_version == "4")
172 scheme = ProxyServer::SCHEME_SOCKS4;
[email protected]861c6c62009-04-20 16:50:56173 if (GetProxyFromEnvVarForScheme("SOCKS_SERVER", scheme, &proxy_server)) {
[email protected]ed4ed0f2010-02-24 00:20:48174 config->proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY;
175 config->proxy_rules().single_proxy = proxy_server;
[email protected]861c6c62009-04-20 16:50:56176 }
177 }
178 // Look for the proxy bypass list.
179 std::string no_proxy;
[email protected]3ba7e082010-08-07 02:57:59180 env_var_getter_->GetVar("no_proxy", &no_proxy);
[email protected]ed4ed0f2010-02-24 00:20:48181 if (config->proxy_rules().empty()) {
[email protected]861c6c62009-04-20 16:50:56182 // Having only "no_proxy" set, presumably to "*", makes it
183 // explicit that env vars do specify a configuration: having no
184 // rules specified only means the user explicitly asks for direct
185 // connections.
186 return !no_proxy.empty();
187 }
[email protected]7541206c2010-02-19 20:24:06188 // Note that this uses "suffix" matching. So a bypass of "google.com"
189 // is understood to mean a bypass of "*google.com".
[email protected]ed4ed0f2010-02-24 00:20:48190 config->proxy_rules().bypass_rules.ParseFromStringUsingSuffixMatching(
191 no_proxy);
[email protected]861c6c62009-04-20 16:50:56192 return true;
193}
194
195namespace {
196
[email protected]d7395e732009-08-28 23:13:43197const int kDebounceTimeoutMilliseconds = 250;
[email protected]3e44697f2009-05-22 14:37:39198
[email protected]6de53d42010-11-09 07:33:19199#if defined(USE_GCONF)
[email protected]573c0502011-05-17 22:19:50200// This setting getter uses gconf, as used in GNOME 2 and some GNOME 3 desktops.
201class SettingGetterImplGConf : public ProxyConfigServiceLinux::SettingGetter {
[email protected]861c6c62009-04-20 16:50:56202 public:
[email protected]573c0502011-05-17 22:19:50203 SettingGetterImplGConf()
[email protected]b160df32012-02-06 20:39:41204 : client_(NULL), system_proxy_id_(0), system_http_proxy_id_(0),
[email protected]76722472012-05-24 08:26:46205 notify_delegate_(NULL) {
[email protected]b160df32012-02-06 20:39:41206 }
[email protected]3e44697f2009-05-22 14:37:39207
[email protected]573c0502011-05-17 22:19:50208 virtual ~SettingGetterImplGConf() {
[email protected]3e44697f2009-05-22 14:37:39209 // client_ should have been released before now, from
[email protected]f5b13442009-07-13 15:23:59210 // Delegate::OnDestroy(), while running on the UI thread. However
[email protected]b160df32012-02-06 20:39:41211 // on exiting the process, it may happen that Delegate::OnDestroy()
212 // task is left pending on the glib loop after the loop was quit,
213 // and pending tasks may then be deleted without being run.
[email protected]f5b13442009-07-13 15:23:59214 if (client_) {
215 // gconf client was not cleaned up.
[email protected]76722472012-05-24 08:26:46216 if (task_runner_->BelongsToCurrentThread()) {
[email protected]f5b13442009-07-13 15:23:59217 // We are on the UI thread so we can clean it safely. This is
218 // the case at least for ui_tests running under Valgrind in
219 // bug 16076.
[email protected]573c0502011-05-17 22:19:50220 VLOG(1) << "~SettingGetterImplGConf: releasing gconf client";
[email protected]d3066142011-05-10 02:36:20221 ShutDown();
[email protected]f5b13442009-07-13 15:23:59222 } else {
[email protected]ed348992011-09-19 20:27:57223 // This is very bad! We are deleting the setting getter but we're not on
224 // the UI thread. This is not supposed to happen: the setting getter is
225 // owned by the proxy config service's delegate, which is supposed to be
226 // destroyed on the UI thread only. We will get change notifications to
227 // a deleted object if we continue here, so fail now.
228 LOG(FATAL) << "~SettingGetterImplGConf: deleting on wrong thread!";
[email protected]f5b13442009-07-13 15:23:59229 }
230 }
[email protected]3e44697f2009-05-22 14:37:39231 DCHECK(!client_);
[email protected]861c6c62009-04-20 16:50:56232 }
233
[email protected]76722472012-05-24 08:26:46234 virtual bool Init(base::SingleThreadTaskRunner* glib_thread_task_runner,
[email protected]c4c1b482011-07-22 17:24:26235 MessageLoopForIO* file_loop) OVERRIDE {
[email protected]76722472012-05-24 08:26:46236 DCHECK(glib_thread_task_runner->BelongsToCurrentThread());
[email protected]3e44697f2009-05-22 14:37:39237 DCHECK(!client_);
[email protected]76722472012-05-24 08:26:46238 DCHECK(!task_runner_);
239 task_runner_ = glib_thread_task_runner;
[email protected]3e44697f2009-05-22 14:37:39240 client_ = gconf_client_get_default();
[email protected]861c6c62009-04-20 16:50:56241 if (!client_) {
[email protected]861c6c62009-04-20 16:50:56242 // It's not clear whether/when this can return NULL.
[email protected]3e44697f2009-05-22 14:37:39243 LOG(ERROR) << "Unable to create a gconf client";
[email protected]76722472012-05-24 08:26:46244 task_runner_ = NULL;
[email protected]3e44697f2009-05-22 14:37:39245 return false;
[email protected]861c6c62009-04-20 16:50:56246 }
[email protected]3e44697f2009-05-22 14:37:39247 GError* error = NULL;
[email protected]b160df32012-02-06 20:39:41248 bool added_system_proxy = false;
[email protected]3e44697f2009-05-22 14:37:39249 // We need to add the directories for which we'll be asking
[email protected]b160df32012-02-06 20:39:41250 // for notifications, and we might as well ask to preload them.
251 // These need to be removed again in ShutDown(); we are careful
252 // here to only leave client_ non-NULL if both have been added.
[email protected]3e44697f2009-05-22 14:37:39253 gconf_client_add_dir(client_, "/system/proxy",
254 GCONF_CLIENT_PRELOAD_ONELEVEL, &error);
255 if (error == NULL) {
[email protected]b160df32012-02-06 20:39:41256 added_system_proxy = true;
[email protected]3e44697f2009-05-22 14:37:39257 gconf_client_add_dir(client_, "/system/http_proxy",
258 GCONF_CLIENT_PRELOAD_ONELEVEL, &error);
259 }
260 if (error != NULL) {
261 LOG(ERROR) << "Error requesting gconf directory: " << error->message;
262 g_error_free(error);
[email protected]b160df32012-02-06 20:39:41263 if (added_system_proxy)
264 gconf_client_remove_dir(client_, "/system/proxy", NULL);
265 g_object_unref(client_);
266 client_ = NULL;
[email protected]76722472012-05-24 08:26:46267 task_runner_ = NULL;
[email protected]3e44697f2009-05-22 14:37:39268 return false;
269 }
270 return true;
271 }
272
[email protected]d3066142011-05-10 02:36:20273 void ShutDown() {
[email protected]3e44697f2009-05-22 14:37:39274 if (client_) {
[email protected]76722472012-05-24 08:26:46275 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]b160df32012-02-06 20:39:41276 // We must explicitly disable gconf notifications here, because the gconf
277 // client will be shared between all setting getters, and they do not all
278 // have the same lifetimes. (For instance, incognito sessions get their
279 // own, which is destroyed when the session ends.)
280 gconf_client_notify_remove(client_, system_http_proxy_id_);
281 gconf_client_notify_remove(client_, system_proxy_id_);
282 gconf_client_remove_dir(client_, "/system/http_proxy", NULL);
283 gconf_client_remove_dir(client_, "/system/proxy", NULL);
[email protected]3e44697f2009-05-22 14:37:39284 g_object_unref(client_);
285 client_ = NULL;
[email protected]76722472012-05-24 08:26:46286 task_runner_ = NULL;
[email protected]3e44697f2009-05-22 14:37:39287 }
288 }
289
[email protected]d3066142011-05-10 02:36:20290 bool SetUpNotifications(ProxyConfigServiceLinux::Delegate* delegate) {
[email protected]3e44697f2009-05-22 14:37:39291 DCHECK(client_);
[email protected]76722472012-05-24 08:26:46292 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]3e44697f2009-05-22 14:37:39293 GError* error = NULL;
[email protected]d7395e732009-08-28 23:13:43294 notify_delegate_ = delegate;
[email protected]b160df32012-02-06 20:39:41295 // We have to keep track of the IDs returned by gconf_client_notify_add() so
296 // that we can remove them in ShutDown(). (Otherwise, notifications will be
297 // delivered to this object after it is deleted, which is bad, m'kay?)
298 system_proxy_id_ = gconf_client_notify_add(
[email protected]3e44697f2009-05-22 14:37:39299 client_, "/system/proxy",
[email protected]d7395e732009-08-28 23:13:43300 OnGConfChangeNotification, this,
[email protected]3e44697f2009-05-22 14:37:39301 NULL, &error);
302 if (error == NULL) {
[email protected]b160df32012-02-06 20:39:41303 system_http_proxy_id_ = gconf_client_notify_add(
[email protected]3e44697f2009-05-22 14:37:39304 client_, "/system/http_proxy",
[email protected]d7395e732009-08-28 23:13:43305 OnGConfChangeNotification, this,
[email protected]3e44697f2009-05-22 14:37:39306 NULL, &error);
307 }
308 if (error != NULL) {
309 LOG(ERROR) << "Error requesting gconf notifications: " << error->message;
310 g_error_free(error);
[email protected]d3066142011-05-10 02:36:20311 ShutDown();
[email protected]3e44697f2009-05-22 14:37:39312 return false;
313 }
[email protected]d3066142011-05-10 02:36:20314 // Simulate a change to avoid possibly losing updates before this point.
315 OnChangeNotification();
[email protected]3e44697f2009-05-22 14:37:39316 return true;
[email protected]861c6c62009-04-20 16:50:56317 }
318
[email protected]76722472012-05-24 08:26:46319 virtual base::SingleThreadTaskRunner* GetNotificationTaskRunner() OVERRIDE {
320 return task_runner_;
[email protected]d7395e732009-08-28 23:13:43321 }
322
[email protected]db8ff912012-06-12 23:32:51323 virtual ProxyConfigSource GetConfigSource() OVERRIDE {
324 return PROXY_CONFIG_SOURCE_GCONF;
[email protected]d7395e732009-08-28 23:13:43325 }
326
[email protected]c4c1b482011-07-22 17:24:26327 virtual bool GetString(StringSetting key, std::string* result) OVERRIDE {
[email protected]573c0502011-05-17 22:19:50328 switch (key) {
329 case PROXY_MODE:
330 return GetStringByPath("/system/proxy/mode", result);
331 case PROXY_AUTOCONF_URL:
332 return GetStringByPath("/system/proxy/autoconfig_url", result);
333 case PROXY_HTTP_HOST:
334 return GetStringByPath("/system/http_proxy/host", result);
335 case PROXY_HTTPS_HOST:
336 return GetStringByPath("/system/proxy/secure_host", result);
337 case PROXY_FTP_HOST:
338 return GetStringByPath("/system/proxy/ftp_host", result);
339 case PROXY_SOCKS_HOST:
340 return GetStringByPath("/system/proxy/socks_host", result);
[email protected]573c0502011-05-17 22:19:50341 }
[email protected]6b5fe742011-05-20 21:46:48342 return false; // Placate compiler.
[email protected]573c0502011-05-17 22:19:50343 }
[email protected]c4c1b482011-07-22 17:24:26344 virtual bool GetBool(BoolSetting key, bool* result) OVERRIDE {
[email protected]573c0502011-05-17 22:19:50345 switch (key) {
346 case PROXY_USE_HTTP_PROXY:
347 return GetBoolByPath("/system/http_proxy/use_http_proxy", result);
348 case PROXY_USE_SAME_PROXY:
349 return GetBoolByPath("/system/http_proxy/use_same_proxy", result);
350 case PROXY_USE_AUTHENTICATION:
351 return GetBoolByPath("/system/http_proxy/use_authentication", result);
[email protected]573c0502011-05-17 22:19:50352 }
[email protected]6b5fe742011-05-20 21:46:48353 return false; // Placate compiler.
[email protected]573c0502011-05-17 22:19:50354 }
[email protected]c4c1b482011-07-22 17:24:26355 virtual bool GetInt(IntSetting key, int* result) OVERRIDE {
[email protected]573c0502011-05-17 22:19:50356 switch (key) {
357 case PROXY_HTTP_PORT:
358 return GetIntByPath("/system/http_proxy/port", result);
359 case PROXY_HTTPS_PORT:
360 return GetIntByPath("/system/proxy/secure_port", result);
361 case PROXY_FTP_PORT:
362 return GetIntByPath("/system/proxy/ftp_port", result);
363 case PROXY_SOCKS_PORT:
364 return GetIntByPath("/system/proxy/socks_port", result);
[email protected]573c0502011-05-17 22:19:50365 }
[email protected]6b5fe742011-05-20 21:46:48366 return false; // Placate compiler.
[email protected]573c0502011-05-17 22:19:50367 }
[email protected]6b5fe742011-05-20 21:46:48368 virtual bool GetStringList(StringListSetting key,
[email protected]c4c1b482011-07-22 17:24:26369 std::vector<std::string>* result) OVERRIDE {
[email protected]573c0502011-05-17 22:19:50370 switch (key) {
371 case PROXY_IGNORE_HOSTS:
372 return GetStringListByPath("/system/http_proxy/ignore_hosts", result);
[email protected]573c0502011-05-17 22:19:50373 }
[email protected]6b5fe742011-05-20 21:46:48374 return false; // Placate compiler.
[email protected]573c0502011-05-17 22:19:50375 }
376
[email protected]c4c1b482011-07-22 17:24:26377 virtual bool BypassListIsReversed() OVERRIDE {
[email protected]573c0502011-05-17 22:19:50378 // This is a KDE-specific setting.
379 return false;
380 }
381
[email protected]c4c1b482011-07-22 17:24:26382 virtual bool MatchHostsUsingSuffixMatching() OVERRIDE {
[email protected]573c0502011-05-17 22:19:50383 return false;
384 }
385
386 private:
387 bool GetStringByPath(const char* key, std::string* result) {
[email protected]3e44697f2009-05-22 14:37:39388 DCHECK(client_);
[email protected]76722472012-05-24 08:26:46389 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]861c6c62009-04-20 16:50:56390 GError* error = NULL;
391 gchar* value = gconf_client_get_string(client_, key, &error);
392 if (HandleGError(error, key))
393 return false;
394 if (!value)
395 return false;
396 *result = value;
397 g_free(value);
398 return true;
399 }
[email protected]573c0502011-05-17 22:19:50400 bool GetBoolByPath(const char* key, bool* result) {
[email protected]3e44697f2009-05-22 14:37:39401 DCHECK(client_);
[email protected]76722472012-05-24 08:26:46402 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]861c6c62009-04-20 16:50:56403 GError* error = NULL;
404 // We want to distinguish unset values from values defaulting to
405 // false. For that we need to use the type-generic
406 // gconf_client_get() rather than gconf_client_get_bool().
407 GConfValue* gconf_value = gconf_client_get(client_, key, &error);
408 if (HandleGError(error, key))
409 return false;
410 if (!gconf_value) {
411 // Unset.
412 return false;
413 }
414 if (gconf_value->type != GCONF_VALUE_BOOL) {
415 gconf_value_free(gconf_value);
416 return false;
417 }
418 gboolean bool_value = gconf_value_get_bool(gconf_value);
419 *result = static_cast<bool>(bool_value);
420 gconf_value_free(gconf_value);
421 return true;
422 }
[email protected]573c0502011-05-17 22:19:50423 bool GetIntByPath(const char* key, int* result) {
[email protected]3e44697f2009-05-22 14:37:39424 DCHECK(client_);
[email protected]76722472012-05-24 08:26:46425 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]861c6c62009-04-20 16:50:56426 GError* error = NULL;
427 int value = gconf_client_get_int(client_, key, &error);
428 if (HandleGError(error, key))
429 return false;
430 // We don't bother to distinguish an unset value because callers
431 // don't care. 0 is returned if unset.
432 *result = value;
433 return true;
434 }
[email protected]573c0502011-05-17 22:19:50435 bool GetStringListByPath(const char* key, std::vector<std::string>* result) {
[email protected]3e44697f2009-05-22 14:37:39436 DCHECK(client_);
[email protected]76722472012-05-24 08:26:46437 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]861c6c62009-04-20 16:50:56438 GError* error = NULL;
439 GSList* list = gconf_client_get_list(client_, key,
440 GCONF_VALUE_STRING, &error);
441 if (HandleGError(error, key))
442 return false;
[email protected]8c20e3d2011-05-19 21:03:57443 if (!list)
[email protected]861c6c62009-04-20 16:50:56444 return false;
[email protected]861c6c62009-04-20 16:50:56445 for (GSList *it = list; it; it = it->next) {
446 result->push_back(static_cast<char*>(it->data));
447 g_free(it->data);
448 }
449 g_slist_free(list);
450 return true;
451 }
452
[email protected]861c6c62009-04-20 16:50:56453 // Logs and frees a glib error. Returns false if there was no error
454 // (error is NULL).
455 bool HandleGError(GError* error, const char* key) {
456 if (error != NULL) {
[email protected]3e44697f2009-05-22 14:37:39457 LOG(ERROR) << "Error getting gconf value for " << key
458 << ": " << error->message;
[email protected]861c6c62009-04-20 16:50:56459 g_error_free(error);
460 return true;
461 }
462 return false;
463 }
464
[email protected]d7395e732009-08-28 23:13:43465 // This is the callback from the debounce timer.
466 void OnDebouncedNotification() {
[email protected]76722472012-05-24 08:26:46467 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]961ac942011-04-28 18:18:14468 CHECK(notify_delegate_);
[email protected]d7395e732009-08-28 23:13:43469 // Forward to a method on the proxy config service delegate object.
470 notify_delegate_->OnCheckProxyConfigSettings();
471 }
472
473 void OnChangeNotification() {
474 // We don't use Reset() because the timer may not yet be running.
475 // (In that case Stop() is a no-op.)
476 debounce_timer_.Stop();
[email protected]d323a172011-09-02 18:23:02477 debounce_timer_.Start(FROM_HERE,
[email protected]8c20e3d2011-05-19 21:03:57478 base::TimeDelta::FromMilliseconds(kDebounceTimeoutMilliseconds),
479 this, &SettingGetterImplGConf::OnDebouncedNotification);
[email protected]d7395e732009-08-28 23:13:43480 }
481
[email protected]8c20e3d2011-05-19 21:03:57482 // gconf notification callback, dispatched on the default glib main loop.
483 static void OnGConfChangeNotification(GConfClient* client, guint cnxn_id,
484 GConfEntry* entry, gpointer user_data) {
[email protected]b30a3f52010-10-16 01:05:46485 VLOG(1) << "gconf change notification for key "
486 << gconf_entry_get_key(entry);
[email protected]d7395e732009-08-28 23:13:43487 // We don't track which key has changed, just that something did change.
[email protected]573c0502011-05-17 22:19:50488 SettingGetterImplGConf* setting_getter =
489 reinterpret_cast<SettingGetterImplGConf*>(user_data);
[email protected]d7395e732009-08-28 23:13:43490 setting_getter->OnChangeNotification();
491 }
492
[email protected]861c6c62009-04-20 16:50:56493 GConfClient* client_;
[email protected]b160df32012-02-06 20:39:41494 // These ids are the values returned from gconf_client_notify_add(), which we
495 // will need in order to later call gconf_client_notify_remove().
496 guint system_proxy_id_;
497 guint system_http_proxy_id_;
498
[email protected]d7395e732009-08-28 23:13:43499 ProxyConfigServiceLinux::Delegate* notify_delegate_;
[email protected]573c0502011-05-17 22:19:50500 base::OneShotTimer<SettingGetterImplGConf> debounce_timer_;
[email protected]861c6c62009-04-20 16:50:56501
[email protected]76722472012-05-24 08:26:46502 // Task runner for the thread that we make gconf calls on. It should
[email protected]3e44697f2009-05-22 14:37:39503 // be the UI thread and all our methods should be called on this
504 // thread. Only for assertions.
[email protected]76722472012-05-24 08:26:46505 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
[email protected]3e44697f2009-05-22 14:37:39506
[email protected]573c0502011-05-17 22:19:50507 DISALLOW_COPY_AND_ASSIGN(SettingGetterImplGConf);
[email protected]d7395e732009-08-28 23:13:43508};
[email protected]6de53d42010-11-09 07:33:19509#endif // defined(USE_GCONF)
[email protected]d7395e732009-08-28 23:13:43510
[email protected]8c20e3d2011-05-19 21:03:57511#if defined(USE_GIO)
512// This setting getter uses gsettings, as used in most GNOME 3 desktops.
513class SettingGetterImplGSettings
514 : public ProxyConfigServiceLinux::SettingGetter {
515 public:
[email protected]0e14e87d2011-06-21 21:24:19516 SettingGetterImplGSettings() :
517#if defined(DLOPEN_GSETTINGS)
518 g_settings_new(NULL),
519 g_settings_get_child(NULL),
520 g_settings_get_boolean(NULL),
521 g_settings_get_string(NULL),
522 g_settings_get_int(NULL),
523 g_settings_get_strv(NULL),
524 g_settings_list_schemas(NULL),
525 gio_handle_(NULL),
526#endif
527 client_(NULL),
528 http_client_(NULL),
529 https_client_(NULL),
530 ftp_client_(NULL),
531 socks_client_(NULL),
[email protected]76722472012-05-24 08:26:46532 notify_delegate_(NULL) {
[email protected]8c20e3d2011-05-19 21:03:57533 }
534
535 virtual ~SettingGetterImplGSettings() {
536 // client_ should have been released before now, from
537 // Delegate::OnDestroy(), while running on the UI thread. However
538 // on exiting the process, it may happen that
539 // Delegate::OnDestroy() task is left pending on the glib loop
540 // after the loop was quit, and pending tasks may then be deleted
541 // without being run.
542 if (client_) {
543 // gconf client was not cleaned up.
[email protected]76722472012-05-24 08:26:46544 if (task_runner_->BelongsToCurrentThread()) {
[email protected]8c20e3d2011-05-19 21:03:57545 // We are on the UI thread so we can clean it safely. This is
546 // the case at least for ui_tests running under Valgrind in
547 // bug 16076.
548 VLOG(1) << "~SettingGetterImplGSettings: releasing gsettings client";
549 ShutDown();
550 } else {
551 LOG(WARNING) << "~SettingGetterImplGSettings: leaking gsettings client";
552 client_ = NULL;
553 }
554 }
555 DCHECK(!client_);
556#if defined(DLOPEN_GSETTINGS)
557 if (gio_handle_) {
558 dlclose(gio_handle_);
559 gio_handle_ = NULL;
560 }
561#endif
562 }
563
[email protected]4cf80f0b2011-05-20 20:30:26564 bool SchemaExists(const char* schema_name) {
565 const gchar* const* schemas = g_settings_list_schemas();
566 while (*schemas) {
[email protected]a099f3ae2011-08-16 21:06:58567 if (strcmp(schema_name, static_cast<const char*>(*schemas)) == 0)
[email protected]4cf80f0b2011-05-20 20:30:26568 return true;
569 schemas++;
570 }
571 return false;
572 }
573
[email protected]8c20e3d2011-05-19 21:03:57574 // LoadAndCheckVersion() must be called *before* Init()!
575 bool LoadAndCheckVersion(base::Environment* env);
576
[email protected]76722472012-05-24 08:26:46577 virtual bool Init(base::SingleThreadTaskRunner* glib_thread_task_runner,
[email protected]c4c1b482011-07-22 17:24:26578 MessageLoopForIO* file_loop) OVERRIDE {
[email protected]76722472012-05-24 08:26:46579 DCHECK(glib_thread_task_runner->BelongsToCurrentThread());
[email protected]8c20e3d2011-05-19 21:03:57580 DCHECK(!client_);
[email protected]76722472012-05-24 08:26:46581 DCHECK(!task_runner_);
[email protected]4cf80f0b2011-05-20 20:30:26582
583 if (!SchemaExists("org.gnome.system.proxy") ||
584 !(client_ = g_settings_new("org.gnome.system.proxy"))) {
[email protected]8c20e3d2011-05-19 21:03:57585 // It's not clear whether/when this can return NULL.
586 LOG(ERROR) << "Unable to create a gsettings client";
587 return false;
588 }
[email protected]76722472012-05-24 08:26:46589 task_runner_ = glib_thread_task_runner;
[email protected]8c20e3d2011-05-19 21:03:57590 // We assume these all work if the above call worked.
591 http_client_ = g_settings_get_child(client_, "http");
592 https_client_ = g_settings_get_child(client_, "https");
593 ftp_client_ = g_settings_get_child(client_, "ftp");
594 socks_client_ = g_settings_get_child(client_, "socks");
595 DCHECK(http_client_ && https_client_ && ftp_client_ && socks_client_);
596 return true;
597 }
598
599 void ShutDown() {
600 if (client_) {
[email protected]76722472012-05-24 08:26:46601 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]8c20e3d2011-05-19 21:03:57602 // This also disables gsettings notifications.
603 g_object_unref(socks_client_);
604 g_object_unref(ftp_client_);
605 g_object_unref(https_client_);
606 g_object_unref(http_client_);
607 g_object_unref(client_);
608 // We only need to null client_ because it's the only one that we check.
609 client_ = NULL;
[email protected]76722472012-05-24 08:26:46610 task_runner_ = NULL;
[email protected]8c20e3d2011-05-19 21:03:57611 }
612 }
613
614 bool SetUpNotifications(ProxyConfigServiceLinux::Delegate* delegate) {
615 DCHECK(client_);
[email protected]76722472012-05-24 08:26:46616 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]8c20e3d2011-05-19 21:03:57617 notify_delegate_ = delegate;
618 // We could watch for the change-event signal instead of changed, but
619 // since we have to watch more than one object, we'd still have to
620 // debounce change notifications. This is conceptually simpler.
621 g_signal_connect(G_OBJECT(client_), "changed",
622 G_CALLBACK(OnGSettingsChangeNotification), this);
623 g_signal_connect(G_OBJECT(http_client_), "changed",
624 G_CALLBACK(OnGSettingsChangeNotification), this);
625 g_signal_connect(G_OBJECT(https_client_), "changed",
626 G_CALLBACK(OnGSettingsChangeNotification), this);
627 g_signal_connect(G_OBJECT(ftp_client_), "changed",
628 G_CALLBACK(OnGSettingsChangeNotification), this);
629 g_signal_connect(G_OBJECT(socks_client_), "changed",
630 G_CALLBACK(OnGSettingsChangeNotification), this);
631 // Simulate a change to avoid possibly losing updates before this point.
632 OnChangeNotification();
633 return true;
634 }
635
[email protected]76722472012-05-24 08:26:46636 virtual base::SingleThreadTaskRunner* GetNotificationTaskRunner() OVERRIDE {
637 return task_runner_;
[email protected]8c20e3d2011-05-19 21:03:57638 }
639
[email protected]db8ff912012-06-12 23:32:51640 virtual ProxyConfigSource GetConfigSource() OVERRIDE {
641 return PROXY_CONFIG_SOURCE_GSETTINGS;
[email protected]8c20e3d2011-05-19 21:03:57642 }
643
[email protected]c4c1b482011-07-22 17:24:26644 virtual bool GetString(StringSetting key, std::string* result) OVERRIDE {
[email protected]8c20e3d2011-05-19 21:03:57645 DCHECK(client_);
646 switch (key) {
647 case PROXY_MODE:
648 return GetStringByPath(client_, "mode", result);
649 case PROXY_AUTOCONF_URL:
650 return GetStringByPath(client_, "autoconfig-url", result);
651 case PROXY_HTTP_HOST:
652 return GetStringByPath(http_client_, "host", result);
653 case PROXY_HTTPS_HOST:
654 return GetStringByPath(https_client_, "host", result);
655 case PROXY_FTP_HOST:
656 return GetStringByPath(ftp_client_, "host", result);
657 case PROXY_SOCKS_HOST:
658 return GetStringByPath(socks_client_, "host", result);
[email protected]8c20e3d2011-05-19 21:03:57659 }
[email protected]6b5fe742011-05-20 21:46:48660 return false; // Placate compiler.
[email protected]8c20e3d2011-05-19 21:03:57661 }
[email protected]c4c1b482011-07-22 17:24:26662 virtual bool GetBool(BoolSetting key, bool* result) OVERRIDE {
[email protected]8c20e3d2011-05-19 21:03:57663 DCHECK(client_);
664 switch (key) {
665 case PROXY_USE_HTTP_PROXY:
666 // Although there is an "enabled" boolean in http_client_, it is not set
667 // to true by the proxy config utility. We ignore it and return false.
668 return false;
669 case PROXY_USE_SAME_PROXY:
670 // Similarly, although there is a "use-same-proxy" boolean in client_,
671 // it is never set to false by the proxy config utility. We ignore it.
672 return false;
673 case PROXY_USE_AUTHENTICATION:
674 // There is also no way to set this in the proxy config utility, but it
675 // doesn't hurt us to get the actual setting (unlike the two above).
676 return GetBoolByPath(http_client_, "use-authentication", result);
[email protected]8c20e3d2011-05-19 21:03:57677 }
[email protected]6b5fe742011-05-20 21:46:48678 return false; // Placate compiler.
[email protected]8c20e3d2011-05-19 21:03:57679 }
[email protected]c4c1b482011-07-22 17:24:26680 virtual bool GetInt(IntSetting key, int* result) OVERRIDE {
[email protected]8c20e3d2011-05-19 21:03:57681 DCHECK(client_);
682 switch (key) {
683 case PROXY_HTTP_PORT:
684 return GetIntByPath(http_client_, "port", result);
685 case PROXY_HTTPS_PORT:
686 return GetIntByPath(https_client_, "port", result);
687 case PROXY_FTP_PORT:
688 return GetIntByPath(ftp_client_, "port", result);
689 case PROXY_SOCKS_PORT:
690 return GetIntByPath(socks_client_, "port", result);
[email protected]8c20e3d2011-05-19 21:03:57691 }
[email protected]6b5fe742011-05-20 21:46:48692 return false; // Placate compiler.
[email protected]8c20e3d2011-05-19 21:03:57693 }
[email protected]6b5fe742011-05-20 21:46:48694 virtual bool GetStringList(StringListSetting key,
[email protected]c4c1b482011-07-22 17:24:26695 std::vector<std::string>* result) OVERRIDE {
[email protected]8c20e3d2011-05-19 21:03:57696 DCHECK(client_);
697 switch (key) {
698 case PROXY_IGNORE_HOSTS:
699 return GetStringListByPath(client_, "ignore-hosts", result);
[email protected]8c20e3d2011-05-19 21:03:57700 }
[email protected]6b5fe742011-05-20 21:46:48701 return false; // Placate compiler.
[email protected]8c20e3d2011-05-19 21:03:57702 }
703
[email protected]c4c1b482011-07-22 17:24:26704 virtual bool BypassListIsReversed() OVERRIDE {
[email protected]8c20e3d2011-05-19 21:03:57705 // This is a KDE-specific setting.
706 return false;
707 }
708
[email protected]c4c1b482011-07-22 17:24:26709 virtual bool MatchHostsUsingSuffixMatching() OVERRIDE {
[email protected]8c20e3d2011-05-19 21:03:57710 return false;
711 }
712
713 private:
714#if defined(DLOPEN_GSETTINGS)
715 // We replicate the prototypes for the g_settings APIs we need. We may not
716 // even be compiling on a system that has them. If we are, these won't
717 // conflict both because they are identical and also due to scoping. The
718 // scoping will also ensure that these get used instead of the global ones.
719 struct _GSettings;
720 typedef struct _GSettings GSettings;
721 GSettings* (*g_settings_new)(const gchar* schema);
722 GSettings* (*g_settings_get_child)(GSettings* settings, const gchar* name);
723 gboolean (*g_settings_get_boolean)(GSettings* settings, const gchar* key);
724 gchar* (*g_settings_get_string)(GSettings* settings, const gchar* key);
725 gint (*g_settings_get_int)(GSettings* settings, const gchar* key);
726 gchar** (*g_settings_get_strv)(GSettings* settings, const gchar* key);
[email protected]4cf80f0b2011-05-20 20:30:26727 const gchar* const* (*g_settings_list_schemas)();
[email protected]8c20e3d2011-05-19 21:03:57728
729 // The library handle.
730 void* gio_handle_;
731
732 // Load a symbol from |gio_handle_| and store it into |*func_ptr|.
733 bool LoadSymbol(const char* name, void** func_ptr) {
734 dlerror();
735 *func_ptr = dlsym(gio_handle_, name);
736 const char* error = dlerror();
737 if (error) {
738 VLOG(1) << "Unable to load symbol " << name << ": " << error;
739 return false;
740 }
741 return true;
742 }
743#endif // defined(DLOPEN_GSETTINGS)
744
745 bool GetStringByPath(GSettings* client, const char* key,
746 std::string* result) {
[email protected]76722472012-05-24 08:26:46747 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]8c20e3d2011-05-19 21:03:57748 gchar* value = g_settings_get_string(client, key);
749 if (!value)
750 return false;
751 *result = value;
752 g_free(value);
753 return true;
754 }
755 bool GetBoolByPath(GSettings* client, const char* key, bool* result) {
[email protected]76722472012-05-24 08:26:46756 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]8c20e3d2011-05-19 21:03:57757 *result = static_cast<bool>(g_settings_get_boolean(client, key));
758 return true;
759 }
760 bool GetIntByPath(GSettings* client, const char* key, int* result) {
[email protected]76722472012-05-24 08:26:46761 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]8c20e3d2011-05-19 21:03:57762 *result = g_settings_get_int(client, key);
763 return true;
764 }
765 bool GetStringListByPath(GSettings* client, const char* key,
766 std::vector<std::string>* result) {
[email protected]76722472012-05-24 08:26:46767 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]8c20e3d2011-05-19 21:03:57768 gchar** list = g_settings_get_strv(client, key);
769 if (!list)
770 return false;
771 for (size_t i = 0; list[i]; ++i) {
772 result->push_back(static_cast<char*>(list[i]));
773 g_free(list[i]);
774 }
775 g_free(list);
776 return true;
777 }
778
779 // This is the callback from the debounce timer.
780 void OnDebouncedNotification() {
[email protected]76722472012-05-24 08:26:46781 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]8c20e3d2011-05-19 21:03:57782 CHECK(notify_delegate_);
783 // Forward to a method on the proxy config service delegate object.
784 notify_delegate_->OnCheckProxyConfigSettings();
785 }
786
787 void OnChangeNotification() {
788 // We don't use Reset() because the timer may not yet be running.
789 // (In that case Stop() is a no-op.)
790 debounce_timer_.Stop();
[email protected]d323a172011-09-02 18:23:02791 debounce_timer_.Start(FROM_HERE,
[email protected]8c20e3d2011-05-19 21:03:57792 base::TimeDelta::FromMilliseconds(kDebounceTimeoutMilliseconds),
793 this, &SettingGetterImplGSettings::OnDebouncedNotification);
794 }
795
796 // gsettings notification callback, dispatched on the default glib main loop.
797 static void OnGSettingsChangeNotification(GSettings* client, gchar* key,
798 gpointer user_data) {
799 VLOG(1) << "gsettings change notification for key " << key;
800 // We don't track which key has changed, just that something did change.
801 SettingGetterImplGSettings* setting_getter =
802 reinterpret_cast<SettingGetterImplGSettings*>(user_data);
803 setting_getter->OnChangeNotification();
804 }
805
806 GSettings* client_;
807 GSettings* http_client_;
808 GSettings* https_client_;
809 GSettings* ftp_client_;
810 GSettings* socks_client_;
811 ProxyConfigServiceLinux::Delegate* notify_delegate_;
812 base::OneShotTimer<SettingGetterImplGSettings> debounce_timer_;
813
[email protected]76722472012-05-24 08:26:46814 // Task runner for the thread that we make gsettings calls on. It should
[email protected]8c20e3d2011-05-19 21:03:57815 // be the UI thread and all our methods should be called on this
816 // thread. Only for assertions.
[email protected]76722472012-05-24 08:26:46817 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
[email protected]8c20e3d2011-05-19 21:03:57818
819 DISALLOW_COPY_AND_ASSIGN(SettingGetterImplGSettings);
820};
821
822bool SettingGetterImplGSettings::LoadAndCheckVersion(
823 base::Environment* env) {
824 // LoadAndCheckVersion() must be called *before* Init()!
825 DCHECK(!client_);
826
827 // The APIs to query gsettings were introduced after the minimum glib
828 // version we target, so we can't link directly against them. We load them
829 // dynamically at runtime, and if they don't exist, return false here. (We
830 // support linking directly via gyp flags though.) Additionally, even when
831 // they are present, we do two additional checks to make sure we should use
832 // them and not gconf. First, we attempt to load the schema for proxy
833 // settings. Second, we check for the program that was used in older
834 // versions of GNOME to configure proxy settings, and return false if it
835 // exists. Some distributions (e.g. Ubuntu 11.04) have the API and schema
836 // but don't use gsettings for proxy settings, but they do have the old
837 // binary, so we detect these systems that way.
838
839#ifdef DLOPEN_GSETTINGS
[email protected]5b5b18a2011-09-22 20:27:16840 gio_handle_ = dlopen("libgio-2.0.so.0", RTLD_NOW | RTLD_GLOBAL);
[email protected]8c20e3d2011-05-19 21:03:57841 if (!gio_handle_) {
[email protected]5b5b18a2011-09-22 20:27:16842 // Try again without .0 at the end; on some systems this may be required.
843 gio_handle_ = dlopen("libgio-2.0.so", RTLD_NOW | RTLD_GLOBAL);
844 if (!gio_handle_) {
845 VLOG(1) << "Cannot load gio library. Will fall back to gconf.";
846 return false;
847 }
[email protected]8c20e3d2011-05-19 21:03:57848 }
849 if (!LoadSymbol("g_settings_new",
850 reinterpret_cast<void**>(&g_settings_new)) ||
851 !LoadSymbol("g_settings_get_child",
852 reinterpret_cast<void**>(&g_settings_get_child)) ||
853 !LoadSymbol("g_settings_get_string",
854 reinterpret_cast<void**>(&g_settings_get_string)) ||
855 !LoadSymbol("g_settings_get_boolean",
856 reinterpret_cast<void**>(&g_settings_get_boolean)) ||
857 !LoadSymbol("g_settings_get_int",
858 reinterpret_cast<void**>(&g_settings_get_int)) ||
859 !LoadSymbol("g_settings_get_strv",
[email protected]4cf80f0b2011-05-20 20:30:26860 reinterpret_cast<void**>(&g_settings_get_strv)) ||
861 !LoadSymbol("g_settings_list_schemas",
862 reinterpret_cast<void**>(&g_settings_list_schemas))) {
[email protected]8c20e3d2011-05-19 21:03:57863 VLOG(1) << "Cannot load gsettings API. Will fall back to gconf.";
864 dlclose(gio_handle_);
865 gio_handle_ = NULL;
866 return false;
867 }
868#endif
869
[email protected]4cf80f0b2011-05-20 20:30:26870 GSettings* client;
871 if (!SchemaExists("org.gnome.system.proxy") ||
872 !(client = g_settings_new("org.gnome.system.proxy"))) {
[email protected]8c20e3d2011-05-19 21:03:57873 VLOG(1) << "Cannot create gsettings client. Will fall back to gconf.";
874 return false;
875 }
876 g_object_unref(client);
877
878 std::string path;
879 if (!env->GetVar("PATH", &path)) {
880 LOG(ERROR) << "No $PATH variable. Assuming no gnome-network-properties.";
881 } else {
882 // Yes, we're on the UI thread. Yes, we're accessing the file system.
883 // Sadly, we don't have much choice. We need the proxy settings and we
884 // need them now, and to figure out where to get them, we have to check
885 // for this binary. See http://crbug.com/69057 for additional details.
886 base::ThreadRestrictions::ScopedAllowIO allow_io;
887 std::vector<std::string> paths;
888 Tokenize(path, ":", &paths);
889 for (size_t i = 0; i < paths.size(); ++i) {
890 FilePath file(paths[i]);
891 if (file_util::PathExists(file.Append("gnome-network-properties"))) {
892 VLOG(1) << "Found gnome-network-properties. Will fall back to gconf.";
893 return false;
894 }
895 }
896 }
897
898 VLOG(1) << "All gsettings tests OK. Will get proxy config from gsettings.";
899 return true;
900}
901#endif // defined(USE_GIO)
902
[email protected]d7395e732009-08-28 23:13:43903// This is the KDE version that reads kioslaverc and simulates gconf.
904// Doing this allows the main Delegate code, as well as the unit tests
905// for it, to stay the same - and the settings map fairly well besides.
[email protected]573c0502011-05-17 22:19:50906class SettingGetterImplKDE : public ProxyConfigServiceLinux::SettingGetter,
907 public base::MessagePumpLibevent::Watcher {
[email protected]d7395e732009-08-28 23:13:43908 public:
[email protected]573c0502011-05-17 22:19:50909 explicit SettingGetterImplKDE(base::Environment* env_var_getter)
[email protected]d7395e732009-08-28 23:13:43910 : inotify_fd_(-1), notify_delegate_(NULL), indirect_manual_(false),
[email protected]a48bf4a2010-06-14 18:24:53911 auto_no_pac_(false), reversed_bypass_list_(false),
[email protected]f18fde22010-05-18 23:49:54912 env_var_getter_(env_var_getter), file_loop_(NULL) {
[email protected]9a8c4022011-01-25 14:25:33913 // This has to be called on the UI thread (http://crbug.com/69057).
914 base::ThreadRestrictions::ScopedAllowIO allow_io;
915
[email protected]f18fde22010-05-18 23:49:54916 // Derive the location of the kde config dir from the environment.
[email protected]92d2dc82010-04-08 17:49:59917 std::string home;
[email protected]3ba7e082010-08-07 02:57:59918 if (env_var_getter->GetVar("KDEHOME", &home) && !home.empty()) {
[email protected]2e8cfe22010-06-12 00:26:24919 // $KDEHOME is set. Use it unconditionally.
[email protected]92d2dc82010-04-08 17:49:59920 kde_config_dir_ = KDEHomeToConfigPath(FilePath(home));
921 } else {
[email protected]2e8cfe22010-06-12 00:26:24922 // $KDEHOME is unset. Try to figure out what to use. This seems to be
[email protected]92d2dc82010-04-08 17:49:59923 // the common case on most distributions.
[email protected]3ba7e082010-08-07 02:57:59924 if (!env_var_getter->GetVar(base::env_vars::kHome, &home))
[email protected]d7395e732009-08-28 23:13:43925 // User has no $HOME? Give up. Later we'll report the failure.
926 return;
[email protected]6b0349ef2010-10-16 04:56:06927 if (base::nix::GetDesktopEnvironment(env_var_getter) ==
928 base::nix::DESKTOP_ENVIRONMENT_KDE3) {
[email protected]92d2dc82010-04-08 17:49:59929 // KDE3 always uses .kde for its configuration.
930 FilePath kde_path = FilePath(home).Append(".kde");
931 kde_config_dir_ = KDEHomeToConfigPath(kde_path);
932 } else {
933 // Some distributions patch KDE4 to use .kde4 instead of .kde, so that
[email protected]fad9c8a52010-06-10 22:30:53934 // both can be installed side-by-side. Sadly they don't all do this, and
935 // they don't always do this: some distributions have started switching
936 // back as well. So if there is a .kde4 directory, check the timestamps
937 // of the config directories within and use the newest one.
[email protected]92d2dc82010-04-08 17:49:59938 // Note that we should currently be running in the UI thread, because in
939 // the gconf version, that is the only thread that can access the proxy
940 // settings (a gconf restriction). As noted below, the initial read of
941 // the proxy settings will be done in this thread anyway, so we check
942 // for .kde4 here in this thread as well.
[email protected]fad9c8a52010-06-10 22:30:53943 FilePath kde3_path = FilePath(home).Append(".kde");
944 FilePath kde3_config = KDEHomeToConfigPath(kde3_path);
[email protected]92d2dc82010-04-08 17:49:59945 FilePath kde4_path = FilePath(home).Append(".kde4");
[email protected]fad9c8a52010-06-10 22:30:53946 FilePath kde4_config = KDEHomeToConfigPath(kde4_path);
947 bool use_kde4 = false;
[email protected]92d2dc82010-04-08 17:49:59948 if (file_util::DirectoryExists(kde4_path)) {
[email protected]2f0193c22010-09-03 02:28:37949 base::PlatformFileInfo kde3_info;
950 base::PlatformFileInfo kde4_info;
[email protected]fad9c8a52010-06-10 22:30:53951 if (file_util::GetFileInfo(kde4_config, &kde4_info)) {
952 if (file_util::GetFileInfo(kde3_config, &kde3_info)) {
953 use_kde4 = kde4_info.last_modified >= kde3_info.last_modified;
954 } else {
955 use_kde4 = true;
956 }
957 }
958 }
959 if (use_kde4) {
[email protected]92d2dc82010-04-08 17:49:59960 kde_config_dir_ = KDEHomeToConfigPath(kde4_path);
961 } else {
[email protected]fad9c8a52010-06-10 22:30:53962 kde_config_dir_ = KDEHomeToConfigPath(kde3_path);
[email protected]92d2dc82010-04-08 17:49:59963 }
964 }
[email protected]d7395e732009-08-28 23:13:43965 }
[email protected]d7395e732009-08-28 23:13:43966 }
967
[email protected]573c0502011-05-17 22:19:50968 virtual ~SettingGetterImplKDE() {
[email protected]d7395e732009-08-28 23:13:43969 // inotify_fd_ should have been closed before now, from
970 // Delegate::OnDestroy(), while running on the file thread. However
971 // on exiting the process, it may happen that Delegate::OnDestroy()
972 // task is left pending on the file loop after the loop was quit,
973 // and pending tasks may then be deleted without being run.
974 // Here in the KDE version, we can safely close the file descriptor
975 // anyway. (Not that it really matters; the process is exiting.)
976 if (inotify_fd_ >= 0)
[email protected]d3066142011-05-10 02:36:20977 ShutDown();
[email protected]d7395e732009-08-28 23:13:43978 DCHECK(inotify_fd_ < 0);
979 }
980
[email protected]76722472012-05-24 08:26:46981 virtual bool Init(base::SingleThreadTaskRunner* glib_thread_task_runner,
[email protected]c4c1b482011-07-22 17:24:26982 MessageLoopForIO* file_loop) OVERRIDE {
[email protected]9a8c4022011-01-25 14:25:33983 // This has to be called on the UI thread (http://crbug.com/69057).
984 base::ThreadRestrictions::ScopedAllowIO allow_io;
[email protected]d7395e732009-08-28 23:13:43985 DCHECK(inotify_fd_ < 0);
986 inotify_fd_ = inotify_init();
987 if (inotify_fd_ < 0) {
[email protected]57b765672009-10-13 18:27:40988 PLOG(ERROR) << "inotify_init failed";
[email protected]d7395e732009-08-28 23:13:43989 return false;
990 }
991 int flags = fcntl(inotify_fd_, F_GETFL);
992 if (fcntl(inotify_fd_, F_SETFL, flags | O_NONBLOCK) < 0) {
[email protected]57b765672009-10-13 18:27:40993 PLOG(ERROR) << "fcntl failed";
[email protected]d7395e732009-08-28 23:13:43994 close(inotify_fd_);
995 inotify_fd_ = -1;
996 return false;
997 }
998 file_loop_ = file_loop;
999 // The initial read is done on the current thread, not |file_loop_|,
[email protected]d3066142011-05-10 02:36:201000 // since we will need to have it for SetUpAndFetchInitialConfig().
[email protected]d7395e732009-08-28 23:13:431001 UpdateCachedSettings();
1002 return true;
1003 }
1004
[email protected]d3066142011-05-10 02:36:201005 void ShutDown() {
[email protected]d7395e732009-08-28 23:13:431006 if (inotify_fd_ >= 0) {
1007 ResetCachedSettings();
1008 inotify_watcher_.StopWatchingFileDescriptor();
1009 close(inotify_fd_);
1010 inotify_fd_ = -1;
1011 }
1012 }
1013
[email protected]d3066142011-05-10 02:36:201014 bool SetUpNotifications(ProxyConfigServiceLinux::Delegate* delegate) {
[email protected]d7395e732009-08-28 23:13:431015 DCHECK(inotify_fd_ >= 0);
[email protected]d3066142011-05-10 02:36:201016 DCHECK(MessageLoop::current() == file_loop_);
[email protected]d7395e732009-08-28 23:13:431017 // We can't just watch the kioslaverc file directly, since KDE will write
1018 // a new copy of it and then rename it whenever settings are changed and
1019 // inotify watches inodes (so we'll be watching the old deleted file after
1020 // the first change, and it will never change again). So, we watch the
1021 // directory instead. We then act only on changes to the kioslaverc entry.
1022 if (inotify_add_watch(inotify_fd_, kde_config_dir_.value().c_str(),
1023 IN_MODIFY | IN_MOVED_TO) < 0)
1024 return false;
1025 notify_delegate_ = delegate;
[email protected]d3066142011-05-10 02:36:201026 if (!file_loop_->WatchFileDescriptor(inotify_fd_, true,
1027 MessageLoopForIO::WATCH_READ, &inotify_watcher_, this))
1028 return false;
1029 // Simulate a change to avoid possibly losing updates before this point.
1030 OnChangeNotification();
1031 return true;
[email protected]d7395e732009-08-28 23:13:431032 }
1033
[email protected]76722472012-05-24 08:26:461034 virtual base::SingleThreadTaskRunner* GetNotificationTaskRunner() OVERRIDE {
[email protected]051e71e2012-06-02 22:13:171035 return file_loop_ ? file_loop_->message_loop_proxy() : NULL;
[email protected]d7395e732009-08-28 23:13:431036 }
1037
[email protected]b160df32012-02-06 20:39:411038 // Implement base::MessagePumpLibevent::Watcher.
[email protected]d7395e732009-08-28 23:13:431039 void OnFileCanReadWithoutBlocking(int fd) {
[email protected]d2e6d592012-02-03 21:49:041040 DCHECK_EQ(fd, inotify_fd_);
[email protected]d7395e732009-08-28 23:13:431041 DCHECK(MessageLoop::current() == file_loop_);
1042 OnChangeNotification();
1043 }
1044 void OnFileCanWriteWithoutBlocking(int fd) {
1045 NOTREACHED();
1046 }
1047
[email protected]db8ff912012-06-12 23:32:511048 virtual ProxyConfigSource GetConfigSource() OVERRIDE {
1049 return PROXY_CONFIG_SOURCE_KDE;
[email protected]d7395e732009-08-28 23:13:431050 }
1051
[email protected]c4c1b482011-07-22 17:24:261052 virtual bool GetString(StringSetting key, std::string* result) OVERRIDE {
[email protected]d7395e732009-08-28 23:13:431053 string_map_type::iterator it = string_table_.find(key);
1054 if (it == string_table_.end())
1055 return false;
1056 *result = it->second;
1057 return true;
1058 }
[email protected]c4c1b482011-07-22 17:24:261059 virtual bool GetBool(BoolSetting key, bool* result) OVERRIDE {
[email protected]d7395e732009-08-28 23:13:431060 // We don't ever have any booleans.
1061 return false;
1062 }
[email protected]c4c1b482011-07-22 17:24:261063 virtual bool GetInt(IntSetting key, int* result) OVERRIDE {
[email protected]d7395e732009-08-28 23:13:431064 // We don't ever have any integers. (See AddProxy() below about ports.)
1065 return false;
1066 }
[email protected]6b5fe742011-05-20 21:46:481067 virtual bool GetStringList(StringListSetting key,
[email protected]c4c1b482011-07-22 17:24:261068 std::vector<std::string>* result) OVERRIDE {
[email protected]d7395e732009-08-28 23:13:431069 strings_map_type::iterator it = strings_table_.find(key);
1070 if (it == strings_table_.end())
1071 return false;
1072 *result = it->second;
1073 return true;
1074 }
1075
[email protected]c4c1b482011-07-22 17:24:261076 virtual bool BypassListIsReversed() OVERRIDE {
[email protected]a48bf4a2010-06-14 18:24:531077 return reversed_bypass_list_;
1078 }
1079
[email protected]c4c1b482011-07-22 17:24:261080 virtual bool MatchHostsUsingSuffixMatching() OVERRIDE {
[email protected]1a597192010-07-09 16:58:381081 return true;
1082 }
1083
[email protected]d7395e732009-08-28 23:13:431084 private:
1085 void ResetCachedSettings() {
1086 string_table_.clear();
1087 strings_table_.clear();
1088 indirect_manual_ = false;
1089 auto_no_pac_ = false;
[email protected]a48bf4a2010-06-14 18:24:531090 reversed_bypass_list_ = false;
[email protected]d7395e732009-08-28 23:13:431091 }
1092
[email protected]92d2dc82010-04-08 17:49:591093 FilePath KDEHomeToConfigPath(const FilePath& kde_home) {
1094 return kde_home.Append("share").Append("config");
1095 }
1096
[email protected]6b5fe742011-05-20 21:46:481097 void AddProxy(StringSetting host_key, const std::string& value) {
[email protected]d7395e732009-08-28 23:13:431098 if (value.empty() || value.substr(0, 3) == "//:")
1099 // No proxy.
1100 return;
[email protected]4b90c202012-04-24 23:27:551101 size_t space = value.find(' ');
1102 if (space != std::string::npos) {
1103 // Newer versions of KDE use a space rather than a colon to separate the
1104 // port number from the hostname. If we find this, we need to convert it.
1105 std::string fixed = value;
1106 fixed[space] = ':';
1107 string_table_[host_key] = fixed;
1108 } else {
1109 // We don't need to parse the port number out; GetProxyFromSettings()
1110 // would only append it right back again. So we just leave the port
1111 // number right in the host string.
1112 string_table_[host_key] = value;
1113 }
[email protected]d7395e732009-08-28 23:13:431114 }
1115
[email protected]6b5fe742011-05-20 21:46:481116 void AddHostList(StringListSetting key, const std::string& value) {
[email protected]f18fde22010-05-18 23:49:541117 std::vector<std::string> tokens;
[email protected]1a597192010-07-09 16:58:381118 StringTokenizer tk(value, ", ");
[email protected]f18fde22010-05-18 23:49:541119 while (tk.GetNext()) {
1120 std::string token = tk.token();
1121 if (!token.empty())
1122 tokens.push_back(token);
1123 }
1124 strings_table_[key] = tokens;
1125 }
1126
[email protected]9a3d8d42009-09-03 17:01:461127 void AddKDESetting(const std::string& key, const std::string& value) {
[email protected]d7395e732009-08-28 23:13:431128 if (key == "ProxyType") {
1129 const char* mode = "none";
1130 indirect_manual_ = false;
1131 auto_no_pac_ = false;
[email protected]e83326f2010-07-31 17:29:251132 int int_value;
1133 base::StringToInt(value, &int_value);
1134 switch (int_value) {
[email protected]d7395e732009-08-28 23:13:431135 case 0: // No proxy, or maybe kioslaverc syntax error.
1136 break;
1137 case 1: // Manual configuration.
1138 mode = "manual";
1139 break;
1140 case 2: // PAC URL.
1141 mode = "auto";
1142 break;
1143 case 3: // WPAD.
1144 mode = "auto";
1145 auto_no_pac_ = true;
1146 break;
1147 case 4: // Indirect manual via environment variables.
1148 mode = "manual";
1149 indirect_manual_ = true;
1150 break;
1151 }
[email protected]573c0502011-05-17 22:19:501152 string_table_[PROXY_MODE] = mode;
[email protected]d7395e732009-08-28 23:13:431153 } else if (key == "Proxy Config Script") {
[email protected]573c0502011-05-17 22:19:501154 string_table_[PROXY_AUTOCONF_URL] = value;
[email protected]d7395e732009-08-28 23:13:431155 } else if (key == "httpProxy") {
[email protected]573c0502011-05-17 22:19:501156 AddProxy(PROXY_HTTP_HOST, value);
[email protected]d7395e732009-08-28 23:13:431157 } else if (key == "httpsProxy") {
[email protected]573c0502011-05-17 22:19:501158 AddProxy(PROXY_HTTPS_HOST, value);
[email protected]d7395e732009-08-28 23:13:431159 } else if (key == "ftpProxy") {
[email protected]573c0502011-05-17 22:19:501160 AddProxy(PROXY_FTP_HOST, value);
[email protected]bfeb7232012-06-08 00:58:371161 } else if (key == "socksProxy") {
1162 // Older versions of KDE configure SOCKS in a weird way involving
1163 // LD_PRELOAD and a library that intercepts network calls to SOCKSify
1164 // them. We don't support it. KDE 4.8 added a proper SOCKS setting.
1165 AddProxy(PROXY_SOCKS_HOST, value);
[email protected]d7395e732009-08-28 23:13:431166 } else if (key == "ReversedException") {
1167 // We count "true" or any nonzero number as true, otherwise false.
1168 // Note that if the value is not actually numeric StringToInt()
1169 // will return 0, which we count as false.
[email protected]e83326f2010-07-31 17:29:251170 int int_value;
1171 base::StringToInt(value, &int_value);
1172 reversed_bypass_list_ = (value == "true" || int_value);
[email protected]d7395e732009-08-28 23:13:431173 } else if (key == "NoProxyFor") {
[email protected]573c0502011-05-17 22:19:501174 AddHostList(PROXY_IGNORE_HOSTS, value);
[email protected]d7395e732009-08-28 23:13:431175 } else if (key == "AuthMode") {
1176 // Check for authentication, just so we can warn.
[email protected]e83326f2010-07-31 17:29:251177 int mode;
1178 base::StringToInt(value, &mode);
[email protected]d7395e732009-08-28 23:13:431179 if (mode) {
1180 // ProxyConfig does not support authentication parameters, but
1181 // Chrome will prompt for the password later. So we ignore this.
1182 LOG(WARNING) <<
1183 "Proxy authentication parameters ignored, see bug 16709";
1184 }
1185 }
1186 }
1187
[email protected]6b5fe742011-05-20 21:46:481188 void ResolveIndirect(StringSetting key) {
[email protected]d7395e732009-08-28 23:13:431189 string_map_type::iterator it = string_table_.find(key);
1190 if (it != string_table_.end()) {
[email protected]f18fde22010-05-18 23:49:541191 std::string value;
[email protected]3ba7e082010-08-07 02:57:591192 if (env_var_getter_->GetVar(it->second.c_str(), &value))
[email protected]d7395e732009-08-28 23:13:431193 it->second = value;
[email protected]8425adc02010-04-18 17:45:311194 else
1195 string_table_.erase(it);
[email protected]d7395e732009-08-28 23:13:431196 }
1197 }
1198
[email protected]6b5fe742011-05-20 21:46:481199 void ResolveIndirectList(StringListSetting key) {
[email protected]f18fde22010-05-18 23:49:541200 strings_map_type::iterator it = strings_table_.find(key);
1201 if (it != strings_table_.end()) {
1202 std::string value;
1203 if (!it->second.empty() &&
[email protected]3ba7e082010-08-07 02:57:591204 env_var_getter_->GetVar(it->second[0].c_str(), &value))
[email protected]f18fde22010-05-18 23:49:541205 AddHostList(key, value);
1206 else
1207 strings_table_.erase(it);
1208 }
1209 }
1210
[email protected]d7395e732009-08-28 23:13:431211 // The settings in kioslaverc could occur in any order, but some affect
1212 // others. Rather than read the whole file in and then query them in an
1213 // order that allows us to handle that, we read the settings in whatever
1214 // order they occur and do any necessary tweaking after we finish.
1215 void ResolveModeEffects() {
1216 if (indirect_manual_) {
[email protected]573c0502011-05-17 22:19:501217 ResolveIndirect(PROXY_HTTP_HOST);
1218 ResolveIndirect(PROXY_HTTPS_HOST);
1219 ResolveIndirect(PROXY_FTP_HOST);
1220 ResolveIndirectList(PROXY_IGNORE_HOSTS);
[email protected]d7395e732009-08-28 23:13:431221 }
1222 if (auto_no_pac_) {
1223 // Remove the PAC URL; we're not supposed to use it.
[email protected]573c0502011-05-17 22:19:501224 string_table_.erase(PROXY_AUTOCONF_URL);
[email protected]d7395e732009-08-28 23:13:431225 }
[email protected]d7395e732009-08-28 23:13:431226 }
1227
1228 // Reads kioslaverc one line at a time and calls AddKDESetting() to add
1229 // each relevant name-value pair to the appropriate value table.
1230 void UpdateCachedSettings() {
[email protected]92d2dc82010-04-08 17:49:591231 FilePath kioslaverc = kde_config_dir_.Append("kioslaverc");
[email protected]d7395e732009-08-28 23:13:431232 file_util::ScopedFILE input(file_util::OpenFile(kioslaverc, "r"));
1233 if (!input.get())
1234 return;
1235 ResetCachedSettings();
1236 bool in_proxy_settings = false;
1237 bool line_too_long = false;
[email protected]9a3d8d42009-09-03 17:01:461238 char line[BUFFER_SIZE];
1239 // fgets() will return NULL on EOF or error.
[email protected]d7395e732009-08-28 23:13:431240 while (fgets(line, sizeof(line), input.get())) {
1241 // fgets() guarantees the line will be properly terminated.
1242 size_t length = strlen(line);
1243 if (!length)
1244 continue;
1245 // This should be true even with CRLF endings.
1246 if (line[length - 1] != '\n') {
1247 line_too_long = true;
1248 continue;
1249 }
1250 if (line_too_long) {
1251 // The previous line had no line ending, but this done does. This is
1252 // the end of the line that was too long, so warn here and skip it.
1253 LOG(WARNING) << "skipped very long line in " << kioslaverc.value();
1254 line_too_long = false;
1255 continue;
1256 }
1257 // Remove the LF at the end, and the CR if there is one.
1258 line[--length] = '\0';
1259 if (length && line[length - 1] == '\r')
1260 line[--length] = '\0';
1261 // Now parse the line.
1262 if (line[0] == '[') {
1263 // Switching sections. All we care about is whether this is
1264 // the (a?) proxy settings section, for both KDE3 and KDE4.
1265 in_proxy_settings = !strncmp(line, "[Proxy Settings]", 16);
1266 } else if (in_proxy_settings) {
1267 // A regular line, in the (a?) proxy settings section.
[email protected]9a3d8d42009-09-03 17:01:461268 char* split = strchr(line, '=');
1269 // Skip this line if it does not contain an = sign.
1270 if (!split)
[email protected]d7395e732009-08-28 23:13:431271 continue;
[email protected]9a3d8d42009-09-03 17:01:461272 // Split the line on the = and advance |split|.
1273 *(split++) = 0;
1274 std::string key = line;
1275 std::string value = split;
1276 TrimWhitespaceASCII(key, TRIM_ALL, &key);
1277 TrimWhitespaceASCII(value, TRIM_ALL, &value);
1278 // Skip this line if the key name is empty.
1279 if (key.empty())
[email protected]d7395e732009-08-28 23:13:431280 continue;
1281 // Is the value name localized?
[email protected]9a3d8d42009-09-03 17:01:461282 if (key[key.length() - 1] == ']') {
1283 // Find the matching bracket.
1284 length = key.rfind('[');
1285 // Skip this line if the localization indicator is malformed.
1286 if (length == std::string::npos)
[email protected]d7395e732009-08-28 23:13:431287 continue;
1288 // Trim the localization indicator off.
[email protected]9a3d8d42009-09-03 17:01:461289 key.resize(length);
1290 // Remove any resulting trailing whitespace.
1291 TrimWhitespaceASCII(key, TRIM_TRAILING, &key);
1292 // Skip this line if the key name is now empty.
1293 if (key.empty())
1294 continue;
[email protected]d7395e732009-08-28 23:13:431295 }
[email protected]d7395e732009-08-28 23:13:431296 // Now fill in the tables.
[email protected]9a3d8d42009-09-03 17:01:461297 AddKDESetting(key, value);
[email protected]d7395e732009-08-28 23:13:431298 }
1299 }
1300 if (ferror(input.get()))
1301 LOG(ERROR) << "error reading " << kioslaverc.value();
1302 ResolveModeEffects();
1303 }
1304
1305 // This is the callback from the debounce timer.
1306 void OnDebouncedNotification() {
1307 DCHECK(MessageLoop::current() == file_loop_);
[email protected]b30a3f52010-10-16 01:05:461308 VLOG(1) << "inotify change notification for kioslaverc";
[email protected]d7395e732009-08-28 23:13:431309 UpdateCachedSettings();
[email protected]961ac942011-04-28 18:18:141310 CHECK(notify_delegate_);
[email protected]d7395e732009-08-28 23:13:431311 // Forward to a method on the proxy config service delegate object.
1312 notify_delegate_->OnCheckProxyConfigSettings();
1313 }
1314
1315 // Called by OnFileCanReadWithoutBlocking() on the file thread. Reads
1316 // from the inotify file descriptor and starts up a debounce timer if
1317 // an event for kioslaverc is seen.
1318 void OnChangeNotification() {
[email protected]d2e6d592012-02-03 21:49:041319 DCHECK_GE(inotify_fd_, 0);
[email protected]d7395e732009-08-28 23:13:431320 DCHECK(MessageLoop::current() == file_loop_);
1321 char event_buf[(sizeof(inotify_event) + NAME_MAX + 1) * 4];
1322 bool kioslaverc_touched = false;
1323 ssize_t r;
1324 while ((r = read(inotify_fd_, event_buf, sizeof(event_buf))) > 0) {
1325 // inotify returns variable-length structures, which is why we have
1326 // this strange-looking loop instead of iterating through an array.
1327 char* event_ptr = event_buf;
1328 while (event_ptr < event_buf + r) {
1329 inotify_event* event = reinterpret_cast<inotify_event*>(event_ptr);
1330 // The kernel always feeds us whole events.
[email protected]b1f031dd2010-03-02 23:19:331331 CHECK_LE(event_ptr + sizeof(inotify_event), event_buf + r);
1332 CHECK_LE(event->name + event->len, event_buf + r);
[email protected]d7395e732009-08-28 23:13:431333 if (!strcmp(event->name, "kioslaverc"))
1334 kioslaverc_touched = true;
1335 // Advance the pointer just past the end of the filename.
1336 event_ptr = event->name + event->len;
1337 }
1338 // We keep reading even if |kioslaverc_touched| is true to drain the
1339 // inotify event queue.
1340 }
1341 if (!r)
1342 // Instead of returning -1 and setting errno to EINVAL if there is not
1343 // enough buffer space, older kernels (< 2.6.21) return 0. Simulate the
1344 // new behavior (EINVAL) so we can reuse the code below.
1345 errno = EINVAL;
1346 if (errno != EAGAIN) {
[email protected]57b765672009-10-13 18:27:401347 PLOG(WARNING) << "error reading inotify file descriptor";
[email protected]d7395e732009-08-28 23:13:431348 if (errno == EINVAL) {
1349 // Our buffer is not large enough to read the next event. This should
1350 // not happen (because its size is calculated to always be sufficiently
1351 // large), but if it does we'd warn continuously since |inotify_fd_|
1352 // would be forever ready to read. Close it and stop watching instead.
1353 LOG(ERROR) << "inotify failure; no longer watching kioslaverc!";
1354 inotify_watcher_.StopWatchingFileDescriptor();
1355 close(inotify_fd_);
1356 inotify_fd_ = -1;
1357 }
1358 }
1359 if (kioslaverc_touched) {
1360 // We don't use Reset() because the timer may not yet be running.
1361 // (In that case Stop() is a no-op.)
1362 debounce_timer_.Stop();
[email protected]d323a172011-09-02 18:23:021363 debounce_timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(
[email protected]d7395e732009-08-28 23:13:431364 kDebounceTimeoutMilliseconds), this,
[email protected]573c0502011-05-17 22:19:501365 &SettingGetterImplKDE::OnDebouncedNotification);
[email protected]d7395e732009-08-28 23:13:431366 }
1367 }
1368
[email protected]6b5fe742011-05-20 21:46:481369 typedef std::map<StringSetting, std::string> string_map_type;
1370 typedef std::map<StringListSetting,
1371 std::vector<std::string> > strings_map_type;
[email protected]d7395e732009-08-28 23:13:431372
1373 int inotify_fd_;
1374 base::MessagePumpLibevent::FileDescriptorWatcher inotify_watcher_;
1375 ProxyConfigServiceLinux::Delegate* notify_delegate_;
[email protected]573c0502011-05-17 22:19:501376 base::OneShotTimer<SettingGetterImplKDE> debounce_timer_;
[email protected]d7395e732009-08-28 23:13:431377 FilePath kde_config_dir_;
1378 bool indirect_manual_;
1379 bool auto_no_pac_;
[email protected]a48bf4a2010-06-14 18:24:531380 bool reversed_bypass_list_;
[email protected]f18fde22010-05-18 23:49:541381 // We don't own |env_var_getter_|. It's safe to hold a pointer to it, since
1382 // both it and us are owned by ProxyConfigServiceLinux::Delegate, and have the
1383 // same lifetime.
[email protected]76b90d312010-08-03 03:00:501384 base::Environment* env_var_getter_;
[email protected]d7395e732009-08-28 23:13:431385
1386 // We cache these settings whenever we re-read the kioslaverc file.
1387 string_map_type string_table_;
1388 strings_map_type strings_table_;
1389
1390 // Message loop of the file thread, for reading kioslaverc. If NULL,
1391 // just read it directly (for testing). We also handle inotify events
1392 // on this thread.
1393 MessageLoopForIO* file_loop_;
1394
[email protected]573c0502011-05-17 22:19:501395 DISALLOW_COPY_AND_ASSIGN(SettingGetterImplKDE);
[email protected]861c6c62009-04-20 16:50:561396};
1397
1398} // namespace
1399
[email protected]573c0502011-05-17 22:19:501400bool ProxyConfigServiceLinux::Delegate::GetProxyFromSettings(
[email protected]6b5fe742011-05-20 21:46:481401 SettingGetter::StringSetting host_key,
[email protected]573c0502011-05-17 22:19:501402 ProxyServer* result_server) {
[email protected]861c6c62009-04-20 16:50:561403 std::string host;
[email protected]573c0502011-05-17 22:19:501404 if (!setting_getter_->GetString(host_key, &host) || host.empty()) {
[email protected]861c6c62009-04-20 16:50:561405 // Unset or empty.
1406 return false;
1407 }
1408 // Check for an optional port.
[email protected]d7395e732009-08-28 23:13:431409 int port = 0;
[email protected]6b5fe742011-05-20 21:46:481410 SettingGetter::IntSetting port_key =
[email protected]573c0502011-05-17 22:19:501411 SettingGetter::HostSettingToPortSetting(host_key);
1412 setting_getter_->GetInt(port_key, &port);
[email protected]861c6c62009-04-20 16:50:561413 if (port != 0) {
1414 // If a port is set and non-zero:
[email protected]528c56d2010-07-30 19:28:441415 host += ":" + base::IntToString(port);
[email protected]861c6c62009-04-20 16:50:561416 }
[email protected]76960f3d2011-04-30 02:15:231417
[email protected]573c0502011-05-17 22:19:501418 // gconf settings do not appear to distinguish between SOCKS version. We
1419 // default to version 5. For more information on this policy decision, see:
[email protected]76960f3d2011-04-30 02:15:231420 // http://code.google.com/p/chromium/issues/detail?id=55912#c2
[email protected]573c0502011-05-17 22:19:501421 ProxyServer::Scheme scheme = (host_key == SettingGetter::PROXY_SOCKS_HOST) ?
1422 ProxyServer::SCHEME_SOCKS5 : ProxyServer::SCHEME_HTTP;
1423 host = FixupProxyHostScheme(scheme, host);
[email protected]87a102b2009-07-14 05:23:301424 ProxyServer proxy_server = ProxyServer::FromURI(host,
1425 ProxyServer::SCHEME_HTTP);
[email protected]861c6c62009-04-20 16:50:561426 if (proxy_server.is_valid()) {
1427 *result_server = proxy_server;
1428 return true;
1429 }
1430 return false;
1431}
1432
[email protected]573c0502011-05-17 22:19:501433bool ProxyConfigServiceLinux::Delegate::GetConfigFromSettings(
[email protected]3e44697f2009-05-22 14:37:391434 ProxyConfig* config) {
[email protected]861c6c62009-04-20 16:50:561435 std::string mode;
[email protected]573c0502011-05-17 22:19:501436 if (!setting_getter_->GetString(SettingGetter::PROXY_MODE, &mode)) {
[email protected]861c6c62009-04-20 16:50:561437 // We expect this to always be set, so if we don't see it then we
[email protected]573c0502011-05-17 22:19:501438 // probably have a gconf/gsettings problem, and so we don't have a valid
[email protected]861c6c62009-04-20 16:50:561439 // proxy config.
1440 return false;
1441 }
[email protected]3e44697f2009-05-22 14:37:391442 if (mode == "none") {
[email protected]861c6c62009-04-20 16:50:561443 // Specifically specifies no proxy.
1444 return true;
[email protected]3e44697f2009-05-22 14:37:391445 }
[email protected]861c6c62009-04-20 16:50:561446
[email protected]3e44697f2009-05-22 14:37:391447 if (mode == "auto") {
[email protected]861c6c62009-04-20 16:50:561448 // automatic proxy config
1449 std::string pac_url_str;
[email protected]573c0502011-05-17 22:19:501450 if (setting_getter_->GetString(SettingGetter::PROXY_AUTOCONF_URL,
1451 &pac_url_str)) {
[email protected]861c6c62009-04-20 16:50:561452 if (!pac_url_str.empty()) {
1453 GURL pac_url(pac_url_str);
1454 if (!pac_url.is_valid())
1455 return false;
[email protected]ed4ed0f2010-02-24 00:20:481456 config->set_pac_url(pac_url);
[email protected]861c6c62009-04-20 16:50:561457 return true;
1458 }
1459 }
[email protected]ed4ed0f2010-02-24 00:20:481460 config->set_auto_detect(true);
[email protected]861c6c62009-04-20 16:50:561461 return true;
1462 }
1463
[email protected]3e44697f2009-05-22 14:37:391464 if (mode != "manual") {
[email protected]861c6c62009-04-20 16:50:561465 // Mode is unrecognized.
1466 return false;
1467 }
1468 bool use_http_proxy;
[email protected]573c0502011-05-17 22:19:501469 if (setting_getter_->GetBool(SettingGetter::PROXY_USE_HTTP_PROXY,
1470 &use_http_proxy)
[email protected]861c6c62009-04-20 16:50:561471 && !use_http_proxy) {
1472 // Another master switch for some reason. If set to false, then no
1473 // proxy. But we don't panic if the key doesn't exist.
1474 return true;
1475 }
1476
1477 bool same_proxy = false;
1478 // Indicates to use the http proxy for all protocols. This one may
[email protected]573c0502011-05-17 22:19:501479 // not exist (presumably on older versions); we assume false in that
[email protected]861c6c62009-04-20 16:50:561480 // case.
[email protected]573c0502011-05-17 22:19:501481 setting_getter_->GetBool(SettingGetter::PROXY_USE_SAME_PROXY,
1482 &same_proxy);
[email protected]861c6c62009-04-20 16:50:561483
[email protected]76960f3d2011-04-30 02:15:231484 ProxyServer proxy_for_http;
1485 ProxyServer proxy_for_https;
1486 ProxyServer proxy_for_ftp;
1487 ProxyServer socks_proxy; // (socks)
1488
1489 // This counts how many of the above ProxyServers were defined and valid.
1490 size_t num_proxies_specified = 0;
1491
1492 // Extract the per-scheme proxies. If we failed to parse it, or no proxy was
1493 // specified for the scheme, then the resulting ProxyServer will be invalid.
[email protected]573c0502011-05-17 22:19:501494 if (GetProxyFromSettings(SettingGetter::PROXY_HTTP_HOST, &proxy_for_http))
[email protected]76960f3d2011-04-30 02:15:231495 num_proxies_specified++;
[email protected]573c0502011-05-17 22:19:501496 if (GetProxyFromSettings(SettingGetter::PROXY_HTTPS_HOST, &proxy_for_https))
[email protected]76960f3d2011-04-30 02:15:231497 num_proxies_specified++;
[email protected]573c0502011-05-17 22:19:501498 if (GetProxyFromSettings(SettingGetter::PROXY_FTP_HOST, &proxy_for_ftp))
[email protected]76960f3d2011-04-30 02:15:231499 num_proxies_specified++;
[email protected]573c0502011-05-17 22:19:501500 if (GetProxyFromSettings(SettingGetter::PROXY_SOCKS_HOST, &socks_proxy))
[email protected]76960f3d2011-04-30 02:15:231501 num_proxies_specified++;
1502
1503 if (same_proxy) {
1504 if (proxy_for_http.is_valid()) {
1505 // Use the http proxy for all schemes.
[email protected]ed4ed0f2010-02-24 00:20:481506 config->proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY;
[email protected]76960f3d2011-04-30 02:15:231507 config->proxy_rules().single_proxy = proxy_for_http;
[email protected]861c6c62009-04-20 16:50:561508 }
[email protected]76960f3d2011-04-30 02:15:231509 } else if (num_proxies_specified > 0) {
1510 if (socks_proxy.is_valid() && num_proxies_specified == 1) {
1511 // If the only proxy specified was for SOCKS, use it for all schemes.
1512 config->proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY;
1513 config->proxy_rules().single_proxy = socks_proxy;
[email protected]861c6c62009-04-20 16:50:561514 } else {
[email protected]76960f3d2011-04-30 02:15:231515 // Otherwise use the indicate proxies per-scheme.
1516 config->proxy_rules().type =
1517 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME;
1518 config->proxy_rules().proxy_for_http = proxy_for_http;
1519 config->proxy_rules().proxy_for_https = proxy_for_https;
1520 config->proxy_rules().proxy_for_ftp = proxy_for_ftp;
1521 config->proxy_rules().fallback_proxy = socks_proxy;
[email protected]861c6c62009-04-20 16:50:561522 }
1523 }
1524
[email protected]ed4ed0f2010-02-24 00:20:481525 if (config->proxy_rules().empty()) {
[email protected]861c6c62009-04-20 16:50:561526 // Manual mode but we couldn't parse any rules.
1527 return false;
1528 }
1529
1530 // Check for authentication, just so we can warn.
[email protected]d7395e732009-08-28 23:13:431531 bool use_auth = false;
[email protected]573c0502011-05-17 22:19:501532 setting_getter_->GetBool(SettingGetter::PROXY_USE_AUTHENTICATION,
1533 &use_auth);
[email protected]62749f182009-07-15 13:16:541534 if (use_auth) {
1535 // ProxyConfig does not support authentication parameters, but
1536 // Chrome will prompt for the password later. So we ignore
1537 // /system/http_proxy/*auth* settings.
1538 LOG(WARNING) << "Proxy authentication parameters ignored, see bug 16709";
1539 }
[email protected]861c6c62009-04-20 16:50:561540
1541 // Now the bypass list.
[email protected]7541206c2010-02-19 20:24:061542 std::vector<std::string> ignore_hosts_list;
[email protected]ed4ed0f2010-02-24 00:20:481543 config->proxy_rules().bypass_rules.Clear();
[email protected]573c0502011-05-17 22:19:501544 if (setting_getter_->GetStringList(SettingGetter::PROXY_IGNORE_HOSTS,
1545 &ignore_hosts_list)) {
[email protected]a8185d02010-06-11 00:19:501546 std::vector<std::string>::const_iterator it(ignore_hosts_list.begin());
[email protected]1a597192010-07-09 16:58:381547 for (; it != ignore_hosts_list.end(); ++it) {
[email protected]573c0502011-05-17 22:19:501548 if (setting_getter_->MatchHostsUsingSuffixMatching()) {
[email protected]1a597192010-07-09 16:58:381549 config->proxy_rules().bypass_rules.
1550 AddRuleFromStringUsingSuffixMatching(*it);
1551 } else {
1552 config->proxy_rules().bypass_rules.AddRuleFromString(*it);
1553 }
1554 }
[email protected]a8185d02010-06-11 00:19:501555 }
[email protected]861c6c62009-04-20 16:50:561556 // Note that there are no settings with semantics corresponding to
[email protected]1a597192010-07-09 16:58:381557 // bypass of local names in GNOME. In KDE, "<local>" is supported
1558 // as a hostname rule.
[email protected]861c6c62009-04-20 16:50:561559
[email protected]a48bf4a2010-06-14 18:24:531560 // KDE allows one to reverse the bypass rules.
[email protected]573c0502011-05-17 22:19:501561 config->proxy_rules().reverse_bypass =
1562 setting_getter_->BypassListIsReversed();
[email protected]a48bf4a2010-06-14 18:24:531563
[email protected]861c6c62009-04-20 16:50:561564 return true;
1565}
1566
[email protected]76b90d312010-08-03 03:00:501567ProxyConfigServiceLinux::Delegate::Delegate(base::Environment* env_var_getter)
[email protected]76722472012-05-24 08:26:461568 : env_var_getter_(env_var_getter) {
[email protected]573c0502011-05-17 22:19:501569 // Figure out which SettingGetterImpl to use, if any.
[email protected]6b0349ef2010-10-16 04:56:061570 switch (base::nix::GetDesktopEnvironment(env_var_getter)) {
1571 case base::nix::DESKTOP_ENVIRONMENT_GNOME:
[email protected]8c20e3d2011-05-19 21:03:571572#if defined(USE_GIO)
1573 {
1574 scoped_ptr<SettingGetterImplGSettings> gs_getter(
1575 new SettingGetterImplGSettings());
1576 // We have to load symbols and check the GNOME version in use to decide
1577 // if we should use the gsettings getter. See LoadAndCheckVersion().
1578 if (gs_getter->LoadAndCheckVersion(env_var_getter))
1579 setting_getter_.reset(gs_getter.release());
1580 }
1581#endif
[email protected]6de53d42010-11-09 07:33:191582#if defined(USE_GCONF)
[email protected]8c20e3d2011-05-19 21:03:571583 // Fall back on gconf if gsettings is unavailable or incorrect.
1584 if (!setting_getter_.get())
1585 setting_getter_.reset(new SettingGetterImplGConf());
[email protected]6de53d42010-11-09 07:33:191586#endif
[email protected]d7395e732009-08-28 23:13:431587 break;
[email protected]6b0349ef2010-10-16 04:56:061588 case base::nix::DESKTOP_ENVIRONMENT_KDE3:
1589 case base::nix::DESKTOP_ENVIRONMENT_KDE4:
[email protected]573c0502011-05-17 22:19:501590 setting_getter_.reset(new SettingGetterImplKDE(env_var_getter));
[email protected]d7395e732009-08-28 23:13:431591 break;
[email protected]6b0349ef2010-10-16 04:56:061592 case base::nix::DESKTOP_ENVIRONMENT_XFCE:
1593 case base::nix::DESKTOP_ENVIRONMENT_OTHER:
[email protected]d7395e732009-08-28 23:13:431594 break;
1595 }
1596}
1597
[email protected]573c0502011-05-17 22:19:501598ProxyConfigServiceLinux::Delegate::Delegate(
1599 base::Environment* env_var_getter, SettingGetter* setting_getter)
[email protected]76722472012-05-24 08:26:461600 : env_var_getter_(env_var_getter), setting_getter_(setting_getter) {
[email protected]861c6c62009-04-20 16:50:561601}
1602
[email protected]d3066142011-05-10 02:36:201603void ProxyConfigServiceLinux::Delegate::SetUpAndFetchInitialConfig(
[email protected]76722472012-05-24 08:26:461604 base::SingleThreadTaskRunner* glib_thread_task_runner,
1605 base::SingleThreadTaskRunner* io_thread_task_runner,
[email protected]d7395e732009-08-28 23:13:431606 MessageLoopForIO* file_loop) {
[email protected]3e44697f2009-05-22 14:37:391607 // We should be running on the default glib main loop thread right
1608 // now. gconf can only be accessed from this thread.
[email protected]76722472012-05-24 08:26:461609 DCHECK(glib_thread_task_runner->BelongsToCurrentThread());
1610 glib_thread_task_runner_ = glib_thread_task_runner;
1611 io_thread_task_runner_ = io_thread_task_runner;
[email protected]3e44697f2009-05-22 14:37:391612
[email protected]76722472012-05-24 08:26:461613 // If we are passed a NULL |io_thread_task_runner| or |file_loop|,
1614 // then we don't set up proxy setting change notifications. This
1615 // should not be the usual case but is intended to simplify test
1616 // setups.
1617 if (!io_thread_task_runner_ || !file_loop)
[email protected]b30a3f52010-10-16 01:05:461618 VLOG(1) << "Monitoring of proxy setting changes is disabled";
[email protected]3e44697f2009-05-22 14:37:391619
1620 // Fetch and cache the current proxy config. The config is left in
[email protected]119655002010-07-23 06:02:401621 // cached_config_, where GetLatestProxyConfig() running on the IO thread
[email protected]3e44697f2009-05-22 14:37:391622 // will expect to find it. This is safe to do because we return
1623 // before this ProxyConfigServiceLinux is passed on to
1624 // the ProxyService.
[email protected]d6cb85b2009-07-23 22:10:531625
1626 // Note: It would be nice to prioritize environment variables
[email protected]92d2dc82010-04-08 17:49:591627 // and only fall back to gconf if env vars were unset. But
[email protected]d6cb85b2009-07-23 22:10:531628 // gnome-terminal "helpfully" sets http_proxy and no_proxy, and it
1629 // does so even if the proxy mode is set to auto, which would
1630 // mislead us.
1631
[email protected]3e44697f2009-05-22 14:37:391632 bool got_config = false;
[email protected]573c0502011-05-17 22:19:501633 if (setting_getter_.get() &&
[email protected]76722472012-05-24 08:26:461634 setting_getter_->Init(glib_thread_task_runner, file_loop) &&
[email protected]573c0502011-05-17 22:19:501635 GetConfigFromSettings(&cached_config_)) {
[email protected]d3066142011-05-10 02:36:201636 cached_config_.set_id(1); // Mark it as valid.
[email protected]db8ff912012-06-12 23:32:511637 cached_config_.set_source(setting_getter_->GetConfigSource());
[email protected]d3066142011-05-10 02:36:201638 VLOG(1) << "Obtained proxy settings from "
[email protected]db8ff912012-06-12 23:32:511639 << ProxyConfigSourceToString(cached_config_.source());
[email protected]d3066142011-05-10 02:36:201640
1641 // If gconf proxy mode is "none", meaning direct, then we take
1642 // that to be a valid config and will not check environment
1643 // variables. The alternative would have been to look for a proxy
1644 // whereever we can find one.
1645 got_config = true;
1646
1647 // Keep a copy of the config for use from this thread for
1648 // comparison with updated settings when we get notifications.
1649 reference_config_ = cached_config_;
1650 reference_config_.set_id(1); // Mark it as valid.
1651
1652 // We only set up notifications if we have IO and file loops available.
1653 // We do this after getting the initial configuration so that we don't have
1654 // to worry about cancelling it if the initial fetch above fails. Note that
1655 // setting up notifications has the side effect of simulating a change, so
1656 // that we won't lose any updates that may have happened after the initial
1657 // fetch and before setting up notifications. We'll detect the common case
1658 // of no changes in OnCheckProxyConfigSettings() (or sooner) and ignore it.
[email protected]76722472012-05-24 08:26:461659 if (io_thread_task_runner && file_loop) {
1660 scoped_refptr<base::SingleThreadTaskRunner> required_loop =
1661 setting_getter_->GetNotificationTaskRunner();
[email protected]434fff82012-04-24 21:13:281662 if (!required_loop || required_loop->BelongsToCurrentThread()) {
[email protected]d3066142011-05-10 02:36:201663 // In this case we are already on an acceptable thread.
1664 SetUpNotifications();
[email protected]d7395e732009-08-28 23:13:431665 } else {
[email protected]d3066142011-05-10 02:36:201666 // Post a task to set up notifications. We don't wait for success.
[email protected]6af889c2011-10-06 23:11:411667 required_loop->PostTask(FROM_HERE, base::Bind(
1668 &ProxyConfigServiceLinux::Delegate::SetUpNotifications, this));
[email protected]d6cb85b2009-07-23 22:10:531669 }
[email protected]d7395e732009-08-28 23:13:431670 }
[email protected]861c6c62009-04-20 16:50:561671 }
[email protected]d6cb85b2009-07-23 22:10:531672
[email protected]3e44697f2009-05-22 14:37:391673 if (!got_config) {
[email protected]d6cb85b2009-07-23 22:10:531674 // We fall back on environment variables.
[email protected]3e44697f2009-05-22 14:37:391675 //
[email protected]d3066142011-05-10 02:36:201676 // Consulting environment variables doesn't need to be done from the
1677 // default glib main loop, but it's a tiny enough amount of work.
[email protected]3e44697f2009-05-22 14:37:391678 if (GetConfigFromEnv(&cached_config_)) {
[email protected]db8ff912012-06-12 23:32:511679 cached_config_.set_source(PROXY_CONFIG_SOURCE_ENV);
[email protected]d3066142011-05-10 02:36:201680 cached_config_.set_id(1); // Mark it as valid.
[email protected]b30a3f52010-10-16 01:05:461681 VLOG(1) << "Obtained proxy settings from environment variables";
[email protected]3e44697f2009-05-22 14:37:391682 }
[email protected]861c6c62009-04-20 16:50:561683 }
[email protected]3e44697f2009-05-22 14:37:391684}
1685
[email protected]573c0502011-05-17 22:19:501686// Depending on the SettingGetter in use, this method will be called
[email protected]d3066142011-05-10 02:36:201687// on either the UI thread (GConf) or the file thread (KDE).
1688void ProxyConfigServiceLinux::Delegate::SetUpNotifications() {
[email protected]76722472012-05-24 08:26:461689 scoped_refptr<base::SingleThreadTaskRunner> required_loop =
1690 setting_getter_->GetNotificationTaskRunner();
[email protected]434fff82012-04-24 21:13:281691 DCHECK(!required_loop || required_loop->BelongsToCurrentThread());
[email protected]573c0502011-05-17 22:19:501692 if (!setting_getter_->SetUpNotifications(this))
[email protected]d3066142011-05-10 02:36:201693 LOG(ERROR) << "Unable to set up proxy configuration change notifications";
1694}
1695
[email protected]119655002010-07-23 06:02:401696void ProxyConfigServiceLinux::Delegate::AddObserver(Observer* observer) {
1697 observers_.AddObserver(observer);
1698}
1699
1700void ProxyConfigServiceLinux::Delegate::RemoveObserver(Observer* observer) {
1701 observers_.RemoveObserver(observer);
1702}
1703
[email protected]3a29593d2011-04-11 10:07:521704ProxyConfigService::ConfigAvailability
1705 ProxyConfigServiceLinux::Delegate::GetLatestProxyConfig(
1706 ProxyConfig* config) {
[email protected]3e44697f2009-05-22 14:37:391707 // This is called from the IO thread.
[email protected]76722472012-05-24 08:26:461708 DCHECK(!io_thread_task_runner_ ||
1709 io_thread_task_runner_->BelongsToCurrentThread());
[email protected]3e44697f2009-05-22 14:37:391710
1711 // Simply return the last proxy configuration that glib_default_loop
1712 // notified us of.
[email protected]db8ff912012-06-12 23:32:511713 if (cached_config_.is_valid()) {
1714 *config = cached_config_;
1715 } else {
1716 *config = ProxyConfig::CreateDirect();
1717 config->set_source(PROXY_CONFIG_SOURCE_SYSTEM_FAILED);
1718 }
[email protected]119655002010-07-23 06:02:401719
[email protected]3a29593d2011-04-11 10:07:521720 // We return CONFIG_VALID to indicate that *config was filled in. It is always
[email protected]119655002010-07-23 06:02:401721 // going to be available since we initialized eagerly on the UI thread.
1722 // TODO(eroman): do lazy initialization instead, so we no longer need
1723 // to construct ProxyConfigServiceLinux on the UI thread.
1724 // In which case, we may return false here.
[email protected]3a29593d2011-04-11 10:07:521725 return CONFIG_VALID;
[email protected]3e44697f2009-05-22 14:37:391726}
1727
[email protected]573c0502011-05-17 22:19:501728// Depending on the SettingGetter in use, this method will be called
[email protected]d7395e732009-08-28 23:13:431729// on either the UI thread (GConf) or the file thread (KDE).
[email protected]3e44697f2009-05-22 14:37:391730void ProxyConfigServiceLinux::Delegate::OnCheckProxyConfigSettings() {
[email protected]76722472012-05-24 08:26:461731 scoped_refptr<base::SingleThreadTaskRunner> required_loop =
1732 setting_getter_->GetNotificationTaskRunner();
[email protected]434fff82012-04-24 21:13:281733 DCHECK(!required_loop || required_loop->BelongsToCurrentThread());
[email protected]3e44697f2009-05-22 14:37:391734 ProxyConfig new_config;
[email protected]573c0502011-05-17 22:19:501735 bool valid = GetConfigFromSettings(&new_config);
[email protected]3e44697f2009-05-22 14:37:391736 if (valid)
1737 new_config.set_id(1); // mark it as valid
1738
[email protected]119655002010-07-23 06:02:401739 // See if it is different from what we had before.
[email protected]3e44697f2009-05-22 14:37:391740 if (new_config.is_valid() != reference_config_.is_valid() ||
1741 !new_config.Equals(reference_config_)) {
[email protected]76722472012-05-24 08:26:461742 // Post a task to the IO thread with the new configuration, so it can
[email protected]3e44697f2009-05-22 14:37:391743 // update |cached_config_|.
[email protected]76722472012-05-24 08:26:461744 io_thread_task_runner_->PostTask(FROM_HERE, base::Bind(
[email protected]6af889c2011-10-06 23:11:411745 &ProxyConfigServiceLinux::Delegate::SetNewProxyConfig,
1746 this, new_config));
[email protected]d1f9d472009-08-13 19:59:301747 // Update the thread-private copy in |reference_config_| as well.
1748 reference_config_ = new_config;
[email protected]d3066142011-05-10 02:36:201749 } else {
1750 VLOG(1) << "Detected no-op change to proxy settings. Doing nothing.";
[email protected]3e44697f2009-05-22 14:37:391751 }
1752}
1753
1754void ProxyConfigServiceLinux::Delegate::SetNewProxyConfig(
1755 const ProxyConfig& new_config) {
[email protected]76722472012-05-24 08:26:461756 DCHECK(io_thread_task_runner_->BelongsToCurrentThread());
[email protected]b30a3f52010-10-16 01:05:461757 VLOG(1) << "Proxy configuration changed";
[email protected]3e44697f2009-05-22 14:37:391758 cached_config_ = new_config;
[email protected]3a29593d2011-04-11 10:07:521759 FOR_EACH_OBSERVER(
1760 Observer, observers_,
1761 OnProxyConfigChanged(new_config, ProxyConfigService::CONFIG_VALID));
[email protected]3e44697f2009-05-22 14:37:391762}
1763
1764void ProxyConfigServiceLinux::Delegate::PostDestroyTask() {
[email protected]573c0502011-05-17 22:19:501765 if (!setting_getter_.get())
[email protected]d7395e732009-08-28 23:13:431766 return;
[email protected]76722472012-05-24 08:26:461767 scoped_refptr<base::SingleThreadTaskRunner> shutdown_loop =
1768 setting_getter_->GetNotificationTaskRunner();
[email protected]434fff82012-04-24 21:13:281769 if (!shutdown_loop || shutdown_loop->BelongsToCurrentThread()) {
[email protected]3e44697f2009-05-22 14:37:391770 // Already on the right thread, call directly.
1771 // This is the case for the unittests.
1772 OnDestroy();
1773 } else {
[email protected]d7395e732009-08-28 23:13:431774 // Post to shutdown thread. Note that on browser shutdown, we may quit
1775 // this MessageLoop and exit the program before ever running this.
[email protected]6af889c2011-10-06 23:11:411776 shutdown_loop->PostTask(FROM_HERE, base::Bind(
1777 &ProxyConfigServiceLinux::Delegate::OnDestroy, this));
[email protected]3e44697f2009-05-22 14:37:391778 }
1779}
1780void ProxyConfigServiceLinux::Delegate::OnDestroy() {
[email protected]76722472012-05-24 08:26:461781 scoped_refptr<base::SingleThreadTaskRunner> shutdown_loop =
1782 setting_getter_->GetNotificationTaskRunner();
[email protected]434fff82012-04-24 21:13:281783 DCHECK(!shutdown_loop || shutdown_loop->BelongsToCurrentThread());
[email protected]573c0502011-05-17 22:19:501784 setting_getter_->ShutDown();
[email protected]3e44697f2009-05-22 14:37:391785}
1786
1787ProxyConfigServiceLinux::ProxyConfigServiceLinux()
[email protected]76b90d312010-08-03 03:00:501788 : delegate_(new Delegate(base::Environment::Create())) {
[email protected]3e44697f2009-05-22 14:37:391789}
1790
[email protected]8e1845e12010-09-15 19:22:241791ProxyConfigServiceLinux::~ProxyConfigServiceLinux() {
1792 delegate_->PostDestroyTask();
1793}
1794
[email protected]3e44697f2009-05-22 14:37:391795ProxyConfigServiceLinux::ProxyConfigServiceLinux(
[email protected]76b90d312010-08-03 03:00:501796 base::Environment* env_var_getter)
[email protected]9a3d8d42009-09-03 17:01:461797 : delegate_(new Delegate(env_var_getter)) {
1798}
1799
1800ProxyConfigServiceLinux::ProxyConfigServiceLinux(
[email protected]573c0502011-05-17 22:19:501801 base::Environment* env_var_getter, SettingGetter* setting_getter)
1802 : delegate_(new Delegate(env_var_getter, setting_getter)) {
[email protected]861c6c62009-04-20 16:50:561803}
1804
[email protected]e4be2dd2010-12-14 00:44:391805void ProxyConfigServiceLinux::AddObserver(Observer* observer) {
1806 delegate_->AddObserver(observer);
1807}
1808
1809void ProxyConfigServiceLinux::RemoveObserver(Observer* observer) {
1810 delegate_->RemoveObserver(observer);
1811}
1812
[email protected]3a29593d2011-04-11 10:07:521813ProxyConfigService::ConfigAvailability
1814 ProxyConfigServiceLinux::GetLatestProxyConfig(ProxyConfig* config) {
[email protected]e4be2dd2010-12-14 00:44:391815 return delegate_->GetLatestProxyConfig(config);
1816}
1817
[email protected]861c6c62009-04-20 16:50:561818} // namespace net