OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/chromeos/profiles/profile_list_chromeos.h" | 5 #include "chrome/browser/chromeos/profiles/profile_list_chromeos.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "ash/shell.h" | 9 #include "ash/shell.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
11 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 11 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
12 #include "chrome/browser/profiles/profile_attributes_entry.h" | |
13 #include "chrome/browser/profiles/profile_attributes_storage.h" | |
12 #include "chrome/browser/profiles/profile_avatar_icon_util.h" | 14 #include "chrome/browser/profiles/profile_avatar_icon_util.h" |
13 #include "chrome/browser/profiles/profile_manager.h" | 15 #include "chrome/browser/profiles/profile_manager.h" |
14 #include "chrome/common/chrome_switches.h" | 16 #include "chrome/common/chrome_switches.h" |
15 #include "components/signin/core/common/profile_management_switches.h" | 17 #include "components/signin/core/common/profile_management_switches.h" |
16 #include "components/user_manager/user_manager.h" | 18 #include "components/user_manager/user_manager.h" |
17 | 19 |
18 // static | 20 // static |
19 ProfileList* ProfileList::Create(ProfileInfoInterface* profile_cache) { | 21 ProfileList* ProfileList::Create(ProfileAttributesStorage* profile_storage) { |
20 return new chromeos::ProfileListChromeOS(profile_cache); | 22 return new chromeos::ProfileListChromeOS(profile_storage); |
21 } | 23 } |
22 | 24 |
23 namespace chromeos { | 25 namespace chromeos { |
24 | 26 |
25 ProfileListChromeOS::ProfileListChromeOS(ProfileInfoInterface* profile_cache) | 27 ProfileListChromeOS::ProfileListChromeOS( |
26 : profile_info_(profile_cache) { | 28 ProfileAttributesStorage* profile_storage) |
29 : profile_storage_(profile_storage) { | |
27 } | 30 } |
28 | 31 |
29 ProfileListChromeOS::~ProfileListChromeOS() { | 32 ProfileListChromeOS::~ProfileListChromeOS() { |
30 ClearMenu(); | 33 ClearMenu(); |
31 } | 34 } |
32 | 35 |
33 size_t ProfileListChromeOS::GetNumberOfItems() const { | 36 size_t ProfileListChromeOS::GetNumberOfItems() const { |
34 return items_.size(); | 37 return items_.size(); |
35 } | 38 } |
36 | 39 |
37 const AvatarMenu::Item& ProfileListChromeOS::GetItemAt(size_t index) const { | 40 const AvatarMenu::Item& ProfileListChromeOS::GetItemAt(size_t index) const { |
38 DCHECK_LT(index, items_.size()); | 41 DCHECK_LT(index, items_.size()); |
39 return *items_[index]; | 42 return *items_[index]; |
40 } | 43 } |
41 | 44 |
42 void ProfileListChromeOS::RebuildMenu() { | 45 void ProfileListChromeOS::RebuildMenu() { |
43 ClearMenu(); | 46 ClearMenu(); |
44 | 47 |
45 // Filter for profiles associated with logged-in users. | 48 // Filter for profiles associated with logged-in users. |
46 user_manager::UserList users = | 49 user_manager::UserList users = |
47 user_manager::UserManager::Get()->GetLoggedInUsers(); | 50 user_manager::UserManager::Get()->GetLoggedInUsers(); |
48 | 51 |
52 std::vector<ProfileAttributesEntry*> entries = | |
53 profile_storage_->GetAllProfilesAttributes(); | |
54 // Prepare a sorted vector of the profile attributes entries that will be used | |
55 // to determine an item's index in the AvatarMenu. | |
56 std::sort(entries.begin(), entries.end(), | |
57 [](ProfileAttributesEntry* first, ProfileAttributesEntry* second) { | |
58 return first->LessThan(*second); | |
59 }); | |
49 // Add corresponding profiles. | 60 // Add corresponding profiles. |
50 for (user_manager::UserList::const_iterator it = users.begin(); | 61 for (user_manager::UserList::const_iterator it = users.begin(); |
51 it != users.end(); | 62 it != users.end(); |
52 ++it) { | 63 ++it) { |
53 size_t i = profile_info_->GetIndexOfProfileWithPath( | 64 const base::FilePath& path = |
54 ProfileHelper::GetProfilePathByUserIdHash((*it)->username_hash())); | 65 ProfileHelper::GetProfilePathByUserIdHash((*it)->username_hash()); |
66 ProfileAttributesEntry* entry; | |
67 if (!profile_storage_->GetProfileAttributesWithPath(path, &entry)) { | |
68 continue; | |
69 } | |
55 | 70 |
56 gfx::Image icon = gfx::Image((*it)->GetImage()); | 71 gfx::Image icon = gfx::Image((*it)->GetImage()); |
57 if (!switches::IsNewProfileManagement() && !icon.IsEmpty()) { | 72 if (!switches::IsNewProfileManagement() && !icon.IsEmpty()) { |
58 // old avatar menu uses resized-small images | 73 // old avatar menu uses resized-small images |
59 icon = profiles::GetAvatarIconForMenu(icon, true); | 74 icon = profiles::GetAvatarIconForMenu(icon, true); |
60 } | 75 } |
61 | 76 |
62 AvatarMenu::Item* item = new AvatarMenu::Item(i, i, icon); | 77 size_t sorted_index = |
Mike Lerman
2015/08/06 16:06:19
Why not just keep an iterator i that you increment
| |
78 std::find(entries.begin(), entries.end(), entry) - entries.begin(); | |
79 DCHECK(sorted_index < entries.size()); | |
80 AvatarMenu::Item* item = new AvatarMenu::Item(sorted_index, icon); | |
63 item->name = (*it)->GetDisplayName(); | 81 item->name = (*it)->GetDisplayName(); |
64 item->username = profile_info_->GetUserNameOfProfileAtIndex(i); | 82 item->username = entry->GetUserName(); |
65 item->profile_path = profile_info_->GetPathOfProfileAtIndex(i); | 83 item->profile_path = entry->GetPath(); |
66 DCHECK(!profile_info_->ProfileIsLegacySupervisedAtIndex(i)); | 84 DCHECK(!entry->IsLegacySupervised()); |
67 item->legacy_supervised = false; | 85 item->legacy_supervised = false; |
68 item->child_account = profile_info_->ProfileIsChildAtIndex(i); | 86 item->child_account = entry->IsChild(); |
69 item->signed_in = true; | 87 item->signed_in = true; |
70 item->active = profile_info_->GetPathOfProfileAtIndex(i) == | 88 item->active = entry->GetPath() == active_profile_path_; |
71 active_profile_path_; | 89 item->signin_required = entry->IsSigninRequired(); |
72 item->signin_required = profile_info_->ProfileIsSigninRequiredAtIndex(i); | |
73 items_.push_back(item); | 90 items_.push_back(item); |
74 } | 91 } |
75 | 92 |
76 SortMenu(); | 93 SortMenu(); |
77 | 94 |
78 // After sorting, assign items their actual indices. | 95 // After sorting, assign items their actual indices. |
79 for (size_t i = 0; i < items_.size(); ++i) | 96 for (size_t i = 0; i < items_.size(); ++i) |
80 items_[i]->menu_index = i; | 97 items_[i]->menu_index = i; |
81 } | 98 } |
82 | 99 |
83 size_t ProfileListChromeOS::MenuIndexFromProfileIndex(size_t index) { | 100 size_t ProfileListChromeOS::MenuIndexFromProfilePath( |
101 const base::FilePath& path) { | |
84 // On ChromeOS, the active profile might be Default, which does not show | 102 // 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. | 103 // up in the model as a logged-in user. In that case, we return 0. |
86 size_t menu_index = 0; | 104 size_t menu_index = 0; |
87 | 105 |
88 for (size_t i = 0; i < GetNumberOfItems(); ++i) { | 106 for (size_t i = 0; i < GetNumberOfItems(); ++i) { |
89 if (items_[i]->profile_index == index) { | 107 if (items_[i]->profile_path == path) { |
90 menu_index = i; | 108 menu_index = i; |
91 break; | 109 break; |
92 } | 110 } |
93 } | 111 } |
94 | 112 |
95 return menu_index; | 113 return menu_index; |
96 } | 114 } |
97 | 115 |
98 void ProfileListChromeOS::ActiveProfilePathChanged(base::FilePath& path) { | 116 void ProfileListChromeOS::ActiveProfilePathChanged(base::FilePath& path) { |
99 active_profile_path_ = path; | 117 active_profile_path_ = path; |
100 } | 118 } |
101 | 119 |
102 void ProfileListChromeOS::ClearMenu() { | 120 void ProfileListChromeOS::ClearMenu() { |
103 STLDeleteElements(&items_); | 121 STLDeleteElements(&items_); |
104 } | 122 } |
105 | 123 |
106 void ProfileListChromeOS::SortMenu() { | 124 void ProfileListChromeOS::SortMenu() { |
107 // Sort list of items by name. | 125 // Sort list of items by name. |
108 std::sort(items_.begin(), items_.end(), &AvatarMenu::CompareItems); | 126 std::sort(items_.begin(), items_.end(), &AvatarMenu::CompareItems); |
109 } | 127 } |
110 | 128 |
111 } // namespace chromeos | 129 } // namespace chromeos |
OLD | NEW |