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