Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(396)

Side by Side Diff: chrome/browser/chromeos/profiles/profile_helper.cc

Issue 398753004: [cros] Move User class to user_manager component. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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.h"
13 #include "chrome/browser/chromeos/login/users/user_manager.h" 12 #include "chrome/browser/chromeos/login/users/user_manager.h"
14 #include "chrome/browser/profiles/profile.h" 13 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/profiles/profile_manager.h" 14 #include "chrome/browser/profiles/profile_manager.h"
16 #include "chrome/browser/profiles/profiles_state.h" 15 #include "chrome/browser/profiles/profiles_state.h"
17 #include "chrome/common/chrome_constants.h" 16 #include "chrome/common/chrome_constants.h"
18 #include "chrome/common/chrome_switches.h" 17 #include "chrome/common/chrome_switches.h"
19 #include "chromeos/chromeos_switches.h" 18 #include "chromeos/chromeos_switches.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 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
30 // chrome::kTestUserProfileDir are always in sync. Otherwise, 30 // chrome::kTestUserProfileDir are always in sync. Otherwise,
31 // TestingProfile::kTestUserProfileDir needs to be dynamically calculated 31 // TestingProfile::kTestUserProfileDir needs to be dynamically calculated
32 // based on whether multi profile is enabled or not. 32 // based on whether multi profile is enabled or not.
33 return user_id_hash != chrome::kLegacyProfileDir && 33 return user_id_hash != chrome::kLegacyProfileDir &&
34 user_id_hash != chrome::kTestUserProfileDir; 34 user_id_hash != chrome::kTestUserProfileDir;
35 } 35 }
36 36
37 class UsernameHashMatcher { 37 class UsernameHashMatcher {
38 public: 38 public:
39 explicit UsernameHashMatcher(const std::string& h) : username_hash(h) {} 39 explicit UsernameHashMatcher(const std::string& h) : username_hash(h) {}
40 bool operator()(const User* user) const { 40 bool operator()(const user_manager::User* user) const {
41 return user->username_hash() == username_hash; 41 return user->username_hash() == username_hash;
42 } 42 }
43 43
44 private: 44 private:
45 const std::string& username_hash; 45 const std::string& username_hash;
46 }; 46 };
47 47
48 } // anonymous namespace 48 } // anonymous namespace
49 49
50 // static 50 // static
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
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* user = UserManager::Get()->FindUser(user_id); 154 const user_manager::User* user = UserManager::Get()->FindUser(user_id);
155 if (user && !user->username_hash().empty()) 155 if (user && !user->username_hash().empty())
156 profile_dir = ProfileHelper::GetUserProfileDir(user->username_hash()); 156 profile_dir = ProfileHelper::GetUserProfileDir(user->username_hash());
157 157
158 ProfileManager* profile_manager = g_browser_process->profile_manager(); 158 ProfileManager* profile_manager = g_browser_process->profile_manager();
159 profile_dir = profile_manager->user_data_dir().Append(profile_dir); 159 profile_dir = profile_manager->user_data_dir().Append(profile_dir);
160 160
161 return profile_dir; 161 return profile_dir;
162 } 162 }
163 163
164 // static 164 // static
165 bool ProfileHelper::IsSigninProfile(Profile* profile) { 165 bool ProfileHelper::IsSigninProfile(Profile* profile) {
166 return profile->GetPath().BaseName().value() == chrome::kInitialProfile; 166 return profile->GetPath().BaseName().value() == chrome::kInitialProfile;
167 } 167 }
168 168
169 // static 169 // static
170 bool ProfileHelper::IsOwnerProfile(Profile* profile) { 170 bool ProfileHelper::IsOwnerProfile(Profile* profile) {
171 if (!profile) 171 if (!profile)
172 return false; 172 return false;
173 chromeos::User* user = ProfileHelper::Get()->GetUserByProfile(profile); 173 user_manager::User* user = ProfileHelper::Get()->GetUserByProfile(profile);
174 if (!user) 174 if (!user)
175 return false; 175 return false;
176 176
177 return user->email() == chromeos::UserManager::Get()->GetOwnerEmail(); 177 return user->email() == chromeos::UserManager::Get()->GetOwnerEmail();
178 } 178 }
179 179
180 void ProfileHelper::ProfileStartup(Profile* profile, bool process_startup) { 180 void ProfileHelper::ProfileStartup(Profile* profile, bool process_startup) {
181 // Initialize Chrome OS preferences like touch pad sensitivity. For the 181 // Initialize Chrome OS preferences like touch pad sensitivity. For the
182 // preferences to work in the guest mode, the initialization has to be 182 // preferences to work in the guest mode, the initialization has to be
183 // done after |profile| is switched to the incognito profile (which 183 // done after |profile| is switched to the incognito profile (which
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 return; 217 return;
218 } 218 }
219 signin_profile_clear_requested_ = true; 219 signin_profile_clear_requested_ = true;
220 BrowsingDataRemover* remover = 220 BrowsingDataRemover* remover =
221 BrowsingDataRemover::CreateForUnboundedRange(GetSigninProfile()); 221 BrowsingDataRemover::CreateForUnboundedRange(GetSigninProfile());
222 remover->AddObserver(this); 222 remover->AddObserver(this);
223 remover->Remove(BrowsingDataRemover::REMOVE_SITE_DATA, 223 remover->Remove(BrowsingDataRemover::REMOVE_SITE_DATA,
224 BrowsingDataHelper::ALL); 224 BrowsingDataHelper::ALL);
225 } 225 }
226 226
227 Profile* ProfileHelper::GetProfileByUser(const User* user) { 227 Profile* ProfileHelper::GetProfileByUser(const user_manager::User* user) {
228 // This map is non-empty only in tests. 228 // This map is non-empty only in tests.
229 if (!user_to_profile_for_testing_.empty()) { 229 if (!user_to_profile_for_testing_.empty()) {
230 std::map<const User*, Profile*>::const_iterator it = 230 std::map<const user_manager::User*, Profile*>::const_iterator it =
231 user_to_profile_for_testing_.find(user); 231 user_to_profile_for_testing_.find(user);
232 return it == user_to_profile_for_testing_.end() ? NULL : it->second; 232 return it == user_to_profile_for_testing_.end() ? NULL : it->second;
233 } 233 }
234 234
235 Profile* profile = NULL; 235 Profile* profile = NULL;
236 if (user->is_profile_created()) 236 if (user->is_profile_created())
237 profile = ProfileHelper::GetProfileByUserIdHash(user->username_hash()); 237 profile = ProfileHelper::GetProfileByUserIdHash(user->username_hash());
238 else 238 else
239 profile = ProfileManager::GetActiveUserProfile(); 239 profile = ProfileManager::GetActiveUserProfile();
240 240
241 // GetActiveUserProfile() or GetProfileByUserIdHash() returns a new instance 241 // GetActiveUserProfile() or GetProfileByUserIdHash() returns a new instance
242 // of ProfileImpl(), but actually its OffTheRecordProfile() should be used. 242 // of ProfileImpl(), but actually its OffTheRecordProfile() should be used.
243 if (profile && UserManager::Get()->IsLoggedInAsGuest()) 243 if (profile && UserManager::Get()->IsLoggedInAsGuest())
244 profile = profile->GetOffTheRecordProfile(); 244 profile = profile->GetOffTheRecordProfile();
245 return profile; 245 return profile;
246 } 246 }
247 247
248 User* ProfileHelper::GetUserByProfile(Profile* profile) { 248 user_manager::User* ProfileHelper::GetUserByProfile(Profile* profile) {
249 // This map is non-empty only in tests. 249 // This map is non-empty only in tests.
250 if (enable_profile_to_user_testing || !user_list_for_testing_.empty()) { 250 if (enable_profile_to_user_testing || !user_list_for_testing_.empty()) {
251 if (always_return_primary_user_for_testing) 251 if (always_return_primary_user_for_testing)
252 return const_cast<User*>(UserManager::Get()->GetPrimaryUser()); 252 return const_cast<user_manager::User*>(
253 UserManager::Get()->GetPrimaryUser());
253 254
254 const std::string& user_name = profile->GetProfileName(); 255 const std::string& user_name = profile->GetProfileName();
255 for (UserList::const_iterator it = user_list_for_testing_.begin(); 256 for (user_manager::UserList::const_iterator it =
257 user_list_for_testing_.begin();
256 it != user_list_for_testing_.end(); 258 it != user_list_for_testing_.end();
257 ++it) { 259 ++it) {
258 if ((*it)->email() == user_name) 260 if ((*it)->email() == user_name)
259 return *it; 261 return *it;
260 } 262 }
261 263
262 // In case of test setup we should always default to primary user. 264 // In case of test setup we should always default to primary user.
263 return const_cast<User*>(UserManager::Get()->GetPrimaryUser()); 265 return const_cast<user_manager::User*>(
266 UserManager::Get()->GetPrimaryUser());
264 } 267 }
265 268
266 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 269 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
267 if (ProfileHelper::IsSigninProfile(profile)) 270 if (ProfileHelper::IsSigninProfile(profile))
268 return NULL; 271 return NULL;
269 272
270 UserManager* user_manager = UserManager::Get(); 273 UserManager* user_manager = UserManager::Get();
271 274
272 // Special case for non-CrOS tests that do create several profiles 275 // Special case for non-CrOS tests that do create several profiles
273 // and don't really care about mapping to the real user. 276 // and don't really care about mapping to the real user.
274 // Without multi-profiles on Chrome OS such tests always got active_user_. 277 // Without multi-profiles on Chrome OS such tests always got active_user_.
275 // Now these tests will specify special flag to continue working. 278 // Now these tests will specify special flag to continue working.
276 // In future those tests can get a proper CrOS configuration i.e. register 279 // In future those tests can get a proper CrOS configuration i.e. register
277 // and login several users if they want to work with an additional profile. 280 // and login several users if they want to work with an additional profile.
278 if (CommandLine::ForCurrentProcess()->HasSwitch( 281 if (CommandLine::ForCurrentProcess()->HasSwitch(
279 switches::kIgnoreUserProfileMappingForTests)) { 282 switches::kIgnoreUserProfileMappingForTests)) {
280 return user_manager->GetActiveUser(); 283 return user_manager->GetActiveUser();
281 } 284 }
282 285
283 const std::string username_hash = 286 const std::string username_hash =
284 ProfileHelper::GetUserIdHashFromProfile(profile); 287 ProfileHelper::GetUserIdHashFromProfile(profile);
285 const UserList& users = user_manager->GetUsers(); 288 const user_manager::UserList& users = user_manager->GetUsers();
286 const UserList::const_iterator pos = std::find_if( 289 const user_manager::UserList::const_iterator pos = std::find_if(
287 users.begin(), users.end(), UsernameHashMatcher(username_hash)); 290 users.begin(), users.end(), UsernameHashMatcher(username_hash));
288 if (pos != users.end()) 291 if (pos != users.end())
289 return *pos; 292 return *pos;
290 293
291 // Many tests do not have their users registered with UserManager and 294 // Many tests do not have their users registered with UserManager and
292 // runs here. If |active_user_| matches |profile|, returns it. 295 // runs here. If |active_user_| matches |profile|, returns it.
293 const User* active_user = user_manager->GetActiveUser(); 296 const user_manager::User* active_user = user_manager->GetActiveUser();
294 return active_user && 297 return active_user &&
295 ProfileHelper::GetProfilePathByUserIdHash( 298 ProfileHelper::GetProfilePathByUserIdHash(
296 active_user->username_hash()) == profile->GetPath() 299 active_user->username_hash()) == profile->GetPath()
297 ? const_cast<User*>(active_user) 300 ? const_cast<user_manager::User*>(active_user)
298 : NULL; 301 : NULL;
299 } 302 }
300 303
301 //////////////////////////////////////////////////////////////////////////////// 304 ////////////////////////////////////////////////////////////////////////////////
302 // ProfileHelper, BrowsingDataRemover::Observer implementation: 305 // ProfileHelper, BrowsingDataRemover::Observer implementation:
303 306
304 void ProfileHelper::OnBrowsingDataRemoverDone() { 307 void ProfileHelper::OnBrowsingDataRemoverDone() {
305 signin_profile_clear_requested_ = false; 308 signin_profile_clear_requested_ = false;
306 for (size_t i = 0; i < on_clear_callbacks_.size(); ++i) { 309 for (size_t i = 0; i < on_clear_callbacks_.size(); ++i) {
307 if (!on_clear_callbacks_[i].is_null()) 310 if (!on_clear_callbacks_[i].is_null())
(...skipping 21 matching lines...) Expand all
329 332
330 //////////////////////////////////////////////////////////////////////////////// 333 ////////////////////////////////////////////////////////////////////////////////
331 // ProfileHelper, UserManager::UserSessionStateObserver implementation: 334 // ProfileHelper, UserManager::UserSessionStateObserver implementation:
332 335
333 void ProfileHelper::ActiveUserHashChanged(const std::string& hash) { 336 void ProfileHelper::ActiveUserHashChanged(const std::string& hash) {
334 active_user_id_hash_ = hash; 337 active_user_id_hash_ = hash;
335 base::FilePath profile_path = GetProfilePathByUserIdHash(hash); 338 base::FilePath profile_path = GetProfilePathByUserIdHash(hash);
336 VLOG(1) << "Switching to profile path: " << profile_path.value(); 339 VLOG(1) << "Switching to profile path: " << profile_path.value();
337 } 340 }
338 341
339 void ProfileHelper::SetProfileToUserMappingForTesting(User* user) { 342 void ProfileHelper::SetProfileToUserMappingForTesting(
343 user_manager::User* user) {
340 user_list_for_testing_.push_back(user); 344 user_list_for_testing_.push_back(user);
341 } 345 }
342 346
343 // static 347 // static
344 void ProfileHelper::SetProfileToUserForTestingEnabled(bool enabled) { 348 void ProfileHelper::SetProfileToUserForTestingEnabled(bool enabled) {
345 enable_profile_to_user_testing = enabled; 349 enable_profile_to_user_testing = enabled;
346 } 350 }
347 351
348 // static 352 // static
349 void ProfileHelper::SetAlwaysReturnPrimaryUserForTesting(bool value) { 353 void ProfileHelper::SetAlwaysReturnPrimaryUserForTesting(bool value) {
350 always_return_primary_user_for_testing = true; 354 always_return_primary_user_for_testing = true;
351 ProfileHelper::SetProfileToUserForTestingEnabled(true); 355 ProfileHelper::SetProfileToUserForTestingEnabled(true);
352 } 356 }
353 357
354 void ProfileHelper::SetUserToProfileMappingForTesting(const User* user, 358 void ProfileHelper::SetUserToProfileMappingForTesting(
355 Profile* profile) { 359 const user_manager::User* user,
360 Profile* profile) {
356 user_to_profile_for_testing_[user] = profile; 361 user_to_profile_for_testing_[user] = profile;
357 } 362 }
358 363
359 } // namespace chromeos 364 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/profiles/profile_helper.h ('k') | chrome/browser/chromeos/profiles/profile_list_chromeos.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698