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.
5 #include "chrome/browser/chromeos/file_manager/path_util.h"
7 #include "base/files/file_path.h"
8 #include "base/logging.h"
9 #include "base/sys_info.h"
10 #include "chrome/browser/chromeos/profiles/profile_helper.h"
11 #include "chrome/browser/download/download_prefs.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "components/drive/file_system_core_util.h"
14 #include "components/user_manager/user.h"
15 #include "components/user_manager/user_manager.h"
16 #include "net/base/escape.h"
18 namespace file_manager
{
23 const char kDownloadsFolderName
[] = "Downloads";
27 base::FilePath
GetDownloadsFolderForProfile(Profile
* profile
) {
28 // On non-ChromeOS system (test+development), the primary profile uses
29 // $HOME/Downloads for ease for accessing local files for debugging.
30 if (!base::SysInfo::IsRunningOnChromeOS() &&
31 user_manager::UserManager::IsInitialized()) {
32 const user_manager::User
* const user
=
33 chromeos::ProfileHelper::Get()->GetUserByProfile(
34 profile
->GetOriginalProfile());
35 const user_manager::User
* const primary_user
=
36 user_manager::UserManager::Get()->GetPrimaryUser();
37 if (user
== primary_user
)
38 return DownloadPrefs::GetDefaultDownloadDirectory();
40 return profile
->GetPath().AppendASCII(kDownloadsFolderName
);
43 bool MigratePathFromOldFormat(Profile
* profile
,
44 const base::FilePath
& old_path
,
45 base::FilePath
* new_path
) {
46 const base::FilePath old_base
= DownloadPrefs::GetDefaultDownloadDirectory();
47 const base::FilePath new_base
= GetDownloadsFolderForProfile(profile
);
49 base::FilePath relative
;
50 if (old_path
== old_base
||
51 old_base
.AppendRelativePath(old_path
, &relative
)) {
52 *new_path
= new_base
.Append(relative
);
53 return old_path
!= *new_path
;
59 std::string
GetDownloadsMountPointName(Profile
* profile
) {
60 // To distinguish profiles in multi-profile session, we append user name hash
61 // to "Downloads". Note that some profiles (like login or test profiles)
62 // are not associated with an user account. In that case, no suffix is added
63 // because such a profile never belongs to a multi-profile session.
64 const user_manager::User
* const user
=
65 user_manager::UserManager::IsInitialized()
66 ? chromeos::ProfileHelper::Get()->GetUserByProfile(
67 profile
->GetOriginalProfile())
69 const std::string id
= user
? "-" + user
->username_hash() : "";
70 return net::EscapeQueryParamValue(kDownloadsFolderName
+ id
, false);
74 } // namespace file_manager