| 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 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 578 | 511 |
| 579 void ProfileInfoCache::SetAvatarIconOfProfileAtIndex(size_t index, | 512 void ProfileInfoCache::SetAvatarIconOfProfileAtIndex(size_t index, |
| 580 size_t icon_index) { | 513 size_t icon_index) { |
| 581 scoped_ptr<base::DictionaryValue> info( | 514 scoped_ptr<base::DictionaryValue> info( |
| 582 GetInfoForProfileAtIndex(index)->DeepCopy()); | 515 GetInfoForProfileAtIndex(index)->DeepCopy()); |
| 583 info->SetString(kAvatarIconKey, | 516 info->SetString(kAvatarIconKey, |
| 584 profiles::GetDefaultAvatarIconUrl(icon_index)); | 517 profiles::GetDefaultAvatarIconUrl(icon_index)); |
| 585 // This takes ownership of |info|. | 518 // This takes ownership of |info|. |
| 586 SetInfoForProfileAtIndex(index, info.release()); | 519 SetInfoForProfileAtIndex(index, info.release()); |
| 587 | 520 |
| 521 // If needed, start downloading the high-res avatar. |
| 522 if (switches::IsNewAvatarMenu()) |
| 523 DownloadHighResAvatar(icon_index); |
| 524 |
| 588 base::FilePath profile_path = GetPathOfProfileAtIndex(index); | 525 base::FilePath profile_path = GetPathOfProfileAtIndex(index); |
| 589 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, | 526 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, |
| 590 observer_list_, | 527 observer_list_, |
| 591 OnProfileAvatarChanged(profile_path)); | 528 OnProfileAvatarChanged(profile_path)); |
| 592 } | 529 } |
| 593 | 530 |
| 594 void ProfileInfoCache::SetIsOmittedProfileAtIndex(size_t index, | 531 void ProfileInfoCache::SetIsOmittedProfileAtIndex(size_t index, |
| 595 bool is_omitted) { | 532 bool is_omitted) { |
| 596 if (IsOmittedProfileAtIndex(index) == is_omitted) | 533 if (IsOmittedProfileAtIndex(index) == is_omitted) |
| 597 return; | 534 return; |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 688 | 625 |
| 689 if (!image) { | 626 if (!image) { |
| 690 // Delete the old bitmap from disk. | 627 // Delete the old bitmap from disk. |
| 691 if (!old_file_name.empty()) { | 628 if (!old_file_name.empty()) { |
| 692 base::FilePath image_path = path.AppendASCII(old_file_name); | 629 base::FilePath image_path = path.AppendASCII(old_file_name); |
| 693 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 630 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 694 base::Bind(&DeleteBitmap, image_path)); | 631 base::Bind(&DeleteBitmap, image_path)); |
| 695 } | 632 } |
| 696 } else { | 633 } else { |
| 697 // Save the new bitmap to disk. | 634 // Save the new bitmap to disk. |
| 698 cached_avatar_images_[key] = new gfx::Image(*image); | 635 new_file_name = |
| 699 scoped_ptr<ImageData> data(new ImageData); | 636 old_file_name.empty() ? profiles::kGAIAPictureFileName : old_file_name; |
| 700 scoped_refptr<base::RefCountedMemory> png_data = image->As1xPNGBytes(); | 637 base::FilePath image_path = path.AppendASCII(new_file_name); |
| 701 data->assign(png_data->front(), png_data->front() + png_data->size()); | 638 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 } | 639 } |
| 715 | 640 |
| 716 scoped_ptr<base::DictionaryValue> info( | 641 scoped_ptr<base::DictionaryValue> info( |
| 717 GetInfoForProfileAtIndex(index)->DeepCopy()); | 642 GetInfoForProfileAtIndex(index)->DeepCopy()); |
| 718 info->SetString(kGAIAPictureFileNameKey, new_file_name); | 643 info->SetString(kGAIAPictureFileNameKey, new_file_name); |
| 719 // This takes ownership of |info|. | 644 // This takes ownership of |info|. |
| 720 SetInfoForProfileAtIndex(index, info.release()); | 645 SetInfoForProfileAtIndex(index, info.release()); |
| 721 | 646 |
| 722 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, | 647 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, |
| 723 observer_list_, | 648 observer_list_, |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 802 if (GetNameOfProfileAtIndex(i) == name) { | 727 if (GetNameOfProfileAtIndex(i) == name) { |
| 803 name_found = true; | 728 name_found = true; |
| 804 break; | 729 break; |
| 805 } | 730 } |
| 806 } | 731 } |
| 807 if (!name_found) | 732 if (!name_found) |
| 808 return name; | 733 return name; |
| 809 } | 734 } |
| 810 } | 735 } |
| 811 | 736 |
| 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 { | 737 size_t ProfileInfoCache::ChooseAvatarIconIndexForNewProfile() const { |
| 845 size_t icon_index = 0; | 738 size_t icon_index = 0; |
| 846 // Try to find a unique, non-generic icon. | 739 // Try to find a unique, non-generic icon. |
| 847 if (ChooseAvatarIconIndexForNewProfile(false, true, &icon_index)) | 740 if (ChooseAvatarIconIndexForNewProfile(false, true, &icon_index)) |
| 848 return icon_index; | 741 return icon_index; |
| 849 // Try to find any unique icon. | 742 // Try to find any unique icon. |
| 850 if (ChooseAvatarIconIndexForNewProfile(true, true, &icon_index)) | 743 if (ChooseAvatarIconIndexForNewProfile(true, true, &icon_index)) |
| 851 return icon_index; | 744 return icon_index; |
| 852 // Settle for any random icon, even if it's not unique. | 745 // Settle for any random icon, even if it's not unique. |
| 853 if (ChooseAvatarIconIndexForNewProfile(true, false, &icon_index)) | 746 if (ChooseAvatarIconIndexForNewProfile(true, false, &icon_index)) |
| 854 return icon_index; | 747 return icon_index; |
| 855 | 748 |
| 856 NOTREACHED(); | 749 NOTREACHED(); |
| 857 return 0; | 750 return 0; |
| 858 } | 751 } |
| 859 | 752 |
| 860 const base::FilePath& ProfileInfoCache::GetUserDataDir() const { | 753 const base::FilePath& ProfileInfoCache::GetUserDataDir() const { |
| 861 return user_data_dir_; | 754 return user_data_dir_; |
| 862 } | 755 } |
| 863 | 756 |
| 757 // static |
| 758 std::vector<base::string16> ProfileInfoCache::GetProfileNames() { |
| 759 std::vector<base::string16> names; |
| 760 PrefService* local_state = g_browser_process->local_state(); |
| 761 const base::DictionaryValue* cache = local_state->GetDictionary( |
| 762 prefs::kProfileInfoCache); |
| 763 base::string16 name; |
| 764 for (base::DictionaryValue::Iterator it(*cache); !it.IsAtEnd(); |
| 765 it.Advance()) { |
| 766 const base::DictionaryValue* info = NULL; |
| 767 it.value().GetAsDictionary(&info); |
| 768 info->GetString(kNameKey, &name); |
| 769 names.push_back(name); |
| 770 } |
| 771 return names; |
| 772 } |
| 773 |
| 774 // static |
| 775 void ProfileInfoCache::RegisterPrefs(PrefRegistrySimple* registry) { |
| 776 registry->RegisterDictionaryPref(prefs::kProfileInfoCache); |
| 777 } |
| 778 |
| 779 void ProfileInfoCache::DownloadHighResAvatar(size_t icon_index) { |
| 780 // TODO(noms): We should check whether the file already exists on disk |
| 781 // before trying to re-download it. For now, since this is behind a flag and |
| 782 // the resources are still changing, re-download it every time the profile |
| 783 // avatar changes, to make sure we have the latest copy. |
| 784 std::string file_name = profiles::GetDefaultAvatarIconFileNameAtIndex( |
| 785 icon_index); |
| 786 // If the file is already being downloaded, don't start another download. |
| 787 if (avatar_images_downloads_in_progress_[file_name]) |
| 788 return; |
| 789 |
| 790 // Start the download for this file. The cache takes ownership of the |
| 791 // |avatar_downloader|, which will be deleted when the download completes, or |
| 792 // if that never happens, when the ProfileInfoCache is destroyed. |
| 793 ProfileAvatarDownloader* avatar_downloader = new ProfileAvatarDownloader( |
| 794 icon_index, |
| 795 this); |
| 796 avatar_images_downloads_in_progress_[file_name] = avatar_downloader; |
| 797 avatar_downloader->Start(); |
| 798 } |
| 799 |
| 800 void ProfileInfoCache::SaveAvatarImageAtPath( |
| 801 const gfx::Image* image, |
| 802 const std::string& key, |
| 803 const base::FilePath& image_path) { |
| 804 cached_avatar_images_[key] = new gfx::Image(*image); |
| 805 |
| 806 scoped_ptr<ImageData> data(new ImageData); |
| 807 scoped_refptr<base::RefCountedMemory> png_data = image->As1xPNGBytes(); |
| 808 data->assign(png_data->front(), png_data->front() + png_data->size()); |
| 809 |
| 810 if (!data->size()) { |
| 811 LOG(ERROR) << "Failed to PNG encode the image."; |
| 812 } else { |
| 813 base::Closure callback = base::Bind( |
| 814 &ProfileInfoCache::OnAvatarPictureSaved, AsWeakPtr(), key); |
| 815 |
| 816 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 817 base::Bind(&SaveBitmap, base::Passed(&data), image_path, callback)); |
| 818 } |
| 819 } |
| 820 |
| 864 const base::DictionaryValue* ProfileInfoCache::GetInfoForProfileAtIndex( | 821 const base::DictionaryValue* ProfileInfoCache::GetInfoForProfileAtIndex( |
| 865 size_t index) const { | 822 size_t index) const { |
| 866 DCHECK_LT(index, GetNumberOfProfiles()); | 823 DCHECK_LT(index, GetNumberOfProfiles()); |
| 867 const base::DictionaryValue* cache = | 824 const base::DictionaryValue* cache = |
| 868 prefs_->GetDictionary(prefs::kProfileInfoCache); | 825 prefs_->GetDictionary(prefs::kProfileInfoCache); |
| 869 const base::DictionaryValue* info = NULL; | 826 const base::DictionaryValue* info = NULL; |
| 870 cache->GetDictionaryWithoutPathExpansion(sorted_keys_[index], &info); | 827 cache->GetDictionaryWithoutPathExpansion(sorted_keys_[index], &info); |
| 871 return info; | 828 return info; |
| 872 } | 829 } |
| 873 | 830 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 907 return sorted_keys_.begin() + i; | 864 return sorted_keys_.begin() + i; |
| 908 if (name_compare == 0) { | 865 if (name_compare == 0) { |
| 909 int key_compare = search_key.compare(sorted_keys_[i]); | 866 int key_compare = search_key.compare(sorted_keys_[i]); |
| 910 if (key_compare < 0) | 867 if (key_compare < 0) |
| 911 return sorted_keys_.begin() + i; | 868 return sorted_keys_.begin() + i; |
| 912 } | 869 } |
| 913 } | 870 } |
| 914 return sorted_keys_.end(); | 871 return sorted_keys_.end(); |
| 915 } | 872 } |
| 916 | 873 |
| 874 bool ProfileInfoCache::IconIndexIsUnique(size_t icon_index) const { |
| 875 for (size_t i = 0; i < GetNumberOfProfiles(); ++i) { |
| 876 if (GetAvatarIconIndexOfProfileAtIndex(i) == icon_index) |
| 877 return false; |
| 878 } |
| 879 return true; |
| 880 } |
| 881 |
| 882 bool ProfileInfoCache::ChooseAvatarIconIndexForNewProfile( |
| 883 bool allow_generic_icon, |
| 884 bool must_be_unique, |
| 885 size_t* out_icon_index) const { |
| 886 // Always allow all icons for new profiles if using the |
| 887 // --new-profile-management flag. |
| 888 if (switches::IsNewProfileManagement()) |
| 889 allow_generic_icon = true; |
| 890 size_t start = allow_generic_icon ? 0 : profiles::GetGenericAvatarIconCount(); |
| 891 size_t end = profiles::GetDefaultAvatarIconCount(); |
| 892 size_t count = end - start; |
| 893 |
| 894 int rand = base::RandInt(0, count); |
| 895 for (size_t i = 0; i < count; ++i) { |
| 896 size_t icon_index = start + (rand + i) % count; |
| 897 if (!must_be_unique || IconIndexIsUnique(icon_index)) { |
| 898 *out_icon_index = icon_index; |
| 899 return true; |
| 900 } |
| 901 } |
| 902 |
| 903 return false; |
| 904 } |
| 905 |
| 917 void ProfileInfoCache::UpdateSortForProfileIndex(size_t index) { | 906 void ProfileInfoCache::UpdateSortForProfileIndex(size_t index) { |
| 918 base::string16 name = GetNameOfProfileAtIndex(index); | 907 base::string16 name = GetNameOfProfileAtIndex(index); |
| 919 | 908 |
| 920 // Remove and reinsert key in |sorted_keys_| to alphasort. | 909 // Remove and reinsert key in |sorted_keys_| to alphasort. |
| 921 std::string key = CacheKeyFromProfilePath(GetPathOfProfileAtIndex(index)); | 910 std::string key = CacheKeyFromProfilePath(GetPathOfProfileAtIndex(index)); |
| 922 std::vector<std::string>::iterator key_it = | 911 std::vector<std::string>::iterator key_it = |
| 923 std::find(sorted_keys_.begin(), sorted_keys_.end(), key); | 912 std::find(sorted_keys_.begin(), sorted_keys_.end(), key); |
| 924 DCHECK(key_it != sorted_keys_.end()); | 913 DCHECK(key_it != sorted_keys_.end()); |
| 925 sorted_keys_.erase(key_it); | 914 sorted_keys_.erase(key_it); |
| 926 sorted_keys_.insert(FindPositionForProfile(key, name), key); | 915 sorted_keys_.insert(FindPositionForProfile(key, name), key); |
| 927 | 916 |
| 928 content::NotificationService::current()->Notify( | 917 content::NotificationService::current()->Notify( |
| 929 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, | 918 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, |
| 930 content::NotificationService::AllSources(), | 919 content::NotificationService::AllSources(), |
| 931 content::NotificationService::NoDetails()); | 920 content::NotificationService::NoDetails()); |
| 932 } | 921 } |
| 933 | 922 |
| 934 // static | 923 const gfx::Image* ProfileInfoCache::GetHighResAvatarOfProfileAtIndex( |
| 935 std::vector<base::string16> ProfileInfoCache::GetProfileNames() { | 924 size_t index) const { |
| 936 std::vector<base::string16> names; | 925 int avatar_index = GetAvatarIconIndexOfProfileAtIndex(index); |
| 937 PrefService* local_state = g_browser_process->local_state(); | 926 std::string key = profiles::GetDefaultAvatarIconFileNameAtIndex(avatar_index); |
| 938 const base::DictionaryValue* cache = local_state->GetDictionary( | 927 |
| 939 prefs::kProfileInfoCache); | 928 if (!strcmp(key.c_str(), profiles::GetNoHighResAvatarFileName())) |
| 940 base::string16 name; | 929 return NULL; |
| 941 for (base::DictionaryValue::Iterator it(*cache); !it.IsAtEnd(); | 930 |
| 942 it.Advance()) { | 931 base::FilePath image_path = |
| 943 const base::DictionaryValue* info = NULL; | 932 profiles::GetPathOfHighResAvatarAtIndex(avatar_index); |
| 944 it.value().GetAsDictionary(&info); | 933 return LoadAvatarPictureFromPath(key, image_path); |
| 945 info->GetString(kNameKey, &name); | |
| 946 names.push_back(name); | |
| 947 } | |
| 948 return names; | |
| 949 } | 934 } |
| 950 | 935 |
| 951 // static | 936 const gfx::Image* ProfileInfoCache::LoadAvatarPictureFromPath( |
| 952 void ProfileInfoCache::RegisterPrefs(PrefRegistrySimple* registry) { | 937 const std::string& key, |
| 953 registry->RegisterDictionaryPref(prefs::kProfileInfoCache); | 938 const base::FilePath& image_path) const { |
| 939 // If the picture is already loaded then use it. |
| 940 if (cached_avatar_images_.count(key)) { |
| 941 if (cached_avatar_images_[key]->IsEmpty()) |
| 942 return NULL; |
| 943 return cached_avatar_images_[key]; |
| 944 } |
| 945 |
| 946 // If the picture is already being loaded then don't try loading it again. |
| 947 if (cached_avatar_images_loading_[key]) |
| 948 return NULL; |
| 949 cached_avatar_images_loading_[key] = true; |
| 950 |
| 951 gfx::Image** image = new gfx::Image*; |
| 952 BrowserThread::PostTaskAndReply(BrowserThread::FILE, FROM_HERE, |
| 953 base::Bind(&ReadBitmap, image_path, image), |
| 954 base::Bind(&ProfileInfoCache::OnAvatarPictureLoaded, |
| 955 const_cast<ProfileInfoCache*>(this)->AsWeakPtr(), key, image)); |
| 956 return NULL; |
| 954 } | 957 } |
| 958 |
| 959 void ProfileInfoCache::OnAvatarPictureLoaded(const std::string& key, |
| 960 gfx::Image** image) const { |
| 961 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 962 |
| 963 cached_avatar_images_loading_[key] = false; |
| 964 delete cached_avatar_images_[key]; |
| 965 |
| 966 if (*image) { |
| 967 cached_avatar_images_[key] = *image; |
| 968 } else { |
| 969 // Place an empty image in the cache to avoid reloading it again. |
| 970 cached_avatar_images_[key] = new gfx::Image(); |
| 971 } |
| 972 delete image; |
| 973 |
| 974 content::NotificationService::current()->Notify( |
| 975 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, |
| 976 content::NotificationService::AllSources(), |
| 977 content::NotificationService::NoDetails()); |
| 978 } |
| 979 |
| 980 void ProfileInfoCache::OnAvatarPictureSaved(const std::string& file_name) { |
| 981 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 982 |
| 983 content::NotificationService::current()->Notify( |
| 984 chrome::NOTIFICATION_PROFILE_CACHE_PICTURE_SAVED, |
| 985 content::NotificationService::AllSources(), |
| 986 content::NotificationService::NoDetails()); |
| 987 |
| 988 // Remove the file from the list of downloads in progress. Note that this list |
| 989 // only contains the high resolution avatars, and not the Gaia profile images. |
| 990 if (!avatar_images_downloads_in_progress_[file_name]) |
| 991 return; |
| 992 |
| 993 delete avatar_images_downloads_in_progress_[file_name]; |
| 994 avatar_images_downloads_in_progress_[file_name] = NULL; |
| 995 } |
| OLD | NEW |