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