| 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/signin/oauth2_login_manager_factory.h" | 11 #include "chrome/browser/chromeos/login/signin/oauth2_login_manager_factory.h" |
| 12 #include "chrome/browser/chromeos/login/users/user_manager.h" | 12 #include "chrome/browser/chromeos/login/users/user_manager.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/profiles/profile_manager.h" | 14 #include "chrome/browser/profiles/profile_manager.h" |
| 15 #include "chrome/browser/profiles/profiles_state.h" | 15 #include "chrome/browser/profiles/profiles_state.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 #include "components/user_manager/user.h" | 19 #include "components/user_manager/user.h" |
| 20 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
| 21 | 21 |
| 22 namespace chromeos { | 22 namespace chromeos { |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 // As defined in /chromeos/dbus/cryptohome_client.cc. |
| 27 static const char kUserIdHashSuffix[] = "-hash"; |
| 28 |
| 26 bool ShouldAddProfileDirPrefix(const std::string& user_id_hash) { | 29 bool ShouldAddProfileDirPrefix(const std::string& user_id_hash) { |
| 27 // Do not add profile dir prefix for legacy profile dir and test | 30 // Do not add profile dir prefix for legacy profile dir and test |
| 28 // user profile. The reason of not adding prefix for test user profile | 31 // user profile. The reason of not adding prefix for test user profile |
| 29 // is to keep the promise that TestingProfile::kTestUserProfileDir and | 32 // is to keep the promise that TestingProfile::kTestUserProfileDir and |
| 30 // chrome::kTestUserProfileDir are always in sync. Otherwise, | 33 // chrome::kTestUserProfileDir are always in sync. Otherwise, |
| 31 // TestingProfile::kTestUserProfileDir needs to be dynamically calculated | 34 // TestingProfile::kTestUserProfileDir needs to be dynamically calculated |
| 32 // based on whether multi profile is enabled or not. | 35 // based on whether multi profile is enabled or not. |
| 33 return user_id_hash != chrome::kLegacyProfileDir && | 36 return user_id_hash != chrome::kLegacyProfileDir && |
| 34 user_id_hash != chrome::kTestUserProfileDir; | 37 user_id_hash != chrome::kTestUserProfileDir; |
| 35 } | 38 } |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 // crbug.com/238998 | 88 // crbug.com/238998 |
| 86 // TODO(nkostylev): Remove this check once these bugs are fixed. | 89 // TODO(nkostylev): Remove this check once these bugs are fixed. |
| 87 DCHECK(!user_id_hash.empty()); | 90 DCHECK(!user_id_hash.empty()); |
| 88 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 91 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 89 base::FilePath profile_path = profile_manager->user_data_dir(); | 92 base::FilePath profile_path = profile_manager->user_data_dir(); |
| 90 | 93 |
| 91 return profile_path.Append(GetUserProfileDir(user_id_hash)); | 94 return profile_path.Append(GetUserProfileDir(user_id_hash)); |
| 92 } | 95 } |
| 93 | 96 |
| 94 // static | 97 // static |
| 95 base::FilePath ProfileHelper::GetProfileDirByLegacyLoginProfileSwitch() { | |
| 96 const std::string login_profile_value = | |
| 97 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
| 98 chromeos::switches::kLoginProfile); | |
| 99 return ProfileHelper::GetUserProfileDir(login_profile_value); | |
| 100 } | |
| 101 | |
| 102 // static | |
| 103 base::FilePath ProfileHelper::GetSigninProfileDir() { | 98 base::FilePath ProfileHelper::GetSigninProfileDir() { |
| 104 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 99 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 105 base::FilePath user_data_dir = profile_manager->user_data_dir(); | 100 base::FilePath user_data_dir = profile_manager->user_data_dir(); |
| 106 return user_data_dir.AppendASCII(chrome::kInitialProfile); | 101 return user_data_dir.AppendASCII(chrome::kInitialProfile); |
| 107 } | 102 } |
| 108 | 103 |
| 109 // static | 104 // static |
| 110 Profile* ProfileHelper::GetSigninProfile() { | 105 Profile* ProfileHelper::GetSigninProfile() { |
| 111 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 106 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 112 return profile_manager->GetProfile(GetSigninProfileDir())-> | 107 return profile_manager->GetProfile(GetSigninProfileDir())-> |
| (...skipping 18 matching lines...) Expand all Loading... |
| 131 return std::string(); | 126 return std::string(); |
| 132 } | 127 } |
| 133 | 128 |
| 134 return profile_dir.substr(prefix.length(), | 129 return profile_dir.substr(prefix.length(), |
| 135 profile_dir.length() - prefix.length()); | 130 profile_dir.length() - prefix.length()); |
| 136 } | 131 } |
| 137 | 132 |
| 138 // static | 133 // static |
| 139 base::FilePath ProfileHelper::GetUserProfileDir( | 134 base::FilePath ProfileHelper::GetUserProfileDir( |
| 140 const std::string& user_id_hash) { | 135 const std::string& user_id_hash) { |
| 141 DCHECK(!user_id_hash.empty()); | 136 CHECK(!user_id_hash.empty()); |
| 142 return ShouldAddProfileDirPrefix(user_id_hash) | 137 return ShouldAddProfileDirPrefix(user_id_hash) |
| 143 ? base::FilePath(chrome::kProfileDirPrefix + user_id_hash) | 138 ? base::FilePath(chrome::kProfileDirPrefix + user_id_hash) |
| 144 : base::FilePath(user_id_hash); | 139 : base::FilePath(user_id_hash); |
| 145 } | 140 } |
| 146 | 141 |
| 147 // static | 142 // static |
| 148 base::FilePath ProfileHelper::GetUserProfileDirByUserId( | |
| 149 const std::string& user_id) { | |
| 150 // TODO(dpolukhin): Remove Chrome OS specific profile path logic from | |
| 151 // ProfileManager and use only this function to construct profile path. | |
| 152 // TODO(nkostylev): Cleanup profile dir related code paths crbug.com/294233 | |
| 153 base::FilePath profile_dir; | |
| 154 const user_manager::User* user = UserManager::Get()->FindUser(user_id); | |
| 155 if (user && !user->username_hash().empty()) | |
| 156 profile_dir = ProfileHelper::GetUserProfileDir(user->username_hash()); | |
| 157 | |
| 158 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
| 159 profile_dir = profile_manager->user_data_dir().Append(profile_dir); | |
| 160 | |
| 161 return profile_dir; | |
| 162 } | |
| 163 | |
| 164 // static | |
| 165 bool ProfileHelper::IsSigninProfile(Profile* profile) { | 143 bool ProfileHelper::IsSigninProfile(Profile* profile) { |
| 166 return profile->GetPath().BaseName().value() == chrome::kInitialProfile; | 144 return profile->GetPath().BaseName().value() == chrome::kInitialProfile; |
| 167 } | 145 } |
| 168 | 146 |
| 169 // static | 147 // static |
| 170 bool ProfileHelper::IsOwnerProfile(Profile* profile) { | 148 bool ProfileHelper::IsOwnerProfile(Profile* profile) { |
| 171 if (!profile) | 149 if (!profile) |
| 172 return false; | 150 return false; |
| 173 user_manager::User* user = ProfileHelper::Get()->GetUserByProfile(profile); | 151 user_manager::User* user = ProfileHelper::Get()->GetUserByProfile(profile); |
| 174 if (!user) | 152 if (!user) |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 295 ++it) { | 273 ++it) { |
| 296 if ((*it)->email() == user_name) | 274 if ((*it)->email() == user_name) |
| 297 return *it; | 275 return *it; |
| 298 } | 276 } |
| 299 | 277 |
| 300 // In case of test setup we should always default to primary user. | 278 // In case of test setup we should always default to primary user. |
| 301 return const_cast<user_manager::User*>( | 279 return const_cast<user_manager::User*>( |
| 302 UserManager::Get()->GetPrimaryUser()); | 280 UserManager::Get()->GetPrimaryUser()); |
| 303 } | 281 } |
| 304 | 282 |
| 305 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 283 DCHECK(!content::BrowserThread::IsThreadInitialized( |
| 284 content::BrowserThread::UI) || |
| 285 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 306 if (ProfileHelper::IsSigninProfile(profile)) | 286 if (ProfileHelper::IsSigninProfile(profile)) |
| 307 return NULL; | 287 return NULL; |
| 308 | 288 |
| 309 UserManager* user_manager = UserManager::Get(); | 289 UserManager* user_manager = UserManager::Get(); |
| 310 | 290 |
| 311 // Special case for non-CrOS tests that do create several profiles | 291 // Special case for non-CrOS tests that do create several profiles |
| 312 // and don't really care about mapping to the real user. | 292 // and don't really care about mapping to the real user. |
| 313 // Without multi-profiles on Chrome OS such tests always got active_user_. | 293 // Without multi-profiles on Chrome OS such tests always got active_user_. |
| 314 // Now these tests will specify special flag to continue working. | 294 // Now these tests will specify special flag to continue working. |
| 315 // In future those tests can get a proper CrOS configuration i.e. register | 295 // In future those tests can get a proper CrOS configuration i.e. register |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 364 login_manager->RemoveObserver(this); | 344 login_manager->RemoveObserver(this); |
| 365 ClearSigninProfile(base::Closure()); | 345 ClearSigninProfile(base::Closure()); |
| 366 } | 346 } |
| 367 } | 347 } |
| 368 | 348 |
| 369 //////////////////////////////////////////////////////////////////////////////// | 349 //////////////////////////////////////////////////////////////////////////////// |
| 370 // ProfileHelper, UserManager::UserSessionStateObserver implementation: | 350 // ProfileHelper, UserManager::UserSessionStateObserver implementation: |
| 371 | 351 |
| 372 void ProfileHelper::ActiveUserHashChanged(const std::string& hash) { | 352 void ProfileHelper::ActiveUserHashChanged(const std::string& hash) { |
| 373 active_user_id_hash_ = hash; | 353 active_user_id_hash_ = hash; |
| 374 base::FilePath profile_path = GetProfilePathByUserIdHash(hash); | |
| 375 VLOG(1) << "Switching to profile path: " << profile_path.value(); | |
| 376 } | 354 } |
| 377 | 355 |
| 378 void ProfileHelper::SetProfileToUserMappingForTesting( | 356 void ProfileHelper::SetProfileToUserMappingForTesting( |
| 379 user_manager::User* user) { | 357 user_manager::User* user) { |
| 380 user_list_for_testing_.push_back(user); | 358 user_list_for_testing_.push_back(user); |
| 381 } | 359 } |
| 382 | 360 |
| 383 // static | 361 // static |
| 384 void ProfileHelper::SetProfileToUserForTestingEnabled(bool enabled) { | 362 void ProfileHelper::SetProfileToUserForTestingEnabled(bool enabled) { |
| 385 enable_profile_to_user_testing = enabled; | 363 enable_profile_to_user_testing = enabled; |
| 386 } | 364 } |
| 387 | 365 |
| 388 // static | 366 // static |
| 389 void ProfileHelper::SetAlwaysReturnPrimaryUserForTesting(bool value) { | 367 void ProfileHelper::SetAlwaysReturnPrimaryUserForTesting(bool value) { |
| 390 always_return_primary_user_for_testing = true; | 368 always_return_primary_user_for_testing = true; |
| 391 ProfileHelper::SetProfileToUserForTestingEnabled(true); | 369 ProfileHelper::SetProfileToUserForTestingEnabled(true); |
| 392 } | 370 } |
| 393 | 371 |
| 394 void ProfileHelper::SetUserToProfileMappingForTesting( | 372 void ProfileHelper::SetUserToProfileMappingForTesting( |
| 395 const user_manager::User* user, | 373 const user_manager::User* user, |
| 396 Profile* profile) { | 374 Profile* profile) { |
| 397 user_to_profile_for_testing_[user] = profile; | 375 user_to_profile_for_testing_[user] = profile; |
| 398 } | 376 } |
| 399 | 377 |
| 378 std::string ProfileHelper::GetUserIdHashByUserIdForTests( |
| 379 const std::string& user_id) const { |
| 380 return user_id + kUserIdHashSuffix; |
| 381 } |
| 382 |
| 400 } // namespace chromeos | 383 } // namespace chromeos |
| OLD | NEW |