diff options
author | evan@chromium.org <evan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-22 23:28:05 +0000 |
---|---|---|
committer | evan@chromium.org <evan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-22 23:28:05 +0000 |
commit | 1c657854c20ee288f8812c707d4bf4fcfecb7495 (patch) | |
tree | 2d157e936beea91a3f9ad2385999ad27804ec5d4 /base | |
parent | a1170ed12a730f79d4224779b90e35923c7bb729 (diff) | |
download | chromium_src-1c657854c20ee288f8812c707d4bf4fcfecb7495.zip chromium_src-1c657854c20ee288f8812c707d4bf4fcfecb7495.tar.gz chromium_src-1c657854c20ee288f8812c707d4bf4fcfecb7495.tar.bz2 |
bsd: refactor XDG bits of linux_util into a shared file.
This allows the shared code to build on the BSDs without
them needing to build _linux files.
Review URL: http://codereview.chromium.org/1701005
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@45380 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/base.gyp | 2 | ||||
-rw-r--r-- | base/base.gypi | 8 | ||||
-rw-r--r-- | base/base_paths_posix.cc | 2 | ||||
-rw-r--r-- | base/file_util.h | 4 | ||||
-rw-r--r-- | base/file_util_posix.cc | 20 | ||||
-rw-r--r-- | base/linux_util.cc | 87 | ||||
-rw-r--r-- | base/linux_util.h | 39 | ||||
-rw-r--r-- | base/mime_util_xdg.cc (renamed from base/mime_util_linux.cc) | 0 | ||||
-rw-r--r-- | base/xdg_util.cc | 83 | ||||
-rw-r--r-- | base/xdg_util.h | 56 | ||||
-rw-r--r-- | base/xdg_util_unittest.cc (renamed from base/linux_util_unittest.cc) | 14 |
11 files changed, 178 insertions, 137 deletions
diff --git a/base/base.gyp b/base/base.gyp index 938b05c..e3a236c 100644 --- a/base/base.gyp +++ b/base/base.gyp @@ -171,7 +171,7 @@ 'worker_pool_linux_unittest.cc', ], 'sources': [ - 'linux_util_unittest.cc', + 'xdg_util_unittest.cc', ], 'dependencies': [ '../build/linux/system.gyp:gtk', diff --git a/base/base.gypi b/base/base.gypi index d57e587..9b5c41b 100644 --- a/base/base.gypi +++ b/base/base.gypi @@ -123,7 +123,7 @@ 'message_pump_win.cc', 'message_pump_win.h', 'mime_util.h', - 'mime_util_linux.cc', + 'mime_util_xdg.cc', 'move.h', 'native_library.h', 'native_library_linux.cc', @@ -279,6 +279,8 @@ 'worker_pool_mac.h', 'worker_pool_mac.mm', 'worker_pool_win.cc', + 'xdg_util.h', + 'xdg_util.cc', ], 'include_dirs': [ '..', @@ -294,18 +296,20 @@ [ 'OS != "linux" and OS != "freebsd" and OS != "openbsd" and OS != "solaris"', { 'sources/': [ ['exclude', '/xdg_user_dirs/'], + ['exclude', '/xdg_[^/]*\\.cc$'], ['exclude', '_nss\.cc$'], ], 'sources!': [ 'atomicops_internals_x86_gcc.cc', 'base_paths_posix.cc', - 'linux_util.cc', 'message_pump_glib.cc', + 'xdg_util.cc', ], }], [ 'OS != "linux"', { 'sources!': [ # Not automatically excluded by the *linux.cc rules. + 'linux_util.cc', 'setproctitle_linux.c', 'setproctitle_linux.h', ], diff --git a/base/base_paths_posix.cc b/base/base_paths_posix.cc index ad50318..d2ecf33 100644 --- a/base/base_paths_posix.cc +++ b/base/base_paths_posix.cc @@ -11,11 +11,11 @@ #include "base/env_var.h" #include "base/file_path.h" #include "base/file_util.h" -#include "base/linux_util.h" #include "base/logging.h" #include "base/path_service.h" #include "base/scoped_ptr.h" #include "base/sys_string_conversions.h" +#include "base/xdg_util.h" namespace base { diff --git a/base/file_util.h b/base/file_util.h index 4473678..96891e6 100644 --- a/base/file_util.h +++ b/base/file_util.h @@ -226,6 +226,10 @@ bool GetTempDir(FilePath* path); // Only useful on POSIX; redirects to GetTempDir() on Windows. bool GetShmemTempDir(FilePath* path); +// Get the home directory. This is more complicated than just getenv("HOME") +// as it knows to fall back on getpwent() etc. +FilePath GetHomeDir(); + // Creates a temporary file. The full path is placed in |path|, and the // function returns true if was successful in creating the file. The file will // be empty and all handles closed after this function returns. diff --git a/base/file_util_posix.cc b/base/file_util_posix.cc index 8947d6a..4d4e572 100644 --- a/base/file_util_posix.cc +++ b/base/file_util_posix.cc @@ -10,6 +10,7 @@ #include <fnmatch.h> #include <libgen.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> #include <sys/errno.h> #include <sys/mman.h> @@ -21,6 +22,8 @@ #if defined(OS_MACOSX) #include <AvailabilityMacros.h> +#else +#include <glib.h> #endif #include <fstream> @@ -728,6 +731,23 @@ bool GetShmemTempDir(FilePath* path) { return true; } +FilePath GetHomeDir() { + const char* home_dir = getenv("HOME"); + if (home_dir && home_dir[0]) + return FilePath(home_dir); + + home_dir = g_get_home_dir(); + if (home_dir && home_dir[0]) + return FilePath(home_dir); + + FilePath rv; + if (file_util::GetTempDir(&rv)) + return rv; + + // Last resort. + return FilePath("/tmp"); +} + bool CopyFile(const FilePath& from_path, const FilePath& to_path) { int infile = open(from_path.value().c_str(), O_RDONLY); if (infile < 0) diff --git a/base/linux_util.cc b/base/linux_util.cc index 6197bc3..0ad9bb0 100644 --- a/base/linux_util.cc +++ b/base/linux_util.cc @@ -20,7 +20,6 @@ #include "base/process_util.h" #include "base/singleton.h" #include "base/string_util.h" -#include "base/third_party/xdg_user_dirs/xdg_user_dir_lookup.h" namespace { @@ -152,23 +151,6 @@ std::string linux_distro = "Unknown"; #endif -FilePath GetHomeDir(EnvVarGetter* env) { - std::string home_dir; - if (env->GetEnv("HOME", &home_dir) && !home_dir.empty()) - return FilePath(home_dir); - - home_dir = g_get_home_dir(); - if (!home_dir.empty()) - return FilePath(home_dir); - - FilePath rv; - if (PathService::Get(base::DIR_TEMP, &rv)) - return rv; - - // Last resort. - return FilePath("/tmp"); -} - std::string GetLinuxDistro() { #if defined(OS_CHROMEOS) return linux_distro; @@ -207,75 +189,6 @@ std::string GetLinuxDistro() { #endif } -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()) - return FilePath(env_value); - return GetHomeDir(env).Append(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); - free(xdg_dir); - return rv; - } - return GetHomeDir(env).Append(fallback_dir); -} - -DesktopEnvironment GetDesktopEnvironment(EnvVarGetter* env) { - std::string 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. - if (env->HasEnv("KDE_SESSION_VERSION")) - return DESKTOP_ENVIRONMENT_KDE4; - return DESKTOP_ENVIRONMENT_KDE3; - } else if (desktop_session.find("xfce") != std::string::npos) { - return DESKTOP_ENVIRONMENT_XFCE; - } - } - - // Fall back on some older environment variables. - // Useful particularly in the DESKTOP_SESSION=default case. - if (env->HasEnv("GNOME_DESKTOP_SESSION_ID")) { - return DESKTOP_ENVIRONMENT_GNOME; - } else if (env->HasEnv("KDE_FULL_SESSION")) { - if (env->HasEnv("KDE_SESSION_VERSION")) - return DESKTOP_ENVIRONMENT_KDE4; - return DESKTOP_ENVIRONMENT_KDE3; - } - - return DESKTOP_ENVIRONMENT_OTHER; -} - -const char* GetDesktopEnvironmentName(DesktopEnvironment env) { - switch (env) { - case DESKTOP_ENVIRONMENT_OTHER: - return NULL; - case DESKTOP_ENVIRONMENT_GNOME: - return "GNOME"; - case DESKTOP_ENVIRONMENT_KDE3: - return "KDE3"; - case DESKTOP_ENVIRONMENT_KDE4: - return "KDE4"; - case DESKTOP_ENVIRONMENT_XFCE: - return "XFCE"; - } - return NULL; -} - -const char* GetDesktopEnvironmentName(EnvVarGetter* env) { - return GetDesktopEnvironmentName(GetDesktopEnvironment(env)); -} - bool FileDescriptorGetInode(ino_t* inode_out, int fd) { DCHECK(inode_out); diff --git a/base/linux_util.h b/base/linux_util.h index 1458cfb..2550f70 100644 --- a/base/linux_util.h +++ b/base/linux_util.h @@ -27,45 +27,6 @@ uint8_t* BGRAToRGBA(const uint8_t* pixels, int width, int height, int stride); // GetWinVersion() in base/win_util.h. std::string GetLinuxDistro(); -// Get the home directory. -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(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(EnvVarGetter* env, const char* dir_name, - const char* fallback_dir); - -enum DesktopEnvironment { - DESKTOP_ENVIRONMENT_OTHER, - DESKTOP_ENVIRONMENT_GNOME, - // KDE3 and KDE4 are sufficiently different that we count - // them as two different desktop environments here. - DESKTOP_ENVIRONMENT_KDE3, - DESKTOP_ENVIRONMENT_KDE4, - DESKTOP_ENVIRONMENT_XFCE, -}; - -// Return an entry from the DesktopEnvironment enum with a best guess -// 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(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(EnvVarGetter* env); - // Return the inode number for the UNIX domain socket |fd|. bool FileDescriptorGetInode(ino_t* inode_out, int fd); diff --git a/base/mime_util_linux.cc b/base/mime_util_xdg.cc index b42122f..b42122f 100644 --- a/base/mime_util_linux.cc +++ b/base/mime_util_xdg.cc diff --git a/base/xdg_util.cc b/base/xdg_util.cc new file mode 100644 index 0000000..0ff6c47 --- /dev/null +++ b/base/xdg_util.cc @@ -0,0 +1,83 @@ +// 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/xdg_util.h" + +#include "base/env_var.h" +#include "base/file_path.h" +#include "base/file_util.h" +#include "base/third_party/xdg_user_dirs/xdg_user_dir_lookup.h" + +namespace base { + +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()) + return FilePath(env_value); + return file_util::GetHomeDir().Append(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); + free(xdg_dir); + return rv; + } + return file_util::GetHomeDir().Append(fallback_dir); +} + +DesktopEnvironment GetDesktopEnvironment(EnvVarGetter* env) { + std::string 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. + if (env->HasEnv("KDE_SESSION_VERSION")) + return DESKTOP_ENVIRONMENT_KDE4; + return DESKTOP_ENVIRONMENT_KDE3; + } else if (desktop_session.find("xfce") != std::string::npos) { + return DESKTOP_ENVIRONMENT_XFCE; + } + } + + // Fall back on some older environment variables. + // Useful particularly in the DESKTOP_SESSION=default case. + if (env->HasEnv("GNOME_DESKTOP_SESSION_ID")) { + return DESKTOP_ENVIRONMENT_GNOME; + } else if (env->HasEnv("KDE_FULL_SESSION")) { + if (env->HasEnv("KDE_SESSION_VERSION")) + return DESKTOP_ENVIRONMENT_KDE4; + return DESKTOP_ENVIRONMENT_KDE3; + } + + return DESKTOP_ENVIRONMENT_OTHER; +} + +const char* GetDesktopEnvironmentName(DesktopEnvironment env) { + switch (env) { + case DESKTOP_ENVIRONMENT_OTHER: + return NULL; + case DESKTOP_ENVIRONMENT_GNOME: + return "GNOME"; + case DESKTOP_ENVIRONMENT_KDE3: + return "KDE3"; + case DESKTOP_ENVIRONMENT_KDE4: + return "KDE4"; + case DESKTOP_ENVIRONMENT_XFCE: + return "XFCE"; + } + return NULL; +} + +const char* GetDesktopEnvironmentName(EnvVarGetter* env) { + return GetDesktopEnvironmentName(GetDesktopEnvironment(env)); +} + +} // namespace base diff --git a/base/xdg_util.h b/base/xdg_util.h new file mode 100644 index 0000000..b69ce51 --- /dev/null +++ b/base/xdg_util.h @@ -0,0 +1,56 @@ +// 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_XDG_UTIL_H_ +#define BASE_XDG_UTIL_H_ + +// XDG refers to http://en.wikipedia.org/wiki/Freedesktop.org . +// This file contains utilities found across free desktop +// environments. + +class FilePath; + +namespace base { + +class EnvVarGetter; + +// 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(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(EnvVarGetter* env, const char* dir_name, + const char* fallback_dir); + +enum DesktopEnvironment { + DESKTOP_ENVIRONMENT_OTHER, + DESKTOP_ENVIRONMENT_GNOME, + // KDE3 and KDE4 are sufficiently different that we count + // them as two different desktop environments here. + DESKTOP_ENVIRONMENT_KDE3, + DESKTOP_ENVIRONMENT_KDE4, + DESKTOP_ENVIRONMENT_XFCE, +}; + +// Return an entry from the DesktopEnvironment enum with a best guess +// 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(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(EnvVarGetter* env); + +} // namespace base + +#endif // BASE_XDG_UTIL_H_ diff --git a/base/linux_util_unittest.cc b/base/xdg_util_unittest.cc index d904080..d5ddc86 100644 --- a/base/linux_util_unittest.cc +++ b/base/xdg_util_unittest.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "base/linux_util.h" +#include "base/xdg_util.h" #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" @@ -14,13 +14,13 @@ using ::testing::Return; using ::testing::SetArgumentPointee; using ::testing::StrEq; +namespace { + class MockEnvVarGetter : public base::EnvVarGetter { public: MOCK_METHOD2(GetEnv, bool(const char*, std::string* result)); }; -namespace { - const char* kGnome = "gnome"; const char* kKDE4 = "kde4"; const char* kKDE = "kde"; @@ -28,7 +28,7 @@ const char* kXFCE = "xfce"; } // namespace -TEST(LinuxUtilTest, GetDesktopEnvironmentGnome) { +TEST(XDGUtilTest, GetDesktopEnvironmentGnome) { MockEnvVarGetter getter; EXPECT_CALL(getter, GetEnv(_, _)).WillRepeatedly(Return(false)); EXPECT_CALL(getter, GetEnv(StrEq("DESKTOP_SESSION"), _)) @@ -38,7 +38,7 @@ TEST(LinuxUtilTest, GetDesktopEnvironmentGnome) { base::GetDesktopEnvironment(&getter)); } -TEST(LinuxUtilTest, GetDesktopEnvironmentKDE4) { +TEST(XDGUtilTest, GetDesktopEnvironmentKDE4) { MockEnvVarGetter getter; EXPECT_CALL(getter, GetEnv(_, _)).WillRepeatedly(Return(false)); EXPECT_CALL(getter, GetEnv(StrEq("DESKTOP_SESSION"), _)) @@ -48,7 +48,7 @@ TEST(LinuxUtilTest, GetDesktopEnvironmentKDE4) { base::GetDesktopEnvironment(&getter)); } -TEST(LinuxUtilTest, GetDesktopEnvironmentKDE3) { +TEST(XDGUtilTest, GetDesktopEnvironmentKDE3) { MockEnvVarGetter getter; EXPECT_CALL(getter, GetEnv(_, _)).WillRepeatedly(Return(false)); EXPECT_CALL(getter, GetEnv(StrEq("DESKTOP_SESSION"), _)) @@ -58,7 +58,7 @@ TEST(LinuxUtilTest, GetDesktopEnvironmentKDE3) { base::GetDesktopEnvironment(&getter)); } -TEST(LinuxUtilTest, GetDesktopEnvironmentXFCE) { +TEST(XDGUtilTest, GetDesktopEnvironmentXFCE) { MockEnvVarGetter getter; EXPECT_CALL(getter, GetEnv(_, _)).WillRepeatedly(Return(false)); EXPECT_CALL(getter, GetEnv(StrEq("DESKTOP_SESSION"), _)) |