| 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/barrier_closure.h" | 7 #include "base/barrier_closure.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 public: | 57 public: |
| 58 explicit UsernameHashMatcher(const std::string& h) : username_hash(h) {} | 58 explicit UsernameHashMatcher(const std::string& h) : username_hash(h) {} |
| 59 bool operator()(const user_manager::User* user) const { | 59 bool operator()(const user_manager::User* user) const { |
| 60 return user->username_hash() == username_hash; | 60 return user->username_hash() == username_hash; |
| 61 } | 61 } |
| 62 | 62 |
| 63 private: | 63 private: |
| 64 const std::string& username_hash; | 64 const std::string& username_hash; |
| 65 }; | 65 }; |
| 66 | 66 |
| 67 // Internal helper to get an already-loaded user profile by user id hash. Return |
| 68 // nullptr if the user profile is not yet loaded. |
| 69 Profile* GetProfileByUserIdHash(const std::string& user_id_hash) { |
| 70 return g_browser_process->profile_manager()->GetProfileByPath( |
| 71 ProfileHelper::GetProfilePathByUserIdHash(user_id_hash)); |
| 72 } |
| 73 |
| 67 } // anonymous namespace | 74 } // anonymous namespace |
| 68 | 75 |
| 69 // static | 76 // static |
| 70 bool ProfileHelper::enable_profile_to_user_testing = false; | 77 bool ProfileHelper::enable_profile_to_user_testing = false; |
| 71 bool ProfileHelper::always_return_primary_user_for_testing = false; | 78 bool ProfileHelper::always_return_primary_user_for_testing = false; |
| 72 | 79 |
| 73 //////////////////////////////////////////////////////////////////////////////// | 80 //////////////////////////////////////////////////////////////////////////////// |
| 74 // ProfileHelper, public | 81 // ProfileHelper, public |
| 75 | 82 |
| 76 ProfileHelper::ProfileHelper() | 83 ProfileHelper::ProfileHelper() |
| 77 : browsing_data_remover_(nullptr), weak_factory_(this) { | 84 : browsing_data_remover_(nullptr), weak_factory_(this) { |
| 78 } | 85 } |
| 79 | 86 |
| 80 ProfileHelper::~ProfileHelper() { | 87 ProfileHelper::~ProfileHelper() { |
| 81 // Checking whether UserManager is initialized covers case | 88 // Checking whether UserManager is initialized covers case |
| 82 // when ScopedTestUserManager is used. | 89 // when ScopedTestUserManager is used. |
| 83 if (user_manager::UserManager::IsInitialized()) | 90 if (user_manager::UserManager::IsInitialized()) |
| 84 user_manager::UserManager::Get()->RemoveSessionStateObserver(this); | 91 user_manager::UserManager::Get()->RemoveSessionStateObserver(this); |
| 85 | 92 |
| 86 if (browsing_data_remover_) | 93 if (browsing_data_remover_) |
| 87 browsing_data_remover_->RemoveObserver(this); | 94 browsing_data_remover_->RemoveObserver(this); |
| 88 } | 95 } |
| 89 | 96 |
| 90 // static | 97 // static |
| 91 ProfileHelper* ProfileHelper::Get() { | 98 ProfileHelper* ProfileHelper::Get() { |
| 92 return g_browser_process->platform_part()->profile_helper(); | 99 return g_browser_process->platform_part()->profile_helper(); |
| 93 } | 100 } |
| 94 | 101 |
| 95 // static | 102 // static |
| 96 Profile* ProfileHelper::GetProfileByUserIdHash( | 103 Profile* ProfileHelper::GetProfileByUserIdHashForTest( |
| 97 const std::string& user_id_hash) { | 104 const std::string& user_id_hash) { |
| 98 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 105 return g_browser_process->profile_manager()->GetProfile( |
| 99 return profile_manager->GetProfile(GetProfilePathByUserIdHash(user_id_hash)); | 106 ProfileHelper::GetProfilePathByUserIdHash(user_id_hash)); |
| 100 } | 107 } |
| 101 | 108 |
| 102 // static | 109 // static |
| 103 base::FilePath ProfileHelper::GetProfilePathByUserIdHash( | 110 base::FilePath ProfileHelper::GetProfilePathByUserIdHash( |
| 104 const std::string& user_id_hash) { | 111 const std::string& user_id_hash) { |
| 105 // Fails for KioskTest.InstallAndLaunchApp test - crbug.com/238985 | 112 // Fails for KioskTest.InstallAndLaunchApp test - crbug.com/238985 |
| 106 // Will probably fail for Guest session / restart after a crash - | 113 // Will probably fail for Guest session / restart after a crash - |
| 107 // crbug.com/238998 | 114 // crbug.com/238998 |
| 108 // TODO(nkostylev): Remove this check once these bugs are fixed. | 115 // TODO(nkostylev): Remove this check once these bugs are fixed. |
| 109 DCHECK(!user_id_hash.empty()); | 116 DCHECK(!user_id_hash.empty()); |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 285 Profile* ProfileHelper::GetProfileByUser(const user_manager::User* user) { | 292 Profile* ProfileHelper::GetProfileByUser(const user_manager::User* user) { |
| 286 // This map is non-empty only in tests. | 293 // This map is non-empty only in tests. |
| 287 if (!user_to_profile_for_testing_.empty()) { | 294 if (!user_to_profile_for_testing_.empty()) { |
| 288 std::map<const user_manager::User*, Profile*>::const_iterator it = | 295 std::map<const user_manager::User*, Profile*>::const_iterator it = |
| 289 user_to_profile_for_testing_.find(user); | 296 user_to_profile_for_testing_.find(user); |
| 290 return it == user_to_profile_for_testing_.end() ? NULL : it->second; | 297 return it == user_to_profile_for_testing_.end() ? NULL : it->second; |
| 291 } | 298 } |
| 292 | 299 |
| 293 if (!user->is_profile_created()) | 300 if (!user->is_profile_created()) |
| 294 return NULL; | 301 return NULL; |
| 295 Profile* profile = | 302 Profile* profile = GetProfileByUserIdHash(user->username_hash()); |
| 296 ProfileHelper::GetProfileByUserIdHash(user->username_hash()); | |
| 297 | 303 |
| 298 // GetActiveUserProfile() or GetProfileByUserIdHash() returns a new instance | 304 // GetActiveUserProfile() or GetProfileByUserIdHash() returns a new instance |
| 299 // of ProfileImpl(), but actually its OffTheRecordProfile() should be used. | 305 // of ProfileImpl(), but actually its OffTheRecordProfile() should be used. |
| 300 if (user_manager::UserManager::Get()->IsLoggedInAsGuest()) | 306 if (user_manager::UserManager::Get()->IsLoggedInAsGuest()) |
| 301 profile = profile->GetOffTheRecordProfile(); | 307 profile = profile->GetOffTheRecordProfile(); |
| 302 | 308 |
| 303 return profile; | 309 return profile; |
| 304 } | 310 } |
| 305 | 311 |
| 306 Profile* ProfileHelper::GetProfileByUserUnsafe(const user_manager::User* user) { | 312 Profile* ProfileHelper::GetProfileByUserUnsafe(const user_manager::User* user) { |
| 307 // This map is non-empty only in tests. | 313 // This map is non-empty only in tests. |
| 308 if (!user_to_profile_for_testing_.empty()) { | 314 if (!user_to_profile_for_testing_.empty()) { |
| 309 std::map<const user_manager::User*, Profile*>::const_iterator it = | 315 std::map<const user_manager::User*, Profile*>::const_iterator it = |
| 310 user_to_profile_for_testing_.find(user); | 316 user_to_profile_for_testing_.find(user); |
| 311 return it == user_to_profile_for_testing_.end() ? NULL : it->second; | 317 return it == user_to_profile_for_testing_.end() ? NULL : it->second; |
| 312 } | 318 } |
| 313 | 319 |
| 314 Profile* profile = NULL; | 320 Profile* profile = NULL; |
| 315 if (user->is_profile_created()) { | 321 if (user->is_profile_created()) { |
| 316 profile = ProfileHelper::GetProfileByUserIdHash(user->username_hash()); | 322 profile = GetProfileByUserIdHash(user->username_hash()); |
| 317 } else { | 323 } else { |
| 318 LOG(ERROR) << "ProfileHelper::GetProfileByUserUnsafe is called when " | 324 LOG(ERROR) << "ProfileHelper::GetProfileByUserUnsafe is called when " |
| 319 "|user|'s profile is not created. It probably means that " | 325 "|user|'s profile is not created. It probably means that " |
| 320 "something is wrong with a calling code. Please report in " | 326 "something is wrong with a calling code. Please report in " |
| 321 "http://crbug.com/361528 if you see this message."; | 327 "http://crbug.com/361528 if you see this message."; |
| 322 profile = ProfileManager::GetActiveUserProfile(); | 328 profile = ProfileManager::GetActiveUserProfile(); |
| 323 } | 329 } |
| 324 | 330 |
| 325 // GetActiveUserProfile() or GetProfileByUserIdHash() returns a new instance | 331 // GetActiveUserProfile() or GetProfileByUserIdHash() returns a new instance |
| 326 // of ProfileImpl(), but actually its OffTheRecordProfile() should be used. | 332 // of ProfileImpl(), but actually its OffTheRecordProfile() should be used. |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 497 DownloadPrefs::FromBrowserContext(profile)->DownloadPath()); | 503 DownloadPrefs::FromBrowserContext(profile)->DownloadPath()); |
| 498 // Let extension system handle extension files. | 504 // Let extension system handle extension files. |
| 499 excludes.push_back(base::FilePath(extensions::kInstallDirectoryName)); | 505 excludes.push_back(base::FilePath(extensions::kInstallDirectoryName)); |
| 500 // Do not flush Drive cache. | 506 // Do not flush Drive cache. |
| 501 excludes.push_back(base::FilePath(chromeos::kDriveCacheDirname)); | 507 excludes.push_back(base::FilePath(chromeos::kDriveCacheDirname)); |
| 502 | 508 |
| 503 profile_flusher_->RequestFlush(profile->GetPath(), excludes, base::Closure()); | 509 profile_flusher_->RequestFlush(profile->GetPath(), excludes, base::Closure()); |
| 504 } | 510 } |
| 505 | 511 |
| 506 } // namespace chromeos | 512 } // namespace chromeos |
| OLD | NEW |