blob: 5012d5fa586ef19c6b34a0fa76c253cc1222b447 [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]c4c1b482011-07-22 17:24:26323 virtual const char* GetDataSource() OVERRIDE {
[email protected]d7395e732009-08-28 23:13:43324 return "gconf";
325 }
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]c4c1b482011-07-22 17:24:26640 virtual const char* GetDataSource() OVERRIDE {
[email protected]8c20e3d2011-05-19 21:03:57641 return "gsettings";
642 }
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]c4c1b482011-07-22 17:24:261048 virtual const char* GetDataSource() OVERRIDE {
[email protected]d7395e732009-08-28 23:13:431049 return "KDE";
1050 }
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 // The astute reader may notice that there is no mention of SOCKS
1129 // here. That's because KDE handles socks is a strange way, and we
1130 // don't support it. Rather than just a setting for the SOCKS server,
1131 // it has a setting for a library to LD_PRELOAD in all your programs
1132 // that will transparently SOCKSify them. Such libraries each have
1133 // their own configuration, and thus, we can't get it from KDE.
1134 if (key == "ProxyType") {
1135 const char* mode = "none";
1136 indirect_manual_ = false;
1137 auto_no_pac_ = false;
[email protected]e83326f2010-07-31 17:29:251138 int int_value;
1139 base::StringToInt(value, &int_value);
1140 switch (int_value) {
[email protected]d7395e732009-08-28 23:13:431141 case 0: // No proxy, or maybe kioslaverc syntax error.
1142 break;
1143 case 1: // Manual configuration.
1144 mode = "manual";
1145 break;
1146 case 2: // PAC URL.
1147 mode = "auto";
1148 break;
1149 case 3: // WPAD.
1150 mode = "auto";
1151 auto_no_pac_ = true;
1152 break;
1153 case 4: // Indirect manual via environment variables.
1154 mode = "manual";
1155 indirect_manual_ = true;
1156 break;
1157 }
[email protected]573c0502011-05-17 22:19:501158 string_table_[PROXY_MODE] = mode;
[email protected]d7395e732009-08-28 23:13:431159 } else if (key == "Proxy Config Script") {
[email protected]573c0502011-05-17 22:19:501160 string_table_[PROXY_AUTOCONF_URL] = value;
[email protected]d7395e732009-08-28 23:13:431161 } else if (key == "httpProxy") {
[email protected]573c0502011-05-17 22:19:501162 AddProxy(PROXY_HTTP_HOST, value);
[email protected]d7395e732009-08-28 23:13:431163 } else if (key == "httpsProxy") {
[email protected]573c0502011-05-17 22:19:501164 AddProxy(PROXY_HTTPS_HOST, value);
[email protected]d7395e732009-08-28 23:13:431165 } else if (key == "ftpProxy") {
[email protected]573c0502011-05-17 22:19:501166 AddProxy(PROXY_FTP_HOST, value);
[email protected]d7395e732009-08-28 23:13:431167 } else if (key == "ReversedException") {
1168 // We count "true" or any nonzero number as true, otherwise false.
1169 // Note that if the value is not actually numeric StringToInt()
1170 // will return 0, which we count as false.
[email protected]e83326f2010-07-31 17:29:251171 int int_value;
1172 base::StringToInt(value, &int_value);
1173 reversed_bypass_list_ = (value == "true" || int_value);
[email protected]d7395e732009-08-28 23:13:431174 } else if (key == "NoProxyFor") {
[email protected]573c0502011-05-17 22:19:501175 AddHostList(PROXY_IGNORE_HOSTS, value);
[email protected]d7395e732009-08-28 23:13:431176 } else if (key == "AuthMode") {
1177 // Check for authentication, just so we can warn.
[email protected]e83326f2010-07-31 17:29:251178 int mode;
1179 base::StringToInt(value, &mode);
[email protected]d7395e732009-08-28 23:13:431180 if (mode) {
1181 // ProxyConfig does not support authentication parameters, but
1182 // Chrome will prompt for the password later. So we ignore this.
1183 LOG(WARNING) <<
1184 "Proxy authentication parameters ignored, see bug 16709";
1185 }
1186 }
1187 }
1188
[email protected]6b5fe742011-05-20 21:46:481189 void ResolveIndirect(StringSetting key) {
[email protected]d7395e732009-08-28 23:13:431190 string_map_type::iterator it = string_table_.find(key);
1191 if (it != string_table_.end()) {
[email protected]f18fde22010-05-18 23:49:541192 std::string value;
[email protected]3ba7e082010-08-07 02:57:591193 if (env_var_getter_->GetVar(it->second.c_str(), &value))
[email protected]d7395e732009-08-28 23:13:431194 it->second = value;
[email protected]8425adc02010-04-18 17:45:311195 else
1196 string_table_.erase(it);
[email protected]d7395e732009-08-28 23:13:431197 }
1198 }
1199
[email protected]6b5fe742011-05-20 21:46:481200 void ResolveIndirectList(StringListSetting key) {
[email protected]f18fde22010-05-18 23:49:541201 strings_map_type::iterator it = strings_table_.find(key);
1202 if (it != strings_table_.end()) {
1203 std::string value;
1204 if (!it->second.empty() &&
[email protected]3ba7e082010-08-07 02:57:591205 env_var_getter_->GetVar(it->second[0].c_str(), &value))
[email protected]f18fde22010-05-18 23:49:541206 AddHostList(key, value);
1207 else
1208 strings_table_.erase(it);
1209 }
1210 }
1211
[email protected]d7395e732009-08-28 23:13:431212 // The settings in kioslaverc could occur in any order, but some affect
1213 // others. Rather than read the whole file in and then query them in an
1214 // order that allows us to handle that, we read the settings in whatever
1215 // order they occur and do any necessary tweaking after we finish.
1216 void ResolveModeEffects() {
1217 if (indirect_manual_) {
[email protected]573c0502011-05-17 22:19:501218 ResolveIndirect(PROXY_HTTP_HOST);
1219 ResolveIndirect(PROXY_HTTPS_HOST);
1220 ResolveIndirect(PROXY_FTP_HOST);
1221 ResolveIndirectList(PROXY_IGNORE_HOSTS);
[email protected]d7395e732009-08-28 23:13:431222 }
1223 if (auto_no_pac_) {
1224 // Remove the PAC URL; we're not supposed to use it.
[email protected]573c0502011-05-17 22:19:501225 string_table_.erase(PROXY_AUTOCONF_URL);
[email protected]d7395e732009-08-28 23:13:431226 }
[email protected]d7395e732009-08-28 23:13:431227 }
1228
1229 // Reads kioslaverc one line at a time and calls AddKDESetting() to add
1230 // each relevant name-value pair to the appropriate value table.
1231 void UpdateCachedSettings() {
[email protected]92d2dc82010-04-08 17:49:591232 FilePath kioslaverc = kde_config_dir_.Append("kioslaverc");
[email protected]d7395e732009-08-28 23:13:431233 file_util::ScopedFILE input(file_util::OpenFile(kioslaverc, "r"));
1234 if (!input.get())
1235 return;
1236 ResetCachedSettings();
1237 bool in_proxy_settings = false;
1238 bool line_too_long = false;
[email protected]9a3d8d42009-09-03 17:01:461239 char line[BUFFER_SIZE];
1240 // fgets() will return NULL on EOF or error.
[email protected]d7395e732009-08-28 23:13:431241 while (fgets(line, sizeof(line), input.get())) {
1242 // fgets() guarantees the line will be properly terminated.
1243 size_t length = strlen(line);
1244 if (!length)
1245 continue;
1246 // This should be true even with CRLF endings.
1247 if (line[length - 1] != '\n') {
1248 line_too_long = true;
1249 continue;
1250 }
1251 if (line_too_long) {
1252 // The previous line had no line ending, but this done does. This is
1253 // the end of the line that was too long, so warn here and skip it.
1254 LOG(WARNING) << "skipped very long line in " << kioslaverc.value();
1255 line_too_long = false;
1256 continue;
1257 }
1258 // Remove the LF at the end, and the CR if there is one.
1259 line[--length] = '\0';
1260 if (length && line[length - 1] == '\r')
1261 line[--length] = '\0';
1262 // Now parse the line.
1263 if (line[0] == '[') {
1264 // Switching sections. All we care about is whether this is
1265 // the (a?) proxy settings section, for both KDE3 and KDE4.
1266 in_proxy_settings = !strncmp(line, "[Proxy Settings]", 16);
1267 } else if (in_proxy_settings) {
1268 // A regular line, in the (a?) proxy settings section.
[email protected]9a3d8d42009-09-03 17:01:461269 char* split = strchr(line, '=');
1270 // Skip this line if it does not contain an = sign.
1271 if (!split)
[email protected]d7395e732009-08-28 23:13:431272 continue;
[email protected]9a3d8d42009-09-03 17:01:461273 // Split the line on the = and advance |split|.
1274 *(split++) = 0;
1275 std::string key = line;
1276 std::string value = split;
1277 TrimWhitespaceASCII(key, TRIM_ALL, &key);
1278 TrimWhitespaceASCII(value, TRIM_ALL, &value);
1279 // Skip this line if the key name is empty.
1280 if (key.empty())
[email protected]d7395e732009-08-28 23:13:431281 continue;
1282 // Is the value name localized?
[email protected]9a3d8d42009-09-03 17:01:461283 if (key[key.length() - 1] == ']') {
1284 // Find the matching bracket.
1285 length = key.rfind('[');
1286 // Skip this line if the localization indicator is malformed.
1287 if (length == std::string::npos)
[email protected]d7395e732009-08-28 23:13:431288 continue;
1289 // Trim the localization indicator off.
[email protected]9a3d8d42009-09-03 17:01:461290 key.resize(length);
1291 // Remove any resulting trailing whitespace.
1292 TrimWhitespaceASCII(key, TRIM_TRAILING, &key);
1293 // Skip this line if the key name is now empty.
1294 if (key.empty())
1295 continue;
[email protected]d7395e732009-08-28 23:13:431296 }
[email protected]d7395e732009-08-28 23:13:431297 // Now fill in the tables.
[email protected]9a3d8d42009-09-03 17:01:461298 AddKDESetting(key, value);
[email protected]d7395e732009-08-28 23:13:431299 }
1300 }
1301 if (ferror(input.get()))
1302 LOG(ERROR) << "error reading " << kioslaverc.value();
1303 ResolveModeEffects();
1304 }
1305
1306 // This is the callback from the debounce timer.
1307 void OnDebouncedNotification() {
1308 DCHECK(MessageLoop::current() == file_loop_);
[email protected]b30a3f52010-10-16 01:05:461309 VLOG(1) << "inotify change notification for kioslaverc";
[email protected]d7395e732009-08-28 23:13:431310 UpdateCachedSettings();
[email protected]961ac942011-04-28 18:18:141311 CHECK(notify_delegate_);
[email protected]d7395e732009-08-28 23:13:431312 // Forward to a method on the proxy config service delegate object.
1313 notify_delegate_->OnCheckProxyConfigSettings();
1314 }
1315
1316 // Called by OnFileCanReadWithoutBlocking() on the file thread. Reads
1317 // from the inotify file descriptor and starts up a debounce timer if
1318 // an event for kioslaverc is seen.
1319 void OnChangeNotification() {
[email protected]d2e6d592012-02-03 21:49:041320 DCHECK_GE(inotify_fd_, 0);
[email protected]d7395e732009-08-28 23:13:431321 DCHECK(MessageLoop::current() == file_loop_);
1322 char event_buf[(sizeof(inotify_event) + NAME_MAX + 1) * 4];
1323 bool kioslaverc_touched = false;
1324 ssize_t r;
1325 while ((r = read(inotify_fd_, event_buf, sizeof(event_buf))) > 0) {
1326 // inotify returns variable-length structures, which is why we have
1327 // this strange-looking loop instead of iterating through an array.
1328 char* event_ptr = event_buf;
1329 while (event_ptr < event_buf + r) {
1330 inotify_event* event = reinterpret_cast<inotify_event*>(event_ptr);
1331 // The kernel always feeds us whole events.
[email protected]b1f031dd2010-03-02 23:19:331332 CHECK_LE(event_ptr + sizeof(inotify_event), event_buf + r);
1333 CHECK_LE(event->name + event->len, event_buf + r);
[email protected]d7395e732009-08-28 23:13:431334 if (!strcmp(event->name, "kioslaverc"))
1335 kioslaverc_touched = true;
1336 // Advance the pointer just past the end of the filename.
1337 event_ptr = event->name + event->len;
1338 }
1339 // We keep reading even if |kioslaverc_touched| is true to drain the
1340 // inotify event queue.
1341 }
1342 if (!r)
1343 // Instead of returning -1 and setting errno to EINVAL if there is not
1344 // enough buffer space, older kernels (< 2.6.21) return 0. Simulate the
1345 // new behavior (EINVAL) so we can reuse the code below.
1346 errno = EINVAL;
1347 if (errno != EAGAIN) {
[email protected]57b765672009-10-13 18:27:401348 PLOG(WARNING) << "error reading inotify file descriptor";
[email protected]d7395e732009-08-28 23:13:431349 if (errno == EINVAL) {
1350 // Our buffer is not large enough to read the next event. This should
1351 // not happen (because its size is calculated to always be sufficiently
1352 // large), but if it does we'd warn continuously since |inotify_fd_|
1353 // would be forever ready to read. Close it and stop watching instead.
1354 LOG(ERROR) << "inotify failure; no longer watching kioslaverc!";
1355 inotify_watcher_.StopWatchingFileDescriptor();
1356 close(inotify_fd_);
1357 inotify_fd_ = -1;
1358 }
1359 }
1360 if (kioslaverc_touched) {
1361 // We don't use Reset() because the timer may not yet be running.
1362 // (In that case Stop() is a no-op.)
1363 debounce_timer_.Stop();
[email protected]d323a172011-09-02 18:23:021364 debounce_timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(
[email protected]d7395e732009-08-28 23:13:431365 kDebounceTimeoutMilliseconds), this,
[email protected]573c0502011-05-17 22:19:501366 &SettingGetterImplKDE::OnDebouncedNotification);
[email protected]d7395e732009-08-28 23:13:431367 }
1368 }
1369
[email protected]6b5fe742011-05-20 21:46:481370 typedef std::map<StringSetting, std::string> string_map_type;
1371 typedef std::map<StringListSetting,
1372 std::vector<std::string> > strings_map_type;
[email protected]d7395e732009-08-28 23:13:431373
1374 int inotify_fd_;
1375 base::MessagePumpLibevent::FileDescriptorWatcher inotify_watcher_;
1376 ProxyConfigServiceLinux::Delegate* notify_delegate_;
[email protected]573c0502011-05-17 22:19:501377 base::OneShotTimer<SettingGetterImplKDE> debounce_timer_;
[email protected]d7395e732009-08-28 23:13:431378 FilePath kde_config_dir_;
1379 bool indirect_manual_;
1380 bool auto_no_pac_;
[email protected]a48bf4a2010-06-14 18:24:531381 bool reversed_bypass_list_;
[email protected]f18fde22010-05-18 23:49:541382 // We don't own |env_var_getter_|. It's safe to hold a pointer to it, since
1383 // both it and us are owned by ProxyConfigServiceLinux::Delegate, and have the
1384 // same lifetime.
[email protected]76b90d312010-08-03 03:00:501385 base::Environment* env_var_getter_;
[email protected]d7395e732009-08-28 23:13:431386
1387 // We cache these settings whenever we re-read the kioslaverc file.
1388 string_map_type string_table_;
1389 strings_map_type strings_table_;
1390
1391 // Message loop of the file thread, for reading kioslaverc. If NULL,
1392 // just read it directly (for testing). We also handle inotify events
1393 // on this thread.
1394 MessageLoopForIO* file_loop_;
1395
[email protected]573c0502011-05-17 22:19:501396 DISALLOW_COPY_AND_ASSIGN(SettingGetterImplKDE);
[email protected]861c6c62009-04-20 16:50:561397};
1398
1399} // namespace
1400
[email protected]573c0502011-05-17 22:19:501401bool ProxyConfigServiceLinux::Delegate::GetProxyFromSettings(
[email protected]6b5fe742011-05-20 21:46:481402 SettingGetter::StringSetting host_key,
[email protected]573c0502011-05-17 22:19:501403 ProxyServer* result_server) {
[email protected]861c6c62009-04-20 16:50:561404 std::string host;
[email protected]573c0502011-05-17 22:19:501405 if (!setting_getter_->GetString(host_key, &host) || host.empty()) {
[email protected]861c6c62009-04-20 16:50:561406 // Unset or empty.
1407 return false;
1408 }
1409 // Check for an optional port.
[email protected]d7395e732009-08-28 23:13:431410 int port = 0;
[email protected]6b5fe742011-05-20 21:46:481411 SettingGetter::IntSetting port_key =
[email protected]573c0502011-05-17 22:19:501412 SettingGetter::HostSettingToPortSetting(host_key);
1413 setting_getter_->GetInt(port_key, &port);
[email protected]861c6c62009-04-20 16:50:561414 if (port != 0) {
1415 // If a port is set and non-zero:
[email protected]528c56d2010-07-30 19:28:441416 host += ":" + base::IntToString(port);
[email protected]861c6c62009-04-20 16:50:561417 }
[email protected]76960f3d2011-04-30 02:15:231418
[email protected]573c0502011-05-17 22:19:501419 // gconf settings do not appear to distinguish between SOCKS version. We
1420 // default to version 5. For more information on this policy decision, see:
[email protected]76960f3d2011-04-30 02:15:231421 // http://code.google.com/p/chromium/issues/detail?id=55912#c2
[email protected]573c0502011-05-17 22:19:501422 ProxyServer::Scheme scheme = (host_key == SettingGetter::PROXY_SOCKS_HOST) ?
1423 ProxyServer::SCHEME_SOCKS5 : ProxyServer::SCHEME_HTTP;
1424 host = FixupProxyHostScheme(scheme, host);
[email protected]87a102b2009-07-14 05:23:301425 ProxyServer proxy_server = ProxyServer::FromURI(host,
1426 ProxyServer::SCHEME_HTTP);
[email protected]861c6c62009-04-20 16:50:561427 if (proxy_server.is_valid()) {
1428 *result_server = proxy_server;
1429 return true;
1430 }
1431 return false;
1432}
1433
[email protected]573c0502011-05-17 22:19:501434bool ProxyConfigServiceLinux::Delegate::GetConfigFromSettings(
[email protected]3e44697f2009-05-22 14:37:391435 ProxyConfig* config) {
[email protected]861c6c62009-04-20 16:50:561436 std::string mode;
[email protected]573c0502011-05-17 22:19:501437 if (!setting_getter_->GetString(SettingGetter::PROXY_MODE, &mode)) {
[email protected]861c6c62009-04-20 16:50:561438 // We expect this to always be set, so if we don't see it then we
[email protected]573c0502011-05-17 22:19:501439 // probably have a gconf/gsettings problem, and so we don't have a valid
[email protected]861c6c62009-04-20 16:50:561440 // proxy config.
1441 return false;
1442 }
[email protected]3e44697f2009-05-22 14:37:391443 if (mode == "none") {
[email protected]861c6c62009-04-20 16:50:561444 // Specifically specifies no proxy.
1445 return true;
[email protected]3e44697f2009-05-22 14:37:391446 }
[email protected]861c6c62009-04-20 16:50:561447
[email protected]3e44697f2009-05-22 14:37:391448 if (mode == "auto") {
[email protected]861c6c62009-04-20 16:50:561449 // automatic proxy config
1450 std::string pac_url_str;
[email protected]573c0502011-05-17 22:19:501451 if (setting_getter_->GetString(SettingGetter::PROXY_AUTOCONF_URL,
1452 &pac_url_str)) {
[email protected]861c6c62009-04-20 16:50:561453 if (!pac_url_str.empty()) {
1454 GURL pac_url(pac_url_str);
1455 if (!pac_url.is_valid())
1456 return false;
[email protected]ed4ed0f2010-02-24 00:20:481457 config->set_pac_url(pac_url);
[email protected]861c6c62009-04-20 16:50:561458 return true;
1459 }
1460 }
[email protected]ed4ed0f2010-02-24 00:20:481461 config->set_auto_detect(true);
[email protected]861c6c62009-04-20 16:50:561462 return true;
1463 }
1464
[email protected]3e44697f2009-05-22 14:37:391465 if (mode != "manual") {
[email protected]861c6c62009-04-20 16:50:561466 // Mode is unrecognized.
1467 return false;
1468 }
1469 bool use_http_proxy;
[email protected]573c0502011-05-17 22:19:501470 if (setting_getter_->GetBool(SettingGetter::PROXY_USE_HTTP_PROXY,
1471 &use_http_proxy)
[email protected]861c6c62009-04-20 16:50:561472 && !use_http_proxy) {
1473 // Another master switch for some reason. If set to false, then no
1474 // proxy. But we don't panic if the key doesn't exist.
1475 return true;
1476 }
1477
1478 bool same_proxy = false;
1479 // Indicates to use the http proxy for all protocols. This one may
[email protected]573c0502011-05-17 22:19:501480 // not exist (presumably on older versions); we assume false in that
[email protected]861c6c62009-04-20 16:50:561481 // case.
[email protected]573c0502011-05-17 22:19:501482 setting_getter_->GetBool(SettingGetter::PROXY_USE_SAME_PROXY,
1483 &same_proxy);
[email protected]861c6c62009-04-20 16:50:561484
[email protected]76960f3d2011-04-30 02:15:231485 ProxyServer proxy_for_http;
1486 ProxyServer proxy_for_https;
1487 ProxyServer proxy_for_ftp;
1488 ProxyServer socks_proxy; // (socks)
1489
1490 // This counts how many of the above ProxyServers were defined and valid.
1491 size_t num_proxies_specified = 0;
1492
1493 // Extract the per-scheme proxies. If we failed to parse it, or no proxy was
1494 // specified for the scheme, then the resulting ProxyServer will be invalid.
[email protected]573c0502011-05-17 22:19:501495 if (GetProxyFromSettings(SettingGetter::PROXY_HTTP_HOST, &proxy_for_http))
[email protected]76960f3d2011-04-30 02:15:231496 num_proxies_specified++;
[email protected]573c0502011-05-17 22:19:501497 if (GetProxyFromSettings(SettingGetter::PROXY_HTTPS_HOST, &proxy_for_https))
[email protected]76960f3d2011-04-30 02:15:231498 num_proxies_specified++;
[email protected]573c0502011-05-17 22:19:501499 if (GetProxyFromSettings(SettingGetter::PROXY_FTP_HOST, &proxy_for_ftp))
[email protected]76960f3d2011-04-30 02:15:231500 num_proxies_specified++;
[email protected]573c0502011-05-17 22:19:501501 if (GetProxyFromSettings(SettingGetter::PROXY_SOCKS_HOST, &socks_proxy))
[email protected]76960f3d2011-04-30 02:15:231502 num_proxies_specified++;
1503
1504 if (same_proxy) {
1505 if (proxy_for_http.is_valid()) {
1506 // Use the http proxy for all schemes.
[email protected]ed4ed0f2010-02-24 00:20:481507 config->proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY;
[email protected]76960f3d2011-04-30 02:15:231508 config->proxy_rules().single_proxy = proxy_for_http;
[email protected]861c6c62009-04-20 16:50:561509 }
[email protected]76960f3d2011-04-30 02:15:231510 } else if (num_proxies_specified > 0) {
1511 if (socks_proxy.is_valid() && num_proxies_specified == 1) {
1512 // If the only proxy specified was for SOCKS, use it for all schemes.
1513 config->proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY;
1514 config->proxy_rules().single_proxy = socks_proxy;
[email protected]861c6c62009-04-20 16:50:561515 } else {
[email protected]76960f3d2011-04-30 02:15:231516 // Otherwise use the indicate proxies per-scheme.
1517 config->proxy_rules().type =
1518 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME;
1519 config->proxy_rules().proxy_for_http = proxy_for_http;
1520 config->proxy_rules().proxy_for_https = proxy_for_https;
1521 config->proxy_rules().proxy_for_ftp = proxy_for_ftp;
1522 config->proxy_rules().fallback_proxy = socks_proxy;
[email protected]861c6c62009-04-20 16:50:561523 }
1524 }
1525
[email protected]ed4ed0f2010-02-24 00:20:481526 if (config->proxy_rules().empty()) {
[email protected]861c6c62009-04-20 16:50:561527 // Manual mode but we couldn't parse any rules.
1528 return false;
1529 }
1530
1531 // Check for authentication, just so we can warn.
[email protected]d7395e732009-08-28 23:13:431532 bool use_auth = false;
[email protected]573c0502011-05-17 22:19:501533 setting_getter_->GetBool(SettingGetter::PROXY_USE_AUTHENTICATION,
1534 &use_auth);
[email protected]62749f182009-07-15 13:16:541535 if (use_auth) {
1536 // ProxyConfig does not support authentication parameters, but
1537 // Chrome will prompt for the password later. So we ignore
1538 // /system/http_proxy/*auth* settings.
1539 LOG(WARNING) << "Proxy authentication parameters ignored, see bug 16709";
1540 }
[email protected]861c6c62009-04-20 16:50:561541
1542 // Now the bypass list.
[email protected]7541206c2010-02-19 20:24:061543 std::vector<std::string> ignore_hosts_list;
[email protected]ed4ed0f2010-02-24 00:20:481544 config->proxy_rules().bypass_rules.Clear();
[email protected]573c0502011-05-17 22:19:501545 if (setting_getter_->GetStringList(SettingGetter::PROXY_IGNORE_HOSTS,
1546 &ignore_hosts_list)) {
[email protected]a8185d02010-06-11 00:19:501547 std::vector<std::string>::const_iterator it(ignore_hosts_list.begin());
[email protected]1a597192010-07-09 16:58:381548 for (; it != ignore_hosts_list.end(); ++it) {
[email protected]573c0502011-05-17 22:19:501549 if (setting_getter_->MatchHostsUsingSuffixMatching()) {
[email protected]1a597192010-07-09 16:58:381550 config->proxy_rules().bypass_rules.
1551 AddRuleFromStringUsingSuffixMatching(*it);
1552 } else {
1553 config->proxy_rules().bypass_rules.AddRuleFromString(*it);
1554 }
1555 }
[email protected]a8185d02010-06-11 00:19:501556 }
[email protected]861c6c62009-04-20 16:50:561557 // Note that there are no settings with semantics corresponding to
[email protected]1a597192010-07-09 16:58:381558 // bypass of local names in GNOME. In KDE, "<local>" is supported
1559 // as a hostname rule.
[email protected]861c6c62009-04-20 16:50:561560
[email protected]a48bf4a2010-06-14 18:24:531561 // KDE allows one to reverse the bypass rules.
[email protected]573c0502011-05-17 22:19:501562 config->proxy_rules().reverse_bypass =
1563 setting_getter_->BypassListIsReversed();
[email protected]a48bf4a2010-06-14 18:24:531564
[email protected]861c6c62009-04-20 16:50:561565 return true;
1566}
1567
[email protected]76b90d312010-08-03 03:00:501568ProxyConfigServiceLinux::Delegate::Delegate(base::Environment* env_var_getter)
[email protected]76722472012-05-24 08:26:461569 : env_var_getter_(env_var_getter) {
[email protected]573c0502011-05-17 22:19:501570 // Figure out which SettingGetterImpl to use, if any.
[email protected]6b0349ef2010-10-16 04:56:061571 switch (base::nix::GetDesktopEnvironment(env_var_getter)) {
1572 case base::nix::DESKTOP_ENVIRONMENT_GNOME:
[email protected]8c20e3d2011-05-19 21:03:571573#if defined(USE_GIO)
1574 {
1575 scoped_ptr<SettingGetterImplGSettings> gs_getter(
1576 new SettingGetterImplGSettings());
1577 // We have to load symbols and check the GNOME version in use to decide
1578 // if we should use the gsettings getter. See LoadAndCheckVersion().
1579 if (gs_getter->LoadAndCheckVersion(env_var_getter))
1580 setting_getter_.reset(gs_getter.release());
1581 }
1582#endif
[email protected]6de53d42010-11-09 07:33:191583#if defined(USE_GCONF)
[email protected]8c20e3d2011-05-19 21:03:571584 // Fall back on gconf if gsettings is unavailable or incorrect.
1585 if (!setting_getter_.get())
1586 setting_getter_.reset(new SettingGetterImplGConf());
[email protected]6de53d42010-11-09 07:33:191587#endif
[email protected]d7395e732009-08-28 23:13:431588 break;
[email protected]6b0349ef2010-10-16 04:56:061589 case base::nix::DESKTOP_ENVIRONMENT_KDE3:
1590 case base::nix::DESKTOP_ENVIRONMENT_KDE4:
[email protected]573c0502011-05-17 22:19:501591 setting_getter_.reset(new SettingGetterImplKDE(env_var_getter));
[email protected]d7395e732009-08-28 23:13:431592 break;
[email protected]6b0349ef2010-10-16 04:56:061593 case base::nix::DESKTOP_ENVIRONMENT_XFCE:
1594 case base::nix::DESKTOP_ENVIRONMENT_OTHER:
[email protected]d7395e732009-08-28 23:13:431595 break;
1596 }
1597}
1598
[email protected]573c0502011-05-17 22:19:501599ProxyConfigServiceLinux::Delegate::Delegate(
1600 base::Environment* env_var_getter, SettingGetter* setting_getter)
[email protected]76722472012-05-24 08:26:461601 : env_var_getter_(env_var_getter), setting_getter_(setting_getter) {
[email protected]861c6c62009-04-20 16:50:561602}
1603
[email protected]d3066142011-05-10 02:36:201604void ProxyConfigServiceLinux::Delegate::SetUpAndFetchInitialConfig(
[email protected]76722472012-05-24 08:26:461605 base::SingleThreadTaskRunner* glib_thread_task_runner,
1606 base::SingleThreadTaskRunner* io_thread_task_runner,
[email protected]d7395e732009-08-28 23:13:431607 MessageLoopForIO* file_loop) {
[email protected]3e44697f2009-05-22 14:37:391608 // We should be running on the default glib main loop thread right
1609 // now. gconf can only be accessed from this thread.
[email protected]76722472012-05-24 08:26:461610 DCHECK(glib_thread_task_runner->BelongsToCurrentThread());
1611 glib_thread_task_runner_ = glib_thread_task_runner;
1612 io_thread_task_runner_ = io_thread_task_runner;
[email protected]3e44697f2009-05-22 14:37:391613
[email protected]76722472012-05-24 08:26:461614 // If we are passed a NULL |io_thread_task_runner| or |file_loop|,
1615 // then we don't set up proxy setting change notifications. This
1616 // should not be the usual case but is intended to simplify test
1617 // setups.
1618 if (!io_thread_task_runner_ || !file_loop)
[email protected]b30a3f52010-10-16 01:05:461619 VLOG(1) << "Monitoring of proxy setting changes is disabled";
[email protected]3e44697f2009-05-22 14:37:391620
1621 // Fetch and cache the current proxy config. The config is left in
[email protected]119655002010-07-23 06:02:401622 // cached_config_, where GetLatestProxyConfig() running on the IO thread
[email protected]3e44697f2009-05-22 14:37:391623 // will expect to find it. This is safe to do because we return
1624 // before this ProxyConfigServiceLinux is passed on to
1625 // the ProxyService.
[email protected]d6cb85b2009-07-23 22:10:531626
1627 // Note: It would be nice to prioritize environment variables
[email protected]92d2dc82010-04-08 17:49:591628 // and only fall back to gconf if env vars were unset. But
[email protected]d6cb85b2009-07-23 22:10:531629 // gnome-terminal "helpfully" sets http_proxy and no_proxy, and it
1630 // does so even if the proxy mode is set to auto, which would
1631 // mislead us.
1632
[email protected]3e44697f2009-05-22 14:37:391633 bool got_config = false;
[email protected]573c0502011-05-17 22:19:501634 if (setting_getter_.get() &&
[email protected]76722472012-05-24 08:26:461635 setting_getter_->Init(glib_thread_task_runner, file_loop) &&
[email protected]573c0502011-05-17 22:19:501636 GetConfigFromSettings(&cached_config_)) {
[email protected]d3066142011-05-10 02:36:201637 cached_config_.set_id(1); // Mark it as valid.
1638 VLOG(1) << "Obtained proxy settings from "
[email protected]573c0502011-05-17 22:19:501639 << setting_getter_->GetDataSource();
[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]d3066142011-05-10 02:36:201679 cached_config_.set_id(1); // Mark it as valid.
[email protected]b30a3f52010-10-16 01:05:461680 VLOG(1) << "Obtained proxy settings from environment variables";
[email protected]3e44697f2009-05-22 14:37:391681 }
[email protected]861c6c62009-04-20 16:50:561682 }
[email protected]3e44697f2009-05-22 14:37:391683}
1684
[email protected]573c0502011-05-17 22:19:501685// Depending on the SettingGetter in use, this method will be called
[email protected]d3066142011-05-10 02:36:201686// on either the UI thread (GConf) or the file thread (KDE).
1687void ProxyConfigServiceLinux::Delegate::SetUpNotifications() {
[email protected]76722472012-05-24 08:26:461688 scoped_refptr<base::SingleThreadTaskRunner> required_loop =
1689 setting_getter_->GetNotificationTaskRunner();
[email protected]434fff82012-04-24 21:13:281690 DCHECK(!required_loop || required_loop->BelongsToCurrentThread());
[email protected]573c0502011-05-17 22:19:501691 if (!setting_getter_->SetUpNotifications(this))
[email protected]d3066142011-05-10 02:36:201692 LOG(ERROR) << "Unable to set up proxy configuration change notifications";
1693}
1694
[email protected]119655002010-07-23 06:02:401695void ProxyConfigServiceLinux::Delegate::AddObserver(Observer* observer) {
1696 observers_.AddObserver(observer);
1697}
1698
1699void ProxyConfigServiceLinux::Delegate::RemoveObserver(Observer* observer) {
1700 observers_.RemoveObserver(observer);
1701}
1702
[email protected]3a29593d2011-04-11 10:07:521703ProxyConfigService::ConfigAvailability
1704 ProxyConfigServiceLinux::Delegate::GetLatestProxyConfig(
1705 ProxyConfig* config) {
[email protected]3e44697f2009-05-22 14:37:391706 // This is called from the IO thread.
[email protected]76722472012-05-24 08:26:461707 DCHECK(!io_thread_task_runner_ ||
1708 io_thread_task_runner_->BelongsToCurrentThread());
[email protected]3e44697f2009-05-22 14:37:391709
1710 // Simply return the last proxy configuration that glib_default_loop
1711 // notified us of.
[email protected]119655002010-07-23 06:02:401712 *config = cached_config_.is_valid() ?
1713 cached_config_ : ProxyConfig::CreateDirect();
1714
[email protected]3a29593d2011-04-11 10:07:521715 // We return CONFIG_VALID to indicate that *config was filled in. It is always
[email protected]119655002010-07-23 06:02:401716 // going to be available since we initialized eagerly on the UI thread.
1717 // TODO(eroman): do lazy initialization instead, so we no longer need
1718 // to construct ProxyConfigServiceLinux on the UI thread.
1719 // In which case, we may return false here.
[email protected]3a29593d2011-04-11 10:07:521720 return CONFIG_VALID;
[email protected]3e44697f2009-05-22 14:37:391721}
1722
[email protected]573c0502011-05-17 22:19:501723// Depending on the SettingGetter in use, this method will be called
[email protected]d7395e732009-08-28 23:13:431724// on either the UI thread (GConf) or the file thread (KDE).
[email protected]3e44697f2009-05-22 14:37:391725void ProxyConfigServiceLinux::Delegate::OnCheckProxyConfigSettings() {
[email protected]76722472012-05-24 08:26:461726 scoped_refptr<base::SingleThreadTaskRunner> required_loop =
1727 setting_getter_->GetNotificationTaskRunner();
[email protected]434fff82012-04-24 21:13:281728 DCHECK(!required_loop || required_loop->BelongsToCurrentThread());
[email protected]3e44697f2009-05-22 14:37:391729 ProxyConfig new_config;
[email protected]573c0502011-05-17 22:19:501730 bool valid = GetConfigFromSettings(&new_config);
[email protected]3e44697f2009-05-22 14:37:391731 if (valid)
1732 new_config.set_id(1); // mark it as valid
1733
[email protected]119655002010-07-23 06:02:401734 // See if it is different from what we had before.
[email protected]3e44697f2009-05-22 14:37:391735 if (new_config.is_valid() != reference_config_.is_valid() ||
1736 !new_config.Equals(reference_config_)) {
[email protected]76722472012-05-24 08:26:461737 // Post a task to the IO thread with the new configuration, so it can
[email protected]3e44697f2009-05-22 14:37:391738 // update |cached_config_|.
[email protected]76722472012-05-24 08:26:461739 io_thread_task_runner_->PostTask(FROM_HERE, base::Bind(
[email protected]6af889c2011-10-06 23:11:411740 &ProxyConfigServiceLinux::Delegate::SetNewProxyConfig,
1741 this, new_config));
[email protected]d1f9d472009-08-13 19:59:301742 // Update the thread-private copy in |reference_config_| as well.
1743 reference_config_ = new_config;
[email protected]d3066142011-05-10 02:36:201744 } else {
1745 VLOG(1) << "Detected no-op change to proxy settings. Doing nothing.";
[email protected]3e44697f2009-05-22 14:37:391746 }
1747}
1748
1749void ProxyConfigServiceLinux::Delegate::SetNewProxyConfig(
1750 const ProxyConfig& new_config) {
[email protected]76722472012-05-24 08:26:461751 DCHECK(io_thread_task_runner_->BelongsToCurrentThread());
[email protected]b30a3f52010-10-16 01:05:461752 VLOG(1) << "Proxy configuration changed";
[email protected]3e44697f2009-05-22 14:37:391753 cached_config_ = new_config;
[email protected]3a29593d2011-04-11 10:07:521754 FOR_EACH_OBSERVER(
1755 Observer, observers_,
1756 OnProxyConfigChanged(new_config, ProxyConfigService::CONFIG_VALID));
[email protected]3e44697f2009-05-22 14:37:391757}
1758
1759void ProxyConfigServiceLinux::Delegate::PostDestroyTask() {
[email protected]573c0502011-05-17 22:19:501760 if (!setting_getter_.get())
[email protected]d7395e732009-08-28 23:13:431761 return;
[email protected]76722472012-05-24 08:26:461762 scoped_refptr<base::SingleThreadTaskRunner> shutdown_loop =
1763 setting_getter_->GetNotificationTaskRunner();
[email protected]434fff82012-04-24 21:13:281764 if (!shutdown_loop || shutdown_loop->BelongsToCurrentThread()) {
[email protected]3e44697f2009-05-22 14:37:391765 // Already on the right thread, call directly.
1766 // This is the case for the unittests.
1767 OnDestroy();
1768 } else {
[email protected]d7395e732009-08-28 23:13:431769 // Post to shutdown thread. Note that on browser shutdown, we may quit
1770 // this MessageLoop and exit the program before ever running this.
[email protected]6af889c2011-10-06 23:11:411771 shutdown_loop->PostTask(FROM_HERE, base::Bind(
1772 &ProxyConfigServiceLinux::Delegate::OnDestroy, this));
[email protected]3e44697f2009-05-22 14:37:391773 }
1774}
1775void ProxyConfigServiceLinux::Delegate::OnDestroy() {
[email protected]76722472012-05-24 08:26:461776 scoped_refptr<base::SingleThreadTaskRunner> shutdown_loop =
1777 setting_getter_->GetNotificationTaskRunner();
[email protected]434fff82012-04-24 21:13:281778 DCHECK(!shutdown_loop || shutdown_loop->BelongsToCurrentThread());
[email protected]573c0502011-05-17 22:19:501779 setting_getter_->ShutDown();
[email protected]3e44697f2009-05-22 14:37:391780}
1781
1782ProxyConfigServiceLinux::ProxyConfigServiceLinux()
[email protected]76b90d312010-08-03 03:00:501783 : delegate_(new Delegate(base::Environment::Create())) {
[email protected]3e44697f2009-05-22 14:37:391784}
1785
[email protected]8e1845e12010-09-15 19:22:241786ProxyConfigServiceLinux::~ProxyConfigServiceLinux() {
1787 delegate_->PostDestroyTask();
1788}
1789
[email protected]3e44697f2009-05-22 14:37:391790ProxyConfigServiceLinux::ProxyConfigServiceLinux(
[email protected]76b90d312010-08-03 03:00:501791 base::Environment* env_var_getter)
[email protected]9a3d8d42009-09-03 17:01:461792 : delegate_(new Delegate(env_var_getter)) {
1793}
1794
1795ProxyConfigServiceLinux::ProxyConfigServiceLinux(
[email protected]573c0502011-05-17 22:19:501796 base::Environment* env_var_getter, SettingGetter* setting_getter)
1797 : delegate_(new Delegate(env_var_getter, setting_getter)) {
[email protected]861c6c62009-04-20 16:50:561798}
1799
[email protected]e4be2dd2010-12-14 00:44:391800void ProxyConfigServiceLinux::AddObserver(Observer* observer) {
1801 delegate_->AddObserver(observer);
1802}
1803
1804void ProxyConfigServiceLinux::RemoveObserver(Observer* observer) {
1805 delegate_->RemoveObserver(observer);
1806}
1807
[email protected]3a29593d2011-04-11 10:07:521808ProxyConfigService::ConfigAvailability
1809 ProxyConfigServiceLinux::GetLatestProxyConfig(ProxyConfig* config) {
[email protected]e4be2dd2010-12-14 00:44:391810 return delegate_->GetLatestProxyConfig(config);
1811}
1812
[email protected]861c6c62009-04-20 16:50:561813} // namespace net