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 "base/linux_util.h" |
8 #include <stdlib.h> | 8 #include "base/scoped_ptr.h" |
9 | |
10 #include "base/file_path.h" | |
11 #include "base/path_service.h" | |
12 #include "chrome/third_party/xdg_user_dirs/xdg_user_dir_lookup.h" | |
13 | |
14 namespace { | |
15 | |
16 FilePath GetHomeDir() { | |
17 const char *home_dir = getenv("HOME"); | |
18 | |
19 if (home_dir && home_dir[0]) | |
20 return FilePath(home_dir); | |
21 | |
22 home_dir = g_get_home_dir(); | |
23 if (home_dir && home_dir[0]) | |
24 return FilePath(home_dir); | |
25 | |
26 FilePath rv; | |
27 if (PathService::Get(base::DIR_TEMP, &rv)) | |
28 return rv; | |
29 | |
30 /* last resort */ | |
31 return FilePath("/tmp/"); | |
32 } | |
33 | |
34 // Wrapper around xdg_user_dir_lookup() from | |
35 // src/chrome/third_party/xdg-user-dirs | |
36 FilePath GetXDGUserDirectory(const char* env_name, const char* fallback_dir) { | |
37 char* xdg_dir = xdg_user_dir_lookup(env_name); | |
38 if (xdg_dir) { | |
39 FilePath rv(xdg_dir); | |
40 free(xdg_dir); | |
41 return rv; | |
42 } | |
43 return GetHomeDir().Append(fallback_dir); | |
44 } | |
45 | |
46 // |env_name| is the name of an environment variable that we want to use to get | |
47 // a directory path. |fallback_dir| is the directory relative to $HOME that we | |
48 // use if |env_name| cannot be found or is empty. |fallback_dir| may be NULL. | |
49 FilePath GetXDGDirectory(const char* env_name, const char* fallback_dir) { | |
50 const char* env_value = getenv(env_name); | |
51 if (env_value && env_value[0]) | |
52 return FilePath(env_value); | |
53 return GetHomeDir().Append(fallback_dir); | |
54 } | |
55 | |
56 } // namespace | |
57 | 9 |
58 namespace chrome { | 10 namespace chrome { |
59 | 11 |
60 // See http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html | 12 // See http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html |
61 // for a spec on where config files go. The net effect for most | 13 // for a spec on where config files go. The net effect for most |
62 // systems is we use ~/.config/chromium/ for Chromium and | 14 // systems is we use ~/.config/chromium/ for Chromium and |
63 // ~/.config/google-chrome/ for official builds. | 15 // ~/.config/google-chrome/ for official builds. |
64 // (This also helps us sidestep issues with other apps grabbing ~/.chromium .) | 16 // (This also helps us sidestep issues with other apps grabbing ~/.chromium .) |
65 bool GetDefaultUserDataDirectory(FilePath* result) { | 17 bool GetDefaultUserDataDirectory(FilePath* result) { |
66 FilePath config_dir(GetXDGDirectory("XDG_CONFIG_HOME", ".config")); | 18 scoped_ptr<base::EnvironmentVariableGetter> env( |
| 19 base::EnvironmentVariableGetter::Create()); |
| 20 FilePath config_dir( |
| 21 base::GetXDGDirectory(env.get(), "XDG_CONFIG_HOME", ".config")); |
67 #if defined(GOOGLE_CHROME_BUILD) | 22 #if defined(GOOGLE_CHROME_BUILD) |
68 *result = config_dir.Append("google-chrome"); | 23 *result = config_dir.Append("google-chrome"); |
69 #else | 24 #else |
70 *result = config_dir.Append("chromium"); | 25 *result = config_dir.Append("chromium"); |
71 #endif | 26 #endif |
72 return true; | 27 return true; |
73 } | 28 } |
74 | 29 |
75 bool GetChromeFrameUserDataDirectory(FilePath* result) { | 30 bool GetChromeFrameUserDataDirectory(FilePath* result) { |
76 FilePath config_dir(GetXDGDirectory("XDG_CONFIG_HOME", ".config")); | 31 scoped_ptr<base::EnvironmentVariableGetter> env( |
| 32 base::EnvironmentVariableGetter::Create()); |
| 33 FilePath config_dir( |
| 34 base::GetXDGDirectory(env.get(), "XDG_CONFIG_HOME", ".config")); |
77 #if defined(GOOGLE_CHROME_BUILD) | 35 #if defined(GOOGLE_CHROME_BUILD) |
78 *result = config_dir.Append("google-chrome-frame"); | 36 *result = config_dir.Append("google-chrome-frame"); |
79 #else | 37 #else |
80 *result = config_dir.Append("chrome-frame"); | 38 *result = config_dir.Append("chrome-frame"); |
81 #endif | 39 #endif |
82 return true; | 40 return true; |
83 } | 41 } |
84 | 42 |
85 // See http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html | |
86 // for a spec on where cache files go. The net effect for most | |
87 // systems is we use ~/.cache/chromium/ for Chromium and | |
88 // ~/.cache/google-chrome/ for official builds. | |
89 bool GetUserCacheDirectory(FilePath* result) { | |
90 FilePath cache_dir(GetXDGDirectory("XDG_CACHE_HOME", ".cache")); | |
91 #if defined(GOOGLE_CHROME_BUILD) | |
92 *result = cache_dir.Append("google-chrome"); | |
93 #else | |
94 *result = cache_dir.Append("chromium"); | |
95 #endif | |
96 return true; | |
97 } | |
98 | |
99 bool GetUserDocumentsDirectory(FilePath* result) { | 43 bool GetUserDocumentsDirectory(FilePath* result) { |
100 *result = GetXDGUserDirectory("DOCUMENTS", "Documents"); | 44 scoped_ptr<base::EnvironmentVariableGetter> env( |
| 45 base::EnvironmentVariableGetter::Create()); |
| 46 *result = base::GetXDGUserDirectory(env.get(), "DOCUMENTS", "Documents"); |
101 return true; | 47 return true; |
102 } | 48 } |
103 | 49 |
104 // We respect the user's preferred download location, unless it is | 50 // We respect the user's preferred download location, unless it is |
105 // ~ or their desktop directory, in which case we default to ~/Downloads. | 51 // ~ or their desktop directory, in which case we default to ~/Downloads. |
106 bool GetUserDownloadsDirectory(FilePath* result) { | 52 bool GetUserDownloadsDirectory(FilePath* result) { |
107 *result = GetXDGUserDirectory("DOWNLOAD", "Downloads"); | 53 scoped_ptr<base::EnvironmentVariableGetter> env( |
| 54 base::EnvironmentVariableGetter::Create()); |
| 55 *result = base::GetXDGUserDirectory(env.get(), "DOWNLOAD", "Downloads"); |
108 | 56 |
109 FilePath home = GetHomeDir(); | 57 FilePath home = base::GetHomeDir(env.get()); |
110 if (*result == home) { | 58 if (*result == home) { |
111 *result = home.Append("Downloads"); | 59 *result = home.Append("Downloads"); |
112 return true; | 60 return true; |
113 } | 61 } |
114 | 62 |
115 FilePath desktop; | 63 FilePath desktop; |
116 GetUserDesktop(&desktop); | 64 GetUserDesktop(&desktop); |
117 if (*result == desktop) { | 65 if (*result == desktop) { |
118 *result = home.Append("Downloads"); | 66 *result = home.Append("Downloads"); |
119 } | 67 } |
120 | 68 |
121 return true; | 69 return true; |
122 } | 70 } |
123 | 71 |
124 bool GetUserDesktop(FilePath* result) { | 72 bool GetUserDesktop(FilePath* result) { |
125 *result = GetXDGUserDirectory("DESKTOP", "Desktop"); | 73 scoped_ptr<base::EnvironmentVariableGetter> env( |
| 74 base::EnvironmentVariableGetter::Create()); |
| 75 *result = base::GetXDGUserDirectory(env.get(), "DESKTOP", "Desktop"); |
126 return true; | 76 return true; |
127 } | 77 } |
128 | 78 |
129 } // namespace chrome | 79 } // namespace chrome |
OLD | NEW |