| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/metrics/field_trial.h" | 9 #include "base/metrics/field_trial.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 content::NotificationService::AllSources()); | 135 content::NotificationService::AllSources()); |
| 136 | 136 |
| 137 // Build the initial menu. | 137 // Build the initial menu. |
| 138 RebuildMenu(); | 138 RebuildMenu(); |
| 139 } | 139 } |
| 140 | 140 |
| 141 AvatarMenuModel::~AvatarMenuModel() { | 141 AvatarMenuModel::~AvatarMenuModel() { |
| 142 ClearMenu(); | 142 ClearMenu(); |
| 143 } | 143 } |
| 144 | 144 |
| 145 AvatarMenuModel::Item::Item(size_t model_index, const gfx::Image& icon) | |
| 146 : icon(icon), | |
| 147 active(false), | |
| 148 signed_in(false), | |
| 149 signin_required(false), | |
| 150 model_index(model_index) { | |
| 151 } | |
| 152 | |
| 153 AvatarMenuModel::Item::~Item() { | |
| 154 } | |
| 155 | |
| 156 void AvatarMenuModel::SwitchToProfile(size_t index, bool always_create) { | 145 void AvatarMenuModel::SwitchToProfile(size_t index, bool always_create) { |
| 157 DCHECK(profiles::IsMultipleProfilesEnabled() || | 146 DCHECK(profiles::IsMultipleProfilesEnabled() || |
| 158 index == GetActiveProfileIndex()); | 147 index == GetActiveProfileIndex()); |
| 159 const Item& item = GetItemAt(index); | 148 const ui::AvatarMenuItemModel& item = GetItemAt(index); |
| 160 base::FilePath path = | 149 base::FilePath path = |
| 161 profile_info_->GetPathOfProfileAtIndex(item.model_index); | 150 profile_info_->GetPathOfProfileAtIndex(item.model_index); |
| 162 | 151 |
| 163 chrome::HostDesktopType desktop_type = chrome::GetActiveDesktop(); | 152 chrome::HostDesktopType desktop_type = chrome::GetActiveDesktop(); |
| 164 if (browser_) | 153 if (browser_) |
| 165 desktop_type = browser_->host_desktop_type(); | 154 desktop_type = browser_->host_desktop_type(); |
| 166 | 155 |
| 167 profiles::SwitchToProfile(path, desktop_type, always_create); | 156 profiles::SwitchToProfile(path, desktop_type, always_create); |
| 168 ProfileMetrics::LogProfileSwitchUser(ProfileMetrics::SWITCH_PROFILE_ICON); | 157 ProfileMetrics::LogProfileSwitchUser(ProfileMetrics::SWITCH_PROFILE_ICON); |
| 169 } | 158 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 187 if (!browser) { | 176 if (!browser) { |
| 188 const Browser::CreateParams params(ProfileManager::GetLastUsedProfile(), | 177 const Browser::CreateParams params(ProfileManager::GetLastUsedProfile(), |
| 189 chrome::GetActiveDesktop()); | 178 chrome::GetActiveDesktop()); |
| 190 browser = new Browser(params); | 179 browser = new Browser(params); |
| 191 } | 180 } |
| 192 chrome::ShowSettingsSubPage(browser, chrome::kCreateProfileSubPage); | 181 chrome::ShowSettingsSubPage(browser, chrome::kCreateProfileSubPage); |
| 193 ProfileMetrics::LogProfileAddNewUser(type); | 182 ProfileMetrics::LogProfileAddNewUser(type); |
| 194 } | 183 } |
| 195 | 184 |
| 196 base::FilePath AvatarMenuModel::GetProfilePath(size_t index) { | 185 base::FilePath AvatarMenuModel::GetProfilePath(size_t index) { |
| 197 const Item& item = GetItemAt(index); | 186 const ui::AvatarMenuItemModel& item = GetItemAt(index); |
| 198 return profile_info_->GetPathOfProfileAtIndex(item.model_index); | 187 return profile_info_->GetPathOfProfileAtIndex(item.model_index); |
| 199 } | 188 } |
| 200 | 189 |
| 201 // static | 190 // static |
| 202 void AvatarMenuModel::SwitchToGuestProfileWindow(Browser* browser) { | 191 void AvatarMenuModel::SwitchToGuestProfileWindow(Browser* browser) { |
| 203 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 192 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 204 profile_manager->CreateProfileAsync(ProfileManager::GetGuestProfilePath(), | 193 profile_manager->CreateProfileAsync(ProfileManager::GetGuestProfilePath(), |
| 205 base::Bind(&OnGuestProfileCreated, | 194 base::Bind(&OnGuestProfileCreated, |
| 206 false, | 195 false, |
| 207 browser->host_desktop_type()), | 196 browser->host_desktop_type()), |
| (...skipping 18 matching lines...) Expand all Loading... |
| 226 else | 215 else |
| 227 active_profile = browser_->profile(); | 216 active_profile = browser_->profile(); |
| 228 | 217 |
| 229 size_t index = | 218 size_t index = |
| 230 profile_info_->GetIndexOfProfileWithPath(active_profile->GetPath()); | 219 profile_info_->GetIndexOfProfileWithPath(active_profile->GetPath()); |
| 231 | 220 |
| 232 DCHECK_LT(index, items_.size()); | 221 DCHECK_LT(index, items_.size()); |
| 233 return index; | 222 return index; |
| 234 } | 223 } |
| 235 | 224 |
| 236 const AvatarMenuModel::Item& AvatarMenuModel::GetItemAt(size_t index) { | 225 const ui::AvatarMenuItemModel& AvatarMenuModel::GetItemAt(size_t index) { |
| 237 DCHECK_LT(index, items_.size()); | 226 DCHECK_LT(index, items_.size()); |
| 238 return *items_[index]; | 227 return *items_[index]; |
| 239 } | 228 } |
| 240 | 229 |
| 241 bool AvatarMenuModel::ShouldShowAddNewProfileLink() const { | 230 bool AvatarMenuModel::ShouldShowAddNewProfileLink() const { |
| 242 // |browser_| can be NULL in unit_tests. | 231 // |browser_| can be NULL in unit_tests. |
| 243 return !browser_ || !browser_->profile()->IsManaged(); | 232 return !browser_ || !browser_->profile()->IsManaged(); |
| 244 } | 233 } |
| 245 | 234 |
| 246 base::string16 AvatarMenuModel::GetManagedUserInformation() const { | 235 base::string16 AvatarMenuModel::GetManagedUserInformation() const { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 287 return profiles::IsNewProfileManagementEnabled() || | 276 return profiles::IsNewProfileManagementEnabled() || |
| 288 (g_browser_process->profile_manager() && | 277 (g_browser_process->profile_manager() && |
| 289 g_browser_process->profile_manager()->GetNumberOfProfiles() > 1); | 278 g_browser_process->profile_manager()->GetNumberOfProfiles() > 1); |
| 290 } | 279 } |
| 291 return false; | 280 return false; |
| 292 } | 281 } |
| 293 | 282 |
| 294 void AvatarMenuModel::RebuildMenu() { | 283 void AvatarMenuModel::RebuildMenu() { |
| 295 ClearMenu(); | 284 ClearMenu(); |
| 296 | 285 |
| 297 const size_t count = profile_info_->GetNumberOfProfiles(); | 286 profiles::PopulateAvatarMenuItemModels( |
| 298 for (size_t i = 0; i < count; ++i) { | 287 profile_info_, |
| 299 bool is_gaia_picture = | 288 browser_ ? browser_->profile()->GetPath() |
| 300 profile_info_->IsUsingGAIAPictureOfProfileAtIndex(i) && | 289 : base::FilePath(), |
| 301 profile_info_->GetGAIAPictureOfProfileAtIndex(i); | 290 &items_); |
| 302 | |
| 303 gfx::Image icon = profile_info_->GetAvatarIconOfProfileAtIndex(i); | |
| 304 if (!CommandLine::ForCurrentProcess()->HasSwitch( | |
| 305 switches::kNewProfileManagement)) { | |
| 306 // old avatar menu uses resized-small images | |
| 307 icon = profiles::GetAvatarIconForMenu(icon, is_gaia_picture); | |
| 308 } | |
| 309 | |
| 310 Item* item = new Item(i, icon); | |
| 311 item->name = profile_info_->GetNameOfProfileAtIndex(i); | |
| 312 item->sync_state = profile_info_->GetUserNameOfProfileAtIndex(i); | |
| 313 item->signed_in = !item->sync_state.empty(); | |
| 314 if (!item->signed_in) { | |
| 315 item->sync_state = l10n_util::GetStringUTF16( | |
| 316 profile_info_->ProfileIsManagedAtIndex(i) ? | |
| 317 IDS_MANAGED_USER_AVATAR_LABEL : IDS_PROFILES_LOCAL_PROFILE_STATE); | |
| 318 } | |
| 319 if (browser_) { | |
| 320 base::FilePath path = profile_info_->GetPathOfProfileAtIndex(i); | |
| 321 item->active = browser_->profile()->GetPath() == path; | |
| 322 } | |
| 323 item->signin_required = profile_info_->ProfileIsSigninRequiredAtIndex(i); | |
| 324 items_.push_back(item); | |
| 325 } | |
| 326 } | 291 } |
| 327 | 292 |
| 328 void AvatarMenuModel::ClearMenu() { | 293 void AvatarMenuModel::ClearMenu() { |
| 329 STLDeleteElements(&items_); | 294 STLDeleteElements(&items_); |
| 330 } | 295 } |
| 331 | 296 |
| 332 | 297 |
| 333 content::WebContents* AvatarMenuModel::BeginSignOut() { | 298 content::WebContents* AvatarMenuModel::BeginSignOut() { |
| 334 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 299 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 335 Profile* current_profile = browser_->profile(); | 300 Profile* current_profile = browser_->profile(); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 354 contents->GetController().LoadURL( | 319 contents->GetController().LoadURL( |
| 355 logout_url, content::Referrer(), | 320 logout_url, content::Referrer(), |
| 356 content::PAGE_TRANSITION_GENERATED, std::string()); | 321 content::PAGE_TRANSITION_GENERATED, std::string()); |
| 357 | 322 |
| 358 // This object may be destructed when the menu closes but we need something | 323 // This object may be destructed when the menu closes but we need something |
| 359 // around to finish the sign-out process and close the profile windows. | 324 // around to finish the sign-out process and close the profile windows. |
| 360 new SignoutTracker(current_profile, GURL(landing_url), contents); | 325 new SignoutTracker(current_profile, GURL(landing_url), contents); |
| 361 | 326 |
| 362 return contents; // returned for testing purposes | 327 return contents; // returned for testing purposes |
| 363 } | 328 } |
| OLD | NEW |