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