OLD | NEW |
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/debug/trace_event.h" | 8 #include "base/debug/trace_event.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/message_loop/message_loop_proxy.h" | 12 #include "base/message_loop/message_loop_proxy.h" |
13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
14 #include "base/path_service.h" | 14 #include "base/path_service.h" |
15 #include "base/prefs/pref_registry_simple.h" | 15 #include "base/prefs/pref_registry_simple.h" |
16 #include "base/prefs/pref_service.h" | 16 #include "base/prefs/pref_service.h" |
17 #include "base/prefs/scoped_user_pref_update.h" | 17 #include "base/prefs/scoped_user_pref_update.h" |
18 #include "base/rand_util.h" | 18 #include "base/rand_util.h" |
19 #include "base/sequenced_task_runner.h" | 19 #include "base/sequenced_task_runner.h" |
20 #include "base/task_runner_util.h" | 20 #include "base/task_runner_util.h" |
21 #include "base/threading/sequenced_worker_pool.h" | 21 #include "base/threading/sequenced_worker_pool.h" |
22 #include "base/time/time.h" | 22 #include "base/time/time.h" |
23 #include "base/values.h" | 23 #include "base/values.h" |
24 #include "chrome/browser/browser_process.h" | 24 #include "chrome/browser/browser_process.h" |
25 #include "chrome/browser/chrome_notification_types.h" | 25 #include "chrome/browser/chrome_notification_types.h" |
26 #include "chrome/browser/chromeos/login/helper.h" | 26 #include "chrome/browser/chromeos/login/helper.h" |
27 #include "chrome/browser/chromeos/login/users/avatar/default_user_images.h" | |
28 #include "chrome/browser/chromeos/login/users/avatar/user_image_sync_observer.h" | 27 #include "chrome/browser/chromeos/login/users/avatar/user_image_sync_observer.h" |
29 #include "chrome/browser/chromeos/login/users/user_manager.h" | 28 #include "chrome/browser/chromeos/login/users/user_manager.h" |
30 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 29 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
31 #include "chrome/browser/profiles/profile_downloader.h" | 30 #include "chrome/browser/profiles/profile_downloader.h" |
32 #include "chrome/browser/profiles/profile_manager.h" | 31 #include "chrome/browser/profiles/profile_manager.h" |
33 #include "chrome/common/chrome_paths.h" | 32 #include "chrome/common/chrome_paths.h" |
| 33 #include "components/user_manager/user_image/default_user_images.h" |
34 #include "components/user_manager/user_image/user_image.h" | 34 #include "components/user_manager/user_image/user_image.h" |
35 #include "components/user_manager/user_type.h" | 35 #include "components/user_manager/user_type.h" |
36 #include "content/public/browser/browser_thread.h" | 36 #include "content/public/browser/browser_thread.h" |
37 #include "content/public/browser/notification_service.h" | 37 #include "content/public/browser/notification_service.h" |
38 #include "policy/policy_constants.h" | 38 #include "policy/policy_constants.h" |
39 #include "ui/gfx/image/image_skia.h" | 39 #include "ui/gfx/image/image_skia.h" |
40 | 40 |
41 namespace chromeos { | 41 namespace chromeos { |
42 | 42 |
43 namespace { | 43 namespace { |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 counter->AddTime(time_delta); | 140 counter->AddTime(time_delta); |
141 | 141 |
142 DVLOG(1) << "Profile image download time: " << time_delta.InSecondsF(); | 142 DVLOG(1) << "Profile image download time: " << time_delta.InSecondsF(); |
143 } | 143 } |
144 | 144 |
145 // Converts |image_index| to UMA histogram value. | 145 // Converts |image_index| to UMA histogram value. |
146 int ImageIndexToHistogramIndex(int image_index) { | 146 int ImageIndexToHistogramIndex(int image_index) { |
147 switch (image_index) { | 147 switch (image_index) { |
148 case User::kExternalImageIndex: | 148 case User::kExternalImageIndex: |
149 // TODO(ivankr): Distinguish this from selected from file. | 149 // TODO(ivankr): Distinguish this from selected from file. |
150 return kHistogramImageFromCamera; | 150 return user_manager::kHistogramImageFromCamera; |
151 case User::kProfileImageIndex: | 151 case User::kProfileImageIndex: |
152 return kHistogramImageFromProfile; | 152 return user_manager::kHistogramImageFromProfile; |
153 default: | 153 default: |
154 return image_index; | 154 return image_index; |
155 } | 155 } |
156 } | 156 } |
157 | 157 |
158 bool SaveImage(const user_manager::UserImage& user_image, | 158 bool SaveImage(const user_manager::UserImage& user_image, |
159 const base::FilePath& image_path) { | 159 const base::FilePath& image_path) { |
160 user_manager::UserImage safe_image; | 160 user_manager::UserImage safe_image; |
161 const user_manager::UserImage::RawImage* encoded_image = NULL; | 161 const user_manager::UserImage::RawImage* encoded_image = NULL; |
162 if (!user_image.is_safe_format()) { | 162 if (!user_image.is_safe_format()) { |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 void UserImageManagerImpl::Job::LoadImage(base::FilePath image_path, | 292 void UserImageManagerImpl::Job::LoadImage(base::FilePath image_path, |
293 const int image_index, | 293 const int image_index, |
294 const GURL& image_url) { | 294 const GURL& image_url) { |
295 DCHECK(!run_); | 295 DCHECK(!run_); |
296 run_ = true; | 296 run_ = true; |
297 | 297 |
298 image_index_ = image_index; | 298 image_index_ = image_index; |
299 image_url_ = image_url; | 299 image_url_ = image_url; |
300 image_path_ = image_path; | 300 image_path_ = image_path; |
301 | 301 |
302 if (image_index_ >= 0 && image_index_ < kDefaultImagesCount) { | 302 if (image_index_ >= 0 && image_index_ < user_manager::kDefaultImagesCount) { |
303 // Load one of the default images. This happens synchronously. | 303 // Load one of the default images. This happens synchronously. |
304 user_image_ = user_manager::UserImage(GetDefaultImage(image_index_)); | 304 user_image_ = |
| 305 user_manager::UserImage(user_manager::GetDefaultImage(image_index_)); |
305 UpdateUser(); | 306 UpdateUser(); |
306 NotifyJobDone(); | 307 NotifyJobDone(); |
307 } else if (image_index_ == User::kExternalImageIndex || | 308 } else if (image_index_ == User::kExternalImageIndex || |
308 image_index_ == User::kProfileImageIndex) { | 309 image_index_ == User::kProfileImageIndex) { |
309 // Load the user image from a file referenced by |image_path|. This happens | 310 // Load the user image from a file referenced by |image_path|. This happens |
310 // asynchronously. The JPEG image loader can be used here because | 311 // asynchronously. The JPEG image loader can be used here because |
311 // LoadImage() is called only for users whose user image has previously | 312 // LoadImage() is called only for users whose user image has previously |
312 // been set by one of the Set*() methods, which transcode to JPEG format. | 313 // been set by one of the Set*() methods, which transcode to JPEG format. |
313 DCHECK(!image_path_.empty()); | 314 DCHECK(!image_path_.empty()); |
314 parent_->image_loader_->Start(image_path_.value(), | 315 parent_->image_loader_->Start(image_path_.value(), |
315 0, | 316 0, |
316 base::Bind(&Job::OnLoadImageDone, | 317 base::Bind(&Job::OnLoadImageDone, |
317 weak_factory_.GetWeakPtr(), | 318 weak_factory_.GetWeakPtr(), |
318 false)); | 319 false)); |
319 } else { | 320 } else { |
320 NOTREACHED(); | 321 NOTREACHED(); |
321 NotifyJobDone(); | 322 NotifyJobDone(); |
322 } | 323 } |
323 } | 324 } |
324 | 325 |
325 void UserImageManagerImpl::Job::SetToDefaultImage(int default_image_index) { | 326 void UserImageManagerImpl::Job::SetToDefaultImage(int default_image_index) { |
326 DCHECK(!run_); | 327 DCHECK(!run_); |
327 run_ = true; | 328 run_ = true; |
328 | 329 |
329 DCHECK_LE(0, default_image_index); | 330 DCHECK_LE(0, default_image_index); |
330 DCHECK_GT(kDefaultImagesCount, default_image_index); | 331 DCHECK_GT(user_manager::kDefaultImagesCount, default_image_index); |
331 | 332 |
332 image_index_ = default_image_index; | 333 image_index_ = default_image_index; |
333 user_image_ = user_manager::UserImage(GetDefaultImage(image_index_)); | 334 user_image_ = |
| 335 user_manager::UserImage(user_manager::GetDefaultImage(image_index_)); |
334 | 336 |
335 UpdateUser(); | 337 UpdateUser(); |
336 UpdateLocalState(); | 338 UpdateLocalState(); |
337 NotifyJobDone(); | 339 NotifyJobDone(); |
338 } | 340 } |
339 | 341 |
340 void UserImageManagerImpl::Job::SetToImage( | 342 void UserImageManagerImpl::Job::SetToImage( |
341 int image_index, | 343 int image_index, |
342 const user_manager::UserImage& user_image) { | 344 const user_manager::UserImage& user_image) { |
343 DCHECK(!run_); | 345 DCHECK(!run_); |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
514 if (IsUserImageManaged() && job_.get()) | 516 if (IsUserImageManaged() && job_.get()) |
515 return; | 517 return; |
516 | 518 |
517 if (!image_properties) { | 519 if (!image_properties) { |
518 SetInitialUserImage(); | 520 SetInitialUserImage(); |
519 return; | 521 return; |
520 } | 522 } |
521 | 523 |
522 int image_index = User::kInvalidImageIndex; | 524 int image_index = User::kInvalidImageIndex; |
523 image_properties->GetInteger(kImageIndexNodeName, &image_index); | 525 image_properties->GetInteger(kImageIndexNodeName, &image_index); |
524 if (image_index >= 0 && image_index < kDefaultImagesCount) { | 526 if (image_index >= 0 && image_index < user_manager::kDefaultImagesCount) { |
525 user->SetImage(user_manager::UserImage(GetDefaultImage(image_index)), | 527 user->SetImage( |
526 image_index); | 528 user_manager::UserImage(user_manager::GetDefaultImage(image_index)), |
| 529 image_index); |
527 return; | 530 return; |
528 } | 531 } |
529 | 532 |
530 if (image_index != User::kExternalImageIndex && | 533 if (image_index != User::kExternalImageIndex && |
531 image_index != User::kProfileImageIndex) { | 534 image_index != User::kProfileImageIndex) { |
532 NOTREACHED(); | 535 NOTREACHED(); |
533 return; | 536 return; |
534 } | 537 } |
535 | 538 |
536 std::string image_url_string; | 539 std::string image_url_string; |
(...skipping 19 matching lines...) Expand all Loading... |
556 | 559 |
557 void UserImageManagerImpl::UserLoggedIn(bool user_is_new, | 560 void UserImageManagerImpl::UserLoggedIn(bool user_is_new, |
558 bool user_is_local) { | 561 bool user_is_local) { |
559 const User* user = GetUser(); | 562 const User* user = GetUser(); |
560 if (user_is_new) { | 563 if (user_is_new) { |
561 if (!user_is_local) | 564 if (!user_is_local) |
562 SetInitialUserImage(); | 565 SetInitialUserImage(); |
563 } else { | 566 } else { |
564 UMA_HISTOGRAM_ENUMERATION("UserImage.LoggedIn", | 567 UMA_HISTOGRAM_ENUMERATION("UserImage.LoggedIn", |
565 ImageIndexToHistogramIndex(user->image_index()), | 568 ImageIndexToHistogramIndex(user->image_index()), |
566 kHistogramImagesCount); | 569 user_manager::kHistogramImagesCount); |
567 | 570 |
568 if (!IsUserImageManaged() && user_needs_migration_) { | 571 if (!IsUserImageManaged() && user_needs_migration_) { |
569 const base::DictionaryValue* prefs_images_unsafe = | 572 const base::DictionaryValue* prefs_images_unsafe = |
570 g_browser_process->local_state()->GetDictionary(kUserImages); | 573 g_browser_process->local_state()->GetDictionary(kUserImages); |
571 const base::DictionaryValue* image_properties = NULL; | 574 const base::DictionaryValue* image_properties = NULL; |
572 if (prefs_images_unsafe->GetDictionaryWithoutPathExpansion( | 575 if (prefs_images_unsafe->GetDictionaryWithoutPathExpansion( |
573 user_id(), &image_properties)) { | 576 user_id(), &image_properties)) { |
574 std::string image_path; | 577 std::string image_path; |
575 image_properties->GetString(kImagePathNodeName, &image_path); | 578 image_properties->GetString(kImagePathNodeName, &image_path); |
576 job_.reset(new Job(this)); | 579 job_.reset(new Job(this)); |
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
845 content::Source<UserImageManager>(this), | 848 content::Source<UserImageManager>(this), |
846 content::NotificationService::NoDetails()); | 849 content::NotificationService::NoDetails()); |
847 } | 850 } |
848 | 851 |
849 bool UserImageManagerImpl::IsUserImageManaged() const { | 852 bool UserImageManagerImpl::IsUserImageManaged() const { |
850 return has_managed_image_; | 853 return has_managed_image_; |
851 } | 854 } |
852 | 855 |
853 void UserImageManagerImpl::SetInitialUserImage() { | 856 void UserImageManagerImpl::SetInitialUserImage() { |
854 // Choose a random default image. | 857 // Choose a random default image. |
855 SaveUserDefaultImageIndex(base::RandInt(kFirstDefaultImageIndex, | 858 SaveUserDefaultImageIndex( |
856 kDefaultImagesCount - 1)); | 859 base::RandInt(user_manager::kFirstDefaultImageIndex, |
| 860 user_manager::kDefaultImagesCount - 1)); |
857 } | 861 } |
858 | 862 |
859 void UserImageManagerImpl::TryToInitDownloadedProfileImage() { | 863 void UserImageManagerImpl::TryToInitDownloadedProfileImage() { |
860 const User* user = GetUser(); | 864 const User* user = GetUser(); |
861 if (user->image_index() == User::kProfileImageIndex && | 865 if (user->image_index() == User::kProfileImageIndex && |
862 downloaded_profile_image_.isNull() && | 866 downloaded_profile_image_.isNull() && |
863 !user->image_is_stub()) { | 867 !user->image_is_stub()) { |
864 // Initialize the |downloaded_profile_image_| for the currently logged-in | 868 // Initialize the |downloaded_profile_image_| for the currently logged-in |
865 // user if it has not been initialized already, the user image is the | 869 // user if it has not been initialized already, the user image is the |
866 // profile image and the user image has been loaded successfully. | 870 // profile image and the user image has been loaded successfully. |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
945 if (!prefs_images_unsafe->GetDictionaryWithoutPathExpansion( | 949 if (!prefs_images_unsafe->GetDictionaryWithoutPathExpansion( |
946 user_id(), &image_properties)) { | 950 user_id(), &image_properties)) { |
947 NOTREACHED(); | 951 NOTREACHED(); |
948 return; | 952 return; |
949 } | 953 } |
950 | 954 |
951 int image_index = User::kInvalidImageIndex; | 955 int image_index = User::kInvalidImageIndex; |
952 image_properties->GetInteger(kImageIndexNodeName, &image_index); | 956 image_properties->GetInteger(kImageIndexNodeName, &image_index); |
953 UMA_HISTOGRAM_ENUMERATION("UserImage.Migration", | 957 UMA_HISTOGRAM_ENUMERATION("UserImage.Migration", |
954 ImageIndexToHistogramIndex(image_index), | 958 ImageIndexToHistogramIndex(image_index), |
955 kHistogramImagesCount); | 959 user_manager::kHistogramImagesCount); |
956 | 960 |
957 std::string image_path; | 961 std::string image_path; |
958 image_properties->GetString(kImagePathNodeName, &image_path); | 962 image_properties->GetString(kImagePathNodeName, &image_path); |
959 if (!image_path.empty()) { | 963 if (!image_path.empty()) { |
960 // If an old image exists, delete it and remove |user_id| from the old prefs | 964 // If an old image exists, delete it and remove |user_id| from the old prefs |
961 // dictionary only after the deletion has completed. This ensures that no | 965 // dictionary only after the deletion has completed. This ensures that no |
962 // orphaned image is left behind if the browser crashes before the deletion | 966 // orphaned image is left behind if the browser crashes before the deletion |
963 // has been performed: In that case, local state will be unchanged and the | 967 // has been performed: In that case, local state will be unchanged and the |
964 // migration will be run again on the user's next login. | 968 // migration will be run again on the user's next login. |
965 background_task_runner_->PostTaskAndReply( | 969 background_task_runner_->PostTaskAndReply( |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1002 | 1006 |
1003 bool UserImageManagerImpl::IsUserLoggedInAndRegular() const { | 1007 bool UserImageManagerImpl::IsUserLoggedInAndRegular() const { |
1004 const User* user = GetUser(); | 1008 const User* user = GetUser(); |
1005 if (!user) | 1009 if (!user) |
1006 return false; | 1010 return false; |
1007 return user->is_logged_in() && | 1011 return user->is_logged_in() && |
1008 user->GetType() == user_manager::USER_TYPE_REGULAR; | 1012 user->GetType() == user_manager::USER_TYPE_REGULAR; |
1009 } | 1013 } |
1010 | 1014 |
1011 } // namespace chromeos | 1015 } // namespace chromeos |
OLD | NEW |