diff options
author | thestig@chromium.org <thestig@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-27 01:04:53 +0000 |
---|---|---|
committer | thestig@chromium.org <thestig@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-27 01:04:53 +0000 |
commit | 6b01b1096cdfff0142fe3d4e5cddadcfa394d2d7 (patch) | |
tree | 6a915ab9aeb477cd644997a95696547f7d6216d2 /chrome/common/chrome_paths_linux.cc | |
parent | d2064f5340bb8a515019d0fb2d08bbe8908afd45 (diff) | |
download | chromium_src-6b01b1096cdfff0142fe3d4e5cddadcfa394d2d7.zip chromium_src-6b01b1096cdfff0142fe3d4e5cddadcfa394d2d7.tar.gz chromium_src-6b01b1096cdfff0142fe3d4e5cddadcfa394d2d7.tar.bz2 |
Use xdg_user_dir_lookup to lookup directories.
Reviewed in issue 27186.
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@10555 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common/chrome_paths_linux.cc')
-rw-r--r-- | chrome/common/chrome_paths_linux.cc | 62 |
1 files changed, 42 insertions, 20 deletions
diff --git a/chrome/common/chrome_paths_linux.cc b/chrome/common/chrome_paths_linux.cc index f416710..93556f1 100644 --- a/chrome/common/chrome_paths_linux.cc +++ b/chrome/common/chrome_paths_linux.cc @@ -5,31 +5,53 @@ #include "chrome/common/chrome_paths_internal.h" #include <glib.h> +#include <stdlib.h> + #include "base/file_path.h" #include "base/logging.h" +#include "base/path_service.h" +#include "chrome/third_party/xdg_user_dirs/xdg_user_dir_lookup.h" namespace { -// |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. -// TODO(thestig): Don't use g_getenv() here because most of the time XDG -// environment variables won't actually be loaded. -FilePath GetStandardDirectory(const char* env_name, const char* fallback_dir) { +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; - const char* env_value = g_getenv(env_name); - if (env_value && env_value[0]) { - rv = FilePath(env_value); - } else { - const char* home_dir = g_getenv("HOME"); - if (!home_dir) - home_dir = g_get_home_dir(); - rv = FilePath(home_dir); - if (fallback_dir) - rv = rv.Append(fallback_dir); + if (PathService::Get(base::DIR_TEMP, &rv)) + return rv; + + /* last resort */ + return FilePath("/tmp/"); +} + +// Wrapper around xdg_user_dir_lookup() from +// src/chrome/third_party/xdg-user-dirs +FilePath GetXDGUserDirectory(const char* env_name, const char* fallback_dir) { + char* xdg_dir = xdg_user_dir_lookup(env_name); + if (xdg_dir) { + FilePath rv(xdg_dir); + free(xdg_dir); + return rv; } + return GetHomeDir().Append(fallback_dir); +} - return rv; +// |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. +FilePath GetXDGDirectory(const char* env_name, const char* fallback_dir) { + const char* env_value = getenv(env_name); + if (env_value && env_value[0]) + return FilePath(env_value); + return GetHomeDir().Append(fallback_dir); } } // namespace @@ -42,7 +64,7 @@ namespace chrome { // ~/.config/google-chrome/ for official builds. // (This also helps us sidestep issues with other apps grabbing ~/.chromium .) bool GetDefaultUserDataDirectory(FilePath* result) { - FilePath config_dir = GetStandardDirectory("XDG_CONFIG_HOME", ".config"); + FilePath config_dir(GetXDGDirectory("XDG_CONFIG_HOME", ".config")); #if defined(GOOGLE_CHROME_BUILD) *result = config_dir.Append("google-chrome"); #else @@ -52,12 +74,12 @@ bool GetDefaultUserDataDirectory(FilePath* result) { } bool GetUserDocumentsDirectory(FilePath* result) { - *result = GetStandardDirectory("XDG_DOCUMENTS_DIR", "Documents"); + *result = GetXDGUserDirectory("DOCUMENTS", "Documents"); return true; } bool GetUserDesktop(FilePath* result) { - *result = GetStandardDirectory("XDG_DESKTOP_DIR", "Desktop"); + *result = GetXDGUserDirectory("DESKTOP", "Desktop"); return true; } |