chrome_paths_linux.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "chrome/common/chrome_paths_internal.h"
  5. #include <memory>
  6. #include "base/base_paths.h"
  7. #include "base/environment.h"
  8. #include "base/files/file_util.h"
  9. #include "base/nix/xdg_util.h"
  10. #include "base/path_service.h"
  11. #include "chrome/common/chrome_paths.h"
  12. namespace chrome {
  13. using base::nix::GetXDGDirectory;
  14. using base::nix::GetXDGUserDirectory;
  15. using base::nix::kDotConfigDir;
  16. using base::nix::kXdgConfigHomeEnvVar;
  17. namespace {
  18. const char kDownloadsDir[] = "Downloads";
  19. const char kMusicDir[] = "Music";
  20. const char kPicturesDir[] = "Pictures";
  21. const char kVideosDir[] = "Videos";
  22. // Generic function for GetUser{Music,Pictures,Video}Directory.
  23. bool GetUserMediaDirectory(const std::string& xdg_name,
  24. const std::string& fallback_name,
  25. base::FilePath* result) {
  26. #if defined(OS_CHROMEOS)
  27. // No local media directories on CrOS.
  28. return false;
  29. #else
  30. *result = GetXDGUserDirectory(xdg_name.c_str(), fallback_name.c_str());
  31. base::FilePath home;
  32. PathService::Get(base::DIR_HOME, &home);
  33. if (*result != home) {
  34. base::FilePath desktop;
  35. if (!PathService::Get(base::DIR_USER_DESKTOP, &desktop))
  36. return false;
  37. if (*result != desktop) {
  38. return true;
  39. }
  40. }
  41. *result = home.Append(fallback_name);
  42. return true;
  43. #endif
  44. }
  45. } // namespace
  46. // See http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
  47. // for a spec on where config files go. The net effect for most
  48. // systems is we use ~/.config/chromium/ for Chromium and
  49. // ~/.config/google-chrome/ for official builds.
  50. // (This also helps us sidestep issues with other apps grabbing ~/.chromium .)
  51. bool GetDefaultUserDataDirectory(base::FilePath* result) {
  52. std::unique_ptr<base::Environment> env(base::Environment::Create());
  53. base::FilePath config_dir(
  54. GetXDGDirectory(env.get(), kXdgConfigHomeEnvVar, kDotConfigDir));
  55. #if defined(GOOGLE_CHROME_BUILD)
  56. *result = config_dir.Append("google-chrome");
  57. #else
  58. *result = config_dir.Append("chromium");
  59. #endif
  60. return true;
  61. }
  62. void GetUserCacheDirectory(const base::FilePath& profile_dir,
  63. base::FilePath* result) {
  64. // See http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
  65. // for a spec on where cache files go. Our rule is:
  66. // - if the user-data-dir in the standard place,
  67. // use same subdirectory of the cache directory.
  68. // (this maps ~/.config/google-chrome to ~/.cache/google-chrome as well
  69. // as the same thing for ~/.config/chromium)
  70. // - otherwise, use the profile dir directly.
  71. // Default value in cases where any of the following fails.
  72. *result = profile_dir;
  73. std::unique_ptr<base::Environment> env(base::Environment::Create());
  74. base::FilePath cache_dir;
  75. if (!PathService::Get(base::DIR_CACHE, &cache_dir))
  76. return;
  77. base::FilePath config_dir(
  78. GetXDGDirectory(env.get(), kXdgConfigHomeEnvVar, kDotConfigDir));
  79. if (!config_dir.AppendRelativePath(profile_dir, &cache_dir))
  80. return;
  81. *result = cache_dir;
  82. }
  83. bool GetUserDocumentsDirectory(base::FilePath* result) {
  84. *result = GetXDGUserDirectory("DOCUMENTS", "Documents");
  85. return true;
  86. }
  87. bool GetUserDownloadsDirectorySafe(base::FilePath* result) {
  88. base::FilePath home;
  89. PathService::Get(base::DIR_HOME, &home);
  90. *result = home.Append(kDownloadsDir);
  91. return true;
  92. }
  93. bool GetUserDownloadsDirectory(base::FilePath* result) {
  94. *result = GetXDGUserDirectory("DOWNLOAD", kDownloadsDir);
  95. return true;
  96. }
  97. // We respect the user's preferred pictures location, unless it is
  98. // ~ or their desktop directory, in which case we default to ~/Music.
  99. bool GetUserMusicDirectory(base::FilePath* result) {
  100. return GetUserMediaDirectory("MUSIC", kMusicDir, result);
  101. }
  102. // We respect the user's preferred pictures location, unless it is
  103. // ~ or their desktop directory, in which case we default to ~/Pictures.
  104. bool GetUserPicturesDirectory(base::FilePath* result) {
  105. return GetUserMediaDirectory("PICTURES", kPicturesDir, result);
  106. }
  107. // We respect the user's preferred pictures location, unless it is
  108. // ~ or their desktop directory, in which case we default to ~/Videos.
  109. bool GetUserVideosDirectory(base::FilePath* result) {
  110. return GetUserMediaDirectory("VIDEOS", kVideosDir, result);
  111. }
  112. bool ProcessNeedsProfileDir(const std::string& process_type) {
  113. // For now we have no reason to forbid this on Linux as we don't
  114. // have the roaming profile troubles there. Moreover the Linux breakpad needs
  115. // profile dir access in all process if enabled on Linux.
  116. return true;
  117. }
  118. } // namespace chrome