Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / file_manager / path_util.cc
1 // Copyright 2013 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/browser/chromeos/file_manager/path_util.h"
6
7 #include "base/files/file_path.h"
8 #include "base/logging.h"
9 #include "base/path_service.h"
10 #include "base/sys_info.h"
11 #include "chrome/browser/chromeos/drive/file_system_util.h"
12 #include "chrome/browser/chromeos/login/user.h"
13 #include "chrome/browser/chromeos/login/user_manager.h"
14 #include "chrome/browser/download/download_prefs.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "net/base/escape.h"
17
18 namespace file_manager {
19 namespace util {
20
21 namespace {
22
23 const char kDownloadsFolderName[] = "Downloads";
24 const base::FilePath::CharType kOldDownloadsFolderPath[] =
25     FILE_PATH_LITERAL("/home/chronos/user/Downloads");
26 const base::FilePath::CharType kOldDriveFolderPath[] =
27     FILE_PATH_LITERAL("/special/drive");
28 const base::FilePath::CharType kNoHashDriveFolderPath[] =
29     FILE_PATH_LITERAL("/special/drive-");
30
31 }  // namespace
32
33 base::FilePath GetDownloadsFolderForProfile(Profile* profile) {
34   if (!base::SysInfo::IsRunningOnChromeOS() &&
35       !chromeos::UserManager::IsMultipleProfilesAllowed()) {
36     // On the developer run on Linux desktop build, if multiple profiles are
37     // not enabled, use $HOME/Downloads for ease for accessing local files for
38     // debugging.
39     base::FilePath path;
40     CHECK(PathService::Get(base::DIR_HOME, &path));
41     return path.AppendASCII(kDownloadsFolderName);
42   }
43
44   return profile->GetPath().AppendASCII(kDownloadsFolderName);
45 }
46
47 bool MigratePathFromOldFormat(Profile* profile,
48                               const base::FilePath& old_path,
49                               base::FilePath* new_path) {
50   // M34:
51   // /home/chronos/user/Downloads/xxx => /home/chronos/u-hash/Downloads/xxx
52   // /special/drive => /special/drive-xxx
53   //
54   // Old path format comes either from stored old settings or from the initial
55   // default value set in DownloadPrefs::RegisterProfilePrefs in profile-unaware
56   // code location. In the former case it is "/home/chronos/user/Downloads",
57   // and in the latter case it is DownloadPrefs::GetDefaultDownloadDirectory().
58   // Those two paths coincides as long as $HOME=/home/chronos/user, but the
59   // environment variable is phasing out (crbug.com/333031) so we care both.
60
61   const base::FilePath downloads = GetDownloadsFolderForProfile(profile);
62   const base::FilePath drive = drive::util::GetDriveMountPointPath(profile);
63   const base::FilePath bases[][2] = {
64     {base::FilePath(kOldDownloadsFolderPath),      downloads},
65     {DownloadPrefs::GetDefaultDownloadDirectory(), downloads},
66     {base::FilePath(kOldDriveFolderPath),          drive},
67     // TODO(kinaba): http://crbug.com/341284 Remove after M34 branching.
68     // For a short period we incorrectly set "/special/drive-" as the Drive path
69     // that needs to be fixed.
70     {base::FilePath(kNoHashDriveFolderPath),       drive},
71   };
72
73   for (size_t i = 0; i < arraysize(bases); ++i) {
74     const base::FilePath& old_base = bases[i][0];
75     const base::FilePath& new_base = bases[i][1];
76     base::FilePath relative;
77     if (old_path == old_base ||
78         old_base.AppendRelativePath(old_path, &relative)) {
79       *new_path = new_base.Append(relative);
80       return old_path != *new_path;
81     }
82   }
83
84   return false;
85 }
86
87 std::string GetDownloadsMountPointName(Profile* profile) {
88   // To distinguish profiles in multi-profile session, we append user name hash
89   // to "Downloads". Note that some profiles (like login or test profiles)
90   // are not associated with an user account. In that case, no suffix is added
91   // because such a profile never belongs to a multi-profile session.
92   chromeos::User* const user =
93       chromeos::UserManager::IsInitialized() ?
94           chromeos::UserManager::Get()->GetUserByProfile(
95               profile->GetOriginalProfile()) : NULL;
96   const std::string id = user ? "-" + user->username_hash() : "";
97   return net::EscapePath(kDownloadsFolderName + id);
98 }
99
100 }  // namespace util
101 }  // namespace file_manager