Try again: Add proxy config (using gnome-network-preferences)

BUG=11507
TEST=Open options, click change proxy, gnome-network-preferences should launch.  If gnome isn't installed or running, LinuxProxyConfig wiki page should load.

Review URL: http://codereview.chromium.org/155792

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@21246 0039d316-1c4b-4281-b951-d872f2087c98


CrOS-Libchrome-Original-Commit: 87fc168b364ef36033f72e545a4894bd7ce9354f
diff --git a/base/linux_util.cc b/base/linux_util.cc
index 604980b..bc99d44 100644
--- a/base/linux_util.cc
+++ b/base/linux_util.cc
@@ -10,6 +10,41 @@
 
 #include "base/command_line.h"
 #include "base/process_util.h"
+#include "base/string_util.h"
+
+namespace {
+
+class EnvironmentVariableGetterImpl
+    : public base::EnvironmentVariableGetter {
+ public:
+  virtual bool Getenv(const char* variable_name, std::string* result) {
+    const char* env_value = ::getenv(variable_name);
+    if (env_value) {
+      // Note that the variable may be defined but empty.
+      *result = env_value;
+      return true;
+    }
+    // Some commonly used variable names are uppercase while others
+    // are lowercase, which is inconsistent. Let's try to be helpful
+    // and look for a variable name with the reverse case.
+    char first_char = variable_name[0];
+    std::string alternate_case_var;
+    if (first_char >= 'a' && first_char <= 'z')
+      alternate_case_var = StringToUpperASCII(std::string(variable_name));
+    else if (first_char >= 'A' && first_char <= 'Z')
+      alternate_case_var = StringToLowerASCII(std::string(variable_name));
+    else
+      return false;
+    env_value = ::getenv(alternate_case_var.c_str());
+    if (env_value) {
+      *result = env_value;
+      return true;
+    }
+    return false;
+  }
+};
+
+} // anonymous namespace
 
 namespace base {
 
@@ -59,4 +94,19 @@
   return linux_distro;
 }
 
+// static
+EnvironmentVariableGetter* EnvironmentVariableGetter::Create() {
+  return new EnvironmentVariableGetterImpl();
+}
+
+bool UseGnomeForSettings(EnvironmentVariableGetter* env_var_getter) {
+  // GNOME_DESKTOP_SESSION_ID being defined is a good indication that
+  // we are probably running under GNOME.
+  // Note: KDE_FULL_SESSION is a corresponding env var to recognize KDE.
+  std::string dummy, desktop_session;
+  return env_var_getter->Getenv("GNOME_DESKTOP_SESSION_ID", &dummy)
+      || (env_var_getter->Getenv("DESKTOP_SESSION", &desktop_session)
+          && desktop_session == "gnome");
+}
+
 }  // namespace base
diff --git a/base/linux_util.h b/base/linux_util.h
index 973a2b0..5a46481 100644
--- a/base/linux_util.h
+++ b/base/linux_util.h
@@ -20,6 +20,24 @@
 // GetWinVersion() in base/win_util.h.
 std::string GetLinuxDistro();
 
+// These are used to derive mocks for unittests.
+class EnvironmentVariableGetter {
+ public:
+  virtual ~EnvironmentVariableGetter() {}
+  // Gets an environment variable's value and stores it in
+  // result. Returns false if the key is unset.
+  virtual bool Getenv(const char* variable_name, std::string* result) = 0;
+
+  // Create an instance of EnvironmentVariableGetter
+  static EnvironmentVariableGetter* Create();
+};
+
+// Return true if we appear to be running under Gnome and should attempt to use
+// some prefrences from the desktop environment (eg proxy settings),
+// If someone adds support for other environments, this function could be
+// replaced with one that returns an enum so we an specify Gnome, KDE, etc.
+bool UseGnomeForSettings(EnvironmentVariableGetter* env_var_getter);
+
 }  // namespace base
 
 #endif  // BASE_LINUX_UTIL_H__