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

Side by Side Diff: chrome/browser/chromeos/login/users/avatar/user_image_manager_impl.cc

Issue 1165323004: We should use UserID object to identify users instead of username. Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 6 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/users/avatar/user_image_manager_impl.h" 5 #include "chrome/browser/chromeos/login/users/avatar/user_image_manager_impl.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/files/file_path.h" 8 #include "base/files/file_path.h"
9 #include "base/files/file_util.h" 9 #include "base/files/file_util.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 void OnSaveImageDone(bool success); 257 void OnSaveImageDone(bool success);
258 258
259 // Updates the user image in local state, setting it to one of the 259 // Updates the user image in local state, setting it to one of the
260 // default images or the saved |user_image_|, depending on 260 // default images or the saved |user_image_|, depending on
261 // |image_index_|. 261 // |image_index_|.
262 void UpdateLocalState(); 262 void UpdateLocalState();
263 263
264 // Notifies the |parent_| that the Job is done. 264 // Notifies the |parent_| that the Job is done.
265 void NotifyJobDone(); 265 void NotifyJobDone();
266 266
267 const std::string& user_id() const { return parent_->user_id(); } 267 const user_manager::UserID& user_id() const { return parent_->user_id(); }
268 268
269 UserImageManagerImpl* parent_; 269 UserImageManagerImpl* parent_;
270 270
271 // Whether one of the Load*() or Set*() methods has been run already. 271 // Whether one of the Load*() or Set*() methods has been run already.
272 bool run_; 272 bool run_;
273 273
274 int image_index_; 274 int image_index_;
275 GURL image_url_; 275 GURL image_url_;
276 base::FilePath image_path_; 276 base::FilePath image_path_;
277 277
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
426 false); 426 false);
427 } 427 }
428 user->SetImageURL(image_url_); 428 user->SetImageURL(image_url_);
429 429
430 parent_->OnJobChangedUserImage(); 430 parent_->OnJobChangedUserImage();
431 } 431 }
432 432
433 void UserImageManagerImpl::Job::SaveImageAndUpdateLocalState() { 433 void UserImageManagerImpl::Job::SaveImageAndUpdateLocalState() {
434 base::FilePath user_data_dir; 434 base::FilePath user_data_dir;
435 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); 435 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
436 image_path_ = user_data_dir.Append(user_id() + kSafeImagePathExtension); 436 image_path_ = user_data_dir.Append(user_id().GetUserEmail() + kSafeImagePathEx tension);
437 437
438 base::PostTaskAndReplyWithResult( 438 base::PostTaskAndReplyWithResult(
439 parent_->background_task_runner_.get(), 439 parent_->background_task_runner_.get(),
440 FROM_HERE, 440 FROM_HERE,
441 base::Bind(&SaveImage, user_image_, image_path_), 441 base::Bind(&SaveImage, user_image_, image_path_),
442 base::Bind(&Job::OnSaveImageDone, weak_factory_.GetWeakPtr())); 442 base::Bind(&Job::OnSaveImageDone, weak_factory_.GetWeakPtr()));
443 } 443 }
444 444
445 void UserImageManagerImpl::Job::OnSaveImageDone(bool success) { 445 void UserImageManagerImpl::Job::OnSaveImageDone(bool success) {
446 if (success || image_index_ == user_manager::User::USER_IMAGE_PROFILE) 446 if (success || image_index_ == user_manager::User::USER_IMAGE_PROFILE)
447 UpdateLocalState(); 447 UpdateLocalState();
448 NotifyJobDone(); 448 NotifyJobDone();
449 } 449 }
450 450
451 void UserImageManagerImpl::Job::UpdateLocalState() { 451 void UserImageManagerImpl::Job::UpdateLocalState() {
452 // Ignore if data stored or cached outside the user's cryptohome is to be 452 // Ignore if data stored or cached outside the user's cryptohome is to be
453 // treated as ephemeral. 453 // treated as ephemeral.
454 if (parent_->user_manager_->IsUserNonCryptohomeDataEphemeral(user_id())) 454 if (parent_->user_manager_->IsUserNonCryptohomeDataEphemeral(user_id()))
455 return; 455 return;
456 456
457 scoped_ptr<base::DictionaryValue> entry(new base::DictionaryValue); 457 scoped_ptr<base::DictionaryValue> entry(new base::DictionaryValue);
458 entry->Set(kImagePathNodeName, new base::StringValue(image_path_.value())); 458 entry->Set(kImagePathNodeName, new base::StringValue(image_path_.value()));
459 entry->Set(kImageIndexNodeName, new base::FundamentalValue(image_index_)); 459 entry->Set(kImageIndexNodeName, new base::FundamentalValue(image_index_));
460 if (!image_url_.is_empty()) 460 if (!image_url_.is_empty())
461 entry->Set(kImageURLNodeName, new base::StringValue(image_url_.spec())); 461 entry->Set(kImageURLNodeName, new base::StringValue(image_url_.spec()));
462 DictionaryPrefUpdate update(g_browser_process->local_state(), 462 DictionaryPrefUpdate update(g_browser_process->local_state(),
463 kUserImageProperties); 463 kUserImageProperties);
464 update->SetWithoutPathExpansion(user_id(), entry.release()); 464 update->SetWithoutPathExpansion(user_id().GetUserEmail(), entry.release());
465 465
466 parent_->user_manager_->NotifyLocalStateChanged(); 466 parent_->user_manager_->NotifyLocalStateChanged();
467 } 467 }
468 468
469 void UserImageManagerImpl::Job::NotifyJobDone() { 469 void UserImageManagerImpl::Job::NotifyJobDone() {
470 parent_->OnJobDone(); 470 parent_->OnJobDone();
471 } 471 }
472 472
473 UserImageManagerImpl::UserImageManagerImpl( 473 UserImageManagerImpl::UserImageManagerImpl(
474 const std::string& user_id, 474 const user_manager::UserID& user_id,
475 user_manager::UserManager* user_manager) 475 user_manager::UserManager* user_manager)
476 : UserImageManager(user_id), 476 : UserImageManager(user_id),
477 user_manager_(user_manager), 477 user_manager_(user_manager),
478 downloading_profile_image_(false), 478 downloading_profile_image_(false),
479 profile_image_requested_(false), 479 profile_image_requested_(false),
480 has_managed_image_(false), 480 has_managed_image_(false),
481 user_needs_migration_(false), 481 user_needs_migration_(false),
482 weak_factory_(this) { 482 weak_factory_(this) {
483 base::SequencedWorkerPool* blocking_pool = 483 base::SequencedWorkerPool* blocking_pool =
484 content::BrowserThread::GetBlockingPool(); 484 content::BrowserThread::GetBlockingPool();
(...skipping 19 matching lines...) Expand all
504 return; 504 return;
505 user_manager::User* user = GetUserAndModify(); 505 user_manager::User* user = GetUserAndModify();
506 bool needs_migration = false; 506 bool needs_migration = false;
507 507
508 // If entries are found in both |prefs_images_unsafe| and |prefs_images|, 508 // If entries are found in both |prefs_images_unsafe| and |prefs_images|,
509 // |prefs_images| is honored for now but |prefs_images_unsafe| will be 509 // |prefs_images| is honored for now but |prefs_images_unsafe| will be
510 // migrated, overwriting the |prefs_images| entry, when the user logs in. 510 // migrated, overwriting the |prefs_images| entry, when the user logs in.
511 const base::DictionaryValue* image_properties = NULL; 511 const base::DictionaryValue* image_properties = NULL;
512 if (prefs_images_unsafe) { 512 if (prefs_images_unsafe) {
513 needs_migration = prefs_images_unsafe->GetDictionaryWithoutPathExpansion( 513 needs_migration = prefs_images_unsafe->GetDictionaryWithoutPathExpansion(
514 user_id(), &image_properties); 514 user_id().GetUserEmail(), &image_properties);
515 if (needs_migration) 515 if (needs_migration)
516 user_needs_migration_ = true; 516 user_needs_migration_ = true;
517 } 517 }
518 if (prefs_images) { 518 if (prefs_images) {
519 prefs_images->GetDictionaryWithoutPathExpansion(user_id(), 519 prefs_images->GetDictionaryWithoutPathExpansion(user_id().GetUserEmail(),
520 &image_properties); 520 &image_properties);
521 } 521 }
522 522
523 // If the user image for |user_id| is managed by policy and the policy-set 523 // If the user image for |user_id| is managed by policy and the policy-set
524 // image is being loaded and persisted right now, let that job continue. It 524 // image is being loaded and persisted right now, let that job continue. It
525 // will update the user image when done. 525 // will update the user image when done.
526 if (IsUserImageManaged() && job_.get()) 526 if (IsUserImageManaged() && job_.get())
527 return; 527 return;
528 528
529 if (!image_properties) { 529 if (!image_properties) {
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
581 } else { 581 } else {
582 UMA_HISTOGRAM_ENUMERATION("UserImage.LoggedIn", 582 UMA_HISTOGRAM_ENUMERATION("UserImage.LoggedIn",
583 ImageIndexToHistogramIndex(user->image_index()), 583 ImageIndexToHistogramIndex(user->image_index()),
584 user_manager::kHistogramImagesCount); 584 user_manager::kHistogramImagesCount);
585 585
586 if (!IsUserImageManaged() && user_needs_migration_) { 586 if (!IsUserImageManaged() && user_needs_migration_) {
587 const base::DictionaryValue* prefs_images_unsafe = 587 const base::DictionaryValue* prefs_images_unsafe =
588 g_browser_process->local_state()->GetDictionary(kUserImages); 588 g_browser_process->local_state()->GetDictionary(kUserImages);
589 const base::DictionaryValue* image_properties = NULL; 589 const base::DictionaryValue* image_properties = NULL;
590 if (prefs_images_unsafe->GetDictionaryWithoutPathExpansion( 590 if (prefs_images_unsafe->GetDictionaryWithoutPathExpansion(
591 user_id(), &image_properties)) { 591 user_id().GetUserEmail(), &image_properties)) {
592 std::string image_path; 592 std::string image_path;
593 image_properties->GetString(kImagePathNodeName, &image_path); 593 image_properties->GetString(kImagePathNodeName, &image_path);
594 job_.reset(new Job(this)); 594 job_.reset(new Job(this));
595 if (!image_path.empty()) { 595 if (!image_path.empty()) {
596 VLOG(0) << "Loading old user image, then migrating it."; 596 VLOG(0) << "Loading old user image, then migrating it.";
597 job_->SetToPath(base::FilePath(image_path), 597 job_->SetToPath(base::FilePath(image_path),
598 user->image_index(), 598 user->image_index(),
599 user->image_url(), 599 user->image_url(),
600 false); 600 false);
601 } else { 601 } else {
(...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after
916 profile_image_load_start_time_ = base::TimeTicks::Now(); 916 profile_image_load_start_time_ = base::TimeTicks::Now();
917 profile_downloader_.reset(new ProfileDownloader(this)); 917 profile_downloader_.reset(new ProfileDownloader(this));
918 profile_downloader_->Start(); 918 profile_downloader_->Start();
919 } 919 }
920 920
921 void UserImageManagerImpl::DeleteUserImageAndLocalStateEntry( 921 void UserImageManagerImpl::DeleteUserImageAndLocalStateEntry(
922 const char* prefs_dict_root) { 922 const char* prefs_dict_root) {
923 DictionaryPrefUpdate update(g_browser_process->local_state(), 923 DictionaryPrefUpdate update(g_browser_process->local_state(),
924 prefs_dict_root); 924 prefs_dict_root);
925 const base::DictionaryValue* image_properties; 925 const base::DictionaryValue* image_properties;
926 if (!update->GetDictionaryWithoutPathExpansion(user_id(), &image_properties)) 926 if (!update->GetDictionaryWithoutPathExpansion(user_id().GetUserEmail(), &imag e_properties))
927 return; 927 return;
928 928
929 std::string image_path; 929 std::string image_path;
930 image_properties->GetString(kImagePathNodeName, &image_path); 930 image_properties->GetString(kImagePathNodeName, &image_path);
931 if (!image_path.empty()) { 931 if (!image_path.empty()) {
932 background_task_runner_->PostTask( 932 background_task_runner_->PostTask(
933 FROM_HERE, 933 FROM_HERE,
934 base::Bind(base::IgnoreResult(&base::DeleteFile), 934 base::Bind(base::IgnoreResult(&base::DeleteFile),
935 base::FilePath(image_path), 935 base::FilePath(image_path),
936 false)); 936 false));
937 } 937 }
938 update->RemoveWithoutPathExpansion(user_id(), NULL); 938 update->RemoveWithoutPathExpansion(user_id().GetUserEmail(), NULL);
939 } 939 }
940 940
941 void UserImageManagerImpl::OnJobChangedUserImage() { 941 void UserImageManagerImpl::OnJobChangedUserImage() {
942 if (GetUser()->is_logged_in()) 942 if (GetUser()->is_logged_in())
943 TryToInitDownloadedProfileImage(); 943 TryToInitDownloadedProfileImage();
944 944
945 content::NotificationService::current()->Notify( 945 content::NotificationService::current()->Notify(
946 chrome::NOTIFICATION_LOGIN_USER_IMAGE_CHANGED, 946 chrome::NOTIFICATION_LOGIN_USER_IMAGE_CHANGED,
947 content::Source<UserImageManagerImpl>(this), 947 content::Source<UserImageManagerImpl>(this),
948 content::Details<const user_manager::User>(GetUser())); 948 content::Details<const user_manager::User>(GetUser()));
949 } 949 }
950 950
951 void UserImageManagerImpl::OnJobDone() { 951 void UserImageManagerImpl::OnJobDone() {
952 if (job_.get()) 952 if (job_.get())
953 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, job_.release()); 953 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, job_.release());
954 else 954 else
955 NOTREACHED(); 955 NOTREACHED();
956 956
957 if (!user_needs_migration_) 957 if (!user_needs_migration_)
958 return; 958 return;
959 // Migration completed for |user_id|. 959 // Migration completed for |user_id|.
960 user_needs_migration_ = false; 960 user_needs_migration_ = false;
961 961
962 const base::DictionaryValue* prefs_images_unsafe = 962 const base::DictionaryValue* prefs_images_unsafe =
963 g_browser_process->local_state()->GetDictionary(kUserImages); 963 g_browser_process->local_state()->GetDictionary(kUserImages);
964 const base::DictionaryValue* image_properties = NULL; 964 const base::DictionaryValue* image_properties = NULL;
965 if (!prefs_images_unsafe->GetDictionaryWithoutPathExpansion( 965 if (!prefs_images_unsafe->GetDictionaryWithoutPathExpansion(
966 user_id(), &image_properties)) { 966 user_id().GetUserEmail(), &image_properties)) {
967 NOTREACHED(); 967 NOTREACHED();
968 return; 968 return;
969 } 969 }
970 970
971 int image_index = user_manager::User::USER_IMAGE_INVALID; 971 int image_index = user_manager::User::USER_IMAGE_INVALID;
972 image_properties->GetInteger(kImageIndexNodeName, &image_index); 972 image_properties->GetInteger(kImageIndexNodeName, &image_index);
973 UMA_HISTOGRAM_ENUMERATION("UserImage.Migration", 973 UMA_HISTOGRAM_ENUMERATION("UserImage.Migration",
974 ImageIndexToHistogramIndex(image_index), 974 ImageIndexToHistogramIndex(image_index),
975 user_manager::kHistogramImagesCount); 975 user_manager::kHistogramImagesCount);
976 976
(...skipping 14 matching lines...) Expand all
991 weak_factory_.GetWeakPtr())); 991 weak_factory_.GetWeakPtr()));
992 } else { 992 } else {
993 // If no old image exists, remove |user_id| from the old prefs dictionary. 993 // If no old image exists, remove |user_id| from the old prefs dictionary.
994 UpdateLocalStateAfterMigration(); 994 UpdateLocalStateAfterMigration();
995 } 995 }
996 } 996 }
997 997
998 void UserImageManagerImpl::UpdateLocalStateAfterMigration() { 998 void UserImageManagerImpl::UpdateLocalStateAfterMigration() {
999 DictionaryPrefUpdate update(g_browser_process->local_state(), 999 DictionaryPrefUpdate update(g_browser_process->local_state(),
1000 kUserImages); 1000 kUserImages);
1001 update->RemoveWithoutPathExpansion(user_id(), NULL); 1001 update->RemoveWithoutPathExpansion(user_id().GetUserEmail(), NULL);
1002 } 1002 }
1003 1003
1004 void UserImageManagerImpl::TryToCreateImageSyncObserver() { 1004 void UserImageManagerImpl::TryToCreateImageSyncObserver() {
1005 const user_manager::User* user = GetUser(); 1005 const user_manager::User* user = GetUser();
1006 // If the currently logged-in user's user image is managed, the sync observer 1006 // If the currently logged-in user's user image is managed, the sync observer
1007 // must not be started so that the policy-set image does not get synced out. 1007 // must not be started so that the policy-set image does not get synced out.
1008 if (!user_image_sync_observer_ && 1008 if (!user_image_sync_observer_ &&
1009 user && user->CanSyncImage() && 1009 user && user->CanSyncImage() &&
1010 !IsUserImageManaged()) { 1010 !IsUserImageManaged()) {
1011 user_image_sync_observer_.reset(new UserImageSyncObserver(user)); 1011 user_image_sync_observer_.reset(new UserImageSyncObserver(user));
1012 } 1012 }
1013 } 1013 }
1014 1014
1015 const user_manager::User* UserImageManagerImpl::GetUser() const { 1015 const user_manager::User* UserImageManagerImpl::GetUser() const {
1016 return user_manager_->FindUser(user_id()); 1016 return user_manager_->FindUser(user_id());
1017 } 1017 }
1018 1018
1019 user_manager::User* UserImageManagerImpl::GetUserAndModify() const { 1019 user_manager::User* UserImageManagerImpl::GetUserAndModify() const {
1020 return user_manager_->FindUserAndModify(user_id()); 1020 return user_manager_->FindUserAndModify(user_id());
1021 } 1021 }
1022 1022
1023 bool UserImageManagerImpl::IsUserLoggedInAndHasGaiaAccount() const { 1023 bool UserImageManagerImpl::IsUserLoggedInAndHasGaiaAccount() const {
1024 const user_manager::User* user = GetUser(); 1024 const user_manager::User* user = GetUser();
1025 if (!user) 1025 if (!user)
1026 return false; 1026 return false;
1027 return user->is_logged_in() && user->HasGaiaAccount(); 1027 return user->is_logged_in() && user->HasGaiaAccount();
1028 } 1028 }
1029 1029
1030 } // namespace chromeos 1030 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698