OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/common/chrome_paths_internal.h" | 5 #include "chrome/common/chrome_paths_internal.h" |
6 | 6 |
7 #include <glib.h> | 7 #include <glib.h> |
| 8 #include <stdlib.h> |
| 9 |
8 #include "base/file_path.h" | 10 #include "base/file_path.h" |
9 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/path_service.h" |
| 13 #include "chrome/third_party/xdg_user_dirs/xdg_user_dir_lookup.h" |
10 | 14 |
11 namespace { | 15 namespace { |
12 | 16 |
| 17 FilePath GetHomeDir() { |
| 18 FilePath rv; |
| 19 const char *home_dir = getenv("HOME"); |
| 20 |
| 21 if (home_dir && home_dir[0]) |
| 22 return FilePath(home_dir); |
| 23 |
| 24 home_dir = g_get_home_dir(); |
| 25 if (home_dir && home_dir[0]) |
| 26 return FilePath(home_dir); |
| 27 |
| 28 if (PathService::Get(base::DIR_TEMP, &rv)) |
| 29 return rv; |
| 30 |
| 31 /* last resort */ |
| 32 return FilePath("/tmp/"); |
| 33 } |
| 34 |
| 35 // Wrapper around xdg_user_dir_lookup() from |
| 36 // src/chrome/third_party/xdg-user-dirs |
| 37 FilePath GetXDGUserDirectory(const char* env_name, const char* fallback_dir) { |
| 38 char* xdg_dir = xdg_user_dir_lookup(env_name); |
| 39 if (xdg_dir) { |
| 40 FilePath rv(xdg_dir); |
| 41 free(xdg_dir); |
| 42 return rv; |
| 43 } |
| 44 return GetHomeDir().Append(fallback_dir); |
| 45 } |
| 46 |
13 // |env_name| is the name of an environment variable that we want to use to get | 47 // |env_name| is the name of an environment variable that we want to use to get |
14 // a directory path. |fallback_dir| is the directory relative to $HOME that we | 48 // a directory path. |fallback_dir| is the directory relative to $HOME that we |
15 // use if |env_name| cannot be found or is empty. |fallback_dir| may be NULL. | 49 // use if |env_name| cannot be found or is empty. |fallback_dir| may be NULL. |
16 // TODO(thestig): Don't use g_getenv() here because most of the time XDG | 50 FilePath GetXDGDirectory(const char* env_name, const char* fallback_dir) { |
17 // environment variables won't actually be loaded. | 51 const char* env_value = getenv(env_name); |
18 FilePath GetStandardDirectory(const char* env_name, const char* fallback_dir) { | 52 if (env_value && env_value[0]) |
19 FilePath rv; | 53 return FilePath(env_value); |
20 const char* env_value = g_getenv(env_name); | 54 return GetHomeDir().Append(fallback_dir); |
21 if (env_value && env_value[0]) { | |
22 rv = FilePath(env_value); | |
23 } else { | |
24 const char* home_dir = g_getenv("HOME"); | |
25 if (!home_dir) | |
26 home_dir = g_get_home_dir(); | |
27 rv = FilePath(home_dir); | |
28 if (fallback_dir) | |
29 rv = rv.Append(fallback_dir); | |
30 } | |
31 | |
32 return rv; | |
33 } | 55 } |
34 | 56 |
35 } // namespace | 57 } // namespace |
36 | 58 |
37 namespace chrome { | 59 namespace chrome { |
38 | 60 |
39 // See http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html | 61 // See http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html |
40 // for a spec on where config files go. The net effect for most | 62 // for a spec on where config files go. The net effect for most |
41 // systems is we use ~/.config/chromium/ for Chromium and | 63 // systems is we use ~/.config/chromium/ for Chromium and |
42 // ~/.config/google-chrome/ for official builds. | 64 // ~/.config/google-chrome/ for official builds. |
43 // (This also helps us sidestep issues with other apps grabbing ~/.chromium .) | 65 // (This also helps us sidestep issues with other apps grabbing ~/.chromium .) |
44 bool GetDefaultUserDataDirectory(FilePath* result) { | 66 bool GetDefaultUserDataDirectory(FilePath* result) { |
45 FilePath config_dir = GetStandardDirectory("XDG_CONFIG_HOME", ".config"); | 67 FilePath config_dir(GetXDGDirectory("XDG_CONFIG_HOME", ".config")); |
46 #if defined(GOOGLE_CHROME_BUILD) | 68 #if defined(GOOGLE_CHROME_BUILD) |
47 *result = config_dir.Append("google-chrome"); | 69 *result = config_dir.Append("google-chrome"); |
48 #else | 70 #else |
49 *result = config_dir.Append("chromium"); | 71 *result = config_dir.Append("chromium"); |
50 #endif | 72 #endif |
51 return true; | 73 return true; |
52 } | 74 } |
53 | 75 |
54 bool GetUserDocumentsDirectory(FilePath* result) { | 76 bool GetUserDocumentsDirectory(FilePath* result) { |
55 *result = GetStandardDirectory("XDG_DOCUMENTS_DIR", "Documents"); | 77 *result = GetXDGUserDirectory("DOCUMENTS", "Documents"); |
56 return true; | 78 return true; |
57 } | 79 } |
58 | 80 |
59 bool GetUserDesktop(FilePath* result) { | 81 bool GetUserDesktop(FilePath* result) { |
60 *result = GetStandardDirectory("XDG_DESKTOP_DIR", "Desktop"); | 82 *result = GetXDGUserDirectory("DESKTOP", "Desktop"); |
61 return true; | 83 return true; |
62 } | 84 } |
63 | 85 |
64 } // namespace chrome | 86 } // namespace chrome |
OLD | NEW |