Commit ed5303f1 authored by David Roger's avatar David Roger Committed by Commit Bot

[signin] Set profile name after signin interception

Bug: 1076880
Change-Id: I9931707d9f15ac3013195592f78a95e7579bca38
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2352812Reviewed-by: default avatarMonica Basta <msalama@chromium.org>
Commit-Queue: David Roger <droger@chromium.org>
Cr-Commit-Position: refs/heads/master@{#797745}
parent 53ce7285
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
#include "base/check.h" #include "base/check.h"
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/no_destructor.h" #include "base/no_destructor.h"
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/browser_process.h" #include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile_attributes_storage.h" #include "chrome/browser/profiles/profile_attributes_storage.h"
#include "chrome/browser/profiles/profile_avatar_icon_util.h" #include "chrome/browser/profiles/profile_avatar_icon_util.h"
...@@ -129,6 +131,7 @@ TokensLoadedCallbackRunner::TokensLoadedCallbackRunner( ...@@ -129,6 +131,7 @@ TokensLoadedCallbackRunner::TokensLoadedCallbackRunner(
DiceSignedInProfileCreator::DiceSignedInProfileCreator( DiceSignedInProfileCreator::DiceSignedInProfileCreator(
Profile* source_profile, Profile* source_profile,
CoreAccountId account_id, CoreAccountId account_id,
const std::string& local_profile_name,
base::OnceCallback<void(Profile*)> callback) base::OnceCallback<void(Profile*)> callback)
: source_profile_(source_profile), : source_profile_(source_profile),
account_id_(account_id), account_id_(account_id),
...@@ -137,9 +140,12 @@ DiceSignedInProfileCreator::DiceSignedInProfileCreator( ...@@ -137,9 +140,12 @@ DiceSignedInProfileCreator::DiceSignedInProfileCreator(
g_browser_process->profile_manager()->GetProfileAttributesStorage(); g_browser_process->profile_manager()->GetProfileAttributesStorage();
size_t icon_index = storage.ChooseAvatarIconIndexForNewProfile(); size_t icon_index = storage.ChooseAvatarIconIndexForNewProfile();
base::string16 name = local_profile_name.empty()
? storage.ChooseNameForNewProfile(icon_index)
: base::UTF8ToUTF16(local_profile_name);
ProfileManager::CreateMultiProfileAsync( ProfileManager::CreateMultiProfileAsync(
storage.ChooseNameForNewProfile(icon_index), name, profiles::GetDefaultAvatarIconUrl(icon_index),
profiles::GetDefaultAvatarIconUrl(icon_index),
base::BindRepeating(&DiceSignedInProfileCreator::OnNewProfileCreated, base::BindRepeating(&DiceSignedInProfileCreator::OnNewProfileCreated,
weak_pointer_factory_.GetWeakPtr())); weak_pointer_factory_.GetWeakPtr()));
} }
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#ifndef CHROME_BROWSER_SIGNIN_DICE_SIGNED_IN_PROFILE_CREATOR_H_ #ifndef CHROME_BROWSER_SIGNIN_DICE_SIGNED_IN_PROFILE_CREATOR_H_
#define CHROME_BROWSER_SIGNIN_DICE_SIGNED_IN_PROFILE_CREATOR_H_ #define CHROME_BROWSER_SIGNIN_DICE_SIGNED_IN_PROFILE_CREATOR_H_
#include <string>
#include "base/callback.h" #include "base/callback.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
...@@ -18,8 +20,11 @@ class DiceSignedInProfileCreator { ...@@ -18,8 +20,11 @@ class DiceSignedInProfileCreator {
// Creates a new profile and moves the account from source_profile to the new // Creates a new profile and moves the account from source_profile to the new
// profile. The callback is called with the new profile or nullptr in case of // profile. The callback is called with the new profile or nullptr in case of
// failure. The callback is never called synchronously. // failure. The callback is never called synchronously.
// If |local_profile_name| is not empty, it will be set as local name for the
// new profile.
DiceSignedInProfileCreator(Profile* source_profile, DiceSignedInProfileCreator(Profile* source_profile,
CoreAccountId account_id, CoreAccountId account_id,
const std::string& local_profile_name,
base::OnceCallback<void(Profile*)> callback); base::OnceCallback<void(Profile*)> callback);
~DiceSignedInProfileCreator(); ~DiceSignedInProfileCreator();
...@@ -36,8 +41,9 @@ class DiceSignedInProfileCreator { ...@@ -36,8 +41,9 @@ class DiceSignedInProfileCreator {
// Callback invoked once the token service is ready for the new profile. // Callback invoked once the token service is ready for the new profile.
void OnNewProfileTokensLoaded(Profile* new_profile); void OnNewProfileTokensLoaded(Profile* new_profile);
Profile* source_profile_; Profile* const source_profile_;
CoreAccountId account_id_; const CoreAccountId account_id_;
base::OnceCallback<void(Profile*)> callback_; base::OnceCallback<void(Profile*)> callback_;
std::unique_ptr<TokensLoadedCallbackRunner> tokens_loaded_callback_runner_; std::unique_ptr<TokensLoadedCallbackRunner> tokens_loaded_callback_runner_;
......
...@@ -10,7 +10,10 @@ ...@@ -10,7 +10,10 @@
#include "base/files/file_util.h" #include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h" #include "base/files/scoped_temp_dir.h"
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "chrome/browser/profiles/profile_attributes_entry.h"
#include "chrome/browser/profiles/profile_attributes_storage.h"
#include "chrome/browser/profiles/profile_manager.h" #include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profile_manager_observer.h" #include "chrome/browser/profiles/profile_manager_observer.h"
#include "chrome/browser/signin/identity_manager_factory.h" #include "chrome/browser/signin/identity_manager_factory.h"
...@@ -24,6 +27,8 @@ ...@@ -24,6 +27,8 @@
namespace { namespace {
const char kProfileTestName[] = "profile_test_name";
std::unique_ptr<TestingProfile> BuildTestingProfile(const base::FilePath& path, std::unique_ptr<TestingProfile> BuildTestingProfile(const base::FilePath& path,
Profile::Delegate* delegate, Profile::Delegate* delegate,
bool tokens_loaded) { bool tokens_loaded) {
...@@ -158,7 +163,7 @@ TEST_F(DiceSignedInProfileCreatorTest, CreateWithTokensLoaded) { ...@@ -158,7 +163,7 @@ TEST_F(DiceSignedInProfileCreatorTest, CreateWithTokensLoaded) {
base::RunLoop loop; base::RunLoop loop;
std::unique_ptr<DiceSignedInProfileCreator> creator = std::unique_ptr<DiceSignedInProfileCreator> creator =
std::make_unique<DiceSignedInProfileCreator>( std::make_unique<DiceSignedInProfileCreator>(
profile(), account_info.account_id, profile(), account_info.account_id, kProfileTestName,
base::BindOnce(&DiceSignedInProfileCreatorTest::OnProfileCreated, base::BindOnce(&DiceSignedInProfileCreatorTest::OnProfileCreated,
base::Unretained(this), loop.QuitClosure())); base::Unretained(this), loop.QuitClosure()));
loop.Run(); loop.Run();
...@@ -175,6 +180,15 @@ TEST_F(DiceSignedInProfileCreatorTest, CreateWithTokensLoaded) { ...@@ -175,6 +180,15 @@ TEST_F(DiceSignedInProfileCreatorTest, CreateWithTokensLoaded) {
.size()); .size());
EXPECT_TRUE(IdentityManagerFactory::GetForProfile(signed_in_profile()) EXPECT_TRUE(IdentityManagerFactory::GetForProfile(signed_in_profile())
->HasAccountWithRefreshToken(account_info.account_id)); ->HasAccountWithRefreshToken(account_info.account_id));
// Check the profile name.
ProfileAttributesEntry* entry = nullptr;
ProfileAttributesStorage& storage =
profile_manager()->GetProfileAttributesStorage();
ASSERT_TRUE(storage.GetProfileAttributesWithPath(
signed_in_profile()->GetPath(), &entry));
ASSERT_TRUE(entry);
EXPECT_EQ(base::UTF8ToUTF16(kProfileTestName), entry->GetLocalProfileName());
} }
TEST_F(DiceSignedInProfileCreatorTest, CreateWithTokensNotLoaded) { TEST_F(DiceSignedInProfileCreatorTest, CreateWithTokensNotLoaded) {
...@@ -187,7 +201,7 @@ TEST_F(DiceSignedInProfileCreatorTest, CreateWithTokensNotLoaded) { ...@@ -187,7 +201,7 @@ TEST_F(DiceSignedInProfileCreatorTest, CreateWithTokensNotLoaded) {
set_profile_added_closure(profile_added_loop.QuitClosure()); set_profile_added_closure(profile_added_loop.QuitClosure());
std::unique_ptr<DiceSignedInProfileCreator> creator = std::unique_ptr<DiceSignedInProfileCreator> creator =
std::make_unique<DiceSignedInProfileCreator>( std::make_unique<DiceSignedInProfileCreator>(
profile(), account_info.account_id, profile(), account_info.account_id, kProfileTestName,
base::BindOnce(&DiceSignedInProfileCreatorTest::OnProfileCreated, base::BindOnce(&DiceSignedInProfileCreatorTest::OnProfileCreated,
base::Unretained(this), creator_loop.QuitClosure())); base::Unretained(this), creator_loop.QuitClosure()));
profile_added_loop.Run(); profile_added_loop.Run();
...@@ -222,7 +236,7 @@ TEST_F(DiceSignedInProfileCreatorTest, DeleteWhileCreating) { ...@@ -222,7 +236,7 @@ TEST_F(DiceSignedInProfileCreatorTest, DeleteWhileCreating) {
identity_test_env()->MakeAccountAvailable("bob@example.com"); identity_test_env()->MakeAccountAvailable("bob@example.com");
std::unique_ptr<DiceSignedInProfileCreator> creator = std::unique_ptr<DiceSignedInProfileCreator> creator =
std::make_unique<DiceSignedInProfileCreator>( std::make_unique<DiceSignedInProfileCreator>(
profile(), account_info.account_id, profile(), account_info.account_id, kProfileTestName,
base::BindOnce(&DiceSignedInProfileCreatorTest::OnProfileCreated, base::BindOnce(&DiceSignedInProfileCreatorTest::OnProfileCreated,
base::Unretained(this), base::OnceClosure())); base::Unretained(this), base::OnceClosure()));
EXPECT_FALSE(creator_callback_called()); EXPECT_FALSE(creator_callback_called());
...@@ -241,7 +255,7 @@ TEST_F(DiceSignedInProfileCreatorTest, DeleteProfile) { ...@@ -241,7 +255,7 @@ TEST_F(DiceSignedInProfileCreatorTest, DeleteProfile) {
set_profile_added_closure(profile_added_loop.QuitClosure()); set_profile_added_closure(profile_added_loop.QuitClosure());
std::unique_ptr<DiceSignedInProfileCreator> creator = std::unique_ptr<DiceSignedInProfileCreator> creator =
std::make_unique<DiceSignedInProfileCreator>( std::make_unique<DiceSignedInProfileCreator>(
profile(), account_info.account_id, profile(), account_info.account_id, kProfileTestName,
base::BindOnce(&DiceSignedInProfileCreatorTest::OnProfileCreated, base::BindOnce(&DiceSignedInProfileCreatorTest::OnProfileCreated,
base::Unretained(this), creator_loop.QuitClosure())); base::Unretained(this), creator_loop.QuitClosure()));
profile_added_loop.Run(); profile_added_loop.Run();
......
...@@ -265,11 +265,23 @@ void DiceWebSigninInterceptor::OnProfileCreationChoice(bool create) { ...@@ -265,11 +265,23 @@ void DiceWebSigninInterceptor::OnProfileCreationChoice(bool create) {
return; return;
} }
std::string profile_name;
base::Optional<AccountInfo> account_info =
identity_manager_
->FindExtendedAccountInfoForAccountWithRefreshTokenByAccountId(
account_id_);
if (account_info) {
bool is_managed = !account_info->hosted_domain.empty() &&
account_info->hosted_domain != kNoHostedDomainFound;
profile_name =
is_managed ? account_info->hosted_domain : account_info->given_name;
}
DCHECK(!dice_signed_in_profile_creator_); DCHECK(!dice_signed_in_profile_creator_);
// Unretained is fine because the profile creator is owned by this. // Unretained is fine because the profile creator is owned by this.
dice_signed_in_profile_creator_ = dice_signed_in_profile_creator_ =
std::make_unique<DiceSignedInProfileCreator>( std::make_unique<DiceSignedInProfileCreator>(
profile_, account_id_, profile_, account_id_, profile_name,
base::BindOnce(&DiceWebSigninInterceptor::OnNewSignedInProfileCreated, base::BindOnce(&DiceWebSigninInterceptor::OnNewSignedInProfileCreated,
base::Unretained(this))); base::Unretained(this)));
} }
......
...@@ -5,7 +5,10 @@ ...@@ -5,7 +5,10 @@
#include "chrome/browser/signin/dice_web_signin_interceptor.h" #include "chrome/browser/signin/dice_web_signin_interceptor.h"
#include "base/scoped_observer.h" #include "base/scoped_observer.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/browser_process.h" #include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile_attributes_entry.h"
#include "chrome/browser/profiles/profile_attributes_storage.h"
#include "chrome/browser/profiles/profile_manager.h" #include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profile_manager_observer.h" #include "chrome/browser/profiles/profile_manager_observer.h"
#include "chrome/browser/signin/chrome_signin_client_factory.h" #include "chrome/browser/signin/chrome_signin_client_factory.h"
...@@ -181,6 +184,15 @@ IN_PROC_BROWSER_TEST_F(DiceWebSigninInterceptorBrowserTest, InterceptionTest) { ...@@ -181,6 +184,15 @@ IN_PROC_BROWSER_TEST_F(DiceWebSigninInterceptorBrowserTest, InterceptionTest) {
EXPECT_TRUE(new_identity_manager->HasAccountWithRefreshToken( EXPECT_TRUE(new_identity_manager->HasAccountWithRefreshToken(
account_info.account_id)); account_info.account_id));
// Check the profile name.
ProfileAttributesEntry* entry = nullptr;
ProfileAttributesStorage& storage =
g_browser_process->profile_manager()->GetProfileAttributesStorage();
ASSERT_TRUE(
storage.GetProfileAttributesWithPath(new_profile->GetPath(), &entry));
ASSERT_TRUE(entry);
EXPECT_EQ("givenname", base::UTF16ToUTF8(entry->GetLocalProfileName()));
// Add the account to the cookies (simulates the account reconcilor). // Add the account to the cookies (simulates the account reconcilor).
EXPECT_EQ(BrowserList::GetInstance()->size(), 1u); EXPECT_EQ(BrowserList::GetInstance()->size(), 1u);
signin::SetCookieAccounts(new_identity_manager, test_url_loader_factory(), signin::SetCookieAccounts(new_identity_manager, test_url_loader_factory(),
......
...@@ -371,6 +371,7 @@ void DiceTurnSyncOnHelper::CreateNewSignedInProfile() { ...@@ -371,6 +371,7 @@ void DiceTurnSyncOnHelper::CreateNewSignedInProfile() {
dice_signed_in_profile_creator_ = dice_signed_in_profile_creator_ =
std::make_unique<DiceSignedInProfileCreator>( std::make_unique<DiceSignedInProfileCreator>(
profile_, account_info_.account_id, profile_, account_info_.account_id,
/*local_profile_name=*/std::string(),
base::BindOnce(&DiceTurnSyncOnHelper::OnNewSignedInProfileCreated, base::BindOnce(&DiceTurnSyncOnHelper::OnNewSignedInProfileCreated,
base::Unretained(this))); base::Unretained(this)));
} }
......
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