summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorthestig@chromium.org <thestig@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-04-03 01:05:39 +0000
committerthestig@chromium.org <thestig@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-04-03 01:05:39 +0000
commit9bc8cff2f20eb98d86cfa77f5e75b5b3efc80ced (patch)
tree969fefb9d6c5d42d974fccd4d640861dc880a73d /base
parentd5c8101d795d43c265d81277305958d38160ab19 (diff)
downloadchromium_src-9bc8cff2f20eb98d86cfa77f5e75b5b3efc80ced.zip
chromium_src-9bc8cff2f20eb98d86cfa77f5e75b5b3efc80ced.tar.gz
chromium_src-9bc8cff2f20eb98d86cfa77f5e75b5b3efc80ced.tar.bz2
Move EnvironmentVariableGetter from base/linux_util.h to base/env_var.h and rename it EnvVarGetter. Label base::SysInfo::{Get,Has}EnvVar as deprecated.
BUG=none TEST=none Review URL: http://codereview.chromium.org/1606007 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@43559 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r--base/base.gypi18
-rw-r--r--base/base_paths_posix.cc6
-rw-r--r--base/env_var.cc80
-rw-r--r--base/env_var.h33
-rw-r--r--base/linux_util.cc70
-rw-r--r--base/linux_util.h35
-rw-r--r--base/sys_info.h6
7 files changed, 158 insertions, 90 deletions
diff --git a/base/base.gypi b/base/base.gypi
index 153f827..df96e51 100644
--- a/base/base.gypi
+++ b/base/base.gypi
@@ -21,12 +21,12 @@
'third_party/icu/icu_utf.h',
'third_party/nspr/prtime.cc',
'third_party/nspr/prtime.h',
- 'atomicops_internals_x86_gcc.cc',
'at_exit.cc',
'at_exit.h',
'atomic_ref_count.h',
'atomic_sequence_num.h',
'atomicops.h',
+ 'atomicops_internals_x86_gcc.cc',
'atomicops_internals_x86_msvc.h',
'base_paths.cc',
'base_paths.h',
@@ -41,8 +41,8 @@
'bits.h',
'bzip2_error_handler.cc',
'callback.h',
- 'cancellation_flag.h',
'cancellation_flag.cc',
+ 'cancellation_flag.h',
'chrome_application_mac.h',
'chrome_application_mac.mm',
'cocoa_protocols_mac.h',
@@ -64,6 +64,8 @@
'dir_reader_fallback.h',
'dir_reader_linux.h',
'dir_reader_posix.h',
+ 'env_var.cc',
+ 'env_var.h',
'event_trace_consumer_win.h',
'event_trace_controller_win.cc',
'event_trace_controller_win.h',
@@ -84,8 +86,8 @@
'fix_wp64.h',
'float_util.h',
'foundation_utils_mac.h',
- 'global_descriptors_posix.h',
'global_descriptors_posix.cc',
+ 'global_descriptors_posix.h',
'hash_tables.h',
'histogram.cc',
'histogram.h',
@@ -104,8 +106,8 @@
'lock_impl_posix.cc',
'lock_impl_win.cc',
'logging.cc',
- 'logging_win.cc',
'logging.h',
+ 'logging_win.cc',
'mac_util.h',
'mac_util.mm',
'mach_ipc_mac.h',
@@ -140,15 +142,13 @@
'pickle.cc',
'pickle.h',
'platform_file.h',
- 'platform_file_win.cc',
'platform_file_posix.cc',
+ 'platform_file_win.cc',
'platform_thread.h',
'platform_thread_mac.mm',
'platform_thread_posix.cc',
'platform_thread_win.cc',
'port.h',
- 'profiler.cc',
- 'profiler.h',
'process.h',
'process_linux.cc',
'process_posix.cc',
@@ -158,6 +158,8 @@
'process_util_posix.cc',
'process_util_win.cc',
'process_win.cc',
+ 'profiler.cc',
+ 'profiler.h',
'rand_util.cc',
'rand_util.h',
'rand_util_posix.cc',
@@ -247,13 +249,13 @@
'tracked_objects.cc',
'tracked_objects.h',
'tuple.h',
+ 'unix_domain_socket_posix.cc',
'utf_offset_string_conversions.cc',
'utf_offset_string_conversions.h',
'utf_string_conversion_utils.cc',
'utf_string_conversion_utils.h',
'utf_string_conversions.cc',
'utf_string_conversions.h',
- 'unix_domain_socket_posix.cc',
'values.cc',
'values.h',
'waitable_event.h',
diff --git a/base/base_paths_posix.cc b/base/base_paths_posix.cc
index 51bb593..e44028e 100644
--- a/base/base_paths_posix.cc
+++ b/base/base_paths_posix.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -8,6 +8,7 @@
#include <unistd.h>
+#include "base/env_var.h"
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/linux_util.h"
@@ -62,8 +63,7 @@ bool PathProviderPosix(int key, FilePath* result) {
<< "Try running from your chromium/src directory.";
return false;
case base::DIR_USER_CACHE:
- scoped_ptr<base::EnvironmentVariableGetter> env(
- base::EnvironmentVariableGetter::Create());
+ scoped_ptr<base::EnvVarGetter> env(base::EnvVarGetter::Create());
FilePath cache_dir(base::GetXDGDirectory(env.get(), "XDG_CACHE_HOME",
".cache"));
*result = cache_dir;
diff --git a/base/env_var.cc b/base/env_var.cc
new file mode 100644
index 0000000..3075fda
--- /dev/null
+++ b/base/env_var.cc
@@ -0,0 +1,80 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/env_var.h"
+
+#if defined(OS_POSIX)
+#include <stdlib.h>
+#elif defined(OS_WIN)
+#include <windows.h>
+#endif
+
+#include "base/string_util.h"
+
+#if defined(OS_WIN)
+#include "base/scoped_ptr.h"
+#include "base/utf_string_conversions.h"
+#endif
+
+namespace {
+
+class EnvVarGetterImpl
+ : public base::EnvVarGetter {
+ public:
+ virtual bool GetEnv(const char* variable_name, std::string* result) {
+ if (GetEnvImpl(variable_name, result))
+ 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.
+ // I.e. HTTP_PROXY may be http_proxy for some users/systems.
+ 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;
+ return GetEnvImpl(alternate_case_var.c_str(), result);
+ }
+ private:
+ bool GetEnvImpl(const char* variable_name, std::string* result) {
+#if defined(OS_POSIX)
+ const char* env_value = getenv(variable_name);
+ if (!env_value)
+ return false;
+ // Note that the variable may be defined but empty.
+ if (result)
+ *result = env_value;
+ return true;
+#elif defined(OS_WIN)
+ DWORD value_length = ::GetEnvironmentVariable(
+ UTF8ToWide(variable_name).c_str(), NULL, 0);
+ if (value_length == 0)
+ return false;
+ if (result) {
+ scoped_array<wchar_t> value(new wchar_t[value_length]);
+ ::GetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), value.get(),
+ value_length);
+ *result = WideToUTF8(value.get());
+ }
+ return true;
+#else
+#error need to port
+#endif
+ }
+};
+
+} // namespace
+
+namespace base {
+
+// static
+EnvVarGetter* EnvVarGetter::Create() {
+ return new EnvVarGetterImpl();
+}
+
+} // namespace base
diff --git a/base/env_var.h b/base/env_var.h
new file mode 100644
index 0000000..af1ec79
--- /dev/null
+++ b/base/env_var.h
@@ -0,0 +1,33 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef BASE_ENV_VAR_H_
+#define BASE_ENV_VAR_H_
+
+#include <string>
+
+#include "base/basictypes.h"
+
+namespace base {
+
+// These are used to derive mocks for unittests.
+class EnvVarGetter {
+ public:
+ virtual ~EnvVarGetter() {}
+ // 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;
+
+ // Syntactic sugar for GetEnv(variable_name, NULL);
+ virtual bool HasEnv(const char* variable_name) {
+ return GetEnv(variable_name, NULL);
+ }
+
+ // Create an instance of EnvVarGetter
+ static EnvVarGetter* Create();
+};
+
+} // namespace base
+
+#endif // BASE_ENV_VAR_H_
diff --git a/base/linux_util.cc b/base/linux_util.cc
index e49a01f..b552b93 100644
--- a/base/linux_util.cc
+++ b/base/linux_util.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -14,6 +14,7 @@
#include <vector>
#include "base/command_line.h"
+#include "base/env_var.h"
#include "base/lock.h"
#include "base/path_service.h"
#include "base/process_util.h"
@@ -23,36 +24,6 @@
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;
- }
-};
-
// Not needed for OS_CHROMEOS.
#if defined(OS_LINUX)
enum LinuxDistroState {
@@ -148,7 +119,7 @@ bool ProcPathGetInode(ino_t* inode_out, const char* path, bool log = false) {
return true;
}
-} // anonymous namespace
+} // namespace
namespace base {
@@ -181,9 +152,9 @@ std::string linux_distro =
"Unknown";
#endif
-FilePath GetHomeDir(EnvironmentVariableGetter* env) {
+FilePath GetHomeDir(EnvVarGetter* env) {
std::string home_dir;
- if (env->Getenv("HOME", &home_dir) && !home_dir.empty())
+ if (env->GetEnv("HOME", &home_dir) && !home_dir.empty())
return FilePath(home_dir);
home_dir = g_get_home_dir();
@@ -236,16 +207,16 @@ std::string GetLinuxDistro() {
#endif
}
-FilePath GetXDGDirectory(EnvironmentVariableGetter* env,
- const char* env_name, const char* fallback_dir) {
+FilePath GetXDGDirectory(EnvVarGetter* env, const char* env_name,
+ const char* fallback_dir) {
std::string env_value;
- if (env->Getenv(env_name, &env_value) && !env_value.empty())
+ if (env->GetEnv(env_name, &env_value) && !env_value.empty())
return FilePath(env_value);
return GetHomeDir(env).Append(fallback_dir);
}
-FilePath GetXDGUserDirectory(EnvironmentVariableGetter* env,
- const char* dir_name, const char* fallback_dir) {
+FilePath GetXDGUserDirectory(EnvVarGetter* env, const char* dir_name,
+ const char* fallback_dir) {
char* xdg_dir = xdg_user_dir_lookup(dir_name);
if (xdg_dir) {
FilePath rv(xdg_dir);
@@ -255,22 +226,16 @@ FilePath GetXDGUserDirectory(EnvironmentVariableGetter* env,
return GetHomeDir(env).Append(fallback_dir);
}
-// static
-EnvironmentVariableGetter* EnvironmentVariableGetter::Create() {
- return new EnvironmentVariableGetterImpl();
-}
-
-DesktopEnvironment GetDesktopEnvironment(EnvironmentVariableGetter* env) {
+DesktopEnvironment GetDesktopEnvironment(EnvVarGetter* env) {
std::string desktop_session;
- if (env->Getenv("DESKTOP_SESSION", &desktop_session)) {
+ if (env->GetEnv("DESKTOP_SESSION", &desktop_session)) {
if (desktop_session == "gnome")
return DESKTOP_ENVIRONMENT_GNOME;
else if (desktop_session == "kde4")
return DESKTOP_ENVIRONMENT_KDE4;
else if (desktop_session == "kde") {
// This may mean KDE4 on newer systems, so we have to check.
- std::string dummy;
- if (env->Getenv("KDE_SESSION_VERSION", &dummy))
+ if (env->HasEnv("KDE_SESSION_VERSION"))
return DESKTOP_ENVIRONMENT_KDE4;
return DESKTOP_ENVIRONMENT_KDE3;
}
@@ -280,11 +245,10 @@ DesktopEnvironment GetDesktopEnvironment(EnvironmentVariableGetter* env) {
// Fall back on some older environment variables.
// Useful particularly in the DESKTOP_SESSION=default case.
- std::string dummy;
- if (env->Getenv("GNOME_DESKTOP_SESSION_ID", &dummy)) {
+ if (env->HasEnv("GNOME_DESKTOP_SESSION_ID")) {
return DESKTOP_ENVIRONMENT_GNOME;
- } else if (env->Getenv("KDE_FULL_SESSION", &dummy)) {
- if (env->Getenv("KDE_SESSION_VERSION", &dummy))
+ } else if (env->HasEnv("KDE_FULL_SESSION")) {
+ if (env->HasEnv("KDE_SESSION_VERSION"))
return DESKTOP_ENVIRONMENT_KDE4;
return DESKTOP_ENVIRONMENT_KDE3;
}
@@ -308,7 +272,7 @@ const char* GetDesktopEnvironmentName(DesktopEnvironment env) {
return NULL;
}
-const char* GetDesktopEnvironmentName(EnvironmentVariableGetter* env) {
+const char* GetDesktopEnvironmentName(EnvVarGetter* env) {
return GetDesktopEnvironmentName(GetDesktopEnvironment(env));
}
diff --git a/base/linux_util.h b/base/linux_util.h
index 2139746..370a65e 100644
--- a/base/linux_util.h
+++ b/base/linux_util.h
@@ -1,15 +1,16 @@
-// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#ifndef BASE_LINUX_UTIL_H__
-#define BASE_LINUX_UTIL_H__
+#ifndef BASE_LINUX_UTIL_H_
+#define BASE_LINUX_UTIL_H_
#include <stdint.h>
#include <sys/types.h>
#include <string>
+class EnvVarGetter;
class FilePath;
namespace base {
@@ -25,34 +26,22 @@ uint8_t* BGRAToRGBA(const uint8_t* pixels, int width, int height, int stride);
// 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();
-};
-
// Get the home directory.
-FilePath GetHomeDir(EnvironmentVariableGetter* env);
+FilePath GetHomeDir(EnvVarGetter* env);
// Utility function for getting XDG directories.
// |env_name| is the name of an environment variable that we want to use to get
// a directory path. |fallback_dir| is the directory relative to $HOME that we
// use if |env_name| cannot be found or is empty. |fallback_dir| may be NULL.
// Examples of |env_name| are XDG_CONFIG_HOME and XDG_DATA_HOME.
-FilePath GetXDGDirectory(EnvironmentVariableGetter* env,
- const char* env_name, const char* fallback_dir);
+FilePath GetXDGDirectory(EnvVarGetter* env, const char* env_name,
+ const char* fallback_dir);
// Wrapper around xdg_user_dir_lookup() from src/base/third_party/xdg-user-dirs
// This looks up "well known" user directories like the desktop and music
// folder. Examples of |dir_name| are DESKTOP and MUSIC.
-FilePath GetXDGUserDirectory(EnvironmentVariableGetter* env,
- const char* dir_name, const char* fallback_dir);
+FilePath GetXDGUserDirectory(EnvVarGetter* env, const char* dir_name,
+ const char* fallback_dir);
enum DesktopEnvironment {
DESKTOP_ENVIRONMENT_OTHER,
@@ -68,13 +57,13 @@ enum DesktopEnvironment {
// of which desktop environment we're using. We use this to know when
// to attempt to use preferences from the desktop environment --
// proxy settings, password manager, etc.
-DesktopEnvironment GetDesktopEnvironment(EnvironmentVariableGetter* env);
+DesktopEnvironment GetDesktopEnvironment(EnvVarGetter* env);
// Return a string representation of the given desktop environment.
// May return NULL in the case of DESKTOP_ENVIRONMENT_OTHER.
const char* GetDesktopEnvironmentName(DesktopEnvironment env);
// Convenience wrapper that calls GetDesktopEnvironment() first.
-const char* GetDesktopEnvironmentName(EnvironmentVariableGetter* env);
+const char* GetDesktopEnvironmentName(EnvVarGetter* env);
// Return the inode number for the UNIX domain socket |fd|.
bool FileDescriptorGetInode(ino_t* inode_out, int fd);
@@ -85,4 +74,4 @@ bool FindProcessHoldingSocket(pid_t* pid_out, ino_t socket_inode);
} // namespace base
-#endif // BASE_LINUX_UTIL_H__
+#endif // BASE_LINUX_UTIL_H_
diff --git a/base/sys_info.h b/base/sys_info.h
index 415dc81..0f27aa7 100644
--- a/base/sys_info.h
+++ b/base/sys_info.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2008 The Chromium Authors. All rights reserved.
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -31,12 +31,12 @@ class SysInfo {
static int64 AmountOfFreeDiskSpace(const FilePath& path);
// Return true if the given environment variable is defined.
- // TODO: find a better place for HasEnvVar.
+ // DEPRECATED in favor of EnvVarGetter in base/env_var.h.
static bool HasEnvVar(const wchar_t* var);
// Return the value of the given environment variable
// or an empty string if not defined.
- // TODO: find a better place for GetEnvVar.
+ // DEPRECATED in favor of EnvVarGetter in base/env_var.h.
static std::wstring GetEnvVar(const wchar_t* var);
// Returns the name of the host operating system.