1 // Copyright (c) 2011 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/environment.h"
8 #include "base/file_util.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/path_service.h"
11 #include "base/nix/xdg_util.h"
12
13 namespace {
14
15 const char kDotConfigDir[] = ".config";
16 const char kDownloadsDir[] = "Downloads";
17 const char kXdgConfigHomeEnvVar[] = "XDG_CONFIG_HOME";
18
19 } // namespace
20
21 namespace chrome {
22
23 // See http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
24 // for a spec on where config files go. The net effect for most
25 // systems is we use ~/.config/chromium/ for Chromium and
26 // ~/.config/google-chrome/ for official builds.
27 // (This also helps us sidestep issues with other apps grabbing ~/.chromium .)
GetDefaultUserDataDirectory(FilePath * result)28 bool GetDefaultUserDataDirectory(FilePath* result) {
29 scoped_ptr<base::Environment> env(base::Environment::Create());
30 FilePath config_dir(base::nix::GetXDGDirectory(env.get(),
31 kXdgConfigHomeEnvVar,
32 kDotConfigDir));
33 #if defined(GOOGLE_CHROME_BUILD)
34 *result = config_dir.Append("google-chrome");
35 #else
36 *result = config_dir.Append("chromium");
37 #endif
38 return true;
39 }
40
GetUserCacheDirectory(const FilePath & profile_dir,FilePath * result)41 void GetUserCacheDirectory(const FilePath& profile_dir, FilePath* result) {
42 // See http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
43 // for a spec on where cache files go. Our rule is:
44 // - if the user-data-dir in the standard place,
45 // use same subdirectory of the cache directory.
46 // (this maps ~/.config/google-chrome to ~/.cache/google-chrome as well
47 // as the same thing for ~/.config/chromium)
48 // - otherwise, use the profile dir directly.
49
50 // Default value in cases where any of the following fails.
51 *result = profile_dir;
52
53 scoped_ptr<base::Environment> env(base::Environment::Create());
54
55 FilePath cache_dir;
56 if (!PathService::Get(base::DIR_CACHE, &cache_dir))
57 return;
58 FilePath config_dir(base::nix::GetXDGDirectory(env.get(),
59 kXdgConfigHomeEnvVar,
60 kDotConfigDir));
61
62 if (!config_dir.AppendRelativePath(profile_dir, &cache_dir))
63 return;
64
65 *result = cache_dir;
66 }
67
GetChromeFrameUserDataDirectory(FilePath * result)68 bool GetChromeFrameUserDataDirectory(FilePath* result) {
69 scoped_ptr<base::Environment> env(base::Environment::Create());
70 FilePath config_dir(base::nix::GetXDGDirectory(env.get(),
71 kXdgConfigHomeEnvVar,
72 kDotConfigDir));
73 #if defined(GOOGLE_CHROME_BUILD)
74 *result = config_dir.Append("google-chrome-frame");
75 #else
76 *result = config_dir.Append("chrome-frame");
77 #endif
78 return true;
79 }
80
GetUserDocumentsDirectory(FilePath * result)81 bool GetUserDocumentsDirectory(FilePath* result) {
82 scoped_ptr<base::Environment> env(base::Environment::Create());
83 *result = base::nix::GetXDGUserDirectory(env.get(), "DOCUMENTS", "Documents");
84 return true;
85 }
86
87 // We respect the user's preferred download location, unless it is
88 // ~ or their desktop directory, in which case we default to ~/Downloads.
GetUserDownloadsDirectory(FilePath * result)89 bool GetUserDownloadsDirectory(FilePath* result) {
90 scoped_ptr<base::Environment> env(base::Environment::Create());
91 *result = base::nix::GetXDGUserDirectory(env.get(), "DOWNLOAD",
92 kDownloadsDir);
93
94 FilePath home = file_util::GetHomeDir();
95 if (*result == home) {
96 *result = home.Append(kDownloadsDir);
97 return true;
98 }
99
100 FilePath desktop;
101 GetUserDesktop(&desktop);
102 if (*result == desktop) {
103 *result = home.Append(kDownloadsDir);
104 }
105
106 return true;
107 }
108
GetUserDesktop(FilePath * result)109 bool GetUserDesktop(FilePath* result) {
110 scoped_ptr<base::Environment> env(base::Environment::Create());
111 *result = base::nix::GetXDGUserDirectory(env.get(), "DESKTOP", "Desktop");
112 return true;
113 }
114
115 } // namespace chrome
116