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
3 // found in the LICENSE file.
5 #include "chrome/browser/profiles/profile_info_cache.h"
8 #include "base/file_util.h"
9 #include "base/format_macros.h"
10 #include "base/i18n/case_conversion.h"
11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/rand_util.h"
14 #include "base/stl_util.h"
15 #include "base/string_number_conversions.h"
16 #include "base/string_piece.h"
17 #include "base/stringprintf.h"
18 #include "base/utf_string_conversions.h"
19 #include "base/values.h"
20 #include "chrome/browser/browser_process.h"
21 #include "chrome/browser/prefs/pref_service.h"
22 #include "chrome/browser/prefs/scoped_user_pref_update.h"
23 #include "chrome/common/chrome_notification_types.h"
24 #include "chrome/common/pref_names.h"
25 #include "content/public/browser/browser_thread.h"
26 #include "content/public/browser/notification_service.h"
27 #include "grit/generated_resources.h"
28 #include "grit/theme_resources.h"
29 #include "third_party/skia/include/core/SkBitmap.h"
30 #include "ui/base/l10n/l10n_util.h"
31 #include "ui/base/resource/resource_bundle.h"
32 #include "ui/gfx/image/image.h"
33 #include "ui/gfx/image/image_util.h"
35 using content::BrowserThread
;
39 const char kNameKey
[] = "name";
40 const char kShortcutNameKey
[] = "shortcut_name";
41 const char kGAIANameKey
[] = "gaia_name";
42 const char kUseGAIANameKey
[] = "use_gaia_name";
43 const char kUserNameKey
[] = "user_name";
44 const char kAvatarIconKey
[] = "avatar_icon";
45 const char kUseGAIAPictureKey
[] = "use_gaia_picture";
46 const char kBackgroundAppsKey
[] = "background_apps";
47 const char kHasMigratedToGAIAInfoKey
[] = "has_migrated_to_gaia_info";
48 const char kGAIAPictureFileNameKey
[] = "gaia_picture_file_name";
50 const char kDefaultUrlPrefix
[] = "chrome://theme/IDR_PROFILE_AVATAR_";
51 const char kGAIAPictureFileName
[] = "Google Profile Picture.png";
53 const int kDefaultAvatarIconResources
[] = {
64 IDR_PROFILE_AVATAR_10
,
65 IDR_PROFILE_AVATAR_11
,
66 IDR_PROFILE_AVATAR_12
,
67 IDR_PROFILE_AVATAR_13
,
68 IDR_PROFILE_AVATAR_14
,
69 IDR_PROFILE_AVATAR_15
,
70 IDR_PROFILE_AVATAR_16
,
71 IDR_PROFILE_AVATAR_17
,
72 IDR_PROFILE_AVATAR_18
,
73 IDR_PROFILE_AVATAR_19
,
74 IDR_PROFILE_AVATAR_20
,
75 IDR_PROFILE_AVATAR_21
,
76 IDR_PROFILE_AVATAR_22
,
77 IDR_PROFILE_AVATAR_23
,
78 IDR_PROFILE_AVATAR_24
,
79 IDR_PROFILE_AVATAR_25
,
82 const size_t kDefaultAvatarIconsCount
= arraysize(kDefaultAvatarIconResources
);
84 // The first 8 icons are generic.
85 const size_t kGenericIconCount
= 8;
87 // First eight are generic icons, which use IDS_NUMBERED_PROFILE_NAME.
88 const int kDefaultNames
[] = {
89 IDS_DEFAULT_AVATAR_NAME_8
,
90 IDS_DEFAULT_AVATAR_NAME_9
,
91 IDS_DEFAULT_AVATAR_NAME_10
,
92 IDS_DEFAULT_AVATAR_NAME_11
,
93 IDS_DEFAULT_AVATAR_NAME_12
,
94 IDS_DEFAULT_AVATAR_NAME_13
,
95 IDS_DEFAULT_AVATAR_NAME_14
,
96 IDS_DEFAULT_AVATAR_NAME_15
,
97 IDS_DEFAULT_AVATAR_NAME_16
,
98 IDS_DEFAULT_AVATAR_NAME_17
,
99 IDS_DEFAULT_AVATAR_NAME_18
,
100 IDS_DEFAULT_AVATAR_NAME_19
,
101 IDS_DEFAULT_AVATAR_NAME_20
,
102 IDS_DEFAULT_AVATAR_NAME_21
,
103 IDS_DEFAULT_AVATAR_NAME_22
,
104 IDS_DEFAULT_AVATAR_NAME_23
,
105 IDS_DEFAULT_AVATAR_NAME_24
,
106 IDS_DEFAULT_AVATAR_NAME_25
109 typedef std::vector
<unsigned char> ImageData
;
111 // Writes |data| to disk and takes ownership of the pointer. On completion
112 // |success| is set to true on success and false on failure.
113 void SaveBitmap(ImageData
* data
,
114 const FilePath
& image_path
,
116 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
117 scoped_ptr
<ImageData
> data_owner(data
);
120 // Make sure the destination directory exists.
121 FilePath dir
= image_path
.DirName();
122 if (!file_util::DirectoryExists(dir
) && !file_util::CreateDirectory(dir
)) {
123 LOG(ERROR
) << "Failed to create parent directory.";
127 if (file_util::WriteFile(image_path
,
128 reinterpret_cast<char*>(&(*data
)[0]),
129 data
->size()) == -1) {
130 LOG(ERROR
) << "Failed to save image to file.";
137 // Reads a PNG from disk and decodes it. If the bitmap was successfully read
138 // from disk the then |out_image| will contain the bitmap image, otherwise it
140 void ReadBitmap(const FilePath
& image_path
,
141 gfx::Image
** out_image
) {
142 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
145 std::string image_data
;
146 if (!file_util::ReadFileToString(image_path
, &image_data
)) {
147 LOG(ERROR
) << "Failed to read PNG file from disk.";
151 const unsigned char* data
=
152 reinterpret_cast<const unsigned char*>(image_data
.data());
153 gfx::Image
* image
= gfx::ImageFromPNGEncodedData(data
, image_data
.length());
155 LOG(ERROR
) << "Failed to decode PNG file.";
162 void DeleteBitmap(const FilePath
& image_path
) {
163 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
164 file_util::Delete(image_path
, false);
169 ProfileInfoCache::ProfileInfoCache(PrefService
* prefs
,
170 const FilePath
& user_data_dir
)
172 user_data_dir_(user_data_dir
) {
173 // Populate the cache
174 const DictionaryValue
* cache
=
175 prefs_
->GetDictionary(prefs::kProfileInfoCache
);
176 for (DictionaryValue::key_iterator it
= cache
->begin_keys();
177 it
!= cache
->end_keys(); ++it
) {
178 std::string key
= *it
;
179 const DictionaryValue
* info
= NULL
;
180 cache
->GetDictionary(key
, &info
);
182 info
->GetString(kNameKey
, &name
);
183 sorted_keys_
.insert(FindPositionForProfile(key
, name
), key
);
187 ProfileInfoCache::~ProfileInfoCache() {
188 STLDeleteContainerPairSecondPointers(
189 gaia_pictures_
.begin(), gaia_pictures_
.end());
192 void ProfileInfoCache::AddProfileToCache(const FilePath
& profile_path
,
193 const string16
& name
,
194 const string16
& username
,
196 std::string key
= CacheKeyFromProfilePath(profile_path
);
197 DictionaryPrefUpdate
update(prefs_
, prefs::kProfileInfoCache
);
198 DictionaryValue
* cache
= update
.Get();
200 scoped_ptr
<DictionaryValue
> info(new DictionaryValue
);
201 info
->SetString(kNameKey
, name
);
202 info
->SetString(kUserNameKey
, username
);
203 info
->SetString(kAvatarIconKey
, GetDefaultAvatarIconUrl(icon_index
));
204 // Default value for whether background apps are running is false.
205 info
->SetBoolean(kBackgroundAppsKey
, false);
206 cache
->Set(key
, info
.release());
208 sorted_keys_
.insert(FindPositionForProfile(key
, name
), key
);
210 FOR_EACH_OBSERVER(ProfileInfoCacheObserver
,
212 OnProfileAdded(profile_path
));
214 content::NotificationService::current()->Notify(
215 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED
,
216 content::NotificationService::AllSources(),
217 content::NotificationService::NoDetails());
220 void ProfileInfoCache::AddObserver(ProfileInfoCacheObserver
* obs
) {
221 observer_list_
.AddObserver(obs
);
224 void ProfileInfoCache::RemoveObserver(ProfileInfoCacheObserver
* obs
) {
225 observer_list_
.RemoveObserver(obs
);
228 void ProfileInfoCache::DeleteProfileFromCache(const FilePath
& profile_path
) {
229 size_t profile_index
= GetIndexOfProfileWithPath(profile_path
);
230 if (profile_index
== std::string::npos
) {
234 string16 name
= GetNameOfProfileAtIndex(profile_index
);
236 FOR_EACH_OBSERVER(ProfileInfoCacheObserver
,
238 OnProfileWillBeRemoved(profile_path
));
240 DictionaryPrefUpdate
update(prefs_
, prefs::kProfileInfoCache
);
241 DictionaryValue
* cache
= update
.Get();
242 std::string key
= CacheKeyFromProfilePath(profile_path
);
243 cache
->Remove(key
, NULL
);
244 sorted_keys_
.erase(std::find(sorted_keys_
.begin(), sorted_keys_
.end(), key
));
246 FOR_EACH_OBSERVER(ProfileInfoCacheObserver
,
248 OnProfileWasRemoved(profile_path
, name
));
250 content::NotificationService::current()->Notify(
251 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED
,
252 content::NotificationService::AllSources(),
253 content::NotificationService::NoDetails());
256 size_t ProfileInfoCache::GetNumberOfProfiles() const {
257 return sorted_keys_
.size();
260 size_t ProfileInfoCache::GetIndexOfProfileWithPath(
261 const FilePath
& profile_path
) const {
262 if (profile_path
.DirName() != user_data_dir_
)
263 return std::string::npos
;
264 std::string search_key
= CacheKeyFromProfilePath(profile_path
);
265 for (size_t i
= 0; i
< sorted_keys_
.size(); ++i
) {
266 if (sorted_keys_
[i
] == search_key
)
269 return std::string::npos
;
272 string16
ProfileInfoCache::GetNameOfProfileAtIndex(size_t index
) const {
274 if (IsUsingGAIANameOfProfileAtIndex(index
))
275 name
= GetGAIANameOfProfileAtIndex(index
);
277 GetInfoForProfileAtIndex(index
)->GetString(kNameKey
, &name
);
281 string16
ProfileInfoCache::GetShortcutNameOfProfileAtIndex(size_t index
)
283 string16 shortcut_name
;
284 GetInfoForProfileAtIndex(index
)->GetString(
285 kShortcutNameKey
, &shortcut_name
);
286 return shortcut_name
;
289 FilePath
ProfileInfoCache::GetPathOfProfileAtIndex(size_t index
) const {
290 return user_data_dir_
.AppendASCII(sorted_keys_
[index
]);
293 string16
ProfileInfoCache::GetUserNameOfProfileAtIndex(size_t index
) const {
295 GetInfoForProfileAtIndex(index
)->GetString(kUserNameKey
, &user_name
);
299 const gfx::Image
& ProfileInfoCache::GetAvatarIconOfProfileAtIndex(
300 size_t index
) const {
301 if (IsUsingGAIAPictureOfProfileAtIndex(index
)) {
302 const gfx::Image
* image
= GetGAIAPictureOfProfileAtIndex(index
);
307 int resource_id
= GetDefaultAvatarIconResourceIDAtIndex(
308 GetAvatarIconIndexOfProfileAtIndex(index
));
309 return ResourceBundle::GetSharedInstance().GetNativeImageNamed(resource_id
);
312 bool ProfileInfoCache::GetBackgroundStatusOfProfileAtIndex(
313 size_t index
) const {
314 bool background_app_status
;
315 if (!GetInfoForProfileAtIndex(index
)->GetBoolean(kBackgroundAppsKey
,
316 &background_app_status
)) {
319 return background_app_status
;
322 string16
ProfileInfoCache::GetGAIANameOfProfileAtIndex(size_t index
) const {
324 GetInfoForProfileAtIndex(index
)->GetString(kGAIANameKey
, &name
);
328 bool ProfileInfoCache::IsUsingGAIANameOfProfileAtIndex(size_t index
) const {
330 GetInfoForProfileAtIndex(index
)->GetBoolean(kUseGAIANameKey
, &value
);
334 const gfx::Image
* ProfileInfoCache::GetGAIAPictureOfProfileAtIndex(
335 size_t index
) const {
336 FilePath path
= GetPathOfProfileAtIndex(index
);
337 std::string key
= CacheKeyFromProfilePath(path
);
339 // If the picture is already loaded then use it.
340 if (gaia_pictures_
.count(key
)) {
341 if (gaia_pictures_
[key
]->IsEmpty())
343 return gaia_pictures_
[key
];
346 std::string file_name
;
347 GetInfoForProfileAtIndex(index
)->GetString(
348 kGAIAPictureFileNameKey
, &file_name
);
350 // If the picture is not on disk or it is already being loaded then return
352 if (file_name
.empty() || gaia_pictures_loading_
[key
])
355 gaia_pictures_loading_
[key
] = true;
356 FilePath image_path
= path
.AppendASCII(file_name
);
357 gfx::Image
** image
= new gfx::Image
*;
358 BrowserThread::PostTaskAndReply(BrowserThread::FILE, FROM_HERE
,
359 base::Bind(&ReadBitmap
, image_path
, image
),
360 base::Bind(&ProfileInfoCache::OnGAIAPictureLoaded
,
361 const_cast<ProfileInfoCache
*>(this)->AsWeakPtr(), path
, image
));
366 void ProfileInfoCache::OnGAIAPictureLoaded(const FilePath
& path
,
367 gfx::Image
** image
) const {
368 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
370 std::string key
= CacheKeyFromProfilePath(path
);
371 gaia_pictures_loading_
[key
] = false;
374 delete gaia_pictures_
[key
];
375 gaia_pictures_
[key
] = *image
;
377 // Place an empty image in the cache to avoid reloading it again.
378 gaia_pictures_
[key
] = new gfx::Image();
382 content::NotificationService::current()->Notify(
383 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED
,
384 content::NotificationService::AllSources(),
385 content::NotificationService::NoDetails());
388 void ProfileInfoCache::OnGAIAPictureSaved(const FilePath
& path
,
389 bool* success
) const {
390 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
393 content::NotificationService::current()->Notify(
394 chrome::NOTIFICATION_PROFILE_CACHE_PICTURE_SAVED
,
395 content::NotificationService::AllSources(),
396 content::NotificationService::NoDetails());
401 bool ProfileInfoCache::IsUsingGAIAPictureOfProfileAtIndex(
402 size_t index
) const {
404 GetInfoForProfileAtIndex(index
)->GetBoolean(kUseGAIAPictureKey
, &value
);
408 size_t ProfileInfoCache::GetAvatarIconIndexOfProfileAtIndex(size_t index
)
410 std::string icon_url
;
411 GetInfoForProfileAtIndex(index
)->GetString(kAvatarIconKey
, &icon_url
);
412 size_t icon_index
= 0;
413 if (IsDefaultAvatarIconUrl(icon_url
, &icon_index
))
416 DLOG(WARNING
) << "Unknown avatar icon: " << icon_url
;
417 return GetDefaultAvatarIconResourceIDAtIndex(0);
420 void ProfileInfoCache::SetNameOfProfileAtIndex(size_t index
,
421 const string16
& name
) {
422 scoped_ptr
<DictionaryValue
> info(GetInfoForProfileAtIndex(index
)->DeepCopy());
423 string16 current_name
;
424 info
->GetString(kNameKey
, ¤t_name
);
425 if (name
== current_name
)
428 string16 old_display_name
= GetNameOfProfileAtIndex(index
);
429 info
->SetString(kNameKey
, name
);
430 // This takes ownership of |info|.
431 SetInfoForProfileAtIndex(index
, info
.release());
432 string16 new_display_name
= GetNameOfProfileAtIndex(index
);
433 FilePath profile_path
= GetPathOfProfileAtIndex(index
);
434 UpdateSortForProfileIndex(index
);
436 if (old_display_name
!= new_display_name
) {
437 FOR_EACH_OBSERVER(ProfileInfoCacheObserver
,
439 OnProfileNameChanged(profile_path
, old_display_name
));
443 void ProfileInfoCache::SetShortcutNameOfProfileAtIndex(
445 const string16
& shortcut_name
) {
446 if (shortcut_name
== GetShortcutNameOfProfileAtIndex(index
))
448 scoped_ptr
<DictionaryValue
> info(GetInfoForProfileAtIndex(index
)->DeepCopy());
449 info
->SetString(kShortcutNameKey
, shortcut_name
);
450 // This takes ownership of |info|.
451 SetInfoForProfileAtIndex(index
, info
.release());
454 void ProfileInfoCache::SetUserNameOfProfileAtIndex(size_t index
,
455 const string16
& user_name
) {
456 if (user_name
== GetUserNameOfProfileAtIndex(index
))
459 scoped_ptr
<DictionaryValue
> info(GetInfoForProfileAtIndex(index
)->DeepCopy());
460 info
->SetString(kUserNameKey
, user_name
);
461 // This takes ownership of |info|.
462 SetInfoForProfileAtIndex(index
, info
.release());
465 void ProfileInfoCache::SetAvatarIconOfProfileAtIndex(size_t index
,
467 scoped_ptr
<DictionaryValue
> info(GetInfoForProfileAtIndex(index
)->DeepCopy());
468 info
->SetString(kAvatarIconKey
, GetDefaultAvatarIconUrl(icon_index
));
469 // This takes ownership of |info|.
470 SetInfoForProfileAtIndex(index
, info
.release());
472 FilePath profile_path
= GetPathOfProfileAtIndex(index
);
473 FOR_EACH_OBSERVER(ProfileInfoCacheObserver
,
475 OnProfileAvatarChanged(profile_path
));
478 void ProfileInfoCache::SetBackgroundStatusOfProfileAtIndex(
480 bool running_background_apps
) {
481 if (GetBackgroundStatusOfProfileAtIndex(index
) == running_background_apps
)
483 scoped_ptr
<DictionaryValue
> info(GetInfoForProfileAtIndex(index
)->DeepCopy());
484 info
->SetBoolean(kBackgroundAppsKey
, running_background_apps
);
485 // This takes ownership of |info|.
486 SetInfoForProfileAtIndex(index
, info
.release());
489 void ProfileInfoCache::SetGAIANameOfProfileAtIndex(size_t index
,
490 const string16
& name
) {
491 if (name
== GetGAIANameOfProfileAtIndex(index
))
494 string16 old_display_name
= GetNameOfProfileAtIndex(index
);
495 scoped_ptr
<DictionaryValue
> info(GetInfoForProfileAtIndex(index
)->DeepCopy());
496 info
->SetString(kGAIANameKey
, name
);
497 // This takes ownership of |info|.
498 SetInfoForProfileAtIndex(index
, info
.release());
499 string16 new_display_name
= GetNameOfProfileAtIndex(index
);
500 FilePath profile_path
= GetPathOfProfileAtIndex(index
);
501 UpdateSortForProfileIndex(index
);
503 if (old_display_name
!= new_display_name
) {
504 FOR_EACH_OBSERVER(ProfileInfoCacheObserver
,
506 OnProfileNameChanged(profile_path
, old_display_name
));
510 void ProfileInfoCache::SetIsUsingGAIANameOfProfileAtIndex(size_t index
,
512 if (value
== IsUsingGAIANameOfProfileAtIndex(index
))
515 string16 old_display_name
= GetNameOfProfileAtIndex(index
);
516 scoped_ptr
<DictionaryValue
> info(GetInfoForProfileAtIndex(index
)->DeepCopy());
517 info
->SetBoolean(kUseGAIANameKey
, value
);
518 // This takes ownership of |info|.
519 SetInfoForProfileAtIndex(index
, info
.release());
520 string16 new_display_name
= GetNameOfProfileAtIndex(index
);
521 FilePath profile_path
= GetPathOfProfileAtIndex(index
);
522 UpdateSortForProfileIndex(index
);
524 if (old_display_name
!= new_display_name
) {
525 FOR_EACH_OBSERVER(ProfileInfoCacheObserver
,
527 OnProfileNameChanged(profile_path
, old_display_name
));
531 void ProfileInfoCache::SetGAIAPictureOfProfileAtIndex(size_t index
,
532 const gfx::Image
* image
) {
533 FilePath path
= GetPathOfProfileAtIndex(index
);
534 std::string key
= CacheKeyFromProfilePath(path
);
536 // Delete the old bitmap from cache.
537 std::map
<std::string
, gfx::Image
*>::iterator it
= gaia_pictures_
.find(key
);
538 if (it
!= gaia_pictures_
.end()) {
540 gaia_pictures_
.erase(it
);
543 std::string old_file_name
;
544 GetInfoForProfileAtIndex(index
)->GetString(
545 kGAIAPictureFileNameKey
, &old_file_name
);
546 std::string new_file_name
;
549 // Delete the old bitmap from disk.
550 if (!old_file_name
.empty()) {
551 FilePath image_path
= path
.AppendASCII(old_file_name
);
552 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE
,
553 base::Bind(&DeleteBitmap
, image_path
));
556 // Save the new bitmap to disk.
557 gaia_pictures_
[key
] = new gfx::Image(*image
);
558 scoped_ptr
<ImageData
> data(new ImageData
);
559 if (!gfx::PNGEncodedDataFromImage(*image
, data
.get())) {
560 LOG(ERROR
) << "Failed to PNG encode the image.";
563 old_file_name
.empty() ? kGAIAPictureFileName
: old_file_name
;
564 FilePath image_path
= path
.AppendASCII(new_file_name
);
565 bool* success
= new bool;
566 BrowserThread::PostTaskAndReply(BrowserThread::FILE, FROM_HERE
,
567 base::Bind(&SaveBitmap
, data
.release(), image_path
, success
),
568 base::Bind(&ProfileInfoCache::OnGAIAPictureSaved
, AsWeakPtr(),
573 scoped_ptr
<DictionaryValue
> info(GetInfoForProfileAtIndex(index
)->DeepCopy());
574 info
->SetString(kGAIAPictureFileNameKey
, new_file_name
);
575 // This takes ownership of |info|.
576 SetInfoForProfileAtIndex(index
, info
.release());
578 FOR_EACH_OBSERVER(ProfileInfoCacheObserver
,
580 OnProfileAvatarChanged(path
));
583 void ProfileInfoCache::SetIsUsingGAIAPictureOfProfileAtIndex(size_t index
,
585 scoped_ptr
<DictionaryValue
> info(GetInfoForProfileAtIndex(index
)->DeepCopy());
586 info
->SetBoolean(kUseGAIAPictureKey
, value
);
587 // This takes ownership of |info|.
588 SetInfoForProfileAtIndex(index
, info
.release());
590 // Retrieve some info to update observers who care about avatar changes.
591 FilePath profile_path
= GetPathOfProfileAtIndex(index
);
592 FOR_EACH_OBSERVER(ProfileInfoCacheObserver
,
594 OnProfileAvatarChanged(profile_path
));
597 string16
ProfileInfoCache::ChooseNameForNewProfile(size_t icon_index
) {
599 for (int name_index
= 1; ; ++name_index
) {
600 if (icon_index
< kGenericIconCount
) {
601 name
= l10n_util::GetStringFUTF16Int(IDS_NUMBERED_PROFILE_NAME
,
604 name
= l10n_util::GetStringUTF16(
605 kDefaultNames
[icon_index
- kGenericIconCount
]);
607 name
.append(UTF8ToUTF16(base::IntToString(name_index
)));
610 // Loop through previously named profiles to ensure we're not duplicating.
611 bool name_found
= false;
612 for (size_t i
= 0; i
< GetNumberOfProfiles(); ++i
) {
613 if (GetNameOfProfileAtIndex(i
) == name
) {
623 bool ProfileInfoCache::GetHasMigratedToGAIAInfoOfProfileAtIndex(
624 size_t index
) const {
626 GetInfoForProfileAtIndex(index
)->GetBoolean(
627 kHasMigratedToGAIAInfoKey
, &value
);
631 void ProfileInfoCache::SetHasMigratedToGAIAInfoOfProfileAtIndex(
632 size_t index
, bool value
) {
633 scoped_ptr
<DictionaryValue
> info(GetInfoForProfileAtIndex(index
)->DeepCopy());
634 info
->SetBoolean(kHasMigratedToGAIAInfoKey
, value
);
635 // This takes ownership of |info|.
636 SetInfoForProfileAtIndex(index
, info
.release());
639 bool ProfileInfoCache::IconIndexIsUnique(size_t icon_index
) const {
640 for (size_t i
= 0; i
< GetNumberOfProfiles(); ++i
) {
641 if (GetAvatarIconIndexOfProfileAtIndex(i
) == icon_index
)
647 bool ProfileInfoCache::ChooseAvatarIconIndexForNewProfile(
648 bool allow_generic_icon
,
650 size_t* out_icon_index
) const {
651 size_t start
= allow_generic_icon
? 0 : kGenericIconCount
;
652 size_t end
= GetDefaultAvatarIconCount();
653 size_t count
= end
- start
;
655 int rand
= base::RandInt(0, count
);
656 for (size_t i
= 0; i
< count
; ++i
) {
657 size_t icon_index
= start
+ (rand
+ i
) % count
;
658 if (!must_be_unique
|| IconIndexIsUnique(icon_index
)) {
659 *out_icon_index
= icon_index
;
667 size_t ProfileInfoCache::ChooseAvatarIconIndexForNewProfile() const {
668 size_t icon_index
= 0;
669 // Try to find a unique, non-generic icon.
670 if (ChooseAvatarIconIndexForNewProfile(false, true, &icon_index
))
672 // Try to find any unique icon.
673 if (ChooseAvatarIconIndexForNewProfile(true, true, &icon_index
))
675 // Settle for any random icon, even if it's not unique.
676 if (ChooseAvatarIconIndexForNewProfile(true, false, &icon_index
))
683 const FilePath
& ProfileInfoCache::GetUserDataDir() const {
684 return user_data_dir_
;
688 size_t ProfileInfoCache::GetDefaultAvatarIconCount() {
689 return kDefaultAvatarIconsCount
;
693 int ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex(size_t index
) {
694 DCHECK(IsDefaultAvatarIconIndex(index
));
695 return kDefaultAvatarIconResources
[index
];
699 std::string
ProfileInfoCache::GetDefaultAvatarIconUrl(size_t index
) {
700 DCHECK(IsDefaultAvatarIconIndex(index
));
701 return StringPrintf("%s%" PRIuS
, kDefaultUrlPrefix
, index
);
705 bool ProfileInfoCache::IsDefaultAvatarIconIndex(size_t index
) {
706 return index
< kDefaultAvatarIconsCount
;
710 bool ProfileInfoCache::IsDefaultAvatarIconUrl(const std::string
& url
,
711 size_t* icon_index
) {
713 if (url
.find(kDefaultUrlPrefix
) != 0)
717 if (base::StringToInt(base::StringPiece(url
.begin() +
718 strlen(kDefaultUrlPrefix
),
722 int_value
>= static_cast<int>(kDefaultAvatarIconsCount
))
724 *icon_index
= int_value
;
731 const DictionaryValue
* ProfileInfoCache::GetInfoForProfileAtIndex(
732 size_t index
) const {
733 DCHECK_LT(index
, GetNumberOfProfiles());
734 const DictionaryValue
* cache
=
735 prefs_
->GetDictionary(prefs::kProfileInfoCache
);
736 const DictionaryValue
* info
= NULL
;
737 cache
->GetDictionary(sorted_keys_
[index
], &info
);
741 void ProfileInfoCache::SetInfoForProfileAtIndex(size_t index
,
742 DictionaryValue
* info
) {
743 DictionaryPrefUpdate
update(prefs_
, prefs::kProfileInfoCache
);
744 DictionaryValue
* cache
= update
.Get();
745 cache
->Set(sorted_keys_
[index
], info
);
747 content::NotificationService::current()->Notify(
748 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED
,
749 content::NotificationService::AllSources(),
750 content::NotificationService::NoDetails());
753 std::string
ProfileInfoCache::CacheKeyFromProfilePath(
754 const FilePath
& profile_path
) const {
755 DCHECK(user_data_dir_
== profile_path
.DirName());
756 FilePath base_name
= profile_path
.BaseName();
757 return base_name
.MaybeAsASCII();
760 std::vector
<std::string
>::iterator
ProfileInfoCache::FindPositionForProfile(
761 const std::string
& search_key
,
762 const string16
& search_name
) {
763 string16 search_name_l
= base::i18n::ToLower(search_name
);
764 for (size_t i
= 0; i
< GetNumberOfProfiles(); ++i
) {
765 string16 name_l
= base::i18n::ToLower(GetNameOfProfileAtIndex(i
));
766 int name_compare
= search_name_l
.compare(name_l
);
767 if (name_compare
< 0)
768 return sorted_keys_
.begin() + i
;
769 if (name_compare
== 0) {
770 int key_compare
= search_key
.compare(sorted_keys_
[i
]);
772 return sorted_keys_
.begin() + i
;
775 return sorted_keys_
.end();
778 void ProfileInfoCache::UpdateSortForProfileIndex(size_t index
) {
779 string16 name
= GetNameOfProfileAtIndex(index
);
781 // Remove and reinsert key in |sorted_keys_| to alphasort.
782 std::string key
= CacheKeyFromProfilePath(GetPathOfProfileAtIndex(index
));
783 std::vector
<std::string
>::iterator key_it
=
784 std::find(sorted_keys_
.begin(), sorted_keys_
.end(), key
);
785 DCHECK(key_it
!= sorted_keys_
.end());
786 sorted_keys_
.erase(key_it
);
787 sorted_keys_
.insert(FindPositionForProfile(key
, name
), key
);
789 content::NotificationService::current()->Notify(
790 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED
,
791 content::NotificationService::AllSources(),
792 content::NotificationService::NoDetails());
796 std::vector
<string16
> ProfileInfoCache::GetProfileNames() {
797 std::vector
<string16
> names
;
798 PrefService
* local_state
= g_browser_process
->local_state();
799 const DictionaryValue
* cache
= local_state
->GetDictionary(
800 prefs::kProfileInfoCache
);
802 for (base::DictionaryValue::key_iterator it
= cache
->begin_keys();
803 it
!= cache
->end_keys();
805 const base::DictionaryValue
* info
= NULL
;
806 cache
->GetDictionary(*it
, &info
);
807 info
->GetString(kNameKey
, &name
);
808 names
.push_back(name
);
814 void ProfileInfoCache::RegisterPrefs(PrefService
* prefs
) {
815 prefs
->RegisterDictionaryPref(prefs::kProfileInfoCache
);