Commit 85330a74 authored by Md. Hasanur Rashid's avatar Md. Hasanur Rashid Committed by Commit Bot

[IdentityManager] Return one account from GetAccountForDicePromos()

This change enforces returning of one AccountInfo based on unconsented
primary account from signin_ui_util::GetAccountForDicePromos().

Bug: 1006167
Change-Id: I46dbd8e307210f80b1fe3e9e8539ff026f137334
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1991043
Commit-Queue: Md. Hasanur Rashid <hasanur.r@samsung.com>
Reviewed-by: default avatarDavid Roger <droger@chromium.org>
Cr-Commit-Position: refs/heads/master@{#732359}
parent c7cee8b2
......@@ -251,46 +251,27 @@ void EnableSyncFromPromo(
}
} // namespace internal
std::vector<AccountInfo> GetAccountsForDicePromos(Profile* profile) {
// Fetch account ids for accounts that have a token.
AccountInfo GetAccountForDicePromos(Profile* profile) {
signin::IdentityManager* identity_manager =
IdentityManagerFactory::GetForProfile(profile);
std::vector<AccountInfo> accounts_with_tokens =
identity_manager->GetExtendedAccountInfoForAccountsWithRefreshToken();
// Compute the default account.
CoreAccountId default_account_id;
if (identity_manager->HasPrimaryAccount()) {
default_account_id = identity_manager->GetPrimaryAccountId();
} else {
// Fetch accounts in the Gaia cookies.
auto accounts_in_cookie_jar_info =
identity_manager->GetAccountsInCookieJar();
std::vector<gaia::ListedAccount> signed_in_accounts =
accounts_in_cookie_jar_info.signed_in_accounts;
UMA_HISTOGRAM_BOOLEAN("Profile.DiceUI.GaiaAccountsStale",
!accounts_in_cookie_jar_info.accounts_are_fresh);
if (accounts_in_cookie_jar_info.accounts_are_fresh &&
!signed_in_accounts.empty())
default_account_id = signed_in_accounts[0].id;
}
CoreAccountId default_account_id =
identity_manager->GetUnconsentedPrimaryAccountId();
AccountInfo default_account_info =
identity_manager
->FindExtendedAccountInfoForAccountWithRefreshTokenByAccountId(
default_account_id)
.value_or(AccountInfo());
// Fetch account information for each id and make sure that the first account
// in the list matches the first account in the Gaia cookies (if available).
std::vector<AccountInfo> accounts;
for (auto& account_info : accounts_with_tokens) {
DCHECK(!account_info.IsEmpty());
if (!signin::IsUsernameAllowedByPatternFromPrefs(
g_browser_process->local_state(), account_info.email)) {
continue;
}
if (account_info.account_id == default_account_id)
accounts.insert(accounts.begin(), std::move(account_info));
else
accounts.push_back(std::move(account_info));
if (default_account_info.IsEmpty()) {
return default_account_info;
}
if (signin::IsUsernameAllowedByPatternFromPrefs(
g_browser_process->local_state(), default_account_info.email)) {
return default_account_info;
}
return accounts;
return AccountInfo();
}
#endif // BUILDFLAG(ENABLE_DICE_SUPPORT)
......
......@@ -53,9 +53,8 @@ void EnableSyncFromPromo(Browser* browser,
bool is_default_promo_account);
#if BUILDFLAG(ENABLE_DICE_SUPPORT)
// Returns the list of all accounts that have a token. The default account in
// the Gaia cookies will be the first account in the list.
std::vector<AccountInfo> GetAccountsForDicePromos(Profile* profile);
// Returns the account to use in Dice promos.
AccountInfo GetAccountForDicePromos(Profile* profile);
#endif
......
......@@ -473,14 +473,14 @@ TEST_F(DiceSigninUiUtilTest, MAYBE_EnableSyncForNewAccountWithOneTab) {
// TODO(https://crbug.com/1014790): Timeout on Mac10.12 and Win7 x64.
#if defined(OS_MACOSX) || defined(OS_WIN)
#define MAYBE_GetAccountsForDicePromos DISABLED_GetAccountsForDicePromos
#define MAYBE_GetAccountForDicePromos DISABLED_GetAccountForDicePromos
#else
#define MAYBE_GetAccountsForDicePromos GetAccountsForDicePromos
#define MAYBE_GetAccountForDicePromos GetAccountForDicePromos
#endif
TEST_F(DiceSigninUiUtilTest, MAYBE_GetAccountsForDicePromos) {
// Should start off with no accounts.
std::vector<AccountInfo> accounts = GetAccountsForDicePromos(profile());
EXPECT_TRUE(accounts.empty());
TEST_F(DiceSigninUiUtilTest, MAYBE_GetAccountForDicePromos) {
// Should start off with no account.
AccountInfo info_empty = GetAccountForDicePromos(profile());
EXPECT_TRUE(info_empty.IsEmpty());
// TODO(tangltom): Flesh out this test.
}
......
......@@ -32,8 +32,8 @@ class Browser;
class ProfileMenuView : public ProfileMenuViewBase {
public:
ProfileMenuView(views::Button* anchor_button,
Browser* browser,
signin_metrics::AccessPoint access_point);
Browser* browser,
signin_metrics::AccessPoint access_point);
~ProfileMenuView() override;
// ProfileMenuViewBase:
......
......@@ -27,12 +27,12 @@ DiceBubbleSyncPromoView::DiceBubbleSyncPromoView(
int accounts_promo_message_resource_id,
bool signin_button_prominent,
int text_style)
: views::View(), delegate_(delegate) {
: delegate_(delegate) {
DCHECK(!profile->IsGuestSession());
std::vector<AccountInfo> accounts;
AccountInfo account;
// Signin promos can be shown in incognito, they use an empty account list.
if (profile->IsRegularProfile())
accounts = signin_ui_util::GetAccountsForDicePromos(profile);
account = signin_ui_util::GetAccountForDicePromos(profile);
// Always show the accounts promo message for now.
const int title_resource_id = accounts_promo_message_resource_id;
......@@ -53,25 +53,22 @@ DiceBubbleSyncPromoView::DiceBubbleSyncPromoView(
AddChildView(title);
}
if (accounts.empty()) {
if (account.IsEmpty()) {
signin_button_view_ =
new DiceSigninButtonView(this, signin_button_prominent);
} else {
gfx::Image account_icon = accounts[0].account_image;
gfx::Image account_icon = account.account_image;
if (account_icon.IsEmpty()) {
account_icon = ui::ResourceBundle::GetSharedInstance().GetImageNamed(
profiles::GetPlaceholderAvatarIconResourceID());
}
signin_button_view_ =
new DiceSigninButtonView(accounts[0], account_icon, this,
new DiceSigninButtonView(account, account_icon, this,
/*use_account_name_as_title=*/true);
// Store account information for submenu.
accounts_for_submenu_.assign(accounts.begin() + 1, accounts.end());
}
signin_metrics::RecordSigninImpressionUserActionForAccessPoint(access_point);
signin_metrics::RecordSigninImpressionWithAccountUserActionForAccessPoint(
access_point, !accounts.empty() /* with_account */);
access_point, !account.IsEmpty() /* with_account */);
AddChildView(signin_button_view_);
}
......
......@@ -65,8 +65,6 @@ class DiceBubbleSyncPromoView : public views::View,
BubbleSyncPromoDelegate* delegate_;
DiceSigninButtonView* signin_button_view_ = nullptr;
std::vector<AccountInfo> accounts_for_submenu_;
DISALLOW_COPY_AND_ASSIGN(DiceBubbleSyncPromoView);
};
#endif // CHROME_BROWSER_UI_VIEWS_SYNC_DICE_BUBBLE_SYNC_PROMO_VIEW_H_
......@@ -533,9 +533,9 @@ base::Value PeopleHandler::GetStoredAccountsList() {
base::Value accounts(base::Value::Type::LIST);
#if BUILDFLAG(ENABLE_DICE_SUPPORT)
if (AccountConsistencyModeManager::IsDiceEnabledForProfile(profile_)) {
// If dice is enabled, show all the accounts.
for (auto const& account :
signin_ui_util::GetAccountsForDicePromos(profile_)) {
// If dice is enabled, show the account.
AccountInfo account = signin_ui_util::GetAccountForDicePromos(profile_);
if (!account.IsEmpty()) {
accounts.Append(GetAccountValue(account));
}
return accounts;
......
......@@ -1218,11 +1218,9 @@ TEST(PeopleHandlerDiceUnifiedConsentTest, StoredAccountsList) {
ASSERT_TRUE(accounts.is_list());
base::Value::ConstListView accounts_list = accounts.GetList();
ASSERT_EQ(2u, accounts_list.size());
ASSERT_EQ(1u, accounts_list.size());
ASSERT_TRUE(accounts_list[0].FindKey("email"));
ASSERT_TRUE(accounts_list[1].FindKey("email"));
EXPECT_EQ("a@gmail.com", accounts_list[0].FindKey("email")->GetString());
EXPECT_EQ("b@gmail.com", accounts_list[1].FindKey("email")->GetString());
}
#endif // BUILDFLAG(ENABLE_DICE_SUPPORT)
......
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