[email protected] | 3a29593d | 2011-04-11 10:07:52 | [diff] [blame] | 1 | // Copyright (c) 2011 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] | 6de53d4 | 2010-11-09 07:33:19 | [diff] [blame] | 11 | #endif |
[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] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 20 | #include "base/environment.h" |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 21 | #include "base/file_path.h" |
| 22 | #include "base/file_util.h" |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 23 | #include "base/logging.h" |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 24 | #include "base/message_loop.h" |
[email protected] | 3a29593d | 2011-04-11 10:07:52 | [diff] [blame] | 25 | #include "base/nix/xdg_util.h" |
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 26 | #include "base/string_number_conversions.h" |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 27 | #include "base/string_tokenizer.h" |
| 28 | #include "base/string_util.h" |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 29 | #include "base/task.h" |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 30 | #include "base/threading/thread_restrictions.h" |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 31 | #include "base/timer.h" |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 32 | #include "googleurl/src/url_canon.h" |
| 33 | #include "net/base/net_errors.h" |
| 34 | #include "net/http/http_util.h" |
| 35 | #include "net/proxy/proxy_config.h" |
| 36 | #include "net/proxy/proxy_server.h" |
| 37 | |
| 38 | namespace net { |
| 39 | |
| 40 | namespace { |
| 41 | |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 42 | // Given a proxy hostname from a setting, returns that hostname with |
| 43 | // an appropriate proxy server scheme prefix. |
| 44 | // scheme indicates the desired proxy scheme: usually http, with |
| 45 | // socks 4 or 5 as special cases. |
[email protected] | 87a102b | 2009-07-14 05:23:30 | [diff] [blame] | 46 | // TODO(arindam): Remove URI string manipulation by using MapUrlSchemeToProxy. |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 47 | std::string FixupProxyHostScheme(ProxyServer::Scheme scheme, |
| 48 | std::string host) { |
[email protected] | e8c5081 | 2010-09-28 00:16:17 | [diff] [blame] | 49 | if (scheme == ProxyServer::SCHEME_SOCKS5 && |
| 50 | StartsWithASCII(host, "socks4://", false)) { |
| 51 | // We default to socks 5, but if the user specifically set it to |
| 52 | // socks4://, then use that. |
| 53 | scheme = ProxyServer::SCHEME_SOCKS4; |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 54 | } |
| 55 | // Strip the scheme if any. |
| 56 | std::string::size_type colon = host.find("://"); |
| 57 | if (colon != std::string::npos) |
| 58 | host = host.substr(colon + 3); |
| 59 | // If a username and perhaps password are specified, give a warning. |
| 60 | std::string::size_type at_sign = host.find("@"); |
| 61 | // Should this be supported? |
| 62 | if (at_sign != std::string::npos) { |
[email protected] | 62749f18 | 2009-07-15 13:16:54 | [diff] [blame] | 63 | // ProxyConfig does not support authentication parameters, but Chrome |
| 64 | // will prompt for the password later. Disregard the |
| 65 | // authentication parameters and continue with this hostname. |
| 66 | LOG(WARNING) << "Proxy authentication parameters ignored, see bug 16709"; |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 67 | host = host.substr(at_sign + 1); |
| 68 | } |
| 69 | // If this is a socks proxy, prepend a scheme so as to tell |
| 70 | // ProxyServer. This also allows ProxyServer to choose the right |
| 71 | // default port. |
| 72 | if (scheme == ProxyServer::SCHEME_SOCKS4) |
| 73 | host = "socks4://" + host; |
| 74 | else if (scheme == ProxyServer::SCHEME_SOCKS5) |
| 75 | host = "socks5://" + host; |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 76 | // If there is a trailing slash, remove it so |host| will parse correctly |
| 77 | // even if it includes a port number (since the slash is not numeric). |
| 78 | if (host.length() && host[host.length() - 1] == '/') |
| 79 | host.resize(host.length() - 1); |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 80 | return host; |
| 81 | } |
| 82 | |
| 83 | } // namespace |
| 84 | |
[email protected] | 8e1845e1 | 2010-09-15 19:22:24 | [diff] [blame] | 85 | ProxyConfigServiceLinux::Delegate::~Delegate() { |
| 86 | } |
| 87 | |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 88 | bool ProxyConfigServiceLinux::Delegate::GetProxyFromEnvVarForScheme( |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 89 | const char* variable, ProxyServer::Scheme scheme, |
| 90 | ProxyServer* result_server) { |
| 91 | std::string env_value; |
[email protected] | 3ba7e08 | 2010-08-07 02:57:59 | [diff] [blame] | 92 | if (env_var_getter_->GetVar(variable, &env_value)) { |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 93 | if (!env_value.empty()) { |
| 94 | env_value = FixupProxyHostScheme(scheme, env_value); |
[email protected] | 87a102b | 2009-07-14 05:23:30 | [diff] [blame] | 95 | ProxyServer proxy_server = |
| 96 | ProxyServer::FromURI(env_value, ProxyServer::SCHEME_HTTP); |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 97 | if (proxy_server.is_valid() && !proxy_server.is_direct()) { |
| 98 | *result_server = proxy_server; |
| 99 | return true; |
| 100 | } else { |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 101 | LOG(ERROR) << "Failed to parse environment variable " << variable; |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | } |
| 105 | return false; |
| 106 | } |
| 107 | |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 108 | bool ProxyConfigServiceLinux::Delegate::GetProxyFromEnvVar( |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 109 | const char* variable, ProxyServer* result_server) { |
| 110 | return GetProxyFromEnvVarForScheme(variable, ProxyServer::SCHEME_HTTP, |
| 111 | result_server); |
| 112 | } |
| 113 | |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 114 | bool ProxyConfigServiceLinux::Delegate::GetConfigFromEnv(ProxyConfig* config) { |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 115 | // Check for automatic configuration first, in |
| 116 | // "auto_proxy". Possibly only the "environment_proxy" firefox |
| 117 | // extension has ever used this, but it still sounds like a good |
| 118 | // idea. |
| 119 | std::string auto_proxy; |
[email protected] | 3ba7e08 | 2010-08-07 02:57:59 | [diff] [blame] | 120 | if (env_var_getter_->GetVar("auto_proxy", &auto_proxy)) { |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 121 | if (auto_proxy.empty()) { |
| 122 | // Defined and empty => autodetect |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 123 | config->set_auto_detect(true); |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 124 | } else { |
| 125 | // specified autoconfig URL |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 126 | config->set_pac_url(GURL(auto_proxy)); |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 127 | } |
| 128 | return true; |
| 129 | } |
| 130 | // "all_proxy" is a shortcut to avoid defining {http,https,ftp}_proxy. |
| 131 | ProxyServer proxy_server; |
| 132 | if (GetProxyFromEnvVar("all_proxy", &proxy_server)) { |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 133 | config->proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY; |
| 134 | config->proxy_rules().single_proxy = proxy_server; |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 135 | } else { |
| 136 | bool have_http = GetProxyFromEnvVar("http_proxy", &proxy_server); |
| 137 | if (have_http) |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 138 | config->proxy_rules().proxy_for_http = proxy_server; |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 139 | // It would be tempting to let http_proxy apply for all protocols |
| 140 | // if https_proxy and ftp_proxy are not defined. Googling turns up |
| 141 | // several documents that mention only http_proxy. But then the |
| 142 | // user really might not want to proxy https. And it doesn't seem |
| 143 | // like other apps do this. So we will refrain. |
| 144 | bool have_https = GetProxyFromEnvVar("https_proxy", &proxy_server); |
| 145 | if (have_https) |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 146 | config->proxy_rules().proxy_for_https = proxy_server; |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 147 | bool have_ftp = GetProxyFromEnvVar("ftp_proxy", &proxy_server); |
| 148 | if (have_ftp) |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 149 | config->proxy_rules().proxy_for_ftp = proxy_server; |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 150 | if (have_http || have_https || have_ftp) { |
| 151 | // mustn't change type unless some rules are actually set. |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 152 | config->proxy_rules().type = |
| 153 | ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME; |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 154 | } |
| 155 | } |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 156 | if (config->proxy_rules().empty()) { |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 157 | // If the above were not defined, try for socks. |
[email protected] | e8c5081 | 2010-09-28 00:16:17 | [diff] [blame] | 158 | // For environment variables, we default to version 5, per the gnome |
| 159 | // documentation: http://library.gnome.org/devel/gnet/stable/gnet-socks.html |
| 160 | ProxyServer::Scheme scheme = ProxyServer::SCHEME_SOCKS5; |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 161 | std::string env_version; |
[email protected] | 3ba7e08 | 2010-08-07 02:57:59 | [diff] [blame] | 162 | if (env_var_getter_->GetVar("SOCKS_VERSION", &env_version) |
[email protected] | e8c5081 | 2010-09-28 00:16:17 | [diff] [blame] | 163 | && env_version == "4") |
| 164 | scheme = ProxyServer::SCHEME_SOCKS4; |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 165 | if (GetProxyFromEnvVarForScheme("SOCKS_SERVER", scheme, &proxy_server)) { |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 166 | config->proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY; |
| 167 | config->proxy_rules().single_proxy = proxy_server; |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | // Look for the proxy bypass list. |
| 171 | std::string no_proxy; |
[email protected] | 3ba7e08 | 2010-08-07 02:57:59 | [diff] [blame] | 172 | env_var_getter_->GetVar("no_proxy", &no_proxy); |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 173 | if (config->proxy_rules().empty()) { |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 174 | // Having only "no_proxy" set, presumably to "*", makes it |
| 175 | // explicit that env vars do specify a configuration: having no |
| 176 | // rules specified only means the user explicitly asks for direct |
| 177 | // connections. |
| 178 | return !no_proxy.empty(); |
| 179 | } |
[email protected] | 7541206c | 2010-02-19 20:24:06 | [diff] [blame] | 180 | // Note that this uses "suffix" matching. So a bypass of "google.com" |
| 181 | // is understood to mean a bypass of "*google.com". |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 182 | config->proxy_rules().bypass_rules.ParseFromStringUsingSuffixMatching( |
| 183 | no_proxy); |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 184 | return true; |
| 185 | } |
| 186 | |
| 187 | namespace { |
| 188 | |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 189 | const int kDebounceTimeoutMilliseconds = 250; |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 190 | |
[email protected] | 6de53d4 | 2010-11-09 07:33:19 | [diff] [blame] | 191 | #if defined(USE_GCONF) |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 192 | // This is the "real" gconf version that actually uses gconf. |
| 193 | class GConfSettingGetterImplGConf |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 194 | : public ProxyConfigServiceLinux::GConfSettingGetter { |
| 195 | public: |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 196 | GConfSettingGetterImplGConf() |
| 197 | : client_(NULL), notify_delegate_(NULL), loop_(NULL) {} |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 198 | |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 199 | virtual ~GConfSettingGetterImplGConf() { |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 200 | // client_ should have been released before now, from |
[email protected] | f5b1344 | 2009-07-13 15:23:59 | [diff] [blame] | 201 | // Delegate::OnDestroy(), while running on the UI thread. However |
| 202 | // on exiting the process, it may happen that |
| 203 | // Delegate::OnDestroy() task is left pending on the glib loop |
| 204 | // after the loop was quit, and pending tasks may then be deleted |
| 205 | // without being run. |
| 206 | if (client_) { |
| 207 | // gconf client was not cleaned up. |
| 208 | if (MessageLoop::current() == loop_) { |
| 209 | // We are on the UI thread so we can clean it safely. This is |
| 210 | // the case at least for ui_tests running under Valgrind in |
| 211 | // bug 16076. |
[email protected] | b30a3f5 | 2010-10-16 01:05:46 | [diff] [blame] | 212 | VLOG(1) << "~GConfSettingGetterImplGConf: releasing gconf client"; |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 213 | Shutdown(); |
[email protected] | f5b1344 | 2009-07-13 15:23:59 | [diff] [blame] | 214 | } else { |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 215 | LOG(WARNING) << "~GConfSettingGetterImplGConf: leaking gconf client"; |
[email protected] | f5b1344 | 2009-07-13 15:23:59 | [diff] [blame] | 216 | client_ = NULL; |
| 217 | } |
| 218 | } |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 219 | DCHECK(!client_); |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 220 | } |
| 221 | |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 222 | virtual bool Init(MessageLoop* glib_default_loop, |
| 223 | MessageLoopForIO* file_loop) { |
| 224 | DCHECK(MessageLoop::current() == glib_default_loop); |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 225 | DCHECK(!client_); |
| 226 | DCHECK(!loop_); |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 227 | loop_ = glib_default_loop; |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 228 | client_ = gconf_client_get_default(); |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 229 | if (!client_) { |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 230 | // It's not clear whether/when this can return NULL. |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 231 | LOG(ERROR) << "Unable to create a gconf client"; |
| 232 | loop_ = NULL; |
| 233 | return false; |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 234 | } |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 235 | GError* error = NULL; |
| 236 | // We need to add the directories for which we'll be asking |
| 237 | // notifications, and we might as well ask to preload them. |
| 238 | gconf_client_add_dir(client_, "/system/proxy", |
| 239 | GCONF_CLIENT_PRELOAD_ONELEVEL, &error); |
| 240 | if (error == NULL) { |
| 241 | gconf_client_add_dir(client_, "/system/http_proxy", |
| 242 | GCONF_CLIENT_PRELOAD_ONELEVEL, &error); |
| 243 | } |
| 244 | if (error != NULL) { |
| 245 | LOG(ERROR) << "Error requesting gconf directory: " << error->message; |
| 246 | g_error_free(error); |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 247 | Shutdown(); |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 248 | return false; |
| 249 | } |
| 250 | return true; |
| 251 | } |
| 252 | |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 253 | void Shutdown() { |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 254 | if (client_) { |
| 255 | DCHECK(MessageLoop::current() == loop_); |
| 256 | // This also disables gconf notifications. |
| 257 | g_object_unref(client_); |
| 258 | client_ = NULL; |
| 259 | loop_ = NULL; |
| 260 | } |
| 261 | } |
| 262 | |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 263 | bool SetupNotification(ProxyConfigServiceLinux::Delegate* delegate) { |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 264 | DCHECK(client_); |
| 265 | DCHECK(MessageLoop::current() == loop_); |
| 266 | GError* error = NULL; |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 267 | notify_delegate_ = delegate; |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 268 | gconf_client_notify_add( |
| 269 | client_, "/system/proxy", |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 270 | OnGConfChangeNotification, this, |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 271 | NULL, &error); |
| 272 | if (error == NULL) { |
| 273 | gconf_client_notify_add( |
| 274 | client_, "/system/http_proxy", |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 275 | OnGConfChangeNotification, this, |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 276 | NULL, &error); |
| 277 | } |
| 278 | if (error != NULL) { |
| 279 | LOG(ERROR) << "Error requesting gconf notifications: " << error->message; |
| 280 | g_error_free(error); |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 281 | Shutdown(); |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 282 | return false; |
| 283 | } |
| 284 | return true; |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 285 | } |
| 286 | |
[email protected] | 9a3d8d4 | 2009-09-03 17:01:46 | [diff] [blame] | 287 | virtual MessageLoop* GetNotificationLoop() { |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 288 | return loop_; |
| 289 | } |
| 290 | |
| 291 | virtual const char* GetDataSource() { |
| 292 | return "gconf"; |
| 293 | } |
| 294 | |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 295 | virtual bool GetString(const char* key, std::string* result) { |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 296 | DCHECK(client_); |
| 297 | DCHECK(MessageLoop::current() == loop_); |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 298 | GError* error = NULL; |
| 299 | gchar* value = gconf_client_get_string(client_, key, &error); |
| 300 | if (HandleGError(error, key)) |
| 301 | return false; |
| 302 | if (!value) |
| 303 | return false; |
| 304 | *result = value; |
| 305 | g_free(value); |
| 306 | return true; |
| 307 | } |
| 308 | virtual bool GetBoolean(const char* key, bool* result) { |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 309 | DCHECK(client_); |
| 310 | DCHECK(MessageLoop::current() == loop_); |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 311 | GError* error = NULL; |
| 312 | // We want to distinguish unset values from values defaulting to |
| 313 | // false. For that we need to use the type-generic |
| 314 | // gconf_client_get() rather than gconf_client_get_bool(). |
| 315 | GConfValue* gconf_value = gconf_client_get(client_, key, &error); |
| 316 | if (HandleGError(error, key)) |
| 317 | return false; |
| 318 | if (!gconf_value) { |
| 319 | // Unset. |
| 320 | return false; |
| 321 | } |
| 322 | if (gconf_value->type != GCONF_VALUE_BOOL) { |
| 323 | gconf_value_free(gconf_value); |
| 324 | return false; |
| 325 | } |
| 326 | gboolean bool_value = gconf_value_get_bool(gconf_value); |
| 327 | *result = static_cast<bool>(bool_value); |
| 328 | gconf_value_free(gconf_value); |
| 329 | return true; |
| 330 | } |
| 331 | virtual bool GetInt(const char* key, int* result) { |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 332 | DCHECK(client_); |
| 333 | DCHECK(MessageLoop::current() == loop_); |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 334 | GError* error = NULL; |
| 335 | int value = gconf_client_get_int(client_, key, &error); |
| 336 | if (HandleGError(error, key)) |
| 337 | return false; |
| 338 | // We don't bother to distinguish an unset value because callers |
| 339 | // don't care. 0 is returned if unset. |
| 340 | *result = value; |
| 341 | return true; |
| 342 | } |
| 343 | virtual bool GetStringList(const char* key, |
| 344 | std::vector<std::string>* result) { |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 345 | DCHECK(client_); |
| 346 | DCHECK(MessageLoop::current() == loop_); |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 347 | GError* error = NULL; |
| 348 | GSList* list = gconf_client_get_list(client_, key, |
| 349 | GCONF_VALUE_STRING, &error); |
| 350 | if (HandleGError(error, key)) |
| 351 | return false; |
| 352 | if (!list) { |
| 353 | // unset |
| 354 | return false; |
| 355 | } |
| 356 | for (GSList *it = list; it; it = it->next) { |
| 357 | result->push_back(static_cast<char*>(it->data)); |
| 358 | g_free(it->data); |
| 359 | } |
| 360 | g_slist_free(list); |
| 361 | return true; |
| 362 | } |
| 363 | |
[email protected] | a48bf4a | 2010-06-14 18:24:53 | [diff] [blame] | 364 | virtual bool BypassListIsReversed() { |
| 365 | // This is a KDE-specific setting. |
| 366 | return false; |
| 367 | } |
| 368 | |
[email protected] | 1a59719 | 2010-07-09 16:58:38 | [diff] [blame] | 369 | virtual bool MatchHostsUsingSuffixMatching() { |
| 370 | return false; |
| 371 | } |
| 372 | |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 373 | private: |
| 374 | // Logs and frees a glib error. Returns false if there was no error |
| 375 | // (error is NULL). |
| 376 | bool HandleGError(GError* error, const char* key) { |
| 377 | if (error != NULL) { |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 378 | LOG(ERROR) << "Error getting gconf value for " << key |
| 379 | << ": " << error->message; |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 380 | g_error_free(error); |
| 381 | return true; |
| 382 | } |
| 383 | return false; |
| 384 | } |
| 385 | |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 386 | // This is the callback from the debounce timer. |
| 387 | void OnDebouncedNotification() { |
| 388 | DCHECK(MessageLoop::current() == loop_); |
[email protected] | 961ac94 | 2011-04-28 18:18:14 | [diff] [blame] | 389 | CHECK(notify_delegate_); |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 390 | // Forward to a method on the proxy config service delegate object. |
| 391 | notify_delegate_->OnCheckProxyConfigSettings(); |
| 392 | } |
| 393 | |
| 394 | void OnChangeNotification() { |
| 395 | // We don't use Reset() because the timer may not yet be running. |
| 396 | // (In that case Stop() is a no-op.) |
| 397 | debounce_timer_.Stop(); |
| 398 | debounce_timer_.Start(base::TimeDelta::FromMilliseconds( |
| 399 | kDebounceTimeoutMilliseconds), this, |
| 400 | &GConfSettingGetterImplGConf::OnDebouncedNotification); |
| 401 | } |
| 402 | |
| 403 | // gconf notification callback, dispatched from the default glib main loop. |
| 404 | static void OnGConfChangeNotification( |
| 405 | GConfClient* client, guint cnxn_id, |
| 406 | GConfEntry* entry, gpointer user_data) { |
[email protected] | b30a3f5 | 2010-10-16 01:05:46 | [diff] [blame] | 407 | VLOG(1) << "gconf change notification for key " |
| 408 | << gconf_entry_get_key(entry); |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 409 | // We don't track which key has changed, just that something did change. |
| 410 | GConfSettingGetterImplGConf* setting_getter = |
| 411 | reinterpret_cast<GConfSettingGetterImplGConf*>(user_data); |
| 412 | setting_getter->OnChangeNotification(); |
| 413 | } |
| 414 | |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 415 | GConfClient* client_; |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 416 | ProxyConfigServiceLinux::Delegate* notify_delegate_; |
| 417 | base::OneShotTimer<GConfSettingGetterImplGConf> debounce_timer_; |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 418 | |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 419 | // Message loop of the thread that we make gconf calls on. It should |
| 420 | // be the UI thread and all our methods should be called on this |
| 421 | // thread. Only for assertions. |
| 422 | MessageLoop* loop_; |
| 423 | |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 424 | DISALLOW_COPY_AND_ASSIGN(GConfSettingGetterImplGConf); |
| 425 | }; |
[email protected] | 6de53d4 | 2010-11-09 07:33:19 | [diff] [blame] | 426 | #endif // defined(USE_GCONF) |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 427 | |
| 428 | // This is the KDE version that reads kioslaverc and simulates gconf. |
| 429 | // Doing this allows the main Delegate code, as well as the unit tests |
| 430 | // for it, to stay the same - and the settings map fairly well besides. |
| 431 | class GConfSettingGetterImplKDE |
| 432 | : public ProxyConfigServiceLinux::GConfSettingGetter, |
| 433 | public base::MessagePumpLibevent::Watcher { |
| 434 | public: |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 435 | explicit GConfSettingGetterImplKDE(base::Environment* env_var_getter) |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 436 | : inotify_fd_(-1), notify_delegate_(NULL), indirect_manual_(false), |
[email protected] | a48bf4a | 2010-06-14 18:24:53 | [diff] [blame] | 437 | auto_no_pac_(false), reversed_bypass_list_(false), |
[email protected] | f18fde2 | 2010-05-18 23:49:54 | [diff] [blame] | 438 | env_var_getter_(env_var_getter), file_loop_(NULL) { |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 439 | // This has to be called on the UI thread (http://crbug.com/69057). |
| 440 | base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 441 | |
[email protected] | f18fde2 | 2010-05-18 23:49:54 | [diff] [blame] | 442 | // Derive the location of the kde config dir from the environment. |
[email protected] | 92d2dc8 | 2010-04-08 17:49:59 | [diff] [blame] | 443 | std::string home; |
[email protected] | 3ba7e08 | 2010-08-07 02:57:59 | [diff] [blame] | 444 | if (env_var_getter->GetVar("KDEHOME", &home) && !home.empty()) { |
[email protected] | 2e8cfe2 | 2010-06-12 00:26:24 | [diff] [blame] | 445 | // $KDEHOME is set. Use it unconditionally. |
[email protected] | 92d2dc8 | 2010-04-08 17:49:59 | [diff] [blame] | 446 | kde_config_dir_ = KDEHomeToConfigPath(FilePath(home)); |
| 447 | } else { |
[email protected] | 2e8cfe2 | 2010-06-12 00:26:24 | [diff] [blame] | 448 | // $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] | 449 | // the common case on most distributions. |
[email protected] | 3ba7e08 | 2010-08-07 02:57:59 | [diff] [blame] | 450 | if (!env_var_getter->GetVar(base::env_vars::kHome, &home)) |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 451 | // User has no $HOME? Give up. Later we'll report the failure. |
| 452 | return; |
[email protected] | 6b0349ef | 2010-10-16 04:56:06 | [diff] [blame] | 453 | if (base::nix::GetDesktopEnvironment(env_var_getter) == |
| 454 | base::nix::DESKTOP_ENVIRONMENT_KDE3) { |
[email protected] | 92d2dc8 | 2010-04-08 17:49:59 | [diff] [blame] | 455 | // KDE3 always uses .kde for its configuration. |
| 456 | FilePath kde_path = FilePath(home).Append(".kde"); |
| 457 | kde_config_dir_ = KDEHomeToConfigPath(kde_path); |
| 458 | } else { |
| 459 | // Some distributions patch KDE4 to use .kde4 instead of .kde, so that |
[email protected] | fad9c8a5 | 2010-06-10 22:30:53 | [diff] [blame] | 460 | // both can be installed side-by-side. Sadly they don't all do this, and |
| 461 | // they don't always do this: some distributions have started switching |
| 462 | // back as well. So if there is a .kde4 directory, check the timestamps |
| 463 | // of the config directories within and use the newest one. |
[email protected] | 92d2dc8 | 2010-04-08 17:49:59 | [diff] [blame] | 464 | // Note that we should currently be running in the UI thread, because in |
| 465 | // the gconf version, that is the only thread that can access the proxy |
| 466 | // settings (a gconf restriction). As noted below, the initial read of |
| 467 | // the proxy settings will be done in this thread anyway, so we check |
| 468 | // for .kde4 here in this thread as well. |
[email protected] | fad9c8a5 | 2010-06-10 22:30:53 | [diff] [blame] | 469 | FilePath kde3_path = FilePath(home).Append(".kde"); |
| 470 | FilePath kde3_config = KDEHomeToConfigPath(kde3_path); |
[email protected] | 92d2dc8 | 2010-04-08 17:49:59 | [diff] [blame] | 471 | FilePath kde4_path = FilePath(home).Append(".kde4"); |
[email protected] | fad9c8a5 | 2010-06-10 22:30:53 | [diff] [blame] | 472 | FilePath kde4_config = KDEHomeToConfigPath(kde4_path); |
| 473 | bool use_kde4 = false; |
[email protected] | 92d2dc8 | 2010-04-08 17:49:59 | [diff] [blame] | 474 | if (file_util::DirectoryExists(kde4_path)) { |
[email protected] | 2f0193c2 | 2010-09-03 02:28:37 | [diff] [blame] | 475 | base::PlatformFileInfo kde3_info; |
| 476 | base::PlatformFileInfo kde4_info; |
[email protected] | fad9c8a5 | 2010-06-10 22:30:53 | [diff] [blame] | 477 | if (file_util::GetFileInfo(kde4_config, &kde4_info)) { |
| 478 | if (file_util::GetFileInfo(kde3_config, &kde3_info)) { |
| 479 | use_kde4 = kde4_info.last_modified >= kde3_info.last_modified; |
| 480 | } else { |
| 481 | use_kde4 = true; |
| 482 | } |
| 483 | } |
| 484 | } |
| 485 | if (use_kde4) { |
[email protected] | 92d2dc8 | 2010-04-08 17:49:59 | [diff] [blame] | 486 | kde_config_dir_ = KDEHomeToConfigPath(kde4_path); |
| 487 | } else { |
[email protected] | fad9c8a5 | 2010-06-10 22:30:53 | [diff] [blame] | 488 | kde_config_dir_ = KDEHomeToConfigPath(kde3_path); |
[email protected] | 92d2dc8 | 2010-04-08 17:49:59 | [diff] [blame] | 489 | } |
| 490 | } |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 491 | } |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | virtual ~GConfSettingGetterImplKDE() { |
| 495 | // inotify_fd_ should have been closed before now, from |
| 496 | // Delegate::OnDestroy(), while running on the file thread. However |
| 497 | // on exiting the process, it may happen that Delegate::OnDestroy() |
| 498 | // task is left pending on the file loop after the loop was quit, |
| 499 | // and pending tasks may then be deleted without being run. |
| 500 | // Here in the KDE version, we can safely close the file descriptor |
| 501 | // anyway. (Not that it really matters; the process is exiting.) |
| 502 | if (inotify_fd_ >= 0) |
| 503 | Shutdown(); |
| 504 | DCHECK(inotify_fd_ < 0); |
| 505 | } |
| 506 | |
| 507 | virtual bool Init(MessageLoop* glib_default_loop, |
| 508 | MessageLoopForIO* file_loop) { |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 509 | // This has to be called on the UI thread (http://crbug.com/69057). |
| 510 | base::ThreadRestrictions::ScopedAllowIO allow_io; |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 511 | DCHECK(inotify_fd_ < 0); |
| 512 | inotify_fd_ = inotify_init(); |
| 513 | if (inotify_fd_ < 0) { |
[email protected] | 57b76567 | 2009-10-13 18:27:40 | [diff] [blame] | 514 | PLOG(ERROR) << "inotify_init failed"; |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 515 | return false; |
| 516 | } |
| 517 | int flags = fcntl(inotify_fd_, F_GETFL); |
| 518 | if (fcntl(inotify_fd_, F_SETFL, flags | O_NONBLOCK) < 0) { |
[email protected] | 57b76567 | 2009-10-13 18:27:40 | [diff] [blame] | 519 | PLOG(ERROR) << "fcntl failed"; |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 520 | close(inotify_fd_); |
| 521 | inotify_fd_ = -1; |
| 522 | return false; |
| 523 | } |
| 524 | file_loop_ = file_loop; |
| 525 | // The initial read is done on the current thread, not |file_loop_|, |
| 526 | // since we will need to have it for SetupAndFetchInitialConfig(). |
| 527 | UpdateCachedSettings(); |
| 528 | return true; |
| 529 | } |
| 530 | |
| 531 | void Shutdown() { |
| 532 | if (inotify_fd_ >= 0) { |
| 533 | ResetCachedSettings(); |
| 534 | inotify_watcher_.StopWatchingFileDescriptor(); |
| 535 | close(inotify_fd_); |
| 536 | inotify_fd_ = -1; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | bool SetupNotification(ProxyConfigServiceLinux::Delegate* delegate) { |
| 541 | DCHECK(inotify_fd_ >= 0); |
| 542 | DCHECK(file_loop_); |
| 543 | // We can't just watch the kioslaverc file directly, since KDE will write |
| 544 | // a new copy of it and then rename it whenever settings are changed and |
| 545 | // inotify watches inodes (so we'll be watching the old deleted file after |
| 546 | // the first change, and it will never change again). So, we watch the |
| 547 | // directory instead. We then act only on changes to the kioslaverc entry. |
| 548 | if (inotify_add_watch(inotify_fd_, kde_config_dir_.value().c_str(), |
| 549 | IN_MODIFY | IN_MOVED_TO) < 0) |
| 550 | return false; |
| 551 | notify_delegate_ = delegate; |
| 552 | return file_loop_->WatchFileDescriptor(inotify_fd_, true, |
| 553 | MessageLoopForIO::WATCH_READ, &inotify_watcher_, this); |
| 554 | } |
| 555 | |
[email protected] | 9a3d8d4 | 2009-09-03 17:01:46 | [diff] [blame] | 556 | virtual MessageLoop* GetNotificationLoop() { |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 557 | return file_loop_; |
| 558 | } |
| 559 | |
| 560 | // Implement base::MessagePumpLibevent::Delegate. |
| 561 | void OnFileCanReadWithoutBlocking(int fd) { |
| 562 | DCHECK(fd == inotify_fd_); |
| 563 | DCHECK(MessageLoop::current() == file_loop_); |
| 564 | OnChangeNotification(); |
| 565 | } |
| 566 | void OnFileCanWriteWithoutBlocking(int fd) { |
| 567 | NOTREACHED(); |
| 568 | } |
| 569 | |
| 570 | virtual const char* GetDataSource() { |
| 571 | return "KDE"; |
| 572 | } |
| 573 | |
| 574 | virtual bool GetString(const char* key, std::string* result) { |
| 575 | string_map_type::iterator it = string_table_.find(key); |
| 576 | if (it == string_table_.end()) |
| 577 | return false; |
| 578 | *result = it->second; |
| 579 | return true; |
| 580 | } |
| 581 | virtual bool GetBoolean(const char* key, bool* result) { |
| 582 | // We don't ever have any booleans. |
| 583 | return false; |
| 584 | } |
| 585 | virtual bool GetInt(const char* key, int* result) { |
| 586 | // We don't ever have any integers. (See AddProxy() below about ports.) |
| 587 | return false; |
| 588 | } |
| 589 | virtual bool GetStringList(const char* key, |
| 590 | std::vector<std::string>* result) { |
| 591 | strings_map_type::iterator it = strings_table_.find(key); |
| 592 | if (it == strings_table_.end()) |
| 593 | return false; |
| 594 | *result = it->second; |
| 595 | return true; |
| 596 | } |
| 597 | |
[email protected] | a48bf4a | 2010-06-14 18:24:53 | [diff] [blame] | 598 | virtual bool BypassListIsReversed() { |
| 599 | return reversed_bypass_list_; |
| 600 | } |
| 601 | |
[email protected] | 1a59719 | 2010-07-09 16:58:38 | [diff] [blame] | 602 | virtual bool MatchHostsUsingSuffixMatching() { |
| 603 | return true; |
| 604 | } |
| 605 | |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 606 | private: |
| 607 | void ResetCachedSettings() { |
| 608 | string_table_.clear(); |
| 609 | strings_table_.clear(); |
| 610 | indirect_manual_ = false; |
| 611 | auto_no_pac_ = false; |
[email protected] | a48bf4a | 2010-06-14 18:24:53 | [diff] [blame] | 612 | reversed_bypass_list_ = false; |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 613 | } |
| 614 | |
[email protected] | 92d2dc8 | 2010-04-08 17:49:59 | [diff] [blame] | 615 | FilePath KDEHomeToConfigPath(const FilePath& kde_home) { |
| 616 | return kde_home.Append("share").Append("config"); |
| 617 | } |
| 618 | |
[email protected] | a8185d0 | 2010-06-11 00:19:50 | [diff] [blame] | 619 | void AddProxy(const std::string& prefix, const std::string& value) { |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 620 | if (value.empty() || value.substr(0, 3) == "//:") |
| 621 | // No proxy. |
| 622 | return; |
| 623 | // We don't need to parse the port number out; GetProxyFromGConf() |
| 624 | // would only append it right back again. So we just leave the port |
| 625 | // number right in the host string. |
| 626 | string_table_[prefix + "host"] = value; |
| 627 | } |
| 628 | |
[email protected] | a8185d0 | 2010-06-11 00:19:50 | [diff] [blame] | 629 | void AddHostList(const std::string& key, const std::string& value) { |
[email protected] | f18fde2 | 2010-05-18 23:49:54 | [diff] [blame] | 630 | std::vector<std::string> tokens; |
[email protected] | 1a59719 | 2010-07-09 16:58:38 | [diff] [blame] | 631 | StringTokenizer tk(value, ", "); |
[email protected] | f18fde2 | 2010-05-18 23:49:54 | [diff] [blame] | 632 | while (tk.GetNext()) { |
| 633 | std::string token = tk.token(); |
| 634 | if (!token.empty()) |
| 635 | tokens.push_back(token); |
| 636 | } |
| 637 | strings_table_[key] = tokens; |
| 638 | } |
| 639 | |
[email protected] | 9a3d8d4 | 2009-09-03 17:01:46 | [diff] [blame] | 640 | void AddKDESetting(const std::string& key, const std::string& value) { |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 641 | // The astute reader may notice that there is no mention of SOCKS |
| 642 | // here. That's because KDE handles socks is a strange way, and we |
| 643 | // don't support it. Rather than just a setting for the SOCKS server, |
| 644 | // it has a setting for a library to LD_PRELOAD in all your programs |
| 645 | // that will transparently SOCKSify them. Such libraries each have |
| 646 | // their own configuration, and thus, we can't get it from KDE. |
| 647 | if (key == "ProxyType") { |
| 648 | const char* mode = "none"; |
| 649 | indirect_manual_ = false; |
| 650 | auto_no_pac_ = false; |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 651 | int int_value; |
| 652 | base::StringToInt(value, &int_value); |
| 653 | switch (int_value) { |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 654 | case 0: // No proxy, or maybe kioslaverc syntax error. |
| 655 | break; |
| 656 | case 1: // Manual configuration. |
| 657 | mode = "manual"; |
| 658 | break; |
| 659 | case 2: // PAC URL. |
| 660 | mode = "auto"; |
| 661 | break; |
| 662 | case 3: // WPAD. |
| 663 | mode = "auto"; |
| 664 | auto_no_pac_ = true; |
| 665 | break; |
| 666 | case 4: // Indirect manual via environment variables. |
| 667 | mode = "manual"; |
| 668 | indirect_manual_ = true; |
| 669 | break; |
| 670 | } |
| 671 | string_table_["/system/proxy/mode"] = mode; |
| 672 | } else if (key == "Proxy Config Script") { |
| 673 | string_table_["/system/proxy/autoconfig_url"] = value; |
| 674 | } else if (key == "httpProxy") { |
| 675 | AddProxy("/system/http_proxy/", value); |
| 676 | } else if (key == "httpsProxy") { |
| 677 | AddProxy("/system/proxy/secure_", value); |
| 678 | } else if (key == "ftpProxy") { |
| 679 | AddProxy("/system/proxy/ftp_", value); |
| 680 | } else if (key == "ReversedException") { |
| 681 | // We count "true" or any nonzero number as true, otherwise false. |
| 682 | // Note that if the value is not actually numeric StringToInt() |
| 683 | // will return 0, which we count as false. |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 684 | int int_value; |
| 685 | base::StringToInt(value, &int_value); |
| 686 | reversed_bypass_list_ = (value == "true" || int_value); |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 687 | } else if (key == "NoProxyFor") { |
[email protected] | f18fde2 | 2010-05-18 23:49:54 | [diff] [blame] | 688 | AddHostList("/system/http_proxy/ignore_hosts", value); |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 689 | } else if (key == "AuthMode") { |
| 690 | // Check for authentication, just so we can warn. |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 691 | int mode; |
| 692 | base::StringToInt(value, &mode); |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 693 | if (mode) { |
| 694 | // ProxyConfig does not support authentication parameters, but |
| 695 | // Chrome will prompt for the password later. So we ignore this. |
| 696 | LOG(WARNING) << |
| 697 | "Proxy authentication parameters ignored, see bug 16709"; |
| 698 | } |
| 699 | } |
| 700 | } |
| 701 | |
[email protected] | a8185d0 | 2010-06-11 00:19:50 | [diff] [blame] | 702 | void ResolveIndirect(const std::string& key) { |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 703 | string_map_type::iterator it = string_table_.find(key); |
| 704 | if (it != string_table_.end()) { |
[email protected] | f18fde2 | 2010-05-18 23:49:54 | [diff] [blame] | 705 | std::string value; |
[email protected] | 3ba7e08 | 2010-08-07 02:57:59 | [diff] [blame] | 706 | if (env_var_getter_->GetVar(it->second.c_str(), &value)) |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 707 | it->second = value; |
[email protected] | 8425adc0 | 2010-04-18 17:45:31 | [diff] [blame] | 708 | else |
| 709 | string_table_.erase(it); |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 710 | } |
| 711 | } |
| 712 | |
[email protected] | a8185d0 | 2010-06-11 00:19:50 | [diff] [blame] | 713 | void ResolveIndirectList(const std::string& key) { |
[email protected] | f18fde2 | 2010-05-18 23:49:54 | [diff] [blame] | 714 | strings_map_type::iterator it = strings_table_.find(key); |
| 715 | if (it != strings_table_.end()) { |
| 716 | std::string value; |
| 717 | if (!it->second.empty() && |
[email protected] | 3ba7e08 | 2010-08-07 02:57:59 | [diff] [blame] | 718 | env_var_getter_->GetVar(it->second[0].c_str(), &value)) |
[email protected] | f18fde2 | 2010-05-18 23:49:54 | [diff] [blame] | 719 | AddHostList(key, value); |
| 720 | else |
| 721 | strings_table_.erase(it); |
| 722 | } |
| 723 | } |
| 724 | |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 725 | // The settings in kioslaverc could occur in any order, but some affect |
| 726 | // others. Rather than read the whole file in and then query them in an |
| 727 | // order that allows us to handle that, we read the settings in whatever |
| 728 | // order they occur and do any necessary tweaking after we finish. |
| 729 | void ResolveModeEffects() { |
| 730 | if (indirect_manual_) { |
| 731 | ResolveIndirect("/system/http_proxy/host"); |
| 732 | ResolveIndirect("/system/proxy/secure_host"); |
| 733 | ResolveIndirect("/system/proxy/ftp_host"); |
[email protected] | f18fde2 | 2010-05-18 23:49:54 | [diff] [blame] | 734 | ResolveIndirectList("/system/http_proxy/ignore_hosts"); |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 735 | } |
| 736 | if (auto_no_pac_) { |
| 737 | // Remove the PAC URL; we're not supposed to use it. |
| 738 | string_table_.erase("/system/proxy/autoconfig_url"); |
| 739 | } |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | // Reads kioslaverc one line at a time and calls AddKDESetting() to add |
| 743 | // each relevant name-value pair to the appropriate value table. |
| 744 | void UpdateCachedSettings() { |
[email protected] | 92d2dc8 | 2010-04-08 17:49:59 | [diff] [blame] | 745 | FilePath kioslaverc = kde_config_dir_.Append("kioslaverc"); |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 746 | file_util::ScopedFILE input(file_util::OpenFile(kioslaverc, "r")); |
| 747 | if (!input.get()) |
| 748 | return; |
| 749 | ResetCachedSettings(); |
| 750 | bool in_proxy_settings = false; |
| 751 | bool line_too_long = false; |
[email protected] | 9a3d8d4 | 2009-09-03 17:01:46 | [diff] [blame] | 752 | char line[BUFFER_SIZE]; |
| 753 | // fgets() will return NULL on EOF or error. |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 754 | while (fgets(line, sizeof(line), input.get())) { |
| 755 | // fgets() guarantees the line will be properly terminated. |
| 756 | size_t length = strlen(line); |
| 757 | if (!length) |
| 758 | continue; |
| 759 | // This should be true even with CRLF endings. |
| 760 | if (line[length - 1] != '\n') { |
| 761 | line_too_long = true; |
| 762 | continue; |
| 763 | } |
| 764 | if (line_too_long) { |
| 765 | // The previous line had no line ending, but this done does. This is |
| 766 | // the end of the line that was too long, so warn here and skip it. |
| 767 | LOG(WARNING) << "skipped very long line in " << kioslaverc.value(); |
| 768 | line_too_long = false; |
| 769 | continue; |
| 770 | } |
| 771 | // Remove the LF at the end, and the CR if there is one. |
| 772 | line[--length] = '\0'; |
| 773 | if (length && line[length - 1] == '\r') |
| 774 | line[--length] = '\0'; |
| 775 | // Now parse the line. |
| 776 | if (line[0] == '[') { |
| 777 | // Switching sections. All we care about is whether this is |
| 778 | // the (a?) proxy settings section, for both KDE3 and KDE4. |
| 779 | in_proxy_settings = !strncmp(line, "[Proxy Settings]", 16); |
| 780 | } else if (in_proxy_settings) { |
| 781 | // A regular line, in the (a?) proxy settings section. |
[email protected] | 9a3d8d4 | 2009-09-03 17:01:46 | [diff] [blame] | 782 | char* split = strchr(line, '='); |
| 783 | // Skip this line if it does not contain an = sign. |
| 784 | if (!split) |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 785 | continue; |
[email protected] | 9a3d8d4 | 2009-09-03 17:01:46 | [diff] [blame] | 786 | // Split the line on the = and advance |split|. |
| 787 | *(split++) = 0; |
| 788 | std::string key = line; |
| 789 | std::string value = split; |
| 790 | TrimWhitespaceASCII(key, TRIM_ALL, &key); |
| 791 | TrimWhitespaceASCII(value, TRIM_ALL, &value); |
| 792 | // Skip this line if the key name is empty. |
| 793 | if (key.empty()) |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 794 | continue; |
| 795 | // Is the value name localized? |
[email protected] | 9a3d8d4 | 2009-09-03 17:01:46 | [diff] [blame] | 796 | if (key[key.length() - 1] == ']') { |
| 797 | // Find the matching bracket. |
| 798 | length = key.rfind('['); |
| 799 | // Skip this line if the localization indicator is malformed. |
| 800 | if (length == std::string::npos) |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 801 | continue; |
| 802 | // Trim the localization indicator off. |
[email protected] | 9a3d8d4 | 2009-09-03 17:01:46 | [diff] [blame] | 803 | key.resize(length); |
| 804 | // Remove any resulting trailing whitespace. |
| 805 | TrimWhitespaceASCII(key, TRIM_TRAILING, &key); |
| 806 | // Skip this line if the key name is now empty. |
| 807 | if (key.empty()) |
| 808 | continue; |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 809 | } |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 810 | // Now fill in the tables. |
[email protected] | 9a3d8d4 | 2009-09-03 17:01:46 | [diff] [blame] | 811 | AddKDESetting(key, value); |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 812 | } |
| 813 | } |
| 814 | if (ferror(input.get())) |
| 815 | LOG(ERROR) << "error reading " << kioslaverc.value(); |
| 816 | ResolveModeEffects(); |
| 817 | } |
| 818 | |
| 819 | // This is the callback from the debounce timer. |
| 820 | void OnDebouncedNotification() { |
| 821 | DCHECK(MessageLoop::current() == file_loop_); |
[email protected] | b30a3f5 | 2010-10-16 01:05:46 | [diff] [blame] | 822 | VLOG(1) << "inotify change notification for kioslaverc"; |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 823 | UpdateCachedSettings(); |
[email protected] | 961ac94 | 2011-04-28 18:18:14 | [diff] [blame] | 824 | CHECK(notify_delegate_); |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 825 | // Forward to a method on the proxy config service delegate object. |
| 826 | notify_delegate_->OnCheckProxyConfigSettings(); |
| 827 | } |
| 828 | |
| 829 | // Called by OnFileCanReadWithoutBlocking() on the file thread. Reads |
| 830 | // from the inotify file descriptor and starts up a debounce timer if |
| 831 | // an event for kioslaverc is seen. |
| 832 | void OnChangeNotification() { |
| 833 | DCHECK(inotify_fd_ >= 0); |
| 834 | DCHECK(MessageLoop::current() == file_loop_); |
| 835 | char event_buf[(sizeof(inotify_event) + NAME_MAX + 1) * 4]; |
| 836 | bool kioslaverc_touched = false; |
| 837 | ssize_t r; |
| 838 | while ((r = read(inotify_fd_, event_buf, sizeof(event_buf))) > 0) { |
| 839 | // inotify returns variable-length structures, which is why we have |
| 840 | // this strange-looking loop instead of iterating through an array. |
| 841 | char* event_ptr = event_buf; |
| 842 | while (event_ptr < event_buf + r) { |
| 843 | inotify_event* event = reinterpret_cast<inotify_event*>(event_ptr); |
| 844 | // The kernel always feeds us whole events. |
[email protected] | b1f031dd | 2010-03-02 23:19:33 | [diff] [blame] | 845 | CHECK_LE(event_ptr + sizeof(inotify_event), event_buf + r); |
| 846 | CHECK_LE(event->name + event->len, event_buf + r); |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 847 | if (!strcmp(event->name, "kioslaverc")) |
| 848 | kioslaverc_touched = true; |
| 849 | // Advance the pointer just past the end of the filename. |
| 850 | event_ptr = event->name + event->len; |
| 851 | } |
| 852 | // We keep reading even if |kioslaverc_touched| is true to drain the |
| 853 | // inotify event queue. |
| 854 | } |
| 855 | if (!r) |
| 856 | // Instead of returning -1 and setting errno to EINVAL if there is not |
| 857 | // enough buffer space, older kernels (< 2.6.21) return 0. Simulate the |
| 858 | // new behavior (EINVAL) so we can reuse the code below. |
| 859 | errno = EINVAL; |
| 860 | if (errno != EAGAIN) { |
[email protected] | 57b76567 | 2009-10-13 18:27:40 | [diff] [blame] | 861 | PLOG(WARNING) << "error reading inotify file descriptor"; |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 862 | if (errno == EINVAL) { |
| 863 | // Our buffer is not large enough to read the next event. This should |
| 864 | // not happen (because its size is calculated to always be sufficiently |
| 865 | // large), but if it does we'd warn continuously since |inotify_fd_| |
| 866 | // would be forever ready to read. Close it and stop watching instead. |
| 867 | LOG(ERROR) << "inotify failure; no longer watching kioslaverc!"; |
| 868 | inotify_watcher_.StopWatchingFileDescriptor(); |
| 869 | close(inotify_fd_); |
| 870 | inotify_fd_ = -1; |
| 871 | } |
| 872 | } |
| 873 | if (kioslaverc_touched) { |
| 874 | // We don't use Reset() because the timer may not yet be running. |
| 875 | // (In that case Stop() is a no-op.) |
| 876 | debounce_timer_.Stop(); |
| 877 | debounce_timer_.Start(base::TimeDelta::FromMilliseconds( |
| 878 | kDebounceTimeoutMilliseconds), this, |
| 879 | &GConfSettingGetterImplKDE::OnDebouncedNotification); |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | typedef std::map<std::string, std::string> string_map_type; |
| 884 | typedef std::map<std::string, std::vector<std::string> > strings_map_type; |
| 885 | |
| 886 | int inotify_fd_; |
| 887 | base::MessagePumpLibevent::FileDescriptorWatcher inotify_watcher_; |
| 888 | ProxyConfigServiceLinux::Delegate* notify_delegate_; |
| 889 | base::OneShotTimer<GConfSettingGetterImplKDE> debounce_timer_; |
| 890 | FilePath kde_config_dir_; |
| 891 | bool indirect_manual_; |
| 892 | bool auto_no_pac_; |
[email protected] | a48bf4a | 2010-06-14 18:24:53 | [diff] [blame] | 893 | bool reversed_bypass_list_; |
[email protected] | f18fde2 | 2010-05-18 23:49:54 | [diff] [blame] | 894 | // We don't own |env_var_getter_|. It's safe to hold a pointer to it, since |
| 895 | // both it and us are owned by ProxyConfigServiceLinux::Delegate, and have the |
| 896 | // same lifetime. |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 897 | base::Environment* env_var_getter_; |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 898 | |
| 899 | // We cache these settings whenever we re-read the kioslaverc file. |
| 900 | string_map_type string_table_; |
| 901 | strings_map_type strings_table_; |
| 902 | |
| 903 | // Message loop of the file thread, for reading kioslaverc. If NULL, |
| 904 | // just read it directly (for testing). We also handle inotify events |
| 905 | // on this thread. |
| 906 | MessageLoopForIO* file_loop_; |
| 907 | |
| 908 | DISALLOW_COPY_AND_ASSIGN(GConfSettingGetterImplKDE); |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 909 | }; |
| 910 | |
| 911 | } // namespace |
| 912 | |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 913 | bool ProxyConfigServiceLinux::Delegate::GetProxyFromGConf( |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 914 | const char* key_prefix, bool is_socks, ProxyServer* result_server) { |
| 915 | std::string key(key_prefix); |
| 916 | std::string host; |
| 917 | if (!gconf_getter_->GetString((key + "host").c_str(), &host) |
| 918 | || host.empty()) { |
| 919 | // Unset or empty. |
| 920 | return false; |
| 921 | } |
| 922 | // Check for an optional port. |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 923 | int port = 0; |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 924 | gconf_getter_->GetInt((key + "port").c_str(), &port); |
| 925 | if (port != 0) { |
| 926 | // If a port is set and non-zero: |
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 927 | host += ":" + base::IntToString(port); |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 928 | } |
[email protected] | 76960f3d | 2011-04-30 02:15:23 | [diff] [blame^] | 929 | |
| 930 | // gconf settings do not appear to distinguish between SOCKS |
| 931 | // version. We default to version 5. For more information on this policy |
| 932 | // decisions, see: |
| 933 | // http://code.google.com/p/chromium/issues/detail?id=55912#c2 |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 934 | host = FixupProxyHostScheme( |
[email protected] | e8c5081 | 2010-09-28 00:16:17 | [diff] [blame] | 935 | is_socks ? ProxyServer::SCHEME_SOCKS5 : ProxyServer::SCHEME_HTTP, |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 936 | host); |
[email protected] | 87a102b | 2009-07-14 05:23:30 | [diff] [blame] | 937 | ProxyServer proxy_server = ProxyServer::FromURI(host, |
| 938 | ProxyServer::SCHEME_HTTP); |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 939 | if (proxy_server.is_valid()) { |
| 940 | *result_server = proxy_server; |
| 941 | return true; |
| 942 | } |
| 943 | return false; |
| 944 | } |
| 945 | |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 946 | bool ProxyConfigServiceLinux::Delegate::GetConfigFromGConf( |
| 947 | ProxyConfig* config) { |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 948 | std::string mode; |
| 949 | if (!gconf_getter_->GetString("/system/proxy/mode", &mode)) { |
| 950 | // We expect this to always be set, so if we don't see it then we |
| 951 | // probably have a gconf problem, and so we don't have a valid |
| 952 | // proxy config. |
| 953 | return false; |
| 954 | } |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 955 | if (mode == "none") { |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 956 | // Specifically specifies no proxy. |
| 957 | return true; |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 958 | } |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 959 | |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 960 | if (mode == "auto") { |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 961 | // automatic proxy config |
| 962 | std::string pac_url_str; |
| 963 | if (gconf_getter_->GetString("/system/proxy/autoconfig_url", |
| 964 | &pac_url_str)) { |
| 965 | if (!pac_url_str.empty()) { |
| 966 | GURL pac_url(pac_url_str); |
| 967 | if (!pac_url.is_valid()) |
| 968 | return false; |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 969 | config->set_pac_url(pac_url); |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 970 | return true; |
| 971 | } |
| 972 | } |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 973 | config->set_auto_detect(true); |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 974 | return true; |
| 975 | } |
| 976 | |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 977 | if (mode != "manual") { |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 978 | // Mode is unrecognized. |
| 979 | return false; |
| 980 | } |
| 981 | bool use_http_proxy; |
| 982 | if (gconf_getter_->GetBoolean("/system/http_proxy/use_http_proxy", |
| 983 | &use_http_proxy) |
| 984 | && !use_http_proxy) { |
| 985 | // Another master switch for some reason. If set to false, then no |
| 986 | // proxy. But we don't panic if the key doesn't exist. |
| 987 | return true; |
| 988 | } |
| 989 | |
| 990 | bool same_proxy = false; |
| 991 | // Indicates to use the http proxy for all protocols. This one may |
| 992 | // not exist (presumably on older versions), assume false in that |
| 993 | // case. |
| 994 | gconf_getter_->GetBoolean("/system/http_proxy/use_same_proxy", |
| 995 | &same_proxy); |
| 996 | |
[email protected] | 76960f3d | 2011-04-30 02:15:23 | [diff] [blame^] | 997 | ProxyServer proxy_for_http; |
| 998 | ProxyServer proxy_for_https; |
| 999 | ProxyServer proxy_for_ftp; |
| 1000 | ProxyServer socks_proxy; // (socks) |
| 1001 | |
| 1002 | // This counts how many of the above ProxyServers were defined and valid. |
| 1003 | size_t num_proxies_specified = 0; |
| 1004 | |
| 1005 | // Extract the per-scheme proxies. If we failed to parse it, or no proxy was |
| 1006 | // specified for the scheme, then the resulting ProxyServer will be invalid. |
| 1007 | if (GetProxyFromGConf("/system/http_proxy/", false, &proxy_for_http)) |
| 1008 | num_proxies_specified++; |
| 1009 | if (GetProxyFromGConf("/system/proxy/secure_", false, &proxy_for_https)) |
| 1010 | num_proxies_specified++; |
| 1011 | if (GetProxyFromGConf("/system/proxy/ftp_", false, &proxy_for_ftp)) |
| 1012 | num_proxies_specified++; |
| 1013 | if (GetProxyFromGConf("/system/proxy/socks_", true, &socks_proxy)) |
| 1014 | num_proxies_specified++; |
| 1015 | |
| 1016 | if (same_proxy) { |
| 1017 | if (proxy_for_http.is_valid()) { |
| 1018 | // Use the http proxy for all schemes. |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 1019 | config->proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY; |
[email protected] | 76960f3d | 2011-04-30 02:15:23 | [diff] [blame^] | 1020 | config->proxy_rules().single_proxy = proxy_for_http; |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1021 | } |
[email protected] | 76960f3d | 2011-04-30 02:15:23 | [diff] [blame^] | 1022 | } else if (num_proxies_specified > 0) { |
| 1023 | if (socks_proxy.is_valid() && num_proxies_specified == 1) { |
| 1024 | // If the only proxy specified was for SOCKS, use it for all schemes. |
| 1025 | config->proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY; |
| 1026 | config->proxy_rules().single_proxy = socks_proxy; |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1027 | } else { |
[email protected] | 76960f3d | 2011-04-30 02:15:23 | [diff] [blame^] | 1028 | // Otherwise use the indicate proxies per-scheme. |
| 1029 | config->proxy_rules().type = |
| 1030 | ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME; |
| 1031 | config->proxy_rules().proxy_for_http = proxy_for_http; |
| 1032 | config->proxy_rules().proxy_for_https = proxy_for_https; |
| 1033 | config->proxy_rules().proxy_for_ftp = proxy_for_ftp; |
| 1034 | config->proxy_rules().fallback_proxy = socks_proxy; |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1035 | } |
| 1036 | } |
| 1037 | |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 1038 | if (config->proxy_rules().empty()) { |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1039 | // Manual mode but we couldn't parse any rules. |
| 1040 | return false; |
| 1041 | } |
| 1042 | |
| 1043 | // Check for authentication, just so we can warn. |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1044 | bool use_auth = false; |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1045 | gconf_getter_->GetBoolean("/system/http_proxy/use_authentication", |
| 1046 | &use_auth); |
[email protected] | 62749f18 | 2009-07-15 13:16:54 | [diff] [blame] | 1047 | if (use_auth) { |
| 1048 | // ProxyConfig does not support authentication parameters, but |
| 1049 | // Chrome will prompt for the password later. So we ignore |
| 1050 | // /system/http_proxy/*auth* settings. |
| 1051 | LOG(WARNING) << "Proxy authentication parameters ignored, see bug 16709"; |
| 1052 | } |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1053 | |
| 1054 | // Now the bypass list. |
[email protected] | 7541206c | 2010-02-19 20:24:06 | [diff] [blame] | 1055 | std::vector<std::string> ignore_hosts_list; |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 1056 | config->proxy_rules().bypass_rules.Clear(); |
[email protected] | a8185d0 | 2010-06-11 00:19:50 | [diff] [blame] | 1057 | if (gconf_getter_->GetStringList("/system/http_proxy/ignore_hosts", |
| 1058 | &ignore_hosts_list)) { |
| 1059 | std::vector<std::string>::const_iterator it(ignore_hosts_list.begin()); |
[email protected] | 1a59719 | 2010-07-09 16:58:38 | [diff] [blame] | 1060 | for (; it != ignore_hosts_list.end(); ++it) { |
| 1061 | if (gconf_getter_->MatchHostsUsingSuffixMatching()) { |
| 1062 | config->proxy_rules().bypass_rules. |
| 1063 | AddRuleFromStringUsingSuffixMatching(*it); |
| 1064 | } else { |
| 1065 | config->proxy_rules().bypass_rules.AddRuleFromString(*it); |
| 1066 | } |
| 1067 | } |
[email protected] | a8185d0 | 2010-06-11 00:19:50 | [diff] [blame] | 1068 | } |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1069 | // Note that there are no settings with semantics corresponding to |
[email protected] | 1a59719 | 2010-07-09 16:58:38 | [diff] [blame] | 1070 | // bypass of local names in GNOME. In KDE, "<local>" is supported |
| 1071 | // as a hostname rule. |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1072 | |
[email protected] | a48bf4a | 2010-06-14 18:24:53 | [diff] [blame] | 1073 | // KDE allows one to reverse the bypass rules. |
| 1074 | config->proxy_rules().reverse_bypass = gconf_getter_->BypassListIsReversed(); |
| 1075 | |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1076 | return true; |
| 1077 | } |
| 1078 | |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 1079 | ProxyConfigServiceLinux::Delegate::Delegate(base::Environment* env_var_getter) |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1080 | : env_var_getter_(env_var_getter), |
| 1081 | glib_default_loop_(NULL), io_loop_(NULL) { |
| 1082 | // Figure out which GConfSettingGetterImpl to use, if any. |
[email protected] | 6b0349ef | 2010-10-16 04:56:06 | [diff] [blame] | 1083 | switch (base::nix::GetDesktopEnvironment(env_var_getter)) { |
| 1084 | case base::nix::DESKTOP_ENVIRONMENT_GNOME: |
[email protected] | 6de53d4 | 2010-11-09 07:33:19 | [diff] [blame] | 1085 | #if defined(USE_GCONF) |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1086 | gconf_getter_.reset(new GConfSettingGetterImplGConf()); |
[email protected] | 6de53d4 | 2010-11-09 07:33:19 | [diff] [blame] | 1087 | #endif |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1088 | break; |
[email protected] | 6b0349ef | 2010-10-16 04:56:06 | [diff] [blame] | 1089 | case base::nix::DESKTOP_ENVIRONMENT_KDE3: |
| 1090 | case base::nix::DESKTOP_ENVIRONMENT_KDE4: |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1091 | gconf_getter_.reset(new GConfSettingGetterImplKDE(env_var_getter)); |
| 1092 | break; |
[email protected] | 6b0349ef | 2010-10-16 04:56:06 | [diff] [blame] | 1093 | case base::nix::DESKTOP_ENVIRONMENT_XFCE: |
| 1094 | case base::nix::DESKTOP_ENVIRONMENT_OTHER: |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1095 | break; |
| 1096 | } |
| 1097 | } |
| 1098 | |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 1099 | ProxyConfigServiceLinux::Delegate::Delegate(base::Environment* env_var_getter, |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1100 | GConfSettingGetter* gconf_getter) |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1101 | : env_var_getter_(env_var_getter), gconf_getter_(gconf_getter), |
| 1102 | glib_default_loop_(NULL), io_loop_(NULL) { |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1103 | } |
| 1104 | |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1105 | void ProxyConfigServiceLinux::Delegate::SetupAndFetchInitialConfig( |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1106 | MessageLoop* glib_default_loop, MessageLoop* io_loop, |
| 1107 | MessageLoopForIO* file_loop) { |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1108 | // We should be running on the default glib main loop thread right |
| 1109 | // now. gconf can only be accessed from this thread. |
| 1110 | DCHECK(MessageLoop::current() == glib_default_loop); |
| 1111 | glib_default_loop_ = glib_default_loop; |
| 1112 | io_loop_ = io_loop; |
| 1113 | |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1114 | // If we are passed a NULL io_loop or file_loop, then we don't set up |
| 1115 | // proxy setting change notifications. This should not be the usual |
| 1116 | // case but is intended to simplify test setups. |
| 1117 | if (!io_loop_ || !file_loop) |
[email protected] | b30a3f5 | 2010-10-16 01:05:46 | [diff] [blame] | 1118 | VLOG(1) << "Monitoring of proxy setting changes is disabled"; |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1119 | |
| 1120 | // Fetch and cache the current proxy config. The config is left in |
[email protected] | 11965500 | 2010-07-23 06:02:40 | [diff] [blame] | 1121 | // cached_config_, where GetLatestProxyConfig() running on the IO thread |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1122 | // will expect to find it. This is safe to do because we return |
| 1123 | // before this ProxyConfigServiceLinux is passed on to |
| 1124 | // the ProxyService. |
[email protected] | d6cb85b | 2009-07-23 22:10:53 | [diff] [blame] | 1125 | |
| 1126 | // Note: It would be nice to prioritize environment variables |
[email protected] | 92d2dc8 | 2010-04-08 17:49:59 | [diff] [blame] | 1127 | // and only fall back to gconf if env vars were unset. But |
[email protected] | d6cb85b | 2009-07-23 22:10:53 | [diff] [blame] | 1128 | // gnome-terminal "helpfully" sets http_proxy and no_proxy, and it |
| 1129 | // does so even if the proxy mode is set to auto, which would |
| 1130 | // mislead us. |
| 1131 | |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1132 | bool got_config = false; |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1133 | if (gconf_getter_.get()) { |
| 1134 | if (gconf_getter_->Init(glib_default_loop, file_loop) && |
| 1135 | (!io_loop || !file_loop || gconf_getter_->SetupNotification(this))) { |
| 1136 | if (GetConfigFromGConf(&cached_config_)) { |
| 1137 | cached_config_.set_id(1); // mark it as valid |
| 1138 | got_config = true; |
[email protected] | b30a3f5 | 2010-10-16 01:05:46 | [diff] [blame] | 1139 | VLOG(1) << "Obtained proxy settings from " |
| 1140 | << gconf_getter_->GetDataSource(); |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1141 | // If gconf proxy mode is "none", meaning direct, then we take |
| 1142 | // that to be a valid config and will not check environment |
| 1143 | // variables. The alternative would have been to look for a proxy |
| 1144 | // whereever we can find one. |
| 1145 | // |
| 1146 | // Keep a copy of the config for use from this thread for |
| 1147 | // comparison with updated settings when we get notifications. |
| 1148 | reference_config_ = cached_config_; |
| 1149 | reference_config_.set_id(1); // mark it as valid |
| 1150 | } else { |
| 1151 | gconf_getter_->Shutdown(); // Stop notifications |
[email protected] | d6cb85b | 2009-07-23 22:10:53 | [diff] [blame] | 1152 | } |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1153 | } |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1154 | } |
[email protected] | d6cb85b | 2009-07-23 22:10:53 | [diff] [blame] | 1155 | |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1156 | if (!got_config) { |
[email protected] | d6cb85b | 2009-07-23 22:10:53 | [diff] [blame] | 1157 | // We fall back on environment variables. |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1158 | // |
| 1159 | // Consulting environment variables doesn't need to be done from |
| 1160 | // the default glib main loop, but it's a tiny enough amount of |
| 1161 | // work. |
| 1162 | if (GetConfigFromEnv(&cached_config_)) { |
| 1163 | cached_config_.set_id(1); // mark it as valid |
[email protected] | b30a3f5 | 2010-10-16 01:05:46 | [diff] [blame] | 1164 | VLOG(1) << "Obtained proxy settings from environment variables"; |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1165 | } |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1166 | } |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1167 | } |
| 1168 | |
[email protected] | 11965500 | 2010-07-23 06:02:40 | [diff] [blame] | 1169 | void ProxyConfigServiceLinux::Delegate::AddObserver(Observer* observer) { |
| 1170 | observers_.AddObserver(observer); |
| 1171 | } |
| 1172 | |
| 1173 | void ProxyConfigServiceLinux::Delegate::RemoveObserver(Observer* observer) { |
| 1174 | observers_.RemoveObserver(observer); |
| 1175 | } |
| 1176 | |
[email protected] | 3a29593d | 2011-04-11 10:07:52 | [diff] [blame] | 1177 | ProxyConfigService::ConfigAvailability |
| 1178 | ProxyConfigServiceLinux::Delegate::GetLatestProxyConfig( |
| 1179 | ProxyConfig* config) { |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1180 | // This is called from the IO thread. |
| 1181 | DCHECK(!io_loop_ || MessageLoop::current() == io_loop_); |
| 1182 | |
| 1183 | // Simply return the last proxy configuration that glib_default_loop |
| 1184 | // notified us of. |
[email protected] | 11965500 | 2010-07-23 06:02:40 | [diff] [blame] | 1185 | *config = cached_config_.is_valid() ? |
| 1186 | cached_config_ : ProxyConfig::CreateDirect(); |
| 1187 | |
[email protected] | 3a29593d | 2011-04-11 10:07:52 | [diff] [blame] | 1188 | // 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] | 1189 | // going to be available since we initialized eagerly on the UI thread. |
| 1190 | // TODO(eroman): do lazy initialization instead, so we no longer need |
| 1191 | // to construct ProxyConfigServiceLinux on the UI thread. |
| 1192 | // In which case, we may return false here. |
[email protected] | 3a29593d | 2011-04-11 10:07:52 | [diff] [blame] | 1193 | return CONFIG_VALID; |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1194 | } |
| 1195 | |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1196 | // Depending on the GConfSettingGetter in use, this method will be called |
| 1197 | // on either the UI thread (GConf) or the file thread (KDE). |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1198 | void ProxyConfigServiceLinux::Delegate::OnCheckProxyConfigSettings() { |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1199 | MessageLoop* required_loop = gconf_getter_->GetNotificationLoop(); |
| 1200 | DCHECK(!required_loop || MessageLoop::current() == required_loop); |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1201 | ProxyConfig new_config; |
| 1202 | bool valid = GetConfigFromGConf(&new_config); |
| 1203 | if (valid) |
| 1204 | new_config.set_id(1); // mark it as valid |
| 1205 | |
[email protected] | 11965500 | 2010-07-23 06:02:40 | [diff] [blame] | 1206 | // See if it is different from what we had before. |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1207 | if (new_config.is_valid() != reference_config_.is_valid() || |
| 1208 | !new_config.Equals(reference_config_)) { |
| 1209 | // Post a task to |io_loop| with the new configuration, so it can |
| 1210 | // update |cached_config_|. |
| 1211 | io_loop_->PostTask( |
| 1212 | FROM_HERE, |
| 1213 | NewRunnableMethod( |
| 1214 | this, |
| 1215 | &ProxyConfigServiceLinux::Delegate::SetNewProxyConfig, |
| 1216 | new_config)); |
[email protected] | d1f9d47 | 2009-08-13 19:59:30 | [diff] [blame] | 1217 | // Update the thread-private copy in |reference_config_| as well. |
| 1218 | reference_config_ = new_config; |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | void ProxyConfigServiceLinux::Delegate::SetNewProxyConfig( |
| 1223 | const ProxyConfig& new_config) { |
| 1224 | DCHECK(MessageLoop::current() == io_loop_); |
[email protected] | b30a3f5 | 2010-10-16 01:05:46 | [diff] [blame] | 1225 | VLOG(1) << "Proxy configuration changed"; |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1226 | cached_config_ = new_config; |
[email protected] | 3a29593d | 2011-04-11 10:07:52 | [diff] [blame] | 1227 | FOR_EACH_OBSERVER( |
| 1228 | Observer, observers_, |
| 1229 | OnProxyConfigChanged(new_config, ProxyConfigService::CONFIG_VALID)); |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | void ProxyConfigServiceLinux::Delegate::PostDestroyTask() { |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1233 | if (!gconf_getter_.get()) |
| 1234 | return; |
| 1235 | MessageLoop* shutdown_loop = gconf_getter_->GetNotificationLoop(); |
| 1236 | if (!shutdown_loop || MessageLoop::current() == shutdown_loop) { |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1237 | // Already on the right thread, call directly. |
| 1238 | // This is the case for the unittests. |
| 1239 | OnDestroy(); |
| 1240 | } else { |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1241 | // Post to shutdown thread. Note that on browser shutdown, we may quit |
| 1242 | // this MessageLoop and exit the program before ever running this. |
| 1243 | shutdown_loop->PostTask( |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1244 | FROM_HERE, |
| 1245 | NewRunnableMethod( |
| 1246 | this, |
| 1247 | &ProxyConfigServiceLinux::Delegate::OnDestroy)); |
| 1248 | } |
| 1249 | } |
| 1250 | void ProxyConfigServiceLinux::Delegate::OnDestroy() { |
[email protected] | d7395e73 | 2009-08-28 23:13:43 | [diff] [blame] | 1251 | MessageLoop* shutdown_loop = gconf_getter_->GetNotificationLoop(); |
| 1252 | DCHECK(!shutdown_loop || MessageLoop::current() == shutdown_loop); |
| 1253 | gconf_getter_->Shutdown(); |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1254 | } |
| 1255 | |
| 1256 | ProxyConfigServiceLinux::ProxyConfigServiceLinux() |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 1257 | : delegate_(new Delegate(base::Environment::Create())) { |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1258 | } |
| 1259 | |
[email protected] | 8e1845e1 | 2010-09-15 19:22:24 | [diff] [blame] | 1260 | ProxyConfigServiceLinux::~ProxyConfigServiceLinux() { |
| 1261 | delegate_->PostDestroyTask(); |
| 1262 | } |
| 1263 | |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1264 | ProxyConfigServiceLinux::ProxyConfigServiceLinux( |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 1265 | base::Environment* env_var_getter) |
[email protected] | 9a3d8d4 | 2009-09-03 17:01:46 | [diff] [blame] | 1266 | : delegate_(new Delegate(env_var_getter)) { |
| 1267 | } |
| 1268 | |
| 1269 | ProxyConfigServiceLinux::ProxyConfigServiceLinux( |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 1270 | base::Environment* env_var_getter, |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 1271 | GConfSettingGetter* gconf_getter) |
| 1272 | : delegate_(new Delegate(env_var_getter, gconf_getter)) { |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1273 | } |
| 1274 | |
[email protected] | e4be2dd | 2010-12-14 00:44:39 | [diff] [blame] | 1275 | void ProxyConfigServiceLinux::AddObserver(Observer* observer) { |
| 1276 | delegate_->AddObserver(observer); |
| 1277 | } |
| 1278 | |
| 1279 | void ProxyConfigServiceLinux::RemoveObserver(Observer* observer) { |
| 1280 | delegate_->RemoveObserver(observer); |
| 1281 | } |
| 1282 | |
[email protected] | 3a29593d | 2011-04-11 10:07:52 | [diff] [blame] | 1283 | ProxyConfigService::ConfigAvailability |
| 1284 | ProxyConfigServiceLinux::GetLatestProxyConfig(ProxyConfig* config) { |
[email protected] | e4be2dd | 2010-12-14 00:44:39 | [diff] [blame] | 1285 | return delegate_->GetLatestProxyConfig(config); |
| 1286 | } |
| 1287 | |
[email protected] | 861c6c6 | 2009-04-20 16:50:56 | [diff] [blame] | 1288 | } // namespace net |