1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/profiles/profile_info_cache.h"
#include "base/bind.h"
#include "base/files/file_util.h"
#include "base/i18n/case_conversion.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/prefs/pref_registry_simple.h"
#include "base/prefs/pref_service.h"
#include "base/prefs/scoped_user_pref_update.h"
#include "base/rand_util.h"
#include "base/stl_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_piece.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/profiles/profile_avatar_downloader.h"
#include "chrome/browser/profiles/profile_avatar_icon_util.h"
#include "chrome/browser/profiles/profiles_state.h"
#include "chrome/common/pref_names.h"
#include "chrome/grit/generated_resources.h"
#include "components/signin/core/common/profile_management_switches.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_service.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_util.h"
#if defined(ENABLE_SUPERVISED_USERS)
#include "chrome/browser/supervised_user/supervised_user_constants.h"
#endif
using content::BrowserThread;
namespace {
const char kNameKey[] = "name";
const char kShortcutNameKey[] = "shortcut_name";
const char kGAIANameKey[] = "gaia_name";
const char kGAIAGivenNameKey[] = "gaia_given_name";
const char kUserNameKey[] = "user_name";
const char kIsUsingDefaultNameKey[] = "is_using_default_name";
const char kIsUsingDefaultAvatarKey[] = "is_using_default_avatar";
const char kAvatarIconKey[] = "avatar_icon";
const char kAuthCredentialsKey[] = "local_auth_credentials";
const char kUseGAIAPictureKey[] = "use_gaia_picture";
const char kBackgroundAppsKey[] = "background_apps";
const char kGAIAPictureFileNameKey[] = "gaia_picture_file_name";
const char kIsOmittedFromProfileListKey[] = "is_omitted_from_profile_list";
const char kSigninRequiredKey[] = "signin_required";
const char kSupervisedUserId[] = "managed_user_id";
const char kProfileIsEphemeral[] = "is_ephemeral";
const char kActiveTimeKey[] = "active_time";
const char kIsAuthErrorKey[] = "is_auth_error";
// First eight are generic icons, which use IDS_NUMBERED_PROFILE_NAME.
const int kDefaultNames[] = {
IDS_DEFAULT_AVATAR_NAME_8,
IDS_DEFAULT_AVATAR_NAME_9,
IDS_DEFAULT_AVATAR_NAME_10,
IDS_DEFAULT_AVATAR_NAME_11,
IDS_DEFAULT_AVATAR_NAME_12,
IDS_DEFAULT_AVATAR_NAME_13,
IDS_DEFAULT_AVATAR_NAME_14,
IDS_DEFAULT_AVATAR_NAME_15,
IDS_DEFAULT_AVATAR_NAME_16,
IDS_DEFAULT_AVATAR_NAME_17,
IDS_DEFAULT_AVATAR_NAME_18,
IDS_DEFAULT_AVATAR_NAME_19,
IDS_DEFAULT_AVATAR_NAME_20,
IDS_DEFAULT_AVATAR_NAME_21,
IDS_DEFAULT_AVATAR_NAME_22,
IDS_DEFAULT_AVATAR_NAME_23,
IDS_DEFAULT_AVATAR_NAME_24,
IDS_DEFAULT_AVATAR_NAME_25,
IDS_DEFAULT_AVATAR_NAME_26
};
typedef std::vector<unsigned char> ImageData;
// Writes |data| to disk and takes ownership of the pointer. On successful
// completion, it runs |callback|.
void SaveBitmap(scoped_ptr<ImageData> data,
const base::FilePath& image_path,
const base::Closure& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
// Make sure the destination directory exists.
base::FilePath dir = image_path.DirName();
if (!base::DirectoryExists(dir) && !base::CreateDirectory(dir)) {
LOG(ERROR) << "Failed to create parent directory.";
return;
}
if (base::WriteFile(image_path, reinterpret_cast<char*>(&(*data)[0]),
data->size()) == -1) {
LOG(ERROR) << "Failed to save image to file.";
return;
}
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, callback);
}
// Reads a PNG from disk and decodes it. If the bitmap was successfully read
// from disk the then |out_image| will contain the bitmap image, otherwise it
// will be NULL.
void ReadBitmap(const base::FilePath& image_path,
gfx::Image** out_image) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
*out_image = NULL;
// If the path doesn't exist, don't even try reading it.
if (!base::PathExists(image_path))
return;
std::string image_data;
if (!base::ReadFileToString(image_path, &image_data)) {
LOG(ERROR) << "Failed to read PNG file from disk.";
return;
}
gfx::Image image = gfx::Image::CreateFrom1xPNGBytes(
base::RefCountedString::TakeString(&image_data));
if (image.IsEmpty()) {
LOG(ERROR) << "Failed to decode PNG file.";
return;
}
*out_image = new gfx::Image(image);
}
void RunCallbackIfFileMissing(const base::FilePath& file_path,
const base::Closure& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
if (!base::PathExists(file_path))
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, callback);
}
void DeleteBitmap(const base::FilePath& image_path) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
base::DeleteFile(image_path, false);
}
// Used by SaveAvatarImageAtPath to post a task to delete the |downloader|
// "soon". We can't just delete it directly there because
// SaveAvatarImageAtPath is called from this very downloader.
void DeleteDownloader(ProfileAvatarDownloader* downloader) {
delete downloader;
}
} // namespace
ProfileInfoCache::ProfileInfoCache(PrefService* prefs,
const base::FilePath& user_data_dir)
: prefs_(prefs),
user_data_dir_(user_data_dir) {
// Populate the cache
DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache);
base::DictionaryValue* cache = update.Get();
for (base::DictionaryValue::Iterator it(*cache);
!it.IsAtEnd(); it.Advance()) {
base::DictionaryValue* info = NULL;
cache->GetDictionaryWithoutPathExpansion(it.key(), &info);
base::string16 name;
info->GetString(kNameKey, &name);
sorted_keys_.insert(FindPositionForProfile(it.key(), name), it.key());
bool using_default_name;
if (!info->GetBoolean(kIsUsingDefaultNameKey, &using_default_name)) {
// If the preference hasn't been set, and the name is default, assume
// that the user hasn't done this on purpose.
using_default_name = IsDefaultProfileName(name);
info->SetBoolean(kIsUsingDefaultNameKey, using_default_name);
}
// For profiles that don't have the "using default avatar" state set yet,
// assume it's the same as the "using default name" state.
if (!info->HasKey(kIsUsingDefaultAvatarKey)) {
info->SetBoolean(kIsUsingDefaultAvatarKey, using_default_name);
}
}
// If needed, start downloading the high-res avatars and migrate any legacy
// profile names.
if (switches::IsNewAvatarMenu())
MigrateLegacyProfileNamesAndDownloadAvatars();
}
ProfileInfoCache::~ProfileInfoCache() {
STLDeleteContainerPairSecondPointers(
cached_avatar_images_.begin(), cached_avatar_images_.end());
STLDeleteContainerPairSecondPointers(
avatar_images_downloads_in_progress_.begin(),
avatar_images_downloads_in_progress_.end());
}
void ProfileInfoCache::AddProfileToCache(
const base::FilePath& profile_path,
const base::string16& name,
const base::string16& username,
size_t icon_index,
const std::string& supervised_user_id) {
std::string key = CacheKeyFromProfilePath(profile_path);
DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache);
base::DictionaryValue* cache = update.Get();
scoped_ptr<base::DictionaryValue> info(new base::DictionaryValue);
info->SetString(kNameKey, name);
info->SetString(kUserNameKey, username);
info->SetString(kAvatarIconKey,
profiles::GetDefaultAvatarIconUrl(icon_index));
// Default value for whether background apps are running is false.
info->SetBoolean(kBackgroundAppsKey, false);
info->SetString(kSupervisedUserId, supervised_user_id);
info->SetBoolean(kIsOmittedFromProfileListKey, !supervised_user_id.empty());
info->SetBoolean(kProfileIsEphemeral, false);
info->SetBoolean(kIsUsingDefaultNameKey, IsDefaultProfileName(name));
// Assume newly created profiles use a default avatar.
info->SetBoolean(kIsUsingDefaultAvatarKey, true);
cache->SetWithoutPathExpansion(key, info.release());
sorted_keys_.insert(FindPositionForProfile(key, name), key);
if (switches::IsNewAvatarMenu())
DownloadHighResAvatarIfNeeded(icon_index, profile_path);
FOR_EACH_OBSERVER(ProfileInfoCacheObserver,
observer_list_,
OnProfileAdded(profile_path));
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED,
content::NotificationService::AllSources(),
content::NotificationService::NoDetails());
}
void ProfileInfoCache::AddObserver(ProfileInfoCacheObserver* obs) {
observer_list_.AddObserver(obs);
}
void ProfileInfoCache::RemoveObserver(ProfileInfoCacheObserver* obs) {
observer_list_.RemoveObserver(obs);
}
void ProfileInfoCache::DeleteProfileFromCache(
const base::FilePath& profile_path) {
size_t profile_index = GetIndexOfProfileWithPath(profile_path);
if (profile_index == std::string::npos) {
NOTREACHED();
return;
}
base::string16 name = GetNameOfProfileAtIndex(profile_index);
FOR_EACH_OBSERVER(ProfileInfoCacheObserver,
observer_list_,
OnProfileWillBeRemoved(profile_path));
DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache);
base::DictionaryValue* cache = update.Get();
std::string key = CacheKeyFromProfilePath(profile_path);
cache->Remove(key, NULL);
sorted_keys_.erase(std::find(sorted_keys_.begin(), sorted_keys_.end(), key));
FOR_EACH_OBSERVER(ProfileInfoCacheObserver,
observer_list_,
OnProfileWasRemoved(profile_path, name));
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED,
content::NotificationService::AllSources(),
content::NotificationService::NoDetails());
}
size_t ProfileInfoCache::GetNumberOfProfiles() const {
return sorted_keys_.size();
}
size_t ProfileInfoCache::GetIndexOfProfileWithPath(
const base::FilePath& profile_path) const {
if (profile_path.DirName() != user_data_dir_)
return std::string::npos;
std::string search_key = CacheKeyFromProfilePath(profile_path);
for (size_t i = 0; i < sorted_keys_.size(); ++i) {
if (sorted_keys_[i] == search_key)
return i;
}
return std::string::npos;
}
base::string16 ProfileInfoCache::GetNameOfProfileAtIndex(size_t index) const {
base::string16 name;
// Unless the user has customized the profile name, we should use the
// profile's Gaia given name, if it's available.
if (ProfileIsUsingDefaultNameAtIndex(index)) {
base::string16 given_name = GetGAIAGivenNameOfProfileAtIndex(index);
name = given_name.empty() ? GetGAIANameOfProfileAtIndex(index) : given_name;
}
if (name.empty())
GetInfoForProfileAtIndex(index)->GetString(kNameKey, &name);
return name;
}
base::string16 ProfileInfoCache::GetShortcutNameOfProfileAtIndex(size_t index)
const {
base::string16 shortcut_name;
GetInfoForProfileAtIndex(index)->GetString(
kShortcutNameKey, &shortcut_name);
return shortcut_name;
}
base::FilePath ProfileInfoCache::GetPathOfProfileAtIndex(size_t index) const {
return user_data_dir_.AppendASCII(sorted_keys_[index]);
}
base::Time ProfileInfoCache::GetProfileActiveTimeAtIndex(size_t index) const {
double dt;
if (GetInfoForProfileAtIndex(index)->GetDouble(kActiveTimeKey, &dt)) {
return base::Time::FromDoubleT(dt);
} else {
return base::Time();
}
}
base::string16 ProfileInfoCache::GetUserNameOfProfileAtIndex(
size_t index) const {
base::string16 user_name;
GetInfoForProfileAtIndex(index)->GetString(kUserNameKey, &user_name);
return user_name;
}
const gfx::Image& ProfileInfoCache::GetAvatarIconOfProfileAtIndex(
size_t index) const {
if (IsUsingGAIAPictureOfProfileAtIndex(index)) {
const gfx::Image* image = GetGAIAPictureOfProfileAtIndex(index);
if (image)
return *image;
}
// Use the high resolution version of the avatar if it exists.
if (switches::IsNewAvatarMenu()) {
const gfx::Image* image = GetHighResAvatarOfProfileAtIndex(index);
if (image)
return *image;
}
int resource_id = profiles::GetDefaultAvatarIconResourceIDAtIndex(
GetAvatarIconIndexOfProfileAtIndex(index));
return ResourceBundle::GetSharedInstance().GetNativeImageNamed(resource_id);
}
std::string ProfileInfoCache::GetLocalAuthCredentialsOfProfileAtIndex(
size_t index) const {
std::string credentials;
GetInfoForProfileAtIndex(index)->GetString(kAuthCredentialsKey, &credentials);
return credentials;
}
bool ProfileInfoCache::GetBackgroundStatusOfProfileAtIndex(
size_t index) const {
bool background_app_status;
if (!GetInfoForProfileAtIndex(index)->GetBoolean(kBackgroundAppsKey,
&background_app_status)) {
return false;
}
return background_app_status;
}
base::string16 ProfileInfoCache::GetGAIANameOfProfileAtIndex(
size_t index) const {
base::string16 name;
GetInfoForProfileAtIndex(index)->GetString(kGAIANameKey, &name);
return name;
}
base::string16 ProfileInfoCache::GetGAIAGivenNameOfProfileAtIndex(
size_t index) const {
base::string16 name;
GetInfoForProfileAtIndex(index)->GetString(kGAIAGivenNameKey, &name);
return name;
}
const gfx::Image* ProfileInfoCache::GetGAIAPictureOfProfileAtIndex(
size_t index) const {
base::FilePath path = GetPathOfProfileAtIndex(index);
std::string key = CacheKeyFromProfilePath(path);
std::string file_name;
GetInfoForProfileAtIndex(index)->GetString(
kGAIAPictureFileNameKey, &file_name);
// If the picture is not on disk then return NULL.
if (file_name.empty())
return NULL;
base::FilePath image_path = path.AppendASCII(file_name);
return LoadAvatarPictureFromPath(path, key, image_path);
}
bool ProfileInfoCache::IsUsingGAIAPictureOfProfileAtIndex(size_t index) const {
bool value = false;
GetInfoForProfileAtIndex(index)->GetBoolean(kUseGAIAPictureKey, &value);
if (!value) {
// Prefer the GAIA avatar over a non-customized avatar.
value = ProfileIsUsingDefaultAvatarAtIndex(index) &&
GetGAIAPictureOfProfileAtIndex(index);
}
return value;
}
bool ProfileInfoCache::ProfileIsSupervisedAtIndex(size_t index) const {
return !GetSupervisedUserIdOfProfileAtIndex(index).empty();
}
bool ProfileInfoCache::ProfileIsChildAtIndex(size_t index) const {
#if defined(ENABLE_SUPERVISED_USERS)
return GetSupervisedUserIdOfProfileAtIndex(index) ==
supervised_users::kChildAccountSUID;
#else
return false;
#endif
}
bool ProfileInfoCache::ProfileIsLegacySupervisedAtIndex(size_t index) const {
return ProfileIsSupervisedAtIndex(index) && !ProfileIsChildAtIndex(index);
}
bool ProfileInfoCache::IsOmittedProfileAtIndex(size_t index) const {
bool value = false;
GetInfoForProfileAtIndex(index)->GetBoolean(kIsOmittedFromProfileListKey,
&value);
return value;
}
bool ProfileInfoCache::ProfileIsSigninRequiredAtIndex(size_t index) const {
bool value = false;
GetInfoForProfileAtIndex(index)->GetBoolean(kSigninRequiredKey, &value);
return value;
}
std::string ProfileInfoCache::GetSupervisedUserIdOfProfileAtIndex(
size_t index) const {
std::string supervised_user_id;
GetInfoForProfileAtIndex(index)->GetString(kSupervisedUserId,
&supervised_user_id);
return supervised_user_id;
}
bool ProfileInfoCache::ProfileIsEphemeralAtIndex(size_t index) const {
bool value = false;
GetInfoForProfileAtIndex(index)->GetBoolean(kProfileIsEphemeral, &value);
return value;
}
bool ProfileInfoCache::ProfileIsUsingDefaultNameAtIndex(size_t index) const {
bool value = false;
GetInfoForProfileAtIndex(index)->GetBoolean(kIsUsingDefaultNameKey, &value);
return value;
}
bool ProfileInfoCache::ProfileIsUsingDefaultAvatarAtIndex(size_t index) const {
bool value = false;
GetInfoForProfileAtIndex(index)->GetBoolean(kIsUsingDefaultAvatarKey, &value);
return value;
}
bool ProfileInfoCache::ProfileIsAuthErrorAtIndex(size_t index) const {
bool value = false;
GetInfoForProfileAtIndex(index)->GetBoolean(kIsAuthErrorKey, &value);
return value;
}
size_t ProfileInfoCache::GetAvatarIconIndexOfProfileAtIndex(size_t index)
const {
std::string icon_url;
GetInfoForProfileAtIndex(index)->GetString(kAvatarIconKey, &icon_url);
size_t icon_index = 0;
if (!profiles::IsDefaultAvatarIconUrl(icon_url, &icon_index))
DLOG(WARNING) << "Unknown avatar icon: " << icon_url;
return icon_index;
}
void ProfileInfoCache::SetProfileActiveTimeAtIndex(size_t index) {
scoped_ptr<base::DictionaryValue> info(
GetInfoForProfileAtIndex(index)->DeepCopy());
info->SetDouble(kActiveTimeKey, base::Time::Now().ToDoubleT());
// This takes ownership of |info|.
SetInfoQuietlyForProfileAtIndex(index, info.release());
}
void ProfileInfoCache::SetNameOfProfileAtIndex(size_t index,
const base::string16& name) {
scoped_ptr<base::DictionaryValue> info(
GetInfoForProfileAtIndex(index)->DeepCopy());
base::string16 current_name;
info->GetString(kNameKey, ¤t_name);
if (name == current_name)
return;
base::string16 old_display_name = GetNameOfProfileAtIndex(index);
info->SetString(kNameKey, name);
// This takes ownership of |info|.
SetInfoForProfileAtIndex(index, info.release());
base::string16 new_display_name = GetNameOfProfileAtIndex(index);
base::FilePath profile_path = GetPathOfProfileAtIndex(index);
UpdateSortForProfileIndex(index);
if (old_display_name != new_display_name) {
FOR_EACH_OBSERVER(ProfileInfoCacheObserver,
observer_list_,
OnProfileNameChanged(profile_path, old_display_name));
}
}
void ProfileInfoCache::SetShortcutNameOfProfileAtIndex(
size_t index,
const base::string16& shortcut_name) {
if (shortcut_name == GetShortcutNameOfProfileAtIndex(index))
return;
scoped_ptr<base::DictionaryValue> info(
GetInfoForProfileAtIndex(index)->DeepCopy());
info->SetString(kShortcutNameKey, shortcut_name);
// This takes ownership of |info|.
SetInfoForProfileAtIndex(index, info.release());
}
void ProfileInfoCache::SetUserNameOfProfileAtIndex(
size_t index,
const base::string16& user_name) {
if (user_name == GetUserNameOfProfileAtIndex(index))
return;
scoped_ptr<base::DictionaryValue> info(
GetInfoForProfileAtIndex(index)->DeepCopy());
info->SetString(kUserNameKey, user_name);
// This takes ownership of |info|.
SetInfoForProfileAtIndex(index, info.release());
}
void ProfileInfoCache::SetAvatarIconOfProfileAtIndex(size_t index,
size_t icon_index) {
scoped_ptr<base::DictionaryValue> info(
GetInfoForProfileAtIndex(index)->DeepCopy());
info->SetString(kAvatarIconKey,
profiles::GetDefaultAvatarIconUrl(icon_index));
// This takes ownership of |info|.
SetInfoForProfileAtIndex(index, info.release());
base::FilePath profile_path = GetPathOfProfileAtIndex(index);
if (switches::IsNewAvatarMenu())
DownloadHighResAvatarIfNeeded(icon_index, profile_path);
FOR_EACH_OBSERVER(ProfileInfoCacheObserver,
observer_list_,
OnProfileAvatarChanged(profile_path));
}
void ProfileInfoCache::SetIsOmittedProfileAtIndex(size_t index,
bool is_omitted) {
if (IsOmittedProfileAtIndex(index) == is_omitted)
return;
scoped_ptr<base::DictionaryValue> info(
GetInfoForProfileAtIndex(index)->DeepCopy());
info->SetBoolean(kIsOmittedFromProfileListKey, is_omitted);
// This takes ownership of |info|.
SetInfoForProfileAtIndex(index, info.release());
}
void ProfileInfoCache::SetSupervisedUserIdOfProfileAtIndex(
size_t index,
const std::string& id) {
if (GetSupervisedUserIdOfProfileAtIndex(index) == id)
return;
scoped_ptr<base::DictionaryValue> info(
GetInfoForProfileAtIndex(index)->DeepCopy());
info->SetString(kSupervisedUserId, id);
// This takes ownership of |info|.
SetInfoForProfileAtIndex(index, info.release());
base::FilePath profile_path = GetPathOfProfileAtIndex(index);
FOR_EACH_OBSERVER(ProfileInfoCacheObserver,
observer_list_,
OnProfileSupervisedUserIdChanged(profile_path));
}
void ProfileInfoCache::SetLocalAuthCredentialsOfProfileAtIndex(
size_t index,
const std::string& credentials) {
scoped_ptr<base::DictionaryValue> info(
GetInfoForProfileAtIndex(index)->DeepCopy());
info->SetString(kAuthCredentialsKey, credentials);
// This takes ownership of |info|.
SetInfoForProfileAtIndex(index, info.release());
}
void ProfileInfoCache::SetBackgroundStatusOfProfileAtIndex(
size_t index,
bool running_background_apps) {
if (GetBackgroundStatusOfProfileAtIndex(index) == running_background_apps)
return;
scoped_ptr<base::DictionaryValue> info(
GetInfoForProfileAtIndex(index)->DeepCopy());
info->SetBoolean(kBackgroundAppsKey, running_background_apps);
// This takes ownership of |info|.
SetInfoForProfileAtIndex(index, info.release());
}
void ProfileInfoCache::SetGAIANameOfProfileAtIndex(size_t index,
const base::string16& name) {
if (name == GetGAIANameOfProfileAtIndex(index))
return;
base::string16 old_display_name = GetNameOfProfileAtIndex(index);
scoped_ptr<base::DictionaryValue> info(
GetInfoForProfileAtIndex(index)->DeepCopy());
info->SetString(kGAIANameKey, name);
// This takes ownership of |info|.
SetInfoForProfileAtIndex(index, info.release());
base::string16 new_display_name = GetNameOfProfileAtIndex(index);
base::FilePath profile_path = GetPathOfProfileAtIndex(index);
UpdateSortForProfileIndex(index);
if (old_display_name != new_display_name) {
FOR_EACH_OBSERVER(ProfileInfoCacheObserver,
observer_list_,
OnProfileNameChanged(profile_path, old_display_name));
}
}
void ProfileInfoCache::SetGAIAGivenNameOfProfileAtIndex(
size_t index,
const base::string16& name) {
if (name == GetGAIAGivenNameOfProfileAtIndex(index))
return;
base::string16 old_display_name = GetNameOfProfileAtIndex(index);
scoped_ptr<base::DictionaryValue> info(
GetInfoForProfileAtIndex(index)->DeepCopy());
info->SetString(kGAIAGivenNameKey, name);
// This takes ownership of |info|.
SetInfoForProfileAtIndex(index, info.release());
base::string16 new_display_name = GetNameOfProfileAtIndex(index);
base::FilePath profile_path = GetPathOfProfileAtIndex(index);
UpdateSortForProfileIndex(index);
if (old_display_name != new_display_name) {
FOR_EACH_OBSERVER(ProfileInfoCacheObserver,
observer_list_,
OnProfileNameChanged(profile_path, old_display_name));
}
}
void ProfileInfoCache::SetGAIAPictureOfProfileAtIndex(size_t index,
const gfx::Image* image) {
base::FilePath path = GetPathOfProfileAtIndex(index);
std::string key = CacheKeyFromProfilePath(path);
// Delete the old bitmap from cache.
std::map<std::string, gfx::Image*>::iterator it =
cached_avatar_images_.find(key);
if (it != cached_avatar_images_.end()) {
delete it->second;
cached_avatar_images_.erase(it);
}
std::string old_file_name;
GetInfoForProfileAtIndex(index)->GetString(
kGAIAPictureFileNameKey, &old_file_name);
std::string new_file_name;
if (!image) {
// Delete the old bitmap from disk.
if (!old_file_name.empty()) {
base::FilePath image_path = path.AppendASCII(old_file_name);
BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
base::Bind(&DeleteBitmap, image_path));
}
} else {
// Save the new bitmap to disk.
new_file_name =
old_file_name.empty() ? profiles::kGAIAPictureFileName : old_file_name;
base::FilePath image_path = path.AppendASCII(new_file_name);
SaveAvatarImageAtPath(
image, key, image_path, GetPathOfProfileAtIndex(index));
}
scoped_ptr<base::DictionaryValue> info(
GetInfoForProfileAtIndex(index)->DeepCopy());
info->SetString(kGAIAPictureFileNameKey, new_file_name);
// This takes ownership of |info|.
SetInfoForProfileAtIndex(index, info.release());
FOR_EACH_OBSERVER(ProfileInfoCacheObserver,
observer_list_,
OnProfileAvatarChanged(path));
}
void ProfileInfoCache::SetIsUsingGAIAPictureOfProfileAtIndex(size_t index,
bool value) {
scoped_ptr<base::DictionaryValue> info(
GetInfoForProfileAtIndex(index)->DeepCopy());
info->SetBoolean(kUseGAIAPictureKey, value);
// This takes ownership of |info|.
SetInfoForProfileAtIndex(index, info.release());
base::FilePath profile_path = GetPathOfProfileAtIndex(index);
FOR_EACH_OBSERVER(ProfileInfoCacheObserver,
observer_list_,
OnProfileAvatarChanged(profile_path));
}
void ProfileInfoCache::SetProfileSigninRequiredAtIndex(size_t index,
bool value) {
if (value == ProfileIsSigninRequiredAtIndex(index))
return;
scoped_ptr<base::DictionaryValue> info(
GetInfoForProfileAtIndex(index)->DeepCopy());
info->SetBoolean(kSigninRequiredKey, value);
// This takes ownership of |info|.
SetInfoForProfileAtIndex(index, info.release());
base::FilePath profile_path = GetPathOfProfileAtIndex(index);
FOR_EACH_OBSERVER(ProfileInfoCacheObserver,
observer_list_,
OnProfileSigninRequiredChanged(profile_path));
}
void ProfileInfoCache::SetProfileIsEphemeralAtIndex(size_t index, bool value) {
if (value == ProfileIsEphemeralAtIndex(index))
return;
scoped_ptr<base::DictionaryValue> info(
GetInfoForProfileAtIndex(index)->DeepCopy());
info->SetBoolean(kProfileIsEphemeral, value);
// This takes ownership of |info|.
SetInfoForProfileAtIndex(index, info.release());
}
void ProfileInfoCache::SetProfileIsUsingDefaultNameAtIndex(
size_t index, bool value) {
if (value == ProfileIsUsingDefaultNameAtIndex(index))
return;
scoped_ptr<base::DictionaryValue> info(
GetInfoForProfileAtIndex(index)->DeepCopy());
info->SetBoolean(kIsUsingDefaultNameKey, value);
// This takes ownership of |info|.
SetInfoForProfileAtIndex(index, info.release());
}
void ProfileInfoCache::SetProfileIsUsingDefaultAvatarAtIndex(
size_t index, bool value) {
if (value == ProfileIsUsingDefaultAvatarAtIndex(index))
return;
scoped_ptr<base::DictionaryValue> info(
GetInfoForProfileAtIndex(index)->DeepCopy());
info->SetBoolean(kIsUsingDefaultAvatarKey, value);
// This takes ownership of |info|.
SetInfoForProfileAtIndex(index, info.release());
}
void ProfileInfoCache::SetProfileIsAuthErrorAtIndex(size_t index, bool value) {
if (value == ProfileIsAuthErrorAtIndex(index))
return;
scoped_ptr<base::DictionaryValue> info(
GetInfoForProfileAtIndex(index)->DeepCopy());
info->SetBoolean(kIsAuthErrorKey, value);
// This takes ownership of |info|.
SetInfoForProfileAtIndex(index, info.release());
}
bool ProfileInfoCache::IsDefaultProfileName(const base::string16& name) const {
// Check if it's a "First user" old-style name.
if (name == l10n_util::GetStringUTF16(IDS_DEFAULT_PROFILE_NAME) ||
name == l10n_util::GetStringUTF16(IDS_LEGACY_DEFAULT_PROFILE_NAME))
return true;
// Check if it's one of the old-style profile names.
for (size_t i = 0; i < arraysize(kDefaultNames); ++i) {
if (name == l10n_util::GetStringUTF16(kDefaultNames[i]))
return true;
}
// Check whether it's one of the "Person %d" style names.
std::string default_name_format = l10n_util::GetStringFUTF8(
IDS_NEW_NUMBERED_PROFILE_NAME, base::ASCIIToUTF16("%d"));
int generic_profile_number; // Unused. Just a placeholder for sscanf.
int assignments = sscanf(base::UTF16ToUTF8(name).c_str(),
default_name_format.c_str(),
&generic_profile_number);
// Unless it matched the format, this is a custom name.
return assignments == 1;
}
base::string16 ProfileInfoCache::ChooseNameForNewProfile(
size_t icon_index) const {
base::string16 name;
for (int name_index = 1; ; ++name_index) {
if (switches::IsNewAvatarMenu()) {
name = l10n_util::GetStringFUTF16Int(IDS_NEW_NUMBERED_PROFILE_NAME,
name_index);
} else if (icon_index < profiles::GetGenericAvatarIconCount()) {
name = l10n_util::GetStringFUTF16Int(IDS_NUMBERED_PROFILE_NAME,
name_index);
} else {
name = l10n_util::GetStringUTF16(
kDefaultNames[icon_index - profiles::GetGenericAvatarIconCount()]);
if (name_index > 1)
name.append(base::UTF8ToUTF16(base::IntToString(name_index)));
}
// Loop through previously named profiles to ensure we're not duplicating.
bool name_found = false;
for (size_t i = 0; i < GetNumberOfProfiles(); ++i) {
if (GetNameOfProfileAtIndex(i) == name) {
name_found = true;
break;
}
}
if (!name_found)
return name;
}
}
size_t ProfileInfoCache::ChooseAvatarIconIndexForNewProfile() const {
size_t icon_index = 0;
// Try to find a unique, non-generic icon.
if (ChooseAvatarIconIndexForNewProfile(false, true, &icon_index))
return icon_index;
// Try to find any unique icon.
if (ChooseAvatarIconIndexForNewProfile(true, true, &icon_index))
return icon_index;
// Settle for any random icon, even if it's not unique.
if (ChooseAvatarIconIndexForNewProfile(true, false, &icon_index))
return icon_index;
NOTREACHED();
return 0;
}
const base::FilePath& ProfileInfoCache::GetUserDataDir() const {
return user_data_dir_;
}
// static
std::vector<base::string16> ProfileInfoCache::GetProfileNames() {
std::vector<base::string16> names;
PrefService* local_state = g_browser_process->local_state();
const base::DictionaryValue* cache = local_state->GetDictionary(
prefs::kProfileInfoCache);
base::string16 name;
for (base::DictionaryValue::Iterator it(*cache); !it.IsAtEnd();
it.Advance()) {
const base::DictionaryValue* info = NULL;
it.value().GetAsDictionary(&info);
info->GetString(kNameKey, &name);
names.push_back(name);
}
return names;
}
// static
void ProfileInfoCache::RegisterPrefs(PrefRegistrySimple* registry) {
registry->RegisterDictionaryPref(prefs::kProfileInfoCache);
}
void ProfileInfoCache::DownloadHighResAvatarIfNeeded(
size_t icon_index,
const base::FilePath& profile_path) {
// Downloading is only supported on desktop.
#if defined(OS_ANDROID) || defined(OS_IOS) || defined(OS_CHROMEOS)
return;
#endif
const base::FilePath& file_path =
profiles::GetPathOfHighResAvatarAtIndex(icon_index);
base::Closure callback =
base::Bind(&ProfileInfoCache::DownloadHighResAvatar,
AsWeakPtr(),
icon_index,
profile_path);
BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
base::Bind(&RunCallbackIfFileMissing, file_path, callback));
}
void ProfileInfoCache::SaveAvatarImageAtPath(
const gfx::Image* image,
const std::string& key,
const base::FilePath& image_path,
const base::FilePath& profile_path) {
cached_avatar_images_[key] = new gfx::Image(*image);
scoped_ptr<ImageData> data(new ImageData);
scoped_refptr<base::RefCountedMemory> png_data = image->As1xPNGBytes();
data->assign(png_data->front(), png_data->front() + png_data->size());
// Remove the file from the list of downloads in progress. Note that this list
// only contains the high resolution avatars, and not the Gaia profile images.
auto downloader_iter = avatar_images_downloads_in_progress_.find(key);
if (downloader_iter != avatar_images_downloads_in_progress_.end()) {
// We mustn't delete the avatar downloader right here, since we're being
// called by it.
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(&DeleteDownloader,
downloader_iter->second));
avatar_images_downloads_in_progress_.erase(downloader_iter);
}
if (!data->size()) {
LOG(ERROR) << "Failed to PNG encode the image.";
} else {
base::Closure callback = base::Bind(&ProfileInfoCache::OnAvatarPictureSaved,
AsWeakPtr(), key, profile_path);
BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
base::Bind(&SaveBitmap, base::Passed(&data), image_path, callback));
}
}
const base::DictionaryValue* ProfileInfoCache::GetInfoForProfileAtIndex(
size_t index) const {
DCHECK_LT(index, GetNumberOfProfiles());
const base::DictionaryValue* cache =
prefs_->GetDictionary(prefs::kProfileInfoCache);
const base::DictionaryValue* info = NULL;
cache->GetDictionaryWithoutPathExpansion(sorted_keys_[index], &info);
return info;
}
void ProfileInfoCache::SetInfoQuietlyForProfileAtIndex(
size_t index, base::DictionaryValue* info) {
DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache);
base::DictionaryValue* cache = update.Get();
cache->SetWithoutPathExpansion(sorted_keys_[index], info);
}
// TODO(noms): Switch to newer notification system.
void ProfileInfoCache::SetInfoForProfileAtIndex(size_t index,
base::DictionaryValue* info) {
SetInfoQuietlyForProfileAtIndex(index, info);
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED,
content::NotificationService::AllSources(),
content::NotificationService::NoDetails());
}
std::string ProfileInfoCache::CacheKeyFromProfilePath(
const base::FilePath& profile_path) const {
DCHECK(user_data_dir_ == profile_path.DirName());
base::FilePath base_name = profile_path.BaseName();
return base_name.MaybeAsASCII();
}
std::vector<std::string>::iterator ProfileInfoCache::FindPositionForProfile(
const std::string& search_key,
const base::string16& search_name) {
base::string16 search_name_l = base::i18n::ToLower(search_name);
for (size_t i = 0; i < GetNumberOfProfiles(); ++i) {
base::string16 name_l = base::i18n::ToLower(GetNameOfProfileAtIndex(i));
int name_compare = search_name_l.compare(name_l);
if (name_compare < 0)
return sorted_keys_.begin() + i;
if (name_compare == 0) {
int key_compare = search_key.compare(sorted_keys_[i]);
if (key_compare < 0)
return sorted_keys_.begin() + i;
}
}
return sorted_keys_.end();
}
bool ProfileInfoCache::IconIndexIsUnique(size_t icon_index) const {
for (size_t i = 0; i < GetNumberOfProfiles(); ++i) {
if (GetAvatarIconIndexOfProfileAtIndex(i) == icon_index)
return false;
}
return true;
}
bool ProfileInfoCache::ChooseAvatarIconIndexForNewProfile(
bool allow_generic_icon,
bool must_be_unique,
size_t* out_icon_index) const {
// Always allow all icons for new profiles if using the
// --new-avatar-menu flag.
if (switches::IsNewAvatarMenu())
allow_generic_icon = true;
size_t start = allow_generic_icon ? 0 : profiles::GetGenericAvatarIconCount();
size_t end = profiles::GetDefaultAvatarIconCount();
size_t count = end - start;
int rand = base::RandInt(0, count);
for (size_t i = 0; i < count; ++i) {
size_t icon_index = start + (rand + i) % count;
if (!must_be_unique || IconIndexIsUnique(icon_index)) {
*out_icon_index = icon_index;
return true;
}
}
return false;
}
void ProfileInfoCache::UpdateSortForProfileIndex(size_t index) {
base::string16 name = GetNameOfProfileAtIndex(index);
// Remove and reinsert key in |sorted_keys_| to alphasort.
std::string key = CacheKeyFromProfilePath(GetPathOfProfileAtIndex(index));
std::vector<std::string>::iterator key_it =
std::find(sorted_keys_.begin(), sorted_keys_.end(), key);
DCHECK(key_it != sorted_keys_.end());
sorted_keys_.erase(key_it);
sorted_keys_.insert(FindPositionForProfile(key, name), key);
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED,
content::NotificationService::AllSources(),
content::NotificationService::NoDetails());
}
const gfx::Image* ProfileInfoCache::GetHighResAvatarOfProfileAtIndex(
size_t index) const {
int avatar_index = GetAvatarIconIndexOfProfileAtIndex(index);
std::string key = profiles::GetDefaultAvatarIconFileNameAtIndex(avatar_index);
if (!strcmp(key.c_str(), profiles::GetNoHighResAvatarFileName()))
return NULL;
base::FilePath image_path =
profiles::GetPathOfHighResAvatarAtIndex(avatar_index);
return LoadAvatarPictureFromPath(GetPathOfProfileAtIndex(index),
key, image_path);
}
void ProfileInfoCache::DownloadHighResAvatar(
size_t icon_index,
const base::FilePath& profile_path) {
// Downloading is only supported on desktop.
#if defined(OS_ANDROID) || defined(OS_IOS) || defined(OS_CHROMEOS)
return;
#endif
const std::string file_name =
profiles::GetDefaultAvatarIconFileNameAtIndex(icon_index);
// If the file is already being downloaded, don't start another download.
if (avatar_images_downloads_in_progress_.count(file_name))
return;
// Start the download for this file. The cache takes ownership of the
// |avatar_downloader|, which will be deleted when the download completes, or
// if that never happens, when the ProfileInfoCache is destroyed.
ProfileAvatarDownloader* avatar_downloader = new ProfileAvatarDownloader(
icon_index,
profile_path,
this);
avatar_images_downloads_in_progress_[file_name] = avatar_downloader;
avatar_downloader->Start();
}
const gfx::Image* ProfileInfoCache::LoadAvatarPictureFromPath(
const base::FilePath& profile_path,
const std::string& key,
const base::FilePath& image_path) const {
// If the picture is already loaded then use it.
if (cached_avatar_images_.count(key)) {
if (cached_avatar_images_[key]->IsEmpty())
return NULL;
return cached_avatar_images_[key];
}
// If the picture is already being loaded then don't try loading it again.
if (cached_avatar_images_loading_[key])
return NULL;
cached_avatar_images_loading_[key] = true;
gfx::Image** image = new gfx::Image*;
BrowserThread::PostTaskAndReply(BrowserThread::FILE, FROM_HERE,
base::Bind(&ReadBitmap, image_path, image),
base::Bind(&ProfileInfoCache::OnAvatarPictureLoaded,
const_cast<ProfileInfoCache*>(this)->AsWeakPtr(),
profile_path, key, image));
return NULL;
}
void ProfileInfoCache::OnAvatarPictureLoaded(const base::FilePath& profile_path,
const std::string& key,
gfx::Image** image) const {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
cached_avatar_images_loading_[key] = false;
delete cached_avatar_images_[key];
if (*image) {
cached_avatar_images_[key] = *image;
} else {
// Place an empty image in the cache to avoid reloading it again.
cached_avatar_images_[key] = new gfx::Image();
}
delete image;
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED,
content::NotificationService::AllSources(),
content::NotificationService::NoDetails());
FOR_EACH_OBSERVER(ProfileInfoCacheObserver,
observer_list_,
OnProfileHighResAvatarLoaded(profile_path));
}
void ProfileInfoCache::OnAvatarPictureSaved(
const std::string& file_name,
const base::FilePath& profile_path) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_PROFILE_CACHE_PICTURE_SAVED,
content::NotificationService::AllSources(),
content::NotificationService::NoDetails());
FOR_EACH_OBSERVER(ProfileInfoCacheObserver,
observer_list_,
OnProfileHighResAvatarLoaded(profile_path));
}
void ProfileInfoCache::MigrateLegacyProfileNamesAndDownloadAvatars() {
DCHECK(switches::IsNewAvatarMenu());
// Only do this on desktop platforms.
#if !defined(OS_ANDROID) && !defined(OS_IOS) && !defined(OS_CHROMEOS)
// Migrate any legacy profile names ("First user", "Default Profile") to
// new style default names ("Person 1"). The problem here is that every
// time you rename a profile, the ProfileInfoCache sorts itself, so
// whatever you were iterating through is no longer valid. We need to
// save a list of the profile paths (which thankfully do not change) that
// need to be renamed. We also can't pre-compute the new names, as they
// depend on the names of all the other profiles in the info cache, so they
// need to be re-computed after each rename.
std::vector<base::FilePath> profiles_to_rename;
const base::string16 default_profile_name = base::i18n::ToLower(
l10n_util::GetStringUTF16(IDS_DEFAULT_PROFILE_NAME));
const base::string16 default_legacy_profile_name = base::i18n::ToLower(
l10n_util::GetStringUTF16(IDS_LEGACY_DEFAULT_PROFILE_NAME));
for (size_t i = 0; i < GetNumberOfProfiles(); i++) {
DownloadHighResAvatarIfNeeded(GetAvatarIconIndexOfProfileAtIndex(i),
GetPathOfProfileAtIndex(i));
base::string16 name = base::i18n::ToLower(GetNameOfProfileAtIndex(i));
if (name == default_profile_name || name == default_legacy_profile_name)
profiles_to_rename.push_back(GetPathOfProfileAtIndex(i));
}
// Rename the necessary profiles.
std::vector<base::FilePath>::const_iterator it;
for (it = profiles_to_rename.begin(); it != profiles_to_rename.end(); ++it) {
size_t profile_index = GetIndexOfProfileWithPath(*it);
SetProfileIsUsingDefaultNameAtIndex(profile_index, true);
// This will assign a new "Person %d" type name and re-sort the cache.
SetNameOfProfileAtIndex(profile_index, ChooseNameForNewProfile(
GetAvatarIconIndexOfProfileAtIndex(profile_index)));
}
#endif
}
|