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 15 matching lines...) Expand all Loading... |
26 #include "chrome/browser/browser_process.h" | 26 #include "chrome/browser/browser_process.h" |
27 #include "chrome/browser/chromeos/cros_settings.h" | 27 #include "chrome/browser/chromeos/cros_settings.h" |
28 #include "chrome/browser/chromeos/cros/cert_library.h" | 28 #include "chrome/browser/chromeos/cros/cert_library.h" |
29 #include "chrome/browser/chromeos/cros/cros_library.h" | 29 #include "chrome/browser/chromeos/cros/cros_library.h" |
30 #include "chrome/browser/chromeos/cros/cryptohome_library.h" | 30 #include "chrome/browser/chromeos/cros/cryptohome_library.h" |
31 #include "chrome/browser/chromeos/input_method/input_method_manager.h" | 31 #include "chrome/browser/chromeos/input_method/input_method_manager.h" |
32 #include "chrome/browser/chromeos/login/default_user_images.h" | 32 #include "chrome/browser/chromeos/login/default_user_images.h" |
33 #include "chrome/browser/chromeos/login/helper.h" | 33 #include "chrome/browser/chromeos/login/helper.h" |
34 #include "chrome/browser/chromeos/login/login_display.h" | 34 #include "chrome/browser/chromeos/login/login_display.h" |
35 #include "chrome/browser/chromeos/login/ownership_service.h" | 35 #include "chrome/browser/chromeos/login/ownership_service.h" |
| 36 #include "chrome/browser/chromeos/login/remove_user_delegate.h" |
36 #include "chrome/browser/chromeos/system/runtime_environment.h" | 37 #include "chrome/browser/chromeos/system/runtime_environment.h" |
37 #include "chrome/browser/prefs/pref_service.h" | 38 #include "chrome/browser/prefs/pref_service.h" |
38 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 39 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
39 #include "chrome/browser/profiles/profile_downloader.h" | 40 #include "chrome/browser/profiles/profile_downloader.h" |
40 #include "chrome/browser/profiles/profile_manager.h" | 41 #include "chrome/browser/profiles/profile_manager.h" |
41 #include "chrome/browser/ui/webui/web_ui_util.h" | 42 #include "chrome/browser/ui/webui/web_ui_util.h" |
42 #include "chrome/common/chrome_notification_types.h" | 43 #include "chrome/common/chrome_notification_types.h" |
43 #include "chrome/common/chrome_paths.h" | 44 #include "chrome/common/chrome_paths.h" |
44 #include "chrome/common/chrome_switches.h" | 45 #include "chrome/common/chrome_switches.h" |
45 #include "content/public/browser/browser_thread.h" | 46 #include "content/public/browser/browser_thread.h" |
(...skipping 11 matching lines...) Expand all Loading... |
57 using content::BrowserThread; | 58 using content::BrowserThread; |
58 | 59 |
59 namespace chromeos { | 60 namespace chromeos { |
60 | 61 |
61 namespace { | 62 namespace { |
62 | 63 |
63 // 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. |
64 const char kLoggedInUsers[] = "LoggedInUsers"; | 65 const char kLoggedInUsers[] = "LoggedInUsers"; |
65 // A dictionary that maps usernames to file paths to their images. | 66 // A dictionary that maps usernames to file paths to their images. |
66 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"; |
67 // A dictionary that maps usernames to OAuth token presence flag. | 70 // A dictionary that maps usernames to OAuth token presence flag. |
68 const char kUserOAuthTokenStatus[] = "OAuthTokenStatus"; | 71 const char kUserOAuthTokenStatus[] = "OAuthTokenStatus"; |
69 | 72 |
70 // 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 |
71 // depends on that and it's hard to figure out what). | 74 // depends on that and it's hard to figure out what). |
72 const char kGuestUser[] = ""; | 75 const char kGuestUser[] = ""; |
73 | 76 |
74 // Stub user email (for test paths). | 77 // Stub user email (for test paths). |
75 const char kStubUser[] = "stub-user@example.com"; | 78 const char kStubUser[] = "stub-user@example.com"; |
76 | 79 |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 return &g_user_manager.Get(); | 221 return &g_user_manager.Get(); |
219 } | 222 } |
220 | 223 |
221 // static | 224 // static |
222 void UserManager::RegisterPrefs(PrefService* local_state) { | 225 void UserManager::RegisterPrefs(PrefService* local_state) { |
223 local_state->RegisterListPref(kLoggedInUsers, PrefService::UNSYNCABLE_PREF); | 226 local_state->RegisterListPref(kLoggedInUsers, PrefService::UNSYNCABLE_PREF); |
224 local_state->RegisterDictionaryPref(kUserImages, | 227 local_state->RegisterDictionaryPref(kUserImages, |
225 PrefService::UNSYNCABLE_PREF); | 228 PrefService::UNSYNCABLE_PREF); |
226 local_state->RegisterDictionaryPref(kUserOAuthTokenStatus, | 229 local_state->RegisterDictionaryPref(kUserOAuthTokenStatus, |
227 PrefService::UNSYNCABLE_PREF); | 230 PrefService::UNSYNCABLE_PREF); |
| 231 local_state->RegisterDictionaryPref(kUserDisplayEmail, |
| 232 PrefService::UNSYNCABLE_PREF); |
228 } | 233 } |
229 | 234 |
230 const UserList& UserManager::GetUsers() const { | 235 const UserList& UserManager::GetUsers() const { |
231 const_cast<UserManager*>(this)->EnsureUsersLoaded(); | 236 const_cast<UserManager*>(this)->EnsureUsersLoaded(); |
232 return users_; | 237 return users_; |
233 } | 238 } |
234 | 239 |
235 void UserManager::UserLoggedIn(const std::string& email) { | 240 void UserManager::UserLoggedIn(const std::string& email) { |
236 DCHECK(!user_is_logged_in_); | 241 DCHECK(!user_is_logged_in_); |
237 | 242 |
(...skipping 15 matching lines...) Expand all Loading... |
253 prefs_users_update->Append(Value::CreateStringValue(email)); | 258 prefs_users_update->Append(Value::CreateStringValue(email)); |
254 UserList::iterator logged_in_user = users_.end(); | 259 UserList::iterator logged_in_user = users_.end(); |
255 for (UserList::iterator it = users_.begin(); it != users_.end(); ++it) { | 260 for (UserList::iterator it = users_.begin(); it != users_.end(); ++it) { |
256 std::string user_email = (*it)->email(); | 261 std::string user_email = (*it)->email(); |
257 // Skip the most recent user. | 262 // Skip the most recent user. |
258 if (email != user_email) | 263 if (email != user_email) |
259 prefs_users_update->Append(Value::CreateStringValue(user_email)); | 264 prefs_users_update->Append(Value::CreateStringValue(user_email)); |
260 else | 265 else |
261 logged_in_user = it; | 266 logged_in_user = it; |
262 } | 267 } |
263 prefs->ScheduleSavePersistentPrefs(); | |
264 | 268 |
265 if (logged_in_user == users_.end()) { | 269 if (logged_in_user == users_.end()) { |
266 current_user_is_new_ = true; | 270 current_user_is_new_ = true; |
267 logged_in_user_ = CreateUser(email); | 271 logged_in_user_ = CreateUser(email); |
268 } else { | 272 } else { |
269 logged_in_user_ = *logged_in_user; | 273 logged_in_user_ = *logged_in_user; |
270 users_.erase(logged_in_user); | 274 users_.erase(logged_in_user); |
271 } | 275 } |
272 // This user must be in the front of the user list. | 276 // This user must be in the front of the user list. |
273 users_.insert(users_.begin(), logged_in_user_); | 277 users_.insert(users_.begin(), logged_in_user_); |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 DictionaryPrefUpdate prefs_images_update(prefs, kUserImages); | 356 DictionaryPrefUpdate prefs_images_update(prefs, kUserImages); |
353 std::string image_path_string; | 357 std::string image_path_string; |
354 prefs_images_update->GetStringWithoutPathExpansion(email, &image_path_string); | 358 prefs_images_update->GetStringWithoutPathExpansion(email, &image_path_string); |
355 prefs_images_update->RemoveWithoutPathExpansion(email, NULL); | 359 prefs_images_update->RemoveWithoutPathExpansion(email, NULL); |
356 | 360 |
357 DictionaryPrefUpdate prefs_oauth_update(prefs, kUserOAuthTokenStatus); | 361 DictionaryPrefUpdate prefs_oauth_update(prefs, kUserOAuthTokenStatus); |
358 int oauth_status; | 362 int oauth_status; |
359 prefs_oauth_update->GetIntegerWithoutPathExpansion(email, &oauth_status); | 363 prefs_oauth_update->GetIntegerWithoutPathExpansion(email, &oauth_status); |
360 prefs_oauth_update->RemoveWithoutPathExpansion(email, NULL); | 364 prefs_oauth_update->RemoveWithoutPathExpansion(email, NULL); |
361 | 365 |
362 prefs->ScheduleSavePersistentPrefs(); | 366 DictionaryPrefUpdate prefs_display_email_update(prefs, kUserDisplayEmail); |
| 367 prefs_display_email_update->RemoveWithoutPathExpansion(email, NULL); |
363 | 368 |
364 if (user_to_remove != users_.end()) { | 369 if (user_to_remove != users_.end()) { |
365 --display_name_count_[(*user_to_remove)->GetDisplayName()]; | 370 --display_name_count_[(*user_to_remove)->GetDisplayName()]; |
366 delete *user_to_remove; | 371 delete *user_to_remove; |
367 users_.erase(user_to_remove); | 372 users_.erase(user_to_remove); |
368 } | 373 } |
369 | 374 |
370 int default_image_id = User::kInvalidImageIndex; | 375 int default_image_id = User::kInvalidImageIndex; |
371 if (!image_path_string.empty() && | 376 if (!image_path_string.empty() && |
372 !IsDefaultImagePath(image_path_string, &default_image_id)) { | 377 !IsDefaultImagePath(image_path_string, &default_image_id)) { |
(...skipping 28 matching lines...) Expand all Loading... |
401 } | 406 } |
402 | 407 |
403 void UserManager::SaveUserOAuthStatus( | 408 void UserManager::SaveUserOAuthStatus( |
404 const std::string& username, | 409 const std::string& username, |
405 User::OAuthTokenStatus oauth_token_status) { | 410 User::OAuthTokenStatus oauth_token_status) { |
406 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 411 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
407 PrefService* local_state = g_browser_process->local_state(); | 412 PrefService* local_state = g_browser_process->local_state(); |
408 DictionaryPrefUpdate oauth_status_update(local_state, kUserOAuthTokenStatus); | 413 DictionaryPrefUpdate oauth_status_update(local_state, kUserOAuthTokenStatus); |
409 oauth_status_update->SetWithoutPathExpansion(username, | 414 oauth_status_update->SetWithoutPathExpansion(username, |
410 new base::FundamentalValue(static_cast<int>(oauth_token_status))); | 415 new base::FundamentalValue(static_cast<int>(oauth_token_status))); |
411 DVLOG(1) << "Saving user OAuth token status in Local State."; | 416 DVLOG(1) << "Saving user OAuth token status in Local State"; |
412 local_state->ScheduleSavePersistentPrefs(); | |
413 User* user = const_cast<User*>(FindUser(username)); | 417 User* user = const_cast<User*>(FindUser(username)); |
414 if (user) | 418 if (user) |
415 user->set_oauth_token_status(oauth_token_status); | 419 user->set_oauth_token_status(oauth_token_status); |
416 } | 420 } |
417 | 421 |
418 User::OAuthTokenStatus UserManager::GetUserOAuthStatus( | 422 User::OAuthTokenStatus UserManager::LoadUserOAuthStatus( |
419 const std::string& username) const { | 423 const std::string& username) const { |
420 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 424 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
421 | 425 |
422 if (CommandLine::ForCurrentProcess()->HasSwitch( | 426 if (CommandLine::ForCurrentProcess()->HasSwitch( |
423 switches::kSkipOAuthLogin)) { | 427 switches::kSkipOAuthLogin)) { |
424 // Use OAUTH_TOKEN_STATUS_VALID flag if kSkipOAuthLogin is present. | 428 // Use OAUTH_TOKEN_STATUS_VALID flag if kSkipOAuthLogin is present. |
425 return User::OAUTH_TOKEN_STATUS_VALID; | 429 return User::OAUTH_TOKEN_STATUS_VALID; |
426 } else { | 430 } else { |
427 PrefService* local_state = g_browser_process->local_state(); | 431 PrefService* local_state = g_browser_process->local_state(); |
428 const DictionaryValue* prefs_oauth_status = | 432 const DictionaryValue* prefs_oauth_status = |
429 local_state->GetDictionary(kUserOAuthTokenStatus); | 433 local_state->GetDictionary(kUserOAuthTokenStatus); |
430 | 434 |
431 int oauth_token_status = User::OAUTH_TOKEN_STATUS_UNKNOWN; | 435 int oauth_token_status = User::OAUTH_TOKEN_STATUS_UNKNOWN; |
432 if (prefs_oauth_status && | 436 if (prefs_oauth_status && |
433 prefs_oauth_status->GetIntegerWithoutPathExpansion(username, | 437 prefs_oauth_status->GetIntegerWithoutPathExpansion(username, |
434 &oauth_token_status)) { | 438 &oauth_token_status)) { |
435 return static_cast<User::OAuthTokenStatus>(oauth_token_status); | 439 return static_cast<User::OAuthTokenStatus>(oauth_token_status); |
436 } | 440 } |
437 } | 441 } |
438 | 442 |
439 return User::OAUTH_TOKEN_STATUS_UNKNOWN; | 443 return User::OAUTH_TOKEN_STATUS_UNKNOWN; |
440 } | 444 } |
441 | 445 |
| 446 void UserManager::SaveUserDisplayEmail(const std::string& username, |
| 447 const std::string& display_email) { |
| 448 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 449 |
| 450 User* user = const_cast<User*>(FindUser(username)); |
| 451 if (!user) |
| 452 return; // Ignore if there is no such user. |
| 453 |
| 454 user->set_display_email(display_email); |
| 455 |
| 456 PrefService* local_state = g_browser_process->local_state(); |
| 457 |
| 458 DictionaryPrefUpdate display_email_update(local_state, kUserDisplayEmail); |
| 459 display_email_update->SetWithoutPathExpansion( |
| 460 username, |
| 461 base::Value::CreateStringValue(display_email)); |
| 462 } |
| 463 |
| 464 std::string UserManager::GetUserDisplayEmail( |
| 465 const std::string& username) const { |
| 466 const User* user = FindUser(username); |
| 467 return user ? user->display_email() : username; |
| 468 } |
| 469 |
442 void UserManager::SaveUserDefaultImageIndex(const std::string& username, | 470 void UserManager::SaveUserDefaultImageIndex(const std::string& username, |
443 int image_index) { | 471 int image_index) { |
444 DCHECK(image_index >= 0 && image_index < kDefaultImagesCount); | 472 DCHECK(image_index >= 0 && image_index < kDefaultImagesCount); |
445 SetUserImage(username, image_index, GetDefaultImage(image_index)); | 473 SetUserImage(username, image_index, GetDefaultImage(image_index)); |
446 SaveImageToLocalState(username, "", image_index, false); | 474 SaveImageToLocalState(username, "", image_index, false); |
447 } | 475 } |
448 | 476 |
449 void UserManager::SaveUserImage(const std::string& username, | 477 void UserManager::SaveUserImage(const std::string& username, |
450 const SkBitmap& image) { | 478 const SkBitmap& image) { |
451 SaveUserImageInternal(username, User::kExternalImageIndex, image); | 479 SaveUserImageInternal(username, User::kExternalImageIndex, image); |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
559 void UserManager::EnsureUsersLoaded() { | 587 void UserManager::EnsureUsersLoaded() { |
560 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 588 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
561 if (!users_.empty()) | 589 if (!users_.empty()) |
562 return; | 590 return; |
563 if (!g_browser_process) | 591 if (!g_browser_process) |
564 return; | 592 return; |
565 | 593 |
566 PrefService* local_state = g_browser_process->local_state(); | 594 PrefService* local_state = g_browser_process->local_state(); |
567 const ListValue* prefs_users = local_state->GetList(kLoggedInUsers); | 595 const ListValue* prefs_users = local_state->GetList(kLoggedInUsers); |
568 const DictionaryValue* prefs_images = local_state->GetDictionary(kUserImages); | 596 const DictionaryValue* prefs_images = local_state->GetDictionary(kUserImages); |
| 597 const DictionaryValue* prefs_display_emails = |
| 598 local_state->GetDictionary(kUserDisplayEmail); |
569 | 599 |
570 if (prefs_users) { | 600 if (prefs_users) { |
571 for (ListValue::const_iterator it = prefs_users->begin(); | 601 for (ListValue::const_iterator it = prefs_users->begin(); |
572 it != prefs_users->end(); ++it) { | 602 it != prefs_users->end(); ++it) { |
573 std::string email; | 603 std::string email; |
574 if ((*it)->GetAsString(&email)) { | 604 if ((*it)->GetAsString(&email)) { |
575 User* user = CreateUser(email); | 605 User* user = CreateUser(email); |
576 users_.push_back(user); | 606 users_.push_back(user); |
577 | 607 |
578 if (prefs_images) { | 608 if (prefs_images) { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
622 image_loader_->Start( | 652 image_loader_->Start( |
623 image_path, 0, | 653 image_path, 0, |
624 base::Bind(&UserManager::SetUserImage, | 654 base::Bind(&UserManager::SetUserImage, |
625 base::Unretained(this), email, image_index)); | 655 base::Unretained(this), email, image_index)); |
626 } | 656 } |
627 } else { | 657 } else { |
628 NOTREACHED(); | 658 NOTREACHED(); |
629 } | 659 } |
630 } | 660 } |
631 } | 661 } |
| 662 |
| 663 std::string display_email; |
| 664 if (prefs_display_emails && |
| 665 prefs_display_emails->GetStringWithoutPathExpansion( |
| 666 email, &display_email)) { |
| 667 user->set_display_email(display_email); |
| 668 } |
632 } | 669 } |
633 } | 670 } |
634 } | 671 } |
635 } | 672 } |
636 | 673 |
637 void UserManager::StubUserLoggedIn() { | 674 void UserManager::StubUserLoggedIn() { |
638 logged_in_user_ = &stub_user_; | 675 logged_in_user_ = &stub_user_; |
639 stub_user_.SetImage(GetDefaultImage(kStubDefaultImageIndex), | 676 stub_user_.SetImage(GetDefaultImage(kStubDefaultImageIndex), |
640 kStubDefaultImageIndex); | 677 kStubDefaultImageIndex); |
641 } | 678 } |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
778 } | 815 } |
779 | 816 |
780 PrefService* local_state = g_browser_process->local_state(); | 817 PrefService* local_state = g_browser_process->local_state(); |
781 DictionaryPrefUpdate images_update(local_state, kUserImages); | 818 DictionaryPrefUpdate images_update(local_state, kUserImages); |
782 base::DictionaryValue* image_properties = new base::DictionaryValue(); | 819 base::DictionaryValue* image_properties = new base::DictionaryValue(); |
783 image_properties->Set(kImagePathNodeName, new StringValue(image_path)); | 820 image_properties->Set(kImagePathNodeName, new StringValue(image_path)); |
784 image_properties->Set(kImageIndexNodeName, | 821 image_properties->Set(kImageIndexNodeName, |
785 new base::FundamentalValue(image_index)); | 822 new base::FundamentalValue(image_index)); |
786 images_update->SetWithoutPathExpansion(username, image_properties); | 823 images_update->SetWithoutPathExpansion(username, image_properties); |
787 DVLOG(1) << "Saving path to user image in Local State."; | 824 DVLOG(1) << "Saving path to user image in Local State."; |
788 local_state->ScheduleSavePersistentPrefs(); | |
789 | 825 |
790 NotifyLocalStateChanged(); | 826 NotifyLocalStateChanged(); |
791 } | 827 } |
792 | 828 |
793 void UserManager::DeleteUserImage(const FilePath& image_path) { | 829 void UserManager::DeleteUserImage(const FilePath& image_path) { |
794 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 830 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
795 if (!file_util::Delete(image_path, false)) { | 831 if (!file_util::Delete(image_path, false)) { |
796 LOG(ERROR) << "Failed to remove user image."; | 832 LOG(ERROR) << "Failed to remove user image."; |
797 return; | 833 return; |
798 } | 834 } |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
898 chrome::NOTIFICATION_PROFILE_IMAGE_UPDATE_FAILED, | 934 chrome::NOTIFICATION_PROFILE_IMAGE_UPDATE_FAILED, |
899 content::Source<UserManager>(this), | 935 content::Source<UserManager>(this), |
900 content::NotificationService::NoDetails()); | 936 content::NotificationService::NoDetails()); |
901 } | 937 } |
902 | 938 |
903 profile_image_downloader_.reset(); | 939 profile_image_downloader_.reset(); |
904 } | 940 } |
905 | 941 |
906 User* UserManager::CreateUser(const std::string& email) const { | 942 User* UserManager::CreateUser(const std::string& email) const { |
907 User* user = new User(email); | 943 User* user = new User(email); |
908 user->set_oauth_token_status(GetUserOAuthStatus(email)); | 944 user->set_oauth_token_status(LoadUserOAuthStatus(email)); |
909 // Used to determine whether user's display name is unique. | 945 // Used to determine whether user's display name is unique. |
910 ++display_name_count_[user->GetDisplayName()]; | 946 ++display_name_count_[user->GetDisplayName()]; |
911 return user; | 947 return user; |
912 } | 948 } |
913 | 949 |
914 } // namespace chromeos | 950 } // namespace chromeos |
OLD | NEW |