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