[email protected] | d2e6d59 | 2012-02-03 21:49:04 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 2 | // 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] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 7 | #include <errno.h> |
| 8 | #include <fcntl.h> |
[email protected] | 6de53d4 | 2010-11-09 07:33:19 | [diff] [blame] | 9 | #if defined(USE_GCONF) |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 10 | #include <gconf/gconf-client.h> |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 11 | #endif // defined(USE_GCONF) |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 12 | #include <limits.h> |
| 13 | #include <stdio.h> |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 14 | #include <stdlib.h> |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 15 | #include <sys/inotify.h> |
| 16 | #include <unistd.h> |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 17 | |
[email protected] | 9bc8cff | 2010-04-03 01:05:39 | [diff] [blame] | 18 | #include <map> |
| 19 | |
[email protected] | 6af889c | 2011-10-06 23:11:41 | [diff] [blame] | 20 | #include "base/bind.h" |
[email protected] | c4c1b48 | 2011-07-22 17:24:26 | [diff] [blame] | 21 | #include "base/compiler_specific.h" |
[email protected] | 4bbb72d | 2014-06-06 18:05:51 | [diff] [blame] | 22 | #include "base/debug/leak_annotations.h" |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 23 | #include "base/environment.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 24 | #include "base/files/file_path.h" |
thestig | d8df033 | 2014-09-04 06:33:29 | [diff] [blame^] | 25 | #include "base/files/file_util.h" |
[email protected] | b9b4a57 | 2014-03-17 23:11:12 | [diff] [blame] | 26 | #include "base/files/scoped_file.h" |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 27 | #include "base/logging.h" |
[email protected] | 18b57741 | 2013-07-18 04:19:15 | [diff] [blame] | 28 | #include "base/message_loop/message_loop.h" |
[email protected] | 3a29593d | 2011-04-11 10:07:52 | [diff] [blame] | 29 | #include "base/nix/xdg_util.h" |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 30 | #include "base/single_thread_task_runner.h" |
[email protected] | fc9be580 | 2013-06-11 10:56:51 | [diff] [blame] | 31 | #include "base/strings/string_number_conversions.h" |
[email protected] | f4ebe77 | 2013-02-02 00:21:39 | [diff] [blame] | 32 | #include "base/strings/string_tokenizer.h" |
[email protected] | 66e96c4 | 2013-06-28 15:20:31 | [diff] [blame] | 33 | #include "base/strings/string_util.h" |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 34 | #include "base/threading/thread_restrictions.h" |
[email protected] | 66e96c4 | 2013-06-28 15:20:31 | [diff] [blame] | 35 | #include "base/timer/timer.h" |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 36 | #include "net/base/net_errors.h" |
| 37 | #include "net/http/http_util.h" |
| 38 | #include "net/proxy/proxy_config.h" |
| 39 | #include "net/proxy/proxy_server.h" |
[email protected] | f89276a7 | 2013-07-12 06:41:54 | [diff] [blame] | 40 | #include "url/url_canon.h" |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 41 | |
[email protected] | 3fc24f5 | 2012-11-30 21:22:34 | [diff] [blame] | 42 | #if defined(USE_GIO) |
| 43 | #include "library_loaders/libgio.h" |
| 44 | #endif // defined(USE_GIO) |
| 45 | |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 46 | namespace net { |
| 47 | |
| 48 | namespace { |
| 49 | |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 50 | // 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] | 87a102b | 2009-07-14 05:23:30 | [diff] [blame] | 54 | // TODO(arindam): Remove URI string manipulation by using MapUrlSchemeToProxy. |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 55 | std::string FixupProxyHostScheme(ProxyServer::Scheme scheme, |
| 56 | std::string host) { |
[email protected] | e8c5081 | 2010-09-28 00:16:17 | [diff] [blame] | 57 | 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] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 62 | } |
| 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] | 62749f18 | 2009-07-15 13:16:54 | [diff] [blame] | 71 | // 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] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 75 | 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] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 84 | // 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] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 88 | return host; |
| 89 | } |
| 90 | |
| 91 | } // namespace |
| 92 | |
[email protected] | 8e1845e1 | 2010-09-15 19:22:24 | [diff] [blame] | 93 | ProxyConfigServiceLinux::Delegate::~Delegate() { |
| 94 | } |
| 95 | |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 96 | bool ProxyConfigServiceLinux::Delegate::GetProxyFromEnvVarForScheme( |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 97 | const char* variable, ProxyServer::Scheme scheme, |
| 98 | ProxyServer* result_server) { |
| 99 | std::string env_value; |
[email protected] | 3ba7e08 | 2010-08-07 02:57:59 | [diff] [blame] | 100 | if (env_var_getter_->GetVar(variable, &env_value)) { |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 101 | if (!env_value.empty()) { |
| 102 | env_value = FixupProxyHostScheme(scheme, env_value); |
[email protected] | 87a102b | 2009-07-14 05:23:30 | [diff] [blame] | 103 | ProxyServer proxy_server = |
| 104 | ProxyServer::FromURI(env_value, ProxyServer::SCHEME_HTTP); |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 105 | if (proxy_server.is_valid() && !proxy_server.is_direct()) { |
| 106 | *result_server = proxy_server; |
| 107 | return true; |
| 108 | } else { |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 109 | LOG(ERROR) << "Failed to parse environment variable " << variable; |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | } |
| 113 | return false; |
| 114 | } |
| 115 | |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 116 | bool ProxyConfigServiceLinux::Delegate::GetProxyFromEnvVar( |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 117 | const char* variable, ProxyServer* result_server) { |
| 118 | return GetProxyFromEnvVarForScheme(variable, ProxyServer::SCHEME_HTTP, |
| 119 | result_server); |
| 120 | } |
| 121 | |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 122 | bool ProxyConfigServiceLinux::Delegate::GetConfigFromEnv(ProxyConfig* config) { |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 123 | // 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] | 3ba7e08 | 2010-08-07 02:57:59 | [diff] [blame] | 128 | if (env_var_getter_->GetVar("auto_proxy", &auto_proxy)) { |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 129 | if (auto_proxy.empty()) { |
| 130 | // Defined and empty => autodetect |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 131 | config->set_auto_detect(true); |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 132 | } else { |
| 133 | // specified autoconfig URL |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 134 | config->set_pac_url(GURL(auto_proxy)); |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 135 | } |
| 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] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 141 | config->proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY; |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 142 | config->proxy_rules().single_proxies.SetSingleProxyServer(proxy_server); |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 143 | } else { |
| 144 | bool have_http = GetProxyFromEnvVar("http_proxy", &proxy_server); |
| 145 | if (have_http) |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 146 | config->proxy_rules().proxies_for_http.SetSingleProxyServer(proxy_server); |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 147 | // 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] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 154 | config->proxy_rules().proxies_for_https. |
| 155 | SetSingleProxyServer(proxy_server); |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 156 | bool have_ftp = GetProxyFromEnvVar("ftp_proxy", &proxy_server); |
| 157 | if (have_ftp) |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 158 | config->proxy_rules().proxies_for_ftp.SetSingleProxyServer(proxy_server); |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 159 | if (have_http || have_https || have_ftp) { |
| 160 | // mustn't change type unless some rules are actually set. |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 161 | config->proxy_rules().type = |
| 162 | ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME; |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 163 | } |
| 164 | } |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 165 | if (config->proxy_rules().empty()) { |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 166 | // If the above were not defined, try for socks. |
[email protected] | e8c5081 | 2010-09-28 00:16:17 | [diff] [blame] | 167 | // For environment variables, we default to version 5, per the gnome |
| 168 | // documentation: http://library.gnome.org/devel/gnet/stable/gnet-socks.html |
| 169 | ProxyServer::Scheme scheme = ProxyServer::SCHEME_SOCKS5; |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 170 | std::string env_version; |
[email protected] | 3ba7e08 | 2010-08-07 02:57:59 | [diff] [blame] | 171 | if (env_var_getter_->GetVar("SOCKS_VERSION", &env_version) |
[email protected] | e8c5081 | 2010-09-28 00:16:17 | [diff] [blame] | 172 | && env_version == "4") |
| 173 | scheme = ProxyServer::SCHEME_SOCKS4; |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 174 | if (GetProxyFromEnvVarForScheme("SOCKS_SERVER", scheme, &proxy_server)) { |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 175 | config->proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY; |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 176 | config->proxy_rules().single_proxies.SetSingleProxyServer(proxy_server); |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | // Look for the proxy bypass list. |
| 180 | std::string no_proxy; |
[email protected] | 3ba7e08 | 2010-08-07 02:57:59 | [diff] [blame] | 181 | env_var_getter_->GetVar("no_proxy", &no_proxy); |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 182 | if (config->proxy_rules().empty()) { |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 183 | // Having only "no_proxy" set, presumably to "*", makes it |
| 184 | // explicit that env vars do specify a configuration: having no |
| 185 | // rules specified only means the user explicitly asks for direct |
| 186 | // connections. |
| 187 | return !no_proxy.empty(); |
| 188 | } |
[email protected] | 7541206c | 2010-02-19 20:24:06 | [diff] [blame] | 189 | // Note that this uses "suffix" matching. So a bypass of "google.com" |
| 190 | // is understood to mean a bypass of "*google.com". |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 191 | config->proxy_rules().bypass_rules.ParseFromStringUsingSuffixMatching( |
| 192 | no_proxy); |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 193 | return true; |
| 194 | } |
| 195 | |
| 196 | namespace { |
| 197 | |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 198 | const int kDebounceTimeoutMilliseconds = 250; |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 199 | |
[email protected] | 6de53d4 | 2010-11-09 07:33:19 | [diff] [blame] | 200 | #if defined(USE_GCONF) |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 201 | // This setting getter uses gconf, as used in GNOME 2 and some GNOME 3 desktops. |
| 202 | class SettingGetterImplGConf : public ProxyConfigServiceLinux::SettingGetter { |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 203 | public: |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 204 | SettingGetterImplGConf() |
[email protected] | b160df3 | 2012-02-06 20:39:41 | [diff] [blame] | 205 | : client_(NULL), system_proxy_id_(0), system_http_proxy_id_(0), |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 206 | notify_delegate_(NULL) { |
[email protected] | b160df3 | 2012-02-06 20:39:41 | [diff] [blame] | 207 | } |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 208 | |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 209 | virtual ~SettingGetterImplGConf() { |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 210 | // client_ should have been released before now, from |
[email protected] | f5b1344 | 2009-07-13 15:23:59 | [diff] [blame] | 211 | // Delegate::OnDestroy(), while running on the UI thread. However |
[email protected] | b160df3 | 2012-02-06 20:39:41 | [diff] [blame] | 212 | // on exiting the process, it may happen that Delegate::OnDestroy() |
| 213 | // task is left pending on the glib loop after the loop was quit, |
| 214 | // and pending tasks may then be deleted without being run. |
[email protected] | f5b1344 | 2009-07-13 15:23:59 | [diff] [blame] | 215 | if (client_) { |
| 216 | // gconf client was not cleaned up. |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 217 | if (task_runner_->BelongsToCurrentThread()) { |
[email protected] | f5b1344 | 2009-07-13 15:23:59 | [diff] [blame] | 218 | // We are on the UI thread so we can clean it safely. This is |
| 219 | // the case at least for ui_tests running under Valgrind in |
| 220 | // bug 16076. |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 221 | VLOG(1) << "~SettingGetterImplGConf: releasing gconf client"; |
[email protected] | d306614 | 2011-05-10 02:36:20 | [diff] [blame] | 222 | ShutDown(); |
[email protected] | f5b1344 | 2009-07-13 15:23:59 | [diff] [blame] | 223 | } else { |
[email protected] | ed34899 | 2011-09-19 20:27:57 | [diff] [blame] | 224 | // This is very bad! We are deleting the setting getter but we're not on |
| 225 | // the UI thread. This is not supposed to happen: the setting getter is |
| 226 | // owned by the proxy config service's delegate, which is supposed to be |
| 227 | // destroyed on the UI thread only. We will get change notifications to |
| 228 | // a deleted object if we continue here, so fail now. |
| 229 | LOG(FATAL) << "~SettingGetterImplGConf: deleting on wrong thread!"; |
[email protected] | f5b1344 | 2009-07-13 15:23:59 | [diff] [blame] | 230 | } |
| 231 | } |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 232 | DCHECK(!client_); |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 233 | } |
| 234 | |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 235 | virtual bool Init(base::SingleThreadTaskRunner* glib_thread_task_runner, |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 236 | base::MessageLoopForIO* file_loop) OVERRIDE { |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 237 | DCHECK(glib_thread_task_runner->BelongsToCurrentThread()); |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 238 | DCHECK(!client_); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 239 | DCHECK(!task_runner_.get()); |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 240 | task_runner_ = glib_thread_task_runner; |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 241 | client_ = gconf_client_get_default(); |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 242 | if (!client_) { |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 243 | // It's not clear whether/when this can return NULL. |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 244 | LOG(ERROR) << "Unable to create a gconf client"; |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 245 | task_runner_ = NULL; |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 246 | return false; |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 247 | } |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 248 | GError* error = NULL; |
[email protected] | b160df3 | 2012-02-06 20:39:41 | [diff] [blame] | 249 | bool added_system_proxy = false; |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 250 | // We need to add the directories for which we'll be asking |
[email protected] | b160df3 | 2012-02-06 20:39:41 | [diff] [blame] | 251 | // for notifications, and we might as well ask to preload them. |
| 252 | // These need to be removed again in ShutDown(); we are careful |
| 253 | // here to only leave client_ non-NULL if both have been added. |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 254 | gconf_client_add_dir(client_, "/system/proxy", |
| 255 | GCONF_CLIENT_PRELOAD_ONELEVEL, &error); |
| 256 | if (error == NULL) { |
[email protected] | b160df3 | 2012-02-06 20:39:41 | [diff] [blame] | 257 | added_system_proxy = true; |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 258 | gconf_client_add_dir(client_, "/system/http_proxy", |
| 259 | GCONF_CLIENT_PRELOAD_ONELEVEL, &error); |
| 260 | } |
| 261 | if (error != NULL) { |
| 262 | LOG(ERROR) << "Error requesting gconf directory: " << error->message; |
| 263 | g_error_free(error); |
[email protected] | b160df3 | 2012-02-06 20:39:41 | [diff] [blame] | 264 | if (added_system_proxy) |
| 265 | gconf_client_remove_dir(client_, "/system/proxy", NULL); |
| 266 | g_object_unref(client_); |
| 267 | client_ = NULL; |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 268 | task_runner_ = NULL; |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 269 | return false; |
| 270 | } |
| 271 | return true; |
| 272 | } |
| 273 | |
[email protected] | 749bf5c | 2012-09-17 03:15:21 | [diff] [blame] | 274 | virtual void ShutDown() OVERRIDE { |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 275 | if (client_) { |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 276 | DCHECK(task_runner_->BelongsToCurrentThread()); |
[email protected] | b160df3 | 2012-02-06 20:39:41 | [diff] [blame] | 277 | // We must explicitly disable gconf notifications here, because the gconf |
| 278 | // client will be shared between all setting getters, and they do not all |
| 279 | // have the same lifetimes. (For instance, incognito sessions get their |
| 280 | // own, which is destroyed when the session ends.) |
| 281 | gconf_client_notify_remove(client_, system_http_proxy_id_); |
| 282 | gconf_client_notify_remove(client_, system_proxy_id_); |
| 283 | gconf_client_remove_dir(client_, "/system/http_proxy", NULL); |
| 284 | gconf_client_remove_dir(client_, "/system/proxy", NULL); |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 285 | g_object_unref(client_); |
| 286 | client_ = NULL; |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 287 | task_runner_ = NULL; |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
[email protected] | 749bf5c | 2012-09-17 03:15:21 | [diff] [blame] | 291 | virtual bool SetUpNotifications( |
| 292 | ProxyConfigServiceLinux::Delegate* delegate) OVERRIDE { |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 293 | DCHECK(client_); |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 294 | DCHECK(task_runner_->BelongsToCurrentThread()); |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 295 | GError* error = NULL; |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 296 | notify_delegate_ = delegate; |
[email protected] | b160df3 | 2012-02-06 20:39:41 | [diff] [blame] | 297 | // We have to keep track of the IDs returned by gconf_client_notify_add() so |
| 298 | // that we can remove them in ShutDown(). (Otherwise, notifications will be |
| 299 | // delivered to this object after it is deleted, which is bad, m'kay?) |
| 300 | system_proxy_id_ = gconf_client_notify_add( |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 301 | client_, "/system/proxy", |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 302 | OnGConfChangeNotification, this, |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 303 | NULL, &error); |
| 304 | if (error == NULL) { |
[email protected] | b160df3 | 2012-02-06 20:39:41 | [diff] [blame] | 305 | system_http_proxy_id_ = gconf_client_notify_add( |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 306 | client_, "/system/http_proxy", |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 307 | OnGConfChangeNotification, this, |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 308 | NULL, &error); |
| 309 | } |
| 310 | if (error != NULL) { |
| 311 | LOG(ERROR) << "Error requesting gconf notifications: " << error->message; |
| 312 | g_error_free(error); |
[email protected] | d306614 | 2011-05-10 02:36:20 | [diff] [blame] | 313 | ShutDown(); |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 314 | return false; |
| 315 | } |
[email protected] | d306614 | 2011-05-10 02:36:20 | [diff] [blame] | 316 | // Simulate a change to avoid possibly losing updates before this point. |
| 317 | OnChangeNotification(); |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 318 | return true; |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 319 | } |
| 320 | |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 321 | virtual base::SingleThreadTaskRunner* GetNotificationTaskRunner() OVERRIDE { |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 322 | return task_runner_.get(); |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 323 | } |
| 324 | |
[email protected] | db8ff91 | 2012-06-12 23:32:51 | [diff] [blame] | 325 | virtual ProxyConfigSource GetConfigSource() OVERRIDE { |
| 326 | return PROXY_CONFIG_SOURCE_GCONF; |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 327 | } |
| 328 | |
[email protected] | c4c1b48 | 2011-07-22 17:24:26 | [diff] [blame] | 329 | virtual bool GetString(StringSetting key, std::string* result) OVERRIDE { |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 330 | switch (key) { |
| 331 | case PROXY_MODE: |
| 332 | return GetStringByPath("/system/proxy/mode", result); |
| 333 | case PROXY_AUTOCONF_URL: |
| 334 | return GetStringByPath("/system/proxy/autoconfig_url", result); |
| 335 | case PROXY_HTTP_HOST: |
| 336 | return GetStringByPath("/system/http_proxy/host", result); |
| 337 | case PROXY_HTTPS_HOST: |
| 338 | return GetStringByPath("/system/proxy/secure_host", result); |
| 339 | case PROXY_FTP_HOST: |
| 340 | return GetStringByPath("/system/proxy/ftp_host", result); |
| 341 | case PROXY_SOCKS_HOST: |
| 342 | return GetStringByPath("/system/proxy/socks_host", result); |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 343 | } |
[email protected] | 6b5fe74 | 2011-05-20 21:46:48 | [diff] [blame] | 344 | return false; // Placate compiler. |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 345 | } |
[email protected] | c4c1b48 | 2011-07-22 17:24:26 | [diff] [blame] | 346 | virtual bool GetBool(BoolSetting key, bool* result) OVERRIDE { |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 347 | switch (key) { |
| 348 | case PROXY_USE_HTTP_PROXY: |
| 349 | return GetBoolByPath("/system/http_proxy/use_http_proxy", result); |
| 350 | case PROXY_USE_SAME_PROXY: |
| 351 | return GetBoolByPath("/system/http_proxy/use_same_proxy", result); |
| 352 | case PROXY_USE_AUTHENTICATION: |
| 353 | return GetBoolByPath("/system/http_proxy/use_authentication", result); |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 354 | } |
[email protected] | 6b5fe74 | 2011-05-20 21:46:48 | [diff] [blame] | 355 | return false; // Placate compiler. |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 356 | } |
[email protected] | c4c1b48 | 2011-07-22 17:24:26 | [diff] [blame] | 357 | virtual bool GetInt(IntSetting key, int* result) OVERRIDE { |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 358 | switch (key) { |
| 359 | case PROXY_HTTP_PORT: |
| 360 | return GetIntByPath("/system/http_proxy/port", result); |
| 361 | case PROXY_HTTPS_PORT: |
| 362 | return GetIntByPath("/system/proxy/secure_port", result); |
| 363 | case PROXY_FTP_PORT: |
| 364 | return GetIntByPath("/system/proxy/ftp_port", result); |
| 365 | case PROXY_SOCKS_PORT: |
| 366 | return GetIntByPath("/system/proxy/socks_port", result); |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 367 | } |
[email protected] | 6b5fe74 | 2011-05-20 21:46:48 | [diff] [blame] | 368 | return false; // Placate compiler. |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 369 | } |
[email protected] | 6b5fe74 | 2011-05-20 21:46:48 | [diff] [blame] | 370 | virtual bool GetStringList(StringListSetting key, |
[email protected] | c4c1b48 | 2011-07-22 17:24:26 | [diff] [blame] | 371 | std::vector<std::string>* result) OVERRIDE { |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 372 | switch (key) { |
| 373 | case PROXY_IGNORE_HOSTS: |
| 374 | return GetStringListByPath("/system/http_proxy/ignore_hosts", result); |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 375 | } |
[email protected] | 6b5fe74 | 2011-05-20 21:46:48 | [diff] [blame] | 376 | return false; // Placate compiler. |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 377 | } |
| 378 | |
[email protected] | c4c1b48 | 2011-07-22 17:24:26 | [diff] [blame] | 379 | virtual bool BypassListIsReversed() OVERRIDE { |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 380 | // This is a KDE-specific setting. |
| 381 | return false; |
| 382 | } |
| 383 | |
[email protected] | c4c1b48 | 2011-07-22 17:24:26 | [diff] [blame] | 384 | virtual bool MatchHostsUsingSuffixMatching() OVERRIDE { |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 385 | return false; |
| 386 | } |
| 387 | |
| 388 | private: |
| 389 | bool GetStringByPath(const char* key, std::string* result) { |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 390 | DCHECK(client_); |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 391 | DCHECK(task_runner_->BelongsToCurrentThread()); |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 392 | GError* error = NULL; |
| 393 | gchar* value = gconf_client_get_string(client_, key, &error); |
| 394 | if (HandleGError(error, key)) |
| 395 | return false; |
| 396 | if (!value) |
| 397 | return false; |
| 398 | *result = value; |
| 399 | g_free(value); |
| 400 | return true; |
| 401 | } |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 402 | bool GetBoolByPath(const char* key, bool* result) { |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 403 | DCHECK(client_); |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 404 | DCHECK(task_runner_->BelongsToCurrentThread()); |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 405 | GError* error = NULL; |
| 406 | // We want to distinguish unset values from values defaulting to |
| 407 | // false. For that we need to use the type-generic |
| 408 | // gconf_client_get() rather than gconf_client_get_bool(). |
| 409 | GConfValue* gconf_value = gconf_client_get(client_, key, &error); |
| 410 | if (HandleGError(error, key)) |
| 411 | return false; |
| 412 | if (!gconf_value) { |
| 413 | // Unset. |
| 414 | return false; |
| 415 | } |
| 416 | if (gconf_value->type != GCONF_VALUE_BOOL) { |
| 417 | gconf_value_free(gconf_value); |
| 418 | return false; |
| 419 | } |
| 420 | gboolean bool_value = gconf_value_get_bool(gconf_value); |
| 421 | *result = static_cast<bool>(bool_value); |
| 422 | gconf_value_free(gconf_value); |
| 423 | return true; |
| 424 | } |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 425 | bool GetIntByPath(const char* key, int* result) { |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 426 | DCHECK(client_); |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 427 | DCHECK(task_runner_->BelongsToCurrentThread()); |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 428 | GError* error = NULL; |
| 429 | int value = gconf_client_get_int(client_, key, &error); |
| 430 | if (HandleGError(error, key)) |
| 431 | return false; |
| 432 | // We don't bother to distinguish an unset value because callers |
| 433 | // don't care. 0 is returned if unset. |
| 434 | *result = value; |
| 435 | return true; |
| 436 | } |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 437 | bool GetStringListByPath(const char* key, std::vector<std::string>* result) { |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 438 | DCHECK(client_); |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 439 | DCHECK(task_runner_->BelongsToCurrentThread()); |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 440 | GError* error = NULL; |
| 441 | GSList* list = gconf_client_get_list(client_, key, |
| 442 | GCONF_VALUE_STRING, &error); |
| 443 | if (HandleGError(error, key)) |
| 444 | return false; |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 445 | if (!list) |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 446 | return false; |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 447 | for (GSList *it = list; it; it = it->next) { |
| 448 | result->push_back(static_cast<char*>(it->data)); |
| 449 | g_free(it->data); |
| 450 | } |
| 451 | g_slist_free(list); |
| 452 | return true; |
| 453 | } |
| 454 | |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 455 | // Logs and frees a glib error. Returns false if there was no error |
| 456 | // (error is NULL). |
| 457 | bool HandleGError(GError* error, const char* key) { |
| 458 | if (error != NULL) { |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 459 | LOG(ERROR) << "Error getting gconf value for " << key |
| 460 | << ": " << error->message; |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 461 | g_error_free(error); |
| 462 | return true; |
| 463 | } |
| 464 | return false; |
| 465 | } |
| 466 | |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 467 | // This is the callback from the debounce timer. |
| 468 | void OnDebouncedNotification() { |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 469 | DCHECK(task_runner_->BelongsToCurrentThread()); |
[email protected] | 961ac94 | 2011-04-28 18:18:14 | [diff] [blame] | 470 | CHECK(notify_delegate_); |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 471 | // Forward to a method on the proxy config service delegate object. |
| 472 | notify_delegate_->OnCheckProxyConfigSettings(); |
| 473 | } |
| 474 | |
| 475 | void OnChangeNotification() { |
| 476 | // We don't use Reset() because the timer may not yet be running. |
| 477 | // (In that case Stop() is a no-op.) |
| 478 | debounce_timer_.Stop(); |
[email protected] | d323a17 | 2011-09-02 18:23:02 | [diff] [blame] | 479 | debounce_timer_.Start(FROM_HERE, |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 480 | base::TimeDelta::FromMilliseconds(kDebounceTimeoutMilliseconds), |
| 481 | this, &SettingGetterImplGConf::OnDebouncedNotification); |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 482 | } |
| 483 | |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 484 | // gconf notification callback, dispatched on the default glib main loop. |
| 485 | static void OnGConfChangeNotification(GConfClient* client, guint cnxn_id, |
| 486 | GConfEntry* entry, gpointer user_data) { |
[email protected] | b30a3f5 | 2010-10-16 01:05:46 | [diff] [blame] | 487 | VLOG(1) << "gconf change notification for key " |
| 488 | << gconf_entry_get_key(entry); |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 489 | // We don't track which key has changed, just that something did change. |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 490 | SettingGetterImplGConf* setting_getter = |
| 491 | reinterpret_cast<SettingGetterImplGConf*>(user_data); |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 492 | setting_getter->OnChangeNotification(); |
| 493 | } |
| 494 | |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 495 | GConfClient* client_; |
[email protected] | b160df3 | 2012-02-06 20:39:41 | [diff] [blame] | 496 | // These ids are the values returned from gconf_client_notify_add(), which we |
| 497 | // will need in order to later call gconf_client_notify_remove(). |
| 498 | guint system_proxy_id_; |
| 499 | guint system_http_proxy_id_; |
| 500 | |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 501 | ProxyConfigServiceLinux::Delegate* notify_delegate_; |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 502 | base::OneShotTimer<SettingGetterImplGConf> debounce_timer_; |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 503 | |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 504 | // Task runner for the thread that we make gconf calls on. It should |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 505 | // be the UI thread and all our methods should be called on this |
| 506 | // thread. Only for assertions. |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 507 | scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 508 | |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 509 | DISALLOW_COPY_AND_ASSIGN(SettingGetterImplGConf); |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 510 | }; |
[email protected] | 6de53d4 | 2010-11-09 07:33:19 | [diff] [blame] | 511 | #endif // defined(USE_GCONF) |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 512 | |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 513 | #if defined(USE_GIO) |
[email protected] | 2297bb2 | 2014-06-19 06:30:14 | [diff] [blame] | 514 | const char kProxyGConfSchema[] = "org.gnome.system.proxy"; |
| 515 | |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 516 | // This setting getter uses gsettings, as used in most GNOME 3 desktops. |
| 517 | class SettingGetterImplGSettings |
| 518 | : public ProxyConfigServiceLinux::SettingGetter { |
| 519 | public: |
[email protected] | 0e14e87d | 2011-06-21 21:24:19 | [diff] [blame] | 520 | SettingGetterImplGSettings() : |
[email protected] | 0e14e87d | 2011-06-21 21:24:19 | [diff] [blame] | 521 | client_(NULL), |
| 522 | http_client_(NULL), |
| 523 | https_client_(NULL), |
| 524 | ftp_client_(NULL), |
| 525 | socks_client_(NULL), |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 526 | notify_delegate_(NULL) { |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | virtual ~SettingGetterImplGSettings() { |
| 530 | // client_ should have been released before now, from |
| 531 | // Delegate::OnDestroy(), while running on the UI thread. However |
| 532 | // on exiting the process, it may happen that |
| 533 | // Delegate::OnDestroy() task is left pending on the glib loop |
| 534 | // after the loop was quit, and pending tasks may then be deleted |
| 535 | // without being run. |
| 536 | if (client_) { |
| 537 | // gconf client was not cleaned up. |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 538 | if (task_runner_->BelongsToCurrentThread()) { |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 539 | // We are on the UI thread so we can clean it safely. This is |
| 540 | // the case at least for ui_tests running under Valgrind in |
| 541 | // bug 16076. |
| 542 | VLOG(1) << "~SettingGetterImplGSettings: releasing gsettings client"; |
| 543 | ShutDown(); |
| 544 | } else { |
| 545 | LOG(WARNING) << "~SettingGetterImplGSettings: leaking gsettings client"; |
| 546 | client_ = NULL; |
| 547 | } |
| 548 | } |
| 549 | DCHECK(!client_); |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 550 | } |
| 551 | |
[email protected] | 4cf80f0b | 2011-05-20 20:30:26 | [diff] [blame] | 552 | bool SchemaExists(const char* schema_name) { |
[email protected] | 3fc24f5 | 2012-11-30 21:22:34 | [diff] [blame] | 553 | const gchar* const* schemas = libgio_loader_.g_settings_list_schemas(); |
[email protected] | 4cf80f0b | 2011-05-20 20:30:26 | [diff] [blame] | 554 | while (*schemas) { |
[email protected] | a099f3ae | 2011-08-16 21:06:58 | [diff] [blame] | 555 | if (strcmp(schema_name, static_cast<const char*>(*schemas)) == 0) |
[email protected] | 4cf80f0b | 2011-05-20 20:30:26 | [diff] [blame] | 556 | return true; |
| 557 | schemas++; |
| 558 | } |
| 559 | return false; |
| 560 | } |
| 561 | |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 562 | // LoadAndCheckVersion() must be called *before* Init()! |
| 563 | bool LoadAndCheckVersion(base::Environment* env); |
| 564 | |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 565 | virtual bool Init(base::SingleThreadTaskRunner* glib_thread_task_runner, |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 566 | base::MessageLoopForIO* file_loop) OVERRIDE { |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 567 | DCHECK(glib_thread_task_runner->BelongsToCurrentThread()); |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 568 | DCHECK(!client_); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 569 | DCHECK(!task_runner_.get()); |
[email protected] | 4cf80f0b | 2011-05-20 20:30:26 | [diff] [blame] | 570 | |
[email protected] | 4bbb72d | 2014-06-06 18:05:51 | [diff] [blame] | 571 | if (!SchemaExists(kProxyGConfSchema) || |
| 572 | !(client_ = libgio_loader_.g_settings_new(kProxyGConfSchema))) { |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 573 | // It's not clear whether/when this can return NULL. |
| 574 | LOG(ERROR) << "Unable to create a gsettings client"; |
| 575 | return false; |
| 576 | } |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 577 | task_runner_ = glib_thread_task_runner; |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 578 | // We assume these all work if the above call worked. |
[email protected] | 3fc24f5 | 2012-11-30 21:22:34 | [diff] [blame] | 579 | http_client_ = libgio_loader_.g_settings_get_child(client_, "http"); |
| 580 | https_client_ = libgio_loader_.g_settings_get_child(client_, "https"); |
| 581 | ftp_client_ = libgio_loader_.g_settings_get_child(client_, "ftp"); |
| 582 | socks_client_ = libgio_loader_.g_settings_get_child(client_, "socks"); |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 583 | DCHECK(http_client_ && https_client_ && ftp_client_ && socks_client_); |
| 584 | return true; |
| 585 | } |
| 586 | |
[email protected] | 749bf5c | 2012-09-17 03:15:21 | [diff] [blame] | 587 | virtual void ShutDown() OVERRIDE { |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 588 | if (client_) { |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 589 | DCHECK(task_runner_->BelongsToCurrentThread()); |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 590 | // This also disables gsettings notifications. |
| 591 | g_object_unref(socks_client_); |
| 592 | g_object_unref(ftp_client_); |
| 593 | g_object_unref(https_client_); |
| 594 | g_object_unref(http_client_); |
| 595 | g_object_unref(client_); |
| 596 | // We only need to null client_ because it's the only one that we check. |
| 597 | client_ = NULL; |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 598 | task_runner_ = NULL; |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 599 | } |
| 600 | } |
| 601 | |
[email protected] | 749bf5c | 2012-09-17 03:15:21 | [diff] [blame] | 602 | virtual bool SetUpNotifications( |
| 603 | ProxyConfigServiceLinux::Delegate* delegate) OVERRIDE { |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 604 | DCHECK(client_); |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 605 | DCHECK(task_runner_->BelongsToCurrentThread()); |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 606 | notify_delegate_ = delegate; |
| 607 | // We could watch for the change-event signal instead of changed, but |
| 608 | // since we have to watch more than one object, we'd still have to |
| 609 | // debounce change notifications. This is conceptually simpler. |
| 610 | g_signal_connect(G_OBJECT(client_), "changed", |
| 611 | G_CALLBACK(OnGSettingsChangeNotification), this); |
| 612 | g_signal_connect(G_OBJECT(http_client_), "changed", |
| 613 | G_CALLBACK(OnGSettingsChangeNotification), this); |
| 614 | g_signal_connect(G_OBJECT(https_client_), "changed", |
| 615 | G_CALLBACK(OnGSettingsChangeNotification), this); |
| 616 | g_signal_connect(G_OBJECT(ftp_client_), "changed", |
| 617 | G_CALLBACK(OnGSettingsChangeNotification), this); |
| 618 | g_signal_connect(G_OBJECT(socks_client_), "changed", |
| 619 | G_CALLBACK(OnGSettingsChangeNotification), this); |
| 620 | // Simulate a change to avoid possibly losing updates before this point. |
| 621 | OnChangeNotification(); |
| 622 | return true; |
| 623 | } |
| 624 | |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 625 | virtual base::SingleThreadTaskRunner* GetNotificationTaskRunner() OVERRIDE { |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 626 | return task_runner_.get(); |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 627 | } |
| 628 | |
[email protected] | db8ff91 | 2012-06-12 23:32:51 | [diff] [blame] | 629 | virtual ProxyConfigSource GetConfigSource() OVERRIDE { |
| 630 | return PROXY_CONFIG_SOURCE_GSETTINGS; |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 631 | } |
| 632 | |
[email protected] | c4c1b48 | 2011-07-22 17:24:26 | [diff] [blame] | 633 | virtual bool GetString(StringSetting key, std::string* result) OVERRIDE { |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 634 | DCHECK(client_); |
| 635 | switch (key) { |
| 636 | case PROXY_MODE: |
| 637 | return GetStringByPath(client_, "mode", result); |
| 638 | case PROXY_AUTOCONF_URL: |
| 639 | return GetStringByPath(client_, "autoconfig-url", result); |
| 640 | case PROXY_HTTP_HOST: |
| 641 | return GetStringByPath(http_client_, "host", result); |
| 642 | case PROXY_HTTPS_HOST: |
| 643 | return GetStringByPath(https_client_, "host", result); |
| 644 | case PROXY_FTP_HOST: |
| 645 | return GetStringByPath(ftp_client_, "host", result); |
| 646 | case PROXY_SOCKS_HOST: |
| 647 | return GetStringByPath(socks_client_, "host", result); |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 648 | } |
[email protected] | 6b5fe74 | 2011-05-20 21:46:48 | [diff] [blame] | 649 | return false; // Placate compiler. |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 650 | } |
[email protected] | c4c1b48 | 2011-07-22 17:24:26 | [diff] [blame] | 651 | virtual bool GetBool(BoolSetting key, bool* result) OVERRIDE { |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 652 | DCHECK(client_); |
| 653 | switch (key) { |
| 654 | case PROXY_USE_HTTP_PROXY: |
| 655 | // Although there is an "enabled" boolean in http_client_, it is not set |
| 656 | // to true by the proxy config utility. We ignore it and return false. |
| 657 | return false; |
| 658 | case PROXY_USE_SAME_PROXY: |
| 659 | // Similarly, although there is a "use-same-proxy" boolean in client_, |
| 660 | // it is never set to false by the proxy config utility. We ignore it. |
| 661 | return false; |
| 662 | case PROXY_USE_AUTHENTICATION: |
| 663 | // There is also no way to set this in the proxy config utility, but it |
| 664 | // doesn't hurt us to get the actual setting (unlike the two above). |
| 665 | return GetBoolByPath(http_client_, "use-authentication", result); |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 666 | } |
[email protected] | 6b5fe74 | 2011-05-20 21:46:48 | [diff] [blame] | 667 | return false; // Placate compiler. |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 668 | } |
[email protected] | c4c1b48 | 2011-07-22 17:24:26 | [diff] [blame] | 669 | virtual bool GetInt(IntSetting key, int* result) OVERRIDE { |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 670 | DCHECK(client_); |
| 671 | switch (key) { |
| 672 | case PROXY_HTTP_PORT: |
| 673 | return GetIntByPath(http_client_, "port", result); |
| 674 | case PROXY_HTTPS_PORT: |
| 675 | return GetIntByPath(https_client_, "port", result); |
| 676 | case PROXY_FTP_PORT: |
| 677 | return GetIntByPath(ftp_client_, "port", result); |
| 678 | case PROXY_SOCKS_PORT: |
| 679 | return GetIntByPath(socks_client_, "port", result); |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 680 | } |
[email protected] | 6b5fe74 | 2011-05-20 21:46:48 | [diff] [blame] | 681 | return false; // Placate compiler. |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 682 | } |
[email protected] | 6b5fe74 | 2011-05-20 21:46:48 | [diff] [blame] | 683 | virtual bool GetStringList(StringListSetting key, |
[email protected] | c4c1b48 | 2011-07-22 17:24:26 | [diff] [blame] | 684 | std::vector<std::string>* result) OVERRIDE { |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 685 | DCHECK(client_); |
| 686 | switch (key) { |
| 687 | case PROXY_IGNORE_HOSTS: |
| 688 | return GetStringListByPath(client_, "ignore-hosts", result); |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 689 | } |
[email protected] | 6b5fe74 | 2011-05-20 21:46:48 | [diff] [blame] | 690 | return false; // Placate compiler. |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 691 | } |
| 692 | |
[email protected] | c4c1b48 | 2011-07-22 17:24:26 | [diff] [blame] | 693 | virtual bool BypassListIsReversed() OVERRIDE { |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 694 | // This is a KDE-specific setting. |
| 695 | return false; |
| 696 | } |
| 697 | |
[email protected] | c4c1b48 | 2011-07-22 17:24:26 | [diff] [blame] | 698 | virtual bool MatchHostsUsingSuffixMatching() OVERRIDE { |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 699 | return false; |
| 700 | } |
| 701 | |
| 702 | private: |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 703 | bool GetStringByPath(GSettings* client, const char* key, |
| 704 | std::string* result) { |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 705 | DCHECK(task_runner_->BelongsToCurrentThread()); |
[email protected] | 3fc24f5 | 2012-11-30 21:22:34 | [diff] [blame] | 706 | gchar* value = libgio_loader_.g_settings_get_string(client, key); |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 707 | if (!value) |
| 708 | return false; |
| 709 | *result = value; |
| 710 | g_free(value); |
| 711 | return true; |
| 712 | } |
| 713 | bool GetBoolByPath(GSettings* client, const char* key, bool* result) { |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 714 | DCHECK(task_runner_->BelongsToCurrentThread()); |
[email protected] | 3fc24f5 | 2012-11-30 21:22:34 | [diff] [blame] | 715 | *result = static_cast<bool>( |
| 716 | libgio_loader_.g_settings_get_boolean(client, key)); |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 717 | return true; |
| 718 | } |
| 719 | bool GetIntByPath(GSettings* client, const char* key, int* result) { |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 720 | DCHECK(task_runner_->BelongsToCurrentThread()); |
[email protected] | 3fc24f5 | 2012-11-30 21:22:34 | [diff] [blame] | 721 | *result = libgio_loader_.g_settings_get_int(client, key); |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 722 | return true; |
| 723 | } |
| 724 | bool GetStringListByPath(GSettings* client, const char* key, |
| 725 | std::vector<std::string>* result) { |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 726 | DCHECK(task_runner_->BelongsToCurrentThread()); |
[email protected] | 3fc24f5 | 2012-11-30 21:22:34 | [diff] [blame] | 727 | gchar** list = libgio_loader_.g_settings_get_strv(client, key); |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 728 | if (!list) |
| 729 | return false; |
| 730 | for (size_t i = 0; list[i]; ++i) { |
| 731 | result->push_back(static_cast<char*>(list[i])); |
| 732 | g_free(list[i]); |
| 733 | } |
| 734 | g_free(list); |
| 735 | return true; |
| 736 | } |
| 737 | |
| 738 | // This is the callback from the debounce timer. |
| 739 | void OnDebouncedNotification() { |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 740 | DCHECK(task_runner_->BelongsToCurrentThread()); |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 741 | CHECK(notify_delegate_); |
| 742 | // Forward to a method on the proxy config service delegate object. |
| 743 | notify_delegate_->OnCheckProxyConfigSettings(); |
| 744 | } |
| 745 | |
| 746 | void OnChangeNotification() { |
| 747 | // We don't use Reset() because the timer may not yet be running. |
| 748 | // (In that case Stop() is a no-op.) |
| 749 | debounce_timer_.Stop(); |
[email protected] | d323a17 | 2011-09-02 18:23:02 | [diff] [blame] | 750 | debounce_timer_.Start(FROM_HERE, |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 751 | base::TimeDelta::FromMilliseconds(kDebounceTimeoutMilliseconds), |
| 752 | this, &SettingGetterImplGSettings::OnDebouncedNotification); |
| 753 | } |
| 754 | |
| 755 | // gsettings notification callback, dispatched on the default glib main loop. |
| 756 | static void OnGSettingsChangeNotification(GSettings* client, gchar* key, |
| 757 | gpointer user_data) { |
| 758 | VLOG(1) << "gsettings change notification for key " << key; |
| 759 | // We don't track which key has changed, just that something did change. |
| 760 | SettingGetterImplGSettings* setting_getter = |
| 761 | reinterpret_cast<SettingGetterImplGSettings*>(user_data); |
| 762 | setting_getter->OnChangeNotification(); |
| 763 | } |
| 764 | |
| 765 | GSettings* client_; |
| 766 | GSettings* http_client_; |
| 767 | GSettings* https_client_; |
| 768 | GSettings* ftp_client_; |
| 769 | GSettings* socks_client_; |
| 770 | ProxyConfigServiceLinux::Delegate* notify_delegate_; |
| 771 | base::OneShotTimer<SettingGetterImplGSettings> debounce_timer_; |
| 772 | |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 773 | // Task runner for the thread that we make gsettings calls on. It should |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 774 | // be the UI thread and all our methods should be called on this |
| 775 | // thread. Only for assertions. |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 776 | scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 777 | |
[email protected] | 3fc24f5 | 2012-11-30 21:22:34 | [diff] [blame] | 778 | LibGioLoader libgio_loader_; |
| 779 | |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 780 | DISALLOW_COPY_AND_ASSIGN(SettingGetterImplGSettings); |
| 781 | }; |
| 782 | |
| 783 | bool SettingGetterImplGSettings::LoadAndCheckVersion( |
| 784 | base::Environment* env) { |
| 785 | // LoadAndCheckVersion() must be called *before* Init()! |
| 786 | DCHECK(!client_); |
| 787 | |
| 788 | // The APIs to query gsettings were introduced after the minimum glib |
| 789 | // version we target, so we can't link directly against them. We load them |
| 790 | // dynamically at runtime, and if they don't exist, return false here. (We |
| 791 | // support linking directly via gyp flags though.) Additionally, even when |
| 792 | // they are present, we do two additional checks to make sure we should use |
| 793 | // them and not gconf. First, we attempt to load the schema for proxy |
| 794 | // settings. Second, we check for the program that was used in older |
| 795 | // versions of GNOME to configure proxy settings, and return false if it |
| 796 | // exists. Some distributions (e.g. Ubuntu 11.04) have the API and schema |
| 797 | // but don't use gsettings for proxy settings, but they do have the old |
| 798 | // binary, so we detect these systems that way. |
| 799 | |
[email protected] | ae82cea | 2012-12-06 22:52:10 | [diff] [blame] | 800 | { |
| 801 | // TODO(phajdan.jr): Redesign the code to load library on different thread. |
| 802 | base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 803 | |
| 804 | // Try also without .0 at the end; on some systems this may be required. |
| 805 | if (!libgio_loader_.Load("libgio-2.0.so.0") && |
| 806 | !libgio_loader_.Load("libgio-2.0.so")) { |
| 807 | VLOG(1) << "Cannot load gio library. Will fall back to gconf."; |
| 808 | return false; |
| 809 | } |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 810 | } |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 811 | |
[email protected] | 4bbb72d | 2014-06-06 18:05:51 | [diff] [blame] | 812 | GSettings* client = NULL; |
| 813 | if (SchemaExists(kProxyGConfSchema)) { |
| 814 | ANNOTATE_SCOPED_MEMORY_LEAK; // http://crbug.com/380782 |
| 815 | client = libgio_loader_.g_settings_new(kProxyGConfSchema); |
| 816 | } |
| 817 | if (!client) { |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 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) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 835 | base::FilePath file(paths[i]); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 836 | if (base::PathExists(file.Append("gnome-network-properties"))) { |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 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] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 848 | // 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] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 851 | class SettingGetterImplKDE : public ProxyConfigServiceLinux::SettingGetter, |
| 852 | public base::MessagePumpLibevent::Watcher { |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 853 | public: |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 854 | explicit SettingGetterImplKDE(base::Environment* env_var_getter) |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 855 | : inotify_fd_(-1), notify_delegate_(NULL), indirect_manual_(false), |
[email protected] | a48bf4a | 2010-06-14 18:24:53 | [diff] [blame] | 856 | auto_no_pac_(false), reversed_bypass_list_(false), |
[email protected] | f18fde2 | 2010-05-18 23:49:54 | [diff] [blame] | 857 | env_var_getter_(env_var_getter), file_loop_(NULL) { |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 858 | // This has to be called on the UI thread (http://crbug.com/69057). |
| 859 | base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 860 | |
[email protected] | f18fde2 | 2010-05-18 23:49:54 | [diff] [blame] | 861 | // Derive the location of the kde config dir from the environment. |
[email protected] | 92d2dc8 | 2010-04-08 17:49:59 | [diff] [blame] | 862 | std::string home; |
[email protected] | 3ba7e08 | 2010-08-07 02:57:59 | [diff] [blame] | 863 | if (env_var_getter->GetVar("KDEHOME", &home) && !home.empty()) { |
[email protected] | 2e8cfe2 | 2010-06-12 00:26:24 | [diff] [blame] | 864 | // $KDEHOME is set. Use it unconditionally. |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 865 | kde_config_dir_ = KDEHomeToConfigPath(base::FilePath(home)); |
[email protected] | 92d2dc8 | 2010-04-08 17:49:59 | [diff] [blame] | 866 | } else { |
[email protected] | 2e8cfe2 | 2010-06-12 00:26:24 | [diff] [blame] | 867 | // $KDEHOME is unset. Try to figure out what to use. This seems to be |
[email protected] | 92d2dc8 | 2010-04-08 17:49:59 | [diff] [blame] | 868 | // the common case on most distributions. |
[email protected] | 3ba7e08 | 2010-08-07 02:57:59 | [diff] [blame] | 869 | if (!env_var_getter->GetVar(base::env_vars::kHome, &home)) |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 870 | // User has no $HOME? Give up. Later we'll report the failure. |
| 871 | return; |
[email protected] | 6b0349ef | 2010-10-16 04:56:06 | [diff] [blame] | 872 | if (base::nix::GetDesktopEnvironment(env_var_getter) == |
| 873 | base::nix::DESKTOP_ENVIRONMENT_KDE3) { |
[email protected] | 92d2dc8 | 2010-04-08 17:49:59 | [diff] [blame] | 874 | // KDE3 always uses .kde for its configuration. |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 875 | base::FilePath kde_path = base::FilePath(home).Append(".kde"); |
[email protected] | 92d2dc8 | 2010-04-08 17:49:59 | [diff] [blame] | 876 | kde_config_dir_ = KDEHomeToConfigPath(kde_path); |
| 877 | } else { |
| 878 | // Some distributions patch KDE4 to use .kde4 instead of .kde, so that |
[email protected] | fad9c8a5 | 2010-06-10 22:30:53 | [diff] [blame] | 879 | // 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] | 92d2dc8 | 2010-04-08 17:49:59 | [diff] [blame] | 883 | // 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] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 888 | base::FilePath kde3_path = base::FilePath(home).Append(".kde"); |
| 889 | base::FilePath kde3_config = KDEHomeToConfigPath(kde3_path); |
| 890 | base::FilePath kde4_path = base::FilePath(home).Append(".kde4"); |
| 891 | base::FilePath kde4_config = KDEHomeToConfigPath(kde4_path); |
[email protected] | fad9c8a5 | 2010-06-10 22:30:53 | [diff] [blame] | 892 | bool use_kde4 = false; |
[email protected] | dcd1661 | 2013-07-15 20:18:09 | [diff] [blame] | 893 | if (base::DirectoryExists(kde4_path)) { |
[email protected] | 54124ed0 | 2014-01-07 10:06:58 | [diff] [blame] | 894 | base::File::Info kde3_info; |
| 895 | base::File::Info kde4_info; |
[email protected] | 9eae4e6 | 2013-12-04 20:56:49 | [diff] [blame] | 896 | if (base::GetFileInfo(kde4_config, &kde4_info)) { |
| 897 | if (base::GetFileInfo(kde3_config, &kde3_info)) { |
[email protected] | fad9c8a5 | 2010-06-10 22:30:53 | [diff] [blame] | 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] | 92d2dc8 | 2010-04-08 17:49:59 | [diff] [blame] | 905 | kde_config_dir_ = KDEHomeToConfigPath(kde4_path); |
| 906 | } else { |
[email protected] | fad9c8a5 | 2010-06-10 22:30:53 | [diff] [blame] | 907 | kde_config_dir_ = KDEHomeToConfigPath(kde3_path); |
[email protected] | 92d2dc8 | 2010-04-08 17:49:59 | [diff] [blame] | 908 | } |
| 909 | } |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 910 | } |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 911 | } |
| 912 | |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 913 | virtual ~SettingGetterImplKDE() { |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 914 | // 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] | d306614 | 2011-05-10 02:36:20 | [diff] [blame] | 922 | ShutDown(); |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 923 | DCHECK(inotify_fd_ < 0); |
| 924 | } |
| 925 | |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 926 | virtual bool Init(base::SingleThreadTaskRunner* glib_thread_task_runner, |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 927 | base::MessageLoopForIO* file_loop) OVERRIDE { |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 928 | // This has to be called on the UI thread (http://crbug.com/69057). |
| 929 | base::ThreadRestrictions::ScopedAllowIO allow_io; |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 930 | DCHECK(inotify_fd_ < 0); |
| 931 | inotify_fd_ = inotify_init(); |
| 932 | if (inotify_fd_ < 0) { |
[email protected] | 57b76567 | 2009-10-13 18:27:40 | [diff] [blame] | 933 | PLOG(ERROR) << "inotify_init failed"; |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 934 | return false; |
| 935 | } |
| 936 | int flags = fcntl(inotify_fd_, F_GETFL); |
| 937 | if (fcntl(inotify_fd_, F_SETFL, flags | O_NONBLOCK) < 0) { |
[email protected] | 57b76567 | 2009-10-13 18:27:40 | [diff] [blame] | 938 | PLOG(ERROR) << "fcntl failed"; |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 939 | 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] | d306614 | 2011-05-10 02:36:20 | [diff] [blame] | 945 | // since we will need to have it for SetUpAndFetchInitialConfig(). |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 946 | UpdateCachedSettings(); |
| 947 | return true; |
| 948 | } |
| 949 | |
[email protected] | 749bf5c | 2012-09-17 03:15:21 | [diff] [blame] | 950 | virtual void ShutDown() OVERRIDE { |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 951 | if (inotify_fd_ >= 0) { |
| 952 | ResetCachedSettings(); |
| 953 | inotify_watcher_.StopWatchingFileDescriptor(); |
| 954 | close(inotify_fd_); |
| 955 | inotify_fd_ = -1; |
| 956 | } |
| 957 | } |
| 958 | |
[email protected] | 749bf5c | 2012-09-17 03:15:21 | [diff] [blame] | 959 | virtual bool SetUpNotifications( |
| 960 | ProxyConfigServiceLinux::Delegate* delegate) OVERRIDE { |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 961 | DCHECK(inotify_fd_ >= 0); |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 962 | DCHECK(base::MessageLoop::current() == file_loop_); |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 963 | // We can't just watch the kioslaverc file directly, since KDE will write |
| 964 | // a new copy of it and then rename it whenever settings are changed and |
| 965 | // inotify watches inodes (so we'll be watching the old deleted file after |
| 966 | // the first change, and it will never change again). So, we watch the |
| 967 | // directory instead. We then act only on changes to the kioslaverc entry. |
| 968 | if (inotify_add_watch(inotify_fd_, kde_config_dir_.value().c_str(), |
| 969 | IN_MODIFY | IN_MOVED_TO) < 0) |
| 970 | return false; |
| 971 | notify_delegate_ = delegate; |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 972 | if (!file_loop_->WatchFileDescriptor(inotify_fd_, |
| 973 | true, |
| 974 | base::MessageLoopForIO::WATCH_READ, |
| 975 | &inotify_watcher_, |
| 976 | this)) |
[email protected] | d306614 | 2011-05-10 02:36:20 | [diff] [blame] | 977 | return false; |
| 978 | // Simulate a change to avoid possibly losing updates before this point. |
| 979 | OnChangeNotification(); |
| 980 | return true; |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 981 | } |
| 982 | |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 983 | virtual base::SingleThreadTaskRunner* GetNotificationTaskRunner() OVERRIDE { |
[email protected] | 198b590 | 2013-06-27 10:36:11 | [diff] [blame] | 984 | return file_loop_ ? file_loop_->message_loop_proxy().get() : NULL; |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 985 | } |
| 986 | |
[email protected] | b160df3 | 2012-02-06 20:39:41 | [diff] [blame] | 987 | // Implement base::MessagePumpLibevent::Watcher. |
[email protected] | 749bf5c | 2012-09-17 03:15:21 | [diff] [blame] | 988 | virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE { |
[email protected] | d2e6d59 | 2012-02-03 21:49:04 | [diff] [blame] | 989 | DCHECK_EQ(fd, inotify_fd_); |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 990 | DCHECK(base::MessageLoop::current() == file_loop_); |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 991 | OnChangeNotification(); |
| 992 | } |
[email protected] | 749bf5c | 2012-09-17 03:15:21 | [diff] [blame] | 993 | virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE { |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 994 | NOTREACHED(); |
| 995 | } |
| 996 | |
[email protected] | db8ff91 | 2012-06-12 23:32:51 | [diff] [blame] | 997 | virtual ProxyConfigSource GetConfigSource() OVERRIDE { |
| 998 | return PROXY_CONFIG_SOURCE_KDE; |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 999 | } |
| 1000 | |
[email protected] | c4c1b48 | 2011-07-22 17:24:26 | [diff] [blame] | 1001 | virtual bool GetString(StringSetting key, std::string* result) OVERRIDE { |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1002 | string_map_type::iterator it = string_table_.find(key); |
| 1003 | if (it == string_table_.end()) |
| 1004 | return false; |
| 1005 | *result = it->second; |
| 1006 | return true; |
| 1007 | } |
[email protected] | c4c1b48 | 2011-07-22 17:24:26 | [diff] [blame] | 1008 | virtual bool GetBool(BoolSetting key, bool* result) OVERRIDE { |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1009 | // We don't ever have any booleans. |
| 1010 | return false; |
| 1011 | } |
[email protected] | c4c1b48 | 2011-07-22 17:24:26 | [diff] [blame] | 1012 | virtual bool GetInt(IntSetting key, int* result) OVERRIDE { |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1013 | // We don't ever have any integers. (See AddProxy() below about ports.) |
| 1014 | return false; |
| 1015 | } |
[email protected] | 6b5fe74 | 2011-05-20 21:46:48 | [diff] [blame] | 1016 | virtual bool GetStringList(StringListSetting key, |
[email protected] | c4c1b48 | 2011-07-22 17:24:26 | [diff] [blame] | 1017 | std::vector<std::string>* result) OVERRIDE { |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1018 | strings_map_type::iterator it = strings_table_.find(key); |
| 1019 | if (it == strings_table_.end()) |
| 1020 | return false; |
| 1021 | *result = it->second; |
| 1022 | return true; |
| 1023 | } |
| 1024 | |
[email protected] | c4c1b48 | 2011-07-22 17:24:26 | [diff] [blame] | 1025 | virtual bool BypassListIsReversed() OVERRIDE { |
[email protected] | a48bf4a | 2010-06-14 18:24:53 | [diff] [blame] | 1026 | return reversed_bypass_list_; |
| 1027 | } |
| 1028 | |
[email protected] | c4c1b48 | 2011-07-22 17:24:26 | [diff] [blame] | 1029 | virtual bool MatchHostsUsingSuffixMatching() OVERRIDE { |
[email protected] | 1a59719 | 2010-07-09 16:58:38 | [diff] [blame] | 1030 | return true; |
| 1031 | } |
| 1032 | |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1033 | private: |
| 1034 | void ResetCachedSettings() { |
| 1035 | string_table_.clear(); |
| 1036 | strings_table_.clear(); |
| 1037 | indirect_manual_ = false; |
| 1038 | auto_no_pac_ = false; |
[email protected] | a48bf4a | 2010-06-14 18:24:53 | [diff] [blame] | 1039 | reversed_bypass_list_ = false; |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1040 | } |
| 1041 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1042 | base::FilePath KDEHomeToConfigPath(const base::FilePath& kde_home) { |
[email protected] | 92d2dc8 | 2010-04-08 17:49:59 | [diff] [blame] | 1043 | return kde_home.Append("share").Append("config"); |
| 1044 | } |
| 1045 | |
[email protected] | 6b5fe74 | 2011-05-20 21:46:48 | [diff] [blame] | 1046 | void AddProxy(StringSetting host_key, const std::string& value) { |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1047 | if (value.empty() || value.substr(0, 3) == "//:") |
| 1048 | // No proxy. |
| 1049 | return; |
[email protected] | 4b90c20 | 2012-04-24 23:27:55 | [diff] [blame] | 1050 | size_t space = value.find(' '); |
| 1051 | if (space != std::string::npos) { |
| 1052 | // Newer versions of KDE use a space rather than a colon to separate the |
| 1053 | // port number from the hostname. If we find this, we need to convert it. |
| 1054 | std::string fixed = value; |
| 1055 | fixed[space] = ':'; |
| 1056 | string_table_[host_key] = fixed; |
| 1057 | } else { |
| 1058 | // We don't need to parse the port number out; GetProxyFromSettings() |
| 1059 | // would only append it right back again. So we just leave the port |
| 1060 | // number right in the host string. |
| 1061 | string_table_[host_key] = value; |
| 1062 | } |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1063 | } |
| 1064 | |
[email protected] | 6b5fe74 | 2011-05-20 21:46:48 | [diff] [blame] | 1065 | void AddHostList(StringListSetting key, const std::string& value) { |
[email protected] | f18fde2 | 2010-05-18 23:49:54 | [diff] [blame] | 1066 | std::vector<std::string> tokens; |
[email protected] | f4ebe77 | 2013-02-02 00:21:39 | [diff] [blame] | 1067 | base::StringTokenizer tk(value, ", "); |
[email protected] | f18fde2 | 2010-05-18 23:49:54 | [diff] [blame] | 1068 | while (tk.GetNext()) { |
| 1069 | std::string token = tk.token(); |
| 1070 | if (!token.empty()) |
| 1071 | tokens.push_back(token); |
| 1072 | } |
| 1073 | strings_table_[key] = tokens; |
| 1074 | } |
| 1075 | |
[email protected] | 9a3d8d4 | 2009-09-03 17:01:46 | [diff] [blame] | 1076 | void AddKDESetting(const std::string& key, const std::string& value) { |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1077 | if (key == "ProxyType") { |
| 1078 | const char* mode = "none"; |
| 1079 | indirect_manual_ = false; |
| 1080 | auto_no_pac_ = false; |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 1081 | int int_value; |
| 1082 | base::StringToInt(value, &int_value); |
| 1083 | switch (int_value) { |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1084 | case 0: // No proxy, or maybe kioslaverc syntax error. |
| 1085 | break; |
| 1086 | case 1: // Manual configuration. |
| 1087 | mode = "manual"; |
| 1088 | break; |
| 1089 | case 2: // PAC URL. |
| 1090 | mode = "auto"; |
| 1091 | break; |
| 1092 | case 3: // WPAD. |
| 1093 | mode = "auto"; |
| 1094 | auto_no_pac_ = true; |
| 1095 | break; |
| 1096 | case 4: // Indirect manual via environment variables. |
| 1097 | mode = "manual"; |
| 1098 | indirect_manual_ = true; |
| 1099 | break; |
| 1100 | } |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 1101 | string_table_[PROXY_MODE] = mode; |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1102 | } else if (key == "Proxy Config Script") { |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 1103 | string_table_[PROXY_AUTOCONF_URL] = value; |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1104 | } else if (key == "httpProxy") { |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 1105 | AddProxy(PROXY_HTTP_HOST, value); |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1106 | } else if (key == "httpsProxy") { |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 1107 | AddProxy(PROXY_HTTPS_HOST, value); |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1108 | } else if (key == "ftpProxy") { |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 1109 | AddProxy(PROXY_FTP_HOST, value); |
[email protected] | bfeb723 | 2012-06-08 00:58:37 | [diff] [blame] | 1110 | } else if (key == "socksProxy") { |
| 1111 | // Older versions of KDE configure SOCKS in a weird way involving |
| 1112 | // LD_PRELOAD and a library that intercepts network calls to SOCKSify |
| 1113 | // them. We don't support it. KDE 4.8 added a proper SOCKS setting. |
| 1114 | AddProxy(PROXY_SOCKS_HOST, value); |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1115 | } else if (key == "ReversedException") { |
| 1116 | // We count "true" or any nonzero number as true, otherwise false. |
| 1117 | // Note that if the value is not actually numeric StringToInt() |
| 1118 | // will return 0, which we count as false. |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 1119 | int int_value; |
| 1120 | base::StringToInt(value, &int_value); |
| 1121 | reversed_bypass_list_ = (value == "true" || int_value); |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1122 | } else if (key == "NoProxyFor") { |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 1123 | AddHostList(PROXY_IGNORE_HOSTS, value); |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1124 | } else if (key == "AuthMode") { |
| 1125 | // Check for authentication, just so we can warn. |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 1126 | int mode; |
| 1127 | base::StringToInt(value, &mode); |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1128 | if (mode) { |
| 1129 | // ProxyConfig does not support authentication parameters, but |
| 1130 | // Chrome will prompt for the password later. So we ignore this. |
| 1131 | LOG(WARNING) << |
| 1132 | "Proxy authentication parameters ignored, see bug 16709"; |
| 1133 | } |
| 1134 | } |
| 1135 | } |
| 1136 | |
[email protected] | 6b5fe74 | 2011-05-20 21:46:48 | [diff] [blame] | 1137 | void ResolveIndirect(StringSetting key) { |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1138 | string_map_type::iterator it = string_table_.find(key); |
| 1139 | if (it != string_table_.end()) { |
[email protected] | f18fde2 | 2010-05-18 23:49:54 | [diff] [blame] | 1140 | std::string value; |
[email protected] | 3ba7e08 | 2010-08-07 02:57:59 | [diff] [blame] | 1141 | if (env_var_getter_->GetVar(it->second.c_str(), &value)) |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1142 | it->second = value; |
[email protected] | 8425adc0 | 2010-04-18 17:45:31 | [diff] [blame] | 1143 | else |
| 1144 | string_table_.erase(it); |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1145 | } |
| 1146 | } |
| 1147 | |
[email protected] | 6b5fe74 | 2011-05-20 21:46:48 | [diff] [blame] | 1148 | void ResolveIndirectList(StringListSetting key) { |
[email protected] | f18fde2 | 2010-05-18 23:49:54 | [diff] [blame] | 1149 | strings_map_type::iterator it = strings_table_.find(key); |
| 1150 | if (it != strings_table_.end()) { |
| 1151 | std::string value; |
| 1152 | if (!it->second.empty() && |
[email protected] | 3ba7e08 | 2010-08-07 02:57:59 | [diff] [blame] | 1153 | env_var_getter_->GetVar(it->second[0].c_str(), &value)) |
[email protected] | f18fde2 | 2010-05-18 23:49:54 | [diff] [blame] | 1154 | AddHostList(key, value); |
| 1155 | else |
| 1156 | strings_table_.erase(it); |
| 1157 | } |
| 1158 | } |
| 1159 | |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1160 | // The settings in kioslaverc could occur in any order, but some affect |
| 1161 | // others. Rather than read the whole file in and then query them in an |
| 1162 | // order that allows us to handle that, we read the settings in whatever |
| 1163 | // order they occur and do any necessary tweaking after we finish. |
| 1164 | void ResolveModeEffects() { |
| 1165 | if (indirect_manual_) { |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 1166 | ResolveIndirect(PROXY_HTTP_HOST); |
| 1167 | ResolveIndirect(PROXY_HTTPS_HOST); |
| 1168 | ResolveIndirect(PROXY_FTP_HOST); |
| 1169 | ResolveIndirectList(PROXY_IGNORE_HOSTS); |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1170 | } |
| 1171 | if (auto_no_pac_) { |
| 1172 | // Remove the PAC URL; we're not supposed to use it. |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 1173 | string_table_.erase(PROXY_AUTOCONF_URL); |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1174 | } |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | // Reads kioslaverc one line at a time and calls AddKDESetting() to add |
| 1178 | // each relevant name-value pair to the appropriate value table. |
| 1179 | void UpdateCachedSettings() { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1180 | base::FilePath kioslaverc = kde_config_dir_.Append("kioslaverc"); |
[email protected] | b9b4a57 | 2014-03-17 23:11:12 | [diff] [blame] | 1181 | base::ScopedFILE input(base::OpenFile(kioslaverc, "r")); |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1182 | if (!input.get()) |
| 1183 | return; |
| 1184 | ResetCachedSettings(); |
| 1185 | bool in_proxy_settings = false; |
| 1186 | bool line_too_long = false; |
[email protected] | 9a3d8d4 | 2009-09-03 17:01:46 | [diff] [blame] | 1187 | char line[BUFFER_SIZE]; |
| 1188 | // fgets() will return NULL on EOF or error. |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1189 | while (fgets(line, sizeof(line), input.get())) { |
| 1190 | // fgets() guarantees the line will be properly terminated. |
| 1191 | size_t length = strlen(line); |
| 1192 | if (!length) |
| 1193 | continue; |
| 1194 | // This should be true even with CRLF endings. |
| 1195 | if (line[length - 1] != '\n') { |
| 1196 | line_too_long = true; |
| 1197 | continue; |
| 1198 | } |
| 1199 | if (line_too_long) { |
| 1200 | // The previous line had no line ending, but this done does. This is |
| 1201 | // the end of the line that was too long, so warn here and skip it. |
| 1202 | LOG(WARNING) << "skipped very long line in " << kioslaverc.value(); |
| 1203 | line_too_long = false; |
| 1204 | continue; |
| 1205 | } |
| 1206 | // Remove the LF at the end, and the CR if there is one. |
| 1207 | line[--length] = '\0'; |
| 1208 | if (length && line[length - 1] == '\r') |
| 1209 | line[--length] = '\0'; |
| 1210 | // Now parse the line. |
| 1211 | if (line[0] == '[') { |
| 1212 | // Switching sections. All we care about is whether this is |
| 1213 | // the (a?) proxy settings section, for both KDE3 and KDE4. |
| 1214 | in_proxy_settings = !strncmp(line, "[Proxy Settings]", 16); |
| 1215 | } else if (in_proxy_settings) { |
| 1216 | // A regular line, in the (a?) proxy settings section. |
[email protected] | 9a3d8d4 | 2009-09-03 17:01:46 | [diff] [blame] | 1217 | char* split = strchr(line, '='); |
| 1218 | // Skip this line if it does not contain an = sign. |
| 1219 | if (!split) |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1220 | continue; |
[email protected] | 9a3d8d4 | 2009-09-03 17:01:46 | [diff] [blame] | 1221 | // Split the line on the = and advance |split|. |
| 1222 | *(split++) = 0; |
| 1223 | std::string key = line; |
| 1224 | std::string value = split; |
[email protected] | 8af69c6c | 2014-03-03 19:05:31 | [diff] [blame] | 1225 | base::TrimWhitespaceASCII(key, base::TRIM_ALL, &key); |
| 1226 | base::TrimWhitespaceASCII(value, base::TRIM_ALL, &value); |
[email protected] | 9a3d8d4 | 2009-09-03 17:01:46 | [diff] [blame] | 1227 | // Skip this line if the key name is empty. |
| 1228 | if (key.empty()) |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1229 | continue; |
| 1230 | // Is the value name localized? |
[email protected] | 9a3d8d4 | 2009-09-03 17:01:46 | [diff] [blame] | 1231 | if (key[key.length() - 1] == ']') { |
| 1232 | // Find the matching bracket. |
| 1233 | length = key.rfind('['); |
| 1234 | // Skip this line if the localization indicator is malformed. |
| 1235 | if (length == std::string::npos) |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1236 | continue; |
| 1237 | // Trim the localization indicator off. |
[email protected] | 9a3d8d4 | 2009-09-03 17:01:46 | [diff] [blame] | 1238 | key.resize(length); |
| 1239 | // Remove any resulting trailing whitespace. |
[email protected] | 8af69c6c | 2014-03-03 19:05:31 | [diff] [blame] | 1240 | base::TrimWhitespaceASCII(key, base::TRIM_TRAILING, &key); |
[email protected] | 9a3d8d4 | 2009-09-03 17:01:46 | [diff] [blame] | 1241 | // Skip this line if the key name is now empty. |
| 1242 | if (key.empty()) |
| 1243 | continue; |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1244 | } |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1245 | // Now fill in the tables. |
[email protected] | 9a3d8d4 | 2009-09-03 17:01:46 | [diff] [blame] | 1246 | AddKDESetting(key, value); |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1247 | } |
| 1248 | } |
| 1249 | if (ferror(input.get())) |
| 1250 | LOG(ERROR) << "error reading " << kioslaverc.value(); |
| 1251 | ResolveModeEffects(); |
| 1252 | } |
| 1253 | |
| 1254 | // This is the callback from the debounce timer. |
| 1255 | void OnDebouncedNotification() { |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 1256 | DCHECK(base::MessageLoop::current() == file_loop_); |
[email protected] | b30a3f5 | 2010-10-16 01:05:46 | [diff] [blame] | 1257 | VLOG(1) << "inotify change notification for kioslaverc"; |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1258 | UpdateCachedSettings(); |
[email protected] | 961ac94 | 2011-04-28 18:18:14 | [diff] [blame] | 1259 | CHECK(notify_delegate_); |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1260 | // Forward to a method on the proxy config service delegate object. |
| 1261 | notify_delegate_->OnCheckProxyConfigSettings(); |
| 1262 | } |
| 1263 | |
| 1264 | // Called by OnFileCanReadWithoutBlocking() on the file thread. Reads |
| 1265 | // from the inotify file descriptor and starts up a debounce timer if |
| 1266 | // an event for kioslaverc is seen. |
| 1267 | void OnChangeNotification() { |
[email protected] | d2e6d59 | 2012-02-03 21:49:04 | [diff] [blame] | 1268 | DCHECK_GE(inotify_fd_, 0); |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 1269 | DCHECK(base::MessageLoop::current() == file_loop_); |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1270 | char event_buf[(sizeof(inotify_event) + NAME_MAX + 1) * 4]; |
| 1271 | bool kioslaverc_touched = false; |
| 1272 | ssize_t r; |
| 1273 | while ((r = read(inotify_fd_, event_buf, sizeof(event_buf))) > 0) { |
| 1274 | // inotify returns variable-length structures, which is why we have |
| 1275 | // this strange-looking loop instead of iterating through an array. |
| 1276 | char* event_ptr = event_buf; |
| 1277 | while (event_ptr < event_buf + r) { |
| 1278 | inotify_event* event = reinterpret_cast<inotify_event*>(event_ptr); |
| 1279 | // The kernel always feeds us whole events. |
[email protected] | b1f031dd | 2010-03-02 23:19:33 | [diff] [blame] | 1280 | CHECK_LE(event_ptr + sizeof(inotify_event), event_buf + r); |
| 1281 | CHECK_LE(event->name + event->len, event_buf + r); |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1282 | if (!strcmp(event->name, "kioslaverc")) |
| 1283 | kioslaverc_touched = true; |
| 1284 | // Advance the pointer just past the end of the filename. |
| 1285 | event_ptr = event->name + event->len; |
| 1286 | } |
| 1287 | // We keep reading even if |kioslaverc_touched| is true to drain the |
| 1288 | // inotify event queue. |
| 1289 | } |
| 1290 | if (!r) |
| 1291 | // Instead of returning -1 and setting errno to EINVAL if there is not |
| 1292 | // enough buffer space, older kernels (< 2.6.21) return 0. Simulate the |
| 1293 | // new behavior (EINVAL) so we can reuse the code below. |
| 1294 | errno = EINVAL; |
| 1295 | if (errno != EAGAIN) { |
[email protected] | 57b76567 | 2009-10-13 18:27:40 | [diff] [blame] | 1296 | PLOG(WARNING) << "error reading inotify file descriptor"; |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1297 | if (errno == EINVAL) { |
| 1298 | // Our buffer is not large enough to read the next event. This should |
| 1299 | // not happen (because its size is calculated to always be sufficiently |
| 1300 | // large), but if it does we'd warn continuously since |inotify_fd_| |
| 1301 | // would be forever ready to read. Close it and stop watching instead. |
| 1302 | LOG(ERROR) << "inotify failure; no longer watching kioslaverc!"; |
| 1303 | inotify_watcher_.StopWatchingFileDescriptor(); |
| 1304 | close(inotify_fd_); |
| 1305 | inotify_fd_ = -1; |
| 1306 | } |
| 1307 | } |
| 1308 | if (kioslaverc_touched) { |
| 1309 | // We don't use Reset() because the timer may not yet be running. |
| 1310 | // (In that case Stop() is a no-op.) |
| 1311 | debounce_timer_.Stop(); |
[email protected] | d323a17 | 2011-09-02 18:23:02 | [diff] [blame] | 1312 | debounce_timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds( |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1313 | kDebounceTimeoutMilliseconds), this, |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 1314 | &SettingGetterImplKDE::OnDebouncedNotification); |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1315 | } |
| 1316 | } |
| 1317 | |
[email protected] | 6b5fe74 | 2011-05-20 21:46:48 | [diff] [blame] | 1318 | typedef std::map<StringSetting, std::string> string_map_type; |
| 1319 | typedef std::map<StringListSetting, |
| 1320 | std::vector<std::string> > strings_map_type; |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1321 | |
| 1322 | int inotify_fd_; |
| 1323 | base::MessagePumpLibevent::FileDescriptorWatcher inotify_watcher_; |
| 1324 | ProxyConfigServiceLinux::Delegate* notify_delegate_; |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 1325 | base::OneShotTimer<SettingGetterImplKDE> debounce_timer_; |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1326 | base::FilePath kde_config_dir_; |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1327 | bool indirect_manual_; |
| 1328 | bool auto_no_pac_; |
[email protected] | a48bf4a | 2010-06-14 18:24:53 | [diff] [blame] | 1329 | bool reversed_bypass_list_; |
[email protected] | f18fde2 | 2010-05-18 23:49:54 | [diff] [blame] | 1330 | // We don't own |env_var_getter_|. It's safe to hold a pointer to it, since |
| 1331 | // both it and us are owned by ProxyConfigServiceLinux::Delegate, and have the |
| 1332 | // same lifetime. |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 1333 | base::Environment* env_var_getter_; |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1334 | |
| 1335 | // We cache these settings whenever we re-read the kioslaverc file. |
| 1336 | string_map_type string_table_; |
| 1337 | strings_map_type strings_table_; |
| 1338 | |
| 1339 | // Message loop of the file thread, for reading kioslaverc. If NULL, |
| 1340 | // just read it directly (for testing). We also handle inotify events |
| 1341 | // on this thread. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 1342 | base::MessageLoopForIO* file_loop_; |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1343 | |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 1344 | DISALLOW_COPY_AND_ASSIGN(SettingGetterImplKDE); |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1345 | }; |
| 1346 | |
| 1347 | } // namespace |
| 1348 | |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 1349 | bool ProxyConfigServiceLinux::Delegate::GetProxyFromSettings( |
[email protected] | 6b5fe74 | 2011-05-20 21:46:48 | [diff] [blame] | 1350 | SettingGetter::StringSetting host_key, |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 1351 | ProxyServer* result_server) { |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1352 | std::string host; |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 1353 | if (!setting_getter_->GetString(host_key, &host) || host.empty()) { |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1354 | // Unset or empty. |
| 1355 | return false; |
| 1356 | } |
| 1357 | // Check for an optional port. |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1358 | int port = 0; |
[email protected] | 6b5fe74 | 2011-05-20 21:46:48 | [diff] [blame] | 1359 | SettingGetter::IntSetting port_key = |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 1360 | SettingGetter::HostSettingToPortSetting(host_key); |
| 1361 | setting_getter_->GetInt(port_key, &port); |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1362 | if (port != 0) { |
| 1363 | // If a port is set and non-zero: |
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 1364 | host += ":" + base::IntToString(port); |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1365 | } |
[email protected] | 76960f3d | 2011-04-30 02:15:23 | [diff] [blame] | 1366 | |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 1367 | // gconf settings do not appear to distinguish between SOCKS version. We |
| 1368 | // default to version 5. For more information on this policy decision, see: |
[email protected] | 76960f3d | 2011-04-30 02:15:23 | [diff] [blame] | 1369 | // http://code.google.com/p/chromium/issues/detail?id=55912#c2 |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 1370 | ProxyServer::Scheme scheme = (host_key == SettingGetter::PROXY_SOCKS_HOST) ? |
| 1371 | ProxyServer::SCHEME_SOCKS5 : ProxyServer::SCHEME_HTTP; |
| 1372 | host = FixupProxyHostScheme(scheme, host); |
[email protected] | 87a102b | 2009-07-14 05:23:30 | [diff] [blame] | 1373 | ProxyServer proxy_server = ProxyServer::FromURI(host, |
| 1374 | ProxyServer::SCHEME_HTTP); |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1375 | if (proxy_server.is_valid()) { |
| 1376 | *result_server = proxy_server; |
| 1377 | return true; |
| 1378 | } |
| 1379 | return false; |
| 1380 | } |
| 1381 | |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 1382 | bool ProxyConfigServiceLinux::Delegate::GetConfigFromSettings( |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1383 | ProxyConfig* config) { |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1384 | std::string mode; |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 1385 | if (!setting_getter_->GetString(SettingGetter::PROXY_MODE, &mode)) { |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1386 | // We expect this to always be set, so if we don't see it then we |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 1387 | // probably have a gconf/gsettings problem, and so we don't have a valid |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1388 | // proxy config. |
| 1389 | return false; |
| 1390 | } |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1391 | if (mode == "none") { |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1392 | // Specifically specifies no proxy. |
| 1393 | return true; |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1394 | } |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1395 | |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1396 | if (mode == "auto") { |
[email protected] | aa3ac2cc | 2012-06-19 00:28:04 | [diff] [blame] | 1397 | // Automatic proxy config. |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1398 | std::string pac_url_str; |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 1399 | if (setting_getter_->GetString(SettingGetter::PROXY_AUTOCONF_URL, |
| 1400 | &pac_url_str)) { |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1401 | if (!pac_url_str.empty()) { |
[email protected] | aa3ac2cc | 2012-06-19 00:28:04 | [diff] [blame] | 1402 | // If the PAC URL is actually a file path, then put file:// in front. |
| 1403 | if (pac_url_str[0] == '/') |
| 1404 | pac_url_str = "file://" + pac_url_str; |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1405 | GURL pac_url(pac_url_str); |
| 1406 | if (!pac_url.is_valid()) |
| 1407 | return false; |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 1408 | config->set_pac_url(pac_url); |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1409 | return true; |
| 1410 | } |
| 1411 | } |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 1412 | config->set_auto_detect(true); |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1413 | return true; |
| 1414 | } |
| 1415 | |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1416 | if (mode != "manual") { |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1417 | // Mode is unrecognized. |
| 1418 | return false; |
| 1419 | } |
| 1420 | bool use_http_proxy; |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 1421 | if (setting_getter_->GetBool(SettingGetter::PROXY_USE_HTTP_PROXY, |
| 1422 | &use_http_proxy) |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1423 | && !use_http_proxy) { |
| 1424 | // Another master switch for some reason. If set to false, then no |
| 1425 | // proxy. But we don't panic if the key doesn't exist. |
| 1426 | return true; |
| 1427 | } |
| 1428 | |
| 1429 | bool same_proxy = false; |
| 1430 | // Indicates to use the http proxy for all protocols. This one may |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 1431 | // not exist (presumably on older versions); we assume false in that |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1432 | // case. |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 1433 | setting_getter_->GetBool(SettingGetter::PROXY_USE_SAME_PROXY, |
| 1434 | &same_proxy); |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1435 | |
[email protected] | 76960f3d | 2011-04-30 02:15:23 | [diff] [blame] | 1436 | ProxyServer proxy_for_http; |
| 1437 | ProxyServer proxy_for_https; |
| 1438 | ProxyServer proxy_for_ftp; |
| 1439 | ProxyServer socks_proxy; // (socks) |
| 1440 | |
| 1441 | // This counts how many of the above ProxyServers were defined and valid. |
| 1442 | size_t num_proxies_specified = 0; |
| 1443 | |
| 1444 | // Extract the per-scheme proxies. If we failed to parse it, or no proxy was |
| 1445 | // specified for the scheme, then the resulting ProxyServer will be invalid. |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 1446 | if (GetProxyFromSettings(SettingGetter::PROXY_HTTP_HOST, &proxy_for_http)) |
[email protected] | 76960f3d | 2011-04-30 02:15:23 | [diff] [blame] | 1447 | num_proxies_specified++; |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 1448 | if (GetProxyFromSettings(SettingGetter::PROXY_HTTPS_HOST, &proxy_for_https)) |
[email protected] | 76960f3d | 2011-04-30 02:15:23 | [diff] [blame] | 1449 | num_proxies_specified++; |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 1450 | if (GetProxyFromSettings(SettingGetter::PROXY_FTP_HOST, &proxy_for_ftp)) |
[email protected] | 76960f3d | 2011-04-30 02:15:23 | [diff] [blame] | 1451 | num_proxies_specified++; |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 1452 | if (GetProxyFromSettings(SettingGetter::PROXY_SOCKS_HOST, &socks_proxy)) |
[email protected] | 76960f3d | 2011-04-30 02:15:23 | [diff] [blame] | 1453 | num_proxies_specified++; |
| 1454 | |
| 1455 | if (same_proxy) { |
| 1456 | if (proxy_for_http.is_valid()) { |
| 1457 | // Use the http proxy for all schemes. |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 1458 | config->proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY; |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 1459 | config->proxy_rules().single_proxies.SetSingleProxyServer(proxy_for_http); |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1460 | } |
[email protected] | 76960f3d | 2011-04-30 02:15:23 | [diff] [blame] | 1461 | } else if (num_proxies_specified > 0) { |
| 1462 | if (socks_proxy.is_valid() && num_proxies_specified == 1) { |
| 1463 | // If the only proxy specified was for SOCKS, use it for all schemes. |
| 1464 | config->proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY; |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 1465 | config->proxy_rules().single_proxies.SetSingleProxyServer(socks_proxy); |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1466 | } else { |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 1467 | // Otherwise use the indicated proxies per-scheme. |
[email protected] | 76960f3d | 2011-04-30 02:15:23 | [diff] [blame] | 1468 | config->proxy_rules().type = |
| 1469 | ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME; |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 1470 | config->proxy_rules().proxies_for_http. |
| 1471 | SetSingleProxyServer(proxy_for_http); |
| 1472 | config->proxy_rules().proxies_for_https. |
| 1473 | SetSingleProxyServer(proxy_for_https); |
| 1474 | config->proxy_rules().proxies_for_ftp.SetSingleProxyServer(proxy_for_ftp); |
| 1475 | config->proxy_rules().fallback_proxies.SetSingleProxyServer(socks_proxy); |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1476 | } |
| 1477 | } |
| 1478 | |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 1479 | if (config->proxy_rules().empty()) { |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1480 | // Manual mode but we couldn't parse any rules. |
| 1481 | return false; |
| 1482 | } |
| 1483 | |
| 1484 | // Check for authentication, just so we can warn. |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1485 | bool use_auth = false; |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 1486 | setting_getter_->GetBool(SettingGetter::PROXY_USE_AUTHENTICATION, |
| 1487 | &use_auth); |
[email protected] | 62749f18 | 2009-07-15 13:16:54 | [diff] [blame] | 1488 | if (use_auth) { |
| 1489 | // ProxyConfig does not support authentication parameters, but |
| 1490 | // Chrome will prompt for the password later. So we ignore |
| 1491 | // /system/http_proxy/*auth* settings. |
| 1492 | LOG(WARNING) << "Proxy authentication parameters ignored, see bug 16709"; |
| 1493 | } |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1494 | |
| 1495 | // Now the bypass list. |
[email protected] | 7541206c | 2010-02-19 20:24:06 | [diff] [blame] | 1496 | std::vector<std::string> ignore_hosts_list; |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 1497 | config->proxy_rules().bypass_rules.Clear(); |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 1498 | if (setting_getter_->GetStringList(SettingGetter::PROXY_IGNORE_HOSTS, |
| 1499 | &ignore_hosts_list)) { |
[email protected] | a8185d0 | 2010-06-11 00:19:50 | [diff] [blame] | 1500 | std::vector<std::string>::const_iterator it(ignore_hosts_list.begin()); |
[email protected] | 1a59719 | 2010-07-09 16:58:38 | [diff] [blame] | 1501 | for (; it != ignore_hosts_list.end(); ++it) { |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 1502 | if (setting_getter_->MatchHostsUsingSuffixMatching()) { |
[email protected] | 1a59719 | 2010-07-09 16:58:38 | [diff] [blame] | 1503 | config->proxy_rules().bypass_rules. |
| 1504 | AddRuleFromStringUsingSuffixMatching(*it); |
| 1505 | } else { |
| 1506 | config->proxy_rules().bypass_rules.AddRuleFromString(*it); |
| 1507 | } |
| 1508 | } |
[email protected] | a8185d0 | 2010-06-11 00:19:50 | [diff] [blame] | 1509 | } |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1510 | // Note that there are no settings with semantics corresponding to |
[email protected] | 1a59719 | 2010-07-09 16:58:38 | [diff] [blame] | 1511 | // bypass of local names in GNOME. In KDE, "<local>" is supported |
| 1512 | // as a hostname rule. |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1513 | |
[email protected] | a48bf4a | 2010-06-14 18:24:53 | [diff] [blame] | 1514 | // KDE allows one to reverse the bypass rules. |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 1515 | config->proxy_rules().reverse_bypass = |
| 1516 | setting_getter_->BypassListIsReversed(); |
[email protected] | a48bf4a | 2010-06-14 18:24:53 | [diff] [blame] | 1517 | |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1518 | return true; |
| 1519 | } |
| 1520 | |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 1521 | ProxyConfigServiceLinux::Delegate::Delegate(base::Environment* env_var_getter) |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 1522 | : env_var_getter_(env_var_getter) { |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 1523 | // Figure out which SettingGetterImpl to use, if any. |
[email protected] | 6b0349ef | 2010-10-16 04:56:06 | [diff] [blame] | 1524 | switch (base::nix::GetDesktopEnvironment(env_var_getter)) { |
| 1525 | case base::nix::DESKTOP_ENVIRONMENT_GNOME: |
[email protected] | 9e6c9bde | 2012-07-17 23:40:17 | [diff] [blame] | 1526 | case base::nix::DESKTOP_ENVIRONMENT_UNITY: |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 1527 | #if defined(USE_GIO) |
| 1528 | { |
| 1529 | scoped_ptr<SettingGetterImplGSettings> gs_getter( |
| 1530 | new SettingGetterImplGSettings()); |
| 1531 | // We have to load symbols and check the GNOME version in use to decide |
| 1532 | // if we should use the gsettings getter. See LoadAndCheckVersion(). |
| 1533 | if (gs_getter->LoadAndCheckVersion(env_var_getter)) |
| 1534 | setting_getter_.reset(gs_getter.release()); |
| 1535 | } |
| 1536 | #endif |
[email protected] | 6de53d4 | 2010-11-09 07:33:19 | [diff] [blame] | 1537 | #if defined(USE_GCONF) |
[email protected] | 8c20e3d | 2011-05-19 21:03:57 | [diff] [blame] | 1538 | // Fall back on gconf if gsettings is unavailable or incorrect. |
| 1539 | if (!setting_getter_.get()) |
| 1540 | setting_getter_.reset(new SettingGetterImplGConf()); |
[email protected] | 6de53d4 | 2010-11-09 07:33:19 | [diff] [blame] | 1541 | #endif |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1542 | break; |
[email protected] | 6b0349ef | 2010-10-16 04:56:06 | [diff] [blame] | 1543 | case base::nix::DESKTOP_ENVIRONMENT_KDE3: |
| 1544 | case base::nix::DESKTOP_ENVIRONMENT_KDE4: |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 1545 | setting_getter_.reset(new SettingGetterImplKDE(env_var_getter)); |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1546 | break; |
[email protected] | 6b0349ef | 2010-10-16 04:56:06 | [diff] [blame] | 1547 | case base::nix::DESKTOP_ENVIRONMENT_XFCE: |
| 1548 | case base::nix::DESKTOP_ENVIRONMENT_OTHER: |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1549 | break; |
| 1550 | } |
| 1551 | } |
| 1552 | |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 1553 | ProxyConfigServiceLinux::Delegate::Delegate( |
| 1554 | base::Environment* env_var_getter, SettingGetter* setting_getter) |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 1555 | : env_var_getter_(env_var_getter), setting_getter_(setting_getter) { |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1556 | } |
| 1557 | |
[email protected] | d306614 | 2011-05-10 02:36:20 | [diff] [blame] | 1558 | void ProxyConfigServiceLinux::Delegate::SetUpAndFetchInitialConfig( |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 1559 | base::SingleThreadTaskRunner* glib_thread_task_runner, |
| 1560 | base::SingleThreadTaskRunner* io_thread_task_runner, |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 1561 | base::MessageLoopForIO* file_loop) { |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1562 | // We should be running on the default glib main loop thread right |
| 1563 | // now. gconf can only be accessed from this thread. |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 1564 | DCHECK(glib_thread_task_runner->BelongsToCurrentThread()); |
| 1565 | glib_thread_task_runner_ = glib_thread_task_runner; |
| 1566 | io_thread_task_runner_ = io_thread_task_runner; |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1567 | |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 1568 | // If we are passed a NULL |io_thread_task_runner| or |file_loop|, |
| 1569 | // then we don't set up proxy setting change notifications. This |
| 1570 | // should not be the usual case but is intended to simplify test |
| 1571 | // setups. |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1572 | if (!io_thread_task_runner_.get() || !file_loop) |
[email protected] | b30a3f5 | 2010-10-16 01:05:46 | [diff] [blame] | 1573 | VLOG(1) << "Monitoring of proxy setting changes is disabled"; |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1574 | |
| 1575 | // Fetch and cache the current proxy config. The config is left in |
[email protected] | 11965500 | 2010-07-23 06:02:40 | [diff] [blame] | 1576 | // cached_config_, where GetLatestProxyConfig() running on the IO thread |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1577 | // will expect to find it. This is safe to do because we return |
| 1578 | // before this ProxyConfigServiceLinux is passed on to |
| 1579 | // the ProxyService. |
[email protected] | d6cb85b | 2009-07-23 22:10:53 | [diff] [blame] | 1580 | |
| 1581 | // Note: It would be nice to prioritize environment variables |
[email protected] | 92d2dc8 | 2010-04-08 17:49:59 | [diff] [blame] | 1582 | // and only fall back to gconf if env vars were unset. But |
[email protected] | d6cb85b | 2009-07-23 22:10:53 | [diff] [blame] | 1583 | // gnome-terminal "helpfully" sets http_proxy and no_proxy, and it |
| 1584 | // does so even if the proxy mode is set to auto, which would |
| 1585 | // mislead us. |
| 1586 | |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1587 | bool got_config = false; |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 1588 | if (setting_getter_.get() && |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 1589 | setting_getter_->Init(glib_thread_task_runner, file_loop) && |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 1590 | GetConfigFromSettings(&cached_config_)) { |
[email protected] | d306614 | 2011-05-10 02:36:20 | [diff] [blame] | 1591 | cached_config_.set_id(1); // Mark it as valid. |
[email protected] | db8ff91 | 2012-06-12 23:32:51 | [diff] [blame] | 1592 | cached_config_.set_source(setting_getter_->GetConfigSource()); |
[email protected] | d306614 | 2011-05-10 02:36:20 | [diff] [blame] | 1593 | VLOG(1) << "Obtained proxy settings from " |
[email protected] | db8ff91 | 2012-06-12 23:32:51 | [diff] [blame] | 1594 | << ProxyConfigSourceToString(cached_config_.source()); |
[email protected] | d306614 | 2011-05-10 02:36:20 | [diff] [blame] | 1595 | |
| 1596 | // If gconf proxy mode is "none", meaning direct, then we take |
| 1597 | // that to be a valid config and will not check environment |
| 1598 | // variables. The alternative would have been to look for a proxy |
| 1599 | // whereever we can find one. |
| 1600 | got_config = true; |
| 1601 | |
| 1602 | // Keep a copy of the config for use from this thread for |
| 1603 | // comparison with updated settings when we get notifications. |
| 1604 | reference_config_ = cached_config_; |
| 1605 | reference_config_.set_id(1); // Mark it as valid. |
| 1606 | |
| 1607 | // We only set up notifications if we have IO and file loops available. |
| 1608 | // We do this after getting the initial configuration so that we don't have |
| 1609 | // to worry about cancelling it if the initial fetch above fails. Note that |
| 1610 | // setting up notifications has the side effect of simulating a change, so |
| 1611 | // that we won't lose any updates that may have happened after the initial |
| 1612 | // fetch and before setting up notifications. We'll detect the common case |
| 1613 | // of no changes in OnCheckProxyConfigSettings() (or sooner) and ignore it. |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 1614 | if (io_thread_task_runner && file_loop) { |
| 1615 | scoped_refptr<base::SingleThreadTaskRunner> required_loop = |
| 1616 | setting_getter_->GetNotificationTaskRunner(); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1617 | if (!required_loop.get() || required_loop->BelongsToCurrentThread()) { |
[email protected] | d306614 | 2011-05-10 02:36:20 | [diff] [blame] | 1618 | // In this case we are already on an acceptable thread. |
| 1619 | SetUpNotifications(); |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1620 | } else { |
[email protected] | d306614 | 2011-05-10 02:36:20 | [diff] [blame] | 1621 | // Post a task to set up notifications. We don't wait for success. |
[email protected] | 6af889c | 2011-10-06 23:11:41 | [diff] [blame] | 1622 | required_loop->PostTask(FROM_HERE, base::Bind( |
| 1623 | &ProxyConfigServiceLinux::Delegate::SetUpNotifications, this)); |
[email protected] | d6cb85b | 2009-07-23 22:10:53 | [diff] [blame] | 1624 | } |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1625 | } |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1626 | } |
[email protected] | d6cb85b | 2009-07-23 22:10:53 | [diff] [blame] | 1627 | |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1628 | if (!got_config) { |
[email protected] | d6cb85b | 2009-07-23 22:10:53 | [diff] [blame] | 1629 | // We fall back on environment variables. |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1630 | // |
[email protected] | d306614 | 2011-05-10 02:36:20 | [diff] [blame] | 1631 | // Consulting environment variables doesn't need to be done from the |
| 1632 | // default glib main loop, but it's a tiny enough amount of work. |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1633 | if (GetConfigFromEnv(&cached_config_)) { |
[email protected] | db8ff91 | 2012-06-12 23:32:51 | [diff] [blame] | 1634 | cached_config_.set_source(PROXY_CONFIG_SOURCE_ENV); |
[email protected] | d306614 | 2011-05-10 02:36:20 | [diff] [blame] | 1635 | cached_config_.set_id(1); // Mark it as valid. |
[email protected] | b30a3f5 | 2010-10-16 01:05:46 | [diff] [blame] | 1636 | VLOG(1) << "Obtained proxy settings from environment variables"; |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1637 | } |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1638 | } |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1639 | } |
| 1640 | |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 1641 | // Depending on the SettingGetter in use, this method will be called |
[email protected] | d306614 | 2011-05-10 02:36:20 | [diff] [blame] | 1642 | // on either the UI thread (GConf) or the file thread (KDE). |
| 1643 | void ProxyConfigServiceLinux::Delegate::SetUpNotifications() { |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 1644 | scoped_refptr<base::SingleThreadTaskRunner> required_loop = |
| 1645 | setting_getter_->GetNotificationTaskRunner(); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1646 | DCHECK(!required_loop.get() || required_loop->BelongsToCurrentThread()); |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 1647 | if (!setting_getter_->SetUpNotifications(this)) |
[email protected] | d306614 | 2011-05-10 02:36:20 | [diff] [blame] | 1648 | LOG(ERROR) << "Unable to set up proxy configuration change notifications"; |
| 1649 | } |
| 1650 | |
[email protected] | 11965500 | 2010-07-23 06:02:40 | [diff] [blame] | 1651 | void ProxyConfigServiceLinux::Delegate::AddObserver(Observer* observer) { |
| 1652 | observers_.AddObserver(observer); |
| 1653 | } |
| 1654 | |
| 1655 | void ProxyConfigServiceLinux::Delegate::RemoveObserver(Observer* observer) { |
| 1656 | observers_.RemoveObserver(observer); |
| 1657 | } |
| 1658 | |
[email protected] | 3a29593d | 2011-04-11 10:07:52 | [diff] [blame] | 1659 | ProxyConfigService::ConfigAvailability |
| 1660 | ProxyConfigServiceLinux::Delegate::GetLatestProxyConfig( |
| 1661 | ProxyConfig* config) { |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1662 | // This is called from the IO thread. |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1663 | DCHECK(!io_thread_task_runner_.get() || |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 1664 | io_thread_task_runner_->BelongsToCurrentThread()); |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1665 | |
| 1666 | // Simply return the last proxy configuration that glib_default_loop |
| 1667 | // notified us of. |
[email protected] | db8ff91 | 2012-06-12 23:32:51 | [diff] [blame] | 1668 | if (cached_config_.is_valid()) { |
| 1669 | *config = cached_config_; |
| 1670 | } else { |
| 1671 | *config = ProxyConfig::CreateDirect(); |
| 1672 | config->set_source(PROXY_CONFIG_SOURCE_SYSTEM_FAILED); |
| 1673 | } |
[email protected] | 11965500 | 2010-07-23 06:02:40 | [diff] [blame] | 1674 | |
[email protected] | 3a29593d | 2011-04-11 10:07:52 | [diff] [blame] | 1675 | // We return CONFIG_VALID to indicate that *config was filled in. It is always |
[email protected] | 11965500 | 2010-07-23 06:02:40 | [diff] [blame] | 1676 | // going to be available since we initialized eagerly on the UI thread. |
| 1677 | // TODO(eroman): do lazy initialization instead, so we no longer need |
| 1678 | // to construct ProxyConfigServiceLinux on the UI thread. |
| 1679 | // In which case, we may return false here. |
[email protected] | 3a29593d | 2011-04-11 10:07:52 | [diff] [blame] | 1680 | return CONFIG_VALID; |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1681 | } |
| 1682 | |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 1683 | // Depending on the SettingGetter in use, this method will be called |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1684 | // on either the UI thread (GConf) or the file thread (KDE). |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1685 | void ProxyConfigServiceLinux::Delegate::OnCheckProxyConfigSettings() { |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 1686 | scoped_refptr<base::SingleThreadTaskRunner> required_loop = |
| 1687 | setting_getter_->GetNotificationTaskRunner(); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1688 | DCHECK(!required_loop.get() || required_loop->BelongsToCurrentThread()); |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1689 | ProxyConfig new_config; |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 1690 | bool valid = GetConfigFromSettings(&new_config); |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1691 | if (valid) |
| 1692 | new_config.set_id(1); // mark it as valid |
| 1693 | |
[email protected] | 11965500 | 2010-07-23 06:02:40 | [diff] [blame] | 1694 | // See if it is different from what we had before. |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1695 | if (new_config.is_valid() != reference_config_.is_valid() || |
| 1696 | !new_config.Equals(reference_config_)) { |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 1697 | // Post a task to the IO thread with the new configuration, so it can |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1698 | // update |cached_config_|. |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 1699 | io_thread_task_runner_->PostTask(FROM_HERE, base::Bind( |
[email protected] | 6af889c | 2011-10-06 23:11:41 | [diff] [blame] | 1700 | &ProxyConfigServiceLinux::Delegate::SetNewProxyConfig, |
| 1701 | this, new_config)); |
[email protected] | d1f9d47 | 2009-08-13 19:59:30 | [diff] [blame] | 1702 | // Update the thread-private copy in |reference_config_| as well. |
| 1703 | reference_config_ = new_config; |
[email protected] | d306614 | 2011-05-10 02:36:20 | [diff] [blame] | 1704 | } else { |
| 1705 | VLOG(1) << "Detected no-op change to proxy settings. Doing nothing."; |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1706 | } |
| 1707 | } |
| 1708 | |
| 1709 | void ProxyConfigServiceLinux::Delegate::SetNewProxyConfig( |
| 1710 | const ProxyConfig& new_config) { |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 1711 | DCHECK(io_thread_task_runner_->BelongsToCurrentThread()); |
[email protected] | b30a3f5 | 2010-10-16 01:05:46 | [diff] [blame] | 1712 | VLOG(1) << "Proxy configuration changed"; |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1713 | cached_config_ = new_config; |
[email protected] | 3a29593d | 2011-04-11 10:07:52 | [diff] [blame] | 1714 | FOR_EACH_OBSERVER( |
| 1715 | Observer, observers_, |
| 1716 | OnProxyConfigChanged(new_config, ProxyConfigService::CONFIG_VALID)); |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1717 | } |
| 1718 | |
| 1719 | void ProxyConfigServiceLinux::Delegate::PostDestroyTask() { |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 1720 | if (!setting_getter_.get()) |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1721 | return; |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 1722 | scoped_refptr<base::SingleThreadTaskRunner> shutdown_loop = |
| 1723 | setting_getter_->GetNotificationTaskRunner(); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1724 | if (!shutdown_loop.get() || shutdown_loop->BelongsToCurrentThread()) { |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1725 | // Already on the right thread, call directly. |
| 1726 | // This is the case for the unittests. |
| 1727 | OnDestroy(); |
| 1728 | } else { |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1729 | // Post to shutdown thread. Note that on browser shutdown, we may quit |
| 1730 | // this MessageLoop and exit the program before ever running this. |
[email protected] | 6af889c | 2011-10-06 23:11:41 | [diff] [blame] | 1731 | shutdown_loop->PostTask(FROM_HERE, base::Bind( |
| 1732 | &ProxyConfigServiceLinux::Delegate::OnDestroy, this)); |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1733 | } |
| 1734 | } |
| 1735 | void ProxyConfigServiceLinux::Delegate::OnDestroy() { |
[email protected] | 7672247 | 2012-05-24 08:26:46 | [diff] [blame] | 1736 | scoped_refptr<base::SingleThreadTaskRunner> shutdown_loop = |
| 1737 | setting_getter_->GetNotificationTaskRunner(); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1738 | DCHECK(!shutdown_loop.get() || shutdown_loop->BelongsToCurrentThread()); |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 1739 | setting_getter_->ShutDown(); |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1740 | } |
| 1741 | |
| 1742 | ProxyConfigServiceLinux::ProxyConfigServiceLinux() |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 1743 | : delegate_(new Delegate(base::Environment::Create())) { |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1744 | } |
| 1745 | |
[email protected] | 8e1845e1 | 2010-09-15 19:22:24 | [diff] [blame] | 1746 | ProxyConfigServiceLinux::~ProxyConfigServiceLinux() { |
| 1747 | delegate_->PostDestroyTask(); |
| 1748 | } |
| 1749 | |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1750 | ProxyConfigServiceLinux::ProxyConfigServiceLinux( |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 1751 | base::Environment* env_var_getter) |
[email protected] | 9a3d8d4 | 2009-09-03 17:01:46 | [diff] [blame] | 1752 | : delegate_(new Delegate(env_var_getter)) { |
| 1753 | } |
| 1754 | |
| 1755 | ProxyConfigServiceLinux::ProxyConfigServiceLinux( |
[email protected] | 573c050 | 2011-05-17 22:19:50 | [diff] [blame] | 1756 | base::Environment* env_var_getter, SettingGetter* setting_getter) |
| 1757 | : delegate_(new Delegate(env_var_getter, setting_getter)) { |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1758 | } |
| 1759 | |
[email protected] | e4be2dd | 2010-12-14 00:44:39 | [diff] [blame] | 1760 | void ProxyConfigServiceLinux::AddObserver(Observer* observer) { |
| 1761 | delegate_->AddObserver(observer); |
| 1762 | } |
| 1763 | |
| 1764 | void ProxyConfigServiceLinux::RemoveObserver(Observer* observer) { |
| 1765 | delegate_->RemoveObserver(observer); |
| 1766 | } |
| 1767 | |
[email protected] | 3a29593d | 2011-04-11 10:07:52 | [diff] [blame] | 1768 | ProxyConfigService::ConfigAvailability |
| 1769 | ProxyConfigServiceLinux::GetLatestProxyConfig(ProxyConfig* config) { |
[email protected] | e4be2dd | 2010-12-14 00:44:39 | [diff] [blame] | 1770 | return delegate_->GetLatestProxyConfig(config); |
| 1771 | } |
| 1772 | |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1773 | } // namespace net |