Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / profiles / profile_list_desktop.cc
blob2e84242a38d97da81926307da7f9128a5027b291
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/profiles/profile_list_desktop.h"
7 #include "chrome/browser/profiles/profile.h"
8 #include "chrome/browser/profiles/profile_avatar_icon_util.h"
9 #include "chrome/browser/profiles/profile_info_cache.h"
10 #include "chrome/grit/generated_resources.h"
11 #include "components/signin/core/common/profile_management_switches.h"
12 #include "ui/base/l10n/l10n_util.h"
14 ProfileListDesktop::ProfileListDesktop(ProfileInfoInterface* profile_cache)
15 : profile_info_(profile_cache),
16 omitted_item_count_(0) {
19 ProfileListDesktop::~ProfileListDesktop() {
20 ClearMenu();
23 // static
24 ProfileList* ProfileList::Create(ProfileInfoInterface* profile_cache) {
25 return new ProfileListDesktop(profile_cache);
28 size_t ProfileListDesktop::GetNumberOfItems() const {
29 return items_.size();
32 const AvatarMenu::Item& ProfileListDesktop::GetItemAt(size_t index) const {
33 DCHECK_LT(index, items_.size());
34 return *items_[index];
37 void ProfileListDesktop::RebuildMenu() {
38 ClearMenu();
40 const size_t count = profile_info_->GetNumberOfProfiles();
41 for (size_t i = 0; i < count; ++i) {
42 if (profile_info_->IsOmittedProfileAtIndex(i)) {
43 omitted_item_count_++;
44 continue;
47 gfx::Image icon = profile_info_->GetAvatarIconOfProfileAtIndex(i);
48 AvatarMenu::Item* item = new AvatarMenu::Item(i - omitted_item_count_,
50 icon);
51 item->name = profile_info_->GetNameOfProfileAtIndex(i);
52 item->username = profile_info_->GetUserNameOfProfileAtIndex(i);
53 item->profile_path = profile_info_->GetPathOfProfileAtIndex(i);
54 item->legacy_supervised =
55 profile_info_->ProfileIsLegacySupervisedAtIndex(i);
56 item->child_account = profile_info_->ProfileIsChildAtIndex(i);
57 item->signed_in = profile_info_->ProfileIsAuthenticatedAtIndex(i);
58 if (!item->signed_in) {
59 item->username = l10n_util::GetStringUTF16(
60 item->legacy_supervised ? IDS_LEGACY_SUPERVISED_USER_AVATAR_LABEL :
61 IDS_PROFILES_LOCAL_PROFILE_STATE);
63 item->active = profile_info_->GetPathOfProfileAtIndex(i) ==
64 active_profile_path_;
65 item->signin_required = profile_info_->ProfileIsSigninRequiredAtIndex(i);
66 items_.push_back(item);
68 // One omitted item is expected when a supervised-user profile is in the
69 // process of being registered, but there shouldn't be more than one.
70 VLOG_IF(2, (omitted_item_count_ > 1)) << omitted_item_count_
71 << " profiles omitted fom list.";
74 size_t ProfileListDesktop::MenuIndexFromProfileIndex(size_t index) {
75 const size_t menu_count = GetNumberOfItems();
76 DCHECK_LT(index, menu_count + omitted_item_count_);
78 // In the common case, valid profile-cache indices correspond to indices in
79 // the menu.
80 if (!omitted_item_count_)
81 return index;
83 for (size_t i = 0; i < menu_count; ++i) {
84 const AvatarMenu::Item item = GetItemAt(i);
85 if (item.profile_index == index)
86 return i;
89 // The desired index was not found; return a fallback value.
90 NOTREACHED();
91 return 0;
94 void ProfileListDesktop::ActiveProfilePathChanged(base::FilePath& path) {
95 active_profile_path_ = path;
98 void ProfileListDesktop::ClearMenu() {
99 STLDeleteElements(&items_);
100 omitted_item_count_ = 0;