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