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 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 | 222 |
223 void ProfileInfoCache::AddObserver(ProfileInfoCacheObserver* obs) { | 223 void ProfileInfoCache::AddObserver(ProfileInfoCacheObserver* obs) { |
224 observer_list_.AddObserver(obs); | 224 observer_list_.AddObserver(obs); |
225 } | 225 } |
226 | 226 |
227 void ProfileInfoCache::RemoveObserver(ProfileInfoCacheObserver* obs) { | 227 void ProfileInfoCache::RemoveObserver(ProfileInfoCacheObserver* obs) { |
228 observer_list_.RemoveObserver(obs); | 228 observer_list_.RemoveObserver(obs); |
229 } | 229 } |
230 | 230 |
231 void ProfileInfoCache::DeleteProfileFromCache(const FilePath& profile_path) { | 231 void ProfileInfoCache::DeleteProfileFromCache(const FilePath& profile_path) { |
232 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache); | 232 string16 name = GetNameOfProfileAtIndex( |
233 DictionaryValue* cache = update.Get(); | 233 GetIndexOfProfileWithPath(profile_path)); |
234 | |
235 std::string key = CacheKeyFromProfilePath(profile_path); | |
236 DictionaryValue* info = NULL; | |
237 cache->GetDictionary(key, &info); | |
238 string16 name; | |
239 info->GetString(kNameKey, &name); | |
240 | 234 |
241 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, | 235 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, |
242 observer_list_, | 236 observer_list_, |
243 OnProfileRemoved(name)); | 237 OnProfileRemoved(name)); |
244 | 238 |
| 239 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache); |
| 240 DictionaryValue* cache = update.Get(); |
| 241 std::string key = CacheKeyFromProfilePath(profile_path); |
245 cache->Remove(key, NULL); | 242 cache->Remove(key, NULL); |
246 sorted_keys_.erase(std::find(sorted_keys_.begin(), sorted_keys_.end(), key)); | 243 sorted_keys_.erase(std::find(sorted_keys_.begin(), sorted_keys_.end(), key)); |
247 | 244 |
248 content::NotificationService::current()->Notify( | 245 content::NotificationService::current()->Notify( |
249 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, | 246 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, |
250 content::NotificationService::AllSources(), | 247 content::NotificationService::AllSources(), |
251 content::NotificationService::NoDetails()); | 248 content::NotificationService::NoDetails()); |
252 } | 249 } |
253 | 250 |
254 size_t ProfileInfoCache::GetNumberOfProfiles() const { | 251 size_t ProfileInfoCache::GetNumberOfProfiles() const { |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
397 DLOG(WARNING) << "Unknown avatar icon: " << icon_url; | 394 DLOG(WARNING) << "Unknown avatar icon: " << icon_url; |
398 return GetDefaultAvatarIconResourceIDAtIndex(0); | 395 return GetDefaultAvatarIconResourceIDAtIndex(0); |
399 } | 396 } |
400 | 397 |
401 void ProfileInfoCache::SetNameOfProfileAtIndex(size_t index, | 398 void ProfileInfoCache::SetNameOfProfileAtIndex(size_t index, |
402 const string16& name) { | 399 const string16& name) { |
403 if (name == GetNameOfProfileAtIndex(index)) | 400 if (name == GetNameOfProfileAtIndex(index)) |
404 return; | 401 return; |
405 | 402 |
406 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); | 403 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); |
407 string16 old_name; | 404 string16 old_name = GetNameOfProfileAtIndex(index); |
408 info->GetString(kNameKey, &old_name); | |
409 info->SetString(kNameKey, name); | 405 info->SetString(kNameKey, name); |
410 // This takes ownership of |info|. | 406 // This takes ownership of |info|. |
411 SetInfoForProfileAtIndex(index, info.release()); | 407 SetInfoForProfileAtIndex(index, info.release()); |
412 UpdateSortForProfileIndex(index); | 408 UpdateSortForProfileIndex(index); |
413 | 409 |
414 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, | 410 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, |
415 observer_list_, | 411 observer_list_, |
416 OnProfileNameChanged(old_name, name)); | 412 OnProfileNameChanged(old_name, name)); |
417 } | 413 } |
418 | 414 |
419 void ProfileInfoCache::SetUserNameOfProfileAtIndex(size_t index, | 415 void ProfileInfoCache::SetUserNameOfProfileAtIndex(size_t index, |
420 const string16& user_name) { | 416 const string16& user_name) { |
421 if (user_name == GetUserNameOfProfileAtIndex(index)) | 417 if (user_name == GetUserNameOfProfileAtIndex(index)) |
422 return; | 418 return; |
423 | 419 |
424 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); | 420 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); |
425 info->SetString(kUserNameKey, user_name); | 421 info->SetString(kUserNameKey, user_name); |
426 // This takes ownership of |info|. | 422 // This takes ownership of |info|. |
427 SetInfoForProfileAtIndex(index, info.release()); | 423 SetInfoForProfileAtIndex(index, info.release()); |
428 } | 424 } |
429 | 425 |
430 void ProfileInfoCache::SetAvatarIconOfProfileAtIndex(size_t index, | 426 void ProfileInfoCache::SetAvatarIconOfProfileAtIndex(size_t index, |
431 size_t icon_index) { | 427 size_t icon_index) { |
432 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); | 428 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); |
433 string16 name; | |
434 info->GetString(kNameKey, &name); | |
435 info->SetString(kAvatarIconKey, GetDefaultAvatarIconUrl(icon_index)); | 429 info->SetString(kAvatarIconKey, GetDefaultAvatarIconUrl(icon_index)); |
436 // This takes ownership of |info|. | 430 // This takes ownership of |info|. |
437 SetInfoForProfileAtIndex(index, info.release()); | 431 SetInfoForProfileAtIndex(index, info.release()); |
438 | 432 |
| 433 string16 name = GetNameOfProfileAtIndex(index); |
439 FilePath profile_path = GetPathOfProfileAtIndex(index); | 434 FilePath profile_path = GetPathOfProfileAtIndex(index); |
440 std::string key = CacheKeyFromProfilePath(profile_path); | 435 std::string key = CacheKeyFromProfilePath(profile_path); |
441 gfx::Image& avatar_img = | 436 gfx::Image& avatar_img = |
442 ResourceBundle::GetSharedInstance().GetNativeImageNamed( | 437 ResourceBundle::GetSharedInstance().GetNativeImageNamed( |
443 GetDefaultAvatarIconResourceIDAtIndex(icon_index)); | 438 GetDefaultAvatarIconResourceIDAtIndex(icon_index)); |
444 | 439 |
445 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, | 440 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, |
446 observer_list_, | 441 observer_list_, |
447 OnProfileAvatarChanged(name, UTF8ToUTF16(key), | 442 OnProfileAvatarChanged(name, UTF8ToUTF16(key), |
448 profile_path, &avatar_img)); | 443 profile_path, &avatar_img)); |
(...skipping 21 matching lines...) Expand all Loading... |
470 SetInfoForProfileAtIndex(index, info.release()); | 465 SetInfoForProfileAtIndex(index, info.release()); |
471 UpdateSortForProfileIndex(index); | 466 UpdateSortForProfileIndex(index); |
472 } | 467 } |
473 | 468 |
474 void ProfileInfoCache::SetIsUsingGAIANameOfProfileAtIndex(size_t index, | 469 void ProfileInfoCache::SetIsUsingGAIANameOfProfileAtIndex(size_t index, |
475 bool value) { | 470 bool value) { |
476 if (value == IsUsingGAIANameOfProfileAtIndex(index)) | 471 if (value == IsUsingGAIANameOfProfileAtIndex(index)) |
477 return; | 472 return; |
478 | 473 |
479 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); | 474 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); |
| 475 string16 old_name; |
| 476 info->GetString(kNameKey, &old_name); |
480 info->SetBoolean(kUseGAIANameKey, value); | 477 info->SetBoolean(kUseGAIANameKey, value); |
481 // This takes ownership of |info|. | 478 // This takes ownership of |info|. |
482 SetInfoForProfileAtIndex(index, info.release()); | 479 SetInfoForProfileAtIndex(index, info.release()); |
| 480 string16 new_name = GetGAIANameOfProfileAtIndex(index); |
483 UpdateSortForProfileIndex(index); | 481 UpdateSortForProfileIndex(index); |
| 482 |
| 483 if (value) { |
| 484 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, |
| 485 observer_list_, |
| 486 OnProfileNameChanged(old_name, new_name)); |
| 487 } |
484 } | 488 } |
485 | 489 |
486 void ProfileInfoCache::SetGAIAPictureOfProfileAtIndex(size_t index, | 490 void ProfileInfoCache::SetGAIAPictureOfProfileAtIndex(size_t index, |
487 const gfx::Image* image) { | 491 const gfx::Image* image) { |
488 FilePath path = GetPathOfProfileAtIndex(index); | 492 FilePath path = GetPathOfProfileAtIndex(index); |
489 std::string key = CacheKeyFromProfilePath(path); | 493 std::string key = CacheKeyFromProfilePath(path); |
490 | 494 |
491 // Delete the old bitmap from cache. | 495 // Delete the old bitmap from cache. |
492 std::map<std::string, gfx::Image*>::iterator it = gaia_pictures_.find(key); | 496 std::map<std::string, gfx::Image*>::iterator it = gaia_pictures_.find(key); |
493 if (it != gaia_pictures_.end()) { | 497 if (it != gaia_pictures_.end()) { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
527 | 531 |
528 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); | 532 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); |
529 info->SetString(kGAIAPictureFileNameKey, new_file_name); | 533 info->SetString(kGAIAPictureFileNameKey, new_file_name); |
530 // This takes ownership of |info|. | 534 // This takes ownership of |info|. |
531 SetInfoForProfileAtIndex(index, info.release()); | 535 SetInfoForProfileAtIndex(index, info.release()); |
532 } | 536 } |
533 | 537 |
534 void ProfileInfoCache::SetIsUsingGAIAPictureOfProfileAtIndex(size_t index, | 538 void ProfileInfoCache::SetIsUsingGAIAPictureOfProfileAtIndex(size_t index, |
535 bool value) { | 539 bool value) { |
536 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); | 540 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); |
537 string16 name; | 541 string16 name = GetNameOfProfileAtIndex(index); |
538 info->GetString(kNameKey, &name); | |
539 info->SetBoolean(kUseGAIAPictureKey, value); | 542 info->SetBoolean(kUseGAIAPictureKey, value); |
540 // This takes ownership of |info|. | 543 // This takes ownership of |info|. |
541 SetInfoForProfileAtIndex(index, info.release()); | 544 SetInfoForProfileAtIndex(index, info.release()); |
542 | 545 |
543 // Retrieve some info to update observers who care about avatar changes. | 546 // Retrieve some info to update observers who care about avatar changes. |
544 if (value) { | 547 if (value) { |
545 FilePath profile_path = GetPathOfProfileAtIndex(index); | 548 FilePath profile_path = GetPathOfProfileAtIndex(index); |
546 std::string key = CacheKeyFromProfilePath(profile_path); | 549 std::string key = CacheKeyFromProfilePath(profile_path); |
547 if (gaia_pictures_.find(key) != gaia_pictures_.end()) { | 550 if (gaia_pictures_.find(key) != gaia_pictures_.end()) { |
548 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, | 551 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, |
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
761 info->GetString(kNameKey, &name); | 764 info->GetString(kNameKey, &name); |
762 names.push_back(name); | 765 names.push_back(name); |
763 } | 766 } |
764 return names; | 767 return names; |
765 } | 768 } |
766 | 769 |
767 // static | 770 // static |
768 void ProfileInfoCache::RegisterPrefs(PrefService* prefs) { | 771 void ProfileInfoCache::RegisterPrefs(PrefService* prefs) { |
769 prefs->RegisterDictionaryPref(prefs::kProfileInfoCache); | 772 prefs->RegisterDictionaryPref(prefs::kProfileInfoCache); |
770 } | 773 } |
OLD | NEW |