Extract SIGPIPE ignoring code to a common place.
[chromium-blink-merge.git] / chrome / common / chrome_paths_linux.cc
blobbabb501d0734f9fc71284b9620463d85c1dce38e
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.
5 #include "chrome/common/chrome_paths_internal.h"
7 #include "base/environment.h"
8 #include "base/file_util.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/nix/xdg_util.h"
11 #include "base/path_service.h"
13 namespace chrome {
15 using base::nix::GetXDGDirectory;
16 using base::nix::GetXDGUserDirectory;
17 using base::nix::kDotConfigDir;
18 using base::nix::kXdgConfigHomeEnvVar;
20 namespace {
22 const char kDownloadsDir[] = "Downloads";
23 const char kMusicDir[] = "Music";
24 const char kPicturesDir[] = "Pictures";
25 const char kVideosDir[] = "Videos";
27 // Generic function for GetUser{Music,Pictures,Video}Directory.
28 bool GetUserMediaDirectory(const std::string& xdg_name,
29 const std::string& fallback_name,
30 FilePath* result) {
31 #if defined(OS_CHROMEOS)
32 // No local media directories on CrOS.
33 return false;
34 #else
35 *result = GetXDGUserDirectory(xdg_name.c_str(), fallback_name.c_str());
37 FilePath home = file_util::GetHomeDir();
38 if (*result != home) {
39 FilePath desktop;
40 if (!PathService::Get(base::DIR_USER_DESKTOP, &desktop))
41 return false;
42 if (*result != desktop) {
43 return true;
47 *result = home.Append(fallback_name);
48 return true;
49 #endif
52 } // namespace
54 // See http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
55 // for a spec on where config files go. The net effect for most
56 // systems is we use ~/.config/chromium/ for Chromium and
57 // ~/.config/google-chrome/ for official builds.
58 // (This also helps us sidestep issues with other apps grabbing ~/.chromium .)
59 bool GetDefaultUserDataDirectory(FilePath* result) {
60 scoped_ptr<base::Environment> env(base::Environment::Create());
61 FilePath config_dir(GetXDGDirectory(env.get(),
62 kXdgConfigHomeEnvVar,
63 kDotConfigDir));
64 #if defined(GOOGLE_CHROME_BUILD)
65 *result = config_dir.Append("google-chrome");
66 #else
67 *result = config_dir.Append("chromium");
68 #endif
69 return true;
72 void GetUserCacheDirectory(const FilePath& profile_dir, FilePath* result) {
73 // See http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
74 // for a spec on where cache files go. Our rule is:
75 // - if the user-data-dir in the standard place,
76 // use same subdirectory of the cache directory.
77 // (this maps ~/.config/google-chrome to ~/.cache/google-chrome as well
78 // as the same thing for ~/.config/chromium)
79 // - otherwise, use the profile dir directly.
81 // Default value in cases where any of the following fails.
82 *result = profile_dir;
84 scoped_ptr<base::Environment> env(base::Environment::Create());
86 FilePath cache_dir;
87 if (!PathService::Get(base::DIR_CACHE, &cache_dir))
88 return;
89 FilePath config_dir(GetXDGDirectory(env.get(),
90 kXdgConfigHomeEnvVar,
91 kDotConfigDir));
93 if (!config_dir.AppendRelativePath(profile_dir, &cache_dir))
94 return;
96 *result = cache_dir;
99 bool GetChromeFrameUserDataDirectory(FilePath* result) {
100 scoped_ptr<base::Environment> env(base::Environment::Create());
101 FilePath config_dir(GetXDGDirectory(env.get(),
102 kXdgConfigHomeEnvVar,
103 kDotConfigDir));
104 #if defined(GOOGLE_CHROME_BUILD)
105 *result = config_dir.Append("google-chrome-frame");
106 #else
107 *result = config_dir.Append("chrome-frame");
108 #endif
109 return true;
112 bool GetUserDocumentsDirectory(FilePath* result) {
113 *result = GetXDGUserDirectory("DOCUMENTS", "Documents");
114 return true;
117 bool GetUserDownloadsDirectorySafe(FilePath* result) {
118 FilePath home = file_util::GetHomeDir();
119 *result = home.Append(kDownloadsDir);
120 return true;
123 bool GetUserDownloadsDirectory(FilePath* result) {
124 *result = base::nix::GetXDGUserDirectory("DOWNLOAD", kDownloadsDir);
125 return true;
128 // We respect the user's preferred pictures location, unless it is
129 // ~ or their desktop directory, in which case we default to ~/Music.
130 bool GetUserMusicDirectory(FilePath* result) {
131 return GetUserMediaDirectory("MUSIC", kMusicDir, result);
134 // We respect the user's preferred pictures location, unless it is
135 // ~ or their desktop directory, in which case we default to ~/Pictures.
136 bool GetUserPicturesDirectory(FilePath* result) {
137 return GetUserMediaDirectory("PICTURES", kPicturesDir, result);
140 // We respect the user's preferred pictures location, unless it is
141 // ~ or their desktop directory, in which case we default to ~/Videos.
142 bool GetUserVideosDirectory(FilePath* result) {
143 return GetUserMediaDirectory("VIDEOS", kVideosDir, result);
146 bool ProcessNeedsProfileDir(const std::string& process_type) {
147 // For now we have no reason to forbid this on Linux as we don't
148 // have the roaming profile troubles there. Moreover the Linux breakpad needs
149 // profile dir access in all process if enabled on Linux.
150 return true;
153 } // namespace chrome