| 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 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 scoped_ptr<DictionaryValue> info(new DictionaryValue); | 199 scoped_ptr<DictionaryValue> info(new DictionaryValue); |
| 200 info->SetString(kNameKey, name); | 200 info->SetString(kNameKey, name); |
| 201 info->SetString(kUserNameKey, username); | 201 info->SetString(kUserNameKey, username); |
| 202 info->SetString(kAvatarIconKey, GetDefaultAvatarIconUrl(icon_index)); | 202 info->SetString(kAvatarIconKey, GetDefaultAvatarIconUrl(icon_index)); |
| 203 // Default value for whether background apps are running is false. | 203 // Default value for whether background apps are running is false. |
| 204 info->SetBoolean(kBackgroundAppsKey, false); | 204 info->SetBoolean(kBackgroundAppsKey, false); |
| 205 cache->Set(key, info.release()); | 205 cache->Set(key, info.release()); |
| 206 | 206 |
| 207 sorted_keys_.insert(FindPositionForProfile(key, name), key); | 207 sorted_keys_.insert(FindPositionForProfile(key, name), key); |
| 208 | 208 |
| 209 gfx::Image& avatar_img = | |
| 210 ResourceBundle::GetSharedInstance().GetNativeImageNamed( | |
| 211 GetDefaultAvatarIconResourceIDAtIndex(icon_index)); | |
| 212 | |
| 213 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, | 209 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, |
| 214 observer_list_, | 210 observer_list_, |
| 215 OnProfileAdded(name, UTF8ToUTF16(key), | 211 OnProfileAdded(profile_path)); |
| 216 profile_path, &avatar_img)); | |
| 217 | 212 |
| 218 content::NotificationService::current()->Notify( | 213 content::NotificationService::current()->Notify( |
| 219 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, | 214 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, |
| 220 content::NotificationService::AllSources(), | 215 content::NotificationService::AllSources(), |
| 221 content::NotificationService::NoDetails()); | 216 content::NotificationService::NoDetails()); |
| 222 } | 217 } |
| 223 | 218 |
| 224 void ProfileInfoCache::AddObserver(ProfileInfoCacheObserver* obs) { | 219 void ProfileInfoCache::AddObserver(ProfileInfoCacheObserver* obs) { |
| 225 observer_list_.AddObserver(obs); | 220 observer_list_.AddObserver(obs); |
| 226 } | 221 } |
| 227 | 222 |
| 228 void ProfileInfoCache::RemoveObserver(ProfileInfoCacheObserver* obs) { | 223 void ProfileInfoCache::RemoveObserver(ProfileInfoCacheObserver* obs) { |
| 229 observer_list_.RemoveObserver(obs); | 224 observer_list_.RemoveObserver(obs); |
| 230 } | 225 } |
| 231 | 226 |
| 232 void ProfileInfoCache::DeleteProfileFromCache(const FilePath& profile_path) { | 227 void ProfileInfoCache::DeleteProfileFromCache(const FilePath& profile_path) { |
| 233 string16 name = GetNameOfProfileAtIndex( | 228 string16 name = GetNameOfProfileAtIndex( |
| 234 GetIndexOfProfileWithPath(profile_path)); | 229 GetIndexOfProfileWithPath(profile_path)); |
| 235 | 230 |
| 236 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, | 231 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, |
| 237 observer_list_, | 232 observer_list_, |
| 238 OnProfileWillBeRemoved(name)); | 233 OnProfileWillBeRemoved(profile_path)); |
| 239 | 234 |
| 240 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache); | 235 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache); |
| 241 DictionaryValue* cache = update.Get(); | 236 DictionaryValue* cache = update.Get(); |
| 242 std::string key = CacheKeyFromProfilePath(profile_path); | 237 std::string key = CacheKeyFromProfilePath(profile_path); |
| 243 cache->Remove(key, NULL); | 238 cache->Remove(key, NULL); |
| 244 sorted_keys_.erase(std::find(sorted_keys_.begin(), sorted_keys_.end(), key)); | 239 sorted_keys_.erase(std::find(sorted_keys_.begin(), sorted_keys_.end(), key)); |
| 245 | 240 |
| 246 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, | 241 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, |
| 247 observer_list_, | 242 observer_list_, |
| 248 OnProfileWasRemoved(name)); | 243 OnProfileWasRemoved(profile_path, name)); |
| 249 | 244 |
| 250 content::NotificationService::current()->Notify( | 245 content::NotificationService::current()->Notify( |
| 251 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, | 246 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, |
| 252 content::NotificationService::AllSources(), | 247 content::NotificationService::AllSources(), |
| 253 content::NotificationService::NoDetails()); | 248 content::NotificationService::NoDetails()); |
| 254 } | 249 } |
| 255 | 250 |
| 256 size_t ProfileInfoCache::GetNumberOfProfiles() const { | 251 size_t ProfileInfoCache::GetNumberOfProfiles() const { |
| 257 return sorted_keys_.size(); | 252 return sorted_keys_.size(); |
| 258 } | 253 } |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 407 string16 current_name; | 402 string16 current_name; |
| 408 info->GetString(kNameKey, ¤t_name); | 403 info->GetString(kNameKey, ¤t_name); |
| 409 if (name == current_name) | 404 if (name == current_name) |
| 410 return; | 405 return; |
| 411 | 406 |
| 412 string16 old_display_name = GetNameOfProfileAtIndex(index); | 407 string16 old_display_name = GetNameOfProfileAtIndex(index); |
| 413 info->SetString(kNameKey, name); | 408 info->SetString(kNameKey, name); |
| 414 // This takes ownership of |info|. | 409 // This takes ownership of |info|. |
| 415 SetInfoForProfileAtIndex(index, info.release()); | 410 SetInfoForProfileAtIndex(index, info.release()); |
| 416 string16 new_display_name = GetNameOfProfileAtIndex(index); | 411 string16 new_display_name = GetNameOfProfileAtIndex(index); |
| 412 FilePath profile_path = GetPathOfProfileAtIndex(index); |
| 417 UpdateSortForProfileIndex(index); | 413 UpdateSortForProfileIndex(index); |
| 418 | 414 |
| 419 if (old_display_name != new_display_name) { | 415 if (old_display_name != new_display_name) { |
| 420 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, | 416 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, |
| 421 observer_list_, | 417 observer_list_, |
| 422 OnProfileNameChanged(old_display_name, new_display_name)); | 418 OnProfileNameChanged(profile_path, old_display_name)); |
| 423 } | 419 } |
| 424 } | 420 } |
| 425 | 421 |
| 426 void ProfileInfoCache::SetUserNameOfProfileAtIndex(size_t index, | 422 void ProfileInfoCache::SetUserNameOfProfileAtIndex(size_t index, |
| 427 const string16& user_name) { | 423 const string16& user_name) { |
| 428 if (user_name == GetUserNameOfProfileAtIndex(index)) | 424 if (user_name == GetUserNameOfProfileAtIndex(index)) |
| 429 return; | 425 return; |
| 430 | 426 |
| 431 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); | 427 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); |
| 432 info->SetString(kUserNameKey, user_name); | 428 info->SetString(kUserNameKey, user_name); |
| 433 // This takes ownership of |info|. | 429 // This takes ownership of |info|. |
| 434 SetInfoForProfileAtIndex(index, info.release()); | 430 SetInfoForProfileAtIndex(index, info.release()); |
| 435 } | 431 } |
| 436 | 432 |
| 437 void ProfileInfoCache::SetAvatarIconOfProfileAtIndex(size_t index, | 433 void ProfileInfoCache::SetAvatarIconOfProfileAtIndex(size_t index, |
| 438 size_t icon_index) { | 434 size_t icon_index) { |
| 439 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); | 435 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); |
| 440 info->SetString(kAvatarIconKey, GetDefaultAvatarIconUrl(icon_index)); | 436 info->SetString(kAvatarIconKey, GetDefaultAvatarIconUrl(icon_index)); |
| 441 // This takes ownership of |info|. | 437 // This takes ownership of |info|. |
| 442 SetInfoForProfileAtIndex(index, info.release()); | 438 SetInfoForProfileAtIndex(index, info.release()); |
| 443 | 439 |
| 444 string16 name = GetNameOfProfileAtIndex(index); | |
| 445 FilePath profile_path = GetPathOfProfileAtIndex(index); | 440 FilePath profile_path = GetPathOfProfileAtIndex(index); |
| 446 std::string key = CacheKeyFromProfilePath(profile_path); | |
| 447 gfx::Image& avatar_img = | |
| 448 ResourceBundle::GetSharedInstance().GetNativeImageNamed( | |
| 449 GetDefaultAvatarIconResourceIDAtIndex(icon_index)); | |
| 450 | |
| 451 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, | 441 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, |
| 452 observer_list_, | 442 observer_list_, |
| 453 OnProfileAvatarChanged(name, UTF8ToUTF16(key), | 443 OnProfileAvatarChanged(profile_path)); |
| 454 profile_path, &avatar_img)); | |
| 455 } | 444 } |
| 456 | 445 |
| 457 void ProfileInfoCache::SetBackgroundStatusOfProfileAtIndex( | 446 void ProfileInfoCache::SetBackgroundStatusOfProfileAtIndex( |
| 458 size_t index, | 447 size_t index, |
| 459 bool running_background_apps) { | 448 bool running_background_apps) { |
| 460 if (GetBackgroundStatusOfProfileAtIndex(index) == running_background_apps) | 449 if (GetBackgroundStatusOfProfileAtIndex(index) == running_background_apps) |
| 461 return; | 450 return; |
| 462 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); | 451 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); |
| 463 info->SetBoolean(kBackgroundAppsKey, running_background_apps); | 452 info->SetBoolean(kBackgroundAppsKey, running_background_apps); |
| 464 // This takes ownership of |info|. | 453 // This takes ownership of |info|. |
| 465 SetInfoForProfileAtIndex(index, info.release()); | 454 SetInfoForProfileAtIndex(index, info.release()); |
| 466 } | 455 } |
| 467 | 456 |
| 468 void ProfileInfoCache::SetGAIANameOfProfileAtIndex(size_t index, | 457 void ProfileInfoCache::SetGAIANameOfProfileAtIndex(size_t index, |
| 469 const string16& name) { | 458 const string16& name) { |
| 470 if (name == GetGAIANameOfProfileAtIndex(index)) | 459 if (name == GetGAIANameOfProfileAtIndex(index)) |
| 471 return; | 460 return; |
| 472 | 461 |
| 473 string16 old_display_name = GetNameOfProfileAtIndex(index); | 462 string16 old_display_name = GetNameOfProfileAtIndex(index); |
| 474 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); | 463 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); |
| 475 info->SetString(kGAIANameKey, name); | 464 info->SetString(kGAIANameKey, name); |
| 476 // This takes ownership of |info|. | 465 // This takes ownership of |info|. |
| 477 SetInfoForProfileAtIndex(index, info.release()); | 466 SetInfoForProfileAtIndex(index, info.release()); |
| 478 string16 new_display_name = GetNameOfProfileAtIndex(index); | 467 string16 new_display_name = GetNameOfProfileAtIndex(index); |
| 468 FilePath profile_path = GetPathOfProfileAtIndex(index); |
| 479 UpdateSortForProfileIndex(index); | 469 UpdateSortForProfileIndex(index); |
| 480 | 470 |
| 481 if (old_display_name != new_display_name) { | 471 if (old_display_name != new_display_name) { |
| 482 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, | 472 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, |
| 483 observer_list_, | 473 observer_list_, |
| 484 OnProfileNameChanged(old_display_name, new_display_name)); | 474 OnProfileNameChanged(profile_path, old_display_name)); |
| 485 } | 475 } |
| 486 } | 476 } |
| 487 | 477 |
| 488 void ProfileInfoCache::SetIsUsingGAIANameOfProfileAtIndex(size_t index, | 478 void ProfileInfoCache::SetIsUsingGAIANameOfProfileAtIndex(size_t index, |
| 489 bool value) { | 479 bool value) { |
| 490 if (value == IsUsingGAIANameOfProfileAtIndex(index)) | 480 if (value == IsUsingGAIANameOfProfileAtIndex(index)) |
| 491 return; | 481 return; |
| 492 | 482 |
| 493 string16 old_display_name = GetNameOfProfileAtIndex(index); | 483 string16 old_display_name = GetNameOfProfileAtIndex(index); |
| 494 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); | 484 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); |
| 495 info->SetBoolean(kUseGAIANameKey, value); | 485 info->SetBoolean(kUseGAIANameKey, value); |
| 496 // This takes ownership of |info|. | 486 // This takes ownership of |info|. |
| 497 SetInfoForProfileAtIndex(index, info.release()); | 487 SetInfoForProfileAtIndex(index, info.release()); |
| 498 string16 new_display_name = GetNameOfProfileAtIndex(index); | 488 string16 new_display_name = GetNameOfProfileAtIndex(index); |
| 489 FilePath profile_path = GetPathOfProfileAtIndex(index); |
| 499 UpdateSortForProfileIndex(index); | 490 UpdateSortForProfileIndex(index); |
| 500 | 491 |
| 501 if (old_display_name != new_display_name) { | 492 if (old_display_name != new_display_name) { |
| 502 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, | 493 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, |
| 503 observer_list_, | 494 observer_list_, |
| 504 OnProfileNameChanged(old_display_name, new_display_name)); | 495 OnProfileNameChanged(profile_path, old_display_name)); |
| 505 } | 496 } |
| 506 } | 497 } |
| 507 | 498 |
| 508 void ProfileInfoCache::SetGAIAPictureOfProfileAtIndex(size_t index, | 499 void ProfileInfoCache::SetGAIAPictureOfProfileAtIndex(size_t index, |
| 509 const gfx::Image* image) { | 500 const gfx::Image* image) { |
| 510 FilePath path = GetPathOfProfileAtIndex(index); | 501 FilePath path = GetPathOfProfileAtIndex(index); |
| 511 std::string key = CacheKeyFromProfilePath(path); | 502 std::string key = CacheKeyFromProfilePath(path); |
| 512 | 503 |
| 513 // Delete the old bitmap from cache. | 504 // Delete the old bitmap from cache. |
| 514 std::map<std::string, gfx::Image*>::iterator it = gaia_pictures_.find(key); | 505 std::map<std::string, gfx::Image*>::iterator it = gaia_pictures_.find(key); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 545 base::Bind(&ProfileInfoCache::OnGAIAPictureSaved, AsWeakPtr(), | 536 base::Bind(&ProfileInfoCache::OnGAIAPictureSaved, AsWeakPtr(), |
| 546 path, success)); | 537 path, success)); |
| 547 } | 538 } |
| 548 } | 539 } |
| 549 | 540 |
| 550 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); | 541 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); |
| 551 info->SetString(kGAIAPictureFileNameKey, new_file_name); | 542 info->SetString(kGAIAPictureFileNameKey, new_file_name); |
| 552 // This takes ownership of |info|. | 543 // This takes ownership of |info|. |
| 553 SetInfoForProfileAtIndex(index, info.release()); | 544 SetInfoForProfileAtIndex(index, info.release()); |
| 554 | 545 |
| 555 string16 name = GetNameOfProfileAtIndex(index); | |
| 556 const gfx::Image& avatar_image = GetAvatarIconOfProfileAtIndex(index); | |
| 557 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, | 546 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, |
| 558 observer_list_, | 547 observer_list_, |
| 559 OnProfileAvatarChanged(name, UTF8ToUTF16(key), | 548 OnProfileAvatarChanged(path)); |
| 560 path, &avatar_image)); | |
| 561 } | 549 } |
| 562 | 550 |
| 563 void ProfileInfoCache::SetIsUsingGAIAPictureOfProfileAtIndex(size_t index, | 551 void ProfileInfoCache::SetIsUsingGAIAPictureOfProfileAtIndex(size_t index, |
| 564 bool value) { | 552 bool value) { |
| 565 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); | 553 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); |
| 566 string16 name = GetNameOfProfileAtIndex(index); | |
| 567 info->SetBoolean(kUseGAIAPictureKey, value); | 554 info->SetBoolean(kUseGAIAPictureKey, value); |
| 568 // This takes ownership of |info|. | 555 // This takes ownership of |info|. |
| 569 SetInfoForProfileAtIndex(index, info.release()); | 556 SetInfoForProfileAtIndex(index, info.release()); |
| 570 | 557 |
| 571 // Retrieve some info to update observers who care about avatar changes. | 558 // Retrieve some info to update observers who care about avatar changes. |
| 572 if (value) { | 559 FilePath profile_path = GetPathOfProfileAtIndex(index); |
| 573 FilePath profile_path = GetPathOfProfileAtIndex(index); | 560 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, |
| 574 std::string key = CacheKeyFromProfilePath(profile_path); | 561 observer_list_, |
| 575 if (gaia_pictures_.find(key) != gaia_pictures_.end()) { | 562 OnProfileAvatarChanged(profile_path)); |
| 576 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, | |
| 577 observer_list_, | |
| 578 OnProfileAvatarChanged(name, UTF8ToUTF16(key), | |
| 579 profile_path, | |
| 580 gaia_pictures_[key])); | |
| 581 } | |
| 582 } | |
| 583 } | 563 } |
| 584 | 564 |
| 585 string16 ProfileInfoCache::ChooseNameForNewProfile(size_t icon_index) { | 565 string16 ProfileInfoCache::ChooseNameForNewProfile(size_t icon_index) { |
| 586 string16 name; | 566 string16 name; |
| 587 for (int name_index = 1; ; ++name_index) { | 567 for (int name_index = 1; ; ++name_index) { |
| 588 if (icon_index < kGenericIconCount) { | 568 if (icon_index < kGenericIconCount) { |
| 589 name = l10n_util::GetStringFUTF16Int(IDS_NUMBERED_PROFILE_NAME, | 569 name = l10n_util::GetStringFUTF16Int(IDS_NUMBERED_PROFILE_NAME, |
| 590 name_index); | 570 name_index); |
| 591 } else { | 571 } else { |
| 592 name = l10n_util::GetStringUTF16( | 572 name = l10n_util::GetStringUTF16( |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 795 info->GetString(kNameKey, &name); | 775 info->GetString(kNameKey, &name); |
| 796 names.push_back(name); | 776 names.push_back(name); |
| 797 } | 777 } |
| 798 return names; | 778 return names; |
| 799 } | 779 } |
| 800 | 780 |
| 801 // static | 781 // static |
| 802 void ProfileInfoCache::RegisterPrefs(PrefService* prefs) { | 782 void ProfileInfoCache::RegisterPrefs(PrefService* prefs) { |
| 803 prefs->RegisterDictionaryPref(prefs::kProfileInfoCache); | 783 prefs->RegisterDictionaryPref(prefs::kProfileInfoCache); |
| 804 } | 784 } |
| OLD | NEW |