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(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); | 90 scoped_ptr<ImageData> data_owner(data); |
92 *success = false; | |
93 | 91 |
94 // Make sure the destination directory exists. | 92 // Make sure the destination directory exists. |
95 base::FilePath dir = image_path.DirName(); | 93 base::FilePath dir = image_path.DirName(); |
96 if (!base::DirectoryExists(dir) && !base::CreateDirectory(dir)) { | 94 if (!base::DirectoryExists(dir) && !base::CreateDirectory(dir)) { |
97 LOG(ERROR) << "Failed to create parent directory."; | 95 LOG(ERROR) << "Failed to create parent directory."; |
98 return; | 96 return; |
99 } | 97 } |
100 | 98 |
101 if (base::WriteFile(image_path, reinterpret_cast<char*>(&(*data)[0]), | 99 if (base::WriteFile(image_path, reinterpret_cast<char*>(&(*data)[0]), |
102 data->size()) == -1) { | 100 data->size()) == -1) { |
103 LOG(ERROR) << "Failed to save image to file."; | 101 LOG(ERROR) << "Failed to save image to file."; |
104 return; | 102 return; |
105 } | 103 } |
106 | 104 |
107 *success = true; | 105 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, callback); |
108 } | 106 } |
109 | 107 |
110 // Reads a PNG from disk and decodes it. If the bitmap was successfully read | 108 // 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 | 109 // from disk the then |out_image| will contain the bitmap image, otherwise it |
112 // will be NULL. | 110 // will be NULL. |
113 void ReadBitmap(const base::FilePath& image_path, | 111 void ReadBitmap(const base::FilePath& image_path, |
114 gfx::Image** out_image) { | 112 gfx::Image** out_image) { |
115 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 113 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
116 *out_image = NULL; | 114 *out_image = NULL; |
117 | 115 |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
181 sorted_keys_.insert(FindPositionForProfile(it.key(), name), it.key()); | 179 sorted_keys_.insert(FindPositionForProfile(it.key(), name), it.key()); |
182 // TODO(ibraaaa): delete this when 97% of our users are using M31. | 180 // TODO(ibraaaa): delete this when 97% of our users are using M31. |
183 // http://crbug.com/276163 | 181 // http://crbug.com/276163 |
184 bool is_managed = false; | 182 bool is_managed = false; |
185 if (info->GetBoolean(kIsManagedKey, &is_managed)) { | 183 if (info->GetBoolean(kIsManagedKey, &is_managed)) { |
186 info->Remove(kIsManagedKey, NULL); | 184 info->Remove(kIsManagedKey, NULL); |
187 info->SetString(kManagedUserId, is_managed ? "DUMMY_ID" : std::string()); | 185 info->SetString(kManagedUserId, is_managed ? "DUMMY_ID" : std::string()); |
188 } | 186 } |
189 info->SetBoolean(kIsUsingDefaultName, IsDefaultName(name)); | 187 info->SetBoolean(kIsUsingDefaultName, IsDefaultName(name)); |
190 } | 188 } |
189 | |
190 // If needed, start downloading the high-res avatars. | |
191 if (switches::IsNewAvatarMenu()) { | |
192 for (size_t i = 0; i < GetNumberOfProfiles(); i++) { | |
msw
2014/04/29 20:11:44
nit: remove unnecessary braces.
noms (inactive)
2014/04/29 20:29:10
Done.
| |
193 DownloadHighResAvatar(GetAvatarIconIndexOfProfileAtIndex(i)); | |
194 } | |
195 } | |
191 } | 196 } |
192 | 197 |
193 ProfileInfoCache::~ProfileInfoCache() { | 198 ProfileInfoCache::~ProfileInfoCache() { |
194 STLDeleteContainerPairSecondPointers( | 199 STLDeleteContainerPairSecondPointers( |
195 cached_avatar_images_.begin(), cached_avatar_images_.end()); | 200 cached_avatar_images_.begin(), cached_avatar_images_.end()); |
201 STLDeleteContainerPairSecondPointers( | |
202 avatar_images_downloads_in_progress_.begin(), | |
203 avatar_images_downloads_in_progress_.end()); | |
196 } | 204 } |
197 | 205 |
198 void ProfileInfoCache::AddProfileToCache(const base::FilePath& profile_path, | 206 void ProfileInfoCache::AddProfileToCache(const base::FilePath& profile_path, |
199 const base::string16& name, | 207 const base::string16& name, |
200 const base::string16& username, | 208 const base::string16& username, |
201 size_t icon_index, | 209 size_t icon_index, |
202 const std::string& managed_user_id) { | 210 const std::string& managed_user_id) { |
203 std::string key = CacheKeyFromProfilePath(profile_path); | 211 std::string key = CacheKeyFromProfilePath(profile_path); |
204 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache); | 212 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache); |
205 base::DictionaryValue* cache = update.Get(); | 213 base::DictionaryValue* cache = update.Get(); |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
325 | 333 |
326 const gfx::Image& ProfileInfoCache::GetAvatarIconOfProfileAtIndex( | 334 const gfx::Image& ProfileInfoCache::GetAvatarIconOfProfileAtIndex( |
327 size_t index) const { | 335 size_t index) const { |
328 if (IsUsingGAIAPictureOfProfileAtIndex(index)) { | 336 if (IsUsingGAIAPictureOfProfileAtIndex(index)) { |
329 const gfx::Image* image = GetGAIAPictureOfProfileAtIndex(index); | 337 const gfx::Image* image = GetGAIAPictureOfProfileAtIndex(index); |
330 if (image) | 338 if (image) |
331 return *image; | 339 return *image; |
332 } | 340 } |
333 | 341 |
334 // Use the high resolution version of the avatar if it exists. | 342 // Use the high resolution version of the avatar if it exists. |
335 if (switches::IsNewProfileManagement()) { | 343 if (switches::IsNewAvatarMenu()) { |
336 const gfx::Image* image = GetHighResAvatarOfProfileAtIndex(index); | 344 const gfx::Image* image = GetHighResAvatarOfProfileAtIndex(index); |
337 if (image) | 345 if (image) |
338 return *image; | 346 return *image; |
339 } | 347 } |
340 | 348 |
341 int resource_id = profiles::GetDefaultAvatarIconResourceIDAtIndex( | 349 int resource_id = profiles::GetDefaultAvatarIconResourceIDAtIndex( |
342 GetAvatarIconIndexOfProfileAtIndex(index)); | 350 GetAvatarIconIndexOfProfileAtIndex(index)); |
343 return ResourceBundle::GetSharedInstance().GetNativeImageNamed(resource_id); | 351 return ResourceBundle::GetSharedInstance().GetNativeImageNamed(resource_id); |
344 } | 352 } |
345 | 353 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
384 kGAIAPictureFileNameKey, &file_name); | 392 kGAIAPictureFileNameKey, &file_name); |
385 | 393 |
386 // If the picture is not on disk then return NULL. | 394 // If the picture is not on disk then return NULL. |
387 if (file_name.empty()) | 395 if (file_name.empty()) |
388 return NULL; | 396 return NULL; |
389 | 397 |
390 base::FilePath image_path = path.AppendASCII(file_name); | 398 base::FilePath image_path = path.AppendASCII(file_name); |
391 return LoadAvatarPictureFromPath(key, image_path); | 399 return LoadAvatarPictureFromPath(key, image_path); |
392 } | 400 } |
393 | 401 |
394 const gfx::Image* ProfileInfoCache::GetHighResAvatarOfProfileAtIndex( | 402 bool ProfileInfoCache::IsUsingGAIAPictureOfProfileAtIndex( |
395 size_t index) const { | 403 size_t index) const { |
msw
2014/04/29 20:11:44
nit: this fits on the line above.
noms (inactive)
2014/04/29 20:29:10
Done.
| |
396 int avatar_index = GetAvatarIconIndexOfProfileAtIndex(index); | 404 bool value = false; |
397 std::string key = profiles::GetDefaultAvatarIconFileNameAtIndex(avatar_index); | 405 GetInfoForProfileAtIndex(index)->GetBoolean(kUseGAIAPictureKey, &value); |
398 | 406 return value; |
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 } | 407 } |
431 | 408 |
432 bool ProfileInfoCache::ProfileIsManagedAtIndex(size_t index) const { | 409 bool ProfileInfoCache::ProfileIsManagedAtIndex(size_t index) const { |
433 return !GetManagedUserIdOfProfileAtIndex(index).empty(); | 410 return !GetManagedUserIdOfProfileAtIndex(index).empty(); |
434 } | 411 } |
435 | 412 |
436 bool ProfileInfoCache::IsOmittedProfileAtIndex(size_t index) const { | 413 bool ProfileInfoCache::IsOmittedProfileAtIndex(size_t index) const { |
437 bool value = false; | 414 bool value = false; |
438 GetInfoForProfileAtIndex(index)->GetBoolean(kIsOmittedFromProfileListKey, | 415 GetInfoForProfileAtIndex(index)->GetBoolean(kIsOmittedFromProfileListKey, |
439 &value); | 416 &value); |
(...skipping 18 matching lines...) Expand all Loading... | |
458 GetInfoForProfileAtIndex(index)->GetBoolean(kProfileIsEphemeral, &value); | 435 GetInfoForProfileAtIndex(index)->GetBoolean(kProfileIsEphemeral, &value); |
459 return value; | 436 return value; |
460 } | 437 } |
461 | 438 |
462 bool ProfileInfoCache::ProfileIsUsingDefaultNameAtIndex(size_t index) const { | 439 bool ProfileInfoCache::ProfileIsUsingDefaultNameAtIndex(size_t index) const { |
463 bool value = false; | 440 bool value = false; |
464 GetInfoForProfileAtIndex(index)->GetBoolean(kIsUsingDefaultName, &value); | 441 GetInfoForProfileAtIndex(index)->GetBoolean(kIsUsingDefaultName, &value); |
465 return value; | 442 return value; |
466 } | 443 } |
467 | 444 |
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) | 445 size_t ProfileInfoCache::GetAvatarIconIndexOfProfileAtIndex(size_t index) |
510 const { | 446 const { |
511 std::string icon_url; | 447 std::string icon_url; |
512 GetInfoForProfileAtIndex(index)->GetString(kAvatarIconKey, &icon_url); | 448 GetInfoForProfileAtIndex(index)->GetString(kAvatarIconKey, &icon_url); |
513 size_t icon_index = 0; | 449 size_t icon_index = 0; |
514 if (!profiles::IsDefaultAvatarIconUrl(icon_url, &icon_index)) | 450 if (!profiles::IsDefaultAvatarIconUrl(icon_url, &icon_index)) |
515 DLOG(WARNING) << "Unknown avatar icon: " << icon_url; | 451 DLOG(WARNING) << "Unknown avatar icon: " << icon_url; |
516 | 452 |
517 return icon_index; | 453 return icon_index; |
518 } | 454 } |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
688 | 624 |
689 if (!image) { | 625 if (!image) { |
690 // Delete the old bitmap from disk. | 626 // Delete the old bitmap from disk. |
691 if (!old_file_name.empty()) { | 627 if (!old_file_name.empty()) { |
692 base::FilePath image_path = path.AppendASCII(old_file_name); | 628 base::FilePath image_path = path.AppendASCII(old_file_name); |
693 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 629 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
694 base::Bind(&DeleteBitmap, image_path)); | 630 base::Bind(&DeleteBitmap, image_path)); |
695 } | 631 } |
696 } else { | 632 } else { |
697 // Save the new bitmap to disk. | 633 // Save the new bitmap to disk. |
698 cached_avatar_images_[key] = new gfx::Image(*image); | 634 new_file_name = |
699 scoped_ptr<ImageData> data(new ImageData); | 635 old_file_name.empty() ? profiles::kGAIAPictureFileName : old_file_name; |
700 scoped_refptr<base::RefCountedMemory> png_data = image->As1xPNGBytes(); | 636 base::FilePath image_path = path.AppendASCII(new_file_name); |
701 data->assign(png_data->front(), png_data->front() + png_data->size()); | 637 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 } | 638 } |
715 | 639 |
716 scoped_ptr<base::DictionaryValue> info( | 640 scoped_ptr<base::DictionaryValue> info( |
717 GetInfoForProfileAtIndex(index)->DeepCopy()); | 641 GetInfoForProfileAtIndex(index)->DeepCopy()); |
718 info->SetString(kGAIAPictureFileNameKey, new_file_name); | 642 info->SetString(kGAIAPictureFileNameKey, new_file_name); |
719 // This takes ownership of |info|. | 643 // This takes ownership of |info|. |
720 SetInfoForProfileAtIndex(index, info.release()); | 644 SetInfoForProfileAtIndex(index, info.release()); |
721 | 645 |
722 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, | 646 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, |
723 observer_list_, | 647 observer_list_, |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
802 if (GetNameOfProfileAtIndex(i) == name) { | 726 if (GetNameOfProfileAtIndex(i) == name) { |
803 name_found = true; | 727 name_found = true; |
804 break; | 728 break; |
805 } | 729 } |
806 } | 730 } |
807 if (!name_found) | 731 if (!name_found) |
808 return name; | 732 return name; |
809 } | 733 } |
810 } | 734 } |
811 | 735 |
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 { | 736 size_t ProfileInfoCache::ChooseAvatarIconIndexForNewProfile() const { |
845 size_t icon_index = 0; | 737 size_t icon_index = 0; |
846 // Try to find a unique, non-generic icon. | 738 // Try to find a unique, non-generic icon. |
847 if (ChooseAvatarIconIndexForNewProfile(false, true, &icon_index)) | 739 if (ChooseAvatarIconIndexForNewProfile(false, true, &icon_index)) |
848 return icon_index; | 740 return icon_index; |
849 // Try to find any unique icon. | 741 // Try to find any unique icon. |
850 if (ChooseAvatarIconIndexForNewProfile(true, true, &icon_index)) | 742 if (ChooseAvatarIconIndexForNewProfile(true, true, &icon_index)) |
851 return icon_index; | 743 return icon_index; |
852 // Settle for any random icon, even if it's not unique. | 744 // Settle for any random icon, even if it's not unique. |
853 if (ChooseAvatarIconIndexForNewProfile(true, false, &icon_index)) | 745 if (ChooseAvatarIconIndexForNewProfile(true, false, &icon_index)) |
854 return icon_index; | 746 return icon_index; |
855 | 747 |
856 NOTREACHED(); | 748 NOTREACHED(); |
857 return 0; | 749 return 0; |
858 } | 750 } |
859 | 751 |
860 const base::FilePath& ProfileInfoCache::GetUserDataDir() const { | 752 const base::FilePath& ProfileInfoCache::GetUserDataDir() const { |
861 return user_data_dir_; | 753 return user_data_dir_; |
862 } | 754 } |
863 | 755 |
756 // static | |
757 std::vector<base::string16> ProfileInfoCache::GetProfileNames() { | |
758 std::vector<base::string16> names; | |
759 PrefService* local_state = g_browser_process->local_state(); | |
760 const base::DictionaryValue* cache = local_state->GetDictionary( | |
761 prefs::kProfileInfoCache); | |
762 base::string16 name; | |
763 for (base::DictionaryValue::Iterator it(*cache); !it.IsAtEnd(); | |
764 it.Advance()) { | |
765 const base::DictionaryValue* info = NULL; | |
766 it.value().GetAsDictionary(&info); | |
767 info->GetString(kNameKey, &name); | |
768 names.push_back(name); | |
769 } | |
770 return names; | |
771 } | |
772 | |
773 // static | |
774 void ProfileInfoCache::RegisterPrefs(PrefRegistrySimple* registry) { | |
775 registry->RegisterDictionaryPref(prefs::kProfileInfoCache); | |
776 } | |
777 | |
778 void ProfileInfoCache::DownloadHighResAvatar(size_t icon_index) { | |
779 std::string file_name = profiles::GetDefaultAvatarIconFileNameAtIndex( | |
780 icon_index); | |
781 // If the file is already being downloaded, don't start another download. | |
782 if (avatar_images_downloads_in_progress_[file_name]) | |
783 return; | |
784 | |
785 // Start the download for this file. The cache takes ownership of the | |
786 // |avatar_downloader|, which will be deleted when the download completes, or | |
787 // if that never happens, when the ProfileInfoCache is destroyed. | |
788 ProfileAvatarDownloader* avatar_downloader = new ProfileAvatarDownloader( | |
789 icon_index, | |
790 this); | |
791 avatar_images_downloads_in_progress_[file_name] = avatar_downloader; | |
792 avatar_downloader->Start(); | |
793 } | |
794 | |
795 void ProfileInfoCache::SaveAvatarImageAtPath( | |
796 const gfx::Image* image, | |
797 const std::string& key, | |
798 const base::FilePath& image_path) { | |
799 cached_avatar_images_[key] = new gfx::Image(*image); | |
800 | |
801 scoped_ptr<ImageData> data(new ImageData); | |
802 scoped_refptr<base::RefCountedMemory> png_data = image->As1xPNGBytes(); | |
803 data->assign(png_data->front(), png_data->front() + png_data->size()); | |
804 | |
805 if (!data->size()) { | |
806 LOG(ERROR) << "Failed to PNG encode the image."; | |
807 } else { | |
808 base::Closure callback = base::Bind( | |
809 &ProfileInfoCache::OnAvatarPictureSaved, AsWeakPtr(), key); | |
810 | |
811 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | |
812 base::Bind(&SaveBitmap, data.release(), image_path, callback)); | |
msw
2014/04/29 20:11:44
nit: do base::Passed(&data) and have SaveBitmap ta
noms (inactive)
2014/04/29 20:29:10
Done.
| |
813 } | |
814 } | |
815 | |
864 const base::DictionaryValue* ProfileInfoCache::GetInfoForProfileAtIndex( | 816 const base::DictionaryValue* ProfileInfoCache::GetInfoForProfileAtIndex( |
865 size_t index) const { | 817 size_t index) const { |
866 DCHECK_LT(index, GetNumberOfProfiles()); | 818 DCHECK_LT(index, GetNumberOfProfiles()); |
867 const base::DictionaryValue* cache = | 819 const base::DictionaryValue* cache = |
868 prefs_->GetDictionary(prefs::kProfileInfoCache); | 820 prefs_->GetDictionary(prefs::kProfileInfoCache); |
869 const base::DictionaryValue* info = NULL; | 821 const base::DictionaryValue* info = NULL; |
870 cache->GetDictionaryWithoutPathExpansion(sorted_keys_[index], &info); | 822 cache->GetDictionaryWithoutPathExpansion(sorted_keys_[index], &info); |
871 return info; | 823 return info; |
872 } | 824 } |
873 | 825 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
907 return sorted_keys_.begin() + i; | 859 return sorted_keys_.begin() + i; |
908 if (name_compare == 0) { | 860 if (name_compare == 0) { |
909 int key_compare = search_key.compare(sorted_keys_[i]); | 861 int key_compare = search_key.compare(sorted_keys_[i]); |
910 if (key_compare < 0) | 862 if (key_compare < 0) |
911 return sorted_keys_.begin() + i; | 863 return sorted_keys_.begin() + i; |
912 } | 864 } |
913 } | 865 } |
914 return sorted_keys_.end(); | 866 return sorted_keys_.end(); |
915 } | 867 } |
916 | 868 |
869 bool ProfileInfoCache::IconIndexIsUnique(size_t icon_index) const { | |
870 for (size_t i = 0; i < GetNumberOfProfiles(); ++i) { | |
871 if (GetAvatarIconIndexOfProfileAtIndex(i) == icon_index) | |
872 return false; | |
873 } | |
874 return true; | |
875 } | |
876 | |
877 bool ProfileInfoCache::ChooseAvatarIconIndexForNewProfile( | |
878 bool allow_generic_icon, | |
879 bool must_be_unique, | |
880 size_t* out_icon_index) const { | |
881 // Always allow all icons for new profiles if using the | |
882 // --new-profile-management flag. | |
883 if (switches::IsNewProfileManagement()) | |
884 allow_generic_icon = true; | |
885 size_t start = allow_generic_icon ? 0 : profiles::GetGenericAvatarIconCount(); | |
886 size_t end = profiles::GetDefaultAvatarIconCount(); | |
887 size_t count = end - start; | |
888 | |
889 int rand = base::RandInt(0, count); | |
890 for (size_t i = 0; i < count; ++i) { | |
891 size_t icon_index = start + (rand + i) % count; | |
892 if (!must_be_unique || IconIndexIsUnique(icon_index)) { | |
893 *out_icon_index = icon_index; | |
894 return true; | |
895 } | |
896 } | |
897 | |
898 return false; | |
899 } | |
900 | |
917 void ProfileInfoCache::UpdateSortForProfileIndex(size_t index) { | 901 void ProfileInfoCache::UpdateSortForProfileIndex(size_t index) { |
918 base::string16 name = GetNameOfProfileAtIndex(index); | 902 base::string16 name = GetNameOfProfileAtIndex(index); |
919 | 903 |
920 // Remove and reinsert key in |sorted_keys_| to alphasort. | 904 // Remove and reinsert key in |sorted_keys_| to alphasort. |
921 std::string key = CacheKeyFromProfilePath(GetPathOfProfileAtIndex(index)); | 905 std::string key = CacheKeyFromProfilePath(GetPathOfProfileAtIndex(index)); |
922 std::vector<std::string>::iterator key_it = | 906 std::vector<std::string>::iterator key_it = |
923 std::find(sorted_keys_.begin(), sorted_keys_.end(), key); | 907 std::find(sorted_keys_.begin(), sorted_keys_.end(), key); |
924 DCHECK(key_it != sorted_keys_.end()); | 908 DCHECK(key_it != sorted_keys_.end()); |
925 sorted_keys_.erase(key_it); | 909 sorted_keys_.erase(key_it); |
926 sorted_keys_.insert(FindPositionForProfile(key, name), key); | 910 sorted_keys_.insert(FindPositionForProfile(key, name), key); |
927 | 911 |
928 content::NotificationService::current()->Notify( | 912 content::NotificationService::current()->Notify( |
929 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, | 913 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, |
930 content::NotificationService::AllSources(), | 914 content::NotificationService::AllSources(), |
931 content::NotificationService::NoDetails()); | 915 content::NotificationService::NoDetails()); |
932 } | 916 } |
933 | 917 |
934 // static | 918 const gfx::Image* ProfileInfoCache::GetHighResAvatarOfProfileAtIndex( |
935 std::vector<base::string16> ProfileInfoCache::GetProfileNames() { | 919 size_t index) const { |
936 std::vector<base::string16> names; | 920 int avatar_index = GetAvatarIconIndexOfProfileAtIndex(index); |
937 PrefService* local_state = g_browser_process->local_state(); | 921 std::string key = profiles::GetDefaultAvatarIconFileNameAtIndex(avatar_index); |
938 const base::DictionaryValue* cache = local_state->GetDictionary( | 922 |
939 prefs::kProfileInfoCache); | 923 if (!strcmp(key.c_str(), profiles::GetNoHighResAvatarFileName())) |
940 base::string16 name; | 924 return NULL; |
941 for (base::DictionaryValue::Iterator it(*cache); !it.IsAtEnd(); | 925 |
942 it.Advance()) { | 926 base::FilePath image_path = |
943 const base::DictionaryValue* info = NULL; | 927 profiles::GetPathOfHighResAvatarAtIndex(avatar_index); |
944 it.value().GetAsDictionary(&info); | 928 return LoadAvatarPictureFromPath(key, image_path); |
945 info->GetString(kNameKey, &name); | |
946 names.push_back(name); | |
947 } | |
948 return names; | |
949 } | 929 } |
950 | 930 |
951 // static | 931 const gfx::Image* ProfileInfoCache::LoadAvatarPictureFromPath( |
952 void ProfileInfoCache::RegisterPrefs(PrefRegistrySimple* registry) { | 932 const std::string& key, |
953 registry->RegisterDictionaryPref(prefs::kProfileInfoCache); | 933 const base::FilePath& image_path) const { |
934 // If the picture is already loaded then use it. | |
935 if (cached_avatar_images_.count(key)) { | |
936 if (cached_avatar_images_[key]->IsEmpty()) | |
937 return NULL; | |
938 return cached_avatar_images_[key]; | |
939 } | |
940 | |
941 // If the picture is already being loaded then don't try loading it again. | |
942 if (cached_avatar_images_loading_[key]) | |
943 return NULL; | |
944 cached_avatar_images_loading_[key] = true; | |
945 | |
946 gfx::Image** image = new gfx::Image*; | |
947 BrowserThread::PostTaskAndReply(BrowserThread::FILE, FROM_HERE, | |
948 base::Bind(&ReadBitmap, image_path, image), | |
949 base::Bind(&ProfileInfoCache::OnAvatarPictureLoaded, | |
950 const_cast<ProfileInfoCache*>(this)->AsWeakPtr(), key, image)); | |
951 return NULL; | |
954 } | 952 } |
953 | |
954 void ProfileInfoCache::OnAvatarPictureLoaded(const std::string& key, | |
955 gfx::Image** image) const { | |
956 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
957 | |
958 cached_avatar_images_loading_[key] = false; | |
959 delete cached_avatar_images_[key]; | |
960 | |
961 if (*image) { | |
962 cached_avatar_images_[key] = *image; | |
963 } else { | |
964 // Place an empty image in the cache to avoid reloading it again. | |
965 cached_avatar_images_[key] = new gfx::Image(); | |
966 } | |
967 delete image; | |
968 | |
969 content::NotificationService::current()->Notify( | |
970 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, | |
971 content::NotificationService::AllSources(), | |
972 content::NotificationService::NoDetails()); | |
973 } | |
974 | |
975 void ProfileInfoCache::OnAvatarPictureSaved(const std::string& file_name) { | |
976 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
977 | |
978 content::NotificationService::current()->Notify( | |
979 chrome::NOTIFICATION_PROFILE_CACHE_PICTURE_SAVED, | |
980 content::NotificationService::AllSources(), | |
981 content::NotificationService::NoDetails()); | |
982 | |
983 // Remove the file from the list of downloads in progress. Note that this list | |
984 // only contains the high resolution avatars, and not the Gaia profile images. | |
985 if (!avatar_images_downloads_in_progress_[file_name]) | |
986 return; | |
987 | |
988 delete avatar_images_downloads_in_progress_[file_name]; | |
989 avatar_images_downloads_in_progress_[file_name] = NULL; | |
990 } | |
OLD | NEW |