blob: 1c8e5a367d84f5de9c47ea42f7d72cde7c0f5a6a [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]749bf5c2012-09-17 03:15:21273 virtual void ShutDown() OVERRIDE {
[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]749bf5c2012-09-17 03:15:21290 virtual bool SetUpNotifications(
291 ProxyConfigServiceLinux::Delegate* delegate) OVERRIDE {
[email protected]3e44697f2009-05-22 14:37:39292 DCHECK(client_);
[email protected]76722472012-05-24 08:26:46293 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]3e44697f2009-05-22 14:37:39294 GError* error = NULL;
[email protected]d7395e732009-08-28 23:13:43295 notify_delegate_ = delegate;
[email protected]b160df32012-02-06 20:39:41296 // We have to keep track of the IDs returned by gconf_client_notify_add() so
297 // that we can remove them in ShutDown(). (Otherwise, notifications will be
298 // delivered to this object after it is deleted, which is bad, m'kay?)
299 system_proxy_id_ = gconf_client_notify_add(
[email protected]3e44697f2009-05-22 14:37:39300 client_, "/system/proxy",
[email protected]d7395e732009-08-28 23:13:43301 OnGConfChangeNotification, this,
[email protected]3e44697f2009-05-22 14:37:39302 NULL, &error);
303 if (error == NULL) {
[email protected]b160df32012-02-06 20:39:41304 system_http_proxy_id_ = gconf_client_notify_add(
[email protected]3e44697f2009-05-22 14:37:39305 client_, "/system/http_proxy",
[email protected]d7395e732009-08-28 23:13:43306 OnGConfChangeNotification, this,
[email protected]3e44697f2009-05-22 14:37:39307 NULL, &error);
308 }
309 if (error != NULL) {
310 LOG(ERROR) << "Error requesting gconf notifications: " << error->message;
311 g_error_free(error);
[email protected]d3066142011-05-10 02:36:20312 ShutDown();
[email protected]3e44697f2009-05-22 14:37:39313 return false;
314 }
[email protected]d3066142011-05-10 02:36:20315 // Simulate a change to avoid possibly losing updates before this point.
316 OnChangeNotification();
[email protected]3e44697f2009-05-22 14:37:39317 return true;
[email protected]861c6c62009-04-20 16:50:56318 }
319
[email protected]76722472012-05-24 08:26:46320 virtual base::SingleThreadTaskRunner* GetNotificationTaskRunner() OVERRIDE {
321 return task_runner_;
[email protected]d7395e732009-08-28 23:13:43322 }
323
[email protected]db8ff912012-06-12 23:32:51324 virtual ProxyConfigSource GetConfigSource() OVERRIDE {
325 return PROXY_CONFIG_SOURCE_GCONF;
[email protected]d7395e732009-08-28 23:13:43326 }
327
[email protected]c4c1b482011-07-22 17:24:26328 virtual bool GetString(StringSetting key, std::string* result) OVERRIDE {
[email protected]573c0502011-05-17 22:19:50329 switch (key) {
330 case PROXY_MODE:
331 return GetStringByPath("/system/proxy/mode", result);
332 case PROXY_AUTOCONF_URL:
333 return GetStringByPath("/system/proxy/autoconfig_url", result);
334 case PROXY_HTTP_HOST:
335 return GetStringByPath("/system/http_proxy/host", result);
336 case PROXY_HTTPS_HOST:
337 return GetStringByPath("/system/proxy/secure_host", result);
338 case PROXY_FTP_HOST:
339 return GetStringByPath("/system/proxy/ftp_host", result);
340 case PROXY_SOCKS_HOST:
341 return GetStringByPath("/system/proxy/socks_host", result);
[email protected]573c0502011-05-17 22:19:50342 }
[email protected]6b5fe742011-05-20 21:46:48343 return false; // Placate compiler.
[email protected]573c0502011-05-17 22:19:50344 }
[email protected]c4c1b482011-07-22 17:24:26345 virtual bool GetBool(BoolSetting key, bool* result) OVERRIDE {
[email protected]573c0502011-05-17 22:19:50346 switch (key) {
347 case PROXY_USE_HTTP_PROXY:
348 return GetBoolByPath("/system/http_proxy/use_http_proxy", result);
349 case PROXY_USE_SAME_PROXY:
350 return GetBoolByPath("/system/http_proxy/use_same_proxy", result);
351 case PROXY_USE_AUTHENTICATION:
352 return GetBoolByPath("/system/http_proxy/use_authentication", result);
[email protected]573c0502011-05-17 22:19:50353 }
[email protected]6b5fe742011-05-20 21:46:48354 return false; // Placate compiler.
[email protected]573c0502011-05-17 22:19:50355 }
[email protected]c4c1b482011-07-22 17:24:26356 virtual bool GetInt(IntSetting key, int* result) OVERRIDE {
[email protected]573c0502011-05-17 22:19:50357 switch (key) {
358 case PROXY_HTTP_PORT:
359 return GetIntByPath("/system/http_proxy/port", result);
360 case PROXY_HTTPS_PORT:
361 return GetIntByPath("/system/proxy/secure_port", result);
362 case PROXY_FTP_PORT:
363 return GetIntByPath("/system/proxy/ftp_port", result);
364 case PROXY_SOCKS_PORT:
365 return GetIntByPath("/system/proxy/socks_port", result);
[email protected]573c0502011-05-17 22:19:50366 }
[email protected]6b5fe742011-05-20 21:46:48367 return false; // Placate compiler.
[email protected]573c0502011-05-17 22:19:50368 }
[email protected]6b5fe742011-05-20 21:46:48369 virtual bool GetStringList(StringListSetting key,
[email protected]c4c1b482011-07-22 17:24:26370 std::vector<std::string>* result) OVERRIDE {
[email protected]573c0502011-05-17 22:19:50371 switch (key) {
372 case PROXY_IGNORE_HOSTS:
373 return GetStringListByPath("/system/http_proxy/ignore_hosts", result);
[email protected]573c0502011-05-17 22:19:50374 }
[email protected]6b5fe742011-05-20 21:46:48375 return false; // Placate compiler.
[email protected]573c0502011-05-17 22:19:50376 }
377
[email protected]c4c1b482011-07-22 17:24:26378 virtual bool BypassListIsReversed() OVERRIDE {
[email protected]573c0502011-05-17 22:19:50379 // This is a KDE-specific setting.
380 return false;
381 }
382
[email protected]c4c1b482011-07-22 17:24:26383 virtual bool MatchHostsUsingSuffixMatching() OVERRIDE {
[email protected]573c0502011-05-17 22:19:50384 return false;
385 }
386
387 private:
388 bool GetStringByPath(const char* key, std::string* result) {
[email protected]3e44697f2009-05-22 14:37:39389 DCHECK(client_);
[email protected]76722472012-05-24 08:26:46390 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]861c6c62009-04-20 16:50:56391 GError* error = NULL;
392 gchar* value = gconf_client_get_string(client_, key, &error);
393 if (HandleGError(error, key))
394 return false;
395 if (!value)
396 return false;
397 *result = value;
398 g_free(value);
399 return true;
400 }
[email protected]573c0502011-05-17 22:19:50401 bool GetBoolByPath(const char* key, bool* result) {
[email protected]3e44697f2009-05-22 14:37:39402 DCHECK(client_);
[email protected]76722472012-05-24 08:26:46403 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]861c6c62009-04-20 16:50:56404 GError* error = NULL;
405 // We want to distinguish unset values from values defaulting to
406 // false. For that we need to use the type-generic
407 // gconf_client_get() rather than gconf_client_get_bool().
408 GConfValue* gconf_value = gconf_client_get(client_, key, &error);
409 if (HandleGError(error, key))
410 return false;
411 if (!gconf_value) {
412 // Unset.
413 return false;
414 }
415 if (gconf_value->type != GCONF_VALUE_BOOL) {
416 gconf_value_free(gconf_value);
417 return false;
418 }
419 gboolean bool_value = gconf_value_get_bool(gconf_value);
420 *result = static_cast<bool>(bool_value);
421 gconf_value_free(gconf_value);
422 return true;
423 }
[email protected]573c0502011-05-17 22:19:50424 bool GetIntByPath(const char* key, int* result) {
[email protected]3e44697f2009-05-22 14:37:39425 DCHECK(client_);
[email protected]76722472012-05-24 08:26:46426 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]861c6c62009-04-20 16:50:56427 GError* error = NULL;
428 int value = gconf_client_get_int(client_, key, &error);
429 if (HandleGError(error, key))
430 return false;
431 // We don't bother to distinguish an unset value because callers
432 // don't care. 0 is returned if unset.
433 *result = value;
434 return true;
435 }
[email protected]573c0502011-05-17 22:19:50436 bool GetStringListByPath(const char* key, std::vector<std::string>* result) {
[email protected]3e44697f2009-05-22 14:37:39437 DCHECK(client_);
[email protected]76722472012-05-24 08:26:46438 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]861c6c62009-04-20 16:50:56439 GError* error = NULL;
440 GSList* list = gconf_client_get_list(client_, key,
441 GCONF_VALUE_STRING, &error);
442 if (HandleGError(error, key))
443 return false;
[email protected]8c20e3d2011-05-19 21:03:57444 if (!list)
[email protected]861c6c62009-04-20 16:50:56445 return false;
[email protected]861c6c62009-04-20 16:50:56446 for (GSList *it = list; it; it = it->next) {
447 result->push_back(static_cast<char*>(it->data));
448 g_free(it->data);
449 }
450 g_slist_free(list);
451 return true;
452 }
453
[email protected]861c6c62009-04-20 16:50:56454 // Logs and frees a glib error. Returns false if there was no error
455 // (error is NULL).
456 bool HandleGError(GError* error, const char* key) {
457 if (error != NULL) {
[email protected]3e44697f2009-05-22 14:37:39458 LOG(ERROR) << "Error getting gconf value for " << key
459 << ": " << error->message;
[email protected]861c6c62009-04-20 16:50:56460 g_error_free(error);
461 return true;
462 }
463 return false;
464 }
465
[email protected]d7395e732009-08-28 23:13:43466 // This is the callback from the debounce timer.
467 void OnDebouncedNotification() {
[email protected]76722472012-05-24 08:26:46468 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]961ac942011-04-28 18:18:14469 CHECK(notify_delegate_);
[email protected]d7395e732009-08-28 23:13:43470 // Forward to a method on the proxy config service delegate object.
471 notify_delegate_->OnCheckProxyConfigSettings();
472 }
473
474 void OnChangeNotification() {
475 // We don't use Reset() because the timer may not yet be running.
476 // (In that case Stop() is a no-op.)
477 debounce_timer_.Stop();
[email protected]d323a172011-09-02 18:23:02478 debounce_timer_.Start(FROM_HERE,
[email protected]8c20e3d2011-05-19 21:03:57479 base::TimeDelta::FromMilliseconds(kDebounceTimeoutMilliseconds),
480 this, &SettingGetterImplGConf::OnDebouncedNotification);
[email protected]d7395e732009-08-28 23:13:43481 }
482
[email protected]8c20e3d2011-05-19 21:03:57483 // gconf notification callback, dispatched on the default glib main loop.
484 static void OnGConfChangeNotification(GConfClient* client, guint cnxn_id,
485 GConfEntry* entry, gpointer user_data) {
[email protected]b30a3f52010-10-16 01:05:46486 VLOG(1) << "gconf change notification for key "
487 << gconf_entry_get_key(entry);
[email protected]d7395e732009-08-28 23:13:43488 // We don't track which key has changed, just that something did change.
[email protected]573c0502011-05-17 22:19:50489 SettingGetterImplGConf* setting_getter =
490 reinterpret_cast<SettingGetterImplGConf*>(user_data);
[email protected]d7395e732009-08-28 23:13:43491 setting_getter->OnChangeNotification();
492 }
493
[email protected]861c6c62009-04-20 16:50:56494 GConfClient* client_;
[email protected]b160df32012-02-06 20:39:41495 // These ids are the values returned from gconf_client_notify_add(), which we
496 // will need in order to later call gconf_client_notify_remove().
497 guint system_proxy_id_;
498 guint system_http_proxy_id_;
499
[email protected]d7395e732009-08-28 23:13:43500 ProxyConfigServiceLinux::Delegate* notify_delegate_;
[email protected]573c0502011-05-17 22:19:50501 base::OneShotTimer<SettingGetterImplGConf> debounce_timer_;
[email protected]861c6c62009-04-20 16:50:56502
[email protected]76722472012-05-24 08:26:46503 // Task runner for the thread that we make gconf calls on. It should
[email protected]3e44697f2009-05-22 14:37:39504 // be the UI thread and all our methods should be called on this
505 // thread. Only for assertions.
[email protected]76722472012-05-24 08:26:46506 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
[email protected]3e44697f2009-05-22 14:37:39507
[email protected]573c0502011-05-17 22:19:50508 DISALLOW_COPY_AND_ASSIGN(SettingGetterImplGConf);
[email protected]d7395e732009-08-28 23:13:43509};
[email protected]6de53d42010-11-09 07:33:19510#endif // defined(USE_GCONF)
[email protected]d7395e732009-08-28 23:13:43511
[email protected]8c20e3d2011-05-19 21:03:57512#if defined(USE_GIO)
513// This setting getter uses gsettings, as used in most GNOME 3 desktops.
514class SettingGetterImplGSettings
515 : public ProxyConfigServiceLinux::SettingGetter {
516 public:
[email protected]0e14e87d2011-06-21 21:24:19517 SettingGetterImplGSettings() :
518#if defined(DLOPEN_GSETTINGS)
519 g_settings_new(NULL),
520 g_settings_get_child(NULL),
521 g_settings_get_boolean(NULL),
522 g_settings_get_string(NULL),
523 g_settings_get_int(NULL),
524 g_settings_get_strv(NULL),
525 g_settings_list_schemas(NULL),
526 gio_handle_(NULL),
527#endif
528 client_(NULL),
529 http_client_(NULL),
530 https_client_(NULL),
531 ftp_client_(NULL),
532 socks_client_(NULL),
[email protected]76722472012-05-24 08:26:46533 notify_delegate_(NULL) {
[email protected]8c20e3d2011-05-19 21:03:57534 }
535
536 virtual ~SettingGetterImplGSettings() {
537 // client_ should have been released before now, from
538 // Delegate::OnDestroy(), while running on the UI thread. However
539 // on exiting the process, it may happen that
540 // Delegate::OnDestroy() task is left pending on the glib loop
541 // after the loop was quit, and pending tasks may then be deleted
542 // without being run.
543 if (client_) {
544 // gconf client was not cleaned up.
[email protected]76722472012-05-24 08:26:46545 if (task_runner_->BelongsToCurrentThread()) {
[email protected]8c20e3d2011-05-19 21:03:57546 // We are on the UI thread so we can clean it safely. This is
547 // the case at least for ui_tests running under Valgrind in
548 // bug 16076.
549 VLOG(1) << "~SettingGetterImplGSettings: releasing gsettings client";
550 ShutDown();
551 } else {
552 LOG(WARNING) << "~SettingGetterImplGSettings: leaking gsettings client";
553 client_ = NULL;
554 }
555 }
556 DCHECK(!client_);
557#if defined(DLOPEN_GSETTINGS)
558 if (gio_handle_) {
559 dlclose(gio_handle_);
560 gio_handle_ = NULL;
561 }
562#endif
563 }
564
[email protected]4cf80f0b2011-05-20 20:30:26565 bool SchemaExists(const char* schema_name) {
566 const gchar* const* schemas = g_settings_list_schemas();
567 while (*schemas) {
[email protected]a099f3ae2011-08-16 21:06:58568 if (strcmp(schema_name, static_cast<const char*>(*schemas)) == 0)
[email protected]4cf80f0b2011-05-20 20:30:26569 return true;
570 schemas++;
571 }
572 return false;
573 }
574
[email protected]8c20e3d2011-05-19 21:03:57575 // LoadAndCheckVersion() must be called *before* Init()!
576 bool LoadAndCheckVersion(base::Environment* env);
577
[email protected]76722472012-05-24 08:26:46578 virtual bool Init(base::SingleThreadTaskRunner* glib_thread_task_runner,
[email protected]c4c1b482011-07-22 17:24:26579 MessageLoopForIO* file_loop) OVERRIDE {
[email protected]76722472012-05-24 08:26:46580 DCHECK(glib_thread_task_runner->BelongsToCurrentThread());
[email protected]8c20e3d2011-05-19 21:03:57581 DCHECK(!client_);
[email protected]76722472012-05-24 08:26:46582 DCHECK(!task_runner_);
[email protected]4cf80f0b2011-05-20 20:30:26583
584 if (!SchemaExists("org.gnome.system.proxy") ||
585 !(client_ = g_settings_new("org.gnome.system.proxy"))) {
[email protected]8c20e3d2011-05-19 21:03:57586 // It's not clear whether/when this can return NULL.
587 LOG(ERROR) << "Unable to create a gsettings client";
588 return false;
589 }
[email protected]76722472012-05-24 08:26:46590 task_runner_ = glib_thread_task_runner;
[email protected]8c20e3d2011-05-19 21:03:57591 // We assume these all work if the above call worked.
592 http_client_ = g_settings_get_child(client_, "http");
593 https_client_ = g_settings_get_child(client_, "https");
594 ftp_client_ = g_settings_get_child(client_, "ftp");
595 socks_client_ = g_settings_get_child(client_, "socks");
596 DCHECK(http_client_ && https_client_ && ftp_client_ && socks_client_);
597 return true;
598 }
599
[email protected]749bf5c2012-09-17 03:15:21600 virtual void ShutDown() OVERRIDE {
[email protected]8c20e3d2011-05-19 21:03:57601 if (client_) {
[email protected]76722472012-05-24 08:26:46602 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]8c20e3d2011-05-19 21:03:57603 // This also disables gsettings notifications.
604 g_object_unref(socks_client_);
605 g_object_unref(ftp_client_);
606 g_object_unref(https_client_);
607 g_object_unref(http_client_);
608 g_object_unref(client_);
609 // We only need to null client_ because it's the only one that we check.
610 client_ = NULL;
[email protected]76722472012-05-24 08:26:46611 task_runner_ = NULL;
[email protected]8c20e3d2011-05-19 21:03:57612 }
613 }
614
[email protected]749bf5c2012-09-17 03:15:21615 virtual bool SetUpNotifications(
616 ProxyConfigServiceLinux::Delegate* delegate) OVERRIDE {
[email protected]8c20e3d2011-05-19 21:03:57617 DCHECK(client_);
[email protected]76722472012-05-24 08:26:46618 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]8c20e3d2011-05-19 21:03:57619 notify_delegate_ = delegate;
620 // We could watch for the change-event signal instead of changed, but
621 // since we have to watch more than one object, we'd still have to
622 // debounce change notifications. This is conceptually simpler.
623 g_signal_connect(G_OBJECT(client_), "changed",
624 G_CALLBACK(OnGSettingsChangeNotification), this);
625 g_signal_connect(G_OBJECT(http_client_), "changed",
626 G_CALLBACK(OnGSettingsChangeNotification), this);
627 g_signal_connect(G_OBJECT(https_client_), "changed",
628 G_CALLBACK(OnGSettingsChangeNotification), this);
629 g_signal_connect(G_OBJECT(ftp_client_), "changed",
630 G_CALLBACK(OnGSettingsChangeNotification), this);
631 g_signal_connect(G_OBJECT(socks_client_), "changed",
632 G_CALLBACK(OnGSettingsChangeNotification), this);
633 // Simulate a change to avoid possibly losing updates before this point.
634 OnChangeNotification();
635 return true;
636 }
637
[email protected]76722472012-05-24 08:26:46638 virtual base::SingleThreadTaskRunner* GetNotificationTaskRunner() OVERRIDE {
639 return task_runner_;
[email protected]8c20e3d2011-05-19 21:03:57640 }
641
[email protected]db8ff912012-06-12 23:32:51642 virtual ProxyConfigSource GetConfigSource() OVERRIDE {
643 return PROXY_CONFIG_SOURCE_GSETTINGS;
[email protected]8c20e3d2011-05-19 21:03:57644 }
645
[email protected]c4c1b482011-07-22 17:24:26646 virtual bool GetString(StringSetting key, std::string* result) OVERRIDE {
[email protected]8c20e3d2011-05-19 21:03:57647 DCHECK(client_);
648 switch (key) {
649 case PROXY_MODE:
650 return GetStringByPath(client_, "mode", result);
651 case PROXY_AUTOCONF_URL:
652 return GetStringByPath(client_, "autoconfig-url", result);
653 case PROXY_HTTP_HOST:
654 return GetStringByPath(http_client_, "host", result);
655 case PROXY_HTTPS_HOST:
656 return GetStringByPath(https_client_, "host", result);
657 case PROXY_FTP_HOST:
658 return GetStringByPath(ftp_client_, "host", result);
659 case PROXY_SOCKS_HOST:
660 return GetStringByPath(socks_client_, "host", result);
[email protected]8c20e3d2011-05-19 21:03:57661 }
[email protected]6b5fe742011-05-20 21:46:48662 return false; // Placate compiler.
[email protected]8c20e3d2011-05-19 21:03:57663 }
[email protected]c4c1b482011-07-22 17:24:26664 virtual bool GetBool(BoolSetting key, bool* result) OVERRIDE {
[email protected]8c20e3d2011-05-19 21:03:57665 DCHECK(client_);
666 switch (key) {
667 case PROXY_USE_HTTP_PROXY:
668 // Although there is an "enabled" boolean in http_client_, it is not set
669 // to true by the proxy config utility. We ignore it and return false.
670 return false;
671 case PROXY_USE_SAME_PROXY:
672 // Similarly, although there is a "use-same-proxy" boolean in client_,
673 // it is never set to false by the proxy config utility. We ignore it.
674 return false;
675 case PROXY_USE_AUTHENTICATION:
676 // There is also no way to set this in the proxy config utility, but it
677 // doesn't hurt us to get the actual setting (unlike the two above).
678 return GetBoolByPath(http_client_, "use-authentication", result);
[email protected]8c20e3d2011-05-19 21:03:57679 }
[email protected]6b5fe742011-05-20 21:46:48680 return false; // Placate compiler.
[email protected]8c20e3d2011-05-19 21:03:57681 }
[email protected]c4c1b482011-07-22 17:24:26682 virtual bool GetInt(IntSetting key, int* result) OVERRIDE {
[email protected]8c20e3d2011-05-19 21:03:57683 DCHECK(client_);
684 switch (key) {
685 case PROXY_HTTP_PORT:
686 return GetIntByPath(http_client_, "port", result);
687 case PROXY_HTTPS_PORT:
688 return GetIntByPath(https_client_, "port", result);
689 case PROXY_FTP_PORT:
690 return GetIntByPath(ftp_client_, "port", result);
691 case PROXY_SOCKS_PORT:
692 return GetIntByPath(socks_client_, "port", result);
[email protected]8c20e3d2011-05-19 21:03:57693 }
[email protected]6b5fe742011-05-20 21:46:48694 return false; // Placate compiler.
[email protected]8c20e3d2011-05-19 21:03:57695 }
[email protected]6b5fe742011-05-20 21:46:48696 virtual bool GetStringList(StringListSetting key,
[email protected]c4c1b482011-07-22 17:24:26697 std::vector<std::string>* result) OVERRIDE {
[email protected]8c20e3d2011-05-19 21:03:57698 DCHECK(client_);
699 switch (key) {
700 case PROXY_IGNORE_HOSTS:
701 return GetStringListByPath(client_, "ignore-hosts", result);
[email protected]8c20e3d2011-05-19 21:03:57702 }
[email protected]6b5fe742011-05-20 21:46:48703 return false; // Placate compiler.
[email protected]8c20e3d2011-05-19 21:03:57704 }
705
[email protected]c4c1b482011-07-22 17:24:26706 virtual bool BypassListIsReversed() OVERRIDE {
[email protected]8c20e3d2011-05-19 21:03:57707 // This is a KDE-specific setting.
708 return false;
709 }
710
[email protected]c4c1b482011-07-22 17:24:26711 virtual bool MatchHostsUsingSuffixMatching() OVERRIDE {
[email protected]8c20e3d2011-05-19 21:03:57712 return false;
713 }
714
715 private:
716#if defined(DLOPEN_GSETTINGS)
717 // We replicate the prototypes for the g_settings APIs we need. We may not
718 // even be compiling on a system that has them. If we are, these won't
719 // conflict both because they are identical and also due to scoping. The
720 // scoping will also ensure that these get used instead of the global ones.
721 struct _GSettings;
722 typedef struct _GSettings GSettings;
723 GSettings* (*g_settings_new)(const gchar* schema);
724 GSettings* (*g_settings_get_child)(GSettings* settings, const gchar* name);
725 gboolean (*g_settings_get_boolean)(GSettings* settings, const gchar* key);
726 gchar* (*g_settings_get_string)(GSettings* settings, const gchar* key);
727 gint (*g_settings_get_int)(GSettings* settings, const gchar* key);
728 gchar** (*g_settings_get_strv)(GSettings* settings, const gchar* key);
[email protected]4cf80f0b2011-05-20 20:30:26729 const gchar* const* (*g_settings_list_schemas)();
[email protected]8c20e3d2011-05-19 21:03:57730
731 // The library handle.
732 void* gio_handle_;
733
734 // Load a symbol from |gio_handle_| and store it into |*func_ptr|.
735 bool LoadSymbol(const char* name, void** func_ptr) {
736 dlerror();
737 *func_ptr = dlsym(gio_handle_, name);
738 const char* error = dlerror();
739 if (error) {
740 VLOG(1) << "Unable to load symbol " << name << ": " << error;
741 return false;
742 }
743 return true;
744 }
745#endif // defined(DLOPEN_GSETTINGS)
746
747 bool GetStringByPath(GSettings* client, const char* key,
748 std::string* result) {
[email protected]76722472012-05-24 08:26:46749 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]8c20e3d2011-05-19 21:03:57750 gchar* value = g_settings_get_string(client, key);
751 if (!value)
752 return false;
753 *result = value;
754 g_free(value);
755 return true;
756 }
757 bool GetBoolByPath(GSettings* client, const char* key, bool* result) {
[email protected]76722472012-05-24 08:26:46758 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]8c20e3d2011-05-19 21:03:57759 *result = static_cast<bool>(g_settings_get_boolean(client, key));
760 return true;
761 }
762 bool GetIntByPath(GSettings* client, const char* key, int* result) {
[email protected]76722472012-05-24 08:26:46763 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]8c20e3d2011-05-19 21:03:57764 *result = g_settings_get_int(client, key);
765 return true;
766 }
767 bool GetStringListByPath(GSettings* client, const char* key,
768 std::vector<std::string>* result) {
[email protected]76722472012-05-24 08:26:46769 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]8c20e3d2011-05-19 21:03:57770 gchar** list = g_settings_get_strv(client, key);
771 if (!list)
772 return false;
773 for (size_t i = 0; list[i]; ++i) {
774 result->push_back(static_cast<char*>(list[i]));
775 g_free(list[i]);
776 }
777 g_free(list);
778 return true;
779 }
780
781 // This is the callback from the debounce timer.
782 void OnDebouncedNotification() {
[email protected]76722472012-05-24 08:26:46783 DCHECK(task_runner_->BelongsToCurrentThread());
[email protected]8c20e3d2011-05-19 21:03:57784 CHECK(notify_delegate_);
785 // Forward to a method on the proxy config service delegate object.
786 notify_delegate_->OnCheckProxyConfigSettings();
787 }
788
789 void OnChangeNotification() {
790 // We don't use Reset() because the timer may not yet be running.
791 // (In that case Stop() is a no-op.)
792 debounce_timer_.Stop();
[email protected]d323a172011-09-02 18:23:02793 debounce_timer_.Start(FROM_HERE,
[email protected]8c20e3d2011-05-19 21:03:57794 base::TimeDelta::FromMilliseconds(kDebounceTimeoutMilliseconds),
795 this, &SettingGetterImplGSettings::OnDebouncedNotification);
796 }
797
798 // gsettings notification callback, dispatched on the default glib main loop.
799 static void OnGSettingsChangeNotification(GSettings* client, gchar* key,
800 gpointer user_data) {
801 VLOG(1) << "gsettings change notification for key " << key;
802 // We don't track which key has changed, just that something did change.
803 SettingGetterImplGSettings* setting_getter =
804 reinterpret_cast<SettingGetterImplGSettings*>(user_data);
805 setting_getter->OnChangeNotification();
806 }
807
808 GSettings* client_;
809 GSettings* http_client_;
810 GSettings* https_client_;
811 GSettings* ftp_client_;
812 GSettings* socks_client_;
813 ProxyConfigServiceLinux::Delegate* notify_delegate_;
814 base::OneShotTimer<SettingGetterImplGSettings> debounce_timer_;
815
[email protected]76722472012-05-24 08:26:46816 // Task runner for the thread that we make gsettings calls on. It should
[email protected]8c20e3d2011-05-19 21:03:57817 // be the UI thread and all our methods should be called on this
818 // thread. Only for assertions.
[email protected]76722472012-05-24 08:26:46819 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
[email protected]8c20e3d2011-05-19 21:03:57820
821 DISALLOW_COPY_AND_ASSIGN(SettingGetterImplGSettings);
822};
823
824bool SettingGetterImplGSettings::LoadAndCheckVersion(
825 base::Environment* env) {
826 // LoadAndCheckVersion() must be called *before* Init()!
827 DCHECK(!client_);
828
829 // The APIs to query gsettings were introduced after the minimum glib
830 // version we target, so we can't link directly against them. We load them
831 // dynamically at runtime, and if they don't exist, return false here. (We
832 // support linking directly via gyp flags though.) Additionally, even when
833 // they are present, we do two additional checks to make sure we should use
834 // them and not gconf. First, we attempt to load the schema for proxy
835 // settings. Second, we check for the program that was used in older
836 // versions of GNOME to configure proxy settings, and return false if it
837 // exists. Some distributions (e.g. Ubuntu 11.04) have the API and schema
838 // but don't use gsettings for proxy settings, but they do have the old
839 // binary, so we detect these systems that way.
840
841#ifdef DLOPEN_GSETTINGS
[email protected]5b5b18a2011-09-22 20:27:16842 gio_handle_ = dlopen("libgio-2.0.so.0", RTLD_NOW | RTLD_GLOBAL);
[email protected]8c20e3d2011-05-19 21:03:57843 if (!gio_handle_) {
[email protected]5b5b18a2011-09-22 20:27:16844 // Try again without .0 at the end; on some systems this may be required.
845 gio_handle_ = dlopen("libgio-2.0.so", RTLD_NOW | RTLD_GLOBAL);
846 if (!gio_handle_) {
847 VLOG(1) << "Cannot load gio library. Will fall back to gconf.";
848 return false;
849 }
[email protected]8c20e3d2011-05-19 21:03:57850 }
851 if (!LoadSymbol("g_settings_new",
852 reinterpret_cast<void**>(&g_settings_new)) ||
853 !LoadSymbol("g_settings_get_child",
854 reinterpret_cast<void**>(&g_settings_get_child)) ||
855 !LoadSymbol("g_settings_get_string",
856 reinterpret_cast<void**>(&g_settings_get_string)) ||
857 !LoadSymbol("g_settings_get_boolean",
858 reinterpret_cast<void**>(&g_settings_get_boolean)) ||
859 !LoadSymbol("g_settings_get_int",
860 reinterpret_cast<void**>(&g_settings_get_int)) ||
861 !LoadSymbol("g_settings_get_strv",
[email protected]4cf80f0b2011-05-20 20:30:26862 reinterpret_cast<void**>(&g_settings_get_strv)) ||
863 !LoadSymbol("g_settings_list_schemas",
864 reinterpret_cast<void**>(&g_settings_list_schemas))) {
[email protected]8c20e3d2011-05-19 21:03:57865 VLOG(1) << "Cannot load gsettings API. Will fall back to gconf.";
866 dlclose(gio_handle_);
867 gio_handle_ = NULL;
868 return false;
869 }
870#endif
871
[email protected]4cf80f0b2011-05-20 20:30:26872 GSettings* client;
873 if (!SchemaExists("org.gnome.system.proxy") ||
874 !(client = g_settings_new("org.gnome.system.proxy"))) {
[email protected]8c20e3d2011-05-19 21:03:57875 VLOG(1) << "Cannot create gsettings client. Will fall back to gconf.";
876 return false;
877 }
878 g_object_unref(client);
879
880 std::string path;
881 if (!env->GetVar("PATH", &path)) {
882 LOG(ERROR) << "No $PATH variable. Assuming no gnome-network-properties.";
883 } else {
884 // Yes, we're on the UI thread. Yes, we're accessing the file system.
885 // Sadly, we don't have much choice. We need the proxy settings and we
886 // need them now, and to figure out where to get them, we have to check
887 // for this binary. See http://crbug.com/69057 for additional details.
888 base::ThreadRestrictions::ScopedAllowIO allow_io;
889 std::vector<std::string> paths;
890 Tokenize(path, ":", &paths);
891 for (size_t i = 0; i < paths.size(); ++i) {
892 FilePath file(paths[i]);
893 if (file_util::PathExists(file.Append("gnome-network-properties"))) {
894 VLOG(1) << "Found gnome-network-properties. Will fall back to gconf.";
895 return false;
896 }
897 }
898 }
899
900 VLOG(1) << "All gsettings tests OK. Will get proxy config from gsettings.";
901 return true;
902}
903#endif // defined(USE_GIO)
904
[email protected]d7395e732009-08-28 23:13:43905// This is the KDE version that reads kioslaverc and simulates gconf.
906// Doing this allows the main Delegate code, as well as the unit tests
907// for it, to stay the same - and the settings map fairly well besides.
[email protected]573c0502011-05-17 22:19:50908class SettingGetterImplKDE : public ProxyConfigServiceLinux::SettingGetter,
909 public base::MessagePumpLibevent::Watcher {
[email protected]d7395e732009-08-28 23:13:43910 public:
[email protected]573c0502011-05-17 22:19:50911 explicit SettingGetterImplKDE(base::Environment* env_var_getter)
[email protected]d7395e732009-08-28 23:13:43912 : inotify_fd_(-1), notify_delegate_(NULL), indirect_manual_(false),
[email protected]a48bf4a2010-06-14 18:24:53913 auto_no_pac_(false), reversed_bypass_list_(false),
[email protected]f18fde22010-05-18 23:49:54914 env_var_getter_(env_var_getter), file_loop_(NULL) {
[email protected]9a8c4022011-01-25 14:25:33915 // This has to be called on the UI thread (http://crbug.com/69057).
916 base::ThreadRestrictions::ScopedAllowIO allow_io;
917
[email protected]f18fde22010-05-18 23:49:54918 // Derive the location of the kde config dir from the environment.
[email protected]92d2dc82010-04-08 17:49:59919 std::string home;
[email protected]3ba7e082010-08-07 02:57:59920 if (env_var_getter->GetVar("KDEHOME", &home) && !home.empty()) {
[email protected]2e8cfe22010-06-12 00:26:24921 // $KDEHOME is set. Use it unconditionally.
[email protected]92d2dc82010-04-08 17:49:59922 kde_config_dir_ = KDEHomeToConfigPath(FilePath(home));
923 } else {
[email protected]2e8cfe22010-06-12 00:26:24924 // $KDEHOME is unset. Try to figure out what to use. This seems to be
[email protected]92d2dc82010-04-08 17:49:59925 // the common case on most distributions.
[email protected]3ba7e082010-08-07 02:57:59926 if (!env_var_getter->GetVar(base::env_vars::kHome, &home))
[email protected]d7395e732009-08-28 23:13:43927 // User has no $HOME? Give up. Later we'll report the failure.
928 return;
[email protected]6b0349ef2010-10-16 04:56:06929 if (base::nix::GetDesktopEnvironment(env_var_getter) ==
930 base::nix::DESKTOP_ENVIRONMENT_KDE3) {
[email protected]92d2dc82010-04-08 17:49:59931 // KDE3 always uses .kde for its configuration.
932 FilePath kde_path = FilePath(home).Append(".kde");
933 kde_config_dir_ = KDEHomeToConfigPath(kde_path);
934 } else {
935 // Some distributions patch KDE4 to use .kde4 instead of .kde, so that
[email protected]fad9c8a52010-06-10 22:30:53936 // both can be installed side-by-side. Sadly they don't all do this, and
937 // they don't always do this: some distributions have started switching
938 // back as well. So if there is a .kde4 directory, check the timestamps
939 // of the config directories within and use the newest one.
[email protected]92d2dc82010-04-08 17:49:59940 // Note that we should currently be running in the UI thread, because in
941 // the gconf version, that is the only thread that can access the proxy
942 // settings (a gconf restriction). As noted below, the initial read of
943 // the proxy settings will be done in this thread anyway, so we check
944 // for .kde4 here in this thread as well.
[email protected]fad9c8a52010-06-10 22:30:53945 FilePath kde3_path = FilePath(home).Append(".kde");
946 FilePath kde3_config = KDEHomeToConfigPath(kde3_path);
[email protected]92d2dc82010-04-08 17:49:59947 FilePath kde4_path = FilePath(home).Append(".kde4");
[email protected]fad9c8a52010-06-10 22:30:53948 FilePath kde4_config = KDEHomeToConfigPath(kde4_path);
949 bool use_kde4 = false;
[email protected]92d2dc82010-04-08 17:49:59950 if (file_util::DirectoryExists(kde4_path)) {
[email protected]2f0193c22010-09-03 02:28:37951 base::PlatformFileInfo kde3_info;
952 base::PlatformFileInfo kde4_info;
[email protected]fad9c8a52010-06-10 22:30:53953 if (file_util::GetFileInfo(kde4_config, &kde4_info)) {
954 if (file_util::GetFileInfo(kde3_config, &kde3_info)) {
955 use_kde4 = kde4_info.last_modified >= kde3_info.last_modified;
956 } else {
957 use_kde4 = true;
958 }
959 }
960 }
961 if (use_kde4) {
[email protected]92d2dc82010-04-08 17:49:59962 kde_config_dir_ = KDEHomeToConfigPath(kde4_path);
963 } else {
[email protected]fad9c8a52010-06-10 22:30:53964 kde_config_dir_ = KDEHomeToConfigPath(kde3_path);
[email protected]92d2dc82010-04-08 17:49:59965 }
966 }
[email protected]d7395e732009-08-28 23:13:43967 }
[email protected]d7395e732009-08-28 23:13:43968 }
969
[email protected]573c0502011-05-17 22:19:50970 virtual ~SettingGetterImplKDE() {
[email protected]d7395e732009-08-28 23:13:43971 // inotify_fd_ should have been closed before now, from
972 // Delegate::OnDestroy(), while running on the file thread. However
973 // on exiting the process, it may happen that Delegate::OnDestroy()
974 // task is left pending on the file loop after the loop was quit,
975 // and pending tasks may then be deleted without being run.
976 // Here in the KDE version, we can safely close the file descriptor
977 // anyway. (Not that it really matters; the process is exiting.)
978 if (inotify_fd_ >= 0)
[email protected]d3066142011-05-10 02:36:20979 ShutDown();
[email protected]d7395e732009-08-28 23:13:43980 DCHECK(inotify_fd_ < 0);
981 }
982
[email protected]76722472012-05-24 08:26:46983 virtual bool Init(base::SingleThreadTaskRunner* glib_thread_task_runner,
[email protected]c4c1b482011-07-22 17:24:26984 MessageLoopForIO* file_loop) OVERRIDE {
[email protected]9a8c4022011-01-25 14:25:33985 // This has to be called on the UI thread (http://crbug.com/69057).
986 base::ThreadRestrictions::ScopedAllowIO allow_io;
[email protected]d7395e732009-08-28 23:13:43987 DCHECK(inotify_fd_ < 0);
988 inotify_fd_ = inotify_init();
989 if (inotify_fd_ < 0) {
[email protected]57b765672009-10-13 18:27:40990 PLOG(ERROR) << "inotify_init failed";
[email protected]d7395e732009-08-28 23:13:43991 return false;
992 }
993 int flags = fcntl(inotify_fd_, F_GETFL);
994 if (fcntl(inotify_fd_, F_SETFL, flags | O_NONBLOCK) < 0) {
[email protected]57b765672009-10-13 18:27:40995 PLOG(ERROR) << "fcntl failed";
[email protected]d7395e732009-08-28 23:13:43996 close(inotify_fd_);
997 inotify_fd_ = -1;
998 return false;
999 }
1000 file_loop_ = file_loop;
1001 // The initial read is done on the current thread, not |file_loop_|,
[email protected]d3066142011-05-10 02:36:201002 // since we will need to have it for SetUpAndFetchInitialConfig().
[email protected]d7395e732009-08-28 23:13:431003 UpdateCachedSettings();
1004 return true;
1005 }
1006
[email protected]749bf5c2012-09-17 03:15:211007 virtual void ShutDown() OVERRIDE {
[email protected]d7395e732009-08-28 23:13:431008 if (inotify_fd_ >= 0) {
1009 ResetCachedSettings();
1010 inotify_watcher_.StopWatchingFileDescriptor();
1011 close(inotify_fd_);
1012 inotify_fd_ = -1;
1013 }
1014 }
1015
[email protected]749bf5c2012-09-17 03:15:211016 virtual bool SetUpNotifications(
1017 ProxyConfigServiceLinux::Delegate* delegate) OVERRIDE {
[email protected]d7395e732009-08-28 23:13:431018 DCHECK(inotify_fd_ >= 0);
[email protected]d3066142011-05-10 02:36:201019 DCHECK(MessageLoop::current() == file_loop_);
[email protected]d7395e732009-08-28 23:13:431020 // We can't just watch the kioslaverc file directly, since KDE will write
1021 // a new copy of it and then rename it whenever settings are changed and
1022 // inotify watches inodes (so we'll be watching the old deleted file after
1023 // the first change, and it will never change again). So, we watch the
1024 // directory instead. We then act only on changes to the kioslaverc entry.
1025 if (inotify_add_watch(inotify_fd_, kde_config_dir_.value().c_str(),
1026 IN_MODIFY | IN_MOVED_TO) < 0)
1027 return false;
1028 notify_delegate_ = delegate;
[email protected]d3066142011-05-10 02:36:201029 if (!file_loop_->WatchFileDescriptor(inotify_fd_, true,
1030 MessageLoopForIO::WATCH_READ, &inotify_watcher_, this))
1031 return false;
1032 // Simulate a change to avoid possibly losing updates before this point.
1033 OnChangeNotification();
1034 return true;
[email protected]d7395e732009-08-28 23:13:431035 }
1036
[email protected]76722472012-05-24 08:26:461037 virtual base::SingleThreadTaskRunner* GetNotificationTaskRunner() OVERRIDE {
[email protected]051e71e2012-06-02 22:13:171038 return file_loop_ ? file_loop_->message_loop_proxy() : NULL;
[email protected]d7395e732009-08-28 23:13:431039 }
1040
[email protected]b160df32012-02-06 20:39:411041 // Implement base::MessagePumpLibevent::Watcher.
[email protected]749bf5c2012-09-17 03:15:211042 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE {
[email protected]d2e6d592012-02-03 21:49:041043 DCHECK_EQ(fd, inotify_fd_);
[email protected]d7395e732009-08-28 23:13:431044 DCHECK(MessageLoop::current() == file_loop_);
1045 OnChangeNotification();
1046 }
[email protected]749bf5c2012-09-17 03:15:211047 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE {
[email protected]d7395e732009-08-28 23:13:431048 NOTREACHED();
1049 }
1050
[email protected]db8ff912012-06-12 23:32:511051 virtual ProxyConfigSource GetConfigSource() OVERRIDE {
1052 return PROXY_CONFIG_SOURCE_KDE;
[email protected]d7395e732009-08-28 23:13:431053 }
1054
[email protected]c4c1b482011-07-22 17:24:261055 virtual bool GetString(StringSetting key, std::string* result) OVERRIDE {
[email protected]d7395e732009-08-28 23:13:431056 string_map_type::iterator it = string_table_.find(key);
1057 if (it == string_table_.end())
1058 return false;
1059 *result = it->second;
1060 return true;
1061 }
[email protected]c4c1b482011-07-22 17:24:261062 virtual bool GetBool(BoolSetting key, bool* result) OVERRIDE {
[email protected]d7395e732009-08-28 23:13:431063 // We don't ever have any booleans.
1064 return false;
1065 }
[email protected]c4c1b482011-07-22 17:24:261066 virtual bool GetInt(IntSetting key, int* result) OVERRIDE {
[email protected]d7395e732009-08-28 23:13:431067 // We don't ever have any integers. (See AddProxy() below about ports.)
1068 return false;
1069 }
[email protected]6b5fe742011-05-20 21:46:481070 virtual bool GetStringList(StringListSetting key,
[email protected]c4c1b482011-07-22 17:24:261071 std::vector<std::string>* result) OVERRIDE {
[email protected]d7395e732009-08-28 23:13:431072 strings_map_type::iterator it = strings_table_.find(key);
1073 if (it == strings_table_.end())
1074 return false;
1075 *result = it->second;
1076 return true;
1077 }
1078
[email protected]c4c1b482011-07-22 17:24:261079 virtual bool BypassListIsReversed() OVERRIDE {
[email protected]a48bf4a2010-06-14 18:24:531080 return reversed_bypass_list_;
1081 }
1082
[email protected]c4c1b482011-07-22 17:24:261083 virtual bool MatchHostsUsingSuffixMatching() OVERRIDE {
[email protected]1a597192010-07-09 16:58:381084 return true;
1085 }
1086
[email protected]d7395e732009-08-28 23:13:431087 private:
1088 void ResetCachedSettings() {
1089 string_table_.clear();
1090 strings_table_.clear();
1091 indirect_manual_ = false;
1092 auto_no_pac_ = false;
[email protected]a48bf4a2010-06-14 18:24:531093 reversed_bypass_list_ = false;
[email protected]d7395e732009-08-28 23:13:431094 }
1095
[email protected]92d2dc82010-04-08 17:49:591096 FilePath KDEHomeToConfigPath(const FilePath& kde_home) {
1097 return kde_home.Append("share").Append("config");
1098 }
1099
[email protected]6b5fe742011-05-20 21:46:481100 void AddProxy(StringSetting host_key, const std::string& value) {
[email protected]d7395e732009-08-28 23:13:431101 if (value.empty() || value.substr(0, 3) == "//:")
1102 // No proxy.
1103 return;
[email protected]4b90c202012-04-24 23:27:551104 size_t space = value.find(' ');
1105 if (space != std::string::npos) {
1106 // Newer versions of KDE use a space rather than a colon to separate the
1107 // port number from the hostname. If we find this, we need to convert it.
1108 std::string fixed = value;
1109 fixed[space] = ':';
1110 string_table_[host_key] = fixed;
1111 } else {
1112 // We don't need to parse the port number out; GetProxyFromSettings()
1113 // would only append it right back again. So we just leave the port
1114 // number right in the host string.
1115 string_table_[host_key] = value;
1116 }
[email protected]d7395e732009-08-28 23:13:431117 }
1118
[email protected]6b5fe742011-05-20 21:46:481119 void AddHostList(StringListSetting key, const std::string& value) {
[email protected]f18fde22010-05-18 23:49:541120 std::vector<std::string> tokens;
[email protected]1a597192010-07-09 16:58:381121 StringTokenizer tk(value, ", ");
[email protected]f18fde22010-05-18 23:49:541122 while (tk.GetNext()) {
1123 std::string token = tk.token();
1124 if (!token.empty())
1125 tokens.push_back(token);
1126 }
1127 strings_table_[key] = tokens;
1128 }
1129
[email protected]9a3d8d42009-09-03 17:01:461130 void AddKDESetting(const std::string& key, const std::string& value) {
[email protected]d7395e732009-08-28 23:13:431131 if (key == "ProxyType") {
1132 const char* mode = "none";
1133 indirect_manual_ = false;
1134 auto_no_pac_ = false;
[email protected]e83326f2010-07-31 17:29:251135 int int_value;
1136 base::StringToInt(value, &int_value);
1137 switch (int_value) {
[email protected]d7395e732009-08-28 23:13:431138 case 0: // No proxy, or maybe kioslaverc syntax error.
1139 break;
1140 case 1: // Manual configuration.
1141 mode = "manual";
1142 break;
1143 case 2: // PAC URL.
1144 mode = "auto";
1145 break;
1146 case 3: // WPAD.
1147 mode = "auto";
1148 auto_no_pac_ = true;
1149 break;
1150 case 4: // Indirect manual via environment variables.
1151 mode = "manual";
1152 indirect_manual_ = true;
1153 break;
1154 }
[email protected]573c0502011-05-17 22:19:501155 string_table_[PROXY_MODE] = mode;
[email protected]d7395e732009-08-28 23:13:431156 } else if (key == "Proxy Config Script") {
[email protected]573c0502011-05-17 22:19:501157 string_table_[PROXY_AUTOCONF_URL] = value;
[email protected]d7395e732009-08-28 23:13:431158 } else if (key == "httpProxy") {
[email protected]573c0502011-05-17 22:19:501159 AddProxy(PROXY_HTTP_HOST, value);
[email protected]d7395e732009-08-28 23:13:431160 } else if (key == "httpsProxy") {
[email protected]573c0502011-05-17 22:19:501161 AddProxy(PROXY_HTTPS_HOST, value);
[email protected]d7395e732009-08-28 23:13:431162 } else if (key == "ftpProxy") {
[email protected]573c0502011-05-17 22:19:501163 AddProxy(PROXY_FTP_HOST, value);
[email protected]bfeb7232012-06-08 00:58:371164 } else if (key == "socksProxy") {
1165 // Older versions of KDE configure SOCKS in a weird way involving
1166 // LD_PRELOAD and a library that intercepts network calls to SOCKSify
1167 // them. We don't support it. KDE 4.8 added a proper SOCKS setting.
1168 AddProxy(PROXY_SOCKS_HOST, value);
[email protected]d7395e732009-08-28 23:13:431169 } else if (key == "ReversedException") {
1170 // We count "true" or any nonzero number as true, otherwise false.
1171 // Note that if the value is not actually numeric StringToInt()
1172 // will return 0, which we count as false.
[email protected]e83326f2010-07-31 17:29:251173 int int_value;
1174 base::StringToInt(value, &int_value);
1175 reversed_bypass_list_ = (value == "true" || int_value);
[email protected]d7395e732009-08-28 23:13:431176 } else if (key == "NoProxyFor") {
[email protected]573c0502011-05-17 22:19:501177 AddHostList(PROXY_IGNORE_HOSTS, value);
[email protected]d7395e732009-08-28 23:13:431178 } else if (key == "AuthMode") {
1179 // Check for authentication, just so we can warn.
[email protected]e83326f2010-07-31 17:29:251180 int mode;
1181 base::StringToInt(value, &mode);
[email protected]d7395e732009-08-28 23:13:431182 if (mode) {
1183 // ProxyConfig does not support authentication parameters, but
1184 // Chrome will prompt for the password later. So we ignore this.
1185 LOG(WARNING) <<
1186 "Proxy authentication parameters ignored, see bug 16709";
1187 }
1188 }
1189 }
1190
[email protected]6b5fe742011-05-20 21:46:481191 void ResolveIndirect(StringSetting key) {
[email protected]d7395e732009-08-28 23:13:431192 string_map_type::iterator it = string_table_.find(key);
1193 if (it != string_table_.end()) {
[email protected]f18fde22010-05-18 23:49:541194 std::string value;
[email protected]3ba7e082010-08-07 02:57:591195 if (env_var_getter_->GetVar(it->second.c_str(), &value))
[email protected]d7395e732009-08-28 23:13:431196 it->second = value;
[email protected]8425adc02010-04-18 17:45:311197 else
1198 string_table_.erase(it);
[email protected]d7395e732009-08-28 23:13:431199 }
1200 }
1201
[email protected]6b5fe742011-05-20 21:46:481202 void ResolveIndirectList(StringListSetting key) {
[email protected]f18fde22010-05-18 23:49:541203 strings_map_type::iterator it = strings_table_.find(key);
1204 if (it != strings_table_.end()) {
1205 std::string value;
1206 if (!it->second.empty() &&
[email protected]3ba7e082010-08-07 02:57:591207 env_var_getter_->GetVar(it->second[0].c_str(), &value))
[email protected]f18fde22010-05-18 23:49:541208 AddHostList(key, value);
1209 else
1210 strings_table_.erase(it);
1211 }
1212 }
1213
[email protected]d7395e732009-08-28 23:13:431214 // The settings in kioslaverc could occur in any order, but some affect
1215 // others. Rather than read the whole file in and then query them in an
1216 // order that allows us to handle that, we read the settings in whatever
1217 // order they occur and do any necessary tweaking after we finish.
1218 void ResolveModeEffects() {
1219 if (indirect_manual_) {
[email protected]573c0502011-05-17 22:19:501220 ResolveIndirect(PROXY_HTTP_HOST);
1221 ResolveIndirect(PROXY_HTTPS_HOST);
1222 ResolveIndirect(PROXY_FTP_HOST);
1223 ResolveIndirectList(PROXY_IGNORE_HOSTS);
[email protected]d7395e732009-08-28 23:13:431224 }
1225 if (auto_no_pac_) {
1226 // Remove the PAC URL; we're not supposed to use it.
[email protected]573c0502011-05-17 22:19:501227 string_table_.erase(PROXY_AUTOCONF_URL);
[email protected]d7395e732009-08-28 23:13:431228 }
[email protected]d7395e732009-08-28 23:13:431229 }
1230
1231 // Reads kioslaverc one line at a time and calls AddKDESetting() to add
1232 // each relevant name-value pair to the appropriate value table.
1233 void UpdateCachedSettings() {
[email protected]92d2dc82010-04-08 17:49:591234 FilePath kioslaverc = kde_config_dir_.Append("kioslaverc");
[email protected]d7395e732009-08-28 23:13:431235 file_util::ScopedFILE input(file_util::OpenFile(kioslaverc, "r"));
1236 if (!input.get())
1237 return;
1238 ResetCachedSettings();
1239 bool in_proxy_settings = false;
1240 bool line_too_long = false;
[email protected]9a3d8d42009-09-03 17:01:461241 char line[BUFFER_SIZE];
1242 // fgets() will return NULL on EOF or error.
[email protected]d7395e732009-08-28 23:13:431243 while (fgets(line, sizeof(line), input.get())) {
1244 // fgets() guarantees the line will be properly terminated.
1245 size_t length = strlen(line);
1246 if (!length)
1247 continue;
1248 // This should be true even with CRLF endings.
1249 if (line[length - 1] != '\n') {
1250 line_too_long = true;
1251 continue;
1252 }
1253 if (line_too_long) {
1254 // The previous line had no line ending, but this done does. This is
1255 // the end of the line that was too long, so warn here and skip it.
1256 LOG(WARNING) << "skipped very long line in " << kioslaverc.value();
1257 line_too_long = false;
1258 continue;
1259 }
1260 // Remove the LF at the end, and the CR if there is one.
1261 line[--length] = '\0';
1262 if (length && line[length - 1] == '\r')
1263 line[--length] = '\0';
1264 // Now parse the line.
1265 if (line[0] == '[') {
1266 // Switching sections. All we care about is whether this is
1267 // the (a?) proxy settings section, for both KDE3 and KDE4.
1268 in_proxy_settings = !strncmp(line, "[Proxy Settings]", 16);
1269 } else if (in_proxy_settings) {
1270 // A regular line, in the (a?) proxy settings section.
[email protected]9a3d8d42009-09-03 17:01:461271 char* split = strchr(line, '=');
1272 // Skip this line if it does not contain an = sign.
1273 if (!split)
[email protected]d7395e732009-08-28 23:13:431274 continue;
[email protected]9a3d8d42009-09-03 17:01:461275 // Split the line on the = and advance |split|.
1276 *(split++) = 0;
1277 std::string key = line;
1278 std::string value = split;
1279 TrimWhitespaceASCII(key, TRIM_ALL, &key);
1280 TrimWhitespaceASCII(value, TRIM_ALL, &value);
1281 // Skip this line if the key name is empty.
1282 if (key.empty())
[email protected]d7395e732009-08-28 23:13:431283 continue;
1284 // Is the value name localized?
[email protected]9a3d8d42009-09-03 17:01:461285 if (key[key.length() - 1] == ']') {
1286 // Find the matching bracket.
1287 length = key.rfind('[');
1288 // Skip this line if the localization indicator is malformed.
1289 if (length == std::string::npos)
[email protected]d7395e732009-08-28 23:13:431290 continue;
1291 // Trim the localization indicator off.
[email protected]9a3d8d42009-09-03 17:01:461292 key.resize(length);
1293 // Remove any resulting trailing whitespace.
1294 TrimWhitespaceASCII(key, TRIM_TRAILING, &key);
1295 // Skip this line if the key name is now empty.
1296 if (key.empty())
1297 continue;
[email protected]d7395e732009-08-28 23:13:431298 }
[email protected]d7395e732009-08-28 23:13:431299 // Now fill in the tables.
[email protected]9a3d8d42009-09-03 17:01:461300 AddKDESetting(key, value);
[email protected]d7395e732009-08-28 23:13:431301 }
1302 }
1303 if (ferror(input.get()))
1304 LOG(ERROR) << "error reading " << kioslaverc.value();
1305 ResolveModeEffects();
1306 }
1307
1308 // This is the callback from the debounce timer.
1309 void OnDebouncedNotification() {
1310 DCHECK(MessageLoop::current() == file_loop_);
[email protected]b30a3f52010-10-16 01:05:461311 VLOG(1) << "inotify change notification for kioslaverc";
[email protected]d7395e732009-08-28 23:13:431312 UpdateCachedSettings();
[email protected]961ac942011-04-28 18:18:141313 CHECK(notify_delegate_);
[email protected]d7395e732009-08-28 23:13:431314 // Forward to a method on the proxy config service delegate object.
1315 notify_delegate_->OnCheckProxyConfigSettings();
1316 }
1317
1318 // Called by OnFileCanReadWithoutBlocking() on the file thread. Reads
1319 // from the inotify file descriptor and starts up a debounce timer if
1320 // an event for kioslaverc is seen.
1321 void OnChangeNotification() {
[email protected]d2e6d592012-02-03 21:49:041322 DCHECK_GE(inotify_fd_, 0);
[email protected]d7395e732009-08-28 23:13:431323 DCHECK(MessageLoop::current() == file_loop_);
1324 char event_buf[(sizeof(inotify_event) + NAME_MAX + 1) * 4];
1325 bool kioslaverc_touched = false;
1326 ssize_t r;
1327 while ((r = read(inotify_fd_, event_buf, sizeof(event_buf))) > 0) {
1328 // inotify returns variable-length structures, which is why we have
1329 // this strange-looking loop instead of iterating through an array.
1330 char* event_ptr = event_buf;
1331 while (event_ptr < event_buf + r) {
1332 inotify_event* event = reinterpret_cast<inotify_event*>(event_ptr);
1333 // The kernel always feeds us whole events.
[email protected]b1f031dd2010-03-02 23:19:331334 CHECK_LE(event_ptr + sizeof(inotify_event), event_buf + r);
1335 CHECK_LE(event->name + event->len, event_buf + r);
[email protected]d7395e732009-08-28 23:13:431336 if (!strcmp(event->name, "kioslaverc"))
1337 kioslaverc_touched = true;
1338 // Advance the pointer just past the end of the filename.
1339 event_ptr = event->name + event->len;
1340 }
1341 // We keep reading even if |kioslaverc_touched| is true to drain the
1342 // inotify event queue.
1343 }
1344 if (!r)
1345 // Instead of returning -1 and setting errno to EINVAL if there is not
1346 // enough buffer space, older kernels (< 2.6.21) return 0. Simulate the
1347 // new behavior (EINVAL) so we can reuse the code below.
1348 errno = EINVAL;
1349 if (errno != EAGAIN) {
[email protected]57b765672009-10-13 18:27:401350 PLOG(WARNING) << "error reading inotify file descriptor";
[email protected]d7395e732009-08-28 23:13:431351 if (errno == EINVAL) {
1352 // Our buffer is not large enough to read the next event. This should
1353 // not happen (because its size is calculated to always be sufficiently
1354 // large), but if it does we'd warn continuously since |inotify_fd_|
1355 // would be forever ready to read. Close it and stop watching instead.
1356 LOG(ERROR) << "inotify failure; no longer watching kioslaverc!";
1357 inotify_watcher_.StopWatchingFileDescriptor();
1358 close(inotify_fd_);
1359 inotify_fd_ = -1;
1360 }
1361 }
1362 if (kioslaverc_touched) {
1363 // We don't use Reset() because the timer may not yet be running.
1364 // (In that case Stop() is a no-op.)
1365 debounce_timer_.Stop();
[email protected]d323a172011-09-02 18:23:021366 debounce_timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(
[email protected]d7395e732009-08-28 23:13:431367 kDebounceTimeoutMilliseconds), this,
[email protected]573c0502011-05-17 22:19:501368 &SettingGetterImplKDE::OnDebouncedNotification);
[email protected]d7395e732009-08-28 23:13:431369 }
1370 }
1371
[email protected]6b5fe742011-05-20 21:46:481372 typedef std::map<StringSetting, std::string> string_map_type;
1373 typedef std::map<StringListSetting,
1374 std::vector<std::string> > strings_map_type;
[email protected]d7395e732009-08-28 23:13:431375
1376 int inotify_fd_;
1377 base::MessagePumpLibevent::FileDescriptorWatcher inotify_watcher_;
1378 ProxyConfigServiceLinux::Delegate* notify_delegate_;
[email protected]573c0502011-05-17 22:19:501379 base::OneShotTimer<SettingGetterImplKDE> debounce_timer_;
[email protected]d7395e732009-08-28 23:13:431380 FilePath kde_config_dir_;
1381 bool indirect_manual_;
1382 bool auto_no_pac_;
[email protected]a48bf4a2010-06-14 18:24:531383 bool reversed_bypass_list_;
[email protected]f18fde22010-05-18 23:49:541384 // We don't own |env_var_getter_|. It's safe to hold a pointer to it, since
1385 // both it and us are owned by ProxyConfigServiceLinux::Delegate, and have the
1386 // same lifetime.
[email protected]76b90d312010-08-03 03:00:501387 base::Environment* env_var_getter_;
[email protected]d7395e732009-08-28 23:13:431388
1389 // We cache these settings whenever we re-read the kioslaverc file.
1390 string_map_type string_table_;
1391 strings_map_type strings_table_;
1392
1393 // Message loop of the file thread, for reading kioslaverc. If NULL,
1394 // just read it directly (for testing). We also handle inotify events
1395 // on this thread.
1396 MessageLoopForIO* file_loop_;
1397
[email protected]573c0502011-05-17 22:19:501398 DISALLOW_COPY_AND_ASSIGN(SettingGetterImplKDE);
[email protected]861c6c62009-04-20 16:50:561399};
1400
1401} // namespace
1402
[email protected]573c0502011-05-17 22:19:501403bool ProxyConfigServiceLinux::Delegate::GetProxyFromSettings(
[email protected]6b5fe742011-05-20 21:46:481404 SettingGetter::StringSetting host_key,
[email protected]573c0502011-05-17 22:19:501405 ProxyServer* result_server) {
[email protected]861c6c62009-04-20 16:50:561406 std::string host;
[email protected]573c0502011-05-17 22:19:501407 if (!setting_getter_->GetString(host_key, &host) || host.empty()) {
[email protected]861c6c62009-04-20 16:50:561408 // Unset or empty.
1409 return false;
1410 }
1411 // Check for an optional port.
[email protected]d7395e732009-08-28 23:13:431412 int port = 0;
[email protected]6b5fe742011-05-20 21:46:481413 SettingGetter::IntSetting port_key =
[email protected]573c0502011-05-17 22:19:501414 SettingGetter::HostSettingToPortSetting(host_key);
1415 setting_getter_->GetInt(port_key, &port);
[email protected]861c6c62009-04-20 16:50:561416 if (port != 0) {
1417 // If a port is set and non-zero:
[email protected]528c56d2010-07-30 19:28:441418 host += ":" + base::IntToString(port);
[email protected]861c6c62009-04-20 16:50:561419 }
[email protected]76960f3d2011-04-30 02:15:231420
[email protected]573c0502011-05-17 22:19:501421 // gconf settings do not appear to distinguish between SOCKS version. We
1422 // default to version 5. For more information on this policy decision, see:
[email protected]76960f3d2011-04-30 02:15:231423 // http://code.google.com/p/chromium/issues/detail?id=55912#c2
[email protected]573c0502011-05-17 22:19:501424 ProxyServer::Scheme scheme = (host_key == SettingGetter::PROXY_SOCKS_HOST) ?
1425 ProxyServer::SCHEME_SOCKS5 : ProxyServer::SCHEME_HTTP;
1426 host = FixupProxyHostScheme(scheme, host);
[email protected]87a102b2009-07-14 05:23:301427 ProxyServer proxy_server = ProxyServer::FromURI(host,
1428 ProxyServer::SCHEME_HTTP);
[email protected]861c6c62009-04-20 16:50:561429 if (proxy_server.is_valid()) {
1430 *result_server = proxy_server;
1431 return true;
1432 }
1433 return false;
1434}
1435
[email protected]573c0502011-05-17 22:19:501436bool ProxyConfigServiceLinux::Delegate::GetConfigFromSettings(
[email protected]3e44697f2009-05-22 14:37:391437 ProxyConfig* config) {
[email protected]861c6c62009-04-20 16:50:561438 std::string mode;
[email protected]573c0502011-05-17 22:19:501439 if (!setting_getter_->GetString(SettingGetter::PROXY_MODE, &mode)) {
[email protected]861c6c62009-04-20 16:50:561440 // We expect this to always be set, so if we don't see it then we
[email protected]573c0502011-05-17 22:19:501441 // probably have a gconf/gsettings problem, and so we don't have a valid
[email protected]861c6c62009-04-20 16:50:561442 // proxy config.
1443 return false;
1444 }
[email protected]3e44697f2009-05-22 14:37:391445 if (mode == "none") {
[email protected]861c6c62009-04-20 16:50:561446 // Specifically specifies no proxy.
1447 return true;
[email protected]3e44697f2009-05-22 14:37:391448 }
[email protected]861c6c62009-04-20 16:50:561449
[email protected]3e44697f2009-05-22 14:37:391450 if (mode == "auto") {
[email protected]aa3ac2cc2012-06-19 00:28:041451 // Automatic proxy config.
[email protected]861c6c62009-04-20 16:50:561452 std::string pac_url_str;
[email protected]573c0502011-05-17 22:19:501453 if (setting_getter_->GetString(SettingGetter::PROXY_AUTOCONF_URL,
1454 &pac_url_str)) {
[email protected]861c6c62009-04-20 16:50:561455 if (!pac_url_str.empty()) {
[email protected]aa3ac2cc2012-06-19 00:28:041456 // If the PAC URL is actually a file path, then put file:// in front.
1457 if (pac_url_str[0] == '/')
1458 pac_url_str = "file://" + pac_url_str;
[email protected]861c6c62009-04-20 16:50:561459 GURL pac_url(pac_url_str);
1460 if (!pac_url.is_valid())
1461 return false;
[email protected]ed4ed0f2010-02-24 00:20:481462 config->set_pac_url(pac_url);
[email protected]861c6c62009-04-20 16:50:561463 return true;
1464 }
1465 }
[email protected]ed4ed0f2010-02-24 00:20:481466 config->set_auto_detect(true);
[email protected]861c6c62009-04-20 16:50:561467 return true;
1468 }
1469
[email protected]3e44697f2009-05-22 14:37:391470 if (mode != "manual") {
[email protected]861c6c62009-04-20 16:50:561471 // Mode is unrecognized.
1472 return false;
1473 }
1474 bool use_http_proxy;
[email protected]573c0502011-05-17 22:19:501475 if (setting_getter_->GetBool(SettingGetter::PROXY_USE_HTTP_PROXY,
1476 &use_http_proxy)
[email protected]861c6c62009-04-20 16:50:561477 && !use_http_proxy) {
1478 // Another master switch for some reason. If set to false, then no
1479 // proxy. But we don't panic if the key doesn't exist.
1480 return true;
1481 }
1482
1483 bool same_proxy = false;
1484 // Indicates to use the http proxy for all protocols. This one may
[email protected]573c0502011-05-17 22:19:501485 // not exist (presumably on older versions); we assume false in that
[email protected]861c6c62009-04-20 16:50:561486 // case.
[email protected]573c0502011-05-17 22:19:501487 setting_getter_->GetBool(SettingGetter::PROXY_USE_SAME_PROXY,
1488 &same_proxy);
[email protected]861c6c62009-04-20 16:50:561489
[email protected]76960f3d2011-04-30 02:15:231490 ProxyServer proxy_for_http;
1491 ProxyServer proxy_for_https;
1492 ProxyServer proxy_for_ftp;
1493 ProxyServer socks_proxy; // (socks)
1494
1495 // This counts how many of the above ProxyServers were defined and valid.
1496 size_t num_proxies_specified = 0;
1497
1498 // Extract the per-scheme proxies. If we failed to parse it, or no proxy was
1499 // specified for the scheme, then the resulting ProxyServer will be invalid.
[email protected]573c0502011-05-17 22:19:501500 if (GetProxyFromSettings(SettingGetter::PROXY_HTTP_HOST, &proxy_for_http))
[email protected]76960f3d2011-04-30 02:15:231501 num_proxies_specified++;
[email protected]573c0502011-05-17 22:19:501502 if (GetProxyFromSettings(SettingGetter::PROXY_HTTPS_HOST, &proxy_for_https))
[email protected]76960f3d2011-04-30 02:15:231503 num_proxies_specified++;
[email protected]573c0502011-05-17 22:19:501504 if (GetProxyFromSettings(SettingGetter::PROXY_FTP_HOST, &proxy_for_ftp))
[email protected]76960f3d2011-04-30 02:15:231505 num_proxies_specified++;
[email protected]573c0502011-05-17 22:19:501506 if (GetProxyFromSettings(SettingGetter::PROXY_SOCKS_HOST, &socks_proxy))
[email protected]76960f3d2011-04-30 02:15:231507 num_proxies_specified++;
1508
1509 if (same_proxy) {
1510 if (proxy_for_http.is_valid()) {
1511 // Use the http proxy for all schemes.
[email protected]ed4ed0f2010-02-24 00:20:481512 config->proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY;
[email protected]76960f3d2011-04-30 02:15:231513 config->proxy_rules().single_proxy = proxy_for_http;
[email protected]861c6c62009-04-20 16:50:561514 }
[email protected]76960f3d2011-04-30 02:15:231515 } else if (num_proxies_specified > 0) {
1516 if (socks_proxy.is_valid() && num_proxies_specified == 1) {
1517 // If the only proxy specified was for SOCKS, use it for all schemes.
1518 config->proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY;
1519 config->proxy_rules().single_proxy = socks_proxy;
[email protected]861c6c62009-04-20 16:50:561520 } else {
[email protected]76960f3d2011-04-30 02:15:231521 // Otherwise use the indicate proxies per-scheme.
1522 config->proxy_rules().type =
1523 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME;
1524 config->proxy_rules().proxy_for_http = proxy_for_http;
1525 config->proxy_rules().proxy_for_https = proxy_for_https;
1526 config->proxy_rules().proxy_for_ftp = proxy_for_ftp;
1527 config->proxy_rules().fallback_proxy = socks_proxy;
[email protected]861c6c62009-04-20 16:50:561528 }
1529 }
1530
[email protected]ed4ed0f2010-02-24 00:20:481531 if (config->proxy_rules().empty()) {
[email protected]861c6c62009-04-20 16:50:561532 // Manual mode but we couldn't parse any rules.
1533 return false;
1534 }
1535
1536 // Check for authentication, just so we can warn.
[email protected]d7395e732009-08-28 23:13:431537 bool use_auth = false;
[email protected]573c0502011-05-17 22:19:501538 setting_getter_->GetBool(SettingGetter::PROXY_USE_AUTHENTICATION,
1539 &use_auth);
[email protected]62749f182009-07-15 13:16:541540 if (use_auth) {
1541 // ProxyConfig does not support authentication parameters, but
1542 // Chrome will prompt for the password later. So we ignore
1543 // /system/http_proxy/*auth* settings.
1544 LOG(WARNING) << "Proxy authentication parameters ignored, see bug 16709";
1545 }
[email protected]861c6c62009-04-20 16:50:561546
1547 // Now the bypass list.
[email protected]7541206c2010-02-19 20:24:061548 std::vector<std::string> ignore_hosts_list;
[email protected]ed4ed0f2010-02-24 00:20:481549 config->proxy_rules().bypass_rules.Clear();
[email protected]573c0502011-05-17 22:19:501550 if (setting_getter_->GetStringList(SettingGetter::PROXY_IGNORE_HOSTS,
1551 &ignore_hosts_list)) {
[email protected]a8185d02010-06-11 00:19:501552 std::vector<std::string>::const_iterator it(ignore_hosts_list.begin());
[email protected]1a597192010-07-09 16:58:381553 for (; it != ignore_hosts_list.end(); ++it) {
[email protected]573c0502011-05-17 22:19:501554 if (setting_getter_->MatchHostsUsingSuffixMatching()) {
[email protected]1a597192010-07-09 16:58:381555 config->proxy_rules().bypass_rules.
1556 AddRuleFromStringUsingSuffixMatching(*it);
1557 } else {
1558 config->proxy_rules().bypass_rules.AddRuleFromString(*it);
1559 }
1560 }
[email protected]a8185d02010-06-11 00:19:501561 }
[email protected]861c6c62009-04-20 16:50:561562 // Note that there are no settings with semantics corresponding to
[email protected]1a597192010-07-09 16:58:381563 // bypass of local names in GNOME. In KDE, "<local>" is supported
1564 // as a hostname rule.
[email protected]861c6c62009-04-20 16:50:561565
[email protected]a48bf4a2010-06-14 18:24:531566 // KDE allows one to reverse the bypass rules.
[email protected]573c0502011-05-17 22:19:501567 config->proxy_rules().reverse_bypass =
1568 setting_getter_->BypassListIsReversed();
[email protected]a48bf4a2010-06-14 18:24:531569
[email protected]861c6c62009-04-20 16:50:561570 return true;
1571}
1572
[email protected]76b90d312010-08-03 03:00:501573ProxyConfigServiceLinux::Delegate::Delegate(base::Environment* env_var_getter)
[email protected]76722472012-05-24 08:26:461574 : env_var_getter_(env_var_getter) {
[email protected]573c0502011-05-17 22:19:501575 // Figure out which SettingGetterImpl to use, if any.
[email protected]6b0349ef2010-10-16 04:56:061576 switch (base::nix::GetDesktopEnvironment(env_var_getter)) {
1577 case base::nix::DESKTOP_ENVIRONMENT_GNOME:
[email protected]9e6c9bde2012-07-17 23:40:171578 case base::nix::DESKTOP_ENVIRONMENT_UNITY:
[email protected]8c20e3d2011-05-19 21:03:571579#if defined(USE_GIO)
1580 {
1581 scoped_ptr<SettingGetterImplGSettings> gs_getter(
1582 new SettingGetterImplGSettings());
1583 // We have to load symbols and check the GNOME version in use to decide
1584 // if we should use the gsettings getter. See LoadAndCheckVersion().
1585 if (gs_getter->LoadAndCheckVersion(env_var_getter))
1586 setting_getter_.reset(gs_getter.release());
1587 }
1588#endif
[email protected]6de53d42010-11-09 07:33:191589#if defined(USE_GCONF)
[email protected]8c20e3d2011-05-19 21:03:571590 // Fall back on gconf if gsettings is unavailable or incorrect.
1591 if (!setting_getter_.get())
1592 setting_getter_.reset(new SettingGetterImplGConf());
[email protected]6de53d42010-11-09 07:33:191593#endif
[email protected]d7395e732009-08-28 23:13:431594 break;
[email protected]6b0349ef2010-10-16 04:56:061595 case base::nix::DESKTOP_ENVIRONMENT_KDE3:
1596 case base::nix::DESKTOP_ENVIRONMENT_KDE4:
[email protected]573c0502011-05-17 22:19:501597 setting_getter_.reset(new SettingGetterImplKDE(env_var_getter));
[email protected]d7395e732009-08-28 23:13:431598 break;
[email protected]6b0349ef2010-10-16 04:56:061599 case base::nix::DESKTOP_ENVIRONMENT_XFCE:
1600 case base::nix::DESKTOP_ENVIRONMENT_OTHER:
[email protected]d7395e732009-08-28 23:13:431601 break;
1602 }
1603}
1604
[email protected]573c0502011-05-17 22:19:501605ProxyConfigServiceLinux::Delegate::Delegate(
1606 base::Environment* env_var_getter, SettingGetter* setting_getter)
[email protected]76722472012-05-24 08:26:461607 : env_var_getter_(env_var_getter), setting_getter_(setting_getter) {
[email protected]861c6c62009-04-20 16:50:561608}
1609
[email protected]d3066142011-05-10 02:36:201610void ProxyConfigServiceLinux::Delegate::SetUpAndFetchInitialConfig(
[email protected]76722472012-05-24 08:26:461611 base::SingleThreadTaskRunner* glib_thread_task_runner,
1612 base::SingleThreadTaskRunner* io_thread_task_runner,
[email protected]d7395e732009-08-28 23:13:431613 MessageLoopForIO* file_loop) {
[email protected]3e44697f2009-05-22 14:37:391614 // We should be running on the default glib main loop thread right
1615 // now. gconf can only be accessed from this thread.
[email protected]76722472012-05-24 08:26:461616 DCHECK(glib_thread_task_runner->BelongsToCurrentThread());
1617 glib_thread_task_runner_ = glib_thread_task_runner;
1618 io_thread_task_runner_ = io_thread_task_runner;
[email protected]3e44697f2009-05-22 14:37:391619
[email protected]76722472012-05-24 08:26:461620 // If we are passed a NULL |io_thread_task_runner| or |file_loop|,
1621 // then we don't set up proxy setting change notifications. This
1622 // should not be the usual case but is intended to simplify test
1623 // setups.
1624 if (!io_thread_task_runner_ || !file_loop)
[email protected]b30a3f52010-10-16 01:05:461625 VLOG(1) << "Monitoring of proxy setting changes is disabled";
[email protected]3e44697f2009-05-22 14:37:391626
1627 // Fetch and cache the current proxy config. The config is left in
[email protected]119655002010-07-23 06:02:401628 // cached_config_, where GetLatestProxyConfig() running on the IO thread
[email protected]3e44697f2009-05-22 14:37:391629 // will expect to find it. This is safe to do because we return
1630 // before this ProxyConfigServiceLinux is passed on to
1631 // the ProxyService.
[email protected]d6cb85b2009-07-23 22:10:531632
1633 // Note: It would be nice to prioritize environment variables
[email protected]92d2dc82010-04-08 17:49:591634 // and only fall back to gconf if env vars were unset. But
[email protected]d6cb85b2009-07-23 22:10:531635 // gnome-terminal "helpfully" sets http_proxy and no_proxy, and it
1636 // does so even if the proxy mode is set to auto, which would
1637 // mislead us.
1638
[email protected]3e44697f2009-05-22 14:37:391639 bool got_config = false;
[email protected]573c0502011-05-17 22:19:501640 if (setting_getter_.get() &&
[email protected]76722472012-05-24 08:26:461641 setting_getter_->Init(glib_thread_task_runner, file_loop) &&
[email protected]573c0502011-05-17 22:19:501642 GetConfigFromSettings(&cached_config_)) {
[email protected]d3066142011-05-10 02:36:201643 cached_config_.set_id(1); // Mark it as valid.
[email protected]db8ff912012-06-12 23:32:511644 cached_config_.set_source(setting_getter_->GetConfigSource());
[email protected]d3066142011-05-10 02:36:201645 VLOG(1) << "Obtained proxy settings from "
[email protected]db8ff912012-06-12 23:32:511646 << ProxyConfigSourceToString(cached_config_.source());
[email protected]d3066142011-05-10 02:36:201647
1648 // If gconf proxy mode is "none", meaning direct, then we take
1649 // that to be a valid config and will not check environment
1650 // variables. The alternative would have been to look for a proxy
1651 // whereever we can find one.
1652 got_config = true;
1653
1654 // Keep a copy of the config for use from this thread for
1655 // comparison with updated settings when we get notifications.
1656 reference_config_ = cached_config_;
1657 reference_config_.set_id(1); // Mark it as valid.
1658
1659 // We only set up notifications if we have IO and file loops available.
1660 // We do this after getting the initial configuration so that we don't have
1661 // to worry about cancelling it if the initial fetch above fails. Note that
1662 // setting up notifications has the side effect of simulating a change, so
1663 // that we won't lose any updates that may have happened after the initial
1664 // fetch and before setting up notifications. We'll detect the common case
1665 // of no changes in OnCheckProxyConfigSettings() (or sooner) and ignore it.
[email protected]76722472012-05-24 08:26:461666 if (io_thread_task_runner && file_loop) {
1667 scoped_refptr<base::SingleThreadTaskRunner> required_loop =
1668 setting_getter_->GetNotificationTaskRunner();
[email protected]434fff82012-04-24 21:13:281669 if (!required_loop || required_loop->BelongsToCurrentThread()) {
[email protected]d3066142011-05-10 02:36:201670 // In this case we are already on an acceptable thread.
1671 SetUpNotifications();
[email protected]d7395e732009-08-28 23:13:431672 } else {
[email protected]d3066142011-05-10 02:36:201673 // Post a task to set up notifications. We don't wait for success.
[email protected]6af889c2011-10-06 23:11:411674 required_loop->PostTask(FROM_HERE, base::Bind(
1675 &ProxyConfigServiceLinux::Delegate::SetUpNotifications, this));
[email protected]d6cb85b2009-07-23 22:10:531676 }
[email protected]d7395e732009-08-28 23:13:431677 }
[email protected]861c6c62009-04-20 16:50:561678 }
[email protected]d6cb85b2009-07-23 22:10:531679
[email protected]3e44697f2009-05-22 14:37:391680 if (!got_config) {
[email protected]d6cb85b2009-07-23 22:10:531681 // We fall back on environment variables.
[email protected]3e44697f2009-05-22 14:37:391682 //
[email protected]d3066142011-05-10 02:36:201683 // Consulting environment variables doesn't need to be done from the
1684 // default glib main loop, but it's a tiny enough amount of work.
[email protected]3e44697f2009-05-22 14:37:391685 if (GetConfigFromEnv(&cached_config_)) {
[email protected]db8ff912012-06-12 23:32:511686 cached_config_.set_source(PROXY_CONFIG_SOURCE_ENV);
[email protected]d3066142011-05-10 02:36:201687 cached_config_.set_id(1); // Mark it as valid.
[email protected]b30a3f52010-10-16 01:05:461688 VLOG(1) << "Obtained proxy settings from environment variables";
[email protected]3e44697f2009-05-22 14:37:391689 }
[email protected]861c6c62009-04-20 16:50:561690 }
[email protected]3e44697f2009-05-22 14:37:391691}
1692
[email protected]573c0502011-05-17 22:19:501693// Depending on the SettingGetter in use, this method will be called
[email protected]d3066142011-05-10 02:36:201694// on either the UI thread (GConf) or the file thread (KDE).
1695void ProxyConfigServiceLinux::Delegate::SetUpNotifications() {
[email protected]76722472012-05-24 08:26:461696 scoped_refptr<base::SingleThreadTaskRunner> required_loop =
1697 setting_getter_->GetNotificationTaskRunner();
[email protected]434fff82012-04-24 21:13:281698 DCHECK(!required_loop || required_loop->BelongsToCurrentThread());
[email protected]573c0502011-05-17 22:19:501699 if (!setting_getter_->SetUpNotifications(this))
[email protected]d3066142011-05-10 02:36:201700 LOG(ERROR) << "Unable to set up proxy configuration change notifications";
1701}
1702
[email protected]119655002010-07-23 06:02:401703void ProxyConfigServiceLinux::Delegate::AddObserver(Observer* observer) {
1704 observers_.AddObserver(observer);
1705}
1706
1707void ProxyConfigServiceLinux::Delegate::RemoveObserver(Observer* observer) {
1708 observers_.RemoveObserver(observer);
1709}
1710
[email protected]3a29593d2011-04-11 10:07:521711ProxyConfigService::ConfigAvailability
1712 ProxyConfigServiceLinux::Delegate::GetLatestProxyConfig(
1713 ProxyConfig* config) {
[email protected]3e44697f2009-05-22 14:37:391714 // This is called from the IO thread.
[email protected]76722472012-05-24 08:26:461715 DCHECK(!io_thread_task_runner_ ||
1716 io_thread_task_runner_->BelongsToCurrentThread());
[email protected]3e44697f2009-05-22 14:37:391717
1718 // Simply return the last proxy configuration that glib_default_loop
1719 // notified us of.
[email protected]db8ff912012-06-12 23:32:511720 if (cached_config_.is_valid()) {
1721 *config = cached_config_;
1722 } else {
1723 *config = ProxyConfig::CreateDirect();
1724 config->set_source(PROXY_CONFIG_SOURCE_SYSTEM_FAILED);
1725 }
[email protected]119655002010-07-23 06:02:401726
[email protected]3a29593d2011-04-11 10:07:521727 // We return CONFIG_VALID to indicate that *config was filled in. It is always
[email protected]119655002010-07-23 06:02:401728 // going to be available since we initialized eagerly on the UI thread.
1729 // TODO(eroman): do lazy initialization instead, so we no longer need
1730 // to construct ProxyConfigServiceLinux on the UI thread.
1731 // In which case, we may return false here.
[email protected]3a29593d2011-04-11 10:07:521732 return CONFIG_VALID;
[email protected]3e44697f2009-05-22 14:37:391733}
1734
[email protected]573c0502011-05-17 22:19:501735// Depending on the SettingGetter in use, this method will be called
[email protected]d7395e732009-08-28 23:13:431736// on either the UI thread (GConf) or the file thread (KDE).
[email protected]3e44697f2009-05-22 14:37:391737void ProxyConfigServiceLinux::Delegate::OnCheckProxyConfigSettings() {
[email protected]76722472012-05-24 08:26:461738 scoped_refptr<base::SingleThreadTaskRunner> required_loop =
1739 setting_getter_->GetNotificationTaskRunner();
[email protected]434fff82012-04-24 21:13:281740 DCHECK(!required_loop || required_loop->BelongsToCurrentThread());
[email protected]3e44697f2009-05-22 14:37:391741 ProxyConfig new_config;
[email protected]573c0502011-05-17 22:19:501742 bool valid = GetConfigFromSettings(&new_config);
[email protected]3e44697f2009-05-22 14:37:391743 if (valid)
1744 new_config.set_id(1); // mark it as valid
1745
[email protected]119655002010-07-23 06:02:401746 // See if it is different from what we had before.
[email protected]3e44697f2009-05-22 14:37:391747 if (new_config.is_valid() != reference_config_.is_valid() ||
1748 !new_config.Equals(reference_config_)) {
[email protected]76722472012-05-24 08:26:461749 // Post a task to the IO thread with the new configuration, so it can
[email protected]3e44697f2009-05-22 14:37:391750 // update |cached_config_|.
[email protected]76722472012-05-24 08:26:461751 io_thread_task_runner_->PostTask(FROM_HERE, base::Bind(
[email protected]6af889c2011-10-06 23:11:411752 &ProxyConfigServiceLinux::Delegate::SetNewProxyConfig,
1753 this, new_config));
[email protected]d1f9d472009-08-13 19:59:301754 // Update the thread-private copy in |reference_config_| as well.
1755 reference_config_ = new_config;
[email protected]d3066142011-05-10 02:36:201756 } else {
1757 VLOG(1) << "Detected no-op change to proxy settings. Doing nothing.";
[email protected]3e44697f2009-05-22 14:37:391758 }
1759}
1760
1761void ProxyConfigServiceLinux::Delegate::SetNewProxyConfig(
1762 const ProxyConfig& new_config) {
[email protected]76722472012-05-24 08:26:461763 DCHECK(io_thread_task_runner_->BelongsToCurrentThread());
[email protected]b30a3f52010-10-16 01:05:461764 VLOG(1) << "Proxy configuration changed";
[email protected]3e44697f2009-05-22 14:37:391765 cached_config_ = new_config;
[email protected]3a29593d2011-04-11 10:07:521766 FOR_EACH_OBSERVER(
1767 Observer, observers_,
1768 OnProxyConfigChanged(new_config, ProxyConfigService::CONFIG_VALID));
[email protected]3e44697f2009-05-22 14:37:391769}
1770
1771void ProxyConfigServiceLinux::Delegate::PostDestroyTask() {
[email protected]573c0502011-05-17 22:19:501772 if (!setting_getter_.get())
[email protected]d7395e732009-08-28 23:13:431773 return;
[email protected]76722472012-05-24 08:26:461774 scoped_refptr<base::SingleThreadTaskRunner> shutdown_loop =
1775 setting_getter_->GetNotificationTaskRunner();
[email protected]434fff82012-04-24 21:13:281776 if (!shutdown_loop || shutdown_loop->BelongsToCurrentThread()) {
[email protected]3e44697f2009-05-22 14:37:391777 // Already on the right thread, call directly.
1778 // This is the case for the unittests.
1779 OnDestroy();
1780 } else {
[email protected]d7395e732009-08-28 23:13:431781 // Post to shutdown thread. Note that on browser shutdown, we may quit
1782 // this MessageLoop and exit the program before ever running this.
[email protected]6af889c2011-10-06 23:11:411783 shutdown_loop->PostTask(FROM_HERE, base::Bind(
1784 &ProxyConfigServiceLinux::Delegate::OnDestroy, this));
[email protected]3e44697f2009-05-22 14:37:391785 }
1786}
1787void ProxyConfigServiceLinux::Delegate::OnDestroy() {
[email protected]76722472012-05-24 08:26:461788 scoped_refptr<base::SingleThreadTaskRunner> shutdown_loop =
1789 setting_getter_->GetNotificationTaskRunner();
[email protected]434fff82012-04-24 21:13:281790 DCHECK(!shutdown_loop || shutdown_loop->BelongsToCurrentThread());
[email protected]573c0502011-05-17 22:19:501791 setting_getter_->ShutDown();
[email protected]3e44697f2009-05-22 14:37:391792}
1793
1794ProxyConfigServiceLinux::ProxyConfigServiceLinux()
[email protected]76b90d312010-08-03 03:00:501795 : delegate_(new Delegate(base::Environment::Create())) {
[email protected]3e44697f2009-05-22 14:37:391796}
1797
[email protected]8e1845e12010-09-15 19:22:241798ProxyConfigServiceLinux::~ProxyConfigServiceLinux() {
1799 delegate_->PostDestroyTask();
1800}
1801
[email protected]3e44697f2009-05-22 14:37:391802ProxyConfigServiceLinux::ProxyConfigServiceLinux(
[email protected]76b90d312010-08-03 03:00:501803 base::Environment* env_var_getter)
[email protected]9a3d8d42009-09-03 17:01:461804 : delegate_(new Delegate(env_var_getter)) {
1805}
1806
1807ProxyConfigServiceLinux::ProxyConfigServiceLinux(
[email protected]573c0502011-05-17 22:19:501808 base::Environment* env_var_getter, SettingGetter* setting_getter)
1809 : delegate_(new Delegate(env_var_getter, setting_getter)) {
[email protected]861c6c62009-04-20 16:50:561810}
1811
[email protected]e4be2dd2010-12-14 00:44:391812void ProxyConfigServiceLinux::AddObserver(Observer* observer) {
1813 delegate_->AddObserver(observer);
1814}
1815
1816void ProxyConfigServiceLinux::RemoveObserver(Observer* observer) {
1817 delegate_->RemoveObserver(observer);
1818}
1819
[email protected]3a29593d2011-04-11 10:07:521820ProxyConfigService::ConfigAvailability
1821 ProxyConfigServiceLinux::GetLatestProxyConfig(ProxyConfig* config) {
[email protected]e4be2dd2010-12-14 00:44:391822 return delegate_->GetLatestProxyConfig(config);
1823}
1824
[email protected]861c6c62009-04-20 16:50:561825} // namespace net