Commit 7141db77 authored by Thomas Tangl's avatar Thomas Tangl Committed by Commit Bot

Show promo account icon in toolbar when user is signed out

Relanding crrev.com/c/1105772 after crbug.com/855797.
Broken tests caused by calls to GetAccountsForDicePromos
were fixed.

When the user is signed out of Chrome and the profile icon
has not been explicitly changed, AvatarToolbarButton now
uses the account icon of the first sync promo account.

Screenshots:
https://drive.google.com/file/d/1a7kr12KtA11Wt7MQ9MLnSf-M3D9nlLdg/view?usp=sharing
https://drive.google.com/file/d/1nKLnoD1sbcZOvwGQY3YtHVTUv32DQNFn/view?usp=sharing

TBR=droger@chromium.org
TBR=tapted@chromium.org
TBR=bsep@chromium.org

Bug: 853363, 855797
Change-Id: I830aedd32c30cb1c39fc24551dd225c81d62c097
Reviewed-on: https://chromium-review.googlesource.com/1112253
Commit-Queue: Thomas Tangl <tangltom@chromium.org>
Reviewed-by: default avatarTrent Apted <tapted@chromium.org>
Reviewed-by: default avatarThomas Tangl <tangltom@chromium.org>
Reviewed-by: default avatarMihai Sardarescu <msarda@chromium.org>
Cr-Commit-Position: refs/heads/master@{#570699}
parent fa2cc2f7
......@@ -14,6 +14,8 @@
#include "chrome/browser/search_engines/chrome_template_url_service_client.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/search_engines/ui_thread_search_terms_data.h"
#include "chrome/browser/signin/fake_gaia_cookie_manager_service_builder.h"
#include "chrome/browser/signin/gaia_cookie_manager_service_factory.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/web_data_service_factory.h"
......@@ -24,6 +26,7 @@
#include "components/omnibox/browser/test_scheme_classifier.h"
#include "components/search_engines/search_terms_data.h"
#include "components/search_engines/template_url_service.h"
#include "components/signin/core/browser/fake_gaia_cookie_manager_service.h"
#include "content/public/test/test_utils.h"
#if defined(OS_CHROMEOS)
......@@ -58,12 +61,12 @@ std::unique_ptr<KeyedService> CreateAutocompleteClassifier(
} // namespace
TestWithBrowserView::TestWithBrowserView() {
}
TestWithBrowserView::TestWithBrowserView() : url_fetcher_factory_(nullptr) {}
TestWithBrowserView::TestWithBrowserView(Browser::Type browser_type,
bool hosted_app)
: BrowserWithTestWindowTest(browser_type, hosted_app) {}
: BrowserWithTestWindowTest(browser_type, hosted_app),
url_fetcher_factory_(nullptr) {}
TestWithBrowserView::~TestWithBrowserView() {
}
......@@ -105,6 +108,13 @@ TestingProfile* TestWithBrowserView::CreateProfile() {
// location bar.
AutocompleteClassifierFactory::GetInstance()->SetTestingFactory(
profile, &CreateAutocompleteClassifier);
// Configure the GaiaCookieManagerService to return no accounts.
FakeGaiaCookieManagerService* gcms =
static_cast<FakeGaiaCookieManagerService*>(
GaiaCookieManagerServiceFactory::GetForProfile(profile));
gcms->Init(&url_fetcher_factory_);
gcms->SetListAccountsResponseHttpNotFound();
return profile;
}
......@@ -113,3 +123,8 @@ BrowserWindow* TestWithBrowserView::CreateBrowserWindow() {
// BrowserView and BrowserFrame.
return nullptr;
}
TestingProfile::TestingFactories TestWithBrowserView::GetTestingFactories() {
return {{GaiaCookieManagerServiceFactory::GetInstance(),
&BuildFakeGaiaCookieManagerService}};
}
......@@ -10,6 +10,7 @@
#include "base/macros.h"
#include "chrome/test/base/browser_with_test_window_test.h"
#include "chrome/test/views/scoped_macviews_browser_mode.h"
#include "net/url_request/test_url_fetcher_factory.h"
class BrowserView;
......@@ -27,6 +28,7 @@ class TestWithBrowserView : public BrowserWithTestWindowTest {
void TearDown() override;
TestingProfile* CreateProfile() override;
BrowserWindow* CreateBrowserWindow() override;
TestingProfile::TestingFactories GetTestingFactories() override;
BrowserView* browser_view() { return browser_view_; }
......@@ -34,6 +36,8 @@ class TestWithBrowserView : public BrowserWithTestWindowTest {
BrowserView* browser_view_; // Not owned.
test::ScopedMacViewsBrowserMode views_mode_{true};
net::FakeURLFetcherFactory url_fetcher_factory_;
DISALLOW_COPY_AND_ASSIGN(TestWithBrowserView);
};
......
......@@ -14,7 +14,10 @@
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profiles_state.h"
#include "chrome/browser/signin/account_consistency_mode_manager.h"
#include "chrome/browser/signin/account_tracker_service_factory.h"
#include "chrome/browser/signin/gaia_cookie_manager_service_factory.h"
#include "chrome/browser/signin/signin_manager_factory.h"
#include "chrome/browser/signin/signin_ui_util.h"
#include "chrome/browser/sync/sync_ui_util.h"
#include "chrome/browser/themes/theme_properties.h"
#include "chrome/browser/ui/layout_constants.h"
......@@ -37,10 +40,19 @@ AvatarToolbarButton::AvatarToolbarButton(Profile* profile,
#if !defined(OS_CHROMEOS)
error_controller_(this, profile_),
#endif // !defined(OS_CHROMEOS)
profile_observer_(this) {
profile_observer_(this),
cookie_manager_service_observer_(this),
account_tracker_service_observer_(this) {
profile_observer_.Add(
&g_browser_process->profile_manager()->GetProfileAttributesStorage());
if (!IsIncognito() && !profile_->IsGuestSession()) {
cookie_manager_service_observer_.Add(
GaiaCookieManagerServiceFactory::GetForProfile(profile_));
account_tracker_service_observer_.Add(
AccountTrackerServiceFactory::GetForProfile(profile_));
}
SetImageAlignment(HorizontalAlignment::ALIGN_CENTER,
VerticalAlignment::ALIGN_MIDDLE);
......@@ -122,6 +134,18 @@ void AvatarToolbarButton::OnProfileNameChanged(
UpdateTooltipText();
}
void AvatarToolbarButton::OnGaiaAccountsInCookieUpdated(
const std::vector<gaia::ListedAccount>& accounts,
const std::vector<gaia::ListedAccount>& signed_out_accounts,
const GoogleServiceAuthError& error) {
UpdateIcon();
}
void AvatarToolbarButton::OnAccountImageUpdated(const std::string& account_id,
const gfx::Image& image) {
UpdateIcon();
}
bool AvatarToolbarButton::IsIncognito() const {
return profile_->IsOffTheRecord() && !profile_->IsGuestSession();
}
......@@ -131,6 +155,10 @@ bool AvatarToolbarButton::ShouldShowGenericIcon() const {
// sessions should be handled separately and never call this function.
DCHECK(!profile_->IsGuestSession());
DCHECK(!profile_->IsOffTheRecord());
#if !defined(OS_CHROMEOS)
if (!signin_ui_util::GetAccountsForDicePromos(profile_).empty())
return false;
#endif // !defined(OS_CHROMEOS)
return g_browser_process->profile_manager()
->GetProfileAttributesStorage()
.GetNumberOfProfiles() == 1 &&
......@@ -223,6 +251,20 @@ gfx::Image AvatarToolbarButton::GetIconImageFromProfile() const {
return gfx::Image();
}
#if !defined(OS_CHROMEOS)
// If the user isn't signed in and the profile icon wasn't changed explicitly,
// try to use the first account icon of the sync promo.
if (!SigninManagerFactory::GetForProfile(profile_)->IsAuthenticated() &&
entry->GetAvatarIconIndex() == 0) {
std::vector<AccountInfo> promo_accounts =
signin_ui_util::GetAccountsForDicePromos(profile_);
if (!promo_accounts.empty()) {
return AccountTrackerServiceFactory::GetForProfile(profile_)
->GetAccountImage(promo_accounts[0].account_id);
}
}
#endif // !defined(OS_CHROMEOS)
return entry->GetAvatarIcon();
}
......
......@@ -12,10 +12,14 @@
#include "chrome/browser/ui/avatar_button_error_controller.h"
#include "chrome/browser/ui/avatar_button_error_controller_delegate.h"
#include "chrome/browser/ui/views/toolbar/toolbar_button.h"
#include "components/signin/core/browser/account_tracker_service.h"
#include "components/signin/core/browser/gaia_cookie_manager_service.h"
class AvatarToolbarButton : public ToolbarButton,
public AvatarButtonErrorControllerDelegate,
public ProfileAttributesStorage::Observer {
public ProfileAttributesStorage::Observer,
public GaiaCookieManagerService::Observer,
public AccountTrackerService::Observer {
public:
AvatarToolbarButton(Profile* profile, views::ButtonListener* listener);
~AvatarToolbarButton() override;
......@@ -39,6 +43,18 @@ class AvatarToolbarButton : public ToolbarButton,
void OnProfileNameChanged(const base::FilePath& profile_path,
const base::string16& old_profile_name) override;
// GaiaCookieManagerService::Observer:
// Needed if the first sync promo account should be displayed.
void OnGaiaAccountsInCookieUpdated(
const std::vector<gaia::ListedAccount>& accounts,
const std::vector<gaia::ListedAccount>& signed_out_accounts,
const GoogleServiceAuthError& error) override;
// AccountTrackerService::Observer:
// Needed if the first sync promo account should be displayed.
void OnAccountImageUpdated(const std::string& account_id,
const gfx::Image& image) override;
bool IsIncognito() const;
bool ShouldShowGenericIcon() const;
base::string16 GetAvatarTooltipText();
......@@ -53,6 +69,10 @@ class AvatarToolbarButton : public ToolbarButton,
#endif // !defined(OS_CHROMEOS)
ScopedObserver<ProfileAttributesStorage, AvatarToolbarButton>
profile_observer_;
ScopedObserver<GaiaCookieManagerService, AvatarToolbarButton>
cookie_manager_service_observer_;
ScopedObserver<AccountTrackerService, AvatarToolbarButton>
account_tracker_service_observer_;
DISALLOW_COPY_AND_ASSIGN(AvatarToolbarButton);
};
......
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