[email protected] | afde262 | 2013-04-18 05:04:29 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/chromeos/profiles/profile_helper.h" |
| 6 | |
ginkage | 74a79da4 | 2015-03-23 10:36:55 | [diff] [blame] | 7 | #include "base/barrier_closure.h" |
[email protected] | f8622a4 | 2013-06-07 14:12:36 | [diff] [blame] | 8 | #include "base/callback.h" |
[email protected] | 7cad6b0d | 2013-04-25 20:29:32 | [diff] [blame] | 9 | #include "base/command_line.h" |
[email protected] | afde262 | 2013-04-18 05:04:29 | [diff] [blame] | 10 | #include "chrome/browser/browser_process.h" |
[email protected] | 4c335a29 | 2013-05-08 21:41:01 | [diff] [blame] | 11 | #include "chrome/browser/browsing_data/browsing_data_helper.h" |
dzhioev | b187591 | 2015-04-02 12:13:25 | [diff] [blame] | 12 | #include "chrome/browser/chromeos/login/helper.h" |
[email protected] | 83d82d4 | 2014-05-16 02:04:42 | [diff] [blame] | 13 | #include "chrome/browser/chromeos/login/signin/oauth2_login_manager_factory.h" |
[email protected] | afde262 | 2013-04-18 05:04:29 | [diff] [blame] | 14 | #include "chrome/browser/profiles/profile.h" |
[email protected] | afde262 | 2013-04-18 05:04:29 | [diff] [blame] | 15 | #include "chrome/browser/profiles/profile_manager.h" |
[email protected] | f9357a44 | 2014-05-15 18:44:07 | [diff] [blame] | 16 | #include "chrome/browser/profiles/profiles_state.h" |
[email protected] | afde262 | 2013-04-18 05:04:29 | [diff] [blame] | 17 | #include "chrome/common/chrome_constants.h" |
[email protected] | 7cad6b0d | 2013-04-25 20:29:32 | [diff] [blame] | 18 | #include "chrome/common/chrome_switches.h" |
[email protected] | 82b1ec87 | 2013-12-06 17:07:15 | [diff] [blame] | 19 | #include "chromeos/chromeos_switches.h" |
fsamuel | 8dfa19a | 2015-05-05 01:00:39 | [diff] [blame^] | 20 | #include "components/guest_view/browser/guest_view_manager.h" |
[email protected] | 2fda997 | 2014-07-23 14:51:59 | [diff] [blame] | 21 | #include "components/user_manager/user.h" |
[email protected] | 4d39078 | 2014-08-15 09:22:58 | [diff] [blame] | 22 | #include "components/user_manager/user_manager.h" |
[email protected] | 052e3ac | 2014-06-30 14:22:47 | [diff] [blame] | 23 | #include "content/public/browser/browser_thread.h" |
ginkage | 74a79da4 | 2015-03-23 10:36:55 | [diff] [blame] | 24 | #include "content/public/browser/storage_partition.h" |
| 25 | #include "content/public/browser/web_contents.h" |
ginkage | 74a79da4 | 2015-03-23 10:36:55 | [diff] [blame] | 26 | #include "extensions/browser/guest_view/web_view/web_view_guest.h" |
[email protected] | afde262 | 2013-04-18 05:04:29 | [diff] [blame] | 27 | |
| 28 | namespace chromeos { |
| 29 | |
[email protected] | 09f34ec7 | 2013-06-24 14:49:55 | [diff] [blame] | 30 | namespace { |
| 31 | |
dzhioev | 0298d72 | 2014-08-26 13:18:52 | [diff] [blame] | 32 | // As defined in /chromeos/dbus/cryptohome_client.cc. |
| 33 | static const char kUserIdHashSuffix[] = "-hash"; |
| 34 | |
[email protected] | 64146ec | 2014-03-19 21:52:25 | [diff] [blame] | 35 | bool ShouldAddProfileDirPrefix(const std::string& user_id_hash) { |
| 36 | // Do not add profile dir prefix for legacy profile dir and test |
| 37 | // user profile. The reason of not adding prefix for test user profile |
| 38 | // is to keep the promise that TestingProfile::kTestUserProfileDir and |
| 39 | // chrome::kTestUserProfileDir are always in sync. Otherwise, |
| 40 | // TestingProfile::kTestUserProfileDir needs to be dynamically calculated |
| 41 | // based on whether multi profile is enabled or not. |
| 42 | return user_id_hash != chrome::kLegacyProfileDir && |
| 43 | user_id_hash != chrome::kTestUserProfileDir; |
| 44 | } |
| 45 | |
[email protected] | 052e3ac | 2014-06-30 14:22:47 | [diff] [blame] | 46 | class UsernameHashMatcher { |
| 47 | public: |
| 48 | explicit UsernameHashMatcher(const std::string& h) : username_hash(h) {} |
[email protected] | 2fda997 | 2014-07-23 14:51:59 | [diff] [blame] | 49 | bool operator()(const user_manager::User* user) const { |
[email protected] | 052e3ac | 2014-06-30 14:22:47 | [diff] [blame] | 50 | return user->username_hash() == username_hash; |
| 51 | } |
| 52 | |
| 53 | private: |
| 54 | const std::string& username_hash; |
| 55 | }; |
| 56 | |
[email protected] | 09f34ec7 | 2013-06-24 14:49:55 | [diff] [blame] | 57 | } // anonymous namespace |
| 58 | |
[email protected] | 052e3ac | 2014-06-30 14:22:47 | [diff] [blame] | 59 | // static |
| 60 | bool ProfileHelper::enable_profile_to_user_testing = false; |
| 61 | bool ProfileHelper::always_return_primary_user_for_testing = false; |
| 62 | |
[email protected] | 7cad6b0d | 2013-04-25 20:29:32 | [diff] [blame] | 63 | //////////////////////////////////////////////////////////////////////////////// |
| 64 | // ProfileHelper, public |
| 65 | |
[email protected] | 4c335a29 | 2013-05-08 21:41:01 | [diff] [blame] | 66 | ProfileHelper::ProfileHelper() |
dzhioev | b187591 | 2015-04-02 12:13:25 | [diff] [blame] | 67 | : browsing_data_remover_(nullptr), weak_factory_(this) { |
[email protected] | 7cad6b0d | 2013-04-25 20:29:32 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | ProfileHelper::~ProfileHelper() { |
[email protected] | f8622a4 | 2013-06-07 14:12:36 | [diff] [blame] | 71 | // Checking whether UserManager is initialized covers case |
| 72 | // when ScopedTestUserManager is used. |
[email protected] | 4d39078 | 2014-08-15 09:22:58 | [diff] [blame] | 73 | if (user_manager::UserManager::IsInitialized()) |
| 74 | user_manager::UserManager::Get()->RemoveSessionStateObserver(this); |
dzhioev | b187591 | 2015-04-02 12:13:25 | [diff] [blame] | 75 | |
| 76 | if (browsing_data_remover_) { |
| 77 | browsing_data_remover_->RemoveObserver(this); |
| 78 | // BrowsingDataRemover deletes itself. |
| 79 | browsing_data_remover_ = nullptr; |
| 80 | } |
[email protected] | 7cad6b0d | 2013-04-25 20:29:32 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | // static |
[email protected] | 052e3ac | 2014-06-30 14:22:47 | [diff] [blame] | 84 | ProfileHelper* ProfileHelper::Get() { |
| 85 | return g_browser_process->platform_part()->profile_helper(); |
| 86 | } |
| 87 | |
| 88 | // static |
[email protected] | 7cad6b0d | 2013-04-25 20:29:32 | [diff] [blame] | 89 | Profile* ProfileHelper::GetProfileByUserIdHash( |
| 90 | const std::string& user_id_hash) { |
| 91 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
[email protected] | 847522f | 2013-05-10 09:32:42 | [diff] [blame] | 92 | return profile_manager->GetProfile(GetProfilePathByUserIdHash(user_id_hash)); |
[email protected] | 7cad6b0d | 2013-04-25 20:29:32 | [diff] [blame] | 93 | } |
| 94 | |
[email protected] | afde262 | 2013-04-18 05:04:29 | [diff] [blame] | 95 | // static |
[email protected] | d02f6c69 | 2013-05-23 13:58:13 | [diff] [blame] | 96 | base::FilePath ProfileHelper::GetProfilePathByUserIdHash( |
| 97 | const std::string& user_id_hash) { |
[email protected] | f9357a44 | 2014-05-15 18:44:07 | [diff] [blame] | 98 | // Fails for KioskTest.InstallAndLaunchApp test - crbug.com/238985 |
| 99 | // Will probably fail for Guest session / restart after a crash - |
| 100 | // crbug.com/238998 |
| 101 | // TODO(nkostylev): Remove this check once these bugs are fixed. |
| 102 | DCHECK(!user_id_hash.empty()); |
[email protected] | d02f6c69 | 2013-05-23 13:58:13 | [diff] [blame] | 103 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 104 | base::FilePath profile_path = profile_manager->user_data_dir(); |
[email protected] | 64146ec | 2014-03-19 21:52:25 | [diff] [blame] | 105 | |
| 106 | return profile_path.Append(GetUserProfileDir(user_id_hash)); |
[email protected] | d02f6c69 | 2013-05-23 13:58:13 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | // static |
[email protected] | c65b9c28 | 2014-04-04 16:37:17 | [diff] [blame] | 110 | base::FilePath ProfileHelper::GetSigninProfileDir() { |
| 111 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 112 | base::FilePath user_data_dir = profile_manager->user_data_dir(); |
| 113 | return user_data_dir.AppendASCII(chrome::kInitialProfile); |
| 114 | } |
| 115 | |
| 116 | // static |
[email protected] | afde262 | 2013-04-18 05:04:29 | [diff] [blame] | 117 | Profile* ProfileHelper::GetSigninProfile() { |
| 118 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
[email protected] | 09f34ec7 | 2013-06-24 14:49:55 | [diff] [blame] | 119 | return profile_manager->GetProfile(GetSigninProfileDir())-> |
[email protected] | afde262 | 2013-04-18 05:04:29 | [diff] [blame] | 120 | GetOffTheRecordProfile(); |
| 121 | } |
| 122 | |
| 123 | // static |
isherman | 91ffd8b | 2015-02-19 08:51:32 | [diff] [blame] | 124 | std::string ProfileHelper::GetUserIdHashFromProfile(const Profile* profile) { |
[email protected] | 847522f | 2013-05-10 09:32:42 | [diff] [blame] | 125 | if (!profile) |
| 126 | return std::string(); |
| 127 | |
[email protected] | 847522f | 2013-05-10 09:32:42 | [diff] [blame] | 128 | std::string profile_dir = profile->GetPath().BaseName().value(); |
[email protected] | 64146ec | 2014-03-19 21:52:25 | [diff] [blame] | 129 | |
| 130 | // Don't strip prefix if the dir is not supposed to be prefixed. |
| 131 | if (!ShouldAddProfileDirPrefix(profile_dir)) |
| 132 | return profile_dir; |
| 133 | |
| 134 | // Check that profile directory starts with the correct prefix. |
[email protected] | 68e3169 | 2013-05-20 13:08:27 | [diff] [blame] | 135 | std::string prefix(chrome::kProfileDirPrefix); |
[email protected] | 847522f | 2013-05-10 09:32:42 | [diff] [blame] | 136 | if (profile_dir.find(prefix) != 0) { |
[email protected] | 0301c5f | 2014-01-23 04:32:15 | [diff] [blame] | 137 | // This happens when creating a TestingProfile in browser tests. |
[email protected] | 847522f | 2013-05-10 09:32:42 | [diff] [blame] | 138 | return std::string(); |
| 139 | } |
| 140 | |
| 141 | return profile_dir.substr(prefix.length(), |
| 142 | profile_dir.length() - prefix.length()); |
| 143 | } |
| 144 | |
| 145 | // static |
[email protected] | 82b1ec87 | 2013-12-06 17:07:15 | [diff] [blame] | 146 | base::FilePath ProfileHelper::GetUserProfileDir( |
| 147 | const std::string& user_id_hash) { |
dzhioev | 0298d72 | 2014-08-26 13:18:52 | [diff] [blame] | 148 | CHECK(!user_id_hash.empty()); |
[email protected] | 64146ec | 2014-03-19 21:52:25 | [diff] [blame] | 149 | return ShouldAddProfileDirPrefix(user_id_hash) |
| 150 | ? base::FilePath(chrome::kProfileDirPrefix + user_id_hash) |
| 151 | : base::FilePath(user_id_hash); |
[email protected] | 82b1ec87 | 2013-12-06 17:07:15 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | // static |
isherman | 91ffd8b | 2015-02-19 08:51:32 | [diff] [blame] | 155 | bool ProfileHelper::IsSigninProfile(const Profile* profile) { |
ginkage | 9d3e646 | 2015-02-26 12:18:05 | [diff] [blame] | 156 | return profile && |
| 157 | profile->GetPath().BaseName().value() == chrome::kInitialProfile; |
[email protected] | 42c0d5ed | 2013-04-30 17:04:45 | [diff] [blame] | 158 | } |
| 159 | |
[email protected] | cb993480 | 2014-01-30 01:45:10 | [diff] [blame] | 160 | // static |
| 161 | bool ProfileHelper::IsOwnerProfile(Profile* profile) { |
[email protected] | 24c22df | 2014-02-10 15:36:52 | [diff] [blame] | 162 | if (!profile) |
| 163 | return false; |
isherman | 91ffd8b | 2015-02-19 08:51:32 | [diff] [blame] | 164 | const user_manager::User* user = |
| 165 | ProfileHelper::Get()->GetUserByProfile(profile); |
[email protected] | 24c22df | 2014-02-10 15:36:52 | [diff] [blame] | 166 | if (!user) |
| 167 | return false; |
[email protected] | 052e3ac | 2014-06-30 14:22:47 | [diff] [blame] | 168 | |
[email protected] | 4d39078 | 2014-08-15 09:22:58 | [diff] [blame] | 169 | return user->email() == user_manager::UserManager::Get()->GetOwnerEmail(); |
[email protected] | cb993480 | 2014-01-30 01:45:10 | [diff] [blame] | 170 | } |
| 171 | |
[email protected] | 6426834 | 2014-08-12 17:18:22 | [diff] [blame] | 172 | // static |
isherman | 91ffd8b | 2015-02-19 08:51:32 | [diff] [blame] | 173 | bool ProfileHelper::IsPrimaryProfile(const Profile* profile) { |
[email protected] | 6465e0f | 2014-08-07 07:57:58 | [diff] [blame] | 174 | if (!profile) |
| 175 | return false; |
isherman | 91ffd8b | 2015-02-19 08:51:32 | [diff] [blame] | 176 | const user_manager::User* user = |
| 177 | ProfileHelper::Get()->GetUserByProfile(profile); |
[email protected] | 6465e0f | 2014-08-07 07:57:58 | [diff] [blame] | 178 | if (!user) |
| 179 | return false; |
[email protected] | 4d39078 | 2014-08-15 09:22:58 | [diff] [blame] | 180 | return user == user_manager::UserManager::Get()->GetPrimaryUser(); |
[email protected] | 6465e0f | 2014-08-07 07:57:58 | [diff] [blame] | 181 | } |
| 182 | |
[email protected] | afde262 | 2013-04-18 05:04:29 | [diff] [blame] | 183 | void ProfileHelper::ProfileStartup(Profile* profile, bool process_startup) { |
| 184 | // Initialize Chrome OS preferences like touch pad sensitivity. For the |
| 185 | // preferences to work in the guest mode, the initialization has to be |
| 186 | // done after |profile| is switched to the incognito profile (which |
| 187 | // is actually GuestSessionProfile in the guest mode). See the |
| 188 | // GetOffTheRecordProfile() call above. |
| 189 | profile->InitChromeOSPreferences(); |
| 190 | |
[email protected] | 56f10afd | 2013-09-12 02:01:16 | [diff] [blame] | 191 | // Add observer so we can see when the first profile's session restore is |
| 192 | // completed. After that, we won't need the default profile anymore. |
| 193 | if (!IsSigninProfile(profile) && |
merkulova | c3ae44d | 2014-11-17 09:35:07 | [diff] [blame] | 194 | user_manager::UserManager::Get()->IsLoggedInAsUserWithGaiaAccount() && |
[email protected] | 4d39078 | 2014-08-15 09:22:58 | [diff] [blame] | 195 | !user_manager::UserManager::Get()->IsLoggedInAsStub()) { |
[email protected] | 56f10afd | 2013-09-12 02:01:16 | [diff] [blame] | 196 | chromeos::OAuth2LoginManager* login_manager = |
| 197 | chromeos::OAuth2LoginManagerFactory::GetInstance()->GetForProfile( |
| 198 | profile); |
| 199 | if (login_manager) |
| 200 | login_manager->AddObserver(this); |
[email protected] | afde262 | 2013-04-18 05:04:29 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
[email protected] | 847522f | 2013-05-10 09:32:42 | [diff] [blame] | 204 | base::FilePath ProfileHelper::GetActiveUserProfileDir() { |
[email protected] | 82b1ec87 | 2013-12-06 17:07:15 | [diff] [blame] | 205 | return ProfileHelper::GetUserProfileDir(active_user_id_hash_); |
[email protected] | 847522f | 2013-05-10 09:32:42 | [diff] [blame] | 206 | } |
| 207 | |
[email protected] | 7cad6b0d | 2013-04-25 20:29:32 | [diff] [blame] | 208 | void ProfileHelper::Initialize() { |
[email protected] | 4d39078 | 2014-08-15 09:22:58 | [diff] [blame] | 209 | user_manager::UserManager::Get()->AddSessionStateObserver(this); |
[email protected] | 7cad6b0d | 2013-04-25 20:29:32 | [diff] [blame] | 210 | } |
| 211 | |
dzhioev | b187591 | 2015-04-02 12:13:25 | [diff] [blame] | 212 | void ProfileHelper::ClearSigninProfile(const base::Closure& on_clear_callback) { |
| 213 | on_clear_callbacks_.push_back(on_clear_callback); |
| 214 | |
| 215 | // Profile is already clearing. |
| 216 | if (on_clear_callbacks_.size() > 1) |
[email protected] | 4c335a29 | 2013-05-08 21:41:01 | [diff] [blame] | 217 | return; |
dzhioev | b187591 | 2015-04-02 12:13:25 | [diff] [blame] | 218 | |
| 219 | on_clear_profile_stage_finished_ = |
| 220 | base::BarrierClosure(2, base::Bind(&ProfileHelper::OnSigninProfileCleared, |
| 221 | weak_factory_.GetWeakPtr())); |
| 222 | |
[email protected] | 09f34ec7 | 2013-06-24 14:49:55 | [diff] [blame] | 223 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 224 | // Check if signin profile was loaded. |
xiyuan | 353d7ce | 2015-04-14 20:08:30 | [diff] [blame] | 225 | if (profile_manager->GetProfileByPath(GetSigninProfileDir())) { |
dzhioev | b187591 | 2015-04-02 12:13:25 | [diff] [blame] | 226 | LOG_ASSERT(!browsing_data_remover_); |
| 227 | browsing_data_remover_ = |
xiyuan | 353d7ce | 2015-04-14 20:08:30 | [diff] [blame] | 228 | BrowsingDataRemover::CreateForUnboundedRange(GetSigninProfile()); |
dzhioev | b187591 | 2015-04-02 12:13:25 | [diff] [blame] | 229 | browsing_data_remover_->AddObserver(this); |
| 230 | browsing_data_remover_->Remove(BrowsingDataRemover::REMOVE_SITE_DATA, |
| 231 | BrowsingDataHelper::ALL); |
| 232 | } else { |
| 233 | on_clear_profile_stage_finished_.Run(); |
[email protected] | 09f34ec7 | 2013-06-24 14:49:55 | [diff] [blame] | 234 | } |
dzhioev | b187591 | 2015-04-02 12:13:25 | [diff] [blame] | 235 | |
| 236 | if (content::StoragePartition* partition = login::GetSigninPartition()) { |
| 237 | partition->ClearData( |
| 238 | content::StoragePartition::REMOVE_DATA_MASK_ALL, |
| 239 | content::StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL, GURL(), |
| 240 | content::StoragePartition::OriginMatcherFunction(), base::Time(), |
| 241 | base::Time::Now(), on_clear_profile_stage_finished_); |
| 242 | } else { |
| 243 | on_clear_profile_stage_finished_.Run(); |
| 244 | } |
[email protected] | 4c335a29 | 2013-05-08 21:41:01 | [diff] [blame] | 245 | } |
| 246 | |
[email protected] | 2fda997 | 2014-07-23 14:51:59 | [diff] [blame] | 247 | Profile* ProfileHelper::GetProfileByUser(const user_manager::User* user) { |
[email protected] | 052e3ac | 2014-06-30 14:22:47 | [diff] [blame] | 248 | // This map is non-empty only in tests. |
| 249 | if (!user_to_profile_for_testing_.empty()) { |
[email protected] | 2fda997 | 2014-07-23 14:51:59 | [diff] [blame] | 250 | std::map<const user_manager::User*, Profile*>::const_iterator it = |
[email protected] | 052e3ac | 2014-06-30 14:22:47 | [diff] [blame] | 251 | user_to_profile_for_testing_.find(user); |
| 252 | return it == user_to_profile_for_testing_.end() ? NULL : it->second; |
| 253 | } |
| 254 | |
[email protected] | 6426834 | 2014-08-12 17:18:22 | [diff] [blame] | 255 | if (!user->is_profile_created()) |
| 256 | return NULL; |
| 257 | Profile* profile = |
| 258 | ProfileHelper::GetProfileByUserIdHash(user->username_hash()); |
| 259 | |
| 260 | // GetActiveUserProfile() or GetProfileByUserIdHash() returns a new instance |
| 261 | // of ProfileImpl(), but actually its OffTheRecordProfile() should be used. |
[email protected] | 4d39078 | 2014-08-15 09:22:58 | [diff] [blame] | 262 | if (user_manager::UserManager::Get()->IsLoggedInAsGuest()) |
[email protected] | 6426834 | 2014-08-12 17:18:22 | [diff] [blame] | 263 | profile = profile->GetOffTheRecordProfile(); |
| 264 | |
| 265 | return profile; |
| 266 | } |
| 267 | |
| 268 | Profile* ProfileHelper::GetProfileByUserUnsafe(const user_manager::User* user) { |
| 269 | // This map is non-empty only in tests. |
| 270 | if (!user_to_profile_for_testing_.empty()) { |
| 271 | std::map<const user_manager::User*, Profile*>::const_iterator it = |
| 272 | user_to_profile_for_testing_.find(user); |
| 273 | return it == user_to_profile_for_testing_.end() ? NULL : it->second; |
| 274 | } |
| 275 | |
[email protected] | 052e3ac | 2014-06-30 14:22:47 | [diff] [blame] | 276 | Profile* profile = NULL; |
[email protected] | 6426834 | 2014-08-12 17:18:22 | [diff] [blame] | 277 | if (user->is_profile_created()) { |
[email protected] | 052e3ac | 2014-06-30 14:22:47 | [diff] [blame] | 278 | profile = ProfileHelper::GetProfileByUserIdHash(user->username_hash()); |
[email protected] | 6426834 | 2014-08-12 17:18:22 | [diff] [blame] | 279 | } else { |
| 280 | LOG(WARNING) << "ProfileHelper::GetProfileByUserUnsafe is called when " |
| 281 | "|user|'s profile is not created. It probably means that " |
| 282 | "something is wrong with a calling code. Please report in " |
nkostylev | 5df7e99 | 2014-09-26 09:03:47 | [diff] [blame] | 283 | "http://crbug.com/361528 if you see this message. user_id: " |
| 284 | << user->email(); |
[email protected] | 052e3ac | 2014-06-30 14:22:47 | [diff] [blame] | 285 | profile = ProfileManager::GetActiveUserProfile(); |
[email protected] | 6426834 | 2014-08-12 17:18:22 | [diff] [blame] | 286 | } |
[email protected] | 052e3ac | 2014-06-30 14:22:47 | [diff] [blame] | 287 | |
| 288 | // GetActiveUserProfile() or GetProfileByUserIdHash() returns a new instance |
| 289 | // of ProfileImpl(), but actually its OffTheRecordProfile() should be used. |
[email protected] | 4d39078 | 2014-08-15 09:22:58 | [diff] [blame] | 290 | if (profile && user_manager::UserManager::Get()->IsLoggedInAsGuest()) |
[email protected] | 052e3ac | 2014-06-30 14:22:47 | [diff] [blame] | 291 | profile = profile->GetOffTheRecordProfile(); |
| 292 | return profile; |
| 293 | } |
| 294 | |
isherman | 91ffd8b | 2015-02-19 08:51:32 | [diff] [blame] | 295 | const user_manager::User* ProfileHelper::GetUserByProfile( |
| 296 | const Profile* profile) const { |
[email protected] | 052e3ac | 2014-06-30 14:22:47 | [diff] [blame] | 297 | // This map is non-empty only in tests. |
| 298 | if (enable_profile_to_user_testing || !user_list_for_testing_.empty()) { |
| 299 | if (always_return_primary_user_for_testing) |
isherman | 91ffd8b | 2015-02-19 08:51:32 | [diff] [blame] | 300 | return user_manager::UserManager::Get()->GetPrimaryUser(); |
[email protected] | 052e3ac | 2014-06-30 14:22:47 | [diff] [blame] | 301 | |
noms | d9d36d7 | 2015-02-10 04:48:26 | [diff] [blame] | 302 | const std::string& user_name = profile->GetProfileUserName(); |
[email protected] | 2fda997 | 2014-07-23 14:51:59 | [diff] [blame] | 303 | for (user_manager::UserList::const_iterator it = |
| 304 | user_list_for_testing_.begin(); |
[email protected] | 052e3ac | 2014-06-30 14:22:47 | [diff] [blame] | 305 | it != user_list_for_testing_.end(); |
| 306 | ++it) { |
| 307 | if ((*it)->email() == user_name) |
| 308 | return *it; |
| 309 | } |
| 310 | |
| 311 | // In case of test setup we should always default to primary user. |
isherman | 91ffd8b | 2015-02-19 08:51:32 | [diff] [blame] | 312 | return user_manager::UserManager::Get()->GetPrimaryUser(); |
[email protected] | 052e3ac | 2014-06-30 14:22:47 | [diff] [blame] | 313 | } |
| 314 | |
dzhioev | 0298d72 | 2014-08-26 13:18:52 | [diff] [blame] | 315 | DCHECK(!content::BrowserThread::IsThreadInitialized( |
| 316 | content::BrowserThread::UI) || |
| 317 | content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
[email protected] | 052e3ac | 2014-06-30 14:22:47 | [diff] [blame] | 318 | if (ProfileHelper::IsSigninProfile(profile)) |
| 319 | return NULL; |
| 320 | |
[email protected] | 4d39078 | 2014-08-15 09:22:58 | [diff] [blame] | 321 | user_manager::UserManager* user_manager = user_manager::UserManager::Get(); |
[email protected] | 052e3ac | 2014-06-30 14:22:47 | [diff] [blame] | 322 | |
| 323 | // Special case for non-CrOS tests that do create several profiles |
| 324 | // and don't really care about mapping to the real user. |
| 325 | // Without multi-profiles on Chrome OS such tests always got active_user_. |
| 326 | // Now these tests will specify special flag to continue working. |
| 327 | // In future those tests can get a proper CrOS configuration i.e. register |
| 328 | // and login several users if they want to work with an additional profile. |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 329 | if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
[email protected] | 052e3ac | 2014-06-30 14:22:47 | [diff] [blame] | 330 | switches::kIgnoreUserProfileMappingForTests)) { |
| 331 | return user_manager->GetActiveUser(); |
| 332 | } |
| 333 | |
| 334 | const std::string username_hash = |
| 335 | ProfileHelper::GetUserIdHashFromProfile(profile); |
[email protected] | 2fda997 | 2014-07-23 14:51:59 | [diff] [blame] | 336 | const user_manager::UserList& users = user_manager->GetUsers(); |
| 337 | const user_manager::UserList::const_iterator pos = std::find_if( |
[email protected] | 052e3ac | 2014-06-30 14:22:47 | [diff] [blame] | 338 | users.begin(), users.end(), UsernameHashMatcher(username_hash)); |
| 339 | if (pos != users.end()) |
| 340 | return *pos; |
| 341 | |
| 342 | // Many tests do not have their users registered with UserManager and |
| 343 | // runs here. If |active_user_| matches |profile|, returns it. |
[email protected] | 2fda997 | 2014-07-23 14:51:59 | [diff] [blame] | 344 | const user_manager::User* active_user = user_manager->GetActiveUser(); |
[email protected] | 052e3ac | 2014-06-30 14:22:47 | [diff] [blame] | 345 | return active_user && |
| 346 | ProfileHelper::GetProfilePathByUserIdHash( |
| 347 | active_user->username_hash()) == profile->GetPath() |
isherman | 91ffd8b | 2015-02-19 08:51:32 | [diff] [blame] | 348 | ? active_user |
[email protected] | 052e3ac | 2014-06-30 14:22:47 | [diff] [blame] | 349 | : NULL; |
| 350 | } |
| 351 | |
isherman | 91ffd8b | 2015-02-19 08:51:32 | [diff] [blame] | 352 | user_manager::User* ProfileHelper::GetUserByProfile(Profile* profile) const { |
| 353 | return const_cast<user_manager::User*>( |
| 354 | GetUserByProfile(static_cast<const Profile*>(profile))); |
| 355 | } |
| 356 | |
dzhioev | b187591 | 2015-04-02 12:13:25 | [diff] [blame] | 357 | void ProfileHelper::OnSigninProfileCleared() { |
| 358 | std::vector<base::Closure> callbacks; |
| 359 | callbacks.swap(on_clear_callbacks_); |
| 360 | for (const base::Closure& callback : callbacks) { |
| 361 | if (!callback.is_null()) |
| 362 | callback.Run(); |
| 363 | } |
| 364 | } |
| 365 | |
[email protected] | 4c335a29 | 2013-05-08 21:41:01 | [diff] [blame] | 366 | //////////////////////////////////////////////////////////////////////////////// |
| 367 | // ProfileHelper, BrowsingDataRemover::Observer implementation: |
| 368 | |
| 369 | void ProfileHelper::OnBrowsingDataRemoverDone() { |
dzhioev | b187591 | 2015-04-02 12:13:25 | [diff] [blame] | 370 | LOG_ASSERT(browsing_data_remover_); |
| 371 | browsing_data_remover_->RemoveObserver(this); |
| 372 | // BrowsingDataRemover deletes itself. |
| 373 | browsing_data_remover_ = nullptr; |
| 374 | |
| 375 | on_clear_profile_stage_finished_.Run(); |
[email protected] | 4c335a29 | 2013-05-08 21:41:01 | [diff] [blame] | 376 | } |
| 377 | |
[email protected] | 7cad6b0d | 2013-04-25 20:29:32 | [diff] [blame] | 378 | //////////////////////////////////////////////////////////////////////////////// |
[email protected] | 56f10afd | 2013-09-12 02:01:16 | [diff] [blame] | 379 | // ProfileHelper, OAuth2LoginManager::Observer implementation: |
[email protected] | f8622a4 | 2013-06-07 14:12:36 | [diff] [blame] | 380 | |
[email protected] | 56f10afd | 2013-09-12 02:01:16 | [diff] [blame] | 381 | void ProfileHelper::OnSessionRestoreStateChanged( |
| 382 | Profile* user_profile, |
| 383 | OAuth2LoginManager::SessionRestoreState state) { |
[email protected] | 8321c5f | 2013-10-11 11:26:18 | [diff] [blame] | 384 | if (state == OAuth2LoginManager::SESSION_RESTORE_DONE || |
| 385 | state == OAuth2LoginManager::SESSION_RESTORE_FAILED || |
| 386 | state == OAuth2LoginManager::SESSION_RESTORE_CONNECTION_FAILED) { |
[email protected] | 56f10afd | 2013-09-12 02:01:16 | [diff] [blame] | 387 | chromeos::OAuth2LoginManager* login_manager = |
[email protected] | 8321c5f | 2013-10-11 11:26:18 | [diff] [blame] | 388 | chromeos::OAuth2LoginManagerFactory::GetInstance()-> |
| 389 | GetForProfile(user_profile); |
[email protected] | 56f10afd | 2013-09-12 02:01:16 | [diff] [blame] | 390 | login_manager->RemoveObserver(this); |
dzhioev | b187591 | 2015-04-02 12:13:25 | [diff] [blame] | 391 | ClearSigninProfile(base::Closure()); |
[email protected] | 56f10afd | 2013-09-12 02:01:16 | [diff] [blame] | 392 | } |
[email protected] | f8622a4 | 2013-06-07 14:12:36 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | //////////////////////////////////////////////////////////////////////////////// |
[email protected] | 7cad6b0d | 2013-04-25 20:29:32 | [diff] [blame] | 396 | // ProfileHelper, UserManager::UserSessionStateObserver implementation: |
| 397 | |
| 398 | void ProfileHelper::ActiveUserHashChanged(const std::string& hash) { |
| 399 | active_user_id_hash_ = hash; |
[email protected] | 7cad6b0d | 2013-04-25 20:29:32 | [diff] [blame] | 400 | } |
| 401 | |
[email protected] | 2fda997 | 2014-07-23 14:51:59 | [diff] [blame] | 402 | void ProfileHelper::SetProfileToUserMappingForTesting( |
| 403 | user_manager::User* user) { |
[email protected] | 052e3ac | 2014-06-30 14:22:47 | [diff] [blame] | 404 | user_list_for_testing_.push_back(user); |
| 405 | } |
| 406 | |
| 407 | // static |
| 408 | void ProfileHelper::SetProfileToUserForTestingEnabled(bool enabled) { |
| 409 | enable_profile_to_user_testing = enabled; |
| 410 | } |
| 411 | |
| 412 | // static |
| 413 | void ProfileHelper::SetAlwaysReturnPrimaryUserForTesting(bool value) { |
| 414 | always_return_primary_user_for_testing = true; |
| 415 | ProfileHelper::SetProfileToUserForTestingEnabled(true); |
| 416 | } |
| 417 | |
[email protected] | 2fda997 | 2014-07-23 14:51:59 | [diff] [blame] | 418 | void ProfileHelper::SetUserToProfileMappingForTesting( |
| 419 | const user_manager::User* user, |
| 420 | Profile* profile) { |
[email protected] | 052e3ac | 2014-06-30 14:22:47 | [diff] [blame] | 421 | user_to_profile_for_testing_[user] = profile; |
| 422 | } |
| 423 | |
dzhioev | 0298d72 | 2014-08-26 13:18:52 | [diff] [blame] | 424 | // static |
| 425 | std::string ProfileHelper::GetUserIdHashByUserIdForTesting( |
| 426 | const std::string& user_id) { |
| 427 | return user_id + kUserIdHashSuffix; |
| 428 | } |
| 429 | |
[email protected] | afde262 | 2013-04-18 05:04:29 | [diff] [blame] | 430 | } // namespace chromeos |