OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "base/linux_util.h" | 5 #include "base/linux_util.h" |
6 | 6 |
7 #include <dirent.h> | 7 #include <dirent.h> |
8 #include <errno.h> | 8 #include <errno.h> |
| 9 #include <glib.h> |
9 #include <stdlib.h> | 10 #include <stdlib.h> |
10 #include <sys/stat.h> | 11 #include <sys/stat.h> |
11 #include <sys/types.h> | |
12 #include <unistd.h> | 12 #include <unistd.h> |
13 | 13 |
14 #include <vector> | 14 #include <vector> |
15 | 15 |
16 #include "base/command_line.h" | 16 #include "base/command_line.h" |
17 #include "base/lock.h" | 17 #include "base/lock.h" |
| 18 #include "base/path_service.h" |
18 #include "base/process_util.h" | 19 #include "base/process_util.h" |
19 #include "base/singleton.h" | 20 #include "base/singleton.h" |
20 #include "base/string_util.h" | 21 #include "base/string_util.h" |
| 22 #include "base/third_party/xdg_user_dirs/xdg_user_dir_lookup.h" |
21 | 23 |
22 namespace { | 24 namespace { |
23 | 25 |
24 class EnvironmentVariableGetterImpl | 26 class EnvironmentVariableGetterImpl |
25 : public base::EnvironmentVariableGetter { | 27 : public base::EnvironmentVariableGetter { |
26 public: | 28 public: |
27 virtual bool Getenv(const char* variable_name, std::string* result) { | 29 virtual bool Getenv(const char* variable_name, std::string* result) { |
28 const char* env_value = ::getenv(variable_name); | 30 const char* env_value = ::getenv(variable_name); |
29 if (env_value) { | 31 if (env_value) { |
30 // Note that the variable may be defined but empty. | 32 // Note that the variable may be defined but empty. |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 | 174 |
173 // We use this static string to hold the Linux distro info. If we | 175 // We use this static string to hold the Linux distro info. If we |
174 // crash, the crash handler code will send this in the crash dump. | 176 // crash, the crash handler code will send this in the crash dump. |
175 std::string linux_distro = | 177 std::string linux_distro = |
176 #if defined(OS_CHROMEOS) | 178 #if defined(OS_CHROMEOS) |
177 "CrOS"; | 179 "CrOS"; |
178 #else // if defined(OS_LINUX) | 180 #else // if defined(OS_LINUX) |
179 "Unknown"; | 181 "Unknown"; |
180 #endif | 182 #endif |
181 | 183 |
| 184 FilePath GetHomeDir(EnvironmentVariableGetter* env) { |
| 185 std::string home_dir; |
| 186 if (env->Getenv("HOME", &home_dir) && !home_dir.empty()) |
| 187 return FilePath(home_dir); |
| 188 |
| 189 home_dir = g_get_home_dir(); |
| 190 if (!home_dir.empty()) |
| 191 return FilePath(home_dir); |
| 192 |
| 193 FilePath rv; |
| 194 if (PathService::Get(base::DIR_TEMP, &rv)) |
| 195 return rv; |
| 196 |
| 197 // Last resort. |
| 198 return FilePath("/tmp"); |
| 199 } |
| 200 |
182 std::string GetLinuxDistro() { | 201 std::string GetLinuxDistro() { |
183 #if defined(OS_CHROMEOS) | 202 #if defined(OS_CHROMEOS) |
184 return linux_distro; | 203 return linux_distro; |
185 #else // if defined(OS_LINUX) | 204 #else // if defined(OS_LINUX) |
186 LinuxDistroHelper* distro_state_singleton = LinuxDistroHelper::Get(); | 205 LinuxDistroHelper* distro_state_singleton = LinuxDistroHelper::Get(); |
187 LinuxDistroState state = distro_state_singleton->State(); | 206 LinuxDistroState state = distro_state_singleton->State(); |
188 if (STATE_DID_NOT_CHECK == state) { | 207 if (STATE_DID_NOT_CHECK == state) { |
189 // We do this check only once per process. If it fails, there's | 208 // We do this check only once per process. If it fails, there's |
190 // little reason to believe it will work if we attempt to run | 209 // little reason to believe it will work if we attempt to run |
191 // lsb_release again. | 210 // lsb_release again. |
(...skipping 16 matching lines...) Expand all Loading... |
208 // If the distro check above is in progress in some other thread, we're | 227 // If the distro check above is in progress in some other thread, we're |
209 // not going to wait for the results. | 228 // not going to wait for the results. |
210 return "Unknown"; | 229 return "Unknown"; |
211 } else { | 230 } else { |
212 // In STATE_CHECK_FINISHED, no more writing to |linux_distro|. | 231 // In STATE_CHECK_FINISHED, no more writing to |linux_distro|. |
213 return linux_distro; | 232 return linux_distro; |
214 } | 233 } |
215 #endif | 234 #endif |
216 } | 235 } |
217 | 236 |
| 237 FilePath GetXDGDirectory(EnvironmentVariableGetter* env, |
| 238 const char* env_name, const char* fallback_dir) { |
| 239 std::string env_value; |
| 240 if (env->Getenv(env_name, &env_value) && !env_value.empty()) |
| 241 return FilePath(env_value); |
| 242 return GetHomeDir(env).Append(fallback_dir); |
| 243 } |
| 244 |
| 245 FilePath GetXDGUserDirectory(EnvironmentVariableGetter* env, |
| 246 const char* dir_name, const char* fallback_dir) { |
| 247 char* xdg_dir = xdg_user_dir_lookup(dir_name); |
| 248 if (xdg_dir) { |
| 249 FilePath rv(xdg_dir); |
| 250 free(xdg_dir); |
| 251 return rv; |
| 252 } |
| 253 return GetHomeDir(env).Append(fallback_dir); |
| 254 } |
| 255 |
218 // static | 256 // static |
219 EnvironmentVariableGetter* EnvironmentVariableGetter::Create() { | 257 EnvironmentVariableGetter* EnvironmentVariableGetter::Create() { |
220 return new EnvironmentVariableGetterImpl(); | 258 return new EnvironmentVariableGetterImpl(); |
221 } | 259 } |
222 | 260 |
223 DesktopEnvironment GetDesktopEnvironment(EnvironmentVariableGetter* env) { | 261 DesktopEnvironment GetDesktopEnvironment(EnvironmentVariableGetter* env) { |
224 std::string desktop_session; | 262 std::string desktop_session; |
225 if (env->Getenv("DESKTOP_SESSION", &desktop_session)) { | 263 if (env->Getenv("DESKTOP_SESSION", &desktop_session)) { |
226 if (desktop_session == "gnome") | 264 if (desktop_session == "gnome") |
227 return DESKTOP_ENVIRONMENT_GNOME; | 265 return DESKTOP_ENVIRONMENT_GNOME; |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 } | 372 } |
335 } | 373 } |
336 | 374 |
337 closedir(fd); | 375 closedir(fd); |
338 } | 376 } |
339 | 377 |
340 return already_found; | 378 return already_found; |
341 } | 379 } |
342 | 380 |
343 } // namespace base | 381 } // namespace base |
OLD | NEW |