OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/login/user_manager.h" | 5 #include "chrome/browser/chromeos/login/user_manager.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
58 using content::BrowserThread; | 58 using content::BrowserThread; |
59 | 59 |
60 namespace chromeos { | 60 namespace chromeos { |
61 | 61 |
62 namespace { | 62 namespace { |
63 | 63 |
64 // A vector pref of the users who have logged into the device. | 64 // A vector pref of the users who have logged into the device. |
65 const char kLoggedInUsers[] = "LoggedInUsers"; | 65 const char kLoggedInUsers[] = "LoggedInUsers"; |
66 // A dictionary that maps usernames to file paths to their images. | 66 // A dictionary that maps usernames to file paths to their images. |
67 const char kUserImages[] = "UserImages"; | 67 const char kUserImages[] = "UserImages"; |
68 // A dictionary that maps usernames to the displayed (non-canonical) emails. | |
69 const char kUserDisplayEmail[] = "UserDisplayEmail"; | |
68 // A dictionary that maps usernames to OAuth token presence flag. | 70 // A dictionary that maps usernames to OAuth token presence flag. |
69 const char kUserOAuthTokenStatus[] = "OAuthTokenStatus"; | 71 const char kUserOAuthTokenStatus[] = "OAuthTokenStatus"; |
70 | 72 |
71 // Incognito user is represented by an empty string (since some code already | 73 // Incognito user is represented by an empty string (since some code already |
72 // depends on that and it's hard to figure out what). | 74 // depends on that and it's hard to figure out what). |
73 const char kGuestUser[] = ""; | 75 const char kGuestUser[] = ""; |
74 | 76 |
75 // Stub user email (for test paths). | 77 // Stub user email (for test paths). |
76 const char kStubUser[] = "stub-user@example.com"; | 78 const char kStubUser[] = "stub-user@example.com"; |
77 | 79 |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
219 return &g_user_manager.Get(); | 221 return &g_user_manager.Get(); |
220 } | 222 } |
221 | 223 |
222 // static | 224 // static |
223 void UserManager::RegisterPrefs(PrefService* local_state) { | 225 void UserManager::RegisterPrefs(PrefService* local_state) { |
224 local_state->RegisterListPref(kLoggedInUsers, PrefService::UNSYNCABLE_PREF); | 226 local_state->RegisterListPref(kLoggedInUsers, PrefService::UNSYNCABLE_PREF); |
225 local_state->RegisterDictionaryPref(kUserImages, | 227 local_state->RegisterDictionaryPref(kUserImages, |
226 PrefService::UNSYNCABLE_PREF); | 228 PrefService::UNSYNCABLE_PREF); |
227 local_state->RegisterDictionaryPref(kUserOAuthTokenStatus, | 229 local_state->RegisterDictionaryPref(kUserOAuthTokenStatus, |
228 PrefService::UNSYNCABLE_PREF); | 230 PrefService::UNSYNCABLE_PREF); |
231 local_state->RegisterDictionaryPref(kUserDisplayEmail, | |
232 PrefService::UNSYNCABLE_PREF); | |
229 } | 233 } |
230 | 234 |
231 const UserList& UserManager::GetUsers() const { | 235 const UserList& UserManager::GetUsers() const { |
232 const_cast<UserManager*>(this)->EnsureUsersLoaded(); | 236 const_cast<UserManager*>(this)->EnsureUsersLoaded(); |
233 return users_; | 237 return users_; |
234 } | 238 } |
235 | 239 |
236 void UserManager::UserLoggedIn(const std::string& email) { | 240 void UserManager::UserLoggedIn(const std::string& email) { |
237 DCHECK(!user_is_logged_in_); | 241 DCHECK(!user_is_logged_in_); |
238 | 242 |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
353 DictionaryPrefUpdate prefs_images_update(prefs, kUserImages); | 357 DictionaryPrefUpdate prefs_images_update(prefs, kUserImages); |
354 std::string image_path_string; | 358 std::string image_path_string; |
355 prefs_images_update->GetStringWithoutPathExpansion(email, &image_path_string); | 359 prefs_images_update->GetStringWithoutPathExpansion(email, &image_path_string); |
356 prefs_images_update->RemoveWithoutPathExpansion(email, NULL); | 360 prefs_images_update->RemoveWithoutPathExpansion(email, NULL); |
357 | 361 |
358 DictionaryPrefUpdate prefs_oauth_update(prefs, kUserOAuthTokenStatus); | 362 DictionaryPrefUpdate prefs_oauth_update(prefs, kUserOAuthTokenStatus); |
359 int oauth_status; | 363 int oauth_status; |
360 prefs_oauth_update->GetIntegerWithoutPathExpansion(email, &oauth_status); | 364 prefs_oauth_update->GetIntegerWithoutPathExpansion(email, &oauth_status); |
361 prefs_oauth_update->RemoveWithoutPathExpansion(email, NULL); | 365 prefs_oauth_update->RemoveWithoutPathExpansion(email, NULL); |
362 | 366 |
367 DictionaryPrefUpdate prefs_display_email_update(prefs, kUserDisplayEmail); | |
368 prefs_display_email_update->RemoveWithoutPathExpansion(email, NULL); | |
369 | |
363 prefs->ScheduleSavePersistentPrefs(); | 370 prefs->ScheduleSavePersistentPrefs(); |
Nikita (slow)
2011/12/05 13:13:04
nit: Cleanup, this call could be removed.
Ivan Korotkov
2011/12/05 14:12:48
Done.
| |
364 | 371 |
365 if (user_to_remove != users_.end()) { | 372 if (user_to_remove != users_.end()) { |
366 --display_name_count_[(*user_to_remove)->GetDisplayName()]; | 373 --display_name_count_[(*user_to_remove)->GetDisplayName()]; |
367 delete *user_to_remove; | 374 delete *user_to_remove; |
368 users_.erase(user_to_remove); | 375 users_.erase(user_to_remove); |
369 } | 376 } |
370 | 377 |
371 int default_image_id = User::kInvalidImageIndex; | 378 int default_image_id = User::kInvalidImageIndex; |
372 if (!image_path_string.empty() && | 379 if (!image_path_string.empty() && |
373 !IsDefaultImagePath(image_path_string, &default_image_id)) { | 380 !IsDefaultImagePath(image_path_string, &default_image_id)) { |
(...skipping 28 matching lines...) Expand all Loading... | |
402 } | 409 } |
403 | 410 |
404 void UserManager::SaveUserOAuthStatus( | 411 void UserManager::SaveUserOAuthStatus( |
405 const std::string& username, | 412 const std::string& username, |
406 User::OAuthTokenStatus oauth_token_status) { | 413 User::OAuthTokenStatus oauth_token_status) { |
407 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 414 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
408 PrefService* local_state = g_browser_process->local_state(); | 415 PrefService* local_state = g_browser_process->local_state(); |
409 DictionaryPrefUpdate oauth_status_update(local_state, kUserOAuthTokenStatus); | 416 DictionaryPrefUpdate oauth_status_update(local_state, kUserOAuthTokenStatus); |
410 oauth_status_update->SetWithoutPathExpansion(username, | 417 oauth_status_update->SetWithoutPathExpansion(username, |
411 new base::FundamentalValue(static_cast<int>(oauth_token_status))); | 418 new base::FundamentalValue(static_cast<int>(oauth_token_status))); |
412 DVLOG(1) << "Saving user OAuth token status in Local State."; | 419 DVLOG(1) << "Saving user OAuth token status in Local State"; |
413 local_state->ScheduleSavePersistentPrefs(); | 420 local_state->ScheduleSavePersistentPrefs(); |
Nikita (slow)
2011/12/05 13:13:04
nit: Cleanup, this call could be removed.
Ivan Korotkov
2011/12/05 14:12:48
Done.
| |
414 User* user = const_cast<User*>(FindUser(username)); | 421 User* user = const_cast<User*>(FindUser(username)); |
415 if (user) | 422 if (user) |
416 user->set_oauth_token_status(oauth_token_status); | 423 user->set_oauth_token_status(oauth_token_status); |
417 } | 424 } |
418 | 425 |
419 User::OAuthTokenStatus UserManager::GetUserOAuthStatus( | 426 User::OAuthTokenStatus UserManager::LoadUserOAuthStatus( |
420 const std::string& username) const { | 427 const std::string& username) const { |
421 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 428 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
422 | 429 |
423 if (CommandLine::ForCurrentProcess()->HasSwitch( | 430 if (CommandLine::ForCurrentProcess()->HasSwitch( |
424 switches::kSkipOAuthLogin)) { | 431 switches::kSkipOAuthLogin)) { |
425 // Use OAUTH_TOKEN_STATUS_VALID flag if kSkipOAuthLogin is present. | 432 // Use OAUTH_TOKEN_STATUS_VALID flag if kSkipOAuthLogin is present. |
426 return User::OAUTH_TOKEN_STATUS_VALID; | 433 return User::OAUTH_TOKEN_STATUS_VALID; |
427 } else { | 434 } else { |
428 PrefService* local_state = g_browser_process->local_state(); | 435 PrefService* local_state = g_browser_process->local_state(); |
429 const DictionaryValue* prefs_oauth_status = | 436 const DictionaryValue* prefs_oauth_status = |
430 local_state->GetDictionary(kUserOAuthTokenStatus); | 437 local_state->GetDictionary(kUserOAuthTokenStatus); |
431 | 438 |
432 int oauth_token_status = User::OAUTH_TOKEN_STATUS_UNKNOWN; | 439 int oauth_token_status = User::OAUTH_TOKEN_STATUS_UNKNOWN; |
433 if (prefs_oauth_status && | 440 if (prefs_oauth_status && |
434 prefs_oauth_status->GetIntegerWithoutPathExpansion(username, | 441 prefs_oauth_status->GetIntegerWithoutPathExpansion(username, |
435 &oauth_token_status)) { | 442 &oauth_token_status)) { |
436 return static_cast<User::OAuthTokenStatus>(oauth_token_status); | 443 return static_cast<User::OAuthTokenStatus>(oauth_token_status); |
437 } | 444 } |
438 } | 445 } |
439 | 446 |
440 return User::OAUTH_TOKEN_STATUS_UNKNOWN; | 447 return User::OAUTH_TOKEN_STATUS_UNKNOWN; |
441 } | 448 } |
442 | 449 |
450 void UserManager::SaveUserDisplayEmail(const std::string& username, | |
451 const std::string& display_email, | |
452 bool update_if_exists) { | |
453 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
454 PrefService* local_state = g_browser_process->local_state(); | |
455 | |
456 if (!update_if_exists) { | |
457 // Check if a display name is already known for that username. | |
458 std::string display_email; | |
459 const DictionaryValue* prefs_display_email = | |
460 local_state->GetDictionary(kUserDisplayEmail); | |
461 if (prefs_display_email && | |
462 prefs_display_email->GetStringWithoutPathExpansion( | |
463 username, &display_email)) | |
464 return; | |
465 } | |
466 | |
467 DictionaryPrefUpdate display_email_update(local_state, kUserDisplayEmail); | |
468 display_email_update->SetWithoutPathExpansion( | |
469 username, | |
470 base::Value::CreateStringValue(display_email)); | |
471 DVLOG(1) << "Saving displayed email in Local State"; | |
Nikita (slow)
2011/12/05 13:13:04
nit: cleanup log.
Ivan Korotkov
2011/12/05 14:12:48
Done.
| |
472 local_state->ScheduleSavePersistentPrefs(); | |
Nikita (slow)
2011/12/05 13:13:04
Please remove since prefs are automatically schedu
Ivan Korotkov
2011/12/05 14:12:48
Done.
| |
473 | |
474 User* user = const_cast<User*>(FindUser(username)); | |
475 if (user) | |
476 user->set_display_email(display_email); | |
477 } | |
478 | |
479 std::string UserManager::GetUserDisplayEmail( | |
480 const std::string& username) const { | |
481 // TODO(ivankr): won't work with users that are not registered but present | |
Nikita (slow)
2011/12/05 13:13:04
Is this only for whitelist? We may as well ignore
Ivan Korotkov
2011/12/05 14:12:48
Yes, it's for whitelist only. I'll leave a TODO fo
| |
482 // in |kUserDisplayEmail| dictionary. | |
483 const User* user = FindUser(username); | |
484 return user ? user->display_email() : username; | |
485 } | |
486 | |
443 void UserManager::SaveUserDefaultImageIndex(const std::string& username, | 487 void UserManager::SaveUserDefaultImageIndex(const std::string& username, |
444 int image_index) { | 488 int image_index) { |
445 DCHECK(image_index >= 0 && image_index < kDefaultImagesCount); | 489 DCHECK(image_index >= 0 && image_index < kDefaultImagesCount); |
446 SetUserImage(username, image_index, GetDefaultImage(image_index)); | 490 SetUserImage(username, image_index, GetDefaultImage(image_index)); |
447 SaveImageToLocalState(username, "", image_index, false); | 491 SaveImageToLocalState(username, "", image_index, false); |
448 } | 492 } |
449 | 493 |
450 void UserManager::SaveUserImage(const std::string& username, | 494 void UserManager::SaveUserImage(const std::string& username, |
451 const SkBitmap& image) { | 495 const SkBitmap& image) { |
452 SaveUserImageInternal(username, User::kExternalImageIndex, image); | 496 SaveUserImageInternal(username, User::kExternalImageIndex, image); |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
560 void UserManager::EnsureUsersLoaded() { | 604 void UserManager::EnsureUsersLoaded() { |
561 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 605 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
562 if (!users_.empty()) | 606 if (!users_.empty()) |
563 return; | 607 return; |
564 if (!g_browser_process) | 608 if (!g_browser_process) |
565 return; | 609 return; |
566 | 610 |
567 PrefService* local_state = g_browser_process->local_state(); | 611 PrefService* local_state = g_browser_process->local_state(); |
568 const ListValue* prefs_users = local_state->GetList(kLoggedInUsers); | 612 const ListValue* prefs_users = local_state->GetList(kLoggedInUsers); |
569 const DictionaryValue* prefs_images = local_state->GetDictionary(kUserImages); | 613 const DictionaryValue* prefs_images = local_state->GetDictionary(kUserImages); |
614 const DictionaryValue* prefs_display_emails = | |
615 local_state->GetDictionary(kUserDisplayEmail); | |
570 | 616 |
571 if (prefs_users) { | 617 if (prefs_users) { |
572 for (ListValue::const_iterator it = prefs_users->begin(); | 618 for (ListValue::const_iterator it = prefs_users->begin(); |
573 it != prefs_users->end(); ++it) { | 619 it != prefs_users->end(); ++it) { |
574 std::string email; | 620 std::string email; |
575 if ((*it)->GetAsString(&email)) { | 621 if ((*it)->GetAsString(&email)) { |
576 User* user = CreateUser(email); | 622 User* user = CreateUser(email); |
577 users_.push_back(user); | 623 users_.push_back(user); |
578 | 624 |
579 if (prefs_images) { | 625 if (prefs_images) { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
623 image_loader_->Start( | 669 image_loader_->Start( |
624 image_path, 0, | 670 image_path, 0, |
625 base::Bind(&UserManager::SetUserImage, | 671 base::Bind(&UserManager::SetUserImage, |
626 base::Unretained(this), email, image_index)); | 672 base::Unretained(this), email, image_index)); |
627 } | 673 } |
628 } else { | 674 } else { |
629 NOTREACHED(); | 675 NOTREACHED(); |
630 } | 676 } |
631 } | 677 } |
632 } | 678 } |
679 | |
680 std::string display_email; | |
681 if (prefs_display_emails && | |
682 prefs_display_emails->GetStringWithoutPathExpansion( | |
683 email, &display_email)) { | |
684 user->set_display_email(display_email); | |
685 } | |
633 } | 686 } |
634 } | 687 } |
635 } | 688 } |
636 } | 689 } |
637 | 690 |
638 void UserManager::StubUserLoggedIn() { | 691 void UserManager::StubUserLoggedIn() { |
639 logged_in_user_ = &stub_user_; | 692 logged_in_user_ = &stub_user_; |
640 stub_user_.SetImage(GetDefaultImage(kStubDefaultImageIndex), | 693 stub_user_.SetImage(GetDefaultImage(kStubDefaultImageIndex), |
641 kStubDefaultImageIndex); | 694 kStubDefaultImageIndex); |
642 } | 695 } |
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
903 chrome::NOTIFICATION_PROFILE_IMAGE_UPDATE_FAILED, | 956 chrome::NOTIFICATION_PROFILE_IMAGE_UPDATE_FAILED, |
904 content::Source<UserManager>(this), | 957 content::Source<UserManager>(this), |
905 content::NotificationService::NoDetails()); | 958 content::NotificationService::NoDetails()); |
906 } | 959 } |
907 | 960 |
908 profile_image_downloader_.reset(); | 961 profile_image_downloader_.reset(); |
909 } | 962 } |
910 | 963 |
911 User* UserManager::CreateUser(const std::string& email) const { | 964 User* UserManager::CreateUser(const std::string& email) const { |
912 User* user = new User(email); | 965 User* user = new User(email); |
913 user->set_oauth_token_status(GetUserOAuthStatus(email)); | 966 user->set_oauth_token_status(LoadUserOAuthStatus(email)); |
914 // Used to determine whether user's display name is unique. | 967 // Used to determine whether user's display name is unique. |
915 ++display_name_count_[user->GetDisplayName()]; | 968 ++display_name_count_[user->GetDisplayName()]; |
916 return user; | 969 return user; |
917 } | 970 } |
918 | 971 |
919 } // namespace chromeos | 972 } // namespace chromeos |
OLD | NEW |