Commit 1ee6a6d0 authored by noms@chromium.org's avatar noms@chromium.org

Refactor the code that creates the list of secondary accounts

BUG=331805
TEST=No visible change. Existing unit tests.

Review URL: https://codereview.chromium.org/128513002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@243712 0039d316-1c4b-4281-b951-d872f2087c98
parent d29484dc
...@@ -12,6 +12,8 @@ ...@@ -12,6 +12,8 @@
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_info_cache.h" #include "chrome/browser/profiles/profile_info_cache.h"
#include "chrome/browser/profiles/profile_manager.h" #include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/signin/profile_oauth2_token_service.h"
#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
#include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser.h"
#include "chrome/common/chrome_constants.h" #include "chrome/common/chrome_constants.h"
#include "chrome/common/pref_names.h" #include "chrome/common/pref_names.h"
...@@ -101,4 +103,21 @@ void UpdateProfileName(Profile* profile, ...@@ -101,4 +103,21 @@ void UpdateProfileName(Profile* profile,
} }
} }
std::vector<std::string> GetSecondaryAccountsForProfile(
Profile* profile,
const std::string& primary_account) {
std::vector<std::string> accounts =
ProfileOAuth2TokenServiceFactory::GetForProfile(profile)->GetAccounts();
// The vector returned by ProfileOAuth2TokenService::GetAccounts() contains
// the primary account too, so we need to remove it from the list.
std::vector<std::string>::iterator primary_index =
std::find_if(accounts.begin(), accounts.end(),
std::bind1st(std::equal_to<std::string>(), primary_account));
DCHECK(primary_index != accounts.end());
accounts.erase(primary_index);
return accounts;
}
} // namespace profiles } // namespace profiles
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#ifndef CHROME_BROWSER_PROFILES_PROFILES_STATE_H_ #ifndef CHROME_BROWSER_PROFILES_PROFILES_STATE_H_
#define CHROME_BROWSER_PROFILES_PROFILES_STATE_H_ #define CHROME_BROWSER_PROFILES_PROFILES_STATE_H_
#include <vector>
#include "base/strings/string16.h" #include "base/strings/string16.h"
class Browser; class Browser;
...@@ -35,6 +36,13 @@ base::string16 GetActiveProfileDisplayName(Browser* browser); ...@@ -35,6 +36,13 @@ base::string16 GetActiveProfileDisplayName(Browser* browser);
void UpdateProfileName(Profile* profile, void UpdateProfileName(Profile* profile,
const base::string16& new_profile_name); const base::string16& new_profile_name);
// Returns the list of secondary accounts for a specific |profile|, which is
// all the email addresses associated with the profile that are not equal to
// the |primary_account|.
std::vector<std::string> GetSecondaryAccountsForProfile(
Profile* profile,
const std::string& primary_account);
} // namespace profiles } // namespace profiles
#endif // CHROME_BROWSER_PROFILES_PROFILES_STATE_H_ #endif // CHROME_BROWSER_PROFILES_PROFILES_STATE_H_
...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
#import "chrome/browser/ui/cocoa/browser/profile_chooser_controller.h" #import "chrome/browser/ui/cocoa/browser/profile_chooser_controller.h"
#include "base/mac/bundle_locations.h" #include "base/mac/bundle_locations.h"
#include "base/stl_util.h"
#include "base/strings/sys_string_conversions.h" #include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "chrome/browser/browser_process.h" #include "chrome/browser/browser_process.h"
...@@ -17,6 +16,7 @@ ...@@ -17,6 +16,7 @@
#include "chrome/browser/profiles/profile_info_util.h" #include "chrome/browser/profiles/profile_info_util.h"
#include "chrome/browser/profiles/profile_manager.h" #include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profile_window.h" #include "chrome/browser/profiles/profile_window.h"
#include "chrome/browser/profiles/profiles_state.h"
#include "chrome/browser/signin/profile_oauth2_token_service.h" #include "chrome/browser/signin/profile_oauth2_token_service.h"
#include "chrome/browser/signin/profile_oauth2_token_service_factory.h" #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
#include "chrome/browser/signin/signin_manager.h" #include "chrome/browser/signin/signin_manager.h"
...@@ -579,28 +579,17 @@ class ActiveProfileObserverBridge : public AvatarMenuObserver, ...@@ -579,28 +579,17 @@ class ActiveProfileObserverBridge : public AvatarMenuObserver,
base::scoped_nsobject<NSView> container([[NSView alloc] initWithFrame:rect]); base::scoped_nsobject<NSView> container([[NSView alloc] initWithFrame:rect]);
currentProfileAccounts_.clear(); currentProfileAccounts_.clear();
// The primary account should always be listed first. However, the vector
// returned by ProfileOAuth2TokenService::GetAccounts() will contain the
// primary account too. Ignore it when it appears later.
// TODO(rogerta): we still need to further differentiate the primary account
// from the others, so more work is likely required here: crbug.com/311124.
Profile* profile = browser_->profile(); Profile* profile = browser_->profile();
// TODO(noms): This code is duplicated by the views implementation. See
// crbug.com/331805.
std::string primaryAccount = std::string primaryAccount =
SigninManagerFactory::GetForProfile(profile)->GetAuthenticatedUsername(); SigninManagerFactory::GetForProfile(profile)->GetAuthenticatedUsername();
DCHECK(!primaryAccount.empty()); DCHECK(!primaryAccount.empty());
std::vector<std::string> accounts = std::vector<std::string>accounts =
ProfileOAuth2TokenServiceFactory::GetForProfile(profile)->GetAccounts(); profiles::GetSecondaryAccountsForProfile(profile, primaryAccount);
DCHECK_EQ(1, std::count_if(accounts.begin(), accounts.end(),
std::bind1st(std::equal_to<std::string>(),
primaryAccount)));
rect.origin.y = 0; rect.origin.y = 0;
for (size_t i = 0; i < accounts.size(); ++i) { for (size_t i = 0; i < accounts.size(); ++i) {
// Save the original email address, as the button text could be elided. // Save the original email address, as the button text could be elided.
currentProfileAccounts_[i] = accounts[i]; currentProfileAccounts_[i] = accounts[i];
if (primaryAccount != accounts[i]) {
NSButton* accountButton = [self makeAccountButtonWithRect:rect NSButton* accountButton = [self makeAccountButtonWithRect:rect
title:accounts[i] title:accounts[i]
canBeDeleted:true]; canBeDeleted:true];
...@@ -608,10 +597,12 @@ class ActiveProfileObserverBridge : public AvatarMenuObserver, ...@@ -608,10 +597,12 @@ class ActiveProfileObserverBridge : public AvatarMenuObserver,
[container addSubview:accountButton]; [container addSubview:accountButton];
rect.origin.y = NSMaxY([accountButton frame]) + kSmallVerticalSpacing; rect.origin.y = NSMaxY([accountButton frame]) + kSmallVerticalSpacing;
} }
}
// Add the primary account button. It doesn't need a tag, as it cannot be // The primary account should always be listed first. It doesn't need a tag,
// removed. // as it cannot be removed.
// TODO(rogerta): we still need to further differentiate the primary account
// from the others in the UI, so more work is likely required here:
// crbug.com/311124.
NSButton* accountButton = [self makeAccountButtonWithRect:rect NSButton* accountButton = [self makeAccountButtonWithRect:rect
title:primaryAccount title:primaryAccount
canBeDeleted:false]; canBeDeleted:false];
......
...@@ -791,22 +791,16 @@ views::View* ProfileChooserView::CreateCurrentProfileAccountsView( ...@@ -791,22 +791,16 @@ views::View* ProfileChooserView::CreateCurrentProfileAccountsView(
std::string primary_account = std::string primary_account =
SigninManagerFactory::GetForProfile(profile)->GetAuthenticatedUsername(); SigninManagerFactory::GetForProfile(profile)->GetAuthenticatedUsername();
DCHECK(!primary_account.empty()); DCHECK(!primary_account.empty());
std::vector<std::string> accounts( std::vector<std::string>accounts =
ProfileOAuth2TokenServiceFactory::GetForProfile(profile)->GetAccounts()); profiles::GetSecondaryAccountsForProfile(profile, primary_account);
DCHECK_EQ(1, std::count_if(accounts.begin(), accounts.end(),
std::bind1st(std::equal_to<std::string>(), // The primary account should always be listed first.
primary_account)));
// The primary account should always be listed first. However, the vector
// returned by ProfileOAuth2TokenService::GetAccounts() will contain the
// primary account too. Ignore it when it appears later.
// TODO(rogerta): we still need to further differentiate the primary account // TODO(rogerta): we still need to further differentiate the primary account
// from the others, so more work is likely required here: crbug.com/311124. // from the others in the UI, so more work is likely required here:
// crbug.com/311124.
CreateAccountButton(layout, primary_account, true); CreateAccountButton(layout, primary_account, true);
for (size_t i = 0; i < accounts.size(); ++i) { for (size_t i = 0; i < accounts.size(); ++i)
if (primary_account != accounts[i])
CreateAccountButton(layout, accounts[i], false); CreateAccountButton(layout, accounts[i], false);
}
layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
......
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