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.
4
5#include "chrome/browser/chromeos/profiles/profile_list_chromeos.h"
6
7#include <algorithm>
8
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"
17
18// static
19ProfileList* ProfileList::Create(ProfileInfoInterface* profile_cache) {
20  return new chromeos::ProfileListChromeOS(profile_cache);
21}
22
23namespace chromeos {
24
25ProfileListChromeOS::ProfileListChromeOS(ProfileInfoInterface* profile_cache)
26    : profile_info_(profile_cache) {
27}
28
29ProfileListChromeOS::~ProfileListChromeOS() {
30  ClearMenu();
31}
32
33size_t ProfileListChromeOS::GetNumberOfItems() const {
34  return items_.size();
35}
36
37const AvatarMenu::Item& ProfileListChromeOS::GetItemAt(size_t index) const {
38  DCHECK_LT(index, items_.size());
39  return *items_[index];
40}
41
42void ProfileListChromeOS::RebuildMenu() {
43  ClearMenu();
44
45  // Filter for profiles associated with logged-in users.
46  user_manager::UserList users =
47      user_manager::UserManager::Get()->GetLoggedInUsers();
48
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()));
55
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);
60    }
61
62    AvatarMenu::Item* item = new AvatarMenu::Item(i, i, icon);
63    item->name = (*it)->GetDisplayName();
64    item->sync_state = profile_info_->GetUserNameOfProfileAtIndex(i);
65    item->profile_path = profile_info_->GetPathOfProfileAtIndex(i);
66    item->supervised = false;
67    item->signed_in = true;
68    item->active = profile_info_->GetPathOfProfileAtIndex(i) ==
69        active_profile_path_;
70    item->signin_required = profile_info_->ProfileIsSigninRequiredAtIndex(i);
71    items_.push_back(item);
72  }
73
74  SortMenu();
75
76  // After sorting, assign items their actual indices.
77  for (size_t i = 0; i < items_.size(); ++i)
78    items_[i]->menu_index = i;
79}
80
81size_t ProfileListChromeOS::MenuIndexFromProfileIndex(size_t index) {
82  // On ChromeOS, the active profile might be Default, which does not show
83  // up in the model as a logged-in user. In that case, we return 0.
84  size_t menu_index = 0;
85
86  for (size_t i = 0; i < GetNumberOfItems(); ++i) {
87    if (items_[i]->profile_index == index) {
88      menu_index = i;
89      break;
90    }
91  }
92
93  return menu_index;
94}
95
96void ProfileListChromeOS::ActiveProfilePathChanged(base::FilePath& path) {
97  active_profile_path_ = path;
98}
99
100void ProfileListChromeOS::ClearMenu() {
101  STLDeleteElements(&items_);
102}
103
104void ProfileListChromeOS::SortMenu() {
105  // Sort list of items by name.
106  std::sort(items_.begin(), items_.end(), &AvatarMenu::CompareItems);
107}
108
109}  // namespace chromeos
110