| 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 "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 // Delay betweeen user login and attempt to update user's profile image. | 63 // Delay betweeen user login and attempt to update user's profile image. |
| 64 const long kProfileImageDownloadDelayMs = 10000; | 64 const long kProfileImageDownloadDelayMs = 10000; |
| 65 | 65 |
| 66 base::LazyInstance<UserManager> g_user_manager(base::LINKER_INITIALIZED); | 66 base::LazyInstance<UserManager> g_user_manager(base::LINKER_INITIALIZED); |
| 67 | 67 |
| 68 // Stores user's OAuthTokenStatus in local state. Runs on UI thread. | 68 // Stores user's OAuthTokenStatus in local state. Runs on UI thread. |
| 69 void SaveOAuthTokenStatusToLocalState(const std::string& username, | 69 void SaveOAuthTokenStatusToLocalState(const std::string& username, |
| 70 UserManager::OAuthTokenStatus oauth_token_status) { | 70 UserManager::OAuthTokenStatus oauth_token_status) { |
| 71 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 71 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 72 PrefService* local_state = g_browser_process->local_state(); | 72 PrefService* local_state = g_browser_process->local_state(); |
| 73 DictionaryPrefUpdate oauth_status_update(local_state, kUserOAuthTokenStatus); | 73 { |
| 74 oauth_status_update->SetWithoutPathExpansion(username, | 74 DictionaryPrefUpdate oauth_status_update( |
| 75 new base::FundamentalValue(static_cast<int>(oauth_token_status))); | 75 local_state, kUserOAuthTokenStatus); |
| 76 DVLOG(1) << "Saving user OAuth token status in Local State."; | 76 oauth_status_update->SetWithoutPathExpansion(username, |
| 77 local_state->SavePersistentPrefs(); | 77 new base::FundamentalValue(static_cast<int>(oauth_token_status))); |
| 78 DVLOG(1) << "Saving user OAuth token status in Local State."; |
| 79 } |
| 80 local_state->CommitPendingWrite(); |
| 78 } | 81 } |
| 79 | 82 |
| 80 // Gets user's OAuthTokenStatus from local state. | 83 // Gets user's OAuthTokenStatus from local state. |
| 81 UserManager::OAuthTokenStatus GetOAuthTokenStatusFromLocalState( | 84 UserManager::OAuthTokenStatus GetOAuthTokenStatusFromLocalState( |
| 82 const std::string& username) { | 85 const std::string& username) { |
| 83 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 84 | 87 |
| 85 if (CommandLine::ForCurrentProcess()->HasSwitch( | 88 if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 86 switches::kSkipOAuthLogin)) { | 89 switches::kSkipOAuthLogin)) { |
| 87 // Use OAUTH_TOKEN_STATUS_VALID flag if kSkipOAuthLogin is present. | 90 // Use OAUTH_TOKEN_STATUS_VALID flag if kSkipOAuthLogin is present. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 101 | 104 |
| 102 return UserManager::OAUTH_TOKEN_STATUS_UNKNOWN; | 105 return UserManager::OAUTH_TOKEN_STATUS_UNKNOWN; |
| 103 } | 106 } |
| 104 | 107 |
| 105 // Stores path to the image and its index in local state. Runs on UI thread. | 108 // Stores path to the image and its index in local state. Runs on UI thread. |
| 106 void SaveImageToLocalState(const std::string& username, | 109 void SaveImageToLocalState(const std::string& username, |
| 107 const std::string& image_path, | 110 const std::string& image_path, |
| 108 int image_index) { | 111 int image_index) { |
| 109 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 110 PrefService* local_state = g_browser_process->local_state(); | 113 PrefService* local_state = g_browser_process->local_state(); |
| 111 DictionaryPrefUpdate images_update(local_state, kUserImages); | 114 { |
| 112 base::DictionaryValue* image_properties = new base::DictionaryValue(); | 115 DictionaryPrefUpdate images_update(local_state, kUserImages); |
| 113 image_properties->Set(kImagePathNodeName, new StringValue(image_path)); | 116 base::DictionaryValue* image_properties = new base::DictionaryValue(); |
| 114 image_properties->Set(kImageIndexNodeName, | 117 image_properties->Set(kImagePathNodeName, new StringValue(image_path)); |
| 115 new base::FundamentalValue(image_index)); | 118 image_properties->Set(kImageIndexNodeName, |
| 116 images_update->SetWithoutPathExpansion(username, image_properties); | 119 new base::FundamentalValue(image_index)); |
| 117 DVLOG(1) << "Saving path to user image in Local State."; | 120 images_update->SetWithoutPathExpansion(username, image_properties); |
| 118 local_state->SavePersistentPrefs(); | 121 DVLOG(1) << "Saving path to user image in Local State."; |
| 122 } |
| 123 local_state->CommitPendingWrite(); |
| 119 UserManager::Get()->NotifyLocalStateChanged(); | 124 UserManager::Get()->NotifyLocalStateChanged(); |
| 120 UserManager* user_manager = UserManager::Get(); | 125 UserManager* user_manager = UserManager::Get(); |
| 121 NotificationService::current()->Notify( | 126 NotificationService::current()->Notify( |
| 122 chrome::NOTIFICATION_LOGIN_USER_IMAGE_CHANGED, | 127 chrome::NOTIFICATION_LOGIN_USER_IMAGE_CHANGED, |
| 123 Source<UserManager>(user_manager), | 128 Source<UserManager>(user_manager), |
| 124 Details<const UserManager::User>(&(user_manager->logged_in_user()))); | 129 Details<const UserManager::User>(&(user_manager->logged_in_user()))); |
| 125 } | 130 } |
| 126 | 131 |
| 127 // Saves image to file with specified path. Runs on FILE thread. | 132 // Saves image to file with specified path. Runs on FILE thread. |
| 128 // Posts task for saving image info to local state on UI thread. | 133 // Posts task for saving image info to local state on UI thread. |
| (...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 428 if (!IsKnownUser(email)) { | 433 if (!IsKnownUser(email)) { |
| 429 current_user_is_new_ = true; | 434 current_user_is_new_ = true; |
| 430 browser_defaults::skip_restore = true; | 435 browser_defaults::skip_restore = true; |
| 431 } | 436 } |
| 432 | 437 |
| 433 // Get a copy of the current users. | 438 // Get a copy of the current users. |
| 434 std::vector<User> users = GetUsers(); | 439 std::vector<User> users = GetUsers(); |
| 435 | 440 |
| 436 // Clear the prefs view of the users. | 441 // Clear the prefs view of the users. |
| 437 PrefService* prefs = g_browser_process->local_state(); | 442 PrefService* prefs = g_browser_process->local_state(); |
| 438 ListPrefUpdate prefs_users_update(prefs, kLoggedInUsers); | 443 { |
| 439 prefs_users_update->Clear(); | 444 ListPrefUpdate prefs_users_update(prefs, kLoggedInUsers); |
| 445 prefs_users_update->Clear(); |
| 440 | 446 |
| 441 user_is_logged_in_ = true; | 447 user_is_logged_in_ = true; |
| 442 logged_in_user_ = User(); | 448 logged_in_user_ = User(); |
| 443 logged_in_user_.set_email(email); | 449 logged_in_user_.set_email(email); |
| 444 | 450 |
| 445 // Make sure this user is first. | 451 // Make sure this user is first. |
| 446 prefs_users_update->Append(Value::CreateStringValue(email)); | 452 prefs_users_update->Append(Value::CreateStringValue(email)); |
| 447 for (std::vector<User>::iterator it = users.begin(); | 453 for (std::vector<User>::iterator it = users.begin(); |
| 448 it != users.end(); | 454 it != users.end(); |
| 449 ++it) { | 455 ++it) { |
| 450 std::string user_email = it->email(); | 456 std::string user_email = it->email(); |
| 451 // Skip the most recent user. | 457 // Skip the most recent user. |
| 452 if (email != user_email) { | 458 if (email != user_email) { |
| 453 prefs_users_update->Append(Value::CreateStringValue(user_email)); | 459 prefs_users_update->Append(Value::CreateStringValue(user_email)); |
| 454 } else { | 460 } else { |
| 455 logged_in_user_ = *it; | 461 logged_in_user_ = *it; |
| 462 } |
| 456 } | 463 } |
| 457 } | 464 } |
| 458 prefs->SavePersistentPrefs(); | 465 prefs->CommitPendingWrite(); |
| 459 NotifyOnLogin(); | 466 NotifyOnLogin(); |
| 460 if (current_user_is_new_) { | 467 if (current_user_is_new_) { |
| 461 SetDefaultUserImage(email); | 468 SetDefaultUserImage(email); |
| 462 } else { | 469 } else { |
| 463 // Download profile image if it's user image and see if it has changed. | 470 // Download profile image if it's user image and see if it has changed. |
| 464 int image_index = logged_in_user_.default_image_index(); | 471 int image_index = logged_in_user_.default_image_index(); |
| 465 if (image_index == User::kProfileImageIndex) { | 472 if (image_index == User::kProfileImageIndex) { |
| 466 BrowserThread::PostDelayedTask( | 473 BrowserThread::PostDelayedTask( |
| 467 BrowserThread::UI, | 474 BrowserThread::UI, |
| 468 FROM_HERE, | 475 FROM_HERE, |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 510 // |RemoveAttempt| deletes itself when done. | 517 // |RemoveAttempt| deletes itself when done. |
| 511 new RemoveAttempt(email, delegate); | 518 new RemoveAttempt(email, delegate); |
| 512 } | 519 } |
| 513 | 520 |
| 514 void UserManager::RemoveUserFromList(const std::string& email) { | 521 void UserManager::RemoveUserFromList(const std::string& email) { |
| 515 // Get a copy of the current users. | 522 // Get a copy of the current users. |
| 516 std::vector<User> users = GetUsers(); | 523 std::vector<User> users = GetUsers(); |
| 517 | 524 |
| 518 // Clear the prefs view of the users. | 525 // Clear the prefs view of the users. |
| 519 PrefService* prefs = g_browser_process->local_state(); | 526 PrefService* prefs = g_browser_process->local_state(); |
| 520 ListPrefUpdate prefs_users_update(prefs, kLoggedInUsers); | 527 std::string image_path_string; |
| 521 prefs_users_update->Clear(); | 528 { |
| 529 ListPrefUpdate prefs_users_update(prefs, kLoggedInUsers); |
| 530 prefs_users_update->Clear(); |
| 522 | 531 |
| 523 for (std::vector<User>::iterator it = users.begin(); | 532 for (std::vector<User>::iterator it = users.begin(); |
| 524 it != users.end(); | 533 it != users.end(); |
| 525 ++it) { | 534 ++it) { |
| 526 std::string user_email = it->email(); | 535 std::string user_email = it->email(); |
| 527 // Skip user that we would like to delete. | 536 // Skip user that we would like to delete. |
| 528 if (email != user_email) | 537 if (email != user_email) |
| 529 prefs_users_update->Append(Value::CreateStringValue(user_email)); | 538 prefs_users_update->Append(Value::CreateStringValue(user_email)); |
| 539 } |
| 540 |
| 541 DictionaryPrefUpdate prefs_images_update(prefs, kUserImages); |
| 542 prefs_images_update->GetStringWithoutPathExpansion( |
| 543 email, &image_path_string); |
| 544 prefs_images_update->RemoveWithoutPathExpansion(email, NULL); |
| 545 |
| 546 DictionaryPrefUpdate prefs_oauth_update(prefs, kUserOAuthTokenStatus); |
| 547 int oauth_status; |
| 548 prefs_oauth_update->GetIntegerWithoutPathExpansion(email, &oauth_status); |
| 549 prefs_oauth_update->RemoveWithoutPathExpansion(email, NULL); |
| 530 } | 550 } |
| 531 | 551 prefs->CommitPendingWrite(); |
| 532 DictionaryPrefUpdate prefs_images_update(prefs, kUserImages); | |
| 533 std::string image_path_string; | |
| 534 prefs_images_update->GetStringWithoutPathExpansion(email, &image_path_string); | |
| 535 prefs_images_update->RemoveWithoutPathExpansion(email, NULL); | |
| 536 | |
| 537 DictionaryPrefUpdate prefs_oauth_update(prefs, kUserOAuthTokenStatus); | |
| 538 int oauth_status; | |
| 539 prefs_oauth_update->GetIntegerWithoutPathExpansion(email, &oauth_status); | |
| 540 prefs_oauth_update->RemoveWithoutPathExpansion(email, NULL); | |
| 541 | |
| 542 prefs->SavePersistentPrefs(); | |
| 543 | 552 |
| 544 int default_image_id = kDefaultImagesCount; | 553 int default_image_id = kDefaultImagesCount; |
| 545 if (!IsDefaultImagePath(image_path_string, &default_image_id)) { | 554 if (!IsDefaultImagePath(image_path_string, &default_image_id)) { |
| 546 FilePath image_path(image_path_string); | 555 FilePath image_path(image_path_string); |
| 547 BrowserThread::PostTask( | 556 BrowserThread::PostTask( |
| 548 BrowserThread::FILE, | 557 BrowserThread::FILE, |
| 549 FROM_HERE, | 558 FROM_HERE, |
| 550 NewRunnableFunction(&DeleteUserImage, | 559 NewRunnableFunction(&DeleteUserImage, |
| 551 image_path)); | 560 image_path)); |
| 552 } | 561 } |
| (...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 813 observer_list_, | 822 observer_list_, |
| 814 LocalStateChanged(this)); | 823 LocalStateChanged(this)); |
| 815 } | 824 } |
| 816 | 825 |
| 817 void UserManager::DownloadProfileImage() { | 826 void UserManager::DownloadProfileImage() { |
| 818 profile_image_downloader_.reset(new ProfileImageDownloader(this)); | 827 profile_image_downloader_.reset(new ProfileImageDownloader(this)); |
| 819 profile_image_downloader_->Start(); | 828 profile_image_downloader_->Start(); |
| 820 } | 829 } |
| 821 | 830 |
| 822 } // namespace chromeos | 831 } // namespace chromeos |
| OLD | NEW |