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

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

Issue 2568973002: chromeos: Replace user image notifications with observer (Closed)
Patch Set: rebase Created 4 years 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 <stddef.h> 7 #include <stddef.h>
8 8
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/files/file_path.h" 12 #include "base/files/file_path.h"
13 #include "base/files/file_util.h" 13 #include "base/files/file_util.h"
14 #include "base/logging.h" 14 #include "base/logging.h"
15 #include "base/macros.h" 15 #include "base/macros.h"
16 #include "base/memory/ptr_util.h" 16 #include "base/memory/ptr_util.h"
17 #include "base/metrics/histogram_macros.h" 17 #include "base/metrics/histogram_macros.h"
18 #include "base/path_service.h" 18 #include "base/path_service.h"
19 #include "base/rand_util.h" 19 #include "base/rand_util.h"
20 #include "base/sequenced_task_runner.h" 20 #include "base/sequenced_task_runner.h"
21 #include "base/strings/string_util.h" 21 #include "base/strings/string_util.h"
22 #include "base/task_runner_util.h" 22 #include "base/task_runner_util.h"
23 #include "base/threading/sequenced_worker_pool.h" 23 #include "base/threading/sequenced_worker_pool.h"
24 #include "base/threading/thread_task_runner_handle.h" 24 #include "base/threading/thread_task_runner_handle.h"
25 #include "base/time/time.h" 25 #include "base/time/time.h"
26 #include "base/trace_event/trace_event.h" 26 #include "base/trace_event/trace_event.h"
27 #include "base/values.h" 27 #include "base/values.h"
28 #include "chrome/browser/browser_process.h" 28 #include "chrome/browser/browser_process.h"
29 #include "chrome/browser/chrome_notification_types.h"
30 #include "chrome/browser/chromeos/login/helper.h" 29 #include "chrome/browser/chromeos/login/helper.h"
31 #include "chrome/browser/chromeos/login/users/avatar/user_image_loader.h" 30 #include "chrome/browser/chromeos/login/users/avatar/user_image_loader.h"
32 #include "chrome/browser/chromeos/login/users/avatar/user_image_sync_observer.h" 31 #include "chrome/browser/chromeos/login/users/avatar/user_image_sync_observer.h"
33 #include "chrome/browser/chromeos/login/users/default_user_image/default_user_im ages.h" 32 #include "chrome/browser/chromeos/login/users/default_user_image/default_user_im ages.h"
34 #include "chrome/browser/chromeos/profiles/profile_helper.h" 33 #include "chrome/browser/chromeos/profiles/profile_helper.h"
35 #include "chrome/browser/profiles/profile_downloader.h" 34 #include "chrome/browser/profiles/profile_downloader.h"
36 #include "chrome/browser/profiles/profile_manager.h" 35 #include "chrome/browser/profiles/profile_manager.h"
37 #include "chrome/common/chrome_paths.h" 36 #include "chrome/common/chrome_paths.h"
38 #include "chrome/grit/theme_resources.h" 37 #include "chrome/grit/theme_resources.h"
39 #include "components/policy/policy_constants.h" 38 #include "components/policy/policy_constants.h"
40 #include "components/prefs/pref_registry_simple.h" 39 #include "components/prefs/pref_registry_simple.h"
41 #include "components/prefs/pref_service.h" 40 #include "components/prefs/pref_service.h"
42 #include "components/prefs/scoped_user_pref_update.h" 41 #include "components/prefs/scoped_user_pref_update.h"
43 #include "components/user_manager/user_image/user_image.h" 42 #include "components/user_manager/user_image/user_image.h"
44 #include "components/user_manager/user_manager.h" 43 #include "components/user_manager/user_manager.h"
45 #include "content/public/browser/browser_thread.h" 44 #include "content/public/browser/browser_thread.h"
46 #include "content/public/browser/notification_service.h"
47 #include "ui/base/resource/resource_bundle.h" 45 #include "ui/base/resource/resource_bundle.h"
48 #include "ui/gfx/image/image_skia.h" 46 #include "ui/gfx/image/image_skia.h"
49 47
50 namespace chromeos { 48 namespace chromeos {
51 49
52 namespace { 50 namespace {
53 51
54 // Delay betweeen user login and attempt to update user's profile data. 52 // Delay betweeen user login and attempt to update user's profile data.
55 const int kProfileDataDownloadDelaySec = 10; 53 const int kProfileDataDownloadDelaySec = 10;
56 54
(...skipping 794 matching lines...) Expand 10 before | Expand all | Expand 10 after
851 DCHECK(!profile_image_load_start_time_.is_null()); 849 DCHECK(!profile_image_load_start_time_.is_null());
852 AddProfileImageTimeHistogram( 850 AddProfileImageTimeHistogram(
853 result, 851 result,
854 profile_image_download_reason_, 852 profile_image_download_reason_,
855 base::TimeTicks::Now() - profile_image_load_start_time_); 853 base::TimeTicks::Now() - profile_image_load_start_time_);
856 854
857 // Ignore the image if it is no longer needed. 855 // Ignore the image if it is no longer needed.
858 if (!NeedProfileImage()) 856 if (!NeedProfileImage())
859 return; 857 return;
860 858
859 const user_manager::User* const user = GetUser();
860
861 if (result == kDownloadDefault) { 861 if (result == kDownloadDefault) {
862 content::NotificationService::current()->Notify( 862 user_manager_->NotifyUserProfileImageUpdateFailed(*user);
863 chrome::NOTIFICATION_PROFILE_IMAGE_UPDATE_FAILED,
864 content::Source<UserImageManager>(this),
865 content::NotificationService::NoDetails());
866 } else { 863 } else {
867 profile_image_requested_ = false; 864 profile_image_requested_ = false;
868 } 865 }
869 866
870 // Nothing to do if the picture is cached or is the default avatar. 867 // Nothing to do if the picture is cached or is the default avatar.
871 if (result != kDownloadSuccess) 868 if (result != kDownloadSuccess)
872 return; 869 return;
873 870
874 downloaded_profile_image_ = gfx::ImageSkia::CreateFrom1xBitmap( 871 downloaded_profile_image_ = gfx::ImageSkia::CreateFrom1xBitmap(
875 downloader->GetProfilePicture()); 872 downloader->GetProfilePicture());
876 profile_image_url_ = GURL(downloader->GetProfilePictureURL()); 873 profile_image_url_ = GURL(downloader->GetProfilePictureURL());
877 874
878 const user_manager::User* user = GetUser();
879 if (user->image_index() == user_manager::User::USER_IMAGE_PROFILE) { 875 if (user->image_index() == user_manager::User::USER_IMAGE_PROFILE) {
880 VLOG(1) << "Updating profile image for logged-in user."; 876 VLOG(1) << "Updating profile image for logged-in user.";
881 UMA_HISTOGRAM_ENUMERATION("UserImage.ProfileDownloadResult", 877 UMA_HISTOGRAM_ENUMERATION("UserImage.ProfileDownloadResult",
882 kDownloadSuccessChanged, 878 kDownloadSuccessChanged,
883 kDownloadResultsCount); 879 kDownloadResultsCount);
884 // This will persist |downloaded_profile_image_| to disk. 880 // This will persist |downloaded_profile_image_| to disk.
885 SaveUserImageFromProfileImage(); 881 SaveUserImageFromProfileImage();
886 } 882 }
887 883
888 content::NotificationService::current()->Notify( 884 user_manager_->NotifyUserProfileImageUpdated(*user,
889 chrome::NOTIFICATION_PROFILE_IMAGE_UPDATED, 885 downloaded_profile_image_);
890 content::Source<UserImageManager>(this),
891 content::Details<const gfx::ImageSkia>(&downloaded_profile_image_));
892 } 886 }
893 887
894 void UserImageManagerImpl::OnProfileDownloadFailure( 888 void UserImageManagerImpl::OnProfileDownloadFailure(
895 ProfileDownloader* downloader, 889 ProfileDownloader* downloader,
896 ProfileDownloaderDelegate::FailureReason reason) { 890 ProfileDownloaderDelegate::FailureReason reason) {
897 DCHECK_EQ(downloader, profile_downloader_.get()); 891 DCHECK_EQ(downloader, profile_downloader_.get());
898 profile_downloader_.reset(); 892 profile_downloader_.reset();
899 893
900 if (downloading_profile_image_) { 894 if (downloading_profile_image_) {
901 UMA_HISTOGRAM_ENUMERATION("UserImage.ProfileDownloadResult", 895 UMA_HISTOGRAM_ENUMERATION("UserImage.ProfileDownloadResult",
902 kDownloadFailure, 896 kDownloadFailure,
903 kDownloadResultsCount); 897 kDownloadResultsCount);
904 DCHECK(!profile_image_load_start_time_.is_null()); 898 DCHECK(!profile_image_load_start_time_.is_null());
905 AddProfileImageTimeHistogram( 899 AddProfileImageTimeHistogram(
906 kDownloadFailure, 900 kDownloadFailure,
907 profile_image_download_reason_, 901 profile_image_download_reason_,
908 base::TimeTicks::Now() - profile_image_load_start_time_); 902 base::TimeTicks::Now() - profile_image_load_start_time_);
909 } 903 }
910 904
911 if (reason == ProfileDownloaderDelegate::NETWORK_ERROR) { 905 if (reason == ProfileDownloaderDelegate::NETWORK_ERROR) {
912 // Retry download after a delay if a network error occurred. 906 // Retry download after a delay if a network error occurred.
913 profile_download_one_shot_timer_.Start( 907 profile_download_one_shot_timer_.Start(
914 FROM_HERE, 908 FROM_HERE,
915 base::TimeDelta::FromSeconds(kProfileDataDownloadRetryIntervalSec), 909 base::TimeDelta::FromSeconds(kProfileDataDownloadRetryIntervalSec),
916 base::Bind(&UserImageManagerImpl::DownloadProfileData, 910 base::Bind(&UserImageManagerImpl::DownloadProfileData,
917 base::Unretained(this), 911 base::Unretained(this),
918 kProfileDownloadReasonRetry)); 912 kProfileDownloadReasonRetry));
919 } 913 }
920 914
921 content::NotificationService::current()->Notify( 915 user_manager_->NotifyUserProfileImageUpdateFailed(*GetUser());
922 chrome::NOTIFICATION_PROFILE_IMAGE_UPDATE_FAILED,
923 content::Source<UserImageManager>(this),
924 content::NotificationService::NoDetails());
925 } 916 }
926 917
927 bool UserImageManagerImpl::IsUserImageManaged() const { 918 bool UserImageManagerImpl::IsUserImageManaged() const {
928 return has_managed_image_; 919 return has_managed_image_;
929 } 920 }
930 921
931 void UserImageManagerImpl::SetInitialUserImage() { 922 void UserImageManagerImpl::SetInitialUserImage() {
932 // Choose a random default image. 923 // Choose a random default image.
933 SaveUserDefaultImageIndex( 924 SaveUserDefaultImageIndex(
934 base::RandInt(default_user_image::kFirstDefaultImageIndex, 925 base::RandInt(default_user_image::kFirstDefaultImageIndex,
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
992 base::FilePath(image_path), 983 base::FilePath(image_path),
993 false)); 984 false));
994 } 985 }
995 update->RemoveWithoutPathExpansion(user_id(), nullptr); 986 update->RemoveWithoutPathExpansion(user_id(), nullptr);
996 } 987 }
997 988
998 void UserImageManagerImpl::OnJobChangedUserImage() { 989 void UserImageManagerImpl::OnJobChangedUserImage() {
999 if (GetUser()->is_logged_in()) 990 if (GetUser()->is_logged_in())
1000 TryToInitDownloadedProfileImage(); 991 TryToInitDownloadedProfileImage();
1001 992
1002 content::NotificationService::current()->Notify( 993 user_manager_->NotifyUserImageChanged(*GetUser());
1003 chrome::NOTIFICATION_LOGIN_USER_IMAGE_CHANGED,
1004 content::Source<UserImageManagerImpl>(this),
1005 content::Details<const user_manager::User>(GetUser()));
1006 } 994 }
1007 995
1008 void UserImageManagerImpl::OnJobDone() { 996 void UserImageManagerImpl::OnJobDone() {
1009 if (job_.get()) 997 if (job_.get())
1010 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, job_.release()); 998 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, job_.release());
1011 else 999 else
1012 NOTREACHED(); 1000 NOTREACHED();
1013 } 1001 }
1014 1002
1015 void UserImageManagerImpl::TryToCreateImageSyncObserver() { 1003 void UserImageManagerImpl::TryToCreateImageSyncObserver() {
(...skipping 16 matching lines...) Expand all
1032 } 1020 }
1033 1021
1034 bool UserImageManagerImpl::IsUserLoggedInAndHasGaiaAccount() const { 1022 bool UserImageManagerImpl::IsUserLoggedInAndHasGaiaAccount() const {
1035 const user_manager::User* user = GetUser(); 1023 const user_manager::User* user = GetUser();
1036 if (!user) 1024 if (!user)
1037 return false; 1025 return false;
1038 return user->is_logged_in() && user->HasGaiaAccount(); 1026 return user->is_logged_in() && user->HasGaiaAccount();
1039 } 1027 }
1040 1028
1041 } // namespace chromeos 1029 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698