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