Commit a776a5ac authored by Monica Basta's avatar Monica Basta Committed by Chromium LUCI CQ

[ProfilePicker]: Freeze the order of profiles on the profile picker.

Initially profiles will be ordered alphabetically on the picker's main
view. The order will be frozen to the initial order, profiles created
will be added at the end of the list, profiles deleted will be moved out
of the list. Any update to the profile local name/gaia name will not
affect the order of profiles.

Bug: 1161528
Change-Id: Ieaa6374a056ed353c35568409bf1cae631431ac2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2601744Reviewed-by: default avatarDavid Roger <droger@chromium.org>
Commit-Queue: Monica Basta <msalama@chromium.org>
Cr-Commit-Position: refs/heads/master@{#840167}
parent 73c681e7
......@@ -593,13 +593,54 @@ void ProfilePickerHandler::PushProfilesList() {
FireWebUIListener("profiles-list-changed", GetProfilesList());
}
base::Value ProfilePickerHandler::GetProfilesList() {
base::ListValue profiles_list;
std::vector<ProfileAttributesEntry*> entries =
void ProfilePickerHandler::SetProfilesOrder(
const std::vector<ProfileAttributesEntry*>& entries) {
profiles_order_.clear();
size_t index = 0;
for (const ProfileAttributesEntry* entry : entries) {
if (entry->IsGuest())
continue;
profiles_order_[entry->GetPath()] = index++;
}
}
std::vector<ProfileAttributesEntry*>
ProfilePickerHandler::GetProfileAttributes() {
size_t number_of_profiles =
g_browser_process->profile_manager()->GetNumberOfProfiles();
std::vector<ProfileAttributesEntry*> ordered_entries =
g_browser_process->profile_manager()
->GetProfileAttributesStorage()
.GetAllProfilesAttributesSortedByLocalProfilName();
if (profiles_order_.size() != number_of_profiles) {
// Should only happen the first time the function is called.
// Profile creation and deletion are handled at
// 'OnProfileAdded', 'OnProfileWasRemoved'.
DCHECK(!profiles_order_.size());
SetProfilesOrder(ordered_entries);
return ordered_entries;
}
// Vector of nullptr entries.
std::vector<ProfileAttributesEntry*> entries(number_of_profiles);
for (ProfileAttributesEntry* entry : ordered_entries) {
if (entry->IsGuest())
continue;
DCHECK(profiles_order_.find(entry->GetPath()) != profiles_order_.end());
size_t index = profiles_order_[entry->GetPath()];
DCHECK_LT(index, number_of_profiles);
DCHECK(!entries[index]);
entries[index] = entry;
}
return entries;
}
base::Value ProfilePickerHandler::GetProfilesList() {
base::ListValue profiles_list;
std::vector<ProfileAttributesEntry*> entries = GetProfileAttributes();
const int avatar_icon_size =
kProfileCardAvatarSize * web_ui()->GetDeviceScaleFactor();
for (const ProfileAttributesEntry* entry : entries) {
......@@ -630,6 +671,8 @@ base::Value ProfilePickerHandler::GetProfilesList() {
}
void ProfilePickerHandler::OnProfileAdded(const base::FilePath& profile_path) {
size_t number_of_profiles = profiles_order_.size();
profiles_order_[profile_path] = number_of_profiles;
PushProfilesList();
}
......@@ -637,6 +680,12 @@ void ProfilePickerHandler::OnProfileWasRemoved(
const base::FilePath& profile_path,
const base::string16& profile_name) {
DCHECK(IsJavascriptAllowed());
size_t index = profiles_order_[profile_path];
profiles_order_.erase(profile_path);
for (auto it : profiles_order_) {
if (it.second > index)
profiles_order_[it.first] = it.second - 1;
}
FireWebUIListener("profile-removed", util::FilePathToValue(profile_path));
}
......
......@@ -5,6 +5,8 @@
#ifndef CHROME_BROWSER_UI_WEBUI_SIGNIN_PROFILE_PICKER_HANDLER_H_
#define CHROME_BROWSER_UI_WEBUI_SIGNIN_PROFILE_PICKER_HANDLER_H_
#include <unordered_map>
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "base/values.h"
......@@ -81,11 +83,23 @@ class ProfilePickerHandler : public content::WebUIMessageHandler,
void DidFirstVisuallyNonEmptyPaint() override;
void OnVisibilityChanged(content::Visibility visibility) override;
// Sets 'profiles_order_' that is used to freeze the order of the profiles on
// the picker when it was first shown.
void SetProfilesOrder(const std::vector<ProfileAttributesEntry*>& entries);
// Returns the list of profiles in the same order as when the picker
// was first shown.
std::vector<ProfileAttributesEntry*> GetProfileAttributes();
// Creation time of the handler, to measure performance on startup. Only set
// when the picker is shown on startup.
base::TimeTicks creation_time_on_startup_;
bool main_view_initialized_ = false;
// The order of the profiles when the picker was first shown. This is used
// to freeze the order of profiles on the picker. Newly added profiles, will
// be added to the end of the list.
std::unordered_map<base::FilePath, size_t> profiles_order_;
base::WeakPtrFactory<ProfilePickerHandler> weak_factory_{this};
DISALLOW_COPY_AND_ASSIGN(ProfilePickerHandler);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment