blob: c575584107ff5997d8dafb379f4ebbabb5361e20 [file] [log] [blame]
[email protected]3a29593d2011-04-11 10:07:521// Copyright (c) 2011 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]76b90d312010-08-03 03:00:5026#include "base/environment.h"
[email protected]d7395e732009-08-28 23:13:4327#include "base/file_path.h"
28#include "base/file_util.h"
[email protected]861c6c62009-04-20 16:50:5629#include "base/logging.h"
[email protected]d7395e732009-08-28 23:13:4330#include "base/message_loop.h"
[email protected]3a29593d2011-04-11 10:07:5231#include "base/nix/xdg_util.h"
[email protected]528c56d2010-07-30 19:28:4432#include "base/string_number_conversions.h"
[email protected]861c6c62009-04-20 16:50:5633#include "base/string_tokenizer.h"
34#include "base/string_util.h"
[email protected]3e44697f2009-05-22 14:37:3935#include "base/task.h"
[email protected]9a8c4022011-01-25 14:25:3336#include "base/threading/thread_restrictions.h"
[email protected]d7395e732009-08-28 23:13:4337#include "base/timer.h"
[email protected]861c6c62009-04-20 16:50:5638#include "googleurl/src/url_canon.h"
39#include "net/base/net_errors.h"
40#include "net/http/http_util.h"
41#include "net/proxy/proxy_config.h"
42#include "net/proxy/proxy_server.h"
43
44namespace net {
45
46namespace {
47
[email protected]861c6c62009-04-20 16:50:5648// Given a proxy hostname from a setting, returns that hostname with
49// an appropriate proxy server scheme prefix.
50// scheme indicates the desired proxy scheme: usually http, with
51// socks 4 or 5 as special cases.
[email protected]87a102b2009-07-14 05:23:3052// TODO(arindam): Remove URI string manipulation by using MapUrlSchemeToProxy.
[email protected]861c6c62009-04-20 16:50:5653std::string FixupProxyHostScheme(ProxyServer::Scheme scheme,
54 std::string host) {
[email protected]e8c50812010-09-28 00:16:1755 if (scheme == ProxyServer::SCHEME_SOCKS5 &&
56 StartsWithASCII(host, "socks4://", false)) {
57 // We default to socks 5, but if the user specifically set it to
58 // socks4://, then use that.
59 scheme = ProxyServer::SCHEME_SOCKS4;
[email protected]861c6c62009-04-20 16:50:5660 }
61 // Strip the scheme if any.
62 std::string::size_type colon = host.find("://");
63 if (colon != std::string::npos)
64 host = host.substr(colon + 3);
65 // If a username and perhaps password are specified, give a warning.
66 std::string::size_type at_sign = host.find("@");
67 // Should this be supported?
68 if (at_sign != std::string::npos) {
[email protected]62749f182009-07-15 13:16:5469 // ProxyConfig does not support authentication parameters, but Chrome
70 // will prompt for the password later. Disregard the
71 // authentication parameters and continue with this hostname.
72 LOG(WARNING) << "Proxy authentication parameters ignored, see bug 16709";
[email protected]861c6c62009-04-20 16:50:5673 host = host.substr(at_sign + 1);
74 }
75 // If this is a socks proxy, prepend a scheme so as to tell
76 // ProxyServer. This also allows ProxyServer to choose the right
77 // default port.
78 if (scheme == ProxyServer::SCHEME_SOCKS4)
79 host = "socks4://" + host;
80 else if (scheme == ProxyServer::SCHEME_SOCKS5)
81 host = "socks5://" + host;
[email protected]d7395e732009-08-28 23:13:4382 // If there is a trailing slash, remove it so |host| will parse correctly
83 // even if it includes a port number (since the slash is not numeric).
84 if (host.length() && host[host.length() - 1] == '/')
85 host.resize(host.length() - 1);
[email protected]861c6c62009-04-20 16:50:5686 return host;
87}
88
89} // namespace
90
[email protected]8e1845e12010-09-15 19:22:2491ProxyConfigServiceLinux::Delegate::~Delegate() {
92}
93
[email protected]3e44697f2009-05-22 14:37:3994bool ProxyConfigServiceLinux::Delegate::GetProxyFromEnvVarForScheme(
[email protected]861c6c62009-04-20 16:50:5695 const char* variable, ProxyServer::Scheme scheme,
96 ProxyServer* result_server) {
97 std::string env_value;
[email protected]3ba7e082010-08-07 02:57:5998 if (env_var_getter_->GetVar(variable, &env_value)) {
[email protected]861c6c62009-04-20 16:50:5699 if (!env_value.empty()) {
100 env_value = FixupProxyHostScheme(scheme, env_value);
[email protected]87a102b2009-07-14 05:23:30101 ProxyServer proxy_server =
102 ProxyServer::FromURI(env_value, ProxyServer::SCHEME_HTTP);
[email protected]861c6c62009-04-20 16:50:56103 if (proxy_server.is_valid() && !proxy_server.is_direct()) {
104 *result_server = proxy_server;
105 return true;
106 } else {
[email protected]3e44697f2009-05-22 14:37:39107 LOG(ERROR) << "Failed to parse environment variable " << variable;
[email protected]861c6c62009-04-20 16:50:56108 }
109 }
110 }
111 return false;
112}
113
[email protected]3e44697f2009-05-22 14:37:39114bool ProxyConfigServiceLinux::Delegate::GetProxyFromEnvVar(
[email protected]861c6c62009-04-20 16:50:56115 const char* variable, ProxyServer* result_server) {
116 return GetProxyFromEnvVarForScheme(variable, ProxyServer::SCHEME_HTTP,
117 result_server);
118}
119
[email protected]3e44697f2009-05-22 14:37:39120bool ProxyConfigServiceLinux::Delegate::GetConfigFromEnv(ProxyConfig* config) {
[email protected]861c6c62009-04-20 16:50:56121 // Check for automatic configuration first, in
122 // "auto_proxy". Possibly only the "environment_proxy" firefox
123 // extension has ever used this, but it still sounds like a good
124 // idea.
125 std::string auto_proxy;
[email protected]3ba7e082010-08-07 02:57:59126 if (env_var_getter_->GetVar("auto_proxy", &auto_proxy)) {
[email protected]861c6c62009-04-20 16:50:56127 if (auto_proxy.empty()) {
128 // Defined and empty => autodetect
[email protected]ed4ed0f2010-02-24 00:20:48129 config->set_auto_detect(true);
[email protected]861c6c62009-04-20 16:50:56130 } else {
131 // specified autoconfig URL
[email protected]ed4ed0f2010-02-24 00:20:48132 config->set_pac_url(GURL(auto_proxy));
[email protected]861c6c62009-04-20 16:50:56133 }
134 return true;
135 }
136 // "all_proxy" is a shortcut to avoid defining {http,https,ftp}_proxy.
137 ProxyServer proxy_server;
138 if (GetProxyFromEnvVar("all_proxy", &proxy_server)) {
[email protected]ed4ed0f2010-02-24 00:20:48139 config->proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY;
140 config->proxy_rules().single_proxy = proxy_server;
[email protected]861c6c62009-04-20 16:50:56141 } else {
142 bool have_http = GetProxyFromEnvVar("http_proxy", &proxy_server);
143 if (have_http)
[email protected]ed4ed0f2010-02-24 00:20:48144 config->proxy_rules().proxy_for_http = proxy_server;
[email protected]861c6c62009-04-20 16:50:56145 // It would be tempting to let http_proxy apply for all protocols
146 // if https_proxy and ftp_proxy are not defined. Googling turns up
147 // several documents that mention only http_proxy. But then the
148 // user really might not want to proxy https. And it doesn't seem
149 // like other apps do this. So we will refrain.
150 bool have_https = GetProxyFromEnvVar("https_proxy", &proxy_server);
151 if (have_https)
[email protected]ed4ed0f2010-02-24 00:20:48152 config->proxy_rules().proxy_for_https = proxy_server;
[email protected]861c6c62009-04-20 16:50:56153 bool have_ftp = GetProxyFromEnvVar("ftp_proxy", &proxy_server);
154 if (have_ftp)
[email protected]ed4ed0f2010-02-24 00:20:48155 config->proxy_rules().proxy_for_ftp = proxy_server;
[email protected]861c6c62009-04-20 16:50:56156 if (have_http || have_https || have_ftp) {
157 // mustn't change type unless some rules are actually set.
[email protected]ed4ed0f2010-02-24 00:20:48158 config->proxy_rules().type =
159 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME;
[email protected]861c6c62009-04-20 16:50:56160 }
161 }
[email protected]ed4ed0f2010-02-24 00:20:48162 if (config->proxy_rules().empty()) {
[email protected]861c6c62009-04-20 16:50:56163 // If the above were not defined, try for socks.
[email protected]e8c50812010-09-28 00:16:17164 // For environment variables, we default to version 5, per the gnome
165 // documentation: http://library.gnome.org/devel/gnet/stable/gnet-socks.html
166 ProxyServer::Scheme scheme = ProxyServer::SCHEME_SOCKS5;
[email protected]861c6c62009-04-20 16:50:56167 std::string env_version;
[email protected]3ba7e082010-08-07 02:57:59168 if (env_var_getter_->GetVar("SOCKS_VERSION", &env_version)
[email protected]e8c50812010-09-28 00:16:17169 && env_version == "4")
170 scheme = ProxyServer::SCHEME_SOCKS4;
[email protected]861c6c62009-04-20 16:50:56171 if (GetProxyFromEnvVarForScheme("SOCKS_SERVER", scheme, &proxy_server)) {
[email protected]ed4ed0f2010-02-24 00:20:48172 config->proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY;
173 config->proxy_rules().single_proxy = proxy_server;
[email protected]861c6c62009-04-20 16:50:56174 }
175 }
176 // Look for the proxy bypass list.
177 std::string no_proxy;
[email protected]3ba7e082010-08-07 02:57:59178 env_var_getter_->GetVar("no_proxy", &no_proxy);
[email protected]ed4ed0f2010-02-24 00:20:48179 if (config->proxy_rules().empty()) {
[email protected]861c6c62009-04-20 16:50:56180 // Having only "no_proxy" set, presumably to "*", makes it
181 // explicit that env vars do specify a configuration: having no
182 // rules specified only means the user explicitly asks for direct
183 // connections.
184 return !no_proxy.empty();
185 }
[email protected]7541206c2010-02-19 20:24:06186 // Note that this uses "suffix" matching. So a bypass of "google.com"
187 // is understood to mean a bypass of "*google.com".
[email protected]ed4ed0f2010-02-24 00:20:48188 config->proxy_rules().bypass_rules.ParseFromStringUsingSuffixMatching(
189 no_proxy);
[email protected]861c6c62009-04-20 16:50:56190 return true;
191}
192
193namespace {
194
[email protected]d7395e732009-08-28 23:13:43195const int kDebounceTimeoutMilliseconds = 250;
[email protected]3e44697f2009-05-22 14:37:39196
[email protected]6de53d42010-11-09 07:33:19197#if defined(USE_GCONF)
[email protected]573c0502011-05-17 22:19:50198// This setting getter uses gconf, as used in GNOME 2 and some GNOME 3 desktops.
199class SettingGetterImplGConf : public ProxyConfigServiceLinux::SettingGetter {
[email protected]861c6c62009-04-20 16:50:56200 public:
[email protected]573c0502011-05-17 22:19:50201 SettingGetterImplGConf()
[email protected]d7395e732009-08-28 23:13:43202 : client_(NULL), notify_delegate_(NULL), loop_(NULL) {}
[email protected]3e44697f2009-05-22 14:37:39203
[email protected]573c0502011-05-17 22:19:50204 virtual ~SettingGetterImplGConf() {
[email protected]3e44697f2009-05-22 14:37:39205 // client_ should have been released before now, from
[email protected]f5b13442009-07-13 15:23:59206 // Delegate::OnDestroy(), while running on the UI thread. However
207 // on exiting the process, it may happen that
208 // Delegate::OnDestroy() task is left pending on the glib loop
209 // after the loop was quit, and pending tasks may then be deleted
210 // without being run.
211 if (client_) {
212 // gconf client was not cleaned up.
213 if (MessageLoop::current() == loop_) {
214 // We are on the UI thread so we can clean it safely. This is
215 // the case at least for ui_tests running under Valgrind in
216 // bug 16076.
[email protected]573c0502011-05-17 22:19:50217 VLOG(1) << "~SettingGetterImplGConf: releasing gconf client";
[email protected]d3066142011-05-10 02:36:20218 ShutDown();
[email protected]f5b13442009-07-13 15:23:59219 } else {
[email protected]573c0502011-05-17 22:19:50220 LOG(WARNING) << "~SettingGetterImplGConf: leaking gconf client";
[email protected]f5b13442009-07-13 15:23:59221 client_ = NULL;
222 }
223 }
[email protected]3e44697f2009-05-22 14:37:39224 DCHECK(!client_);
[email protected]861c6c62009-04-20 16:50:56225 }
226
[email protected]d7395e732009-08-28 23:13:43227 virtual bool Init(MessageLoop* glib_default_loop,
228 MessageLoopForIO* file_loop) {
229 DCHECK(MessageLoop::current() == glib_default_loop);
[email protected]3e44697f2009-05-22 14:37:39230 DCHECK(!client_);
231 DCHECK(!loop_);
[email protected]d7395e732009-08-28 23:13:43232 loop_ = glib_default_loop;
[email protected]3e44697f2009-05-22 14:37:39233 client_ = gconf_client_get_default();
[email protected]861c6c62009-04-20 16:50:56234 if (!client_) {
[email protected]861c6c62009-04-20 16:50:56235 // It's not clear whether/when this can return NULL.
[email protected]3e44697f2009-05-22 14:37:39236 LOG(ERROR) << "Unable to create a gconf client";
237 loop_ = NULL;
238 return false;
[email protected]861c6c62009-04-20 16:50:56239 }
[email protected]3e44697f2009-05-22 14:37:39240 GError* error = NULL;
241 // We need to add the directories for which we'll be asking
242 // notifications, and we might as well ask to preload them.
243 gconf_client_add_dir(client_, "/system/proxy",
244 GCONF_CLIENT_PRELOAD_ONELEVEL, &error);
245 if (error == NULL) {
246 gconf_client_add_dir(client_, "/system/http_proxy",
247 GCONF_CLIENT_PRELOAD_ONELEVEL, &error);
248 }
249 if (error != NULL) {
250 LOG(ERROR) << "Error requesting gconf directory: " << error->message;
251 g_error_free(error);
[email protected]d3066142011-05-10 02:36:20252 ShutDown();
[email protected]3e44697f2009-05-22 14:37:39253 return false;
254 }
255 return true;
256 }
257
[email protected]d3066142011-05-10 02:36:20258 void ShutDown() {
[email protected]3e44697f2009-05-22 14:37:39259 if (client_) {
260 DCHECK(MessageLoop::current() == loop_);
261 // This also disables gconf notifications.
262 g_object_unref(client_);
263 client_ = NULL;
264 loop_ = NULL;
265 }
266 }
267
[email protected]d3066142011-05-10 02:36:20268 bool SetUpNotifications(ProxyConfigServiceLinux::Delegate* delegate) {
[email protected]3e44697f2009-05-22 14:37:39269 DCHECK(client_);
270 DCHECK(MessageLoop::current() == loop_);
271 GError* error = NULL;
[email protected]d7395e732009-08-28 23:13:43272 notify_delegate_ = delegate;
[email protected]3e44697f2009-05-22 14:37:39273 gconf_client_notify_add(
274 client_, "/system/proxy",
[email protected]d7395e732009-08-28 23:13:43275 OnGConfChangeNotification, this,
[email protected]3e44697f2009-05-22 14:37:39276 NULL, &error);
277 if (error == NULL) {
278 gconf_client_notify_add(
279 client_, "/system/http_proxy",
[email protected]d7395e732009-08-28 23:13:43280 OnGConfChangeNotification, this,
[email protected]3e44697f2009-05-22 14:37:39281 NULL, &error);
282 }
283 if (error != NULL) {
284 LOG(ERROR) << "Error requesting gconf notifications: " << error->message;
285 g_error_free(error);
[email protected]d3066142011-05-10 02:36:20286 ShutDown();
[email protected]3e44697f2009-05-22 14:37:39287 return false;
288 }
[email protected]d3066142011-05-10 02:36:20289 // Simulate a change to avoid possibly losing updates before this point.
290 OnChangeNotification();
[email protected]3e44697f2009-05-22 14:37:39291 return true;
[email protected]861c6c62009-04-20 16:50:56292 }
293
[email protected]9a3d8d42009-09-03 17:01:46294 virtual MessageLoop* GetNotificationLoop() {
[email protected]d7395e732009-08-28 23:13:43295 return loop_;
296 }
297
298 virtual const char* GetDataSource() {
299 return "gconf";
300 }
301
[email protected]573c0502011-05-17 22:19:50302 virtual bool GetString(Setting key, std::string* result) {
303 switch (key) {
304 case PROXY_MODE:
305 return GetStringByPath("/system/proxy/mode", result);
306 case PROXY_AUTOCONF_URL:
307 return GetStringByPath("/system/proxy/autoconfig_url", result);
308 case PROXY_HTTP_HOST:
309 return GetStringByPath("/system/http_proxy/host", result);
310 case PROXY_HTTPS_HOST:
311 return GetStringByPath("/system/proxy/secure_host", result);
312 case PROXY_FTP_HOST:
313 return GetStringByPath("/system/proxy/ftp_host", result);
314 case PROXY_SOCKS_HOST:
315 return GetStringByPath("/system/proxy/socks_host", result);
316 default:
317 return false;
318 }
319 }
320 virtual bool GetBool(Setting key, bool* result) {
321 switch (key) {
322 case PROXY_USE_HTTP_PROXY:
323 return GetBoolByPath("/system/http_proxy/use_http_proxy", result);
324 case PROXY_USE_SAME_PROXY:
325 return GetBoolByPath("/system/http_proxy/use_same_proxy", result);
326 case PROXY_USE_AUTHENTICATION:
327 return GetBoolByPath("/system/http_proxy/use_authentication", result);
328 default:
329 return false;
330 }
331 }
332 virtual bool GetInt(Setting key, int* result) {
333 switch (key) {
334 case PROXY_HTTP_PORT:
335 return GetIntByPath("/system/http_proxy/port", result);
336 case PROXY_HTTPS_PORT:
337 return GetIntByPath("/system/proxy/secure_port", result);
338 case PROXY_FTP_PORT:
339 return GetIntByPath("/system/proxy/ftp_port", result);
340 case PROXY_SOCKS_PORT:
341 return GetIntByPath("/system/proxy/socks_port", result);
342 default:
343 return false;
344 }
345 }
[email protected]8c20e3d2011-05-19 21:03:57346 virtual bool GetStringList(Setting key, std::vector<std::string>* result) {
[email protected]573c0502011-05-17 22:19:50347 switch (key) {
348 case PROXY_IGNORE_HOSTS:
349 return GetStringListByPath("/system/http_proxy/ignore_hosts", result);
350 default:
351 return false;
352 }
353 }
354
355 virtual bool BypassListIsReversed() {
356 // This is a KDE-specific setting.
357 return false;
358 }
359
360 virtual bool MatchHostsUsingSuffixMatching() {
361 return false;
362 }
363
364 private:
365 bool GetStringByPath(const char* key, std::string* result) {
[email protected]3e44697f2009-05-22 14:37:39366 DCHECK(client_);
367 DCHECK(MessageLoop::current() == loop_);
[email protected]861c6c62009-04-20 16:50:56368 GError* error = NULL;
369 gchar* value = gconf_client_get_string(client_, key, &error);
370 if (HandleGError(error, key))
371 return false;
372 if (!value)
373 return false;
374 *result = value;
375 g_free(value);
376 return true;
377 }
[email protected]573c0502011-05-17 22:19:50378 bool GetBoolByPath(const char* key, bool* result) {
[email protected]3e44697f2009-05-22 14:37:39379 DCHECK(client_);
380 DCHECK(MessageLoop::current() == loop_);
[email protected]861c6c62009-04-20 16:50:56381 GError* error = NULL;
382 // We want to distinguish unset values from values defaulting to
383 // false. For that we need to use the type-generic
384 // gconf_client_get() rather than gconf_client_get_bool().
385 GConfValue* gconf_value = gconf_client_get(client_, key, &error);
386 if (HandleGError(error, key))
387 return false;
388 if (!gconf_value) {
389 // Unset.
390 return false;
391 }
392 if (gconf_value->type != GCONF_VALUE_BOOL) {
393 gconf_value_free(gconf_value);
394 return false;
395 }
396 gboolean bool_value = gconf_value_get_bool(gconf_value);
397 *result = static_cast<bool>(bool_value);
398 gconf_value_free(gconf_value);
399 return true;
400 }
[email protected]573c0502011-05-17 22:19:50401 bool GetIntByPath(const char* key, int* result) {
[email protected]3e44697f2009-05-22 14:37:39402 DCHECK(client_);
403 DCHECK(MessageLoop::current() == loop_);
[email protected]861c6c62009-04-20 16:50:56404 GError* error = NULL;
405 int value = gconf_client_get_int(client_, key, &error);
406 if (HandleGError(error, key))
407 return false;
408 // We don't bother to distinguish an unset value because callers
409 // don't care. 0 is returned if unset.
410 *result = value;
411 return true;
412 }
[email protected]573c0502011-05-17 22:19:50413 bool GetStringListByPath(const char* key, std::vector<std::string>* result) {
[email protected]3e44697f2009-05-22 14:37:39414 DCHECK(client_);
415 DCHECK(MessageLoop::current() == loop_);
[email protected]861c6c62009-04-20 16:50:56416 GError* error = NULL;
417 GSList* list = gconf_client_get_list(client_, key,
418 GCONF_VALUE_STRING, &error);
419 if (HandleGError(error, key))
420 return false;
[email protected]8c20e3d2011-05-19 21:03:57421 if (!list)
[email protected]861c6c62009-04-20 16:50:56422 return false;
[email protected]861c6c62009-04-20 16:50:56423 for (GSList *it = list; it; it = it->next) {
424 result->push_back(static_cast<char*>(it->data));
425 g_free(it->data);
426 }
427 g_slist_free(list);
428 return true;
429 }
430
[email protected]861c6c62009-04-20 16:50:56431 // Logs and frees a glib error. Returns false if there was no error
432 // (error is NULL).
433 bool HandleGError(GError* error, const char* key) {
434 if (error != NULL) {
[email protected]3e44697f2009-05-22 14:37:39435 LOG(ERROR) << "Error getting gconf value for " << key
436 << ": " << error->message;
[email protected]861c6c62009-04-20 16:50:56437 g_error_free(error);
438 return true;
439 }
440 return false;
441 }
442
[email protected]d7395e732009-08-28 23:13:43443 // This is the callback from the debounce timer.
444 void OnDebouncedNotification() {
445 DCHECK(MessageLoop::current() == loop_);
[email protected]961ac942011-04-28 18:18:14446 CHECK(notify_delegate_);
[email protected]d7395e732009-08-28 23:13:43447 // Forward to a method on the proxy config service delegate object.
448 notify_delegate_->OnCheckProxyConfigSettings();
449 }
450
451 void OnChangeNotification() {
452 // We don't use Reset() because the timer may not yet be running.
453 // (In that case Stop() is a no-op.)
454 debounce_timer_.Stop();
[email protected]8c20e3d2011-05-19 21:03:57455 debounce_timer_.Start(
456 base::TimeDelta::FromMilliseconds(kDebounceTimeoutMilliseconds),
457 this, &SettingGetterImplGConf::OnDebouncedNotification);
[email protected]d7395e732009-08-28 23:13:43458 }
459
[email protected]8c20e3d2011-05-19 21:03:57460 // gconf notification callback, dispatched on the default glib main loop.
461 static void OnGConfChangeNotification(GConfClient* client, guint cnxn_id,
462 GConfEntry* entry, gpointer user_data) {
[email protected]b30a3f52010-10-16 01:05:46463 VLOG(1) << "gconf change notification for key "
464 << gconf_entry_get_key(entry);
[email protected]d7395e732009-08-28 23:13:43465 // We don't track which key has changed, just that something did change.
[email protected]573c0502011-05-17 22:19:50466 SettingGetterImplGConf* setting_getter =
467 reinterpret_cast<SettingGetterImplGConf*>(user_data);
[email protected]d7395e732009-08-28 23:13:43468 setting_getter->OnChangeNotification();
469 }
470
[email protected]861c6c62009-04-20 16:50:56471 GConfClient* client_;
[email protected]d7395e732009-08-28 23:13:43472 ProxyConfigServiceLinux::Delegate* notify_delegate_;
[email protected]573c0502011-05-17 22:19:50473 base::OneShotTimer<SettingGetterImplGConf> debounce_timer_;
[email protected]861c6c62009-04-20 16:50:56474
[email protected]3e44697f2009-05-22 14:37:39475 // Message loop of the thread that we make gconf calls on. It should
476 // be the UI thread and all our methods should be called on this
477 // thread. Only for assertions.
478 MessageLoop* loop_;
479
[email protected]573c0502011-05-17 22:19:50480 DISALLOW_COPY_AND_ASSIGN(SettingGetterImplGConf);
[email protected]d7395e732009-08-28 23:13:43481};
[email protected]6de53d42010-11-09 07:33:19482#endif // defined(USE_GCONF)
[email protected]d7395e732009-08-28 23:13:43483
[email protected]8c20e3d2011-05-19 21:03:57484#if defined(USE_GIO)
485// This setting getter uses gsettings, as used in most GNOME 3 desktops.
486class SettingGetterImplGSettings
487 : public ProxyConfigServiceLinux::SettingGetter {
488 public:
489 SettingGetterImplGSettings()
490 : client_(NULL), notify_delegate_(NULL), loop_(NULL) {
491#if defined(DLOPEN_GSETTINGS)
492 gio_handle_ = NULL;
493#endif
494 }
495
496 virtual ~SettingGetterImplGSettings() {
497 // client_ should have been released before now, from
498 // Delegate::OnDestroy(), while running on the UI thread. However
499 // on exiting the process, it may happen that
500 // Delegate::OnDestroy() task is left pending on the glib loop
501 // after the loop was quit, and pending tasks may then be deleted
502 // without being run.
503 if (client_) {
504 // gconf client was not cleaned up.
505 if (MessageLoop::current() == loop_) {
506 // We are on the UI thread so we can clean it safely. This is
507 // the case at least for ui_tests running under Valgrind in
508 // bug 16076.
509 VLOG(1) << "~SettingGetterImplGSettings: releasing gsettings client";
510 ShutDown();
511 } else {
512 LOG(WARNING) << "~SettingGetterImplGSettings: leaking gsettings client";
513 client_ = NULL;
514 }
515 }
516 DCHECK(!client_);
517#if defined(DLOPEN_GSETTINGS)
518 if (gio_handle_) {
519 dlclose(gio_handle_);
520 gio_handle_ = NULL;
521 }
522#endif
523 }
524
525 // LoadAndCheckVersion() must be called *before* Init()!
526 bool LoadAndCheckVersion(base::Environment* env);
527
528 virtual bool Init(MessageLoop* glib_default_loop,
529 MessageLoopForIO* file_loop) {
530 DCHECK(MessageLoop::current() == glib_default_loop);
531 DCHECK(!client_);
532 DCHECK(!loop_);
533 client_ = g_settings_new("org.gnome.system.proxy");
534 if (!client_) {
535 // It's not clear whether/when this can return NULL.
536 LOG(ERROR) << "Unable to create a gsettings client";
537 return false;
538 }
539 loop_ = glib_default_loop;
540 // We assume these all work if the above call worked.
541 http_client_ = g_settings_get_child(client_, "http");
542 https_client_ = g_settings_get_child(client_, "https");
543 ftp_client_ = g_settings_get_child(client_, "ftp");
544 socks_client_ = g_settings_get_child(client_, "socks");
545 DCHECK(http_client_ && https_client_ && ftp_client_ && socks_client_);
546 return true;
547 }
548
549 void ShutDown() {
550 if (client_) {
551 DCHECK(MessageLoop::current() == loop_);
552 // This also disables gsettings notifications.
553 g_object_unref(socks_client_);
554 g_object_unref(ftp_client_);
555 g_object_unref(https_client_);
556 g_object_unref(http_client_);
557 g_object_unref(client_);
558 // We only need to null client_ because it's the only one that we check.
559 client_ = NULL;
560 loop_ = NULL;
561 }
562 }
563
564 bool SetUpNotifications(ProxyConfigServiceLinux::Delegate* delegate) {
565 DCHECK(client_);
566 DCHECK(MessageLoop::current() == loop_);
567 notify_delegate_ = delegate;
568 // We could watch for the change-event signal instead of changed, but
569 // since we have to watch more than one object, we'd still have to
570 // debounce change notifications. This is conceptually simpler.
571 g_signal_connect(G_OBJECT(client_), "changed",
572 G_CALLBACK(OnGSettingsChangeNotification), this);
573 g_signal_connect(G_OBJECT(http_client_), "changed",
574 G_CALLBACK(OnGSettingsChangeNotification), this);
575 g_signal_connect(G_OBJECT(https_client_), "changed",
576 G_CALLBACK(OnGSettingsChangeNotification), this);
577 g_signal_connect(G_OBJECT(ftp_client_), "changed",
578 G_CALLBACK(OnGSettingsChangeNotification), this);
579 g_signal_connect(G_OBJECT(socks_client_), "changed",
580 G_CALLBACK(OnGSettingsChangeNotification), this);
581 // Simulate a change to avoid possibly losing updates before this point.
582 OnChangeNotification();
583 return true;
584 }
585
586 virtual MessageLoop* GetNotificationLoop() {
587 return loop_;
588 }
589
590 virtual const char* GetDataSource() {
591 return "gsettings";
592 }
593
594 virtual bool GetString(Setting key, std::string* result) {
595 DCHECK(client_);
596 switch (key) {
597 case PROXY_MODE:
598 return GetStringByPath(client_, "mode", result);
599 case PROXY_AUTOCONF_URL:
600 return GetStringByPath(client_, "autoconfig-url", result);
601 case PROXY_HTTP_HOST:
602 return GetStringByPath(http_client_, "host", result);
603 case PROXY_HTTPS_HOST:
604 return GetStringByPath(https_client_, "host", result);
605 case PROXY_FTP_HOST:
606 return GetStringByPath(ftp_client_, "host", result);
607 case PROXY_SOCKS_HOST:
608 return GetStringByPath(socks_client_, "host", result);
609 default:
610 return false;
611 }
612 }
613 virtual bool GetBool(Setting key, bool* result) {
614 DCHECK(client_);
615 switch (key) {
616 case PROXY_USE_HTTP_PROXY:
617 // Although there is an "enabled" boolean in http_client_, it is not set
618 // to true by the proxy config utility. We ignore it and return false.
619 return false;
620 case PROXY_USE_SAME_PROXY:
621 // Similarly, although there is a "use-same-proxy" boolean in client_,
622 // it is never set to false by the proxy config utility. We ignore it.
623 return false;
624 case PROXY_USE_AUTHENTICATION:
625 // There is also no way to set this in the proxy config utility, but it
626 // doesn't hurt us to get the actual setting (unlike the two above).
627 return GetBoolByPath(http_client_, "use-authentication", result);
628 default:
629 return false;
630 }
631 }
632 virtual bool GetInt(Setting key, int* result) {
633 DCHECK(client_);
634 switch (key) {
635 case PROXY_HTTP_PORT:
636 return GetIntByPath(http_client_, "port", result);
637 case PROXY_HTTPS_PORT:
638 return GetIntByPath(https_client_, "port", result);
639 case PROXY_FTP_PORT:
640 return GetIntByPath(ftp_client_, "port", result);
641 case PROXY_SOCKS_PORT:
642 return GetIntByPath(socks_client_, "port", result);
643 default:
644 return false;
645 }
646 }
647 virtual bool GetStringList(Setting key, std::vector<std::string>* result) {
648 DCHECK(client_);
649 switch (key) {
650 case PROXY_IGNORE_HOSTS:
651 return GetStringListByPath(client_, "ignore-hosts", result);
652 default:
653 return false;
654 }
655 }
656
657 virtual bool BypassListIsReversed() {
658 // This is a KDE-specific setting.
659 return false;
660 }
661
662 virtual bool MatchHostsUsingSuffixMatching() {
663 return false;
664 }
665
666 private:
667#if defined(DLOPEN_GSETTINGS)
668 // We replicate the prototypes for the g_settings APIs we need. We may not
669 // even be compiling on a system that has them. If we are, these won't
670 // conflict both because they are identical and also due to scoping. The
671 // scoping will also ensure that these get used instead of the global ones.
672 struct _GSettings;
673 typedef struct _GSettings GSettings;
674 GSettings* (*g_settings_new)(const gchar* schema);
675 GSettings* (*g_settings_get_child)(GSettings* settings, const gchar* name);
676 gboolean (*g_settings_get_boolean)(GSettings* settings, const gchar* key);
677 gchar* (*g_settings_get_string)(GSettings* settings, const gchar* key);
678 gint (*g_settings_get_int)(GSettings* settings, const gchar* key);
679 gchar** (*g_settings_get_strv)(GSettings* settings, const gchar* key);
680
681 // The library handle.
682 void* gio_handle_;
683
684 // Load a symbol from |gio_handle_| and store it into |*func_ptr|.
685 bool LoadSymbol(const char* name, void** func_ptr) {
686 dlerror();
687 *func_ptr = dlsym(gio_handle_, name);
688 const char* error = dlerror();
689 if (error) {
690 VLOG(1) << "Unable to load symbol " << name << ": " << error;
691 return false;
692 }
693 return true;
694 }
695#endif // defined(DLOPEN_GSETTINGS)
696
697 bool GetStringByPath(GSettings* client, const char* key,
698 std::string* result) {
699 DCHECK(MessageLoop::current() == loop_);
700 gchar* value = g_settings_get_string(client, key);
701 if (!value)
702 return false;
703 *result = value;
704 g_free(value);
705 return true;
706 }
707 bool GetBoolByPath(GSettings* client, const char* key, bool* result) {
708 DCHECK(MessageLoop::current() == loop_);
709 *result = static_cast<bool>(g_settings_get_boolean(client, key));
710 return true;
711 }
712 bool GetIntByPath(GSettings* client, const char* key, int* result) {
713 DCHECK(MessageLoop::current() == loop_);
714 *result = g_settings_get_int(client, key);
715 return true;
716 }
717 bool GetStringListByPath(GSettings* client, const char* key,
718 std::vector<std::string>* result) {
719 DCHECK(MessageLoop::current() == loop_);
720 gchar** list = g_settings_get_strv(client, key);
721 if (!list)
722 return false;
723 for (size_t i = 0; list[i]; ++i) {
724 result->push_back(static_cast<char*>(list[i]));
725 g_free(list[i]);
726 }
727 g_free(list);
728 return true;
729 }
730
731 // This is the callback from the debounce timer.
732 void OnDebouncedNotification() {
733 DCHECK(MessageLoop::current() == loop_);
734 CHECK(notify_delegate_);
735 // Forward to a method on the proxy config service delegate object.
736 notify_delegate_->OnCheckProxyConfigSettings();
737 }
738
739 void OnChangeNotification() {
740 // We don't use Reset() because the timer may not yet be running.
741 // (In that case Stop() is a no-op.)
742 debounce_timer_.Stop();
743 debounce_timer_.Start(
744 base::TimeDelta::FromMilliseconds(kDebounceTimeoutMilliseconds),
745 this, &SettingGetterImplGSettings::OnDebouncedNotification);
746 }
747
748 // gsettings notification callback, dispatched on the default glib main loop.
749 static void OnGSettingsChangeNotification(GSettings* client, gchar* key,
750 gpointer user_data) {
751 VLOG(1) << "gsettings change notification for key " << key;
752 // We don't track which key has changed, just that something did change.
753 SettingGetterImplGSettings* setting_getter =
754 reinterpret_cast<SettingGetterImplGSettings*>(user_data);
755 setting_getter->OnChangeNotification();
756 }
757
758 GSettings* client_;
759 GSettings* http_client_;
760 GSettings* https_client_;
761 GSettings* ftp_client_;
762 GSettings* socks_client_;
763 ProxyConfigServiceLinux::Delegate* notify_delegate_;
764 base::OneShotTimer<SettingGetterImplGSettings> debounce_timer_;
765
766 // Message loop of the thread that we make gsettings calls on. It should
767 // be the UI thread and all our methods should be called on this
768 // thread. Only for assertions.
769 MessageLoop* loop_;
770
771 DISALLOW_COPY_AND_ASSIGN(SettingGetterImplGSettings);
772};
773
774bool SettingGetterImplGSettings::LoadAndCheckVersion(
775 base::Environment* env) {
776 // LoadAndCheckVersion() must be called *before* Init()!
777 DCHECK(!client_);
778
779 // The APIs to query gsettings were introduced after the minimum glib
780 // version we target, so we can't link directly against them. We load them
781 // dynamically at runtime, and if they don't exist, return false here. (We
782 // support linking directly via gyp flags though.) Additionally, even when
783 // they are present, we do two additional checks to make sure we should use
784 // them and not gconf. First, we attempt to load the schema for proxy
785 // settings. Second, we check for the program that was used in older
786 // versions of GNOME to configure proxy settings, and return false if it
787 // exists. Some distributions (e.g. Ubuntu 11.04) have the API and schema
788 // but don't use gsettings for proxy settings, but they do have the old
789 // binary, so we detect these systems that way.
790
791#ifdef DLOPEN_GSETTINGS
792 gio_handle_ = dlopen("libgio-2.0.so", RTLD_NOW | RTLD_GLOBAL);
793 if (!gio_handle_) {
794 VLOG(1) << "Cannot load gio library. Will fall back to gconf.";
795 return false;
796 }
797 if (!LoadSymbol("g_settings_new",
798 reinterpret_cast<void**>(&g_settings_new)) ||
799 !LoadSymbol("g_settings_get_child",
800 reinterpret_cast<void**>(&g_settings_get_child)) ||
801 !LoadSymbol("g_settings_get_string",
802 reinterpret_cast<void**>(&g_settings_get_string)) ||
803 !LoadSymbol("g_settings_get_boolean",
804 reinterpret_cast<void**>(&g_settings_get_boolean)) ||
805 !LoadSymbol("g_settings_get_int",
806 reinterpret_cast<void**>(&g_settings_get_int)) ||
807 !LoadSymbol("g_settings_get_strv",
808 reinterpret_cast<void**>(&g_settings_get_strv))) {
809 VLOG(1) << "Cannot load gsettings API. Will fall back to gconf.";
810 dlclose(gio_handle_);
811 gio_handle_ = NULL;
812 return false;
813 }
814#endif
815
816 GSettings* client = g_settings_new("org.gnome.system.proxy");
817 if (!client) {
818 VLOG(1) << "Cannot create gsettings client. Will fall back to gconf.";
819 return false;
820 }
821 g_object_unref(client);
822
823 std::string path;
824 if (!env->GetVar("PATH", &path)) {
825 LOG(ERROR) << "No $PATH variable. Assuming no gnome-network-properties.";
826 } else {
827 // Yes, we're on the UI thread. Yes, we're accessing the file system.
828 // Sadly, we don't have much choice. We need the proxy settings and we
829 // need them now, and to figure out where to get them, we have to check
830 // for this binary. See http://crbug.com/69057 for additional details.
831 base::ThreadRestrictions::ScopedAllowIO allow_io;
832 std::vector<std::string> paths;
833 Tokenize(path, ":", &paths);
834 for (size_t i = 0; i < paths.size(); ++i) {
835 FilePath file(paths[i]);
836 if (file_util::PathExists(file.Append("gnome-network-properties"))) {
837 VLOG(1) << "Found gnome-network-properties. Will fall back to gconf.";
838 return false;
839 }
840 }
841 }
842
843 VLOG(1) << "All gsettings tests OK. Will get proxy config from gsettings.";
844 return true;
845}
846#endif // defined(USE_GIO)
847
[email protected]d7395e732009-08-28 23:13:43848// This is the KDE version that reads kioslaverc and simulates gconf.
849// Doing this allows the main Delegate code, as well as the unit tests
850// for it, to stay the same - and the settings map fairly well besides.
[email protected]573c0502011-05-17 22:19:50851class SettingGetterImplKDE : public ProxyConfigServiceLinux::SettingGetter,
852 public base::MessagePumpLibevent::Watcher {
[email protected]d7395e732009-08-28 23:13:43853 public:
[email protected]573c0502011-05-17 22:19:50854 explicit SettingGetterImplKDE(base::Environment* env_var_getter)
[email protected]d7395e732009-08-28 23:13:43855 : inotify_fd_(-1), notify_delegate_(NULL), indirect_manual_(false),
[email protected]a48bf4a2010-06-14 18:24:53856 auto_no_pac_(false), reversed_bypass_list_(false),
[email protected]f18fde22010-05-18 23:49:54857 env_var_getter_(env_var_getter), file_loop_(NULL) {
[email protected]9a8c4022011-01-25 14:25:33858 // This has to be called on the UI thread (http://crbug.com/69057).
859 base::ThreadRestrictions::ScopedAllowIO allow_io;
860
[email protected]f18fde22010-05-18 23:49:54861 // Derive the location of the kde config dir from the environment.
[email protected]92d2dc82010-04-08 17:49:59862 std::string home;
[email protected]3ba7e082010-08-07 02:57:59863 if (env_var_getter->GetVar("KDEHOME", &home) && !home.empty()) {
[email protected]2e8cfe22010-06-12 00:26:24864 // $KDEHOME is set. Use it unconditionally.
[email protected]92d2dc82010-04-08 17:49:59865 kde_config_dir_ = KDEHomeToConfigPath(FilePath(home));
866 } else {
[email protected]2e8cfe22010-06-12 00:26:24867 // $KDEHOME is unset. Try to figure out what to use. This seems to be
[email protected]92d2dc82010-04-08 17:49:59868 // the common case on most distributions.
[email protected]3ba7e082010-08-07 02:57:59869 if (!env_var_getter->GetVar(base::env_vars::kHome, &home))
[email protected]d7395e732009-08-28 23:13:43870 // User has no $HOME? Give up. Later we'll report the failure.
871 return;
[email protected]6b0349ef2010-10-16 04:56:06872 if (base::nix::GetDesktopEnvironment(env_var_getter) ==
873 base::nix::DESKTOP_ENVIRONMENT_KDE3) {
[email protected]92d2dc82010-04-08 17:49:59874 // KDE3 always uses .kde for its configuration.
875 FilePath kde_path = FilePath(home).Append(".kde");
876 kde_config_dir_ = KDEHomeToConfigPath(kde_path);
877 } else {
878 // Some distributions patch KDE4 to use .kde4 instead of .kde, so that
[email protected]fad9c8a52010-06-10 22:30:53879 // both can be installed side-by-side. Sadly they don't all do this, and
880 // they don't always do this: some distributions have started switching
881 // back as well. So if there is a .kde4 directory, check the timestamps
882 // of the config directories within and use the newest one.
[email protected]92d2dc82010-04-08 17:49:59883 // Note that we should currently be running in the UI thread, because in
884 // the gconf version, that is the only thread that can access the proxy
885 // settings (a gconf restriction). As noted below, the initial read of
886 // the proxy settings will be done in this thread anyway, so we check
887 // for .kde4 here in this thread as well.
[email protected]fad9c8a52010-06-10 22:30:53888 FilePath kde3_path = FilePath(home).Append(".kde");
889 FilePath kde3_config = KDEHomeToConfigPath(kde3_path);
[email protected]92d2dc82010-04-08 17:49:59890 FilePath kde4_path = FilePath(home).Append(".kde4");
[email protected]fad9c8a52010-06-10 22:30:53891 FilePath kde4_config = KDEHomeToConfigPath(kde4_path);
892 bool use_kde4 = false;
[email protected]92d2dc82010-04-08 17:49:59893 if (file_util::DirectoryExists(kde4_path)) {
[email protected]2f0193c22010-09-03 02:28:37894 base::PlatformFileInfo kde3_info;
895 base::PlatformFileInfo kde4_info;
[email protected]fad9c8a52010-06-10 22:30:53896 if (file_util::GetFileInfo(kde4_config, &kde4_info)) {
897 if (file_util::GetFileInfo(kde3_config, &kde3_info)) {
898 use_kde4 = kde4_info.last_modified >= kde3_info.last_modified;
899 } else {
900 use_kde4 = true;
901 }
902 }
903 }
904 if (use_kde4) {
[email protected]92d2dc82010-04-08 17:49:59905 kde_config_dir_ = KDEHomeToConfigPath(kde4_path);
906 } else {
[email protected]fad9c8a52010-06-10 22:30:53907 kde_config_dir_ = KDEHomeToConfigPath(kde3_path);
[email protected]92d2dc82010-04-08 17:49:59908 }
909 }
[email protected]d7395e732009-08-28 23:13:43910 }
[email protected]d7395e732009-08-28 23:13:43911 }
912
[email protected]573c0502011-05-17 22:19:50913 virtual ~SettingGetterImplKDE() {
[email protected]d7395e732009-08-28 23:13:43914 // inotify_fd_ should have been closed before now, from
915 // Delegate::OnDestroy(), while running on the file thread. However
916 // on exiting the process, it may happen that Delegate::OnDestroy()
917 // task is left pending on the file loop after the loop was quit,
918 // and pending tasks may then be deleted without being run.
919 // Here in the KDE version, we can safely close the file descriptor
920 // anyway. (Not that it really matters; the process is exiting.)
921 if (inotify_fd_ >= 0)
[email protected]d3066142011-05-10 02:36:20922 ShutDown();
[email protected]d7395e732009-08-28 23:13:43923 DCHECK(inotify_fd_ < 0);
924 }
925
926 virtual bool Init(MessageLoop* glib_default_loop,
927 MessageLoopForIO* file_loop) {
[email protected]9a8c4022011-01-25 14:25:33928 // This has to be called on the UI thread (http://crbug.com/69057).
929 base::ThreadRestrictions::ScopedAllowIO allow_io;
[email protected]d7395e732009-08-28 23:13:43930 DCHECK(inotify_fd_ < 0);
931 inotify_fd_ = inotify_init();
932 if (inotify_fd_ < 0) {
[email protected]57b765672009-10-13 18:27:40933 PLOG(ERROR) << "inotify_init failed";
[email protected]d7395e732009-08-28 23:13:43934 return false;
935 }
936 int flags = fcntl(inotify_fd_, F_GETFL);
937 if (fcntl(inotify_fd_, F_SETFL, flags | O_NONBLOCK) < 0) {
[email protected]57b765672009-10-13 18:27:40938 PLOG(ERROR) << "fcntl failed";
[email protected]d7395e732009-08-28 23:13:43939 close(inotify_fd_);
940 inotify_fd_ = -1;
941 return false;
942 }
943 file_loop_ = file_loop;
944 // The initial read is done on the current thread, not |file_loop_|,
[email protected]d3066142011-05-10 02:36:20945 // since we will need to have it for SetUpAndFetchInitialConfig().
[email protected]d7395e732009-08-28 23:13:43946 UpdateCachedSettings();
947 return true;
948 }
949
[email protected]d3066142011-05-10 02:36:20950 void ShutDown() {
[email protected]d7395e732009-08-28 23:13:43951 if (inotify_fd_ >= 0) {
952 ResetCachedSettings();
953 inotify_watcher_.StopWatchingFileDescriptor();
954 close(inotify_fd_);
955 inotify_fd_ = -1;
956 }
957 }
958
[email protected]d3066142011-05-10 02:36:20959 bool SetUpNotifications(ProxyConfigServiceLinux::Delegate* delegate) {
[email protected]d7395e732009-08-28 23:13:43960 DCHECK(inotify_fd_ >= 0);
[email protected]d3066142011-05-10 02:36:20961 DCHECK(MessageLoop::current() == file_loop_);
[email protected]d7395e732009-08-28 23:13:43962 // We can't just watch the kioslaverc file directly, since KDE will write
963 // a new copy of it and then rename it whenever settings are changed and
964 // inotify watches inodes (so we'll be watching the old deleted file after
965 // the first change, and it will never change again). So, we watch the
966 // directory instead. We then act only on changes to the kioslaverc entry.
967 if (inotify_add_watch(inotify_fd_, kde_config_dir_.value().c_str(),
968 IN_MODIFY | IN_MOVED_TO) < 0)
969 return false;
970 notify_delegate_ = delegate;
[email protected]d3066142011-05-10 02:36:20971 if (!file_loop_->WatchFileDescriptor(inotify_fd_, true,
972 MessageLoopForIO::WATCH_READ, &inotify_watcher_, this))
973 return false;
974 // Simulate a change to avoid possibly losing updates before this point.
975 OnChangeNotification();
976 return true;
[email protected]d7395e732009-08-28 23:13:43977 }
978
[email protected]9a3d8d42009-09-03 17:01:46979 virtual MessageLoop* GetNotificationLoop() {
[email protected]d7395e732009-08-28 23:13:43980 return file_loop_;
981 }
982
983 // Implement base::MessagePumpLibevent::Delegate.
984 void OnFileCanReadWithoutBlocking(int fd) {
985 DCHECK(fd == inotify_fd_);
986 DCHECK(MessageLoop::current() == file_loop_);
987 OnChangeNotification();
988 }
989 void OnFileCanWriteWithoutBlocking(int fd) {
990 NOTREACHED();
991 }
992
993 virtual const char* GetDataSource() {
994 return "KDE";
995 }
996
[email protected]573c0502011-05-17 22:19:50997 virtual bool GetString(Setting key, std::string* result) {
[email protected]d7395e732009-08-28 23:13:43998 string_map_type::iterator it = string_table_.find(key);
999 if (it == string_table_.end())
1000 return false;
1001 *result = it->second;
1002 return true;
1003 }
[email protected]573c0502011-05-17 22:19:501004 virtual bool GetBool(Setting key, bool* result) {
[email protected]d7395e732009-08-28 23:13:431005 // We don't ever have any booleans.
1006 return false;
1007 }
[email protected]573c0502011-05-17 22:19:501008 virtual bool GetInt(Setting key, int* result) {
[email protected]d7395e732009-08-28 23:13:431009 // We don't ever have any integers. (See AddProxy() below about ports.)
1010 return false;
1011 }
[email protected]8c20e3d2011-05-19 21:03:571012 virtual bool GetStringList(Setting key, std::vector<std::string>* result) {
[email protected]d7395e732009-08-28 23:13:431013 strings_map_type::iterator it = strings_table_.find(key);
1014 if (it == strings_table_.end())
1015 return false;
1016 *result = it->second;
1017 return true;
1018 }
1019
[email protected]a48bf4a2010-06-14 18:24:531020 virtual bool BypassListIsReversed() {
1021 return reversed_bypass_list_;
1022 }
1023
[email protected]1a597192010-07-09 16:58:381024 virtual bool MatchHostsUsingSuffixMatching() {
1025 return true;
1026 }
1027
[email protected]d7395e732009-08-28 23:13:431028 private:
1029 void ResetCachedSettings() {
1030 string_table_.clear();
1031 strings_table_.clear();
1032 indirect_manual_ = false;
1033 auto_no_pac_ = false;
[email protected]a48bf4a2010-06-14 18:24:531034 reversed_bypass_list_ = false;
[email protected]d7395e732009-08-28 23:13:431035 }
1036
[email protected]92d2dc82010-04-08 17:49:591037 FilePath KDEHomeToConfigPath(const FilePath& kde_home) {
1038 return kde_home.Append("share").Append("config");
1039 }
1040
[email protected]573c0502011-05-17 22:19:501041 void AddProxy(Setting host_key, const std::string& value) {
[email protected]d7395e732009-08-28 23:13:431042 if (value.empty() || value.substr(0, 3) == "//:")
1043 // No proxy.
1044 return;
[email protected]573c0502011-05-17 22:19:501045 // We don't need to parse the port number out; GetProxyFromSettings()
[email protected]d7395e732009-08-28 23:13:431046 // would only append it right back again. So we just leave the port
1047 // number right in the host string.
[email protected]573c0502011-05-17 22:19:501048 string_table_[host_key] = value;
[email protected]d7395e732009-08-28 23:13:431049 }
1050
[email protected]573c0502011-05-17 22:19:501051 void AddHostList(Setting key, const std::string& value) {
[email protected]f18fde22010-05-18 23:49:541052 std::vector<std::string> tokens;
[email protected]1a597192010-07-09 16:58:381053 StringTokenizer tk(value, ", ");
[email protected]f18fde22010-05-18 23:49:541054 while (tk.GetNext()) {
1055 std::string token = tk.token();
1056 if (!token.empty())
1057 tokens.push_back(token);
1058 }
1059 strings_table_[key] = tokens;
1060 }
1061
[email protected]9a3d8d42009-09-03 17:01:461062 void AddKDESetting(const std::string& key, const std::string& value) {
[email protected]d7395e732009-08-28 23:13:431063 // The astute reader may notice that there is no mention of SOCKS
1064 // here. That's because KDE handles socks is a strange way, and we
1065 // don't support it. Rather than just a setting for the SOCKS server,
1066 // it has a setting for a library to LD_PRELOAD in all your programs
1067 // that will transparently SOCKSify them. Such libraries each have
1068 // their own configuration, and thus, we can't get it from KDE.
1069 if (key == "ProxyType") {
1070 const char* mode = "none";
1071 indirect_manual_ = false;
1072 auto_no_pac_ = false;
[email protected]e83326f2010-07-31 17:29:251073 int int_value;
1074 base::StringToInt(value, &int_value);
1075 switch (int_value) {
[email protected]d7395e732009-08-28 23:13:431076 case 0: // No proxy, or maybe kioslaverc syntax error.
1077 break;
1078 case 1: // Manual configuration.
1079 mode = "manual";
1080 break;
1081 case 2: // PAC URL.
1082 mode = "auto";
1083 break;
1084 case 3: // WPAD.
1085 mode = "auto";
1086 auto_no_pac_ = true;
1087 break;
1088 case 4: // Indirect manual via environment variables.
1089 mode = "manual";
1090 indirect_manual_ = true;
1091 break;
1092 }
[email protected]573c0502011-05-17 22:19:501093 string_table_[PROXY_MODE] = mode;
[email protected]d7395e732009-08-28 23:13:431094 } else if (key == "Proxy Config Script") {
[email protected]573c0502011-05-17 22:19:501095 string_table_[PROXY_AUTOCONF_URL] = value;
[email protected]d7395e732009-08-28 23:13:431096 } else if (key == "httpProxy") {
[email protected]573c0502011-05-17 22:19:501097 AddProxy(PROXY_HTTP_HOST, value);
[email protected]d7395e732009-08-28 23:13:431098 } else if (key == "httpsProxy") {
[email protected]573c0502011-05-17 22:19:501099 AddProxy(PROXY_HTTPS_HOST, value);
[email protected]d7395e732009-08-28 23:13:431100 } else if (key == "ftpProxy") {
[email protected]573c0502011-05-17 22:19:501101 AddProxy(PROXY_FTP_HOST, value);
[email protected]d7395e732009-08-28 23:13:431102 } else if (key == "ReversedException") {
1103 // We count "true" or any nonzero number as true, otherwise false.
1104 // Note that if the value is not actually numeric StringToInt()
1105 // will return 0, which we count as false.
[email protected]e83326f2010-07-31 17:29:251106 int int_value;
1107 base::StringToInt(value, &int_value);
1108 reversed_bypass_list_ = (value == "true" || int_value);
[email protected]d7395e732009-08-28 23:13:431109 } else if (key == "NoProxyFor") {
[email protected]573c0502011-05-17 22:19:501110 AddHostList(PROXY_IGNORE_HOSTS, value);
[email protected]d7395e732009-08-28 23:13:431111 } else if (key == "AuthMode") {
1112 // Check for authentication, just so we can warn.
[email protected]e83326f2010-07-31 17:29:251113 int mode;
1114 base::StringToInt(value, &mode);
[email protected]d7395e732009-08-28 23:13:431115 if (mode) {
1116 // ProxyConfig does not support authentication parameters, but
1117 // Chrome will prompt for the password later. So we ignore this.
1118 LOG(WARNING) <<
1119 "Proxy authentication parameters ignored, see bug 16709";
1120 }
1121 }
1122 }
1123
[email protected]573c0502011-05-17 22:19:501124 void ResolveIndirect(Setting key) {
[email protected]d7395e732009-08-28 23:13:431125 string_map_type::iterator it = string_table_.find(key);
1126 if (it != string_table_.end()) {
[email protected]f18fde22010-05-18 23:49:541127 std::string value;
[email protected]3ba7e082010-08-07 02:57:591128 if (env_var_getter_->GetVar(it->second.c_str(), &value))
[email protected]d7395e732009-08-28 23:13:431129 it->second = value;
[email protected]8425adc02010-04-18 17:45:311130 else
1131 string_table_.erase(it);
[email protected]d7395e732009-08-28 23:13:431132 }
1133 }
1134
[email protected]573c0502011-05-17 22:19:501135 void ResolveIndirectList(Setting key) {
[email protected]f18fde22010-05-18 23:49:541136 strings_map_type::iterator it = strings_table_.find(key);
1137 if (it != strings_table_.end()) {
1138 std::string value;
1139 if (!it->second.empty() &&
[email protected]3ba7e082010-08-07 02:57:591140 env_var_getter_->GetVar(it->second[0].c_str(), &value))
[email protected]f18fde22010-05-18 23:49:541141 AddHostList(key, value);
1142 else
1143 strings_table_.erase(it);
1144 }
1145 }
1146
[email protected]d7395e732009-08-28 23:13:431147 // The settings in kioslaverc could occur in any order, but some affect
1148 // others. Rather than read the whole file in and then query them in an
1149 // order that allows us to handle that, we read the settings in whatever
1150 // order they occur and do any necessary tweaking after we finish.
1151 void ResolveModeEffects() {
1152 if (indirect_manual_) {
[email protected]573c0502011-05-17 22:19:501153 ResolveIndirect(PROXY_HTTP_HOST);
1154 ResolveIndirect(PROXY_HTTPS_HOST);
1155 ResolveIndirect(PROXY_FTP_HOST);
1156 ResolveIndirectList(PROXY_IGNORE_HOSTS);
[email protected]d7395e732009-08-28 23:13:431157 }
1158 if (auto_no_pac_) {
1159 // Remove the PAC URL; we're not supposed to use it.
[email protected]573c0502011-05-17 22:19:501160 string_table_.erase(PROXY_AUTOCONF_URL);
[email protected]d7395e732009-08-28 23:13:431161 }
[email protected]d7395e732009-08-28 23:13:431162 }
1163
1164 // Reads kioslaverc one line at a time and calls AddKDESetting() to add
1165 // each relevant name-value pair to the appropriate value table.
1166 void UpdateCachedSettings() {
[email protected]92d2dc82010-04-08 17:49:591167 FilePath kioslaverc = kde_config_dir_.Append("kioslaverc");
[email protected]d7395e732009-08-28 23:13:431168 file_util::ScopedFILE input(file_util::OpenFile(kioslaverc, "r"));
1169 if (!input.get())
1170 return;
1171 ResetCachedSettings();
1172 bool in_proxy_settings = false;
1173 bool line_too_long = false;
[email protected]9a3d8d42009-09-03 17:01:461174 char line[BUFFER_SIZE];
1175 // fgets() will return NULL on EOF or error.
[email protected]d7395e732009-08-28 23:13:431176 while (fgets(line, sizeof(line), input.get())) {
1177 // fgets() guarantees the line will be properly terminated.
1178 size_t length = strlen(line);
1179 if (!length)
1180 continue;
1181 // This should be true even with CRLF endings.
1182 if (line[length - 1] != '\n') {
1183 line_too_long = true;
1184 continue;
1185 }
1186 if (line_too_long) {
1187 // The previous line had no line ending, but this done does. This is
1188 // the end of the line that was too long, so warn here and skip it.
1189 LOG(WARNING) << "skipped very long line in " << kioslaverc.value();
1190 line_too_long = false;
1191 continue;
1192 }
1193 // Remove the LF at the end, and the CR if there is one.
1194 line[--length] = '\0';
1195 if (length && line[length - 1] == '\r')
1196 line[--length] = '\0';
1197 // Now parse the line.
1198 if (line[0] == '[') {
1199 // Switching sections. All we care about is whether this is
1200 // the (a?) proxy settings section, for both KDE3 and KDE4.
1201 in_proxy_settings = !strncmp(line, "[Proxy Settings]", 16);
1202 } else if (in_proxy_settings) {
1203 // A regular line, in the (a?) proxy settings section.
[email protected]9a3d8d42009-09-03 17:01:461204 char* split = strchr(line, '=');
1205 // Skip this line if it does not contain an = sign.
1206 if (!split)
[email protected]d7395e732009-08-28 23:13:431207 continue;
[email protected]9a3d8d42009-09-03 17:01:461208 // Split the line on the = and advance |split|.
1209 *(split++) = 0;
1210 std::string key = line;
1211 std::string value = split;
1212 TrimWhitespaceASCII(key, TRIM_ALL, &key);
1213 TrimWhitespaceASCII(value, TRIM_ALL, &value);
1214 // Skip this line if the key name is empty.
1215 if (key.empty())
[email protected]d7395e732009-08-28 23:13:431216 continue;
1217 // Is the value name localized?
[email protected]9a3d8d42009-09-03 17:01:461218 if (key[key.length() - 1] == ']') {
1219 // Find the matching bracket.
1220 length = key.rfind('[');
1221 // Skip this line if the localization indicator is malformed.
1222 if (length == std::string::npos)
[email protected]d7395e732009-08-28 23:13:431223 continue;
1224 // Trim the localization indicator off.
[email protected]9a3d8d42009-09-03 17:01:461225 key.resize(length);
1226 // Remove any resulting trailing whitespace.
1227 TrimWhitespaceASCII(key, TRIM_TRAILING, &key);
1228 // Skip this line if the key name is now empty.
1229 if (key.empty())
1230 continue;
[email protected]d7395e732009-08-28 23:13:431231 }
[email protected]d7395e732009-08-28 23:13:431232 // Now fill in the tables.
[email protected]9a3d8d42009-09-03 17:01:461233 AddKDESetting(key, value);
[email protected]d7395e732009-08-28 23:13:431234 }
1235 }
1236 if (ferror(input.get()))
1237 LOG(ERROR) << "error reading " << kioslaverc.value();
1238 ResolveModeEffects();
1239 }
1240
1241 // This is the callback from the debounce timer.
1242 void OnDebouncedNotification() {
1243 DCHECK(MessageLoop::current() == file_loop_);
[email protected]b30a3f52010-10-16 01:05:461244 VLOG(1) << "inotify change notification for kioslaverc";
[email protected]d7395e732009-08-28 23:13:431245 UpdateCachedSettings();
[email protected]961ac942011-04-28 18:18:141246 CHECK(notify_delegate_);
[email protected]d7395e732009-08-28 23:13:431247 // Forward to a method on the proxy config service delegate object.
1248 notify_delegate_->OnCheckProxyConfigSettings();
1249 }
1250
1251 // Called by OnFileCanReadWithoutBlocking() on the file thread. Reads
1252 // from the inotify file descriptor and starts up a debounce timer if
1253 // an event for kioslaverc is seen.
1254 void OnChangeNotification() {
1255 DCHECK(inotify_fd_ >= 0);
1256 DCHECK(MessageLoop::current() == file_loop_);
1257 char event_buf[(sizeof(inotify_event) + NAME_MAX + 1) * 4];
1258 bool kioslaverc_touched = false;
1259 ssize_t r;
1260 while ((r = read(inotify_fd_, event_buf, sizeof(event_buf))) > 0) {
1261 // inotify returns variable-length structures, which is why we have
1262 // this strange-looking loop instead of iterating through an array.
1263 char* event_ptr = event_buf;
1264 while (event_ptr < event_buf + r) {
1265 inotify_event* event = reinterpret_cast<inotify_event*>(event_ptr);
1266 // The kernel always feeds us whole events.
[email protected]b1f031dd2010-03-02 23:19:331267 CHECK_LE(event_ptr + sizeof(inotify_event), event_buf + r);
1268 CHECK_LE(event->name + event->len, event_buf + r);
[email protected]d7395e732009-08-28 23:13:431269 if (!strcmp(event->name, "kioslaverc"))
1270 kioslaverc_touched = true;
1271 // Advance the pointer just past the end of the filename.
1272 event_ptr = event->name + event->len;
1273 }
1274 // We keep reading even if |kioslaverc_touched| is true to drain the
1275 // inotify event queue.
1276 }
1277 if (!r)
1278 // Instead of returning -1 and setting errno to EINVAL if there is not
1279 // enough buffer space, older kernels (< 2.6.21) return 0. Simulate the
1280 // new behavior (EINVAL) so we can reuse the code below.
1281 errno = EINVAL;
1282 if (errno != EAGAIN) {
[email protected]57b765672009-10-13 18:27:401283 PLOG(WARNING) << "error reading inotify file descriptor";
[email protected]d7395e732009-08-28 23:13:431284 if (errno == EINVAL) {
1285 // Our buffer is not large enough to read the next event. This should
1286 // not happen (because its size is calculated to always be sufficiently
1287 // large), but if it does we'd warn continuously since |inotify_fd_|
1288 // would be forever ready to read. Close it and stop watching instead.
1289 LOG(ERROR) << "inotify failure; no longer watching kioslaverc!";
1290 inotify_watcher_.StopWatchingFileDescriptor();
1291 close(inotify_fd_);
1292 inotify_fd_ = -1;
1293 }
1294 }
1295 if (kioslaverc_touched) {
1296 // We don't use Reset() because the timer may not yet be running.
1297 // (In that case Stop() is a no-op.)
1298 debounce_timer_.Stop();
1299 debounce_timer_.Start(base::TimeDelta::FromMilliseconds(
1300 kDebounceTimeoutMilliseconds), this,
[email protected]573c0502011-05-17 22:19:501301 &SettingGetterImplKDE::OnDebouncedNotification);
[email protected]d7395e732009-08-28 23:13:431302 }
1303 }
1304
[email protected]573c0502011-05-17 22:19:501305 typedef std::map<Setting, std::string> string_map_type;
1306 typedef std::map<Setting, std::vector<std::string> > strings_map_type;
[email protected]d7395e732009-08-28 23:13:431307
1308 int inotify_fd_;
1309 base::MessagePumpLibevent::FileDescriptorWatcher inotify_watcher_;
1310 ProxyConfigServiceLinux::Delegate* notify_delegate_;
[email protected]573c0502011-05-17 22:19:501311 base::OneShotTimer<SettingGetterImplKDE> debounce_timer_;
[email protected]d7395e732009-08-28 23:13:431312 FilePath kde_config_dir_;
1313 bool indirect_manual_;
1314 bool auto_no_pac_;
[email protected]a48bf4a2010-06-14 18:24:531315 bool reversed_bypass_list_;
[email protected]f18fde22010-05-18 23:49:541316 // We don't own |env_var_getter_|. It's safe to hold a pointer to it, since
1317 // both it and us are owned by ProxyConfigServiceLinux::Delegate, and have the
1318 // same lifetime.
[email protected]76b90d312010-08-03 03:00:501319 base::Environment* env_var_getter_;
[email protected]d7395e732009-08-28 23:13:431320
1321 // We cache these settings whenever we re-read the kioslaverc file.
1322 string_map_type string_table_;
1323 strings_map_type strings_table_;
1324
1325 // Message loop of the file thread, for reading kioslaverc. If NULL,
1326 // just read it directly (for testing). We also handle inotify events
1327 // on this thread.
1328 MessageLoopForIO* file_loop_;
1329
[email protected]573c0502011-05-17 22:19:501330 DISALLOW_COPY_AND_ASSIGN(SettingGetterImplKDE);
[email protected]861c6c62009-04-20 16:50:561331};
1332
1333} // namespace
1334
[email protected]573c0502011-05-17 22:19:501335bool ProxyConfigServiceLinux::Delegate::GetProxyFromSettings(
1336 SettingGetter::Setting host_key,
1337 ProxyServer* result_server) {
[email protected]861c6c62009-04-20 16:50:561338 std::string host;
[email protected]573c0502011-05-17 22:19:501339 if (!setting_getter_->GetString(host_key, &host) || host.empty()) {
[email protected]861c6c62009-04-20 16:50:561340 // Unset or empty.
1341 return false;
1342 }
1343 // Check for an optional port.
[email protected]d7395e732009-08-28 23:13:431344 int port = 0;
[email protected]573c0502011-05-17 22:19:501345 SettingGetter::Setting port_key =
1346 SettingGetter::HostSettingToPortSetting(host_key);
1347 setting_getter_->GetInt(port_key, &port);
[email protected]861c6c62009-04-20 16:50:561348 if (port != 0) {
1349 // If a port is set and non-zero:
[email protected]528c56d2010-07-30 19:28:441350 host += ":" + base::IntToString(port);
[email protected]861c6c62009-04-20 16:50:561351 }
[email protected]76960f3d2011-04-30 02:15:231352
[email protected]573c0502011-05-17 22:19:501353 // gconf settings do not appear to distinguish between SOCKS version. We
1354 // default to version 5. For more information on this policy decision, see:
[email protected]76960f3d2011-04-30 02:15:231355 // http://code.google.com/p/chromium/issues/detail?id=55912#c2
[email protected]573c0502011-05-17 22:19:501356 ProxyServer::Scheme scheme = (host_key == SettingGetter::PROXY_SOCKS_HOST) ?
1357 ProxyServer::SCHEME_SOCKS5 : ProxyServer::SCHEME_HTTP;
1358 host = FixupProxyHostScheme(scheme, host);
[email protected]87a102b2009-07-14 05:23:301359 ProxyServer proxy_server = ProxyServer::FromURI(host,
1360 ProxyServer::SCHEME_HTTP);
[email protected]861c6c62009-04-20 16:50:561361 if (proxy_server.is_valid()) {
1362 *result_server = proxy_server;
1363 return true;
1364 }
1365 return false;
1366}
1367
[email protected]573c0502011-05-17 22:19:501368bool ProxyConfigServiceLinux::Delegate::GetConfigFromSettings(
[email protected]3e44697f2009-05-22 14:37:391369 ProxyConfig* config) {
[email protected]861c6c62009-04-20 16:50:561370 std::string mode;
[email protected]573c0502011-05-17 22:19:501371 if (!setting_getter_->GetString(SettingGetter::PROXY_MODE, &mode)) {
[email protected]861c6c62009-04-20 16:50:561372 // We expect this to always be set, so if we don't see it then we
[email protected]573c0502011-05-17 22:19:501373 // probably have a gconf/gsettings problem, and so we don't have a valid
[email protected]861c6c62009-04-20 16:50:561374 // proxy config.
1375 return false;
1376 }
[email protected]3e44697f2009-05-22 14:37:391377 if (mode == "none") {
[email protected]861c6c62009-04-20 16:50:561378 // Specifically specifies no proxy.
1379 return true;
[email protected]3e44697f2009-05-22 14:37:391380 }
[email protected]861c6c62009-04-20 16:50:561381
[email protected]3e44697f2009-05-22 14:37:391382 if (mode == "auto") {
[email protected]861c6c62009-04-20 16:50:561383 // automatic proxy config
1384 std::string pac_url_str;
[email protected]573c0502011-05-17 22:19:501385 if (setting_getter_->GetString(SettingGetter::PROXY_AUTOCONF_URL,
1386 &pac_url_str)) {
[email protected]861c6c62009-04-20 16:50:561387 if (!pac_url_str.empty()) {
1388 GURL pac_url(pac_url_str);
1389 if (!pac_url.is_valid())
1390 return false;
[email protected]ed4ed0f2010-02-24 00:20:481391 config->set_pac_url(pac_url);
[email protected]861c6c62009-04-20 16:50:561392 return true;
1393 }
1394 }
[email protected]ed4ed0f2010-02-24 00:20:481395 config->set_auto_detect(true);
[email protected]861c6c62009-04-20 16:50:561396 return true;
1397 }
1398
[email protected]3e44697f2009-05-22 14:37:391399 if (mode != "manual") {
[email protected]861c6c62009-04-20 16:50:561400 // Mode is unrecognized.
1401 return false;
1402 }
1403 bool use_http_proxy;
[email protected]573c0502011-05-17 22:19:501404 if (setting_getter_->GetBool(SettingGetter::PROXY_USE_HTTP_PROXY,
1405 &use_http_proxy)
[email protected]861c6c62009-04-20 16:50:561406 && !use_http_proxy) {
1407 // Another master switch for some reason. If set to false, then no
1408 // proxy. But we don't panic if the key doesn't exist.
1409 return true;
1410 }
1411
1412 bool same_proxy = false;
1413 // Indicates to use the http proxy for all protocols. This one may
[email protected]573c0502011-05-17 22:19:501414 // not exist (presumably on older versions); we assume false in that
[email protected]861c6c62009-04-20 16:50:561415 // case.
[email protected]573c0502011-05-17 22:19:501416 setting_getter_->GetBool(SettingGetter::PROXY_USE_SAME_PROXY,
1417 &same_proxy);
[email protected]861c6c62009-04-20 16:50:561418
[email protected]76960f3d2011-04-30 02:15:231419 ProxyServer proxy_for_http;
1420 ProxyServer proxy_for_https;
1421 ProxyServer proxy_for_ftp;
1422 ProxyServer socks_proxy; // (socks)
1423
1424 // This counts how many of the above ProxyServers were defined and valid.
1425 size_t num_proxies_specified = 0;
1426
1427 // Extract the per-scheme proxies. If we failed to parse it, or no proxy was
1428 // specified for the scheme, then the resulting ProxyServer will be invalid.
[email protected]573c0502011-05-17 22:19:501429 if (GetProxyFromSettings(SettingGetter::PROXY_HTTP_HOST, &proxy_for_http))
[email protected]76960f3d2011-04-30 02:15:231430 num_proxies_specified++;
[email protected]573c0502011-05-17 22:19:501431 if (GetProxyFromSettings(SettingGetter::PROXY_HTTPS_HOST, &proxy_for_https))
[email protected]76960f3d2011-04-30 02:15:231432 num_proxies_specified++;
[email protected]573c0502011-05-17 22:19:501433 if (GetProxyFromSettings(SettingGetter::PROXY_FTP_HOST, &proxy_for_ftp))
[email protected]76960f3d2011-04-30 02:15:231434 num_proxies_specified++;
[email protected]573c0502011-05-17 22:19:501435 if (GetProxyFromSettings(SettingGetter::PROXY_SOCKS_HOST, &socks_proxy))
[email protected]76960f3d2011-04-30 02:15:231436 num_proxies_specified++;
1437
1438 if (same_proxy) {
1439 if (proxy_for_http.is_valid()) {
1440 // Use the http proxy for all schemes.
[email protected]ed4ed0f2010-02-24 00:20:481441 config->proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY;
[email protected]76960f3d2011-04-30 02:15:231442 config->proxy_rules().single_proxy = proxy_for_http;
[email protected]861c6c62009-04-20 16:50:561443 }
[email protected]76960f3d2011-04-30 02:15:231444 } else if (num_proxies_specified > 0) {
1445 if (socks_proxy.is_valid() && num_proxies_specified == 1) {
1446 // If the only proxy specified was for SOCKS, use it for all schemes.
1447 config->proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY;
1448 config->proxy_rules().single_proxy = socks_proxy;
[email protected]861c6c62009-04-20 16:50:561449 } else {
[email protected]76960f3d2011-04-30 02:15:231450 // Otherwise use the indicate proxies per-scheme.
1451 config->proxy_rules().type =
1452 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME;
1453 config->proxy_rules().proxy_for_http = proxy_for_http;
1454 config->proxy_rules().proxy_for_https = proxy_for_https;
1455 config->proxy_rules().proxy_for_ftp = proxy_for_ftp;
1456 config->proxy_rules().fallback_proxy = socks_proxy;
[email protected]861c6c62009-04-20 16:50:561457 }
1458 }
1459
[email protected]ed4ed0f2010-02-24 00:20:481460 if (config->proxy_rules().empty()) {
[email protected]861c6c62009-04-20 16:50:561461 // Manual mode but we couldn't parse any rules.
1462 return false;
1463 }
1464
1465 // Check for authentication, just so we can warn.
[email protected]d7395e732009-08-28 23:13:431466 bool use_auth = false;
[email protected]573c0502011-05-17 22:19:501467 setting_getter_->GetBool(SettingGetter::PROXY_USE_AUTHENTICATION,
1468 &use_auth);
[email protected]62749f182009-07-15 13:16:541469 if (use_auth) {
1470 // ProxyConfig does not support authentication parameters, but
1471 // Chrome will prompt for the password later. So we ignore
1472 // /system/http_proxy/*auth* settings.
1473 LOG(WARNING) << "Proxy authentication parameters ignored, see bug 16709";
1474 }
[email protected]861c6c62009-04-20 16:50:561475
1476 // Now the bypass list.
[email protected]7541206c2010-02-19 20:24:061477 std::vector<std::string> ignore_hosts_list;
[email protected]ed4ed0f2010-02-24 00:20:481478 config->proxy_rules().bypass_rules.Clear();
[email protected]573c0502011-05-17 22:19:501479 if (setting_getter_->GetStringList(SettingGetter::PROXY_IGNORE_HOSTS,
1480 &ignore_hosts_list)) {
[email protected]a8185d02010-06-11 00:19:501481 std::vector<std::string>::const_iterator it(ignore_hosts_list.begin());
[email protected]1a597192010-07-09 16:58:381482 for (; it != ignore_hosts_list.end(); ++it) {
[email protected]573c0502011-05-17 22:19:501483 if (setting_getter_->MatchHostsUsingSuffixMatching()) {
[email protected]1a597192010-07-09 16:58:381484 config->proxy_rules().bypass_rules.
1485 AddRuleFromStringUsingSuffixMatching(*it);
1486 } else {
1487 config->proxy_rules().bypass_rules.AddRuleFromString(*it);
1488 }
1489 }
[email protected]a8185d02010-06-11 00:19:501490 }
[email protected]861c6c62009-04-20 16:50:561491 // Note that there are no settings with semantics corresponding to
[email protected]1a597192010-07-09 16:58:381492 // bypass of local names in GNOME. In KDE, "<local>" is supported
1493 // as a hostname rule.
[email protected]861c6c62009-04-20 16:50:561494
[email protected]a48bf4a2010-06-14 18:24:531495 // KDE allows one to reverse the bypass rules.
[email protected]573c0502011-05-17 22:19:501496 config->proxy_rules().reverse_bypass =
1497 setting_getter_->BypassListIsReversed();
[email protected]a48bf4a2010-06-14 18:24:531498
[email protected]861c6c62009-04-20 16:50:561499 return true;
1500}
1501
[email protected]76b90d312010-08-03 03:00:501502ProxyConfigServiceLinux::Delegate::Delegate(base::Environment* env_var_getter)
[email protected]d7395e732009-08-28 23:13:431503 : env_var_getter_(env_var_getter),
1504 glib_default_loop_(NULL), io_loop_(NULL) {
[email protected]573c0502011-05-17 22:19:501505 // Figure out which SettingGetterImpl to use, if any.
[email protected]6b0349ef2010-10-16 04:56:061506 switch (base::nix::GetDesktopEnvironment(env_var_getter)) {
1507 case base::nix::DESKTOP_ENVIRONMENT_GNOME:
[email protected]8c20e3d2011-05-19 21:03:571508#if defined(USE_GIO)
1509 {
1510 scoped_ptr<SettingGetterImplGSettings> gs_getter(
1511 new SettingGetterImplGSettings());
1512 // We have to load symbols and check the GNOME version in use to decide
1513 // if we should use the gsettings getter. See LoadAndCheckVersion().
1514 if (gs_getter->LoadAndCheckVersion(env_var_getter))
1515 setting_getter_.reset(gs_getter.release());
1516 }
1517#endif
[email protected]6de53d42010-11-09 07:33:191518#if defined(USE_GCONF)
[email protected]8c20e3d2011-05-19 21:03:571519 // Fall back on gconf if gsettings is unavailable or incorrect.
1520 if (!setting_getter_.get())
1521 setting_getter_.reset(new SettingGetterImplGConf());
[email protected]6de53d42010-11-09 07:33:191522#endif
[email protected]d7395e732009-08-28 23:13:431523 break;
[email protected]6b0349ef2010-10-16 04:56:061524 case base::nix::DESKTOP_ENVIRONMENT_KDE3:
1525 case base::nix::DESKTOP_ENVIRONMENT_KDE4:
[email protected]573c0502011-05-17 22:19:501526 setting_getter_.reset(new SettingGetterImplKDE(env_var_getter));
[email protected]d7395e732009-08-28 23:13:431527 break;
[email protected]6b0349ef2010-10-16 04:56:061528 case base::nix::DESKTOP_ENVIRONMENT_XFCE:
1529 case base::nix::DESKTOP_ENVIRONMENT_OTHER:
[email protected]d7395e732009-08-28 23:13:431530 break;
1531 }
1532}
1533
[email protected]573c0502011-05-17 22:19:501534ProxyConfigServiceLinux::Delegate::Delegate(
1535 base::Environment* env_var_getter, SettingGetter* setting_getter)
1536 : env_var_getter_(env_var_getter), setting_getter_(setting_getter),
[email protected]3e44697f2009-05-22 14:37:391537 glib_default_loop_(NULL), io_loop_(NULL) {
[email protected]861c6c62009-04-20 16:50:561538}
1539
[email protected]d3066142011-05-10 02:36:201540void ProxyConfigServiceLinux::Delegate::SetUpAndFetchInitialConfig(
[email protected]d7395e732009-08-28 23:13:431541 MessageLoop* glib_default_loop, MessageLoop* io_loop,
1542 MessageLoopForIO* file_loop) {
[email protected]3e44697f2009-05-22 14:37:391543 // We should be running on the default glib main loop thread right
1544 // now. gconf can only be accessed from this thread.
1545 DCHECK(MessageLoop::current() == glib_default_loop);
1546 glib_default_loop_ = glib_default_loop;
1547 io_loop_ = io_loop;
1548
[email protected]d7395e732009-08-28 23:13:431549 // If we are passed a NULL io_loop or file_loop, then we don't set up
1550 // proxy setting change notifications. This should not be the usual
1551 // case but is intended to simplify test setups.
1552 if (!io_loop_ || !file_loop)
[email protected]b30a3f52010-10-16 01:05:461553 VLOG(1) << "Monitoring of proxy setting changes is disabled";
[email protected]3e44697f2009-05-22 14:37:391554
1555 // Fetch and cache the current proxy config. The config is left in
[email protected]119655002010-07-23 06:02:401556 // cached_config_, where GetLatestProxyConfig() running on the IO thread
[email protected]3e44697f2009-05-22 14:37:391557 // will expect to find it. This is safe to do because we return
1558 // before this ProxyConfigServiceLinux is passed on to
1559 // the ProxyService.
[email protected]d6cb85b2009-07-23 22:10:531560
1561 // Note: It would be nice to prioritize environment variables
[email protected]92d2dc82010-04-08 17:49:591562 // and only fall back to gconf if env vars were unset. But
[email protected]d6cb85b2009-07-23 22:10:531563 // gnome-terminal "helpfully" sets http_proxy and no_proxy, and it
1564 // does so even if the proxy mode is set to auto, which would
1565 // mislead us.
1566
[email protected]3e44697f2009-05-22 14:37:391567 bool got_config = false;
[email protected]573c0502011-05-17 22:19:501568 if (setting_getter_.get() &&
1569 setting_getter_->Init(glib_default_loop, file_loop) &&
1570 GetConfigFromSettings(&cached_config_)) {
[email protected]d3066142011-05-10 02:36:201571 cached_config_.set_id(1); // Mark it as valid.
1572 VLOG(1) << "Obtained proxy settings from "
[email protected]573c0502011-05-17 22:19:501573 << setting_getter_->GetDataSource();
[email protected]d3066142011-05-10 02:36:201574
1575 // If gconf proxy mode is "none", meaning direct, then we take
1576 // that to be a valid config and will not check environment
1577 // variables. The alternative would have been to look for a proxy
1578 // whereever we can find one.
1579 got_config = true;
1580
1581 // Keep a copy of the config for use from this thread for
1582 // comparison with updated settings when we get notifications.
1583 reference_config_ = cached_config_;
1584 reference_config_.set_id(1); // Mark it as valid.
1585
1586 // We only set up notifications if we have IO and file loops available.
1587 // We do this after getting the initial configuration so that we don't have
1588 // to worry about cancelling it if the initial fetch above fails. Note that
1589 // setting up notifications has the side effect of simulating a change, so
1590 // that we won't lose any updates that may have happened after the initial
1591 // fetch and before setting up notifications. We'll detect the common case
1592 // of no changes in OnCheckProxyConfigSettings() (or sooner) and ignore it.
1593 if (io_loop && file_loop) {
[email protected]573c0502011-05-17 22:19:501594 MessageLoop* required_loop = setting_getter_->GetNotificationLoop();
[email protected]d3066142011-05-10 02:36:201595 if (!required_loop || MessageLoop::current() == required_loop) {
1596 // In this case we are already on an acceptable thread.
1597 SetUpNotifications();
[email protected]d7395e732009-08-28 23:13:431598 } else {
[email protected]d3066142011-05-10 02:36:201599 // Post a task to set up notifications. We don't wait for success.
1600 required_loop->PostTask(
1601 FROM_HERE,
1602 NewRunnableMethod(
1603 this,
1604 &ProxyConfigServiceLinux::Delegate::SetUpNotifications));
[email protected]d6cb85b2009-07-23 22:10:531605 }
[email protected]d7395e732009-08-28 23:13:431606 }
[email protected]861c6c62009-04-20 16:50:561607 }
[email protected]d6cb85b2009-07-23 22:10:531608
[email protected]3e44697f2009-05-22 14:37:391609 if (!got_config) {
[email protected]d6cb85b2009-07-23 22:10:531610 // We fall back on environment variables.
[email protected]3e44697f2009-05-22 14:37:391611 //
[email protected]d3066142011-05-10 02:36:201612 // Consulting environment variables doesn't need to be done from the
1613 // default glib main loop, but it's a tiny enough amount of work.
[email protected]3e44697f2009-05-22 14:37:391614 if (GetConfigFromEnv(&cached_config_)) {
[email protected]d3066142011-05-10 02:36:201615 cached_config_.set_id(1); // Mark it as valid.
[email protected]b30a3f52010-10-16 01:05:461616 VLOG(1) << "Obtained proxy settings from environment variables";
[email protected]3e44697f2009-05-22 14:37:391617 }
[email protected]861c6c62009-04-20 16:50:561618 }
[email protected]3e44697f2009-05-22 14:37:391619}
1620
[email protected]573c0502011-05-17 22:19:501621// Depending on the SettingGetter in use, this method will be called
[email protected]d3066142011-05-10 02:36:201622// on either the UI thread (GConf) or the file thread (KDE).
1623void ProxyConfigServiceLinux::Delegate::SetUpNotifications() {
[email protected]573c0502011-05-17 22:19:501624 MessageLoop* required_loop = setting_getter_->GetNotificationLoop();
[email protected]d3066142011-05-10 02:36:201625 DCHECK(!required_loop || MessageLoop::current() == required_loop);
[email protected]573c0502011-05-17 22:19:501626 if (!setting_getter_->SetUpNotifications(this))
[email protected]d3066142011-05-10 02:36:201627 LOG(ERROR) << "Unable to set up proxy configuration change notifications";
1628}
1629
[email protected]119655002010-07-23 06:02:401630void ProxyConfigServiceLinux::Delegate::AddObserver(Observer* observer) {
1631 observers_.AddObserver(observer);
1632}
1633
1634void ProxyConfigServiceLinux::Delegate::RemoveObserver(Observer* observer) {
1635 observers_.RemoveObserver(observer);
1636}
1637
[email protected]3a29593d2011-04-11 10:07:521638ProxyConfigService::ConfigAvailability
1639 ProxyConfigServiceLinux::Delegate::GetLatestProxyConfig(
1640 ProxyConfig* config) {
[email protected]3e44697f2009-05-22 14:37:391641 // This is called from the IO thread.
1642 DCHECK(!io_loop_ || MessageLoop::current() == io_loop_);
1643
1644 // Simply return the last proxy configuration that glib_default_loop
1645 // notified us of.
[email protected]119655002010-07-23 06:02:401646 *config = cached_config_.is_valid() ?
1647 cached_config_ : ProxyConfig::CreateDirect();
1648
[email protected]3a29593d2011-04-11 10:07:521649 // We return CONFIG_VALID to indicate that *config was filled in. It is always
[email protected]119655002010-07-23 06:02:401650 // going to be available since we initialized eagerly on the UI thread.
1651 // TODO(eroman): do lazy initialization instead, so we no longer need
1652 // to construct ProxyConfigServiceLinux on the UI thread.
1653 // In which case, we may return false here.
[email protected]3a29593d2011-04-11 10:07:521654 return CONFIG_VALID;
[email protected]3e44697f2009-05-22 14:37:391655}
1656
[email protected]573c0502011-05-17 22:19:501657// Depending on the SettingGetter in use, this method will be called
[email protected]d7395e732009-08-28 23:13:431658// on either the UI thread (GConf) or the file thread (KDE).
[email protected]3e44697f2009-05-22 14:37:391659void ProxyConfigServiceLinux::Delegate::OnCheckProxyConfigSettings() {
[email protected]573c0502011-05-17 22:19:501660 MessageLoop* required_loop = setting_getter_->GetNotificationLoop();
[email protected]d7395e732009-08-28 23:13:431661 DCHECK(!required_loop || MessageLoop::current() == required_loop);
[email protected]3e44697f2009-05-22 14:37:391662 ProxyConfig new_config;
[email protected]573c0502011-05-17 22:19:501663 bool valid = GetConfigFromSettings(&new_config);
[email protected]3e44697f2009-05-22 14:37:391664 if (valid)
1665 new_config.set_id(1); // mark it as valid
1666
[email protected]119655002010-07-23 06:02:401667 // See if it is different from what we had before.
[email protected]3e44697f2009-05-22 14:37:391668 if (new_config.is_valid() != reference_config_.is_valid() ||
1669 !new_config.Equals(reference_config_)) {
1670 // Post a task to |io_loop| with the new configuration, so it can
1671 // update |cached_config_|.
1672 io_loop_->PostTask(
1673 FROM_HERE,
1674 NewRunnableMethod(
1675 this,
1676 &ProxyConfigServiceLinux::Delegate::SetNewProxyConfig,
1677 new_config));
[email protected]d1f9d472009-08-13 19:59:301678 // Update the thread-private copy in |reference_config_| as well.
1679 reference_config_ = new_config;
[email protected]d3066142011-05-10 02:36:201680 } else {
1681 VLOG(1) << "Detected no-op change to proxy settings. Doing nothing.";
[email protected]3e44697f2009-05-22 14:37:391682 }
1683}
1684
1685void ProxyConfigServiceLinux::Delegate::SetNewProxyConfig(
1686 const ProxyConfig& new_config) {
1687 DCHECK(MessageLoop::current() == io_loop_);
[email protected]b30a3f52010-10-16 01:05:461688 VLOG(1) << "Proxy configuration changed";
[email protected]3e44697f2009-05-22 14:37:391689 cached_config_ = new_config;
[email protected]3a29593d2011-04-11 10:07:521690 FOR_EACH_OBSERVER(
1691 Observer, observers_,
1692 OnProxyConfigChanged(new_config, ProxyConfigService::CONFIG_VALID));
[email protected]3e44697f2009-05-22 14:37:391693}
1694
1695void ProxyConfigServiceLinux::Delegate::PostDestroyTask() {
[email protected]573c0502011-05-17 22:19:501696 if (!setting_getter_.get())
[email protected]d7395e732009-08-28 23:13:431697 return;
[email protected]573c0502011-05-17 22:19:501698 MessageLoop* shutdown_loop = setting_getter_->GetNotificationLoop();
[email protected]d7395e732009-08-28 23:13:431699 if (!shutdown_loop || MessageLoop::current() == shutdown_loop) {
[email protected]3e44697f2009-05-22 14:37:391700 // Already on the right thread, call directly.
1701 // This is the case for the unittests.
1702 OnDestroy();
1703 } else {
[email protected]d7395e732009-08-28 23:13:431704 // Post to shutdown thread. Note that on browser shutdown, we may quit
1705 // this MessageLoop and exit the program before ever running this.
1706 shutdown_loop->PostTask(
[email protected]3e44697f2009-05-22 14:37:391707 FROM_HERE,
1708 NewRunnableMethod(
1709 this,
1710 &ProxyConfigServiceLinux::Delegate::OnDestroy));
1711 }
1712}
1713void ProxyConfigServiceLinux::Delegate::OnDestroy() {
[email protected]573c0502011-05-17 22:19:501714 MessageLoop* shutdown_loop = setting_getter_->GetNotificationLoop();
[email protected]d7395e732009-08-28 23:13:431715 DCHECK(!shutdown_loop || MessageLoop::current() == shutdown_loop);
[email protected]573c0502011-05-17 22:19:501716 setting_getter_->ShutDown();
[email protected]3e44697f2009-05-22 14:37:391717}
1718
1719ProxyConfigServiceLinux::ProxyConfigServiceLinux()
[email protected]76b90d312010-08-03 03:00:501720 : delegate_(new Delegate(base::Environment::Create())) {
[email protected]3e44697f2009-05-22 14:37:391721}
1722
[email protected]8e1845e12010-09-15 19:22:241723ProxyConfigServiceLinux::~ProxyConfigServiceLinux() {
1724 delegate_->PostDestroyTask();
1725}
1726
[email protected]3e44697f2009-05-22 14:37:391727ProxyConfigServiceLinux::ProxyConfigServiceLinux(
[email protected]76b90d312010-08-03 03:00:501728 base::Environment* env_var_getter)
[email protected]9a3d8d42009-09-03 17:01:461729 : delegate_(new Delegate(env_var_getter)) {
1730}
1731
1732ProxyConfigServiceLinux::ProxyConfigServiceLinux(
[email protected]573c0502011-05-17 22:19:501733 base::Environment* env_var_getter, SettingGetter* setting_getter)
1734 : delegate_(new Delegate(env_var_getter, setting_getter)) {
[email protected]861c6c62009-04-20 16:50:561735}
1736
[email protected]e4be2dd2010-12-14 00:44:391737void ProxyConfigServiceLinux::AddObserver(Observer* observer) {
1738 delegate_->AddObserver(observer);
1739}
1740
1741void ProxyConfigServiceLinux::RemoveObserver(Observer* observer) {
1742 delegate_->RemoveObserver(observer);
1743}
1744
[email protected]3a29593d2011-04-11 10:07:521745ProxyConfigService::ConfigAvailability
1746 ProxyConfigServiceLinux::GetLatestProxyConfig(ProxyConfig* config) {
[email protected]e4be2dd2010-12-14 00:44:391747 return delegate_->GetLatestProxyConfig(config);
1748}
1749
[email protected]861c6c62009-04-20 16:50:561750} // namespace net