OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 5 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
10 #include "chrome/browser/browsing_data/browsing_data_helper.h" | 10 #include "chrome/browser/browsing_data/browsing_data_helper.h" |
11 #include "chrome/browser/chromeos/login/oauth2_login_manager_factory.h" | 11 #include "chrome/browser/chromeos/login/oauth2_login_manager_factory.h" |
12 #include "chrome/browser/chromeos/login/user.h" | 12 #include "chrome/browser/chromeos/login/user.h" |
13 #include "chrome/browser/chromeos/login/user_manager.h" | 13 #include "chrome/browser/chromeos/login/user_manager.h" |
14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/browser/profiles/profile_manager.h" | 15 #include "chrome/browser/profiles/profile_manager.h" |
16 #include "chrome/common/chrome_constants.h" | 16 #include "chrome/common/chrome_constants.h" |
17 #include "chrome/common/chrome_switches.h" | 17 #include "chrome/common/chrome_switches.h" |
18 #include "chromeos/chromeos_switches.h" | 18 #include "chromeos/chromeos_switches.h" |
19 | 19 |
20 namespace chromeos { | 20 namespace chromeos { |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 base::FilePath GetSigninProfileDir() { | 24 base::FilePath GetSigninProfileDir() { |
25 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 25 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
26 base::FilePath user_data_dir = profile_manager->user_data_dir(); | 26 base::FilePath user_data_dir = profile_manager->user_data_dir(); |
27 return user_data_dir.AppendASCII(chrome::kInitialProfile); | 27 return user_data_dir.AppendASCII(chrome::kInitialProfile); |
28 } | 28 } |
29 | 29 |
| 30 bool ShouldAddProfileDirPrefix(const std::string& user_id_hash) { |
| 31 // Do not add profile dir prefix for legacy profile dir and test |
| 32 // user profile. The reason of not adding prefix for test user profile |
| 33 // is to keep the promise that TestingProfile::kTestUserProfileDir and |
| 34 // chrome::kTestUserProfileDir are always in sync. Otherwise, |
| 35 // TestingProfile::kTestUserProfileDir needs to be dynamically calculated |
| 36 // based on whether multi profile is enabled or not. |
| 37 return user_id_hash != chrome::kLegacyProfileDir && |
| 38 user_id_hash != chrome::kTestUserProfileDir; |
| 39 } |
| 40 |
30 } // anonymous namespace | 41 } // anonymous namespace |
31 | 42 |
32 //////////////////////////////////////////////////////////////////////////////// | 43 //////////////////////////////////////////////////////////////////////////////// |
33 // ProfileHelper, public | 44 // ProfileHelper, public |
34 | 45 |
35 ProfileHelper::ProfileHelper() | 46 ProfileHelper::ProfileHelper() |
36 : signin_profile_clear_requested_(false) { | 47 : signin_profile_clear_requested_(false) { |
37 } | 48 } |
38 | 49 |
39 ProfileHelper::~ProfileHelper() { | 50 ProfileHelper::~ProfileHelper() { |
40 // Checking whether UserManager is initialized covers case | 51 // Checking whether UserManager is initialized covers case |
41 // when ScopedTestUserManager is used. | 52 // when ScopedTestUserManager is used. |
42 if (UserManager::IsInitialized()) | 53 if (UserManager::IsInitialized()) |
43 UserManager::Get()->RemoveSessionStateObserver(this); | 54 UserManager::Get()->RemoveSessionStateObserver(this); |
44 } | 55 } |
45 | 56 |
46 // static | 57 // static |
47 Profile* ProfileHelper::GetProfileByUserIdHash( | 58 Profile* ProfileHelper::GetProfileByUserIdHash( |
48 const std::string& user_id_hash) { | 59 const std::string& user_id_hash) { |
49 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 60 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
50 return profile_manager->GetProfile(GetProfilePathByUserIdHash(user_id_hash)); | 61 return profile_manager->GetProfile(GetProfilePathByUserIdHash(user_id_hash)); |
51 } | 62 } |
52 | 63 |
53 // static | 64 // static |
54 base::FilePath ProfileHelper::GetProfilePathByUserIdHash( | 65 base::FilePath ProfileHelper::GetProfilePathByUserIdHash( |
55 const std::string& user_id_hash) { | 66 const std::string& user_id_hash) { |
56 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | |
57 // Fails for KioskTest.InstallAndLaunchApp test - crbug.com/238985 | |
58 // Will probably fail for Guest session / restart after a crash - | |
59 // crbug.com/238998 | |
60 // TODO(nkostylev): Remove this check once these bugs are fixed. | |
61 if (command_line.HasSwitch(::switches::kMultiProfiles)) | |
62 DCHECK(!user_id_hash.empty()); | |
63 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 67 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
64 base::FilePath profile_path = profile_manager->user_data_dir(); | 68 base::FilePath profile_path = profile_manager->user_data_dir(); |
65 return profile_path.Append( | 69 |
66 base::FilePath(chrome::kProfileDirPrefix + user_id_hash)); | 70 return profile_path.Append(GetUserProfileDir(user_id_hash)); |
67 } | 71 } |
68 | 72 |
69 // static | 73 // static |
70 base::FilePath ProfileHelper::GetProfileDirByLegacyLoginProfileSwitch() { | 74 base::FilePath ProfileHelper::GetProfileDirByLegacyLoginProfileSwitch() { |
71 base::FilePath profile_dir; | 75 const std::string login_profile_value = |
72 std::string login_profile_value = CommandLine::ForCurrentProcess()-> | 76 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
73 GetSwitchValueASCII(chromeos::switches::kLoginProfile); | 77 chromeos::switches::kLoginProfile); |
74 if (login_profile_value == chrome::kLegacyProfileDir || | 78 return ProfileHelper::GetUserProfileDir(login_profile_value); |
75 login_profile_value == chrome::kTestUserProfileDir) { | |
76 profile_dir = base::FilePath(login_profile_value); | |
77 } else { | |
78 profile_dir = ProfileHelper::GetUserProfileDir(login_profile_value); | |
79 } | |
80 return profile_dir; | |
81 } | 79 } |
82 | 80 |
83 // static | 81 // static |
84 Profile* ProfileHelper::GetSigninProfile() { | 82 Profile* ProfileHelper::GetSigninProfile() { |
85 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 83 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
86 return profile_manager->GetProfile(GetSigninProfileDir())-> | 84 return profile_manager->GetProfile(GetSigninProfileDir())-> |
87 GetOffTheRecordProfile(); | 85 GetOffTheRecordProfile(); |
88 } | 86 } |
89 | 87 |
90 // static | 88 // static |
91 std::string ProfileHelper::GetUserIdHashFromProfile(Profile* profile) { | 89 std::string ProfileHelper::GetUserIdHashFromProfile(Profile* profile) { |
92 if (!profile) | 90 if (!profile) |
93 return std::string(); | 91 return std::string(); |
94 | 92 |
| 93 std::string profile_dir = profile->GetPath().BaseName().value(); |
| 94 |
| 95 // Don't strip prefix if the dir is not supposed to be prefixed. |
| 96 if (!ShouldAddProfileDirPrefix(profile_dir)) |
| 97 return profile_dir; |
| 98 |
95 // Check that profile directory starts with the correct prefix. | 99 // Check that profile directory starts with the correct prefix. |
96 std::string profile_dir = profile->GetPath().BaseName().value(); | |
97 std::string prefix(chrome::kProfileDirPrefix); | 100 std::string prefix(chrome::kProfileDirPrefix); |
98 if (profile_dir.find(prefix) != 0) { | 101 if (profile_dir.find(prefix) != 0) { |
99 // This happens when creating a TestingProfile in browser tests. | 102 // This happens when creating a TestingProfile in browser tests. |
100 return std::string(); | 103 return std::string(); |
101 } | 104 } |
102 | 105 |
103 return profile_dir.substr(prefix.length(), | 106 return profile_dir.substr(prefix.length(), |
104 profile_dir.length() - prefix.length()); | 107 profile_dir.length() - prefix.length()); |
105 } | 108 } |
106 | 109 |
107 // static | 110 // static |
108 base::FilePath ProfileHelper::GetUserProfileDir( | 111 base::FilePath ProfileHelper::GetUserProfileDir( |
109 const std::string& user_id_hash) { | 112 const std::string& user_id_hash) { |
110 DCHECK(!user_id_hash.empty()); | 113 DCHECK(!user_id_hash.empty()); |
111 return base::FilePath(chrome::kProfileDirPrefix + user_id_hash); | 114 return ShouldAddProfileDirPrefix(user_id_hash) |
| 115 ? base::FilePath(chrome::kProfileDirPrefix + user_id_hash) |
| 116 : base::FilePath(user_id_hash); |
112 } | 117 } |
113 | 118 |
114 // static | 119 // static |
115 bool ProfileHelper::IsSigninProfile(Profile* profile) { | 120 bool ProfileHelper::IsSigninProfile(Profile* profile) { |
116 return profile->GetPath().BaseName().value() == chrome::kInitialProfile; | 121 return profile->GetPath().BaseName().value() == chrome::kInitialProfile; |
117 } | 122 } |
118 | 123 |
119 // static | 124 // static |
120 bool ProfileHelper::IsOwnerProfile(Profile* profile) { | 125 bool ProfileHelper::IsOwnerProfile(Profile* profile) { |
121 if (!profile) | 126 if (!profile) |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 //////////////////////////////////////////////////////////////////////////////// | 211 //////////////////////////////////////////////////////////////////////////////// |
207 // ProfileHelper, UserManager::UserSessionStateObserver implementation: | 212 // ProfileHelper, UserManager::UserSessionStateObserver implementation: |
208 | 213 |
209 void ProfileHelper::ActiveUserHashChanged(const std::string& hash) { | 214 void ProfileHelper::ActiveUserHashChanged(const std::string& hash) { |
210 active_user_id_hash_ = hash; | 215 active_user_id_hash_ = hash; |
211 base::FilePath profile_path = GetProfilePathByUserIdHash(hash); | 216 base::FilePath profile_path = GetProfilePathByUserIdHash(hash); |
212 VLOG(1) << "Switching to profile path: " << profile_path.value(); | 217 VLOG(1) << "Switching to profile path: " << profile_path.value(); |
213 } | 218 } |
214 | 219 |
215 } // namespace chromeos | 220 } // namespace chromeos |
OLD | NEW |