Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(278)

Side by Side Diff: chrome/browser/profiles/profile_info_cache.cc

Issue 9271018: Merge 115621 - Refactor ProfileInfoCacheObserver interface and usage thereof. (Closed) Base URL: svn://svn.chromium.org/chrome/branches/963/src/
Patch Set: Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
212 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, 208 FOR_EACH_OBSERVER(ProfileInfoCacheObserver,
213 observer_list_, 209 observer_list_,
214 OnProfileAdded(name, UTF8ToUTF16(key), 210 OnProfileAdded(profile_path));
215 profile_path, &avatar_img));
216 211
217 content::NotificationService::current()->Notify( 212 content::NotificationService::current()->Notify(
218 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, 213 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED,
219 content::NotificationService::AllSources(), 214 content::NotificationService::AllSources(),
220 content::NotificationService::NoDetails()); 215 content::NotificationService::NoDetails());
221 } 216 }
222 217
223 void ProfileInfoCache::AddObserver(ProfileInfoCacheObserver* obs) { 218 void ProfileInfoCache::AddObserver(ProfileInfoCacheObserver* obs) {
224 observer_list_.AddObserver(obs); 219 observer_list_.AddObserver(obs);
225 } 220 }
226 221
227 void ProfileInfoCache::RemoveObserver(ProfileInfoCacheObserver* obs) { 222 void ProfileInfoCache::RemoveObserver(ProfileInfoCacheObserver* obs) {
228 observer_list_.RemoveObserver(obs); 223 observer_list_.RemoveObserver(obs);
229 } 224 }
230 225
231 void ProfileInfoCache::DeleteProfileFromCache(const FilePath& profile_path) { 226 void ProfileInfoCache::DeleteProfileFromCache(const FilePath& profile_path) {
232 string16 name = GetNameOfProfileAtIndex( 227 string16 name = GetNameOfProfileAtIndex(
233 GetIndexOfProfileWithPath(profile_path)); 228 GetIndexOfProfileWithPath(profile_path));
234 229
235 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, 230 FOR_EACH_OBSERVER(ProfileInfoCacheObserver,
236 observer_list_, 231 observer_list_,
237 OnProfileWillBeRemoved(name)); 232 OnProfileWillBeRemoved(profile_path));
238 233
239 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache); 234 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache);
240 DictionaryValue* cache = update.Get(); 235 DictionaryValue* cache = update.Get();
241 std::string key = CacheKeyFromProfilePath(profile_path); 236 std::string key = CacheKeyFromProfilePath(profile_path);
242 cache->Remove(key, NULL); 237 cache->Remove(key, NULL);
243 sorted_keys_.erase(std::find(sorted_keys_.begin(), sorted_keys_.end(), key)); 238 sorted_keys_.erase(std::find(sorted_keys_.begin(), sorted_keys_.end(), key));
244 239
245 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, 240 FOR_EACH_OBSERVER(ProfileInfoCacheObserver,
246 observer_list_, 241 observer_list_,
247 OnProfileWasRemoved(name)); 242 OnProfileWasRemoved(profile_path, name));
248 243
249 content::NotificationService::current()->Notify( 244 content::NotificationService::current()->Notify(
250 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, 245 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED,
251 content::NotificationService::AllSources(), 246 content::NotificationService::AllSources(),
252 content::NotificationService::NoDetails()); 247 content::NotificationService::NoDetails());
253 } 248 }
254 249
255 size_t ProfileInfoCache::GetNumberOfProfiles() const { 250 size_t ProfileInfoCache::GetNumberOfProfiles() const {
256 return sorted_keys_.size(); 251 return sorted_keys_.size();
257 } 252 }
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
405 string16 current_name; 400 string16 current_name;
406 info->GetString(kNameKey, &current_name); 401 info->GetString(kNameKey, &current_name);
407 if (name == current_name) 402 if (name == current_name)
408 return; 403 return;
409 404
410 string16 old_display_name = GetNameOfProfileAtIndex(index); 405 string16 old_display_name = GetNameOfProfileAtIndex(index);
411 info->SetString(kNameKey, name); 406 info->SetString(kNameKey, name);
412 // This takes ownership of |info|. 407 // This takes ownership of |info|.
413 SetInfoForProfileAtIndex(index, info.release()); 408 SetInfoForProfileAtIndex(index, info.release());
414 string16 new_display_name = GetNameOfProfileAtIndex(index); 409 string16 new_display_name = GetNameOfProfileAtIndex(index);
410 FilePath profile_path = GetPathOfProfileAtIndex(index);
415 UpdateSortForProfileIndex(index); 411 UpdateSortForProfileIndex(index);
416 412
417 if (old_display_name != new_display_name) { 413 if (old_display_name != new_display_name) {
418 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, 414 FOR_EACH_OBSERVER(ProfileInfoCacheObserver,
419 observer_list_, 415 observer_list_,
420 OnProfileNameChanged(old_display_name, new_display_name)); 416 OnProfileNameChanged(profile_path, old_display_name));
421 } 417 }
422 } 418 }
423 419
424 void ProfileInfoCache::SetUserNameOfProfileAtIndex(size_t index, 420 void ProfileInfoCache::SetUserNameOfProfileAtIndex(size_t index,
425 const string16& user_name) { 421 const string16& user_name) {
426 if (user_name == GetUserNameOfProfileAtIndex(index)) 422 if (user_name == GetUserNameOfProfileAtIndex(index))
427 return; 423 return;
428 424
429 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); 425 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy());
430 info->SetString(kUserNameKey, user_name); 426 info->SetString(kUserNameKey, user_name);
431 // This takes ownership of |info|. 427 // This takes ownership of |info|.
432 SetInfoForProfileAtIndex(index, info.release()); 428 SetInfoForProfileAtIndex(index, info.release());
433 } 429 }
434 430
435 void ProfileInfoCache::SetAvatarIconOfProfileAtIndex(size_t index, 431 void ProfileInfoCache::SetAvatarIconOfProfileAtIndex(size_t index,
436 size_t icon_index) { 432 size_t icon_index) {
437 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); 433 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy());
438 info->SetString(kAvatarIconKey, GetDefaultAvatarIconUrl(icon_index)); 434 info->SetString(kAvatarIconKey, GetDefaultAvatarIconUrl(icon_index));
439 // This takes ownership of |info|. 435 // This takes ownership of |info|.
440 SetInfoForProfileAtIndex(index, info.release()); 436 SetInfoForProfileAtIndex(index, info.release());
441 437
442 string16 name = GetNameOfProfileAtIndex(index);
443 FilePath profile_path = GetPathOfProfileAtIndex(index); 438 FilePath profile_path = GetPathOfProfileAtIndex(index);
444 std::string key = CacheKeyFromProfilePath(profile_path);
445 gfx::Image& avatar_img =
446 ResourceBundle::GetSharedInstance().GetNativeImageNamed(
447 GetDefaultAvatarIconResourceIDAtIndex(icon_index));
448
449 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, 439 FOR_EACH_OBSERVER(ProfileInfoCacheObserver,
450 observer_list_, 440 observer_list_,
451 OnProfileAvatarChanged(name, UTF8ToUTF16(key), 441 OnProfileAvatarChanged(profile_path));
452 profile_path, &avatar_img));
453 } 442 }
454 443
455 void ProfileInfoCache::SetBackgroundStatusOfProfileAtIndex( 444 void ProfileInfoCache::SetBackgroundStatusOfProfileAtIndex(
456 size_t index, 445 size_t index,
457 bool running_background_apps) { 446 bool running_background_apps) {
458 if (GetBackgroundStatusOfProfileAtIndex(index) == running_background_apps) 447 if (GetBackgroundStatusOfProfileAtIndex(index) == running_background_apps)
459 return; 448 return;
460 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); 449 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy());
461 info->SetBoolean(kBackgroundAppsKey, running_background_apps); 450 info->SetBoolean(kBackgroundAppsKey, running_background_apps);
462 // This takes ownership of |info|. 451 // This takes ownership of |info|.
463 SetInfoForProfileAtIndex(index, info.release()); 452 SetInfoForProfileAtIndex(index, info.release());
464 } 453 }
465 454
466 void ProfileInfoCache::SetGAIANameOfProfileAtIndex(size_t index, 455 void ProfileInfoCache::SetGAIANameOfProfileAtIndex(size_t index,
467 const string16& name) { 456 const string16& name) {
468 if (name == GetGAIANameOfProfileAtIndex(index)) 457 if (name == GetGAIANameOfProfileAtIndex(index))
469 return; 458 return;
470 459
471 string16 old_display_name = GetNameOfProfileAtIndex(index); 460 string16 old_display_name = GetNameOfProfileAtIndex(index);
472 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); 461 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy());
473 info->SetString(kGAIANameKey, name); 462 info->SetString(kGAIANameKey, name);
474 // This takes ownership of |info|. 463 // This takes ownership of |info|.
475 SetInfoForProfileAtIndex(index, info.release()); 464 SetInfoForProfileAtIndex(index, info.release());
476 string16 new_display_name = GetNameOfProfileAtIndex(index); 465 string16 new_display_name = GetNameOfProfileAtIndex(index);
466 FilePath profile_path = GetPathOfProfileAtIndex(index);
477 UpdateSortForProfileIndex(index); 467 UpdateSortForProfileIndex(index);
478 468
479 if (old_display_name != new_display_name) { 469 if (old_display_name != new_display_name) {
480 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, 470 FOR_EACH_OBSERVER(ProfileInfoCacheObserver,
481 observer_list_, 471 observer_list_,
482 OnProfileNameChanged(old_display_name, new_display_name)); 472 OnProfileNameChanged(profile_path, old_display_name));
483 } 473 }
484 } 474 }
485 475
486 void ProfileInfoCache::SetIsUsingGAIANameOfProfileAtIndex(size_t index, 476 void ProfileInfoCache::SetIsUsingGAIANameOfProfileAtIndex(size_t index,
487 bool value) { 477 bool value) {
488 if (value == IsUsingGAIANameOfProfileAtIndex(index)) 478 if (value == IsUsingGAIANameOfProfileAtIndex(index))
489 return; 479 return;
490 480
491 string16 old_display_name = GetNameOfProfileAtIndex(index); 481 string16 old_display_name = GetNameOfProfileAtIndex(index);
492 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); 482 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy());
493 info->SetBoolean(kUseGAIANameKey, value); 483 info->SetBoolean(kUseGAIANameKey, value);
494 // This takes ownership of |info|. 484 // This takes ownership of |info|.
495 SetInfoForProfileAtIndex(index, info.release()); 485 SetInfoForProfileAtIndex(index, info.release());
496 string16 new_display_name = GetNameOfProfileAtIndex(index); 486 string16 new_display_name = GetNameOfProfileAtIndex(index);
487 FilePath profile_path = GetPathOfProfileAtIndex(index);
497 UpdateSortForProfileIndex(index); 488 UpdateSortForProfileIndex(index);
498 489
499 if (old_display_name != new_display_name) { 490 if (old_display_name != new_display_name) {
500 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, 491 FOR_EACH_OBSERVER(ProfileInfoCacheObserver,
501 observer_list_, 492 observer_list_,
502 OnProfileNameChanged(old_display_name, new_display_name)); 493 OnProfileNameChanged(profile_path, old_display_name));
503 } 494 }
504 } 495 }
505 496
506 void ProfileInfoCache::SetGAIAPictureOfProfileAtIndex(size_t index, 497 void ProfileInfoCache::SetGAIAPictureOfProfileAtIndex(size_t index,
507 const gfx::Image* image) { 498 const gfx::Image* image) {
508 FilePath path = GetPathOfProfileAtIndex(index); 499 FilePath path = GetPathOfProfileAtIndex(index);
509 std::string key = CacheKeyFromProfilePath(path); 500 std::string key = CacheKeyFromProfilePath(path);
510 501
511 // Delete the old bitmap from cache. 502 // Delete the old bitmap from cache.
512 std::map<std::string, gfx::Image*>::iterator it = gaia_pictures_.find(key); 503 std::map<std::string, gfx::Image*>::iterator it = gaia_pictures_.find(key);
(...skipping 30 matching lines...) Expand all
543 base::Bind(&ProfileInfoCache::OnGAIAPictureSaved, AsWeakPtr(), 534 base::Bind(&ProfileInfoCache::OnGAIAPictureSaved, AsWeakPtr(),
544 path, success)); 535 path, success));
545 } 536 }
546 } 537 }
547 538
548 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); 539 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy());
549 info->SetString(kGAIAPictureFileNameKey, new_file_name); 540 info->SetString(kGAIAPictureFileNameKey, new_file_name);
550 // This takes ownership of |info|. 541 // This takes ownership of |info|.
551 SetInfoForProfileAtIndex(index, info.release()); 542 SetInfoForProfileAtIndex(index, info.release());
552 543
553 string16 name = GetNameOfProfileAtIndex(index);
554 const gfx::Image& avatar_image = GetAvatarIconOfProfileAtIndex(index);
555 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, 544 FOR_EACH_OBSERVER(ProfileInfoCacheObserver,
556 observer_list_, 545 observer_list_,
557 OnProfileAvatarChanged(name, UTF8ToUTF16(key), 546 OnProfileAvatarChanged(path));
558 path, &avatar_image));
559 } 547 }
560 548
561 void ProfileInfoCache::SetIsUsingGAIAPictureOfProfileAtIndex(size_t index, 549 void ProfileInfoCache::SetIsUsingGAIAPictureOfProfileAtIndex(size_t index,
562 bool value) { 550 bool value) {
563 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); 551 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy());
564 string16 name = GetNameOfProfileAtIndex(index);
565 info->SetBoolean(kUseGAIAPictureKey, value); 552 info->SetBoolean(kUseGAIAPictureKey, value);
566 // This takes ownership of |info|. 553 // This takes ownership of |info|.
567 SetInfoForProfileAtIndex(index, info.release()); 554 SetInfoForProfileAtIndex(index, info.release());
568 555
569 // Retrieve some info to update observers who care about avatar changes. 556 // Retrieve some info to update observers who care about avatar changes.
570 if (value) { 557 FilePath profile_path = GetPathOfProfileAtIndex(index);
571 FilePath profile_path = GetPathOfProfileAtIndex(index); 558 FOR_EACH_OBSERVER(ProfileInfoCacheObserver,
572 std::string key = CacheKeyFromProfilePath(profile_path); 559 observer_list_,
573 if (gaia_pictures_.find(key) != gaia_pictures_.end()) { 560 OnProfileAvatarChanged(profile_path));
574 FOR_EACH_OBSERVER(ProfileInfoCacheObserver,
575 observer_list_,
576 OnProfileAvatarChanged(name, UTF8ToUTF16(key),
577 profile_path,
578 gaia_pictures_[key]));
579 }
580 }
581 } 561 }
582 562
583 string16 ProfileInfoCache::ChooseNameForNewProfile(size_t icon_index) { 563 string16 ProfileInfoCache::ChooseNameForNewProfile(size_t icon_index) {
584 string16 name; 564 string16 name;
585 for (int name_index = 1; ; ++name_index) { 565 for (int name_index = 1; ; ++name_index) {
586 if (icon_index < kGenericIconCount) { 566 if (icon_index < kGenericIconCount) {
587 name = l10n_util::GetStringFUTF16Int(IDS_NUMBERED_PROFILE_NAME, 567 name = l10n_util::GetStringFUTF16Int(IDS_NUMBERED_PROFILE_NAME,
588 name_index); 568 name_index);
589 } else { 569 } else {
590 name = l10n_util::GetStringUTF16( 570 name = l10n_util::GetStringUTF16(
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
787 info->GetString(kNameKey, &name); 767 info->GetString(kNameKey, &name);
788 names.push_back(name); 768 names.push_back(name);
789 } 769 }
790 return names; 770 return names;
791 } 771 }
792 772
793 // static 773 // static
794 void ProfileInfoCache::RegisterPrefs(PrefService* prefs) { 774 void ProfileInfoCache::RegisterPrefs(PrefService* prefs) {
795 prefs->RegisterDictionaryPref(prefs::kProfileInfoCache); 775 prefs->RegisterDictionaryPref(prefs::kProfileInfoCache);
796 } 776 }
OLDNEW
« no previous file with comments | « chrome/browser/background/background_mode_manager_unittest.cc ('k') | chrome/browser/profiles/profile_info_cache_observer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698