OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/profiles/profile_info_cache.h" | 5 #include "chrome/browser/profiles/profile_info_cache.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/i18n/case_conversion.h" | 9 #include "base/i18n/case_conversion.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/path_service.h" | |
13 #include "base/prefs/pref_registry_simple.h" | 12 #include "base/prefs/pref_registry_simple.h" |
14 #include "base/prefs/pref_service.h" | 13 #include "base/prefs/pref_service.h" |
15 #include "base/prefs/scoped_user_pref_update.h" | 14 #include "base/prefs/scoped_user_pref_update.h" |
16 #include "base/rand_util.h" | 15 #include "base/rand_util.h" |
17 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
18 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
19 #include "base/strings/string_piece.h" | 18 #include "base/strings/string_piece.h" |
20 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
21 #include "base/values.h" | 20 #include "base/values.h" |
22 #include "chrome/browser/browser_process.h" | 21 #include "chrome/browser/browser_process.h" |
23 #include "chrome/browser/chrome_notification_types.h" | 22 #include "chrome/browser/chrome_notification_types.h" |
| 23 #include "chrome/browser/profiles/profile_avatar_downloader.h" |
24 #include "chrome/browser/profiles/profile_avatar_icon_util.h" | 24 #include "chrome/browser/profiles/profile_avatar_icon_util.h" |
25 #include "chrome/common/chrome_paths.h" | 25 #include "chrome/browser/profiles/profiles_state.h" |
26 #include "chrome/common/pref_names.h" | 26 #include "chrome/common/pref_names.h" |
27 #include "components/signin/core/common/profile_management_switches.h" | 27 #include "components/signin/core/common/profile_management_switches.h" |
28 #include "content/public/browser/browser_thread.h" | 28 #include "content/public/browser/browser_thread.h" |
29 #include "content/public/browser/notification_service.h" | 29 #include "content/public/browser/notification_service.h" |
30 #include "grit/generated_resources.h" | 30 #include "grit/generated_resources.h" |
31 #include "grit/theme_resources.h" | 31 #include "grit/theme_resources.h" |
32 #include "third_party/skia/include/core/SkBitmap.h" | |
33 #include "ui/base/l10n/l10n_util.h" | 32 #include "ui/base/l10n/l10n_util.h" |
34 #include "ui/base/resource/resource_bundle.h" | 33 #include "ui/base/resource/resource_bundle.h" |
35 #include "ui/gfx/image/image.h" | 34 #include "ui/gfx/image/image.h" |
36 #include "ui/gfx/image/image_util.h" | 35 #include "ui/gfx/image/image_util.h" |
37 | 36 |
38 using content::BrowserThread; | 37 using content::BrowserThread; |
39 | 38 |
40 namespace { | 39 namespace { |
41 | 40 |
42 const char kNameKey[] = "name"; | 41 const char kNameKey[] = "name"; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 IDS_DEFAULT_AVATAR_NAME_21, | 74 IDS_DEFAULT_AVATAR_NAME_21, |
76 IDS_DEFAULT_AVATAR_NAME_22, | 75 IDS_DEFAULT_AVATAR_NAME_22, |
77 IDS_DEFAULT_AVATAR_NAME_23, | 76 IDS_DEFAULT_AVATAR_NAME_23, |
78 IDS_DEFAULT_AVATAR_NAME_24, | 77 IDS_DEFAULT_AVATAR_NAME_24, |
79 IDS_DEFAULT_AVATAR_NAME_25, | 78 IDS_DEFAULT_AVATAR_NAME_25, |
80 IDS_DEFAULT_AVATAR_NAME_26 | 79 IDS_DEFAULT_AVATAR_NAME_26 |
81 }; | 80 }; |
82 | 81 |
83 typedef std::vector<unsigned char> ImageData; | 82 typedef std::vector<unsigned char> ImageData; |
84 | 83 |
85 // Writes |data| to disk and takes ownership of the pointer. On completion | 84 // Writes |data| to disk and takes ownership of the pointer. On successful |
86 // |success| is set to true on success and false on failure. | 85 // completion, it runs |callback|. |
87 void SaveBitmap(ImageData* data, | 86 void SaveBitmap(scoped_ptr<ImageData> data, |
88 const base::FilePath& image_path, | 87 const base::FilePath& image_path, |
89 bool* success) { | 88 const base::Closure& callback) { |
90 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 89 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
91 scoped_ptr<ImageData> data_owner(data); | |
92 *success = false; | |
93 | 90 |
94 // Make sure the destination directory exists. | 91 // Make sure the destination directory exists. |
95 base::FilePath dir = image_path.DirName(); | 92 base::FilePath dir = image_path.DirName(); |
96 if (!base::DirectoryExists(dir) && !base::CreateDirectory(dir)) { | 93 if (!base::DirectoryExists(dir) && !base::CreateDirectory(dir)) { |
97 LOG(ERROR) << "Failed to create parent directory."; | 94 LOG(ERROR) << "Failed to create parent directory."; |
98 return; | 95 return; |
99 } | 96 } |
100 | 97 |
101 if (base::WriteFile(image_path, reinterpret_cast<char*>(&(*data)[0]), | 98 if (base::WriteFile(image_path, reinterpret_cast<char*>(&(*data)[0]), |
102 data->size()) == -1) { | 99 data->size()) == -1) { |
103 LOG(ERROR) << "Failed to save image to file."; | 100 LOG(ERROR) << "Failed to save image to file."; |
104 return; | 101 return; |
105 } | 102 } |
106 | 103 |
107 *success = true; | 104 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, callback); |
108 } | 105 } |
109 | 106 |
110 // Reads a PNG from disk and decodes it. If the bitmap was successfully read | 107 // Reads a PNG from disk and decodes it. If the bitmap was successfully read |
111 // from disk the then |out_image| will contain the bitmap image, otherwise it | 108 // from disk the then |out_image| will contain the bitmap image, otherwise it |
112 // will be NULL. | 109 // will be NULL. |
113 void ReadBitmap(const base::FilePath& image_path, | 110 void ReadBitmap(const base::FilePath& image_path, |
114 gfx::Image** out_image) { | 111 gfx::Image** out_image) { |
115 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
116 *out_image = NULL; | 113 *out_image = NULL; |
117 | 114 |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 sorted_keys_.insert(FindPositionForProfile(it.key(), name), it.key()); | 178 sorted_keys_.insert(FindPositionForProfile(it.key(), name), it.key()); |
182 // TODO(ibraaaa): delete this when 97% of our users are using M31. | 179 // TODO(ibraaaa): delete this when 97% of our users are using M31. |
183 // http://crbug.com/276163 | 180 // http://crbug.com/276163 |
184 bool is_managed = false; | 181 bool is_managed = false; |
185 if (info->GetBoolean(kIsManagedKey, &is_managed)) { | 182 if (info->GetBoolean(kIsManagedKey, &is_managed)) { |
186 info->Remove(kIsManagedKey, NULL); | 183 info->Remove(kIsManagedKey, NULL); |
187 info->SetString(kManagedUserId, is_managed ? "DUMMY_ID" : std::string()); | 184 info->SetString(kManagedUserId, is_managed ? "DUMMY_ID" : std::string()); |
188 } | 185 } |
189 info->SetBoolean(kIsUsingDefaultName, IsDefaultName(name)); | 186 info->SetBoolean(kIsUsingDefaultName, IsDefaultName(name)); |
190 } | 187 } |
| 188 |
| 189 // If needed, start downloading the high-res avatars. |
| 190 if (switches::IsNewAvatarMenu()) { |
| 191 for (size_t i = 0; i < GetNumberOfProfiles(); i++) |
| 192 DownloadHighResAvatar(GetAvatarIconIndexOfProfileAtIndex(i)); |
| 193 } |
191 } | 194 } |
192 | 195 |
193 ProfileInfoCache::~ProfileInfoCache() { | 196 ProfileInfoCache::~ProfileInfoCache() { |
194 STLDeleteContainerPairSecondPointers( | 197 STLDeleteContainerPairSecondPointers( |
195 cached_avatar_images_.begin(), cached_avatar_images_.end()); | 198 cached_avatar_images_.begin(), cached_avatar_images_.end()); |
| 199 STLDeleteContainerPairSecondPointers( |
| 200 avatar_images_downloads_in_progress_.begin(), |
| 201 avatar_images_downloads_in_progress_.end()); |
196 } | 202 } |
197 | 203 |
198 void ProfileInfoCache::AddProfileToCache(const base::FilePath& profile_path, | 204 void ProfileInfoCache::AddProfileToCache(const base::FilePath& profile_path, |
199 const base::string16& name, | 205 const base::string16& name, |
200 const base::string16& username, | 206 const base::string16& username, |
201 size_t icon_index, | 207 size_t icon_index, |
202 const std::string& managed_user_id) { | 208 const std::string& managed_user_id) { |
203 std::string key = CacheKeyFromProfilePath(profile_path); | 209 std::string key = CacheKeyFromProfilePath(profile_path); |
204 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache); | 210 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache); |
205 base::DictionaryValue* cache = update.Get(); | 211 base::DictionaryValue* cache = update.Get(); |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
325 | 331 |
326 const gfx::Image& ProfileInfoCache::GetAvatarIconOfProfileAtIndex( | 332 const gfx::Image& ProfileInfoCache::GetAvatarIconOfProfileAtIndex( |
327 size_t index) const { | 333 size_t index) const { |
328 if (IsUsingGAIAPictureOfProfileAtIndex(index)) { | 334 if (IsUsingGAIAPictureOfProfileAtIndex(index)) { |
329 const gfx::Image* image = GetGAIAPictureOfProfileAtIndex(index); | 335 const gfx::Image* image = GetGAIAPictureOfProfileAtIndex(index); |
330 if (image) | 336 if (image) |
331 return *image; | 337 return *image; |
332 } | 338 } |
333 | 339 |
334 // Use the high resolution version of the avatar if it exists. | 340 // Use the high resolution version of the avatar if it exists. |
335 if (switches::IsNewProfileManagement()) { | 341 if (switches::IsNewAvatarMenu()) { |
336 const gfx::Image* image = GetHighResAvatarOfProfileAtIndex(index); | 342 const gfx::Image* image = GetHighResAvatarOfProfileAtIndex(index); |
337 if (image) | 343 if (image) |
338 return *image; | 344 return *image; |
339 } | 345 } |
340 | 346 |
341 int resource_id = profiles::GetDefaultAvatarIconResourceIDAtIndex( | 347 int resource_id = profiles::GetDefaultAvatarIconResourceIDAtIndex( |
342 GetAvatarIconIndexOfProfileAtIndex(index)); | 348 GetAvatarIconIndexOfProfileAtIndex(index)); |
343 return ResourceBundle::GetSharedInstance().GetNativeImageNamed(resource_id); | 349 return ResourceBundle::GetSharedInstance().GetNativeImageNamed(resource_id); |
344 } | 350 } |
345 | 351 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
384 kGAIAPictureFileNameKey, &file_name); | 390 kGAIAPictureFileNameKey, &file_name); |
385 | 391 |
386 // If the picture is not on disk then return NULL. | 392 // If the picture is not on disk then return NULL. |
387 if (file_name.empty()) | 393 if (file_name.empty()) |
388 return NULL; | 394 return NULL; |
389 | 395 |
390 base::FilePath image_path = path.AppendASCII(file_name); | 396 base::FilePath image_path = path.AppendASCII(file_name); |
391 return LoadAvatarPictureFromPath(key, image_path); | 397 return LoadAvatarPictureFromPath(key, image_path); |
392 } | 398 } |
393 | 399 |
394 const gfx::Image* ProfileInfoCache::GetHighResAvatarOfProfileAtIndex( | 400 bool ProfileInfoCache::IsUsingGAIAPictureOfProfileAtIndex(size_t index) const { |
395 size_t index) const { | 401 bool value = false; |
396 int avatar_index = GetAvatarIconIndexOfProfileAtIndex(index); | 402 GetInfoForProfileAtIndex(index)->GetBoolean(kUseGAIAPictureKey, &value); |
397 std::string key = profiles::GetDefaultAvatarIconFileNameAtIndex(avatar_index); | 403 return value; |
398 | |
399 if (!strcmp(key.c_str(), profiles::GetNoHighResAvatarFileName())) | |
400 return NULL; | |
401 | |
402 base::FilePath user_data_dir; | |
403 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); | |
404 base::FilePath image_path = user_data_dir. | |
405 AppendASCII(profiles::kHighResAvatarFolderName).AppendASCII(key); | |
406 return LoadAvatarPictureFromPath(key, image_path); | |
407 } | |
408 | |
409 const gfx::Image* ProfileInfoCache::LoadAvatarPictureFromPath( | |
410 const std::string& key, | |
411 const base::FilePath& image_path) const { | |
412 // If the picture is already loaded then use it. | |
413 if (cached_avatar_images_.count(key)) { | |
414 if (cached_avatar_images_[key]->IsEmpty()) | |
415 return NULL; | |
416 return cached_avatar_images_[key]; | |
417 } | |
418 | |
419 // If the picture is already being loaded then don't try loading it again. | |
420 if (cached_avatar_images_loading_[key]) | |
421 return NULL; | |
422 cached_avatar_images_loading_[key] = true; | |
423 | |
424 gfx::Image** image = new gfx::Image*; | |
425 BrowserThread::PostTaskAndReply(BrowserThread::FILE, FROM_HERE, | |
426 base::Bind(&ReadBitmap, image_path, image), | |
427 base::Bind(&ProfileInfoCache::OnAvatarPictureLoaded, | |
428 const_cast<ProfileInfoCache*>(this)->AsWeakPtr(), key, image)); | |
429 return NULL; | |
430 } | 404 } |
431 | 405 |
432 bool ProfileInfoCache::ProfileIsManagedAtIndex(size_t index) const { | 406 bool ProfileInfoCache::ProfileIsManagedAtIndex(size_t index) const { |
433 return !GetManagedUserIdOfProfileAtIndex(index).empty(); | 407 return !GetManagedUserIdOfProfileAtIndex(index).empty(); |
434 } | 408 } |
435 | 409 |
436 bool ProfileInfoCache::IsOmittedProfileAtIndex(size_t index) const { | 410 bool ProfileInfoCache::IsOmittedProfileAtIndex(size_t index) const { |
437 bool value = false; | 411 bool value = false; |
438 GetInfoForProfileAtIndex(index)->GetBoolean(kIsOmittedFromProfileListKey, | 412 GetInfoForProfileAtIndex(index)->GetBoolean(kIsOmittedFromProfileListKey, |
439 &value); | 413 &value); |
(...skipping 18 matching lines...) Expand all Loading... |
458 GetInfoForProfileAtIndex(index)->GetBoolean(kProfileIsEphemeral, &value); | 432 GetInfoForProfileAtIndex(index)->GetBoolean(kProfileIsEphemeral, &value); |
459 return value; | 433 return value; |
460 } | 434 } |
461 | 435 |
462 bool ProfileInfoCache::ProfileIsUsingDefaultNameAtIndex(size_t index) const { | 436 bool ProfileInfoCache::ProfileIsUsingDefaultNameAtIndex(size_t index) const { |
463 bool value = false; | 437 bool value = false; |
464 GetInfoForProfileAtIndex(index)->GetBoolean(kIsUsingDefaultName, &value); | 438 GetInfoForProfileAtIndex(index)->GetBoolean(kIsUsingDefaultName, &value); |
465 return value; | 439 return value; |
466 } | 440 } |
467 | 441 |
468 void ProfileInfoCache::OnAvatarPictureLoaded(const std::string& key, | |
469 gfx::Image** image) const { | |
470 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
471 | |
472 cached_avatar_images_loading_[key] = false; | |
473 | |
474 delete cached_avatar_images_[key]; | |
475 if (*image) { | |
476 cached_avatar_images_[key] = *image; | |
477 } else { | |
478 // Place an empty image in the cache to avoid reloading it again. | |
479 cached_avatar_images_[key] = new gfx::Image(); | |
480 } | |
481 delete image; | |
482 | |
483 content::NotificationService::current()->Notify( | |
484 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, | |
485 content::NotificationService::AllSources(), | |
486 content::NotificationService::NoDetails()); | |
487 } | |
488 | |
489 void ProfileInfoCache::OnGAIAPictureSaved(const base::FilePath& path, | |
490 bool* success) const { | |
491 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
492 | |
493 if (*success) { | |
494 content::NotificationService::current()->Notify( | |
495 chrome::NOTIFICATION_PROFILE_CACHE_PICTURE_SAVED, | |
496 content::NotificationService::AllSources(), | |
497 content::NotificationService::NoDetails()); | |
498 } | |
499 delete success; | |
500 } | |
501 | |
502 bool ProfileInfoCache::IsUsingGAIAPictureOfProfileAtIndex( | |
503 size_t index) const { | |
504 bool value = false; | |
505 GetInfoForProfileAtIndex(index)->GetBoolean(kUseGAIAPictureKey, &value); | |
506 return value; | |
507 } | |
508 | |
509 size_t ProfileInfoCache::GetAvatarIconIndexOfProfileAtIndex(size_t index) | 442 size_t ProfileInfoCache::GetAvatarIconIndexOfProfileAtIndex(size_t index) |
510 const { | 443 const { |
511 std::string icon_url; | 444 std::string icon_url; |
512 GetInfoForProfileAtIndex(index)->GetString(kAvatarIconKey, &icon_url); | 445 GetInfoForProfileAtIndex(index)->GetString(kAvatarIconKey, &icon_url); |
513 size_t icon_index = 0; | 446 size_t icon_index = 0; |
514 if (!profiles::IsDefaultAvatarIconUrl(icon_url, &icon_index)) | 447 if (!profiles::IsDefaultAvatarIconUrl(icon_url, &icon_index)) |
515 DLOG(WARNING) << "Unknown avatar icon: " << icon_url; | 448 DLOG(WARNING) << "Unknown avatar icon: " << icon_url; |
516 | 449 |
517 return icon_index; | 450 return icon_index; |
518 } | 451 } |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
688 | 621 |
689 if (!image) { | 622 if (!image) { |
690 // Delete the old bitmap from disk. | 623 // Delete the old bitmap from disk. |
691 if (!old_file_name.empty()) { | 624 if (!old_file_name.empty()) { |
692 base::FilePath image_path = path.AppendASCII(old_file_name); | 625 base::FilePath image_path = path.AppendASCII(old_file_name); |
693 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 626 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
694 base::Bind(&DeleteBitmap, image_path)); | 627 base::Bind(&DeleteBitmap, image_path)); |
695 } | 628 } |
696 } else { | 629 } else { |
697 // Save the new bitmap to disk. | 630 // Save the new bitmap to disk. |
698 cached_avatar_images_[key] = new gfx::Image(*image); | 631 new_file_name = |
699 scoped_ptr<ImageData> data(new ImageData); | 632 old_file_name.empty() ? profiles::kGAIAPictureFileName : old_file_name; |
700 scoped_refptr<base::RefCountedMemory> png_data = image->As1xPNGBytes(); | 633 base::FilePath image_path = path.AppendASCII(new_file_name); |
701 data->assign(png_data->front(), png_data->front() + png_data->size()); | 634 SaveAvatarImageAtPath(image, key, image_path); |
702 if (!data->size()) { | |
703 LOG(ERROR) << "Failed to PNG encode the image."; | |
704 } else { | |
705 new_file_name = old_file_name.empty() ? | |
706 profiles::kGAIAPictureFileName : old_file_name; | |
707 base::FilePath image_path = path.AppendASCII(new_file_name); | |
708 bool* success = new bool; | |
709 BrowserThread::PostTaskAndReply(BrowserThread::FILE, FROM_HERE, | |
710 base::Bind(&SaveBitmap, data.release(), image_path, success), | |
711 base::Bind(&ProfileInfoCache::OnGAIAPictureSaved, AsWeakPtr(), | |
712 path, success)); | |
713 } | |
714 } | 635 } |
715 | 636 |
716 scoped_ptr<base::DictionaryValue> info( | 637 scoped_ptr<base::DictionaryValue> info( |
717 GetInfoForProfileAtIndex(index)->DeepCopy()); | 638 GetInfoForProfileAtIndex(index)->DeepCopy()); |
718 info->SetString(kGAIAPictureFileNameKey, new_file_name); | 639 info->SetString(kGAIAPictureFileNameKey, new_file_name); |
719 // This takes ownership of |info|. | 640 // This takes ownership of |info|. |
720 SetInfoForProfileAtIndex(index, info.release()); | 641 SetInfoForProfileAtIndex(index, info.release()); |
721 | 642 |
722 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, | 643 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, |
723 observer_list_, | 644 observer_list_, |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
802 if (GetNameOfProfileAtIndex(i) == name) { | 723 if (GetNameOfProfileAtIndex(i) == name) { |
803 name_found = true; | 724 name_found = true; |
804 break; | 725 break; |
805 } | 726 } |
806 } | 727 } |
807 if (!name_found) | 728 if (!name_found) |
808 return name; | 729 return name; |
809 } | 730 } |
810 } | 731 } |
811 | 732 |
812 bool ProfileInfoCache::IconIndexIsUnique(size_t icon_index) const { | |
813 for (size_t i = 0; i < GetNumberOfProfiles(); ++i) { | |
814 if (GetAvatarIconIndexOfProfileAtIndex(i) == icon_index) | |
815 return false; | |
816 } | |
817 return true; | |
818 } | |
819 | |
820 bool ProfileInfoCache::ChooseAvatarIconIndexForNewProfile( | |
821 bool allow_generic_icon, | |
822 bool must_be_unique, | |
823 size_t* out_icon_index) const { | |
824 // Always allow all icons for new profiles if using the | |
825 // --new-profile-management flag. | |
826 if (switches::IsNewProfileManagement()) | |
827 allow_generic_icon = true; | |
828 size_t start = allow_generic_icon ? 0 : profiles::GetGenericAvatarIconCount(); | |
829 size_t end = profiles::GetDefaultAvatarIconCount(); | |
830 size_t count = end - start; | |
831 | |
832 int rand = base::RandInt(0, count); | |
833 for (size_t i = 0; i < count; ++i) { | |
834 size_t icon_index = start + (rand + i) % count; | |
835 if (!must_be_unique || IconIndexIsUnique(icon_index)) { | |
836 *out_icon_index = icon_index; | |
837 return true; | |
838 } | |
839 } | |
840 | |
841 return false; | |
842 } | |
843 | |
844 size_t ProfileInfoCache::ChooseAvatarIconIndexForNewProfile() const { | 733 size_t ProfileInfoCache::ChooseAvatarIconIndexForNewProfile() const { |
845 size_t icon_index = 0; | 734 size_t icon_index = 0; |
846 // Try to find a unique, non-generic icon. | 735 // Try to find a unique, non-generic icon. |
847 if (ChooseAvatarIconIndexForNewProfile(false, true, &icon_index)) | 736 if (ChooseAvatarIconIndexForNewProfile(false, true, &icon_index)) |
848 return icon_index; | 737 return icon_index; |
849 // Try to find any unique icon. | 738 // Try to find any unique icon. |
850 if (ChooseAvatarIconIndexForNewProfile(true, true, &icon_index)) | 739 if (ChooseAvatarIconIndexForNewProfile(true, true, &icon_index)) |
851 return icon_index; | 740 return icon_index; |
852 // Settle for any random icon, even if it's not unique. | 741 // Settle for any random icon, even if it's not unique. |
853 if (ChooseAvatarIconIndexForNewProfile(true, false, &icon_index)) | 742 if (ChooseAvatarIconIndexForNewProfile(true, false, &icon_index)) |
854 return icon_index; | 743 return icon_index; |
855 | 744 |
856 NOTREACHED(); | 745 NOTREACHED(); |
857 return 0; | 746 return 0; |
858 } | 747 } |
859 | 748 |
860 const base::FilePath& ProfileInfoCache::GetUserDataDir() const { | 749 const base::FilePath& ProfileInfoCache::GetUserDataDir() const { |
861 return user_data_dir_; | 750 return user_data_dir_; |
862 } | 751 } |
863 | 752 |
| 753 // static |
| 754 std::vector<base::string16> ProfileInfoCache::GetProfileNames() { |
| 755 std::vector<base::string16> names; |
| 756 PrefService* local_state = g_browser_process->local_state(); |
| 757 const base::DictionaryValue* cache = local_state->GetDictionary( |
| 758 prefs::kProfileInfoCache); |
| 759 base::string16 name; |
| 760 for (base::DictionaryValue::Iterator it(*cache); !it.IsAtEnd(); |
| 761 it.Advance()) { |
| 762 const base::DictionaryValue* info = NULL; |
| 763 it.value().GetAsDictionary(&info); |
| 764 info->GetString(kNameKey, &name); |
| 765 names.push_back(name); |
| 766 } |
| 767 return names; |
| 768 } |
| 769 |
| 770 // static |
| 771 void ProfileInfoCache::RegisterPrefs(PrefRegistrySimple* registry) { |
| 772 registry->RegisterDictionaryPref(prefs::kProfileInfoCache); |
| 773 } |
| 774 |
| 775 void ProfileInfoCache::DownloadHighResAvatar(size_t icon_index) { |
| 776 // TODO(noms): We should check whether the file already exists on disk |
| 777 // before trying to re-download it. For now, since this is behind a flag and |
| 778 // the resources are still changing, re-download it every time the profile |
| 779 // avatar changes, to make sure we have the latest copy. |
| 780 std::string file_name = profiles::GetDefaultAvatarIconFileNameAtIndex( |
| 781 icon_index); |
| 782 // If the file is already being downloaded, don't start another download. |
| 783 if (avatar_images_downloads_in_progress_[file_name]) |
| 784 return; |
| 785 |
| 786 // Start the download for this file. The cache takes ownership of the |
| 787 // |avatar_downloader|, which will be deleted when the download completes, or |
| 788 // if that never happens, when the ProfileInfoCache is destroyed. |
| 789 ProfileAvatarDownloader* avatar_downloader = new ProfileAvatarDownloader( |
| 790 icon_index, |
| 791 this); |
| 792 avatar_images_downloads_in_progress_[file_name] = avatar_downloader; |
| 793 avatar_downloader->Start(); |
| 794 } |
| 795 |
| 796 void ProfileInfoCache::SaveAvatarImageAtPath( |
| 797 const gfx::Image* image, |
| 798 const std::string& key, |
| 799 const base::FilePath& image_path) { |
| 800 cached_avatar_images_[key] = new gfx::Image(*image); |
| 801 |
| 802 scoped_ptr<ImageData> data(new ImageData); |
| 803 scoped_refptr<base::RefCountedMemory> png_data = image->As1xPNGBytes(); |
| 804 data->assign(png_data->front(), png_data->front() + png_data->size()); |
| 805 |
| 806 if (!data->size()) { |
| 807 LOG(ERROR) << "Failed to PNG encode the image."; |
| 808 } else { |
| 809 base::Closure callback = base::Bind( |
| 810 &ProfileInfoCache::OnAvatarPictureSaved, AsWeakPtr(), key); |
| 811 |
| 812 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 813 base::Bind(&SaveBitmap, base::Passed(&data), image_path, callback)); |
| 814 } |
| 815 } |
| 816 |
864 const base::DictionaryValue* ProfileInfoCache::GetInfoForProfileAtIndex( | 817 const base::DictionaryValue* ProfileInfoCache::GetInfoForProfileAtIndex( |
865 size_t index) const { | 818 size_t index) const { |
866 DCHECK_LT(index, GetNumberOfProfiles()); | 819 DCHECK_LT(index, GetNumberOfProfiles()); |
867 const base::DictionaryValue* cache = | 820 const base::DictionaryValue* cache = |
868 prefs_->GetDictionary(prefs::kProfileInfoCache); | 821 prefs_->GetDictionary(prefs::kProfileInfoCache); |
869 const base::DictionaryValue* info = NULL; | 822 const base::DictionaryValue* info = NULL; |
870 cache->GetDictionaryWithoutPathExpansion(sorted_keys_[index], &info); | 823 cache->GetDictionaryWithoutPathExpansion(sorted_keys_[index], &info); |
871 return info; | 824 return info; |
872 } | 825 } |
873 | 826 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
907 return sorted_keys_.begin() + i; | 860 return sorted_keys_.begin() + i; |
908 if (name_compare == 0) { | 861 if (name_compare == 0) { |
909 int key_compare = search_key.compare(sorted_keys_[i]); | 862 int key_compare = search_key.compare(sorted_keys_[i]); |
910 if (key_compare < 0) | 863 if (key_compare < 0) |
911 return sorted_keys_.begin() + i; | 864 return sorted_keys_.begin() + i; |
912 } | 865 } |
913 } | 866 } |
914 return sorted_keys_.end(); | 867 return sorted_keys_.end(); |
915 } | 868 } |
916 | 869 |
| 870 bool ProfileInfoCache::IconIndexIsUnique(size_t icon_index) const { |
| 871 for (size_t i = 0; i < GetNumberOfProfiles(); ++i) { |
| 872 if (GetAvatarIconIndexOfProfileAtIndex(i) == icon_index) |
| 873 return false; |
| 874 } |
| 875 return true; |
| 876 } |
| 877 |
| 878 bool ProfileInfoCache::ChooseAvatarIconIndexForNewProfile( |
| 879 bool allow_generic_icon, |
| 880 bool must_be_unique, |
| 881 size_t* out_icon_index) const { |
| 882 // Always allow all icons for new profiles if using the |
| 883 // --new-profile-management flag. |
| 884 if (switches::IsNewProfileManagement()) |
| 885 allow_generic_icon = true; |
| 886 size_t start = allow_generic_icon ? 0 : profiles::GetGenericAvatarIconCount(); |
| 887 size_t end = profiles::GetDefaultAvatarIconCount(); |
| 888 size_t count = end - start; |
| 889 |
| 890 int rand = base::RandInt(0, count); |
| 891 for (size_t i = 0; i < count; ++i) { |
| 892 size_t icon_index = start + (rand + i) % count; |
| 893 if (!must_be_unique || IconIndexIsUnique(icon_index)) { |
| 894 *out_icon_index = icon_index; |
| 895 return true; |
| 896 } |
| 897 } |
| 898 |
| 899 return false; |
| 900 } |
| 901 |
917 void ProfileInfoCache::UpdateSortForProfileIndex(size_t index) { | 902 void ProfileInfoCache::UpdateSortForProfileIndex(size_t index) { |
918 base::string16 name = GetNameOfProfileAtIndex(index); | 903 base::string16 name = GetNameOfProfileAtIndex(index); |
919 | 904 |
920 // Remove and reinsert key in |sorted_keys_| to alphasort. | 905 // Remove and reinsert key in |sorted_keys_| to alphasort. |
921 std::string key = CacheKeyFromProfilePath(GetPathOfProfileAtIndex(index)); | 906 std::string key = CacheKeyFromProfilePath(GetPathOfProfileAtIndex(index)); |
922 std::vector<std::string>::iterator key_it = | 907 std::vector<std::string>::iterator key_it = |
923 std::find(sorted_keys_.begin(), sorted_keys_.end(), key); | 908 std::find(sorted_keys_.begin(), sorted_keys_.end(), key); |
924 DCHECK(key_it != sorted_keys_.end()); | 909 DCHECK(key_it != sorted_keys_.end()); |
925 sorted_keys_.erase(key_it); | 910 sorted_keys_.erase(key_it); |
926 sorted_keys_.insert(FindPositionForProfile(key, name), key); | 911 sorted_keys_.insert(FindPositionForProfile(key, name), key); |
927 | 912 |
928 content::NotificationService::current()->Notify( | 913 content::NotificationService::current()->Notify( |
929 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, | 914 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, |
930 content::NotificationService::AllSources(), | 915 content::NotificationService::AllSources(), |
931 content::NotificationService::NoDetails()); | 916 content::NotificationService::NoDetails()); |
932 } | 917 } |
933 | 918 |
934 // static | 919 const gfx::Image* ProfileInfoCache::GetHighResAvatarOfProfileAtIndex( |
935 std::vector<base::string16> ProfileInfoCache::GetProfileNames() { | 920 size_t index) const { |
936 std::vector<base::string16> names; | 921 int avatar_index = GetAvatarIconIndexOfProfileAtIndex(index); |
937 PrefService* local_state = g_browser_process->local_state(); | 922 std::string key = profiles::GetDefaultAvatarIconFileNameAtIndex(avatar_index); |
938 const base::DictionaryValue* cache = local_state->GetDictionary( | 923 |
939 prefs::kProfileInfoCache); | 924 if (!strcmp(key.c_str(), profiles::GetNoHighResAvatarFileName())) |
940 base::string16 name; | 925 return NULL; |
941 for (base::DictionaryValue::Iterator it(*cache); !it.IsAtEnd(); | 926 |
942 it.Advance()) { | 927 base::FilePath image_path = |
943 const base::DictionaryValue* info = NULL; | 928 profiles::GetPathOfHighResAvatarAtIndex(avatar_index); |
944 it.value().GetAsDictionary(&info); | 929 return LoadAvatarPictureFromPath(key, image_path); |
945 info->GetString(kNameKey, &name); | |
946 names.push_back(name); | |
947 } | |
948 return names; | |
949 } | 930 } |
950 | 931 |
951 // static | 932 const gfx::Image* ProfileInfoCache::LoadAvatarPictureFromPath( |
952 void ProfileInfoCache::RegisterPrefs(PrefRegistrySimple* registry) { | 933 const std::string& key, |
953 registry->RegisterDictionaryPref(prefs::kProfileInfoCache); | 934 const base::FilePath& image_path) const { |
| 935 // If the picture is already loaded then use it. |
| 936 if (cached_avatar_images_.count(key)) { |
| 937 if (cached_avatar_images_[key]->IsEmpty()) |
| 938 return NULL; |
| 939 return cached_avatar_images_[key]; |
| 940 } |
| 941 |
| 942 // If the picture is already being loaded then don't try loading it again. |
| 943 if (cached_avatar_images_loading_[key]) |
| 944 return NULL; |
| 945 cached_avatar_images_loading_[key] = true; |
| 946 |
| 947 gfx::Image** image = new gfx::Image*; |
| 948 BrowserThread::PostTaskAndReply(BrowserThread::FILE, FROM_HERE, |
| 949 base::Bind(&ReadBitmap, image_path, image), |
| 950 base::Bind(&ProfileInfoCache::OnAvatarPictureLoaded, |
| 951 const_cast<ProfileInfoCache*>(this)->AsWeakPtr(), key, image)); |
| 952 return NULL; |
954 } | 953 } |
| 954 |
| 955 void ProfileInfoCache::OnAvatarPictureLoaded(const std::string& key, |
| 956 gfx::Image** image) const { |
| 957 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 958 |
| 959 cached_avatar_images_loading_[key] = false; |
| 960 delete cached_avatar_images_[key]; |
| 961 |
| 962 if (*image) { |
| 963 cached_avatar_images_[key] = *image; |
| 964 } else { |
| 965 // Place an empty image in the cache to avoid reloading it again. |
| 966 cached_avatar_images_[key] = new gfx::Image(); |
| 967 } |
| 968 delete image; |
| 969 |
| 970 content::NotificationService::current()->Notify( |
| 971 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, |
| 972 content::NotificationService::AllSources(), |
| 973 content::NotificationService::NoDetails()); |
| 974 } |
| 975 |
| 976 void ProfileInfoCache::OnAvatarPictureSaved(const std::string& file_name) { |
| 977 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 978 |
| 979 content::NotificationService::current()->Notify( |
| 980 chrome::NOTIFICATION_PROFILE_CACHE_PICTURE_SAVED, |
| 981 content::NotificationService::AllSources(), |
| 982 content::NotificationService::NoDetails()); |
| 983 |
| 984 // Remove the file from the list of downloads in progress. Note that this list |
| 985 // only contains the high resolution avatars, and not the Gaia profile images. |
| 986 if (!avatar_images_downloads_in_progress_[file_name]) |
| 987 return; |
| 988 |
| 989 delete avatar_images_downloads_in_progress_[file_name]; |
| 990 avatar_images_downloads_in_progress_[file_name] = NULL; |
| 991 } |
OLD | NEW |