| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/format_macros.h" | 9 #include "base/format_macros.h" |
| 10 #include "base/i18n/case_conversion.h" | 10 #include "base/i18n/case_conversion.h" |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 scoped_ptr<DictionaryValue> info(new DictionaryValue); | 198 scoped_ptr<DictionaryValue> info(new DictionaryValue); |
| 199 info->SetString(kNameKey, name); | 199 info->SetString(kNameKey, name); |
| 200 info->SetString(kUserNameKey, username); | 200 info->SetString(kUserNameKey, username); |
| 201 info->SetString(kAvatarIconKey, GetDefaultAvatarIconUrl(icon_index)); | 201 info->SetString(kAvatarIconKey, GetDefaultAvatarIconUrl(icon_index)); |
| 202 // Default value for whether background apps are running is false. | 202 // Default value for whether background apps are running is false. |
| 203 info->SetBoolean(kBackgroundAppsKey, false); | 203 info->SetBoolean(kBackgroundAppsKey, false); |
| 204 cache->Set(key, info.release()); | 204 cache->Set(key, info.release()); |
| 205 | 205 |
| 206 sorted_keys_.insert(FindPositionForProfile(key, name), key); | 206 sorted_keys_.insert(FindPositionForProfile(key, name), key); |
| 207 | 207 |
| 208 gfx::Image& avatar_img = |
| 209 ResourceBundle::GetSharedInstance().GetNativeImageNamed( |
| 210 GetDefaultAvatarIconResourceIDAtIndex(icon_index)); |
| 211 |
| 208 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, | 212 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, |
| 209 observer_list_, | 213 observer_list_, |
| 210 OnProfileAdded(name, UTF8ToUTF16(key))); | 214 OnProfileAdded(name, UTF8ToUTF16(key), |
| 215 profile_path, &avatar_img)); |
| 211 | 216 |
| 212 content::NotificationService::current()->Notify( | 217 content::NotificationService::current()->Notify( |
| 213 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, | 218 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, |
| 214 content::NotificationService::AllSources(), | 219 content::NotificationService::AllSources(), |
| 215 content::NotificationService::NoDetails()); | 220 content::NotificationService::NoDetails()); |
| 216 } | 221 } |
| 217 | 222 |
| 218 void ProfileInfoCache::AddObserver(ProfileInfoCacheObserver* obs) { | 223 void ProfileInfoCache::AddObserver(ProfileInfoCacheObserver* obs) { |
| 219 observer_list_.AddObserver(obs); | 224 observer_list_.AddObserver(obs); |
| 220 } | 225 } |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 const gfx::Image& ProfileInfoCache::GetAvatarIconOfProfileAtIndex( | 289 const gfx::Image& ProfileInfoCache::GetAvatarIconOfProfileAtIndex( |
| 285 size_t index) const { | 290 size_t index) const { |
| 286 if (IsUsingGAIAPictureOfProfileAtIndex(index)) { | 291 if (IsUsingGAIAPictureOfProfileAtIndex(index)) { |
| 287 const gfx::Image* image = GetGAIAPictureOfProfileAtIndex(index); | 292 const gfx::Image* image = GetGAIAPictureOfProfileAtIndex(index); |
| 288 if (image) | 293 if (image) |
| 289 return *image; | 294 return *image; |
| 290 } | 295 } |
| 291 | 296 |
| 292 int resource_id = GetDefaultAvatarIconResourceIDAtIndex( | 297 int resource_id = GetDefaultAvatarIconResourceIDAtIndex( |
| 293 GetAvatarIconIndexOfProfileAtIndex(index)); | 298 GetAvatarIconIndexOfProfileAtIndex(index)); |
| 294 return ResourceBundle::GetSharedInstance().GetImageNamed(resource_id); | 299 return ResourceBundle::GetSharedInstance().GetNativeImageNamed(resource_id); |
| 295 } | 300 } |
| 296 | 301 |
| 297 bool ProfileInfoCache::GetBackgroundStatusOfProfileAtIndex( | 302 bool ProfileInfoCache::GetBackgroundStatusOfProfileAtIndex( |
| 298 size_t index) const { | 303 size_t index) const { |
| 299 bool background_app_status; | 304 bool background_app_status; |
| 300 GetInfoForProfileAtIndex(index)->GetBoolean(kBackgroundAppsKey, | 305 GetInfoForProfileAtIndex(index)->GetBoolean(kBackgroundAppsKey, |
| 301 &background_app_status); | 306 &background_app_status); |
| 302 return background_app_status; | 307 return background_app_status; |
| 303 } | 308 } |
| 304 | 309 |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 418 | 423 |
| 419 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); | 424 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); |
| 420 info->SetString(kUserNameKey, user_name); | 425 info->SetString(kUserNameKey, user_name); |
| 421 // This takes ownership of |info|. | 426 // This takes ownership of |info|. |
| 422 SetInfoForProfileAtIndex(index, info.release()); | 427 SetInfoForProfileAtIndex(index, info.release()); |
| 423 } | 428 } |
| 424 | 429 |
| 425 void ProfileInfoCache::SetAvatarIconOfProfileAtIndex(size_t index, | 430 void ProfileInfoCache::SetAvatarIconOfProfileAtIndex(size_t index, |
| 426 size_t icon_index) { | 431 size_t icon_index) { |
| 427 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); | 432 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); |
| 433 string16 name; |
| 434 info->GetString(kNameKey, &name); |
| 428 info->SetString(kAvatarIconKey, GetDefaultAvatarIconUrl(icon_index)); | 435 info->SetString(kAvatarIconKey, GetDefaultAvatarIconUrl(icon_index)); |
| 429 // This takes ownership of |info|. | 436 // This takes ownership of |info|. |
| 430 SetInfoForProfileAtIndex(index, info.release()); | 437 SetInfoForProfileAtIndex(index, info.release()); |
| 438 |
| 439 FilePath profile_path = GetPathOfProfileAtIndex(index); |
| 440 std::string key = CacheKeyFromProfilePath(profile_path); |
| 441 gfx::Image& avatar_img = |
| 442 ResourceBundle::GetSharedInstance().GetNativeImageNamed( |
| 443 GetDefaultAvatarIconResourceIDAtIndex(icon_index)); |
| 444 |
| 445 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, |
| 446 observer_list_, |
| 447 OnProfileAvatarChanged(name, UTF8ToUTF16(key), |
| 448 profile_path, &avatar_img)); |
| 431 } | 449 } |
| 432 | 450 |
| 433 void ProfileInfoCache::SetBackgroundStatusOfProfileAtIndex( | 451 void ProfileInfoCache::SetBackgroundStatusOfProfileAtIndex( |
| 434 size_t index, | 452 size_t index, |
| 435 bool running_background_apps) { | 453 bool running_background_apps) { |
| 436 if (GetBackgroundStatusOfProfileAtIndex(index) == running_background_apps) | 454 if (GetBackgroundStatusOfProfileAtIndex(index) == running_background_apps) |
| 437 return; | 455 return; |
| 438 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); | 456 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); |
| 439 info->SetBoolean(kBackgroundAppsKey, running_background_apps); | 457 info->SetBoolean(kBackgroundAppsKey, running_background_apps); |
| 440 // This takes ownership of |info|. | 458 // This takes ownership of |info|. |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 509 | 527 |
| 510 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); | 528 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); |
| 511 info->SetString(kGAIAPictureFileNameKey, new_file_name); | 529 info->SetString(kGAIAPictureFileNameKey, new_file_name); |
| 512 // This takes ownership of |info|. | 530 // This takes ownership of |info|. |
| 513 SetInfoForProfileAtIndex(index, info.release()); | 531 SetInfoForProfileAtIndex(index, info.release()); |
| 514 } | 532 } |
| 515 | 533 |
| 516 void ProfileInfoCache::SetIsUsingGAIAPictureOfProfileAtIndex(size_t index, | 534 void ProfileInfoCache::SetIsUsingGAIAPictureOfProfileAtIndex(size_t index, |
| 517 bool value) { | 535 bool value) { |
| 518 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); | 536 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); |
| 537 string16 name; |
| 538 info->GetString(kNameKey, &name); |
| 519 info->SetBoolean(kUseGAIAPictureKey, value); | 539 info->SetBoolean(kUseGAIAPictureKey, value); |
| 520 // This takes ownership of |info|. | 540 // This takes ownership of |info|. |
| 521 SetInfoForProfileAtIndex(index, info.release()); | 541 SetInfoForProfileAtIndex(index, info.release()); |
| 542 |
| 543 // Retrieve some info to update observers who care about avatar changes. |
| 544 if (value) { |
| 545 FilePath profile_path = GetPathOfProfileAtIndex(index); |
| 546 std::string key = CacheKeyFromProfilePath(profile_path); |
| 547 if (gaia_pictures_.find(key) != gaia_pictures_.end()) { |
| 548 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, |
| 549 observer_list_, |
| 550 OnProfileAvatarChanged(name, UTF8ToUTF16(key), |
| 551 profile_path, |
| 552 gaia_pictures_[key])); |
| 553 } |
| 554 } |
| 522 } | 555 } |
| 523 | 556 |
| 524 string16 ProfileInfoCache::ChooseNameForNewProfile(size_t icon_index) { | 557 string16 ProfileInfoCache::ChooseNameForNewProfile(size_t icon_index) { |
| 525 string16 name; | 558 string16 name; |
| 526 for (int name_index = 1; ; ++name_index) { | 559 for (int name_index = 1; ; ++name_index) { |
| 527 if (icon_index < kGenericIconCount) { | 560 if (icon_index < kGenericIconCount) { |
| 528 name = l10n_util::GetStringFUTF16Int(IDS_NUMBERED_PROFILE_NAME, | 561 name = l10n_util::GetStringFUTF16Int(IDS_NUMBERED_PROFILE_NAME, |
| 529 name_index); | 562 name_index); |
| 530 } else { | 563 } else { |
| 531 name = l10n_util::GetStringUTF16( | 564 name = l10n_util::GetStringUTF16( |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 733 info->GetString(kNameKey, &name); | 766 info->GetString(kNameKey, &name); |
| 734 names.push_back(name); | 767 names.push_back(name); |
| 735 } | 768 } |
| 736 return names; | 769 return names; |
| 737 } | 770 } |
| 738 | 771 |
| 739 // static | 772 // static |
| 740 void ProfileInfoCache::RegisterPrefs(PrefService* prefs) { | 773 void ProfileInfoCache::RegisterPrefs(PrefService* prefs) { |
| 741 prefs->RegisterDictionaryPref(prefs::kProfileInfoCache); | 774 prefs->RegisterDictionaryPref(prefs::kProfileInfoCache); |
| 742 } | 775 } |
| OLD | NEW |