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