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" | |
13 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/browser/profiles/profile_manager.h" | 13 #include "chrome/browser/profiles/profile_manager.h" |
15 #include "chrome/browser/profiles/profiles_state.h" | 14 #include "chrome/browser/profiles/profiles_state.h" |
16 #include "chrome/common/chrome_constants.h" | 15 #include "chrome/common/chrome_constants.h" |
17 #include "chrome/common/chrome_switches.h" | 16 #include "chrome/common/chrome_switches.h" |
18 #include "chromeos/chromeos_switches.h" | 17 #include "chromeos/chromeos_switches.h" |
19 #include "components/user_manager/user.h" | 18 #include "components/user_manager/user.h" |
| 19 #include "components/user_manager/user_manager.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 bool ShouldAddProfileDirPrefix(const std::string& user_id_hash) { | 26 bool ShouldAddProfileDirPrefix(const std::string& user_id_hash) { |
27 // Do not add profile dir prefix for legacy profile dir and test | 27 // 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 | 28 // user profile. The reason of not adding prefix for test user profile |
29 // is to keep the promise that TestingProfile::kTestUserProfileDir and | 29 // is to keep the promise that TestingProfile::kTestUserProfileDir and |
(...skipping 24 matching lines...) Expand all Loading... |
54 //////////////////////////////////////////////////////////////////////////////// | 54 //////////////////////////////////////////////////////////////////////////////// |
55 // ProfileHelper, public | 55 // ProfileHelper, public |
56 | 56 |
57 ProfileHelper::ProfileHelper() | 57 ProfileHelper::ProfileHelper() |
58 : signin_profile_clear_requested_(false) { | 58 : signin_profile_clear_requested_(false) { |
59 } | 59 } |
60 | 60 |
61 ProfileHelper::~ProfileHelper() { | 61 ProfileHelper::~ProfileHelper() { |
62 // Checking whether UserManager is initialized covers case | 62 // Checking whether UserManager is initialized covers case |
63 // when ScopedTestUserManager is used. | 63 // when ScopedTestUserManager is used. |
64 if (UserManager::IsInitialized()) | 64 if (user_manager::UserManager::IsInitialized()) |
65 UserManager::Get()->RemoveSessionStateObserver(this); | 65 user_manager::UserManager::Get()->RemoveSessionStateObserver(this); |
66 } | 66 } |
67 | 67 |
68 // static | 68 // static |
69 ProfileHelper* ProfileHelper::Get() { | 69 ProfileHelper* ProfileHelper::Get() { |
70 return g_browser_process->platform_part()->profile_helper(); | 70 return g_browser_process->platform_part()->profile_helper(); |
71 } | 71 } |
72 | 72 |
73 // static | 73 // static |
74 Profile* ProfileHelper::GetProfileByUserIdHash( | 74 Profile* ProfileHelper::GetProfileByUserIdHash( |
75 const std::string& user_id_hash) { | 75 const std::string& user_id_hash) { |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 : base::FilePath(user_id_hash); | 144 : base::FilePath(user_id_hash); |
145 } | 145 } |
146 | 146 |
147 // static | 147 // static |
148 base::FilePath ProfileHelper::GetUserProfileDirByUserId( | 148 base::FilePath ProfileHelper::GetUserProfileDirByUserId( |
149 const std::string& user_id) { | 149 const std::string& user_id) { |
150 // TODO(dpolukhin): Remove Chrome OS specific profile path logic from | 150 // TODO(dpolukhin): Remove Chrome OS specific profile path logic from |
151 // ProfileManager and use only this function to construct profile path. | 151 // ProfileManager and use only this function to construct profile path. |
152 // TODO(nkostylev): Cleanup profile dir related code paths crbug.com/294233 | 152 // TODO(nkostylev): Cleanup profile dir related code paths crbug.com/294233 |
153 base::FilePath profile_dir; | 153 base::FilePath profile_dir; |
154 const user_manager::User* user = UserManager::Get()->FindUser(user_id); | 154 const user_manager::User* user = |
| 155 user_manager::UserManager::Get()->FindUser(user_id); |
155 if (user && !user->username_hash().empty()) | 156 if (user && !user->username_hash().empty()) |
156 profile_dir = ProfileHelper::GetUserProfileDir(user->username_hash()); | 157 profile_dir = ProfileHelper::GetUserProfileDir(user->username_hash()); |
157 | 158 |
158 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 159 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
159 profile_dir = profile_manager->user_data_dir().Append(profile_dir); | 160 profile_dir = profile_manager->user_data_dir().Append(profile_dir); |
160 | 161 |
161 return profile_dir; | 162 return profile_dir; |
162 } | 163 } |
163 | 164 |
164 // static | 165 // static |
165 bool ProfileHelper::IsSigninProfile(Profile* profile) { | 166 bool ProfileHelper::IsSigninProfile(Profile* profile) { |
166 return profile->GetPath().BaseName().value() == chrome::kInitialProfile; | 167 return profile->GetPath().BaseName().value() == chrome::kInitialProfile; |
167 } | 168 } |
168 | 169 |
169 // static | 170 // static |
170 bool ProfileHelper::IsOwnerProfile(Profile* profile) { | 171 bool ProfileHelper::IsOwnerProfile(Profile* profile) { |
171 if (!profile) | 172 if (!profile) |
172 return false; | 173 return false; |
173 user_manager::User* user = ProfileHelper::Get()->GetUserByProfile(profile); | 174 user_manager::User* user = ProfileHelper::Get()->GetUserByProfile(profile); |
174 if (!user) | 175 if (!user) |
175 return false; | 176 return false; |
176 | 177 |
177 return user->email() == chromeos::UserManager::Get()->GetOwnerEmail(); | 178 return user->email() == user_manager::UserManager::Get()->GetOwnerEmail(); |
178 } | 179 } |
179 | 180 |
180 // static | 181 // static |
181 bool ProfileHelper::IsPrimaryProfile(Profile* profile) { | 182 bool ProfileHelper::IsPrimaryProfile(Profile* profile) { |
182 if (!profile) | 183 if (!profile) |
183 return false; | 184 return false; |
184 user_manager::User* user = ProfileHelper::Get()->GetUserByProfile(profile); | 185 user_manager::User* user = ProfileHelper::Get()->GetUserByProfile(profile); |
185 if (!user) | 186 if (!user) |
186 return false; | 187 return false; |
187 return user == chromeos::UserManager::Get()->GetPrimaryUser(); | 188 return user == user_manager::UserManager::Get()->GetPrimaryUser(); |
188 } | 189 } |
189 | 190 |
190 void ProfileHelper::ProfileStartup(Profile* profile, bool process_startup) { | 191 void ProfileHelper::ProfileStartup(Profile* profile, bool process_startup) { |
191 // Initialize Chrome OS preferences like touch pad sensitivity. For the | 192 // Initialize Chrome OS preferences like touch pad sensitivity. For the |
192 // preferences to work in the guest mode, the initialization has to be | 193 // preferences to work in the guest mode, the initialization has to be |
193 // done after |profile| is switched to the incognito profile (which | 194 // done after |profile| is switched to the incognito profile (which |
194 // is actually GuestSessionProfile in the guest mode). See the | 195 // is actually GuestSessionProfile in the guest mode). See the |
195 // GetOffTheRecordProfile() call above. | 196 // GetOffTheRecordProfile() call above. |
196 profile->InitChromeOSPreferences(); | 197 profile->InitChromeOSPreferences(); |
197 | 198 |
198 // Add observer so we can see when the first profile's session restore is | 199 // Add observer so we can see when the first profile's session restore is |
199 // completed. After that, we won't need the default profile anymore. | 200 // completed. After that, we won't need the default profile anymore. |
200 if (!IsSigninProfile(profile) && | 201 if (!IsSigninProfile(profile) && |
201 UserManager::Get()->IsLoggedInAsRegularUser() && | 202 user_manager::UserManager::Get()->IsLoggedInAsRegularUser() && |
202 !UserManager::Get()->IsLoggedInAsStub()) { | 203 !user_manager::UserManager::Get()->IsLoggedInAsStub()) { |
203 chromeos::OAuth2LoginManager* login_manager = | 204 chromeos::OAuth2LoginManager* login_manager = |
204 chromeos::OAuth2LoginManagerFactory::GetInstance()->GetForProfile( | 205 chromeos::OAuth2LoginManagerFactory::GetInstance()->GetForProfile( |
205 profile); | 206 profile); |
206 if (login_manager) | 207 if (login_manager) |
207 login_manager->AddObserver(this); | 208 login_manager->AddObserver(this); |
208 } | 209 } |
209 } | 210 } |
210 | 211 |
211 base::FilePath ProfileHelper::GetActiveUserProfileDir() { | 212 base::FilePath ProfileHelper::GetActiveUserProfileDir() { |
212 return ProfileHelper::GetUserProfileDir(active_user_id_hash_); | 213 return ProfileHelper::GetUserProfileDir(active_user_id_hash_); |
213 } | 214 } |
214 | 215 |
215 void ProfileHelper::Initialize() { | 216 void ProfileHelper::Initialize() { |
216 UserManager::Get()->AddSessionStateObserver(this); | 217 user_manager::UserManager::Get()->AddSessionStateObserver(this); |
217 } | 218 } |
218 | 219 |
219 void ProfileHelper::ClearSigninProfile(const base::Closure& on_clear_callback) { | 220 void ProfileHelper::ClearSigninProfile(const base::Closure& on_clear_callback) { |
220 on_clear_callbacks_.push_back(on_clear_callback); | 221 on_clear_callbacks_.push_back(on_clear_callback); |
221 if (signin_profile_clear_requested_) | 222 if (signin_profile_clear_requested_) |
222 return; | 223 return; |
223 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 224 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
224 // Check if signin profile was loaded. | 225 // Check if signin profile was loaded. |
225 if (!profile_manager->GetProfileByPath(GetSigninProfileDir())) { | 226 if (!profile_manager->GetProfileByPath(GetSigninProfileDir())) { |
226 OnBrowsingDataRemoverDone(); | 227 OnBrowsingDataRemoverDone(); |
(...skipping 15 matching lines...) Expand all Loading... |
242 return it == user_to_profile_for_testing_.end() ? NULL : it->second; | 243 return it == user_to_profile_for_testing_.end() ? NULL : it->second; |
243 } | 244 } |
244 | 245 |
245 if (!user->is_profile_created()) | 246 if (!user->is_profile_created()) |
246 return NULL; | 247 return NULL; |
247 Profile* profile = | 248 Profile* profile = |
248 ProfileHelper::GetProfileByUserIdHash(user->username_hash()); | 249 ProfileHelper::GetProfileByUserIdHash(user->username_hash()); |
249 | 250 |
250 // GetActiveUserProfile() or GetProfileByUserIdHash() returns a new instance | 251 // GetActiveUserProfile() or GetProfileByUserIdHash() returns a new instance |
251 // of ProfileImpl(), but actually its OffTheRecordProfile() should be used. | 252 // of ProfileImpl(), but actually its OffTheRecordProfile() should be used. |
252 if (UserManager::Get()->IsLoggedInAsGuest()) | 253 if (user_manager::UserManager::Get()->IsLoggedInAsGuest()) |
253 profile = profile->GetOffTheRecordProfile(); | 254 profile = profile->GetOffTheRecordProfile(); |
254 | 255 |
255 return profile; | 256 return profile; |
256 } | 257 } |
257 | 258 |
258 Profile* ProfileHelper::GetProfileByUserUnsafe(const user_manager::User* user) { | 259 Profile* ProfileHelper::GetProfileByUserUnsafe(const user_manager::User* user) { |
259 // This map is non-empty only in tests. | 260 // This map is non-empty only in tests. |
260 if (!user_to_profile_for_testing_.empty()) { | 261 if (!user_to_profile_for_testing_.empty()) { |
261 std::map<const user_manager::User*, Profile*>::const_iterator it = | 262 std::map<const user_manager::User*, Profile*>::const_iterator it = |
262 user_to_profile_for_testing_.find(user); | 263 user_to_profile_for_testing_.find(user); |
263 return it == user_to_profile_for_testing_.end() ? NULL : it->second; | 264 return it == user_to_profile_for_testing_.end() ? NULL : it->second; |
264 } | 265 } |
265 | 266 |
266 Profile* profile = NULL; | 267 Profile* profile = NULL; |
267 if (user->is_profile_created()) { | 268 if (user->is_profile_created()) { |
268 profile = ProfileHelper::GetProfileByUserIdHash(user->username_hash()); | 269 profile = ProfileHelper::GetProfileByUserIdHash(user->username_hash()); |
269 } else { | 270 } else { |
270 LOG(WARNING) << "ProfileHelper::GetProfileByUserUnsafe is called when " | 271 LOG(WARNING) << "ProfileHelper::GetProfileByUserUnsafe is called when " |
271 "|user|'s profile is not created. It probably means that " | 272 "|user|'s profile is not created. It probably means that " |
272 "something is wrong with a calling code. Please report in " | 273 "something is wrong with a calling code. Please report in " |
273 "http://crbug.com/361528 if you see this message."; | 274 "http://crbug.com/361528 if you see this message."; |
274 profile = ProfileManager::GetActiveUserProfile(); | 275 profile = ProfileManager::GetActiveUserProfile(); |
275 } | 276 } |
276 | 277 |
277 // GetActiveUserProfile() or GetProfileByUserIdHash() returns a new instance | 278 // GetActiveUserProfile() or GetProfileByUserIdHash() returns a new instance |
278 // of ProfileImpl(), but actually its OffTheRecordProfile() should be used. | 279 // of ProfileImpl(), but actually its OffTheRecordProfile() should be used. |
279 if (profile && UserManager::Get()->IsLoggedInAsGuest()) | 280 if (profile && user_manager::UserManager::Get()->IsLoggedInAsGuest()) |
280 profile = profile->GetOffTheRecordProfile(); | 281 profile = profile->GetOffTheRecordProfile(); |
281 return profile; | 282 return profile; |
282 } | 283 } |
283 | 284 |
284 user_manager::User* ProfileHelper::GetUserByProfile(Profile* profile) { | 285 user_manager::User* ProfileHelper::GetUserByProfile(Profile* profile) { |
285 // This map is non-empty only in tests. | 286 // This map is non-empty only in tests. |
286 if (enable_profile_to_user_testing || !user_list_for_testing_.empty()) { | 287 if (enable_profile_to_user_testing || !user_list_for_testing_.empty()) { |
287 if (always_return_primary_user_for_testing) | 288 if (always_return_primary_user_for_testing) |
288 return const_cast<user_manager::User*>( | 289 return const_cast<user_manager::User*>( |
289 UserManager::Get()->GetPrimaryUser()); | 290 user_manager::UserManager::Get()->GetPrimaryUser()); |
290 | 291 |
291 const std::string& user_name = profile->GetProfileName(); | 292 const std::string& user_name = profile->GetProfileName(); |
292 for (user_manager::UserList::const_iterator it = | 293 for (user_manager::UserList::const_iterator it = |
293 user_list_for_testing_.begin(); | 294 user_list_for_testing_.begin(); |
294 it != user_list_for_testing_.end(); | 295 it != user_list_for_testing_.end(); |
295 ++it) { | 296 ++it) { |
296 if ((*it)->email() == user_name) | 297 if ((*it)->email() == user_name) |
297 return *it; | 298 return *it; |
298 } | 299 } |
299 | 300 |
300 // In case of test setup we should always default to primary user. | 301 // In case of test setup we should always default to primary user. |
301 return const_cast<user_manager::User*>( | 302 return const_cast<user_manager::User*>( |
302 UserManager::Get()->GetPrimaryUser()); | 303 user_manager::UserManager::Get()->GetPrimaryUser()); |
303 } | 304 } |
304 | 305 |
305 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 306 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
306 if (ProfileHelper::IsSigninProfile(profile)) | 307 if (ProfileHelper::IsSigninProfile(profile)) |
307 return NULL; | 308 return NULL; |
308 | 309 |
309 UserManager* user_manager = UserManager::Get(); | 310 user_manager::UserManager* user_manager = user_manager::UserManager::Get(); |
310 | 311 |
311 // Special case for non-CrOS tests that do create several profiles | 312 // Special case for non-CrOS tests that do create several profiles |
312 // and don't really care about mapping to the real user. | 313 // and don't really care about mapping to the real user. |
313 // Without multi-profiles on Chrome OS such tests always got active_user_. | 314 // Without multi-profiles on Chrome OS such tests always got active_user_. |
314 // Now these tests will specify special flag to continue working. | 315 // Now these tests will specify special flag to continue working. |
315 // In future those tests can get a proper CrOS configuration i.e. register | 316 // In future those tests can get a proper CrOS configuration i.e. register |
316 // and login several users if they want to work with an additional profile. | 317 // and login several users if they want to work with an additional profile. |
317 if (CommandLine::ForCurrentProcess()->HasSwitch( | 318 if (CommandLine::ForCurrentProcess()->HasSwitch( |
318 switches::kIgnoreUserProfileMappingForTests)) { | 319 switches::kIgnoreUserProfileMappingForTests)) { |
319 return user_manager->GetActiveUser(); | 320 return user_manager->GetActiveUser(); |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
391 ProfileHelper::SetProfileToUserForTestingEnabled(true); | 392 ProfileHelper::SetProfileToUserForTestingEnabled(true); |
392 } | 393 } |
393 | 394 |
394 void ProfileHelper::SetUserToProfileMappingForTesting( | 395 void ProfileHelper::SetUserToProfileMappingForTesting( |
395 const user_manager::User* user, | 396 const user_manager::User* user, |
396 Profile* profile) { | 397 Profile* profile) { |
397 user_to_profile_for_testing_[user] = profile; | 398 user_to_profile_for_testing_[user] = profile; |
398 } | 399 } |
399 | 400 |
400 } // namespace chromeos | 401 } // namespace chromeos |
OLD | NEW |