| 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/avatar_menu_model.h" | 5 #include "chrome/browser/profiles/avatar_menu_model.h" |
| 6 | 6 |
| 7 #include "base/stl_util.h" | 7 #include "base/stl_util.h" |
| 8 #include "base/string_number_conversions.h" | 8 #include "base/string_number_conversions.h" |
| 9 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
| 10 #include "chrome/browser/profiles/avatar_menu_model_observer.h" | 10 #include "chrome/browser/profiles/avatar_menu_model_observer.h" |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 : icon(icon), | 70 : icon(icon), |
| 71 active(false), | 71 active(false), |
| 72 model_index(model_index) { | 72 model_index(model_index) { |
| 73 } | 73 } |
| 74 | 74 |
| 75 AvatarMenuModel::Item::~Item() { | 75 AvatarMenuModel::Item::~Item() { |
| 76 } | 76 } |
| 77 | 77 |
| 78 void AvatarMenuModel::SwitchToProfile(size_t index) { | 78 void AvatarMenuModel::SwitchToProfile(size_t index) { |
| 79 const Item& item = GetItemAt(index); | 79 const Item& item = GetItemAt(index); |
| 80 FilePath path = profile_info_->GetPathOfProfileAtIndex(item.model_index); | |
| 81 | 80 |
| 82 // This will be deleted by the manager after the profile is ready. | 81 // This will be deleted by the manager after the profile is ready. |
| 83 ProfileSwitchObserver* observer = new ProfileSwitchObserver(); | 82 ProfileSwitchObserver* observer = new ProfileSwitchObserver(); |
| 84 g_browser_process->profile_manager()->CreateProfileAsync( | 83 g_browser_process->profile_manager()->CreateProfileAsync( |
| 85 path, observer); | 84 item.path, observer); |
| 86 ProfileMetrics::LogProfileOpenMethod(ProfileMetrics::SWITCH_PROFILE_ICON); | 85 ProfileMetrics::LogProfileOpenMethod(ProfileMetrics::SWITCH_PROFILE_ICON); |
| 87 } | 86 } |
| 88 | 87 |
| 89 void AvatarMenuModel::EditProfile(size_t index) { | 88 void AvatarMenuModel::EditProfile(size_t index) { |
| 90 Browser* browser = browser_; | 89 Browser* browser = browser_; |
| 91 if (!browser) { | 90 if (!browser) { |
| 92 Profile* profile = g_browser_process->profile_manager()->GetProfileByPath( | 91 Profile* profile = g_browser_process->profile_manager()->GetProfileByPath( |
| 93 profile_info_->GetPathOfProfileAtIndex(GetItemAt(index).model_index)); | 92 GetItemAt(index).path); |
| 94 browser = Browser::Create(profile); | 93 browser = Browser::Create(profile); |
| 95 } | 94 } |
| 96 std::string page = chrome::kManageProfileSubPage; | 95 std::string page = chrome::kManageProfileSubPage; |
| 97 page += "#"; | 96 page += "#"; |
| 98 page += base::IntToString(static_cast<int>(index)); | 97 page += base::IntToString(static_cast<int>(index)); |
| 99 browser->ShowOptionsTab(page); | 98 browser->ShowOptionsTab(page); |
| 100 } | 99 } |
| 101 | 100 |
| 102 void AvatarMenuModel::AddNewProfile() { | 101 void AvatarMenuModel::AddNewProfile() { |
| 103 ProfileManager::CreateMultiProfileAsync(); | 102 ProfileManager::CreateMultiProfileAsync(); |
| 104 ProfileMetrics::LogProfileOpenMethod(ProfileMetrics::ADD_NEW_USER); | 103 ProfileMetrics::LogProfileOpenMethod(ProfileMetrics::ADD_NEW_USER); |
| 105 ProfileMetrics::LogProfileOpenMethod(ProfileMetrics::ADD_NEW_USER_ICON); | 104 ProfileMetrics::LogProfileOpenMethod(ProfileMetrics::ADD_NEW_USER_ICON); |
| 106 } | 105 } |
| 107 | 106 |
| 108 size_t AvatarMenuModel::GetNumberOfItems() { | 107 size_t AvatarMenuModel::GetNumberOfItems() { |
| 109 return items_.size(); | 108 return items_.size(); |
| 110 } | 109 } |
| 111 | 110 |
| 112 size_t AvatarMenuModel::GetActiveProfileIndex() { | 111 size_t AvatarMenuModel::GetActiveProfileIndex() { |
| 113 // During singleton profile deletion, this function can be called with no | 112 for (size_t i = 0; i < items_.size(); ++i) { |
| 114 // profiles in the model - crbug.com/102278 . | 113 if (items_[i]->active) |
| 115 if (items_.size() == 0) | 114 return i; |
| 116 return 0; | 115 } |
| 117 | 116 |
| 118 Profile* active_profile = NULL; | 117 // We can reach here during singleton profile deleting. |
| 119 if (!browser_) | 118 return 0; |
| 120 active_profile = ProfileManager::GetLastUsedProfile(); | |
| 121 else | |
| 122 active_profile = browser_->profile(); | |
| 123 | |
| 124 size_t index = | |
| 125 profile_info_->GetIndexOfProfileWithPath(active_profile->GetPath()); | |
| 126 | |
| 127 DCHECK_LT(index, items_.size()); | |
| 128 return index; | |
| 129 } | 119 } |
| 130 | 120 |
| 131 const AvatarMenuModel::Item& AvatarMenuModel::GetItemAt(size_t index) { | 121 const AvatarMenuModel::Item& AvatarMenuModel::GetItemAt(size_t index) { |
| 132 DCHECK_LT(index, items_.size()); | 122 DCHECK_LT(index, items_.size()); |
| 133 return *items_[index]; | 123 return *items_[index]; |
| 134 } | 124 } |
| 135 | 125 |
| 136 void AvatarMenuModel::Observe(int type, | 126 void AvatarMenuModel::Observe(int type, |
| 137 const content::NotificationSource& source, | 127 const content::NotificationSource& source, |
| 138 const content::NotificationDetails& details) { | 128 const content::NotificationDetails& details) { |
| 139 DCHECK_EQ(chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, type); | 129 DCHECK_EQ(chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, type); |
| 140 RebuildMenu(); | 130 RebuildMenu(); |
| 141 observer_->OnAvatarMenuModelChanged(this); | 131 observer_->OnAvatarMenuModelChanged(this); |
| 142 } | 132 } |
| 143 | 133 |
| 144 // static | 134 // static |
| 145 bool AvatarMenuModel::ShouldShowAvatarMenu() { | 135 bool AvatarMenuModel::ShouldShowAvatarMenu() { |
| 146 return ProfileManager::IsMultipleProfilesEnabled() && | 136 return ProfileManager::IsMultipleProfilesEnabled() && |
| 147 g_browser_process->profile_manager()->GetNumberOfProfiles() > 1; | 137 g_browser_process->profile_manager()->GetNumberOfProfiles() > 1; |
| 148 } | 138 } |
| 149 | 139 |
| 150 void AvatarMenuModel::RebuildMenu() { | 140 void AvatarMenuModel::RebuildMenu() { |
| 151 ClearMenu(); | 141 ClearMenu(); |
| 152 | 142 |
| 153 const size_t count = profile_info_->GetNumberOfProfiles(); | 143 std::vector<ProfileInfoEntry> entries( |
| 154 for (size_t i = 0; i < count; ++i) { | 144 profile_info_->GetProfilesSortedByName()); |
| 155 Item* item = new Item(i, profile_info_->GetAvatarIconOfProfileAtIndex(i)); | 145 for (size_t i = 0; i < entries.size(); ++i) { |
| 156 item->name = profile_info_->GetNameOfProfileAtIndex(i); | 146 const ProfileInfoEntry& entry = entries[i]; |
| 157 item->sync_state = profile_info_->GetUserNameOfProfileAtIndex(i); | 147 Item* item = new Item(i, entry.GetIcon()); |
| 148 item->name = entry.name(); |
| 149 item->sync_state = entry.user_name(); |
| 158 if (item->sync_state.empty()) { | 150 if (item->sync_state.empty()) { |
| 159 item->sync_state = l10n_util::GetStringUTF16( | 151 item->sync_state = l10n_util::GetStringUTF16( |
| 160 IDS_PROFILES_LOCAL_PROFILE_STATE); | 152 IDS_PROFILES_LOCAL_PROFILE_STATE); |
| 161 } | 153 } |
| 162 if (browser_) { | 154 if (browser_) |
| 163 FilePath path = profile_info_->GetPathOfProfileAtIndex(i); | 155 item->active = browser_->profile()->GetPath() == entry.path(); |
| 164 item->active = browser_->profile()->GetPath() == path; | |
| 165 } | |
| 166 items_.push_back(item); | 156 items_.push_back(item); |
| 167 } | 157 } |
| 168 } | 158 } |
| 169 | 159 |
| 170 void AvatarMenuModel::ClearMenu() { | 160 void AvatarMenuModel::ClearMenu() { |
| 171 STLDeleteContainerPointers(items_.begin(), items_.end()); | 161 STLDeleteContainerPointers(items_.begin(), items_.end()); |
| 172 items_.clear(); | 162 items_.clear(); |
| 173 } | 163 } |
| OLD | NEW |