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 <set> | 5 #include <set> |
6 | 6 |
7 #include "chrome/browser/profiles/profile_manager.h" | 7 #include "chrome/browser/profiles/profile_manager.h" |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 using content::BrowserThread; | 49 using content::BrowserThread; |
50 | 50 |
51 namespace { | 51 namespace { |
52 | 52 |
53 // Profiles that should be deleted on shutdown. | 53 // Profiles that should be deleted on shutdown. |
54 std::vector<FilePath>& ProfilesToDelete() { | 54 std::vector<FilePath>& ProfilesToDelete() { |
55 CR_DEFINE_STATIC_LOCAL(std::vector<FilePath>, profiles_to_delete, ()); | 55 CR_DEFINE_STATIC_LOCAL(std::vector<FilePath>, profiles_to_delete, ()); |
56 return profiles_to_delete; | 56 return profiles_to_delete; |
57 } | 57 } |
58 | 58 |
| 59 // Checks if any user prefs for |profile| have default values. |
| 60 bool HasAnyDefaultUserPrefs(Profile* profile) { |
| 61 const PrefService::Preference* avatar_index = |
| 62 profile->GetPrefs()->FindPreference(prefs::kProfileAvatarIndex); |
| 63 DCHECK(avatar_index); |
| 64 const PrefService::Preference* profile_name = |
| 65 profile->GetPrefs()->FindPreference(prefs::kProfileName); |
| 66 DCHECK(profile_name); |
| 67 return avatar_index->IsDefaultValue() || |
| 68 profile_name->IsDefaultValue(); |
| 69 } |
| 70 |
59 // Simple task to log the size of the current profile. | 71 // Simple task to log the size of the current profile. |
60 class ProfileSizeTask : public Task { | 72 class ProfileSizeTask : public Task { |
61 public: | 73 public: |
62 explicit ProfileSizeTask(Profile* profile); | 74 explicit ProfileSizeTask(Profile* profile); |
63 virtual ~ProfileSizeTask() {} | 75 virtual ~ProfileSizeTask() {} |
64 | 76 |
65 virtual void Run(); | 77 virtual void Run(); |
66 private: | 78 private: |
67 FilePath path_; | 79 FilePath path_; |
68 int extension_count_; | 80 int extension_count_; |
(...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
446 DCHECK(local_state); | 458 DCHECK(local_state); |
447 // Only keep track of profiles that we are managing; tests may create others. | 459 // Only keep track of profiles that we are managing; tests may create others. |
448 if (profiles_info_.find(last_active->GetPath()) != profiles_info_.end()) { | 460 if (profiles_info_.find(last_active->GetPath()) != profiles_info_.end()) { |
449 local_state->SetString(prefs::kProfileLastUsed, | 461 local_state->SetString(prefs::kProfileLastUsed, |
450 last_active->GetPath().BaseName().MaybeAsASCII()); | 462 last_active->GetPath().BaseName().MaybeAsASCII()); |
451 } | 463 } |
452 } | 464 } |
453 | 465 |
454 void ProfileManager::DoFinalInit(Profile* profile, bool go_off_the_record) { | 466 void ProfileManager::DoFinalInit(Profile* profile, bool go_off_the_record) { |
455 DoFinalInitForServices(profile, go_off_the_record); | 467 DoFinalInitForServices(profile, go_off_the_record); |
| 468 InitProfileUserPrefs(profile); |
456 AddProfileToCache(profile); | 469 AddProfileToCache(profile); |
457 DoFinalInitLogging(profile); | 470 DoFinalInitLogging(profile); |
458 } | 471 } |
459 | 472 |
460 void ProfileManager::DoFinalInitForServices(Profile* profile, | 473 void ProfileManager::DoFinalInitForServices(Profile* profile, |
461 bool go_off_the_record) { | 474 bool go_off_the_record) { |
462 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 475 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
463 profile->InitExtensions(!go_off_the_record); | 476 profile->InitExtensions(!go_off_the_record); |
464 if (!command_line.HasSwitch(switches::kDisableWebResources)) | 477 if (!command_line.HasSwitch(switches::kDisableWebResources)) |
465 profile->InitPromoResources(); | 478 profile->InitPromoResources(); |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
588 ProfileInfoCache& cache = GetProfileInfoCache(); | 601 ProfileInfoCache& cache = GetProfileInfoCache(); |
589 if (profile->GetPath().DirName() != cache.GetUserDataDir()) | 602 if (profile->GetPath().DirName() != cache.GetUserDataDir()) |
590 return; | 603 return; |
591 | 604 |
592 if (cache.GetIndexOfProfileWithPath(profile->GetPath()) != std::string::npos) | 605 if (cache.GetIndexOfProfileWithPath(profile->GetPath()) != std::string::npos) |
593 return; | 606 return; |
594 | 607 |
595 string16 username = UTF8ToUTF16(profile->GetPrefs()->GetString( | 608 string16 username = UTF8ToUTF16(profile->GetPrefs()->GetString( |
596 prefs::kGoogleServicesUsername)); | 609 prefs::kGoogleServicesUsername)); |
597 | 610 |
598 if (profile->GetPath() == GetDefaultProfileDir(cache.GetUserDataDir())) { | 611 // Profile name and avatar are set by InitProfileUserPrefs and stored in the |
599 cache.AddProfileToCache( | 612 // profile. Use those values to setup the cache entry. |
600 profile->GetPath(), | 613 string16 profile_name = UTF8ToUTF16(profile->GetPrefs()->GetString( |
601 l10n_util::GetStringUTF16(IDS_DEFAULT_PROFILE_NAME), username, 0); | 614 prefs::kProfileName)); |
602 } else { | 615 |
603 size_t icon_index = cache.ChooseAvatarIconIndexForNewProfile(); | 616 size_t icon_index = profile->GetPrefs()->GetInteger( |
604 cache.AddProfileToCache(profile->GetPath(), | 617 prefs::kProfileAvatarIndex); |
605 cache.ChooseNameForNewProfile(icon_index), | 618 |
606 username, | 619 cache.AddProfileToCache(profile->GetPath(), |
607 icon_index); | 620 profile_name, |
| 621 username, |
| 622 icon_index); |
| 623 } |
| 624 |
| 625 void ProfileManager::InitProfileUserPrefs(Profile* profile) { |
| 626 ProfileInfoCache& cache = GetProfileInfoCache(); |
| 627 |
| 628 if (profile->GetPath().DirName() != cache.GetUserDataDir()) |
| 629 return; |
| 630 |
| 631 // Initialize the user preferences (name and avatar) only if the profile |
| 632 // doesn't have default preferenc values for them. |
| 633 if (HasAnyDefaultUserPrefs(profile)) { |
| 634 size_t profile_cache_index = |
| 635 cache.GetIndexOfProfileWithPath(profile->GetPath()); |
| 636 // If the cache has an entry for this profile, use the cache data |
| 637 if (profile_cache_index != std::string::npos) { |
| 638 profile->GetPrefs()->SetInteger(prefs::kProfileAvatarIndex, |
| 639 cache.GetAvatarIconIndexOfProfileAtIndex(profile_cache_index)); |
| 640 profile->GetPrefs()->SetString(prefs::kProfileName, |
| 641 UTF16ToUTF8(cache.GetNameOfProfileAtIndex(profile_cache_index))); |
| 642 } else if (profile->GetPath() == |
| 643 GetDefaultProfileDir(cache.GetUserDataDir())) { |
| 644 profile->GetPrefs()->SetInteger(prefs::kProfileAvatarIndex, 0); |
| 645 profile->GetPrefs()->SetString(prefs::kProfileName, |
| 646 l10n_util::GetStringUTF8(IDS_DEFAULT_PROFILE_NAME)); |
| 647 } else { |
| 648 size_t icon_index = cache.ChooseAvatarIconIndexForNewProfile(); |
| 649 profile->GetPrefs()->SetInteger(prefs::kProfileAvatarIndex, icon_index); |
| 650 profile->GetPrefs()->SetString( |
| 651 prefs::kProfileName, |
| 652 UTF16ToUTF8(cache.ChooseNameForNewProfile(icon_index))); |
| 653 } |
608 } | 654 } |
609 } | 655 } |
610 | 656 |
611 bool ProfileManager::ShouldGoOffTheRecord() { | 657 bool ProfileManager::ShouldGoOffTheRecord() { |
612 bool go_off_the_record = false; | 658 bool go_off_the_record = false; |
613 #if defined(OS_CHROMEOS) | 659 #if defined(OS_CHROMEOS) |
614 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 660 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
615 if (!logged_in_ && | 661 if (!logged_in_ && |
616 (!command_line.HasSwitch(switches::kTestType) || | 662 (!command_line.HasSwitch(switches::kTestType) || |
617 command_line.HasSwitch(switches::kLoginProfile))) { | 663 command_line.HasSwitch(switches::kLoginProfile))) { |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
683 } | 729 } |
684 } | 730 } |
685 | 731 |
686 ProfileManagerWithoutInit::ProfileManagerWithoutInit( | 732 ProfileManagerWithoutInit::ProfileManagerWithoutInit( |
687 const FilePath& user_data_dir) : ProfileManager(user_data_dir) { | 733 const FilePath& user_data_dir) : ProfileManager(user_data_dir) { |
688 } | 734 } |
689 | 735 |
690 void ProfileManager::RegisterTestingProfile(Profile* profile, | 736 void ProfileManager::RegisterTestingProfile(Profile* profile, |
691 bool add_to_cache) { | 737 bool add_to_cache) { |
692 RegisterProfile(profile, true); | 738 RegisterProfile(profile, true); |
693 if (add_to_cache) | 739 if (add_to_cache){ |
| 740 InitProfileUserPrefs(profile); |
694 AddProfileToCache(profile); | 741 AddProfileToCache(profile); |
| 742 } |
695 } | 743 } |
696 | 744 |
697 #if defined(OS_WIN) | 745 #if defined(OS_WIN) |
698 void ProfileManager::RemoveProfileShortcutManagerForTesting() { | 746 void ProfileManager::RemoveProfileShortcutManagerForTesting() { |
699 profile_info_cache_->RemoveObserver(profile_shortcut_manager_.get()); | 747 profile_info_cache_->RemoveObserver(profile_shortcut_manager_.get()); |
700 } | 748 } |
701 #endif | 749 #endif |
702 | 750 |
703 void ProfileManager::RunCallbacks(const std::vector<CreateCallback>& callbacks, | 751 void ProfileManager::RunCallbacks(const std::vector<CreateCallback>& callbacks, |
704 Profile* profile, | 752 Profile* profile, |
705 Profile::CreateStatus status) { | 753 Profile::CreateStatus status) { |
706 for (size_t i = 0; i < callbacks.size(); ++i) | 754 for (size_t i = 0; i < callbacks.size(); ++i) |
707 callbacks[i].Run(profile, status); | 755 callbacks[i].Run(profile, status); |
708 } | 756 } |
OLD | NEW |