Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / chromeos / profiles / profile_list_chromeos.cc
blob840d29f084511c2cf753115a80a4ffe31c7b7d88
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/profiles/profile_list_chromeos.h"
7 #include <algorithm>
9 #include "ash/shell.h"
10 #include "base/command_line.h"
11 #include "chrome/browser/chromeos/profiles/profile_helper.h"
12 #include "chrome/browser/profiles/profile_avatar_icon_util.h"
13 #include "chrome/browser/profiles/profile_manager.h"
14 #include "chrome/common/chrome_switches.h"
15 #include "components/signin/core/common/profile_management_switches.h"
16 #include "components/user_manager/user_manager.h"
18 // static
19 ProfileList* ProfileList::Create(ProfileInfoInterface* profile_cache) {
20 return new chromeos::ProfileListChromeOS(profile_cache);
23 namespace chromeos {
25 ProfileListChromeOS::ProfileListChromeOS(ProfileInfoInterface* profile_cache)
26 : profile_info_(profile_cache) {
29 ProfileListChromeOS::~ProfileListChromeOS() {
30 ClearMenu();
33 size_t ProfileListChromeOS::GetNumberOfItems() const {
34 return items_.size();
37 const AvatarMenu::Item& ProfileListChromeOS::GetItemAt(size_t index) const {
38 DCHECK_LT(index, items_.size());
39 return *items_[index];
42 void ProfileListChromeOS::RebuildMenu() {
43 ClearMenu();
45 // Filter for profiles associated with logged-in users.
46 user_manager::UserList users =
47 user_manager::UserManager::Get()->GetLoggedInUsers();
49 // Add corresponding profiles.
50 for (user_manager::UserList::const_iterator it = users.begin();
51 it != users.end();
52 ++it) {
53 size_t i = profile_info_->GetIndexOfProfileWithPath(
54 ProfileHelper::GetProfilePathByUserIdHash((*it)->username_hash()));
56 gfx::Image icon = gfx::Image((*it)->GetImage());
57 if (!switches::IsNewProfileManagement() && !icon.IsEmpty()) {
58 // old avatar menu uses resized-small images
59 icon = profiles::GetAvatarIconForMenu(icon, true);
62 AvatarMenu::Item* item = new AvatarMenu::Item(i, i, icon);
63 item->name = (*it)->GetDisplayName();
64 item->username = profile_info_->GetUserNameOfProfileAtIndex(i);
65 item->profile_path = profile_info_->GetPathOfProfileAtIndex(i);
66 DCHECK(!profile_info_->ProfileIsLegacySupervisedAtIndex(i));
67 item->legacy_supervised = false;
68 item->child_account = profile_info_->ProfileIsChildAtIndex(i);
69 item->signed_in = true;
70 item->active = profile_info_->GetPathOfProfileAtIndex(i) ==
71 active_profile_path_;
72 item->signin_required = profile_info_->ProfileIsSigninRequiredAtIndex(i);
73 items_.push_back(item);
76 SortMenu();
78 // After sorting, assign items their actual indices.
79 for (size_t i = 0; i < items_.size(); ++i)
80 items_[i]->menu_index = i;
83 size_t ProfileListChromeOS::MenuIndexFromProfileIndex(size_t index) {
84 // On ChromeOS, the active profile might be Default, which does not show
85 // up in the model as a logged-in user. In that case, we return 0.
86 size_t menu_index = 0;
88 for (size_t i = 0; i < GetNumberOfItems(); ++i) {
89 if (items_[i]->profile_index == index) {
90 menu_index = i;
91 break;
95 return menu_index;
98 void ProfileListChromeOS::ActiveProfilePathChanged(base::FilePath& path) {
99 active_profile_path_ = path;
102 void ProfileListChromeOS::ClearMenu() {
103 STLDeleteElements(&items_);
106 void ProfileListChromeOS::SortMenu() {
107 // Sort list of items by name.
108 std::sort(items_.begin(), items_.end(), &AvatarMenu::CompareItems);
111 } // namespace chromeos