Skip to content

Commit

Permalink
Move EnvironmentVariableGetter from base/linux_util.h to base/env_var…
Browse files Browse the repository at this point in the history
….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
  • Loading branch information
thestig@chromium.org committed Apr 3, 2010
1 parent d5c8101 commit 9bc8cff
Show file tree
Hide file tree
Showing 19 changed files with 242 additions and 179 deletions.
4 changes: 2 additions & 2 deletions app/gtk_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <gtk/gtk.h>

#include "app/l10n_util.h"
#include "base/env_var.h"
#include "base/linux_util.h"
#include "base/logging.h"
#include "base/string_util.h"
Expand Down Expand Up @@ -56,8 +57,7 @@ void GetWidgetSizeFromCharacters(
void ApplyMessageDialogQuirks(GtkWidget* dialog) {
if (gtk_window_get_modal(GTK_WINDOW(dialog))) {
// Work around a KDE 3 window manager bug.
scoped_ptr<base::EnvironmentVariableGetter> env(
base::EnvironmentVariableGetter::Create());
scoped_ptr<base::EnvVarGetter> env(base::EnvVarGetter::Create());
if (base::DESKTOP_ENVIRONMENT_KDE3 == GetDesktopEnvironment(env.get()))
gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog), FALSE);
}
Expand Down
18 changes: 10 additions & 8 deletions base/base.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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',
Expand Down
6 changes: 3 additions & 3 deletions base/base_paths_posix.cc
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -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"
Expand Down Expand Up @@ -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;
Expand Down
80 changes: 80 additions & 0 deletions base/env_var.cc
Original file line number Diff line number Diff line change
@@ -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
33 changes: 33 additions & 0 deletions base/env_var.h
Original file line number Diff line number Diff line change
@@ -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_
70 changes: 17 additions & 53 deletions base/linux_util.cc
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -148,7 +119,7 @@ bool ProcPathGetInode(ino_t* inode_out, const char* path, bool log = false) {
return true;
}

} // anonymous namespace
} // namespace

namespace base {

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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));
}

Expand Down
Loading

0 comments on commit 9bc8cff

Please sign in to comment.