Home | History | Annotate | Download | only in common
      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/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"
     12 
     13 namespace chrome {
     14 
     15 using base::nix::GetXDGDirectory;
     16 using base::nix::GetXDGUserDirectory;
     17 using base::nix::kDotConfigDir;
     18 using base::nix::kXdgConfigHomeEnvVar;
     19 
     20 namespace {
     21 
     22 const char kDownloadsDir[] = "Downloads";
     23 const char kMusicDir[] = "Music";
     24 const char kPicturesDir[] = "Pictures";
     25 const char kVideosDir[] = "Videos";
     26 
     27 // Generic function for GetUser{Music,Pictures,Video}Directory.
     28 bool GetUserMediaDirectory(const std::string& xdg_name,
     29                            const std::string& fallback_name,
     30                            base::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());
     36 
     37   base::FilePath home = base::GetHomeDir();
     38   if (*result != home) {
     39     base::FilePath desktop;
     40     if (!PathService::Get(base::DIR_USER_DESKTOP, &desktop))
     41       return false;
     42     if (*result != desktop) {
     43       return true;
     44     }
     45   }
     46 
     47   *result = home.Append(fallback_name);
     48   return true;
     49 #endif
     50 }
     51 
     52 }  // namespace
     53 
     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(base::FilePath* result) {
     60   scoped_ptr<base::Environment> env(base::Environment::Create());
     61   base::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;
     70 }
     71 
     72 void GetUserCacheDirectory(const base::FilePath& profile_dir,
     73                            base::FilePath* result) {
     74   // See http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
     75   // for a spec on where cache files go.  Our rule is:
     76   // - if the user-data-dir in the standard place,
     77   //     use same subdirectory of the cache directory.
     78   //     (this maps ~/.config/google-chrome to ~/.cache/google-chrome as well
     79   //      as the same thing for ~/.config/chromium)
     80   // - otherwise, use the profile dir directly.
     81 
     82   // Default value in cases where any of the following fails.
     83   *result = profile_dir;
     84 
     85   scoped_ptr<base::Environment> env(base::Environment::Create());
     86 
     87   base::FilePath cache_dir;
     88   if (!PathService::Get(base::DIR_CACHE, &cache_dir))
     89     return;
     90   base::FilePath config_dir(GetXDGDirectory(env.get(),
     91                                       kXdgConfigHomeEnvVar,
     92                                       kDotConfigDir));
     93 
     94   if (!config_dir.AppendRelativePath(profile_dir, &cache_dir))
     95     return;
     96 
     97   *result = cache_dir;
     98 }
     99 
    100 bool GetChromeFrameUserDataDirectory(base::FilePath* result) {
    101   scoped_ptr<base::Environment> env(base::Environment::Create());
    102   base::FilePath config_dir(GetXDGDirectory(env.get(),
    103                                       kXdgConfigHomeEnvVar,
    104                                       kDotConfigDir));
    105 #if defined(GOOGLE_CHROME_BUILD)
    106   *result = config_dir.Append("google-chrome-frame");
    107 #else
    108   *result = config_dir.Append("chrome-frame");
    109 #endif
    110   return true;
    111 }
    112 
    113 bool GetUserDocumentsDirectory(base::FilePath* result) {
    114   *result = GetXDGUserDirectory("DOCUMENTS", "Documents");
    115   return true;
    116 }
    117 
    118 bool GetUserDownloadsDirectorySafe(base::FilePath* result) {
    119   base::FilePath home = base::GetHomeDir();
    120   *result = home.Append(kDownloadsDir);
    121   return true;
    122 }
    123 
    124 bool GetUserDownloadsDirectory(base::FilePath* result) {
    125   *result = base::nix::GetXDGUserDirectory("DOWNLOAD", kDownloadsDir);
    126   return true;
    127 }
    128 
    129 // We respect the user's preferred pictures location, unless it is
    130 // ~ or their desktop directory, in which case we default to ~/Music.
    131 bool GetUserMusicDirectory(base::FilePath* result) {
    132   return GetUserMediaDirectory("MUSIC", kMusicDir, result);
    133 }
    134 
    135 // We respect the user's preferred pictures location, unless it is
    136 // ~ or their desktop directory, in which case we default to ~/Pictures.
    137 bool GetUserPicturesDirectory(base::FilePath* result) {
    138   return GetUserMediaDirectory("PICTURES", kPicturesDir, result);
    139 }
    140 
    141 // We respect the user's preferred pictures location, unless it is
    142 // ~ or their desktop directory, in which case we default to ~/Videos.
    143 bool GetUserVideosDirectory(base::FilePath* result) {
    144   return GetUserMediaDirectory("VIDEOS", kVideosDir, result);
    145 }
    146 
    147 bool ProcessNeedsProfileDir(const std::string& process_type) {
    148   // For now we have no reason to forbid this on Linux as we don't
    149   // have the roaming profile troubles there. Moreover the Linux breakpad needs
    150   // profile dir access in all process if enabled on Linux.
    151   return true;
    152 }
    153 
    154 }  // namespace chrome
    155